Blocks Handlers Context Objects in LCB

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Blocks Handlers Context Objects in LCB

Post by PaulDaMacMan » Thu Mar 26, 2020 6:00 pm

I'm trying to set up some callback handlers in LCB/FFI ... some of these things are looking to receive a context object pointer. I'm not exactly sure what this means. I've looked it up on the web and in several books about Objective C and it's still not clear to me what this is looking for to have passed as the object that this pointer points to. Would this always be the object that represents the LiveCode Engine, like as retrieved by NSApplication? Or is it the CFRunLoop of the calling thread? OR would it point to the function that invoked the callback? If it's the later, then how do I get a pointer to the invoking handler's "self" to pass?
Most importantly does anyone know or understand what I'm asking? Am I hopelessly confused?
I think I need to understand this in order to setup 2 to 3 callbacks blocks that I need to get my CoreMIDI LCB Library to a v1.0.

Here is an example of what I'm talking about in C:

Code: Select all

void setupMIDI(MyMIDIPlayer *player) {
	MIDIClientRef client;
	CheckError (MIDIClientCreate(CFSTR("Core MIDI to System Sounds Demo"),
						    MyMIDINotifyProc, player, &client),
			 "Couldn't create MIDI client");
That 3rd parameter,"player", is what I'm unclear about, but it should be a "refCon", reference context, which is "a context/user info pointer to send to the callback", that I think I need to pass around for callbacks. I already have this translated this to LCB and can pass null "nothing" in parameter 2 and 3 if I don't need a callback, which works for my currently send-only lib. Now I'm trying to setup the second parameter, MyMIDINotifyProc, which is a block pointer to my callback handler.

The callback handler should receive data as a Struct (which I've setup with "MCAggregateType="Jj", MIDINotificationMessageID is SInt32 and messageSize is UInt32), and the "refCon" pointer that I'm trying to understand what exactly that should be.
In C the callback looks like this:

Code: Select all

void MyMIDINotifyProc (const MIDINotification *message, void *refCon) { 
       printf("MIDI Notify, messageId=%d,", message->messageID);
}
if I pass a null pointer as the refCon then LiveCode Engine crashes hard whenever the callback is triggered (but at least I know the callback is being invoked). It crashes so hard that the Apple Bug reporter doesn't even come up with the window with the button to "Send (the crash log) to Apple"!

Any help would be very much appreciated.
T.I.A.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

Post Reply

Return to “LiveCode Builder”