src/joystick/bsd/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 10 Oct 2009 09:38:02 +0000
branchSDL-1.2
changeset 4305 ecb22f65f0aa
parent 4159 a1b03ba2fcd0
child 4306 aa6aac8a5243
permissions -rw-r--r--
Adapted from 215_kfreebsd_gnu.diff
slouken@278
     1
/*
slouken@278
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 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@4022
   126
	int	x;
slouken@4022
   127
	int	y;
slouken@4022
   128
	int	xmin;
slouken@4022
   129
	int	ymin;
slouken@4022
   130
	int	xmax;
slouken@4022
   131
	int	ymax;
slouken@278
   132
};
slouken@278
   133
slouken@278
   134
static char *joynames[MAX_JOYS];
slouken@278
   135
static char *joydevnames[MAX_JOYS];
slouken@278
   136
slouken@278
   137
static int	report_alloc(struct report *, struct report_desc *, int);
slouken@278
   138
static void	report_free(struct report *);
slouken@278
   139
slouken@381
   140
#ifdef USBHID_UCR_DATA
slouken@381
   141
#define REP_BUF_DATA(rep) ((rep)->buf->ucr_data)
slouken@381
   142
#else
slouken@381
   143
#define REP_BUF_DATA(rep) ((rep)->buf->data)
slouken@381
   144
#endif
slouken@381
   145
slouken@278
   146
int
slouken@278
   147
SDL_SYS_JoystickInit(void)
slouken@278
   148
{
slouken@503
   149
	char s[16];
slouken@278
   150
	int i, fd;
slouken@278
   151
slouken@278
   152
	SDL_numjoysticks = 0;
slouken@278
   153
slouken@1336
   154
	SDL_memset(joynames, 0, sizeof(joynames));
slouken@1336
   155
	SDL_memset(joydevnames, 0, sizeof(joydevnames));
slouken@278
   156
slouken@278
   157
	for (i = 0; i < MAX_UHID_JOYS; i++) {
slouken@544
   158
		SDL_Joystick nj;
slouken@544
   159
slouken@1338
   160
		SDL_snprintf(s, SDL_arraysize(s), "/dev/uhid%d", i);
slouken@544
   161
slouken@544
   162
		nj.index = SDL_numjoysticks;
slouken@544
   163
		joynames[nj.index] = strdup(s);
slouken@544
   164
slouken@544
   165
		if (SDL_SYS_JoystickOpen(&nj) == 0) {
slouken@544
   166
			SDL_SYS_JoystickClose(&nj);
slouken@544
   167
			SDL_numjoysticks++;
slouken@544
   168
		} else {
slouken@1336
   169
			SDL_free(joynames[nj.index]);
slouken@965
   170
			joynames[nj.index] = NULL;
slouken@278
   171
		}
slouken@278
   172
	}
slouken@278
   173
	for (i = 0; i < MAX_JOY_JOYS; i++) {
slouken@1338
   174
		SDL_snprintf(s, SDL_arraysize(s), "/dev/joy%d", i);
slouken@544
   175
		fd = open(s, O_RDONLY);
slouken@544
   176
		if (fd != -1) {
slouken@278
   177
			joynames[SDL_numjoysticks++] = strdup(s);
slouken@278
   178
			close(fd);
slouken@278
   179
		}
slouken@278
   180
	}
slouken@278
   181
slouken@278
   182
	/* Read the default USB HID usage table. */
slouken@278
   183
	hid_init(NULL);
slouken@278
   184
slouken@278
   185
	return (SDL_numjoysticks);
slouken@278
   186
}
slouken@278
   187
slouken@278
   188
const char *
slouken@278
   189
SDL_SYS_JoystickName(int index)
slouken@278
   190
{
slouken@278
   191
	if (joydevnames[index] != NULL) {
slouken@278
   192
		return (joydevnames[index]);
slouken@278
   193
	}
slouken@278
   194
	return (joynames[index]);
slouken@278
   195
}
slouken@278
   196
slouken@632
   197
static int
slouken@632
   198
