fix(hook): align cursor overlay with GDI screen coordinates
This commit is contained in:
1 parent
a698c54c66
commit
55eaf98e75
5 files changed
+148
-44
No files matched your search
@@ -20,6 +20,8 @@ jobs:
|
|||||||
cargo test --locked --all-targets
|
cargo test --locked --all-targets
|
||||||
cc -std=c11 -O2 -Wall -Wextra -Werror tests/frame-protocol.c -o /tmp/frame-protocol
|
cc -std=c11 -O2 -Wall -Wextra -Werror tests/frame-protocol.c -o /tmp/frame-protocol
|
||||||
/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
|
- name: Build
|
||||||
run: cargo build --locked --release
|
run: cargo build --locked --release
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
#ifndef UUR_CURSOR_OVERLAY_H
|
||||||
|
#define UUR_CURSOR_OVERLAY_H
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
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
|
||||||
+22
-44
@@ -26,6 +26,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "cursor-overlay.h"
|
||||||
#include "frame-protocol.h"
|
#include "frame-protocol.h"
|
||||||
|
|
||||||
#define UUR_MAGIC 0x50495555u
|
#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;
|
CURSORINFO cursor;
|
||||||
ICONINFO icon;
|
ICONINFO icon;
|
||||||
|
BITMAP mask;
|
||||||
POINT position;
|
POINT position;
|
||||||
int virtual_x;
|
uur_cursor_rect source = {source_x, source_y, source_width, source_height};
|
||||||
int virtual_y;
|
uur_cursor_rect destination = {x, y, width, height};
|
||||||
int cursor_x;
|
uur_cursor_rect overlay;
|
||||||
int cursor_y;
|
uur_cursor_shape shape;
|
||||||
int output_x;
|
|
||||||
int output_y;
|
|
||||||
int cursor_width;
|
|
||||||
int cursor_height;
|
|
||||||
int hotspot_x;
|
|
||||||
int hotspot_y;
|
|
||||||
|
|
||||||
if (hdc_dest == NULL || width <= 0 || height <= 0 || source_width <= 0 ||
|
if (hdc_dest == NULL || width <= 0 || height <= 0 || source_width <= 0 ||
|
||||||
source_height <= 0 || source_is_screen_dc(hdc_dest))
|
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)
|
if (cursor.hCursor == NULL)
|
||||||
return;
|
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));
|
ZeroMemory(&icon, sizeof(icon));
|
||||||
if (!GetIconInfo(cursor.hCursor, &icon))
|
if (!GetIconInfo(cursor.hCursor, &icon))
|
||||||
return;
|
return;
|
||||||
hotspot_x = (int)(((int64_t)icon.xHotspot * width + source_width / 2) /
|
|
||||||
source_width);
|
/* GetIconInfo's mask holds the cursor's actual bitmap dimensions. For a
|
||||||
hotspot_y = (int)(((int64_t)icon.yHotspot * height + source_height / 2) /
|
* monochrome cursor the AND/XOR masks are stacked vertically. */
|
||||||
source_height);
|
if (icon.hbmMask != NULL &&
|
||||||
cursor_width = (int)(((int64_t)cursor_width * width + source_width / 2) /
|
GetObjectW(icon.hbmMask, sizeof(mask), &mask) == sizeof(mask)) {
|
||||||
source_width);
|
shape.x = position.x;
|
||||||
cursor_height = (int)(((int64_t)cursor_height * height + source_height / 2) /
|
shape.y = position.y;
|
||||||
source_height);
|
shape.width = mask.bmWidth;
|
||||||
if (cursor_width < 1)
|
shape.height = uur_cursor_bitmap_height(mask.bmHeight,
|
||||||
cursor_width = 1;
|
icon.hbmColor != NULL);
|
||||||
if (cursor_height < 1)
|
shape.hotspot_x = (int)icon.xHotspot;
|
||||||
cursor_height = 1;
|
shape.hotspot_y = (int)icon.yHotspot;
|
||||||
DrawIconEx(hdc_dest, output_x - hotspot_x, output_y - hotspot_y,
|
if (uur_map_cursor_overlay(&source, &destination, &shape, &overlay))
|
||||||
cursor.hCursor, cursor_width, cursor_height, 0, NULL, DI_NORMAL);
|
DrawIconEx(hdc_dest, overlay.x, overlay.y, cursor.hCursor,
|
||||||
|
overlay.width, overlay.height, 0, NULL, DI_NORMAL);
|
||||||
|
}
|
||||||
if (icon.hbmColor != NULL)
|
if (icon.hbmColor != NULL)
|
||||||
DeleteObject(icon.hbmColor);
|
DeleteObject(icon.hbmColor);
|
||||||
if (icon.hbmMask != NULL)
|
if (icon.hbmMask != NULL)
|
||||||
|
|||||||
@@ -12,5 +12,8 @@ cargo test --manifest-path "$project_root/Cargo.toml" --all-targets
|
|||||||
cc -std=c11 -O2 -Wall -Wextra -Werror \
|
cc -std=c11 -O2 -Wall -Wextra -Werror \
|
||||||
"$project_root/tests/frame-protocol.c" -o "$temporary/frame-protocol"
|
"$project_root/tests/frame-protocol.c" -o "$temporary/frame-protocol"
|
||||||
"$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/hook/build.sh"
|
||||||
"$project_root/capture/build.sh"
|
"$project_root/capture/build.sh"
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user