summaryrefslogtreecommitdiffstats
path: root/gfx/gtk_native_view_id_manager.cc
blob: 680ec135ad1354eb2212c1580b2c597285710841 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gfx/gtk_native_view_id_manager.h"

#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <gdk/gdk.h>
#include <X11/Xlib.h>

#include "base/logging.h"
#include "base/rand_util.h"
#include "gfx/rect.h"

// -----------------------------------------------------------------------------
// Bounce functions for GTK to callback into a C++ object...
namespace {

void OnRealize(gfx::NativeView widget, void* arg) {
  GtkNativeViewManager* manager = reinterpret_cast<GtkNativeViewManager*>(arg);
  manager->OnRealize(widget);
}

void OnUnrealize(gfx::NativeView widget, void *arg) {
  GtkNativeViewManager* manager = reinterpret_cast<GtkNativeViewManager*>(arg);
  manager->OnUnrealize(widget);
}

void OnDestroy(GtkObject* obj, void* arg) {
  GtkNativeViewManager* manager = reinterpret_cast<GtkNativeViewManager*>(arg);
  manager->OnDestroy(reinterpret_cast<GtkWidget*>(obj));
}

void OnSizeAllocate(gfx::NativeView widget,
                    GtkAllocation* alloc,
                    void* arg) {
  GtkNativeViewManager* manager = reinterpret_cast<GtkNativeViewManager*>(arg);
  manager->OnSizeAllocate(widget, alloc);
}

XID CreateOverlay(XID underlying) {
  XID overlay = 0;
  Display* dpy = gdk_x11_get_default_xdisplay();

  // Prevent interference with reparenting/mapping of overlay
  XSetWindowAttributes attr;
  attr.override_redirect = True;

  if (underlying) {
    XID root;
    int x, y;
    unsigned int width, height;
    unsigned int border_width;
    unsigned int depth;
    XGetGeometry(
        dpy, underlying, &root, &x, &y,
        &width, &height, &border_width, &depth);
    overlay = XCreateWindow(
        dpy,                      // display
        underlying,               // parent
        0, 0, width, height,      // x, y, width, height
        0,                        // border width
        CopyFromParent,           // depth
        InputOutput,              // class
        CopyFromParent,           // visual
        CWOverrideRedirect,       // value mask
        &attr);                   // value attributes
    XMapWindow(dpy, overlay);
  } else {
    overlay = XCreateWindow(
        dpy,                                 // display
        gdk_x11_get_default_root_xwindow(),  // parent
        0, 0, 1, 1,                          // x, y, width, height
        0,                                   // border width
        CopyFromParent,                      // depth
        InputOutput,                         // class
        CopyFromParent,                      // visual
        CWOverrideRedirect,                  // value mask
        &attr);                              // value attributes
  }
  XSync(dpy, False);
  return overlay;
}

}

// -----------------------------------------------------------------------------


// -----------------------------------------------------------------------------
// Public functions...

GtkNativeViewManager::GtkNativeViewManager() {
}

GtkNativeViewManager::~GtkNativeViewManager() {
}

gfx::NativeViewId GtkNativeViewManager::GetIdForWidget(gfx::NativeView widget) {
  // This is just for unit tests:
  if (!widget)
    return 0;

  AutoLock locked(lock_);

  std::map<gfx::NativeView, gfx::NativeViewId>::const_iterator i =
    native_view_to_id_.find(widget);

  if (i != native_view_to_id_.end())
    return i->second;

  gfx::NativeViewId new_id =
      static_cast<gfx::NativeViewId>(base::RandUint64());
  while (id_to_info_.find(new_id) != id_to_info_.end())
    new_id = static_cast<gfx::NativeViewId>(base::RandUint64());

  NativeViewInfo info;
  if (GTK_WIDGET_REALIZED(widget)) {
    GdkWindow *gdk_window = widget->window;
    CHECK(gdk_window);
    info.x_window_id = GDK_WINDOW_XID(gdk_window);
  }
  native_view_to_id_[widget] = new_id;
  id_to_info_[new_id] = info;

  g_signal_connect(widget, "realize", G_CALLBACK(::OnRealize), this);
  g_signal_connect(widget, "unrealize", G_CALLBACK(::OnUnrealize), this);
  g_signal_connect(widget, "destroy", G_CALLBACK(::OnDestroy), this);
  g_signal_connect(
      widget, "size-allocate", G_CALLBACK(::OnSizeAllocate), this);

  return new_id;
}