usage_to_joyaxe(unsigned usage)
slouken@632
   199
{
slouken@632
   200
    int joyaxe;
slouken@632
   201
    switch (usage) {
slouken@632
   202
    case HUG_X:
slouken@632
   203
	joyaxe = JOYAXE_X; break;
slouken@632
   204
    case HUG_Y:
slouken@632
   205
	joyaxe = JOYAXE_Y; break;
slouken@632
   206
    case HUG_Z:
slouken@632
   207
	joyaxe = JOYAXE_Z; break;
slouken@632
   208
    case HUG_SLIDER:
slouken@632
   209
	joyaxe = JOYAXE_SLIDER; break;
slouken@632
   210
    case HUG_WHEEL:
slouken@632
   211
	joyaxe = JOYAXE_WHEEL; break;
slouken@632
   212
    case HUG_RX:
slouken@632
   213
	joyaxe = JOYAXE_RX; break;
slouken@632
   214
    case HUG_RY:
slouken@632
   215
	joyaxe = JOYAXE_RY; break;
slouken@632
   216
    case HUG_RZ:
slouken@632
   217
	joyaxe = JOYAXE_RZ; break;
slouken@632
   218
    default:
slouken@632
   219
	joyaxe = -1;
slouken@632
   220
    }
slouken@632
   221
    return joyaxe;    
slouken@632
   222
}
slouken@632
   223
slouken@632
   224
static unsigned
slouken@632
   225
hatval_to_sdl(Sint32 hatval)
slouken@632
   226
{
slouken@632
   227
    static const unsigned hat_dir_map[8] = {
slouken@632
   228
	SDL_HAT_UP, SDL_HAT_RIGHTUP, SDL_HAT_RIGHT, SDL_HAT_RIGHTDOWN, 
slouken@632
   229
	SDL_HAT_DOWN, SDL_HAT_LEFTDOWN, SDL_HAT_LEFT, SDL_HAT_LEFTUP
slouken@632
   230
    };
slouken@632
   231
    unsigned result;
slouken@632
   232
    if ((hatval & 7) == hatval) 
slouken@632
   233
	result = hat_dir_map[hatval];
slouken@632
   234
    else 
slouken@632
   235
	result = SDL_HAT_CENTERED;
slouken@632
   236
    return result;
slouken@632
   237
}
slouken@632
   238
slouken@632
   239
slouken@278
   240
int
slouken@278
   241
SDL_SYS_JoystickOpen(SDL_Joystick *joy)
slouken@278
   242
{
slouken@278
   243
	char *path = joynames[joy->index];
slouken@278
   244
	struct joystick_hwdata *hw;
slouken@278
   245
	struct hid_item hitem;
slouken@278
   246
	struct hid_data *hdata;
slouken@278
   247
	struct report *rep;
slouken@278
   248
	int fd;
slouken@1565
   249
	int i;
slouken@278
   250
slouken@715
   251
	fd = open(path, O_RDONLY);
slouken@544
   252
	if (fd == -1) {
slouken@278
   253
		SDL_SetError("%s: %s", path, strerror(errno));
slouken@278
   254
		return (-1);
slouken@278
   255
	}
slouken@278
   256
slouken@1336
   257
	hw = (struct joystick_hwdata *)SDL_malloc(sizeof(struct joystick_hwdata));
slouken@278
   258
	if (hw == NULL) {
slouken@278
   259
		SDL_OutOfMemory();
slouken@278
   260
		close(fd);
slouken@278
   261
		return (-1);
slouken@278
   262
	}
slouken@278
   263
	joy->hwdata = hw;
slouken@278
   264
	hw->fd = fd;
slouken@278
   265
	hw->path = strdup(path);
slouken@4022
   266
	hw->x = 0;
slouken@4022
   267
	hw->y = 0;
slouken@4022
   268
	hw->xmin = 0xffff;
slouken@4022
   269
	hw->ymin = 0xffff;
slouken@4022
   270
	hw->xmax = 0;
slouken@4022
   271
	hw->ymax = 0;
slouken@1336
   272
	if (! SDL_strncmp(path, "/dev/joy", 8)) {
slouken@776
   273
		hw->type = BSDJOY_JOY;
slouken@776
   274
		joy->naxes = 2;
slouken@776
   275
		joy->nbuttons = 2;
slouken@776
   276
		joy->nhats = 0;
slouken@776
   277
		joy->nballs = 0;
slouken@776
   278
		joydevnames[joy->index] = strdup("Gameport joystick");
slouken@776
   279
		goto usbend;
slouken@776
   280
	} else {
slouken@776
   281
		hw->type = BSDJOY_UHID;
slouken@776
   282
	}
slouken@776
   283
slouken@632
   284
	{
slouken@632
   285
	    int ax;
slouken@632
   286
	    for (ax = 0; ax < JOYAXE_count; ax++)
slouken@632
   287
		hw->axis_map[ax] = -1;
slouken@632
   288
	}
slouken@278
   289
	hw->repdesc = hid_get_report_desc(fd);
slouken@278
   290
	if (hw->repdesc == NULL) {
slouken@278
   291
		SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
slouken@278
   292
		    strerror(errno));
slouken@278
   293
		goto usberr;
slouken@278
   294
	}
