From 5ebb317383b9863ca7da037a7348ab3ee4f9f34c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristian=20H=C3=B8gsberg?= Date: Sat, 11 Oct 2008 19:21:35 -0400 Subject: [PATCH] Recomposite screen in idle handler. --- client.c | 6 +++--- egl-compositor.c | 8 ++++++-- event-loop.c | 46 ++++++++++++++++++++++++++++++++++++++-------- wayland.c | 6 ++++++ wayland.h | 10 ++++++++-- 5 files changed, 61 insertions(+), 15 deletions(-) diff --git a/client.c b/client.c index 2fb1484..87df3b7 100644 --- a/client.c +++ b/client.c @@ -141,11 +141,11 @@ int main(int argc, char *argv[]) cairo_image_surface_get_stride(s)); i = 0; - while (ret = poll(p, 1, 200), ret >= 0) { + while (ret = poll(p, 1, 20), ret >= 0) { if (ret == 0) { wl_surface_map(surface, - x + cos(i / 10.0) * 150, - y + sin(i / 10.0) * 150, + x + cos(i / 10.0) * 200, + y + sin(i / 11.0) * 200, width, height); i++; continue; diff --git a/egl-compositor.c b/egl-compositor.c index fd39216..6d8404e 100644 --- a/egl-compositor.c +++ b/egl-compositor.c @@ -137,8 +137,9 @@ void notify_surface_attach(struct wl_compositor *compositor, } static void -repaint(struct egl_compositor *ec) +repaint(void *data) { + struct egl_compositor *ec = data; struct wl_surface_iterator *iterator; struct wl_surface *surface; struct surface_data *sd; @@ -197,13 +198,16 @@ void notify_surface_map(struct wl_compositor *compositor, { struct egl_compositor *ec = (struct egl_compositor *) compositor; struct surface_data *sd; + struct wl_event_loop *loop; sd = wl_surface_get_data(surface); if (sd == NULL) return; sd->map = *map; - repaint(ec); + + loop = wl_display_get_event_loop(ec->wl_display); + wl_event_loop_add_idle(loop, repaint, ec); } struct wl_compositor_interface interface = { diff --git a/event-loop.c b/event-loop.c index 96b5680..f431161 100644 --- a/event-loop.c +++ b/event-loop.c @@ -11,18 +11,20 @@ struct wl_event_loop { int epoll_fd; + wl_event_loop_idle_func_t idle_func; + void *idle_data; }; struct wl_event_source { int fd; - wl_event_loop_func_t func; + wl_event_loop_fd_func_t func; void *data; }; struct wl_event_source * wl_event_loop_add_fd(struct wl_event_loop *loop, int fd, uint32_t mask, - wl_event_loop_func_t func, + wl_event_loop_fd_func_t func, void *data) { struct wl_event_source *source; @@ -48,16 +50,23 @@ wl_event_loop_add_fd(struct wl_event_loop *loop, return source; } +struct wl_event_source idle_source; + int wl_event_loop_remove_source(struct wl_event_loop *loop, struct wl_event_source *source) { int fd; - fd = source->fd; - free(source); - - return epoll_ctl(loop->epoll_fd, EPOLL_CTL_DEL, fd, NULL); + if (source == &idle_source) { + loop->idle_func = NULL; + return 0; + } else { + fd = source->fd; + free(source); + return epoll_ctl(loop->epoll_fd, + EPOLL_CTL_DEL, fd, NULL); + }; } int @@ -103,6 +112,17 @@ wl_event_loop_destroy(struct wl_event_loop *loop) free(loop); } +struct wl_event_source * +wl_event_loop_add_idle(struct wl_event_loop *loop, + wl_event_loop_idle_func_t func, + void *data) +{ + loop->idle_func = func; + loop->idle_data = data; + + return &idle_source; +} + #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) int @@ -110,10 +130,15 @@ wl_event_loop_wait(struct wl_event_loop *loop) { struct epoll_event ep[32]; struct wl_event_source *source; - int i, count; + int i, count, timeout; uint32_t mask; - count = epoll_wait(loop->epoll_fd, ep, ARRAY_LENGTH(ep), -1); + if (loop->idle_func) + timeout = 0; + else + timeout = -1; + + count = epoll_wait(loop->epoll_fd, ep, ARRAY_LENGTH(ep), timeout); if (count < 0) return -1; @@ -128,5 +153,10 @@ wl_event_loop_wait(struct wl_event_loop *loop) source->func(source->fd, mask, source->data); } + if (count == 0 && loop->idle_func != NULL) { + loop->idle_func(loop->idle_data); + loop->idle_func = NULL; + } + return 0; } diff --git a/wayland.c b/wayland.c index 8604bba..20fbcb8 100644 --- a/wayland.c +++ b/wayland.c @@ -469,6 +469,12 @@ wl_display_set_compositor(struct wl_display *display, display->compositor = compositor; } +struct wl_event_loop * +wl_display_get_event_loop(struct wl_display *display) +{ + return display->loop; +} + void wl_display_run(struct wl_display *display) { diff --git a/wayland.h b/wayland.h index 2865cfe..471ec7a 100644 --- a/wayland.h +++ b/wayland.h @@ -10,13 +10,14 @@ enum { struct wl_event_loop; struct wl_event_source; -typedef void (*wl_event_loop_func_t)(int fd, uint32_t mask, void *data); +typedef void (*wl_event_loop_fd_func_t)(int fd, uint32_t mask, void *data); +typedef void (*wl_event_loop_idle_func_t)(void *data); struct wl_event_loop *wl_event_loop_create(void); void wl_event_loop_destroy(struct wl_event_loop *loop); struct wl_event_source *wl_event_loop_add_fd(struct wl_event_loop *loop, int fd, uint32_t mask, - wl_event_loop_func_t func, + wl_event_loop_fd_func_t func, void *data); int wl_event_loop_update_source(struct wl_event_loop *loop, struct wl_event_source *source, @@ -25,6 +26,9 @@ int wl_event_loop_update_source(struct wl_event_loop *loop, int wl_event_loop_remove_source(struct wl_event_loop *loop, struct wl_event_source *source); int wl_event_loop_wait(struct wl_event_loop *loop); +struct wl_event_source *wl_event_loop_add_idle(struct wl_event_loop *loop, + wl_event_loop_idle_func_t func, + void *data); struct wl_hash { struct wl_object **objects; @@ -81,6 +85,8 @@ struct wl_map { int32_t x, y, width, height; }; +struct wl_event_loop *wl_display_get_event_loop(struct wl_display *display); + void wl_surface_set_data(struct wl_surface *surface, void *data); void *wl_surface_get_data(struct wl_surface *surface);