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