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