Skip to content

Instantly share code, notes, and snippets.

@igormorgado
Created July 12, 2020 07:42
Show Gist options
  • Save igormorgado/8ce167c009f4f244789a23d628683897 to your computer and use it in GitHub Desktop.
Save igormorgado/8ce167c009f4f244789a23d628683897 to your computer and use it in GitHub Desktop.
#include <SDL2/SDL.h>
void list_joysticks();
int main(void) {
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
int sdl_flags = SDL_INIT_JOYSTICK;
int is_running = 1;
SDL_Init(sdl_flags);
SDL_Event event;
while(is_running == 1)
{
while(SDL_PollEvent(&event)) {
switch(event.type)
{
case SDL_JOYDEVICEADDED:
SDL_Log("Joystick device %d added.\n", (int) event.jdevice.which);
list_joysticks();
break;
case SDL_JOYDEVICEREMOVED:
SDL_Log("Joystick device %d removed.\n", (int) event.jdevice.which);
list_joysticks();
break;
case SDL_QUIT:
is_running = 0;
break;
default:
break;
}
}
}
SDL_Log("Closing application\n");
SDL_Quit();
SDL_Log("Bye.\n");
return 0;
}
void
list_joysticks(void)
{
for (Uint8 i=0; i < SDL_NumJoysticks(); i++)
SDL_Log("JoyID: %d - Name: %s\n", i, SDL_JoystickNameForIndex(i));
}
INFO: Joystick device 0 added.
INFO: JoyID: 0 - Name: Gasia Co.,Ltd PS(R) Gamepad
INFO: JoyID: 1 - Name: Gasia Co.,Ltd PS(R) Gamepad Motion Sensors
INFO: JoyID: 2 - Name: Xbox One S Controller
INFO: JoyID: 3 - Name: Sony Interactive Entertainment Wireless Controller
INFO: Joystick device 1 added.
INFO: JoyID: 0 - Name: Gasia Co.,Ltd PS(R) Gamepad
INFO: JoyID: 1 - Name: Gasia Co.,Ltd PS(R) Gamepad Motion Sensors
INFO: JoyID: 2 - Name: Xbox One S Controller
INFO: JoyID: 3 - Name: Sony Interactive Entertainment Wireless Controller
INFO: Joystick device 2 added.
INFO: JoyID: 0 - Name: Gasia Co.,Ltd PS(R) Gamepad
INFO: JoyID: 1 - Name: Gasia Co.,Ltd PS(R) Gamepad Motion Sensors
INFO: JoyID: 2 - Name: Xbox One S Controller
INFO: JoyID: 3 - Name: Sony Interactive Entertainment Wireless Controller
INFO: Joystick device 3 added.
INFO: JoyID: 0 - Name: Gasia Co.,Ltd PS(R) Gamepad
INFO: JoyID: 1 - Name: Gasia Co.,Ltd PS(R) Gamepad Motion Sensors
INFO: JoyID: 2 - Name: Xbox One S Controller
INFO: JoyID: 3 - Name: Sony Interactive Entertainment Wireless Controller
INFO: Joystick device 2 removed.
INFO: JoyID: 0 - Name: Gasia Co.,Ltd PS(R) Gamepad
INFO: JoyID: 1 - Name: Gasia Co.,Ltd PS(R) Gamepad Motion Sensors
INFO: JoyID: 2 - Name: Sony Interactive Entertainment Wireless Controller
INFO: Joystick device 3 added.
INFO: JoyID: 0 - Name: Gasia Co.,Ltd PS(R) Gamepad
INFO: JoyID: 1 - Name: Gasia Co.,Ltd PS(R) Gamepad Motion Sensors
INFO: JoyID: 2 - Name: Sony Interactive Entertainment Wireless Controller
INFO: JoyID: 3 - Name: Xbox One S Controller
INFO: Joystick device 4 removed.
INFO: JoyID: 0 - Name: Gasia Co.,Ltd PS(R) Gamepad
INFO: JoyID: 1 - Name: Gasia Co.,Ltd PS(R) Gamepad Motion Sensors
INFO: JoyID: 2 - Name: Sony Interactive Entertainment Wireless Controller
INFO: Joystick device 3 added.
INFO: JoyID: 0 - Name: Gasia Co.,Ltd PS(R) Gamepad
INFO: JoyID: 1 - Name: Gasia Co.,Ltd PS(R) Gamepad Motion Sensors
INFO: JoyID: 2 - Name: Sony Interactive Entertainment Wireless Controller
INFO: JoyID: 3 - Name: Xbox One S Controller
INFO: Joystick device 5 removed.
INFO: JoyID: 0 - Name: Gasia Co.,Ltd PS(R) Gamepad
INFO: JoyID: 1 - Name: Gasia Co.,Ltd PS(R) Gamepad Motion Sensors
INFO: JoyID: 2 - Name: Sony Interactive Entertainment Wireless Controller
INFO: Joystick device 3 added.
INFO: JoyID: 0 - Name: Gasia Co.,Ltd PS(R) Gamepad
INFO: JoyID: 1 - Name: Gasia Co.,Ltd PS(R) Gamepad Motion Sensors
INFO: JoyID: 2 - Name: Sony Interactive Entertainment Wireless Controller
INFO: JoyID: 3 - Name: Xbox One S Controller
INFO: Joystick device 6 removed.
INFO: JoyID: 0 - Name: Gasia Co.,Ltd PS(R) Gamepad
INFO: JoyID: 1 - Name: Gasia Co.,Ltd PS(R) Gamepad Motion Sensors
INFO: JoyID: 2 - Name: Sony Interactive Entertainment Wireless Controller
INFO: Joystick device 3 added.
INFO: JoyID: 0 - Name: Gasia Co.,Ltd PS(R) Gamepad
INFO: JoyID: 1 - Name: Gasia Co.,Ltd PS(R) Gamepad Motion Sensors
INFO: JoyID: 2 - Name: Sony Interactive Entertainment Wireless Controller
INFO: JoyID: 3 - Name: Xbox One S Controller
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment