0
\$\begingroup\$

SDL2 recognizes my PS4 controller but not the button presses. Here is a minimal code example.

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

int main(int argc, char** argv) {
  if (SDL_Init(SDL_INIT_VIDEO) != 0 ) {
    printf("Unable to initialize SDL (%s)\n",SDL_GetError());
    printf("Exit\n");
    return -1;
  }

  SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);

  SDL_GameController *gameController = NULL;
  int numJoysticks;
  numJoysticks = SDL_NumJoysticks();
  printf("%d joysticks found\n", numJoysticks);

  if( numJoysticks <= 0 ) {
    return -1;
  }

  if( ! SDL_IsGameController(0) ) {
    printf("Error: joystick 0 is not a game controller\n");
    printf("Exit\n");
    SDL_Quit();
    return -1;
  }

  gameController = SDL_GameControllerOpen(0);

  if( ! gameController ) {
    printf("No game controller found\n");
    printf("Exit\n");
    SDL_Quit();
  }

  printf("Opened Game Controller 0\n");
  printf("Found a valid controller, named: %s\n", SDL_GameControllerName(gameController));

  if( SDL_GameControllerGetButton(gameController, SDL_CONTROLLER_BUTTON_A) ) {
    printf("Button press!\n"); // Never printed
  }

  // Exit
  SDL_GameControllerClose(gameController);
  SDL_Quit();
  return 0;
}

Here is the output I get:

1 joysticks found
Opened Game Controller 0
Found a valid controller, named: PS4 Controller

What am I doing wrong?

\$\endgroup\$
1
  • \$\begingroup\$ I wanted to add I am on Linux (Debian jessie) and I tested my controller and it works (on html5gamepad.com and with the application jstest-gtk). \$\endgroup\$
    – Itsbananas
    Commented Oct 28, 2018 at 12:39

1 Answer 1

2
\$\begingroup\$

You are only polling the button press once, at a very specific moment. It's very likely that the button is not pressed at that time, and you miss the event. Try replacing the if( SDL_GameControllerGetButton(gameController, SDL_CONTROLLER_BUTTON_A) ) statement with the following:

bool poll = true;

while (poll)
{
    SDL_Event e;
    while (SDL_PollEvent(&e))   
    {
        if (e.type == SDL_CONTROLLERBUTTONDOWN && e.cbutton.button == SDL_CONTROLLER_BUTTON_A)
        {
            printf("Button press!\n");
            poll = false;
            break;
        }
    }
}

The code runs a loop that polls the button state until you press it down, and then stops.

\$\endgroup\$
9
  • \$\begingroup\$ Thanks for answering! Unfortunately this still doesn't work. I pressed on every button possible and it doesn't detect anything. \$\endgroup\$
    – Itsbananas
    Commented Oct 28, 2018 at 12:00
  • \$\begingroup\$ Could you try setting up this? It might be possible that there are no default mappings for a PS4 controller. \$\endgroup\$
    – user35344
    Commented Oct 28, 2018 at 12:14
  • \$\begingroup\$ I'm currently trying this. I'll answer in a moment. \$\endgroup\$
    – Itsbananas
    Commented Oct 28, 2018 at 12:28
  • \$\begingroup\$ I generated some mapping strings but it still does not work. I'll try to test both the code and the game controller on my Windows machine. \$\endgroup\$
    – Itsbananas
    Commented Oct 28, 2018 at 13:01
  • \$\begingroup\$ Did you just try the default mapping file provided in the repo I linked? \$\endgroup\$
    – user35344
    Commented Oct 28, 2018 at 13:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .