Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

21.1 KB
Raw Blame
"""Select an area of the screen for a region appshot.

:func:`pick_region` opens a selection in the prepared picker sidecar
(:mod:`dodaios.appshot.picker`), waits for the user to drag a rectangle and
returns it; :func:`selection_to_bbox` turns that into capture coordinates.

Two coordinate worlds meet here:

- **capture space** — what Screen Context grabs in: physical pixels on
  Windows (per-monitor DPI aware), points on macOS, root-window pixels on X11.
  ``monitors`` are mss-shaped dicts (``[0]`` = the virtual desktop).
- **Qt space** — the picker's logical pixels, one ``QScreen`` per monitor
  with its own ``devicePixelRatio``.

A selection never crosses between them as absolute coordinates. The picker
reports *which screen* (its Qt geometry and scale) and the selection as
*fractions of that screen*; :func:`match_monitor` finds the same screen in
capture space. Fractions survive every mixed-DPI layout, because a screen's
content scales uniformly within itself even where the virtual-desktop origins
of different screens disagree between the two worlds.

Everything above :func:`picker_capability` is pure and imports nothing
heavy; the picker itself is the only place PySide6 is loaded.
"""

from __future__ import annotations

import asyncio
import contextlib
import contextvars
import importlib.util
import logging
import os
import subprocess
import sys
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
from uuid import UUID

from dodaios.appshot.markup import Markup, parse_markup

log = logging.getLogger(__name__)

#: A drag smaller than this (logical px, either side) is a click, not a pick.
MIN_SELECTION_PX = 4

#: The picker gives up on its own after this long without a result. Long
#: enough to mark an area up in peace; Esc still ends it at once.
PICK_TIMEOUT_S = 900.0
#: Grace for a stopped or failed picker process to exit.
_EXIT_GRACE_S = 2.0
#: Extra settle time so the compositor has repainted without the dim layer.
_SETTLE_S = 0.08

Rect = tuple[int, int, int, int]
FRect = tuple[float, float, float, float]


@dataclass(frozen=True, slots=True)
class Selection:
    """A finished selection: which Qt screen, which part of it, and how it ended.

    ``action`` is the button the user finished with (``done``, ``copy``,
    ``save`` or ``edit``); ``markup`` holds what they drew on the area, if
    anything (:mod:`dodaios.appshot.markup`).
    """

    screen: dict[str, float]
    rect: FRect
    action: str = "done"
    markup: Markup | None = None


class RegionUnavailable(RuntimeError):
    """No area can be selected on this host; the message says why."""


#: The toolbar's tooltip language for the picker being started.
_language: contextvars.ContextVar[str] = contextvars.ContextVar(
    "appshot_picker_language", default="en"
)
#: "Adjust the area before taking it" for the picker being started.
_keep_area: contextvars.ContextVar[bool] = contextvars.ContextVar(
    "appshot_picker_keep_area", default=False
)

#: One picker at a time: a second shortcut press, the page button and the
#: voice tool must not stack overlays (one Esc would cancel all of them).
_picking = False


# --------------------------------------------------------------------------
# Pure geometry
# --------------------------------------------------------------------------


def selection_fractions(
    x0: float, y0: float, x1: float, y1: float, width: float, height: float
) -> FRect | None:
    """A drag from ``(x0, y0)`` to ``(x1, y1)`` on a ``width × height`` screen.

    Returns ``(fx, fy, fw, fh)`` clamped to the screen, or ``None`` when the
    drag is too small to be a selection. Works for a drag in any direction.
    """
    if width <= 0 or height <= 0:
        return None
    left = max(0.0, min(float(x0), float(x1)))
    top = max(0.0, min(float(y0), float(y1)))
    right = min(float(width), max(float(x0), float(x1)))
    bottom = min(float(height), max(float(y0), float(y1)))
    if right - left < MIN_SELECTION_PX or bottom - top < MIN_SELECTION_PX:
        return None
    return (left / width, top / height, (right - left) / width, (bottom - top) / height)


