From 3009767ed640dea9fcd1cdfd346d18ca17424e51 Mon Sep 17 00:00:00 2001 From: ceski <56656010+ceski-1@users.noreply.github.com> Date: Wed, 12 Feb 2025 13:10:37 -0800 Subject: [PATCH] Prevent accidentally disabling gamepad when it's being used --- src/i_gamepad.c | 2 +- src/i_gamepad.h | 2 +- src/i_input.c | 14 +++++++++++++- src/i_input.h | 2 +- src/mn_setup.c | 2 +- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/i_gamepad.c b/src/i_gamepad.c index 3da01d261..e3cec48ad 100644 --- a/src/i_gamepad.c +++ b/src/i_gamepad.c @@ -46,7 +46,7 @@ enum }; static boolean joy_enable; -int joy_device; +int joy_device, last_joy_device; joy_platform_t joy_platform; static int joy_stick_layout; static int joy_forward_sensitivity; diff --git a/src/i_gamepad.h b/src/i_gamepad.h index 24db28cf2..5700c8416 100644 --- a/src/i_gamepad.h +++ b/src/i_gamepad.h @@ -70,7 +70,7 @@ typedef struct axes_s float outer_deadzone; // Normalized outer deadzone. } axes_t; -extern int joy_device; // Gamepad device. +extern int joy_device, last_joy_device; // Gamepad device. extern joy_platform_t joy_platform; // Gamepad platform (button names). extern boolean joy_invert_forward; // Invert forward axis. extern boolean joy_invert_strafe; // Invert strafe axis. diff --git a/src/i_input.c b/src/i_input.c index 91647c6e9..cc10c2d19 100644 --- a/src/i_input.c +++ b/src/i_input.c @@ -496,6 +496,7 @@ void I_InitGamepad(void) MN_UpdateAllGamepadItems(); } + last_joy_device = joy_device; SDL_FlushEvents(SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEREMOVED); } @@ -510,6 +511,7 @@ static boolean CheckActiveGamepad(void) if (SDL_JoystickGetDeviceInstanceID(i) == gamepad_instance_id) { joy_device = i + 1; + last_joy_device = joy_device; MN_UpdateAllGamepadItems(); return true; } @@ -530,6 +532,7 @@ static void CloseGamepad(void) gamepad = NULL; gamepad_instance_id = -1; joy_device = 0; + last_joy_device = joy_device; DisableGamepadEvents(); UpdatePlatform(); I_ResetGamepad(); @@ -555,6 +558,7 @@ void I_OpenGamepad(int device_index) gamepad_instance_id = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamepad)); joy_device = device_index + 1; + last_joy_device = joy_device; I_SetRumbleSupported(gamepad); I_ResetAllRumbleChannels(); I_ResetGamepad(); @@ -601,8 +605,16 @@ void I_CloseGamepad(SDL_JoystickID instance_id) MN_UpdateAllGamepadItems(); } -void I_UpdateGamepadDevice(void) +void I_UpdateGamepadDevice(boolean gamepad_input) { + if (gamepad_input && joy_device == 0) + { + // Prevent accidentally disabling gamepad when it's being used. + joy_device = last_joy_device; + return; + } + + last_joy_device = joy_device; const int device_index = joy_device - 1; CloseGamepad(); diff --git a/src/i_input.h b/src/i_input.h index 836cba370..0eaf6af43 100644 --- a/src/i_input.h +++ b/src/i_input.h @@ -34,7 +34,7 @@ void I_SetSensorsEnabled(boolean condition); void I_InitGamepad(void); void I_OpenGamepad(int device_index); void I_CloseGamepad(SDL_JoystickID instance_id); -void I_UpdateGamepadDevice(void); +void I_UpdateGamepadDevice(boolean gamepad_input); const char **I_GamepadDeviceList(void); boolean I_GamepadDevices(void); diff --git a/src/mn_setup.c b/src/mn_setup.c index ee0abe48b..b6636cd5c 100644 --- a/src/mn_setup.c +++ b/src/mn_setup.c @@ -2903,7 +2903,7 @@ static void MN_Gyro(void); static void UpdateGamepadDevice(void) { - I_UpdateGamepadDevice(); + I_UpdateGamepadDevice(menu_input == pad_mode); } static setup_menu_t gen_settings4[] = {