60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
from core.models.session.SessionProfile import SessionProfile
|
|
from core.models.Result import Result, ResultError
|
|
|
|
import os
|
|
import re
|
|
import subprocess
|
|
import time
|
|
import shutil
|
|
|
|
|
|
def find_unused_display() -> str:
|
|
"""Get a string of an unused X11 display port"""
|
|
|
|
file_names = os.listdir('/tmp/.X11-unix')
|
|
active_displays = []
|
|
|
|
for file_name in file_names:
|
|
|
|
match_object = re.search(r'X(\d+)', file_name)
|
|
|
|
if match_object:
|
|
|
|
detected_display = int(match_object.group(1))
|
|
|
|
if 170 <= detected_display <= 198:
|
|
active_displays.append(detected_display)
|
|
|
|
if len(active_displays) > 0:
|
|
|
|
unused_display = sorted(active_displays)[-1] + 1
|
|
return f':{str(unused_display)}'
|
|
|
|
else:
|
|
return ':170'
|
|
|
|
|
|
|
|
def launch_xephyr(profile: SessionProfile, display: str) -> Result:
|
|
"""Run Xephyr with the correct display, and get it's persistent Popen object"""
|
|
|
|
if shutil.which('Xephyr') is None:
|
|
return Result(valid=False, error_type=ResultError.MISSING_DEPENDENCY, message="Xephyr pre-req is missing.")
|
|
|
|
virtual_display_process = subprocess.Popen(('Xephyr', '-ac', '-br', '-title', f'HydraVeil - {profile.name or "Unnamed Profile"}', '-screen', profile.resolution, '-no-host-grab', display), stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
|
|
|
|
start_time = time.time()
|
|
timeout = float(10)
|
|
|
|
while not os.path.exists(f'/tmp/.X11-unix/X{display[1:]}'):
|
|
|
|
if time.time() - start_time < timeout:
|
|
time.sleep(0.1)
|
|
|
|
else:
|
|
|
|
virtual_display_process.kill()
|
|
return
|
|
|
|
return Result(valid=True, data=virtual_display_process)
|
|
|