src/joystick/bsd/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 12 Jun 2002 03:32:01 +0000
changeset 407 173909e17b7f
parent 405 b5de7389a0a5
child 426 33712e5d1ec8
permissions -rw-r--r--
API changes on OpenBSD and FreeBSD...
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@278
    35
#include <stdio.h>
slouken@278
    36
#include <stdlib.h>
slouken@278
    37
#include <unistd.h>
slouken@278
    38
#include <fcntl.h>
slouken@278
    39
#include <string.h>
slouken@278
    40
#include <errno.h>
slouken@278
    41
slouken@403
    42
#if defined(HAVE_USB_H)
slouken@403
    43
#include <usb.h>
slouken@403
    44
#endif
slouken@278
    45
#include <dev/usb/usb.h>
slouken@278
    46
#include <dev/usb/usbhid.h>
slouken@358
    47
slouken@381
    48
#if defined(HAVE_USBHID_H)
slouken@381
    49
#include <usbhid.h>
slouken@381
    50
#elif defined(HAVE_LIBUSB_H)
slouken@381
    51
#include <libusb.h>
slouken@381
    52
#elif defined(HAVE_LIBUSBHID_H)
slouken@381
    53
#include <libusbhid.h>
slouken@358
    54
#endif
slouken@278
    55
slouken@278
    56
#include "SDL_error.h"
slouken@278
    57
#include "SDL_joystick.h"
slouken@278
    58
#include "SDL_sysjoystick.h"
slouken@278
    59
#include "SDL_joystick_c.h"
slouken@278
    60
slouken@278
    61
#define MAX_UHID_JOYS	4
slouken@278
    62
#define MAX_JOY_JOYS	2
slouken@278
    63
#define MAX_JOYS	(MAX_UHID_JOYS + MAX_JOY_JOYS)
slouken@278
    64
slouken@278
    65
struct report {
slouken@278
    66
	struct	usb_ctl_report *buf;	/* Buffer */
slouken@278
    67
	size_t	size;			/* Buffer size */
slouken@278
    68
	int	rid;			/* Report ID */
slouken@278
    69
	enum {
slouken@278
    70
		SREPORT_UNINIT,
slouken@278
    71
		SREPORT_CLEAN,
slouken@278
    72
		SREPORT_DIRTY
slouken@278
    73
	} status;
slouken@278
    74
};
slouken@278
    75
slouken@278
    76
static struct {
slouken@278
    77
	int	uhid_report;
slouken@402
    78
	hid_kind_t kind;
slouken@278
    79
	const	char *name;
slouken@278
    80
} const repinfo[] = {
slouken@278
    81
	{ UHID_INPUT_REPORT,	hid_input,	"input" },
slouken@278
    82
	{ UHID_OUTPUT_REPORT,	hid_output,	"output" },
slouken@278
    83
	{ UHID_FEATURE_REPORT,	hid_feature,	"feature" }
slouken@278
    84
};
slouken@307
    85
slouken@307
    86
enum {
slouken@307
    87
	REPORT_INPUT = 0,
slouken@307
    88
	REPORT_OUTPUT = 1,
slouken@307
    89
	REPORT_FEATURE = 2
slouken@307
    90
};
slouken@307
    91
slouken@307
    92
enum {
slouken@307
    93
	JOYAXE_X,
slouken@307
    94
	JOYAXE_Y,
slouken@307
    95
	JOYAXE_Z,
slouken@307
    96
	JOYAXE_SLIDER,
slouken@307
    97
	JOYAXE_WHEEL
slouken@307
    98
};
slouken@278
    99
slouken@278
   100
struct joystick_hwdata {
slouken@278
   101
	int	fd;
slouken@278
   102
	char	*path;
slouken@278
   103
	enum {
slouken@278
   104
		BSDJOY_UHID,	/* uhid(4) */
slouken@278
   105
		BSDJOY_JOY	/* joy(4) */
slouken@278
   106
	} type;
slouken@278
   107
	struct	report_desc *repdesc;
slouken@278
   108
	struct	report inreport;
slouken@307
   109
#if 0
slouken@307
   110
	int	axismin[];
slouken@307
   111
	int	axismax[];
slouken@307
   112
#endif
slouken@278
   113
};
slouken@278
   114
slouken@278
   115
static char *joynames[MAX_JOYS];
slouken@278
   116
static char *joydevnames[MAX_JOYS];
slouken@278
   117
slouken@278
   118
static int	report_alloc(struct report *, struct report_desc *, int);
slouken@278
   119
static void	report_free(struct report *);
slouken@278
   120
slouken@381
   121
#ifdef USBHID_UCR_DATA
slouken@381
   122
#define REP_BUF_DATA(rep) ((rep)->buf->ucr_data)
slouken@381
   123
#else
slouken@381
   124
#define REP_BUF_DATA(rep) ((rep)->buf->data)
slouken@381
   125
#endif
slouken@381
   126
slouken@278
   127
int
slouken@278
   128
SDL_SYS_JoystickInit(void)
slouken@278
   129
{
slouken@278
   130
	char s[10];
slouken@278
   131
	int i, fd;
slouken@278
   132
slouken@278
   133
	SDL_numjoysticks = 0;
slouken@278
   134
slouken@278
   135
	memset(joynames, NULL, sizeof(joynames));
slouken@278
   136
	memset(joydevnames, NULL, sizeof(joydevnames));
slouken@278
   137
slouken@278
   138
	for (i = 0; i < MAX_UHID_JOYS; i++) {
slouken@278
   139
		sprintf(s, "/dev/uhid%d", i);
slouken@278
   140
		fd = open(s, O_RDWR);
slouken@278
   141
		if (fd > 0) {
slouken@278
   142
			joynames[SDL_numjoysticks++] = strdup(s);
slouken@278
   143
			close(fd);
slouken@278
   144
		}
slouken@278
   145
	}
slouken@278
   146
	for (i = 0; i < MAX_JOY_JOYS; i++) {
slouken@278
   147
		sprintf(s, "/dev/joy%d", i);
slouken@278
   148
		fd = open(s, O_RDWR);
slouken@278
   149
		if (fd > 0) {
slouken@278
   150
			joynames[SDL_numjoysticks++] = strdup(s);
slouken@278
   151
			close(fd);
slouken@278
   152
		}
slouken@278
   153
	}
slouken@278
   154
slouken@278
   155
	/* Read the default USB HID usage table. */
slouken@278
   156
	hid_init(NULL);
slouken@278
   157
slouken@278
   158
	return (SDL_numjoysticks);
slouken@278
   159
}
slouken@278
   160
slouken@278
   161
const char *
slouken@278
   162
SDL_SYS_JoystickName(int index)
slouken@278
   163
{
slouken@278
   164
	if (joydevnames[index] != NULL) {
slouken@278
   165
		return (joydevnames[index]);
slouken@278
   166
	}
slouken@278
   167
	return (joynames[index]);
slouken@278
   168
}
slouken@278
   169
slouken@278
   170
int
slouken@278
   171
SDL_SYS_JoystickOpen(SDL_Joystick *joy)
slouken@278
   172
{
slouken@278
   173
	char *path = joynames[joy->index];
slouken@278
   174
	struct joystick_hwdata *hw;
slouken@278
   175
	struct hid_item hitem;
slouken@278
   176
	struct hid_data *hdata;
slouken@278
   177
	struct report *rep;
slouken@278
   178
	int fd;
slouken@278
   179
slouken@278
   180
	fd = open(path, O_RDWR);
slouken@278
   181
	if (fd < 0) {
slouken@278
   182
		SDL_SetError("%s: %s", path, strerror(errno));
slouken@278
   183
		return (-1);
slouken@278
   184
	}
slouken@278
   185
slouken@278
   186
	hw = (struct joystick_hwdata *)malloc(sizeof(struct joystick_hwdata));
slouken@278
   187
	if (hw == NULL) {
slouken@278
   188
		SDL_OutOfMemory();
slouken@278
   189
		close(fd);
slouken@278
   190
		return (-1);
slouken@278
   191
	}
slouken@278
   192
	joy->hwdata = hw;
slouken@278
   193
	hw->fd = fd;
slouken@278
   194
	hw->path = strdup(path);
slouken@278
   195
	hw->type = BSDJOY_UHID;
slouken@278
   196
	hw->repdesc = hid_get_report_desc(fd);
slouken@278
   197
	if (hw->repdesc == NULL) {
slouken@278
   198
		SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
slouken@278
   199
		    strerror(errno));
slouken@278
   200
		goto usberr;
slouken@278
   201
	}
