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