unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Custom foreign types
@ 2024-06-17  2:13 Ryan Raymond
  2024-06-19 22:16 ` Maxime Devos via General Guile related discussions
  2024-06-20  0:11 ` Matt Wette
  0 siblings, 2 replies; 4+ messages in thread
From: Ryan Raymond @ 2024-06-17  2:13 UTC (permalink / raw)
  To: Guile User

Hello, all.
I know we can create pointers to structs with make-c-struct, but I would
like to pass structs directly as arguments to foreign functions. I can do
that by using bytevector-uint-ref, and setting the size to that of the
struct, but when it is time to specify the type in pointer->procedure, I am
out of luck if the sizes don't match.

Here is how I am binding Raylib using FFI currently.

(use-modules
  (rnrs bytevectors)
  (system foreign))
(define rl (dynamic-link "libraylib"))
(define pp pointer->procedure)
(define df dynamic-func)

(define BeginDrawing (pp void (df "BeginDrawing" rl) '()))
(define ClearBackground_c (pp void (df "ClearBackground" rl) (list uint32)))
(define (ClearBackground color)
  (ClearBackground_c (make-c-struct (list uint8 uint8 uint8 uint8) color)))
(define CloseWindow (pp void (df "CloseWindow" rl) '()))
(define DrawGrid (pp void (df "DrawPlane" rl) (list int float)))
(define EndDrawing (pp void (df "EndDrawing" rl) '()))
(define GetFrameTime (pp float (df "GetFrameTime" rl) '()))
(define InitWindow_c (pp void (df "InitWindow" rl) (list int int '*)))
(define (InitWindow width height title) (InitWindow_c width height
(string->pointer title)))
(define SetTargetFPS (pp void (df "SetTargetFPS" rl) (list int)))
(define WindowShouldClose_c (pp int8 (df "WindowShouldClose" rl) '()))
(define (WindowShouldClose) (= 1 (WindowShouldClose_c)))

(define width 600)
(define height 400)
(define title "This is a Guile-bound thinga")

(define (make-Color r g b a)
  (bytevector-uint-ref
    (u8-list->bytevector (list r g b a)) 0 (native-endianness) 4))

(define (make-Vector3 x y z)
  (bytevector-uint-ref
    (f32vector x y z) 0 (native-endianness) 4))


Is there a cleaner way to do this?
Ryan


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

* RE: Custom foreign types
  2024-06-17  2:13 Custom foreign types Ryan Raymond
@ 2024-06-19 22:16 ` Maxime Devos via General Guile related discussions
  2024-06-20  0:11 ` Matt Wette
  1 sibling, 0 replies; 4+ messages in thread
From: Maxime Devos via General Guile related discussions @ 2024-06-19 22:16 UTC (permalink / raw)
  To: Ryan Raymond, Guile User

>Hello, all.

>I know we can create pointers to structs with make-c-struct, but I would
like to pass structs directly as arguments to foreign functions. I can do
that by using bytevector-uint-ref, and setting the size to that of the
struct, but when it is time to specify the type in pointer->procedure, I am
out of luck if the sizes don't match.

Instead of searching for a type of the same size of the struct, why not pass the struct type and struct directly to Guile? From the manual:
>One may also pass structs as values, passing structs as foreign pointers. See Foreign Structs, for more information on how to express struct types and struct values.
I’m not really clear on what the precise API is for setting struct arguments and whether I interpreted that sentence correctly(*), but ...

> Is there a cleaner way to do this?

... I think the answer is ‘yes’ , though I don’t know what this cleaner way is.
You may want to look at the implementation for clarity (and if you find the answer, an addition to the manual would be appreciated).

Best regards,
Maxime Devos.

(*) I think what it means is that as type you set (list this-field-type that-field-type ...) and as ‘Scheme value’ you set a foreign pointer that contains the struct. If it doesn’t mean that, you could try letting the value be (list this-field-value that-field-value ...).


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

* Re: Custom foreign types
  2024-06-17  2:13 Custom foreign types Ryan Raymond
  2024-06-19 22:16 ` Maxime Devos via General Guile related discussions
@ 2024-06-20  0:11 ` Matt Wette
  2024-06-20 13:17   ` Matt Wette
  1 sibling, 1 reply; 4+ messages in thread
From: Matt Wette @ 2024-06-20  0:11 UTC (permalink / raw)
  To: guile-user

On 6/16/24 7:13 PM, Ryan Raymond wrote:
> Hello, all.
> I know we can create pointers to structs with make-c-struct, but I would
> like to pass structs directly as arguments to foreign functions. I can do
> that by using bytevector-uint-ref, and setting the size to that of the
> struct, but when it is time to specify the type in pointer->procedure, I am
> out of luck if the sizes don't match.
>
> Here is how I am binding Raylib using FFI currently.
>
> (use-modules
>    (rnrs bytevectors)
>    (system foreign))
> (define rl (dynamic-link "libraylib"))
> (define pp pointer->procedure)
> (define df dynamic-func)
>
> (define BeginDrawing (pp void (df "BeginDrawing" rl) '()))
> (define ClearBackground_c (pp void (df "ClearBackground" rl) (list uint32)))
> (define (ClearBackground color)
>    (ClearBackground_c (make-c-struct (list uint8 uint8 uint8 uint8) color)))
> (define CloseWindow (pp void (df "CloseWindow" rl) '()))
> (define DrawGrid (pp void (df "DrawPlane" rl) (list int float)))
> (define EndDrawing (pp void (df "EndDrawing" rl) '()))
> (define GetFrameTime (pp float (df "GetFrameTime" rl) '()))
> (define InitWindow_c (pp void (df "InitWindow" rl) (list int int '*)))
> (define (InitWindow width height title) (InitWindow_c width height
> (string->pointer title)))
> (define SetTargetFPS (pp void (df "SetTargetFPS" rl) (list int)))
> (define WindowShouldClose_c (pp int8 (df "WindowShouldClose" rl) '()))
> (define (WindowShouldClose) (= 1 (WindowShouldClose_c)))
>
> (define width 600)
> (define height 400)
> (define title "This is a Guile-bound thinga")
>
> (define (make-Color r g b a)
>    (bytevector-uint-ref
>      (u8-list->bytevector (list r g b a)) 0 (native-endianness) 4))
>
> (define (make-Vector3 x y z)
>    (bytevector-uint-ref
>      (f32vector x y z) 0 (native-endianness) 4))
>
>
> Is there a cleaner way to do this?
> Ryan

Since Color is a struct of four chars you need to declare ClearBackground as
  (pp void (df "ClearBackground" rl)  (list (list char char char char)))

Matt






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

* Re: Custom foreign types
  2024-06-20  0:11 ` Matt Wette
@ 2024-06-20 13:17   ` Matt Wette
  0 siblings, 0 replies; 4+ messages in thread
From: Matt Wette @ 2024-06-20 13:17 UTC (permalink / raw)
  To: guile-user



On 6/19/24 5:11 PM, Matt Wette wrote:
> On 6/16/24 7:13 PM, Ryan Raymond wrote:
>> Hello, all.
>> I know we can create pointers to structs with make-c-struct, but I would
>> like to pass structs directly as arguments to foreign functions. I 
>> can do
>> that by using bytevector-uint-ref, and setting the size to that of the
>> struct, but when it is time to specify the type in 
>> pointer->procedure, I am
>> out of luck if the sizes don't match.
>>
>> Here is how I am binding Raylib using FFI currently.

>>
>> Is there a cleaner way to do this?
>> Ryan
>

I should also mention the nyacc FFI Helper tool I wrote.  You give it 
the header file and
it will generate the ffi code.   Look here: 
https://www.nongnu.org/nyacc/nyacc-fh-ug.html

Matt





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

end of thread, other threads:[~2024-06-20 13:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-17  2:13 Custom foreign types Ryan Raymond
2024-06-19 22:16 ` Maxime Devos via General Guile related discussions
2024-06-20  0:11 ` Matt Wette
2024-06-20 13:17   ` 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).