# The app on each system

The machine is reached through Praecise Control, one small program per system. It is
an app a person can see, in the menu bar on a Mac and in the corner of the taskbar on
Windows and Linux. Its menu is where a person sees and manages everything it holds:

| In the menu | What it does |
|---|---|
| Agents | How many programs are connected, the app each is using and since when. Pause refuses every request until Resume. Disconnect All closes them from this end |
| Stored | How many recordings are kept and how much room they take of what is allowed. Show Recordings opens the folder. Delete All Recordings asks first |
| Cache | How many things are remembered from windows, which is how a reference still means the same thing. Clear Cache forgets them; an agent reads again |
| Permissions (Mac) | Whether Accessibility and Screen Recording are allowed, and the way to allow them |
| Start when I sign in (Windows) | Whether the app starts with the session |

The menu is the system's own on each: the menu bar's on a Mac, with named sections and
symbols; Windows' own menu and question box, which each version of Windows draws its
own way; and on Linux the menu the desktop draws for a tray icon, with nothing drawn by
a toolkit of the helper's. So it looks as that version of the system does, without
the helper deciding how.

What the menu shows a helper also answers (`storage`, `sessions`, `clear`), for a
person's own programs. An agent is not offered these.

A harness does not start the app. It starts a relay, which passes what it is asked to
the app and the app's answers back:

```ts
computerControl({
  apps: ["TextEdit", "Numbers"],
  helper: ["/Applications/Praecise Control.app/Contents/MacOS/praecise-control", "connect"],
});
```

| | The app | The relay a harness starts |
|---|---|---|
| macOS | `Praecise Control.app`, built by `helpers/macos/build-app.sh` | `Praecise Control.app/Contents/MacOS/praecise-control connect` |
| Windows | `praecise-control.exe`, built by `helpers\\windows\\build.cmd` | `praecise-computer-helper.exe connect` |
| Linux | `helpers/linux/praecise-computer-helper --app` | `praecise-computer-helper connect` |

The app answers on a socket or pipe that only the signed-in person's programs can
open. On a Mac and on Linux the relay starts the app if it is not running. On Windows
the app has to be running in the signed-in session already, which its "Start when I
sign in" menu item arranges; a relay started over SSH or by a service then reaches it.

It keeps nothing on disk but that socket. Pictures are made when asked for and handed
back; none is stored.

[PROTOCOL.md](PROTOCOL.md) is what a helper answers. Each program also still runs by
itself, answering on its own input and output, which is what `praecise-computer-helper`
with no arguments does.

Each helper was tried against a small real app with a field, a button that saves and
then goes, a line of text and a checkbox (`tests/fixtures`), and what each can and
cannot do is below.

| | macOS | Linux | Windows |
|---|---|---|---|
| Read a window as elements | Accessibility API | AT-SPI, on X11 and Wayland | UI Automation |
| Press and fill by reference, without the pointer | yes | yes | yes |
| Picture of a window | ScreenCaptureKit | X11 only | PrintWindow, also behind another window |
| Pointer and keys by position | posted to the app's process | X11, and only in a session of the helper's own | posted to the window under the position |
| Chords | not acted on by an app that is not in front | yes, in its own session | refused, except shift with a character |
| Menus | listed beside the window, read by reference | in the window | in the window |

## macOS

[helpers/macos](helpers/macos/) is one Swift file, for macOS 14 and later.

```sh
SIGN="Developer ID Application: Name (TEAM)" NOTARY_PROFILE=profile \\
  computer-control/helpers/macos/build-app.sh /Applications
```

It builds for both kinds of Mac, in Swift 6, with one compiler job each at the lowest
priority. It
is distributed outside the Mac App Store, signed with a Developer ID and notarized: an
app that works other apps through the Accessibility API cannot be sandboxed. It needs
Accessibility and Screen Recording, which the person allows from its menu. They are
granted to the signed app under its own name, so they survive an update. `doctor`
says which is missing.

Tried against TextEdit kept behind another app: reading, filling, pressing a button,
typing, single keys, a picture and a zoom all worked, and nothing was brought to the
front. What did not: a Command chord, and Select All pressed in the Edit menu by
reference. Both act on whatever has the keyboard, and an app that is not in front has
nothing that does. Replace what a field holds with `fill`. Where a piece of work needs
such commands, run the apps in a second user session that is the agent's own.

## Linux

[helpers/linux](helpers/linux/) is one Python file. It needs `python3-gi` and
`gir1.2-atspi-2.0`, and `gir1.2-gtk-3.0` for pictures and held keys on X11. It has to be
started inside the desktop session, where the accessibility bus is.

Elements, pressing and filling work the same on X11 and Wayland. A pointer and keys by
position on X11 can only be the one pointer and keyboard the session has, so they are
refused unless the helper is started with `--own-session`, which says no person is
working in that session. On Wayland they need the RemoteDesktop portal, and a picture
needs the ScreenCast portal; neither is used yet, and both are refused there.

Tried in a session of its own (Xvfb, a GTK 3 app) through the whole environment, with
`tests/computer-control-real.test.ts`.

## Windows

[helpers/windows](helpers/windows/) is one C# file, built with the compiler that is part
of Windows. Nothing is installed.

```
computer-control\helpers\windows\build.cmd C:\tools\praecise-computer-helper.exe
```

The app has to run in the session of whoever is signed in. A helper started by itself
over SSH or by a service can see no window, and says so; started with `connect` it
reaches the app that can. No request can hold the app: one an app does not answer in
twenty seconds is given up on and said to be. Only apps with a window of their own are listed;
an app from the Store that is shown inside another process's frame is not.

Tried against a Windows Forms app in the signed-in session with
`tests/fixtures/windows-check.ps1`: 28 checks.
