Xbox Cloud Gaming app for PS5 Homebrew
C
psbox: a DualSense's touchpad no longer moves the mouse
Rafael Abud Ferreira6030efa90e1e
diff --git a/src/input/kbm_sdl.cpp b/src/input/kbm_sdl.cppindex b20c7d0..be52107 100644--- a/src/input/kbm_sdl.cpp+++ b/src/input/kbm_sdl.cpp@@ -5,6 +5,22 @@// PS5's keyboard does (SDL's scancodes are the same USB HID usages); the// mouse's movement counts only while the window holds it (kbmSetCaptured:// in a game), its buttons and wheel while the window has the focus.+//+// A DualSense's (or DS4's) touchpad is not the mouse. Linux's driver gives+// it a pointer device of its own, so the desktop moves the pointer with it+// and SDL hands that on as mouse events, which in a game moved the right+// stick (keyboard as a controller) or the game's cursor. The pad's touchpad+// is read through the pad (SDL_GetGamepadTouchpadFinger), so those events+// are dropped, two ways:+// - by device, where SDL names the mouse a motion came from (X11: each+// pointer device): isPadPointerName();+// - by time, where it can't: Wayland merges every pointer into the seat's+// ("Virtual core pointer (seat0)"), so while a finger is on a pad's+// touchpad, and kTouchGraceNs after (a tap turns into a click a moment+// after the finger leaves), the mouse is ignored. The pad's touch events+// come after the pointer's from the same SDL_PumpEvents, so a touch also+// drops what the mouse gathered since the last poll. This needs the pad+// opened through HIDAPI (controller_sdl.cpp), which reports its touchpad.#include "input/input_ext.h"#include "input/kbm.h"#include "input/sdl_backend.h"@@ -14,7 +30,9 @@#include <SDL3/SDL.h>#include <algorithm>+#include <map>#include <mutex>+#include <set>#include <vector>namespace xc::input {@@ -36,6 +54,30 @@ uint32_t g_sdlClicked = 0; // went down since the last poll (a click// What the last poll reported.std::vector<uint8_t> g_held;uint8_t g_buttons = 0;+// The pads' touchpads: fingers on them (pad, touchpad, finger) and when+// the last one left (SDL ns), for kTouchGraceNs.+constexpr uint64_t kTouchGraceNs = 400'000'000;+std::set<uint64_t> g_fingers;+uint64_t g_liftNs = 0;+// Main thread (SDL is called for the names): which mice are a pad's.+std::map<SDL_MouseID, bool> g_padPointer;++// Whether events of mouse `id` are a controller's touchpad (by its name;+// main thread, without g_mutex).+bool padPointer(SDL_MouseID id) {+ if (id == 0 || id == SDL_TOUCH_MOUSEID || id == SDL_PEN_MOUSEID) return false;+ auto it = g_padPointer.find(id);+ if (it != g_padPointer.end()) return it->second;+ const char* name = SDL_GetMouseNameForID(id);+ bool pad = isPadPointerName(name);+ g_padPointer[id] = pad;+ if (pad) XC_LOGI("mouse: '%s' is a controller's touchpad: its events are ignored", name);+ return pad;+}++// Whether the mouse's events now are a pad's touchpad going by time+// (Wayland). Caller holds g_mutex.+bool padTouching(uint64_t nowNs) { return !g_fingers.empty() || (g_liftNs && nowNs - g_liftNs < kTouchGraceNs); }// Holds or lets go of the mouse: held while a game wants it and the window// has the focus.@@ -93,8 +135,41 @@ void kbmSdlEvent(const SDL_Event& e) {}break;}+ case SDL_EVENT_MOUSE_ADDED:+ case SDL_EVENT_MOUSE_REMOVED:+ g_padPointer.erase(e.mdevice.which);+ break;+ case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN:+ case SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION:+ case SDL_EVENT_GAMEPAD_TOUCHPAD_UP: {+ uint64_t key = static_cast<uint64_t>(e.gtouchpad.which) << 16 | static_cast<uint64_t>(e.gtouchpad.touchpad & 0xFF) << 8 |+ static_cast<uint64_t>(e.gtouchpad.finger & 0xFF);+ std::lock_guard<std::mutex> lock(g_mutex);+ static bool logged = false;+ if (!logged) XC_LOGI("mouse: ignored while a finger is on a pad's touchpad"), logged = true;+ if (e.type == SDL_EVENT_GAMEPAD_TOUCHPAD_UP) {+ g_fingers.erase(key);+ g_liftNs = e.common.timestamp ? e.common.timestamp : SDL_GetTicksNS();+ } else {+ g_fingers.insert(key);+ // The pointer's events from this pump came first: the touchpad's too.+ g_dx = g_dy = 0;+ g_wheel = 0;+ g_sdlClicked = 0;+ }+ break;+ }+ case SDL_EVENT_GAMEPAD_REMOVED: {+ std::lock_guard<std::mutex> lock(g_mutex);+ uint64_t pad = static_cast<uint64_t>(e.gdevice.which);+ for (auto it = g_fingers.begin(); it != g_fingers.end();)+ it = (*it >> 16) == pad ? g_fingers.erase(it) : std::next(it);+ break;+ }case SDL_EVENT_MOUSE_MOTION: {+ if (padPointer(e.motion.which)) break;std::lock_guard<std::mutex> lock(g_mutex);+ if (padTouching(e.common.timestamp)) break;if (g_captured) {g_dx += e.motion.xrel;g_dy += e.motion.yrel;@@ -104,8 +179,10 @@ void kbmSdlEvent(const SDL_Event& e) {case SDL_EVENT_MOUSE_BUTTON_DOWN:case SDL_EVENT_MOUSE_BUTTON_UP: {if (e.button.button < SDL_BUTTON_LEFT || e.button.button > SDL_BUTTON_X2) break;+ if (padPointer(e.button.which)) break;uint32_t bit = SDL_BUTTON_MASK(e.button.button);std::lock_guard<std::mutex> lock(g_mutex);+ if (e.button.down && padTouching(e.common.timestamp)) break; // a release always countsif (e.button.down) {g_sdlButtons |= bit;g_sdlClicked |= bit;@@ -115,9 +192,11 @@ void kbmSdlEvent(const SDL_Event& e) {break;}case SDL_EVENT_MOUSE_WHEEL: {+ if (padPointer(e.wheel.which)) break;int32_t notches = e.wheel.integer_y;if (e.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) notches = -notches;std::lock_guard<std::mutex> lock(g_mutex);+ if (padTouching(e.common.timestamp)) break;g_wheel += notches;break;}diff --git a/src/input/sdl_backend.h b/src/input/sdl_backend.hindex 1d68889..d66c95b 100644--- a/src/input/sdl_backend.h+++ b/src/input/sdl_backend.h@@ -20,7 +20,9 @@ void padSdlShutdown();// The window whose events come in and which holds the mouse.void kbmSdlInit(SDL_Window* window);-// Keys, mouse movement / buttons / wheel, the window's focus.+// Keys, mouse movement / buttons / wheel, the window's focus; the pads'+// touchpad events and removal, the mice coming and going (a pad's touchpad+// moving the pointer isn't the mouse).void kbmSdlEvent(const SDL_Event& event);// Lets the mouse go.void kbmSdlShutdown();diff --git a/src/input/sdl_input.cpp b/src/input/sdl_input.cppindex eb20093..92e27dc 100644--- a/src/input/sdl_input.cpp+++ b/src/input/sdl_input.cpp@@ -18,8 +18,18 @@ void sdlInputInit(SDL_Window* window) {void sdlInputEvent(const SDL_Event& event) {switch (event.type) {case SDL_EVENT_GAMEPAD_ADDED:+ padSdlEvent(event);+ break;case SDL_EVENT_GAMEPAD_REMOVED:padSdlEvent(event);+ kbmSdlEvent(event); // its fingers off the touchpad+ break;+ case SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN:+ case SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION:+ case SDL_EVENT_GAMEPAD_TOUCHPAD_UP:+ case SDL_EVENT_MOUSE_ADDED:+ case SDL_EVENT_MOUSE_REMOVED:+ kbmSdlEvent(event); // a pad's touchpad isn't the mousebreak;case SDL_EVENT_KEY_DOWN:case SDL_EVENT_KEY_UP:diff --git a/src/input/sdl_mapping.cpp b/src/input/sdl_mapping.cppindex 65c914a..4316d17 100644--- a/src/input/sdl_mapping.cpp+++ b/src/input/sdl_mapping.cpp@@ -5,7 +5,9 @@#include "input/kbm.h"#include <algorithm>+#include <cctype>#include <cstring>+#include <string>namespace xc::input {@@ -26,6 +28,14 @@ uint8_t sdlMouseButtons(uint32_t m) {return static_cast<uint8_t>((m & 1u ? 1 : 0) | (m & 4u ? 2 : 0) | (m & 2u ? 4 : 0) | (m & 8u ? 8 : 0) | (m & 16u ? 16 : 0));}+bool isPadPointerName(const char* name) {+ if (!name) return false;+ std::string n(name);+ std::transform(n.begin(), n.end(), n.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });+ auto has = [&](const char* word) { return n.find(word) != std::string::npos; };+ return has("wireless controller") || has("dualsense") || has("dualshock") || (has("controller") && has("touchpad"));+}+void playerLightColour(int slot, uint8_t rgb[3]) {static constexpr uint8_t kColours[4][3] = {{0, 112, 220}, {220, 50, 50}, {40, 170, 80}, {225, 90, 175}};const uint8_t* c = kColours[std::clamp(slot, 0, 3)];diff --git a/src/input/sdl_mapping.h b/src/input/sdl_mapping.hindex 8b652fe..b30e80c 100644--- a/src/input/sdl_mapping.h+++ b/src/input/sdl_mapping.h@@ -30,6 +30,14 @@ uint8_t scancodeToVk(int scancode);// app's and the wire's (kbm.h: 1 left, 2 right, 4 middle, 8 back, 16 forward).uint8_t sdlMouseButtons(uint32_t sdlMask);+// Whether a pointer device of this name is a game controller's: Linux+// (hid-playstation) gives the DualSense's and DS4's touchpads a pointer+// device of their own ("Sony Interactive Entertainment DualSense Wireless+// Controller Touchpad", "Wireless Controller Touchpad"); its movement isn't+// the mouse's (the pad's touchpad is read through the pad). A laptop's+// touchpad or a mouse isn't one. Case doesn't matter; null is none.+bool isPadPointerName(const char* name);+// The light bar of player `slot` (0..3) while the app sets none: the// player's colour (ui::playerColor: blue, red, green, pink).void playerLightColour(int slot, uint8_t rgb[3]);diff --git a/tests/test_main.cpp b/tests/test_main.cppindex ae15c39..ccde30d 100644--- a/tests/test_main.cpp+++ b/tests/test_main.cpp@@ -1383,6 +1383,16 @@ static void testSdlMouseButtons() {CHECK(sdlMouseButtons(8) == 8); // X1: backCHECK(sdlMouseButtons(16) == 16); // X2: forwardCHECK(sdlMouseButtons(1 | 4) == 3 && sdlMouseButtons(31) == 31 && sdlMouseButtons(0xFFFFFFE0u) == 0);+ // A controller's touchpad as a pointer device (Linux) isn't the mouse.+ using xc::input::isPadPointerName;+ CHECK(isPadPointerName("Sony Interactive Entertainment DualSense Wireless Controller Touchpad"));+ CHECK(isPadPointerName("DualSense Wireless Controller Touchpad")); // Bluetooth+ CHECK(isPadPointerName("Wireless Controller Touchpad")); // DS4, Bluetooth+ CHECK(isPadPointerName("Sony Computer Entertainment DUALSHOCK 4 Wireless Controller Touchpad"));+ CHECK(isPadPointerName("Generic Controller Touchpad"));+ CHECK(!isPadPointerName("ELAN1200:00 04F3:30F7 Touchpad") && !isPadPointerName("SynPS/2 Synaptics TouchPad"));+ CHECK(!isPadPointerName("Logitech G502 HERO Gaming Mouse") && !isPadPointerName("Virtual core pointer (seat0)"));+ CHECK(!isPadPointerName("") && !isPadPointerName(nullptr));}// SDL's scancodes are HID usages: the same keys as the PS5's keyboard.