Lisp HUG Maillist Archive

define-foreign-callable

define-foreign-callable
Hello,

We would need  a C function that, after receiving a MIDI event
from some external MIDI device,
would in turn call a Lisp function to notify LW that the system has
received a MIDI event. After reading the FLI manual we tried to
solve this problem by using the LW 'define-foreign-callable' macro.

To simulate this behavior in C code we tried this :

int notify_lisp(int x)
{
 return(x);
}

int receive_midi(int x)
{
 notify_lisp(x);
 return(0);
}

The foreign-callable LW code in turn is defined like this:

(fli:define-foreign-callable
    ("notify_lisp" :result-type :int)
    ((in-arg :int))  (print (format t "MIDI received ~a" in-arg)))

To test this from LW we do the following:

(fli:define-foreign-function (test-midi-info "
receive_midi")
    ((in-arg :int)) :result-type :int)


But when calling:

(test-midi-info 60)

'test-midi-info' returns only 0 and does not call the 'notify_lisp' function in Lisp as we hoped.
Probably the C function 'receive_midi' only calls the 'notify_lisp' that is defined in C.
How can we force the C function 'receive_midi' to call the
foreign-callable function 'notify_lisp' that would print the MIDI message in LW?
Do we need to deal with function pointers in C, and if this is the case,
how can we pass a function pointer of a foreign-callable  from Lisp to C?

We are using LWM 4.3.7.

Any help appreciated.

Mikael

Re: define-foreign-callable

On Fri, 19 Nov 2004 18:29:35 +0200, Mikael Laurson <laurson@siba.fi> wrote:

> We would need a C function that, after receiving a MIDI event from
> some external MIDI device, would in turn call a Lisp function to
> notify LW that the system has received a MIDI event. After reading
> the FLI manual we tried to solve this problem by using the LW
> 'define-foreign-callable' macro.
>
> To simulate this behavior in C code we tried this :
>
> int notify_lisp(int x)
> {
>   return(x);
> }
>
> int receive_midi(int x)
> {
>   notify_lisp(x);
>   return(0);
> }
>
> The foreign-callable LW code in turn is defined like this:
>
> (fli:define-foreign-callable
>      ("notify_lisp" :result-type :int)
>      ((in-arg :int))  (print (format t "MIDI received ~a" in-arg)))
>
> To test this from LW we do the following:
>
> (fli:define-foreign-function (test-midi-info "receive_midi")
>      ((in-arg :int)) :result-type :int)
>
>
> But when calling:
>
> (test-midi-info 60)
>
> 'test-midi-info' returns only 0 and does not call the 'notify_lisp'
> function in Lisp as we hoped.  Probably the C function
> 'receive_midi' only calls the 'notify_lisp' that is defined in C.

Yes.

> How can we force the C function 'receive_midi' to call the
> foreign-callable function 'notify_lisp' that would print the MIDI
> message in LW?  Do we need to deal with function pointers in C, and
> if this is the case, how can we pass a function pointer of a
> foreign-callable from Lisp to C?

See here:

  <http://www1.xanalys.com/support/lisp/kbase.nsf/51fe6e1cdfe748a180256639005a2ba9/404d1bad7fe771f3802568c400540b50?OpenDocument>

Edi.


Updated at: 2020-12-10 08:54 UTC