glfw/src/linux_joystick.c

401 lines
11 KiB
C
Raw Normal View History

2010-09-07 17:34:51 +02:00
//========================================================================
2016-08-18 23:42:15 +02:00
// GLFW 3.3 Linux - www.glfw.org
2010-09-07 17:34:51 +02:00
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Marcus Geelnard
2016-11-21 16:23:59 +01:00
// Copyright (c) 2006-2016 Camilla Löwy <elmindreda@glfw.org>
2010-09-07 17:34:51 +02: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.
//
//========================================================================
#include "internal.h"
2012-08-26 18:11:31 +02:00
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/inotify.h>
2012-08-26 18:11:31 +02:00
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
2012-08-26 18:11:31 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2012-08-26 18:11:31 +02:00
#define TEST_BIT(bit, arr) (arr[(bit) / 8] & (1 << ((bit) % 8)))
static void handleKeyEvent(_GLFWjoystick* js, int code, int value)
{
int jid = js - _glfw.joysticks;
int button = js->linjs.keyMap[code];
_glfwInputJoystickButton(jid, button, value ? 1 : 0);
}
static void handleAbsEvent(_GLFWjoystick* js, int code, int value)
{
int jid = js - _glfw.joysticks;
int index = js->linjs.absMap[code];
if (code >= ABS_HAT0X && code <= ABS_HAT3Y)
{
static const char stateMap[3][3] =
{
{GLFW_HAT_CENTERED, GLFW_HAT_UP, GLFW_HAT_DOWN},
{GLFW_HAT_LEFT, GLFW_HAT_LEFT_UP, GLFW_HAT_LEFT_DOWN},
{GLFW_HAT_RIGHT, GLFW_HAT_RIGHT_UP, GLFW_HAT_RIGHT_DOWN},
};
int hat = (code - ABS_HAT0X) / 2;
int axis = (code - ABS_HAT0X) % 2;
int *state = js->linjs.hats[hat];
// Looking at several input drivers, it seems all hat events use
// -1 for left / up, 0 for centered and 1 for right / down
if (value == 0)
state[axis] = 0;
else if (value < 0)
state[axis] = 1;
else if (value > 0)
state[axis] = 2;
_glfwInputJoystickHat(jid, index, stateMap[state[0]][state[1]]);
}
else
{
struct input_absinfo *info = &js->linjs.absInfo[code];
int range = info->maximum - info->minimum;
float normalized = value;
if (range != 0)
{
// Normalize from 0.0 -> 1.0
normalized = (normalized - info->minimum) / range;
// Normalize from -1.0 -> 1.0
normalized = normalized * 2.0f - 1.0f;
}
_glfwInputJoystickAxis(jid, index, normalized);
}
}
static void pollJoystick(_GLFWjoystick* js)
{
int i;
for (i = 0; i < ABS_CNT; i++)
{
if (js->linjs.absMap[i] < 0)
continue;
struct input_absinfo *info = &js->linjs.absInfo[i];
if (ioctl(js->linjs.fd, EVIOCGABS(i), info) < 0)
continue;
handleAbsEvent(js, i, info->value);
}
}
2010-09-07 17:34:51 +02:00
2012-08-26 18:11:31 +02:00
// Attempt to open the specified joystick device
2013-02-04 13:22:10 +01:00
//
static GLFWbool openJoystickDevice(const char* path)
2012-08-26 18:11:31 +02:00
{
int jid, fd, i;
char name[256] = "";
char evBits[(EV_CNT + 7) / 8] = {0};
char keyBits[(KEY_CNT + 7) / 8] = {0};
char absBits[(ABS_CNT + 7) / 8] = {0};
int axisCount = 0;
int buttonCount = 0;
int hatCount = 0;
_GLFWjoystickLinux linjs = {0};
_GLFWjoystick* js = NULL;
2017-01-12 05:30:56 +01:00
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
{
if (!_glfw.joysticks[jid].present)
continue;
if (strcmp(_glfw.joysticks[jid].linjs.path, path) == 0)
return GLFW_FALSE;
}
2012-09-07 01:01:17 +02:00
fd = open(path, O_RDONLY | O_NONBLOCK);
2012-08-26 18:11:31 +02:00
if (fd == -1)
return GLFW_FALSE;
2010-09-07 17:34:51 +02:00
// Ensure this device supports the events expected of a joystick
if (ioctl(fd, EVIOCGBIT(0, sizeof(evBits)), evBits) < 0 ||
ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(keyBits)), keyBits) < 0 ||
ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(absBits)), absBits) < 0 ||
!TEST_BIT(EV_KEY, evBits) || !TEST_BIT(EV_ABS, evBits))
2012-08-26 18:11:31 +02:00
{
close(fd);
return GLFW_FALSE;
2012-08-26 18:11:31 +02:00
}
2010-09-07 17:34:51 +02:00
if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) < 0)
strncpy(name, "Unknown", sizeof(name));
for (i = BTN_MISC; i < KEY_CNT; i++)
{
if (!TEST_BIT(i, keyBits))
continue;
linjs.keyMap[i] = buttonCount++;
}
for (i = 0; i < ABS_CNT; i++)
{
linjs.absMap[i] = -1;
if (!TEST_BIT(i, absBits))
continue;
2015-12-13 17:38:50 +01:00
if (i >= ABS_HAT0X && i <= ABS_HAT3Y)
{
linjs.absMap[i] = hatCount++;
// Skip the Y axis
i++;
}
else
{
if (ioctl(fd, EVIOCGABS(i), &linjs.absInfo[i]) < 0)
continue;
linjs.absMap[i] = axisCount++;
}
}
js = _glfwAllocJoystick(name, axisCount, buttonCount, hatCount);
if (!js)
{
close(fd);
return GLFW_FALSE;
}
linjs.fd = fd;
strncpy(linjs.path, path, sizeof(linjs.path));
memcpy(&js->linjs, &linjs, sizeof(linjs));
// Set initial values for absolute axes
pollJoystick(js);
_glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_CONNECTED);
return GLFW_TRUE;
2012-08-26 18:11:31 +02:00
}
2010-09-07 17:34:51 +02:00
// Frees all resources associated with the specified joystick
2013-02-04 13:22:10 +01:00
//
static void closeJoystick(_GLFWjoystick* js)
{
close(js->linjs.fd);
_glfwFreeJoystick(js);
_glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_DISCONNECTED);
2010-09-07 17:34:51 +02:00
}
2016-07-31 19:26:57 +02:00
// Lexically compare joysticks by name; used by qsort
//
static int compareJoysticks(const void* fp, const void* sp)
{
const _GLFWjoystick* fj = fp;
const _GLFWjoystick* sj = sp;
return strcmp(fj->linjs.path, sj->linjs.path);
}
2010-09-07 17:34:51 +02:00
2012-08-26 18:11:31 +02:00
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////
// Initialize joystick interface
2013-02-04 13:22:10 +01:00
//
2015-12-10 17:10:05 +01:00
GLFWbool _glfwInitJoysticksLinux(void)
2010-09-07 17:34:51 +02:00
{
DIR* dir;
int count = 0;
const char* dirname = "/dev/input";
2010-09-07 17:34:51 +02:00
_glfw.linjs.inotify = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (_glfw.linjs.inotify == -1)
2010-09-07 17:34:51 +02:00
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Linux: Failed to initialize inotify: %s",
strerror(errno));
2015-08-23 19:30:04 +02:00
return GLFW_FALSE;
}
// HACK: Register for IN_ATTRIB as well to get notified when udev is done
// This works well in practice but the true way is libudev
_glfw.linjs.watch = inotify_add_watch(_glfw.linjs.inotify,
dirname,
IN_CREATE | IN_ATTRIB | IN_DELETE);
if (_glfw.linjs.watch == -1)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Linux: Failed to watch for joystick connections in %s: %s",
dirname,
strerror(errno));
// Continue without device connection notifications
}
if (regcomp(&_glfw.linjs.regex, "^event[0-9]\\+$", 0) != 0)
{
_glfwInputError(GLFW_PLATFORM_ERROR, "Linux: Failed to compile regex");
2015-08-23 19:30:04 +02:00
return GLFW_FALSE;
}
dir = opendir(dirname);
if (dir)
{
struct dirent* entry;
2010-09-08 15:51:25 +02:00
while ((entry = readdir(dir)))
{
regmatch_t match;
if (regexec(&_glfw.linjs.regex, entry->d_name, 1, &match, 0) != 0)
continue;
2017-06-11 23:15:44 +02:00
const size_t length = strlen(dirname) + strlen(entry->d_name) + 1;
char* path = calloc(length + 1, 1);
snprintf(path, length + 1, "%s/%s", dirname, entry->d_name);
if (openJoystickDevice(path))
count++;
free(path);
}
closedir(dir);
}
else
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Linux: Failed to open joystick device directory %s: %s",
dirname,
strerror(errno));
// Continue with no joysticks detected
2010-09-07 17:34:51 +02:00
}
qsort(_glfw.joysticks, count, sizeof(_GLFWjoystick), compareJoysticks);
2015-08-23 19:30:04 +02:00
return GLFW_TRUE;
2010-09-07 17:34:51 +02:00
}
2012-08-26 18:11:31 +02:00
// Close all opened joystick handles
2013-02-04 13:22:10 +01:00
//
void _glfwTerminateJoysticksLinux(void)
2010-09-07 17:34:51 +02:00
{
int jid;
2010-09-07 17:34:51 +02:00
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
2010-09-07 17:34:51 +02:00
{
_GLFWjoystick* js = _glfw.joysticks + jid;
if (js->present)
closeJoystick(js);
2010-09-07 17:34:51 +02:00
}
regfree(&_glfw.linjs.regex);
if (_glfw.linjs.inotify > 0)
{
if (_glfw.linjs.watch > 0)
inotify_rm_watch(_glfw.linjs.inotify, _glfw.linjs.watch);
close(_glfw.linjs.inotify);
}
2010-09-07 17:34:51 +02:00
}
void _glfwDetectJoystickConnectionLinux(void)
2015-12-13 17:38:50 +01:00
{
ssize_t offset = 0;
char buffer[16384];
const ssize_t size = read(_glfw.linjs.inotify, buffer, sizeof(buffer));
2015-12-13 17:38:50 +01:00
while (size > offset)
{
regmatch_t match;
const struct inotify_event* e = (struct inotify_event*) (buffer + offset);
if (regexec(&_glfw.linjs.regex, e->name, 1, &match, 0) == 0)
2015-12-13 17:38:50 +01:00
{
char path[20];
snprintf(path, sizeof(path), "/dev/input/%s", e->name);
if (e->mask & (IN_CREATE | IN_ATTRIB))
openJoystickDevice(path);
else if (e->mask & IN_DELETE)
{
int jid;
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
{
if (strcmp(_glfw.joysticks[jid].linjs.path, path) == 0)
{
closeJoystick(_glfw.joysticks + jid);
break;
}
}
}
2015-12-13 17:38:50 +01:00
}
offset += sizeof(struct inotify_event) + e->len;
}
}
2010-09-07 17:34:51 +02:00
2010-09-15 16:44:43 +02:00
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
2010-09-07 17:34:51 +02:00
int _glfwPlatformPollJoystick(int jid, int mode)
2010-09-07 17:34:51 +02:00
{
_GLFWjoystick* js = _glfw.joysticks + jid;
// Read all queued events (non-blocking)
for (;;)
{
struct input_event e;
2010-09-07 17:34:51 +02:00
errno = 0;
if (read(js->linjs.fd, &e, sizeof(e)) < 0)
{
// Reset the joystick slot if the device was disconnected
if (errno == ENODEV)
closeJoystick(js);
break;
}
2010-09-07 17:34:51 +02:00
if (e.type == EV_KEY)
handleKeyEvent(js, e.code, e.value);
else if (e.type == EV_ABS)
handleAbsEvent(js, e.code, e.value);
else if (e.type == EV_SYN && e.code == SYN_DROPPED)
// Refresh axes
pollJoystick(js);
}
return js->present;
}