OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "athena/input/power_button_controller.h" |
| 6 |
| 7 #include "athena/input/public/accelerator_manager.h" |
| 8 #include "chromeos/dbus/dbus_thread_manager.h" |
| 9 #include "ui/events/event_constants.h" |
| 10 |
| 11 namespace athena { |
| 12 namespace { |
| 13 |
| 14 // The amount of time that the power button must be held to be |
| 15 // treated as long press. |
| 16 const int kLongPressTimeoutMs = 1000; |
| 17 |
| 18 enum { |
| 19 CMD_DEBUG_POWER_BUTTON_PRESSED, |
| 20 CMD_DEBUG_POWER_BUTTON_RELEASED, |
| 21 }; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 PowerButtonController::PowerButtonController() |
| 26 : power_button_timeout_ms_(kLongPressTimeoutMs), |
| 27 brightness_is_zero_(false) { |
| 28 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( |
| 29 this); |
| 30 } |
| 31 |
| 32 PowerButtonController::~PowerButtonController() { |
| 33 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( |
| 34 this); |
| 35 } |
| 36 |
| 37 void PowerButtonController::AddPowerButtonObserver( |
| 38 PowerButtonObserver* observer) { |
| 39 observers_.AddObserver(observer); |
| 40 } |
| 41 |
| 42 void PowerButtonController::RemovePowerButtonObserver( |
| 43 PowerButtonObserver* observer) { |
| 44 observers_.RemoveObserver(observer); |
| 45 } |
| 46 |
| 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}, |
| 59 }; |
| 60 AcceleratorManager::Get()->RegisterAccelerators( |
| 61 accelerator_data, arraysize(accelerator_data), this); |
| 62 } |
| 63 |
| 64 int PowerButtonController::SetPowerButtonTimeoutMsForTest(int timeout) { |
| 65 int old_timeout = power_button_timeout_ms_; |
| 66 power_button_timeout_ms_ = timeout; |
| 67 return old_timeout; |
| 68 } |
| 69 |
| 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); |
| 74 } |
| 75 |
| 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; |
| 89 |
| 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(); |
| 103 } |
| 104 } |
| 105 |
| 106 bool PowerButtonController::IsCommandEnabled(int command_id) const { |
| 107 return true; |
| 108 } |
| 109 |
| 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; |
| 120 } |
| 121 return true; |
| 122 } |
| 123 |
| 124 void PowerButtonController::NotifyLongPress() { |
| 125 FOR_EACH_OBSERVER( |
| 126 PowerButtonObserver, |
| 127 observers_, |
| 128 OnPowerButtonStateChanged(PowerButtonObserver::LONG_PRESSED)); |
| 129 } |
| 130 |
| 131 } // namespace |
OLD | NEW |