From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Jan Synacek Newsgroups: gmane.lisp.guile.user Subject: Re: FFI questions Date: Sun, 17 May 2020 12:06:10 +0200 Message-ID: References: <6762c9b7-ad3d-3bf8-0b65-5d30bcb8ea38@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="67963"; mail-complaints-to="usenet@ciao.gmane.io" Cc: guile-user@gnu.org To: Taylan Kammer Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun May 17 12:06:49 2020 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 1jaGC0-000HZP-7y for guile-user@m.gmane-mx.org; Sun, 17 May 2020 12:06:48 +0200 Original-Received: from localhost ([::1]:44658 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jaGBz-0005Bz-9k for guile-user@m.gmane-mx.org; Sun, 17 May 2020 06:06:47 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:40152) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jaGBj-00059i-Uq for guile-user@gnu.org; Sun, 17 May 2020 06:06:32 -0400 Original-Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:40990 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1jaGBh-0004UA-U1 for guile-user@gnu.org; Sun, 17 May 2020 06:06:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1589709988; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=NUG8eHyilMAa66BCXAZvSltnpwBUzFzu/Zt2qQxcmPI=; b=ZbQNUzOpufRzNvJTprxfCDZXFftZUTkTXGn6lcTMB+yEXkUjQ71HfL1lpijHWP62/vYp71 sqOKq+SjPeQt7gbJIA7uta9q8h17+4JfYDvkLIg40T32kvedM55R1F9YhIvTKmpWdmEPRM m47Wxq9lYNg6tTiOT+G2FhbrwdKMCiE= Original-Received: from mail-lf1-f70.google.com (mail-lf1-f70.google.com [209.85.167.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-126-pfPs2vZdOh6Sc6WjANzrLA-1; Sun, 17 May 2020 06:06:24 -0400 X-MC-Unique: pfPs2vZdOh6Sc6WjANzrLA-1 Original-Received: by mail-lf1-f70.google.com with SMTP id 125so2565595lfk.19 for ; Sun, 17 May 2020 03:06:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=NUG8eHyilMAa66BCXAZvSltnpwBUzFzu/Zt2qQxcmPI=; b=pj104fQ9T1nd6lJd6w3qzGLQGVOcBOZ3L7RWQEnjGR89gsv/Gjdkiq8rOsTqM+0Dt1 ohrAK4NSzNvApYYskyOQfDF8EMh6Q1uWOWC5e/wenuz2nxoF6WJO2lFlzSmPMjCbRgtJ WRYzlO4snJmAISH+vt07gAzbql/DZYE44r6CZHJ1oRilMpkAYBxL/b6W3pHcQaeMRMw1 Sni428LbruhfxxgHZcSEfJI4JoLawrNJ7KsB/zt4rCy2FZ/Gq7IJCANsGWSG8Ftj+WAP ttQ2Z/RZcTfXQze1uyph8H/oHxyGga4QlLzbxcDxhBc7tJ4PTLPq5UTjw1JHZYl5KIxQ zaZQ== X-Gm-Message-State: AOAM533/gSSjPChiBHiBYqkhBSp948fIecr0aORXMIoi70vXRBIDppH5 Ta+g04FWfnhtDZi4AfghGP41FAd8oqRQlAlztw8l+YqbE+oX1temtvrpzic5A5wnw5NLHghRTNW 7BL45If2fQliGgvTfbMGMvQfEDzs= X-Received: by 2002:a2e:9e97:: with SMTP id f23mr7167850ljk.228.1589709982695; Sun, 17 May 2020 03:06:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzdVJYjln+tWUosB9SH6D2QD0wMRfayamFmOxEARTY/XVF9E1juTnpAS2pu4BwFQ9WlhjYYDzuPP679l0TeSwc= X-Received: by 2002:a2e:9e97:: with SMTP id f23mr7167834ljk.228.1589709982298; Sun, 17 May 2020 03:06:22 -0700 (PDT) In-Reply-To: <6762c9b7-ad3d-3bf8-0b65-5d30bcb8ea38@gmail.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Received-SPF: pass client-ip=205.139.110.120; envelope-from=jsynacek@redhat.com; helo=us-smtp-1.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/05/17 04:13:55 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] 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, DKIMWL_WL_HIGH=0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 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" Xref: news.gmane.io gmane.lisp.guile.user:16467 Archived-At: On Fri, May 15, 2020 at 10:09 PM Taylan Kammer wrote: > > On 15.05.2020 15:47, Jan Synacek wrote: > > Hello, > > > > Currently I have something like this: > > > > (define libxcb (dynamic-link "libxcb")) > > > > ... > > > > (define c-change-window-attributes > > (pointer->procedure void > > (dynamic-func "xcb_change_window_attributes" libxcb) > > (list '* uint32 uint32 '*))) > > > > (define (change-window-attributes conn win mask vals) > > (c-change-window-attributes conn win mask (bytevector->pointer vals))) > > > > The last argument to xcb_change_window_attributes is 'const void *' and I > > need to pass a u32 vector to it. As it is right now, it segfaults when I > > try passing #u32(something) to 'change-window-attributes'. Is it possible > > to make it accept a u32 vector using just Scheme or do I have to work > > around it on the C level? > > I think the reason it segfaults might be that the #u32() object gets > garbage collected soon after you call bytevector->pointer on it. See > that you put it in a variable, and reference that variable at some point > after xcb_change_windor_attributes should be done with the vector, to > make sure that this is not the problem. > > Otherwise I'm not sure why it should segfault. A #u32() bytevector > really should be backed by a contiguous array of uint32_t values, just > like what you'll get when you define a uint32_t[] in C. This works, thanks! (define (change-window-attributes conn win mask vals) (define b (list->u32vector vals)) (c-xcb-change-window-attributes conn win mask (bytevector->pointer b))) > > My second question is about FFI and structs. Is it possible to access C > > struct members from Scheme by name? If not, how do I generally approach the > > problem? I've checked how guile-xcb does it and it seems to be building > > hash tables with field names as keys. But that basically requires me to > > "redefine" all the C structs that I would be interested in at the Scheme > > level. > > These might be useful: > > https://github.com/TaylanUB/scheme-bytestructures > > https://www.nongnu.org/nyacc/ffi-help.html > > I maintain bytestructures, am happy to answer questions. (If I don't > respond on the mailing list, feel free to mail me directly or open an > issue on GitHub; mails with guile-user@gnu.org and guile-devel@gnu.org > in the To: or Cc: fields land in a folder that I don't always check.) Very interesting, I'll take a look. Thank you, -- Jan Synacek Software Engineer, Red Hat