mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-30 18:15:42 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54ca518553 | ||
|
|
72c8b8426d |
No files matched your search
@@ -59,13 +59,18 @@ object GameStorage {
|
||||
return when {
|
||||
pref == SessionPrefs.GAME_STORAGE_OFF -> null
|
||||
pref.isEmpty() -> options(context).firstOrNull()?.let { Option(it.name, it.path) }
|
||||
else -> Option(SessionPrefs.gameStorageLabel(context), pref).takeIf { attached(File(pref)) }
|
||||
else -> reachable(pref).let { path -> Option(SessionPrefs.gameStorageLabel(context), path).takeIf { attached(File(path)) } }
|
||||
}
|
||||
}
|
||||
|
||||
fun disconnected(context: Context): Boolean {
|
||||
val pref = SessionPrefs.gameStorage(context)
|
||||
return pref.isNotEmpty() && pref != SessionPrefs.GAME_STORAGE_OFF && !attached(File(pref))
|
||||
return pref.isNotEmpty() && pref != SessionPrefs.GAME_STORAGE_OFF && !attached(File(reachable(pref)))
|
||||
}
|
||||
|
||||
internal fun reachable(path: String, usable: (File) -> Boolean = { it.isDirectory && it.canWrite() }): String {
|
||||
val alias = UsbLink.storagePath(path)
|
||||
return if (alias != path && !usable(File(path)) && usable(File(alias))) alias else path
|
||||
}
|
||||
|
||||
internal fun attached(
|
||||
|
||||
@@ -46,7 +46,16 @@ object SecondaryLibrary {
|
||||
}
|
||||
|
||||
fun privateRoot(files: File, library: File): File {
|
||||
val digest = MessageDigest.getInstance("SHA-256").digest(library.canonicalPath.toByteArray())
|
||||
val own = privateDir(files, library.canonicalPath)
|
||||
if (own.exists()) return own
|
||||
val raw = UsbLink.rawPath(library.absolutePath)
|
||||
val alias = if (raw != library.absolutePath) raw else UsbLink.storagePath(library.absolutePath)
|
||||
if (alias == library.absolutePath) return own
|
||||
return privateDir(files, runCatching { File(alias).canonicalPath }.getOrDefault(alias)).takeIf { it.exists() } ?: own
|
||||
}
|
||||
|
||||
private fun privateDir(files: File, path: String): File {
|
||||
val digest = MessageDigest.getInstance("SHA-256").digest(path.toByteArray())
|
||||
.joinToString("") { "%02x".format(it.toInt() and 255) }
|
||||
return File(files, "steam-libraries/$digest")
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.frontend.AddedGames
|
||||
import com.droiddeck.launcher.runtime.BattleNet
|
||||
import com.droiddeck.launcher.runtime.LinuxRuntime
|
||||
import com.droiddeck.launcher.session.GameStorage
|
||||
import com.droiddeck.launcher.stores.CatalogItem
|
||||
import com.droiddeck.launcher.stores.InstalledStoreGame
|
||||
import com.droiddeck.launcher.stores.Store
|
||||
@@ -130,13 +131,13 @@ object BattleNetClient {
|
||||
StoresState.post { opener?.invoke(args) }
|
||||
}
|
||||
|
||||
fun library(context: Context, root: File = preferred(context)): List<String> =
|
||||
AddedGames.guestPath(context, StoreInstallRoot.storeDir(root, Store.BATTLENET))?.let { listOf("--library", it) }.orEmpty()
|
||||
fun library(context: Context, root: File? = preferred(context)): List<String> =
|
||||
root?.let { AddedGames.guestPath(context, StoreInstallRoot.storeDir(it, Store.BATTLENET)) }?.let { listOf("--library", it) }.orEmpty()
|
||||
|
||||
internal fun preferred(context: Context): File {
|
||||
internal fun preferred(context: Context): File? {
|
||||
val second = StoreInstallRoot.libraryRoot(context)?.absolutePath
|
||||
return StoreInstallRoot.targets(context).firstOrNull { it.root.absolutePath == second && (it.root.isDirectory || it.root.mkdirs()) }?.root
|
||||
?: StoreInstallRoot.internalRoot(context)
|
||||
?: StoreInstallRoot.internalRoot(context).takeIf { second == null && !GameStorage.disconnected(context) }
|
||||
}
|
||||
|
||||
fun installGame(context: Context, uid: String, root: File) {
|
||||
|
||||
@@ -114,6 +114,19 @@ class GameStorageTest {
|
||||
assertFalse(GameStorage.disconnected(app))
|
||||
}
|
||||
|
||||
@Test fun aRawCardPathTheProcessCannotUseFallsBackToItsStoragePath() {
|
||||
val usable = setOf("/storage/A042-9B16/Games")
|
||||
assertEquals("/storage/A042-9B16/Games", GameStorage.reachable("/mnt/media_rw/A042-9B16/Games") { it.path in usable })
|
||||
}
|
||||
|
||||
@Test fun aUsableRawPathAndOtherPathsAreKept() {
|
||||
assertEquals("/mnt/media_rw/A042-9B16/Games", GameStorage.reachable("/mnt/media_rw/A042-9B16/Games") { true })
|
||||
assertEquals("/mnt/media_rw/0000-0000/Games", GameStorage.reachable("/mnt/media_rw/0000-0000/Games") { false })
|
||||
assertEquals("/storage/A042-9B16/Games", GameStorage.reachable("/storage/A042-9B16/Games") { false })
|
||||
val folder = tmp.newFolder("library").path
|
||||
assertEquals(folder, GameStorage.reachable(folder) { false })
|
||||
}
|
||||
|
||||
@Test fun onlyAVfatMountCountsAsFat32() {
|
||||
val mounts = """
|
||||
/dev/fuse /storage/emulated fuse rw 0 0
|
||||
|
||||
@@ -21,6 +21,24 @@ class SecondaryLibraryTest {
|
||||
assertFalse(SecondaryLibrary.privateRoot(files, content).exists())
|
||||
}
|
||||
|
||||
@Test fun aCardReachedThroughItsOtherPathKeepsItsPrivateDirectories() {
|
||||
val files = tmp.newFolder("files")
|
||||
val raw = SecondaryLibrary.privateRoot(files, File("/mnt/media_rw/A042-9B16/Games")).apply { mkdirs() }
|
||||
assertEquals(raw, SecondaryLibrary.privateRoot(files, File("/storage/A042-9B16/Games")))
|
||||
val storage = SecondaryLibrary.privateRoot(files, File("/storage/B2BD-BCFC/Games")).apply { mkdirs() }
|
||||
assertEquals(storage, SecondaryLibrary.privateRoot(files, File("/mnt/media_rw/B2BD-BCFC/Games")))
|
||||
}
|
||||
|
||||
@Test fun aLibraryWithItsOwnPrivateDirectoriesKeepsThem() {
|
||||
val files = tmp.newFolder("files")
|
||||
val own = SecondaryLibrary.privateRoot(files, File("/storage/A042-9B16/Games")).apply { mkdirs() }
|
||||
val raw = File(own.parentFile, SecondaryLibrary.privateRoot(tmp.newFolder("other"), File("/mnt/media_rw/A042-9B16/Games")).name)
|
||||
raw.mkdirs()
|
||||
assertEquals(own, SecondaryLibrary.privateRoot(files, File("/storage/A042-9B16/Games")))
|
||||
assertEquals(raw, SecondaryLibrary.privateRoot(files, File("/mnt/media_rw/A042-9B16/Games")))
|
||||
assertFalse(SecondaryLibrary.privateRoot(files, File("/storage/emulated/0/Games")).exists())
|
||||
}
|
||||
|
||||
@Test fun aWritableAliasIsSelectedAndItsProbeIsRemoved() {
|
||||
val library = tmp.newFolder("shared")
|
||||
val candidate = File(tmp.root, "direct")
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.droiddeck.launcher.stores.battlenet
|
||||
|
||||
import com.droiddeck.launcher.session.SessionPrefs
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [33])
|
||||
class BattleNetLibraryTest {
|
||||
private val app get() = RuntimeEnvironment.getApplication()
|
||||
@get:Rule val tmp = TemporaryFolder()
|
||||
|
||||
@Test fun aMissingLibraryLeavesBattleNetsInstallFolderAlone() {
|
||||
SessionPrefs.setGameStorage(app, "/storage/0000-0000/Games", "SD card")
|
||||
assertTrue(BattleNetClient.library(app).isEmpty())
|
||||
}
|
||||
|
||||
@Test fun anAttachedLibraryIsHandedToBattleNet() {
|
||||
SessionPrefs.setGameStorage(app, tmp.newFolder("card").path, "SD card")
|
||||
assertEquals("--library", BattleNetClient.library(app).firstOrNull())
|
||||
}
|
||||
|
||||
@Test fun internalStorageIsHandedToBattleNetWhenItIsTheChoice() {
|
||||
SessionPrefs.setGameStorage(app, SessionPrefs.GAME_STORAGE_OFF, "")
|
||||
assertEquals("--library", BattleNetClient.library(app).firstOrNull())
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include <link.h>
|
||||
#include <linux/input.h>
|
||||
#include <pthread.h>
|
||||
#include <stdatomic.h>
|
||||
@@ -15,6 +16,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/sysmacros.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
@@ -34,7 +36,8 @@
|
||||
"NAME=\"Xbox 360 Controller (%d)\"\n" \
|
||||
"PHYS=\"usb-fakeinput/input%d\"\n"
|
||||
|
||||
#define RING_SDL_ID 0x045e028eu
|
||||
#define STEAM_VIRTUAL_VENDOR 0x28de
|
||||
#define STEAM_VIRTUAL_PRODUCT 0x11ff
|
||||
|
||||
#define PARENT_NAME "droiddeck-input"
|
||||
#define PARENT_SYSPATH_FORMAT "/sys/devices/droiddeck%d/" PARENT_NAME "%d"
|
||||
@@ -141,66 +144,8 @@ static int any_virtual_pad(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char *sdl_id_list_text(const char *list) {
|
||||
char *text = NULL;
|
||||
char *grown;
|
||||
size_t length = 0;
|
||||
size_t got;
|
||||
FILE *file;
|
||||
|
||||
if (list[0] != '@') return strdup(list);
|
||||
file = fopen(list + 1, "re");
|
||||
if (file == NULL) return NULL;
|
||||
do {
|
||||
grown = realloc(text, length + 4097);
|
||||
if (grown == NULL) {
|
||||
free(text);
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
text = grown;
|
||||
got = fread(text + length, 1, 4096, file);
|
||||
length += got;
|
||||
} while (got > 0);
|
||||
fclose(file);
|
||||
text[length] = '\0';
|
||||
return text;
|
||||
}
|
||||
|
||||
static int sdl_id_list_entries(const char *list, int *has_ring) {
|
||||
char *text = sdl_id_list_text(list);
|
||||
char *spot = text;
|
||||
uint32_t entry;
|
||||
int entries = 0;
|
||||
|
||||
*has_ring = 0;
|
||||
if (text == NULL) return 0;
|
||||
while ((spot = strstr(spot, "0x")) != NULL) {
|
||||
entry = (uint32_t)(uint16_t)strtol(spot, &spot, 0) << 16;
|
||||
spot = strstr(spot, "0x");
|
||||
if (spot == NULL) break;
|
||||
entry |= (uint16_t)strtol(spot, &spot, 0);
|
||||
entries++;
|
||||
if (entry == RING_SDL_ID) *has_ring = 1;
|
||||
}
|
||||
free(text);
|
||||
return entries;
|
||||
}
|
||||
|
||||
static int sdl_bus_skips_ring_pad(void) {
|
||||
const char *steam_virtual = getenv("FAKE_EVDEV_STEAM_VIRTUAL");
|
||||
const char *except = getenv("SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT");
|
||||
const char *ignore = getenv("SDL_GAMECONTROLLER_IGNORE_DEVICES");
|
||||
int has_ring;
|
||||
|
||||
if (steam_virtual != NULL && atoi(steam_virtual) != 0) return 1;
|
||||
if (except != NULL && sdl_id_list_entries(except, &has_ring) > 0) return !has_ring;
|
||||
return ignore != NULL && sdl_id_list_entries(ignore, &has_ring) > 0 && has_ring;
|
||||
}
|
||||
|
||||
static const char *ring_uevent_format(void) {
|
||||
if (uinput_enabled() && any_virtual_pad()) return PAD_PHYSICAL_UEVENT_FORMAT;
|
||||
return sdl_bus_skips_ring_pad() ? PAD_UEVENT_FORMAT : PAD_PHYSICAL_UEVENT_FORMAT;
|
||||
return uinput_enabled() && any_virtual_pad() ? PAD_PHYSICAL_UEVENT_FORMAT : PAD_UEVENT_FORMAT;
|
||||
}
|
||||
|
||||
static void refresh_locked(int node) {
|
||||
@@ -514,6 +459,60 @@ static int caller_in_library(void *caller, const char *library) {
|
||||
|
||||
static int caller_is_winebus(void *caller) { return caller_in_library(caller, "winebus.so"); }
|
||||
|
||||
static int note_winebus(struct dl_phdr_info *info, size_t size, void *data) {
|
||||
(void)size;
|
||||
if (info->dlpi_name == NULL || strstr(info->dlpi_name, "winebus.so") == NULL) return 0;
|
||||
*(int *)data = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int winebus_reads_udev(void) {
|
||||
const char *x360 = getenv("FAKE_EVDEV_STEAM_PAD_AS_X360");
|
||||
int loaded = 0;
|
||||
|
||||
if (x360 != NULL && atoi(x360) != 0) return 0;
|
||||
dl_iterate_phdr(note_winebus, &loaded);
|
||||
return loaded;
|
||||
}
|
||||
|
||||
static int ring_pad_fd(int fd) {
|
||||
const char *spec = getenv("FAKE_EVDEV_MEMFD_PATHS");
|
||||
const char *entry;
|
||||
struct stat opened;
|
||||
struct stat ring;
|
||||
char path[PATH_MAX];
|
||||
|
||||
if (spec == NULL || fstat(fd, &opened) != 0) return 0;
|
||||
for (entry = spec; *entry != '\0';) {
|
||||
char *end;
|
||||
long slot = strtol(entry, &end, 10);
|
||||
const char *next = strchr(entry, ';');
|
||||
size_t length;
|
||||
|
||||
if (end != entry && *end == '=' && slot >= 0 && slot < RING_MAX) {
|
||||
length = next != NULL ? (size_t)(next - end - 1) : strlen(end + 1);
|
||||
if (length < sizeof(path)) {
|
||||
memcpy(path, end + 1, length);
|
||||
path[length] = '\0';
|
||||
if (stat(path, &ring) == 0 && ring.st_dev == opened.st_dev && ring.st_ino == opened.st_ino) return 1;
|
||||
}
|
||||
}
|
||||
if (next == NULL) break;
|
||||
entry = next + 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
__attribute__((visibility("hidden"))) void dd_ring_pad_sdl_id(int fd, unsigned long request, void *arg,
|
||||
void *caller, int rc) {
|
||||
struct input_id *id = arg;
|
||||
|
||||
if (rc != 0 || request != EVIOCGID || id == NULL) return;
|
||||
if (!caller_in_library(caller, "libSDL") || !winebus_reads_udev() || !ring_pad_fd(fd)) return;
|
||||
id->vendor = STEAM_VIRTUAL_VENDOR;
|
||||
id->product = STEAM_VIRTUAL_PRODUCT;
|
||||
}
|
||||
|
||||
typedef int (*sdl_rumble_fn)(void *, uint16_t, uint16_t, uint32_t);
|
||||
static _Atomic(sdl_rumble_fn) real_sdl_rumble;
|
||||
static __thread int winebus_sdl_call;
|
||||
|
||||
@@ -424,6 +424,8 @@ int dd_venc_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute
|
||||
int dd_vdec_ioctl(int fd, unsigned long request, void *arg, int *rc) __attribute__((visibility("hidden")));
|
||||
int dd_winebus_rumble_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request,
|
||||
void *arg, void *caller, int *rc) __attribute__((visibility("hidden")));
|
||||
void dd_ring_pad_sdl_id(int fd, unsigned long request, void *arg, void *caller, int rc)
|
||||
__attribute__((visibility("hidden")));
|
||||
int dd_writewatch_ioctl(int (*real)(int, unsigned long, void *), int fd, unsigned long request, void *arg,
|
||||
int *rc) __attribute__((visibility("hidden")));
|
||||
|
||||
@@ -444,6 +446,7 @@ int ioctl(int fd, unsigned long request, ...) {
|
||||
if (dd_winebus_rumble_ioctl(real, fd, request, arg, __builtin_return_address(0), &rc)) return rc;
|
||||
if (dd_writewatch_ioctl(real, fd, request, arg, &rc)) return rc;
|
||||
if (!dd_kgsl_poll(real, fd, request, arg, &rc)) rc = quest_kgsl_ioctl(real, fd, request, arg);
|
||||
dd_ring_pad_sdl_id(fd, request, arg, __builtin_return_address(0), rc);
|
||||
dd_kgsl_track(real, fd, request, arg, &rc);
|
||||
dd_kgsl_chip_id_fixup(request, arg, rc);
|
||||
if (arg == NULL || !is_link_query(request)) return rc;
|
||||
|
||||
@@ -707,36 +707,15 @@ class InputUdevPreloadTest(unittest.TestCase):
|
||||
def identity(self, pads=None, **extra):
|
||||
return self.run_program("identity", pads=pads, extra=extra)
|
||||
|
||||
@classmethod
|
||||
def id_list(cls, name, text):
|
||||
path = cls.dir / name
|
||||
path.write_text(text)
|
||||
return "@" + str(path)
|
||||
|
||||
def test_a_pad_wine_also_reads_through_sdl_is_described_as_the_pad_sdl_sees(self):
|
||||
xbox = ["pad PRODUCT=3/45e/28e/110", "parent PRODUCT=3/45e/28e/110"]
|
||||
for extra in ({}, {"FAKE_EVDEV_UINPUT": "1"}, {"SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x28de/0x1142"},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": "0x045e/0x028e"}, {"FAKE_EVDEV_STEAM_VIRTUAL": "0"},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES": self.id_list("ignore-other.txt", "0x28de/0x1142\n")},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES": "@" + str(self.dir / "missing.txt")},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": self.id_list("except-ring.txt", "0x045E/0x028E\n")},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": "0x45e/0x28e"},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": ""},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": "", "SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x28de/0x1142"},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x045e/0x028f"}):
|
||||
with self.subTest(**extra):
|
||||
self.assertEqual(self.identity(**extra), xbox)
|
||||
|
||||
def test_a_pad_sdl_skips_keeps_the_steam_input_identity(self):
|
||||
def test_a_ring_pad_keeps_the_steam_input_identity_whatever_sdl_is_told(self):
|
||||
steam = ["pad PRODUCT=6/28de/11ff/110", "parent PRODUCT=6/28de/11ff/110"]
|
||||
for extra in ({"FAKE_EVDEV_STEAM_VIRTUAL": "1"}, {"SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x28de/0x1142,0x045E/0x028E"},
|
||||
for extra in ({}, {"FAKE_EVDEV_UINPUT": "1"}, {"FAKE_EVDEV_STEAM_VIRTUAL": "0"}, {"FAKE_EVDEV_STEAM_VIRTUAL": "1"},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x28de/0x1142"},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x28de/0x1142,0x045E/0x028E"},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": "0x045e/0x028e"},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": "0x28de/0x11ff"},
|
||||
{"FAKE_EVDEV_UINPUT": "1", "SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x045e/0x028e"},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES": self.id_list("ignore-ring.txt", "0x28de/0x1142\n0x045e/0x028e\n")},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": self.id_list("except-other.txt", "0x28de/0x11ff\n")},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES": self.id_list("ignore-long.txt", "0x28de/0x1142\n" * 400 + "0x045e/0x028e\n")},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x45e/0x28e"},
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": "", "SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x045e/0x028e"}):
|
||||
{"SDL_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT": ""},
|
||||
{"FAKE_EVDEV_UINPUT": "1", "SDL_GAMECONTROLLER_IGNORE_DEVICES": "0x045e/0x028e"}):
|
||||
with self.subTest(**extra):
|
||||
self.assertEqual(self.identity(**extra), steam)
|
||||
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
from pathlib import Path
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
|
||||
|
||||
SHIM = r"""
|
||||
#include <linux/input.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
int ioctl(int fd, unsigned long request, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, request);
|
||||
struct input_id *id = va_arg(ap, struct input_id *);
|
||||
va_end(ap);
|
||||
(void)fd;
|
||||
if (request != EVIOCGID) return -1;
|
||||
memset(id, 0, sizeof(*id));
|
||||
id->bustype = 3;
|
||||
id->vendor = 0x045e;
|
||||
id->product = 0x028e;
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
QUERY = r"""
|
||||
#include <fcntl.h>
|
||||
#include <linux/input.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void query(const char *path) {
|
||||
struct input_id id;
|
||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
int rc = ioctl(fd, EVIOCGID, &id);
|
||||
printf("%d %04x:%04x\n", rc, id.vendor, id.product);
|
||||
close(fd);
|
||||
}
|
||||
"""
|
||||
|
||||
PROGRAM = r"""
|
||||
#include <dlfcn.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
void query(const char *path);
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
(void)argc;
|
||||
if (strcmp(argv[1], "-") != 0) dlopen(argv[1], RTLD_NOW);
|
||||
if (strcmp(argv[2], "program") == 0) {
|
||||
query(argv[3]);
|
||||
} else {
|
||||
void *sdl = dlopen(argv[2], RTLD_NOW | RTLD_LOCAL);
|
||||
void (*sdl_query)(const char *) = (void (*)(const char *))dlsym(sdl, "query");
|
||||
sdl_query(argv[3]);
|
||||
}
|
||||
fflush(stdout);
|
||||
return 0;
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
@unittest.skipUnless(sys.platform == "linux" and shutil.which("cc"), "needs Linux and cc")
|
||||
class RingPadSdlIdentityTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.dir = Path(tempfile.mkdtemp())
|
||||
build = cls.dir / "build"
|
||||
build.mkdir()
|
||||
for name, text in (("shim.c", SHIM), ("query.c", QUERY), ("program.c", PROGRAM), ("empty.c", "int bus;\n")):
|
||||
(build / name).write_text(text)
|
||||
cls.lib = cls.dir / "libddsession.so"
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-pthread", "-o", str(cls.lib),
|
||||
*map(str, sorted(PRELOAD.glob("*.c"))), "-ldl"], check=True)
|
||||
cls.shim = cls.dir / "libshim.so"
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.shim), str(build / "shim.c")], check=True)
|
||||
cls.winebus = cls.dir / "winebus.so"
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.winebus), str(build / "empty.c")], check=True)
|
||||
cls.sdl_library = cls.dir / "libSDL2-2.0.so.0"
|
||||
subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-o", str(cls.sdl_library), str(build / "query.c")], check=True)
|
||||
cls.program = cls.dir / "program"
|
||||
subprocess.run(["cc", "-O2", "-o", str(cls.program), str(build / "program.c"), str(build / "query.c"), "-ldl"],
|
||||
check=True)
|
||||
cls.ring = cls.dir / "ring0"
|
||||
cls.ring.write_text("")
|
||||
cls.virtual = cls.dir / "ring16"
|
||||
cls.virtual.write_text("")
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
shutil.rmtree(cls.dir, True)
|
||||
|
||||
def query(self, winebus=True, caller="sdl", path=None, **extra):
|
||||
env = dict(os.environ, LD_PRELOAD="%s:%s" % (self.lib, self.shim),
|
||||
FAKE_EVDEV_MEMFD_PATHS="0=%s;16=%s" % (self.ring, self.virtual))
|
||||
env.pop("FAKE_EVDEV_STEAM_PAD_AS_X360", None)
|
||||
env.update(extra)
|
||||
result = subprocess.run([str(self.program), str(self.winebus) if winebus else "-",
|
||||
str(self.sdl_library) if caller == "sdl" else "program", str(path or self.ring)],
|
||||
env=env, capture_output=True, text=True, timeout=10, check=True)
|
||||
return result.stdout.strip()
|
||||
|
||||
def test_sdl_in_a_winebus_process_sees_a_ring_pad_as_the_steam_input_pad(self):
|
||||
self.assertEqual(self.query(), "0 28de:11ff")
|
||||
|
||||
def test_sdl_outside_winebus_keeps_the_pad_identity(self):
|
||||
self.assertEqual(self.query(winebus=False), "0 045e:028e")
|
||||
|
||||
def test_other_callers_and_devices_keep_their_identity(self):
|
||||
self.assertEqual(self.query(caller="program"), "0 045e:028e")
|
||||
self.assertEqual(self.query(path=self.virtual), "0 045e:028e")
|
||||
|
||||
def test_a_winebus_without_a_udev_bus_keeps_the_pad_for_sdl(self):
|
||||
self.assertEqual(self.query(FAKE_EVDEV_STEAM_PAD_AS_X360="1"), "0 045e:028e")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user