Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: ui/platform_window/x11/x11_window_base.cc

Issue 1602173005: Add PlatformWindow/Event related code for Ozone X11. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Split X11EventSource and X11Window. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ui/platform_window/x11/x11_window_base.h"
6
7 #include <X11/Xatom.h>
8 #include <X11/Xlib.h>
9 #include <X11/Xutil.h>
10 #include <X11/extensions/XInput2.h>
11
12 #include <string>
13
14 #include "base/strings/utf_string_conversions.h"
15 #include "ui/events/devices/x11/touch_factory_x11.h"
16 #include "ui/events/event.h"
17 #include "ui/events/event_utils.h"
18 #include "ui/events/platform/platform_event_dispatcher.h"
19 #include "ui/events/platform/platform_event_source.h"
20 #include "ui/events/platform/x11/x11_event_source.h"
21 #include "ui/events/x/events_x_utils.h"
22 #include "ui/gfx/geometry/rect.h"
23 #include "ui/gfx/x/x11_atom_cache.h"
24 #include "ui/gfx/x/x11_types.h"
25 #include "ui/platform_window/platform_window_delegate.h"
26
27 namespace ui {
28
29 namespace {
30
31 const char* kAtomsToCache[] = {"UTF8_STRING", "WM_DELETE_WINDOW",
32 "_NET_WM_NAME", "_NET_WM_PID",
33 "_NET_WM_PING", NULL};
34
35 bool g_override_redirect = false;
36
37 } // namespace
38
39 X11WindowBase::X11WindowBase(PlatformWindowDelegate* delegate)
40 : delegate_(delegate),
41 xdisplay_(gfx::GetXDisplay()),
42 xwindow_(None),
43 xroot_window_(DefaultRootWindow(xdisplay_)),
44 atom_cache_(xdisplay_, kAtomsToCache) {
45 DCHECK(delegate_);
46 TouchFactory::SetTouchDeviceListFromCommandLine();
47 }
48
49 X11WindowBase::~X11WindowBase() {
50 Destroy();
51 }
52
53 void X11WindowBase::Destroy() {
54 if (xwindow_ == None)
55 return;
56
57 // Stop processing events.
58 X11EventSource::GetInstance()->RemovePlatformEventDispatcher(this);
59 XID xwindow = xwindow_;
60 XDisplay* xdisplay = xdisplay_;
61 xwindow_ = None;
62 delegate_->OnClosed();
63 // |this| might be deleted because of the above call.
64
65 XDestroyWindow(xdisplay, xwindow);
66 }
67
68 void X11WindowBase::Create() {
69 DCHECK(X11EventSource::GetInstance());
70 X11EventSource::GetInstance()->AddPlatformEventDispatcher(this);
71
72 XSetWindowAttributes swa;
73 memset(&swa, 0, sizeof(swa));
74 swa.background_pixmap = None;
75 swa.bit_gravity = NorthWestGravity;
76 swa.override_redirect = g_override_redirect;
77 xwindow_ = XCreateWindow(
78 xdisplay_, xroot_window_, requested_bounds_.x(), requested_bounds_.y(),
79 requested_bounds_.width(), requested_bounds_.height(),
80 0, // border width
81 CopyFromParent, // depth
82 InputOutput,
83 CopyFromParent, // visual
84 CWBackPixmap | CWBitGravity | CWOverrideRedirect, &swa);
85
86 long event_mask = ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
87 KeyPressMask | KeyReleaseMask | EnterWindowMask |
88 LeaveWindowMask | ExposureMask | VisibilityChangeMask |
89 StructureNotifyMask | PropertyChangeMask |
90 PointerMotionMask;
91 XSelectInput(xdisplay_, xwindow_, event_mask);
92
93 unsigned char mask[XIMaskLen(XI_LASTEVENT)];
94 memset(mask, 0, sizeof(mask));
95
96 XISetMask(mask, XI_TouchBegin);
97 XISetMask(mask, XI_TouchUpdate);
98 XISetMask(mask, XI_TouchEnd);
99 XISetMask(mask, XI_ButtonPress);
100 XISetMask(mask, XI_ButtonRelease);
101 XISetMask(mask, XI_Motion);
102 XISetMask(mask, XI_KeyPress);
103 XISetMask(mask, XI_KeyRelease);
104
105 XIEventMask evmask;
106 evmask.deviceid = XIAllDevices;
107 evmask.mask_len = sizeof(mask);
108 evmask.mask = mask;
109 XISelectEvents(xdisplay_, xwindow_, &evmask, 1);
110 XFlush(xdisplay_);
111
112 ::Atom protocols[2];
113 protocols[0] = atom_cache_.GetAtom("WM_DELETE_WINDOW");
114 protocols[1] = atom_cache_.GetAtom("_NET_WM_PING");
115 XSetWMProtocols(xdisplay_, xwindow_, protocols, 2);
116
117 // We need a WM_CLIENT_MACHINE and WM_LOCALE_NAME value so we integrate with
118 // the desktop environment.
119 XSetWMProperties(xdisplay_, xwindow_, NULL, NULL, NULL, 0, NULL, NULL, NULL);
120
121 // Likewise, the X server needs to know this window's pid so it knows which
122 // program to kill if the window hangs.
123 // XChangeProperty() expects "pid" to be long.
124 static_assert(sizeof(long) >= sizeof(pid_t),
125 "pid_t should not be larger than long");
126 long pid = getpid();
127 XChangeProperty(xdisplay_, xwindow_, atom_cache_.GetAtom("_NET_WM_PID"),
128 XA_CARDINAL, 32, PropModeReplace,
129 reinterpret_cast<unsigned char*>(&pid), 1);
130 // Before we map the window, set size hints. Otherwise, some window managers
131 // will ignore toplevel XMoveWindow commands.
132 XSizeHints size_hints;
133 size_hints.flags = PPosition | PWinGravity;
134 size_hints.x = requested_bounds_.x();
135 size_hints.y = requested_bounds_.y();
136 // Set StaticGravity so that the window position is not affected by the
137 // frame width when running with window manager.
138 size_hints.win_gravity = StaticGravity;
139 XSetWMNormalHints(xdisplay_, xwindow_, &size_hints);
140
141 // TODO(sky): provide real scale factor.
142 delegate_->OnAcceleratedWidgetAvailable(xwindow_, 1.f);
143 }
144
145 void X11WindowBase::Show() {
146 if (window_mapped_)
147 return;
148 if (xwindow_ == None)
149 Create();
150
151 XMapWindow(xdisplay_, xwindow_);
152
153 // We now block until our window is mapped. Some X11 APIs will crash and
154 // burn if passed |xwindow_| before the window is mapped, and XMapWindow is
155 // asynchronous.
156 if (X11EventSource::GetInstance())
157 X11EventSource::GetInstance()->BlockUntilWindowMapped(xwindow_);
158 window_mapped_ = true;
159 }
160
161 void X11WindowBase::Hide() {
162 if (!window_mapped_)
163 return;
164 XWithdrawWindow(xdisplay_, xwindow_, 0);
165 window_mapped_ = false;
166 }
167
168 void X11WindowBase::Close() {
169 Destroy();
170 }
171
172 void X11WindowBase::SetBounds(const gfx::Rect& bounds) {
173 requested_bounds_ = bounds;
174 if (!window_mapped_ || bounds == confirmed_bounds_)
175 return;
176 XWindowChanges changes = {0};
177 unsigned value_mask = CWX | CWY | CWWidth | CWHeight;
178 changes.x = bounds.x();
179 changes.y = bounds.y();
180 changes.width = bounds.width();
181 changes.height = bounds.height();
182 XConfigureWindow(xdisplay_, xwindow_, value_mask, &changes);
183 }
184
185 gfx::Rect X11WindowBase::GetBounds() {
186 return confirmed_bounds_;
187 }
188
189 void X11WindowBase::SetTitle(const base::string16& title) {
190 if (window_title_ == title)
191 return;
192 window_title_ = title;
193 std::string utf8str = base::UTF16ToUTF8(title);
194 XChangeProperty(xdisplay_, xwindow_, atom_cache_.GetAtom("_NET_WM_NAME"),
195 atom_cache_.GetAtom("UTF8_STRING"), 8, PropModeReplace,
196 reinterpret_cast<const unsigned char*>(utf8str.c_str()),
197 utf8str.size());
198 XTextProperty xtp;
199 char* c_utf8_str = const_cast<char*>(utf8str.c_str());
200 if (Xutf8TextListToTextProperty(xdisplay_, &c_utf8_str, 1, XUTF8StringStyle,
201 &xtp) == Success) {
202 XSetWMName(xdisplay_, xwindow_, &xtp);
203 XFree(xtp.value);
204 }
205 }
206
207 void X11WindowBase::SetCapture() {}
208
209 void X11WindowBase::ReleaseCapture() {}
210
211 void X11WindowBase::ToggleFullscreen() {}
212
213 void X11WindowBase::Maximize() {}
214
215 void X11WindowBase::Minimize() {}
216
217 void X11WindowBase::Restore() {}
218
219 void X11WindowBase::MoveCursorTo(const gfx::Point& location) {}
220
221 void X11WindowBase::ConfineCursorToBounds(const gfx::Rect& bounds) {}
222
223 PlatformImeController* X11WindowBase::GetPlatformImeController() {
224 return nullptr;
225 }
226
227 void X11WindowBase::ProcessXWindowEvent(XEvent* xev) {
228 switch (xev->type) {
229 case Expose: {
230 gfx::Rect damage_rect(xev->xexpose.x, xev->xexpose.y, xev->xexpose.width,
231 xev->xexpose.height);
232 delegate_->OnDamageRect(damage_rect);
233 break;
234 }
235
236 case FocusOut:
237 if (xev->xfocus.mode != NotifyGrab)
238 delegate_->OnLostCapture();
239 break;
240
241 case ConfigureNotify: {
242 DCHECK_EQ(xwindow_, xev->xconfigure.event);
243 DCHECK_EQ(xwindow_, xev->xconfigure.window);
244 gfx::Rect bounds(xev->xconfigure.x, xev->xconfigure.y,
245 xev->xconfigure.width, xev->xconfigure.height);
246 if (confirmed_bounds_ != bounds) {
247 confirmed_bounds_ = bounds;
248 delegate_->OnBoundsChanged(confirmed_bounds_);
249 }
250 break;
251 }
252
253 case ClientMessage: {
254 Atom message = static_cast<Atom>(xev->xclient.data.l[0]);
255 if (message == atom_cache_.GetAtom("WM_DELETE_WINDOW")) {
256 delegate_->OnCloseRequest();
257 } else if (message == atom_cache_.GetAtom("_NET_WM_PING")) {
258 XEvent reply_event = *xev;
259 reply_event.xclient.window = xroot_window_;
260
261 XSendEvent(xdisplay_, reply_event.xclient.window, False,
262 SubstructureRedirectMask | SubstructureNotifyMask,
263 &reply_event);
264 XFlush(xdisplay_);
265 }
266 break;
267 }
268 }
269 }
270
271 namespace test {
272
273 void SetUseOverrideRedirectWindowByDefault(bool override_redirect) {
274 g_override_redirect = override_redirect;
275 }
276
277 } // namespace test
278 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698