2010-09-07 15:34:51 +00:00
|
|
|
//========================================================================
|
2011-03-06 00:46:39 +00:00
|
|
|
// GLFW - An OpenGL library
|
2012-08-28 13:03:57 +00:00
|
|
|
// Platform: X11
|
2010-09-07 15:41:26 +00:00
|
|
|
// API version: 3.0
|
2010-09-07 15:34:51 +00:00
|
|
|
// WWW: http://www.glfw.org/
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// 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"
|
|
|
|
|
2012-11-05 15:19:11 +00:00
|
|
|
#ifdef __linux__
|
2012-08-26 16:11:31 +00:00
|
|
|
#include <linux/joystick.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2012-08-28 18:16:43 +00:00
|
|
|
#include <errno.h>
|
2012-09-07 13:27:41 +00:00
|
|
|
#include <regex.h>
|
|
|
|
#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>
|
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
|
|
|
//
|
2012-08-26 16:11:31 +00:00
|
|
|
static int openJoystickDevice(int joy, const char* path)
|
|
|
|
{
|
2012-11-05 15:19:11 +00:00
|
|
|
#ifdef __linux__
|
2012-08-26 16:11:31 +00:00
|
|
|
char numAxes, numButtons;
|
2012-09-06 23:01:34 +00:00
|
|
|
char name[256];
|
2012-08-26 16:11:31 +00:00
|
|
|
int fd, version;
|
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 GL_FALSE;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
_glfw.x11.joystick[joy].fd = fd;
|
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 GL_FALSE;
|
|
|
|
}
|
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-01-02 00:40:42 +00:00
|
|
|
_glfw.x11.joystick[joy].name = strdup(name);
|
2012-09-06 23:01:34 +00:00
|
|
|
|
2012-08-26 16:11:31 +00:00
|
|
|
ioctl(fd, JSIOCGAXES, &numAxes);
|
2013-01-02 00:40:42 +00:00
|
|
|
_glfw.x11.joystick[joy].numAxes = (int) numAxes;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-08-26 16:11:31 +00:00
|
|
|
ioctl(fd, JSIOCGBUTTONS, &numButtons);
|
2013-01-02 00:40:42 +00:00
|
|
|
_glfw.x11.joystick[joy].numButtons = (int) numButtons;
|
2012-08-26 16:11:31 +00:00
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
_glfw.x11.joystick[joy].axis = (float*) malloc(sizeof(float) * numAxes);
|
|
|
|
_glfw.x11.joystick[joy].button = (unsigned char*) malloc(numButtons);
|
2012-08-26 16:11:31 +00:00
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
_glfw.x11.joystick[joy].present = GL_TRUE;
|
2012-11-05 15:19:11 +00:00
|
|
|
#endif // __linux__
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-08-26 16:11:31 +00:00
|
|
|
return GL_TRUE;
|
|
|
|
}
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-08-26 16:11:31 +00:00
|
|
|
// Polls for and processes events for all present joysticks
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2012-08-26 16:11:31 +00:00
|
|
|
static void pollJoystickEvents(void)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2012-11-05 15:19:11 +00:00
|
|
|
#ifdef __linux__
|
2012-08-26 16:11:31 +00:00
|
|
|
int i;
|
2012-08-28 18:16:43 +00:00
|
|
|
ssize_t result;
|
2012-08-26 16:11:31 +00:00
|
|
|
struct js_event e;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2010-09-08 13:51:25 +00:00
|
|
|
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
if (!_glfw.x11.joystick[i].present)
|
2012-08-26 16:11:31 +00:00
|
|
|
continue;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-08-26 16:11:31 +00:00
|
|
|
// Read all queued events (non-blocking)
|
2012-08-28 18:16:43 +00:00
|
|
|
for (;;)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2012-08-28 18:16:43 +00:00
|
|
|
errno = 0;
|
2013-01-02 00:40:42 +00:00
|
|
|
result = read(_glfw.x11.joystick[i].fd, &e, sizeof(e));
|
2012-08-28 18:16:43 +00:00
|
|
|
|
|
|
|
if (errno == ENODEV)
|
2013-01-02 00:40:42 +00:00
|
|
|
_glfw.x11.joystick[i].present = GL_FALSE;
|
2012-08-28 18:16:43 +00:00
|
|
|
|
2012-09-06 23:01:17 +00:00
|
|
|
if (result == -1)
|
2012-08-28 18:16:43 +00:00
|
|
|
break;
|
|
|
|
|
2012-08-26 16:11:31 +00:00
|
|
|
// We don't care if it's an init event or not
|
|
|
|
e.type &= ~JS_EVENT_INIT;
|
|
|
|
|
|
|
|
switch (e.type)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2012-08-26 16:11:31 +00:00
|
|
|
case JS_EVENT_AXIS:
|
2013-01-02 00:40:42 +00:00
|
|
|
_glfw.x11.joystick[i].axis[e.number] =
|
2012-08-26 16:11:31 +00:00
|
|
|
(float) e.value / 32767.0f;
|
|
|
|
|
|
|
|
// We need to change the sign for the Y axes, so that
|
|
|
|
// positive = up/forward, according to the GLFW spec.
|
|
|
|
if (e.number & 1)
|
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
_glfw.x11.joystick[i].axis[e.number] =
|
|
|
|
-_glfw.x11.joystick[i].axis[e.number];
|
2012-08-26 16:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JS_EVENT_BUTTON:
|
2013-01-02 00:40:42 +00:00
|
|
|
_glfw.x11.joystick[i].button[e.number] =
|
2012-08-26 16:11:31 +00:00
|
|
|
e.value ? GLFW_PRESS : GLFW_RELEASE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-05 15:19:11 +00:00
|
|
|
#endif // __linux__
|
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
|
|
|
//
|
2012-09-07 13:27:41 +00:00
|
|
|
int _glfwInitJoysticks(void)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2012-11-05 15:19:11 +00:00
|
|
|
#ifdef __linux__
|
2012-09-07 13:27:41 +00:00
|
|
|
int i, joy = 0;
|
|
|
|
regex_t regex;
|
|
|
|
DIR* dir;
|
2012-09-07 13:48:03 +00:00
|
|
|
const char* dirs[] =
|
2012-08-26 16:11:31 +00:00
|
|
|
{
|
2012-09-07 13:27:41 +00:00
|
|
|
"/dev/input",
|
|
|
|
"/dev"
|
2012-08-26 16:11:31 +00:00
|
|
|
};
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-09-07 13:27:41 +00:00
|
|
|
if (regcomp(®ex, "^js[0-9]\\+$", 0) != 0)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2012-12-31 20:05:28 +00:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR, "X11: Failed to compile regex");
|
2012-09-07 13:27:41 +00:00
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
2012-09-07 13:48:03 +00:00
|
|
|
for (i = 0; i < sizeof(dirs) / sizeof(dirs[0]); i++)
|
2012-09-07 13:27:41 +00:00
|
|
|
{
|
|
|
|
struct dirent* entry;
|
|
|
|
|
2012-09-07 13:48:03 +00:00
|
|
|
dir = opendir(dirs[i]);
|
2012-09-07 13:27:41 +00:00
|
|
|
if (!dir)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
while ((entry = readdir(dir)))
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2012-09-07 13:27:41 +00:00
|
|
|
char path[20];
|
|
|
|
regmatch_t match;
|
2010-09-08 13:51:25 +00:00
|
|
|
|
2012-09-07 13:27:41 +00:00
|
|
|
if (regexec(®ex, entry->d_name, 1, &match, 0) != 0)
|
|
|
|
continue;
|
|
|
|
|
2012-09-07 13:48:03 +00:00
|
|
|
snprintf(path, sizeof(path), "%s/%s", dirs[i], entry->d_name);
|
2012-08-26 16:11:31 +00:00
|
|
|
if (openJoystickDevice(joy, path))
|
|
|
|
joy++;
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
2012-09-07 13:27:41 +00:00
|
|
|
|
|
|
|
closedir(dir);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
2012-09-07 13:27:41 +00:00
|
|
|
|
|
|
|
regfree(®ex);
|
2012-11-05 15:19:11 +00:00
|
|
|
#endif // __linux__
|
2012-09-07 13:27:41 +00:00
|
|
|
|
|
|
|
return GL_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
|
|
|
//
|
2012-08-26 16:11:31 +00:00
|
|
|
void _glfwTerminateJoysticks(void)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2012-11-05 15:19:11 +00:00
|
|
|
#ifdef __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
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
if (_glfw.x11.joystick[i].present)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
close(_glfw.x11.joystick[i].fd);
|
|
|
|
free(_glfw.x11.joystick[i].axis);
|
|
|
|
free(_glfw.x11.joystick[i].button);
|
|
|
|
free(_glfw.x11.joystick[i].name);
|
2012-08-26 16:11:31 +00:00
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
_glfw.x11.joystick[i].present = GL_FALSE;
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-05 15:19:11 +00:00
|
|
|
#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
|
|
|
|
2010-09-08 13:51:25 +00:00
|
|
|
int _glfwPlatformGetJoystickParam(int joy, int param)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2012-08-28 18:16:43 +00:00
|
|
|
pollJoystickEvents();
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
if (!_glfw.x11.joystick[joy].present)
|
2010-09-07 15:34:51 +00:00
|
|
|
return 0;
|
|
|
|
|
2010-09-08 13:51:25 +00:00
|
|
|
switch (param)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2010-09-08 14:02:02 +00:00
|
|
|
case GLFW_PRESENT:
|
|
|
|
return GL_TRUE;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2010-09-08 14:02:02 +00:00
|
|
|
case GLFW_AXES:
|
2013-01-02 00:40:42 +00:00
|
|
|
return _glfw.x11.joystick[joy].numAxes;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2010-09-08 14:02:02 +00:00
|
|
|
case GLFW_BUTTONS:
|
2013-01-02 00:40:42 +00:00
|
|
|
return _glfw.x11.joystick[joy].numButtons;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2010-09-08 14:02:02 +00:00
|
|
|
default:
|
2012-12-31 20:05:28 +00:00
|
|
|
_glfwInputError(GLFW_INVALID_ENUM, NULL);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-29 16:31:12 +00:00
|
|
|
int _glfwPlatformGetJoystickAxes(int joy, float* axes, int numAxes)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2012-08-28 18:16:43 +00:00
|
|
|
pollJoystickEvents();
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
if (!_glfw.x11.joystick[joy].present)
|
2010-09-07 15:34:51 +00:00
|
|
|
return 0;
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
if (_glfw.x11.joystick[joy].numAxes < numAxes)
|
|
|
|
numAxes = _glfw.x11.joystick[joy].numAxes;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-08-26 16:11:31 +00:00
|
|
|
for (i = 0; i < numAxes; i++)
|
2013-01-02 00:40:42 +00:00
|
|
|
axes[i] = _glfw.x11.joystick[joy].axis[i];
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-08-26 16:11:31 +00:00
|
|
|
return numAxes;
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2010-09-08 13:58:43 +00:00
|
|
|
int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
|
2012-08-26 16:11:31 +00:00
|
|
|
int numButtons)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2012-08-28 18:16:43 +00:00
|
|
|
pollJoystickEvents();
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
if (!_glfw.x11.joystick[joy].present)
|
2010-09-07 15:34:51 +00:00
|
|
|
return 0;
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
if (_glfw.x11.joystick[joy].numButtons < numButtons)
|
|
|
|
numButtons = _glfw.x11.joystick[joy].numButtons;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-08-26 16:11:31 +00:00
|
|
|
for (i = 0; i < numButtons; i++)
|
2013-01-02 00:40:42 +00:00
|
|
|
buttons[i] = _glfw.x11.joystick[joy].button[i];
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2012-08-26 16:11:31 +00:00
|
|
|
return numButtons;
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-09-06 23:01:34 +00:00
|
|
|
const char* _glfwPlatformGetJoystickName(int joy)
|
|
|
|
{
|
2013-01-02 00:40:42 +00:00
|
|
|
if (!_glfw.x11.joystick[joy].present)
|
2012-11-08 15:26:43 +00:00
|
|
|
return NULL;
|
|
|
|
|
2013-01-02 00:40:42 +00:00
|
|
|
return _glfw.x11.joystick[joy].name;
|
2012-09-06 23:01:34 +00:00
|
|
|
}
|
|
|
|
|