2010-09-07 15:34:51 +00:00
|
|
|
//========================================================================
|
2016-08-18 21:42:15 +00:00
|
|
|
// GLFW 3.3 Linux - www.glfw.org
|
2010-09-07 15:34:51 +00:00
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Copyright (c) 2002-2006 Marcus Geelnard
|
2016-11-21 15:23:59 +00:00
|
|
|
// Copyright (c) 2006-2016 Camilla Löwy <elmindreda@glfw.org>
|
2010-09-07 15:34:51 +00: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"
|
|
|
|
|
2013-10-16 15:24:37 +00:00
|
|
|
#if defined(__linux__)
|
2012-08-26 16:11:31 +00:00
|
|
|
#include <linux/joystick.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2013-10-16 15:24:37 +00:00
|
|
|
#include <sys/inotify.h>
|
2012-08-26 16:11:31 +00:00
|
|
|
#include <fcntl.h>
|
2012-08-28 18:16:43 +00:00
|
|
|
#include <errno.h>
|
2012-09-07 13:27:41 +00:00
|
|
|
#include <dirent.h>
|
2012-08-26 16:11:31 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-09-06 23:01:34 +00:00
|
|
|
#include <string.h>
|
2014-03-17 21:53:43 +00:00
|
|
|
#include <unistd.h>
|
2012-11-05 15:19:11 +00:00
|
|
|
#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
|
|
|
//
|
2016-05-04 14:56:17 +00:00
|
|
|
#if defined(__linux__)
|
2015-12-15 03:16:20 +00:00
|
|
|
static GLFWbool openJoystickDevice(const char* path)
|
2012-08-26 16:11:31 +00:00
|
|
|
{
|
2013-04-24 17:25:42 +00:00
|
|
|
char axisCount, buttonCount;
|
2016-07-31 15:54:13 +00:00
|
|
|
char name[256] = "";
|
2016-10-10 01:24:07 +00:00
|
|
|
int jid, fd, version;
|
2017-01-05 18:44:15 +00:00
|
|
|
_GLFWjoystick* js;
|
2013-10-16 15:24:37 +00:00
|
|
|
|
2017-01-12 04:30:56 +00:00
|
|
|
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
|
2013-10-16 15:24:37 +00:00
|
|
|
{
|
2017-01-05 18:44:15 +00:00
|
|
|
if (!_glfw.joysticks[jid].present)
|
2013-10-16 15:24:37 +00:00
|
|
|
continue;
|
2017-01-05 18:44:15 +00:00
|
|
|
if (strcmp(_glfw.joysticks[jid].linjs.path, path) == 0)
|
2015-12-15 03:16:20 +00:00
|
|
|
return GLFW_FALSE;
|
2013-10-16 15:24:37 +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)
|
2015-12-15 03:16:20 +00:00
|
|
|
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);
|
2015-12-15 03:16:20 +00:00
|
|
|
return GLFW_FALSE;
|
2012-08-26 16:11:31 +00:00
|
|
|
}
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-09-06 23:01:34 +00:00
|
|
|
if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0)
|
|
|
|
strncpy(name, "Unknown", sizeof(name));
|
|
|
|
|
2013-04-24 17:25:42 +00:00
|
|
|
ioctl(fd, JSIOCGAXES, &axisCount);
|
|
|
|
ioctl(fd, JSIOCGBUTTONS, &buttonCount);
|
2015-12-13 16:38:50 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
js = _glfwAllocJoystick(name, axisCount, buttonCount);
|
|
|
|
if (!js)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
return GLFW_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
js->linjs.path = strdup(path);
|
|
|
|
js->linjs.fd = fd;
|
|
|
|
|
|
|
|
_glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_CONNECTED);
|
2015-12-15 03:16:20 +00:00
|
|
|
return GLFW_TRUE;
|
2012-08-26 16:11:31 +00:00
|
|
|
}
|
2016-05-04 14:56:17 +00:00
|
|
|
#endif // __linux__
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
// Frees all resources associated with the specified joystick
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2013-10-16 15:24:37 +00:00
|
|
|
#if defined(__linux__)
|
2017-01-05 18:44:15 +00:00
|
|
|
static void closeJoystick(_GLFWjoystick* js)
|
|
|
|
{
|
|
|
|
close(js->linjs.fd);
|
|
|
|
free(js->linjs.path);
|
|
|
|
_glfwFreeJoystick(js);
|
|
|
|
_glfwInputJoystick(_GLFW_JOYSTICK_ID(js), GLFW_DISCONNECTED);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
2017-01-05 18:44:15 +00:00
|
|
|
#endif // __linux__
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2016-07-31 17:26:57 +00:00
|
|
|
// Lexically compare joysticks by name; used by qsort
|
2015-12-15 03:16:20 +00:00
|
|
|
//
|
2016-05-04 14:56:17 +00:00
|
|
|
#if defined(__linux__)
|
2015-12-15 03:16:20 +00:00
|
|
|
static int compareJoysticks(const void* fp, const void* sp)
|
|
|
|
{
|
2017-01-05 18:44:15 +00:00
|
|
|
const _GLFWjoystick* fj = fp;
|
|
|
|
const _GLFWjoystick* sj = sp;
|
|
|
|
return strcmp(fj->linjs.path, sj->linjs.path);
|
2015-12-15 03:16:20 +00:00
|
|
|
}
|
2016-05-04 14:56:17 +00:00
|
|
|
#endif // __linux__
|
2015-12-15 03:16:20 +00:00
|
|
|
|
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
|
|
|
{
|
2013-10-16 15:24:37 +00:00
|
|
|
#if defined(__linux__)
|
2012-09-07 13:27:41 +00:00
|
|
|
DIR* dir;
|
2015-12-15 03:16:20 +00:00
|
|
|
int count = 0;
|
|
|
|
const char* dirname = "/dev/input";
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
_glfw.linjs.inotify = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
|
|
|
if (_glfw.linjs.inotify == -1)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2015-01-06 18:22:19 +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;
|
2012-09-07 13:27:41 +00:00
|
|
|
}
|
|
|
|
|
2013-10-16 15:24:37 +00:00
|
|
|
// 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
|
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
_glfw.linjs.watch = inotify_add_watch(_glfw.linjs.inotify,
|
|
|
|
dirname,
|
2017-01-30 23:17:08 +00:00
|
|
|
IN_CREATE | IN_ATTRIB | IN_DELETE);
|
2017-01-05 18:44:15 +00:00
|
|
|
if (_glfw.linjs.watch == -1)
|
2012-09-07 13:27:41 +00:00
|
|
|
{
|
2013-10-16 15:24:37 +00:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
2015-01-06 18:22:19 +00:00
|
|
|
"Linux: Failed to watch for joystick connections in %s: %s",
|
|
|
|
dirname,
|
|
|
|
strerror(errno));
|
|
|
|
// Continue without device connection notifications
|
2013-10-16 15:24:37 +00:00
|
|
|
}
|
2012-09-07 13:27:41 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
if (regcomp(&_glfw.linjs.regex, "^js[0-9]\\+$", 0) != 0)
|
2013-10-16 15:24:37 +00:00
|
|
|
{
|
2015-01-06 00:23:10 +00:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR, "Linux: Failed to compile regex");
|
2015-08-23 17:30:04 +00:00
|
|
|
return GLFW_FALSE;
|
2013-10-16 15:24:37 +00:00
|
|
|
}
|
2012-09-07 13:27:41 +00:00
|
|
|
|
2013-10-16 15:24:37 +00:00
|
|
|
dir = opendir(dirname);
|
2015-01-06 18:22:19 +00:00
|
|
|
if (dir)
|
2013-10-16 15:24:37 +00:00
|
|
|
{
|
2015-01-06 18:22:19 +00:00
|
|
|
struct dirent* entry;
|
2010-09-08 13:51:25 +00:00
|
|
|
|
2015-01-06 18:22:19 +00:00
|
|
|
while ((entry = readdir(dir)))
|
|
|
|
{
|
|
|
|
char path[20];
|
|
|
|
regmatch_t match;
|
2012-09-07 13:27:41 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
if (regexec(&_glfw.linjs.regex, entry->d_name, 1, &match, 0) != 0)
|
2015-01-06 18:22:19 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
snprintf(path, sizeof(path), "%s/%s", dirname, entry->d_name);
|
2015-12-15 03:16:20 +00:00
|
|
|
if (openJoystickDevice(path))
|
|
|
|
count++;
|
2015-01-06 18:22:19 +00:00
|
|
|
}
|
2012-09-07 13:27:41 +00:00
|
|
|
|
2015-01-06 18:22:19 +00:00
|
|
|
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
|
|
|
}
|
2012-09-07 13:27:41 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
qsort(_glfw.joysticks, count, sizeof(_GLFWjoystick), compareJoysticks);
|
2012-11-05 15:19:11 +00:00
|
|
|
#endif // __linux__
|
2013-10-16 15:24:37 +00:00
|
|
|
|
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
|
|
|
//
|
2015-12-03 17:16:46 +00:00
|
|
|
void _glfwTerminateJoysticksLinux(void)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2013-10-16 15:24:37 +00:00
|
|
|
#if defined(__linux__)
|
2017-01-05 18:44:15 +00:00
|
|
|
int jid;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2017-01-05 18:44:15 +00:00
|
|
|
_GLFWjoystick* js = _glfw.joysticks + jid;
|
|
|
|
if (js->present)
|
|
|
|
closeJoystick(js);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
2013-10-16 15:24:37 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
regfree(&_glfw.linjs.regex);
|
2013-10-16 15:24:37 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
if (_glfw.linjs.inotify > 0)
|
2015-01-23 21:53:41 +00:00
|
|
|
{
|
2017-01-05 18:44:15 +00:00
|
|
|
if (_glfw.linjs.watch > 0)
|
|
|
|
inotify_rm_watch(_glfw.linjs.inotify, _glfw.linjs.watch);
|
2015-01-23 21:53:41 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
close(_glfw.linjs.inotify);
|
2015-01-23 21:53:41 +00:00
|
|
|
}
|
2012-11-05 15:19:11 +00:00
|
|
|
#endif // __linux__
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
void _glfwDetectJoystickConnectionLinux(void)
|
2015-12-13 16:38:50 +00:00
|
|
|
{
|
2016-05-04 14:56:17 +00:00
|
|
|
#if defined(__linux__)
|
2015-12-13 16:38:50 +00:00
|
|
|
ssize_t offset = 0;
|
|
|
|
char buffer[16384];
|
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
const ssize_t size = read(_glfw.linjs.inotify, buffer, sizeof(buffer));
|
2015-12-13 16:38:50 +00:00
|
|
|
|
|
|
|
while (size > offset)
|
|
|
|
{
|
|
|
|
regmatch_t match;
|
|
|
|
const struct inotify_event* e = (struct inotify_event*) (buffer + offset);
|
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
if (regexec(&_glfw.linjs.regex, e->name, 1, &match, 0) == 0)
|
2015-12-13 16:38:50 +00:00
|
|
|
{
|
|
|
|
char path[20];
|
|
|
|
snprintf(path, sizeof(path), "/dev/input/%s", e->name);
|
2017-01-30 23:17:08 +00:00
|
|
|
|
|
|
|
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 16:38:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
offset += sizeof(struct inotify_event) + e->len;
|
|
|
|
}
|
2016-05-04 14:56:17 +00:00
|
|
|
#endif
|
2015-12-13 16:38:50 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
int _glfwPlatformPollJoystick(int jid, int mode)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2017-01-05 18:44:15 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
_GLFWjoystick* js = _glfw.joysticks + jid;
|
2012-08-28 18:16:43 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
// Read all queued events (non-blocking)
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
struct js_event e;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2017-01-05 18:44:15 +00: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);
|
2012-08-28 18:16:43 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
// Clear the initial-state bit
|
|
|
|
e.type &= ~JS_EVENT_INIT;
|
2012-11-08 15:26:43 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
if (e.type == JS_EVENT_AXIS)
|
|
|
|
_glfwInputJoystickAxis(jid, e.number, e.value / 32767.0f);
|
|
|
|
else if (e.type == JS_EVENT_BUTTON)
|
|
|
|
_glfwInputJoystickButton(jid, e.number, e.value ? 1 : 0);
|
|
|
|
}
|
|
|
|
#endif // __linux__
|
|
|
|
return js->present;
|
2012-09-06 23:01:34 +00:00
|
|
|
}
|
|
|
|
|