Files
javis_bot/scripts/m0/m0lib/vdisplay.py
javis-bot e0b172e796 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>
2026-11-10 13:29:58 +09:00

207 lines
9.9 KiB
Python

"""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())