src/joystick/bsd/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 14 Feb 2002 01:24:08 +0000
changeset 278 dcd9f7b50a1c
child 297 f6ffac90895c
permissions -rw-r--r--
Added support for joysticks on *BSD (thanks Wilbern!)
slouken@278
     1
/*
slouken@278
     2
    SDL - Simple DirectMedia Layer
slouken@278
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  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@278
    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@278
    42
#include <dev/usb/usb.h>
slouken@278
    43
#include <dev/usb/usbhid.h>
slouken@278
    44
#include <usbhid.h>
slouken@278
    45
slouken@278
    46
#include "SDL_error.h"
slouken@278
    47
#include "SDL_joystick.h"
slouken@278
    48
#include "SDL_sysjoystick.h"
slouken@278
    49
#include "SDL_joystick_c.h"
slouken@278
    50
slouken@278
    51
#define MAX_UHID_JOYS	4
slouken@278
    52
#define MAX_JOY_JOYS	2
slouken@278
    53
#define MAX_JOYS	(MAX_UHID_JOYS + MAX_JOY_JOYS)
slouken@278
    54
slouken@278
    55
#define SDLAXIS_UINT8(v)		\
slouken@278
    56
	((v == 127) ? 0 :		\
slouken@278
    57
	 (v == 255) ? 32767 :		\
slouken@278
    58
	 -32767)
slouken@278
    59
slouken@278
    60
struct report {
slouken@278
    61
	struct	usb_ctl_report *buf;	/* Buffer */
slouken@278
    62
	size_t	size;			/* Buffer size */
slouken@278
    63
	int	rid;			/* Report ID */
slouken@278
    64
	enum {
slouken@278
    65
		SREPORT_UNINIT,
slouken@278
    66
		SREPORT_CLEAN,
slouken@278
    67
		SREPORT_DIRTY
slouken@278
    68
	} status;
slouken@278
    69
};
slouken@278
    70
slouken@278
    71
static struct {
slouken@278
    72
	int	uhid_report;
slouken@278
    73
	enum	hid_kind kind;
slouken@278
    74
	const	char *name;
slouken@278
    75
} const repinfo[] = {
slouken@278
    76
	{ UHID_INPUT_REPORT,	hid_input,	"input" },
slouken@278
    77
	{ UHID_OUTPUT_REPORT,	hid_output,	"output" },
slouken@278
    78
	{ UHID_FEATURE_REPORT,	hid_feature,	"feature" }
slouken@278
    79
};
slouken@278
    80
#define REPORT_INPUT	0
slouken@278
    81
#define REPORT_OUTPUT	1
slouken@278
    82
#define REPORT_FEATURE	2
slouken@278
    83
slouken@278
    84
struct joystick_hwdata {
slouken@278
    85
	int	fd;
slouken@278
    86
	char	*path;
slouken@278
    87
	enum {
slouken@278
    88
		BSDJOY_UHID,	/* uhid(4) */
slouken@278
    89
		BSDJOY_JOY	/* joy(4) */
slouken@278
    90
	} type;
slouken@278
    91
	struct	report_desc *repdesc;
slouken@278
    92
	struct	report inreport;
slouken@278
    93
	int	axismin[3];
slouken@278
    94
	int	axismax[3];
slouken@278
    95
};
slouken@278
    96
slouken@278
    97
static char *joynames[MAX_JOYS];
slouken@278
    98
static char *joydevnames[MAX_JOYS];
slouken@278
    99
slouken@278
   100
static int	report_alloc(struct report *, struct report_desc *, int);
slouken@278
   101
static void	report_free(struct report *);
slouken@278
   102
slouken@278
   103
int
slouken@278
   104
