Changeset 118 for trunk/Cocoa/Pester/Source/PSPowerManager.m
- Timestamp:
- 03/12/03 21:30:33 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Cocoa/Pester/Source/PSPowerManager.m
r105 r118 8 8 9 9 #import "PSPowerManager.h" 10 #import "wakein.h" 10 11 11 12 #import <IOKit/pwr_mgt/IOPMLib.h> 12 13 #import <IOKit/IOMessage.h> 13 14 #import <CoreFoundation/CoreFoundation.h> 15 16 // MoreIsBetter interfaces 17 #include "MoreUNIX.h" 18 #include "MoreSecurity.h" 19 #include "MoreCFQ.h" 20 21 // exceptions 22 NSString * const PSPowerManagerException = @"PSPowerManagerException"; 14 23 15 24 /* … … 52 61 // for superusers 53 62 54 #define PMU_MAGIC_PASSWORD 0x0101FACE // BEEF 63 // njr 2003.03.12: both seem to be interchangeable now, and the sleep time is 64 // no longer settable as non-root. 65 66 #ifdef WAKEIN 67 #define PMU_MAGIC_PASSWORD 0x0101BEEF 68 #else 69 #define PMU_MAGIC_PASSWORD 0x0101FACE 70 #endif 55 71 56 72 /* ========================================== … … 173 189 if (pmuReference == NULL) return NO; 174 190 closePMUComPort(pmuReference); 191 NS_DURING 192 [self authorize]; 193 NS_HANDLER 194 return NO; // XXX display error? 195 NS_ENDHANDLER 175 196 return YES; 176 197 } … … 196 217 if (rawWakeTime == 0) return nil; 197 218 // XXX no idea what the epoch is supposed to be, but this works... 198 return [NSDate dateWithTimeIntervalSinceReferenceDate: rawWakeTime - 18446744072475718320LLU]; 199 } 200 201 + (void)setWakeTime:(NSDate *)time; 202 { 219 return [NSDate dateWithTimeIntervalSinceReferenceDate: rawWakeTime - [[NSTimeZone systemTimeZone] secondsFromGMT] - 18446744072475736320LLU]; 220 } 221 222 + (void)_execWakeToolWithRequestDictionary:(NSDictionary *)request; 223 { 224 AuthorizationRef auth = NULL; 225 NSException *exception = NULL; 226 227 NS_DURING 228 CFURLRef tool = NULL; 229 Boolean toolFound; 230 OSStatus err = MoreSecCopyHelperToolURLAndCheckBundled( 231 CFBundleGetMainBundle(), CFSTR("wakeinTemplate"), kApplicationSupportFolderType, CFSTR("Pester"), CFSTR("wakein"), &tool, &toolFound); 232 if (err != noErr) [NSException raise: PSPowerManagerException format: NSLocalizedString(@"Can't set up timed wake from sleep: unable to copy helper tool to Application Support folder (error %u)", "MoreSecCopyHelperToolURLAndCheckBundled failure"), err]; 233 [(NSURL *)tool autorelease]; 234 235 // if we've found the tool (and it's setuid root), still get an AuthorizationRef, but don't bother to obtain additional rights 236 err = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, toolFound ? kAuthorizationFlagExtendRights | kAuthorizationFlagInteractionAllowed : kAuthorizationFlagDefaults, &auth); 237 if (err != noErr) [NSException raise: PSPowerManagerException format: NSLocalizedString(@"Can't set up timed wake from sleep: AuthorizationCreate failed (error %u)", "AuthorizationCreate failure"), err]; 238 239 NSDictionary *response; 240 err = MoreSecExecuteRequestInHelperTool(tool, auth, (CFDictionaryRef)request, (CFDictionaryRef *)&response); 241 [response autorelease]; 242 if (err != noErr) [NSException raise: PSPowerManagerException format: NSLocalizedString(@"Can't set up timed wake from sleep: can't obtain response from helper tool (error %u)", "MoreSecExecuteRequestInHelperTool failure"), err]; 243 244 NSLog(@"%@", response); 245 246 NSString *wakeinException = [response objectForKey: kPesterWakeException]; 247 if (wakeinException != nil) [NSException raise: PSPowerManagerException format: NSLocalizedString(@"Can't set up timed wake from sleep: helper tool reported the error '%@'", "kPesterWakeException"), wakeinException]; 248 249 err = MoreSecGetErrorFromResponse((CFDictionaryRef)response); 250 if (err != noErr) [NSException raise: PSPowerManagerException format: NSLocalizedString(@"Can't set up timed wake from sleep: helper tool reported an error of type %u", "MoreSecGetErrorFromResponse"), err]; 251 252 NS_HANDLER 253 exception = localException; 254 NS_ENDHANDLER 255 256 if (auth != NULL) AuthorizationFree(auth, kAuthorizationFlagDestroyRights); 257 if (exception != NULL) [exception raise]; 258 } 259 260 + (void)authorize; 261 { 262 [self _execWakeToolWithRequestDictionary: [NSDictionary dictionary]]; 263 } 264 265 + (void)setWakeInterval:(unsigned long)wakeInterval; 266 { 267 #ifdef WAKEIN 203 268 io_service_t pmuReference = [self _pmuReference]; 204 unsigned long wakeTime; 205 206 if (time == nil) wakeTime = 0; 207 else { 208 wakeTime = [time timeIntervalSinceNow]; 209 if (wakeTime == 0) wakeTime++; // 0 will disable 269 NSLog(@"writePMUProperty[%u] 0x%lX AutoWake = %lu", pmuReference, PMU_MAGIC_PASSWORD, wakeInterval); 270 writePMUProperty(pmuReference, CFSTR("AutoWake"), (unsigned long *)&wakeInterval, sizeof(wakeInterval)); 271 272 closePMUComPort(pmuReference); 273 #else 274 [self _execWakeToolWithRequestDictionary: [NSDictionary dictionaryWithObject: [NSNumber numberWithUnsignedLong: wakeInterval] forKey: kPesterWakeTime]]; 275 #endif 276 } 277 278 + (void)setWakeTime:(NSDate *)time overrideIfEarlier:(BOOL)override; 279 { 280 unsigned long wakeInterval; 281 282 if (time == nil) { 283 wakeInterval = 0; 284 override = YES; 285 } else { 286 wakeInterval = [time timeIntervalSinceNow]; 287 if (wakeInterval == 0) wakeInterval++; // 0 will disable 288 if (!override) { 289 NSDate *wakeTime = [self wakeTime]; 290 override = (wakeTime == nil || [wakeTime compare: time] == NSOrderedDescending); 291 } 210 292 } 211 writePMUProperty(pmuReference, CFSTR("AutoWake"), (unsigned long *)&wakeTime, sizeof(wakeTime)); 212 213 closePMUComPort(pmuReference); 293 294 if (override) { 295 [self setWakeInterval: wakeInterval]; 296 } 214 297 } 215 298 216 299 + (void)clearWakeTime; 217 300 { 218 [self setWakeTime: nil ];301 [self setWakeTime: nil overrideIfEarlier: YES]; 219 302 } 220 303
Note:
See TracChangeset
for help on using the changeset viewer.