SDL_sysjoystick.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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_EMSCRIPTEN
  20. #include <stdio.h> // For the definition of NULL
  21. #include "SDL_sysjoystick_c.h"
  22. #include "../SDL_joystick_c.h"
  23. #include "../usb_ids.h"
  24. static SDL_joylist_item *JoystickByIndex(int index);
  25. static SDL_joylist_item *SDL_joylist = NULL;
  26. static SDL_joylist_item *SDL_joylist_tail = NULL;
  27. static int numjoysticks = 0;
  28. static int SDL_GetEmscriptenJoystickVendor(int device_index)
  29. {
  30. return MAIN_THREAD_EM_ASM_INT({
  31. let gamepad = navigator['getGamepads']()[$0];
  32. if (!gamepad) {
  33. return 0;
  34. }
  35. // Chrome, Edge, Opera: Wireless Controller (STANDARD GAMEPAD Vendor: 054c Product: 09cc)
  36. let vendor_str = 'Vendor: ';
  37. if (gamepad['id']['indexOf'](vendor_str) > 0) {
  38. let vendor_str_index = gamepad['id']['indexOf'](vendor_str) + vendor_str['length'];
  39. return parseInt(gamepad['id']['substr'](vendor_str_index, 4), 16);
  40. }
  41. // Firefox, Safari: 046d-c216-Logitech Dual Action (or 46d-c216-Logicool Dual Action)
  42. let id_split = gamepad['id']['split']('-');
  43. if (id_split['length'] > 1 && !isNaN(parseInt(id_split[0], 16))) {
  44. return parseInt(id_split[0], 16);
  45. }
  46. return 0;
  47. }, device_index);
  48. }
  49. static int SDL_GetEmscriptenJoystickProduct(int device_index)
  50. {
  51. return MAIN_THREAD_EM_ASM_INT({
  52. let gamepad = navigator['getGamepads']()[$0];
  53. if (!gamepad) {
  54. return 0;
  55. }
  56. // Chrome, Edge, Opera: Wireless Controller (STANDARD GAMEPAD Vendor: 054c Product: 09cc)
  57. let product_str = 'Product: ';
  58. if (gamepad['id']['indexOf'](product_str) > 0) {
  59. let product_str_index = gamepad['id']['indexOf'](product_str) + product_str['length'];
  60. return parseInt(gamepad['id']['substr'](product_str_index, 4), 16);
  61. }
  62. // Firefox, Safari: 046d-c216-Logitech Dual Action (or 46d-c216-Logicool Dual Action)
  63. let id_split = gamepad['id']['split']('-');
  64. if (id_split['length'] > 1 && !isNaN(parseInt(id_split[1], 16))) {
  65. return parseInt(id_split[1], 16);
  66. }
  67. return 0;
  68. }, device_index);
  69. }
  70. static int SDL_IsEmscriptenJoystickXInput(int device_index)
  71. {
  72. return MAIN_THREAD_EM_ASM_INT({
  73. let gamepad = navigator['getGamepads']()[$0];
  74. if (!gamepad) {
  75. return 0;
  76. }
  77. // Chrome, Edge, Opera: Xbox 360 Controller (XInput STANDARD GAMEPAD)
  78. // Firefox: xinput
  79. // TODO: Safari
  80. return gamepad['id']['toLowerCase']()['indexOf']('xinput') >= 0;
  81. }, device_index);
  82. }
  83. static int SDL_GetEmscriptenOSID()
  84. {
  85. return MAIN_THREAD_EM_ASM_INT({
  86. const os = ([
  87. 'Android',
  88. 'Linux',
  89. 'iPhone',
  90. 'Macintosh',
  91. 'Windows',
  92. ]);
  93. const ua = navigator['userAgent'];
  94. for (let i = 0; i < os.length; i++) {
  95. if (ua['indexOf'](os[i]) >= 0) {
  96. return i + 1;
  97. }
  98. }
  99. return 0;
  100. });
  101. }
  102. static EM_BOOL Emscripten_JoyStickConnected(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData)
  103. {
  104. SDL_joylist_item *item;
  105. int i;
  106. Uint16 bus;
  107. Uint16 vendor, product;
  108. Uint8 os_id;
  109. bool is_xinput;
  110. SDL_LockJoysticks();
  111. if (JoystickByIndex(gamepadEvent->index) != NULL) {
  112. goto done;
  113. }
  114. item = (SDL_joylist_item *)SDL_malloc(sizeof(SDL_joylist_item));
  115. if (!item) {
  116. goto done;
  117. }
  118. SDL_zerop(item);
  119. item->index = gamepadEvent->index;
  120. bus = SDL_HARDWARE_BUS_UNKNOWN;
  121. vendor = SDL_GetEmscriptenJoystickVendor(gamepadEvent->index);
  122. product = SDL_GetEmscriptenJoystickProduct(gamepadEvent->index);
  123. is_xinput = SDL_IsEmscriptenJoystickXInput(gamepadEvent->index);
  124. os_id = SDL_GetEmscriptenOSID();
  125. item->trigger_rumble_available = MAIN_THREAD_EM_ASM_INT({
  126. let gamepad = navigator['getGamepads']()[$0];
  127. // This effect is not supported in Safari, so it's okay for us to check the vibrationActuator.effects array here for the browsers that do support it
  128. if (!gamepad || !gamepad['vibrationActuator'] || !gamepad['vibrationActuator']['effects'] || !gamepad['vibrationActuator']['effects']['includes']('trigger-rumble')) {
  129. return false;
  130. }
  131. return true;
  132. }, item->index);
  133. if (os_id != 0) {
  134. if (os_id == 1 || os_id == 3) { // Android or iOS (mobile)
  135. bus = SDL_HARDWARE_BUS_BLUETOOTH;
  136. } else { // Desktop
  137. bus = SDL_HARDWARE_BUS_USB;
  138. }
  139. }
  140. if (!vendor && !product && is_xinput) {
  141. // Use a generic VID/PID representing an XInput controller
  142. vendor = USB_VENDOR_MICROSOFT;
  143. product = USB_PRODUCT_XBOX360_XUSB_CONTROLLER;
  144. if (item->trigger_rumble_available) {
  145. // Assume Xbox One S Controller
  146. if (bus == SDL_HARDWARE_BUS_BLUETOOTH) {
  147. product = USB_PRODUCT_XBOX_ONE_S_REV1_BLUETOOTH;
  148. } else {
  149. product = USB_PRODUCT_XBOX_ONE_S;
  150. }
  151. }
  152. }
  153. if (SDL_strcmp(gamepadEvent->mapping, "standard") == 0) {
  154. // We should differentiate between devices that are mapped or unmapped by the browser.
  155. os_id += 0x80;
  156. }
  157. item->name = SDL_CreateJoystickName(vendor, product, NULL, gamepadEvent->id);
  158. if (!item->name) {
  159. SDL_free(item);
  160. goto done;
  161. }
  162. if (vendor && product) {
  163. item->guid = SDL_CreateJoystickGUID(bus, vendor, product, 0, NULL, gamepadEvent->id, 0, os_id);
  164. } else {
  165. item->guid = SDL_CreateJoystickGUIDForName(item->name);
  166. item->guid.data[15] = os_id;
  167. }
  168. item->mapping = SDL_strdup(gamepadEvent->mapping);
  169. if (!item->mapping) {
  170. SDL_free(item->name);
  171. SDL_free(item);
  172. goto done;
  173. }
  174. const int real_button_count = gamepadEvent->numButtons;
  175. const int real_axis_count = gamepadEvent->numAxes;
  176. int first_trigger_button = -1;
  177. int first_hat_button = -1;
  178. int num_buttons = gamepadEvent->numButtons;
  179. int num_axes = gamepadEvent->numAxes;
  180. bool triggers_are_buttons = false;
  181. if ((SDL_strcmp(gamepadEvent->mapping, "standard") == 0) && (num_buttons >= 16)) { // maps to a game console gamepad layout, turn the d-pad into a hat, treat triggers as analog.
  182. num_buttons -= 4; // 4 dpad buttons become a hat.
  183. first_hat_button = 12;
  184. if (num_axes == 4) { // Chrome gives the triggers analog button values, Firefox exposes them as extra axes. Both have the digital buttons.
  185. num_axes += 2; // the two trigger "buttons"
  186. triggers_are_buttons = true;
  187. }
  188. // dump the digital trigger buttons in any case.
  189. first_trigger_button = 6;
  190. num_buttons -= 2;
  191. }
  192. item->first_hat_button = first_hat_button;
  193. item->first_trigger_button = first_trigger_button;
  194. item->triggers_are_buttons = triggers_are_buttons;
  195. item->nhats = (first_hat_button >= 0) ? 1 : 0;
  196. item->naxes = num_axes;
  197. item->nbuttons = num_buttons;
  198. item->device_instance = SDL_GetNextObjectID();
  199. item->timestamp = gamepadEvent->timestamp;
  200. int buttonidx = 0;
  201. for (i = 0; i < real_button_count; i++, buttonidx++) {
  202. if (buttonidx == first_hat_button) {
  203. buttonidx += 4; // skip these buttons, we're treating them as hat input.
  204. } else if (buttonidx == first_trigger_button) {
  205. buttonidx += 2; // skip these buttons, we're treating them as axes.
  206. }
  207. item->analogButton[i] = gamepadEvent->analogButton[buttonidx];
  208. item->digitalButton[i] = gamepadEvent->digitalButton[buttonidx];
  209. }
  210. for (i = 0; i < real_axis_count; i++) {
  211. item->axis[i] = gamepadEvent->axis[i];
  212. }
  213. if (item->triggers_are_buttons) {
  214. item->axis[real_axis_count] = (gamepadEvent->analogButton[first_trigger_button] * 2.0f) - 1.0f;
  215. item->axis[real_axis_count+1] = (gamepadEvent->analogButton[first_trigger_button+1] * 2.0f) - 1.0f;
  216. }
  217. SDL_assert(item->nhats <= 1); // there is (currently) only ever one of these, faked from the d-pad buttons.
  218. if (first_hat_button != -1) {
  219. Uint8 value = SDL_HAT_CENTERED;
  220. // this currently expects the first button to be up, then down, then left, then right.
  221. if (gamepadEvent->digitalButton[first_hat_button + 0]) {
  222. value |= SDL_HAT_UP;
  223. }
  224. if (gamepadEvent->digitalButton[first_hat_button + 1]) {
  225. value |= SDL_HAT_DOWN;
  226. }
  227. if (gamepadEvent->digitalButton[first_hat_button + 2]) {
  228. value |= SDL_HAT_LEFT;
  229. }
  230. if (gamepadEvent->digitalButton[first_hat_button + 3]) {
  231. value |= SDL_HAT_RIGHT;
  232. }
  233. item->hat = value;
  234. }
  235. if (!SDL_joylist_tail) {
  236. SDL_joylist = SDL_joylist_tail = item;
  237. } else {
  238. SDL_joylist_tail->next = item;
  239. SDL_joylist_tail = item;
  240. }
  241. ++numjoysticks;
  242. SDL_PrivateJoystickAdded(item->device_instance);
  243. #ifdef DEBUG_JOYSTICK
  244. SDL_Log("Number of joysticks is %d", numjoysticks);
  245. #endif
  246. #ifdef DEBUG_JOYSTICK
  247. SDL_Log("Added joystick with index %d", item->index);
  248. #endif
  249. done:
  250. SDL_UnlockJoysticks();
  251. return 1;
  252. }
  253. static EM_BOOL Emscripten_JoyStickDisconnected(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData)
  254. {
  255. SDL_joylist_item *item = SDL_joylist;
  256. SDL_joylist_item *prev = NULL;
  257. SDL_LockJoysticks();
  258. while (item) {
  259. if (item->index == gamepadEvent->index) {
  260. break;
  261. }
  262. prev = item;
  263. item = item->next;
  264. }
  265. if (!item) {
  266. goto done;
  267. }
  268. if (item->joystick) {
  269. item->joystick->hwdata = NULL;
  270. }
  271. if (prev) {
  272. prev->next = item->next;
  273. } else {
  274. SDL_assert(SDL_joylist == item);
  275. SDL_joylist = item->next;
  276. }
  277. if (item == SDL_joylist_tail) {
  278. SDL_joylist_tail = prev;
  279. }
  280. // Need to decrement the joystick count before we post the event
  281. --numjoysticks;
  282. SDL_PrivateJoystickRemoved(item->device_instance);
  283. #ifdef DEBUG_JOYSTICK
  284. SDL_Log("Removed joystick with id %d", item->device_instance);
  285. #endif
  286. SDL_free(item->name);
  287. SDL_free(item->mapping);
  288. SDL_free(item);
  289. done:
  290. SDL_UnlockJoysticks();
  291. return 1;
  292. }
  293. // Function to perform any system-specific joystick related cleanup
  294. static void EMSCRIPTEN_JoystickQuit(void)
  295. {
  296. SDL_joylist_item *item = NULL;
  297. SDL_joylist_item *next = NULL;
  298. for (item = SDL_joylist; item; item = next) {
  299. next = item->next;
  300. SDL_free(item->mapping);
  301. SDL_free(item->name);
  302. SDL_free(item);
  303. }
  304. SDL_joylist = SDL_joylist_tail = NULL;
  305. numjoysticks = 0;
  306. emscripten_set_gamepadconnected_callback(NULL, 0, NULL);
  307. emscripten_set_gamepaddisconnected_callback(NULL, 0, NULL);
  308. }
  309. // Function to scan the system for joysticks.
  310. static bool EMSCRIPTEN_JoystickInit(void)
  311. {
  312. int rc, i, numjs;
  313. EmscriptenGamepadEvent gamepadState;
  314. numjoysticks = 0;
  315. rc = emscripten_sample_gamepad_data();
  316. // Check if gamepad is supported by browser
  317. if (rc == EMSCRIPTEN_RESULT_NOT_SUPPORTED) {
  318. return SDL_SetError("Gamepads not supported");
  319. }
  320. numjs = emscripten_get_num_gamepads();
  321. // handle already connected gamepads
  322. if (numjs > 0) {
  323. for (i = 0; i < numjs; i++) {
  324. rc = emscripten_get_gamepad_status(i, &gamepadState);
  325. if (rc == EMSCRIPTEN_RESULT_SUCCESS) {
  326. Emscripten_JoyStickConnected(EMSCRIPTEN_EVENT_GAMEPADCONNECTED,
  327. &gamepadState,
  328. NULL);
  329. }
  330. }
  331. }
  332. rc = emscripten_set_gamepadconnected_callback(NULL,
  333. 0,
  334. Emscripten_JoyStickConnected);
  335. if (rc != EMSCRIPTEN_RESULT_SUCCESS) {
  336. EMSCRIPTEN_JoystickQuit();
  337. return SDL_SetError("Could not set gamepad connect callback");
  338. }
  339. rc = emscripten_set_gamepaddisconnected_callback(NULL,
  340. 0,
  341. Emscripten_JoyStickDisconnected);
  342. if (rc != EMSCRIPTEN_RESULT_SUCCESS) {
  343. EMSCRIPTEN_JoystickQuit();
  344. return SDL_SetError("Could not set gamepad disconnect callback");
  345. }
  346. return true;
  347. }
  348. // Returns item matching given SDL device index.
  349. static SDL_joylist_item *JoystickByDeviceIndex(int device_index)
  350. {
  351. SDL_joylist_item *item = SDL_joylist;
  352. while (0 < device_index) {
  353. --device_index;
  354. item = item->next;
  355. }
  356. return item;
  357. }
  358. // Returns item matching given HTML gamepad index.
  359. static SDL_joylist_item *JoystickByIndex(int index)
  360. {
  361. SDL_joylist_item *item = SDL_joylist;
  362. if (index < 0) {
  363. return NULL;
  364. }
  365. while (item) {
  366. if (item->index == index) {
  367. break;
  368. }
  369. item = item->next;
  370. }
  371. return item;
  372. }
  373. static int EMSCRIPTEN_JoystickGetCount(void)
  374. {
  375. return numjoysticks;
  376. }
  377. static void EMSCRIPTEN_JoystickDetect(void)
  378. {
  379. }
  380. static bool EMSCRIPTEN_JoystickIsDevicePresent(Uint16 vendor_id, Uint16 product_id, Uint16 version, const char *name)
  381. {
  382. // We don't override any other drivers
  383. return false;
  384. }
  385. static const char *EMSCRIPTEN_JoystickGetDeviceName(int device_index)
  386. {
  387. return JoystickByDeviceIndex(device_index)->name;
  388. }
  389. static const char *EMSCRIPTEN_JoystickGetDevicePath(int device_index)
  390. {
  391. return NULL;
  392. }
  393. static int EMSCRIPTEN_JoystickGetDeviceSteamVirtualGamepadSlot(int device_index)
  394. {
  395. return -1;
  396. }
  397. static int EMSCRIPTEN_JoystickGetDevicePlayerIndex(int device_index)
  398. {
  399. return -1;
  400. }
  401. static void EMSCRIPTEN_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  402. {
  403. }
  404. static SDL_JoystickID EMSCRIPTEN_JoystickGetDeviceInstanceID(int device_index)
  405. {
  406. return JoystickByDeviceIndex(device_index)->device_instance;
  407. }
  408. static bool EMSCRIPTEN_JoystickOpen(SDL_Joystick *joystick, int device_index)
  409. {
  410. SDL_joylist_item *item = JoystickByDeviceIndex(device_index);
  411. if (!item) {
  412. return SDL_SetError("No such device");
  413. }
  414. if (item->joystick) {
  415. return SDL_SetError("Joystick already opened");
  416. }
  417. joystick->hwdata = (struct joystick_hwdata *)item;
  418. item->joystick = joystick;
  419. // HTML5 Gamepad API doesn't offer hats, but we can fake it from the d-pad buttons on the "standard" mapping.
  420. joystick->nhats = item->nhats;
  421. joystick->nbuttons = item->nbuttons;
  422. joystick->naxes = item->naxes;
  423. item->rumble_available = MAIN_THREAD_EM_ASM_INT({
  424. let gamepad = navigator['getGamepads']()[$0];
  425. // Don't check the vibrationActuator.effects array here, because it's not defined in Safari
  426. if (!gamepad || !gamepad['vibrationActuator']) {
  427. return false;
  428. }
  429. return true;
  430. }, item->index);
  431. if (item->rumble_available) {
  432. SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_RUMBLE_BOOLEAN, true);
  433. }
  434. // item->trigger_rumble_available is set in Emscripten_JoyStickConnected
  435. if (item->trigger_rumble_available) {
  436. SDL_SetBooleanProperty(SDL_GetJoystickProperties(joystick), SDL_PROP_JOYSTICK_CAP_TRIGGER_RUMBLE_BOOLEAN, true);
  437. }
  438. return true;
  439. }
  440. /* Function to update the state of a joystick - called as a device poll.
  441. * This function shouldn't update the joystick structure directly,
  442. * but instead should call SDL_PrivateJoystick*() to deliver events
  443. * and update joystick device state.
  444. */
  445. static void EMSCRIPTEN_JoystickUpdate(SDL_Joystick *joystick)
  446. {
  447. EmscriptenGamepadEvent gamepadState;
  448. SDL_joylist_item *item = (SDL_joylist_item *)joystick->hwdata;
  449. int i, result;
  450. Uint64 timestamp = SDL_GetTicksNS();
  451. emscripten_sample_gamepad_data();
  452. if (item) {
  453. result = emscripten_get_gamepad_status(item->index, &gamepadState);
  454. if (result == EMSCRIPTEN_RESULT_SUCCESS) {
  455. if (gamepadState.timestamp == 0 || gamepadState.timestamp != item->timestamp) {
  456. const int first_hat_button = item->first_hat_button;
  457. const int first_trigger_button = item->first_trigger_button;
  458. const int real_button_count = gamepadState.numButtons;
  459. const int real_axis_count = gamepadState.numAxes;
  460. int buttonidx = 0;
  461. for (i = 0; i < real_button_count; i++, buttonidx++) {
  462. if (buttonidx == first_hat_button) {
  463. buttonidx += 4; // skip these buttons, we're treating them as hat input.
  464. } else if (buttonidx == first_trigger_button) {
  465. buttonidx += 2; // skip these buttons, we're treating them as axes.
  466. }
  467. if (item->digitalButton[i] != gamepadState.digitalButton[buttonidx]) {
  468. const bool down = (gamepadState.digitalButton[buttonidx] != 0);
  469. SDL_SendJoystickButton(timestamp, item->joystick, i, down);
  470. }
  471. // store values to compare them in the next update
  472. item->analogButton[i] = gamepadState.analogButton[buttonidx];
  473. item->digitalButton[i] = gamepadState.digitalButton[buttonidx];
  474. }
  475. for (i = 0; i < real_axis_count; i++) {
  476. if (item->axis[i] != gamepadState.axis[i]) {
  477. SDL_SendJoystickAxis(timestamp, item->joystick, i, (Sint16)(32767.0f * gamepadState.axis[i]));
  478. item->axis[i] = gamepadState.axis[i];
  479. }
  480. }
  481. if (item->triggers_are_buttons) {
  482. for (i = 0; i < 2; i++) {
  483. if (item->axis[real_axis_count+i] != gamepadState.analogButton[first_trigger_button+i]) {
  484. SDL_SendJoystickAxis(timestamp, item->joystick, real_axis_count+i, (Sint16)(32767.0f * ((gamepadState.analogButton[first_trigger_button+i] * 2.0f) - 1.0f)));
  485. item->axis[real_axis_count+i] = gamepadState.analogButton[first_trigger_button+i];
  486. }
  487. }
  488. }
  489. SDL_assert(item->nhats <= 1); // there is (currently) only ever one of these, faked from the d-pad buttons.
  490. if (item->nhats) {
  491. Uint8 value = SDL_HAT_CENTERED;
  492. // this currently expects the first button to be up, then down, then left, then right.
  493. if (gamepadState.digitalButton[first_hat_button + 0]) {
  494. value |= SDL_HAT_UP;
  495. } else if (gamepadState.digitalButton[first_hat_button + 1]) {
  496. value |= SDL_HAT_DOWN;
  497. }
  498. if (gamepadState.digitalButton[first_hat_button + 2]) {
  499. value |= SDL_HAT_LEFT;
  500. } else if (gamepadState.digitalButton[first_hat_button + 3]) {
  501. value |= SDL_HAT_RIGHT;
  502. }
  503. if (item->hat != value) {
  504. item->hat = value;
  505. SDL_SendJoystickHat(timestamp, item->joystick, 0, value);
  506. }
  507. }
  508. item->timestamp = gamepadState.timestamp;
  509. }
  510. }
  511. }
  512. }
  513. // Function to close a joystick after use
  514. static void EMSCRIPTEN_JoystickClose(SDL_Joystick *joystick)
  515. {
  516. SDL_joylist_item *item = (SDL_joylist_item *)joystick->hwdata;
  517. if (item) {
  518. item->joystick = NULL;
  519. }
  520. }
  521. static SDL_GUID EMSCRIPTEN_JoystickGetDeviceGUID(int device_index)
  522. {
  523. return JoystickByDeviceIndex(device_index)->guid;
  524. }
  525. static bool Emscripten_UpdateRumble(SDL_joylist_item *item)
  526. {
  527. bool result = MAIN_THREAD_EM_ASM_INT({
  528. let gamepad = navigator['getGamepads']()[$0];
  529. if (!gamepad) {
  530. return false;
  531. }
  532. // We check if rumble is available in EMSCRIPTEN_JoystickRumble() and EMSCRIPTEN_JoystickRumbleTriggers().
  533. // From my testing using "dual-rumble" here covers both main rumble and trigger rumble.
  534. gamepad['vibrationActuator']['playEffect']('dual-rumble', {
  535. 'startDelay': 0,
  536. 'duration': 3000,
  537. 'weakMagnitude': $1 / 0xFFFF,
  538. 'strongMagnitude': $2 / 0xFFFF,
  539. 'leftTrigger': $3 / 0xFFFF,
  540. 'rightTrigger': $4 / 0xFFFF,
  541. });
  542. return true;
  543. }, item->index, item->weak_magnitude_rumble, item->strong_magnitude_rumble, item->left_trigger_rumble, item->right_trigger_rumble);
  544. return result;
  545. }
  546. static bool EMSCRIPTEN_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  547. {
  548. SDL_joylist_item *item = (SDL_joylist_item *)joystick->hwdata;
  549. if (!item || !item->rumble_available) {
  550. return SDL_Unsupported();
  551. }
  552. item->strong_magnitude_rumble = low_frequency_rumble;
  553. item->weak_magnitude_rumble = high_frequency_rumble;
  554. return Emscripten_UpdateRumble(item);
  555. }
  556. static bool EMSCRIPTEN_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  557. {
  558. SDL_joylist_item *item = (SDL_joylist_item *)joystick->hwdata;
  559. if (!item || !item->trigger_rumble_available) {
  560. return SDL_Unsupported();
  561. }
  562. item->left_trigger_rumble = left_rumble;
  563. item->right_trigger_rumble = right_rumble;
  564. return Emscripten_UpdateRumble(item);
  565. }
  566. static bool EMSCRIPTEN_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  567. {
  568. return false;
  569. }
  570. static bool EMSCRIPTEN_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  571. {
  572. return SDL_Unsupported();
  573. }
  574. static bool EMSCRIPTEN_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
  575. {
  576. return SDL_Unsupported();
  577. }
  578. static bool EMSCRIPTEN_JoystickSetSensorsEnabled(SDL_Joystick *joystick, bool enabled)
  579. {
  580. return SDL_Unsupported();
  581. }
  582. SDL_JoystickDriver SDL_EMSCRIPTEN_JoystickDriver = {
  583. EMSCRIPTEN_JoystickInit,
  584. EMSCRIPTEN_JoystickGetCount,
  585. EMSCRIPTEN_JoystickDetect,
  586. EMSCRIPTEN_JoystickIsDevicePresent,
  587. EMSCRIPTEN_JoystickGetDeviceName,
  588. EMSCRIPTEN_JoystickGetDevicePath,
  589. EMSCRIPTEN_JoystickGetDeviceSteamVirtualGamepadSlot,
  590. EMSCRIPTEN_JoystickGetDevicePlayerIndex,
  591. EMSCRIPTEN_JoystickSetDevicePlayerIndex,
  592. EMSCRIPTEN_JoystickGetDeviceGUID,
  593. EMSCRIPTEN_JoystickGetDeviceInstanceID,
  594. EMSCRIPTEN_JoystickOpen,
  595. EMSCRIPTEN_JoystickRumble,
  596. EMSCRIPTEN_JoystickRumbleTriggers,
  597. EMSCRIPTEN_JoystickSetLED,
  598. EMSCRIPTEN_JoystickSendEffect,
  599. EMSCRIPTEN_JoystickSetSensorsEnabled,
  600. EMSCRIPTEN_JoystickUpdate,
  601. EMSCRIPTEN_JoystickClose,
  602. EMSCRIPTEN_JoystickQuit,
  603. EMSCRIPTEN_JoystickGetGamepadMapping
  604. };
  605. #endif // SDL_JOYSTICK_EMSCRIPTEN