From 19f043744a134cf7972feddae412fc9117bdb2bd Mon Sep 17 00:00:00 2001 From: Oskari Alaranta Date: Mon, 1 Jun 2026 21:32:14 +0300 Subject: [PATCH] Allow querying global cursor position If a platform implements this, e.g. xeyes can track cursor when the window is not receiving mouse movement events --- xbanan/Base.cpp | 18 ++++++++++++++---- xbanan/Platform.h | 2 ++ xbanan/SDL3/sdl3.cpp | 9 +++++++++ xbanan/banan-os/banan-os.cpp | 7 +++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/xbanan/Base.cpp b/xbanan/Base.cpp index 3c84389..9c08ebc 100644 --- a/xbanan/Base.cpp +++ b/xbanan/Base.cpp @@ -1967,6 +1967,16 @@ BAN::ErrorOr handle_packet(Client& client_info, BAN::ConstByteSpan packet) } const auto [root_x, root_y] = get_window_position(wid); + + int32_t root_cursor_x, root_cursor_y; + if (g_platform_ops.query_pointer) + g_platform_ops.query_pointer(&root_cursor_x, &root_cursor_y); + else + { + root_cursor_x = root_x + window.cursor_x; + root_cursor_y = root_y + window.cursor_y; + } + xQueryPointerReply reply { .type = X_Reply, .sameScreen = xTrue, @@ -1974,10 +1984,10 @@ BAN::ErrorOr handle_packet(Client& client_info, BAN::ConstByteSpan packet) .length = 0, .root = g_root.windowId, .child = static_cast(child_wid), - .rootX = static_cast(root_x + window.cursor_x), - .rootY = static_cast(root_y + window.cursor_y), - .winX = static_cast(window.cursor_x), - .winY = static_cast(window.cursor_y), + .rootX = static_cast(root_cursor_x), + .rootY = static_cast(root_cursor_y), + .winX = static_cast(root_cursor_x - root_x), + .winY = static_cast(root_cursor_y - root_y), .mask = static_cast(g_keymask | g_butmask), }; TRY(encode(client_info.output_buffer, reply)); diff --git a/xbanan/Platform.h b/xbanan/Platform.h index caca7b9..e018b1b 100644 --- a/xbanan/Platform.h +++ b/xbanan/Platform.h @@ -61,6 +61,8 @@ struct PlatformOps void (*request_fullscreen)(PlatformWindow*, bool fullscreen); /* Warp pointer */ void (*warp_pointer)(int32_t x, int32_t y, bool relative); + /* Get global position of the pointer */ + void (*query_pointer)(int32_t* x, int32_t* y); /* Create a system cursor */ BAN::ErrorOr> (*create_system_cursor)(SystemCursorType); /* Create cursor from custom bitmap */ diff --git a/xbanan/SDL3/sdl3.cpp b/xbanan/SDL3/sdl3.cpp index a55e540..1dfc3fd 100644 --- a/xbanan/SDL3/sdl3.cpp +++ b/xbanan/SDL3/sdl3.cpp @@ -348,6 +348,14 @@ static void sdl3_warp_pointer(int32_t x, int32_t y, bool relative) SDL_WarpMouseGlobal(x, y); } +static void sdl3_query_pointer(int32_t* x, int32_t* y) +{ + float cx, cy; + SDL_GetGlobalMouseState(&cx, &cy); + *x = cx; + *y = cy; +} + static BAN::ErrorOr> sdl3_create_system_cursor(SystemCursorType type) { static constexpr SDL_SystemCursor cursor_type_map[] { @@ -428,6 +436,7 @@ PlatformOps g_platform_ops = { .request_reposition = sdl3_request_reposition, .request_fullscreen = sdl3_request_fullscreen, .warp_pointer = sdl3_warp_pointer, + .query_pointer = sdl3_query_pointer, .create_system_cursor = sdl3_create_system_cursor, .create_bitmap_cursor = sdl3_create_bitmap_cursor, .set_cursor = sdl3_set_cursor, diff --git a/xbanan/banan-os/banan-os.cpp b/xbanan/banan-os/banan-os.cpp index 99d24db..77d0f15 100644 --- a/xbanan/banan-os/banan-os.cpp +++ b/xbanan/banan-os/banan-os.cpp @@ -154,6 +154,12 @@ static void bananos_warp_pointer(int32_t x, int32_t y, bool relative) (void)relative; } +static void bananos_query_pointer(int32_t* x, int32_t* y) +{ + (void)x; + (void)y; +} + static BAN::ErrorOr> bananos_create_system_cursor(SystemCursorType type) { (void)type; @@ -198,6 +204,7 @@ PlatformOps g_platform_ops = { .request_reposition = bananos_request_reposition, .request_fullscreen = bananos_request_fullscreen, .warp_pointer = bananos_warp_pointer, + .query_pointer = nullptr, /* bananos_query_pointer */ .create_system_cursor = bananos_create_system_cursor, .create_bitmap_cursor = bananos_create_bitmap_cursor, .set_cursor = bananos_set_cursor,