feat(m0): check a virtual monitor for the bot browser window

The user wants the bot window on no real screen at all. Add a vdisplay
stage that drives the Parsec Virtual Display Adapter already on the PC
(no install, the display dies with the script), moves the clock window
onto it as a normal and as a tool window, and asks about real monitors,
Alt+Tab, Discord's Screens tab and whether the cursor can wander there.

Record the M0 defaults the user confirmed in docs/v2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-10 13:29:58 +09:00
parent af0c773998
commit e0b172e796
9 changed files with 567 additions and 9 deletions

View File

@@ -21,6 +21,7 @@ LLM 단계는 8080 포트를 씁니다. 다른 프로그램이 이 포트를 쓰
| 확인 | 하실 일 |
|---|---|
| 봇용 창 숨김 | 시계가 움직이는 테스트 창이 뜹니다. Alt+Tab, 작업 표시줄, 디스코드 "화면 공유 → 창" 목록에 보이는지 답합니다. |
| 가상 모니터 | 이미 설치된 Parsec 가상 디스플레이로 모니터를 잠시 하나 만들고 시계 창을 그리로 옮깁니다. 실제 모니터와 Alt+Tab에 보이는지, 디스코드 "화면 공유 → 화면" 탭에 그 모니터가 나오는지, 마우스가 그쪽으로 넘어가는지 답합니다. 끝나면 가상 모니터는 사라지고 아무것도 설치하지 않습니다. |
| 디스코드 창 분리 | 친구(또는 다른 기기의 내 계정)의 화면공유를 시청하고 "창 분리(팝아웃)"를 누릅니다. 지금 할 수 없으면 `s`로 건너뜁니다. |
| 게임 감지 | 게임을 하나 켜고(테두리 없는 창 모드면 가장 좋음), 안티치트 게임이 있으면 그것도 켭니다. 마지막으로 브라우저 영상을 전체화면으로 틉니다. 각각 Enter 후 15초 안에 그 화면으로 바꾸면 됩니다. |
@@ -50,6 +51,6 @@ LLM 단계는 8080 포트를 씁니다. 다른 프로그램이 이 포트를 쓰
.\run_m0.ps1 -Only stt,tts_melo -Redo # 특정 단계만 다시
```
단계 이름: `sysinfo`, `winchecks`, `tts_melo`, `tts_cosy2`, `tts_cosy3`, `stt`, `llm_fast`, `llm_recommended`
단계 이름: `sysinfo`, `winchecks`, `vdisplay`, `tts_melo`, `tts_cosy2`, `tts_cosy3`, `stt`, `llm_fast`, `llm_recommended`
다 끝나고 필요 없으면 작업 폴더(`\JarvisM0`)를 통째로 지우면 됩니다. Python, 모델, Strata(엔진과 설정 포함)는 모두 그 안에 설치하도록 지정해 두었습니다.

View File

@@ -28,6 +28,7 @@ from m0lib.common import read_json, say, step, write_json # noqa: E402
STAGES = [
("sysinfo", "PC 정보", "", "sysinfo.py", [], 0),
("winchecks", "창·디스코드·게임 확인 (직접 답해 주셔야 합니다)", "", "winchecks.py", [], 0),
("vdisplay", "가상 모니터에 봇용 창 두기 (직접 답해 주셔야 합니다)", "", "vdisplay.py", [], 0),
("tts_melo", "MeloTTS 속도", "melo", "tts_melo.py", [], 10),
("tts_cosy2", "CosyVoice2 속도", "cosy", "tts_cosy.py", ["models=cosyvoice2"], 15),
("tts_cosy3", "Fun-CosyVoice3 속도", "cosy", "tts_cosy.py", ["models=cosyvoice3"], 8),
@@ -114,7 +115,7 @@ def main() -> int:
say("Jarvis v2 M0 측정을 시작합니다.")
say(f"작업 폴더: {work} (남은 공간 {free_gb(work):.0f} GB)")
say("모든 파일은 이 작업 폴더 안에만 받습니다. 끝나면 결과 zip 파일 위치를 알려 드립니다.")
say("처음 두 단계는 직접 답해 주셔야 하고, 그 뒤는 자리를 비우셔도 됩니다.")
say("앞쪽의 '직접 답해 주셔야 합니다' 단계가 끝나면 자리를 비우셔도 됩니다.")
say("측정이 정확하도록 그동안 게임이나 무거운 프로그램은 꺼 두세요.\n")
t0 = time.time()
@@ -141,7 +142,7 @@ def main() -> int:
except Exception as e:
say(f" [오류] {title}: {e}")
write_json(out, {"stage": key, "status": "failed", "errors": [{"where": "setup", "error": str(e)}]})
if key == "winchecks":
if key == "vdisplay":
say("\n직접 확인하는 단계는 끝났습니다. 이제 자리를 비우셔도 됩니다.")
target = pack(work)

View File

@@ -0,0 +1,206 @@
"""Stage: a virtual monitor for the bot browser window (needs the user at the PC).
The plan's fallback (docs/v2/pc-features.md, "봇용 크롬창과 디스코드 화면공유"):
the bot window lives on a virtual monitor as a tool window, so it is on no
real screen and not in Alt+Tab, and Discord shares it through the "Screens"
tab. This stage checks each part on the PC with the Parsec Virtual Display
Adapter that is already installed there:
driver can the kit open the driver and add a display without admin rights
window the bot window moved onto that display, normal and as a tool window
discord the display in Discord's "Screens" tab, and the shared preview moving
mouse whether the cursor can wander onto the display
cleanup the display goes away again, and where the window ends up
The display only lives while this script pings the driver, so it disappears by
itself if the script stops. Nothing is installed and nothing is kept.
"""
from __future__ import annotations
import os
import subprocess
import sys
import time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from m0lib.common import IS_WINDOWS, StageResult, ask, say, stage_args, step, wait_enter # noqa: E402
from m0lib.winchecks import TEST_TITLE, find_browser, frames, html_page # noqa: E402
SWP_NOZORDER = 0x0004
def _inside(pt: tuple[int, int], rect: list[int]) -> bool:
return rect[0] <= pt[0] < rect[2] and rect[1] <= pt[1] < rect[3]
def watch_cursor(w, rect: list[int], seconds: float) -> dict:
import ctypes
from ctypes import wintypes
p = wintypes.POINT()
hits, samples = 0, 0
end = time.time() + seconds
while time.time() < end:
w.user32.GetCursorPos(ctypes.byref(p))
samples += 1
hits += int(_inside((p.x, p.y), rect))
time.sleep(0.02)
return {"samples": samples, "inside_virtual": hits}
def open_bot_window(work: Path):
from m0lib import winwin as w
browser = find_browser()
if not browser:
return None, None, None
page = html_page(work / "m0_clock.html")
profile = work / "browser_vdisplay_profile"
proc = subprocess.Popen([browser, f"--user-data-dir={profile}", "--no-first-run", "--no-default-browser-check",
"--disable-backgrounding-occluded-windows", "--disable-renderer-backgrounding",
"--disable-background-timer-throttling", "--window-size=900,600",
"--window-position=200,200", f"--app={page.as_uri()}"])
for _ in range(60):
time.sleep(0.5)
for win in w.top_windows():
if TEST_TITLE in win["title"] and win["class"].startswith("Chrome_WidgetWin"):
return proc, profile, win["hwnd"]
return proc, profile, None
def run(r: StageResult, work: Path) -> None:
from m0lib import winvdd as v
from m0lib import winwin as w
res: dict = {}
r["vdisplay"] = res
before = v.displays()
res["displays_before"] = [{k: d[k] for k in ("adapter", "rect", "primary", "virtual")} for d in before]
res["parsec_app_running"] = "parsecd.exe" in set(w.processes().values())
step("1/5 가상 모니터 드라이버 열기")
try:
vdd = v.Vdd()
except OSError as e:
r.error("driver_open", e)
res["driver_open"] = False
say(f" 드라이버를 열지 못했습니다: {e}")
return
res["driver_open"] = True
proc = profile = None
try:
res["driver_minor_version"] = vdd.version()
step("2/5 가상 모니터 추가")
say(" 잠시 뒤 가상 모니터가 하나 생깁니다. 실제 모니터가 잠깐 깜빡일 수 있습니다.")
t0 = time.perf_counter()
idx = vdd.add()
res["add_index"] = idx
after = v.wait_for_displays(lambda cur: sum(d["virtual"] for d in cur) > sum(d["virtual"] for d in before))
res["add_seconds"] = round(time.perf_counter() - t0, 2)
new = [d for d in after if d["virtual"] and d["device"] not in {b["device"] for b in before}]
if idx < 0 or not new:
r.error("add", f"가상 모니터가 생기지 않았습니다 (index {idx})")
return
virt = new[0]
res["virtual_default"] = {"rect": virt["rect"], "hz": virt["hz"]}
say(f" 생겼습니다: {virt['rect'][2] - virt['rect'][0]}x{virt['rect'][3] - virt['rect'][1]}, "
f"{res['add_seconds']}초 걸림")
res["flicker_on_add"] = ask(" 가상 모니터가 생길 때 실제 모니터가 깜빡이거나 열린 창 위치가 바뀌었나요?", "yn")
physical = [d for d in after if not d["virtual"]]
x, y = v.corner_position(physical)
res["move_target"] = [x, y]
res["move_result"] = v.move_display(virt["device"], x, y)
time.sleep(1.5)
virt = next((d for d in v.displays() if d["device"] == virt["device"]), virt)
res["virtual_rect"] = virt["rect"]
res["corner_only"] = virt["rect"][0] == x and virt["rect"][1] == y
res["physical_rects"] = [d["rect"] for d in physical]
step("3/5 봇용 창을 가상 모니터로 옮기기")
proc, profile, hwnd = open_bot_window(work)
if not hwnd:
r.error("window", "테스트 창을 찾지 못했습니다")
return
vx, vy = virt["rect"][0], virt["rect"][1]
w.user32.SetWindowPos(hwnd, None, vx + 50, vy + 50, 900, 600, w.SWP_NOACTIVATE | SWP_NOZORDER)
time.sleep(1)
res["window_on_virtual"] = w.monitor_rect(hwnd) == virt["rect"]
res["drawing_normal"] = frames(hwnd, 3)
say(f" 옮김: {'예' if res['window_on_virtual'] else '아니요'}, "
f"그리기 계속: {'예' if res['drawing_normal']['drawing'] else '아니요'}")
res["seen_on_real_monitors"] = ask(f" 실제 모니터 어디에든 '{TEST_TITLE}'(시계 창)가 보이나요?", "yn", "n")
res["alt_tab_listed_normal"] = ask(" Alt+Tab을 눌러 보세요. 목록에 이 창이 있나요?", "yn")
w.set_tool_window(hwnd)
w.user32.SetWindowPos(hwnd, None, vx + 50, vy + 50, 900, 600, w.SWP_NOACTIVATE | SWP_NOZORDER)
time.sleep(1.5)
res["tool_window_on_virtual"] = w.monitor_rect(hwnd) == virt["rect"]
res["drawing_tool"] = frames(hwnd, 3)
say("\n 같은 창을 '도구 창' 속성으로 바꿨습니다 (계획에 적은 실제 사용 형태입니다).")
res["alt_tab_hidden_tool"] = ask(" Alt+Tab을 다시 눌러 보세요. 이번에는 목록에서 빠졌나요?", "yn")
res["taskbar_hidden_tool"] = ask(" 작업 표시줄에서도 이 창 아이콘이 없나요?", "yn")
step("4/5 디스코드 '화면' 탭에서 가상 모니터 공유")
say(" 디스코드에서 '화면 공유'를 누르고 '화면'(모니터) 탭을 열어 보세요.")
res["discord_screens_listed"] = ask(" 모니터가 하나 더(가상 모니터) 보이나요? 미리보기에 시계 창이 보이면 그것입니다. (s=디스코드 없음)",
"yns", "s")
if res["discord_screens_listed"] == "y":
say(" 그 화면을 실제로 공유해 보세요 (나만 있는 채널도 괜찮습니다).")
if wait_enter(" 공유를 시작했으면 Enter (건너뛰려면 s 입력 후 Enter):") != "s":
res["drawing_while_shared"] = frames(hwnd, 5)
res["discord_preview_moving"] = ask(" 디스코드 공유 미리보기에서 시계가 계속 움직이나요?", "yn")
say(" 공유를 끝내 주세요.")
wait_enter(" 끝냈으면 Enter:")
step("5/5 마우스가 가상 모니터로 넘어가는지")
say(" 10초 동안 마우스를 모든 모니터의 가장자리와 구석, 특히 오른쪽 아래 구석으로 여러 번 밀어 보세요.")
if wait_enter(" 준비되면 Enter (건너뛰려면 s 입력 후 Enter):") != "s":
res["cursor"] = watch_cursor(w, virt["rect"], 10)
say(f" 커서가 가상 모니터로 넘어간 횟수(측정 표본): {res['cursor']['inside_virtual']}")
res["cursor_lost_user"] = ask(" 그동안 커서가 화면 밖으로 사라진 적이 있나요?", "yn")
finally:
def new_virtual(cur: list[dict]) -> list[dict]:
return [d for d in cur if d["virtual"] and d["device"] not in {b["device"] for b in before}]
try:
t0 = time.perf_counter()
vdd.close()
res["removed"] = not new_virtual(v.wait_for_displays(lambda cur: not new_virtual(cur)))
res["remove_seconds"] = round(time.perf_counter() - t0, 2)
except Exception as e:
r.error("remove", e)
if proc is not None:
# Where Windows puts the window once its display is gone: if the app ever stops
# without closing its browser, this is where the bot window would show up.
time.sleep(1)
for win in w.top_windows():
if TEST_TITLE in win["title"]:
corner = (win["rect"][0] + 10, win["rect"][1] + 10)
res["window_after_remove"] = {"on_real_monitor": any(_inside(corner, d["rect"]) for d in before)}
break
w.kill_tree(proc.pid)
w.kill_by_cmdline(profile.name)
say(f" 가상 모니터를 없앴습니다: {'예' if res.get('removed') else '아니요'}")
def main() -> int:
out, work, _opts = stage_args()
r = StageResult("vdisplay", out)
if not IS_WINDOWS:
r.error("vdisplay", "Windows가 아니라서 건너뜁니다")
r.save("skipped")
return 0
from m0lib import winwin as w
w.dpi_aware()
try:
run(r, work)
except Exception as e:
r.error("vdisplay", e)
# "failed" so the next run asks again: this stage exists to answer one question.
r.save("ok" if not r["errors"] else "failed")
return 0
if __name__ == "__main__":
sys.exit(main())

269
scripts/m0/m0lib/winvdd.py Normal file
View File

@@ -0,0 +1,269 @@
"""Parsec Virtual Display Adapter control and display enumeration over ctypes. Windows only at run time.
The driver interface follows nomi-san/parsec-vdd (core/parsec-vdd.h): open the
device interface, then DeviceIoControl with a 32-byte input buffer and a DWORD
output. An added display stays only while UPDATE is sent at least every 100 ms,
so a display dies with the process that keeps it (no display is left behind
if the kit crashes).
The structures are defined at import time so their sizes can be checked on any
OS; the DLLs are loaded only when a function is called.
"""
from __future__ import annotations
import ctypes
import threading
import time
from ctypes import wintypes
GUID_VDD_ADAPTER = (0x00B41627, 0x04C4, 0x429E, (0xA2, 0x6E, 0x02, 0x65, 0xCF, 0x50, 0xC8, 0xFA))
IOCTL_ADD, IOCTL_REMOVE, IOCTL_UPDATE, IOCTL_VERSION = 0x0022E004, 0x0022A008, 0x0022A00C, 0x0022E010
DIGCF_PRESENT, DIGCF_DEVICEINTERFACE = 0x2, 0x10
DISPLAY_DEVICE_ATTACHED_TO_DESKTOP, DISPLAY_DEVICE_PRIMARY_DEVICE = 0x1, 0x4
ENUM_CURRENT_SETTINGS = -1
DM_POSITION = 0x20
CDS_UPDATEREGISTRY, CDS_NORESET = 0x1, 0x10000000
# UTF-16 code units, so the layout is the Windows one on every OS (c_wchar is 4 bytes on Linux).
# DWORD/LONG are fixed 32-bit for the same reason (wintypes.DWORD is 8 bytes on 64-bit Linux).
WCHAR, WORD, DWORD, LONG = ctypes.c_uint16, ctypes.c_uint16, ctypes.c_uint32, ctypes.c_int32
def wstr(units) -> str:
raw = bytes(memoryview(units).cast("B"))
return raw.decode("utf-16-le", errors="replace").split("\x00", 1)[0]
class GUID(ctypes.Structure):
_fields_ = [("Data1", DWORD), ("Data2", WORD), ("Data3", WORD),
("Data4", ctypes.c_ubyte * 8)]
class SP_DEVICE_INTERFACE_DATA(ctypes.Structure):
_fields_ = [("cbSize", DWORD), ("InterfaceClassGuid", GUID), ("Flags", DWORD),
("Reserved", ctypes.c_size_t)]
class OVERLAPPED(ctypes.Structure):
_fields_ = [("Internal", ctypes.c_size_t), ("InternalHigh", ctypes.c_size_t), ("Offset", DWORD),
("OffsetHigh", DWORD), ("hEvent", ctypes.c_void_p)]
class DISPLAY_DEVICEW(ctypes.Structure):
_fields_ = [("cb", DWORD), ("DeviceName", WCHAR * 32), ("DeviceString", WCHAR * 128),
("StateFlags", DWORD), ("DeviceID", WCHAR * 128), ("DeviceKey", WCHAR * 128)]
class POINTL(ctypes.Structure):
_fields_ = [("x", LONG), ("y", LONG)]
class DEVMODEW(ctypes.Structure):
"""The display variant of DEVMODEW (220 bytes)."""
_fields_ = [("dmDeviceName", WCHAR * 32), ("dmSpecVersion", WORD),
("dmDriverVersion", WORD), ("dmSize", WORD), ("dmDriverExtra", WORD),
("dmFields", DWORD), ("dmPosition", POINTL), ("dmDisplayOrientation", DWORD),
("dmDisplayFixedOutput", DWORD), ("dmColor", ctypes.c_short), ("dmDuplex", ctypes.c_short),
("dmYResolution", ctypes.c_short), ("dmTTOption", ctypes.c_short), ("dmCollate", ctypes.c_short),
("dmFormName", WCHAR * 32), ("dmLogPixels", WORD), ("dmBitsPerPel", DWORD),
("dmPelsWidth", DWORD), ("dmPelsHeight", DWORD), ("dmDisplayFlags", DWORD),
("dmDisplayFrequency", DWORD), ("dmICMMethod", DWORD),
("dmICMIntent", DWORD), ("dmMediaType", DWORD), ("dmDitherType", DWORD),
("dmReserved1", DWORD), ("dmReserved2", DWORD),
("dmPanningWidth", DWORD), ("dmPanningHeight", DWORD)]
def _guid(parts) -> GUID:
return GUID(parts[0], parts[1], parts[2], (ctypes.c_ubyte * 8)(*parts[3]))
def _dlls():
setupapi = ctypes.WinDLL("setupapi", use_last_error=True)
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
user32 = ctypes.WinDLL("user32", use_last_error=True)
vp, H, D, B = ctypes.c_void_p, wintypes.HANDLE, wintypes.DWORD, wintypes.BOOL
for fn, args, res in (
(setupapi.SetupDiGetClassDevsW, [ctypes.POINTER(GUID), wintypes.LPCWSTR, wintypes.HWND, D], vp),
(setupapi.SetupDiEnumDeviceInterfaces, [vp, vp, ctypes.POINTER(GUID), D,
ctypes.POINTER(SP_DEVICE_INTERFACE_DATA)], B),
(setupapi.SetupDiGetDeviceInterfaceDetailW, [vp, ctypes.POINTER(SP_DEVICE_INTERFACE_DATA), vp, D,
ctypes.POINTER(D), vp], B),
(setupapi.SetupDiDestroyDeviceInfoList, [vp], B),
(kernel32.CreateFileW, [wintypes.LPCWSTR, D, D, vp, D, D, H], H),
(kernel32.CreateEventW, [vp, B, B, wintypes.LPCWSTR], H),
(kernel32.DeviceIoControl, [H, D, vp, D, vp, D, ctypes.POINTER(D), ctypes.POINTER(OVERLAPPED)], B),
(kernel32.GetOverlappedResultEx, [H, ctypes.POINTER(OVERLAPPED), ctypes.POINTER(D), D, B], B),
(kernel32.CancelIoEx, [H, ctypes.POINTER(OVERLAPPED)], B),
(kernel32.GetOverlappedResult, [H, ctypes.POINTER(OVERLAPPED), ctypes.POINTER(D), B], B),
(kernel32.CloseHandle, [H], B),
(user32.EnumDisplayDevicesW, [wintypes.LPCWSTR, D, ctypes.POINTER(DISPLAY_DEVICEW), D], B),
(user32.EnumDisplaySettingsW, [wintypes.LPCWSTR, D, ctypes.POINTER(DEVMODEW)], B),
(user32.ChangeDisplaySettingsExW, [wintypes.LPCWSTR, ctypes.POINTER(DEVMODEW), wintypes.HWND, D, vp],
ctypes.c_long),
):
fn.argtypes, fn.restype = args, res
return setupapi, kernel32, user32
INVALID = ctypes.c_void_p(-1).value
class Vdd:
"""One open handle to the driver, with the keep-alive thread."""
def __init__(self):
self.setupapi, self.kernel32, self.user32 = _dlls()
self.handle = self._open()
self.lock = threading.Lock()
self._stop = threading.Event()
self._pinger: threading.Thread | None = None
self.added: list[int] = []
def _open(self):
guid = _guid(GUID_VDD_ADAPTER)
info = self.setupapi.SetupDiGetClassDevsW(ctypes.byref(guid), None, None,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE)
if not info or info == INVALID:
raise OSError(ctypes.get_last_error(), "Parsec 가상 디스플레이 장치를 찾지 못했습니다")
try:
i = 0
data = SP_DEVICE_INTERFACE_DATA()
data.cbSize = ctypes.sizeof(SP_DEVICE_INTERFACE_DATA)
last_err = 0
while self.setupapi.SetupDiEnumDeviceInterfaces(info, None, ctypes.byref(guid), i, ctypes.byref(data)):
i += 1
size = wintypes.DWORD(0)
self.setupapi.SetupDiGetDeviceInterfaceDetailW(info, ctypes.byref(data), None, 0, ctypes.byref(size),
None)
buf = ctypes.create_string_buffer(max(size.value, 16))
# cbSize is the fixed part of SP_DEVICE_INTERFACE_DETAIL_DATA_W: 8 on 64-bit, 6 on 32-bit.
ctypes.cast(buf, ctypes.POINTER(wintypes.DWORD))[0] = 8 if ctypes.sizeof(ctypes.c_void_p) == 8 else 6
if not self.setupapi.SetupDiGetDeviceInterfaceDetailW(info, ctypes.byref(data), buf, size,
ctypes.byref(size), None):
last_err = ctypes.get_last_error()
continue
path = ctypes.wstring_at(ctypes.addressof(buf) + 4)
h = self.kernel32.CreateFileW(path, 0xC0000000, 0x3, None, 3,
0x80 | 0x20000000 | 0x40000000 | 0x80000000, None)
if h and h != INVALID:
return h
last_err = ctypes.get_last_error()
raise OSError(last_err or ctypes.get_last_error(), f"장치를 열지 못했습니다 (인터페이스 {i}개)")
finally:
self.setupapi.SetupDiDestroyDeviceInfoList(info)
def ioctl(self, code: int, data: bytes = b"") -> int:
inbuf = ctypes.create_string_buffer(data[:32], 32)
out = wintypes.DWORD(0)
ov = OVERLAPPED()
ov.hEvent = self.kernel32.CreateEventW(None, True, False, None)
n = wintypes.DWORD(0)
try:
with self.lock:
self.kernel32.DeviceIoControl(self.handle, code, inbuf, 32, ctypes.byref(out), 4, None,
ctypes.byref(ov))
if not self.kernel32.GetOverlappedResultEx(self.handle, ctypes.byref(ov), ctypes.byref(n), 5000,
False):
# The request may still be pending: cancel it and wait, or the driver
# could later write into these buffers after they are freed.
self.kernel32.CancelIoEx(self.handle, ctypes.byref(ov))
self.kernel32.GetOverlappedResult(self.handle, ctypes.byref(ov), ctypes.byref(n), True)
return -1
return out.value
finally:
if ov.hEvent:
self.kernel32.CloseHandle(ov.hEvent)
def version(self) -> int:
return self.ioctl(IOCTL_VERSION)
def _ping(self) -> None:
while not self._stop.wait(0.05):
self.ioctl(IOCTL_UPDATE)
def add(self) -> int:
if self._pinger is None:
self._pinger = threading.Thread(target=self._ping, daemon=True)
self._pinger.start()
idx = self.ioctl(IOCTL_ADD)
self.ioctl(IOCTL_UPDATE)
if idx >= 0:
self.added.append(idx)
return idx
def remove(self, idx: int) -> None:
# The driver reads the index as a big-endian UINT16.
self.ioctl(IOCTL_REMOVE, (idx & 0xFFFF).to_bytes(2, "big"))
self.ioctl(IOCTL_UPDATE)
if idx in self.added:
self.added.remove(idx)
def close(self) -> None:
for idx in list(self.added):
self.remove(idx)
self._stop.set()
if self._pinger:
self._pinger.join(timeout=2)
self.kernel32.CloseHandle(self.handle)
def displays() -> list[dict]:
"""Displays attached to the desktop: device name, adapter, rect in desktop pixels, primary flag."""
_, _, user32 = _dlls()
out = []
i = 0
while True:
dd = DISPLAY_DEVICEW()
dd.cb = ctypes.sizeof(DISPLAY_DEVICEW)
if not user32.EnumDisplayDevicesW(None, i, ctypes.byref(dd), 0):
break
i += 1
if not dd.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP:
continue
dm = DEVMODEW()
dm.dmSize = ctypes.sizeof(DEVMODEW)
name, adapter = wstr(dd.DeviceName), wstr(dd.DeviceString)
if not user32.EnumDisplaySettingsW(name, ENUM_CURRENT_SETTINGS, ctypes.byref(dm)):
continue
x, y = dm.dmPosition.x, dm.dmPosition.y
out.append({"device": name, "adapter": adapter,
"rect": [x, y, x + dm.dmPelsWidth, y + dm.dmPelsHeight], "hz": dm.dmDisplayFrequency,
"primary": bool(dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE),
"virtual": "parsec" in adapter.lower()})
return out
def wait_for_displays(pred, timeout: float = 10.0) -> list[dict]:
end = time.time() + timeout
cur = displays()
while not pred(cur) and time.time() < end:
time.sleep(0.25)
cur = displays()
return cur
def corner_position(physical: list[dict]) -> tuple[int, int]:
"""Where a display only touches the others at the bottom-right corner of the desktop.
The cursor crosses from one display to another only along a shared edge, so a display that meets the
rest at a single point is the hardest one for the mouse to wander onto.
"""
right = max(d["rect"][2] for d in physical)
bottom = max(d["rect"][3] for d in physical if d["rect"][2] == right)
return right, bottom
def move_display(device: str, x: int, y: int) -> list[int]:
"""Move a display; returns the two ChangeDisplaySettingsEx results (0 = DISP_CHANGE_SUCCESSFUL)."""
_, _, user32 = _dlls()
dm = DEVMODEW()
dm.dmSize = ctypes.sizeof(DEVMODEW)
if not user32.EnumDisplaySettingsW(device, ENUM_CURRENT_SETTINGS, ctypes.byref(dm)):
return [-99]
dm.dmFields = DM_POSITION
dm.dmPosition.x, dm.dmPosition.y = x, y
a = user32.ChangeDisplaySettingsExW(device, ctypes.byref(dm), None, CDS_UPDATEREGISTRY | CDS_NORESET, None)
b = user32.ChangeDisplaySettingsExW(None, None, None, 0, None)
return [a, b]