mirror of
https://github.com/glfw/glfw.git
synced 2024-11-14 22:34:42 +00:00
8e99996321
This patch introduces a new backend that enables GLFW applications to run on Wayland. For now, only output is supported (windowed and fullscreen). Pointer cursor management, input devices, clipboard etc are not supported yet. There are some concepts that can not be supported, more specifically glfwSetWindowPos, glfwGetWindowPos and glfwSetCursorPos, as they are not supported by Wayland. This patch also changes the time and joystick implementations used by the X11 backend to be shared between the Wayland backend and the X11 backend.
265 lines
7.2 KiB
C
265 lines
7.2 KiB
C
//========================================================================
|
|
// GLFW 3.1 Linux - 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"
|
|
|
|
#ifdef __linux__
|
|
#include <linux/joystick.h>
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <errno.h>
|
|
#include <regex.h>
|
|
#include <dirent.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#endif // __linux__
|
|
|
|
|
|
// Attempt to open the specified joystick device
|
|
//
|
|
static int openJoystickDevice(int joy, const char* path)
|
|
{
|
|
#ifdef __linux__
|
|
char axisCount, buttonCount;
|
|
char name[256];
|
|
int fd, version;
|
|
|
|
fd = open(path, O_RDONLY | O_NONBLOCK);
|
|
if (fd == -1)
|
|
return GL_FALSE;
|
|
|
|
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].fd = fd;
|
|
|
|
// 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;
|
|
}
|
|
|
|
if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0)
|
|
strncpy(name, "Unknown", sizeof(name));
|
|
|
|
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].name = strdup(name);
|
|
|
|
ioctl(fd, JSIOCGAXES, &axisCount);
|
|
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].axisCount = (int) axisCount;
|
|
|
|
ioctl(fd, JSIOCGBUTTONS, &buttonCount);
|
|
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].buttonCount = (int) buttonCount;
|
|
|
|
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].axes =
|
|
calloc(axisCount, sizeof(float));
|
|
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].buttons =
|
|
calloc(buttonCount, 1);
|
|
|
|
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].present = GL_TRUE;
|
|
#endif // __linux__
|
|
|
|
return GL_TRUE;
|
|
}
|
|
|
|
// Polls for and processes events for all present joysticks
|
|
//
|
|
static void pollJoystickEvents(void)
|
|
{
|
|
#ifdef __linux__
|
|
int i;
|
|
ssize_t result;
|
|
struct js_event e;
|
|
|
|
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
|
|
{
|
|
if (!_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].present)
|
|
continue;
|
|
|
|
// Read all queued events (non-blocking)
|
|
for (;;)
|
|
{
|
|
errno = 0;
|
|
result = read(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].fd,
|
|
&e,
|
|
sizeof(e));
|
|
|
|
if (errno == ENODEV)
|
|
{
|
|
free(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].axes);
|
|
free(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].buttons);
|
|
free(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].name);
|
|
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].present = GL_FALSE;
|
|
}
|
|
|
|
if (result == -1)
|
|
break;
|
|
|
|
// We don't care if it's an init event or not
|
|
e.type &= ~JS_EVENT_INIT;
|
|
|
|
switch (e.type)
|
|
{
|
|
case JS_EVENT_AXIS:
|
|
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].axes[e.number] =
|
|
(float) e.value / 32767.0f;
|
|
break;
|
|
|
|
case JS_EVENT_BUTTON:
|
|
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].buttons[e.number] =
|
|
e.value ? GLFW_PRESS : GLFW_RELEASE;
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
#endif // __linux__
|
|
}
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
////// GLFW internal API //////
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Initialize joystick interface
|
|
//
|
|
void _glfwInitJoysticks(void)
|
|
{
|
|
#ifdef __linux__
|
|
int joy = 0;
|
|
size_t i;
|
|
regex_t regex;
|
|
DIR* dir;
|
|
const char* dirs[] =
|
|
{
|
|
"/dev/input",
|
|
"/dev"
|
|
};
|
|
|
|
if (regcomp(®ex, "^js[0-9]\\+$", 0) != 0)
|
|
{
|
|
_glfwInputError(GLFW_PLATFORM_ERROR, "X11: Failed to compile regex");
|
|
return;
|
|
}
|
|
|
|
for (i = 0; i < sizeof(dirs) / sizeof(dirs[0]); i++)
|
|
{
|
|
struct dirent* entry;
|
|
|
|
dir = opendir(dirs[i]);
|
|
if (!dir)
|
|
continue;
|
|
|
|
while ((entry = readdir(dir)))
|
|
{
|
|
char path[20];
|
|
regmatch_t match;
|
|
|
|
if (regexec(®ex, entry->d_name, 1, &match, 0) != 0)
|
|
continue;
|
|
|
|
snprintf(path, sizeof(path), "%s/%s", dirs[i], entry->d_name);
|
|
if (openJoystickDevice(joy, path))
|
|
joy++;
|
|
}
|
|
|
|
closedir(dir);
|
|
}
|
|
|
|
regfree(®ex);
|
|
#endif // __linux__
|
|
}
|
|
|
|
// Close all opened joystick handles
|
|
//
|
|
void _glfwTerminateJoysticks(void)
|
|
{
|
|
#ifdef __linux__
|
|
int i;
|
|
|
|
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
|
|
{
|
|
if (_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].present)
|
|
{
|
|
close(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].fd);
|
|
free(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].axes);
|
|
free(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].buttons);
|
|
free(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].name);
|
|
|
|
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].present = GL_FALSE;
|
|
}
|
|
}
|
|
#endif // __linux__
|
|
}
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
////// GLFW platform API //////
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
int _glfwPlatformJoystickPresent(int joy)
|
|
{
|
|
pollJoystickEvents();
|
|
|
|
return _GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].present;
|
|
}
|
|
|
|
const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
|
|
{
|
|
pollJoystickEvents();
|
|
|
|
if (!_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].present)
|
|
return NULL;
|
|
|
|
*count = _GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].axisCount;
|
|
return _GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].axes;
|
|
}
|
|
|
|
const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
|
|
{
|
|
pollJoystickEvents();
|
|
|
|
if (!_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].present)
|
|
return NULL;
|
|
|
|
*count = _GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].buttonCount;
|
|
return _GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].buttons;
|
|
}
|
|
|
|
const char* _glfwPlatformGetJoystickName(int joy)
|
|
{
|
|
pollJoystickEvents();
|
|
|
|
return _GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].name;
|
|
}
|
|
|