SDL_rawinputjoystick.c 71 KB

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