| 1 |
/* GDK - The GIMP Drawing Kit |
| 2 |
* Copyright (C) 2009 Carlos Garnacho <[email protected]> |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "config.h" |
| 19 |
|
| 20 |
#include "gdkinternals.h" |
| 21 |
#include "gdkprivate-wayland.h" |
| 22 |
|
| 23 |
#include <unistd.h> |
| 24 |
#include <errno.h> |
| 25 |
|
| 26 |
typedef struct _GdkWaylandEventSource { |
| 27 |
GSource source; |
| 28 |
GPollFD pfd; |
| 29 |
uint32_t mask; |
| 30 |
GdkDisplay *display; |
| 31 |
gboolean reading; |
| 32 |
} GdkWaylandEventSource; |
| 33 |
|
| 34 |
static gboolean |
| 35 |
gdk_event_source_prepare (GSource *base, |
| 36 |
gint *timeout) |
| 37 |
{ |
| 38 |
GdkWaylandEventSource *source = (GdkWaylandEventSource *) base; |
| 39 |
GdkWaylandDisplay *display = (GdkWaylandDisplay *) source->display; |
| 40 |
|
| 41 |
*timeout = -1; |
| 42 |
|
| 43 |
if (source->display->event_pause_count > 0) |
| 44 |
return _gdk_event_queue_find_first (source->display) != NULL; |
| 45 |
|
| 46 |
/* We have to add/remove the GPollFD if we want to update our |
| 47 |
* poll event mask dynamically. Instead, let's just flush all |
| 48 |
* write on idle instead, which is what this amounts to. |
| 49 |
*/ |
| 50 |
|
| 51 |
if (_gdk_event_queue_find_first (source->display) != NULL) |
| 52 |
return TRUE; |
| 53 |
|
| 54 |
/* wl_display_prepare_read() needs to be balanced with either |
| 55 |
* wl_display_read_events() or wl_display_cancel_read() |
| 56 |
* (in gdk_event_source_check() */ |
| 57 |
if (source->reading) |
| 58 |
return FALSE; |
| 59 |
|
| 60 |
/* if prepare_read() returns non-zero, there are events to be dispatched */ |
| 61 |
if (wl_display_prepare_read (display->wl_display) != 0) |
| 62 |
return TRUE; |
| 63 |
source->reading = TRUE; |
| 64 |
|
| 65 |
if (wl_display_flush (display->wl_display) < 0) |
| 66 |
{ |
| 67 |
g_message ("Error flushing display: %s", g_strerror (errno)); |
| 68 |
_exit (1); |
| 69 |
} |
| 70 |
|
| 71 |
return FALSE; |
| 72 |
} |
| 73 |
|
| 74 |
static gboolean |
| 75 |
gdk_event_source_check (GSource *base) |
| 76 |
{ |
| 77 |
GdkWaylandEventSource *source = (GdkWaylandEventSource *) base; |
| 78 |
GdkWaylandDisplay *display_wayland = (GdkWaylandDisplay *) source->display; |
| 79 |
|
| 80 |
if (source->display->event_pause_count > 0) |
| 81 |
{ |
| 82 |
if (source->reading) |
| 83 |
wl_display_cancel_read (display_wayland->wl_display); |
| 84 |
source->reading = FALSE; |
| 85 |
|
| 86 |
return _gdk_event_queue_find_first (source->display) != NULL; |
| 87 |
} |
| 88 |
|
| 89 |
/* read the events from the wayland fd into their respective queues if we have data */ |
| 90 |
if (source->reading) |
| 91 |
{ |
| 92 |
if (source->pfd.revents & G_IO_IN) |
| 93 |
{ |
| 94 |
if (wl_display_read_events (display_wayland->wl_display) < 0) |
| 95 |
{ |
| 96 |
g_message ("Error reading events from display: %s", g_strerror (errno)); |
| 97 |
_exit (1); |
| 98 |
} |
| 99 |
} |
| 100 |
else |
| 101 |
wl_display_cancel_read (display_wayland->wl_display); |
| 102 |
source->reading = FALSE; |
| 103 |
} |
| 104 |
|
| 105 |
return _gdk_event_queue_find_first (source->display) != NULL || |
| 106 |
source->pfd.revents; |
| 107 |
} |
| 108 |
|
| 109 |
static gboolean |
| 110 |
gdk_event_source_dispatch (GSource *base, |
| 111 |
GSourceFunc callback, |
| 112 |
gpointer data) |
| 113 |
{ |
| 114 |
GdkWaylandEventSource *source = (GdkWaylandEventSource *) base; |
| 115 |
GdkDisplay *display = source->display; |
| 116 |
GdkEvent *event; |
| 117 |
|
| 118 |
gdk_threads_enter (); |
| 119 |
|
| 120 |
event = gdk_display_get_event (display); |
| 121 |
|
| 122 |
if (event) |
| 123 |
{ |
| 124 |
_gdk_event_emit (event); |
| 125 |
|
| 126 |
gdk_event_free (event); |
| 127 |
} |
| 128 |
|
| 129 |
gdk_threads_leave (); |
| 130 |
|
| 131 |
return TRUE; |
| 132 |
} |
| 133 |
|
| 134 |
static void |
| 135 |
gdk_event_source_finalize (GSource *base) |
| 136 |
{ |
| 137 |
GdkWaylandEventSource *source = (GdkWaylandEventSource *) base; |
| 138 |
GdkWaylandDisplay *display = (GdkWaylandDisplay *) source->display; |
| 139 |
|
| 140 |
if (source->reading) |
| 141 |
wl_display_cancel_read (display->wl_display); |
| 142 |
source->reading = FALSE; |
| 143 |
} |
| 144 |
|
| 145 |
static GSourceFuncs wl_glib_source_funcs = { |
| 146 |
gdk_event_source_prepare, |
| 147 |
gdk_event_source_check, |
| 148 |
gdk_event_source_dispatch, |
| 149 |
gdk_event_source_finalize |
| 150 |
}; |
| 151 |
|
| 152 |
void |
| 153 |
_gdk_wayland_display_deliver_event (GdkDisplay *display, |
| 154 |
GdkEvent *event) |
| 155 |
{ |
| 156 |
GList *node; |
| 157 |
|
| 158 |
node = _gdk_event_queue_append (display, event); |
| 159 |
_gdk_windowing_got_event (display, node, event, |
| 160 |
_gdk_display_get_next_serial (display)); |
| 161 |
} |
| 162 |
|
| 163 |
GSource * |
| 164 |
_gdk_wayland_display_event_source_new (GdkDisplay *display) |
| 165 |
{ |
| 166 |
GSource *source; |
| 167 |
GdkWaylandEventSource *wl_source; |
| 168 |
GdkWaylandDisplay *display_wayland; |
| 169 |
char *name; |
| 170 |
|
| 171 |
source = g_source_new (&wl_glib_source_funcs, |
| 172 |
sizeof (GdkWaylandEventSource)); |
| 173 |
name = g_strdup_printf ("GDK Wayland Event source (%s)", |
| 174 |
gdk_display_get_name (display)); |
| 175 |
g_source_set_name (source, name); |
| 176 |
g_free (name); |
| 177 |
wl_source = (GdkWaylandEventSource *) source; |
| 178 |
|
| 179 |
display_wayland = GDK_WAYLAND_DISPLAY (display); |
| 180 |
wl_source->display = display; |
| 181 |
wl_source->pfd.fd = wl_display_get_fd (display_wayland->wl_display); |
| 182 |
wl_source->pfd.events = G_IO_IN | G_IO_ERR | G_IO_HUP; |
| 183 |
g_source_add_poll (source, &wl_source->pfd); |
| 184 |
|
| 185 |
g_source_set_priority (source, GDK_PRIORITY_EVENTS); |
| 186 |
g_source_set_can_recurse (source, TRUE); |
| 187 |
g_source_attach (source, NULL); |
| 188 |
|
| 189 |
return source; |
| 190 |
} |
| 191 |
|
| 192 |
void |
| 193 |
_gdk_wayland_display_queue_events (GdkDisplay *display) |
| 194 |
{ |
| 195 |
GdkWaylandDisplay *display_wayland; |
| 196 |
GdkWaylandEventSource *source; |
| 197 |
|
| 198 |
display_wayland = GDK_WAYLAND_DISPLAY (display); |
| 199 |
source = (GdkWaylandEventSource *) display_wayland->event_source; |
| 200 |
|
| 201 |
if (wl_display_dispatch_pending (display_wayland->wl_display) < 0) |
| 202 |
{ |
| 203 |
g_message ("Error %d (%s) dispatching to Wayland display.", |
| 204 |
errno, g_strerror (errno)); |
| 205 |
_exit (1); |
| 206 |
} |
| 207 |
|
| 208 |
if (source->pfd.revents & (G_IO_ERR | G_IO_HUP)) |
| 209 |
{ |
| 210 |
g_message ("Lost connection to Wayland compositor."); |
| 211 |
_exit (1); |
| 212 |
} |
| 213 |
source->pfd.revents = 0; |
| 214 |
} |