src/joystick/bsd/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Apr 2003 16:02:08 +0000
changeset 611 0a2ef9e8ca41
parent 552 639d58d32471
child 632 85e104fe14c2
permissions -rw-r--r--
Date: Wed, 9 Apr 2003 01:03:25 -0400 (EDT)
From: "Matthew N. Dodd"
Subject: SDL patch: FreeBSD joystick support.

This patch deals with the recent changes in FreeBSD.

We're making an effort not to diverge our libusbhid from NetBSD's.
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@307
   103
	JOYAXE_WHEEL
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@307
   115
#if 0
slouken@307
   116
	int	axismin[];
slouken@307
   117
	int	axismax[];
slouken@307
   118
#endif
slouken@278
   119
};
slouken@278
   120
slouken@278
   121
static char *joynames[MAX_JOYS];
slouken@278
   122
static char *joydevnames[MAX_JOYS];
slouken@278
   123
slouken@278
   124
static int	report_alloc(struct report *, struct report_desc *, int);
slouken@278
   125
static void	report_free(struct report *);
slouken@278
   126
slouken@381
   127
#ifdef USBHID_UCR_DATA
slouken@381
   128
#define REP_BUF_DATA(rep) ((rep)->buf->ucr_data)
slouken@381
   129
#else
slouken@381
   130
#define REP_BUF_DATA(rep) ((rep)->buf->data)
slouken@381
   131
#endif
slouken@381
   132
slouken@278
   133
int
slouken@278
   134
SDL_SYS_JoystickInit(void)
slouken@278
   135
{
slouken@503
   136
	char s[16];
slouken@278
   137
	int i, fd;
slouken@278
   138
slouken@278
   139
	SDL_numjoysticks = 0;
slouken@278
   140
slouken@278
   141
	memset(joynames, NULL, sizeof(joynames));
slouken@278
   142
	memset(joydevnames, NULL, sizeof(joydevnames));
slouken@278
   143
slouken@278
   144
	for (i = 0; i < MAX_UHID_JOYS; i++) {
slouken@544
   145
		SDL_Joystick nj;
slouken@544
   146
slouken@278
   147
		sprintf(s, "/dev/uhid%d", i);
slouken@544
   148
slouken@544
   149
		nj.index = SDL_numjoysticks;
slouken@544
   150
		joynames[nj.index] = strdup(s);
slouken@544
   151
slouken@544
   152
		if (SDL_SYS_JoystickOpen(&nj) == 0) {
slouken@544
   153
			SDL_SYS_JoystickClose(&nj);
slouken@544
   154
			SDL_numjoysticks++;
slouken@544
   155
		} else {
slouken@544
   156
			free(joynames[nj.index]);
slouken@278
   157
		}
slouken@278
   158
	}
slouken@278
   159
	for (i = 0; i < MAX_JOY_JOYS; i++) {
slouken@278
   160
		sprintf(s, "/dev/joy%d", i);
slouken@544
   161
		fd = open(s, O_RDONLY);
slouken@544
   162
		if (fd != -1) {
slouken@278
   163
			joynames[SDL_numjoysticks++] = strdup(s);
slouken@278
   164
			close(fd);
slouken@278
   165
		}
slouken@278
   166
	}
slouken@278
   167
slouken@278
   168
	/* Read the default USB HID usage table. */
slouken@278
   169
	hid_init(NULL);
slouken@278
   170
slouken@278
   171
	return (SDL_numjoysticks);
slouken@278
   172
}
slouken@278
   173
slouken@278
   174
const char *
slouken@278
   175
SDL_SYS_JoystickName(int index)
slouken@278
   176
{
slouken@278
   177
	if (joydevnames[index] != NULL) {
slouken@278
   178
		return (joydevnames[index]);
slouken@278
   179
	}
slouken@278
   180
	return (joynames[index]);
slouken@278
   181
}
slouken@278
   182
slouken@278
   183
int
slouken@278
   184
