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