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