def screen_monitor_score(screen: dict, monitor: dict) -> float:
    """How far a Qt screen is from a capture-space monitor (0 = identical).

    Each plausible reading of the Qt geometry is tried — logical as-is (macOS
    points, X11 at scale 1), logical origin with physical size (Qt 6 on
    Windows), and fully physical — and the closest one counts.
    """
    x = float(screen.get("x", 0))
    y = float(screen.get("y", 0))
    w = float(screen.get("w", 0))
    h = float(screen.get("h", 0))
    dpr = float(screen.get("dpr", 1.0) or 1.0)
    ml, mt = float(monitor.get("left", 0)), float(monitor.get("top", 0))
    mw, mh = float(monitor.get("width", 0)), float(monitor.get("height", 0))
    best = float("inf")
    for rx, ry, rw, rh in (
        (x, y, w, h),
        (x, y, w * dpr, h * dpr),
        (x * dpr, y * dpr, w * dpr, h * dpr),
    ):
        # Size mismatches weigh more than origin mismatches: origins are the
        # part mixed-DPI layouts disagree about.
        score = 2.0 * (abs(rw - mw) + abs(rh - mh)) + abs(rx - ml) + abs(ry - mt)
        best = min(best, score)
    return best


def match_monitor(screen: dict, monitors: list[dict]) -> dict | None:
    """The capture-space monitor that is the picker's Qt ``screen``."""
    physical = [m for m in (monitors[1:] if len(monitors) > 1 else monitors) if isinstance(m, dict)]
    if not physical:
        return None
    return min(physical, key=lambda mon: screen_monitor_score(screen, mon))


def fraction_to_bbox(monitor: dict, frac: FRect | list[float]) -> Rect:
    """Fractions of ``monitor`` → ``(left, top, width, height)`` in capture space."""
    ml, mt = int(monitor.get("left", 0)), int(monitor.get("top", 0))
    mw, mh = max(1, int(monitor.get("width", 1))), max(1, int(monitor.get("height", 1)))
    fx, fy, fw, fh = (max(0.0, min(1.0, float(v))) for v in frac)
    x0 = ml + round(fx * mw)
    y0 = mt + round(fy * mh)
    x1 = ml + round(min(1.0, fx + fw) * mw)
    y1 = mt + round(min(1.0, fy + fh) * mh)
    return (x0, y0, max(1, x1 - x0), max(1, y1 - y0))


def selection_to_bbox(selection: Selection, monitors: list[dict]) -> Rect | None:
    """The selection in capture coordinates, or ``None`` without monitors."""
    monitor = match_monitor(selection.screen, monitors)
    if monitor is None:
        return None
    return fraction_to_bbox(monitor, selection.rect)


def snap_rects_on_screen(
    screen: dict,
    size: tuple[float, float],
    monitors: list[dict],
    windows: list[list[int]],
    *,
    min_px: float,
) -> list[FRect]:
    """Capture-space window rects → the picker screen's logical pixels.

    ``size`` is the picker window's logical size. Rects are clipped to the
    screen; slivers under ``min_px`` and rects on other screens are dropped.
    Order (top-most first) is kept, so the first hit is the visible window.
    """
    return [
        rect
        for _index, rect in snap_targets_on_screen(screen, size, monitors, windows, min_px=min_px)
    ]


def snap_targets_on_screen(
    screen: dict,
    size: tuple[float, float],
    monitors: list[dict],
    windows: list[list[int]],
    *,
    min_px: float,
) -> list[tuple[int, FRect]]:
    """:func:`snap_rects_on_screen`, each rect with its index in ``windows``.

    The index ties a rect that survived the clipping back to what came with
    it (the window handle its elements are read from).
    """
    monitor = match_monitor(screen, monitors)
    if monitor is None:
        return []
    ml, mt = float(monitor.get("left", 0)), float(monitor.get("top", 0))
    mw = max(1.0, float(monitor.get("width", 1)))
    mh = max(1.0, float(monitor.get("height", 1)))
    width, height = float(size[0]), float(size[1])
    sx, sy = width / mw, height / mh
    out: list[tuple[int, FRect]] = []
    for index, rect in enumerate(windows):
        if not isinstance(rect, (list, tuple)) or len(rect) != 4:
            continue
        try:
            left, top, w, h = (float(v) for v in rect)
        except (TypeError, ValueError):  # a garbled rect is no snap target
            continue
        x0, y0 = max(0.0, (left - ml) * sx), max(0.0, (top - mt) * sy)
        x1 = min(width, (left + w - ml) * sx)
        y1 = min(height, (top + h - mt) * sy)
        if x1 - x0 >= min_px and y1 - y0 >= min_px:
            out.append((index, (x0, y0, x1 - x0, y1 - y0)))
    return out


