SDL_rawinputjoystick.c 74 KB

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