Home | History | Annotate | Download | only in x
      1 // Copyright (c) 2012 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 "ui/base/x/root_window_property_watcher_x.h"
      6 
      7 #include <gdk/gdk.h>
      8 #include <gdk/gdkx.h>
      9 
     10 #include "base/memory/singleton.h"
     11 #include "ui/base/x/active_window_watcher_x.h"
     12 #include "ui/base/x/work_area_watcher_x.h"
     13 
     14 namespace ui {
     15 
     16 namespace internal {
     17 
     18 // static
     19 RootWindowPropertyWatcherX* RootWindowPropertyWatcherX::GetInstance() {
     20   return Singleton<RootWindowPropertyWatcherX>::get();
     21 }
     22 
     23 RootWindowPropertyWatcherX::RootWindowPropertyWatcherX() {
     24   GdkWindow* root = gdk_get_default_root_window();
     25 
     26   // Set up X Event filter to listen for PropertyChange X events.
     27   // Don't use XSelectInput directly here, as gdk internally seems to cache the
     28   // mask and reapply XSelectInput after this, resetting any mask we set here.
     29   gdk_window_set_events(root,
     30                         static_cast<GdkEventMask>(gdk_window_get_events(root) |
     31                                                   GDK_PROPERTY_CHANGE_MASK));
     32   gdk_window_add_filter(root,
     33                         &RootWindowPropertyWatcherX::OnWindowXEventThunk,
     34                         this);
     35 }
     36 
     37 RootWindowPropertyWatcherX::~RootWindowPropertyWatcherX() {
     38   gdk_window_remove_filter(NULL,
     39                            &RootWindowPropertyWatcherX::OnWindowXEventThunk,
     40                            this);
     41 }
     42 
     43 GdkFilterReturn RootWindowPropertyWatcherX::OnWindowXEvent(
     44     GdkXEvent* xevent, GdkEvent* event) {
     45   XEvent* xev = static_cast<XEvent*>(xevent);
     46 
     47   if (xev->xany.type == PropertyNotify) {
     48     if (xev->xproperty.atom == ActiveWindowWatcherX::GetPropertyAtom())
     49       ActiveWindowWatcherX::Notify();
     50     else if (xev->xproperty.atom == WorkAreaWatcherX::GetPropertyAtom())
     51       WorkAreaWatcherX::Notify();
     52   }
     53 
     54   return GDK_FILTER_CONTINUE;
     55 }
     56 
     57 }  // namespace internal
     58 
     59 }  // namespace ui
     60