slouken@278
   295
slouken@278
   296
	rep = &hw->inreport;
slouken@1559
   297
	if (ioctl(fd, USB_GET_REPORT_ID, &rep->rid) < 0) {
slouken@1559
   298
		rep->rid = -1; /* XXX */
slouken@1559
   299
	}
slouken@278
   300
	if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
slouken@278
   301
		goto usberr;
slouken@278
   302
	}
slouken@278
   303
	if (rep->size <= 0) {
slouken@307
   304
		SDL_SetError("%s: Input report descriptor has invalid length",
slouken@307
   305
		    hw->path);
slouken@278
   306
		goto usberr;
slouken@278
   307
	}
slouken@278
   308
slouken@4305
   309
#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111)
slouken@407
   310
	hdata = hid_start_parse(hw->repdesc, 1 << hid_input, rep->rid);
slouken@407
   311
#else
slouken@278
   312
	hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
slouken@407
   313
#endif
slouken@278
   314
	if (hdata == NULL) {
slouken@278
   315
		SDL_SetError("%s: Cannot start HID parser", hw->path);
slouken@278
   316
		goto usberr;
slouken@278
   317
	}
slouken@278
   318
	joy->naxes = 0;
slouken@278
   319
	joy->nbuttons = 0;
slouken@278
   320
	joy->nhats = 0;
slouken@278
   321
	joy->nballs = 0;
slouken@1565
   322
	for (i=0; i<JOYAXE_count; i++)
slouken@1565
   323
		hw->axis_map[i] = -1;
slouken@278
   324
slouken@278
   325
	while (hid_get_item(hdata, &hitem) > 0) {
slouken@358
   326
		char *sp;
slouken@358
   327
		const char *s;
slouken@278
   328
slouken@278
   329
		switch (hitem.kind) {
slouken@278
   330
		case hid_collection:
slouken@278
   331
			switch (HID_PAGE(hitem.usage)) {
slouken@278
   332
			case HUP_GENERIC_DESKTOP:
slouken@278
   333
				switch (HID_USAGE(hitem.usage)) {
slouken@278
   334
				case HUG_JOYSTICK:
slouken@278
   335
				case HUG_GAME_PAD:
slouken@278
   336
					s = hid_usage_in_page(hitem.usage);
slouken@1336
   337
					sp = SDL_malloc(SDL_strlen(s) + 5);
slouken@1338
   338
					SDL_snprintf(sp, SDL_strlen(s) + 5, "%s (%d)", s,
slouken@278
   339
					    joy->index);
slouken@278
   340
					joydevnames[joy->index] = sp;
slouken@278
   341
				}
slouken@278
   342
			}
slouken@278
   343
			break;
slouken@278
   344
		case hid_input:
slouken@278
   345
			switch (HID_PAGE(hitem.usage)) {
slouken@632
   346
			case HUP_GENERIC_DESKTOP: {
slouken@632
   347
			    unsigned usage = HID_USAGE(hitem.usage);
slouken@632
   348
			    int joyaxe = usage_to_joyaxe(usage);
slouken@632
   349
			    if (joyaxe >= 0) {
slouken@1565
   350
				hw->axis_map[joyaxe] = 1;
slouken@632
   351
			    } else if (usage == HUG_HAT_SWITCH) {
slouken@632
   352
				joy->nhats++;
slouken@632
   353
			    }
slouken@632
   354
			    break;
slouken@632
   355
			}
slouken@278
   356
			case HUP_BUTTON:
slouken@278
   357
				joy->nbuttons++;
slouken@278
   358
				break;
slouken@405
   359
			default:
slouken@405
   360
				break;
slouken@278
   361
			}
slouken@278
   362
			break;
slouken@278
   363
		default:
slouken@278
   364
			break;
slouken@278
   365
		}
slouken@278
   366
	}
