SDL_bsdjoystick.c 24 KB

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