Xbox Cloud Gaming app for PS5 Homebrew
C
psbox: type into the home search with the keyboard
Rafael Abud Ferreira7e2666034ca5
diff --git a/docs/multiplatform.md b/docs/multiplatform.mdindex 901dd17..b79731e 100644--- a/docs/multiplatform.md+++ b/docs/multiplatform.md@@ -86,7 +86,10 @@ the 120 Hz video-out setup), `afterDisplay(haveDisplay)` (the desktop: itswindow to the input backend), `iconFontPaths()`, `pumpEvents()` each pass(false once the user closes the app; never on the PS5), `hasFocus()`,`canSelfUpdate()`, `isInstalledCopy(dir)` and `restartOrExit()` around an-update, `beforeShutdown()` (desktop only). `caps()` (console / openUrl /+update, `setTextInput(on)` / `takeTextInput()` each pass (the desktop's+keyboard typing into the home search, `AppUi::wantsTextInput()` /+`typeText()`; nothing on the PS5, whose keyboard moves as before),+`beforeShutdown()` (desktop only). `caps()` (console / openUrl /selfUpdate) words the screens; `openUrl(url)` opens the browser (false on thePS5).diff --git a/src/app/app_core.cpp b/src/app/app_core.cppindex 498ac85..9db4d45 100644--- a/src/app/app_core.cpp+++ b/src/app/app_core.cpp@@ -459,6 +459,20 @@ bool Core::frame() {bool appMenuKey = std::binary_search(kbm.held.begin(), kbm.held.end(), input::kVkAppMenu);bool appMenuPressed = appMenuKey && !appMenuKeyBefore;appMenuKeyBefore = appMenuKey;+ // The home search's keys focused: a desktop's keyboard types into the+ // query (the shell's text input); what it types (letters, Space, Enter,+ // Backspace...) then doesn't move through the screens, until let go.+ // Arrows, Esc, Tab and Page Up / Down still do. The PS5 has no text+ // input: its keyboard moves through the screens as before.+ const bool typing = shell::setTextInput(!inGame && !capturing && g_ui->wantsTextInput());+ if (std::string typed = shell::takeTextInput(); !typed.empty()) g_ui->typeText(typed);+ if (typing) {+ static constexpr uint8_t kStillNavigate[] = {0x09, 0x1B, 0x21, 0x22, 0x25, 0x26, 0x27, 0x28};+ for (uint8_t vk : kbm.held)+ if (std::find(std::begin(kStillNavigate), std::end(kStillNavigate), vk) == std::end(kStillNavigate) &&+ std::find(swallowedKeys.begin(), swallowedKeys.end(), vk) == swallowedKeys.end())+ swallowedKeys.push_back(vk);+ }if ((!inGame || streamScreen->menuOpen()) && !capturing) {std::vector<uint8_t> navKeys;for (uint8_t vk : kbm.held)diff --git a/src/app/desktop_selftest.cpp b/src/app/desktop_selftest.cppindex 9fc9751..0a78446 100644--- a/src/app/desktop_selftest.cpp+++ b/src/app/desktop_selftest.cpp@@ -225,6 +225,35 @@ int desktopSelftest(const std::string& dir) {ui::g_glyphStyle = ui::GlyphStyle::PlayStation;ui.invalidate();}+ // The search typed on the keyboard: the shell's text input, the text+ // through AppUi::typeText().+ {+ std::vector<ui::GameTile> pool(3);+ pool[0].productId = "1", pool[0].name = "Halo Infinite";+ pool[1].productId = "2", pool[1].name = "Forza Horizon 5";+ pool[2].productId = "3", pool[2].name = "Halo: The Master Chief Collection";+ ui.setSearchPools(pool, {});+ ui::NavInput tri;+ tri.triangle = true;+ tri.nowMs = platform::nowMs();+ ui.handle(tri);+ bool wants = ui.wantsTextInput();+ bool on = shell::setTextInput(wants);+ ui.typeText("halox\b"); // a typo, then Backspace+ ui.render(canvas, platform::nowMs());+ drawCanvas(canvas);+ auto hits = ui.searchResults(8);+ check(wants && on && hits.size() == 2 && screenshot(dir, "2d-search-typed.png", shot) && varied(shot),+ "search typed with the keyboard (2d-search-typed.png)");+ ui.typeText("\n"); // Enter: on to the results+ check(!ui.wantsTextInput(), "Enter moves to the results");+ shell::setTextInput(false);+ ui::NavInput back;+ back.back = true;+ back.nowMs = platform::nowMs();+ ui.handle(back);+ ui.handle(back);+ }// A video frame through the mailbox, then under the game menu.Yuv bars = colourBars();diff --git a/src/app/shell.h b/src/app/shell.hindex 7a37176..16699e2 100644--- a/src/app/shell.h+++ b/src/app/shell.h@@ -64,5 +64,13 @@ void setFullscreen(bool on);bool fullscreenSwitched(bool& on);// Opens `url` in the system's browser; false where there is none (PS5).bool openUrl(const std::string& url);+// The keyboard typing text into a field (the home search): on while the UI+// wants it (main thread, each pass). True where typed text comes through+// takeTextInput() (the desktop); false where there is none (PS5, null),+// whose keys keep moving through the screens as before.+bool setTextInput(bool on);+// The text typed since the last call, UTF-8, with "\b" for each Backspace+// and "\n" for Enter pressed while text input is on. Empty elsewhere.+std::string takeTextInput();} // namespace xc::app::shelldiff --git a/src/app/shell_desktop.cpp b/src/app/shell_desktop.cppindex df7fef7..2a3f5d5 100644--- a/src/app/shell_desktop.cpp+++ b/src/app/shell_desktop.cpp@@ -5,7 +5,8 @@// empties SDL's event queue: closing the window (or Ctrl+C, which SDL turns// into a quit event) ends the app in order, F11 / Alt+Enter switch// fullscreen (the setting follows), every event goes on to the input backend (pads, keyboard,-// mouse). Notices are toasts in the window; links open in the browser; no+// mouse); while a field wants text (setTextInput()) what is typed is kept+// for it. Notices are toasts in the window; links open in the browser; no// update is installed (Settings opens the release page).#include "app/shell.h"@@ -27,6 +28,8 @@ namespace {bool g_quit = false; // main threadint g_switchedTo = -1; // F11 / Alt+Enter: the fullscreen state asked (main thread), -1 nonestd::atomic<bool> g_focus{true}; // the window has the keyboard+bool g_textOn = false; // SDL text input started (main thread)+std::string g_typed; // typed since takeTextInput() (main thread)volatile std::sig_atomic_t g_signalled = 0;void onSignal(int) { g_signalled = 1; }} // namespace@@ -90,6 +93,11 @@ bool pumpEvents() {}continue;}+ if (g_textOn && ev.key.key == SDLK_BACKSPACE) g_typed += '\b'; // repeats too+ if (g_textOn && !ev.key.repeat && (ev.key.key == SDLK_RETURN || ev.key.key == SDLK_KP_ENTER)) g_typed += '\n';+ break;+ case SDL_EVENT_TEXT_INPUT:+ if (g_textOn && ev.text.text) g_typed += ev.text.text;break;default: break;}@@ -128,6 +136,27 @@ bool openUrl(const std::string& url) {return false;}+bool setTextInput(bool on) {+ if (on == g_textOn) return g_textOn;+ SDL_Window* w = display::sdlWindow();+ if (on) {+ g_textOn = w && SDL_StartTextInput(w);+ static bool told = false;+ if (!g_textOn && !told) told = true, XC_LOGW("text input: %s", SDL_GetError());+ } else {+ if (w) SDL_StopTextInput(w);+ g_textOn = false;+ g_typed.clear();+ }+ return g_textOn;+}++std::string takeTextInput() {+ std::string s;+ s.swap(g_typed);+ return s;+}+void restartOrExit() {// Never asked (no update is installed here).std::fflush(nullptr);diff --git a/src/app/shell_null.cpp b/src/app/shell_null.cppindex 024de79..0b190d6 100644--- a/src/app/shell_null.cpp+++ b/src/app/shell_null.cpp@@ -40,6 +40,8 @@ Caps caps() { return {false, false, false}; }void setFullscreen(bool) {}bool fullscreenSwitched(bool&) { return false; }bool openUrl(const std::string&) { return false; }+bool setTextInput(bool) { return false; }+std::string takeTextInput() { return {}; }void restartOrExit() {// Never asked (no update is installed here); closed as the PS5 does.diff --git a/src/app/shell_ps5.cpp b/src/app/shell_ps5.cppindex be351c7..18fdef1 100644--- a/src/app/shell_ps5.cpp+++ b/src/app/shell_ps5.cpp@@ -65,6 +65,8 @@ void setFullscreen(bool) {}bool fullscreenSwitched(bool&) { return false; }// No browser to send a link to: the screens show a QR code instead.bool openUrl(const std::string&) { return false; }+bool setTextInput(bool) { return false; } // the USB keyboard keeps moving through the screens+std::string takeTextInput() { return {}; }bool isInstalledCopy(const std::string& dir) {// /app0 is the folder the system started the app from.diff --git a/src/ui/app_ui.cpp b/src/ui/app_ui.cppindex 8de2683..c7b4485 100644--- a/src/ui/app_ui.cpp+++ b/src/ui/app_ui.cpp@@ -1977,6 +1977,39 @@ void AppUi::pressFilter(Filter f) {dirty_ = true;}+bool AppUi::wantsTextInput() const {+ std::lock_guard<std::mutex> lock(mutex_);+ return screen_ == Screen::Home && searching_ && searchOnKeys_ && filterList_ < 0 && keyRow_ <= kKeyRows;+}++void AppUi::typeText(std::string_view utf8) {+ std::lock_guard<std::mutex> lock(mutex_);+ if (!(screen_ == Screen::Home && searching_ && searchOnKeys_)) return;+ bool edited = false;+ for (char ch : utf8) {+ if (ch == '\b') {+ // The last character, all of its UTF-8 bytes.+ while (!query_.empty() && (static_cast<unsigned char>(query_.back()) & 0xC0) == 0x80) query_.pop_back();+ if (!query_.empty()) query_.pop_back(), edited = true;+ } else if (ch == '\n') {+ if (edited) runSearch(), edited = false;+ if (!results_.empty()) {+ searchOnKeys_ = false;+ resultFocus_ = 0;+ dirty_ = true;+ }+ } else if (static_cast<unsigned char>(ch) >= 0x20 && (query_.size() < 64 || (ch & 0xC0) == 0x80)) {+ if (ch == ' ' && (query_.empty() || query_.back() == ' ')) continue; // as the Space key+ query_ += ch;+ edited = true;+ }+ }+ if (edited) {+ runSearch();+ dirty_ = true;+ }+}+std::pair<int, int> AppUi::searchKeyFocus() const {std::lock_guard<std::mutex> lock(mutex_);return {keyRow_, keyCol_};diff --git a/src/ui/app_ui.h b/src/ui/app_ui.hindex e1e6de0..c6a816c 100644--- a/src/ui/app_ui.h+++ b/src/ui/app_ui.h@@ -19,6 +19,7 @@#include <set>#include <mutex>#include <string>+#include <string_view>#include <vector>namespace xc::ui {@@ -360,6 +361,12 @@ public:// moving through the screens with it.bool capturingKey() const;void captureKey(uint8_t code);+ // The home search's keys have the focus: a physical keyboard types into+ // the query (the desktop), its letters then don't move through the screens.+ bool wantsTextInput() const;+ // Text typed on that keyboard (UTF-8): added to the query; "\b" deletes+ // the last character, "\n" goes to the results (as Right past the keys).+ void typeText(std::string_view utf8);bool needsRedraw(uint64_t nowMs) const;void render(Canvas& c, uint64_t nowMs);