Metadata-Version: 2.1
Name: vfb
Version: 0.2.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, which `vfb setup` installs (npm's install runs it); 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, vfb needs the [Virtual Display Driver](https://github.com/VirtualDrivers/Virtual-Display-Driver); `vfb setup` installs it, and `npm install` runs that for you (Windows asks for an administrator once; `VFB_NO_SETUP=1` skips it). With pip, or if that was skipped, the first `vfb` that needs the driver runs it, or run `vfb setup` yourself.

## Usage

```
vfb [:N] [-displayfd FD] [-screen S WxH[xD]]... [-refresh HZ] [-dpi DPI]
    [-report geometry] [-eofexit] [-noparentwatch]
    [any other Xvfb option]
    macOS:   [-scale 1|2] [-gamut p3|srgb] [-name NAME]
    Windows: [-scale S] [-match ID]
vfb setup [-count N] [-mode WxH[@HZ]]...   (Windows)
vfb list [-match ID]                       (Windows)
vfb detach N                               (Windows)
```

- Every option of Xvfb's command line is accepted with its values, so a command line written for Xvfb runs unchanged (`tests/xvfbopts.py` checks all 91, from one option table the three builds share). The ones a virtual monitor can honour take effect; the rest are accepted and ignored. `-I` ends the options, as in Xvfb.
- `-screen` can be given more than once, one display per screen, all held by the one vfb; the first line of output names each display number, in screen order. To give displays different refresh rates or scales, run one vfb per display: any number can run at once.

- `-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.
- `-dpi` is the panel's pixels per inch on macOS (its size in millimetres follows) and a scale of DPI / 96 on Windows.

### 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), or of `-dpi`, and P3 primaries unless `-gamut srgb` is given. A depth of 24 (the default) is an SDR display; 30 is an HDR one, which is what a Mac's 30 means to a browser: Chrome reports `screen.colorDepth` 30, and matches `(dynamic-range: high)`, only on an HDR display, while Firefox reports 30 on either. (Virtual displays always scan out 8 bits per channel; HDR is what changes.) Apple silicon Macs make the HDR display: checked on macOS 15, 26 and 27. The Intel Mac tested (a macOS 15 CI VM) never does, so there vfb exits with an error rather than give a display that reports 24. Other depths are an error. With several `-screen`s vfb runs one child process per display, since a process holds one virtual display reliably, and stops them all together.

### 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 scale (`-scale` or `-dpi`, as Windows' display scaling), and detaches it on exit. `vfb list` shows the pool.

`vfb setup` downloads the driver release vfb was tested with (pinned by SHA-256), installs it, and writes the monitor count and mode list into its settings, keeping the backup of the old file next to it. The driver fails every monitor once its settings come to more than 100 modes, counting every size at its own rate and again at each global refresh rate, so setup removes the global rates and lists each mode once: the sizes real devices report most (Windows, macOS and Linux), at 60 Hz, plus the common high-refresh monitors (1920x1080 and 2560x1440 at 75 to 240 Hz, 3440x1440, 3840x2160 and 1920x1200 at 120 and 144). Your own entries in the file stay. A size or rate not in the list makes vfb run setup again with it added (`vfb setup -mode WxH@HZ` adds one ahead of time); 8 places are kept free for those, and the entries setup added are listed in `vfb_modes.txt` beside the settings, so it knows which it may drop. Setup asks for an administrator once, and restarts the driver only when the settings changed.

After the driver restarts, Windows can bring its monitors up cloned onto your own display, or switched off; vfb finds them in that state, separates them from your display, and leaves them detached. 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 `vfb setup` installs the driver first. CI also runs `tests/xvfbopts.py` on each.

## 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.
