blob: 567e241ae7379901e05616bd1831dd8aac51f65a [file] [log] [blame]
Kristian Høgsbergfb590842008-11-07 14:27:23 -05001#include <stdint.h>
2#include <glib/giochannel.h>
3#include "wayland-client.h"
4#include "wayland-glib.h"
5
6struct _WaylandSource {
7 GSource source;
8 GPollFD pfd;
9 uint32_t mask;
10 struct wl_display *display;
11};
12
13static gboolean
14wayland_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
31static gboolean
32wayland_source_check(GSource *base)
33{
34 WaylandSource *source = (WaylandSource *) base;
35
36 return source->pfd.revents;
37}
38
39static gboolean
40wayland_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øgsberg1cbaa6a2008-11-07 15:54:48 -050047 WL_DISPLAY_READABLE);
Kristian Høgsbergfb590842008-11-07 14:27:23 -050048
49 return TRUE;
50}
51
52static GSourceFuncs wayland_source_funcs = {
53 wayland_source_prepare,
54 wayland_source_check,
55 wayland_source_dispatch,
56 NULL
57};
58
59static int
60wayland_source_update(uint32_t mask, void *data)
61{
62 WaylandSource *source = data;
63
64 source->mask = mask;
65
66 return 0;
67}
68
69GSource *
70wayland_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}