src/joystick/bsd/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 02 Jun 2003 14:50:22 +0000
changeset 632 85e104fe14c2
parent 611 0a2ef9e8ca41
child 715 de0351c47596
permissions -rw-r--r--
Date: Sun, 1 Jun 2003 15:38:45 -0700 (PDT)
From: Jeff Brown <jabrown@caida.org>
Subject: [patch] SDL-1.2.5 + FreeBSD joystick axes, hat fixes

Hello again! When I sent in some SDL fixes last December, I found out
they'd already been fixed in the CVS version. This time, I checked the
repository before bugging you. =)

I'm using SDL-1.2.5 on a FreeBSD 4.6.2-RELEASE system, and in the course
of getting my multi-analog-axis USB controller (with a hat switch!)
working with d2x-sdl -- the SDL port of the Descent 2 engine -- I came
across a few problems:

1) The second analog stick is reported as a slider in one direction, and
"Rz" in the other. SDL was ignoring the Rz axis, so I added Rx/Ry/Rz to
the set of things SDL considers to be axes.

2) After the above change, the set of JOYAXE_* axes for my gamepad was
{0,1,3,7}; however, d2x-sdl expects the axes to be contiguously numbered
from 0, which seems like a pretty reasonable expectation, rather than
having to scan the entire space of axes that SDL may or may not have.
So, I added a table lookup which maps the JOYAXE_* axis numbers to 0,1,...
in the order they're detected by SDL_SYS_JoystickOpen(), when reporting
them to the application. I also added a function "usage_to_joyaxe()"
which maps the USB HUG_* usage values to JOYAXE_values, since the repeated
case statements testing for HUG_* were getting out of hand.

3) The BSD joystick driver had no hat support, so I added it. It looks
like our USB library can only support one hat switch per device, which
makes life easy.

The patch against SDL-1.2.5 which implements these changes is at:

http://www.caida.org/~jabrown/patches/sdl-1.2.5-bsdhat.diff

After applying, SDL's "testjoystick" reports all activity from my gamepad
correctly, and d2x works too (though it needed some other fixes).

Moving on...

There is also a problem with slightly different USBHID library interfaces
on different versions of FreeBSD. I wasn't going to mention this since the
FreeBSD port for SDL-1.2.5 (and not SDL itself) was doing the FreeBSD
version-specific patching, so I e-mailed the port maintainer with this
change. However, I see that you've incorporated the FreeBSD
version-checking stuff into the CVS version of SDL, so now it's relevant
for you too.

The problem is, the FreeBSD #if tests don't work right for FreeBSD
4.6.2-RELEASE. There may be other versions with this problem, but I've
only tested 4.6.2-R. The following patch against your latest CVS version
fixes this:

--- SDL_sysjoystick.c-1.16 Tue Apr 15 09:02:08 2003
+++ SDL_sysjoystick.c Sun Jun 1 15:10:28 2003
@@ -420,6 +420,8 @@
# else
len = hid_report_size(rd, repinfo[repind].kind, r->rid);
# endif
+# elif (__FreeBSD_version == 460002)
+ len = hid_report_size(rd, r->rid, repinfo[repind].kind);
# else
len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
#endif


I hope this is all useful to you. I've been getting myself dizzy playing
Descent 2 with it, all morning!

-Jeff Brown


P.S. My USB controller is a Thrustmaster Firestorm Dual Analog 2. That's
probably irrelevant, but I threw it in for completeness.
slouken@278
     1
/*
slouken@278
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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@611
    60
#endif
slouken@611
    61
slouken@278
    62
#include "SDL_error.h"
slouken@278
    63
#include "SDL_joystick.h"
slouken@278
    64
#include "SDL_sysjoystick.h"
slouken@278
    65
#include "SDL_joystick_c.h"
slouken@278
    66
slouken@278
    67
#define MAX_UHID_JOYS	4
slouken@278
    68
#define MAX_JOY_JOYS	2
slouken@278
    69
#define MAX_JOYS	(MAX_UHID_JOYS + MAX_JOY_JOYS)
slouken@278
    70
slouken@278
    71
struct report {
slouken@278
    72
	struct	usb_ctl_report *buf;	/* Buffer */
slouken@278
    73
	size_t	size;			/* Buffer size */
