@m4ike1/ion-tui 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (196) hide show
  1. package/README.md +114 -0
  2. package/dist/alt-screen-search.d.ts +46 -0
  3. package/dist/alt-screen-search.d.ts.map +1 -0
  4. package/dist/alt-screen-search.js +282 -0
  5. package/dist/alt-screen-search.js.map +1 -0
  6. package/dist/autocomplete.d.ts +75 -0
  7. package/dist/autocomplete.d.ts.map +1 -0
  8. package/dist/autocomplete.js +749 -0
  9. package/dist/autocomplete.js.map +1 -0
  10. package/dist/components/alt-screen-flash.d.ts +13 -0
  11. package/dist/components/alt-screen-flash.d.ts.map +1 -0
  12. package/dist/components/alt-screen-flash.js +37 -0
  13. package/dist/components/alt-screen-flash.js.map +1 -0
  14. package/dist/components/box.d.ts +24 -0
  15. package/dist/components/box.d.ts.map +1 -0
  16. package/dist/components/box.js +133 -0
  17. package/dist/components/box.js.map +1 -0
  18. package/dist/components/cancellable-loader.d.ts +22 -0
  19. package/dist/components/cancellable-loader.d.ts.map +1 -0
  20. package/dist/components/cancellable-loader.js +35 -0
  21. package/dist/components/cancellable-loader.js.map +1 -0
  22. package/dist/components/editor.d.ts +266 -0
  23. package/dist/components/editor.d.ts.map +1 -0
  24. package/dist/components/editor.js +2087 -0
  25. package/dist/components/editor.js.map +1 -0
  26. package/dist/components/h-stack.d.ts +7 -0
  27. package/dist/components/h-stack.d.ts.map +1 -0
  28. package/dist/components/h-stack.js +43 -0
  29. package/dist/components/h-stack.js.map +1 -0
  30. package/dist/components/image.d.ts +28 -0
  31. package/dist/components/image.d.ts.map +1 -0
  32. package/dist/components/image.js +90 -0
  33. package/dist/components/image.js.map +1 -0
  34. package/dist/components/input.d.ts +48 -0
  35. package/dist/components/input.d.ts.map +1 -0
  36. package/dist/components/input.js +417 -0
  37. package/dist/components/input.js.map +1 -0
  38. package/dist/components/loader.d.ts +33 -0
  39. package/dist/components/loader.d.ts.map +1 -0
  40. package/dist/components/loader.js +76 -0
  41. package/dist/components/loader.js.map +1 -0
  42. package/dist/components/markdown.d.ts +103 -0
  43. package/dist/components/markdown.d.ts.map +1 -0
  44. package/dist/components/markdown.js +858 -0
  45. package/dist/components/markdown.js.map +1 -0
  46. package/dist/components/mouse-region.d.ts +12 -0
  47. package/dist/components/mouse-region.d.ts.map +1 -0
  48. package/dist/components/mouse-region.js +21 -0
  49. package/dist/components/mouse-region.js.map +1 -0
  50. package/dist/components/scroll-view.d.ts +68 -0
  51. package/dist/components/scroll-view.d.ts.map +1 -0
  52. package/dist/components/scroll-view.js +199 -0
  53. package/dist/components/scroll-view.js.map +1 -0
  54. package/dist/components/select-list.d.ts +53 -0
  55. package/dist/components/select-list.d.ts.map +1 -0
  56. package/dist/components/select-list.js +206 -0
  57. package/dist/components/select-list.js.map +1 -0
  58. package/dist/components/settings-list.d.ts +60 -0
  59. package/dist/components/settings-list.d.ts.map +1 -0
  60. package/dist/components/settings-list.js +252 -0
  61. package/dist/components/settings-list.js.map +1 -0
  62. package/dist/components/spacer.d.ts +12 -0
  63. package/dist/components/spacer.d.ts.map +1 -0
  64. package/dist/components/spacer.js +23 -0
  65. package/dist/components/spacer.js.map +1 -0
  66. package/dist/components/stack.d.ts +32 -0
  67. package/dist/components/stack.d.ts.map +1 -0
  68. package/dist/components/stack.js +111 -0
  69. package/dist/components/stack.js.map +1 -0
  70. package/dist/components/text.d.ts +19 -0
  71. package/dist/components/text.d.ts.map +1 -0
  72. package/dist/components/text.js +90 -0
  73. package/dist/components/text.js.map +1 -0
  74. package/dist/components/truncated-text.d.ts +13 -0
  75. package/dist/components/truncated-text.d.ts.map +1 -0
  76. package/dist/components/truncated-text.js +51 -0
  77. package/dist/components/truncated-text.js.map +1 -0
  78. package/dist/components/v-stack.d.ts +8 -0
  79. package/dist/components/v-stack.d.ts.map +1 -0
  80. package/dist/components/v-stack.js +26 -0
  81. package/dist/components/v-stack.js.map +1 -0
  82. package/dist/components/virtual-list.d.ts +51 -0
  83. package/dist/components/virtual-list.d.ts.map +1 -0
  84. package/dist/components/virtual-list.js +170 -0
  85. package/dist/components/virtual-list.js.map +1 -0
  86. package/dist/editor-component.d.ts +39 -0
  87. package/dist/editor-component.d.ts.map +1 -0
  88. package/dist/editor-component.js +2 -0
  89. package/dist/editor-component.js.map +1 -0
  90. package/dist/fuzzy.d.ts +16 -0
  91. package/dist/fuzzy.d.ts.map +1 -0
  92. package/dist/fuzzy.js +110 -0
  93. package/dist/fuzzy.js.map +1 -0
  94. package/dist/index.d.ts +34 -0
  95. package/dist/index.d.ts.map +1 -0
  96. package/dist/index.js +46 -0
  97. package/dist/index.js.map +1 -0
  98. package/dist/keybindings.d.ts +366 -0
  99. package/dist/keybindings.d.ts.map +1 -0
  100. package/dist/keybindings.js +331 -0
  101. package/dist/keybindings.js.map +1 -0
  102. package/dist/keys.d.ts +184 -0
  103. package/dist/keys.d.ts.map +1 -0
  104. package/dist/keys.js +1174 -0
  105. package/dist/keys.js.map +1 -0
  106. package/dist/kill-ring.d.ts +28 -0
  107. package/dist/kill-ring.d.ts.map +1 -0
  108. package/dist/kill-ring.js +44 -0
  109. package/dist/kill-ring.js.map +1 -0
  110. package/dist/latex.d.ts +10 -0
  111. package/dist/latex.d.ts.map +1 -0
  112. package/dist/latex.js +1285 -0
  113. package/dist/latex.js.map +1 -0
  114. package/dist/layout-node.d.ts +59 -0
  115. package/dist/layout-node.d.ts.map +1 -0
  116. package/dist/layout-node.js +12 -0
  117. package/dist/layout-node.js.map +1 -0
  118. package/dist/layout.d.ts +42 -0
  119. package/dist/layout.d.ts.map +1 -0
  120. package/dist/layout.js +404 -0
  121. package/dist/layout.js.map +1 -0
  122. package/dist/native-modifiers.d.ts +4 -0
  123. package/dist/native-modifiers.d.ts.map +1 -0
  124. package/dist/native-modifiers.js +13 -0
  125. package/dist/native-modifiers.js.map +1 -0
  126. package/dist/native-module-path.d.ts +7 -0
  127. package/dist/native-module-path.d.ts.map +1 -0
  128. package/dist/native-module-path.js +19 -0
  129. package/dist/native-module-path.js.map +1 -0
  130. package/dist/native-platform.d.ts +18 -0
  131. package/dist/native-platform.d.ts.map +1 -0
  132. package/dist/native-platform.js +42 -0
  133. package/dist/native-platform.js.map +1 -0
  134. package/dist/pty-host.d.ts +224 -0
  135. package/dist/pty-host.d.ts.map +1 -0
  136. package/dist/pty-host.js +347 -0
  137. package/dist/pty-host.js.map +1 -0
  138. package/dist/stdin-buffer.d.ts +56 -0
  139. package/dist/stdin-buffer.d.ts.map +1 -0
  140. package/dist/stdin-buffer.js +366 -0
  141. package/dist/stdin-buffer.js.map +1 -0
  142. package/dist/terminal-colors.d.ts +10 -0
  143. package/dist/terminal-colors.d.ts.map +1 -0
  144. package/dist/terminal-colors.js +59 -0
  145. package/dist/terminal-colors.js.map +1 -0
  146. package/dist/terminal-image.d.ts +118 -0
  147. package/dist/terminal-image.d.ts.map +1 -0
  148. package/dist/terminal-image.js +550 -0
  149. package/dist/terminal-image.js.map +1 -0
  150. package/dist/terminal.d.ts +123 -0
  151. package/dist/terminal.d.ts.map +1 -0
  152. package/dist/terminal.js +442 -0
  153. package/dist/terminal.js.map +1 -0
  154. package/dist/tui-alt-screen.d.ts +165 -0
  155. package/dist/tui-alt-screen.d.ts.map +1 -0
  156. package/dist/tui-alt-screen.js +1515 -0
  157. package/dist/tui-alt-screen.js.map +1 -0
  158. package/dist/tui-main-screen.d.ts +39 -0
  159. package/dist/tui-main-screen.d.ts.map +1 -0
  160. package/dist/tui-main-screen.js +594 -0
  161. package/dist/tui-main-screen.js.map +1 -0
  162. package/dist/tui.d.ts +431 -0
  163. package/dist/tui.d.ts.map +1 -0
  164. package/dist/tui.js +1142 -0
  165. package/dist/tui.js.map +1 -0
  166. package/dist/undo-stack.d.ts +17 -0
  167. package/dist/undo-stack.d.ts.map +1 -0
  168. package/dist/undo-stack.js +25 -0
  169. package/dist/undo-stack.js.map +1 -0
  170. package/dist/utils.d.ts +98 -0
  171. package/dist/utils.d.ts.map +1 -0
  172. package/dist/utils.js +1200 -0
  173. package/dist/utils.js.map +1 -0
  174. package/dist/word-navigation.d.ts +25 -0
  175. package/dist/word-navigation.d.ts.map +1 -0
  176. package/dist/word-navigation.js +96 -0
  177. package/dist/word-navigation.js.map +1 -0
  178. package/native/clipboard.h +170 -0
  179. package/native/darwin/README.md +22 -0
  180. package/native/darwin/build.sh +69 -0
  181. package/native/darwin/prebuilds/darwin-arm64/darwin-platform.node +0 -0
  182. package/native/darwin/prebuilds/darwin-x64/darwin-platform.node +0 -0
  183. package/native/darwin/src/darwin-platform.m +93 -0
  184. package/native/linux/README.md +23 -0
  185. package/native/linux/build.sh +65 -0
  186. package/native/linux/prebuilds/linux-arm64/linux-platform-x11.node +0 -0
  187. package/native/linux/prebuilds/linux-x64/linux-platform-x11.node +0 -0
  188. package/native/linux/src/clipboard-worker.h +70 -0
  189. package/native/linux/src/linux-platform-x11.c +274 -0
  190. package/native/napi.h +89 -0
  191. package/native/win32/README.md +31 -0
  192. package/native/win32/build.mjs +232 -0
  193. package/native/win32/prebuilds/win32-arm64/win32-platform.node +0 -0
  194. package/native/win32/prebuilds/win32-x64/win32-platform.node +0 -0
  195. package/native/win32/src/win32-platform.c +241 -0
  196. package/package.json +62 -0
@@ -0,0 +1,93 @@
1
+ #import <AppKit/AppKit.h>
2
+ #import <CoreGraphics/CoreGraphics.h>
3
+ #import <Foundation/Foundation.h>
4
+ #include <stdlib.h>
5
+ #include <string.h>
6
+ #define PI_CLIPBOARD_WRITE
7
+ #include "../../clipboard.h"
8
+
9
+ static CGEventFlags modifier_mask_for_name(const char* name) {
10
+ if (strcmp(name, "shift") == 0) return kCGEventFlagMaskShift;
11
+ if (strcmp(name, "command") == 0) return kCGEventFlagMaskCommand;
12
+ if (strcmp(name, "control") == 0) return kCGEventFlagMaskControl;
13
+ if (strcmp(name, "option") == 0) return kCGEventFlagMaskAlternate;
14
+ return 0;
15
+ }
16
+
17
+ static napi_value is_modifier_pressed(napi_env env, napi_callback_info info) {
18
+ napi_get_cb_info_fn napi_get_cb_info = (napi_get_cb_info_fn)node_symbol("napi_get_cb_info");
19
+ napi_get_value_string_utf8_fn napi_get_value_string_utf8 =
20
+ (napi_get_value_string_utf8_fn)node_symbol("napi_get_value_string_utf8");
21
+ napi_get_boolean_fn napi_get_boolean = (napi_get_boolean_fn)node_symbol("napi_get_boolean");
22
+
23
+ bool pressed = false;
24
+ if (napi_get_cb_info && napi_get_value_string_utf8) {
25
+ size_t argc = 1;
26
+ napi_value args[1] = {0};
27
+ if (napi_get_cb_info(env, info, &argc, args, 0, 0) == 0 && argc >= 1 && args[0]) {
28
+ char name[16] = {0};
29
+ size_t copied = 0;
30
+ if (napi_get_value_string_utf8(env, args[0], name, sizeof(name), &copied) == 0) {
31
+ CGEventFlags mask = modifier_mask_for_name(name);
32
+ if (mask != 0) {
33
+ CGEventFlags flags = CGEventSourceFlagsState(kCGEventSourceStateCombinedSessionState);
34
+ pressed = (flags & mask) != 0;
35
+ }
36
+ }
37
+ }
38
+ }
39
+
40
+ napi_value result = 0;
41
+ if (!napi_get_boolean || napi_get_boolean(env, pressed, &result) != 0) {
42
+ return fail(env, "Could not inspect modifier state");
43
+ }
44
+ return result;
45
+ }
46
+
47
+ static void clipboard_execute(clipboard_job* job) {
48
+ @autoreleasepool {
49
+ NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
50
+ if (!pasteboard) {
51
+ job->error = "Could not open clipboard";
52
+ } else if (job->operation == CLIPBOARD_WRITE) {
53
+ NSString* text = [[NSString alloc] initWithBytes:job->data length:job->length encoding:NSUTF8StringEncoding];
54
+ if (!text) {
55
+ job->error = "Clipboard text is not valid UTF-8";
56
+ return;
57
+ }
58
+ [pasteboard clearContents];
59
+ if (![pasteboard setString:text forType:NSPasteboardTypeString]) job->error = "Could not set clipboard text";
60
+ } else if (job->operation == CLIPBOARD_TEXT) {
61
+ NSString* text = [pasteboard stringForType:NSPasteboardTypeString];
62
+ if (!text) {
63
+ job->format = CLIPBOARD_EMPTY;
64
+ } else if (!text.UTF8String) {
65
+ job->error = "Could not encode clipboard text";
66
+ } else {
67
+ clipboard_copy(job, text.UTF8String, [text lengthOfBytesUsingEncoding:NSUTF8StringEncoding], CLIPBOARD_UTF8);
68
+ }
69
+ } else {
70
+ if (![pasteboard availableTypeFromArray:@[ NSPasteboardTypePNG, NSPasteboardTypeTIFF ]]) {
71
+ job->format = CLIPBOARD_EMPTY;
72
+ return;
73
+ }
74
+ NSData* png = [pasteboard dataForType:NSPasteboardTypePNG];
75
+ if (!png) {
76
+ NSImage* image = [[NSImage alloc] initWithPasteboard:pasteboard];
77
+ NSData* tiff = image.TIFFRepresentation;
78
+ NSBitmapImageRep* bitmap = tiff ? [NSBitmapImageRep imageRepWithData:tiff] : nil;
79
+ png = bitmap ? [bitmap representationUsingType:NSBitmapImageFileTypePNG properties:@{}] : nil;
80
+ }
81
+ if (png) clipboard_copy(job, png.bytes, png.length, CLIPBOARD_BUFFER);
82
+ else job->error = "Clipboard does not contain an image";
83
+ }
84
+ }
85
+ }
86
+
87
+ PI_NAPI_EXPORT napi_value napi_register_module_v1(napi_env env, napi_value exports) {
88
+ set_function_export(env, exports, "isModifierPressed", is_modifier_pressed);
89
+ set_function_export(env, exports, "getText", get_clipboard_text);
90
+ set_function_export(env, exports, "setText", set_clipboard_text);
91
+ set_function_export(env, exports, "getImage", get_clipboard_image);
92
+ return exports;
93
+ }
@@ -0,0 +1,23 @@
1
+ # Linux clipboard helper
2
+
3
+ Provides asynchronous X11 text and image reads using `libxcb.so.1`. Prebuilds support x64 and arm64 on glibc and musl. Reads have bounded waits; if a native operation stalls, the helper remains unavailable until it finishes.
4
+
5
+ Coding-agent falls back to command-line tools when native reads are unavailable. Wayland reads use `wl-paste`; all Linux writes use the existing command-line or terminal clipboard paths.
6
+
7
+ ## Building
8
+
9
+ Install a C compiler and XCB development headers, then run from the repository root on each supported Linux architecture:
10
+
11
+ ```sh
12
+ npm --prefix packages/tui run build:native:linux
13
+ ```
14
+
15
+ ## Testing
16
+
17
+ Install the build dependencies plus `pkg-config`, `Xvfb`, and `xclip`, then run from `packages/tui`:
18
+
19
+ ```sh
20
+ node --test test/native-clipboard-linux.test.ts
21
+ ```
22
+
23
+ Tests use isolated X11 servers, not the desktop clipboard. They skip when dependencies are missing.
@@ -0,0 +1,65 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -euo pipefail
4
+
5
+ script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6
+ compiler="${CC:-cc}"
7
+
8
+ if [[ "$(uname -s)" != "Linux" ]]; then
9
+ echo "Linux native helpers must be built on Linux" >&2
10
+ exit 1
11
+ fi
12
+
13
+ case "$(uname -m)" in
14
+ x86_64)
15
+ arch="x64"
16
+ ;;
17
+ aarch64|arm64)
18
+ arch="arm64"
19
+ ;;
20
+ *)
21
+ echo "Unsupported Linux architecture: $(uname -m)" >&2
22
+ exit 1
23
+ ;;
24
+ esac
25
+
26
+ if ! command -v "$compiler" >/dev/null 2>&1; then
27
+ echo "Linux C compiler not found: $compiler" >&2
28
+ exit 1
29
+ fi
30
+ build_dir="$(mktemp -d "${TMPDIR:-/tmp}/ion-tui-linux.XXXXXX")"
31
+ trap 'rm -rf "$build_dir"' EXIT
32
+
33
+ common_flags=(
34
+ -std=c11
35
+ -D_POSIX_C_SOURCE=200809L
36
+ -Wall
37
+ -Wextra
38
+ -Werror
39
+ -Os
40
+ -flto
41
+ -fPIC
42
+ -pthread
43
+ -fvisibility=hidden
44
+ -fno-stack-protector
45
+ -shared
46
+ -nostdlib
47
+ -Wl,--unresolved-symbols=ignore-all
48
+ -Wl,--no-as-needed
49
+ -Wl,-O2
50
+ -Wl,--gc-sections
51
+ # A timed-out private thread may outlive the Node environment that loaded us.
52
+ -Wl,-z,nodelete
53
+ -Wl,-s
54
+ )
55
+
56
+ x11_output="$build_dir/linux-platform-x11.node"
57
+ "$compiler" "${common_flags[@]}" \
58
+ "$script_dir/src/linux-platform-x11.c" \
59
+ -lxcb \
60
+ -o "$x11_output"
61
+
62
+ output_dir="$script_dir/prebuilds/linux-$arch"
63
+ mkdir -p "$output_dir"
64
+ install -m 755 "$x11_output" "$output_dir/linux-platform-x11.node"
65
+ printf 'Built %s\n' "$output_dir/linux-platform-x11.node"
@@ -0,0 +1,70 @@
1
+ #include <pthread.h>
2
+ #include <time.h>
3
+ #include "../../clipboard.h"
4
+
5
+ static void read_clipboard(clipboard_job* job);
6
+ static pthread_mutex_t worker_mutex = PTHREAD_MUTEX_INITIALIZER;
7
+ static pthread_once_t worker_once = PTHREAD_ONCE_INIT;
8
+ static pthread_cond_t worker_changed;
9
+ static bool worker_ready, worker_busy, worker_waiting, worker_finished;
10
+ static clipboard_job worker_result;
11
+ static int (*signal_worker)(pthread_cond_t*);
12
+ static int (*wait_for_worker)(pthread_cond_t*, pthread_mutex_t*, const struct timespec*);
13
+
14
+ static void initialize_worker_condition(void) {
15
+ // Unversioned ELF imports otherwise bind obsolete condition variables on glibc x64.
16
+ int (*initialize)(pthread_cond_t*, const pthread_condattr_t*) = node_symbol("pthread_cond_init");
17
+ signal_worker = node_symbol("pthread_cond_signal");
18
+ wait_for_worker = node_symbol("pthread_cond_timedwait");
19
+ if (!initialize || !signal_worker || !wait_for_worker) return;
20
+ pthread_condattr_t attributes;
21
+ if (pthread_condattr_init(&attributes) != 0) return;
22
+ worker_ready = pthread_condattr_setclock(&attributes, CLOCK_MONOTONIC) == 0 &&
23
+ initialize(&worker_changed, &attributes) == 0;
24
+ pthread_condattr_destroy(&attributes);
25
+ }
26
+
27
+ static void* read_on_private_thread(void* unused) {
28
+ (void)unused;
29
+ read_clipboard(&worker_result);
30
+ pthread_mutex_lock(&worker_mutex);
31
+ worker_finished = true;
32
+ if (!worker_waiting) {
33
+ free(worker_result.data); // The caller timed out; discard the late result.
34
+ worker_busy = false;
35
+ }
36
+ signal_worker(&worker_changed);
37
+ pthread_mutex_unlock(&worker_mutex);
38
+ return 0;
39
+ }
40
+
41
+ static void clipboard_execute(clipboard_job* job) {
42
+ pthread_once(&worker_once, initialize_worker_condition);
43
+ if (!worker_ready) return;
44
+ pthread_mutex_lock(&worker_mutex);
45
+ if (!worker_busy) {
46
+ worker_busy = worker_waiting = true;
47
+ worker_finished = false;
48
+ worker_result = (clipboard_job){.operation = job->operation};
49
+ struct timespec deadline;
50
+ clock_gettime(CLOCK_MONOTONIC, &deadline);
51
+ deadline.tv_sec += 3; // Allow the two-second transfer deadline to report its own error first.
52
+ pthread_t thread;
53
+ if (pthread_create(&thread, 0, read_on_private_thread, 0) == 0) {
54
+ pthread_detach(thread);
55
+ // Only this bounded wait occupies libuv. Other callers return unavailable.
56
+ while (!worker_finished && wait_for_worker(&worker_changed, &worker_mutex, &deadline) == 0) {}
57
+ if (worker_finished) {
58
+ job->data = worker_result.data;
59
+ job->length = worker_result.length;
60
+ job->format = worker_result.format;
61
+ job->error = worker_result.error;
62
+ worker_busy = false;
63
+ }
64
+ } else {
65
+ worker_busy = false;
66
+ }
67
+ worker_waiting = false;
68
+ }
69
+ pthread_mutex_unlock(&worker_mutex);
70
+ }
@@ -0,0 +1,274 @@
1
+ #include <xcb/xcb.h>
2
+ #include <string.h>
3
+ #include <xcb/xcbext.h>
4
+ #include <errno.h>
5
+ #include <poll.h>
6
+ #include <time.h>
7
+ #include "clipboard-worker.h"
8
+
9
+ #define MAX_CLIPBOARD_BYTES (50u * 1024u * 1024u)
10
+ #define CLIPBOARD_TIMEOUT_MS 2000
11
+
12
+ static const char* const text_types[] = {
13
+ "text/plain;charset=utf-8", "text/plain;charset=UTF-8", "UTF8_STRING", "text/plain", "STRING",
14
+ };
15
+ static const char* const image_types[] = {
16
+ "image/png", "image/jpeg", "image/webp", "image/gif", "image/bmp", "image/tiff",
17
+ };
18
+
19
+ static int64_t monotonic_ms(void) {
20
+ struct timespec now;
21
+ clock_gettime(CLOCK_MONOTONIC, &now);
22
+ return (int64_t)now.tv_sec * 1000 + now.tv_nsec / 1000000;
23
+ }
24
+
25
+ typedef struct {
26
+ unsigned char* data;
27
+ size_t length;
28
+ uint32_t items;
29
+ uint8_t format;
30
+ xcb_atom_t type;
31
+ } property_data;
32
+
33
+ typedef struct {
34
+ xcb_connection_t* connection;
35
+ xcb_window_t window;
36
+ xcb_atom_t clipboard;
37
+ xcb_atom_t property;
38
+ xcb_atom_t targets;
39
+ xcb_atom_t incr;
40
+ int64_t deadline;
41
+ } x11_clipboard;
42
+
43
+ static bool append_bytes(property_data* result, const unsigned char* bytes, size_t length) {
44
+ if (length > MAX_CLIPBOARD_BYTES - result->length) return false;
45
+ unsigned char* data = realloc(result->data, result->length + length + 1);
46
+ if (!data) return false;
47
+ memcpy(data + result->length, bytes, length);
48
+ result->length += length;
49
+ data[result->length] = 0;
50
+ result->data = data;
51
+ return true;
52
+ }
53
+
54
+ // Share one deadline across discovery, replies, and incremental chunks.
55
+ // Connection setup and flushing are libxcb calls on the private thread.
56
+ static bool wait_for_input(x11_clipboard* clipboard) {
57
+ for (;;) {
58
+ int64_t remaining = clipboard->deadline - monotonic_ms();
59
+ if (remaining <= 0 || xcb_connection_has_error(clipboard->connection)) return false;
60
+ struct pollfd descriptor = {xcb_get_file_descriptor(clipboard->connection), POLLIN, 0};
61
+ int ready = poll(&descriptor, 1, (int)remaining);
62
+ if (ready >= 0) return ready && (descriptor.revents & POLLIN);
63
+ if (errno != EINTR) return false;
64
+ }
65
+ }
66
+
67
+ static void* wait_for_reply(x11_clipboard* clipboard, unsigned sequence) {
68
+ if (xcb_flush(clipboard->connection) <= 0) return 0;
69
+ while (monotonic_ms() < clipboard->deadline) {
70
+ void* reply = 0;
71
+ xcb_generic_error_t* error = 0;
72
+ if (xcb_poll_for_reply(clipboard->connection, sequence, &reply, &error)) {
73
+ free(error);
74
+ return reply;
75
+ }
76
+ if (!wait_for_input(clipboard)) break;
77
+ }
78
+ xcb_discard_reply(clipboard->connection, sequence);
79
+ return 0;
80
+ }
81
+
82
+ static xcb_generic_event_t* wait_for_event(x11_clipboard* clipboard, uint8_t type) {
83
+ if (xcb_flush(clipboard->connection) <= 0) return 0;
84
+ while (monotonic_ms() < clipboard->deadline) {
85
+ xcb_generic_event_t* event = xcb_poll_for_event(clipboard->connection);
86
+ if (event) {
87
+ if ((event->response_type & 0x7f) == type) return event;
88
+ free(event);
89
+ } else if (!wait_for_input(clipboard)) {
90
+ break;
91
+ }
92
+ }
93
+ return 0;
94
+ }
95
+
96
+ static bool read_property(x11_clipboard* clipboard, bool remove, property_data* result) {
97
+ xcb_get_property_cookie_t cookie = xcb_get_property(
98
+ clipboard->connection, remove, clipboard->window, clipboard->property,
99
+ XCB_GET_PROPERTY_TYPE_ANY, 0, MAX_CLIPBOARD_BYTES / 4
100
+ );
101
+ xcb_get_property_reply_t* reply = wait_for_reply(clipboard, cookie.sequence);
102
+ if (!reply) return false;
103
+ bool copied = false;
104
+ bool valid_format = reply->format == 8 || reply->format == 16 || reply->format == 32;
105
+ uint64_t length = (uint64_t)reply->value_len * (reply->format / 8);
106
+ if (reply->bytes_after == 0 && reply->type != XCB_NONE && valid_format &&
107
+ length <= MAX_CLIPBOARD_BYTES && length <= (uint64_t)reply->length * 4) {
108
+ result->type = reply->type;
109
+ result->format = reply->format;
110
+ result->items = reply->value_len;
111
+ copied = append_bytes(result, xcb_get_property_value(reply), (size_t)length);
112
+ }
113
+ free(reply);
114
+ return copied;
115
+ }
116
+
117
+ static bool append_property(property_data* result, const property_data* chunk) {
118
+ if ((chunk->format != 8 && chunk->format != 16 && chunk->format != 32) ||
119
+ chunk->type == XCB_NONE ||
120
+ (uint64_t)chunk->items * (chunk->format / 8) != chunk->length ||
121
+ (result->type != XCB_NONE && (result->type != chunk->type || result->format != chunk->format)) ||
122
+ chunk->items > UINT32_MAX - result->items) return false;
123
+ if (!append_bytes(result, chunk->data, chunk->length)) return false;
124
+ result->type = chunk->type;
125
+ result->format = chunk->format;
126
+ result->items += chunk->items;
127
+ return true;
128
+ }
129
+
130
+ static bool request_selection(x11_clipboard* clipboard, xcb_atom_t target, property_data* result) {
131
+ memset(result, 0, sizeof(*result));
132
+ xcb_delete_property(clipboard->connection, clipboard->window, clipboard->property);
133
+ xcb_convert_selection(
134
+ clipboard->connection, clipboard->window, clipboard->clipboard,
135
+ target, clipboard->property, XCB_CURRENT_TIME
136
+ );
137
+
138
+ while (true) {
139
+ xcb_selection_notify_event_t* event = (xcb_selection_notify_event_t*)wait_for_event(clipboard, XCB_SELECTION_NOTIFY);
140
+ if (!event) return false;
141
+ bool matches = event->selection == clipboard->clipboard && event->target == target;
142
+ bool received = event->property != XCB_NONE;
143
+ free(event);
144
+ if (!matches) continue;
145
+ if (!received) return true; // No owner, or the owner does not offer this target.
146
+ if (!read_property(clipboard, false, result)) return false;
147
+ break;
148
+ }
149
+ if (result->type != clipboard->incr) return true;
150
+
151
+ free(result->data);
152
+ memset(result, 0, sizeof(*result));
153
+ xcb_delete_property(clipboard->connection, clipboard->window, clipboard->property);
154
+
155
+ while (true) {
156
+ xcb_property_notify_event_t* event = (xcb_property_notify_event_t*)wait_for_event(clipboard, XCB_PROPERTY_NOTIFY);
157
+ if (!event) return false;
158
+ bool matches = event->atom == clipboard->property && event->state == XCB_PROPERTY_NEW_VALUE;
159
+ free(event);
160
+ if (!matches) continue;
161
+
162
+ property_data chunk = {0};
163
+ if (!read_property(clipboard, true, &chunk)) return false;
164
+ bool appended = append_property(result, &chunk);
165
+ bool finished = chunk.items == 0;
166
+ free(chunk.data);
167
+ if (!appended) return false;
168
+ if (finished) return true;
169
+ }
170
+ }
171
+
172
+ static xcb_atom_t intern_atom(x11_clipboard* clipboard, const char* name) {
173
+ xcb_intern_atom_cookie_t cookie = xcb_intern_atom(clipboard->connection, false, strlen(name), name);
174
+ xcb_intern_atom_reply_t* reply = wait_for_reply(clipboard, cookie.sequence);
175
+ if (!reply) return XCB_NONE;
176
+ xcb_atom_t atom = reply->atom;
177
+ free(reply);
178
+ return atom;
179
+ }
180
+
181
+ static bool open_clipboard(x11_clipboard* clipboard) {
182
+ memset(clipboard, 0, sizeof(*clipboard));
183
+ clipboard->deadline = monotonic_ms() + CLIPBOARD_TIMEOUT_MS;
184
+ int screen_number = 0;
185
+ clipboard->connection = xcb_connect(0, &screen_number);
186
+ if (xcb_connection_has_error(clipboard->connection)) return false;
187
+ xcb_screen_iterator_t screens = xcb_setup_roots_iterator(xcb_get_setup(clipboard->connection));
188
+ while (screen_number-- > 0 && screens.rem) xcb_screen_next(&screens);
189
+ if (!screens.rem) return false;
190
+
191
+ clipboard->window = xcb_generate_id(clipboard->connection);
192
+ uint32_t mask = XCB_EVENT_MASK_PROPERTY_CHANGE;
193
+ xcb_create_window(
194
+ clipboard->connection, XCB_COPY_FROM_PARENT, clipboard->window,
195
+ screens.data->root, 0, 0, 1, 1, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
196
+ XCB_COPY_FROM_PARENT, XCB_CW_EVENT_MASK, &mask
197
+ );
198
+ clipboard->clipboard = intern_atom(clipboard, "CLIPBOARD");
199
+ clipboard->property = intern_atom(clipboard, "PI_CLIPBOARD");
200
+ clipboard->targets = intern_atom(clipboard, "TARGETS");
201
+ clipboard->incr = intern_atom(clipboard, "INCR");
202
+ return clipboard->clipboard && clipboard->property && clipboard->targets && clipboard->incr;
203
+ }
204
+
205
+ static void close_clipboard(x11_clipboard* clipboard) {
206
+ if (clipboard->connection) xcb_disconnect(clipboard->connection);
207
+ }
208
+
209
+ static bool preferred_target(x11_clipboard* clipboard, bool image, xcb_atom_t* target) {
210
+ const char* const* types = image ? image_types : text_types;
211
+ size_t type_count = image ? 6 : 5;
212
+ xcb_atom_t wanted[6];
213
+ for (size_t index = 0; index < type_count; index++) {
214
+ wanted[index] = intern_atom(clipboard, types[index]);
215
+ if (wanted[index] == XCB_NONE) return false;
216
+ }
217
+
218
+ property_data targets = {0};
219
+ bool received = request_selection(clipboard, clipboard->targets, &targets);
220
+ bool valid = targets.type == XCB_ATOM_ATOM && targets.format == 32 &&
221
+ targets.items == targets.length / sizeof(xcb_atom_t) && targets.length % sizeof(xcb_atom_t) == 0;
222
+ if (received && valid) {
223
+ xcb_atom_t* offered = (xcb_atom_t*)targets.data;
224
+ for (size_t wanted_index = 0; wanted_index < type_count; wanted_index++) {
225
+ for (uint32_t offered_index = 0; offered_index < targets.items; offered_index++) {
226
+ if (offered[offered_index] == wanted[wanted_index]) {
227
+ free(targets.data);
228
+ *target = wanted[wanted_index];
229
+ return true;
230
+ }
231
+ }
232
+ }
233
+ }
234
+ free(targets.data);
235
+ // Some owners refuse TARGETS but still serve text directly.
236
+ if (received && !image && targets.type == XCB_NONE) {
237
+ *target = intern_atom(clipboard, "UTF8_STRING");
238
+ return *target != XCB_NONE;
239
+ }
240
+ return received && (valid || targets.type == XCB_NONE);
241
+ }
242
+
243
+ static bool read_selection(x11_clipboard* clipboard, bool image, property_data* result) {
244
+ xcb_atom_t target = XCB_NONE;
245
+ bool received = preferred_target(clipboard, image, &target) &&
246
+ (target == XCB_NONE || request_selection(clipboard, target, result));
247
+ if (!received) {
248
+ free(result->data);
249
+ memset(result, 0, sizeof(*result));
250
+ }
251
+ return received;
252
+ }
253
+
254
+ static void read_clipboard(clipboard_job* job) {
255
+ x11_clipboard clipboard;
256
+ if (open_clipboard(&clipboard)) {
257
+ property_data contents = {0};
258
+ if (!read_selection(&clipboard, job->operation == CLIPBOARD_IMAGE, &contents)) {
259
+ job->error = "Could not read X11 clipboard";
260
+ } else {
261
+ job->data = contents.data;
262
+ job->length = contents.length;
263
+ job->format = !contents.data ? CLIPBOARD_EMPTY : job->operation == CLIPBOARD_IMAGE
264
+ ? CLIPBOARD_BUFFER : contents.type == XCB_ATOM_STRING ? CLIPBOARD_LATIN1 : CLIPBOARD_UTF8;
265
+ }
266
+ }
267
+ close_clipboard(&clipboard);
268
+ }
269
+
270
+ PI_NAPI_EXPORT napi_value napi_register_module_v1(napi_env env, napi_value exports) {
271
+ set_function_export(env, exports, "getText", get_clipboard_text);
272
+ set_function_export(env, exports, "getImage", get_clipboard_image);
273
+ return exports;
274
+ }
package/native/napi.h ADDED
@@ -0,0 +1,89 @@
1
+ #ifndef PI_NAPI_H
2
+ #define PI_NAPI_H
3
+
4
+ // Resolve the small N-API surface we use from Node or Bun, without Node headers
5
+ // or a link-time dependency on a particular runtime.
6
+ #include <stdbool.h>
7
+ #include <stddef.h>
8
+ #include <stdint.h>
9
+ #ifdef _WIN32
10
+ #include <windows.h>
11
+ #define PI_NAPI_CALL __cdecl
12
+ #define PI_NAPI_EXPORT __declspec(dllexport)
13
+ #else
14
+ #include <dlfcn.h>
15
+ #define PI_NAPI_CALL
16
+ #define PI_NAPI_EXPORT __attribute__((visibility("default")))
17
+ #endif
18
+
19
+ #define NAPI_AUTO_LENGTH ((size_t)-1)
20
+
21
+ typedef void* napi_env;
22
+ typedef void* napi_value;
23
+ typedef void* napi_callback_info;
24
+ typedef void* napi_async_work;
25
+ typedef void* napi_deferred;
26
+ typedef void (PI_NAPI_CALL *napi_async_execute_callback)(napi_env, void*);
27
+ typedef void (PI_NAPI_CALL *napi_async_complete_callback)(napi_env, int, void*);
28
+ typedef int (PI_NAPI_CALL *napi_create_async_work_fn)(napi_env, napi_value, napi_value, napi_async_execute_callback, napi_async_complete_callback, void*, napi_async_work*);
29
+ typedef int (PI_NAPI_CALL *napi_async_work_fn)(napi_env, napi_async_work);
30
+ typedef int (PI_NAPI_CALL *napi_create_promise_fn)(napi_env, napi_deferred*, napi_value*);
31
+ typedef int (PI_NAPI_CALL *napi_settle_deferred_fn)(napi_env, napi_deferred, napi_value);
32
+ typedef int (PI_NAPI_CALL *napi_create_error_fn)(napi_env, napi_value, napi_value, napi_value*);
33
+ typedef napi_value (PI_NAPI_CALL *napi_callback)(napi_env, napi_callback_info);
34
+ typedef int (PI_NAPI_CALL *napi_create_buffer_copy_fn)(napi_env, size_t, const void*, void**, napi_value*);
35
+ typedef int (PI_NAPI_CALL *napi_create_function_fn)(napi_env, const char*, size_t, napi_callback, void*, napi_value*);
36
+ typedef int (PI_NAPI_CALL *napi_create_string_utf8_fn)(napi_env, const char*, size_t, napi_value*);
37
+ typedef int (PI_NAPI_CALL *napi_create_string_utf16_fn)(napi_env, const uint16_t*, size_t, napi_value*);
38
+ typedef int (PI_NAPI_CALL *napi_get_boolean_fn)(napi_env, bool, napi_value*);
39
+ typedef int (PI_NAPI_CALL *napi_get_cb_info_fn)(napi_env, napi_callback_info, size_t*, napi_value*, napi_value*, void**);
40
+ typedef int (PI_NAPI_CALL *napi_get_value_fn)(napi_env, napi_value*);
41
+ typedef int (PI_NAPI_CALL *napi_get_value_string_utf8_fn)(napi_env, napi_value, char*, size_t, size_t*);
42
+ typedef int (PI_NAPI_CALL *napi_get_value_string_utf16_fn)(napi_env, napi_value, uint16_t*, size_t, size_t*);
43
+ typedef int (PI_NAPI_CALL *napi_set_named_property_fn)(napi_env, napi_value, const char*, napi_value);
44
+ typedef int (PI_NAPI_CALL *napi_throw_error_fn)(napi_env, const char*, const char*);
45
+
46
+ static void* node_symbol(const char* name) {
47
+ #ifdef _WIN32
48
+ HMODULE module = GetModuleHandleA(0);
49
+ void* proc = module ? (void*)GetProcAddress(module, name) : 0;
50
+ if (proc) return proc;
51
+ module = GetModuleHandleA("node.dll");
52
+ return module ? (void*)GetProcAddress(module, name) : 0;
53
+ #else
54
+ return dlsym(RTLD_DEFAULT, name);
55
+ #endif
56
+ }
57
+
58
+ static napi_value undefined_value(napi_env env) {
59
+ napi_get_value_fn get_value = (napi_get_value_fn)node_symbol("napi_get_undefined");
60
+ napi_value result = 0;
61
+ if (get_value) get_value(env, &result);
62
+ return result;
63
+ }
64
+
65
+ static napi_value null_value(napi_env env) {
66
+ napi_get_value_fn get_value = (napi_get_value_fn)node_symbol("napi_get_null");
67
+ napi_value result = 0;
68
+ if (get_value) get_value(env, &result);
69
+ return result;
70
+ }
71
+
72
+ static napi_value fail(napi_env env, const char* message) {
73
+ napi_throw_error_fn napi_throw_error = (napi_throw_error_fn)node_symbol("napi_throw_error");
74
+ if (napi_throw_error) napi_throw_error(env, 0, message);
75
+ return undefined_value(env);
76
+ }
77
+
78
+ static void set_function_export(napi_env env, napi_value exports, const char* name, napi_callback callback) {
79
+ napi_create_function_fn napi_create_function = (napi_create_function_fn)node_symbol("napi_create_function");
80
+ napi_set_named_property_fn napi_set_named_property =
81
+ (napi_set_named_property_fn)node_symbol("napi_set_named_property");
82
+ napi_value fn = 0;
83
+ if (napi_create_function && napi_set_named_property &&
84
+ napi_create_function(env, name, NAPI_AUTO_LENGTH, callback, 0, &fn) == 0) {
85
+ napi_set_named_property(env, exports, name, fn);
86
+ }
87
+ }
88
+
89
+ #endif
@@ -0,0 +1,31 @@
1
+ # Windows native prebuilds
2
+
3
+ Provides console input setup, modifier-key state, and asynchronous text/image clipboard access. Links to `kernel32` and `user32`; no Node headers are required.
4
+
5
+ ## Building
6
+
7
+ On Windows, install Visual Studio's "Desktop development with C++" workload, then run from the repository root to build x64 and arm64:
8
+
9
+ ```sh
10
+ npm --prefix packages/tui run build:native:win32
11
+ ```
12
+
13
+ For cross-builds or custom toolchains, provide MinGW-compatible compilers:
14
+
15
+ ```sh
16
+ ION_TUI_WIN32_TOOLCHAIN=mingw \
17
+ CC_X64=/path/to/x86_64-w64-mingw32-gcc \
18
+ CC_ARM64=/path/to/aarch64-w64-mingw32-gcc \
19
+ npm --prefix packages/tui run build:native:win32
20
+ ```
21
+
22
+ ## Testing clipboard writes
23
+
24
+ On a Windows test desktop, run from `packages/tui` in PowerShell:
25
+
26
+ ```powershell
27
+ $env:ION_TEST_NATIVE_CLIPBOARD = "1"
28
+ node --test test/native-platform.test.ts
29
+ ```
30
+
31
+ This opt-in test checks native text writes and reads. **It replaces the system clipboard contents.**