blob: b6f2c27b25f24f2b26da3409823816ac5a0ddef5 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsbergfb590842008-11-07 14:27:23 -050023#include <stdint.h>
24#include <glib/giochannel.h>
25#include "wayland-client.h"
26#include "wayland-glib.h"
27
28struct _WaylandSource {
29 GSource source;
30 GPollFD pfd;
31 uint32_t mask;
32 struct wl_display *display;
33};
34
35static gboolean
36wayland_source_prepare(GSource *base, gint *timeout)
37{
38 WaylandSource *source = (WaylandSource *) base;
39
40 *timeout = -1;
41
42 /* We have to add/remove the GPollFD if we want to update our
43 * poll event mask dynamically. Instead, let's just flush all
44 * write on idle instead, which is what this amounts to. */
45
46 while (source->mask & WL_DISPLAY_WRITABLE)
47 wl_display_iterate(source->display,
48 WL_DISPLAY_WRITABLE);
49
50 return FALSE;
51}
52
53static gboolean
54wayland_source_check(GSource *base)
55{
56 WaylandSource *source = (WaylandSource *) base;
57
58 return source->pfd.revents;
59}
60
61static gboolean
62wayland_source_dispatch(GSource *base,
63 GSourceFunc callback,
64 gpointer data)
65{
66 WaylandSource *source = (WaylandSource *) base;
67
68 wl_display_iterate(source->display,
Kristian Høgsberg1cbaa6a2008-11-07 15:54:48 -050069 WL_DISPLAY_READABLE);
Kristian Høgsbergfb590842008-11-07 14:27:23 -050070
71 return TRUE;
72}
73
74static GSourceFuncs wayland_source_funcs = {
75 wayland_source_prepare,
76 wayland_source_check,
77 wayland_source_dispatch,
78 NULL
79};
80
81static int
82wayland_source_update(uint32_t mask, void *data)
83{
84 WaylandSource *source = data;
85
86 source->mask = mask;
87
88 return 0;
89}
90
91GSource *
92wayland_source_new(struct wl_display *display)
93{
94 WaylandSource *source;
95
96 source = (WaylandSource *) g_source_new(&wayland_source_funcs,
97 sizeof (WaylandSource));
98 source->display = display;
99 source->pfd.fd = wl_display_get_fd(display,
100 wayland_source_update, source);
101 source->pfd.events = G_IO_IN | G_IO_ERR;
102 g_source_add_poll(&source->source, &source->pfd);
103
104 return &source->source;
105}