aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2015-09-12 19:45:02 -0400
committerDavid Robillard <d@drobilla.net>2015-09-12 19:45:02 -0400
commit809ead2220f5a9ed66f88b1ae84a93e334e2717f (patch)
tree64ef3ffc307e8d6b8847e2daf878e6e2997bb87d
parent6b4a5a128ef5d87374dfef017cd20e069c068a4b (diff)
downloadpugl-809ead2220f5a9ed66f88b1ae84a93e334e2717f.tar.gz
pugl-809ead2220f5a9ed66f88b1ae84a93e334e2717f.tar.bz2
pugl-809ead2220f5a9ed66f88b1ae84a93e334e2717f.zip
Add puglWaitForEvent for blocking main loops.
-rw-r--r--pugl/pugl.h14
-rw-r--r--pugl/pugl_osx.m18
-rw-r--r--pugl/pugl_win.cpp7
-rw-r--r--pugl/pugl_x11.c9
-rw-r--r--pugl_cairo_test.c1
-rw-r--r--pugl_test.c1
6 files changed, 47 insertions, 3 deletions
diff --git a/pugl/pugl.h b/pugl/pugl.h
index 3ff66f9..8dca956 100644
--- a/pugl/pugl.h
+++ b/pugl/pugl.h
@@ -362,10 +362,22 @@ PUGL_API void
puglGrabFocus(PuglView* view);
/**
+ Block and wait for an event to be ready.
+
+ This can be used in a loop to only process events via puglProcessEvents when
+ necessary. This function will block indefinitely if no events are
+ available, so is not appropriate for use in programs that need to perform
+ regular updates (e.g. animation).
+*/
+PUGL_API PuglStatus
+puglWaitForEvent(PuglView* view);
+
+/**
Process all pending window events.
This handles input events as well as rendering, so it should be called
- regularly and rapidly enough to keep the UI responsive.
+ regularly and rapidly enough to keep the UI responsive. This function does
+ not block if no events are pending.
*/
PUGL_API PuglStatus
puglProcessEvents(PuglView* view);
diff --git a/pugl/pugl_osx.m b/pugl/pugl_osx.m
index 85e71a8..47c8138 100644
--- a/pugl/pugl_osx.m
+++ b/pugl/pugl_osx.m
@@ -525,10 +525,24 @@ puglGrabFocus(PuglView* view)
}
PuglStatus
+puglWaitForEvent(PuglView* view)
+{
+ [view->impl->window nextEventMatchingMask: NSAnyEventMask
+ untilDate: [NSDate distantFuture]
+ dequeue: NO];
+
+ return PUGL_SUCCESS;
+}
+
+PuglStatus
puglProcessEvents(PuglView* view)
{
- NSEvent* ev = [view->impl->window nextEventMatchingMask: NSAnyEventMask];
- [view->impl->app sendEvent: ev];
+ NSEvent* ev = [view->impl->window nextEventMatchingMask: NSAnyEventMask
+ untilDate: [NSDate distantPast]];
+
+ if (ev) {
+ [view->impl->app sendEvent: ev];
+ }
return PUGL_SUCCESS;
}
diff --git a/pugl/pugl_win.cpp b/pugl/pugl_win.cpp
index 04bc4b6..850947d 100644
--- a/pugl/pugl_win.cpp
+++ b/pugl/pugl_win.cpp
@@ -425,6 +425,13 @@ puglGrabFocus(PuglView* view)
}
PuglStatus
+puglWaitForEvent(PuglView* view)
+{
+ WaitMessage();
+ return PUGL_SUCCESS;
+}
+
+PuglStatus
puglProcessEvents(PuglView* view)
{
MSG msg;
diff --git a/pugl/pugl_x11.c b/pugl/pugl_x11.c
index 6375609..32501da 100644
--- a/pugl/pugl_x11.c
+++ b/pugl/pugl_x11.c
@@ -494,6 +494,14 @@ puglGrabFocus(PuglView* view)
}
PuglStatus
+puglWaitForEvent(PuglView* view)
+{
+ XEvent xevent;
+ XPeekEvent(view->impl->display, &xevent);
+ return PUGL_SUCCESS;
+}
+
+PuglStatus
puglProcessEvents(PuglView* view)
{
XEvent xevent;
@@ -544,6 +552,7 @@ puglProcessEvents(PuglView* view)
if (view->ctx_type == PUGL_CAIRO) {
cairo_xlib_surface_set_size(
view->impl->surface, view->width, view->height);
+ view->redisplay = true;
}
#endif
}
diff --git a/pugl_cairo_test.c b/pugl_cairo_test.c
index bd97668..9c1bef8 100644
--- a/pugl_cairo_test.c
+++ b/pugl_cairo_test.c
@@ -166,6 +166,7 @@ main(int argc, char** argv)
puglShowWindow(view);
while (!quit) {
+ puglWaitForEvent(view);
puglProcessEvents(view);
}
diff --git a/pugl_test.c b/pugl_test.c
index 248a49a..af51e53 100644
--- a/pugl_test.c
+++ b/pugl_test.c
@@ -191,6 +191,7 @@ main(int argc, char** argv)
puglShowWindow(view);
while (!quit) {
+ puglWaitForEvent(view);
puglProcessEvents(view);
}