SDL_bsdjoystick.c 24 KB

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