C callback functions in Cocoa
C callback functions in Cocoa
- Subject: C callback functions in Cocoa
- From: Bill Cheeseman <email@hidden>
- Date: Sat, 16 Nov 2002 08:09:55 -0500
The accessibility mailing list appears to be down, so I'm posting this here.
It's really a Cocoa question, anyway.
I'm stuck trying to get accessibility notifications to work in my Cocoa app.
The accessibility API is strictly a C API. It provides a mechanism to create
and register "observers" to receive callbacks, where a callback should be a
C function conforming to a defined prototype. I can successfully create an
observer, but I can't figure out why my C callback function isn't being
called.
The key requirement is to use the Core Foundation CFRunLoopAddSource
function to add the observer to the current run loop in default mode. This
function is used in a number of callback situations in Carbon apps, and I've
looked at examples of its usage. But I don't know how to do this in a Cocoa
application.
Here are the essential features of my code:
1. I declare the callback function according to the prescribed prototype in
myClass.h like so:
void observerCallbackFunction(AXObserverRef observer, AXUIElementRef
element, CFStringRef notification, void *refcon);
2. I define the callback function in myClass.m like so:
void observerCallbackFunction(AXObserverRef observer, AXUIElementRef
element, CFStringRef notification, void *refcon) {
NSLog(@"notification = %@", (NSString *)notification);
}
3. I create a new observer by calling the following in a Cocoa method:
AXObserverRef observer;
AXError err = AXObserverCreate(pid, observerCallbackFunction, &observer);
4. Right after creating the observer, I add it to the run loop like this:
CFRunLoopAddSource(CFRunLoopGetCurrent(),
AXObserverGetRunLoopSource(observer), kCFRunLoopDefaultMode);
5. I register a notification with the observer in another Cocoa method like
this:
AXError err = AXObserverAddNotification(observer, element,
(CFStringRef)notification, info);
In debugging, I've verified that observer, element, notification, and pid
are correct (I don't care about refcon for the moment). In particular, I've
verified that the observer created and the observer registered are one and
the same observer. At run time, I register notification
NSAccessibilityWindowCreatedNotification on element AXApplication in the
target application. My app runs without generating any error messages, and
it doesn't crash. It correctly reads the screen and triggers actions in the
target application. But my callback function is never called and I receive
no notifications.
I don't fully understand the Core Foundation run loop stuff, except that
people tell me I should be able to use it because Cocoa's runloop sits on
top of Core Foundation's run loop. If I understood them correctly, that is.
If I were to try to use Cocoa's NSRunLoop, I think I would have to create a
port, add it to the runloop, and maybe do some other things. Do I have to do
anything like that when using the Core Foundation CFRunLoopAddSource
function?
I did find several references that tell me I have to run the runloop after
adding the source, using CFRunLoopRun(). But if I insert that statement into
my method, my app freezes. It looks to me as if I have to do something very
different to get my Cocoa app to call the C callback function, but I can't
figure out what.
--
Bill Cheeseman - email@hidden
Quechee Software, Quechee, Vermont, USA
http://www.quecheesoftware.com
The AppleScript Sourcebook -
http://www.AppleScriptSourcebook.com
Vermont Recipes -
http://www.stepwise.com/Articles/VermontRecipes
Croquet Club of Vermont -
http://members.valley.net/croquetvermont
_______________________________________________
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.