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