#include #include #include "lephisto.h" #include "log.h" #include "joystick.h" static SDL_Joystick* joystick = NULL; int joystick_get(char* namjoystick) { int i; for(i = 0; i < SDL_NumJoysticks(); i++) if(strstr(SDL_JoystickName(i), namjoystick)) return i; WARN("Joystick '%s' not found, using default joystick '%s'", namjoystick, SDL_JoystickName(0)); return 0; } int joystick_use(int indjoystick) { if(indjoystick < 0 || indjoystick >= SDL_NumJoysticks()) { WARN("Joystick of index number %d does not exist. Switching to default (0)", indjoystick); indjoystick = 0; } if(joystick) /* Might as well close it if it is open already. */ SDL_JoystickClose(joystick); /* Start using the joystick. */ LOG("Using joystick %d", indjoystick); joystick = SDL_JoystickOpen(indjoystick); if(joystick == NULL) { WARN("Error opening joystick %d [%s]", indjoystick, SDL_JoystickName(indjoystick)); return -1; } DEBUG("\t\tWith %d axes, %d buttons, %d balls, and %d hats", SDL_JoystickNumAxes(joystick), SDL_JoystickNumButtons(joystick), SDL_JoystickNumBalls(joystick), SDL_JoystickNumHats(joystick)); return 0; } int joystick_init(void) { int numjoysticks, i; /* Init the SDL subsys. */ if(SDL_InitSubSystem(SDL_INIT_JOYSTICK)) { WARN("Unable to init the joystick subsystem."); return -1; } /* Figure out how many joysticks there are. */ numjoysticks = SDL_NumJoysticks(); LOG("%d joystick%s detected", numjoysticks, (numjoysticks==1)?"":"s"); for(i = 0; i < numjoysticks; i++) LOG("\t\t%d. %s", i, SDL_JoystickName(i)); /* Enable joystick events. */ SDL_JoystickEventState(SDL_ENABLE); return 0; } void joystick_exit(void) { SDL_JoystickClose(joystick); }