#!/usr/bin/python3 """The guest side of the agent bridge (docs/agent-control.md). Started by droiddeck-session inside the session, so whatever it runs has the session's own environment: DISPLAY, the games' Xwayland, XDG_RUNTIME_DIR, and the preloads every guest process gets - which is what lets it reach the Steam client's DevTools port, open to guest processes only. The app talks to it through files under $BL_LAUNCH_DIR/agent (the session root, the same path on both sides): req/.json a request the app wrote (tmp + rename) resp/.json the answer, written the same way hello.json this daemon is up: pid, version, what it can do focus.json gamescope's focus and every server's top-level windows, rewritten on change cdp-port the client's DevTools port, written by droiddeck-session Requests: ping, exec, focus, cdp, quit. Each answer is {"ok": true, ...} or {"ok": false, "error": {"code", "message"}}. """ import base64 import json import math import re import os import selectors import signal import socket import struct import subprocess import sys import threading import time import urllib.request import urllib.parse from pathlib import Path VERSION = 1 OUTPUT_CAP = 256 * 1024 REQUEST_CAP = 256 * 1024 RESPONSE_CAP = 1024 * 1024 FOCUS_PROPS = ( "GAMESCOPE_FOCUSED_APP", "GAMESCOPE_FOCUSED_WINDOW", "GAMESCOPE_FOCUSABLE_APPS", "GAMESCOPE_FOCUSABLE_WINDOWS", "GAMESCOPECTRL_BASELAYER_APPID", ) WINDOW_PROPS = ( "STEAM_GAME", "WM_STATE", "_NET_WM_PID", "WM_CLASS", "_NET_WM_WINDOW_OPACITY", "STEAM_OVERLAY", "GAMESCOPE_EXTERNAL_OVERLAY", "STEAM_INPUT_FOCUS", "_NET_WM_STATE", ) MAP_STATE = ("unmapped", "unviewable", "viewable") WM_STATES = {0: "withdrawn", 1: "normal", 3: "iconic"} def log(message): print("== agent: " + message, flush=True) def write_json(path, value): tmp = path.with_name(path.name + ".tmp") with os.fdopen(os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC | os.O_NOFOLLOW, 0o600), "w") as stream: os.fchmod(stream.fileno(), 0o600) json.dump(value, stream, separators=(",", ":")) os.replace(tmp, path) class AgentError(Exception): def __init__(self, code, message): super().__init__(message) self.code = code class X: """One connection to an Xwayland server. The rootfs has xprop but no python X library.""" def __init__(self, display): num = display.lstrip(":").split(".")[0] last = None for address in ("\0/tmp/.X11-unix/X" + num, "/tmp/.X11-unix/X" + num): try: self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.s.settimeout(3) self.s.connect(address) break except OSError as e: last = e self.s.close() else: raise last self.s.sendall(struct.pack(" 1: return head def atom(self, name): if name not in self._atoms: n = name.encode() pad = (4 - len(n) % 4) % 4 r = self.request(struct.pack("