mit-scheme-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[MIT-Scheme-devel] FFI and callbacks


From: Matt Birkholz
Subject: [MIT-Scheme-devel] FFI and callbacks
Date: Thu, 3 Sep 2015 14:58:04 -0700

> From: address@hidden
> Date: Sat, 01 Aug 2015 20:22:48 +0200
> 
> [Description of cairo_surface_write_to_png_stream...]
> 
> I'd like to make this accessible from Scheme, preferably by just calling
> it in C, and returning all the data it writes as a vector8 (so a
> string).
> 
> I've not been successful in declaring the callback,

(callback cairo_status_t
          write_to_png
          (ID (* void))
          (data (* (const uchar)))
          (length uint))

> and if I just return a char* from a wrapper function, it will be
> truncated at the first #\nul.

You used c-peek-cstring?  Try c-peek-bytes.

> How can I either implement the callback, so that I get at the data and
> length in a Scheme function, or so that I can read all the incoming data
> in C, and then pass it all back to Scheme correctly?

To do it in Scheme, you would need to do something like this:

(define (cairo-surface-write-to-png-string surface)
  (let ((accumulator '())
        (callback-id))
    (dynamic-wind
     (lambda ()
       (set! callback-id
             (C-callback
              (named-lambda (write-to-png data len)
                (let ((str (make-string len)))
                  (c-peek-bytes data 0 len str 0)
                  (set! accumulator (cons str accumulator))
                  (C-enum "CAIRO_STATUS_SUCCESS"))))))
     (lambda ()
       (let ((status
              (C-call "cairo_surface_write_to_png_stream"
                      surface (C-callback "write_to_png") callback-id)))
         (if (not (eq? status (C-enum "CAIRO_STATUS_SUCCESS")))
             (error "Cairo write-to-png unsuccessful:" status))))
     (lambda ()
       (de-register-c-callback callback-id)))
    (apply string-append accumulator)))

So you see why I used cairo_surface_write_to_png and a temp. file to
put up my tellurion.

    http://birchwood-abbey.net/~matt/Scheme/#Tellurion



reply via email to

[Prev in Thread] Current Thread [Next in Thread]