SystemConfiguration framework in Cocoa?
SystemConfiguration framework in Cocoa?
- Subject: SystemConfiguration framework in Cocoa?
- From: "Steven M.Palm" <email@hidden>
- Date: Mon, 18 Aug 2003 15:28:51 -0500
I am trying to make a small background (faceless) application that will
watch for certain changes in the system and act on them.
I walked through the Carbon stuff for SystemFramework, and went through
the sample code for user login/out.
I am trying to wrap this into a Cocoa app, though, and having some real
issues... This is unfamiliar ground for me, in several areas. First,
the whole "background faceless" app thing, how to get it to now show up
in the dock or application switcher/etc...
Also, since it has no user interface, I'm not using NSApplication,
etc....
Can anyone take a quick look at this code and tell me where I'm falling
off the truck? It's never calling the callback function. Also, would
this work at all to have the C callback function trigger my object's
method?
Any and all comments will be considered most helpful, including
pointing out what horrible code I have cobbled together. :-)
=======================
SCWatcher_Prefix.h
=======================
#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#define SCSTR(s) (NSString *)CFSTR(s)
#import <SystemConfiguration/SystemConfiguration.h>
#endif
=======================
main.c
=======================
#import <Cocoa/Cocoa.h>
#import "SCWatcher.h"
void SysConfCallBackFunction(SCDynamicStoreRef store,
CFArrayRef changedKeys, void * info)
{
struct SCWatcherExtraInfoStructure* SysConfCallbackInfo;
id myObject;
SEL mySelector;
NSLog(@"In Callback");
SysConfCallbackInfo =
(struct SCWatcherExtraInfoStructure*) info;
// Call method to look at the changedKeys array
myObject = SysConfCallbackInfo->object;
mySelector = SysConfCallbackInfo->selector;
objc_msgSend(myObject, mySelector, changedKeys);
}
int main(int argc, const char *argv[])
{
SCWatcher* myWatcher;
// return NSApplicationMain(argc, argv);
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init ];
myWatcher = [SCWatcher sharedInstance];
NSLog(@"RUNLOOP: %@", [NSRunLoop currentRunLoop]);
[[ NSRunLoop currentRunLoop ] run ];
[pool release];
return 0;
}
=======================
SCWatcher.h
=======================
#import <Foundation/Foundation.h>
void SysConfCallBackFunction(SCDynamicStoreRef store,
CFArrayRef changedKeys, void * info);
struct SCWatcherExtraInfoStructure {
id object;
SEL selector;
};
@interface SCWatcher : NSObject {
}
+(SCWatcher *)sharedInstance;
@end
=======================
SCWatcher.c
=======================
#import "SCWatcher.h"
extern void SysConfCallBackFunction(SCDynamicStoreRef store,
CFArrayRef changedKeys, void * info);
@implementation SCWatcher
+ (SCWatcher *)sharedInstance {
static id sharedInstance = nil;
if (!sharedInstance) {
sharedInstance = [[self alloc] init];
}
return sharedInstance;
}
-(void) SCHandleChangedKeys:(NSArray)changedKeys
{
NSEnumerator* myEnum;
NSString* key;
NSLog(@"SCWatcher: In SCHandleChangedKeys method");
myEnum = [changedKeys objectEnumerator];
while (key = [myEnum nextObject])
{
NSLog(@"Changed key: %@", key);
}
}
-(int) setupNotify
{
NSString* SCDebuggingString = NULL;
SCDynamicStoreContext DynamicStoreContext = { 0, NULL, NULL, NULL,
NULL };
SCDynamicStoreRef DynamicStoreCommunicationMechanism = NULL;
NSMutableArray* ArrayOfNotificationKeys;
Boolean Result;
struct SCWatcherExtraInfoStructure* SysConfCallbackInfo;
CFRunLoopSourceRef SysConfRunLoopSource = NULL;
CFRunLoopRef ourCFRunLoop;
SCDebuggingString = @"SCWatcher";
SysConfCallbackInfo = malloc(sizeof(struct
SCWatcherExtraInfoStructure));
if (SysConfCallbackInfo == NULL)
{
NSLog(@"Setup ExtraInfo Failure");
return (-3);
}
SysConfCallbackInfo->object = self;
SysConfCallbackInfo->selector = @selector(SCHandleChangedKeys:);
DynamicStoreContext.info = (void *)SysConfCallbackInfo;
DynamicStoreCommunicationMechanism = SCDynamicStoreCreate
(NULL, (CFStringRef)SCDebuggingString, SysConfCallBackFunction,
&DynamicStoreContext);
if (DynamicStoreCommunicationMechanism == NULL)
{
NSLog(@"Setup DSCM Failure");
return(-2);
}
ArrayOfNotificationKeys = [NSMutableArray arrayWithCapacity:1];
if (!ArrayOfNotificationKeys)
{
CFRelease(DynamicStoreCommunicationMechanism);
NSLog(@"Setup Array Failure");
return(-5);
}
[ArrayOfNotificationKeys addObject:(NSString *)
SCDynamicStoreKeyCreate(NULL,
CFSTR("State:/IOKit/PowerMangement/CurrentSettings"))];
Result =
SCDynamicStoreSetNotificationKeys(DynamicStoreCommunicationMechanism,
(CFArrayRef)ArrayOfNotificationKeys, NULL);
if (Result == FALSE)
{
CFRelease(DynamicStoreCommunicationMechanism);
NSLog(@"Setup StoreNotification Failure");
return(-6);
}
SysConfRunLoopSource = SCDynamicStoreCreateRunLoopSource
(NULL, DynamicStoreCommunicationMechanism, (CFIndex) 0);
if (SysConfRunLoopSource == NULL)
{
CFRelease(DynamicStoreCommunicationMechanism);
NSLog(@"Setup RunLoop Failure");
return (-7);
}
ourCFRunLoop = [[NSRunLoop currentRunLoop] getCFRunLoop];
CFRunLoopAddSource(ourRunLoop, SysConfRunLoopSource,
kCFRunLoopDefaultMode);
return(0);
}
-(id) init
{
if (self = [super init])
{
NSLog(@"SCWatcher: Initialized....");
[self setupNotify];
}
return self;
}
@end
-. ----. -.-- - -.--
Steve Palm - email@hidden
-. ----. -.-- - -.--
_______________________________________________
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.