-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPDPowerManagement.m
135 lines (97 loc) · 2.9 KB
/
PDPowerManagement.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
//
// PDPowerManagement.m
// SproutedUtilities
//
// Created by Philip Dow on 3/21/06.
// Copyright Sprouted. All rights reserved.
// All inquiries should be directed to [email protected]
//
#import <SproutedUtilities/PDPowerManagement.h>
@implementation PDPowerManagement
PDPowerManagement *_self;
io_connect_t root_port;
void callback(void * x,io_service_t y,natural_t messageType,void * messageArgument);
void callback(void * x,io_service_t y,natural_t messageType,void * messageArgument)
{
switch ( messageType ) {
case kIOMessageSystemWillSleep:
if ( [_self _shouldAllowSleep] ) {
[_self _postPMNotification:PDPowerManagementWillSleep];
IOAllowPowerChange(root_port,(long)messageArgument);
}
else
IOCancelPowerChange(root_port,(long)messageArgument);
break;
case kIOMessageCanSystemSleep:
IOAllowPowerChange(root_port,(long)messageArgument);
break;
case kIOMessageSystemHasPoweredOn:
[_self _postPMNotification:PDPowerManagementPoweredOn];
break;
}
}
#pragma mark -
+ (id)sharedPowerManagement {
static PDPowerManagement *sharedPowerManagement = nil;
if (!sharedPowerManagement)
sharedPowerManagement = [[PDPowerManagement allocWithZone:NULL] init];
return sharedPowerManagement;
}
- (id) init {
if ( self = [super init] ) {
IONotificationPortRef notify;
io_object_t anIterator;
root_port = IORegisterForSystemPower (0,¬ify,callback,&anIterator);
if ( root_port == IO_OBJECT_NULL ) {
NSLog(@"IORegisterForSystemPower failed");
return nil;
}
CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notify), kCFRunLoopDefaultMode);
_permitSleep = YES;
_self = self;
}
return self;
}
#pragma mark -
- (BOOL) permitSleep { return _permitSleep; }
- (void) setPermitSleep:(BOOL)permitSleep {
_permitSleep = permitSleep;
}
- (id) delegate { return _delegate; }
- (void) setDelegate:(id)delegate {
_delegate = delegate;
}
#pragma mark -
- (void) _postPMNotification:(int)message {
NSNumber *dictionaryMessage;
NSDictionary *userInfo;
NSNotification *notification;
// Useful for debugging
/*
switch ( message ) {
case PDPowerManagementWillSleep:
NSLog(@"Going to sleep now");
break;
case PDPowerManagementPoweredOn:
NSLog(@"Just had a nice snooze");
break;
}
*/
dictionaryMessage = [NSNumber numberWithInt:message];
userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
dictionaryMessage, PDPowerManagementMessage, nil];
notification = [NSNotification notificationWithName:PDPowerManagementNotification
object:self userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
- (BOOL) _shouldAllowSleep {
if ( !_permitSleep )
return NO;
else {
if ( _delegate && [_delegate respondsToSelector:@selector(shouldAllowIdleSleep:)] )
return [_delegate shouldAllowIdleSleep:self];
else
return YES;
}
}
@end