Joystick Instance IDs ordering

We found that when multiple controllers are added at once, the order of the joystick instance IDs varies between OS. E.g. Linux and MacOS add them in ascending order, while Windows adds them in descending order. The latter causes problems in our code, especially when adding two or more of the same controller. Then it makes a difference if you add the controllers at once or one by one.

A solution would be to cache the added IDs if they do not follow an ascending order. Only when the cache contains all IDs without a gap to previously added IDs, the code would add the cached IDs, now in ascending order.

Example:

  • already added are ID0 + ID1
  • ID3 gets added by SDL
  • code detects a gap to ID1 and caches ID3
  • ID2 gets added by SDL
  • code detects that there is no gap to ID1 and adds ID2
  • code detects that there is no gap to ID2 and adds ID3

However this will only work if we can make the following assumptions

  • IDs are always starting with 0
  • IDs have no (permanent) gaps

Are these assumptions valid? Or is there any other option to make sure that the OS adds the IDs in ascending order?

BTW: Does anyone know if the reversed ID order for Windows is an SDL2 for Windows implementation detail or cause by Windows?

Also a really unique controller identifier would help us. But the id returned by SDL_JoystickGetDeviceGUID is not unique if you have two identical controllers.

The IDs aren’t index values, so if you’re treating them like they are, the best fix would be in your code rather than SDL’s.

No, we don’t treat the ID as index. But since the GUID is not fully unique, we have to use something to differentiate identical controllers and create really unique IDs. So we extend the GUID with a counter. The problem is, that due to the reversed order in Windows, the counter is different if you add the controllers one by one (while the program is running) or all at once (at program start).