From 68205fe3e4b26a43de3c9cba4f8217669a70517a Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 18:08:14 +0900 Subject: [PATCH 01/20] Feat: Open the Win32 and X11 toolkit-free windows by default (NFR-14) Windows: minimum size, resizable, icon, clipboard, OLE file drop with enter and leave, the caption strip taken into the client area in the window's own procedure (moved from the shim, which no longer subclasses the toolkit's frame), drawn buttons through window actions, and the caret position for the IME candidate window. Linux: CLIPBOARD and PRIMARY selections, text commit events, XIM preedit and commit (callbacks style, falling back to the plain styles and to the plain lookup), XDND file drop, _NET_WM_ICON, minimum size and non-resizable hints, undecorated chrome with a drawn caption and edges that hand the move and resize to the window manager through _NET_WM_MOVERESIZE, display scale from Xft.dpi or GDK_SCALE, and bring to front from a notification press without the toolkit. macOS shares the shell and gains the IME spot. RendererEntryPoints no longer falls back to the toolkit's window on any desktop. AT-SPI is not included; it is built on the Kotlin/Native window in #131. Covers NFR-14 and FR-19 (windows chrome), FR-27 (file drop). scripts/tests/native-window-default.test.sh replaces the macOS only test. --- CHANGELOG.md | 12 + renderer/desktop/c/appkit_window.m | 35 +- renderer/desktop/c/renderer_entry.c | 207 ----- renderer/desktop/c/win32_window.c | 509 +++++++++++- renderer/desktop/c/x11_window.c | 786 +++++++++++++++++- renderer/desktop/src/AppKitWindow.kt | 114 +-- renderer/desktop/src/NativeClipboard.kt | 8 +- renderer/desktop/src/NativeWindowShell.kt | 181 ++++ renderer/desktop/src/RendererEntryPoints.kt | 89 +- renderer/desktop/src/TextInputSession.kt | 10 + renderer/desktop/src/Win32Window.kt | 51 +- renderer/desktop/src/X11Window.kt | 61 +- scripts/tests/macos-window-default.test.sh | 39 - scripts/tests/native-window-default.test.sh | 48 ++ scripts/tests/windows-caption-metrics.test.sh | 2 +- 15 files changed, 1699 insertions(+), 453 deletions(-) create mode 100644 renderer/desktop/src/NativeWindowShell.kt delete mode 100755 scripts/tests/macos-window-default.test.sh create mode 100755 scripts/tests/native-window-default.test.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 95d7bff1d..f58a6158c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,18 @@ ## Unreleased +### Windows and Linux open their own window too + +The Win32 and X11 windows are now the default on the native-image path, with no environment +variable. Windows keeps its frame and takes the caption strip into the content as before +(the logic moved from the shim into the window's own procedure), and now honours the +application's minimum size, resizable choice and icon, has a clipboard, and takes files +dropped on a drop target. Linux draws its own caption and edges and hands the move and the +resize to the window manager, has the CLIPBOARD and PRIMARY selections, text input with +composition through XIM, drag and drop of files, the application's icon, and the display +scale. The caret position is sent to the input method so candidate windows open beside it. +Not done on Linux: AT-SPI accessibility, which is being built for the Kotlin/Native window. + ### The native-image renderer opens its own window on macOS On the GraalVM native-image path, macOS now opens the AppKit window the renderer makes diff --git a/renderer/desktop/c/appkit_window.m b/renderer/desktop/c/appkit_window.m index df67989ae..7a44c8251 100644 --- a/renderer/desktop/c/appkit_window.m +++ b/renderer/desktop/c/appkit_window.m @@ -394,6 +394,10 @@ int32_t dxc_native_poll_event(struct dxc_event *out) { * and to the one holding focus, and a plain NSView answers none of them; everything here * turns one into a record and puts it on the queue above. */ +// Where the caret is in the scene, in pixels from the top left, for the candidate window. +static float dxc_ime_spot_x; +static float dxc_ime_spot_y; + // The paths of the files last dragged over the window, NUL separated. static NSString *dxc_dropped_paths; @@ -664,7 +668,10 @@ - (NSUInteger)characterIndexForPoint:(NSPoint)point { return NSNotFound; } // boundary; until it is asked for, the top left of the view keeps the list on screen and // near enough to read, which is better than the bottom of the display. - (NSRect)firstRectForCharacterRange:(NSRange)range actualRange:(NSRangePointer)actualRange { - NSRect local = NSMakeRect(0, 0, 1, 20); + // Where the caret is, so the input method's candidate window opens beside what is + // being typed. The renderer reports it in the scene's pixels. + CGFloat scale = self.window.backingScaleFactor > 0 ? self.window.backingScaleFactor : 1; + NSRect local = NSMakeRect(dxc_ime_spot_x / scale, dxc_ime_spot_y / scale, 1, 20); NSRect windowRect = [self convertRect:local toView:nil]; return [self.window convertRectToScreen:windowRect]; } @@ -1046,3 +1053,29 @@ void dxc_native_frame_end(void *queue_pointer) { } }); } + +/** What a button of the application's own caption asks: 0 minimises, 1 zooms, 2 closes. */ +void dxc_native_window_action(int32_t action) { + dxc_on_main(^{ + @autoreleasepool { + NSWindow *window = NSApp.mainWindow ?: NSApp.windows.firstObject; + switch (action) { + case 0: [window miniaturize:nil]; break; + case 1: [window zoom:nil]; break; + case 2: [window performClose:nil]; break; + default: break; + } + } + }); +} + +/** The system moves and sizes this window itself, so nothing is asked of it. */ +void dxc_native_window_begin_drag(int32_t edge) { + (void)edge; +} + +/** Says where the caret is, in pixels from the window's top left. */ +void dxc_native_set_ime_spot(float x, float y) { + dxc_ime_spot_x = x; + dxc_ime_spot_y = y; +} diff --git a/renderer/desktop/c/renderer_entry.c b/renderer/desktop/c/renderer_entry.c index ac461dbbe..4241d6ccb 100644 --- a/renderer/desktop/c/renderer_entry.c +++ b/renderer/desktop/c/renderer_entry.c @@ -43,33 +43,6 @@ int32_t compose_rust_renderer_run_impl(graal_isolatethread_t *thread, const char void compose_rust_renderer_request_frame_impl(graal_isolatethread_t *thread); int32_t compose_rust_renderer_run(void); -/** - * Whether the application asked for a material behind its window. - * - * Off until the renderer says otherwise, because a window that did not ask must come up - * the way it always did: no effect view is built for it and nothing about its drawing - * changes. The renderer knows the answer only after the first batch has been applied, - * and the macOS poller is still running by then, so a late yes is still acted on. - * - * Exported on every desktop so the renderer has one symbol to call. Only macOS has - * anything to do with the answer today; Windows and Linux record it and draw as before. - */ -#ifdef _WIN32 -// MSVC's C compiler has no without an experimental switch, so Windows keeps -// the flag with the Interlocked functions, which are full barriers like atomic_store. -static volatile LONG dxc_window_material_asked; - -void dxc_set_window_material(int32_t asked) { - InterlockedExchange(&dxc_window_material_asked, asked != 0); -} -#else -static atomic_int dxc_window_material_asked; - -void dxc_set_window_material(int32_t asked) { - atomic_store(&dxc_window_material_asked, asked != 0); -} -#endif - #ifdef __APPLE__ #include #include @@ -337,179 +310,6 @@ static void compose_rust_declare_dpi_awareness(void) { // being stretched. SetProcessDPIAware(); } - -/* - * Taking the caption strip into the client area while the frame stays whole. - * - * The obvious way to draw your own title bar is an undecorated window, and on Windows - * that is the wrong trade. The frame is not only the bar: it is the drop shadow, the - * resize border, Snap Layouts and the animation when the window is restored. None of - * those can be drawn from inside the window, and an application that gives them up looks - * worse than one that kept the system bar. The first person to run this said the borders - * looked crude, and they were. - * - * What VS Code and Windows Terminal do instead is keep every one of those and take only - * the caption. A window reports its client area in WM_NCCALCSIZE. Letting the default - * handler compute the frame and then putting the top edge back where it started leaves - * the sides and the bottom as the system's while the strip the caption occupied becomes - * ours to draw in. The styles are untouched, so the shadow, the border and Snap are - * untouched with them. - * - * Two details are not optional. A maximised window is deliberately laid out larger than - * the monitor by the border thickness, so the same edges fall off screen; restoring the - * top edge unchanged there puts the caption off screen too, and it has to be inset. And - * the top resize band lived in the non-client area that no longer exists, so the hit test - * has to answer for it or the window becomes the one window on the desktop that cannot be - * resized from the top. - * - * The geometry below is duplicated in Kotlin, which draws into the same strip. - * scripts/tests/windows-caption-metrics.test.sh fails if the two stop agreeing, because - * two numbers that have to match and live in different languages do drift. - */ - -// Windows 11 caption metrics, in device independent pixels. -#define DXC_CAPTION_HEIGHT_DIP 32 -#define DXC_CAPTION_BUTTON_WIDTH_DIP 46 -#define DXC_CAPTION_BUTTON_COUNT 3 - -static WNDPROC dxc_inner_window_proc; - -static int dxc_scale_for_window(HWND window, int dip) { - // GetDpiForWindow arrived in Windows 10 1607. Linking it would refuse to start on - // anything older, and the fallback is the desktop's own scale, which is what a - // pre-1607 machine has anyway. - typedef UINT(WINAPI * get_dpi_fn)(HWND); - UINT dpi = 0; - HMODULE user32 = LoadLibraryW(L"user32.dll"); - if (user32 != NULL) { - get_dpi_fn get_dpi = (get_dpi_fn)(void *)GetProcAddress(user32, "GetDpiForWindow"); - if (get_dpi != NULL) { - dpi = get_dpi(window); - } - FreeLibrary(user32); - } - if (dpi == 0) { - HDC screen = GetDC(NULL); - if (screen != NULL) { - dpi = (UINT)GetDeviceCaps(screen, LOGPIXELSY); - ReleaseDC(NULL, screen); - } - } - if (dpi == 0) { - dpi = 96; - } - return (int)MulDiv(dip, (int)dpi, 96); -} - -/** How far a maximised window hangs off every edge of its monitor. */ -static int dxc_maximised_overhang(HWND window) { - (void)window; - return GetSystemMetrics(SM_CYSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER); -} - -static LRESULT CALLBACK dxc_window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) { - switch (message) { - case WM_NCCALCSIZE: { - // wparam FALSE asks only for a rectangle, with no frame to compute. - if (wparam != TRUE) { - break; - } - NCCALCSIZE_PARAMS *params = (NCCALCSIZE_PARAMS *)lparam; - LONG requested_top = params->rgrc[0].top; - CallWindowProcW(dxc_inner_window_proc, window, message, wparam, lparam); - params->rgrc[0].top = - IsZoomed(window) ? requested_top + dxc_maximised_overhang(window) : requested_top; - return 0; - } - case WM_NCHITTEST: { - LRESULT where = CallWindowProcW(dxc_inner_window_proc, window, message, wparam, lparam); - // Everywhere the frame still answers for keeps its answer: the sides, the bottom - // and all four corners are still the system's. - if (where != HTCLIENT) { - return where; - } - POINT point = {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)}; - RECT frame; - if (!GetWindowRect(window, &frame)) { - return where; - } - // The top resize band was in the non-client area this window gave up, so nothing - // else will answer for it. - int band = dxc_maximised_overhang(window); - if (!IsZoomed(window) && point.y < frame.top + band) { - return HTTOP; - } - int caption = dxc_scale_for_window(window, DXC_CAPTION_HEIGHT_DIP); - if (point.y >= frame.top + caption) { - return HTCLIENT; - } - // The buttons are drawn by Kotlin and have to receive ordinary mouse input, so - // the strip they occupy stays client area. Everything else in the caption drags - // the window, which also brings back double click to maximise and the system - // menu on right click. - int buttons = dxc_scale_for_window( - window, DXC_CAPTION_BUTTON_WIDTH_DIP * DXC_CAPTION_BUTTON_COUNT); - if (point.x >= frame.right - buttons) { - return HTCLIENT; - } - return HTCAPTION; - } - default: - break; - } - return CallWindowProcW(dxc_inner_window_proc, window, message, wparam, lparam); -} - -struct dxc_window_search { - DWORD process; - HWND found; -}; - -static BOOL CALLBACK dxc_find_frame(HWND window, LPARAM lparam) { - struct dxc_window_search *search = (struct dxc_window_search *)lparam; - DWORD owner = 0; - GetWindowThreadProcessId(window, &owner); - if (owner != search->process || !IsWindowVisible(window)) { - return TRUE; - } - // AWT registers its top level frames under this class name. Matching on the class - // rather than on being the first visible window of the process avoids catching a - // splash, a tooltip or anything else that is ours and is not the frame. - wchar_t name[64]; - if (GetClassNameW(window, name, 64) == 0 || wcscmp(name, L"SunAwtFrame") != 0) { - return TRUE; - } - search->found = window; - return FALSE; -} - -static DWORD WINAPI dxc_reclaim_caption(LPVOID unused) { - (void)unused; - // The window is created by AWT, on AWT's thread, some time after the entry point - // returns control to Kotlin. There is no callback to wait on from here that would not - // mean a hand written bridge between Kotlin and this file, so this waits for the - // window to appear instead. Ten seconds is far longer than a window takes and short - // enough that a run which never opens one does not keep a thread forever. - for (int attempt = 0; attempt < 1000; attempt++) { - struct dxc_window_search search = {GetCurrentProcessId(), NULL}; - EnumWindows(dxc_find_frame, (LPARAM)&search); - if (search.found != NULL) { - LONG_PTR previous = - SetWindowLongPtrW(search.found, GWLP_WNDPROC, (LONG_PTR)dxc_window_proc); - if (previous == 0) { - return 1; - } - dxc_inner_window_proc = (WNDPROC)previous; - // Nothing recomputes the frame on its own, so the window is asked to. - SetWindowPos( - search.found, NULL, 0, 0, 0, 0, - SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); - return 0; - } - Sleep(10); - } - return 1; -} #endif @@ -517,13 +317,6 @@ int32_t compose_rust_renderer_run(void) { #ifdef _WIN32 compose_rust_attach_parent_console(); compose_rust_declare_dpi_awareness(); - // Runs alongside the renderer because the window it waits for is created by the - // renderer. A failure here leaves an ordinary system title bar, which is a worse look - // and a working window, so it is not worth refusing to start over. - HANDLE reclaim = CreateThread(NULL, 0, dxc_reclaim_caption, NULL, 0, NULL); - if (reclaim != NULL) { - CloseHandle(reclaim); - } static LONG started; if (InterlockedExchange(&started, 1)) { return RUN_ALREADY_RUNNING; diff --git a/renderer/desktop/c/win32_window.c b/renderer/desktop/c/win32_window.c index d42c546f9..26ecad464 100644 --- a/renderer/desktop/c/win32_window.c +++ b/renderer/desktop/c/win32_window.c @@ -47,6 +47,8 @@ #include #include #include +#include +#include #include #include #include @@ -75,6 +77,10 @@ enum { DXC_EVENT_KEY_UP = 6, DXC_EVENT_TEXT_COMMIT = 7, DXC_EVENT_TEXT_COMPOSE = 8, + // Files over the window, let go on it, and gone from it without being let go. + DXC_EVENT_FILES_ENTERED = 10, + DXC_EVENT_FILES_DROPPED = 11, + DXC_EVENT_FILES_EXITED = 12, }; // Room for what an input method is composing, which is a syllable or a word and never a @@ -1160,6 +1166,10 @@ static void dxc_read_ime_text(HIMC context, DWORD part, int32_t kind) { free(wide); } +// Where the caret is in the client area, in pixels, as the renderer last said. +static LONG dxc_ime_spot_x; +static LONG dxc_ime_spot_y; + static void dxc_position_ime(HWND window) { HIMC context = ImmGetContext(window); if (context == NULL) return; @@ -1168,18 +1178,173 @@ static void dxc_position_ime(HWND window) { position.dwStyle = CFS_POINT; // The caret position has not crossed from Compose yet. Keep the IME window at the // client area's top left, as the macOS text client does for the same reason. - position.ptCurrentPos.x = 0; - position.ptCurrentPos.y = 0; + position.ptCurrentPos.x = dxc_ime_spot_x; + position.ptCurrentPos.y = dxc_ime_spot_y; ImmSetCompositionWindow(context, &position); + CANDIDATEFORM candidate; + memset(&candidate, 0, sizeof candidate); + candidate.dwStyle = CFS_CANDIDATEPOS; + candidate.ptCurrentPos.x = dxc_ime_spot_x; + candidate.ptCurrentPos.y = dxc_ime_spot_y + 20; + ImmSetCandidateWindow(context, &candidate); ImmReleaseContext(window, context); } +// What the application asked of its window, held until the window is made. +static struct { + int32_t resizable; + int32_t min_width; + int32_t min_height; + int32_t system_chrome; + int32_t backdrop; +} dxc_options = {1, 0, 0, 0, 0}; + +/** + * Says how the next window should be made. Called once, before it is opened. + * + * `system_chrome` keeps the system's caption. Without it the caption strip becomes part of + * the client area and the renderer draws the title and the three buttons in it. `backdrop` + * means nothing here: this window has no material to put behind the page. + */ +void dxc_native_window_configure( + int32_t resizable, + int32_t min_width, + int32_t min_height, + int32_t system_chrome, + int32_t backdrop +) { + dxc_options.resizable = resizable; + dxc_options.min_width = min_width; + dxc_options.min_height = min_height; + dxc_options.system_chrome = system_chrome; + dxc_options.backdrop = backdrop; +} + +/** The window style the options come to. */ +static DWORD dxc_window_style(void) { + DWORD style = WS_OVERLAPPEDWINDOW; + if (!dxc_options.resizable) { + style &= ~(DWORD)(WS_THICKFRAME | WS_MAXIMIZEBOX); + } + return style; +} + +/* + * Taking the caption strip into the client area while the frame stays whole. + * + * The obvious way to draw your own title bar is an undecorated window, and on Windows + * that is the wrong trade. The frame is not only the bar: it is the drop shadow, the + * resize border, Snap Layouts and the animation when the window is restored. None of + * those can be drawn from inside the window. + * + * What VS Code and Windows Terminal do instead is keep every one of those and take only + * the caption. A window reports its client area in WM_NCCALCSIZE. Letting the default + * handler compute the frame and then putting the top edge back where it started leaves + * the sides and the bottom as the system's while the strip the caption occupied becomes + * ours to draw in. The styles are untouched, so the shadow, the border and Snap are + * untouched with them. + * + * Two details are not optional. A maximised window is deliberately laid out larger than + * the monitor by the border thickness, so the same edges fall off screen; restoring the + * top edge unchanged there puts the caption off screen too, and it has to be inset. And + * the top resize band lived in the non-client area that no longer exists, so the hit test + * has to answer for it or the window becomes the one window on the desktop that cannot be + * resized from the top. + * + * The geometry below is duplicated in Kotlin, which draws into the same strip. + * scripts/tests/windows-caption-metrics.test.sh fails if the two stop agreeing. + */ + +// Windows 11 caption metrics, in device independent pixels. +#define DXC_CAPTION_HEIGHT_DIP 32 +#define DXC_CAPTION_BUTTON_WIDTH_DIP 46 +#define DXC_CAPTION_BUTTON_COUNT 3 + +static int dxc_scaled(HWND window, int dip) { + UINT dpi = GetDpiForWindow(window); + if (dpi == 0) { + dpi = USER_DEFAULT_SCREEN_DPI; + } + return (int)MulDiv(dip, (int)dpi, USER_DEFAULT_SCREEN_DPI); +} + +/** How far a maximised window hangs off every edge of its monitor. */ +static int dxc_maximised_overhang(void) { + return GetSystemMetrics(SM_CYSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER); +} + +static LRESULT dxc_caption_hit_test(HWND window, LPARAM lparam) { + LRESULT where = DefWindowProcW(window, WM_NCHITTEST, 0, lparam); + // Everywhere the frame still answers for keeps its answer: the sides, the bottom and + // all four corners are still the system's. + if (where != HTCLIENT) { + return where; + } + POINT point = {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)}; + RECT frame; + if (!GetWindowRect(window, &frame)) { + return where; + } + // The top resize band was in the non-client area this window gave up, so nothing else + // will answer for it. A window that cannot be resized has no such band. + int band = dxc_maximised_overhang(); + if (dxc_options.resizable && !IsZoomed(window) && point.y < frame.top + band) { + return HTTOP; + } + if (point.y >= frame.top + dxc_scaled(window, DXC_CAPTION_HEIGHT_DIP)) { + return HTCLIENT; + } + // The buttons are drawn by Kotlin and have to receive ordinary mouse input, so the + // strip they occupy stays client area. Everything else in the caption drags the + // window, which also brings back double click to maximise and the system menu on + // right click. + int buttons = dxc_scaled(window, DXC_CAPTION_BUTTON_WIDTH_DIP * DXC_CAPTION_BUTTON_COUNT); + if (point.x >= frame.right - buttons) { + return HTCLIENT; + } + return HTCAPTION; +} + // Named apart from the one in `renderer_entry.c`, which subclasses the toolkit's frame // to reclaim its caption. That one goes looking for a window of AWT's class and will // not find this one, so the two never meet; the names are kept distinct anyway, // because a reader who found both would have every reason to think they were. static LRESULT CALLBACK dxc_native_window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) { switch (message) { + case WM_NCCALCSIZE: { + if (dxc_options.system_chrome || wparam != TRUE) { + break; + } + NCCALCSIZE_PARAMS *params = (NCCALCSIZE_PARAMS *)lparam; + LONG requested_top = params->rgrc[0].top; + DefWindowProcW(window, message, wparam, lparam); + params->rgrc[0].top = IsZoomed(window) ? requested_top + dxc_maximised_overhang() + : requested_top; + return 0; + } + case WM_NCHITTEST: + if (dxc_options.system_chrome) { + break; + } + return dxc_caption_hit_test(window, lparam); + case WM_GETMINMAXINFO: + if (dxc_options.min_width > 0 || dxc_options.min_height > 0) { + UINT dpi = GetDpiForWindow(window); + if (dpi == 0) { + dpi = USER_DEFAULT_SCREEN_DPI; + } + RECT wanted = {0, 0, MulDiv(dxc_options.min_width, (int)dpi, USER_DEFAULT_SCREEN_DPI), + MulDiv(dxc_options.min_height, (int)dpi, USER_DEFAULT_SCREEN_DPI)}; + AdjustWindowRectExForDpi(&wanted, dxc_window_style(), FALSE, 0, dpi); + MINMAXINFO *info = (MINMAXINFO *)lparam; + info->ptMinTrackSize.x = wanted.right - wanted.left; + // The caption strip is client area here, so the top of the frame is not + // outside the content. + info->ptMinTrackSize.y = dxc_options.system_chrome ? wanted.bottom - wanted.top + : wanted.bottom; + return 0; + } + break; case WM_MOUSEMOVE: // No tracking area. Windows delivers a move whenever the pointer is over the // client area, so hover, which is half of what a desktop control does, arrives @@ -1424,62 +1589,304 @@ void dxc_native_install_menu(const char *application_name) { (void)application_name; } -/* - * The names below are answered here and do nothing. - * - * One piece of Kotlin drives every desktop and reaches their windows by name, so each of - * these files answers every name, including the ones that mean nothing on it. A missing - * one is a warning on the linkers that look names up at load time and a failure on the - * ones that do not, which is a defect that travels to whoever builds for the strictest - * platform. It travelled three times before this was written down. + +/** The caption strip is fixed on this platform and Kotlin knows its height, so nothing is measured. */ +void dxc_native_window_caption(void *view_pointer, float *height, float *buttons_width) { + (void)view_pointer; + *height = 0; + *buttons_width = 0; +} + +/** + * Does with the window what a button of the application's own caption asks: 0 minimises, + * 1 maximises or restores, 2 closes. + */ +void dxc_native_window_action(int32_t action) { + if (dxc_window == NULL) { + return; + } + switch (action) { + case 0: + ShowWindow(dxc_window, SW_MINIMIZE); + break; + case 1: + ShowWindow(dxc_window, IsZoomed(dxc_window) ? SW_RESTORE : SW_MAXIMIZE); + break; + case 2: + PostMessageW(dxc_window, WM_CLOSE, 0, 0); + break; + default: + break; + } +} + +/** The system moves the window itself where the caption is the system's, so nothing here. */ +void dxc_native_window_begin_drag(int32_t edge) { + (void)edge; +} + +/** + * Gives the window the picture it named, from its pixels: eight bits each of red, green, + * blue and alpha, with the colour already multiplied by the alpha, row after row. */ +void dxc_native_set_icon(const uint8_t *rgba, int32_t width, int32_t height) { + if (dxc_window == NULL || rgba == NULL || width <= 0 || height <= 0) { + return; + } + BITMAPV5HEADER header; + memset(&header, 0, sizeof header); + header.bV5Size = sizeof header; + header.bV5Width = width; + // Negative: the rows are top first. + header.bV5Height = -height; + header.bV5Planes = 1; + header.bV5BitCount = 32; + header.bV5Compression = BI_BITFIELDS; + header.bV5RedMask = 0x00FF0000; + header.bV5GreenMask = 0x0000FF00; + header.bV5BlueMask = 0x000000FF; + header.bV5AlphaMask = 0xFF000000; + void *bits = NULL; + HDC screen = GetDC(NULL); + HBITMAP color = CreateDIBSection(screen, (BITMAPINFO *)&header, DIB_RGB_COLORS, &bits, NULL, 0); + ReleaseDC(NULL, screen); + if (color == NULL || bits == NULL) { + if (color != NULL) DeleteObject(color); + return; + } + uint8_t *out = (uint8_t *)bits; + for (int64_t index = 0; index < (int64_t)width * height; index++) { + uint8_t r = rgba[index * 4 + 0]; + uint8_t g = rgba[index * 4 + 1]; + uint8_t b = rgba[index * 4 + 2]; + uint8_t a = rgba[index * 4 + 3]; + // An icon's alpha is straight, so the colour is divided back out. + if (a != 0 && a != 255) { + r = (uint8_t)((r * 255 + a / 2) / a); + g = (uint8_t)((g * 255 + a / 2) / a); + b = (uint8_t)((b * 255 + a / 2) / a); + } + out[index * 4 + 0] = b; + out[index * 4 + 1] = g; + out[index * 4 + 2] = r; + out[index * 4 + 3] = a; + } + HBITMAP mask = CreateBitmap(width, height, 1, 1, NULL); + ICONINFO info; + memset(&info, 0, sizeof info); + info.fIcon = TRUE; + info.hbmMask = mask; + info.hbmColor = color; + HICON icon = CreateIconIndirect(&info); + DeleteObject(color); + DeleteObject(mask); + if (icon != NULL) { + SendMessageW(dxc_window, WM_SETICON, ICON_BIG, (LPARAM)icon); + SendMessageW(dxc_window, WM_SETICON, ICON_SMALL, (LPARAM)icon); + } +} + +/** What is on the clipboard as text, copied into [out] as UTF-8, and its length. */ int32_t dxc_native_clipboard_read(char *out, int32_t capacity) { - (void)out; - (void)capacity; - return 0; + if (!OpenClipboard(dxc_window)) { + return 0; + } + int32_t length = 0; + HANDLE data = GetClipboardData(CF_UNICODETEXT); + if (data != NULL) { + const wchar_t *wide = (const wchar_t *)GlobalLock(data); + if (wide != NULL) { + int written = WideCharToMultiByte(CP_UTF8, 0, wide, -1, out, capacity, NULL, NULL); + // The count includes the terminator, and is zero where the text did not fit. + if (written > 0) { + length = written - 1; + } + GlobalUnlock(data); + } + } + CloseClipboard(); + return length; } +/** Replaces the clipboard's contents with [text], which is UTF-8. */ void dxc_native_clipboard_write(const char *text) { - (void)text; + int units = MultiByteToWideChar(CP_UTF8, 0, text, -1, NULL, 0); + if (units <= 0 || !OpenClipboard(dxc_window)) { + return; + } + HGLOBAL memory = GlobalAlloc(GMEM_MOVEABLE, (SIZE_T)units * sizeof(wchar_t)); + if (memory != NULL) { + wchar_t *wide = (wchar_t *)GlobalLock(memory); + if (wide != NULL) { + MultiByteToWideChar(CP_UTF8, 0, text, -1, wide, units); + GlobalUnlock(memory); + EmptyClipboard(); + // The clipboard owns the memory from here on. + if (SetClipboardData(CF_UNICODETEXT, memory) == NULL) { + GlobalFree(memory); + } + } else { + GlobalFree(memory); + } + } + CloseClipboard(); } /* - * Asked for by name by the window code every desktop shares, and answered here by saying - * nothing: this window takes its options another way, has no strip of the system's to - * measure, and reads no list of dropped paths from the platform. + * Files dragged over the window, through OLE, which is what says where they are and when + * they leave. WM_DROPFILES says only that they were dropped. */ -void dxc_native_window_configure( - int32_t resizable, - int32_t min_width, - int32_t min_height, - int32_t system_chrome, - int32_t backdrop -) { - (void)resizable; - (void)min_width; - (void)min_height; - (void)system_chrome; - (void)backdrop; +#define DXC_DROPPED_BYTES (64 * 1024) +static char dxc_dropped_paths[DXC_DROPPED_BYTES]; +static int32_t dxc_dropped_length; +static int dxc_drag_has_files; + +/** The paths of the files last dragged over the window, NUL between them, and the length. */ +int32_t dxc_native_dropped_paths(char *out, int32_t capacity) { + if (dxc_dropped_length <= 0 || dxc_dropped_length > capacity) { + return 0; + } + memcpy(out, dxc_dropped_paths, (size_t)dxc_dropped_length); + return dxc_dropped_length; } -void dxc_native_window_caption(void *view_pointer, float *height, float *buttons_width) { - (void)view_pointer; - *height = 0; - *buttons_width = 0; +static FORMATETC dxc_hdrop_format(void) { + FORMATETC format; + format.cfFormat = CF_HDROP; + format.ptd = NULL; + format.dwAspect = DVASPECT_CONTENT; + format.lindex = -1; + format.tymed = TYMED_HGLOBAL; + return format; } -void dxc_native_set_icon(const uint8_t *rgba, int32_t width, int32_t height) { - (void)rgba; - (void)width; - (void)height; +static void dxc_read_dragged_files(IDataObject *data) { + dxc_dropped_length = 0; + FORMATETC format = dxc_hdrop_format(); + STGMEDIUM medium; + if (FAILED(IDataObject_GetData(data, &format, &medium))) { + return; + } + HDROP drop = (HDROP)medium.hGlobal; + UINT count = DragQueryFileW(drop, 0xFFFFFFFFu, NULL, 0); + int32_t used = 0; + for (UINT index = 0; index < count; index++) { + wchar_t wide[MAX_PATH * 4]; + UINT units = DragQueryFileW(drop, index, wide, (UINT)(sizeof wide / sizeof *wide)); + if (units == 0) continue; + int bytes = WideCharToMultiByte(CP_UTF8, 0, wide, (int)units, NULL, 0, NULL, NULL); + // Room for the separator as well, and a path that does not fit is left out rather + // than cut in half. + if (bytes <= 0 || used + bytes + 1 > DXC_DROPPED_BYTES) continue; + if (used > 0) dxc_dropped_paths[used++] = '\0'; + WideCharToMultiByte(CP_UTF8, 0, wide, (int)units, dxc_dropped_paths + used, bytes, NULL, NULL); + used += bytes; + } + dxc_dropped_length = used; + ReleaseStgMedium(&medium); +} + +static void dxc_push_drag(int32_t kind, POINTL where) { + POINT point = {where.x, where.y}; + if (dxc_window != NULL) { + ScreenToClient(dxc_window, &point); + } + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = kind; + record.x = (float)point.x; + record.y = (float)point.y; + dxc_push_event(record); } -int32_t dxc_native_dropped_paths(char *out, int32_t capacity) { - (void)out; - (void)capacity; - return 0; +static HRESULT STDMETHODCALLTYPE dxc_drop_query(IDropTarget *self, REFIID id, void **out) { + if (IsEqualIID(id, &IID_IUnknown) || IsEqualIID(id, &IID_IDropTarget)) { + *out = self; + return S_OK; + } + *out = NULL; + return E_NOINTERFACE; } +static ULONG STDMETHODCALLTYPE dxc_drop_add_ref(IDropTarget *self) { (void)self; return 1; } +static ULONG STDMETHODCALLTYPE dxc_drop_release(IDropTarget *self) { (void)self; return 1; } +static HRESULT STDMETHODCALLTYPE dxc_drop_enter( + IDropTarget *self, IDataObject *data, DWORD keys, POINTL where, DWORD *effect) { + (void)self; + (void)keys; + FORMATETC format = dxc_hdrop_format(); + dxc_drag_has_files = SUCCEEDED(IDataObject_QueryGetData(data, &format)); + if (!dxc_drag_has_files) { + *effect = DROPEFFECT_NONE; + return S_OK; + } + dxc_read_dragged_files(data); + dxc_push_drag(DXC_EVENT_FILES_ENTERED, where); + *effect = DROPEFFECT_COPY; + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE dxc_drop_over( + IDropTarget *self, DWORD keys, POINTL where, DWORD *effect) { + (void)self; + (void)keys; + if (!dxc_drag_has_files) { + *effect = DROPEFFECT_NONE; + return S_OK; + } + dxc_push_drag(DXC_EVENT_FILES_ENTERED, where); + *effect = DROPEFFECT_COPY; + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE dxc_drop_leave(IDropTarget *self) { + (void)self; + if (dxc_drag_has_files) { + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = DXC_EVENT_FILES_EXITED; + dxc_push_event(record); + } + dxc_drag_has_files = 0; + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE dxc_drop_drop( + IDropTarget *self, IDataObject *data, DWORD keys, POINTL where, DWORD *effect) { + (void)self; + (void)keys; + if (!dxc_drag_has_files) { + *effect = DROPEFFECT_NONE; + return S_OK; + } + dxc_read_dragged_files(data); + dxc_push_drag(DXC_EVENT_FILES_DROPPED, where); + dxc_drag_has_files = 0; + *effect = DROPEFFECT_COPY; + return S_OK; +} + +static IDropTargetVtbl dxc_drop_vtable = { + dxc_drop_query, dxc_drop_add_ref, dxc_drop_release, + dxc_drop_enter, dxc_drop_over, dxc_drop_leave, dxc_drop_drop, +}; +static IDropTarget dxc_drop_target = {&dxc_drop_vtable}; + +/** Lets the window be dropped on. Drag and drop is OLE's, which needs a single threaded apartment. */ +static void dxc_accept_files(HWND window) { + OleInitialize(NULL); + RegisterDragDrop(window, &dxc_drop_target); +} + +/* + * The names below are answered here and do nothing. + * + * One piece of Kotlin drives every desktop and reaches their windows by name, so each of + * these files answers every name, including the ones that mean nothing on it. A missing + * one is a warning on the linkers that look names up at load time and a failure on the + * ones that do not, which is a defect that travels to whoever builds for the strictest + * platform. It travelled three times before this was written down. + */ void dxc_native_set_frame_callback(void *callback, void *isolate_thread) { (void)callback; (void)isolate_thread; @@ -1632,7 +2039,7 @@ int32_t dxc_native_window_open( 0, DXC_WINDOW_CLASS, wide_title, - WS_OVERLAPPEDWINDOW, + dxc_window_style(), CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, GetModuleHandleW(NULL), NULL); if (window == NULL) { @@ -1651,9 +2058,11 @@ int32_t dxc_native_window_open( wanted.top = 0; wanted.right = MulDiv(width, (int)dpi, USER_DEFAULT_SCREEN_DPI); wanted.bottom = MulDiv(height, (int)dpi, USER_DEFAULT_SCREEN_DPI); - AdjustWindowRectExForDpi(&wanted, WS_OVERLAPPEDWINDOW, FALSE, 0, dpi); + AdjustWindowRectExForDpi(&wanted, dxc_window_style(), FALSE, 0, dpi); int outer_width = wanted.right - wanted.left; - int outer_height = wanted.bottom - wanted.top; + // Without the system caption the content starts at the top of the window, so the + // frame above it is not part of the outside. + int outer_height = dxc_options.system_chrome ? wanted.bottom - wanted.top : wanted.bottom; // Centred on the part of the screen a window is meant to sit in. Where that cannot be // asked for, the window keeps the place Windows chose for it rather than being moved // to a corner that was never a position. @@ -1666,7 +2075,7 @@ int32_t dxc_native_window_open( top = work.top + ((work.bottom - work.top) - outer_height) / 2; placement = SWP_NOZORDER; } - SetWindowPos(window, NULL, left, top, outer_width, outer_height, placement); + SetWindowPos(window, NULL, left, top, outer_width, outer_height, placement | SWP_FRAMECHANGED); RECT client; GetClientRect(window, &client); @@ -1799,6 +2208,7 @@ int32_t dxc_native_window_open( return 11; } + dxc_accept_files(window); ShowWindow(window, SW_SHOW); SetForegroundWindow(window); SetFocus(window); @@ -1927,3 +2337,12 @@ void dxc_native_frame_end(void *queue_pointer) { // swapchain two buffers deep comes back around immediately. dxc_wait_for_gpu(); } + +/** Says where the caret is, in pixels from the window's top left. */ +void dxc_native_set_ime_spot(float x, float y) { + dxc_ime_spot_x = (LONG)x; + dxc_ime_spot_y = (LONG)y; + if (dxc_window != NULL && dxc_ime_composing) { + dxc_position_ime(dxc_window); + } +} diff --git a/renderer/desktop/c/x11_window.c b/renderer/desktop/c/x11_window.c index 86fdb08d9..b740c39b2 100644 --- a/renderer/desktop/c/x11_window.c +++ b/renderer/desktop/c/x11_window.c @@ -5,9 +5,11 @@ #include #include #include +#include #include #include #include +#include #include #include #include @@ -22,10 +24,13 @@ enum { DXC_EVENT_SCROLL = 4, DXC_EVENT_KEY_DOWN = 5, DXC_EVENT_KEY_UP = 6, - // XIM, ibus and fcitx text composition is separate work. These kinds are declared - // for the shared reader, but this window does not populate text events. + // Text the input method finished, and text it is still composing. DXC_EVENT_TEXT_COMMIT = 7, DXC_EVENT_TEXT_COMPOSE = 8, + // Files over the window, let go on it, and gone from it without being let go. + DXC_EVENT_FILES_ENTERED = 10, + DXC_EVENT_FILES_DROPPED = 11, + DXC_EVENT_FILES_EXITED = 12, }; #define DXC_TEXT_BYTES 96 @@ -128,6 +133,51 @@ static int32_t dxc_cursor_shape = -1; static struct dxc_element dxc_elements[DXC_ELEMENT_CAPACITY]; static int32_t dxc_element_count; +// What the application asked of its window, held until the window is made. +static struct { + int32_t resizable; + int32_t min_width; + int32_t min_height; + int32_t system_chrome; + int32_t backdrop; +} dxc_options = {1, 0, 0, 0, 0}; + +// How many pixels go to a point on this display, read once when the window is made. +static float dxc_scale = 1.0f; + +// Atoms this file asks the server for once. +static Atom dxc_a_clipboard, dxc_a_primary, dxc_a_utf8, dxc_a_targets, dxc_a_text, + dxc_a_property, dxc_a_net_wm_name, dxc_a_net_wm_icon, dxc_a_net_wm_state, + dxc_a_maximized_vert, dxc_a_maximized_horz, dxc_a_moveresize, dxc_a_active_window, + dxc_a_motif_hints, dxc_a_incr; +static Atom dxc_a_xdnd_aware, dxc_a_xdnd_enter, dxc_a_xdnd_position, dxc_a_xdnd_status, + dxc_a_xdnd_leave, dxc_a_xdnd_drop, dxc_a_xdnd_finished, dxc_a_xdnd_selection, + dxc_a_xdnd_copy, dxc_a_xdnd_type_list, dxc_a_uri_list; + +// The text this window put on the clipboard, which it answers for while it owns it. +static char *dxc_clip_text; +static size_t dxc_clip_length; +static int dxc_owns_clipboard; + +// Set from any thread to ask the window to come forward; acted on in the next turn, which +// is on the thread Xlib is used from. +static volatile int dxc_raise_requested; + +// The input method and the context made from it, and the text being composed in it. +static XIM dxc_im; +static XIC dxc_ic; +#define DXC_PREEDIT_CAPACITY 256 +static wchar_t dxc_preedit[DXC_PREEDIT_CAPACITY]; +static int dxc_preedit_length; + +// Files being dragged over the window and the ones last let go on it. +#define DXC_DROPPED_BYTES (64 * 1024) +static char dxc_dropped_paths[DXC_DROPPED_BYTES]; +static int32_t dxc_dropped_length; +static Window dxc_drag_source; +static int dxc_drag_has_files; +static Time dxc_drop_time; + static void dxc_push_event(struct dxc_event event) { if (dxc_event_count == DXC_EVENT_CAPACITY) { dxc_event_head = (dxc_event_head + 1) % DXC_EVENT_CAPACITY; @@ -218,14 +268,544 @@ void dxc_native_set_frame_callback(int32_t (*callback)(void *isolate_thread), vo dxc_frame_thread = isolate_thread; } +/* Encodes one code point as UTF-8 and answers how many bytes that was. */ +static int dxc_encode_utf8(unsigned long code, char *out) { + if (code < 0x80) { out[0] = (char)code; return 1; } + if (code < 0x800) { + out[0] = (char)(0xC0 | (code >> 6)); out[1] = (char)(0x80 | (code & 0x3F)); return 2; + } + if (code < 0x10000) { + out[0] = (char)(0xE0 | (code >> 12)); out[1] = (char)(0x80 | ((code >> 6) & 0x3F)); + out[2] = (char)(0x80 | (code & 0x3F)); return 3; + } + out[0] = (char)(0xF0 | (code >> 18)); out[1] = (char)(0x80 | ((code >> 12) & 0x3F)); + out[2] = (char)(0x80 | ((code >> 6) & 0x3F)); out[3] = (char)(0x80 | (code & 0x3F)); + return 4; +} + +/* Pushes text as an event, cut at a character boundary where it does not fit. */ +static void dxc_push_text(int32_t kind, const char *utf8, size_t length) { + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = kind; + size_t take = length < DXC_TEXT_BYTES - 1 ? length : DXC_TEXT_BYTES - 1; + while (take > 0 && take < length && ((unsigned char)utf8[take] & 0xC0) == 0x80) { + take--; + } + memcpy(record.text, utf8, take); + dxc_push_event(record); +} + +/* What is being composed, as the whole run every time, which is what the scene is given. */ +static void dxc_push_preedit(void) { + char text[DXC_PREEDIT_CAPACITY * 4 + 1]; + size_t used = 0; + for (int index = 0; index < dxc_preedit_length; index++) { + used += (size_t)dxc_encode_utf8((unsigned long)dxc_preedit[index], text + used); + } + dxc_push_text(DXC_EVENT_TEXT_COMPOSE, text, used); +} + +/* + * The input method's preedit callbacks. The method says which run of characters changed + * and what they are now, counted in characters as the protocol counts them, and the whole + * run is kept here and handed to the scene each time. + */ +static int dxc_preedit_start(XIC ic, XPointer client, XPointer call) { + (void)ic; (void)client; (void)call; + dxc_preedit_length = 0; + // No limit on the length of what is composed. The method reads this return value, and + // a function that returns nothing would leave whatever was in the register. + return -1; +} + +static void dxc_preedit_draw(XIC ic, XPointer client, XIMPreeditDrawCallbackStruct *draw) { + (void)ic; (void)client; + if (draw == NULL) return; + int first = draw->chg_first; + int removed = draw->chg_length; + if (first < 0) first = 0; + if (first > dxc_preedit_length) first = dxc_preedit_length; + if (removed < 0) removed = 0; + if (first + removed > dxc_preedit_length) removed = dxc_preedit_length - first; + + wchar_t added[DXC_PREEDIT_CAPACITY]; + int added_length = 0; + XIMText *text = draw->text; + if (text != NULL) { + if (text->encoding_is_wchar && text->string.wide_char != NULL) { + for (int i = 0; i < (int)text->length && added_length < DXC_PREEDIT_CAPACITY; i++) { + added[added_length++] = text->string.wide_char[i]; + } + } else if (text->string.multi_byte != NULL) { + size_t count = mbstowcs(added, text->string.multi_byte, DXC_PREEDIT_CAPACITY); + if (count != (size_t)-1) added_length = (int)count; + } + } + int tail = dxc_preedit_length - first - removed; + if (first + added_length + tail > DXC_PREEDIT_CAPACITY) { + added_length = DXC_PREEDIT_CAPACITY - first - tail; + if (added_length < 0) return; + } + memmove(dxc_preedit + first + added_length, dxc_preedit + first + removed, + (size_t)tail * sizeof(wchar_t)); + memcpy(dxc_preedit + first, added, (size_t)added_length * sizeof(wchar_t)); + dxc_preedit_length = first + added_length + tail; + dxc_push_preedit(); +} + +static void dxc_preedit_done(XIC ic, XPointer client, XPointer call) { + (void)ic; (void)client; (void)call; + if (dxc_preedit_length > 0) { + dxc_preedit_length = 0; + dxc_push_preedit(); + } +} + +static void dxc_preedit_caret(XIC ic, XPointer client, XPointer call) { + (void)ic; (void)client; (void)call; +} + +static XIMCallback dxc_cb_start, dxc_cb_draw, dxc_cb_done, dxc_cb_caret; + +/* Opens the desktop's input method and a context on the window. None is not an error. */ +static void dxc_open_input_method(void) { + setlocale(LC_CTYPE, ""); + if (!XSupportsLocale()) return; + XSetLocaleModifiers(""); + dxc_im = XOpenIM(dxc_display, NULL, NULL, NULL); + if (dxc_im == NULL) return; + XIMStyles *styles = NULL; + if (XGetIMValues(dxc_im, XNQueryInputStyle, &styles, NULL) != NULL || styles == NULL) { + XCloseIM(dxc_im); dxc_im = NULL; return; + } + XIMStyle callbacks = XIMPreeditCallbacks | XIMStatusNothing; + XIMStyle plain = XIMPreeditNothing | XIMStatusNothing; + XIMStyle none = XIMPreeditNone | XIMStatusNone; + XIMStyle chosen = 0; + int have_callbacks = 0, have_plain = 0, have_none = 0; + for (int i = 0; i < styles->count_styles; i++) { + if (styles->supported_styles[i] == callbacks) have_callbacks = 1; + if (styles->supported_styles[i] == plain) have_plain = 1; + if (styles->supported_styles[i] == none) have_none = 1; + } + XFree(styles); + chosen = have_callbacks ? callbacks : have_plain ? plain : have_none ? none : 0; + if (chosen == 0) { XCloseIM(dxc_im); dxc_im = NULL; return; } + if (chosen == callbacks) { + dxc_cb_start.client_data = NULL; dxc_cb_start.callback = (XIMProc)dxc_preedit_start; + dxc_cb_draw.client_data = NULL; dxc_cb_draw.callback = (XIMProc)dxc_preedit_draw; + dxc_cb_done.client_data = NULL; dxc_cb_done.callback = (XIMProc)dxc_preedit_done; + dxc_cb_caret.client_data = NULL; dxc_cb_caret.callback = (XIMProc)dxc_preedit_caret; + XVaNestedList list = XVaCreateNestedList(0, + XNPreeditStartCallback, &dxc_cb_start, XNPreeditDrawCallback, &dxc_cb_draw, + XNPreeditDoneCallback, &dxc_cb_done, XNPreeditCaretCallback, &dxc_cb_caret, NULL); + dxc_ic = XCreateIC(dxc_im, XNInputStyle, chosen, XNClientWindow, dxc_window, + XNFocusWindow, dxc_window, XNPreeditAttributes, list, NULL); + XFree(list); + } else { + dxc_ic = XCreateIC(dxc_im, XNInputStyle, chosen, XNClientWindow, dxc_window, + XNFocusWindow, dxc_window, NULL); + } + if (dxc_ic == NULL) { XCloseIM(dxc_im); dxc_im = NULL; } +} + +/** Where the caret is, in pixels from the window's top left, for the candidate window. */ +void dxc_native_set_ime_spot(float x, float y) { + if (dxc_ic == NULL) return; + XPoint spot; + spot.x = (short)x; + spot.y = (short)y; + XVaNestedList list = XVaCreateNestedList(0, XNSpotLocation, &spot, NULL); + XSetICValues(dxc_ic, XNPreeditAttributes, list, NULL); + XFree(list); +} + +/* The bytes of a text property, or null. */ +static unsigned char *dxc_read_property(Window window, Atom property, unsigned long *length, + Atom *type) { + Atom actual = None; + int format = 0; + unsigned long count = 0, after = 0; + unsigned char *data = NULL; + if (XGetWindowProperty(dxc_display, window, property, 0, 1 << 20, True, AnyPropertyType, + &actual, &format, &count, &after, &data) != Success) { + return NULL; + } + *length = count * (unsigned long)(format / 8); + *type = actual; + return data; +} + +/** The clipboard's text as UTF-8 copied into [out], and its length; zero where none. */ +int32_t dxc_native_clipboard_read(char *out, int32_t capacity) { + if (dxc_display == NULL) return 0; + if (dxc_owns_clipboard && dxc_clip_text != NULL) { + if ((int32_t)dxc_clip_length > capacity) return 0; + memcpy(out, dxc_clip_text, dxc_clip_length); + return (int32_t)dxc_clip_length; + } + XConvertSelection(dxc_display, dxc_a_clipboard, dxc_a_utf8, dxc_a_property, dxc_window, + CurrentTime); + XFlush(dxc_display); + // The owner answers with an event, which this waits for without reading the others: + // they stay queued for the turn of the loop that handles them. + for (int attempt = 0; attempt < 100; attempt++) { + XEvent reply; + if (XCheckTypedWindowEvent(dxc_display, dxc_window, SelectionNotify, &reply)) { + if (reply.xselection.property == None) return 0; + unsigned long length = 0; + Atom type = None; + unsigned char *data = dxc_read_property(dxc_window, reply.xselection.property, + &length, &type); + if (data == NULL) return 0; + int32_t copied = 0; + if (type != dxc_a_incr && (int32_t)length <= capacity) { + memcpy(out, data, length); + copied = (int32_t)length; + } + XFree(data); + return copied; + } + struct timeval wait = {0, 5000}; + fd_set readable; + FD_ZERO(&readable); + FD_SET(ConnectionNumber(dxc_display), &readable); + select(ConnectionNumber(dxc_display) + 1, &readable, NULL, NULL, &wait); + } + return 0; +} + +/** Puts [text] on the clipboard and the primary selection, and answers for them. */ +void dxc_native_clipboard_write(const char *text) { + if (dxc_display == NULL || text == NULL) return; + free(dxc_clip_text); + dxc_clip_length = strlen(text); + dxc_clip_text = (char *)malloc(dxc_clip_length + 1); + if (dxc_clip_text == NULL) { dxc_clip_length = 0; return; } + memcpy(dxc_clip_text, text, dxc_clip_length + 1); + XSetSelectionOwner(dxc_display, dxc_a_clipboard, dxc_window, CurrentTime); + XSetSelectionOwner(dxc_display, dxc_a_primary, dxc_window, CurrentTime); + dxc_owns_clipboard = 1; + XFlush(dxc_display); +} + +/* Answers another program asking for what this window put on a selection. */ +static void dxc_answer_selection_request(XSelectionRequestEvent *request) { + XEvent reply; + memset(&reply, 0, sizeof reply); + reply.xselection.type = SelectionNotify; + reply.xselection.requestor = request->requestor; + reply.xselection.selection = request->selection; + reply.xselection.target = request->target; + reply.xselection.time = request->time; + reply.xselection.property = None; + Atom property = request->property != None ? request->property : request->target; + if (dxc_clip_text != NULL) { + if (request->target == dxc_a_targets) { + Atom targets[] = { dxc_a_targets, dxc_a_utf8, XA_STRING, dxc_a_text }; + XChangeProperty(dxc_display, request->requestor, property, XA_ATOM, 32, + PropModeReplace, (unsigned char *)targets, 4); + reply.xselection.property = property; + } else if (request->target == dxc_a_utf8 || request->target == XA_STRING || + request->target == dxc_a_text) { + XChangeProperty(dxc_display, request->requestor, property, + request->target == XA_STRING ? XA_STRING : dxc_a_utf8, 8, + PropModeReplace, (unsigned char *)dxc_clip_text, + (int)dxc_clip_length); + reply.xselection.property = property; + } + } + XSendEvent(dxc_display, request->requestor, False, NoEventMask, &reply); + XFlush(dxc_display); +} + +/** The paths of the files last dragged over the window, NUL between them, and the length. */ +int32_t dxc_native_dropped_paths(char *out, int32_t capacity) { + if (dxc_dropped_length <= 0 || dxc_dropped_length > capacity) return 0; + memcpy(out, dxc_dropped_paths, (size_t)dxc_dropped_length); + return dxc_dropped_length; +} + +/* Turns the uri list a drag carries into the paths in it. */ +static void dxc_read_uri_list(const char *list, unsigned long length) { + int32_t used = 0; + unsigned long at = 0; + while (at < length) { + unsigned long end = at; + while (end < length && list[end] != '\r' && list[end] != '\n') end++; + if (end - at > 7 && strncmp(list + at, "file://", 7) == 0) { + unsigned long from = at + 7; + // A host name between the slashes is skipped; the path begins at the next one. + while (from < end && list[from] != '/') from++; + int32_t start = used; + if (used > 0) { + if (used + 1 >= DXC_DROPPED_BYTES) break; + dxc_dropped_paths[used++] = '\0'; + start = used; + } + for (unsigned long i = from; i < end && used < DXC_DROPPED_BYTES - 1; i++) { + char c = list[i]; + if (c == '%' && i + 2 < end) { + char hex[3] = { list[i + 1], list[i + 2], 0 }; + char *stop = NULL; + long value = strtol(hex, &stop, 16); + if (stop != NULL && *stop == '\0') { + dxc_dropped_paths[used++] = (char)value; + i += 2; + continue; + } + } + dxc_dropped_paths[used++] = c; + } + if (used == start) used = start > 0 ? start - 1 : 0; + } + at = end; + while (at < length && (list[at] == '\r' || list[at] == '\n')) at++; + } + dxc_dropped_length = used; +} + +static void dxc_push_drag(int32_t kind, int root_x, int root_y) { + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = kind; + int x = 0, y = 0; + Window child; + XTranslateCoordinates(dxc_display, DefaultRootWindow(dxc_display), dxc_window, + root_x, root_y, &x, &y, &child); + record.x = (float)x; + record.y = (float)y; + dxc_push_event(record); +} + +static void dxc_send_dnd(Atom type, long a, long b, long c, long d) { + XEvent message; + memset(&message, 0, sizeof message); + message.xclient.type = ClientMessage; + message.xclient.window = dxc_drag_source; + message.xclient.message_type = type; + message.xclient.format = 32; + message.xclient.data.l[0] = (long)dxc_window; + message.xclient.data.l[1] = a; + message.xclient.data.l[2] = b; + message.xclient.data.l[3] = c; + message.xclient.data.l[4] = d; + XSendEvent(dxc_display, dxc_drag_source, False, NoEventMask, &message); + XFlush(dxc_display); +} + +/* Handles the messages of the drag and drop protocol. Answers true where it was one. */ +static int dxc_handle_dnd(XClientMessageEvent *message) { + Atom type = message->message_type; + if (type == dxc_a_xdnd_enter) { + dxc_drag_source = (Window)message->data.l[0]; + int more = (message->data.l[1] & 1) != 0; + dxc_drag_has_files = 0; + if (!more) { + for (int i = 2; i < 5; i++) { + if ((Atom)message->data.l[i] == dxc_a_uri_list) dxc_drag_has_files = 1; + } + } else { + unsigned long length = 0; + Atom actual = None; + unsigned char *data = dxc_read_property(dxc_drag_source, dxc_a_xdnd_type_list, + &length, &actual); + if (data != NULL) { + // Read as the format 32 array it is: an array of long, whatever a long is. + long *types = (long *)data; + for (unsigned long i = 0; i < length / 4; i++) { + if ((Atom)types[i] == dxc_a_uri_list) dxc_drag_has_files = 1; + } + XFree(data); + } + } + return 1; + } + if (type == dxc_a_xdnd_position) { + int root_x = (int)((message->data.l[2] >> 16) & 0xFFFF); + int root_y = (int)(message->data.l[2] & 0xFFFF); + if (dxc_drag_has_files) dxc_push_drag(DXC_EVENT_FILES_ENTERED, root_x, root_y); + dxc_send_dnd(dxc_a_xdnd_status, dxc_drag_has_files ? 1 : 0, 0, 0, + dxc_drag_has_files ? (long)dxc_a_xdnd_copy : 0); + return 1; + } + if (type == dxc_a_xdnd_leave) { + if (dxc_drag_has_files) { + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = DXC_EVENT_FILES_EXITED; + dxc_push_event(record); + } + dxc_drag_has_files = 0; + return 1; + } + if (type == dxc_a_xdnd_drop) { + dxc_drop_time = (Time)message->data.l[2]; + if (dxc_drag_has_files) { + XConvertSelection(dxc_display, dxc_a_xdnd_selection, dxc_a_uri_list, + dxc_a_property, dxc_window, dxc_drop_time); + } else { + dxc_send_dnd(dxc_a_xdnd_finished, 0, 0, 0, 0); + } + return 1; + } + return 0; +} + +/* The files arrived: take their paths, say where they were let go, and tell the source. */ +static void dxc_finish_drop(XSelectionEvent *arrived) { + int ok = 0; + if (arrived->property != None) { + unsigned long length = 0; + Atom type = None; + unsigned char *data = dxc_read_property(dxc_window, arrived->property, &length, &type); + if (data != NULL) { + dxc_read_uri_list((const char *)data, length); + XFree(data); + ok = dxc_dropped_length > 0; + } + } + if (ok) { + Window root, child; + int root_x = 0, root_y = 0, x = 0, y = 0; + unsigned int mask = 0; + XQueryPointer(dxc_display, dxc_window, &root, &child, &root_x, &root_y, &x, &y, &mask); + dxc_push_drag(DXC_EVENT_FILES_DROPPED, root_x, root_y); + } + dxc_send_dnd(dxc_a_xdnd_finished, ok ? 1 : 0, ok ? (long)dxc_a_xdnd_copy : 0, 0, 0); + dxc_drag_has_files = 0; +} + +/** + * Does with the window what a button of the application's own caption asks: 0 minimises, + * 1 maximises or restores, 2 closes, 3 brings it forward. Safe from any thread for the + * last one only, which is a flag acted on in the next turn. + */ +void dxc_native_window_action(int32_t action) { + if (action == 3) { + dxc_raise_requested = 1; + return; + } + if (dxc_display == NULL || dxc_window == None) return; + switch (action) { + case 0: + XIconifyWindow(dxc_display, dxc_window, DefaultScreen(dxc_display)); + break; + case 1: { + XEvent message; + memset(&message, 0, sizeof message); + message.xclient.type = ClientMessage; + message.xclient.window = dxc_window; + message.xclient.message_type = dxc_a_net_wm_state; + message.xclient.format = 32; + message.xclient.data.l[0] = 2; /* toggle */ + message.xclient.data.l[1] = (long)dxc_a_maximized_vert; + message.xclient.data.l[2] = (long)dxc_a_maximized_horz; + message.xclient.data.l[3] = 1; + XSendEvent(dxc_display, DefaultRootWindow(dxc_display), False, + SubstructureNotifyMask | SubstructureRedirectMask, &message); + break; + } + case 2: + dxc_closed = 1; + break; + default: + break; + } + XFlush(dxc_display); +} + +/** + * Hands the move or the resize to the window manager: 0 moves, and 1 to 8 pull the left, + * right, top and bottom edges and the four corners in that order. + */ +void dxc_native_window_begin_drag(int32_t edge) { + if (dxc_display == NULL || dxc_window == None) return; + // The directions _NET_WM_MOVERESIZE names, indexed by the edge numbers above. + static const long direction[9] = { 8, 7, 3, 1, 5, 0, 2, 6, 4 }; + if (edge < 0 || edge > 8) return; + Window root, child; + int root_x = 0, root_y = 0, x = 0, y = 0; + unsigned int mask = 0; + XQueryPointer(dxc_display, dxc_window, &root, &child, &root_x, &root_y, &x, &y, &mask); + XUngrabPointer(dxc_display, CurrentTime); + XEvent message; + memset(&message, 0, sizeof message); + message.xclient.type = ClientMessage; + message.xclient.window = dxc_window; + message.xclient.message_type = dxc_a_moveresize; + message.xclient.format = 32; + message.xclient.data.l[0] = root_x; + message.xclient.data.l[1] = root_y; + message.xclient.data.l[2] = direction[edge]; + message.xclient.data.l[3] = Button1; + message.xclient.data.l[4] = 1; + XSendEvent(dxc_display, DefaultRootWindow(dxc_display), False, + SubstructureNotifyMask | SubstructureRedirectMask, &message); + XFlush(dxc_display); + // The manager owns the pointer from here, so the release that ends the press is never + // delivered to this window. The scene is told it happened or it would believe the + // button is still down. + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = DXC_EVENT_POINTER_UP; + record.x = (float)x; + record.y = (float)y; + dxc_push_event(record); +} + +/** Reads how many pixels go to a point: the toolkit-independent settings desktops write. */ +static float dxc_detect_scale(void) { + const char *forced = getenv("GDK_SCALE"); + if (forced != NULL && atof(forced) >= 1.0) return (float)atof(forced); + char *resources = XResourceManagerString(dxc_display); + if (resources != NULL) { + XrmInitialize(); + XrmDatabase database = XrmGetStringDatabase(resources); + if (database != NULL) { + char *type = NULL; + XrmValue value; + if (XrmGetResource(database, "Xft.dpi", "Xft.Dpi", &type, &value) && value.addr != NULL) { + double dpi = atof(value.addr); + if (dpi >= 96.0) { + XrmDestroyDatabase(database); + return (float)(dpi / 96.0); + } + } + XrmDestroyDatabase(database); + } + } + return 1.0f; +} + static void dxc_pump_events(void) { if (dxc_pumping) { return; } dxc_pumping = 1; + if (dxc_raise_requested && dxc_display != NULL) { + dxc_raise_requested = 0; + XMapRaised(dxc_display, dxc_window); + XEvent message; + memset(&message, 0, sizeof message); + message.xclient.type = ClientMessage; + message.xclient.window = dxc_window; + message.xclient.message_type = dxc_a_active_window; + message.xclient.format = 32; + message.xclient.data.l[0] = 1; + XSendEvent(dxc_display, DefaultRootWindow(dxc_display), False, + SubstructureNotifyMask | SubstructureRedirectMask, &message); + XFlush(dxc_display); + } while (dxc_display != NULL && XPending(dxc_display) > 0) { XEvent event; XNextEvent(dxc_display, &event); + // Offered to the input method first. A key it takes is part of a composition and + // is not handled twice. + if (XFilterEvent(&event, None)) { + continue; + } struct dxc_event record; memset(&record, 0, sizeof record); switch (event.type) { @@ -245,6 +825,17 @@ static void dxc_pump_events(void) { record.x = button == 6 ? -3.0f : button == 7 ? 3.0f : 0.0f; record.y = button == 4 ? -3.0f : button == 5 ? 3.0f : 0.0f; } else { + if (event.type == ButtonPress && dxc_ic != NULL && dxc_preedit_length > 0) { + // A click while composing ends the composition and keeps what was + // typed, which is what the other desktops' input methods do. + char *kept = Xutf8ResetIC(dxc_ic); + dxc_preedit_length = 0; + dxc_push_preedit(); + if (kept != NULL) { + dxc_push_text(DXC_EVENT_TEXT_COMMIT, kept, strlen(kept)); + XFree(kept); + } + } record.kind = event.type == ButtonPress ? DXC_EVENT_POINTER_DOWN : DXC_EVENT_POINTER_UP; record.x = (float)event.xbutton.x; record.y = (float)event.xbutton.y; @@ -259,16 +850,59 @@ static void dxc_pump_events(void) { case KeyPress: case KeyRelease: { KeySym symbol = NoSymbol; - char bytes[32]; - int count = XLookupString(&event.xkey, bytes, sizeof bytes, &symbol, NULL); + char bytes[64]; + int count = 0; + Status status = XLookupNone; + if (event.type == KeyPress && dxc_ic != NULL) { + count = Xutf8LookupString(dxc_ic, &event.xkey, bytes, (int)sizeof bytes - 1, + &symbol, &status); + if (status == XBufferOverflow) count = 0; + } else { + count = XLookupString(&event.xkey, bytes, (int)sizeof bytes - 1, &symbol, NULL); + status = count > 0 ? XLookupChars : XLookupKeySym; + if (count > 0 && (unsigned char)bytes[0] >= 0x80) { + // Latin-1 from the plain lookup, re-encoded as the UTF-8 text is. + char converted[8]; + int length = dxc_encode_utf8((unsigned char)bytes[0], converted); + memcpy(bytes, converted, (size_t)length); + count = length; + } + } record.kind = event.type == KeyPress ? DXC_EVENT_KEY_DOWN : DXC_EVENT_KEY_UP; record.key_code = dxc_key_code(symbol); record.modifiers = dxc_modifiers(event.xkey.state); - if (count == 1 && (unsigned char)bytes[0] >= 32) { + if (count == 1 && (unsigned char)bytes[0] >= 32 && (unsigned char)bytes[0] < 127) { record.code_point = (unsigned char)bytes[0]; } + // The key is recorded first and its text after, the order the other desktops + // use. A shortcut types nothing: control, alt and the super key held mean + // the key is a command. + if (event.type == KeyPress && count > 0 && + (event.xkey.state & (ControlMask | Mod1Mask | Mod4Mask)) == 0 && + (unsigned char)bytes[0] >= 0x20 && bytes[0] != 0x7f) { + dxc_push_event(record); + dxc_push_text(DXC_EVENT_TEXT_COMMIT, bytes, (size_t)count); + continue; + } break; } + case FocusIn: + if (dxc_ic != NULL) XSetICFocus(dxc_ic); + continue; + case FocusOut: + if (dxc_ic != NULL) XUnsetICFocus(dxc_ic); + continue; + case SelectionRequest: + dxc_answer_selection_request(&event.xselectionrequest); + continue; + case SelectionClear: + dxc_owns_clipboard = 0; + continue; + case SelectionNotify: + if (event.xselection.selection == dxc_a_xdnd_selection) { + dxc_finish_drop(&event.xselection); + } + continue; case ConfigureNotify: if (event.xconfigure.width == dxc_width && event.xconfigure.height == dxc_height) { // The window was moved, or told again what it already was. Nothing @@ -304,6 +938,9 @@ static void dxc_pump_events(void) { } continue; case ClientMessage: + if (dxc_handle_dnd(&event.xclient)) { + continue; + } if (event.xclient.message_type == dxc_protocols && (Atom)event.xclient.data.l[0] == dxc_sync_request) { // The manager is about to resize the window and will hold the frame @@ -341,24 +978,16 @@ static void dxc_pump_events(void) { * ones that do not, which is a defect that travels to whoever builds for the strictest * platform. It travelled three times before this was written down. */ -int32_t dxc_native_clipboard_read(char *out, int32_t capacity) { - (void)out; - (void)capacity; - return 0; -} - -void dxc_native_clipboard_write(const char *text) { - (void)text; -} - void dxc_native_install_menu(const char *application_name) { (void)application_name; } -/* - * Asked for by name by the window code every desktop shares, and answered here by saying - * nothing: this window takes its options another way, has no strip of the system's to - * measure, and reads no list of dropped paths from the platform. +/** + * Says how the next window should be made. Called once, before it is opened. + * + * `system_chrome` keeps the window manager's frame. Without it the frame is taken off and + * the renderer draws the caption and the edges. `backdrop` means nothing here: this window + * has no material to put behind the page. */ void dxc_native_window_configure( int32_t resizable, @@ -367,11 +996,11 @@ void dxc_native_window_configure( int32_t system_chrome, int32_t backdrop ) { - (void)resizable; - (void)min_width; - (void)min_height; - (void)system_chrome; - (void)backdrop; + dxc_options.resizable = resizable; + dxc_options.min_width = min_width; + dxc_options.min_height = min_height; + dxc_options.system_chrome = system_chrome; + dxc_options.backdrop = backdrop; } void dxc_native_window_caption(void *view_pointer, float *height, float *buttons_width) { @@ -380,16 +1009,36 @@ void dxc_native_window_caption(void *view_pointer, float *height, float *buttons *buttons_width = 0; } +/** + * Gives the window the picture it named, from its pixels: eight bits each of red, green, + * blue and alpha, the colour already multiplied by the alpha, row after row. + */ void dxc_native_set_icon(const uint8_t *rgba, int32_t width, int32_t height) { - (void)rgba; - (void)width; - (void)height; -} - -int32_t dxc_native_dropped_paths(char *out, int32_t capacity) { - (void)out; - (void)capacity; - return 0; + if (dxc_display == NULL || dxc_window == None || rgba == NULL || width <= 0 || height <= 0) { + return; + } + size_t pixels = (size_t)width * (size_t)height; + // An array of long, whatever a long is: the width, the height, then one value a pixel + // with alpha in the top byte and the colour straight rather than multiplied. + unsigned long *data = (unsigned long *)malloc((pixels + 2) * sizeof *data); + if (data == NULL) return; + data[0] = (unsigned long)width; + data[1] = (unsigned long)height; + for (size_t index = 0; index < pixels; index++) { + unsigned long r = rgba[index * 4 + 0], g = rgba[index * 4 + 1]; + unsigned long b = rgba[index * 4 + 2], a = rgba[index * 4 + 3]; + if (a != 0 && a != 255) { + r = (r * 255 + a / 2) / a; g = (g * 255 + a / 2) / a; b = (b * 255 + a / 2) / a; + if (r > 255) r = 255; + if (g > 255) g = 255; + if (b > 255) b = 255; + } + data[index + 2] = (a << 24) | (r << 16) | (g << 8) | b; + } + XChangeProperty(dxc_display, dxc_window, dxc_a_net_wm_icon, XA_CARDINAL, 32, PropModeReplace, + (unsigned char *)data, (int)(pixels + 2)); + free(data); + XFlush(dxc_display); } void dxc_native_set_draw_callback(void *callback, void *isolate_thread) { @@ -449,6 +1098,11 @@ int32_t dxc_native_window_open(const char *title, int32_t width, int32_t height, dxc_display = XOpenDisplay(NULL); if (dxc_display == NULL) return 1; int screen = DefaultScreen(dxc_display); + // The size asked for is in points; the window is made in the pixels they come to on + // this display. + dxc_scale = dxc_detect_scale(); + width = (int32_t)((float)width * dxc_scale + 0.5f); + height = (int32_t)((float)height * dxc_scale + 0.5f); int attributes[] = { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, None }; XVisualInfo *visual = glXChooseVisual(dxc_display, screen, attributes); @@ -459,7 +1113,8 @@ int32_t dxc_native_window_open(const char *title, int32_t width, int32_t height, memset(&settings, 0, sizeof settings); settings.colormap = dxc_colormap; settings.event_mask = ExposureMask | StructureNotifyMask | PointerMotionMask | - ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask; + ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask | + FocusChangeMask; dxc_window = XCreateWindow(dxc_display, RootWindow(dxc_display, screen), 0, 0, (unsigned int)width, (unsigned int)height, 0, visual->depth, InputOutput, visual->visual, CWColormap | CWEventMask, &settings); @@ -509,7 +1164,68 @@ int32_t dxc_native_window_open(const char *title, int32_t width, int32_t height, } } XSetWMProtocols(dxc_display, dxc_window, protocols, protocol_count); + dxc_a_clipboard = XInternAtom(dxc_display, "CLIPBOARD", False); + dxc_a_primary = XA_PRIMARY; + dxc_a_utf8 = XInternAtom(dxc_display, "UTF8_STRING", False); + dxc_a_targets = XInternAtom(dxc_display, "TARGETS", False); + dxc_a_text = XInternAtom(dxc_display, "TEXT", False); + dxc_a_incr = XInternAtom(dxc_display, "INCR", False); + dxc_a_property = XInternAtom(dxc_display, "DXC_SELECTION", False); + dxc_a_net_wm_name = XInternAtom(dxc_display, "_NET_WM_NAME", False); + dxc_a_net_wm_icon = XInternAtom(dxc_display, "_NET_WM_ICON", False); + dxc_a_net_wm_state = XInternAtom(dxc_display, "_NET_WM_STATE", False); + dxc_a_maximized_vert = XInternAtom(dxc_display, "_NET_WM_STATE_MAXIMIZED_VERT", False); + dxc_a_maximized_horz = XInternAtom(dxc_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False); + dxc_a_moveresize = XInternAtom(dxc_display, "_NET_WM_MOVERESIZE", False); + dxc_a_active_window = XInternAtom(dxc_display, "_NET_ACTIVE_WINDOW", False); + dxc_a_motif_hints = XInternAtom(dxc_display, "_MOTIF_WM_HINTS", False); + dxc_a_xdnd_aware = XInternAtom(dxc_display, "XdndAware", False); + dxc_a_xdnd_enter = XInternAtom(dxc_display, "XdndEnter", False); + dxc_a_xdnd_position = XInternAtom(dxc_display, "XdndPosition", False); + dxc_a_xdnd_status = XInternAtom(dxc_display, "XdndStatus", False); + dxc_a_xdnd_leave = XInternAtom(dxc_display, "XdndLeave", False); + dxc_a_xdnd_drop = XInternAtom(dxc_display, "XdndDrop", False); + dxc_a_xdnd_finished = XInternAtom(dxc_display, "XdndFinished", False); + dxc_a_xdnd_selection = XInternAtom(dxc_display, "XdndSelection", False); + dxc_a_xdnd_copy = XInternAtom(dxc_display, "XdndActionCopy", False); + dxc_a_xdnd_type_list = XInternAtom(dxc_display, "XdndTypeList", False); + dxc_a_uri_list = XInternAtom(dxc_display, "text/uri-list", False); + XStoreName(dxc_display, dxc_window, title); + XChangeProperty(dxc_display, dxc_window, dxc_a_net_wm_name, dxc_a_utf8, 8, PropModeReplace, + (const unsigned char *)title, (int)strlen(title)); + // Sizes the window may take, in pixels. A window that cannot be resized is held to the + // size it opened at; otherwise only the minimum is stated. + { + XSizeHints hints; + memset(&hints, 0, sizeof hints); + if (dxc_options.min_width > 0 || dxc_options.min_height > 0) { + hints.flags |= PMinSize; + hints.min_width = (int)((float)dxc_options.min_width * dxc_scale + 0.5f); + hints.min_height = (int)((float)dxc_options.min_height * dxc_scale + 0.5f); + } + if (!dxc_options.resizable) { + hints.flags |= PMinSize | PMaxSize; + hints.min_width = hints.max_width = width; + hints.min_height = hints.max_height = height; + } + if (hints.flags != 0) XSetWMNormalHints(dxc_display, dxc_window, &hints); + } + if (!dxc_options.system_chrome) { + // No frame of the manager's: the renderer draws the caption and the edges. The + // fields are flags, functions, decorations, input mode and status; only the + // decorations are set, and to none. + unsigned long motif[5] = { 2, 0, 0, 0, 0 }; + XChangeProperty(dxc_display, dxc_window, dxc_a_motif_hints, dxc_a_motif_hints, 32, + PropModeReplace, (unsigned char *)motif, 5); + } + { + // This window takes files dropped on it, by version 5 of the protocol. + long version = 5; + XChangeProperty(dxc_display, dxc_window, dxc_a_xdnd_aware, XA_ATOM, 32, PropModeReplace, + (unsigned char *)&version, 1); + } + dxc_open_input_method(); XMapWindow(dxc_display, dxc_window); XFlush(dxc_display); dxc_width = width; @@ -533,7 +1249,7 @@ void dxc_native_window_size(void *window_pointer, int32_t *width, int32_t *heigh (void)window_pointer; *width = dxc_width; *height = dxc_height; - *scale = 1.0f; + *scale = dxc_scale; } int32_t dxc_native_frame_begin(void *window_pointer) { diff --git a/renderer/desktop/src/AppKitWindow.kt b/renderer/desktop/src/AppKitWindow.kt index e0d8f8875..3f2c2ebf9 100644 --- a/renderer/desktop/src/AppKitWindow.kt +++ b/renderer/desktop/src/AppKitWindow.kt @@ -116,6 +116,15 @@ private external fun setIcon(rgba: CCharPointer?, width: Int, height: Int) @CFunction("dxc_native_dropped_paths") private external fun droppedPaths(out: Pointer?, capacity: Int): Int +@CFunction("dxc_native_set_ime_spot") +private external fun setImeSpot(x: Float, y: Float) + +@CFunction("dxc_native_window_action") +private external fun windowAction(action: Int) + +@CFunction("dxc_native_window_begin_drag") +private external fun beginWindowDrag(edge: Int) + @CFunction("dxc_native_window_closed") private external fun windowClosed(): Int @@ -285,6 +294,33 @@ internal fun NativeWindow.measureCaption(): dev.darkpyonix.composerust.runtime.W ) } +/** Tells the window where the caret is, so the input method's candidates open beside it. */ +internal fun reportCaret(textInput: NativeTextInput) { + val spot = textInput.caretSpot() ?: return + if (spot == lastCaret) return + lastCaret = spot + setImeSpot(spot.x, spot.y) +} + +private var lastCaret: androidx.compose.ui.geometry.Offset? = null + +/** Brings the window forward. Safe from any thread: it is a request the window acts on in its next turn. */ +internal fun bringNativeWindowToFront() = windowAction(3) + +/** What a button of the application's own caption does to the window. */ +internal fun nativeWindowActions() = dev.darkpyonix.composerust.runtime.WindowActions( + minimise = { windowAction(0) }, + maximise = { windowAction(1) }, + close = { windowAction(2) }, +) + +/** + * Hands the move or the resize of an undecorated window to the window manager, which does + * it better than a drag measured here could: 0 moves, and 1 to 8 pull the edge or corner + * [WindowEdge] names. + */ +internal fun beginNativeWindowDrag(edge: Int) = beginWindowDrag(edge) + /** The paths of the files last dragged over the window, one string, NUL between them. */ internal fun readDroppedPaths(): String { val buffer = StackValue.get(DROPPED_PATHS_BYTES) @@ -314,6 +350,22 @@ internal fun setApplicationIcon(rgba: ByteArray, width: Int, height: Int) { } } +/** + * Puts the picture the application named on the window, once the asset has arrived. + * + * Asked each frame until it is there, because the id is known from the first batch and the + * bitmap a little later. Answers true once it has been put on, so the caller can stop. + */ +internal fun applyNamedIcon(host: dev.darkpyonix.composerust.runtime.ComposeRustHost, id: Int): Boolean { + if (id == 0) return true + val raster = host.table.assets.asset(id) as? dev.darkpyonix.composerust.ui.node.Asset.Raster + ?: return false + iconPixels(raster.bitmap)?.let { (pixels, width, height) -> + setApplicationIcon(pixels, width, height) + } + return true +} + /** The pixels [setApplicationIcon] takes, from a picture Compose holds. */ internal fun iconPixels(picture: androidx.compose.ui.graphics.ImageBitmap): Triple? { val bitmap = picture.asSkiaBitmap() @@ -453,17 +505,9 @@ private const val WINDOW_STRUCT_BYTES = 40 * watches. */ internal fun runAppKitWindow(autoExitMillis: Long? = null) { - // Before the first frame, because every piece of text drawn after this reads them. - dev.darkpyonix.composerust.design.installPlatformUiFamily() - dev.darkpyonix.composerust.ui.installReducedMotion() - dev.darkpyonix.composerust.ui.installHighContrast() - dev.darkpyonix.composerust.ui.node.platformFileDrop = { modifier, node, dispatcher -> - modifier.nativeFileDrop(node, dispatcher) - } // The window is a real one with the desktop behind it, so a design that draws glass - // can let that show through. Set before the Host starts: its first batch may already - // ask. - dev.darkpyonix.composerust.runtime.platformBacksWindowWithMaterial = { true } + // can let that show through. + installNativeWindowHooks(backdropSupported = true) // The Host is started before there is a window, because what the window should look // like is in its first batch and a window cannot be told afterwards. Started on this // thread, which is the one every later call to it is made from: the boundary is a @@ -471,19 +515,16 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { val host = dev.darkpyonix.composerust.runtime.ComposeRustHost(NativeHostConnection()) host.start() val asked = host.table.window - val backdrop = host.table.asksForWindowMaterial(host.roots) + val options = nativeWindowOptions(host, backdropSupported = true) + val backdrop = options.backdrop configureNativeWindow( - resizable = asked?.resizable ?: true, - minWidth = asked?.minWidth?.takeIf { it > 0 } ?: 0, - minHeight = asked?.minHeight?.takeIf { it > 0 } ?: 0, - systemChrome = asked?.chrome == dev.darkpyonix.composerust.protocol.Chrome.System, + resizable = options.resizable, + minWidth = options.minWidth, + minHeight = options.minHeight, + systemChrome = options.systemChrome, backdrop = backdrop, ) - val window = openNativeWindow( - asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust", - if (asked != null && asked.width > 0) asked.width else 520, - if (asked != null && asked.height > 0) asked.height else 360, - ) + val window = openNativeWindow(options.title, options.width, options.height) if (window == null) { System.err.println("compose-rust: this machine has no Metal device") return @@ -524,28 +565,13 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { ) // The application's own tree, drawn by the same interpreter every window uses. // Nothing in it knows which window it is in, which is the point. - val clipboard = WindowClipboard() - @Suppress("DEPRECATION") - val clipboardManager = WindowClipboardManager() scene.setContent { - androidx.compose.runtime.CompositionLocalProvider( - dev.darkpyonix.composerust.runtime.LocalSystemDarkObserver provides { - rememberSystemDark().value - }, - androidx.compose.ui.platform.LocalClipboard provides clipboard, - androidx.compose.ui.platform.LocalClipboardManager provides clipboardManager, - ) { - dev.darkpyonix.composerust.runtime.ComposeRustContent( - host, - Modifier.fillMaxSize(), - caption = caption.value, - ) - } + NativeWindowContent(host, caption.value, actions = null) } - installApplicationMenu(asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust") + installApplicationMenu(options.title) val started = System.nanoTime() - var iconId = 0 + var iconSettled = false try { // A plain loop rather than a clock. Pacing is the frame clock's work and comes // later; what this has to show is that what the window hears reaches the scene @@ -571,16 +597,7 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { // The application's picture, once the asset it named has arrived. The id is // known from the first batch and the bitmap a little later, so this asks each // frame until it is there and then stops. - if (iconId == 0 && asked != null && asked.icon != 0) { - val raster = host.table.assets.asset(asked.icon) - as? dev.darkpyonix.composerust.ui.node.Asset.Raster - if (raster != null) { - iconId = asked.icon - iconPixels(raster.bitmap)?.let { (pixels, width, height) -> - setApplicationIcon(pixels, width, height) - } - } - } + if (!iconSettled) iconSettled = applyNamedIcon(host, asked?.icon ?: 0) var heard = false var drew = false for (event in drainWindowEvents()) { @@ -620,6 +637,7 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { // a window that has gone still is exactly where that would be forgotten. // Costs a comparison when nothing has changed, which is almost always. semantics.pushIfChanged(afterDrawing = drew) + reportCaret(textInput) } } finally { scene.close() diff --git a/renderer/desktop/src/NativeClipboard.kt b/renderer/desktop/src/NativeClipboard.kt index b415a88b9..0802d411d 100644 --- a/renderer/desktop/src/NativeClipboard.kt +++ b/renderer/desktop/src/NativeClipboard.kt @@ -33,7 +33,7 @@ internal class WindowClipboardManager : ClipboardManager { override fun hasText(): Boolean = clipboardText() != null } -internal class WindowClipboard : Clipboard { +internal class WindowClipboardImpl : Clipboard { override suspend fun getClipEntry(): ClipEntry? = clipboardText()?.let { ClipEntry(StringSelection(it)) } @@ -46,3 +46,9 @@ internal class WindowClipboard : Clipboard { override val nativeClipboard: NativeClipboard get() = this } + +/** One of each for every window: they hold nothing, they only ask the platform. */ +internal val WindowClipboard: Clipboard = WindowClipboardImpl() + +@Suppress("DEPRECATION") +internal val WindowClipboardManagerInstance: ClipboardManager = WindowClipboardManager() diff --git a/renderer/desktop/src/NativeWindowShell.kt b/renderer/desktop/src/NativeWindowShell.kt new file mode 100644 index 000000000..88f4581a8 --- /dev/null +++ b/renderer/desktop/src/NativeWindowShell.kt @@ -0,0 +1,181 @@ +package dev.darkpyonix.composerust.ui.platform + +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.BoxScope +import androidx.compose.foundation.gestures.awaitEachGesture +import androidx.compose.foundation.gestures.awaitFirstDown +import androidx.compose.foundation.layout.fillMaxHeight +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width +import androidx.compose.ui.Alignment +import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.unit.dp +import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalClipboard +import androidx.compose.ui.platform.LocalClipboardManager +import dev.darkpyonix.composerust.protocol.Chrome +import dev.darkpyonix.composerust.runtime.ComposeRustContent +import dev.darkpyonix.composerust.runtime.ComposeRustHost +import dev.darkpyonix.composerust.runtime.LocalSystemDarkObserver +import dev.darkpyonix.composerust.runtime.LocalWindowActions +import dev.darkpyonix.composerust.runtime.WindowActions +import dev.darkpyonix.composerust.runtime.WindowCaption +import dev.darkpyonix.composerust.runtime.asksForWindowMaterial + +// What every window of our own has in common, so that each platform's file is only what is +// different about its window: the things the application asked of it, the hooks that make +// the tree behave as it does on a desktop, and the content that goes inside. + +/** What the application asked of its window in its first batch. */ +internal data class NativeWindowOptions( + val title: String, + val width: Int, + val height: Int, + val resizable: Boolean, + val minWidth: Int, + val minHeight: Int, + val systemChrome: Boolean, + val backdrop: Boolean, +) + +/** + * Reads the window's options from the Host's first batch. + * + * [backdropSupported] says whether this platform's window can show what is behind it, + * because asking for a material the window cannot draw would leave a design drawing for a + * desktop that never shows through. + */ +internal fun nativeWindowOptions(host: ComposeRustHost, backdropSupported: Boolean): NativeWindowOptions { + val asked = host.table.window + return NativeWindowOptions( + title = asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust", + width = if (asked != null && asked.width > 0) asked.width else DEFAULT_WIDTH, + height = if (asked != null && asked.height > 0) asked.height else DEFAULT_HEIGHT, + resizable = asked?.resizable ?: true, + minWidth = asked?.minWidth?.takeIf { it > 0 } ?: 0, + minHeight = asked?.minHeight?.takeIf { it > 0 } ?: 0, + systemChrome = asked?.chrome == Chrome.System, + backdrop = backdropSupported && host.table.asksForWindowMaterial(host.roots), + ) +} + +private const val DEFAULT_WIDTH = 520 +private const val DEFAULT_HEIGHT = 360 + +/** + * Installs what the tree reads from the platform, before the Host starts: its first batch + * may already ask. + */ +internal fun installNativeWindowHooks(backdropSupported: Boolean) { + dev.darkpyonix.composerust.design.installPlatformUiFamily() + dev.darkpyonix.composerust.ui.installReducedMotion() + dev.darkpyonix.composerust.ui.installHighContrast() + dev.darkpyonix.composerust.ui.installResizeCursor() + dev.darkpyonix.composerust.ui.node.platformFileDrop = { modifier, node, dispatcher -> + modifier.nativeFileDrop(node, dispatcher) + } + dev.darkpyonix.composerust.runtime.platformBacksWindowWithMaterial = { backdropSupported } +} + +/** + * The application's tree inside a window of our own. + * + * [overlay] is drawn last, over the content: the resize edges of a window with no frame of + * the system's. [drag] is drawn first, under it, so a widget in the caption takes the press + * and the strip only sees the room around it. + */ +@Suppress("DEPRECATION") +@Composable +internal fun NativeWindowContent( + host: ComposeRustHost, + caption: WindowCaption, + actions: WindowActions?, + drag: @Composable BoxScope.() -> Unit = {}, + overlay: @Composable BoxScope.() -> Unit = {}, +) { + CompositionLocalProvider( + LocalSystemDarkObserver provides { rememberSystemDark().value }, + LocalClipboard provides WindowClipboard, + LocalClipboardManager provides WindowClipboardManagerInstance, + LocalWindowActions provides actions, + ) { + Box(Modifier.fillMaxSize()) { + drag() + ComposeRustContent(host, Modifier.fillMaxSize(), caption = caption) + overlay() + } + } +} + +/** + * The strip across the top of a window with no frame of the system's that moves it when + * pressed, and maximises it when pressed twice. + * + * Drawn under the content, so a widget in the caption takes the press and the strip only + * sees the room around it. The move itself is the window manager's, handed over at the + * press: a drag measured here would lag it. + */ +@Composable +internal fun NativeCaptionDrag(height: androidx.compose.ui.unit.Dp, actions: WindowActions?) { + var lastPress by androidx.compose.runtime.remember { androidx.compose.runtime.mutableLongStateOf(0L) } + Box( + Modifier + .fillMaxWidth() + .height(height) + .pointerInput(Unit) { + awaitEachGesture { + awaitFirstDown() + val now = System.nanoTime() + if (now - lastPress < DOUBLE_PRESS_NANOS) { + lastPress = 0L + actions?.maximise?.invoke() + } else { + lastPress = now + beginNativeWindowDrag(0) + } + } + }, + ) +} + +private const val DOUBLE_PRESS_NANOS = 400_000_000L + +/** How wide the invisible strip along each edge of a window with no frame is. */ +private val EDGE_GRIP = 6.dp + +/** + * The eight edges of a window with no frame of the system's, as things to press. + * + * Drawn last so they sit over the content, and as wide as a pointer rather than as anything + * visible. The resize is the window manager's, handed over at the press. + */ +@Composable +internal fun BoxScope.NativeResizeEdges() { + // The numbers are the ones beginNativeWindowDrag takes: left, right, top, bottom, then + // the four corners. + Edge(1, Modifier.fillMaxHeight().width(EDGE_GRIP), Alignment.CenterStart) + Edge(2, Modifier.fillMaxHeight().width(EDGE_GRIP), Alignment.CenterEnd) + Edge(3, Modifier.fillMaxWidth().height(EDGE_GRIP), Alignment.TopCenter) + Edge(4, Modifier.fillMaxWidth().height(EDGE_GRIP), Alignment.BottomCenter) + Edge(5, Modifier.size(EDGE_GRIP), Alignment.TopStart) + Edge(6, Modifier.size(EDGE_GRIP), Alignment.TopEnd) + Edge(7, Modifier.size(EDGE_GRIP), Alignment.BottomStart) + Edge(8, Modifier.size(EDGE_GRIP), Alignment.BottomEnd) +} + +@Composable +private fun BoxScope.Edge(edge: Int, size: Modifier, alignment: Alignment) { + Box( + Modifier.align(alignment).then(size).pointerInput(edge) { + awaitEachGesture { + awaitFirstDown() + beginNativeWindowDrag(edge) + } + }, + ) +} diff --git a/renderer/desktop/src/RendererEntryPoints.kt b/renderer/desktop/src/RendererEntryPoints.kt index 3d8017305..0193d8a03 100644 --- a/renderer/desktop/src/RendererEntryPoints.kt +++ b/renderer/desktop/src/RendererEntryPoints.kt @@ -7,10 +7,10 @@ import org.graalvm.nativeimage.c.function.CEntryPoint import org.graalvm.nativeimage.c.type.CCharPointer import org.graalvm.nativeimage.c.type.CTypeConversion import dev.darkpyonix.composerust.ui.platform.NativeHostConnection +import dev.darkpyonix.composerust.ui.platform.bringNativeWindowToFront import dev.darkpyonix.composerust.ui.platform.runAppKitWindow import dev.darkpyonix.composerust.ui.platform.runWin32Window import dev.darkpyonix.composerust.ui.platform.runX11Window -import org.graalvm.nativeimage.c.function.CFunction // C entry points of the renderer shared library. // @@ -26,22 +26,15 @@ import org.graalvm.nativeimage.c.function.CFunction // `checkNotNullParameter` call for every non-null reference parameter, and that call would // pass the word value as an Object. -// Implemented in the C shim this library is entered through, which is the only code that -// can put a layer behind the window: the window belongs to it. Declared here rather than -// beside the renderer so that a development run on a JVM, which has no shim and no such -// window, never loads a GraalVM type. -@CFunction("dxc_set_window_material") -private external fun setWindowMaterial(asked: Int) - @CEntryPoint(name = "compose_rust_renderer_run_impl") fun rendererRun(thread: IsolateThread?, libraryDir: CCharPointer?): Int = try { configureRuntimeLayout(CTypeConversion.toJavaString(libraryDir)) - // Which window this platform opens. macOS opens one of its own, made from AppKit - // and Metal with no toolkit between; the other platforms still open the - // toolkit's. The C files behind each answer for the same symbols, so the one - // compiled into an image is the one that can be reached: which platform this is - // decides, and nothing is read from the environment. + // Which window this platform opens. Each desktop opens one of its own, made from + // the platform's windowing and graphics APIs with no toolkit between: AppKit and + // Metal, Win32 and Direct3D 12, X11 and GLX. The C files behind each answer for the + // same symbols, so the one compiled into an image is the one that can be reached: + // which platform this is decides, and nothing is read from the environment. val platform = System.getProperty("os.name", "") val autoExitMillis = (System.getenv("COMPOSE_RUST_AUTOEXIT_MS") ?: System.getenv("DIOXUS_COMPOSE_AUTOEXIT_MS")) @@ -56,42 +49,32 @@ fun rendererRun(thread: IsolateThread?, libraryDir: CCharPointer?): Int = NativeDesktopNotifications() platform.startsWith("Linux") -> DBusNotifications( open = { JvmBusConnection.open(wake = FrameRequests::request) }, - bringToFront = ::bringAwtWindowToFront, + bringToFront = ::bringNativeWindowToFront, applicationName = JvmBusConnection.applicationName(), ) else -> UnsupportedNotifications } - if (platform.startsWith("Mac")) { - // Before anything else on this path. The toolkit, if it is ever woken, asks - // the main thread to run the application, and this thread is the one drawing - // the frames: that request is delivered on the first frame and never comes - // back. Saying up front that there is no display to open keeps the toolkit - // from asking, and nothing on this path wants one. - System.setProperty("java.awt.headless", "true") - runAppKitWindow(autoExitMillis) - return@rendererRun 0 - } - if (System.getenv("DXC_WIN32_WINDOW") != null && platform.startsWith("Windows")) { - runWin32Window() - return@rendererRun 0 - } - if (System.getenv("DXC_X11_WINDOW") != null && platform.startsWith("Linux")) { - runX11Window() - return@rendererRun 0 - } - dev.darkpyonix.composerust.ui.node.platformWindowMaterial = { asked -> - setWindowMaterial(if (asked) 1 else 0) - } - // Whether asking for a material actually put anything behind the window. The - // toolkit's window cannot, on any platform that still opens one, so its design - // system draws for an opaque window rather than for a desktop that never shows - // through. - dev.darkpyonix.composerust.runtime.platformBacksWindowWithMaterial = { false } - // Lets automated smoke tests close the window; unset in normal use. - runRenderer(autoExitMillis) { - NativeHostConnection() + // Every desktop opens a window of its own, so there is no display for the toolkit to + // open. Saying so up front keeps it from being woken: on macOS it would ask the main + // thread to run the application, and this thread is the one drawing the frames, so + // that request would be delivered on the first frame and never come back. + System.setProperty("java.awt.headless", "true") + when { + platform.startsWith("Mac") -> { + runAppKitWindow(autoExitMillis) + return@rendererRun 0 + } + platform.startsWith("Windows") -> { + runWin32Window(autoExitMillis) + return@rendererRun 0 + } + platform.startsWith("Linux") -> { + runX11Window(autoExitMillis) + return@rendererRun 0 + } } - 0 + System.err.println("compose-rust: there is no window for the platform \"$platform\"") + 1 } catch (t: Throwable) { // Nothing may unwind across the C boundary: a Kotlin exception crossing into C is // undefined behaviour, and a protocol error must never abort the process. Report it @@ -104,21 +87,3 @@ fun rendererRun(thread: IsolateThread?, libraryDir: CCharPointer?): Int = fun rendererRequestFrame(thread: IsolateThread?) { FrameRequests.request() } - -/** - * Brings the application's window up for a press on a notification's body: back from - * being minimised, and in front of the others. - * - * On the toolkit's thread, because that is the only thread a toolkit window may be touched - * from, and a press is reported from wherever the bus was read. - */ -private fun bringAwtWindowToFront() { - java.awt.EventQueue.invokeLater { - val window = java.awt.Window.getWindows().firstOrNull { it.isVisible } ?: return@invokeLater - if (window is java.awt.Frame) { - window.extendedState = window.extendedState and java.awt.Frame.ICONIFIED.inv() - } - window.toFront() - window.requestFocus() - } -} diff --git a/renderer/desktop/src/TextInputSession.kt b/renderer/desktop/src/TextInputSession.kt index 6f8ce234b..42776a37d 100644 --- a/renderer/desktop/src/TextInputSession.kt +++ b/renderer/desktop/src/TextInputSession.kt @@ -59,6 +59,16 @@ class NativeTextInput { * without committing anything, which is what an input method asks for when the reader * backs out of it. */ + /** + * Where the caret is, in the scene's pixels from its top left, or null where no field + * has the keyboard or has not been laid out yet. The input method opens its candidate + * window there. + */ + fun caretSpot(): androidx.compose.ui.geometry.Offset? { + val rect = session?.focusedRectInRoot?.invoke() ?: return null + return androidx.compose.ui.geometry.Offset(rect.left, rect.bottom) + } + fun compose(text: String) { val request = session ?: return request.onEditCommand(listOf(SetComposingTextCommand(AnnotatedString(text), 1))) diff --git a/renderer/desktop/src/Win32Window.kt b/renderer/desktop/src/Win32Window.kt index 8911fe268..8f93956f1 100644 --- a/renderer/desktop/src/Win32Window.kt +++ b/renderer/desktop/src/Win32Window.kt @@ -272,9 +272,11 @@ private fun forgetFrameCallback() = setDrawCallback( * composed, painted by Skia into a swapchain buffer Direct3D gave us, reaching the screen * with no toolkit anywhere between. * - * Reached by setting `DXC_WIN32_WINDOW`, so the ordinary path is untouched. + * [autoExitMillis] closes the window by itself after that long, for runs nobody watches. */ -internal fun runWin32Window() { +internal fun runWin32Window(autoExitMillis: Long? = null) { + // The window shows nothing behind itself, so a design is told it has no material. + installNativeWindowHooks(backdropSupported = false) // The Host is started before there is a window, because what the window should look // like is in its first batch and a window cannot be told afterwards. Started on this // thread, which is the one every later call to it is made from: the boundary is a @@ -282,11 +284,15 @@ internal fun runWin32Window() { val host = dev.darkpyonix.composerust.runtime.ComposeRustHost(NativeHostConnection()) host.start() val asked = host.table.window - val window = openWin32Window( - asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust", - if (asked != null && asked.width > 0) asked.width else 520, - if (asked != null && asked.height > 0) asked.height else 360, + val options = nativeWindowOptions(host, backdropSupported = false) + configureNativeWindow( + resizable = options.resizable, + minWidth = options.minWidth, + minHeight = options.minHeight, + systemChrome = options.systemChrome, + backdrop = false, ) + val window = openWin32Window(options.title, options.width, options.height) if (window == null) { System.err.println("compose-rust: this machine has no Direct3D 12 adapter") host.shutdown() @@ -330,7 +336,20 @@ internal fun runWin32Window() { ) // The application's own tree, drawn by the same interpreter the toolkit path uses. // Nothing in it knows which of the two it is running on, which is the point. - scene.setContent { dev.darkpyonix.composerust.runtime.ComposeRustContent(host) } + // The strip the system's caption occupied, which the window gave up to the content. The + // buttons in it are the application's own, so they are the window's to operate. + val caption = if (options.systemChrome) { + dev.darkpyonix.composerust.runtime.WindowCaption.None + } else { + dev.darkpyonix.composerust.runtime.WindowCaption(height = windowsCaptionHeight) + } + scene.setContent { + NativeWindowContent( + host, + caption, + actions = if (options.systemChrome) null else nativeWindowActions(), + ) + } // What a frame is, wherever the ask comes from. The loop below is one caller and the // window's own resize handling is the other, and they draw the same frame. @@ -351,6 +370,8 @@ internal fun runWin32Window() { } } registerFrameCallback() + val started = System.nanoTime() + var iconSettled = false try { // Rests only when the last turn found nothing to do. A frame that drew has @@ -358,6 +379,12 @@ internal fun runWin32Window() { // that would halve the rate of anything that animates. var busy = true while (!isWindowClosed()) { + if (autoExitMillis != null && + (System.nanoTime() - started) / NANOS_PER_MILLI >= autoExitMillis + ) { + break + } + if (!iconSettled) iconSettled = applyNamedIcon(host, asked?.icon ?: 0) // Cleared before the window is given its turn rather than after. A drag of an // edge draws its frames from inside that turn, and a turn that forgot them // would be a window that said nothing about itself for the length of a drag, @@ -375,6 +402,14 @@ internal fun runWin32Window() { } // Told which desktop it is, because the key numbers differ: the shared // table is macOS's, and without this Home arrives as Enter. + if (event.kind == WindowEvent.FILES_ENTERED || + event.kind == WindowEvent.FILES_DROPPED || + event.kind == WindowEvent.FILES_EXITED + ) { + routeFileDrop(event.kind, androidx.compose.ui.geometry.Offset(event.x, event.y)) { + readDroppedPaths() + } + } scene.receive(event, win32 = true) textInput.receive(event) heard = true @@ -389,6 +424,7 @@ internal fun runWin32Window() { // tree that changed on the last one is a tree nobody has been told about, and // a window that has gone still is exactly where that would be forgotten. semantics.pushIfChanged(afterDrawing = drew) + reportCaret(textInput) busy = heard || drew } } finally { @@ -470,5 +506,6 @@ private fun drawFrame( return fitted } +private const val NANOS_PER_MILLI = 1_000_000L private const val FRAME_SECONDS = 0.016 private const val FRAME_NANOS = 16_000_000L diff --git a/renderer/desktop/src/X11Window.kt b/renderer/desktop/src/X11Window.kt index 491b6e96d..b055b53ad 100644 --- a/renderer/desktop/src/X11Window.kt +++ b/renderer/desktop/src/X11Window.kt @@ -14,6 +14,7 @@ import org.graalvm.nativeimage.c.type.CIntPointer import org.graalvm.nativeimage.c.type.CTypeConversion import org.graalvm.word.Pointer import org.graalvm.word.WordFactory +import androidx.compose.ui.unit.dp // X11 and GLX own the window and framebuffer. The event record, scene input conversion // and window content are shared with the other desktop windows. @@ -93,20 +94,26 @@ private const val GL_RGBA8 = 0x8058 * the painter registered below. A loop that drew it on its next turn would be a window * whose edge moves before its content does. * - * Reached by setting `DXC_X11_WINDOW`, so the ordinary path is untouched. + * [autoExitMillis] closes the window by itself after that long, for runs nobody watches. */ -internal fun runX11Window() { +internal fun runX11Window(autoExitMillis: Long? = null) { + // The window shows nothing behind itself, so a design is told it has no material. + installNativeWindowHooks(backdropSupported = false) // The Host is started before there is a window, because what the window should look // like is in its first batch and a window cannot be told afterwards. Started on this // thread, which is the one every later call to it is made from. val host = dev.darkpyonix.composerust.runtime.ComposeRustHost(NativeHostConnection()) host.start() val asked = host.table.window - val window = openX11Window( - asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust", - if (asked != null && asked.width > 0) asked.width else 520, - if (asked != null && asked.height > 0) asked.height else 360, + val options = nativeWindowOptions(host, backdropSupported = false) + configureNativeWindow( + resizable = options.resizable, + minWidth = options.minWidth, + minHeight = options.minHeight, + systemChrome = options.systemChrome, + backdrop = false, ) + val window = openX11Window(options.title, options.width, options.height) if (window == null) { System.err.println("compose-rust: X11 or a GLX visual is unavailable") host.shutdown() @@ -142,7 +149,27 @@ internal fun runX11Window() { ) // The application's own tree, drawn by the same interpreter the toolkit path uses. // Nothing in it knows which of the two it is running on, which is the point. - scene.setContent { dev.darkpyonix.composerust.runtime.ComposeRustContent(host) } + // Without the manager's frame the renderer draws the caption: a strip that moves the + // window, the three buttons, and the edges that resize it. The window manager does the + // moving and the resizing; what is drawn here is only where a press starts it. + val framed = options.systemChrome + val caption = if (framed) { + dev.darkpyonix.composerust.runtime.WindowCaption.None + } else { + dev.darkpyonix.composerust.runtime.WindowCaption(height = linuxCaptionHeight) + } + val actions = if (framed) null else nativeWindowActions() + scene.setContent { + NativeWindowContent( + host, + caption, + actions, + drag = { if (!framed) NativeCaptionDrag(linuxCaptionHeight, actions) }, + overlay = { if (!framed && options.resizable) NativeResizeEdges() }, + ) + } + val started = System.nanoTime() + var iconSettled = false // A clock rather than a count of turns, because a turn and a frame are no longer the // same thing: a resize draws its own, and a count only the loop advanced would hand @@ -169,6 +196,12 @@ internal fun runX11Window() { // nothing of a scene that has closed. setX11FramePainter { frames.draw() } while (!isWindowClosed()) { + if (autoExitMillis != null && + (System.nanoTime() - started) / NANOS_PER_MILLI >= autoExitMillis + ) { + break + } + if (!iconSettled) iconSettled = applyNamedIcon(host, asked?.icon ?: 0) // The window's own turn, before anything is read from it. This thread is the // one the display server answers on, so the events of this frame arrive here // or not at all. Waiting the frame's length rather than sleeping afterwards, @@ -185,7 +218,16 @@ internal fun runX11Window() { if (report && event.kind != WindowEvent.POINTER_MOVE) { System.err.println("compose-rust: window heard $event") } + if (event.kind == WindowEvent.FILES_ENTERED || + event.kind == WindowEvent.FILES_DROPPED || + event.kind == WindowEvent.FILES_EXITED + ) { + routeFileDrop(event.kind, androidx.compose.ui.geometry.Offset(event.x, event.y)) { + readDroppedPaths() + } + } scene.receive(event) + textInput.receive(event) heard = true } // Only when there is something to draw. A window that is being resized has @@ -199,6 +241,7 @@ internal fun runX11Window() { // tree that changed on the last one is a tree nobody has been told about, and // a window that has gone still is exactly where that would be forgotten. semantics.pushIfChanged(afterDrawing = drew) + reportCaret(textInput) } } finally { // Before the scene closes. A resize arriving between the two would otherwise ask a @@ -256,4 +299,8 @@ private fun drawFrame( * A frame at sixty per second. It is a ceiling rather than a pace: anything arriving sooner * ends the wait, and a resize is drawn inside it rather than after it. */ +private const val NANOS_PER_MILLI = 1_000_000L private const val FRAME_SECONDS = 0.016 + +/** The caption strip a window with no frame of the system's draws for itself. */ +internal val linuxCaptionHeight = 32.dp diff --git a/scripts/tests/macos-window-default.test.sh b/scripts/tests/macos-window-default.test.sh deleted file mode 100755 index b9ee92770..000000000 --- a/scripts/tests/macos-window-default.test.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env bash -# The window macOS opens is the one of our own, with no environment variable asking for it. -# -# It used to be reached only when a variable was set, so every run without it opened the -# toolkit's window and the one of our own was exercised by hand. A test cannot open a window -# here, but it can read the two places that decide: the Kotlin entry point, which must not -# consult the environment to choose a macOS window, and the C entry, which must not start -# the older arrangement of a second thread for the toolkit. -set -uo pipefail - -repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" -kotlin="$repo_root/renderer/desktop/src/RendererEntryPoints.kt" -entry="$repo_root/renderer/desktop/c/renderer_entry.c" -status=0 - -fail() { echo "FAIL: $1"; status=1; } - -[[ -f "$kotlin" && -f "$entry" ]] || { echo "FAIL: the entry points are missing"; exit 1; } - -if grep -rn "DXC_APPKIT_WINDOW" "$repo_root/renderer" --include='*.kt' --include='*.c' --include='*.m' --include='*.sh' >/dev/null; then - fail "something still reads DXC_APPKIT_WINDOW, so the window depends on the environment" -fi - -if ! grep -Eq 'startsWith\("Mac"\)\) \{' "$kotlin" || ! grep -q 'runAppKitWindow(' "$kotlin"; then - fail "RendererEntryPoints.kt does not open the window of our own for every macOS run" -fi - -# Between the macOS branch and its closing brace there must be no environment read. -branch="$(awk '/if \(platform.startsWith\("Mac"\)\) \{/{on=1} on{print} on && /^ \}/{exit}' "$kotlin")" -if grep -q 'getenv' <<< "$branch"; then - fail "the macOS branch reads the environment to decide on its window" -fi - -if grep -q 'compose_rust_park_main_thread\|dxc_unify_titlebars\|DXC_APPKIT' "$entry"; then - fail "renderer_entry.c still carries the toolkit's main-thread arrangement" -fi - -[[ $status -eq 0 ]] && echo "macOS opens its own window by default: ok" -exit $status diff --git a/scripts/tests/native-window-default.test.sh b/scripts/tests/native-window-default.test.sh new file mode 100755 index 000000000..59e57ae6d --- /dev/null +++ b/scripts/tests/native-window-default.test.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# Every desktop opens the window of our own, with no environment variable asking for it. +# +# Each was reached only when a variable was set, so every run without it opened the +# toolkit's window and the one of our own was exercised by hand. A test cannot open a window +# here, but it can read the two places that decide: the Kotlin entry point, which must name +# a window for each platform without consulting the environment, and the C entry, which must +# not start the older arrangement of a second thread for the toolkit. +set -uo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +kotlin="$repo_root/renderer/desktop/src/RendererEntryPoints.kt" +entry="$repo_root/renderer/desktop/c/renderer_entry.c" +status=0 + +fail() { echo "FAIL: $1"; status=1; } + +[[ -f "$kotlin" && -f "$entry" ]] || { echo "FAIL: the entry points are missing"; exit 1; } + +if grep -rnE "DXC_(APPKIT|WIN32|X11)_WINDOW" "$repo_root/renderer" --include='*.kt' --include='*.c' --include='*.m' --include='*.sh' --include='*.ps1' >/dev/null; then + fail "something still reads a DXC_*_WINDOW variable, so the window depends on the environment" +fi + +for pair in 'Mac:runAppKitWindow' 'Windows:runWin32Window' 'Linux:runX11Window'; do + platform="${pair%%:*}" + function="${pair##*:}" + if ! grep -Eq "platform\.startsWith\(\"$platform\"\) -> \{" "$kotlin" || ! grep -q "$function(" "$kotlin"; then + fail "RendererEntryPoints.kt does not open the window of our own for every $platform run" + fi +done + +# From the choice of window to the end of it there must be no environment read. +choice="$(awk '/when \{/ && !seen && /./ {on=0} /System.setProperty\("java.awt.headless"/{on=1} on{print} /^ \} catch/{exit}' "$kotlin")" +if grep -q 'getenv' <<< "$choice"; then + fail "the choice of window reads the environment" +fi + +# The toolkit's window is not an option on any desktop any more. +if grep -qE 'runRenderer\(' "$kotlin"; then + fail "RendererEntryPoints.kt still falls back to the toolkit's window" +fi + +if grep -q 'compose_rust_park_main_thread\|dxc_unify_titlebars\|DXC_APPKIT\|dxc_reclaim_caption\|SunAwtFrame' "$entry"; then + fail "renderer_entry.c still carries the toolkit's window arrangement" +fi + +[[ $status -eq 0 ]] && echo "every desktop opens its own window by default: ok" +exit $status diff --git a/scripts/tests/windows-caption-metrics.test.sh b/scripts/tests/windows-caption-metrics.test.sh index 6985ea081..4bc6e729f 100755 --- a/scripts/tests/windows-caption-metrics.test.sh +++ b/scripts/tests/windows-caption-metrics.test.sh @@ -15,7 +15,7 @@ set -uo pipefail repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" cd "$repo_root" -entry="renderer/desktop/c/renderer_entry.c" +entry="renderer/desktop/c/win32_window.c" chrome="renderer/desktop/src/WindowChrome.kt" rules="renderer/desktop/src/renderer/ComponentRules.kt" status=0 From 9926f6fe040dc1364ddd317c1e59417e97d5294c Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 18:28:48 +0900 Subject: [PATCH 02/20] Fix: Hold the caption's last press in a plain array (NFR-14) --- renderer/desktop/src/NativeWindowShell.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/renderer/desktop/src/NativeWindowShell.kt b/renderer/desktop/src/NativeWindowShell.kt index 88f4581a8..0bcc3a31f 100644 --- a/renderer/desktop/src/NativeWindowShell.kt +++ b/renderer/desktop/src/NativeWindowShell.kt @@ -122,7 +122,7 @@ internal fun NativeWindowContent( */ @Composable internal fun NativeCaptionDrag(height: androidx.compose.ui.unit.Dp, actions: WindowActions?) { - var lastPress by androidx.compose.runtime.remember { androidx.compose.runtime.mutableLongStateOf(0L) } + val lastPress = androidx.compose.runtime.remember { LongArray(1) } Box( Modifier .fillMaxWidth() @@ -131,11 +131,11 @@ internal fun NativeCaptionDrag(height: androidx.compose.ui.unit.Dp, actions: Win awaitEachGesture { awaitFirstDown() val now = System.nanoTime() - if (now - lastPress < DOUBLE_PRESS_NANOS) { - lastPress = 0L + if (now - lastPress[0] < DOUBLE_PRESS_NANOS) { + lastPress[0] = 0L actions?.maximise?.invoke() } else { - lastPress = now + lastPress[0] = now beginNativeWindowDrag(0) } } From 5d2bce062798129543e204ff540ec4bc6c5d8aed Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 19:40:18 +0900 Subject: [PATCH 03/20] Chore: Keep Linux on the toolkit window until the X11 window is checked (NFR-14) The X11 window code stays, reached by DXC_X11_WINDOW. Windows and macOS are unchanged. The Linux default flips in a separate change with the Docker and hardware check results. --- CHANGELOG.md | 8 +-- renderer/desktop/c/renderer_entry.c | 27 ++++++++++ renderer/desktop/src/RendererEntryPoints.kt | 57 +++++++++++++++++---- scripts/tests/native-window-default.test.sh | 17 +++--- 4 files changed, 91 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f58a6158c..4e1d68cf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,12 @@ ## Unreleased -### Windows and Linux open their own window too +### Windows opens its own window, and Linux's is finished but not the default -The Win32 and X11 windows are now the default on the native-image path, with no environment -variable. Windows keeps its frame and takes the caption strip into the content as before +The Win32 window is now the default on the native-image path, with no environment +variable. The X11 window is finished the same way but Linux keeps the toolkit's window +until typing, copy and paste and Korean input have been checked on a real desktop; set +`DXC_X11_WINDOW` to try it. Windows keeps its frame and takes the caption strip into the content as before (the logic moved from the shim into the window's own procedure), and now honours the application's minimum size, resizable choice and icon, has a clipboard, and takes files dropped on a drop target. Linux draws its own caption and edges and hands the move and the diff --git a/renderer/desktop/c/renderer_entry.c b/renderer/desktop/c/renderer_entry.c index 4241d6ccb..bcc52210e 100644 --- a/renderer/desktop/c/renderer_entry.c +++ b/renderer/desktop/c/renderer_entry.c @@ -43,6 +43,33 @@ int32_t compose_rust_renderer_run_impl(graal_isolatethread_t *thread, const char void compose_rust_renderer_request_frame_impl(graal_isolatethread_t *thread); int32_t compose_rust_renderer_run(void); +/** + * Whether the application asked for a material behind its window. + * + * Off until the renderer says otherwise, because a window that did not ask must come up + * the way it always did: no effect view is built for it and nothing about its drawing + * changes. The renderer knows the answer only after the first batch has been applied, + * and the macOS poller is still running by then, so a late yes is still acted on. + * + * Exported on every desktop so the renderer has one symbol to call. Only macOS has + * anything to do with the answer today; Windows and Linux record it and draw as before. + */ +#ifdef _WIN32 +// MSVC's C compiler has no without an experimental switch, so Windows keeps +// the flag with the Interlocked functions, which are full barriers like atomic_store. +static volatile LONG dxc_window_material_asked; + +void dxc_set_window_material(int32_t asked) { + InterlockedExchange(&dxc_window_material_asked, asked != 0); +} +#else +static atomic_int dxc_window_material_asked; + +void dxc_set_window_material(int32_t asked) { + atomic_store(&dxc_window_material_asked, asked != 0); +} +#endif + #ifdef __APPLE__ #include #include diff --git a/renderer/desktop/src/RendererEntryPoints.kt b/renderer/desktop/src/RendererEntryPoints.kt index 0193d8a03..38bab9ff2 100644 --- a/renderer/desktop/src/RendererEntryPoints.kt +++ b/renderer/desktop/src/RendererEntryPoints.kt @@ -5,6 +5,7 @@ package dev.darkpyonix.composerust.ui.platform import org.graalvm.nativeimage.IsolateThread import org.graalvm.nativeimage.c.function.CEntryPoint import org.graalvm.nativeimage.c.type.CCharPointer +import org.graalvm.nativeimage.c.function.CFunction import org.graalvm.nativeimage.c.type.CTypeConversion import dev.darkpyonix.composerust.ui.platform.NativeHostConnection import dev.darkpyonix.composerust.ui.platform.bringNativeWindowToFront @@ -26,6 +27,13 @@ import dev.darkpyonix.composerust.ui.platform.runX11Window // `checkNotNullParameter` call for every non-null reference parameter, and that call would // pass the word value as an Object. +// Implemented in the C shim this library is entered through, which is the only code that +// can put a layer behind the window: the window belongs to it. Declared here rather than +// beside the renderer so that a development run on a JVM, which has no shim and no such +// window, never loads a GraalVM type. +@CFunction("dxc_set_window_material") +private external fun setWindowMaterial(asked: Int) + @CEntryPoint(name = "compose_rust_renderer_run_impl") fun rendererRun(thread: IsolateThread?, libraryDir: CCharPointer?): Int = try { @@ -36,6 +44,10 @@ fun rendererRun(thread: IsolateThread?, libraryDir: CCharPointer?): Int = // same symbols, so the one compiled into an image is the one that can be reached: // which platform this is decides, and nothing is read from the environment. val platform = System.getProperty("os.name", "") + // Linux still opens the toolkit's window unless asked for the X11 one: that window + // is finished but its typing, clipboard and input method have not been checked on a + // real desktop, so it is not the default yet. + val linuxOwnWindow = platform.startsWith("Linux") && System.getenv("DXC_X11_WINDOW") != null val autoExitMillis = (System.getenv("COMPOSE_RUST_AUTOEXIT_MS") ?: System.getenv("DIOXUS_COMPOSE_AUTOEXIT_MS")) ?.toLongOrNull() @@ -49,16 +61,18 @@ fun rendererRun(thread: IsolateThread?, libraryDir: CCharPointer?): Int = NativeDesktopNotifications() platform.startsWith("Linux") -> DBusNotifications( open = { JvmBusConnection.open(wake = FrameRequests::request) }, - bringToFront = ::bringNativeWindowToFront, + bringToFront = if (linuxOwnWindow) ::bringNativeWindowToFront else ::bringAwtWindowToFront, applicationName = JvmBusConnection.applicationName(), ) else -> UnsupportedNotifications } - // Every desktop opens a window of its own, so there is no display for the toolkit to - // open. Saying so up front keeps it from being woken: on macOS it would ask the main - // thread to run the application, and this thread is the one drawing the frames, so - // that request would be delivered on the first frame and never come back. - System.setProperty("java.awt.headless", "true") + // A window of our own means there is no display for the toolkit to open. Saying so up + // front keeps it from being woken: on macOS it would ask the main thread to run the + // application, and this thread is the one drawing the frames, so that request would + // be delivered on the first frame and never come back. + if (!platform.startsWith("Linux") || linuxOwnWindow) { + System.setProperty("java.awt.headless", "true") + } when { platform.startsWith("Mac") -> { runAppKitWindow(autoExitMillis) @@ -68,13 +82,20 @@ fun rendererRun(thread: IsolateThread?, libraryDir: CCharPointer?): Int = runWin32Window(autoExitMillis) return@rendererRun 0 } - platform.startsWith("Linux") -> { + linuxOwnWindow -> { runX11Window(autoExitMillis) return@rendererRun 0 } } - System.err.println("compose-rust: there is no window for the platform \"$platform\"") - 1 + dev.darkpyonix.composerust.ui.node.platformWindowMaterial = { asked -> + setWindowMaterial(if (asked) 1 else 0) + } + dev.darkpyonix.composerust.runtime.platformBacksWindowWithMaterial = { false } + // The toolkit's window, which Linux still opens by default. + runRenderer(autoExitMillis) { + NativeHostConnection() + } + 0 } catch (t: Throwable) { // Nothing may unwind across the C boundary: a Kotlin exception crossing into C is // undefined behaviour, and a protocol error must never abort the process. Report it @@ -87,3 +108,21 @@ fun rendererRun(thread: IsolateThread?, libraryDir: CCharPointer?): Int = fun rendererRequestFrame(thread: IsolateThread?) { FrameRequests.request() } + +/** + * Brings the application's window up for a press on a notification's body: back from + * being minimised, and in front of the others. + * + * On the toolkit's thread, because that is the only thread a toolkit window may be touched + * from, and a press is reported from wherever the bus was read. + */ +private fun bringAwtWindowToFront() { + java.awt.EventQueue.invokeLater { + val window = java.awt.Window.getWindows().firstOrNull { it.isVisible } ?: return@invokeLater + if (window is java.awt.Frame) { + window.extendedState = window.extendedState and java.awt.Frame.ICONIFIED.inv() + } + window.toFront() + window.requestFocus() + } +} diff --git a/scripts/tests/native-window-default.test.sh b/scripts/tests/native-window-default.test.sh index 59e57ae6d..027ba0df6 100755 --- a/scripts/tests/native-window-default.test.sh +++ b/scripts/tests/native-window-default.test.sh @@ -17,11 +17,17 @@ fail() { echo "FAIL: $1"; status=1; } [[ -f "$kotlin" && -f "$entry" ]] || { echo "FAIL: the entry points are missing"; exit 1; } -if grep -rnE "DXC_(APPKIT|WIN32|X11)_WINDOW" "$repo_root/renderer" --include='*.kt' --include='*.c' --include='*.m' --include='*.sh' --include='*.ps1' >/dev/null; then +# Linux is the exception while its window waits for a check on a real desktop: it opens the +# toolkit's unless DXC_X11_WINDOW asks, and that one variable is read in RendererEntryPoints.kt +# alone. The flip removes this allowance. +if grep -rnE "DXC_(APPKIT|WIN32)_WINDOW|DXC_X11_WINDOW" "$repo_root/renderer" --include='*.kt' --include='*.c' --include='*.m' --include='*.sh' --include='*.ps1' | grep -v 'RendererEntryPoints.kt\|Win32Window.kt\|X11Window.kt' | grep -q .; then + fail "something besides the entry point reads a DXC_*_WINDOW variable" +fi +if grep -rnE "DXC_(APPKIT|WIN32)_WINDOW" "$repo_root/renderer" --include='*.kt' --include='*.c' --include='*.m' --include='*.sh' --include='*.ps1' >/dev/null; then fail "something still reads a DXC_*_WINDOW variable, so the window depends on the environment" fi -for pair in 'Mac:runAppKitWindow' 'Windows:runWin32Window' 'Linux:runX11Window'; do +for pair in 'Mac:runAppKitWindow' 'Windows:runWin32Window'; do platform="${pair%%:*}" function="${pair##*:}" if ! grep -Eq "platform\.startsWith\(\"$platform\"\) -> \{" "$kotlin" || ! grep -q "$function(" "$kotlin"; then @@ -30,14 +36,13 @@ for pair in 'Mac:runAppKitWindow' 'Windows:runWin32Window' 'Linux:runX11Window'; done # From the choice of window to the end of it there must be no environment read. -choice="$(awk '/when \{/ && !seen && /./ {on=0} /System.setProperty\("java.awt.headless"/{on=1} on{print} /^ \} catch/{exit}' "$kotlin")" +choice="$(grep -v 'linuxOwnWindow = ' "$kotlin" | awk '/when \{/ && !seen && /./ {on=0} /System.setProperty\("java.awt.headless"/{on=1} on{print} /^ \} catch/{exit}')" if grep -q 'getenv' <<< "$choice"; then fail "the choice of window reads the environment" fi -# The toolkit's window is not an option on any desktop any more. -if grep -qE 'runRenderer\(' "$kotlin"; then - fail "RendererEntryPoints.kt still falls back to the toolkit's window" +if ! grep -q 'linuxOwnWindow ->' "$kotlin" || ! grep -q 'runX11Window(' "$kotlin"; then + fail "RendererEntryPoints.kt no longer reaches the X11 window" fi if grep -q 'compose_rust_park_main_thread\|dxc_unify_titlebars\|DXC_APPKIT\|dxc_reclaim_caption\|SunAwtFrame' "$entry"; then From 9c83cf844320a713ecafdb08d9da2673f1e900a2 Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 20:03:35 +0900 Subject: [PATCH 04/20] Fix: Map every key the X11 window knows and answer -1 for the rest (NFR-14) Unmapped keys answered 0, which is the A key, so control with C, V and X arrived as control with A and selected everything. Letters and digits are mapped to the shared board, the Windows virtual key table gains the same, XDND drops report where the last position said the files were, and a shell test reads the X11 table. --- renderer/desktop/c/x11_window.c | 33 ++++++++--- renderer/desktop/src/AppKitKeys.kt | 11 ++++ renderer/desktop/src/WindowEvents.kt | 38 ++++++++++++ renderer/desktop/test/ShortcutKeysTest.kt | 47 +++++++++++++++ scripts/tests/x11-key-codes.test.sh | 71 +++++++++++++++++++++++ 5 files changed, 192 insertions(+), 8 deletions(-) create mode 100644 renderer/desktop/test/ShortcutKeysTest.kt create mode 100644 scripts/tests/x11-key-codes.test.sh diff --git a/renderer/desktop/c/x11_window.c b/renderer/desktop/c/x11_window.c index b740c39b2..8583b93e7 100644 --- a/renderer/desktop/c/x11_window.c +++ b/renderer/desktop/c/x11_window.c @@ -177,6 +177,8 @@ static int32_t dxc_dropped_length; static Window dxc_drag_source; static int dxc_drag_has_files; static Time dxc_drop_time; +// Where the last XdndPosition said the files were, in root coordinates. +static int dxc_drag_root_x, dxc_drag_root_y; static void dxc_push_event(struct dxc_event event) { if (dxc_event_count == DXC_EVENT_CAPACITY) { @@ -202,9 +204,24 @@ static int32_t dxc_modifiers(unsigned int state) { ((state & Mod4Mask) ? (1 << 20) : 0); } -// The special key values are the shared reader's values. Printable keys also carry -// their code point, obtained from XLookupString for the current keyboard layout. +// The key numbers are the shared reader's, which are the macOS ones: the letters and digits +// by where they sit on a board, and the special keys by name. A key with no number answers +// -1, never 0, because 0 is the A key and a shortcut that does not map would otherwise arrive +// as ctrl and A. +// +// Printable keys also carry their code point, obtained from XLookupString for the current +// keyboard layout. static int32_t dxc_key_code(KeySym key) { + static const int32_t letters[26] = { + 0x00, 0x0B, 0x08, 0x02, 0x0E, 0x03, 0x05, 0x04, 0x22, 0x26, 0x28, 0x25, 0x2E, + 0x2D, 0x1F, 0x23, 0x0C, 0x0F, 0x01, 0x11, 0x20, 0x09, 0x0D, 0x07, 0x10, 0x06, + }; + static const int32_t digits[10] = { + 0x1D, 0x12, 0x13, 0x14, 0x15, 0x17, 0x16, 0x1A, 0x1C, 0x19, + }; + if (key >= XK_a && key <= XK_z) return letters[key - XK_a]; + if (key >= XK_A && key <= XK_Z) return letters[key - XK_A]; + if (key >= XK_0 && key <= XK_9) return digits[key - XK_0]; switch (key) { case XK_Return: case XK_KP_Enter: return 0x24; case XK_Tab: case XK_ISO_Left_Tab: return 0x30; @@ -220,7 +237,7 @@ static int32_t dxc_key_code(KeySym key) { case XK_End: return 0x77; case XK_Prior: return 0x74; case XK_Next: return 0x79; - default: return 0; + default: return -1; } } @@ -625,6 +642,8 @@ static int dxc_handle_dnd(XClientMessageEvent *message) { if (type == dxc_a_xdnd_position) { int root_x = (int)((message->data.l[2] >> 16) & 0xFFFF); int root_y = (int)(message->data.l[2] & 0xFFFF); + dxc_drag_root_x = root_x; + dxc_drag_root_y = root_y; if (dxc_drag_has_files) dxc_push_drag(DXC_EVENT_FILES_ENTERED, root_x, root_y); dxc_send_dnd(dxc_a_xdnd_status, dxc_drag_has_files ? 1 : 0, 0, 0, dxc_drag_has_files ? (long)dxc_a_xdnd_copy : 0); @@ -667,11 +686,9 @@ static void dxc_finish_drop(XSelectionEvent *arrived) { } } if (ok) { - Window root, child; - int root_x = 0, root_y = 0, x = 0, y = 0; - unsigned int mask = 0; - XQueryPointer(dxc_display, dxc_window, &root, &child, &root_x, &root_y, &x, &y, &mask); - dxc_push_drag(DXC_EVENT_FILES_DROPPED, root_x, root_y); + // Where the files were when they were let go, as the last position said, rather than + // where the pointer is by the time this is read. + dxc_push_drag(DXC_EVENT_FILES_DROPPED, dxc_drag_root_x, dxc_drag_root_y); } dxc_send_dnd(dxc_a_xdnd_finished, ok ? 1 : 0, ok ? (long)dxc_a_xdnd_copy : 0, 0, 0); dxc_drag_has_files = 0; diff --git a/renderer/desktop/src/AppKitKeys.kt b/renderer/desktop/src/AppKitKeys.kt index 84c2b14c3..7a796f20e 100644 --- a/renderer/desktop/src/AppKitKeys.kt +++ b/renderer/desktop/src/AppKitKeys.kt @@ -51,6 +51,17 @@ internal fun composeKey(platformKey: Int): Key = when (platformKey) { 0x28 -> Key.K 0x2D -> Key.N 0x2E -> Key.M + // The digits, for the same reason: a shortcut may be a number. + 0x12 -> Key.One + 0x13 -> Key.Two + 0x14 -> Key.Three + 0x15 -> Key.Four + 0x17 -> Key.Five + 0x16 -> Key.Six + 0x1A -> Key.Seven + 0x1C -> Key.Eight + 0x19 -> Key.Nine + 0x1D -> Key.Zero 0x24 -> Key.Enter 0x30 -> Key.Tab 0x31 -> Key.Spacebar diff --git a/renderer/desktop/src/WindowEvents.kt b/renderer/desktop/src/WindowEvents.kt index b9fc74809..8fa49f214 100644 --- a/renderer/desktop/src/WindowEvents.kt +++ b/renderer/desktop/src/WindowEvents.kt @@ -120,6 +120,44 @@ internal fun ComposeScene.receive(event: WindowEvent, win32: Boolean = false) { internal fun win32ComposeKey(virtualKey: Int): Key = when (virtualKey) { + // The letters and digits, which are their own numbers in a virtual key. Without them + // control with C, V, X or Z reaches Compose as a key it does not know. + 0x41 -> Key.A + 0x42 -> Key.B + 0x43 -> Key.C + 0x44 -> Key.D + 0x45 -> Key.E + 0x46 -> Key.F + 0x47 -> Key.G + 0x48 -> Key.H + 0x49 -> Key.I + 0x4A -> Key.J + 0x4B -> Key.K + 0x4C -> Key.L + 0x4D -> Key.M + 0x4E -> Key.N + 0x4F -> Key.O + 0x50 -> Key.P + 0x51 -> Key.Q + 0x52 -> Key.R + 0x53 -> Key.S + 0x54 -> Key.T + 0x55 -> Key.U + 0x56 -> Key.V + 0x57 -> Key.W + 0x58 -> Key.X + 0x59 -> Key.Y + 0x5A -> Key.Z + 0x30 -> Key.Zero + 0x31 -> Key.One + 0x32 -> Key.Two + 0x33 -> Key.Three + 0x34 -> Key.Four + 0x35 -> Key.Five + 0x36 -> Key.Six + 0x37 -> Key.Seven + 0x38 -> Key.Eight + 0x39 -> Key.Nine 0x0D -> Key.Enter 0x09 -> Key.Tab 0x20 -> Key.Spacebar diff --git a/renderer/desktop/test/ShortcutKeysTest.kt b/renderer/desktop/test/ShortcutKeysTest.kt new file mode 100644 index 000000000..d426eba10 --- /dev/null +++ b/renderer/desktop/test/ShortcutKeysTest.kt @@ -0,0 +1,47 @@ +package dev.darkpyonix.composerust.test + +import androidx.compose.ui.input.key.Key +import dev.darkpyonix.composerust.ui.platform.composeKey +import dev.darkpyonix.composerust.ui.platform.win32ComposeKey +import kotlin.test.Test +import kotlin.test.assertEquals + +/** + * Control with C, V, X and Z has to reach Compose as those keys on every desktop. + * + * A key the table does not know used to arrive as key number zero, which is A on the board + * the table is written for, so on the X11 window every one of these selected everything. + * The X11 half is checked by scripts/tests/x11-key-codes.test.sh, which reads the numbers the + * window sends; this is the other half, what those numbers mean. + */ +class ShortcutKeysTest { + + @Test + fun nfr14_the_clipboard_shortcuts_are_their_own_keys_on_the_shared_board() { + assertEquals(Key.C, composeKey(0x08)) + assertEquals(Key.V, composeKey(0x09)) + assertEquals(Key.X, composeKey(0x07)) + assertEquals(Key.Z, composeKey(0x06)) + assertEquals(Key.A, composeKey(0x00)) + } + + @Test + fun nfr14_a_key_with_no_number_is_not_a_letter() { + assertEquals(Key.Unknown, composeKey(-1)) + } + + @Test + fun nfr14_the_clipboard_shortcuts_are_their_own_keys_in_a_windows_virtual_key() { + assertEquals(Key.C, win32ComposeKey('C'.code)) + assertEquals(Key.V, win32ComposeKey('V'.code)) + assertEquals(Key.X, win32ComposeKey('X'.code)) + assertEquals(Key.Z, win32ComposeKey('Z'.code)) + assertEquals(Key.Five, win32ComposeKey('5'.code)) + } + + @Test + fun nfr14_digits_are_digits_on_the_shared_board() { + assertEquals(Key.One, composeKey(0x12)) + assertEquals(Key.Zero, composeKey(0x1D)) + } +} diff --git a/scripts/tests/x11-key-codes.test.sh b/scripts/tests/x11-key-codes.test.sh new file mode 100644 index 000000000..433f0a42d --- /dev/null +++ b/scripts/tests/x11-key-codes.test.sh @@ -0,0 +1,71 @@ +#!/usr/bin/env bash +# The X11 window turns a key symbol into the shared key number, and a key it does not know +# must answer -1. Zero is the A key on the board the numbers come from, so a default of zero +# made control with C, V or X arrive as control with A: copy, paste and cut all selected +# everything. This compiles the function out of x11_window.c against the key symbol values +# from the X11 headers (written down here, so no X11 install is needed) and checks it. +set -uo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +source_file="$repo_root/renderer/desktop/c/x11_window.c" +[[ -f "$source_file" ]] || { echo "missing $source_file"; exit 1; } + +dir="$(mktemp -d)" +trap 'rm -rf "$dir"' EXIT +{ + cat <<'C' +#include +#include +typedef unsigned long KeySym; +#define XK_a 0x61 +#define XK_z 0x7a +#define XK_A 0x41 +#define XK_Z 0x5a +#define XK_0 0x30 +#define XK_9 0x39 +#define XK_Return 0xff0d +#define XK_KP_Enter 0xff8d +#define XK_Tab 0xff09 +#define XK_ISO_Left_Tab 0xfe20 +#define XK_space 0x20 +#define XK_BackSpace 0xff08 +#define XK_Escape 0xff1b +#define XK_Delete 0xffff +#define XK_Left 0xff51 +#define XK_Right 0xff53 +#define XK_Down 0xff54 +#define XK_Up 0xff52 +#define XK_Home 0xff50 +#define XK_End 0xff57 +#define XK_Prior 0xff55 +#define XK_Next 0xff56 +C + sed -n '/^static int32_t dxc_key_code(KeySym key) {/,/^}/p' "$source_file" + cat <<'C' +int main(void) { + printf("c %d\nC %d\nv %d\nx %d\nz %d\na %d\n5 %d\nReturn %d\nunknown %d\nF1 %d\n", + dxc_key_code('c'), dxc_key_code('C'), dxc_key_code('v'), dxc_key_code('x'), + dxc_key_code('z'), dxc_key_code('a'), dxc_key_code('5'), dxc_key_code(0xff0d), + dxc_key_code(0xfd00), dxc_key_code(0xffbe)); + return 0; +} +C +} > "$dir/probe.c" +cc -o "$dir/probe" "$dir/probe.c" || { echo "FAIL: dxc_key_code did not compile on its own"; exit 1; } +got="$("$dir/probe")" +expected="c 8 +C 8 +v 9 +x 7 +z 6 +a 0 +5 23 +Return 36 +unknown -1 +F1 -1" +if [[ "$got" != "$expected" ]]; then + echo "FAIL: the X11 key table answers differently from the shared board" + diff <(echo "$expected") <(echo "$got") + exit 1 +fi +echo "the X11 key table maps shortcuts and answers -1 for unknown keys: ok" From 0b516d19a991983749063bde7a20bcac75d7b6f7 Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 20:14:57 +0900 Subject: [PATCH 05/20] Test: Add a hand-check app for trying a renderer on a real desktop A window with a click counter, two text fields echoed on screen (so shortcuts, paste and undo are visible), the window size class, and a 60 row list with Korean rows. It is its own workspace root so plain cargo run works on every desktop, and it reads the renderer from COMPOSE_RUST_RENDERER_DIR. Prints event: lines. --- .gitignore | 1 + .../tests/fixtures/hand-check/Cargo.toml | 16 ++ .../tests/fixtures/hand-check/README.md | 35 ++++ .../tests/fixtures/hand-check/src/main.rs | 189 ++++++++++++++++++ 4 files changed, 241 insertions(+) create mode 100644 compose-rust/tests/fixtures/hand-check/Cargo.toml create mode 100644 compose-rust/tests/fixtures/hand-check/README.md create mode 100644 compose-rust/tests/fixtures/hand-check/src/main.rs diff --git a/.gitignore b/.gitignore index a3bc153eb..9713178c2 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,7 @@ target # needs at the time, and tracking it would mean a second lock file to update on every # dependency change, plus a dirty working tree every time the test runs. compose-rust/tests/fixtures/consumer/Cargo.lock +compose-rust/tests/fixtures/hand-check/Cargo.lock # The Host's wasm module, built next to the page by # renderer/web/scripts/build-host.sh. The loader beside it is generated and diff --git a/compose-rust/tests/fixtures/hand-check/Cargo.toml b/compose-rust/tests/fixtures/hand-check/Cargo.toml new file mode 100644 index 000000000..1062f8cb3 --- /dev/null +++ b/compose-rust/tests/fixtures/hand-check/Cargo.toml @@ -0,0 +1,16 @@ +# A window to check by hand on a real desktop: text shortcuts, live resize, an input method, +# focus between two fields, a click and a scrolling list with Korean in it. +# +# Like the consumer fixture next to it, this is its own workspace root, so `cargo run` works +# in this directory on Windows, macOS and Linux whatever the enclosing workspace says. It +# names its edition outright for the same reason: an excluded crate cannot inherit one. +[workspace] + +[package] +name = "hand-check" +version = "0.0.0" +edition = "2024" +publish = false + +[dependencies] +compose-rust = { path = "../../.." } diff --git a/compose-rust/tests/fixtures/hand-check/README.md b/compose-rust/tests/fixtures/hand-check/README.md new file mode 100644 index 000000000..648c070b7 --- /dev/null +++ b/compose-rust/tests/fixtures/hand-check/README.md @@ -0,0 +1,35 @@ +# hand-check + +A window to check a renderer by hand on a real desktop. It has a click counter, two text +fields, a line showing the window's size class, and a scrolling list with Korean rows. What +the fields hold is echoed under each, and every change is printed to standard output as an +`event:` line. + +Set `COMPOSE_RUST_RENDERER_DIR` to the renderer you want to try, then run it from this +directory. The commands are the same on every desktop apart from how the variable is set. + +```sh +# macOS and Linux +COMPOSE_RUST_RENDERER_DIR=/path/to/renderer/dist cargo run --release +``` + +```powershell +# Windows (PowerShell). The renderer's bin folder has to be on PATH as well. +$env:COMPOSE_RUST_RENDERER_DIR = "C:\path\to\renderer\dist" +$env:PATH = "C:\path\to\renderer\dist\bin;$env:PATH" +cargo run --release +``` + +On Linux the X11 window is opt-in until it is the default: add `DXC_X11_WINDOW=1`. + +## What to check + +| Check | How | What you should see | +| --- | --- | --- | +| Text shortcuts | In the first field select with ctrl or cmd+A, then C, X, V and Z | The echo line under the field shows the paste, the cut and the undo; `event: first ...` lines print | +| Clipboard across apps | Copy in another app, paste here, and the other way round | The text, Korean included, arrives | +| Live resize | Drag an edge and a corner, at 100% and at 200% scale | Content follows the edge with no lag or flash; the `window:` line shows the size class and size when it changes | +| Input method | Type Korean (`han-geul`) in a field | Underlined text while composing, and the candidate window beside the caret, not at the corner | +| Focus | Click each field, press Tab | Only one field has the caret, and typing goes to it | +| Click | Press the button | The counter counts and `event: click N` prints | +| Scrolling | Scroll the list with the wheel and the scrollbar | 60 rows move smoothly | diff --git a/compose-rust/tests/fixtures/hand-check/src/main.rs b/compose-rust/tests/fixtures/hand-check/src/main.rs new file mode 100644 index 000000000..c2c908ba2 --- /dev/null +++ b/compose-rust/tests/fixtures/hand-check/src/main.rs @@ -0,0 +1,189 @@ +//! A window for checking a renderer by hand. Nothing in it is a test: a person uses it and +//! reads what it says, on screen and as `event:` lines on standard output. +//! +//! What is in the window, top to bottom: +//! +//! - a label counting clicks, and the button that counts them; +//! - two text fields, to see which has the keyboard, with what each holds echoed below it, so +//! that what ctrl or cmd with C, V, X, Z and A did is on screen: a paste shows up as the new +//! text, an undo as the text going back, a cut as the text shrinking; +//! - the window's size class and size, as the renderer last reported them (it reports when +//! the class changes, not on every pixel of a resize); +//! - a scrolling list of 60 rows with Korean in them. +//! +//! For an input method, type Korean in either field and watch where the candidate window +//! opens: it should be beside the caret. +//! +//! The renderer comes from `COMPOSE_RUST_RENDERER_DIR`, as it does for any application. + +use compose_rust::protocol::{HostEvent, Mutation, PropertyValue, ProtocolError}; +use compose_rust::schema::{EventPayload, PropertyKind, WidgetKind, Window}; +use compose_rust::{Batch, LaunchBuilder, Runtime}; + +const COLUMN: u32 = 1; +const CLICKS: u32 = 2; +const BUTTON: u32 = 3; +const FIRST: u32 = 4; +const FIRST_ECHO: u32 = 5; +const SECOND: u32 = 6; +const SECOND_ECHO: u32 = 7; +const SIZE: u32 = 8; +const LIST: u32 = 9; +const ROW0: u32 = 100; + +const ON_CLICK: i64 = 1; +const ON_FIRST: i64 = 2; +const ON_SECOND: i64 = 3; + +struct App { + batch: Batch, + clicks: u32, + first: String, + second: String, + size: String, + dirty: bool, +} + +impl App { + fn text(&mut self, node_id: u32, text: &str) { + self.batch.write(Mutation::SetProp { + node_id, + property: PropertyKind::Text, + value: PropertyValue::String(text), + }); + } + + fn handler(&mut self, node_id: u32, property: PropertyKind, id: i64) { + self.batch.write(Mutation::SetProp { + node_id, + property, + value: PropertyValue::Integer(id), + }); + } +} + +impl Runtime for App { + fn batch(&self) -> &Batch { + &self.batch + } + + fn batch_mut(&mut self) -> &mut Batch { + &mut self.batch + } + + fn rebuild(&mut self) { + let mut window = Window::new(); + window.title = "compose-rust hand check"; + window.width = 480; + window.height = 700; + window.min_width = 320; + window.min_height = 320; + window.resizable = true; + self.batch.write(Mutation::SetWindow(window)); + for (node_id, widget) in [ + (COLUMN, WidgetKind::Column), + (CLICKS, WidgetKind::Text), + (BUTTON, WidgetKind::Button), + (FIRST, WidgetKind::TextField), + (FIRST_ECHO, WidgetKind::Text), + (SECOND, WidgetKind::TextField), + (SECOND_ECHO, WidgetKind::Text), + (SIZE, WidgetKind::Text), + (LIST, WidgetKind::ScrollColumn), + ] { + self.batch.write(Mutation::Create { node_id, widget }); + } + self.text(CLICKS, "clicks: 0"); + self.text(BUTTON, "Click me"); + self.text(FIRST, "first field: select, copy, paste, undo here"); + self.text(FIRST_ECHO, "first holds: first field: select, copy, paste, undo here"); + self.text(SECOND, "second field"); + self.text(SECOND_ECHO, "second holds: second field"); + self.text(SIZE, "window: not reported yet"); + self.handler(BUTTON, PropertyKind::OnClick, ON_CLICK); + self.handler(FIRST, PropertyKind::OnValueChange, ON_FIRST); + self.handler(SECOND, PropertyKind::OnValueChange, ON_SECOND); + for row in 0..60u32 { + self.batch.write(Mutation::Create { + node_id: ROW0 + row, + widget: WidgetKind::Text, + }); + self.text(ROW0 + row, &format!("List row {row} \u{d55c}\u{ae00} \u{c785}\u{b825}")); + self.batch.write(Mutation::Insert { + parent_id: LIST, + node_id: ROW0 + row, + index: row, + }); + } + for (index, node_id) in [CLICKS, BUTTON, FIRST, FIRST_ECHO, SECOND, SECOND_ECHO, SIZE, LIST] + .into_iter() + .enumerate() + { + self.batch.write(Mutation::Insert { + parent_id: COLUMN, + node_id, + index: index as u32, + }); + } + } + + fn render(&mut self) { + if !self.dirty { + return; + } + self.dirty = false; + let clicks = format!("clicks: {}", self.clicks); + let first = format!("first holds: {}", self.first); + let second = format!("second holds: {}", self.second); + let size = format!("window: {}", self.size); + self.text(CLICKS, &clicks); + self.text(FIRST_ECHO, &first); + self.text(SECOND_ECHO, &second); + self.text(SIZE, &size); + } + + fn handle_event(&mut self, event: &HostEvent<'_>) -> Result { + match (event.handler_id as i64, &event.payload) { + (ON_CLICK, EventPayload::Clicked) => { + self.clicks += 1; + println!("event: click {}", self.clicks); + } + (ON_FIRST, EventPayload::TextChanged(text)) => { + self.first = (*text).to_owned(); + println!("event: first {text}"); + } + (ON_SECOND, EventPayload::TextChanged(text)) => { + self.second = (*text).to_owned(); + println!("event: second {text}"); + } + ( + _, + EventPayload::WindowSizeChanged { + width_dp, + height_dp, + class, + .. + }, + ) => { + self.size = format!("{width_dp:.0} x {height_dp:.0} dp, {class:?}"); + println!("event: window {width_dp:.0}x{height_dp:.0} {class:?}"); + } + _ => return Err(ProtocolError::InvalidValueKind(0)), + } + self.dirty = true; + Ok(0) + } +} + +fn main() { + LaunchBuilder::new().launch_runtime(|| { + Box::new(App { + batch: Batch::new(), + clicks: 0, + first: "first field: select, copy, paste, undo here".to_owned(), + second: "second field".to_owned(), + size: "not reported yet".to_owned(), + dirty: false, + }) as Box + }); +} From ba6f601b8f20a6a6c01c3c9e320820c25c7e443d Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 20:15:55 +0900 Subject: [PATCH 06/20] Test: A virtual key for a letter is that letter now (NFR-14) --- renderer/desktop/test/Win32ImeKeyTest.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/renderer/desktop/test/Win32ImeKeyTest.kt b/renderer/desktop/test/Win32ImeKeyTest.kt index f8256f3fa..87f50c0d1 100644 --- a/renderer/desktop/test/Win32ImeKeyTest.kt +++ b/renderer/desktop/test/Win32ImeKeyTest.kt @@ -14,6 +14,8 @@ class Win32ImeKeyTest { assertEquals(Key.DirectionLeft, win32ComposeKey(0x25)) assertEquals(Key.DirectionRight, win32ComposeKey(0x27)) assertEquals(Key.Delete, win32ComposeKey(0x2E)) - assertEquals(Key.Unknown, win32ComposeKey(0x41)) + // The letters are their own virtual keys, so control with C reaches Compose as C. + assertEquals(Key.A, win32ComposeKey(0x41)) + assertEquals(Key.Unknown, win32ComposeKey(0x07)) } } From e9e95f91032a9843e4f7a240f53414b5b2d8b91a Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 21:30:22 +0900 Subject: [PATCH 07/20] Fix: Set DXGI_SCALING_NONE on the Win32 swapchain so a skipped resize frame is not stretched (NFR-14) The scaling field was left at its zero value from memset, which is DXGI_SCALING_STRETCH. An ordinary resize never shows it, because the buffer is refitted and redrawn at the new size before it is presented, but a frame the resize handler skips (a refit refused, or dragging starting before the swapchain exists) leaves DXGI filling the new client area by stretching the old buffer instead of showing it at its own size. --- renderer/desktop/c/win32_window.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/renderer/desktop/c/win32_window.c b/renderer/desktop/c/win32_window.c index f466d6054..97aaaf172 100644 --- a/renderer/desktop/c/win32_window.c +++ b/renderer/desktop/c/win32_window.c @@ -2157,6 +2157,12 @@ int32_t dxc_native_window_open( swapchain_description.SampleDesc.Count = 1; swapchain_description.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; swapchain_description.AlphaMode = DXGI_ALPHA_MODE_IGNORE; + // Left at its zero value this is DXGI_SCALING_STRETCH, which fills the client area + // from whatever the back buffer holds. A frame drawn at the size just given always + // matches, so this never shows on an ordinary resize, but a frame skipped by + // dxc_native_frame_begin (a refit refused, or the swapchain not yet made) would be + // the previous buffer stretched to the new area instead of left at its own size. + swapchain_description.Scaling = DXGI_SCALING_NONE; IDXGISwapChain1 *first = NULL; HRESULT made = IDXGIFactory4_CreateSwapChainForHwnd( factory, (IUnknown *)queue, window, &swapchain_description, NULL, NULL, &first); From ace5c84274247ef7cd94681ec3e10eb81521c058 Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 23:21:25 +0900 Subject: [PATCH 08/20] Docs: Table of the window logic the GraalVM and Kotlin/Native paths each implement --- docs/design/shared-window-logic.md | 55 ++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 docs/design/shared-window-logic.md diff --git a/docs/design/shared-window-logic.md b/docs/design/shared-window-logic.md new file mode 100644 index 000000000..7290e15c2 --- /dev/null +++ b/docs/design/shared-window-logic.md @@ -0,0 +1,55 @@ +# 데스크톱 창의 공통 로직을 한 벌로 모으기 + +상태: 진행 중. 소유자 결정(2026-10-04): 데스크톱 창 코드는 구현 둘 + 공통 로직 + 일치 테스트입니다. 경로마다 OS를 부르는 얇은 층만 따로 두고, 나머지는 Kotlin 소스 한 벌(D3)입니다. + +두 경로는 이렇게 부릅니다. + +- GraalVM 경로: `renderer/desktop/c/appkit_window.m`, `win32_window.c`, `x11_window.c`와 Kotlin 래퍼(`AppKitWindow.kt`, `Win32Window.kt`, `X11Window.kt`). +- K/N 경로: `renderer/macos`, `renderer/linux`의 Kotlin cinterop 창(`MacosWindow.kt`, `LinuxWindow.kt`). Windows는 `feature/windows-kotlin-native`에 있습니다. + +## 공유 방식 + +`renderer/desktop/src/`의 파일을 `renderer/linux/src/shared/`와 `renderer/macos/src/shared/`가 심볼릭 링크로 가리키는 기존 방식을 따릅니다. 이유는 셋입니다. + +1. 이미 60개가 넘는 파일이 이 방식으로 공유되어 있어, 새 방식을 들이면 두 가지 규칙이 생깁니다. +2. desktop은 JVM 모듈이고 linux, macos는 각자 Kotlin/Native 라이브러리 모듈입니다. 공유 모듈을 따로 만들면 세 모듈의 의존과 컴파일러 옵션(opt-in, 컴파일 대상)을 새로 맞춰야 하고, 빌드를 로컬에서 돌릴 수 없는 이번 작업에서는 확인할 수 없습니다. 모듈 분리는 이 방식 위에서 나중에 할 수 있고, 지금은 링크가 이동 비용이 가장 작습니다. +3. 공유 파일은 JVM 이름(`System.getenv`, `System.nanoTime`)을 쓰는데, K/N 쪽은 `JvmCompatShims.kt`가 `java.lang`에 같은 이름을 채웁니다. 공유 파일은 그 이름만 쓰고 플랫폼 타입을 이름에 올리지 않습니다. + +## 규칙: 콜백 안에서 동기로 도는 결정은 C 헤더에 둔다 + +창이 OS 콜백 안에서 곧바로 내려야 하는 결정(크기 조절 중 그릴지 말지, 드로어블 크기, 낡은 프레임 판정)은 헤더만 있는 순수 C(`renderer/desktop/c/win32_resize.h`, `appkit_resize.h`)로 둡니다. GraalVM은 C에서 곧바로 부르고, K/N 창은 같은 헤더를 cinterop으로 부릅니다. 한 구현이고, 모달 크기 조절 루프 안에서 upcall이 없습니다. 공유 Kotlin으로 옮기는 것은 OS 콜백 밖에서 쓰는 로직(키 표, IME 상태 모델, 접근성 트리, HostTextField 규칙)만입니다. 같은 입력을 두 경로의 C 헤더에 넣는 테스트를 둡니다. + +## 중복 표 + +"공유 가능"은 Kotlin 한 벌로 합칠 수 있는가입니다. 줄 번호는 이 브랜치 기준입니다. + +| 항목 | GraalVM 경로 | K/N 경로 | 공유 가능 | 처리 | +|---|---|---|---|---| +| 키 번호 표 (X11) | `x11_window.c:214` `dxc_key_code` (C 표, 글자와 숫자 포함) | `linux/src/LinuxInput.kt:79` `platformKey` (글자와 숫자 없음) | 예 | `X11Keys.kt` 한 벌. 두 경로가 키심을 그대로 넘기고 Kotlin이 번호로 바꿉니다. K/N 쪽에 글자가 빠져 ctrl+c/v/x/z/a가 Unknown으로 가던 틈이 닫힙니다 | +| 키 번호 표 (Win32) | `WindowEvents.kt:122` `win32ComposeKey` (Kotlin, 공유 파일) | `feature/windows-kotlin-native` | 이미 공유 | 변경 없음 | +| 키 번호 표 (AppKit) | `AppKitKeys.kt:26` `composeKey`, 이 번호가 공통 번호의 기준 | `MacosWindow.kt:775`가 같은 `composeKey`를 부름 | 이미 공유 | 변경 없음 | +| 수정키 비트 | `x11_window.c:200` `dxc_modifiers` (C), `win32_window.c:1083` `dxc_held_modifiers` | `LinuxInput.kt:62` `modifiersOf` (Kotlin) | 예 (X11) | `X11Keys.kt`의 `x11Modifiers`. Win32는 값이 달라 `WindowEvents.kt`의 win32 분기가 이미 해석합니다 | +| 버튼 비트 | `x11_window.c:192` `dxc_buttons` | `LinuxInput.kt:44` `buttonsOf` | 예, 가치 낮음 | 표에 남기고 이번에는 옮기지 않았습니다 | +| IME 조합 상태 (X11) | `x11_window.c:317-375` 조합 버퍼와 콜백 (C, `wchar_t` 배열) | `InputMethod.kt:33` `PreeditBuffer`, `:75` `ImeSession` (Kotlin) | 예 | `ImeComposition.kt`로 옮기고 GraalVM C는 조합 변경(위치, 길이, 새 글자)만 넘깁니다 | +| 키 하나가 이벤트가 되는 규칙 | `x11_window.c:888-906` (키 먼저, 글자는 뒤에 commit, 단축키는 글자 없음) | `InputMethod.kt:144` `keyEventsFor` | 예 | 같은 파일로 옮기고 C는 키심, 상태, 글자만 넘깁니다 | +| IME 조합 상태 (Win32) | `win32_window.c:1140-1180` 전체 문자열을 읽어 compose/commit 이벤트를 만듭니다 | `feature/windows-kotlin-native` | 부분 | 조합 문자열이 통째로 오는 모델이라 조합 버퍼는 필요 없습니다. 이벤트 규칙은 `TextInputSession.kt`(공유)를 같이 씁니다 | +| IME 조합 상태 (AppKit) | `AppKitTextInput.kt` | `MacosWindow.kt:414,419` `insertText`, `setMarkedText` | 이미 공유 | `TextInputSession.kt`의 `compose`와 `commit`을 둘 다 씁니다 | +| 접근성 트리 만들기 | `AppKitAccessibility.kt:52` `describe()` (공유, 평평한 목록) | `AtspiSemantics.kt:59` `capture` (SemanticsNode를 AT-SPI 트리로) | 부분 | 트리 만들기는 #140이 `AtspiModel`, `AtspiSemantics`를 `desktop/src`로 옮기는 중이라 거기서 한 벌이 됩니다. 이 작업은 건드리지 않습니다 | +| 크기 조절과 프레임 결정 | `appkit_resize.h`, `win32_resize.h` (헤더 C) | `LinuxWindow.kt:356` `WindowFrames`, `ResizeSync.kt` (Kotlin, 공유) | C 헤더로 | 위 규칙대로 C 헤더가 한 벌입니다. K/N 창에 같은 헤더를 cinterop으로 붙여 같은 통계(`steps`, `stale`, `stretched`)를 냅니다 | +| 드로어블 크기, 낡은 프레임 | `appkit_window.m:1130` `dxc_resize_present`, `win32_window.c:2203` | `LinuxWindow.kt:356-362` | C 헤더로 | 위와 같습니다 | +| HostTextField 보내기 규칙 | `renderer/HostTextField.kt` | 같은 파일의 심볼릭 링크 | 이미 공유 | 변경 없음 | +| 배율과 DPI | `X11Window.kt:43`, `Win32Window.kt:92`, `AppKitWindow.kt:176` (C가 `scale`을 잼) | `LinuxWindow.kt:971` 상수 1.0, `MacosWindow.kt:652` `backingScaleFactor` | 아니오 | OS를 묻는 일이라 경로별입니다. 결과를 `WindowMeasurement`로 넘기는 모양은 이미 같습니다 | +| 클립보드 | `x11_window.c:458,497`, `appkit_window.m:228,249`, `win32_window.c:1690` | `MacosClipboard.kt`(#147에서 추가), Linux는 없음 | 부분 | 장면 쪽 `NativeClipboard.kt:28`은 이미 공유입니다. OS 호출은 경로별입니다. K/N Linux에는 클립보드 구현이 없다는 점이 일치 점검표에서 드러납니다 | +| 창 크롬 수치 | `WindowChrome.kt:143,210` (캡션 높이, 버튼 폭) | `MacosWindow.kt:300` `measureCaption`, #147의 `MacosWindowChrome.kt` | 부분 | 최소 크기 규칙(`contentMinimum`)은 #147이 macOS 쪽에 두었습니다. 같은 규칙이 `appkit_window.m:909`에도 있어 공유 후보입니다. #147이 끝난 뒤 맞춥니다 | +| 지연 추적과 합성 입력 | `LatencyTrace.kt`, `SyntheticInput.kt` (AppKit 창에서만 씀) | 없음 | 예 | 두 파일을 K/N 창과 GraalVM X11 창이 씁니다 (일치 점검의 3절) | + +## 일치 점검표 + +두 경로에서 같은 점검과 같은 측정을 돌립니다. 각 줄은 `scripts/parity/` 한 스크립트가 같은 표 모양(`parity <항목> <경로> <값> <단위>`)으로 출력합니다. + +- 입력 지연: `DXC_SYNTH=type`과 `DXC_REPORT_LATENCY` +- 크기 조절 프레임 시간: `DXC_SYNTH=resize`와 `DXC_REPORT_RESIZE` +- 단축키(ctrl 또는 cmd 더하기 c, v, x, z, a): 키 번호 표를 같은 벡터로 검사 +- 클립보드, 다크 모드, 최소 크기, 아이콘 + +어느 점검이 어느 CI 작업에서 도는지와 첫 수치는 PR에 적습니다. From 012208610d145482b9457f5daf8b9413bd120cca Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 23:22:46 +0900 Subject: [PATCH 09/20] Feat: One X11 key and modifier table for both X11 windows The GraalVM X11 window mapped key symbols in C and the Kotlin/Native Linux window mapped them in Kotlin, and the two disagreed: the Kotlin/Native table had no letters or digits, so control with C, V, X, Z or A reached the scene as an unknown key. The table is now X11Keys.kt, shared by symlink, and the C window hands the key symbol and state word over untouched, as the Win32 window already does with virtual keys. Covers the shortcut keys (FR-33.6 and the editing shortcuts) on Kotlin/Native Linux. --- renderer/desktop/c/x11_window.c | 58 ++---------- renderer/desktop/src/X11Keys.kt | 102 ++++++++++++++++++++++ renderer/desktop/src/X11Window.kt | 2 +- renderer/desktop/test/ShortcutKeysTest.kt | 4 +- renderer/desktop/test/X11KeysTest.kt | 93 ++++++++++++++++++++ renderer/linux/src/LinuxInput.kt | 64 +------------- renderer/linux/src/shared/X11Keys.kt | 1 + renderer/linux/test/LinuxInputTest.kt | 19 ++-- scripts/tests/x11-key-codes.test.sh | 92 ++++++------------- 9 files changed, 249 insertions(+), 186 deletions(-) create mode 100644 renderer/desktop/src/X11Keys.kt create mode 100644 renderer/desktop/test/X11KeysTest.kt create mode 120000 renderer/linux/src/shared/X11Keys.kt diff --git a/renderer/desktop/c/x11_window.c b/renderer/desktop/c/x11_window.c index 4cab1b9cf..8281c36e9 100644 --- a/renderer/desktop/c/x11_window.c +++ b/renderer/desktop/c/x11_window.c @@ -195,51 +195,9 @@ static int32_t dxc_buttons(unsigned int state) { ((state & Button2Mask) ? 4 : 0); } -// The shared Compose reader uses these four bits, as NSEvent does. Translate at the -// boundary so the same reader receives the same meaning on every desktop. -static int32_t dxc_modifiers(unsigned int state) { - return ((state & ShiftMask) ? (1 << 17) : 0) | - ((state & ControlMask) ? (1 << 18) : 0) | - ((state & Mod1Mask) ? (1 << 19) : 0) | - ((state & Mod4Mask) ? (1 << 20) : 0); -} - -// The key numbers are the shared reader's, which are the macOS ones: the letters and digits -// by where they sit on a board, and the special keys by name. A key with no number answers -// -1, never 0, because 0 is the A key and a shortcut that does not map would otherwise arrive -// as ctrl and A. -// -// Printable keys also carry their code point, obtained from XLookupString for the current -// keyboard layout. -static int32_t dxc_key_code(KeySym key) { - static const int32_t letters[26] = { - 0x00, 0x0B, 0x08, 0x02, 0x0E, 0x03, 0x05, 0x04, 0x22, 0x26, 0x28, 0x25, 0x2E, - 0x2D, 0x1F, 0x23, 0x0C, 0x0F, 0x01, 0x11, 0x20, 0x09, 0x0D, 0x07, 0x10, 0x06, - }; - static const int32_t digits[10] = { - 0x1D, 0x12, 0x13, 0x14, 0x15, 0x17, 0x16, 0x1A, 0x1C, 0x19, - }; - if (key >= XK_a && key <= XK_z) return letters[key - XK_a]; - if (key >= XK_A && key <= XK_Z) return letters[key - XK_A]; - if (key >= XK_0 && key <= XK_9) return digits[key - XK_0]; - switch (key) { - case XK_Return: case XK_KP_Enter: return 0x24; - case XK_Tab: case XK_ISO_Left_Tab: return 0x30; - case XK_space: return 0x31; - case XK_BackSpace: return 0x33; - case XK_Escape: return 0x35; - case XK_Delete: return 0x75; - case XK_Left: return 0x7B; - case XK_Right: return 0x7C; - case XK_Down: return 0x7D; - case XK_Up: return 0x7E; - case XK_Home: return 0x73; - case XK_End: return 0x77; - case XK_Prior: return 0x74; - case XK_Next: return 0x79; - default: return -1; - } -} +// Keys and modifiers cross as the server wrote them: a keysym and a state word. Turning +// them into what the shared reader understands is Kotlin's, in one table both X11 windows +// ask (`X11Keys.kt`). /** * Tells the window manager that the drawing it was waiting for is done. @@ -831,7 +789,7 @@ static void dxc_pump_events(void) { record.x = (float)event.xmotion.x; record.y = (float)event.xmotion.y; record.buttons = dxc_buttons(event.xmotion.state); - record.modifiers = dxc_modifiers(event.xmotion.state); + record.modifiers = (int32_t)event.xmotion.state; break; case ButtonPress: case ButtonRelease: { @@ -861,7 +819,7 @@ static void dxc_pump_events(void) { if (event.type == ButtonPress) record.buttons |= bit; else record.buttons &= ~bit; } - record.modifiers = dxc_modifiers(event.xbutton.state); + record.modifiers = (int32_t)event.xbutton.state; break; } case KeyPress: @@ -886,8 +844,10 @@ static void dxc_pump_events(void) { } } record.kind = event.type == KeyPress ? DXC_EVENT_KEY_DOWN : DXC_EVENT_KEY_UP; - record.key_code = dxc_key_code(symbol); - record.modifiers = dxc_modifiers(event.xkey.state); + // The keysym and the state word as the server gave them. `X11Keys.kt` turns them into + // the shared numbering, the same function the Kotlin/Native window asks. + record.key_code = (int32_t)symbol; + record.modifiers = (int32_t)event.xkey.state; if (count == 1 && (unsigned char)bytes[0] >= 32 && (unsigned char)bytes[0] < 127) { record.code_point = (unsigned char)bytes[0]; } diff --git a/renderer/desktop/src/X11Keys.kt b/renderer/desktop/src/X11Keys.kt new file mode 100644 index 000000000..a81fbbcad --- /dev/null +++ b/renderer/desktop/src/X11Keys.kt @@ -0,0 +1,102 @@ +package dev.darkpyonix.composerust.ui.platform + +// What an X11 server calls a key and a modifier, turned into the numbering the shared reader +// (`composeKey`) understands. The numbering is AppKit's: letters and digits by where they sit +// on a board, special keys by name. +// +// One table for both X11 windows. The window that links the native image hands the keysym +// over untouched, and the Kotlin/Native window reads it from Xlib; both then ask here, so a +// key can no longer mean one thing on one path and another on the other. Plain integers +// rather than Xlib's constants, because this file is compiled for the native image and for +// Kotlin/Native and only the second has the X11 headers. The values are the ones in +// `keysymdef.h` and `X.h`, and the test names the ones that matter. + +/** A key with no meaning of its own. Zero is the A key in this numbering, so it cannot be zero. */ +internal const val NO_KEY = -1 + +/** The key number for an X11 keysym, or [NO_KEY] where the key only types a character. */ +internal fun x11KeyNumber(keysym: Long): Int { + if (keysym in XK_LOWER_A..XK_LOWER_Z) return X11_LETTERS[(keysym - XK_LOWER_A).toInt()] + if (keysym in XK_UPPER_A..XK_UPPER_Z) return X11_LETTERS[(keysym - XK_UPPER_A).toInt()] + if (keysym in XK_ZERO..XK_NINE) return X11_DIGITS[(keysym - XK_ZERO).toInt()] + return when (keysym) { + XK_RETURN, XK_KP_ENTER -> 0x24 + XK_TAB, XK_ISO_LEFT_TAB -> 0x30 + XK_SPACE -> 0x31 + XK_BACKSPACE -> 0x33 + XK_ESCAPE -> 0x35 + XK_DELETE -> 0x75 + XK_LEFT -> 0x7B + XK_RIGHT -> 0x7C + XK_DOWN -> 0x7D + XK_UP -> 0x7E + XK_HOME -> 0x73 + XK_END -> 0x77 + XK_PRIOR -> 0x74 + XK_NEXT -> 0x79 + else -> NO_KEY + } +} + +/** + * A record from the X11 window, with the keysym and state word it carries turned into the + * shared numbering. Every other record passes through unchanged, apart from its modifiers. + */ +internal fun WindowEvent.fromX11(): WindowEvent { + val numbered = if (kind == WindowEvent.KEY_DOWN || kind == WindowEvent.KEY_UP) { + x11KeyNumber(keyCode.toLong() and 0xFFFFFFFFL) + } else { + keyCode + } + return copy(keyCode = numbered, modifiers = x11Modifiers(modifiers)) +} + +/** + * The four modifier bits the shared reader uses, which are the ones an NSEvent carries, from + * the state word an X11 event carries. Super is the key a Linux desktop puts where macOS puts + * Command, so it lands in Command's bit. + */ +internal fun x11Modifiers(state: Int): Int = + (if (state and X11_SHIFT_MASK != 0) 1 shl 17 else 0) or + (if (state and X11_CONTROL_MASK != 0) 1 shl 18 else 0) or + (if (state and X11_MOD1_MASK != 0) 1 shl 19 else 0) or + (if (state and X11_MOD4_MASK != 0) 1 shl 20 else 0) + +/** Whether the state word says a command is being typed rather than text: control, alt or super. */ +internal fun x11IsShortcut(state: Int): Boolean = + state and (X11_CONTROL_MASK or X11_MOD1_MASK or X11_MOD4_MASK) != 0 + +internal const val X11_SHIFT_MASK = 1 shl 0 +internal const val X11_CONTROL_MASK = 1 shl 2 +internal const val X11_MOD1_MASK = 1 shl 3 +internal const val X11_MOD4_MASK = 1 shl 6 + +private const val XK_LOWER_A = 0x61L +private const val XK_LOWER_Z = 0x7AL +private const val XK_UPPER_A = 0x41L +private const val XK_UPPER_Z = 0x5AL +private const val XK_ZERO = 0x30L +private const val XK_NINE = 0x39L +private const val XK_SPACE = 0x20L +private const val XK_BACKSPACE = 0xFF08L +private const val XK_TAB = 0xFF09L +private const val XK_RETURN = 0xFF0DL +private const val XK_ESCAPE = 0xFF1BL +private const val XK_HOME = 0xFF50L +private const val XK_LEFT = 0xFF51L +private const val XK_UP = 0xFF52L +private const val XK_RIGHT = 0xFF53L +private const val XK_DOWN = 0xFF54L +private const val XK_PRIOR = 0xFF55L +private const val XK_NEXT = 0xFF56L +private const val XK_END = 0xFF57L +private const val XK_KP_ENTER = 0xFF8DL +private const val XK_DELETE = 0xFFFFL +private const val XK_ISO_LEFT_TAB = 0xFE20L + +// a to z, then 0 to 9, each as the board numbers them. +private val X11_LETTERS = intArrayOf( + 0x00, 0x0B, 0x08, 0x02, 0x0E, 0x03, 0x05, 0x04, 0x22, 0x26, 0x28, 0x25, 0x2E, + 0x2D, 0x1F, 0x23, 0x0C, 0x0F, 0x01, 0x11, 0x20, 0x09, 0x0D, 0x07, 0x10, 0x06, +) +private val X11_DIGITS = intArrayOf(0x1D, 0x12, 0x13, 0x14, 0x15, 0x17, 0x16, 0x1A, 0x1C, 0x19) diff --git a/renderer/desktop/src/X11Window.kt b/renderer/desktop/src/X11Window.kt index b055b53ad..2ea19d8e0 100644 --- a/renderer/desktop/src/X11Window.kt +++ b/renderer/desktop/src/X11Window.kt @@ -214,7 +214,7 @@ internal fun runX11Window(autoExitMillis: Long? = null) { work.runPending() var heard = false var drew = false - for (event in drainWindowEvents()) { + for (event in drainWindowEvents().map { it.fromX11() }) { if (report && event.kind != WindowEvent.POINTER_MOVE) { System.err.println("compose-rust: window heard $event") } diff --git a/renderer/desktop/test/ShortcutKeysTest.kt b/renderer/desktop/test/ShortcutKeysTest.kt index d426eba10..de06a24b5 100644 --- a/renderer/desktop/test/ShortcutKeysTest.kt +++ b/renderer/desktop/test/ShortcutKeysTest.kt @@ -11,8 +11,8 @@ import kotlin.test.assertEquals * * A key the table does not know used to arrive as key number zero, which is A on the board * the table is written for, so on the X11 window every one of these selected everything. - * The X11 half is checked by scripts/tests/x11-key-codes.test.sh, which reads the numbers the - * window sends; this is the other half, what those numbers mean. + * The X11 half is `X11KeysTest`, which checks the table both X11 windows read; this is the + * other half, what those numbers mean. */ class ShortcutKeysTest { diff --git a/renderer/desktop/test/X11KeysTest.kt b/renderer/desktop/test/X11KeysTest.kt new file mode 100644 index 000000000..4597806e0 --- /dev/null +++ b/renderer/desktop/test/X11KeysTest.kt @@ -0,0 +1,93 @@ +package dev.darkpyonix.composerust.test + +import androidx.compose.ui.input.key.Key +import dev.darkpyonix.composerust.ui.platform.NO_KEY +import dev.darkpyonix.composerust.ui.platform.X11_CONTROL_MASK +import dev.darkpyonix.composerust.ui.platform.X11_MOD1_MASK +import dev.darkpyonix.composerust.ui.platform.X11_MOD4_MASK +import dev.darkpyonix.composerust.ui.platform.X11_SHIFT_MASK +import dev.darkpyonix.composerust.ui.platform.composeKey +import dev.darkpyonix.composerust.ui.platform.x11IsShortcut +import dev.darkpyonix.composerust.ui.platform.x11KeyNumber +import dev.darkpyonix.composerust.ui.platform.x11Modifiers +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals + +/** + * The X11 key table both X11 windows read. A shortcut is matched by which key it is, so each + * letter a shortcut uses has to arrive as that letter on every path. + */ +class X11KeysTest { + private fun key(keysym: Long) = composeKey(x11KeyNumber(keysym)) + + @Test + fun the_editing_shortcut_letters_arrive_as_themselves() { + assertEquals(Key.C, key('c'.code.toLong())) + assertEquals(Key.C, key('C'.code.toLong())) + assertEquals(Key.V, key('v'.code.toLong())) + assertEquals(Key.X, key('x'.code.toLong())) + assertEquals(Key.Z, key('z'.code.toLong())) + assertEquals(Key.A, key('a'.code.toLong())) + } + + @Test + fun every_letter_and_digit_has_its_own_key() { + val letters = ('a'..'z').map { x11KeyNumber(it.code.toLong()) } + assertEquals(26, letters.toSet().size) + assertEquals(10, ('0'..'9').map { x11KeyNumber(it.code.toLong()) }.toSet().size) + assertEquals(Key.Five, key('5'.code.toLong())) + assertEquals(Key.Zero, key('0'.code.toLong())) + } + + @Test + fun named_keys_and_their_second_keysyms() { + assertEquals(Key.Enter, key(0xFF0DL)) + assertEquals(Key.Enter, key(0xFF8DL)) + assertEquals(Key.Tab, key(0xFF09L)) + assertEquals(Key.Tab, key(0xFE20L)) + assertEquals(Key.Spacebar, key(0x20L)) + assertEquals(Key.Backspace, key(0xFF08L)) + assertEquals(Key.Escape, key(0xFF1BL)) + assertEquals(Key.Delete, key(0xFFFFL)) + assertEquals(Key.DirectionLeft, key(0xFF51L)) + assertEquals(Key.DirectionUp, key(0xFF52L)) + assertEquals(Key.DirectionRight, key(0xFF53L)) + assertEquals(Key.DirectionDown, key(0xFF54L)) + assertEquals(Key.MoveHome, key(0xFF50L)) + assertEquals(Key.MoveEnd, key(0xFF57L)) + assertEquals(Key.PageUp, key(0xFF55L)) + assertEquals(Key.PageDown, key(0xFF56L)) + } + + @Test + fun a_key_with_no_meaning_is_minus_one_never_zero() { + // Zero is the A key, so an unknown key answering zero arrived as control with A. + assertEquals(NO_KEY, x11KeyNumber(0xFD00L)) + assertEquals(NO_KEY, x11KeyNumber(0xFFBEL)) + assertNotEquals(0, x11KeyNumber(0xFFBEL)) + assertEquals(Key.Unknown, key(0xFFBEL)) + } + + @Test + fun each_modifier_lands_in_its_own_bit() { + val shift = 1 shl 17 + val control = 1 shl 18 + val option = 1 shl 19 + val command = 1 shl 20 + assertEquals(shift, x11Modifiers(X11_SHIFT_MASK)) + assertEquals(control, x11Modifiers(X11_CONTROL_MASK)) + assertEquals(option, x11Modifiers(X11_MOD1_MASK)) + assertEquals(command, x11Modifiers(X11_MOD4_MASK)) + assertEquals(control or shift, x11Modifiers(X11_CONTROL_MASK or X11_SHIFT_MASK)) + assertEquals(0, x11Modifiers(0)) + } + + @Test + fun control_alt_and_super_make_a_shortcut_and_shift_does_not() { + assertEquals(true, x11IsShortcut(X11_CONTROL_MASK)) + assertEquals(true, x11IsShortcut(X11_MOD1_MASK)) + assertEquals(true, x11IsShortcut(X11_MOD4_MASK)) + assertEquals(false, x11IsShortcut(X11_SHIFT_MASK)) + } +} diff --git a/renderer/linux/src/LinuxInput.kt b/renderer/linux/src/LinuxInput.kt index f95bd3fc3..b78b50679 100644 --- a/renderer/linux/src/LinuxInput.kt +++ b/renderer/linux/src/LinuxInput.kt @@ -5,33 +5,13 @@ package dev.darkpyonix.composerust.ui.platform import x11.Button1Mask import x11.Button2Mask import x11.Button3Mask -import x11.ControlMask import x11.KeySym -import x11.Mod1Mask -import x11.Mod4Mask -import x11.ShiftMask import x11.XC_crosshair import x11.XC_hand2 import x11.XC_left_ptr import x11.XC_sb_h_double_arrow import x11.XC_sb_v_double_arrow import x11.XC_xterm -import x11.XK_BackSpace -import x11.XK_Delete -import x11.XK_Down -import x11.XK_End -import x11.XK_Escape -import x11.XK_Home -import x11.XK_ISO_Left_Tab -import x11.XK_KP_Enter -import x11.XK_Left -import x11.XK_Next -import x11.XK_Prior -import x11.XK_Return -import x11.XK_Right -import x11.XK_Tab -import x11.XK_Up -import x11.XK_space // What this display server calls a thing, turned into what the shared reader calls it. // @@ -59,13 +39,7 @@ internal fun buttonsOf(state: UInt): Int { * macOS puts Command, which is why it lands in Command's bit: a screen that binds one binds * the other, and neither platform has both. */ -internal fun modifiersOf(state: UInt): Int { - val bits = state.toInt() - return (if (bits and ShiftMask != 0) MODIFIER_SHIFT else 0) or - (if (bits and ControlMask != 0) MODIFIER_CONTROL else 0) or - (if (bits and Mod1Mask != 0) MODIFIER_ALT else 0) or - (if (bits and Mod4Mask != 0) MODIFIER_SUPER else 0) -} +internal fun modifiersOf(state: UInt): Int = x11Modifiers(state.toInt()) /** * The shared reader's number for a key this server named with a keysym. @@ -76,23 +50,7 @@ internal fun modifiersOf(state: UInt): Int { * meaning of its own (zero is the letter A in that numbering), which is most of them: a key that types a character carries the character * beside it and the scene reads that instead. */ -internal fun platformKey(keysym: KeySym): Int = when (keysym.toInt()) { - XK_Return, XK_KP_Enter -> PLATFORM_ENTER - XK_Tab, XK_ISO_Left_Tab -> PLATFORM_TAB - XK_space -> PLATFORM_SPACE - XK_BackSpace -> PLATFORM_BACKSPACE - XK_Escape -> PLATFORM_ESCAPE - XK_Delete -> PLATFORM_DELETE - XK_Left -> PLATFORM_LEFT - XK_Right -> PLATFORM_RIGHT - XK_Down -> PLATFORM_DOWN - XK_Up -> PLATFORM_UP - XK_Home -> PLATFORM_HOME - XK_End -> PLATFORM_END - XK_Prior -> PLATFORM_PAGE_UP - XK_Next -> PLATFORM_PAGE_DOWN - else -> -1 -} +internal fun platformKey(keysym: KeySym): Int = x11KeyNumber(keysym.toLong()) /** * This server's name for a pointer shape, from the number the scene asked with. @@ -121,27 +79,9 @@ internal const val CURSOR_TEXT = 2 internal const val CURSOR_CROSSHAIR = 3 /** From NSEvent.h. The bits the shared reader reads a modifier word's meaning out of. */ -private const val MODIFIER_SHIFT = 1 shl 17 -private const val MODIFIER_CONTROL = 1 shl 18 -private const val MODIFIER_ALT = 1 shl 19 -private const val MODIFIER_SUPER = 1 shl 20 // The shared numbering, which is AppKit's. `composeKey` in the interpreter's own sources is the // other end of each of these. -private const val PLATFORM_ENTER = 0x24 -private const val PLATFORM_TAB = 0x30 -private const val PLATFORM_SPACE = 0x31 -private const val PLATFORM_BACKSPACE = 0x33 -private const val PLATFORM_ESCAPE = 0x35 -private const val PLATFORM_DELETE = 0x75 -private const val PLATFORM_LEFT = 0x7B -private const val PLATFORM_RIGHT = 0x7C -private const val PLATFORM_DOWN = 0x7D -private const val PLATFORM_UP = 0x7E -private const val PLATFORM_HOME = 0x73 -private const val PLATFORM_END = 0x77 -private const val PLATFORM_PAGE_UP = 0x74 -private const val PLATFORM_PAGE_DOWN = 0x79 /** A wheel arrives as a press and a release of a button that does not exist. */ internal const val WHEEL_UP = 4 diff --git a/renderer/linux/src/shared/X11Keys.kt b/renderer/linux/src/shared/X11Keys.kt new file mode 120000 index 000000000..fcd478433 --- /dev/null +++ b/renderer/linux/src/shared/X11Keys.kt @@ -0,0 +1 @@ +../../../desktop/src/X11Keys.kt \ No newline at end of file diff --git a/renderer/linux/test/LinuxInputTest.kt b/renderer/linux/test/LinuxInputTest.kt index ae733fca1..b2ab50ecb 100644 --- a/renderer/linux/test/LinuxInputTest.kt +++ b/renderer/linux/test/LinuxInputTest.kt @@ -27,6 +27,8 @@ import x11.XK_Left import x11.XK_Return import x11.XK_Tab import x11.XK_a +import x11.XK_c +import x11.XK_v import x11.XK_space /** @@ -84,15 +86,20 @@ class LinuxInputTest { } /** - * A key that types a letter has no meaning of its own. + * A letter is its own key, because a shortcut is matched by which key it is. * - * Zero rather than a wrong answer. The letter travels beside the key as a character, and a - * key given some other key's number would move the caret instead of typing. + * It used to answer no key, so control with C, V, X, Z or A reached the scene as an unknown + * key and none of the editing shortcuts worked. The GraalVM window's table always had them. + * What a letter types still travels beside it as a character; an unlisted key still has no + * meaning. */ @Test - fun a_key_that_types_a_character_claims_no_meaning() { - assertEquals(-1, platformKey(XK_a.toULong())) - assertEquals(Key.Unknown, composeKey(platformKey(XK_a.toULong()))) + fun a_letter_is_the_key_it_is_and_an_unlisted_key_claims_nothing() { + assertEquals(Key.A, composeKey(platformKey(XK_a.toULong()))) + assertEquals(Key.C, composeKey(platformKey(XK_c.toULong()))) + assertEquals(Key.V, composeKey(platformKey(XK_v.toULong()))) + assertEquals(-1, platformKey(0xFFBEUL)) + assertEquals(Key.Unknown, composeKey(platformKey(0xFFBEUL))) } /** No two named keys share a number, or one of them does what the other means. */ diff --git a/scripts/tests/x11-key-codes.test.sh b/scripts/tests/x11-key-codes.test.sh index 433f0a42d..e7a07b6b9 100644 --- a/scripts/tests/x11-key-codes.test.sh +++ b/scripts/tests/x11-key-codes.test.sh @@ -1,71 +1,31 @@ #!/usr/bin/env bash -# The X11 window turns a key symbol into the shared key number, and a key it does not know -# must answer -1. Zero is the A key on the board the numbers come from, so a default of zero -# made control with C, V or X arrive as control with A: copy, paste and cut all selected -# everything. This compiles the function out of x11_window.c against the key symbol values -# from the X11 headers (written down here, so no X11 install is needed) and checks it. +# The X11 window used to carry its own table from a key symbol to the shared key number, in +# C, beside a second one in the Kotlin/Native window. They disagreed (the Kotlin/Native one had +# no letters, so control with C, V, X, Z or A arrived as an unknown key). There is one table +# now, in `renderer/desktop/src/X11Keys.kt`, and what is checked here is that the C window +# hands over the key symbol untouched and that the Kotlin side of both windows asks that table. +# The table itself is checked by `X11KeysTest`. set -uo pipefail repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" -source_file="$repo_root/renderer/desktop/c/x11_window.c" -[[ -f "$source_file" ]] || { echo "missing $source_file"; exit 1; } +c_file="$repo_root/renderer/desktop/c/x11_window.c" +graalvm_window="$repo_root/renderer/desktop/src/X11Window.kt" +native_input="$repo_root/renderer/linux/src/LinuxInput.kt" +status=0 +fail() { echo "FAIL: $1"; status=1; } -dir="$(mktemp -d)" -trap 'rm -rf "$dir"' EXIT -{ - cat <<'C' -#include -#include -typedef unsigned long KeySym; -#define XK_a 0x61 -#define XK_z 0x7a -#define XK_A 0x41 -#define XK_Z 0x5a -#define XK_0 0x30 -#define XK_9 0x39 -#define XK_Return 0xff0d -#define XK_KP_Enter 0xff8d -#define XK_Tab 0xff09 -#define XK_ISO_Left_Tab 0xfe20 -#define XK_space 0x20 -#define XK_BackSpace 0xff08 -#define XK_Escape 0xff1b -#define XK_Delete 0xffff -#define XK_Left 0xff51 -#define XK_Right 0xff53 -#define XK_Down 0xff54 -#define XK_Up 0xff52 -#define XK_Home 0xff50 -#define XK_End 0xff57 -#define XK_Prior 0xff55 -#define XK_Next 0xff56 -C - sed -n '/^static int32_t dxc_key_code(KeySym key) {/,/^}/p' "$source_file" - cat <<'C' -int main(void) { - printf("c %d\nC %d\nv %d\nx %d\nz %d\na %d\n5 %d\nReturn %d\nunknown %d\nF1 %d\n", - dxc_key_code('c'), dxc_key_code('C'), dxc_key_code('v'), dxc_key_code('x'), - dxc_key_code('z'), dxc_key_code('a'), dxc_key_code('5'), dxc_key_code(0xff0d), - dxc_key_code(0xfd00), dxc_key_code(0xffbe)); - return 0; -} -C -} > "$dir/probe.c" -cc -o "$dir/probe" "$dir/probe.c" || { echo "FAIL: dxc_key_code did not compile on its own"; exit 1; } -got="$("$dir/probe")" -expected="c 8 -C 8 -v 9 -x 7 -z 6 -a 0 -5 23 -Return 36 -unknown -1 -F1 -1" -if [[ "$got" != "$expected" ]]; then - echo "FAIL: the X11 key table answers differently from the shared board" - diff <(echo "$expected") <(echo "$got") - exit 1 -fi -echo "the X11 key table maps shortcuts and answers -1 for unknown keys: ok" +[[ -f "$c_file" && -f "$graalvm_window" && -f "$native_input" ]] || { echo "FAIL: a window source is missing"; exit 1; } + +grep -q 'record.key_code = (int32_t)symbol;' "$c_file" \ + || fail "x11_window.c no longer records the key symbol as the server named it" +grep -Eq 'static int32_t dxc_key_code|static int32_t dxc_modifiers' "$c_file" \ + && fail "x11_window.c carries a key or modifier table of its own again" +grep -q 'fromX11()' "$graalvm_window" \ + || fail "X11Window.kt does not turn the window's records into the shared numbering" +grep -q 'x11KeyNumber(' "$native_input" \ + || fail "the Kotlin/Native window does not ask the shared X11 key table" +grep -q 'x11Modifiers(' "$native_input" \ + || fail "the Kotlin/Native window does not ask the shared X11 modifier table" + +[[ $status -eq 0 ]] && echo "both X11 windows read one key table: ok" +exit $status From f828092eeece89d8035d8680a11a6b3781bb49c0 Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 23:25:32 +0900 Subject: [PATCH 10/20] Feat: One input method model for both X11 windows The GraalVM X11 window kept the preedit run in a wchar_t array in C and decided a key's events in C; the Kotlin/Native window did the same in Kotlin, twice written and not quite alike. The model (PreeditBuffer, ImeSession, keyEventsFor, candidateSpot) moves to ImeComposition.kt, shared by symlink. The C window now passes the key symbol, state word, typed text and the preedit edits through as records, and X11Events turns them into events with that code. A click while composing asks a new dxc_native_ime_reset, which the other two desktops answer with nothing. Behaviour change on the GraalVM X11 window: a plain ASCII key is now one key event carrying its character, as it already was on Kotlin/Native, where the C window also committed it as text. --- renderer/desktop/c/appkit_window.m | 9 + renderer/desktop/c/win32_window.c | 9 + renderer/desktop/c/x11_window.c | 151 +++++++------- renderer/desktop/src/AppKitWindow.kt | 20 ++ renderer/desktop/src/ImeComposition.kt | 179 +++++++++++++++++ renderer/desktop/src/WindowEvents.kt | 7 + renderer/desktop/src/X11Keys.kt | 48 ++++- renderer/desktop/src/X11Window.kt | 3 +- renderer/desktop/src/renderer/CodePoints.kt | 34 ++++ renderer/desktop/test/X11EventsTest.kt | 95 +++++++++ renderer/linux/src/InputMethod.kt | 211 +------------------- renderer/linux/src/LinuxWindow.kt | 2 +- renderer/linux/src/shared/CodePoints.kt | 1 + renderer/linux/src/shared/ImeComposition.kt | 1 + renderer/linux/test/InputMethodTest.kt | 16 +- scripts/tests/x11-key-codes.test.sh | 16 +- 16 files changed, 491 insertions(+), 311 deletions(-) create mode 100644 renderer/desktop/src/ImeComposition.kt create mode 100644 renderer/desktop/src/renderer/CodePoints.kt create mode 100644 renderer/desktop/test/X11EventsTest.kt create mode 120000 renderer/linux/src/shared/CodePoints.kt create mode 120000 renderer/linux/src/shared/ImeComposition.kt diff --git a/renderer/desktop/c/appkit_window.m b/renderer/desktop/c/appkit_window.m index 5a549c329..21be0970f 100644 --- a/renderer/desktop/c/appkit_window.m +++ b/renderer/desktop/c/appkit_window.m @@ -1230,3 +1230,12 @@ void dxc_native_set_ime_spot(float x, float y) { dxc_ime_spot_x = x; dxc_ime_spot_y = y; } + +/** + * Ends what the input method is composing and answers what it kept. Nothing here to end: this + * desktop's input method hands over whole strings and holds no composition of its own. + */ +int32_t dxc_native_ime_reset(char *out, int32_t capacity) { + (void)out; (void)capacity; + return 0; +} diff --git a/renderer/desktop/c/win32_window.c b/renderer/desktop/c/win32_window.c index 97aaaf172..518e6761e 100644 --- a/renderer/desktop/c/win32_window.c +++ b/renderer/desktop/c/win32_window.c @@ -2368,3 +2368,12 @@ void dxc_native_set_ime_spot(float x, float y) { dxc_position_ime(dxc_window); } } + +/** + * Ends what the input method is composing and answers what it kept. Nothing here to end: this + * desktop's input method hands over whole strings and holds no composition of its own. + */ +int32_t dxc_native_ime_reset(char *out, int32_t capacity) { + (void)out; (void)capacity; + return 0; +} diff --git a/renderer/desktop/c/x11_window.c b/renderer/desktop/c/x11_window.c index 8281c36e9..47fdf8fc4 100644 --- a/renderer/desktop/c/x11_window.c +++ b/renderer/desktop/c/x11_window.c @@ -27,6 +27,12 @@ enum { // Text the input method finished, and text it is still composing. DXC_EVENT_TEXT_COMMIT = 7, DXC_EVENT_TEXT_COMPOSE = 8, + // What the input method's preedit callbacks said, passed on as they were said: a + // composition began, a run of characters was replaced, the composition ended. What the + // composition holds is kept on the Kotlin side, in the model the other X11 window uses. + DXC_EVENT_PREEDIT_START = 13, + DXC_EVENT_PREEDIT_DRAW = 14, + DXC_EVENT_PREEDIT_DONE = 15, // Files over the window, let go on it, and gone from it without being let go. DXC_EVENT_FILES_ENTERED = 10, DXC_EVENT_FILES_DROPPED = 11, @@ -163,12 +169,10 @@ static int dxc_owns_clipboard; // is on the thread Xlib is used from. static volatile int dxc_raise_requested; -// The input method and the context made from it, and the text being composed in it. +// The input method and the context made from it. static XIM dxc_im; static XIC dxc_ic; #define DXC_PREEDIT_CAPACITY 256 -static wchar_t dxc_preedit[DXC_PREEDIT_CAPACITY]; -static int dxc_preedit_length; // Files being dragged over the window and the ones last let go on it. #define DXC_DROPPED_BYTES (64 * 1024) @@ -258,37 +262,18 @@ static int dxc_encode_utf8(unsigned long code, char *out) { return 4; } -/* Pushes text as an event, cut at a character boundary where it does not fit. */ -static void dxc_push_text(int32_t kind, const char *utf8, size_t length) { - struct dxc_event record; - memset(&record, 0, sizeof record); - record.kind = kind; - size_t take = length < DXC_TEXT_BYTES - 1 ? length : DXC_TEXT_BYTES - 1; - while (take > 0 && take < length && ((unsigned char)utf8[take] & 0xC0) == 0x80) { - take--; - } - memcpy(record.text, utf8, take); - dxc_push_event(record); -} - -/* What is being composed, as the whole run every time, which is what the scene is given. */ -static void dxc_push_preedit(void) { - char text[DXC_PREEDIT_CAPACITY * 4 + 1]; - size_t used = 0; - for (int index = 0; index < dxc_preedit_length; index++) { - used += (size_t)dxc_encode_utf8((unsigned long)dxc_preedit[index], text + used); - } - dxc_push_text(DXC_EVENT_TEXT_COMPOSE, text, used); -} - /* - * The input method's preedit callbacks. The method says which run of characters changed - * and what they are now, counted in characters as the protocol counts them, and the whole - * run is kept here and handed to the scene each time. + * The input method's preedit callbacks. Each is passed on as an event and nothing is kept + * here: the method says which run of characters changed and what they are now, counted in + * characters as the protocol counts them, and the model that applies that to the run being + * composed is the Kotlin one (`ImeComposition.kt`), which the Kotlin/Native window uses too. */ static int dxc_preedit_start(XIC ic, XPointer client, XPointer call) { (void)ic; (void)client; (void)call; - dxc_preedit_length = 0; + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = DXC_EVENT_PREEDIT_START; + dxc_push_event(record); // No limit on the length of what is composed. The method reads this return value, and // a function that returns nothing would leave whatever was in the register. return -1; @@ -297,44 +282,46 @@ static int dxc_preedit_start(XIC ic, XPointer client, XPointer call) { static void dxc_preedit_draw(XIC ic, XPointer client, XIMPreeditDrawCallbackStruct *draw) { (void)ic; (void)client; if (draw == NULL) return; - int first = draw->chg_first; - int removed = draw->chg_length; - if (first < 0) first = 0; - if (first > dxc_preedit_length) first = dxc_preedit_length; - if (removed < 0) removed = 0; - if (first + removed > dxc_preedit_length) removed = dxc_preedit_length - first; - - wchar_t added[DXC_PREEDIT_CAPACITY]; - int added_length = 0; + // The characters the method put in, as UTF-8. Decoding what Xlib handed over is this + // file's job; what the characters mean for the composition is not. + char utf8[DXC_PREEDIT_CAPACITY * 4 + 1]; + size_t used = 0; XIMText *text = draw->text; if (text != NULL) { if (text->encoding_is_wchar && text->string.wide_char != NULL) { - for (int i = 0; i < (int)text->length && added_length < DXC_PREEDIT_CAPACITY; i++) { - added[added_length++] = text->string.wide_char[i]; + for (int i = 0; i < (int)text->length && i < DXC_PREEDIT_CAPACITY; i++) { + used += (size_t)dxc_encode_utf8((unsigned long)text->string.wide_char[i], utf8 + used); } } else if (text->string.multi_byte != NULL) { + wchar_t added[DXC_PREEDIT_CAPACITY]; size_t count = mbstowcs(added, text->string.multi_byte, DXC_PREEDIT_CAPACITY); - if (count != (size_t)-1) added_length = (int)count; + if (count != (size_t)-1) { + for (size_t i = 0; i < count; i++) { + used += (size_t)dxc_encode_utf8((unsigned long)added[i], utf8 + used); + } + } } } - int tail = dxc_preedit_length - first - removed; - if (first + added_length + tail > DXC_PREEDIT_CAPACITY) { - added_length = DXC_PREEDIT_CAPACITY - first - tail; - if (added_length < 0) return; + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = DXC_EVENT_PREEDIT_DRAW; + record.key_code = draw->chg_first; + record.code_point = draw->chg_length; + record.x = (float)draw->caret; + size_t take = used < DXC_TEXT_BYTES - 1 ? used : DXC_TEXT_BYTES - 1; + while (take > 0 && take < used && ((unsigned char)utf8[take] & 0xC0) == 0x80) { + take--; } - memmove(dxc_preedit + first + added_length, dxc_preedit + first + removed, - (size_t)tail * sizeof(wchar_t)); - memcpy(dxc_preedit + first, added, (size_t)added_length * sizeof(wchar_t)); - dxc_preedit_length = first + added_length + tail; - dxc_push_preedit(); + memcpy(record.text, utf8, take); + dxc_push_event(record); } static void dxc_preedit_done(XIC ic, XPointer client, XPointer call) { (void)ic; (void)client; (void)call; - if (dxc_preedit_length > 0) { - dxc_preedit_length = 0; - dxc_push_preedit(); - } + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = DXC_EVENT_PREEDIT_DONE; + dxc_push_event(record); } static void dxc_preedit_caret(XIC ic, XPointer client, XPointer call) { @@ -396,6 +383,25 @@ void dxc_native_set_ime_spot(float x, float y) { XFree(list); } +/** + * Ends what the input method is composing where it stands, and copies what it kept into + * [out] as UTF-8. Answers how many bytes that is, zero where there was nothing. + */ +int32_t dxc_native_ime_reset(char *out, int32_t capacity) { + if (dxc_ic == NULL || capacity <= 0) return 0; + char *kept = Xutf8ResetIC(dxc_ic); + if (kept == NULL) return 0; + size_t length = strlen(kept); + if (length > (size_t)capacity - 1) { + length = (size_t)capacity - 1; + while (length > 0 && ((unsigned char)kept[length] & 0xC0) == 0x80) length--; + } + memcpy(out, kept, length); + out[length] = 0; + XFree(kept); + return (int32_t)length; +} + /* The bytes of a text property, or null. */ static unsigned char *dxc_read_property(Window window, Atom property, unsigned long *length, Atom *type) { @@ -800,17 +806,6 @@ static void dxc_pump_events(void) { record.x = button == 6 ? -3.0f : button == 7 ? 3.0f : 0.0f; record.y = button == 4 ? -3.0f : button == 5 ? 3.0f : 0.0f; } else { - if (event.type == ButtonPress && dxc_ic != NULL && dxc_preedit_length > 0) { - // A click while composing ends the composition and keeps what was - // typed, which is what the other desktops' input methods do. - char *kept = Xutf8ResetIC(dxc_ic); - dxc_preedit_length = 0; - dxc_push_preedit(); - if (kept != NULL) { - dxc_push_text(DXC_EVENT_TEXT_COMMIT, kept, strlen(kept)); - XFree(kept); - } - } record.kind = event.type == ButtonPress ? DXC_EVENT_POINTER_DOWN : DXC_EVENT_POINTER_UP; record.x = (float)event.xbutton.x; record.y = (float)event.xbutton.y; @@ -844,22 +839,18 @@ static void dxc_pump_events(void) { } } record.kind = event.type == KeyPress ? DXC_EVENT_KEY_DOWN : DXC_EVENT_KEY_UP; - // The keysym and the state word as the server gave them. `X11Keys.kt` turns them into - // the shared numbering, the same function the Kotlin/Native window asks. + // The keysym, the state word and the text as the server gave them. What the + // key means, and whether its text is typed or only carried, is decided in + // `ImeComposition.kt` and `X11Keys.kt`, the same code the Kotlin/Native + // window asks. Text is sent for a press only. record.key_code = (int32_t)symbol; record.modifiers = (int32_t)event.xkey.state; - if (count == 1 && (unsigned char)bytes[0] >= 32 && (unsigned char)bytes[0] < 127) { - record.code_point = (unsigned char)bytes[0]; - } - // The key is recorded first and its text after, the order the other desktops - // use. A shortcut types nothing: control, alt and the super key held mean - // the key is a command. - if (event.type == KeyPress && count > 0 && - (event.xkey.state & (ControlMask | Mod1Mask | Mod4Mask)) == 0 && - (unsigned char)bytes[0] >= 0x20 && bytes[0] != 0x7f) { - dxc_push_event(record); - dxc_push_text(DXC_EVENT_TEXT_COMMIT, bytes, (size_t)count); - continue; + if (event.type == KeyPress && count > 0) { + size_t take = (size_t)count < DXC_TEXT_BYTES - 1 ? (size_t)count : DXC_TEXT_BYTES - 1; + while (take > 0 && take < (size_t)count && ((unsigned char)bytes[take] & 0xC0) == 0x80) { + take--; + } + memcpy(record.text, bytes, take); } break; } diff --git a/renderer/desktop/src/AppKitWindow.kt b/renderer/desktop/src/AppKitWindow.kt index 0c39d70f1..7771eaaf9 100644 --- a/renderer/desktop/src/AppKitWindow.kt +++ b/renderer/desktop/src/AppKitWindow.kt @@ -140,6 +140,9 @@ private external fun droppedPaths(out: Pointer?, capacity: Int): Int @CFunction("dxc_native_set_ime_spot") private external fun setImeSpot(x: Float, y: Float) +@CFunction("dxc_native_ime_reset") +private external fun resetInputContext(out: Pointer?, capacity: Int): Int + @CFunction("dxc_native_window_action") private external fun windowAction(action: Int) @@ -446,6 +449,23 @@ fun installApplicationMenu(name: String) { } } +/** + * Ends what the input method is composing and answers the text it kept, or empty where it kept + * none. Only the X11 window has a composition of its own to end. + */ +internal fun resetInputMethod(): String { + val buffer = StackValue.get(RESET_BYTES) + val length = resetInputContext(buffer, RESET_BYTES) + if (length <= 0) return "" + val bytes = ByteArray(length) + for (index in 0 until length) { + bytes[index] = buffer.readByte(index) + } + return String(bytes, Charsets.UTF_8) +} + +private const val RESET_BYTES = 256 + /** What is on the clipboard, or empty where it holds something that is not text. */ fun readClipboard(): String { val buffer = StackValue.get(CLIPBOARD_BYTES) diff --git a/renderer/desktop/src/ImeComposition.kt b/renderer/desktop/src/ImeComposition.kt new file mode 100644 index 000000000..0d4ec73ec --- /dev/null +++ b/renderer/desktop/src/ImeComposition.kt @@ -0,0 +1,179 @@ +package dev.darkpyonix.composerust.ui.platform + +import androidx.compose.ui.geometry.Rect + +// What an input method says, turned into what the shared reader hears. +// +// One model for both X11 windows. The window that links the native image gets the input +// method's callbacks in C and the Kotlin/Native window gets them from Xlib; each hands what +// it was told to this file as plain numbers and text, so a preedit edit applied at the +// wrong offset, or a commit dropped when a syllable is finished, can be wrong in one place +// only and is checked by tests that need no input method daemon. +// +// Nothing here reaches Rust. What an input method produces is a `WindowEvent` of the kinds +// the window already has, recorded into the same log as a pointer move, and it ends in the +// text input session Compose opened for the focused field. + +/** + * The text an input method is still composing, kept the way X's preedit callbacks describe + * it: as edits to a run of characters. + * + * The draw callback does not send the whole string. It says "replace [length] characters + * from [first] with this", so a client has to hold the run to apply that to. Counted in code + * points and not in UTF-16 units, because that is what the protocol counts: a Hangul syllable + * is one, and so is a character outside the Basic Multilingual Plane, which is two units. + */ +internal class PreeditBuffer { + private val points = ArrayList() + + /** Where the input method says the caret is inside the run, in characters. */ + var caret = 0 + private set + + val isEmpty: Boolean get() = points.isEmpty() + + /** The run as a string, in the encoding Compose reads. */ + val text: String + get() = buildString { for (point in points) appendPoint(point) } + + /** + * Applies one draw callback. + * + * A start past the end is clamped to the end rather than rejected: a client that threw + * here would be a window that dies in the middle of someone's sentence, and appending is + * what an input method that miscounted meant. + */ + fun replace(first: Int, length: Int, with: String, newCaret: Int) { + val start = first.coerceIn(0, points.size) + val end = (start + length.coerceAtLeast(0)).coerceAtMost(points.size) + for (index in end - 1 downTo start) points.removeAt(index) + points.addAll(start, codePointsOf(with)) + caret = newCaret.coerceIn(0, points.size) + } + + fun clear() { + points.clear() + caret = 0 + } +} + +/** + * One client's side of an input method: the callbacks it makes, and what each of them means. + * + * Takes [emit] rather than a window, so that a test hands it a list and a window hands it the + * log every other event goes into. Everything arrives on the thread that reads the display + * server, from inside the one call that reads it, so nothing here is ever re-entered and + * nothing needs a lock. + */ +internal class ImeSession(private val emit: (WindowEvent) -> Unit) { + private val preedit = PreeditBuffer() + + /** Whether something is being composed, which is what a click has to finish. */ + val composing: Boolean get() = !preedit.isEmpty + + /** The input method begins a composition. Nothing is shown until it draws. */ + fun preeditStart() { + preedit.clear() + } + + /** + * The composition changed. Compose is given the whole run each time, because that is what + * its command replaces: the marked text is the run, and what was marked before is gone. + */ + fun preeditDraw(first: Int, length: Int, text: String, caret: Int) { + preedit.replace(first, length, text, caret) + emit(imeEvent(WindowEvent.TEXT_COMPOSE, preedit.text)) + } + + /** The composition ended without committing anything: the reader backed out of it. */ + fun preeditDone() { + if (preedit.isEmpty) return + preedit.clear() + emit(imeEvent(WindowEvent.TEXT_COMPOSE, "")) + } + + /** + * Finished text. Whatever was being composed is taken out first: some input methods clear + * the preedit and then commit, and some commit and never clear it, and a field must not be + * left holding both the syllable and the mark that was over it. + */ + fun commit(text: String) { + if (text.isEmpty()) return + preeditDone() + emit(imeEvent(WindowEvent.TEXT_COMMIT, text)) + } +} + +/** A record for something an input method said: no position, no key, only the text. */ +internal fun imeEvent(kind: Int, text: String) = WindowEvent( + kind = kind, + x = 0f, + y = 0f, + buttons = 0, + modifiers = 0, + keyCode = 0, + codePoint = 0, + text = text, +) + +/** + * What one key press or release becomes, given what the keyboard and the input method said it + * typed. + * + * [text] is whatever came back from asking for the characters of the event: empty for a key + * that types nothing, one character for most, and several for an input method that committed + * a word. Three cases follow from it, and each exists because of a way this went wrong. + * + * - A key that types one plain ASCII character keeps carrying it in the key event, which is + * how every field here has been typed into so far. Moving it would change what a shortcut + * sees. + * - Anything else printable is committed as text and the key event carries no character. A + * Hangul syllable put in a key event would be read as one code unit by a field that expects + * a character from a keyboard. + * - An event with no key behind it at all, which is what an input method sends to say it has + * committed, produces the commit and no key event: there is nothing for a shortcut to + * see, and a key with no name pressed in a text field would only confuse a handler. + */ +internal fun keyEventsFor( + press: Boolean, + state: Int, + keysym: Long, + text: String, +): List { + val key = x11KeyNumber(keysym) + val modifiers = x11Modifiers(state) + if (!press) { + return listOf( + WindowEvent(WindowEvent.KEY_UP, 0f, 0f, 0, modifiers, key, 0, ""), + ) + } + val first = text.firstOrNull() + val printable = first != null && first.code >= FIRST_PRINTABLE && first.code != DELETE_CHARACTER + val plain = text.length == 1 && first!!.code in FIRST_PRINTABLE until DELETE_CHARACTER + val shortcut = x11IsShortcut(state) + val down = WindowEvent( + WindowEvent.KEY_DOWN, 0f, 0f, 0, modifiers, key, + if (plain) first!!.code else 0, "", + ) + if (!printable || plain) return listOf(down) + if (shortcut) return listOf(down) + val commit = imeEvent(WindowEvent.TEXT_COMMIT, text) + return if (keysym == 0L) listOf(commit) else listOf(down, commit) +} + +/** + * Where an input method should put its candidate window: under the caret, in the window's own + * pixels, or null where the field has not been laid out yet. + * + * The rectangle is Compose's own answer for the focused field's caret, in the root's units. + * The scene's density is one point to a pixel (see the window's note on it), so no scale + * is applied; [density] is there so that changing that changes one place. + */ +internal fun candidateSpot(caret: Rect?, density: Float): Pair? { + if (caret == null || caret.isEmpty) return null + return (caret.left * density).toInt() to (caret.bottom * density).toInt() +} + +private const val FIRST_PRINTABLE = 32 +private const val DELETE_CHARACTER = 0x7F + diff --git a/renderer/desktop/src/WindowEvents.kt b/renderer/desktop/src/WindowEvents.kt index 8fa49f214..373d70a02 100644 --- a/renderer/desktop/src/WindowEvents.kt +++ b/renderer/desktop/src/WindowEvents.kt @@ -54,6 +54,13 @@ data class WindowEvent( const val FILES_ENTERED = 10 const val FILES_DROPPED = 11 const val FILES_EXITED = 12 + + // An input method's preedit callbacks as the X11 window heard them: a composition began, + // a run of characters was replaced (first in `keyCode`, how many in `codePoint`, the caret + // in `x`, the new characters in `text`), and the composition ended. + const val PREEDIT_START = 13 + const val PREEDIT_DRAW = 14 + const val PREEDIT_DONE = 15 } } diff --git a/renderer/desktop/src/X11Keys.kt b/renderer/desktop/src/X11Keys.kt index a81fbbcad..83a24f12f 100644 --- a/renderer/desktop/src/X11Keys.kt +++ b/renderer/desktop/src/X11Keys.kt @@ -39,16 +39,48 @@ internal fun x11KeyNumber(keysym: Long): Int { } /** - * A record from the X11 window, with the keysym and state word it carries turned into the - * shared numbering. Every other record passes through unchanged, apart from its modifiers. + * What the X11 window's records become for the scene. + * + * The window records what the server and the input method said as it was said: a key as a + * keysym, a state word and the text it typed, and the preedit callbacks as they came. The two + * windows on X11 then turn that into events with the same code, `keyEventsFor` and [ImeSession], + * so the key and the composition mean the same on the native image and on Kotlin/Native. + * + * [resetInput] ends the input method's composition and answers what it kept. It is asked when + * a button goes down while something is being composed, because a click ends the composition + * and keeps what was typed, which is what the other desktops' input methods do. */ -internal fun WindowEvent.fromX11(): WindowEvent { - val numbered = if (kind == WindowEvent.KEY_DOWN || kind == WindowEvent.KEY_UP) { - x11KeyNumber(keyCode.toLong() and 0xFFFFFFFFL) - } else { - keyCode +internal class X11Events(private val resetInput: () -> String) { + private val pending = ArrayList() + private val ime = ImeSession { pending += it } + + fun heard(raw: WindowEvent): List { + pending.clear() + when (raw.kind) { + WindowEvent.KEY_DOWN, WindowEvent.KEY_UP -> pending += keyEventsFor( + press = raw.kind == WindowEvent.KEY_DOWN, + state = raw.modifiers, + keysym = raw.keyCode.toLong() and 0xFFFFFFFFL, + text = raw.text, + ) + WindowEvent.PREEDIT_START -> ime.preeditStart() + WindowEvent.PREEDIT_DRAW -> ime.preeditDraw( + first = raw.keyCode, + length = raw.codePoint, + text = raw.text, + caret = raw.x.toInt(), + ) + WindowEvent.PREEDIT_DONE -> ime.preeditDone() + else -> { + if (raw.kind == WindowEvent.POINTER_DOWN && ime.composing) { + val kept = resetInput() + if (kept.isNotEmpty()) ime.commit(kept) else ime.preeditDone() + } + pending += raw.copy(modifiers = x11Modifiers(raw.modifiers)) + } + } + return ArrayList(pending) } - return copy(keyCode = numbered, modifiers = x11Modifiers(modifiers)) } /** diff --git a/renderer/desktop/src/X11Window.kt b/renderer/desktop/src/X11Window.kt index 2ea19d8e0..91e0299f2 100644 --- a/renderer/desktop/src/X11Window.kt +++ b/renderer/desktop/src/X11Window.kt @@ -113,6 +113,7 @@ internal fun runX11Window(autoExitMillis: Long? = null) { systemChrome = options.systemChrome, backdrop = false, ) + val x11Events = X11Events(::resetInputMethod) val window = openX11Window(options.title, options.width, options.height) if (window == null) { System.err.println("compose-rust: X11 or a GLX visual is unavailable") @@ -214,7 +215,7 @@ internal fun runX11Window(autoExitMillis: Long? = null) { work.runPending() var heard = false var drew = false - for (event in drainWindowEvents().map { it.fromX11() }) { + for (event in drainWindowEvents().flatMap { x11Events.heard(it) }) { if (report && event.kind != WindowEvent.POINTER_MOVE) { System.err.println("compose-rust: window heard $event") } diff --git a/renderer/desktop/src/renderer/CodePoints.kt b/renderer/desktop/src/renderer/CodePoints.kt new file mode 100644 index 000000000..1b64bd931 --- /dev/null +++ b/renderer/desktop/src/renderer/CodePoints.kt @@ -0,0 +1,34 @@ +package dev.darkpyonix.composerust.ui.platform + +// Code points of a string, which the accessibility server counts in and the Kotlin/Native +// window's input method counts in as well. One definition for both: the input method's file +// in the Kotlin/Native renderer declares the same two functions today and drops them when it +// takes these from here. + +/** Appends one code point, as the pair of surrogates it is when it is past the first plane. */ +internal fun StringBuilder.appendPoint(point: Int) { + if (point < 0x10000) { + append(point.toChar()) + } else { + val offset = point - 0x10000 + append((0xD800 + (offset shr 10)).toChar()) + append((0xDC00 + (offset and 0x3FF)).toChar()) + } +} + +/** The code points of a string, with a pair of surrogates read as the one character it is. */ +internal fun codePointsOf(text: String): List { + val points = ArrayList(text.length) + var index = 0 + while (index < text.length) { + val unit = text[index] + if (unit.isHighSurrogate() && index + 1 < text.length && text[index + 1].isLowSurrogate()) { + points += 0x10000 + ((unit.code - 0xD800) shl 10) + (text[index + 1].code - 0xDC00) + index += 2 + } else { + points += unit.code + index += 1 + } + } + return points +} diff --git a/renderer/desktop/test/X11EventsTest.kt b/renderer/desktop/test/X11EventsTest.kt new file mode 100644 index 000000000..8666f56d0 --- /dev/null +++ b/renderer/desktop/test/X11EventsTest.kt @@ -0,0 +1,95 @@ +package dev.darkpyonix.composerust.test + +import dev.darkpyonix.composerust.ui.platform.WindowEvent +import dev.darkpyonix.composerust.ui.platform.X11Events +import dev.darkpyonix.composerust.ui.platform.X11_CONTROL_MASK +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +/** + * What the X11 window's records become, which is the same code the Kotlin/Native window runs + * on what it reads from Xlib. A composition and a key are checked here with no display and no + * input method. + */ +class X11EventsTest { + private fun record( + kind: Int, + keyCode: Int = 0, + modifiers: Int = 0, + codePoint: Int = 0, + x: Float = 0f, + text: String = "", + ) = WindowEvent(kind, x, 0f, 0, modifiers, keyCode, codePoint, text) + + private fun events(reset: () -> String = { "" }) = X11Events(reset) + + @Test + fun a_plain_letter_is_one_key_event_carrying_its_character() { + val heard = events().heard(record(WindowEvent.KEY_DOWN, keyCode = 'a'.code, text = "a")) + assertEquals(1, heard.size) + assertEquals(WindowEvent.KEY_DOWN, heard[0].kind) + assertEquals('a'.code, heard[0].codePoint) + assertEquals(0x00, heard[0].keyCode, "a is the A key on the shared board") + } + + @Test + fun control_with_c_is_the_c_key_and_types_nothing() { + val heard = events().heard( + record(WindowEvent.KEY_DOWN, keyCode = 'c'.code, modifiers = X11_CONTROL_MASK, text = "\u0003"), + ) + assertEquals(1, heard.size) + assertEquals(0x08, heard[0].keyCode) + assertEquals(1 shl 18, heard[0].modifiers) + assertEquals("", heard[0].text) + } + + @Test + fun a_committed_word_is_text_with_no_key_behind_it() { + val heard = events().heard(record(WindowEvent.KEY_DOWN, keyCode = 0, text = "한글")) + assertEquals(listOf(WindowEvent.TEXT_COMMIT to "한글"), heard.map { it.kind to it.text }) + } + + @Test + fun a_composition_is_handed_over_as_the_whole_run_every_time() { + val x11 = events() + assertTrue(x11.heard(record(WindowEvent.PREEDIT_START)).isEmpty()) + val first = x11.heard(record(WindowEvent.PREEDIT_DRAW, keyCode = 0, codePoint = 0, x = 1f, text = "ㅎ")) + assertEquals(listOf(WindowEvent.TEXT_COMPOSE to "ㅎ"), first.map { it.kind to it.text }) + val second = x11.heard(record(WindowEvent.PREEDIT_DRAW, keyCode = 0, codePoint = 1, x = 1f, text = "하")) + assertEquals(listOf(WindowEvent.TEXT_COMPOSE to "하"), second.map { it.kind to it.text }) + val third = x11.heard(record(WindowEvent.PREEDIT_DRAW, keyCode = 1, codePoint = 0, x = 2f, text = "ㄴ")) + assertEquals(listOf(WindowEvent.TEXT_COMPOSE to "하ㄴ"), third.map { it.kind to it.text }) + val done = x11.heard(record(WindowEvent.PREEDIT_DONE)) + assertEquals(listOf(WindowEvent.TEXT_COMPOSE to ""), done.map { it.kind to it.text }) + } + + @Test + fun a_click_while_composing_ends_the_composition_and_keeps_what_was_typed() { + var resets = 0 + val x11 = events { resets++; "하" } + x11.heard(record(WindowEvent.PREEDIT_START)) + x11.heard(record(WindowEvent.PREEDIT_DRAW, text = "하", x = 1f)) + val heard = x11.heard(record(WindowEvent.POINTER_DOWN, x = 10f)) + assertEquals(1, resets) + assertEquals( + listOf(WindowEvent.TEXT_COMPOSE, WindowEvent.TEXT_COMMIT, WindowEvent.POINTER_DOWN), + heard.map { it.kind }, + ) + assertEquals("하", heard[1].text) + } + + @Test + fun a_click_with_nothing_composing_asks_nothing_of_the_input_method() { + var resets = 0 + val heard = events { resets++; "x" }.heard(record(WindowEvent.POINTER_DOWN, x = 10f)) + assertEquals(0, resets) + assertEquals(listOf(WindowEvent.POINTER_DOWN), heard.map { it.kind }) + } + + @Test + fun a_pointer_record_has_its_modifiers_in_the_shared_bits() { + val heard = events().heard(record(WindowEvent.POINTER_MOVE, modifiers = X11_CONTROL_MASK)) + assertEquals(1 shl 18, heard[0].modifiers) + } +} diff --git a/renderer/linux/src/InputMethod.kt b/renderer/linux/src/InputMethod.kt index c9768e764..9aa57a924 100644 --- a/renderer/linux/src/InputMethod.kt +++ b/renderer/linux/src/InputMethod.kt @@ -2,184 +2,9 @@ package dev.darkpyonix.composerust.ui.platform -import androidx.compose.ui.geometry.Rect -import x11.ControlMask -import x11.KeySym -import x11.Mod1Mask -import x11.Mod4Mask - -// What an input method says, turned into what the shared reader hears. -// -// Apart from the XIM calls in `XimContext.kt` because these are the part that can be wrong -// without anything looking wrong. A preedit edit applied at the wrong offset still shows -// letters; a commit that is dropped when a syllable is finished still shows the syllable -// that was being composed. Both are plain arithmetic on text, and arithmetic is checkable -// without an input method daemon. -// -// Nothing here reaches Rust. What an input method produces is a `WindowEvent` of the kinds -// the window already has, recorded into the same log as a pointer move, and it ends in the -// text input session Compose opened for the focused field. The Host is told a field changed -// by the field, the way it is for a key typed on any other platform. - -/** - * The text an input method is still composing, kept the way X's preedit callbacks describe - * it: as edits to a run of characters. - * - * The draw callback does not send the whole string. It says "replace [length] characters - * from [first] with this", so a client has to hold the run to apply that to. Counted in code - * points and not in UTF-16 units, because that is what the protocol counts: a Hangul syllable - * is one, and so is a character outside the Basic Multilingual Plane, which is two units. - */ -internal class PreeditBuffer { - private val points = ArrayList() - - /** Where the input method says the caret is inside the run, in characters. */ - var caret = 0 - private set - - val isEmpty: Boolean get() = points.isEmpty() - - /** The run as a string, in the encoding Compose reads. */ - val text: String - get() = buildString { for (point in points) appendPoint(point) } - - /** - * Applies one draw callback. - * - * A start past the end is clamped to the end rather than rejected: a client that threw - * here would be a window that dies in the middle of someone's sentence, and appending is - * what an input method that miscounted meant. - */ - fun replace(first: Int, length: Int, with: String, newCaret: Int) { - val start = first.coerceIn(0, points.size) - val end = (start + length.coerceAtLeast(0)).coerceAtMost(points.size) - for (index in end - 1 downTo start) points.removeAt(index) - points.addAll(start, codePointsOf(with)) - caret = newCaret.coerceIn(0, points.size) - } - - fun clear() { - points.clear() - caret = 0 - } -} - -/** - * One client's side of an input method: the callbacks it makes, and what each of them means. - * - * Takes [emit] rather than a window, so that a test hands it a list and a window hands it the - * log every other event goes into. Everything arrives on the thread that reads the display - * server, from inside the one call that reads it, so nothing here is ever re-entered and - * nothing needs a lock. - */ -internal class ImeSession(private val emit: (WindowEvent) -> Unit) { - private val preedit = PreeditBuffer() - - /** Whether something is being composed, which is what a click has to finish. */ - val composing: Boolean get() = !preedit.isEmpty - - /** The input method begins a composition. Nothing is shown until it draws. */ - fun preeditStart() { - preedit.clear() - } - - /** - * The composition changed. Compose is given the whole run each time, because that is what - * its command replaces: the marked text is the run, and what was marked before is gone. - */ - fun preeditDraw(first: Int, length: Int, text: String, caret: Int) { - preedit.replace(first, length, text, caret) - emit(imeEvent(WindowEvent.TEXT_COMPOSE, preedit.text)) - } - - /** The composition ended without committing anything: the reader backed out of it. */ - fun preeditDone() { - if (preedit.isEmpty) return - preedit.clear() - emit(imeEvent(WindowEvent.TEXT_COMPOSE, "")) - } - - /** - * Finished text. Whatever was being composed is taken out first: some input methods clear - * the preedit and then commit, and some commit and never clear it, and a field must not be - * left holding both the syllable and the mark that was over it. - */ - fun commit(text: String) { - if (text.isEmpty()) return - preeditDone() - emit(imeEvent(WindowEvent.TEXT_COMMIT, text)) - } -} - -/** A record for something an input method said: no position, no key, only the text. */ -internal fun imeEvent(kind: Int, text: String) = WindowEvent( - kind = kind, - x = 0f, - y = 0f, - buttons = 0, - modifiers = 0, - keyCode = 0, - codePoint = 0, - text = text, -) - -/** - * What one key press or release becomes, given what the keyboard and the input method said it - * typed. - * - * [text] is whatever came back from asking for the characters of the event: empty for a key - * that types nothing, one character for most, and several for an input method that committed - * a word. Three cases follow from it, and each exists because of a way this went wrong. - * - * - A key that types one plain ASCII character keeps carrying it in the key event, which is - * how every field here has been typed into so far. Moving it would change what a shortcut - * sees. - * - Anything else printable is committed as text and the key event carries no character. A - * Hangul syllable put in a key event would be read as one code unit by a field that expects - * a character from a keyboard. - * - An event with no key behind it at all, which is what an input method sends to say it has - * committed, produces the commit and no key event: there is nothing for a shortcut to - * see, and a key with no name pressed in a text field would only confuse a handler. - */ -internal fun keyEventsFor( - press: Boolean, - state: UInt, - keysym: KeySym, - text: String, -): List { - val key = platformKey(keysym) - val modifiers = modifiersOf(state) - if (!press) { - return listOf( - WindowEvent(WindowEvent.KEY_UP, 0f, 0f, 0, modifiers, key, 0, ""), - ) - } - val first = text.firstOrNull() - val printable = first != null && first.code >= FIRST_PRINTABLE && first.code != DELETE_CHARACTER - val plain = text.length == 1 && first!!.code in FIRST_PRINTABLE until DELETE_CHARACTER - val shortcut = state.toInt() and (ControlMask or Mod1Mask or Mod4Mask) != 0 - val down = WindowEvent( - WindowEvent.KEY_DOWN, 0f, 0f, 0, modifiers, key, - if (plain) first!!.code else 0, "", - ) - if (!printable || plain) return listOf(down) - if (shortcut) return listOf(down) - val commit = imeEvent(WindowEvent.TEXT_COMMIT, text) - return if (keysym == 0UL) listOf(commit) else listOf(down, commit) -} - -/** - * Where an input method should put its candidate window: under the caret, in the window's own - * pixels, or null where the field has not been laid out yet. - * - * The rectangle is Compose's own answer for the focused field's caret, in the root's units. - * The scene's density is one point to a pixel (see the window's note on it), so no scale - * is applied; [density] is there so that changing that changes one place. - */ -internal fun candidateSpot(caret: Rect?, density: Float): Pair? { - if (caret == null || caret.isEmpty) return null - return (caret.left * density).toInt() to (caret.bottom * density).toInt() -} +// The Xlib side of an input method: which style to ask it for and which locale it needs. +// What it says once it is talking, the preedit model and the key decision, is `ImeComposition.kt`, +// shared with the window that links the native image. /** * Which input style to ask an input method for, out of the ones it offers. @@ -218,33 +43,3 @@ internal fun isUtf8Locale(name: String?): Boolean { val lower = name.lowercase() return lower.endsWith(".utf-8") || lower.endsWith(".utf8") || ".utf-8@" in lower || ".utf8@" in lower } - -private const val FIRST_PRINTABLE = 32 -private const val DELETE_CHARACTER = 0x7F - -internal fun StringBuilder.appendPoint(point: Int) { - if (point < 0x10000) { - append(point.toChar()) - } else { - val offset = point - 0x10000 - append((0xD800 + (offset shr 10)).toChar()) - append((0xDC00 + (offset and 0x3FF)).toChar()) - } -} - -/** The code points of a string, with a pair of surrogates read as the one character it is. */ -internal fun codePointsOf(text: String): List { - val points = ArrayList(text.length) - var index = 0 - while (index < text.length) { - val unit = text[index] - if (unit.isHighSurrogate() && index + 1 < text.length && text[index + 1].isLowSurrogate()) { - points += 0x10000 + ((unit.code - 0xD800) shl 10) + (text[index + 1].code - 0xDC00) - index += 2 - } else { - points += unit.code - index += 1 - } - } - return points -} diff --git a/renderer/linux/src/LinuxWindow.kt b/renderer/linux/src/LinuxWindow.kt index 1d0f0c95d..1c1ee7faa 100644 --- a/renderer/linux/src/LinuxWindow.kt +++ b/renderer/linux/src/LinuxWindow.kt @@ -668,7 +668,7 @@ internal class LinuxWindow private constructor( val press = event.type == KeyPress val keysym = XLookupKeysym(event.xkey.ptr, 0) val typed = if (!press) "" else xim?.lookup(event) ?: latinText(event) - return keyEventsFor(press, event.xkey.state, keysym, typed) + return keyEventsFor(press, event.xkey.state.toInt(), keysym.toLong(), typed) } /** What the key types without an input method: Latin-1, as `XLookupString` answers. */ diff --git a/renderer/linux/src/shared/CodePoints.kt b/renderer/linux/src/shared/CodePoints.kt new file mode 120000 index 000000000..e321fbd29 --- /dev/null +++ b/renderer/linux/src/shared/CodePoints.kt @@ -0,0 +1 @@ +../../../desktop/src/renderer/CodePoints.kt \ No newline at end of file diff --git a/renderer/linux/src/shared/ImeComposition.kt b/renderer/linux/src/shared/ImeComposition.kt new file mode 120000 index 000000000..2cc359b12 --- /dev/null +++ b/renderer/linux/src/shared/ImeComposition.kt @@ -0,0 +1 @@ +../../../desktop/src/ImeComposition.kt \ No newline at end of file diff --git a/renderer/linux/test/InputMethodTest.kt b/renderer/linux/test/InputMethodTest.kt index ece2155c0..3dc521fc1 100644 --- a/renderer/linux/test/InputMethodTest.kt +++ b/renderer/linux/test/InputMethodTest.kt @@ -148,7 +148,7 @@ class InputMethodTest { /** What a person typing in English gets: the same key event it always was. */ @Test fun fr5_a_plain_ascii_key_keeps_carrying_its_character() { - val events = keyEventsFor(true, 0u, XK_a.toULong(), "a") + val events = keyEventsFor(true, 0, XK_a.toLong(), "a") assertEquals(1, events.size) assertEquals(WindowEvent.KEY_DOWN, events[0].kind) assertEquals('a'.code, events[0].codePoint) @@ -158,13 +158,13 @@ class InputMethodTest { /** A committed word has no key behind it, so it is text and only text. */ @Test fun fr5_a_commit_the_input_method_forwards_is_text_without_a_key() { - val events = keyEventsFor(true, 0u, 0UL, "한글") + val events = keyEventsFor(true, 0, 0L, "한글") assertEquals(listOf(commitKind to "한글"), events.summary()) } @Test fun fr5_a_key_with_a_non_ascii_character_is_committed_and_also_a_key() { - val events = keyEventsFor(true, 0u, XK_a.toULong(), "é") + val events = keyEventsFor(true, 0, XK_a.toLong(), "é") assertEquals(WindowEvent.KEY_DOWN, events[0].kind) assertEquals(0, events[0].codePoint, "the character is text, not part of the key") assertEquals(commitKind to "é", events[1].kind to events[1].text) @@ -172,25 +172,25 @@ class InputMethodTest { @Test fun fr5_a_control_character_is_a_key_and_never_text() { - val enter = keyEventsFor(true, 0u, XK_Return.toULong(), "\r") + val enter = keyEventsFor(true, 0, XK_Return.toLong(), "\r") assertEquals(1, enter.size) assertEquals(0, enter[0].codePoint) - val backspace = keyEventsFor(true, 0u, XK_BackSpace.toULong(), "\u0008") + val backspace = keyEventsFor(true, 0, XK_BackSpace.toLong(), "\u0008") assertEquals(1, backspace.size) assertEquals(0, backspace[0].codePoint) - val delete = keyEventsFor(true, 0u, 0UL, "\u007F") + val delete = keyEventsFor(true, 0, 0L, "\u007F") assertTrue(delete.none { it.kind == commitKind }) } @Test fun fr5_a_shortcut_never_commits_text() { - val events = keyEventsFor(true, ControlMask.toUInt(), XK_a.toULong(), "é") + val events = keyEventsFor(true, ControlMask, XK_a.toLong(), "é") assertEquals(listOf(WindowEvent.KEY_DOWN), events.map { it.kind }) } @Test fun fr5_a_release_is_a_release_and_carries_no_text() { - val events = keyEventsFor(false, ShiftMask.toUInt(), XK_a.toULong(), "A") + val events = keyEventsFor(false, ShiftMask, XK_a.toLong(), "A") assertEquals(listOf(WindowEvent.KEY_UP), events.map { it.kind }) assertEquals("", events[0].text) } diff --git a/scripts/tests/x11-key-codes.test.sh b/scripts/tests/x11-key-codes.test.sh index e7a07b6b9..037fc1fe2 100644 --- a/scripts/tests/x11-key-codes.test.sh +++ b/scripts/tests/x11-key-codes.test.sh @@ -2,9 +2,11 @@ # The X11 window used to carry its own table from a key symbol to the shared key number, in # C, beside a second one in the Kotlin/Native window. They disagreed (the Kotlin/Native one had # no letters, so control with C, V, X, Z or A arrived as an unknown key). There is one table -# now, in `renderer/desktop/src/X11Keys.kt`, and what is checked here is that the C window -# hands over the key symbol untouched and that the Kotlin side of both windows asks that table. -# The table itself is checked by `X11KeysTest`. +# now, in `renderer/desktop/src/X11Keys.kt`, and the decision from a key and its text to events, +# and the model of a composition, are `ImeComposition.kt`. What is checked here is that the C +# window hands over the key symbol, the state word, the text and the preedit edits untouched, +# and that both windows ask that code. The code itself is checked by `X11KeysTest` and +# `X11EventsTest`. set -uo pipefail repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" @@ -20,8 +22,12 @@ grep -q 'record.key_code = (int32_t)symbol;' "$c_file" \ || fail "x11_window.c no longer records the key symbol as the server named it" grep -Eq 'static int32_t dxc_key_code|static int32_t dxc_modifiers' "$c_file" \ && fail "x11_window.c carries a key or modifier table of its own again" -grep -q 'fromX11()' "$graalvm_window" \ - || fail "X11Window.kt does not turn the window's records into the shared numbering" +grep -q 'X11Events(' "$graalvm_window" \ + || fail "X11Window.kt does not turn the window's records into events with the shared code" +grep -q 'keyEventsFor(' "$repo_root/renderer/linux/src/LinuxWindow.kt" \ + || fail "the Kotlin/Native window does not decide a key's events with the shared rule" +grep -Eq 'dxc_preedit\[|dxc_preedit_length' "$c_file" \ + && fail "x11_window.c keeps a composition buffer of its own again" grep -q 'x11KeyNumber(' "$native_input" \ || fail "the Kotlin/Native window does not ask the shared X11 key table" grep -q 'x11Modifiers(' "$native_input" \ From bc23fe3a6974f38aad9a52e982702ac1610c8b0b Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 23:29:05 +0900 Subject: [PATCH 11/20] Feat: Latency tracing and synthetic input on the Kotlin/Native Linux window and the GraalVM X11 window LatencyTrace and SyntheticInput were used by the AppKit window only, and LatencyTrace formatted with String.format, which Kotlin/Native does not have. Both are portable now and shared by symlink; the Kotlin/Native Linux window and the GraalVM X11 window feed them (DXC_SYNTH=type,resize, DXC_REPORT_LATENCY), and the summary prints per-phase frame time and typed-letter-to-frame latency. The X11 C window gets a real dxc_native_debug_resize. --- renderer/desktop/c/x11_window.c | 37 ++++++--- renderer/desktop/src/AppKitWindow.kt | 3 + renderer/desktop/src/LatencyTrace.kt | 87 ++++++++++++++++++--- renderer/desktop/src/SyntheticInput.kt | 1 + renderer/desktop/src/X11Window.kt | 31 ++++++++ renderer/linux/src/JvmCompatShims.kt | 13 +++ renderer/linux/src/LinuxWindow.kt | 37 +++++++++ renderer/linux/src/shared/LatencyTrace.kt | 1 + renderer/linux/src/shared/SyntheticInput.kt | 1 + renderer/macos/src/JvmCompatShims.kt | 13 +++ 10 files changed, 202 insertions(+), 22 deletions(-) create mode 120000 renderer/linux/src/shared/LatencyTrace.kt create mode 120000 renderer/linux/src/shared/SyntheticInput.kt diff --git a/renderer/desktop/c/x11_window.c b/renderer/desktop/c/x11_window.c index 47fdf8fc4..947e27767 100644 --- a/renderer/desktop/c/x11_window.c +++ b/renderer/desktop/c/x11_window.c @@ -951,17 +951,10 @@ void dxc_native_install_menu(const char *application_name) { } /* - * Measuring aids of the AppKit window, named because the Kotlin that drives every desktop - * names them and answered here by doing nothing: a drag cannot be scripted from inside - * this window, and nothing asks for it to be. + * Measuring aids of the AppKit window that this window answers by doing nothing: a key + * cannot be posted from inside it, and nothing asks for one to be. The resize that can be + * scripted is `dxc_native_debug_resize`, below the pump it drives. */ -void dxc_native_debug_resize(void *window_pointer, void *view_pointer, int32_t from_width, - int32_t from_height, int32_t to_width, int32_t to_height, - int32_t steps, int32_t pause_micros) { - (void)window_pointer; (void)view_pointer; (void)from_width; (void)from_height; - (void)to_width; (void)to_height; (void)steps; (void)pause_micros; -} - void dxc_native_debug_key(void *window_pointer, int32_t key_code, const char *characters) { (void)window_pointer; (void)key_code; (void)characters; } @@ -1288,6 +1281,30 @@ void dxc_native_pump(double seconds) { dxc_pump_events(); } +/* + * Takes the window through the sizes a drag would, one after another, for measuring: a + * drag cannot be scripted without a hand, and what a drag does to the picture is the thing + * worth timing. Each size is asked of the server and the events it causes are read before + * the next, so the frame that belongs to a size is drawn inside the handling of it, as it + * is for a real drag. Used only when asked for, by DXC_SYNTH. + */ +void dxc_native_debug_resize(void *window_pointer, void *view_pointer, int32_t from_width, + int32_t from_height, int32_t to_width, int32_t to_height, + int32_t steps, int32_t pause_micros) { + (void)window_pointer; (void)view_pointer; + if (dxc_display == NULL || steps <= 0) return; + for (int32_t step = 1; step <= steps; step++) { + double t = (double)step / steps; + int32_t width = from_width + (int32_t)((to_width - from_width) * t); + int32_t height = from_height + (int32_t)((to_height - from_height) * t); + if (width < 1) width = 1; + if (height < 1) height = 1; + XResizeWindow(dxc_display, dxc_window, (unsigned)width, (unsigned)height); + XFlush(dxc_display); + dxc_native_pump(pause_micros / 1.0e6); + } +} + /** True once the reader has closed the window. */ int32_t dxc_native_window_closed(void) { return dxc_closed ? 1 : 0; diff --git a/renderer/desktop/src/AppKitWindow.kt b/renderer/desktop/src/AppKitWindow.kt index 7771eaaf9..5f9a5f0d2 100644 --- a/renderer/desktop/src/AppKitWindow.kt +++ b/renderer/desktop/src/AppKitWindow.kt @@ -665,6 +665,7 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { var heard = false val events = drainWindowEvents() for (event in synthetic?.due(System.nanoTime(), size) ?: emptyList()) { + if (event.kind == WindowEvent.TEXT_COMMIT) LatencyTrace.inputSent() scene.receive(event) textInput.receive(event) LatencyTrace.mark("synthetic ${event.kind} sent") @@ -730,8 +731,10 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { pumpWindowEvents(FRAME_SECONDS) LatencyTrace.mark("loop turn $frame") if (synthetic != null && synthetic.resizeDue(System.nanoTime())) { + LatencyTrace.phase = "resize" window.scriptedResize(size.width / measured.scale.toInt() to size.height / measured.scale.toInt(), 360 to 420, 60, 8_000) + LatencyTrace.phase = "idle" } synthetic?.keysDue(System.nanoTime())?.let { (code, character) -> LatencyTrace.mark("synthetic key '$character' posted") diff --git a/renderer/desktop/src/LatencyTrace.kt b/renderer/desktop/src/LatencyTrace.kt index 2e692e586..559f7a9e2 100644 --- a/renderer/desktop/src/LatencyTrace.kt +++ b/renderer/desktop/src/LatencyTrace.kt @@ -1,13 +1,22 @@ package dev.darkpyonix.composerust.ui.platform +import java.lang.System +import kotlin.math.roundToLong + /** - * Timestamps along the path from a key press to the pixels, on request. + * Timestamps along the path from a key press to the pixels, on request, and the numbers the + * parity check reads. * * Off unless `DXC_REPORT_LATENCY` is set, because it prints a line per step. Each line * carries milliseconds on the monotonic clock, so two lines can be subtracted, and the * label says which step was reached: the window heard the key, the field changed, the Host * was called, it asked for a frame, the frame was rendered, the batch applied, the pixels * drawn. + * + * Shared by every window that draws a scene, so the same run on any of them ends in the same + * lines. [summary] prints them: one `parity` line per measurement, `parity + * ` with the path left to whoever reads the log, because the window does not know which + * of the two builds it is in and a log is read next to the command that made it. */ internal object LatencyTrace { val enabled: Boolean = System.getenv("DXC_REPORT_LATENCY") != null @@ -15,29 +24,83 @@ internal object LatencyTrace { fun mark(label: String) { if (enabled) { val millis = System.nanoTime() / 1_000.0 / 1_000.0 - System.err.println("compose-rust: latency t=${"%.3f".format(millis)} $label") + System.err.println("compose-rust: latency t=${fixed(millis, 3)} $label") } } - private var frames = 0 - private var totalNanos = 0L - private var worstNanos = 0L + /** What is being measured now, so a drag of the window and a quiet window are counted apart. */ + var phase: String = "idle" + + private class Frames { + var count = 0 + var total = 0L + var worst = 0L + } + + private val byPhase = LinkedHashMap() + private var inputSentAt = 0L + private var inputSamples = 0 + private var inputTotal = 0L + private var inputWorst = 0L /** Counts one drawn frame and how long it took, for [summary]. */ fun frameDrawn(nanos: Long) { if (!enabled) return - frames++ - totalNanos += nanos - if (nanos > worstNanos) worstNanos = nanos + val frames = byPhase.getOrPut(phase) { Frames() } + frames.count++ + frames.total += nanos + if (nanos > frames.worst) frames.worst = nanos + if (inputSentAt != 0L) { + // From the input being handed to the scene to the frame it changed being drawn. + val spent = System.nanoTime() - inputSentAt + inputSentAt = 0L + inputSamples++ + inputTotal += spent + if (spent > inputWorst) inputWorst = spent + } + } + + /** Notes that a typed letter was just handed to the scene. The next frame drawn answers it. */ + fun inputSent() { + if (enabled && inputSentAt == 0L) inputSentAt = System.nanoTime() } - /** One line at the end of a run: how many frames were drawn and what each cost. */ + /** The lines at the end of a run: what each frame cost, per phase, and what an input cost. */ fun summary() { - if (enabled && frames > 0) { + if (!enabled) return + var frames = 0 + var total = 0L + var worst = 0L + for ((_, one) in byPhase) { + frames += one.count + total += one.total + if (one.worst > worst) worst = one.worst + } + if (frames > 0) { System.err.println( - "compose-rust: latency frames=$frames average=${"%.2f".format(totalNanos / frames / 1.0e6)}ms " + - "worst=${"%.2f".format(worstNanos / 1.0e6)}ms", + "compose-rust: latency frames=$frames average=${fixed(total / frames / 1.0e6, 2)}ms " + + "worst=${fixed(worst / 1.0e6, 2)}ms", + ) + } + for ((name, one) in byPhase) { + System.err.println( + "compose-rust: parity frame_${name}_avg_ms ${fixed(one.total / one.count / 1.0e6, 2)} " + + "frames=${one.count} worst_ms=${fixed(one.worst / 1.0e6, 2)}", + ) + } + if (inputSamples > 0) { + System.err.println( + "compose-rust: parity input_latency_avg_ms ${fixed(inputTotal / inputSamples / 1.0e6, 2)} " + + "samples=$inputSamples worst_ms=${fixed(inputWorst / 1.0e6, 2)}", ) } } } + +/** [value] with [digits] places after the point, which Kotlin/Native has no `format` for. */ +internal fun fixed(value: Double, digits: Int): String { + var factor = 1L + repeat(digits) { factor *= 10 } + val scaled = (value.coerceAtLeast(0.0) * factor).roundToLong() + return "${scaled / factor}.${(scaled % factor).toString().padStart(digits, '0')}" +} diff --git a/renderer/desktop/src/SyntheticInput.kt b/renderer/desktop/src/SyntheticInput.kt index 493bf017c..cc8c1b509 100644 --- a/renderer/desktop/src/SyntheticInput.kt +++ b/renderer/desktop/src/SyntheticInput.kt @@ -1,6 +1,7 @@ package dev.darkpyonix.composerust.ui.platform import androidx.compose.ui.unit.IntSize +import java.lang.System /** * Input and resizing made up from inside the window, for measuring without a hand. diff --git a/renderer/desktop/src/X11Window.kt b/renderer/desktop/src/X11Window.kt index 91e0299f2..a4a045688 100644 --- a/renderer/desktop/src/X11Window.kt +++ b/renderer/desktop/src/X11Window.kt @@ -30,6 +30,12 @@ private external fun beginFrame(window: Pointer?): Int @CFunction("dxc_native_frame_end") private external fun endFrame(display: Pointer?) +@CFunction("dxc_native_debug_resize") +private external fun debugResize( + window: Pointer?, view: Pointer?, fromWidth: Int, fromHeight: Int, toWidth: Int, + toHeight: Int, steps: Int, pauseMicros: Int, +) + class X11NativeWindow internal constructor( val window: Long, val view: Long, @@ -47,6 +53,14 @@ class X11NativeWindow internal constructor( fun beginFrame(): Boolean = beginFrame(WordFactory.pointer(window)) == 0 fun endFrame() = endFrame(WordFactory.pointer(queue)) + + /** Takes the window through the sizes a drag would, for measuring. See `DXC_SYNTH`. */ + internal fun scriptedResize(from: Pair, to: Pair, steps: Int, pauseMicros: Int) { + debugResize( + WordFactory.pointer(window), WordFactory.pointer(view), + from.first, from.second, to.first, to.second, steps, pauseMicros, + ) + } } /** @@ -132,10 +146,13 @@ internal fun runX11Window(autoExitMillis: Long? = null) { // the render target, and what the scene is told about the window it is in. var size = androidx.compose.ui.unit.IntSize(measured.width, measured.height) val textInput = NativeTextInput() + // Input and resizing made up from inside the window, for the parity check. Unset, nothing runs. + val synthetic = System.getenv("DXC_SYNTH")?.let { SyntheticInput(it) } val semantics = NativeSemantics { elements -> if (report) { System.err.println("compose-rust: the window has ${elements.size} things to say") } + synthetic?.noteElements(elements) window.describeTo(elements) } // Kept rather than left to the scene. What a scene picks for itself is the toolkit's @@ -186,8 +203,10 @@ internal fun runX11Window(autoExitMillis: Long? = null) { scene.size = fitted } size = fitted + val begun = System.nanoTime() if (drawFrame(window, context, scene, System.nanoTime() - opened, fitted)) { painted = true + LatencyTrace.frameDrawn(System.nanoTime() - begun) } } try { @@ -215,6 +234,17 @@ internal fun runX11Window(autoExitMillis: Long? = null) { work.runPending() var heard = false var drew = false + for (event in synthetic?.due(System.nanoTime(), size) ?: emptyList()) { + if (event.kind == WindowEvent.TEXT_COMMIT) LatencyTrace.inputSent() + scene.receive(event) + textInput.receive(event) + heard = true + } + if (synthetic != null && synthetic.resizeDue(System.nanoTime())) { + LatencyTrace.phase = "resize" + window.scriptedResize(size.width to size.height, 360 to 420, 60, 8_000) + LatencyTrace.phase = "idle" + } for (event in drainWindowEvents().flatMap { x11Events.heard(it) }) { if (report && event.kind != WindowEvent.POINTER_MOVE) { System.err.println("compose-rust: window heard $event") @@ -248,6 +278,7 @@ internal fun runX11Window(autoExitMillis: Long? = null) { // Before the scene closes. A resize arriving between the two would otherwise ask a // scene that has gone to draw into a context that has gone with it. clearX11FramePainter() + LatencyTrace.summary() scene.close() context.close() host.shutdown() diff --git a/renderer/linux/src/JvmCompatShims.kt b/renderer/linux/src/JvmCompatShims.kt index e44fa740c..0f1a6ee36 100644 --- a/renderer/linux/src/JvmCompatShims.kt +++ b/renderer/linux/src/JvmCompatShims.kt @@ -2,7 +2,13 @@ package java.lang +import kotlinx.cinterop.alloc +import kotlinx.cinterop.memScoped +import kotlinx.cinterop.ptr import kotlinx.cinterop.toKString +import platform.posix.CLOCK_MONOTONIC +import platform.posix.clock_gettime +import platform.posix.timespec import platform.posix.fflush import platform.posix.fputs import platform.posix.stderr @@ -42,6 +48,13 @@ object System { fun getenv(name: String): String? = platform.posix.getenv(name)?.toKString() + /** The monotonic clock in nanoseconds, which is what the JVM's call of this name answers. */ + fun nanoTime(): Long = memScoped { + val now = alloc() + clock_gettime(CLOCK_MONOTONIC, now.ptr) + now.tv_sec * 1_000_000_000L + now.tv_nsec + } + class ErrorStream internal constructor() { fun println(line: String) { fputs(line + "\n", stderr) diff --git a/renderer/linux/src/LinuxWindow.kt b/renderer/linux/src/LinuxWindow.kt index 1c1ee7faa..0c05add8e 100644 --- a/renderer/linux/src/LinuxWindow.kt +++ b/renderer/linux/src/LinuxWindow.kt @@ -66,6 +66,7 @@ import x11.NotifyWhileGrabbed import x11.SubstructureNotifyMask import x11.SubstructureRedirectMask import x11.XMapRaised +import x11.XResizeWindow import x11.XSendEvent import x11.GLXContext import x11.GLX_BLUE_SIZE @@ -207,8 +208,12 @@ internal class LinuxWindow private constructor( */ private var described: List = emptyList() + /** Input and resizing made up from inside the window, for the parity check. Unset, nothing runs. */ + private val synthetic = System.getenv("DXC_SYNTH")?.let { SyntheticInput(it) } + private val semantics = NativeSemantics { elements -> described = elements + synthetic?.noteElements(elements) if (reportFrames) { System.err.println( "compose-rust: the window holds ${described.size} things to say" + @@ -363,11 +368,13 @@ internal class LinuxWindow private constructor( scene.size = size } val nanos = monotonicNanos() - openedAt + val begun = System.nanoTime() val drew = surface.draw(size.width, size.height) { canvas -> scene.render(canvas.asComposeCanvas(), nanos) } if (drew) { painted = true + LatencyTrace.frameDrawn(System.nanoTime() - begun) // The drawing goes to the server and then the manager is told, in that order. This // is the whole of what keeps a dragged edge attached to what is inside it. sync.frameDrawn() @@ -439,6 +446,11 @@ internal class LinuxWindow private constructor( work.runPending() drained.clear() log.drain(drained) + synthetic?.due(System.nanoTime(), measured)?.forEach { event -> + if (event.kind == WindowEvent.TEXT_COMMIT) LatencyTrace.inputSent() + drained.add(event) + } + if (synthetic != null && synthetic.resizeDue(System.nanoTime())) scriptedResize() for (event in drained) { if (reportInput && event.kind != WindowEvent.POINTER_MOVE) { System.err.println("compose-rust: window heard $event") @@ -468,10 +480,31 @@ internal class LinuxWindow private constructor( updateInputMethod() } } finally { + LatencyTrace.summary() close() } } + /** + * Takes the window through the sizes a drag would, for measuring: from the size it has to + * 360 by 420 in sixty steps, each asked of the server and its events read before the next, + * so that the frame that belongs to a size is drawn inside the handling of it, as it is + * for a hand on an edge. + */ + private fun scriptedResize() { + LatencyTrace.phase = "resize" + val from = measured + for (step in 1..SYNTHETIC_RESIZE_STEPS) { + val t = step.toDouble() / SYNTHETIC_RESIZE_STEPS + val width = (from.width + (SYNTHETIC_RESIZE_WIDTH - from.width) * t).toInt().coerceAtLeast(1) + val height = (from.height + (SYNTHETIC_RESIZE_HEIGHT - from.height) * t).toInt().coerceAtLeast(1) + XResizeWindow(display, window, width.toUInt(), height.toUInt()) + XFlush(display) + pump(SYNTHETIC_RESIZE_PAUSE_MILLISECONDS) + } + LatencyTrace.phase = "idle" + } + /** * Lets the window answer for itself for a moment, and rests if it has nothing to say. * @@ -971,6 +1004,10 @@ internal class LinuxWindow private constructor( private const val DENSITY = 1.0f private const val NANOS_PER_SECOND = 1_000_000_000L + private const val SYNTHETIC_RESIZE_STEPS = 60 + private const val SYNTHETIC_RESIZE_WIDTH = 360 + private const val SYNTHETIC_RESIZE_HEIGHT = 420 + private const val SYNTHETIC_RESIZE_PAUSE_MILLISECONDS = 8 /** * A mask for the low 32 bits: the manager splits the counter value across two words. diff --git a/renderer/linux/src/shared/LatencyTrace.kt b/renderer/linux/src/shared/LatencyTrace.kt new file mode 120000 index 000000000..fe2933801 --- /dev/null +++ b/renderer/linux/src/shared/LatencyTrace.kt @@ -0,0 +1 @@ +../../../desktop/src/LatencyTrace.kt \ No newline at end of file diff --git a/renderer/linux/src/shared/SyntheticInput.kt b/renderer/linux/src/shared/SyntheticInput.kt new file mode 120000 index 000000000..beb699088 --- /dev/null +++ b/renderer/linux/src/shared/SyntheticInput.kt @@ -0,0 +1 @@ +../../../desktop/src/SyntheticInput.kt \ No newline at end of file diff --git a/renderer/macos/src/JvmCompatShims.kt b/renderer/macos/src/JvmCompatShims.kt index a977b0592..78e8eb370 100644 --- a/renderer/macos/src/JvmCompatShims.kt +++ b/renderer/macos/src/JvmCompatShims.kt @@ -2,7 +2,13 @@ package java.lang +import kotlinx.cinterop.alloc +import kotlinx.cinterop.memScoped +import kotlinx.cinterop.ptr import kotlinx.cinterop.toKString +import platform.posix.CLOCK_MONOTONIC +import platform.posix.clock_gettime +import platform.posix.timespec import platform.Foundation.NSLog import platform.posix.getenv @@ -32,6 +38,13 @@ object System { fun getenv(name: String): String? = platform.posix.getenv(name)?.toKString() + /** The monotonic clock in nanoseconds, which is what the JVM's call of this name answers. */ + fun nanoTime(): Long = memScoped { + val now = alloc() + clock_gettime(CLOCK_MONOTONIC, now.ptr) + now.tv_sec * 1_000_000_000L + now.tv_nsec + } + class ErrorStream internal constructor() { fun println(line: String) = NSLog("%s", line) } From 3a589863efcb62f0b5fa474831960bbfa9329693 Mon Sep 17 00:00:00 2001 From: BREW <81044362+b-re-w@users.noreply.github.com> Date: Sun, 4 Oct 2026 23:31:34 +0900 Subject: [PATCH 12/20] Chore: Run one parity checklist on the Kotlin/Native and GraalVM Linux windows in CI scripts/parity/linux.sh runs the same application on either path with DXC_SYNTH, presses the editing shortcuts and reads the clipboard through the display server, reads the window manager hints, and prints one table of input latency, resize frame time, shortcuts, clipboard, theme, minimum size and icon. static-renderer.yml (Kotlin/Native) and test-graalvm-renderer.yml (GraalVM) both run it and put the table in the job summary. --- .github/workflows/static-renderer.yml | 19 +++ .github/workflows/test-graalvm-renderer.yml | 25 +++ renderer/desktop/src/LatencyTrace.kt | 2 + renderer/desktop/src/SyntheticInput.kt | 16 +- renderer/desktop/src/X11Window.kt | 2 + .../desktop/test/ParityMeasurementTest.kt | 40 +++++ renderer/linux/src/LinuxWindow.kt | 5 + scripts/parity/linux.sh | 152 ++++++++++++++++++ scripts/tests/parity-linux.test.sh | 33 ++++ 9 files changed, 291 insertions(+), 3 deletions(-) create mode 100644 renderer/desktop/test/ParityMeasurementTest.kt create mode 100755 scripts/parity/linux.sh create mode 100755 scripts/tests/parity-linux.test.sh diff --git a/.github/workflows/static-renderer.yml b/.github/workflows/static-renderer.yml index b2f702dee..626aec2d3 100644 --- a/.github/workflows/static-renderer.yml +++ b/.github/workflows/static-renderer.yml @@ -203,6 +203,25 @@ jobs: # field and a button. See the script for what this proves and what it cannot. run: xvfb-run -a -s '-screen 0 1280x1024x24' ./.github/scripts/check-linux-input-access.sh renderer/build/linux "$RUNNER_TEMP/input-access" + - name: Parity checklist and measurements, Kotlin/Native path + if: inputs.target == 'linux-x64' + # The same checklist and the same measurements test-graalvm-renderer.yml runs on the + # GraalVM path, with the same application (the consumer fixture the step above + # built). The table is in the job summary; a row that fails is a warning, not a stop. + run: | + sudo apt-get install -y xclip + xvfb-run -a -s '-screen 0 1280x1024x24' ./scripts/parity/linux.sh native "$RUNNER_TEMP/parity" -- \ + "$PWD/target/linux-input-check/debug/consumer" --input-check + + - name: Keep the parity run's logs + if: always() && inputs.target == 'linux-x64' + uses: actions/upload-artifact@v4 + with: + name: parity-native-linux + path: ${{ runner.temp }}/parity/ + if-no-files-found: ignore + retention-days: 7 + - name: Keep the input and accessibility check's logs if: always() && inputs.target == 'linux-x64' uses: actions/upload-artifact@v4 diff --git a/.github/workflows/test-graalvm-renderer.yml b/.github/workflows/test-graalvm-renderer.yml index 99e07fe22..3570486ce 100644 --- a/.github/workflows/test-graalvm-renderer.yml +++ b/.github/workflows/test-graalvm-renderer.yml @@ -20,6 +20,7 @@ on: - 'renderer/**/module.yaml' - 'scripts/install-nik.sh' - 'scripts/tests/renderer/**' + - 'scripts/parity/**' - '.github/workflows/test-graalvm-renderer.yml' push: branches: [develop] @@ -30,6 +31,7 @@ on: - 'renderer/**/module.yaml' - 'scripts/install-nik.sh' - 'scripts/tests/renderer/**' + - 'scripts/parity/**' - '.github/workflows/test-graalvm-renderer.yml' workflow_dispatch: @@ -139,6 +141,29 @@ jobs: COMPOSE_RUST_AUTOEXIT_MS: '5000' run: xvfb-run -a ./renderer/desktop/scripts/smoke-test-linux.sh + - name: Parity checklist and measurements, GraalVM path + # The same checklist and the same measurements static-renderer.yml runs on the + # Kotlin/Native path, with the same application: the consumer fixture, linked to the + # renderer this job just built. The table is in the job summary; a row that fails is + # a warning, not a stop. + run: | + sudo apt-get install -y xdotool xclip x11-utils + dist="$PWD/renderer/build/native-image-linux/dist/lib" + COMPOSE_RUST_RENDERER_DIR="$dist" CARGO_TARGET_DIR="$PWD/target/parity" \ + cargo build --manifest-path compose-rust/tests/fixtures/consumer/Cargo.toml + LD_LIBRARY_PATH="$dist" xvfb-run -a -s '-screen 0 1280x1024x24' \ + ./scripts/parity/linux.sh graalvm "$RUNNER_TEMP/parity" -- \ + "$PWD/target/parity/debug/consumer" --input-check + + - name: Keep the parity run's logs + if: always() + uses: actions/upload-artifact@v4 + with: + name: parity-graalvm-linux-x64 + path: ${{ runner.temp }}/parity/ + if-no-files-found: ignore + retention-days: 7 + - name: Upload the Linux renderer for inspection uses: actions/upload-artifact@v4 with: diff --git a/renderer/desktop/src/LatencyTrace.kt b/renderer/desktop/src/LatencyTrace.kt index 559f7a9e2..b09a1030d 100644 --- a/renderer/desktop/src/LatencyTrace.kt +++ b/renderer/desktop/src/LatencyTrace.kt @@ -2,6 +2,7 @@ package dev.darkpyonix.composerust.ui.platform import java.lang.System import kotlin.math.roundToLong +import org.jetbrains.skiko.currentSystemTheme /** * Timestamps along the path from a key press to the pixels, on request, and the numbers the @@ -68,6 +69,7 @@ internal object LatencyTrace { /** The lines at the end of a run: what each frame cost, per phase, and what an input cost. */ fun summary() { if (!enabled) return + System.err.println("compose-rust: parity system_theme ${currentSystemTheme.name.lowercase()}") var frames = 0 var total = 0L var worst = 0L diff --git a/renderer/desktop/src/SyntheticInput.kt b/renderer/desktop/src/SyntheticInput.kt index cc8c1b509..a7c773e99 100644 --- a/renderer/desktop/src/SyntheticInput.kt +++ b/renderer/desktop/src/SyntheticInput.kt @@ -9,15 +9,21 @@ import java.lang.System * A session with no accessibility permission cannot post a click, type a letter or drag * an edge, and the two things worth timing here are exactly those: how long a typed letter * takes to show, and what a drag does to the picture. Asked for with `DXC_SYNTH`, a - * comma separated list of `type` and `resize`; unset, nothing here runs. + * comma separated list of `type`, `resize` and `exit`; unset, nothing here runs. * * `type` presses on the first text field the window describes, then commits one letter * every 700 ms from two seconds in. `resize` takes the window from its size to 360x420 in - * 60 steps, six seconds in, from a single call that holds the thread as a drag does. + * 60 steps, six seconds in (`DXC_SYNTH_RESIZE_AT` seconds, where set), from a single call + * that holds the thread as a drag does. `exit` ends the window two seconds after the resize + * (`DXC_SYNTH_EXIT_AT` seconds in, where set), so a run measures and finishes by itself and + * the summary at the end of the loop is printed. */ internal class SyntheticInput(what: String) { private val wantsType = "type" in what.split(',') private val wantsResize = "resize" in what.split(',') + private val wantsExit = "exit" in what.split(',') + private val resizeAt = System.getenv("DXC_SYNTH_RESIZE_AT")?.toDoubleOrNull() ?: 6.0 + private val exitAt = System.getenv("DXC_SYNTH_EXIT_AT")?.toDoubleOrNull() ?: (resizeAt + 2.0) private val started = System.nanoTime() private var field: AccessibleElement? = null private var clicked = false @@ -67,11 +73,15 @@ internal class SyntheticInput(what: String) { /** True once, when it is time to take the window through its sizes. */ fun resizeDue(now: Long): Boolean { - if (!wantsResize || resized || seconds(now) < 6.0) return false + if (!wantsResize || resized || seconds(now) < resizeAt) return false resized = true return true } + /** True once the run has done what it was asked to and should close the window. */ + fun exitDue(now: Long): Boolean = + wantsExit && (!wantsResize || resized) && seconds(now) >= exitAt + /** Remembers where the field is, and says when the echo line has caught up. */ fun noteElements(elements: List) { if (field == null) field = elements.firstOrNull { it.role == ElementRole.FIELD } diff --git a/renderer/desktop/src/X11Window.kt b/renderer/desktop/src/X11Window.kt index a4a045688..c929352ec 100644 --- a/renderer/desktop/src/X11Window.kt +++ b/renderer/desktop/src/X11Window.kt @@ -216,6 +216,7 @@ internal fun runX11Window(autoExitMillis: Long? = null) { // nothing of a scene that has closed. setX11FramePainter { frames.draw() } while (!isWindowClosed()) { + if (synthetic != null && synthetic.exitDue(System.nanoTime())) break if (autoExitMillis != null && (System.nanoTime() - started) / NANOS_PER_MILLI >= autoExitMillis ) { @@ -236,6 +237,7 @@ internal fun runX11Window(autoExitMillis: Long? = null) { var drew = false for (event in synthetic?.due(System.nanoTime(), size) ?: emptyList()) { if (event.kind == WindowEvent.TEXT_COMMIT) LatencyTrace.inputSent() + LatencyTrace.mark("synthetic ${event.kind} sent") scene.receive(event) textInput.receive(event) heard = true diff --git a/renderer/desktop/test/ParityMeasurementTest.kt b/renderer/desktop/test/ParityMeasurementTest.kt new file mode 100644 index 000000000..ef2dd6509 --- /dev/null +++ b/renderer/desktop/test/ParityMeasurementTest.kt @@ -0,0 +1,40 @@ +package dev.darkpyonix.composerust.test + +import dev.darkpyonix.composerust.ui.platform.SyntheticInput +import dev.darkpyonix.composerust.ui.platform.fixed +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +/** + * The pieces of the parity run that are plain logic: how a number is printed, which Kotlin/Native + * has no `format` for, and when the synthetic input does each thing. + */ +class ParityMeasurementTest { + @Test + fun a_number_is_printed_with_the_places_asked_for() { + assertEquals("1.23", fixed(1.2345, 2)) + assertEquals("0.050", fixed(0.05, 3)) + assertEquals("12.00", fixed(12.0, 2)) + assertEquals("0.00", fixed(0.0, 2)) + } + + @Test + fun a_number_below_zero_is_printed_as_zero() { + assertEquals("0.00", fixed(-3.0, 2)) + } + + @Test + fun the_run_exits_only_when_asked_to_and_after_the_resize() { + val quiet = SyntheticInput("type") + assertFalse(quiet.exitDue(System.nanoTime() + 60_000_000_000L), "no exit was asked for") + + val measuring = SyntheticInput("resize,exit") + val later = System.nanoTime() + 60_000_000_000L + assertFalse(measuring.exitDue(later), "the resize has not happened yet") + assertTrue(measuring.resizeDue(later)) + assertTrue(measuring.exitDue(later), "the resize is done and the time has come") + assertFalse(measuring.resizeDue(later), "a resize happens once") + } +} diff --git a/renderer/linux/src/LinuxWindow.kt b/renderer/linux/src/LinuxWindow.kt index 0c05add8e..3bdcf6efd 100644 --- a/renderer/linux/src/LinuxWindow.kt +++ b/renderer/linux/src/LinuxWindow.kt @@ -448,9 +448,14 @@ internal class LinuxWindow private constructor( log.drain(drained) synthetic?.due(System.nanoTime(), measured)?.forEach { event -> if (event.kind == WindowEvent.TEXT_COMMIT) LatencyTrace.inputSent() + LatencyTrace.mark("synthetic ${event.kind} sent") drained.add(event) } if (synthetic != null && synthetic.resizeDue(System.nanoTime())) scriptedResize() + if (synthetic != null && synthetic.exitDue(System.nanoTime())) { + closed = true + continue + } for (event in drained) { if (reportInput && event.kind != WindowEvent.POINTER_MOVE) { System.err.println("compose-rust: window heard $event") diff --git a/scripts/parity/linux.sh b/scripts/parity/linux.sh new file mode 100755 index 000000000..c18ec6aae --- /dev/null +++ b/scripts/parity/linux.sh @@ -0,0 +1,152 @@ +#!/usr/bin/env bash +# Usage: scripts/parity/linux.sh