From: ·······@gmail.com
Subject: CFFI Question
Date: 
Message-ID: <1174417006.178050.127730@d57g2000hsg.googlegroups.com>
I have a couple questions. I'm trying to write an OpenGL application,
about 95% of it is written in C. the one thing I want to have LISP do
is have a function which acts as a callback in C.


I have this so far in Lisp:
(asdf:oos 'asdf:load-op :cffi)

(defpackage :plasma-test
  (:use :common-lisp :cffi))

(in-package :plasma-test)

(define-foreign-library plasma
  (:unix "plasma.so"))

(use-foreign-library plasma)

(defcfun "init_main" :void)

(defcallback plasma-one :int ((x :int) (y :int))
  (+ 128.0 (* 128.0 (sin (/ x 16.0)))))

Couple of issues is as follows. When I try to load the file (called
plasma.lisp) I get an error that init_main is an undefined alien.
However, I have void init_main() declared and defined within the C
file that I create a shared library from.

 Secondly, because of OpenGL the function void display() is passed as
a function pointer to GlutDisplayFunc. GlutDisplayFunc requires a
function pointer of type void (*fp)(void). However, the code which I
want to set up as a callback to lisp is within display in the form of:
color = (*fp)(x,y) where *fp is defined as int (*fp)(int x, int y). Is
there a way within CFFI to set fp as a Lisp Callback without
explicitly passing in the function pointer as a parameter to display?

From: Luís Oliveira
Subject: Re: CFFI Question
Date: 
Message-ID: <m1ps73nwne.fsf@deadspam.com>
·······@gmail.com writes:
> I have this so far in Lisp:
[...]
> (define-foreign-library plasma
>   (:unix "plasma.so"))
[...]
> Couple of issues is as follows. When I try to load the file (called
> plasma.lisp) I get an error that init_main is an undefined alien.

Unless you have plasma.so in some place where dlopen(3) can find it (see
that manpage for a list of places it searches) that will not work.

So you need to make sure dlopen() will find it or, alternatively, use
CFFI:*FOREIGN-LIBRARY-DIRECTORIES*:
  <http://common-lisp.net/project/cffi/manual/html_node/_002aforeign_002dlibrary_002ddirectories_002a.html>

In any case, assuming plasma.so really does contain init_main, a
LOAD-FOREIGN-LIBRARY-ERROR should have been signaled.  What Lisp are you
using (I'm guessing either CMUCL or SBCL by your description) and what
CFFI version?  My first guess is that you're using CMUCL and an old
version of CFFI.


>  Secondly, because of OpenGL the function void display() is passed as
> a function pointer to GlutDisplayFunc. GlutDisplayFunc requires a
> function pointer of type void (*fp)(void). However, the code which I
> want to set up as a callback to lisp is within display in the form of:
> color = (*fp)(x,y) where *fp is defined as int (*fp)(int x, int y). Is
> there a way within CFFI to set fp as a Lisp Callback without
> explicitly passing in the function pointer as a parameter to display?

You'll have to inform your C code with the callback's address somehow.
I don't really see much of an alternative.

Have you looked at the cl-opengl project btw?
  <http://common-lis.net/project/cl-opengl/>
It might be useful for you.

Finally, I should mention that the best place for CFFI questions is the
cffi-devel mailing list.

-- 
Luís Oliveira
http://student.dei.uc.pt/~lmoliv/
From: ·······@gmail.com
Subject: Re: CFFI Question
Date: 
Message-ID: <1174457026.221354.110020@l75g2000hse.googlegroups.com>
On Mar 20, 10:44 pm, ·············@deadspam.com (Luís Oliveira) wrote:
> ·······@gmail.com writes:
> > I have this so far in Lisp:
> [...]
> > (define-foreign-library plasma
> >   (:unix "plasma.so"))
> [...]
> > Couple of issues is as follows. When I try to load the file (called
> > plasma.lisp) I get an error that init_main is an undefined alien.
>
> Unless you have plasma.so in some place where dlopen(3) can find it (see
> that manpage for a list of places it searches) that will not work.
>
> So you need to make sure dlopen() will find it or, alternatively, use
> CFFI:*FOREIGN-LIBRARY-DIRECTORIES*:
>   <http://common-lisp.net/project/cffi/manual/html_node/_002aforeign_002...>
>
> In any case, assuming plasma.so really does contain init_main, a
> LOAD-FOREIGN-LIBRARY-ERROR should have been signaled.  What Lisp are you
> using (I'm guessing either CMUCL or SBCL by your description) and what
> CFFI version?  My first guess is that you're using CMUCL and an old
> version of CFFI.
>
> >  Secondly, because of OpenGL the function void display() is passed as
> > a function pointer to GlutDisplayFunc. GlutDisplayFunc requires a
> > function pointer of type void (*fp)(void). However, the code which I
> > want to set up as a callback to lisp is within display in the form of:
> > color = (*fp)(x,y) where *fp is defined as int (*fp)(int x, int y). Is
> > there a way within CFFI to set fp as a Lisp Callback without
> > explicitly passing in the function pointer as a parameter to display?
>
> You'll have to inform your C code with the callback's address somehow.
> I don't really see much of an alternative.
>
> Have you looked at the cl-opengl project btw?
>   <http://common-lis.net/project/cl-opengl/>
> It might be useful for you.
>
> Finally, I should mention that the best place for CFFI questions is the
> cffi-devel mailing list.
>
> --
> Luís Oliveirahttp://student.dei.uc.pt/~lmoliv/

Thanks, I greatly appreciate your help. The problem within not finding
init_main was a bad move on my part since I wasn't invokving gcc to
produce the library but using ld. Didn't realize it for some reason.
Thanks for the pointers, I'll take a look at cl-opengl.