Commit e42d46b463e75efd0d6ce8facc2fdd93d94d7328

Ryan C. Gordon 2014-02-06T07:37:20

Wired up haptic hotplugging for Windows DirectInput/XInput code.

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
diff --git a/src/haptic/windows/SDL_syshaptic.c b/src/haptic/windows/SDL_syshaptic.c
index 34ce2de..a68cd34 100644
--- a/src/haptic/windows/SDL_syshaptic.c
+++ b/src/haptic/windows/SDL_syshaptic.c
@@ -33,12 +33,12 @@
 #include "../../joystick/SDL_sysjoystick.h"     /* For the real SDL_Joystick */
 #include "../../joystick/windows/SDL_dxjoystick_c.h"      /* For joystick hwdata */
 
-#define MAX_HAPTICS  32
+#include "SDL_syshaptic_c.h"
 
 /*
  * List of available haptic devices.
  */
-static struct
+typedef struct SDL_hapticlist_item
 {
     DIDEVICEINSTANCE instance;
     char *name;
@@ -46,7 +46,8 @@ static struct
     DIDEVCAPS capabilities;
     Uint8 bXInputHaptic; /* Supports force feedback via XInput. */
     Uint8 userid; /* XInput userid index for this joystick */
-} SDL_hapticlist[MAX_HAPTICS];
+    struct SDL_hapticlist_item *next;
+} SDL_hapticlist_item;
 
 
 /*
@@ -83,7 +84,9 @@ struct haptic_hweffect
 static SDL_bool coinitialized = SDL_FALSE;
 static LPDIRECTINPUT8 dinput = NULL;
 static SDL_bool loaded_xinput = SDL_FALSE;
-
+static SDL_hapticlist_item *SDL_hapticlist = NULL;
+static SDL_hapticlist_item *SDL_hapticlist_tail = NULL;
+static int numhaptics = 0;
 
 /*
  * External stuff.
@@ -101,7 +104,7 @@ static int SDL_SYS_HapticOpenFromInstance(SDL_Haptic * haptic,
 static int SDL_SYS_HapticOpenFromDevice8(SDL_Haptic * haptic,
                                          LPDIRECTINPUTDEVICE8 device8,
                                          SDL_bool is_joystick);
-static int SDL_SYS_HapticOpenFromXInput(SDL_Haptic * haptic, Uint8 userid);
+static int SDL_SYS_HapticOpenFromXInput(SDL_Haptic * haptic, const Uint8 userid);
 static DWORD DIGetTriggerButton(Uint16 button);
 static int SDL_SYS_SetDirection(DIEFFECT * effect, SDL_HapticDirection * dir,
                                 int naxes);
@@ -155,11 +158,6 @@ SDL_SYS_HapticInit(void)
         return SDL_SetError("Haptic: SubSystem already open.");
     }
 
-    /* Clear all the memory. */
-    SDL_memset(SDL_hapticlist, 0, sizeof(SDL_hapticlist));
-
-    SDL_numhaptics = 0;
-
     ret = WIN_CoInitialize();
     if (FAILED(ret)) {
         return DI_SetError("Coinitialize", ret);
@@ -205,73 +203,251 @@ SDL_SYS_HapticInit(void)
 
     if (loaded_xinput) {
         DWORD i;
-        const SDL_bool bIs14OrLater = (SDL_XInputVersion >= ((1<<16)|4));
-
-        for (i = 0; (i < SDL_XINPUT_MAX_DEVICES) && (SDL_numhaptics < MAX_HAPTICS); i++) {
-            XINPUT_CAPABILITIES caps;
-            if (XINPUTGETCAPABILITIES(i, XINPUT_FLAG_GAMEPAD, &caps) == ERROR_SUCCESS) {
-                if ((!bIs14OrLater) || (caps.Flags & XINPUT_CAPS_FFB_SUPPORTED)) {
-                    /* !!! FIXME: I'm not bothering to query for a real name right now. */
-                    char buf[64];
-                    SDL_snprintf(buf, sizeof (buf), "XInput Controller #%u", i+1);
-                    SDL_hapticlist[SDL_numhaptics].name = SDL_strdup(buf);
-                    SDL_hapticlist[SDL_numhaptics].bXInputHaptic = 1;
-                    SDL_hapticlist[SDL_numhaptics].userid = (Uint8) i;
-                    SDL_numhaptics++;
-                }
-            }
+        for (i = 0; i < SDL_XINPUT_MAX_DEVICES; i++) {
+            XInputHaptic_MaybeAddDevice(i);
         }
     }
 
-    return SDL_numhaptics;
+    return numhaptics;
 }
 
-/*
- * Callback to find the haptic devices.
- */
-static BOOL CALLBACK
-EnumHapticsCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
+
+int
+DirectInputHaptic_MaybeAddDevice(const DIDEVICEINSTANCE * pdidInstance)
 {
     HRESULT ret;
     LPDIRECTINPUTDEVICE8 device;
+    const DWORD needflags = DIDC_ATTACHED | DIDC_FORCEFEEDBACK;
+    DIDEVCAPS capabilities;
+    SDL_hapticlist_item *item = NULL;
 
-    /* Copy the instance over, useful for creating devices. */
-    SDL_memcpy(&SDL_hapticlist[SDL_numhaptics].instance, pdidInstance,
-               sizeof(DIDEVICEINSTANCE));
+    /* Make sure we don't already have it */
+    for (item = SDL_hapticlist; item; item = item->next) {
+        if ( (!item->bXInputHaptic) && (SDL_memcmp(&item->instance, pdidInstance, sizeof (*pdidInstance)) == 0) ) {
+            return -1;  /* Already added */
+        }
+    }
 
     /* Open the device */
-    ret = IDirectInput8_CreateDevice(dinput, &pdidInstance->guidInstance,
-                                    &device, NULL);
+    ret = IDirectInput8_CreateDevice(dinput, &pdidInstance->guidInstance, &device, NULL);
     if (FAILED(ret)) {
         /* DI_SetError("Creating DirectInput device",ret); */
-        return DIENUM_CONTINUE;
+        return -1;
     }
 
     /* Get capabilities. */
-    SDL_hapticlist[SDL_numhaptics].capabilities.dwSize = sizeof(DIDEVCAPS);
-    ret = IDirectInputDevice8_GetCapabilities(device,
-                                             &SDL_hapticlist[SDL_numhaptics].
-                                             capabilities);
+    SDL_zero(capabilities);
+    capabilities.dwSize = sizeof (DIDEVCAPS);
+    ret = IDirectInputDevice8_GetCapabilities(device, &capabilities);
+    IDirectInputDevice8_Release(device);
     if (FAILED(ret)) {
         /* DI_SetError("Getting device capabilities",ret); */
-        IDirectInputDevice8_Release(device);
-        return DIENUM_CONTINUE;
+        return -1;
     }
 
-    /* Copy the name */
-    SDL_hapticlist[SDL_numhaptics].name = WIN_StringToUTF8(SDL_hapticlist[SDL_numhaptics].instance.tszProductName);
+    if ((capabilities.dwFlags & needflags) != needflags) {
+        return -1;  /* not a device we can use. */
+    }
 
-    /* Close up device and count it. */
-    IDirectInputDevice8_Release(device);
-    SDL_numhaptics++;
+    item = (SDL_hapticlist_item *)SDL_malloc( sizeof(SDL_hapticlist_item));
+    if (item == NULL) {
+        return SDL_OutOfMemory();
+    }
 
-    /* Watch out for hard limit. */
-    if (SDL_numhaptics >= MAX_HAPTICS)
-        return DIENUM_STOP;
+    SDL_zerop(item);
 
-    return DIENUM_CONTINUE;
+    item->name = WIN_StringToUTF8(pdidInstance->tszProductName);
+    if (!item->name) {
+        SDL_free(item);
+        return -1;
+    }
+
+    /* Copy the instance over, useful for creating devices. */
+    SDL_memcpy(&item->instance, pdidInstance, sizeof (DIDEVICEINSTANCE));
+    SDL_memcpy(&item->capabilities, &capabilities, sizeof (capabilities));
+
+    if (SDL_hapticlist_tail == NULL) {
+        SDL_hapticlist = SDL_hapticlist_tail = item;
+    } else {
+        SDL_hapticlist_tail->next = item;
+        SDL_hapticlist_tail = item;
+    }
+
+    /* Device has been added. */
+    ++numhaptics;
+
+    return numhaptics;
+}
+
+
+int
+DirectInputHaptic_MaybeRemoveDevice(const DIDEVICEINSTANCE * pdidInstance)
+{
+    SDL_hapticlist_item *item;
+    SDL_hapticlist_item *prev = NULL;
+
+    for (item = SDL_hapticlist; item != NULL; item = item->next) {
+        if ( (!item->bXInputHaptic) && (SDL_memcmp(&item->instance, pdidInstance, sizeof (*pdidInstance)) == 0) ) {
+            /* found it, remove it. */
+            const int retval = item->haptic ? item->haptic->index : -1;
+            if (prev != NULL) {
+                prev->next = item->next;
+            } else {
+                SDL_assert(SDL_hapticlist == item);
+                SDL_hapticlist = item->next;
+            }
+            if (item == SDL_hapticlist_tail) {
+                SDL_hapticlist_tail = prev;
+            }
+            --numhaptics;
+            /* !!! TODO: Send a haptic remove event? */
+            SDL_free(item);
+            return retval;
+        }
+        prev = item;
+    }
+
+    return -1;
+}
+
+
+int
+XInputHaptic_MaybeAddDevice(const DWORD dwUserid)
+{
+    const Uint8 userid = (Uint8) dwUserid;
+    XINPUT_CAPABILITIES caps;
+    const SDL_bool bIs14OrLater = (SDL_XInputVersion >= ((1<<16)|4));
+    SDL_hapticlist_item *item;
+
+    if ((!loaded_xinput) || (dwUserid >= SDL_XINPUT_MAX_DEVICES)) {
+        return -1;
+    }
+
+    /* Make sure we don't already have it */
+    for (item = SDL_hapticlist; item; item = item->next) {
+        if ((item->bXInputHaptic) && (item->userid == userid)) {
+            return -1;  /* Already added */
+        }
+    }
+
+    if (XINPUTGETCAPABILITIES(dwUserid, XINPUT_FLAG_GAMEPAD, &caps) != ERROR_SUCCESS) {
+        return -1;  /* maybe controller isn't plugged in. */
+    }
+
+    /* XInput < 1.4 is probably only for original XBox360 controllers,
+        which don't offer the flag, and always have force feedback */
+    if ( (bIs14OrLater) && ((caps.Flags & XINPUT_CAPS_FFB_SUPPORTED) == 0) ) {
+        return -1;  /* no force feedback on this device. */
+    }
+
+    item = (SDL_hapticlist_item *)SDL_malloc( sizeof(SDL_hapticlist_item));
+    if (item == NULL) {
+        return SDL_OutOfMemory();
+    }
+
+    SDL_zerop(item);
+
+    /* !!! FIXME: I'm not bothering to query for a real name right now (can we even?) */
+    {
+        char buf[64];
+        SDL_snprintf(buf, sizeof (buf), "XInput Controller #%u", (unsigned int) (userid+1));
+        item->name = SDL_strdup(buf);
+    }
+
+    if (!item->name) {
+        SDL_free(item);
+        return -1;
+    }
+
+    /* Copy the instance over, useful for creating devices. */
+    item->bXInputHaptic = 1;
+    item->userid = userid;
+
+    if (SDL_hapticlist_tail == NULL) {
+        SDL_hapticlist = SDL_hapticlist_tail = item;
+    } else {
+        SDL_hapticlist_tail->next = item;
+        SDL_hapticlist_tail = item;
+    }
+
+    /* Device has been added. */
+    ++numhaptics;
+
+    return numhaptics;
+}
+
+
+int
+XInputHaptic_MaybeRemoveDevice(const DWORD dwUserid)
+{
+    const Uint8 userid = (Uint8) dwUserid;
+    SDL_hapticlist_item *item;
+    SDL_hapticlist_item *prev = NULL;
+
+    if ((!loaded_xinput) || (dwUserid >= SDL_XINPUT_MAX_DEVICES)) {
+        return -1;
+    }
+
+    for (item = SDL_hapticlist; item != NULL; item = item->next) {
+        if ((item->bXInputHaptic) && (item->userid == userid)) {
+            /* found it, remove it. */
+            const int retval = item->haptic ? item->haptic->index : -1;
+            if (prev != NULL) {
+                prev->next = item->next;
+            } else {
+                SDL_assert(SDL_hapticlist == item);
+                SDL_hapticlist = item->next;
+            }
+            if (item == SDL_hapticlist_tail) {
+                SDL_hapticlist_tail = prev;
+            }
+            --numhaptics;
+            /* !!! TODO: Send a haptic remove event? */
+            SDL_free(item);
+            return retval;
+        }
+        prev = item;
+    }
+
+    return -1;
+}
+
+
+/*
+ * Callback to find the haptic devices.
+ */
+static BOOL CALLBACK
+EnumHapticsCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
+{
+    (void) pContext;
+    DirectInputHaptic_MaybeAddDevice(pdidInstance);
+    return DIENUM_CONTINUE;  /* continue enumerating */
+}
+
+
+int
+SDL_SYS_NumHaptics()
+{
+    return numhaptics;
 }
 
+static SDL_hapticlist_item *
+HapticByDevIndex(int device_index)
+{
+    SDL_hapticlist_item *item = SDL_hapticlist;
+
+    if ((device_index < 0) || (device_index >= numhaptics)) {
+        return NULL;
+    }
+
+    while (device_index > 0) {
+        SDL_assert(item != NULL);
+        device_index--;
+        item = item->next;
+    }
+
+    return item;
+}
 
 /*
  * Return the name of a haptic device, does not need to be opened.
@@ -279,7 +455,8 @@ EnumHapticsCallback(const DIDEVICEINSTANCE * pdidInstance, VOID * pContext)
 const char *
 SDL_SYS_HapticName(int index)
 {
-    return SDL_hapticlist[index].name;
+    SDL_hapticlist_item *item = HapticByDevIndex(index);
+    return item->name;
 }
 
 
@@ -384,7 +561,7 @@ SDL_SYS_HapticOpenFromInstance(SDL_Haptic * haptic, DIDEVICEINSTANCE instance)
 }
 
 static int
-SDL_SYS_HapticOpenFromXInput(SDL_Haptic * haptic, Uint8 userid)
+SDL_SYS_HapticOpenFromXInput(SDL_Haptic *haptic, const Uint8 userid)
 {
     char threadName[32];
     XINPUT_VIBRATION vibration = { 0, 0 };  /* stop any current vibration */
@@ -595,11 +772,8 @@ SDL_SYS_HapticOpenFromDevice8(SDL_Haptic * haptic,
 int
 SDL_SYS_HapticOpen(SDL_Haptic * haptic)
 {
-    if (SDL_hapticlist[haptic->index].bXInputHaptic) {
-        return SDL_SYS_HapticOpenFromXInput(haptic, SDL_hapticlist[haptic->index].userid);
-    }
-
-    return SDL_SYS_HapticOpenFromInstance(haptic, SDL_hapticlist[haptic->index].instance);
+    SDL_hapticlist_item *item = HapticByDevIndex(haptic->index);
+    return (item->bXInputHaptic) ? SDL_SYS_HapticOpenFromXInput(haptic, item->userid) : SDL_SYS_HapticOpenFromInstance(haptic, item->instance);
 }
 
 
@@ -609,13 +783,16 @@ SDL_SYS_HapticOpen(SDL_Haptic * haptic)
 int
 SDL_SYS_HapticMouse(void)
 {
-    int i;
+    SDL_hapticlist_item *item;
+    int index = numhaptics-1;
 
     /* Grab the first mouse haptic device we find. */
-    for (i = 0; i < SDL_numhaptics; i++) {
-        if (SDL_hapticlist[i].capabilities.dwDevType == DI8DEVCLASS_POINTER ) {
-            return i;
+    for (item = SDL_hapticlist; item != NULL; item = item->next) {
+        SDL_assert(index >= 0);
+        if (item->capabilities.dwDevType == DI8DEVCLASS_POINTER ) {
+            return index;
         }
+        index--;
     }
 
     return -1;
@@ -677,34 +854,39 @@ SDL_SYS_JoystickSameHaptic(SDL_Haptic * haptic, SDL_Joystick * joystick)
 int
 SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
 {
-    int i;
-    HRESULT idret;
-    DIDEVICEINSTANCE joy_instance;
-    joy_instance.dwSize = sizeof(DIDEVICEINSTANCE);
+    SDL_hapticlist_item *item;
+    int index = numhaptics-1;
 
     /* Since it comes from a joystick we have to try to match it with a haptic device on our haptic list. */
     if (joystick->hwdata->bXInputDevice) {
         const Uint8 userid = joystick->hwdata->userid;
-        for (i=0; i<SDL_numhaptics; i++) {
-            if ((SDL_hapticlist[i].bXInputHaptic) && (SDL_hapticlist[i].userid == userid)) {
+        for (item = SDL_hapticlist; item != NULL; item = item->next) {
+            if ((item->bXInputHaptic) && (item->userid == userid)) {
                 SDL_assert(joystick->hwdata->bXInputHaptic);
-                haptic->index = i;
-                return SDL_SYS_HapticOpenFromXInput(haptic, SDL_hapticlist[haptic->index].userid);
+                haptic->index = index;
+                return SDL_SYS_HapticOpenFromXInput(haptic, userid);
             }
+            index--;
         }
     } else {
-        for (i=0; i<SDL_numhaptics; i++) {
-            idret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice, &joy_instance);
-            if (FAILED(idret)) {
-                return -1;
-            }
-            if (DI_GUIDIsSame(&SDL_hapticlist[i].instance.guidInstance,
-                              &joy_instance.guidInstance)) {
-                haptic->index = i;
+        HRESULT idret;
+        DIDEVICEINSTANCE joy_instance;
+
+        joy_instance.dwSize = sizeof(DIDEVICEINSTANCE);
+        idret = IDirectInputDevice8_GetDeviceInfo(joystick->hwdata->InputDevice, &joy_instance);
+        if (FAILED(idret)) {
+            return -1;
+        }
+
+        for (item = SDL_hapticlist; item != NULL; item = item->next) {
+            if (DI_GUIDIsSame(&item->instance.guidInstance, &joy_instance.guidInstance)) {
+                haptic->index = index;
                 return SDL_SYS_HapticOpenFromDevice8(haptic, joystick->hwdata->InputDevice, SDL_TRUE);
             }
+            index--;
         }
     }
+
     /* No match to our haptic list */
     return -1;
 }
@@ -749,16 +931,20 @@ SDL_SYS_HapticClose(SDL_Haptic * haptic)
 void
 SDL_SYS_HapticQuit(void)
 {
-    int i;
+    SDL_hapticlist_item *item;
+    SDL_hapticlist_item *next = NULL;
 
     if (loaded_xinput) {
         WIN_UnloadXInputDLL();
         loaded_xinput = SDL_FALSE;
     }
 
-    for (i = 0; i < SDL_arraysize(SDL_hapticlist); ++i) {
-        SDL_free(SDL_hapticlist[i].name);
-        SDL_hapticlist[i].name = NULL;
+    for (item = SDL_hapticlist; item; item = next) {
+        /* Opened and not closed haptics are leaked, this is on purpose.
+         * Close your haptic devices after usage. */
+        next = item->next;
+        SDL_free(item->name);
+        SDL_free(item);
     }
 
     if (dinput != NULL) {
diff --git a/src/haptic/windows/SDL_syshaptic_c.h b/src/haptic/windows/SDL_syshaptic_c.h
new file mode 100644
index 0000000..1e0fa19
--- /dev/null
+++ b/src/haptic/windows/SDL_syshaptic_c.h
@@ -0,0 +1,28 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+
+extern int DirectInputHaptic_MaybeAddDevice(const DIDEVICEINSTANCE *pdidInstance);
+extern int DirectInputHaptic_MaybeRemoveDevice(const DIDEVICEINSTANCE *pdidInstance);
+extern int XInputHaptic_MaybeAddDevice(const DWORD dwUserid);
+extern int XInputHaptic_MaybeRemoveDevice(const DWORD dwUserid);
+
+/* vi: set ts=4 sw=4 expandtab: */
+
diff --git a/src/joystick/windows/SDL_dxjoystick.c b/src/joystick/windows/SDL_dxjoystick.c
index 9021213..da96b60 100644
--- a/src/joystick/windows/SDL_dxjoystick.c
+++ b/src/joystick/windows/SDL_dxjoystick.c
@@ -49,6 +49,10 @@
 #define INITGUID /* Only set here, if set twice will cause mingw32 to break. */
 #include "SDL_dxjoystick_c.h"
 
+#if SDL_HAPTIC_DINPUT
+#include "../../haptic/windows/SDL_syshaptic_c.h"    /* For haptic hot plugging */
+#endif
+
 #ifndef DIDFT_OPTIONAL
 #define DIDFT_OPTIONAL      0x80000000
 #endif
@@ -824,7 +828,17 @@ void SDL_SYS_JoystickDetect()
         while ( pCurList )
         {
             JoyStick_DeviceData *pListNext = NULL;
+
+#if SDL_HAPTIC_DINPUT
+            if (pCurList->bXInputDevice) {
+                XInputHaptic_MaybeRemoveDevice(pCurList->XInputUserId);
+            } else {
+                DirectInputHaptic_MaybeRemoveDevice(&pCurList->dxdevice);
+            }
+#endif
+
 #if !SDL_EVENTS_DISABLED
+            {
             SDL_Event event;
             event.type = SDL_JOYDEVICEREMOVED;
 
@@ -835,6 +849,7 @@ void SDL_SYS_JoystickDetect()
                         SDL_PushEvent(&event);
                 }
             }
+            }
 #endif /* !SDL_EVENTS_DISABLED */
 
             pListNext = pCurList->pNext;
@@ -855,7 +870,16 @@ void SDL_SYS_JoystickDetect()
         {
             if ( pNewJoystick->send_add_event )
             {
+#if SDL_HAPTIC_DINPUT
+                if (pNewJoystick->bXInputDevice) {
+                    XInputHaptic_MaybeAddDevice(pNewJoystick->XInputUserId);
+                } else {
+                    DirectInputHaptic_MaybeAddDevice(&pNewJoystick->dxdevice);
+                }
+#endif
+
 #if !SDL_EVENTS_DISABLED
+                {
                 SDL_Event event;
                 event.type = SDL_JOYDEVICEADDED;
 
@@ -866,6 +890,7 @@ void SDL_SYS_JoystickDetect()
                             SDL_PushEvent(&event);
                     }
                 }
+                }
 #endif /* !SDL_EVENTS_DISABLED */
                 pNewJoystick->send_add_event = 0;
             }