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
;
41 bool g_override_redirect
= false;
45 X11Window::X11Window(PlatformWindowDelegate
* delegate
)
46 : delegate_(delegate
),
47 xdisplay_(gfx::GetXDisplay()),
49 xroot_window_(DefaultRootWindow(xdisplay_
)),
50 atom_cache_(xdisplay_
, kAtomsToCache
),
51 window_mapped_(false) {
53 TouchFactory::SetTouchDeviceListFromCommandLine();
56 X11Window::~X11Window() {
59 void X11Window::Destroy() {
63 // Stop processing events.
64 PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
65 XID xwindow
= xwindow_
;
66 XDisplay
* xdisplay
= xdisplay_
;
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
))
77 EventType event_type
= EventTypeFromNative(xev
);
80 case ET_KEY_RELEASED
: {
81 KeyEvent
key_event(xev
);
82 delegate_
->DispatchEvent(&key_event
);
85 case ET_MOUSE_PRESSED
:
87 case ET_MOUSE_DRAGGED
:
88 case ET_MOUSE_RELEASED
: {
89 MouseEvent
mouse_event(xev
);
90 delegate_
->DispatchEvent(&mouse_event
);
94 MouseWheelEvent
wheel_event(xev
);
95 delegate_
->DispatchEvent(&wheel_event
);
98 case ET_SCROLL_FLING_START
:
99 case ET_SCROLL_FLING_CANCEL
:
101 ScrollEvent
scroll_event(xev
);
102 delegate_
->DispatchEvent(&scroll_event
);
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
);
118 void X11Window::Show() {
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_
,
131 requested_bounds_
.x(),
132 requested_bounds_
.y(),
133 requested_bounds_
.width(),
134 requested_bounds_
.height(),
136 CopyFromParent
, // depth
138 CopyFromParent
, // visual
139 CWBackPixmap
| CWOverrideRedirect
,
142 long event_mask
= ButtonPressMask
| ButtonReleaseMask
| FocusChangeMask
|
143 KeyPressMask
| KeyReleaseMask
| EnterWindowMask
|
144 LeaveWindowMask
| ExposureMask
| VisibilityChangeMask
|
145 StructureNotifyMask
| PropertyChangeMask
|
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
);
162 evmask
.deviceid
= XIAllDevices
;
163 evmask
.mask_len
= sizeof(mask
);
165 XISelectEvents(xdisplay_
, xwindow_
, &evmask
, 1);
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.
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");
184 XChangeProperty(xdisplay_
,
186 atom_cache_
.GetAtom("_NET_WM_PID"),
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
211 if (X11EventSource::GetInstance())
212 X11EventSource::GetInstance()->BlockUntilWindowMapped(xwindow_
);
213 window_mapped_
= true;
216 void X11Window::Hide() {
219 XWithdrawWindow(xdisplay_
, xwindow_
, 0);
220 window_mapped_
= false;
223 void X11Window::Close() {
227 void X11Window::SetBounds(const gfx::Rect
& bounds
) {
228 requested_bounds_
= bounds
;
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
) {
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
);
280 MouseEvent
mouse_event(xev
);
281 delegate_
->DispatchEvent(&mouse_event
);
286 gfx::Rect
damage_rect(xev
->xexpose
.x
,
289 xev
->xexpose
.height
);
290 delegate_
->OnDamageRect(damage_rect
);
296 KeyEvent
key_event(xev
);
297 delegate_
->DispatchEvent(&key_event
);
302 case ButtonRelease
: {
303 switch (EventTypeFromNative(xev
)) {
304 case ET_MOUSEWHEEL
: {
305 MouseWheelEvent
mouseev(xev
);
306 delegate_
->DispatchEvent(&mouseev
);
309 case ET_MOUSE_PRESSED
:
310 case ET_MOUSE_RELEASED
: {
311 MouseEvent
mouseev(xev
);
312 delegate_
->DispatchEvent(&mouseev
);
316 // No event is created for X11-release events for mouse-wheel
326 if (xev
->xfocus
.mode
!= NotifyGrab
)
327 delegate_
->OnLostCapture();
330 case ConfigureNotify
: {
331 DCHECK_EQ(xwindow_
, xev
->xconfigure
.event
);
332 DCHECK_EQ(xwindow_
, xev
->xconfigure
.window
);
333 gfx::Rect
bounds(xev
->xconfigure
.x
,
335 xev
->xconfigure
.width
,
336 xev
->xconfigure
.height
);
337 if (confirmed_bounds_
!= bounds
) {
338 confirmed_bounds_
= bounds
;
339 delegate_
->OnBoundsChanged(confirmed_bounds_
);
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
,
355 SubstructureRedirectMask
| SubstructureNotifyMask
,
363 ProcessXInput2Event(xev
);
367 return POST_DISPATCH_STOP_PROPAGATION
;
372 void SetUseOverrideRedirectWindowByDefault(bool override_redirect
) {
373 g_override_redirect
= override_redirect
;