1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-23 10:48:51 -05:00

Fixed broken Mir and Wayland builds.

Fixes #411.
This commit is contained in:
Camilla Berglund 2015-01-06 01:23:10 +01:00
parent fe7a4eb4ff
commit 0fcbaeb596
5 changed files with 82 additions and 68 deletions

View File

@ -54,16 +54,16 @@ static void openJoystickDevice(const char* path)
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++) for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
{ {
if (!_glfw.linux_js[joy].present) if (!_glfw.linux_js.js[joy].present)
continue; continue;
if (strcmp(_glfw.linux_js[joy].path, path) == 0) if (strcmp(_glfw.linux_js.js[joy].path, path) == 0)
return; return;
} }
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++) for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
{ {
if (!_glfw.linux_js[joy].present) if (!_glfw.linux_js.js[joy].present)
break; break;
} }
@ -74,7 +74,7 @@ static void openJoystickDevice(const char* path)
if (fd == -1) if (fd == -1)
return; return;
_glfw.linux_js[joy].fd = fd; _glfw.linux_js.js[joy].fd = fd;
// Verify that the joystick driver version is at least 1.0 // Verify that the joystick driver version is at least 1.0
ioctl(fd, JSIOCGVERSION, &version); ioctl(fd, JSIOCGVERSION, &version);
@ -88,19 +88,19 @@ static void openJoystickDevice(const char* path)
if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0) if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0)
strncpy(name, "Unknown", sizeof(name)); strncpy(name, "Unknown", sizeof(name));
_glfw.linux_js[joy].name = strdup(name); _glfw.linux_js.js[joy].name = strdup(name);
_glfw.linux_js[joy].path = strdup(path); _glfw.linux_js.js[joy].path = strdup(path);
ioctl(fd, JSIOCGAXES, &axisCount); ioctl(fd, JSIOCGAXES, &axisCount);
_glfw.linux_js[joy].axisCount = (int) axisCount; _glfw.linux_js.js[joy].axisCount = (int) axisCount;
ioctl(fd, JSIOCGBUTTONS, &buttonCount); ioctl(fd, JSIOCGBUTTONS, &buttonCount);
_glfw.linux_js[joy].buttonCount = (int) buttonCount; _glfw.linux_js.js[joy].buttonCount = (int) buttonCount;
_glfw.linux_js[joy].axes = calloc(axisCount, sizeof(float)); _glfw.linux_js.js[joy].axes = calloc(axisCount, sizeof(float));
_glfw.linux_js[joy].buttons = calloc(buttonCount, 1); _glfw.linux_js.js[joy].buttons = calloc(buttonCount, 1);
_glfw.linux_js[joy].present = GL_TRUE; _glfw.linux_js.js[joy].present = GL_TRUE;
#endif // __linux__ #endif // __linux__
} }
@ -114,14 +114,14 @@ static void pollJoystickEvents(void)
ssize_t offset = 0; ssize_t offset = 0;
char buffer[16384]; char buffer[16384];
const ssize_t size = read(_glfw.x11.inotify.fd, buffer, sizeof(buffer)); const ssize_t size = read(_glfw.linux_js.inotify, buffer, sizeof(buffer));
while (size > offset) while (size > offset)
{ {
regmatch_t match; regmatch_t match;
const struct inotify_event* e = (struct inotify_event*) (buffer + offset); const struct inotify_event* e = (struct inotify_event*) (buffer + offset);
if (regexec(&_glfw.x11.inotify.regex, e->name, 1, &match, 0) == 0) if (regexec(&_glfw.linux_js.regex, e->name, 1, &match, 0) == 0)
{ {
char path[20]; char path[20];
snprintf(path, sizeof(path), "/dev/input/%s", e->name); snprintf(path, sizeof(path), "/dev/input/%s", e->name);
@ -133,25 +133,25 @@ static void pollJoystickEvents(void)
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++) for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
{ {
if (!_glfw.linux_js[i].present) if (!_glfw.linux_js.js[i].present)
continue; continue;
// Read all queued events (non-blocking) // Read all queued events (non-blocking)
for (;;) for (;;)
{ {
errno = 0; errno = 0;
if (read(_glfw.linux_js[i].fd, &e, sizeof(e)) < 0) if (read(_glfw.linux_js.js[i].fd, &e, sizeof(e)) < 0)
{ {
if (errno == ENODEV) if (errno == ENODEV)
{ {
// The joystick was disconnected // The joystick was disconnected
free(_glfw.linux_js[i].axes); free(_glfw.linux_js.js[i].axes);
free(_glfw.linux_js[i].buttons); free(_glfw.linux_js.js[i].buttons);
free(_glfw.linux_js[i].name); free(_glfw.linux_js.js[i].name);
free(_glfw.linux_js[i].path); free(_glfw.linux_js.js[i].path);
memset(&_glfw.linux_js[i], 0, sizeof(_glfw.linux_js[i])); memset(&_glfw.linux_js.js[i], 0, sizeof(_glfw.linux_js.js[i]));
} }
break; break;
@ -163,12 +163,12 @@ static void pollJoystickEvents(void)
switch (e.type) switch (e.type)
{ {
case JS_EVENT_AXIS: case JS_EVENT_AXIS:
_glfw.linux_js[i].axes[e.number] = _glfw.linux_js.js[i].axes[e.number] =
(float) e.value / 32767.0f; (float) e.value / 32767.0f;
break; break;
case JS_EVENT_BUTTON: case JS_EVENT_BUTTON:
_glfw.linux_js[i].buttons[e.number] = _glfw.linux_js.js[i].buttons[e.number] =
e.value ? GLFW_PRESS : GLFW_RELEASE; e.value ? GLFW_PRESS : GLFW_RELEASE;
break; break;
@ -194,36 +194,36 @@ int _glfwInitJoysticks(void)
DIR* dir; DIR* dir;
struct dirent* entry; struct dirent* entry;
_glfw.x11.inotify.fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); _glfw.linux_js.inotify = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (_glfw.x11.inotify.fd == -1) if (_glfw.linux_js.inotify == -1)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, "X11: Failed to initialize inotify"); _glfwInputError(GLFW_PLATFORM_ERROR, "Linux: Failed to initialize inotify");
return GL_FALSE; return GL_FALSE;
} }
// HACK: Register for IN_ATTRIB as well to get notified when udev is done // 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 // This works well in practice but the true way is libudev
_glfw.x11.inotify.wd = inotify_add_watch(_glfw.x11.inotify.fd, _glfw.linux_js.watch = inotify_add_watch(_glfw.linux_js.inotify,
dirname, dirname,
IN_CREATE | IN_ATTRIB); IN_CREATE | IN_ATTRIB);
if (_glfw.x11.inotify.wd == -1) if (_glfw.linux_js.watch == -1)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,
"X11: Failed to add watch to %s", dirname); "Linux: Failed to add watch to %s", dirname);
return GL_FALSE; return GL_FALSE;
} }
if (regcomp(&_glfw.x11.inotify.regex, "^js[0-9]\\+$", 0) != 0) if (regcomp(&_glfw.linux_js.regex, "^js[0-9]\\+$", 0) != 0)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, "X11: Failed to compile regex"); _glfwInputError(GLFW_PLATFORM_ERROR, "Linux: Failed to compile regex");
return GL_FALSE; return GL_FALSE;
} }
dir = opendir(dirname); dir = opendir(dirname);
if (!dir) if (!dir)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, "X11: Failed to open %s", dirname); _glfwInputError(GLFW_PLATFORM_ERROR, "Linux: Failed to open %s", dirname);
return GL_FALSE; return GL_FALSE;
} }
@ -232,7 +232,7 @@ int _glfwInitJoysticks(void)
char path[20]; char path[20];
regmatch_t match; regmatch_t match;
if (regexec(&_glfw.x11.inotify.regex, entry->d_name, 1, &match, 0) != 0) if (regexec(&_glfw.linux_js.regex, entry->d_name, 1, &match, 0) != 0)
continue; continue;
snprintf(path, sizeof(path), "%s/%s", dirname, entry->d_name); snprintf(path, sizeof(path), "%s/%s", dirname, entry->d_name);
@ -254,23 +254,23 @@ void _glfwTerminateJoysticks(void)
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++) for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
{ {
if (_glfw.linux_js[i].present) if (_glfw.linux_js.js[i].present)
{ {
close(_glfw.linux_js[i].fd); close(_glfw.linux_js.js[i].fd);
free(_glfw.linux_js[i].axes); free(_glfw.linux_js.js[i].axes);
free(_glfw.linux_js[i].buttons); free(_glfw.linux_js.js[i].buttons);
free(_glfw.linux_js[i].name); free(_glfw.linux_js.js[i].name);
free(_glfw.linux_js[i].path); free(_glfw.linux_js.js[i].path);
} }
} }
regfree(&_glfw.x11.inotify.regex); regfree(&_glfw.linux_js.regex);
if (_glfw.x11.inotify.wd > 0) if (_glfw.linux_js.watch > 0)
close(_glfw.x11.inotify.wd); close(_glfw.linux_js.watch);
if (_glfw.x11.inotify.fd > 0) if (_glfw.linux_js.inotify > 0)
close(_glfw.x11.inotify.fd); close(_glfw.linux_js.inotify);
#endif // __linux__ #endif // __linux__
} }
@ -283,29 +283,29 @@ int _glfwPlatformJoystickPresent(int joy)
{ {
pollJoystickEvents(); pollJoystickEvents();
return _glfw.linux_js[joy].present; return _glfw.linux_js.js[joy].present;
} }
const float* _glfwPlatformGetJoystickAxes(int joy, int* count) const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
{ {
pollJoystickEvents(); pollJoystickEvents();
*count = _glfw.linux_js[joy].axisCount; *count = _glfw.linux_js.js[joy].axisCount;
return _glfw.linux_js[joy].axes; return _glfw.linux_js.js[joy].axes;
} }
const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count) const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
{ {
pollJoystickEvents(); pollJoystickEvents();
*count = _glfw.linux_js[joy].buttonCount; *count = _glfw.linux_js.js[joy].buttonCount;
return _glfw.linux_js[joy].buttons; return _glfw.linux_js.js[joy].buttons;
} }
const char* _glfwPlatformGetJoystickName(int joy) const char* _glfwPlatformGetJoystickName(int joy)
{ {
pollJoystickEvents(); pollJoystickEvents();
return _glfw.linux_js[joy].name; return _glfw.linux_js.js[joy].name;
} }

