Update glfw from upstream

Fixes https://github.com/glfw/glfw/issues/1281
This commit is contained in:
Kovid Goyal 2018-06-07 21:46:01 +05:30
parent 6c72c93b02
commit dd085a9297
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 93 additions and 25 deletions

42
glfw/x11_init.c vendored
View File

@ -34,6 +34,7 @@
#include <limits.h>
#include <stdio.h>
#include <locale.h>
#include <fcntl.h>
// Check whether the specified atom is supported
@ -589,6 +590,23 @@ Cursor _glfwCreateCursorX11(const GLFWimage* image, int xhot, int yhot)
return cursor;
}
static inline GLFWbool
selfPipe(int fds[2]) {
int flags;
flags = pipe(fds);
if (flags != 0) return GLFW_FALSE;
for (int i = 0; i < 2; i++) {
flags = fcntl(fds[i], F_GETFD);
if (flags == -1) { return GLFW_FALSE; }
if (fcntl(fds[i], F_SETFD, flags | FD_CLOEXEC) == -1) { return GLFW_FALSE; }
flags = fcntl(fds[i], F_GETFL);
if (flags == -1) { return GLFW_FALSE; }
if (fcntl(fds[i], F_SETFL, flags | O_NONBLOCK) == -1) { return GLFW_FALSE; }
}
return GLFW_TRUE;
}
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
@ -599,6 +617,13 @@ int _glfwPlatformInit(void)
XInitThreads();
XrmInitialize();
if (!selfPipe(_glfw.x11.eventLoopData.wakeupFds))
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"X11: failed to create self pipe");
return GLFW_FALSE;
}
_glfw.x11.display = XOpenDisplay(NULL);
if (!_glfw.x11.display)
{
@ -617,6 +642,12 @@ int _glfwPlatformInit(void)
return GLFW_FALSE;
}
_glfw.x11.eventLoopData.fds[0].fd = _glfw.x11.eventLoopData.wakeupFds[0];
_glfw.x11.eventLoopData.fds[1].fd = ConnectionNumber(_glfw.x11.display);
_glfw.x11.eventLoopData.fds[0].events = POLLIN;
_glfw.x11.eventLoopData.fds[1].events = POLLIN;
_glfw.x11.eventLoopData.fds[2].events = POLLIN;
_glfw.x11.screen = DefaultScreen(_glfw.x11.display);
_glfw.x11.root = RootWindow(_glfw.x11.display, _glfw.x11.screen);
_glfw.x11.context = XUniqueContext();
@ -668,6 +699,17 @@ void _glfwPlatformTerminate(void)
{
XCloseDisplay(_glfw.x11.display);
_glfw.x11.display = NULL;
if (_glfw.x11.eventLoopData.wakeupFds[0] > 0)
{
close(_glfw.x11.eventLoopData.wakeupFds[0]);
_glfw.x11.eventLoopData.wakeupFds[0] = -1;
}
if (_glfw.x11.eventLoopData.wakeupFds[1] > 0)
{
_glfw.x11.eventLoopData.wakeupFds[1] = -1;
close(_glfw.x11.eventLoopData.wakeupFds[1]);
}
_glfw.x11.eventLoopData.fds[0].fd = -1;
}
if (_glfw.x11.xcursor.handle)

6
glfw/x11_platform.h vendored
View File

@ -29,6 +29,7 @@
#include <signal.h>
#include <stdint.h>
#include <dlfcn.h>
#include <poll.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
@ -378,6 +379,11 @@ typedef struct _GLFWlibraryX11
PFN_XRenderFindVisualFormat FindVisualFormat;
} xrender;
struct {
struct pollfd fds[3];
int wakeupFds[2];
} eventLoopData;
} _GLFWlibraryX11;
// X11-specific per-monitor data

70
glfw/x11_window.c vendored
View File

@ -30,8 +30,6 @@
#include <X11/cursorfont.h>
#include <X11/Xmd.h>
#include <sys/select.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
@ -50,50 +48,67 @@
#define _GLFW_XDND_VERSION 5
static inline void
drainFd(int fd) {
static char drain_buf[64];
while(1) {
ssize_t len = read(fd, drain_buf, sizeof(drain_buf));
if (len < 0) {
if (errno == EINTR) continue;
break;
}
break;
}
}
// Wait for data to arrive using select
// Wait for data to arrive using poll
// This avoids blocking other threads via the per-display Xlib lock that also
// covers GLX functions
//
static GLFWbool waitForEvent(double* timeout)
{
fd_set fds;
const int fd = ConnectionNumber(_glfw.x11.display);
int count = fd + 1;
nfds_t count = 2;
#if defined(__linux__)
if (_glfw.linjs.inotify > fd)
count = _glfw.linjs.inotify + 1;
if (_glfw.linjs.inotify > 0)
{
count = 3;
_glfw.x11.eventLoopData.fds[2].fd = _glfw.linjs.inotify;
}
#endif
for (;;)
{
FD_ZERO(&fds);
FD_SET(fd, &fds);
#if defined(__linux__)
if (_glfw.linjs.inotify > 0)
FD_SET(_glfw.linjs.inotify, &fds);
#endif
for (nfds_t i = 0; i < count; i++) _glfw.x11.eventLoopData.fds[i].revents = 0;
if (timeout)
{
const long seconds = (long) *timeout;
const long microseconds = (long) ((*timeout - seconds) * 1e6);
struct timeval tv = { seconds, microseconds };
const int milliseconds = (int) ((*timeout) * 1000);
const uint64_t base = _glfwPlatformGetTimerValue();
const int result = select(count, &fds, NULL, NULL, &tv);
const int error = errno;
const int result = poll(_glfw.x11.eventLoopData.fds, count, milliseconds);
*timeout -= (_glfwPlatformGetTimerValue() - base) /
(double) _glfwPlatformGetTimerFrequency();
if (result > 0)
{
if (_glfw.x11.eventLoopData.fds[0].revents && POLLIN) drainFd(_glfw.x11.eventLoopData.fds[0].fd);
return GLFW_TRUE;
if ((result == -1 && error == EINTR) || *timeout <= 0.0)
}
if (result == 0)
return GLFW_FALSE;
if (*timeout > 0 && (errno == EINTR || errno == EAGAIN)) continue;
return GLFW_FALSE;
}
else {
const int result = poll(_glfw.x11.eventLoopData.fds, count, -1);
if (result > 0)
{
if (_glfw.x11.eventLoopData.fds[0].revents && POLLIN) drainFd(_glfw.x11.eventLoopData.fds[0].fd);
return GLFW_TRUE;
}
if (result == 0)
return GLFW_FALSE;
if (errno != EINTR && errno != EAGAIN) return GLFW_FALSE;
}
else if (select(count, &fds, NULL, NULL, NULL) != -1 || errno != EINTR)
return GLFW_TRUE;
}
}
@ -2597,6 +2612,11 @@ void _glfwPlatformPostEmptyEvent(void)
XSendEvent(_glfw.x11.display, _glfw.x11.helperWindowHandle, False, 0, &event);
XFlush(_glfw.x11.display);
while(1)
{
if (write(_glfw.x11.eventLoopData.wakeupFds[1], "w", 1) >= 0 || errno != EINTR)
break;
}
}
void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos)