Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

Input: DualSense touchpad as a mouse in MKB games

Rafael Abud Ferreira60bb74920986
diff --git a/ROADMAP.md b/ROADMAP.md
index 1f854b1..3d14df6 100644
--- a/ROADMAP.md
+++ b/ROADMAP.md
@@ -224,14 +224,14 @@ is rolled back at the next start), then the app restarts itself
| Free play with ads | Microsoft's ad-supported offering (`xgpuwebf2p` login and sessions, its own list): play without a subscription, with the service's limits; the ads are never skipped (as green-nx, Nubix, XStreaming) | small–medium |
| Game Pass trial badge (later) | The store's answer says `isTrial`; a TRIAL mark next to the plan. Not now | small |
| Unreliable input channel (protocol v9) | The official client sends input on an unordered channel, so a lost packet doesn't hold the ones after it; needs a capture of its frames | medium |
-| DualSense button remapping (done), touchpad mouse | Full DualSense button customization in Settings and each game's settings (Edge back buttons/paddles too), with its own remapping screen like keyboard keys (done); touchpad as a mouse for MKB games | medium |
+| DualSense button remapping, touchpad mouse (done) | Full DualSense button customization in Settings and each game's settings (Edge back buttons/paddles too), with its own remapping screen like keyboard keys; touchpad as a mouse for MKB games (done) | medium |
| Gamepad report physicality & touchpad click robustness (#39, done) | In `src/stream/input_packet.cpp`: compute accurate `physicalPhysicality` bitmask (0 when idle) and send 0 for `virtualPhysicality` (LE), preventing idle secondary controllers/bridges from continuously reporting active physical input to xCloud. In `src/app/stream_screen.cpp`: suppress touchpad swipe gesture detection while `btnTouchpad` is physically pressed. Add one-time diagnostic log on first `kView` / `kMenu` sent per stream session | small |
| Gyro in Settings (done) | Gyro aiming in Settings and the game settings too (done); an inverted / separate horizontal sensitivity if asked | small |
| Desktop Vulkan display (done) | `display/gpu.cpp` shared with the SDL3 desktop window (`SDL_Vulkan_*`): FSR 1, Anime4K x2, Anime4K Restore, CAS sharpening and block smoothing on Linux, with `SDL_Renderer` kept as fallback | medium |
-| Favourites, game language | A favourites row; the game's language apart from the app's | small |
| Lost audio filled | Opus concealment / in-band FEC for lost audio packets: needs libopus (FFmpeg's own Opus decoder has neither) | small–medium |
| Home-screen tile per game | The PS5 Forwarder Format (ProsperoLight): a tile that starts one cloud game directly | small |
| Dedicated voice chat output routing | Route voice chat audio independently to DualSense controller speaker or headset while keeping game audio on TV (if xCloud exposes separate voice track) | medium |
+| Favourites, game language (later) | A favourites row; the game's language apart from the app's | small |
## Dropped (and why)
diff --git a/src/app/stream_screen.cpp b/src/app/stream_screen.cpp
index 88bfcb1..2f94d3b 100644
--- a/src/app/stream_screen.cpp
+++ b/src/app/stream_screen.cpp
@@ -115,6 +115,8 @@ void StreamScreen::update(const input::ControllerState& pad, ui::NavInput nav, b
tile = g_playTile;
}
gameKey_ = gameProfileKey(tile.productId, tile.titleId);
+ mouseKeyboard_ = tile.mouseKeyboard && !tile.homeConsole;
+ touchMouse_ = {};
std::lock_guard<std::mutex> settingsLock(g_settingsMutex);
bool hasCustom = false;
@@ -352,10 +354,20 @@ void StreamScreen::update(const input::ControllerState& pad, ui::NavInput nav, b
}
overlaySeq_ = 0;
}
+ input::KbmInput touchKbm;
+ bool touchDrag = false;
+ if (mouseKeyboard_ && !menu_.isOpen() && g_keyboardFor.empty() && !menuCombo) {
+ touchDrag = input::touchpadAsMouse(pad, platform::nowMs(), touchMouse_, touchKbm);
+ } else {
+ if (touchMouse_.prevButtons != 0) touchKbm.moved = true; // let go of any held touchpad mouse button
+ touchMouse_ = {};
+ }
// The game gets the buttons through this game's map (Settings >
// Controller buttons); everything above (the menu's combo, the
// menu itself) read them as they are.
- input::ControllerState sent = input::applyPadMap(pad, padMap_);
+ input::ControllerState padIn = pad;
+ if (touchDrag) padIn.btnTouchpad = false; // held as a mouse button after moving the cursor, not View
+ input::ControllerState sent = input::applyPadMap(padIn, padMap_);
if (g_syntheticA) sent.btnA = true;
// While another window has the focus on the cloud console (a
// publisher's page such as NetEase's terms in Marvel Rivals,
@@ -374,8 +386,10 @@ void StreamScreen::update(const input::ControllerState& pad, ui::NavInput nav, b
else if (!held) padReleased_ = true;
if (!padReleased_ || menuCombo) sent = input::ControllerState{};
// Swiping up or right on the touchpad is the Xbox button (once
- // per swipe; the games never see the touchpad's touches).
- if (pad.touching && !pad.btnTouchpad && g_keyboardFor.empty()) {
+ // per swipe; the games never see the touchpad's touches). In MKB
+ // games the touchpad moves the mouse while the menu is closed.
+ bool swipeOn = !mouseKeyboard_ || menu_.isOpen();
+ if (swipeOn && pad.touching && !pad.btnTouchpad && g_keyboardFor.empty()) {
if (!swipe_.active) swipe_ = {true, false, pad.touchX, pad.touchY};
float dx = pad.touchX - swipe_.x, dy = swipe_.y - pad.touchY; // dy > 0: up
bool right = dx > 0.30f && std::abs(dy) < 0.30f, up = dy > 0.40f && std::abs(dx) < 0.30f;
@@ -414,9 +428,12 @@ void StreamScreen::update(const input::ControllerState& pad, ui::NavInput nav, b
g_player->sendInput(kbPad_, alone); // all released while the app's menu is open
} else if (kbSlot >= 0) {
// Added to that player's pad in the loop below.
+ }
+ if (kbSlot >= 0 && !menu_.isOpen()) {
+ if (touchKbm.moved) g_player->sendKbm(touchKbm);
} else if (!menu_.isOpen()) {
- // USB keyboard and mouse: straight to the game (those that take
- // them) while the app's menu is closed; F12 is the app's.
+ // USB keyboard and mouse (plus the touchpad mouse in MKB games):
+ // straight to the game while the app's menu is closed; F12 is the app's.
input::KbmInput toGame = kbm_;
toGame.keys.erase(std::remove_if(toGame.keys.begin(), toGame.keys.end(),
[](const input::KeyEvent& k) { return k.vk == input::kVkAppMenu || k.vk == 0x24; }),
@@ -426,10 +443,15 @@ void StreamScreen::update(const input::ControllerState& pad, ui::NavInput nav, b
if (k.pressed && at == keysInGame_.end()) keysInGame_.push_back(k.vk);
if (!k.pressed && at != keysInGame_.end()) keysInGame_.erase(at);
}
+ toGame.dx += touchKbm.dx;
+ toGame.dy += touchKbm.dy;
+ toGame.wheel += touchKbm.wheel;
+ toGame.buttons |= touchKbm.buttons;
+ toGame.moved |= touchKbm.moved;
g_player->sendKbm(toGame);
- } else if (!keysInGame_.empty()) {
- // The menu opened with keys down: the game lets go of them.
- input::KbmInput release;
+ } else if (!keysInGame_.empty() || touchKbm.moved) {
+ // The menu opened with keys or a touchpad mouse button down: let go of them.
+ input::KbmInput release = touchKbm;
for (uint8_t vk : keysInGame_) release.keys.push_back({vk, false});
keysInGame_.clear();
g_player->sendKbm(release);
diff --git a/src/app/stream_screen.h b/src/app/stream_screen.h
index 34ba70e..5bd2074 100644
--- a/src/app/stream_screen.h
+++ b/src/app/stream_screen.h
@@ -81,9 +81,11 @@ private:
input::PadMap padMap_ = input::defaultPadMap(); // this game's controller buttons, toward the game only
int gyroMode_ = 0, gyroLevel_ = 5; // gyro aiming (input/gyro.h), this game's
bool highRefresh_ = false; // 120 Hz turned on for this stream
+ bool mouseKeyboard_ = false; // the game takes native mouse and keyboard (touchpad acts as a mouse)
input::KbmInput kbm_;
int kbSlotAttached_ = -1; // the keyboard's slot as a controller, announced
input::MouseStick mouseStick_;
+ input::TouchpadMouse touchMouse_;
input::ControllerState kbPad_; // the keyboard as a controller, this pass
std::vector<uint8_t> keysInGame_; // keys the game was told are down
bool wasStreaming_ = false;
diff --git a/src/input/controller.h b/src/input/controller.h
index 707aec4..d5b4885 100644
--- a/src/input/controller.h
+++ b/src/input/controller.h
@@ -44,8 +44,9 @@ struct ControllerState {
float triggerL2 = 0.0f;
float triggerR2 = 0.0f;
- // A finger on the touchpad, at (touchX, touchY) in [0, 1] from the top left.
- bool touching = false;
+ // A finger on the touchpad, at (touchX, touchY) in [0, 1] from the top left;
+ // `touchSecond` when a second finger is down too.
+ bool touching = false, touchSecond = false;
float touchX = 0.0f, touchY = 0.0f;
};
diff --git a/src/input/controller_backend.h b/src/input/controller_backend.h
index 754b19a..6c1e1d8 100644
--- a/src/input/controller_backend.h
+++ b/src/input/controller_backend.h
@@ -30,7 +30,7 @@ struct RawPad {
float gyro[3] = {0, 0, 0}; // rad/s: x pitch, y yaw, z roll
float accel[3] = {0, 0, 0}; // g
bool hasAccel = false; // accel was read (gyro aiming takes the pad as flat otherwise)
- bool touching = false;
+ bool touching = false, touchSecond = false;
float touchX = 0, touchY = 0; // 0..1 from the top left
};
diff --git a/src/input/controller_common.cpp b/src/input/controller_common.cpp
index adba359..1959a9d 100644
--- a/src/input/controller_common.cpp
+++ b/src/input/controller_common.cpp
@@ -291,6 +291,7 @@ bool pollPad(int index, ControllerState& out) {
if (pad.touching) {
out.touching = true;
+ out.touchSecond = pad.touchSecond;
out.touchX = pad.touchX;
out.touchY = pad.touchY;
}
diff --git a/src/input/controller_ps5.cpp b/src/input/controller_ps5.cpp
index 264de13..41ba860 100644
--- a/src/input/controller_ps5.cpp
+++ b/src/input/controller_ps5.cpp
@@ -207,6 +207,7 @@ bool readPad(int slot, RawPad& out) {
// The DualSense touchpad reports 0..1919 x 0..1079.
if (pad.touchData.touchNum > 0) {
out.touching = true;
+ out.touchSecond = pad.touchData.touchNum > 1;
out.touchX = std::clamp(pad.touchData.touch[0].x / 1919.0f, 0.0f, 1.0f);
out.touchY = std::clamp(pad.touchData.touch[0].y / 1079.0f, 0.0f, 1.0f);
}
diff --git a/src/input/controller_sdl.cpp b/src/input/controller_sdl.cpp
index d4953f2..c1ed44a 100644
--- a/src/input/controller_sdl.cpp
+++ b/src/input/controller_sdl.cpp
@@ -328,10 +328,11 @@ bool readPad(int slot, RawPad& out) {
}
if (s.caps.touchpad) {
- bool down = false;
- float x = 0, y = 0, pressure = 0;
+ bool down = false, down2 = false;
+ float x = 0, y = 0, pressure = 0, x2 = 0, y2 = 0, p2 = 0;
if (SDL_GetGamepadTouchpadFinger(gp, 0, 0, &down, &x, &y, &pressure) && down) {
out.touching = true; // SDL gives 0..1 from the top left already
+ out.touchSecond = SDL_GetGamepadTouchpadFinger(gp, 0, 1, &down2, &x2, &y2, &p2) && down2;
out.touchX = std::clamp(x, 0.0f, 1.0f);
out.touchY = std::clamp(y, 0.0f, 1.0f);
}
diff --git a/src/input/keyboard_nav.cpp b/src/input/keyboard_nav.cpp
index 0b9f5c0..e771a8c 100644
--- a/src/input/keyboard_nav.cpp
+++ b/src/input/keyboard_nav.cpp
@@ -112,4 +112,79 @@ void keyboardToPad(const std::vector<uint8_t>& held, ControllerState& pad) {
if (down({0x22})) pad.rightStickY = 1.0f; // page down
}
+bool touchpadAsMouse(const ControllerState& pad, uint64_t nowMs, TouchpadMouse& s, KbmInput& io) {
+ // Full sweep across the touchpad = 960 x 540 counts (1920 x 1080 in the stream).
+ constexpr float kScaleX = 960.0f, kScaleY = 540.0f, kWheelScale = 8.0f, kMoveThresh = 0.025f;
+ constexpr uint64_t kTapMaxMs = 200, kTapHoldMs = 50;
+
+ if (pad.touching) {
+ if (!s.touching) {
+ s.touching = true;
+ s.second = s.hadSecond = pad.touchSecond;
+ s.moved = false;
+ s.dragging = false;
+ s.dragButton = 0;
+ s.x = s.startX = pad.touchX;
+ s.y = s.startY = pad.touchY;
+ s.remX = s.remY = s.remWheel = 0;
+ s.downMs = pad.btnTouchpad ? 0 : nowMs;
+ } else {
+ if (pad.touchSecond != s.second) {
+ s.second = pad.touchSecond;
+ if (pad.touchSecond) s.hadSecond = true;
+ // Re-anchor so a second finger coming down or lifting doesn't jump the cursor.
+ s.x = pad.touchX;
+ s.y = pad.touchY;
+ s.remX = s.remY = 0;
+ }
+ if (std::hypot(pad.touchX - s.startX, pad.touchY - s.startY) > kMoveThresh) s.moved = true;
+ if (pad.touchSecond) {
+ s.remWheel += (s.y - pad.touchY) * kWheelScale;
+ int32_t w = static_cast<int32_t>(s.remWheel);
+ s.remWheel -= static_cast<float>(w);
+ io.wheel += w;
+ if (w != 0) s.moved = true;
+ } else {
+ s.remX += (pad.touchX - s.x) * kScaleX;
+ s.remY += (pad.touchY - s.y) * kScaleY;
+ int32_t dx = static_cast<int32_t>(s.remX), dy = static_cast<int32_t>(s.remY);
+ s.remX -= static_cast<float>(dx);
+ s.remY -= static_cast<float>(dy);
+ io.dx += dx;
+ io.dy += dy;
+ }
+ s.x = pad.touchX;
+ s.y = pad.touchY;
+ if (pad.btnTouchpad && !pad.btnOptions) {
+ if (!s.dragging && s.moved) {
+ s.dragging = true;
+ s.dragButton = (s.hadSecond || pad.touchX >= 0.65f) ? 2 : 1;
+ } else if (!s.dragging) {
+ s.downMs = 0; // plain click without moving first: controller View
+ }
+ } else {
+ s.dragging = false;
+ s.dragButton = 0;
+ }
+ }
+ } else if (s.touching) {
+ if (!s.moved && s.downMs > 0 && nowMs >= s.downMs && (nowMs - s.downMs) <= kTapMaxMs && !pad.btnTouchpad) {
+ s.tapButton = s.hadSecond ? 2 : 1;
+ s.tapUntilMs = nowMs + kTapHoldMs;
+ }
+ s.touching = s.second = s.hadSecond = s.moved = s.dragging = false;
+ s.dragButton = 0;
+ s.downMs = 0;
+ }
+
+ uint8_t buttons = 0;
+ if (s.dragging) buttons |= s.dragButton;
+ if (nowMs < s.tapUntilMs) buttons |= s.tapButton;
+ else s.tapButton = 0;
+ io.buttons |= buttons;
+ if (io.dx != 0 || io.dy != 0 || io.wheel != 0 || buttons != s.prevButtons) io.moved = true;
+ s.prevButtons = buttons;
+ return s.dragging;
+}
+
} // namespace xc::input
diff --git a/src/input/keyboard_nav.h b/src/input/keyboard_nav.h
index 66210c4..13c527f 100644
--- a/src/input/keyboard_nav.h
+++ b/src/input/keyboard_nav.h
@@ -47,4 +47,21 @@ void mergePads(ControllerState& pad, const ControllerState& extra);
// controller's own input stays.
void keyboardToPad(const std::vector<uint8_t>& held, ControllerState& pad);
+// The DualSense's touchpad as a mouse in games that take native mouse and
+// keyboard: one finger moves the cursor and a light tap clicks left; two
+// fingers scroll vertically and a light tap clicks right; clicking the pad
+// after moving the cursor holds left (or right on the right third / with two
+// fingers) for dragging while keeping a plain click as the controller's View.
+struct TouchpadMouse {
+ bool touching = false, second = false, hadSecond = false, moved = false, dragging = false;
+ float x = 0, y = 0, startX = 0, startY = 0;
+ float remX = 0, remY = 0, remWheel = 0;
+ uint64_t downMs = 0, tapUntilMs = 0;
+ uint8_t tapButton = 0, dragButton = 0, prevButtons = 0;
+};
+// Adds what the touchpad does to `io` (dx, dy, wheel, buttons, moved); returns
+// true while a physical touchpad click is held as a mouse button (so the
+// controller doesn't also press View).
+bool touchpadAsMouse(const ControllerState& pad, uint64_t nowMs, TouchpadMouse& state, KbmInput& io);
+
} // namespace xc::input
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index 5a38603..8d62a56 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -1058,6 +1058,100 @@ static void testKeyboardAsGamepad() {
CHECK(pad.btnA && pad.btnB && pad.leftStickX == -1.0f && pad.triggerR2 == 1.0f);
}
+// The DualSense's touchpad as a mouse in MKB games: cursor motion, taps,
+// two-finger scroll and click-to-drag after moving.
+static void testTouchpadAsMouse() {
+ using namespace xc::input;
+ TouchpadMouse tm;
+ ControllerState pad;
+ KbmInput io;
+
+ // 1. One finger down, then sliding right by 0.25 and down by 0.5 -> 240 x 270 counts.
+ pad.touching = true;
+ pad.touchX = 0.25f;
+ pad.touchY = 0.25f;
+ CHECK(!touchpadAsMouse(pad, 100, tm, io));
+ CHECK(io.dx == 0 && io.dy == 0 && io.buttons == 0 && !io.moved);
+
+ io = {};
+ pad.touchX = 0.5f;
+ pad.touchY = 0.75f;
+ CHECK(!touchpadAsMouse(pad, 116, tm, io));
+ CHECK(io.dx == 240 && io.dy == 270 && io.buttons == 0 && io.moved);
+
+ // Clicking the touchpad after moving holds the left mouse button for dragging.
+ io = {};
+ pad.btnTouchpad = true;
+ CHECK(touchpadAsMouse(pad, 124, tm, io));
+ CHECK(io.buttons == 1 && io.moved);
+
+ // Releasing the finger lets go of the button.
+ io = {};
+ pad.touching = false;
+ pad.btnTouchpad = false;
+ CHECK(!touchpadAsMouse(pad, 140, tm, io));
+ CHECK(io.buttons == 0 && io.moved);
+
+ // 2. A light one-finger tap clicks the left button for 50 ms.
+ io = {};
+ pad.touching = true;
+ pad.touchX = 0.5f;
+ pad.touchY = 0.5f;
+ CHECK(!touchpadAsMouse(pad, 200, tm, io));
+ io = {};
+ pad.touching = false;
+ CHECK(!touchpadAsMouse(pad, 260, tm, io));
+ CHECK(io.buttons == 1 && io.moved);
+ io = {};
+ CHECK(!touchpadAsMouse(pad, 290, tm, io));
+ CHECK(io.buttons == 1 && !io.moved);
+ io = {};
+ CHECK(!touchpadAsMouse(pad, 315, tm, io));
+ CHECK(io.buttons == 0 && io.moved);
+
+ // 3. A light two-finger tap clicks the right button.
+ io = {};
+ pad.touching = true;
+ pad.touchSecond = true;
+ CHECK(!touchpadAsMouse(pad, 400, tm, io));
+ io = {};
+ pad.touching = false;
+ pad.touchSecond = false;
+ CHECK(!touchpadAsMouse(pad, 450, tm, io));
+ CHECK(io.buttons == 2 && io.moved);
+ io = {};
+ CHECK(!touchpadAsMouse(pad, 510, tm, io));
+ CHECK(io.buttons == 0 && io.moved);
+
+ // 4. Two-finger vertical slide scrolls the wheel without moving the cursor.
+ io = {};
+ pad.touching = true;
+ pad.touchSecond = true;
+ pad.touchX = 0.5f;
+ pad.touchY = 0.6f;
+ CHECK(!touchpadAsMouse(pad, 600, tm, io));
+ io = {};
+ pad.touchY = 0.35f; // up by 0.25 -> +2 wheel notches
+ CHECK(!touchpadAsMouse(pad, 620, tm, io));
+ CHECK(io.dx == 0 && io.dy == 0 && io.wheel == 2 && io.moved);
+ pad.touching = false;
+ pad.touchSecond = false;
+ io = {};
+ touchpadAsMouse(pad, 640, tm, io);
+
+ // 5. A plain physical touchpad click without moving first stays View (not a mouse click).
+ io = {};
+ pad.touching = true;
+ pad.btnTouchpad = true;
+ CHECK(!touchpadAsMouse(pad, 700, tm, io));
+ CHECK(io.buttons == 0 && !io.moved);
+ pad.touching = false;
+ pad.btnTouchpad = false;
+ io = {};
+ CHECK(!touchpadAsMouse(pad, 740, tm, io));
+ CHECK(io.buttons == 0 && !io.moved);
+}
+
// Keys chosen in Settings: one key moves off the input it was on.
static void testKeyMap() {
using namespace xc::input;
@@ -1659,6 +1753,7 @@ int main() {
testHidToVk();
testKeyboardNav();
testKeyboardAsGamepad();
+ testTouchpadAsMouse();
testKeyMap();
testPadMap();
testTeredo();