Using game controller

I’m trying to use the Gamecontroller api and can’t get it to work
Just from the test code below

    if (SDL_NumJoysticks() > 0) {
        printf("%d\n",SDL_NumJoysticks());
        joy = SDL_JoystickOpen(0);
        if (joy) {
            if(SDL_IsGameController(0)){
                printf("Is game controller\n");
                controller = SDL_GameControllerOpen(0);
                char *mapping;
                mapping = SDL_GameControllerMapping(controller);
                printf("Controller mapped as \n%s\n",mapping);
                SDL_GameControllerEventState(SDL_ENABLE);
            }
            printf("Opened Joystick 0\n");
            printf("Name: %s\n", SDL_JoystickNameForIndex(0));
            printf("Number of Axes: %d\n", SDL_JoystickNumAxes(joy));
            printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(joy));
            printf("Number of Balls: %d\n", SDL_JoystickNumBalls(joy));
        } else {
            printf("Couldn't open Joystick 0\n");
        }
    }

SDL is initialiazed with SDL_INIT_JOYSTICK, the test seems detect my controller and mapping just fine, but when I use:

while(SDL_PollEvent(&event)){
    switch(event.type){
        ...
        case SDL_CONTROLLERBUTTONDOWN:
            printf("test\n");
    }
}

I can never make it print anything, it seems that the event is never generated.

What am I doing wrong?

I think you need to use the SDL_INIT_GAMECONTROLLER flag when executing SDL_Init().
Note that the SDL_INIT_GAMECONTROLLER flag automatically initializes the joystick subsystem, which means you don’t need to use both the SDL_INIT_JOYSTICK and the SDL_INIT_GAMECONTROLLER flags when SDL_init() is executed.

Thanks, that took care of the lack of events, but still having problems with the events themselves. for example:

while(SDL_PollEvent(&event)){
switch(event.type){
    ...
    case SDL_CONTROLLERBUTTONDOWN:
       printf("%s\n",SDL_GameControllerGetStringForButton(event.button));
    }
}

Should print the name of the button no? But I get a incompatible type error with

expected ‘SDL_GameControllerButton {aka enum <anonymous>}’ but argument is of type ‘SDL_MouseButtonEvent {aka struct SDL_MouseButtonEvent}’

The event received from SDL_CONTROLLERBUTTONDOWN should not be SDL_ControllerButtonEvent ?

See https://wiki.libsdl.org/SDL_Event
event.button is a SDL_MouseButtonEvent, the SDL_ControllerButtonEvent is called cbutton.

Also, what you need to pass to SDL_GameControllerGetStringForButton() is not a SDL_ControllerButtonEvent but a SDL_GameControllerButton which is in event.cbutton.button