From 899df71f847b9070d170881bfc02dbf56d5b70e5 Mon Sep 17 00:00:00 2001 From: Emre Ucan Date: Thu, 2 Jul 2015 13:17:22 +0200 Subject: EGLWLInputEventExample: modify WaitforEvent to read the socket it is not enough to dispatch the pending events in the mainloop. Event should be also read from the socket with wl_display_read_events call. This Implementation also avoids the busy loop in main.c, because WaitforEvent function waits at poll() till an event comes to the socket. Signed-off-by: Emre Ucan --- .../include/WLEyesRenderer.h | 2 +- .../EGLWLInputEventExample/src/WLEyesRenderer.cpp | 35 ++++++++++++++++++++-- .../EGLWLInputEventExample/src/main.cpp | 5 ++-- 3 files changed, 37 insertions(+), 5 deletions(-) (limited to 'ivi-layermanagement-examples') diff --git a/ivi-layermanagement-examples/EGLWLInputEventExample/include/WLEyesRenderer.h b/ivi-layermanagement-examples/EGLWLInputEventExample/include/WLEyesRenderer.h index e630ecc..ea76110 100644 --- a/ivi-layermanagement-examples/EGLWLInputEventExample/include/WLEyesRenderer.h +++ b/ivi-layermanagement-examples/EGLWLInputEventExample/include/WLEyesRenderer.h @@ -27,7 +27,7 @@ bool InitRenderer(); bool InitShader(); bool InitVertexBuffer(); -void WaitForEvent(struct wl_display* wlDisplay); +void WaitForEvent(struct wl_display* wlDisplay, int fd); void TerminateRenderer(); // Pointer event handler diff --git a/ivi-layermanagement-examples/EGLWLInputEventExample/src/WLEyesRenderer.cpp b/ivi-layermanagement-examples/EGLWLInputEventExample/src/WLEyesRenderer.cpp index e21f347..a4f6c11 100644 --- a/ivi-layermanagement-examples/EGLWLInputEventExample/src/WLEyesRenderer.cpp +++ b/ivi-layermanagement-examples/EGLWLInputEventExample/src/WLEyesRenderer.cpp @@ -24,6 +24,7 @@ #include "WLEGLSurface.h" #include "WLEyes.h" #include "WLEyesRenderer.h" +#include #define WL_UNUSED(A) (A)=(A) @@ -55,9 +56,39 @@ const struct wl_touch_listener TouchListener = { TouchHandleCancel, }; -void WaitForEvent(struct wl_display* wlDisplay) +void WaitForEvent(struct wl_display* wlDisplay, int fd) { - wl_display_dispatch_pending(wlDisplay); + int err; + + /* Execute every pending event in the display and stop any other thread from placing + * events into this display */ + while (wl_display_prepare_read(wlDisplay) != 0) { + wl_display_dispatch_pending(wlDisplay); + } + + if (wl_display_flush(wlDisplay) == -1) + { + err = wl_display_get_error(wlDisplay); + printf("Error communicating with wayland: %d",err); + return; + } + + /*Wait till an event occurs */ + struct pollfd pfd[1]; + pfd[0].fd = fd; + pfd[0].events = POLLIN; + int pollret = poll(pfd, 1, -1); + + if (pollret != -1 && (pfd[0].revents & POLLIN)) + { + /* Read the upcoming events from the file descriptor and execute them */ + wl_display_read_events(wlDisplay); + wl_display_dispatch_pending(wlDisplay); + } + else { + /*Unblock other threads, if an error happens */ + wl_display_cancel_read(wlDisplay); + } } ////////////////////////////////////////////////////////////////////////////// diff --git a/ivi-layermanagement-examples/EGLWLInputEventExample/src/main.cpp b/ivi-layermanagement-examples/EGLWLInputEventExample/src/main.cpp index f1b29a1..20c611f 100644 --- a/ivi-layermanagement-examples/EGLWLInputEventExample/src/main.cpp +++ b/ivi-layermanagement-examples/EGLWLInputEventExample/src/main.cpp @@ -79,6 +79,8 @@ int main(int argc, char **argv) wlContext = new WLContext(); wlContext->InitWLContext(&PointerListener, &KeyboardListener, &TouchListener); + int const fd = wl_display_get_fd(wlContext->GetWLDisplay()); + ilmClient_init((t_ilm_nativedisplay)wlContext->GetWLDisplay()); eglSurface = new WLEGLSurface(wlContext); @@ -100,12 +102,11 @@ int main(int argc, char **argv) gRunLoop = 1; gNeedRedraw = 0; while (gRunLoop){ - WaitForEvent(wlContext->GetWLDisplay()); + WaitForEvent(wlContext->GetWLDisplay(), fd); if (gNeedRedraw && gRunLoop){ DrawEyes(eglSurface, eyes); gNeedRedraw = 0; } - usleep(50); } TerminateRenderer(); -- cgit v1.2.1