Commit b5aff9d7c347cd9346759fdb8ab0982a3f44bd4f

Sam Lantinga 2019-11-22T13:12:12

Added SDL_GameControllerTypeForIndex() and SDL_GameControllerGetType() to return the type of controller attached.

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
591
592
593
594
595
596
597
598
599
600
601
diff --git a/include/SDL_gamecontroller.h b/include/SDL_gamecontroller.h
index ebde387..883f2fd 100644
--- a/include/SDL_gamecontroller.h
+++ b/include/SDL_gamecontroller.h
@@ -57,6 +57,15 @@ extern "C" {
 struct _SDL_GameController;
 typedef struct _SDL_GameController SDL_GameController;
 
+typedef enum
+{
+    SDL_CONTROLLER_TYPE_UNKNOWN = 0,
+    SDL_CONTROLLER_TYPE_XBOX360,
+    SDL_CONTROLLER_TYPE_XBOXONE,
+    SDL_CONTROLLER_TYPE_PS3,
+    SDL_CONTROLLER_TYPE_PS4,
+    SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO,
+} SDL_GameControllerType;
 
 typedef enum
 {
@@ -176,6 +185,12 @@ extern DECLSPEC SDL_bool SDLCALL SDL_IsGameController(int joystick_index);
 extern DECLSPEC const char *SDLCALL SDL_GameControllerNameForIndex(int joystick_index);
 
 /**
+ *  Get the type of a game controller.
+ *  This can be called before any controllers are opened.
+ */
+extern DECLSPEC SDL_GameControllerType SDLCALL SDL_GameControllerTypeForIndex(int joystick_index);
+
+/**
  *  Get the mapping of a game controller.
  *  This can be called before any controllers are opened.
  *
@@ -205,6 +220,11 @@ extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerFromInstanceID(SDL
 extern DECLSPEC const char *SDLCALL SDL_GameControllerName(SDL_GameController *gamecontroller);
 
 /**
+ *  Return the type of this currently opened controller
+ */
+extern DECLSPEC SDL_GameControllerType SDLCALL SDL_GameControllerGetType(SDL_GameController *gamecontroller);
+
+/**
  *  Get the player index of an opened game controller, or -1 if it's not available
  *
  *  For XInput controllers this returns the XInput user index.
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index a21d4ba..56e461a 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -731,3 +731,5 @@
 #define SDL_strtokr SDL_strtokr_REAL
 #define SDL_wcsstr SDL_wcsstr_REAL
 #define SDL_wcsncmp SDL_wcsncmp_REAL
+#define SDL_GameControllerTypeForIndex SDL_GameControllerTypeForIndex_REAL
+#define SDL_GameControllerGetType SDL_GameControllerGetType_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 65a9a8f..d7b8f04 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -787,3 +787,5 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_HasARMSIMD,(void),(),return)
 SDL_DYNAPI_PROC(char*,SDL_strtokr,(char *a, const char *b, char **c),(a,b,c),return)
 SDL_DYNAPI_PROC(wchar_t*,SDL_wcsstr,(const wchar_t *a, const wchar_t *b),(a,b),return)
 SDL_DYNAPI_PROC(int,SDL_wcsncmp,(const wchar_t *a, const wchar_t *b, size_t c),(a,b,c),return)
+SDL_DYNAPI_PROC(SDL_GameControllerType,SDL_GameControllerTypeForIndex,(int a),(a),return)
+SDL_DYNAPI_PROC(SDL_GameControllerType,SDL_GameControllerGetType,(SDL_GameController *a),(a),return)
diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index 012a459..47bf1bd 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -1413,6 +1413,17 @@ SDL_GameControllerNameForIndex(int device_index)
 
 
 /**
+ *  Get the type of a game controller.
+ */
+SDL_GameControllerType
+SDL_GameControllerTypeForIndex(int joystick_index)
+{
+    SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(joystick_index);
+    return SDL_GetGameControllerTypeFromGUID(guid);
+}
+
+
+/**
  *  Get the mapping of a game controller.
  *  This can be called before any controllers are opened.
  *  If no mapping can be found, this function returns NULL.
@@ -1743,6 +1754,15 @@ SDL_GameControllerName(SDL_GameController * gamecontroller)
     }
 }
 
+SDL_GameControllerType
+SDL_GameControllerGetType(SDL_GameController *gamecontroller)
+{
+    if (!gamecontroller) {
+        return SDL_CONTROLLER_TYPE_UNKNOWN;
+    }
+    return SDL_GetGameControllerTypeFromGUID(gamecontroller->joystick->guid);
+}
+
 int
 SDL_GameControllerGetPlayerIndex(SDL_GameController *gamecontroller)
 {
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index dfbdfeb..eec3ea9 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -1159,12 +1159,6 @@ void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *prod
 }
 
 SDL_bool
-SDL_IsJoystickPS4(Uint16 vendor, Uint16 product)
-{
-    return (GuessControllerType(vendor, product) == k_eControllerType_PS4Controller);
-}
-
-SDL_bool
 SDL_IsJoystickNintendoSwitchPro(Uint16 vendor, Uint16 product)
 {
     EControllerType eType = GuessControllerType(vendor, product);
@@ -1172,38 +1166,64 @@ SDL_IsJoystickNintendoSwitchPro(Uint16 vendor, Uint16 product)
             eType == k_eControllerType_SwitchInputOnlyController);
 }
 
-SDL_bool
-SDL_IsJoystickNintendoSwitchProInputOnly(Uint16 vendor, Uint16 product)
+SDL_GameControllerType
+SDL_GetGameControllerTypeFromGUID(SDL_JoystickGUID guid)
 {
-    EControllerType eType = GuessControllerType(vendor, product);
-    return (eType == k_eControllerType_SwitchInputOnlyController);
-}
+    SDL_GameControllerType type;
+    Uint16 vendor, product;
 
-SDL_bool
-SDL_IsJoystickSteamController(Uint16 vendor, Uint16 product)
-{
-    EControllerType eType = GuessControllerType(vendor, product);
-    return (eType == k_eControllerType_SteamController ||
-            eType == k_eControllerType_SteamControllerV2);
+    SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL);
+    type = SDL_GetGameControllerType(vendor, product);
+    if (type == SDL_CONTROLLER_TYPE_UNKNOWN) {
+        if (SDL_IsJoystickXInput(guid)) {
+            /* This is probably an Xbox One controller */
+            return SDL_CONTROLLER_TYPE_XBOXONE;
+        }
+    }
+    return type;
 }
 
