Sik’s got a point here:On Wed, Oct 09, 2013 at 04:20:44PM -0300, Sik the hedgehog wrote:
Also since we’re talking about working on the API: does anybody know
what the program is supposed to do with SDL_JOYDEVICEREMOVED? Because
SDL_JoystickClose() wants a joystick structure, but the event data
gives an instance ID instead (and there’s no way to get a joystick
structure out of an instance ID, so one would need to do bookkeeping
of every joystick). I guess you’ll say “keep track of every
joystick!”, but since my game is single player I opted to just handle
all joysticks the same without paying attention which is which
Using the event interface, you should be able to handle an arbitrary
number of joysticks as they are added to and removed from the system.
And you can, but at the moment you must create a structure in your
program to store pointers to SDL_Joysticks in a linked list. Which
is stupid because SDL_Joystick is already a linked list?but one you
don’t have access to outside of SDL.
So to support an arbitrary number of joysticks, you wait for SDL to
give you SDL_JOYDEVICEADDED, which it now does for every supported
target except Windows mmjoystick which is happily getting nuked!
You query the index, open it, etc. And now you’ve got this pointer
to some SDL_Joystick node. If you are going to keep track of it, you
need to create a linked list structure to store the nodes of the
joysticks you open, because you don’t have access to the head of the
So when you get SDL_JOYDEVICEREMOVED, you have to check your
program’s linked list to find the one that matches event.which, the
instance id of the removed joystick, and then you have to remove that
node from your program’s list and call SDL_JoyStickClose. The reason
SDL_JOYDEVICEREMOVED doesn’t close the joystick, Sam confirmed, is
that it can’t?the program may have a pointer to the SDL_Joystick and
SDL cannot know that the program isn’t going to dereference that
pointer at any moment.
After all, you might not be watching for joystick device events. You
may not even have events compiled into SDL,though I can’t imagine
why that is still even an option. They’re pretty much expected in
every SDL program I’ve ever seen, even (or should I say especially?)
on portable devices like iOS and Android. But still, if you’re using
the old API and polling stuff the hard way, you’re probably using
that pointer SDL_JoystickOpen gave you.
Of course, if you’re using the event API the way it ought to be used,
you get that pointer from SDL_JoystickOpen, save off the instance id
from the SDL_Joystick if you need to know it later on, and you ought
to be able to promptly forget about it. After all, it’s a pointer to
a structure internal to (and managed by) SDL.
The only issue is that you cannot close a joystick by instance id.
You have to send SDL the pointer, which you had no reason to save,
other than being able to close a joystick without shutting down the
whole joystick subsystem, basically upon SDL_JOYDEVICEREMOVED.
It’d be much easier if you could just ask SDL for the SDL_Joystick
pointer corresponding to the instance id. And so I’ve got a patch to
that effect, attached.
-------------- next part --------------
diff -r d9f0067a5421 include/SDL_joystick.h
— a/include/SDL_joystick.h Sun Oct 06 20:39:23 2013 -0700
+++ b/include/SDL_joystick.h Mon Oct 07 12:40:53 2013 -0700
@@ -133,6 +133,11 @@
extern DECLSPEC SDL_JoystickID SDLCALL SDL_JoystickInstanceID(SDL_Joystick * joystick);
- Get the opened joystick or NULL if the instance id is invalid.
+extern DECLSPEC SDL_Joystick *SDLCALL SDL_JoystickForInstanceID(SDL_JoystickID instance_id);
- Get the number of general axis controls on a joystick.
extern DECLSPEC int SDLCALL SDL_JoystickNumAxes(SDL_Joystick * joystick);
diff -r d9f0067a5421 src/joystick/SDL_joystick.c
— a/src/joystick/SDL_joystick.c Sun Oct 06 20:39:23 2013 -0700
+++ b/src/joystick/SDL_joystick.c Mon Oct 07 12:40:53 2013 -0700
@@ -379,6 +379,29 @@
- Get the friendly name of this joystick
const char *