SDL_sysjoystick.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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_EMSCRIPTEN
  20. #include <stdio.h> /* For the definition of NULL */
  21. #include "SDL_error.h"
  22. #include "SDL_events.h"
  23. #if !SDL_EVENTS_DISABLED
  24. #include "../../events/SDL_events_c.h"
  25. #endif
  26. #include "SDL_joystick.h"
  27. #include "SDL_hints.h"
  28. #include "SDL_assert.h"
  29. #include "SDL_timer.h"
  30. #include "SDL_log.h"
  31. #include "SDL_sysjoystick_c.h"
  32. #include "../SDL_joystick_c.h"
  33. static SDL_joylist_item * JoystickByIndex(int index);
  34. static SDL_joylist_item *SDL_joylist = NULL;
  35. static SDL_joylist_item *SDL_joylist_tail = NULL;
  36. static int numjoysticks = 0;
  37. static int instance_counter = 0;
  38. EM_BOOL
  39. Emscripten_JoyStickConnected(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData)
  40. {
  41. int i;
  42. SDL_joylist_item *item;
  43. if (JoystickByIndex(gamepadEvent->index) != NULL) {
  44. return 1;
  45. }
  46. #if !SDL_EVENTS_DISABLED
  47. SDL_Event event;
  48. #endif
  49. item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
  50. if (item == NULL) {
  51. return 1;
  52. }
  53. SDL_zerop(item);
  54. item->index = gamepadEvent->index;
  55. item->name = SDL_strdup(gamepadEvent->id);
  56. if ( item->name == NULL ) {
  57. SDL_free(item);
  58. return 1;
  59. }
  60. item->mapping = SDL_strdup(gamepadEvent->mapping);
  61. if ( item->mapping == NULL ) {
  62. SDL_free(item->name);
  63. SDL_free(item);
  64. return 1;
  65. }
  66. item->naxes = gamepadEvent->numAxes;
  67. item->nbuttons = gamepadEvent->numButtons;
  68. item->device_instance = instance_counter++;
  69. item->timestamp = gamepadEvent->timestamp;
  70. for( i = 0; i < item->naxes; i++) {
  71. item->axis[i] = gamepadEvent->axis[i];
  72. }
  73. for( i = 0; i < item->nbuttons; i++) {
  74. item->analogButton[i] = gamepadEvent->analogButton[i];
  75. item->digitalButton[i] = gamepadEvent->digitalButton[i];
  76. }
  77. if (SDL_joylist_tail == NULL) {
  78. SDL_joylist = SDL_joylist_tail = item;
  79. } else {
  80. SDL_joylist_tail->next = item;
  81. SDL_joylist_tail = item;
  82. }
  83. ++numjoysticks;
  84. #ifdef DEBUG_JOYSTICK
  85. SDL_Log("Number of joysticks is %d", numjoysticks);
  86. #endif
  87. #if !SDL_EVENTS_DISABLED
  88. event.type = SDL_JOYDEVICEADDED;
  89. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  90. event.jdevice.which = numjoysticks - 1;
  91. if ( (SDL_EventOK == NULL) ||
  92. (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
  93. SDL_PushEvent(&event);
  94. }
  95. }
  96. #endif /* !SDL_EVENTS_DISABLED */
  97. #ifdef DEBUG_JOYSTICK
  98. SDL_Log("Added joystick with index %d", item->index);
  99. #endif
  100. return 1;
  101. }
  102. EM_BOOL
  103. Emscripten_JoyStickDisconnected(int eventType, const EmscriptenGamepadEvent *gamepadEvent, void *userData)
  104. {
  105. SDL_joylist_item *item = SDL_joylist;
  106. SDL_joylist_item *prev = NULL;
  107. #if !SDL_EVENTS_DISABLED
  108. SDL_Event event;
  109. #endif
  110. while (item != NULL) {
  111. if (item->index == gamepadEvent->index) {
  112. break;
  113. }
  114. prev = item;
  115. item = item->next;
  116. }
  117. if (item == NULL) {
  118. return 1;
  119. }
  120. if (item->joystick) {
  121. item->joystick->hwdata = NULL;
  122. }
  123. if (prev != NULL) {
  124. prev->next = item->next;
  125. } else {
  126. SDL_assert(SDL_joylist == item);
  127. SDL_joylist = item->next;
  128. }
  129. if (item == SDL_joylist_tail) {
  130. SDL_joylist_tail = prev;
  131. }
  132. /* Need to decrement the joystick count before we post the event */
  133. --numjoysticks;
  134. #if !SDL_EVENTS_DISABLED
  135. event.type = SDL_JOYDEVICEREMOVED;
  136. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  137. event.jdevice.which = item->device_instance;
  138. if ( (SDL_EventOK == NULL) ||
  139. (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
  140. SDL_PushEvent(&event);
  141. }
  142. }
  143. #endif /* !SDL_EVENTS_DISABLED */
  144. #ifdef DEBUG_JOYSTICK
  145. SDL_Log("Removed joystick with id %d", item->device_instance);
  146. #endif
  147. SDL_free(item->name);
  148. SDL_free(item->mapping);
  149. SDL_free(item);
  150. return 1;
  151. }
  152. /* Function to scan the system for joysticks.
  153. * It should return 0, or -1 on an unrecoverable fatal error.
  154. */
  155. int
  156. SDL_SYS_JoystickInit(void)
  157. {
  158. int retval, i, numjs;
  159. EmscriptenGamepadEvent gamepadState;
  160. numjoysticks = 0;
  161. numjs = emscripten_get_num_gamepads();
  162. /* Check if gamepad is supported by browser */
  163. if (numjs == EMSCRIPTEN_RESULT_NOT_SUPPORTED) {
  164. return -1;
  165. }
  166. /* handle already connected gamepads */
  167. if (numjs > 0) {
  168. for(i = 0; i < numjs; i++) {
  169. retval = emscripten_get_gamepad_status(i, &gamepadState);
  170. if (retval == EMSCRIPTEN_RESULT_SUCCESS) {
  171. Emscripten_JoyStickConnected(EMSCRIPTEN_EVENT_GAMEPADCONNECTED,
  172. &gamepadState,
  173. NULL);
  174. }
  175. }
  176. }
  177. retval = emscripten_set_gamepadconnected_callback(NULL,
  178. 0,
  179. Emscripten_JoyStickConnected);
  180. if(retval != EMSCRIPTEN_RESULT_SUCCESS) {
  181. SDL_SYS_JoystickQuit();
  182. return -1;
  183. }
  184. retval = emscripten_set_gamepaddisconnected_callback(NULL,
  185. 0,
  186. Emscripten_JoyStickDisconnected);
  187. if(retval != EMSCRIPTEN_RESULT_SUCCESS) {
  188. SDL_SYS_JoystickQuit();
  189. return -1;
  190. }
  191. return 0;
  192. }
  193. /* Returns item matching given SDL device index. */
  194. static SDL_joylist_item *
  195. JoystickByDeviceIndex(int device_index)
  196. {
  197. SDL_joylist_item *item = SDL_joylist;
  198. while (0 < device_index) {
  199. --device_index;
  200. item = item->next;
  201. }
  202. return item;
  203. }
  204. /* Returns item matching given HTML gamepad index. */
  205. static SDL_joylist_item *
  206. JoystickByIndex(int index)
  207. {
  208. SDL_joylist_item *item = SDL_joylist;
  209. if (index < 0) {
  210. return NULL;
  211. }
  212. while (item != NULL) {
  213. if (item->index == index) {
  214. break;
  215. }
  216. item = item->next;
  217. }
  218. return item;
  219. }
  220. int SDL_SYS_NumJoysticks()
  221. {
  222. return numjoysticks;
  223. }
  224. void SDL_SYS_JoystickDetect()
  225. {
  226. }
  227. /* Function to get the device-dependent name of a joystick */
  228. const char *
  229. SDL_SYS_JoystickNameForDeviceIndex(int device_index)
  230. {
  231. return JoystickByDeviceIndex(device_index)->name;
  232. }
  233. /* Function to perform the mapping from device index to the instance id for this index */
  234. SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
  235. {
  236. return JoystickByDeviceIndex(device_index)->device_instance;
  237. }
  238. /* Function to open a joystick for use.
  239. The joystick to open is specified by the device index.
  240. This should fill the nbuttons and naxes fields of the joystick structure.
  241. It returns 0, or -1 if there is an error.
  242. */
  243. int
  244. SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
  245. {
  246. SDL_joylist_item *item = JoystickByDeviceIndex(device_index);
  247. if (item == NULL ) {
  248. return SDL_SetError("No such device");
  249. }
  250. if (item->joystick != NULL) {
  251. return SDL_SetError("Joystick already opened");
  252. }
  253. joystick->instance_id = item->device_instance;
  254. joystick->hwdata = (struct joystick_hwdata *) item;
  255. item->joystick = joystick;
  256. /* HTML5 Gamepad API doesn't say anything about these */
  257. joystick->nhats = 0;
  258. joystick->nballs = 0;
  259. joystick->nbuttons = item->nbuttons;
  260. joystick->naxes = item->naxes;
  261. return (0);
  262. }
  263. /* Function to determine is this joystick is attached to the system right now */
  264. SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
  265. {
  266. return joystick->hwdata != NULL;
  267. }
  268. /* Function to update the state of a joystick - called as a device poll.
  269. * This function shouldn't update the joystick structure directly,
  270. * but instead should call SDL_PrivateJoystick*() to deliver events
  271. * and update joystick device state.
  272. */
  273. void
  274. SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
  275. {
  276. EmscriptenGamepadEvent gamepadState;
  277. SDL_joylist_item *item = (SDL_joylist_item *) joystick->hwdata;
  278. int i, result, buttonState;
  279. if (item) {
  280. result = emscripten_get_gamepad_status(item->index, &gamepadState);
  281. if( result == EMSCRIPTEN_RESULT_SUCCESS) {
  282. if(gamepadState.timestamp == 0 || gamepadState.timestamp != item->timestamp) {
  283. for(i = 0; i < item->nbuttons; i++) {
  284. if(item->digitalButton[i] != gamepadState.digitalButton[i]) {
  285. buttonState = gamepadState.digitalButton[i]? SDL_PRESSED: SDL_RELEASED;
  286. SDL_PrivateJoystickButton(item->joystick, i, buttonState);
  287. }
  288. /* store values to compare them in the next update */
  289. item->analogButton[i] = gamepadState.analogButton[i];
  290. item->digitalButton[i] = gamepadState.digitalButton[i];
  291. }
  292. for(i = 0; i < item->naxes; i++) {
  293. if(item->axis[i] != gamepadState.axis[i]) {
  294. // do we need to do conversion?
  295. SDL_PrivateJoystickAxis(item->joystick, i,
  296. (Sint16) (32767.*gamepadState.axis[i]));
  297. }
  298. /* store to compare in next update */
  299. item->axis[i] = gamepadState.axis[i];
  300. }
  301. item->timestamp = gamepadState.timestamp;
  302. }
  303. }
  304. }
  305. }
  306. /* Function to close a joystick after use */
  307. void
  308. SDL_SYS_JoystickClose(SDL_Joystick * joystick)
  309. {
  310. }
  311. /* Function to perform any system-specific joystick related cleanup */
  312. void
  313. SDL_SYS_JoystickQuit(void)
  314. {
  315. SDL_joylist_item *item = NULL;
  316. SDL_joylist_item *next = NULL;
  317. for (item = SDL_joylist; item; item = next) {
  318. next = item->next;
  319. SDL_free(item->mapping);
  320. SDL_free(item->name);
  321. SDL_free(item);
  322. }
  323. SDL_joylist = SDL_joylist_tail = NULL;
  324. numjoysticks = 0;
  325. instance_counter = 0;
  326. emscripten_set_gamepadconnected_callback(NULL, 0, NULL);
  327. emscripten_set_gamepaddisconnected_callback(NULL, 0, NULL);
  328. }
  329. SDL_JoystickGUID
  330. SDL_SYS_JoystickGetDeviceGUID(int device_index)
  331. {
  332. SDL_JoystickGUID guid;
  333. /* the GUID is just the first 16 chars of the name for now */
  334. const char *name = SDL_SYS_JoystickNameForDeviceIndex(device_index);
  335. SDL_zero(guid);
  336. SDL_memcpy(&guid, name, SDL_min(sizeof(guid), SDL_strlen(name)));
  337. return guid;
  338. }
  339. SDL_JoystickGUID
  340. SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
  341. {
  342. SDL_JoystickGUID guid;
  343. /* the GUID is just the first 16 chars of the name for now */
  344. const char *name = joystick->name;
  345. SDL_zero(guid);
  346. SDL_memcpy(&guid, name, SDL_min(sizeof(guid), SDL_strlen(name)));
  347. return guid;
  348. }
  349. #endif /* SDL_JOYSTICK_EMSCRIPTEN */