Always lock the HIDAPI device when closing, in case rumble is pending

This commit is contained in:
Sam Lantinga 2021-01-14 23:49:20 -08:00
parent 907b8eebc3
commit e2f46ed8ef
9 changed files with 103 additions and 59 deletions

View File

@ -193,6 +193,8 @@ HIDAPI_DriverGameCube_InitDevice(SDL_HIDAPI_Device *device)
return SDL_TRUE; return SDL_TRUE;
error: error:
SDL_LockMutex(device->dev_lock);
{
if (device->dev) { if (device->dev) {
hid_close(device->dev); hid_close(device->dev);
device->dev = NULL; device->dev = NULL;
@ -201,6 +203,9 @@ error:
SDL_free(device->context); SDL_free(device->context);
device->context = NULL; device->context = NULL;
} }
}
SDL_UnlockMutex(device->dev_lock);
return SDL_FALSE; return SDL_FALSE;
} }
@ -404,15 +409,19 @@ HIDAPI_DriverGameCube_FreeDevice(SDL_HIDAPI_Device *device)
{ {
SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context; SDL_DriverGameCube_Context *ctx = (SDL_DriverGameCube_Context *)device->context;
hid_close(device->dev);
device->dev = NULL;
SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_USE_BUTTON_LABELS, SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_USE_BUTTON_LABELS,
SDL_GameControllerButtonReportingHintChanged, ctx); SDL_GameControllerButtonReportingHintChanged, ctx);
SDL_LockMutex(device->dev_lock);
{
hid_close(device->dev);
device->dev = NULL;
SDL_free(device->context); SDL_free(device->context);
device->context = NULL; device->context = NULL;
} }
SDL_UnlockMutex(device->dev_lock);
}
SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube = SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverGameCube =
{ {

View File

@ -890,12 +890,16 @@ HIDAPI_DriverPS4_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick
SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE,
SDL_PS4RumbleHintChanged, ctx); SDL_PS4RumbleHintChanged, ctx);
SDL_LockMutex(device->dev_lock);
{
hid_close(device->dev); hid_close(device->dev);
device->dev = NULL; device->dev = NULL;
SDL_free(device->context); SDL_free(device->context);
device->context = NULL; device->context = NULL;
} }
SDL_UnlockMutex(device->dev_lock);
}
static void static void
HIDAPI_DriverPS4_FreeDevice(SDL_HIDAPI_Device *device) HIDAPI_DriverPS4_FreeDevice(SDL_HIDAPI_Device *device)

View File

@ -1045,12 +1045,16 @@ HIDAPI_DriverPS5_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick
SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED, SDL_DelHintCallback(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED,
SDL_PS5PlayerLEDHintChanged, ctx); SDL_PS5PlayerLEDHintChanged, ctx);
SDL_LockMutex(device->dev_lock);
{
hid_close(device->dev); hid_close(device->dev);
device->dev = NULL; device->dev = NULL;
SDL_free(device->context); SDL_free(device->context);
device->context = NULL; device->context = NULL;
} }
SDL_UnlockMutex(device->dev_lock);
}
static void static void
HIDAPI_DriverPS5_FreeDevice(SDL_HIDAPI_Device *device) HIDAPI_DriverPS5_FreeDevice(SDL_HIDAPI_Device *device)

View File

@ -1016,6 +1016,8 @@ HIDAPI_DriverSteam_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystic
return SDL_TRUE; return SDL_TRUE;
error: error:
SDL_LockMutex(device->dev_lock);
{
if (device->dev) { if (device->dev) {
hid_close(device->dev); hid_close(device->dev);
device->dev = NULL; device->dev = NULL;
@ -1024,6 +1026,8 @@ error:
SDL_free(device->context); SDL_free(device->context);
device->context = NULL; device->context = NULL;
} }
}
SDL_UnlockMutex(device->dev_lock);
return SDL_FALSE; return SDL_FALSE;
} }
@ -1169,14 +1173,19 @@ HIDAPI_DriverSteam_UpdateDevice(SDL_HIDAPI_Device *device)
static void static void
HIDAPI_DriverSteam_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick) HIDAPI_DriverSteam_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
SDL_LockMutex(device->dev_lock);
{ {
CloseSteamController(device->dev); CloseSteamController(device->dev);
hid_close(device->dev); hid_close(device->dev);
device->dev = NULL; device->dev = NULL;
SDL_free(device->context); SDL_free(device->context);
device->context = NULL; device->context = NULL;
} }
SDL_UnlockMutex(device->dev_lock);
}
static void static void
HIDAPI_DriverSteam_FreeDevice(SDL_HIDAPI_Device *device) HIDAPI_DriverSteam_FreeDevice(SDL_HIDAPI_Device *device)

