Home | History | Annotate | Download | only in aura
      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/aura/env.h"
      6 
      7 #include "base/command_line.h"
      8 #include "ui/aura/env_observer.h"
      9 #include "ui/aura/input_state_lookup.h"
     10 #include "ui/aura/window.h"
     11 #include "ui/compositor/compositor.h"
     12 #include "ui/compositor/compositor_switches.h"
     13 #include "ui/events/event_target_iterator.h"
     14 
     15 #if defined(USE_X11)
     16 #include "base/message_loop/message_pump_x11.h"
     17 #endif
     18 
     19 namespace aura {
     20 
     21 // static
     22 Env* Env::instance_ = NULL;
     23 
     24 ////////////////////////////////////////////////////////////////////////////////
     25 // Env, public:
     26 
     27 Env::Env()
     28     : mouse_button_flags_(0),
     29       is_touch_down_(false),
     30       input_state_lookup_(InputStateLookup::Create().Pass()) {
     31 }
     32 
     33 Env::~Env() {
     34 #if defined(USE_X11)
     35   base::MessagePumpX11::Current()->RemoveObserver(
     36       &device_list_updater_aurax11_);
     37 #endif
     38 
     39   FOR_EACH_OBSERVER(EnvObserver, observers_, OnWillDestroyEnv());
     40 
     41   ui::Compositor::Terminate();
     42 }
     43 
     44 //static
     45 void Env::CreateInstance() {
     46   if (!instance_) {
     47     instance_ = new Env;
     48     instance_->Init();
     49   }
     50 }
     51 
     52 // static
     53 Env* Env::GetInstance() {
     54   DCHECK(instance_) << "Env::CreateInstance must be called before getting "
     55                        "the instance of Env.";
     56   return instance_;
     57 }
     58 
     59 // static
     60 void Env::DeleteInstance() {
     61   delete instance_;
     62   instance_ = NULL;
     63 }
     64 
     65 void Env::AddObserver(EnvObserver* observer) {
     66   observers_.AddObserver(observer);
     67 }
     68 
     69 void Env::RemoveObserver(EnvObserver* observer) {
     70   observers_.RemoveObserver(observer);
     71 }
     72 
     73 bool Env::IsMouseButtonDown() const {
     74   return input_state_lookup_.get() ? input_state_lookup_->IsMouseButtonDown() :
     75       mouse_button_flags_ != 0;
     76 }
     77 
     78 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) && \
     79     !defined(USE_GTK_MESSAGE_PUMP)
     80 base::MessageLoop::Dispatcher* Env::GetDispatcher() {
     81 #if defined(USE_X11)
     82   return base::MessagePumpX11::Current();
     83 #else
     84   return dispatcher_.get();
     85 #endif
     86 }
     87 #endif
     88 
     89 void Env::RootWindowActivated(RootWindow* root_window) {
     90   FOR_EACH_OBSERVER(EnvObserver, observers_,
     91                     OnRootWindowActivated(root_window));
     92 }
     93 
     94 ////////////////////////////////////////////////////////////////////////////////
     95 // Env, private:
     96 
     97 void Env::Init() {
     98 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) && !defined(USE_X11) && \
     99     !defined(USE_OZONE)
    100   dispatcher_.reset(CreateDispatcher());
    101 #endif
    102 #if defined(USE_X11)
    103   // We can't do this with a root window listener because XI_HierarchyChanged
    104   // messages don't have a target window.
    105   base::MessagePumpX11::Current()->AddObserver(
    106       &device_list_updater_aurax11_);
    107 #endif
    108   ui::Compositor::Initialize();
    109 }
    110 
    111 void Env::NotifyWindowInitialized(Window* window) {
    112   FOR_EACH_OBSERVER(EnvObserver, observers_, OnWindowInitialized(window));
    113 }
    114 
    115 void Env::NotifyRootWindowInitialized(RootWindow* root_window) {
    116   FOR_EACH_OBSERVER(EnvObserver,
    117                     observers_,
    118                     OnRootWindowInitialized(root_window));
    119 }
    120 
    121 ////////////////////////////////////////////////////////////////////////////////
    122 // Env, ui::EventTarget implementation:
    123 
    124 bool Env::CanAcceptEvent(const ui::Event& event) {
    125   return true;
    126 }
    127 
    128 ui::EventTarget* Env::GetParentTarget() {
    129   return NULL;
    130 }
    131 
    132 scoped_ptr<ui::EventTargetIterator> Env::GetChildIterator() const {
    133   return scoped_ptr<ui::EventTargetIterator>();
    134 }
    135 
    136 ui::EventTargeter* Env::GetEventTargeter() {
    137   NOTREACHED();
    138   return NULL;
    139 }
    140 
    141 }  // namespace aura
    142