1 /*
2  * GStreamer
3  * Copyright (C) 2010  Intel Corporation.
4  * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
5  * Copyright (C) 2016 Matthew Waters <matthew@centricular.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  * Authors:
23  *  Matthew Allum
24  *  Robert Bragg
25  *  Kristian Høgsberg
26  */
27 
28 /* code originally from clutter's wayland backend found here
29  * http://git.gnome.org/browse/clutter/tree/clutter/wayland/clutter-event-wayland.c
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <wayland-client.h>
39 
40 #include "wayland_event_source.h"
41 
42 static void
sync_callback(void * data,struct wl_callback * callback,uint32_t serial)43 sync_callback (void *data, struct wl_callback *callback, uint32_t serial)
44 {
45   gboolean *done = data;
46 
47   *done = TRUE;
48   wl_callback_destroy (callback);
49 }
50 
51 static const struct wl_callback_listener sync_listener = {
52   sync_callback
53 };
54 
55 /* only thread safe iff called on the same thread @queue is being dispatched on */
56 gint
gst_vulkan_wl_display_roundtrip_queue(struct wl_display * display,struct wl_event_queue * queue)57 gst_vulkan_wl_display_roundtrip_queue (struct wl_display *display,
58     struct wl_event_queue *queue)
59 {
60   struct wl_callback *callback;
61   gboolean done = FALSE;
62   gint ret = 0;
63 
64   if (queue) {
65     /* creating a wl_proxy and setting the queue is racy with the dispatching
66      * of the default queue */
67     while (wl_display_prepare_read_queue (display, queue) != 0) {
68       if ((ret = wl_display_dispatch_queue_pending (display, queue)) < 0)
69         return ret;
70     }
71   }
72   if (!(callback = wl_display_sync (display))) {
73     return -1;
74   }
75   wl_callback_add_listener (callback, &sync_listener, &done);
76   if (queue) {
77     wl_proxy_set_queue ((struct wl_proxy *) callback, queue);
78     wl_display_cancel_read (display);
79     while (!done && ret >= 0)
80       ret = wl_display_dispatch_queue (display, queue);
81   } else {
82     while (!done && ret >= 0)
83       ret = wl_display_dispatch (display);
84   }
85 
86   if (ret == -1 && !done)
87     wl_callback_destroy (callback);
88 
89   return ret;
90 }
91 
92 typedef struct _WaylandEventSource
93 {
94   GSource source;
95   GPollFD pfd;
96   uint32_t mask;
97   struct wl_display *display;
98   struct wl_event_queue *queue;
99   gboolean reading;
100 } WaylandEventSource;
101 
102 static gboolean
wayland_event_source_prepare(GSource * base,gint * timeout)103 wayland_event_source_prepare (GSource * base, gint * timeout)
104 {
105   WaylandEventSource *source = (WaylandEventSource *) base;
106 
107   *timeout = -1;
108 
109   /* we may be called multiple times for prepare */
110   if (source->reading)
111     wl_display_cancel_read (source->display);
112 
113   if (source->queue) {
114     while (wl_display_prepare_read_queue (source->display, source->queue) != 0) {
115       if (wl_display_dispatch_queue_pending (source->display,
116               source->queue) < 0) {
117         g_critical ("Failed to dispatch pending events\n");
118       }
119     }
120   } else {
121     while (wl_display_prepare_read (source->display) != 0) {
122       if (wl_display_dispatch_pending (source->display) < 0) {
123         g_critical ("Failed to dispatch pending events\n");
124       }
125     }
126   }
127   source->reading = TRUE;
128 
129   /* FIXME: this may return EAGAIN if the fd is full */
130   if (wl_display_flush (source->display) < 0)
131     g_critical ("Failed to flush Wayland connection\n");
132 
133   return FALSE;
134 }
135 
136 static gboolean
wayland_event_source_check(GSource * base)137 wayland_event_source_check (GSource * base)
138 {
139   WaylandEventSource *source = (WaylandEventSource *) base;
140   gboolean retval;
141 
142   retval = source->pfd.revents;
143 
144   if (source->pfd.revents & G_IO_IN) {
145     wl_display_read_events (source->display);
146   } else {
147     wl_display_cancel_read (source->display);
148   }
149   source->reading = FALSE;
150 
151   return retval;
152 }
153 
154 static gboolean
wayland_event_source_dispatch(GSource * base,GSourceFunc callback,gpointer data)155 wayland_event_source_dispatch (GSource * base,
156     GSourceFunc callback, gpointer data)
157 {
158   WaylandEventSource *source = (WaylandEventSource *) base;
159 
160   if (source->queue)
161     wl_display_dispatch_queue_pending (source->display, source->queue);
162   else
163     wl_display_dispatch_pending (source->display);
164   source->pfd.revents = 0;
165 
166   if (callback)
167     callback (data);
168 
169   return TRUE;
170 }
171 
172 static void
wayland_event_source_finalize(GSource * base)173 wayland_event_source_finalize (GSource * base)
174 {
175   WaylandEventSource *source = (WaylandEventSource *) base;
176 
177   if (source->reading) {
178     wl_display_cancel_read (source->display);
179   }
180   source->reading = FALSE;
181 }
182 
183 static GSourceFuncs wayland_event_source_funcs = {
184   wayland_event_source_prepare,
185   wayland_event_source_check,
186   wayland_event_source_dispatch,
187   wayland_event_source_finalize
188 };
189 
190 GSource *
wayland_event_source_new(struct wl_display * display,struct wl_event_queue * queue)191 wayland_event_source_new (struct wl_display *display,
192     struct wl_event_queue *queue)
193 {
194   WaylandEventSource *source;
195 
196   source = (WaylandEventSource *)
197       g_source_new (&wayland_event_source_funcs, sizeof (WaylandEventSource));
198   source->display = display;
199   source->queue = queue;
200   source->pfd.fd = wl_display_get_fd (display);
201   source->pfd.events = G_IO_IN | G_IO_ERR;
202   g_source_add_poll (&source->source, &source->pfd);
203 
204   return &source->source;
205 }
206