Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make sure we don't read and write to HIDAPI at the same time, it's no…
…t thread-safe on Windows
  • Loading branch information
slouken committed Oct 3, 2018
1 parent ae5317e commit 1944556
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/joystick/hidapi/SDL_hidapijoystick.c
Expand Up @@ -25,6 +25,7 @@
#include "SDL_endian.h"
#include "SDL_hints.h"
#include "SDL_log.h"
#include "SDL_mutex.h"
#include "SDL_thread.h"
#include "SDL_timer.h"
#include "SDL_joystick.h"
Expand Down Expand Up @@ -54,6 +55,7 @@ struct joystick_hwdata
SDL_HIDAPI_DeviceDriver *driver;
void *context;

SDL_mutex *mutex;
hid_device *dev;
};

Expand Down Expand Up @@ -959,6 +961,7 @@ HIDAPI_JoystickOpen(SDL_Joystick * joystick, int device_index)
SDL_free(hwdata);
return SDL_SetError("Couldn't open HID device %s", device->path);
}
hwdata->mutex = SDL_CreateMutex();

if (!device->driver->Init(joystick, hwdata->dev, device->vendor_id, device->product_id, &hwdata->context)) {
hid_close(hwdata->dev);
Expand All @@ -975,15 +978,26 @@ HIDAPI_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint
{
struct joystick_hwdata *hwdata = joystick->hwdata;
SDL_HIDAPI_DeviceDriver *driver = hwdata->driver;
return driver->Rumble(joystick, hwdata->dev, hwdata->context, low_frequency_rumble, high_frequency_rumble, duration_ms);
int result;

SDL_LockMutex(hwdata->mutex);
result = driver->Rumble(joystick, hwdata->dev, hwdata->context, low_frequency_rumble, high_frequency_rumble, duration_ms);
SDL_UnlockMutex(hwdata->mutex);
return result;
}

static void
HIDAPI_JoystickUpdate(SDL_Joystick * joystick)
{
struct joystick_hwdata *hwdata = joystick->hwdata;
SDL_HIDAPI_DeviceDriver *driver = hwdata->driver;
if (!driver->Update(joystick, hwdata->dev, hwdata->context)) {
SDL_bool succeeded;

SDL_LockMutex(hwdata->mutex);
succeeded = driver->Update(joystick, hwdata->dev, hwdata->context);
SDL_UnlockMutex(hwdata->mutex);

if (!succeeded) {
SDL_HIDAPI_Device *device;
for (device = SDL_HIDAPI_devices; device; device = device->next) {
if (device->instance_id == joystick->instance_id) {
Expand All @@ -1002,6 +1016,7 @@ HIDAPI_JoystickClose(SDL_Joystick * joystick)
driver->Quit(joystick, hwdata->dev, hwdata->context);

hid_close(hwdata->dev);
SDL_DestroyMutex(hwdata->mutex);
SDL_free(hwdata);
joystick->hwdata = NULL;
}
Expand Down

0 comments on commit 1944556

Please sign in to comment.