slouken@278
    74
	int	rid;			/* Report ID */
slouken@278
    75
	enum {
slouken@278
    76
		SREPORT_UNINIT,
slouken@278
    77
		SREPORT_CLEAN,
slouken@278
    78
		SREPORT_DIRTY
slouken@278
    79
	} status;
slouken@278
    80
};
slouken@278
    81
slouken@278
    82
static struct {
slouken@278
    83
	int	uhid_report;
slouken@402
    84
	hid_kind_t kind;
slouken@278
    85
	const	char *name;
slouken@278
    86
} const repinfo[] = {
slouken@278
    87
	{ UHID_INPUT_REPORT,	hid_input,	"input" },
slouken@278
    88
	{ UHID_OUTPUT_REPORT,	hid_output,	"output" },
slouken@278
    89
	{ UHID_FEATURE_REPORT,	hid_feature,	"feature" }
slouken@278
    90
};
slouken@307
    91
slouken@307
    92
enum {
slouken@307
    93
	REPORT_INPUT = 0,
slouken@307
    94
	REPORT_OUTPUT = 1,
slouken@307
    95
	REPORT_FEATURE = 2
slouken@307
    96
};
slouken@307
    97
slouken@307
    98
enum {
slouken@307
    99
	JOYAXE_X,
slouken@307
   100
	JOYAXE_Y,
slouken@307
   101
	JOYAXE_Z,
slouken@307
   102
	JOYAXE_SLIDER,
slouken@632
   103
	JOYAXE_WHEEL,
slouken@632
   104
	JOYAXE_RX,
slouken@632
   105
	JOYAXE_RY,
slouken@632
   106
	JOYAXE_RZ,
slouken@632
   107
	JOYAXE_count
slouken@307
   108
};
slouken@278
   109
slouken@278
   110
struct joystick_hwdata {
slouken@278
   111
	int	fd;
slouken@278
   112
	char	*path;
slouken@278
   113
	enum {
slouken@278
   114
		BSDJOY_UHID,	/* uhid(4) */
slouken@278
   115
		BSDJOY_JOY	/* joy(4) */
slouken@278
   116
	} type;
slouken@278
   117
	struct	report_desc *repdesc;
slouken@278
   118
	struct	report inreport;
slouken@632
   119
	int	axis_map[JOYAXE_count];	/* map present JOYAXE_* to 0,1,..*/
slouken@278
   120
};
slouken@278
   121
slouken@278
   122
static char *joynames[MAX_JOYS];
slouken@278
   123
static char *joydevnames[MAX_JOYS];
slouken@278
   124
slouken@278
   125
static int	report_alloc(struct report *, struct report_desc *, int);
slouken@278
   126
static void	report_free(struct report *);
slouken@278
   127
slouken@381
   128
#ifdef USBHID_UCR_DATA
slouken@381
   129
#define REP_BUF_DATA(rep) ((rep)->buf->ucr_data)
slouken@381
   130
#else
slouken@381
   131
#define REP_BUF_DATA(rep) ((rep)->buf->data)
slouken@381
   132
#endif
slouken@381
   133
slouken@278
   134
int
slouken@278
   135
SDL_SYS_JoystickInit(void)
slouken@278
   136
{
slouken@503
   137
	char s[16];
slouken@278
   138
	int i, fd;
slouken@278
   139
slouken@278
   140
	SDL_numjoysticks = 0;
slouken@278
   141
slouken@278
   142
	memset(joynames, NULL, sizeof(joynames));
slouken@278
   143
	memset(joydevnames, NULL, sizeof(joydevnames));
slouken@278
   144
slouken@278
   145
	for (i = 0; i < MAX_UHID_JOYS; i++) {
slouken@544
   146
		SDL_Joystick nj;
slouken@544
   147
slouken@278
   148
		sprintf(s, "/dev/uhid%d", i);
slouken@544
   149
slouken@544
   150
		nj.index = SDL_numjoysticks;
slouken@544
   151
		joynames[nj.index] = strdup(s);
slouken@544
   152
slouken@544
   153
		if (SDL_SYS_JoystickOpen(&nj) == 0) {
slouken@544
   154
			SDL_SYS_JoystickClose(&nj);
slouken@544
   155
			SDL_numjoysticks++;
slouken@544
   156
		} else {
slouken@544
   157
			free(joynames[nj.index]);
slouken@278
   158
		}
slouken@278
   159
	}
slouken@278
   160
	for (i = 0; i < MAX_JOY_JOYS; i++) {
slouken@278
   161
		sprintf(s, "/dev/joy%d", i);
slouken@544
   162
		fd = open(s, O_RDONLY);
slouken@544
   163
		if (fd != -1) {
slouken@278
   164
			joynames[SDL_numjoysticks++] = strdup(s);
slouken@278
   165
			close(fd);
slouken@278
   166
		}
slouken@278
   167
	}
slouken@278
   168
slouken@278
   169
	/* Read the default USB HID usage table. */
slouken@278
   170
	hid_init(NULL);
slouken@278
   171
slouken@278
   172
	return (SDL_numjoysticks);
slouken@278
   173
}
slouken@278
   174
