From b163cef6e77d0fd64af917e34971f01227ad3952 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kristian=20H=C3=B8gsberg?= Date: Fri, 19 Nov 2010 10:47:28 -0500 Subject: [PATCH] Make the server event loop embeddable By exposing the epoll fd, we can embed the server event loop in other main loops suchs as Qt or GTK+. --- wayland/event-loop.c | 45 ++++++++++++++++----------------------------- wayland/wayland-server.c | 2 +- wayland/wayland-server.h | 3 ++- 3 files changed, 19 insertions(+), 31 deletions(-) diff --git a/wayland/event-loop.c b/wayland/event-loop.c index 345660f..bf2a9aa 100644 --- a/wayland/event-loop.c +++ b/wayland/event-loop.c @@ -409,36 +409,13 @@ wl_event_loop_destroy(struct wl_event_loop *loop) free(loop); } -static void -dispatch_idles(struct wl_event_loop *loop) -{ - struct wl_event_source_idle *source, *next; - - source = container_of(loop->idle_list.next, - struct wl_event_source_idle, link); - - while (&source->link != &loop->idle_list) { - source->func(source->data); - next = container_of(source->link.next, - struct wl_event_source_idle, link); - free(source); - source = next; - } - - wl_list_init(&loop->idle_list); -} - WL_EXPORT int -wl_event_loop_wait(struct wl_event_loop *loop) +wl_event_loop_dispatch(struct wl_event_loop *loop, int timeout) { struct epoll_event ep[32]; struct wl_event_source *source; - int i, count, timeout; - - if (wl_list_empty(&loop->idle_list)) - timeout = -1; - else - timeout = 0; + struct wl_event_source_idle *idle; + int i, count; count = epoll_wait(loop->epoll_fd, ep, ARRAY_LENGTH(ep), timeout); if (count < 0) @@ -449,9 +426,19 @@ wl_event_loop_wait(struct wl_event_loop *loop) source->interface->dispatch(source, &ep[i]); } - if (count == 0) - dispatch_idles(loop); + while (!wl_list_empty(&loop->idle_list)) { + idle = container_of(loop->idle_list.next, + struct wl_event_source_idle, link); + wl_list_remove(&idle->link); + idle->func(idle->data); + free(idle); + } - return 0; } + +WL_EXPORT int +wl_event_loop_get_fd(struct wl_event_loop *loop) +{ + return loop->epoll_fd; +} diff --git a/wayland/wayland-server.c b/wayland/wayland-server.c index a64ceb8..3ddfc33 100644 --- a/wayland/wayland-server.c +++ b/wayland/wayland-server.c @@ -436,7 +436,7 @@ WL_EXPORT void wl_display_run(struct wl_display *display) { while (1) - wl_event_loop_wait(display->loop); + wl_event_loop_dispatch(display->loop, -1); } static void diff --git a/wayland/wayland-server.h b/wayland/wayland-server.h index 53c78ba..98a1a6c 100644 --- a/wayland/wayland-server.h +++ b/wayland/wayland-server.h @@ -70,10 +70,11 @@ int wl_event_source_timer_update(struct wl_event_source *source, int wl_event_source_remove(struct wl_event_source *source); -int wl_event_loop_wait(struct wl_event_loop *loop); +int wl_event_loop_dispatch(struct wl_event_loop *loop, int timeout); struct wl_event_source *wl_event_loop_add_idle(struct wl_event_loop *loop, wl_event_loop_idle_func_t func, void *data); +int wl_event_get_fd(struct wl_event_loop *loop); struct wl_client; struct wl_display; -- 2.7.4