unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* getting to know the FFI ...
@ 2020-11-20  3:02 Tim Meehan
  2020-11-21 10:19 ` randomlooser
  2020-11-22 14:55 ` Matt Wette
  0 siblings, 2 replies; 3+ messages in thread
From: Tim Meehan @ 2020-11-20  3:02 UTC (permalink / raw)
  To: guile-user

I figured that I would try and do something simple-ish to see how well I
understood the FFI. I found this GTK tutorial, written in Chez Scheme:
https://github.com/jhidding/lyonesse/blob/master/gtk-tutorial/window.scm
I just tried to replace the Chez FFI calls with Guile FFI calls.

I'm not sure how to tell GTK about a callback that is written in Guile.
I'm not sure how to pass a string to GTK ...

Cheers,
My bug-ridden source follows:

; The original code was in Chez Scheme, link below:
; https://github.com/jhidding/lyonesse/blob/master/gtk-tutorial/window.scm

; I gather that it just pops up a window?
; He didn't connect the "X" to the destroy action,
; so the window might be hard to get rid of.

(use-modules (system foreign))

(define libgtk (dynamic-link "libgtk-3"))
(define libgdk (dynamic-link "libgdk-3"))
(define libgio (dynamic-link "libgio-2.0"))
(define libgobject (dynamic-link "libgobject-2.0"))

;; GtkApplication* -> GtkWidget*
(define gtk-application-window-new
    (pointer->procedure
        '*
        (dynamic-func "gtk_application_window_new" libgtk)
        (list '*)))


;; GtkWindow*, gchar* -> void
(define gtk-window-set-title
    (pointer->procedure
        void
        (dynamic-func "gtk_window_set_title" libgtk)
        (list '* '*)))


(define gtk-window-set-default-size
    (pointer->procedure
        void
        (dynamic-func "gtk_window_set_default_size" libgtk)
        (list '*)))


;; GtkWidget* -> void
(define gtk-widget-show-all
    (pointer->procedure
        void
        (dynamic-func "gtk_widget_show_all" libgtk)
        (list '*)))


;; gchar*, GApplicationFlags -> GtkApplication*
(define gtk-application-new
    (pointer->procedure
        '*
        (dynamic-func "gtk_application_new" libgtk)
        (list '* int))) ; FIXME guess


;; GApplication*, int, char ** -> int
(define g-application-run
    (pointer->procedure
        int
        (dynamic-func "g_application_run" libgio)
        (list '* int '*)))


;; gpointer, gchar*, GCallback, gpointer, GConnectFlags -> gulong
(define g-signal-connect-object
    (pointer->procedure
        unsigned-long
        (dynamic-func "g_signal_connect_object" libgobject)
        (list '* '* '* '* int)))


;; gpointer -> void
(define g-object-unref
    (pointer->procedure
        void
        (dynamic-func "g_object_unref" libgobject)
        (list '*)))


(define (activate gtk-app user-data)
    (let ([window (gtk-application-window-new gtk-app)])
        (gtk-window-set-title window "Example Window")
        (gtk-window-set-default-size window 200 200)
        (gtk-widget-show-all window)))


;; HELP: This obviously won't work.
;; How would I give GTK a Guile callback?
(define (callback p)
    (let ([code (foreign-callable p (iptr iptr) void)])
        (lock-object code)
        (foreign-callable-entry-point code)))


;; HELP: how do you pass a string from Guile to a C function?
(define (main)
    (let ([argc (length (command-line))]
          [argv (command-line)]
          [app (gtk-application-new (scm->pointer "what needs this
anyway?") 0)])
        (g-signal-connect-object app "activate" (callback activate) 0 0)
        (g-application-run app 0 0)
        (g-object-unref app)))

(main)


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-11-22 14:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-20  3:02 getting to know the FFI Tim Meehan
2020-11-21 10:19 ` randomlooser
2020-11-22 14:55 ` Matt Wette

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).