summaryrefslogtreecommitdiffstats
path: root/subprojects/shew/src/shew-window-exporter.c
blob: ab84bf8a95f71b146cd03a2b67322d754200c542 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * 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/x11/gdkx.h>
#endif
#ifdef GDK_WINDOWING_WAYLAND
#include <gdk/wayland/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 (GdkToplevel *toplevel,
                         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)))
    {
      GdkSurface *s = gtk_native_get_surface (GTK_NATIVE (widget));
      guint32 xid = (guint32) gdk_x11_surface_get_xid (s);

      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)))
    {
      GdkSurface *s = gtk_native_get_surface (GTK_NATIVE (widget));
      gdk_wayland_toplevel_export_handle (GDK_WAYLAND_TOPLEVEL (s),
                                          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)))
    {
      GdkSurface *s = gtk_native_get_surface (GTK_NATIVE (widget));
      gdk_wayland_toplevel_unexport_handle (GDK_WAYLAND_TOPLEVEL (s));
    }
#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));
}