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