#include int main(void) { SDL_Window *window; SDL_Renderer *renderer; SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG); SDL_bool is_running = SDL_TRUE; SDL_Init(SDL_INIT_EVERYTHING); SDL_GameControllerAddMappingsFromFile("gamecontrollerdb.txt"); window = SDL_CreateWindow("Gamepads", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, SDL_WINDOW_SHOWN); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, SDL_ALPHA_OPAQUE); SDL_Event event; const char *name; SDL_GameController *gamecontroller; SDL_Joystick *joystick; SDL_JoystickPowerLevel plevel; 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; /* * GAME CONTROLLER */ case SDL_CONTROLLERDEVICEADDED: name = SDL_JoystickNameForIndex(event.cdevice.which); /* Fixed in SDLBUG 5233 */ if (name && SDL_strstr(name, "Motion Sensors")) { break; } gamecontroller = SDL_GameControllerOpen(event.cdevice.which); if(gamecontroller) { joystick = SDL_GameControllerGetJoystick (gamecontroller); 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); gamecontroller = SDL_GameControllerFromInstanceID(event.cdevice.which); if(gamecontroller) { SDL_GameControllerClose(gamecontroller); SDL_Log("Game controller device %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_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "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_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "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_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "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(1); /* Keep CPU cool */ } SDL_Log("Closing application\n"); if (renderer) SDL_DestroyRenderer(renderer); if (window) SDL_DestroyWindow(window); SDL_Quit(); SDL_Log("Bye.\n"); return 0; }