Skip to content

Commit

Permalink
fix: using unsigned int for the button pressed API
Browse files Browse the repository at this point in the history
  • Loading branch information
ABeltramo committed Jun 25, 2024
1 parent aff7519 commit 93a81cb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 26 deletions.
10 changes: 5 additions & 5 deletions include/inputtino/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand All @@ -333,7 +333,7 @@ class XboxOneJoypad : public Joypad {

std::vector<std::string> 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<void(int low_freq, int high_freq)> &callback);
Expand Down Expand Up @@ -361,7 +361,7 @@ class SwitchJoypad : public Joypad {

std::vector<std::string> 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<void(int low_freq, int high_freq)> &callback);
Expand Down Expand Up @@ -390,7 +390,7 @@ class PS5Joypad : public Joypad {

std::vector<std::string> 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<void(int low_freq, int high_freq)> &callback);
Expand Down
2 changes: 1 addition & 1 deletion src/uhid/joypad_ps5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ std::vector<std::string> 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;
Expand Down
2 changes: 1 addition & 1 deletion src/uinput/joypad_nintendo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Result<SwitchJoypad> 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
Expand Down
2 changes: 1 addition & 1 deletion src/uinput/joypad_xbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Result<XboxOneJoypad> 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
Expand Down
56 changes: 38 additions & 18 deletions tests/testJoypads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,34 @@ 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;
}
}
}

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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 93a81cb

Please sign in to comment.