19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL - Simple DirectMedia Layer 39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Copyright (C) 1997-2012 Sam Lantinga 49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is free software; you can redistribute it and/or 69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall modify it under the terms of the GNU Lesser General Public 79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License as published by the Free Software Foundation; either 89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall version 2.1 of the License, or (at your option) any later version. 99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is distributed in the hope that it will be useful, 119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall but WITHOUT ANY WARRANTY; without even the implied warranty of 129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Lesser General Public License for more details. 149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall You should have received a copy of the GNU Lesser General Public 169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License along with this library; if not, write to the Free Software 179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sam Lantinga 209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall slouken@libsdl.org 219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h" 239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_JOYSTICK_USBHID 259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Joystick driver for the uhid(4) interface found in OpenBSD, 289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * NetBSD and FreeBSD. 299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * 309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Maintainer: <vedge at csoft.org> 319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */ 329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/param.h> 349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h> 369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <fcntl.h> 379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h> 389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef __FreeBSD_kernel_version 409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define __FreeBSD_kernel_version __FreeBSD_version 419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(HAVE_USB_H) 449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <usb.h> 459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __DragonFly__ 479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <bus/usb/usb.h> 489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <bus/usb/usbhid.h> 499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <dev/usb/usb.h> 519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <dev/usb/usbhid.h> 529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(HAVE_USBHID_H) 559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <usbhid.h> 569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif defined(HAVE_LIBUSB_H) 579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <libusb.h> 589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif defined(HAVE_LIBUSBHID_H) 599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <libusbhid.h> 609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(__FREEBSD__) || defined(__FreeBSD_kernel__) 639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifndef __DragonFly__ 649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <osreldate.h> 659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if __FreeBSD_kernel_version > 800063 679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <dev/usb/usb_ioctl.h> 689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/joystick.h> 709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H 739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <machine/joystick.h> 749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_joystick.h" 779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysjoystick.h" 789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_joystick_c.h" 799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MAX_UHID_JOYS 4 819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MAX_JOY_JOYS 2 829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MAX_JOYS (MAX_UHID_JOYS + MAX_JOY_JOYS) 839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct report { 859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063) 869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct usb_gen_descriptor *buf; /* Buffer */ 879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct usb_ctl_report *buf; /* Buffer */ 899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall size_t size; /* Buffer size */ 919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int rid; /* Report ID */ 929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall enum { 939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SREPORT_UNINIT, 949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SREPORT_CLEAN, 959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SREPORT_DIRTY 969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } status; 979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}; 989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct { 1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int uhid_report; 1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hid_kind_t kind; 1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const char *name; 1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} const repinfo[] = { 1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { UHID_INPUT_REPORT, hid_input, "input" }, 1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { UHID_OUTPUT_REPORT, hid_output, "output" }, 1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { UHID_FEATURE_REPORT, hid_feature, "feature" } 1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}; 1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallenum { 1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall REPORT_INPUT = 0, 1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall REPORT_OUTPUT = 1, 1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall REPORT_FEATURE = 2 1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}; 1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallenum { 1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall JOYAXE_X, 1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall JOYAXE_Y, 1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall JOYAXE_Z, 1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall JOYAXE_SLIDER, 1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall JOYAXE_WHEEL, 1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall JOYAXE_RX, 1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall JOYAXE_RY, 1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall JOYAXE_RZ, 1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall JOYAXE_count 1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}; 1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct joystick_hwdata { 1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int fd; 1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall char *path; 1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall enum { 1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall BSDJOY_UHID, /* uhid(4) */ 1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall BSDJOY_JOY /* joy(4) */ 1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } type; 1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct report_desc *repdesc; 1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct report inreport; 1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int axis_map[JOYAXE_count]; /* map present JOYAXE_* to 0,1,..*/ 1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int x; 1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int y; 1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int xmin; 1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int ymin; 1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int xmax; 1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int ymax; 1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}; 1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char *joynames[MAX_JOYS]; 1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char *joydevnames[MAX_JOYS]; 1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int report_alloc(struct report *, struct report_desc *, int); 1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void report_free(struct report *); 1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(USBHID_UCR_DATA) || defined(__FreeBSD_kernel__) 1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define REP_BUF_DATA(rep) ((rep)->buf->ucr_data) 1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif (defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063)) 1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define REP_BUF_DATA(rep) ((rep)->buf->ugd_data) 1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define REP_BUF_DATA(rep) ((rep)->buf->data) 1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint 1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_SYS_JoystickInit(void) 1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall char s[16]; 1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int i, fd; 1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_numjoysticks = 0; 1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(joynames, 0, sizeof(joynames)); 1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(joydevnames, 0, sizeof(joydevnames)); 1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for (i = 0; i < MAX_UHID_JOYS; i++) { 1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Joystick nj; 1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i); 1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall nj.index = SDL_numjoysticks; 1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joynames[nj.index] = strdup(s); 1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (SDL_SYS_JoystickOpen(&nj) == 0) { 1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SYS_JoystickClose(&nj); 1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_numjoysticks++; 1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(joynames[nj.index]); 1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joynames[nj.index] = NULL; 1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for (i = 0; i < MAX_JOY_JOYS; i++) { 1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i); 1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fd = open(s, O_RDONLY); 1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (fd != -1) { 1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joynames[SDL_numjoysticks++] = strdup(s); 1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall close(fd); 1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Read the default USB HID usage table. */ 1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hid_init(NULL); 1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (SDL_numjoysticks); 1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst char * 2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_SYS_JoystickName(int index) 2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (joydevnames[index] != NULL) { 2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (joydevnames[index]); 2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (joynames[index]); 2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int 2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallusage_to_joyaxe(unsigned usage) 2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int joyaxe; 2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch (usage) { 2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUG_X: 2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joyaxe = JOYAXE_X; break; 2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUG_Y: 2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joyaxe = JOYAXE_Y; break; 2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUG_Z: 2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joyaxe = JOYAXE_Z; break; 2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUG_SLIDER: 2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joyaxe = JOYAXE_SLIDER; break; 2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUG_WHEEL: 2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joyaxe = JOYAXE_WHEEL; break; 2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUG_RX: 2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joyaxe = JOYAXE_RX; break; 2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUG_RY: 2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joyaxe = JOYAXE_RY; break; 2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUG_RZ: 2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joyaxe = JOYAXE_RZ; break; 2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall default: 2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joyaxe = -1; 2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return joyaxe; 2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic unsigned 2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallhatval_to_sdl(Sint32 hatval) 2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall static const unsigned hat_dir_map[8] = { 2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_HAT_UP, SDL_HAT_RIGHTUP, SDL_HAT_RIGHT, SDL_HAT_RIGHTDOWN, 2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_HAT_DOWN, SDL_HAT_LEFTDOWN, SDL_HAT_LEFT, SDL_HAT_LEFTUP 2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall }; 2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall unsigned result; 2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ((hatval & 7) == hatval) 2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = hat_dir_map[hatval]; 2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall else 2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = SDL_HAT_CENTERED; 2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return result; 2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint 2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_SYS_JoystickOpen(SDL_Joystick *joy) 2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall char *path = joynames[joy->index]; 2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct joystick_hwdata *hw; 2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct hid_item hitem; 2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct hid_data *hdata; 2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct report *rep; 2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int fd; 2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int i; 2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fd = open(path, O_RDONLY); 2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (fd == -1) { 2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("%s: %s", path, strerror(errno)); 2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (-1); 2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw = (struct joystick_hwdata *)SDL_malloc(sizeof(struct joystick_hwdata)); 2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (hw == NULL) { 2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_OutOfMemory(); 2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall close(fd); 2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (-1); 2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->hwdata = hw; 2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->fd = fd; 2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->path = strdup(path); 2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->x = 0; 2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->y = 0; 2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->xmin = 0xffff; 2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->ymin = 0xffff; 2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->xmax = 0; 2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->ymax = 0; 2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (! SDL_strncmp(path, "/dev/joy", 8)) { 2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->type = BSDJOY_JOY; 2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->naxes = 2; 2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->nbuttons = 2; 2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->nhats = 0; 2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->nballs = 0; 2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joydevnames[joy->index] = strdup("Gameport joystick"); 2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto usbend; 2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->type = BSDJOY_UHID; 2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { 2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int ax; 2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for (ax = 0; ax < JOYAXE_count; ax++) 3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->axis_map[ax] = -1; 3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->repdesc = hid_get_report_desc(fd); 3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (hw->repdesc == NULL) { 3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path, 3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall strerror(errno)); 3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto usberr; 3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall rep = &hw->inreport; 3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(__FREEBSD__) && (__FreeBSD_kernel_version > 800063) || defined(__FreeBSD_kernel__) 3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall rep->rid = hid_get_report_id(fd); 3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (rep->rid < 0) { 3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (ioctl(fd, USB_GET_REPORT_ID, &rep->rid) < 0) { 3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall rep->rid = -1; /* XXX */ 3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) { 3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto usberr; 3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (rep->size <= 0) { 3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("%s: Input report descriptor has invalid length", 3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->path); 3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto usberr; 3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111) || defined(__FreeBSD_kernel__) 3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hdata = hid_start_parse(hw->repdesc, 1 << hid_input, rep->rid); 3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hdata = hid_start_parse(hw->repdesc, 1 << hid_input); 3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (hdata == NULL) { 3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("%s: Cannot start HID parser", hw->path); 3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall goto usberr; 3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->naxes = 0; 3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->nbuttons = 0; 3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->nhats = 0; 3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->nballs = 0; 3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for (i=0; i<JOYAXE_count; i++) 3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->axis_map[i] = -1; 3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall while (hid_get_item(hdata, &hitem) > 0) { 3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall char *sp; 3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const char *s; 3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch (hitem.kind) { 3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case hid_collection: 3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch (HID_PAGE(hitem.usage)) { 3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUP_GENERIC_DESKTOP: 3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch (HID_USAGE(hitem.usage)) { 3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUG_JOYSTICK: 3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUG_GAME_PAD: 3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall s = hid_usage_in_page(hitem.usage); 3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall sp = SDL_malloc(SDL_strlen(s) + 5); 3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_snprintf(sp, SDL_strlen(s) + 5, "%s (%d)", s, 3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->index); 3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joydevnames[joy->index] = sp; 3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case hid_input: 3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch (HID_PAGE(hitem.usage)) { 3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUP_GENERIC_DESKTOP: { 3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall unsigned usage = HID_USAGE(hitem.usage); 3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int joyaxe = usage_to_joyaxe(usage); 3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (joyaxe >= 0) { 3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->axis_map[joyaxe] = 1; 3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else if (usage == HUG_HAT_SWITCH) { 3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->nhats++; 3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUP_BUTTON: 3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->nbuttons++; 3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall default: 3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall default: 3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hid_end_parse(hdata); 3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for (i=0; i<JOYAXE_count; i++) 3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (hw->axis_map[i] > 0) 3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hw->axis_map[i] = joy->naxes++; 3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallusbend: 3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* The poll blocks the event thread. */ 3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fcntl(fd, F_SETFL, O_NONBLOCK); 3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (0); 3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallusberr: 3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall close(hw->fd); 3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(hw->path); 3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(hw); 3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (-1); 3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid 4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_SYS_JoystickUpdate(SDL_Joystick *joy) 4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct hid_item hitem; 4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct hid_data *hdata; 4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct report *rep; 4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int nbutton, naxe = -1; 4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sint32 v; 4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H || defined(__FreeBSD_kernel__) 4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct joystick gameport; 4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (joy->hwdata->type == BSDJOY_JOY) { 4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (read(joy->hwdata->fd, &gameport, sizeof gameport) != sizeof gameport) 4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (abs(joy->hwdata->x - gameport.x) > 8) { 4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->hwdata->x = gameport.x; 4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (joy->hwdata->x < joy->hwdata->xmin) { 4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->hwdata->xmin = joy->hwdata->x; 4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (joy->hwdata->x > joy->hwdata->xmax) { 4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->hwdata->xmax = joy->hwdata->x; 4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (joy->hwdata->xmin == joy->hwdata->xmax) { 4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->hwdata->xmin--; 4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->hwdata->xmax++; 4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v = (Sint32)joy->hwdata->x; 4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v -= (joy->hwdata->xmax + joy->hwdata->xmin + 1)/2; 4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v *= 32768/((joy->hwdata->xmax - joy->hwdata->xmin + 1)/2); 4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_PrivateJoystickAxis(joy, 0, v); 4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (abs(joy->hwdata->y - gameport.y) > 8) { 4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->hwdata->y = gameport.y; 4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (joy->hwdata->y < joy->hwdata->ymin) { 4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->hwdata->ymin = joy->hwdata->y; 4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (joy->hwdata->y > joy->hwdata->ymax) { 4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->hwdata->ymax = joy->hwdata->y; 4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (joy->hwdata->ymin == joy->hwdata->ymax) { 4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->hwdata->ymin--; 4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->hwdata->ymax++; 4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v = (Sint32)joy->hwdata->y; 4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v -= (joy->hwdata->ymax + joy->hwdata->ymin + 1)/2; 4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v *= 32768/((joy->hwdata->ymax - joy->hwdata->ymin + 1)/2); 4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_PrivateJoystickAxis(joy, 1, v); 4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (gameport.b1 != joy->buttons[0]) { 4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_PrivateJoystickButton(joy, 0, gameport.b1); 4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (gameport.b2 != joy->buttons[1]) { 4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_PrivateJoystickButton(joy, 1, gameport.b2); 4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H */ 4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall rep = &joy->hwdata->inreport; 4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) { 4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111) || defined(__FreeBSD_kernel__) 4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid); 4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input); 4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (hdata == NULL) { 4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fprintf(stderr, "%s: Cannot start HID parser\n", 4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall joy->hwdata->path); 4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) { 4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch (hitem.kind) { 4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case hid_input: 4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch (HID_PAGE(hitem.usage)) { 4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUP_GENERIC_DESKTOP: { 4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall unsigned usage = HID_USAGE(hitem.usage); 4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int joyaxe = usage_to_joyaxe(usage); 4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (joyaxe >= 0) { 4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall naxe = joy->hwdata->axis_map[joyaxe]; 4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* scaleaxe */ 4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v = (Sint32)hid_get_data(REP_BUF_DATA(rep), 4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall &hitem); 4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v -= (hitem.logical_maximum + hitem.logical_minimum + 1)/2; 4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v *= 32768/((hitem.logical_maximum - hitem.logical_minimum + 1)/2); 4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (v != joy->axes[naxe]) { 4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_PrivateJoystickAxis(joy, naxe, v); 4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else if (usage == HUG_HAT_SWITCH) { 4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v = (Sint32)hid_get_data(REP_BUF_DATA(rep), 4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall &hitem); 4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_PrivateJoystickHat(joy, 0, 4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hatval_to_sdl(v)-hitem.logical_minimum); 4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case HUP_BUTTON: 5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall v = (Sint32)hid_get_data(REP_BUF_DATA(rep), 5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall &hitem); 5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (joy->buttons[nbutton] != v) { 5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_PrivateJoystickButton(joy, 5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall nbutton, v); 5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall nbutton++; 5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall default: 5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall continue; 5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall default: 5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hid_end_parse(hdata); 5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to close a joystick after use */ 5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid 5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_SYS_JoystickClose(SDL_Joystick *joy) 5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (SDL_strncmp(joy->hwdata->path, "/dev/joy", 8)) { 5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall report_free(&joy->hwdata->inreport); 5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall hid_dispose_report_desc(joy->hwdata->repdesc); 5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall close(joy->hwdata->fd); 5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(joy->hwdata->path); 5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(joy->hwdata); 5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid 5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_SYS_JoystickQuit(void) 5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int i; 5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for (i = 0; i < MAX_JOYS; i++) { 5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (joynames[i] != NULL) 5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(joynames[i]); 5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (joydevnames[i] != NULL) 5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(joydevnames[i]); 5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int 5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallreport_alloc(struct report *r, struct report_desc *rd, int repind) 5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int len; 5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __DragonFly__ 5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall len = hid_report_size(rd, r->rid, repinfo[repind].kind); 5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif __FREEBSD__ 5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# if (__FreeBSD_kernel_version >= 460000) || defined(__FreeBSD_kernel__) 5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# if (__FreeBSD_kernel_version <= 500111) 5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall len = hid_report_size(rd, r->rid, repinfo[repind].kind); 5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# else 5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall len = hid_report_size(rd, repinfo[repind].kind, r->rid); 5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# endif 5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# else 5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall len = hid_report_size(rd, repinfo[repind].kind, &r->rid); 5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# endif 5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# ifdef USBHID_NEW 5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall len = hid_report_size(rd, repinfo[repind].kind, r->rid); 5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# else 5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall len = hid_report_size(rd, repinfo[repind].kind, &r->rid); 5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall# endif 5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (len < 0) { 5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Negative HID report size"); 5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (-1); 5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall r->size = len; 5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (r->size > 0) { 5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall r->buf = SDL_malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) + 5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall r->size); 5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (r->buf == NULL) { 5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_OutOfMemory(); 5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (-1); 5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall r->buf = NULL; 5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall r->status = SREPORT_CLEAN; 5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return (0); 5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void 6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallreport_free(struct report *r) 6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (r->buf != NULL) { 6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(r->buf); 6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall r->status = SREPORT_UNINIT; 6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_JOYSTICK_USBHID */ 609