SDL_joystick.c 21 KB

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