def parse_selection(payload: dict[str, Any]) -> Selection | None:
    """A picker ``selection`` event → :class:`Selection` (``None`` = cancelled)."""
    if payload.get("cancelled"):
        return None
    screen, rect = payload.get("screen"), payload.get("rect")
    if not isinstance(screen, dict) or not isinstance(rect, list) or len(rect) != 4:
        return None
    try:
        info = {key: float(screen[key]) for key in ("x", "y", "w", "h", "dpr")}
        frac = tuple(max(0.0, min(1.0, float(v))) for v in rect)
    except (KeyError, TypeError, ValueError):  # a malformed selection is refused via None
        return None
    if frac[2] <= 0.0 or frac[3] <= 0.0:
        return None
    from dodaios.appshot.picker import ACTION_DONE, ACTIONS  # noqa: PLC0415

    action = payload.get("action")
    return Selection(
        screen=info,
        rect=frac,  # type: ignore[arg-type]
        action=action if action in ACTIONS else ACTION_DONE,
        markup=parse_markup(payload.get("markup")),
    )


# --------------------------------------------------------------------------
# The picker process
# --------------------------------------------------------------------------


def picker_capability() -> tuple[bool, str]:
    """Whether an area can be selected on this host, and why not."""
    try:
        from dodaios.platform.probes import display_present, is_wayland  # noqa: PLC0415

        if not display_present():
            return False, "there is no screen on this computer"
        if is_wayland():
            return False, "Wayland does not let apps draw a selection over other windows"
    except Exception:  # noqa: BLE001 - probes missing: let the picker itself decide
        log.debug("appshot: platform probes unavailable", exc_info=True)
    if importlib.util.find_spec("PySide6") is None:
        return False, "the selection overlay needs PySide6 (the [desktop] extra)"
    from dodaios.platform.qt_sidecar import missing_system_library  # noqa: PLC0415

    missing = missing_system_library()
    if missing:
        return False, missing
    return True, ""


def _is_cloaked(handle: int | None) -> bool:
    """Windows only: a DWM-cloaked window (hidden UWP shells) is no target."""
    if os.name != "nt" or not handle:
        return False
    try:
        import ctypes  # noqa: PLC0415
        from ctypes import wintypes  # noqa: PLC0415

        dwmapi = ctypes.WinDLL("dwmapi")
        cloaked = wintypes.DWORD(0)
        result = dwmapi.DwmGetWindowAttribute(
            wintypes.HWND(handle), 14, ctypes.byref(cloaked), ctypes.sizeof(cloaked)
        )
        return result == 0 and bool(cloaked.value)
    except Exception:  # noqa: BLE001 - unknown cloak state keeps the window
        return False


def snap_layout(*, skip_pid: int | None = None) -> dict[str, Any]:
    """Monitors and window rectangles for the picker's window snapping.

    Read BEFORE the picker exists, so its own overlay never becomes a target,
    or with ``skip_pid`` (the picker's process) once it is already up.
    Rects are in capture coordinates, top-most window first; an empty window
    list (Wayland, an unreadable desktop) only switches snapping off.
    ``handles`` runs alongside ``windows`` (``0`` = unknown): the picker reads
    the parts of the window under the pointer through them, to snap to a
    panel, list or button instead of the whole window.
    """
    from dodaios.screen_context.ports import make_display_enumerator  # noqa: PLC0415

    monitors = make_display_enumerator().monitors()
    windows: list[list[int]] = []
    handles: list[int] = []
    try:
        from dodaios.platform import window_state as ws  # noqa: PLC0415
        from dodaios.screen_context.ports import _input_space  # noqa: PLC0415

        with _input_space():
            for win in ws.list_windows():
                if getattr(win, "minimized", False) or _is_cloaked(getattr(win, "handle", None)):
                    continue
                if skip_pid and getattr(win, "pid", None) == skip_pid:
                    continue
                rect = ws.window_frame_rect(win) or ws.window_rect(win)
                if rect and rect[2] > 0 and rect[3] > 0:
                    handle = getattr(win, "handle", None)
                    windows.append([int(v) for v in rect])
                    handles.append(handle if isinstance(handle, int) else 0)
    except Exception:  # noqa: BLE001 - snapping is a convenience; dragging still works
        log.debug("appshot: window list for snapping unavailable", exc_info=True)
    return {"monitors": [dict(m) for m in monitors], "windows": windows, "handles": handles}


