unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Parsing a struct within a struct
@ 2011-03-08  7:20 Aidan Gauland
  2011-03-09  8:24 ` Aidan Gauland
  0 siblings, 1 reply; 4+ messages in thread
From: Aidan Gauland @ 2011-03-08  7:20 UTC (permalink / raw)
  To: guile-user


[-- Attachment #1.1: Type: text/plain, Size: 2235 bytes --]

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 #1.2: fruitflats.tar.gz --]
[-- Type: application/octet-stream, Size: 1984 bytes --]

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-03-11  2:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-03-08  7:20 Parsing a struct within a struct Aidan Gauland
2011-03-09  8:24 ` Aidan Gauland
2011-03-10 21:36   ` Ludovic Courtès
2011-03-11  2:33     ` Aidan Gauland

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).