Skip to content

Commit 31ff088

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

1 file changed

Lines changed: 53 additions & 12 deletions

File tree

input/drivers/winraw_input.c

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,38 @@ typedef struct
9494
unsigned mouse_cnt;
9595
uint8_t kb_keys[SC_LAST];
9696
uint8_t flags;
97+
bool winraw_focus;
98+
bool last_focus;
9799
} winraw_input_t;
98100

99101
/* TODO/FIXME - static globals */
100102
static winraw_mouse_t *g_mice = NULL;
103+
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+
}
101129
static bool winraw_focus = false;
102130

103131
#define WINRAW_KEYBOARD_PRESSED(wr, key) (wr->kb_keys[rarch_keysym_lut[(enum retro_key)(key)]])
@@ -290,24 +318,31 @@ static bool winraw_mouse_button_pressed(
290318
static void winraw_init_mouse_xy_mapping(winraw_input_t *wr)
291319
{
292320
struct video_viewport viewport;
321+
int mouse_x;
322+
int mouse_y;
323+
unsigned i;
293324

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;
325+
if (!video_driver_get_viewport_info(&viewport))
326+
return;
299327

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

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

309-
wr->flags |= WRAW_INP_FLG_MOUSE_XY_MAPPING_READY;
310-
}
345+
wr->flags |= WRAW_INP_FLG_MOUSE_XY_MAPPING_READY;
311346
}
312347

313348
static void winraw_update_mouse_state(winraw_input_t *wr,
@@ -635,6 +670,12 @@ static void winraw_poll(void *data)
635670
unsigned i;
636671
winraw_input_t *wr = (winraw_input_t*)data;
637672

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

0 commit comments

Comments
 (0)