def _spawn(language: str = "en", *, resident: bool = False) -> subprocess.Popen[str]:
    from dodaios.core.process_utils import NO_WINDOW_CREATIONFLAGS  # noqa: PLC0415
    from dodaios.platform.qt_sidecar import stderr_sink  # noqa: PLC0415

    return subprocess.Popen(
        [sys.executable, "-m", "dodaios.appshot.picker", "--lang", language or "en"]
        + (["--resident"] if resident else []),
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=stderr_sink("appshot-picker"),
        text=True,
        encoding="utf-8",
        creationflags=NO_WINDOW_CREATIONFLAGS,
        env=os.environ.copy(),
    )


def _read_result(
    proc: subprocess.Popen[str], on_marking: Callable[[], None] | None = None,
    on_ready: Callable[[dict[str, Any]], None] | None = None,
    on_option: Callable[[dict[str, Any]], None] | None = None,
) -> dict[str, Any] | None:
    """Block until the picker reports its selection; ``None`` on EOF.

    ``on_marking`` runs (on this reader thread) once the user has marked
    something, so the picker's own Esc may close a text box from then on;
    ``on_option`` with each switch the user flips in the toolbar.
    """
    from dodaios.appshot import picker as wire  # noqa: PLC0415

    assert proc.stdout is not None
    for line in proc.stdout:
        payload = wire.decode(line)
        if payload is None:
            continue
        event = payload.get("event")
        if event == wire.EVENT_SELECTION:
            return payload
        if event == wire.EVENT_READY and on_ready is not None:
            on_ready(payload)
        if event == wire.EVENT_MARKING and on_marking is not None:
            with contextlib.suppress(Exception):  # a lost hint only keeps the global Esc
                on_marking()
        if event == wire.EVENT_OPTION and on_option is not None:
            with contextlib.suppress(Exception):  # a lost switch is only not remembered
                on_option(payload)
    return None


def _send_layout(proc: subprocess.Popen[str], layout: dict[str, Any]) -> None:
    from dodaios.appshot import picker as wire  # noqa: PLC0415

    with contextlib.suppress(Exception):  # a closed pipe means it already ended
        if proc.stdin is not None and proc.poll() is None:
            proc.stdin.write(wire.encode({"cmd": wire.CMD_LAYOUT, **layout}))
            proc.stdin.flush()


def _cancel(proc: subprocess.Popen[str]) -> None:
    from dodaios.appshot import picker as wire  # noqa: PLC0415

    with contextlib.suppress(Exception):  # a closed pipe means it already ended
        if proc.stdin is not None and proc.poll() is None:
            proc.stdin.write(wire.encode({"cmd": wire.CMD_CANCEL}))
            proc.stdin.flush()


def _reap(proc: subprocess.Popen[str]) -> int | None:
    try:
        proc.wait(timeout=_EXIT_GRACE_S)
    except subprocess.TimeoutExpired:
        log.info("appshot: area picker did not exit in time; stopping it")
        with contextlib.suppress(Exception):
            proc.kill()
            proc.wait(timeout=1.0)
    for pipe in (proc.stdin, proc.stdout):
        if pipe is not None:
            with contextlib.suppress(Exception):
                pipe.close()
    return proc.returncode


async def _escape_cancels(proc: subprocess.Popen[str]) -> None:
    """A global Escape also cancels: the picker may not get keyboard focus.

    Windows refuses to hand the foreground to a background process, so the
    picker's own Esc handler can stay deaf until the first click; the shared
    hotkey backend is not.
    """
    try:
        from dodaios.platform.probes import has_hotkey  # noqa: PLC0415

        if not has_hotkey():
            return
        from dodaios.trigger.hotkey import HotkeyTrigger  # noqa: PLC0415

        key = "escape" if sys.platform == "win32" else "esc"
        trigger = HotkeyTrigger({"appshot_region_cancel": [key]})
        async with trigger:
            async for name in trigger.events():
                if name == "appshot_region_cancel":
                    await asyncio.to_thread(_cancel, proc)
                    return
    except asyncio.CancelledError:
        raise
    except Exception:  # noqa: BLE001 - the picker's own Esc and right-click still cancel
        log.debug("appshot: global Escape for the area picker unavailable", exc_info=True)


