guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Parsing a struct within a struct


From: Aidan Gauland
Subject: Parsing a struct within a struct
Date: Tue, 8 Mar 2011 20:20:29 +1300
User-agent: Mutt/1.5.20 (2009-06-14)

Hello,

I'm trying to write a game in Guile Scheme using SDL, and I'm having
trouble parsing a C struct within a struct.  (The files for my program
are in the attached tarball, for those with the time to examine it
that closely.)

This is how the struct is parsed...

(parse-c-struct event-pointer
                (list uint8 uint8 uint8
                      (list uint8 int int uint16 )))

...which is called only when the event (an SDL event) is a key-down
event.

These are the relevant SDL structs...

/** General event structure */
typedef union SDL_Event {
        Uint8 type;
        SDL_ActiveEvent active;
        SDL_KeyboardEvent key;
        SDL_MouseMotionEvent motion;
        SDL_MouseButtonEvent button;
        SDL_JoyAxisEvent jaxis;
        SDL_JoyBallEvent jball;
        SDL_JoyHatEvent jhat;
        SDL_JoyButtonEvent jbutton;
        SDL_ResizeEvent resize;
        SDL_ExposeEvent expose;
        SDL_QuitEvent quit;
        SDL_UserEvent user;
        SDL_SysWMEvent syswm;
} SDL_Event;

typedef struct SDL_KeyboardEvent {
        Uint8 type;     /**< SDL_KEYDOWN or SDL_KEYUP */
        Uint8 which;    /**< The keyboard device index */
        Uint8 state;    /**< SDL_PRESSED or SDL_RELEASED */
        SDL_keysym keysym;
} SDL_KeyboardEvent;

typedef struct SDL_keysym {
        Uint8 scancode;                 /**< hardware specific scancode */
        SDLKey sym;                     /**< SDL virtual keysym */
        SDLMod mod;                     /**< current key modifiers */
        Uint16 unicode;                 /**< translated character */
} SDL_keysym;

SDLKey and SDLMod are declared as typedef enums.
i.e.
typedef enum {
  ...
} SDLKey or SDLMod;

In my program, I print the result of the above call to
`parse-c-struct'.  In the program's output, the value of the sym field
is always a high-magnitude, negative number, but the value is unique
for each key, and consistent for each key (i.e. a key-press of, say, A
is always the same value).  But the defined values of SDLKey range
from 0 to 322, so the parsed value is obviously incorrect.  (I think
the parsed value of the mod field is also incorrect.)

Am I doing something wrong?  Are the arguments to `parse-c-struct'
incorrect?  Am I neglecting some important detail of how structs are
dealt with in C (such as alignment)?  Or (heaven forbid) is it a bug
in the FFI?

Regards,
Aidan Gauland

Attachment: fruitflats.tar.gz
Description: Binary data

Attachment: signature.asc
Description: Digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]