1 // Copyright (c) 2009 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.
9 #include "app/active_window_watcher_x.h"
11 static Atom kNetActiveWindowAtom
= None
;
14 ActiveWindowWatcherX
* ActiveWindowWatcherX::GetInstance() {
15 return Singleton
<ActiveWindowWatcherX
>::get();
19 void ActiveWindowWatcherX::AddObserver(Observer
* observer
) {
20 GetInstance()->observers_
.AddObserver(observer
);
24 void ActiveWindowWatcherX::RemoveObserver(Observer
* observer
) {
25 GetInstance()->observers_
.RemoveObserver(observer
);
28 ActiveWindowWatcherX::ActiveWindowWatcherX() {
32 ActiveWindowWatcherX::~ActiveWindowWatcherX() {
35 void ActiveWindowWatcherX::Init() {
36 GdkAtom kNetActiveWindow
= gdk_atom_intern("_NET_ACTIVE_WINDOW", FALSE
);
37 kNetActiveWindowAtom
= gdk_x11_atom_to_xatom_for_display(
38 gdk_screen_get_display(gdk_screen_get_default()), kNetActiveWindow
);
40 GdkWindow
* root
= gdk_get_default_root_window();
42 // Set up X Event filter to listen for PropertyChange X events. These events
43 // tell us when the active window changes.
44 // Don't use XSelectInput directly here, as gdk internally seems to cache the
45 // mask and reapply XSelectInput after this, resetting any mask we set here.
46 gdk_window_set_events(root
,
47 static_cast<GdkEventMask
>(gdk_window_get_events(root
) |
48 GDK_PROPERTY_CHANGE_MASK
));
49 gdk_window_add_filter(NULL
, &ActiveWindowWatcherX::OnWindowXEvent
, this);
52 void ActiveWindowWatcherX::NotifyActiveWindowChanged() {
53 // We don't use gdk_screen_get_active_window() because it caches
54 // whether or not the window manager supports _NET_ACTIVE_WINDOW.
55 // This causes problems at startup for chromiumos.
57 int format
= 0; // size in bits of each item in 'property'
58 long unsigned int num_items
= 0, remaining_bytes
= 0;
59 unsigned char* property
= NULL
;
61 XGetWindowProperty(gdk_x11_get_default_xdisplay(),
62 GDK_WINDOW_XID(gdk_get_default_root_window()),
64 0, // offset into property data to read
65 1, // length to get in 32-bit quantities
74 // Check that the property was set and contained a single 32-bit item (we
75 // don't check that remaining_bytes is 0, though, as XFCE's window manager
76 // seems to actually store two values in the property for some unknown
78 if (format
== 32 && num_items
== 1) {
79 int xid
= *reinterpret_cast<int*>(property
);
80 GdkWindow
* active_window
= gdk_window_lookup(xid
);
84 ActiveWindowChanged(active_window
));
90 GdkFilterReturn
ActiveWindowWatcherX::OnWindowXEvent(GdkXEvent
* xevent
,
91 GdkEvent
* event
, gpointer window_watcher
) {
92 ActiveWindowWatcherX
* watcher
= reinterpret_cast<ActiveWindowWatcherX
*>(
94 XEvent
* xev
= static_cast<XEvent
*>(xevent
);
96 if (xev
->xany
.type
== PropertyNotify
&&
97 xev
->xproperty
.atom
== kNetActiveWindowAtom
) {
98 watcher
->NotifyActiveWindowChanged();
101 return GDK_FILTER_CONTINUE
;