From: Frank Buss
Subject: OpenMCL and event handling with SDL
Date: 
Message-ID: <1wuoz45zms6hg.otndo6ebxks2$.dlg@40tude.net>
My program doesn't receive mouse events when started in OpenMCL 1.0 with
CFFI and SDL 1.2.9 on MacOS X 10.4. This is a step-by-step manual how to
install everything and how to reproduce the error:

http://www.libsdl.org/release/SDL-1.2.9.dmg
http://openmcl.clozure.com/Distributions/full-archive-for-32_002dbit-DarwinPPC_002fMacOS-X-_005bopenmcl_002ddarwinppc_002dall_002d1_005f0_005ftar_005fgz_005d.html#full-archive-for-32_002dbit-DarwinPPC_002fMacOS-X-_005bopenmcl_002ddarwinppc_002dall_002d1_005f0_005ftar_005fgz_005d

- download the file openmcl-darwinppc-all-1.0.tar.gz from 
http://openmcl.clozure.com/Distributions/Download_005f1_005f0.html

- start "terminal" program (e.g. in Finder: Apple-F->search for
"Terminal"->double click on Terminal; add a shortcut to your desktop, if
you like)

- unpack OpenMCL in a termimal windows, e.g.: "tar -xvf
/Users/fb/Desktop/openmcl-darwinppc-all-1.0.tar"

- download SDL-1.2.9.dmg:
http://www.libsdl.org/release/SDL-1.2.9.dmg

- double click on SDL-1.2.9.dmg: a SDL drive appears on your desktop. Open
it and copy the Folder SDL.Framework to /Library/Frameworks/, e.g. with
"cp -R /Volumes/SDL/SDL.framework /Library/Frameworks/" (you must be login
as root user with "su" from the terminal before. If you don't have root
access, you can install it in some local directory and then change the path
later when loading the library)

- download CFFI: 
http://common-lisp.net/project/cffi/tarballs/cffi-060228.tar.gz

- install it:
tar -xvf /Users/fb/Desktop/cffi-060228.tar 
mv cffi-060228 ccl/cffi

- download lispbuilder-sdl:
http://prdownloads.sourceforge.net/lispbuilder/lispbuilder-sdl-0.5.zip?download

- install it:
mv /Users/fb/Desktop/lispbuilder-sdl/ ccl

- edit sdl_random_rects.lisp: delete the line (load-sdl-library) in the
random-rects2 function

- edit lispbuilder-sdl/sdl_drawing.lisp and delete the line
(cffi:load-foreign-library "sdl.dll") in the mouse-painter function 

- start OpenMCL from the terminal (double click on OpenMCL or dppcl didn't
work on my installation):
cd ccl
./dppccl 

- start the cocoa REPL (without it it crashed when trying to start the SDL
programs on my installation):
(require "cocoa")

- in the cocoa REPL, enter the following:
(load "tools/asdf")
(push "cffi/" asdf:*central-registry*)
(asdf:operate 'asdf:load-op :cffi)
(push "lispbuilder-sdl/" asdf:*central-registry*)
(cffi:load-foreign-library "/Library/Frameworks/SDL.framework/SDL")
(asdf:operate 'asdf:load-op :lispbuilder-sdl)
(load "lispbuilder-sdl/sdl_random_rects")
(lispbuilder-sdl::random-rects2)

now you should see a window with some colored rects. This is not
Plug-And-Play, like normal Mac programs, but when the lispbuilder-sdl
library is more mature, we'll pack all to a single installable file.

Close it and start the drawing program example:

(load "lispbuilder-sdl/sdl_drawing")
(lispbuilder-sdl::mouse-painter)

For Windows (see 
http://svn.sourceforge.net/viewcvs.cgi/*checkout*/lispbuilder/trunk/lispbuilder-sdl/index.html
for a description how to install it in Windows with CLISP) it works and you
can draw lines, when holding down the mouse and moving the mouse. On Mac it
doesn't work. Any ideas why not or how I can debug it? I'm a newbie on Mac.

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de

From: Luís Oliveira
Subject: Re: OpenMCL and event handling with SDL
Date: 
Message-ID: <m264mr4f7b.fsf@deadspam.com>
Frank Buss <··@frank-buss.de> writes:
> My program doesn't receive mouse events when started in OpenMCL 1.0 with

I don't know much about this but I've been told that OSX is picky
about which thread is handling events. Only the initial thread can
handle events and OpenMCL does *not* run its REPL in the initial
thread.

Here's some ugly (untested) code to run an event loop (or whatever) in
the initial thread:

#+openmcl
(defun run-something-in-initial-thread (function)
  (ccl:process-interrupt
   ccl::*initial-process*
   (lambda ()
     (ccl:%set-toplevel (lambda ()
                          (ccl:%set-toplevel nil)
                          (funcall function)))
     (ccl:toplevel))))

HTH

-- 
Luís Oliveira
luismbo (@) gmail (.) com
Equipa Portuguesa do Translation Project
http://www.iro.umontreal.ca/translation/registry.cgi?team=pt
From: Frank Buss
Subject: Re: OpenMCL and event handling with SDL
Date: 
Message-ID: <sa00jx4ym2te$.1ga69dg1p90gm$.dlg@40tude.net>
Lu�s Oliveira wrote:

> I don't know much about this but I've been told that OSX is picky
> about which thread is handling events. Only the initial thread can
> handle events and OpenMCL does *not* run its REPL in the initial
> thread.

Thanks, your run-something-in-initial-thread function works! When starting
the drawing program like this:

(run-something-in-initial-thread #'lispbuilder-sdl::mouse-painter)

First the OpenMCL kernel debugger says

? 2006-03-06 19:21:37.927 dppccl[272] *** Exception handlers were not
properly removed. Some code has jumped or returned out of an
NS_DURING...NS_HANDLER region without using the NS_VOIDRETURN or
NS_VALUERETURN macros.
Unhandled exception 11 at 0x9075c834, context->regs
at #xbffff4c8
Read operation to unmapped address 0x0

but when ignoring it with the "Advance" command, I was able to draw with
the mouse. This is a good starting point, when I have some more time to
make a clean solution with your idea.

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Luís Oliveira
Subject: Re: OpenMCL and event handling with SDL
Date: 
Message-ID: <m2hd68ej7r.fsf@deadspam.com>
Frank Buss <··@frank-buss.de> writes:
> ? 2006-03-06 19:21:37.927 dppccl[272] *** Exception handlers were not
> properly removed. Some code has jumped or returned out of an
> NS_DURING...NS_HANDLER region without using the NS_VOIDRETURN or
> NS_VALUERETURN macros.

What about this slightly different version?

(defun run-in-initial-thread (function)
  (flet ((start ()
           (ccl:%set-toplevel nil)
           (funcall function)))
    (ccl:process-interrupt ccl::*initial-process*
                           (lambda ()
                             (ccl:%set-toplevel #'start)
                             (ccl:toplevel)))))

-- 
Luís Oliveira
luismbo (@) gmail (.) com
Equipa Portuguesa do Translation Project
http://www.iro.umontreal.ca/translation/registry.cgi?team=pt