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

* Re: Parsing a struct within a struct
  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
  0 siblings, 1 reply; 4+ messages in thread
From: Aidan Gauland @ 2011-03-09  8:24 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 658 bytes --]

On Tue, Mar 08, 2011 at 08:20:29PM +1300, Aidan Gauland wrote:
> (parse-c-struct event-pointer
>                 (list uint8 uint8 uint8
>                       (list uint8 int int uint16 )))
>

It seemed to be an off-by-one(-byte) error.  There was padding to
align (to 32-bits, I think) the sub-struct.

This works on my 32-bit machine, but I suspect it is not portable.

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

What would be a better, portable solution?  I suspect the `alignof'
procedure is my friend here, but I don't understand how to use it.

--Aidan

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

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

* Re: Parsing a struct within a struct
  2011-03-09  8:24 ` Aidan Gauland
@ 2011-03-10 21:36   ` Ludovic Courtès
  2011-03-11  2:33     ` Aidan Gauland
  0 siblings, 1 reply; 4+ messages in thread
From: Ludovic Courtès @ 2011-03-10 21:36 UTC (permalink / raw)
  To: guile-user

Hi Aidan,

Aidan Gauland <aidalgol@no8wireless.co.nz> writes:

> On Tue, Mar 08, 2011 at 08:20:29PM +1300, Aidan Gauland wrote:
>> (parse-c-struct event-pointer
>>                 (list uint8 uint8 uint8
>>                       (list uint8 int int uint16 )))
>>
>
> It seemed to be an off-by-one(-byte) error.  There was padding to
> align (to 32-bits, I think) the sub-struct.
>
> This works on my 32-bit machine, but I suspect it is not portable.
>
> (parse-c-struct event-pointer
>                 (list uint8 uint8 uint8	uint8 ; Padding
>                       (list uint8 int int uint16 )))
>
> What would be a better, portable solution?  I suspect the `alignof'
> procedure is my friend here, but I don't understand how to use it.

The ‘alignof’ procedure was actually your enemy here.  ;-)

This patch should fix it (will be in 2.0.1):

  http://git.savannah.gnu.org/cgit/guile.git/commit/?h=stable-2.0&id=d82f8518b96bbfa4f29e03d922369c37b64824d9

Thanks!

Ludo’.

PS: Let us know when your game is ready.  :-)




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

* Re: Parsing a struct within a struct
  2011-03-10 21:36   ` Ludovic Courtès
@ 2011-03-11  2:33     ` Aidan Gauland
  0 siblings, 0 replies; 4+ messages in thread
From: Aidan Gauland @ 2011-03-11  2:33 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 239 bytes --]

On Thu, Mar 10, 2011 at 10:36:43PM +0100, Ludovic Courtès wrote:
> This patch should fix it (will be in 2.0.1):

It did!  Fantastic!  Thank you very much! :D

> PS: Let us know when your game is ready.  :-)

Definitely.

--Aidan

[-- 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).