slouken@278
   367
	hid_end_parse(hdata);
slouken@1565
   368
	for (i=0; i<JOYAXE_count; i++)
slouken@1565
   369
		if (hw->axis_map[i] > 0)
slouken@1565
   370
			hw->axis_map[i] = joy->naxes++;
slouken@278
   371
slouken@776
   372
usbend:
slouken@278
   373
	/* The poll blocks the event thread. */
slouken@278
   374
	fcntl(fd, F_SETFL, O_NONBLOCK);
slouken@278
   375
slouken@278
   376
	return (0);
slouken@278
   377
usberr:
slouken@278
   378
	close(hw->fd);
slouken@1336
   379
	SDL_free(hw->path);
slouken@1336
   380
	SDL_free(hw);
slouken@278
   381
	return (-1);
slouken@278
   382
}
slouken@278
   383
slouken@278
   384
void
slouken@278
   385
SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
slouken@278
   386
{
slouken@381
   387
	struct hid_item hitem;
slouken@381
   388
	struct hid_data *hdata;
slouken@381
   389
	struct report *rep;
slouken@358
   390
	int nbutton, naxe = -1;
slouken@307
   391
	Sint32 v;
slouken@776
   392
slouken@1565
   393
#if defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H
slouken@776
   394
	struct joystick gameport;
slouken@776
   395
 
slouken@776
   396
	if (joy->hwdata->type == BSDJOY_JOY) {
slouken@776
   397
		if (read(joy->hwdata->fd, &gameport, sizeof gameport) != sizeof gameport)
slouken@776
   398
			return;
slouken@4022
   399
		if (abs(joy->hwdata->x - gameport.x) > 8) {
slouken@4022
   400
			joy->hwdata->x = gameport.x;
slouken@4022
   401
			if (joy->hwdata->x < joy->hwdata->xmin) {
slouken@4022
   402
				joy->hwdata->xmin = joy->hwdata->x;
slouken@776
   403
			}
slouken@4022
   404
			if (joy->hwdata->x > joy->hwdata->xmax) {
slouken@4022
   405
				joy->hwdata->xmax = joy->hwdata->x;
slouken@776
   406
			}
slouken@4022
   407
			if (joy->hwdata->xmin == joy->hwdata->xmax) {
slouken@4022
   408
				joy->hwdata->xmin--;
slouken@4022
   409
				joy->hwdata->xmax++;
slouken@776
   410
			}
slouken@4022
   411
			v = (Sint32)joy->hwdata->x;
slouken@4022
   412
			v -= (joy->hwdata->xmax + joy->hwdata->xmin + 1)/2;
slouken@4022
   413
			v *= 32768/((joy->hwdata->xmax - joy->hwdata->xmin + 1)/2);
slouken@776
   414
			SDL_PrivateJoystickAxis(joy, 0, v);
slouken@776
   415
		}
slouken@4022
   416
		if (abs(joy->hwdata->y - gameport.y) > 8) {
slouken@4022
   417
			joy->hwdata->y = gameport.y;
slouken@4022
   418
			if (joy->hwdata->y < joy->hwdata->ymin) {
slouken@4022
   419
				joy->hwdata->ymin = joy->hwdata->y;
slouken@776
   420
			}
slouken@4022
   421
			if (joy->hwdata->y > joy->hwdata->ymax) {
slouken@4022
   422
				joy->hwdata->ymax = joy->hwdata->y;
slouken@776
   423
			}
slouken@4022
   424
			if (joy->hwdata->ymin == joy->hwdata->ymax) {
slouken@4022
   425
				joy->hwdata->ymin--;
slouken@4022
   426
				joy->hwdata->ymax++;
slouken@776
   427
			}
slouken@4022
   428
			v = (Sint32)joy->hwdata->y;
slouken@4022
   429
			v -= (joy->hwdata->ymax + joy->hwdata->ymin + 1)/2;
slouken@4022
   430
			v *= 32768/((joy->hwdata->ymax - joy->hwdata->ymin + 1)/2);
slouken@776
   431
			SDL_PrivateJoystickAxis(joy, 1, v);
slouken@776
   432
		}
slouken@776
   433
		if (gameport.b1 != joy->buttons[0]) {
slouken@776
   434
			SDL_PrivateJoystickButton(joy, 0, gameport.b1);
slouken@776
   435
		}
slouken@776
   436
		if (gameport.b2 != joy->buttons[1]) {
slouken@776
   437
			SDL_PrivateJoystickButton(joy, 1, gameport.b2);
slouken@776
   438
		}
slouken@776
   439
		return;
slouken@776
   440
	}
slouken@1565
   441
#endif /* defined(__FREEBSD__) || SDL_JOYSTICK_USBHID_MACHINE_JOYSTICK_H */
slouken@278
   442
	
slouken@278
   443
	rep = &joy->hwdata->inreport;
slouken@381
   444
slouken@381
   445
	if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
slouken@278
   446
		return;
slouken@278
   447
	}
