early-access version 1988

This commit is contained in:
pineappleEA
2021-08-12 01:07:27 +02:00
parent e37f82ce96
commit 24ddfcbb39
265 changed files with 68343 additions and 5348 deletions

View File

@@ -589,6 +589,18 @@ LINUX_InotifyJoystickDetect(void)
* 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 int
filter_entries(const struct dirent *entry)
{
return (SDL_strlen(entry->d_name) > 5 && SDL_strncmp(entry->d_name, "event", 5) == 0);
}
static int
sort_entries(const struct dirent **a, const struct dirent **b)
{
int numA = SDL_atoi((*a)->d_name+5);
int numB = SDL_atoi((*b)->d_name+5);
return (numA - numB);
}
static void
LINUX_FallbackJoystickDetect(void)
{
@@ -600,22 +612,18 @@ LINUX_FallbackJoystickDetect(void)
/* Opening input devices can generate synchronous device I/O, so avoid it if we can */
if (stat("/dev/input", &sb) == 0 && sb.st_mtime != last_input_dir_mtime) {
DIR *folder;
struct dirent *dent;
int i, count;
struct dirent **entries;
char path[PATH_MAX];
folder = opendir("/dev/input");
if (folder) {
while ((dent = readdir(folder))) {
int len = SDL_strlen(dent->d_name);
if (len > 5 && SDL_strncmp(dent->d_name, "event", 5) == 0) {
char path[PATH_MAX];
SDL_snprintf(path, SDL_arraysize(path), "/dev/input/%s", dent->d_name);
MaybeAddDevice(path);
}
}
count = scandir("/dev/input", &entries, filter_entries, sort_entries);
for (i = 0; i < count; ++i) {
SDL_snprintf(path, SDL_arraysize(path), "/dev/input/%s", entries[i]->d_name);
MaybeAddDevice(path);
closedir(folder);
free(entries[i]); /* This should NOT be SDL_free() */
}
free(entries); /* This should NOT be SDL_free() */
last_input_dir_mtime = sb.st_mtime;
}
@@ -965,6 +973,48 @@ ConfigJoystick(SDL_Joystick *joystick, int fd)
}
/* This is used to do the heavy lifting for LINUX_JoystickOpen and
also LINUX_JoystickGetGamepadMapping, so we can query the hardware
without adding an opened SDL_Joystick object to the system.
This expects `joystick->hwdata` to be allocated and will not free it
on error. Returns -1 on error, 0 on success. */
static int
PrepareJoystickHwdata(SDL_Joystick *joystick, SDL_joylist_item *item)
{
joystick->hwdata->item = item;
joystick->hwdata->guid = item->guid;
joystick->hwdata->effect.id = -1;
joystick->hwdata->m_bSteamController = item->m_bSteamController;
SDL_memset(joystick->hwdata->abs_map, 0xFF, sizeof(joystick->hwdata->abs_map));
if (item->m_bSteamController) {
joystick->hwdata->fd = -1;
SDL_GetSteamControllerInputs(&joystick->nbuttons,
&joystick->naxes,
&joystick->nhats);
} else {
const int fd = open(item->path, O_RDWR, 0);
if (fd < 0) {
return SDL_SetError("Unable to open %s", item->path);
}
joystick->hwdata->fd = fd;
joystick->hwdata->fname = SDL_strdup(item->path);
if (joystick->hwdata->fname == NULL) {
close(fd);
return SDL_OutOfMemory();
}
/* Set the joystick to non-blocking read mode */
fcntl(fd, F_SETFL, O_NONBLOCK);
/* Get the number of buttons and axes on the joystick */
ConfigJoystick(joystick, fd);
}
return 0;
}
/* Function to open a joystick for use.
The joystick to open is specified by the device index.
This should fill the nbuttons and naxes fields of the joystick structure.
@@ -985,39 +1035,11 @@ LINUX_JoystickOpen(SDL_Joystick *joystick, int device_index)
if (joystick->hwdata == NULL) {
return SDL_OutOfMemory();
}
joystick->hwdata->item = item;
joystick->hwdata->guid = item->guid;
joystick->hwdata->effect.id = -1;
joystick->hwdata->m_bSteamController = item->m_bSteamController;
SDL_memset(joystick->hwdata->abs_map, 0xFF, sizeof(joystick->hwdata->abs_map));
if (item->m_bSteamController) {
joystick->hwdata->fd = -1;
SDL_GetSteamControllerInputs(&joystick->nbuttons,
&joystick->naxes,
&joystick->nhats);
} else {
int fd = open(item->path, O_RDWR, 0);
if (fd < 0) {
SDL_free(joystick->hwdata);
joystick->hwdata = NULL;
return SDL_SetError("Unable to open %s", item->path);
}
joystick->hwdata->fd = fd;
joystick->hwdata->fname = SDL_strdup(item->path);
if (joystick->hwdata->fname == NULL) {
SDL_free(joystick->hwdata);
joystick->hwdata = NULL;
close(fd);
return SDL_OutOfMemory();
}
/* Set the joystick to non-blocking read mode */
fcntl(fd, F_SETFL, O_NONBLOCK);
/* Get the number of buttons and axes on the joystick */
ConfigJoystick(joystick, fd);
if (PrepareJoystickHwdata(joystick, item) == -1) {
SDL_free(joystick->hwdata);
joystick->hwdata = NULL;
return -1; /* SDL_SetError will already have been called */
}
SDL_assert(item->hwdata == NULL);
@@ -1026,7 +1048,7 @@ LINUX_JoystickOpen(SDL_Joystick *joystick, int device_index)
/* mark joystick as fresh and ready */
joystick->hwdata->fresh = SDL_TRUE;
return (0);
return 0;
}
static int
@@ -1089,6 +1111,12 @@ LINUX_JoystickSetLED(SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
return SDL_Unsupported();
}
static int
LINUX_JoystickSendEffect(SDL_Joystick *joystick, const void *data, int size)
{
return SDL_Unsupported();
}
static int
LINUX_JoystickSetSensorsEnabled(SDL_Joystick *joystick, SDL_bool enabled)
{
@@ -1411,18 +1439,32 @@ LINUX_JoystickGetGamepadMapping(int device_index, SDL_GamepadMapping *out)
return SDL_TRUE;
}
/* We temporarily open the device to check how it's configured. Make
a fake SDL_Joystick object to do so. */
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) {
joystick->hwdata = (struct joystick_hwdata *)
SDL_calloc(1, sizeof(*joystick->hwdata));
if (joystick->hwdata == NULL) {
SDL_free(joystick);
SDL_OutOfMemory();
return SDL_FALSE;
}
if (PrepareJoystickHwdata(joystick, item) == -1) {
SDL_free(joystick->hwdata);
SDL_free(joystick);
return SDL_FALSE; /* SDL_SetError will already have been called */
}
/* don't assign `item->hwdata` so it's not in any global state. */
/* it is now safe to call LINUX_JoystickClose on this fake joystick. */
if (!joystick->hwdata->has_key[BTN_GAMEPAD]) {
/* Not a gamepad according to the specs. */
LINUX_JoystickClose(joystick);
@@ -1603,6 +1645,7 @@ SDL_JoystickDriver SDL_LINUX_JoystickDriver =
LINUX_JoystickRumbleTriggers,
LINUX_JoystickHasLED,
LINUX_JoystickSetLED,
LINUX_JoystickSendEffect,
LINUX_JoystickSetSensorsEnabled,
LINUX_JoystickUpdate,
LINUX_JoystickClose,