[Openmcl-devel] External struct decomposition

Gary Byers gb at clozure.com
Thu Aug 12 23:42:19 UTC 2004



On Thu, 12 Aug 2004, Andrew P. Lentvorski, Jr. wrote:

> I'm trying to decompose the fields of an external struct so that I can
> use them in Lisp.  Specifically, I have the following callback from
> Gtk:
>
> (defcallback configure-event-callback
>    (:address widget :address event :address data :signed-int)
>    (format t "Configure event received~%")
>    +gtk-true+)
>
> That event is a pointer to a structure GdkEventConfigure whose
> information I would like to extract:
>
> struct _GdkEventConfigure
> {
>    GdkEventType type;
>    GdkWindow *window;
>    gint8 send_event;
>    gint x, y;
>    gint width;
>    gint height;
> };
>

(def-foreign-type :<G>dk<E>vent<T>ype :int)
(def-foreign-type :gint8 :signed-byte)
(def-foreign-type :gint :int)

(def-foreign-type nil
    (:struct :_<G>dk<E>vent<C>onfigure
             (:type :<G>dk<E>vent<T>ype)
             (:window :address)
             (:send_event :gint8)
             (:x :gint)
             (:y :gint)
             (:width :gint)
             (:height :gint)))


> GdkEventType is an enum (hence an int).  I presume that the magic
> keyword is :struct, but I'm not finding anything in the docs that gives
> an example of how to do this.

There aren't too many examples of this (in the OpenMCL sources);
translating interfaces by hand is a tedious, error-prone process.
Fortunately, there's rarely a good reason to do this by hand.
If there were some reason to do this by hand, it might look like:

(def-foreign-type :<G>dk<E>vent<T>ype :int)
(def-foreign-type :gint8 :signed-byte)
(def-foreign-type :gint :int)

(def-foreign-type nil
    (:struct :_<G>dk<E>vent<C>onfigure
             (:type :<G>dk<E>vent<T>ype)
             (:window :address)
             (:send_event :gint8)
             (:x :gint)
             (:y :gint)
             (:width :gint)
             (:height :gint)))



> I presume that gint8 is going to need
> some form of packing directives just to make matters more interesting.

If it were necessary to use packing directives to achieve natural alignment
(to ensure that the :gint which followed the :gint8 was aligned on a 32-bit
boundary, in this case), matters would be so interesting that it'd be
very difficult to get anything done.  Fortunately, it's not necesary to
use such directives to achieve natural alignment of structure fields.
(OSX's default alignment rules don't quite agree with what's natural
for the processor; DOUBLE-FLOATs and other things that're bigger than
32 bits only have to be aligned on 32-bit boundaries, unless they're
the first field in a record or it's Tuesday.  I may be misremembering
the part about Tuesday, but I believe that DEF-FOREIGN-TYPE knows the
gory details and how to handle them.)

There are some legacy MacOS interfaces that date back to 68K days;
the 68K only imposed 16-bit alignment on fields whose size is >=
16 bits.  The interface translator handles this, but don't believe
that DEF-FOREIGN-TYPE offers syntax for this.


>
> Any examples or pointers would be appreciated.

I believe that Rick Taube said that he had GTK+2 .cdb files;  I'd be
glad to incorporate them into the next release.

Personally, I'd trust the accuracy and completeness of automatically
generated interfaces a lot more than I'd trust manual translations
like the one above.

>
> Thanks,
> -a
>
>
> _______________________________________________
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
> http://clozure.com/mailman/listinfo/openmcl-devel
>
>



More information about the Openmcl-devel mailing list