2012-11-28 14:01:59 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2010 Intel Corporation.
|
|
|
|
* Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Matthew Allum
|
|
|
|
* Robert Bragg
|
|
|
|
* Kristian Høgsberg
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* code originally from clutter's wayland backend found here
|
|
|
|
* http://git.gnome.org/browse/clutter/tree/clutter/wayland/clutter-event-wayland.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wayland-client.h>
|
2018-01-04 04:33:33 +00:00
|
|
|
#include <gst/gst.h>
|
2012-11-28 14:01:59 +00:00
|
|
|
|
|
|
|
#include "wayland_event_source.h"
|
|
|
|
|
2015-05-22 06:07:49 +00:00
|
|
|
static void
|
|
|
|
sync_callback (void *data, struct wl_callback *callback, uint32_t serial)
|
|
|
|
{
|
|
|
|
gboolean *done = data;
|
|
|
|
|
2018-01-04 04:33:33 +00:00
|
|
|
GST_TRACE ("roundtrip done. callback:%p", callback);
|
|
|
|
|
2015-05-22 06:07:49 +00:00
|
|
|
*done = TRUE;
|
|
|
|
wl_callback_destroy (callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_callback_listener sync_listener = {
|
|
|
|
sync_callback
|
|
|
|
};
|
|
|
|
|
2018-01-04 04:33:33 +00:00
|
|
|
/* only thread safe iff called on the same thread @queue is being dispatched on.
|
|
|
|
* Otherwise, two prepare_read{_queue}()'s can be indicated for the same
|
|
|
|
* queue and dispatch{_queue}() may be called for different threads which
|
|
|
|
* will cause deadlocks as no guarantees for thread-safety are given when
|
|
|
|
* pumping the same queue from multiple threads.
|
|
|
|
* As a concrete example, if the wayland event source (below) for a @queue is
|
|
|
|
* running on a certain thread, then this function must only be called in that
|
|
|
|
* thread (with that @queue). */
|
2015-05-22 06:07:49 +00:00
|
|
|
gint
|
|
|
|
gst_gl_wl_display_roundtrip_queue (struct wl_display *display,
|
|
|
|
struct wl_event_queue *queue)
|
|
|
|
{
|
2016-06-30 13:06:43 +00:00
|
|
|
struct wl_callback *callback;
|
2015-05-22 06:07:49 +00:00
|
|
|
gboolean done = FALSE;
|
|
|
|
gint ret = 0;
|
|
|
|
|
2018-01-04 04:33:33 +00:00
|
|
|
GST_TRACE ("roundtrip start for dpy %p and queue %p", display, queue);
|
|
|
|
|
2016-06-30 13:06:43 +00:00
|
|
|
if (queue) {
|
|
|
|
/* creating a wl_proxy and setting the queue is racy with the dispatching
|
|
|
|
* of the default queue */
|
|
|
|
while (wl_display_prepare_read_queue (display, queue) != 0) {
|
2018-01-04 04:33:33 +00:00
|
|
|
if ((ret = wl_display_dispatch_queue_pending (display, queue)) < 0) {
|
2016-06-30 13:06:43 +00:00
|
|
|
return ret;
|
2018-01-04 04:33:33 +00:00
|
|
|
}
|
2016-06-30 13:06:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(callback = wl_display_sync (display))) {
|
2015-05-22 06:07:49 +00:00
|
|
|
return -1;
|
2016-06-30 13:06:43 +00:00
|
|
|
}
|
2018-01-04 04:33:33 +00:00
|
|
|
GST_TRACE ("create roundtrip callback %p", callback);
|
2015-05-22 06:07:49 +00:00
|
|
|
wl_callback_add_listener (callback, &sync_listener, &done);
|
2015-05-27 05:44:33 +00:00
|
|
|
if (queue) {
|
|
|
|
wl_proxy_set_queue ((struct wl_proxy *) callback, queue);
|
2016-06-30 13:06:43 +00:00
|
|
|
wl_display_cancel_read (display);
|
2018-01-04 04:33:33 +00:00
|
|
|
while (!done && ret >= 0) {
|
2015-05-27 05:44:33 +00:00
|
|
|
ret = wl_display_dispatch_queue (display, queue);
|
2018-01-04 04:33:33 +00:00
|
|
|
}
|
2015-05-27 05:44:33 +00:00
|
|
|
} else {
|
2018-01-04 04:33:33 +00:00
|
|
|
while (!done && ret >= 0) {
|
2015-05-27 05:44:33 +00:00
|
|
|
ret = wl_display_dispatch (display);
|
2018-01-04 04:33:33 +00:00
|
|
|
}
|
2015-05-27 05:44:33 +00:00
|
|
|
}
|
2015-05-22 06:07:49 +00:00
|
|
|
|
|
|
|
if (ret == -1 && !done)
|
|
|
|
wl_callback_destroy (callback);
|
2018-01-04 04:33:33 +00:00
|
|
|
GST_TRACE ("roundtrip done for dpy %p and queue %p. ret %i", display, queue,
|
|
|
|
ret);
|
2015-05-22 06:07:49 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-11-28 14:01:59 +00:00
|
|
|
typedef struct _WaylandEventSource
|
|
|
|
{
|
|
|
|
GSource source;
|
|
|
|
GPollFD pfd;
|
|
|
|
uint32_t mask;
|
|
|
|
struct wl_display *display;
|
2015-05-22 06:07:49 +00:00
|
|
|
struct wl_event_queue *queue;
|
2016-06-30 13:06:43 +00:00
|
|
|
gboolean reading;
|
2012-11-28 14:01:59 +00:00
|
|
|
} WaylandEventSource;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
wayland_event_source_prepare (GSource * base, gint * timeout)
|
|
|
|
{
|
|
|
|
WaylandEventSource *source = (WaylandEventSource *) base;
|
|
|
|
|
|
|
|
*timeout = -1;
|
|
|
|
|
2016-06-30 13:06:43 +00:00
|
|
|
/* we may be called multiple times for prepare */
|
|
|
|
if (source->reading)
|
|
|
|
wl_display_cancel_read (source->display);
|
|
|
|
|
|
|
|
if (source->queue) {
|
2019-03-08 18:52:25 +00:00
|
|
|
if (wl_display_prepare_read_queue (source->display, source->queue) != 0)
|
|
|
|
return TRUE;
|
2016-06-30 13:06:43 +00:00
|
|
|
} else {
|
2019-03-08 18:52:25 +00:00
|
|
|
if (wl_display_prepare_read (source->display) != 0)
|
|
|
|
return TRUE;
|
2016-06-30 13:06:43 +00:00
|
|
|
}
|
2019-03-08 18:52:25 +00:00
|
|
|
|
2016-06-30 13:06:43 +00:00
|
|
|
source->reading = TRUE;
|
|
|
|
|
|
|
|
/* FIXME: this may return EAGAIN if the fd is full */
|
|
|
|
if (wl_display_flush (source->display) < 0)
|
|
|
|
g_critical ("Failed to flush Wayland connection\n");
|
2012-11-28 14:01:59 +00:00
|
|
|
|
2015-05-22 03:12:09 +00:00
|
|
|
return FALSE;
|
2012-11-28 14:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
wayland_event_source_check (GSource * base)
|
|
|
|
{
|
|
|
|
WaylandEventSource *source = (WaylandEventSource *) base;
|
|
|
|
|
2016-06-30 13:06:43 +00:00
|
|
|
source->reading = FALSE;
|
|
|
|
|
2019-05-28 15:43:25 +00:00
|
|
|
if (source->pfd.revents & G_IO_IN) {
|
|
|
|
if (wl_display_read_events (source->display) == 0)
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
wl_display_cancel_read (source->display);
|
|
|
|
}
|
2019-03-08 18:52:25 +00:00
|
|
|
|
|
|
|
return FALSE;
|
2012-11-28 14:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
wayland_event_source_dispatch (GSource * base,
|
|
|
|
GSourceFunc callback, gpointer data)
|
|
|
|
{
|
|
|
|
WaylandEventSource *source = (WaylandEventSource *) base;
|
|
|
|
|
2018-01-04 04:33:33 +00:00
|
|
|
if (source->queue) {
|
2016-06-30 13:06:43 +00:00
|
|
|
wl_display_dispatch_queue_pending (source->display, source->queue);
|
2018-01-04 04:33:33 +00:00
|
|
|
} else {
|
2016-06-30 13:06:43 +00:00
|
|
|
wl_display_dispatch_pending (source->display);
|
2018-01-04 04:33:33 +00:00
|
|
|
}
|
2016-06-30 13:06:43 +00:00
|
|
|
source->pfd.revents = 0;
|
2012-11-28 14:01:59 +00:00
|
|
|
|
|
|
|
if (callback)
|
|
|
|
callback (data);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-06-30 13:06:43 +00:00
|
|
|
static void
|
|
|
|
wayland_event_source_finalize (GSource * base)
|
|
|
|
{
|
|
|
|
WaylandEventSource *source = (WaylandEventSource *) base;
|
|
|
|
|
|
|
|
if (source->reading) {
|
|
|
|
wl_display_cancel_read (source->display);
|
|
|
|
}
|
|
|
|
source->reading = FALSE;
|
|
|
|
}
|
|
|
|
|
2012-11-28 14:01:59 +00:00
|
|
|
static GSourceFuncs wayland_event_source_funcs = {
|
|
|
|
wayland_event_source_prepare,
|
|
|
|
wayland_event_source_check,
|
|
|
|
wayland_event_source_dispatch,
|
2016-06-30 13:06:43 +00:00
|
|
|
wayland_event_source_finalize
|
2012-11-28 14:01:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
GSource *
|
2015-05-22 06:07:49 +00:00
|
|
|
wayland_event_source_new (struct wl_display *display,
|
|
|
|
struct wl_event_queue *queue)
|
2012-11-28 14:01:59 +00:00
|
|
|
{
|
|
|
|
WaylandEventSource *source;
|
|
|
|
|
|
|
|
source = (WaylandEventSource *)
|
|
|
|
g_source_new (&wayland_event_source_funcs, sizeof (WaylandEventSource));
|
|
|
|
source->display = display;
|
2015-05-22 06:07:49 +00:00
|
|
|
source->queue = queue;
|
2012-11-28 14:01:59 +00:00
|
|
|
source->pfd.fd = wl_display_get_fd (display);
|
|
|
|
source->pfd.events = G_IO_IN | G_IO_ERR;
|
|
|
|
g_source_add_poll (&source->source, &source->pfd);
|
|
|
|
|
|
|
|
return &source->source;
|
|
|
|
}
|