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 "base/message_loop/message_loop.h"
8 #include "ui/aura/env.h"
9 #include "ui/aura/root_window.h"
10 #include "ui/base/x/x11_util.h"
12 #if !defined(OS_CHROMEOS)
13 #include "ui/views/ime/input_method.h"
14 #include "ui/views/widget/desktop_aura/desktop_root_window_host_x11.h"
19 const char* kAtomsToCache[] = {
24 // Our global instance. Deleted when our Env() is deleted.
25 views::X11DesktopHandler* g_handler = NULL;
32 X11DesktopHandler* X11DesktopHandler::get() {
34 g_handler = new X11DesktopHandler;
39 X11DesktopHandler::X11DesktopHandler()
40 : xdisplay_(gfx::GetXDisplay()),
41 x_root_window_(DefaultRootWindow(xdisplay_)),
42 current_window_(None),
43 atom_cache_(xdisplay_, kAtomsToCache) {
44 base::MessagePumpX11::Current()->AddDispatcherForRootWindow(this);
45 aura::Env::GetInstance()->AddObserver(this);
47 XWindowAttributes attr;
48 XGetWindowAttributes(xdisplay_, x_root_window_, &attr);
49 XSelectInput(xdisplay_, x_root_window_,
50 attr.your_event_mask | PropertyChangeMask |
51 StructureNotifyMask | SubstructureNotifyMask);
54 X11DesktopHandler::~X11DesktopHandler() {
55 aura::Env::GetInstance()->RemoveObserver(this);
56 base::MessagePumpX11::Current()->RemoveDispatcherForRootWindow(this);
59 void X11DesktopHandler::ActivateWindow(::Window window) {
60 DCHECK_EQ(gfx::GetXDisplay(), xdisplay_);
63 memset(&xclient, 0, sizeof(xclient));
64 xclient.type = ClientMessage;
65 xclient.xclient.window = window;
66 xclient.xclient.message_type = atom_cache_.GetAtom("_NET_ACTIVE_WINDOW");
67 xclient.xclient.format = 32;
68 xclient.xclient.data.l[0] = 1; // Specified we are an app.
69 xclient.xclient.data.l[1] = CurrentTime;
70 xclient.xclient.data.l[2] = None;
71 xclient.xclient.data.l[3] = 0;
72 xclient.xclient.data.l[4] = 0;
74 XSendEvent(xdisplay_, x_root_window_, False,
75 SubstructureRedirectMask | SubstructureNotifyMask,
79 bool X11DesktopHandler::IsActiveWindow(::Window window) const {
80 return window == current_window_;
83 bool X11DesktopHandler::Dispatch(const base::NativeEvent& event) {
84 // Check for a change to the active window.
85 switch (event->type) {
86 case PropertyNotify: {
87 ::Atom active_window = atom_cache_.GetAtom("_NET_ACTIVE_WINDOW");
89 if (event->xproperty.window == x_root_window_ &&
90 event->xproperty.atom == active_window) {
92 if (ui::GetIntProperty(x_root_window_, "_NET_ACTIVE_WINDOW", &window) &&
94 OnActiveWindowChanged(static_cast< ::Window>(window));
104 void X11DesktopHandler::OnWindowInitialized(aura::Window* window) {
107 void X11DesktopHandler::OnWillDestroyEnv() {
112 void X11DesktopHandler::OnActiveWindowChanged(::Window xid) {
113 DesktopRootWindowHostX11* old_host =
114 views::DesktopRootWindowHostX11::GetHostForXID(current_window_);
116 old_host->HandleNativeWidgetActivationChanged(false);
118 DesktopRootWindowHostX11* new_host =
119 views::DesktopRootWindowHostX11::GetHostForXID(xid);
121 new_host->HandleNativeWidgetActivationChanged(true);
123 current_window_ = xid;