Add glfwWaitEventsTimeout

This function will put the calling thread to sleep until an event
arrives or until the specified timeout has elapsed.
This commit is contained in:
Camilla Berglund
2016-02-02 21:11:16 +01:00
parent 337c77a39b
commit 5620895e88
11 changed files with 150 additions and 0 deletions
+13
View File
@@ -1214,6 +1214,19 @@ void _glfwPlatformWaitEvents(void)
_glfwPlatformPollEvents();
}
void _glfwPlatformWaitEventsTimeout(double timeout)
{
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:timeout];
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:date
inMode:NSDefaultRunLoopMode
dequeue:YES];
if (event)
[NSApp sendEvent:event];
_glfwPlatformPollEvents();
}
void _glfwPlatformPostEmptyEvent(void)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+5
View File
@@ -717,6 +717,11 @@ void _glfwPlatformPollEvents(void);
*/
void _glfwPlatformWaitEvents(void);
/*! @copydoc glfwWaitEventsTimeout
* @ingroup platform
*/
void _glfwPlatformWaitEventsTimeout(double timeout);
/*! @copydoc glfwPostEmptyEvent
* @ingroup platform
*/
+18
View File
@@ -550,6 +550,24 @@ void _glfwPlatformWaitEvents(void)
_glfwPlatformPollEvents();
}
void _glfwPlatformWaitEventsTimeout(double timeout)
{
pthread_mutex_lock(&_glfw.mir.event_mutex);
if (emptyEventQueue(_glfw.mir.event_queue))
{
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
time.tv_sec += (long) timeout;
time.tv_nsec += (long) ((timeout - (long) timeout) * 1e9);
pthread_cond_timedwait(&_glfw.mir.event_cond, &_glfw.mir.event_mutex, &time);
}
pthread_mutex_unlock(&_glfw.mir.event_mutex);
_glfwPlatformPollEvents();
}
void _glfwPlatformPostEmptyEvent(void)
{
}
+7
View File
@@ -1142,6 +1142,13 @@ void _glfwPlatformWaitEvents(void)
_glfwPlatformPollEvents();
}
void _glfwPlatformWaitEventsTimeout(double timeout)
{
MsgWaitForMultipleObjects(0, NULL, FALSE, (DWORD) (timeout * 1e3), QS_ALLEVENTS);
_glfwPlatformPollEvents();
}
void _glfwPlatformPostEmptyEvent(void)
{
_GLFWwindow* window = _glfw.windowListHead;
+14
View File
@@ -31,6 +31,7 @@
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <float.h>
//////////////////////////////////////////////////////////////////////////
@@ -807,6 +808,19 @@ GLFWAPI void glfwWaitEvents(void)
_glfwPlatformWaitEvents();
}
GLFWAPI void glfwWaitEventsTimeout(double timeout)
{
_GLFW_REQUIRE_INIT();
if (timeout != timeout || timeout < 0.0 || timeout > DBL_MAX)
{
_glfwInputError(GLFW_INVALID_VALUE, "Invalid time");
return;
}
_glfwPlatformWaitEventsTimeout(timeout);
}
GLFWAPI void glfwPostEmptyEvent(void)
{
_GLFW_REQUIRE_INIT();
+5
View File
@@ -516,6 +516,11 @@ void _glfwPlatformWaitEvents(void)
handleEvents(-1);
}
void _glfwPlatformWaitEventsTimeout(double timeout)
{
handleEvents((int) (timeout * 1e3));
}
void _glfwPlatformPostEmptyEvent(void)
{
wl_display_sync(_glfw.wl.display);
+22
View File
@@ -1912,6 +1912,28 @@ void _glfwPlatformWaitEvents(void)
_glfwPlatformPollEvents();
}
void _glfwPlatformWaitEventsTimeout(double timeout)
{
const double deadline = timeout + _glfwPlatformGetTimerValue() /
(double) _glfwPlatformGetTimerFrequency();
while (!XPending(_glfw.x11.display))
{
const double remaining = deadline - _glfwPlatformGetTimerValue() /
(double) _glfwPlatformGetTimerFrequency();
if (remaining <= 0.0)
return;
const long seconds = (long) remaining;
const long microseconds = (long) ((remaining - seconds) * 1e6);
struct timeval tv = { seconds, microseconds };
selectDisplayConnection(&tv);
}
_glfwPlatformPollEvents();
}
void _glfwPlatformPostEmptyEvent(void)
{
XEvent event;