Re: Getting Carbon Volume Mount Events in a Cocoa (Core Foundation) process
Re: Getting Carbon Volume Mount Events in a Cocoa (Core Foundation) process
- Subject: Re: Getting Carbon Volume Mount Events in a Cocoa (Core Foundation) process
- From: James Bucanek <email@hidden>
- Date: Tue, 9 Oct 2007 08:59:46 -0700
Eric Schlegel <mailto:email@hidden> wrote (Monday, October
8, 2007 11:06 PM -0700):
For future reference, I'd code this something like:
EventTypeSpec eventTypes[] =
{
{ kEventClassVolume, kEventVolumeMounted },
{ kEventClassVolume, kEventVolumeUnmounted }
};
InstallApplicationEventHandler(sVolumeEventCallback,GetEventTypeCount(eventTypes),eventTypes,self,&volumeEventHandlerRef);
Thanks, that's much cleaner. I clearly don't do enough Carbon
programming. ;)
I've gone over the Carbon-Cocoa Integration guide and it
doesn't mention that you have to do anything special to get
Carbon events in a Cocoa application. It indicates that this
should "just work," but clearly something is missing.
The only thing that really should be required is that you spin the runloop, and it sounds like you're doing that.
If it makes any difference, the main NSRunLoop for this
process is run using [NSRunLoop runMode:beforeDate:].
What mode are you passing?
NSDefaultRunLoopMode
Anything custom?
A tiny bit custom. I add a special port and a couple of timers
to the run loop before I start it running. The port is so I can
terminate the run loop. My -terminate method clears the
isRunning flag and then sends a message to
receiveStopMessagePort, this lets [NSRunLoop
runMode:beforeDate:] method return and the program can exit.
Beyond that, I don't think I'm doing anything unusual. Here's my
entire run method.
- (void)run
{
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
NSDate* endOfTime = [NSDate distantFuture];
receiveStopMessagePort = [[NSPort port] retain];
[receiveStopMessagePort setDelegate:self];
[runLoop addPort:receiveStopMessagePort forMode:NSDefaultRunLoopMode];
// Add a timer to the run loop to fire the startScheduler message
[runLoop addTimer:[[[NSTimer alloc]
initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:0.1]
interval:0.0
target:self
selector:@selector(startScheduler)
userInfo:nil
repeats:NO] autorelease]
forMode:NSDefaultRunLoopMode];
// Create a timer that normally never fires. Used to
terminate the command if a SIGTERM signal is received
sTermSignalTimer = [[NSTimer alloc] initWithFireDate:endOfTime
interval:0.0
target:self
selector:@selector(termSignalReceived:)
userInfo:nil
repeats:NO];
[runLoop addTimer:sTermSignalTimer forMode:NSDefaultRunLoopMode];
// Register a signal handler that will fire the timer
signal(SIGTERM,signal_handler); // catch SIGTERM
signal(SIGHUP,SIG_IGN); // ignore SIGHUP
isRunning = YES;
while (isRunning && [runLoop runMode:NSDefaultRunLoopMode beforeDate:endOfTime])
;
[sTermSignalTimer release]; // SIGTERM
timer is now invalid
sTermSignalTimer = nil;
receiveStopMessagePort = nil;
}
--
James Bucanek
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden