Re: Power management with Cocoa
Re: Power management with Cocoa
- Subject: Re: Power management with Cocoa
- From: Aaron Hillegass <email@hidden>
- Date: Wed, 29 Jan 2003 14:22:32 -0500
I would like to know if there is a way to know if the computer is using
battery or sector power on PowerBook machines?
The best way to do this is to use the SystemConfiguration framework.
At the Big Nerd Ranch, this is covered in our Core Mac OS X and Unix
Programming class. All the stuff you wish you knew is covered in this
class: rendezvous, multithreading, the run loop, the keychain, etc. It
will be offered again on Mar 31-April 4 in Atlanta.
To see changes in the state of the battery, you will want to register
to receive notifications (create a function to be called and add an
item to the runloop) from configd. All this rotates around the
SCDynamicStore.
I've created an example of what you want to do:
http://www.bignerdranch.com/Resources/ComputerUser.tgz
Here are the highlights:
void storeCallBack( SCDynamicStoreRef store,
CFArrayRef changedKeys,
void *info)
{
// info is the AppController
NSLog(@"reloading");
AppController *object = (AppController *)info;
[object reloadKeysValues];
}
#define PATTERN
(CFStringRef)@"State:/IOKit/PowerSources/InternalBattery-1"
SCDynamicStoreRef dynamicStore;
// This reads the data from the dynamic store
- (void)reloadKeysValues
{
[values release];
values = (NSDictionary
*)SCDynamicStoreCopyValue(dynamicStore,PATTERN);
[keys release];
keys = [[values allKeys] retain];
[interfaces reloadData];
}
- (id)init
{
SCDynamicStoreContext context = {0, self, NULL, NULL, NULL};
[super init];
// Create a dynamic store ref
dynamicStore = SCDynamicStoreCreate (NULL,
(CFStringRef)@"ThisIsATest", storeCallBack, &context);
// What am I interested in receiving notifications
// about?
NSArray *array = [NSArray arrayWithObject:(NSString *)PATTERN];
// Register for the notification
if (!SCDynamicStoreSetNotificationKeys(dynamicStore,
(CFArrayRef)array, NULL)) {
NSLog(@"failed to set notification keys");
}
// Create a run loop source
CFRunLoopSourceRef runLoopSource =
SCDynamicStoreCreateRunLoopSource(NULL,dynamicStore,0);
// Get hold of the current run loop
CFRunLoopRef runLoop = CFRunLoopGetCurrent();
// Add the source to the run loop
CFRunLoopAddSource(runLoop, runLoopSource, kCFRunLoopDefaultMode);
// The source will be retained by the run loop
CFRelease(runLoopSource);
// Update the data for the table
[self reloadKeysValues];
return self;
}
I hope this is useful to you.
Best wishes,
Aaron Hillegass
Big Nerd Ranch -- Intensive Classes for Programmers
404-210-5663
http://www.bignerdranch.com/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.