Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ash/wm/power_button_controller.h" |
| 6 | |
| 7 | #include "ash/ash_switches.h" |
Bo Liu | 5c02ac1 | 2014-05-01 10:37:37 -0700 | [diff] [blame] | 8 | #include "ash/session/session_state_delegate.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 9 | #include "ash/shell.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 10 | #include "ash/shell_window_ids.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 11 | #include "ash/wm/lock_state_controller.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 12 | #include "ash/wm/session_state_animator.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | #include "base/command_line.h" |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 14 | #include "ui/aura/window_event_dispatcher.h" |
Ben Murdoch | a02191e | 2014-04-16 11:17:03 +0100 | [diff] [blame] | 15 | #include "ui/display/types/chromeos/display_snapshot.h" |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 16 | #include "ui/wm/core/compound_event_filter.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 17 | |
| 18 | namespace ash { |
| 19 | |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 20 | PowerButtonController::PowerButtonController( |
| 21 | LockStateController* controller) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 22 | : power_button_down_(false), |
| 23 | lock_button_down_(false), |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 24 | brightness_is_zero_(false), |
| 25 | internal_display_off_and_external_display_on_(false), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 26 | has_legacy_power_button_( |
| 27 | CommandLine::ForCurrentProcess()->HasSwitch( |
| 28 | switches::kAuraLegacyPowerButton)), |
| 29 | controller_(controller) { |
Ben Murdoch | effb81e | 2014-03-31 11:51:25 +0100 | [diff] [blame] | 30 | #if defined(OS_CHROMEOS) |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 31 | Shell::GetInstance()->display_configurator()->AddObserver(this); |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 32 | #endif |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | PowerButtonController::~PowerButtonController() { |
Ben Murdoch | effb81e | 2014-03-31 11:51:25 +0100 | [diff] [blame] | 36 | #if defined(OS_CHROMEOS) |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 37 | Shell::GetInstance()->display_configurator()->RemoveObserver(this); |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 38 | #endif |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void PowerButtonController::OnScreenBrightnessChanged(double percent) { |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 42 | brightness_is_zero_ = percent <= 0.001; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void PowerButtonController::OnPowerButtonEvent( |
| 46 | bool down, const base::TimeTicks& timestamp) { |
| 47 | power_button_down_ = down; |
| 48 | |
| 49 | if (controller_->ShutdownRequested()) |
| 50 | return; |
| 51 | |
| 52 | // Avoid starting the lock/shutdown sequence if the power button is pressed |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 53 | // while the screen is off (http://crbug.com/128451), unless an external |
| 54 | // display is still on (http://crosbug.com/p/24912). |
| 55 | if (brightness_is_zero_ && !internal_display_off_and_external_display_on_) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 56 | return; |
| 57 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 58 | const SessionStateDelegate* session_state_delegate = |
| 59 | Shell::GetInstance()->session_state_delegate(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 60 | if (has_legacy_power_button_) { |
| 61 | // If power button releases won't get reported correctly because we're not |
| 62 | // running on official hardware, just lock the screen or shut down |
| 63 | // immediately. |
| 64 | if (down) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 65 | if (session_state_delegate->CanLockScreen() && |
| 66 | !session_state_delegate->IsScreenLocked() && |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 67 | !controller_->LockRequested()) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 68 | controller_->StartLockAnimationAndLockImmediately(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 69 | } else { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 70 | controller_->RequestShutdown(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 71 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 72 | } |
| 73 | } else { // !has_legacy_power_button_ |
| 74 | if (down) { |
| 75 | // If we already have a pending request to lock the screen, wait. |
| 76 | if (controller_->LockRequested()) |
| 77 | return; |
| 78 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 79 | if (session_state_delegate->CanLockScreen() && |
| 80 | !session_state_delegate->IsScreenLocked()) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 81 | controller_->StartLockAnimation(true); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 82 | } else { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 83 | controller_->StartShutdownAnimation(); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 84 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 85 | } else { // Button is up. |
| 86 | if (controller_->CanCancelLockAnimation()) |
| 87 | controller_->CancelLockAnimation(); |
| 88 | else if (controller_->CanCancelShutdownAnimation()) |
| 89 | controller_->CancelShutdownAnimation(); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void PowerButtonController::OnLockButtonEvent( |
| 95 | bool down, const base::TimeTicks& timestamp) { |
| 96 | lock_button_down_ = down; |
| 97 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 98 | const SessionStateDelegate* session_state_delegate = |
| 99 | Shell::GetInstance()->session_state_delegate(); |
| 100 | if (!session_state_delegate->CanLockScreen() || |
| 101 | session_state_delegate->IsScreenLocked() || |
| 102 | controller_->LockRequested() || |
| 103 | controller_->ShutdownRequested()) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 104 | return; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 105 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 106 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 107 | // Give the power button precedence over the lock button (we don't expect both |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 108 | // buttons to be present, so this is just making sure that we don't do |
| 109 | // something completely stupid if that assumption changes later). |
| 110 | if (power_button_down_) |
| 111 | return; |
| 112 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 113 | if (down) |
| 114 | controller_->StartLockAnimation(false); |
| 115 | else |
| 116 | controller_->CancelLockAnimation(); |
| 117 | } |
| 118 | |
Ben Murdoch | effb81e | 2014-03-31 11:51:25 +0100 | [diff] [blame] | 119 | #if defined(OS_CHROMEOS) |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 120 | void PowerButtonController::OnDisplayModeChanged( |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 121 | const ui::DisplayConfigurator::DisplayStateList& display_states) { |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 122 | bool internal_display_off = false; |
| 123 | bool external_display_on = false; |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 124 | for (size_t i = 0; i < display_states.size(); ++i) { |
| 125 | const ui::DisplayConfigurator::DisplayState& state = display_states[i]; |
| 126 | if (state.display->type() == ui::DISPLAY_CONNECTION_TYPE_INTERNAL) { |
| 127 | if (!state.display->current_mode()) |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 128 | internal_display_off = true; |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 129 | } else if (state.display->current_mode()) { |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 130 | external_display_on = true; |
| 131 | } |
| 132 | } |
| 133 | internal_display_off_and_external_display_on_ = |
| 134 | internal_display_off && external_display_on; |
| 135 | } |
| 136 | #endif |
| 137 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 138 | } // namespace ash |