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
Hot Swapping Joysticks
Dw77


Joined: 15 Oct 2014
Posts: 3
Is there a way to hot swap Joysticks while SDL is running without having to call SDL_QuitSubSystem and SDL_InitSubSystem repeatedly?
I ask because I'd like to be able to do things like switch a new controller in, or add another game pad should a friend want to play, Ideally I want to be able to write code that automatically responds to the new controller.

Thank You
CarlRapp


Joined: 04 Nov 2014
Posts: 1
The current solution of pluggin out/in controllers during runtime isn't really optimal in SDL 2.0, but I'll tell you how I managed to achieve it!

First of all, when you connect a new device two types of IDs will be made, there is a sort of Controller ID which is between 0 and <number of controllers plugged in>, and then there is some sort of Joystick ID hidden behind the Controller ID.
This Joystick ID is always counting up, so if you have only one controller and plugs it in, it will get (first time you plug it in)
Controller ID: 0
Joystick ID: 0

If you then unplug it and plug it back in it will get
Controller ID: 0
Joystick ID: 1

And this Joystick ID will always increase for every controller you plug in, so what I did to still being able to map "Controller ID 0" to "Joystick ID n" was to use a simple array where ControllerID[0] was the Joystick ID for Controller 0 and so on.
And then whenever I had the Joystick ID and needed to know what controller it belonged to I just checked the array to find this value.

It's probably not the best solution but it works for the scenarios I've tested, I can unplug and plug back controller 0 over and over again and still get the input!
I'm probably also gonna rewrite my whole Input Manager to a more neat solution, but I can link you my current controller code if you would like to.