2014-04-02 00:35:26 +00:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
|
|
|
|
2012-01-07 22:21:32 +00:00
|
|
|
/*
|
|
|
|
* X Wayland Support
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Intel Corporation
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
* 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2014-04-02 00:35:26 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2018-07-10 08:36:24 +00:00
|
|
|
#include "wayland/meta-xwayland.h"
|
|
|
|
#include "wayland/meta-xwayland-private.h"
|
2012-01-07 22:21:32 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
2018-07-10 08:36:24 +00:00
|
|
|
#include <glib-unix.h>
|
|
|
|
#include <glib.h>
|
2012-01-07 22:21:32 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
2015-07-08 08:14:00 +00:00
|
|
|
#include "compositor/meta-surface-actor-wayland.h"
|
2018-07-10 08:36:24 +00:00
|
|
|
#include "meta/main.h"
|
2017-12-20 11:07:40 +00:00
|
|
|
#include "wayland/meta-wayland-actor-surface.h"
|
2015-07-08 08:14:00 +00:00
|
|
|
|
2016-04-01 12:57:19 +00:00
|
|
|
enum {
|
|
|
|
XWAYLAND_SURFACE_WINDOW_ASSOCIATED,
|
|
|
|
|
|
|
|
XWAYLAND_SURFACE_LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
guint xwayland_surface_signals[XWAYLAND_SURFACE_LAST_SIGNAL];
|
|
|
|
|
2015-07-08 08:14:00 +00:00
|
|
|
#define META_TYPE_WAYLAND_SURFACE_ROLE_XWAYLAND (meta_wayland_surface_role_xwayland_get_type ())
|
|
|
|
G_DECLARE_FINAL_TYPE (MetaWaylandSurfaceRoleXWayland,
|
|
|
|
meta_wayland_surface_role_xwayland,
|
|
|
|
META, WAYLAND_SURFACE_ROLE_XWAYLAND,
|
2017-12-20 11:07:40 +00:00
|
|
|
MetaWaylandActorSurface)
|
2015-07-08 08:14:00 +00:00
|
|
|
|
|
|
|
struct _MetaWaylandSurfaceRoleXWayland
|
|
|
|
{
|
2017-12-20 11:07:40 +00:00
|
|
|
MetaWaylandActorSurface parent;
|
2015-07-08 08:14:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (MetaWaylandSurfaceRoleXWayland,
|
|
|
|
meta_wayland_surface_role_xwayland,
|
2017-12-20 11:07:40 +00:00
|
|
|
META_TYPE_WAYLAND_ACTOR_SURFACE)
|
2015-07-08 08:14:00 +00:00
|
|
|
|
2018-04-06 13:47:50 +00:00
|
|
|
void
|
|
|
|
meta_xwayland_associate_window_with_surface (MetaWindow *window,
|
|
|
|
MetaWaylandSurface *surface)
|
2013-08-12 08:06:13 +00:00
|
|
|
{
|
2014-04-02 14:19:39 +00:00
|
|
|
MetaDisplay *display = window->display;
|
2014-01-31 16:24:02 +00:00
|
|
|
|
2015-08-20 08:54:45 +00:00
|
|
|
if (!meta_wayland_surface_assign_role (surface,
|
2016-01-28 09:14:06 +00:00
|
|
|
META_TYPE_WAYLAND_SURFACE_ROLE_XWAYLAND,
|
|
|
|
NULL))
|
2015-08-20 08:54:45 +00:00
|
|
|
{
|
|
|
|
wl_resource_post_error (surface->resource,
|
|
|
|
WL_DISPLAY_ERROR_INVALID_OBJECT,
|
|
|
|
"wl_surface@%d already has a different role",
|
|
|
|
wl_resource_get_id (surface->resource));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-31 16:24:02 +00:00
|
|
|
window->surface = surface;
|
2016-04-01 12:57:19 +00:00
|
|
|
meta_wayland_surface_set_window (surface, window);
|
|
|
|
g_signal_emit (surface->role,
|
|
|
|
xwayland_surface_signals[XWAYLAND_SURFACE_WINDOW_ASSOCIATED],
|
|
|
|
0);
|
2014-02-25 00:22:56 +00:00
|
|
|
|
|
|
|
meta_compositor_window_surface_changed (display->compositor, window);
|
2014-04-02 15:20:30 +00:00
|
|
|
|
2014-04-08 16:39:15 +00:00
|
|
|
/* Now that we have a surface check if it should have focus. */
|
|
|
|
meta_display_sync_wayland_input_focus (display);
|
2013-08-12 08:06:13 +00:00
|
|
|
}
|
|
|
|
|
2014-03-20 17:12:43 +00:00
|
|
|
static gboolean
|
|
|
|
associate_window_with_surface_id (MetaXWaylandManager *manager,
|
|
|
|
MetaWindow *window,
|
|
|
|
guint32 surface_id)
|
2014-04-02 14:19:39 +00:00
|
|
|
{
|
2014-03-20 17:12:43 +00:00
|
|
|
struct wl_resource *resource;
|
|
|
|
|
2015-09-25 14:18:55 +00:00
|
|
|
/* If the window has an existing surface, like if we're
|
|
|
|
* undecorating or decorating the window, then we need
|
|
|
|
* to detach the window from its old surface.
|
|
|
|
*/
|
|
|
|
if (window->surface)
|
|
|
|
{
|
|
|
|
meta_wayland_surface_set_window (window->surface, NULL);
|
|
|
|
window->surface = NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-20 17:12:43 +00:00
|
|
|
resource = wl_client_get_object (manager->client, surface_id);
|
|
|
|
if (resource)
|
|
|
|
{
|
|
|
|
MetaWaylandSurface *surface = wl_resource_get_user_data (resource);
|
2018-04-06 13:47:50 +00:00
|
|
|
meta_xwayland_associate_window_with_surface (window, surface);
|
2014-03-20 17:12:43 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
meta_xwayland_handle_wl_surface_id (MetaWindow *window,
|
|
|
|
guint32 surface_id)
|
2013-08-12 08:06:13 +00:00
|
|
|
{
|
2014-03-20 17:12:43 +00:00
|
|
|
MetaWaylandCompositor *compositor = meta_wayland_compositor_get_default ();
|
|
|
|
MetaXWaylandManager *manager = &compositor->xwayland_manager;
|
|
|
|
|
|
|
|
if (!associate_window_with_surface_id (manager, window, surface_id))
|
|
|
|
{
|
2018-04-06 13:47:50 +00:00
|
|
|
/* No surface ID yet, schedule this association for whenever the
|
|
|
|
* surface is made known.
|
2014-03-20 17:12:43 +00:00
|
|
|
*/
|
2018-04-06 13:47:50 +00:00
|
|
|
meta_wayland_compositor_schedule_surface_association (compositor,
|
|
|
|
surface_id, window);
|
2014-03-20 17:12:43 +00:00
|
|
|
}
|
2013-08-12 08:06:13 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 07:49:46 +00:00
|
|
|
gboolean
|
|
|
|
meta_xwayland_is_xwayland_surface (MetaWaylandSurface *surface)
|
|
|
|
{
|
|
|
|
MetaWaylandCompositor *compositor = meta_wayland_compositor_get_default ();
|
|
|
|
MetaXWaylandManager *manager = &compositor->xwayland_manager;
|
|
|
|
|
|
|
|
return wl_resource_get_client (surface->resource) == manager->client;
|
|
|
|
}
|
|
|
|
|
2014-04-17 19:03:45 +00:00
|
|
|
static gboolean
|
|
|
|
try_display (int display,
|
|
|
|
char **filename_out,
|
|
|
|
int *fd_out)
|
2012-01-07 22:21:32 +00:00
|
|
|
{
|
2014-04-17 19:03:45 +00:00
|
|
|
gboolean ret = FALSE;
|
2012-01-07 22:21:32 +00:00
|
|
|
char *filename;
|
|
|
|
int fd;
|
|
|
|
|
2014-04-17 19:03:45 +00:00
|
|
|
filename = g_strdup_printf ("/tmp/.X%d-lock", display);
|
|
|
|
|
|
|
|
again:
|
|
|
|
fd = open (filename, O_WRONLY | O_CLOEXEC | O_CREAT | O_EXCL, 0444);
|
|
|
|
|
|
|
|
if (fd < 0 && errno == EEXIST)
|
2012-01-07 22:21:32 +00:00
|
|
|
{
|
2014-04-17 19:03:45 +00:00
|
|
|
char pid[11];
|
2012-01-07 22:21:32 +00:00
|
|
|
char *end;
|
|
|
|
pid_t other;
|
|
|
|
|
2014-04-17 19:03:45 +00:00
|
|
|
fd = open (filename, O_CLOEXEC, O_RDONLY);
|
|
|
|
if (fd < 0 || read (fd, pid, 11) != 11)
|
|
|
|
{
|
|
|
|
g_warning ("can't read lock file %s: %m", filename);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
close (fd);
|
|
|
|
fd = -1;
|
2012-01-07 22:21:32 +00:00
|
|
|
|
xwayland: Fix lockfile size confusion
Similarly to Weston (where this code originated), there were two errors
in the X11 lockfile handling.
Firstly, after reading 11 characters from the lock file (which could
have been placed by any process), there was no guarantee of
NUL-termination, meaning strtol could've theoretically run off the end
of the string.
Secondly, whilst writing the new lock, the trailing NUL byte was not
correctly accounted for. The size passed as an input to snprintf takes
the maximum size of the string including the trailing NUL, whilst the
return (and the input to write) gives the actual size of the string
without the trailing NUL.
The code did attempt to check the return value, however snprintf returns
the size of the _potential_ string written, before snprintf culls it, so
this was off by one, and the LF was not being written.
Signed-off-by: Daniel Stone <daniels@collabora.com>
https://bugzilla.gnome.org/show_bug.cgi?id=774613
2016-11-17 11:00:25 +00:00
|
|
|
pid[10] = '\0';
|
2014-04-17 19:03:45 +00:00
|
|
|
other = strtol (pid, &end, 0);
|
|
|
|
if (end != pid + 10)
|
2012-01-07 22:21:32 +00:00
|
|
|
{
|
2014-04-17 19:03:45 +00:00
|
|
|
g_warning ("can't parse lock file %s", filename);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-01-07 22:21:32 +00:00
|
|
|
|
2014-04-17 19:03:45 +00:00
|
|
|
if (kill (other, 0) < 0 && errno == ESRCH)
|
|
|
|
{
|
2015-04-23 14:41:34 +00:00
|
|
|
/* Process is dead. Try unlinking the lock file and trying again. */
|
2014-04-17 19:03:45 +00:00
|
|
|
if (unlink (filename) < 0)
|
|
|
|
{
|
|
|
|
g_warning ("failed to unlink stale lock file %s: %m", filename);
|
|
|
|
goto out;
|
2014-01-29 15:23:58 +00:00
|
|
|
}
|
2012-01-07 22:21:32 +00:00
|
|
|
|
2014-04-17 19:03:45 +00:00
|
|
|
goto again;
|
|
|
|
}
|
2012-01-07 22:21:32 +00:00
|
|
|
|
2014-04-17 19:03:45 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else if (fd < 0)
|
|
|
|
{
|
|
|
|
g_warning ("failed to create lock file %s: %m", filename);
|
|
|
|
goto out;
|
|
|
|
}
|
2012-01-07 22:21:32 +00:00
|
|
|
|
2014-04-17 19:03:45 +00:00
|
|
|
ret = TRUE;
|
2012-01-07 22:21:32 +00:00
|
|
|
|
2014-04-17 19:03:45 +00:00
|
|
|
out:
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
g_free (filename);
|
|
|
|
filename = NULL;
|
|
|
|
|
|
|
|
if (fd >= 0)
|
2012-01-07 22:21:32 +00:00
|
|
|
{
|
2014-04-17 19:03:45 +00:00
|
|
|
close (fd);
|
|
|
|
fd = -1;
|
2012-01-07 22:21:32 +00:00
|
|
|
}
|
2014-04-17 19:03:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*filename_out = filename;
|
|
|
|
*fd_out = fd;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2015-04-23 14:41:34 +00:00
|
|
|
create_lock_file (int display, int *display_out)
|
2014-04-17 19:03:45 +00:00
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
int fd;
|
|
|
|
|
xwayland: Fix lockfile size confusion
Similarly to Weston (where this code originated), there were two errors
in the X11 lockfile handling.
Firstly, after reading 11 characters from the lock file (which could
have been placed by any process), there was no guarantee of
NUL-termination, meaning strtol could've theoretically run off the end
of the string.
Secondly, whilst writing the new lock, the trailing NUL byte was not
correctly accounted for. The size passed as an input to snprintf takes
the maximum size of the string including the trailing NUL, whilst the
return (and the input to write) gives the actual size of the string
without the trailing NUL.
The code did attempt to check the return value, however snprintf returns
the size of the _potential_ string written, before snprintf culls it, so
this was off by one, and the LF was not being written.
Signed-off-by: Daniel Stone <daniels@collabora.com>
https://bugzilla.gnome.org/show_bug.cgi?id=774613
2016-11-17 11:00:25 +00:00
|
|
|
char pid[12];
|
2014-04-17 19:03:45 +00:00
|
|
|
int size;
|
2015-03-20 19:09:13 +00:00
|
|
|
int number_of_tries = 0;
|
2014-04-17 19:03:45 +00:00
|
|
|
|
|
|
|
while (!try_display (display, &filename, &fd))
|
|
|
|
{
|
|
|
|
display++;
|
2015-03-20 19:09:13 +00:00
|
|
|
number_of_tries++;
|
2012-01-07 22:21:32 +00:00
|
|
|
|
2015-03-20 19:09:13 +00:00
|
|
|
/* If we can't get a display after 50 times, then something's wrong. Just
|
2014-04-17 19:03:45 +00:00
|
|
|
* abort in this case. */
|
2015-03-20 19:09:13 +00:00
|
|
|
if (number_of_tries >= 50)
|
2014-04-17 19:03:45 +00:00
|
|
|
return NULL;
|
2012-01-07 22:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Subtle detail: we use the pid of the wayland compositor, not the xserver
|
xwayland: Fix lockfile size confusion
Similarly to Weston (where this code originated), there were two errors
in the X11 lockfile handling.
Firstly, after reading 11 characters from the lock file (which could
have been placed by any process), there was no guarantee of
NUL-termination, meaning strtol could've theoretically run off the end
of the string.
Secondly, whilst writing the new lock, the trailing NUL byte was not
correctly accounted for. The size passed as an input to snprintf takes
the maximum size of the string including the trailing NUL, whilst the
return (and the input to write) gives the actual size of the string
without the trailing NUL.
The code did attempt to check the return value, however snprintf returns
the size of the _potential_ string written, before snprintf culls it, so
this was off by one, and the LF was not being written.
Signed-off-by: Daniel Stone <daniels@collabora.com>
https://bugzilla.gnome.org/show_bug.cgi?id=774613
2016-11-17 11:00:25 +00:00
|
|
|
* in the lock file. Another subtlety: snprintf returns the number of bytes
|
|
|
|
* it _would've_ written without either the NUL or the size clamping, hence
|
|
|
|
* the disparity in size. */
|
|
|
|
size = snprintf (pid, 12, "%10d\n", getpid ());
|
2012-01-07 22:21:32 +00:00
|
|
|
if (size != 11 || write (fd, pid, 11) != 11)
|
|
|
|
{
|
|
|
|
unlink (filename);
|
|
|
|
close (fd);
|
|
|
|
g_warning ("failed to write pid to lock file %s", filename);
|
|
|
|
g_free (filename);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
*display_out = display;
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-08-06 14:17:33 +00:00
|
|
|
bind_to_abstract_socket (int display,
|
|
|
|
gboolean *fatal)
|
2012-01-07 22:21:32 +00:00
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t size, name_size;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = socket (PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
|
|
|
if (fd < 0)
|
2016-08-06 14:17:33 +00:00
|
|
|
{
|
|
|
|
*fatal = TRUE;
|
|
|
|
g_warning ("Failed to create socket: %m");
|
|
|
|
return -1;
|
|
|
|
}
|
2012-01-07 22:21:32 +00:00
|
|
|
|
|
|
|
addr.sun_family = AF_LOCAL;
|
|
|
|
name_size = snprintf (addr.sun_path, sizeof addr.sun_path,
|
|
|
|
"%c/tmp/.X11-unix/X%d", 0, display);
|
|
|
|
size = offsetof (struct sockaddr_un, sun_path) + name_size;
|
|
|
|
if (bind (fd, (struct sockaddr *) &addr, size) < 0)
|
|
|
|
{
|
2016-08-06 14:17:33 +00:00
|
|
|
*fatal = errno != EADDRINUSE;
|
2014-04-01 23:57:55 +00:00
|
|
|
g_warning ("failed to bind to @%s: %m", addr.sun_path + 1);
|
2012-01-07 22:21:32 +00:00
|
|
|
close (fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen (fd, 1) < 0)
|
|
|
|
{
|
2016-08-06 14:17:33 +00:00
|
|
|
*fatal = errno != EADDRINUSE;
|
|
|
|
g_warning ("Failed to listen on abstract socket @%s: %m",
|
|
|
|
addr.sun_path + 1);
|
2012-01-07 22:21:32 +00:00
|
|
|
close (fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
bind_to_unix_socket (int display)
|
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t size, name_size;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = socket (PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
addr.sun_family = AF_LOCAL;
|
|
|
|
name_size = snprintf (addr.sun_path, sizeof addr.sun_path,
|
|
|
|
"/tmp/.X11-unix/X%d", display) + 1;
|
|
|
|
size = offsetof (struct sockaddr_un, sun_path) + name_size;
|
|
|
|
unlink (addr.sun_path);
|
|
|
|
if (bind (fd, (struct sockaddr *) &addr, size) < 0)
|
|
|
|
{
|
2014-04-01 23:57:55 +00:00
|
|
|
g_warning ("failed to bind to %s: %m\n", addr.sun_path);
|
2012-01-07 22:21:32 +00:00
|
|
|
close (fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-01-29 15:23:58 +00:00
|
|
|
if (listen (fd, 1) < 0)
|
|
|
|
{
|
2012-01-07 22:21:32 +00:00
|
|
|
unlink (addr.sun_path);
|
|
|
|
close (fd);
|
|
|
|
return -1;
|
2014-01-29 15:23:58 +00:00
|
|
|
}
|
2012-01-07 22:21:32 +00:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2013-08-12 07:46:07 +00:00
|
|
|
static void
|
2015-03-22 01:24:37 +00:00
|
|
|
xserver_died (GObject *source,
|
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
2013-08-12 07:46:07 +00:00
|
|
|
{
|
2015-03-22 01:24:37 +00:00
|
|
|
GSubprocess *proc = G_SUBPROCESS (source);
|
2018-04-12 18:03:03 +00:00
|
|
|
g_autoptr (GError) error = NULL;
|
2015-03-22 01:24:37 +00:00
|
|
|
|
2017-10-25 07:42:49 +00:00
|
|
|
if (!g_subprocess_wait_finish (proc, result, &error))
|
|
|
|
{
|
2018-04-12 18:06:01 +00:00
|
|
|
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_warning ("Failed to finish waiting for Xwayland: %s", error->message);
|
2017-10-25 07:42:49 +00:00
|
|
|
}
|
|
|
|
else if (!g_subprocess_get_successful (proc))
|
2018-04-12 18:06:01 +00:00
|
|
|
g_warning ("X Wayland crashed; exiting");
|
2013-08-12 07:46:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* For now we simply abort if we see the server exit.
|
|
|
|
*
|
|
|
|
* In the future X will only be loaded lazily for legacy X support
|
|
|
|
* but for now it's a hard requirement. */
|
2018-04-12 18:06:01 +00:00
|
|
|
g_warning ("Spurious exit of X Wayland server");
|
2013-08-12 07:46:07 +00:00
|
|
|
}
|
2018-04-12 18:06:01 +00:00
|
|
|
|
|
|
|
meta_exit (META_EXIT_ERROR);
|
2013-08-12 07:46:07 +00:00
|
|
|
}
|
|
|
|
|
2013-08-12 13:31:18 +00:00
|
|
|
static int
|
|
|
|
x_io_error (Display *display)
|
|
|
|
{
|
2018-04-12 18:06:01 +00:00
|
|
|
g_warning ("Connection to xwayland lost");
|
|
|
|
meta_exit (META_EXIT_ERROR);
|
2013-08-12 13:31:18 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-02 00:00:59 +00:00
|
|
|
static gboolean
|
|
|
|
choose_xdisplay (MetaXWaylandManager *manager)
|
2012-01-07 22:21:32 +00:00
|
|
|
{
|
|
|
|
int display = 0;
|
2015-04-23 14:41:34 +00:00
|
|
|
char *lock_file = NULL;
|
2016-08-06 14:17:33 +00:00
|
|
|
gboolean fatal = FALSE;
|
2012-01-07 22:21:32 +00:00
|
|
|
|
2015-03-16 14:57:44 +00:00
|
|
|
/* Hack to keep the unused Xwayland instance on
|
|
|
|
* the login screen from taking the prime :0 display
|
|
|
|
* number.
|
|
|
|
*/
|
|
|
|
if (g_getenv ("RUNNING_UNDER_GDM") != NULL)
|
|
|
|
display = 1024;
|
|
|
|
|
2012-01-07 22:21:32 +00:00
|
|
|
do
|
|
|
|
{
|
2015-04-23 14:41:34 +00:00
|
|
|
lock_file = create_lock_file (display, &display);
|
|
|
|
if (!lock_file)
|
2012-01-07 22:21:32 +00:00
|
|
|
{
|
2014-01-29 15:23:58 +00:00
|
|
|
g_warning ("Failed to create an X lock file");
|
|
|
|
return FALSE;
|
2012-01-07 22:21:32 +00:00
|
|
|
}
|
|
|
|
|
2016-08-06 14:17:33 +00:00
|
|
|
manager->abstract_fd = bind_to_abstract_socket (display, &fatal);
|
2014-02-04 00:37:23 +00:00
|
|
|
if (manager->abstract_fd < 0)
|
2012-01-07 22:21:32 +00:00
|
|
|
{
|
2015-04-23 14:41:34 +00:00
|
|
|
unlink (lock_file);
|
2012-01-07 22:21:32 +00:00
|
|
|
|
2016-08-06 14:17:33 +00:00
|
|
|
if (!fatal)
|
2012-01-07 22:21:32 +00:00
|
|
|
{
|
|
|
|
display++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
2016-08-06 14:17:33 +00:00
|
|
|
{
|
|
|
|
g_warning ("Failed to bind abstract socket");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-01-07 22:21:32 +00:00
|
|
|
}
|
|
|
|
|
2014-02-04 00:37:23 +00:00
|
|
|
manager->unix_fd = bind_to_unix_socket (display);
|
2016-08-06 14:16:41 +00:00
|
|
|
if (manager->unix_fd < 0)
|
2012-01-07 22:21:32 +00:00
|
|
|
{
|
2015-04-23 14:41:34 +00:00
|
|
|
unlink (lock_file);
|
2014-02-04 00:37:23 +00:00
|
|
|
close (manager->abstract_fd);
|
2018-02-02 15:35:26 +00:00
|
|
|
display++;
|
|
|
|
continue;
|
2012-01-07 22:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (1);
|
|
|
|
|
2014-02-04 00:37:23 +00:00
|
|
|
manager->display_index = display;
|
2014-04-01 23:38:05 +00:00
|
|
|
manager->display_name = g_strdup_printf (":%d", manager->display_index);
|
2015-04-23 14:41:34 +00:00
|
|
|
manager->lock_file = lock_file;
|
2012-01-07 22:21:32 +00:00
|
|
|
|
2014-04-02 00:00:59 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-04-02 14:22:01 +00:00
|
|
|
static void
|
|
|
|
xserver_finished_init (MetaXWaylandManager *manager)
|
|
|
|
{
|
|
|
|
/* At this point xwayland is all setup to start accepting
|
|
|
|
* connections so we can quit the transient initialization mainloop
|
|
|
|
* and unblock meta_wayland_init() to continue initializing mutter.
|
|
|
|
* */
|
|
|
|
g_main_loop_quit (manager->init_loop);
|
|
|
|
g_clear_pointer (&manager->init_loop, g_main_loop_unref);
|
|
|
|
}
|
|
|
|
|
2014-04-02 14:25:56 +00:00
|
|
|
static gboolean
|
2014-04-17 20:05:08 +00:00
|
|
|
on_displayfd_ready (int fd,
|
|
|
|
GIOCondition condition,
|
|
|
|
gpointer user_data)
|
2014-04-02 14:25:56 +00:00
|
|
|
{
|
|
|
|
MetaXWaylandManager *manager = user_data;
|
|
|
|
|
2014-04-17 20:05:08 +00:00
|
|
|
/* The server writes its display name to the displayfd
|
|
|
|
* socket when it's ready. We don't care about the data
|
|
|
|
* in the socket, just that it wrote something, since
|
|
|
|
* that means it's ready. */
|
2014-04-02 14:25:56 +00:00
|
|
|
xserver_finished_init (manager);
|
|
|
|
|
|
|
|
return G_SOURCE_REMOVE;
|
|
|
|
}
|
|
|
|
|
2014-04-02 00:00:59 +00:00
|
|
|
gboolean
|
|
|
|
meta_xwayland_start (MetaXWaylandManager *manager,
|
|
|
|
struct wl_display *wl_display)
|
|
|
|
{
|
2014-04-17 19:37:53 +00:00
|
|
|
int xwayland_client_fd[2];
|
2014-04-17 20:05:08 +00:00
|
|
|
int displayfd[2];
|
2015-04-23 14:26:38 +00:00
|
|
|
gboolean started = FALSE;
|
2015-03-31 18:47:34 +00:00
|
|
|
g_autoptr(GSubprocessLauncher) launcher = NULL;
|
2015-03-22 01:24:37 +00:00
|
|
|
GSubprocessFlags flags;
|
|
|
|
GError *error = NULL;
|
2014-04-02 00:00:59 +00:00
|
|
|
|
|
|
|
if (!choose_xdisplay (manager))
|
2015-04-23 14:26:38 +00:00
|
|
|
goto out;
|
2014-04-02 00:00:59 +00:00
|
|
|
|
2012-01-07 22:21:32 +00:00
|
|
|
/* We want xwayland to be a wayland client so we make a socketpair to setup a
|
|
|
|
* wayland protocol connection. */
|
2014-04-17 19:37:53 +00:00
|
|
|
if (socketpair (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, xwayland_client_fd) < 0)
|
2012-01-07 22:21:32 +00:00
|
|
|
{
|
2014-04-17 19:37:53 +00:00
|
|
|
g_warning ("xwayland_client_fd socketpair failed\n");
|
2015-04-23 14:26:38 +00:00
|
|
|
goto out;
|
2012-01-07 22:21:32 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 20:05:08 +00:00
|
|
|
if (socketpair (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, displayfd) < 0)
|
|
|
|
{
|
|
|
|
g_warning ("displayfd socketpair failed\n");
|
2015-04-23 14:26:38 +00:00
|
|
|
goto out;
|
2014-04-17 20:05:08 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 01:24:37 +00:00
|
|
|
/* xwayland, please. */
|
|
|
|
flags = G_SUBPROCESS_FLAGS_NONE;
|
2014-04-01 23:51:43 +00:00
|
|
|
|
2015-03-22 01:24:37 +00:00
|
|
|
if (getenv ("XWAYLAND_STFU"))
|
|
|
|
{
|
|
|
|
flags |= G_SUBPROCESS_FLAGS_STDOUT_SILENCE;
|
|
|
|
flags |= G_SUBPROCESS_FLAGS_STDERR_SILENCE;
|
2014-04-01 23:51:43 +00:00
|
|
|
}
|
2015-03-22 01:24:37 +00:00
|
|
|
|
|
|
|
launcher = g_subprocess_launcher_new (flags);
|
|
|
|
|
|
|
|
g_subprocess_launcher_take_fd (launcher, xwayland_client_fd[1], 3);
|
|
|
|
g_subprocess_launcher_take_fd (launcher, manager->abstract_fd, 4);
|
|
|
|
g_subprocess_launcher_take_fd (launcher, manager->unix_fd, 5);
|
|
|
|
g_subprocess_launcher_take_fd (launcher, displayfd[1], 6);
|
|
|
|
|
|
|
|
g_subprocess_launcher_setenv (launcher, "WAYLAND_SOCKET", "3", TRUE);
|
2017-10-25 07:42:49 +00:00
|
|
|
|
|
|
|
/* Use the -terminate parameter to ensure that Xwayland exits cleanly
|
|
|
|
* after the last client disconnects. Fortunately that includes the window
|
|
|
|
* manager so it won't exit prematurely either. This ensures that Xwayland
|
|
|
|
* won't try to reconnect and crash, leaving uninteresting core dumps. We do
|
|
|
|
* want core dumps from Xwayland but only if a real bug occurs...
|
|
|
|
*/
|
|
|
|
manager->proc = g_subprocess_launcher_spawn (launcher, &error,
|
|
|
|
XWAYLAND_PATH, manager->display_name,
|
|
|
|
"-rootless",
|
|
|
|
"-terminate",
|
2017-09-14 07:20:30 +00:00
|
|
|
"-accessx",
|
2017-10-25 07:42:49 +00:00
|
|
|
"-core",
|
|
|
|
"-listen", "4",
|
|
|
|
"-listen", "5",
|
|
|
|
"-displayfd", "6",
|
|
|
|
NULL);
|
|
|
|
if (!manager->proc)
|
2014-04-02 17:03:17 +00:00
|
|
|
{
|
2015-03-22 01:24:37 +00:00
|
|
|
g_error ("Failed to spawn Xwayland: %s", error->message);
|
2015-04-23 14:26:38 +00:00
|
|
|
goto out;
|
2014-04-02 17:03:17 +00:00
|
|
|
}
|
2014-04-01 23:51:43 +00:00
|
|
|
|
2017-10-25 07:42:49 +00:00
|
|
|
manager->xserver_died_cancellable = g_cancellable_new ();
|
|
|
|
g_subprocess_wait_async (manager->proc, manager->xserver_died_cancellable,
|
|
|
|
xserver_died, NULL);
|
2014-04-17 20:05:08 +00:00
|
|
|
g_unix_fd_add (displayfd[0], G_IO_IN, on_displayfd_ready, manager);
|
2014-04-17 19:37:53 +00:00
|
|
|
manager->client = wl_client_create (wl_display, xwayland_client_fd[0]);
|
2012-01-07 22:21:32 +00:00
|
|
|
|
2013-08-12 08:06:13 +00:00
|
|
|
/* We need to run a mainloop until we know xwayland has a binding
|
|
|
|
* for our xserver interface at which point we can assume it's
|
|
|
|
* ready to start accepting connections. */
|
2014-02-04 00:37:23 +00:00
|
|
|
manager->init_loop = g_main_loop_new (NULL, FALSE);
|
|
|
|
g_main_loop_run (manager->init_loop);
|
2013-08-12 08:06:13 +00:00
|
|
|
|
2015-04-23 14:26:38 +00:00
|
|
|
started = TRUE;
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (!started)
|
2015-04-23 14:29:30 +00:00
|
|
|
{
|
2015-04-23 14:41:34 +00:00
|
|
|
unlink (manager->lock_file);
|
|
|
|
g_clear_pointer (&manager->lock_file, g_free);
|
2015-04-23 14:29:30 +00:00
|
|
|
}
|
2015-04-23 14:26:38 +00:00
|
|
|
return started;
|
2012-01-07 22:21:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-30 07:04:48 +00:00
|
|
|
static void
|
|
|
|
on_x11_display_closing (MetaDisplay *display)
|
|
|
|
{
|
|
|
|
meta_xwayland_shutdown_selection ();
|
|
|
|
}
|
|
|
|
|
2013-08-12 13:31:18 +00:00
|
|
|
/* To be called right after connecting */
|
|
|
|
void
|
2018-06-30 07:04:48 +00:00
|
|
|
meta_xwayland_complete_init (MetaDisplay *display)
|
2013-08-12 13:31:18 +00:00
|
|
|
{
|
|
|
|
/* We install an X IO error handler in addition to the child watch,
|
|
|
|
because after Xlib connects our child watch may not be called soon
|
|
|
|
enough, and therefore we won't crash when X exits (and most important
|
|
|
|
we won't reset the tty).
|
|
|
|
*/
|
|
|
|
XSetIOErrorHandler (x_io_error);
|
2014-10-10 16:55:00 +00:00
|
|
|
|
2018-06-30 07:04:48 +00:00
|
|
|
g_signal_connect (display, "x11-display-closing",
|
|
|
|
G_CALLBACK (on_x11_display_closing), NULL);
|
2014-10-10 16:55:00 +00:00
|
|
|
meta_xwayland_init_selection ();
|
2013-08-12 13:31:18 +00:00
|
|
|
}
|
|
|
|
|
2012-01-07 22:21:32 +00:00
|
|
|
void
|
2014-02-04 00:37:23 +00:00
|
|
|
meta_xwayland_stop (MetaXWaylandManager *manager)
|
2012-01-07 22:21:32 +00:00
|
|
|
{
|
|
|
|
char path[256];
|
|
|
|
|
2017-10-25 07:42:49 +00:00
|
|
|
g_cancellable_cancel (manager->xserver_died_cancellable);
|
|
|
|
g_clear_object (&manager->proc);
|
|
|
|
g_clear_object (&manager->xserver_died_cancellable);
|
2014-10-10 16:55:00 +00:00
|
|
|
|
2014-02-04 00:37:23 +00:00
|
|
|
snprintf (path, sizeof path, "/tmp/.X11-unix/X%d", manager->display_index);
|
2012-01-07 22:21:32 +00:00
|
|
|
unlink (path);
|
|
|
|
|
2015-04-23 14:34:43 +00:00
|
|
|
g_clear_pointer (&manager->display_name, g_free);
|
|
|
|
if (manager->lock_file)
|
|
|
|
{
|
|
|
|
unlink (manager->lock_file);
|
|
|
|
g_clear_pointer (&manager->lock_file, g_free);
|
|
|
|
}
|
2012-01-07 22:21:32 +00:00
|
|
|
}
|
2015-07-08 08:14:00 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
xwayland_surface_assigned (MetaWaylandSurfaceRole *surface_role)
|
|
|
|
{
|
|
|
|
MetaWaylandSurface *surface =
|
|
|
|
meta_wayland_surface_role_get_surface (surface_role);
|
2017-12-20 11:07:40 +00:00
|
|
|
MetaWaylandSurfaceRoleClass *surface_role_class =
|
|
|
|
META_WAYLAND_SURFACE_ROLE_CLASS (meta_wayland_surface_role_xwayland_parent_class);
|
2015-07-08 08:14:00 +00:00
|
|
|
|
|
|
|
/* See comment in xwayland_surface_commit for why we reply even though the
|
|
|
|
* surface may not be drawn the next frame.
|
|
|
|
*/
|
|
|
|
wl_list_insert_list (&surface->compositor->frame_callbacks,
|
|
|
|
&surface->pending_frame_callback_list);
|
|
|
|
wl_list_init (&surface->pending_frame_callback_list);
|
2017-12-20 11:07:40 +00:00
|
|
|
|
|
|
|
surface_role_class->assigned (surface_role);
|
2015-07-08 08:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
xwayland_surface_commit (MetaWaylandSurfaceRole *surface_role,
|
|
|
|
MetaWaylandPendingState *pending)
|
|
|
|
{
|
|
|
|
MetaWaylandSurface *surface =
|
|
|
|
meta_wayland_surface_role_get_surface (surface_role);
|
2017-12-20 11:07:40 +00:00
|
|
|
MetaWaylandSurfaceRoleClass *surface_role_class =
|
|
|
|
META_WAYLAND_SURFACE_ROLE_CLASS (meta_wayland_surface_role_xwayland_parent_class);
|
2015-07-08 08:14:00 +00:00
|
|
|
|
|
|
|
/* For Xwayland windows, throttling frames when the window isn't actually
|
|
|
|
* drawn is less useful, because Xwayland still has to do the drawing sent
|
|
|
|
* from the application - the throttling would only be of sending us damage
|
|
|
|
* messages, so we simplify and send frame callbacks after the next paint of
|
|
|
|
* the screen, whether the window was drawn or not.
|
|
|
|
*
|
|
|
|
* Currently it may take a few frames before we draw the window, for not
|
|
|
|
* completely understood reasons, and in that case, not thottling frame
|
|
|
|
* callbacks to drawing has the happy side effect that we avoid showing the
|
|
|
|
* user the initial black frame from when the window is mapped empty.
|
|
|
|
*/
|
|
|
|
meta_wayland_surface_queue_pending_state_frame_callbacks (surface, pending);
|
2017-12-20 11:07:40 +00:00
|
|
|
|
|
|
|
surface_role_class->commit (surface_role, pending);
|
2015-07-08 08:14:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 09:43:47 +00:00
|
|
|
static MetaWaylandSurface *
|
|
|
|
xwayland_surface_get_toplevel (MetaWaylandSurfaceRole *surface_role)
|
|
|
|
{
|
|
|
|
return meta_wayland_surface_role_get_surface (surface_role);
|
|
|
|
}
|
|
|
|
|
2015-07-08 08:14:00 +00:00
|
|
|
static void
|
|
|
|
meta_wayland_surface_role_xwayland_init (MetaWaylandSurfaceRoleXWayland *role)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
meta_wayland_surface_role_xwayland_class_init (MetaWaylandSurfaceRoleXWaylandClass *klass)
|
|
|
|
{
|
|
|
|
MetaWaylandSurfaceRoleClass *surface_role_class =
|
|
|
|
META_WAYLAND_SURFACE_ROLE_CLASS (klass);
|
|
|
|
|
|
|
|
surface_role_class->assigned = xwayland_surface_assigned;
|
|
|
|
surface_role_class->commit = xwayland_surface_commit;
|
2016-01-14 09:43:47 +00:00
|
|
|
surface_role_class->get_toplevel = xwayland_surface_get_toplevel;
|
2016-04-01 12:57:19 +00:00
|
|
|
|
|
|
|
xwayland_surface_signals[XWAYLAND_SURFACE_WINDOW_ASSOCIATED] =
|
|
|
|
g_signal_new ("window-associated",
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
0, NULL, NULL,
|
|
|
|
g_cclosure_marshal_VOID__VOID,
|
|
|
|
G_TYPE_NONE, 0);
|
2015-07-08 08:14:00 +00:00
|
|
|
}
|