src/joystick/bsd/SDL_sysjoystick.c
author Sam Lantinga
Tue, 11 Jun 2002 20:11:30 +0000
changeset 405 b5de7389a0a5
parent 404 cbb346aca05d
child 407 173909e17b7f
permissions -rw-r--r--
*** empty log message ***
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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 <stdio.h>
    36 #include <stdlib.h>
    37 #include <unistd.h>
    38 #include <fcntl.h>
    39 #include <string.h>
    40 #include <errno.h>
    41 
    42 #if defined(HAVE_USB_H)
    43 #include <usb.h>
    44 #endif
    45 #include <dev/usb/usb.h>
    46 #include <dev/usb/usbhid.h>
    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 #include "SDL_error.h"
    57 #include "SDL_joystick.h"
    58 #include "SDL_sysjoystick.h"
    59 #include "SDL_joystick_c.h"
    60 
    61 #define MAX_UHID_JOYS	4
    62 #define MAX_JOY_JOYS	2
    63 #define MAX_JOYS	(MAX_UHID_JOYS + MAX_JOY_JOYS)
    64 
    65 struct report {
    66 	struct	usb_ctl_report *buf;	/* Buffer */
    67 	size_t	size;			/* Buffer size */
    68 	int	rid;			/* Report ID */
    69 	enum {
    70 		SREPORT_UNINIT,
    71 		SREPORT_CLEAN,
    72 		SREPORT_DIRTY
    73 	} status;
    74 };
    75 
    76 static struct {
    77 	int	uhid_report;
    78 	hid_kind_t kind;
    79 	const	char *name;
    80 } const repinfo[] = {
    81 	{ UHID_INPUT_REPORT,	hid_input,	"input" },
    82 	{ UHID_OUTPUT_REPORT,	hid_output,	"output" },
    83 	{ UHID_FEATURE_REPORT,	hid_feature,	"feature" }
    84 };
    85 
    86 enum {
    87 	REPORT_INPUT = 0,
    88 	REPORT_OUTPUT = 1,
    89 	REPORT_FEATURE = 2
    90 };
    91 
    92 enum {
    93 	JOYAXE_X,
    94 	JOYAXE_Y,
    95 	JOYAXE_Z,
    96 	JOYAXE_SLIDER,
    97 	JOYAXE_WHEEL
    98 };
    99 
   100 struct joystick_hwdata {
   101 	int	fd;
   102 	char	*path;
   103 	enum {
   104 		BSDJOY_UHID,	/* uhid(4) */
   105 		BSDJOY_JOY	/* joy(4) */
   106 	} type;
   107 	struct	report_desc *repdesc;
   108 	struct	report inreport;
   109 #if 0
   110 	int	axismin[];
   111 	int	axismax[];
   112 #endif
   113 };
   114 
   115 static char *joynames[MAX_JOYS];
   116 static char *joydevnames[MAX_JOYS];
   117 
   118 static int	report_alloc(struct report *, struct report_desc *, int);
   119 static void	report_free(struct report *);
   120 
   121 #ifdef USBHID_UCR_DATA
   122 #define REP_BUF_DATA(rep) ((rep)->buf->ucr_data)
   123 #else
   124 #define REP_BUF_DATA(rep) ((rep)->buf->data)
   125 #endif
   126 
   127 int
   128 SDL_SYS_JoystickInit(void)
   129 {
   130 	char s[10];
   131 	int i, fd;
   132 
   133 	SDL_numjoysticks = 0;
   134 
   135 	memset(joynames, NULL, sizeof(joynames));
   136 	memset(joydevnames, NULL, sizeof(joydevnames));
   137 
   138 	for (i = 0; i < MAX_UHID_JOYS; i++) {
   139 		sprintf(s, "/dev/uhid%d", i);
   140 		fd = open(s, O_RDWR);
   141 		if (fd > 0) {
   142 			joynames[SDL_numjoysticks++] = strdup(s);
   143 			close(fd);
   144 		}
   145 	}
   146 	for (i = 0; i < MAX_JOY_JOYS; i++) {
   147 		sprintf(s, "/dev/joy%d", i);
   148 		fd = open(s, O_RDWR);
   149 		if (fd > 0) {
   150 			joynames[SDL_numjoysticks++] = strdup(s);
   151 			close(fd);
   152 		}
   153 	}
   154 
   155 	/* Read the default USB HID usage table. */
   156 	hid_init(NULL);
   157 
   158 	return (SDL_numjoysticks);
   159 }
   160 
   161 const char *
   162 SDL_SYS_JoystickName(int index)
   163 {
   164 	if (joydevnames[index] != NULL) {
   165 		return (joydevnames[index]);
   166 	}
   167 	return (joynames[index]);
   168 }
   169 
   170 int
   171 SDL_SYS_JoystickOpen(SDL_Joystick *joy)
   172 {
   173 	char *path = joynames[joy->index];
   174 	struct joystick_hwdata *hw;
   175 	struct hid_item hitem;
   176 	struct hid_data *hdata;
   177 	struct report *rep;
   178 	int fd;
   179 
   180 	fd = open(path, O_RDWR);
   181 	if (fd < 0) {
   182 		SDL_SetError("%s: %s", path, strerror(errno));
   183 		return (-1);
   184 	}
   185 
   186 	hw = (struct joystick_hwdata *)malloc(sizeof(struct joystick_hwdata));
   187 	if (hw == NULL) {
   188 		SDL_OutOfMemory();
   189 		close(fd);
   190 		return (-1);
   191 	}
   192 	joy->hwdata = hw;
   193 	hw->fd = fd;
   194 	hw->path = strdup(path);
   195 	hw->type = BSDJOY_UHID;
   196 	hw->repdesc = hid_get_report_desc(fd);
   197 	if (hw->repdesc == NULL) {
   198 		SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
   199 		    strerror(errno));
   200 		goto usberr;
   201 	}
   202 
   203 	rep = &hw->inreport;
   204 	if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
   205 		goto usberr;
   206 	}
   207 	if (rep->size <= 0) {
   208 		SDL_SetError("%s: Input report descriptor has invalid length",
   209 		    hw->path);
   210 		goto usberr;
   211 	}
   212 
   213 	hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
   214 	if (hdata == NULL) {
   215 		SDL_SetError("%s: Cannot start HID parser", hw->path);
   216 		goto usberr;
   217 	}
   218 	joy->naxes = 0;
   219 	joy->nbuttons = 0;
   220 	joy->nhats = 0;
   221 	joy->nballs = 0;
   222 
   223 	while (hid_get_item(hdata, &hitem) > 0) {
   224 		char *sp;
   225 		const char *s;
   226 
   227 		switch (hitem.kind) {
   228 		case hid_collection:
   229 			switch (HID_PAGE(hitem.usage)) {
   230 			case HUP_GENERIC_DESKTOP:
   231 				switch (HID_USAGE(hitem.usage)) {
   232 				case HUG_JOYSTICK:
   233 				case HUG_GAME_PAD:
   234 					s = hid_usage_in_page(hitem.usage);
   235 					sp = malloc(strlen(s) + 5);
   236 					sprintf(sp, "%s (%d)", s,
   237 					    joy->index);
   238 					joydevnames[joy->index] = sp;
   239 				}
   240 			}
   241 			break;
   242 		case hid_input:
   243 			switch (HID_PAGE(hitem.usage)) {
   244 			case HUP_GENERIC_DESKTOP:
   245 				switch (HID_USAGE(hitem.usage)) {
   246 				case HUG_X:
   247 				case HUG_Y:
   248 				case HUG_Z:
   249 				case HUG_SLIDER:
   250 				case HUG_WHEEL:
   251 #if 0
   252 					hw->axismin[joy->naxes] =
   253 					    hitem.logical_minimum;
   254 					hw->axismax[joy->naxes] =
   255 					    hitem.logical_maximum;
   256 #endif
   257 					joy->naxes++;
   258 					break;
   259 				}
   260 				break;
   261 			case HUP_BUTTON:
   262 				joy->nbuttons++;
   263 				break;
   264 			default:
   265 				break;
   266 			}
   267 			break;
   268 		default:
   269 			break;
   270 		}
   271 	}
   272 	hid_end_parse(hdata);
   273 
   274 	/* The poll blocks the event thread. */
   275 	fcntl(fd, F_SETFL, O_NONBLOCK);
   276 
   277 	return (0);
   278 usberr:
   279 	close(hw->fd);
   280 	free(hw->path);
   281 	free(hw);
   282 	return (-1);
   283 }
   284 
   285 void
   286 SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
   287 {
   288 	struct hid_item hitem;
   289 	struct hid_data *hdata;
   290 	struct report *rep;
   291 	int nbutton, naxe = -1;
   292 	Sint32 v;
   293 	
   294 	rep = &joy->hwdata->inreport;
   295 
   296 	if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
   297 		return;
   298 	}
   299 	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
   300 	if (hdata == NULL) {
   301 		fprintf(stderr, "%s: Cannot start HID parser\n",
   302 		    joy->hwdata->path);
   303 		return;
   304 	}
   305 
   306 	for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
   307 		switch (hitem.kind) {
   308 		case hid_input:
   309 			switch (HID_PAGE(hitem.usage)) {
   310 			case HUP_GENERIC_DESKTOP:
   311 				switch (HID_USAGE(hitem.usage)) {
   312 				case HUG_X:
   313 					naxe = JOYAXE_X;
   314 					goto scaleaxe;
   315 				case HUG_Y:
   316 					naxe = JOYAXE_Y;
   317 					goto scaleaxe;
   318 				case HUG_Z:
   319 					naxe = JOYAXE_Z;
   320 					goto scaleaxe;
   321 				case HUG_SLIDER:
   322 					naxe = JOYAXE_SLIDER;
   323 					goto scaleaxe;
   324 				case HUG_WHEEL:
   325 					naxe = JOYAXE_WHEEL;
   326 					goto scaleaxe;
   327 				}
   328 scaleaxe:
   329 				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
   330 				    &hitem);
   331 				if (v != 127) {
   332 					if (v < 127) {
   333 						v = -(256 - v);
   334 						v <<= 7;
   335 						v++;
   336 					} else {
   337 						v++;
   338 						v <<= 7;
   339 						v--;
   340 					}
   341 				} else {
   342 					v = 0;
   343 				}
   344 				if (v != joy->axes[naxe]) {
   345 					SDL_PrivateJoystickAxis(joy, naxe, v);
   346 				}
   347 				break;
   348 			case HUP_BUTTON:
   349 				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
   350 				    &hitem);
   351 				if (joy->buttons[nbutton] != v) {
   352 					SDL_PrivateJoystickButton(joy,
   353 					    nbutton, v);
   354 				}
   355 				nbutton++;
   356 				break;
   357 			default:
   358 				continue;
   359 			}
   360 			break;
   361 		default:
   362 			break;
   363 		}
   364 	}
   365 	hid_end_parse(hdata);
   366 
   367 	return;
   368 }
   369 
   370 /* Function to close a joystick after use */
   371 void
   372 SDL_SYS_JoystickClose(SDL_Joystick *joy)
   373 {
   374 	report_free(&joy->hwdata->inreport);
   375 	hid_dispose_report_desc(joy->hwdata->repdesc);
   376 	close(joy->hwdata->fd);
   377 	free(joy->hwdata->path);
   378 	free(joy->hwdata);
   379 
   380 	return;
   381 }
   382 
   383 void
   384 SDL_SYS_JoystickQuit(void)
   385 {
   386 	int i;
   387 
   388 	for (i = 0; i < MAX_JOYS; i++) {
   389 		if (joynames[i] != NULL)
   390 			free(joynames[i]);
   391 		if (joydevnames[i] != NULL)
   392 			free(joydevnames[i]);
   393 	}
   394 
   395 	return;
   396 }
   397 
   398 static int
   399 report_alloc(struct report *r, struct report_desc *rd, int repind)
   400 {
   401 	int len;
   402 
   403 	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
   404 	if (len < 0) {
   405 		SDL_SetError("Negative HID report size");
   406 		return (-1);
   407 	}
   408 	r->size = len;
   409 
   410 	if (r->size > 0) {
   411 		r->buf = malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) +
   412 		    r->size);
   413 		if (r->buf == NULL) {
   414 			SDL_OutOfMemory();
   415 			return (-1);
   416 		}
   417 	} else {
   418 		r->buf = NULL;
   419 	}
   420 
   421 	r->status = SREPORT_CLEAN;
   422 	return (0);
   423 }
   424 
   425 static void
   426 report_free(struct report *r)
   427 {
   428 	if (r->buf != NULL) {
   429 		free(r->buf);
   430 	}
   431 	r->status = SREPORT_UNINIT;
   432 }
   433