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