-
Notifications
You must be signed in to change notification settings - Fork 5
/
undop.m
136 lines (103 loc) · 3.75 KB
/
undop.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
#import <IOKit/graphics/IOGraphicsLib.h>
#import <ApplicationServices/ApplicationServices.h>
NSArray *_badSites;
NSDictionary *_browserScripts;
CFStringRef key = CFSTR(kIODisplayBrightnessKey);
double lastCheckTime;
double checkDelta = 1.0;
float expectedBrightness;
float baselineBrightness;
#define fatal(fmt, ...) do { fprintf(stderr, fmt, ## __VA_ARGS__); exit(1); } while(0);
NSArray *badSites() {
if(!_badSites) {
_badSites = [[[NSString stringWithContentsOfFile:[NSString stringWithFormat:@"%@/.bad_sites", NSHomeDirectory()]]
componentsSeparatedByString:@"\n"] retain];
}
return _badSites;
}
NSDictionary *browserScripts() {
NSString *safariScript = @"tell application \"Safari\"\n\treturn URL of front document as string\nend tell";
NSString *chromeScript = @"tell application \"Google Chrome\"\n\treturn URL of active tab of window 1\nend tell";
if(!_browserScripts) {
_browserScripts = [[NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:
[[NSAppleScript alloc] initWithSource:safariScript],
[[NSAppleScript alloc] initWithSource:chromeScript], nil]
forKeys: [NSArray arrayWithObjects: @"Safari", @"Google Chrome", nil]] retain];
}
return _browserScripts;
}
NSString *currentHost(NSString *browser) {
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
NSString *host = nil;
if((now - lastCheckTime) > checkDelta) {
NSDictionary *err;
NSAppleScript *script = [browserScripts() objectForKey: browser];
NSAppleEventDescriptor *ret = [script executeAndReturnError:&err];
NSString *url = [ret stringValue];
if(url) {
host = [[[NSURL URLWithString:url] host] retain];
}
lastCheckTime = now;
}
return host;
}
float getBrightness(io_service_t service) {
float brightness;
CGDisplayErr err;
err = IODisplayGetFloatParameter(service, kNilOptions, key, &brightness);
if (err != kIOReturnSuccess)
fatal("Couldn't get display brightness");
if(fabs(expectedBrightness - brightness) > 0.01)
baselineBrightness = brightness;
expectedBrightness = brightness;
return brightness;
}
void setBrightness(io_service_t service, float brightness) {
CGDisplayErr err;
err = IODisplaySetFloatParameter(service, kNilOptions, key, brightness);
expectedBrightness = brightness;
if (err != kIOReturnSuccess)
fatal("Couldn't set display brightness");
}
void decrementBrightness(io_service_t service) {
float brightness = getBrightness(service);
brightness -= 0.3;
setBrightness(service, brightness);
}
int main(int argc, char **argv)
{
io_service_t service;
CGDirectDisplayID targetDisplay;
targetDisplay = CGMainDisplayID();
service = CGDisplayIOServicePort(targetDisplay);
BOOL shocked = NO;
while(true) {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
float brightness = getBrightness(service);
NSString *activeApp = [[[NSWorkspace sharedWorkspace] activeApplication] objectForKey:@"NSApplicationName"];
if([browserScripts() objectForKey: activeApp]) {
NSString *host = currentHost(activeApp);
if(host) {
if([badSites() containsObject: host]) {
if(!shocked) {
shocked = YES;
decrementBrightness(service);
brightness = getBrightness(service);
}
}
[host release];
}
}
else if(shocked) {
shocked = NO;
}
if(brightness < baselineBrightness) {
setBrightness(service, brightness + 0.01);
}
[pool release];
sleep(1);
}
}