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