--- scripts/watcher.py +++ scripts/watcher.py @@ -51,6 +51,8 @@ LOGS_DIR = SYSTEM_DIR / "logs" RECALL_MSG = "Continue SESSION 2147483647" CHECK_INTERVAL = 30 # 秒 +# host 8568:DISPLAY 唔准寫死。由 env 攞;冇就 fallback ':99' 並一定 log(唔准靜靜 fallback)。 +DISPLAY = os.environ.get("DISPLAY") or ":99" def load_seats(): seats = {} @@ -116,7 +118,7 @@ result = subprocess.run( ['xdotool', 'search', '--onlyvisible', '--name', 'Arena'], capture_output=True, text=True, timeout=5, - env={**os.environ, 'DISPLAY': ':99'} + env={**os.environ, 'DISPLAY': DISPLAY} ) wids = result.stdout.strip().split() if not wids: @@ -127,20 +129,20 @@ geom = subprocess.run( ['xdotool', 'getwindowgeometry', wid], capture_output=True, text=True, timeout=5, - env={**os.environ, 'DISPLAY': ':99'} + env={**os.environ, 'DISPLAY': DISPLAY} ).stdout if '1408x900' not in geom: # 試較返 subprocess.run( ['xdotool', 'windowsize', wid, '1408', '900'], - timeout=5, env={**os.environ, 'DISPLAY': ':99'} + timeout=5, env={**os.environ, 'DISPLAY': DISPLAY} ) time.sleep(1) # 再驗 geom2 = subprocess.run( ['xdotool', 'getwindowgeometry', wid], capture_output=True, text=True, timeout=5, - env={**os.environ, 'DISPLAY': ':99'} + env={**os.environ, 'DISPLAY': DISPLAY} ).stdout if '1408x900' not in geom2: log(sid, f"🚨 window {wid} 較唔返 1408x900,呢個 cycle 唔運行") @@ -950,7 +952,7 @@ try: out = subprocess.run(['xdotool', 'search', '--name', 'Chrome'], capture_output=True, text=True, timeout=5, - env={**__import__('os').environ, 'DISPLAY': ':99'}) + env={**__import__('os').environ, 'DISPLAY': DISPLAY}) wids = [w for w in out.stdout.strip().split('\n') if w] # 搵最大嗰個(browser 主視窗) best, best_area = None, 0 @@ -958,7 +960,7 @@ try: g = subprocess.run(['xdotool', 'getwindowgeometry', wid], capture_output=True, text=True, timeout=3, - env={**__import__('os').environ, 'DISPLAY': ':99'}) + env={**__import__('os').environ, 'DISPLAY': DISPLAY}) for line in g.stdout.split('\n'): if 'Geometry:' in line: parts = line.split()[1].split('x') @@ -978,7 +980,7 @@ out = subprocess.run(['import', '-window', str(window_id), '-crop', f'1x1+{x}+{y}', '-depth', '8', 'txt:-'], capture_output=True, text=True, timeout=5, - env={**__import__('os').environ, 'DISPLAY': ':99'}) + env={**__import__('os').environ, 'DISPLAY': DISPLAY}) import re m = re.search(r'#([0-9a-fA-F]{6})', out.stdout) return '#' + m.group(1).lower() if m else None @@ -1014,7 +1016,7 @@ def _xdotool_click(window_id, x, y): """跟 CA:xdotool 三步點擊""" import subprocess, time - env = {**__import__('os').environ, 'DISPLAY': ':99'} + env = {**__import__('os').environ, 'DISPLAY': DISPLAY} try: subprocess.run(['xdotool', 'windowraise', str(window_id)], env=env, timeout=3, capture_output=True) time.sleep(0.2) @@ -1142,7 +1144,7 @@ def ca_checkbox_click(window_id, x, y): """CA 式:用 xdotool 做 OS 層級滑鼠點擊(唔係 DOM click)""" import subprocess - env = {'DISPLAY': ':99'} + env = {'DISPLAY': DISPLAY} try: subprocess.run(['xdotool', 'windowraise', str(window_id)], env={**os.environ, **env}, timeout=3, capture_output=True) subprocess.run(['xdotool', 'windowactivate', '--sync', str(window_id)], env={**os.environ, **env}, timeout=3, capture_output=True) @@ -1210,6 +1212,7 @@ log(sid, f"Profile 直接用 workspace: {profile_dir}") log(sid, f"啟動 watcher: profile={cfg['profile']}, vpn={cfg['vpn']}:{port}, url={cfg['url']}") + log(sid, f"DISPLAY={DISPLAY}" + ("(由 env)" if os.environ.get("DISPLAY") else "(⚠️ fallback :99:unset DISPLAY)")) with sync_playwright() as p: browser = None