SDL_SYS_JoystickOpen(SDL_Joystick *joy)
slouken@278
   185
{
slouken@278
   186
	char *path = joynames[joy->index];
slouken@278
   187
	struct joystick_hwdata *hw;
slouken@278
   188
	struct hid_item hitem;
slouken@278
   189
	struct hid_data *hdata;
slouken@278
   190
	struct report *rep;
slouken@278
   191
	int fd;
slouken@278
   192
slouken@278
   193
	fd = open(path, O_RDWR);
slouken@544
   194
	if (fd == -1) {
slouken@278
   195
		SDL_SetError("%s: %s", path, strerror(errno));
slouken@278
   196
		return (-1);
slouken@278
   197
	}
slouken@278
   198
slouken@278
   199
	hw = (struct joystick_hwdata *)malloc(sizeof(struct joystick_hwdata));
slouken@278
   200
	if (hw == NULL) {
slouken@278
   201
		SDL_OutOfMemory();
slouken@278
   202
		close(fd);
slouken@278
   203
		return (-1);
slouken@278
   204
	}
slouken@278
   205
	joy->hwdata = hw;
slouken@278
   206
	hw->fd = fd;
slouken@278
   207
	hw->path = strdup(path);
slouken@278
   208
	hw->type = BSDJOY_UHID;
slouken@278
   209
	hw->repdesc = hid_get_report_desc(fd);
slouken@278
   210
	if (hw->repdesc == NULL) {
slouken@278
   211
		SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
slouken@278
   212
		    strerror(errno));
slouken@278
   213
		goto usberr;
slouken@278
   214
	}
slouken@278
   215
slouken@278
   216
	rep = &hw->inreport;
slouken@611
   217
	rep->rid = 0;
slouken@278
   218
	if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
slouken@278
   219
		goto usberr;
slouken@278
   220
	}
slouken@278
   221
	if (rep->size <= 0) {
slouken@307
   222
		SDL_SetError("%s: Input report descriptor has invalid length",
slouken@307
   223
		    hw->path);
slouken@278
   224
		goto usberr;
slouken@278
   225
	}
slouken@278
   226
slouken@611
   227
#if defined(USBHID_NEW) || (defined(__FreeBSD__) && __FreeBSD_version >= 500111)
slouken@407
   228
	hdata = hid_start_parse(hw->repdesc, 1 << hid_input, rep->rid);
slouken@407
   229
#else
slouken@278
   230
	hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
slouken@407
   231
#endif
slouken@278
   232
	if (hdata == NULL) {
slouken@278
   233
		SDL_SetError("%s: Cannot start HID parser", hw->path);
slouken@278
   234
		goto usberr;
slouken@278
   235
	}
slouken@278
   236
	joy->naxes = 0;
slouken@278
   237
	joy->nbuttons = 0;
slouken@278
   238
	joy->nhats = 0;
slouken@278
   239
	joy->nballs = 0;
slouken@278
   240
slouken@278
   241
	while (hid_get_item(hdata, &hitem) > 0) {
slouken@358
   242
		char *sp;
slouken@358
   243
		const char *s;
slouken@278
   244
slouken@278
   245
		switch (hitem.kind) {
slouken@278
   246
		case hid_collection:
slouken@278
   247
			switch (HID_PAGE(hitem.usage)) {
slouken@278
   248
			case HUP_GENERIC_DESKTOP:
slouken@278
   249
				switch (HID_USAGE(hitem.usage)) {
slouken@278
   250
				case HUG_JOYSTICK:
slouken@278
   251
				case HUG_GAME_PAD:
slouken@278
   252
					s = hid_usage_in_page(hitem.usage);
slouken@278
   253
					sp = malloc(strlen(s) + 5);
slouken@278
   254
					sprintf(sp, "%s (%d)", s,
slouken@278
   255
					    joy->index);
slouken@278
   256
					joydevnames[joy->index] = sp;
slouken@278
   257
				}
slouken@278
   258
			}
slouken@278
   259
			break;
slouken@278
   260
		case hid_input:
slouken@278
   261
			switch (HID_PAGE(hitem.usage)) {
slouken@278
   262
			case HUP_GENERIC_DESKTOP:
slouken@278
   263
				switch (HID_USAGE(hitem.usage)) {
slouken@278
   264
				case HUG_X:
slouken@278
   265
				case HUG_Y:
slouken@278
   266
				case HUG_Z:
slouken@307
   267
				case HUG_SLIDER:
slouken@307
   268
				case HUG_WHEEL:
slouken@307
   269
#if 0
slouken@278
   270
					hw->axismin[joy->naxes] =
slouken@278
   271
					    hitem.logical_minimum;
slouken@278
   272
					hw->axismax[joy->naxes] =
slouken@278
   273
					    hitem.logical_maximum;
slouken@307
   274
#endif
slouken@278
   275
					joy->naxes++;
slouken@278
   276
					break;
slouken@278
   277
				}
slouken@278
   278
				break;
slouken@278
   279
			case HUP_BUTTON:
slouken@278
   280
				joy->nbuttons++;
slouken@278
   281
				break;
slouken@405
   282
			default:
slouken@405
   283
				break;
slouken@278
   284
			}
slouken@278
   285
			break;
slouken@278
   286
		default:
slouken@278
   287
			break;
slouken@278
   288
		}
slouken@278
   289
	}
