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