Types of the mouse button constants and SDL_Keycode constants

I’m working on an input handler and I’d really like to make a map of named
actions to key presses or mouse buttons. In the API constants like
SDL_BUTTON_LEFT are preprocessor defines in SDL_mouse.h and the SDL_Keycode
constants are defined as an enum and they’re mapped to chars (actually
there’s something I don’t understand, SDL_Keycode is an int Sint32 but the
enum is anonymous and the constants are chars). Since these constants are
in no way related and neither really even have a type its difficult to
separate them with overloaded methods.

In my use case, I created a map with a string as the key and a string as
the value so I could store the names of each key or the names of the mouse
buttons. I also needed to create a function for getting a name from the
mouse button constants. The way the constants are currently defined makes
this ugly, the mouse buttons don’t even have a type and the SDL_Keycode
type falsely catches the button constants since it maps to an int. This
means one would need to use char as the key code type and int as the mouse
button type. I’m interested in hearing opinions on changing the key
constants from an anonymous enum to a typedef enum, i.e

typedef enum {
//SDLK_* constants
} SDL_Keycode;

and changing the the mouse button defines to a typedef enum like the above
as well. It would also be nice to add a function like SDL_GetKeyName for
the mouse buttons.

I’ve attached a patch that changes the SDL_K constants to a typedef enum
and gives them the type SDL_Keycode and changes the mouse button defines to
a typedef enum and gives them the type SDL_MouseButton. I could also write
an SDL_GetMouseButtonName function and provide a patch but I’m not sure if
the diff should be done on the original tree or after this initial patch
has been applied.
-------------- next part --------------
A non-text attachment was scrubbed…
Name: include.patch
Type: text/x-patch
Size: 3350 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20140129/7bad87a6/attachment.bin

Quoth Andrew , on 2014-01-29 01:27:03 -0500:

I’m interested in hearing opinions on changing the key
constants from an anonymous enum to a typedef enum

There’s more potential for ABI problems with that, and in particular
it’s not guaranteed to be backwards-compatible with existing int that
way. C compilers are often allowed to choose whatever integer width
they want for enums based on the values declared. That also means ABI
breakage if the set of values gets extended in a way that changes the
width. I would imagine this is why they’re set up the way they are in
the first place; I’ve specified explicit-width typedefs and declared
the enum constants separately in C libraries before for this exact
reason.

(It may be that most/all of the environments SDL supports don’t have
this characteristic, but I vaguely recall Android liking short-enums
in particular, and it’s one more way to trip and fall.)

—> Drake Wilson

Surely it would at least be okay to change the defines in SDL_mouse.h
to an enum with a type at least though? That would allow my use case
at least. It seems to me that the mouse change wouldn’t break the ABI
if we used the same values as the original defines.On Wed Jan 29, 2014 12:38:45 -0500, Drake Wilson wrote:

I’m interested in hearing opinions on changing the key
constants from an anonymous enum to a typedef enum

There’s more potential for ABI problems with that, and in particular
it’s not guaranteed to be backwards-compatible with existing int that
way. C compilers are often allowed to choose whatever integer width
they want for enums based on the values declared. That also means ABI
breakage if the set of values gets extended in a way that changes the
width. I would imagine this is why they’re set up the way they are in
the first place; I’ve specified explicit-width typedefs and declared
the enum constants separately in C libraries before for this exact
reason.

(It may be that most/all of the environments SDL supports don’t have
this characteristic, but I vaguely recall Android liking short-enums
in particular, and it’s one more way to trip and fall.)

—> Drake Wilson

Quoth Andrew , on 2014-01-29 14:45:52 -0500:

Surely it would at least be okay to change the defines in SDL_mouse.h
to an enum with a type at least though? That would allow my use case
at least.

That still feels inconsistent and fragile and invites someone to make
a function that takes an SDL_MouseButton later.

its difficult to separate them with overloaded methods

Nor am I a fan of “let’s fiddle with the type system in a C library to
make things easier for C++” (I assume “overloaded methods” implies
that in this context).

(But I’m not the one to decide, mind.)

Is making your own thin disjunction/sum type for "keycode or button"
so problematic? C++ is pretty good at that, after all.

—> Drake Wilson