From 44b84ba2954e20479091c2e01dd129f7ba3311ab Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 8 Jun 2018 14:27:30 +0530 Subject: [PATCH] Update glfw from upstream --- glfw/backend_utils.h | 66 +++++++++++++++ glfw/source-info.json | 188 +++++++++++++++++++++--------------------- glfw/wl_init.c | 18 +--- glfw/wl_window.c | 26 ++---- glfw/x11_init.c | 17 +--- glfw/x11_window.c | 20 +---- 6 files changed, 177 insertions(+), 158 deletions(-) create mode 100644 glfw/backend_utils.h diff --git a/glfw/backend_utils.h b/glfw/backend_utils.h new file mode 100644 index 000000000..d7b63d3aa --- /dev/null +++ b/glfw/backend_utils.h @@ -0,0 +1,66 @@ +//======================================================================== +// GLFW 3.3 Wayland - www.glfw.org +//------------------------------------------------------------------------ +// Copyright (c) 2014 Jonas Ã…dahl +// +// 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. +// +//======================================================================== + +#pragma once +#include +#include +#include + +#ifdef __NetBSD__ +#define ppoll pollts +#endif + +static inline void +drainFd(int fd) { + static char drain_buf[64]; + while(read(fd, drain_buf, sizeof(drain_buf)) < 0 && errno == EINTR); +} + + +static inline int +pollWithTimeout(struct pollfd *fds, nfds_t nfds, double timeout) { + const long seconds = (long) timeout; + const long nanoseconds = (long) ((timeout - seconds) * 1e9); + struct timespec tv = { seconds, nanoseconds }; + return ppoll(fds, nfds, &tv, NULL); +} + +static inline void +initPollData(struct pollfd *fds, int wakeup_fd, int display_fd) { + fds[0].fd = wakeup_fd; fds[1].fd = display_fd; + fds[0].events = POLLIN; fds[1].events = POLLIN; +} + +static inline void +closeFds(int *fds, size_t count) { + while(count--) { + if (*fds > 0) { + close(*fds); + *fds = -1; + } + fds++; + } +} diff --git a/glfw/source-info.json b/glfw/source-info.json index 7a3e5800d..61b095144 100644 --- a/glfw/source-info.json +++ b/glfw/source-info.json @@ -1,135 +1,137 @@ { "cocoa": { "headers": [ - "cocoa_platform.h", - "cocoa_joystick.h", - "posix_thread.h", - "nsgl_context.h", - "egl_context.h", + "cocoa_platform.h", + "cocoa_joystick.h", + "posix_thread.h", + "nsgl_context.h", + "egl_context.h", "osmesa_context.h" - ], + ], "sources": [ - "cocoa_init.m", - "cocoa_joystick.m", - "cocoa_monitor.m", - "cocoa_window.m", - "cocoa_time.c", - "posix_thread.c", - "nsgl_context.m", - "egl_context.c", + "cocoa_init.m", + "cocoa_joystick.m", + "cocoa_monitor.m", + "cocoa_window.m", + "cocoa_time.c", + "posix_thread.c", + "nsgl_context.m", + "egl_context.c", "osmesa_context.c" ] - }, + }, "common": { "headers": [ - "internal.h", + "internal.h", "mappings.h" - ], + ], "sources": [ - "context.c", - "init.c", - "input.c", - "monitor.c", - "vulkan.c", + "context.c", + "init.c", + "input.c", + "monitor.c", + "vulkan.c", "window.c" ] - }, + }, "osmesa": { "headers": [ - "null_platform.h", - "null_joystick.h", - "posix_time.h", - "posix_thread.h", + "null_platform.h", + "null_joystick.h", + "posix_time.h", + "posix_thread.h", "osmesa_context.h" - ], + ], "sources": [ - "null_init.c", - "null_monitor.c", - "null_window.c", - "null_joystick.c", - "posix_time.c", - "posix_thread.c", + "null_init.c", + "null_monitor.c", + "null_window.c", + "null_joystick.c", + "posix_time.c", + "posix_thread.c", "osmesa_context.c" ] - }, + }, "wayland": { "headers": [ - "wl_platform.h", - "posix_time.h", - "posix_thread.h", - "xkb_glfw.h", - "egl_context.h", - "osmesa_context.h", - "linux_joystick.h", + "wl_platform.h", + "posix_time.h", + "posix_thread.h", + "xkb_glfw.h", + "backend_utils.h", + "egl_context.h", + "osmesa_context.h", + "linux_joystick.h", "null_joystick.h" - ], + ], "protocols": [ - "stable/xdg-shell/xdg-shell.xml", - "stable/viewporter/viewporter.xml", - "unstable/relative-pointer/relative-pointer-unstable-v1.xml", - "unstable/pointer-constraints/pointer-constraints-unstable-v1.xml", + "stable/xdg-shell/xdg-shell.xml", + "stable/viewporter/viewporter.xml", + "unstable/relative-pointer/relative-pointer-unstable-v1.xml", + "unstable/pointer-constraints/pointer-constraints-unstable-v1.xml", "unstable/idle-inhibit/idle-inhibit-unstable-v1.xml" - ], + ], "sources": [ - "wl_init.c", - "wl_monitor.c", - "wl_window.c", - "posix_time.c", - "posix_thread.c", - "xkb_glfw.c", - "egl_context.c", - "osmesa_context.c", - "linux_joystick.c", + "wl_init.c", + "wl_monitor.c", + "wl_window.c", + "posix_time.c", + "posix_thread.c", + "xkb_glfw.c", + "egl_context.c", + "osmesa_context.c", + "linux_joystick.c", "null_joystick.c" ] - }, + }, "wayland_protocols": [ - 1, + 1, 12 - ], + ], "win32": { "headers": [ - "win32_platform.h", - "win32_joystick.h", - "wgl_context.h", - "egl_context.h", + "win32_platform.h", + "win32_joystick.h", + "wgl_context.h", + "egl_context.h", "osmesa_context.h" - ], + ], "sources": [ - "win32_init.c", - "win32_joystick.c", - "win32_monitor.c", - "win32_time.c", - "win32_thread.c", - "win32_window.c", - "wgl_context.c", - "egl_context.c", + "win32_init.c", + "win32_joystick.c", + "win32_monitor.c", + "win32_time.c", + "win32_thread.c", + "win32_window.c", + "wgl_context.c", + "egl_context.c", "osmesa_context.c" ] - }, + }, "x11": { "headers": [ - "x11_platform.h", - "xkb_glfw.h", - "posix_time.h", - "posix_thread.h", - "glx_context.h", - "egl_context.h", - "osmesa_context.h", - "linux_joystick.h", + "x11_platform.h", + "xkb_glfw.h", + "backend_utils.h", + "posix_time.h", + "posix_thread.h", + "glx_context.h", + "egl_context.h", + "osmesa_context.h", + "linux_joystick.h", "null_joystick.h" - ], + ], "sources": [ - "x11_init.c", - "x11_monitor.c", - "x11_window.c", - "xkb_glfw.c", - "posix_time.c", - "posix_thread.c", - "glx_context.c", - "egl_context.c", - "osmesa_context.c", - "linux_joystick.c", + "x11_init.c", + "x11_monitor.c", + "x11_window.c", + "xkb_glfw.c", + "posix_time.c", + "posix_thread.c", + "glx_context.c", + "egl_context.c", + "osmesa_context.c", + "linux_joystick.c", "null_joystick.c" ] } diff --git a/glfw/wl_init.c b/glfw/wl_init.c index 80a3271fa..70f9eed7a 100644 --- a/glfw/wl_init.c +++ b/glfw/wl_init.c @@ -26,6 +26,7 @@ #define _GNU_SOURCE #include "internal.h" +#include "backend_utils.h" #include #include @@ -663,10 +664,7 @@ int _glfwPlatformInit(void) "Wayland: Failed to connect to display"); return GLFW_FALSE; } - _glfw.wl.eventLoopData.fds[0].fd = _glfw.wl.eventLoopData.wakeupFds[0]; - _glfw.wl.eventLoopData.fds[1].fd = wl_display_get_fd(_glfw.wl.display); - _glfw.wl.eventLoopData.fds[0].events = POLLIN; - _glfw.wl.eventLoopData.fds[1].events = POLLIN; + initPollData(_glfw.wl.eventLoopData.fds, _glfw.wl.eventLoopData.wakeupFds[0], wl_display_get_fd(_glfw.wl.display)); _glfw.wl.registry = wl_display_get_registry(_glfw.wl.display); wl_registry_add_listener(_glfw.wl.registry, ®istryListener, NULL); @@ -704,17 +702,6 @@ int _glfwPlatformInit(void) void _glfwPlatformTerminate(void) { - if (_glfw.wl.eventLoopData.wakeupFds[0] > 0) - { - close(_glfw.wl.eventLoopData.wakeupFds[0]); - _glfw.wl.eventLoopData.wakeupFds[0] = -1; - } - if (_glfw.wl.eventLoopData.wakeupFds[1] > 0) - { - close(_glfw.wl.eventLoopData.wakeupFds[1]); - _glfw.wl.eventLoopData.wakeupFds[1] = -1; - } - #ifdef __linux__ _glfwTerminateJoysticksLinux(); #endif @@ -768,6 +755,7 @@ void _glfwPlatformTerminate(void) wl_display_flush(_glfw.wl.display); wl_display_disconnect(_glfw.wl.display); } + closeFds(_glfw.wl.eventLoopData.wakeupFds, sizeof(_glfw.wl.eventLoopData.wakeupFds)/sizeof(_glfw.wl.eventLoopData.wakeupFds[0])); } const char* _glfwPlatformGetVersionString(void) diff --git a/glfw/wl_window.c b/glfw/wl_window.c index 082cd7c53..ae5431baf 100644 --- a/glfw/wl_window.c +++ b/glfw/wl_window.c @@ -27,15 +27,14 @@ #define _GNU_SOURCE #include "internal.h" +#include "backend_utils.h" #include #include #include -#include #include #include #include -#include static void handlePing(void* data, @@ -710,16 +709,6 @@ adjustTimeoutForKeyRepeat(double timeout) { return timeout; } -#ifdef __NetBSD__ -#define ppoll pollts -#endif - -static inline void -drainFd(int fd) { - static char drain_buf[64]; - while(read(fd, drain_buf, sizeof(drain_buf)) < 0 && errno == EINTR); -} - static void handleEvents(double timeout) { @@ -748,19 +737,16 @@ handleEvents(double timeout) GLFWbool read_ok = GLFW_FALSE; if (timeout >= 0) { - const long seconds = (long) timeout; - const long nanoseconds = (long) ((timeout - seconds) * 1e9); - struct timespec tv = { seconds, nanoseconds }; - const int result = ppoll(_glfw.wl.eventLoopData.fds, 2, &tv, NULL); + const int result = pollWithTimeout(_glfw.wl.eventLoopData.fds, 2, timeout); if (result > 0) { - if (_glfw.wl.eventLoopData.fds[0].revents && POLLIN) drainFd(_glfw.wl.eventLoopData.fds[0].fd); - read_ok = _glfw.wl.eventLoopData.fds[1].revents && POLLIN; + if (_glfw.wl.eventLoopData.fds[0].revents & POLLIN) drainFd(_glfw.wl.eventLoopData.fds[0].fd); + read_ok = _glfw.wl.eventLoopData.fds[1].revents & POLLIN; } } else { if (poll(_glfw.wl.eventLoopData.fds, 2, -1) > 0) { - if (_glfw.wl.eventLoopData.fds[0].revents && POLLIN) drainFd(_glfw.wl.eventLoopData.fds[0].fd); - read_ok = _glfw.wl.eventLoopData.fds[1].revents && POLLIN; + if (_glfw.wl.eventLoopData.fds[0].revents & POLLIN) drainFd(_glfw.wl.eventLoopData.fds[0].fd); + read_ok = _glfw.wl.eventLoopData.fds[1].revents & POLLIN; } } if (read_ok) { diff --git a/glfw/x11_init.c b/glfw/x11_init.c index 8b7e04799..cf9bc5d1f 100644 --- a/glfw/x11_init.c +++ b/glfw/x11_init.c @@ -27,6 +27,7 @@ #define _GNU_SOURCE #include "internal.h" +#include "backend_utils.h" #include @@ -625,10 +626,7 @@ 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; + initPollData(_glfw.x11.eventLoopData.fds, _glfw.x11.eventLoopData.wakeupFds[0], ConnectionNumber(_glfw.x11.display)); _glfw.x11.eventLoopData.fds[2].events = POLLIN; _glfw.x11.screen = DefaultScreen(_glfw.x11.display); @@ -682,16 +680,6 @@ 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) - { - close(_glfw.x11.eventLoopData.wakeupFds[1]); - _glfw.x11.eventLoopData.wakeupFds[1] = -1; - } _glfw.x11.eventLoopData.fds[0].fd = -1; } @@ -739,6 +727,7 @@ void _glfwPlatformTerminate(void) #if defined(__linux__) _glfwTerminateJoysticksLinux(); #endif + closeFds(_glfw.x11.eventLoopData.wakeupFds, sizeof(_glfw.x11.eventLoopData.wakeupFds)/sizeof(_glfw.x11.eventLoopData.wakeupFds[0])); } const char* _glfwPlatformGetVersionString(void) diff --git a/glfw/x11_window.c b/glfw/x11_window.c index be99cbafb..f2c64e475 100644 --- a/glfw/x11_window.c +++ b/glfw/x11_window.c @@ -27,6 +27,7 @@ #define _GNU_SOURCE #include "internal.h" +#include "backend_utils.h" #include #include @@ -49,16 +50,6 @@ #define _GLFW_XDND_VERSION 5 -static inline void -drainFd(int fd) { - static char drain_buf[64]; - while(read(fd, drain_buf, sizeof(drain_buf)) < 0 && errno == EINTR); -} - -#ifdef __NetBSD__ -#define ppoll pollts -#endif - // Wait for data to arrive using poll // This avoids blocking other threads via the per-display Xlib lock that also // covers GLX functions @@ -79,17 +70,14 @@ static GLFWbool waitForEvent(double* timeout) for (nfds_t i = 0; i < count; i++) _glfw.x11.eventLoopData.fds[i].revents = 0; if (timeout) { - const long seconds = (long) *timeout; - const long nanoseconds = (long) ((*timeout - seconds) * 1e9); - struct timespec tv = { seconds, nanoseconds }; const uint64_t base = _glfwPlatformGetTimerValue(); - const int result = ppoll(_glfw.x11.eventLoopData.fds, count, &tv, NULL); + const int result = pollWithTimeout(_glfw.x11.eventLoopData.fds, count, *timeout); *timeout -= (_glfwPlatformGetTimerValue() - base) / (double) _glfwPlatformGetTimerFrequency(); if (result > 0) { - if (_glfw.x11.eventLoopData.fds[0].revents && POLLIN) drainFd(_glfw.x11.eventLoopData.fds[0].fd); + if (_glfw.x11.eventLoopData.fds[0].revents & POLLIN) drainFd(_glfw.x11.eventLoopData.fds[0].fd); return GLFW_TRUE; } if (result == 0) @@ -101,7 +89,7 @@ static GLFWbool waitForEvent(double* timeout) 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); + if (_glfw.x11.eventLoopData.fds[0].revents & POLLIN) drainFd(_glfw.x11.eventLoopData.fds[0].fd); return GLFW_TRUE; } if (result == 0)