slouken@278
   202
slouken@278
   203
	rep = &hw->inreport;
slouken@278
   204
	if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
slouken@278
   205
		goto usberr;
slouken@278
   206
	}
slouken@278
   207
	if (rep->size <= 0) {
slouken@307
   208
		SDL_SetError("%s: Input report descriptor has invalid length",
slouken@307
   209
		    hw->path);
slouken@278
   210
		goto usberr;
slouken@278
   211
	}
slouken@278
   212
slouken@407
   213
#ifdef USBHID_NEW
slouken@407
   214
	hdata = hid_start_parse(hw->repdesc, 1 << hid_input, rep->rid);
slouken@407
   215
#else
slouken@278
   216
	hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
slouken@407
   217
#endif
slouken@278
   218
	if (hdata == NULL) {
slouken@278
   219
		SDL_SetError("%s: Cannot start HID parser", hw->path);
slouken@278
   220
		goto usberr;
slouken@278
   221
	}
slouken@278
   222
	joy->naxes = 0;
slouken@278
   223
	joy->nbuttons = 0;
slouken@278
   224
	joy->nhats = 0;
slouken@278
   225
	joy->nballs = 0;
slouken@278
   226
slouken@278
   227
	while (hid_get_item(hdata, &hitem) > 0) {
slouken@358
   228
		char *sp;
slouken@358
   229
		const char *s;
slouken@278
   230
slouken@278
   231
		switch (hitem.kind) {
slouken@278
   232
		case hid_collection:
slouken@278
   233
			switch (HID_PAGE(hitem.usage)) {
slouken@278
   234
			case HUP_GENERIC_DESKTOP:
slouken@278
   235
				switch (HID_USAGE(hitem.usage)) {
slouken@278
   236
				case HUG_JOYSTICK:
slouken@278
   237
				case HUG_GAME_PAD:
slouken@278
   238
					s = hid_usage_in_page(hitem.usage);
slouken@278
   239
					sp = malloc(strlen(s) + 5);
slouken@278
   240
					sprintf(sp, "%s (%d)", s,
slouken@278
   241
					    joy->index);
slouken@278
   242
					joydevnames[joy->index] = sp;
slouken@278
   243
				}
slouken@278
   244
			}
slouken@278
   245
			break;
slouken@278
   246
		case hid_input:
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_X:
slouken@278
   251
				case HUG_Y:
slouken@278
   252
				case HUG_Z:
slouken@307
   253
				case HUG_SLIDER:
slouken@307
   254
				case HUG_WHEEL:
slouken@307
   255
#if 0
slouken@278
   256
					hw->axismin[joy->naxes] =
slouken@278
   257
					    hitem.logical_minimum;
slouken@278
   258
					hw->axismax[joy->naxes] =
slouken@278
   259
					    hitem.logical_maximum;
slouken@307
   260
#endif
slouken@278
   261
					joy->naxes++;
slouken@278
   262
					break;
slouken@278
   263
				}
slouken@278
   264
				break;
slouken@278
   265
			case HUP_BUTTON:
slouken@278
   266
				joy->nbuttons++;
slouken@278
   267
				break;
slouken@405
   268
			default:
slouken@405
   269
				break;
slouken@278
   270
			}
slouken@278
   271
			break;
