[plt-scheme] Interacting with win32 Event Loops

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Thu Jun 26 21:41:27 EDT 2008

At Thu, 26 Jun 2008 13:17:18 -0400, "Christopher Bowron" wrote:
> I'm writing a couple extensions in C to get to some lower level platform
> features using mzc, and I'm having trouble figuring out good ways to achieve
> my goals.
> 
> I want to be able to interact with some win32 features including console
> lock notifications and hotkey notifications.
> 
> The problem is the notifications come in through the window message loop,
> and I don't know a good way to get that information back into the scheme
> environment.
> 
> I've got it working for the console locks using the following method:
> 
> the scheme_initialize() function starts a thread that creates a hidden
> dialog box with an event handler that toggles a global variable when the
> console gets locked or unlocked.  The scheme_initialize() functions also
> installs a function just checks the value of this global variable and
> returns scheme_false or scheme_true.  Then inside the scheme environment I
> basically poll on that function.

That's the first thing I'd try too.

I'd probably set up an sychronizable event instead of polling. To
implement the event, use scheme_add_evt(). The message-loop thread
would need post to a Windows sempahore; polling the event would poll
the semeaphore, and the event would implemented the need-wakeup
function by registering the semaphore via scheme_add_fd_handle().

> I would like to add some global hotkeys which I would also get notifications
> of in the same event loop as the console lock notifications.
> 
> My current thinking is to create an input port in the C code that blocks
> until a hot key message is processed in the event loop, and from the scheme
> environment launch a thread that loops while reading in from that port.
> 
> There might be a better way to do this, and I'm not sure how to trigger the
> input port when a message does get processed if I do go with this route.

In principle, you don't need a whole port --- just a synchronizable
event as above. When the event is ready and synchronized, it can return
specific data. Unfortunately, I see that the return-a-value part of the
event API isn't exposed to extensions.

Still, you can use your extension-implemented object on the Scheme side
as an event to determine when data is available, and then use a
separate operation (also provided by the extension) to extract data
from the object.

> My other thought is that this is inside a MrEd frame based program.  If
> there were a way to extend the message loop of the MrEd frame itself, it
> would also be possible to just register the hotkey with the MrEd frame's
> HWND from (send frame get-handle).

You can get the HWND of a `widow<%>' object through the `get-handle'
method.


Matthew



Posted on the users mailing list.