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.
5 #include "ui/base/idle/idle.h"
7 #include <ApplicationServices/ApplicationServices.h>
8 #import <Cocoa/Cocoa.h>
10 @interface MacScreenMonitor : NSObject {
12 BOOL screensaverRunning_;
18 getter=isScreensaverRunning) BOOL screensaverRunning;
19 @property (readonly, nonatomic, getter=isScreenLocked) BOOL screenLocked;
23 @implementation MacScreenMonitor
25 @synthesize screensaverRunning = screensaverRunning_;
26 @synthesize screenLocked = screenLocked_;
29 if ((self = [super init])) {
30 NSDistributedNotificationCenter* distCenter =
31 [NSDistributedNotificationCenter defaultCenter];
32 [distCenter addObserver:self
33 selector:@selector(onScreenSaverStarted:)
34 name:@"com.apple.screensaver.didstart"
36 [distCenter addObserver:self
37 selector:@selector(onScreenSaverStopped:)
38 name:@"com.apple.screensaver.didstop"
40 [distCenter addObserver:self
41 selector:@selector(onScreenLocked:)
42 name:@"com.apple.screenIsLocked"
44 [distCenter addObserver:self
45 selector:@selector(onScreenUnlocked:)
46 name:@"com.apple.screenIsUnlocked"
53 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
57 - (void)onScreenSaverStarted:(NSNotification*)notification {
58 screensaverRunning_ = YES;
61 - (void)onScreenSaverStopped:(NSNotification*)notification {
62 screensaverRunning_ = NO;
65 - (void)onScreenLocked:(NSNotification*)notification {
69 - (void)onScreenUnlocked:(NSNotification*)notification {
78 static MacScreenMonitor* g_screenMonitor = nil;
82 void InitIdleMonitor() {
84 g_screenMonitor = [[MacScreenMonitor alloc] init];
87 void CalculateIdleTime(IdleTimeCallback notify) {
88 CFTimeInterval idle_time = CGEventSourceSecondsSinceLastEventType(
89 kCGEventSourceStateCombinedSessionState,
90 kCGAnyInputEventType);
91 notify.Run(static_cast<int>(idle_time));
94 bool CheckIdleStateIsLocked() {
95 return [g_screenMonitor isScreensaverRunning] ||
96 [g_screenMonitor isScreenLocked];