Xbox Cloud Gaming app for PS5 Homebrew
C
Linux platform layer: XDG data folder, files beside the executable, toasts
Rafael Abud Ferreira4f1a825fa1d6
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 13f7f4a..b6c9745 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -41,6 +41,7 @@ add_library(xcloud_core STATICsrc/input/keyboard_nav.cppsrc/xcloud/social.cppsrc/platform/posix_common.cpp+ src/platform/toasts.cppsrc/input/gyro.cpp)target_include_directories(xcloud_core PUBLIC src)@@ -142,7 +143,7 @@ if(XCLOUD_PS5)target_link_libraries(xcloud_app PUBLIC xcloud_stream xcloud_media xcloud_ui)else()target_link_libraries(xcloud_core PUBLIC Threads::Threads)- target_sources(xcloud_core PRIVATE src/platform/host/platform_host.cpp)+ target_sources(xcloud_core PRIVATE src/platform/linux/platform_linux.cpp)# No screen, pads, USB keyboard or hardware decoder: what a desktop# build links in place of the PS5's backends (docs/multiplatform.md).diff --git a/src/auth/auth_manager.cpp b/src/auth/auth_manager.cppindex 86ed5fc..36dd59b 100644--- a/src/auth/auth_manager.cpp+++ b/src/auth/auth_manager.cpp@@ -60,6 +60,7 @@ bool AuthManager::save() const {saved_ = false;return false;}+ platform::makePrivate(storePath_); // the refresh token: the user's alone on a desktopsaved_ = true;return true;}diff --git a/src/platform/host/platform_host.cpp b/src/platform/host/platform_host.cppdeleted file mode 100644index bf91882..0000000--- a/src/platform/host/platform_host.cpp+++ /dev/null@@ -1,101 +0,0 @@-// SPDX-License-Identifier: GPL-3.0-or-later-// Copyright (C) 2026 RafaelNGP-// Linux host build: used to develop and validate auth, catalog and streaming-// on a PC before deploying to the console.-#include "platform/platform.h"--#include <cstdio>-#include <cstdlib>-#include <netdb.h>-#include <netinet/in.h>-#include <sys/random.h>-#include <sys/socket.h>-#include <sys/stat.h>--namespace xc::platform {--namespace {-std::string g_dataDir;-}--bool init() {- const char* env = std::getenv("XCLOUD_DATA_DIR");- if (env && *env) {- g_dataDir = env;- } else {- const char* home = std::getenv("HOME");- g_dataDir = std::string(home ? home : ".") + "/.local/share/xcloud-ps5";- }- std::string cmd = g_dataDir;- // mkdir -p- for (size_t i = 1; i <= cmd.size(); ++i) {- if (i == cmd.size() || cmd[i] == '/') ::mkdir(cmd.substr(0, i).c_str(), 0700);- }- return true;-}--void shutdown() {}--std::string dataDir() { return g_dataDir; }-std::string dataDirNote() { return {}; }-std::string appDir() { return g_dataDir; }--std::string assetDir() {- if (const char* env = std::getenv("XCLOUD_ASSETS")) return env;- return "assets";-}--std::string caBundlePath() {- if (const char* env = std::getenv("XCLOUD_CA_BUNDLE")) return env;- static const char* candidates[] = {- "/etc/pki/tls/certs/ca-bundle.crt", // Fedora / RHEL- "/etc/ssl/certs/ca-certificates.crt", // Debian / Ubuntu / Arch- "/etc/ssl/cert.pem", // Alpine / macOS- };- for (const char* c : candidates) {- struct stat st{};- if (::stat(c, &st) == 0) return c;- }- return "assets/cacert.pem";-}--bool randomBytes(void* out, size_t len) {- auto* p = static_cast<unsigned char*>(out);- while (len) {- ssize_t n = ::getrandom(p, len, 0);- if (n <= 0) return false;- p += n;- len -= static_cast<size_t>(n);- }- return true;-}--bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {- addrinfo hints{};- hints.ai_family = AF_INET;- hints.ai_socktype = SOCK_STREAM;- addrinfo* res = nullptr;- int rc = ::getaddrinfo(host.c_str(), nullptr, &hints, &res);- if (rc != 0 || !res) {- err = "DNS lookup failed for " + host + ": " + gai_strerror(rc);- return false;- }- addr = reinterpret_cast<sockaddr_in*>(res->ai_addr)->sin_addr.s_addr;- ::freeaddrinfo(res);- return true;-}--void probeNetworking() {}--void notify(const std::string& text) { std::printf("\n>>> %s\n\n", text.c_str()); }-void notify(const std::string& text, const std::string&) { notify(text); }--bool systemKeyboardAvailable() { return false; }-bool openSystemKeyboard(const std::string&, const std::string&, size_t, KeyboardKind) { return false; }-KeyboardStatus pollSystemKeyboard(std::string&) { return KeyboardStatus::Closed; }--bool restartApp() { return false; }--std::string firmwareVersion() { return {}; }--} // namespace xc::platformdiff --git a/src/platform/linux/platform_linux.cpp b/src/platform/linux/platform_linux.cppnew file mode 100644index 0000000..e6c5efa--- /dev/null+++ b/src/platform/linux/platform_linux.cpp@@ -0,0 +1,168 @@+// SPDX-License-Identifier: GPL-3.0-or-later+// Copyright (C) 2026 RafaelNGP+// The Linux desktop's platform layer (psbox, psbox-headless, xcloud-cli,+// xcloud-tests): the data folder where XDG puts it, the assets beside the+// executable, and notifications as toasts inside the desktop app (or on+// stdout for the tools).+#include "platform/platform.h"++#include "platform/toasts.h"+#include "util/log.h"++#include <cerrno>+#include <csignal>+#include <cstdio>+#include <cstdlib>+#include <cstring>+#include <netdb.h>+#include <netinet/in.h>+#include <sys/random.h>+#include <sys/socket.h>+#include <sys/stat.h>+#include <unistd.h>++namespace xc::platform {++namespace {++std::string g_dataDir, g_dataDirNote;++bool isDir(const std::string& path) {+ struct stat st {};+ return ::stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode);+}++// mkdir -p; the folders it makes are the user's alone (the sign-in is kept there).+void makeDirs(const std::string& path) {+ for (size_t i = 1; i <= path.size(); ++i)+ if (i == path.size() || path[i] == '/') ::mkdir(path.substr(0, i).c_str(), 0700);+}++// The folder of the running executable; empty when /proc doesn't say.+const std::string& exeDir() {+ static const std::string dir = [] {+ char buf[4096];+ ssize_t n = ::readlink("/proc/self/exe", buf, sizeof buf - 1);+ if (n <= 0) return std::string();+ std::string path(buf, static_cast<size_t>(n));+ size_t slash = path.rfind('/');+ if (slash == std::string::npos) return std::string();+ return slash ? path.substr(0, slash) : std::string("/");+ }();+ return dir;+}++} // namespace++bool init() {+ // A server closing a connection while it is written to: an error to+ // handle, not the end of the app (the default for SIGPIPE).+ std::signal(SIGPIPE, SIG_IGN);+ const char* env = std::getenv("XCLOUD_DATA_DIR");+ if (env && *env) {+ g_dataDir = env;+ } else {+ const char* home = std::getenv("HOME");+ const std::string homeDir = home && *home ? home : ".";+ // XDG: a relative XDG_DATA_HOME is to be ignored.+ const char* xdg = std::getenv("XDG_DATA_HOME");+ const std::string base = xdg && *xdg == '/' ? std::string(xdg) : homeDir + "/.local/share";+ g_dataDir = base + "/psbox";+ // Before the desktop app the folder was ~/.local/share/xcloud-ps5:+ // moved once, the first time the new one is missing.+ const std::string old = homeDir + "/.local/share/xcloud-ps5";+ if (!isDir(g_dataDir) && isDir(old)) {+ makeDirs(base);+ if (std::rename(old.c_str(), g_dataDir.c_str()) == 0) {+ g_dataDirNote = "moved from " + old;+ } else {+ g_dataDirNote = "the old folder kept (" + old + " could not be moved: " + std::strerror(errno) + ")";+ g_dataDir = old;+ }+ }+ }+ makeDirs(g_dataDir);+ return true;+}++void shutdown() {}++std::string dataDir() { return g_dataDir; }+std::string dataDirNote() { return g_dataDirNote; }+// Where the executable is (an update would go there): never the data folder.+std::string appDir() { return exeDir().empty() ? std::string(".") : exeDir(); }++std::string assetDir() {+ if (const char* env = std::getenv("XCLOUD_ASSETS"); env && *env) return env;+ // The desktop app's build copies assets/ next to it; the tools run from+ // the source tree.+ if (!exeDir().empty() && isDir(exeDir() + "/assets")) return exeDir() + "/assets";+ return "assets";+}++std::string caBundlePath() {+ if (const char* env = std::getenv("XCLOUD_CA_BUNDLE"); env && *env) return env;+ static const char* candidates[] = {+ "/etc/pki/tls/certs/ca-bundle.crt", // Fedora / RHEL+ "/etc/ssl/certs/ca-certificates.crt", // Debian / Ubuntu / Arch+ "/etc/ssl/cert.pem", // Alpine / macOS+ };+ for (const char* c : candidates) {+ struct stat st {};+ if (::stat(c, &st) == 0) return c;+ }+ return assetDir() + "/cacert.pem";+}++bool randomBytes(void* out, size_t len) {+ auto* p = static_cast<unsigned char*>(out);+ while (len) {+ ssize_t n = ::getrandom(p, len, 0);+ if (n <= 0) return false;+ p += n;+ len -= static_cast<size_t>(n);+ }+ return true;+}++bool resolveIPv4(const std::string& host, uint32_t& addr, std::string& err) {+ addrinfo hints{};+ hints.ai_family = AF_INET;+ hints.ai_socktype = SOCK_STREAM;+ addrinfo* res = nullptr;+ int rc = ::getaddrinfo(host.c_str(), nullptr, &hints, &res);+ if (rc != 0 || !res) {+ err = "DNS lookup failed for " + host + ": " + gai_strerror(rc);+ return false;+ }+ addr = reinterpret_cast<sockaddr_in*>(res->ai_addr)->sin_addr.s_addr;+ ::freeaddrinfo(res);+ return true;+}++void probeNetworking() {}++void notify(const std::string& text) { notify(text, text); }++void notify(const std::string& text, const std::string& forLog) {+ if (toastsEnabled()) {+ // The desktop app: a toast in the window. The log gets `forLog`, as+ // on the PS5 (`text` may name the user).+ XC_LOGI("NOTIFY: %s", forLog.c_str());+ toasts().push(text, nowMs());+ return;+ }+ std::printf("\n>>> %s\n\n", text.c_str());+}++void makePrivate(const std::string& path) { ::chmod(path.c_str(), 0600); }++bool systemKeyboardAvailable() { return false; }+bool openSystemKeyboard(const std::string&, const std::string&, size_t, KeyboardKind) { return false; }+KeyboardStatus pollSystemKeyboard(std::string&) { return KeyboardStatus::Closed; }++bool restartApp() { return false; }++std::string firmwareVersion() { return {}; }++} // namespace xc::platformdiff --git a/src/platform/platform.h b/src/platform/platform.hindex e83256a..c2f2b19 100644--- a/src/platform/platform.h+++ b/src/platform/platform.h@@ -1,7 +1,7 @@// SPDX-License-Identifier: GPL-3.0-or-later// Copyright (C) 2026 RafaelNGP-// Thin platform layer: everything that differs between the Linux host build-// and the PS5 build lives behind these functions (see src/platform/host and+// Thin platform layer: everything that differs between the desktop builds+// and the PS5 build lives behind these functions (see src/platform/linux and// src/platform/ps5).#pragma once@@ -18,7 +18,8 @@ void shutdown();// Writable directory for tokens, caches and logs (no trailing slash). On the// PS5 outside the app's folder, so uninstalling, reinstalling or a store-// update keeps them.+// update keeps them; on Linux $XCLOUD_DATA_DIR, else $XDG_DATA_HOME/psbox+// (~/.local/share/psbox).std::string dataDir();// What start-up did about the data folder (moved from the app's folder,// fell back...), for the log once it is open; empty when nothing.@@ -52,7 +53,8 @@ struct Thread {};bool startThread(Thread& t, std::function<void()> fn, size_t stackBytes = 8u << 20);-// Short on-screen message (PS5 system notification / host stdout).+// Short on-screen message: the PS5's system notification; in the desktop+// app a toast in the window (platform/toasts.h); stdout for the tools.void notify(const std::string& text);// The same, with `forLog` written to the log instead of `text` (which may// name the user: the log gets attached to public bug reports).@@ -78,5 +80,8 @@ std::string firmwareVersion();bool readFile(const std::string& path, std::string& out);bool writeFileAtomic(const std::string& path, const std::string& data);+// A file holding the sign-in, once written: readable by this user only on a+// desktop (mode 0600); nothing on the PS5, which has no other users.+void makePrivate(const std::string& path);} // namespace xc::platformdiff --git a/src/platform/ps5/platform_ps5.cpp b/src/platform/ps5/platform_ps5.cppindex 762fd12..f5dc3ad 100644--- a/src/platform/ps5/platform_ps5.cpp+++ b/src/platform/ps5/platform_ps5.cpp@@ -197,6 +197,9 @@ std::string firmwareVersion() {return s;}+// One user per app sandbox: nothing to hide the sign-in from.+void makePrivate(const std::string&) {}+void notify(const std::string& text) { notify(text, text); }void notify(const std::string& text, const std::string& forLog) {diff --git a/src/platform/toasts.cpp b/src/platform/toasts.cppnew file mode 100644index 0000000..83fb082--- /dev/null+++ b/src/platform/toasts.cpp@@ -0,0 +1,65 @@+// SPDX-License-Identifier: GPL-3.0-or-later+// Copyright (C) 2026 RafaelNGP+// The in-app notices' queue (platform/toasts.h).+#include "platform/toasts.h"++#include <algorithm>++namespace xc::platform {++namespace {+std::atomic<bool> g_enabled{false};+} // namespace++void ToastQueue::push(const std::string& text, uint64_t nowMs) {+ if (text.empty()) return;+ std::lock_guard<std::mutex> lock(mutex_);+ std::erase_if(toasts_, [&](const Toast& t) { return t.text == text; });+ if (toasts_.size() >= kMaxQueued) toasts_.erase(toasts_.begin());+ toasts_.push_back({text, false, 0});+ ++version_;+ update(nowMs);+}++void ToastQueue::update(uint64_t nowMs) {+ size_t before = toasts_.size();+ std::erase_if(toasts_, [&](const Toast& t) { return t.shown && nowMs - t.shownAt >= kShowMs; });+ if (toasts_.size() != before) ++version_;+ for (size_t i = 0; i < toasts_.size() && i < kMaxShown; ++i) {+ if (toasts_[i].shown) continue;+ toasts_[i].shown = true;+ toasts_[i].shownAt = nowMs;+ ++version_;+ }+}++std::vector<std::string> ToastQueue::visible(uint64_t nowMs) {+ std::lock_guard<std::mutex> lock(mutex_);+ update(nowMs);+ std::vector<std::string> out;+ for (const auto& t : toasts_)+ if (t.shown) out.push_back(t.text);+ return out;+}++uint64_t ToastQueue::version(uint64_t nowMs) {+ std::lock_guard<std::mutex> lock(mutex_);+ update(nowMs);+ return version_;+}++void ToastQueue::clear() {+ std::lock_guard<std::mutex> lock(mutex_);+ if (!toasts_.empty()) ++version_;+ toasts_.clear();+}++ToastQueue& toasts() {+ static ToastQueue queue;+ return queue;+}++void enableToasts(bool on) { g_enabled = on; }+bool toastsEnabled() { return g_enabled; }++} // namespace xc::platformdiff --git a/src/platform/toasts.h b/src/platform/toasts.hnew file mode 100644index 0000000..03d3940--- /dev/null+++ b/src/platform/toasts.h@@ -0,0 +1,51 @@+// SPDX-License-Identifier: GPL-3.0-or-later+// Copyright (C) 2026 RafaelNGP+// Short notices shown inside the app: platform::notify() in the desktop app,+// where there is no system notification to send. A thread-safe queue that+// the menus (ui::AppUi) and the streaming overlay (StreamScreen) draw from.+// Off until the desktop shell turns it on: the PS5 sends system+// notifications, the tools print to stdout.+#pragma once++#include <atomic>+#include <cstdint>+#include <mutex>+#include <string>+#include <vector>++namespace xc::platform {++class ToastQueue {+public:+ static constexpr uint64_t kShowMs = 5000; // each one's time on screen+ static constexpr size_t kMaxShown = 3; // at once; the next ones wait their turn+ static constexpr size_t kMaxQueued = 12; // the oldest dropped beyond this++ // A notice; the same text again restarts it instead of stacking up.+ void push(const std::string& text, uint64_t nowMs);+ // The notices on screen at `nowMs`, oldest first: those whose time is up+ // are dropped and waiting ones take their place.+ std::vector<std::string> visible(uint64_t nowMs);+ // Changes whenever visible() would answer differently: a redraw is due.+ uint64_t version(uint64_t nowMs);+ void clear();++private:+ struct Toast {+ std::string text;+ bool shown = false;+ uint64_t shownAt = 0;+ };+ void update(uint64_t nowMs); // mutex_ held++ std::mutex mutex_;+ std::vector<Toast> toasts_;+ uint64_t version_ = 0;+};++// The app's queue; platform::notify() pushes to it once it is enabled.+ToastQueue& toasts();+void enableToasts(bool on);+bool toastsEnabled();++} // namespace xc::platformdiff --git a/tests/test_main.cpp b/tests/test_main.cppindex f43f387..4ee59de 100644--- a/tests/test_main.cpp+++ b/tests/test_main.cpp@@ -16,6 +16,7 @@#include "input/tuning.h"#include "net/http.h"#include "platform/platform.h"+#include "platform/toasts.h"#include "stream/input_packet.h"#include "stream/stream_session.h"#include "ui/app_ui.h"@@ -1138,6 +1139,42 @@ static void testGyro() {// The desktop's stand-ins for the PS5 backends: nothing connected, the// settings kept for their getters.+// The desktop app's in-app notices (platform/toasts.h).+static void testToasts() {+ using platform::ToastQueue;+ ToastQueue q;+ uint64_t v0 = q.version(1000);+ CHECK(q.visible(1000).empty() && q.version(1000) == v0);+ q.push("one", 1000);+ q.push("", 1000); // nothing+ CHECK(q.visible(1000) == std::vector<std::string>{"one"});+ uint64_t v1 = q.version(1000);+ CHECK(v1 != v0 && q.version(1001) == v1); // nothing changed: no redraw+ q.push("two", 2000);+ q.push("three", 2500);+ q.push("four", 3000); // waits: three on screen at most+ CHECK((q.visible(3000) == std::vector<std::string>{"one", "two", "three"}));+ // "one" has had its time: "four" takes its place, and the version moves.+ uint64_t v2 = q.version(5999);+ CHECK(q.version(6000) != v2);+ CHECK((q.visible(6000) == std::vector<std::string>{"two", "three", "four"}));+ // The same text again restarts it at the end instead of stacking.+ q.push("two", 6500);+ CHECK((q.visible(6500) == std::vector<std::string>{"three", "four", "two"}));+ CHECK((q.visible(7499) == std::vector<std::string>{"three", "four", "two"}));+ CHECK((q.visible(7500) == std::vector<std::string>{"four", "two"})); // three's time (from 2500) is up+ CHECK((q.visible(11000) == std::vector<std::string>{"two"})); // four's (from 6000)+ CHECK(q.visible(11500).empty());+ // Bounded: the oldest dropped beyond kMaxQueued.+ for (size_t i = 0; i < ToastQueue::kMaxQueued + 5; ++i) q.push("n" + std::to_string(i), 20000);+ auto shown = q.visible(20000);+ CHECK(shown.size() == ToastQueue::kMaxShown && shown.front() == "n5");+ q.clear();+ CHECK(q.visible(20000).empty());+ // Off by default: the tools keep printing notifications.+ CHECK(!platform::toastsEnabled());+}+static void testNullBackends() {CHECK(input::init());input::ControllerState pad;@@ -1161,6 +1198,7 @@ static void testNullBackends() {int main() {testNullBackends();+ testToasts();testLoginRetry();testGyro();testReport();