Skip to content

Commit

Permalink
Prevent accidentally disabling gamepad when it's being used
Browse files Browse the repository at this point in the history
  • Loading branch information
ceski-1 committed Feb 12, 2025
1 parent 02355c8 commit 3009767
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/i_gamepad.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/i_gamepad.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 13 additions & 1 deletion src/i_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ void I_InitGamepad(void)
MN_UpdateAllGamepadItems();
}

last_joy_device = joy_device;
SDL_FlushEvents(SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEREMOVED);
}

Expand All @@ -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;
}
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/i_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/mn_setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand Down

0 comments on commit 3009767

Please sign in to comment.