Skip navigation.
 
mlRe: Power management with Cocoa
FROM : Aaron Hillegass
DATE : Wed Jan 29 20:22:32 2003

> 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_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

Related mailsAuthorDate
mlPower management with Cocoa Arthur VIGAN Jan 29, 19:04
mlRe: Power management with Cocoa Aaron Hillegass Jan 29, 20:22
mlRe: Power management with Cocoa Chris Ridd Jan 29, 20:30