Calling sub from XS via IPC: Tkx functions don't run

[email protected] (Kevin Walzer) Wed, 03 Jun 2015 10:15:02 -0400
Newsgroups perl.tcltk
Organization WordTech Communications LLC
Message-ID <[email protected]>
(Sorry if this is a double-post, I sent first to the newsgroup rather 
than the mailing list).

As I've mentioned before on the list, I'm working on a Perl extension 
that allows integration between a Perl app and other Mac apps via the 
Apple Events IPC mechanism. There is an older Perl package (Mac::Carbon) 
that provides such functionality among its other methods, but much of it 
is obsolete and/or will no longer build, and the author has not updated 
the package for years.

I have made excellent progress and have gotten the C side pretty much 
worked out: I can send basic commands via the Apple Event/AppleScript 
mechanism to my Perl/Tkx app, have the app run a command, and return 
output via the Apple Event pipeline. I am running into an unexpected 
difficulty, however. The commands that I am exposing involve a 
combination of data processing and also updating the UI of my 
application. The data side works, but the UI (which is controlled by the 
Tkx event loop) does not respond.

Here's a brief example:

sub scriptSetDir {
     my $dname = shift;
     &selectFileSystem("$dname");
     return "Set directory as $dname\n";

}

This command is supposed to grab a directory name as the command 
parameter, sets a variable to that value, run the internal 
"selectFileSystem" command that updates the app UI with the contents of 
the directory, and the returns the command result to the external IPC 
mechanism. The first and the last things happen, but the 
&selectFileSystem call does not fire. It's not clear to me why this is 
the case.

Getting the form of the command name passed to the Apple Events 
mechanism has proven a bit tricky; Perl would complain about "undefined 
subroutine" if I just passed the bare name of the command to the IPC 
mechanism.  Interestingly, it would complain that the name 
"Tkx::i::scriptSetDir" was the command. I'm not sure where the Tkx::i 
namespace came from in this regard. Eventually, this form of mapping the 
command worked:

     Mac::AEM::installeventhandler( "CoKv", "fMdr", "main::scriptSetDir" );

The first two args above are the IPC event codes used by the Apple OS to 
register what events/commands my app will handle. The third, which maps 
the Perl command to the "main::'" namespace, fires the Perl-level data 
processing pieces but not the updates to the Tkx UI.

I suspect there is something going on with the Tkx event loop that 
simply does not receive the message to fire the command; I know that 
from trying to integrate Tkx with other Perl IPC systems that only 
running a Tcl-based socket plays nicely with Tkx. The Apple Event 
mechanism, however, is lower-level and plays nicely with Tk in other 
contexts (I've gotten it to work with Tcl/Tk and Python/Tkinter). 
Because there are so many layers here, it's hard to figure out where to 
debug it. Has anyone else used some kind of IPC mechanism with Perl/Tkx 
successfully, perhaps with COM on Windows or dbus on Linux (the rough 
analogs to Apple Events on other platforms)?

Advice is appreciated.

--Kevin

-- 
Kevin Walzer
Code by Kevin/Mobile Code by Kevin
http://www.codebykevin.com
http://www.wtmobilesoftware.com