Kristian Høgsberg | ffd710e | 2008-12-02 15:15:01 -0500 | [diff] [blame] | 1 | /* |
| 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øgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <glib/giochannel.h> |
| 25 | #include "wayland-client.h" |
| 26 | #include "wayland-glib.h" |
| 27 | |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 28 | typedef struct _WlSource { |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 29 | GSource source; |
| 30 | GPollFD pfd; |
| 31 | uint32_t mask; |
| 32 | struct wl_display *display; |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 33 | } WlSource; |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 34 | |
| 35 | static gboolean |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 36 | wl_glib_source_prepare(GSource *base, gint *timeout) |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 37 | { |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 38 | WlSource *source = (WlSource *) base; |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 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 | |
| 53 | static gboolean |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 54 | wl_glib_source_check(GSource *base) |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 55 | { |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 56 | WlSource *source = (WlSource *) base; |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 57 | |
| 58 | return source->pfd.revents; |
| 59 | } |
| 60 | |
| 61 | static gboolean |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 62 | wl_glib_source_dispatch(GSource *base, |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 63 | GSourceFunc callback, |
| 64 | gpointer data) |
| 65 | { |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 66 | WlSource *source = (WlSource *) base; |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 67 | |
| 68 | wl_display_iterate(source->display, |
Kristian Høgsberg | 1cbaa6a | 2008-11-07 15:54:48 -0500 | [diff] [blame] | 69 | WL_DISPLAY_READABLE); |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 70 | |
| 71 | return TRUE; |
| 72 | } |
| 73 | |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 74 | static GSourceFuncs wl_glib_source_funcs = { |
| 75 | wl_glib_source_prepare, |
| 76 | wl_glib_source_check, |
| 77 | wl_glib_source_dispatch, |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 78 | NULL |
| 79 | }; |
| 80 | |
| 81 | static int |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 82 | wl_glib_source_update(uint32_t mask, void *data) |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 83 | { |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 84 | WlSource *source = data; |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 85 | |
| 86 | source->mask = mask; |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | GSource * |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 92 | wl_glib_source_new(struct wl_display *display) |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 93 | { |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 94 | WlSource *source; |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 95 | |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 96 | source = (WlSource *) g_source_new(&wl_glib_source_funcs, |
| 97 | sizeof (WlSource)); |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 98 | source->display = display; |
| 99 | source->pfd.fd = wl_display_get_fd(display, |
Kristian Høgsberg | e2ce43a | 2008-12-02 18:28:23 -0500 | [diff] [blame] | 100 | wl_glib_source_update, source); |
Kristian Høgsberg | fb59084 | 2008-11-07 14:27:23 -0500 | [diff] [blame] | 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 | } |