Skip to content

Commit 67231fe

Browse files
authored
Fix raw input mouse position after window state changes (#18586)
Co-authored-by: PoloniumRain <[email protected]>
1 parent d4d40a7 commit 67231fe

File tree

1 file changed

+52
-12
lines changed

1 file changed

+52
-12
lines changed

input/drivers/winraw_input.c

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,39 @@ typedef struct
9494
unsigned mouse_cnt;
9595
uint8_t kb_keys[SC_LAST];
9696
uint8_t flags;
97+
bool last_focus;
9798
} winraw_input_t;
9899

99100
/* TODO/FIXME - static globals */
100101
static winraw_mouse_t *g_mice = NULL;
101102
static bool winraw_focus = false;
102103

104+
/* Sync internal mouse coordinates with the OS cursor position.
105+
* Used after events such as window mode, size, and focus changes */
106+
static bool winraw_sync_mouse_to_cursor(winraw_input_t *wr)
107+
{
108+
HWND wnd;
109+
POINT p;
110+
unsigned i;
111+
112+
if (!wr || !wr->mouse_cnt)
113+
return false;
114+
115+
wnd = (HWND)video_driver_window_get();
116+
if (!wnd || !GetCursorPos(&p))
117+
return false;
118+
119+
ScreenToClient(wnd, &p);
120+
121+
for (i = 0; i < wr->mouse_cnt; ++i)
122+
{
123+
g_mice[i].x = (LONG)p.x;
124+
g_mice[i].y = (LONG)p.y;
125+
}
126+
127+
return true;
128+
}
129+
103130
#define WINRAW_KEYBOARD_PRESSED(wr, key) (wr->kb_keys[rarch_keysym_lut[(enum retro_key)(key)]])
104131

105132
static HWND winraw_create_window(WNDPROC wnd_proc)
@@ -290,24 +317,31 @@ static bool winraw_mouse_button_pressed(
290317
static void winraw_init_mouse_xy_mapping(winraw_input_t *wr)
291318
{
292319
struct video_viewport viewport;
320+
int mouse_x;
321+
int mouse_y;
322+
unsigned i;
293323

294-
if (video_driver_get_viewport_info(&viewport))
295-
{
296-
unsigned i;
297-
int center_x = viewport.x + viewport.width / 2;
298-
int center_y = viewport.y + viewport.height / 2;
324+
if (!video_driver_get_viewport_info(&viewport))
325+
return;
299326

300-
for (i = 0; i < wr->mouse_cnt; ++i)
327+
/* Default fallback: center of the viewport */
328+
mouse_x = viewport.x + viewport.width / 2;
329+
mouse_y = viewport.y + viewport.height / 2;
330+
331+
/* Sync to OS cursor position; fall back to center if it fails */
332+
if (!winraw_sync_mouse_to_cursor(wr))
333+
{
334+
for (i = 0; i < wr->mouse_cnt; i++)
301335
{
302-
g_mice[i].x = center_x;
303-
g_mice[i].y = center_y;
336+
g_mice[i].x = mouse_x;
337+
g_mice[i].y = mouse_y;
304338
}
339+
}
305340

306-
wr->view_abs_ratio_x = (double)viewport.full_width / 65535.0f;
307-
wr->view_abs_ratio_y = (double)viewport.full_height / 65535.0f;
341+
wr->view_abs_ratio_x = (double)viewport.full_width / 65535.0;
342+
wr->view_abs_ratio_y = (double)viewport.full_height / 65535.0;
308343

309-
wr->flags |= WRAW_INP_FLG_MOUSE_XY_MAPPING_READY;
310-
}
344+
wr->flags |= WRAW_INP_FLG_MOUSE_XY_MAPPING_READY;
311345
}
312346

313347
static void winraw_update_mouse_state(winraw_input_t *wr,
@@ -635,6 +669,12 @@ static void winraw_poll(void *data)
635669
unsigned i;
636670
winraw_input_t *wr = (winraw_input_t*)data;
637671

672+
/* Sync coordinates when window regains focus */
673+
if (winraw_focus && !wr->last_focus)
674+
winraw_sync_mouse_to_cursor(wr);
675+
676+
wr->last_focus = winraw_focus;
677+
638678
for (i = 0; i < wr->mouse_cnt; ++i)
639679
{
640680
/* Clear buttons when not focused */

0 commit comments

Comments
 (0)