-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathme_riodoro1AppDelegate.m
135 lines (115 loc) · 5.09 KB
/
me_riodoro1AppDelegate.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
//
// me_riodoro1AppDelegate.m
// Bed Time
//
// Created by Rafał Białek on 12/13/12.
// Copyright (c) 2012 Rafał Białek. All rights reserved.
//
#import "me_riodoro1AppDelegate.h"
#include <IOKit/IOKitLib.h>
#include <IOKit/ps/IOPowerSources.h>
@implementation me_riodoro1AppDelegate
bool safeSleeped;
int64_t systemIdleTime(void) { //This function I got from the internet
int64_t idlesecs = -1;
io_iterator_t iter = 0;
if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iter) == KERN_SUCCESS) {
io_registry_entry_t entry = IOIteratorNext(iter);
if (entry) {
CFMutableDictionaryRef dict = NULL;
if (IORegistryEntryCreateCFProperties(entry, &dict, kCFAllocatorDefault, 0) == KERN_SUCCESS) {
CFNumberRef obj = CFDictionaryGetValue(dict, CFSTR("HIDIdleTime"));
if (obj) {
int64_t nanoseconds = 0;
if (CFNumberGetValue(obj, kCFNumberSInt64Type, &nanoseconds)) {
idlesecs = (nanoseconds >> 30); // Divide by 10^9 to convert from nanoseconds to seconds.
}
}
CFRelease(dict);
}
IOObjectRelease(entry);
}
IOObjectRelease(iter);
}
return idlesecs;
} //Check idle time.
- (void)dealloc
{
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(@"Starting timer... Will check power saver status every 20 seconds.");
[NSTimer scheduledTimerWithTimeInterval:20.0
target:self
selector:@selector(check:)
userInfo:nil
repeats:YES];
safeSleeped = FALSE;
}
-(IBAction)check:(id)sender
{
NSInteger disp = [self displayState];
NSInteger batt = [self battCharge];
int64_t idle = systemIdleTime();
NSInteger pwSource = [self getPowerSource]; //1 for AC, 0 for Battery
NSDictionary* dict = [NSMutableDictionary dictionaryWithContentsOfFile:@"/Library/Preferences/SystemConfiguration/com.apple.PowerManagement.plist"];
NSInteger sleep;
NSInteger criticalBatt = 10;
if (pwSource) sleep = [[dict valueForKeyPath:@"Custom Profile.AC Power.System Sleep Timer"]integerValue];
else sleep = [[dict valueForKeyPath:@"Custom Profile.Battery Power.System Sleep Timer"]integerValue];
sleep*=60;
//NSLog(@"Chcking with idle time: %llds, sleep timer %lds display state: %ld power source: %ld",idle,sleep,disp,pwSource);
if([dict valueForKeyPath:@"Custom Profile.Battery Power.Critical Battery Level"]!=NULL)
{
criticalBatt=[[dict valueForKeyPath:@"Custom Profile.Battery Power.Critical Battery Level"]integerValue];
//NSLog(@"assuming overriden value for critical battery level which is now: %ld",criticalBatt);
}
if(batt <= criticalBatt && !pwSource && !safeSleeped) //Cheking safe sleep
{
safeSleeped=TRUE;
NSLog(@"Safe sleeping the computer!");
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"tell application \"System Events\" to sleep"];
[script executeAndReturnError:nil];
[script release];
}
if(sleep!=0 && idle>=sleep && disp==0) //Checking idle sleep
{
[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(sleepMac:)
userInfo:nil
repeats:NO];
NSLog(@"Computer's been idle for more then %ld, Will wait 10 more seconds for user activity.",sleep);
}
}
-(NSInteger)getPowerSource
{
if(IOPSCopyExternalPowerAdapterDetails()!=NULL) return 1; else return 0;
}
-(IBAction)sleepMac:(id)sender
{
if(systemIdleTime()>7)
{
NSLog(@"Time passed, sleeping...");
safeSleeped=FALSE;
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"tell application \"System Events\" to sleep"];
[script executeAndReturnError:nil];
[script release];
}
}
-(NSInteger)battCharge
{
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:@"return do shell script \"ioreg -l | grep -i capacity | tr '\\n' ' | ' | awk '{printf(\\\"%.0f\\\", $10/$5 * 100)}'\""];
NSAppleEventDescriptor* descriptor = [script executeAndReturnError:nil];
[script release];
return [[descriptor stringValue] integerValue];
}
-(NSInteger)displayState
{
NSAppleScript* script = [[NSAppleScript alloc] initWithSource:@"set result to do shell script \"ioreg -n IODisplayWrangler |grep -i IOPowerManagement\" \n if result contains \"\\\"CurrentPowerState\\\"=4\" then \n return\"2\" \n else if result contains \"\\\"CurrentPowerState\\\"=3\" then \n return \"1\" \n else \n return \"0\" \n end if"];
NSAppleEventDescriptor* descriptor = [script executeAndReturnError:nil];
[script release];
return [[descriptor stringValue] integerValue];
}
@end