-SDL_bool
-SDL_IsJoystickXbox360(Uint16 vendor, Uint16 product)
+SDL_GameControllerType
+SDL_GetGameControllerType(Uint16 vendor, Uint16 product)
 {
     /* Filter out some bogus values here */
     if (vendor == 0x0000 && product == 0x0000) {
-        return SDL_FALSE;
+        return SDL_CONTROLLER_TYPE_UNKNOWN;
     }
     if (vendor == 0x0001 && product == 0x0001) {
-        return SDL_FALSE;
+        return SDL_CONTROLLER_TYPE_UNKNOWN;
+    }
+
+    switch (GuessControllerType(vendor, product)) {
+    case k_eControllerType_XBox360Controller:
+        return SDL_CONTROLLER_TYPE_XBOX360;
+    case k_eControllerType_XBoxOneController:
+        return SDL_CONTROLLER_TYPE_XBOXONE;
+    case k_eControllerType_PS3Controller:
+        return SDL_CONTROLLER_TYPE_PS3;
+    case k_eControllerType_PS4Controller:
+        return SDL_CONTROLLER_TYPE_PS4;
+    case k_eControllerType_SwitchProController:
+    case k_eControllerType_SwitchInputOnlyController:
+        return SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO;
+    default:
+        return SDL_CONTROLLER_TYPE_UNKNOWN;
     }
-    return (GuessControllerType(vendor, product) == k_eControllerType_XBox360Controller);
 }
 
 SDL_bool
