SDL_joystick.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2015 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. /* This is the joystick API for Simple DirectMedia Layer */
  20. #include "SDL.h"
  21. #include "SDL_events.h"
  22. #include "SDL_sysjoystick.h"
  23. #include "SDL_assert.h"
  24. #include "SDL_hints.h"
  25. #if !SDL_EVENTS_DISABLED
  26. #include "../events/SDL_events_c.h"
  27. #endif
  28. static SDL_bool SDL_joystick_allows_background_events = SDL_FALSE;
  29. static SDL_Joystick *SDL_joysticks = NULL;
  30. static SDL_Joystick *SDL_updating_joystick = NULL;
  31. static void
  32. SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
  33. {
  34. if (hint && *hint == '1') {
  35. SDL_joystick_allows_background_events = SDL_TRUE;
  36. } else {
  37. SDL_joystick_allows_background_events = SDL_FALSE;
  38. }
  39. }
  40. int
  41. SDL_JoystickInit(void)
  42. {
  43. int status;
  44. /* See if we should allow joystick events while in the background */
  45. SDL_AddHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
  46. SDL_JoystickAllowBackgroundEventsChanged, NULL);
  47. #if !SDL_EVENTS_DISABLED
  48. if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
  49. return -1;
  50. }
  51. #endif /* !SDL_EVENTS_DISABLED */
  52. status = SDL_SYS_JoystickInit();
  53. if (status >= 0) {
  54. status = 0;
  55. }
  56. return (status);
  57. }
  58. /*
  59. * Count the number of joysticks attached to the system
  60. */
  61. int
  62. SDL_NumJoysticks(void)
  63. {
  64. return SDL_SYS_NumJoysticks();
  65. }
  66. /*
  67. * Get the implementation dependent name of a joystick
  68. */
  69. const char *
  70. SDL_JoystickNameForIndex(int device_index)
  71. {
  72. if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
  73. SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
  74. return (NULL);
  75. }
  76. return (SDL_SYS_JoystickNameForDeviceIndex(device_index));
  77. }
  78. /*
  79. * Open a joystick for use - the index passed as an argument refers to
  80. * the N'th joystick on the system. This index is the value which will
  81. * identify this joystick in future joystick events.
  82. *
  83. * This function returns a joystick identifier, or NULL if an error occurred.
  84. */
  85. SDL_Joystick *
  86. SDL_JoystickOpen(int device_index)
  87. {
  88. SDL_Joystick *joystick;
  89. SDL_Joystick *joysticklist;
  90. const char *joystickname = NULL;
  91. if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
  92. SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
  93. return (NULL);
  94. }
  95. joysticklist = SDL_joysticks;
  96. /* If the joystick is already open, return it
  97. * it is important that we have a single joystick * for each instance id
  98. */
  99. while (joysticklist) {
  100. if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == joysticklist->instance_id) {
  101. joystick = joysticklist;
  102. ++joystick->ref_count;
  103. return (joystick);
  104. }
  105. joysticklist = joysticklist->next;
  106. }
  107. /* Create and initialize the joystick */
  108. joystick = (SDL_Joystick *) SDL_malloc((sizeof *joystick));
  109. if (joystick == NULL) {
  110. SDL_OutOfMemory();
  111. return NULL;
  112. }
  113. SDL_memset(joystick, 0, (sizeof *joystick));
  114. if (SDL_SYS_JoystickOpen(joystick, device_index) < 0) {
  115. SDL_free(joystick);
  116. return NULL;
  117. }
  118. joystickname = SDL_SYS_JoystickNameForDeviceIndex(device_index);
  119. if (joystickname)
  120. joystick->name = SDL_strdup(joystickname);
  121. else
  122. joystick->name = NULL;
  123. if (joystick->naxes > 0) {
  124. joystick->axes = (Sint16 *) SDL_malloc
  125. (joystick->naxes * sizeof(Sint16));
  126. }
  127. if (joystick->nhats > 0) {
  128. joystick->hats = (Uint8 *) SDL_malloc
  129. (joystick->nhats * sizeof(Uint8));
  130. }
  131. if (joystick->nballs > 0) {
  132. joystick->balls = (struct balldelta *) SDL_malloc
  133. (joystick->nballs * sizeof(*joystick->balls));
  134. }
  135. if (joystick->nbuttons > 0) {
  136. joystick->buttons = (Uint8 *) SDL_malloc
  137. (joystick->nbuttons * sizeof(Uint8));
  138. }
  139. if (((joystick->naxes > 0) && !joystick->axes)
  140. || ((joystick->nhats > 0) && !joystick->hats)
  141. || ((joystick->nballs > 0) && !joystick->balls)
  142. || ((joystick->nbuttons > 0) && !joystick->buttons)) {
  143. SDL_OutOfMemory();
  144. SDL_JoystickClose(joystick);
  145. return NULL;
  146. }
  147. if (joystick->axes) {
  148. SDL_memset(joystick->axes, 0, joystick->naxes * sizeof(Sint16));
  149. }
  150. if (joystick->hats) {
  151. SDL_memset(joystick->hats, 0, joystick->nhats * sizeof(Uint8));
  152. }
  153. if (joystick->balls) {
  154. SDL_memset(joystick->balls, 0,
  155. joystick->nballs * sizeof(*joystick->balls));
  156. }
  157. if (joystick->buttons) {
  158. SDL_memset(joystick->buttons, 0, joystick->nbuttons * sizeof(Uint8));
  159. }
  160. /* Add joystick to list */
  161. ++joystick->ref_count;
  162. /* Link the joystick in the list */
  163. joystick->next = SDL_joysticks;
  164. SDL_joysticks = joystick;
  165. SDL_SYS_JoystickUpdate(joystick);
  166. return (joystick);
  167. }
  168. /*
  169. * Checks to make sure the joystick is valid.
  170. */
  171. int
  172. SDL_PrivateJoystickValid(SDL_Joystick * joystick)
  173. {
  174. int valid;
  175. if (joystick == NULL) {
  176. SDL_SetError("Joystick hasn't been opened yet");
  177. valid = 0;
  178. } else {
  179. valid = 1;
  180. }
  181. return valid;
  182. }
  183. /*
  184. * Get the number of multi-dimensional axis controls on a joystick
  185. */
  186. int
  187. SDL_JoystickNumAxes(SDL_Joystick * joystick)
  188. {
  189. if (!SDL_PrivateJoystickValid(joystick)) {
  190. return (-1);
  191. }
  192. return (joystick->naxes);
  193. }
  194. /*
  195. * Get the number of hats on a joystick
  196. */
  197. int
  198. SDL_JoystickNumHats(SDL_Joystick * joystick)
  199. {
  200. if (!SDL_PrivateJoystickValid(joystick)) {
  201. return (-1);
  202. }
  203. return (joystick->nhats);
  204. }
  205. /*
  206. * Get the number of trackballs on a joystick
  207. */
  208. int
  209. SDL_JoystickNumBalls(SDL_Joystick * joystick)
  210. {
  211. if (!SDL_PrivateJoystickValid(joystick)) {
  212. return (-1);
  213. }
  214. return (joystick->nballs);
  215. }
  216. /*
  217. * Get the number of buttons on a joystick
  218. */
  219. int
  220. SDL_JoystickNumButtons(SDL_Joystick * joystick)
  221. {
  222. if (!SDL_PrivateJoystickValid(joystick)) {
  223. return (-1);
  224. }
  225. return (joystick->nbuttons);
  226. }
  227. /*
  228. * Get the current state of an axis control on a joystick
  229. */
  230. Sint16
  231. SDL_JoystickGetAxis(SDL_Joystick * joystick, int axis)
  232. {
  233. Sint16 state;
  234. if (!SDL_PrivateJoystickValid(joystick)) {
  235. return (0);
  236. }
  237. if (axis < joystick->naxes) {
  238. state = joystick->axes[axis];
  239. } else {
  240. SDL_SetError("Joystick only has %d axes", joystick->naxes);
  241. state = 0;
  242. }
  243. return (state);
  244. }
  245. /*
  246. * Get the current state of a hat on a joystick
  247. */
  248. Uint8
  249. SDL_JoystickGetHat(SDL_Joystick * joystick, int hat)
  250. {
  251. Uint8 state;
  252. if (!SDL_PrivateJoystickValid(joystick)) {
  253. return (0);
  254. }
  255. if (hat < joystick->nhats) {
  256. state = joystick->hats[hat];
  257. } else {
  258. SDL_SetError("Joystick only has %d hats", joystick->nhats);
  259. state = 0;
  260. }
  261. return (state);
  262. }
  263. /*
  264. * Get the ball axis change since the last poll
  265. */
  266. int
  267. SDL_JoystickGetBall(SDL_Joystick * joystick, int ball, int *dx, int *dy)
  268. {
  269. int retval;
  270. if (!SDL_PrivateJoystickValid(joystick)) {
  271. return (-1);
  272. }
  273. retval = 0;
  274. if (ball < joystick->nballs) {
  275. if (dx) {
  276. *dx = joystick->balls[ball].dx;
  277. }
  278. if (dy) {
  279. *dy = joystick->balls[ball].dy;
  280. }
  281. joystick->balls[ball].dx = 0;
  282. joystick->balls[ball].dy = 0;
  283. } else {
  284. return SDL_SetError("Joystick only has %d balls", joystick->nballs);
  285. }
  286. return (retval);
  287. }
  288. /*
  289. * Get the current state of a button on a joystick
  290. */
  291. Uint8
  292. SDL_JoystickGetButton(SDL_Joystick * joystick, int button)
  293. {
  294. Uint8 state;
  295. if (!SDL_PrivateJoystickValid(joystick)) {
  296. return (0);
  297. }
  298. if (button < joystick->nbuttons) {
  299. state = joystick->buttons[button];
  300. } else {
  301. SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
  302. state = 0;
  303. }
  304. return (state);
  305. }
  306. /*
  307. * Return if the joystick in question is currently attached to the system,
  308. * \return SDL_FALSE if not plugged in, SDL_TRUE if still present.
  309. */
  310. SDL_bool
  311. SDL_JoystickGetAttached(SDL_Joystick * joystick)
  312. {
  313. if (!SDL_PrivateJoystickValid(joystick)) {
  314. return SDL_FALSE;
  315. }
  316. return SDL_SYS_JoystickAttached(joystick);
  317. }
  318. /*
  319. * Get the instance id for this opened joystick
  320. */
  321. SDL_JoystickID
  322. SDL_JoystickInstanceID(SDL_Joystick * joystick)
  323. {
  324. if (!SDL_PrivateJoystickValid(joystick)) {
  325. return (-1);
  326. }
  327. return (joystick->instance_id);
  328. }
  329. /*
  330. * Get the friendly name of this joystick
  331. */
  332. const char *
  333. SDL_JoystickName(SDL_Joystick * joystick)
  334. {
  335. if (!SDL_PrivateJoystickValid(joystick)) {
  336. return (NULL);
  337. }
  338. return (joystick->name);
  339. }
  340. /*
  341. * Close a joystick previously opened with SDL_JoystickOpen()
  342. */
  343. void
  344. SDL_JoystickClose(SDL_Joystick * joystick)
  345. {
  346. SDL_Joystick *joysticklist;
  347. SDL_Joystick *joysticklistprev;
  348. if (!joystick) {
  349. return;
  350. }
  351. /* First decrement ref count */
  352. if (--joystick->ref_count > 0) {
  353. return;
  354. }
  355. if (joystick == SDL_updating_joystick) {
  356. return;
  357. }
  358. SDL_SYS_JoystickClose(joystick);
  359. joystick->hwdata = NULL;
  360. joysticklist = SDL_joysticks;
  361. joysticklistprev = NULL;
  362. while (joysticklist) {
  363. if (joystick == joysticklist) {
  364. if (joysticklistprev) {
  365. /* unlink this entry */
  366. joysticklistprev->next = joysticklist->next;
  367. } else {
  368. SDL_joysticks = joystick->next;
  369. }
  370. break;
  371. }
  372. joysticklistprev = joysticklist;
  373. joysticklist = joysticklist->next;
  374. }
  375. SDL_free(joystick->name);
  376. /* Free the data associated with this joystick */
  377. SDL_free(joystick->axes);
  378. SDL_free(joystick->hats);
  379. SDL_free(joystick->balls);
  380. SDL_free(joystick->buttons);
  381. SDL_free(joystick);
  382. }
  383. void
  384. SDL_JoystickQuit(void)
  385. {
  386. /* Make sure we're not getting called in the middle of updating joysticks */
  387. SDL_assert(!SDL_updating_joystick);
  388. /* Stop the event polling */
  389. while (SDL_joysticks) {
  390. SDL_joysticks->ref_count = 1;
  391. SDL_JoystickClose(SDL_joysticks);
  392. }
  393. /* Quit the joystick setup */
  394. SDL_SYS_JoystickQuit();
  395. #if !SDL_EVENTS_DISABLED
  396. SDL_QuitSubSystem(SDL_INIT_EVENTS);
  397. #endif
  398. }
  399. static SDL_bool
  400. SDL_PrivateJoystickShouldIgnoreEvent()
  401. {
  402. if (SDL_joystick_allows_background_events) {
  403. return SDL_FALSE;
  404. }
  405. if (SDL_WasInit(SDL_INIT_VIDEO)) {
  406. if (SDL_GetKeyboardFocus() == NULL) {
  407. /* Video is initialized and we don't have focus, ignore the event. */
  408. return SDL_TRUE;
  409. } else {
  410. return SDL_FALSE;
  411. }
  412. }
  413. /* Video subsystem wasn't initialized, always allow the event */
  414. return SDL_FALSE;
  415. }
  416. /* These are global for SDL_sysjoystick.c and SDL_events.c */
  417. int
  418. SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
  419. {
  420. int posted;
  421. /* Make sure we're not getting garbage or duplicate events */
  422. if (axis >= joystick->naxes) {
  423. return 0;
  424. }
  425. if (value == joystick->axes[axis]) {
  426. return 0;
  427. }
  428. /* We ignore events if we don't have keyboard focus, except for centering
  429. * events.
  430. */
  431. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  432. if ((value > 0 && value >= joystick->axes[axis]) ||
  433. (value < 0 && value <= joystick->axes[axis])) {
  434. return 0;
  435. }
  436. }
  437. /* Update internal joystick state */
  438. joystick->axes[axis] = value;
  439. /* Post the event, if desired */
  440. posted = 0;
  441. #if !SDL_EVENTS_DISABLED
  442. if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) {
  443. SDL_Event event;
  444. event.type = SDL_JOYAXISMOTION;
  445. event.jaxis.which = joystick->instance_id;
  446. event.jaxis.axis = axis;
  447. event.jaxis.value = value;
  448. posted = SDL_PushEvent(&event) == 1;
  449. }
  450. #endif /* !SDL_EVENTS_DISABLED */
  451. return (posted);
  452. }
  453. int
  454. SDL_PrivateJoystickHat(SDL_Joystick * joystick, Uint8 hat, Uint8 value)
  455. {
  456. int posted;
  457. /* Make sure we're not getting garbage or duplicate events */
  458. if (hat >= joystick->nhats) {
  459. return 0;
  460. }
  461. if (value == joystick->hats[hat]) {
  462. return 0;
  463. }
  464. /* We ignore events if we don't have keyboard focus, except for centering
  465. * events.
  466. */
  467. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  468. if (value != SDL_HAT_CENTERED) {
  469. return 0;
  470. }
  471. }
  472. /* Update internal joystick state */
  473. joystick->hats[hat] = value;
  474. /* Post the event, if desired */
  475. posted = 0;
  476. #if !SDL_EVENTS_DISABLED
  477. if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) {
  478. SDL_Event event;
  479. event.jhat.type = SDL_JOYHATMOTION;
  480. event.jhat.which = joystick->instance_id;
  481. event.jhat.hat = hat;
  482. event.jhat.value = value;
  483. posted = SDL_PushEvent(&event) == 1;
  484. }
  485. #endif /* !SDL_EVENTS_DISABLED */
  486. return (posted);
  487. }
  488. int
  489. SDL_PrivateJoystickBall(SDL_Joystick * joystick, Uint8 ball,
  490. Sint16 xrel, Sint16 yrel)
  491. {
  492. int posted;
  493. /* Make sure we're not getting garbage events */
  494. if (ball >= joystick->nballs) {
  495. return 0;
  496. }
  497. /* We ignore events if we don't have keyboard focus. */
  498. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  499. return 0;
  500. }
  501. /* Update internal mouse state */
  502. joystick->balls[ball].dx += xrel;
  503. joystick->balls[ball].dy += yrel;
  504. /* Post the event, if desired */
  505. posted = 0;
  506. #if !SDL_EVENTS_DISABLED
  507. if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) {
  508. SDL_Event event;
  509. event.jball.type = SDL_JOYBALLMOTION;
  510. event.jball.which = joystick->instance_id;
  511. event.jball.ball = ball;
  512. event.jball.xrel = xrel;
  513. event.jball.yrel = yrel;
  514. posted = SDL_PushEvent(&event) == 1;
  515. }
  516. #endif /* !SDL_EVENTS_DISABLED */
  517. return (posted);
  518. }
  519. int
  520. SDL_PrivateJoystickButton(SDL_Joystick * joystick, Uint8 button, Uint8 state)
  521. {
  522. int posted;
  523. #if !SDL_EVENTS_DISABLED
  524. SDL_Event event;
  525. switch (state) {
  526. case SDL_PRESSED:
  527. event.type = SDL_JOYBUTTONDOWN;
  528. break;
  529. case SDL_RELEASED:
  530. event.type = SDL_JOYBUTTONUP;
  531. break;
  532. default:
  533. /* Invalid state -- bail */
  534. return (0);
  535. }
  536. #endif /* !SDL_EVENTS_DISABLED */
  537. /* Make sure we're not getting garbage or duplicate events */
  538. if (button >= joystick->nbuttons) {
  539. return 0;
  540. }
  541. if (state == joystick->buttons[button]) {
  542. return 0;
  543. }
  544. /* We ignore events if we don't have keyboard focus, except for button
  545. * release. */
  546. if (SDL_PrivateJoystickShouldIgnoreEvent()) {
  547. if (state == SDL_PRESSED) {
  548. return 0;
  549. }
  550. }
  551. /* Update internal joystick state */
  552. joystick->buttons[button] = state;
  553. /* Post the event, if desired */
  554. posted = 0;
  555. #if !SDL_EVENTS_DISABLED
  556. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  557. event.jbutton.which = joystick->instance_id;
  558. event.jbutton.button = button;
  559. event.jbutton.state = state;
  560. posted = SDL_PushEvent(&event) == 1;
  561. }
  562. #endif /* !SDL_EVENTS_DISABLED */
  563. return (posted);
  564. }
  565. void
  566. SDL_JoystickUpdate(void)
  567. {
  568. SDL_Joystick *joystick;
  569. joystick = SDL_joysticks;
  570. while (joystick) {
  571. SDL_Joystick *joysticknext;
  572. /* save off the next pointer, the Update call may cause a joystick removed event
  573. * and cause our joystick pointer to be freed
  574. */
  575. joysticknext = joystick->next;
  576. SDL_updating_joystick = joystick;
  577. SDL_SYS_JoystickUpdate(joystick);
  578. if (joystick->force_recentering) {
  579. int i;
  580. /* Tell the app that everything is centered/unpressed... */
  581. for (i = 0; i < joystick->naxes; i++)
  582. SDL_PrivateJoystickAxis(joystick, i, 0);
  583. for (i = 0; i < joystick->nbuttons; i++)
  584. SDL_PrivateJoystickButton(joystick, i, 0);
  585. for (i = 0; i < joystick->nhats; i++)
  586. SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
  587. joystick->force_recentering = SDL_FALSE;
  588. }
  589. SDL_updating_joystick = NULL;
  590. /* If the joystick was closed while updating, free it here */
  591. if (joystick->ref_count <= 0) {
  592. SDL_JoystickClose(joystick);
  593. }
  594. joystick = joysticknext;
  595. }
  596. /* this needs to happen AFTER walking the joystick list above, so that any
  597. dangling hardware data from removed devices can be free'd
  598. */
  599. SDL_SYS_JoystickDetect();
  600. }
  601. int
  602. SDL_JoystickEventState(int state)
  603. {
  604. #if SDL_EVENTS_DISABLED
  605. return SDL_DISABLE;
  606. #else
  607. const Uint32 event_list[] = {
  608. SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYHATMOTION,
  609. SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED
  610. };
  611. unsigned int i;
  612. switch (state) {
  613. case SDL_QUERY:
  614. state = SDL_DISABLE;
  615. for (i = 0; i < SDL_arraysize(event_list); ++i) {
  616. state = SDL_EventState(event_list[i], SDL_QUERY);
  617. if (state == SDL_ENABLE) {
  618. break;
  619. }
  620. }
  621. break;
  622. default:
  623. for (i = 0; i < SDL_arraysize(event_list); ++i) {
  624. SDL_EventState(event_list[i], state);
  625. }
  626. break;
  627. }
  628. return (state);
  629. #endif /* SDL_EVENTS_DISABLED */
  630. }
  631. /* return the guid for this index */
  632. SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
  633. {
  634. if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
  635. SDL_JoystickGUID emptyGUID;
  636. SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
  637. SDL_zero(emptyGUID);
  638. return emptyGUID;
  639. }
  640. return SDL_SYS_JoystickGetDeviceGUID(device_index);
  641. }
  642. /* return the guid for this opened device */
  643. SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick * joystick)
  644. {
  645. if (!SDL_PrivateJoystickValid(joystick)) {
  646. SDL_JoystickGUID emptyGUID;
  647. SDL_zero(emptyGUID);
  648. return emptyGUID;
  649. }
  650. return SDL_SYS_JoystickGetGUID(joystick);
  651. }
  652. /* convert the guid to a printable string */
  653. void SDL_JoystickGetGUIDString(SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
  654. {
  655. static const char k_rgchHexToASCII[] = "0123456789abcdef";
  656. int i;
  657. if ((pszGUID == NULL) || (cbGUID <= 0)) {
  658. return;
  659. }
  660. for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
  661. /* each input byte writes 2 ascii chars, and might write a null byte. */
  662. /* If we don't have room for next input byte, stop */
  663. unsigned char c = guid.data[i];
  664. *pszGUID++ = k_rgchHexToASCII[c >> 4];
  665. *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
  666. }
  667. *pszGUID = '\0';
  668. }
  669. /*-----------------------------------------------------------------------------
  670. * Purpose: Returns the 4 bit nibble for a hex character
  671. * Input : c -
  672. * Output : unsigned char
  673. *-----------------------------------------------------------------------------*/
  674. static unsigned char nibble(char c)
  675. {
  676. if ((c >= '0') && (c <= '9')) {
  677. return (unsigned char)(c - '0');
  678. }
  679. if ((c >= 'A') && (c <= 'F')) {
  680. return (unsigned char)(c - 'A' + 0x0a);
  681. }
  682. if ((c >= 'a') && (c <= 'f')) {
  683. return (unsigned char)(c - 'a' + 0x0a);
  684. }
  685. /* received an invalid character, and no real way to return an error */
  686. /* AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c); */
  687. return 0;
  688. }
  689. /* convert the string version of a joystick guid to the struct */
  690. SDL_JoystickGUID SDL_JoystickGetGUIDFromString(const char *pchGUID)
  691. {
  692. SDL_JoystickGUID guid;
  693. int maxoutputbytes= sizeof(guid);
  694. size_t len = SDL_strlen(pchGUID);
  695. Uint8 *p;
  696. size_t i;
  697. /* Make sure it's even */
  698. len = (len) & ~0x1;
  699. SDL_memset(&guid, 0x00, sizeof(guid));
  700. p = (Uint8 *)&guid;
  701. for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
  702. *p = (nibble(pchGUID[i]) << 4) | nibble(pchGUID[i+1]);
  703. }
  704. return guid;
  705. }
  706. /* vi: set ts=4 sw=4 expandtab: */