- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / widget / desktop_aura / x11_desktop_handler.cc
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.
4
5 #include "ui/views/widget/desktop_aura/x11_desktop_handler.h"
6
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"
11
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"
15 #endif
16
17 namespace {
18
19 const char* kAtomsToCache[] = {
20   "_NET_ACTIVE_WINDOW",
21   NULL
22 };
23
24 // Our global instance. Deleted when our Env() is deleted.
25 views::X11DesktopHandler* g_handler = NULL;
26
27 }  // namespace
28
29 namespace views {
30
31 // static
32 X11DesktopHandler* X11DesktopHandler::get() {
33   if (!g_handler)
34     g_handler = new X11DesktopHandler;
35
36   return g_handler;
37 }
38
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);
46
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);
52 }
53
54 X11DesktopHandler::~X11DesktopHandler() {
55   aura::Env::GetInstance()->RemoveObserver(this);
56   base::MessagePumpX11::Current()->RemoveDispatcherForRootWindow(this);
57 }
58
59 void X11DesktopHandler::ActivateWindow(::Window window) {
60   DCHECK_EQ(gfx::GetXDisplay(), xdisplay_);
61
62   XEvent xclient;
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;
73
74   XSendEvent(xdisplay_, x_root_window_, False,
75              SubstructureRedirectMask | SubstructureNotifyMask,
76              &xclient);
77 }
78
79 bool X11DesktopHandler::IsActiveWindow(::Window window) const {
80   return window == current_window_;
81 }
82
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");
88
89       if (event->xproperty.window == x_root_window_ &&
90           event->xproperty.atom == active_window) {
91         int window;
92         if (ui::GetIntProperty(x_root_window_, "_NET_ACTIVE_WINDOW", &window) &&
93             window) {
94           OnActiveWindowChanged(static_cast< ::Window>(window));
95         }
96       }
97       break;
98     }
99   }
100
101   return true;
102 }
103
104 void X11DesktopHandler::OnWindowInitialized(aura::Window* window) {
105 }
106
107 void X11DesktopHandler::OnWillDestroyEnv() {
108   g_handler = NULL;
109   delete this;
110 }
111
112 void X11DesktopHandler::OnActiveWindowChanged(::Window xid) {
113   DesktopRootWindowHostX11* old_host =
114       views::DesktopRootWindowHostX11::GetHostForXID(current_window_);
115   if (old_host)
116     old_host->HandleNativeWidgetActivationChanged(false);
117
118   DesktopRootWindowHostX11* new_host =
119       views::DesktopRootWindowHostX11::GetHostForXID(xid);
120   if (new_host)
121     new_host->HandleNativeWidgetActivationChanged(true);
122
123   current_window_ = xid;
124 }
125
126 }  // namespace views