Skip to content

Commit

Permalink
monitor host sleep/wake state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rsanders committed Sep 29, 2010
1 parent b51f3a8 commit 28fdb27
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 9 deletions.
16 changes: 10 additions & 6 deletions BatteryMonitor.m
Expand Up @@ -37,6 +37,7 @@
"Transport Type" = Internal;
*/

// http://www.cocoabuilder.com/archive/cocoa/196169-unable-to-get-event-for-shutdown-restart-using-qa1340.html

@implementation BatteryMonitor

Expand Down Expand Up @@ -124,15 +125,18 @@ - (id)init

_batteryStatus = SCDynamicStoreCopyValue(_dynamicStore, kInternalBatteryKey);

// // Check if we're running on AC power
// CFStringRef powerSourceState = CFDictionaryGetValue(_batteryStatus, CFSTR("Power Source State"));
// if (CFStringCompare(powerSourceState, CFSTR("AC Power"), 0) == kCFCompareEqualTo) {
// isPluggedIn = YES;
// }


_machine = [[SleeperStateMachine alloc] initWithState:(isPluggedIn ? STATE_AC : STATE_BATTERY_NORMAL)
delegate:_delegate];
[_machine _batteryStatusChange:_batteryStatus];

// Check if we're running on AC power
CFStringRef powerSourceState = CFDictionaryGetValue(_batteryStatus, CFSTR("Power Source State"));
if (CFStringCompare(powerSourceState, CFSTR("AC Power"), 0) == kCFCompareEqualTo) {
[_machine powerChangeToAC];
} else {
[_machine _batteryStatusChange:_batteryStatus];
}

// if (! isPluggedIn) {
// // Check if we're already below the battery empty warning threshold
Expand Down
2 changes: 2 additions & 0 deletions SleeperStateMachine.h
Expand Up @@ -26,6 +26,8 @@ typedef enum {
- (sstate_t) batteryNormal;
- (sstate_t) powerChangeToAC;
- (sstate_t) powerChangeToBattery;
- (sstate_t) hostWake;
- (sstate_t) hostSleep;
@end

@interface SleeperStateObject : NSObject <SleepEventHandler> {
Expand Down
85 changes: 82 additions & 3 deletions SleeperStateMachine.m
Expand Up @@ -7,7 +7,7 @@
//

#import "SleeperStateMachine.h"

#import "AppDelegate.h"

@implementation SleeperStateMachine
@synthesize handler;
Expand All @@ -29,6 +29,12 @@ - (SleeperStateMachine*) initWithState:(sstate_t)_state delegate:(AppDelegate*)_

self.warningMinutesLeft = 15;
self.sleepMinutesLeft = 8;

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self selector:@selector(didWakeNotification:)
name: @"NSWorkspaceDidWakeNotification" object: nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self selector:@selector(willSleepNotification:)
name: @"NSWorkspaceWillSleepNotification" object: nil];


return self;
}
Expand Down Expand Up @@ -79,7 +85,7 @@ - (void)_batteryStatusChange:(CFDictionaryRef)newValue
NSInteger hours = timeToFullCharge / 60;
NSInteger minutes = timeToFullCharge - (hours * 60);
NSLog(@"Current battery charge: %d%% Estimated time until full: %d:%02d", currentCapacity, hours, minutes);
[self powerChangeToAC];
// [self powerChangeToAC];
}
else {
NSInteger timeToEmpty = [self _timeToEmpty:newValue];
Expand Down Expand Up @@ -111,7 +117,25 @@ - (void)_powerAdapterStatusChange:(CFDictionaryRef)newValue
}
}

// old
- (void)didWakeNotification:(NSNotification *)notification {
[self hostWake];
NSLog(@"didWakeNotification: received NSWorkspaceDidWakeNotification");
}

- (void)willSleepNotification:(NSNotification *)notification{
[self hostSleep];
NSLog(@"willSleepNotification: received NSWorkspaceWillSleepNotification");
}

// state machine methods

- (sstate_t) hostSleep {
return [self newState:[handler hostSleep]];
}

- (sstate_t) hostWake {
return [self newState:[handler hostWake]];
}

- (sstate_t) batteryCritical {
return [self newState:[handler batteryCritical]];
Expand Down Expand Up @@ -187,6 +211,16 @@ - (void) exit {
NSLog(@"Exiting state %@", [[self class] className]);
}

- (sstate_t) hostWake {
NSLog(@"Host waking up");
return STATE_NO_CHANGE;
}

- (sstate_t) hostSleep {
NSLog(@"Host going to sleep");
return STATE_NO_CHANGE;
}

- (sstate_t) batteryCritical {
return STATE_BATTERY_CRITICAL;
}
Expand All @@ -213,14 +247,59 @@ @implementation SleeperStateAC
@end

@implementation SleeperStateBatteryNormal
- (sstate_t) powerChangeToBattery {
return STATE_BATTERY_NORMAL;
}
@end

@implementation SleeperStateBatteryCritical
- (void) enter
{
NSLog(@"Entering critical battery state, showing dialog and shutting down!");
[machine.delegate emergencySleep];
}

- (void) exit
{
NSLog(@"Exiting critical battery state, hiding dialog!");
}

- (sstate_t) powerChangeToBattery {
return STATE_NO_CHANGE;
}

// low doesn't take us out of shutdown - that may just be jitter, only AC will take us out of shutdown
- (sstate_t) batteryLow {
return STATE_NO_CHANGE;
}

- (sstate_t) batteryNormal {
return STATE_NO_CHANGE;
}
@end



@implementation SleeperStateBatteryLow
- (void) enter
{
NSLog(@"Entering low battery state, showing dialog!");
[machine.delegate showLowBatteryWarning];
}

- (void) exit
{
NSLog(@"Exiting low battery state, hiding dialog!");
[machine.delegate closeLowBatteryWarning:self];
}

- (sstate_t) powerChangeToBattery {
return STATE_NO_CHANGE;
}
@end



@implementation SleeperStateAsleep
@end

Expand Down

0 comments on commit 28fdb27

Please sign in to comment.