slouken@278
   290
	hid_end_parse(hdata);
slouken@278
   291
slouken@278
   292
	/* The poll blocks the event thread. */
slouken@278
   293
	fcntl(fd, F_SETFL, O_NONBLOCK);
slouken@278
   294
slouken@278
   295
	return (0);
slouken@278
   296
usberr:
slouken@278
   297
	close(hw->fd);
slouken@278
   298
	free(hw->path);
slouken@278
   299
	free(hw);
slouken@278
   300
	return (-1);
slouken@278
   301
}
slouken@278
   302
slouken@278
   303
void
slouken@278
   304
SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
slouken@278
   305
{
slouken@381
   306
	struct hid_item hitem;
slouken@381
   307
	struct hid_data *hdata;
slouken@381
   308
	struct report *rep;
slouken@358
   309
	int nbutton, naxe = -1;
slouken@307
   310
	Sint32 v;
slouken@278
   311
	
slouken@278
   312
	rep = &joy->hwdata->inreport;
slouken@381
   313
slouken@381
   314
	if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
slouken@278
   315
		return;
slouken@278
   316
	}
slouken@611
   317
#if defined(USBHID_NEW) || (defined(__FreeBSD__) && __FreeBSD_version >= 500111)
slouken@407
   318
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
slouken@407
   319
#else
slouken@278
   320
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
slouken@407
   321
#endif
slouken@278
   322
	if (hdata == NULL) {
slouken@278
   323
		fprintf(stderr, "%s: Cannot start HID parser\n",
slouken@278
   324
		    joy->hwdata->path);
slouken@278
   325
		return;
slouken@278
   326
	}
slouken@278
   327
slouken@307
   328
	for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
slouken@278
   329
		switch (hitem.kind) {
slouken@278
   330
		case hid_input:
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_X:
slouken@307
   335
					naxe = JOYAXE_X;
slouken@307
   336
					goto scaleaxe;
slouken@278
   337
				case HUG_Y:
slouken@307
   338
					naxe = JOYAXE_Y;
slouken@307
   339
					goto scaleaxe;
slouken@278
   340
				case HUG_Z:
slouken@307
   341
					naxe = JOYAXE_Z;
slouken@307
   342
					goto scaleaxe;
slouken@307
   343
				case HUG_SLIDER:
slouken@307
   344
					naxe = JOYAXE_SLIDER;
slouken@307
   345
					goto scaleaxe;
slouken@307
   346
				case HUG_WHEEL:
slouken@307
   347
					naxe = JOYAXE_WHEEL;
slouken@307
   348
					goto scaleaxe;
slouken@461
   349
				default:
slouken@461
   350
					continue;
slouken@307
   351
				}
slouken@307
   352
scaleaxe:
slouken@381
   353
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
slouken@381
   354
				    &hitem);
slouken@461
   355
				v -= (hitem.logical_maximum + hitem.logical_minimum + 1)/2;
slouken@461
   356
				v *= 32768/((hitem.logical_maximum - hitem.logical_minimum + 1)/2);
slouken@307
   357
				if (v != joy->axes[naxe]) {
slouken@307
   358
					SDL_PrivateJoystickAxis(joy, naxe, v);
slouken@278
   359
				}
slouken@278
   360
				break;
slouken@278
   361
			case HUP_BUTTON:
slouken@381
   362
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
slouken@307
   363
				    &hitem);
