SDL2 Game Controller GUID

From Free Knowledge Base- The DUCK Project: information for everyone
Revision as of 20:48, 14 May 2020 by Admin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Ubuntu / Debian / Mint

To install SDL2

sudo apt-get update
sudo apt-get install libsdl2-dev

Save the following

vi sdl2-joystick.c 
#include <SDL.h>

int main()
{
  SDL_Init(SDL_INIT_JOYSTICK);
  atexit(SDL_Quit);

  int num_joysticks = SDL_NumJoysticks();
  int i;
  for(i = 0; i < num_joysticks; ++i)
  {
    SDL_Joystick* js = SDL_JoystickOpen(i);
    if (js)
    {
      SDL_JoystickGUID guid = SDL_JoystickGetGUID(js);
      char guid_str[1024];
      SDL_JoystickGetGUIDString(guid, guid_str, sizeof(guid_str));
      const char* name = SDL_JoystickName(js);

      int num_axes = SDL_JoystickNumAxes(js);
      int num_buttons = SDL_JoystickNumButtons(js);
      int num_hats = SDL_JoystickNumHats(js);
      int num_balls = SDL_JoystickNumBalls(js);

      printf("%s \"%s\" axes:%d buttons:%d hats:%d balls:%d\n", 
             guid_str, name,
             num_axes, num_buttons, num_hats, num_balls);

      SDL_JoystickClose(js);
    }
  }

  return 0;
}

To compile sdl2-joystick.c

gcc -o sdl2-joystick sdl2-joystick.c `pkg-config --libs --cflags sdl2`

Execute the binary

./sdl2-joystick