SDL_SYS_JoystickInit(void)
slouken@278
   105
{
slouken@278
   106
	char s[10];
slouken@278
   107
	int i, fd;
slouken@278
   108
slouken@278
   109
	SDL_numjoysticks = 0;
slouken@278
   110
slouken@278
   111
	memset(joynames, NULL, sizeof(joynames));
slouken@278
   112
	memset(joydevnames, NULL, sizeof(joydevnames));
slouken@278
   113
slouken@278
   114
	for (i = 0; i < MAX_UHID_JOYS; i++) {
slouken@278
   115
		sprintf(s, "/dev/uhid%d", i);
slouken@278
   116
		fd = open(s, O_RDWR);
slouken@278
   117
		if (fd > 0) {
slouken@278
   118
			joynames[SDL_numjoysticks++] = strdup(s);
slouken@278
   119
			close(fd);
slouken@278
   120
		}
slouken@278
   121
	}
slouken@278
   122
	for (i = 0; i < MAX_JOY_JOYS; i++) {
slouken@278
   123
		sprintf(s, "/dev/joy%d", i);
slouken@278
   124
		fd = open(s, O_RDWR);
slouken@278
   125
		if (fd > 0) {
slouken@278
   126
			joynames[SDL_numjoysticks++] = strdup(s);
slouken@278
   127
			close(fd);
slouken@278
   128
		}
slouken@278
   129
	}
slouken@278
   130
slouken@278
   131
	/* Read the default USB HID usage table. */
slouken@278
   132
	hid_init(NULL);
slouken@278
   133
slouken@278
   134
	return (SDL_numjoysticks);
slouken@278
   135
}
slouken@278
   136
slouken@278
   137
const char *
slouken@278
   138
SDL_SYS_JoystickName(int index)
slouken@278
   139
{
slouken@278
   140
	if (joydevnames[index] != NULL) {
slouken@278
   141
		return (joydevnames[index]);
slouken@278
   142
	}
slouken@278
   143
	return (joynames[index]);
slouken@278
   144
}
slouken@278
   145
slouken@278
   146
int
slouken@278
   147
SDL_SYS_JoystickOpen(SDL_Joystick *joy)
slouken@278
   148
{
slouken@278
   149
	char *path = joynames[joy->index];
slouken@278
   150
	struct joystick_hwdata *hw;
slouken@278
   151
	struct hid_item hitem;
slouken@278
   152
	struct hid_data *hdata;
slouken@278
   153
	struct report *rep;
slouken@278
   154
	int fd;
slouken@278
   155
slouken@278
   156
	fd = open(path, O_RDWR);
slouken@278
   157
	if (fd < 0) {
slouken@278
   158
		SDL_SetError("%s: %s", path, strerror(errno));
slouken@278
   159
		return (-1);
slouken@278
   160
	}
slouken@278
   161
slouken@278
   162
	hw = (struct joystick_hwdata *)malloc(sizeof(struct joystick_hwdata));
slouken@278
   163
	if (hw == NULL) {
slouken@278
   164
		SDL_OutOfMemory();
slouken@278
   165
		close(fd);
slouken@278
   166
		return (-1);
slouken@278
   167
	}
slouken@278
   168
	joy->hwdata = hw;
slouken@278
   169
	hw->fd = fd;
slouken@278
   170
	hw->path = strdup(path);
slouken@278
   171
	hw->type = BSDJOY_UHID;
slouken@278
   172
	hw->repdesc = hid_get_report_desc(fd);
slouken@278
   173
	if (hw->repdesc == NULL) {
slouken@278
   174
		SDL_SetError("%s: USB_GET_REPORT_DESC: %s", hw->path,
slouken@278
   175
		    strerror(errno));
slouken@278
   176
		goto usberr;
slouken@278
   177
	}
slouken@278
   178
slouken@278
   179
	rep = &hw->inreport;
slouken@278
   180
	if (report_alloc(rep, hw->repdesc, REPORT_INPUT) < 0) {
slouken@278
   181
		goto usberr;
slouken@278
   182
	}
slouken@278
   183
	if (rep->size <= 0) {
slouken@278
   184
		SDL_SetError("Input report descriptor has invalid length");
slouken@278
   185
		goto usberr;
slouken@278
   186
	}
slouken@278
   187
slouken@278
   188
	hdata = hid_start_parse(hw->repdesc, 1 << hid_input);
slouken@278
   189
	if (hdata == NULL) {
slouken@278
   190
		SDL_SetError("%s: Cannot start HID parser", hw->path);
slouken@278
   191
		goto usberr;
slouken@278
   192
	}
slouken@278
   193
	joy->naxes = 0;
slouken@278
   194
	joy->nbuttons = 0;
slouken@278
   195
	joy->nhats = 0;
slouken@278
   196
	joy->nballs = 0;
slouken@278
   197
slouken@278
   198
	while (hid_get_item(hdata, &hitem) > 0) {
slouken@278
   199
		char *s, *sp;
slouken@278
   200
slouken@278
   201
		switch (hitem.kind) {
slouken@278
   202
		case hid_collection:
slouken@278
   203
			switch (HID_PAGE(hitem.usage)) {
slouken@278
   204
			case HUP_GENERIC_DESKTOP:
slouken@278
   205
				switch (HID_USAGE(hitem.usage)) {
slouken@278
   206
				case HUG_JOYSTICK:
slouken@278
   207
				case HUG_GAME_PAD:
slouken@278
   208
					s = hid_usage_in_page(hitem.usage);
slouken@278
   209
					sp = malloc(strlen(s) + 5);
slouken@278
   210
					sprintf(sp, "%s (%d)", s,
slouken@278
   211
					    joy->index);
slouken@278
   212
					joydevnames[joy->index] = sp;
slouken@278
   213
				}
slouken@278
   214
			}
slouken@278
   215
			break;
slouken@278
   216
		case hid_input:
slouken@278
   217
			switch (HID_PAGE(hitem.usage)) {
slouken@278
   218
			case HUP_UNDEFINED:
slouken@278
   219
				break;
slouken@278
   220
			case HUP_GENERIC_DESKTOP:
slouken@278
   221
				switch (HID_USAGE(hitem.usage)) {
slouken@278
   222
				case HUG_X:
slouken@278
   223
				case HUG_Y:
slouken@278
   224
				case HUG_Z:
slouken@278
   225
					hw->axismin[joy->naxes] =
slouken@278
   226
					    hitem.logical_minimum;
slouken@278
   227
					hw->axismax[joy->naxes] =
slouken@278
   228
					    hitem.logical_maximum;
slouken@278
   229
					joy->naxes++;
slouken@278
   230
					break;
slouken@278
   231
				}
slouken@278
   232
				break;
slouken@278
   233
			case HUP_BUTTON:
slouken@278
   234
				joy->nbuttons++;
slouken@278
   235
				break;
slouken@278
   236
			}
slouken@278
   237
			break;
slouken@278
   238
		default:
slouken@278
   239
			break;
slouken@278
   240
		}
slouken@278
   241
	}
slouken@278
   242
	hid_end_parse(hdata);
slouken@278
   243
slouken@278
   244
	/* The poll blocks the event thread. */
slouken@278
   245
	fcntl(fd, F_SETFL, O_NONBLOCK);
slouken@278
   246
slouken@278
   247
	return (0);
slouken@278
   248
usberr:
slouken@278
   249
	close(hw->fd);
slouken@278
   250
	free(hw->path);
slouken@278
   251
	free(hw);
slouken@278
   252
	return (-1);
slouken@278
   253
}
slouken@278
   254
