All checks were successful
Build & Push Docker Image / build-and-publish (push) Successful in 21s
119 lines
3.7 KiB
Python
119 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Window title synchronization for Selkies
|
|
Monitors the active window title and updates the browser tab title dynamically
|
|
|
|
Note: This script is designed to run inside a Linux container with X11 tools installed.
|
|
Pylance warnings about missing imports are expected in the development environment.
|
|
"""
|
|
|
|
import subprocess
|
|
import time
|
|
import os
|
|
import sys
|
|
|
|
def get_active_window_title():
|
|
"""Get the title of the currently active window"""
|
|
try:
|
|
# Try using xdotool first
|
|
result = subprocess.run(
|
|
['xdotool', 'getactivewindow', 'getwindowname'],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=1
|
|
)
|
|
if result.returncode == 0:
|
|
return result.stdout.strip()
|
|
except (subprocess.TimeoutExpired, FileNotFoundError):
|
|
pass
|
|
|
|
try:
|
|
# Fallback to wmctrl
|
|
result = subprocess.run(
|
|
['wmctrl', '-l'],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=1
|
|
)
|
|
if result.returncode == 0:
|
|
lines = result.stdout.strip().split('\n')
|
|
for line in lines:
|
|
if 'RuneLite' in line:
|
|
# Extract title (after the third column)
|
|
parts = line.split(None, 3)
|
|
if len(parts) >= 4:
|
|
return parts[3]
|
|
except (subprocess.TimeoutExpired, FileNotFoundError):
|
|
pass
|
|
|
|
return None
|
|
|
|
def update_browser_title(title):
|
|
"""Update the Selkies browser tab title"""
|
|
# Try multiple possible locations for Selkies title file
|
|
title_locations = [
|
|
'/opt/gst-web/title',
|
|
'/tmp/selkies_title',
|
|
'/var/run/selkies/title'
|
|
]
|
|
|
|
success = False
|
|
for title_file in title_locations:
|
|
try:
|
|
# Create directory if it doesn't exist
|
|
os.makedirs(os.path.dirname(title_file), exist_ok=True)
|
|
|
|
with open(title_file, 'w') as f:
|
|
f.write(title)
|
|
success = True
|
|
break
|
|
except Exception:
|
|
continue
|
|
|
|
if not success:
|
|
# Fallback: try to update via xdotool if title file doesn't work
|
|
try:
|
|
subprocess.run(['xdotool', 'set_desktop_name', title], timeout=1, check=False)
|
|
except Exception:
|
|
pass
|
|
|
|
def main():
|
|
print("Window title sync started - monitoring RuneLite window", flush=True)
|
|
|
|
# Ensure DISPLAY is set
|
|
if 'DISPLAY' not in os.environ:
|
|
os.environ['DISPLAY'] = ':0'
|
|
print("Set DISPLAY to :0", flush=True)
|
|
|
|
last_title = None
|
|
|
|
# Initial delay to let X11 and RuneLite window appear
|
|
print("Waiting for RuneLite window to appear...", flush=True)
|
|
time.sleep(10)
|
|
|
|
while True:
|
|
try:
|
|
current_title = get_active_window_title()
|
|
|
|
# Only update if title has changed and contains "RuneLite"
|
|
if current_title and 'RuneLite' in current_title and current_title != last_title:
|
|
print(f"Window title changed to: {current_title}", flush=True)
|
|
update_browser_title(current_title)
|
|
last_title = current_title
|
|
elif current_title and last_title is None:
|
|
# Log first title found for debugging
|
|
print(f"First window title detected: {current_title}", flush=True)
|
|
|
|
# Check every 2 seconds
|
|
time.sleep(2)
|
|
|
|
except KeyboardInterrupt:
|
|
print("\nWindow title sync stopped", flush=True)
|
|
break
|
|
except Exception as e:
|
|
print(f"Error in title monitoring: {e}", file=sys.stderr, flush=True)
|
|
time.sleep(5)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|