Revert of Removing Chrome Gamepad transition cruft (https://codereview.chromium.org...
[chromium-blink-merge.git] / ui / aura / env.cc
blob21b4213d33f2b57c48f5c85bf906b94f5bb12243
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.
5 #include "ui/aura/env.h"
7 #include "base/command_line.h"
8 #include "base/message_loop/message_pump_dispatcher.h"
9 #include "ui/aura/env_observer.h"
10 #include "ui/aura/input_state_lookup.h"
11 #include "ui/aura/window.h"
12 #include "ui/compositor/compositor.h"
13 #include "ui/compositor/compositor_switches.h"
14 #include "ui/events/event_target_iterator.h"
16 #if defined(USE_X11)
17 #include "base/message_loop/message_pump_x11.h"
18 #endif
20 namespace aura {
22 // static
23 Env* Env::instance_ = NULL;
25 ////////////////////////////////////////////////////////////////////////////////
26 // Env, public:
28 Env::Env()
29 : mouse_button_flags_(0),
30 is_touch_down_(false),
31 input_state_lookup_(InputStateLookup::Create().Pass()) {
34 Env::~Env() {
35 #if defined(USE_X11)
36 base::MessagePumpX11::Current()->RemoveObserver(
37 &device_list_updater_aurax11_);
38 #endif
40 FOR_EACH_OBSERVER(EnvObserver, observers_, OnWillDestroyEnv());
42 ui::Compositor::Terminate();
45 //static
46 void Env::CreateInstance() {
47 if (!instance_) {
48 instance_ = new Env;
49 instance_->Init();
53 // static
54 Env* Env::GetInstance() {
55 DCHECK(instance_) << "Env::CreateInstance must be called before getting "
56 "the instance of Env.";
57 return instance_;
60 // static
61 void Env::DeleteInstance() {
62 delete instance_;
63 instance_ = NULL;
66 void Env::AddObserver(EnvObserver* observer) {
67 observers_.AddObserver(observer);
70 void Env::RemoveObserver(EnvObserver* observer) {
71 observers_.RemoveObserver(observer);
74 bool Env::IsMouseButtonDown() const {
75 return input_state_lookup_.get() ? input_state_lookup_->IsMouseButtonDown() :
76 mouse_button_flags_ != 0;
79 ////////////////////////////////////////////////////////////////////////////////
80 // Env, private:
82 void Env::Init() {
83 #if defined(USE_X11)
84 // We can't do this with a root window listener because XI_HierarchyChanged
85 // messages don't have a target window.
86 base::MessagePumpX11::Current()->AddObserver(
87 &device_list_updater_aurax11_);
88 #endif
89 ui::Compositor::Initialize();
92 void Env::NotifyWindowInitialized(Window* window) {
93 FOR_EACH_OBSERVER(EnvObserver, observers_, OnWindowInitialized(window));
96 void Env::NotifyHostInitialized(WindowTreeHost* host) {
97 FOR_EACH_OBSERVER(EnvObserver, observers_, OnHostInitialized(host));
100 void Env::NotifyHostActivated(WindowTreeHost* host) {
101 FOR_EACH_OBSERVER(EnvObserver, observers_, OnHostActivated(host));
104 ////////////////////////////////////////////////////////////////////////////////
105 // Env, ui::EventTarget implementation:
107 bool Env::CanAcceptEvent(const ui::Event& event) {
108 return true;
111 ui::EventTarget* Env::GetParentTarget() {
112 return NULL;
115 scoped_ptr<ui::EventTargetIterator> Env::GetChildIterator() const {
116 return scoped_ptr<ui::EventTargetIterator>();
119 ui::EventTargeter* Env::GetEventTargeter() {
120 NOTREACHED();
121 return NULL;
124 } // namespace aura