SDL_bsdjoystick.c 22 KB

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