slouken@278
   272
		default:
slouken@278
   273
			break;
slouken@278
   274
		}
slouken@278
   275
	}
slouken@278
   276
	hid_end_parse(hdata);
slouken@278
   277
slouken@278
   278
	/* The poll blocks the event thread. */
slouken@278
   279
	fcntl(fd, F_SETFL, O_NONBLOCK);
slouken@278
   280
slouken@278
   281
	return (0);
slouken@278
   282
usberr:
slouken@278
   283
	close(hw->fd);
slouken@278
   284
	free(hw->path);
slouken@278
   285
	free(hw);
slouken@278
   286
	return (-1);
slouken@278
   287
}
slouken@278
   288
slouken@278
   289
void
slouken@278
   290
SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
slouken@278
   291
{
slouken@381
   292
	struct hid_item hitem;
slouken@381
   293
	struct hid_data *hdata;
slouken@381
   294
	struct report *rep;
slouken@358
   295
	int nbutton, naxe = -1;
slouken@307
   296
	Sint32 v;
slouken@278
   297
	
slouken@278
   298
	rep = &joy->hwdata->inreport;
slouken@381
   299
slouken@381
   300
	if (read(joy->hwdata->fd, REP_BUF_DATA(rep), rep->size) != rep->size) {
slouken@278
   301
		return;
slouken@278
   302
	}
slouken@407
   303
#ifdef USBHID_NEW
slouken@407
   304
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input, rep->rid);
slouken@407
   305
#else
slouken@278
   306
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
slouken@407
   307
#endif
slouken@278
   308
	if (hdata == NULL) {
slouken@278
   309
		fprintf(stderr, "%s: Cannot start HID parser\n",
slouken@278
   310
		    joy->hwdata->path);
slouken@278
   311
		return;
slouken@278
   312
	}
slouken@278
   313
slouken@307
   314
	for (nbutton = 0; hid_get_item(hdata, &hitem) > 0;) {
slouken@278
   315
		switch (hitem.kind) {
slouken@278
   316
		case hid_input:
slouken@278
   317
			switch (HID_PAGE(hitem.usage)) {
slouken@278
   318
			case HUP_GENERIC_DESKTOP:
slouken@278
   319
				switch (HID_USAGE(hitem.usage)) {
slouken@278
   320
				case HUG_X:
slouken@307
   321
					naxe = JOYAXE_X;
slouken@307
   322
					goto scaleaxe;
slouken@278
   323
				case HUG_Y:
slouken@307
   324
					naxe = JOYAXE_Y;
slouken@307
   325
					goto scaleaxe;
slouken@278
   326
				case HUG_Z:
slouken@307
   327
					naxe = JOYAXE_Z;
slouken@307
   328
					goto scaleaxe;
slouken@307
   329
				case HUG_SLIDER:
slouken@307
   330
					naxe = JOYAXE_SLIDER;
slouken@307
   331
					goto scaleaxe;
slouken@307
   332
				case HUG_WHEEL:
slouken@307
   333
					naxe = JOYAXE_WHEEL;
slouken@307
   334
					goto scaleaxe;
slouken@307
   335
				}
slouken@307
   336
scaleaxe:
slouken@381
   337
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
slouken@381
   338
				    &hitem);
slouken@307
   339
				if (v != 127) {
slouken@307
   340
					if (v < 127) {
slouken@307
   341
						v = -(256 - v);
slouken@307
   342
						v <<= 7;
slouken@307
   343
						v++;
slouken@307
   344
					} else {
slouken@307
   345
						v++;
slouken@307
   346
						v <<= 7;
slouken@307
   347
						v--;
slouken@278
   348
					}
slouken@307
   349
				} else {
slouken@307
   350
					v = 0;
slouken@307
   351
				}
slouken@307
   352
				if (v != joy->axes[naxe]) {
slouken@307
   353
					SDL_PrivateJoystickAxis(joy, naxe, v);
slouken@278
   354
				}
slouken@278
   355
				break;
slouken@278
   356
			case HUP_BUTTON:
slouken@381
   357
				v = (Sint32)hid_get_data(REP_BUF_DATA(rep),
slouken@307
   358
				    &hitem);
slouken@278
   359
				if (joy->buttons[nbutton] != v) {
slouken@278
   360
					SDL_PrivateJoystickButton(joy,
slouken@278
   361
					    nbutton, v);
slouken@278
   362
				}
slouken@278
   363
				nbutton++;
slouken@278
   364
				break;
slouken@405
   365
			default:
slouken@405
   366
				continue;
slouken@278
   367
			}
slouken@278
   368
			break;
slouken@278
   369
		default:
slouken@278
   370
			break;
slouken@278
   371
		}
