mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(pad): add read input state logic to libScePad
This commit is contained in:
@@ -12,9 +12,7 @@ Execution reaches `_start`, [stack unwinding](core/libs/prx/libc/src/exception/U
|
||||
|
||||
The [shader recompiler](core/shader/recompiler/Recompiler.cpp) successfully produces validated via [Spirv-Tools](3rdparty/SPIRV-Tools) SPIR-V.
|
||||
|
||||
The real game reaches the main menu, which renders correctly with buttons and audio.
|
||||
|
||||
Current stop: [libScePad](core/libs/prx/libScePad) does not record the input, so the button cannot be pressed.
|
||||
The real game reaches the logo, main menu, and [gameplay](https://gist.github.com/user-attachments/assets/81d28e9b-c237-4545-b2ca-720071129816) with audio.
|
||||
|
||||
## Build
|
||||
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
add_library(libScePad SHARED EXCLUDE_FROM_ALL
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Export.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/PadState.cpp
|
||||
)
|
||||
target_include_directories(libScePad PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
set_target_properties(libScePad PROPERTIES CXX_EXTENSIONS OFF)
|
||||
configure_windows_unwind(libScePad)
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "SceTypes.hpp"
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include "prx/libScePad/Pad.hpp"
|
||||
#include "prx/libScePad/PadState.hpp"
|
||||
|
||||
extern "C" {
|
||||
|
||||
@@ -68,6 +69,7 @@ int APS5_VABI scePadGetTriggerEffectState(int handle, PadTriggerEffectStateInfor
|
||||
}
|
||||
|
||||
int APS5_VABI scePadInit_nid_postfix(void) {
|
||||
Pad::Initialize();
|
||||
return PAD_OK;
|
||||
}
|
||||
|
||||
@@ -96,30 +98,7 @@ int APS5_VABI scePadReadState(int handle, PadData* data) {
|
||||
if (handle != 1) APS5_INVALID_ARG_EX;
|
||||
if (data == nullptr) APS5_INVALID_ARG_EX;
|
||||
|
||||
std::memset(data, 0, sizeof(*data));
|
||||
|
||||
data->buttons = 0;
|
||||
data->left_stick_x = 128;
|
||||
data->left_stick_y = 128;
|
||||
data->right_stick_x = 128;
|
||||
data->right_stick_y = 128;
|
||||
data->analog_buttons_l2 = 0;
|
||||
data->analog_buttons_r2 = 0;
|
||||
data->acceleration_x = 0.0f;
|
||||
data->acceleration_y = 1.0f;
|
||||
data->acceleration_z = 0.0f;
|
||||
data->angular_velocity_x = 0.0f;
|
||||
data->angular_velocity_y = 0.0f;
|
||||
data->angular_velocity_z = 0.0f;
|
||||
data->orientation_x = 0.0f;
|
||||
data->orientation_y = 0.0f;
|
||||
data->orientation_z = 0.0f;
|
||||
data->orientation_w = 1.0f;
|
||||
data->touch_data_touch_num = 0;
|
||||
data->connected = true;
|
||||
data->timestamp = 0;
|
||||
data->connected_count = 1;
|
||||
data->device_unique_data_len = 0;
|
||||
*data = Pad::ReadState();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
#ifndef CORE_LIBS_PRX_LIBSCEPAD_INPUTMAPPING_HPP
|
||||
#define CORE_LIBS_PRX_LIBSCEPAD_INPUTMAPPING_HPP
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include "SDL_scancode.h"
|
||||
#include "SDL_mouse.h"
|
||||
|
||||
namespace Pad {
|
||||
enum class InputControl {
|
||||
Button,
|
||||
LeftStickLeft,
|
||||
LeftStickRight,
|
||||
LeftStickUp,
|
||||
LeftStickDown,
|
||||
RightStickLeft,
|
||||
RightStickRight,
|
||||
RightStickUp,
|
||||
RightStickDown,
|
||||
TouchLeft,
|
||||
TouchRight,
|
||||
ToggleMouse
|
||||
};
|
||||
|
||||
struct InputBinding {
|
||||
SDL_Scancode key;
|
||||
std::uint8_t mouseButton;
|
||||
InputControl control;
|
||||
std::uint32_t button = 0;
|
||||
int wheelDirection = 0;
|
||||
};
|
||||
|
||||
inline constexpr std::array InputMapping{
|
||||
InputBinding{SDL_SCANCODE_RETURN, 0, InputControl::Button, 0x4000},
|
||||
InputBinding{SDL_SCANCODE_ESCAPE, 0, InputControl::Button, 0x8},
|
||||
InputBinding{SDL_SCANCODE_I, 0, InputControl::Button, 0x1000},
|
||||
InputBinding{SDL_SCANCODE_K, 0, InputControl::Button, 0x8000},
|
||||
InputBinding{SDL_SCANCODE_L, 0, InputControl::Button, 0x2000},
|
||||
InputBinding{SDL_SCANCODE_Q, 0, InputControl::Button, 0x400},
|
||||
InputBinding{SDL_SCANCODE_E, 0, InputControl::Button, 0x800},
|
||||
InputBinding{SDL_SCANCODE_LSHIFT, 0, InputControl::Button, 0x2},
|
||||
InputBinding{SDL_SCANCODE_RSHIFT, 0, InputControl::Button, 0x2},
|
||||
InputBinding{SDL_SCANCODE_LCTRL, 0, InputControl::Button, 0x4},
|
||||
InputBinding{SDL_SCANCODE_RCTRL, 0, InputControl::Button, 0x4},
|
||||
InputBinding{SDL_SCANCODE_UP, 0, InputControl::Button, 0x10},
|
||||
InputBinding{SDL_SCANCODE_RIGHT, 0, InputControl::Button, 0x20},
|
||||
InputBinding{SDL_SCANCODE_DOWN, 0, InputControl::Button, 0x40},
|
||||
InputBinding{SDL_SCANCODE_LEFT, 0, InputControl::Button, 0x80},
|
||||
InputBinding{SDL_SCANCODE_A, 0, InputControl::LeftStickLeft},
|
||||
InputBinding{SDL_SCANCODE_D, 0, InputControl::LeftStickRight},
|
||||
InputBinding{SDL_SCANCODE_W, 0, InputControl::LeftStickUp},
|
||||
InputBinding{SDL_SCANCODE_S, 0, InputControl::LeftStickDown},
|
||||
InputBinding{SDL_SCANCODE_F, 0, InputControl::RightStickLeft},
|
||||
InputBinding{SDL_SCANCODE_H, 0, InputControl::RightStickRight},
|
||||
InputBinding{SDL_SCANCODE_T, 0, InputControl::RightStickUp},
|
||||
InputBinding{SDL_SCANCODE_G, 0, InputControl::RightStickDown},
|
||||
InputBinding{SDL_SCANCODE_BACKSPACE, 0, InputControl::TouchLeft},
|
||||
InputBinding{SDL_SCANCODE_TAB, 0, InputControl::TouchRight},
|
||||
InputBinding{SDL_SCANCODE_UNKNOWN, SDL_BUTTON_MIDDLE, InputControl::ToggleMouse},
|
||||
InputBinding{SDL_SCANCODE_UNKNOWN, SDL_BUTTON_LEFT, InputControl::Button, 0x200},
|
||||
InputBinding{SDL_SCANCODE_UNKNOWN, SDL_BUTTON_RIGHT, InputControl::Button, 0x100},
|
||||
InputBinding{SDL_SCANCODE_UNKNOWN, 0, InputControl::Button, 0x10, 1},
|
||||
InputBinding{SDL_SCANCODE_UNKNOWN, 0, InputControl::Button, 0x40, -1}
|
||||
};
|
||||
|
||||
inline constexpr int MousePollIntervalMs = 33;
|
||||
inline constexpr int WheelPressDurationMs = 80;
|
||||
inline constexpr double MouseSensitivity = 1.0;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,59 @@
|
||||
#include "prx/libScePad/PadState.hpp"
|
||||
#include "prx/libkernel/Time/include/Time.hpp"
|
||||
#include <mutex>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace {
|
||||
std::mutex stateMutex;
|
||||
PadInputState state;
|
||||
std::uint64_t timestamp = 0;
|
||||
std::exception_ptr failure;
|
||||
bool initialized = false;
|
||||
}
|
||||
|
||||
void Pad::Initialize() {
|
||||
std::lock_guard lock(stateMutex);
|
||||
if (failure) std::rethrow_exception(failure);
|
||||
if (!initialized) timestamp = sceKernelGetProcessTime();
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
PadData Pad::ReadState() {
|
||||
std::lock_guard lock(stateMutex);
|
||||
if (failure) std::rethrow_exception(failure);
|
||||
if (!initialized) throw std::runtime_error("Pad: read before initialization");
|
||||
PadData data{};
|
||||
data.buttons = state.buttons;
|
||||
data.left_stick_x = state.sticks[0];
|
||||
data.left_stick_y = state.sticks[1];
|
||||
data.right_stick_x = state.sticks[2];
|
||||
data.right_stick_y = state.sticks[3];
|
||||
data.analog_buttons_l2 = (state.buttons & 0x100) != 0 ? 255 : 0;
|
||||
data.analog_buttons_r2 = (state.buttons & 0x200) != 0 ? 255 : 0;
|
||||
data.acceleration_y = 1.0f;
|
||||
data.orientation_w = 1.0f;
|
||||
data.connected = true;
|
||||
data.connected_count = 1;
|
||||
data.timestamp = timestamp;
|
||||
if (state.touchLeft || state.touchRight) {
|
||||
data.buttons |= 0x100000;
|
||||
data.touch_data_touch_num = 1;
|
||||
data.touch_data_touch0_x = state.touchRight ? 1440 : 480;
|
||||
data.touch_data_touch0_y = 471;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
extern "C" void PadPublishInput_nid_postfix(const PadInputState& input) {
|
||||
std::lock_guard lock(stateMutex);
|
||||
if (failure) std::rethrow_exception(failure);
|
||||
if (state.buttons == input.buttons && state.sticks == input.sticks && state.touchLeft == input.touchLeft && state.touchRight == input.touchRight) return;
|
||||
state = input;
|
||||
timestamp = sceKernelGetProcessTime();
|
||||
}
|
||||
|
||||
extern "C" void PadReportInputFailure_nid_postfix(std::exception_ptr error) {
|
||||
if (!error) throw std::invalid_argument("Pad: missing input failure");
|
||||
std::lock_guard lock(stateMutex);
|
||||
if (!failure) failure = error;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef CORE_LIBS_PRX_LIBSCEPAD_PADSTATE_HPP
|
||||
#define CORE_LIBS_PRX_LIBSCEPAD_PADSTATE_HPP
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <exception>
|
||||
#include "SceTypes.hpp"
|
||||
|
||||
struct PadInputState {
|
||||
std::uint32_t buttons = 0;
|
||||
std::array<std::uint8_t, 4> sticks{128, 128, 128, 128};
|
||||
bool touchLeft = false;
|
||||
bool touchRight = false;
|
||||
};
|
||||
|
||||
namespace Pad {
|
||||
void Initialize();
|
||||
PadData ReadState();
|
||||
}
|
||||
|
||||
extern "C" void PadPublishInput_nid_postfix(const PadInputState& input);
|
||||
extern "C" void PadReportInputFailure_nid_postfix(std::exception_ptr error);
|
||||
|
||||
#endif
|
||||
@@ -4,6 +4,7 @@ add_library(libSceVideoOut SHARED EXCLUDE_FROM_ALL
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/Output.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/VideoOutDriver.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/DisplayWindow.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src/PadInput.cpp
|
||||
)
|
||||
|
||||
target_include_directories(libSceVideoOut PRIVATE
|
||||
@@ -11,7 +12,7 @@ target_include_directories(libSceVideoOut PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/3rdparty/SDL2/include
|
||||
${CMAKE_SOURCE_DIR}/3rdparty/Vulkan-Headers/include
|
||||
)
|
||||
target_link_libraries(libSceVideoOut PRIVATE libSceAgcDriver libc libkernel SDL2-static)
|
||||
target_link_libraries(libSceVideoOut PRIVATE libSceAgcDriver libScePad libc libkernel SDL2-static)
|
||||
if(WIN32)
|
||||
target_link_libraries(libSceVideoOut PRIVATE comctl32)
|
||||
endif()
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef CORE_LIBS_PRX_LIBSCEVIDEOOUT_PADINPUT_HPP
|
||||
#define CORE_LIBS_PRX_LIBSCEVIDEOOUT_PADINPUT_HPP
|
||||
|
||||
#include "SDL_events.h"
|
||||
#include "prx/libScePad/InputMapping.hpp"
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
|
||||
class PadInput {
|
||||
public:
|
||||
void HandleEvent(const SDL_Event& event);
|
||||
void Update();
|
||||
|
||||
private:
|
||||
void publish();
|
||||
void setMouseMode(bool enabled);
|
||||
std::array<bool, Pad::InputMapping.size()> pressed{};
|
||||
std::array<std::chrono::steady_clock::time_point, Pad::InputMapping.size()> wheelReleaseTimes{};
|
||||
std::array<std::uint8_t, 2> mouseStick{128, 128};
|
||||
std::chrono::steady_clock::time_point nextMousePoll{};
|
||||
bool mouseEnabled = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -98,7 +98,7 @@ void DisplayWindow::UpdateTitle() {
|
||||
fpsFrames = 0;
|
||||
}
|
||||
char text[160];
|
||||
std::snprintf(text, sizeof(text), "%s | FPS: %.2f (%llu)", title.value, currentFps, static_cast<unsigned long long>(frameNum));
|
||||
std::snprintf(text, sizeof(text), "%s | %llu", title.value, static_cast<unsigned long long>(frameNum));
|
||||
SDL_SetWindowTitle(window, text);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
#include "prx/libSceVideoOut/include/PadInput.hpp"
|
||||
#include "prx/libScePad/PadState.hpp"
|
||||
#include "SDL.h"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
void PadInput::setMouseMode(bool enabled) {
|
||||
if (SDL_SetRelativeMouseMode(enabled ? SDL_TRUE : SDL_FALSE) != 0) throw std::runtime_error(std::string("Pad: relative mouse mode failed: ") + SDL_GetError());
|
||||
int deltaX = 0;
|
||||
int deltaY = 0;
|
||||
SDL_GetRelativeMouseState(&deltaX, &deltaY);
|
||||
mouseEnabled = enabled;
|
||||
mouseStick = {128, 128};
|
||||
nextMousePoll = std::chrono::steady_clock::now() + std::chrono::milliseconds(Pad::MousePollIntervalMs);
|
||||
}
|
||||
|
||||
void PadInput::HandleEvent(const SDL_Event& event) {
|
||||
if (event.type == SDL_WINDOWEVENT && (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST || event.window.event == SDL_WINDOWEVENT_CLOSE)) {
|
||||
pressed.fill(false);
|
||||
wheelReleaseTimes.fill({});
|
||||
if (mouseEnabled) setMouseMode(false);
|
||||
publish();
|
||||
return;
|
||||
}
|
||||
if (event.type == SDL_MOUSEWHEEL) {
|
||||
int direction = (event.wheel.y > 0) - (event.wheel.y < 0);
|
||||
if (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) direction = -direction;
|
||||
if (direction == 0) return;
|
||||
const auto releaseTime = std::chrono::steady_clock::now() + std::chrono::milliseconds(Pad::WheelPressDurationMs);
|
||||
for (std::size_t index = 0; index < Pad::InputMapping.size(); ++index) {
|
||||
const auto& binding = Pad::InputMapping[index];
|
||||
if (binding.wheelDirection == 0) continue;
|
||||
pressed[index] = binding.wheelDirection == direction;
|
||||
wheelReleaseTimes[index] = pressed[index] ? releaseTime : std::chrono::steady_clock::time_point{};
|
||||
}
|
||||
publish();
|
||||
return;
|
||||
}
|
||||
const bool keyboard = event.type == SDL_KEYDOWN || event.type == SDL_KEYUP;
|
||||
const bool mouse = event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP;
|
||||
if (!keyboard && !mouse) return;
|
||||
if (keyboard && event.key.repeat != 0) return;
|
||||
const bool down = event.type == SDL_KEYDOWN || event.type == SDL_MOUSEBUTTONDOWN;
|
||||
for (std::size_t index = 0; index < Pad::InputMapping.size(); ++index) {
|
||||
const auto& binding = Pad::InputMapping[index];
|
||||
const bool matches = keyboard ? binding.key != SDL_SCANCODE_UNKNOWN && binding.key == event.key.keysym.scancode : binding.mouseButton != 0 && binding.mouseButton == event.button.button;
|
||||
if (!matches) continue;
|
||||
if (binding.control == Pad::InputControl::ToggleMouse && down && !pressed[index]) setMouseMode(!mouseEnabled);
|
||||
pressed[index] = down;
|
||||
}
|
||||
publish();
|
||||
}
|
||||
|
||||
void PadInput::Update() {
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
bool released = false;
|
||||
for (std::size_t index = 0; index < Pad::InputMapping.size(); ++index) {
|
||||
if (Pad::InputMapping[index].wheelDirection == 0 || !pressed[index] || now < wheelReleaseTimes[index]) continue;
|
||||
pressed[index] = false;
|
||||
wheelReleaseTimes[index] = {};
|
||||
released = true;
|
||||
}
|
||||
if (released) publish();
|
||||
if (!mouseEnabled) return;
|
||||
if (SDL_GetKeyboardFocus() == nullptr) {
|
||||
pressed.fill(false);
|
||||
wheelReleaseTimes.fill({});
|
||||
setMouseMode(false);
|
||||
publish();
|
||||
return;
|
||||
}
|
||||
if (now < nextMousePoll) return;
|
||||
nextMousePoll = now + std::chrono::milliseconds(Pad::MousePollIntervalMs);
|
||||
int deltaX = 0;
|
||||
int deltaY = 0;
|
||||
SDL_GetRelativeMouseState(&deltaX, &deltaY);
|
||||
mouseStick = {128, 128};
|
||||
if (deltaX != 0 || deltaY != 0) {
|
||||
const double distance = std::hypot(deltaX, deltaY);
|
||||
const double scale = std::clamp(distance * Pad::MouseSensitivity + 16.0, 64.0, 128.0) / distance;
|
||||
const auto mapAxis = [scale](int delta) { return static_cast<std::uint8_t>(std::clamp(128L + std::lround(delta * scale), 0L, 255L)); };
|
||||
mouseStick = {mapAxis(deltaX), mapAxis(deltaY)};
|
||||
}
|
||||
publish();
|
||||
}
|
||||
|
||||
void PadInput::publish() {
|
||||
PadInputState state;
|
||||
std::array<bool, 4> negative{};
|
||||
std::array<bool, 4> positive{};
|
||||
for (std::size_t index = 0; index < Pad::InputMapping.size(); ++index) {
|
||||
if (!pressed[index]) continue;
|
||||
const auto& binding = Pad::InputMapping[index];
|
||||
switch (binding.control) {
|
||||
case Pad::InputControl::Button: state.buttons |= binding.button; break;
|
||||
case Pad::InputControl::LeftStickLeft: negative[0] = true; break;
|
||||
case Pad::InputControl::LeftStickRight: positive[0] = true; break;
|
||||
case Pad::InputControl::LeftStickUp: negative[1] = true; break;
|
||||
case Pad::InputControl::LeftStickDown: positive[1] = true; break;
|
||||
case Pad::InputControl::RightStickLeft: negative[2] = true; break;
|
||||
case Pad::InputControl::RightStickRight: positive[2] = true; break;
|
||||
case Pad::InputControl::RightStickUp: negative[3] = true; break;
|
||||
case Pad::InputControl::RightStickDown: positive[3] = true; break;
|
||||
case Pad::InputControl::TouchLeft: state.touchLeft = true; break;
|
||||
case Pad::InputControl::TouchRight: state.touchRight = true; break;
|
||||
case Pad::InputControl::ToggleMouse: break;
|
||||
}
|
||||
}
|
||||
for (std::size_t axis = 0; axis < state.sticks.size(); ++axis) {
|
||||
state.sticks[axis] = negative[axis] == positive[axis] ? 128 : negative[axis] ? 0 : 255;
|
||||
}
|
||||
if (mouseEnabled) {
|
||||
state.sticks[2] = mouseStick[0];
|
||||
state.sticks[3] = mouseStick[1];
|
||||
}
|
||||
PadPublishInput_nid_postfix(state);
|
||||
}
|
||||
@@ -5,6 +5,8 @@
|
||||
|
||||
#include "SDL.h"
|
||||
#include "SDL_vulkan.h"
|
||||
#include "prx/libSceVideoOut/include/PadInput.hpp"
|
||||
#include "prx/libScePad/PadState.hpp"
|
||||
#include "prx/libkernel/Equeue/Equeue.hpp"
|
||||
#include "prx/libkernel/Time/include/Time.hpp"
|
||||
#include "prx/libSceVideoOut/include/VideoOutDriver.hpp"
|
||||
@@ -396,6 +398,7 @@ void VideoOutDriver::processFlip(FlipRequest& req) {
|
||||
|
||||
void VideoOutDriver::presentLoop(std::stop_token token) {
|
||||
std::shared_ptr<FlipRequest> current;
|
||||
PadInput padInput;
|
||||
try {
|
||||
while (!token.stop_requested()) {
|
||||
{
|
||||
@@ -427,7 +430,9 @@ void VideoOutDriver::presentLoop(std::stop_token token) {
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
require(event.type != SDL_QUIT, "window was closed");
|
||||
padInput.HandleEvent(event);
|
||||
}
|
||||
padInput.Update();
|
||||
}
|
||||
std::list<std::shared_ptr<FlipRequest>> cancelled;
|
||||
{
|
||||
@@ -441,6 +446,7 @@ void VideoOutDriver::presentLoop(std::stop_token token) {
|
||||
} catch (...) {
|
||||
auto error = std::current_exception();
|
||||
if (!error) std::terminate();
|
||||
PadReportInputFailure_nid_postfix(error);
|
||||
if (current) current->Fail(error);
|
||||
std::list<std::shared_ptr<FlipRequest>> failed;
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user