Skip to content

Xbox Cloud Gaming app for PS5 Homebrew

C

WIP: the players' row shows the keyboard in its player's slot while it was used after that player's controller, and switches at once

Rafael Abud Ferreira8a4860f4fca2
diff --git a/src/app/ps5_main.cpp b/src/app/ps5_main.cpp
index 99539b2..bbb56d0 100644
--- a/src/app/ps5_main.cpp
+++ b/src/app/ps5_main.cpp
@@ -287,6 +287,21 @@ int main(int argc, char** argv) {
input::ControllerState prev{}, pad{};
bool appMenuKeyBefore = false, homeKeyBefore = false; // F12 / Home held at the last pass
bool kbmConnected = false; // a USB keyboard or mouse in use
+ uint64_t keyboardActiveAt = 0; // when a key or the mouse was last used (ms)
+ // The slot showing the keyboard (-1 none): the keyboard's player, while
+ // the keyboard was used after that player's own controller (or it has none).
+ auto keyboardIconSlot = [&]() -> int {
+ if (!kbmConnected) return -1;
+ int player;
+ {
+ std::lock_guard<std::mutex> lock(g_settingsMutex);
+ player = g_settings.keyboardPlayer;
+ }
+ if (player < 0 || player >= input::kMaxPads) return -1;
+ bool keyboardLast = !input::padConnected(player) || keyboardActiveAt > input::padActiveAt(player);
+ return keyboardLast ? player : -1;
+ };
+ int keyboardIconShown = -2; // as last drawn in the players' row
auto now0 = [] { return platform::nowMs(); };
Repeater up, down, left, right, dpadLeft, dpadRight;
// Smooth scrolling: rows / cards still to move (fractions carry over),
@@ -363,7 +378,7 @@ int main(int argc, char** argv) {
std::abs(pad.leftStickX) > 0.5f || std::abs(pad.leftStickY) > 0.5f ||
std::abs(pad.rightStickX) > 0.5f || std::abs(pad.rightStickY) > 0.5f;
bool keyboard = ui::g_keyboardHints;
- if (keyPressed || kbm.moved) keyboard = true;
+ if (keyPressed || kbm.moved) keyboard = true, keyboardActiveAt = now0();
else if (padUsed) keyboard = false;
if (keyboard != ui::g_keyboardHints) {
ui::g_keyboardHints = keyboard;
@@ -397,6 +412,10 @@ int main(int argc, char** argv) {
appMenuKeyBefore = appMenuKey;
if (!inGame || streamScreen.menuOpen()) input::keyboardToPad(kbm.held, pad);
kbmConnected = kbm.keyboard || kbm.mouse || g_autoplay.kbHints;
+ if (int shown = keyboardIconSlot(); shown != keyboardIconShown) {
+ keyboardIconShown = shown;
+ padsCheckedAt = 0; // the players' row at once, as the device in use changes
+ }
streamScreen.setKbm(std::move(kbm));
{
// The triggers' resistance: in a game and in its Settings tester only.
@@ -436,8 +455,10 @@ int main(int argc, char** argv) {
slots[static_cast<size_t>(i)] = {input::padConnected(i), input::padUserName(i)};
// The keyboard: in a slot of its own as another player's
// controller (Settings), else on player 1's while it's the device in use.
- if (int kb = keyboardPadSlot(kbmConnected); kb >= 0) slots[static_cast<size_t>(kb)] = {true, "", true};
- else if (kbmConnected && ui::g_keyboardHints) slots[0].keyboard = slots[0].connected = true;
+ if (int kb = keyboardIconSlot(); kb >= 0) {
+ slots[static_cast<size_t>(kb)].keyboard = true;
+ slots[static_cast<size_t>(kb)].connected = true;
+ }
g_ui->setPads(slots);
streamScreen.setPads(slots);
}
diff --git a/src/input/controller.cpp b/src/input/controller.cpp
index f0c863a..3df0f14 100644
--- a/src/input/controller.cpp
+++ b/src/input/controller.cpp
@@ -12,6 +12,15 @@
#include "platform/platform.h"
+namespace xc::input {
+bool inUse(const ControllerState& s) {
+ return s.btnA || s.btnB || s.btnX || s.btnY || s.dpadUp || s.dpadDown || s.dpadLeft || s.dpadRight || s.btnL1 ||
+ s.btnR1 || s.btnL3 || s.btnR3 || s.btnOptions || s.btnTouchpad || s.triggerL2 > 0.5f || s.triggerR2 > 0.5f ||
+ std::abs(s.leftStickX) > 0.5f || std::abs(s.leftStickY) > 0.5f || std::abs(s.rightStickX) > 0.5f ||
+ std::abs(s.rightStickY) > 0.5f;
+}
+} // namespace xc::input
+
#if defined(XCLOUD_PS5)
extern "C" {
@@ -152,6 +161,7 @@ struct Pad {
uint32_t lightApplied = 0;
float light[3] = {0, 0, 0};
bool lightStarted = false;
+ std::atomic<uint64_t> activeAt{0}; // the last poll with a button or stick in use (ms)
};
Pad g_pads[kMaxPads];
std::atomic<float> g_deadzone[2] = {0.15f, 0.15f};
@@ -440,10 +450,13 @@ bool pollPad(int index, ControllerState& out) {
out.touchX = std::clamp(pad.touchData.touch[0].x / 1919.0f, 0.0f, 1.0f);
out.touchY = std::clamp(pad.touchData.touch[0].y / 1079.0f, 0.0f, 1.0f);
}
+ if (inUse(out)) slot.activeAt = platform::nowMs();
return true;
}
+uint64_t padActiveAt(int index) { return index >= 0 && index < kMaxPads ? g_pads[index].activeAt.load() : 0; }
+
void setEmbeddedMicActive(bool active, int pad) {
if (pad < 0 || pad >= kMaxPads) return;
int32_t handle = g_pads[pad].handle;
@@ -480,6 +493,7 @@ bool circleConfirms() { return false; }
void setLightBar(uint8_t, uint8_t, uint8_t, int) {}
void resetLightBar(int) {}
bool padConnected(int index) { return index == 0; }
+uint64_t padActiveAt(int) { return 0; }
std::string padUserName(int index) { return index == 0 ? "Player" : ""; }
int32_t padUserId(int) { return 0; }
void setTriggerFeel(int, int, int, bool) {}
diff --git a/src/input/controller.h b/src/input/controller.h
index 9d06eea..f04e18d 100644
--- a/src/input/controller.h
+++ b/src/input/controller.h
@@ -63,6 +63,10 @@ void refreshPads();
// Whether the pad at `index` answered its last poll, and its user's name
// (empty when the slot is free).
bool padConnected(int index);
+// When pad `index` was last used (a button or a stick pushed), ms; 0 never.
+uint64_t padActiveAt(int index);
+// A button pressed or a stick / trigger pushed past halfway.
+bool inUse(const ControllerState& s);
std::string padUserName(int index);
int32_t padUserId(int index = 0);
// Rumble, 0..255 per motor (large = low frequency, small = high frequency),