Metadata-Version: 2.1
Name: vfb
Version: 0.1.0
Summary: A virtual display with Xvfb's command line on Linux, macOS and Windows
Home-page: https://github.com/JWriter20/vfb
License: MIT
Keywords: xvfb,virtual-display,headless,display,monitor
Requires-Python: >=3.8
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Description-Content-Type: text/markdown

# vfb

One virtual display command for Linux, macOS and Windows, with Xvfb's command line. On macOS and Windows `vfb` adds a monitor that does not exist to the logged-in desktop and reports its number the way `Xvfb -displayfd` does; the system compositor draws it on the GPU like a real panel, with no screen plugged in. On Linux it runs Xvfb.

| | Linux | macOS | Windows |
|---|---|---|---|
| Display | an Xvfb server (Xvfb on PATH, or `VFB_XVFB`) | `CGVirtualDisplay` (private CoreGraphics API, macOS 12+) | a monitor of the [Virtual Display Driver](https://github.com/VirtualDrivers/Virtual-Display-Driver) (signed, MIT) |
| Display number | the X display number (`:N`) | the `CGDirectDisplayID` | the `\\.\DISPLAYn` number |
| Needs | Xvfb installed (`apt install xvfb`) | a logged-in GUI session (a plain ssh session works) | the driver installed, run inside an interactive session |
| Lives | as long as the process; vfb stops Xvfb when it stops, and the kernel stops Xvfb if vfb is killed | as long as the process | the monitor is attached while vfb runs and detached when it stops |

## Install

```
pip install vfb          # Python: vfb.path() is the binary; `vfb` is on PATH
npm install @jwriter20/vfb   # Node: require("@jwriter20/vfb").path(); `npx vfb`
```

Each carries the binary for its platform (macOS 12+ universal, Linux x86_64, arm64 and i686, Windows x64 and x86: every platform Camoufox ships). On Linux, install Xvfb too (`apt install xvfb`). On Windows, install the [Virtual Display Driver](https://github.com/VirtualDrivers/Virtual-Display-Driver) once, as administrator; `ci/install-vdd.ps1` does it on a machine nobody configured by hand.

## Usage

```
vfb [:N] [-displayfd FD] [-screen 0 WxH[xD]] [-refresh HZ]
    [-report geometry] [-eofexit] [-noparentwatch]
    macOS only:   [-scale 1|2] [-gamut p3|srgb] [-name NAME]
    Windows only: [-match ID]
vfb list [-match ID]        (Windows)
vfb detach N                (Windows)
```

- `-displayfd FD` writes the display number and a newline to FD once the display is up at the asked-for mode. Without it the number goes to stdout. On Windows only `1` (stdout) is supported, since a child inherits no other descriptor.
- `-report geometry` makes the line `N X Y W H SCALE`: where the display sits on the desktop (points on macOS, pixels on Windows) and its scale: the backing scale on macOS, the system's display scaling (effective DPI / 96) on Windows. A browser window that fills the display is W x H CSS pixels on macOS and W/SCALE x H/SCALE on Windows.
- `-eofexit` stops vfb when its stdin closes. This is the graceful stop on Windows, where a console-less child gets no signal; it lets vfb detach the monitor it attached. A launcher that holds vfb's stdin also stops vfb if the launcher dies.
- vfb exits with its parent unless `-noparentwatch` is given.
- Common Xvfb flags (`-ac`, `-nolisten tcp`, `-nocursor`, `-br`, `-extension X`, `+extension X`, `-fp X`, `-dpi X`) are accepted and ignored.

### Linux

vfb starts Xvfb with `-displayfd` and every Xvfb flag it was given (`-nolisten tcp`, `-ac`, `+extension GLX`, ...), and handles the options Xvfb lacks: `-report`, `-eofexit`, `-noparentwatch`, and `-refresh`, `-scale`, `-gamut`, `-name`, `-match`, which are ignored. With `-report geometry` the line is `N 0 0 W H 1`. Without `-screen` the screen is 1920x1080x24, the same default as on the other systems. vfb stops Xvfb with SIGTERM, so Xvfb removes its lock file and socket, and exits with Xvfb's status if Xvfb exits on its own.

### macOS

`-screen 0 1440x900 -scale 2` is a 1440x900-point HiDPI display backed by 2880x1800 pixels. The display gets the size in millimetres of a 218 ppi panel (2x) or a 109 ppi one (1x), and P3 primaries unless `-gamut srgb` is given.

### Windows

The driver's monitors are a fixed pool, set by `count` in `C:\VirtualDisplayDriver\vdd_settings.xml`. vfb claims a free one with a session-local mutex (`Local\vfb-DISPLAYn`), attaches it to the right of the rightmost monitor, sets the mode, and detaches it on exit. A mode has to be listed in the driver's `resolutions` first; vfb names the available ones when it is not. `vfb list` shows the pool. A killed vfb cannot detach its monitor; it leaves a marker in `%LOCALAPPDATA%\vfb`, `vfb list` shows that monitor as `stale`, and the next vfb to claim it detaches it when it stops. `vfb detach N` detaches one by hand.

Run vfb inside the interactive session whose desktop it extends (for automation, a scheduled task with `LogonType Interactive`); from a service or ssh session it finds no monitors.

## Build

```
macos/build.sh                  # universal binary, ad-hoc signed
cd linux && CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o vfb .
cd windows && GOOS=windows GOARCH=amd64 go build -trimpath -ldflags="-s -w" -o vfb.exe .
```

## Release

`.github/workflows/release.yml` builds the four binaries, packages them with `scripts/package.py` (reproducible wheels and npm tarballs, standard library only), and installs the packages on each OS. A tag `vX.Y.Z` also publishes them to PyPI and npm through trusted publishing.

## Tests

`tests/smoke.py SERVER` (Python standard library only) opens displays one at a time and two at once, checks through the OS itself that each is up at the asked-for mode, closes them by every stop path, and checks nothing is left: no process, no display, no lock or marker, no leaked memory (macOS `leaks`), no growth in ColorSync's device registry. CI runs it on Linux (x86_64 and 32-bit builds), macOS arm64 and x86_64, and Windows (x64 and 32-bit builds), where `ci/install-vdd.ps1` installs the driver first.

## macOS: display changes are spaced out

Every display added or removed makes WindowServer re-lay out every window and colorsync re-read its device registry. So vfb:

- makes one display change at a time per user (a lock in the user's temporary directory) and leaves at least 2 s between one change and the next;
- on exit, waits until the display has left WindowServer's online list before the next vfb may change anything;
- gives each display a stable identity. ColorSync keeps every display it has seen as a device for good, so a new serial per run grows that registry forever and makes every later change slower. vfb's serial comes from the lowest slot no running vfb holds, so the registry holds one device per vfb that ever ran at the same time. There are 256 slots; with all of them held, vfb refuses to start (exit 1) rather than make a one-off identity that only root could remove.
- sets the mode it was asked for. macOS remembers the last mode used for each display identity (including 1x versus HiDPI), so a slot that last ran at another size comes up in that size. vfb switches it once and reports the display only when both the size in points and the size in pixels match.