slouken@278
   255
void
slouken@278
   256
SDL_SYS_JoystickUpdate(SDL_Joystick *joy)
slouken@278
   257
{
slouken@278
   258
	static struct hid_item hitem;
slouken@278
   259
	static struct hid_data *hdata;
slouken@278
   260
	static int nbutton, naxe, v, max, min;
slouken@278
   261
	static struct report *rep;
slouken@278
   262
	
slouken@278
   263
	rep = &joy->hwdata->inreport;
slouken@278
   264
	if (read(joy->hwdata->fd, rep->buf->data, rep->size) != rep->size) {
slouken@278
   265
		return;
slouken@278
   266
	}
slouken@278
   267
	hdata = hid_start_parse(joy->hwdata->repdesc, 1 << hid_input);
slouken@278
   268
	if (hdata == NULL) {
slouken@278
   269
		fprintf(stderr, "%s: Cannot start HID parser\n",
slouken@278
   270
		    joy->hwdata->path);
slouken@278
   271
		return;
slouken@278
   272
	}
slouken@278
   273
slouken@278
   274
	for (nbutton = 0, naxe = 0; hid_get_item(hdata, &hitem) > 0;) {
slouken@278
   275
		switch (hitem.kind) {
slouken@278
   276
		case hid_input:
slouken@278
   277
			switch (HID_PAGE(hitem.usage)) {
slouken@278
   278
			case HUP_UNDEFINED:
slouken@278
   279
				continue;
slouken@278
   280
			case HUP_GENERIC_DESKTOP:
slouken@278
   281
				switch (HID_USAGE(hitem.usage)) {
slouken@278
   282
				case HUG_X:
slouken@278
   283
				case HUG_Y:
slouken@278
   284
				case HUG_Z:
slouken@278
   285
					v = hid_get_data(rep->buf->data,
slouken@278
   286
					    &hitem);
slouken@278
   287
slouken@278
   288
					/*
slouken@278
   289
					 * XXX revisit later. need to test
slouken@278
   290
					 * with more devices.
slouken@278
   291
					 */
slouken@278
   292
					if (joy->hwdata->axismin[naxe] == 0 &&
slouken@278
   293
					    joy->hwdata->axismax[naxe] == 255) {
slouken@278
   294
						v = SDLAXIS_UINT8(v);
slouken@278
   295
					}
slouken@278
   296
slouken@278
   297
					if (v != joy->axes[naxe]) {
slouken@278
   298
						SDL_PrivateJoystickAxis(joy,
slouken@278
   299
						    naxe, (Sint32)v);
slouken@278
   300
					}
slouken@278
   301
					naxe++;
slouken@278
   302
					break;
slouken@278
   303
				}
slouken@278
   304
				break;
slouken@278
   305
			case HUP_BUTTON:
slouken@278
   306
				/* XXX assume a 0..1 range */
slouken@278
   307
				v = hid_get_data(rep->buf->data, &hitem);
slouken@278
   308
				if (joy->buttons[nbutton] != v) {
slouken@278
   309
					SDL_PrivateJoystickButton(joy,
slouken@278
   310
					    nbutton, v);
slouken@278
   311
				}
slouken@278
   312
				nbutton++;
slouken@278
   313
				break;
slouken@278
   314
			}
slouken@278
   315
			break;
slouken@278
   316
		default:
slouken@278
   317
			break;
slouken@278
   318
		}
slouken@278
   319
	}
slouken@278
   320
	hid_end_parse(hdata);
slouken@278
   321
slouken@278
   322
	return;
slouken@278
   323
}
slouken@278
   324
