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>
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"
27 const char* kAtomsToCache
[] = {
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
;
43 X11Window::X11Window(PlatformWindowDelegate
* delegate
)
44 : delegate_(delegate
),
45 xdisplay_(gfx::GetXDisplay()),
47 xroot_window_(DefaultRootWindow(xdisplay_
)),
48 atom_cache_(xdisplay_
, kAtomsToCache
),
49 window_mapped_(false) {
51 TouchFactory::SetTouchDeviceListFromCommandLine();
54 X11Window::~X11Window() {
58 void X11Window::Destroy() {
59 delegate_
->OnClosed();
63 // Stop processing events.
64 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
65 XDestroyWindow(xdisplay_
, xwindow_
);
69 void X11Window::ProcessXInput2Event(XEvent
* xev
) {
70 if (!TouchFactory::GetInstance()->ShouldProcessXI2Event(xev
))
72 EventType event_type
= EventTypeFromNative(xev
);
75 case ET_KEY_RELEASED
: {
76 KeyEvent
key_event(xev
);
77 delegate_
->DispatchEvent(&key_event
);
80 case ET_MOUSE_PRESSED
:
82 case ET_MOUSE_DRAGGED
:
83 case ET_MOUSE_RELEASED
: {
84 MouseEvent
mouse_event(xev
);
85 delegate_
->DispatchEvent(&mouse_event
);
89 MouseWheelEvent
wheel_event(xev
);
90 delegate_
->DispatchEvent(&wheel_event
);
93 case ET_SCROLL_FLING_START
:
94 case ET_SCROLL_FLING_CANCEL
:
96 ScrollEvent
scroll_event(xev
);
97 delegate_
->DispatchEvent(&scroll_event
);
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
);
113 void X11Window::Show() {
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_
,
126 requested_bounds_
.x(),
127 requested_bounds_
.y(),
128 requested_bounds_
.width(),
129 requested_bounds_
.height(),
131 CopyFromParent
, // depth
133 CopyFromParent
, // visual
134 CWBackPixmap
| CWOverrideRedirect
,
137 long event_mask
= ButtonPressMask
| ButtonReleaseMask
| FocusChangeMask
|
138 KeyPressMask
| KeyReleaseMask
| EnterWindowMask
|
139 LeaveWindowMask
| ExposureMask
| VisibilityChangeMask
|
140 StructureNotifyMask
| PropertyChangeMask
|
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
);
157 evmask
.deviceid
= XIAllDevices
;
158 evmask
.mask_len
= sizeof(mask
);
160 XISelectEvents(xdisplay_
, xwindow_
, &evmask
, 1);
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.
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
);
178 XChangeProperty(xdisplay_
,
180 atom_cache_
.GetAtom("_NET_WM_PID"),
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
204 if (X11EventSource::GetInstance())
205 X11EventSource::GetInstance()->BlockUntilWindowMapped(xwindow_
);
206 window_mapped_
= true;
209 void X11Window::Hide() {
212 XWithdrawWindow(xdisplay_
, xwindow_
, 0);
213 window_mapped_
= false;
216 void X11Window::Close() {
220 void X11Window::SetBounds(const gfx::Rect
& bounds
) {
221 requested_bounds_
= bounds
;
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
) {
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
);
270 MouseEvent
mouse_event(xev
);
271 delegate_
->DispatchEvent(&mouse_event
);
276 gfx::Rect
damage_rect(xev
->xexpose
.x
,
279 xev
->xexpose
.height
);
280 delegate_
->OnDamageRect(damage_rect
);
286 KeyEvent
key_event(xev
);
287 delegate_
->DispatchEvent(&key_event
);
292 case ButtonRelease
: {
293 switch (EventTypeFromNative(xev
)) {
294 case ET_MOUSEWHEEL
: {
295 MouseWheelEvent
mouseev(xev
);
296 delegate_
->DispatchEvent(&mouseev
);
299 case ET_MOUSE_PRESSED
:
300 case ET_MOUSE_RELEASED
: {
301 MouseEvent
mouseev(xev
);
302 delegate_
->DispatchEvent(&mouseev
);
306 // No event is created for X11-release events for mouse-wheel
316 if (xev
->xfocus
.mode
!= NotifyGrab
)
317 delegate_
->OnLostCapture();
320 case ConfigureNotify
: {
321 DCHECK_EQ(xwindow_
, xev
->xconfigure
.event
);
322 DCHECK_EQ(xwindow_
, xev
->xconfigure
.window
);
323 gfx::Rect
bounds(xev
->xconfigure
.x
,
325 xev
->xconfigure
.width
,
326 xev
->xconfigure
.height
);
327 if (confirmed_bounds_
!= bounds
) {
328 confirmed_bounds_
= bounds
;
329 delegate_
->OnBoundsChanged(confirmed_bounds_
);
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
,
345 SubstructureRedirectMask
| SubstructureNotifyMask
,
353 ProcessXInput2Event(xev
);
357 return POST_DISPATCH_STOP_PROPAGATION
;