SDL: WGI/DInput: Fix SDL_IsXInputDevice() checks when RawInput is enabled

From f6dc47caefa8ef83bd5d718aa0f399fd47ca7d6b Mon Sep 17 00:00:00 2001
From: Cameron Gutman <[EMAIL REDACTED]>
Date: Sun, 7 Nov 2021 16:00:47 -0600
Subject: [PATCH] WGI/DInput: Fix SDL_IsXInputDevice() checks when RawInput is
 enabled

Enabling the RawInput backend causes SDL_XINPUT_Enabled() to return false.
That causes WGI and DInput backends to take ownership of XInput-compatible
controllers, because they think there's no XInput-specific backend enabled.

In WGI's case, it will actually race with RawInput to open the device. By
properly excluding XInput devices from WGI, we can ensure that the sets of
devices managed by WGI and RawInput don't intersect. This makes the race
harmless, since they'll never both go after the same device.
---
 src/joystick/windows/SDL_dinputjoystick.c       | 7 ++++++-
 src/joystick/windows/SDL_windows_gaming_input.c | 7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/joystick/windows/SDL_dinputjoystick.c b/src/joystick/windows/SDL_dinputjoystick.c
index f334c00b23..8f01474b5d 100644
--- a/src/joystick/windows/SDL_dinputjoystick.c
+++ b/src/joystick/windows/SDL_dinputjoystick.c
@@ -238,7 +238,12 @@ SDL_IsXInputDevice(Uint16 vendor_id, Uint16 product_id, const char* hidPath)
 {
     SDL_GameControllerType type;
 
-    if (!SDL_XINPUT_Enabled()) {
+    /* XInput and RawInput backends will pick up XInput-compatible devices */
+    if (!SDL_XINPUT_Enabled()
+#ifdef SDL_JOYSTICK_RAWINPUT
+        && !RAWINPUT_IsEnabled()
+#endif
+        ) {
         return SDL_FALSE;
     }
 
diff --git a/src/joystick/windows/SDL_windows_gaming_input.c b/src/joystick/windows/SDL_windows_gaming_input.c
index af623c8cae..6d7e1d7cad 100644
--- a/src/joystick/windows/SDL_windows_gaming_input.c
+++ b/src/joystick/windows/SDL_windows_gaming_input.c
@@ -98,7 +98,12 @@ SDL_IsXInputDevice(Uint16 vendor, Uint16 product)
     UINT i, raw_device_count = 0;
     LONG vidpid = MAKELONG(vendor, product);
 
-    if (!SDL_XINPUT_Enabled()) {
+    /* XInput and RawInput backends will pick up XInput-compatible devices */
+    if (!SDL_XINPUT_Enabled()
+#ifdef SDL_JOYSTICK_RAWINPUT
+        && !RAWINPUT_IsEnabled()
+#endif
+        ) {
         return SDL_FALSE;
     }