# SDL: Continue running XInput detection while RAWINPUT is active (9f19b)

**URL:** <https://discourse.libsdl.org/t/sdl-continue-running-xinput-detection-while-rawinput-is-active-9f19b/44712>\
**Category:** SDL Commits\
**Created:** [July 7, 2023, 4:52pm UTC](https://discourse.libsdl.org/t/sdl-continue-running-xinput-detection-while-rawinput-is-active-9f19b/44712 "2023-07-07T16:52:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SDLPushPostBot](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sdlpushpostbot/32/4370_2.png) [@SDLPushPostBot](https://discourse.libsdl.org/u/SDLPushPostBot)\
**Post date:** [July 7, 2023, 4:52pm UTC](https://discourse.libsdl.org/t/sdl-continue-running-xinput-detection-while-rawinput-is-active-9f19b/44712/1 "2023-07-07T16:52:02Z")

</div>

> <https://github.com/libsdl-org/SDL/commit/9f19b2bd97a1348f24adc52b1d890d7a4d915694>
>
> This allows us to re-enable XInput correlation for RAWINPUT devices without rein…troducing the controller shutdown bug for controllers using the wireless adapter in https://github.com/libsdl-org/SDL/issues/3468.
> 
> Fixes https://github.com/libsdl-org/SDL/issues/7940
> 
> (cherry picked from commit a0a3957eb65e22721a124476072af0f6f5a7ca18)

```diff
From 9f19b2bd97a1348f24adc52b1d890d7a4d915694 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 7 Jul 2023 09:33:53 -0700
Subject: [PATCH] Continue running XInput detection while RAWINPUT is active

This allows us to re-enable XInput correlation for RAWINPUT devices without reintroducing the controller shutdown bug for controllers using the wireless adapter in https://github.com/libsdl-org/SDL/issues/3468.

Fixes https://github.com/libsdl-org/SDL/issues/7940

(cherry picked from commit a0a3957eb65e22721a124476072af0f6f5a7ca18)
---
 src/joystick/windows/SDL_rawinputjoystick.c | 2 +-
 src/joystick/windows/SDL_xinputjoystick.c | 21 ++++++++++++++-------
 2 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/src/joystick/windows/SDL_rawinputjoystick.c b/src/joystick/windows/SDL_rawinputjoystick.c
index e7ec4052187f..44aad79f6f63 100644
--- a/src/joystick/windows/SDL_rawinputjoystick.c
+++ b/src/joystick/windows/SDL_rawinputjoystick.c
@@ -43,7 +43,7 @@
    raw input will turn off the Xbox Series X controller when it is connected via the
    Xbox One Wireless Adapter.
  */
-#if 0 /*def HAVE_XINPUT_H*/
+#ifdef HAVE_XINPUT_H
 #define SDL_JOYSTICK_RAWINPUT_XINPUT
 #endif
 #ifdef HAVE_WINDOWS_GAMING_INPUT_H
diff --git a/src/joystick/windows/SDL_xinputjoystick.c b/src/joystick/windows/SDL_xinputjoystick.c
index 573c5e57b034..debc6d443627 100644
--- a/src/joystick/windows/SDL_xinputjoystick.c
+++ b/src/joystick/windows/SDL_xinputjoystick.c
@@ -233,6 +233,20 @@ static void AddXInputDevice(Uint8 userid, BYTE SubType, JoyStick_DeviceData **pC
     JoyStick_DeviceData *pPrevJoystick = NULL;
     JoyStick_DeviceData *pNewJoystick = *pContext;
 
+#ifdef SDL_JOYSTICK_RAWINPUT
+ if (RAWINPUT_IsEnabled()) {
+ /* The raw input driver handles more than 4 controllers, so prefer that when available */
+ /* We do this check here rather than at the top of SDL_XINPUT_JoystickDetect() because
+ we need to check XInput state before RAWINPUT gets a hold of the device, otherwise
+ when a controller is connected via the wireless adapter, it will shut down at the
+ first subsequent XInput call. This seems like a driver stack bug?
+
+ Reference: https://github.com/libsdl-org/SDL/issues/3468
+ */
+ return;
+ }
+#endif
+
     if (SDL_XInputUseOldJoystickMapping() && SubType != XINPUT_DEVSUBTYPE_GAMEPAD) {
         return;
     }
@@ -320,13 +334,6 @@ void SDL_XINPUT_JoystickDetect(JoyStick_DeviceData **pContext)
         return;
     }
 
-#ifdef SDL_JOYSTICK_RAWINPUT
- if (RAWINPUT_IsEnabled()) {
- /* The raw input driver handles more than 4 controllers, so prefer that when available */
- return;
- }
-#endif
-
     /* iterate in reverse, so these are in the final list in ascending numeric order. */
     for (iuserid = XUSER_MAX_COUNT - 1; iuserid >= 0; iuserid--) {
         const Uint8 userid = (Uint8)iuserid;

```
