Xbox Cloud Gaming app for PS5 Homebrew
C
fix(input): time the keyboard-as-gamepad mouse and wheel by the clock
Rafael Abud Ferreiraacddb534a2df
diff --git a/src/app/stream_screen.cpp b/src/app/stream_screen.cppindex 7e491c9..4552aaa 100644--- a/src/app/stream_screen.cpp+++ b/src/app/stream_screen.cpp@@ -440,7 +440,7 @@ void StreamScreen::update(const input::ControllerState& pad, ui::NavInput nav, b// in the slot by itself.int kbSlot = keyboardPadSlot(kbm_.keyboard || kbm_.mouse);kbPad_ = input::ControllerState{};- if (kbSlot >= 0 && !menu_.isOpen()) input::keyboardAsGamepad(kbm_, mouseStick_, kbPad_, keyMap_);+ if (kbSlot >= 0 && !menu_.isOpen()) input::keyboardAsGamepad(kbm_, mouseStick_, kbPad_, platform::nowMs(), keyMap_);int alone = kbSlot >= 0 && !input::padConnected(kbSlot) ? kbSlot : -1;if (alone != kbSlotAttached_) {if (kbSlotAttached_ >= 0 && !input::padConnected(kbSlotAttached_)) g_player->setPadConnected(kbSlotAttached_, false);diff --git a/src/input/keyboard_nav.cpp b/src/input/keyboard_nav.cppindex e771a8c..1f8a4ca 100644--- a/src/input/keyboard_nav.cpp+++ b/src/input/keyboard_nav.cpp@@ -7,7 +7,8 @@namespace xc::input {-void keyboardAsGamepad(const KbmInput& in, MouseStick& stick, ControllerState& out, const KeyMap& map) {+void keyboardAsGamepad(const KbmInput& in, MouseStick& stick, ControllerState& out, uint64_t nowMs,+ const KeyMap& map) {out = ControllerState{};out.connected = in.keyboard || in.mouse;auto down = [&](KeyAction a) {@@ -38,26 +39,42 @@ void keyboardAsGamepad(const KbmInput& in, MouseStick& stick, ControllerState& oout.btnOptions = down(KeyAction::Menu);out.triggerL2 = down(KeyAction::LT) ? 1.0f : 0.0f;out.triggerR2 = down(KeyAction::RT) ? 1.0f : 0.0f;- // The mouse: about 12 counts a pass is the stick all the way; eased so- // a still mouse lets the stick come back to the centre.- constexpr float kFull = 12.0f, kEase = 0.45f;- float tx = std::clamp(in.dx / kFull, -1.0f, 1.0f), ty = std::clamp(in.dy / kFull, -1.0f, 1.0f);- stick.x += (tx - stick.x) * kEase;- stick.y += (ty - stick.y) * kEase;+ // The time this pass's mouse counts cover: since the previous pass, 8 ms+ // for the first one; at most 50 ms (after the app's menu, say), so a+ // long pause doesn't read as a slow mouse.+ float dtMs = stick.lastMs ? std::clamp(static_cast<float>(nowMs - stick.lastMs), 1.0f, 50.0f) : 8.0f;+ stick.lastMs = nowMs;+ // The mouse: 1.5 counts a millisecond is the stick all the way (the 12+ // counts per 8 ms pass it was tuned with); eased so a still mouse lets+ // the stick come back to the centre: 45 % of the way every 8 ms, the+ // same curve at any pass length.+ constexpr float kFullPerMs = 12.0f / 8.0f, kEase8Ms = 0.45f;+ float full = kFullPerMs * dtMs;+ float tx = std::clamp(in.dx / full, -1.0f, 1.0f), ty = std::clamp(in.dy / full, -1.0f, 1.0f);+ float ease = 1.0f - std::pow(1.0f - kEase8Ms, dtMs / 8.0f);+ stick.x += (tx - stick.x) * ease;+ stick.y += (ty - stick.y) * ease;if (std::abs(stick.x) < 0.02f) stick.x = 0;if (std::abs(stick.y) < 0.02f) stick.y = 0;out.rightStickX = stick.x;out.rightStickY = stick.y;// The wheel: a press of Y per notch, ~50 ms down and ~30 ms up so the- // game sees each one (a pass is ~8 ms).- constexpr int kDown = 6, kCycle = 10;+ // game sees each one, whatever the pass length.+ constexpr uint64_t kDownMs = 48, kCycleMs = 80;stick.wheelPresses = std::min(stick.wheelPresses + std::abs(in.wheel), 4);if (stick.wheelPresses > 0) {- if (stick.wheelPhase < kDown) out.btnY = true;- if (++stick.wheelPhase >= kCycle) {- stick.wheelPhase = 0;+ if (!stick.wheelPressing) {+ stick.wheelPressing = true;+ stick.wheelSinceMs = nowMs;+ }+ uint64_t t = nowMs - stick.wheelSinceMs;+ if (t >= kCycleMs) { // this press is over: the next one, if any, starts now--stick.wheelPresses;+ stick.wheelPressing = stick.wheelPresses > 0;+ stick.wheelSinceMs = nowMs;+ t = 0;}+ if (stick.wheelPressing && t < kDownMs) out.btnY = true;}}diff --git a/src/input/keyboard_nav.h b/src/input/keyboard_nav.hindex 13c527f..f2d4738 100644--- a/src/input/keyboard_nav.h+++ b/src/input/keyboard_nav.h@@ -30,13 +30,21 @@ constexpr uint8_t kVkAppMenu = 0x7B;// a second controller isn't needed): the keys as `map` says (defaultKeyMap()// unless changed in Settings), the mouse the right stick, a wheel notch a// press of Y.+//+// Timed by the clock (`nowMs`, monotonic), not by the passes: the mouse's+// speed (counts per ms), the stick's easing and the wheel's presses come+// out the same however often it runs (the stream's input loop, 4 or 8 ms;+// the desktop's, woken by events).struct MouseStick {float x = 0, y = 0; // the right stick, eased towards the mouse's speed+ uint64_t lastMs = 0; // the previous pass (0: none yet)// The wheel: each notch is a short press of Y (switch weapon in most- // shooters): presses still to give, and where the current one is.- int wheelPresses = 0, wheelPhase = 0;+ // shooters): presses still to give, and when the current one began.+ int wheelPresses = 0;+ bool wheelPressing = false;+ uint64_t wheelSinceMs = 0;};-void keyboardAsGamepad(const KbmInput& in, MouseStick& stick, ControllerState& out,+void keyboardAsGamepad(const KbmInput& in, MouseStick& stick, ControllerState& out, uint64_t nowMs,const KeyMap& map = defaultKeyMap());// Adds `extra` (the keyboard as a controller) to `pad`: buttons pressed ondiff --git a/tests/test_main.cpp b/tests/test_main.cppindex 53be4fd..a421686 100644--- a/tests/test_main.cpp+++ b/tests/test_main.cpp@@ -1389,27 +1389,58 @@ static void testKeyboardAsGamepad() {in.dx = 24; // a fast move rightxc::input::MouseStick stick;xc::input::ControllerState out;- xc::input::keyboardAsGamepad(in, stick, out);+ uint64_t now = 1000;+ xc::input::keyboardAsGamepad(in, stick, out, now);CHECK(out.connected && out.btnA && out.leftStickX == 1.0f && out.leftStickY == -1.0f);CHECK(out.triggerR2 == 1.0f && out.triggerL2 == 0.0f);CHECK(out.rightStickX > 0.4f && out.rightStickY == 0.0f);in = {};in.keyboard = true;- for (int i = 0; i < 20; ++i) xc::input::keyboardAsGamepad(in, stick, out); // the mouse still: back to the centre+ for (int i = 0; i < 20; ++i) xc::input::keyboardAsGamepad(in, stick, out, now += 8); // the mouse still: back to the centreCHECK(out.rightStickX == 0.0f && !out.btnA);- // A wheel notch: Y pressed for a few passes, then released.- in = {};- in.keyboard = true;- in.wheel = 1;- xc::input::keyboardAsGamepad(in, stick, out);- CHECK(out.btnY);- in.wheel = 0;- bool released = false;- for (int i = 0; i < 12; ++i) {- xc::input::keyboardAsGamepad(in, stick, out);- released |= !out.btnY;++ // The same mouse speed gives the same stick whether the passes are 4 or+ // 8 ms apart (the counts a pass covers halve with it).+ auto stickAfter = [](uint64_t passMs, float countsPerMs, uint64_t forMs) {+ xc::input::MouseStick st;+ xc::input::ControllerState o;+ xc::input::KbmInput mv;+ mv.mouse = true;+ uint64_t t = 5000;+ xc::input::keyboardAsGamepad(mv, st, o, t); // a still first pass: the clock starts+ for (uint64_t el = 0; el < forMs; el += passMs) {+ mv.dx = static_cast<int32_t>(countsPerMs * static_cast<float>(passMs));+ xc::input::keyboardAsGamepad(mv, st, o, t += passMs);+ }+ return o.rightStickX;+ };+ for (uint64_t forMs : {8u, 24u, 48u, 400u}) {+ float at4 = stickAfter(4, 0.75f, forMs), at8 = stickAfter(8, 0.75f, forMs);+ CHECK(std::abs(at4 - at8) < 0.01f);+ }+ CHECK(std::abs(stickAfter(4, 0.75f, 400) - 0.5f) < 0.01f); // half of full speed: half the stick+ CHECK(std::abs(stickAfter(8, 0.75f, 400) - 0.5f) < 0.01f);+ CHECK(stickAfter(4, 3.0f, 400) > 0.999f && stickAfter(8, 3.0f, 400) > 0.999f); // faster: all the way++ // A wheel notch: Y held ~50 ms, then let go ~30 ms, at either pass length.+ for (uint64_t passMs : {4u, 8u}) {+ xc::input::MouseStick st;+ in = {};+ in.keyboard = true;+ in.wheel = 2; // two notches: two presses+ uint64_t t = 9000, downMs = 0, presses = 0;+ bool was = false;+ for (uint64_t el = 0; el < 400; el += passMs) {+ xc::input::keyboardAsGamepad(in, st, out, t);+ in.wheel = 0;+ if (out.btnY) downMs += passMs;+ if (out.btnY && !was) ++presses;+ was = out.btnY;+ t += passMs;+ }+ CHECK(presses == 2 && !out.btnY);+ CHECK(downMs >= 2 * 40 && downMs <= 2 * 56); // ~48 ms each}- CHECK(released && !out.btnY);xc::input::ControllerState pad, kb;pad.btnB = true;pad.leftStickX = 0.3f;@@ -1527,7 +1558,7 @@ static void testKeyMap() {in.buttons = 8; // backMouseStick stick;ControllerState out;- keyboardAsGamepad(in, stick, out, map);+ keyboardAsGamepad(in, stick, out, 1000, map);CHECK(out.btnA && out.btnY && !out.triggerR2);CHECK(keyName(0x20) == "Space" && keyName('W') == "W" && keyName(kMouseLeft) == "Mouse L" && keyName(0x7B) == "F12");CHECK(isReservedKey(0x7B) && isReservedKey('2') && !isReservedKey('5'));