Last active
July 15, 2020 08:20
-
-
Save igormorgado/687ae0da0a0359c3d16ef25340dfc33c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SDL2/SDL.h> | |
int main(void) { | |
SDL_Init(SDL_INIT_EVERYTHING); | |
SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt"); | |
SDL_Window *window = SDL_CreateWindow("Controllers", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN); | |
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); | |
SDL_bool is_running = SDL_TRUE; | |
SDL_GameController *ctrl; | |
SDL_Joystick *joystick; | |
SDL_JoystickPowerLevel plevel; | |
SDL_Event event; | |
const char *name; | |
while(is_running == SDL_TRUE) | |
{ | |
while(SDL_PollEvent(&event)) | |
{ | |
switch(event.type) | |
{ | |
case SDL_QUIT: | |
is_running = SDL_FALSE; | |
break; | |
case SDL_KEYDOWN: | |
if (event.key.keysym.sym == SDLK_ESCAPE) { | |
is_running = SDL_FALSE; | |
} | |
break; | |
case SDL_CONTROLLERDEVICEADDED: | |
name = SDL_JoystickNameForIndex(event.cdevice.which); | |
if (name && SDL_strstr(name, "Motion Sensors")) { break; } | |
ctrl = SDL_GameControllerOpen(event.cdevice.which); | |
if(ctrl) | |
{ | |
joystick = SDL_GameControllerGetJoystick (ctrl); | |
plevel = SDL_JoystickCurrentPowerLevel(joystick); | |
if (plevel == SDL_JOYSTICK_POWER_UNKNOWN) | |
{ | |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, | |
"%s: SDL_JoystickCurrentPowerLevel(): %s\n", | |
__func__, SDL_GetError()); | |
} | |
SDL_Log("Controller %d opened - PowerLevel %d\n", event.cdevice.which, plevel); | |
} else { | |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, | |
"%s: SDL_GameControllerOpen(%d): %s\n", | |
__func__, event.cdevice.which, SDL_GetError()); | |
} | |
break; | |
case SDL_CONTROLLERDEVICEREMOVED: | |
SDL_Log("Controller %d detached.\n", (int) event.cdevice.which); | |
ctrl = SDL_GameControllerFromInstanceID(event.cdevice.which); | |
if(ctrl) | |
{ | |
SDL_GameControllerClose(ctrl); | |
SDL_Log("Controller %d closed.\n", (int) event.cdevice.which); | |
} else { | |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, | |
"%s: SDL_GameControllerFromInstanceID(%d): %s\n", | |
__func__, event.cdevice.which, SDL_GetError()); | |
} | |
break; | |
case SDL_CONTROLLERBUTTONDOWN: | |
case SDL_CONTROLLERBUTTONUP: | |
SDL_Log("Controller %d button '%s' %s\n", | |
event.cdevice.which, | |
SDL_GameControllerGetStringForButton((SDL_GameControllerButton)event.cbutton.button), | |
event.cbutton.state ? "pressed" : "released"); | |
break; | |
/* | |
* JOYSTICK | |
*/ | |
case SDL_JOYDEVICEADDED: | |
/* This device is a Game Controller we will handle elsewhere */ | |
if (SDL_IsGameController(event.jdevice.which)) { | |
SDL_Log("JOYDEVADD: Guard triggered\n"); | |
break; } | |
SDL_Log("Joystick %d found.\n", event.jdevice.which ); | |
joystick = SDL_JoystickOpen(event.jdevice.which); | |
if(joystick) { | |
plevel = SDL_JoystickCurrentPowerLevel(joystick); | |
if (plevel == SDL_JOYSTICK_POWER_UNKNOWN) | |
{ | |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, | |
"%s: SDL_JoystickCurrentPowerLevel(): %s\n", | |
__func__, SDL_GetError()); | |
} | |
SDL_Log("Joystick %d opened - PowerLevel %d\n", event.jdevice.which, plevel); | |
} else { | |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, | |
"%s: SDL_JoystickOpen(%d): %s\n", | |
__func__, event.jdevice.which, SDL_GetError()); | |
} | |
break; | |
case SDL_JOYDEVICEREMOVED: | |
/* | |
* This guard is necessary for two reasons: | |
* 1. If a joystick event was received but we will handle | |
* as a GameController; | |
* 2. If it was ALREADY handled as game controller, hence | |
* we will not be able to fetch the controller or the | |
* joystick id | |
*/ | |
joystick = SDL_JoystickFromInstanceID(event.jdevice.which); | |
if (SDL_GameControllerFromInstanceID(event.jdevice.which) || !joystick) { | |
SDL_Log("JOYDEVREM: Guard triggered\n"); | |
break; } | |
SDL_Log("Joystick %d dettached.\n", event.jdevice.which); | |
if(joystick) | |
{ | |
SDL_JoystickClose(joystick); | |
SDL_Log("Joystick %d closed.\n", (int) event.jdevice.which); | |
} else { | |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, | |
"%s: SDL_JoystickFromInstanceID(%d): %s\n", | |
__func__, event.jdevice.which, SDL_GetError()); | |
} | |
break; | |
case SDL_JOYBUTTONDOWN: | |
case SDL_JOYBUTTONUP: | |
/* This device is a Game Controller we will handle elsewhere */ | |
if (SDL_GameControllerFromInstanceID(event.jdevice.which)) { | |
SDL_Log("JOYDEVBUT: Guard triggered\n"); | |
break; } | |
SDL_Log("Joystick %d - Button: %d %s\n", | |
event.jdevice.which, | |
event.jbutton.button, | |
event.jbutton.state ? "pressed" : "released"); | |
break; | |
default: | |
break; | |
} | |
} | |
SDL_RenderClear(renderer); | |
SDL_RenderPresent(renderer); | |
SDL_Delay(10); /* Keep CPU cool */ | |
} | |
if (renderer) SDL_DestroyRenderer(renderer); | |
if (window) SDL_DestroyWindow(window); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment