summaryrefslogtreecommitdiffstats
path: root/subprojects/shew/src/shew-external-window-x11.c
blob: 5c4c8e69e82562196c203e757119633ec975b9d9 (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
/*
 * 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;
}