SDL_gameinputjoystick.cpp 28 KB

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