mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
Fix: Remote Play gets the Steam Deck, Steam Deck Controller, Stream Controller, and touch controls (#2033)
* fix: Remote Play gets the Steam Deck controller Steam's Remote Play client reads the controller itself from its own streaming_client process. The input preload only exposed the virtual Deck controller (and the DualShock 4 / DualSense pads that share its code) to the process named steam, so in Steam Deck controller mode streaming_client was refused /dev/hidraw16 and the stream got no input: streaming_clien: deck: /dev/hidraw16 refused: not the Steam client streaming_client now sees the same Deck, DualShock 4 and DualSense pads as Steam. Each opener gets its own report stream from the read-only ring, so the two processes do not compete for input. The Steam Controller paths stay Steam-only: the paired controller's feature and output slot is serialised only within one process. Fixes #1860 * fix: Remote Play also gets the Steam Controller and touch controls streaming_client now opens the Steam Controller pads (the app's emulated one and a paired controller) and Steam's touch controller, like the Steam Deck pad. A paired controller's feature request and output slots, and the touch controller's state, are shared with the app and written under a seqlock that expects one writer. Their writers now also take a flock on a lock file in the hook directory, so Steam and streaming_client take turns. touch_state() was a template holding its own static mutex, so each caller had a separate one; it now shares a single mutex. The pad type and touch plug watchers run in streaming_client too, so it gets the udev events for mode switches and replugs; those only reach the process that injects them. Poking /dev watchers and moving event0 on the shared filesystem stay with Steam, so they happen once. * fix: the shared slot lock fails safe and never waits forever If the lock file cannot be opened or locked, ProcessSlotLock used to carry on unlocked. Steam alone is still a single writer then, exactly as before streaming_client could open these devices, so it carries on; streaming_client now leaves the paired controller's feature and output slots and the touch state alone instead (feature reads fail with EIO). The first failure is logged. The wait for the lock is now bounded: a session freeze can stop the process holding it, and writers wait with controller_mutex held, so an unbounded flock could stall all input in the other process. Feature reads wait up to their 500 ms reply timeout, outputs 100 ms and touch state 20 ms. When the deadline passes the lock is held by the other process, so neither proceeds; the operation is skipped and logged. * fix: Steam no longer turns motion off under its Remote Play client Steam and streaming_client both set the Deck pad's IMU mode, and both wrote the one word Android reads to decide whether to feed the gyro. Steam keeps toggling its own mode during a stream, so it could switch motion off while the Remote Play layout needed it, and the client never re-sent an unchanged mode. Each process now has its own word. Android reads motion while either wants it, and ignores the Remote Play word once that process is gone. * fix: Remote Play decides motion while Steam has set no mode Until Steam published its IMU mode, Android reported the mode as unknown without reading the Remote Play client's word, so a Remote Play request to stop motion had no effect. The word that has been set now decides, and the mode stays unknown only while neither process has set one. The rule moves into ImuModes so it can be tested on the JVM. --------- Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
This commit is contained in:
1 parent
a4558c841a
commit
5dd973587f
5 files changed
+376
-24
No files matched your search
@@ -37,6 +37,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/eventfd.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/mman.h>
|
||||
@@ -197,6 +198,7 @@ static constexpr size_t kSnapshotButtonCount =
|
||||
|
||||
namespace Logger {
|
||||
void init();
|
||||
void log(const char *message, ...);
|
||||
}
|
||||
|
||||
static void controller_fork_prepare();
|
||||
@@ -288,6 +290,68 @@ static bool fake_vibration_enabled() { return config().vibration_enabled; }
|
||||
static bool fake_xbox360_identity() { return config().xbox360_identity; }
|
||||
static bool fake_uinput_enabled() { return config().uinput_enabled; }
|
||||
|
||||
static bool process_is_steam_client();
|
||||
|
||||
// Steam and its Remote Play client write the same shared slots; the caller's mutex serialises its own threads.
|
||||
// Without the lock Steam stays the only writer, as before streaming_client could open these devices. The wait
|
||||
// is bounded because a session freeze can stop the holder, and callers wait with controller_mutex held.
|
||||
class ProcessSlotLock {
|
||||
public:
|
||||
ProcessSlotLock(int &fd, const char *name, int timeout_ms) : fd_(fd) {
|
||||
int saved_errno = errno;
|
||||
if (fd_ < 0) {
|
||||
std::string path = std::string(fake_hook_dir()) + "/." + name + ".lock";
|
||||
fd_ = fd = static_cast<int>(syscall(SYS_openat, AT_FDCWD, path.c_str(), O_CREAT | O_RDWR | O_CLOEXEC, 0600));
|
||||
}
|
||||
int error = fd_ < 0 ? errno : 0;
|
||||
if (fd_ >= 0) {
|
||||
struct timespec start, now, pause = {0, 1000 * 1000};
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
for (;;) {
|
||||
if (flock(fd_, LOCK_EX | LOCK_NB) == 0) {
|
||||
locked_ = true;
|
||||
break;
|
||||
}
|
||||
if (errno == EINTR) continue;
|
||||
if (errno != EWOULDBLOCK) {
|
||||
error = errno;
|
||||
break;
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
if ((now.tv_sec - start.tv_sec) * 1000 + (now.tv_nsec - start.tv_nsec) / 1000000 >= timeout_ms) {
|
||||
busy_ = true;
|
||||
break;
|
||||
}
|
||||
nanosleep(&pause, nullptr);
|
||||
}
|
||||
}
|
||||
if (busy_) {
|
||||
static std::atomic<unsigned> waits;
|
||||
unsigned n = ++waits;
|
||||
if ((n & (n - 1)) == 0) Logger::log("lock: %s still held after %d ms, skipped (x%u)\n", name, timeout_ms, n);
|
||||
} else if (!locked_) {
|
||||
static std::atomic<bool> noted{false};
|
||||
if (!noted.exchange(true))
|
||||
Logger::log("lock: %s not taken (%s)%s\n", name, strerror(error),
|
||||
process_is_steam_client() ? "" : "; this process leaves the shared slots alone");
|
||||
}
|
||||
errno = saved_errno;
|
||||
}
|
||||
~ProcessSlotLock() {
|
||||
int saved_errno = errno;
|
||||
if (locked_) flock(fd_, LOCK_UN);
|
||||
errno = saved_errno;
|
||||
}
|
||||
bool may_write() const { return locked_ || (!busy_ && process_is_steam_client()); }
|
||||
ProcessSlotLock(const ProcessSlotLock &) = delete;
|
||||
ProcessSlotLock &operator=(const ProcessSlotLock &) = delete;
|
||||
|
||||
private:
|
||||
int fd_;
|
||||
bool locked_ = false;
|
||||
bool busy_ = false;
|
||||
};
|
||||
|
||||
static bool ring_paths_loaded = false;
|
||||
static int next_ff_id = 0;
|
||||
|
||||
@@ -1214,10 +1278,13 @@ struct DeckImu {
|
||||
uint32_t controls;
|
||||
uint32_t pad_type;
|
||||
uint32_t latency;
|
||||
uint32_t remote_imu_mode;
|
||||
uint32_t remote_imu_pid;
|
||||
};
|
||||
static_assert(offsetof(DeckImu, pad_type) == FAKE_INPUT_RING_PAD_TYPE_OFFSET,
|
||||
"DeckImu::pad_type matches FakeInputWriter and the ring reader");
|
||||
static_assert(offsetof(DeckImu, latency) == 48, "DeckImu::latency matches FakeInputWriter");
|
||||
static_assert(offsetof(DeckImu, remote_imu_mode) == 52, "DeckImu::remote_imu_mode matches FakeInputWriter");
|
||||
static constexpr uint32_t LATENCY_CONTROLLER = 1, LATENCY_TOUCH = 2;
|
||||
static constexpr int LOW_LATENCY_REPORT_INTERVAL_US = 1000;
|
||||
static constexpr uint32_t DECK_EXTRA_L4 = 1, DECK_EXTRA_R4 = 2, DECK_EXTRA_L5 = 4,
|
||||
@@ -1424,6 +1491,25 @@ __attribute__((visibility("hidden"))) static bool process_is_steam_client() {
|
||||
return known == 1;
|
||||
}
|
||||
|
||||
// Steam's Remote Play client reads the controller itself, in its own streaming_client process.
|
||||
__attribute__((visibility("hidden"))) static bool process_reads_pads() {
|
||||
static int known;
|
||||
if (known == 0) {
|
||||
bool reader = process_is_steam_client();
|
||||
if (!reader) {
|
||||
char exe[PATH_MAX];
|
||||
ssize_t length = readlink("/proc/self/exe", exe, sizeof(exe) - 1);
|
||||
if (length > 0) {
|
||||
exe[length] = '\0';
|
||||
const char *name = strrchr(exe, '/');
|
||||
reader = !strcmp(name ? name + 1 : exe, "streaming_client");
|
||||
}
|
||||
}
|
||||
known = reader ? 1 : -1;
|
||||
}
|
||||
return known == 1;
|
||||
}
|
||||
|
||||
__attribute__((constructor)) static void log_client_start() {
|
||||
config();
|
||||
if (process_is_steam_client())
|
||||
@@ -1468,7 +1554,7 @@ __attribute__((visibility("hidden"))) static bool is_withdrawn_pad_path(const ch
|
||||
free(fake);
|
||||
return !exposed;
|
||||
}
|
||||
return number == 0 || !process_is_steam_client();
|
||||
return number == 0 || !process_reads_pads();
|
||||
}
|
||||
|
||||
static bool fake_pad_withdrawn(const FakeController &fake) {
|
||||
@@ -1683,7 +1769,7 @@ __attribute__((visibility("hidden"))) static void *sony_report_thread(void *arg)
|
||||
__attribute__((visibility("hidden"))) static int open_deck_hidraw(int flags, uint8_t sony = 0) {
|
||||
const char *path = sony == 4 ? DS4_HIDRAW_PATH : sony == 5 ? DUALSENSE_HIDRAW_PATH : DECK_HIDRAW_PATH;
|
||||
static auto my_open = reinterpret_cast<int (*)(const char *, int, ...)>(dlsym(RTLD_NEXT, "open"));
|
||||
if (!process_is_steam_client()) {
|
||||
if (!process_reads_pads()) {
|
||||
Logger::log("deck: %s refused: not the Steam client\n", path);
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
@@ -1892,7 +1978,7 @@ __attribute__((visibility("hidden"))) static void *triton_pad_thread(void *arg)
|
||||
|
||||
__attribute__((visibility("hidden"))) static int open_triton_hidraw(int flags, bool emulated) {
|
||||
static auto my_open = reinterpret_cast<int (*)(const char *, int, ...)>(dlsym(RTLD_NEXT, "open"));
|
||||
if (!process_is_steam_client()) {
|
||||
if (!process_reads_pads()) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
@@ -2033,15 +2119,19 @@ __attribute__((visibility("hidden"))) static bool touch_plugged() {
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) static bool is_touch_hidraw_path(const char *pathname) {
|
||||
return pathname && !strcmp(pathname, TOUCH_HIDRAW_PATH) && process_is_steam_client() && touch_ring();
|
||||
return pathname && !strcmp(pathname, TOUCH_HIDRAW_PATH) && process_reads_pads() && touch_ring();
|
||||
}
|
||||
|
||||
static std::mutex touch_state_mutex;
|
||||
static int touch_state_lock_fd = -1;
|
||||
|
||||
template <typename F>
|
||||
__attribute__((visibility("hidden"))) static void touch_state(F &&write) {
|
||||
TouchRingFile *ring = touch_ring();
|
||||
if (!ring) return;
|
||||
static std::mutex mutex;
|
||||
std::lock_guard<std::mutex> guard(mutex);
|
||||
std::lock_guard<std::mutex> guard(touch_state_mutex);
|
||||
ProcessSlotLock slot(touch_state_lock_fd, "touch-state", 20);
|
||||
if (!slot.may_write()) return;
|
||||
__atomic_fetch_add(&ring->state_seq, 1, __ATOMIC_ACQ_REL);
|
||||
write(*ring);
|
||||
__atomic_fetch_add(&ring->state_seq, 1, __ATOMIC_ACQ_REL);
|
||||
@@ -2293,9 +2383,10 @@ __attribute__((visibility("hidden"))) static void *pad_type_thread(void *) {
|
||||
struct timespec settle = {0, 300 * 1000 * 1000};
|
||||
nanosleep(&settle, nullptr);
|
||||
int added = pad_type_uevent(now, "add");
|
||||
if (was != PAD_TYPE_XBOX) poke_hidraw_watchers(pad_type_minor(was));
|
||||
if (now != PAD_TYPE_XBOX) poke_hidraw_watchers(pad_type_minor(now));
|
||||
if (was == PAD_TYPE_XBOX || now == PAD_TYPE_XBOX) {
|
||||
bool shared = process_is_steam_client();
|
||||
if (shared && was != PAD_TYPE_XBOX) poke_hidraw_watchers(pad_type_minor(was));
|
||||
if (shared && now != PAD_TYPE_XBOX) poke_hidraw_watchers(pad_type_minor(now));
|
||||
if (shared && (was == PAD_TYPE_XBOX || now == PAD_TYPE_XBOX)) {
|
||||
std::string node = std::string(fake_hook_dir()) + "/event0";
|
||||
std::string aside = std::string(fake_hook_dir()) + "/.event0-switching";
|
||||
if (rename(node.c_str(), aside.c_str()) == 0) {
|
||||
@@ -2314,7 +2405,7 @@ __attribute__((visibility("hidden"))) static void *pad_type_thread(void *) {
|
||||
|
||||
__attribute__((constructor)) static void start_pad_type_watch() {
|
||||
config();
|
||||
if (!process_is_steam_client() || !fake_deck_enabled()) return;
|
||||
if (!process_reads_pads() || !fake_deck_enabled()) return;
|
||||
pthread_t thread;
|
||||
if (pthread_create(&thread, nullptr, pad_type_thread, nullptr) == 0) pthread_detach(thread);
|
||||
}
|
||||
@@ -2335,7 +2426,7 @@ __attribute__((visibility("hidden"))) static void *touch_plug_thread(void *) {
|
||||
|
||||
__attribute__((constructor)) static void start_touch_controller() {
|
||||
config();
|
||||
if (!process_is_steam_client() || !touch_ring()) return;
|
||||
if (!process_reads_pads() || !touch_ring()) return;
|
||||
pthread_t thread;
|
||||
if (pthread_create(&thread, nullptr, touch_plug_thread, nullptr) == 0) pthread_detach(thread);
|
||||
}
|
||||
@@ -2442,8 +2533,13 @@ __attribute__((visibility("hidden"))) static void publish_imu_mode(const DeckHid
|
||||
std::string path = get_ring_path_for_slot(0);
|
||||
int fd = path.empty() ? -1 : my_open(path.c_str(), O_WRONLY | O_CLOEXEC);
|
||||
if (fd < 0) return;
|
||||
uint32_t value = 0x80000000u | static_cast<uint32_t>(mode);
|
||||
if (pwrite(fd, &value, sizeof(value), FAKE_INPUT_RING_SIZE + offsetof(DeckImu, imu_mode)) != sizeof(value))
|
||||
// Steam and its Remote Play client each keep their own word, so one turning motion off does not starve the
|
||||
// other; Android reads motion while either wants it. The pid lets it ignore a Remote Play client that has gone.
|
||||
uint32_t value[2] = {0x80000000u | static_cast<uint32_t>(mode), static_cast<uint32_t>(getpid())};
|
||||
bool steam = process_is_steam_client();
|
||||
size_t length = steam ? sizeof(value[0]) : sizeof(value);
|
||||
off_t offset = FAKE_INPUT_RING_SIZE + (steam ? offsetof(DeckImu, imu_mode) : offsetof(DeckImu, remote_imu_mode));
|
||||
if (pwrite(fd, value, length, offset) != static_cast<ssize_t>(length))
|
||||
Logger::log("deck: imu mode not published (%s)\n", strerror(errno));
|
||||
syscall(SYS_close, fd);
|
||||
}
|
||||
@@ -2648,7 +2744,10 @@ __attribute__((visibility("hidden"))) static void triton_queue_output(DeckHidraw
|
||||
auto *shared = reinterpret_cast<TritonSharedData *>(reinterpret_cast<uint8_t *>(triton.ring) + TRITON_SHARED_OFFSET);
|
||||
if (__atomic_load_n(&shared->magic, __ATOMIC_ACQUIRE) != TRITON_SHARED_MAGIC) return;
|
||||
static std::mutex one_at_a_time;
|
||||
static int lock_fd = -1;
|
||||
std::lock_guard<std::mutex> guard(one_at_a_time);
|
||||
ProcessSlotLock slot(lock_fd, "triton-output", TRITON_OUTPUT_TIMEOUT_MS * 2);
|
||||
if (!slot.may_write()) return;
|
||||
for (int waited = 0;
|
||||
static_cast<uint32_t>(__atomic_load_n(&shared->output_seq, __ATOMIC_ACQUIRE)) !=
|
||||
__atomic_load_n(&shared->output_ack, __ATOMIC_ACQUIRE) &&
|
||||
@@ -2680,7 +2779,13 @@ __attribute__((visibility("hidden"))) static int triton_get_feature(DeckHidraw &
|
||||
}
|
||||
auto *shared = reinterpret_cast<TritonSharedData *>(reinterpret_cast<uint8_t *>(triton.ring) + TRITON_SHARED_OFFSET);
|
||||
static std::mutex one_at_a_time;
|
||||
static int lock_fd = -1;
|
||||
std::lock_guard<std::mutex> guard(one_at_a_time);
|
||||
ProcessSlotLock slot(lock_fd, "triton-feature", TRITON_FEATURE_TIMEOUT_MS);
|
||||
if (!slot.may_write()) {
|
||||
errno = EIO;
|
||||
return -1;
|
||||
}
|
||||
uint8_t report_id = report[0];
|
||||
__atomic_store_n(&shared->get_report_id, report_id, __ATOMIC_RELAXED);
|
||||
uint64_t request = __atomic_add_fetch(&shared->get_request, 1, __ATOMIC_RELEASE);
|
||||
@@ -2868,7 +2973,7 @@ __attribute__((visibility("hidden"))) static int ioctl_triton(DeckHidraw &triton
|
||||
__attribute__((visibility("hidden"))) static std::string note_sys_failure(int dirfd, const char *pathname) {
|
||||
int saved_errno = errno;
|
||||
std::string path;
|
||||
if (saved_errno == ENOENT || !pathname || !process_is_steam_client()) {
|
||||
if (saved_errno == ENOENT || !pathname || !process_reads_pads()) {
|
||||
errno = saved_errno;
|
||||
return path;
|
||||
}
|
||||
@@ -2907,7 +3012,7 @@ __attribute__((visibility("hidden"))) static std::string note_sys_failure(int di
|
||||
|
||||
__attribute__((visibility("hidden"))) static std::string deck_listing_for(const char *path) {
|
||||
const char *listing = deck_listing_dir();
|
||||
if (!path || !listing || errno != EACCES || !fake_deck_enabled() || !process_is_steam_client()) return {};
|
||||
if (!path || !listing || errno != EACCES || !fake_deck_enabled() || !process_reads_pads()) return {};
|
||||
std::string p(path);
|
||||
while (p.size() > 1 && p.back() == '/') p.pop_back();
|
||||
int which = p == "/sys" ? 0 : p == "/sys/class" ? 1 : p == "/sys/bus" ? 2 : -1;
|
||||
@@ -2976,7 +3081,7 @@ EXPORT int statx(int dirfd, const char *path, int flags, unsigned mask, struct s
|
||||
constexpr unsigned unique_mount_id = 0x4000;
|
||||
if (result == 0 && (mask & STATX_MNT_ID) &&
|
||||
!(buf->stx_mask & (STATX_MNT_ID | unique_mount_id)) && fake_deck_enabled() &&
|
||||
process_is_steam_client() && deck_device_library(__builtin_return_address(0))) {
|
||||
process_reads_pads() && deck_device_library(__builtin_return_address(0))) {
|
||||
bool recovered = deck_mount_id_from_fdinfo(dirfd, path, flags, buf);
|
||||
static std::atomic<bool> noted_success{false}, noted_failure{false};
|
||||
if (!(recovered ? noted_success : noted_failure).exchange(true))
|
||||
@@ -3165,7 +3270,7 @@ template <typename S, typename Real>
|
||||
static int fake_stat_path(const char *pathname, S *statbuf, Real real) {
|
||||
if (is_withdrawn_pad_path(pathname) ||
|
||||
((is_deck_hidraw_path(pathname) || is_triton_hidraw_path(pathname) || is_triton_pad_path(pathname) ||
|
||||
sony_pad_path(pathname)) && !process_is_steam_client())) {
|
||||
sony_pad_path(pathname)) && !process_reads_pads())) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
@@ -3290,9 +3395,9 @@ EXPORT int fstat(int fd, struct stat *buf) {
|
||||
EXPORT int access(const char *pathname, int mode) {
|
||||
static auto my_access = reinterpret_cast<decltype(&::access)>(dlsym(RTLD_NEXT, "access"));
|
||||
if (fake_uinput_enabled() && is_uinput_path(pathname)) return 0;
|
||||
if (is_deck_hidraw_path(pathname) && process_is_steam_client()) return 0;
|
||||
if (is_deck_hidraw_path(pathname) && process_reads_pads()) return 0;
|
||||
if ((is_triton_hidraw_path(pathname) || is_triton_pad_path(pathname) || sony_pad_path(pathname)) &&
|
||||
process_is_steam_client())
|
||||
process_reads_pads())
|
||||
return 0;
|
||||
if (is_touch_hidraw_path(pathname)) return 0;
|
||||
if (is_withdrawn_pad_path(pathname) || is_deck_hidraw_path(pathname) || is_triton_hidraw_path(pathname) ||
|
||||
@@ -3331,9 +3436,9 @@ EXPORT int access(const char *pathname, int mode) {
|
||||
EXPORT int faccessat(int dirfd, const char *pathname, int mode, int flags) {
|
||||
static auto my_faccessat = reinterpret_cast<decltype(&::faccessat)>(dlsym(RTLD_NEXT, "faccessat"));
|
||||
if (fake_uinput_enabled() && is_uinput_path(pathname)) return 0;
|
||||
if (is_deck_hidraw_path(pathname) && process_is_steam_client()) return 0;
|
||||
if (is_deck_hidraw_path(pathname) && process_reads_pads()) return 0;
|
||||
if ((is_triton_hidraw_path(pathname) || is_triton_pad_path(pathname) || sony_pad_path(pathname)) &&
|
||||
process_is_steam_client())
|
||||
process_reads_pads())
|
||||
return 0;
|
||||
if (is_touch_hidraw_path(pathname)) return 0;
|
||||
if (is_withdrawn_pad_path(pathname) || is_deck_hidraw_path(pathname) || is_triton_hidraw_path(pathname) ||
|
||||
@@ -3400,7 +3505,7 @@ EXPORT int inotify_add_watch(int fd, const char *pathname, uint32_t mask) {
|
||||
pathname = fake_path;
|
||||
} else if (!strcmp(pathname, "/dev/input")) {
|
||||
pathname = fake_hook_dir();
|
||||
} else if (!strcmp(pathname, "/dev") && fake_deck_enabled() && process_is_steam_client()) {
|
||||
} else if (!strcmp(pathname, "/dev") && fake_deck_enabled() && process_reads_pads()) {
|
||||
std::string events = hidraw_event_dir();
|
||||
mkdir(events.c_str(), 0700);
|
||||
my_inotify_add_watch(fd, events.c_str(), mask);
|
||||
|
||||
@@ -82,6 +82,8 @@ public class FakeInputWriter {
|
||||
private static final int PAD_TYPE_OFFSET = IMU_OFFSET + 44;
|
||||
private static final int PAD_TYPE_SET = 0x80000000;
|
||||
private static final int LATENCY_OFFSET = IMU_OFFSET + 48;
|
||||
private static final int REMOTE_IMU_MODE_OFFSET = IMU_OFFSET + 52;
|
||||
private static final int REMOTE_IMU_PID_OFFSET = IMU_OFFSET + 56;
|
||||
public static final int LATENCY_CONTROLLER = 1;
|
||||
public static final int LATENCY_TOUCH = 2;
|
||||
private static volatile int latency = 0;
|
||||
@@ -231,6 +233,8 @@ public class FakeInputWriter {
|
||||
}
|
||||
data.putInt(IMU_OFFSET, 0);
|
||||
data.putInt(IMU_MODE_OFFSET, 0);
|
||||
data.putInt(REMOTE_IMU_MODE_OFFSET, 0);
|
||||
data.putInt(REMOTE_IMU_PID_OFFSET, 0);
|
||||
data.putLong(IMU_SEQ_OFFSET, 0L);
|
||||
for (int i = 0; i < 12; i++) {
|
||||
data.putShort(IMU_ACCEL_OFFSET + (i * 2), (short) 0);
|
||||
@@ -494,8 +498,11 @@ public class FakeInputWriter {
|
||||
if (ringSlot == null) return -1;
|
||||
synchronized (ringSlot) {
|
||||
ByteBuffer ring = ringSlot.data;
|
||||
int value = ring == null ? 0 : ring.getInt(IMU_MODE_OFFSET);
|
||||
return value < 0 ? value & 0xffff : -1;
|
||||
if (ring == null) return 0;
|
||||
int remote = ring.getInt(REMOTE_IMU_MODE_OFFSET);
|
||||
int pid = ring.getInt(REMOTE_IMU_PID_OFFSET);
|
||||
return ImuModes.combine(ring.getInt(IMU_MODE_OFFSET), remote,
|
||||
remote < 0 && pid > 0 && new File("/proc/" + pid).exists());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.droiddeck.launcher.input;
|
||||
|
||||
final class ImuModes {
|
||||
private ImuModes() {}
|
||||
|
||||
// Steam and its Remote Play client each keep their own mode word, with the top bit set once written. Motion is
|
||||
// read while either process wants it, and a word that has not been set leaves the choice to the other one.
|
||||
// -1 means neither has set a mode yet.
|
||||
static int combine(int steam, int remote, boolean remoteRunning) {
|
||||
boolean remoteSet = remote < 0 && remoteRunning;
|
||||
if (steam >= 0 && !remoteSet) return -1;
|
||||
return ((steam < 0 ? steam : 0) | (remoteSet ? remote : 0)) & 0xffff;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
class FakeInputImuModeTest {
|
||||
private fun set(mode: Int) = Int.MIN_VALUE or mode
|
||||
|
||||
@Test fun motionIsUnknownUntilEitherProcessSetsAMode() {
|
||||
assertEquals(-1, ImuModes.combine(0, 0, GONE))
|
||||
}
|
||||
|
||||
@Test fun motionFollowsSteamAloneBeforeRemotePlayStarts() {
|
||||
assertEquals(0x18, ImuModes.combine(set(0x18), 0, GONE))
|
||||
assertEquals(0, ImuModes.combine(set(0), 0, GONE))
|
||||
}
|
||||
|
||||
@Test fun motionStaysOnWhileEitherProcessWantsIt() {
|
||||
assertEquals(0x18, ImuModes.combine(set(0), set(0x18), RUNNING))
|
||||
assertEquals(0x18, ImuModes.combine(set(0x18), set(0), RUNNING))
|
||||
}
|
||||
|
||||
@Test fun remotePlayDecidesWhileSteamHasSetNoMode() {
|
||||
assertEquals(0, ImuModes.combine(0, set(0), RUNNING))
|
||||
assertEquals(0x18, ImuModes.combine(0, set(0x18), RUNNING))
|
||||
}
|
||||
|
||||
@Test fun aRemotePlayClientThatHasGoneIsIgnored() {
|
||||
assertEquals(0, ImuModes.combine(set(0), set(0x18), GONE))
|
||||
assertEquals(-1, ImuModes.combine(0, set(0x18), GONE))
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val RUNNING = true
|
||||
const val GONE = false
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import fcntl
|
||||
import mmap
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
@@ -5,6 +7,8 @@ import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
|
||||
SOURCE = Path(__file__).resolve().parents[2] / "app/src/main/cpp/fakeinput_steam.cpp"
|
||||
@@ -14,6 +18,14 @@ HEADER = 64
|
||||
EVENT = 24
|
||||
CAPACITY = 4096
|
||||
DESCRIPTION = 1824
|
||||
RING_SIZE = HEADER + CAPACITY * EVENT
|
||||
DECK_IMU = 64
|
||||
TRITON_BLOCK = 256
|
||||
TRITON_MAGIC = 0x4E525454
|
||||
TOUCH_MAGIC = 0x31484354
|
||||
TOUCH_RING_SIZE = 256
|
||||
PAD_TYPE_DECK = 0
|
||||
PAD_TYPE_STEAM = 1
|
||||
READER = r"""
|
||||
#include <fcntl.h>
|
||||
#include <linux/input.h>
|
||||
@@ -215,6 +227,72 @@ int main(int argc, char **argv)
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
HIDRAW_PROBE = r"""
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/hidraw.h>
|
||||
#include <poll.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int fd = open(argv[1], O_RDONLY | O_NONBLOCK);
|
||||
if (fd < 0) {
|
||||
puts(errno == ENOENT ? "refused" : "failed");
|
||||
return 0;
|
||||
}
|
||||
struct hidraw_devinfo info;
|
||||
if (ioctl(fd, HIDIOCGRAWINFO, &info) < 0)
|
||||
return 1;
|
||||
struct pollfd ready = {fd, POLLIN, 0};
|
||||
unsigned char report[64];
|
||||
if (poll(&ready, 1, 2000) != 1 || read(fd, report, sizeof(report)) <= 0)
|
||||
return 2;
|
||||
printf("%04hx:%04hx report %02x\n", info.vendor, info.product, report[0]);
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
FEATURE_PROBE = r"""
|
||||
#include <fcntl.h>
|
||||
#include <linux/hidraw.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
unsigned char id = (unsigned char)strtoul(argv[1], NULL, 0);
|
||||
int fd = open("/dev/hidraw18", O_RDWR);
|
||||
if (fd < 0)
|
||||
return 1;
|
||||
int count = argc > 2 ? atoi(argv[2]) : 200, wrong = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
unsigned char report[64] = {id};
|
||||
if (ioctl(fd, HIDIOCGFEATURE(sizeof(report)), report) < 0 || report[0] != id || report[1] != id)
|
||||
wrong++;
|
||||
}
|
||||
printf("%d wrong\n", wrong);
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
IMU_MODE_PROBE = r"""
|
||||
#include <fcntl.h>
|
||||
#include <linux/hidraw.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
unsigned mode = (unsigned)strtoul(argv[1], NULL, 0);
|
||||
int fd = open("/dev/hidraw16", O_RDWR);
|
||||
if (fd < 0)
|
||||
return 1;
|
||||
unsigned char report[65] = {0x00, 0x87, 3, 48, mode & 0xff, mode >> 8};
|
||||
return ioctl(fd, HIDIOCSFEATURE(sizeof(report)), report) < 0;
|
||||
}
|
||||
"""
|
||||
OVERLAY_READ = r"""
|
||||
#include <sys/uio.h>
|
||||
#include <unistd.h>
|
||||
@@ -348,6 +426,117 @@ class FakeinputRingTest(unittest.TestCase):
|
||||
self.assertEqual(run.returncode, 0, run.stderr)
|
||||
self.assertEqual(run.stdout, "JSIOCGNAME -1 EINVAL\n28de:11ff Microsoft X-Box 360 pad 0\n")
|
||||
|
||||
def pad_ring(self, pad_type, triton_connected=False):
|
||||
hooks, ring = self.ring(virtual=False)
|
||||
data = bytearray(ring.read_bytes()) + bytearray(DECK_IMU + TRITON_BLOCK)
|
||||
struct.pack_into("<I", data, RING_SIZE + 44, 0x80000000 | pad_type)
|
||||
struct.pack_into("<II", data, RING_SIZE + DECK_IMU, TRITON_MAGIC, 1 if triton_connected else 0)
|
||||
ring.write_bytes(bytes(data))
|
||||
env = dict(os.environ, FAKE_EVDEV_DIR=str(hooks), FAKE_EVDEV_DECK="1", FAKE_EVDEV_TRITON="1",
|
||||
FAKE_EVDEV_MEMFD_PATHS="0=" + str(ring))
|
||||
return ring, env
|
||||
|
||||
def as_process(self, binary, name):
|
||||
named = self.tmp / name
|
||||
shutil.copy(binary, named)
|
||||
return str(named)
|
||||
|
||||
def assert_opened_by_steam_and_remote_play_only(self, device, expected, env):
|
||||
library, probe = self.build("-m64", HIDRAW_PROBE, "hidraw-probe")
|
||||
env = dict(env, LD_PRELOAD=str(library))
|
||||
for name, want in (("steam", expected), ("streaming_client", expected), ("game", "refused\n")):
|
||||
with self.subTest(name=name):
|
||||
run = subprocess.run([self.as_process(probe, name), device], capture_output=True, text=True,
|
||||
timeout=10, env=env)
|
||||
self.assertEqual(run.returncode, 0, run.stderr)
|
||||
self.assertEqual(run.stdout, want)
|
||||
|
||||
def test_the_deck_pad_opens_in_steam_and_its_remote_play_client_only(self):
|
||||
_, env = self.pad_ring(PAD_TYPE_DECK)
|
||||
self.assert_opened_by_steam_and_remote_play_only("/dev/hidraw16", "28de:1205 report 01\n", env)
|
||||
|
||||
def test_the_steam_controller_pad_opens_in_steam_and_its_remote_play_client_only(self):
|
||||
_, env = self.pad_ring(PAD_TYPE_STEAM)
|
||||
self.assert_opened_by_steam_and_remote_play_only("/dev/hidraw19", "28de:1302 report 42\n", env)
|
||||
|
||||
def test_the_touch_controller_opens_in_steam_and_its_remote_play_client_only(self):
|
||||
_, env = self.pad_ring(PAD_TYPE_DECK)
|
||||
touch = self.tmp / "touchctl"
|
||||
data = bytearray(TOUCH_RING_SIZE)
|
||||
struct.pack_into("<I", data, 0, TOUCH_MAGIC)
|
||||
struct.pack_into("<I", data, 56, 1)
|
||||
touch.write_bytes(bytes(data))
|
||||
env["FAKE_TOUCHCTL_RING"] = str(touch)
|
||||
self.assert_opened_by_steam_and_remote_play_only("/dev/hidraw17", "0000:11fb report 00\n", env)
|
||||
|
||||
def ask_paired_controller(self, probes, locks_unavailable=False, held=False, count=200):
|
||||
library, probe = self.build("-m64", FEATURE_PROBE, "feature-probe")
|
||||
ring, env = self.pad_ring(PAD_TYPE_DECK, triton_connected=True)
|
||||
env["LD_PRELOAD"] = str(library)
|
||||
lock = Path(env["FAKE_EVDEV_DIR"]) / ".triton-feature.lock"
|
||||
if locks_unavailable:
|
||||
lock.mkdir()
|
||||
holder = open(lock, "w") if held else None
|
||||
if holder:
|
||||
self.addCleanup(holder.close)
|
||||
fcntl.flock(holder, fcntl.LOCK_EX)
|
||||
block = RING_SIZE + DECK_IMU
|
||||
stop = threading.Event()
|
||||
|
||||
def controller():
|
||||
with open(ring, "r+b") as f, mmap.mmap(f.fileno(), 0) as shared:
|
||||
served = 0
|
||||
while not stop.is_set():
|
||||
request = struct.unpack_from("<Q", shared, block + 168)[0]
|
||||
if request == served:
|
||||
time.sleep(0.0001)
|
||||
continue
|
||||
report_id = struct.unpack_from("<I", shared, block + 184)[0]
|
||||
struct.pack_into("<i", shared, block + 188, 2)
|
||||
struct.pack_into("<BB", shared, block + 192, report_id, report_id)
|
||||
struct.pack_into("<Q", shared, block + 176, request)
|
||||
served = request
|
||||
|
||||
thread = threading.Thread(target=controller)
|
||||
thread.start()
|
||||
try:
|
||||
runs = [subprocess.Popen([self.as_process(probe, name), report_id, str(count)], stdout=subprocess.PIPE,
|
||||
text=True, env=env)
|
||||
for name, report_id in probes]
|
||||
outputs = [run.communicate(timeout=60)[0] for run in runs]
|
||||
finally:
|
||||
stop.set()
|
||||
thread.join()
|
||||
self.assertEqual([run.returncode for run in runs], [0] * len(probes))
|
||||
return outputs
|
||||
|
||||
def test_steam_and_its_remote_play_client_take_turns_with_the_paired_controllers_features(self):
|
||||
outputs = self.ask_paired_controller((("steam", "0x11"), ("streaming_client", "0x22")))
|
||||
self.assertEqual(outputs, ["0 wrong\n", "0 wrong\n"])
|
||||
|
||||
def test_without_the_lock_only_steam_asks_the_paired_controller(self):
|
||||
outputs = self.ask_paired_controller((("steam", "0x11"), ("streaming_client", "0x22")), locks_unavailable=True)
|
||||
self.assertEqual(outputs, ["0 wrong\n", "200 wrong\n"])
|
||||
|
||||
def test_a_stopped_lock_holder_does_not_stall_steam_or_its_remote_play_client(self):
|
||||
started = time.monotonic()
|
||||
outputs = self.ask_paired_controller((("steam", "0x11"), ("streaming_client", "0x22")), held=True, count=3)
|
||||
self.assertEqual(outputs, ["3 wrong\n", "3 wrong\n"])
|
||||
self.assertLess(time.monotonic() - started, 20)
|
||||
|
||||
def test_steam_turning_motion_off_leaves_it_on_for_its_remote_play_client(self):
|
||||
library, probe = self.build("-m64", IMU_MODE_PROBE, "imu-mode-probe")
|
||||
ring, env = self.pad_ring(PAD_TYPE_DECK)
|
||||
env["LD_PRELOAD"] = str(library)
|
||||
pids = {}
|
||||
for name, mode in (("steam", "0x18"), ("streaming_client", "0x18"), ("steam", "0x00")):
|
||||
run = subprocess.Popen([self.as_process(probe, name), mode], env=env)
|
||||
self.assertEqual(run.wait(timeout=10), 0)
|
||||
pids[name] = run.pid
|
||||
data = ring.read_bytes()
|
||||
self.assertEqual(struct.unpack_from("<I", data, RING_SIZE + 4)[0], 0x80000000)
|
||||
self.assertEqual(struct.unpack_from("<II", data, RING_SIZE + 52), (0x80000018, pids["streaming_client"]))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user