shew: Add small library for dealing with external windows

In order to support OpenExtensionPrefs()'s parentWindow parameter,
we will need the ability to make a window transient to an external
window identified by a string handle.

This takes the corresponding code from xdg-desktop-portal-gtk and
brings it into an introspectable form, likewise the counterpart in
libportal to export a string handle for a GtkWindow.

https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/1087
This commit is contained in:
Florian Müllner
2020-03-05 04:52:09 +01:00
parent f971b8426d
commit 6baf490aab
14 changed files with 1364 additions and 0 deletions

View File

@ -0,0 +1,29 @@
shew_public_headers = files(
'shew-external-window.h',
'shew-window-exporter.h',
)
shew_sources = [
'shew-external-window-wayland.c',
'shew-external-window-x11.c',
'shew-external-window.c',
'shew-window-exporter.c',
]
libshew = library(full_name,
sources: shew_sources,
dependencies: [gtk_dep],
install_dir: pkglibdir,
install: true,
)
libshew_gir = gnome.generate_gir(libshew,
sources: shew_sources + shew_public_headers,
nsversion: api_version,
namespace: 'Shew',
includes: ['Gdk-3.0', 'Gtk-3.0'],
extra_args: ['--quiet'],
install_dir_gir: girdir,
install_dir_typelib: typelibdir,
install: true,
)

View File

@ -0,0 +1,117 @@
/*
* Copyright © 2016 Red Hat, Inc
*
* This program 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
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Jonas Ådahl <jadahl@redhat.com>
*/
#include <gdk/gdk.h>
#ifdef GDK_WINDOWING_WAYLAND
#include <gdk/gdkwayland.h>
#endif
#include "shew-external-window-wayland.h"
static GdkDisplay *wayland_display;
struct _ShewExternalWindowWayland
{
ShewExternalWindow parent;
char *handle_str;
};
G_DEFINE_TYPE (ShewExternalWindowWayland, shew_external_window_wayland,
SHEW_TYPE_EXTERNAL_WINDOW)
static GdkDisplay *
get_wayland_display (void)
{
if (wayland_display)
return wayland_display;
gdk_set_allowed_backends ("wayland");
wayland_display = gdk_display_open (NULL);
gdk_set_allowed_backends (NULL);
if (!wayland_display)
g_warning ("Failed to open Wayland display");
return wayland_display;
}
ShewExternalWindowWayland *
shew_external_window_wayland_new (const char *handle_str)
{
ShewExternalWindowWayland *external_window_wayland;
GdkDisplay *display;
display = get_wayland_display ();
if (!display)
{
g_warning ("No Wayland display connection, ignoring Wayland parent");
return NULL;
}
external_window_wayland = g_object_new (SHEW_TYPE_EXTERNAL_WINDOW_WAYLAND,
"display", display,
NULL);
external_window_wayland->handle_str = g_strdup (handle_str);
return external_window_wayland;
}
static void
shew_external_window_wayland_set_parent_of (ShewExternalWindow *external_window,
GdkWindow *child_window)
{
ShewExternalWindowWayland *external_window_wayland =
SHEW_EXTERNAL_WINDOW_WAYLAND (external_window);
char *handle_str = external_window_wayland->handle_str;
#ifdef GDK_WINDOWING_WAYLAND
if (!gdk_wayland_window_set_transient_for_exported (child_window, handle_str))
g_warning ("Failed to set portal window transient for external parent");
#endif
}
static void
shew_external_window_wayland_dispose (GObject *object)
{
ShewExternalWindowWayland *external_window_wayland =
SHEW_EXTERNAL_WINDOW_WAYLAND (object);
g_free (external_window_wayland->handle_str);
G_OBJECT_CLASS (shew_external_window_wayland_parent_class)->dispose (object);
}
static void
shew_external_window_wayland_init (ShewExternalWindowWayland *external_window_wayland)
{
}
static void
shew_external_window_wayland_class_init (ShewExternalWindowWaylandClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
ShewExternalWindowClass *external_window_class = SHEW_EXTERNAL_WINDOW_CLASS (klass);
object_class->dispose = shew_external_window_wayland_dispose;
external_window_class->set_parent_of = shew_external_window_wayland_set_parent_of;
}

View File

@ -0,0 +1,30 @@
/*
* Copyright © 2016 Red Hat, Inc
*
* This program 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
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Jonas Ådahl <jadahl@redhat.com>
*/
#pragma once
#include <glib-object.h>
#include "shew-external-window.h"
#define SHEW_TYPE_EXTERNAL_WINDOW_WAYLAND (shew_external_window_wayland_get_type ())
G_DECLARE_FINAL_TYPE (ShewExternalWindowWayland, shew_external_window_wayland, SHEW, EXTERNAL_WINDOW_WAYLAND, ShewExternalWindow)
ShewExternalWindowWayland *shew_external_window_wayland_new (const char *handle_str);

View File

@ -0,0 +1,133 @@
/*
* Copyright © 2016 Red Hat, Inc
*
* This program 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
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Jonas Ådahl <jadahl@redhat.com>
*/
#include <errno.h>
#include <gdk/gdk.h>
#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#endif
#include <stdlib.h>
#include "shew-external-window-x11.h"
static GdkDisplay *x11_display;
struct _ShewExternalWindowX11
{
ShewExternalWindow parent;
GdkWindow *foreign_gdk_window;
};
G_DEFINE_TYPE (ShewExternalWindowX11, shew_external_window_x11,
SHEW_TYPE_EXTERNAL_WINDOW)
static GdkDisplay *
get_x11_display (void)
{
if (x11_display)
return x11_display;
gdk_set_allowed_backends ("x11");
x11_display = gdk_display_open (NULL);
gdk_set_allowed_backends (NULL);
if (!x11_display)
g_warning ("Failed to open X11 display");
return x11_display;
}
ShewExternalWindowX11 *
shew_external_window_x11_new (const char *handle_str)
{
ShewExternalWindowX11 *external_window_x11;
GdkDisplay *display;
int xid;
GdkWindow *foreign_gdk_window = NULL;
display = get_x11_display ();
if (!display)
{
g_warning ("No X display connection, ignoring X11 parent");
return NULL;
}
errno = 0;
xid = strtol (handle_str, NULL, 16);
if (errno != 0)
{
g_warning ("Failed to reference external X11 window, invalid XID %s", handle_str);
return NULL;
}
#ifdef GDK_WINDOWING_X11
foreign_gdk_window = gdk_x11_window_foreign_new_for_display (display, xid);
#endif
if (!foreign_gdk_window)
{
g_warning ("Failed to create foreign window for XID %d", xid);
return NULL;
}
external_window_x11 = g_object_new (SHEW_TYPE_EXTERNAL_WINDOW_X11,
"display", display,
NULL);
external_window_x11->foreign_gdk_window = foreign_gdk_window;
return external_window_x11;
}
static void
shew_external_window_x11_set_parent_of (ShewExternalWindow *external_window,
GdkWindow *child_window)
{
ShewExternalWindowX11 *external_window_x11 =
SHEW_EXTERNAL_WINDOW_X11 (external_window);
gdk_window_set_transient_for (child_window,
external_window_x11->foreign_gdk_window);
}
static void
shew_external_window_x11_dispose (GObject *object)
{
ShewExternalWindowX11 *external_window_x11 = SHEW_EXTERNAL_WINDOW_X11 (object);
g_clear_object (&external_window_x11->foreign_gdk_window);
G_OBJECT_CLASS (shew_external_window_x11_parent_class)->dispose (object);
}
static void
shew_external_window_x11_init (ShewExternalWindowX11 *external_window_x11)
{
}
static void
shew_external_window_x11_class_init (ShewExternalWindowX11Class *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
ShewExternalWindowClass *external_window_class = SHEW_EXTERNAL_WINDOW_CLASS (klass);
object_class->dispose = shew_external_window_x11_dispose;
external_window_class->set_parent_of = shew_external_window_x11_set_parent_of;
}

View File

@ -0,0 +1,30 @@
/*
* Copyright © 2016 Red Hat, Inc
*
* This program 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
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Jonas Ådahl <jadahl@redhat.com>
*/
#pragma once
#include <glib-object.h>
#include "shew-external-window.h"
#define SHEW_TYPE_EXTERNAL_WINDOW_X11 (shew_external_window_x11_get_type ())
G_DECLARE_FINAL_TYPE (ShewExternalWindowX11, shew_external_window_x11, SHEW, EXTERNAL_WINDOW_X11, ShewExternalWindow)
ShewExternalWindowX11 *shew_external_window_x11_new (const char *handle_str);

View File

@ -0,0 +1,161 @@
/*
* Copyright © 2016 Red Hat, Inc
*
* This program 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
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Jonas Ådahl <jadahl@redhat.com>
*/
#include <string.h>
#include "shew-external-window.h"
#include "shew-external-window-x11.h"
#include "shew-external-window-wayland.h"
enum
{
PROP_0,
PROP_DISPLAY,
};
typedef struct _ShewExternalWindowPrivate
{
GdkDisplay *display;
} ShewExternalWindowPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (ShewExternalWindow, shew_external_window, G_TYPE_OBJECT)
ShewExternalWindow *
shew_external_window_new_from_handle (const char *handle_str)
{
#ifdef GDK_WINDOWING_X11
{
const char x11_prefix[] = "x11:";
if (g_str_has_prefix (handle_str, x11_prefix))
{
ShewExternalWindowX11 *external_window_x11;
const char *x11_handle_str = handle_str + strlen (x11_prefix);
external_window_x11 = shew_external_window_x11_new (x11_handle_str);
return SHEW_EXTERNAL_WINDOW (external_window_x11);
}
}
#endif
#ifdef GDK_WINDOWING_WAYLAND
{
const char wayland_prefix[] = "wayland:";
if (g_str_has_prefix (handle_str, wayland_prefix))
{
ShewExternalWindowWayland *external_window_wayland;
const char *wayland_handle_str = handle_str + strlen (wayland_prefix);
external_window_wayland =
shew_external_window_wayland_new (wayland_handle_str);
return SHEW_EXTERNAL_WINDOW (external_window_wayland);
}
}
#endif
g_warning ("Unhandled parent window type %s\n", handle_str);
return NULL;
}
void
shew_external_window_set_parent_of (ShewExternalWindow *external_window,
GdkWindow *child_window)
{
SHEW_EXTERNAL_WINDOW_GET_CLASS (external_window)->set_parent_of (external_window,
child_window);
}
/**
* shew_external_window_get_display:
* Returns: (transfer none)
*/
GdkDisplay *
shew_external_window_get_display (ShewExternalWindow *external_window)
{
ShewExternalWindowPrivate *priv =
shew_external_window_get_instance_private (external_window);
return priv->display;
}
static void
shew_external_window_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
ShewExternalWindow *external_window = SHEW_EXTERNAL_WINDOW (object);
ShewExternalWindowPrivate *priv =
shew_external_window_get_instance_private (external_window);
switch (prop_id)
{
case PROP_DISPLAY:
g_set_object (&priv->display, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
shew_external_window_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
ShewExternalWindow *external_window = SHEW_EXTERNAL_WINDOW (object);
ShewExternalWindowPrivate *priv =
shew_external_window_get_instance_private (external_window);
switch (prop_id)
{
case PROP_DISPLAY:
g_value_set_object (value, priv->display);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
shew_external_window_init (ShewExternalWindow *external_window)
{
}
static void
shew_external_window_class_init (ShewExternalWindowClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->get_property = shew_external_window_get_property;
object_class->set_property = shew_external_window_set_property;
g_object_class_install_property (object_class,
PROP_DISPLAY,
g_param_spec_object ("display",
"GdkDisplay",
"The GdkDisplay instance",
GDK_TYPE_DISPLAY,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
}

View File

@ -0,0 +1,43 @@
/*
* Copyright © 2016 Red Hat, Inc
*
* This program 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
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Jonas Ådahl <jadahl@redhat.com>
*/
#pragma once
#include <glib-object.h>
#include <gtk/gtk.h>
#define SHEW_TYPE_EXTERNAL_WINDOW (shew_external_window_get_type ())
G_DECLARE_DERIVABLE_TYPE (ShewExternalWindow, shew_external_window, SHEW, EXTERNAL_WINDOW, GObject)
struct _ShewExternalWindowClass
{
GObjectClass parent_class;
void (*set_parent_of) (ShewExternalWindow *external_window,
GdkWindow *child_window);
};
ShewExternalWindow *shew_external_window_new_from_handle (const char *handle_str);
void shew_external_window_set_parent_of (ShewExternalWindow *external_window,
GdkWindow *child_window);
GdkDisplay *shew_external_window_get_display (ShewExternalWindow *external_window);

View File

@ -0,0 +1,216 @@
/*
* Copyright © 2020 Red Hat, Inc
*
* This program 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
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Florian Müllner <fmuellner@gnome.org>
*/
#include "shew-window-exporter.h"
#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#endif
#ifdef GDK_WINDOWING_WAYLAND
#include <gdk/gdkwayland.h>
#endif
struct _ShewWindowExporter
{
GObject parent;
GtkWindow *window;
};
G_DEFINE_TYPE (ShewWindowExporter, shew_window_exporter, G_TYPE_OBJECT)
enum
{
PROP_0,
PROP_WINDOW,
};
ShewWindowExporter *
shew_window_exporter_new (GtkWindow *window)
{
return g_object_new (SHEW_TYPE_WINDOW_EXPORTER,
"window", window,
NULL);
}
#ifdef GDK_WINDOWING_WAYLAND
static void
wayland_window_exported (GdkWindow *window,
const char *handle,
gpointer user_data)
{
g_autoptr (GTask) task = user_data;
g_task_return_pointer (task, g_strdup_printf ("wayland:%s", handle), g_free);
}
#endif
void
shew_window_exporter_export (ShewWindowExporter *exporter,
GAsyncReadyCallback callback,
gpointer user_data)
{
g_autoptr (GTask) task = NULL;
GtkWidget *widget;
g_return_if_fail (SHEW_IS_WINDOW_EXPORTER (exporter));
if (exporter->window == NULL)
{
g_task_report_new_error (exporter, callback, user_data,
shew_window_exporter_export,
G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"No window to export");
return;
}
task = g_task_new (exporter, NULL, callback, user_data);
g_task_set_source_tag (task, shew_window_exporter_export);
widget = GTK_WIDGET (exporter->window);
#ifdef GDK_WINDOWING_X11
if (GDK_IS_X11_DISPLAY (gtk_widget_get_display (widget)))
{
GdkWindow *w = gtk_widget_get_window (widget);
guint32 xid = (guint32) gdk_x11_window_get_xid (w);
g_task_return_pointer (task, g_strdup_printf ("x11:%x", xid), g_free);
}
#endif
#ifdef GDK_WINDOWING_WAYLAND
if (GDK_IS_WAYLAND_DISPLAY (gtk_widget_get_display (widget)))
{
GdkWindow *w = gtk_widget_get_window (widget);
gdk_wayland_window_export_handle (w, wayland_window_exported,
g_steal_pointer (&task), NULL);
}
#endif
if (task != NULL && !g_task_get_completed (task))
{
g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
"Unsupported windowing system");
}
}
char *
shew_window_exporter_export_finish (ShewWindowExporter *exporter,
GAsyncResult *result,
GError **error)
{
g_return_val_if_fail (SHEW_IS_WINDOW_EXPORTER (exporter), NULL);
g_return_val_if_fail (g_async_result_is_tagged (result, shew_window_exporter_export), NULL);
return g_task_propagate_pointer (G_TASK (result), error);
}
void
shew_window_exporter_unexport (ShewWindowExporter *exporter)
{
GtkWidget *widget;
g_return_if_fail (SHEW_IS_WINDOW_EXPORTER (exporter));
widget = GTK_WIDGET (exporter->window);
#ifdef GDK_WINDOWING_WAYLAND
if (GDK_IS_WAYLAND_DISPLAY (gtk_widget_get_display (widget)))
{
GdkWindow *w = gtk_widget_get_window (widget);
gdk_wayland_window_unexport_handle (w);
}
#endif
}
static void
shew_window_exporter_dispose (GObject *object)
{
ShewWindowExporter *exporter = SHEW_WINDOW_EXPORTER (object);
g_clear_object (&exporter->window);
G_OBJECT_CLASS (shew_window_exporter_parent_class)->dispose (object);
}
static void
shew_window_exporter_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
ShewWindowExporter *exporter = SHEW_WINDOW_EXPORTER (object);
switch (prop_id)
{
case PROP_WINDOW:
g_set_object (&exporter->window, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
shew_window_exporter_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
ShewWindowExporter *exporter = SHEW_WINDOW_EXPORTER (object);
switch (prop_id)
{
case PROP_WINDOW:
g_value_set_object (value, exporter->window);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
shew_window_exporter_init (ShewWindowExporter *exporter)
{
}
static void
shew_window_exporter_class_init (ShewWindowExporterClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->get_property = shew_window_exporter_get_property;
object_class->set_property = shew_window_exporter_set_property;
object_class->dispose = shew_window_exporter_dispose;
g_object_class_install_property (object_class,
PROP_WINDOW,
g_param_spec_object ("window",
"GtkWindow",
"The GtkWindow to export",
GTK_TYPE_WINDOW,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
}

View File

@ -0,0 +1,38 @@
/*
* Copyright © 2020 Red Hat, Inc
*
* This program 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
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Florian Müllner <fmuellner@gnome.org>
*/
#pragma once
#include <gtk/gtk.h>
#define SHEW_TYPE_WINDOW_EXPORTER (shew_window_exporter_get_type ())
G_DECLARE_FINAL_TYPE (ShewWindowExporter, shew_window_exporter, SHEW, WINDOW_EXPORTER, GObject)
ShewWindowExporter *shew_window_exporter_new (GtkWindow *window);
void shew_window_exporter_export (ShewWindowExporter *exporter,
GAsyncReadyCallback callback,
gpointer user_data);
char *shew_window_exporter_export_finish (ShewWindowExporter *exporter,
GAsyncResult *result,
GError **error);
void shew_window_exporter_unexport (ShewWindowExporter *exporter);