SDL_rawinputjoystick.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /*
  19. RAWINPUT Joystick API for better handling XInput-capable devices on Windows.
  20. XInput is limited to 4 devices.
  21. Windows.Gaming.Input does not get inputs from XBox One controllers when not in the foreground.
  22. DirectInput does not get inputs from XBox One controllers when not in the foreground, nor rumble or accurate triggers.
  23. RawInput does not get rumble or accurate triggers.
  24. So, combine them as best we can!
  25. */
  26. #include "SDL_internal.h"
  27. #ifdef SDL_JOYSTICK_RAWINPUT
  28. #include "../usb_ids.h"
  29. #include "../SDL_sysjoystick.h"
  30. #include "../../core/windows/SDL_windows.h"
  31. #include "../../core/windows/SDL_hid.h"
  32. #include "../hidapi/SDL_hidapijoystick_c.h"
  33. /* SDL_JOYSTICK_RAWINPUT_XINPUT is disabled because using XInput at the same time as
  34. raw input will turn off the Xbox Series X controller when it is connected via the
  35. Xbox One Wireless Adapter.
  36. */
  37. #ifdef HAVE_XINPUT_H
  38. #define SDL_JOYSTICK_RAWINPUT_XINPUT
  39. #endif
  40. #ifdef HAVE_WINDOWS_GAMING_INPUT_H
  41. #define SDL_JOYSTICK_RAWINPUT_WGI
  42. #endif
  43. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  44. #include "../../core/windows/SDL_xinput.h"
  45. #endif
  46. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  47. #include "../../core/windows/SDL_windows.h"
  48. typedef struct WindowsGamingInputGamepadState WindowsGamingInputGamepadState;
  49. #define GamepadButtons_GUIDE 0x40000000
  50. #define COBJMACROS
  51. #include "windows.gaming.input.h"
  52. #include <roapi.h>
  53. #endif
  54. #if defined(SDL_JOYSTICK_RAWINPUT_XINPUT) || defined(SDL_JOYSTICK_RAWINPUT_WGI)
  55. #define SDL_JOYSTICK_RAWINPUT_MATCHING
  56. #define SDL_JOYSTICK_RAWINPUT_MATCH_AXES
  57. #define SDL_JOYSTICK_RAWINPUT_MATCH_TRIGGERS
  58. #ifdef SDL_JOYSTICK_RAWINPUT_MATCH_TRIGGERS
  59. #define SDL_JOYSTICK_RAWINPUT_MATCH_COUNT 6 // stick + trigger axes
  60. #else
  61. #define SDL_JOYSTICK_RAWINPUT_MATCH_COUNT 4 // stick axes
  62. #endif
  63. #endif
  64. #if 0
  65. #define DEBUG_RAWINPUT
  66. #endif
  67. #ifndef RIDEV_EXINPUTSINK
  68. #define RIDEV_EXINPUTSINK 0x00001000
  69. #define RIDEV_DEVNOTIFY 0x00002000
  70. #endif
  71. #ifndef WM_INPUT_DEVICE_CHANGE
  72. #define WM_INPUT_DEVICE_CHANGE 0x00FE
  73. #endif
  74. #ifndef WM_INPUT
  75. #define WM_INPUT 0x00FF
  76. #endif
  77. #ifndef GIDC_ARRIVAL
  78. #define GIDC_ARRIVAL 1
  79. #define GIDC_REMOVAL 2
  80. #endif
  81. extern void WINDOWS_RAWINPUTEnabledChanged(void);
  82. extern void WINDOWS_JoystickDetect(void);
  83. static bool SDL_RAWINPUT_inited = false;
  84. static bool SDL_RAWINPUT_remote_desktop = false;
  85. static int SDL_RAWINPUT_numjoysticks = 0;
  86. static void RAWINPUT_JoystickClose(SDL_Joystick *joystick);
  87. typedef struct SDL_RAWINPUT_Device
  88. {
  89. SDL_AtomicInt refcount;
  90. char *name;
  91. char *path;
  92. Uint16 vendor_id;
  93. Uint16 product_id;
  94. Uint16 version;
  95. SDL_GUID guid;
  96. bool is_xinput;
  97. bool is_xboxone;
  98. int steam_virtual_gamepad_slot;
  99. PHIDP_PREPARSED_DATA preparsed_data;
  100. HANDLE hDevice;
  101. SDL_Joystick *joystick;
  102. SDL_JoystickID joystick_id;
  103. struct SDL_RAWINPUT_Device *next;
  104. } SDL_RAWINPUT_Device;
  105. struct joystick_hwdata
  106. {
  107. bool is_xinput;
  108. bool is_xboxone;
  109. PHIDP_PREPARSED_DATA preparsed_data;
  110. ULONG max_data_length;
  111. HIDP_DATA *data;
  112. USHORT *button_indices;
  113. USHORT *axis_indices;
  114. USHORT *hat_indices;
  115. bool guide_hack;
  116. bool trigger_hack;
  117. USHORT trigger_hack_index;
  118. #ifdef SDL_JOYSTICK_RAWINPUT_MATCHING
  119. Uint64 match_state; // Lowest 16 bits for button states, higher 24 for 6 4bit axes
  120. Uint64 last_state_packet;
  121. #endif
  122. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  123. bool xinput_enabled;
  124. bool xinput_correlated;
  125. Uint8 xinput_correlation_id;
  126. Uint8 xinput_correlation_count;
  127. Uint8 xinput_uncorrelate_count;
  128. Uint8 xinput_slot;
  129. #endif
  130. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  131. bool wgi_correlated;
  132. Uint8 wgi_correlation_id;
  133. Uint8 wgi_correlation_count;
  134. Uint8 wgi_uncorrelate_count;
  135. WindowsGamingInputGamepadState *wgi_slot;
  136. #endif
  137. SDL_RAWINPUT_Device *device;
  138. };
  139. typedef struct joystick_hwdata RAWINPUT_DeviceContext;
  140. SDL_RAWINPUT_Device *SDL_RAWINPUT_devices;
  141. static const Uint16 subscribed_devices[] = {
  142. USB_USAGE_GENERIC_GAMEPAD,
  143. /* Don't need Joystick for any devices we're handling here (XInput-capable)
  144. USB_USAGE_GENERIC_JOYSTICK,
  145. USB_USAGE_GENERIC_MULTIAXISCONTROLLER,
  146. */
  147. };
  148. #ifdef SDL_JOYSTICK_RAWINPUT_MATCHING
  149. static struct
  150. {
  151. Uint64 last_state_packet;
  152. SDL_Joystick *joystick;
  153. SDL_Joystick *last_joystick;
  154. } guide_button_candidate;
  155. typedef struct WindowsMatchState
  156. {
  157. #ifdef SDL_JOYSTICK_RAWINPUT_MATCH_AXES
  158. SHORT match_axes[SDL_JOYSTICK_RAWINPUT_MATCH_COUNT];
  159. #endif
  160. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  161. WORD xinput_buttons;
  162. #endif
  163. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  164. Uint32 wgi_buttons;
  165. #endif
  166. bool any_data;
  167. } WindowsMatchState;
  168. static void RAWINPUT_FillMatchState(WindowsMatchState *state, Uint64 match_state)
  169. {
  170. #ifdef SDL_JOYSTICK_RAWINPUT_MATCH_AXES
  171. int ii;
  172. #endif
  173. bool any_axes_data = false;
  174. #ifdef SDL_JOYSTICK_RAWINPUT_MATCH_AXES
  175. /* SHORT state->match_axes[4] = {
  176. (match_state & 0x000F0000) >> 4,
  177. (match_state & 0x00F00000) >> 8,
  178. (match_state & 0x0F000000) >> 12,
  179. (match_state & 0xF0000000) >> 16,
  180. }; */
  181. for (ii = 0; ii < 4; ii++) {
  182. state->match_axes[ii] = (SHORT)((match_state & (0x000F0000ull << (ii * 4))) >> (4 + ii * 4));
  183. any_axes_data |= ((Uint32)(state->match_axes[ii] + 0x1000) > 0x2000); // match_state bit is not 0xF, 0x1, or 0x2
  184. }
  185. #endif // SDL_JOYSTICK_RAWINPUT_MATCH_AXES
  186. #ifdef SDL_JOYSTICK_RAWINPUT_MATCH_TRIGGERS
  187. for (; ii < SDL_JOYSTICK_RAWINPUT_MATCH_COUNT; ii++) {
  188. state->match_axes[ii] = (SHORT)((match_state & (0x000F0000ull << (ii * 4))) >> (4 + ii * 4));
  189. any_axes_data |= (state->match_axes[ii] != SDL_MIN_SINT16);
  190. }
  191. #endif // SDL_JOYSTICK_RAWINPUT_MATCH_TRIGGERS
  192. state->any_data = any_axes_data;
  193. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  194. // Match axes by checking if the distance between the high 4 bits of axis and the 4 bits from match_state is 1 or less
  195. #define XInputAxesMatch(gamepad) ( \
  196. (Uint32)(gamepad.sThumbLX - state->match_axes[0] + 0x1000) <= 0x2fff && \
  197. (Uint32)(~gamepad.sThumbLY - state->match_axes[1] + 0x1000) <= 0x2fff && \
  198. (Uint32)(gamepad.sThumbRX - state->match_axes[2] + 0x1000) <= 0x2fff && \
  199. (Uint32)(~gamepad.sThumbRY - state->match_axes[3] + 0x1000) <= 0x2fff)
  200. /* Explicit
  201. #define XInputAxesMatch(gamepad) (\
  202. SDL_abs((Sint8)((gamepad.sThumbLX & 0xF000) >> 8) - ((match_state & 0x000F0000) >> 12)) <= 0x10 && \
  203. SDL_abs((Sint8)((~gamepad.sThumbLY & 0xF000) >> 8) - ((match_state & 0x00F00000) >> 16)) <= 0x10 && \
  204. SDL_abs((Sint8)((gamepad.sThumbRX & 0xF000) >> 8) - ((match_state & 0x0F000000) >> 20)) <= 0x10 && \
  205. SDL_abs((Sint8)((~gamepad.sThumbRY & 0xF000) >> 8) - ((match_state & 0xF0000000) >> 24)) <= 0x10) */
  206. // Can only match trigger values if a single trigger has a value.
  207. #define XInputTriggersMatch(gamepad) ( \
  208. ((state->match_axes[4] == SDL_MIN_SINT16) && (state->match_axes[5] == SDL_MIN_SINT16)) || \
  209. ((gamepad.bLeftTrigger != 0) && (gamepad.bRightTrigger != 0)) || \
  210. ((Uint32)((((int)gamepad.bLeftTrigger * 257) - 32768) - state->match_axes[4]) <= 0x2fff) || \
  211. ((Uint32)((((int)gamepad.bRightTrigger * 257) - 32768) - state->match_axes[5]) <= 0x2fff))
  212. state->xinput_buttons =
  213. // Bitwise map .RLDUWVQTS.KYXBA -> YXBA..WVQTKSRLDU
  214. (WORD)(match_state << 12 | (match_state & 0x0780) >> 1 | (match_state & 0x0010) << 1 | (match_state & 0x0040) >> 2 | (match_state & 0x7800) >> 11);
  215. /* Explicit
  216. ((match_state & (1<<SDL_GAMEPAD_BUTTON_SOUTH)) ? XINPUT_GAMEPAD_A : 0) |
  217. ((match_state & (1<<SDL_GAMEPAD_BUTTON_EAST)) ? XINPUT_GAMEPAD_B : 0) |
  218. ((match_state & (1<<SDL_GAMEPAD_BUTTON_WEST)) ? XINPUT_GAMEPAD_X : 0) |
  219. ((match_state & (1<<SDL_GAMEPAD_BUTTON_NORTH)) ? XINPUT_GAMEPAD_Y : 0) |
  220. ((match_state & (1<<SDL_GAMEPAD_BUTTON_BACK)) ? XINPUT_GAMEPAD_BACK : 0) |
  221. ((match_state & (1<<SDL_GAMEPAD_BUTTON_START)) ? XINPUT_GAMEPAD_START : 0) |
  222. ((match_state & (1<<SDL_GAMEPAD_BUTTON_LEFT_STICK)) ? XINPUT_GAMEPAD_LEFT_THUMB : 0) |
  223. ((match_state & (1<<SDL_GAMEPAD_BUTTON_RIGHT_STICK)) ? XINPUT_GAMEPAD_RIGHT_THUMB: 0) |
  224. ((match_state & (1<<SDL_GAMEPAD_BUTTON_LEFT_SHOULDER)) ? XINPUT_GAMEPAD_LEFT_SHOULDER : 0) |
  225. ((match_state & (1<<SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER)) ? XINPUT_GAMEPAD_RIGHT_SHOULDER : 0) |
  226. ((match_state & (1<<SDL_GAMEPAD_BUTTON_DPAD_UP)) ? XINPUT_GAMEPAD_DPAD_UP : 0) |
  227. ((match_state & (1<<SDL_GAMEPAD_BUTTON_DPAD_DOWN)) ? XINPUT_GAMEPAD_DPAD_DOWN : 0) |
  228. ((match_state & (1<<SDL_GAMEPAD_BUTTON_DPAD_LEFT)) ? XINPUT_GAMEPAD_DPAD_LEFT : 0) |
  229. ((match_state & (1<<SDL_GAMEPAD_BUTTON_DPAD_RIGHT)) ? XINPUT_GAMEPAD_DPAD_RIGHT : 0);
  230. */
  231. if (state->xinput_buttons) {
  232. state->any_data = true;
  233. }
  234. #endif
  235. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  236. // Match axes by checking if the distance between the high 4 bits of axis and the 4 bits from match_state is 1 or less
  237. #define WindowsGamingInputAxesMatch(gamepad) ( \
  238. (Uint16)(((Sint16)(gamepad.LeftThumbstickX * SDL_MAX_SINT16) & 0xF000) - state->match_axes[0] + 0x1000) <= 0x2fff && \
  239. (Uint16)((~(Sint16)(gamepad.LeftThumbstickY * SDL_MAX_SINT16) & 0xF000) - state->match_axes[1] + 0x1000) <= 0x2fff && \
  240. (Uint16)(((Sint16)(gamepad.RightThumbstickX * SDL_MAX_SINT16) & 0xF000) - state->match_axes[2] + 0x1000) <= 0x2fff && \
  241. (Uint16)((~(Sint16)(gamepad.RightThumbstickY * SDL_MAX_SINT16) & 0xF000) - state->match_axes[3] + 0x1000) <= 0x2fff)
  242. #define WindowsGamingInputTriggersMatch(gamepad) ( \
  243. ((state->match_axes[4] == SDL_MIN_SINT16) && (state->match_axes[5] == SDL_MIN_SINT16)) || \
  244. ((gamepad.LeftTrigger == 0.0f) && (gamepad.RightTrigger == 0.0f)) || \
  245. ((Uint16)((((int)(gamepad.LeftTrigger * SDL_MAX_UINT16)) - 32768) - state->match_axes[4]) <= 0x2fff) || \
  246. ((Uint16)((((int)(gamepad.RightTrigger * SDL_MAX_UINT16)) - 32768) - state->match_axes[5]) <= 0x2fff))
  247. state->wgi_buttons =
  248. // Bitwise map .RLD UWVQ TS.K YXBA -> ..QT WVRL DUYX BAKS
  249. // RStick/LStick (QT) RShould/LShould (WV) DPad R/L/D/U YXBA bac(K) (S)tart
  250. (match_state & 0x0180) << 5 | (match_state & 0x0600) << 1 | (match_state & 0x7800) >> 5 | (match_state & 0x000F) << 2 | (match_state & 0x0010) >> 3 | (match_state & 0x0040) >> 6;
  251. /* Explicit
  252. ((match_state & (1<<SDL_GAMEPAD_BUTTON_SOUTH)) ? GamepadButtons_A : 0) |
  253. ((match_state & (1<<SDL_GAMEPAD_BUTTON_EAST)) ? GamepadButtons_B : 0) |
  254. ((match_state & (1<<SDL_GAMEPAD_BUTTON_WEST)) ? GamepadButtons_X : 0) |
  255. ((match_state & (1<<SDL_GAMEPAD_BUTTON_NORTH)) ? GamepadButtons_Y : 0) |
  256. ((match_state & (1<<SDL_GAMEPAD_BUTTON_BACK)) ? GamepadButtons_View : 0) |
  257. ((match_state & (1<<SDL_GAMEPAD_BUTTON_START)) ? GamepadButtons_Menu : 0) |
  258. ((match_state & (1<<SDL_GAMEPAD_BUTTON_LEFT_STICK)) ? GamepadButtons_LeftThumbstick : 0) |
  259. ((match_state & (1<<SDL_GAMEPAD_BUTTON_RIGHT_STICK)) ? GamepadButtons_RightThumbstick: 0) |
  260. ((match_state & (1<<SDL_GAMEPAD_BUTTON_LEFT_SHOULDER)) ? GamepadButtons_LeftShoulder: 0) |
  261. ((match_state & (1<<SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER)) ? GamepadButtons_RightShoulder: 0) |
  262. ((match_state & (1<<SDL_GAMEPAD_BUTTON_DPAD_UP)) ? GamepadButtons_DPadUp : 0) |
  263. ((match_state & (1<<SDL_GAMEPAD_BUTTON_DPAD_DOWN)) ? GamepadButtons_DPadDown : 0) |
  264. ((match_state & (1<<SDL_GAMEPAD_BUTTON_DPAD_LEFT)) ? GamepadButtons_DPadLeft : 0) |
  265. ((match_state & (1<<SDL_GAMEPAD_BUTTON_DPAD_RIGHT)) ? GamepadButtons_DPadRight : 0); */
  266. if (state->wgi_buttons) {
  267. state->any_data = true;
  268. }
  269. #endif
  270. }
  271. #endif // SDL_JOYSTICK_RAWINPUT_MATCHING
  272. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  273. static struct
  274. {
  275. XINPUT_STATE state;
  276. XINPUT_BATTERY_INFORMATION_EX battery;
  277. bool connected; // Currently has an active XInput device
  278. bool used; // Is currently mapped to an SDL device
  279. Uint8 correlation_id;
  280. } xinput_state[XUSER_MAX_COUNT];
  281. static bool xinput_device_change = true;
  282. static bool xinput_state_dirty = true;
  283. static void RAWINPUT_UpdateXInput(void)
  284. {
  285. DWORD user_index;
  286. if (xinput_device_change) {
  287. for (user_index = 0; user_index < XUSER_MAX_COUNT; user_index++) {
  288. XINPUT_CAPABILITIES capabilities;
  289. xinput_state[user_index].connected = (XINPUTGETCAPABILITIES(user_index, XINPUT_FLAG_GAMEPAD, &capabilities) == ERROR_SUCCESS);
  290. }
  291. xinput_device_change = false;
  292. xinput_state_dirty = true;
  293. }
  294. if (xinput_state_dirty) {
  295. xinput_state_dirty = false;
  296. for (user_index = 0; user_index < SDL_arraysize(xinput_state); ++user_index) {
  297. if (xinput_state[user_index].connected) {
  298. if (XINPUTGETSTATE(user_index, &xinput_state[user_index].state) != ERROR_SUCCESS) {
  299. xinput_state[user_index].connected = false;
  300. }
  301. xinput_state[user_index].battery.BatteryType = BATTERY_TYPE_UNKNOWN;
  302. if (XINPUTGETBATTERYINFORMATION) {
  303. XINPUTGETBATTERYINFORMATION(user_index, BATTERY_DEVTYPE_GAMEPAD, &xinput_state[user_index].battery);
  304. }
  305. }
  306. }
  307. }
  308. }
  309. static void RAWINPUT_MarkXInputSlotUsed(Uint8 xinput_slot)
  310. {
  311. if (xinput_slot != XUSER_INDEX_ANY) {
  312. xinput_state[xinput_slot].used = true;
  313. }
  314. }
  315. static void RAWINPUT_MarkXInputSlotFree(Uint8 xinput_slot)
  316. {
  317. if (xinput_slot != XUSER_INDEX_ANY) {
  318. xinput_state[xinput_slot].used = false;
  319. }
  320. }
  321. static bool RAWINPUT_MissingXInputSlot(void)
  322. {
  323. int ii;
  324. for (ii = 0; ii < SDL_arraysize(xinput_state); ii++) {
  325. if (xinput_state[ii].connected && !xinput_state[ii].used) {
  326. return true;
  327. }
  328. }
  329. return false;
  330. }
  331. static bool RAWINPUT_XInputSlotMatches(const WindowsMatchState *state, Uint8 slot_idx)
  332. {
  333. if (xinput_state[slot_idx].connected) {
  334. WORD xinput_buttons = xinput_state[slot_idx].state.Gamepad.wButtons;
  335. if ((xinput_buttons & ~XINPUT_GAMEPAD_GUIDE) == state->xinput_buttons
  336. #ifdef SDL_JOYSTICK_RAWINPUT_MATCH_AXES
  337. && XInputAxesMatch(xinput_state[slot_idx].state.Gamepad)
  338. #endif
  339. #ifdef SDL_JOYSTICK_RAWINPUT_MATCH_TRIGGERS
  340. && XInputTriggersMatch(xinput_state[slot_idx].state.Gamepad)
  341. #endif
  342. ) {
  343. return true;
  344. }
  345. }
  346. return false;
  347. }
  348. static bool RAWINPUT_GuessXInputSlot(const WindowsMatchState *state, Uint8 *correlation_id, Uint8 *slot_idx)
  349. {
  350. Uint8 user_index;
  351. int match_count;
  352. /* If there is only one available slot, let's use that
  353. * That will be right most of the time, and uncorrelation will fix any bad guesses
  354. */
  355. match_count = 0;
  356. for (user_index = 0; user_index < XUSER_MAX_COUNT; ++user_index) {
  357. if (xinput_state[user_index].connected && !xinput_state[user_index].used) {
  358. *slot_idx = user_index;
  359. ++match_count;
  360. }
  361. }
  362. if (match_count == 1) {
  363. *correlation_id = ++xinput_state[*slot_idx].correlation_id;
  364. return true;
  365. }
  366. *slot_idx = 0;
  367. match_count = 0;
  368. for (user_index = 0; user_index < XUSER_MAX_COUNT; ++user_index) {
  369. if (!xinput_state[user_index].used && RAWINPUT_XInputSlotMatches(state, user_index)) {
  370. ++match_count;
  371. *slot_idx = user_index;
  372. // Incrementing correlation_id for any match, as negative evidence for others being correlated
  373. *correlation_id = ++xinput_state[user_index].correlation_id;
  374. }
  375. }
  376. /* Only return a match if we match exactly one, and we have some non-zero data (buttons or axes) that matched.
  377. Note that we're still invalidating *other* potential correlations if we have more than one match or we have no
  378. data. */
  379. if (match_count == 1 && state->any_data) {
  380. return true;
  381. }
  382. return false;
  383. }
  384. #endif // SDL_JOYSTICK_RAWINPUT_XINPUT
  385. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  386. typedef struct WindowsGamingInputGamepadState
  387. {
  388. __x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad;
  389. struct __x_ABI_CWindows_CGaming_CInput_CGamepadReading state;
  390. RAWINPUT_DeviceContext *correlated_context;
  391. bool used; // Is currently mapped to an SDL device
  392. bool connected; // Just used during update to track disconnected
  393. Uint8 correlation_id;
  394. struct __x_ABI_CWindows_CGaming_CInput_CGamepadVibration vibration;
  395. } WindowsGamingInputGamepadState;
  396. static struct
  397. {
  398. WindowsGamingInputGamepadState **per_gamepad;
  399. int per_gamepad_count;
  400. bool initialized;
  401. bool dirty;
  402. bool need_device_list_update;
  403. int ref_count;
  404. __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics *gamepad_statics;
  405. EventRegistrationToken gamepad_added_token;
  406. EventRegistrationToken gamepad_removed_token;
  407. } wgi_state;
  408. typedef struct GamepadDelegate
  409. {
  410. __FIEventHandler_1_Windows__CGaming__CInput__CGamepad iface;
  411. SDL_AtomicInt refcount;
  412. } GamepadDelegate;
  413. static const IID IID_IEventHandler_Gamepad = { 0x8a7639ee, 0x624a, 0x501a, { 0xbb, 0x53, 0x56, 0x2d, 0x1e, 0xc1, 0x1b, 0x52 } };
  414. static HRESULT STDMETHODCALLTYPE IEventHandler_CGamepadVtbl_QueryInterface(__FIEventHandler_1_Windows__CGaming__CInput__CGamepad *This, REFIID riid, void **ppvObject)
  415. {
  416. if (!ppvObject) {
  417. return E_INVALIDARG;
  418. }
  419. *ppvObject = NULL;
  420. if (WIN_IsEqualIID(riid, &IID_IUnknown) || WIN_IsEqualIID(riid, &IID_IAgileObject) || WIN_IsEqualIID(riid, &IID_IEventHandler_Gamepad)) {
  421. *ppvObject = This;
  422. __FIEventHandler_1_Windows__CGaming__CInput__CGamepad_AddRef(This);
  423. return S_OK;
  424. } else if (WIN_IsEqualIID(riid, &IID_IMarshal)) {
  425. // This seems complicated. Let's hope it doesn't happen.
  426. return E_OUTOFMEMORY;
  427. } else {
  428. return E_NOINTERFACE;
  429. }
  430. }
  431. static ULONG STDMETHODCALLTYPE IEventHandler_CGamepadVtbl_AddRef(__FIEventHandler_1_Windows__CGaming__CInput__CGamepad *This)
  432. {
  433. GamepadDelegate *self = (GamepadDelegate *)This;
  434. return SDL_AddAtomicInt(&self->refcount, 1) + 1UL;
  435. }
  436. static ULONG STDMETHODCALLTYPE IEventHandler_CGamepadVtbl_Release(__FIEventHandler_1_Windows__CGaming__CInput__CGamepad *This)
  437. {
  438. GamepadDelegate *self = (GamepadDelegate *)This;
  439. int rc = SDL_AddAtomicInt(&self->refcount, -1) - 1;
  440. // Should never free the static delegate objects
  441. SDL_assert(rc > 0);
  442. return rc;
  443. }
  444. static HRESULT STDMETHODCALLTYPE IEventHandler_CGamepadVtbl_InvokeAdded(__FIEventHandler_1_Windows__CGaming__CInput__CGamepad *This, IInspectable *sender, __x_ABI_CWindows_CGaming_CInput_CIGamepad *e)
  445. {
  446. wgi_state.need_device_list_update = true;
  447. return S_OK;
  448. }
  449. static HRESULT STDMETHODCALLTYPE IEventHandler_CGamepadVtbl_InvokeRemoved(__FIEventHandler_1_Windows__CGaming__CInput__CGamepad *This, IInspectable *sender, __x_ABI_CWindows_CGaming_CInput_CIGamepad *e)
  450. {
  451. wgi_state.need_device_list_update = true;
  452. return S_OK;
  453. }
  454. #ifdef _MSC_VER
  455. #pragma warning(push)
  456. #pragma warning(disable : 4028) // formal parameter 3 different from declaration, when using older buggy WGI headers
  457. #pragma warning(disable : 4113) // X differs in parameter lists from Y, when using older buggy WGI headers
  458. #endif
  459. static __FIEventHandler_1_Windows__CGaming__CInput__CGamepadVtbl gamepad_added_vtbl = {
  460. IEventHandler_CGamepadVtbl_QueryInterface,
  461. IEventHandler_CGamepadVtbl_AddRef,
  462. IEventHandler_CGamepadVtbl_Release,
  463. IEventHandler_CGamepadVtbl_InvokeAdded
  464. };
  465. static GamepadDelegate gamepad_added = {
  466. { &gamepad_added_vtbl },
  467. { 1 }
  468. };
  469. static __FIEventHandler_1_Windows__CGaming__CInput__CGamepadVtbl gamepad_removed_vtbl = {
  470. IEventHandler_CGamepadVtbl_QueryInterface,
  471. IEventHandler_CGamepadVtbl_AddRef,
  472. IEventHandler_CGamepadVtbl_Release,
  473. IEventHandler_CGamepadVtbl_InvokeRemoved
  474. };
  475. static GamepadDelegate gamepad_removed = {
  476. { &gamepad_removed_vtbl },
  477. { 1 }
  478. };
  479. #ifdef _MSC_VER
  480. #pragma warning(pop)
  481. #endif
  482. static void RAWINPUT_MarkWindowsGamingInputSlotUsed(WindowsGamingInputGamepadState *wgi_slot, RAWINPUT_DeviceContext *ctx)
  483. {
  484. wgi_slot->used = true;
  485. wgi_slot->correlated_context = ctx;
  486. }
  487. static void RAWINPUT_MarkWindowsGamingInputSlotFree(WindowsGamingInputGamepadState *wgi_slot)
  488. {
  489. wgi_slot->used = false;
  490. wgi_slot->correlated_context = NULL;
  491. }
  492. static bool RAWINPUT_MissingWindowsGamingInputSlot(void)
  493. {
  494. int ii;
  495. for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
  496. if (!wgi_state.per_gamepad[ii]->used) {
  497. return true;
  498. }
  499. }
  500. return false;
  501. }
  502. static bool RAWINPUT_UpdateWindowsGamingInput(void)
  503. {
  504. int ii;
  505. if (!wgi_state.gamepad_statics) {
  506. return true;
  507. }
  508. if (!wgi_state.dirty) {
  509. return true;
  510. }
  511. wgi_state.dirty = false;
  512. if (wgi_state.need_device_list_update) {
  513. HRESULT hr;
  514. __FIVectorView_1_Windows__CGaming__CInput__CGamepad *gamepads;
  515. wgi_state.need_device_list_update = false;
  516. for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
  517. wgi_state.per_gamepad[ii]->connected = false;
  518. }
  519. hr = __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_get_Gamepads(wgi_state.gamepad_statics, &gamepads);
  520. if (SUCCEEDED(hr)) {
  521. unsigned int num_gamepads;
  522. hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_get_Size(gamepads, &num_gamepads);
  523. if (SUCCEEDED(hr)) {
  524. unsigned int i;
  525. for (i = 0; i < num_gamepads; ++i) {
  526. __x_ABI_CWindows_CGaming_CInput_CIGamepad *gamepad;
  527. hr = __FIVectorView_1_Windows__CGaming__CInput__CGamepad_GetAt(gamepads, i, &gamepad);
  528. if (SUCCEEDED(hr)) {
  529. bool found = false;
  530. int jj;
  531. for (jj = 0; jj < wgi_state.per_gamepad_count; jj++) {
  532. if (wgi_state.per_gamepad[jj]->gamepad == gamepad) {
  533. found = true;
  534. wgi_state.per_gamepad[jj]->connected = true;
  535. break;
  536. }
  537. }
  538. if (!found) {
  539. // New device, add it
  540. WindowsGamingInputGamepadState *gamepad_state;
  541. WindowsGamingInputGamepadState **new_per_gamepad;
  542. gamepad_state = SDL_calloc(1, sizeof(*gamepad_state));
  543. if (!gamepad_state) {
  544. return false;
  545. }
  546. new_per_gamepad = SDL_realloc(wgi_state.per_gamepad, sizeof(wgi_state.per_gamepad[0]) * (wgi_state.per_gamepad_count + 1));
  547. if (!new_per_gamepad) {
  548. SDL_free(gamepad_state);
  549. return false;
  550. }
  551. wgi_state.per_gamepad = new_per_gamepad;
  552. wgi_state.per_gamepad_count++;
  553. wgi_state.per_gamepad[wgi_state.per_gamepad_count - 1] = gamepad_state;
  554. gamepad_state->gamepad = gamepad;
  555. gamepad_state->connected = true;
  556. } else {
  557. // Already tracked
  558. __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(gamepad);
  559. }
  560. }
  561. }
  562. for (ii = wgi_state.per_gamepad_count - 1; ii >= 0; ii--) {
  563. WindowsGamingInputGamepadState *gamepad_state = wgi_state.per_gamepad[ii];
  564. if (!gamepad_state->connected) {
  565. // Device missing, must be disconnected
  566. if (gamepad_state->correlated_context) {
  567. gamepad_state->correlated_context->wgi_correlated = false;
  568. gamepad_state->correlated_context->wgi_slot = NULL;
  569. }
  570. __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(gamepad_state->gamepad);
  571. SDL_free(gamepad_state);
  572. wgi_state.per_gamepad[ii] = wgi_state.per_gamepad[wgi_state.per_gamepad_count - 1];
  573. --wgi_state.per_gamepad_count;
  574. }
  575. }
  576. }
  577. __FIVectorView_1_Windows__CGaming__CInput__CGamepad_Release(gamepads);
  578. }
  579. } // need_device_list_update
  580. for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
  581. HRESULT hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_GetCurrentReading(wgi_state.per_gamepad[ii]->gamepad, &wgi_state.per_gamepad[ii]->state);
  582. if (!SUCCEEDED(hr)) {
  583. wgi_state.per_gamepad[ii]->connected = false; // Not used by anything, currently
  584. }
  585. }
  586. return true;
  587. }
  588. static void RAWINPUT_InitWindowsGamingInput(RAWINPUT_DeviceContext *ctx)
  589. {
  590. if (!SDL_GetHintBoolean(SDL_HINT_JOYSTICK_WGI, true)) {
  591. return;
  592. }
  593. wgi_state.ref_count++;
  594. if (!wgi_state.initialized) {
  595. static const IID SDL_IID_IGamepadStatics = { 0x8BBCE529, 0xD49C, 0x39E9, { 0x95, 0x60, 0xE4, 0x7D, 0xDE, 0x96, 0xB7, 0xC8 } };
  596. HRESULT hr;
  597. if (FAILED(WIN_RoInitialize())) {
  598. return;
  599. }
  600. wgi_state.initialized = true;
  601. wgi_state.dirty = true;
  602. {
  603. typedef HRESULT(WINAPI * WindowsCreateStringReference_t)(PCWSTR sourceString, UINT32 length, HSTRING_HEADER * hstringHeader, HSTRING * string);
  604. typedef HRESULT(WINAPI * RoGetActivationFactory_t)(HSTRING activatableClassId, REFIID iid, void **factory);
  605. WindowsCreateStringReference_t WindowsCreateStringReferenceFunc = (WindowsCreateStringReference_t)WIN_LoadComBaseFunction("WindowsCreateStringReference");
  606. RoGetActivationFactory_t RoGetActivationFactoryFunc = (RoGetActivationFactory_t)WIN_LoadComBaseFunction("RoGetActivationFactory");
  607. if (WindowsCreateStringReferenceFunc && RoGetActivationFactoryFunc) {
  608. PCWSTR pNamespace = L"Windows.Gaming.Input.Gamepad";
  609. HSTRING_HEADER hNamespaceStringHeader;
  610. HSTRING hNamespaceString;
  611. hr = WindowsCreateStringReferenceFunc(pNamespace, (UINT32)SDL_wcslen(pNamespace), &hNamespaceStringHeader, &hNamespaceString);
  612. if (SUCCEEDED(hr)) {
  613. RoGetActivationFactoryFunc(hNamespaceString, &SDL_IID_IGamepadStatics, (void **)&wgi_state.gamepad_statics);
  614. }
  615. if (wgi_state.gamepad_statics) {
  616. wgi_state.need_device_list_update = true;
  617. hr = __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_add_GamepadAdded(wgi_state.gamepad_statics, &gamepad_added.iface, &wgi_state.gamepad_added_token);
  618. if (!SUCCEEDED(hr)) {
  619. SDL_SetError("add_GamepadAdded() failed: 0x%lx\n", hr);
  620. }
  621. hr = __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_add_GamepadRemoved(wgi_state.gamepad_statics, &gamepad_removed.iface, &wgi_state.gamepad_removed_token);
  622. if (!SUCCEEDED(hr)) {
  623. SDL_SetError("add_GamepadRemoved() failed: 0x%lx\n", hr);
  624. }
  625. }
  626. }
  627. }
  628. }
  629. }
  630. static bool RAWINPUT_WindowsGamingInputSlotMatches(const WindowsMatchState *state, WindowsGamingInputGamepadState *slot, bool xinput_correlated)
  631. {
  632. Uint32 wgi_buttons = slot->state.Buttons;
  633. if ((wgi_buttons & 0x3FFF) == state->wgi_buttons
  634. #ifdef SDL_JOYSTICK_RAWINPUT_MATCH_AXES
  635. && WindowsGamingInputAxesMatch(slot->state)
  636. #endif
  637. #ifdef SDL_JOYSTICK_RAWINPUT_MATCH_TRIGGERS
  638. // Don't try to match WGI triggers if getting values from XInput
  639. && (xinput_correlated || WindowsGamingInputTriggersMatch(slot->state))
  640. #endif
  641. ) {
  642. return true;
  643. }
  644. return false;
  645. }
  646. static bool RAWINPUT_GuessWindowsGamingInputSlot(const WindowsMatchState *state, Uint8 *correlation_id, WindowsGamingInputGamepadState **slot, bool xinput_correlated)
  647. {
  648. int match_count, user_index;
  649. WindowsGamingInputGamepadState *gamepad_state = NULL;
  650. /* If there is only one available slot, let's use that
  651. * That will be right most of the time, and uncorrelation will fix any bad guesses
  652. */
  653. match_count = 0;
  654. for (user_index = 0; user_index < wgi_state.per_gamepad_count; ++user_index) {
  655. gamepad_state = wgi_state.per_gamepad[user_index];
  656. if (gamepad_state->connected && !gamepad_state->used) {
  657. *slot = gamepad_state;
  658. ++match_count;
  659. }
  660. }
  661. if (match_count == 1) {
  662. *correlation_id = ++gamepad_state->correlation_id;
  663. return true;
  664. }
  665. match_count = 0;
  666. for (user_index = 0; user_index < wgi_state.per_gamepad_count; ++user_index) {
  667. gamepad_state = wgi_state.per_gamepad[user_index];
  668. if (RAWINPUT_WindowsGamingInputSlotMatches(state, gamepad_state, xinput_correlated)) {
  669. ++match_count;
  670. *slot = gamepad_state;
  671. // Incrementing correlation_id for any match, as negative evidence for others being correlated
  672. *correlation_id = ++gamepad_state->correlation_id;
  673. }
  674. }
  675. /* Only return a match if we match exactly one, and we have some non-zero data (buttons or axes) that matched.
  676. Note that we're still invalidating *other* potential correlations if we have more than one match or we have no
  677. data. */
  678. if (match_count == 1 && state->any_data) {
  679. return true;
  680. }
  681. return false;
  682. }
  683. static void RAWINPUT_QuitWindowsGamingInput(RAWINPUT_DeviceContext *ctx)
  684. {
  685. --wgi_state.ref_count;
  686. if (!wgi_state.ref_count && wgi_state.initialized) {
  687. int ii;
  688. for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
  689. __x_ABI_CWindows_CGaming_CInput_CIGamepad_Release(wgi_state.per_gamepad[ii]->gamepad);
  690. }
  691. if (wgi_state.per_gamepad) {
  692. SDL_free(wgi_state.per_gamepad);
  693. wgi_state.per_gamepad = NULL;
  694. }
  695. wgi_state.per_gamepad_count = 0;
  696. if (wgi_state.gamepad_statics) {
  697. __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_remove_GamepadAdded(wgi_state.gamepad_statics, wgi_state.gamepad_added_token);
  698. __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_remove_GamepadRemoved(wgi_state.gamepad_statics, wgi_state.gamepad_removed_token);
  699. __x_ABI_CWindows_CGaming_CInput_CIGamepadStatics_Release(wgi_state.gamepad_statics);
  700. wgi_state.gamepad_statics = NULL;
  701. }
  702. WIN_RoUninitialize();
  703. wgi_state.initialized = false;
  704. }
  705. }
  706. #endif // SDL_JOYSTICK_RAWINPUT_WGI
  707. static SDL_RAWINPUT_Device *RAWINPUT_AcquireDevice(SDL_RAWINPUT_Device *device)
  708. {
  709. SDL_AtomicIncRef(&device->refcount);
  710. return device;
  711. }
  712. static void RAWINPUT_ReleaseDevice(SDL_RAWINPUT_Device *device)
  713. {
  714. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  715. if (device->joystick) {
  716. RAWINPUT_DeviceContext *ctx = device->joystick->hwdata;
  717. if (ctx->xinput_enabled && ctx->xinput_correlated) {
  718. RAWINPUT_MarkXInputSlotFree(ctx->xinput_slot);
  719. ctx->xinput_correlated = false;
  720. }
  721. }
  722. #endif // SDL_JOYSTICK_RAWINPUT_XINPUT
  723. if (SDL_AtomicDecRef(&device->refcount)) {
  724. SDL_free(device->preparsed_data);
  725. SDL_free(device->name);
  726. SDL_free(device->path);
  727. SDL_free(device);
  728. }
  729. }
  730. static SDL_RAWINPUT_Device *RAWINPUT_DeviceFromHandle(HANDLE hDevice)
  731. {
  732. SDL_RAWINPUT_Device *curr;
  733. for (curr = SDL_RAWINPUT_devices; curr; curr = curr->next) {
  734. if (curr->hDevice == hDevice) {
  735. return curr;
  736. }
  737. }
  738. return NULL;
  739. }
  740. static int GetSteamVirtualGamepadSlot(Uint16 vendor_id, Uint16 product_id, const char *device_path)
  741. {
  742. int slot = -1;
  743. // The format for the raw input device path is documented here:
  744. // https://partner.steamgames.com/doc/features/steam_controller/steam_input_gamepad_emulation_bestpractices
  745. if (vendor_id == USB_VENDOR_VALVE &&
  746. product_id == USB_PRODUCT_STEAM_VIRTUAL_GAMEPAD) {
  747. (void)SDL_sscanf(device_path, "\\\\.\\pipe\\HID#VID_045E&PID_028E&IG_00#%*X&%*X&%*X#%d#%*u", &slot);
  748. }
  749. return slot;
  750. }
  751. static void RAWINPUT_AddDevice(HANDLE hDevice)
  752. {
  753. #define CHECK(expression) \
  754. { \
  755. if (!(expression)) \
  756. goto err; \
  757. }
  758. SDL_RAWINPUT_Device *device = NULL;
  759. SDL_RAWINPUT_Device *curr, *last;
  760. RID_DEVICE_INFO rdi;
  761. UINT size;
  762. char dev_name[MAX_PATH] = { 0 };
  763. HANDLE hFile = INVALID_HANDLE_VALUE;
  764. // Make sure we're not trying to add the same device twice
  765. if (RAWINPUT_DeviceFromHandle(hDevice)) {
  766. return;
  767. }
  768. // Figure out what kind of device it is
  769. size = sizeof(rdi);
  770. SDL_zero(rdi);
  771. CHECK(GetRawInputDeviceInfoA(hDevice, RIDI_DEVICEINFO, &rdi, &size) != (UINT)-1);
  772. CHECK(rdi.dwType == RIM_TYPEHID);
  773. // Get the device "name" (HID Path)
  774. size = SDL_arraysize(dev_name);
  775. CHECK(GetRawInputDeviceInfoA(hDevice, RIDI_DEVICENAME, dev_name, &size) != (UINT)-1);
  776. // Only take XInput-capable devices
  777. CHECK(SDL_strstr(dev_name, "IG_") != NULL);
  778. CHECK(!SDL_ShouldIgnoreJoystick((Uint16)rdi.hid.dwVendorId, (Uint16)rdi.hid.dwProductId, (Uint16)rdi.hid.dwVersionNumber, ""));
  779. CHECK(!SDL_JoystickHandledByAnotherDriver(&SDL_RAWINPUT_JoystickDriver, (Uint16)rdi.hid.dwVendorId, (Uint16)rdi.hid.dwProductId, (Uint16)rdi.hid.dwVersionNumber, ""));
  780. device = (SDL_RAWINPUT_Device *)SDL_calloc(1, sizeof(SDL_RAWINPUT_Device));
  781. CHECK(device);
  782. device->hDevice = hDevice;
  783. device->vendor_id = (Uint16)rdi.hid.dwVendorId;
  784. device->product_id = (Uint16)rdi.hid.dwProductId;
  785. device->version = (Uint16)rdi.hid.dwVersionNumber;
  786. device->is_xinput = true;
  787. device->is_xboxone = SDL_IsJoystickXboxOne(device->vendor_id, device->product_id);
  788. device->steam_virtual_gamepad_slot = GetSteamVirtualGamepadSlot(device->vendor_id, device->product_id, dev_name);
  789. // Get HID Top-Level Collection Preparsed Data
  790. size = 0;
  791. CHECK(GetRawInputDeviceInfoA(hDevice, RIDI_PREPARSEDDATA, NULL, &size) != (UINT)-1);
  792. device->preparsed_data = (PHIDP_PREPARSED_DATA)SDL_calloc(size, sizeof(BYTE));
  793. CHECK(device->preparsed_data);
  794. CHECK(GetRawInputDeviceInfoA(hDevice, RIDI_PREPARSEDDATA, device->preparsed_data, &size) != (UINT)-1);
  795. hFile = CreateFileA(dev_name, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
  796. CHECK(hFile != INVALID_HANDLE_VALUE);
  797. {
  798. char *manufacturer_string = NULL;
  799. char *product_string = NULL;
  800. WCHAR string[128];
  801. if (SDL_HidD_GetManufacturerString(hFile, string, sizeof(string))) {
  802. manufacturer_string = WIN_StringToUTF8W(string);
  803. }
  804. if (SDL_HidD_GetProductString(hFile, string, sizeof(string))) {
  805. product_string = WIN_StringToUTF8W(string);
  806. }
  807. device->name = SDL_CreateJoystickName(device->vendor_id, device->product_id, manufacturer_string, product_string);
  808. device->guid = SDL_CreateJoystickGUID(SDL_HARDWARE_BUS_USB, device->vendor_id, device->product_id, device->version, manufacturer_string, product_string, 'r', 0);
  809. if (manufacturer_string) {
  810. SDL_free(manufacturer_string);
  811. }
  812. if (product_string) {
  813. SDL_free(product_string);
  814. }
  815. }
  816. device->path = SDL_strdup(dev_name);
  817. CloseHandle(hFile);
  818. hFile = INVALID_HANDLE_VALUE;
  819. device->joystick_id = SDL_GetNextObjectID();
  820. #ifdef DEBUG_RAWINPUT
  821. SDL_Log("Adding RAWINPUT device '%s' VID 0x%.4x, PID 0x%.4x, version %d, handle 0x%.8x", device->name, device->vendor_id, device->product_id, device->version, device->hDevice);
  822. #endif
  823. // Add it to the list
  824. RAWINPUT_AcquireDevice(device);
  825. for (curr = SDL_RAWINPUT_devices, last = NULL; curr; last = curr, curr = curr->next) {
  826. }
  827. if (last) {
  828. last->next = device;
  829. } else {
  830. SDL_RAWINPUT_devices = device;
  831. }
  832. ++SDL_RAWINPUT_numjoysticks;
  833. SDL_PrivateJoystickAdded(device->joystick_id);
  834. return;
  835. err:
  836. if (hFile != INVALID_HANDLE_VALUE) {
  837. CloseHandle(hFile);
  838. }
  839. if (device) {
  840. if (device->name) {
  841. SDL_free(device->name);
  842. }
  843. if (device->path) {
  844. SDL_free(device->path);
  845. }
  846. SDL_free(device);
  847. }
  848. #undef CHECK
  849. }
  850. static void RAWINPUT_DelDevice(SDL_RAWINPUT_Device *device, bool send_event)
  851. {
  852. SDL_RAWINPUT_Device *curr, *last;
  853. for (curr = SDL_RAWINPUT_devices, last = NULL; curr; last = curr, curr = curr->next) {
  854. if (curr == device) {
  855. if (last) {
  856. last->next = curr->next;
  857. } else {
  858. SDL_RAWINPUT_devices = curr->next;
  859. }
  860. --SDL_RAWINPUT_numjoysticks;
  861. SDL_PrivateJoystickRemoved(device->joystick_id);
  862. #ifdef DEBUG_RAWINPUT
  863. SDL_Log("Removing RAWINPUT device '%s' VID 0x%.4x, PID 0x%.4x, version %d, handle %p", device->name, device->vendor_id, device->product_id, device->version, device->hDevice);
  864. #endif
  865. RAWINPUT_ReleaseDevice(device);
  866. return;
  867. }
  868. }
  869. }
  870. static void RAWINPUT_DetectDevices(void)
  871. {
  872. UINT device_count = 0;
  873. if ((GetRawInputDeviceList(NULL, &device_count, sizeof(RAWINPUTDEVICELIST)) != -1) && device_count > 0) {
  874. PRAWINPUTDEVICELIST devices = NULL;
  875. UINT i;
  876. devices = (PRAWINPUTDEVICELIST)SDL_malloc(sizeof(RAWINPUTDEVICELIST) * device_count);
  877. if (devices) {
  878. device_count = GetRawInputDeviceList(devices, &device_count, sizeof(RAWINPUTDEVICELIST));
  879. if (device_count != (UINT)-1) {
  880. for (i = 0; i < device_count; ++i) {
  881. RAWINPUT_AddDevice(devices[i].hDevice);
  882. }
  883. }
  884. SDL_free(devices);
  885. }
  886. }
  887. }
  888. static void RAWINPUT_RemoveDevices(void)
  889. {
  890. while (SDL_RAWINPUT_devices) {
  891. RAWINPUT_DelDevice(SDL_RAWINPUT_devices, false);
  892. }
  893. SDL_assert(SDL_RAWINPUT_numjoysticks == 0);
  894. }
  895. static bool RAWINPUT_JoystickInit(void)
  896. {
  897. SDL_assert(!SDL_RAWINPUT_inited);
  898. if (!SDL_GetHintBoolean(SDL_HINT_JOYSTICK_RAWINPUT, true)) {
  899. return true;
  900. }
  901. if (!WIN_IsWindowsVistaOrGreater()) {
  902. // According to bug 6400, this doesn't work on Windows XP
  903. return false;
  904. }
  905. if (!WIN_LoadHIDDLL()) {
  906. return false;
  907. }
  908. SDL_RAWINPUT_inited = true;
  909. RAWINPUT_DetectDevices();
  910. return true;
  911. }
  912. static int RAWINPUT_JoystickGetCount(void)
  913. {
  914. return SDL_RAWINPUT_numjoysticks;
  915. }
  916. bool RAWINPUT_IsEnabled(void)
  917. {
  918. return SDL_RAWINPUT_inited && !SDL_RAWINPUT_remote_desktop;
  919. }
  920. static void RAWINPUT_PostUpdate(void)
  921. {
  922. #ifdef SDL_JOYSTICK_RAWINPUT_MATCHING
  923. bool unmapped_guide_pressed = false;
  924. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  925. if (!wgi_state.dirty) {
  926. int ii;
  927. for (ii = 0; ii < wgi_state.per_gamepad_count; ii++) {
  928. WindowsGamingInputGamepadState *gamepad_state = wgi_state.per_gamepad[ii];
  929. if (!gamepad_state->used && (gamepad_state->state.Buttons & GamepadButtons_GUIDE)) {
  930. unmapped_guide_pressed = true;
  931. break;
  932. }
  933. }
  934. }
  935. wgi_state.dirty = true;
  936. #endif
  937. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  938. if (!xinput_state_dirty) {
  939. int ii;
  940. for (ii = 0; ii < SDL_arraysize(xinput_state); ii++) {
  941. if (xinput_state[ii].connected && !xinput_state[ii].used && (xinput_state[ii].state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE)) {
  942. unmapped_guide_pressed = true;
  943. break;
  944. }
  945. }
  946. }
  947. xinput_state_dirty = true;
  948. #endif
  949. if (unmapped_guide_pressed) {
  950. if (guide_button_candidate.joystick && !guide_button_candidate.last_joystick) {
  951. SDL_Joystick *joystick = guide_button_candidate.joystick;
  952. RAWINPUT_DeviceContext *ctx = joystick->hwdata;
  953. if (ctx->guide_hack) {
  954. int guide_button = joystick->nbuttons - 1;
  955. SDL_SendJoystickButton(SDL_GetTicksNS(), guide_button_candidate.joystick, (Uint8)guide_button, true);
  956. }
  957. guide_button_candidate.last_joystick = guide_button_candidate.joystick;
  958. }
  959. } else if (guide_button_candidate.last_joystick) {
  960. SDL_Joystick *joystick = guide_button_candidate.last_joystick;
  961. RAWINPUT_DeviceContext *ctx = joystick->hwdata;
  962. if (ctx->guide_hack) {
  963. int guide_button = joystick->nbuttons - 1;
  964. SDL_SendJoystickButton(SDL_GetTicksNS(), joystick, (Uint8)guide_button, false);
  965. }
  966. guide_button_candidate.last_joystick = NULL;
  967. }
  968. guide_button_candidate.joystick = NULL;
  969. #endif // SDL_JOYSTICK_RAWINPUT_MATCHING
  970. }
  971. static void RAWINPUT_JoystickDetect(void)
  972. {
  973. bool remote_desktop;
  974. if (!SDL_RAWINPUT_inited) {
  975. return;
  976. }
  977. remote_desktop = GetSystemMetrics(SM_REMOTESESSION) ? true : false;
  978. if (remote_desktop != SDL_RAWINPUT_remote_desktop) {
  979. SDL_RAWINPUT_remote_desktop = remote_desktop;
  980. WINDOWS_RAWINPUTEnabledChanged();
  981. if (remote_desktop) {
  982. RAWINPUT_RemoveDevices();
  983. WINDOWS_JoystickDetect();
  984. } else {
  985. WINDOWS_JoystickDetect();
  986. RAWINPUT_DetectDevices();
  987. }
  988. }
  989. RAWINPUT_PostUpdate();
  990. }
  991. static bool RAWINPUT_JoystickIsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  992. {
  993. SDL_RAWINPUT_Device *device;
  994. // If we're being asked about a device, that means another API just detected one, so rescan
  995. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  996. xinput_device_change = true;
  997. #endif
  998. device = SDL_RAWINPUT_devices;
  999. while (device) {
  1000. if (vendor_id == device->vendor_id && product_id == device->product_id) {
  1001. return true;
  1002. }
  1003. /* The Xbox 360 wireless controller shows up as product 0 in WGI.
  1004. Try to match it to a Raw Input device via name or known product ID. */
  1005. if (vendor_id == device->vendor_id && product_id == 0 &&
  1006. ((name && SDL_strstr(device->name, name) != NULL) ||
  1007. (device->vendor_id == USB_VENDOR_MICROSOFT &&
  1008. device->product_id == USB_PRODUCT_XBOX360_XUSB_CONTROLLER))) {
  1009. return true;
  1010. }
  1011. // The Xbox One controller shows up as a hardcoded raw input VID/PID
  1012. if (name && SDL_strcmp(name, "Xbox One Game Controller") == 0 &&
  1013. device->vendor_id == USB_VENDOR_MICROSOFT &&
  1014. device->product_id == USB_PRODUCT_XBOX_ONE_XBOXGIP_CONTROLLER) {
  1015. return true;
  1016. }
  1017. device = device->next;
  1018. }
  1019. return false;
  1020. }
  1021. static SDL_RAWINPUT_Device *RAWINPUT_GetDeviceByIndex(int device_index)
  1022. {
  1023. SDL_RAWINPUT_Device *device = SDL_RAWINPUT_devices;
  1024. while (device) {
  1025. if (device_index == 0) {
  1026. break;
  1027. }
  1028. --device_index;
  1029. device = device->next;
  1030. }
  1031. return device;
  1032. }
  1033. static const char *RAWINPUT_JoystickGetDeviceName(int device_index)
  1034. {
  1035. return RAWINPUT_GetDeviceByIndex(device_index)->name;
  1036. }
  1037. static const char *RAWINPUT_JoystickGetDevicePath(int device_index)
  1038. {
  1039. return RAWINPUT_GetDeviceByIndex(device_index)->path;
  1040. }
  1041. static int RAWINPUT_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index)
  1042. {
  1043. return RAWINPUT_GetDeviceByIndex(device_index)->steam_virtual_gamepad_slot;
  1044. }
  1045. static int RAWINPUT_JoystickGetDevicePlayerIndex(int device_index)
  1046. {
  1047. return false;
  1048. }
  1049. static void RAWINPUT_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  1050. {
  1051. }
  1052. static SDL_GUID RAWINPUT_JoystickGetDeviceGUID(int device_index)
  1053. {
  1054. return RAWINPUT_GetDeviceByIndex(device_index)->guid;
  1055. }
  1056. static SDL_JoystickID RAWINPUT_JoystickGetDeviceInstanceID(int device_index)
  1057. {
  1058. return RAWINPUT_GetDeviceByIndex(device_index)->joystick_id;
  1059. }
  1060. static int SDLCALL RAWINPUT_SortValueCaps(const void *A, const void *B)
  1061. {
  1062. HIDP_VALUE_CAPS *capsA = (HIDP_VALUE_CAPS *)A;
  1063. HIDP_VALUE_CAPS *capsB = (HIDP_VALUE_CAPS *)B;
  1064. // Sort by Usage for single values, or UsageMax for range of values
  1065. return (int)capsA->NotRange.Usage - capsB->NotRange.Usage;
  1066. }
  1067. static bool RAWINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
  1068. {
  1069. SDL_RAWINPUT_Device *device = RAWINPUT_GetDeviceByIndex(device_index);
  1070. RAWINPUT_DeviceContext *ctx;
  1071. HIDP_CAPS caps;
  1072. HIDP_BUTTON_CAPS *button_caps;
  1073. HIDP_VALUE_CAPS *value_caps;
  1074. ULONG i;
  1075. ctx = (RAWINPUT_DeviceContext *)SDL_calloc(1, sizeof(RAWINPUT_DeviceContext));
  1076. if (!ctx) {
  1077. return false;
  1078. }
  1079. joystick->hwdata = ctx;
  1080. ctx->device = RAWINPUT_AcquireDevice(device);
  1081. device->joystick = joystick;
  1082. if (device->is_xinput) {
  1083. // We'll try to get guide button and trigger axes from XInput
  1084. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  1085. xinput_device_change = true;
  1086. ctx->xinput_enabled = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_RAWINPUT_CORRELATE_XINPUT, true);
  1087. if (ctx->xinput_enabled && (!WIN_LoadXInputDLL() || !XINPUTGETSTATE)) {
  1088. ctx->xinput_enabled = false;
  1089. }
  1090. ctx->xinput_slot = XUSER_INDEX_ANY;
  1091. #endif
  1092. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  1093. RAWINPUT_InitWindowsGamingInput(ctx);
  1094. #endif
  1095. }
  1096. ctx->is_xinput = device->is_xinput;
  1097. ctx->is_xboxone = device->is_xboxone;
  1098. #ifdef SDL_JOYSTICK_RAWINPUT_MATCHING
  1099. ctx->match_state = 0x0000008800000000ULL; // Trigger axes at rest
  1100. #endif
  1101. ctx->preparsed_data = device->preparsed_data;
  1102. ctx->max_data_length = SDL_HidP_MaxDataListLength(HidP_Input, ctx->preparsed_data);
  1103. ctx->data = (HIDP_DATA *)SDL_malloc(ctx->max_data_length * sizeof(*ctx->data));
  1104. if (!ctx->data) {
  1105. RAWINPUT_JoystickClose(joystick);
  1106. return false;
  1107. }
  1108. if (SDL_HidP_GetCaps(ctx->preparsed_data, &caps) != HIDP_STATUS_SUCCESS) {
  1109. RAWINPUT_JoystickClose(joystick);
  1110. return SDL_SetError("Couldn't get device capabilities");
  1111. }
  1112. button_caps = SDL_stack_alloc(HIDP_BUTTON_CAPS, caps.NumberInputButtonCaps);
  1113. if (SDL_HidP_GetButtonCaps(HidP_Input, button_caps, &caps.NumberInputButtonCaps, ctx->preparsed_data) != HIDP_STATUS_SUCCESS) {
  1114. RAWINPUT_JoystickClose(joystick);
  1115. return SDL_SetError("Couldn't get device button capabilities");
  1116. }
  1117. value_caps = SDL_stack_alloc(HIDP_VALUE_CAPS, caps.NumberInputValueCaps);
  1118. if (SDL_HidP_GetValueCaps(HidP_Input, value_caps, &caps.NumberInputValueCaps, ctx->preparsed_data) != HIDP_STATUS_SUCCESS) {
  1119. RAWINPUT_JoystickClose(joystick);
  1120. SDL_stack_free(button_caps);
  1121. return SDL_SetError("Couldn't get device value capabilities");
  1122. }
  1123. // Sort the axes by usage, so X comes before Y, etc.
  1124. SDL_qsort(value_caps, caps.NumberInputValueCaps, sizeof(*value_caps), RAWINPUT_SortValueCaps);
  1125. for (i = 0; i < caps.NumberInputButtonCaps; ++i) {
  1126. HIDP_BUTTON_CAPS *cap = &button_caps[i];
  1127. if (cap->UsagePage == USB_USAGEPAGE_BUTTON) {
  1128. int count;
  1129. if (cap->IsRange) {
  1130. count = 1 + (cap->Range.DataIndexMax - cap->Range.DataIndexMin);
  1131. } else {
  1132. count = 1;
  1133. }
  1134. joystick->nbuttons += count;
  1135. }
  1136. }
  1137. if (joystick->nbuttons > 0) {
  1138. int button_index = 0;
  1139. ctx->button_indices = (USHORT *)SDL_malloc(joystick->nbuttons * sizeof(*ctx->button_indices));
  1140. if (!ctx->button_indices) {
  1141. RAWINPUT_JoystickClose(joystick);
  1142. SDL_stack_free(value_caps);
  1143. SDL_stack_free(button_caps);
  1144. return false;
  1145. }
  1146. for (i = 0; i < caps.NumberInputButtonCaps; ++i) {
  1147. HIDP_BUTTON_CAPS *cap = &button_caps[i];
  1148. if (cap->UsagePage == USB_USAGEPAGE_BUTTON) {
  1149. if (cap->IsRange) {
  1150. int j, count = 1 + (cap->Range.DataIndexMax - cap->Range.DataIndexMin);
  1151. for (j = 0; j < count; ++j) {
  1152. ctx->button_indices[button_index++] = (USHORT)(cap->Range.DataIndexMin + j);
  1153. }
  1154. } else {
  1155. ctx->button_indices[button_index++] = cap->NotRange.DataIndex;
  1156. }
  1157. }
  1158. }
  1159. }
  1160. if (ctx->is_xinput && joystick->nbuttons == 10) {
  1161. ctx->guide_hack = true;
  1162. joystick->nbuttons += 1;
  1163. }
  1164. SDL_stack_free(button_caps);
  1165. for (i = 0; i < caps.NumberInputValueCaps; ++i) {
  1166. HIDP_VALUE_CAPS *cap = &value_caps[i];
  1167. if (cap->IsRange) {
  1168. continue;
  1169. }
  1170. if (ctx->trigger_hack && cap->NotRange.Usage == USB_USAGE_GENERIC_Z) {
  1171. continue;
  1172. }
  1173. if (cap->NotRange.Usage == USB_USAGE_GENERIC_HAT) {
  1174. joystick->nhats += 1;
  1175. continue;
  1176. }
  1177. if (ctx->is_xinput && cap->NotRange.Usage == USB_USAGE_GENERIC_Z) {
  1178. continue;
  1179. }
  1180. joystick->naxes += 1;
  1181. }
  1182. if (joystick->naxes > 0) {
  1183. int axis_index = 0;
  1184. ctx->axis_indices = (USHORT *)SDL_malloc(joystick->naxes * sizeof(*ctx->axis_indices));
  1185. if (!ctx->axis_indices) {
  1186. RAWINPUT_JoystickClose(joystick);
  1187. SDL_stack_free(value_caps);
  1188. return false;
  1189. }
  1190. for (i = 0; i < caps.NumberInputValueCaps; ++i) {
  1191. HIDP_VALUE_CAPS *cap = &value_caps[i];
  1192. if (cap->IsRange) {
  1193. continue;
  1194. }
  1195. if (cap->NotRange.Usage == USB_USAGE_GENERIC_HAT) {
  1196. continue;
  1197. }
  1198. if (ctx->is_xinput && cap->NotRange.Usage == USB_USAGE_GENERIC_Z) {
  1199. ctx->trigger_hack = true;
  1200. ctx->trigger_hack_index = cap->NotRange.DataIndex;
  1201. continue;
  1202. }
  1203. ctx->axis_indices[axis_index++] = cap->NotRange.DataIndex;
  1204. }
  1205. }
  1206. if (ctx->trigger_hack) {
  1207. joystick->naxes += 2;
  1208. }
  1209. if (joystick->nhats > 0) {
  1210. int hat_index = 0;
  1211. ctx->hat_indices = (USHORT *)SDL_malloc(joystick->nhats * sizeof(*ctx->hat_indices));
  1212. if (!ctx->hat_indices) {
  1213. RAWINPUT_JoystickClose(joystick);
  1214. SDL_stack_free(value_caps);
  1215. return false;
  1216. }
  1217. for (i = 0; i < caps.NumberInputValueCaps; ++i) {
  1218. HIDP_VALUE_CAPS *cap = &value_caps[i];
  1219. if (cap->IsRange) {
  1220. continue;
  1221. }
  1222. if (cap->NotRange.Usage != USB_USAGE_GENERIC_HAT) {
  1223. continue;
  1224. }
  1225. ctx->hat_indices[hat_index++] = cap->NotRange.DataIndex;
  1226. }
  1227. }
  1228. SDL_stack_free(value_caps);
  1229. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  1230. if (ctx->is_xinput) {
  1231. SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, true);
  1232. }
  1233. #endif
  1234. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  1235. if (ctx->is_xinput) {
  1236. SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, true);
  1237. if (ctx->is_xboxone) {
  1238. SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN, true);
  1239. }
  1240. }
  1241. #endif
  1242. return true;
  1243. }
  1244. static bool RAWINPUT_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  1245. {
  1246. #if defined(SDL_JOYSTICK_RAWINPUT_WGI) || defined(SDL_JOYSTICK_RAWINPUT_XINPUT)
  1247. RAWINPUT_DeviceContext *ctx = joystick->hwdata;
  1248. #endif
  1249. bool rumbled = false;
  1250. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  1251. // Prefer XInput over WGI because it allows rumble in the background
  1252. if (!rumbled && ctx->xinput_correlated) {
  1253. XINPUT_VIBRATION XVibration;
  1254. if (!XINPUTSETSTATE) {
  1255. return SDL_Unsupported();
  1256. }
  1257. XVibration.wLeftMotorSpeed = low_frequency_rumble;
  1258. XVibration.wRightMotorSpeed = high_frequency_rumble;
  1259. if (XINPUTSETSTATE(ctx->xinput_slot, &XVibration) == ERROR_SUCCESS) {
  1260. rumbled = true;
  1261. } else {
  1262. return SDL_SetError("XInputSetState() failed");
  1263. }
  1264. }
  1265. #endif // SDL_JOYSTICK_RAWINPUT_XINPUT
  1266. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  1267. if (!rumbled && ctx->wgi_correlated) {
  1268. WindowsGamingInputGamepadState *gamepad_state = ctx->wgi_slot;
  1269. HRESULT hr;
  1270. gamepad_state->vibration.LeftMotor = (DOUBLE)low_frequency_rumble / SDL_MAX_UINT16;
  1271. gamepad_state->vibration.RightMotor = (DOUBLE)high_frequency_rumble / SDL_MAX_UINT16;
  1272. hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_put_Vibration(gamepad_state->gamepad, gamepad_state->vibration);
  1273. if (SUCCEEDED(hr)) {
  1274. rumbled = true;
  1275. }
  1276. }
  1277. #endif
  1278. if (!rumbled) {
  1279. #if defined(SDL_JOYSTICK_RAWINPUT_WGI) || defined(SDL_JOYSTICK_RAWINPUT_XINPUT)
  1280. return SDL_SetError("Controller isn't correlated yet, try hitting a button first");
  1281. #else
  1282. return SDL_Unsupported();
  1283. #endif
  1284. }
  1285. return true;
  1286. }
  1287. static bool RAWINPUT_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  1288. {
  1289. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  1290. RAWINPUT_DeviceContext *ctx = joystick->hwdata;
  1291. if (ctx->wgi_correlated) {
  1292. WindowsGamingInputGamepadState *gamepad_state = ctx->wgi_slot;
  1293. HRESULT hr;
  1294. gamepad_state->vibration.LeftTrigger = (DOUBLE)left_rumble / SDL_MAX_UINT16;
  1295. gamepad_state->vibration.RightTrigger = (DOUBLE)right_rumble / SDL_MAX_UINT16;
  1296. hr = __x_ABI_CWindows_CGaming_CInput_CIGamepad_put_Vibration(gamepad_state->gamepad, gamepad_state->vibration);
  1297. if (!SUCCEEDED(hr)) {
  1298. return SDL_SetError("Setting vibration failed: 0x%lx\n", hr);
  1299. }
  1300. return true;
  1301. } else {
  1302. return SDL_SetError("Controller isn't correlated yet, try hitting a button first");
  1303. }
  1304. #else
  1305. return SDL_Unsupported();
  1306. #endif
  1307. }
  1308. static bool RAWINPUT_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  1309. {
  1310. return SDL_Unsupported();
  1311. }
  1312. static bool RAWINPUT_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
  1313. {
  1314. return SDL_Unsupported();
  1315. }
  1316. static bool RAWINPUT_JoystickSetSensorsEnabled(SDL_Joystick *joystick, bool enabled)
  1317. {
  1318. return SDL_Unsupported();
  1319. }
  1320. static HIDP_DATA *GetData(USHORT index, HIDP_DATA *data, ULONG length)
  1321. {
  1322. ULONG i;
  1323. // Check to see if the data is at the expected offset
  1324. if (index < length && data[index].DataIndex == index) {
  1325. return &data[index];
  1326. }
  1327. // Loop through the data to find it
  1328. for (i = 0; i < length; ++i) {
  1329. if (data[i].DataIndex == index) {
  1330. return &data[i];
  1331. }
  1332. }
  1333. return NULL;
  1334. }
  1335. /* This is the packet format for Xbox 360 and Xbox One controllers on Windows,
  1336. however with this interface there is no rumble support, no guide button,
  1337. and the left and right triggers are tied together as a single axis.
  1338. We use XInput and Windows.Gaming.Input to make up for these shortcomings.
  1339. */
  1340. static void RAWINPUT_HandleStatePacket(SDL_Joystick *joystick, Uint8 *data, int size)
  1341. {
  1342. RAWINPUT_DeviceContext *ctx = joystick->hwdata;
  1343. #ifdef SDL_JOYSTICK_RAWINPUT_MATCHING
  1344. // Map new buttons and axes into game controller controls
  1345. static const int button_map[] = {
  1346. SDL_GAMEPAD_BUTTON_SOUTH,
  1347. SDL_GAMEPAD_BUTTON_EAST,
  1348. SDL_GAMEPAD_BUTTON_WEST,
  1349. SDL_GAMEPAD_BUTTON_NORTH,
  1350. SDL_GAMEPAD_BUTTON_LEFT_SHOULDER,
  1351. SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER,
  1352. SDL_GAMEPAD_BUTTON_BACK,
  1353. SDL_GAMEPAD_BUTTON_START,
  1354. SDL_GAMEPAD_BUTTON_LEFT_STICK,
  1355. SDL_GAMEPAD_BUTTON_RIGHT_STICK
  1356. };
  1357. #define HAT_MASK ((1 << SDL_GAMEPAD_BUTTON_DPAD_UP) | (1 << SDL_GAMEPAD_BUTTON_DPAD_DOWN) | (1 << SDL_GAMEPAD_BUTTON_DPAD_LEFT) | (1 << SDL_GAMEPAD_BUTTON_DPAD_RIGHT))
  1358. static const int hat_map[] = {
  1359. 0,
  1360. (1 << SDL_GAMEPAD_BUTTON_DPAD_UP),
  1361. (1 << SDL_GAMEPAD_BUTTON_DPAD_UP) | (1 << SDL_GAMEPAD_BUTTON_DPAD_RIGHT),
  1362. (1 << SDL_GAMEPAD_BUTTON_DPAD_RIGHT),
  1363. (1 << SDL_GAMEPAD_BUTTON_DPAD_DOWN) | (1 << SDL_GAMEPAD_BUTTON_DPAD_RIGHT),
  1364. (1 << SDL_GAMEPAD_BUTTON_DPAD_DOWN),
  1365. (1 << SDL_GAMEPAD_BUTTON_DPAD_DOWN) | (1 << SDL_GAMEPAD_BUTTON_DPAD_LEFT),
  1366. (1 << SDL_GAMEPAD_BUTTON_DPAD_LEFT),
  1367. (1 << SDL_GAMEPAD_BUTTON_DPAD_UP) | (1 << SDL_GAMEPAD_BUTTON_DPAD_LEFT),
  1368. 0,
  1369. };
  1370. Uint64 match_state = ctx->match_state;
  1371. // Update match_state with button bit, then fall through
  1372. #define SDL_SendJoystickButton(timestamp, joystick, button, down) \
  1373. if (button < SDL_arraysize(button_map)) { \
  1374. Uint64 button_bit = 1ull << button_map[button]; \
  1375. match_state = (match_state & ~button_bit) | (button_bit * (down)); \
  1376. } \
  1377. SDL_SendJoystickButton(timestamp, joystick, button, down)
  1378. #ifdef SDL_JOYSTICK_RAWINPUT_MATCH_AXES
  1379. // Grab high 4 bits of value, then fall through
  1380. #define AddAxisToMatchState(axis, value) \
  1381. { \
  1382. match_state = (match_state & ~(0xFull << (4 * axis + 16))) | ((value)&0xF000ull) << (4 * axis + 4); \
  1383. }
  1384. #define SDL_SendJoystickAxis(timestamp, joystick, axis, value) \
  1385. if (axis < 4) \
  1386. AddAxisToMatchState(axis, value); \
  1387. SDL_SendJoystickAxis(timestamp, joystick, axis, value)
  1388. #endif
  1389. #endif // SDL_JOYSTICK_RAWINPUT_MATCHING
  1390. ULONG data_length = ctx->max_data_length;
  1391. int i;
  1392. int nbuttons = joystick->nbuttons - (ctx->guide_hack * 1);
  1393. int naxes = joystick->naxes - (ctx->trigger_hack * 2);
  1394. int nhats = joystick->nhats;
  1395. Uint32 button_mask = 0;
  1396. Uint64 timestamp = SDL_GetTicksNS();
  1397. if (SDL_HidP_GetData(HidP_Input, ctx->data, &data_length, ctx->preparsed_data, (PCHAR)data, size) != HIDP_STATUS_SUCCESS) {
  1398. return;
  1399. }
  1400. for (i = 0; i < nbuttons; ++i) {
  1401. HIDP_DATA *item = GetData(ctx->button_indices[i], ctx->data, data_length);
  1402. if (item && item->On) {
  1403. button_mask |= (1 << i);
  1404. }
  1405. }
  1406. for (i = 0; i < nbuttons; ++i) {
  1407. SDL_SendJoystickButton(timestamp, joystick, (Uint8)i, ((button_mask & (1 << i)) != 0));
  1408. }
  1409. for (i = 0; i < naxes; ++i) {
  1410. HIDP_DATA *item = GetData(ctx->axis_indices[i], ctx->data, data_length);
  1411. if (item) {
  1412. Sint16 axis = (int)(Uint16)item->RawValue - 0x8000;
  1413. SDL_SendJoystickAxis(timestamp, joystick, (Uint8)i, axis);
  1414. }
  1415. }
  1416. for (i = 0; i < nhats; ++i) {
  1417. HIDP_DATA *item = GetData(ctx->hat_indices[i], ctx->data, data_length);
  1418. if (item) {
  1419. Uint8 hat = SDL_HAT_CENTERED;
  1420. const Uint8 hat_states[] = {
  1421. SDL_HAT_CENTERED,
  1422. SDL_HAT_UP,
  1423. SDL_HAT_UP | SDL_HAT_RIGHT,
  1424. SDL_HAT_RIGHT,
  1425. SDL_HAT_DOWN | SDL_HAT_RIGHT,
  1426. SDL_HAT_DOWN,
  1427. SDL_HAT_DOWN | SDL_HAT_LEFT,
  1428. SDL_HAT_LEFT,
  1429. SDL_HAT_UP | SDL_HAT_LEFT,
  1430. SDL_HAT_CENTERED,
  1431. };
  1432. ULONG state = item->RawValue;
  1433. if (state < SDL_arraysize(hat_states)) {
  1434. #ifdef SDL_JOYSTICK_RAWINPUT_MATCHING
  1435. match_state = (match_state & ~HAT_MASK) | hat_map[state];
  1436. #endif
  1437. hat = hat_states[state];
  1438. }
  1439. SDL_SendJoystickHat(timestamp, joystick, (Uint8)i, hat);
  1440. }
  1441. }
  1442. #ifdef SDL_SendJoystickButton
  1443. #undef SDL_SendJoystickButton
  1444. #endif
  1445. #ifdef SDL_SendJoystickAxis
  1446. #undef SDL_SendJoystickAxis
  1447. #endif
  1448. #ifdef SDL_JOYSTICK_RAWINPUT_MATCH_TRIGGERS
  1449. #define AddTriggerToMatchState(axis, value) \
  1450. { \
  1451. int match_axis = axis + SDL_JOYSTICK_RAWINPUT_MATCH_COUNT - joystick->naxes; \
  1452. AddAxisToMatchState(match_axis, value); \
  1453. }
  1454. #endif // SDL_JOYSTICK_RAWINPUT_MATCH_TRIGGERS
  1455. if (ctx->trigger_hack) {
  1456. bool has_trigger_data = false;
  1457. int left_trigger = joystick->naxes - 2;
  1458. int right_trigger = joystick->naxes - 1;
  1459. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  1460. // Prefer XInput over WindowsGamingInput, it continues to provide data in the background
  1461. if (!has_trigger_data && ctx->xinput_enabled && ctx->xinput_correlated) {
  1462. has_trigger_data = true;
  1463. }
  1464. #endif // SDL_JOYSTICK_RAWINPUT_XINPUT
  1465. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  1466. if (!has_trigger_data && ctx->wgi_correlated) {
  1467. has_trigger_data = true;
  1468. }
  1469. #endif // SDL_JOYSTICK_RAWINPUT_WGI
  1470. #ifndef SDL_JOYSTICK_RAWINPUT_MATCH_TRIGGERS
  1471. if (!has_trigger_data)
  1472. #endif
  1473. {
  1474. HIDP_DATA *item = GetData(ctx->trigger_hack_index, ctx->data, data_length);
  1475. if (item) {
  1476. Sint16 value = (int)(Uint16)item->RawValue - 0x8000;
  1477. Sint16 left_value = (value > 0) ? (value * 2 - 32767) : SDL_MIN_SINT16;
  1478. Sint16 right_value = (value < 0) ? (-value * 2 - 32769) : SDL_MIN_SINT16;
  1479. #ifdef SDL_JOYSTICK_RAWINPUT_MATCH_TRIGGERS
  1480. AddTriggerToMatchState(left_trigger, left_value);
  1481. AddTriggerToMatchState(right_trigger, right_value);
  1482. if (!has_trigger_data)
  1483. #endif // SDL_JOYSTICK_RAWINPUT_MATCH_TRIGGERS
  1484. {
  1485. SDL_SendJoystickAxis(timestamp, joystick, (Uint8)left_trigger, left_value);
  1486. SDL_SendJoystickAxis(timestamp, joystick, (Uint8)right_trigger, right_value);
  1487. }
  1488. }
  1489. }
  1490. }
  1491. #ifdef AddAxisToMatchState
  1492. #undef AddAxisToMatchState
  1493. #endif
  1494. #ifdef AddTriggerToMatchState
  1495. #undef AddTriggerToMatchState
  1496. #endif
  1497. #ifdef SDL_JOYSTICK_RAWINPUT_MATCHING
  1498. if (ctx->is_xinput) {
  1499. ctx->match_state = match_state;
  1500. ctx->last_state_packet = SDL_GetTicks();
  1501. }
  1502. #endif
  1503. }
  1504. static void RAWINPUT_UpdateOtherAPIs(SDL_Joystick *joystick)
  1505. {
  1506. #ifdef SDL_JOYSTICK_RAWINPUT_MATCHING
  1507. RAWINPUT_DeviceContext *ctx = joystick->hwdata;
  1508. bool has_trigger_data = false;
  1509. bool correlated = false;
  1510. WindowsMatchState match_state_xinput;
  1511. int guide_button = joystick->nbuttons - 1;
  1512. int left_trigger = joystick->naxes - 2;
  1513. int right_trigger = joystick->naxes - 1;
  1514. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  1515. bool xinput_correlated;
  1516. #endif
  1517. RAWINPUT_FillMatchState(&match_state_xinput, ctx->match_state);
  1518. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  1519. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  1520. xinput_correlated = ctx->xinput_correlated;
  1521. #else
  1522. xinput_correlated = false;
  1523. #endif
  1524. // Parallel logic to WINDOWS_XINPUT below
  1525. RAWINPUT_UpdateWindowsGamingInput();
  1526. if (ctx->wgi_correlated &&
  1527. !joystick->low_frequency_rumble && !joystick->high_frequency_rumble &&
  1528. !joystick->left_trigger_rumble && !joystick->right_trigger_rumble) {
  1529. // We have been previously correlated, ensure we are still matching, see comments in XINPUT section
  1530. if (RAWINPUT_WindowsGamingInputSlotMatches(&match_state_xinput, ctx->wgi_slot, xinput_correlated)) {
  1531. ctx->wgi_uncorrelate_count = 0;
  1532. } else {
  1533. ++ctx->wgi_uncorrelate_count;
  1534. /* Only un-correlate if this is consistent over multiple Update() calls - the timing of polling/event
  1535. pumping can easily cause this to uncorrelate for a frame. 2 seemed reliable in my testing, but
  1536. let's set it to 5 to be safe. An incorrect un-correlation will simply result in lower precision
  1537. triggers for a frame. */
  1538. if (ctx->wgi_uncorrelate_count >= 5) {
  1539. #ifdef DEBUG_RAWINPUT
  1540. SDL_Log("UN-Correlated joystick %d to WindowsGamingInput device #%d", joystick->instance_id, ctx->wgi_slot);
  1541. #endif
  1542. RAWINPUT_MarkWindowsGamingInputSlotFree(ctx->wgi_slot);
  1543. ctx->wgi_correlated = false;
  1544. ctx->wgi_correlation_count = 0;
  1545. // Force release of Guide button, it can't possibly be down on this device now.
  1546. /* It gets left down if we were actually correlated incorrectly and it was released on the WindowsGamingInput
  1547. device but we didn't get a state packet. */
  1548. if (ctx->guide_hack) {
  1549. SDL_SendJoystickButton(0, joystick, (Uint8)guide_button, false);
  1550. }
  1551. }
  1552. }
  1553. }
  1554. if (!ctx->wgi_correlated) {
  1555. Uint8 new_correlation_count = 0;
  1556. if (RAWINPUT_MissingWindowsGamingInputSlot()) {
  1557. Uint8 correlation_id = 0;
  1558. WindowsGamingInputGamepadState *slot_idx = NULL;
  1559. if (RAWINPUT_GuessWindowsGamingInputSlot(&match_state_xinput, &correlation_id, &slot_idx, xinput_correlated)) {
  1560. // we match exactly one WindowsGamingInput device
  1561. /* Probably can do without wgi_correlation_count, just check and clear wgi_slot to NULL, unless we need
  1562. even more frames to be sure. */
  1563. if (ctx->wgi_correlation_count && ctx->wgi_slot == slot_idx) {
  1564. // was correlated previously, and still the same device
  1565. if (ctx->wgi_correlation_id + 1 == correlation_id) {
  1566. // no one else was correlated in the meantime
  1567. new_correlation_count = ctx->wgi_correlation_count + 1;
  1568. if (new_correlation_count == 2) {
  1569. // correlation stayed steady and uncontested across multiple frames, guaranteed match
  1570. ctx->wgi_correlated = true;
  1571. #ifdef DEBUG_RAWINPUT
  1572. SDL_Log("Correlated joystick %d to WindowsGamingInput device #%d", joystick->instance_id, slot_idx);
  1573. #endif
  1574. correlated = true;
  1575. RAWINPUT_MarkWindowsGamingInputSlotUsed(ctx->wgi_slot, ctx);
  1576. // If the generalized Guide button was using us, it doesn't need to anymore
  1577. if (guide_button_candidate.joystick == joystick) {
  1578. guide_button_candidate.joystick = NULL;
  1579. }
  1580. if (guide_button_candidate.last_joystick == joystick) {
  1581. guide_button_candidate.last_joystick = NULL;
  1582. }
  1583. }
  1584. } else {
  1585. // someone else also possibly correlated to this device, start over
  1586. new_correlation_count = 1;
  1587. }
  1588. } else {
  1589. // new possible correlation
  1590. new_correlation_count = 1;
  1591. ctx->wgi_slot = slot_idx;
  1592. }
  1593. ctx->wgi_correlation_id = correlation_id;
  1594. } else {
  1595. // Match multiple WindowsGamingInput devices, or none (possibly due to no buttons pressed)
  1596. }
  1597. }
  1598. ctx->wgi_correlation_count = new_correlation_count;
  1599. } else {
  1600. correlated = true;
  1601. }
  1602. #endif // SDL_JOYSTICK_RAWINPUT_WGI
  1603. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  1604. // Parallel logic to WINDOWS_GAMING_INPUT above
  1605. if (ctx->xinput_enabled) {
  1606. RAWINPUT_UpdateXInput();
  1607. if (ctx->xinput_correlated &&
  1608. !joystick->low_frequency_rumble && !joystick->high_frequency_rumble) {
  1609. // We have been previously correlated, ensure we are still matching
  1610. /* This is required to deal with two (mostly) un-preventable mis-correlation situations:
  1611. A) Since the HID data stream does not provide an initial state (but polling XInput does), if we open
  1612. 5 controllers (#1-4 XInput mapped, #5 is not), and controller 1 had the A button down (and we don't
  1613. know), and the user presses A on controller #5, we'll see exactly 1 controller with A down (#5) and
  1614. exactly 1 XInput device with A down (#1), and incorrectly correlate. This code will then un-correlate
  1615. when A is released from either controller #1 or #5.
  1616. B) Since the app may not open all controllers, we could have a similar situation where only controller #5
  1617. is opened, and the user holds A on controllers #1 and #5 simultaneously - again we see only 1 controller
  1618. with A down and 1 XInput device with A down, and incorrectly correlate. This should be very unusual
  1619. (only when apps do not open all controllers, yet are listening to Guide button presses, yet
  1620. for some reason want to ignore guide button presses on the un-opened controllers, yet users are
  1621. pressing buttons on the unopened controllers), and will resolve itself when either button is released
  1622. and we un-correlate. We could prevent this by processing the state packets for *all* controllers,
  1623. even un-opened ones, as that would allow more precise correlation.
  1624. */
  1625. if (RAWINPUT_XInputSlotMatches(&match_state_xinput, ctx->xinput_slot)) {
  1626. ctx->xinput_uncorrelate_count = 0;
  1627. } else {
  1628. ++ctx->xinput_uncorrelate_count;
  1629. /* Only un-correlate if this is consistent over multiple Update() calls - the timing of polling/event
  1630. pumping can easily cause this to uncorrelate for a frame. 2 seemed reliable in my testing, but
  1631. let's set it to 5 to be safe. An incorrect un-correlation will simply result in lower precision
  1632. triggers for a frame. */
  1633. if (ctx->xinput_uncorrelate_count >= 5) {
  1634. #ifdef DEBUG_RAWINPUT
  1635. SDL_Log("UN-Correlated joystick %d to XInput device #%d", joystick->instance_id, ctx->xinput_slot);
  1636. #endif
  1637. RAWINPUT_MarkXInputSlotFree(ctx->xinput_slot);
  1638. ctx->xinput_correlated = false;
  1639. ctx->xinput_correlation_count = 0;
  1640. // Force release of Guide button, it can't possibly be down on this device now.
  1641. /* It gets left down if we were actually correlated incorrectly and it was released on the XInput
  1642. device but we didn't get a state packet. */
  1643. if (ctx->guide_hack) {
  1644. SDL_SendJoystickButton(0, joystick, (Uint8)guide_button, false);
  1645. }
  1646. }
  1647. }
  1648. }
  1649. if (!ctx->xinput_correlated) {
  1650. Uint8 new_correlation_count = 0;
  1651. if (RAWINPUT_MissingXInputSlot()) {
  1652. Uint8 correlation_id = 0;
  1653. Uint8 slot_idx = 0;
  1654. if (RAWINPUT_GuessXInputSlot(&match_state_xinput, &correlation_id, &slot_idx)) {
  1655. // we match exactly one XInput device
  1656. /* Probably can do without xinput_correlation_count, just check and clear xinput_slot to ANY, unless
  1657. we need even more frames to be sure */
  1658. if (ctx->xinput_correlation_count && ctx->xinput_slot == slot_idx) {
  1659. // was correlated previously, and still the same device
  1660. if (ctx->xinput_correlation_id + 1 == correlation_id) {
  1661. // no one else was correlated in the meantime
  1662. new_correlation_count = ctx->xinput_correlation_count + 1;
  1663. if (new_correlation_count == 2) {
  1664. // correlation stayed steady and uncontested across multiple frames, guaranteed match
  1665. ctx->xinput_correlated = true;
  1666. #ifdef DEBUG_RAWINPUT
  1667. SDL_Log("Correlated joystick %d to XInput device #%d", joystick->instance_id, slot_idx);
  1668. #endif
  1669. correlated = true;
  1670. RAWINPUT_MarkXInputSlotUsed(ctx->xinput_slot);
  1671. // If the generalized Guide button was using us, it doesn't need to anymore
  1672. if (guide_button_candidate.joystick == joystick) {
  1673. guide_button_candidate.joystick = NULL;
  1674. }
  1675. if (guide_button_candidate.last_joystick == joystick) {
  1676. guide_button_candidate.last_joystick = NULL;
  1677. }
  1678. }
  1679. } else {
  1680. // someone else also possibly correlated to this device, start over
  1681. new_correlation_count = 1;
  1682. }
  1683. } else {
  1684. // new possible correlation
  1685. new_correlation_count = 1;
  1686. ctx->xinput_slot = slot_idx;
  1687. }
  1688. ctx->xinput_correlation_id = correlation_id;
  1689. } else {
  1690. // Match multiple XInput devices, or none (possibly due to no buttons pressed)
  1691. }
  1692. }
  1693. ctx->xinput_correlation_count = new_correlation_count;
  1694. } else {
  1695. correlated = true;
  1696. }
  1697. }
  1698. #endif // SDL_JOYSTICK_RAWINPUT_XINPUT
  1699. // Poll for trigger data once (not per-state-packet)
  1700. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  1701. // Prefer XInput over WindowsGamingInput, it continues to provide data in the background
  1702. if (!has_trigger_data && ctx->xinput_enabled && ctx->xinput_correlated) {
  1703. RAWINPUT_UpdateXInput();
  1704. if (xinput_state[ctx->xinput_slot].connected) {
  1705. XINPUT_BATTERY_INFORMATION_EX *battery_info = &xinput_state[ctx->xinput_slot].battery;
  1706. Uint64 timestamp;
  1707. if (ctx->guide_hack || ctx->trigger_hack) {
  1708. timestamp = SDL_GetTicksNS();
  1709. } else {
  1710. // timestamp won't be used
  1711. timestamp = 0;
  1712. }
  1713. if (ctx->guide_hack) {
  1714. bool down = ((xinput_state[ctx->xinput_slot].state.Gamepad.wButtons & XINPUT_GAMEPAD_GUIDE) != 0);
  1715. SDL_SendJoystickButton(timestamp, joystick, (Uint8)guide_button, down);
  1716. }
  1717. if (ctx->trigger_hack) {
  1718. SDL_SendJoystickAxis(timestamp, joystick, (Uint8)left_trigger, ((int)xinput_state[ctx->xinput_slot].state.Gamepad.bLeftTrigger * 257) - 32768);
  1719. SDL_SendJoystickAxis(timestamp, joystick, (Uint8)right_trigger, ((int)xinput_state[ctx->xinput_slot].state.Gamepad.bRightTrigger * 257) - 32768);
  1720. }
  1721. has_trigger_data = true;
  1722. SDL_PowerState state;
  1723. int percent;
  1724. switch (battery_info->BatteryType) {
  1725. case BATTERY_TYPE_WIRED:
  1726. state = SDL_POWERSTATE_CHARGING;
  1727. break;
  1728. case BATTERY_TYPE_UNKNOWN:
  1729. case BATTERY_TYPE_DISCONNECTED:
  1730. state = SDL_POWERSTATE_UNKNOWN;
  1731. break;
  1732. default:
  1733. state = SDL_POWERSTATE_ON_BATTERY;
  1734. break;
  1735. }
  1736. switch (battery_info->BatteryLevel) {
  1737. case BATTERY_LEVEL_EMPTY:
  1738. percent = 10;
  1739. break;
  1740. case BATTERY_LEVEL_LOW:
  1741. percent = 40;
  1742. break;
  1743. case BATTERY_LEVEL_MEDIUM:
  1744. percent = 70;
  1745. break;
  1746. default:
  1747. case BATTERY_LEVEL_FULL:
  1748. percent = 100;
  1749. break;
  1750. }
  1751. SDL_SendJoystickPowerInfo(joystick, state, percent);
  1752. }
  1753. }
  1754. #endif // SDL_JOYSTICK_RAWINPUT_XINPUT
  1755. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  1756. if (!has_trigger_data && ctx->wgi_correlated) {
  1757. RAWINPUT_UpdateWindowsGamingInput(); // May detect disconnect / cause uncorrelation
  1758. if (ctx->wgi_correlated) { // Still connected
  1759. struct __x_ABI_CWindows_CGaming_CInput_CGamepadReading *state = &ctx->wgi_slot->state;
  1760. Uint64 timestamp;
  1761. if (ctx->guide_hack || ctx->trigger_hack) {
  1762. timestamp = SDL_GetTicksNS();
  1763. } else {
  1764. // timestamp won't be used
  1765. timestamp = 0;
  1766. }
  1767. if (ctx->guide_hack) {
  1768. bool down = ((state->Buttons & GamepadButtons_GUIDE) != 0);
  1769. SDL_SendJoystickButton(timestamp, joystick, (Uint8)guide_button, down);
  1770. }
  1771. if (ctx->trigger_hack) {
  1772. SDL_SendJoystickAxis(timestamp, joystick, (Uint8)left_trigger, (Sint16)(((int)(state->LeftTrigger * SDL_MAX_UINT16)) - 32768));
  1773. SDL_SendJoystickAxis(timestamp, joystick, (Uint8)right_trigger, (Sint16)(((int)(state->RightTrigger * SDL_MAX_UINT16)) - 32768));
  1774. }
  1775. has_trigger_data = true;
  1776. }
  1777. }
  1778. #endif // SDL_JOYSTICK_RAWINPUT_WGI
  1779. if (!correlated) {
  1780. if (!guide_button_candidate.joystick ||
  1781. (ctx->last_state_packet && (!guide_button_candidate.last_state_packet ||
  1782. ctx->last_state_packet >= guide_button_candidate.last_state_packet))) {
  1783. guide_button_candidate.joystick = joystick;
  1784. guide_button_candidate.last_state_packet = ctx->last_state_packet;
  1785. }
  1786. }
  1787. #endif // SDL_JOYSTICK_RAWINPUT_MATCHING
  1788. }
  1789. static void RAWINPUT_JoystickUpdate(SDL_Joystick *joystick)
  1790. {
  1791. RAWINPUT_UpdateOtherAPIs(joystick);
  1792. }
  1793. static void RAWINPUT_JoystickClose(SDL_Joystick *joystick)
  1794. {
  1795. RAWINPUT_DeviceContext *ctx = joystick->hwdata;
  1796. #ifdef SDL_JOYSTICK_RAWINPUT_MATCHING
  1797. if (guide_button_candidate.joystick == joystick) {
  1798. guide_button_candidate.joystick = NULL;
  1799. }
  1800. if (guide_button_candidate.last_joystick == joystick) {
  1801. guide_button_candidate.last_joystick = NULL;
  1802. }
  1803. #endif
  1804. if (ctx) {
  1805. SDL_RAWINPUT_Device *device;
  1806. #ifdef SDL_JOYSTICK_RAWINPUT_XINPUT
  1807. xinput_device_change = true;
  1808. if (ctx->xinput_enabled) {
  1809. if (ctx->xinput_correlated) {
  1810. RAWINPUT_MarkXInputSlotFree(ctx->xinput_slot);
  1811. }
  1812. WIN_UnloadXInputDLL();
  1813. }
  1814. #endif
  1815. #ifdef SDL_JOYSTICK_RAWINPUT_WGI
  1816. RAWINPUT_QuitWindowsGamingInput(ctx);
  1817. #endif
  1818. device = ctx->device;
  1819. if (device) {
  1820. SDL_assert(device->joystick == joystick);
  1821. device->joystick = NULL;
  1822. RAWINPUT_ReleaseDevice(device);
  1823. }
  1824. SDL_free(ctx->data);
  1825. SDL_free(ctx->button_indices);
  1826. SDL_free(ctx->axis_indices);
  1827. SDL_free(ctx->hat_indices);
  1828. SDL_free(ctx);
  1829. joystick->hwdata = NULL;
  1830. }
  1831. }
  1832. bool RAWINPUT_RegisterNotifications(HWND hWnd)
  1833. {
  1834. int i;
  1835. RAWINPUTDEVICE rid[SDL_arraysize(subscribed_devices)];
  1836. if (!SDL_RAWINPUT_inited) {
  1837. return true;
  1838. }
  1839. for (i = 0; i < SDL_arraysize(subscribed_devices); i++) {
  1840. rid[i].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP;
  1841. rid[i].usUsage = subscribed_devices[i];
  1842. rid[i].dwFlags = RIDEV_DEVNOTIFY | RIDEV_INPUTSINK; // Receive messages when in background, including device add/remove
  1843. rid[i].hwndTarget = hWnd;
  1844. }
  1845. if (!RegisterRawInputDevices(rid, SDL_arraysize(rid), sizeof(RAWINPUTDEVICE))) {
  1846. return SDL_SetError("Couldn't register for raw input events");
  1847. }
  1848. return true;
  1849. }
  1850. bool RAWINPUT_UnregisterNotifications(void)
  1851. {
  1852. int i;
  1853. RAWINPUTDEVICE rid[SDL_arraysize(subscribed_devices)];
  1854. if (!SDL_RAWINPUT_inited) {
  1855. return true;
  1856. }
  1857. for (i = 0; i < SDL_arraysize(subscribed_devices); i++) {
  1858. rid[i].usUsagePage = USB_USAGEPAGE_GENERIC_DESKTOP;
  1859. rid[i].usUsage = subscribed_devices[i];
  1860. rid[i].dwFlags = RIDEV_REMOVE;
  1861. rid[i].hwndTarget = NULL;
  1862. }
  1863. if (!RegisterRawInputDevices(rid, SDL_arraysize(rid), sizeof(RAWINPUTDEVICE))) {
  1864. return SDL_SetError("Couldn't unregister for raw input events");
  1865. }
  1866. return true;
  1867. }
  1868. LRESULT CALLBACK
  1869. RAWINPUT_WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  1870. {
  1871. LRESULT result = -1;
  1872. if (SDL_RAWINPUT_inited) {
  1873. SDL_LockJoysticks();
  1874. switch (msg) {
  1875. case WM_INPUT_DEVICE_CHANGE:
  1876. {
  1877. HANDLE hDevice = (HANDLE)lParam;
  1878. switch (wParam) {
  1879. case GIDC_ARRIVAL:
  1880. RAWINPUT_AddDevice(hDevice);
  1881. break;
  1882. case GIDC_REMOVAL:
  1883. {
  1884. SDL_RAWINPUT_Device *device;
  1885. device = RAWINPUT_DeviceFromHandle(hDevice);
  1886. if (device) {
  1887. RAWINPUT_DelDevice(device, true);
  1888. }
  1889. break;
  1890. }
  1891. default:
  1892. break;
  1893. }
  1894. }
  1895. result = 0;
  1896. break;
  1897. case WM_INPUT:
  1898. {
  1899. Uint8 data[sizeof(RAWINPUTHEADER) + sizeof(RAWHID) + USB_PACKET_LENGTH];
  1900. UINT buffer_size = SDL_arraysize(data);
  1901. if ((int)GetRawInputData((HRAWINPUT)lParam, RID_INPUT, data, &buffer_size, sizeof(RAWINPUTHEADER)) > 0) {
  1902. PRAWINPUT raw_input = (PRAWINPUT)data;
  1903. SDL_RAWINPUT_Device *device = RAWINPUT_DeviceFromHandle(raw_input->header.hDevice);
  1904. if (device) {
  1905. SDL_Joystick *joystick = device->joystick;
  1906. if (joystick) {
  1907. RAWINPUT_HandleStatePacket(joystick, raw_input->data.hid.bRawData, raw_input->data.hid.dwSizeHid);
  1908. }
  1909. }
  1910. }
  1911. }
  1912. result = 0;
  1913. break;
  1914. }
  1915. SDL_UnlockJoysticks();
  1916. }
  1917. if (result >= 0) {
  1918. return result;
  1919. }
  1920. return CallWindowProc(DefWindowProc, hWnd, msg, wParam, lParam);
  1921. }
  1922. static void RAWINPUT_JoystickQuit(void)
  1923. {
  1924. if (!SDL_RAWINPUT_inited) {
  1925. return;
  1926. }
  1927. RAWINPUT_RemoveDevices();
  1928. WIN_UnloadHIDDLL();
  1929. SDL_RAWINPUT_inited = false;
  1930. }
  1931. static bool RAWINPUT_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  1932. {
  1933. return false;
  1934. }
  1935. SDL_JoystickDriver SDL_RAWINPUT_JoystickDriver = {
  1936. RAWINPUT_JoystickInit,
  1937. RAWINPUT_JoystickGetCount,
  1938. RAWINPUT_JoystickDetect,
  1939. RAWINPUT_JoystickIsDevicePresent,
  1940. RAWINPUT_JoystickGetDeviceName,
  1941. RAWINPUT_JoystickGetDevicePath,
  1942. RAWINPUT_JoystickGetDeviceSteamVirtualGamepadSlot,
  1943. RAWINPUT_JoystickGetDevicePlayerIndex,
  1944. RAWINPUT_JoystickSetDevicePlayerIndex,
  1945. RAWINPUT_JoystickGetDeviceGUID,
  1946. RAWINPUT_JoystickGetDeviceInstanceID,
  1947. RAWINPUT_JoystickOpen,
  1948. RAWINPUT_JoystickRumble,
  1949. RAWINPUT_JoystickRumbleTriggers,
  1950. RAWINPUT_JoystickSetLED,
  1951. RAWINPUT_JoystickSendEffect,
  1952. RAWINPUT_JoystickSetSensorsEnabled,
  1953. RAWINPUT_JoystickUpdate,
  1954. RAWINPUT_JoystickClose,
  1955. RAWINPUT_JoystickQuit,
  1956. RAWINPUT_JoystickGetGamepadMapping
  1957. };
  1958. #endif // SDL_JOYSTICK_RAWINPUT