Files
mediadeck/tests/uidriver.py
T
Denis 851b7681ff Add held mouse buttons (Left click / Right click toggles) to the Touchpad tab
Two toggle buttons in the free corners of the D-pad press and hold the PC's
left or right mouse button until tapped again, for drag and drop, text
selection and window moves, with the touchpad moving the pointer in between.

- New input action "button" ({button, pressed}); the state carries
  input.buttons. VirtualInput remembers which connection pressed each button.
- A click on a held button is ignored so a touchpad tap does not drop the
  drag; the other button stays usable. Repeating a state is a no-op.
- A held button is always released: when its client disconnects (only its own
  buttons), when the server stops, and in the client when the tab is left or
  the page is hidden.
- Incoming states are set aside for 500 ms after a local tap, so a state
  already in flight cannot undo the toggle for a moment.
- D-pad rows are equal-sized (minmax(0, 1fr)) so the corner buttons do not
  stretch the top row.

tests/test_input.py covers the device logic and the full server (disconnect,
several clients); tests/test_ui_mouse_hold.py drives a real Chromium. Also
checked through the real uinput device with its pointer grabbed exclusively.
README (fr/en) and CLAUDE.md are updated.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-31 09:02:19 +01:00

178 lines
8.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Pilote minimal de Chromium (protocole DevTools) et faux serveur de la vraie interface, pour les tests de mise en page."""
import asyncio
import json
import os
import shutil
import signal
import socket
import subprocess
import tempfile
from typing import Any
import aiohttp
from aiohttp import WSMsgType, web
from mediadeck.server import render_page, versioned_static
CHROMIUM = shutil.which("chromium") or shutil.which("chromium-browser") or shutil.which("google-chrome")
PLAYER: dict[str, Any] = {
"name": "org.mpris.MediaPlayer2.test", "identity": "Lecteur de test", "can_raise": True, "status": "Playing",
"metadata": {"title": "Concerto pour piano n°21 en do majeur, K. 467 – II. Andante", "artist": "Wolfgang Amadeus Mozart",
"album": "Les plus grands concertos", "length": 412_000_000, "art": None},
"volume": 0.8, "shuffle": False, "loop": "None", "can_play": True, "can_pause": True, "can_seek": True,
"can_next": True, "can_previous": True, "position": 131_000_000,
}
SINKS = [{"name": "hdmi", "description": "Sortie HDMI / DisplayPort", "volume": 0.62, "muted": False}]
LAST = {"name": "Un très long nom de fichier vidéo qui dépasse largement une ligne - 1080p", "type": "video", "art": None}
def state(scenario: str) -> dict[str, Any]:
players = [] if scenario == "last" else [PLAYER]
media = {"roots": [{"id": "videos", "name": "Videos"}], "thumbnails": False, "last": LAST if scenario == "last" else None}
return {
"type": "state", "version": "test",
"mpris": {"players": players, "active": PLAYER["name"] if players else None, "available": True},
"audio": {"available": True, "default": "hdmi", "max_volume": 1.0, "sinks": SINKS},
"input": {"available": True, "reason": None, "keys": [],
"text": {"available": False, "reason": "disabled in config (features.text_input)"}},
"system": {"power": True, "admin": True, "launchers": [], "media": media},
}
async def _websocket(request: web.Request) -> web.WebSocketResponse:
socket_ = web.WebSocketResponse()
await socket_.prepare(request)
# La page ne transmet que le jeton : il porte ici le nom du scénario (« player » ou « last »)
await socket_.send_json(state(request.query.get("token", "player")))
async for message in socket_:
if message.type == WSMsgType.ERROR:
break
# Un test peut enregistrer les messages du client et répondre (voir make_app)
handler = request.app.get("on_message")
if message.type == WSMsgType.TEXT and handler is not None:
await handler(json.loads(message.data), socket_)
return socket_
async def _index(_request: web.Request) -> web.Response:
return render_page("index.html")
def make_app(on_message: Any = None) -> web.Application:
"""La vraie page et les vrais fichiers statiques (mêmes fonctions que le serveur), avec un état de démonstration.
``on_message(message, socket)`` : appelé pour chaque message reçu du client (coroutine)."""
app = web.Application()
if on_message is not None:
app["on_message"] = on_message
app.router.add_get("/", _index)
app.router.add_get("/ws", _websocket)
app.router.add_get("/static/{version}/{path:.+}", versioned_static)
return app
def free_port() -> int:
with socket.socket() as sock:
sock.bind(("127.0.0.1", 0))
return sock.getsockname()[1]
class Browser:
"""Chromium sans interface, piloté par DevTools."""
def __init__(self) -> None:
assert CHROMIUM
self._profile = tempfile.TemporaryDirectory(ignore_cleanup_errors=True)
self._port = free_port()
self._process: subprocess.Popen[bytes] | None = None
self._session: aiohttp.ClientSession | None = None
self._socket: aiohttp.ClientWebSocketResponse | None = None
self._next_id = 0
self.exceptions: list[str] = [] # exceptions JavaScript non rattrapées, signalées par la page
async def start(self) -> None:
assert CHROMIUM
self._process = subprocess.Popen(
[CHROMIUM, "--headless=new", "--no-sandbox", "--disable-gpu", "--hide-scrollbars", "--password-store=basic",
f"--remote-debugging-port={self._port}", f"--user-data-dir={self._profile.name}", "about:blank"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
# Aucun bus D-Bus : sinon Chromium hériterait du bus de test (ou de celui de l'utilisateur) et y ferait
# activer des services réels (portails, kwallet, kdeconnect…) qui survivent au test
env={**os.environ, "DBUS_SESSION_BUS_ADDRESS": "disabled:"},
start_new_session=True, # groupe à part : close() termine aussi les processus enfants de Chromium
)
self._session = aiohttp.ClientSession()
for _ in range(100):
try:
async with self._session.get(f"http://127.0.0.1:{self._port}/json") as response:
pages = [page for page in await response.json() if page["type"] == "page"]
if pages:
self._socket = await self._session.ws_connect(pages[0]["webSocketDebuggerUrl"], max_msg_size=0)
await self.call("Runtime.enable") # active Runtime.exceptionThrown
return
except aiohttp.ClientError:
pass
await asyncio.sleep(0.1)
raise RuntimeError("Chromium did not start")
def signal_group(self, number: signal.Signals) -> None:
assert self._process
try:
os.killpg(self._process.pid, number)
except ProcessLookupError:
pass
async def close(self) -> None:
if self._socket:
await self._socket.close()
if self._session:
await self._session.close()
if self._process:
# Les processus enfants de Chromium écrivent encore dans le profil : on termine tout le groupe avant de le supprimer
self.signal_group(signal.SIGTERM)
try:
self._process.wait(timeout=5)
except subprocess.TimeoutExpired:
pass
self.signal_group(signal.SIGKILL) # enfants restants
self._process.wait(timeout=10)
self._profile.cleanup()
async def call(self, method: str, **params: Any) -> dict[str, Any]:
assert self._socket
self._next_id += 1
await self._socket.send_json({"id": self._next_id, "method": method, "params": params})
while True:
message = json.loads((await self._socket.receive()).data)
if message.get("method") == "Runtime.exceptionThrown":
details = message["params"]["exceptionDetails"]
self.exceptions.append(details.get("exception", {}).get("description") or details.get("text", "?"))
if message.get("id") == self._next_id:
if "error" in message:
raise RuntimeError(message["error"])
return message.get("result", {})
async def open(self, url: str, width: int, height: int) -> None:
await self.call("Emulation.setDeviceMetricsOverride", width=width, height=height, deviceScaleFactor=1, mobile=height > width)
await self.call("Page.navigate", url=url)
async def evaluate(self, expression: str) -> Any:
result = await self.call("Runtime.evaluate", expression=expression, returnByValue=True)
if "exceptionDetails" in result:
raise RuntimeError(result["exceptionDetails"])
return result["result"].get("value")
async def wait_for(self, expression: str, timeout: float = 8.0) -> None:
loop = asyncio.get_running_loop()
deadline = loop.time() + timeout
while loop.time() < deadline:
try:
if await self.evaluate(expression):
return
except RuntimeError:
pass # page pas encore chargée (élément absent) : on réessaie jusqu'au délai
await asyncio.sleep(0.1)
raise TimeoutError(expression)