From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ryan Raymond Newsgroups: gmane.lisp.guile.user Subject: Custom foreign types Date: Sun, 16 Jun 2024 22:13:24 -0400 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="7802"; mail-complaints-to="usenet@ciao.gmane.io" To: Guile User Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Mon Jun 17 04:14:27 2024 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sJ1t5-0001rg-J2 for guile-user@m.gmane-mx.org; Mon, 17 Jun 2024 04:14:27 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sJ1sR-00086l-La; Sun, 16 Jun 2024 22:13:47 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sJ1sQ-00086V-7J for guile-user@gnu.org; Sun, 16 Jun 2024 22:13:46 -0400 Original-Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sJ1sO-0001lO-IZ for guile-user@gnu.org; Sun, 16 Jun 2024 22:13:45 -0400 Original-Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-2c327a0d4c9so3569488a91.1 for ; Sun, 16 Jun 2024 19:13:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oakland.edu; s=google; t=1718590421; x=1719195221; darn=gnu.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=qs1LwdhyIY2NhE9RoQBGk0E7Nxo+OYfY0vh1AB5QNKU=; b=XmDPs1YePUMNG0piTBD0sWM2nkYVXzszmsTtOK+YrwZuqZKo1DmDbsiJ4HZRUTB9V8 iOMYckN6wYpN/gekxmatHYf2TKAMIOWh4N7T+C4Pj2C2dhTObl9jWYdJPYyeHQvY3kwm bvPDi8XPEquHVQ1Gay4FlZCBGU+xRSfKHuHkfnK5xo1DmOLbzhgrLzVACK0iPIEzQt0Q CCkZEij19sDX/ZLfuIWuBeKwtXYiEzibgFkMKi9mbs2uYC2ZI1nPAPneYsGEHIOTags1 qIBes8HuOF69Gs/37rSDFVy6/1YcC2MAa/Xg4H04tnHtil9FSzBYjCyHkYh6s63rBxN3 TJ1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718590421; x=1719195221; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=qs1LwdhyIY2NhE9RoQBGk0E7Nxo+OYfY0vh1AB5QNKU=; b=Ax7i3VGd8jrkEQ3qV95fzu0TVp9it/O5iH36I/Rtdvy8qul8GPdMtLPUJXcweC8tN/ nxR1fqBadHSnfwM8S6F77HhLEfwlzV9Sapw4hSlGbO3xZgOmOXybZQ67gfjfhUGz9u9z qm8CLHAQuNyqe75Wru8g5GNoXqwr/GNoPd/o/BmFBKtYPSg0Ci/hyqZfBGxc0sBdUD+n i2isv9Iid3iT8xodQlIpYnq7HXQUSYc6hn+y2gqO1Lc70x/siHVLIlaix/wBHr5Fx0Uy iX2luJ1E6/mNi8pgcpP0pSSOBt+x5Wz2/27lg7T7GhEEhYEr51xuCtpzkETQvAt5gSRv qKhw== X-Gm-Message-State: AOJu0YywfytEtf+URBHwFMJG4qqLkaf7LWMB82Or1sHVuwOvACpgJi4L myVCPqNV1dV1HtezmkmNemhv5sDAF7FpH+djAs4tPApf6YXwvULHjOZIdoZlMMgsDqGcMCyyFwM 9kHRWELv1hlO1edLaWEXfOEIbcy+j3coCqrZqVvm7kJ+mV8HlIA== X-Google-Smtp-Source: AGHT+IFRqM2T7VKtWzGEDL7be5kCXreo1+NfCm7cVj8kfqVCQa7VJ4qJ8TrSOoi0b8/0Oxb9K61RoLoa4j8OKKAVwX4= X-Received: by 2002:a17:90b:1c02:b0:2c2:db4e:16eb with SMTP id 98e67ed59e1d1-2c4da54259amr13814340a91.7.1718590420682; Sun, 16 Jun 2024 19:13:40 -0700 (PDT) Received-SPF: pass client-ip=2607:f8b0:4864:20::1036; envelope-from=rjraymond@oakland.edu; helo=mail-pj1-x1036.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.user:19719 Archived-At: 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