From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Matt Wette Newsgroups: gmane.lisp.guile.user Subject: Re: Custom foreign types Date: Wed, 19 Jun 2024 17:11:23 -0700 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19586"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla Thunderbird To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Thu Jun 20 02:11:59 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 1sK5PD-0004xh-Br for guile-user@m.gmane-mx.org; Thu, 20 Jun 2024 02:11:59 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sK5Ol-0007Jo-IA; Wed, 19 Jun 2024 20:11:31 -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 1sK5Oj-0007Jg-U1 for guile-user@gnu.org; Wed, 19 Jun 2024 20:11:29 -0400 Original-Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sK5Oi-0007C4-AC for guile-user@gnu.org; Wed, 19 Jun 2024 20:11:29 -0400 Original-Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1f6559668e1so2388255ad.3 for ; Wed, 19 Jun 2024 17:11:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718842285; x=1719447085; darn=gnu.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=tTkkzi1l2jQZQ/QhkQNql0jvMTqiU8CvRKukWX0/JrM=; b=S/dJGwE9FkFlF78sXjwdTbdDFUgM7xuy2CMU4Lrx0RNADbm2s50xGlux40/uvIzqvo 8rzj8j6yolM0P0gh0/BewwvN/ydl9YbXBa7m+Nf5EIIG4yuy1N24UIihAmIkslXNxyfp owiQsCTJmmUdB2TRrLNtpL7Lxa3zIhcy130T5HDDAQM6kKVe/Iuw4jme/BxbYk/iexBi /qNOUWFwtn1VchhW3Bdo37RGCrjpeQ5ZK7f0JuhzXxOhiro+UaM6EbGYYSOcc0RY0Jsj eLbEHzZHXdXpGtlF/CYys1fTWRgL4SByfq58oPcV+WcuyOsh7KSatkgDYa/4KC2mGh5r 4feg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718842285; x=1719447085; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tTkkzi1l2jQZQ/QhkQNql0jvMTqiU8CvRKukWX0/JrM=; b=tyrrSroPqDBQ8gSjiFhimzytweqSuxlEplA1R56jufaatO6+lgFzE2OBZIB2sX+gte T2vET3t1uTi+qFwyYvWg0hV2lQm+TEAGqGQMKV/tk1QcltfGx3XbfJsEFV9vOTP3ubDY g3ERnNL6pDUZiSwtTcMFIVKz06coB+RNLHI8c++hQzWDNhxDBHV1y4HjfuErqJXmFBD0 Gwvv18gRPJkuCniikakWwZKulWqODxueIHEu6AOQLi//N2vZ2ceo3nkzB8OH/ZjD7Yjm oxZ307l/D7D/hwSq7tzIBVo0iscDGwMxe9ichznEAIQqaeqytbYmODfBY3HESUCunULL QSYA== X-Gm-Message-State: AOJu0Yzmt+WH6Vqkr3LV0MsLpW4p/y+Hyv0fw+7vZOiKNKFQcojYmrGL w3uqxaALHBJ0+yGZ9PFZyzETJw21feCOUFyLYSGmWMvVYHgg1IE8qgog0w== X-Google-Smtp-Source: AGHT+IExd53efD1H5wvd5nT/7Jxh6lLGUzRhr+cgMi3/0SwUea39GPkiMOB8gzKVmcIRRl1MZm8A+Q== X-Received: by 2002:a17:902:6805:b0:1f9:c8cc:9e0f with SMTP id d9443c01a7336-1f9c8cca23cmr4408105ad.18.1718842285378; Wed, 19 Jun 2024 17:11:25 -0700 (PDT) Original-Received: from [192.168.2.154] (64-52-138-37.championbroadband.com. [64.52.138.37]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9c85f4229sm2580785ad.156.2024.06.19.17.11.24 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 19 Jun 2024 17:11:24 -0700 (PDT) Content-Language: en-US In-Reply-To: Received-SPF: pass client-ip=2607:f8b0:4864:20::62e; envelope-from=matt.wette@gmail.com; helo=mail-pl1-x62e.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, FREEMAIL_FROM=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-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:19722 Archived-At: 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