| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ash/wm/power_button_controller.h" | 5 #include "ash/wm/power_button_controller.h" |
| 6 | 6 |
| 7 #include "ash/common/accelerators/accelerator_controller.h" | 7 #include "ash/common/accelerators/accelerator_controller.h" |
| 8 #include "ash/common/ash_switches.h" | 8 #include "ash/common/ash_switches.h" |
| 9 #include "ash/common/session/session_state_delegate.h" | 9 #include "ash/common/session/session_state_delegate.h" |
| 10 #include "ash/common/system/tray/system_tray.h" | 10 #include "ash/common/system/tray/system_tray.h" |
| 11 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h" | 11 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h" |
| 12 #include "ash/common/wm_shell.h" | 12 #include "ash/common/wm_shell.h" |
| 13 #include "ash/public/cpp/shell_window_ids.h" | 13 #include "ash/public/cpp/shell_window_ids.h" |
| 14 #include "ash/shell.h" | 14 #include "ash/shell.h" |
| 15 #include "ash/wm/lock_state_controller.h" | 15 #include "ash/wm/lock_state_controller.h" |
| 16 #include "ash/wm/session_state_animator.h" | 16 #include "ash/wm/session_state_animator.h" |
| 17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 18 #include "ui/aura/window_event_dispatcher.h" | 18 #include "ui/aura/window_event_dispatcher.h" |
| 19 #include "ui/display/types/display_snapshot.h" | 19 #include "ui/display/types/display_snapshot.h" |
| 20 #include "ui/events/event_handler.h" | |
| 21 #include "ui/wm/core/compound_event_filter.h" | 20 #include "ui/wm/core/compound_event_filter.h" |
| 22 | 21 |
| 23 #if defined(OS_CHROMEOS) | 22 #if defined(OS_CHROMEOS) |
| 24 #include "ash/common/system/chromeos/audio/tray_audio.h" | 23 #include "ash/common/system/chromeos/audio/tray_audio.h" |
| 24 #include "ash/system/chromeos/power/tablet_power_button_controller.h" |
| 25 #include "chromeos/audio/cras_audio_handler.h" | 25 #include "chromeos/audio/cras_audio_handler.h" |
| 26 #include "chromeos/dbus/dbus_thread_manager.h" | 26 #include "chromeos/dbus/dbus_thread_manager.h" |
| 27 #endif | 27 #endif |
| 28 | 28 |
| 29 namespace ash { | 29 namespace ash { |
| 30 | 30 |
| 31 PowerButtonController::PowerButtonController(LockStateController* controller) | 31 PowerButtonController::PowerButtonController(LockStateController* controller) |
| 32 : power_button_down_(false), | 32 : power_button_down_(false), |
| 33 lock_button_down_(false), | 33 lock_button_down_(false), |
| 34 volume_down_pressed_(false), | 34 volume_down_pressed_(false), |
| 35 #if defined(OS_CHROMEOS) | 35 #if defined(OS_CHROMEOS) |
| 36 volume_percent_before_screenshot_(0), | 36 volume_percent_before_screenshot_(0), |
| 37 #endif | 37 #endif |
| 38 brightness_is_zero_(false), | 38 brightness_is_zero_(false), |
| 39 internal_display_off_and_external_display_on_(false), | 39 internal_display_off_and_external_display_on_(false), |
| 40 has_legacy_power_button_( | 40 has_legacy_power_button_( |
| 41 base::CommandLine::ForCurrentProcess()->HasSwitch( | 41 base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 42 switches::kAuraLegacyPowerButton)), | 42 switches::kAuraLegacyPowerButton)), |
| 43 #if defined(OS_CHROMEOS) | 43 lock_state_controller_(controller) { |
| 44 enable_quick_lock_(base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 45 switches::kAshEnableTouchView)), | |
| 46 #else | |
| 47 enable_quick_lock_(false), | |
| 48 #endif | |
| 49 controller_(controller) { | |
| 50 #if defined(OS_CHROMEOS) | 44 #if defined(OS_CHROMEOS) |
| 51 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( | 45 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( |
| 52 this); | 46 this); |
| 47 tablet_controller_.reset( |
| 48 new TabletPowerButtonController(lock_state_controller_)); |
| 53 Shell::GetInstance()->display_configurator()->AddObserver(this); | 49 Shell::GetInstance()->display_configurator()->AddObserver(this); |
| 54 #endif | 50 #endif |
| 55 Shell::GetInstance()->PrependPreTargetHandler(this); | 51 Shell::GetInstance()->PrependPreTargetHandler(this); |
| 56 } | 52 } |
| 57 | 53 |
| 58 PowerButtonController::~PowerButtonController() { | 54 PowerButtonController::~PowerButtonController() { |
| 59 Shell::GetInstance()->RemovePreTargetHandler(this); | 55 Shell::GetInstance()->RemovePreTargetHandler(this); |
| 60 #if defined(OS_CHROMEOS) | 56 #if defined(OS_CHROMEOS) |
| 61 Shell::GetInstance()->display_configurator()->RemoveObserver(this); | 57 Shell::GetInstance()->display_configurator()->RemoveObserver(this); |
| 58 tablet_controller_.reset(); |
| 62 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( | 59 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( |
| 63 this); | 60 this); |
| 64 #endif | 61 #endif |
| 65 } | 62 } |
| 66 | 63 |
| 67 void PowerButtonController::OnScreenBrightnessChanged(double percent) { | 64 void PowerButtonController::OnScreenBrightnessChanged(double percent) { |
| 68 brightness_is_zero_ = percent <= 0.001; | 65 brightness_is_zero_ = percent <= 0.001; |
| 69 } | 66 } |
| 70 | 67 |
| 71 void PowerButtonController::OnPowerButtonEvent( | 68 void PowerButtonController::OnPowerButtonEvent( |
| 72 bool down, | 69 bool down, |
| 73 const base::TimeTicks& timestamp) { | 70 const base::TimeTicks& timestamp) { |
| 74 power_button_down_ = down; | 71 power_button_down_ = down; |
| 75 | 72 |
| 76 if (controller_->ShutdownRequested()) | 73 if (lock_state_controller_->ShutdownRequested()) |
| 77 return; | 74 return; |
| 78 | 75 |
| 76 #if defined(OS_CHROMEOS) |
| 77 bool should_take_screenshot = down && volume_down_pressed_ && |
| 78 WmShell::Get() |
| 79 ->maximize_mode_controller() |
| 80 ->IsMaximizeModeWindowManagerEnabled(); |
| 81 |
| 82 if (!has_legacy_power_button_ && !should_take_screenshot && |
| 83 tablet_controller_->ShouldHandlePowerButtonEvents()) { |
| 84 tablet_controller_->OnPowerButtonEvent(down, timestamp); |
| 85 return; |
| 86 } |
| 87 #endif |
| 88 |
| 79 // Avoid starting the lock/shutdown sequence if the power button is pressed | 89 // Avoid starting the lock/shutdown sequence if the power button is pressed |
| 80 // while the screen is off (http://crbug.com/128451), unless an external | 90 // while the screen is off (http://crbug.com/128451), unless an external |
| 81 // display is still on (http://crosbug.com/p/24912). | 91 // display is still on (http://crosbug.com/p/24912). |
| 82 if (brightness_is_zero_ && !internal_display_off_and_external_display_on_) | 92 if (brightness_is_zero_ && !internal_display_off_and_external_display_on_) |
| 83 return; | 93 return; |
| 84 | 94 |
| 85 #if defined(OS_CHROMEOS) | 95 #if defined(OS_CHROMEOS) |
| 86 // Take screenshot on power button down plus volume down when in touch view. | 96 // Take screenshot on power button down plus volume down when in touch view. |
| 87 if (volume_down_pressed_ && down && | 97 if (should_take_screenshot) { |
| 88 WmShell::Get() | |
| 89 ->maximize_mode_controller() | |
| 90 ->IsMaximizeModeWindowManagerEnabled()) { | |
| 91 SystemTray* system_tray = Shell::GetInstance()->GetPrimarySystemTray(); | 98 SystemTray* system_tray = Shell::GetInstance()->GetPrimarySystemTray(); |
| 92 if (system_tray && system_tray->GetTrayAudio()) | 99 if (system_tray && system_tray->GetTrayAudio()) |
| 93 system_tray->GetTrayAudio()->HideDetailedView(false); | 100 system_tray->GetTrayAudio()->HideDetailedView(false); |
| 94 | 101 |
| 95 WmShell::Get()->accelerator_controller()->PerformActionIfEnabled( | 102 WmShell::Get()->accelerator_controller()->PerformActionIfEnabled( |
| 96 TAKE_SCREENSHOT); | 103 TAKE_SCREENSHOT); |
| 97 | 104 |
| 98 // Restore volume. | 105 // Restore volume. |
| 99 chromeos::CrasAudioHandler* audio_handler = | 106 chromeos::CrasAudioHandler* audio_handler = |
| 100 chromeos::CrasAudioHandler::Get(); | 107 chromeos::CrasAudioHandler::Get(); |
| 101 audio_handler->SetOutputVolumePercentWithoutNotifyingObservers( | 108 audio_handler->SetOutputVolumePercentWithoutNotifyingObservers( |
| 102 volume_percent_before_screenshot_, | 109 volume_percent_before_screenshot_, |
| 103 chromeos::CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT); | 110 chromeos::CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT); |
| 104 return; | 111 return; |
| 105 } | 112 } |
| 106 #endif // defined(OS_CHROMEOS) | 113 #endif // defined(OS_CHROMEOS) |
| 107 | 114 |
| 108 const SessionStateDelegate* session_state_delegate = | 115 const SessionStateDelegate* session_state_delegate = |
| 109 WmShell::Get()->GetSessionStateDelegate(); | 116 WmShell::Get()->GetSessionStateDelegate(); |
| 110 if (has_legacy_power_button_) { | 117 if (has_legacy_power_button_) { |
| 111 // If power button releases won't get reported correctly because we're not | 118 // If power button releases won't get reported correctly because we're not |
| 112 // running on official hardware, just lock the screen or shut down | 119 // running on official hardware, just lock the screen or shut down |
| 113 // immediately. | 120 // immediately. |
| 114 if (down) { | 121 if (down) { |
| 115 if (session_state_delegate->CanLockScreen() && | 122 if (session_state_delegate->CanLockScreen() && |
| 116 !session_state_delegate->IsUserSessionBlocked() && | 123 !session_state_delegate->IsUserSessionBlocked() && |
| 117 !controller_->LockRequested()) { | 124 !lock_state_controller_->LockRequested()) { |
| 118 controller_->StartLockAnimationAndLockImmediately(false); | 125 lock_state_controller_->StartLockAnimationAndLockImmediately(false); |
| 119 } else { | 126 } else { |
| 120 controller_->RequestShutdown(); | 127 lock_state_controller_->RequestShutdown(); |
| 121 } | 128 } |
| 122 } | 129 } |
| 123 } else { // !has_legacy_power_button_ | 130 } else { // !has_legacy_power_button_ |
| 124 if (down) { | 131 if (down) { |
| 125 // If we already have a pending request to lock the screen, wait. | 132 // If we already have a pending request to lock the screen, wait. |
| 126 if (controller_->LockRequested()) | 133 if (lock_state_controller_->LockRequested()) |
| 127 return; | 134 return; |
| 128 | 135 |
| 129 if (session_state_delegate->CanLockScreen() && | 136 if (session_state_delegate->CanLockScreen() && |
| 130 !session_state_delegate->IsUserSessionBlocked()) { | 137 !session_state_delegate->IsUserSessionBlocked()) { |
| 131 if (WmShell::Get() | 138 lock_state_controller_->StartLockAnimation(true); |
| 132 ->maximize_mode_controller() | |
| 133 ->IsMaximizeModeWindowManagerEnabled() && | |
| 134 enable_quick_lock_) | |
| 135 controller_->StartLockAnimationAndLockImmediately(true); | |
| 136 else | |
| 137 controller_->StartLockAnimation(true); | |
| 138 } else { | 139 } else { |
| 139 controller_->StartShutdownAnimation(); | 140 lock_state_controller_->StartShutdownAnimation(); |
| 140 } | 141 } |
| 141 } else { // Button is up. | 142 } else { // Button is up. |
| 142 if (controller_->CanCancelLockAnimation()) | 143 if (lock_state_controller_->CanCancelLockAnimation()) |
| 143 controller_->CancelLockAnimation(); | 144 lock_state_controller_->CancelLockAnimation(); |
| 144 else if (controller_->CanCancelShutdownAnimation()) | 145 else if (lock_state_controller_->CanCancelShutdownAnimation()) |
| 145 controller_->CancelShutdownAnimation(); | 146 lock_state_controller_->CancelShutdownAnimation(); |
| 146 } | 147 } |
| 147 } | 148 } |
| 148 } | 149 } |
| 149 | 150 |
| 150 void PowerButtonController::OnLockButtonEvent( | 151 void PowerButtonController::OnLockButtonEvent( |
| 151 bool down, | 152 bool down, |
| 152 const base::TimeTicks& timestamp) { | 153 const base::TimeTicks& timestamp) { |
| 153 lock_button_down_ = down; | 154 lock_button_down_ = down; |
| 154 | 155 |
| 155 const SessionStateDelegate* session_state_delegate = | 156 const SessionStateDelegate* session_state_delegate = |
| 156 WmShell::Get()->GetSessionStateDelegate(); | 157 WmShell::Get()->GetSessionStateDelegate(); |
| 157 if (!session_state_delegate->CanLockScreen() || | 158 if (!session_state_delegate->CanLockScreen() || |
| 158 session_state_delegate->IsScreenLocked() || | 159 session_state_delegate->IsScreenLocked() || |
| 159 controller_->LockRequested() || controller_->ShutdownRequested()) { | 160 lock_state_controller_->LockRequested() || |
| 161 lock_state_controller_->ShutdownRequested()) { |
| 160 return; | 162 return; |
| 161 } | 163 } |
| 162 | 164 |
| 163 // Give the power button precedence over the lock button. | 165 // Give the power button precedence over the lock button. |
| 164 if (power_button_down_) | 166 if (power_button_down_) |
| 165 return; | 167 return; |
| 166 | 168 |
| 167 if (down) | 169 if (down) |
| 168 controller_->StartLockAnimation(false); | 170 lock_state_controller_->StartLockAnimation(false); |
| 169 else | 171 else |
| 170 controller_->CancelLockAnimation(); | 172 lock_state_controller_->CancelLockAnimation(); |
| 171 } | 173 } |
| 172 | 174 |
| 173 void PowerButtonController::OnKeyEvent(ui::KeyEvent* event) { | 175 void PowerButtonController::OnKeyEvent(ui::KeyEvent* event) { |
| 174 if (event->key_code() == ui::VKEY_VOLUME_DOWN) { | 176 if (event->key_code() == ui::VKEY_VOLUME_DOWN) { |
| 175 volume_down_pressed_ = event->type() == ui::ET_KEY_PRESSED; | 177 volume_down_pressed_ = event->type() == ui::ET_KEY_PRESSED; |
| 176 #if defined(OS_CHROMEOS) | 178 #if defined(OS_CHROMEOS) |
| 177 if (!event->is_repeat()) { | 179 if (!event->is_repeat()) { |
| 178 chromeos::CrasAudioHandler* audio_handler = | 180 chromeos::CrasAudioHandler* audio_handler = |
| 179 chromeos::CrasAudioHandler::Get(); | 181 chromeos::CrasAudioHandler::Get(); |
| 180 volume_percent_before_screenshot_ = | 182 volume_percent_before_screenshot_ = |
| (...skipping 21 matching lines...) Expand all Loading... |
| 202 } | 204 } |
| 203 | 205 |
| 204 void PowerButtonController::PowerButtonEventReceived( | 206 void PowerButtonController::PowerButtonEventReceived( |
| 205 bool down, | 207 bool down, |
| 206 const base::TimeTicks& timestamp) { | 208 const base::TimeTicks& timestamp) { |
| 207 OnPowerButtonEvent(down, timestamp); | 209 OnPowerButtonEvent(down, timestamp); |
| 208 } | 210 } |
| 209 #endif // defined(OS_CHROMEOS) | 211 #endif // defined(OS_CHROMEOS) |
| 210 | 212 |
| 211 } // namespace ash | 213 } // namespace ash |
| OLD | NEW |