2010-09-07 17:34:51 +02:00
|
|
|
//========================================================================
|
2011-03-06 01:46:39 +01:00
|
|
|
// GLFW - An OpenGL library
|
2012-08-28 15:03:57 +02:00
|
|
|
// Platform: X11
|
2010-09-07 17:41:26 +02:00
|
|
|
// API version: 3.0
|
2010-09-07 17:34:51 +02: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-08-26 18:11:31 +02:00
|
|
|
#ifdef _GLFW_USE_LINUX_JOYSTICKS
|
|
|
|
#include <linux/joystick.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2012-08-28 20:16:43 +02:00
|
|
|
#include <errno.h>
|
2012-08-26 18:11:31 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif // _GLFW_USE_LINUX_JOYSTICKS
|
|
|
|
|
2010-09-07 17:34:51 +02:00
|
|
|
|
|
|
|
//========================================================================
|
2012-08-26 18:11:31 +02:00
|
|
|
// Attempt to open the specified joystick device
|
2010-09-07 17:34:51 +02:00
|
|
|
//========================================================================
|
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
static int openJoystickDevice(int joy, const char* path)
|
|
|
|
{
|
2010-09-07 17:34:51 +02:00
|
|
|
#ifdef _GLFW_USE_LINUX_JOYSTICKS
|
2012-08-26 18:11:31 +02:00
|
|
|
char numAxes, numButtons;
|
|
|
|
int fd, version;
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
fd = open(path, O_NONBLOCK);
|
|
|
|
if (fd == -1)
|
|
|
|
return GL_FALSE;
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
_glfwLibrary.X11.joystick[joy].fd = fd;
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2012-08-26 18:11:31 +02: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 17:34:51 +02:00
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
ioctl(fd, JSIOCGAXES, &numAxes);
|
|
|
|
_glfwLibrary.X11.joystick[joy].numAxes = (int) numAxes;
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
ioctl(fd, JSIOCGBUTTONS, &numButtons);
|
|
|
|
_glfwLibrary.X11.joystick[joy].numButtons = (int) numButtons;
|
|
|
|
|
|
|
|
_glfwLibrary.X11.joystick[joy].axis =
|
|
|
|
(float*) malloc(sizeof(float) * numAxes);
|
|
|
|
if (_glfwLibrary.X11.joystick[joy].axis == NULL)
|
|
|
|
{
|
|
|
|
close(fd);
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
_glfwSetError(GLFW_OUT_OF_MEMORY, NULL);
|
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
_glfwLibrary.X11.joystick[joy].button =
|
|
|
|
(unsigned char*) malloc(sizeof(char) * numButtons);
|
|
|
|
if (_glfwLibrary.X11.joystick[joy].button == NULL)
|
|
|
|
{
|
|
|
|
free(_glfwLibrary.X11.joystick[joy].axis);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
_glfwSetError(GLFW_OUT_OF_MEMORY, NULL);
|
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
_glfwLibrary.X11.joystick[joy].present = GL_TRUE;
|
2010-09-07 17:34:51 +02:00
|
|
|
#endif // _GLFW_USE_LINUX_JOYSTICKS
|
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
return GL_TRUE;
|
|
|
|
}
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2010-09-15 16:44:43 +02:00
|
|
|
|
2010-09-07 17:34:51 +02:00
|
|
|
//========================================================================
|
2012-08-26 18:11:31 +02:00
|
|
|
// Polls for and processes events for all present joysticks
|
2010-09-07 17:34:51 +02:00
|
|
|
//========================================================================
|
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
static void pollJoystickEvents(void)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
|
|
|
#ifdef _GLFW_USE_LINUX_JOYSTICKS
|
2012-08-26 18:11:31 +02:00
|
|
|
int i;
|
2012-08-28 20:16:43 +02:00
|
|
|
ssize_t result;
|
2012-08-26 18:11:31 +02:00
|
|
|
struct js_event e;
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2010-09-08 15:51:25 +02:00
|
|
|
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2012-08-26 18:11:31 +02:00
|
|
|
if (!_glfwLibrary.X11.joystick[i].present)
|
|
|
|
continue;
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
// Read all queued events (non-blocking)
|
2012-08-28 20:16:43 +02:00
|
|
|
for (;;)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2012-08-28 20:16:43 +02:00
|
|
|
errno = 0;
|
|
|
|
result = read(_glfwLibrary.X11.joystick[i].fd, &e, sizeof(e));
|
|
|
|
|
|
|
|
if (errno == ENODEV)
|
|
|
|
_glfwLibrary.X11.joystick[i].present = GL_FALSE;
|
|
|
|
|
|
|
|
if (result <= 0)
|
|
|
|
break;
|
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
// We don't care if it's an init event or not
|
|
|
|
e.type &= ~JS_EVENT_INIT;
|
|
|
|
|
|
|
|
switch (e.type)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2012-08-26 18:11:31 +02:00
|
|
|
case JS_EVENT_AXIS:
|
|
|
|
_glfwLibrary.X11.joystick[i].axis[e.number] =
|
|
|
|
(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)
|
|
|
|
{
|
|
|
|
_glfwLibrary.X11.joystick[i].axis[e.number] =
|
|
|
|
-_glfwLibrary.X11.joystick[i].axis[e.number];
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JS_EVENT_BUTTON:
|
|
|
|
_glfwLibrary.X11.joystick[i].button[e.number] =
|
|
|
|
e.value ? GLFW_PRESS : GLFW_RELEASE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2010-09-07 17:34:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // _GLFW_USE_LINUX_JOYSTICKS
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW internal API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-09-07 17:34:51 +02:00
|
|
|
//========================================================================
|
2012-08-26 18:11:31 +02:00
|
|
|
// Initialize joystick interface
|
2010-09-07 17:34:51 +02:00
|
|
|
//========================================================================
|
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
void _glfwInitJoysticks(void)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
|
|
|
#ifdef _GLFW_USE_LINUX_JOYSTICKS
|
2012-08-26 18:11:31 +02:00
|
|
|
int i, j, joy = 0;
|
|
|
|
char path[20];
|
|
|
|
const char* bases[] =
|
|
|
|
{
|
|
|
|
"/dev/input/js",
|
|
|
|
"/dev/js"
|
|
|
|
};
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
for (i = 0; i < sizeof(bases) / sizeof(bases[0]); i++)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2012-08-26 18:11:31 +02:00
|
|
|
for (j = 0; j < 50; j++)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2012-08-26 18:11:31 +02:00
|
|
|
if (joy > GLFW_JOYSTICK_LAST)
|
|
|
|
break;
|
2010-09-08 15:51:25 +02:00
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
sprintf(path, "%s%i", bases[i], j);
|
|
|
|
if (openJoystickDevice(joy, path))
|
|
|
|
joy++;
|
2010-09-07 17:34:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // _GLFW_USE_LINUX_JOYSTICKS
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//========================================================================
|
2012-08-26 18:11:31 +02:00
|
|
|
// Close all opened joystick handles
|
2010-09-07 17:34:51 +02:00
|
|
|
//========================================================================
|
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
void _glfwTerminateJoysticks(void)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
|
|
|
#ifdef _GLFW_USE_LINUX_JOYSTICKS
|
|
|
|
int i;
|
|
|
|
|
2010-09-08 15:51:25 +02:00
|
|
|
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2012-08-26 18:11:31 +02:00
|
|
|
if (_glfwLibrary.X11.joystick[i].present)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2012-08-26 18:11:31 +02:00
|
|
|
close(_glfwLibrary.X11.joystick[i].fd);
|
|
|
|
free(_glfwLibrary.X11.joystick[i].axis);
|
|
|
|
free(_glfwLibrary.X11.joystick[i].button);
|
|
|
|
|
|
|
|
_glfwLibrary.X11.joystick[i].present = GL_FALSE;
|
2010-09-07 17:34:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // _GLFW_USE_LINUX_JOYSTICKS
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-15 16:44:43 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW platform API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2010-09-07 17:34:51 +02:00
|
|
|
|
|
|
|
//========================================================================
|
2010-09-08 04:15:49 +02:00
|
|
|
// Determine joystick capabilities
|
2010-09-07 17:34:51 +02:00
|
|
|
//========================================================================
|
|
|
|
|
2010-09-08 15:51:25 +02:00
|
|
|
int _glfwPlatformGetJoystickParam(int joy, int param)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2012-08-28 20:16:43 +02:00
|
|
|
pollJoystickEvents();
|
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
if (!_glfwLibrary.X11.joystick[joy].present)
|
2010-09-07 17:34:51 +02:00
|
|
|
return 0;
|
|
|
|
|
2010-09-08 15:51:25 +02:00
|
|
|
switch (param)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2010-09-08 16:02:02 +02:00
|
|
|
case GLFW_PRESENT:
|
|
|
|
return GL_TRUE;
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2010-09-08 16:02:02 +02:00
|
|
|
case GLFW_AXES:
|
2012-08-26 18:11:31 +02:00
|
|
|
return _glfwLibrary.X11.joystick[joy].numAxes;
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2010-09-08 16:02:02 +02:00
|
|
|
case GLFW_BUTTONS:
|
2012-08-26 18:11:31 +02:00
|
|
|
return _glfwLibrary.X11.joystick[joy].numButtons;
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2010-09-08 16:02:02 +02:00
|
|
|
default:
|
2012-08-26 18:28:30 +02:00
|
|
|
_glfwSetError(GLFW_INVALID_ENUM, NULL);
|
2010-09-07 17:34:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//========================================================================
|
2010-09-08 04:15:49 +02:00
|
|
|
// Get joystick axis positions
|
2010-09-07 17:34:51 +02:00
|
|
|
//========================================================================
|
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
int _glfwPlatformGetJoystickPos(int joy, float* pos, int numAxes)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2012-08-28 20:16:43 +02:00
|
|
|
pollJoystickEvents();
|
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
if (!_glfwLibrary.X11.joystick[joy].present)
|
2010-09-07 17:34:51 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
if (_glfwLibrary.X11.joystick[joy].numAxes < numAxes)
|
|
|
|
numAxes = _glfwLibrary.X11.joystick[joy].numAxes;
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
for (i = 0; i < numAxes; i++)
|
|
|
|
pos[i] = _glfwLibrary.X11.joystick[joy].axis[i];
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
return numAxes;
|
2010-09-07 17:34:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//========================================================================
|
2010-09-08 04:15:49 +02:00
|
|
|
// Get joystick button states
|
2010-09-07 17:34:51 +02:00
|
|
|
//========================================================================
|
|
|
|
|
2010-09-08 15:58:43 +02:00
|
|
|
int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
|
2012-08-26 18:11:31 +02:00
|
|
|
int numButtons)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2012-08-28 20:16:43 +02:00
|
|
|
pollJoystickEvents();
|
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
if (!_glfwLibrary.X11.joystick[joy].present)
|
2010-09-07 17:34:51 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
if (_glfwLibrary.X11.joystick[joy].numButtons < numButtons)
|
|
|
|
numButtons = _glfwLibrary.X11.joystick[joy].numButtons;
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
for (i = 0; i < numButtons; i++)
|
|
|
|
buttons[i] = _glfwLibrary.X11.joystick[joy].button[i];
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2012-08-26 18:11:31 +02:00
|
|
|
return numButtons;
|
2010-09-07 17:34:51 +02:00
|
|
|
}
|
|
|
|
|