2010-09-07 11:34:51 -04:00
|
|
|
//========================================================================
|
2014-03-17 17:53:43 -04:00
|
|
|
// GLFW 3.1 Linux - www.glfw.org
|
2010-09-07 11:34:51 -04: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"
|
|
|
|
|
2013-10-16 11:24:37 -04:00
|
|
|
#if defined(__linux__)
|
2012-08-26 12:11:31 -04:00
|
|
|
#include <linux/joystick.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2013-10-16 11:24:37 -04:00
|
|
|
#include <sys/inotify.h>
|
2012-08-26 12:11:31 -04:00
|
|
|
#include <fcntl.h>
|
2012-08-28 14:16:43 -04:00
|
|
|
#include <errno.h>
|
2012-09-07 09:27:41 -04:00
|
|
|
#include <dirent.h>
|
2012-08-26 12:11:31 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-09-06 19:01:34 -04:00
|
|
|
#include <string.h>
|
2014-03-17 17:53:43 -04:00
|
|
|
#include <unistd.h>
|
2012-11-05 10:19:11 -05:00
|
|
|
#endif // __linux__
|
2012-08-26 12:11:31 -04:00
|
|
|
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2012-08-26 12:11:31 -04:00
|
|
|
// Attempt to open the specified joystick device
|
2013-02-04 07:22:10 -05:00
|
|
|
//
|
2013-10-16 11:24:37 -04:00
|
|
|
static void openJoystickDevice(const char* path)
|
2012-08-26 12:11:31 -04:00
|
|
|
{
|
2013-10-16 11:24:37 -04:00
|
|
|
#if defined(__linux__)
|
2013-04-24 13:25:42 -04:00
|
|
|
char axisCount, buttonCount;
|
2012-09-06 19:01:34 -04:00
|
|
|
char name[256];
|
2013-10-16 11:24:37 -04:00
|
|
|
int joy, fd, version;
|
|
|
|
|
|
|
|
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
|
|
|
|
{
|
2015-01-05 19:23:10 -05:00
|
|
|
if (!_glfw.linux_js.js[joy].present)
|
2013-10-16 11:24:37 -04:00
|
|
|
continue;
|
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
if (strcmp(_glfw.linux_js.js[joy].path, path) == 0)
|
2013-10-16 11:24:37 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
|
|
|
|
{
|
2015-01-05 19:23:10 -05:00
|
|
|
if (!_glfw.linux_js.js[joy].present)
|
2013-10-16 11:24:37 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (joy > GLFW_JOYSTICK_LAST)
|
|
|
|
return;
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2012-09-06 19:01:17 -04:00
|
|
|
fd = open(path, O_RDONLY | O_NONBLOCK);
|
2012-08-26 12:11:31 -04:00
|
|
|
if (fd == -1)
|
2013-10-16 11:24:37 -04:00
|
|
|
return;
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
_glfw.linux_js.js[joy].fd = fd;
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2012-08-26 12:11:31 -04: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);
|
2013-10-16 11:24:37 -04:00
|
|
|
return;
|
2012-08-26 12:11:31 -04:00
|
|
|
}
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2012-09-06 19:01:34 -04:00
|
|
|
if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0)
|
|
|
|
strncpy(name, "Unknown", sizeof(name));
|
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
_glfw.linux_js.js[joy].name = strdup(name);
|
|
|
|
_glfw.linux_js.js[joy].path = strdup(path);
|
2012-09-06 19:01:34 -04:00
|
|
|
|
2013-04-24 13:25:42 -04:00
|
|
|
ioctl(fd, JSIOCGAXES, &axisCount);
|
2015-01-05 19:23:10 -05:00
|
|
|
_glfw.linux_js.js[joy].axisCount = (int) axisCount;
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2013-04-24 13:25:42 -04:00
|
|
|
ioctl(fd, JSIOCGBUTTONS, &buttonCount);
|
2015-01-05 19:23:10 -05:00
|
|
|
_glfw.linux_js.js[joy].buttonCount = (int) buttonCount;
|
2012-08-26 12:11:31 -04:00
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
_glfw.linux_js.js[joy].axes = calloc(axisCount, sizeof(float));
|
|
|
|
_glfw.linux_js.js[joy].buttons = calloc(buttonCount, 1);
|
2012-08-26 12:11:31 -04:00
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
_glfw.linux_js.js[joy].present = GL_TRUE;
|
2012-11-05 10:19:11 -05:00
|
|
|
#endif // __linux__
|
2012-08-26 12:11:31 -04:00
|
|
|
}
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2012-08-26 12:11:31 -04:00
|
|
|
// Polls for and processes events for all present joysticks
|
2013-02-04 07:22:10 -05:00
|
|
|
//
|
2012-08-26 12:11:31 -04:00
|
|
|
static void pollJoystickEvents(void)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2013-10-16 11:24:37 -04:00
|
|
|
#if defined(__linux__)
|
2012-08-26 12:11:31 -04:00
|
|
|
int i;
|
|
|
|
struct js_event e;
|
2013-10-16 11:24:37 -04:00
|
|
|
ssize_t offset = 0;
|
|
|
|
char buffer[16384];
|
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
const ssize_t size = read(_glfw.linux_js.inotify, buffer, sizeof(buffer));
|
2013-10-16 11:24:37 -04:00
|
|
|
|
|
|
|
while (size > offset)
|
|
|
|
{
|
|
|
|
regmatch_t match;
|
|
|
|
const struct inotify_event* e = (struct inotify_event*) (buffer + offset);
|
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
if (regexec(&_glfw.linux_js.regex, e->name, 1, &match, 0) == 0)
|
2013-10-16 11:24:37 -04:00
|
|
|
{
|
|
|
|
char path[20];
|
|
|
|
snprintf(path, sizeof(path), "/dev/input/%s", e->name);
|
|
|
|
openJoystickDevice(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += sizeof(struct inotify_event) + e->len;
|
|
|
|
}
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2010-09-08 09:51:25 -04:00
|
|
|
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2015-01-05 19:23:10 -05:00
|
|
|
if (!_glfw.linux_js.js[i].present)
|
2012-08-26 12:11:31 -04:00
|
|
|
continue;
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2012-08-26 12:11:31 -04:00
|
|
|
// Read all queued events (non-blocking)
|
2012-08-28 14:16:43 -04:00
|
|
|
for (;;)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2012-08-28 14:16:43 -04:00
|
|
|
errno = 0;
|
2015-01-05 19:23:10 -05:00
|
|
|
if (read(_glfw.linux_js.js[i].fd, &e, sizeof(e)) < 0)
|
2013-04-24 13:25:42 -04:00
|
|
|
{
|
2014-11-04 15:23:13 -05:00
|
|
|
if (errno == ENODEV)
|
|
|
|
{
|
|
|
|
// The joystick was disconnected
|
2014-05-16 05:25:34 -04:00
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
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);
|
2014-11-04 15:23:13 -05:00
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
memset(&_glfw.linux_js.js[i], 0, sizeof(_glfw.linux_js.js[i]));
|
2014-11-04 15:23:13 -05:00
|
|
|
}
|
2012-08-28 14:16:43 -04:00
|
|
|
|
|
|
|
break;
|
2014-11-04 15:23:13 -05:00
|
|
|
}
|
2012-08-28 14:16:43 -04:00
|
|
|
|
2012-08-26 12:11:31 -04:00
|
|
|
// We don't care if it's an init event or not
|
|
|
|
e.type &= ~JS_EVENT_INIT;
|
|
|
|
|
|
|
|
switch (e.type)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2012-08-26 12:11:31 -04:00
|
|
|
case JS_EVENT_AXIS:
|
2015-01-05 19:23:10 -05:00
|
|
|
_glfw.linux_js.js[i].axes[e.number] =
|
2012-08-26 12:11:31 -04:00
|
|
|
(float) e.value / 32767.0f;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JS_EVENT_BUTTON:
|
2015-01-05 19:23:10 -05:00
|
|
|
_glfw.linux_js.js[i].buttons[e.number] =
|
2012-08-26 12:11:31 -04:00
|
|
|
e.value ? GLFW_PRESS : GLFW_RELEASE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-05 10:19:11 -05:00
|
|
|
#endif // __linux__
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-26 12:11:31 -04:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW internal API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// Initialize joystick interface
|
2013-02-04 07:22:10 -05:00
|
|
|
//
|
2013-10-16 11:24:37 -04:00
|
|
|
int _glfwInitJoysticks(void)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2013-10-16 11:24:37 -04:00
|
|
|
#if defined(__linux__)
|
|
|
|
const char* dirname = "/dev/input";
|
2012-09-07 09:27:41 -04:00
|
|
|
DIR* dir;
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
_glfw.linux_js.inotify = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
|
|
|
if (_glfw.linux_js.inotify == -1)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2015-01-06 13:22:19 -05:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
|
|
|
"Linux: Failed to initialize inotify: %s",
|
|
|
|
strerror(errno));
|
2013-10-16 11:24:37 -04:00
|
|
|
return GL_FALSE;
|
2012-09-07 09:27:41 -04:00
|
|
|
}
|
|
|
|
|
2013-10-16 11:24:37 -04: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
|
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
_glfw.linux_js.watch = inotify_add_watch(_glfw.linux_js.inotify,
|
2013-10-16 11:24:37 -04:00
|
|
|
dirname,
|
|
|
|
IN_CREATE | IN_ATTRIB);
|
2015-01-05 19:23:10 -05:00
|
|
|
if (_glfw.linux_js.watch == -1)
|
2012-09-07 09:27:41 -04:00
|
|
|
{
|
2013-10-16 11:24:37 -04:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
2015-01-06 13:22:19 -05:00
|
|
|
"Linux: Failed to watch for joystick connections in %s: %s",
|
|
|
|
dirname,
|
|
|
|
strerror(errno));
|
|
|
|
// Continue without device connection notifications
|
2013-10-16 11:24:37 -04:00
|
|
|
}
|
2012-09-07 09:27:41 -04:00
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
if (regcomp(&_glfw.linux_js.regex, "^js[0-9]\\+$", 0) != 0)
|
2013-10-16 11:24:37 -04:00
|
|
|
{
|
2015-01-05 19:23:10 -05:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR, "Linux: Failed to compile regex");
|
2013-10-16 11:24:37 -04:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
2012-09-07 09:27:41 -04:00
|
|
|
|
2013-10-16 11:24:37 -04:00
|
|
|
dir = opendir(dirname);
|
2015-01-06 13:22:19 -05:00
|
|
|
if (dir)
|
2013-10-16 11:24:37 -04:00
|
|
|
{
|
2015-01-06 13:22:19 -05:00
|
|
|
struct dirent* entry;
|
2010-09-08 09:51:25 -04:00
|
|
|
|
2015-01-06 13:22:19 -05:00
|
|
|
while ((entry = readdir(dir)))
|
|
|
|
{
|
|
|
|
char path[20];
|
|
|
|
regmatch_t match;
|
2012-09-07 09:27:41 -04:00
|
|
|
|
2015-01-06 13:22:19 -05:00
|
|
|
if (regexec(&_glfw.linux_js.regex, entry->d_name, 1, &match, 0) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
snprintf(path, sizeof(path), "%s/%s", dirname, entry->d_name);
|
|
|
|
openJoystickDevice(path);
|
|
|
|
}
|
2012-09-07 09:27:41 -04:00
|
|
|
|
2015-01-06 13:22:19 -05: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 11:34:51 -04:00
|
|
|
}
|
2012-09-07 09:27:41 -04:00
|
|
|
|
2012-11-05 10:19:11 -05:00
|
|
|
#endif // __linux__
|
2013-10-16 11:24:37 -04:00
|
|
|
|
|
|
|
return GL_TRUE;
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|
2012-08-26 12:11:31 -04:00
|
|
|
// Close all opened joystick handles
|
2013-02-04 07:22:10 -05:00
|
|
|
//
|
2012-08-26 12:11:31 -04:00
|
|
|
void _glfwTerminateJoysticks(void)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2013-10-16 11:24:37 -04:00
|
|
|
#if defined(__linux__)
|
2010-09-07 11:34:51 -04:00
|
|
|
int i;
|
|
|
|
|
2010-09-08 09:51:25 -04:00
|
|
|
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2015-01-05 19:23:10 -05:00
|
|
|
if (_glfw.linux_js.js[i].present)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2015-01-05 19:23:10 -05: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 11:34:51 -04:00
|
|
|
}
|
|
|
|
}
|
2013-10-16 11:24:37 -04:00
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
regfree(&_glfw.linux_js.regex);
|
2013-10-16 11:24:37 -04:00
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
if (_glfw.linux_js.inotify > 0)
|
2015-01-23 16:53:41 -05:00
|
|
|
{
|
|
|
|
if (_glfw.linux_js.watch > 0)
|
|
|
|
inotify_rm_watch(_glfw.linux_js.inotify, _glfw.linux_js.watch);
|
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
close(_glfw.linux_js.inotify);
|
2015-01-23 16:53:41 -05:00
|
|
|
}
|
2012-11-05 10:19:11 -05:00
|
|
|
#endif // __linux__
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-15 10:44:43 -04:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW platform API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2013-04-24 13:25:42 -04:00
|
|
|
int _glfwPlatformJoystickPresent(int joy)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2012-08-28 14:16:43 -04:00
|
|
|
pollJoystickEvents();
|
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
return _glfw.linux_js.js[joy].present;
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|
2013-06-04 12:20:38 -04:00
|
|
|
const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2012-08-28 14:16:43 -04:00
|
|
|
pollJoystickEvents();
|
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
*count = _glfw.linux_js.js[joy].axisCount;
|
|
|
|
return _glfw.linux_js.js[joy].axes;
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|
2013-06-04 12:20:38 -04:00
|
|
|
const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2012-08-28 14:16:43 -04:00
|
|
|
pollJoystickEvents();
|
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
*count = _glfw.linux_js.js[joy].buttonCount;
|
|
|
|
return _glfw.linux_js.js[joy].buttons;
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|
2012-09-06 19:01:34 -04:00
|
|
|
const char* _glfwPlatformGetJoystickName(int joy)
|
|
|
|
{
|
2013-04-24 13:25:42 -04:00
|
|
|
pollJoystickEvents();
|
2012-11-08 10:26:43 -05:00
|
|
|
|
2015-01-05 19:23:10 -05:00
|
|
|
return _glfw.linux_js.js[joy].name;
|
2012-09-06 19:01:34 -04:00
|
|
|
}
|
|
|
|
|