Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / ui / platform_window / x11 / x11_window.cc
blobe86cf3c9a09cf6631f6ffd6f5672dd79190d4a21
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 } // namespace
43 X11Window::X11Window(PlatformWindowDelegate* delegate)
44 : delegate_(delegate),
45 xdisplay_(gfx::GetXDisplay()),
46 xwindow_(None),
47 xroot_window_(DefaultRootWindow(xdisplay_)),
48 atom_cache_(xdisplay_, kAtomsToCache),
49 window_mapped_(false) {
50 CHECK(delegate_);
51 TouchFactory::SetTouchDeviceListFromCommandLine();
54 X11Window::~X11Window() {
55 Destroy();
58 void X11Window::Destroy() {
59 delegate_->OnClosed();
60 if (xwindow_ == None)
61 return;
63 // Stop processing events.
64 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
65 XDestroyWindow(xdisplay_, xwindow_);
66 xwindow_ = None;
69 void X11Window::ProcessXInput2Event(XEvent* xev) {
70 if (!TouchFactory::GetInstance()->ShouldProcessXI2Event(xev))
71 return;
72 EventType event_type = EventTypeFromNative(xev);
73 switch (event_type) {
74 case ET_KEY_PRESSED:
75 case ET_KEY_RELEASED: {
76 KeyEvent key_event(xev);
77 delegate_->DispatchEvent(&key_event);
78 break;
80 case ET_MOUSE_PRESSED:
81 case ET_MOUSE_MOVED:
82 case ET_MOUSE_DRAGGED:
83 case ET_MOUSE_RELEASED: {
84 MouseEvent mouse_event(xev);
85 delegate_->DispatchEvent(&mouse_event);
86 break;
88 case ET_MOUSEWHEEL: {
89 MouseWheelEvent wheel_event(xev);
90 delegate_->DispatchEvent(&wheel_event);
91 break;
93 case ET_SCROLL_FLING_START:
94 case ET_SCROLL_FLING_CANCEL:
95 case ET_SCROLL: {
96 ScrollEvent scroll_event(xev);
97 delegate_->DispatchEvent(&scroll_event);
98 break;
100 case ET_TOUCH_MOVED:
101 case ET_TOUCH_PRESSED:
102 case ET_TOUCH_CANCELLED:
103 case ET_TOUCH_RELEASED: {
104 TouchEvent touch_event(xev);
105 delegate_->DispatchEvent(&touch_event);
106 break;
108 default:
109 break;
113 void X11Window::Show() {
114 if (window_mapped_)
115 return;
117 CHECK(PlatformEventSource::GetInstance());
118 PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
120 XSetWindowAttributes swa;
121 memset(&swa, 0, sizeof(swa));
122 swa.background_pixmap = None;
123 swa.override_redirect = False;
124 xwindow_ = XCreateWindow(xdisplay_,
125 xroot_window_,
126 requested_bounds_.x(),
127 requested_bounds_.y(),
128 requested_bounds_.width(),
129 requested_bounds_.height(),
130 0, // border width
131 CopyFromParent, // depth
132 InputOutput,
133 CopyFromParent, // visual
134 CWBackPixmap | CWOverrideRedirect,
135 &swa);
137 long event_mask = ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
138 KeyPressMask | KeyReleaseMask | EnterWindowMask |
139 LeaveWindowMask | ExposureMask | VisibilityChangeMask |
140 StructureNotifyMask | PropertyChangeMask |
141 PointerMotionMask;
142 XSelectInput(xdisplay_, xwindow_, event_mask);
144 unsigned char mask[XIMaskLen(XI_LASTEVENT)];
145 memset(mask, 0, sizeof(mask));
147 XISetMask(mask, XI_TouchBegin);
148 XISetMask(mask, XI_TouchUpdate);
149 XISetMask(mask, XI_TouchEnd);
150 XISetMask(mask, XI_ButtonPress);
151 XISetMask(mask, XI_ButtonRelease);
152 XISetMask(mask, XI_Motion);
153 XISetMask(mask, XI_KeyPress);
154 XISetMask(mask, XI_KeyRelease);
156 XIEventMask evmask;
157 evmask.deviceid = XIAllDevices;
158 evmask.mask_len = sizeof(mask);
159 evmask.mask = mask;
160 XISelectEvents(xdisplay_, xwindow_, &evmask, 1);
161 XFlush(xdisplay_);
163 ::Atom protocols[2];
164 protocols[0] = atom_cache_.GetAtom("WM_DELETE_WINDOW");
165 protocols[1] = atom_cache_.GetAtom("_NET_WM_PING");
166 XSetWMProtocols(xdisplay_, xwindow_, protocols, 2);
168 // We need a WM_CLIENT_MACHINE and WM_LOCALE_NAME value so we integrate with
169 // the desktop environment.
170 XSetWMProperties(
171 xdisplay_, xwindow_, NULL, NULL, NULL, 0, NULL, NULL, NULL);
173 // Likewise, the X server needs to know this window's pid so it knows which
174 // program to kill if the window hangs.
175 // XChangeProperty() expects "pid" to be long.
176 COMPILE_ASSERT(sizeof(long) >= sizeof(pid_t), pid_t_bigger_than_long);
177 long pid = getpid();
178 XChangeProperty(xdisplay_,
179 xwindow_,
180 atom_cache_.GetAtom("_NET_WM_PID"),
181 XA_CARDINAL,
183 PropModeReplace,
184 reinterpret_cast<unsigned char*>(&pid),
186 // Before we map the window, set size hints. Otherwise, some window managers
187 // will ignore toplevel XMoveWindow commands.
188 XSizeHints size_hints;
189 size_hints.flags = PPosition | PWinGravity;
190 size_hints.x = requested_bounds_.x();
191 size_hints.y = requested_bounds_.y();
192 // Set StaticGravity so that the window position is not affected by the
193 // frame width when running with window manager.
194 size_hints.win_gravity = StaticGravity;
195 XSetWMNormalHints(xdisplay_, xwindow_, &size_hints);
197 delegate_->OnAcceleratedWidgetAvailable(xwindow_);
199 XMapWindow(xdisplay_, xwindow_);
201 // We now block until our window is mapped. Some X11 APIs will crash and
202 // burn if passed |xwindow_| before the window is mapped, and XMapWindow is
203 // asynchronous.
204 if (X11EventSource::GetInstance())
205 X11EventSource::GetInstance()->BlockUntilWindowMapped(xwindow_);
206 window_mapped_ = true;
209 void X11Window::Hide() {
210 if (!window_mapped_)
211 return;
212 XWithdrawWindow(xdisplay_, xwindow_, 0);
213 window_mapped_ = false;
216 void X11Window::Close() {
217 Destroy();
220 void X11Window::SetBounds(const gfx::Rect& bounds) {
221 requested_bounds_ = bounds;
222 if (!window_mapped_)
223 return;
224 XWindowChanges changes = {0};
225 unsigned value_mask = CWX | CWY | CWWidth | CWHeight;
226 changes.x = bounds.x();
227 changes.y = bounds.y();
228 changes.width = bounds.width();
229 changes.height = bounds.height();
230 XConfigureWindow(xdisplay_, xwindow_, value_mask, &changes);
233 gfx::Rect X11Window::GetBounds() {
234 return confirmed_bounds_;
237 void X11Window::SetCapture() {}
239 void X11Window::ReleaseCapture() {}
241 void X11Window::ToggleFullscreen() {}
243 void X11Window::Maximize() {}
245 void X11Window::Minimize() {}
247 void X11Window::Restore() {}
249 void X11Window::SetCursor(PlatformCursor cursor) {}
251 void X11Window::MoveCursorTo(const gfx::Point& location) {}
253 bool X11Window::CanDispatchEvent(const PlatformEvent& event) {
254 return FindXEventTarget(event) == xwindow_;
257 uint32_t X11Window::DispatchEvent(const PlatformEvent& event) {
258 XEvent* xev = event;
259 switch (xev->type) {
260 case EnterNotify: {
261 // EnterNotify creates ET_MOUSE_MOVED. Mark as synthesized as this is
262 // not real mouse move event.
263 MouseEvent mouse_event(xev);
264 CHECK_EQ(ET_MOUSE_MOVED, mouse_event.type());
265 mouse_event.set_flags(mouse_event.flags() | EF_IS_SYNTHESIZED);
266 delegate_->DispatchEvent(&mouse_event);
267 break;
269 case LeaveNotify: {
270 MouseEvent mouse_event(xev);
271 delegate_->DispatchEvent(&mouse_event);
272 break;
275 case Expose: {
276 gfx::Rect damage_rect(xev->xexpose.x,
277 xev->xexpose.y,
278 xev->xexpose.width,
279 xev->xexpose.height);
280 delegate_->OnDamageRect(damage_rect);
281 break;
284 case KeyPress:
285 case KeyRelease: {
286 KeyEvent key_event(xev);
287 delegate_->DispatchEvent(&key_event);
288 break;
291 case ButtonPress:
292 case ButtonRelease: {
293 switch (EventTypeFromNative(xev)) {
294 case ET_MOUSEWHEEL: {
295 MouseWheelEvent mouseev(xev);
296 delegate_->DispatchEvent(&mouseev);
297 break;
299 case ET_MOUSE_PRESSED:
300 case ET_MOUSE_RELEASED: {
301 MouseEvent mouseev(xev);
302 delegate_->DispatchEvent(&mouseev);
303 break;
305 case ET_UNKNOWN:
306 // No event is created for X11-release events for mouse-wheel
307 // buttons.
308 break;
309 default:
310 NOTREACHED();
312 break;
315 case FocusOut:
316 if (xev->xfocus.mode != NotifyGrab)
317 delegate_->OnLostCapture();
318 break;
320 case ConfigureNotify: {
321 DCHECK_EQ(xwindow_, xev->xconfigure.event);
322 DCHECK_EQ(xwindow_, xev->xconfigure.window);
323 gfx::Rect bounds(xev->xconfigure.x,
324 xev->xconfigure.y,
325 xev->xconfigure.width,
326 xev->xconfigure.height);
327 if (confirmed_bounds_ != bounds) {
328 confirmed_bounds_ = bounds;
329 delegate_->OnBoundsChanged(confirmed_bounds_);
331 break;
334 case ClientMessage: {
335 Atom message = static_cast<Atom>(xev->xclient.data.l[0]);
336 if (message == atom_cache_.GetAtom("WM_DELETE_WINDOW")) {
337 delegate_->OnCloseRequest();
338 } else if (message == atom_cache_.GetAtom("_NET_WM_PING")) {
339 XEvent reply_event = *xev;
340 reply_event.xclient.window = xroot_window_;
342 XSendEvent(xdisplay_,
343 reply_event.xclient.window,
344 False,
345 SubstructureRedirectMask | SubstructureNotifyMask,
346 &reply_event);
347 XFlush(xdisplay_);
349 break;
352 case GenericEvent: {
353 ProcessXInput2Event(xev);
354 break;
357 return POST_DISPATCH_STOP_PROPAGATION;
360 } // namespace ui