3

I was writing a small program in C that utilizes SDL 2.0, and ran into a problem when I couldn't get SDL_NumJoysticks() to report the number of joysticks plugged in at the time of the function call. I believe that it is reporting the number of joysticks connected during one of SDL's initialization functions (I would guess 'SDL_Init()', but I have no evidence), and then keeps on giving you that number throughout the rest of the program. Here is a short test program that I have been using:

#include <stdio.h>
#include <SDL2/SDL.h>

int main() {
    SDL_Event event;
    SDL_Window *window;
    short joysticks = 0;

    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
        fprintf(stderr, "SDL_Init error: %s\n", SDL_GetError());
        return 1;
    }

    window = SDL_CreateWindow("Test window", 0, 0, 800, 600, SDL_WINDOW_SHOWN);
    if (window == NULL) {
        fprintf(stderr, "SDL_CreateWindow error: %s\n", SDL_GetError());
        return 1;
    }

    printf("%s\n", SDL_GetError());

    while (1) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                printf("%s\n", SDL_GetError());
                SDL_DestroyWindow(window);
                SDL_Quit();
                return 0;
            } else if (event.type == SDL_JOYDEVICEADDED) {
                printf("Joystick added!\n");
            } else if (event.type == SDL_JOYDEVICEREMOVED) {
                printf("Joystick removed!\n");
            }
        }

        if (SDL_NumJoysticks() > joysticks) {
            printf("Joystick inserted.\n");
            joysticks++;
        } else if (SDL_NumJoysticks() < joysticks && SDL_NumJoysticks() >= 0) {
            printf("Joystick removed.\n");
            joysticks--;
        } else if (SDL_NumJoysticks() < 0) {
            printf("Something went wrong!\n");
            SDL_DestroyWindow(window);
            SDL_Quit();
            return 1;
        }
    }

    return 0;
}

The program accurately reports the number of joysticks plugged in when the program is started, but does absolutely nothing after that.

The official SDL docs for SDL_Numjoysticks() state that it "Returns the number of attached joysticks on success". How can I get it to tell me the number of joysticks plugged in at the time of the function call? Am I making a mistake in my code, or is that just not the way that SDL_NumJoysticks() works?

fouric
  • 1,598
  • 1
  • 19
  • 37
  • Which OS were you running on? – Charlie Apr 14 '14 at 23:52
  • @Charlie, 32-bit Lubuntu 13.10 with the stock SDL 2.0 from the repositories. – fouric Apr 20 '14 at 22:41
  • I'm having similar (if not exactly the same) trouble on OSX. The way I see it, the method should work as it does on Windows, and if it doesn't, the differences should be documented. This thread on the official SDL BB goes into some detail, but appears to stop short of actually clarifying the situation - http://forums.libsdl.org/viewtopic.php?t=9592&sid=f1468617007f482a33de58d62234405f - If you find more details, please post back here! – Charlie Apr 20 '14 at 22:54

1 Answers1

5

Make sure you follow these steps and see if you still have problems:

  • Call SDL_JoystickEventState(SDL_ENABLE) after SDL_INIT
  • If that is not enough, try to force an update by calling SDL_JoystickUpdate(); before while (SDL_PollEvent(&event))

EDIT: More info that I think should be useful:

  • Always call SDL_PollEvent on the Main Thread (Or SDL_PumpEvents() on Main Thread and SDL_PeepEvents elsewhere)
  • If you dont do that, SDL wont update the number of joysticks nor hotplug events
  • Other joystick events work when you pump outside the Main thread but it is not recommended.
Rodrigo
  • 674
  • 8
  • 19