slouken@278
   325
/* Function to close a joystick after use */
slouken@278
   326
void
slouken@278
   327
SDL_SYS_JoystickClose(SDL_Joystick *joy)
slouken@278
   328
{
slouken@278
   329
	report_free(&joy->hwdata->inreport);
slouken@278
   330
	hid_dispose_report_desc(joy->hwdata->repdesc);
slouken@278
   331
	close(joy->hwdata->fd);
slouken@278
   332
	free(joy->hwdata->path);
slouken@278
   333
	free(joy->hwdata);
slouken@278
   334
slouken@278
   335
	return;
slouken@278
   336
}
slouken@278
   337
slouken@278
   338
void
slouken@278
   339
SDL_SYS_JoystickQuit(void)
slouken@278
   340
{
slouken@278
   341
	int i;
slouken@278
   342
slouken@278
   343
	for (i = 0; i < MAX_JOYS; i++) {
slouken@278
   344
		if (joynames[i] != NULL)
slouken@278
   345
			free(joynames[i]);
slouken@278
   346
		if (joydevnames[i] != NULL)
slouken@278
   347
			free(joydevnames[i]);
slouken@278
   348
	}
slouken@278
   349
slouken@278
   350
	return;
slouken@278
   351
}
slouken@278
   352
slouken@278
   353
static int
slouken@278
   354
report_alloc(struct report *r, struct report_desc *rd, int repind)
slouken@278
   355
{
slouken@278
   356
	int len;
slouken@278
   357
slouken@278
   358
	len = hid_report_size(rd, repinfo[repind].kind, &r->rid);
slouken@278
   359
	if (len < 0) {
slouken@278
   360
		SDL_SetError("Negative HID report size");
slouken@278
   361
		return (-1);
slouken@278
   362
	}
slouken@278
   363
	r->size = len;
slouken@278
   364
slouken@278
   365
	if (r->size > 0) {
slouken@278
   366
		r->buf = malloc(sizeof(*r->buf) - sizeof(r->buf->data) +
slouken@278
   367
		    r->size);
slouken@278
   368
		if (r->buf == NULL) {
slouken@278
   369
			SDL_OutOfMemory();
slouken@278
   370
			return (-1);
slouken@278
   371
		}
slouken@278
   372
	} else {
slouken@278
   373
		r->buf = NULL;
slouken@278
   374
	}
slouken@278
   375
slouken@278
   376
	r->status = SREPORT_CLEAN;
slouken@278
   377
	return (0);
slouken@278
   378
}
slouken@278
   379
slouken@278
   380
static void
slouken@278
   381
report_free(struct report *r)
slouken@278
   382
{
slouken@278
   383
	if (r->buf != NULL) {
slouken@278
   384
		free(r->buf);
slouken@278
   385
	}
slouken@278
   386
	r->status = SREPORT_UNINIT;
slouken@278
   387
}
slouken@278
   388