OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/platform_window/x11/x11_window_base.h" | 5 #include "ui/platform_window/x11/x11_window_base.h" |
6 | 6 |
7 #include <X11/extensions/XInput2.h> | 7 #include <X11/extensions/XInput2.h> |
8 #include <X11/Xatom.h> | 8 #include <X11/Xatom.h> |
9 #include <X11/Xlib.h> | 9 #include <X11/Xlib.h> |
10 #include <X11/Xutil.h> | 10 #include <X11/Xutil.h> |
11 | 11 |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "ui/base/platform_window_defaults.h" | 15 #include "ui/base/platform_window_defaults.h" |
| 16 #include "ui/base/x/x11_util.h" |
16 #include "ui/base/x/x11_window_event_manager.h" | 17 #include "ui/base/x/x11_window_event_manager.h" |
17 #include "ui/events/devices/x11/touch_factory_x11.h" | 18 #include "ui/events/devices/x11/touch_factory_x11.h" |
18 #include "ui/events/event.h" | 19 #include "ui/events/event.h" |
19 #include "ui/events/event_utils.h" | 20 #include "ui/events/event_utils.h" |
20 #include "ui/events/platform/platform_event_dispatcher.h" | 21 #include "ui/events/platform/platform_event_dispatcher.h" |
21 #include "ui/events/platform/platform_event_source.h" | 22 #include "ui/events/platform/platform_event_source.h" |
22 #include "ui/events/platform/x11/x11_event_source.h" | 23 #include "ui/events/platform/x11/x11_event_source.h" |
23 #include "ui/gfx/geometry/rect.h" | 24 #include "ui/gfx/geometry/rect.h" |
24 #include "ui/platform_window/platform_window_delegate.h" | 25 #include "ui/platform_window/platform_window_delegate.h" |
25 | 26 |
26 namespace ui { | 27 namespace ui { |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 const char* kAtomsToCache[] = {"UTF8_STRING", "WM_DELETE_WINDOW", | |
31 "_NET_WM_NAME", "_NET_WM_PID", | |
32 "_NET_WM_PING", NULL}; | |
33 | |
34 XID FindXEventTarget(const XEvent& xev) { | 31 XID FindXEventTarget(const XEvent& xev) { |
35 XID target = xev.xany.window; | 32 XID target = xev.xany.window; |
36 if (xev.type == GenericEvent) | 33 if (xev.type == GenericEvent) |
37 target = static_cast<XIDeviceEvent*>(xev.xcookie.data)->event; | 34 target = static_cast<XIDeviceEvent*>(xev.xcookie.data)->event; |
38 return target; | 35 return target; |
39 } | 36 } |
40 | 37 |
41 } // namespace | 38 } // namespace |
42 | 39 |
43 X11WindowBase::X11WindowBase(PlatformWindowDelegate* delegate, | 40 X11WindowBase::X11WindowBase(PlatformWindowDelegate* delegate, |
44 const gfx::Rect& bounds) | 41 const gfx::Rect& bounds) |
45 : delegate_(delegate), | 42 : delegate_(delegate), |
46 xdisplay_(gfx::GetXDisplay()), | 43 xdisplay_(gfx::GetXDisplay()), |
47 xwindow_(None), | 44 xwindow_(None), |
48 xroot_window_(DefaultRootWindow(xdisplay_)), | 45 xroot_window_(DefaultRootWindow(xdisplay_)), |
49 atom_cache_(xdisplay_, kAtomsToCache), | |
50 bounds_(bounds) { | 46 bounds_(bounds) { |
51 DCHECK(delegate_); | 47 DCHECK(delegate_); |
52 TouchFactory::SetTouchDeviceListFromCommandLine(); | 48 TouchFactory::SetTouchDeviceListFromCommandLine(); |
53 } | 49 } |
54 | 50 |
55 X11WindowBase::~X11WindowBase() { | 51 X11WindowBase::~X11WindowBase() { |
56 Destroy(); | 52 Destroy(); |
57 } | 53 } |
58 | 54 |
59 void X11WindowBase::Destroy() { | 55 void X11WindowBase::Destroy() { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 XISetMask(mask, XI_HierarchyChanged); | 106 XISetMask(mask, XI_HierarchyChanged); |
111 | 107 |
112 XIEventMask evmask; | 108 XIEventMask evmask; |
113 evmask.deviceid = XIAllDevices; | 109 evmask.deviceid = XIAllDevices; |
114 evmask.mask_len = sizeof(mask); | 110 evmask.mask_len = sizeof(mask); |
115 evmask.mask = mask; | 111 evmask.mask = mask; |
116 XISelectEvents(xdisplay_, xwindow_, &evmask, 1); | 112 XISelectEvents(xdisplay_, xwindow_, &evmask, 1); |
117 XFlush(xdisplay_); | 113 XFlush(xdisplay_); |
118 | 114 |
119 ::Atom protocols[2]; | 115 ::Atom protocols[2]; |
120 protocols[0] = atom_cache_.GetAtom("WM_DELETE_WINDOW"); | 116 protocols[0] = GetAtom("WM_DELETE_WINDOW"); |
121 protocols[1] = atom_cache_.GetAtom("_NET_WM_PING"); | 117 protocols[1] = GetAtom("_NET_WM_PING"); |
122 XSetWMProtocols(xdisplay_, xwindow_, protocols, 2); | 118 XSetWMProtocols(xdisplay_, xwindow_, protocols, 2); |
123 | 119 |
124 // We need a WM_CLIENT_MACHINE and WM_LOCALE_NAME value so we integrate with | 120 // We need a WM_CLIENT_MACHINE and WM_LOCALE_NAME value so we integrate with |
125 // the desktop environment. | 121 // the desktop environment. |
126 XSetWMProperties(xdisplay_, xwindow_, NULL, NULL, NULL, 0, NULL, NULL, NULL); | 122 XSetWMProperties(xdisplay_, xwindow_, NULL, NULL, NULL, 0, NULL, NULL, NULL); |
127 | 123 |
128 // Likewise, the X server needs to know this window's pid so it knows which | 124 // Likewise, the X server needs to know this window's pid so it knows which |
129 // program to kill if the window hangs. | 125 // program to kill if the window hangs. |
130 // XChangeProperty() expects "pid" to be long. | 126 // XChangeProperty() expects "pid" to be long. |
131 static_assert(sizeof(long) >= sizeof(pid_t), | 127 static_assert(sizeof(long) >= sizeof(pid_t), |
132 "pid_t should not be larger than long"); | 128 "pid_t should not be larger than long"); |
133 long pid = getpid(); | 129 long pid = getpid(); |
134 XChangeProperty(xdisplay_, xwindow_, atom_cache_.GetAtom("_NET_WM_PID"), | 130 XChangeProperty(xdisplay_, xwindow_, GetAtom("_NET_WM_PID"), XA_CARDINAL, 32, |
135 XA_CARDINAL, 32, PropModeReplace, | 131 PropModeReplace, reinterpret_cast<unsigned char*>(&pid), 1); |
136 reinterpret_cast<unsigned char*>(&pid), 1); | |
137 // Before we map the window, set size hints. Otherwise, some window managers | 132 // Before we map the window, set size hints. Otherwise, some window managers |
138 // will ignore toplevel XMoveWindow commands. | 133 // will ignore toplevel XMoveWindow commands. |
139 XSizeHints size_hints; | 134 XSizeHints size_hints; |
140 size_hints.flags = PPosition | PWinGravity; | 135 size_hints.flags = PPosition | PWinGravity; |
141 size_hints.x = bounds_.x(); | 136 size_hints.x = bounds_.x(); |
142 size_hints.y = bounds_.y(); | 137 size_hints.y = bounds_.y(); |
143 // Set StaticGravity so that the window position is not affected by the | 138 // Set StaticGravity so that the window position is not affected by the |
144 // frame width when running with window manager. | 139 // frame width when running with window manager. |
145 size_hints.win_gravity = StaticGravity; | 140 size_hints.win_gravity = StaticGravity; |
146 XSetWMNormalHints(xdisplay_, xwindow_, &size_hints); | 141 XSetWMNormalHints(xdisplay_, xwindow_, &size_hints); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 | 207 |
213 gfx::Rect X11WindowBase::GetBounds() { | 208 gfx::Rect X11WindowBase::GetBounds() { |
214 return bounds_; | 209 return bounds_; |
215 } | 210 } |
216 | 211 |
217 void X11WindowBase::SetTitle(const base::string16& title) { | 212 void X11WindowBase::SetTitle(const base::string16& title) { |
218 if (window_title_ == title) | 213 if (window_title_ == title) |
219 return; | 214 return; |
220 window_title_ = title; | 215 window_title_ = title; |
221 std::string utf8str = base::UTF16ToUTF8(title); | 216 std::string utf8str = base::UTF16ToUTF8(title); |
222 XChangeProperty(xdisplay_, xwindow_, atom_cache_.GetAtom("_NET_WM_NAME"), | 217 XChangeProperty(xdisplay_, xwindow_, GetAtom("_NET_WM_NAME"), |
223 atom_cache_.GetAtom("UTF8_STRING"), 8, PropModeReplace, | 218 GetAtom("UTF8_STRING"), 8, PropModeReplace, |
224 reinterpret_cast<const unsigned char*>(utf8str.c_str()), | 219 reinterpret_cast<const unsigned char*>(utf8str.c_str()), |
225 utf8str.size()); | 220 utf8str.size()); |
226 XTextProperty xtp; | 221 XTextProperty xtp; |
227 char* c_utf8_str = const_cast<char*>(utf8str.c_str()); | 222 char* c_utf8_str = const_cast<char*>(utf8str.c_str()); |
228 if (Xutf8TextListToTextProperty(xdisplay_, &c_utf8_str, 1, XUTF8StringStyle, | 223 if (Xutf8TextListToTextProperty(xdisplay_, &c_utf8_str, 1, XUTF8StringStyle, |
229 &xtp) == Success) { | 224 &xtp) == Success) { |
230 XSetWMName(xdisplay_, xwindow_, &xtp); | 225 XSetWMName(xdisplay_, xwindow_, &xtp); |
231 XFree(xtp.value); | 226 XFree(xtp.value); |
232 } | 227 } |
233 } | 228 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 xev->xconfigure.width, xev->xconfigure.height); | 286 xev->xconfigure.width, xev->xconfigure.height); |
292 if (bounds_ != bounds) { | 287 if (bounds_ != bounds) { |
293 bounds_ = bounds; | 288 bounds_ = bounds; |
294 delegate_->OnBoundsChanged(bounds_); | 289 delegate_->OnBoundsChanged(bounds_); |
295 } | 290 } |
296 break; | 291 break; |
297 } | 292 } |
298 | 293 |
299 case ClientMessage: { | 294 case ClientMessage: { |
300 Atom message = static_cast<Atom>(xev->xclient.data.l[0]); | 295 Atom message = static_cast<Atom>(xev->xclient.data.l[0]); |
301 if (message == atom_cache_.GetAtom("WM_DELETE_WINDOW")) { | 296 if (message == GetAtom("WM_DELETE_WINDOW")) { |
302 delegate_->OnCloseRequest(); | 297 delegate_->OnCloseRequest(); |
303 } else if (message == atom_cache_.GetAtom("_NET_WM_PING")) { | 298 } else if (message == GetAtom("_NET_WM_PING")) { |
304 XEvent reply_event = *xev; | 299 XEvent reply_event = *xev; |
305 reply_event.xclient.window = xroot_window_; | 300 reply_event.xclient.window = xroot_window_; |
306 | 301 |
307 XSendEvent(xdisplay_, reply_event.xclient.window, False, | 302 XSendEvent(xdisplay_, reply_event.xclient.window, False, |
308 SubstructureRedirectMask | SubstructureNotifyMask, | 303 SubstructureRedirectMask | SubstructureNotifyMask, |
309 &reply_event); | 304 &reply_event); |
310 XFlush(xdisplay_); | 305 XFlush(xdisplay_); |
311 } | 306 } |
312 break; | 307 break; |
313 } | 308 } |
314 } | 309 } |
315 } | 310 } |
316 | 311 |
317 } // namespace ui | 312 } // namespace ui |
OLD | NEW |