From 55eaf98e75a2f7deb185464eeabfddb33c9f0835 Mon Sep 17 00:00:00 2001 From: Xuc Pan Date: Mon, 21 Sep 2026 21:14:05 +0800 Subject: [PATCH] fix(hook): align cursor overlay with GDI screen coordinates --- .github/workflows/ci.yml | 2 + hook/cursor-overlay.h | 83 ++++++++++++++++++++++++++++++++++++++++ hook/uur-hook.c | 66 +++++++++++--------------------- scripts/check.sh | 3 ++ tests/cursor-overlay.c | 38 ++++++++++++++++++ 5 files changed, 148 insertions(+), 44 deletions(-) create mode 100644 hook/cursor-overlay.h create mode 100644 tests/cursor-overlay.c diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3b053aa..613d220 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,6 +20,8 @@ jobs: cargo test --locked --all-targets cc -std=c11 -O2 -Wall -Wextra -Werror tests/frame-protocol.c -o /tmp/frame-protocol /tmp/frame-protocol + cc -std=c11 -O2 -Wall -Wextra -Werror tests/cursor-overlay.c -o /tmp/cursor-overlay + /tmp/cursor-overlay - name: Build run: cargo build --locked --release diff --git a/hook/cursor-overlay.h b/hook/cursor-overlay.h new file mode 100644 index 0000000..06626f2 --- /dev/null +++ b/hook/cursor-overlay.h @@ -0,0 +1,83 @@ +#ifndef UUR_CURSOR_OVERLAY_H +#define UUR_CURSOR_OVERLAY_H + +#include +#include + +typedef struct uur_cursor_rect { + int x; + int y; + int width; + int height; +} uur_cursor_rect; + +typedef struct uur_cursor_shape { + /* The hotspot is in the same screen coordinates as the GDI source DC. */ + int x; + int y; + int width; + int height; + int hotspot_x; + int hotspot_y; +} uur_cursor_shape; + +static inline int uur_cursor_bitmap_height(int mask_height, int has_color) +{ + return has_color ? mask_height : mask_height / 2; +} + +static inline int uur_map_cursor_overlay(const uur_cursor_rect *source, + const uur_cursor_rect *destination, + const uur_cursor_shape *cursor, + uur_cursor_rect *overlay) +{ + int64_t left; + int64_t top; + int64_t scaled_hotspot_x; + int64_t scaled_hotspot_y; + int64_t scaled_width; + int64_t scaled_height; + int64_t mapped_x; + int64_t mapped_y; + + if (source->width <= 0 || source->height <= 0 || + destination->width <= 0 || destination->height <= 0 || + cursor->width <= 0 || cursor->height <= 0) + return 0; + + left = (int64_t)cursor->x - cursor->hotspot_x; + top = (int64_t)cursor->y - cursor->hotspot_y; + /* Keep partially visible cursors at the edges of the captured region. */ + if (left >= (int64_t)source->x + source->width || + top >= (int64_t)source->y + source->height || + left + cursor->width <= source->x || + top + cursor->height <= source->y) + return 0; + + scaled_hotspot_x = ((int64_t)cursor->hotspot_x * destination->width + + source->width / 2) / source->width; + scaled_hotspot_y = ((int64_t)cursor->hotspot_y * destination->height + + source->height / 2) / source->height; + scaled_width = ((int64_t)cursor->width * destination->width + + source->width / 2) / source->width; + scaled_height = ((int64_t)cursor->height * destination->height + + source->height / 2) / source->height; + + mapped_x = (int64_t)destination->x + + (((int64_t)cursor->x - source->x) * destination->width) / + source->width - scaled_hotspot_x; + mapped_y = (int64_t)destination->y + + (((int64_t)cursor->y - source->y) * destination->height) / + source->height - scaled_hotspot_y; + if (mapped_x < INT_MIN || mapped_x > INT_MAX || mapped_y < INT_MIN || + mapped_y > INT_MAX || scaled_width > INT_MAX || + scaled_height > INT_MAX) + return 0; + overlay->x = (int)mapped_x; + overlay->y = (int)mapped_y; + overlay->width = scaled_width > 0 ? (int)scaled_width : 1; + overlay->height = scaled_height > 0 ? (int)scaled_height : 1; + return 1; +} + +#endif diff --git a/hook/uur-hook.c b/hook/uur-hook.c index e32aecb..5f90e05 100644 --- a/hook/uur-hook.c +++ b/hook/uur-hook.c @@ -26,6 +26,7 @@ #include #include #include +#include "cursor-overlay.h" #include "frame-protocol.h" #define UUR_MAGIC 0x50495555u @@ -337,17 +338,12 @@ static void draw_cursor_overlay(HDC hdc_dest, int x, int y, int width, int heigh { CURSORINFO cursor; ICONINFO icon; + BITMAP mask; POINT position; - int virtual_x; - int virtual_y; - int cursor_x; - int cursor_y; - int output_x; - int output_y; - int cursor_width; - int cursor_height; - int hotspot_x; - int hotspot_y; + uur_cursor_rect source = {source_x, source_y, source_width, source_height}; + uur_cursor_rect destination = {x, y, width, height}; + uur_cursor_rect overlay; + uur_cursor_shape shape; if (hdc_dest == NULL || width <= 0 || height <= 0 || source_width <= 0 || source_height <= 0 || source_is_screen_dc(hdc_dest)) @@ -365,43 +361,25 @@ static void draw_cursor_overlay(HDC hdc_dest, int x, int y, int width, int heigh if (cursor.hCursor == NULL) return; - virtual_x = GetSystemMetrics(SM_XVIRTUALSCREEN); - virtual_y = GetSystemMetrics(SM_YVIRTUALSCREEN); - cursor_x = position.x - virtual_x; - cursor_y = position.y - virtual_y; - if (cursor_x < source_x || cursor_y < source_y || - cursor_x >= source_x + source_width || - cursor_y >= source_y + source_height) - return; - - output_x = x + (int)(((int64_t)(cursor_x - source_x) * width) / - source_width); - output_y = y + (int)(((int64_t)(cursor_y - source_y) * height) / - source_height); - cursor_width = GetSystemMetrics(SM_CXCURSOR); - cursor_height = GetSystemMetrics(SM_CYCURSOR); - if (cursor_width < 1) - cursor_width = 32; - if (cursor_height < 1) - cursor_height = 32; - ZeroMemory(&icon, sizeof(icon)); if (!GetIconInfo(cursor.hCursor, &icon)) return; - hotspot_x = (int)(((int64_t)icon.xHotspot * width + source_width / 2) / - source_width); - hotspot_y = (int)(((int64_t)icon.yHotspot * height + source_height / 2) / - source_height); - cursor_width = (int)(((int64_t)cursor_width * width + source_width / 2) / - source_width); - cursor_height = (int)(((int64_t)cursor_height * height + source_height / 2) / - source_height); - if (cursor_width < 1) - cursor_width = 1; - if (cursor_height < 1) - cursor_height = 1; - DrawIconEx(hdc_dest, output_x - hotspot_x, output_y - hotspot_y, - cursor.hCursor, cursor_width, cursor_height, 0, NULL, DI_NORMAL); + + /* GetIconInfo's mask holds the cursor's actual bitmap dimensions. For a + * monochrome cursor the AND/XOR masks are stacked vertically. */ + if (icon.hbmMask != NULL && + GetObjectW(icon.hbmMask, sizeof(mask), &mask) == sizeof(mask)) { + shape.x = position.x; + shape.y = position.y; + shape.width = mask.bmWidth; + shape.height = uur_cursor_bitmap_height(mask.bmHeight, + icon.hbmColor != NULL); + shape.hotspot_x = (int)icon.xHotspot; + shape.hotspot_y = (int)icon.yHotspot; + if (uur_map_cursor_overlay(&source, &destination, &shape, &overlay)) + DrawIconEx(hdc_dest, overlay.x, overlay.y, cursor.hCursor, + overlay.width, overlay.height, 0, NULL, DI_NORMAL); + } if (icon.hbmColor != NULL) DeleteObject(icon.hbmColor); if (icon.hbmMask != NULL) diff --git a/scripts/check.sh b/scripts/check.sh index cd7860c..17f7dcc 100755 --- a/scripts/check.sh +++ b/scripts/check.sh @@ -12,5 +12,8 @@ cargo test --manifest-path "$project_root/Cargo.toml" --all-targets cc -std=c11 -O2 -Wall -Wextra -Werror \ "$project_root/tests/frame-protocol.c" -o "$temporary/frame-protocol" "$temporary/frame-protocol" +cc -std=c11 -O2 -Wall -Wextra -Werror \ + "$project_root/tests/cursor-overlay.c" -o "$temporary/cursor-overlay" +"$temporary/cursor-overlay" "$project_root/hook/build.sh" "$project_root/capture/build.sh" diff --git a/tests/cursor-overlay.c b/tests/cursor-overlay.c new file mode 100644 index 0000000..85c0258 --- /dev/null +++ b/tests/cursor-overlay.c @@ -0,0 +1,38 @@ +#include + +#include "../hook/cursor-overlay.h" + +int main(void) +{ + uur_cursor_rect source = {0, 0, 1920, 1080}; + uur_cursor_rect destination = {0, 0, 1920, 1080}; + uur_cursor_rect overlay; + uur_cursor_shape cursor = {100, 200, 64, 48, 16, 8}; + + /* A negative virtual-screen origin must not shift a primary-screen blit. */ + assert(uur_map_cursor_overlay(&source, &destination, &cursor, &overlay)); + assert(overlay.x == 84 && overlay.y == 192); + assert(overlay.width == 64 && overlay.height == 48); + + source.x = -1920; + cursor.x = -1820; + assert(uur_map_cursor_overlay(&source, &destination, &cursor, &overlay)); + assert(overlay.x == 84); + + /* StretchBlt scales the cursor bitmap and hotspot together. */ + destination.width = 960; + destination.height = 540; + assert(uur_map_cursor_overlay(&source, &destination, &cursor, &overlay)); + assert(overlay.x == 42 && overlay.y == 96); + assert(overlay.width == 32 && overlay.height == 24); + + /* A cursor overlapping the capture edge remains partially visible. */ + cursor.x = -1925; + assert(uur_map_cursor_overlay(&source, &destination, &cursor, &overlay)); + cursor.x = -2000; + assert(!uur_map_cursor_overlay(&source, &destination, &cursor, &overlay)); + + assert(uur_cursor_bitmap_height(48, 1) == 48); + assert(uur_cursor_bitmap_height(96, 0) == 48); + return 0; +}