-
Notifications
You must be signed in to change notification settings - Fork 65
/
ComputerStateMonitor.m
62 lines (51 loc) · 1.63 KB
/
ComputerStateMonitor.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// ComputerStateMonitor.m
// yubiswitch
//
// Created by Angelo Failla on 8/29/15.
// Copyright (c) 2015 Angelo Failla. All rights reserved.
//
#import "ComputerStateMonitor.h"
#import <Foundation/NSDistributedNotificationCenter.h>
@implementation ComputerStateMonitor
- (id)initWithYubiKey:(YubiKey *)yubikey {
if ( self = [super init] ) {
if (yubikey) {
yk = yubikey;
NSDistributedNotificationCenter * center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver: self
selector: @selector(receive:)
name: @"com.apple.screenIsLocked"
object: nil
];
[center addObserver: self
selector: @selector(receive:)
name: @"com.apple.screenIsUnlocked"
object: nil
];
return self;
}
else {
return nil;
}
} else {
return nil;
}
}
-(void) receive: (NSNotification*) notification {
BOOL activated =
[[NSUserDefaults standardUserDefaults] boolForKey:@"disableAtLockSleep"];
if (!activated) {
return;
}
NSLog(@"Received notification %s", [[notification name] UTF8String]);
if ([[notification name] isEqual:@"com.apple.screenIsLocked"]) {
NSLog(@"Screen is locked, renabling yubikey");
[yk enable];
}
if ([[notification name] isEqual:@"com.apple.screenIsUnlocked"]) {
NSLog(@"Screen is unlocked, disabling yubikey");
[yk disable];
}
}
@end