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