2010-12-18 20:38:49 +00:00
|
|
|
/* GDK - The GIMP Drawing Kit
|
|
|
|
* Copyright (C) 2009 Carlos Garnacho <carlosg@gnome.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2010-12-18 20:38:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2011-02-09 19:48:29 +00:00
|
|
|
#include "gdkprivate-wayland.h"
|
2010-12-18 20:38:49 +00:00
|
|
|
|
2021-09-24 19:49:38 +00:00
|
|
|
#include "gdkeventsprivate.h"
|
|
|
|
|
2017-05-24 14:42:01 +00:00
|
|
|
#include <unistd.h>
|
2013-07-09 17:08:21 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
2010-12-18 20:38:49 +00:00
|
|
|
typedef struct _GdkWaylandEventSource {
|
|
|
|
GSource source;
|
|
|
|
GPollFD pfd;
|
|
|
|
uint32_t mask;
|
|
|
|
GdkDisplay *display;
|
2016-03-18 06:17:23 +00:00
|
|
|
gboolean reading;
|
2010-12-18 20:38:49 +00:00
|
|
|
} GdkWaylandEventSource;
|
|
|
|
|
|
|
|
static gboolean
|
2015-02-28 03:28:28 +00:00
|
|
|
gdk_event_source_prepare (GSource *base,
|
2020-07-24 13:54:49 +00:00
|
|
|
int *timeout)
|
2010-12-18 20:38:49 +00:00
|
|
|
{
|
|
|
|
GdkWaylandEventSource *source = (GdkWaylandEventSource *) base;
|
2012-04-16 15:55:43 +00:00
|
|
|
GdkWaylandDisplay *display = (GdkWaylandDisplay *) source->display;
|
2020-01-07 16:13:22 +00:00
|
|
|
GList *l;
|
2010-12-18 20:38:49 +00:00
|
|
|
|
|
|
|
*timeout = -1;
|
|
|
|
|
2013-02-20 13:53:59 +00:00
|
|
|
if (source->display->event_pause_count > 0)
|
2013-11-11 23:04:34 +00:00
|
|
|
return _gdk_event_queue_find_first (source->display) != NULL;
|
2013-02-20 13:53:59 +00:00
|
|
|
|
2010-12-18 20:38:49 +00:00
|
|
|
/* We have to add/remove the GPollFD if we want to update our
|
|
|
|
* poll event mask dynamically. Instead, let's just flush all
|
2016-05-01 15:06:33 +00:00
|
|
|
* write on idle instead, which is what this amounts to.
|
|
|
|
*/
|
2010-12-18 20:38:49 +00:00
|
|
|
|
|
|
|
if (_gdk_event_queue_find_first (source->display) != NULL)
|
|
|
|
return TRUE;
|
|
|
|
|
2016-03-18 06:17:23 +00:00
|
|
|
/* wl_display_prepare_read() needs to be balanced with either
|
|
|
|
* wl_display_read_events() or wl_display_cancel_read()
|
|
|
|
* (in gdk_event_source_check() */
|
|
|
|
if (source->reading)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* if prepare_read() returns non-zero, there are events to be dispatched */
|
|
|
|
if (wl_display_prepare_read (display->wl_display) != 0)
|
|
|
|
return TRUE;
|
2020-01-07 16:13:22 +00:00
|
|
|
|
|
|
|
/* We need to check whether there are pending events on the surface queues as well,
|
|
|
|
* but we also need to make sure to only have one active "read" in the end,
|
|
|
|
* or none if we immediately return TRUE, as multiple reads expect reads from
|
|
|
|
* as many threads.
|
|
|
|
*/
|
|
|
|
for (l = display->event_queues; l; l = l->next)
|
|
|
|
{
|
|
|
|
struct wl_event_queue *queue = l->data;
|
|
|
|
|
|
|
|
if (wl_display_prepare_read_queue (display->wl_display, queue) != 0)
|
|
|
|
{
|
|
|
|
wl_display_cancel_read (display->wl_display);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
wl_display_cancel_read (display->wl_display);
|
|
|
|
}
|
|
|
|
|
2016-03-18 06:17:23 +00:00
|
|
|
source->reading = TRUE;
|
|
|
|
|
2013-07-09 17:08:21 +00:00
|
|
|
if (wl_display_flush (display->wl_display) < 0)
|
2017-05-24 14:08:07 +00:00
|
|
|
{
|
|
|
|
g_message ("Error flushing display: %s", g_strerror (errno));
|
|
|
|
_exit (1);
|
|
|
|
}
|
2013-08-04 13:36:01 +00:00
|
|
|
|
2010-12-18 20:38:49 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2015-02-28 03:28:28 +00:00
|
|
|
gdk_event_source_check (GSource *base)
|
2010-12-18 20:38:49 +00:00
|
|
|
{
|
|
|
|
GdkWaylandEventSource *source = (GdkWaylandEventSource *) base;
|
2016-03-18 06:17:23 +00:00
|
|
|
GdkWaylandDisplay *display_wayland = (GdkWaylandDisplay *) source->display;
|
2010-12-18 20:38:49 +00:00
|
|
|
|
2013-02-20 13:53:59 +00:00
|
|
|
if (source->display->event_pause_count > 0)
|
2016-03-18 06:17:23 +00:00
|
|
|
{
|
|
|
|
if (source->reading)
|
|
|
|
wl_display_cancel_read (display_wayland->wl_display);
|
|
|
|
source->reading = FALSE;
|
|
|
|
|
|
|
|
return _gdk_event_queue_find_first (source->display) != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read the events from the wayland fd into their respective queues if we have data */
|
|
|
|
if (source->reading)
|
|
|
|
{
|
|
|
|
if (source->pfd.revents & G_IO_IN)
|
|
|
|
{
|
|
|
|
if (wl_display_read_events (display_wayland->wl_display) < 0)
|
2016-11-30 18:43:54 +00:00
|
|
|
{
|
|
|
|
g_message ("Error reading events from display: %s", g_strerror (errno));
|
|
|
|
_exit (1);
|
|
|
|
}
|
2016-03-18 06:17:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
wl_display_cancel_read (display_wayland->wl_display);
|
|
|
|
source->reading = FALSE;
|
|
|
|
}
|
2013-02-20 13:53:59 +00:00
|
|
|
|
2010-12-18 20:38:49 +00:00
|
|
|
return _gdk_event_queue_find_first (source->display) != NULL ||
|
|
|
|
source->pfd.revents;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2015-02-28 03:28:28 +00:00
|
|
|
gdk_event_source_dispatch (GSource *base,
|
|
|
|
GSourceFunc callback,
|
|
|
|
gpointer data)
|
2010-12-18 20:38:49 +00:00
|
|
|
{
|
|
|
|
GdkWaylandEventSource *source = (GdkWaylandEventSource *) base;
|
|
|
|
GdkDisplay *display = source->display;
|
|
|
|
GdkEvent *event;
|
|
|
|
|
|
|
|
event = gdk_display_get_event (display);
|
|
|
|
|
|
|
|
if (event)
|
|
|
|
{
|
|
|
|
_gdk_event_emit (event);
|
|
|
|
|
2020-02-15 20:07:24 +00:00
|
|
|
gdk_event_unref (event);
|
2010-12-18 20:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-03-18 06:17:23 +00:00
|
|
|
gdk_event_source_finalize (GSource *base)
|
2010-12-18 20:38:49 +00:00
|
|
|
{
|
2016-03-18 06:17:23 +00:00
|
|
|
GdkWaylandEventSource *source = (GdkWaylandEventSource *) base;
|
|
|
|
GdkWaylandDisplay *display = (GdkWaylandDisplay *) source->display;
|
|
|
|
|
|
|
|
if (source->reading)
|
|
|
|
wl_display_cancel_read (display->wl_display);
|
|
|
|
source->reading = FALSE;
|
2010-12-18 20:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GSourceFuncs wl_glib_source_funcs = {
|
|
|
|
gdk_event_source_prepare,
|
|
|
|
gdk_event_source_check,
|
|
|
|
gdk_event_source_dispatch,
|
|
|
|
gdk_event_source_finalize
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2015-02-28 03:28:28 +00:00
|
|
|
_gdk_wayland_display_deliver_event (GdkDisplay *display,
|
|
|
|
GdkEvent *event)
|
2010-12-18 20:38:49 +00:00
|
|
|
{
|
|
|
|
GList *node;
|
|
|
|
|
|
|
|
node = _gdk_event_queue_append (display, event);
|
2012-01-06 16:48:13 +00:00
|
|
|
_gdk_windowing_got_event (display, node, event,
|
|
|
|
_gdk_display_get_next_serial (display));
|
2010-12-18 20:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GSource *
|
|
|
|
_gdk_wayland_display_event_source_new (GdkDisplay *display)
|
|
|
|
{
|
|
|
|
GSource *source;
|
|
|
|
GdkWaylandEventSource *wl_source;
|
2012-04-16 15:55:43 +00:00
|
|
|
GdkWaylandDisplay *display_wayland;
|
2010-12-18 20:38:49 +00:00
|
|
|
char *name;
|
|
|
|
|
|
|
|
source = g_source_new (&wl_glib_source_funcs,
|
|
|
|
sizeof (GdkWaylandEventSource));
|
2016-05-01 14:52:31 +00:00
|
|
|
name = g_strdup_printf ("GDK Wayland Event source (%s)",
|
|
|
|
gdk_display_get_name (display));
|
2010-12-18 20:38:49 +00:00
|
|
|
g_source_set_name (source, name);
|
|
|
|
g_free (name);
|
|
|
|
wl_source = (GdkWaylandEventSource *) source;
|
|
|
|
|
2012-04-16 15:55:43 +00:00
|
|
|
display_wayland = GDK_WAYLAND_DISPLAY (display);
|
2010-12-18 20:38:49 +00:00
|
|
|
wl_source->display = display;
|
2014-12-29 01:54:09 +00:00
|
|
|
wl_source->pfd.fd = wl_display_get_fd (display_wayland->wl_display);
|
2013-02-06 18:08:15 +00:00
|
|
|
wl_source->pfd.events = G_IO_IN | G_IO_ERR | G_IO_HUP;
|
2014-12-29 01:54:09 +00:00
|
|
|
g_source_add_poll (source, &wl_source->pfd);
|
2010-12-18 20:38:49 +00:00
|
|
|
|
|
|
|
g_source_set_priority (source, GDK_PRIORITY_EVENTS);
|
|
|
|
g_source_set_can_recurse (source, TRUE);
|
|
|
|
g_source_attach (source, NULL);
|
|
|
|
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_gdk_wayland_display_queue_events (GdkDisplay *display)
|
|
|
|
{
|
2012-04-16 15:55:43 +00:00
|
|
|
GdkWaylandDisplay *display_wayland;
|
2010-12-18 20:38:49 +00:00
|
|
|
GdkWaylandEventSource *source;
|
2020-01-07 16:13:22 +00:00
|
|
|
GList *l;
|
2010-12-18 20:38:49 +00:00
|
|
|
|
2012-04-16 15:55:43 +00:00
|
|
|
display_wayland = GDK_WAYLAND_DISPLAY (display);
|
2010-12-18 20:38:49 +00:00
|
|
|
source = (GdkWaylandEventSource *) display_wayland->event_source;
|
2013-07-09 22:35:06 +00:00
|
|
|
|
2016-03-18 06:17:23 +00:00
|
|
|
if (wl_display_dispatch_pending (display_wayland->wl_display) < 0)
|
2013-07-09 17:08:21 +00:00
|
|
|
{
|
2017-05-24 14:42:01 +00:00
|
|
|
g_message ("Error %d (%s) dispatching to Wayland display.",
|
2016-03-18 06:17:23 +00:00
|
|
|
errno, g_strerror (errno));
|
2017-05-24 14:42:01 +00:00
|
|
|
_exit (1);
|
2013-07-09 17:08:21 +00:00
|
|
|
}
|
2013-03-30 18:17:07 +00:00
|
|
|
|
2020-01-07 16:13:22 +00:00
|
|
|
for (l = display_wayland->event_queues; l; l = l->next)
|
|
|
|
{
|
|
|
|
struct wl_event_queue *queue = l->data;
|
|
|
|
|
|
|
|
if (wl_display_dispatch_queue_pending (display_wayland->wl_display, queue) < 0)
|
|
|
|
{
|
|
|
|
g_message ("Error %d (%s) dispatching to Wayland display.",
|
|
|
|
errno, g_strerror (errno));
|
|
|
|
_exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-30 18:17:07 +00:00
|
|
|
if (source->pfd.revents & (G_IO_ERR | G_IO_HUP))
|
2015-02-27 12:06:29 +00:00
|
|
|
{
|
2017-05-24 14:42:01 +00:00
|
|
|
g_message ("Lost connection to Wayland compositor.");
|
|
|
|
_exit (1);
|
2015-02-27 12:06:29 +00:00
|
|
|
}
|
2013-07-09 22:35:06 +00:00
|
|
|
source->pfd.revents = 0;
|
2010-12-18 20:38:49 +00:00
|
|
|
}
|