SDL_sysjoystick.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 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_USBHID
  20. /*
  21. * Joystick driver for the uhid(4) interface found in OpenBSD,
  22. * NetBSD and FreeBSD.
  23. *
  24. * Maintainer: <vedge at csoft.org>
  25. */
  26. #include <sys/param.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <errno.h>
  30. #ifndef __FreeBSD_kernel_version
  31. #define __FreeBSD_kernel_version __FreeBSD_version
  32. #endif
  33. #if defined(HAVE_USB_H)
  34. #include <usb.h>
  35. #endif
  36. #ifdef __DragonFly__
  37. #include <bus/usb/usb.h>
  38. #include <bus/usb/usbhid.h>
  39. #else
  40. #include <dev/usb/usb.h>
  41. #include <dev/usb/usbhid.h>
  42. #endif
  43. #if defined(HAVE_USBHID_H)
  44. #include <usbhid.h>
  45. #elif defined(HAVE_LIBUSB_H)
  46. #include <libusb.h>
  47. #elif defined(HAVE_LIBUSBHID_H)
  48. #include <libusbhid.h>
  49. #endif
  50. #if defined(__FREEBSD__) || defined(__FreeBSD_kernel__)
  51. #ifndef __DragonFly__
  52. #include <osreldate.h>
  53. #endif
  54. #if __FreeBSD_kernel_version > 800063
  55. #include <dev/usb/usb_ioctl.h>
  56. #endif
  57. #include <sys/joystick.h>
  58. #endif
  59. #if SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H
  60. #include <machine/joystick.h>
  61. #endif
  62. #include "SDL_joystick.h"
  63. #include "../SDL_sysjoystick.h"
  64. #include "../SDL_joystick_c.h"
  65. #define MAX_UHID_JOYS 64
  66. #define MAX_JOY_JOYS 2
  67. #define MAX_JOYS (MAX_UHID_JOYS + MAX_JOY_JOYS)
  68. #ifdef __OpenBSD__
  69. #define HUG_DPAD_UP 0x90
  70. #define HUG_DPAD_DOWN 0x91
  71. #define HUG_DPAD_RIGHT 0x92
  72. #define HUG_DPAD_LEFT 0x93
  73. #define HAT_CENTERED 0x00
  74. #define HAT_UP 0x01
  75. #define HAT_RIGHT 0x02
  76. #define HAT_DOWN 0x04
  77. #define HAT_LEFT 0x08
  78. #define HAT_RIGHTUP (HAT_RIGHT|HAT_UP)
  79. #define HAT_RIGHTDOWN (HAT_RIGHT|HAT_DOWN)
  80. #define HAT_LEFTUP (HAT_LEFT|HAT_UP)
  81. #define HAT_LEFTDOWN (HAT_LEFT|HAT_DOWN)
  82. /* calculate the value from the state of the dpad */
  83. int
  84. dpad_to_sdl(Sint32 *dpad)
  85. {
  86. if (dpad[2]) {
  87. if (dpad[0])
  88. return HAT_RIGHTUP;
  89. else if (dpad[1])
  90. return HAT_RIGHTDOWN;
  91. else
  92. return HAT_RIGHT;
  93. } else if (dpad[3]) {
  94. if (dpad[0])
  95. return HAT_LEFTUP;
  96. else if (dpad[1])
  97. return HAT_LEFTDOWN;
  98. else
  99. return HAT_LEFT;
  100. } else if (dpad[0]) {
  101. return HAT_UP;
  102. } else if (dpad[1]) {
  103. return HAT_DOWN;
  104. }
  105. return HAT_CENTERED;
  106. }
  107. #endif
  108. struct report
  109. {
  110. #if defined(__FREEBSD__) && (__FreeBSD_kernel_version > 900000)
  111. void *buf; /* Buffer */
  112. #elif defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063)
  113. struct usb_gen_descriptor *buf; /* Buffer */
  114. #else
  115. struct usb_ctl_report *buf; /* Buffer */
  116. #endif
  117. size_t size; /* Buffer size */
  118. int rid; /* Report ID */
  119. enum
  120. {
  121. SREPORT_UNINIT,
  122. SREPORT_CLEAN,
  123. SREPORT_DIRTY
  124. } status;
  125. };
  126. static struct
  127. {
  128. int uhid_report;
  129. hid_kind_t kind;
  130. const char *name;
  131. } const repinfo[] = {
  132. {UHID_INPUT_REPORT, hid_input, "input"},
  133. {UHID_OUTPUT_REPORT, hid_output, "output"},
  134. {UHID_FEATURE_REPORT, hid_feature, "feature"}
  135. };
  136. enum
  137. {
  138. REPORT_INPUT = 0,
  139. REPORT_OUTPUT = 1,
  140. REPORT_FEATURE = 2
  141. };
  142. enum
  143. {
  144. JOYAXE_X,
  145. JOYAXE_Y,
  146. JOYAXE_Z,
  147. JOYAXE_SLIDER,
  148. JOYAXE_WHEEL,
  149. JOYAXE_RX,
  150. JOYAXE_RY,
  151. JOYAXE_RZ,
  152. JOYAXE_count
  153. };
  154. struct joystick_hwdata
  155. {
  156. int fd;
  157. char *path;
  158. enum
  159. {
  160. BSDJOY_UHID, /* uhid(4) */
  161. BSDJOY_JOY /* joy(4) */
  162. } type;
  163. struct report_desc *repdesc;
  164. struct report inreport;
  165. int axis_map[JOYAXE_count]; /* map present JOYAXE_* to 0,1,.. */
  166. };
  167. static char *joynames[MAX_JOYS];
  168. static char *joydevnames[MAX_JOYS];
  169. static int report_alloc(struct report *, struct report_desc *, int);
  170. static void report_free(struct report *);
  171. #if defined(USBHID_UCR_DATA) || (defined(__FreeBSD_kernel__) && __FreeBSD_kernel_version <= 800063)
  172. #define REP_BUF_DATA(rep) ((rep)->buf->ucr_data)
  173. #elif (defined(__FREEBSD__) && (__FreeBSD_kernel_version > 900000))
  174. #define REP_BUF_DATA(rep) ((rep)->buf)
  175. #elif (defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063))
  176. #define REP_BUF_DATA(rep) ((rep)->buf->ugd_data)
  177. #else
  178. #define REP_BUF_DATA(rep) ((rep)->buf->data)
  179. #endif
  180. static int numjoysticks = 0;
  181. static int BSD_JoystickOpen(SDL_Joystick * joy, int device_index);
  182. static void BSD_JoystickClose(SDL_Joystick * joy);
  183. static int
  184. BSD_JoystickInit(void)
  185. {
  186. char s[16];
  187. int i, fd;
  188. numjoysticks = 0;
  189. SDL_memset(joynames, 0, sizeof(joynames));
  190. SDL_memset(joydevnames, 0, sizeof(joydevnames));
  191. for (i = 0; i < MAX_UHID_JOYS; i++) {
  192. SDL_Joystick nj;
  193. SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i);
  194. joynames[numjoysticks] = SDL_strdup(s);
  195. if (BSD_JoystickOpen(&nj, numjoysticks) == 0) {
  196. BSD_JoystickClose(&nj);
  197. numjoysticks++;
  198. } else {
  199. SDL_free(joynames[numjoysticks]);
  200. joynames[numjoysticks] = NULL;
  201. }
  202. }
  203. for (i = 0; i < MAX_JOY_JOYS; i++) {
  204. SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i);
  205. fd = open(s, O_RDONLY);
  206. if (fd != -1) {
  207. joynames[numjoysticks++] = SDL_strdup(s);
  208. close(fd);
  209. }
  210. }
  211. /* Read the default USB HID usage table. */
  212. hid_init(NULL);
  213. return (numjoysticks);
  214. }
  215. static int
  216. BSD_JoystickGetCount(void)
  217. {
  218. return numjoysticks;
  219. }
  220. static void
  221. BSD_JoystickDetect(void)
  222. {
  223. }
  224. static const char *
  225. BSD_JoystickGetDeviceName(int device_index)
  226. {
  227. if (joydevnames[device_index] != NULL) {
  228. return (joydevnames[device_index]);
  229. }
  230. return (joynames[device_index]);
  231. }
  232. static int
  233. BSD_JoystickGetDevicePlayerIndex(int device_index)
  234. {
  235. return -1;
  236. }
  237. static void
  238. BSD_JoystickSetDevicePlayerIndex(int device_index, int player_index)
  239. {
  240. }
  241. /* Function to perform the mapping from device index to the instance id for this index */
  242. static SDL_JoystickID
  243. BSD_JoystickGetDeviceInstanceID(int device_index)
  244. {
  245. return device_index;
  246. }
  247. static int
  248. usage_to_joyaxe(unsigned usage)
  249. {
  250. int joyaxe;
  251. switch (usage) {
  252. case HUG_X:
  253. joyaxe = JOYAXE_X;
  254. break;
  255. case HUG_Y:
  256. joyaxe = JOYAXE_Y;
  257. break;
  258. case HUG_Z:
  259. joyaxe = JOYAXE_Z;
  260. break;
  261. case HUG_SLIDER:
  262. joyaxe = JOYAXE_SLIDER;
  263. break;
  264. case HUG_WHEEL:
  265. joyaxe = JOYAXE_WHEEL;
  266. break;
  267. case HUG_RX:
  268. joyaxe = JOYAXE_RX;
  269. break;
  270. case HUG_RY:
  271. joyaxe = JOYAXE_RY;
  272. break;
  273. case HUG_RZ:
  274. joyaxe = JOYAXE_RZ;
  275. break;
  276. default:
  277. joyaxe = -1;
  278. }
  279. return joyaxe;
  280. }
  281. static unsigned
  282. hatval_to_sdl(Sint32 hatval)
  283. {
  284. static const unsigned hat_dir_map[8] = {
  285. SDL_HAT_UP, SDL_HAT_RIGHTUP, SDL_HAT_RIGHT, SDL_HAT_RIGHTDOWN,
  286. SDL_HAT_DOWN, SDL_HAT_LEFTDOWN, SDL_HAT_LEFT, SDL_HAT_LEFTUP
  287. };
  288. unsigned result;
  289. if ((hatval & 7) == hatval)
  290. result = hat_dir_map[hatval];
  291. else
  292. result = SDL_HAT_CENTERED;
  293. return result;
  294. }
  295. static int
  296. BSD_JoystickOpen(SDL_Joystick * joy, int device_index)
  297. {
  298. char *path = joynames[device_index];
  299. struct joystick_hwdata *hw;
  300. struct hid_item hitem;
  301. struct hid_data *hdata;
  302. struct report *rep = NULL;
  303. #if defined(__NetBSD__)
  304. usb_device_descriptor_t udd;
  305. struct usb_string_desc usd;
  306. #endif
  307. int fd;
  308. int i;
  309. fd = open(path, O_RDONLY);
  310. if (fd == -1) {
  311. return SDL_SetError("%s: %s", path, strerror(errno));
  312. }
  313. joy->instance_id = device_index;
  314. hw = (struct joystick_hwdata *)
  315. SDL_malloc(sizeof(struct joystick_hwdata));
  316. if (hw == NULL) {
  317. close(fd);
  318. return SDL_OutOfMemory();
  319. }
  320. joy->hwdata = hw;
  321. hw->fd = fd;
  322. hw->path = SDL_strdup(path);
  323. if (!SDL_strncmp(path, "/dev/joy", 8)) {
  324. hw->type = BSDJOY_JOY;
  325. joy->naxes = 2;
  326. joy->nbuttons = 2;
  327. joy->nhats = 0;
  328. joy->nballs = 0;
  329. joydevnames[device_index] = SDL_strdup("Gameport joystick");
  330. goto usbend;
  331. } else {
  332. hw->type = BSDJOY_UHID;
  333. }
  334. {
  335. int ax;
  336. for (ax = 0; ax < JOYAXE_count; ax++)
  337. hw->axis_map[ax] = -1;
  338. }
  339. hw->repdesc = hid_get_report_desc(fd);
  340. if (hw->repdesc == NULL) {
  341. SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
  342. strerror(errno));
  343. goto usberr;
  344. }
  345. rep = &hw->inreport;
  346. #if defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063) || defined(__FreeBSD_kernel__)
  347. rep->rid = hid_get_report_id(fd);
  348. if (rep->rid < 0) {
  349. #else
  350. if (ioctl(fd, USB_GET_REPORT_ID, &rep->rid) < 0) {
  351. #endif
  352. rep->rid = -1; /* XXX */
  353. }
  354. #if defined(__NetBSD__)
  355. if (ioctl(fd, USB_GET_DEVICE_DESC, &udd) == -1)
  356. goto desc_failed;
  357. /* Get default language */
  358. usd.usd_string_index = USB_LANGUAGE_TABLE;
  359. usd.usd_language_id = 0;
  360. if (ioctl(fd, USB_GET_STRING_DESC, &usd) == -1 || usd.usd_desc.bLength < 4) {
  361. usd.usd_language_id = 0;
  362. } else {
  363. usd.usd_language_id = UGETW(usd.usd_desc.bString[0]);
  364. }
  365. usd.usd_string_index = udd.iProduct;
  366. if (ioctl(fd, USB_GET_STRING_DESC, &usd) == 0) {
  367. char str[128];
  368. char *new_name = NULL;
  369. int i;
  370. for (i = 0; i < (usd.usd_desc.bLength >> 1) - 1 && i < sizeof(str) - 1; i++) {
  371. str[i] = UGETW(usd.usd_desc.bString[i]);
  372. }
  373. str[i] = '\0';
  374. asprintf(&new_name, "%s @ %s", str, path);
  375. if (new_name != NULL) {
  376. SDL_free(joydevnames[numjoysticks]);
  377. joydevnames[numjoysticks] = new_name;
  378. }
  379. }
  380. desc_failed:
  381. #endif
  382. if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
  383. goto usberr;
  384. }
  385. if (rep->size <= 0) {
  386. SDL_SetError("%s: Input report descriptor has invalid length",
  387. hw->path);
  388. goto usberr;
  389. }
  390. #if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111) || defined(__FreeBSD_kernel__)
  391. hdata = hid_start_parse(hw->repdesc, 1 << hid_input, rep->rid);
  392. #else
  393. hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
  394. #endif
  395. if (hdata == NULL) {
  396. SDL_SetError("%s: Cannot start HID parser", hw->path);
  397. goto usberr;
  398. }
  399. joy->naxes = 0;
  400. joy->nbuttons = 0;
  401. joy->nhats = 0;
  402. joy->nballs = 0;
  403. for (i = 0; i < JOYAXE_count; i++)
  404. hw->axis_map[i] = -1;
  405. while (hid_get_item(hdata, &hitem) > 0) {
  406. char *sp;
  407. const char *s;
  408. switch (hitem.kind) {
  409. case hid_collection:
  410. switch (HID_PAGE(hitem.usage)) {
  411. case HUP_GENERIC_DESKTOP:
  412. switch (HID_USAGE(hitem.usage)) {
  413. case HUG_JOYSTICK:
  414. case HUG_GAME_PAD:
  415. s = hid_usage_in_page(hitem.usage);
  416. sp = SDL_malloc(SDL_strlen(s) + 5);
  417. SDL_snprintf(sp, SDL_strlen(s) + 5, "%s (%d)",
  418. s, device_index);
  419. joydevnames[device_index] = sp;
  420. }
  421. }
  422. break;
  423. case hid_input:
  424. switch (HID_PAGE(hitem.usage)) {
  425. case HUP_GENERIC_DESKTOP:
  426. {
  427. unsigned usage = HID_USAGE(hitem.usage);
  428. int joyaxe = usage_to_joyaxe(usage);
  429. if (joyaxe >= 0) {
  430. hw->axis_map[joyaxe] = 1;
  431. } else if (usage == HUG_HAT_SWITCH
  432. #ifdef __OpenBSD__
  433. || usage == HUG_DPAD_UP
  434. #endif
  435. ) {
  436. joy->nhats++;
  437. }
  438. break;
  439. }
  440. case HUP_BUTTON:
  441. joy->nbuttons++;
  442. break;
  443. default:
  444. break;
  445. }
  446. break;
  447. default:
  448. break;
  449. }
  450. }
  451. hid_end_parse(hdata);
  452. for (i = 0; i < JOYAXE_count; i++)
  453. if (hw->axis_map[i] > 0)
  454. hw->axis_map[i] = joy->naxes++;
  455. if (joy->naxes == 0 && joy->nbuttons == 0 && joy->nhats == 0 && joy->nballs == 0) {
  456. SDL_SetError("%s: Not a joystick, ignoring", hw->path);
  457. goto usberr;
  458. }
  459. usbend:
  460. /* The poll blocks the event thread. */
  461. fcntl(fd, F_SETFL, O_NONBLOCK);
  462. #ifdef __NetBSD__
  463. /* Flush pending events */
  464. if (rep) {
  465. while (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) == rep->size)
  466. ;
  467. }
  468. #endif
  469. return (0);
  470. usberr:
  471. close(hw->fd);
  472. SDL_free(hw->path);
  473. SDL_free(hw);
  474. return (-1);
  475. }
  476. static void
  477. BSD_JoystickUpdate(SDL_Joystick * joy)
  478. {
  479. struct hid_item hitem;
  480. struct hid_data *hdata;
  481. struct report *rep;
  482. int nbutton, naxe = -1;
  483. Sint32 v;
  484. #ifdef __OpenBSD__
  485. Sint32 dpad[4] = {0, 0, 0, 0};
  486. #endif
  487. #if defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H || defined(__FreeBSD_kernel__)
  488. struct joystick gameport;
  489. static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0;
  490. if (joy->hwdata->type == BSDJOY_JOY) {
  491. while (read(joy->hwdata->fd, &gameport, sizeof gameport) == sizeof gameport) {
  492. if (abs(x - gameport.x) > 8) {
  493. x = gameport.x;
  494. if (x < xmin) {
  495. xmin = x;
  496. }
  497. if (x > xmax) {
  498. xmax = x;
  499. }
  500. if (xmin == xmax) {
  501. xmin--;
  502. xmax++;
  503. }
  504. v = (Sint32) x;
  505. v -= (xmax + xmin + 1) / 2;
  506. v *= 32768 / ((xmax - xmin + 1) / 2);
  507. SDL_PrivateJoystickAxis(joy, 0, v);
  508. }
  509. if (abs(y - gameport.y) > 8) {
  510. y = gameport.y;
  511. if (y < ymin) {
  512. ymin = y;
  513. }
  514. if (y > ymax) {
  515. ymax = y;
  516. }
  517. if (ymin == ymax) {
  518. ymin--;
  519. ymax++;
  520. }
  521. v = (Sint32) y;
  522. v -= (ymax + ymin + 1) / 2;
  523. v *= 32768 / ((ymax - ymin + 1) / 2);
  524. SDL_PrivateJoystickAxis(joy, 1, v);
  525. }
  526. SDL_PrivateJoystickButton(joy, 0, gameport.b1);
  527. SDL_PrivateJoystickButton(joy, 1, gameport.b2);
  528. }
  529. return;
  530. }
  531. #endif /* defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H */
  532. rep = &joy->hwdata->inreport;
  533. while (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) == rep->size) {
  534. #if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111) || defined(__FreeBSD_kernel__)
  535. hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
  536. #else
  537. hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
  538. #endif
  539. if (hdata == NULL) {
  540. /*fprintf(stderr, "%s: Cannot start HID parser\n", joy->hwdata->path);*/
  541. continue;
  542. }
  543. for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
  544. switch (hitem.kind) {
  545. case hid_input:
  546. switch (HID_PAGE(hitem.usage)) {
  547. case HUP_GENERIC_DESKTOP:
  548. {
  549. unsigned usage = HID_USAGE(hitem.usage);
  550. int joyaxe = usage_to_joyaxe(usage);
  551. if (joyaxe >= 0) {
  552. naxe = joy->hwdata->axis_map[joyaxe];
  553. /* scaleaxe */
  554. v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
  555. v -= (hitem.logical_maximum +
  556. hitem.logical_minimum + 1) / 2;
  557. v *= 32768 /
  558. ((hitem.logical_maximum -
  559. hitem.logical_minimum + 1) / 2);
  560. SDL_PrivateJoystickAxis(joy, naxe, v);
  561. } else if (usage == HUG_HAT_SWITCH) {
  562. v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
  563. SDL_PrivateJoystickHat(joy, 0,
  564. hatval_to_sdl(v) -
  565. hitem.logical_minimum);
  566. }
  567. #ifdef __OpenBSD__
  568. else if (usage == HUG_DPAD_UP) {
  569. dpad[0] = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
  570. SDL_PrivateJoystickHat(joy, 0, dpad_to_sdl(dpad));
  571. }
  572. else if (usage == HUG_DPAD_DOWN) {
  573. dpad[1] = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
  574. SDL_PrivateJoystickHat(joy, 0, dpad_to_sdl(dpad));
  575. }
  576. else if (usage == HUG_DPAD_RIGHT) {
  577. dpad[2] = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
  578. SDL_PrivateJoystickHat(joy, 0, dpad_to_sdl(dpad));
  579. }
  580. else if (usage == HUG_DPAD_LEFT) {
  581. dpad[3] = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
  582. SDL_PrivateJoystickHat(joy, 0, dpad_to_sdl(dpad));
  583. }
  584. #endif
  585. break;
  586. }
  587. case HUP_BUTTON:
  588. v = (Sint32) hid_get_data(REP_BUF_DATA(rep), &hitem);
  589. SDL_PrivateJoystickButton(joy, nbutton, v);
  590. nbutton++;
  591. break;
  592. default:
  593. continue;
  594. }
  595. break;
  596. default:
  597. break;
  598. }
  599. }
  600. hid_end_parse(hdata);
  601. }
  602. }
  603. /* Function to close a joystick after use */
  604. static void
  605. BSD_JoystickClose(SDL_Joystick * joy)
  606. {
  607. if (SDL_strncmp(joy->hwdata->path, "/dev/joy", 8)) {
  608. report_free(&joy->hwdata->inreport);
  609. hid_dispose_report_desc(joy->hwdata->repdesc);
  610. }
  611. close(joy->hwdata->fd);
  612. SDL_free(joy->hwdata->path);
  613. SDL_free(joy->hwdata);
  614. }
  615. static void
  616. BSD_JoystickQuit(void)
  617. {
  618. int i;
  619. for (i = 0; i < MAX_JOYS; i++) {
  620. SDL_free(joynames[i]);
  621. SDL_free(joydevnames[i]);
  622. }
  623. return;
  624. }
  625. static SDL_JoystickGUID
  626. BSD_JoystickGetDeviceGUID( int device_index )
  627. {
  628. SDL_JoystickGUID guid;
  629. /* the GUID is just the first 16 chars of the name for now */
  630. const char *name = BSD_JoystickGetDeviceName( device_index );
  631. SDL_zero( guid );
  632. SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
  633. return guid;
  634. }
  635. static int
  636. report_alloc(struct report *r, struct report_desc *rd, int repind)
  637. {
  638. int len;
  639. #ifdef __DragonFly__
  640. len = hid_report_size(rd, r->rid, repinfo[repind].kind);
  641. #elif __FREEBSD__
  642. # if (__FreeBSD_kernel_version >= 460000) || defined(__FreeBSD_kernel__)
  643. # if (__FreeBSD_kernel_version <= 500111)
  644. len = hid_report_size(rd, r->rid, repinfo[repind].kind);
  645. # else
  646. len = hid_report_size(rd, repinfo[repind].kind, r->rid);
  647. # endif
  648. # else
  649. len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
  650. # endif
  651. #else
  652. # ifdef USBHID_NEW
  653. len = hid_report_size(rd, repinfo[repind].kind, r->rid);
  654. # else
  655. len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
  656. # endif
  657. #endif
  658. if (len < 0) {
  659. return SDL_SetError("Negative HID report size");
  660. }
  661. r->size = len;
  662. if (r->size > 0) {
  663. #if defined(__FREEBSD__) && (__FreeBSD_kernel_version > 900000)
  664. r->buf = SDL_malloc(r->size);
  665. #else
  666. r->buf = SDL_malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) +
  667. r->size);
  668. #endif
  669. if (r->buf == NULL) {
  670. return SDL_OutOfMemory();
  671. }
  672. } else {
  673. r->buf = NULL;
  674. }
  675. r->status = SREPORT_CLEAN;
  676. return 0;
  677. }
  678. static void
  679. report_free(struct report *r)
  680. {
  681. SDL_free(r->buf);
  682. r->status = SREPORT_UNINIT;
  683. }
  684. static int
  685. BSD_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  686. {
  687. return SDL_Unsupported();
  688. }
  689. static int
  690. BSD_JoystickRumbleTriggers(SDL_Joystick * joystick, Uint16 left_rumble, Uint16 right_rumble)
  691. {
  692. return SDL_Unsupported();
  693. }
  694. static SDL_bool
  695. BSD_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
  696. {
  697. return SDL_FALSE;
  698. }
  699. static SDL_bool
  700. BSD_JoystickHasLED(SDL_Joystick * joystick)
  701. {
  702. return SDL_FALSE;
  703. }
  704. static int
  705. BSD_JoystickSetLED(SDL_Joystick * joystick, Uint8 red, Uint8 green, Uint8 blue)
  706. {
  707. return SDL_Unsupported();
  708. }
  709. SDL_JoystickDriver SDL_BSD_JoystickDriver =
  710. {
  711. BSD_JoystickInit,
  712. BSD_JoystickGetCount,
  713. BSD_JoystickDetect,
  714. BSD_JoystickGetDeviceName,
  715. BSD_JoystickGetDevicePlayerIndex,
  716. BSD_JoystickSetDevicePlayerIndex,
  717. BSD_JoystickGetDeviceGUID,
  718. BSD_JoystickGetDeviceInstanceID,
  719. BSD_JoystickOpen,
  720. BSD_JoystickRumble,
  721. BSD_JoystickRumbleTriggers,
  722. BSD_JoystickHasLED,
  723. BSD_JoystickSetLED,
  724. BSD_JoystickUpdate,
  725. BSD_JoystickClose,
  726. BSD_JoystickQuit,
  727. BSD_JoystickGetGamepadMapping
  728. };
  729. #endif /* SDL_JOYSTICK_USBHID */
  730. /* vi: set ts=4 sw=4 expandtab: */