Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/idle.h" | 5 #import "chrome/browser/idle.h" |
| 6 | 6 |
| 7 #include <CoreGraphics/CGEventSource.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 #import <CoreGraphics/CGEventSource.h> | |
| 9 | |
| 10 @interface MacScreenMonitor : NSObject { | |
| 11 @private | |
| 12 BOOL isScreensaverRunning_; | |
| 13 BOOL isScreenLocked_; | |
| 14 } | |
| 15 | |
| 16 - (BOOL)isScreensaverRunning; | |
| 17 - (BOOL)isScreenLocked; | |
| 18 | |
| 19 @end | |
| 20 | |
| 21 @implementation MacScreenMonitor | |
| 22 | |
| 23 - (id)init { | |
| 24 if ((self = [super init])) { | |
| 25 NSDistributedNotificationCenter* distCenter = | |
| 26 [NSDistributedNotificationCenter defaultCenter]; | |
| 27 [distCenter addObserver:self | |
| 28 selector:@selector(onScreenSaverStarted:) | |
| 29 name:@"com.apple.screensaver.didstart" | |
| 30 object:nil]; | |
| 31 [distCenter addObserver:self | |
| 32 selector:@selector(onScreenSaverStopped:) | |
| 33 name:@"com.apple.screensaver.didstop" | |
| 34 object:nil]; | |
| 35 [distCenter addObserver:self | |
| 36 selector:@selector(onScreenLocked:) | |
| 37 name:@"com.apple.screenIsLocked" | |
| 38 object:nil]; | |
| 39 [distCenter addObserver:self | |
| 40 selector:@selector(onScreenUnlocked:) | |
| 41 name:@"com.apple.screenIsUnlocked" | |
| 42 object:nil]; | |
| 43 } | |
| 44 return self; | |
| 45 } | |
| 46 | |
| 47 - (void)dealloc { | |
| 48 [[NSDistributedNotificationCenter defaultCenter] removeObject:self]; | |
| 49 [super dealloc]; | |
| 50 } | |
| 51 | |
| 52 - (void)onScreenSaverStarted:(NSNotification*)notification { | |
| 53 isScreensaverRunning_ = YES; | |
|
dmac
2011/01/21 19:34:44
cocoa conventions would not have the "is" in front
jianli
2011/01/21 20:21:00
Done.
| |
| 54 } | |
| 55 | |
| 56 - (void)onScreenSaverStopped:(NSNotification*)notification { | |
| 57 isScreensaverRunning_ = NO; | |
| 58 } | |
| 59 | |
| 60 - (void)onScreenLocked:(NSNotification*)notification { | |
| 61 isScreenLocked_ = YES; | |
| 62 } | |
| 63 | |
| 64 - (void)onScreenUnlocked:(NSNotification*)notification { | |
| 65 isScreenLocked_ = NO; | |
| 66 } | |
| 67 | |
| 68 - (BOOL)isScreensaverRunning { | |
| 69 return isScreensaverRunning_; | |
| 70 } | |
| 71 | |
| 72 - (BOOL)isScreenLocked { | |
| 73 return isScreenLocked_; | |
| 74 } | |
| 75 | |
| 76 @end | |
| 77 | |
| 78 MacScreenMonitor* g_screenMonitor = nil; | |
| 79 | |
| 80 void InitIdleMonitor() { | |
| 81 if (!g_screenMonitor) | |
| 82 g_screenMonitor = [[MacScreenMonitor alloc] init]; | |
| 83 } | |
| 84 | |
| 85 void StopIdleMonitor() { | |
| 86 g_screenMonitor = nil; | |
| 87 } | |
| 8 | 88 |
| 9 IdleState CalculateIdleState(unsigned int idle_threshold) { | 89 IdleState CalculateIdleState(unsigned int idle_threshold) { |
| 90 if ([g_screenMonitor isScreensaverRunning] || | |
| 91 [g_screenMonitor isScreenLocked]) | |
| 92 return IDLE_STATE_LOCKED; | |
| 93 | |
| 10 unsigned int idle_time = CGEventSourceSecondsSinceLastEventType( | 94 unsigned int idle_time = CGEventSourceSecondsSinceLastEventType( |
| 11 kCGEventSourceStateCombinedSessionState, | 95 kCGEventSourceStateCombinedSessionState, |
| 12 kCGAnyInputEventType); | 96 kCGAnyInputEventType); |
| 13 if (idle_time >= idle_threshold) | 97 if (idle_time >= idle_threshold) |
| 14 return IDLE_STATE_IDLE; | 98 return IDLE_STATE_IDLE; |
| 99 | |
| 15 return IDLE_STATE_ACTIVE; | 100 return IDLE_STATE_ACTIVE; |
| 16 } | 101 } |
| OLD | NEW |