From 777e442595347a668d06d54d9f9c784e83bf6a21 Mon Sep 17 00:00:00 2001 From: bones_was_here Date: Fri, 6 Oct 2023 04:44:37 +1000 Subject: [PATCH] input: stop capturing mouse movement if window is hidden without first losing focus Fixes players looking all around after they alt+tabbed. Moves the logic for selecting the mouse mode out of screen drawing and into SDL event handling where it's unaffected if we early-out of drawing the frame. Prevents mouse "grab" (raw/relative mode + cursor hiding) in a few "loading" situations. Signed-off-by: bones_was_here --- cl_screen.c | 10 +--------- vid.h | 4 +++- vid_null.c | 4 ---- vid_sdl.c | 17 +++++++++++++---- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/cl_screen.c b/cl_screen.c index e4ba43d9..6435b576 100644 --- a/cl_screen.c +++ b/cl_screen.c @@ -100,7 +100,7 @@ int jpeg_supported = false; qbool scr_initialized; // ready to draw -static qbool scr_loading = false; // we are in a loading screen +qbool scr_loading = false; // we are in a loading screen unsigned int scr_con_current; static unsigned int scr_con_margin_bottom; @@ -2325,14 +2325,6 @@ void CL_UpdateScreen(void) #endif qglFlush(); // ensure that the commands are submitted to the GPU before we do other things - - if (!vid_activewindow || key_consoleactive) - VID_SetMouse(false, false); - else if (key_dest == key_menu || key_dest == key_menu_grabbed || scr_loading) - VID_SetMouse(vid_mouse.integer && !in_client_mouse && !vid_touchscreen.integer, !vid_touchscreen.integer); - else - VID_SetMouse(vid_mouse.integer && !cl.csqc_wantsmousemove && cl_prydoncursor.integer <= 0 && (!cls.demoplayback || cl_demo_mousegrab.integer) && !vid_touchscreen.integer, !vid_touchscreen.integer); - VID_Finish(); } diff --git a/vid.h b/vid.h index 816a6935..cab74969 100644 --- a/vid.h +++ b/vid.h @@ -133,6 +133,9 @@ int VID_Shared_SetJoystick(int index); qbool VID_JoyBlockEmulatedKeys(int keycode); void VID_EnableJoystick(qbool enable); +extern cvar_t cl_demo_mousegrab; +extern qbool scr_loading; + extern qbool vid_hidden; extern qbool vid_activewindow; extern qbool vid_supportrefreshrate; @@ -231,7 +234,6 @@ qbool VID_HasScreenKeyboardSupport(void); void VID_ShowKeyboard(qbool show); qbool VID_ShowingKeyboard(void); -void VID_SetMouse(qbool relative, qbool hidecursor); void VID_Finish (void); void VID_Restart_f(struct cmd_state_s *cmd); diff --git a/vid_null.c b/vid_null.c index 5b2d869c..fb3cba08 100644 --- a/vid_null.c +++ b/vid_null.c @@ -51,10 +51,6 @@ static void InitSig(void) } #endif -void VID_SetMouse(qbool relative, qbool hidecursor) -{ -} - void VID_Finish (void) { } diff --git a/vid_sdl.c b/vid_sdl.c index 7f33a50f..5c7721a9 100644 --- a/vid_sdl.c +++ b/vid_sdl.c @@ -368,7 +368,7 @@ qbool VID_ShowingKeyboard(void) return SDL_IsTextInputActive() != 0; } -void VID_SetMouse(qbool relative, qbool hidecursor) +static void VID_SetMouse(qbool relative, qbool hidecursor) { #ifndef DP_MOBILETOUCH #ifdef MACOSX @@ -1013,6 +1013,8 @@ void IN_Move( void ) in_windowmouse_y = y; } + //Con_Printf("Mouse position: in_mouse %f %f in_windowmouse %f %f\n", in_mouse_x, in_mouse_y, in_windowmouse_x, in_windowmouse_y); + VID_BuildJoyState(&joystate); VID_ApplyJoyState(&joystate); } @@ -1348,8 +1350,10 @@ void Sys_SDL_HandleEvents(void) break; } + vid_activewindow = !vid_hidden && vid_hasfocus; + // enable/disable sound on focus gain/loss - if ((!vid_hidden && vid_activewindow) || !snd_mutewhenidle.integer) + if (vid_activewindow || !snd_mutewhenidle.integer) { if (!sound_active) { @@ -1365,6 +1369,13 @@ void Sys_SDL_HandleEvents(void) sound_active = false; } } + + if (!vid_activewindow || key_consoleactive || scr_loading) + VID_SetMouse(false, false); + else if (key_dest == key_menu || key_dest == key_menu_grabbed) + VID_SetMouse(vid_mouse.integer && !in_client_mouse && !vid_touchscreen.integer, !vid_touchscreen.integer); + else + VID_SetMouse(vid_mouse.integer && !cl.csqc_wantsmousemove && cl_prydoncursor.integer <= 0 && (!cls.demoplayback || cl_demo_mousegrab.integer) && !vid_touchscreen.integer, !vid_touchscreen.integer); } ///////////////// @@ -1825,8 +1836,6 @@ void VID_Shutdown (void) void VID_Finish (void) { - vid_activewindow = !vid_hidden && vid_hasfocus; - VID_UpdateGamma(); if (!vid_hidden) -- 2.39.2