Skip to content

Commit 10336b7

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

1 file changed

Lines changed: 50 additions & 11 deletions

File tree

input/drivers/winraw_input.c

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,31 @@ typedef struct
9898

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

103128
#define WINRAW_KEYBOARD_PRESSED(wr, key) (wr->kb_keys[rarch_keysym_lut[(enum retro_key)(key)]])
@@ -291,23 +316,29 @@ static void winraw_init_mouse_xy_mapping(winraw_input_t *wr)
291316
{
292317
struct video_viewport viewport;
293318

294-
if (video_driver_get_viewport_info(&viewport))
319+
if (!video_driver_get_viewport_info(&viewport))
295320
{
296-
unsigned i;
297-
int center_x = viewport.x + viewport.width / 2;
298-
int center_y = viewport.y + viewport.height / 2;
321+
return;
322+
}
323+
324+
/* Default fallback: center of the viewport */
325+
int mouse_x = viewport.x + viewport.width / 2;
326+
int mouse_y = viewport.y + viewport.height / 2;
299327

300-
for (i = 0; i < wr->mouse_cnt; ++i)
328+
/* Sync to OS cursor position; fall back to center if it fails */
329+
if (!winraw_sync_mouse_to_cursor(wr))
330+
{
331+
for (unsigned i = 0; i < wr->mouse_cnt; ++i)
301332
{
302-
g_mice[i].x = center_x;
303-
g_mice[i].y = center_y;
333+
g_mice[i].x = mouse_x;
334+
g_mice[i].y = mouse_y;
304335
}
336+
}
305337

306-
wr->view_abs_ratio_x = (double)viewport.full_width / 65535.0f;
307-
wr->view_abs_ratio_y = (double)viewport.full_height / 65535.0f;
338+
wr->view_abs_ratio_x = (double)viewport.full_width / 65535.0;
339+
wr->view_abs_ratio_y = (double)viewport.full_height / 65535.0;
308340

309-
wr->flags |= WRAW_INP_FLG_MOUSE_XY_MAPPING_READY;
310-
}
341+
wr->flags |= WRAW_INP_FLG_MOUSE_XY_MAPPING_READY;
311342
}
312343

313344
static void winraw_update_mouse_state(winraw_input_t *wr,
@@ -635,6 +666,14 @@ static void winraw_poll(void *data)
635666
unsigned i;
636667
winraw_input_t *wr = (winraw_input_t*)data;
637668

669+
/* Sync coordinates when window regains focus */
670+
static bool last_focus = false;
671+
672+
if (winraw_focus && !last_focus)
673+
winraw_sync_mouse_to_cursor(wr);
674+
675+
last_focus = winraw_focus;
676+
638677
for (i = 0; i < wr->mouse_cnt; ++i)
639678
{
640679
/* Clear buttons when not focused */

0 commit comments

Comments
 (0)