Files
PS2Recomp/ps2xRuntime/src/lib/ps2_pad.cpp
T
Ranieri 8c8a97af65 Feature/mpeg decoder (#120)
* feat: added ffmepg as dependency

* feat: wip decoder video

* feat: some perf and cleanup

* feat:  added generic MPEG stream notification

* feat: CMakeLists.txt in ps2xStudio to configure SDL2 build options for static linking.
fix: fix ffmpeg setup for linux
fix: now MPEG decoder now identify that movie has ended and can play again anytime
feat: better audio stub to not block games

* feat: fix expansion test

* feat: foo

* a

* feat: finally added a helper to to prevent thread starvation

* feat: added basic  vu0 code execution

* feat: added yield Guest Execution After Wake to prevent deadlock

* feat: added  options  on  cmake for logs
feat: better input for keyboard pad

* feat: small corrections like top and itop vu branches etc

* feat: changes

* feat: working feature

* feat: fatal frame iop

* feat: test fix
feat: z buffer fix

* fix: gix GsPutIMR IMR

* feat: added rl imgui

* feat: added helper to get snapshot

* feat: added debug panel consuming snapshots

* feat: added pad snapshot
feat: added RCP debug events

* feat: final cleanup from old code

* feat: added EE timer counter
feat: applyed sound driver for Lotr
feat: better check for sound driver compat layout
feat: enquee and cosumed DMa cause
feat: added Pad execCMd
feat: update GS vsync signal flag
feat: custom IOPs for LotR

* feat: small cleanups

* fix: fix wrong import
2026-06-26 21:04:39 -03:00

126 lines
4.7 KiB
C++

#include "runtime/ps2_pad.h"
#include "ps2_host_backend.h"
#include <cstring>
namespace
{
constexpr uint8_t kPadAnalogMarker = 0x73;
constexpr uint8_t kPadStickCenter = 0x80;
constexpr uint16_t PAD_LEFT = 0x0080u;
constexpr uint16_t PAD_DOWN = 0x0040u;
constexpr uint16_t PAD_RIGHT = 0x0020u;
constexpr uint16_t PAD_UP = 0x0010u;
constexpr uint16_t PAD_START = 0x0008u;
constexpr uint16_t PAD_R3 = 0x0004u;
constexpr uint16_t PAD_L3 = 0x0002u;
constexpr uint16_t PAD_SELECT = 0x0001u;
constexpr uint16_t PAD_SQUARE = 0x8000u;
constexpr uint16_t PAD_CROSS = 0x4000u;
constexpr uint16_t PAD_CIRCLE = 0x2000u;
constexpr uint16_t PAD_TRIANGLE = 0x1000u;
constexpr uint16_t PAD_R1 = 0x0800u;
constexpr uint16_t PAD_L1 = 0x0400u;
constexpr uint16_t PAD_R2 = 0x0200u;
constexpr uint16_t PAD_L2 = 0x0100u;
}
bool PSPadBackend::readState(int /*port*/, int /*slot*/, uint8_t *data, size_t size)
{
if (!data || size < 32)
return false;
std::memset(data, 0, 32);
data[0] = 0x01;
data[1] = kPadAnalogMarker;
data[2] = 0xFF;
data[3] = 0xFF;
data[4] = data[5] = data[6] = data[7] = kPadStickCenter;
uint16_t btns = 0xFFFFu;
constexpr int kGamepad = 0;
const bool useGamepad = IsGamepadAvailable(kGamepad);
auto clearBit = [&btns](uint16_t mask)
{ btns &= ~mask; };
if (useGamepad)
{
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_LEFT_FACE_UP))
clearBit(PAD_UP);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_LEFT_FACE_DOWN))
clearBit(PAD_DOWN);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_LEFT_FACE_LEFT))
clearBit(PAD_LEFT);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_LEFT_FACE_RIGHT))
clearBit(PAD_RIGHT);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_RIGHT_FACE_DOWN))
clearBit(PAD_CROSS);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_RIGHT_FACE_RIGHT))
clearBit(PAD_CIRCLE);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_RIGHT_FACE_LEFT))
clearBit(PAD_SQUARE);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_RIGHT_FACE_UP))
clearBit(PAD_TRIANGLE);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_LEFT_TRIGGER_1))
clearBit(PAD_L1);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_RIGHT_TRIGGER_1))
clearBit(PAD_R1);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_LEFT_TRIGGER_2))
clearBit(PAD_L2);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_RIGHT_TRIGGER_2))
clearBit(PAD_R2);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_MIDDLE_RIGHT))
clearBit(PAD_START);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_MIDDLE_LEFT))
clearBit(PAD_SELECT);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_LEFT_THUMB))
clearBit(PAD_L3);
if (IsGamepadButtonDown(kGamepad, GAMEPAD_BUTTON_RIGHT_THUMB))
clearBit(PAD_R3);
float lx = GetGamepadAxisMovement(kGamepad, GAMEPAD_AXIS_LEFT_X);
float ly = GetGamepadAxisMovement(kGamepad, GAMEPAD_AXIS_LEFT_Y);
float rx = GetGamepadAxisMovement(kGamepad, GAMEPAD_AXIS_RIGHT_X);
float ry = GetGamepadAxisMovement(kGamepad, GAMEPAD_AXIS_RIGHT_Y);
data[6] = static_cast<uint8_t>(128 + lx * 127);
data[7] = static_cast<uint8_t>(128 + ly * 127);
data[4] = static_cast<uint8_t>(128 + rx * 127);
data[5] = static_cast<uint8_t>(128 + ry * 127);
}
else
{
if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W))
clearBit(PAD_UP);
if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S))
clearBit(PAD_DOWN);
if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A))
clearBit(PAD_LEFT);
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D))
clearBit(PAD_RIGHT);
if (IsKeyDown(KEY_X) || IsKeyDown(KEY_SPACE))
clearBit(PAD_CROSS);
if (IsKeyDown(KEY_C) || IsKeyDown(KEY_ESCAPE))
clearBit(PAD_CIRCLE);
if (IsKeyDown(KEY_Z) || IsKeyDown(KEY_KP_0))
clearBit(PAD_SQUARE);
if (IsKeyDown(KEY_V) || IsKeyDown(KEY_KP_1))
clearBit(PAD_TRIANGLE);
if (IsKeyDown(KEY_Q))
clearBit(PAD_L1);
if (IsKeyDown(KEY_E))
clearBit(PAD_R1);
if (IsKeyDown(KEY_LEFT_SHIFT))
clearBit(PAD_L2);
if (IsKeyDown(KEY_RIGHT_SHIFT))
clearBit(PAD_R2);
if (IsKeyDown(KEY_ENTER))
clearBit(PAD_START);
if (IsKeyDown(KEY_TAB))
clearBit(PAD_SELECT);
}
data[2] = static_cast<uint8_t>(btns & 0xFF);
data[3] = static_cast<uint8_t>(btns >> 8);
return true;
}