SDL_gameinputjoystick.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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. #include "SDL_internal.h"
  19. #ifdef SDL_JOYSTICK_GAMEINPUT
  20. #include "../SDL_sysjoystick.h"
  21. #include "../usb_ids.h"
  22. #include "../../core/windows/SDL_windows.h"
  23. #include "../../core/windows/SDL_gameinput.h"
  24. // Default value for SDL_HINT_JOYSTICK_GAMEINPUT
  25. #if defined(SDL_PLATFORM_GDK)
  26. #define SDL_GAMEINPUT_DEFAULT true
  27. #else
  28. #define SDL_GAMEINPUT_DEFAULT false
  29. #endif
  30. // Enable sensor support in GameInput 2.0, once we have a device that can be used for testing
  31. #if GAMEINPUT_API_VERSION >= 2
  32. //#define GAMEINPUT_SENSOR_SUPPORT
  33. #endif
  34. enum
  35. {
  36. SDL_GAMEPAD_BUTTON_GAMEINPUT_SHARE = 11
  37. };
  38. typedef struct GAMEINPUT_InternalDevice
  39. {
  40. IGameInputDevice *device;
  41. char path[(APP_LOCAL_DEVICE_ID_SIZE * 2) + 1];
  42. char *name;
  43. SDL_GUID guid; // generated by SDL
  44. SDL_JoystickID device_instance; // generated by SDL
  45. const GameInputDeviceInfo *info;
  46. int steam_virtual_gamepad_slot;
  47. bool isAdded;
  48. bool isDeleteRequested;
  49. } GAMEINPUT_InternalDevice;
  50. typedef struct GAMEINPUT_InternalList
  51. {
  52. GAMEINPUT_InternalDevice **devices;
  53. int count;
  54. } GAMEINPUT_InternalList;
  55. typedef struct joystick_hwdata
  56. {
  57. GAMEINPUT_InternalDevice *devref;
  58. bool report_sensors;
  59. GameInputRumbleParams rumbleParams;
  60. GameInputCallbackToken system_button_callback_token;
  61. } GAMEINPUT_InternalJoystickHwdata;
  62. static GAMEINPUT_InternalList g_GameInputList = { NULL };
  63. static IGameInput *g_pGameInput = NULL;
  64. static GameInputCallbackToken g_GameInputCallbackToken = 0;
  65. static Uint64 g_GameInputTimestampOffset;
  66. extern "C"
  67. {
  68. extern bool SDL_XINPUT_Enabled(void);
  69. }
  70. static bool GAMEINPUT_InternalIsGamepad(const GameInputDeviceInfo *info)
  71. {
  72. if (info->supportedInput & GameInputKindGamepad) {
  73. return true;
  74. }
  75. return false;
  76. }
  77. static Uint8 GAMEINPUT_GetDeviceSubtype(const GameInputDeviceInfo *info)
  78. {
  79. GameInputKind supportedInput = info->supportedInput;
  80. if (supportedInput & GameInputKindRacingWheel) {
  81. return SDL_JOYSTICK_TYPE_WHEEL;
  82. }
  83. if (supportedInput & GameInputKindArcadeStick) {
  84. return SDL_JOYSTICK_TYPE_ARCADE_STICK;
  85. }
  86. if (supportedInput & GameInputKindFlightStick) {
  87. return SDL_JOYSTICK_TYPE_FLIGHT_STICK;
  88. }
  89. if (supportedInput & (GameInputKindGamepad | GameInputKindController)) {
  90. return SDL_JOYSTICK_TYPE_GAMEPAD;
  91. }
  92. // Other device subtypes don't have their own GameInputKind enum entries.
  93. return 0;
  94. }
  95. #if GAMEINPUT_API_VERSION >= 1
  96. static int GetSteamVirtualGamepadSlot(const char *device_path)
  97. {
  98. int slot = -1;
  99. // The format for the raw input device path is documented here:
  100. // https://partner.steamgames.com/doc/features/steam_controller/steam_input_gamepad_emulation_bestpractices
  101. (void)SDL_sscanf(device_path, "\\\\.\\pipe\\HID#VID_045E&PID_028E&IG_00#%*X&%*X&%*X#%d#%*u", &slot);
  102. return slot;
  103. }
  104. #endif // GAMEINPUT_API_VERSION >= 1
  105. static bool GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
  106. {
  107. GAMEINPUT_InternalDevice **devicelist = NULL;
  108. GAMEINPUT_InternalDevice *elem = NULL;
  109. const GameInputDeviceInfo *info = NULL;
  110. Uint16 bus = SDL_HARDWARE_BUS_USB;
  111. Uint16 vendor = 0;
  112. Uint16 product = 0;
  113. Uint16 version = 0;
  114. const char *product_string = NULL;
  115. Uint8 driver_signature = 'g';
  116. Uint8 subtype = 0;
  117. char tmp[4];
  118. int idx = 0;
  119. SDL_AssertJoysticksLocked();
  120. #if GAMEINPUT_API_VERSION >= 1
  121. HRESULT hr = pDevice->GetDeviceInfo(&info);
  122. if (FAILED(hr)) {
  123. return WIN_SetErrorFromHRESULT("IGameInputDevice::GetDeviceInfo", hr);
  124. }
  125. #else
  126. info = pDevice->GetDeviceInfo();
  127. #endif
  128. if (false /*info->capabilities & GameInputDeviceCapabilityWireless*/) {
  129. bus = SDL_HARDWARE_BUS_BLUETOOTH;
  130. } else {
  131. bus = SDL_HARDWARE_BUS_USB;
  132. }
  133. vendor = info->vendorId;
  134. product = info->productId;
  135. //version = (info->firmwareVersion.major << 8) | info->firmwareVersion.minor;
  136. subtype = GAMEINPUT_GetDeviceSubtype(info);
  137. #if GAMEINPUT_API_VERSION >= 1
  138. if (info->displayName) {
  139. product_string = info->displayName;
  140. }
  141. #else
  142. if (info->displayName) {
  143. product_string = info->displayName->data;
  144. }
  145. #endif
  146. if (SDL_ShouldIgnoreJoystick(vendor, product, version, product_string) ||
  147. SDL_JoystickHandledByAnotherDriver(&SDL_GAMEINPUT_JoystickDriver, vendor, product, version, product_string)) {
  148. return true;
  149. }
  150. #if defined(SDL_JOYSTICK_DINPUT) && defined(SDL_HAPTIC_DINPUT)
  151. // This joystick backend currently doesn't provide a haptic backend, so fallback to DirectInput for haptic-capable devices.
  152. if (SDL_GetHintBoolean(SDL_HINT_JOYSTICK_DIRECTINPUT, true) && info->forceFeedbackMotorCount > 0 && pDevice->IsForceFeedbackMotorPoweredOn(0)) {
  153. return true;
  154. }
  155. #endif
  156. if (!GAMEINPUT_InternalIsGamepad(info)) {
  157. #if defined(SDL_JOYSTICK_DINPUT) || defined(SDL_JOYSTICK_XINPUT)
  158. if (SDL_GetHintBoolean(SDL_HINT_JOYSTICK_DIRECTINPUT, true) || SDL_XINPUT_Enabled()) {
  159. // Let other backends handle non-gamepad controllers to possibly avoid bugs and/or regressions.
  160. return true;
  161. }
  162. #endif
  163. if (info->supportedInput & GameInputKindController) {
  164. // Maintain GUID compatibility with DirectInput controller mappings.
  165. driver_signature = 0;
  166. subtype = 0;
  167. } else {
  168. // This joystick backend currently doesn't provide proper reading of other joystick types.
  169. return true;
  170. }
  171. }
  172. for (idx = 0; idx < g_GameInputList.count; ++idx) {
  173. elem = g_GameInputList.devices[idx];
  174. if (elem && elem->device == pDevice) {
  175. // we're already added
  176. elem->isDeleteRequested = false;
  177. return true;
  178. }
  179. }
  180. elem = (GAMEINPUT_InternalDevice *)SDL_calloc(1, sizeof(*elem));
  181. if (!elem) {
  182. return false;
  183. }
  184. devicelist = (GAMEINPUT_InternalDevice **)SDL_realloc(g_GameInputList.devices, sizeof(elem) * (g_GameInputList.count + 1LL));
  185. if (!devicelist) {
  186. SDL_free(elem);
  187. return false;
  188. }
  189. // Generate a device path
  190. for (idx = 0; idx < APP_LOCAL_DEVICE_ID_SIZE; ++idx) {
  191. SDL_snprintf(tmp, SDL_arraysize(tmp), "%02hhX", info->deviceId.value[idx]);
  192. SDL_strlcat(elem->path, tmp, SDL_arraysize(elem->path));
  193. }
  194. pDevice->AddRef();
  195. elem->device = pDevice;
  196. elem->name = SDL_CreateJoystickName(vendor, product, NULL, product_string);
  197. elem->guid = SDL_CreateJoystickGUID(bus, vendor, product, version, NULL, product_string, driver_signature, subtype);
  198. elem->device_instance = SDL_GetNextObjectID();
  199. elem->info = info;
  200. #if GAMEINPUT_API_VERSION >= 1
  201. elem->steam_virtual_gamepad_slot = GetSteamVirtualGamepadSlot(info->pnpPath);
  202. #else
  203. elem->steam_virtual_gamepad_slot = -1;
  204. #endif
  205. g_GameInputList.devices = devicelist;
  206. g_GameInputList.devices[g_GameInputList.count++] = elem;
  207. return true;
  208. }
  209. static bool GAMEINPUT_InternalRemoveByIndex(int idx)
  210. {
  211. GAMEINPUT_InternalDevice **devicelist = NULL;
  212. GAMEINPUT_InternalDevice *elem;
  213. int bytes = 0;
  214. SDL_AssertJoysticksLocked();
  215. if (idx < 0 || idx >= g_GameInputList.count) {
  216. return SDL_SetError("GAMEINPUT_InternalRemoveByIndex argument idx %d is out of range", idx);
  217. }
  218. elem = g_GameInputList.devices[idx];
  219. if (elem) {
  220. elem->device->Release();
  221. SDL_free(elem->name);
  222. SDL_free(elem);
  223. }
  224. g_GameInputList.devices[idx] = NULL;
  225. if (g_GameInputList.count == 1) {
  226. // last element in the list, free the entire list then
  227. SDL_free(g_GameInputList.devices);
  228. g_GameInputList.devices = NULL;
  229. } else {
  230. if (idx != g_GameInputList.count - 1) {
  231. bytes = sizeof(*devicelist) * (g_GameInputList.count - idx);
  232. SDL_memmove(&g_GameInputList.devices[idx], &g_GameInputList.devices[idx + 1], bytes);
  233. }
  234. }
  235. // decrement the count and return
  236. --g_GameInputList.count;
  237. return true;
  238. }
  239. static GAMEINPUT_InternalDevice *GAMEINPUT_InternalFindByIndex(int idx)
  240. {
  241. // We're guaranteed that the index is in range when this is called
  242. SDL_AssertJoysticksLocked();
  243. return g_GameInputList.devices[idx];
  244. }
  245. static void CALLBACK GAMEINPUT_InternalJoystickDeviceCallback(
  246. _In_ GameInputCallbackToken callbackToken,
  247. _In_ void *context,
  248. _In_ IGameInputDevice *device,
  249. _In_ uint64_t timestamp,
  250. _In_ GameInputDeviceStatus currentStatus,
  251. _In_ GameInputDeviceStatus previousStatus)
  252. {
  253. int idx = 0;
  254. GAMEINPUT_InternalDevice *elem = NULL;
  255. if (!device) {
  256. // This should never happen, but ignore it if it does
  257. return;
  258. }
  259. SDL_LockJoysticks();
  260. if (currentStatus & GameInputDeviceConnected) {
  261. GAMEINPUT_InternalAddOrFind(device);
  262. } else {
  263. for (idx = 0; idx < g_GameInputList.count; ++idx) {
  264. elem = g_GameInputList.devices[idx];
  265. if (elem && elem->device == device) {
  266. // will be deleted on the next Detect call
  267. elem->isDeleteRequested = true;
  268. break;
  269. }
  270. }
  271. }
  272. SDL_UnlockJoysticks();
  273. }
  274. static void GAMEINPUT_JoystickDetect(void);
  275. static void GAMEINPUT_JoystickQuit(void);
  276. static bool GAMEINPUT_JoystickInit(void)
  277. {
  278. HRESULT hr;
  279. if (!SDL_GetHintBoolean(SDL_HINT_JOYSTICK_GAMEINPUT, SDL_GAMEINPUT_DEFAULT)) {
  280. return true;
  281. }
  282. if (!SDL_InitGameInput(&g_pGameInput)) {
  283. return false;
  284. }
  285. #if GAMEINPUT_API_VERSION >= 2
  286. // Allow background controller input
  287. // SDL manages focus policy at a higher level, so we can set this unconditionally.
  288. g_pGameInput->SetFocusPolicy(GameInputEnableBackgroundInput | GameInputEnableBackgroundGuideButton | GameInputEnableBackgroundShareButton);
  289. #endif
  290. hr = g_pGameInput->RegisterDeviceCallback(NULL,
  291. GameInputKindController,
  292. GameInputDeviceConnected,
  293. GameInputBlockingEnumeration,
  294. NULL,
  295. GAMEINPUT_InternalJoystickDeviceCallback,
  296. &g_GameInputCallbackToken);
  297. if (FAILED(hr)) {
  298. GAMEINPUT_JoystickQuit();
  299. return WIN_SetErrorFromHRESULT("IGameInput::RegisterDeviceCallback", hr);
  300. }
  301. // Calculate the relative offset between SDL timestamps and GameInput timestamps
  302. Uint64 now = SDL_GetTicksNS();
  303. uint64_t timestampUS = g_pGameInput->GetCurrentTimestamp();
  304. g_GameInputTimestampOffset = (SDL_NS_TO_US(now) - timestampUS);
  305. GAMEINPUT_JoystickDetect();
  306. return true;
  307. }
  308. static int GAMEINPUT_JoystickGetCount(void)
  309. {
  310. SDL_AssertJoysticksLocked();
  311. return g_GameInputList.count;
  312. }
  313. static void GAMEINPUT_JoystickDetect(void)
  314. {
  315. int idx;
  316. GAMEINPUT_InternalDevice *elem = NULL;
  317. SDL_AssertJoysticksLocked();
  318. for (idx = 0; idx < g_GameInputList.count; ++idx) {
  319. elem = g_GameInputList.devices[idx];
  320. if (!elem) {
  321. continue;
  322. }
  323. if (!elem->isAdded) {
  324. SDL_PrivateJoystickAdded(elem->device_instance);
  325. elem->isAdded = true;
  326. }
  327. if (elem->isDeleteRequested || !(elem->device->GetDeviceStatus() & GameInputDeviceConnected)) {
  328. SDL_PrivateJoystickRemoved(elem->device_instance);
  329. GAMEINPUT_InternalRemoveByIndex(idx--);
  330. }
  331. }
  332. }
  333. static bool GAMEINPUT_JoystickIsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  334. {
  335. SDL_AssertJoysticksLocked();
  336. if (g_pGameInput) {
  337. if (vendor_id == USB_VENDOR_MICROSOFT &&
  338. product_id == USB_PRODUCT_XBOX_ONE_XBOXGIP_CONTROLLER) {
  339. // The Xbox One controller shows up as a hardcoded raw input VID/PID, which we definitely handle
  340. return true;
  341. }
  342. for (int i = 0; i < g_GameInputList.count; ++i) {
  343. GAMEINPUT_InternalDevice *elem = g_GameInputList.devices[i];
  344. if (elem && vendor_id == elem->info->vendorId && product_id == elem->info->productId) {
  345. return true;
  346. }
  347. }
  348. }
  349. return false;
  350. }
  351. static const char *GAMEINPUT_JoystickGetDeviceName(int device_index)
  352. {
  353. return GAMEINPUT_InternalFindByIndex(device_index)->name;
  354. }
  355. static const char *GAMEINPUT_JoystickGetDevicePath(int device_index)
  356. {
  357. // APP_LOCAL_DEVICE_ID as a hex string, since it's required for some association callbacks
  358. return GAMEINPUT_InternalFindByIndex(device_index)->path;
  359. }
  360. static int GAMEINPUT_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index)
  361. {
  362. return GAMEINPUT_InternalFindByIndex(device_index)->steam_virtual_gamepad_slot;
  363. }
  364. static int GAMEINPUT_JoystickGetDevicePlayerIndex(int device_index)
  365. {
  366. return -1;
  367. }
  368. static void GAMEINPUT_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  369. {
  370. }
  371. static SDL_GUID GAMEINPUT_JoystickGetDeviceGUID(int device_index)
  372. {
  373. return GAMEINPUT_InternalFindByIndex(device_index)->guid;
  374. }
  375. static SDL_JoystickID GAMEINPUT_JoystickGetDeviceInstanceID(int device_index)
  376. {
  377. return GAMEINPUT_InternalFindByIndex(device_index)->device_instance;
  378. }
  379. static void GAMEINPUT_UpdatePowerInfo(SDL_Joystick *joystick, IGameInputDevice *device)
  380. {
  381. #if 0
  382. GameInputBatteryState battery_state;
  383. SDL_PowerState state;
  384. int percent = 0;
  385. SDL_zero(battery_state);
  386. IGameInputDevice_GetBatteryState(device, &battery_state);
  387. switch (battery_state.status) {
  388. case GameInputBatteryNotPresent:
  389. state = SDL_POWERSTATE_NO_BATTERY;
  390. break;
  391. case GameInputBatteryDischarging:
  392. state = SDL_POWERSTATE_ON_BATTERY;
  393. break;
  394. case GameInputBatteryIdle:
  395. state = SDL_POWERSTATE_CHARGED;
  396. break;
  397. case GameInputBatteryCharging:
  398. state = SDL_POWERSTATE_CHARGING;
  399. break;
  400. default:
  401. state = SDL_POWERSTATE_UNKNOWN;
  402. break;
  403. }
  404. if (battery_state.fullChargeCapacity > 0.0f) {
  405. percent = (int)SDL_roundf((battery_state.remainingCapacity / battery_state.fullChargeCapacity) * 100.0f);
  406. }
  407. SDL_SendJoystickPowerInfo(joystick, state, percent);
  408. #endif
  409. }
  410. #if GAMEINPUT_API_VERSION >= 1
  411. static void CALLBACK GAMEINPUT_InternalSystemButtonCallback(
  412. _In_ GameInputCallbackToken callbackToken,
  413. _In_ void * context,
  414. _In_ IGameInputDevice * device,
  415. _In_ uint64_t timestampUS,
  416. _In_ GameInputSystemButtons currentButtons,
  417. _In_ GameInputSystemButtons previousButtons)
  418. {
  419. SDL_Joystick *joystick = (SDL_Joystick *)context;
  420. GameInputSystemButtons changedButtons = (previousButtons ^ currentButtons);
  421. if (changedButtons) {
  422. Uint64 timestamp = SDL_US_TO_NS(timestampUS + g_GameInputTimestampOffset);
  423. SDL_LockJoysticks();
  424. if (changedButtons & GameInputSystemButtonGuide) {
  425. bool down = ((currentButtons & GameInputSystemButtonGuide) != 0);
  426. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, down);
  427. }
  428. if (changedButtons & GameInputSystemButtonShare) {
  429. bool down = ((currentButtons & GameInputSystemButtonShare) != 0);
  430. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GAMEINPUT_SHARE, down);
  431. }
  432. SDL_UnlockJoysticks();
  433. }
  434. }
  435. #endif // GAMEINPUT_API_VERSION >= 1
  436. static bool GAMEINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
  437. {
  438. GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index);
  439. const GameInputDeviceInfo *info = elem->info;
  440. GAMEINPUT_InternalJoystickHwdata *hwdata = NULL;
  441. if (!elem) {
  442. return false;
  443. }
  444. hwdata = (GAMEINPUT_InternalJoystickHwdata *)SDL_calloc(1, sizeof(*hwdata));
  445. if (!hwdata) {
  446. return false;
  447. }
  448. hwdata->devref = elem;
  449. joystick->hwdata = hwdata;
  450. if (GAMEINPUT_InternalIsGamepad(info)) {
  451. joystick->naxes = 6;
  452. joystick->nbuttons = 11;
  453. joystick->nhats = 1;
  454. #if GAMEINPUT_API_VERSION >= 1
  455. if (info->supportedSystemButtons != GameInputSystemButtonNone) {
  456. if (info->supportedSystemButtons & GameInputSystemButtonShare) {
  457. ++joystick->nbuttons;
  458. }
  459. g_pGameInput->RegisterSystemButtonCallback(elem->device, (GameInputSystemButtonGuide | GameInputSystemButtonShare), joystick, GAMEINPUT_InternalSystemButtonCallback, &hwdata->system_button_callback_token);
  460. }
  461. #endif // GAMEINPUT_API_VERSION >= 1
  462. } else {
  463. joystick->naxes = info->controllerAxisCount;
  464. joystick->nbuttons = info->controllerButtonCount;
  465. joystick->nhats = info->controllerSwitchCount;
  466. }
  467. if (info->supportedRumbleMotors & (GameInputRumbleLowFrequency | GameInputRumbleHighFrequency)) {
  468. SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, true);
  469. }
  470. if (info->supportedRumbleMotors & (GameInputRumbleLeftTrigger | GameInputRumbleRightTrigger)) {
  471. SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN, true);
  472. }
  473. #ifdef GAMEINPUT_SENSOR_SUPPORT
  474. if (info->supportedInput & GameInputKindSensors) {
  475. // FIXME: What's the sensor update rate?
  476. if (info->sensorsInfo->supportedSensors & GameInputSensorsGyrometer) {
  477. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 250.0f);
  478. }
  479. if (info->sensorsInfo->supportedSensors & GameInputSensorsAccelerometer) {
  480. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 250.0f);
  481. }
  482. }
  483. #endif
  484. return true;
  485. }
  486. static bool GAMEINPUT_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  487. {
  488. // don't check for caps here, since SetRumbleState doesn't return any result - we don't need to check it
  489. GAMEINPUT_InternalJoystickHwdata *hwdata = joystick->hwdata;
  490. GameInputRumbleParams *params = &hwdata->rumbleParams;
  491. params->lowFrequency = (float)low_frequency_rumble / (float)SDL_MAX_UINT16;
  492. params->highFrequency = (float)high_frequency_rumble / (float)SDL_MAX_UINT16;
  493. hwdata->devref->device->SetRumbleState(params);
  494. return true;
  495. }
  496. static bool GAMEINPUT_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  497. {
  498. // don't check for caps here, since SetRumbleState doesn't return any result - we don't need to check it
  499. GAMEINPUT_InternalJoystickHwdata *hwdata = joystick->hwdata;
  500. GameInputRumbleParams *params = &hwdata->rumbleParams;
  501. params->leftTrigger = (float)left_rumble / (float)SDL_MAX_UINT16;
  502. params->rightTrigger = (float)right_rumble / (float)SDL_MAX_UINT16;
  503. hwdata->devref->device->SetRumbleState(params);
  504. return true;
  505. }
  506. static bool GAMEINPUT_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  507. {
  508. return SDL_Unsupported();
  509. }
  510. static bool GAMEINPUT_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
  511. {
  512. return SDL_Unsupported();
  513. }
  514. static bool GAMEINPUT_JoystickSetSensorsEnabled(SDL_Joystick *joystick, bool enabled)
  515. {
  516. joystick->hwdata->report_sensors = enabled;
  517. return true;
  518. }
  519. static void GAMEINPUT_JoystickUpdate(SDL_Joystick *joystick)
  520. {
  521. GAMEINPUT_InternalJoystickHwdata *hwdata = joystick->hwdata;
  522. IGameInputDevice *device = hwdata->devref->device;
  523. const GameInputDeviceInfo *info = hwdata->devref->info;
  524. IGameInputReading *reading = NULL;
  525. Uint64 timestamp;
  526. GameInputGamepadState state;
  527. HRESULT hr;
  528. hr = g_pGameInput->GetCurrentReading(info->supportedInput, device, &reading);
  529. if (FAILED(hr)) {
  530. // don't SetError here since there can be a legitimate case when there's no reading avail
  531. return;
  532. }
  533. timestamp = SDL_US_TO_NS(reading->GetTimestamp() + g_GameInputTimestampOffset);
  534. if (GAMEINPUT_InternalIsGamepad(info)) {
  535. static WORD s_XInputButtons[] = {
  536. GameInputGamepadA, // SDL_GAMEPAD_BUTTON_SOUTH
  537. GameInputGamepadB, // SDL_GAMEPAD_BUTTON_EAST
  538. GameInputGamepadX, // SDL_GAMEPAD_BUTTON_WEST
  539. GameInputGamepadY, // SDL_GAMEPAD_BUTTON_NORTH
  540. GameInputGamepadView, // SDL_GAMEPAD_BUTTON_BACK
  541. 0, // The guide button is not available
  542. GameInputGamepadMenu, // SDL_GAMEPAD_BUTTON_START
  543. GameInputGamepadLeftThumbstick, // SDL_GAMEPAD_BUTTON_LEFT_STICK
  544. GameInputGamepadRightThumbstick, // SDL_GAMEPAD_BUTTON_RIGHT_STICK
  545. GameInputGamepadLeftShoulder, // SDL_GAMEPAD_BUTTON_LEFT_SHOULDER
  546. GameInputGamepadRightShoulder, // SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER
  547. };
  548. Uint8 btnidx = 0, hat = 0;
  549. if (reading->GetGamepadState(&state)) {
  550. for (btnidx = 0; btnidx < SDL_arraysize(s_XInputButtons); ++btnidx) {
  551. WORD button_mask = s_XInputButtons[btnidx];
  552. if (!button_mask) {
  553. continue;
  554. }
  555. bool down = ((state.buttons & button_mask) != 0);
  556. SDL_SendJoystickButton(timestamp, joystick, btnidx, down);
  557. }
  558. if (state.buttons & GameInputGamepadDPadUp) {
  559. hat |= SDL_HAT_UP;
  560. }
  561. if (state.buttons & GameInputGamepadDPadDown) {
  562. hat |= SDL_HAT_DOWN;
  563. }
  564. if (state.buttons & GameInputGamepadDPadLeft) {
  565. hat |= SDL_HAT_LEFT;
  566. }
  567. if (state.buttons & GameInputGamepadDPadRight) {
  568. hat |= SDL_HAT_RIGHT;
  569. }
  570. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  571. #define CONVERT_AXIS(v) (Sint16)(((v) < 0.0f) ? ((v)*32768.0f) : ((v)*32767.0f))
  572. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, CONVERT_AXIS(state.leftThumbstickX));
  573. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, CONVERT_AXIS(-state.leftThumbstickY));
  574. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, CONVERT_AXIS(state.rightThumbstickX));
  575. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, CONVERT_AXIS(-state.rightThumbstickY));
  576. #undef CONVERT_AXIS
  577. #define CONVERT_TRIGGER(v) (Sint16)((v)*65535.0f - 32768.0f)
  578. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, CONVERT_TRIGGER(state.leftTrigger));
  579. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, CONVERT_TRIGGER(state.rightTrigger));
  580. #undef CONVERT_TRIGGER
  581. }
  582. } else {
  583. bool *button_state = SDL_stack_alloc(bool, info->controllerButtonCount);
  584. float *axis_state = SDL_stack_alloc(float, info->controllerAxisCount);
  585. GameInputSwitchPosition *switch_state = SDL_stack_alloc(GameInputSwitchPosition, info->controllerSwitchCount);
  586. if (button_state) {
  587. uint32_t i;
  588. uint32_t button_count = reading->GetControllerButtonState(info->controllerButtonCount, button_state);
  589. for (i = 0; i < button_count; ++i) {
  590. SDL_SendJoystickButton(timestamp, joystick, (Uint8)i, button_state[i]);
  591. }
  592. SDL_stack_free(button_state);
  593. }
  594. #define CONVERT_AXIS(v) (Sint16)((v)*65535.0f - 32768.0f)
  595. if (axis_state) {
  596. uint32_t i;
  597. uint32_t axis_count = reading->GetControllerAxisState(info->controllerAxisCount, axis_state);
  598. for (i = 0; i < axis_count; ++i) {
  599. SDL_SendJoystickAxis(timestamp, joystick, (Uint8)i, CONVERT_AXIS(axis_state[i]));
  600. }
  601. SDL_stack_free(axis_state);
  602. }
  603. #undef CONVERT_AXIS
  604. if (switch_state) {
  605. uint32_t i;
  606. uint32_t switch_count = reading->GetControllerSwitchState(info->controllerSwitchCount, switch_state);
  607. for (i = 0; i < switch_count; ++i) {
  608. Uint8 hat;
  609. switch (switch_state[i]) {
  610. case GameInputSwitchUp:
  611. hat = SDL_HAT_UP;
  612. break;
  613. case GameInputSwitchUpRight:
  614. hat = SDL_HAT_UP | SDL_HAT_RIGHT;
  615. break;
  616. case GameInputSwitchRight:
  617. hat = SDL_HAT_RIGHT;
  618. break;
  619. case GameInputSwitchDownRight:
  620. hat = SDL_HAT_DOWN | SDL_HAT_RIGHT;
  621. break;
  622. case GameInputSwitchDown:
  623. hat = SDL_HAT_DOWN;
  624. break;
  625. case GameInputSwitchDownLeft:
  626. hat = SDL_HAT_DOWN | SDL_HAT_LEFT;
  627. break;
  628. case GameInputSwitchLeft:
  629. hat = SDL_HAT_LEFT;
  630. break;
  631. case GameInputSwitchUpLeft:
  632. hat = SDL_HAT_UP | SDL_HAT_LEFT;
  633. break;
  634. case GameInputSwitchCenter:
  635. default:
  636. hat = SDL_HAT_CENTERED;
  637. break;
  638. }
  639. SDL_SendJoystickHat(timestamp, joystick, (Uint8)i, hat);
  640. }
  641. SDL_stack_free(switch_state);
  642. }
  643. }
  644. #ifdef GAMEINPUT_SENSOR_SUPPORT
  645. if (hwdata->report_sensors) {
  646. GameInputSensorsState sensor_state;
  647. if (reading->GetSensorsState(&sensor_state)) {
  648. if ((info->sensorsInfo->supportedSensors & GameInputSensorsGyrometer) != 0) {
  649. float data[3] = {
  650. sensor_state.angularVelocityInRadPerSecX,
  651. sensor_state.angularVelocityInRadPerSecY,
  652. sensor_state.angularVelocityInRadPerSecZ
  653. };
  654. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, timestamp, data, SDL_arraysize(data));
  655. }
  656. if ((info->sensorsInfo->supportedSensors & GameInputSensorsAccelerometer) != 0) {
  657. float data[3] = {
  658. sensor_state.accelerationInGX * SDL_STANDARD_GRAVITY,
  659. sensor_state.accelerationInGY * SDL_STANDARD_GRAVITY,
  660. sensor_state.accelerationInGZ * SDL_STANDARD_GRAVITY
  661. };
  662. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, timestamp, data, SDL_arraysize(data));
  663. }
  664. }
  665. }
  666. #endif // GAMEINPUT_SENSOR_SUPPORT
  667. reading->Release();
  668. // FIXME: We can poll this at a much lower rate
  669. GAMEINPUT_UpdatePowerInfo(joystick, device);
  670. }
  671. static void GAMEINPUT_JoystickClose(SDL_Joystick *joystick)
  672. {
  673. GAMEINPUT_InternalJoystickHwdata *hwdata = joystick->hwdata;
  674. if (hwdata->system_button_callback_token) {
  675. #if GAMEINPUT_API_VERSION >= 1
  676. g_pGameInput->UnregisterCallback(hwdata->system_button_callback_token);
  677. #else
  678. g_pGameInput->UnregisterCallback(hwdata->system_button_callback_token, 10000);
  679. #endif
  680. }
  681. SDL_free(hwdata);
  682. joystick->hwdata = NULL;
  683. }
  684. static void GAMEINPUT_JoystickQuit(void)
  685. {
  686. if (g_pGameInput) {
  687. // free the callback
  688. if (g_GameInputCallbackToken) {
  689. #if GAMEINPUT_API_VERSION >= 1
  690. g_pGameInput->UnregisterCallback(g_GameInputCallbackToken);
  691. #else
  692. g_pGameInput->UnregisterCallback(g_GameInputCallbackToken, 10000);
  693. #endif
  694. g_GameInputCallbackToken = 0;
  695. }
  696. // free the list
  697. while (g_GameInputList.count > 0) {
  698. GAMEINPUT_InternalRemoveByIndex(0);
  699. }
  700. SDL_QuitGameInput();
  701. g_pGameInput = NULL;
  702. }
  703. }
  704. static bool GAMEINPUT_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  705. {
  706. GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index);
  707. if (!GAMEINPUT_InternalIsGamepad(elem->info)) {
  708. return false;
  709. }
  710. out->a.kind = EMappingKind_Button;
  711. out->a.target = SDL_GAMEPAD_BUTTON_SOUTH;
  712. out->b.kind = EMappingKind_Button;
  713. out->b.target = SDL_GAMEPAD_BUTTON_EAST;
  714. out->x.kind = EMappingKind_Button;
  715. out->x.target = SDL_GAMEPAD_BUTTON_WEST;
  716. out->y.kind = EMappingKind_Button;
  717. out->y.target = SDL_GAMEPAD_BUTTON_NORTH;
  718. out->back.kind = EMappingKind_Button;
  719. out->back.target = SDL_GAMEPAD_BUTTON_BACK;
  720. #if GAMEINPUT_API_VERSION >= 1
  721. if (elem->info->supportedSystemButtons & GameInputSystemButtonGuide) {
  722. out->guide.kind = EMappingKind_Button;
  723. out->guide.target = SDL_GAMEPAD_BUTTON_GUIDE;
  724. }
  725. if (elem->info->supportedSystemButtons & GameInputSystemButtonShare) {
  726. out->misc1.kind = EMappingKind_Button;
  727. out->misc1.target = SDL_GAMEPAD_BUTTON_GAMEINPUT_SHARE;
  728. }
  729. #endif // GAMEINPUT_API_VERSION >= 1
  730. out->start.kind = EMappingKind_Button;
  731. out->start.target = SDL_GAMEPAD_BUTTON_START;
  732. out->leftstick.kind = EMappingKind_Button;
  733. out->leftstick.target = SDL_GAMEPAD_BUTTON_LEFT_STICK;
  734. out->rightstick.kind = EMappingKind_Button;
  735. out->rightstick.target = SDL_GAMEPAD_BUTTON_RIGHT_STICK;
  736. out->leftshoulder.kind = EMappingKind_Button;
  737. out->leftshoulder.target = SDL_GAMEPAD_BUTTON_LEFT_SHOULDER;
  738. out->rightshoulder.kind = EMappingKind_Button;
  739. out->rightshoulder.target = SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER;
  740. out->dpup.kind = EMappingKind_Hat;
  741. out->dpup.target = SDL_HAT_UP;
  742. out->dpdown.kind = EMappingKind_Hat;
  743. out->dpdown.target = SDL_HAT_DOWN;
  744. out->dpleft.kind = EMappingKind_Hat;
  745. out->dpleft.target = SDL_HAT_LEFT;
  746. out->dpright.kind = EMappingKind_Hat;
  747. out->dpright.target = SDL_HAT_RIGHT;
  748. out->leftx.kind = EMappingKind_Axis;
  749. out->leftx.target = SDL_GAMEPAD_AXIS_LEFTX;
  750. out->lefty.kind = EMappingKind_Axis;
  751. out->lefty.target = SDL_GAMEPAD_AXIS_LEFTY;
  752. out->rightx.kind = EMappingKind_Axis;
  753. out->rightx.target = SDL_GAMEPAD_AXIS_RIGHTX;
  754. out->righty.kind = EMappingKind_Axis;
  755. out->righty.target = SDL_GAMEPAD_AXIS_RIGHTY;
  756. out->lefttrigger.kind = EMappingKind_Axis;
  757. out->lefttrigger.target = SDL_GAMEPAD_AXIS_LEFT_TRIGGER;
  758. out->righttrigger.kind = EMappingKind_Axis;
  759. out->righttrigger.target = SDL_GAMEPAD_AXIS_RIGHT_TRIGGER;
  760. return true;
  761. }
  762. SDL_JoystickDriver SDL_GAMEINPUT_JoystickDriver =
  763. {
  764. GAMEINPUT_JoystickInit,
  765. GAMEINPUT_JoystickGetCount,
  766. GAMEINPUT_JoystickDetect,
  767. GAMEINPUT_JoystickIsDevicePresent,
  768. GAMEINPUT_JoystickGetDeviceName,
  769. GAMEINPUT_JoystickGetDevicePath,
  770. GAMEINPUT_JoystickGetDeviceSteamVirtualGamepadSlot,
  771. GAMEINPUT_JoystickGetDevicePlayerIndex,
  772. GAMEINPUT_JoystickSetDevicePlayerIndex,
  773. GAMEINPUT_JoystickGetDeviceGUID,
  774. GAMEINPUT_JoystickGetDeviceInstanceID,
  775. GAMEINPUT_JoystickOpen,
  776. GAMEINPUT_JoystickRumble,
  777. GAMEINPUT_JoystickRumbleTriggers,
  778. GAMEINPUT_JoystickSetLED,
  779. GAMEINPUT_JoystickSendEffect,
  780. GAMEINPUT_JoystickSetSensorsEnabled,
  781. GAMEINPUT_JoystickUpdate,
  782. GAMEINPUT_JoystickClose,
  783. GAMEINPUT_JoystickQuit,
  784. GAMEINPUT_JoystickGetGamepadMapping
  785. };
  786. #endif // SDL_JOYSTICK_GAMEINPUT