"""Register / unregister AR Display Manager as a Windows autostart entry.""" from __future__ import annotations import sys from pathlib import Path _REG_KEY = r"Software\Microsoft\Windows\CurrentVersion\Run" _APP_NAME = "AR Display Manager" def _pythonw() -> str: """Return the pythonw.exe path (no console window on start).""" exe = Path(sys.executable) pw = exe.parent / "pythonw.exe" return str(pw) if pw.exists() else sys.executable def enable() -> bool: """Add the autostart registry entry. Returns True on success.""" if sys.platform != "win32": return False try: import winreg # type: ignore[import-not-found] launcher = Path(__file__).resolve().parents[1] / "display_manager_main.py" cmd = f'"{_pythonw()}" "{launcher}"' with winreg.OpenKey( winreg.HKEY_CURRENT_USER, _REG_KEY, 0, winreg.KEY_SET_VALUE ) as key: winreg.SetValueEx(key, _APP_NAME, 0, winreg.REG_SZ, cmd) return True except Exception: return False def disable() -> bool: """Remove the autostart registry entry. Returns True if it existed.""" if sys.platform != "win32": return False try: import winreg # type: ignore[import-not-found] with winreg.OpenKey( winreg.HKEY_CURRENT_USER, _REG_KEY, 0, winreg.KEY_SET_VALUE ) as key: winreg.DeleteValue(key, _APP_NAME) return True except FileNotFoundError: return False except Exception: return False def is_enabled() -> bool: """Return True if the autostart entry exists.""" if sys.platform != "win32": return False try: import winreg # type: ignore[import-not-found] with winreg.OpenKey( winreg.HKEY_CURRENT_USER, _REG_KEY, 0, winreg.KEY_READ ) as key: winreg.QueryValueEx(key, _APP_NAME) return True except FileNotFoundError: return False except Exception: return False