Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / athena / input / power_button_controller.cc
blob8dfca82b97ea93a1618fe3127c8be23819e47868
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"
11 namespace athena {
12 namespace {
14 // The amount of time that the power button must be held to be
15 // treated as long press.
16 const int kLongPressTimeoutMs = 1000;
18 enum {
19 CMD_DEBUG_POWER_BUTTON_PRESSED,
20 CMD_DEBUG_POWER_BUTTON_RELEASED,
23 } // namespace
25 PowerButtonController::PowerButtonController()
26 : power_button_timeout_ms_(kLongPressTimeoutMs),
27 brightness_is_zero_(false) {
28 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
29 this);
32 PowerButtonController::~PowerButtonController() {
33 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(
34 this);
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[] = {
49 {TRIGGER_ON_PRESS,
50 ui::VKEY_P,
51 ui::EF_ALT_DOWN,
52 CMD_DEBUG_POWER_BUTTON_PRESSED,
53 AF_DEBUG | AF_NON_AUTO_REPEATABLE},
54 {TRIGGER_ON_RELEASE,
55 ui::VKEY_P,
56 ui::EF_ALT_DOWN,
57 CMD_DEBUG_POWER_BUTTON_RELEASED,
58 AF_DEBUG},
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;
67 return old_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(
77 bool down,
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 =
83 brightness_is_zero_
84 ? base::TimeDelta()
85 : (base::TimeTicks::Now() - zero_brightness_end_time_);
86 const int kShortTimeMs = 10;
87 if (time_since_zero_brightness.InMilliseconds() <= kShortTimeMs)
88 return;
90 if (down) {
91 FOR_EACH_OBSERVER(PowerButtonObserver,
92 observers_,
93 OnPowerButtonStateChanged(PowerButtonObserver::PRESSED));
94 timer_.Start(FROM_HERE,
95 base::TimeDelta::FromMilliseconds(kLongPressTimeoutMs),
96 this,
97 &PowerButtonController::NotifyLongPress);
98 } else {
99 FOR_EACH_OBSERVER(PowerButtonObserver,
100 observers_,
101 OnPowerButtonStateChanged(PowerButtonObserver::RELEASED));
102 timer_.Stop();
106 bool PowerButtonController::IsCommandEnabled(int command_id) const {
107 return true;
110 bool PowerButtonController::OnAcceleratorFired(
111 int command_id,
112 const ui::Accelerator& accelerator) {
113 switch (command_id) {
114 case CMD_DEBUG_POWER_BUTTON_PRESSED:
115 PowerButtonEventReceived(true, base::TimeTicks());
116 break;
117 case CMD_DEBUG_POWER_BUTTON_RELEASED:
118 PowerButtonEventReceived(false, base::TimeTicks());
119 break;
121 return true;
124 void PowerButtonController::NotifyLongPress() {
125 FOR_EACH_OBSERVER(
126 PowerButtonObserver,
127 observers_,
128 OnPowerButtonStateChanged(PowerButtonObserver::LONG_PRESSED));
131 } // namespace