slouken@278
   175
const char *
slouken@278
   176
SDL_SYS_JoystickName(int index)
slouken@278
   177
{
slouken@278
   178
	if (joydevnames[index] != NULL) {
slouken@278
   179
		return (joydevnames[index]);
slouken@278
   180
	}
slouken@278
   181
	return (joynames[index]);
slouken@278
   182
}
slouken@278
   183
slouken@632
   184
static int
slouken@632
   185
usage_to_joyaxe(unsigned usage)
slouken@632
   186
{
slouken@632
   187
    int joyaxe;
slouken@632
   188
    switch (usage) {
slouken@632
   189
    case HUG_X:
slouken@632
   190
	joyaxe = JOYAXE_X; break;
slouken@632
   191
    case HUG_Y:
slouken@632
   192
	joyaxe = JOYAXE_Y; break;
slouken@632
   193
    case HUG_Z:
slouken@632
   194
	joyaxe = JOYAXE_Z; break;
slouken@632
   195
    case HUG_SLIDER:
slouken@632
   196
	joyaxe = JOYAXE_SLIDER; break;
slouken@632
   197
    case HUG_WHEEL:
slouken@632
   198
	joyaxe = JOYAXE_WHEEL; break;
slouken@632
   199
    case HUG_RX:
slouken@632
   200
	joyaxe = JOYAXE_RX; break;
slouken@632
   201
    case HUG_RY:
slouken@632
   202
	joyaxe = JOYAXE_RY; break;
slouken@632
   203
    case HUG_RZ:
slouken@632
   204
	joyaxe = JOYAXE_RZ; break;
slouken@632
   205
    default:
slouken@632
   206
	joyaxe = -1;
slouken@632
   207
    }
slouken@632
   208
    return joyaxe;    
slouken@632
   209
}
slouken@632
   210
slouken@632
   211
static unsigned
slouken@632
   212
hatval_to_sdl(Sint32 hatval)
slouken@632
   213
{
slouken@632
   214
    static const unsigned hat_dir_map[8] = {
slouken@632
   215
	SDL_HAT_UP, SDL_HAT_RIGHTUP, SDL_HAT_RIGHT, SDL_HAT_RIGHTDOWN, 
slouken@632
   216
	SDL_HAT_DOWN, SDL_HAT_LEFTDOWN, SDL_HAT_LEFT, SDL_HAT_LEFTUP
slouken@632
   217
    };
slouken@632
   218
    unsigned result;
slouken@632
   219
    if ((hatval & 7) == hatval) 
slouken@632
   220
	result = hat_dir_map[hatval];
slouken@632
   221
    else 
slouken@632
   222
	result = SDL_HAT_CENTERED;
slouken@632
   223
    return result;
slouken@632
   224
}
slouken@632
   225
slouken@632
   226
slouken@278
   227
int
slouken@278
   228
