[plt-scheme] Re: Interacting with win32 Event Loops

From: cwbowron at gmail.com (cwbowron at gmail.com)
Date: Fri Jun 27 14:55:43 EDT 2008

Thanks for the help.  I got it working with the following in case
anyone ever needs to do something similar:

Excerpt of the dialog procedure:

INT_PTR CALLBACK HiddenWindowDialogProc(HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_HOTKEY:
		g_last_hotkey_modifiers 	= LOWORD( lParam );
		g_last_hotkey_virtual_keycode 	= HIWORD( lParam );
		g_hotkey_available = 1;
		ReleaseSemaphore( g_native_semaphore, 1, NULL );
		break;

   /* SNIP */
}

Excerpts from the initialization code:

Scheme_Object* scm_last_hotkey( int argc, Scheme_Object * argv[] )
{
	g_hotkey_available = 0;
	return scheme_make_integer( g_last_hotkey_virtual_keycode );
}

static int scheme_hotkey_inactive( void * data )
{
	return g_hotkey_available;
}

static void scheme_hotkey_needs_wakeup( void * data, void * fds )
{
	scheme_add_fd_handle( g_native_semaphore, fds, 0 );
}

Scheme_Object* scheme_initialize( Scheme_Env * env )
{
	...
	g_native_semaphore = CreateSemaphore( NULL, 0, 500, SEMA_NAME );

	g_hotkey_event_type        = scheme_make_type("<hotkey-event>");
	g_hotkey_event.iso.so.type = g_hotkey_event_type;
	g_hotkey_event.u.int_val   = (long)g_native_semaphore;

	scheme_add_evt( g_hotkey_event_type,
			(Scheme_Ready_Fun) scheme_hotkey_inactive,
			(Scheme_Needs_Wakeup_Fun) scheme_hotkey_needs_wakeup,
			NULL, 0);

	scheme_add_global( "hotkey-event", (Scheme_Object*) &g_hotkey_event,
menv );

	scheme_add_global( "last-hotkey",
	                  scheme_make_prim_w_arity( scm_last_hotkey, "last-
hotkey", 0, 0 ),
	                  menv );
	....
}

Then inside scheme I can use (sync hotkey-event) to block until a
hotkey is pressed and (last-hotkey) to read what key was actually
pressed.

On Jun 26, 9:41 pm, Matthew Flatt <mfl... at cs.utah.edu> wrote:
> 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().


Posted on the users mailing list.