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