From 4e6e90dfb9c42151ef9d79493d694068eabc629d Mon Sep 17 00:00:00 2001 From: counter185 <33550839+counter185@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:48:50 +0200 Subject: [PATCH 1/7] add touchpad support, auto reconnect --- src/core/libraries/pad/pad.cpp | 11 ++++++----- src/input/controller.cpp | 16 ++++++++++++++-- src/input/controller.h | 8 ++++++++ src/sdl_window.cpp | 16 ++++++++++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/core/libraries/pad/pad.cpp b/src/core/libraries/pad/pad.cpp index 5bf9c3af..b617f735 100644 --- a/src/core/libraries/pad/pad.cpp +++ b/src/core/libraries/pad/pad.cpp @@ -309,12 +309,13 @@ int PS4_SYSV_ABI scePadRead(s32 handle, OrbisPadData* pData, s32 num) { pData[i].angularVelocity.x = 0.0f; pData[i].angularVelocity.y = 0.0f; pData[i].angularVelocity.z = 0.0f; - pData[i].touchData.touchNum = 0; - pData[i].touchData.touch[0].x = 0; - pData[i].touchData.touch[0].y = 0; + pData[i].touchData.touchNum = (states[i].touchpad[0].state ? 1 : 0) + + (states[i].touchpad[1].state ? 1 : 0); + pData[i].touchData.touch[0].x = states[i].touchpad[0].x; + pData[i].touchData.touch[0].y = states[i].touchpad[0].y; pData[i].touchData.touch[0].id = 1; - pData[i].touchData.touch[1].x = 0; - pData[i].touchData.touch[1].y = 0; + pData[i].touchData.touch[1].x = states[i].touchpad[1].x; + pData[i].touchData.touch[1].y = states[i].touchpad[1].y; pData[i].touchData.touch[1].id = 2; pData[i].connected = connected; pData[i].timestamp = states[i].time; diff --git a/src/input/controller.cpp b/src/input/controller.cpp index 4a3db163..cb4529a7 100644 --- a/src/input/controller.cpp +++ b/src/input/controller.cpp @@ -132,15 +132,27 @@ bool GameController::SetVibration(u8 smallMotor, u8 largeMotor) { return true; } +void GameController::SetTouchpadState(int touchIndex, bool touchDown, float x, float y) { + if (touchIndex < 2) { // DS4 has 2-point multitouch + auto state = GetLastState(); + + state.time = Libraries::Kernel::sceKernelGetProcessTime(); + state.touchpad[touchIndex].state = touchDown; + state.touchpad[touchIndex].x = static_cast(x * 1920); + state.touchpad[touchIndex].y = static_cast(y * 1080); + AddState(state); + } +} + void GameController::TryOpenSDLController() { if (m_sdl_gamepad == nullptr || !SDL_GamepadConnected(m_sdl_gamepad)) { int gamepad_count; SDL_JoystickID* gamepads = SDL_GetGamepads(&gamepad_count); m_sdl_gamepad = gamepad_count > 0 ? SDL_OpenGamepad(gamepads[0]) : nullptr; SDL_free(gamepads); - } - SetLightBarRGB(0, 0, 255); + SetLightBarRGB(0, 0, 255); + } } } // namespace Input diff --git a/src/input/controller.h b/src/input/controller.h index ef099156..cb9fe172 100644 --- a/src/input/controller.h +++ b/src/input/controller.h @@ -21,10 +21,17 @@ enum class Axis { AxisMax }; +struct TouchpadEntry { + bool state; + u16 x; + u16 y; +}; + struct State { u32 buttonsState = 0; u64 time = 0; int axes[static_cast(Axis::AxisMax)] = {128, 128, 128, 128, 0, 0}; + TouchpadEntry touchpad[2] = {{false, 0, 0}, {false, 0, 0}}; }; inline int GetAxis(int min, int max, int value) { @@ -47,6 +54,7 @@ public: void Axis(int id, Input::Axis axis, int value); void SetLightBarRGB(u8 r, u8 g, u8 b); bool SetVibration(u8 smallMotor, u8 largeMotor); + void SetTouchpadState(int touchIndex, bool touchDown, float x, float y); void TryOpenSDLController(); private: diff --git a/src/sdl_window.cpp b/src/sdl_window.cpp index ff44e6c3..b83afd29 100644 --- a/src/sdl_window.cpp +++ b/src/sdl_window.cpp @@ -98,6 +98,11 @@ void WindowSDL::waitEvent() { case SDL_EVENT_GAMEPAD_BUTTON_DOWN: case SDL_EVENT_GAMEPAD_BUTTON_UP: case SDL_EVENT_GAMEPAD_AXIS_MOTION: + case SDL_EVENT_GAMEPAD_ADDED: + case SDL_EVENT_GAMEPAD_REMOVED: + case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN: + case SDL_EVENT_GAMEPAD_TOUCHPAD_UP: + case SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION: onGamepadEvent(&event); break; case SDL_EVENT_QUIT: @@ -299,6 +304,17 @@ void WindowSDL::onGamepadEvent(const SDL_Event* event) { u32 button = 0; Input::Axis axis = Input::Axis::AxisMax; switch (event->type) { + case SDL_EVENT_GAMEPAD_ADDED: + case SDL_EVENT_GAMEPAD_REMOVED: + controller->TryOpenSDLController(); + break; + case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN: + case SDL_EVENT_GAMEPAD_TOUCHPAD_UP: + case SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION: + controller->SetTouchpadState(event->gtouchpad.finger, + event->type != SDL_EVENT_GAMEPAD_TOUCHPAD_UP, + event->gtouchpad.x, event->gtouchpad.y); + break; case SDL_EVENT_GAMEPAD_BUTTON_DOWN: case SDL_EVENT_GAMEPAD_BUTTON_UP: button = sdlGamepadToOrbisButton(event->gbutton.button); From 698dade8645fdc542409d6ba1a6d2e1110bbfa11 Mon Sep 17 00:00:00 2001 From: counter185 <33550839+counter185@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:53:07 +0200 Subject: [PATCH 2/7] * --- src/input/controller.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/input/controller.cpp b/src/input/controller.cpp index cb4529a7..ed415e23 100644 --- a/src/input/controller.cpp +++ b/src/input/controller.cpp @@ -134,12 +134,14 @@ bool GameController::SetVibration(u8 smallMotor, u8 largeMotor) { void GameController::SetTouchpadState(int touchIndex, bool touchDown, float x, float y) { if (touchIndex < 2) { // DS4 has 2-point multitouch + std::scoped_lock lock{m_mutex}; auto state = GetLastState(); - state.time = Libraries::Kernel::sceKernelGetProcessTime(); + state.touchpad[touchIndex].state = touchDown; state.touchpad[touchIndex].x = static_cast(x * 1920); state.touchpad[touchIndex].y = static_cast(y * 1080); + AddState(state); } } From 0b24ac6991b8829f2d2dda8c903b0af81bcfb5b5 Mon Sep 17 00:00:00 2001 From: counter185 <33550839+counter185@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:56:49 +0200 Subject: [PATCH 3/7] actually get rid of that comment --- src/input/controller.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input/controller.cpp b/src/input/controller.cpp index ed415e23..96273555 100644 --- a/src/input/controller.cpp +++ b/src/input/controller.cpp @@ -133,7 +133,7 @@ bool GameController::SetVibration(u8 smallMotor, u8 largeMotor) { } void GameController::SetTouchpadState(int touchIndex, bool touchDown, float x, float y) { - if (touchIndex < 2) { // DS4 has 2-point multitouch + if (touchIndex < 2) { std::scoped_lock lock{m_mutex}; auto state = GetLastState(); state.time = Libraries::Kernel::sceKernelGetProcessTime(); From 15fc267f0d8075bbec4c9a68ea878b14dd1c4dda Mon Sep 17 00:00:00 2001 From: counter185 <33550839+counter185@users.noreply.github.com> Date: Wed, 28 Aug 2024 13:58:37 +0200 Subject: [PATCH 4/7] Update controller.h --- src/input/controller.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/input/controller.h b/src/input/controller.h index cb9fe172..0a0d663a 100644 --- a/src/input/controller.h +++ b/src/input/controller.h @@ -22,9 +22,9 @@ enum class Axis { }; struct TouchpadEntry { - bool state; - u16 x; - u16 y; + bool state{}; + u16 x{}; + u16 y{}; }; struct State { From 89bdd3bba20444e1a9627f04b967834d5c8fef02 Mon Sep 17 00:00:00 2001 From: counter185 <33550839+counter185@users.noreply.github.com> Date: Wed, 28 Aug 2024 14:15:01 +0200 Subject: [PATCH 5/7] it's actually 1920x941 --- src/input/controller.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input/controller.cpp b/src/input/controller.cpp index 96273555..4de6d83b 100644 --- a/src/input/controller.cpp +++ b/src/input/controller.cpp @@ -140,7 +140,7 @@ void GameController::SetTouchpadState(int touchIndex, bool touchDown, float x, f state.touchpad[touchIndex].state = touchDown; state.touchpad[touchIndex].x = static_cast(x * 1920); - state.touchpad[touchIndex].y = static_cast(y * 1080); + state.touchpad[touchIndex].y = static_cast(y * 941); AddState(state); } From 2e2f3bb2c682d0305a69f94b29db93482b051161 Mon Sep 17 00:00:00 2001 From: counter185 <33550839+counter185@users.noreply.github.com> Date: Wed, 28 Aug 2024 14:22:59 +0200 Subject: [PATCH 6/7] clang format --- src/core/libraries/pad/pad.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/libraries/pad/pad.cpp b/src/core/libraries/pad/pad.cpp index b617f735..01cc85cc 100644 --- a/src/core/libraries/pad/pad.cpp +++ b/src/core/libraries/pad/pad.cpp @@ -309,8 +309,8 @@ int PS4_SYSV_ABI scePadRead(s32 handle, OrbisPadData* pData, s32 num) { pData[i].angularVelocity.x = 0.0f; pData[i].angularVelocity.y = 0.0f; pData[i].angularVelocity.z = 0.0f; - pData[i].touchData.touchNum = (states[i].touchpad[0].state ? 1 : 0) - + (states[i].touchpad[1].state ? 1 : 0); + pData[i].touchData.touchNum = + (states[i].touchpad[0].state ? 1 : 0) + (states[i].touchpad[1].state ? 1 : 0); pData[i].touchData.touch[0].x = states[i].touchpad[0].x; pData[i].touchData.touch[0].y = states[i].touchpad[0].y; pData[i].touchData.touch[0].id = 1; From bc661039c541d240c7bb2a4cf6bff9dc66c88c71 Mon Sep 17 00:00:00 2001 From: counter185 <33550839+counter185@users.noreply.github.com> Date: Wed, 28 Aug 2024 14:25:07 +0200 Subject: [PATCH 7/7] get rid of one space --- src/core/libraries/pad/pad.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/libraries/pad/pad.cpp b/src/core/libraries/pad/pad.cpp index 01cc85cc..a0abfdf7 100644 --- a/src/core/libraries/pad/pad.cpp +++ b/src/core/libraries/pad/pad.cpp @@ -309,7 +309,7 @@ int PS4_SYSV_ABI scePadRead(s32 handle, OrbisPadData* pData, s32 num) { pData[i].angularVelocity.x = 0.0f; pData[i].angularVelocity.y = 0.0f; pData[i].angularVelocity.z = 0.0f; - pData[i].touchData.touchNum = + pData[i].touchData.touchNum = (states[i].touchpad[0].state ? 1 : 0) + (states[i].touchpad[1].state ? 1 : 0); pData[i].touchData.touch[0].x = states[i].touchpad[0].x; pData[i].touchData.touch[0].y = states[i].touchpad[0].y;