#include "gamepad.h" #include #include "zx_ula.h" #include #define GAMEPAD_BUTTON_LEFT 0 #define GAMEPAD_BUTTON_RIGHT 1 #define GAMEPAD_BUTTON_UP 2 #define GAMEPAD_BUTTON_DOWN 3 #define GAMEPAD_BUTTON_FIRE 4 namespace gamepad { struct gamepad_t { SDL_GameController *gamepad; int32_t index; uint8_t type; bool buttons[5] { false, false, false, false, false }; }; std::vector gamepads; uint8_t getNextType() { uint8_t next_type = GAMEPAD_TYPE_SINCLAIR_1; bool found = false; while (!found) { found = true; for (auto gamepad : gamepads) { if (gamepad.type == next_type) { found = false; next_type++; if (next_type == GAMEPAD_TYPE_CUSTOM) return next_type; break; } } } return next_type; } uint8_t add(int32_t index, uint8_t type) { if (SDL_IsGameController(index)) { gamepad_t gamepad; gamepad.gamepad = SDL_GameControllerOpen(index); if (SDL_GameControllerGetAttached(gamepad.gamepad) == SDL_TRUE) SDL_GameControllerEventState(SDL_ENABLE); gamepad.index = index; gamepad.type = ( (type == GAMEPAD_TYPE_ANY) ? getNextType() : type ); gamepads.push_back(gamepad); } return gamepads.size()-1; } void modify(int32_t index, uint8_t type) { for (auto& gamepad : gamepads) { if (gamepad.index == index) { gamepad.type = type; return; } } } void remove(int32_t index) { SDL_GameController *game_controller = SDL_GameControllerFromInstanceID(index); for (int i=0; iindex) return gamepads[index].type; return 0; } void setGamepadType(uint8_t index, uint8_t type) { if (gamepads.size()>index) gamepads[index].type = type; } }