Xbox Cloud Gaming app for PS5 Homebrew
C
Touch on while the game is out of focus; overlay alpha; autotest "pad"
Rafael Abud Ferreira186392fb0147
diff --git a/src/app/ps5_main.cpp b/src/app/ps5_main.cppindex 501da7e..3c4455f 100644--- a/src/app/ps5_main.cpp+++ b/src/app/ps5_main.cpp@@ -27,6 +27,7 @@#include "xcloud/regions.h"#include <atomic>+#include <cmath>#include <ctime>#include <map>#include <strings.h>@@ -157,6 +158,7 @@ int g_autoplaySeconds = 0;bool g_autoplayDump = false;int g_autoplayRuns = 1;bool g_autoplayIdle = false;+bool g_autoplayPad = false; // autoplay "pad": the physical pad stays in use, its buttons loggedbool g_autoplayDetailTest = false; // open a game to buy far down the list, save its pagebool g_autoplayLibraryTest = false; // open "Your games", save it at 4 s and 25 sbool g_autoplayVkTest = false; // the GPU presenting instead of the CPU display@@ -201,6 +203,7 @@ void loadAutoplay() {if (opt.rfind("swap=", 0) == 0) display::gpu::setSwapImages(std::atoi(opt.c_str() + 5));if (opt == "nopace") display::gpu::setPresentWait(false);if (opt == "detailtest") g_autoplayDetailTest = true;+ if (opt == "pad") g_autoplayPad = true;if (opt == "librarytest") g_autoplayLibraryTest = true;if (opt == "imetest") g_autoplayImeTest = true;if (opt == "menutest") g_autoplayMenuTest = true;@@ -815,6 +818,7 @@ int main(int argc, char** argv) {const app::StreamPlayer* overlayPlayer = nullptr;int streamResolution = 0; // as SettingsChoice::resolutionbool showStats = false, overlayShown = false;+ bool touchEnabled = false; // touch input announced on (StreamPlayer::setTouchEnabled)int sharpness = 0; // 0..3, as Settings::sharpnessint deband = 1; // 0..2, as Settings::debandint upscaler = 0; // as Settings::upscaler@@ -832,8 +836,34 @@ int main(int argc, char** argv) {bool uiSaved = false, launchSaved = false;for (;;) {input::poll(pad);- // Autoplay runs unattended: the physical pad must not interfere.- if (!g_autoplayTitle.empty()) pad = input::ControllerState{};+ // Autoplay runs unattended: the physical pad must not interfere+ // (unless "pad": someone is playing along, and each press is logged).+ if (!g_autoplayTitle.empty() && !g_autoplayPad) pad = input::ControllerState{};+ if (g_autoplayPad) {+ static std::string lastPressed;+ std::string pressed;+ auto add = [&](bool on, const char* name) {+ if (on) pressed += pressed.empty() ? name : std::string(" ") + name;+ };+ add(pad.btnA, "cross");+ add(pad.btnB, "circle");+ add(pad.btnX, "square");+ add(pad.btnY, "triangle");+ add(pad.dpadUp, "up");+ add(pad.dpadDown, "down");+ add(pad.dpadLeft, "left");+ add(pad.dpadRight, "right");+ add(pad.btnL1, "L1");+ add(pad.btnR1, "R1");+ add(pad.btnOptions, "options");+ add(pad.btnTouchpad, "touchpad");+ add(pad.triggerL2 > 0.5f, "L2");+ add(pad.triggerR2 > 0.5f, "R2");+ add(std::abs(pad.leftStickX) > 0.5f || std::abs(pad.leftStickY) > 0.5f, "lstick");+ add(std::abs(pad.rightStickX) > 0.5f || std::abs(pad.rightStickY) > 0.5f, "rstick");+ if (pressed != lastPressed && !pressed.empty()) XC_LOGI("pad: %s", pressed.c_str());+ lastPressed = pressed;+ }uint64_t now = platform::nowMs();if (now - lightBarAt >= 100) { // the light bar follows the game in focuslightBarAt = now;@@ -983,6 +1013,15 @@ int main(int argc, char** argv) {}input::ControllerState sent = pad;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,+ // which then shows its own pad-driven cursor), touch input is+ // on, as the web client has it on touch screens.+ bool unfocused = !g_player->titleFocused();+ if (unfocused != touchEnabled) {+ touchEnabled = unfocused;+ g_player->setTouchEnabled(unfocused);+ }// The menu and the keyboard have the pad while they are up,// and the buttons that closed them until they are let go.bool held = pad.dpadUp || pad.dpadDown || pad.dpadLeft || pad.dpadRight || pad.btnA || pad.btnB ||@@ -1030,6 +1069,7 @@ int main(int argc, char** argv) {platform::sleepMs(8); // ~120 Hz inputcontinue;}+ touchEnabled = false; // each stream starts with touch offif (overlayShown) {display::setOverlay(nullptr, 0, 0, 0, 0, 0);overlayShown = false;diff --git a/src/app/stream_player.cpp b/src/app/stream_player.cppindex a42d46b..4eab779 100644--- a/src/app/stream_player.cpp+++ b/src/app/stream_player.cpp@@ -48,6 +48,7 @@ struct StreamPlayer::Impl {// Reconnecting after a lost connection, as the xbox.com client does: up// to 20 attempts a second apart while the cloud session is still there.std::atomic<bool> reconnecting{false};+ std::atomic<bool> titleFocused{true};int reconnectAttempts = 0;std::atomic<uint32_t> reconnects{0};uint64_t lastRtpPackets = 0;@@ -330,6 +331,9 @@ struct StreamPlayer::Impl {input::setRumble(scale(v.leftMotor), scale(v.rightMotor), v.durationMs, v.gamepadIndex);input::setTriggerRumble(scale(v.leftTrigger), scale(v.rightTrigger), v.durationMs, v.gamepadIndex);};+ cb.titleFocus = [this](bool focused) {+ if (titleFocused.exchange(focused) != focused) XC_LOGI("title %s the focus", focused ? "has" : "lost");+ };cb.idleWarning = [](int seconds) {platform::notify(ui::trf(ui::Str::IdleWarning, std::to_string(seconds)));};@@ -568,6 +572,12 @@ void StreamPlayer::sendInput(const input::ControllerState& p, int index) {s->sendGamepad(f);}+bool StreamPlayer::titleFocused() const { return impl_->titleFocused; }++void StreamPlayer::setTouchEnabled(bool on) {+ if (auto s = impl_->current(); s && impl_->running) s->setTouchEnabled(on);+}+void StreamPlayer::setPadConnected(int index, bool connected) {if (auto s = impl_->current(); s && impl_->running) s->setGamepadConnected(index, connected);}diff --git a/src/app/stream_player.h b/src/app/stream_player.hindex 149dfc7..928e038 100644--- a/src/app/stream_player.h+++ b/src/app/stream_player.h@@ -26,6 +26,11 @@ public:// From the input thread, once per polled pad state; `index` 0..3 is the// controller's slot (input::pollPad).void sendInput(const input::ControllerState& pad, int index = 0);+ // False while another window has the focus on the cloud console (the+ // account picker, a publisher's page).+ bool titleFocused() const;+ // Touch input announced on or off (off at the start).+ void setTouchEnabled(bool on);// Controllers 1..3 coming and going (0 is there from the start).void setPadConnected(int index, bool connected);// The game's requests for text, oldest first (main thread): false whendiff --git a/src/display/display.cpp b/src/display/display.cppindex 34bd903..a389255 100644--- a/src/display/display.cpp+++ b/src/display/display.cpp@@ -323,7 +323,7 @@ void convertTiles(const YuvJob& job, unsigned first, unsigned last) {int ox = static_cast<int>(x) - ov->x, oy = static_cast<int>(y) - ov->y;if (ox >= 0 && oy >= 0 && ox < ov->w && oy < ov->h) {uint32_t p = ov->px[static_cast<size_t>(oy) * ov->w + ox];- uint32_t a = ov->alpha, na = 255 - a;+ uint32_t a = (p >> 24) * ov->alpha / 255, na = 255 - a;r = ((p & 0xFF) * a + r * na) / 255;g = (((p >> 8) & 0xFF) * a + g * na) / 255;b = (((p >> 16) & 0xFF) * a + b * na) / 255;diff --git a/src/display/gpu.cpp b/src/display/gpu.cppindex 24d3c6b..f8a4dbf 100644--- a/src/display/gpu.cpp+++ b/src/display/gpu.cpp@@ -873,16 +873,16 @@ void setOverlay(const uint32_t* pixels, int x, int y, int w, int h, uint8_t opacstd::fill(g_overlayPixels.begin(), g_overlayPixels.end(), 0u);g_overlayShown = pixels && w > 0 && h > 0;if (g_overlayShown) {- // Alpha carries the opacity: RCAS mixes by it.- uint32_t alpha = static_cast<uint32_t>(opacity) << 24;+ // Alpha carries the opacity (times the pixel's own): RCAS mixes by it.for (int row = 0; row < h; ++row) {int sy = y + row;if (sy < 0 || sy >= static_cast<int>(kUiH)) continue;for (int col = 0; col < w; ++col) {int sx = x + col;if (sx < 0 || sx >= static_cast<int>(kUiW)) continue;- g_overlayPixels[static_cast<size_t>(sy) * kUiW + sx] =- (pixels[static_cast<size_t>(row) * w + col] & 0x00FFFFFFu) | alpha;+ uint32_t p = pixels[static_cast<size_t>(row) * w + col];+ uint32_t alpha = (p >> 24) * opacity / 255;+ g_overlayPixels[static_cast<size_t>(sy) * kUiW + sx] = (p & 0x00FFFFFFu) | (alpha << 24);}}}diff --git a/src/stream/stream_session.cpp b/src/stream/stream_session.cppindex da54658..0820a1f 100644--- a/src/stream/stream_session.cpp+++ b/src/stream/stream_session.cpp@@ -282,6 +282,8 @@ struct StreamSession::Impl {if (!opt.resolutionAlias.empty()) sendResolution(opt.resolutionAlias);+ // One touch point, as the web client announces (touch is then turned+ // on only while a touch screen is up: setTouchEnabled()).sendBinary(input, clientMetadataReport(0, nowMs(), static_cast<uint8_t>(std::getenv("XC_TOUCH_POINTS") ? std::atoi(std::getenv("XC_TOUCH_POINTS")) : 1)));sendClientConfig();{@@ -316,6 +318,10 @@ struct StreamSession::Impl {std::string target = (*j)["target"].str();std::string id = (*j)["id"].str();XC_LOGI("message %s %s", type.c_str(), target.c_str());+ if (target == "/streaming/properties/titleinfo") {+ auto info = json::parse((*j)["content"].str());+ if (info && cb.titleFocus) cb.titleFocus((*info)["focused"].asBool());+ }if (target == "/streaming/sessionLifetimeManagement/serverInitiatedDisconnect") {std::string body = (*j)["content"].str();XC_LOGW("server disconnect: %s", body.substr(0, 300).c_str());@@ -641,6 +647,13 @@ void StreamSession::sendGamepad(const GamepadFrame& frame) {Impl::sendBinary(impl_->input, gamepadReport(impl_->inputSequence++, impl_->nowMs(), frame));}+void StreamSession::setTouchEnabled(bool on) {+ if (!impl_->open) return;+ json::Value touch = json::Value::object();+ touch.set("touchInputEnabled", on);+ Impl::sendText(impl_->message, impl_->messageEnvelope("/streaming/characteristics/touchinputenabledchanged", touch));+}+void StreamSession::setGamepadConnected(int index, bool connected) {if (!impl_->open) return;XC_LOGI("gamepad %d %s", index, connected ? "attached" : "detached");diff --git a/src/stream/stream_session.h b/src/stream/stream_session.hindex 4ca0921..b826a06 100644--- a/src/stream/stream_session.h+++ b/src/stream/stream_session.h@@ -35,6 +35,9 @@ struct StreamCallbacks {// The server will end the session for inactivity in `seconds` unless// input arrives.std::function<void(int seconds)> idleWarning;+ // The game gained or lost the focus on the cloud console (lost: another+ // window is up, such as a publisher's web page wanting touch).+ std::function<void(bool focused)> titleFocus;// Set: the game's keyboard requests come here (else the server draws its// own); `textInputCancelled` when the game withdraws one.std::function<void(const TextInputRequest&)> textInput;@@ -83,6 +86,8 @@ public:bool isOpen() const;void sendGamepad(const GamepadFrame& frame);+ // Touch input on or off (off at the start).+ void setTouchEnabled(bool on);// A controller at `index` (1..3; 0 is attached at the start) came or went.void setGamepadConnected(int index, bool connected);void requestKeyframe();