OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include <X11/Xlib.h> | |
6 #include <gdk/gdk.h> | |
7 #include <gdk/gdkx.h> | |
8 | |
9 #include "app/active_window_watcher_x.h" | |
10 | |
11 static Atom kNetActiveWindowAtom = None; | |
12 | |
13 // static | |
14 ActiveWindowWatcherX* ActiveWindowWatcherX::GetInstance() { | |
15 return Singleton<ActiveWindowWatcherX>::get(); | |
16 } | |
17 | |
18 // static | |
19 void ActiveWindowWatcherX::AddObserver(Observer* observer) { | |
20 GetInstance()->observers_.AddObserver(observer); | |
21 } | |
22 | |
23 // static | |
24 void ActiveWindowWatcherX::RemoveObserver(Observer* observer) { | |
25 GetInstance()->observers_.RemoveObserver(observer); | |
26 } | |
27 | |
28 ActiveWindowWatcherX::ActiveWindowWatcherX() { | |
29 Init(); | |
30 } | |
31 | |
32 ActiveWindowWatcherX::~ActiveWindowWatcherX() { | |
33 } | |
34 | |
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); | |
39 | |
40 GdkWindow* root = gdk_get_default_root_window(); | |
41 | |
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); | |
50 } | |
51 | |
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. | |
56 Atom type = None; | |
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; | |
60 | |
61 XGetWindowProperty(gdk_x11_get_default_xdisplay(), | |
62 GDK_WINDOW_XID(gdk_get_default_root_window()), | |
63 kNetActiveWindowAtom, | |
64 0, // offset into property data to read | |
65 1, // length to get in 32-bit quantities | |
66 False, // deleted | |
67 AnyPropertyType, | |
68 &type, | |
69 &format, | |
70 &num_items, | |
71 &remaining_bytes, | |
72 &property); | |
73 | |
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 | |
77 // reason.) | |
78 if (format == 32 && num_items == 1) { | |
79 int xid = *reinterpret_cast<int*>(property); | |
80 GdkWindow* active_window = gdk_window_lookup(xid); | |
81 FOR_EACH_OBSERVER( | |
82 Observer, | |
83 observers_, | |
84 ActiveWindowChanged(active_window)); | |
85 } | |
86 if (property) | |
87 XFree(property); | |
88 } | |
89 | |
90 GdkFilterReturn ActiveWindowWatcherX::OnWindowXEvent(GdkXEvent* xevent, | |
91 GdkEvent* event, gpointer window_watcher) { | |
92 ActiveWindowWatcherX* watcher = reinterpret_cast<ActiveWindowWatcherX*>( | |
93 window_watcher); | |
94 XEvent* xev = static_cast<XEvent*>(xevent); | |
95 | |
96 if (xev->xany.type == PropertyNotify && | |
97 xev->xproperty.atom == kNetActiveWindowAtom) { | |
98 watcher->NotifyActiveWindowChanged(); | |
99 } | |
100 | |
101 return GDK_FILTER_CONTINUE; | |
102 } | |
OLD | NEW |