Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <glib/giochannel.h> |
| 3 | #include "wayland-client.h" |
| 4 | #include "wayland-glib.h" |
| 5 | |
| 6 | struct _WaylandSource { |
| 7 | GSource source; |
| 8 | GPollFD pfd; |
| 9 | uint32_t mask; |
| 10 | struct wl_display *display; |
| 11 | }; |
| 12 | |
| 13 | static gboolean |
| 14 | wayland_source_prepare(GSource *base, gint *timeout) |
| 15 | { |
| 16 | WaylandSource *source = (WaylandSource *) base; |
| 17 | |
| 18 | *timeout = -1; |
| 19 | |
| 20 | /* We have to add/remove the GPollFD if we want to update our |
| 21 | * poll event mask dynamically. Instead, let's just flush all |
| 22 | * write on idle instead, which is what this amounts to. */ |
| 23 | |
| 24 | while (source->mask & WL_DISPLAY_WRITABLE) |
| 25 | wl_display_iterate(source->display, |
| 26 | WL_DISPLAY_WRITABLE); |
| 27 | |
| 28 | return FALSE; |
| 29 | } |
| 30 | |
| 31 | static gboolean |
| 32 | wayland_source_check(GSource *base) |
| 33 | { |
| 34 | WaylandSource *source = (WaylandSource *) base; |
| 35 | |
| 36 | return source->pfd.revents; |
| 37 | } |
| 38 | |
| 39 | static gboolean |
| 40 | wayland_source_dispatch(GSource *base, |
| 41 | GSourceFunc callback, |
| 42 | gpointer data) |
| 43 | { |
| 44 | WaylandSource *source = (WaylandSource *) base; |
| 45 | |
| 46 | wl_display_iterate(source->display, |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame^] | 47 | WL_DISPLAY_READABLE); |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 48 | |
| 49 | return TRUE; |
| 50 | } |
| 51 | |
| 52 | static GSourceFuncs wayland_source_funcs = { |
| 53 | wayland_source_prepare, |
| 54 | wayland_source_check, |
| 55 | wayland_source_dispatch, |
| 56 | NULL |
| 57 | }; |
| 58 | |
| 59 | static int |
| 60 | wayland_source_update(uint32_t mask, void *data) |
| 61 | { |
| 62 | WaylandSource *source = data; |
| 63 | |
| 64 | source->mask = mask; |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | GSource * |
| 70 | wayland_source_new(struct wl_display *display) |
| 71 | { |
| 72 | WaylandSource *source; |
| 73 | |
| 74 | source = (WaylandSource *) g_source_new(&wayland_source_funcs, |
| 75 | sizeof (WaylandSource)); |
| 76 | source->display = display; |
| 77 | source->pfd.fd = wl_display_get_fd(display, |
| 78 | wayland_source_update, source); |
| 79 | source->pfd.events = G_IO_IN | G_IO_ERR; |
| 80 | g_source_add_poll(&source->source, &source->pfd); |
| 81 | |
| 82 | return &source->source; |
| 83 | } |