Skip to content

Commit

Permalink
feat: implemented touchpad support%
Browse files Browse the repository at this point in the history
  • Loading branch information
ABeltramo committed Mar 11, 2024
1 parent e70644d commit 0d174f9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/inputtino/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ class PS5Joypad : public Joypad {
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);

void place_finger(int finger_nr, float x, float y, float pressure, int orientation);
void place_finger(int finger_nr, float x, float y);
void release_finger(int finger_nr);

enum MOTION_TYPE : uint8_t {
Expand Down
5 changes: 5 additions & 0 deletions src/uhid/include/uhid/ps5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ static constexpr unsigned char ps5_pairing_info[] = {
};

struct dualsense_touch_point {
/*
* Status of a DualShock4 touch point contact.
* Contact IDs, with highest bit set are 'inactive'
* and any associated data is then invalid.
*/
uint8_t contact;
uint8_t x_lo;
uint8_t x_hi : 4, y_lo : 4;
Expand Down
28 changes: 25 additions & 3 deletions src/uhid/joypad_ps5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ std::vector<std::string> PS5Joypad::get_nodes() const {
// TODO
return std::vector<std::string>();
}

void PS5Joypad::set_pressed_buttons(int pressed) {
{ // First reset everything to non-pressed
this->_state->current_state.buttons[0] = 0;
Expand Down Expand Up @@ -270,17 +271,38 @@ void PS5Joypad::set_motion(PS5Joypad::MOTION_TYPE type, float x, float y, float
}
}
}

void PS5Joypad::set_battery(PS5Joypad::BATTERY_STATE state, int percentage) {
// TODO
}

void PS5Joypad::set_on_led(const std::function<void(int, int, int)> &callback) {
this->_state->on_led = callback;
}
void PS5Joypad::place_finger(int finger_nr, float x, float y, float pressure, int orientation) {
// TODO

void PS5Joypad::place_finger(int finger_nr, float x, float y) {
if (finger_nr <= 1) {
this->_state->current_state.points[finger_nr].contact = 0x0;

uint8_t *array;

array = reinterpret_cast<uint8_t *>(&x);
this->_state->current_state.points[finger_nr].x_lo = array[0];
this->_state->current_state.points[finger_nr].x_hi = array[3];

array = reinterpret_cast<uint8_t *>(&y);
this->_state->current_state.points[finger_nr].y_lo = array[0];
this->_state->current_state.points[finger_nr].y_hi = array[3];

send_report(*this->_state);
}
}

void PS5Joypad::release_finger(int finger_nr) {
// TODO
if (finger_nr <= 1) {
this->_state->current_state.points[finger_nr].contact = 0xFF;
send_report(*this->_state);
}
}

} // namespace inputtino
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ if (UNIX AND NOT APPLE)
option(SDL_CUSTOM_SRC "Use a custom SDL source location (useful to better debug)" OFF)
if (SDL_CUSTOM_SRC)
SET(SDL_TEST OFF)
SET(SDL_HIDAPI_JOYSTICK ON)
add_subdirectory(${SDL_CUSTOM_SRC} ${CMAKE_CURRENT_BINARY_DIR}/sdl EXCLUDE_FROM_ALL)
else ()
find_package(SDL2 REQUIRED CONFIG REQUIRED COMPONENTS SDL2)
Expand Down
18 changes: 16 additions & 2 deletions tests/testJoypads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ TEST_CASE_METHOD(SDLTestsFixture, "PS Joypad", "[SDL]") {

std::this_thread::sleep_for(250ms);

// TODO: seems that I can't force it to use HIDAPI, it's picking up sysjoystick which is lacking features
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI, "1");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5, "1");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, "1");
SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED, "1");
Expand Down Expand Up @@ -121,7 +123,8 @@ TEST_CASE_METHOD(SDLTestsFixture, "PS Joypad", "[SDL]") {
}

{ // LED
// Unfortunately LINUX_JoystickSetLED is not implemented in SDL2
// Unfortunately LINUX_JoystickSetLED is not implemented in sysjoystick
// TODO: force hidapi driver
// REQUIRE(SDL_GameControllerHasLED(gc));
// struct LED {
// int r;
Expand Down Expand Up @@ -221,10 +224,21 @@ TEST_CASE_METHOD(SDLTestsFixture, "PS Joypad", "[SDL]") {
REQUIRE_THAT(event.csensor.data[1], WithinAbs(gyro_data[1], 0.001f));
REQUIRE_THAT(event.csensor.data[2], WithinAbs(gyro_data[2], 0.001f));
}
// TODO: test touchpad

{ // Test touchpad
// TODO: sysjoystick is lacking implementation, force hidapi
// REQUIRE(SDL_GameControllerGetNumTouchpads(gc) == 1);

joypad.place_finger(0, 1920, 1080);
joypad.place_finger(1, 1920, 1080);
joypad.release_finger(0);
joypad.release_finger(1);
}
// TODO: test battery

// Adaptive triggers aren't supported by SDL
// see:https://github.com/libsdl-org/SDL/issues/5125#issuecomment-1204261666
// see: HIDAPI_DriverPS5_RumbleJoystickTriggers()

SDL_GameControllerClose(gc);
}
Expand Down

0 comments on commit 0d174f9

Please sign in to comment.