def preview_capture_allowed() -> bool:
    """A picker helper may read its grant silently, but must never request one."""
    from dodaios.platform.screen_access import screen_recording_state, state_allows_capture

    return state_allows_capture(screen_recording_state(deep=False))


def grab_preview(screen: Any) -> Any | None:
    """Read a preview only with this helper's grant; no Qt import or prompt."""
    try:
        if not preview_capture_allowed():
            return None
        pixmap = screen.grabWindow(0)
        return None if pixmap.isNull() or pixmap.width() <= 0 else pixmap
    except Exception:  # noqa: BLE001 - the picker can dim the live desktop instead
        log.debug("appshot: preview unavailable; using a live overlay", exc_info=True)
        return None


async def pick_region(
    *,
    timeout_s: float = PICK_TIMEOUT_S,
    language: str = "en",
    keep_area: bool = False,
    trace_id: UUID | None = None,
) -> Selection | None:
    """Let the user drag a rectangle and mark it up. ``None`` = cancelled or timed out.

    ``language`` is the toolbar's tooltip language (``[ui].language``);
    ``keep_area`` keeps a released area to adjust it before it is taken
    (``[appshot].region_keep_area``), instead of taking it at once.
    Raises :class:`RegionUnavailable` when no picker can run on this host.
    """
    global _picking
    ok, reason = picker_capability()
    if not ok:
        raise RegionUnavailable(f"An area cannot be selected here: {reason}.")
    if _picking:
        raise RegionUnavailable("An area is already being selected. Finish or press Esc first.")
    _picking = True
    try:
        # This is the user gesture. Ask in the parent before even enumerating
        # windows or starting a helper that freezes the desktop (AP-35).
        from dodaios.platform.screen_access import require_screen_recording_async

        await require_screen_recording_async("appshot", trace_id=trace_id)
        token = _language.set(language or "en")
        keep_token = _keep_area.set(bool(keep_area))
        try:
            payload, code, timed_out = await _run_picker(timeout_s)
        finally:
            _keep_area.reset(keep_token)
            _language.reset(token)
    finally:
        _picking = False
    if payload is None and not timed_out and code not in (0, None):
        log.warning("appshot: area picker exited with code %s and no selection", code)
        raise RegionUnavailable(_exit_message(code))
    selection = parse_selection(payload) if payload is not None else None
    if selection is not None:
        await asyncio.sleep(_SETTLE_S)
    return selection


def _exit_message(code: int) -> str:
    from dodaios.appshot.picker import EXIT_NO_GUI  # noqa: PLC0415

    if code == EXIT_NO_GUI:
        return "An area cannot be selected here: the selection overlay found no usable screen."
    from dodaios.platform.qt_sidecar import crash_hint  # noqa: PLC0415

    return (
        f"The selection overlay stopped unexpectedly (exit code {code}). "
        f"Nothing was captured.{crash_hint(code, 'appshot-picker')}"
    )


async def _run_picker(timeout_s: float) -> tuple[dict[str, Any] | None, int | None, bool]:
    """Use the prepared runtime; only the explicit pick samples the desktop."""
    from dodaios.appshot.picker_host import get_picker_host

    try:
        return await get_picker_host().pick(
            timeout_s, _language.get(), keep_area=_keep_area.get()
        )
    except (OSError, TimeoutError) as exc:
        log.warning("appshot: area picker could not be started", exc_info=True)
        raise RegionUnavailable("The selection overlay could not be started.") from exc



__all__ = [
    "MIN_SELECTION_PX",
    "RegionUnavailable",
    "Selection",
    "fraction_to_bbox",
    "match_monitor",
    "parse_selection",
    "pick_region",
    "picker_capability",
    "screen_monitor_score",
    "selection_fractions",
    "selection_to_bbox",
    "snap_layout",
    "snap_rects_on_screen",
    "snap_targets_on_screen",
]