The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
CONTROLLERDEVICEREMOVED .which increments on each unplug
Daniel Holth
Guest

In SDL2 2.0.3 on OSX, I'm using the attached pysdl2-cffi Python code
to keep track of attached game controllers. I'm plugging in and
removing a single XBox controller to test.

When the code gets CONTROLLERDEVICEADDED, event.cdevice.which is
always 0, but when it gets CONTROLLERDEVICEREMOVED,
event.cdevice.which is 0, then 1, then 2, and so on.

Since 'which' on remove does not match 'which' on added, there is no
property on the controller objects that I can just match up with the
'removed' events. Instead, when I get a 'removed' event I iterate over
all my controller objects and discard the ones that are not still
attached.

Is this a bug, or have I misunderstood the 'event.cdevice.which' value
on CONTROLLERDEVICEREMOVED?

Thanks,

Daniel
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
CONTROLLERDEVICEREMOVED .which increments on each unplug
Ryan C. Gordon
Guest

Quote:
Is this a bug, or have I misunderstood the 'event.cdevice.which' value
on CONTROLLERDEVICEREMOVED?

We should make this more clear at the API level for SDL 2.1, but "which"
means something different for each of those events:

Sint32 which; /**< The joystick device index for the ADDED
event, instance id for the REMOVED or REMAPPED event */

The idea is when it's added, you can do
SDL_GameControllerOpen(which)...and several other things that operate on
unopened joystick indexes. This is a number that might get recycled if
we add a stick after one has been removed, to keep
SDL_NumJoysticks()-sized arrays reasonable).

On removal, though, this will be an instance id. This number is unique
and increments on each new joystick the system sees. You can get this
for a controller with:

myInstance =
SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(myController));

(maybe for 2.1, we'll get rid of joystick indexes and use instance ids
for everything.)

--ryan.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
CONTROLLERDEVICEREMOVED .which increments on each unplug
Daniel Holth
Guest

On Oct 7, 2014 11:40 AM, "Ryan C. Gordon" wrote:
Quote:


Quote:
Is this a bug, or have I misunderstood the 'event.cdevice.which' value
on CONTROLLERDEVICEREMOVED?


We should make this more clear at the API level for SDL 2.1, but "which" means something different for each of those events:

    Sint32 which;       /**< The joystick device index for the ADDED event, instance id for the REMOVED or REMAPPED event */

The idea is when it's added, you can do SDL_GameControllerOpen(which)...and several other things that operate on unopened joystick indexes. This is a number that might get recycled if we add a stick after one has been removed, to keep SDL_NumJoysticks()-sized arrays reasonable).

On removal, though, this will be an instance id. This number is unique and increments on each new joystick the system sees. You can get this for a controller with:

    myInstance = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(myController));

(maybe for 2.1, we'll get rid of joystick indexes and use instance ids for everything.)
No wonder. I was trying to ignore the joystick api, and wondered why I could not find game controller get instance id.
Quote:
--ryan.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org