Fixed controller hotplug detection when joystick thread is not enabled
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
diff --git a/src/joystick/windows/SDL_windowsjoystick.c b/src/joystick/windows/SDL_windowsjoystick.c
index 5ea939e..5722f50 100644
--- a/src/joystick/windows/SDL_windowsjoystick.c
+++ b/src/joystick/windows/SDL_windowsjoystick.c
@@ -61,8 +61,7 @@
/* local variables */
static SDL_bool s_bJoystickThread = SDL_FALSE;
-static SDL_bool s_bDeviceAdded = SDL_FALSE;
-static SDL_bool s_bDeviceRemoved = SDL_FALSE;
+static SDL_bool s_bWindowsDeviceChanged = SDL_FALSE;
static SDL_cond *s_condJoystickThread = NULL;
static SDL_mutex *s_mutexJoyStickEnum = NULL;
static SDL_Thread *s_joystickThread = NULL;
@@ -70,8 +69,6 @@ static SDL_bool s_bJoystickThreadQuit = SDL_FALSE;
JoyStick_DeviceData *SYS_Joystick; /* array to hold joystick ID values */
-static SDL_bool s_bWindowsDeviceChanged = SDL_FALSE;
-
#ifdef __WINRT__
typedef struct
@@ -127,9 +124,13 @@ SDL_PrivateJoystickDetectProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
}
return 0;
case WM_TIMER:
- KillTimer(hwnd, wParam);
- s_bWindowsDeviceChanged = SDL_TRUE;
- return 0;
+ if (wParam == IDT_SDL_DEVICE_CHANGE_TIMER_1 ||
+ wParam == IDT_SDL_DEVICE_CHANGE_TIMER_2) {
+ KillTimer(hwnd, wParam);
+ s_bWindowsDeviceChanged = SDL_TRUE;
+ return 0;
+ }
+ break;
}
#if SDL_JOYSTICK_RAWINPUT
@@ -246,8 +247,6 @@ SDL_JoystickThread(void *_data)
SDL_LockMutex(s_mutexJoyStickEnum);
while (s_bJoystickThreadQuit == SDL_FALSE) {
- SDL_bool bXInputChanged = SDL_FALSE;
-
if (SDL_WaitForDeviceNotification(&s_notification_data, s_mutexJoyStickEnum) == SDL_FALSE) {
#if SDL_JOYSTICK_XINPUT
/* WM_DEVICECHANGE not working, poll for new XINPUT controllers */
@@ -260,7 +259,7 @@ SDL_JoystickThread(void *_data)
const DWORD result = XINPUTGETCAPABILITIES(userId, XINPUT_FLAG_GAMEPAD, &capabilities);
const SDL_bool available = (result == ERROR_SUCCESS);
if (bOpenedXInputDevices[userId] != available) {
- bXInputChanged = SDL_TRUE;
+ s_bWindowsDeviceChanged = SDL_TRUE;
bOpenedXInputDevices[userId] = available;
}
}
@@ -270,12 +269,6 @@ SDL_JoystickThread(void *_data)
break;
#endif /* SDL_JOYSTICK_XINPUT */
}
-
- if (s_bWindowsDeviceChanged || bXInputChanged) {
- s_bDeviceRemoved = SDL_TRUE;
- s_bDeviceAdded = SDL_TRUE;
- s_bWindowsDeviceChanged = SDL_FALSE;
- }
}
SDL_UnlockMutex(s_mutexJoyStickEnum);
@@ -337,8 +330,6 @@ void WINDOWS_AddJoystickDevice(JoyStick_DeviceData *device)
device->nInstanceID = SDL_GetNextJoystickInstanceID();
device->pNext = SYS_Joystick;
SYS_Joystick = device;
-
- s_bDeviceAdded = SDL_TRUE;
}
static void WINDOWS_JoystickDetect(void);
@@ -361,7 +352,7 @@ WINDOWS_JoystickInit(void)
return -1;
}
- s_bDeviceAdded = SDL_TRUE; /* force a scan of the system for joysticks this first time */
+ s_bWindowsDeviceChanged = SDL_TRUE; /* force a scan of the system for joysticks this first time */
WINDOWS_JoystickDetect();
@@ -396,10 +387,11 @@ WINDOWS_JoystickGetCount(void)
static void
WINDOWS_JoystickDetect(void)
{
+ int device_index = 0;
JoyStick_DeviceData *pCurList = NULL;
/* only enum the devices if the joystick thread told us something changed */
- if (!s_bDeviceAdded && !s_bDeviceRemoved) {
+ if (!s_bWindowsDeviceChanged) {
return; /* thread hasn't signaled, nothing to do right now. */
}
@@ -407,8 +399,7 @@ WINDOWS_JoystickDetect(void)
SDL_LockMutex(s_mutexJoyStickEnum);
}
- s_bDeviceAdded = SDL_FALSE;
- s_bDeviceRemoved = SDL_FALSE;
+ s_bWindowsDeviceChanged = SDL_FALSE;
pCurList = SYS_Joystick;
SYS_Joystick = NULL;
@@ -444,29 +435,21 @@ WINDOWS_JoystickDetect(void)
pCurList = pListNext;
}
- if (s_bDeviceAdded) {
- JoyStick_DeviceData *pNewJoystick;
- int device_index = 0;
- s_bDeviceAdded = SDL_FALSE;
- pNewJoystick = SYS_Joystick;
- while (pNewJoystick) {
- if (pNewJoystick->send_add_event) {
- if (pNewJoystick->bXInputDevice) {
+ for (device_index = 0, pCurList = SYS_Joystick; pCurList; ++device_index, pCurList = pCurList->pNext) {
+ if (pCurList->send_add_event) {
+ if (pCurList->bXInputDevice) {
#if SDL_HAPTIC_XINPUT
- SDL_XINPUT_MaybeAddDevice(pNewJoystick->XInputUserId);
+ SDL_XINPUT_MaybeAddDevice(pCurList->XInputUserId);
#endif
- } else {
+ } else {
#if SDL_HAPTIC_DINPUT
- SDL_DINPUT_MaybeAddDevice(&pNewJoystick->dxdevice);
+ SDL_DINPUT_MaybeAddDevice(&pCurList->dxdevice);
#endif
- }
+ }
- SDL_PrivateJoystickAdded(pNewJoystick->nInstanceID);
+ SDL_PrivateJoystickAdded(pCurList->nInstanceID);
- pNewJoystick->send_add_event = SDL_FALSE;
- }
- device_index++;
- pNewJoystick = pNewJoystick->pNext;
+ pCurList->send_add_event = SDL_FALSE;
}
}
}
@@ -642,8 +625,7 @@ WINDOWS_JoystickQuit(void)
SDL_DINPUT_JoystickQuit();
SDL_XINPUT_JoystickQuit();
- s_bDeviceAdded = SDL_FALSE;
- s_bDeviceRemoved = SDL_FALSE;
+ s_bWindowsDeviceChanged = SDL_FALSE;
}
static SDL_bool