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
3 // found in the LICENSE file.
5 #include "athena/input/power_button_controller.h"
7 #include "athena/input/public/accelerator_manager.h"
8 #include "chromeos/dbus/dbus_thread_manager.h"
9 #include "ui/events/event_constants.h"
14 // The amount of time that the power button must be held to be
15 // treated as long press.
16 const int kLongPressTimeoutMs
= 1000;
19 CMD_DEBUG_POWER_BUTTON_PRESSED
,
20 CMD_DEBUG_POWER_BUTTON_RELEASED
,
25 PowerButtonController::PowerButtonController()
26 : power_button_timeout_ms_(kLongPressTimeoutMs
),
27 brightness_is_zero_(false) {
28 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
32 PowerButtonController::~PowerButtonController() {
33 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(
37 void PowerButtonController::AddPowerButtonObserver(
38 PowerButtonObserver
* observer
) {
39 observers_
.AddObserver(observer
);
42 void PowerButtonController::RemovePowerButtonObserver(
43 PowerButtonObserver
* observer
) {
44 observers_
.RemoveObserver(observer
);
47 void PowerButtonController::InstallAccelerators() {
48 const AcceleratorData accelerator_data
[] = {
52 CMD_DEBUG_POWER_BUTTON_PRESSED
,
53 AF_DEBUG
| AF_NON_AUTO_REPEATABLE
},
57 CMD_DEBUG_POWER_BUTTON_RELEASED
,
60 AcceleratorManager::Get()->RegisterAccelerators(
61 accelerator_data
, arraysize(accelerator_data
), this);
64 int PowerButtonController::SetPowerButtonTimeoutMsForTest(int timeout
) {
65 int old_timeout
= power_button_timeout_ms_
;
66 power_button_timeout_ms_
= timeout
;
70 void PowerButtonController::BrightnessChanged(int level
, bool user_initiated
) {
71 if (brightness_is_zero_
)
72 zero_brightness_end_time_
= base::TimeTicks::Now();
73 brightness_is_zero_
= (level
== 0);
76 void PowerButtonController::PowerButtonEventReceived(
78 const base::TimeTicks
& timestamp
) {
79 // Ignore power button pressed while the screen is off
80 // (http://crbug.com/128451).
81 // TODO(oshima): This needs to be revisited for athena.
82 base::TimeDelta time_since_zero_brightness
=
85 : (base::TimeTicks::Now() - zero_brightness_end_time_
);
86 const int kShortTimeMs
= 10;
87 if (time_since_zero_brightness
.InMilliseconds() <= kShortTimeMs
)
91 FOR_EACH_OBSERVER(PowerButtonObserver
,
93 OnPowerButtonStateChanged(PowerButtonObserver::PRESSED
));
94 timer_
.Start(FROM_HERE
,
95 base::TimeDelta::FromMilliseconds(kLongPressTimeoutMs
),
97 &PowerButtonController::NotifyLongPress
);
99 FOR_EACH_OBSERVER(PowerButtonObserver
,
101 OnPowerButtonStateChanged(PowerButtonObserver::RELEASED
));
106 bool PowerButtonController::IsCommandEnabled(int command_id
) const {
110 bool PowerButtonController::OnAcceleratorFired(
112 const ui::Accelerator
& accelerator
) {
113 switch (command_id
) {
114 case CMD_DEBUG_POWER_BUTTON_PRESSED
:
115 PowerButtonEventReceived(true, base::TimeTicks());
117 case CMD_DEBUG_POWER_BUTTON_RELEASED
:
118 PowerButtonEventReceived(false, base::TimeTicks());
124 void PowerButtonController::NotifyLongPress() {
128 OnPowerButtonStateChanged(PowerButtonObserver::LONG_PRESSED
));