glfw/src/linux_joystick.c

328 lines
8.9 KiB
C
Raw Normal View History

2010-09-07 15:34:51 +00:00
//========================================================================
2015-06-01 20:55:06 +00:00
// GLFW 3.2 Linux - www.glfw.org
2010-09-07 15:34:51 +00:00
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Marcus Geelnard
// Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.org>
//
// 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"
#if defined(__linux__)
2012-08-26 16:11:31 +00:00
#include <linux/joystick.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/inotify.h>
2012-08-26 16:11:31 +00:00
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
2012-08-26 16:11:31 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#endif // __linux__
2012-08-26 16:11:31 +00:00
2010-09-07 15:34:51 +00:00
2012-08-26 16:11:31 +00:00
// Attempt to open the specified joystick device
2013-02-04 12:22:10 +00:00
//
static GLFWbool openJoystickDevice(const char* path)
2012-08-26 16:11:31 +00:00
{
#if defined(__linux__)
2013-04-24 17:25:42 +00:00
char axisCount, buttonCount;
char name[256];
int joy, fd, version;
_GLFWjoystickLinux* js;
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
{
if (!_glfw.linux_js.js[joy].present)
continue;
if (strcmp(_glfw.linux_js.js[joy].path, path) == 0)
return GLFW_FALSE;
}
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
{
if (!_glfw.linux_js.js[joy].present)
break;
}
if (joy > GLFW_JOYSTICK_LAST)
return GLFW_FALSE;
2010-09-07 15:34:51 +00:00
2012-09-06 23:01:17 +00:00
fd = open(path, O_RDONLY | O_NONBLOCK);
2012-08-26 16:11:31 +00:00
if (fd == -1)
return GLFW_FALSE;
2010-09-07 15:34:51 +00:00
2012-08-26 16:11:31 +00:00
// Verify that the joystick driver version is at least 1.0
ioctl(fd, JSIOCGVERSION, &version);
if (version < 0x010000)
{
// It's an old 0.x interface (we don't support it)
close(fd);
return GLFW_FALSE;
2012-08-26 16:11:31 +00:00
}
2010-09-07 15:34:51 +00:00
if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0)
strncpy(name, "Unknown", sizeof(name));
js = _glfw.linux_js.js + joy;
js->present = GLFW_TRUE;
js->name = strdup(name);
js->path = strdup(path);
js->fd = fd;
2013-04-24 17:25:42 +00:00
ioctl(fd, JSIOCGAXES, &axisCount);
js->axisCount = (int) axisCount;
js->axes = calloc(axisCount, sizeof(float));
2010-09-07 15:34:51 +00:00
2013-04-24 17:25:42 +00:00
ioctl(fd, JSIOCGBUTTONS, &buttonCount);
js->buttonCount = (int) buttonCount;
js->buttons = calloc(buttonCount, 1);
#endif // __linux__
return GLFW_TRUE;
2012-08-26 16:11:31 +00:00
}
2010-09-07 15:34:51 +00:00
// Polls for and processes events the specified joystick
2013-02-04 12:22:10 +00:00
//
static GLFWbool pollJoystickEvents(_GLFWjoystickLinux* js)
2010-09-07 15:34:51 +00:00
{
#if defined(__linux__)
ssize_t offset = 0;
char buffer[16384];
const ssize_t size = read(_glfw.linux_js.inotify, buffer, sizeof(buffer));
while (size > offset)
{
regmatch_t match;
const struct inotify_event* e = (struct inotify_event*) (buffer + offset);
if (regexec(&_glfw.linux_js.regex, e->name, 1, &match, 0) == 0)
{
char path[20];
snprintf(path, sizeof(path), "/dev/input/%s", e->name);
openJoystickDevice(path);
}
offset += sizeof(struct inotify_event) + e->len;
}
2010-09-07 15:34:51 +00:00
if (!js->present)
return GLFW_FALSE;
// Read all queued events (non-blocking)
for (;;)
2010-09-07 15:34:51 +00:00
{
struct js_event e;
2010-09-07 15:34:51 +00:00
errno = 0;
if (read(js->fd, &e, sizeof(e)) < 0)
2010-09-07 15:34:51 +00:00
{
// Reset the joystick slot if the device was disconnected
if (errno == ENODEV)
2013-04-24 17:25:42 +00:00
{
free(js->axes);
free(js->buttons);
free(js->name);
free(js->path);
2014-11-04 20:23:13 +00:00
memset(js, 0, sizeof(_GLFWjoystickLinux));
2014-11-04 20:23:13 +00:00
}
break;
2010-09-07 15:34:51 +00:00
}
// Clear the initial-state bit
e.type &= ~JS_EVENT_INIT;
if (e.type == JS_EVENT_AXIS)
js->axes[e.number] = (float) e.value / 32767.0f;
else if (e.type == JS_EVENT_BUTTON)
js->buttons[e.number] = e.value ? GLFW_PRESS : GLFW_RELEASE;
2010-09-07 15:34:51 +00:00
}
#endif // __linux__
return js->present;
2010-09-07 15:34:51 +00:00
}
// Lexically compare the specified joysticks by device paths
//
static int compareJoysticks(const void* fp, const void* sp)
{
const _GLFWjoystickLinux* fj = fp;
const _GLFWjoystickLinux* sj = sp;
return strcmp(fj->path, sj->path);
}
2010-09-07 15:34:51 +00:00
2012-08-26 16:11:31 +00:00
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////
// Initialize joystick interface
2013-02-04 12:22:10 +00:00
//
2015-12-10 16:10:05 +00:00
GLFWbool _glfwInitJoysticksLinux(void)
2010-09-07 15:34:51 +00:00
{
#if defined(__linux__)
DIR* dir;
int count = 0;
const char* dirname = "/dev/input";
2010-09-07 15:34:51 +00:00
_glfw.linux_js.inotify = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (_glfw.linux_js.inotify == -1)
2010-09-07 15:34:51 +00:00
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Linux: Failed to initialize inotify: %s",
strerror(errno));
2015-08-23 17:30:04 +00: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.linux_js.watch = inotify_add_watch(_glfw.linux_js.inotify,
dirname,
IN_CREATE | IN_ATTRIB);
if (_glfw.linux_js.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.linux_js.regex, "^js[0-9]\\+$", 0) != 0)
{
_glfwInputError(GLFW_PLATFORM_ERROR, "Linux: Failed to compile regex");
2015-08-23 17:30:04 +00:00
return GLFW_FALSE;
}
dir = opendir(dirname);
if (dir)
{
struct dirent* entry;
2010-09-08 13:51:25 +00:00
while ((entry = readdir(dir)))
{
char path[20];
regmatch_t match;
if (regexec(&_glfw.linux_js.regex, entry->d_name, 1, &match, 0) != 0)
continue;
snprintf(path, sizeof(path), "%s/%s", dirname, entry->d_name);
if (openJoystickDevice(path))
count++;
}
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 15:34:51 +00:00
}
qsort(_glfw.linux_js.js, count, sizeof(_GLFWjoystickLinux), compareJoysticks);
#endif // __linux__
2015-08-23 17:30:04 +00:00
return GLFW_TRUE;
2010-09-07 15:34:51 +00:00
}
2012-08-26 16:11:31 +00:00
// Close all opened joystick handles
2013-02-04 12:22:10 +00:00
//
void _glfwTerminateJoysticksLinux(void)
2010-09-07 15:34:51 +00:00
{
#if defined(__linux__)
2010-09-07 15:34:51 +00:00
int i;
2010-09-08 13:51:25 +00:00
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
2010-09-07 15:34:51 +00:00
{
if (_glfw.linux_js.js[i].present)
2010-09-07 15:34:51 +00:00
{
close(_glfw.linux_js.js[i].fd);
free(_glfw.linux_js.js[i].axes);
free(_glfw.linux_js.js[i].buttons);
free(_glfw.linux_js.js[i].name);
free(_glfw.linux_js.js[i].path);
2010-09-07 15:34:51 +00:00
}
}
regfree(&_glfw.linux_js.regex);
if (_glfw.linux_js.inotify > 0)
{
if (_glfw.linux_js.watch > 0)
inotify_rm_watch(_glfw.linux_js.inotify, _glfw.linux_js.watch);
close(_glfw.linux_js.inotify);
}
#endif // __linux__
2010-09-07 15:34:51 +00:00
}
2010-09-15 14:44:43 +00:00
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
2010-09-07 15:34:51 +00:00
2013-04-24 17:25:42 +00:00
int _glfwPlatformJoystickPresent(int joy)
2010-09-07 15:34:51 +00:00
{
_GLFWjoystickLinux* js = _glfw.linux_js.js + joy;
return pollJoystickEvents(js);
2010-09-07 15:34:51 +00:00
}
2013-06-04 16:20:38 +00:00
const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
2010-09-07 15:34:51 +00:00
{
_GLFWjoystickLinux* js = _glfw.linux_js.js + joy;
if (!pollJoystickEvents(js))
return NULL;
*count = js->axisCount;
return js->axes;
2010-09-07 15:34:51 +00:00
}
2013-06-04 16:20:38 +00:00
const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
2010-09-07 15:34:51 +00:00
{
_GLFWjoystickLinux* js = _glfw.linux_js.js + joy;
if (!pollJoystickEvents(js))
return NULL;
*count = js->buttonCount;
return js->buttons;
2010-09-07 15:34:51 +00:00
}
const char* _glfwPlatformGetJoystickName(int joy)
{
_GLFWjoystickLinux* js = _glfw.linux_js.js + joy;
if (!pollJoystickEvents(js))
return NULL;
return js->name;
}