feat(libSceVideoOut): route SDL mouse events to libSceMouse

Forward relative motion, supported button transitions, and horizontal and vertical scrolling from the existing SDL event loop. Reset pressed buttons on focus loss without reporting a device disconnect, and track window close and refocus transitions.

Link libSceVideoOut to the mouse library using the existing Pad integration pattern. Add targeted coverage for API errors, event mapping, focus changes, history overflow, and reopening.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Julien Legay
2026-09-28 11:45:31 +02:00
parent 8fac768b9c
commit ddb8add753
6 changed files with 197 additions and 1 deletions
+15
View File
@@ -5,3 +5,18 @@ add_library(libSceMouse SHARED EXCLUDE_FROM_ALL
target_include_directories(libSceMouse PRIVATE ${LIBS_INCLUDE_DIR})
set_target_properties(libSceMouse PROPERTIES CXX_EXTENSIONS OFF)
configure_windows_unwind(libSceMouse)
if(BUILD_TESTING)
add_executable(mouse_tests
${CMAKE_CURRENT_SOURCE_DIR}/tests/Mouse.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../libSceVideoOut/src/MouseInput.cpp
)
target_include_directories(mouse_tests PRIVATE ${LIBS_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/3rdparty/SDL2/include)
target_link_libraries(mouse_tests PRIVATE libSceMouse libkernel libc)
configure_windows_unwind(mouse_tests)
add_test(NAME mouse_input COMMAND mouse_tests)
if(WIN32)
set_tests_properties(mouse_input PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceMouse>")
endif()
endif()
+108
View File
@@ -0,0 +1,108 @@
#include "prx/libSceMouse/include/mouse_structs.h"
#include "prx/libSceMouse/include/MouseState.hpp"
#include "prx/libSceVideoOut/include/MouseInput.hpp"
#include "SDL_events.h"
#include "SDL_mouse.h"
#include <cstdlib>
extern "C" {
int APS5_VABI sceMouseInit();
int APS5_VABI sceMouseOpen(int, std::int32_t, std::int32_t, const void*);
int APS5_VABI sceMouseClose(std::int32_t);
int APS5_VABI sceMouseRead(std::int32_t, MouseData*, std::int32_t);
}
static void Require(bool value) { if (!value) std::abort(); }
int main() {
MouseData data[64]{};
Require(sceMouseOpen(1, 0, 0, nullptr) == MOUSE_ERROR_NOT_INITIALIZED);
Require(sceMouseInit() == MOUSE_OK);
Require(sceMouseInit() == MOUSE_OK);
Require(sceMouseOpen(1, 0, 1, nullptr) == MOUSE_ERROR_INVALID_ARG);
MouseOpenParam unsupported{};
unsupported.behaviorFlag = 2;
Require(sceMouseOpen(1, 0, 0, &unsupported) == MOUSE_ERROR_INVALID_ARG);
Require(sceMouseOpen(1, 0, 0, nullptr) == MOUSE_HANDLE);
Require(sceMouseOpen(1, 0, 0, nullptr) == MOUSE_ERROR_ALREADY_OPENED);
Require(sceMouseRead(42, data, 1) == MOUSE_ERROR_INVALID_HANDLE);
Require(sceMouseRead(MOUSE_HANDLE, nullptr, 1) == MOUSE_ERROR_INVALID_ARG);
Require(sceMouseRead(MOUSE_HANDLE, data, 65) == MOUSE_ERROR_INVALID_ARG);
Require(sceMouseRead(MOUSE_HANDLE, data, 1) == 1);
Require(data[0].connected && data[0].buttons == 0);
Require(sceMouseRead(MOUSE_HANDLE, data, 1) == 0);
MouseInput input;
SDL_Event event{};
event.type = SDL_MOUSEMOTION;
event.motion.windowID = 7;
event.motion.xrel = 9;
event.motion.yrel = -4;
input.HandleEvent(event, 8);
Require(sceMouseRead(MOUSE_HANDLE, data, 1) == 0);
input.HandleEvent(event, 7);
event = {};
event.type = SDL_MOUSEBUTTONDOWN;
event.button.windowID = 7;
event.button.button = SDL_BUTTON_RIGHT;
input.HandleEvent(event, 7);
event = {};
event.type = SDL_MOUSEWHEEL;
event.wheel.windowID = 7;
event.wheel.x = 2;
event.wheel.y = -3;
event.wheel.direction = SDL_MOUSEWHEEL_FLIPPED;
input.HandleEvent(event, 7);
Require(sceMouseRead(MOUSE_HANDLE, data, 3) == 3);
Require(data[0].x_axis == 9 && data[0].y_axis == -4 && data[0].buttons == 0);
Require(data[1].buttons == 2);
Require(data[2].buttons == 2 && data[2].wheel == 3 && data[2].tilt == -2);
Require(data[0].timestamp <= data[2].timestamp);
event = {};
event.type = SDL_MOUSEBUTTONDOWN;
event.button.windowID = 7;
event.button.button = SDL_BUTTON_X1;
input.HandleEvent(event, 7);
Require(sceMouseRead(MOUSE_HANDLE, data, 1) == 0);
event = {};
event.type = SDL_WINDOWEVENT;
event.window.windowID = 7;
event.window.event = SDL_WINDOWEVENT_FOCUS_LOST;
input.HandleEvent(event, 7);
Require(sceMouseRead(MOUSE_HANDLE, data, 1) == 1);
Require(data[0].connected && data[0].buttons == 0);
event.type = SDL_MOUSEMOTION;
event.motion.xrel = 5;
input.HandleEvent(event, 7);
Require(sceMouseRead(MOUSE_HANDLE, data, 1) == 0);
event.type = SDL_WINDOWEVENT;
event.window.event = SDL_WINDOWEVENT_FOCUS_GAINED;
input.HandleEvent(event, 7);
Require(sceMouseRead(MOUSE_HANDLE, data, 1) == 0);
event.window.event = SDL_WINDOWEVENT_CLOSE;
input.HandleEvent(event, 7);
Require(sceMouseRead(MOUSE_HANDLE, data, 1) == 1 && !data[0].connected);
event.window.event = SDL_WINDOWEVENT_FOCUS_GAINED;
input.HandleEvent(event, 7);
Require(sceMouseRead(MOUSE_HANDLE, data, 1) == 1 && data[0].connected);
for (int i = 0; i < 70; ++i) {
MouseInputEvent motion{};
motion.x = i;
MousePublishInput_nid_postfix(motion);
}
Require(sceMouseRead(MOUSE_HANDLE, data, 64) == 64);
Require(data[0].x_axis == 6 && data[63].x_axis == 69);
Require(sceMouseRead(MOUSE_HANDLE, data, 1) == 0);
Require(sceMouseClose(MOUSE_HANDLE) == MOUSE_OK);
Require(sceMouseRead(MOUSE_HANDLE, data, 1) == MOUSE_ERROR_INVALID_HANDLE);
Require(sceMouseClose(MOUSE_HANDLE) == MOUSE_ERROR_INVALID_HANDLE);
MouseOpenParam merged{};
merged.behaviorFlag = MOUSE_OPEN_PARAM_MERGED;
Require(sceMouseOpen(1, 0, 0, &merged) == MOUSE_HANDLE);
Require(sceMouseRead(MOUSE_HANDLE, data, 1) == 1 && data[0].buttons == 0);
Require(sceMouseClose(MOUSE_HANDLE) == MOUSE_OK);
}
+2 -1
View File
@@ -6,6 +6,7 @@ add_library(libSceVideoOut SHARED EXCLUDE_FROM_ALL
${CMAKE_CURRENT_SOURCE_DIR}/src/VideoOutDriver.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/DisplayWindow.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/PadInput.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/MouseInput.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/InputMapping.cpp
)
@@ -14,7 +15,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 libScePad libc libkernel SDL2-static)
target_link_libraries(libSceVideoOut PRIVATE libSceAgcDriver libScePad libSceMouse libc libkernel SDL2-static)
if(WIN32)
target_link_libraries(libSceVideoOut PRIVATE comctl32)
endif()
@@ -0,0 +1,14 @@
#ifndef CORE_LIBS_PRX_LIBSCEVIDEOOUT_MOUSEINPUT_HPP
#define CORE_LIBS_PRX_LIBSCEVIDEOOUT_MOUSEINPUT_HPP
#include "SDL_events.h"
class MouseInput {
public:
void HandleEvent(const SDL_Event& event, unsigned windowId);
private:
bool focused = true;
};
#endif
@@ -0,0 +1,55 @@
#include "prx/libSceVideoOut/include/MouseInput.hpp"
#include "prx/libSceMouse/include/MouseState.hpp"
#include "SDL_mouse.h"
void MouseInput::HandleEvent(const SDL_Event& event, unsigned windowId) {
MouseInputEvent input{};
switch (event.type) {
case SDL_MOUSEMOTION:
if (event.motion.windowID != windowId || !focused) return;
input.x = event.motion.xrel;
input.y = event.motion.yrel;
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
if (event.button.windowID != windowId || !focused) return;
switch (event.button.button) {
case SDL_BUTTON_LEFT: input.button = 1; break;
case SDL_BUTTON_RIGHT: input.button = 2; break;
case SDL_BUTTON_MIDDLE: input.button = 4; break;
default: return;
}
input.pressed = event.type == SDL_MOUSEBUTTONDOWN;
break;
case SDL_MOUSEWHEEL:
if (event.wheel.windowID != windowId || !focused) return;
input.wheel = event.wheel.y;
input.tilt = event.wheel.x;
if (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) {
input.wheel = -input.wheel;
input.tilt = -input.tilt;
}
if (input.wheel == 0 && input.tilt == 0) return;
break;
case SDL_WINDOWEVENT:
if (event.window.windowID != windowId) return;
if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) {
focused = false;
input.resetButtons = true;
} else if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) {
focused = true;
input.connectionChange = true;
} else if (event.window.event == SDL_WINDOWEVENT_CLOSE) {
focused = false;
input.connectionChange = true;
input.connected = false;
} else {
return;
}
break;
default:
return;
}
MousePublishInput_nid_postfix(input);
}
@@ -9,6 +9,7 @@
#include "SDL.h"
#include "SDL_vulkan.h"
#include "prx/libSceVideoOut/include/PadInput.hpp"
#include "prx/libSceVideoOut/include/MouseInput.hpp"
#include "prx/libScePad/include/PadState.hpp"
#include "prx/libkernel/Equeue/Equeue.hpp"
#include "prx/libkernel/Time/include/Time.hpp"
@@ -465,6 +466,7 @@ void VideoOutDriver::presentLoop(std::stop_token token) {
std::shared_ptr<FlipRequest> current;
try {
PadInput padInput;
MouseInput mouseInput;
while (!token.stop_requested()) {
{
std::unique_lock lock(flipQueue->mutex);
@@ -480,6 +482,7 @@ void VideoOutDriver::presentLoop(std::stop_token token) {
while (SDL_PollEvent(&event)) {
require(event.type != SDL_QUIT, "window was closed");
padInput.HandleEvent(event, window);
if (window.Handle() != nullptr) mouseInput.HandleEvent(event, SDL_GetWindowID(window.Handle()));
}
padInput.Update();
if (current) {