SDL_SYS_JoystickOpen(SDL_Joystick *joy)
slouken@278
   229
{
slouken@278
   230
	char *path = joynames[joy->index];
slouken@278
   231
	struct joystick_hwdata *hw;
slouken@278
   232
	struct hid_item hitem;
slouken@278
   233
	struct hid_data *hdata;
slouken@278
   234
	struct report *rep;
slouken@278
   235
	int fd;
slouken@278
   236
slouken@278
   237
	fd = open(path, O_RDWR);
slouken@544
   238
	if (fd == -1) {
slouken@278
   239
		SDL_SetError("%s: %s", path, strerror(errno));
slouken@278
   240
		return (-1);
slouken@278
   241
	}
slouken@278
   242
slouken@278
   243
	hw = (struct joystick_hwdata *)malloc(sizeof(struct joystick_hwdata));
slouken@278
   244
	if (hw == NULL) {
slouken@278
   245
		SDL_OutOfMemory();
slouken@278
   246
		close(fd);
slouken@278
   247
		return (-1);
slouken@278
   248
	}
slouken@278
   249
	joy->hwdata = hw;
slouken@278
   250
	hw->fd = fd;
slouken@278
   251
	hw->path = strdup(path);
slouken@278
   252
	hw->type = BSDJOY_UHID;
slouken@632
   253
	{
slouken@632
   254
	    int ax;
slouken@632
   255
	    for (ax = 0; ax < JOYAXE_count; ax++)
slouken@632
   256
		hw->axis_map[ax] = -1;
slouken@632
   257
	}
slouken@278
   258
	hw->repdesc = hid_get_report_desc(fd);
slouken@278
   259
	if (hw->repdesc == NULL) {
slouken@278
   260
		SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
slouken@278
   261
		    strerror(errno));
slouken@278
   262
		goto usberr;
slouken@278
   263
	}
slouken@278
   264
slouken@278
   265
	rep = &hw->inreport;
slouken@611
   266
	rep->rid = 0;
slouken@278
   267
	if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
slouken@278
   268
		goto usberr;
slouken@278
   269
	}
slouken@278
   270
	if (rep->size <= 0) {
slouken@307
   271
		SDL_SetError("%s: Input report descriptor has invalid length",
slouken@307
   272
		    hw->path);
slouken@278
   273
		goto usberr;
slouken@278
   274
	}
slouken@278
   275
slouken@611
   276
#if defined(USBHID_NEW) || (defined(__FreeBSD__) && __FreeBSD_version >= 500111)
slouken@407
   277
	hdata = hid_start_parse(hw->repdesc, 1 << hid_input, rep->rid);
slouken@407
   278
#else
slouken@278
   279
	hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
slouken@407
   280
#endif
slouken@278
   281
	if (hdata == NULL) {
slouken@278
   282
		SDL_SetError("%s: Cannot start HID parser", hw->path);
slouken@278
   283
		goto usberr;
slouken@278
   284
	}
slouken@278
   285
	joy->naxes = 0;
slouken@278
   286
	joy->nbuttons = 0;
slouken@278
   287
	joy->nhats = 0;
slouken@278
   288
	joy->nballs = 0;
slouken@278
   289
slouken@278
   290
	while (hid_get_item(hdata, &hitem) > 0) {
slouken@358
   291
		char *sp;
slouken@358
   292
		const char *s;
slouken@278
   293
slouken@278
   294
		switch (hitem.kind) {
slouken@278
   295
		case hid_collection:
slouken@278
   296
			switch (HID_PAGE(hitem.usage)) {
slouken@278
   297
			case HUP_GENERIC_DESKTOP:
slouken@278
   298
				switch (HID_USAGE(hitem.usage)) {
slouken@278
   299
				case HUG_JOYSTICK:
slouken@278
   300
				case HUG_GAME_PAD:
slouken@278
   301
					s = hid_usage_in_page(hitem.usage);
slouken@278
   302
					sp = malloc(strlen(s) + 5);
slouken@278
   303
					sprintf(sp, "%s (%d)", s,
slouken@278
   304
					    joy->index);
slouken@278
   305
					joydevnames[joy->index] = sp;
slouken@278
   306
				}
slouken@278
   307
			}
slouken@278
   308
			break;
slouken@278
   309
		case hid_input:
slouken@278
   310
			switch (HID_PAGE(hitem.usage)) {
slouken@632
   311
			case HUP_GENERIC_DESKTOP: {
slouken@632
   312
			    unsigned usage = HID_USAGE(hitem.usage);
slouken@632
   313
			    int joyaxe = usage_to_joyaxe(usage);
slouken@632
   314
			    if (joyaxe >= 0) {
slouken@632
   315
				hw->axis_map[joyaxe] = joy->naxes;
slouken@632
   316
				joy->naxes++;
slouken@632
   317
			    } else if (usage == HUG_HAT_SWITCH) {
slouken@632
   318
				joy->nhats++;
slouken@632
   319
			    }
slouken@632
   320
			    break;
slouken@632
   321
			}
slouken@278
   322
			case HUP_BUTTON:
slouken@278
   323
				joy->nbuttons++;
slouken@278
   324
				break;
slouken@405
   325
			default:
slouken@405
   326
				break;
slouken@278
   327
			}
slouken@278
   328
			break;
slouken@278
   329
		default:
slouken@278
   330
			break;
slouken@278
   331
		}
slouken@278
   332
	}
