2001-04-26 16:45:43 +00:00
|
|
|
/*
|
2011-04-08 13:03:26 -07:00
|
|
|
Simple DirectMedia Layer
|
2011-12-31 09:28:07 -05:00
|
|
|
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
|
2011-04-08 13:03:26 -07:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
2001-04-26 16:45:43 +00:00
|
|
|
*/
|
2006-02-21 08:46:50 +00:00
|
|
|
#include "SDL_config.h"
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2006-04-14 04:46:47 +00:00
|
|
|
#ifdef SDL_JOYSTICK_LINUX
|
|
|
|
|
2012-12-11 11:07:48 -05:00
|
|
|
#ifndef SDL_INPUT_LINUXEV
|
|
|
|
#error SDL now requires a Linux 2.4+ kernel with /dev/input/event support.
|
|
|
|
#endif
|
|
|
|
|
2001-04-26 16:45:43 +00:00
|
|
|
/* This is the system specific header for the SDL joystick API */
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
2006-07-10 21:04:37 +00:00
|
|
|
#include <limits.h> /* For the definition of PATH_MAX */
|
2001-04-26 16:45:43 +00:00
|
|
|
#include <linux/joystick.h>
|
|
|
|
|
|
|
|
#include "SDL_joystick.h"
|
2006-02-16 10:11:48 +00:00
|
|
|
#include "../SDL_sysjoystick.h"
|
|
|
|
#include "../SDL_joystick_c.h"
|
2008-08-25 09:55:03 +00:00
|
|
|
#include "SDL_sysjoystick_c.h"
|
2001-04-26 16:45:43 +00:00
|
|
|
|
|
|
|
/* The maximum number of joysticks we'll detect */
|
|
|
|
#define MAX_JOYSTICKS 32
|
|
|
|
|
|
|
|
/* A list of available joysticks */
|
Date: Sun, 18 Apr 2004 16:09:53 -0400 (EDT)
From: David MacCormack
Subject: [SDL] Linux joystick patch
I recently got myself a PS2 -> USB converter (a super joybox 5). It
accepts 4 PSX/PS2 controllers. It's implemented as a HID, which is nice
because it doesn't require its own driver, but the problem is that it's
implemented as a *single* HID -- that is, it shows up as a single
joystick with 19 axes, 4 hats, and 48 buttons. This poses a problem for a
number of apps which use SDL (stella, fce ultra, zsnes, to name a few) and
see only a single (physical) joystick even though there are really 4
(logical) joysticks. There are a number of these types of devices on the
market, and I've seen others post messages (in the zsnes forum, for
example) with the same problem, so I came up with what I think is a pretty
generic solution.
I patched src/joystick/linux/SDL_sysjoystic.c to include support for
logical joysticks; basically, it's a static array and supporting functions
that map a single physical joystick to multiple logical joysticks. The
attached patch has the new code. It's wrapped inside #ifndef
statements so that you can get the old behavior if you want.
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%40893
2004-05-16 18:46:24 +00:00
|
|
|
static struct
|
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
char *fname;
|
Date: Sun, 18 Apr 2004 16:09:53 -0400 (EDT)
From: David MacCormack
Subject: [SDL] Linux joystick patch
I recently got myself a PS2 -> USB converter (a super joybox 5). It
accepts 4 PSX/PS2 controllers. It's implemented as a HID, which is nice
because it doesn't require its own driver, but the problem is that it's
implemented as a *single* HID -- that is, it shows up as a single
joystick with 19 axes, 4 hats, and 48 buttons. This poses a problem for a
number of apps which use SDL (stella, fce ultra, zsnes, to name a few) and
see only a single (physical) joystick even though there are really 4
(logical) joysticks. There are a number of these types of devices on the
market, and I've seen others post messages (in the zsnes forum, for
example) with the same problem, so I came up with what I think is a pretty
generic solution.
I patched src/joystick/linux/SDL_sysjoystic.c to include support for
logical joysticks; basically, it's a static array and supporting functions
that map a single physical joystick to multiple logical joysticks. The
attached patch has the new code. It's wrapped inside #ifndef
statements so that you can get the old behavior if you want.
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%40893
2004-05-16 18:46:24 +00:00
|
|
|
} SDL_joylist[MAX_JOYSTICKS];
|
|
|
|
|
2001-04-26 16:45:43 +00:00
|
|
|
|
|
|
|
#define test_bit(nr, addr) \
|
2009-10-18 16:14:35 +00:00
|
|
|
(((1UL << ((nr) % (sizeof(long) * 8))) & ((addr)[(nr) / (sizeof(long) * 8)])) != 0)
|
|
|
|
#define NBITS(x) ((((x)-1)/(sizeof(long) * 8))+1)
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
static int
|
2012-12-11 11:07:48 -05:00
|
|
|
IsJoystick(int fd)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2009-10-18 16:14:35 +00:00
|
|
|
unsigned long evbit[NBITS(EV_MAX)] = { 0 };
|
|
|
|
unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
|
|
|
|
unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
|
2006-07-10 21:04:37 +00:00
|
|
|
|
|
|
|
if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
|
|
|
|
(ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
|
|
|
|
(ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
|
|
|
|
return (0);
|
|
|
|
}
|
2009-02-21 18:02:55 +00:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (!(test_bit(EV_KEY, evbit) && test_bit(EV_ABS, evbit) &&
|
2009-02-21 18:02:55 +00:00
|
|
|
test_bit(ABS_X, absbit) && test_bit(ABS_Y, absbit))) {
|
2006-07-10 21:04:37 +00:00
|
|
|
return 0;
|
2009-02-21 18:02:55 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
return (1);
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-26 22:27:49 -08:00
|
|
|
static int SDL_SYS_numjoysticks = 0;
|
2012-11-26 16:37:54 -08:00
|
|
|
|
2001-04-26 16:45:43 +00:00
|
|
|
/* Function to scan the system for joysticks */
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
|
|
|
SDL_SYS_JoystickInit(void)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
/* The base path of the joystick devices */
|
|
|
|
const char *joydev_pattern[] = {
|
|
|
|
"/dev/input/event%d",
|
|
|
|
};
|
|
|
|
int numjoysticks;
|
|
|
|
int i, j;
|
|
|
|
int fd;
|
|
|
|
char path[PATH_MAX];
|
|
|
|
dev_t dev_nums[MAX_JOYSTICKS]; /* major/minor device numbers */
|
|
|
|
struct stat sb;
|
|
|
|
int n, duplicate;
|
|
|
|
|
|
|
|
numjoysticks = 0;
|
|
|
|
|
2011-02-16 04:51:13 -08:00
|
|
|
/* First see if the user specified one or more joysticks to use */
|
2006-07-10 21:04:37 +00:00
|
|
|
if (SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL) {
|
2011-02-16 04:51:13 -08:00
|
|
|
char *envcopy, *envpath, *delim;
|
|
|
|
envcopy = SDL_strdup(SDL_getenv("SDL_JOYSTICK_DEVICE"));
|
|
|
|
envpath = envcopy;
|
|
|
|
while (envpath != NULL) {
|
|
|
|
delim = SDL_strchr(envpath, ':');
|
|
|
|
if (delim != NULL) {
|
|
|
|
*delim++ = '\0';
|
|
|
|
}
|
|
|
|
if (stat(envpath, &sb) == 0) {
|
|
|
|
fd = open(envpath, O_RDONLY, 0);
|
|
|
|
if (fd >= 0) {
|
|
|
|
/* Assume the user knows what they're doing. */
|
|
|
|
SDL_joylist[numjoysticks].fname = SDL_strdup(envpath);
|
|
|
|
if (SDL_joylist[numjoysticks].fname) {
|
|
|
|
dev_nums[numjoysticks] = sb.st_rdev;
|
|
|
|
++numjoysticks;
|
|
|
|
}
|
|
|
|
close(fd);
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-16 04:51:13 -08:00
|
|
|
envpath = delim;
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
2011-02-16 04:51:13 -08:00
|
|
|
SDL_free(envcopy);
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < SDL_arraysize(joydev_pattern); ++i) {
|
|
|
|
for (j = 0; j < MAX_JOYSTICKS; ++j) {
|
|
|
|
SDL_snprintf(path, SDL_arraysize(path), joydev_pattern[i], j);
|
|
|
|
|
|
|
|
/* rcg06302000 replaced access(F_OK) call with stat().
|
|
|
|
* stat() will fail if the file doesn't exist, so it's
|
|
|
|
* equivalent behaviour.
|
|
|
|
*/
|
|
|
|
if (stat(path, &sb) == 0) {
|
|
|
|
/* Check to make sure it's not already in list.
|
|
|
|
* This happens when we see a stick via symlink.
|
|
|
|
*/
|
|
|
|
duplicate = 0;
|
|
|
|
for (n = 0; (n < numjoysticks) && !duplicate; ++n) {
|
|
|
|
if (sb.st_rdev == dev_nums[n]) {
|
|
|
|
duplicate = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (duplicate) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = open(path, O_RDONLY, 0);
|
|
|
|
if (fd < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2001-04-26 16:45:43 +00:00
|
|
|
#ifdef DEBUG_INPUT_EVENTS
|
2006-07-10 21:04:37 +00:00
|
|
|
printf("Checking %s\n", path);
|
2001-04-26 16:45:43 +00:00
|
|
|
#endif
|
2012-12-11 11:07:48 -05:00
|
|
|
if ((i == 0) && !IsJoystick(fd)) {
|
2006-07-10 21:04:37 +00:00
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* We're fine, add this joystick */
|
|
|
|
SDL_joylist[numjoysticks].fname = SDL_strdup(path);
|
|
|
|
if (SDL_joylist[numjoysticks].fname) {
|
|
|
|
dev_nums[numjoysticks] = sb.st_rdev;
|
|
|
|
++numjoysticks;
|
|
|
|
}
|
2007-12-29 06:17:31 +00:00
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
2002-12-02 03:11:36 +00:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
/* This is a special case...
|
|
|
|
If the event devices are valid then the joystick devices
|
|
|
|
will be duplicates but without extra information about their
|
|
|
|
hats or balls. Unfortunately, the event devices can't
|
|
|
|
currently be calibrated, so it's a win-lose situation.
|
2012-11-26 16:37:54 -08:00
|
|
|
So : /dev/input/eventX = /dev/input/jsY = /dev/js
|
2006-07-10 21:04:37 +00:00
|
|
|
*/
|
|
|
|
if ((i == 0) && (numjoysticks > 0))
|
|
|
|
break;
|
|
|
|
}
|
2002-12-02 03:11:36 +00:00
|
|
|
|
2012-11-26 16:37:54 -08:00
|
|
|
SDL_SYS_numjoysticks = numjoysticks;
|
2006-07-10 21:04:37 +00:00
|
|
|
return (numjoysticks);
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 00:58:12 -08:00
|
|
|
int SDL_SYS_NumJoysticks()
|
|
|
|
{
|
|
|
|
return SDL_SYS_numjoysticks;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDL_SYS_JoystickDetect()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_bool SDL_SYS_JoystickNeedsPolling()
|
|
|
|
{
|
|
|
|
return SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
2001-04-26 16:45:43 +00:00
|
|
|
/* Function to get the device-dependent name of a joystick */
|
2006-07-10 21:04:37 +00:00
|
|
|
const char *
|
2012-11-27 00:58:12 -08:00
|
|
|
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
int fd;
|
|
|
|
static char namebuf[128];
|
2012-11-26 21:11:28 -08:00
|
|
|
const char *name;
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
name = NULL;
|
2012-11-27 00:58:12 -08:00
|
|
|
fd = open(SDL_joylist[device_index].fname, O_RDONLY, 0);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (fd >= 0) {
|
2012-12-11 11:07:48 -05:00
|
|
|
if (ioctl(fd, EVIOCGNAME(sizeof(namebuf)), namebuf) <= 0) {
|
2012-11-27 00:58:12 -08:00
|
|
|
name = SDL_joylist[device_index].fname;
|
2006-07-10 21:04:37 +00:00
|
|
|
} else {
|
|
|
|
name = namebuf;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
return name;
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 00:58:12 -08:00
|
|
|
/* Function to perform the mapping from device index to the instance id for this index */
|
|
|
|
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
|
|
|
|
{
|
|
|
|
return device_index;
|
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
static int
|
|
|
|
allocate_hatdata(SDL_Joystick * joystick)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
joystick->hwdata->hats =
|
|
|
|
(struct hwdata_hat *) SDL_malloc(joystick->nhats *
|
|
|
|
sizeof(struct hwdata_hat));
|
|
|
|
if (joystick->hwdata->hats == NULL) {
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
for (i = 0; i < joystick->nhats; ++i) {
|
|
|
|
joystick->hwdata->hats[i].axis[0] = 1;
|
|
|
|
joystick->hwdata->hats[i].axis[1] = 1;
|
|
|
|
}
|
|
|
|
return (0);
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
static int
|
|
|
|
allocate_balldata(SDL_Joystick * joystick)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
joystick->hwdata->balls =
|
|
|
|
(struct hwdata_ball *) SDL_malloc(joystick->nballs *
|
|
|
|
sizeof(struct hwdata_ball));
|
|
|
|
if (joystick->hwdata->balls == NULL) {
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
for (i = 0; i < joystick->nballs; ++i) {
|
|
|
|
joystick->hwdata->balls[i].axis[0] = 0;
|
|
|
|
joystick->hwdata->balls[i].axis[1] = 0;
|
|
|
|
}
|
|
|
|
return (0);
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 11:07:48 -05:00
|
|
|
static void
|
|
|
|
ConfigJoystick(SDL_Joystick * joystick, int fd)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
int i, t;
|
2009-10-18 16:14:35 +00:00
|
|
|
unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
|
|
|
|
unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
|
|
|
|
unsigned long relbit[NBITS(REL_MAX)] = { 0 };
|
2006-07-10 21:04:37 +00:00
|
|
|
|
|
|
|
/* See if this device uses the new unified event API */
|
|
|
|
if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
|
|
|
|
(ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) >= 0) &&
|
|
|
|
(ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) >= 0)) {
|
|
|
|
|
|
|
|
/* Get the number of buttons, axes, and other thingamajigs */
|
|
|
|
for (i = BTN_JOYSTICK; i < KEY_MAX; ++i) {
|
|
|
|
if (test_bit(i, keybit)) {
|
2001-04-26 16:45:43 +00:00
|
|
|
#ifdef DEBUG_INPUT_EVENTS
|
2006-07-10 21:04:37 +00:00
|
|
|
printf("Joystick has button: 0x%x\n", i);
|
2001-04-26 16:45:43 +00:00
|
|
|
#endif
|
2006-07-10 21:04:37 +00:00
|
|
|
joystick->hwdata->key_map[i - BTN_MISC] = joystick->nbuttons;
|
|
|
|
++joystick->nbuttons;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = BTN_MISC; i < BTN_JOYSTICK; ++i) {
|
|
|
|
if (test_bit(i, keybit)) {
|
2001-04-26 16:45:43 +00:00
|
|
|
#ifdef DEBUG_INPUT_EVENTS
|
2006-07-10 21:04:37 +00:00
|
|
|
printf("Joystick has button: 0x%x\n", i);
|
2001-04-26 16:45:43 +00:00
|
|
|
#endif
|
2006-07-10 21:04:37 +00:00
|
|
|
joystick->hwdata->key_map[i - BTN_MISC] = joystick->nbuttons;
|
|
|
|
++joystick->nbuttons;
|
|
|
|
}
|
|
|
|
}
|
2011-07-13 17:38:09 -07:00
|
|
|
for (i = 0; i < ABS_MISC; ++i) {
|
2006-07-10 21:04:37 +00:00
|
|
|
/* Skip hats */
|
|
|
|
if (i == ABS_HAT0X) {
|
|
|
|
i = ABS_HAT3Y;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (test_bit(i, absbit)) {
|
2011-01-24 14:36:12 -08:00
|
|
|
struct input_absinfo absinfo;
|
2006-07-10 21:04:37 +00:00
|
|
|
|
2011-01-24 15:10:16 -08:00
|
|
|
if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0)
|
2006-07-10 21:04:37 +00:00
|
|
|
continue;
|
2001-04-26 16:45:43 +00:00
|
|
|
#ifdef DEBUG_INPUT_EVENTS
|
2006-07-10 21:04:37 +00:00
|
|
|
printf("Joystick has absolute axis: %x\n", i);
|
|
|
|
printf("Values = { %d, %d, %d, %d, %d }\n",
|
2011-01-24 14:36:12 -08:00
|
|
|
absinfo.value, absinfo.minimum, absinfo.maximum,
|
|
|
|
absinfo.fuzz, absinfo.flat);
|
2001-04-26 16:45:43 +00:00
|
|
|
#endif /* DEBUG_INPUT_EVENTS */
|
2006-07-10 21:04:37 +00:00
|
|
|
joystick->hwdata->abs_map[i] = joystick->naxes;
|
2011-01-24 14:36:12 -08:00
|
|
|
if (absinfo.minimum == absinfo.maximum) {
|
2006-07-10 21:04:37 +00:00
|
|
|
joystick->hwdata->abs_correct[i].used = 0;
|
|
|
|
} else {
|
|
|
|
joystick->hwdata->abs_correct[i].used = 1;
|
|
|
|
joystick->hwdata->abs_correct[i].coef[0] =
|
2011-01-24 14:36:12 -08:00
|
|
|
(absinfo.maximum + absinfo.minimum) / 2 - absinfo.flat;
|
2006-07-10 21:04:37 +00:00
|
|
|
joystick->hwdata->abs_correct[i].coef[1] =
|
2011-01-24 14:36:12 -08:00
|
|
|
(absinfo.maximum + absinfo.minimum) / 2 + absinfo.flat;
|
|
|
|
t = ((absinfo.maximum - absinfo.minimum) / 2 - 2 * absinfo.flat);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (t != 0) {
|
|
|
|
joystick->hwdata->abs_correct[i].coef[2] =
|
|
|
|
(1 << 29) / t;
|
|
|
|
} else {
|
|
|
|
joystick->hwdata->abs_correct[i].coef[2] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++joystick->naxes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = ABS_HAT0X; i <= ABS_HAT3Y; i += 2) {
|
|
|
|
if (test_bit(i, absbit) || test_bit(i + 1, absbit)) {
|
2001-04-26 16:45:43 +00:00
|
|
|
#ifdef DEBUG_INPUT_EVENTS
|
2006-07-10 21:04:37 +00:00
|
|
|
printf("Joystick has hat %d\n", (i - ABS_HAT0X) / 2);
|
2001-04-26 16:45:43 +00:00
|
|
|
#endif
|
2006-07-10 21:04:37 +00:00
|
|
|
++joystick->nhats;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (test_bit(REL_X, relbit) || test_bit(REL_Y, relbit)) {
|
|
|
|
++joystick->nballs;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate data to keep track of these thingamajigs */
|
|
|
|
if (joystick->nhats > 0) {
|
|
|
|
if (allocate_hatdata(joystick) < 0) {
|
|
|
|
joystick->nhats = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (joystick->nballs > 0) {
|
|
|
|
if (allocate_balldata(joystick) < 0) {
|
|
|
|
joystick->nballs = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
Date: Sun, 18 Apr 2004 16:09:53 -0400 (EDT)
From: David MacCormack
Subject: [SDL] Linux joystick patch
I recently got myself a PS2 -> USB converter (a super joybox 5). It
accepts 4 PSX/PS2 controllers. It's implemented as a HID, which is nice
because it doesn't require its own driver, but the problem is that it's
implemented as a *single* HID -- that is, it shows up as a single
joystick with 19 axes, 4 hats, and 48 buttons. This poses a problem for a
number of apps which use SDL (stella, fce ultra, zsnes, to name a few) and
see only a single (physical) joystick even though there are really 4
(logical) joysticks. There are a number of these types of devices on the
market, and I've seen others post messages (in the zsnes forum, for
example) with the same problem, so I came up with what I think is a pretty
generic solution.
I patched src/joystick/linux/SDL_sysjoystic.c to include support for
logical joysticks; basically, it's a static array and supporting functions
that map a single physical joystick to multiple logical joysticks. The
attached patch has the new code. It's wrapped inside #ifndef
statements so that you can get the old behavior if you want.
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%40893
2004-05-16 18:46:24 +00:00
|
|
|
|
2001-04-26 16:45:43 +00:00
|
|
|
/* Function to open a joystick for use.
|
|
|
|
The joystick to open is specified by the index field of the joystick.
|
|
|
|
This should fill the nbuttons and naxes fields of the joystick structure.
|
|
|
|
It returns 0, or -1 if there is an error.
|
|
|
|
*/
|
2006-07-10 21:04:37 +00:00
|
|
|
int
|
2012-11-26 16:37:54 -08:00
|
|
|
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
int fd;
|
2008-08-25 09:55:03 +00:00
|
|
|
char *fname;
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
/* Open the joystick and set the joystick file descriptor */
|
2012-11-26 16:37:54 -08:00
|
|
|
fd = open(SDL_joylist[joystick->instance_id].fname, O_RDONLY, 0);
|
|
|
|
fname = SDL_joylist[joystick->instance_id].fname;
|
Date: Sun, 18 Apr 2004 16:09:53 -0400 (EDT)
From: David MacCormack
Subject: [SDL] Linux joystick patch
I recently got myself a PS2 -> USB converter (a super joybox 5). It
accepts 4 PSX/PS2 controllers. It's implemented as a HID, which is nice
because it doesn't require its own driver, but the problem is that it's
implemented as a *single* HID -- that is, it shows up as a single
joystick with 19 axes, 4 hats, and 48 buttons. This poses a problem for a
number of apps which use SDL (stella, fce ultra, zsnes, to name a few) and
see only a single (physical) joystick even though there are really 4
(logical) joysticks. There are a number of these types of devices on the
market, and I've seen others post messages (in the zsnes forum, for
example) with the same problem, so I came up with what I think is a pretty
generic solution.
I patched src/joystick/linux/SDL_sysjoystic.c to include support for
logical joysticks; basically, it's a static array and supporting functions
that map a single physical joystick to multiple logical joysticks. The
attached patch has the new code. It's wrapped inside #ifndef
statements so that you can get the old behavior if you want.
--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%40893
2004-05-16 18:46:24 +00:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
if (fd < 0) {
|
2012-11-26 16:37:54 -08:00
|
|
|
SDL_SetError("Unable to open %s\n", SDL_joylist[joystick->instance_id]);
|
2006-07-10 21:04:37 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2012-11-26 22:27:49 -08:00
|
|
|
joystick->instance_id = device_index;
|
2006-07-10 21:04:37 +00:00
|
|
|
joystick->hwdata = (struct joystick_hwdata *)
|
|
|
|
SDL_malloc(sizeof(*joystick->hwdata));
|
|
|
|
if (joystick->hwdata == NULL) {
|
|
|
|
SDL_OutOfMemory();
|
|
|
|
close(fd);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
SDL_memset(joystick->hwdata, 0, sizeof(*joystick->hwdata));
|
|
|
|
joystick->hwdata->fd = fd;
|
2008-08-25 09:55:03 +00:00
|
|
|
joystick->hwdata->fname = fname;
|
2006-07-10 21:04:37 +00:00
|
|
|
|
|
|
|
/* Set the joystick to non-blocking read mode */
|
|
|
|
fcntl(fd, F_SETFL, O_NONBLOCK);
|
|
|
|
|
|
|
|
/* Get the number of buttons and axes on the joystick */
|
2012-12-11 11:07:48 -05:00
|
|
|
ConfigJoystick(joystick, fd);
|
2002-12-02 03:11:36 +00:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
return (0);
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 00:58:12 -08:00
|
|
|
/* Function to determine is this joystick is attached to the system right now */
|
|
|
|
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
|
|
|
|
{
|
|
|
|
return SDL_TRUE;
|
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
static __inline__ void
|
|
|
|
HandleHat(SDL_Joystick * stick, Uint8 hat, int axis, int value)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
struct hwdata_hat *the_hat;
|
|
|
|
const Uint8 position_map[3][3] = {
|
|
|
|
{SDL_HAT_LEFTUP, SDL_HAT_UP, SDL_HAT_RIGHTUP},
|
|
|
|
{SDL_HAT_LEFT, SDL_HAT_CENTERED, SDL_HAT_RIGHT},
|
|
|
|
{SDL_HAT_LEFTDOWN, SDL_HAT_DOWN, SDL_HAT_RIGHTDOWN}
|
|
|
|
};
|
|
|
|
|
|
|
|
the_hat = &stick->hwdata->hats[hat];
|
|
|
|
if (value < 0) {
|
|
|
|
value = 0;
|
|
|
|
} else if (value == 0) {
|
|
|
|
value = 1;
|
|
|
|
} else if (value > 0) {
|
|
|
|
value = 2;
|
|
|
|
}
|
|
|
|
if (value != the_hat->axis[axis]) {
|
|
|
|
the_hat->axis[axis] = value;
|
|
|
|
SDL_PrivateJoystickHat(stick, hat,
|
2009-01-10 21:50:26 +00:00
|
|
|
position_map[the_hat->
|
|
|
|
axis[1]][the_hat->axis[0]]);
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
static __inline__ void
|
|
|
|
HandleBall(SDL_Joystick * stick, Uint8 ball, int axis, int value)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
stick->hwdata->balls[ball].axis[axis] += value;
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
|
|
|
|
static __inline__ int
|
2012-12-11 11:07:48 -05:00
|
|
|
AxisCorrect(SDL_Joystick * joystick, int which, int value)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
struct axis_correct *correct;
|
|
|
|
|
|
|
|
correct = &joystick->hwdata->abs_correct[which];
|
|
|
|
if (correct->used) {
|
|
|
|
if (value > correct->coef[0]) {
|
|
|
|
if (value < correct->coef[1]) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
value -= correct->coef[1];
|
|
|
|
} else {
|
|
|
|
value -= correct->coef[0];
|
|
|
|
}
|
|
|
|
value *= correct->coef[2];
|
|
|
|
value >>= 14;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clamp and return */
|
|
|
|
if (value < -32768)
|
|
|
|
return -32768;
|
|
|
|
if (value > 32767)
|
|
|
|
return 32767;
|
|
|
|
|
|
|
|
return value;
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
static __inline__ void
|
2012-12-11 11:07:48 -05:00
|
|
|
HandleInputEvents(SDL_Joystick * joystick)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
struct input_event events[32];
|
|
|
|
int i, len;
|
|
|
|
int code;
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
while ((len = read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
|
|
|
|
len /= sizeof(events[0]);
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
code = events[i].code;
|
|
|
|
switch (events[i].type) {
|
|
|
|
case EV_KEY:
|
|
|
|
if (code >= BTN_MISC) {
|
|
|
|
code -= BTN_MISC;
|
2012-12-10 15:50:42 -05:00
|
|
|
SDL_PrivateJoystickButton(joystick,
|
|
|
|
joystick->hwdata->key_map[code],
|
|
|
|
events[i].value);
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EV_ABS:
|
2012-06-03 05:05:34 -04:00
|
|
|
if (code >= ABS_MISC) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
switch (code) {
|
|
|
|
case ABS_HAT0X:
|
|
|
|
case ABS_HAT0Y:
|
|
|
|
case ABS_HAT1X:
|
|
|
|
case ABS_HAT1Y:
|
|
|
|
case ABS_HAT2X:
|
|
|
|
case ABS_HAT2Y:
|
|
|
|
case ABS_HAT3X:
|
|
|
|
case ABS_HAT3Y:
|
|
|
|
code -= ABS_HAT0X;
|
|
|
|
HandleHat(joystick, code / 2, code % 2, events[i].value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
events[i].value =
|
2012-12-11 11:07:48 -05:00
|
|
|
AxisCorrect(joystick, code, events[i].value);
|
2012-12-10 15:50:42 -05:00
|
|
|
SDL_PrivateJoystickAxis(joystick,
|
|
|
|
joystick->hwdata->abs_map[code],
|
|
|
|
events[i].value);
|
2006-07-10 21:04:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EV_REL:
|
|
|
|
switch (code) {
|
|
|
|
case REL_X:
|
|
|
|
case REL_Y:
|
|
|
|
code -= REL_X;
|
|
|
|
HandleBall(joystick, code / 2, code % 2, events[i].value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
void
|
|
|
|
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
int i;
|
|
|
|
|
2012-12-11 11:07:48 -05:00
|
|
|
HandleInputEvents(joystick);
|
2006-07-10 21:04:37 +00:00
|
|
|
|
|
|
|
/* Deliver ball motion updates */
|
|
|
|
for (i = 0; i < joystick->nballs; ++i) {
|
|
|
|
int xrel, yrel;
|
|
|
|
|
|
|
|
xrel = joystick->hwdata->balls[i].axis[0];
|
|
|
|
yrel = joystick->hwdata->balls[i].axis[1];
|
|
|
|
if (xrel || yrel) {
|
|
|
|
joystick->hwdata->balls[i].axis[0] = 0;
|
|
|
|
joystick->hwdata->balls[i].axis[1] = 0;
|
|
|
|
SDL_PrivateJoystickBall(joystick, (Uint8) i, xrel, yrel);
|
|
|
|
}
|
|
|
|
}
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Function to close a joystick after use */
|
2006-07-10 21:04:37 +00:00
|
|
|
void
|
|
|
|
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
if (joystick->hwdata) {
|
2012-12-10 15:50:42 -05:00
|
|
|
close(joystick->hwdata->fd);
|
2006-07-10 21:04:37 +00:00
|
|
|
if (joystick->hwdata->hats) {
|
|
|
|
SDL_free(joystick->hwdata->hats);
|
|
|
|
}
|
|
|
|
if (joystick->hwdata->balls) {
|
|
|
|
SDL_free(joystick->hwdata->balls);
|
|
|
|
}
|
|
|
|
SDL_free(joystick->hwdata);
|
|
|
|
joystick->hwdata = NULL;
|
|
|
|
}
|
2012-11-26 16:37:54 -08:00
|
|
|
joystick->closed = 1;
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Function to perform any system-specific joystick related cleanup */
|
2006-07-10 21:04:37 +00:00
|
|
|
void
|
|
|
|
SDL_SYS_JoystickQuit(void)
|
2001-04-26 16:45:43 +00:00
|
|
|
{
|
2006-07-10 21:04:37 +00:00
|
|
|
int i;
|
2001-04-26 16:45:43 +00:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
for (i = 0; SDL_joylist[i].fname; ++i) {
|
2009-03-06 05:53:33 +00:00
|
|
|
if (SDL_joylist[i].fname) {
|
|
|
|
SDL_free(SDL_joylist[i].fname);
|
|
|
|
SDL_joylist[i].fname = NULL;
|
|
|
|
}
|
2006-07-10 21:04:37 +00:00
|
|
|
}
|
2001-04-26 16:45:43 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 00:58:12 -08:00
|
|
|
JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
|
2012-11-26 16:37:54 -08:00
|
|
|
{
|
2012-11-26 21:11:28 -08:00
|
|
|
JoystickGUID guid;
|
2012-11-26 16:37:54 -08:00
|
|
|
// the GUID is just the first 16 chars of the name for now
|
2012-11-27 00:58:12 -08:00
|
|
|
const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
|
2012-11-26 21:11:28 -08:00
|
|
|
SDL_zero( guid );
|
|
|
|
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
|
2012-11-26 16:37:54 -08:00
|
|
|
return guid;
|
|
|
|
}
|
|
|
|
|
2012-11-27 00:58:12 -08:00
|
|
|
JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
|
2012-11-26 16:37:54 -08:00
|
|
|
{
|
2012-11-26 21:11:28 -08:00
|
|
|
JoystickGUID guid;
|
2012-11-26 16:37:54 -08:00
|
|
|
// the GUID is just the first 16 chars of the name for now
|
2012-11-26 21:11:28 -08:00
|
|
|
const char *name = joystick->name;
|
|
|
|
SDL_zero( guid );
|
|
|
|
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
|
2012-11-26 16:37:54 -08:00
|
|
|
return guid;
|
|
|
|
}
|
|
|
|
|
2006-04-14 04:46:47 +00:00
|
|
|
#endif /* SDL_JOYSTICK_LINUX */
|
2012-11-26 21:11:28 -08:00
|
|
|
|
2006-07-10 21:04:37 +00:00
|
|
|
/* vi: set ts=4 sw=4 expandtab: */
|