Fix up some rough edges with Steam under Orca's interface.
There is a setting in the extension to ignore processes that have antitamper mechanisms; as well as supporting things that run directly from Steam.
This commit is contained in:
1 parent
e920543dcb
commit
4aefc3ef79
5 files changed
+296
-23
No files matched your search
+134
-19
@@ -9,7 +9,7 @@ restart with exponential backoff.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, field
|
||||
import hashlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
@@ -25,13 +25,27 @@ MAX_BACKOFF_SECONDS = 60.0
|
||||
STABLE_RUN_SECONDS = 10.0
|
||||
LOG_LIMIT_BYTES = 256 * 1024
|
||||
HELPER_PROGRAM = "wine-access.exe.so"
|
||||
# The files that must stay beside the helper launcher; see the staging in
|
||||
# HelperManager._helper_for.
|
||||
HELPER_SIBLINGS = ("wine-access-menu.dll", "wine-access-menu32.dll",
|
||||
"wine-access-menu-host32.exe")
|
||||
LOADER_NAMES = {"wine", "wine64"}
|
||||
PRELOADER_NAMES = {"wine-preloader", "wine64-preloader"}
|
||||
# Wine's own background processes outlive applications while anything in the
|
||||
# prefix runs, including the helper; they must not keep a prefix active.
|
||||
# xalia.exe is Proton's gamepad overlay, in the same position: it would
|
||||
# otherwise keep the prefix "active" for as long as the helper it keeps
|
||||
# alive holds the wineserver up, so the helper would never stop.
|
||||
WINE_SERVICES = {"services.exe", "explorer.exe", "winedevice.exe", "plugplay.exe",
|
||||
"svchost.exe", "rpcss.exe", "conhost.exe", "tabtip.exe",
|
||||
"wineboot.exe", "winemenubuilder.exe", "start.exe", "wineserver"}
|
||||
"wineboot.exe", "winemenubuilder.exe", "start.exe", "wineserver",
|
||||
"xalia.exe"}
|
||||
# Proton launches games through its builtin steam.exe shim, which lingers
|
||||
# the same way. Only the shim's system32/syswow64 home qualifies: a real
|
||||
# Steam client installed in a prefix runs from its own directory and is
|
||||
# an application like any other.
|
||||
WINE_SERVICE_PATHS = ("windows/system32/steam.exe",
|
||||
"windows/syswow64/steam.exe")
|
||||
FORWARDED_ENVIRONMENT = ("DISPLAY", "WAYLAND_DISPLAY", "XAUTHORITY",
|
||||
"XDG_RUNTIME_DIR", "DBUS_SESSION_BUS_ADDRESS",
|
||||
# A client joining a running server (Proton enables
|
||||
@@ -59,6 +73,37 @@ def helper_candidates() -> list[str]:
|
||||
return [path for path in candidates if path]
|
||||
|
||||
|
||||
def extra_helper_arguments(environment: dict[str, str] = os.environ) -> list[str]:
|
||||
"""Extra helper flags from the reader's environment.
|
||||
|
||||
WINE_ACCESS_NO_DIALOG_READER=1 runs helpers controller-only: no
|
||||
WinEvent hooks, no native hook DLL in applications, no MSAA polling.
|
||||
That is the mode for games whose anti-tamper code objects to injected
|
||||
hooks; their NVDA/Tolk speech still works, standard controls don't.
|
||||
"""
|
||||
|
||||
if environment.get("WINE_ACCESS_NO_DIALOG_READER", "") not in ("", "0"):
|
||||
return ["--no-dialog-reader"]
|
||||
return []
|
||||
|
||||
|
||||
def controller_only_arguments(programs: set[str], listed,
|
||||
always: bool = False) -> list[str]:
|
||||
"""--no-dialog-reader if controller-only applies to these programs.
|
||||
|
||||
Matching is by lower-case executable file name, as reported by
|
||||
discover(); entries in the user's list may use any case. Settings
|
||||
layers hand string arrays back as lists or tuples depending on the
|
||||
path taken, so any sequence of strings is accepted.
|
||||
"""
|
||||
|
||||
names = {name.lower() for name in listed
|
||||
if isinstance(name, str)} if isinstance(listed, (list, tuple)) else set()
|
||||
if always or programs & names:
|
||||
return ["--no-dialog-reader"]
|
||||
return []
|
||||
|
||||
|
||||
def find_helper() -> str | None:
|
||||
for path in helper_candidates():
|
||||
if os.access(path, os.X_OK):
|
||||
@@ -79,14 +124,28 @@ def read_environment(pid: int, proc: Path = Path("/proc")) -> dict[str, str]:
|
||||
return result
|
||||
|
||||
|
||||
def program_name(pid: int, proc: Path = Path("/proc")) -> str:
|
||||
"""Lower-case file name of the program a process runs, Windows or Linux."""
|
||||
def program_path(pid: int, proc: Path = Path("/proc")) -> str:
|
||||
"""Lower-case argv0 of a process, with Windows separators normalized."""
|
||||
|
||||
try:
|
||||
argv0 = (proc / str(pid) / "cmdline").read_bytes().split(b"\0", 1)[0]
|
||||
except OSError:
|
||||
return ""
|
||||
return argv0.decode(errors="replace").replace("\\", "/").rsplit("/", 1)[-1].lower()
|
||||
return argv0.decode(errors="replace").replace("\\", "/").lower()
|
||||
|
||||
|
||||
def program_name(pid: int, proc: Path = Path("/proc")) -> str:
|
||||
"""Lower-case file name of the program a process runs, Windows or Linux."""
|
||||
|
||||
return program_path(pid, proc).rsplit("/", 1)[-1]
|
||||
|
||||
|
||||
def is_wine_service(pid: int, proc: Path = Path("/proc")) -> bool:
|
||||
"""Whether a process is Wine/Proton infrastructure, not an application."""
|
||||
|
||||
path = program_path(pid, proc)
|
||||
return (path.rsplit("/", 1)[-1] in WINE_SERVICES
|
||||
or path.endswith(WINE_SERVICE_PATHS))
|
||||
|
||||
|
||||
def prefix_for(environment: dict[str, str]) -> str:
|
||||
@@ -208,6 +267,7 @@ class PrefixState:
|
||||
started_at: float = 0.0
|
||||
failures: int = 0
|
||||
next_start: float = 0.0
|
||||
extra_arguments: list[str] = field(default_factory=list)
|
||||
|
||||
|
||||
class HelperManager:
|
||||
@@ -221,9 +281,13 @@ class HelperManager:
|
||||
log_directory: Path | None = None,
|
||||
clock: Callable[[], float] = time.monotonic,
|
||||
spawn: Callable[..., subprocess.Popen] = subprocess.Popen,
|
||||
arguments_for: Callable[[set[str]], list[str]] | None = None,
|
||||
) -> None:
|
||||
self.helper = helper if helper is not None else find_helper()
|
||||
self.reader_arguments = reader_arguments
|
||||
# Extra helper arguments for a prefix's program names, consulted
|
||||
# at each spawn so settings changes need no reader restart.
|
||||
self.arguments_for = arguments_for or (lambda programs: [])
|
||||
self.proc = proc
|
||||
self.log_directory = log_directory or Path(
|
||||
os.environ.get("XDG_STATE_HOME") or os.path.expanduser("~/.local/state")
|
||||
@@ -235,10 +299,10 @@ class HelperManager:
|
||||
# running by an Orca that was restarted.
|
||||
self.stale: dict[str, list[int]] = {}
|
||||
|
||||
def discover(self) -> dict[str, tuple[dict[str, str], str]]:
|
||||
"""Active prefixes with the environment and loader of one process each."""
|
||||
def discover(self) -> dict[str, tuple[dict[str, str], str, set[str]]]:
|
||||
"""Active prefixes: one process's environment/loader, all program names."""
|
||||
|
||||
found: dict[str, tuple[dict[str, str], str]] = {}
|
||||
found: dict[str, tuple[dict[str, str], str, set[str]]] = {}
|
||||
self.stale = {}
|
||||
uid = os.getuid()
|
||||
helpers = {state.process.pid for state in self.prefixes.values()
|
||||
@@ -264,9 +328,13 @@ class HelperManager:
|
||||
self.stale.setdefault(prefix_for(environment), []).append(pid)
|
||||
continue
|
||||
loader = loader_for(pid, environment, self.proc)
|
||||
if loader is None or program_name(pid, self.proc) in WINE_SERVICES:
|
||||
if loader is None or is_wine_service(pid, self.proc):
|
||||
continue
|
||||
found.setdefault(prefix_for(environment), (environment, loader))
|
||||
prefix = prefix_for(environment)
|
||||
if prefix in found:
|
||||
found[prefix][2].add(program_name(pid, self.proc))
|
||||
else:
|
||||
found[prefix] = (environment, loader, {program_name(pid, self.proc)})
|
||||
return found
|
||||
|
||||
def poll(self) -> bool:
|
||||
@@ -276,10 +344,10 @@ class HelperManager:
|
||||
return True
|
||||
now = self.clock()
|
||||
active = self.discover()
|
||||
for prefix, (environment, loader) in active.items():
|
||||
for prefix, (environment, loader, programs) in active.items():
|
||||
state = self.prefixes.setdefault(prefix, PrefixState())
|
||||
state.last_seen = now
|
||||
self._ensure_running(prefix, state, environment, loader, now)
|
||||
self._ensure_running(prefix, state, environment, loader, now, programs)
|
||||
for prefix, state in list(self.prefixes.items()):
|
||||
self._record_exit(state, now)
|
||||
if prefix not in active and now - state.last_seen >= IDLE_GRACE_SECONDS:
|
||||
@@ -287,12 +355,57 @@ class HelperManager:
|
||||
del self.prefixes[prefix]
|
||||
return True
|
||||
|
||||
def _command(self) -> list[str]:
|
||||
return [self.helper, *self.reader_arguments]
|
||||
def _helper_for(self, source: dict[str, str]) -> str:
|
||||
"""The helper launcher path this prefix's wineserver can open.
|
||||
|
||||
A wineserver started inside pressure-vessel resolves unix paths
|
||||
in the container, whose /usr is the Steam Runtime, so a
|
||||
system-installed helper is invisible to it (the spawn fails with
|
||||
"ShellExecuteEx failed: File not found"). The home directory is
|
||||
bound at the same path in both namespaces, so for such prefixes
|
||||
stage a copy of the helper set under the state directory.
|
||||
"""
|
||||
|
||||
if "PRESSURE_VESSEL_RUNTIME" not in source:
|
||||
return self.helper
|
||||
origin = Path(self.helper)
|
||||
stage = self.log_directory / "helper"
|
||||
try:
|
||||
stage.mkdir(parents=True, exist_ok=True)
|
||||
names = [origin.name, origin.name + ".so", *HELPER_SIBLINGS]
|
||||
for name in names:
|
||||
source_file = origin.parent / name
|
||||
target = stage / name
|
||||
if not source_file.is_file():
|
||||
continue
|
||||
if target.is_file() and target.read_bytes() == source_file.read_bytes():
|
||||
continue
|
||||
shutil.copy2(source_file, target)
|
||||
except OSError:
|
||||
return self.helper
|
||||
staged = stage / origin.name
|
||||
return str(staged) if staged.is_file() else self.helper
|
||||
|
||||
def _command(self, source: dict[str, str], programs: set[str]) -> list[str]:
|
||||
return [self._helper_for(source), *self.reader_arguments,
|
||||
*self.arguments_for(programs)]
|
||||
|
||||
def _ensure_running(self, prefix: str, state: PrefixState,
|
||||
source: dict[str, str], loader: str, now: float) -> None:
|
||||
source: dict[str, str], loader: str, now: float,
|
||||
programs: set[str]) -> None:
|
||||
self._record_exit(state, now)
|
||||
if state.process is not None:
|
||||
# A helper can start one pass before the program it serves is
|
||||
# discovered (a loader process shows up first). If that
|
||||
# program turns out to be controller-only, restart the helper
|
||||
# in that mode; never downgrade a running controller-only
|
||||
# helper the same way, or modes would flap with the process
|
||||
# list.
|
||||
if ("--no-dialog-reader" in self.arguments_for(programs)
|
||||
and "--no-dialog-reader" not in state.extra_arguments):
|
||||
self._stop(state)
|
||||
else:
|
||||
return
|
||||
if state.process is not None or now < state.next_start:
|
||||
return
|
||||
stale = self.stale.get(prefix, [])
|
||||
@@ -318,21 +431,22 @@ class HelperManager:
|
||||
environment[key] = source[key]
|
||||
environment.update(WINEPREFIX=prefix, WINELOADER=loader, WINEDEBUG="-all")
|
||||
environment[MANAGED_KEY] = "1"
|
||||
log = self._log_file(prefix)
|
||||
log = self._log_file(prefix, programs)
|
||||
try:
|
||||
state.process = self.spawn(
|
||||
self._command(), env=environment, stdin=subprocess.DEVNULL,
|
||||
self._command(source, programs), env=environment, stdin=subprocess.DEVNULL,
|
||||
stdout=log or subprocess.DEVNULL, stderr=log or subprocess.DEVNULL,
|
||||
start_new_session=True,
|
||||
)
|
||||
state.started_at = now
|
||||
state.extra_arguments = self.arguments_for(programs)
|
||||
except OSError:
|
||||
self._back_off(state, now)
|
||||
finally:
|
||||
if log is not None:
|
||||
log.close()
|
||||
|
||||
def _log_file(self, prefix: str):
|
||||
def _log_file(self, prefix: str, programs: set[str]):
|
||||
try:
|
||||
self.log_directory.mkdir(parents=True, exist_ok=True)
|
||||
name = hashlib.sha256(prefix.encode()).hexdigest()[:12]
|
||||
@@ -340,7 +454,8 @@ class HelperManager:
|
||||
# Keep earlier starts for diagnosis, within a bounded size.
|
||||
mode = "a" if path.exists() and path.stat().st_size < LOG_LIMIT_BYTES else "w"
|
||||
log = open(path, mode, encoding="utf-8")
|
||||
log.write(f"--- {time.strftime('%Y-%m-%d %H:%M:%S')} starting helper for {prefix}\n")
|
||||
log.write(f"--- {time.strftime('%Y-%m-%d %H:%M:%S')} starting helper for {prefix} "
|
||||
f"programs={sorted(programs)} extra={self.arguments_for(programs)}\n")
|
||||
log.flush()
|
||||
return log
|
||||
except OSError:
|
||||
|
||||
Reference in new issue
Block a user