diff options
author | David Robillard <d@drobilla.net> | 2019-07-22 16:53:36 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2019-09-03 08:32:16 +0200 |
commit | 496f17c3804c79d304aa6095b92768593d1cc700 (patch) | |
tree | c8a9f9b831ae838f2005dd459abf3a65162fac4f /pugl/detail/x11.c | |
parent | dacaaa5f328ac2598123aa1f0744ddd68c87e9cc (diff) | |
download | pugl-496f17c3804c79d304aa6095b92768593d1cc700.tar.gz pugl-496f17c3804c79d304aa6095b92768593d1cc700.tar.bz2 pugl-496f17c3804c79d304aa6095b92768593d1cc700.zip |
Add puglPollEvents()
This allows waiting for events for any view in the world. It also improves on
puglWaitForEvent() by the addition of a time parameter that allows indefinite
blocking, non-blocking polling, and blocking polling with a timeout.
Diffstat (limited to 'pugl/detail/x11.c')
-rw-r--r-- | pugl/detail/x11.c | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/pugl/detail/x11.c b/pugl/detail/x11.c index d54f1bf..7ef3866 100644 --- a/pugl/detail/x11.c +++ b/pugl/detail/x11.c @@ -32,6 +32,7 @@ #include <X11/Xutil.h> #include <X11/keysym.h> +#include <sys/select.h> #include <sys/time.h> #include <stdbool.h> @@ -102,6 +103,30 @@ puglInitViewInternals(void) return (PuglInternals*)calloc(1, sizeof(PuglInternals)); } +PuglStatus +puglPollEvents(PuglWorld* world, const double timeout) +{ + XFlush(world->impl->display); + + const int fd = ConnectionNumber(world->impl->display); + const int nfds = fd + 1; + int ret = 0; + fd_set fds; + FD_ZERO(&fds); + FD_SET(fd, &fds); + + if (timeout < 0.0) { + ret = select(nfds, &fds, NULL, NULL, NULL); + } else { + const long sec = (long)timeout; + const long msec = (long)((timeout - (double)sec) * 1e6); + struct timeval tv = {sec, msec}; + ret = select(nfds, &fds, NULL, NULL, &tv); + } + + return ret < 0 ? PUGL_ERR_UNKNOWN : ret == 0 ? PUGL_FAILURE : PUGL_SUCCESS; +} + int puglCreateWindow(PuglView* view, const char* title) { @@ -193,6 +218,7 @@ void puglShowWindow(PuglView* view) { XMapRaised(view->impl->display, view->impl->win); + puglPostRedisplay(view); view->visible = true; } |