Changeset 119 for trunk/Cocoa/Pester/Source/PSPowerManager.m
- Timestamp:
- 03/13/03 21:46:49 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Cocoa/Pester/Source/PSPowerManager.m
r118 r119 8 8 9 9 #import "PSPowerManager.h" 10 #import "wakein.h"11 10 12 11 #import <IOKit/pwr_mgt/IOPMLib.h> 13 12 #import <IOKit/IOMessage.h> 14 13 #import <CoreFoundation/CoreFoundation.h> 15 16 // MoreIsBetter interfaces17 #include "MoreUNIX.h"18 #include "MoreSecurity.h"19 #include "MoreCFQ.h"20 21 // exceptions22 NSString * const PSPowerManagerException = @"PSPowerManagerException";23 14 24 15 /* … … 61 52 // for superusers 62 53 63 // njr 2003.03.12: both seem to be interchangeable now, and the sleep time is64 // no longer settable as non-root.65 66 #ifdef WAKEIN67 #define PMU_MAGIC_PASSWORD 0x0101BEEF68 #else69 54 #define PMU_MAGIC_PASSWORD 0x0101FACE 70 #endif71 55 72 56 /* ========================================== … … 104 88 kern_return_t kr = kIOReturnNoMemory; 105 89 CFDataRef data; 106 CFMutableDictionaryRef dict = 0;107 90 108 91 data = CFDataCreate(kCFAllocatorDefault, bytes, size); … … 110 93 [(NSData *)data autorelease]; 111 94 112 dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 113 NSCAssert(data != NULL, @"writeDataProperty: CFDictionaryCreateMutable failed"); 114 [(NSMutableDictionary *)dict autorelease]; 115 116 CFDictionarySetValue(dict, name, data); 117 kr = IOConnectSetCFProperties(handle, dict); 118 NSCAssert1(kr == KERN_SUCCESS, @"writeDataProperty: IOConnectSetCFProperties returned an error of type %08lx", (unsigned long)kr); 95 kr = IOConnectSetCFProperty(handle, name, data); 96 NSCAssert1(kr == KERN_SUCCESS, @"writeDataProperty: IOConnectSetCFProperty returned an error of type %08lx", (unsigned long)kr); 119 97 } 120 98 … … 189 167 if (pmuReference == NULL) return NO; 190 168 closePMUComPort(pmuReference); 191 NS_DURING192 [self authorize];193 NS_HANDLER194 return NO; // XXX display error?195 NS_ENDHANDLER196 169 return YES; 197 170 } … … 220 193 } 221 194 222 + (void)_execWakeToolWithRequestDictionary:(NSDictionary *)request;223 {224 AuthorizationRef auth = NULL;225 NSException *exception = NULL;226 227 NS_DURING228 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 rights236 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_HANDLER253 exception = localException;254 NS_ENDHANDLER255 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 195 + (void)setWakeInterval:(unsigned long)wakeInterval; 266 196 { 267 #ifdef WAKEIN268 197 io_service_t pmuReference = [self _pmuReference]; 269 NSLog(@"writePMUProperty[%u] 0x%lX AutoWake = %lu", pmuReference, PMU_MAGIC_PASSWORD, wakeInterval);270 198 writePMUProperty(pmuReference, CFSTR("AutoWake"), (unsigned long *)&wakeInterval, sizeof(wakeInterval)); 271 272 199 closePMUComPort(pmuReference); 273 #else274 [self _execWakeToolWithRequestDictionary: [NSDictionary dictionaryWithObject: [NSNumber numberWithUnsignedLong: wakeInterval] forKey: kPesterWakeTime]];275 #endif276 200 } 277 201 … … 288 212 if (!override) { 289 213 NSDate *wakeTime = [self wakeTime]; 290 override = (wakeTime == nil || [wakeTime compare: time] == NSOrdered Descending);214 override = (wakeTime == nil || [wakeTime compare: time] == NSOrderedAscending); 291 215 } 292 216 }
Note:
See TracChangeset
for help on using the changeset viewer.