-
Notifications
You must be signed in to change notification settings - Fork 10
/
iMacMonitorAppDelegate.m
106 lines (74 loc) · 2.14 KB
/
iMacMonitorAppDelegate.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// iMacMonitorAppDelegate.m
// iMacMonitor
//
// Created by Florian Bogner on 4/16/13.
// Copyright 2013 __MyCompanyName__. All rights reserved.
//
#import "iMacMonitorAppDelegate.h"
@implementation iMacMonitorAppDelegate
@synthesize window;
#define DELAY_INTERVAL 5
- (void)pressKeyWithModifiers:(CGKeyCode) key {
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStatePrivate);
CGEventRef keyDown = CGEventCreateKeyboardEvent(source, key, TRUE);
CGPostKeyboardEvent(0,key,TRUE);
CFRelease(keyDown);
CFRelease(source);
}
- (void)releaseKeyWithModifiers:(CGKeyCode) key {
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStatePrivate);
CGEventRef keyUp = CGEventCreateKeyboardEvent(source, key, FALSE);
CGPostKeyboardEvent(0,key,FALSE);
CFRelease(keyUp);
CFRelease(source);
}
-(void)enableMonitorMode {
printf("Enabling monitor mode!\n");
// press cmd
[self pressKeyWithModifiers:kVK_Command];
// press brightness up
[self pressKeyWithModifiers:144];
// release both keys
[self releaseKeyWithModifiers:144];
[self releaseKeyWithModifiers: kVK_Command];
}
- (int)checkForMacOverThunderbolt {
const char* cmd="/usr/sbin/system_profiler SPThunderboltDataType|/usr/bin/grep 0xA27";
return system(cmd);
}
- (void)sleepComputer {
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"tell application \"System Events\" to sleep"];
NSDictionary *errorInfo;
[script executeAndReturnError:&errorInfo];
[script release];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
timer=[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timedCheck:) userInfo:nil repeats:YES];
macConnected=false;
delay=0;
}
-(void) dealloc {
[timer invalidate];
[super dealloc];
}
- (void)timedCheck: (NSNotification*)aNotification {
if ([self checkForMacOverThunderbolt]==0) {
delay=0;
if (!macConnected) {
macConnected=TRUE;
[self enableMonitorMode];
}
}
else {
delay++;
macConnected=FALSE;
if (delay==DELAY_INTERVAL) {
[self sleepComputer];
}
if (delay>DELAY_INTERVAL) {
delay=DELAY_INTERVAL+1;
}
}
}
@end