blob: fa7a9f5b7a94c0f8cfcd2aec49f215742356fc99 [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
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050028typedef struct _WlSource {
Kristian Høgsbergfb590842008-11-07 14:27:23 -050029 GSource source;
30 GPollFD pfd;
31 uint32_t mask;
32 struct wl_display *display;
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050033} WlSource;
Kristian Høgsbergfb590842008-11-07 14:27:23 -050034
35static gboolean
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050036wl_glib_source_prepare(GSource *base, gint *timeout)
Kristian Høgsbergfb590842008-11-07 14:27:23 -050037{
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050038 WlSource *source = (WlSource *) base;
Kristian Høgsbergfb590842008-11-07 14:27:23 -050039
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
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050054wl_glib_source_check(GSource *base)
Kristian Høgsbergfb590842008-11-07 14:27:23 -050055{
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050056 WlSource *source = (WlSource *) base;
Kristian Høgsbergfb590842008-11-07 14:27:23 -050057
58 return source->pfd.revents;
59}
60
61static gboolean
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050062wl_glib_source_dispatch(GSource *base,
Kristian Høgsbergfb590842008-11-07 14:27:23 -050063 GSourceFunc callback,
64 gpointer data)
65{
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050066 WlSource *source = (WlSource *) base;
Kristian Høgsbergfb590842008-11-07 14:27:23 -050067
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
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050074static GSourceFuncs wl_glib_source_funcs = {
75 wl_glib_source_prepare,
76 wl_glib_source_check,
77 wl_glib_source_dispatch,
Kristian Høgsbergfb590842008-11-07 14:27:23 -050078 NULL
79};
80
81static int
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050082wl_glib_source_update(uint32_t mask, void *data)
Kristian Høgsbergfb590842008-11-07 14:27:23 -050083{
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050084 WlSource *source = data;
Kristian Høgsbergfb590842008-11-07 14:27:23 -050085
86 source->mask = mask;
87
88 return 0;
89}
90
91GSource *
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050092wl_glib_source_new(struct wl_display *display)
Kristian Høgsbergfb590842008-11-07 14:27:23 -050093{
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050094 WlSource *source;
Kristian Høgsbergfb590842008-11-07 14:27:23 -050095
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -050096 source = (WlSource *) g_source_new(&wl_glib_source_funcs,
97 sizeof (WlSource));
Kristian Høgsbergfb590842008-11-07 14:27:23 -050098 source->display = display;
99 source->pfd.fd = wl_display_get_fd(display,
Kristian Høgsberge2ce43a2008-12-02 18:28:23 -0500100 wl_glib_source_update, source);
Kristian Høgsbergfb590842008-11-07 14:27:23 -0500101 source->pfd.events = G_IO_IN | G_IO_ERR;
102 g_source_add_poll(&source->source, &source->pfd);
103
104 return &source->source;
105}