# Running GUI programs

https://virtx.sh/docs/guides/gui-programs/

Run the code editor VS Code in a sandbox on a screen in memory, watch and drive it over VNC, and take screenshots of it.

A sandbox has no screen of its own.
[`display`](https://virtx.sh/docs/tutorials/built-in-programs/) gives it one, where GUI programs run as on a desktop: a person watches and takes over from a VNC viewer, and code takes screenshots of it.
The image needs the GUI program.
This guide runs [VS Code](https://code.visualstudio.com/) as its example.

## Show a program on a screen

Give the sandbox `display` and the VNC port, 5900.
`display run -- <program>` shows the program on a screen and says `ready` once a viewer can connect; then connect one through a [port forwarder](https://virtx.sh/docs/tutorials/networks-ports/).

This builds an image with VS Code in it, as [Working with images](https://virtx.sh/docs/tutorials/images/) shows, and opens it on `/root`:

**Python**

```python
from virtx import PortForwarder, Recipe, Stdout

await client.build_image(
    Recipe("debian:bookworm-slim").run(
        # VS Code needs libx11-xcb1 to start, and its package does not install it
        "apt-get update && apt-get install -y --no-install-recommends ca-certificates curl libx11-xcb1 fonts-dejavu-core"
        " && curl -fsSL -o /tmp/code.deb https://update.code.visualstudio.com/latest/linux-deb-$(dpkg --print-architecture | sed s/amd64/x64/)/stable"
        " && apt-get install -y --no-install-recommends /tmp/code.deb"
    ),
    "vscode:latest"
).complete()

sandbox = await client.create_sandbox("vscode:latest", abins=["display"], ports=[5900])

async for entry in sandbox.exec(
    ["display", "run", "--password", "secret", "--",
     "code", "--wait", "--no-sandbox", "--user-data-dir", "/tmp/code", "/root"]
):
    if isinstance(entry.entry, Stdout) and entry.entry.line == "ready":
        break  # the screen keeps running

with await PortForwarder.open(sandbox, 5900) as forwarder:
    print(f"vnc://{forwarder.local}")
    input("Press Enter to stop watching")
```

**Node.js**

```js
const readline = require('node:readline/promises')
const { PortForwarder, Recipe } = require('@brekkylab/virtx')

await client.buildImage(
  new Recipe('debian:bookworm-slim').run(
    // VS Code needs libx11-xcb1 to start, and its package does not install it
    'apt-get update && apt-get install -y --no-install-recommends ca-certificates curl libx11-xcb1 fonts-dejavu-core' +
      ' && curl -fsSL -o /tmp/code.deb https://update.code.visualstudio.com/latest/linux-deb-$(dpkg --print-architecture | sed s/amd64/x64/)/stable' +
      ' && apt-get install -y --no-install-recommends /tmp/code.deb'
  ),
  'vscode:latest'
).complete()

const sandbox = await client.createSandbox('vscode:latest', { abins: ['display'], ports: [5900] })

for await (const entry of sandbox.exec(
  ['display', 'run', '--password', 'secret', '--',
   'code', '--wait', '--no-sandbox', '--user-data-dir', '/tmp/code', '/root']
)) {
  if (entry.stdout === 'ready') break // the screen keeps running
}

const forwarder = await PortForwarder.open(sandbox, 5900)
console.log(`vnc://${forwarder.local}`)
const prompt = readline.createInterface({ input: process.stdin, output: process.stdout })
await prompt.question('Press Enter to stop watching')
prompt.close()
forwarder.close()
```

**Rust**

```rust
client.build_image(
    Recipe::new("debian:bookworm-slim").step(Step::run(concat!(
        // VS Code needs libx11-xcb1 to start, and its package does not install it
        "apt-get update && apt-get install -y --no-install-recommends ca-certificates curl libx11-xcb1 fonts-dejavu-core",
        " && curl -fsSL -o /tmp/code.deb https://update.code.visualstudio.com/latest/linux-deb-$(dpkg --print-architecture | sed s/amd64/x64/)/stable",
        " && apt-get install -y --no-install-recommends /tmp/code.deb",
    ))),
    "vscode:latest"
).complete(false).await?;

let args = SandboxCreateArgs::new("vscode:latest").abin("display").port(5900);
let sandbox = client.create_sandbox(&args).await?;

let mut log = sandbox.exec(&SandboxExecArgs::new(
    ["display", "run", "--password", "secret", "--",
     "code", "--wait", "--no-sandbox", "--user-data-dir", "/tmp/code", "/root"]
)).log();
while let Some(entry) = log.next().await {
    if matches!(entry?.entry, SandboxJobEntry::Stdout(ref line) if line == "ready") {
        break; // the screen keeps running
    }
}

let forwarder = PortForwarder::open(&sandbox, 5900, PortSide::Sandbox, None).await?;
println!("vnc://{}", forwarder.local());
println!("Press Enter to stop watching");
std::io::stdin().read_line(&mut String::new())?;
```

VS Code runs as root here, which takes `--no-sandbox` and a `--user-data-dir`, and `--wait` keeps `display run` up while its window is.

<img src={vscode.src} width={vscode.width} height={vscode.height} alt="A VNC viewer showing the sandbox's screen, with VS Code open on a folder." />

In the viewer you see the program, and type and click in it as on your own desktop.
The screen stays up as long as the program runs.

> **Caution:**
> Without `--password`, any viewer that reaches the port is let in.

`display run --help` lists its options, such as the screen's size.

## Let an AI agent use the screen

An AI agent that works a GUI program needs to see the screen, with no person watching.
These commands are what it looks with:

```bash
display shot /tmp/screen.png   # what the screen shows now, as a PNG
display cursor                 # where the pointer is, and the cursor's name: 640 360 xterm
```

An agent learns them from `/abin/skills/display/SKILL.md`, and an [Ailoy](https://github.com/brekkylab/ailoy) agent is given it by itself.

## What does not run

Programs that need OpenGL from the X server (GLX) do not start on this screen.
