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 screensaverRunning_; |
| 13 BOOL screenLocked_; |
| 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 screensaverRunning_ = YES; |
| 54 } |
| 55 |
| 56 - (void)onScreenSaverStopped:(NSNotification*)notification { |
| 57 screensaverRunning_ = NO; |
| 58 } |
| 59 |
| 60 - (void)onScreenLocked:(NSNotification*)notification { |
| 61 screenLocked_ = YES; |
| 62 } |
| 63 |
| 64 - (void)onScreenUnlocked:(NSNotification*)notification { |
| 65 screenLocked_ = NO; |
| 66 } |
| 67 |
| 68 - (BOOL)isScreensaverRunning { |
| 69 return screensaverRunning_; |
| 70 } |
| 71 |
| 72 - (BOOL)isScreenLocked { |
| 73 return screenLocked_; |
| 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 release]; |
| 87 g_screenMonitor = nil; |
| 88 } |
8 | 89 |
9 IdleState CalculateIdleState(unsigned int idle_threshold) { | 90 IdleState CalculateIdleState(unsigned int idle_threshold) { |
| 91 if ([g_screenMonitor isScreensaverRunning] || |
| 92 [g_screenMonitor isScreenLocked]) |
| 93 return IDLE_STATE_LOCKED; |
| 94 |
10 unsigned int idle_time = CGEventSourceSecondsSinceLastEventType( | 95 unsigned int idle_time = CGEventSourceSecondsSinceLastEventType( |
11 kCGEventSourceStateCombinedSessionState, | 96 kCGEventSourceStateCombinedSessionState, |
12 kCGAnyInputEventType); | 97 kCGAnyInputEventType); |
13 if (idle_time >= idle_threshold) | 98 if (idle_time >= idle_threshold) |
14 return IDLE_STATE_IDLE; | 99 return IDLE_STATE_IDLE; |
| 100 |
15 return IDLE_STATE_ACTIVE; | 101 return IDLE_STATE_ACTIVE; |
16 } | 102 } |
OLD | NEW |