add touchpad support, auto reconnect

This commit is contained in:
counter185 2024-08-28 13:48:50 +02:00
parent be49871c68
commit 4e6e90dfb9
4 changed files with 44 additions and 7 deletions

View File

@ -309,12 +309,13 @@ int PS4_SYSV_ABI scePadRead(s32 handle, OrbisPadData* pData, s32 num) {
pData[i].angularVelocity.x = 0.0f; pData[i].angularVelocity.x = 0.0f;
pData[i].angularVelocity.y = 0.0f; pData[i].angularVelocity.y = 0.0f;
pData[i].angularVelocity.z = 0.0f; pData[i].angularVelocity.z = 0.0f;
pData[i].touchData.touchNum = 0; pData[i].touchData.touchNum = (states[i].touchpad[0].state ? 1 : 0)
pData[i].touchData.touch[0].x = 0; + (states[i].touchpad[1].state ? 1 : 0);
pData[i].touchData.touch[0].y = 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[0].id = 1;
pData[i].touchData.touch[1].x = 0; pData[i].touchData.touch[1].x = states[i].touchpad[1].x;
pData[i].touchData.touch[1].y = 0; pData[i].touchData.touch[1].y = states[i].touchpad[1].y;
pData[i].touchData.touch[1].id = 2; pData[i].touchData.touch[1].id = 2;
pData[i].connected = connected; pData[i].connected = connected;
pData[i].timestamp = states[i].time; pData[i].timestamp = states[i].time;

View File

@ -132,15 +132,27 @@ bool GameController::SetVibration(u8 smallMotor, u8 largeMotor) {
return true; 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<u16>(x * 1920);
state.touchpad[touchIndex].y = static_cast<u16>(y * 1080);
AddState(state);
}
}
void GameController::TryOpenSDLController() { void GameController::TryOpenSDLController() {
if (m_sdl_gamepad == nullptr || !SDL_GamepadConnected(m_sdl_gamepad)) { if (m_sdl_gamepad == nullptr || !SDL_GamepadConnected(m_sdl_gamepad)) {
int gamepad_count; int gamepad_count;
SDL_JoystickID* gamepads = SDL_GetGamepads(&gamepad_count); SDL_JoystickID* gamepads = SDL_GetGamepads(&gamepad_count);
m_sdl_gamepad = gamepad_count > 0 ? SDL_OpenGamepad(gamepads[0]) : nullptr; m_sdl_gamepad = gamepad_count > 0 ? SDL_OpenGamepad(gamepads[0]) : nullptr;
SDL_free(gamepads); SDL_free(gamepads);
}
SetLightBarRGB(0, 0, 255); SetLightBarRGB(0, 0, 255);
} }
}
} // namespace Input } // namespace Input

View File

@ -21,10 +21,17 @@ enum class Axis {
AxisMax AxisMax
}; };
struct TouchpadEntry {
bool state;
u16 x;
u16 y;
};
struct State { struct State {
u32 buttonsState = 0; u32 buttonsState = 0;
u64 time = 0; u64 time = 0;
int axes[static_cast<int>(Axis::AxisMax)] = {128, 128, 128, 128, 0, 0}; int axes[static_cast<int>(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) { inline int GetAxis(int min, int max, int value) {
@ -47,6 +54,7 @@ public:
void Axis(int id, Input::Axis axis, int value); void Axis(int id, Input::Axis axis, int value);
void SetLightBarRGB(u8 r, u8 g, u8 b); void SetLightBarRGB(u8 r, u8 g, u8 b);
bool SetVibration(u8 smallMotor, u8 largeMotor); bool SetVibration(u8 smallMotor, u8 largeMotor);
void SetTouchpadState(int touchIndex, bool touchDown, float x, float y);
void TryOpenSDLController(); void TryOpenSDLController();
private: private:

View File

@ -98,6 +98,11 @@ void WindowSDL::waitEvent() {
case SDL_EVENT_GAMEPAD_BUTTON_DOWN: case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
case SDL_EVENT_GAMEPAD_BUTTON_UP: case SDL_EVENT_GAMEPAD_BUTTON_UP:
case SDL_EVENT_GAMEPAD_AXIS_MOTION: 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); onGamepadEvent(&event);
break; break;
case SDL_EVENT_QUIT: case SDL_EVENT_QUIT:
@ -299,6 +304,17 @@ void WindowSDL::onGamepadEvent(const SDL_Event* event) {
u32 button = 0; u32 button = 0;
Input::Axis axis = Input::Axis::AxisMax; Input::Axis axis = Input::Axis::AxisMax;
switch (event->type) { 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_DOWN:
case SDL_EVENT_GAMEPAD_BUTTON_UP: case SDL_EVENT_GAMEPAD_BUTTON_UP:
button = sdlGamepadToOrbisButton(event->gbutton.button); button = sdlGamepadToOrbisButton(event->gbutton.button);