[SDL 2] Controller even behaving oddly

Hello! I have recently started working with SDL 2 and I am not sure that I
am using the controller/joystick interface correctly. I have a few
questions if you guys don’t mind me asking.

  1. The SDL_CONTROLLERDEVICEADDED event does not seem to be firing upon
    launch if I already have a controller connected before the process and
    SDL_JOYDEVICEADDED does get fired, while both the Joy and Controller
    disconnect events get fired when they are disconnected. Is this normal?
    How would I go about mitigating this feature?

  2. I am somewhat confused on the matter of device IDs and instance
    IDs.While instance IDs seem to increment, device IDs seem almost random. I
    am having a really hard time trying to map players to controllers. For
    example, if I plug in two controllers and listen to the Joystick add event,
    their ‘which’ fields are different, 0 and 1 respectively. But if I unplug
    controller with the ‘which’ of 1, it disconnects with a ‘which’ of 0. Any
    help understanding the interface would be much appreciated. I am aiming
    for the ability for players to be able to add controllers at any time and
    have their individual controller persist to their player even if it gets
    unplugged mid-game.

Thanks guys!–
Jake Shirley, RTIS
@Jake_Shirley <@Jake_Shirley>

Just another note (sorry for the double post, I originally posted from email), this is on Windows 8.1.

I have not used the game controller or joystick api myself yet in
SDL2, however, I will explain it as best as I can (someone else should
shout at me if I am giving incorrect info on this)

  1. SDL_CONTROLLERDEVICEADDED is a thin wrapper around a combination
    of detecting SDL_JOYDEVICEADDED and a call to SDL_IsGameController
    with the ID of the joystick from that event. You could duplicate this
    behavior manually. Although, it appears unintended that it would not
    fire. Are you remembering to init SDL_INIT_GAMECONTROLLER at the
    start?

  2. Device IDs identify a device, they are GUID’s, they attempt to
    identify the same physical device over multiple sessions
    (disconnecting and reconnecting the device, restarting your
    application, etc). An SDL_JoystickID is an ID to identify a joystick
    by ID within a given lifetime of the device being connected. Use
    device IDs to attempt to find the same device again between
    application restarts, etc and use the joystick ID in your code to
    access the device itself (in your code or when references in events,
    etc).On Sat, Nov 23, 2013 at 4:16 PM, darknavi wrote:

Just another note (sorry for the double post, I originally posted from
email), this is on Windows 8.1.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

I just realized #2 did not fully answer your question. Once you
SDL_JoystickOpen the joystick, use SDL_JoystickInstanceID to get the
instance ID of the opened joystick. The close events will use that ID
for the which field. The ID used in the add event is a device ID (but
not a GUID like I described, just some value useless to you beyond
referring to which device you want to open). All events referring to
open joysticks use the actual instance ID, I believe you can open a
joystick by hardware ID multiple times and get multiple instance IDs.On Sun, Nov 24, 2013 at 2:46 PM, Andre D <@Andre_D> wrote:

I have not used the game controller or joystick api myself yet in
SDL2, however, I will explain it as best as I can (someone else should
shout at me if I am giving incorrect info on this)

  1. SDL_CONTROLLERDEVICEADDED is a thin wrapper around a combination
    of detecting SDL_JOYDEVICEADDED and a call to SDL_IsGameController
    with the ID of the joystick from that event. You could duplicate this
    behavior manually. Although, it appears unintended that it would not
    fire. Are you remembering to init SDL_INIT_GAMECONTROLLER at the
    start?

  2. Device IDs identify a device, they are GUID’s, they attempt to
    identify the same physical device over multiple sessions
    (disconnecting and reconnecting the device, restarting your
    application, etc). An SDL_JoystickID is an ID to identify a joystick
    by ID within a given lifetime of the device being connected. Use
    device IDs to attempt to find the same device again between
    application restarts, etc and use the joystick ID in your code to
    access the device itself (in your code or when references in events,
    etc).

On Sat, Nov 23, 2013 at 4:16 PM, darknavi wrote:

Just another note (sorry for the double post, I originally posted from
email), this is on Windows 8.1.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks for the awesome help Andre! I do have a question though.

The following line seems to always return a GUID of all zeros:

Code:
SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(event.jdevice.which);

Any ideas why that would be happening?------------------------
Jake Shirley, RTIS

Means your “which” is invalid, the function appears to take a device id and
not an instance id. I guess it is intended for use at device detection
time.On Sun, Nov 24, 2013 at 6:36 PM, darknavi wrote:

Thanks for the awesome help Andre! I do have a question though.

The following line seems to always return a GUID of all zeros:

Code:

SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(event.jdevice.which);

Any ideas why that would be happening?


Jake Shirley, RTIS


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Very odd. I am using the SDL_JOYDEVICEADDED event and the ‘which’ seems to be a valid value at first glance. With four controllers plugged in I get device IDs 0-3.------------------------
Jake Shirley, RTIS

darknavi wrote:

Thanks for the awesome help Andre! I do have a question though.

The following line seems to always return a GUID of all zeros:

Code:
SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(event.jdevice.which);

Any ideas why that would be happening?

I’m getting all 0’s for my GUID as well on an XBOX360 Afterglow controller. Happens only in windows, not in linux (in linux it works perfectly). Recognizes my hat as buttons as well. Also switches some other buttons around.

I run the code directly from http://www.libsdl.org/tmp/SDL/test/testjoystick.c
and still have the issue. Is really annoying.

SDL_GetError() returns nothing for the entire life of the app - even when I specifically add a call to get the guid and then call GetError.

Don’t want to have to add a mapping for a GUID of all 0’s, but I don’t see another way.