diff --git a/include/inputtino/input.hpp b/include/inputtino/input.hpp index 639affc..b4500ec 100644 --- a/include/inputtino/input.hpp +++ b/include/inputtino/input.hpp @@ -267,7 +267,7 @@ class Keyboard : public VirtualDevice { */ class Joypad : public VirtualDevice { public: - enum CONTROLLER_BTN : int { + enum CONTROLLER_BTN : unsigned int { DPAD_UP = 0x0001, DPAD_DOWN = 0x0002, DPAD_LEFT = 0x0004, @@ -310,7 +310,7 @@ class Joypad : public VirtualDevice { * * Example: previous state had `DPAD_UP` and `A` -> user release `A` -> new state only has `DPAD_UP` */ - virtual void set_pressed_buttons(int newly_pressed) = 0; + virtual void set_pressed_buttons(unsigned int newly_pressed) = 0; virtual void set_triggers(int16_t left, int16_t right) = 0; @@ -333,7 +333,7 @@ class XboxOneJoypad : public Joypad { std::vector get_nodes() const override; - void set_pressed_buttons(int newly_pressed) override; + void set_pressed_buttons(unsigned int newly_pressed) override; void set_triggers(int16_t left, int16_t right) override; void set_stick(STICK_POSITION stick_type, short x, short y) override; void set_on_rumble(const std::function &callback); @@ -361,7 +361,7 @@ class SwitchJoypad : public Joypad { std::vector get_nodes() const override; - void set_pressed_buttons(int newly_pressed) override; + void set_pressed_buttons(unsigned int newly_pressed) override; void set_triggers(int16_t left, int16_t right) override; void set_stick(STICK_POSITION stick_type, short x, short y) override; void set_on_rumble(const std::function &callback); @@ -390,7 +390,7 @@ class PS5Joypad : public Joypad { std::vector get_sys_nodes() const; - void set_pressed_buttons(int newly_pressed) override; + void set_pressed_buttons(unsigned int newly_pressed) override; void set_triggers(int16_t left, int16_t right) override; void set_stick(STICK_POSITION stick_type, short x, short y) override; void set_on_rumble(const std::function &callback); diff --git a/src/uhid/joypad_ps5.cpp b/src/uhid/joypad_ps5.cpp index 41b5a5e..790dede 100644 --- a/src/uhid/joypad_ps5.cpp +++ b/src/uhid/joypad_ps5.cpp @@ -252,7 +252,7 @@ std::vector PS5Joypad::get_nodes() const { return nodes; } -void PS5Joypad::set_pressed_buttons(int pressed) { +void PS5Joypad::set_pressed_buttons(unsigned int pressed) { { // First reset everything to non-pressed this->_state->current_state.buttons[0] = 0; this->_state->current_state.buttons[1] = 0; diff --git a/src/uinput/joypad_nintendo.cpp b/src/uinput/joypad_nintendo.cpp index 2293f0b..ad50edf 100644 --- a/src/uinput/joypad_nintendo.cpp +++ b/src/uinput/joypad_nintendo.cpp @@ -106,7 +106,7 @@ Result SwitchJoypad::create(const DeviceDefinition &device) { return joypad; } -void SwitchJoypad::set_pressed_buttons(int newly_pressed) { +void SwitchJoypad::set_pressed_buttons(unsigned int newly_pressed) { // Button flags that have been changed between current and prev auto bf_changed = newly_pressed ^ this->_state->currently_pressed_btns; // Button flags that are only part of the new packet diff --git a/src/uinput/joypad_xbox.cpp b/src/uinput/joypad_xbox.cpp index e2ead40..7f26249 100644 --- a/src/uinput/joypad_xbox.cpp +++ b/src/uinput/joypad_xbox.cpp @@ -102,7 +102,7 @@ Result XboxOneJoypad::create(const DeviceDefinition &device) { return joypad; } -void XboxOneJoypad::set_pressed_buttons(int newly_pressed) { +void XboxOneJoypad::set_pressed_buttons(unsigned int newly_pressed) { // Button flags that have been changed between current and prev auto bf_changed = newly_pressed ^ this->_state->currently_pressed_btns; // Button flags that are only part of the new packet diff --git a/tests/testJoypads.cpp b/tests/testJoypads.cpp index df5f3e9..5944400 100644 --- a/tests/testJoypads.cpp +++ b/tests/testJoypads.cpp @@ -28,6 +28,23 @@ void flush_sdl_events() { case SDL_CONTROLLERDEVICEREMAPPED: std::cout << "SDL_CONTROLLERDEVICEREMAPPED " << SDL_GameControllerNameForIndex(event.cdevice.which) << std::endl; break; + case SDL_CONTROLLERBUTTONDOWN: + case SDL_CONTROLLERBUTTONUP: + case SDL_JOYBUTTONDOWN: + case SDL_JOYBUTTONUP: + std::cout << "SDL button - " << (event.cbutton.state == SDL_PRESSED ? "pressed " : "released ") + << (int)event.cbutton.button << std::endl; + break; + case SDL_JOYAXISMOTION: + case SDL_CONTROLLERAXISMOTION: + std::cout << "SDL axis - " << (int)event.jaxis.axis << " " << event.jaxis.value << std::endl; + break; + case SDL_JOYHATMOTION: + std::cout << "SDL_JOYHATMOTION " << (int)event.jhat.value << std::endl; + break; + default: + std::cout << "SDL event: " << event.type << "\n"; + break; } } } @@ -35,7 +52,10 @@ void flush_sdl_events() { class SDLTestsFixture { public: SDLTestsFixture() { - SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER | SDL_INIT_SENSOR); + if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER | SDL_INIT_SENSOR | SDL_INIT_EVENTS) < + 0) { + std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl; + } SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE); SDL_GameControllerEventState(SDL_ENABLE); } @@ -52,24 +72,24 @@ class SDLTestsFixture { REQUIRE(SDL_GameControllerGetButton(gc, SDL_BTN) == 1); void test_buttons(SDL_GameController *gc, Joypad &joypad) { - SDL_TEST_BUTTON(Joypad::DPAD_UP, SDL_CONTROLLER_BUTTON_DPAD_UP); - SDL_TEST_BUTTON(Joypad::DPAD_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN); - SDL_TEST_BUTTON(Joypad::DPAD_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT); - SDL_TEST_BUTTON(Joypad::DPAD_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT); + SDL_TEST_BUTTON(Joypad::DPAD_UP, SDL_CONTROLLER_BUTTON_DPAD_UP) + SDL_TEST_BUTTON(Joypad::DPAD_DOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN) + SDL_TEST_BUTTON(Joypad::DPAD_LEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT) + SDL_TEST_BUTTON(Joypad::DPAD_RIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT) - SDL_TEST_BUTTON(Joypad::LEFT_STICK, SDL_CONTROLLER_BUTTON_LEFTSTICK); - SDL_TEST_BUTTON(Joypad::RIGHT_STICK, SDL_CONTROLLER_BUTTON_RIGHTSTICK); - SDL_TEST_BUTTON(Joypad::LEFT_BUTTON, SDL_CONTROLLER_BUTTON_LEFTSHOULDER); - SDL_TEST_BUTTON(Joypad::RIGHT_BUTTON, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); + SDL_TEST_BUTTON(Joypad::LEFT_STICK, SDL_CONTROLLER_BUTTON_LEFTSTICK) + SDL_TEST_BUTTON(Joypad::RIGHT_STICK, SDL_CONTROLLER_BUTTON_RIGHTSTICK) + SDL_TEST_BUTTON(Joypad::LEFT_BUTTON, SDL_CONTROLLER_BUTTON_LEFTSHOULDER) + SDL_TEST_BUTTON(Joypad::RIGHT_BUTTON, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) - SDL_TEST_BUTTON(Joypad::A, SDL_CONTROLLER_BUTTON_A); - SDL_TEST_BUTTON(Joypad::B, SDL_CONTROLLER_BUTTON_B); - SDL_TEST_BUTTON(Joypad::X, SDL_CONTROLLER_BUTTON_X); - SDL_TEST_BUTTON(Joypad::Y, SDL_CONTROLLER_BUTTON_Y); + SDL_TEST_BUTTON(Joypad::A, SDL_CONTROLLER_BUTTON_A) + SDL_TEST_BUTTON(Joypad::B, SDL_CONTROLLER_BUTTON_B) + SDL_TEST_BUTTON(Joypad::X, SDL_CONTROLLER_BUTTON_X) + SDL_TEST_BUTTON(Joypad::Y, SDL_CONTROLLER_BUTTON_Y) - SDL_TEST_BUTTON(Joypad::START, SDL_CONTROLLER_BUTTON_START); - SDL_TEST_BUTTON(Joypad::BACK, SDL_CONTROLLER_BUTTON_BACK); - SDL_TEST_BUTTON(Joypad::HOME, SDL_CONTROLLER_BUTTON_GUIDE); + SDL_TEST_BUTTON(Joypad::START, SDL_CONTROLLER_BUTTON_START) + SDL_TEST_BUTTON(Joypad::BACK, SDL_CONTROLLER_BUTTON_BACK) + SDL_TEST_BUTTON(Joypad::HOME, SDL_CONTROLLER_BUTTON_GUIDE) // Release all buttons joypad.set_pressed_buttons(0); @@ -172,8 +192,8 @@ TEST_CASE_METHOD(SDLTestsFixture, "PS Joypad", "[SDL]") { // REQUIRE(led_data->b == 150); } - test_buttons(gc, joypad); // TODO: fix failing buttons - { // Sticks + test_buttons(gc, joypad); + { // Sticks REQUIRE(SDL_GameControllerHasAxis(gc, SDL_CONTROLLER_AXIS_LEFTX)); REQUIRE(SDL_GameControllerHasAxis(gc, SDL_CONTROLLER_AXIS_LEFTY)); REQUIRE(SDL_GameControllerHasAxis(gc, SDL_CONTROLLER_AXIS_RIGHTX));