Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / platform_window / x11 / x11_window.cc
blob747581700efe9fa1aaf4792ec8906e8611ec27c6
1 // Copyright 2014 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.
5 #include "ui/platform_window/x11/x11_window.h"
7 #include <X11/extensions/XInput2.h>
8 #include <X11/Xatom.h>
9 #include <X11/Xlib.h>
10 #include <X11/Xutil.h>
12 #include "ui/events/devices/x11/touch_factory_x11.h"
13 #include "ui/events/event.h"
14 #include "ui/events/event_utils.h"
15 #include "ui/events/platform/platform_event_dispatcher.h"
16 #include "ui/events/platform/platform_event_source.h"
17 #include "ui/events/platform/x11/x11_event_source.h"
18 #include "ui/gfx/geometry/rect.h"
19 #include "ui/gfx/x/x11_atom_cache.h"
20 #include "ui/gfx/x/x11_types.h"
21 #include "ui/platform_window/platform_window_delegate.h"
23 namespace ui {
25 namespace {
27 const char* kAtomsToCache[] = {
28 "WM_DELETE_WINDOW",
29 "_NET_WM_PING",
30 "_NET_WM_PID",
31 NULL
34 XID FindXEventTarget(XEvent* xevent) {
35 XID target = xevent->xany.window;
36 if (xevent->type == GenericEvent)
37 target = static_cast<XIDeviceEvent*>(xevent->xcookie.data)->event;
38 return target;
41 bool g_override_redirect = false;
43 } // namespace
45 X11Window::X11Window(PlatformWindowDelegate* delegate)
46 : delegate_(delegate),
47 xdisplay_(gfx::GetXDisplay()),
48 xwindow_(None),
49 xroot_window_(DefaultRootWindow(xdisplay_)),
50 atom_cache_(xdisplay_, kAtomsToCache),
51 window_mapped_(false) {
52 CHECK(delegate_);
53 TouchFactory::SetTouchDeviceListFromCommandLine();
56 X11Window::~X11Window() {
59 void X11Window::Destroy() {
60 if (xwindow_ == None)
61 return;
63 // Stop processing events.
64 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
65 XID xwindow = xwindow_;
66 XDisplay* xdisplay = xdisplay_;
67 xwindow_ = None;
68 delegate_->OnClosed();
69 // |this| might be deleted because of the above call.
71 XDestroyWindow(xdisplay, xwindow);
74 void X11Window::ProcessXInput2Event(XEvent* xev) {
75 if (!TouchFactory::GetInstance()->ShouldProcessXI2Event(xev))
76 return;
77 EventType event_type = EventTypeFromNative(xev);
78 switch (event_type) {
79 case ET_KEY_PRESSED:
80 case ET_KEY_RELEASED: {
81 KeyEvent key_event(xev);
82 delegate_->DispatchEvent(&key_event);
83 break;
85 case ET_MOUSE_PRESSED:
86 case ET_MOUSE_MOVED:
87 case ET_MOUSE_DRAGGED:
88 case ET_MOUSE_RELEASED: {
89 MouseEvent mouse_event(xev);
90 delegate_->DispatchEvent(&mouse_event);
91 break;
93 case ET_MOUSEWHEEL: {
94 MouseWheelEvent wheel_event(xev);
95 delegate_->DispatchEvent(&wheel_event);
96 break;
98 case ET_SCROLL_FLING_START:
99 case ET_SCROLL_FLING_CANCEL:
100 case ET_SCROLL: {
101 ScrollEvent scroll_event(xev);
102 delegate_->DispatchEvent(&scroll_event);
103 break;
105 case ET_TOUCH_MOVED:
106 case ET_TOUCH_PRESSED:
107 case ET_TOUCH_CANCELLED:
108 case ET_TOUCH_RELEASED: {
109 TouchEvent touch_event(xev);
110 delegate_->DispatchEvent(&touch_event);
111 break;
113 default:
114 break;
118 void X11Window::Show() {
119 if (window_mapped_)
120 return;
122 CHECK(PlatformEventSource::GetInstance());
123 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
125 XSetWindowAttributes swa;
126 memset(&swa, 0, sizeof(swa));
127 swa.background_pixmap = None;
128 swa.override_redirect = g_override_redirect;
129 xwindow_ = XCreateWindow(xdisplay_,
130 xroot_window_,
131 requested_bounds_.x(),
132 requested_bounds_.y(),
133 requested_bounds_.width(),
134 requested_bounds_.height(),
135 0, // border width
136 CopyFromParent, // depth
137 InputOutput,
138 CopyFromParent, // visual
139 CWBackPixmap | CWOverrideRedirect,
140 &swa);
142 long event_mask = ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
143 KeyPressMask | KeyReleaseMask | EnterWindowMask |
144 LeaveWindowMask | ExposureMask | VisibilityChangeMask |
145 StructureNotifyMask | PropertyChangeMask |
146 PointerMotionMask;
147 XSelectInput(xdisplay_, xwindow_, event_mask);
149 unsigned char mask[XIMaskLen(XI_LASTEVENT)];
150 memset(mask, 0, sizeof(mask));
152 XISetMask(mask, XI_TouchBegin);
153 XISetMask(mask, XI_TouchUpdate);
154 XISetMask(mask, XI_TouchEnd);
155 XISetMask(mask, XI_ButtonPress);
156 XISetMask(mask, XI_ButtonRelease);
157 XISetMask(mask, XI_Motion);
158 XISetMask(mask, XI_KeyPress);
159 XISetMask(mask, XI_KeyRelease);
161 XIEventMask evmask;
162 evmask.deviceid = XIAllDevices;
163 evmask.mask_len = sizeof(mask);
164 evmask.mask = mask;
165 XISelectEvents(xdisplay_, xwindow_, &evmask, 1);
166 XFlush(xdisplay_);
168 ::Atom protocols[2];
169 protocols[0] = atom_cache_.GetAtom("WM_DELETE_WINDOW");
170 protocols[1] = atom_cache_.GetAtom("_NET_WM_PING");
171 XSetWMProtocols(xdisplay_, xwindow_, protocols, 2);
173 // We need a WM_CLIENT_MACHINE and WM_LOCALE_NAME value so we integrate with
174 // the desktop environment.
175 XSetWMProperties(
176 xdisplay_, xwindow_, NULL, NULL, NULL, 0, NULL, NULL, NULL);
178 // Likewise, the X server needs to know this window's pid so it knows which
179 // program to kill if the window hangs.
180 // XChangeProperty() expects "pid" to be long.
181 static_assert(sizeof(long) >= sizeof(pid_t),
182 "pid_t should not be larger than long");
183 long pid = getpid();
184 XChangeProperty(xdisplay_,
185 xwindow_,
186 atom_cache_.GetAtom("_NET_WM_PID"),
187 XA_CARDINAL,
189 PropModeReplace,
190 reinterpret_cast<unsigned char*>(&pid),
192 // Before we map the window, set size hints. Otherwise, some window managers
193 // will ignore toplevel XMoveWindow commands.
194 XSizeHints size_hints;
195 size_hints.flags = PPosition | PWinGravity;
196 size_hints.x = requested_bounds_.x();
197 size_hints.y = requested_bounds_.y();
198 // Set StaticGravity so that the window position is not affected by the
199 // frame width when running with window manager.
200 size_hints.win_gravity = StaticGravity;
201 XSetWMNormalHints(xdisplay_, xwindow_, &size_hints);
203 // TODO(sky): provide real scale factor.
204 delegate_->OnAcceleratedWidgetAvailable(xwindow_, 1.f);
206 XMapWindow(xdisplay_, xwindow_);
208 // We now block until our window is mapped. Some X11 APIs will crash and
209 // burn if passed |xwindow_| before the window is mapped, and XMapWindow is
210 // asynchronous.
211 if (X11EventSource::GetInstance())
212 X11EventSource::GetInstance()->BlockUntilWindowMapped(xwindow_);
213 window_mapped_ = true;
216 void X11Window::Hide() {
217 if (!window_mapped_)
218 return;
219 XWithdrawWindow(xdisplay_, xwindow_, 0);
220 window_mapped_ = false;
223 void X11Window::Close() {
224 Destroy();
227 void X11Window::SetBounds(const gfx::Rect& bounds) {
228 requested_bounds_ = bounds;
229 if (!window_mapped_)
230 return;
231 XWindowChanges changes = {0};
232 unsigned value_mask = CWX | CWY | CWWidth | CWHeight;
233 changes.x = bounds.x();
234 changes.y = bounds.y();
235 changes.width = bounds.width();
236 changes.height = bounds.height();
237 XConfigureWindow(xdisplay_, xwindow_, value_mask, &changes);
240 gfx::Rect X11Window::GetBounds() {
241 return confirmed_bounds_;
244 void X11Window::SetCapture() {}
246 void X11Window::ReleaseCapture() {}
248 void X11Window::ToggleFullscreen() {}
250 void X11Window::Maximize() {}
252 void X11Window::Minimize() {}
254 void X11Window::Restore() {}
256 void X11Window::SetCursor(PlatformCursor cursor) {}
258 void X11Window::MoveCursorTo(const gfx::Point& location) {}
260 void X11Window::ConfineCursorToBounds(const gfx::Rect& bounds) {
263 bool X11Window::CanDispatchEvent(const PlatformEvent& event) {
264 return FindXEventTarget(event) == xwindow_;
267 uint32_t X11Window::DispatchEvent(const PlatformEvent& event) {
268 XEvent* xev = event;
269 switch (xev->type) {
270 case EnterNotify: {
271 // EnterNotify creates ET_MOUSE_MOVED. Mark as synthesized as this is
272 // not real mouse move event.
273 MouseEvent mouse_event(xev);
274 CHECK_EQ(ET_MOUSE_MOVED, mouse_event.type());
275 mouse_event.set_flags(mouse_event.flags() | EF_IS_SYNTHESIZED);
276 delegate_->DispatchEvent(&mouse_event);
277 break;
279 case LeaveNotify: {
280 MouseEvent mouse_event(xev);
281 delegate_->DispatchEvent(&mouse_event);
282 break;
285 case Expose: {
286 gfx::Rect damage_rect(xev->xexpose.x,
287 xev->xexpose.y,
288 xev->xexpose.width,
289 xev->xexpose.height);
290 delegate_->OnDamageRect(damage_rect);
291 break;
294 case KeyPress:
295 case KeyRelease: {
296 KeyEvent key_event(xev);
297 delegate_->DispatchEvent(&key_event);
298 break;
301 case ButtonPress:
302 case ButtonRelease: {
303 switch (EventTypeFromNative(xev)) {
304 case ET_MOUSEWHEEL: {
305 MouseWheelEvent mouseev(xev);
306 delegate_->DispatchEvent(&mouseev);
307 break;
309 case ET_MOUSE_PRESSED:
310 case ET_MOUSE_RELEASED: {
311 MouseEvent mouseev(xev);
312 delegate_->DispatchEvent(&mouseev);
313 break;
315 case ET_UNKNOWN:
316 // No event is created for X11-release events for mouse-wheel
317 // buttons.
318 break;
319 default:
320 NOTREACHED();
322 break;
325 case FocusOut:
326 if (xev->xfocus.mode != NotifyGrab)
327 delegate_->OnLostCapture();
328 break;
330 case ConfigureNotify: {
331 DCHECK_EQ(xwindow_, xev->xconfigure.event);
332 DCHECK_EQ(xwindow_, xev->xconfigure.window);
333 gfx::Rect bounds(xev->xconfigure.x,
334 xev->xconfigure.y,
335 xev->xconfigure.width,
336 xev->xconfigure.height);
337 if (confirmed_bounds_ != bounds) {
338 confirmed_bounds_ = bounds;
339 delegate_->OnBoundsChanged(confirmed_bounds_);
341 break;
344 case ClientMessage: {
345 Atom message = static_cast<Atom>(xev->xclient.data.l[0]);
346 if (message == atom_cache_.GetAtom("WM_DELETE_WINDOW")) {
347 delegate_->OnCloseRequest();
348 } else if (message == atom_cache_.GetAtom("_NET_WM_PING")) {
349 XEvent reply_event = *xev;
350 reply_event.xclient.window = xroot_window_;
352 XSendEvent(xdisplay_,
353 reply_event.xclient.window,
354 False,
355 SubstructureRedirectMask | SubstructureNotifyMask,
356 &reply_event);
357 XFlush(xdisplay_);
359 break;
362 case GenericEvent: {
363 ProcessXInput2Event(xev);
364 break;
367 return POST_DISPATCH_STOP_PROPAGATION;
370 namespace test {
372 void SetUseOverrideRedirectWindowByDefault(bool override_redirect) {
373 g_override_redirect = override_redirect;
376 } // namespace test
377 } // namespace ui