| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <X11/Xlib.h> | 5 #include "ui/base/x/active_window_watcher_x.h" |
| 6 |
| 6 #include <gdk/gdk.h> | 7 #include <gdk/gdk.h> |
| 7 #include <gdk/gdkx.h> | 8 #include <gdk/gdkx.h> |
| 8 | 9 |
| 9 #include "ui/base/x/active_window_watcher_x.h" | 10 #include "ui/base/x/active_window_watcher_x_observer.h" |
| 11 #include "ui/base/x/root_window_property_watcher_x.h" |
| 12 #include "ui/base/x/x11_util.h" |
| 10 | 13 |
| 11 namespace ui { | 14 namespace ui { |
| 12 | 15 |
| 13 static Atom g_net_active_window_atom = None; | 16 static const char* const kNetActiveWindow = "_NET_ACTIVE_WINDOW"; |
| 14 | 17 |
| 15 // static | 18 // static |
| 16 ActiveWindowWatcherX* ActiveWindowWatcherX::GetInstance() { | 19 ActiveWindowWatcherX* ActiveWindowWatcherX::GetInstance() { |
| 17 return Singleton<ActiveWindowWatcherX>::get(); | 20 return Singleton<ActiveWindowWatcherX>::get(); |
| 18 } | 21 } |
| 19 | 22 |
| 20 // static | 23 // static |
| 21 void ActiveWindowWatcherX::AddObserver(Observer* observer) { | 24 void ActiveWindowWatcherX::AddObserver(ActiveWindowWatcherXObserver* observer) { |
| 25 // Ensure that RootWindowPropertyWatcherX exists. |
| 26 internal::RootWindowPropertyWatcherX::GetInstance(); |
| 22 GetInstance()->observers_.AddObserver(observer); | 27 GetInstance()->observers_.AddObserver(observer); |
| 23 } | 28 } |
| 24 | 29 |
| 25 // static | 30 // static |
| 26 void ActiveWindowWatcherX::RemoveObserver(Observer* observer) { | 31 void ActiveWindowWatcherX::RemoveObserver( |
| 32 ActiveWindowWatcherXObserver* observer) { |
| 27 GetInstance()->observers_.RemoveObserver(observer); | 33 GetInstance()->observers_.RemoveObserver(observer); |
| 28 } | 34 } |
| 29 | 35 |
| 30 // static | 36 // static |
| 37 Atom ActiveWindowWatcherX::GetPropertyAtom() { |
| 38 return GetAtom(kNetActiveWindow); |
| 39 } |
| 40 |
| 41 // static |
| 42 void ActiveWindowWatcherX::Notify() { |
| 43 GetInstance()->NotifyActiveWindowChanged(); |
| 44 } |
| 45 |
| 46 // static |
| 31 bool ActiveWindowWatcherX::WMSupportsActivation() { | 47 bool ActiveWindowWatcherX::WMSupportsActivation() { |
| 32 return gdk_x11_screen_supports_net_wm_hint( | 48 return gdk_x11_screen_supports_net_wm_hint( |
| 33 gdk_screen_get_default(), | 49 gdk_screen_get_default(), |
| 34 gdk_atom_intern_static_string("_NET_ACTIVE_WINDOW")); | 50 gdk_atom_intern_static_string(kNetActiveWindow)); |
| 35 } | 51 } |
| 36 | 52 |
| 37 ActiveWindowWatcherX::ActiveWindowWatcherX() { | 53 ActiveWindowWatcherX::ActiveWindowWatcherX() { |
| 38 Init(); | |
| 39 } | 54 } |
| 40 | 55 |
| 41 ActiveWindowWatcherX::~ActiveWindowWatcherX() { | 56 ActiveWindowWatcherX::~ActiveWindowWatcherX() { |
| 42 } | 57 } |
| 43 | 58 |
| 44 void ActiveWindowWatcherX::Init() { | |
| 45 GdkAtom net_active_window = | |
| 46 gdk_atom_intern_static_string("_NET_ACTIVE_WINDOW"); | |
| 47 g_net_active_window_atom = gdk_x11_atom_to_xatom_for_display( | |
| 48 gdk_screen_get_display(gdk_screen_get_default()), net_active_window); | |
| 49 | |
| 50 GdkWindow* root = gdk_get_default_root_window(); | |
| 51 | |
| 52 // Set up X Event filter to listen for PropertyChange X events. These events | |
| 53 // tell us when the active window changes. | |
| 54 // Don't use XSelectInput directly here, as gdk internally seems to cache the | |
| 55 // mask and reapply XSelectInput after this, resetting any mask we set here. | |
| 56 gdk_window_set_events(root, | |
| 57 static_cast<GdkEventMask>(gdk_window_get_events(root) | | |
| 58 GDK_PROPERTY_CHANGE_MASK)); | |
| 59 gdk_window_add_filter(NULL, &ActiveWindowWatcherX::OnWindowXEventThunk, this); | |
| 60 } | |
| 61 | |
| 62 void ActiveWindowWatcherX::NotifyActiveWindowChanged() { | 59 void ActiveWindowWatcherX::NotifyActiveWindowChanged() { |
| 63 // We don't use gdk_screen_get_active_window() because it caches | 60 // We don't use gdk_screen_get_active_window() because it caches |
| 64 // whether or not the window manager supports _NET_ACTIVE_WINDOW. | 61 // whether or not the window manager supports _NET_ACTIVE_WINDOW. |
| 65 // This causes problems at startup for chromiumos. | 62 // This causes problems at startup for chromiumos. |
| 66 Atom type = None; | 63 Atom type = None; |
| 67 int format = 0; // size in bits of each item in 'property' | 64 int format = 0; // size in bits of each item in 'property' |
| 68 long unsigned int num_items = 0, remaining_bytes = 0; | 65 long unsigned int num_items = 0, remaining_bytes = 0; |
| 69 unsigned char* property = NULL; | 66 unsigned char* property = NULL; |
| 70 | 67 |
| 71 XGetWindowProperty(gdk_x11_get_default_xdisplay(), | 68 XGetWindowProperty(gdk_x11_get_default_xdisplay(), |
| 72 GDK_WINDOW_XID(gdk_get_default_root_window()), | 69 GDK_WINDOW_XID(gdk_get_default_root_window()), |
| 73 g_net_active_window_atom, | 70 GetAtom(kNetActiveWindow), |
| 74 0, // offset into property data to read | 71 0, // offset into property data to read |
| 75 1, // length to get in 32-bit quantities | 72 1, // length to get in 32-bit quantities |
| 76 False, // deleted | 73 False, // deleted |
| 77 AnyPropertyType, | 74 AnyPropertyType, |
| 78 &type, | 75 &type, |
| 79 &format, | 76 &format, |
| 80 &num_items, | 77 &num_items, |
| 81 &remaining_bytes, | 78 &remaining_bytes, |
| 82 &property); | 79 &property); |
| 83 | 80 |
| 84 // Check that the property was set and contained a single 32-bit item (we | 81 // Check that the property was set and contained a single 32-bit item (we |
| 85 // don't check that remaining_bytes is 0, though, as XFCE's window manager | 82 // don't check that remaining_bytes is 0, though, as XFCE's window manager |
| 86 // seems to actually store two values in the property for some unknown | 83 // seems to actually store two values in the property for some unknown |
| 87 // reason.) | 84 // reason.) |
| 88 if (format == 32 && num_items == 1) { | 85 if (format == 32 && num_items == 1) { |
| 89 int xid = *reinterpret_cast<int*>(property); | 86 int xid = *reinterpret_cast<int*>(property); |
| 90 GdkWindow* active_window = gdk_window_lookup(xid); | 87 GdkWindow* active_window = gdk_window_lookup(xid); |
| 91 FOR_EACH_OBSERVER( | 88 FOR_EACH_OBSERVER(ActiveWindowWatcherXObserver, observers_, |
| 92 Observer, | 89 ActiveWindowChanged(active_window)); |
| 93 observers_, | |
| 94 ActiveWindowChanged(active_window)); | |
| 95 } | 90 } |
| 96 if (property) | 91 if (property) |
| 97 XFree(property); | 92 XFree(property); |
| 98 } | 93 } |
| 99 | 94 |
| 100 GdkFilterReturn ActiveWindowWatcherX::OnWindowXEvent(GdkXEvent* xevent, | |
| 101 GdkEvent* event) { | |
| 102 XEvent* xev = static_cast<XEvent*>(xevent); | |
| 103 | |
| 104 if (xev->xany.type == PropertyNotify && | |
| 105 xev->xproperty.atom == g_net_active_window_atom) { | |
| 106 NotifyActiveWindowChanged(); | |
| 107 } | |
| 108 | |
| 109 return GDK_FILTER_CONTINUE; | |
| 110 } | |
| 111 | |
| 112 } // namespace ui | 95 } // namespace ui |
| OLD | NEW |