power_button_controller.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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" 8#include "ash/shell.h" 9#include "ash/shell_delegate.h" 10#include "ash/shell_window_ids.h" 11#include "ash/wm/session_state_animator.h" 12#include "ash/wm/session_state_controller.h" 13#include "base/command_line.h" 14#include "ui/aura/root_window.h" 15#include "ui/views/corewm/compound_event_filter.h" 16 17namespace ash { 18 19PowerButtonController::PowerButtonController(SessionStateController* controller) 20 : power_button_down_(false), 21 lock_button_down_(false), 22 screen_is_off_(false), 23 has_legacy_power_button_( 24 CommandLine::ForCurrentProcess()->HasSwitch( 25 switches::kAuraLegacyPowerButton)), 26 controller_(controller) { 27} 28 29PowerButtonController::~PowerButtonController() { 30} 31 32void PowerButtonController::OnScreenBrightnessChanged(double percent) { 33 screen_is_off_ = percent <= 0.001; 34} 35 36void PowerButtonController::OnPowerButtonEvent( 37 bool down, const base::TimeTicks& timestamp) { 38 power_button_down_ = down; 39 40 if (controller_->ShutdownRequested()) 41 return; 42 43 // Avoid starting the lock/shutdown sequence if the power button is pressed 44 // while the screen is off (http://crbug.com/128451). 45 if (screen_is_off_) 46 return; 47 48 Shell* shell = Shell::GetInstance(); 49 if (has_legacy_power_button_) { 50 // If power button releases won't get reported correctly because we're not 51 // running on official hardware, just lock the screen or shut down 52 // immediately. 53 if (down) { 54 if (shell->CanLockScreen() && !shell->IsScreenLocked() && 55 !controller_->LockRequested()) { 56 controller_->StartLockAnimationAndLockImmediately(); 57 } else { 58 controller_->RequestShutdown(); 59 } 60 } 61 } else { // !has_legacy_power_button_ 62 if (down) { 63 // If we already have a pending request to lock the screen, wait. 64 if (controller_->LockRequested()) 65 return; 66 67 if (shell->CanLockScreen() && !shell->IsScreenLocked()) 68 controller_->StartLockAnimation(true); 69 else 70 controller_->StartShutdownAnimation(); 71 } else { // Button is up. 72 if (controller_->CanCancelLockAnimation()) 73 controller_->CancelLockAnimation(); 74 else if (controller_->CanCancelShutdownAnimation()) 75 controller_->CancelShutdownAnimation(); 76 } 77 } 78} 79 80void PowerButtonController::OnLockButtonEvent( 81 bool down, const base::TimeTicks& timestamp) { 82 lock_button_down_ = down; 83 84 Shell* shell = Shell::GetInstance(); 85 if (!shell->CanLockScreen() || shell->IsScreenLocked() || 86 controller_->LockRequested() || controller_->ShutdownRequested()) { 87 return; 88 } 89 90 // Give the power button precedence over the lock button (we don't expect both 91 // buttons to be present, so this is just making sure that we don't do 92 // something completely stupid if that assumption changes later). 93 if (power_button_down_) 94 return; 95 96 if (down) 97 controller_->StartLockAnimation(false); 98 else 99 controller_->CancelLockAnimation(); 100} 101 102} // namespace ash 103