-SDL_IsJoystickXboxOne(Uint16 vendor, Uint16 product)
+SDL_IsJoystickNintendoSwitchProInputOnly(Uint16 vendor, Uint16 product)
 {
-    return (GuessControllerType(vendor, product) == k_eControllerType_XBoxOneController);
+    EControllerType eType = GuessControllerType(vendor, product);
+    return (eType == k_eControllerType_SwitchInputOnlyController);
+}
+
+SDL_bool
+SDL_IsJoystickSteamController(Uint16 vendor, Uint16 product)
+{
+    EControllerType eType = GuessControllerType(vendor, product);
+    return (eType == k_eControllerType_SteamController ||
+            eType == k_eControllerType_SteamControllerV2);
 }
 
 SDL_bool
@@ -1481,7 +1501,7 @@ SDL_bool SDL_ShouldIgnoreJoystick(const char *name, SDL_JoystickGUID guid)
         }
     }
 
-    if (SDL_IsJoystickPS4(vendor, product) && SDL_IsPS4RemapperRunning()) {
+    if (SDL_GetGameControllerType(vendor, product) == SDL_CONTROLLER_TYPE_PS4 && SDL_IsPS4RemapperRunning()) {
         return SDL_TRUE;
     }
 
diff --git a/src/joystick/SDL_joystick_c.h b/src/joystick/SDL_joystick_c.h
index 2b3e7c2..d84c362 100644
--- a/src/joystick/SDL_joystick_c.h
+++ b/src/joystick/SDL_joystick_c.h
@@ -25,6 +25,7 @@
 #include "../SDL_internal.h"
 
 /* Useful functions and variables from SDL_joystick.c */
+#include "SDL_gamecontroller.h"
 #include "SDL_joystick.h"
 
 struct _SDL_JoystickDriver;
@@ -51,22 +52,16 @@ extern int SDL_JoystickGetDeviceIndexFromInstanceID(SDL_JoystickID instance_id);
 /* Function to extract information from an SDL joystick GUID */
 extern void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version);
 
-/* Function to return whether a joystick is a PS4 controller */
-extern SDL_bool SDL_IsJoystickPS4(Uint16 vendor_id, Uint16 product_id);
+/* Function to return the type of a controller */
+extern SDL_GameControllerType SDL_GetGameControllerTypeFromGUID(SDL_JoystickGUID guid);
+extern SDL_GameControllerType SDL_GetGameControllerType(Uint16 vendor, Uint16 product);
 
 /* Function to return whether a joystick is a Nintendo Switch Pro controller */
-extern SDL_bool SDL_IsJoystickNintendoSwitchPro( Uint16 vendor_id, Uint16 product_id );
 extern SDL_bool SDL_IsJoystickNintendoSwitchProInputOnly(Uint16 vendor_id, Uint16 product_id);
 
 /* Function to return whether a joystick is a Steam Controller */
 extern SDL_bool SDL_IsJoystickSteamController(Uint16 vendor_id, Uint16 product_id);
 
-/* Function to return whether a joystick is an Xbox 360 controller */
-extern SDL_bool SDL_IsJoystickXbox360(Uint16 vendor_id, Uint16 product_id);
-
-/* Function to return whether a joystick is an Xbox One controller */
-extern SDL_bool SDL_IsJoystickXboxOne(Uint16 vendor_id, Uint16 product_id);
-
 /* Function to return whether a joystick guid comes from the XInput driver */
 extern SDL_bool SDL_IsJoystickXInput(SDL_JoystickGUID guid);
 
diff --git a/src/joystick/hidapi/SDL_hidapi_ps4.c b/src/joystick/hidapi/SDL_hidapi_ps4.c
index af17ad3..f69c43d 100644
--- a/src/joystick/hidapi/SDL_hidapi_ps4.c
+++ b/src/joystick/hidapi/SDL_hidapi_ps4.c
@@ -141,7 +141,7 @@ static Uint32 crc32(Uint32 crc, const void *data, int count)
 static SDL_bool
 HIDAPI_DriverPS4_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, const char *name)
 {
-    return SDL_IsJoystickPS4(vendor_id, product_id);
+    return (SDL_GetGameControllerType(vendor_id, product_id) == SDL_CONTROLLER_TYPE_PS4);
 }
 
 static const char *
diff --git a/src/joystick/hidapi/SDL_hidapi_xbox360.c b/src/joystick/hidapi/SDL_hidapi_xbox360.c
index afc21d2..5193c35 100644
--- a/src/joystick/hidapi/SDL_hidapi_xbox360.c
+++ b/src/joystick/hidapi/SDL_hidapi_xbox360.c
@@ -249,6 +249,8 @@ HIDAPI_DriverXbox360_QuitWindowsGamingInput(SDL_DriverXbox360_Context *ctx)
 static SDL_bool
 HIDAPI_DriverXbox360_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, const char *name)
 {
+    SDL_GameControllerType type = SDL_GameControllerType(vendor_id, product_id);
+
 #if defined(__MACOSX__) || defined(__WIN32__)
     if (vendor_id == 0x045e && product_id == 0x028e && version == 1) {
         /* This is the Steam Virtual Gamepad, which isn't supported by this driver */
@@ -258,9 +260,9 @@ HIDAPI_DriverXbox360_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint
         /* This is the old Bluetooth Xbox One S firmware, which isn't supported by this driver */
         return SDL_FALSE;
     }
-    return SDL_IsJoystickXbox360(vendor_id, product_id) || SDL_IsJoystickXboxOne(vendor_id, product_id);
+    return (type == SDL_CONTROLLER_TYPE_XBOX360 || type == SDL_CONTROLLER_TYPE_XBOXONE);
 #else
-    return SDL_IsJoystickXbox360(vendor_id, product_id);
+    return (type == SDL_CONTROLLER_TYPE_XBOX360);
 #endif
 }
 
diff --git a/src/joystick/hidapi/SDL_hidapi_xboxone.c b/src/joystick/hidapi/SDL_hidapi_xboxone.c
index 90f3c09..6894854 100644
--- a/src/joystick/hidapi/SDL_hidapi_xboxone.c
+++ b/src/joystick/hidapi/SDL_hidapi_xboxone.c
@@ -197,7 +197,7 @@ HIDAPI_DriverXboxOne_IsSupportedDevice(Uint16 vendor_id, Uint16 product_id, Uint
         return SDL_FALSE;
     }
 #endif
-    return SDL_IsJoystickXboxOne(vendor_id, product_id);
+    return (SDL_GetGameControllerType(vendor_id, product_id) == SDL_CONTROLLER_TYPE_XBOXONE);
 }
 
 static const char *
