From dec7875a6e23212021e4d9080330a42832dfe02a Mon Sep 17 00:00:00 2001
From: Edward Rudd <urkle@outoforder.cc>
Date: Tue, 15 Jun 2021 01:40:19 +0000
Subject: [PATCH] update SDL soruce to 2.0.14
---
source/src/joystick/linux/SDL_sysjoystick.c | 716 +++++++++++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 590 insertions(+), 126 deletions(-)
diff --git a/source/src/joystick/linux/SDL_sysjoystick.c b/source/src/joystick/linux/SDL_sysjoystick.c
index fcfc0f3..ee4a8c1 100644
--- a/source/src/joystick/linux/SDL_sysjoystick.c
+++ b/source/src/joystick/linux/SDL_sysjoystick.c
@@ -32,13 +32,17 @@
#include <errno.h> /* errno, strerror */
#include <fcntl.h>
#include <limits.h> /* For the definition of PATH_MAX */
+#ifdef HAVE_INOTIFY
+#include <sys/inotify.h>
+#endif
#include <sys/ioctl.h>
#include <unistd.h>
#include <dirent.h>
#include <linux/joystick.h>
-#include "SDL_assert.h"
+#include "SDL_hints.h"
#include "SDL_joystick.h"
+#include "SDL_log.h"
#include "SDL_endian.h"
#include "SDL_timer.h"
#include "../../events/SDL_events_c.h"
@@ -52,13 +56,49 @@
#ifndef SYN_DROPPED
#define SYN_DROPPED 3
#endif
+#ifndef BTN_SOUTH
+#define BTN_SOUTH 0x130
+#endif
+#ifndef BTN_EAST
+#define BTN_EAST 0x131
+#endif
+#ifndef BTN_NORTH
+#define BTN_NORTH 0x133
+#endif
+#ifndef BTN_WEST
+#define BTN_WEST 0x134
+#endif
+#ifndef BTN_DPAD_UP
+#define BTN_DPAD_UP 0x220
+#endif
+#ifndef BTN_DPAD_DOWN
+#define BTN_DPAD_DOWN 0x221
+#endif
+#ifndef BTN_DPAD_LEFT
+#define BTN_DPAD_LEFT 0x222
+#endif
+#ifndef BTN_DPAD_RIGHT
+#define BTN_DPAD_RIGHT 0x223
+#endif
+#include "../../core/linux/SDL_evdev_capabilities.h"
#include "../../core/linux/SDL_udev.h"
+#if 0
+#define DEBUG_INPUT_EVENTS 1
+#endif
+
+typedef enum
+{
+ ENUMERATION_UNSET,
+ ENUMERATION_LIBUDEV,
+ ENUMERATION_FALLBACK
+} EnumerationMethod;
+
+static EnumerationMethod enumeration_method = ENUMERATION_UNSET;
+
static int MaybeAddDevice(const char *path);
-#if SDL_USE_LIBUDEV
static int MaybeRemoveDevice(const char *path);
-#endif /* SDL_USE_LIBUDEV */
/* A linked list of available joysticks */
typedef struct SDL_joylist_item
@@ -78,29 +118,10 @@
static SDL_joylist_item *SDL_joylist = NULL;
static SDL_joylist_item *SDL_joylist_tail = NULL;
static int numjoysticks = 0;
+static int inotify_fd = -1;
-#if !SDL_USE_LIBUDEV
static Uint32 last_joy_detect_time;
static time_t last_input_dir_mtime;
-#endif
-
-#define test_bit(nr, addr) \
- (((1UL << ((nr) % (sizeof(long) * 8))) & ((addr)[(nr) / (sizeof(long) * 8)])) != 0)
-#define NBITS(x) ((((x)-1)/(sizeof(long) * 8))+1)
-
-static int
-PrefixMatch(const char *a, const char *b)
-{
- int matchlen = 0;
- while (*a && *b) {
- if (*a++ == *b++) {
- ++matchlen;
- } else {
- break;
- }
- }
- return matchlen;
-}
static void
FixupDeviceInfoForMapping(int fd, struct input_id *inpid)
@@ -118,58 +139,75 @@
}
}
+#ifdef SDL_JOYSTICK_HIDAPI
+static SDL_bool
+IsVirtualJoystick(Uint16 vendor, Uint16 product, Uint16 version, const char *name)
+{
+ if (vendor == USB_VENDOR_MICROSOFT && product == USB_PRODUCT_XBOX_ONE_S && version == 0 &&
+ SDL_strcmp(name, "Xbox One S Controller") == 0) {
+ /* This is the virtual device created by the xow driver */
+ return SDL_TRUE;
+ }
+ return SDL_FALSE;
+}
+#endif /* SDL_JOYSTICK_HIDAPI */
static int
-IsJoystick(int fd, char *namebuf, const size_t namebuflen, SDL_JoystickGUID *guid)
+GuessIsJoystick(int fd)
{
- struct input_id inpid;
- Uint16 *guid16 = (Uint16 *)guid->data;
- const char *name;
- const char *spot;
-
-#if !SDL_USE_LIBUDEV
- /* When udev is enabled we only get joystick devices here, so there's no need to test them */
unsigned long evbit[NBITS(EV_MAX)] = { 0 };
unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
+ unsigned long relbit[NBITS(REL_MAX)] = { 0 };
+ int devclass;
if ((ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), evbit) < 0) ||
(ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) < 0) ||
+ (ioctl(fd, EVIOCGBIT(EV_REL, sizeof(relbit)), relbit) < 0) ||
(ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absbit)), absbit) < 0)) {
return (0);
}
- if (!(test_bit(EV_KEY, evbit) && test_bit(EV_ABS, evbit) &&
- test_bit(ABS_X, absbit) && test_bit(ABS_Y, absbit))) {
+ devclass = SDL_EVDEV_GuessDeviceClass(evbit, absbit, keybit, relbit);
+
+ if (devclass & SDL_UDEV_DEVICE_JOYSTICK) {
+ return 1;
+ }
+
+ return 0;
+}
+
+static int
+IsJoystick(int fd, char **name_return, SDL_JoystickGUID *guid)
+{
+ struct input_id inpid;
+ Uint16 *guid16 = (Uint16 *)guid->data;
+ char *name;
+ char product_string[128];
+
+ /* When udev is enabled we only get joystick devices here, so there's no need to test them */
+ if (enumeration_method != ENUMERATION_LIBUDEV && !GuessIsJoystick(fd)) {
return 0;
}
-#endif
if (ioctl(fd, EVIOCGID, &inpid) < 0) {
return 0;
}
- name = SDL_GetCustomJoystickName(inpid.vendor, inpid.product);
- if (name) {
- SDL_strlcpy(namebuf, name, namebuflen);
- } else {
- if (ioctl(fd, EVIOCGNAME(namebuflen), namebuf) < 0) {
- return 0;
- }
+ if (ioctl(fd, EVIOCGNAME(sizeof(product_string)), product_string) < 0) {
+ return 0;
+ }
- /* Remove duplicate manufacturer in the name */
- for (spot = namebuf + 1; *spot; ++spot) {
- int matchlen = PrefixMatch(namebuf, spot);
- if (matchlen > 0 && spot[matchlen - 1] == ' ') {
- SDL_memmove(namebuf, spot, SDL_strlen(spot)+1);
- break;
- }
- }
+ name = SDL_CreateJoystickName(inpid.vendor, inpid.product, NULL, product_string);
+ if (!name) {
+ return 0;
}
#ifdef SDL_JOYSTICK_HIDAPI
- if (HIDAPI_IsDevicePresent(inpid.vendor, inpid.product, inpid.version, namebuf)) {
+ if (!IsVirtualJoystick(inpid.vendor, inpid.product, inpid.version, name) &&
+ HIDAPI_IsDevicePresent(inpid.vendor, inpid.product, inpid.version, name)) {
/* The HIDAPI driver is taking care of this device */
+ SDL_free(name);
return 0;
}
#endif
@@ -177,7 +215,7 @@
FixupDeviceInfoForMapping(fd, &inpid);
#ifdef DEBUG_JOYSTICK
- printf("Joystick: %s, bustype = %d, vendor = 0x%.4x, product = 0x%.4x, version = %d\n", namebuf, inpid.bustype, inpid.vendor, inpid.product, inpid.version);
+ printf("Joystick: %s, bustype = %d, vendor = 0x%.4x, product = 0x%.4x, version = %d\n", name, inpid.bustype, inpid.vendor, inpid.product, inpid.version);
#endif
SDL_memset(guid->data, 0, sizeof(guid->data));
@@ -195,12 +233,14 @@
*guid16++ = SDL_SwapLE16(inpid.version);
*guid16++ = 0;
} else {
- SDL_strlcpy((char*)guid16, namebuf, sizeof(guid->data) - 4);
+ SDL_strlcpy((char*)guid16, name, sizeof(guid->data) - 4);
}
- if (SDL_ShouldIgnoreJoystick(namebuf, *guid)) {
+ if (SDL_ShouldIgnoreJoystick(name, *guid)) {
+ SDL_free(name);
return 0;
}
+ *name_return = name;
return 1;
}
@@ -236,7 +276,7 @@
struct stat sb;
int fd = -1;
int isstick = 0;
- char namebuf[128];
+ char *name = NULL;
SDL_JoystickGUID guid;
SDL_joylist_item *item;
@@ -264,7 +304,7 @@
printf("Checking %s\n", path);
#endif
- isstick = IsJoystick(fd, namebuf, sizeof (namebuf), &guid);
+ isstick = IsJoystick(fd, &name, &guid);
close(fd);
if (!isstick) {
return -1;
@@ -278,7 +318,7 @@
SDL_zerop(item);
item->devnum = sb.st_rdev;
item->path = SDL_strdup(path);
- item->name = SDL_strdup(namebuf);
+ item->name = name;
item->guid = guid;
if ((item->path == NULL) || (item->name == NULL)) {
@@ -304,7 +344,6 @@
return numjoysticks;
}
-#if SDL_USE_LIBUDEV
static int
MaybeRemoveDevice(const char *path)
{
@@ -347,7 +386,6 @@
return -1;
}
-#endif
static void
HandlePendingRemovals(void)
@@ -461,12 +499,98 @@
}
}
-static void
-LINUX_JoystickDetect(void)
-{
-#if SDL_USE_LIBUDEV
- SDL_UDEV_Poll();
+#ifdef HAVE_INOTIFY
+#ifdef HAVE_INOTIFY_INIT1
+static int SDL_inotify_init1(void) {
+ return inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
+}
#else
+static int SDL_inotify_init1(void) {
+ int fd = inotify_init();
+ if (fd < 0) return -1;
+ fcntl(fd, F_SETFL, O_NONBLOCK);
+ fcntl(fd, F_SETFD, FD_CLOEXEC);
+ return fd;
+}
+#endif
+
+static int
+StrHasPrefix(const char *string, const char *prefix)
+{
+ return (SDL_strncmp(string, prefix, SDL_strlen(prefix)) == 0);
+}
+
+static int
+StrIsInteger(const char *string)
+{
+ const char *p;
+
+ if (*string == '\0') {
+ return 0;
+ }
+
+ for (p = string; *p != '\0'; p++) {
+ if (*p < '0' || *p > '9') {
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
+static void
+LINUX_InotifyJoystickDetect(void)
+{
+ union
+ {
+ struct inotify_event event;
+ char storage[4096];
+ char enough_for_inotify[sizeof (struct inotify_event) + NAME_MAX + 1];
+ } buf;
+ ssize_t bytes;
+ size_t remain = 0;
+ size_t len;
+
+ bytes = read(inotify_fd, &buf, sizeof (buf));
+
+ if (bytes > 0) {
+ remain = (size_t) bytes;
+ }
+
+ while (remain > 0) {
+ if (buf.event.len > 0) {
+ if (StrHasPrefix(buf.event.name, "event") &&
+ StrIsInteger(buf.event.name + strlen ("event"))) {
+ char path[PATH_MAX];
+
+ SDL_snprintf(path, SDL_arraysize(path), "/dev/input/%s", buf.event.name);
+
+ if (buf.event.mask & (IN_CREATE | IN_MOVED_TO | IN_ATTRIB)) {
+ MaybeAddDevice(path);
+ }
+ else if (buf.event.mask & (IN_DELETE | IN_MOVED_FROM)) {
+ MaybeRemoveDevice(path);
+ }
+ }
+ }
+
+ len = sizeof (struct inotify_event) + buf.event.len;
+ remain -= len;
+
+ if (remain != 0) {
+ memmove (&buf.storage[0], &buf.storage[len], remain);
+ }
+ }
+}
+#endif /* HAVE_INOTIFY */
+
+/* Detect devices by reading /dev/input. In the inotify code path we
+ * have to do this the first time, to detect devices that already existed
+ * before we started; in the non-inotify code path we do this repeatedly
+ * (polling). */
+static void
+LINUX_FallbackJoystickDetect(void)
+{
const Uint32 SDL_JOY_DETECT_INTERVAL_MS = 3000; /* Update every 3 seconds */
Uint32 now = SDL_GetTicks();
@@ -497,7 +621,26 @@
last_joy_detect_time = now;
}
+}
+
+static void
+LINUX_JoystickDetect(void)
+{
+#if SDL_USE_LIBUDEV
+ if (enumeration_method == ENUMERATION_LIBUDEV) {
+ SDL_UDEV_Poll();
+ }
+ else
#endif
+#ifdef HAVE_INOTIFY
+ if (inotify_fd >= 0 && last_joy_detect_time != 0) {
+ LINUX_InotifyJoystickDetect();
+ }
+ else
+#endif
+ {
+ LINUX_FallbackJoystickDetect();
+ }
HandlePendingRemovals();
@@ -507,6 +650,27 @@
static int
LINUX_JoystickInit(void)
{
+#if SDL_USE_LIBUDEV
+ if (enumeration_method == ENUMERATION_UNSET) {
+ if (SDL_getenv("SDL_JOYSTICK_DISABLE_UDEV") != NULL) {
+ SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
+ "udev disabled by SDL_JOYSTICK_DISABLE_UDEV");
+ enumeration_method = ENUMERATION_FALLBACK;
+ }
+ else if (access("/.flatpak-info", F_OK) == 0
+ || access("/run/pressure-vessel", F_OK) == 0) {
+ SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
+ "Container detected, disabling udev integration");
+ enumeration_method = ENUMERATION_FALLBACK;
+ }
+ else {
+ SDL_LogDebug(SDL_LOG_CATEGORY_INPUT,
+ "Using udev for joystick device discovery");
+ enumeration_method = ENUMERATION_LIBUDEV;
+ }
+ }
+#endif
+
/* First see if the user specified one or more joysticks to use */
if (SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL) {
char *envcopy, *envpath, *delim;
@@ -526,27 +690,55 @@
SDL_InitSteamControllers(SteamControllerConnectedCallback,
SteamControllerDisconnectedCallback);
-#if SDL_USE_LIBUDEV
- if (SDL_UDEV_Init() < 0) {
- return SDL_SetError("Could not initialize UDEV");
- }
-
- /* Set up the udev callback */
- if (SDL_UDEV_AddCallback(joystick_udev_callback) < 0) {
- SDL_UDEV_Quit();
- return SDL_SetError("Could not set up joystick <-> udev callback");
- }
-
- /* Force a scan to build the initial device list */
- SDL_UDEV_Scan();
-#else
- /* Force immediate joystick detection */
+ /* Force immediate joystick detection if using fallback */
last_joy_detect_time = 0;
last_input_dir_mtime = 0;
- /* Report all devices currently present */
- LINUX_JoystickDetect();
+#if SDL_USE_LIBUDEV
+ if (enumeration_method == ENUMERATION_LIBUDEV) {
+ if (SDL_UDEV_Init() < 0) {
+ return SDL_SetError("Could not initialize UDEV");
+ }
+
+ /* Set up the udev callback */
+ if (SDL_UDEV_AddCallback(joystick_udev_callback) < 0) {
+ SDL_UDEV_Quit();
+ return SDL_SetError("Could not set up joystick <-> udev callback");
+ }
+
+ /* Force a scan to build the initial device list */
+ SDL_UDEV_Scan();
+ }
+ else
#endif
+ {
+#if defined(HAVE_INOTIFY)
+ inotify_fd = SDL_inotify_init1();
+
+ if (inotify_fd < 0) {
+ SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
+ "Unable to initialize inotify, falling back to polling: %s",
+ strerror (errno));
+ } else {
+ /* We need to watch for attribute changes in addition to
+ * creation, because when a device is first created, it has
+ * permissions that we can't read. When udev chmods it to
+ * something that we maybe *can* read, we'll get an
+ * IN_ATTRIB event to tell us. */
+ if (inotify_add_watch(inotify_fd, "/dev/input",
+ IN_CREATE | IN_DELETE | IN_MOVE | IN_ATTRIB) < 0) {
+ close(inotify_fd);
+ inotify_fd = -1;
+ SDL_LogWarn(SDL_LOG_CATEGORY_INPUT,
+ "Unable to add inotify watch, falling back to polling: %s",
+ strerror (errno));
+ }
+ }
+#endif /* HAVE_INOTIFY */
+
+ /* Report all devices currently present */
+ LINUX_JoystickDetect();
+ }
return 0;
}
@@ -607,7 +799,7 @@
}
static int
-allocate_hatdata(SDL_Joystick * joystick)
+allocate_hatdata(SDL_Joystick *joystick)
{
int i;
@@ -625,7 +817,7 @@
}
static int
-allocate_balldata(SDL_Joystick * joystick)
+allocate_balldata(SDL_Joystick *joystick)
{
int i;
@@ -643,13 +835,14 @@
}
static void
-ConfigJoystick(SDL_Joystick * joystick, int fd)
+ConfigJoystick(SDL_Joystick *joystick, int fd)
{
int i, t;
unsigned long keybit[NBITS(KEY_MAX)] = { 0 };
unsigned long absbit[NBITS(ABS_MAX)] = { 0 };
unsigned long relbit[NBITS(REL_MAX)] = { 0 };
unsigned long ffbit[NBITS(FF_MAX)] = { 0 };
+ SDL_bool use_deadzones = SDL_GetHintBoolean(SDL_HINT_LINUX_JOYSTICK_DEADZONES, SDL_FALSE);
/* See if this device uses the new unified event API */
if ((ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keybit)), keybit) >= 0) &&
@@ -663,6 +856,7 @@
printf("Joystick has button: 0x%x\n", i);
#endif
joystick->hwdata->key_map[i] = joystick->nbuttons;
+ joystick->hwdata->has_key[i] = SDL_TRUE;
++joystick->nbuttons;
}
}
@@ -672,6 +866,7 @@
printf("Joystick has button: 0x%x\n", i);
#endif
joystick->hwdata->key_map[i] = joystick->nbuttons;
+ joystick->hwdata->has_key[i] = SDL_TRUE;
++joystick->nbuttons;
}
}
@@ -683,6 +878,7 @@
}
if (test_bit(i, absbit)) {
struct input_absinfo absinfo;
+ struct axis_correct *correct = &joystick->hwdata->abs_correct[i];
if (ioctl(fd, EVIOCGABS(i), &absinfo) < 0) {
continue;
@@ -694,20 +890,26 @@
absinfo.fuzz, absinfo.flat);
#endif /* DEBUG_INPUT_EVENTS */
joystick->hwdata->abs_map[i] = joystick->naxes;
- if (absinfo.minimum == absinfo.maximum) {
- joystick->hwdata->abs_correct[i].used = 0;
- } else {
- joystick->hwdata->abs_correct[i].used = 1;
- joystick->hwdata->abs_correct[i].coef[0] =
- (absinfo.maximum + absinfo.minimum) - 2 * absinfo.flat;
- joystick->hwdata->abs_correct[i].coef[1] =
- (absinfo.maximum + absinfo.minimum) + 2 * absinfo.flat;
- t = ((absinfo.maximum - absinfo.minimum) - 4 * absinfo.flat);
- if (t != 0) {
- joystick->hwdata->abs_correct[i].coef[2] =
- (1 << 28) / t;
+ joystick->hwdata->has_abs[i] = SDL_TRUE;
+
+ correct->minimum = absinfo.minimum;
+ correct->maximum = absinfo.maximum;
+ if (correct->minimum != correct->maximum) {
+ if (use_deadzones) {
+ correct->use_deadzones = SDL_TRUE;
+ correct->coef[0] = (absinfo.maximum + absinfo.minimum) - 2 * absinfo.flat;
+ correct->coef[1] = (absinfo.maximum + absinfo.minimum) + 2 * absinfo.flat;
+ t = ((absinfo.maximum - absinfo.minimum) - 4 * absinfo.flat);
+ if (t != 0) {
+ correct->coef[2] = (1 << 28) / t;
+ } else {
+ correct->coef[2] = 0;
+ }
} else {
- joystick->hwdata->abs_correct[i].coef[2] = 0;
+ float value_range = (correct->maximum - correct->minimum);
+ float output_range = (SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN);
+
+ correct->scale = (output_range / value_range);
}
}
++joystick->naxes;
@@ -728,6 +930,7 @@
absinfo.fuzz, absinfo.flat);
#endif /* DEBUG_INPUT_EVENTS */
joystick->hwdata->hats_indices[hat_index] = joystick->nhats++;
+ joystick->hwdata->has_hat[hat_index] = SDL_TRUE;
}
}
if (test_bit(REL_X, relbit) || test_bit(REL_Y, relbit)) {
@@ -764,7 +967,7 @@
It returns 0, or -1 if there is an error.
*/
static int
-LINUX_JoystickOpen(SDL_Joystick * joystick, int device_index)
+LINUX_JoystickOpen(SDL_Joystick *joystick, int device_index)
{
SDL_joylist_item *item = JoystickByDevIndex(device_index);
@@ -817,13 +1020,13 @@
item->hwdata = joystick->hwdata;
/* mark joystick as fresh and ready */
- joystick->hwdata->fresh = 1;
+ joystick->hwdata->fresh = SDL_TRUE;
return (0);
}
static int
-LINUX_JoystickRumble(SDL_Joystick * joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
+LINUX_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
{
struct input_event event;
@@ -864,8 +1067,32 @@
return 0;
}
+static int
+LINUX_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
+{
+ return SDL_Unsupported();
+}
+
+static SDL_bool
+LINUX_JoystickHasLED(SDL_Joystick *joystick)
+{
+ return SDL_FALSE;
+}
+
+static int
+LINUX_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
+{
+ return SDL_Unsupported();
+}
+
+static int
+LINUX_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
+{
+ return SDL_Unsupported();
+}
+
static SDL_INLINE void
-HandleHat(SDL_Joystick * stick, Uint8 hat, int axis, int value)
+HandleHat(SDL_Joystick *stick, Uint8 hat, int axis, int value)
{
struct hwdata_hat *the_hat;
const Uint8 position_map[3][3] = {
@@ -890,54 +1117,60 @@
}
static SDL_INLINE void
-HandleBall(SDL_Joystick * stick, Uint8 ball, int axis, int value)
+HandleBall(SDL_Joystick *stick, Uint8 ball, int axis, int value)
{
stick->hwdata->balls[ball].axis[axis] += value;
}
static SDL_INLINE int
-AxisCorrect(SDL_Joystick * joystick, int which, int value)
+AxisCorrect(SDL_Joystick *joystick, int which, int value)
{
struct axis_correct *correct;
correct = &joystick->hwdata->abs_correct[which];
- if (correct->used) {
- value *= 2;
- if (value > correct->coef[0]) {
- if (value < correct->coef[1]) {
- return 0;
+ if (correct->minimum != correct->maximum) {
+ if (correct->use_deadzones) {
+ value *= 2;
+ if (value > correct->coef[0]) {
+ if (value < correct->coef[1]) {
+ return 0;
+ }
+ value -= correct->coef[1];
+ } else {
+ value -= correct->coef[0];
}
- value -= correct->coef[1];
+ value *= correct->coef[2];
+ value >>= 13;
} else {
- value -= correct->coef[0];
+ value = (int)SDL_floorf((value - correct->minimum) * correct->scale + SDL_JOYSTICK_AXIS_MIN + 0.5f);
}
- value *= correct->coef[2];
- value >>= 13;
}
/* Clamp and return */
- if (value < -32768)
- return -32768;
- if (value > 32767)
- return 32767;
-
+ if (value < SDL_JOYSTICK_AXIS_MIN) {
+ return SDL_JOYSTICK_AXIS_MIN;
+ }
+ if (value > SDL_JOYSTICK_AXIS_MAX) {
+ return SDL_JOYSTICK_AXIS_MAX;
+ }
return value;
}
static SDL_INLINE void
-PollAllValues(SDL_Joystick * joystick)
+PollAllValues(SDL_Joystick *joystick)
{
struct input_absinfo absinfo;
+ unsigned long keyinfo[NBITS(KEY_MAX)];
int i;
/* Poll all axis */
for (i = ABS_X; i < ABS_MAX; i++) {
- if (i == ABS_HAT0X) {
+ if (i == ABS_HAT0X) { /* we handle hats in the next loop, skip them for now. */
i = ABS_HAT3Y;
continue;
}
- if (joystick->hwdata->abs_correct[i].used) {
+ if (joystick->hwdata->has_abs[i]) {
if (ioctl(joystick->hwdata->fd, EVIOCGABS(i), &absinfo) >= 0) {
absinfo.value = AxisCorrect(joystick, i, absinfo.value);
@@ -951,10 +1184,41 @@
}
}
}
+
+ /* Poll all hats */
+ for (i = ABS_HAT0X; i <= ABS_HAT3Y; i++) {
+ const int baseaxis = i - ABS_HAT0X;
+ const int hatidx = baseaxis / 2;
+ SDL_assert(hatidx < SDL_arraysize(joystick->hwdata->has_hat));
+ if (joystick->hwdata->has_hat[hatidx]) {
+ if (ioctl(joystick->hwdata->fd, EVIOCGABS(i), &absinfo) >= 0) {
+ const int hataxis = baseaxis % 2;
+ HandleHat(joystick, joystick->hwdata->hats_indices[hatidx], hataxis, absinfo.value);
+ }
+ }
+ }
+
+ /* Poll all buttons */
+ SDL_zeroa(keyinfo);
+ if (ioctl(joystick->hwdata->fd, EVIOCGKEY(sizeof (keyinfo)), keyinfo) >= 0) {
+ for (i = 0; i < KEY_MAX; i++) {
+ if (joystick->hwdata->has_key[i]) {
+ const Uint8 value = test_bit(i, keyinfo) ? SDL_PRESSED : SDL_RELEASED;
+#ifdef DEBUG_INPUT_EVENTS
+ printf("Joystick : Re-read Button %d (%d) val= %d\n",
+ joystick->hwdata->key_map[i], i, value);
+#endif
+ SDL_PrivateJoystickButton(joystick,
+ joystick->hwdata->key_map[i], value);
+ }
+ }
+ }
+
+ /* Joyballs are relative input, so there's no poll state. Events only! */
}
static SDL_INLINE void
-HandleInputEvents(SDL_Joystick * joystick)
+HandleInputEvents(SDL_Joystick *joystick)
{
struct input_event events[32];
int i, len;
@@ -962,13 +1226,21 @@
if (joystick->hwdata->fresh) {
PollAllValues(joystick);
- joystick->hwdata->fresh = 0;
+ joystick->hwdata->fresh = SDL_FALSE;
}
while ((len = read(joystick->hwdata->fd, events, (sizeof events))) > 0) {
len /= sizeof(events[0]);
for (i = 0; i < len; ++i) {
code = events[i].code;
+
+ /* If the kernel sent a SYN_DROPPED, we are supposed to ignore the
+ rest of the packet (the end of it signified by a SYN_REPORT) */
+ if ( joystick->hwdata->recovering_from_dropped &&
+ ((events[i].type != EV_SYN) || (code != SYN_REPORT)) ) {
+ continue;
+ }
+
switch (events[i].type) {
case EV_KEY:
SDL_PrivateJoystickButton(joystick,
@@ -1016,7 +1288,13 @@
#ifdef DEBUG_INPUT_EVENTS
printf("Event SYN_DROPPED detected\n");
#endif
- PollAllValues(joystick);
+ joystick->hwdata->recovering_from_dropped = SDL_TRUE;
+ break;
+ case SYN_REPORT :
+ if (joystick->hwdata->recovering_from_dropped) {
+ joystick->hwdata->recovering_from_dropped = SDL_FALSE;
+ PollAllValues(joystick); /* try to sync up to current state now */
+ }
break;
default:
break;
@@ -1034,7 +1312,7 @@
}
static void
-LINUX_JoystickUpdate(SDL_Joystick * joystick)
+LINUX_JoystickUpdate(SDL_Joystick *joystick)
{
int i;
@@ -1061,7 +1339,7 @@
/* Function to close a joystick after use */
static void
-LINUX_JoystickClose(SDL_Joystick * joystick)
+LINUX_JoystickClose(SDL_Joystick *joystick)
{
if (joystick->hwdata) {
if (joystick->hwdata->effect.id >= 0) {
@@ -1088,6 +1366,11 @@
SDL_joylist_item *item = NULL;
SDL_joylist_item *next = NULL;
+ if (inotify_fd >= 0) {
+ close(inotify_fd);
+ inotify_fd = -1;
+ }
+
for (item = SDL_joylist; item; item = next) {
next = item->next;
SDL_free(item->path);
@@ -1100,11 +1383,187 @@
numjoysticks = 0;
#if SDL_USE_LIBUDEV
- SDL_UDEV_DelCallback(joystick_udev_callback);
- SDL_UDEV_Quit();
+ if (enumeration_method == ENUMERATION_LIBUDEV) {
+ SDL_UDEV_DelCallback(joystick_udev_callback);
+ SDL_UDEV_Quit();
+ }
#endif
SDL_QuitSteamControllers();
+}
+
+/*
+ This is based on the Linux Gamepad Specification
+ available at: https://www.kernel.org/doc/html/v4.15/input/gamepad.html
+ */
+static SDL_bool
+LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
+{
+ SDL_Joystick *joystick;
+
+ joystick = (SDL_Joystick *) SDL_calloc(sizeof(*joystick), 1);
+ if (joystick == NULL) {
+ SDL_OutOfMemory();
+ return SDL_FALSE;
+ }
+
+ /* We temporarily open the device to check how it's configured. */
+ if (LINUX_JoystickOpen(joystick, device_index) < 0) {
+ SDL_free(joystick);
+ return SDL_FALSE;
+ }
+
+ if (!joystick->hwdata->has_key[BTN_GAMEPAD]) {
+ /* Not a gamepad according to the specs. */
+ LINUX_JoystickClose(joystick);
+ SDL_free(joystick);
+ return SDL_FALSE;
+ }
+
+ /* We have a gamepad, start filling out the mappings */
+
+ if (joystick->hwdata->has_key[BTN_SOUTH]) {
+ out->a.kind = EMappingKind_Button;
+ out->a.target = joystick->hwdata->key_map[BTN_SOUTH];
+ }
+
+ if (joystick->hwdata->has_key[BTN_EAST]) {
+ out->b.kind = EMappingKind_Button;
+ out->b.target = joystick->hwdata->key_map[BTN_EAST];
+ }
+
+ if (joystick->hwdata->has_key[BTN_NORTH]) {
+ out->y.kind = EMappingKind_Button;
+ out->y.target = joystick->hwdata->key_map[BTN_NORTH];
+ }
+
+ if (joystick->hwdata->has_key[BTN_WEST]) {
+ out->x.kind = EMappingKind_Button;
+ out->x.target = joystick->hwdata->key_map[BTN_WEST];
+ }
+
+ if (joystick->hwdata->has_key[BTN_SELECT]) {
+ out->back.kind = EMappingKind_Button;
+ out->back.target = joystick->hwdata->key_map[BTN_SELECT];
+ }
+
+ if (joystick->hwdata->has_key[BTN_START]) {
+ out->start.kind = EMappingKind_Button;
+ out->start.target = joystick->hwdata->key_map[BTN_START];
+ }
+
+ if (joystick->hwdata->has_key[BTN_THUMBL]) {
+ out->leftstick.kind = EMappingKind_Button;
+ out->leftstick.target = joystick->hwdata->key_map[BTN_THUMBL];
+ }
+
+ if (joystick->hwdata->has_key[BTN_THUMBR]) {
+ out->rightstick.kind = EMappingKind_Button;
+ out->rightstick.target = joystick->hwdata->key_map[BTN_THUMBR];
+ }
+
+ if (joystick->hwdata->has_key[BTN_MODE]) {
+ out->guide.kind = EMappingKind_Button;
+ out->guide.target = joystick->hwdata->key_map[BTN_MODE];
+ }
+
+ /*
+ According to the specs the D-Pad, the shoulder buttons and the triggers
+ can be digital, or analog, or both at the same time.
+ */
+
+ /* Prefer digital shoulder buttons, but settle for analog if missing. */
+ if (joystick->hwdata->has_key[BTN_TL]) {
+ out->leftshoulder.kind = EMappingKind_Button;
+ out->leftshoulder.target = joystick->hwdata->key_map[BTN_TL];
+ }
+
+ if (joystick->hwdata->has_key[BTN_TR]) {
+ out->rightshoulder.kind = EMappingKind_Button;
+ out->rightshoulder.target = joystick->hwdata->key_map[BTN_TR];
+ }
+
+ if (joystick->hwdata->has_hat[1] && /* Check if ABS_HAT1{X, Y} is available. */
+ (!joystick->hwdata->has_key[BTN_TL] || !joystick->hwdata->has_key[BTN_TR])) {
+ int hat = joystick->hwdata->hats_indices[1] << 4;
+ out->leftshoulder.kind = EMappingKind_Hat;
+ out->rightshoulder.kind = EMappingKind_Hat;
+ out->leftshoulder.target = hat | 0x4;
+ out->rightshoulder.target = hat | 0x2;
+ }
+
+ /* Prefer analog triggers, but settle for digital if missing. */
+ if (joystick->hwdata->has_hat[2]) { /* Check if ABS_HAT2{X,Y} is available. */
+ int hat = joystick->hwdata->hats_indices[2] << 4;
+ out->lefttrigger.kind = EMappingKind_Hat;
+ out->righttrigger.kind = EMappingKind_Hat;
+ out->lefttrigger.target = hat | 0x4;
+ out->righttrigger.target = hat | 0x2;
+ } else {
+ if (joystick->hwdata->has_key[BTN_TL2]) {
+ out->lefttrigger.kind = EMappingKind_Button;
+ out->lefttrigger.target = joystick->hwdata->key_map[BTN_TL2];
+ }
+
+ if (joystick->hwdata->has_key[BTN_TR2]) {
+ out->righttrigger.kind = EMappingKind_Button;
+ out->righttrigger.target = joystick->hwdata->key_map[BTN_TR2];
+ }
+ }
+
+ /* Prefer digital D-Pad, but settle for analog if missing. */
+ if (joystick->hwdata->has_key[BTN_DPAD_UP]) {
+ out->dpup.kind = EMappingKind_Button;
+ out->dpup.target = joystick->hwdata->key_map[BTN_DPAD_UP];
+ }
+
+ if (joystick->hwdata->has_key[BTN_DPAD_DOWN]) {
+ out->dpdown.kind = EMappingKind_Button;
+ out->dpdown.target = joystick->hwdata->key_map[BTN_DPAD_DOWN];
+ }
+
+ if (joystick->hwdata->has_key[BTN_DPAD_LEFT]) {
+ out->dpleft.kind = EMappingKind_Button;
+ out->dpleft.target = joystick->hwdata->key_map[BTN_DPAD_LEFT];
+ }
+
+ if (joystick->hwdata->has_key[BTN_DPAD_RIGHT]) {
+ out->dpright.kind = EMappingKind_Button;
+ out->dpright.target = joystick->hwdata->key_map[BTN_DPAD_RIGHT];
+ }
+
+ if (joystick->hwdata->has_hat[0] && /* Check if ABS_HAT0{X,Y} is available. */
+ (!joystick->hwdata->has_key[BTN_DPAD_LEFT] || !joystick->hwdata->has_key[BTN_DPAD_RIGHT] ||
+ !joystick->hwdata->has_key[BTN_DPAD_UP] || !joystick->hwdata->has_key[BTN_DPAD_DOWN])) {
+ int hat = joystick->hwdata->hats_indices[0] << 4;
+ out->dpleft.kind = EMappingKind_Hat;
+ out->dpright.kind = EMappingKind_Hat;
+ out->dpup.kind = EMappingKind_Hat;
+ out->dpdown.kind = EMappingKind_Hat;
+ out->dpleft.target = hat | 0x8;
+ out->dpright.target = hat | 0x2;
+ out->dpup.target = hat | 0x1;
+ out->dpdown.target = hat | 0x4;
+ }
+
+ if (joystick->hwdata->has_abs[ABS_X] && joystick->hwdata->has_abs[ABS_Y]) {
+ out->leftx.kind = EMappingKind_Axis;
+ out->lefty.kind = EMappingKind_Axis;
+ out->leftx.target = joystick->hwdata->abs_map[ABS_X];
+ out->lefty.target = joystick->hwdata->abs_map[ABS_Y];
+ }
+
+ if (joystick->hwdata->has_abs[ABS_RX] && joystick->hwdata->has_abs[ABS_RY]) {
+ out->rightx.kind = EMappingKind_Axis;
+ out->righty.kind = EMappingKind_Axis;
+ out->rightx.target = joystick->hwdata->abs_map[ABS_RX];
+ out->righty.target = joystick->hwdata->abs_map[ABS_RY];
+ }
+
+ LINUX_JoystickClose(joystick);
+ SDL_free(joystick);
+
+ return SDL_TRUE;
}
SDL_JoystickDriver SDL_LINUX_JoystickDriver =
@@ -1119,9 +1578,14 @@
LINUX_JoystickGetDeviceInstanceID,
LINUX_JoystickOpen,
LINUX_JoystickRumble,
+ LINUX_JoystickRumbleTriggers,
+ LINUX_JoystickHasLED,
+ LINUX_JoystickSetLED,
+ LINUX_JoystickSetSensorsEnabled,
LINUX_JoystickUpdate,
LINUX_JoystickClose,
LINUX_JoystickQuit,
+ LINUX_JoystickGetGamepadMapping
};
#endif /* SDL_JOYSTICK_LINUX */
--
Gitblit v1.9.3