From: Steve Smith
Subject: GTK/CFFI trivial demo
Date: 
Message-ID: <8764px5y9d.fsf@lucretia.remote.isay.com.au>
Hi,

I was playing around with CFFI on SBCL today and monkeyed-up the code
below as a quick comparison to the recent ECL/Qt demo. I had
originally intended to run it under ECL too but the CFFI backend
appears to be broken at the moment (missing utils.c I think).

Some thoughts from doing this:

 * It would be nice to be able to automatically map to C enums rather
   than redefining them, which may lead to future breakage.  I cheated
   below.

 * The bindings below could probably be generated by Verazanno, but
   doing it by hand is actually pretty painless.

 * If I was building a complete CFFI/GTK API I'd want to map it to
   CLOS anyway, probably with a similar API to PyGtk.

Anyway, HTH.

Cheers,
Steve

;; ;; ;; ;; ;; ;; ;;

(require 'cffi)
(use-package :cffi)

;; GTK2 interface mapping

(load-foreign-library "libgtk-x11-2.0.so")

(defcfun ("gtk_init" gtk-init) :void
  (argc :pointer)
  (argv :pointer))

(defcfun ("gtk_window_new" gtk-window-new) :pointer
  (wtype :int))

(defcfun ("gtk_widget_show" gtk-widget-show) :void
  (widget :pointer))

(defcfun ("gtk_widget_show_all" gtk-widget-show-all) :void
  (widget :pointer))

(defcfun ("gtk_main" gtk-main) :void)

(defcfun ("gtk_main_quit" gtk-main-quit) :void)

(defcfun ("gtk_container_set_border_width" gtk-container-set-border-width) :void
  (widget :pointer)
  (width :int))

(defcfun ("gtk_button_new_with_label" gtk-button-new-with-label) :pointer
  (label :string))

(defcfun ("gtk_container_add" gtk-container-add) :void
  (container :pointer) 
  (button :pointer))

(defcfun ("g_signal_connect_data" g-signal-connect-data) :void
  (instance :pointer)
  (sig :string)
  (callback :pointer)
  (data :pointer)
  (gclosurenotify :pointer)
  (gconnectflags :int))

; This is a macro in gobject/gsignal.h
(defun g-signal-connect (instance sig callback data)
  (g-signal-connect-data instance sig callback data (null-pointer) 0))


;; GTK tests

(defcallback delete-event :int 
    ((widget :pointer)
     (event :pointer)
     (data :pointer))
  (format t "Got delete event, quitting~%")
  (gtk-main-quit)
  0)

(defcallback click-event :void
    ((widget :pointer)
     (event :pointer)
     (data :pointer))
  (format t "Got click!~%"))

(defun gtk-button-test ()
  (with-foreign-object (win :pointer)
      (gtk-init (make-pointer 0) (make-pointer 0))

      (setf win (gtk-window-new 0)) ; FIXME: 0 -> GTK_WINDOW_TOPLEVEL
      (g-signal-connect win "delete_event" (callback delete-event)
      (null-pointer))

      (gtk-container-set-border-width win 10)

      (with-foreign-object (button :pointer)
        (setf button (gtk-button-new-with-label "Press Me"))
        (g-signal-connect button "clicked" (callback click-event)
        (null-pointer))
        (gtk-container-add win button))

      (gtk-widget-show-all win)
      (gtk-main)))

From: Steve Smith
Subject: Re: GTK/CFFI trivial demo
Date: 
Message-ID: <87oe3o2tj7.fsf@lucretia.remote.isay.com.au>
"Steve" == Steve Smith <·····@internode.on.net> writes:
> I had originally intended to run it under ECL too but the CFFI
> backend appears to be broken at the moment (missing utils.c I
> think).

Never-mind, that was my fault.  I've now confirmed that the code works
unmodified (apart from loading CFFI with ASDF) on ECL-cvs and Clisp
2.36.  Great work from the CFFI guys.

Cheers,
Steve
From: Kenny Tilton
Subject: Re: GTK/CFFI trivial demo
Date: 
Message-ID: <_2Rmf.5858$Ys4.2365@twister.nyc.rr.com>
Steve Smith wrote:
> "Steve" == Steve Smith <·····@internode.on.net> writes:
> 
>>I had originally intended to run it under ECL too but the CFFI
>>backend appears to be broken at the moment (missing utils.c I
>>think).
> 
> 
> Never-mind, that was my fault.  I've now confirmed that the code works
> unmodified (apart from loading CFFI with ASDF) on ECL-cvs and Clisp
> 2.36.  Great work from the CFFI guys.

Yep. we need the next release of Verrazano (due RSN) and a SWIG def 
parser and just maybe every C/C++ library is also a Lisp library with 
negligible effort per lib.

Any C++ geniuses should hie ye to the c-l.net Fetter list and see if 
Rayiner needs help with vzn.

kt
From: Steve Smith
Subject: Re: GTK/CFFI trivial demo
Date: 
Message-ID: <8764pvetgb.fsf@lucretia.remote.isay.com.au>
"Kenny" == Kenny Tilton <·············@nyc.rr.com> writes:
> Yep. we need the next release of Verrazano (due RSN) and a SWIG def
> parser and just maybe every C/C++ library is also a Lisp library
> with negligible effort per lib.

Incidentally, how does Verrazano handle C++ -> CLOS mappings?
Esp. with CFFI as a backend, which doesn't seem to support C++ calls?

Cheers,
Steve
From: Kenny Tilton
Subject: Re: GTK/CFFI trivial demo
Date: 
Message-ID: <%9Wmf.6958$Ys4.5487@twister.nyc.rr.com>
Steve Smith wrote:
> "Kenny" == Kenny Tilton <·············@nyc.rr.com> writes:
> 
>>Yep. we need the next release of Verrazano (due RSN) and a SWIG def
>>parser and just maybe every C/C++ library is also a Lisp library
>>with negligible effort per lib.
> 
> 
> Incidentally, how does Verrazano handle C++ -> CLOS mappings?
> Esp. with CFFI as a backend, which doesn't seem to support C++ calls?
> 
> Cheers,
> Steve

CFFI is OK. Vzn emits C++-compiler specific glue to handle calls. I am 
still waiting for VC++ support so I can play with it. I suggest you 
check with Rayiner on the c-l.net fetter-devel mailing list for any more 
deets -- it is his baby.

kenny