SDL_rawinputjoystick.c 76 KB

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