This commit is contained in:
Patrick Walton 2014-12-12 18:36:19 +00:00
commit 118f39b296
3 changed files with 22 additions and 13 deletions

View File

@ -29,10 +29,12 @@
#include <X11/Xresource.h> #include <X11/Xresource.h>
#include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
// Translate an X11 key code to a GLFW key code. // Translate an X11 key code to a GLFW key code.
@ -731,6 +733,8 @@ int _glfwPlatformInit(void)
_glfw.x11.root = RootWindow(_glfw.x11.display, _glfw.x11.screen); _glfw.x11.root = RootWindow(_glfw.x11.display, _glfw.x11.screen);
_glfw.x11.context = XUniqueContext(); _glfw.x11.context = XUniqueContext();
pipe2(_glfw.x11.emptyEventFDs, O_CLOEXEC);
if (!initExtensions()) if (!initExtensions())
return GL_FALSE; return GL_FALSE;

View File

@ -121,6 +121,9 @@ typedef struct _GLFWlibraryX11
// LUT for mapping X11 key codes to GLFW key codes // LUT for mapping X11 key codes to GLFW key codes
int keyCodeLUT[256]; int keyCodeLUT[256];
// File descriptors for glfwPostEmptyEvent()
int emptyEventFDs[2];
// Window manager atoms // Window manager atoms
Atom WM_PROTOCOLS; Atom WM_PROTOCOLS;
Atom WM_STATE; Atom WM_STATE;

View File

@ -43,6 +43,8 @@
#define Button6 6 #define Button6 6
#define Button7 7 #define Button7 7
#define MAX(a,b) ((a) > (b) ? (a) : (b))
typedef struct typedef struct
{ {
unsigned long flags; unsigned long flags;
@ -1671,15 +1673,24 @@ void _glfwPlatformWaitEvents(void)
{ {
fd_set fds; fd_set fds;
const int fd = ConnectionNumber(_glfw.x11.display); const int fd = ConnectionNumber(_glfw.x11.display);
const int emptyEventFD = _glfw.x11.emptyEventFDs[0];
FD_ZERO(&fds); FD_ZERO(&fds);
FD_SET(fd, &fds); FD_SET(fd, &fds);
FD_SET(emptyEventFD, &fds);
// select(1) is used instead of an X function like XNextEvent, as the // select(1) is used instead of an X function like XNextEvent, as the
// wait inside those are guarded by the mutex protecting the display // wait inside those are guarded by the mutex protecting the display
// struct, locking out other threads from using X (including GLX) // struct, locking out other threads from using X (including GLX).
if (select(fd + 1, &fds, NULL, NULL, NULL) < 0) // Also, it lets us avoid synchronization on the display, which is
// important since glfwPostEmptyEvent() can be called from other
// threads.
if (select(MAX(fd, emptyEventFD) + 1, &fds, NULL, NULL, NULL) < 0)
return; return;
char zero;
if (FD_ISSET(emptyEventFD, &fds))
read(emptyEventFD, &zero, 1);
} }
_glfwPlatformPollEvents(); _glfwPlatformPollEvents();
@ -1687,17 +1698,8 @@ void _glfwPlatformWaitEvents(void)
void _glfwPlatformPostEmptyEvent(void) void _glfwPlatformPostEmptyEvent(void)
{ {
XEvent event; char zero = 0;
_GLFWwindow* window = _glfw.windowListHead; write(_glfw.x11.emptyEventFDs[1], &zero, 1);
memset(&event, 0, sizeof(event));
event.type = ClientMessage;
event.xclient.window = window->x11.handle;
event.xclient.format = 32; // Data is 32-bit longs
event.xclient.message_type = _glfw.x11._NULL;
XSendEvent(_glfw.x11.display, window->x11.handle, False, 0, &event);
XFlush(_glfw.x11.display);
} }
void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y) void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)