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