2010-09-07 11:34:51 -04:00
|
|
|
//========================================================================
|
2016-08-18 17:42:15 -04:00
|
|
|
// GLFW 3.3 Linux - www.glfw.org
|
2010-09-07 11:34:51 -04:00
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Copyright (c) 2002-2006 Marcus Geelnard
|
2019-04-15 14:50:00 -04:00
|
|
|
// Copyright (c) 2006-2017 Camilla Löwy <elmindreda@glfw.org>
|
2010-09-07 11:34:51 -04: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 12:11:31 -04:00
|
|
|
#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-08-26 12:11:31 -04:00
|
|
|
|
2018-01-27 08:43:06 -05:00
|
|
|
#ifndef SYN_DROPPED // < v2.6.39 kernel headers
|
|
|
|
// Workaround for CentOS-6, which is supported till 2020-11-30, but still on v2.6.32
|
|
|
|
#define SYN_DROPPED 3
|
|
|
|
#endif
|
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
// Apply an EV_KEY event to the specified joystick
|
|
|
|
//
|
2017-05-06 16:34:56 -04:00
|
|
|
static void handleKeyEvent(_GLFWjoystick* js, int code, int value)
|
|
|
|
{
|
2017-07-09 08:01:48 -04:00
|
|
|
_glfwInputJoystickButton(js,
|
2017-06-15 11:13:23 -04:00
|
|
|
js->linjs.keyMap[code - BTN_MISC],
|
|
|
|
value ? GLFW_PRESS : GLFW_RELEASE);
|
2017-05-06 16:34:56 -04:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
// Apply an EV_ABS event to the specified joystick
|
|
|
|
//
|
2017-05-06 16:34:56 -04:00
|
|
|
static void handleAbsEvent(_GLFWjoystick* js, int code, int value)
|
|
|
|
{
|
2017-06-15 11:13:23 -04:00
|
|
|
const int index = js->linjs.absMap[code];
|
2017-05-06 16:34:56 -04:00
|
|
|
|
|
|
|
if (code >= ABS_HAT0X && code <= ABS_HAT3Y)
|
|
|
|
{
|
|
|
|
static const char stateMap[3][3] =
|
|
|
|
{
|
2017-06-15 11:13:23 -04:00
|
|
|
{ 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 },
|
2017-05-06 16:34:56 -04:00
|
|
|
};
|
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
const int hat = (code - ABS_HAT0X) / 2;
|
|
|
|
const int axis = (code - ABS_HAT0X) % 2;
|
|
|
|
int* state = js->linjs.hats[hat];
|
2017-05-06 16:34:56 -04:00
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
// NOTE: Looking at several input drivers, it seems all hat events use
|
|
|
|
// -1 for left / up, 0 for centered and 1 for right / down
|
2017-05-06 16:34:56 -04:00
|
|
|
if (value == 0)
|
|
|
|
state[axis] = 0;
|
|
|
|
else if (value < 0)
|
|
|
|
state[axis] = 1;
|
|
|
|
else if (value > 0)
|
|
|
|
state[axis] = 2;
|
|
|
|
|
2017-07-09 08:01:48 -04:00
|
|
|
_glfwInputJoystickHat(js, index, stateMap[state[0]][state[1]]);
|
2017-05-06 16:34:56 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-15 11:13:23 -04:00
|
|
|
const struct input_absinfo* info = &js->linjs.absInfo[code];
|
2017-05-06 16:34:56 -04:00
|
|
|
float normalized = value;
|
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
const int range = info->maximum - info->minimum;
|
|
|
|
if (range)
|
2017-05-06 16:34:56 -04:00
|
|
|
{
|
2017-06-15 11:13:23 -04:00
|
|
|
// Normalize to 0.0 -> 1.0
|
2017-05-06 16:34:56 -04:00
|
|
|
normalized = (normalized - info->minimum) / range;
|
2017-06-15 11:13:23 -04:00
|
|
|
// Normalize to -1.0 -> 1.0
|
2017-05-06 16:34:56 -04:00
|
|
|
normalized = normalized * 2.0f - 1.0f;
|
|
|
|
}
|
|
|
|
|
2017-07-09 08:01:48 -04:00
|
|
|
_glfwInputJoystickAxis(js, index, normalized);
|
2017-05-06 16:34:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
// Poll state of absolute axes
|
|
|
|
//
|
|
|
|
static void pollAbsState(_GLFWjoystick* js)
|
2017-05-06 16:34:56 -04:00
|
|
|
{
|
2017-06-15 11:13:23 -04:00
|
|
|
int code;
|
2017-05-06 16:34:56 -04:00
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
for (code = 0; code < ABS_CNT; code++)
|
2017-05-06 16:34:56 -04:00
|
|
|
{
|
2017-06-15 11:13:23 -04:00
|
|
|
if (js->linjs.absMap[code] < 0)
|
2017-06-15 10:03:04 -04:00
|
|
|
continue;
|
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
struct input_absinfo* info = &js->linjs.absInfo[code];
|
2017-05-06 16:34:56 -04:00
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
if (ioctl(js->linjs.fd, EVIOCGABS(code), info) < 0)
|
2017-05-06 16:34:56 -04:00
|
|
|
continue;
|
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
handleAbsEvent(js, code, info->value);
|
2017-05-06 16:34:56 -04:00
|
|
|
}
|
|
|
|
}
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
#define isBitSet(bit, arr) (arr[(bit) / 8] & (1 << ((bit) % 8)))
|
|
|
|
|
2012-08-26 12:11:31 -04:00
|
|
|
// Attempt to open the specified joystick device
|
2013-02-04 07:22:10 -05:00
|
|
|
//
|
2015-12-14 22:16:20 -05:00
|
|
|
static GLFWbool openJoystickDevice(const char* path)
|
2012-08-26 12:11:31 -04:00
|
|
|
{
|
2017-06-15 11:13:23 -04:00
|
|
|
int jid, code;
|
2016-07-31 11:54:13 -04:00
|
|
|
char name[256] = "";
|
2017-06-18 09:13:25 -04:00
|
|
|
char guid[33] = "";
|
2017-06-15 10:03:04 -04:00
|
|
|
char evBits[(EV_CNT + 7) / 8] = {0};
|
|
|
|
char keyBits[(KEY_CNT + 7) / 8] = {0};
|
|
|
|
char absBits[(ABS_CNT + 7) / 8] = {0};
|
2017-06-15 11:13:23 -04:00
|
|
|
int axisCount = 0, buttonCount = 0, hatCount = 0;
|
2017-06-18 09:13:25 -04:00
|
|
|
struct input_id id;
|
2017-05-06 16:34:56 -04:00
|
|
|
_GLFWjoystickLinux linjs = {0};
|
|
|
|
_GLFWjoystick* js = NULL;
|
2013-10-16 11:24:37 -04:00
|
|
|
|
2017-01-11 23:30:56 -05:00
|
|
|
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
|
2013-10-16 11:24:37 -04:00
|
|
|
{
|
2017-01-05 13:44:15 -05:00
|
|
|
if (!_glfw.joysticks[jid].present)
|
2013-10-16 11:24:37 -04:00
|
|
|
continue;
|
2017-01-05 13:44:15 -05:00
|
|
|
if (strcmp(_glfw.joysticks[jid].linjs.path, path) == 0)
|
2015-12-14 22:16:20 -05:00
|
|
|
return GLFW_FALSE;
|
2013-10-16 11:24:37 -04:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
linjs.fd = open(path, O_RDONLY | O_NONBLOCK);
|
|
|
|
if (linjs.fd == -1)
|
|
|
|
return GLFW_FALSE;
|
|
|
|
|
|
|
|
if (ioctl(linjs.fd, EVIOCGBIT(0, sizeof(evBits)), evBits) < 0 ||
|
|
|
|
ioctl(linjs.fd, EVIOCGBIT(EV_KEY, sizeof(keyBits)), keyBits) < 0 ||
|
2017-06-18 09:13:25 -04:00
|
|
|
ioctl(linjs.fd, EVIOCGBIT(EV_ABS, sizeof(absBits)), absBits) < 0 ||
|
|
|
|
ioctl(linjs.fd, EVIOCGID, &id) < 0)
|
2017-06-15 11:13:23 -04:00
|
|
|
{
|
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR,
|
2017-06-18 09:13:25 -04:00
|
|
|
"Linux: Failed to query input device: %s",
|
2017-06-15 11:13:23 -04:00
|
|
|
strerror(errno));
|
|
|
|
close(linjs.fd);
|
2015-12-14 22:16:20 -05:00
|
|
|
return GLFW_FALSE;
|
2017-06-15 11:13:23 -04:00
|
|
|
}
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2017-05-06 16:34:56 -04:00
|
|
|
// Ensure this device supports the events expected of a joystick
|
2017-06-15 11:13:23 -04:00
|
|
|
if (!isBitSet(EV_KEY, evBits) || !isBitSet(EV_ABS, evBits))
|
2012-08-26 12:11:31 -04:00
|
|
|
{
|
2017-06-15 11:13:23 -04:00
|
|
|
close(linjs.fd);
|
2015-12-14 22:16:20 -05:00
|
|
|
return GLFW_FALSE;
|
2012-08-26 12:11:31 -04:00
|
|
|
}
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
if (ioctl(linjs.fd, EVIOCGNAME(sizeof(name)), name) < 0)
|
2012-09-06 19:01:34 -04:00
|
|
|
strncpy(name, "Unknown", sizeof(name));
|
|
|
|
|
2017-06-18 09:13:25 -04:00
|
|
|
// Generate a joystick GUID that matches the SDL 2.0.5+ one
|
|
|
|
if (id.vendor && id.product && id.version)
|
|
|
|
{
|
|
|
|
sprintf(guid, "%02x%02x0000%02x%02x0000%02x%02x0000%02x%02x0000",
|
|
|
|
id.bustype & 0xff, id.bustype >> 8,
|
|
|
|
id.vendor & 0xff, id.vendor >> 8,
|
|
|
|
id.product & 0xff, id.product >> 8,
|
|
|
|
id.version & 0xff, id.version >> 8);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(guid, "%02x%02x0000%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x00",
|
|
|
|
id.bustype & 0xff, id.bustype >> 8,
|
|
|
|
name[0], name[1], name[2], name[3],
|
|
|
|
name[4], name[5], name[6], name[7],
|
|
|
|
name[8], name[9], name[10]);
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
for (code = BTN_MISC; code < KEY_CNT; code++)
|
2017-05-06 16:34:56 -04:00
|
|
|
{
|
2017-06-15 11:13:23 -04:00
|
|
|
if (!isBitSet(code, keyBits))
|
2017-05-06 16:34:56 -04:00
|
|
|
continue;
|
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
linjs.keyMap[code - BTN_MISC] = buttonCount;
|
|
|
|
buttonCount++;
|
2017-05-06 16:34:56 -04:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
for (code = 0; code < ABS_CNT; code++)
|
2017-05-06 16:34:56 -04:00
|
|
|
{
|
2017-06-15 11:13:23 -04:00
|
|
|
linjs.absMap[code] = -1;
|
|
|
|
if (!isBitSet(code, absBits))
|
2017-05-06 16:34:56 -04:00
|
|
|
continue;
|
2015-12-13 11:38:50 -05:00
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
if (code >= ABS_HAT0X && code <= ABS_HAT3Y)
|
2017-05-06 16:34:56 -04:00
|
|
|
{
|
2017-06-15 11:13:23 -04:00
|
|
|
linjs.absMap[code] = hatCount;
|
|
|
|
hatCount++;
|
2017-05-06 16:34:56 -04:00
|
|
|
// Skip the Y axis
|
2017-06-15 11:13:23 -04:00
|
|
|
code++;
|
2017-05-06 16:34:56 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-15 11:13:23 -04:00
|
|
|
if (ioctl(linjs.fd, EVIOCGABS(code), &linjs.absInfo[code]) < 0)
|
2017-05-06 16:34:56 -04:00
|
|
|
continue;
|
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
linjs.absMap[code] = axisCount;
|
|
|
|
axisCount++;
|
2017-05-06 16:34:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-18 09:13:25 -04:00
|
|
|
js = _glfwAllocJoystick(name, guid, axisCount, buttonCount, hatCount);
|
2017-01-05 13:44:15 -05:00
|
|
|
if (!js)
|
|
|
|
{
|
2017-06-15 11:13:23 -04:00
|
|
|
close(linjs.fd);
|
2017-01-05 13:44:15 -05:00
|
|
|
return GLFW_FALSE;
|
|
|
|
}
|
|
|
|
|
2018-10-03 14:49:07 -04:00
|
|
|
strncpy(linjs.path, path, sizeof(linjs.path) - 1);
|
2017-05-06 16:34:56 -04:00
|
|
|
memcpy(&js->linjs, &linjs, sizeof(linjs));
|
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
pollAbsState(js);
|
2017-01-05 13:44:15 -05:00
|
|
|
|
2017-07-09 08:01:48 -04:00
|
|
|
_glfwInputJoystick(js, GLFW_CONNECTED);
|
2015-12-14 22:16:20 -05:00
|
|
|
return GLFW_TRUE;
|
2012-08-26 12:11:31 -04:00
|
|
|
}
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2017-06-15 11:13:23 -04:00
|
|
|
#undef isBitSet
|
|
|
|
|
2017-01-05 13:44:15 -05:00
|
|
|
// Frees all resources associated with the specified joystick
|
2013-02-04 07:22:10 -05:00
|
|
|
//
|
2017-01-05 13:44:15 -05:00
|
|
|
static void closeJoystick(_GLFWjoystick* js)
|
|
|
|
{
|
|
|
|
close(js->linjs.fd);
|
|
|
|
_glfwFreeJoystick(js);
|
2017-07-09 08:01:48 -04:00
|
|
|
_glfwInputJoystick(js, GLFW_DISCONNECTED);
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|
2016-07-31 13:26:57 -04:00
|
|
|
// Lexically compare joysticks by name; used by qsort
|
2015-12-14 22:16:20 -05:00
|
|
|
//
|
|
|
|
static int compareJoysticks(const void* fp, const void* sp)
|
|
|
|
{
|
2017-01-05 13:44:15 -05:00
|
|
|
const _GLFWjoystick* fj = fp;
|
|
|
|
const _GLFWjoystick* sj = sp;
|
|
|
|
return strcmp(fj->linjs.path, sj->linjs.path);
|
2015-12-14 22:16:20 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
//
|
2015-12-10 11:10:05 -05:00
|
|
|
GLFWbool _glfwInitJoysticksLinux(void)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2012-09-07 09:27:41 -04:00
|
|
|
DIR* dir;
|
2015-12-14 22:16:20 -05:00
|
|
|
int count = 0;
|
|
|
|
const char* dirname = "/dev/input";
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2017-01-05 13:44:15 -05:00
|
|
|
_glfw.linjs.inotify = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
2017-08-09 06:04:38 -04:00
|
|
|
if (_glfw.linjs.inotify > 0)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2017-08-09 06:04:38 -04:00
|
|
|
// HACK: Register for IN_ATTRIB to get notified when udev is done
|
|
|
|
// This works well in practice but the true way is libudev
|
2013-10-16 11:24:37 -04:00
|
|
|
|
2017-08-09 06:04:38 -04:00
|
|
|
_glfw.linjs.watch = inotify_add_watch(_glfw.linjs.inotify,
|
|
|
|
dirname,
|
|
|
|
IN_CREATE | IN_ATTRIB | IN_DELETE);
|
2013-10-16 11:24:37 -04:00
|
|
|
}
|
2012-09-07 09:27:41 -04:00
|
|
|
|
2017-08-09 06:04:38 -04:00
|
|
|
// Continue without device connection notifications if inotify fails
|
|
|
|
|
2017-05-06 16:34:56 -04:00
|
|
|
if (regcomp(&_glfw.linjs.regex, "^event[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");
|
2015-08-23 13:30:04 -04:00
|
|
|
return GLFW_FALSE;
|
2013-10-16 11:24:37 -04:00
|
|
|
}
|
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)))
|
|
|
|
{
|
|
|
|
regmatch_t match;
|
2012-09-07 09:27:41 -04:00
|
|
|
|
2017-01-05 13:44:15 -05:00
|
|
|
if (regexec(&_glfw.linjs.regex, entry->d_name, 1, &match, 0) != 0)
|
2015-01-06 13:22:19 -05:00
|
|
|
continue;
|
|
|
|
|
2017-06-15 13:56:25 -04:00
|
|
|
char path[PATH_MAX];
|
2017-06-11 17:15:44 -04:00
|
|
|
|
2017-06-15 13:56:25 -04:00
|
|
|
snprintf(path, sizeof(path), "%s/%s", dirname, entry->d_name);
|
2017-06-08 13:23:06 -04:00
|
|
|
|
2015-12-14 22:16:20 -05:00
|
|
|
if (openJoystickDevice(path))
|
|
|
|
count++;
|
2015-01-06 13:22:19 -05:00
|
|
|
}
|
2012-09-07 09:27:41 -04:00
|
|
|
|
2015-01-06 13:22:19 -05:00
|
|
|
closedir(dir);
|
|
|
|
}
|
2017-08-09 06:04:38 -04:00
|
|
|
|
|
|
|
// Continue with no joysticks if enumeration fails
|
2012-09-07 09:27:41 -04:00
|
|
|
|
2017-01-05 13:44:15 -05:00
|
|
|
qsort(_glfw.joysticks, count, sizeof(_GLFWjoystick), compareJoysticks);
|
2015-08-23 13:30:04 -04:00
|
|
|
return GLFW_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
|
|
|
//
|
2015-12-03 12:16:46 -05:00
|
|
|
void _glfwTerminateJoysticksLinux(void)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2017-01-05 13:44:15 -05:00
|
|
|
int jid;
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2017-01-05 13:44:15 -05:00
|
|
|
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2017-01-05 13:44:15 -05:00
|
|
|
_GLFWjoystick* js = _glfw.joysticks + jid;
|
|
|
|
if (js->present)
|
|
|
|
closeJoystick(js);
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
2013-10-16 11:24:37 -04:00
|
|
|
|
2017-01-05 13:44:15 -05:00
|
|
|
regfree(&_glfw.linjs.regex);
|
2013-10-16 11:24:37 -04:00
|
|
|
|
2017-01-05 13:44:15 -05:00
|
|
|
if (_glfw.linjs.inotify > 0)
|
2015-01-23 16:53:41 -05:00
|
|
|
{
|
2017-01-05 13:44:15 -05:00
|
|
|
if (_glfw.linjs.watch > 0)
|
|
|
|
inotify_rm_watch(_glfw.linjs.inotify, _glfw.linjs.watch);
|
2015-01-23 16:53:41 -05:00
|
|
|
|
2017-01-05 13:44:15 -05:00
|
|
|
close(_glfw.linjs.inotify);
|
2015-01-23 16:53:41 -05:00
|
|
|
}
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|
2017-01-05 13:44:15 -05:00
|
|
|
void _glfwDetectJoystickConnectionLinux(void)
|
2015-12-13 11:38:50 -05:00
|
|
|
{
|
|
|
|
ssize_t offset = 0;
|
|
|
|
char buffer[16384];
|
|
|
|
|
2017-08-09 06:04:38 -04:00
|
|
|
if (_glfw.linjs.inotify <= 0)
|
|
|
|
return;
|
|
|
|
|
2017-01-05 13:44:15 -05:00
|
|
|
const ssize_t size = read(_glfw.linjs.inotify, buffer, sizeof(buffer));
|
2015-12-13 11:38:50 -05:00
|
|
|
|
|
|
|
while (size > offset)
|
|
|
|
{
|
|
|
|
regmatch_t match;
|
|
|
|
const struct inotify_event* e = (struct inotify_event*) (buffer + offset);
|
|
|
|
|
2017-06-15 13:56:25 -04:00
|
|
|
offset += sizeof(struct inotify_event) + e->len;
|
|
|
|
|
|
|
|
if (regexec(&_glfw.linjs.regex, e->name, 1, &match, 0) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char path[PATH_MAX];
|
|
|
|
snprintf(path, sizeof(path), "/dev/input/%s", e->name);
|
|
|
|
|
|
|
|
if (e->mask & (IN_CREATE | IN_ATTRIB))
|
|
|
|
openJoystickDevice(path);
|
|
|
|
else if (e->mask & IN_DELETE)
|
2015-12-13 11:38:50 -05:00
|
|
|
{
|
2017-06-15 13:56:25 -04:00
|
|
|
int jid;
|
2017-01-30 18:17:08 -05:00
|
|
|
|
2017-06-15 13:56:25 -04:00
|
|
|
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
|
2017-01-30 18:17:08 -05:00
|
|
|
{
|
2017-06-15 13:56:25 -04:00
|
|
|
if (strcmp(_glfw.joysticks[jid].linjs.path, path) == 0)
|
2017-01-30 18:17:08 -05:00
|
|
|
{
|
2017-06-15 13:56:25 -04:00
|
|
|
closeJoystick(_glfw.joysticks + jid);
|
|
|
|
break;
|
2017-01-30 18:17:08 -05:00
|
|
|
}
|
|
|
|
}
|
2015-12-13 11:38:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-07-09 08:01:48 -04:00
|
|
|
int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2017-01-05 13:44:15 -05:00
|
|
|
// Read all queued events (non-blocking)
|
|
|
|
for (;;)
|
|
|
|
{
|
2017-05-06 16:34:56 -04:00
|
|
|
struct input_event e;
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2017-01-05 13:44:15 -05: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 14:16:43 -04:00
|
|
|
|
2017-01-05 13:44:15 -05:00
|
|
|
break;
|
|
|
|
}
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2017-06-15 11:40:57 -04:00
|
|
|
if (e.type == EV_SYN)
|
|
|
|
{
|
|
|
|
if (e.code == SYN_DROPPED)
|
|
|
|
_glfw.linjs.dropped = GLFW_TRUE;
|
|
|
|
else if (e.code == SYN_REPORT)
|
|
|
|
{
|
|
|
|
_glfw.linjs.dropped = GLFW_FALSE;
|
|
|
|
pollAbsState(js);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_glfw.linjs.dropped)
|
|
|
|
continue;
|
|
|
|
|
2017-05-06 16:34:56 -04:00
|
|
|
if (e.type == EV_KEY)
|
|
|
|
handleKeyEvent(js, e.code, e.value);
|
|
|
|
else if (e.type == EV_ABS)
|
|
|
|
handleAbsEvent(js, e.code, e.value);
|
2017-01-05 13:44:15 -05:00
|
|
|
}
|
2017-01-30 21:29:28 -05:00
|
|
|
|
2017-01-05 13:44:15 -05:00
|
|
|
return js->present;
|
2012-09-06 19:01:34 -04:00
|
|
|
}
|
|
|
|
|
2017-06-18 09:13:25 -04:00
|
|
|
void _glfwPlatformUpdateGamepadGUID(char* guid)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|