| 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); |
| 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 bool should_take_screenshot = down && volume_down_pressed_ && |
| 77 WmShell::Get() |
| 78 ->maximize_mode_controller() |
| 79 ->IsMaximizeModeWindowManagerEnabled(); |
| 80 |
| 81 #if defined(OS_CHROMEOS) |
| 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 (volume_down_pressed_ && down && | 95 if (should_take_screenshot) { |
| 86 WmShell::Get() | |
| 87 ->maximize_mode_controller() | |
| 88 ->IsMaximizeModeWindowManagerEnabled()) { | |
| 89 SystemTray* system_tray = Shell::GetInstance()->GetPrimarySystemTray(); | 96 SystemTray* system_tray = Shell::GetInstance()->GetPrimarySystemTray(); |
| 90 if (system_tray && system_tray->GetTrayAudio()) | 97 if (system_tray && system_tray->GetTrayAudio()) |
| 91 system_tray->GetTrayAudio()->HideDetailedView(false); | 98 system_tray->GetTrayAudio()->HideDetailedView(false); |
| 92 | 99 |
| 93 WmShell::Get()->accelerator_controller()->PerformActionIfEnabled( | 100 WmShell::Get()->accelerator_controller()->PerformActionIfEnabled( |
| 94 TAKE_SCREENSHOT); | 101 TAKE_SCREENSHOT); |
| 95 | 102 |
| 96 #if defined(OS_CHROMEOS) | 103 #if defined(OS_CHROMEOS) |
| 97 // Restore volume. | 104 // Restore volume. |
| 98 chromeos::CrasAudioHandler* audio_handler = | 105 chromeos::CrasAudioHandler* audio_handler = |
| 99 chromeos::CrasAudioHandler::Get(); | 106 chromeos::CrasAudioHandler::Get(); |
| 100 audio_handler->SetOutputVolumePercentWithoutNotifyingObservers( | 107 audio_handler->SetOutputVolumePercentWithoutNotifyingObservers( |
| 101 volume_percent_before_screenshot_, | 108 volume_percent_before_screenshot_, |
| 102 chromeos::CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT); | 109 chromeos::CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT); |
| 103 #endif | 110 #endif |
| 104 return; | 111 return; |
| 105 } | 112 } |
| 106 | 113 |
| 107 const SessionStateDelegate* session_state_delegate = | 114 const SessionStateDelegate* session_state_delegate = |
| 108 WmShell::Get()->GetSessionStateDelegate(); | 115 WmShell::Get()->GetSessionStateDelegate(); |
| 109 if (has_legacy_power_button_) { | 116 if (has_legacy_power_button_) { |
| 110 // If power button releases won't get reported correctly because we're not | 117 // 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 | 118 // running on official hardware, just lock the screen or shut down |
| 112 // immediately. | 119 // immediately. |
| 113 if (down) { | 120 if (down) { |
| 114 if (session_state_delegate->CanLockScreen() && | 121 if (session_state_delegate->CanLockScreen() && |
| 115 !session_state_delegate->IsUserSessionBlocked() && | 122 !session_state_delegate->IsUserSessionBlocked() && |
| 116 !controller_->LockRequested()) { | 123 !lock_state_controller_->LockRequested()) { |
| 117 controller_->StartLockAnimationAndLockImmediately(false); | 124 lock_state_controller_->StartLockAnimationAndLockImmediately(false); |
| 118 } else { | 125 } else { |
| 119 controller_->RequestShutdown(); | 126 lock_state_controller_->RequestShutdown(); |
| 120 } | 127 } |
| 121 } | 128 } |
| 122 } else { // !has_legacy_power_button_ | 129 } else { // !has_legacy_power_button_ |
| 123 if (down) { | 130 if (down) { |
| 124 // If we already have a pending request to lock the screen, wait. | 131 // If we already have a pending request to lock the screen, wait. |
| 125 if (controller_->LockRequested()) | 132 if (lock_state_controller_->LockRequested()) |
| 126 return; | 133 return; |
| 127 | 134 |
| 128 if (session_state_delegate->CanLockScreen() && | 135 if (session_state_delegate->CanLockScreen() && |
| 129 !session_state_delegate->IsUserSessionBlocked()) { | 136 !session_state_delegate->IsUserSessionBlocked()) { |
| 130 if (WmShell::Get() | 137 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 { | 138 } else { |
| 138 controller_->StartShutdownAnimation(); | 139 lock_state_controller_->StartShutdownAnimation(); |
| 139 } | 140 } |
| 140 } else { // Button is up. | 141 } else { // Button is up. |
| 141 if (controller_->CanCancelLockAnimation()) | 142 if (lock_state_controller_->CanCancelLockAnimation()) |
| 142 controller_->CancelLockAnimation(); | 143 lock_state_controller_->CancelLockAnimation(); |
| 143 else if (controller_->CanCancelShutdownAnimation()) | 144 else if (lock_state_controller_->CanCancelShutdownAnimation()) |
| 144 controller_->CancelShutdownAnimation(); | 145 lock_state_controller_->CancelShutdownAnimation(); |
| 145 } | 146 } |
| 146 } | 147 } |
| 147 } | 148 } |
| 148 | 149 |
| 149 void PowerButtonController::OnLockButtonEvent( | 150 void PowerButtonController::OnLockButtonEvent( |
| 150 bool down, | 151 bool down, |
| 151 const base::TimeTicks& timestamp) { | 152 const base::TimeTicks& timestamp) { |
| 152 lock_button_down_ = down; | 153 lock_button_down_ = down; |
| 153 | 154 |
| 154 const SessionStateDelegate* session_state_delegate = | 155 const SessionStateDelegate* session_state_delegate = |
| 155 WmShell::Get()->GetSessionStateDelegate(); | 156 WmShell::Get()->GetSessionStateDelegate(); |
| 156 if (!session_state_delegate->CanLockScreen() || | 157 if (!session_state_delegate->CanLockScreen() || |
| 157 session_state_delegate->IsScreenLocked() || | 158 session_state_delegate->IsScreenLocked() || |
| 158 controller_->LockRequested() || controller_->ShutdownRequested()) { | 159 lock_state_controller_->LockRequested() || |
| 160 lock_state_controller_->ShutdownRequested()) { |
| 159 return; | 161 return; |
| 160 } | 162 } |
| 161 | 163 |
| 162 // Give the power button precedence over the lock button. | 164 // Give the power button precedence over the lock button. |
| 163 if (power_button_down_) | 165 if (power_button_down_) |
| 164 return; | 166 return; |
| 165 | 167 |
| 166 if (down) | 168 if (down) |
| 167 controller_->StartLockAnimation(false); | 169 lock_state_controller_->StartLockAnimation(false); |
| 168 else | 170 else |
| 169 controller_->CancelLockAnimation(); | 171 lock_state_controller_->CancelLockAnimation(); |
| 170 } | 172 } |
| 171 | 173 |
| 172 void PowerButtonController::OnKeyEvent(ui::KeyEvent* event) { | 174 void PowerButtonController::OnKeyEvent(ui::KeyEvent* event) { |
| 173 if (event->key_code() == ui::VKEY_VOLUME_DOWN) { | 175 if (event->key_code() == ui::VKEY_VOLUME_DOWN) { |
| 174 volume_down_pressed_ = event->type() == ui::ET_KEY_PRESSED; | 176 volume_down_pressed_ = event->type() == ui::ET_KEY_PRESSED; |
| 175 #if defined(OS_CHROMEOS) | 177 #if defined(OS_CHROMEOS) |
| 176 if (!event->is_repeat()) { | 178 if (!event->is_repeat()) { |
| 177 chromeos::CrasAudioHandler* audio_handler = | 179 chromeos::CrasAudioHandler* audio_handler = |
| 178 chromeos::CrasAudioHandler::Get(); | 180 chromeos::CrasAudioHandler::Get(); |
| 179 volume_percent_before_screenshot_ = | 181 volume_percent_before_screenshot_ = |
| (...skipping 21 matching lines...) Expand all Loading... |
| 201 } | 203 } |
| 202 | 204 |
| 203 void PowerButtonController::PowerButtonEventReceived( | 205 void PowerButtonController::PowerButtonEventReceived( |
| 204 bool down, | 206 bool down, |
| 205 const base::TimeTicks& timestamp) { | 207 const base::TimeTicks& timestamp) { |
| 206 OnPowerButtonEvent(down, timestamp); | 208 OnPowerButtonEvent(down, timestamp); |
| 207 } | 209 } |
| 208 #endif // defined(OS_CHROMEOS) | 210 #endif // defined(OS_CHROMEOS) |
| 209 | 211 |
| 210 } // namespace ash | 212 } // namespace ash |
| OLD | NEW |