OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "athena/input/public/input_manager.h" | 5 #include "athena/input/input_manager_impl.h" |
6 | 6 |
7 #include "athena/input/accelerator_manager_impl.h" | |
8 #include "base/logging.h" | 7 #include "base/logging.h" |
9 #include "ui/aura/client/event_client.h" | 8 #include "chromeos/dbus/dbus_thread_manager.h" |
10 #include "ui/aura/env.h" | 9 #include "ui/aura/env.h" |
11 #include "ui/aura/window.h" | 10 #include "ui/aura/window.h" |
12 #include "ui/events/event_target.h" | |
13 | 11 |
14 namespace athena { | 12 namespace athena { |
15 namespace { | 13 namespace { |
16 | 14 |
15 // The amount of time that the power button must be held to be | |
16 // treated as long press. | |
17 const int kLongPressTimeoutMs = 1000; | |
18 | |
17 InputManager* instance = NULL; | 19 InputManager* instance = NULL; |
18 | 20 |
19 class InputManagerImpl : public InputManager, | 21 enum { |
20 public ui::EventTarget, | 22 CMD_DEBUG_POWER_BUTTON_PRESSED, |
21 public aura::client::EventClient { | 23 CMD_DEBUG_POWER_BUTTON_RELEASED, |
22 public: | 24 }; |
23 InputManagerImpl(); | |
24 virtual ~InputManagerImpl(); | |
25 | 25 |
26 void Init(); | 26 } // namespace |
27 void Shutdown(); | |
28 | |
29 private: | |
30 // InputManager: | |
31 virtual void OnRootWindowCreated(aura::Window* root_window) override; | |
32 virtual ui::EventTarget* GetTopmostEventTarget() override { return this; } | |
33 virtual AcceleratorManager* GetAcceleratorManager() override { | |
34 return accelerator_manager_.get(); | |
35 } | |
36 | |
37 // Overridden from aura::client::EventClient: | |
38 virtual bool CanProcessEventsWithinSubtree( | |
39 const aura::Window* window) const override { | |
40 return window && !window->ignore_events(); | |
41 } | |
42 virtual ui::EventTarget* GetToplevelEventTarget() override { return this; } | |
43 | |
44 // ui::EventTarget: | |
45 virtual bool CanAcceptEvent(const ui::Event& event) override; | |
46 virtual ui::EventTarget* GetParentTarget() override; | |
47 virtual scoped_ptr<ui::EventTargetIterator> GetChildIterator() const override; | |
48 virtual ui::EventTargeter* GetEventTargeter() override; | |
49 virtual void OnEvent(ui::Event* event) override; | |
50 | |
51 scoped_ptr<AcceleratorManagerImpl> accelerator_manager_; | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(InputManagerImpl); | |
54 }; | |
55 | 27 |
56 InputManagerImpl::InputManagerImpl() | 28 InputManagerImpl::InputManagerImpl() |
57 : accelerator_manager_( | 29 : accelerator_manager_( |
58 AcceleratorManagerImpl::CreateGlobalAcceleratorManager()) { | 30 AcceleratorManagerImpl::CreateGlobalAcceleratorManager()), |
31 power_button_timeout_ms_(kLongPressTimeoutMs), | |
32 brightness_is_zero_(false) { | |
59 DCHECK(!instance); | 33 DCHECK(!instance); |
60 instance = this; | 34 instance = this; |
35 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( | |
36 this); | |
61 } | 37 } |
62 | 38 |
63 InputManagerImpl::~InputManagerImpl() { | 39 InputManagerImpl::~InputManagerImpl() { |
40 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( | |
41 this); | |
64 DCHECK_EQ(instance, this); | 42 DCHECK_EQ(instance, this); |
65 Shutdown(); | 43 Shutdown(); |
66 instance = NULL; | 44 instance = NULL; |
67 } | 45 } |
68 | 46 |
69 void InputManagerImpl::Init() { | 47 void InputManagerImpl::Init() { |
70 accelerator_manager_->Init(); | 48 accelerator_manager_->Init(); |
49 InstallAccelerators(); | |
71 } | 50 } |
72 | 51 |
73 void InputManagerImpl::Shutdown() { | 52 void InputManagerImpl::Shutdown() { |
74 accelerator_manager_.reset(); | 53 accelerator_manager_.reset(); |
75 } | 54 } |
76 | 55 |
77 void InputManagerImpl::OnRootWindowCreated(aura::Window* root_window) { | 56 void InputManagerImpl::OnRootWindowCreated(aura::Window* root_window) { |
78 aura::client::SetEventClient(root_window, this); | 57 aura::client::SetEventClient(root_window, this); |
79 accelerator_manager_->OnRootWindowCreated(root_window); | 58 accelerator_manager_->OnRootWindowCreated(root_window); |
80 } | 59 } |
81 | 60 |
61 ui::EventTarget* InputManagerImpl::GetTopmostEventTarget() { | |
62 return this; | |
63 } | |
64 | |
65 AcceleratorManager* InputManagerImpl::GetAcceleratorManager() { | |
66 return accelerator_manager_.get(); | |
67 } | |
68 | |
69 void InputManagerImpl::AddPowerButtonObserver(PowerButtonObserver* observer) { | |
70 power_button_observers_.AddObserver(observer); | |
71 } | |
72 void InputManagerImpl::RemovePowerButtonObserver( | |
73 PowerButtonObserver* observer) { | |
74 power_button_observers_.RemoveObserver(observer); | |
75 } | |
76 | |
77 bool InputManagerImpl::CanProcessEventsWithinSubtree( | |
78 const aura::Window* window) const { | |
79 return window && !window->ignore_events(); | |
80 } | |
81 | |
82 ui::EventTarget* InputManagerImpl::GetToplevelEventTarget() { | |
83 return this; | |
84 } | |
85 | |
82 bool InputManagerImpl::CanAcceptEvent(const ui::Event& event) { | 86 bool InputManagerImpl::CanAcceptEvent(const ui::Event& event) { |
83 return true; | 87 return true; |
84 } | 88 } |
85 | 89 |
86 ui::EventTarget* InputManagerImpl::GetParentTarget() { | 90 ui::EventTarget* InputManagerImpl::GetParentTarget() { |
87 return aura::Env::GetInstance(); | 91 return aura::Env::GetInstance(); |
88 } | 92 } |
89 | 93 |
90 scoped_ptr<ui::EventTargetIterator> InputManagerImpl::GetChildIterator() const { | 94 scoped_ptr<ui::EventTargetIterator> InputManagerImpl::GetChildIterator() const { |
91 return scoped_ptr<ui::EventTargetIterator>(); | 95 return scoped_ptr<ui::EventTargetIterator>(); |
92 } | 96 } |
93 | 97 |
94 ui::EventTargeter* InputManagerImpl::GetEventTargeter() { | 98 ui::EventTargeter* InputManagerImpl::GetEventTargeter() { |
95 NOTREACHED(); | 99 NOTREACHED(); |
96 return NULL; | 100 return NULL; |
97 } | 101 } |
98 | 102 |
99 void InputManagerImpl::OnEvent(ui::Event* event) { | 103 void InputManagerImpl::OnEvent(ui::Event* event) { |
100 } | 104 } |
101 | 105 |
102 } // namespace | 106 void InputManagerImpl::BrightnessChanged(int level, bool user_initiated) { |
107 if (brightness_is_zero_) | |
108 zero_brightness_end_time_ = base::TimeTicks::Now(); | |
109 brightness_is_zero_ = (level == 0); | |
Jun Mukai
2014/10/15 23:38:45
I personally feel that these power-button related
| |
110 } | |
111 | |
112 void InputManagerImpl::PowerButtonEventReceived( | |
113 bool down, | |
114 const base::TimeTicks& timestamp) { | |
115 // Ignore power button pressed while the screen is off (http://crbug.com/12845 1). | |
Jun Mukai
2014/10/15 23:38:45
exceeds 80 chars?
| |
116 // TODO(oshima): This needs to be revisited for athena. | |
117 base::TimeDelta time_since_zero_brightness = | |
118 brightness_is_zero_ | |
119 ? base::TimeDelta() | |
120 : (base::TimeTicks::Now() - zero_brightness_end_time_); | |
121 const int kShortTimeMs = 10; | |
122 if (time_since_zero_brightness.InMilliseconds() <= kShortTimeMs) | |
123 return; | |
124 | |
125 if (down) { | |
126 FOR_EACH_OBSERVER(PowerButtonObserver, | |
127 power_button_observers_, | |
128 OnPowerButtonStateChanged(PowerButtonObserver::PRESSED)); | |
129 timer_.Start(FROM_HERE, | |
130 base::TimeDelta::FromMilliseconds(kLongPressTimeoutMs), | |
131 this, | |
132 &InputManagerImpl::NotifyLongPress); | |
133 } else { | |
134 FOR_EACH_OBSERVER(PowerButtonObserver, | |
135 power_button_observers_, | |
136 OnPowerButtonStateChanged(PowerButtonObserver::RELEASED)); | |
137 timer_.Stop(); | |
138 } | |
139 } | |
140 | |
141 bool InputManagerImpl::IsCommandEnabled(int command_id) const { | |
142 return true; | |
143 } | |
144 | |
145 bool InputManagerImpl::OnAcceleratorFired(int command_id, | |
146 const ui::Accelerator& accelerator) { | |
147 switch (command_id) { | |
148 case CMD_DEBUG_POWER_BUTTON_PRESSED: | |
149 PowerButtonEventReceived(true, base::TimeTicks()); | |
150 break; | |
151 case CMD_DEBUG_POWER_BUTTON_RELEASED: | |
152 PowerButtonEventReceived(false, base::TimeTicks()); | |
153 break; | |
154 } | |
155 return true; | |
156 } | |
157 | |
158 void InputManagerImpl::NotifyLongPress() { | |
159 FOR_EACH_OBSERVER( | |
160 PowerButtonObserver, | |
161 power_button_observers_, | |
162 OnPowerButtonStateChanged(PowerButtonObserver::LONG_PRESSED)); | |
163 } | |
164 | |
165 void InputManagerImpl::InstallAccelerators() { | |
166 const AcceleratorData accelerator_data[] = { | |
167 {TRIGGER_ON_PRESS, | |
168 ui::VKEY_P, | |
169 ui::EF_ALT_DOWN, | |
170 CMD_DEBUG_POWER_BUTTON_PRESSED, | |
171 AF_DEBUG | AF_NON_AUTO_REPEATABLE}, | |
172 {TRIGGER_ON_RELEASE, | |
173 ui::VKEY_P, | |
174 ui::EF_ALT_DOWN, | |
175 CMD_DEBUG_POWER_BUTTON_RELEASED, | |
176 AF_DEBUG}, | |
177 }; | |
178 AcceleratorManager::Get()->RegisterAccelerators( | |
179 accelerator_data, arraysize(accelerator_data), this); | |
180 } | |
181 | |
182 int InputManagerImpl::SetPowerButtonTimeoutMsForTest(int timeout) { | |
183 int old_timeout = power_button_timeout_ms_; | |
184 power_button_timeout_ms_ = timeout; | |
185 return old_timeout; | |
186 } | |
103 | 187 |
104 // static | 188 // static |
105 InputManager* InputManager::Create() { | 189 InputManager* InputManager::Create() { |
106 (new InputManagerImpl)->Init(); | 190 (new InputManagerImpl)->Init(); |
107 DCHECK(instance); | 191 DCHECK(instance); |
108 return instance; | 192 return instance; |
109 } | 193 } |
110 | 194 |
111 // static | 195 // static |
112 InputManager* InputManager::Get() { | 196 InputManager* InputManager::Get() { |
113 DCHECK(instance); | 197 DCHECK(instance); |
114 return instance; | 198 return instance; |
115 } | 199 } |
116 | 200 |
117 // static | 201 // static |
118 void InputManager::Shutdown() { | 202 void InputManager::Shutdown() { |
119 DCHECK(instance); | 203 DCHECK(instance); |
120 delete instance; | 204 delete instance; |
121 DCHECK(!instance); | 205 DCHECK(!instance); |
122 } | 206 } |
123 | 207 |
124 } // namespace athena | 208 } // namespace athena |
OLD | NEW |