unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Aidan Gauland <aidalgol@no8wireless.co.nz>
To: guile-user@gnu.org
Subject: Parsing a struct within a struct
Date: Tue, 8 Mar 2011 20:20:29 +1300	[thread overview]
Message-ID: <20110308072028.GA11631@neko> (raw)


[-- 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 --]

             reply	other threads:[~2011-03-08  7:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-08  7:20 Aidan Gauland [this message]
2011-03-09  8:24 ` Parsing a struct within a struct Aidan Gauland
2011-03-10 21:36   ` Ludovic Courtès
2011-03-11  2:33     ` Aidan Gauland

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110308072028.GA11631@neko \
    --to=aidalgol@no8wireless.co.nz \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).