slouken@278
   333
	hid_end_parse(hdata);
slouken@278
   334
slouken@278
   335
	/* The poll blocks the event thread. */
slouken@278
   336
	fcntl(fd, F_SETFL, O_NONBLOCK);
slouken@278
   337
slouken@278
   338
	return (0);
slouken@278
   339
usberr:
slouken@278
   340
	close(hw->fd);
slouken@278
   341
	free(hw->path);
slouken@278
   342
	free(hw);
slouken@278
   343
	return (-1);
slouken@278
   344
}
slouken@278
   345
slouken@278
   346
void
slouken@278
   347
SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
slouken@278
   348
{
slouken@381
   349
	struct hid_item hitem;
slouken@381
   350
	struct hid_data *hdata;
slouken@381
   351
	struct report *rep;
slouken@358
   352
	int nbutton, naxe = -1;
slouken@307
   353
	Sint32 v;
slouken@278
   354
	
slouken@278
   355
	rep = &joy->hwdata->inreport;
slouken@381
   356
slouken@381
   357
	if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
slouken@278
   358
		return;
slouken@278
   359
	}
slouken@611
   360
#if defined(USBHID_NEW) || (defined(__FreeBSD__) && __FreeBSD_version >= 500111)
slouken@407
   361
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
slouken@407
   362
#else
slouken@278
   363
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
slouken@407
   364
#endif
slouken@278
   365
	if (hdata == NULL) {
slouken@278
   366
		fprintf(stderr, "%s: Cannot start HID parser\n",
slouken@278
   367
		    joy->hwdata->path);
slouken@278
   368
		return;
slouken@278
   369
	}
slouken@278
   370
slouken@307
   371
	for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
slouken@278
   372
		switch (hitem.kind) {
slouken@278
   373
		case hid_input:
slouken@278
   374
			switch (HID_PAGE(hitem.usage)) {
slouken@632
   375
			case HUP_GENERIC_DESKTOP: {
slouken@632
   376
			    unsigned usage = HID_USAGE(hitem.usage);
slouken@632
   377
			    int joyaxe = usage_to_joyaxe(usage);
slouken@632
   378
			    if (joyaxe >= 0) {
slouken@632
   379
				naxe = joy->hwdata->axis_map[joyaxe];
slouken@632
   380
				/* scaleaxe */
slouken@381
   381
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
slouken@632
   382
							 &hitem);
slouken@461
   383
				v -= (hitem.logical_maximum + hitem.logical_minimum + 1)/2;
slouken@461
   384
				v *= 32768/((hitem.logical_maximum - hitem.logical_minimum + 1)/2);
slouken@307
   385
				if (v != joy->axes[naxe]) {
slouken@632
   386
				    SDL_PrivateJoystickAxis(joy, naxe, v);
slouken@278
   387
				}
slouken@632
   388
			    } else if (usage == HUG_HAT_SWITCH) {
slouken@632
   389
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
slouken@632
   390
							 &hitem);
slouken@632
   391
				SDL_PrivateJoystickHat(joy, 0, hatval_to_sdl(v));
slouken@632
   392
			    }
slouken@632
   393
			    break;
slouken@632
   394
			}
slouken@278
   395
			case HUP_BUTTON:
slouken@381
   396
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
slouken@307
   397
				    &hitem);
slouken@278
   398
				if (joy->buttons[nbutton] != v) {
slouken@278
   399
					SDL_PrivateJoystickButton(joy,
slouken@278
   400
					    nbutton, v);
slouken@278
   401
				}
slouken@278
   402
				nbutton++;