View File

@ -827,6 +827,8 @@ HIDAPI_DriverSwitch_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joysti
return SDL_TRUE; return SDL_TRUE;
error: error:
SDL_LockMutex(device->dev_lock);
{
if (device->dev) { if (device->dev) {
hid_close(device->dev); hid_close(device->dev);
device->dev = NULL; device->dev = NULL;
@ -835,6 +837,8 @@ error:
SDL_free(device->context); SDL_free(device->context);
device->context = NULL; device->context = NULL;
} }
}
SDL_UnlockMutex(device->dev_lock);
return SDL_FALSE; return SDL_FALSE;
} }
@ -1353,12 +1357,16 @@ HIDAPI_DriverSwitch_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joyst
SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_USE_BUTTON_LABELS, SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_USE_BUTTON_LABELS,
SDL_GameControllerButtonReportingHintChanged, ctx); SDL_GameControllerButtonReportingHintChanged, ctx);
SDL_LockMutex(device->dev_lock);
{
hid_close(device->dev); hid_close(device->dev);
device->dev = NULL; device->dev = NULL;
SDL_free(device->context); SDL_free(device->context);
device->context = NULL; device->context = NULL;
} }
SDL_UnlockMutex(device->dev_lock);
}
static void static void
HIDAPI_DriverSwitch_FreeDevice(SDL_HIDAPI_Device *device) HIDAPI_DriverSwitch_FreeDevice(SDL_HIDAPI_Device *device)

View File

@ -323,6 +323,8 @@ HIDAPI_DriverXbox360_UpdateDevice(SDL_HIDAPI_Device *device)
static void static void
HIDAPI_DriverXbox360_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick) HIDAPI_DriverXbox360_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
SDL_LockMutex(device->dev_lock);
{ {
if (device->dev) { if (device->dev) {
hid_close(device->dev); hid_close(device->dev);
@ -332,6 +334,8 @@ HIDAPI_DriverXbox360_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joys
SDL_free(device->context); SDL_free(device->context);
device->context = NULL; device->context = NULL;
} }
SDL_UnlockMutex(device->dev_lock);
}
static void static void
HIDAPI_DriverXbox360_FreeDevice(SDL_HIDAPI_Device *device) HIDAPI_DriverXbox360_FreeDevice(SDL_HIDAPI_Device *device)

View File

@ -311,6 +311,8 @@ HIDAPI_DriverXbox360W_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joy
static void static void
HIDAPI_DriverXbox360W_FreeDevice(SDL_HIDAPI_Device *device) HIDAPI_DriverXbox360W_FreeDevice(SDL_HIDAPI_Device *device)
{
SDL_LockMutex(device->dev_lock);
{ {
hid_close(device->dev); hid_close(device->dev);
device->dev = NULL; device->dev = NULL;
@ -318,6 +320,8 @@ HIDAPI_DriverXbox360W_FreeDevice(SDL_HIDAPI_Device *device)
SDL_free(device->context); SDL_free(device->context);
device->context = NULL; device->context = NULL;
} }
SDL_UnlockMutex(device->dev_lock);
}
SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W = SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverXbox360W =
{ {

View File

@ -1057,6 +1057,8 @@ HIDAPI_DriverXboxOne_UpdateDevice(SDL_HIDAPI_Device *device)
static void static void
HIDAPI_DriverXboxOne_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick) HIDAPI_DriverXboxOne_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
{
SDL_LockMutex(device->dev_lock);
{ {
hid_close(device->dev); hid_close(device->dev);
device->dev = NULL; device->dev = NULL;
@ -1064,6 +1066,8 @@ HIDAPI_DriverXboxOne_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joys
SDL_free(device->context); SDL_free(device->context);
device->context = NULL; device->context = NULL;
} }
SDL_UnlockMutex(device->dev_lock);
}
static void static void
HIDAPI_DriverXboxOne_FreeDevice(SDL_HIDAPI_Device *device) HIDAPI_DriverXboxOne_FreeDevice(SDL_HIDAPI_Device *device)

View File

@ -1221,9 +1221,7 @@ HIDAPI_JoystickClose(SDL_Joystick * joystick)
SDL_LockMutex(device->dev_lock); SDL_LockMutex(device->dev_lock);
} }
SDL_LockMutex(device->dev_lock);
device->driver->CloseJoystick(device, joystick); device->driver->CloseJoystick(device, joystick);
SDL_UnlockMutex(device->dev_lock);
SDL_free(joystick->hwdata); SDL_free(joystick->hwdata);
joystick->hwdata = NULL; joystick->hwdata = NULL;