SDL_sysjoystick.c 12 KB

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