mirror of
https://gitlab.gnome.org/GNOME/gtk.git
synced 2025-01-16 23:24:16 +00:00
524ce7f88e
This will prevent us opening, closing and reopening the same display connection in the majority of cases. https://bugzilla.gnome.org/show_bug.cgi?id=694465
172 lines
5.0 KiB
C
172 lines
5.0 KiB
C
/* GDK - The GIMP Drawing Kit
|
|
* gdkdisplaymanager-wayland.c
|
|
*
|
|
* Copyright 2010 Red Hat, Inc.
|
|
*
|
|
* Author: Matthias clasen
|
|
*
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
#include "gdkdisplaymanagerprivate.h"
|
|
#include "gdkdisplay-wayland.h"
|
|
#include "gdkprivate-wayland.h"
|
|
#include "gdkwayland.h"
|
|
#include "gdkinternals.h"
|
|
|
|
struct _GdkWaylandDisplayManager
|
|
{
|
|
GdkDisplayManager parent;
|
|
|
|
GSList *displays;
|
|
|
|
gboolean init_failed;
|
|
struct {
|
|
struct wl_display *wl_display;
|
|
gchar *name;
|
|
} cached_display;
|
|
};
|
|
|
|
struct _GdkWaylandDisplayManagerClass
|
|
{
|
|
GdkDisplayManagerClass parent_class;
|
|
};
|
|
|
|
static void g_initable_iface_init (GInitableIface *iface);
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GdkWaylandDisplayManager, gdk_wayland_display_manager, GDK_TYPE_DISPLAY_MANAGER,
|
|
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, g_initable_iface_init))
|
|
|
|
static gboolean
|
|
gdk_wayland_display_manager_initable_init (GInitable *initable,
|
|
GCancellable *cancellable,
|
|
GError **error)
|
|
{
|
|
GdkWaylandDisplayManager *manager = GDK_WAYLAND_DISPLAY_MANAGER (initable);
|
|
struct wl_display *wl_display;
|
|
|
|
/* Set by the compositor when launching a special client - and it gets reset
|
|
* by wl_display_connect so we must avoid calling it twice
|
|
*/
|
|
if (g_getenv ("WAYLAND_SOCKET"))
|
|
return TRUE;
|
|
|
|
/* check that a connection to the default display is possible */
|
|
wl_display = wl_display_connect (gdk_get_display_arg_name ());
|
|
|
|
if (!wl_display)
|
|
{
|
|
manager->init_failed = TRUE;
|
|
return FALSE;
|
|
}
|
|
|
|
manager->cached_display.name = g_strdup (gdk_get_display_arg_name ());
|
|
manager->cached_display.wl_display = wl_display;
|
|
return TRUE;
|
|
}
|
|
|
|
void
|
|
g_initable_iface_init (GInitableIface *iface)
|
|
{
|
|
iface->init = gdk_wayland_display_manager_initable_init;
|
|
}
|
|
|
|
static void
|
|
gdk_wayland_display_manager_finalize (GObject *object)
|
|
{
|
|
if (GDK_WAYLAND_DISPLAY_MANAGER (object)->init_failed == FALSE)
|
|
g_error ("A GdkWaylandDisplayManager object was finalized. This should not happen");
|
|
|
|
G_OBJECT_CLASS (gdk_wayland_display_manager_parent_class)->finalize (object);
|
|
}
|
|
|
|
static GdkDisplay *
|
|
gdk_wayland_display_manager_open_display (GdkDisplayManager *display_manager,
|
|
const gchar *name)
|
|
{
|
|
GdkWaylandDisplayManager *manager = GDK_WAYLAND_DISPLAY_MANAGER (display_manager);
|
|
struct wl_display *wl_display;
|
|
|
|
if (name == manager->cached_display.name ||
|
|
g_strcmp0 (name, manager->cached_display.name))
|
|
{
|
|
/* Cache hit */
|
|
wl_display = manager->cached_display.wl_display;
|
|
}
|
|
|
|
if (!wl_display)
|
|
{
|
|
/* Cache miss */
|
|
if (manager->cached_display.wl_display)
|
|
wl_display_disconnect (manager->cached_display.wl_display);
|
|
wl_display = wl_display_connect (name);
|
|
}
|
|
|
|
/* Use the cache at most once */
|
|
g_free (manager->cached_display.name);
|
|
manager->cached_display.wl_display = NULL;
|
|
|
|
if (!wl_display)
|
|
return NULL;
|
|
|
|
return _gdk_wayland_display_open (wl_display);
|
|
}
|
|
|
|
static void
|
|
gdk_wayland_display_manager_class_init (GdkWaylandDisplayManagerClass *class)
|
|
{
|
|
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
|
GdkDisplayManagerClass *manager_class = GDK_DISPLAY_MANAGER_CLASS (class);
|
|
|
|
object_class->finalize = gdk_wayland_display_manager_finalize;
|
|
|
|
manager_class->open_display = gdk_wayland_display_manager_open_display;
|
|
}
|
|
|
|
static void
|
|
gdk_wayland_display_manager_init (GdkWaylandDisplayManager *manager)
|
|
{
|
|
}
|
|
|
|
void
|
|
_gdk_wayland_display_manager_add_display (GdkDisplayManager *manager,
|
|
GdkDisplay *display)
|
|
{
|
|
GdkWaylandDisplayManager *manager_wayland = GDK_WAYLAND_DISPLAY_MANAGER (manager);
|
|
|
|
if (manager_wayland->displays == NULL)
|
|
gdk_display_manager_set_default_display (manager, display);
|
|
|
|
manager_wayland->displays = g_slist_prepend (manager_wayland->displays, display);
|
|
}
|
|
|
|
void
|
|
_gdk_wayland_display_manager_remove_display (GdkDisplayManager *manager,
|
|
GdkDisplay *display)
|
|
{
|
|
GdkWaylandDisplayManager *manager_wayland = GDK_WAYLAND_DISPLAY_MANAGER (manager);
|
|
|
|
manager_wayland->displays = g_slist_remove (manager_wayland->displays, display);
|
|
|
|
if (gdk_display_manager_get_default_display (manager) == display)
|
|
{
|
|
if (manager_wayland->displays)
|
|
gdk_display_manager_set_default_display (manager, manager_wayland->displays->data);
|
|
else
|
|
gdk_display_manager_set_default_display (manager, NULL);
|
|
}
|
|
}
|