slouken@278
   364
				if (joy->buttons[nbutton] != v) {
slouken@278
   365
					SDL_PrivateJoystickButton(joy,
slouken@278
   366
					    nbutton, v);
slouken@278
   367
				}
slouken@278
   368
				nbutton++;
slouken@278
   369
				break;
slouken@405
   370
			default:
slouken@405
   371
				continue;
slouken@278
   372
			}
slouken@278
   373
			break;
slouken@278
   374
		default:
slouken@278
   375
			break;
slouken@278
   376
		}
slouken@278
   377
	}
slouken@278
   378
	hid_end_parse(hdata);
slouken@278
   379
slouken@278
   380
	return;
slouken@278
   381
}
slouken@278
   382
slouken@278
   383
/* Function to close a joystick after use */
slouken@278
   384
void
slouken@278
   385
SDL_SYS_JoystickClose(SDL_Joystick *joy)
slouken@278
   386
{
slouken@278
   387
	report_free(&joy->hwdata->inreport);
slouken@278
   388
	hid_dispose_report_desc(joy->hwdata->repdesc);
slouken@278
   389
	close(joy->hwdata->fd);
slouken@278
   390
	free(joy->hwdata->path);
slouken@278
   391
	free(joy->hwdata);
slouken@278
   392
slouken@278
   393
	return;
slouken@278
   394
}
slouken@278
   395
slouken@278
   396
void
slouken@278
   397
SDL_SYS_JoystickQuit(void)
slouken@278
   398
{
slouken@278
   399
	int i;
slouken@278
   400
slouken@278
   401
	for (i = 0; i < MAX_JOYS; i++) {
slouken@278
   402
		if (joynames[i] != NULL)
slouken@278
   403
			free(joynames[i]);
slouken@278
   404
		if (joydevnames[i] != NULL)
slouken@278
   405
			free(joydevnames[i]);
slouken@278
   406
	}
slouken@278
   407
slouken@278
   408
	return;
slouken@278
   409
}
slouken@278
   410
slouken@278
   411
static int
slouken@278
   412
report_alloc(struct report *r, struct report_desc *rd, int repind)
slouken@278
   413
{
slouken@278
   414
	int len;
slouken@278
   415
slouken@552
   416
#ifdef __FreeBSD__
slouken@552
   417
# if (__FreeBSD_version >= 470000)
slouken@611
   418
#  if (__FreeBSD_version <= 500111)
slouken@552
   419
	len = hid_report_size(rd, r->rid, repinfo[repind].kind);
slouken@611
   420
#  else
slouken@611
   421
	len = hid_report_size(rd, repinfo[repind].kind, r->rid);
slouken@611
   422
#  endif
slouken@552
   423
# else
icculus@426
   424
	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
slouken@407
   425
#endif
slouken@552
   426
#else
slouken@552
   427
# ifdef USBHID_NEW
slouken@552
   428
	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
slouken@552
   429
# else
slouken@552
   430
	len = hid_report_size(rd, repinfo[repind].kind, r->rid);
slouken@552
   431
# endif
slouken@552
   432
#endif
slouken@552
   433
slouken@278
   434
	if (len < 0) {
slouken@278
   435
		SDL_SetError("Negative HID report size");
slouken@278
   436
		return (-1);
slouken@278
   437
	}
slouken@278
   438
	r->size = len;
slouken@278
   439
slouken@278
   440
	if (r->size > 0) {
slouken@381
   441
		r->buf = malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) +
slouken@278
   442
		    r->size);
slouken@278
   443
		if (r->buf == NULL) {
slouken@278
   444
			SDL_OutOfMemory();
slouken@278
   445
			return (-1);
slouken@278
   446
		}
slouken@278
   447
	} else {
slouken@278
   448
		r->buf = NULL;
slouken@278
   449
	}
slouken@278
   450
slouken@278
   451
	r->status = SREPORT_CLEAN;
slouken@278
   452
	return (0);
slouken@278
   453
}
slouken@278
   454
slouken@278
   455
static void
slouken@278
   456
report_free(struct report *r)
slouken@278
   457
{
slouken@278
   458
	if (r->buf != NULL) {
slouken@278
   459
		free(r->buf);
slouken@278
   460
	}
slouken@278
   461
	r->status = SREPORT_UNINIT;
slouken@278
   462
}
slouken@278
   463