slouken@278
   372
	}
slouken@278
   373
	hid_end_parse(hdata);
slouken@278
   374
slouken@278
   375
	return;
slouken@278
   376
}
slouken@278
   377
slouken@278
   378
/* Function to close a joystick after use */
slouken@278
   379
void
slouken@278
   380
SDL_SYS_JoystickClose(SDL_Joystick *joy)
slouken@278
   381
{
slouken@278
   382
	report_free(&joy->hwdata->inreport);
slouken@278
   383
	hid_dispose_report_desc(joy->hwdata->repdesc);
slouken@278
   384
	close(joy->hwdata->fd);
slouken@278
   385
	free(joy->hwdata->path);
slouken@278
   386
	free(joy->hwdata);
slouken@278
   387
slouken@278
   388
	return;
slouken@278
   389
}
slouken@278
   390
slouken@278
   391
void
slouken@278
   392
SDL_SYS_JoystickQuit(void)
slouken@278
   393
{
slouken@278
   394
	int i;
slouken@278
   395
slouken@278
   396
	for (i = 0; i < MAX_JOYS; i++) {
slouken@278
   397
		if (joynames[i] != NULL)
slouken@278
   398
			free(joynames[i]);
slouken@278
   399
		if (joydevnames[i] != NULL)
slouken@278
   400
			free(joydevnames[i]);
slouken@278
   401
	}
slouken@278
   402
slouken@278
   403
	return;
slouken@278
   404
}
slouken@278
   405
slouken@278
   406
static int
slouken@278
   407
report_alloc(struct report *r, struct report_desc *rd, int repind)
slouken@278
   408
{
slouken@278
   409
	int len;
slouken@278
   410
slouken@407
   411
#ifdef USBHID_NEW
slouken@407
   412
	len = hid_report_size(rd, repinfo[repind].kind, r->rid);
slouken@407
   413
#else
slouken@278
   414
	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
slouken@407
   415
#endif
slouken@278
   416
	if (len < 0) {
slouken@278
   417
		SDL_SetError("Negative HID report size");
slouken@278
   418
		return (-1);
slouken@278
   419
	}
slouken@278
   420
	r->size = len;
slouken@278
   421
slouken@278
   422
	if (r->size > 0) {
slouken@381
   423
		r->buf = malloc(sizeof(*r->buf) - sizeof(REP_BUF_DATA(r)) +
slouken@278
   424
		    r->size);
slouken@278
   425
		if (r->buf == NULL) {
slouken@278
   426
			SDL_OutOfMemory();
slouken@278
   427
			return (-1);
slouken@278
   428
		}
slouken@278
   429
	} else {
slouken@278
   430
		r->buf = NULL;
slouken@278
   431
	}
slouken@278
   432
slouken@278
   433
	r->status = SREPORT_CLEAN;
slouken@278
   434
	return (0);
slouken@278
   435
}
slouken@278
   436
slouken@278
   437
static void
slouken@278
   438
report_free(struct report *r)
slouken@278
   439
{
slouken@278
   440
	if (r->buf != NULL) {
slouken@278
   441
		free(r->buf);
slouken@278
   442
	}
slouken@278
   443
	r->status = SREPORT_UNINIT;
slouken@278
   444
}
slouken@278
   445