slouken@4305
   448
#if defined(USBHID_NEW) || (defined(__FREEBSD__) && __FreeBSD_kernel_version >= 500111)
slouken@407
   449
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
slouken@407
   450
#else
slouken@278
   451
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
slouken@407
   452
#endif
slouken@278
   453
	if (hdata == NULL) {
slouken@278
   454
		fprintf(stderr, "%s: Cannot start HID parser\n",
slouken@278
   455
		    joy->hwdata->path);
slouken@278
   456
		return;
slouken@278
   457
	}
slouken@278
   458
slouken@307
   459
	for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
slouken@278
   460
		switch (hitem.kind) {
slouken@278
   461
		case hid_input:
slouken@278
   462
			switch (HID_PAGE(hitem.usage)) {
slouken@632
   463
			case HUP_GENERIC_DESKTOP: {
slouken@632
   464
			    unsigned usage = HID_USAGE(hitem.usage);
slouken@632
   465
			    int joyaxe = usage_to_joyaxe(usage);
slouken@632
   466
			    if (joyaxe >= 0) {
slouken@632
   467
				naxe = joy->hwdata->axis_map[joyaxe];
slouken@632
   468
				/* scaleaxe */
slouken@381
   469
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
slouken@632
   470
							 &hitem);
slouken@461
   471
				v -= (hitem.logical_maximum + hitem.logical_minimum + 1)/2;
slouken@461
   472
				v *= 32768/((hitem.logical_maximum - hitem.logical_minimum + 1)/2);
slouken@307
   473
				if (v != joy->axes[naxe]) {
slouken@632
   474
				    SDL_PrivateJoystickAxis(joy, naxe, v);
slouken@278
   475
				}
slouken@632
   476
			    } else if (usage == HUG_HAT_SWITCH) {
slouken@632
   477
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
slouken@632
   478
							 &hitem);
slouken@1565
   479
				SDL_PrivateJoystickHat(joy, 0,
slouken@1565
   480
					hatval_to_sdl(v)-hitem.logical_minimum);
slouken@632
   481
			    }
slouken@632
   482
			    break;
slouken@632
   483
			}
slouken@278
   484
			case HUP_BUTTON:
slouken@381
   485
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
slouken@307
   486
				    &hitem);
slouken@278
   487
				if (joy->buttons[nbutton] != v) {
slouken@278
   488
					SDL_PrivateJoystickButton(joy,
slouken@278
   489
					    nbutton, v);
slouken@278
   490
				}
slouken@278
   491
				nbutton++;
slouken@278
   492
				break;
slouken@405
   493
			default:
slouken@405
   494
				continue;
slouken@278
   495
			}
slouken@278
   496
			break;
slouken@278
   497
		default:
slouken@278
   498
			break;
slouken@278
   499
		}
slouken@278
   500
	}
slouken@278
   501
	hid_end_parse(hdata);
slouken@278
   502
slouken@278
   503
	return;
