src/joystick/bsd/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 Mar 2006 08:54:50 +0000
changeset 1565 57431b199aed
parent 1559 e7f61857028a
child 1635 92947e3a18db
permissions -rw-r--r--
Fixed bug #52

Integrated most of the NetBSD and DragonFly patches at:
ftp://ftp.netbsd.org/pub/NetBSD/packages/pkgsrc/devel/SDL/patches/

Thanks to Thomas Klausner for defailed information on the patches
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /*
    25  * Joystick driver for the uhid(4) interface found in OpenBSD,
    26  * NetBSD and FreeBSD.
    27  *
    28  * Maintainer: <vedge at csoft.org>
    29  */
    30 
    31 #include <sys/param.h>
    32 
    33 #include <unistd.h>
    34 #include <fcntl.h>
    35 #include <errno.h>
    36 
    37 #if defined(HAVE_USB_H)
    38 #include <usb.h>
    39 #endif
    40 #ifdef __DragonFly__
    41 #include <bus/usb/usb.h>
    42 #include <bus/usb/usbhid.h>
    43 #else
    44 #include <dev/usb/usb.h>
    45 #include <dev/usb/usbhid.h>
    46 #endif
    47 
    48 #if defined(HAVE_USBHID_H)
    49 #include <usbhid.h>
    50 #elif defined(HAVE_LIBUSB_H)
    51 #include <libusb.h>
    52 #elif defined(HAVE_LIBUSBHID_H)
    53 #include <libusbhid.h>
    54 #endif
    55 
    56 #ifdef __FREEBSD__
    57 #ifndef __DragonFly__
    58 #include <osreldate.h>
    59 #endif
    60 #include <sys/joystick.h>
    61 #endif
    62 
    63 #if SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H
    64 #include <machine/joystick.h>
    65 #endif
    66 
    67 #include "SDL_joystick.h"
    68 #include "../SDL_sysjoystick.h"
    69 #include "../SDL_joystick_c.h"
    70 
    71 #define MAX_UHID_JOYS	4
    72 #define MAX_JOY_JOYS	2
    73 #define MAX_JOYS	(MAX_UHID_JOYS + MAX_JOY_JOYS)
    74 
    75 struct report {
    76 	struct	usb_ctl_report *buf;	/* Buffer */
    77 	size_t	size;			/* Buffer size */
    78 	int	rid;			/* Report ID */
    79 	enum {
    80 		SREPORT_UNINIT,
    81 		SREPORT_CLEAN,
    82 		SREPORT_DIRTY
    83 	} status;
    84 };
    85 
    86 static struct {
    87 	int	uhid_report;
    88 	hid_kind_t kind;
    89 	const	char *name;
    90 } const repinfo[] = {
    91 	{ UHID_INPUT_REPORT,	hid_input,	"input" },
    92 	{ UHID_OUTPUT_REPORT,	hid_output,	"output" },
    93 	{ UHID_FEATURE_REPORT,	hid_feature,	"feature" }
    94 };
    95 
    96 enum {
    97 	REPORT_INPUT = 0,
    98 	REPORT_OUTPUT = 1,
    99 	REPORT_FEATURE = 2
   100 };
   101 
   102 enum {
   103 	JOYAXE_X,
   104 	JOYAXE_Y,
   105 	JOYAXE_Z,
   106 	JOYAXE_SLIDER,
   107 	JOYAXE_WHEEL,
   108 	JOYAXE_RX,
   109 	JOYAXE_RY,
   110 	JOYAXE_RZ,
   111 	JOYAXE_count
   112 };
   113 
   114 struct joystick_hwdata {
   115 	int	fd;
   116 	char	*path;
   117 	enum {
   118 		BSDJOY_UHID,	/* uhid(4) */
   119 		BSDJOY_JOY	/* joy(4) */
   120 	} type;
   121 	struct	report_desc *repdesc;
   122 	struct	report inreport;
   123 	int	axis_map[JOYAXE_count];	/* map present JOYAXE_* to 0,1,..*/
   124 };
   125 
   126 static char *joynames[MAX_JOYS];
   127 static char *joydevnames[MAX_JOYS];
   128 
   129 static int	report_alloc(struct report *, struct report_desc *, int);
   130 static void	report_free(struct report *);
   131 
   132 #ifdef USBHID_UCR_DATA
   133 #define REP_BUF_DATA(rep) ((rep)->buf->ucr_data)
   134 #else
   135 #define REP_BUF_DATA(rep) ((rep)->buf->data)
   136 #endif
   137 
   138 int
   139 SDL_SYS_JoystickInit(void)
   140 {
   141 	char s[16];
   142 	int i, fd;
   143 
   144 	SDL_numjoysticks = 0;
   145 
   146 	SDL_memset(joynames, 0, sizeof(joynames));
   147 	SDL_memset(joydevnames, 0, sizeof(joydevnames));
   148 
   149 	for (i = 0; i < MAX_UHID_JOYS; i++) {
   150 		SDL_Joystick nj;
   151 
   152 		SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i);
   153 
   154 		nj.index = SDL_numjoysticks;
   155 		joynames[nj.index] = strdup(s);
   156 
   157 		if (SDL_SYS_JoystickOpen(&nj) == 0) {
   158 			SDL_SYS_JoystickClose(&nj);
   159 			SDL_numjoysticks++;
   160 		} else {
   161 			SDL_free(joynames[nj.index]);
   162 			joynames[nj.index] = NULL;
   163 		}
   164 	}
   165 	for (i = 0; i < MAX_JOY_JOYS; i++) {
   166 		SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i);
   167 		fd = open(s, O_RDONLY);
   168 		if (fd != -1) {
   169 			joynames[SDL_numjoysticks++] = strdup(s);
   170 			close(fd);
   171 		}
   172 	}
   173 
   174 	/* Read the default USB HID usage table. */
   175 	hid_init(NULL);
   176 
   177 	return (SDL_numjoysticks);
   178 }
   179 
   180 const char *
   181 SDL_SYS_JoystickName(int index)
   182 {
   183 	if (joydevnames[index] != NULL) {
   184 		return (joydevnames[index]);
   185 	}
   186 	return (joynames[index]);
   187 }
   188 
   189 static int
   190 usage_to_joyaxe(unsigned usage)
   191 {
   192     int joyaxe;
   193     switch (usage) {
   194     case HUG_X:
   195 	joyaxe = JOYAXE_X; break;
   196     case HUG_Y:
   197 	joyaxe = JOYAXE_Y; break;
   198     case HUG_Z:
   199 	joyaxe = JOYAXE_Z; break;
   200     case HUG_SLIDER:
   201 	joyaxe = JOYAXE_SLIDER; break;
   202     case HUG_WHEEL:
   203 	joyaxe = JOYAXE_WHEEL; break;
   204     case HUG_RX:
   205 	joyaxe = JOYAXE_RX; break;
   206     case HUG_RY:
   207 	joyaxe = JOYAXE_RY; break;
   208     case HUG_RZ:
   209 	joyaxe = JOYAXE_RZ; break;
   210     default:
   211 	joyaxe = -1;
   212     }
   213     return joyaxe;    
   214 }
   215 
   216 static unsigned
   217 hatval_to_sdl(Sint32 hatval)
   218 {
   219     static const unsigned hat_dir_map[8] = {
   220 	SDL_HAT_UP, SDL_HAT_RIGHTUP, SDL_HAT_RIGHT, SDL_HAT_RIGHTDOWN, 
   221 	SDL_HAT_DOWN, SDL_HAT_LEFTDOWN, SDL_HAT_LEFT, SDL_HAT_LEFTUP
   222     };
   223     unsigned result;
   224     if ((hatval & 7) == hatval) 
   225 	result = hat_dir_map[hatval];
   226     else 
   227 	result = SDL_HAT_CENTERED;
   228     return result;
   229 }
   230 
   231 
   232 int
   233 SDL_SYS_JoystickOpen(SDL_Joystick *joy)
   234 {
   235 	char *path = joynames[joy->index];
   236 	struct joystick_hwdata *hw;
   237 	struct hid_item hitem;
   238 	struct hid_data *hdata;
   239 	struct report *rep;
   240 	int fd;
   241 	int i;
   242 
   243 	fd = open(path, O_RDONLY);
   244 	if (fd == -1) {
   245 		SDL_SetError("%s: %s", path, strerror(errno));
   246 		return (-1);
   247 	}
   248 
   249 	hw = (struct joystick_hwdata *)SDL_malloc(sizeof(struct joystick_hwdata));
   250 	if (hw == NULL) {
   251 		SDL_OutOfMemory();
   252 		close(fd);
   253 		return (-1);
   254 	}
   255 	joy->hwdata = hw;
   256 	hw->fd = fd;
   257 	hw->path = strdup(path);
   258 	if (! SDL_strncmp(path, "/dev/joy", 8)) {
   259 		hw->type = BSDJOY_JOY;
   260 		joy->naxes = 2;
   261 		joy->nbuttons = 2;
   262 		joy->nhats = 0;
   263 		joy->nballs = 0;
   264 		joydevnames[joy->index] = strdup("Gameport joystick");
   265 		goto usbend;
   266 	} else {
   267 		hw->type = BSDJOY_UHID;
   268 	}
   269 
   270 	{
   271 	    int ax;
   272 	    for (ax = 0; ax < JOYAXE_count; ax++)
   273 		hw->axis_map[ax] = -1;
   274 	}
   275 	hw->repdesc = hid_get_report_desc(fd);
   276 	if (hw->repdesc == NULL) {
   277 		SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
   278 		    strerror(errno));
   279 		goto usberr;
   280 	}
   281 
   282 	rep = &hw->inreport;
   283 	if (ioctl(fd, USB_GET_REPORT_ID, &rep->rid) < 0) {
   284 		rep->rid = -1; /* XXX */
   285 	}
   286 	if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
   287 		goto usberr;
   288 	}
   289 	if (rep->size <= 0) {
   290 		SDL_SetError("%s: Input report descriptor has invalid length",
   291 		    hw->path);
   292 		goto usberr;
   293 	}
   294 
   295 #if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_version >= 500111)
   296 	hdata = hid_start_parse(hw->repdesc, 1 << hid_input, rep->rid);
   297 #else
   298 	hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
   299 #endif
   300 	if (hdata == NULL) {
   301 		SDL_SetError("%s: Cannot start HID parser", hw->path);
   302 		goto usberr;
   303 	}
   304 	joy->naxes = 0;
   305 	joy->nbuttons = 0;
   306 	joy->nhats = 0;
   307 	joy->nballs = 0;
   308 	for (i=0; i<JOYAXE_count; i++)
   309 		hw->axis_map[i] = -1;
   310 
   311 	while (hid_get_item(hdata, &hitem) > 0) {
   312 		char *sp;
   313 		const char *s;
   314 
   315 		switch (hitem.kind) {
   316 		case hid_collection:
   317 			switch (HID_PAGE(hitem.usage)) {
   318 			case HUP_GENERIC_DESKTOP:
   319 				switch (HID_USAGE(hitem.usage)) {
   320 				case HUG_JOYSTICK:
   321 				case HUG_GAME_PAD:
   322 					s = hid_usage_in_page(hitem.usage);
   323 					sp = SDL_malloc(SDL_strlen(s) + 5);
   324 					SDL_snprintf(sp, SDL_strlen(s) + 5, "%s (%d)", s,
   325 					    joy->index);
   326 					joydevnames[joy->index] = sp;
   327 				}
   328 			}
   329 			break;
   330 		case hid_input:
   331 			switch (HID_PAGE(hitem.usage)) {
   332 			case HUP_GENERIC_DESKTOP: {
   333 			    unsigned usage = HID_USAGE(hitem.usage);
   334 			    int joyaxe = usage_to_joyaxe(usage);
   335 			    if (joyaxe >= 0) {
   336 				hw->axis_map[joyaxe] = 1;
   337 			    } else if (usage == HUG_HAT_SWITCH) {
   338 				joy->nhats++;
   339 			    }
   340 			    break;
   341 			}
   342 			case HUP_BUTTON:
   343 				joy->nbuttons++;
   344 				break;
   345 			default:
   346 				break;
   347 			}
   348 			break;
   349 		default:
   350 			break;
   351 		}
   352 	}
   353 	hid_end_parse(hdata);
   354 	for (i=0; i<JOYAXE_count; i++)
   355 		if (hw->axis_map[i] > 0)
   356 			hw->axis_map[i] = joy->naxes++;
   357 
   358 usbend:
   359 	/* The poll blocks the event thread. */
   360 	fcntl(fd, F_SETFL, O_NONBLOCK);
   361 
   362 	return (0);
   363 usberr:
   364 	close(hw->fd);
   365 	SDL_free(hw->path);
   366 	SDL_free(hw);
   367 	return (-1);
   368 }
   369 
   370 void
   371 SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
   372 {
   373 	struct hid_item hitem;
   374 	struct hid_data *hdata;
   375 	struct report *rep;
   376 	int nbutton, naxe = -1;
   377 	Sint32 v;
   378 
   379 #if defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H
   380 	struct joystick gameport;
   381 	static int x, y, xmin = 0xffff, ymin = 0xffff, xmax = 0, ymax = 0;
   382  
   383 	if (joy->hwdata->type == BSDJOY_JOY) {
   384 		if (read(joy->hwdata->fd, &gameport, sizeof gameport) != sizeof gameport)
   385 			return;
   386 		if (abs(x - gameport.x) > 8) {
   387 			x = gameport.x;
   388 			if (x < xmin) {
   389 				xmin = x;
   390 			}
   391 			if (x > xmax) {
   392 				xmax = x;
   393 			}
   394 			if (xmin == xmax) {
   395 				xmin--;
   396 				xmax++;
   397 			}
   398 			v = (Sint32)x;
   399 			v -= (xmax + xmin + 1)/2;
   400 			v *= 32768/((xmax - xmin + 1)/2);
   401 			SDL_PrivateJoystickAxis(joy, 0, v);
   402 		}
   403 		if (abs(y - gameport.y) > 8) {
   404 			y = gameport.y;
   405 			if (y < ymin) {
   406 				ymin = y;
   407 			}
   408 			if (y > ymax) {
   409 				ymax = y;
   410 			}
   411 			if (ymin == ymax) {
   412 				ymin--;
   413 				ymax++;
   414 			}
   415 			v = (Sint32)y;
   416 			v -= (ymax + ymin + 1)/2;
   417 			v *= 32768/((ymax - ymin + 1)/2);
   418 			SDL_PrivateJoystickAxis(joy, 1, v);
   419 		}
   420 		if (gameport.b1 != joy->buttons[0]) {
   421 			SDL_PrivateJoystickButton(joy, 0, gameport.b1);
   422 		}
   423 		if (gameport.b2 != joy->buttons[1]) {
   424 			SDL_PrivateJoystickButton(joy, 1, gameport.b2);
   425 		}
   426 		return;
   427 	}
   428 #endif /* defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H */
   429 	
   430 	rep = &joy->hwdata->inreport;
   431 
   432 	if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
   433 		return;
   434 	}
   435 #if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_version >= 500111)
   436 	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
   437 #else
   438 	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
   439 #endif
   440 	if (hdata == NULL) {
   441 		fprintf(stderr, "%s: Cannot start HID parser\n",
   442 		    joy->hwdata->path);
   443 		return;
   444 	}
   445 
   446 	for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
   447 		switch (hitem.kind) {
   448 		case hid_input:
   449 			switch (HID_PAGE(hitem.usage)) {
   450 			case HUP_GENERIC_DESKTOP: {
   451 			    unsigned usage = HID_USAGE(hitem.usage);
   452 			    int joyaxe = usage_to_joyaxe(usage);
   453 			    if (joyaxe >= 0) {
   454 				naxe = joy->hwdata->axis_map[joyaxe];
   455 				/* scaleaxe */
   456 				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
   457 							 &hitem);
   458 				v -= (hitem.logical_maximum + hitem.logical_minimum + 1)/2;
   459 				v *= 32768/((hitem.logical_maximum - hitem.logical_minimum + 1)/2);
   460 				if (v != joy->axes[naxe]) {
   461 				    SDL_PrivateJoystickAxis(joy, naxe, v);
   462 				}
   463 			    } else if (usage == HUG_HAT_SWITCH) {
   464 				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
   465 							 &hitem);
   466 				SDL_PrivateJoystickHat(joy, 0,
   467 					hatval_to_sdl(v)-hitem.logical_minimum);
   468 			    }
   469 			    break;
   470 			}
   471 			case HUP_BUTTON:
   472 				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
   473 				    &hitem);
   474 				if (joy->buttons[nbutton] != v) {
   475 					SDL_PrivateJoystickButton(joy,
   476 					    nbutton, v);
   477 				}
   478 				nbutton++;
   479 				break;
   480 			default:
   481 				continue;
   482 			}
   483 			break;
   484 		default:
   485 			break;
   486 		}
   487 	}
   488 	hid_end_parse(hdata);
   489 
   490 	return;
   491 }
   492 
   493 /* Function to close a joystick after use */
   494 void
   495 SDL_SYS_JoystickClose(SDL_Joystick *joy)
   496 {
   497 	if (SDL_strncmp(joy->hwdata->path, "/dev/joy", 8))	{
   498 		report_free(&joy->hwdata->inreport);
   499 		hid_dispose_report_desc(joy->hwdata->repdesc);
   500 	}
   501 	close(joy->hwdata->fd);
   502 	SDL_free(joy->hwdata->path);
   503 	SDL_free(joy->hwdata);
   504 
   505 	return;
   506 }
   507 
   508 void
   509 SDL_SYS_JoystickQuit(void)
   510 {
   511 	int i;
   512 
   513 	for (i = 0; i < MAX_JOYS; i++) {
   514 		if (joynames[i] != NULL)
   515 			SDL_free(joynames[i]);
   516 		if (joydevnames[i] != NULL)
   517 			SDL_free(joydevnames[i]);
   518 	}
   519 
   520 	return;
   521 }
   522 
   523 static int
   524 report_alloc(struct report *r, struct report_desc *rd, int repind)
   525 {
   526 	int len;
   527 
   528 #ifdef __DragonFly__
   529 	len = hid_report_size(rd, r->rid, repinfo[repind].kind);
   530 #elif __FREEBSD__
   531 # if (__FreeBSD_version >= 460000)
   532 #  if (__FreeBSD_version <= 500111)
   533 	len = hid_report_size(rd, r->rid, repinfo[repind].kind);
   534 #  else
   535 	len = hid_report_size(rd, repinfo[repind].kind, r->rid);
   536 #  endif
   537 # else
   538 	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
   539 # endif
   540 #else
   541 # ifdef USBHID_NEW
   542 	len = hid_report_size(rd, repinfo[repind].kind, r->rid);
   543 # else
   544 	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
   545 # endif
   546 #endif
   547 
   548 	if (len < 0) {
   549 		SDL_SetError("Negative HID report size");
   550 		return (-1);
   551 	}
   552 	r->size = len;
   553 
   554 	if (r->size > 0) {
   555 		r->buf = SDL_malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) +
   556 		    r->size);
   557 		if (r->buf == NULL) {
   558 			SDL_OutOfMemory();
   559 			return (-1);
   560 		}
   561 	} else {
   562 		r->buf = NULL;
   563 	}
   564 
   565 	r->status = SREPORT_CLEAN;
   566 	return (0);
   567 }
   568 
   569 static void
   570 report_free(struct report *r)
   571 {
   572 	if (r->buf != NULL) {
   573 		SDL_free(r->buf);
   574 	}
   575 	r->status = SREPORT_UNINIT;
   576 }
   577