SDL_rawinputjoystick.c 68 KB

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