Popular sites on the NTP: Favicon improvements
[chromium-blink-merge.git] / ui / views / widget / desktop_aura / desktop_window_tree_host_x11.cc
blob4a6931050b59b44eacd92cff30aaf7664f3552ad
1 // Copyright (c) 2012 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/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
7 #include <X11/extensions/shape.h>
8 #include <X11/extensions/XInput2.h>
9 #include <X11/Xatom.h>
10 #include <X11/Xregion.h>
11 #include <X11/Xutil.h>
13 #include "base/basictypes.h"
14 #include "base/command_line.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/trace_event/trace_event.h"
18 #include "third_party/skia/include/core/SkPath.h"
19 #include "ui/aura/client/cursor_client.h"
20 #include "ui/aura/client/focus_client.h"
21 #include "ui/aura/window.h"
22 #include "ui/aura/window_event_dispatcher.h"
23 #include "ui/aura/window_property.h"
24 #include "ui/base/dragdrop/os_exchange_data_provider_aurax11.h"
25 #include "ui/base/hit_test.h"
26 #include "ui/base/ime/input_method.h"
27 #include "ui/base/x/x11_util.h"
28 #include "ui/events/devices/x11/device_data_manager_x11.h"
29 #include "ui/events/devices/x11/device_list_cache_x11.h"
30 #include "ui/events/devices/x11/touch_factory_x11.h"
31 #include "ui/events/event_utils.h"
32 #include "ui/events/platform/platform_event_source.h"
33 #include "ui/events/platform/x11/x11_event_source.h"
34 #include "ui/gfx/display.h"
35 #include "ui/gfx/geometry/insets.h"
36 #include "ui/gfx/geometry/size_conversions.h"
37 #include "ui/gfx/image/image_skia.h"
38 #include "ui/gfx/image/image_skia_rep.h"
39 #include "ui/gfx/path.h"
40 #include "ui/gfx/path_x11.h"
41 #include "ui/gfx/screen.h"
42 #include "ui/native_theme/native_theme.h"
43 #include "ui/views/corewm/tooltip_aura.h"
44 #include "ui/views/linux_ui/linux_ui.h"
45 #include "ui/views/views_delegate.h"
46 #include "ui/views/views_switches.h"
47 #include "ui/views/widget/desktop_aura/desktop_drag_drop_client_aurax11.h"
48 #include "ui/views/widget/desktop_aura/desktop_native_cursor_manager.h"
49 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
50 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_observer_x11.h"
51 #include "ui/views/widget/desktop_aura/x11_desktop_handler.h"
52 #include "ui/views/widget/desktop_aura/x11_desktop_window_move_client.h"
53 #include "ui/views/widget/desktop_aura/x11_pointer_grab.h"
54 #include "ui/views/widget/desktop_aura/x11_window_event_filter.h"
55 #include "ui/wm/core/compound_event_filter.h"
56 #include "ui/wm/core/window_util.h"
58 DECLARE_WINDOW_PROPERTY_TYPE(views::DesktopWindowTreeHostX11*);
60 namespace views {
62 DesktopWindowTreeHostX11* DesktopWindowTreeHostX11::g_current_capture =
63 NULL;
64 std::list<XID>* DesktopWindowTreeHostX11::open_windows_ = NULL;
66 DEFINE_WINDOW_PROPERTY_KEY(
67 aura::Window*, kViewsWindowForRootWindow, NULL);
69 DEFINE_WINDOW_PROPERTY_KEY(
70 DesktopWindowTreeHostX11*, kHostForRootWindow, NULL);
72 namespace {
74 // Constants that are part of EWMH.
75 const int k_NET_WM_STATE_ADD = 1;
76 const int k_NET_WM_STATE_REMOVE = 0;
78 // Special value of the _NET_WM_DESKTOP property which indicates that the window
79 // should appear on all desktops.
80 const int kAllDesktops = 0xFFFFFFFF;
82 const char* kAtomsToCache[] = {
83 "UTF8_STRING",
84 "WM_DELETE_WINDOW",
85 "WM_PROTOCOLS",
86 "_NET_FRAME_EXTENTS",
87 "_NET_WM_CM_S0",
88 "_NET_WM_DESKTOP",
89 "_NET_WM_ICON",
90 "_NET_WM_NAME",
91 "_NET_WM_PID",
92 "_NET_WM_PING",
93 "_NET_WM_STATE",
94 "_NET_WM_STATE_ABOVE",
95 "_NET_WM_STATE_FULLSCREEN",
96 "_NET_WM_STATE_HIDDEN",
97 "_NET_WM_STATE_MAXIMIZED_HORZ",
98 "_NET_WM_STATE_MAXIMIZED_VERT",
99 "_NET_WM_STATE_SKIP_TASKBAR",
100 "_NET_WM_STATE_STICKY",
101 "_NET_WM_USER_TIME",
102 "_NET_WM_WINDOW_OPACITY",
103 "_NET_WM_WINDOW_TYPE",
104 "_NET_WM_WINDOW_TYPE_DND",
105 "_NET_WM_WINDOW_TYPE_MENU",
106 "_NET_WM_WINDOW_TYPE_NORMAL",
107 "_NET_WM_WINDOW_TYPE_NOTIFICATION",
108 "_NET_WM_WINDOW_TYPE_TOOLTIP",
109 "XdndActionAsk",
110 "XdndActionCopy"
111 "XdndActionLink",
112 "XdndActionList",
113 "XdndActionMove",
114 "XdndActionPrivate",
115 "XdndAware",
116 "XdndDrop",
117 "XdndEnter",
118 "XdndFinished",
119 "XdndLeave",
120 "XdndPosition",
121 "XdndProxy", // Proxy windows?
122 "XdndSelection",
123 "XdndStatus",
124 "XdndTypeList",
125 NULL
128 const char kX11WindowRolePopup[] = "popup";
129 const char kX11WindowRoleBubble[] = "bubble";
131 // Returns the whole path from |window| to the root.
132 std::vector<::Window> GetParentsList(XDisplay* xdisplay, ::Window window) {
133 ::Window parent_win, root_win;
134 Window* child_windows;
135 unsigned int num_child_windows;
136 std::vector<::Window> result;
138 while (window) {
139 result.push_back(window);
140 if (!XQueryTree(xdisplay, window,
141 &root_win, &parent_win, &child_windows, &num_child_windows))
142 break;
143 if (child_windows)
144 XFree(child_windows);
145 window = parent_win;
147 return result;
150 } // namespace
152 ////////////////////////////////////////////////////////////////////////////////
153 // DesktopWindowTreeHostX11, public:
155 DesktopWindowTreeHostX11::DesktopWindowTreeHostX11(
156 internal::NativeWidgetDelegate* native_widget_delegate,
157 DesktopNativeWidgetAura* desktop_native_widget_aura)
158 : xdisplay_(gfx::GetXDisplay()),
159 xwindow_(0),
160 x_root_window_(DefaultRootWindow(xdisplay_)),
161 atom_cache_(xdisplay_, kAtomsToCache),
162 window_mapped_(false),
163 is_fullscreen_(false),
164 is_always_on_top_(false),
165 use_native_frame_(false),
166 should_maximize_after_map_(false),
167 use_argb_visual_(false),
168 drag_drop_client_(NULL),
169 native_widget_delegate_(native_widget_delegate),
170 desktop_native_widget_aura_(desktop_native_widget_aura),
171 content_window_(NULL),
172 window_parent_(NULL),
173 custom_window_shape_(false),
174 urgency_hint_set_(false),
175 close_widget_factory_(this) {
178 DesktopWindowTreeHostX11::~DesktopWindowTreeHostX11() {
179 window()->ClearProperty(kHostForRootWindow);
180 aura::client::SetWindowMoveClient(window(), NULL);
181 desktop_native_widget_aura_->OnDesktopWindowTreeHostDestroyed(this);
182 DestroyDispatcher();
185 // static
186 aura::Window* DesktopWindowTreeHostX11::GetContentWindowForXID(XID xid) {
187 aura::WindowTreeHost* host =
188 aura::WindowTreeHost::GetForAcceleratedWidget(xid);
189 return host ? host->window()->GetProperty(kViewsWindowForRootWindow) : NULL;
192 // static
193 DesktopWindowTreeHostX11* DesktopWindowTreeHostX11::GetHostForXID(XID xid) {
194 aura::WindowTreeHost* host =
195 aura::WindowTreeHost::GetForAcceleratedWidget(xid);
196 return host ? host->window()->GetProperty(kHostForRootWindow) : NULL;
199 // static
200 std::vector<aura::Window*> DesktopWindowTreeHostX11::GetAllOpenWindows() {
201 std::vector<aura::Window*> windows(open_windows().size());
202 std::transform(open_windows().begin(),
203 open_windows().end(),
204 windows.begin(),
205 GetContentWindowForXID);
206 return windows;
209 gfx::Rect DesktopWindowTreeHostX11::GetX11RootWindowBounds() const {
210 return bounds_in_pixels_;
213 gfx::Rect DesktopWindowTreeHostX11::GetX11RootWindowOuterBounds() const {
214 gfx::Rect outer_bounds(bounds_in_pixels_);
215 outer_bounds.Inset(-native_window_frame_borders_in_pixels_);
216 return outer_bounds;
219 ::Region DesktopWindowTreeHostX11::GetWindowShape() const {
220 return window_shape_.get();
223 void DesktopWindowTreeHostX11::HandleNativeWidgetActivationChanged(
224 bool active) {
225 if (active) {
226 FlashFrame(false);
227 OnHostActivated();
228 open_windows().remove(xwindow_);
229 open_windows().insert(open_windows().begin(), xwindow_);
230 } else {
231 ReleaseCapture();
234 desktop_native_widget_aura_->HandleActivationChanged(active);
236 native_widget_delegate_->AsWidget()->GetRootView()->SchedulePaint();
239 void DesktopWindowTreeHostX11::AddObserver(
240 views::DesktopWindowTreeHostObserverX11* observer) {
241 observer_list_.AddObserver(observer);
244 void DesktopWindowTreeHostX11::RemoveObserver(
245 views::DesktopWindowTreeHostObserverX11* observer) {
246 observer_list_.RemoveObserver(observer);
249 void DesktopWindowTreeHostX11::SwapNonClientEventHandler(
250 scoped_ptr<ui::EventHandler> handler) {
251 wm::CompoundEventFilter* compound_event_filter =
252 desktop_native_widget_aura_->root_window_event_filter();
253 if (x11_non_client_event_filter_)
254 compound_event_filter->RemoveHandler(x11_non_client_event_filter_.get());
255 compound_event_filter->AddHandler(handler.get());
256 x11_non_client_event_filter_ = handler.Pass();
259 void DesktopWindowTreeHostX11::CleanUpWindowList(
260 void (*func)(aura::Window* window)) {
261 if (!open_windows_)
262 return;
263 while (!open_windows_->empty()) {
264 XID xid = open_windows_->front();
265 func(GetContentWindowForXID(xid));
266 if (!open_windows_->empty() && open_windows_->front() == xid)
267 open_windows_->erase(open_windows_->begin());
270 delete open_windows_;
271 open_windows_ = NULL;
274 ////////////////////////////////////////////////////////////////////////////////
275 // DesktopWindowTreeHostX11, DesktopWindowTreeHost implementation:
277 void DesktopWindowTreeHostX11::Init(aura::Window* content_window,
278 const Widget::InitParams& params) {
279 content_window_ = content_window;
281 // TODO(erg): Check whether we *should* be building a WindowTreeHost here, or
282 // whether we should be proxying requests to another DRWHL.
284 // In some situations, views tries to make a zero sized window, and that
285 // makes us crash. Make sure we have valid sizes.
286 Widget::InitParams sanitized_params = params;
287 if (sanitized_params.bounds.width() == 0)
288 sanitized_params.bounds.set_width(100);
289 if (sanitized_params.bounds.height() == 0)
290 sanitized_params.bounds.set_height(100);
292 InitX11Window(sanitized_params);
295 void DesktopWindowTreeHostX11::OnNativeWidgetCreated(
296 const Widget::InitParams& params) {
297 window()->SetProperty(kViewsWindowForRootWindow, content_window_);
298 window()->SetProperty(kHostForRootWindow, this);
300 // Ensure that the X11DesktopHandler exists so that it dispatches activation
301 // messages to us.
302 X11DesktopHandler::get();
304 // TODO(erg): Unify this code once the other consumer goes away.
305 SwapNonClientEventHandler(
306 scoped_ptr<ui::EventHandler>(new X11WindowEventFilter(this)).Pass());
307 SetUseNativeFrame(params.type == Widget::InitParams::TYPE_WINDOW &&
308 !params.remove_standard_frame);
310 x11_window_move_client_.reset(new X11DesktopWindowMoveClient);
311 aura::client::SetWindowMoveClient(window(), x11_window_move_client_.get());
313 SetWindowTransparency();
315 native_widget_delegate_->OnNativeWidgetCreated(true);
318 scoped_ptr<corewm::Tooltip> DesktopWindowTreeHostX11::CreateTooltip() {
319 return make_scoped_ptr(new corewm::TooltipAura);
322 scoped_ptr<aura::client::DragDropClient>
323 DesktopWindowTreeHostX11::CreateDragDropClient(
324 DesktopNativeCursorManager* cursor_manager) {
325 drag_drop_client_ = new DesktopDragDropClientAuraX11(
326 window(), cursor_manager, xdisplay_, xwindow_);
327 drag_drop_client_->Init();
328 return make_scoped_ptr(drag_drop_client_);
331 void DesktopWindowTreeHostX11::Close() {
332 // TODO(erg): Might need to do additional hiding tasks here.
333 delayed_resize_task_.Cancel();
335 if (!close_widget_factory_.HasWeakPtrs()) {
336 // And we delay the close so that if we are called from an ATL callback,
337 // we don't destroy the window before the callback returned (as the caller
338 // may delete ourselves on destroy and the ATL callback would still
339 // dereference us when the callback returns).
340 base::MessageLoop::current()->PostTask(
341 FROM_HERE,
342 base::Bind(&DesktopWindowTreeHostX11::CloseNow,
343 close_widget_factory_.GetWeakPtr()));
347 void DesktopWindowTreeHostX11::CloseNow() {
348 if (xwindow_ == None)
349 return;
351 ReleaseCapture();
352 native_widget_delegate_->OnNativeWidgetDestroying();
354 // If we have children, close them. Use a copy for iteration because they'll
355 // remove themselves.
356 std::set<DesktopWindowTreeHostX11*> window_children_copy = window_children_;
357 for (std::set<DesktopWindowTreeHostX11*>::iterator it =
358 window_children_copy.begin(); it != window_children_copy.end();
359 ++it) {
360 (*it)->CloseNow();
362 DCHECK(window_children_.empty());
364 // If we have a parent, remove ourselves from its children list.
365 if (window_parent_) {
366 window_parent_->window_children_.erase(this);
367 window_parent_ = NULL;
370 // Remove the event listeners we've installed. We need to remove these
371 // because otherwise we get assert during ~WindowEventDispatcher().
372 desktop_native_widget_aura_->root_window_event_filter()->RemoveHandler(
373 x11_non_client_event_filter_.get());
374 x11_non_client_event_filter_.reset();
376 // Destroy the compositor before destroying the |xwindow_| since shutdown
377 // may try to swap, and the swap without a window causes an X error, which
378 // causes a crash with in-process renderer.
379 DestroyCompositor();
381 open_windows().remove(xwindow_);
382 // Actually free our native resources.
383 if (ui::PlatformEventSource::GetInstance())
384 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
385 XDestroyWindow(xdisplay_, xwindow_);
386 xwindow_ = None;
388 desktop_native_widget_aura_->OnHostClosed();
391 aura::WindowTreeHost* DesktopWindowTreeHostX11::AsWindowTreeHost() {
392 return this;
395 void DesktopWindowTreeHostX11::ShowWindowWithState(
396 ui::WindowShowState show_state) {
397 if (compositor())
398 compositor()->SetVisible(true);
399 if (!window_mapped_)
400 MapWindow(show_state);
402 switch (show_state) {
403 case ui::SHOW_STATE_NORMAL:
404 Activate();
405 break;
406 case ui::SHOW_STATE_MAXIMIZED:
407 Maximize();
408 break;
409 case ui::SHOW_STATE_MINIMIZED:
410 Minimize();
411 break;
412 case ui::SHOW_STATE_FULLSCREEN:
413 SetFullscreen(true);
414 break;
415 default:
416 break;
419 native_widget_delegate_->AsWidget()->SetInitialFocus(show_state);
422 void DesktopWindowTreeHostX11::ShowMaximizedWithBounds(
423 const gfx::Rect& restored_bounds) {
424 ShowWindowWithState(ui::SHOW_STATE_MAXIMIZED);
425 // Enforce |restored_bounds_in_pixels_| since calling Maximize() could have
426 // reset it.
427 restored_bounds_in_pixels_ = ToPixelRect(restored_bounds);
430 bool DesktopWindowTreeHostX11::IsVisible() const {
431 return window_mapped_;
434 void DesktopWindowTreeHostX11::SetSize(const gfx::Size& requested_size) {
435 gfx::Size size_in_pixels = ToPixelRect(gfx::Rect(requested_size)).size();
436 size_in_pixels = AdjustSize(size_in_pixels);
437 bool size_changed = bounds_in_pixels_.size() != size_in_pixels;
438 XResizeWindow(xdisplay_, xwindow_, size_in_pixels.width(),
439 size_in_pixels.height());
440 bounds_in_pixels_.set_size(size_in_pixels);
441 if (size_changed) {
442 OnHostResized(size_in_pixels);
443 ResetWindowRegion();
447 void DesktopWindowTreeHostX11::StackAbove(aura::Window* window) {
448 if (window && window->GetRootWindow()) {
449 ::Window window_below = window->GetHost()->GetAcceleratedWidget();
450 // Find all parent windows up to the root.
451 std::vector<::Window> window_below_parents =
452 GetParentsList(xdisplay_, window_below);
453 std::vector<::Window> window_above_parents =
454 GetParentsList(xdisplay_, xwindow_);
456 // Find their common ancestor.
457 auto it_below_window = window_below_parents.rbegin();
458 auto it_above_window = window_above_parents.rbegin();
459 for (; it_below_window != window_below_parents.rend() &&
460 it_above_window != window_above_parents.rend() &&
461 *it_below_window == *it_above_window;
462 ++it_below_window, ++it_above_window) {
465 if (it_below_window != window_below_parents.rend() &&
466 it_above_window != window_above_parents.rend()) {
467 // First stack |xwindow_| below so Z-order of |window| stays the same.
468 ::Window windows[] = {*it_below_window, *it_above_window};
469 if (XRestackWindows(xdisplay_, windows, 2) == 0) {
470 // Now stack them properly.
471 std::swap(windows[0], windows[1]);
472 XRestackWindows(xdisplay_, windows, 2);
478 void DesktopWindowTreeHostX11::StackAtTop() {
479 XRaiseWindow(xdisplay_, xwindow_);
482 void DesktopWindowTreeHostX11::CenterWindow(const gfx::Size& size) {
483 gfx::Size size_in_pixels = ToPixelRect(gfx::Rect(size)).size();
484 gfx::Rect parent_bounds_in_pixels = GetWorkAreaBoundsInPixels();
486 // If |window_|'s transient parent bounds are big enough to contain |size|,
487 // use them instead.
488 if (wm::GetTransientParent(content_window_)) {
489 gfx::Rect transient_parent_rect =
490 wm::GetTransientParent(content_window_)->GetBoundsInScreen();
491 if (transient_parent_rect.height() >= size.height() &&
492 transient_parent_rect.width() >= size.width()) {
493 parent_bounds_in_pixels = ToPixelRect(transient_parent_rect);
497 gfx::Rect window_bounds_in_pixels(
498 parent_bounds_in_pixels.x() +
499 (parent_bounds_in_pixels.width() - size_in_pixels.width()) / 2,
500 parent_bounds_in_pixels.y() +
501 (parent_bounds_in_pixels.height() - size_in_pixels.height()) / 2,
502 size_in_pixels.width(), size_in_pixels.height());
503 // Don't size the window bigger than the parent, otherwise the user may not be
504 // able to close or move it.
505 window_bounds_in_pixels.AdjustToFit(parent_bounds_in_pixels);
507 SetBounds(window_bounds_in_pixels);
510 void DesktopWindowTreeHostX11::GetWindowPlacement(
511 gfx::Rect* bounds,
512 ui::WindowShowState* show_state) const {
513 *bounds = GetRestoredBounds();
515 if (IsFullscreen()) {
516 *show_state = ui::SHOW_STATE_FULLSCREEN;
517 } else if (IsMinimized()) {
518 *show_state = ui::SHOW_STATE_MINIMIZED;
519 } else if (IsMaximized()) {
520 *show_state = ui::SHOW_STATE_MAXIMIZED;
521 } else if (!IsActive()) {
522 *show_state = ui::SHOW_STATE_INACTIVE;
523 } else {
524 *show_state = ui::SHOW_STATE_NORMAL;
528 gfx::Rect DesktopWindowTreeHostX11::GetWindowBoundsInScreen() const {
529 return ToDIPRect(bounds_in_pixels_);
532 gfx::Rect DesktopWindowTreeHostX11::GetClientAreaBoundsInScreen() const {
533 // TODO(erg): The NativeWidgetAura version returns |bounds_in_pixels_|,
534 // claiming it's needed for View::ConvertPointToScreen() to work correctly.
535 // DesktopWindowTreeHostWin::GetClientAreaBoundsInScreen() just asks windows
536 // what it thinks the client rect is.
538 // Attempts to calculate the rect by asking the NonClientFrameView what it
539 // thought its GetBoundsForClientView() were broke combobox drop down
540 // placement.
541 return GetWindowBoundsInScreen();
544 gfx::Rect DesktopWindowTreeHostX11::GetRestoredBounds() const {
545 // We can't reliably track the restored bounds of a window, but we can get
546 // the 90% case down. When *chrome* is the process that requests maximizing
547 // or restoring bounds, we can record the current bounds before we request
548 // maximization, and clear it when we detect a state change.
549 if (!restored_bounds_in_pixels_.IsEmpty())
550 return ToDIPRect(restored_bounds_in_pixels_);
552 return GetWindowBoundsInScreen();
555 gfx::Rect DesktopWindowTreeHostX11::GetWorkAreaBoundsInScreen() const {
556 return ToDIPRect(GetWorkAreaBoundsInPixels());
559 void DesktopWindowTreeHostX11::SetShape(SkRegion* native_region) {
560 custom_window_shape_ = false;
561 window_shape_.reset();
563 if (native_region) {
564 gfx::Transform transform = GetRootTransform();
565 if (!transform.IsIdentity() && !native_region->isEmpty()) {
566 SkPath path_in_dip;
567 if (native_region->getBoundaryPath(&path_in_dip)) {
568 SkPath path_in_pixels;
569 path_in_dip.transform(transform.matrix(), &path_in_pixels);
570 window_shape_.reset(gfx::CreateRegionFromSkPath(path_in_pixels));
571 } else {
572 window_shape_.reset(XCreateRegion());
574 } else {
575 window_shape_.reset(gfx::CreateRegionFromSkRegion(*native_region));
578 custom_window_shape_ = true;
579 delete native_region;
581 ResetWindowRegion();
584 void DesktopWindowTreeHostX11::Activate() {
585 if (!window_mapped_)
586 return;
588 X11DesktopHandler::get()->ActivateWindow(xwindow_);
591 void DesktopWindowTreeHostX11::Deactivate() {
592 if (!IsActive())
593 return;
595 ReleaseCapture();
596 X11DesktopHandler::get()->DeactivateWindow(xwindow_);
599 bool DesktopWindowTreeHostX11::IsActive() const {
600 return X11DesktopHandler::get()->IsActiveWindow(xwindow_);
603 void DesktopWindowTreeHostX11::Maximize() {
604 if (HasWMSpecProperty("_NET_WM_STATE_FULLSCREEN")) {
605 // Unfullscreen the window if it is fullscreen.
606 SetWMSpecState(false,
607 atom_cache_.GetAtom("_NET_WM_STATE_FULLSCREEN"),
608 None);
610 // Resize the window so that it does not have the same size as a monitor.
611 // (Otherwise, some window managers immediately put the window back in
612 // fullscreen mode).
613 gfx::Rect adjusted_bounds_in_pixels(bounds_in_pixels_.origin(),
614 AdjustSize(bounds_in_pixels_.size()));
615 if (adjusted_bounds_in_pixels != bounds_in_pixels_)
616 SetBounds(adjusted_bounds_in_pixels);
619 // Some WMs do not respect maximization hints on unmapped windows, so we
620 // save this one for later too.
621 should_maximize_after_map_ = !window_mapped_;
623 // When we are in the process of requesting to maximize a window, we can
624 // accurately keep track of our restored bounds instead of relying on the
625 // heuristics that are in the PropertyNotify and ConfigureNotify handlers.
626 restored_bounds_in_pixels_ = bounds_in_pixels_;
628 SetWMSpecState(true,
629 atom_cache_.GetAtom("_NET_WM_STATE_MAXIMIZED_VERT"),
630 atom_cache_.GetAtom("_NET_WM_STATE_MAXIMIZED_HORZ"));
631 if (IsMinimized())
632 ShowWindowWithState(ui::SHOW_STATE_NORMAL);
635 void DesktopWindowTreeHostX11::Minimize() {
636 ReleaseCapture();
637 XIconifyWindow(xdisplay_, xwindow_, 0);
640 void DesktopWindowTreeHostX11::Restore() {
641 should_maximize_after_map_ = false;
642 SetWMSpecState(false,
643 atom_cache_.GetAtom("_NET_WM_STATE_MAXIMIZED_VERT"),
644 atom_cache_.GetAtom("_NET_WM_STATE_MAXIMIZED_HORZ"));
645 if (IsMinimized())
646 ShowWindowWithState(ui::SHOW_STATE_NORMAL);
649 bool DesktopWindowTreeHostX11::IsMaximized() const {
650 return (HasWMSpecProperty("_NET_WM_STATE_MAXIMIZED_VERT") &&
651 HasWMSpecProperty("_NET_WM_STATE_MAXIMIZED_HORZ"));
654 bool DesktopWindowTreeHostX11::IsMinimized() const {
655 return HasWMSpecProperty("_NET_WM_STATE_HIDDEN");
658 bool DesktopWindowTreeHostX11::HasCapture() const {
659 return g_current_capture == this;
662 void DesktopWindowTreeHostX11::SetAlwaysOnTop(bool always_on_top) {
663 is_always_on_top_ = always_on_top;
664 SetWMSpecState(always_on_top,
665 atom_cache_.GetAtom("_NET_WM_STATE_ABOVE"),
666 None);
669 bool DesktopWindowTreeHostX11::IsAlwaysOnTop() const {
670 return is_always_on_top_;
673 void DesktopWindowTreeHostX11::SetVisibleOnAllWorkspaces(bool always_visible) {
674 SetWMSpecState(always_visible,
675 atom_cache_.GetAtom("_NET_WM_STATE_STICKY"),
676 None);
678 int new_desktop = 0;
679 if (always_visible) {
680 new_desktop = kAllDesktops;
681 } else {
682 if (!ui::GetCurrentDesktop(&new_desktop))
683 return;
686 XEvent xevent;
687 memset (&xevent, 0, sizeof (xevent));
688 xevent.type = ClientMessage;
689 xevent.xclient.window = xwindow_;
690 xevent.xclient.message_type = atom_cache_.GetAtom("_NET_WM_DESKTOP");
691 xevent.xclient.format = 32;
692 xevent.xclient.data.l[0] = new_desktop;
693 xevent.xclient.data.l[1] = 0;
694 xevent.xclient.data.l[2] = 0;
695 xevent.xclient.data.l[3] = 0;
696 xevent.xclient.data.l[4] = 0;
697 XSendEvent(xdisplay_, x_root_window_, False,
698 SubstructureRedirectMask | SubstructureNotifyMask,
699 &xevent);
702 bool DesktopWindowTreeHostX11::SetWindowTitle(const base::string16& title) {
703 if (window_title_ == title)
704 return false;
705 window_title_ = title;
706 std::string utf8str = base::UTF16ToUTF8(title);
707 XChangeProperty(xdisplay_,
708 xwindow_,
709 atom_cache_.GetAtom("_NET_WM_NAME"),
710 atom_cache_.GetAtom("UTF8_STRING"),
712 PropModeReplace,
713 reinterpret_cast<const unsigned char*>(utf8str.c_str()),
714 utf8str.size());
715 XTextProperty xtp;
716 char *c_utf8_str = const_cast<char *>(utf8str.c_str());
717 if (Xutf8TextListToTextProperty(xdisplay_, &c_utf8_str, 1,
718 XUTF8StringStyle, &xtp) == Success) {
719 XSetWMName(xdisplay_, xwindow_, &xtp);
720 XFree(xtp.value);
722 return true;
725 void DesktopWindowTreeHostX11::ClearNativeFocus() {
726 // This method is weird and misnamed. Instead of clearing the native focus,
727 // it sets the focus to our |content_window_|, which will trigger a cascade
728 // of focus changes into views.
729 if (content_window_ && aura::client::GetFocusClient(content_window_) &&
730 content_window_->Contains(
731 aura::client::GetFocusClient(content_window_)->GetFocusedWindow())) {
732 aura::client::GetFocusClient(content_window_)->FocusWindow(content_window_);
736 Widget::MoveLoopResult DesktopWindowTreeHostX11::RunMoveLoop(
737 const gfx::Vector2d& drag_offset,
738 Widget::MoveLoopSource source,
739 Widget::MoveLoopEscapeBehavior escape_behavior) {
740 aura::client::WindowMoveSource window_move_source =
741 source == Widget::MOVE_LOOP_SOURCE_MOUSE ?
742 aura::client::WINDOW_MOVE_SOURCE_MOUSE :
743 aura::client::WINDOW_MOVE_SOURCE_TOUCH;
744 if (x11_window_move_client_->RunMoveLoop(content_window_, drag_offset,
745 window_move_source) == aura::client::MOVE_SUCCESSFUL)
746 return Widget::MOVE_LOOP_SUCCESSFUL;
748 return Widget::MOVE_LOOP_CANCELED;
751 void DesktopWindowTreeHostX11::EndMoveLoop() {
752 x11_window_move_client_->EndMoveLoop();
755 void DesktopWindowTreeHostX11::SetVisibilityChangedAnimationsEnabled(
756 bool value) {
757 // Much like the previous NativeWidgetGtk, we don't have anything to do here.
760 bool DesktopWindowTreeHostX11::ShouldUseNativeFrame() const {
761 return use_native_frame_;
764 bool DesktopWindowTreeHostX11::ShouldWindowContentsBeTransparent() const {
765 return false;
768 void DesktopWindowTreeHostX11::FrameTypeChanged() {
769 Widget::FrameType new_type =
770 native_widget_delegate_->AsWidget()->frame_type();
771 if (new_type == Widget::FRAME_TYPE_DEFAULT) {
772 // The default is determined by Widget::InitParams::remove_standard_frame
773 // and does not change.
774 return;
777 SetUseNativeFrame(new_type == Widget::FRAME_TYPE_FORCE_NATIVE);
778 // Replace the frame and layout the contents. Even though we don't have a
779 // swapable glass frame like on Windows, we still replace the frame because
780 // the button assets don't update otherwise.
781 native_widget_delegate_->AsWidget()->non_client_view()->UpdateFrame();
784 void DesktopWindowTreeHostX11::SetFullscreen(bool fullscreen) {
785 if (is_fullscreen_ == fullscreen)
786 return;
787 is_fullscreen_ = fullscreen;
788 if (is_fullscreen_)
789 delayed_resize_task_.Cancel();
791 // Work around a bug where if we try to unfullscreen, metacity immediately
792 // fullscreens us again. This is a little flickery and not necessary if
793 // there's a gnome-panel, but it's not easy to detect whether there's a
794 // panel or not.
795 bool unmaximize_and_remaximize = !fullscreen && IsMaximized() &&
796 ui::GuessWindowManager() == ui::WM_METACITY;
798 if (unmaximize_and_remaximize)
799 Restore();
800 SetWMSpecState(fullscreen,
801 atom_cache_.GetAtom("_NET_WM_STATE_FULLSCREEN"),
802 None);
803 if (unmaximize_and_remaximize)
804 Maximize();
806 // Try to guess the size we will have after the switch to/from fullscreen:
807 // - (may) avoid transient states
808 // - works around Flash content which expects to have the size updated
809 // synchronously.
810 // See https://crbug.com/361408
811 if (fullscreen) {
812 restored_bounds_in_pixels_ = bounds_in_pixels_;
813 const gfx::Display display =
814 gfx::Screen::GetScreenFor(NULL)->GetDisplayNearestWindow(window());
815 bounds_in_pixels_ = ToPixelRect(display.bounds());
816 } else {
817 bounds_in_pixels_ = restored_bounds_in_pixels_;
819 OnHostMoved(bounds_in_pixels_.origin());
820 OnHostResized(bounds_in_pixels_.size());
822 if (HasWMSpecProperty("_NET_WM_STATE_FULLSCREEN") == fullscreen) {
823 Relayout();
824 ResetWindowRegion();
826 // Else: the widget will be relaid out either when the window bounds change or
827 // when |xwindow_|'s fullscreen state changes.
830 bool DesktopWindowTreeHostX11::IsFullscreen() const {
831 return is_fullscreen_;
834 void DesktopWindowTreeHostX11::SetOpacity(unsigned char opacity) {
835 // X server opacity is in terms of 32 bit unsigned int space, and counts from
836 // the opposite direction.
837 // XChangeProperty() expects "cardinality" to be long.
838 unsigned long cardinality = opacity * 0x1010101;
840 if (cardinality == 0xffffffff) {
841 XDeleteProperty(xdisplay_, xwindow_,
842 atom_cache_.GetAtom("_NET_WM_WINDOW_OPACITY"));
843 } else {
844 XChangeProperty(xdisplay_, xwindow_,
845 atom_cache_.GetAtom("_NET_WM_WINDOW_OPACITY"),
846 XA_CARDINAL, 32,
847 PropModeReplace,
848 reinterpret_cast<unsigned char*>(&cardinality), 1);
852 void DesktopWindowTreeHostX11::SetWindowIcons(
853 const gfx::ImageSkia& window_icon, const gfx::ImageSkia& app_icon) {
854 // TODO(erg): The way we handle icons across different versions of chrome
855 // could be substantially improved. The Windows version does its own thing
856 // and only sometimes comes down this code path. The icon stuff in
857 // ChromeViewsDelegate is hard coded to use HICONs. Likewise, we're hard
858 // coded to be given two images instead of an arbitrary collection of images
859 // so that we can pass to the WM.
861 // All of this could be made much, much better.
862 std::vector<unsigned long> data;
864 if (window_icon.HasRepresentation(1.0f))
865 SerializeImageRepresentation(window_icon.GetRepresentation(1.0f), &data);
867 if (app_icon.HasRepresentation(1.0f))
868 SerializeImageRepresentation(app_icon.GetRepresentation(1.0f), &data);
870 if (!data.empty())
871 ui::SetAtomArrayProperty(xwindow_, "_NET_WM_ICON", "CARDINAL", data);
874 void DesktopWindowTreeHostX11::InitModalType(ui::ModalType modal_type) {
875 switch (modal_type) {
876 case ui::MODAL_TYPE_NONE:
877 break;
878 default:
879 // TODO(erg): Figure out under what situations |modal_type| isn't
880 // none. The comment in desktop_native_widget_aura.cc suggests that this
881 // is rare.
882 NOTIMPLEMENTED();
886 void DesktopWindowTreeHostX11::FlashFrame(bool flash_frame) {
887 if (urgency_hint_set_ == flash_frame)
888 return;
890 gfx::XScopedPtr<XWMHints> hints(XGetWMHints(xdisplay_, xwindow_));
891 if (!hints) {
892 // The window hasn't had its hints set yet.
893 hints.reset(XAllocWMHints());
896 if (flash_frame)
897 hints->flags |= XUrgencyHint;
898 else
899 hints->flags &= ~XUrgencyHint;
901 XSetWMHints(xdisplay_, xwindow_, hints.get());
903 urgency_hint_set_ = flash_frame;
906 void DesktopWindowTreeHostX11::OnRootViewLayout() {
907 UpdateMinAndMaxSize();
910 void DesktopWindowTreeHostX11::OnNativeWidgetFocus() {
913 void DesktopWindowTreeHostX11::OnNativeWidgetBlur() {
916 bool DesktopWindowTreeHostX11::IsAnimatingClosed() const {
917 return false;
920 bool DesktopWindowTreeHostX11::IsTranslucentWindowOpacitySupported() const {
921 return false;
924 void DesktopWindowTreeHostX11::SizeConstraintsChanged() {
925 UpdateMinAndMaxSize();
928 ////////////////////////////////////////////////////////////////////////////////
929 // DesktopWindowTreeHostX11, aura::WindowTreeHost implementation:
931 gfx::Transform DesktopWindowTreeHostX11::GetRootTransform() const {
932 gfx::Display display = gfx::Screen::GetNativeScreen()->GetPrimaryDisplay();
933 if (window_mapped_) {
934 aura::Window* win = const_cast<aura::Window*>(window());
935 display = gfx::Screen::GetNativeScreen()->GetDisplayNearestWindow(win);
938 float scale = display.device_scale_factor();
939 gfx::Transform transform;
940 transform.Scale(scale, scale);
941 return transform;
944 ui::EventSource* DesktopWindowTreeHostX11::GetEventSource() {
945 return this;
948 gfx::AcceleratedWidget DesktopWindowTreeHostX11::GetAcceleratedWidget() {
949 return xwindow_;
952 void DesktopWindowTreeHostX11::ShowImpl() {
953 ShowWindowWithState(ui::SHOW_STATE_NORMAL);
954 native_widget_delegate_->OnNativeWidgetVisibilityChanged(true);
957 void DesktopWindowTreeHostX11::HideImpl() {
958 if (window_mapped_) {
959 XWithdrawWindow(xdisplay_, xwindow_, 0);
960 window_mapped_ = false;
962 native_widget_delegate_->OnNativeWidgetVisibilityChanged(false);
965 gfx::Rect DesktopWindowTreeHostX11::GetBounds() const {
966 return bounds_in_pixels_;
969 void DesktopWindowTreeHostX11::SetBounds(
970 const gfx::Rect& requested_bounds_in_pixel) {
971 gfx::Rect bounds_in_pixels(requested_bounds_in_pixel.origin(),
972 AdjustSize(requested_bounds_in_pixel.size()));
973 bool origin_changed = bounds_in_pixels_.origin() != bounds_in_pixels.origin();
974 bool size_changed = bounds_in_pixels_.size() != bounds_in_pixels.size();
975 XWindowChanges changes = {0};
976 unsigned value_mask = 0;
978 if (size_changed) {
979 if (bounds_in_pixels.width() < min_size_in_pixels_.width() ||
980 bounds_in_pixels.height() < min_size_in_pixels_.height() ||
981 (!max_size_in_pixels_.IsEmpty() &&
982 (bounds_in_pixels.width() > max_size_in_pixels_.width() ||
983 bounds_in_pixels.height() > max_size_in_pixels_.height()))) {
984 // Update the minimum and maximum sizes in case they have changed.
985 UpdateMinAndMaxSize();
987 gfx::Size size_in_pixels = bounds_in_pixels.size();
988 size_in_pixels.SetToMin(max_size_in_pixels_);
989 size_in_pixels.SetToMax(min_size_in_pixels_);
990 bounds_in_pixels.set_size(size_in_pixels);
993 changes.width = bounds_in_pixels.width();
994 changes.height = bounds_in_pixels.height();
995 value_mask |= CWHeight | CWWidth;
998 if (origin_changed) {
999 changes.x = bounds_in_pixels.x();
1000 changes.y = bounds_in_pixels.y();
1001 value_mask |= CWX | CWY;
1003 if (value_mask)
1004 XConfigureWindow(xdisplay_, xwindow_, value_mask, &changes);
1006 // Assume that the resize will go through as requested, which should be the
1007 // case if we're running without a window manager. If there's a window
1008 // manager, it can modify or ignore the request, but (per ICCCM) we'll get a
1009 // (possibly synthetic) ConfigureNotify about the actual size and correct
1010 // |bounds_in_pixels_| later.
1011 bounds_in_pixels_ = bounds_in_pixels;
1013 if (origin_changed)
1014 native_widget_delegate_->AsWidget()->OnNativeWidgetMove();
1015 if (size_changed) {
1016 OnHostResized(bounds_in_pixels.size());
1017 ResetWindowRegion();
1021 gfx::Point DesktopWindowTreeHostX11::GetLocationOnNativeScreen() const {
1022 return bounds_in_pixels_.origin();
1025 void DesktopWindowTreeHostX11::SetCapture() {
1026 if (HasCapture())
1027 return;
1029 // Grabbing the mouse is asynchronous. However, we synchronously start
1030 // forwarding all mouse events received by Chrome to the
1031 // aura::WindowEventDispatcher which has capture. This makes capture
1032 // synchronous for all intents and purposes if either:
1033 // - |g_current_capture|'s X window has capture.
1034 // OR
1035 // - The topmost window underneath the mouse is managed by Chrome.
1036 DesktopWindowTreeHostX11* old_capturer = g_current_capture;
1038 // Update |g_current_capture| prior to calling OnHostLostWindowCapture() to
1039 // avoid releasing pointer grab.
1040 g_current_capture = this;
1041 if (old_capturer)
1042 old_capturer->OnHostLostWindowCapture();
1044 GrabPointer(xwindow_, true, None);
1047 void DesktopWindowTreeHostX11::ReleaseCapture() {
1048 if (g_current_capture == this) {
1049 // Release mouse grab asynchronously. A window managed by Chrome is likely
1050 // the topmost window underneath the mouse so the capture release being
1051 // asynchronous is likely inconsequential.
1052 g_current_capture = NULL;
1053 UngrabPointer();
1055 OnHostLostWindowCapture();
1059 void DesktopWindowTreeHostX11::SetCursorNative(gfx::NativeCursor cursor) {
1060 XDefineCursor(xdisplay_, xwindow_, cursor.platform());
1063 void DesktopWindowTreeHostX11::MoveCursorToNative(const gfx::Point& location) {
1064 XWarpPointer(xdisplay_, None, x_root_window_, 0, 0, 0, 0,
1065 bounds_in_pixels_.x() + location.x(),
1066 bounds_in_pixels_.y() + location.y());
1069 void DesktopWindowTreeHostX11::OnCursorVisibilityChangedNative(bool show) {
1070 // TODO(erg): Conditional on us enabling touch on desktop linux builds, do
1071 // the same tap-to-click disabling here that chromeos does.
1074 ////////////////////////////////////////////////////////////////////////////////
1075 // DesktopWindowTreeHostX11, private:
1077 void DesktopWindowTreeHostX11::InitX11Window(
1078 const Widget::InitParams& params) {
1079 unsigned long attribute_mask = CWBackPixmap;
1080 XSetWindowAttributes swa;
1081 memset(&swa, 0, sizeof(swa));
1082 swa.background_pixmap = None;
1084 ::Atom window_type;
1085 switch (params.type) {
1086 case Widget::InitParams::TYPE_MENU:
1087 swa.override_redirect = True;
1088 window_type = atom_cache_.GetAtom("_NET_WM_WINDOW_TYPE_MENU");
1089 break;
1090 case Widget::InitParams::TYPE_TOOLTIP:
1091 swa.override_redirect = True;
1092 window_type = atom_cache_.GetAtom("_NET_WM_WINDOW_TYPE_TOOLTIP");
1093 break;
1094 case Widget::InitParams::TYPE_POPUP:
1095 swa.override_redirect = True;
1096 window_type = atom_cache_.GetAtom("_NET_WM_WINDOW_TYPE_NOTIFICATION");
1097 break;
1098 case Widget::InitParams::TYPE_DRAG:
1099 swa.override_redirect = True;
1100 window_type = atom_cache_.GetAtom("_NET_WM_WINDOW_TYPE_DND");
1101 break;
1102 default:
1103 window_type = atom_cache_.GetAtom("_NET_WM_WINDOW_TYPE_NORMAL");
1104 break;
1106 if (swa.override_redirect)
1107 attribute_mask |= CWOverrideRedirect;
1109 // Detect whether we're running inside a compositing manager. If so, try to
1110 // use the ARGB visual. Otherwise, just use our parent's visual.
1111 Visual* visual = CopyFromParent;
1112 int depth = CopyFromParent;
1113 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
1114 switches::kEnableTransparentVisuals) &&
1115 XGetSelectionOwner(xdisplay_, atom_cache_.GetAtom("_NET_WM_CM_S0")) !=
1116 None) {
1117 Visual* rgba_visual = GetARGBVisual();
1118 if (rgba_visual) {
1119 visual = rgba_visual;
1120 depth = 32;
1122 attribute_mask |= CWColormap;
1123 swa.colormap = XCreateColormap(xdisplay_, x_root_window_, visual,
1124 AllocNone);
1126 // x.org will BadMatch if we don't set a border when the depth isn't the
1127 // same as the parent depth.
1128 attribute_mask |= CWBorderPixel;
1129 swa.border_pixel = 0;
1131 use_argb_visual_ = true;
1135 bounds_in_pixels_ = ToPixelRect(params.bounds);
1136 bounds_in_pixels_.set_size(AdjustSize(bounds_in_pixels_.size()));
1137 xwindow_ = XCreateWindow(xdisplay_, x_root_window_, bounds_in_pixels_.x(),
1138 bounds_in_pixels_.y(), bounds_in_pixels_.width(),
1139 bounds_in_pixels_.height(),
1140 0, // border width
1141 depth, InputOutput, visual, attribute_mask, &swa);
1142 if (ui::PlatformEventSource::GetInstance())
1143 ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
1144 open_windows().push_back(xwindow_);
1146 // TODO(erg): Maybe need to set a ViewProp here like in RWHL::RWHL().
1148 long event_mask = ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
1149 KeyPressMask | KeyReleaseMask |
1150 EnterWindowMask | LeaveWindowMask |
1151 ExposureMask | VisibilityChangeMask |
1152 StructureNotifyMask | PropertyChangeMask |
1153 PointerMotionMask;
1154 XSelectInput(xdisplay_, xwindow_, event_mask);
1155 XFlush(xdisplay_);
1157 if (ui::IsXInput2Available())
1158 ui::TouchFactory::GetInstance()->SetupXI2ForXWindow(xwindow_);
1160 // TODO(erg): We currently only request window deletion events. We also
1161 // should listen for activation events and anything else that GTK+ listens
1162 // for, and do something useful.
1163 ::Atom protocols[2];
1164 protocols[0] = atom_cache_.GetAtom("WM_DELETE_WINDOW");
1165 protocols[1] = atom_cache_.GetAtom("_NET_WM_PING");
1166 XSetWMProtocols(xdisplay_, xwindow_, protocols, 2);
1168 // We need a WM_CLIENT_MACHINE and WM_LOCALE_NAME value so we integrate with
1169 // the desktop environment.
1170 XSetWMProperties(xdisplay_, xwindow_, NULL, NULL, NULL, 0, NULL, NULL, NULL);
1172 // Likewise, the X server needs to know this window's pid so it knows which
1173 // program to kill if the window hangs.
1174 // XChangeProperty() expects "pid" to be long.
1175 static_assert(sizeof(long) >= sizeof(pid_t),
1176 "pid_t should not be larger than long");
1177 long pid = getpid();
1178 XChangeProperty(xdisplay_,
1179 xwindow_,
1180 atom_cache_.GetAtom("_NET_WM_PID"),
1181 XA_CARDINAL,
1183 PropModeReplace,
1184 reinterpret_cast<unsigned char*>(&pid), 1);
1186 XChangeProperty(xdisplay_,
1187 xwindow_,
1188 atom_cache_.GetAtom("_NET_WM_WINDOW_TYPE"),
1189 XA_ATOM,
1191 PropModeReplace,
1192 reinterpret_cast<unsigned char*>(&window_type), 1);
1194 // List of window state properties (_NET_WM_STATE) to set, if any.
1195 std::vector< ::Atom> state_atom_list;
1197 // Remove popup windows from taskbar unless overridden.
1198 if ((params.type == Widget::InitParams::TYPE_POPUP ||
1199 params.type == Widget::InitParams::TYPE_BUBBLE) &&
1200 !params.force_show_in_taskbar) {
1201 state_atom_list.push_back(
1202 atom_cache_.GetAtom("_NET_WM_STATE_SKIP_TASKBAR"));
1205 // If the window should stay on top of other windows, add the
1206 // _NET_WM_STATE_ABOVE property.
1207 is_always_on_top_ = params.keep_on_top;
1208 if (is_always_on_top_)
1209 state_atom_list.push_back(atom_cache_.GetAtom("_NET_WM_STATE_ABOVE"));
1211 if (params.visible_on_all_workspaces) {
1212 state_atom_list.push_back(atom_cache_.GetAtom("_NET_WM_STATE_STICKY"));
1213 ui::SetIntProperty(xwindow_, "_NET_WM_DESKTOP", "CARDINAL", kAllDesktops);
1216 // Setting _NET_WM_STATE by sending a message to the root_window (with
1217 // SetWMSpecState) has no effect here since the window has not yet been
1218 // mapped. So we manually change the state.
1219 if (!state_atom_list.empty()) {
1220 ui::SetAtomArrayProperty(xwindow_,
1221 "_NET_WM_STATE",
1222 "ATOM",
1223 state_atom_list);
1226 if (!params.wm_class_name.empty() || !params.wm_class_class.empty()) {
1227 ui::SetWindowClassHint(
1228 xdisplay_, xwindow_, params.wm_class_name, params.wm_class_class);
1231 const char* wm_role_name = NULL;
1232 // If the widget isn't overriding the role, provide a default value for popup
1233 // and bubble types.
1234 if (!params.wm_role_name.empty()) {
1235 wm_role_name = params.wm_role_name.c_str();
1236 } else {
1237 switch (params.type) {
1238 case Widget::InitParams::TYPE_POPUP:
1239 wm_role_name = kX11WindowRolePopup;
1240 break;
1241 case Widget::InitParams::TYPE_BUBBLE:
1242 wm_role_name = kX11WindowRoleBubble;
1243 break;
1244 default:
1245 break;
1248 if (wm_role_name)
1249 ui::SetWindowRole(xdisplay_, xwindow_, std::string(wm_role_name));
1251 if (params.remove_standard_frame) {
1252 // Setting _GTK_HIDE_TITLEBAR_WHEN_MAXIMIZED tells gnome-shell to not force
1253 // fullscreen on the window when it matches the desktop size.
1254 ui::SetHideTitlebarWhenMaximizedProperty(xwindow_,
1255 ui::HIDE_TITLEBAR_WHEN_MAXIMIZED);
1258 // If we have a parent, record the parent/child relationship. We use this
1259 // data during destruction to make sure that when we try to close a parent
1260 // window, we also destroy all child windows.
1261 if (params.parent && params.parent->GetHost()) {
1262 XID parent_xid =
1263 params.parent->GetHost()->GetAcceleratedWidget();
1264 window_parent_ = GetHostForXID(parent_xid);
1265 DCHECK(window_parent_);
1266 window_parent_->window_children_.insert(this);
1269 // If we have a delegate which is providing a default window icon, use that
1270 // icon.
1271 gfx::ImageSkia* window_icon =
1272 ViewsDelegate::GetInstance()
1273 ? ViewsDelegate::GetInstance()->GetDefaultWindowIcon()
1274 : NULL;
1275 if (window_icon) {
1276 SetWindowIcons(gfx::ImageSkia(), *window_icon);
1278 CreateCompositor(GetAcceleratedWidget());
1281 gfx::Size DesktopWindowTreeHostX11::AdjustSize(
1282 const gfx::Size& requested_size_in_pixels) {
1283 std::vector<gfx::Display> displays =
1284 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE)->GetAllDisplays();
1285 // Compare against all monitor sizes. The window manager can move the window
1286 // to whichever monitor it wants.
1287 for (size_t i = 0; i < displays.size(); ++i) {
1288 if (requested_size_in_pixels == displays[i].GetSizeInPixel()) {
1289 return gfx::Size(requested_size_in_pixels.width() - 1,
1290 requested_size_in_pixels.height() - 1);
1294 // Do not request a 0x0 window size. It causes an XError.
1295 gfx::Size size_in_pixels = requested_size_in_pixels;
1296 size_in_pixels.SetToMax(gfx::Size(1, 1));
1297 return size_in_pixels;
1300 void DesktopWindowTreeHostX11::OnWMStateUpdated() {
1301 std::vector< ::Atom> atom_list;
1302 // Ignore the return value of ui::GetAtomArrayProperty(). Fluxbox removes the
1303 // _NET_WM_STATE property when no _NET_WM_STATE atoms are set.
1304 ui::GetAtomArrayProperty(xwindow_, "_NET_WM_STATE", &atom_list);
1306 bool was_minimized = IsMinimized();
1308 window_properties_.clear();
1309 std::copy(atom_list.begin(), atom_list.end(),
1310 inserter(window_properties_, window_properties_.begin()));
1312 // Propagate the window minimization information to the content window, so
1313 // the render side can update its visibility properly. OnWMStateUpdated() is
1314 // called by PropertyNofify event from DispatchEvent() when the browser is
1315 // minimized or shown from minimized state. On Windows, this is realized by
1316 // calling OnHostResized() with an empty size. In particular,
1317 // HWNDMessageHandler::GetClientAreaBounds() returns an empty size when the
1318 // window is minimized. On Linux, returning empty size in GetBounds() or
1319 // SetBounds() does not work.
1320 // We also propagate the minimization to the compositor, to makes sure that we
1321 // don't draw any 'blank' frames that could be noticed in applications such as
1322 // window manager previews, which show content even when a window is
1323 // minimized.
1324 bool is_minimized = IsMinimized();
1325 if (is_minimized != was_minimized) {
1326 if (is_minimized) {
1327 compositor()->SetVisible(false);
1328 content_window_->Hide();
1329 } else {
1330 content_window_->Show();
1331 compositor()->SetVisible(true);
1335 if (restored_bounds_in_pixels_.IsEmpty()) {
1336 DCHECK(!IsFullscreen());
1337 if (IsMaximized()) {
1338 // The request that we become maximized originated from a different
1339 // process. |bounds_in_pixels_| already contains our maximized bounds. Do
1340 // a best effort attempt to get restored bounds by setting it to our
1341 // previously set bounds (and if we get this wrong, we aren't any worse
1342 // off since we'd otherwise be returning our maximized bounds).
1343 restored_bounds_in_pixels_ = previous_bounds_in_pixels_;
1345 } else if (!IsMaximized() && !IsFullscreen()) {
1346 // If we have restored bounds, but WM_STATE no longer claims to be
1347 // maximized or fullscreen, we should clear our restored bounds.
1348 restored_bounds_in_pixels_ = gfx::Rect();
1351 // Ignore requests by the window manager to enter or exit fullscreen (e.g. as
1352 // a result of pressing a window manager accelerator key). Chrome does not
1353 // handle window manager initiated fullscreen. In particular, Chrome needs to
1354 // do preprocessing before the x window's fullscreen state is toggled.
1356 is_always_on_top_ = HasWMSpecProperty("_NET_WM_STATE_ABOVE");
1358 // Now that we have different window properties, we may need to relayout the
1359 // window. (The windows code doesn't need this because their window change is
1360 // synchronous.)
1361 Relayout();
1362 ResetWindowRegion();
1365 void DesktopWindowTreeHostX11::OnFrameExtentsUpdated() {
1366 std::vector<int> insets;
1367 if (ui::GetIntArrayProperty(xwindow_, "_NET_FRAME_EXTENTS", &insets) &&
1368 insets.size() == 4) {
1369 // |insets| are returned in the order: [left, right, top, bottom].
1370 native_window_frame_borders_in_pixels_ =
1371 gfx::Insets(insets[2], insets[0], insets[3], insets[1]);
1372 } else {
1373 native_window_frame_borders_in_pixels_ = gfx::Insets();
1377 void DesktopWindowTreeHostX11::UpdateMinAndMaxSize() {
1378 if (!window_mapped_)
1379 return;
1381 gfx::Size minimum_in_pixels =
1382 ToPixelRect(gfx::Rect(native_widget_delegate_->GetMinimumSize())).size();
1383 gfx::Size maximum_in_pixels =
1384 ToPixelRect(gfx::Rect(native_widget_delegate_->GetMaximumSize())).size();
1385 if (min_size_in_pixels_ == minimum_in_pixels &&
1386 max_size_in_pixels_ == maximum_in_pixels)
1387 return;
1389 min_size_in_pixels_ = minimum_in_pixels;
1390 max_size_in_pixels_ = maximum_in_pixels;
1392 XSizeHints hints;
1393 long supplied_return;
1394 XGetWMNormalHints(xdisplay_, xwindow_, &hints, &supplied_return);
1396 if (minimum_in_pixels.IsEmpty()) {
1397 hints.flags &= ~PMinSize;
1398 } else {
1399 hints.flags |= PMinSize;
1400 hints.min_width = min_size_in_pixels_.width();
1401 hints.min_height = min_size_in_pixels_.height();
1404 if (maximum_in_pixels.IsEmpty()) {
1405 hints.flags &= ~PMaxSize;
1406 } else {
1407 hints.flags |= PMaxSize;
1408 hints.max_width = max_size_in_pixels_.width();
1409 hints.max_height = max_size_in_pixels_.height();
1412 XSetWMNormalHints(xdisplay_, xwindow_, &hints);
1415 void DesktopWindowTreeHostX11::UpdateWMUserTime(
1416 const ui::PlatformEvent& event) {
1417 if (!IsActive())
1418 return;
1420 ui::EventType type = ui::EventTypeFromNative(event);
1421 if (type == ui::ET_MOUSE_PRESSED ||
1422 type == ui::ET_KEY_PRESSED ||
1423 type == ui::ET_TOUCH_PRESSED) {
1424 unsigned long wm_user_time_ms = static_cast<unsigned long>(
1425 ui::EventTimeFromNative(event).InMilliseconds());
1426 XChangeProperty(xdisplay_,
1427 xwindow_,
1428 atom_cache_.GetAtom("_NET_WM_USER_TIME"),
1429 XA_CARDINAL,
1431 PropModeReplace,
1432 reinterpret_cast<const unsigned char *>(&wm_user_time_ms),
1434 X11DesktopHandler::get()->set_wm_user_time_ms(wm_user_time_ms);
1438 void DesktopWindowTreeHostX11::SetWMSpecState(bool enabled,
1439 ::Atom state1,
1440 ::Atom state2) {
1441 XEvent xclient;
1442 memset(&xclient, 0, sizeof(xclient));
1443 xclient.type = ClientMessage;
1444 xclient.xclient.window = xwindow_;
1445 xclient.xclient.message_type = atom_cache_.GetAtom("_NET_WM_STATE");
1446 xclient.xclient.format = 32;
1447 xclient.xclient.data.l[0] =
1448 enabled ? k_NET_WM_STATE_ADD : k_NET_WM_STATE_REMOVE;
1449 xclient.xclient.data.l[1] = state1;
1450 xclient.xclient.data.l[2] = state2;
1451 xclient.xclient.data.l[3] = 1;
1452 xclient.xclient.data.l[4] = 0;
1454 XSendEvent(xdisplay_, x_root_window_, False,
1455 SubstructureRedirectMask | SubstructureNotifyMask,
1456 &xclient);
1459 bool DesktopWindowTreeHostX11::HasWMSpecProperty(const char* property) const {
1460 return window_properties_.find(atom_cache_.GetAtom(property)) !=
1461 window_properties_.end();
1464 void DesktopWindowTreeHostX11::SetUseNativeFrame(bool use_native_frame) {
1465 use_native_frame_ = use_native_frame;
1466 ui::SetUseOSWindowFrame(xwindow_, use_native_frame);
1467 ResetWindowRegion();
1470 void DesktopWindowTreeHostX11::DispatchMouseEvent(ui::MouseEvent* event) {
1471 // In Windows, the native events sent to chrome are separated into client
1472 // and non-client versions of events, which we record on our LocatedEvent
1473 // structures. On X11, we emulate the concept of non-client. Before we pass
1474 // this event to the cross platform event handling framework, we need to
1475 // make sure it is appropriately marked as non-client if it's in the non
1476 // client area, or otherwise, we can get into a state where the a window is
1477 // set as the |mouse_pressed_handler_| in window_event_dispatcher.cc
1478 // despite the mouse button being released.
1480 // We can't do this later in the dispatch process because we share that
1481 // with ash, and ash gets confused about event IS_NON_CLIENT-ness on
1482 // events, since ash doesn't expect this bit to be set, because it's never
1483 // been set before. (This works on ash on Windows because none of the mouse
1484 // events on the ash desktop are clicking in what Windows considers to be a
1485 // non client area.) Likewise, we won't want to do the following in any
1486 // WindowTreeHost that hosts ash.
1487 if (content_window_ && content_window_->delegate()) {
1488 int flags = event->flags();
1489 int hit_test_code =
1490 content_window_->delegate()->GetNonClientComponent(event->location());
1491 if (hit_test_code != HTCLIENT && hit_test_code != HTNOWHERE)
1492 flags |= ui::EF_IS_NON_CLIENT;
1493 event->set_flags(flags);
1496 // While we unset the urgency hint when we gain focus, we also must remove it
1497 // on mouse clicks because we can call FlashFrame() on an active window.
1498 if (event->IsAnyButton() || event->IsMouseWheelEvent())
1499 FlashFrame(false);
1501 if (!g_current_capture || g_current_capture == this) {
1502 SendEventToProcessor(event);
1503 } else {
1504 // Another DesktopWindowTreeHostX11 has installed itself as
1505 // capture. Translate the event's location and dispatch to the other.
1506 ConvertEventToDifferentHost(event, g_current_capture);
1507 g_current_capture->SendEventToProcessor(event);
1511 void DesktopWindowTreeHostX11::DispatchTouchEvent(ui::TouchEvent* event) {
1512 if (g_current_capture && g_current_capture != this &&
1513 event->type() == ui::ET_TOUCH_PRESSED) {
1514 ConvertEventToDifferentHost(event, g_current_capture);
1515 g_current_capture->SendEventToProcessor(event);
1516 } else {
1517 SendEventToProcessor(event);
1521 void DesktopWindowTreeHostX11::DispatchKeyEvent(ui::KeyEvent* event) {
1522 GetInputMethod()->DispatchKeyEvent(event);
1525 void DesktopWindowTreeHostX11::ConvertEventToDifferentHost(
1526 ui::LocatedEvent* located_event,
1527 DesktopWindowTreeHostX11* host) {
1528 DCHECK_NE(this, host);
1529 const gfx::Display display_src =
1530 gfx::Screen::GetNativeScreen()->GetDisplayNearestWindow(window());
1531 const gfx::Display display_dest =
1532 gfx::Screen::GetNativeScreen()->GetDisplayNearestWindow(host->window());
1533 DCHECK_EQ(display_src.device_scale_factor(),
1534 display_dest.device_scale_factor());
1535 gfx::Vector2d offset = GetLocationOnNativeScreen() -
1536 host->GetLocationOnNativeScreen();
1537 gfx::Point location_in_pixel_in_host = located_event->location() + offset;
1538 located_event->set_location(location_in_pixel_in_host);
1541 void DesktopWindowTreeHostX11::ResetWindowRegion() {
1542 // If a custom window shape was supplied then apply it.
1543 if (custom_window_shape_) {
1544 XShapeCombineRegion(xdisplay_, xwindow_, ShapeBounding, 0, 0,
1545 window_shape_.get(), false);
1546 return;
1549 window_shape_.reset();
1551 if (!IsMaximized() && !IsFullscreen()) {
1552 gfx::Path window_mask;
1553 views::Widget* widget = native_widget_delegate_->AsWidget();
1554 if (widget->non_client_view()) {
1555 // Some frame views define a custom (non-rectangular) window mask. If
1556 // so, use it to define the window shape. If not, fall through.
1557 widget->non_client_view()->GetWindowMask(bounds_in_pixels_.size(),
1558 &window_mask);
1559 if (window_mask.countPoints() > 0) {
1560 window_shape_.reset(gfx::CreateRegionFromSkPath(window_mask));
1561 XShapeCombineRegion(xdisplay_, xwindow_, ShapeBounding, 0, 0,
1562 window_shape_.get(), false);
1563 return;
1568 // If we didn't set the shape for any reason, reset the shaping information.
1569 // How this is done depends on the border style, due to quirks and bugs in
1570 // various window managers.
1571 if (ShouldUseNativeFrame()) {
1572 // If the window has system borders, the mask must be set to null (not a
1573 // rectangle), because several window managers (eg, KDE, XFCE, XMonad) will
1574 // not put borders on a window with a custom shape.
1575 XShapeCombineMask(xdisplay_, xwindow_, ShapeBounding, 0, 0, None, ShapeSet);
1576 } else {
1577 // Conversely, if the window does not have system borders, the mask must be
1578 // manually set to a rectangle that covers the whole window (not null). This
1579 // is due to a bug in KWin <= 4.11.5 (KDE bug #330573) where setting a null
1580 // shape causes the hint to disable system borders to be ignored (resulting
1581 // in a double border).
1582 XRectangle r = {0,
1584 static_cast<unsigned short>(bounds_in_pixels_.width()),
1585 static_cast<unsigned short>(bounds_in_pixels_.height())};
1586 XShapeCombineRectangles(
1587 xdisplay_, xwindow_, ShapeBounding, 0, 0, &r, 1, ShapeSet, YXBanded);
1591 void DesktopWindowTreeHostX11::SerializeImageRepresentation(
1592 const gfx::ImageSkiaRep& rep,
1593 std::vector<unsigned long>* data) {
1594 int width = rep.GetWidth();
1595 data->push_back(width);
1597 int height = rep.GetHeight();
1598 data->push_back(height);
1600 const SkBitmap& bitmap = rep.sk_bitmap();
1601 SkAutoLockPixels locker(bitmap);
1603 for (int y = 0; y < height; ++y)
1604 for (int x = 0; x < width; ++x)
1605 data->push_back(bitmap.getColor(x, y));
1608 Visual* DesktopWindowTreeHostX11::GetARGBVisual() {
1609 XVisualInfo visual_template;
1610 visual_template.screen = 0;
1612 int visuals_len;
1613 gfx::XScopedPtr<XVisualInfo[]> visual_list(XGetVisualInfo(
1614 xdisplay_, VisualScreenMask, &visual_template, &visuals_len));
1615 for (int i = 0; i < visuals_len; ++i) {
1616 // Why support only 8888 ARGB? Because it's all that GTK+ supports. In
1617 // gdkvisual-x11.cc, they look for this specific visual and use it for all
1618 // their alpha channel using needs.
1620 // TODO(erg): While the following does find a valid visual, some GL drivers
1621 // don't believe that this has an alpha channel. According to marcheu@,
1622 // this should work on open source driver though. (It doesn't work with
1623 // NVidia's binaries currently.) http://crbug.com/369209
1624 const XVisualInfo& info = visual_list[i];
1625 if (info.depth == 32 && info.visual->red_mask == 0xff0000 &&
1626 info.visual->green_mask == 0x00ff00 &&
1627 info.visual->blue_mask == 0x0000ff) {
1628 return info.visual;
1632 return nullptr;
1635 std::list<XID>& DesktopWindowTreeHostX11::open_windows() {
1636 if (!open_windows_)
1637 open_windows_ = new std::list<XID>();
1638 return *open_windows_;
1641 void DesktopWindowTreeHostX11::MapWindow(ui::WindowShowState show_state) {
1642 if (show_state != ui::SHOW_STATE_DEFAULT &&
1643 show_state != ui::SHOW_STATE_NORMAL &&
1644 show_state != ui::SHOW_STATE_INACTIVE &&
1645 show_state != ui::SHOW_STATE_MAXIMIZED) {
1646 // It will behave like SHOW_STATE_NORMAL.
1647 NOTIMPLEMENTED();
1650 // Before we map the window, set size hints. Otherwise, some window managers
1651 // will ignore toplevel XMoveWindow commands.
1652 XSizeHints size_hints;
1653 size_hints.flags = PPosition;
1654 size_hints.x = bounds_in_pixels_.x();
1655 size_hints.y = bounds_in_pixels_.y();
1656 XSetWMNormalHints(xdisplay_, xwindow_, &size_hints);
1658 // If SHOW_STATE_INACTIVE, tell the window manager not to focus the window
1659 // when mapping. This is done by setting the _NET_WM_USER_TIME to 0. See e.g.
1660 // http://standards.freedesktop.org/wm-spec/latest/ar01s05.html
1661 unsigned long wm_user_time_ms = (show_state == ui::SHOW_STATE_INACTIVE) ?
1662 0 : X11DesktopHandler::get()->wm_user_time_ms();
1663 if (show_state == ui::SHOW_STATE_INACTIVE || wm_user_time_ms != 0) {
1664 XChangeProperty(xdisplay_,
1665 xwindow_,
1666 atom_cache_.GetAtom("_NET_WM_USER_TIME"),
1667 XA_CARDINAL,
1669 PropModeReplace,
1670 reinterpret_cast<const unsigned char *>(&wm_user_time_ms),
1674 XMapWindow(xdisplay_, xwindow_);
1676 // We now block until our window is mapped. Some X11 APIs will crash and
1677 // burn if passed |xwindow_| before the window is mapped, and XMapWindow is
1678 // asynchronous.
1679 if (ui::X11EventSource::GetInstance())
1680 ui::X11EventSource::GetInstance()->BlockUntilWindowMapped(xwindow_);
1681 window_mapped_ = true;
1683 UpdateMinAndMaxSize();
1685 // Some WMs only respect maximize hints after the window has been mapped.
1686 // Check whether we need to re-do a maximization.
1687 if (should_maximize_after_map_) {
1688 Maximize();
1689 should_maximize_after_map_ = false;
1693 void DesktopWindowTreeHostX11::SetWindowTransparency() {
1694 compositor()->SetHostHasTransparentBackground(use_argb_visual_);
1695 window()->SetTransparent(use_argb_visual_);
1696 content_window_->SetTransparent(use_argb_visual_);
1699 void DesktopWindowTreeHostX11::Relayout() {
1700 Widget* widget = native_widget_delegate_->AsWidget();
1701 NonClientView* non_client_view = widget->non_client_view();
1702 // non_client_view may be NULL, especially during creation.
1703 if (non_client_view) {
1704 non_client_view->client_view()->InvalidateLayout();
1705 non_client_view->InvalidateLayout();
1707 widget->GetRootView()->Layout();
1710 ////////////////////////////////////////////////////////////////////////////////
1711 // DesktopWindowTreeHostX11, ui::PlatformEventDispatcher implementation:
1713 bool DesktopWindowTreeHostX11::CanDispatchEvent(
1714 const ui::PlatformEvent& event) {
1715 return event->xany.window == xwindow_ ||
1716 (event->type == GenericEvent &&
1717 static_cast<XIDeviceEvent*>(event->xcookie.data)->event == xwindow_);
1720 uint32_t DesktopWindowTreeHostX11::DispatchEvent(
1721 const ui::PlatformEvent& event) {
1722 XEvent* xev = event;
1724 TRACE_EVENT1("views", "DesktopWindowTreeHostX11::Dispatch",
1725 "event->type", event->type);
1727 UpdateWMUserTime(event);
1729 // May want to factor CheckXEventForConsistency(xev); into a common location
1730 // since it is called here.
1731 switch (xev->type) {
1732 case EnterNotify:
1733 case LeaveNotify: {
1734 // Ignore EventNotify and LeaveNotify events from children of |xwindow_|.
1735 // NativeViewGLSurfaceGLX adds a child to |xwindow_|.
1736 // TODO(pkotwicz|tdanderson): Figure out whether the suppression is
1737 // necessary. crbug.com/385716
1738 if (xev->xcrossing.detail == NotifyInferior)
1739 break;
1741 ui::MouseEvent mouse_event(xev);
1742 DispatchMouseEvent(&mouse_event);
1743 break;
1745 case Expose: {
1746 gfx::Rect damage_rect_in_pixels(xev->xexpose.x, xev->xexpose.y,
1747 xev->xexpose.width, xev->xexpose.height);
1748 compositor()->ScheduleRedrawRect(damage_rect_in_pixels);
1749 break;
1751 case KeyPress: {
1752 ui::KeyEvent keydown_event(xev);
1753 DispatchKeyEvent(&keydown_event);
1754 break;
1756 case KeyRelease: {
1757 // There is no way to deactivate a window in X11 so ignore input if
1758 // window is supposed to be 'inactive'. See comments in
1759 // X11DesktopHandler::DeactivateWindow() for more details.
1760 if (!IsActive() && !HasCapture())
1761 break;
1763 ui::KeyEvent key_event(xev);
1764 DispatchKeyEvent(&key_event);
1765 break;
1767 case ButtonPress:
1768 case ButtonRelease: {
1769 ui::EventType event_type = ui::EventTypeFromNative(xev);
1770 switch (event_type) {
1771 case ui::ET_MOUSEWHEEL: {
1772 ui::MouseWheelEvent mouseev(xev);
1773 DispatchMouseEvent(&mouseev);
1774 break;
1776 case ui::ET_MOUSE_PRESSED:
1777 case ui::ET_MOUSE_RELEASED: {
1778 ui::MouseEvent mouseev(xev);
1779 DispatchMouseEvent(&mouseev);
1780 break;
1782 case ui::ET_UNKNOWN:
1783 // No event is created for X11-release events for mouse-wheel buttons.
1784 break;
1785 default:
1786 NOTREACHED() << event_type;
1788 break;
1790 case FocusOut:
1791 if (xev->xfocus.mode != NotifyGrab) {
1792 ReleaseCapture();
1793 OnHostLostWindowCapture();
1794 X11DesktopHandler::get()->ProcessXEvent(xev);
1795 } else {
1796 dispatcher()->OnHostLostMouseGrab();
1798 break;
1799 case FocusIn:
1800 X11DesktopHandler::get()->ProcessXEvent(xev);
1801 break;
1802 case ConfigureNotify: {
1803 DCHECK_EQ(xwindow_, xev->xconfigure.window);
1804 DCHECK_EQ(xwindow_, xev->xconfigure.event);
1805 // It's possible that the X window may be resized by some other means than
1806 // from within aura (e.g. the X window manager can change the size). Make
1807 // sure the root window size is maintained properly.
1808 int translated_x_in_pixels = xev->xconfigure.x;
1809 int translated_y_in_pixels = xev->xconfigure.y;
1810 if (!xev->xconfigure.send_event && !xev->xconfigure.override_redirect) {
1811 Window unused;
1812 XTranslateCoordinates(xdisplay_, xwindow_, x_root_window_, 0, 0,
1813 &translated_x_in_pixels, &translated_y_in_pixels,
1814 &unused);
1816 gfx::Rect bounds_in_pixels(translated_x_in_pixels, translated_y_in_pixels,
1817 xev->xconfigure.width, xev->xconfigure.height);
1818 bool size_changed = bounds_in_pixels_.size() != bounds_in_pixels.size();
1819 bool origin_changed =
1820 bounds_in_pixels_.origin() != bounds_in_pixels.origin();
1821 previous_bounds_in_pixels_ = bounds_in_pixels_;
1822 bounds_in_pixels_ = bounds_in_pixels;
1824 if (origin_changed)
1825 OnHostMoved(bounds_in_pixels_.origin());
1827 if (size_changed) {
1828 delayed_resize_task_.Reset(base::Bind(
1829 &DesktopWindowTreeHostX11::DelayedResize,
1830 close_widget_factory_.GetWeakPtr(), bounds_in_pixels.size()));
1831 base::MessageLoop::current()->PostTask(
1832 FROM_HERE, delayed_resize_task_.callback());
1834 break;
1836 case GenericEvent: {
1837 ui::TouchFactory* factory = ui::TouchFactory::GetInstance();
1838 if (!factory->ShouldProcessXI2Event(xev))
1839 break;
1841 ui::EventType type = ui::EventTypeFromNative(xev);
1842 XEvent last_event;
1843 int num_coalesced = 0;
1845 switch (type) {
1846 case ui::ET_TOUCH_MOVED:
1847 num_coalesced = ui::CoalescePendingMotionEvents(xev, &last_event);
1848 if (num_coalesced > 0)
1849 xev = &last_event;
1850 // fallthrough
1851 case ui::ET_TOUCH_PRESSED:
1852 case ui::ET_TOUCH_RELEASED: {
1853 ui::TouchEvent touchev(xev);
1854 DispatchTouchEvent(&touchev);
1855 break;
1857 case ui::ET_MOUSE_MOVED:
1858 case ui::ET_MOUSE_DRAGGED:
1859 case ui::ET_MOUSE_PRESSED:
1860 case ui::ET_MOUSE_RELEASED:
1861 case ui::ET_MOUSE_ENTERED:
1862 case ui::ET_MOUSE_EXITED: {
1863 if (type == ui::ET_MOUSE_MOVED || type == ui::ET_MOUSE_DRAGGED) {
1864 // If this is a motion event, we want to coalesce all pending motion
1865 // events that are at the top of the queue.
1866 num_coalesced = ui::CoalescePendingMotionEvents(xev, &last_event);
1867 if (num_coalesced > 0)
1868 xev = &last_event;
1870 ui::MouseEvent mouseev(xev);
1871 DispatchMouseEvent(&mouseev);
1872 break;
1874 case ui::ET_MOUSEWHEEL: {
1875 ui::MouseWheelEvent mouseev(xev);
1876 DispatchMouseEvent(&mouseev);
1877 break;
1879 case ui::ET_SCROLL_FLING_START:
1880 case ui::ET_SCROLL_FLING_CANCEL:
1881 case ui::ET_SCROLL: {
1882 ui::ScrollEvent scrollev(xev);
1883 SendEventToProcessor(&scrollev);
1884 break;
1886 case ui::ET_KEY_PRESSED:
1887 case ui::ET_KEY_RELEASED: {
1888 ui::KeyEvent key_event(xev);
1889 DispatchKeyEvent(&key_event);
1890 break;
1892 case ui::ET_UNKNOWN:
1893 break;
1894 default:
1895 NOTREACHED();
1898 // If we coalesced an event we need to free its cookie.
1899 if (num_coalesced > 0)
1900 XFreeEventData(xev->xgeneric.display, &last_event.xcookie);
1901 break;
1903 case MapNotify: {
1904 FOR_EACH_OBSERVER(DesktopWindowTreeHostObserverX11,
1905 observer_list_,
1906 OnWindowMapped(xwindow_));
1907 break;
1909 case UnmapNotify: {
1910 FOR_EACH_OBSERVER(DesktopWindowTreeHostObserverX11,
1911 observer_list_,
1912 OnWindowUnmapped(xwindow_));
1913 break;
1915 case ClientMessage: {
1916 Atom message_type = xev->xclient.message_type;
1917 if (message_type == atom_cache_.GetAtom("WM_PROTOCOLS")) {
1918 Atom protocol = static_cast<Atom>(xev->xclient.data.l[0]);
1919 if (protocol == atom_cache_.GetAtom("WM_DELETE_WINDOW")) {
1920 // We have received a close message from the window manager.
1921 OnHostCloseRequested();
1922 } else if (protocol == atom_cache_.GetAtom("_NET_WM_PING")) {
1923 XEvent reply_event = *xev;
1924 reply_event.xclient.window = x_root_window_;
1926 XSendEvent(xdisplay_,
1927 reply_event.xclient.window,
1928 False,
1929 SubstructureRedirectMask | SubstructureNotifyMask,
1930 &reply_event);
1932 } else if (message_type == atom_cache_.GetAtom("XdndEnter")) {
1933 drag_drop_client_->OnXdndEnter(xev->xclient);
1934 } else if (message_type == atom_cache_.GetAtom("XdndLeave")) {
1935 drag_drop_client_->OnXdndLeave(xev->xclient);
1936 } else if (message_type == atom_cache_.GetAtom("XdndPosition")) {
1937 drag_drop_client_->OnXdndPosition(xev->xclient);
1938 } else if (message_type == atom_cache_.GetAtom("XdndStatus")) {
1939 drag_drop_client_->OnXdndStatus(xev->xclient);
1940 } else if (message_type == atom_cache_.GetAtom("XdndFinished")) {
1941 drag_drop_client_->OnXdndFinished(xev->xclient);
1942 } else if (message_type == atom_cache_.GetAtom("XdndDrop")) {
1943 drag_drop_client_->OnXdndDrop(xev->xclient);
1945 break;
1947 case MappingNotify: {
1948 switch (xev->xmapping.request) {
1949 case MappingModifier:
1950 case MappingKeyboard:
1951 XRefreshKeyboardMapping(&xev->xmapping);
1952 break;
1953 case MappingPointer:
1954 ui::DeviceDataManagerX11::GetInstance()->UpdateButtonMap();
1955 break;
1956 default:
1957 NOTIMPLEMENTED() << " Unknown request: " << xev->xmapping.request;
1958 break;
1960 break;
1962 case MotionNotify: {
1963 // Discard all but the most recent motion event that targets the same
1964 // window with unchanged state.
1965 XEvent last_event;
1966 while (XPending(xev->xany.display)) {
1967 XEvent next_event;
1968 XPeekEvent(xev->xany.display, &next_event);
1969 if (next_event.type == MotionNotify &&
1970 next_event.xmotion.window == xev->xmotion.window &&
1971 next_event.xmotion.subwindow == xev->xmotion.subwindow &&
1972 next_event.xmotion.state == xev->xmotion.state) {
1973 XNextEvent(xev->xany.display, &last_event);
1974 xev = &last_event;
1975 } else {
1976 break;
1980 ui::MouseEvent mouseev(xev);
1981 DispatchMouseEvent(&mouseev);
1982 break;
1984 case PropertyNotify: {
1985 ::Atom changed_atom = xev->xproperty.atom;
1986 if (changed_atom == atom_cache_.GetAtom("_NET_WM_STATE"))
1987 OnWMStateUpdated();
1988 else if (changed_atom == atom_cache_.GetAtom("_NET_FRAME_EXTENTS"))
1989 OnFrameExtentsUpdated();
1990 break;
1992 case SelectionNotify: {
1993 drag_drop_client_->OnSelectionNotify(xev->xselection);
1994 break;
1997 return ui::POST_DISPATCH_STOP_PROPAGATION;
2000 void DesktopWindowTreeHostX11::DelayedResize(const gfx::Size& size_in_pixels) {
2001 OnHostResized(size_in_pixels);
2002 ResetWindowRegion();
2003 delayed_resize_task_.Cancel();
2006 gfx::Rect DesktopWindowTreeHostX11::GetWorkAreaBoundsInPixels() const {
2007 std::vector<int> value;
2008 if (ui::GetIntArrayProperty(x_root_window_, "_NET_WORKAREA", &value) &&
2009 value.size() >= 4) {
2010 return gfx::Rect(value[0], value[1], value[2], value[3]);
2013 // Fetch the geometry of the root window.
2014 Window root;
2015 int x, y;
2016 unsigned int width, height;
2017 unsigned int border_width, depth;
2018 if (!XGetGeometry(xdisplay_, x_root_window_, &root, &x, &y, &width, &height,
2019 &border_width, &depth)) {
2020 NOTIMPLEMENTED();
2021 return gfx::Rect(0, 0, 10, 10);
2024 return gfx::Rect(x, y, width, height);
2027 gfx::Rect DesktopWindowTreeHostX11::ToDIPRect(
2028 const gfx::Rect& rect_in_pixels) const {
2029 gfx::RectF rect_in_dip = rect_in_pixels;
2030 GetRootTransform().TransformRectReverse(&rect_in_dip);
2031 return gfx::ToEnclosingRect(rect_in_dip);
2034 gfx::Rect DesktopWindowTreeHostX11::ToPixelRect(
2035 const gfx::Rect& rect_in_dip) const {
2036 gfx::RectF rect_in_pixels = rect_in_dip;
2037 GetRootTransform().TransformRect(&rect_in_pixels);
2038 return gfx::ToEnclosingRect(rect_in_pixels);
2041 ////////////////////////////////////////////////////////////////////////////////
2042 // DesktopWindowTreeHost, public:
2044 // static
2045 DesktopWindowTreeHost* DesktopWindowTreeHost::Create(
2046 internal::NativeWidgetDelegate* native_widget_delegate,
2047 DesktopNativeWidgetAura* desktop_native_widget_aura) {
2048 return new DesktopWindowTreeHostX11(native_widget_delegate,
2049 desktop_native_widget_aura);
2052 // static
2053 ui::NativeTheme* DesktopWindowTreeHost::GetNativeTheme(aura::Window* window) {
2054 const views::LinuxUI* linux_ui = views::LinuxUI::instance();
2055 if (linux_ui) {
2056 ui::NativeTheme* native_theme = linux_ui->GetNativeTheme(window);
2057 if (native_theme)
2058 return native_theme;
2061 return ui::NativeTheme::instance();
2064 } // namespace views