slouken@278
   504
}
slouken@278
   505
slouken@278
   506
/* Function to close a joystick after use */
slouken@278
   507
void
slouken@278
   508
SDL_SYS_JoystickClose(SDL_Joystick *joy)
slouken@278
   509
{
slouken@1336
   510
	if (SDL_strncmp(joy->hwdata->path, "/dev/joy", 8))	{
slouken@776
   511
		report_free(&joy->hwdata->inreport);
slouken@776
   512
		hid_dispose_report_desc(joy->hwdata->repdesc);
slouken@776
   513
	}
slouken@278
   514
	close(joy->hwdata->fd);
slouken@1336
   515
	SDL_free(joy->hwdata->path);
slouken@1336
   516
	SDL_free(joy->hwdata);
slouken@278
   517
slouken@278
   518
	return;
slouken@278
   519
}
slouken@278
   520
slouken@278
   521
void
slouken@278
   522
SDL_SYS_JoystickQuit(void)
slouken@278
   523
{
slouken@278
   524
	int i;
slouken@278
   525
slouken@278
   526
	for (i = 0; i < MAX_JOYS; i++) {
slouken@278
   527
		if (joynames[i] != NULL)
slouken@1336
   528
			SDL_free(joynames[i]);
slouken@278
   529
		if (joydevnames[i] != NULL)
slouken@1336
   530
			SDL_free(joydevnames[i]);
slouken@278
   531
	}
slouken@278
   532
slouken@278
   533
	return;
slouken@278
   534
}
slouken@278
   535
slouken@278
   536
static int
slouken@278
   537
report_alloc(struct report *r, struct report_desc *rd, int repind)
slouken@278
   538
{
slouken@278
   539
	int len;
slouken@278
   540
slouken@1565
   541
#ifdef __DragonFly__
slouken@1565
   542
	len = hid_report_size(rd, r->rid, repinfo[repind].kind);
slouken@1565
   543
#elif __FREEBSD__
slouken@4305
   544
# if (__FreeBSD_kernel_version >= 460000) || defined(__FreeBSD_kernel__)
slouken@4305
   545
#  if (__FreeBSD_kernel_version <= 500111)
slouken@552
   546
	len = hid_report_size(rd, r->rid, repinfo[repind].kind);
slouken@611
   547
#  else
slouken@611
   548
	len = hid_report_size(rd, repinfo[repind].kind, r->rid);
slouken@611
   549
#  endif
slouken@552
   550
# else
icculus@426
   551
	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
slouken@1565
   552
# endif
slouken@552
   553
#else
slouken@552
   554
# ifdef USBHID_NEW
slouken@1565
   555
	len = hid_report_size(rd, repinfo[repind].kind, r->rid);
slouken@1565
   556
# else
slouken@552
   557
	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
slouken@552
   558
# endif
slouken@552
   559
#endif
slouken@552
   560
slouken@278
   561
	if (len < 0) {
slouken@278
   562
		SDL_SetError("Negative HID report size");
slouken@278
   563
		return (-1);
slouken@278
   564
	}
slouken@278
   565
	r->size = len;
slouken@278
   566
slouken@278
   567
	if (r->size > 0) {
slouken@1336
   568
		r->buf = SDL_malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) +
slouken@278
   569
		    r->size);
slouken@278
   570
		if (r->buf == NULL) {
slouken@278
   571
			SDL_OutOfMemory();
slouken@278
   572
			return (-1);
slouken@278
   573
		}
slouken@278
   574
	} else {
slouken@278
   575
		r->buf = NULL;
slouken@278
   576
	}
slouken@278
   577
slouken@278
   578
	r->status = SREPORT_CLEAN;
slouken@278
   579
	return (0);
slouken@278
   580
}
slouken@278
   581
slouken@278
   582
static void
slouken@278
   583
report_free(struct report *r)
slouken@278
   584
{
slouken@278
   585
	if (r->buf != NULL) {
slouken@1336
   586
		SDL_free(r->buf);
slouken@278
   587
	}
slouken@278
   588
	r->status = SREPORT_UNINIT;
slouken@278
   589
}
slouken@278
   590
slouken@1635
   591
#endif /* SDL_JOYSTICK_USBHID */