Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

WIP: keys held between keyboard reads; the app's screens by keyboard; F12 the app's menu in a game

Rafael Abud Ferreira91cec918e052
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4a0f265..5895c51 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -36,6 +36,7 @@ add_library(xcloud_core STATIC
src/xcloud/prices.cpp
src/input/kbm.cpp
src/input/kbm_map.cpp
+ src/input/keyboard_nav.cpp
src/xcloud/social.cpp
src/platform/posix_common.cpp
)
diff --git a/src/app/ps5_main.cpp b/src/app/ps5_main.cpp
index a1f72b4..b1d42e3 100644
--- a/src/app/ps5_main.cpp
+++ b/src/app/ps5_main.cpp
@@ -16,6 +16,7 @@
#include "display/gpu.h"
#include "input/controller.h"
#include "input/kbm.h"
+#include "input/keyboard_nav.h"
#include "input/tuning.h"
#include "net/http.h"
#include "platform/platform.h"
@@ -273,6 +274,7 @@ int main(int argc, char** argv) {
// Never return from main: the app is closed from the home screen.
input::ControllerState prev{}, pad{};
+ bool appMenuKeyBefore = false; // F12 held at the last pass
Repeater up, down, left, right, dpadLeft, dpadRight;
// Smooth scrolling: rows / cards still to move (fractions carry over),
// the touchpad's last point, and its speed for the glide after release.
@@ -335,6 +337,15 @@ int main(int argc, char** argv) {
}
input::poll(pad);
autoplayPad(pad);
+ // USB keyboard: the app's screens (and its menu in a game) as with the
+ // controller; in a game otherwise, its keys and the mouse go to the game.
+ input::KbmInput kbm = input::kbmPoll();
+ bool inGame = g_ui->screen() == ui::Screen::Streaming;
+ bool appMenuKey = std::binary_search(kbm.held.begin(), kbm.held.end(), input::kVkAppMenu);
+ bool appMenuPressed = appMenuKey && !appMenuKeyBefore;
+ appMenuKeyBefore = appMenuKey;
+ if (!inGame || streamScreen.menuOpen()) input::keyboardToPad(kbm.held, pad);
+ streamScreen.setKbm(std::move(kbm));
{
// The triggers' resistance: in a game and in its Settings tester only.
float l2, r2;
@@ -440,7 +451,8 @@ int main(int argc, char** argv) {
scroll.cards -= static_cast<float>(nav.scrollCards);
}
nav.nowMs = now;
- bool menuCombo = pad.btnOptions && pad.btnTouchpad && !(prev.btnOptions && prev.btnTouchpad);
+ bool menuCombo = (pad.btnOptions && pad.btnTouchpad && !(prev.btnOptions && prev.btnTouchpad)) ||
+ (inGame && appMenuPressed);
prev = pad;
autoplayNav(nav, menuCombo, now);
if (g_ui->screen() == ui::Screen::Streaming) {
diff --git a/src/app/stream_screen.cpp b/src/app/stream_screen.cpp
index 9b652a7..7649da1 100644
--- a/src/app/stream_screen.cpp
+++ b/src/app/stream_screen.cpp
@@ -9,6 +9,7 @@
#include "display/display.h"
#include "display/gpu.h"
#include "input/kbm.h"
+#include "input/keyboard_nav.h"
#include "media/audio_out.h"
#include "platform/platform.h"
#include "ui/strings.h"
@@ -336,8 +337,26 @@ void StreamScreen::update(const input::ControllerState& pad, ui::NavInput nav, b
}
if (platform::nowMs() < g_xboxButtonUntil) sent.btnNexus = true;
g_player->sendInput(sent);
- // USB keyboard and mouse: straight to the game (those that take them).
- g_player->sendKbm(input::kbmPoll());
+ // USB keyboard and mouse: straight to the game (those that take
+ // them) while the app's menu is closed; F12 is the app's.
+ if (!menu_.isOpen()) {
+ 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; }),
+ toGame.keys.end());
+ for (const auto& k : toGame.keys) {
+ auto at = std::find(keysInGame_.begin(), keysInGame_.end(), k.vk);
+ if (k.pressed && at == keysInGame_.end()) keysInGame_.push_back(k.vk);
+ if (!k.pressed && at != keysInGame_.end()) keysInGame_.erase(at);
+ }
+ g_player->sendKbm(toGame);
+ } else if (!keysInGame_.empty()) {
+ // The menu opened with keys down: the game lets go of them.
+ input::KbmInput release;
+ for (uint8_t vk : keysInGame_) release.keys.push_back({vk, false});
+ keysInGame_.clear();
+ g_player->sendKbm(release);
+ }
// The other players' controllers, straight to the game.
for (int i = 1; i < input::kMaxPads; ++i) {
input::ControllerState other;
@@ -406,7 +425,10 @@ void StreamScreen::update(const input::ControllerState& pad, ui::NavInput nav, b
}
}
+bool StreamScreen::menuOpen() const { return menu_.isOpen(); }
+
void StreamScreen::idle() {
+ keysInGame_.clear();
if (!g_keyboardFor.empty()) updateTextInput(nullptr);
touchEnabled_ = false; // each stream starts with touch off
if (overlayShown_) {
diff --git a/src/app/stream_screen.h b/src/app/stream_screen.h
index 2388bc5..b21d53d 100644
--- a/src/app/stream_screen.h
+++ b/src/app/stream_screen.h
@@ -9,6 +9,7 @@
#include "app/auto_deband.h"
#include "input/controller.h"
+#include "input/kbm.h"
#include "ui/app_ui.h"
#include "ui/stream_menu.h"
@@ -27,6 +28,10 @@ public:
// Each pass while streaming: `pad` the first controller, `nav` its
// presses, `menuCombo` OPTIONS + TOUCHPAD just pressed.
void update(const input::ControllerState& pad, ui::NavInput nav, bool menuCombo);
+ // This frame's USB keyboard and mouse: to the game while the app's menu
+ // is closed (update() sends them).
+ void setKbm(input::KbmInput kbm) { kbm_ = std::move(kbm); }
+ bool menuOpen() const;
// Each pass on any other screen: what the stream left is cleared, and a
// console keyboard still up is closed.
void idle();
@@ -59,6 +64,8 @@ private:
int deadzone_ = 15;
bool circleConfirms_ = false;
bool highRefresh_ = false; // 120 Hz turned on for this stream
+ input::KbmInput kbm_;
+ std::vector<uint8_t> keysInGame_; // keys the game was told are down
bool wasStreaming_ = false;
uint64_t lastMenuRedrawMs_ = 0;
};
diff --git a/src/input/kbm.cpp b/src/input/kbm.cpp
index 08599da..fe4a7a9 100644
--- a/src/input/kbm.cpp
+++ b/src/input/kbm.cpp
@@ -67,6 +67,10 @@ bool g_keyboardReady = false, g_mouseReady = false;
int g_keyboards[kSlots] = {-1, -1}, g_mice[kSlots] = {-1, -1};
uint64_t g_nextScanMs = 0;
std::vector<uint8_t> g_held; // virtual keys down, sorted
+// Each keyboard's keys held at its last record: a read with no new record
+// means nothing changed (not that every key went up).
+std::vector<uint8_t> g_slotHeld[kSlots];
+bool g_slotConnected[kSlots] = {};
uint8_t g_buttons = 0;
bool g_loggedKeyboard = false, g_loggedMouse = false;
@@ -119,7 +123,8 @@ KbmInput kbmPoll() {
if (!g_keyboardReady && !g_mouseReady) return out;
rescan(platform::nowMs());
- // Keyboards: the newest record says what is held now.
+ // Keyboards: the newest record says what is held now; with none new,
+ // what the last one said still holds.
std::vector<uint8_t> held;
KeyboardState ks[16];
for (int i = 0; i < kSlots; ++i) {
@@ -128,21 +133,28 @@ KbmInput kbmPoll() {
if (n < 0) { // gone: closed, reopened at the next scan
sceKeyboardClose(g_keyboards[i]);
g_keyboards[i] = -1;
+ g_slotHeld[i].clear();
+ g_slotConnected[i] = false;
continue;
}
- if (n == 0) continue;
- const KeyboardState& k = ks[std::min(n, 16) - 1];
- if (!k.connected) continue;
+ if (n > 0) {
+ const KeyboardState& k = ks[std::min(n, 16) - 1];
+ g_slotConnected[i] = k.connected != 0;
+ g_slotHeld[i].clear();
+ if (k.connected && !k.intercepted) { // intercepted: the system has it (its own keyboard UI)
+ for (int b = 0; b < 8; ++b)
+ if (k.modifiers & (1u << b)) g_slotHeld[i].push_back(modifierVk(b));
+ for (int j = 0; j < std::clamp(k.length, 0, 16); ++j)
+ if (uint8_t vk = hidToVk(k.keys[j])) g_slotHeld[i].push_back(vk);
+ }
+ }
+ if (!g_slotConnected[i]) continue;
out.keyboard = true;
if (!g_loggedKeyboard) {
g_loggedKeyboard = true;
XC_LOGI("keyboard in use");
}
- if (k.intercepted) continue; // the system has it (its own keyboard UI)
- for (int b = 0; b < 8; ++b)
- if (k.modifiers & (1u << b)) held.push_back(modifierVk(b));
- for (int j = 0; j < std::clamp(k.length, 0, 16); ++j)
- if (uint8_t vk = hidToVk(k.keys[j])) held.push_back(vk);
+ held.insert(held.end(), g_slotHeld[i].begin(), g_slotHeld[i].end());
}
std::sort(held.begin(), held.end());
held.erase(std::unique(held.begin(), held.end()), held.end());
@@ -151,6 +163,7 @@ KbmInput kbmPoll() {
for (uint8_t vk : held)
if (!std::binary_search(g_held.begin(), g_held.end(), vk)) out.keys.push_back({vk, true});
g_held.swap(held);
+ out.held = g_held;
static int logged = 0; // the first few, to check a keyboard works (not what is typed after)
for (const auto& e : out.keys)
if (logged < 10) XC_LOGI("keyboard: key 0x%02x %s (%d)", e.vk, e.pressed ? "down" : "up", ++logged);
diff --git a/src/input/kbm.h b/src/input/kbm.h
index b4c982f..03d2f10 100644
--- a/src/input/kbm.h
+++ b/src/input/kbm.h
@@ -20,7 +20,8 @@ struct KeyEvent {
struct KbmInput {
bool keyboard = false, mouse = false; // connected
- std::vector<KeyEvent> keys;
+ std::vector<KeyEvent> keys; // went down / up since the last poll
+ std::vector<uint8_t> held; // held now, sorted
int32_t dx = 0, dy = 0, wheel = 0; // mouse movement and wheel steps since the last poll
uint8_t buttons = 0; // 1 left, 2 right, 4 middle, 8 back, 16 forward
bool moved = false; // anything to send for the mouse (movement, wheel or buttons changed)
diff --git a/src/input/keyboard_nav.cpp b/src/input/keyboard_nav.cpp
new file mode 100644
index 0000000..a9c5f4e
--- /dev/null
+++ b/src/input/keyboard_nav.cpp
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+// Copyright (C) 2026 RafaelNGP
+#include "input/keyboard_nav.h"
+
+#include <algorithm>
+
+namespace xc::input {
+
+void keyboardToPad(const std::vector<uint8_t>& held, ControllerState& pad) {
+ auto down = [&](std::initializer_list<uint8_t> vks) {
+ for (uint8_t vk : vks)
+ if (std::find(held.begin(), held.end(), vk) != held.end()) return true;
+ return false;
+ };
+ pad.dpadUp |= down({0x26, 'W'});
+ pad.dpadDown |= down({0x28, 'S'});
+ pad.dpadLeft |= down({0x25, 'A'});
+ pad.dpadRight |= down({0x27, 'D'});
+ pad.btnA |= down({0x0D, 0x20}); // enter, space
+ pad.btnB |= down({0x1B, 0x08}); // escape, backspace
+ pad.btnOptions |= down({0x09}); // tab
+ pad.btnY |= down({'F', 0xBF}); // f, slash
+ pad.btnX |= down({'H', 'X'});
+ pad.btnL1 |= down({'Q'});
+ pad.btnR1 |= down({'E'});
+ pad.btnR3 |= down({'R'});
+ pad.btnTouchpad |= down({0x2E}); // delete
+ if (down({'Z'})) pad.triggerL2 = 1.0f;
+ if (down({'C'})) pad.triggerR2 = 1.0f;
+ if (down({0x21})) pad.rightStickY = -1.0f; // page up
+ if (down({0x22})) pad.rightStickY = 1.0f; // page down
+}
+
+} // namespace xc::input
diff --git a/src/input/keyboard_nav.h b/src/input/keyboard_nav.h
new file mode 100644
index 0000000..8cc51c1
--- /dev/null
+++ b/src/input/keyboard_nav.h
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+// Copyright (C) 2026 RafaelNGP
+// The app's screens with a keyboard: the keys held (Windows virtual-key
+// codes, whatever reads them: the PS5's library, SDL on a desktop) become
+// the controller buttons the screens already answer to.
+//
+// arrows / W A S D D-pad Enter / Space Cross (select)
+// Esc / Backspace Circle (back) Tab OPTIONS (settings)
+// F / "/" Triangle (search) H / X Square (hide)
+// Q / E L1 / R1 (tabs) Z / C L2 / R2
+// Page Up / Down right stick (scroll by pages) R R3
+// Delete touchpad (held: sign out)
+//
+// In a game the keys go to the game; F12 opens and closes the app's menu.
+#pragma once
+
+#include "input/controller.h"
+
+#include <cstdint>
+#include <vector>
+
+namespace xc::input {
+
+// The key that opens the app's menu during a game (F12).
+constexpr uint8_t kVkAppMenu = 0x7B;
+
+// Adds what the keys held press to `pad` (buttons on, sticks pushed); the
+// controller's own input stays.
+void keyboardToPad(const std::vector<uint8_t>& held, ControllerState& pad);
+
+} // namespace xc::input
diff --git a/tests/test_main.cpp b/tests/test_main.cpp
index 3c63568..5085c65 100644
--- a/tests/test_main.cpp
+++ b/tests/test_main.cpp
@@ -22,6 +22,7 @@
#include "auth/auth_manager.h"
#include "stream/h264_receiver.h"
#include "input/kbm.h"
+#include "input/keyboard_nav.h"
#include "platform/platform.h"
#include "auth/msa.h"
#include "ui/image_cache.h"
@@ -934,6 +935,20 @@ static void testHidToVk() {
CHECK(xc::input::modifierVk(5) == 0x10 && xc::input::modifierVk(7) == 0x5C);
}
+// The app's screens by keyboard: keys held -> the controller's buttons.
+static void testKeyboardNav() {
+ xc::input::ControllerState pad;
+ xc::input::keyboardToPad({0x0D, 'W', 0x22}, pad); // enter, w, page down
+ CHECK(pad.btnA && pad.dpadUp && !pad.dpadDown && pad.rightStickY == 1.0f);
+ xc::input::ControllerState p2;
+ p2.btnB = true; // the controller's own press stays
+ xc::input::keyboardToPad({0x25, 0x1B, 0x09, 'Q', 'E', 'F', 'H', 'Z'}, p2);
+ CHECK(p2.dpadLeft && p2.btnB && p2.btnOptions && p2.btnL1 && p2.btnR1 && p2.btnY && p2.btnX && p2.triggerL2 == 1.0f);
+ xc::input::ControllerState p3;
+ xc::input::keyboardToPad({}, p3);
+ CHECK(!p3.btnA && !p3.dpadUp && p3.rightStickY == 0.0f);
+}
+
int main() {
testReport();
testCrashGuard();
@@ -963,6 +978,7 @@ int main() {
testInputPacket();
testMouseKeyboardReports();
testHidToVk();
+ testKeyboardNav();
testTeredo();
if (g_failures) {
std::fprintf(stderr, "%d failure(s)\n", g_failures);