slouken@278
   403
				break;
slouken@405
   404
			default:
slouken@405
   405
				continue;
slouken@278
   406
			}
slouken@278
   407
			break;
slouken@278
   408
		default:
slouken@278
   409
			break;
slouken@278
   410
		}
slouken@278
   411
	}
slouken@278
   412
	hid_end_parse(hdata);
slouken@278
   413
slouken@278
   414
	return;
slouken@278
   415
}
slouken@278
   416
slouken@278
   417
/* Function to close a joystick after use */
slouken@278
   418
void
slouken@278
   419
SDL_SYS_JoystickClose(SDL_Joystick *joy)
slouken@278
   420
{
slouken@278
   421
	report_free(&joy->hwdata->inreport);
slouken@278
   422
	hid_dispose_report_desc(joy->hwdata->repdesc);
slouken@278
   423
	close(joy->hwdata->fd);
slouken@278
   424
	free(joy->hwdata->path);
slouken@278
   425
	free(joy->hwdata);
slouken@278
   426
slouken@278
   427
	return;
slouken@278
   428
}
slouken@278
   429
slouken@278
   430
void
slouken@278
   431
SDL_SYS_JoystickQuit(void)
slouken@278
   432
{
slouken@278
   433
	int i;
slouken@278
   434
slouken@278
   435
	for (i = 0; i < MAX_JOYS; i++) {
slouken@278
   436
		if (joynames[i] != NULL)
slouken@278
   437
			free(joynames[i]);
slouken@278
   438
		if (joydevnames[i] != NULL)
slouken@278
   439
			free(joydevnames[i]);
slouken@278
   440
	}
slouken@278
   441
slouken@278
   442
	return;
slouken@278
   443
}
slouken@278
   444
slouken@278
   445
static int
slouken@278
   446
report_alloc(struct report *r, struct report_desc *rd, int repind)
slouken@278
   447
{
slouken@278
   448
	int len;
slouken@278
   449
slouken@552
   450
#ifdef __FreeBSD__
slouken@552
   451
# if (__FreeBSD_version >= 470000)
slouken@611
   452
#  if (__FreeBSD_version <= 500111)
slouken@552
   453
	len = hid_report_size(rd, r->rid, repinfo[repind].kind);
slouken@611
   454
#  else
slouken@611
   455
	len = hid_report_size(rd, repinfo[repind].kind, r->rid);
slouken@611
   456
#  endif
slouken@632
   457
# elif (__FreeBSD_version == 460002)
slouken@632
   458
	len = hid_report_size(rd, r->rid, repinfo[repind].kind);
slouken@552
   459
# else
icculus@426
   460
	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
slouken@407
   461
#endif
slouken@552
   462
#else
slouken@552
   463
# ifdef USBHID_NEW
slouken@552
   464
	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
slouken@552
   465
# else
slouken@552
   466
	len = hid_report_size(rd, repinfo[repind].kind, r->rid);
slouken@552
   467
# endif
slouken@552
   468
#endif
slouken@552
   469
slouken@278
   470
	if (len < 0) {
slouken@278
   471
		SDL_SetError("Negative HID report size");
slouken@278
   472
		return (-1);
slouken@278
   473
	}
slouken@278
   474
	r->size = len;
slouken@278
   475
slouken@278
   476
	if (r->size > 0) {
slouken@381
   477
		r->buf = malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) +
slouken@278
   478
		    r->size);
slouken@278
   479
		if (r->buf == NULL) {
slouken@278
   480
			SDL_OutOfMemory();
slouken@278
   481
			return (-1);
slouken@278
   482
		}
slouken@278
   483
	} else {
slouken@278
   484
		r->buf = NULL;
slouken@278
   485
	}
slouken@278
   486
slouken@278
   487
	r->status = SREPORT_CLEAN;
slouken@278
   488
	return (0);
slouken@278
   489
}
slouken@278
   490
slouken@278
   491
static void
slouken@278
   492
report_free(struct report *r)
slouken@278
   493
{
slouken@278
   494
	if (r->buf != NULL) {
slouken@278
   495
		free(r->buf);
slouken@278
   496
	}
slouken@278
   497
	r->status = SREPORT_UNINIT;
slouken@278
   498
}
slouken@278
   499