| 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/root_window_property_watcher_x.h" |
| 11 #include "ui/base/x/x11_util.h" |
| 10 | 12 |
| 11 namespace ui { | 13 namespace ui { |
| 12 | 14 |
| 13 static Atom g_net_active_window_atom = None; | 15 static const char* kNetActiveWindow = "_NET_ACTIVE_WINDOW"; |
| 16 |
| 17 ActiveWindowWatcherX::Observer::~Observer() { |
| 18 } |
| 14 | 19 |
| 15 // static | 20 // static |
| 16 ActiveWindowWatcherX* ActiveWindowWatcherX::GetInstance() { | 21 ActiveWindowWatcherX* ActiveWindowWatcherX::GetInstance() { |
| 17 return Singleton<ActiveWindowWatcherX>::get(); | 22 return Singleton<ActiveWindowWatcherX>::get(); |
| 18 } | 23 } |
| 19 | 24 |
| 20 // static | 25 // static |
| 21 void ActiveWindowWatcherX::AddObserver(Observer* observer) { | 26 void ActiveWindowWatcherX::AddObserver(Observer* observer) { |
| 27 // Ensure that RootWindowPropertyWatcherX exists. |
| 28 internal::RootWindowPropertyWatcherX::GetInstance(); |
| 22 GetInstance()->observers_.AddObserver(observer); | 29 GetInstance()->observers_.AddObserver(observer); |
| 23 } | 30 } |
| 24 | 31 |
| 25 // static | 32 // static |
| 26 void ActiveWindowWatcherX::RemoveObserver(Observer* observer) { | 33 void ActiveWindowWatcherX::RemoveObserver(Observer* observer) { |
| 27 GetInstance()->observers_.RemoveObserver(observer); | 34 GetInstance()->observers_.RemoveObserver(observer); |
| 28 } | 35 } |
| 29 | 36 |
| 30 // static | 37 // static |
| 38 Atom ActiveWindowWatcherX::GetPropertyAtom() { |
| 39 return GetAtom(kNetActiveWindow); |
| 40 } |
| 41 |
| 42 // static |
| 43 void ActiveWindowWatcherX::Notify() { |
| 44 GetInstance()->NotifyActiveWindowChanged(); |
| 45 } |
| 46 |
| 47 // static |
| 31 bool ActiveWindowWatcherX::WMSupportsActivation() { | 48 bool ActiveWindowWatcherX::WMSupportsActivation() { |
| 32 return gdk_x11_screen_supports_net_wm_hint( | 49 return gdk_x11_screen_supports_net_wm_hint( |
| 33 gdk_screen_get_default(), | 50 gdk_screen_get_default(), |
| 34 gdk_atom_intern_static_string("_NET_ACTIVE_WINDOW")); | 51 gdk_atom_intern_static_string(kNetActiveWindow)); |
| 35 } | 52 } |
| 36 | 53 |
| 37 ActiveWindowWatcherX::ActiveWindowWatcherX() { | 54 ActiveWindowWatcherX::ActiveWindowWatcherX() { |
| 38 Init(); | |
| 39 } | 55 } |
| 40 | 56 |
| 41 ActiveWindowWatcherX::~ActiveWindowWatcherX() { | 57 ActiveWindowWatcherX::~ActiveWindowWatcherX() { |
| 42 } | 58 } |
| 43 | 59 |
| 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() { | 60 void ActiveWindowWatcherX::NotifyActiveWindowChanged() { |
| 63 // We don't use gdk_screen_get_active_window() because it caches | 61 // We don't use gdk_screen_get_active_window() because it caches |
| 64 // whether or not the window manager supports _NET_ACTIVE_WINDOW. | 62 // whether or not the window manager supports _NET_ACTIVE_WINDOW. |
| 65 // This causes problems at startup for chromiumos. | 63 // This causes problems at startup for chromiumos. |
| 66 Atom type = None; | 64 Atom type = None; |
| 67 int format = 0; // size in bits of each item in 'property' | 65 int format = 0; // size in bits of each item in 'property' |
| 68 long unsigned int num_items = 0, remaining_bytes = 0; | 66 long unsigned int num_items = 0, remaining_bytes = 0; |
| 69 unsigned char* property = NULL; | 67 unsigned char* property = NULL; |
| 70 | 68 |
| 71 XGetWindowProperty(gdk_x11_get_default_xdisplay(), | 69 XGetWindowProperty(gdk_x11_get_default_xdisplay(), |
| 72 GDK_WINDOW_XID(gdk_get_default_root_window()), | 70 GDK_WINDOW_XID(gdk_get_default_root_window()), |
| 73 g_net_active_window_atom, | 71 GetAtom(kNetActiveWindow), |
| 74 0, // offset into property data to read | 72 0, // offset into property data to read |
| 75 1, // length to get in 32-bit quantities | 73 1, // length to get in 32-bit quantities |
| 76 False, // deleted | 74 False, // deleted |
| 77 AnyPropertyType, | 75 AnyPropertyType, |
| 78 &type, | 76 &type, |
| 79 &format, | 77 &format, |
| 80 &num_items, | 78 &num_items, |
| 81 &remaining_bytes, | 79 &remaining_bytes, |
| 82 &property); | 80 &property); |
| 83 | 81 |
| 84 // Check that the property was set and contained a single 32-bit item (we | 82 // 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 | 83 // 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 | 84 // seems to actually store two values in the property for some unknown |
| 87 // reason.) | 85 // reason.) |
| 88 if (format == 32 && num_items == 1) { | 86 if (format == 32 && num_items == 1) { |
| 89 int xid = *reinterpret_cast<int*>(property); | 87 int xid = *reinterpret_cast<int*>(property); |
| 90 GdkWindow* active_window = gdk_window_lookup(xid); | 88 GdkWindow* active_window = gdk_window_lookup(xid); |
| 91 FOR_EACH_OBSERVER( | 89 FOR_EACH_OBSERVER(Observer, observers_, ActiveWindowChanged(active_window)); |
| 92 Observer, | |
| 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 |