Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Mac OS X Notifications about USB device removal / attachment



Hi Ajay,

On Sep 28, 2006, at 4:03 PM, email@hidden wrote:

Hi Garth,

Using the Apple sample code as a base I make the following calls in my Cocoa app:

1) notificationObject = IONotificationPortCreate (kIOMasterPortDefault);

2) notificationRunLoopSource = IONotificationPortGetRunLoopSource (notificationObject);

3) CFRunLoopAddSource (CFRunLoopGetCurrent(), notificationRunLoopSource, kCFRunLoopDefaultMode);

4) I then create a matchingDictionary for my device and use it like this:

IOServiceAddMatchingNotification (notificationObject, kIOTerminatedNotification, matchingDict, MyUSBDeviceWasRemoved,
NULL, &removedIter);

The above call succeeds.

But my apps callbacks never get called! I even tried adding a call to CFRunLoopRun () but when I do this my app appears to "hang".

Yeah, that won't work.

Did you remember to arm the callbacks by calling the callback yourself once? The notification isn't armed when first created--see the documentation on IOServiceAddMatchingNotification.

Also, CFRunLoopGetCurrent() will get the run loop for the thread currently running, so it matters where you call it.

There are a couple of samples you can look at in /Developer/Examples/IOKit:

scsi/SCSITaskLib/Authoring/Cocoa
firewire/AVCBrowser



I can't figure out what I am doing wrong. Perhaps in my call to CFRunLoopAddSource instead of calling CFRunLoopGetCurrent I need to do something different to get the Cocoa apps CFRunLoop?

Thanks for any help, Ajay Nath

--gc


-----Original Message-----
Subject: Re: Mac OS X Notifications about USB device removal / attachment

  Hi bwooster0,
On Sep 27, 2006, at 6:25 PM, email@hidden wrote:

All the Apple samples are for CoreFoundation tools that usually make a call to CFRunLoopRun after the callbacks are set up.

Is there a way to get the callback system working with a Cocoa app?

Yes.


Take a look at NSRunLoop in the Cocoa docs. You can get the NSRunLoop for the current thread, then ask that NSRunLoop for its underlying CFRunLoop. Once you have that everything else is the same.


Re. using the main thread's run loop vs. spawning a separate thread, this is a design question you have to answer for yourself. If the callbacks are lightweight or if they do any Cocoa UI stuff, they can be run on the main thread. That approach greatly simplifies your design and avoids the whole raft of concurrency issues that crop up once you start a second thread.


But if the callbacks do lots of work, keep in mind that other events like keyboard and mouse events will be blocked until your callback completes. That could make your app appear to be unresponsive to the user.


HTH,
--gc
____________________________________________________________________
Garth Cummings  email@hidden
Sr. Software Engineer
Apple Developer Technical Support






=

________________________________________________________________________
Check Out the new free AIM(R) Mail -- 2 GB of storage and industry-leading spam and email virus protection.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Usb mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

____________________________________________________________________
Garth Cummings  email@hidden
Sr. Software Engineer
Apple Developer Technical Support



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Usb mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/usb/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.