Skip to content

Commit f87cb88

Browse files
committed
Fix raw input mouse position after window state changes
1 parent abc7ea3 commit f87cb88

1 file changed

Lines changed: 52 additions & 12 deletions

File tree

input/drivers/winraw_input.c

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

99100
/* TODO/FIXME - static globals */
100101
static winraw_mouse_t *g_mice = NULL;
102+
103+
/* Sync internal mouse coordinates with the OS cursor position.
104+
* Used after events such as window mode, size, and focus changes */
105+
static bool winraw_sync_mouse_to_cursor(winraw_input_t *wr)
106+
{
107+
HWND wnd;
108+
POINT p;
109+
unsigned i;
110+
111+
if (!wr || !wr->mouse_cnt)
112+
return false;
113+
114+
wnd = (HWND)video_driver_window_get();
115+
if (!wnd || !GetCursorPos(&p))
116+
return false;
117+
118+
ScreenToClient(wnd, &p);
119+
120+
for (i = 0; i < wr->mouse_cnt; ++i)
121+
{
122+
g_mice[i].x = (LONG)p.x;
123+
g_mice[i].y = (LONG)p.y;
124+
}
125+
126+
return true;
127+
}
101128
static bool winraw_focus = false;
102129

103130
#define WINRAW_KEYBOARD_PRESSED(wr, key) (wr->kb_keys[rarch_keysym_lut[(enum retro_key)(key)]])
@@ -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)