bool GtkNativeViewManager::GetXIDForId(XID* output, gfx::NativeViewId id) {
  AutoLock locked(lock_);

  std::map<gfx::NativeViewId, NativeViewInfo>::const_iterator i =
    id_to_info_.find(id);

  if (i == id_to_info_.end())
    return false;

  *output = i->second.x_window_id;
  return true;
}

bool GtkNativeViewManager::GetPermanentXIDForId(XID* output,
                                                gfx::NativeViewId id) {
  AutoLock locked(lock_);

  std::map<gfx::NativeViewId, NativeViewInfo>::iterator i =
    id_to_info_.find(id);

  if (i == id_to_info_.end())
    return false;

  if (!i->second.permanent_window_id) {
    i->second.permanent_window_id = CreateOverlay(i->second.x_window_id);
  }

  *output = i->second.permanent_window_id;
  return true;
}

// -----------------------------------------------------------------------------


// -----------------------------------------------------------------------------
// Private functions...

gfx::NativeViewId GtkNativeViewManager::GetWidgetId(gfx::NativeView widget) {
  lock_.AssertAcquired();

  std::map<gfx::NativeView, gfx::NativeViewId>::const_iterator i =
    native_view_to_id_.find(widget);

  CHECK(i != native_view_to_id_.end());
  return i->second;
}

void GtkNativeViewManager::OnRealize(gfx::NativeView widget) {
  AutoLock locked(lock_);

  const gfx::NativeViewId id = GetWidgetId(widget);
  std::map<gfx::NativeViewId, NativeViewInfo>::iterator i =
    id_to_info_.find(id);

  CHECK(i != id_to_info_.end());
  CHECK(widget->window);

  i->second.x_window_id = GDK_WINDOW_XID(widget->window);

  if (i->second.permanent_window_id) {
    Display* dpy = gdk_x11_get_default_xdisplay();
    XReparentWindow(
        dpy, i->second.permanent_window_id, i->second.x_window_id, 0, 0);
    XMapWindow(dpy, i->second.permanent_window_id);
    XSync(dpy, False);
  }
}

void GtkNativeViewManager::OnUnrealize(gfx::NativeView widget) {
  AutoLock unrealize_locked(unrealize_lock_);
  AutoLock locked(lock_);

  const gfx::NativeViewId id = GetWidgetId(widget);
  std::map<gfx::NativeViewId, NativeViewInfo>::iterator i =
    id_to_info_.find(id);

  CHECK(i != id_to_info_.end());

  i->second.x_window_id = 0;
  if (i->second.permanent_window_id) {
    Display* dpy = gdk_x11_get_default_xdisplay();
    XUnmapWindow(dpy, i->second.permanent_window_id);
    XReparentWindow(dpy, i->second.permanent_window_id,
                    gdk_x11_get_default_root_xwindow(), 0, 0);
    XSync(dpy, False);
  }
}

void GtkNativeViewManager::OnDestroy(gfx::NativeView widget) {
  AutoLock locked(lock_);

  std::map<gfx::NativeView, gfx::NativeViewId>::iterator i =
    native_view_to_id_.find(widget);
  CHECK(i != native_view_to_id_.end());

  std::map<gfx::NativeViewId, NativeViewInfo>::iterator j =
    id_to_info_.find(i->second);
  CHECK(j != id_to_info_.end());

  if (j->second.permanent_window_id) {
    XDestroyWindow(gdk_x11_get_default_xdisplay(),
                   j->second.permanent_window_id);
  }

  native_view_to_id_.erase(i);
  id_to_info_.erase(j);
}

void GtkNativeViewManager::OnSizeAllocate(
    gfx::NativeView widget, GtkAllocation *alloc) {
  AutoLock locked(lock_);

  const gfx::NativeViewId id = GetWidgetId(widget);
  std::map<gfx::NativeViewId, NativeViewInfo>::iterator i =
    id_to_info_.find(id);

  if (i->second.permanent_window_id) {
    XResizeWindow(gdk_x11_get_default_xdisplay(),
                  i->second.permanent_window_id,
                  alloc->width, alloc->height);
  }
}

// -----------------------------------------------------------------------------