diff --git a/src/sdl_input.c b/src/sdl_input.c index 8a818a2..2229341 100644 --- a/src/sdl_input.c +++ b/src/sdl_input.c @@ -20,23 +20,25 @@ static int sdl_match_gamepad(SDL_JoystickID id, void* data) { SDL_GameControllerGetJoystick(gamepad)) == id); } - +/* static int sdl_event_filter(void*, SDL_Event* event) { return ( SDL_QUIT == event->type || SDL_KEYDOWN == event->type || SDL_KEYUP == event->type || + SDL_CONTROLLERBUTTONDOWN == event->type || + SDL_CONTROLLERBUTTONUP == event->type || SDL_CONTROLLERDEVICEADDED == event->type || SDL_CONTROLLERDEVICEREMOVED == event->type ); } - +*/ static int sdl_input_init(nes_Input_Reader* reader) { int status = SDL_Init(SDL_INIT_EVENTS | SDL_INIT_GAMECONTROLLER); if (status == 0) { reader->data = sdl_find_gamepad(); - SDL_SetEventFilter(sdl_event_filter, NULL); +// SDL_SetEventFilter(sdl_event_filter, NULL); } return status; @@ -57,13 +59,7 @@ static const int sdl_keycodes[nes_controller_num_buttons] = { SDLK_RIGHT, }; -static int button_index(int keycode) { - int index = nes_controller_num_buttons - 1; - for ( ; index > 0 && keycode != sdl_keycodes[index]; --index); - return index; -} - -static const int sdl_gamepad_buttons[nes_controller_num_buttons] = { +static const int sdl_buttons[nes_controller_num_buttons] = { SDL_CONTROLLER_BUTTON_A, SDL_CONTROLLER_BUTTON_B, SDL_CONTROLLER_BUTTON_BACK, @@ -74,6 +70,12 @@ static const int sdl_gamepad_buttons[nes_controller_num_buttons] = { SDL_CONTROLLER_BUTTON_DPAD_RIGHT, }; +static int button_index(int keycode, const int* codes) { + int index = nes_controller_num_buttons - 1; + for ( ; index >= 0 && keycode != codes[index]; --index); + return index; +} + static int sdl_input_update(nes_Input_Reader* reader, nes_input* input) { int status = 0; @@ -86,12 +88,28 @@ static int sdl_input_update(nes_Input_Reader* reader, } else if ( SDL_KEYDOWN == event.type || SDL_KEYUP == event.type) { - int index = button_index(event.key.keysym.sym); - uint8_t mask = (1 << index); - if (SDL_KEYDOWN == event.type) { - input->controllers[0].buttons |= mask; - } else { - input->controllers[0].buttons &= ~mask; + int index = button_index(event.key.keysym.sym, + sdl_keycodes); + if (index >= 0) { + uint8_t mask = (1 << index); + if (SDL_KEYDOWN == event.type) { + input->controllers[0].buttons |= mask; + } else { + input->controllers[0].buttons &= ~mask; + } + } + + } else if ( SDL_CONTROLLERBUTTONDOWN == event.type || + SDL_CONTROLLERBUTTONUP == event.type) { + int index = button_index(event.cbutton.button, + sdl_buttons); + if (index >= 0) { + uint8_t mask = (1 << index); + if (SDL_CONTROLLERBUTTONDOWN == event.type) { + input->controllers[0].buttons |= mask; + } else { + input->controllers[0].buttons &= ~mask; + } } } else if (SDL_CONTROLLERDEVICEADDED == event.type) { @@ -108,19 +126,6 @@ static int sdl_input_update(nes_Input_Reader* reader, } } - if (NULL != reader->data) { - SDL_GameController* gamepad = (SDL_GameController*)reader->data; - - for (int b = 0; b < nes_controller_num_buttons; ++b) { - if (SDL_GameControllerGetButton( - gamepad, sdl_gamepad_buttons[b])) { - input->controllers[0].buttons |= (1 << b); - } else { - input->controllers[0].buttons &= ~(1 << b); - } - } - } - return status; }