SDL_rawinputjoystick.c 76 KB

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