src/joystick/bsd/SDL_sysjoystick.c
author Sam Lantinga
Sun, 21 Sep 2003 18:32:04 +0000
changeset 720 f90d80d68071
parent 715 de0351c47596
child 769 b8d311d90021
permissions -rw-r--r--
N Sep 17 8791 Sam Lantinga Re: tks source released
Date: Sun, 07 Sep 2003 02:51:58 +0200
From: Stephane Marchesin
Subject: [SDL] Two little patches

Compiling SDL with a recent gcc (gcc 3.3.1, 3.3 doesn't have this
behaviour) gives some nasty warnings :

SDL_blit_A.c: In function `BlitRGBtoRGBSurfaceAlpha128MMX':
SDL_blit_A.c:223: warning: integer constant is too large for "long" type
SDL_blit_A.c:225: warning: integer constant is too large for "long" type
SDL_blit_A.c:227: warning: integer constant is too large for "long" type
[...]

The first attached patch (longlongfix.patch) tells gcc to really treat
those constants as unsigned long long and not long.

The second patch (nasinclude.patch) fixes an include problem I had while
compiling nas audio : when the <audio/audiolib.h> file lies in
/usr/X11R6/include, a -I/usr/X11R6/include option is needed or the file
isn't found.
     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 <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 #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, NULL, sizeof(joynames));
   143 	memset(joydevnames, NULL, 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 		}
   159 	}
   160 	for (i = 0; i < MAX_JOY_JOYS; i++) {
   161 		sprintf(s, "/dev/joy%d", i);
   162 		fd = open(s, O_RDONLY);
   163 		if (fd != -1) {
   164 			joynames[SDL_numjoysticks++] = strdup(s);
   165 			close(fd);
   166 		}
   167 	}
   168 
   169 	/* Read the default USB HID usage table. */
   170 	hid_init(NULL);
   171 
   172 	return (SDL_numjoysticks);
   173 }
   174 
   175 const char *
   176 SDL_SYS_JoystickName(int index)
   177 {
   178 	if (joydevnames[index] != NULL) {
   179 		return (joydevnames[index]);
   180 	}
   181 	return (joynames[index]);
   182 }
   183 
   184 static int
   185 usage_to_joyaxe(unsigned usage)
   186 {
   187     int joyaxe;
   188     switch (usage) {
   189     case HUG_X:
   190 	joyaxe = JOYAXE_X; break;
   191     case HUG_Y:
   192 	joyaxe = JOYAXE_Y; break;
   193     case HUG_Z:
   194 	joyaxe = JOYAXE_Z; break;
   195     case HUG_SLIDER:
   196 	joyaxe = JOYAXE_SLIDER; break;
   197     case HUG_WHEEL:
   198 	joyaxe = JOYAXE_WHEEL; break;
   199     case HUG_RX:
   200 	joyaxe = JOYAXE_RX; break;
   201     case HUG_RY:
   202 	joyaxe = JOYAXE_RY; break;
   203     case HUG_RZ:
   204 	joyaxe = JOYAXE_RZ; break;
   205     default:
   206 	joyaxe = -1;
   207     }
   208     return joyaxe;    
   209 }
   210 
   211 static unsigned
   212 hatval_to_sdl(Sint32 hatval)
   213 {
   214     static const unsigned hat_dir_map[8] = {
   215 	SDL_HAT_UP, SDL_HAT_RIGHTUP, SDL_HAT_RIGHT, SDL_HAT_RIGHTDOWN, 
   216 	SDL_HAT_DOWN, SDL_HAT_LEFTDOWN, SDL_HAT_LEFT, SDL_HAT_LEFTUP
   217     };
   218     unsigned result;
   219     if ((hatval & 7) == hatval) 
   220 	result = hat_dir_map[hatval];
   221     else 
   222 	result = SDL_HAT_CENTERED;
   223     return result;
   224 }
   225 
   226 
   227 int
   228 SDL_SYS_JoystickOpen(SDL_Joystick *joy)
   229 {
   230 	char *path = joynames[joy->index];
   231 	struct joystick_hwdata *hw;
   232 	struct hid_item hitem;
   233 	struct hid_data *hdata;
   234 	struct report *rep;
   235 	int fd;
   236 
   237 	fd = open(path, O_RDONLY);
   238 	if (fd == -1) {
   239 		SDL_SetError("%s: %s", path, strerror(errno));
   240 		return (-1);
   241 	}
   242 
   243 	hw = (struct joystick_hwdata *)malloc(sizeof(struct joystick_hwdata));
   244 	if (hw == NULL) {
   245 		SDL_OutOfMemory();
   246 		close(fd);
   247 		return (-1);
   248 	}
   249 	joy->hwdata = hw;
   250 	hw->fd = fd;
   251 	hw->path = strdup(path);
   252 	hw->type = BSDJOY_UHID;
   253 	{
   254 	    int ax;
   255 	    for (ax = 0; ax < JOYAXE_count; ax++)
   256 		hw->axis_map[ax] = -1;
   257 	}
   258 	hw->repdesc = hid_get_report_desc(fd);
   259 	if (hw->repdesc == NULL) {
   260 		SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
   261 		    strerror(errno));
   262 		goto usberr;
   263 	}
   264 
   265 	rep = &hw->inreport;
   266 	rep->rid = 0;
   267 	if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
   268 		goto usberr;
   269 	}
   270 	if (rep->size <= 0) {
   271 		SDL_SetError("%s: Input report descriptor has invalid length",
   272 		    hw->path);
   273 		goto usberr;
   274 	}
   275 
   276 #if defined(USBHID_NEW) || (defined(__FreeBSD__) && __FreeBSD_version >= 500111)
   277 	hdata = hid_start_parse(hw->repdesc, 1 << hid_input, rep->rid);
   278 #else
   279 	hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
   280 #endif
   281 	if (hdata == NULL) {
   282 		SDL_SetError("%s: Cannot start HID parser", hw->path);
   283 		goto usberr;
   284 	}
   285 	joy->naxes = 0;
   286 	joy->nbuttons = 0;
   287 	joy->nhats = 0;
   288 	joy->nballs = 0;
   289 
   290 	while (hid_get_item(hdata, &hitem) > 0) {
   291 		char *sp;
   292 		const char *s;
   293 
   294 		switch (hitem.kind) {
   295 		case hid_collection:
   296 			switch (HID_PAGE(hitem.usage)) {
   297 			case HUP_GENERIC_DESKTOP:
   298 				switch (HID_USAGE(hitem.usage)) {
   299 				case HUG_JOYSTICK:
   300 				case HUG_GAME_PAD:
   301 					s = hid_usage_in_page(hitem.usage);
   302 					sp = malloc(strlen(s) + 5);
   303 					sprintf(sp, "%s (%d)", s,
   304 					    joy->index);
   305 					joydevnames[joy->index] = sp;
   306 				}
   307 			}
   308 			break;
   309 		case hid_input:
   310 			switch (HID_PAGE(hitem.usage)) {
   311 			case HUP_GENERIC_DESKTOP: {
   312 			    unsigned usage = HID_USAGE(hitem.usage);
   313 			    int joyaxe = usage_to_joyaxe(usage);
   314 			    if (joyaxe >= 0) {
   315 				hw->axis_map[joyaxe] = joy->naxes;
   316 				joy->naxes++;
   317 			    } else if (usage == HUG_HAT_SWITCH) {
   318 				joy->nhats++;
   319 			    }
   320 			    break;
   321 			}
   322 			case HUP_BUTTON:
   323 				joy->nbuttons++;
   324 				break;
   325 			default:
   326 				break;
   327 			}
   328 			break;
   329 		default:
   330 			break;
   331 		}
   332 	}
   333 	hid_end_parse(hdata);
   334 
   335 	/* The poll blocks the event thread. */
   336 	fcntl(fd, F_SETFL, O_NONBLOCK);
   337 
   338 	return (0);
   339 usberr:
   340 	close(hw->fd);
   341 	free(hw->path);
   342 	free(hw);
   343 	return (-1);
   344 }
   345 
   346 void
   347 SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
   348 {
   349 	struct hid_item hitem;
   350 	struct hid_data *hdata;
   351 	struct report *rep;
   352 	int nbutton, naxe = -1;
   353 	Sint32 v;
   354 	
   355 	rep = &joy->hwdata->inreport;
   356 
   357 	if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
   358 		return;
   359 	}
   360 #if defined(USBHID_NEW) || (defined(__FreeBSD__) && __FreeBSD_version >= 500111)
   361 	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
   362 #else
   363 	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
   364 #endif
   365 	if (hdata == NULL) {
   366 		fprintf(stderr, "%s: Cannot start HID parser\n",
   367 		    joy->hwdata->path);
   368 		return;
   369 	}
   370 
   371 	for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
   372 		switch (hitem.kind) {
   373 		case hid_input:
   374 			switch (HID_PAGE(hitem.usage)) {
   375 			case HUP_GENERIC_DESKTOP: {
   376 			    unsigned usage = HID_USAGE(hitem.usage);
   377 			    int joyaxe = usage_to_joyaxe(usage);
   378 			    if (joyaxe >= 0) {
   379 				naxe = joy->hwdata->axis_map[joyaxe];
   380 				/* scaleaxe */
   381 				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
   382 							 &hitem);
   383 				v -= (hitem.logical_maximum + hitem.logical_minimum + 1)/2;
   384 				v *= 32768/((hitem.logical_maximum - hitem.logical_minimum + 1)/2);
   385 				if (v != joy->axes[naxe]) {
   386 				    SDL_PrivateJoystickAxis(joy, naxe, v);
   387 				}
   388 			    } else if (usage == HUG_HAT_SWITCH) {
   389 				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
   390 							 &hitem);
   391 				SDL_PrivateJoystickHat(joy, 0, hatval_to_sdl(v));
   392 			    }
   393 			    break;
   394 			}
   395 			case HUP_BUTTON:
   396 				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
   397 				    &hitem);
   398 				if (joy->buttons[nbutton] != v) {
   399 					SDL_PrivateJoystickButton(joy,
   400 					    nbutton, v);
   401 				}
   402 				nbutton++;
   403 				break;
   404 			default:
   405 				continue;
   406 			}
   407 			break;
   408 		default:
   409 			break;
   410 		}
   411 	}
   412 	hid_end_parse(hdata);
   413 
   414 	return;
   415 }
   416 
   417 /* Function to close a joystick after use */
   418 void
   419 SDL_SYS_JoystickClose(SDL_Joystick *joy)
   420 {
   421 	report_free(&joy->hwdata->inreport);
   422 	hid_dispose_report_desc(joy->hwdata->repdesc);
   423 	close(joy->hwdata->fd);
   424 	free(joy->hwdata->path);
   425 	free(joy->hwdata);
   426 
   427 	return;
   428 }
   429 
   430 void
   431 SDL_SYS_JoystickQuit(void)
   432 {
   433 	int i;
   434 
   435 	for (i = 0; i < MAX_JOYS; i++) {
   436 		if (joynames[i] != NULL)
   437 			free(joynames[i]);
   438 		if (joydevnames[i] != NULL)
   439 			free(joydevnames[i]);
   440 	}
   441 
   442 	return;
   443 }
   444 
   445 static int
   446 report_alloc(struct report *r, struct report_desc *rd, int repind)
   447 {
   448 	int len;
   449 
   450 #ifdef __FreeBSD__
   451 # if (__FreeBSD_version >= 460000)
   452 #  if (__FreeBSD_version <= 500111)
   453 	len = hid_report_size(rd, r->rid, repinfo[repind].kind);
   454 #  else
   455 	len = hid_report_size(rd, repinfo[repind].kind, r->rid);
   456 #  endif
   457 # else
   458 	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
   459 #endif
   460 #else
   461 # ifdef USBHID_NEW
   462 	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
   463 # else
   464 	len = hid_report_size(rd, repinfo[repind].kind, r->rid);
   465 # endif
   466 #endif
   467 
   468 	if (len < 0) {
   469 		SDL_SetError("Negative HID report size");
   470 		return (-1);
   471 	}
   472 	r->size = len;
   473 
   474 	if (r->size > 0) {
   475 		r->buf = malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) +
   476 		    r->size);
   477 		if (r->buf == NULL) {
   478 			SDL_OutOfMemory();
   479 			return (-1);
   480 		}
   481 	} else {
   482 		r->buf = NULL;
   483 	}
   484 
   485 	r->status = SREPORT_CLEAN;
   486 	return (0);
   487 }
   488 
   489 static void
   490 report_free(struct report *r)
   491 {
   492 	if (r->buf != NULL) {
   493 		free(r->buf);
   494 	}
   495 	r->status = SREPORT_UNINIT;
   496 }
   497