Android: Get rid of extra dup()s on launching child processes
[chromium-blink-merge.git] / ui / views / widget / desktop_aura / x11_desktop_handler.cc
blobee90cbc5fae046d7aeee9284c7adbb84b0e58f4c
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/x11_desktop_handler.h"
7 #include <X11/Xatom.h>
8 #include <X11/Xlib.h>
10 #include "base/message_loop/message_loop.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/window_event_dispatcher.h"
13 #include "ui/base/x/x11_foreign_window_manager.h"
14 #include "ui/base/x/x11_menu_list.h"
15 #include "ui/base/x/x11_util.h"
16 #include "ui/events/platform/platform_event_source.h"
17 #include "ui/gfx/x/x11_error_tracker.h"
18 #include "ui/views/ime/input_method.h"
19 #include "ui/views/widget/desktop_aura/desktop_window_tree_host_x11.h"
21 namespace {
23 const char* kAtomsToCache[] = {
24 "_NET_ACTIVE_WINDOW",
25 NULL
28 // Our global instance. Deleted when our Env() is deleted.
29 views::X11DesktopHandler* g_handler = NULL;
31 } // namespace
33 namespace views {
35 // static
36 X11DesktopHandler* X11DesktopHandler::get() {
37 if (!g_handler)
38 g_handler = new X11DesktopHandler;
40 return g_handler;
43 X11DesktopHandler::X11DesktopHandler()
44 : xdisplay_(gfx::GetXDisplay()),
45 x_root_window_(DefaultRootWindow(xdisplay_)),
46 wm_user_time_ms_(0),
47 current_window_(None),
48 current_window_active_state_(NOT_ACTIVE),
49 atom_cache_(xdisplay_, kAtomsToCache),
50 wm_supports_active_window_(false) {
51 if (ui::PlatformEventSource::GetInstance())
52 ui::PlatformEventSource::GetInstance()->AddPlatformEventDispatcher(this);
53 aura::Env::GetInstance()->AddObserver(this);
55 XWindowAttributes attr;
56 XGetWindowAttributes(xdisplay_, x_root_window_, &attr);
57 XSelectInput(xdisplay_, x_root_window_,
58 attr.your_event_mask | PropertyChangeMask |
59 StructureNotifyMask | SubstructureNotifyMask);
61 if (ui::GuessWindowManager() == ui::WM_WMII) {
62 // wmii says that it supports _NET_ACTIVE_WINDOW but does not.
63 // https://code.google.com/p/wmii/issues/detail?id=266
64 wm_supports_active_window_ = false;
65 } else {
66 wm_supports_active_window_ =
67 ui::WmSupportsHint(atom_cache_.GetAtom("_NET_ACTIVE_WINDOW"));
71 X11DesktopHandler::~X11DesktopHandler() {
72 aura::Env::GetInstance()->RemoveObserver(this);
73 if (ui::PlatformEventSource::GetInstance())
74 ui::PlatformEventSource::GetInstance()->RemovePlatformEventDispatcher(this);
77 void X11DesktopHandler::ActivateWindow(::Window window) {
78 if (current_window_ == window &&
79 current_window_active_state_ == NOT_ACTIVE) {
80 // |window| is most likely still active wrt to the X server. Undo the
81 // changes made in DeactivateWindow().
82 OnActiveWindowChanged(window, ACTIVE);
84 // Go through the regular activation path such that calling
85 // DeactivateWindow() and ActivateWindow() immediately afterwards results
86 // in an active X window.
89 XRaiseWindow(xdisplay_, window);
91 if (wm_supports_active_window_) {
92 DCHECK_EQ(gfx::GetXDisplay(), xdisplay_);
94 XEvent xclient;
95 memset(&xclient, 0, sizeof(xclient));
96 xclient.type = ClientMessage;
97 xclient.xclient.window = window;
98 xclient.xclient.message_type = atom_cache_.GetAtom("_NET_ACTIVE_WINDOW");
99 xclient.xclient.format = 32;
100 xclient.xclient.data.l[0] = 1; // Specified we are an app.
101 xclient.xclient.data.l[1] = wm_user_time_ms_;
102 xclient.xclient.data.l[2] = None;
103 xclient.xclient.data.l[3] = 0;
104 xclient.xclient.data.l[4] = 0;
106 XSendEvent(xdisplay_, x_root_window_, False,
107 SubstructureRedirectMask | SubstructureNotifyMask,
108 &xclient);
109 } else {
110 // XRaiseWindow will not give input focus to the window. We now need to ask
111 // the X server to do that. Note that the call will raise an X error if the
112 // window is not mapped.
113 XSetInputFocus(xdisplay_, window, RevertToParent, CurrentTime);
115 OnActiveWindowChanged(window, ACTIVE);
119 void X11DesktopHandler::DeactivateWindow(::Window window) {
120 if (!IsActiveWindow(window))
121 return;
123 XLowerWindow(xdisplay_, window);
125 // Per ICCCM: http://tronche.com/gui/x/icccm/sec-4.html#s-4.1.7
126 // "Clients should not give up the input focus of their own volition.
127 // They should ignore input that they receive instead."
129 // There is nothing else that we can do. Pretend that we have been
130 // deactivated and ignore keyboard input in DesktopWindowTreeHostX11.
131 OnActiveWindowChanged(window, NOT_ACTIVE);
134 bool X11DesktopHandler::IsActiveWindow(::Window window) const {
135 return window == current_window_ && current_window_active_state_ == ACTIVE;
138 void X11DesktopHandler::ProcessXEvent(XEvent* event) {
139 switch (event->type) {
140 case FocusIn:
141 if (current_window_ != event->xfocus.window)
142 OnActiveWindowChanged(event->xfocus.window, ACTIVE);
143 break;
144 case FocusOut:
145 if (current_window_ == event->xfocus.window)
146 OnActiveWindowChanged(None, NOT_ACTIVE);
147 break;
148 default:
149 NOTREACHED();
153 bool X11DesktopHandler::CanDispatchEvent(const ui::PlatformEvent& event) {
154 return event->type == CreateNotify || event->type == DestroyNotify ||
155 (event->type == PropertyNotify &&
156 event->xproperty.window == x_root_window_);
159 uint32_t X11DesktopHandler::DispatchEvent(const ui::PlatformEvent& event) {
160 switch (event->type) {
161 case PropertyNotify: {
162 // Check for a change to the active window.
163 CHECK_EQ(x_root_window_, event->xproperty.window);
164 ::Atom active_window_atom = atom_cache_.GetAtom("_NET_ACTIVE_WINDOW");
165 if (event->xproperty.atom == active_window_atom) {
166 ::Window window;
167 if (ui::GetXIDProperty(x_root_window_, "_NET_ACTIVE_WINDOW", &window) &&
168 window) {
169 OnActiveWindowChanged(window, ACTIVE);
172 break;
175 case CreateNotify:
176 OnWindowCreatedOrDestroyed(event->type, event->xcreatewindow.window);
177 break;
178 case DestroyNotify:
179 OnWindowCreatedOrDestroyed(event->type, event->xdestroywindow.window);
180 break;
181 default:
182 NOTREACHED();
185 return ui::POST_DISPATCH_NONE;
188 void X11DesktopHandler::OnWindowInitialized(aura::Window* window) {
191 void X11DesktopHandler::OnWillDestroyEnv() {
192 g_handler = NULL;
193 delete this;
196 void X11DesktopHandler::OnActiveWindowChanged(::Window xid,
197 ActiveState active_state) {
198 if (current_window_ == xid && current_window_active_state_ == active_state)
199 return;
201 if (current_window_active_state_ == ACTIVE) {
202 DesktopWindowTreeHostX11* old_host =
203 views::DesktopWindowTreeHostX11::GetHostForXID(current_window_);
204 if (old_host)
205 old_host->HandleNativeWidgetActivationChanged(false);
208 // Update the current window ID to effectively change the active widget.
209 current_window_ = xid;
210 current_window_active_state_ = active_state;
212 if (active_state == ACTIVE) {
213 DesktopWindowTreeHostX11* new_host =
214 views::DesktopWindowTreeHostX11::GetHostForXID(xid);
215 if (new_host)
216 new_host->HandleNativeWidgetActivationChanged(true);
220 void X11DesktopHandler::OnWindowCreatedOrDestroyed(int event_type,
221 XID window) {
222 // Menus created by Chrome can be drag and drop targets. Since they are
223 // direct children of the screen root window and have override_redirect
224 // we cannot use regular _NET_CLIENT_LIST_STACKING property to find them
225 // and use a separate cache to keep track of them.
226 // TODO(varkha): Implement caching of all top level X windows and their
227 // coordinates and stacking order to eliminate repeated calls to the X server
228 // during mouse movement, drag and shaping events.
229 if (event_type == CreateNotify) {
230 // The window might be destroyed if the message pump did not get a chance to
231 // run but we can safely ignore the X error.
232 gfx::X11ErrorTracker error_tracker;
233 ui::XMenuList::GetInstance()->MaybeRegisterMenu(window);
234 } else {
235 ui::XMenuList::GetInstance()->MaybeUnregisterMenu(window);
238 if (event_type == DestroyNotify) {
239 // Notify the XForeignWindowManager that |window| has been destroyed.
240 ui::XForeignWindowManager::GetInstance()->OnWindowDestroyed(window);
244 } // namespace views