From 920b00348ef5bc565703c3d22f7a06071a74815a Mon Sep 17 00:00:00 2001 From: Eidolon Date: Sun, 17 Aug 2025 16:55:16 -0500 Subject: [PATCH] Make the mouse cursor sane --- src/dedicated/i_video.c | 2 - src/m_menudefs.c | 3 - src/screen.h | 2 - src/sdl/i_system.cpp | 4 +- src/sdl/i_video.c | 170 ++++++++++------------------------------ src/sdl/sdlmain.h | 2 - 6 files changed, 41 insertions(+), 142 deletions(-) diff --git a/src/dedicated/i_video.c b/src/dedicated/i_video.c index e2a712995..5d1fd9b17 100644 --- a/src/dedicated/i_video.c +++ b/src/dedicated/i_video.c @@ -35,8 +35,6 @@ consvar_t cv_vidwait = {"vid_wait", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL static CV_PossibleValue_t keyboardlayout_cons_t[] = {{1,"Default US"}, {2, "Native"}, {3, "AZERTY"}, {0, NULL}}; consvar_t cv_keyboardlayout = {"keyboardlayout", "Default US", CV_SAVE, keyboardlayout_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}; -consvar_t cv_alwaysgrabmouse = {"alwaysgrabmouse", "Off", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; - INT32 numcontrollers = 0; UINT8 graphics_started = 0; // Is used in console.c and screen.c diff --git a/src/m_menudefs.c b/src/m_menudefs.c index 9f20b5f91..f9d1e0ea9 100644 --- a/src/m_menudefs.c +++ b/src/m_menudefs.c @@ -1186,8 +1186,6 @@ static menuitem_t OP_FocusOptionsMenu[] = {IT_STRING|IT_CVAR, NULL, "Background FPS Cap", &cv_fpscapbg, 80}, {IT_STRING|IT_CVAR, NULL, "Show \"FOCUS LOST\"", &cv_showfocuslost, 100}, - - {IT_STRING|IT_CVAR, NULL, "Always Grab Mouse While Focused", &cv_alwaysgrabmouse, 120}, }; static const char* OP_FocusOptionsTooltips[] = @@ -1198,7 +1196,6 @@ static const char* OP_FocusOptionsTooltips[] = "Should the game pause while the game is unfocused?", "Set manual framerate cap while the game is unfocused.", "Should the FOCUS LOST window appear\n while the game is unfocused?", - "Should the mouse cursor be grabbed and hidden\n while the game is in focus?", }; static menuitem_t OP_DataOptionsMenu[] = diff --git a/src/screen.h b/src/screen.h index 42bc6c144..7cde5a6dc 100644 --- a/src/screen.h +++ b/src/screen.h @@ -95,8 +95,6 @@ extern consvar_t cv_highreshudscale; extern consvar_t cv_vidwait; extern consvar_t cv_timescale; -extern consvar_t cv_alwaysgrabmouse; - extern consvar_t cv_votebgscaling; void R_SetColumnFunc(size_t id); diff --git a/src/sdl/i_system.cpp b/src/sdl/i_system.cpp index 033783d4c..b002eeb19 100644 --- a/src/sdl/i_system.cpp +++ b/src/sdl/i_system.cpp @@ -1962,7 +1962,7 @@ void I_Quit(void) /* prevent recursive I_Quit() */ if (quiting) goto death; - SDLforceUngrabMouse(); + SDL_ShowCursor(SDL_TRUE); quiting = SDL_FALSE; I_ShutdownConsole(); M_SaveConfig(NULL); //save game config, cvars.. @@ -2041,8 +2041,6 @@ void I_Error(const char *error, ...) if (shutdowning) { errorcount++; - if (errorcount == 1) - SDLforceUngrabMouse(); // try to shutdown each subsystem separately if (errorcount == 2) I_ShutdownMusic(); diff --git a/src/sdl/i_video.c b/src/sdl/i_video.c index 34413cf2b..684dc4d00 100644 --- a/src/sdl/i_video.c +++ b/src/sdl/i_video.c @@ -140,9 +140,6 @@ static void I_CheckDesktopRes(void); consvar_t cv_vidwait = {"vid_wait", "Off", CV_SAVE|CV_CALL|CV_NOINIT, CV_OnOff, Impl_SetVsync, 0, NULL, NULL, 0, 0, NULL}; static consvar_t cv_stretch = {"stretch", "Off", CV_SAVE|CV_NOSHOWHELP, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; -static void mousegrabOnChange(void); -consvar_t cv_alwaysgrabmouse = {"alwaysgrabmouse", "Off", CV_SAVE|CV_CALL, CV_OnOff, mousegrabOnChange, 0, NULL, NULL, 0, 0, NULL}; - // these cant be used since config is read after window creation, so need to use command line parameter instead //static CV_PossibleValue_t msaa_cons_t[] = {{0, "Off"}, {2, "2X"}, {4, "4X"}, {8, "8X"}, {16, "16X"}, {0, NULL}}; //consvar_t cv_msaa = {"msaa", "Off", CV_SAVE, msaa_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}; @@ -170,7 +167,6 @@ static UINT32 localPalette[256]; Uint16 realwidth = BASEVIDWIDTH; Uint16 realheight = BASEVIDHEIGHT; static SDL_bool mousegrabok = SDL_TRUE; -static SDL_bool wrapmouseok = SDL_FALSE; #define HalfWarpMouse(x,y) if (wrapmouseok) SDL_WarpMouseInWindow(window, (Uint16)(x/2),(Uint16)(y/2)) static SDL_bool exposevideo = SDL_FALSE; static SDL_bool usesdl2soft = SDL_FALSE; @@ -218,9 +214,6 @@ static void Impl_VideoSetupBuffer(void); static SDL_bool Impl_CreateWindow(SDL_bool fullscreen); static void Impl_SetWindowIcon(void); -static void SDLdoGrabMouse(void); -static void SDLdoUngrabMouse(void); - #ifdef USE_FBO_OGL boolean downsample = false; void RefreshOGLSDLSurface(void) @@ -230,23 +223,6 @@ void RefreshOGLSDLSurface(void) } #endif -static void mousegrabOnChange(void) -{ - static SDL_bool firsttimeonmouse = SDL_TRUE; - - if (!firsttimeonmouse) - { - HalfWarpMouse(realwidth, realheight); // warp to center - } - else - firsttimeonmouse = SDL_FALSE; - - if (cv_usemouse.value || cv_alwaysgrabmouse.value) - SDLdoGrabMouse(); - else - SDLdoUngrabMouse(); -} - static INT32 Impl_SDL_Scancode_To_Keycode(SDL_Scancode code) { if (code >= SDL_SCANCODE_A && code <= SDL_SCANCODE_Z) @@ -448,34 +424,6 @@ static INT32 Impl_SDL_Keysym_To_Keycode(SDL_Keysym keysym) return Impl_SDL_Scancode_To_Keycode(scancode); } -static void SDLdoGrabMouse(void) -{ - SDL_ShowCursor(SDL_DISABLE); - SDL_SetWindowGrab(window, SDL_TRUE); - - if (SDL_SetRelativeMouseMode(SDL_TRUE) == 0) // already warps mouse if successful - wrapmouseok = SDL_TRUE; // TODO: is wrapmouseok or HalfWarpMouse needed anymore? -} - -static void SDLdoUngrabMouse(void) -{ - SDL_ShowCursor(SDL_ENABLE); - SDL_SetWindowGrab(window, SDL_FALSE); - wrapmouseok = SDL_FALSE; - SDL_SetRelativeMouseMode(SDL_FALSE); -} - -void SDLforceUngrabMouse(void) -{ - if (SDL_WasInit(SDL_INIT_VIDEO)==SDL_INIT_VIDEO && window != NULL) - { - SDL_ShowCursor(SDL_ENABLE); - SDL_SetWindowGrab(window, SDL_FALSE); - wrapmouseok = SDL_FALSE; - SDL_SetRelativeMouseMode(SDL_FALSE); - } -} - static void VID_Command_NumModes_f (void) { CONS_Printf(M_GetText("%d video mode(s) available(s)\n"), VID_NumModes()); @@ -651,10 +599,12 @@ static void Impl_HandleWindowEvent(SDL_WindowEvent evt) case SDL_WINDOWEVENT_FOCUS_GAINED: kbfocus = SDL_TRUE; mousefocus = SDL_TRUE; + SDL_ShowCursor(SDL_FALSE); break; case SDL_WINDOWEVENT_FOCUS_LOST: kbfocus = SDL_FALSE; mousefocus = SDL_FALSE; + SDL_ShowCursor(SDL_TRUE); break; case SDL_WINDOWEVENT_MAXIMIZED: break; @@ -701,17 +651,7 @@ static void Impl_HandleWindowEvent(SDL_WindowEvent evt) if (!cv_playsoundifunfocused.value) S_StopSounds(); - if (!disable_mouse) - { - SDLforceUngrabMouse(); - } - memset(gamekeydown, 0, NUMKEYS); // TODO this is a scary memset - - if (MOUSE_MENU) - { - SDLdoUngrabMouse(); - } } #undef FOCUSUNION } @@ -751,58 +691,48 @@ static void Impl_HandleKeyboardEvent(SDL_KeyboardEvent evt, Uint32 type) static void Impl_HandleMouseMotionEvent(SDL_MouseMotionEvent evt) { - if (USE_MOUSEINPUT) + if (!USE_MOUSEINPUT) + return; + + const boolean windowinfocus = (SDL_GetMouseFocus() == window && SDL_GetKeyboardFocus() == window); + + if (!windowinfocus) { - const boolean windowinfocus = (SDL_GetMouseFocus() == window && SDL_GetKeyboardFocus() == window); + SDL_ShowCursor(SDL_ENABLE); + SDL_SetWindowGrab(window, SDL_FALSE); + SDL_SetRelativeMouseMode(SDL_FALSE); + return; + } - if (!windowinfocus) - { - SDLdoUngrabMouse(); - return; - } - - // If using relative mouse mode, don't post an event_t just now, - // add on the offsets so we can make an overall event later. - if (SDL_GetRelativeMouseMode()) - { - if (windowinfocus) - { - mousemovex += evt.xrel; - mousemovey += -evt.yrel; - SDL_SetWindowGrab(window, SDL_TRUE); - } - return; - } - - // If the event is from warping the pointer to middle - // of the screen then ignore it. - if ((evt.x == realwidth/2) && (evt.y == realheight/2)) - { - return; - } - - // Don't send an event_t if not in relative mouse mode anymore, - // just grab and set relative mode - // this fixes the stupid camera jerk on mouse entering bug - // -- Monster Iestyn + // If using relative mouse mode, don't post an event_t just now, + // add on the offsets so we can make an overall event later. + if (SDL_GetRelativeMouseMode()) + { if (windowinfocus) { - SDLdoGrabMouse(); + mousemovex += evt.xrel; + mousemovey += -evt.yrel; + SDL_SetWindowGrab(window, SDL_TRUE); } + return; } - else if (cv_alwaysgrabmouse.value) - { - const boolean windowinfocus = (SDL_GetMouseFocus() == window && SDL_GetKeyboardFocus() == window); - if (!windowinfocus) - { - SDLdoUngrabMouse(); - return; - } - else if (windowinfocus) - { - SDLdoGrabMouse(); - } + // If the event is from warping the pointer to middle + // of the screen then ignore it. + if ((evt.x == realwidth/2) && (evt.y == realheight/2)) + { + return; + } + + // Don't send an event_t if not in relative mouse mode anymore, + // just grab and set relative mode + // this fixes the stupid camera jerk on mouse entering bug + // -- Monster Iestyn + if (windowinfocus) + { + SDL_ShowCursor(SDL_DISABLE); + SDL_SetWindowGrab(window, SDL_TRUE); + SDL_SetRelativeMouseMode(SDL_TRUE); } } @@ -1248,22 +1178,12 @@ void I_GetEvent(void) void I_StartupMouse(void) { - static SDL_bool firsttimeonmouse = SDL_TRUE; - if (disable_mouse) return; - if (!firsttimeonmouse) - { - HalfWarpMouse(realwidth, realheight); // warp to center - } - else - firsttimeonmouse = SDL_FALSE; - - if (cv_usemouse.value || cv_alwaysgrabmouse.value) - SDLdoGrabMouse(); - else - SDLdoUngrabMouse(); + SDL_ShowCursor(SDL_FALSE); + SDL_SetWindowGrab(window, SDL_FALSE); + SDL_SetRelativeMouseMode(SDL_FALSE); } // @@ -1585,8 +1505,6 @@ static UINT32 VID_GetRefreshRate(void) INT32 VID_SetMode(INT32 modeNum) { - SDLdoUngrabMouse(); - vid.recalc = true; if (modeNum >= 0 && modeNum < MAXWINMODES) @@ -1793,7 +1711,6 @@ void I_StartupGraphics(void) COM_AddCommand ("vid_mode", VID_Command_Mode_f); CV_RegisterVar (&cv_vidwait); CV_RegisterVar (&cv_stretch); - CV_RegisterVar (&cv_alwaysgrabmouse); disable_mouse = M_CheckParm("-nomouse"); disable_fullscreen = M_CheckParm("-win") ? 1 : 0; @@ -1990,15 +1907,8 @@ void I_StartupGraphics(void) realwidth = (Uint16)vid.width; realheight = (Uint16)vid.height; - SDLdoUngrabMouse(); - SDL_RaiseWindow(window); - if (mousegrabok && !disable_mouse) - { - SDLdoGrabMouse(); - } - graphics_started = true; } diff --git a/src/sdl/sdlmain.h b/src/sdl/sdlmain.h index 0c5bfaeff..ac204ea99 100644 --- a/src/sdl/sdlmain.h +++ b/src/sdl/sdlmain.h @@ -83,8 +83,6 @@ void I_UpdateJoystickDeviceIndices(UINT8 excludePlayer); void I_GetConsoleEvents(void); -void SDLforceUngrabMouse(void); - // Needed for some WIN32 functions extern SDL_Window *window;