From 496f17c3804c79d304aa6095b92768593d1cc700 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 22 Jul 2019 16:53:36 +0200 Subject: 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. --- pugl/detail/x11.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'pugl/detail/x11.c') 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 #include +#include #include #include @@ -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; } -- cgit v1.2.1