View File

@ -27,13 +27,17 @@
#ifndef _linux_joystick_h_ #ifndef _linux_joystick_h_
#define _linux_joystick_h_ #define _linux_joystick_h_
#include <regex.h>
#define _GLFW_PLATFORM_LIBRARY_JOYSTICK_STATE \ #define _GLFW_PLATFORM_LIBRARY_JOYSTICK_STATE \
_GLFWjoystickLinux linux_js[GLFW_JOYSTICK_LAST + 1] _GLFWjoystickLinux linux_js
// Linux-specific per-joystick data // Linux-specific joystick API data
// //
typedef struct _GLFWjoystickLinux typedef struct _GLFWjoystickLinux
{
struct
{ {
int present; int present;
int fd; int fd;
@ -43,6 +47,13 @@ typedef struct _GLFWjoystickLinux
int buttonCount; int buttonCount;
char* name; char* name;
char* path; char* path;
} js[GLFW_JOYSTICK_LAST + 1];
#if defined(__linux__)
int inotify;
int watch;
regex_t regex;
#endif /*__linux__*/
} _GLFWjoystickLinux; } _GLFWjoystickLinux;

View File

@ -651,6 +651,12 @@ void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
"Mir: Unsupported Function %s!", __PRETTY_FUNCTION__); "Mir: Unsupported Function %s!", __PRETTY_FUNCTION__);
} }
void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Mir: Unsupported Function %s!", __PRETTY_FUNCTION__);
}
void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos) void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos)
{ {
_glfwInputError(GLFW_PLATFORM_ERROR, _glfwInputError(GLFW_PLATFORM_ERROR,

View File

@ -372,6 +372,12 @@ void _glfwPlatformPostEmptyEvent(void)
wl_display_sync(_glfw.wl.display); wl_display_sync(_glfw.wl.display);
} }
void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos)
{
// TODO
fprintf(stderr, "_glfwPlatformGetCursorPos not implemented yet\n");
}
void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y) void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
{ {
// A Wayland client can not set the cursor position // A Wayland client can not set the cursor position

View File

@ -31,7 +31,6 @@
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <stdint.h> #include <stdint.h>
#include <regex.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/keysym.h> #include <X11/keysym.h>
@ -216,14 +215,6 @@ typedef struct _GLFWlibraryX11
int versionMinor; int versionMinor;
} xinerama; } xinerama;
#if defined(__linux__)
struct {
int fd;
int wd;
regex_t regex;
} inotify;
#endif
} _GLFWlibraryX11; } _GLFWlibraryX11;