source: trunk/Cocoa/Pester/Source/PSPowerManager.m @ 355

Last change on this file since 355 was 355, checked in by Nicholas Riley, 14 years ago

English.lproj/MainMenu.nib: Modernize menu and alarm set dialog
layout. Use keyed archiving (10.2+) nib format.

Info-Pester.plist: Moved from old PBX project.

NJRFSObjectSelector.m: Bug fixes from code sent to Joey: remove
incorrect usage of tryToPerform:with:; fix logic error in menu
construction. Work around Cocoa's deciding that the menu font size
needs adjustment when it doesn't - so the menu font size now matches
the button font size, though the position is still off. Don't pop up
a menu if we're disabled. Use IconRefs? for menu icons, though not
(yet) for the button icon.

NJRHistoryTrackingComboBox.m: Remove item height adjustment
workaround; it now makes the items too tall.

NJRHotKey.m: Add a missing [super dealloc] caught by current GCC.

NJRHotKeyField.m: Add a missing [super dealloc] caught by current GCC.

NJRHotKeyManager.m: Add a missing [super dealloc] caught by current
GCC.

NJRIntervalField.m: Fix some type errors.

NJRQTMediaPopUpButton.m: Replace SoundFileManager? SPI usage, which
doesn't work in Leopard anyway, with manual enumeration of system
sounds. Start migration to QTKit. Use IconRefs? for menu icons.

NJRReadMeController.m: Change source encoding to UTF-8.

NJRSoundManager.m: Fix a type error.

NJRVoicePopUpButton.m: Change source encoding to UTF-8.

NSMenuItem-NJRExtensions.[hm]: Code from ICeCoffEE to use IconRefs? for
menu item icons.

PSAlarm.m: Change source encoding to UTF-8.

PSAlarms.m: Fix a signedness mismatch.

PSAlarmsController.m: Change source encoding to UTF-8.

PSAlarmSetController.m: Set keyboard focus after unchecking "Do
script:" and "Play" checkboxes.

PSAlerts.m: Add a missing [super dealloc] caught by current GCC. Fix
a memory leak in property list serialization.

PSPowerManager.[hm]: There's now API for scheduling wakeups; use it
(the old code asserted on startup). To fix: removing scheduled
wakeup. Fix a small type-checking error.

PSPreferencesController.m: Add a missing [super dealloc] caught by
current GCC.

PSScriptAlert.m: Change source encoding to UTF-8.

PSTimeDateEditor.m: Fix a tiny, and one-time, memory leak.

PSTimer.m: Update for new PSPowerManager API.

Pester.pbproj: Deleted; now supporting OS X 10.4+ (up from 10.1,
aiee.)

Pester.xcodeproj: Xcode 2.4+ project, upgraded targets, etc.

SoundFileManager?.h: Deleted; this SPI no longer exists in Leopard and
possibly earlier.

File size: 2.5 KB
Line 
1//
2//  PSPowerManager.m
3//  Pester
4//
5//  Created by Nicholas Riley on Mon Dec 23 2002.
6//  Copyright (c) 2002 Nicholas Riley. All rights reserved.
7//
8
9#import "PSPowerManager.h"
10
11#import <IOKit/pwr_mgt/IOPMLib.h>
12#import <IOKit/IOMessage.h>
13
14@implementation PSPowerManager
15
16+ (BOOL)autoWakeSupported;
17{
18    // XXX imagine it's supported on all machines that support 10.4
19    return YES;
20}
21
22+ (void)setWakeTime:(NSDate *)time;
23{
24    IOPMSchedulePowerEvent((CFDateRef)time, (CFStringRef)[[NSBundle mainBundle] bundleIdentifier], CFSTR(kIOPMAutoWake));
25}
26
27+ (void)clearWakeTime;
28{
29    // XXX implement (IOPMCancelScheduledPowerEvent)
30}
31
32// modified from RegisterForSleep sample code
33
34- (void)_messageReceived:(natural_t)messageType withArgument:(void *)messageArgument;
35{
36    switch (messageType) {
37        case kIOMessageSystemWillSleep:
38            if ([delegate respondsToSelector: @selector(powerManagerWillDemandSleep:)]) {
39                [delegate powerManagerWillDemandSleep: self];
40                IOAllowPowerChange(root_port, (long)messageArgument);
41            }
42            break;
43        case kIOMessageCanSystemSleep:
44            if ([delegate respondsToSelector: @selector(powerManagerShouldIdleSleep:)]) {
45                if ([delegate powerManagerShouldIdleSleep: self]) {
46                    IOAllowPowerChange(root_port, (long)messageArgument);
47                } else {
48                    IOCancelPowerChange(root_port, (long)messageArgument);
49                }
50            }
51            break;
52        case kIOMessageSystemHasPoweredOn:
53            if ([delegate respondsToSelector: @selector(powerManagerDidWake:)])
54                [delegate powerManagerDidWake: self];
55            break;
56    }
57}
58
59void
60powerCallback(void *refCon, io_service_t service, natural_t messageType, void *messageArgument)
61{
62    [(PSPowerManager *)refCon _messageReceived: messageType withArgument: messageArgument];
63}
64
65- (id)initWithDelegate:(id)aDelegate;
66{
67    if ( (self = [super init]) != nil) {
68        IONotificationPortRef notificationPort;
69
70        delegate = [aDelegate retain];
71        root_port = IORegisterForSystemPower(self, &notificationPort, powerCallback, &notifier);
72        NSAssert(root_port != 0, @"IORegisterForSystemPower failed");
73
74        CFRunLoopAddSource(CFRunLoopGetCurrent(), IONotificationPortGetRunLoopSource(notificationPort), kCFRunLoopDefaultMode);
75    }
76    return self;
77}
78
79- (void)dealloc;
80{
81    IODeregisterForSystemPower(&notifier);
82    [delegate release];
83    [super dealloc];
84}
85
86@end
Note: See TracBrowser for help on using the repository browser.