SDL_gameinputjoystick.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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 <stdbool.h>
  23. #define COBJMACROS
  24. #include <gameinput.h>
  25. enum
  26. {
  27. SDL_GAMEPAD_BUTTON_GAMEINPUT_SHARE = 11
  28. };
  29. typedef struct GAMEINPUT_InternalDevice
  30. {
  31. IGameInputDevice *device;
  32. char path[(APP_LOCAL_DEVICE_ID_SIZE * 2) + 1];
  33. char *name;
  34. SDL_GUID guid; /* generated by SDL */
  35. SDL_JoystickID device_instance; /* generated by SDL */
  36. const GameInputDeviceInfo *info;
  37. SDL_bool isAdded;
  38. SDL_bool isDeleteRequested;
  39. } GAMEINPUT_InternalDevice;
  40. typedef struct GAMEINPUT_InternalList
  41. {
  42. GAMEINPUT_InternalDevice **devices;
  43. int count;
  44. } GAMEINPUT_InternalList;
  45. typedef struct joystick_hwdata
  46. {
  47. GAMEINPUT_InternalDevice *devref;
  48. SDL_bool report_sensors;
  49. GameInputRumbleParams rumbleParams;
  50. GameInputCallbackToken system_button_callback_token;
  51. } GAMEINPUT_InternalJoystickHwdata;
  52. static GAMEINPUT_InternalList g_GameInputList = { NULL };
  53. static void *g_hGameInputDLL = NULL;
  54. static IGameInput *g_pGameInput = NULL;
  55. static GameInputCallbackToken g_GameInputCallbackToken = GAMEINPUT_INVALID_CALLBACK_TOKEN_VALUE;
  56. static Uint64 g_GameInputTimestampOffset;
  57. static SDL_bool GAMEINPUT_InternalIsGamepad(const GameInputDeviceInfo *info)
  58. {
  59. if (info->supportedInput & GameInputKindGamepad) {
  60. return SDL_TRUE;
  61. }
  62. return SDL_FALSE;
  63. }
  64. static int GAMEINPUT_InternalAddOrFind(IGameInputDevice *pDevice)
  65. {
  66. GAMEINPUT_InternalDevice **devicelist = NULL;
  67. GAMEINPUT_InternalDevice *elem = NULL;
  68. const GameInputDeviceInfo *info = NULL;
  69. Uint16 bus = SDL_HARDWARE_BUS_USB;
  70. Uint16 vendor = 0;
  71. Uint16 product = 0;
  72. Uint16 version = 0;
  73. const char *manufacturer_string = NULL;
  74. const char *product_string = NULL;
  75. char tmp[4];
  76. int idx = 0;
  77. SDL_AssertJoysticksLocked();
  78. info = IGameInputDevice_GetDeviceInfo(pDevice);
  79. if (info->capabilities & GameInputDeviceCapabilityWireless) {
  80. bus = SDL_HARDWARE_BUS_BLUETOOTH;
  81. } else {
  82. bus = SDL_HARDWARE_BUS_USB;
  83. }
  84. vendor = info->vendorId;
  85. product = info->productId;
  86. version = (info->firmwareVersion.major << 8) | info->firmwareVersion.minor;
  87. if (SDL_JoystickHandledByAnotherDriver(&SDL_GAMEINPUT_JoystickDriver, vendor, product, version, "")) {
  88. return 0;
  89. }
  90. for (idx = 0; idx < g_GameInputList.count; ++idx) {
  91. elem = g_GameInputList.devices[idx];
  92. if (elem && elem->device == pDevice) {
  93. /* we're already added */
  94. elem->isDeleteRequested = SDL_FALSE;
  95. return 0;
  96. }
  97. }
  98. elem = (GAMEINPUT_InternalDevice *)SDL_calloc(1, sizeof(*elem));
  99. if (!elem) {
  100. return -1;
  101. }
  102. devicelist = (GAMEINPUT_InternalDevice **)SDL_realloc(g_GameInputList.devices, sizeof(elem) * (g_GameInputList.count + 1LL));
  103. if (!devicelist) {
  104. SDL_free(elem);
  105. return -1;
  106. }
  107. /* Generate a device path */
  108. for (idx = 0; idx < APP_LOCAL_DEVICE_ID_SIZE; ++idx) {
  109. SDL_snprintf(tmp, SDL_arraysize(tmp), "%02hhX", info->deviceId.value[idx]);
  110. SDL_strlcat(elem->path, tmp, SDL_arraysize(tmp));
  111. }
  112. if (info->deviceStrings) {
  113. /* In theory we could get the manufacturer and product strings here, but they're NULL for all the controllers I've tested */
  114. }
  115. if (info->displayName) {
  116. /* This could give us a product string, but it's NULL for all the controllers I've tested */
  117. }
  118. IGameInputDevice_AddRef(pDevice);
  119. elem->device = pDevice;
  120. elem->name = SDL_CreateJoystickName(vendor, product, manufacturer_string, product_string);
  121. elem->guid = SDL_CreateJoystickGUID(bus, vendor, product, version, manufacturer_string, product_string, 'g', 0);
  122. elem->device_instance = SDL_GetNextObjectID();
  123. elem->info = info;
  124. g_GameInputList.devices = devicelist;
  125. g_GameInputList.devices[g_GameInputList.count++] = elem;
  126. return 0;
  127. }
  128. static int GAMEINPUT_InternalRemoveByIndex(int idx)
  129. {
  130. GAMEINPUT_InternalDevice **devicelist = NULL;
  131. GAMEINPUT_InternalDevice *elem;
  132. int bytes = 0;
  133. SDL_AssertJoysticksLocked();
  134. if (idx < 0 || idx >= g_GameInputList.count) {
  135. return SDL_SetError("GAMEINPUT_InternalRemoveByIndex argument idx %d is out of range", idx);
  136. }
  137. elem = g_GameInputList.devices[idx];
  138. if (elem) {
  139. IGameInputDevice_Release(elem->device);
  140. SDL_free(elem->name);
  141. SDL_free(elem);
  142. }
  143. g_GameInputList.devices[idx] = NULL;
  144. if (g_GameInputList.count == 1) {
  145. /* last element in the list, free the entire list then */
  146. SDL_free(g_GameInputList.devices);
  147. g_GameInputList.devices = NULL;
  148. } else {
  149. if (idx != g_GameInputList.count - 1) {
  150. bytes = sizeof(*devicelist) * (g_GameInputList.count - idx);
  151. SDL_memmove(&g_GameInputList.devices[idx], &g_GameInputList.devices[idx + 1], bytes);
  152. }
  153. }
  154. /* decrement the count and return */
  155. return g_GameInputList.count--;
  156. }
  157. static GAMEINPUT_InternalDevice *GAMEINPUT_InternalFindByIndex(int idx)
  158. {
  159. /* We're guaranteed that the index is in range when this is called */
  160. SDL_AssertJoysticksLocked();
  161. return g_GameInputList.devices[idx];
  162. }
  163. static void CALLBACK GAMEINPUT_InternalJoystickDeviceCallback(
  164. _In_ GameInputCallbackToken callbackToken,
  165. _In_ void* context,
  166. _In_ IGameInputDevice* device,
  167. _In_ uint64_t timestamp,
  168. _In_ GameInputDeviceStatus currentStatus,
  169. _In_ GameInputDeviceStatus previousStatus)
  170. {
  171. int idx = 0;
  172. GAMEINPUT_InternalDevice *elem = NULL;
  173. if (!device) {
  174. /* This should never happen, but ignore it if it does */
  175. return;
  176. }
  177. SDL_LockJoysticks();
  178. if (currentStatus & GameInputDeviceConnected) {
  179. GAMEINPUT_InternalAddOrFind(device);
  180. } else {
  181. for (idx = 0; idx < g_GameInputList.count; ++idx) {
  182. elem = g_GameInputList.devices[idx];
  183. if (elem && elem->device == device) {
  184. /* will be deleted on the next Detect call */
  185. elem->isDeleteRequested = SDL_TRUE;
  186. break;
  187. }
  188. }
  189. }
  190. SDL_UnlockJoysticks();
  191. }
  192. static void GAMEINPUT_JoystickDetect(void);
  193. static int GAMEINPUT_JoystickInit(void)
  194. {
  195. HRESULT hR;
  196. if (!g_hGameInputDLL) {
  197. g_hGameInputDLL = SDL_LoadObject("gameinput.dll");
  198. if (!g_hGameInputDLL) {
  199. return -1;
  200. }
  201. }
  202. if (!g_pGameInput) {
  203. typedef HRESULT (WINAPI *GameInputCreate_t)(IGameInput * *gameInput);
  204. GameInputCreate_t GameInputCreateFunc = (GameInputCreate_t)SDL_LoadFunction(g_hGameInputDLL, "GameInputCreate");
  205. if (!GameInputCreateFunc) {
  206. return -1;
  207. }
  208. hR = GameInputCreateFunc(&g_pGameInput);
  209. if (FAILED(hR)) {
  210. return SDL_SetError("GameInputCreate failure with HRESULT of %08X", hR);
  211. }
  212. }
  213. hR = IGameInput_RegisterDeviceCallback(g_pGameInput,
  214. NULL,
  215. GameInputKindController,
  216. GameInputDeviceConnected,
  217. GameInputBlockingEnumeration,
  218. NULL,
  219. GAMEINPUT_InternalJoystickDeviceCallback,
  220. &g_GameInputCallbackToken);
  221. if (FAILED(hR)) {
  222. return SDL_SetError("IGameInput::RegisterDeviceCallback failure with HRESULT of %08X", hR);
  223. }
  224. // Calculate the relative offset between SDL timestamps and GameInput timestamps
  225. Uint64 now = SDL_GetTicksNS();
  226. uint64_t timestampUS = IGameInput_GetCurrentTimestamp(g_pGameInput);
  227. g_GameInputTimestampOffset = (SDL_NS_TO_US(now) - timestampUS);
  228. GAMEINPUT_JoystickDetect();
  229. return 0;
  230. }
  231. static int GAMEINPUT_JoystickGetCount(void)
  232. {
  233. SDL_AssertJoysticksLocked();
  234. return g_GameInputList.count;
  235. }
  236. static void GAMEINPUT_JoystickDetect(void)
  237. {
  238. int idx;
  239. GAMEINPUT_InternalDevice *elem = NULL;
  240. SDL_AssertJoysticksLocked();
  241. for (idx = 0; idx < g_GameInputList.count; ++idx) {
  242. elem = g_GameInputList.devices[idx];
  243. if (!elem) {
  244. continue;
  245. }
  246. if (!elem->isAdded) {
  247. SDL_PrivateJoystickAdded(elem->device_instance);
  248. elem->isAdded = SDL_TRUE;
  249. }
  250. if (elem->isDeleteRequested || !(IGameInputDevice_GetDeviceStatus(elem->device) & GameInputDeviceConnected)) {
  251. SDL_PrivateJoystickRemoved(elem->device_instance);
  252. GAMEINPUT_InternalRemoveByIndex(idx--);
  253. }
  254. }
  255. }
  256. static SDL_bool GAMEINPUT_JoystickIsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  257. {
  258. int idx = 0;
  259. GAMEINPUT_InternalDevice *elem = NULL;
  260. SDL_AssertJoysticksLocked();
  261. if (vendor_id == USB_VENDOR_MICROSOFT &&
  262. product_id == USB_PRODUCT_XBOX_ONE_XBOXGIP_CONTROLLER) {
  263. /* The Xbox One controller shows up as a hardcoded raw input VID/PID, which we definitely handle */
  264. return SDL_TRUE;
  265. }
  266. for (idx = 0; idx < g_GameInputList.count; ++idx) {
  267. elem = g_GameInputList.devices[idx];
  268. if (elem && vendor_id == elem->info->vendorId && product_id == elem->info->productId) {
  269. return SDL_TRUE;
  270. }
  271. }
  272. return SDL_FALSE;
  273. }
  274. static const char *GAMEINPUT_JoystickGetDeviceName(int device_index)
  275. {
  276. return GAMEINPUT_InternalFindByIndex(device_index)->name;
  277. }
  278. static const char *GAMEINPUT_JoystickGetDevicePath(int device_index)
  279. {
  280. /* APP_LOCAL_DEVICE_ID as a hex string, since it's required for some association callbacks */
  281. return GAMEINPUT_InternalFindByIndex(device_index)->path;
  282. }
  283. static int GAMEINPUT_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index)
  284. {
  285. /* Steamworks API is not available in GDK */
  286. return -1;
  287. }
  288. static int GAMEINPUT_JoystickGetDevicePlayerIndex(int device_index)
  289. {
  290. return -1;
  291. }
  292. static void GAMEINPUT_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  293. {
  294. }
  295. static SDL_GUID GAMEINPUT_JoystickGetDeviceGUID(int device_index)
  296. {
  297. return GAMEINPUT_InternalFindByIndex(device_index)->guid;
  298. }
  299. static SDL_JoystickID GAMEINPUT_JoystickGetDeviceInstanceID(int device_index)
  300. {
  301. return GAMEINPUT_InternalFindByIndex(device_index)->device_instance;
  302. }
  303. static void GAMEINPUT_UpdatePowerInfo(SDL_Joystick *joystick, IGameInputDevice *device)
  304. {
  305. GameInputBatteryState battery_state;
  306. SDL_PowerState state;
  307. int percent = 0;
  308. SDL_zero(battery_state);
  309. IGameInputDevice_GetBatteryState(device, &battery_state);
  310. switch (battery_state.status) {
  311. case GameInputBatteryNotPresent:
  312. state = SDL_POWERSTATE_NO_BATTERY;
  313. break;
  314. case GameInputBatteryDischarging:
  315. state = SDL_POWERSTATE_ON_BATTERY;
  316. break;
  317. case GameInputBatteryIdle:
  318. state = SDL_POWERSTATE_CHARGED;
  319. break;
  320. case GameInputBatteryCharging:
  321. state = SDL_POWERSTATE_CHARGING;
  322. break;
  323. default:
  324. state = SDL_POWERSTATE_UNKNOWN;
  325. break;
  326. }
  327. if (battery_state.fullChargeCapacity > 0.0f) {
  328. percent = (int)SDL_roundf((battery_state.remainingCapacity / battery_state.fullChargeCapacity) * 100.0f);
  329. }
  330. SDL_SendJoystickPowerInfo(joystick, state, percent);
  331. }
  332. #ifdef IGameInput_RegisterSystemButtonCallback
  333. static void CALLBACK GAMEINPUT_InternalSystemButtonCallback(
  334. _In_ GameInputCallbackToken callbackToken,
  335. _In_ void * context,
  336. _In_ IGameInputDevice * device,
  337. _In_ uint64_t timestampUS,
  338. _In_ GameInputSystemButtons currentButtons,
  339. _In_ GameInputSystemButtons previousButtons)
  340. {
  341. SDL_Joystick *joystick = (SDL_Joystick *)context;
  342. GameInputSystemButtons changedButtons = (previousButtons ^ currentButtons);
  343. if (changedButtons) {
  344. Uint64 timestamp = SDL_US_TO_NS(timestampUS + g_GameInputTimestampOffset);
  345. SDL_LockJoysticks();
  346. if (changedButtons & GameInputSystemButtonGuide) {
  347. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, (currentButtons & GameInputSystemButtonGuide) ? SDL_PRESSED : SDL_RELEASED);
  348. }
  349. if (changedButtons & GameInputSystemButtonShare) {
  350. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GAMEINPUT_SHARE, (currentButtons & GameInputSystemButtonShare) ? SDL_PRESSED : SDL_RELEASED);
  351. }
  352. SDL_UnlockJoysticks();
  353. }
  354. }
  355. #endif // IGameInput_RegisterSystemButtonCallback
  356. static int GAMEINPUT_JoystickOpen(SDL_Joystick *joystick, int device_index)
  357. {
  358. GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index);
  359. const GameInputDeviceInfo *info = elem->info;
  360. GAMEINPUT_InternalJoystickHwdata *hwdata = NULL;
  361. if (!elem) {
  362. return -1;
  363. }
  364. hwdata = (GAMEINPUT_InternalJoystickHwdata *)SDL_calloc(1, sizeof(*hwdata));
  365. if (!hwdata) {
  366. return -1;
  367. }
  368. hwdata->devref = elem;
  369. joystick->hwdata = hwdata;
  370. if (GAMEINPUT_InternalIsGamepad(info)) {
  371. joystick->naxes = 6;
  372. joystick->nbuttons = 11;
  373. joystick->nhats = 1;
  374. #ifdef IGameInput_RegisterSystemButtonCallback
  375. if (info->supportedSystemButtons != GameInputSystemButtonNone) {
  376. if (info->supportedSystemButtons & GameInputSystemButtonShare) {
  377. ++joystick->nbuttons;
  378. }
  379. #if 1 // The C macro in GameInput.h version 10.0.26100 refers to a focus policy which I guess has been removed from the final API?
  380. #undef IGameInput_RegisterSystemButtonCallback
  381. #define IGameInput_RegisterSystemButtonCallback(This, device, buttonFilter, context, callbackFunc, callbackToken) ((This)->lpVtbl->RegisterSystemButtonCallback(This, device, buttonFilter, context, callbackFunc, callbackToken))
  382. #endif
  383. IGameInput_RegisterSystemButtonCallback(g_pGameInput, elem->device, (GameInputSystemButtonGuide | GameInputSystemButtonShare), joystick, GAMEINPUT_InternalSystemButtonCallback, &hwdata->system_button_callback_token);
  384. }
  385. #endif // IGameInput_RegisterSystemButtonCallback
  386. } else {
  387. joystick->naxes = info->controllerAxisCount;
  388. joystick->nbuttons = info->controllerButtonCount;
  389. joystick->nhats = info->controllerSwitchCount;
  390. }
  391. if (info->supportedRumbleMotors & (GameInputRumbleLowFrequency | GameInputRumbleHighFrequency)) {
  392. SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, SDL_TRUE);
  393. }
  394. if (info->supportedRumbleMotors & (GameInputRumbleLeftTrigger | GameInputRumbleRightTrigger)) {
  395. SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN, SDL_TRUE);
  396. }
  397. if (info->supportedInput & GameInputKindTouch) {
  398. SDL_PrivateJoystickAddTouchpad(joystick, info->touchPointCount);
  399. }
  400. if (info->supportedInput & GameInputKindMotion) {
  401. /* FIXME: What's the sensor update rate? */
  402. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, 250.0f);
  403. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, 250.0f);
  404. }
  405. if (info->capabilities & GameInputDeviceCapabilityWireless) {
  406. joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRELESS;
  407. } else {
  408. joystick->connection_state = SDL_JOYSTICK_CONNECTION_WIRED;
  409. }
  410. return 0;
  411. }
  412. static int GAMEINPUT_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  413. {
  414. /* don't check for caps here, since SetRumbleState doesn't return any result - we don't need to check it */
  415. GAMEINPUT_InternalJoystickHwdata *hwdata = joystick->hwdata;
  416. GameInputRumbleParams *params = &hwdata->rumbleParams;
  417. params->lowFrequency = (float)low_frequency_rumble / (float)SDL_MAX_UINT16;
  418. params->highFrequency = (float)high_frequency_rumble / (float)SDL_MAX_UINT16;
  419. IGameInputDevice_SetRumbleState(hwdata->devref->device, params);
  420. return 0;
  421. }
  422. static int GAMEINPUT_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  423. {
  424. /* don't check for caps here, since SetRumbleState doesn't return any result - we don't need to check it */
  425. GAMEINPUT_InternalJoystickHwdata *hwdata = joystick->hwdata;
  426. GameInputRumbleParams *params = &hwdata->rumbleParams;
  427. params->leftTrigger = (float)left_rumble / (float)SDL_MAX_UINT16;
  428. params->rightTrigger = (float)right_rumble / (float)SDL_MAX_UINT16;
  429. IGameInputDevice_SetRumbleState(hwdata->devref->device, params);
  430. return 0;
  431. }
  432. static int GAMEINPUT_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  433. {
  434. return SDL_Unsupported();
  435. }
  436. static int GAMEINPUT_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
  437. {
  438. return SDL_Unsupported();
  439. }
  440. static int GAMEINPUT_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
  441. {
  442. joystick->hwdata->report_sensors = enabled;
  443. return 0;
  444. }
  445. static void GAMEINPUT_JoystickUpdate(SDL_Joystick *joystick)
  446. {
  447. GAMEINPUT_InternalJoystickHwdata *hwdata = joystick->hwdata;
  448. IGameInputDevice *device = hwdata->devref->device;
  449. const GameInputDeviceInfo *info = hwdata->devref->info;
  450. IGameInputReading *reading = NULL;
  451. Uint64 timestamp;
  452. GameInputGamepadState state;
  453. HRESULT hR;
  454. hR = IGameInput_GetCurrentReading(g_pGameInput, info->supportedInput, device, &reading);
  455. if (FAILED(hR)) {
  456. /* don't SetError here since there can be a legitimate case when there's no reading avail */
  457. return;
  458. }
  459. timestamp = SDL_US_TO_NS(IGameInputReading_GetTimestamp(reading) + g_GameInputTimestampOffset);
  460. if (GAMEINPUT_InternalIsGamepad(info)) {
  461. static WORD s_XInputButtons[] = {
  462. GameInputGamepadA, /* SDL_GAMEPAD_BUTTON_SOUTH */
  463. GameInputGamepadB, /* SDL_GAMEPAD_BUTTON_EAST */
  464. GameInputGamepadX, /* SDL_GAMEPAD_BUTTON_WEST */
  465. GameInputGamepadY, /* SDL_GAMEPAD_BUTTON_NORTH */
  466. GameInputGamepadView, /* SDL_GAMEPAD_BUTTON_BACK */
  467. 0, /* The guide button is not available */
  468. GameInputGamepadMenu, /* SDL_GAMEPAD_BUTTON_START */
  469. GameInputGamepadLeftThumbstick, /* SDL_GAMEPAD_BUTTON_LEFT_STICK */
  470. GameInputGamepadRightThumbstick, /* SDL_GAMEPAD_BUTTON_RIGHT_STICK */
  471. GameInputGamepadLeftShoulder, /* SDL_GAMEPAD_BUTTON_LEFT_SHOULDER */
  472. GameInputGamepadRightShoulder, /* SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER */
  473. };
  474. Uint8 btnidx = 0, btnstate = 0, hat = 0;
  475. if (IGameInputReading_GetGamepadState(reading, &state)) {
  476. for (btnidx = 0; btnidx < SDL_arraysize(s_XInputButtons); ++btnidx) {
  477. WORD button_mask = s_XInputButtons[btnidx];
  478. if (!button_mask) {
  479. continue;
  480. }
  481. btnstate = (state.buttons & button_mask) ? SDL_PRESSED : SDL_RELEASED;
  482. SDL_SendJoystickButton(timestamp, joystick, btnidx, btnstate);
  483. }
  484. if (state.buttons & GameInputGamepadDPadUp) {
  485. hat |= SDL_HAT_UP;
  486. }
  487. if (state.buttons & GameInputGamepadDPadDown) {
  488. hat |= SDL_HAT_DOWN;
  489. }
  490. if (state.buttons & GameInputGamepadDPadLeft) {
  491. hat |= SDL_HAT_LEFT;
  492. }
  493. if (state.buttons & GameInputGamepadDPadRight) {
  494. hat |= SDL_HAT_RIGHT;
  495. }
  496. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  497. #define CONVERT_AXIS(v) (Sint16)(((v) < 0.0f) ? ((v)*32768.0f) : ((v)*32767.0f))
  498. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, CONVERT_AXIS(state.leftThumbstickX));
  499. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, CONVERT_AXIS(-state.leftThumbstickY));
  500. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, CONVERT_AXIS(state.rightThumbstickX));
  501. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, CONVERT_AXIS(-state.rightThumbstickY));
  502. #undef CONVERT_AXIS
  503. #define CONVERT_TRIGGER(v) (Sint16)((v)*65535.0f - 32768.0f)
  504. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, CONVERT_TRIGGER(state.leftTrigger));
  505. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, CONVERT_TRIGGER(state.rightTrigger));
  506. #undef CONVERT_TRIGGER
  507. }
  508. } else {
  509. bool *button_state = SDL_stack_alloc(bool, info->controllerButtonCount);
  510. float *axis_state = SDL_stack_alloc(float, info->controllerAxisCount);
  511. if (button_state) {
  512. uint32_t i;
  513. uint32_t button_count = IGameInputReading_GetControllerButtonState(reading, info->controllerButtonCount, button_state);
  514. for (i = 0; i < button_count; ++i) {
  515. SDL_SendJoystickButton(timestamp, joystick, (Uint8)i, button_state[i]);
  516. }
  517. SDL_stack_free(button_state);
  518. }
  519. #define CONVERT_AXIS(v) (Sint16)((v)*65535.0f - 32768.0f)
  520. if (axis_state) {
  521. uint32_t i;
  522. uint32_t axis_count = IGameInputReading_GetControllerAxisState(reading, info->controllerAxisCount, axis_state);
  523. for (i = 0; i < axis_count; ++i) {
  524. SDL_SendJoystickAxis(timestamp, joystick, (Uint8)i, CONVERT_AXIS(axis_state[i]));
  525. }
  526. SDL_stack_free(axis_state);
  527. }
  528. #undef CONVERT_AXIS
  529. }
  530. if (info->supportedInput & GameInputKindTouch) {
  531. GameInputTouchState *touch_state = SDL_stack_alloc(GameInputTouchState, info->touchPointCount);
  532. if (touch_state) {
  533. uint32_t i;
  534. uint32_t touch_count = IGameInputReading_GetTouchState(reading, info->touchPointCount, touch_state);
  535. for (i = 0; i < touch_count; ++i) {
  536. GameInputTouchState *touch = &touch_state[i];
  537. /* FIXME: We should use touch->touchId to track fingers instead of using i below */
  538. SDL_SendJoystickTouchpad(timestamp, joystick, 0, i, SDL_PRESSED, touch->positionX * info->touchSensorInfo[i].resolutionX, touch->positionY * info->touchSensorInfo[0].resolutionY, touch->pressure);
  539. }
  540. SDL_stack_free(touch_state);
  541. }
  542. }
  543. if (hwdata->report_sensors) {
  544. GameInputMotionState motion_state;
  545. if (IGameInputReading_GetMotionState(reading, &motion_state)) {
  546. /* FIXME: How do we interpret the motion data? */
  547. }
  548. }
  549. IGameInputReading_Release(reading);
  550. /* FIXME: We can poll this at a much lower rate */
  551. GAMEINPUT_UpdatePowerInfo(joystick, device);
  552. }
  553. static void GAMEINPUT_JoystickClose(SDL_Joystick* joystick)
  554. {
  555. GAMEINPUT_InternalJoystickHwdata *hwdata = joystick->hwdata;
  556. if (hwdata->system_button_callback_token) {
  557. IGameInput_UnregisterCallback(g_pGameInput, hwdata->system_button_callback_token, 5000);
  558. }
  559. SDL_free(hwdata);
  560. joystick->hwdata = NULL;
  561. }
  562. static void GAMEINPUT_JoystickQuit(void)
  563. {
  564. if (g_pGameInput) {
  565. /* free the callback */
  566. IGameInput_UnregisterCallback(g_pGameInput, g_GameInputCallbackToken, /*timeoutInUs:*/ 10000);
  567. g_GameInputCallbackToken = GAMEINPUT_INVALID_CALLBACK_TOKEN_VALUE;
  568. /* free the list */
  569. while (g_GameInputList.count > 0) {
  570. GAMEINPUT_InternalRemoveByIndex(0);
  571. }
  572. IGameInput_Release(g_pGameInput);
  573. g_pGameInput = NULL;
  574. }
  575. if (g_hGameInputDLL) {
  576. SDL_UnloadObject(g_hGameInputDLL);
  577. g_hGameInputDLL = NULL;
  578. }
  579. }
  580. static SDL_bool GAMEINPUT_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  581. {
  582. GAMEINPUT_InternalDevice *elem = GAMEINPUT_InternalFindByIndex(device_index);
  583. if (!GAMEINPUT_InternalIsGamepad(elem->info)) {
  584. return SDL_FALSE;
  585. }
  586. out->a.kind = EMappingKind_Button;
  587. out->a.target = SDL_GAMEPAD_BUTTON_SOUTH;
  588. out->b.kind = EMappingKind_Button;
  589. out->b.target = SDL_GAMEPAD_BUTTON_EAST;
  590. out->x.kind = EMappingKind_Button;
  591. out->x.target = SDL_GAMEPAD_BUTTON_WEST;
  592. out->y.kind = EMappingKind_Button;
  593. out->y.target = SDL_GAMEPAD_BUTTON_NORTH;
  594. out->back.kind = EMappingKind_Button;
  595. out->back.target = SDL_GAMEPAD_BUTTON_BACK;
  596. #ifdef IGameInput_RegisterSystemButtonCallback
  597. if (elem->info->supportedSystemButtons & GameInputSystemButtonGuide) {
  598. out->guide.kind = EMappingKind_Button;
  599. out->guide.target = SDL_GAMEPAD_BUTTON_GUIDE;
  600. }
  601. if (elem->info->supportedSystemButtons & GameInputSystemButtonShare) {
  602. out->misc1.kind = EMappingKind_Button;
  603. out->misc1.target = SDL_GAMEPAD_BUTTON_GAMEINPUT_SHARE;
  604. }
  605. #endif
  606. out->start.kind = EMappingKind_Button;
  607. out->start.target = SDL_GAMEPAD_BUTTON_START;
  608. out->leftstick.kind = EMappingKind_Button;
  609. out->leftstick.target = SDL_GAMEPAD_BUTTON_LEFT_STICK;
  610. out->rightstick.kind = EMappingKind_Button;
  611. out->rightstick.target = SDL_GAMEPAD_BUTTON_RIGHT_STICK;
  612. out->leftshoulder.kind = EMappingKind_Button;
  613. out->leftshoulder.target = SDL_GAMEPAD_BUTTON_LEFT_SHOULDER;
  614. out->rightshoulder.kind = EMappingKind_Button;
  615. out->rightshoulder.target = SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER;
  616. out->dpup.kind = EMappingKind_Hat;
  617. out->dpup.target = SDL_HAT_UP;
  618. out->dpdown.kind = EMappingKind_Hat;
  619. out->dpdown.target = SDL_HAT_DOWN;
  620. out->dpleft.kind = EMappingKind_Hat;
  621. out->dpleft.target = SDL_HAT_LEFT;
  622. out->dpright.kind = EMappingKind_Hat;
  623. out->dpright.target = SDL_HAT_RIGHT;
  624. out->leftx.kind = EMappingKind_Axis;
  625. out->leftx.target = SDL_GAMEPAD_AXIS_LEFTX;
  626. out->lefty.kind = EMappingKind_Axis;
  627. out->lefty.target = SDL_GAMEPAD_AXIS_LEFTY;
  628. out->rightx.kind = EMappingKind_Axis;
  629. out->rightx.target = SDL_GAMEPAD_AXIS_RIGHTX;
  630. out->righty.kind = EMappingKind_Axis;
  631. out->righty.target = SDL_GAMEPAD_AXIS_RIGHTY;
  632. out->lefttrigger.kind = EMappingKind_Axis;
  633. out->lefttrigger.target = SDL_GAMEPAD_AXIS_LEFT_TRIGGER;
  634. out->righttrigger.kind = EMappingKind_Axis;
  635. out->righttrigger.target = SDL_GAMEPAD_AXIS_RIGHT_TRIGGER;
  636. return SDL_TRUE;
  637. }
  638. SDL_JoystickDriver SDL_GAMEINPUT_JoystickDriver =
  639. {
  640. GAMEINPUT_JoystickInit,
  641. GAMEINPUT_JoystickGetCount,
  642. GAMEINPUT_JoystickDetect,
  643. GAMEINPUT_JoystickIsDevicePresent,
  644. GAMEINPUT_JoystickGetDeviceName,
  645. GAMEINPUT_JoystickGetDevicePath,
  646. GAMEINPUT_JoystickGetDeviceSteamVirtualGamepadSlot,
  647. GAMEINPUT_JoystickGetDevicePlayerIndex,
  648. GAMEINPUT_JoystickSetDevicePlayerIndex,
  649. GAMEINPUT_JoystickGetDeviceGUID,
  650. GAMEINPUT_JoystickGetDeviceInstanceID,
  651. GAMEINPUT_JoystickOpen,
  652. GAMEINPUT_JoystickRumble,
  653. GAMEINPUT_JoystickRumbleTriggers,
  654. GAMEINPUT_JoystickSetLED,
  655. GAMEINPUT_JoystickSendEffect,
  656. GAMEINPUT_JoystickSetSensorsEnabled,
  657. GAMEINPUT_JoystickUpdate,
  658. GAMEINPUT_JoystickClose,
  659. GAMEINPUT_JoystickQuit,
  660. GAMEINPUT_JoystickGetGamepadMapping
  661. };
  662. #endif /* SDL_JOYSTICK_GAMEINPUT */