diff --git a/src/joystick/windows/SDL_dinputjoystick.c b/src/joystick/windows/SDL_dinputjoystick.c
index 54a10e8..1a39b7a 100644
--- a/src/joystick/windows/SDL_dinputjoystick.c
+++ b/src/joystick/windows/SDL_dinputjoystick.c
@@ -246,139 +246,141 @@ static const IID IID_IWbemLocator = { 0xdc12a687, 0x737f, 0x11cf,{ 0x88, 0x4d, 0
 static SDL_bool
 WIN_IsXInputDevice(const GUID* pGuidProductFromDirectInput)
 {
-	IWbemLocator*           pIWbemLocator = NULL;
-	IEnumWbemClassObject*   pEnumDevices = NULL;
-	IWbemClassObject*       pDevices[20];
-	IWbemServices*          pIWbemServices = NULL;
-	BSTR                    bstrNamespace = NULL;
-	BSTR                    bstrDeviceID = NULL;
-	BSTR                    bstrClassName = NULL;
-	DWORD                   uReturned = 0;
-	SDL_bool                bIsXinputDevice = SDL_FALSE;
-	UINT                    iDevice = 0;
-	VARIANT                 var;
-	HRESULT                 hr;
-
-	SDL_zero(pDevices);
-
-	// Create WMI
-	hr = CoCreateInstance(&CLSID_WbemLocator,
-		NULL,
-		CLSCTX_INPROC_SERVER,
-		&IID_IWbemLocator,
-		(LPVOID*)&pIWbemLocator);
-	if (FAILED(hr) || pIWbemLocator == NULL)
-		goto LCleanup;
-
-	bstrNamespace = SysAllocString(L"\\\\.\\root\\cimv2"); if (bstrNamespace == NULL) goto LCleanup;
-	bstrClassName = SysAllocString(L"Win32_PNPEntity");   if (bstrClassName == NULL) goto LCleanup;
-	bstrDeviceID = SysAllocString(L"DeviceID");          if (bstrDeviceID == NULL)  goto LCleanup;
-
-	// Connect to WMI 
-	hr = IWbemLocator_ConnectServer(pIWbemLocator, bstrNamespace, NULL, NULL, 0L,
-		0L, NULL, NULL, &pIWbemServices);
-	if (FAILED(hr) || pIWbemServices == NULL) {
-		goto LCleanup;
-    }
-
-	// Switch security level to IMPERSONATE. 
-	CoSetProxyBlanket((IUnknown *)pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
-		RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
-
-	hr = IWbemServices_CreateInstanceEnum(pIWbemServices, bstrClassName, 0, NULL, &pEnumDevices);
-	if (FAILED(hr) || pEnumDevices == NULL)
-		goto LCleanup;
-
-	// Loop over all devices
-	for (;;) {
-		// Get 20 at a time
-		hr = IEnumWbemClassObject_Next(pEnumDevices, 10000, SDL_arraysize(pDevices), pDevices, &uReturned);
-		if (FAILED(hr)) {
-			goto LCleanup;
+    IWbemLocator*           pIWbemLocator = NULL;
+    IEnumWbemClassObject*   pEnumDevices = NULL;
+    IWbemClassObject*       pDevices[20];
+    IWbemServices*          pIWbemServices = NULL;
+    BSTR                    bstrNamespace = NULL;
+    BSTR                    bstrDeviceID = NULL;
+    BSTR                    bstrClassName = NULL;
+    DWORD                   uReturned = 0;
+    SDL_bool                bIsXinputDevice = SDL_FALSE;
+    UINT                    iDevice = 0;
+    VARIANT                 var;
+    HRESULT                 hr;
+
+    SDL_zero(pDevices);
+
+    // Create WMI
+    hr = CoCreateInstance(&CLSID_WbemLocator,
+        NULL,
+        CLSCTX_INPROC_SERVER,
+        &IID_IWbemLocator,
+        (LPVOID*)&pIWbemLocator);
+    if (FAILED(hr) || pIWbemLocator == NULL)
+        goto LCleanup;
+
+    bstrNamespace = SysAllocString(L"\\\\.\\root\\cimv2"); if (bstrNamespace == NULL) goto LCleanup;
+    bstrClassName = SysAllocString(L"Win32_PNPEntity");   if (bstrClassName == NULL) goto LCleanup;
+    bstrDeviceID = SysAllocString(L"DeviceID");          if (bstrDeviceID == NULL)  goto LCleanup;
+
+    // Connect to WMI 
+    hr = IWbemLocator_ConnectServer(pIWbemLocator, bstrNamespace, NULL, NULL, 0L,
+        0L, NULL, NULL, &pIWbemServices);
+    if (FAILED(hr) || pIWbemServices == NULL) {
+        goto LCleanup;
+    }
+
+    // Switch security level to IMPERSONATE. 
+    CoSetProxyBlanket((IUnknown *)pIWbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
+        RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
+
+    hr = IWbemServices_CreateInstanceEnum(pIWbemServices, bstrClassName, 0, NULL, &pEnumDevices);
+    if (FAILED(hr) || pEnumDevices == NULL)
+        goto LCleanup;
+
+    // Loop over all devices
+    for (;;) {
+        // Get 20 at a time
+        hr = IEnumWbemClassObject_Next(pEnumDevices, 10000, SDL_arraysize(pDevices), pDevices, &uReturned);
+        if (FAILED(hr)) {
+            goto LCleanup;
         }
-		if (uReturned == 0) {
-			break;
+        if (uReturned == 0) {
+            break;
         }
 
-		for (iDevice = 0; iDevice < uReturned; iDevice++) {
-			// For each device, get its device ID
-			hr = IWbemClassObject_Get(pDevices[iDevice], bstrDeviceID, 0L, &var, NULL, NULL);
-			if (SUCCEEDED(hr) && var.vt == VT_BSTR && var.bstrVal != NULL) {
-				// Check if the device ID contains "IG_".  If it does, then it's an XInput device
-				// This information can not be found from DirectInput 
-				if (SDL_wcsstr(var.bstrVal, L"IG_")) {
-					char *bstrVal = WIN_StringToUTF8(var.bstrVal);
-
-					// If it does, then get the VID/PID from var.bstrVal
-					DWORD dwPid = 0, dwVid = 0, dwVidPid;
-					const char *strVid, *strPid;
-					strVid = SDL_strstr(bstrVal, "VID_");
-					if (strVid && SDL_sscanf(strVid, "VID_%4X", &dwVid) != 1)
-						dwVid = 0;
-					strPid = SDL_strstr(bstrVal, "PID_");
-					if (strPid && SDL_sscanf(strPid, "PID_%4X", &dwPid) != 1)
-						dwPid = 0;
-
-					SDL_free(bstrVal);
-
-					// Compare the VID/PID to the DInput device
-					dwVidPid = MAKELONG(dwVid, dwPid);
-					if (dwVidPid == pGuidProductFromDirectInput->Data1) {
-						bIsXinputDevice = SDL_TRUE;
-						goto LCleanup;
-					}
-				}
-			}
-			IWbemClassObject_Release(pDevices[iDevice]);
-		}
-	}
+        for (iDevice = 0; iDevice < uReturned; iDevice++) {
+            // For each device, get its device ID
+            hr = IWbemClassObject_Get(pDevices[iDevice], bstrDeviceID, 0L, &var, NULL, NULL);
+            if (SUCCEEDED(hr) && var.vt == VT_BSTR && var.bstrVal != NULL) {
+                // Check if the device ID contains "IG_".  If it does, then it's an XInput device
+                // This information can not be found from DirectInput 
+                if (SDL_wcsstr(var.bstrVal, L"IG_")) {
+                    char *bstrVal = WIN_StringToUTF8(var.bstrVal);
+
+                    // If it does, then get the VID/PID from var.bstrVal
+                    DWORD dwPid = 0, dwVid = 0, dwVidPid;
+                    const char *strVid, *strPid;
+                    strVid = SDL_strstr(bstrVal, "VID_");
+                    if (strVid && SDL_sscanf(strVid, "VID_%4X", &dwVid) != 1)
+                        dwVid = 0;
+                    strPid = SDL_strstr(bstrVal, "PID_");
+                    if (strPid && SDL_sscanf(strPid, "PID_%4X", &dwPid) != 1)
+                        dwPid = 0;
+
+                    SDL_free(bstrVal);
+
+                    // Compare the VID/PID to the DInput device
+                    dwVidPid = MAKELONG(dwVid, dwPid);
+                    if (dwVidPid == pGuidProductFromDirectInput->Data1) {
+                        bIsXinputDevice = SDL_TRUE;
+                        goto LCleanup;
+                    }
+                }
+            }
+            IWbemClassObject_Release(pDevices[iDevice]);
+        }
+    }
 
 LCleanup:
-	if (bstrNamespace) {
-		SysFreeString(bstrNamespace);
-    }
-	if (bstrDeviceID) {
-		SysFreeString(bstrDeviceID);
-    }
-	if (bstrClassName) {
-		SysFreeString(bstrClassName);
-    }
-	for (iDevice = 0; iDevice < SDL_arraysize(pDevices); iDevice++) {
-		if (pDevices[iDevice]) {
-			IWbemClassObject_Release(pDevices[iDevice]);
-		}
-	}
-	if (pEnumDevices) {
-		IEnumWbemClassObject_Release(pEnumDevices);
-	}
-	if (pIWbemLocator) {
-		IWbemLocator_Release(pIWbemLocator);
-	}
-	if (pIWbemServices) {
-		IWbemServices_Release(pIWbemServices);
-	}
-
-	return bIsXinputDevice;
+    if (bstrNamespace) {
+        SysFreeString(bstrNamespace);
+    }
+    if (bstrDeviceID) {
+        SysFreeString(bstrDeviceID);
+    }
+    if (bstrClassName) {
+        SysFreeString(bstrClassName);
+    }
+    for (iDevice = 0; iDevice < SDL_arraysize(pDevices); iDevice++) {
+        if (pDevices[iDevice]) {
+            IWbemClassObject_Release(pDevices[iDevice]);
+        }
+    }
+    if (pEnumDevices) {
+        IEnumWbemClassObject_Release(pEnumDevices);
+    }
+    if (pIWbemLocator) {
+        IWbemLocator_Release(pIWbemLocator);
+    }
+    if (pIWbemServices) {
+        IWbemServices_Release(pIWbemServices);
+    }
+
+    return bIsXinputDevice;
 }
 #endif /* 0 */
 
 static SDL_bool
 SDL_IsXInputDevice(const GUID* pGuidProductFromDirectInput)
 {
-	UINT i;
-
-	if (!SDL_XINPUT_Enabled()) {
-		return SDL_FALSE;
-	}
-
-	if (SDL_memcmp(&pGuidProductFromDirectInput->Data4[2], "PIDVID", 6) == 0) {
-		Uint16 vendor_id = (Uint16)LOWORD(pGuidProductFromDirectInput->Data1);
-		Uint16 product_id = (Uint16)HIWORD(pGuidProductFromDirectInput->Data1);
-		if (SDL_IsJoystickXbox360(vendor_id, product_id) || SDL_IsJoystickXboxOne(vendor_id, product_id) ||
-			(vendor_id == 0x28DE && product_id == 0x11FF)) {
-			return SDL_TRUE;
-		}
-	}
+    UINT i;
+
+    if (!SDL_XINPUT_Enabled()) {
+        return SDL_FALSE;
+    }
+
+    if (SDL_memcmp(&pGuidProductFromDirectInput->Data4[2], "PIDVID", 6) == 0) {
+        Uint16 vendor_id = (Uint16)LOWORD(pGuidProductFromDirectInput->Data1);
+        Uint16 product_id = (Uint16)HIWORD(pGuidProductFromDirectInput->Data1);
+        SDL_GameControllerType type = SDL_GetGameControllerType(vendor_id, product_id);
+        if (type == SDL_CONTROLLER_TYPE_XBOX360 ||
+            type == SDL_CONTROLLER_TYPE_XBOXONE ||
+            (vendor_id == 0x28DE && product_id == 0x11FF)) {
+            return SDL_TRUE;
+        }
+    }
 
     /* Go through RAWINPUT (WinXP and later) to find HID devices. */
     /* Cache this if we end up using it. */
diff --git a/test/testgamecontroller.c b/test/testgamecontroller.c
index 8b7f488..1bcf440 100644
--- a/test/testgamecontroller.c
+++ b/test/testgamecontroller.c
@@ -292,7 +292,26 @@ main(int argc, char *argv[])
         {
             nController++;
             name = SDL_GameControllerNameForIndex(i);
-            description = "Controller";
+            switch (SDL_GameControllerTypeForIndex(i)) {
+            case SDL_CONTROLLER_TYPE_XBOX360:
+                description = "XBox 360 Controller";
+                break;
+            case SDL_CONTROLLER_TYPE_XBOXONE:
+                description = "XBox One Controller";
+                break;
+            case SDL_CONTROLLER_TYPE_PS3:
+                description = "PS3 Controller";
+                break;
+            case SDL_CONTROLLER_TYPE_PS4:
+                description = "PS4 Controller";
+                break;
+            case SDL_CONTROLLER_TYPE_NINTENDO_SWITCH_PRO:
+                description = "Nintendo Switch Pro Controller";
+                break;
+            default:
+                description = "Game Controller";
+                break;
+            }
         } else {
             name = SDL_JoystickNameForIndex(i);
             description = "Joystick";