From 0a3520130ba087ce8f110cd9914f38e039bf3253 Mon Sep 17 00:00:00 2001 From: Tony Jansson Date: Wed, 28 Oct 2020 16:13:31 +0200 Subject: [PATCH] (X11) Mouse grab redux --- input/drivers/x11_input.c | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/input/drivers/x11_input.c b/input/drivers/x11_input.c index 9965d5d127..583fe618ad 100644 --- a/input/drivers/x11_input.c +++ b/input/drivers/x11_input.c @@ -39,6 +39,7 @@ typedef struct x11_input int mouse_x, mouse_y; int mouse_last_x, mouse_last_y; + bool mouse_grabbed; char state[32]; bool mouse_l, mouse_r, mouse_m; } x11_input_t; @@ -664,6 +665,44 @@ static void x_input_poll(void *data) x11->mouse_l = mask & Button1Mask; x11->mouse_m = mask & Button2Mask; x11->mouse_r = mask & Button3Mask; + + /* Major grab kludge required to circumvent + * absolute pointer area limitation + * AND to be able to use mouse in menu + */ + if (x11->mouse_grabbed && video_has_focus) + { + int new_x = win_x, new_y = win_y; + int margin = 0; + float margin_pct = 0.05f; + struct video_viewport vp; + + video_driver_get_viewport_info(&vp); + + margin = ((vp.full_height < vp.full_width) ? vp.full_height : vp.full_width) * margin_pct; + + if (win_x + 1 > vp.full_width - margin) + new_x = vp.full_width - margin; + else if (win_x + 1 < margin) + new_x = margin; + + if (win_y + 1 > vp.full_height - margin) + new_y = vp.full_height - margin; + else if (win_y + 1 < margin) + new_y = margin; + + if (new_x != win_x || new_y != win_y) + { + XWarpPointer(x11->display, None, x11->win, + 0, 0, 0 ,0, + new_x, new_y); + + XSync(x11->display, False); + } + + x11->mouse_last_x = new_x + x11->mouse_last_x - x11->mouse_x; + x11->mouse_last_y = new_y + x11->mouse_last_y - x11->mouse_y; + } } } @@ -673,6 +712,8 @@ static void x_grab_mouse(void *data, bool state) if (!x11) return; + x11->mouse_grabbed = state; + if (state) { XGrabPointer(x11->display, x11->win, False,