# Linux from a console

> On a Linux console with no desktop, rmp builds the game for DRM/KMS by itself: what decides it, the folders it builds in, what to install, the screen and the keyboard from /dev, sudo and the groups, and the terminal it gives back.

Source: https://omardev29.github.io/rmp-docs/manual/platforms/linux-console.html

A Linux machine with no desktop — a Raspberry Pi, a server, a laptop started to a text login — has no X11 and no Wayland for a window to open on, and often none of their headers to build against. On such a console `rmp` builds the game for DRM/KMS instead: raylib's backend that draws straight to the screen through the kernel. There is nothing to configure. `rmp run`, `rmp build`, `rmp test` and `rmp pack` decide by themselves, and say so.

## What decides it

Once per command, before anything is configured, `rmp` asks these in order, and the first that answers decides:

1. `RMP_DRM=1` builds for DRM/KMS and `RMP_DRM=0` for the desktop, whatever else is true. Empty is the same as unset. Any other value is refused rather than read as one of the two, and so is `RMP_DRM=1` anywhere but Linux.
2. Not Linux: the desktop. DRM/KMS is a Linux backend.
3. `DISPLAY` or `WAYLAND_DISPLAY` is set: the desktop. There is one to open a window on.
4. Logged in over SSH — `SSH_CONNECTION`, `SSH_CLIENT` or `SSH_TTY` is set — with no display: the desktop, and a note saying so.
5. `XDG_SESSION_TYPE=tty`, which logind sets for a text login: DRM/KMS.
6. The terminal `rmp` runs in is a virtual console, `/dev/tty1`, `/dev/tty2` and so on: DRM/KMS, for a console that logind did not set up. The terminal is stdin's or, when stdin is not one, the controlling terminal's, so `rmp run <
/dev/null` typed on a console still counts. A terminal window, SSH and tmux are `/dev/pts/…` and a serial line is `/dev/ttyS0`: none of them is a virtual console.
7. Anything else: the desktop. With a terminal and no display, a note says why.

The order is there because a console's own word for itself is wrong twice. `startx` keeps the console's `XDG_SESSION_TYPE=tty` inside the desktop it starts, so a display is asked about first. And logind calls a login over SSH a `tty` session too, while the screen of a machine you are logged into from elsewhere is not yours to take — so SSH comes before it. From SSH, `RMP_DRM=1` asks for the machine's own screen anyway:

```shell
RMP_DRM=1 rmp run
```

When it builds for DRM/KMS, `rmp` says why on a line of its own: `XDG_SESSION_TYPE=tty: building for DRM/KMS, in build/debug-drm and build/release-drm
(RMP_DRM=0 for the desktop)`. The framework's tests walk every combination of them.

## Its own two folders

On a console the debug build goes to `build/debug-drm` and the release to `build/release-drm`, never to `build/debug` and `build/release`. They are presets of their own, `debug-drm` and `release-drm`: the debug and release presets with `RMP_TARGET_DRM` on, which picks raylib's DRM platform. A console and a desktop on the same machine never share a cache, so going from one to the other rebuilds nothing.

```shell
rmp run              # builds build/debug-drm/my_game, checks /dev, starts it
rmp build release    # builds build/release-drm/my_game
```

Without `rmp`, the same presets by hand:

```shell
cmake --preset debug-drm
cmake --build --preset debug-drm
```

CMake never looks for X11 in either. A test configures both presets with X11 made impossible to find, and the desktop's preset beside them, which has to fail that way for the test to count. A debug build on a console is sanitized the way any debug build is, as [`[dev] sanitize`](https://omardev29.github.io/rmp-docs/manual/basics/configuration.html#sanitizers) says.

## What to install

A console build needs the development files of libdrm, GBM, EGL and OpenGL ES — what raylib's DRM backend draws with — and none of X11's. [The installer](https://omardev29.github.io/rmp-docs/getting-started/install.html) decides the way `rmp` does, and on a console it looks for those instead of the X11 headers and lists what is missing with the command for this system:

| System | Packages |
|---|---|
| `apt` | `libdrm-dev libgbm-dev libegl1-mesa-dev libgles2-mesa-dev` |
| `dnf` | `libdrm-devel mesa-libgbm-devel mesa-libEGL-devel mesa-libGLES-devel` |
| `pacman` | `libdrm mesa libglvnd` |
| `zypper` | `libdrm-devel libgbm-devel Mesa-libEGL-devel Mesa-libGLESv2-devel` |
| `apk` (Alpine) | `libdrm-dev mesa-dev linux-headers` |

```shell
sudo apt install libdrm-dev libgbm-dev libegl1-mesa-dev libgles2-mesa-dev
```

It says what it decided, too: `This is a Linux console with no desktop: rmp builds games for
DRM/KMS here,` and how to build for a desktop instead. As everywhere, it prints the command and never runs it.

## The screen and the keyboard

A DRM/KMS game opens the screen through `/dev/dri/card*`, which it has to be able to read and write and be the *DRM master* of: the kernel makes the first program that opens a card nobody else draws on its master. It reads the keyboard, the mouse and the gamepads from `/dev/input/event*`. Those belong to root and to a group, `video` and `input`, and logind lets the person at the console open the card but not read the keyboard. A game that cannot reach them finds out after the screen has gone black, so `rmp run` asks first, between the build and the start:

- **No card.** The cards that count are the ones raylib tries, in its order: `/dev/dri/by-path/platform-gpu-card`, then `card1`, `card0` and `card2` — `card1` before `card0` because on a Raspberry Pi `card0` is the 3D engine and `card1` the display. It takes the first of them it can open that has a display — that answers `drmModeGetResources()`, as raylib asks — and says which one the game draws on. With none of them, `rmp run` refuses: no display driver with modesetting has loaded, as in a virtual machine without a display device or a container started without `--device /dev/dri`; and with cards that answer no display — a GPU with no display engine, or a driver loaded with `nomodeset` — it says so, and `sudo` does not help.
- **A card something else holds.** When another program is the DRM master of the card the game would take — a desktop on another virtual console, most likely — `rmp run` refuses: `FAIL: something else draws on the screen: it is the DRM master of …, and a game can only take
a screen nobody holds.` Log out of that desktop or stop it. sudo is not offered, because root cannot take a screen from its master either.
- **A card or an input device you cannot open.** `rmp run` refuses with `the game could not reach the screen and the keyboard from here (DRM/KMS):`, lists each one with its owner and mode the way `ls -l` shows them, and gives the two ways out below.
- **No input device at all.** A note, and the game starts: it reads the keys of the terminal it runs on, and no mouse or gamepad.

The smoke stage of `rmp test` boots the game on the console's screen, and asks the same before it does.

### Two ways out: sudo once, or the groups

Start the game as root this once, or put yourself in the `video` and `input` groups for good. A group you are added to counts from your next login, so log out and in again, and from then on `rmp run` starts the game as you, without root:

```shell
rmp run sudo                          # this once: the game as root, the build as you
sudo usermod -aG video,input "$USER"  # for good, then log out and in again
```

`rmp
run sudo` builds as you, says the exact command, and starts only the game with `sudo --`: nothing `rmp` writes is ever root's. It asks the same questions first, but the last: a card that is not there, or one that something else holds, is refused before sudo asks for a password, because sudo fixes neither — what only root may open is what sudo is for.

On a desktop it is refused before anything is built: a window needs no root. When `rmp` is root already it starts the game as it is, and on a machine with no `sudo` it refuses and prints the game's path, to start as root some other way. `rmp` never runs sudo unless you typed it.

### Never sudo rmp

`rmp` never builds as root. Under sudo — root, with `SUDO_USER` naming somebody else — every command but `rmp help` and `rmp clean` is refused before anything runs — `FAIL: rmp is running as root through sudo, and would build as root: …` — because `build/`, the files generated from the `.toml`, the sources and `.git` would be root's, and the next command you ran as yourself could not write them. `sudo rmp clean` is how what an earlier root left goes away. Root with nobody behind it — a container, CI — has no other user to be, and builds.

## Saves under sudo

A game started with sudo is root, and saves as root. With sudo's defaults its home is root's, so [`rmp::save`](https://omardev29.github.io/rmp-docs/manual/modules/saving.html) writes to `/root/.local/share/<name>/` and not to your folder; a sudoers that keeps your `HOME` gives your own folder, written as root, and `[save] portable = true` writes root's files next to the game. `rmp run
sudo` says so before it starts the game.

The game says it as well, once, as it starts — `WARNING: CONSOLE: started with sudo by …` — with your name, the folder it really saves in, read from the environment sudo left it, and the groups. In the `video` and `input` groups the game runs as you and saves where it always does.

## The terminal comes back

raylib's DRM backend reads the keyboard from the terminal the game starts on, and changes it: no echo, no line editing, no Ctrl+C, the terminal made non-blocking — a setting your shell shares — and a virtual console's keyboard put in another mode. It puts them back only from an `atexit` handler, which a crash, `abort()`, a sanitizer's report and a kill never run, and the shell after them would type blind, with no Ctrl+C.

So the framework puts it back however the game ends. Before the window opens it remembers the terminal, and puts it back on every fatal signal — then hands the signal on to whoever had it, so a crash is still a crash: a core dump, AddressSanitizer's report, the status your shell sees — on the sanitizers' way out, and at exit. Its handler runs on a stack set apart for signals — AddressSanitizer's, or one of its own where nothing set one up — because a stack overflow leaves none to run on.

SIGKILL is the one ending nothing inside a process can catch, so `rmp run` also saves the terminal before the game and restores it after, and says when it had to: `the game left the
terminal changed -- … -- and rmp put it back`. The screen needs none of this: the kernel gives the console its screen back when the game's last handle on the card closes, however the process ended.

The framework's tests make a small program die each of those ways on a pseudo-terminal — an exit, a segmentation fault, `abort()`, SIGTERM, a trap, a division by zero, a stack overflow, an AddressSanitizer report — with the guard, and the terminal has to come back each time, with the program still ending the way it would have. Without the guard, a crash, `abort()`, SIGTERM, a stack overflow and the sanitizer's report have to leave it changed, or the test would prove nothing. SIGKILL leaves it changed with the guard too, which is why `rmp run` mends it from outside.

## While it runs

Ctrl+C does nothing while the game runs: the terminal's signals are off. The game quits from its own menu, and Escape is the game's key, not a way out — see [the Escape key](https://omardev29.github.io/rmp-docs/manual/core/app.html#the-escape-key).

Ctrl+Alt+F1, F2 and the others still switch to another virtual console, but the screen may go on showing the game: the kernel does not take a screen from its DRM master. To end a game that is stuck, log in on another console — without seeing what you type, if it comes to that — or over SSH, and end it from there.

## When it does not work

| What you see | What it means |
|---|---|
| `rmp run` says something else draws on the screen | A desktop, or another program on the card, holds it. Log out of the desktop or stop the program; sudo does not help. |
| `rmp run` says no card raylib tries has a display | A GPU with no display engine, or a driver loaded with `nomodeset`. `sudo` does not help: give the machine a display driver with modesetting. |
| `rmp run` says there is no card | No display driver with modesetting has loaded. In a container, pass the card in with `--device /dev/dri`. |
| `rmp run` lists nodes under `/dev/dri` or `/dev/input` | You cannot open them. `rmp run sudo` this once, or the `video` and `input` groups and a new login. |
| Added to the groups, and still refused | Groups are read at login: log out and in again. |
| The game reads the keyboard, and no mouse or gamepad | There is no `/dev/input/event*`; the game reads the terminal's keys. |
| Over SSH, it built a game that opens a window | SSH counts as the desktop. `RMP_DRM=1 rmp run` builds for the machine's own screen. |
| `sudo rmp …` is refused | Run `rmp` as yourself; `rmp run sudo` gives root to the game alone. |
| The screen stays black, and raylib's log says it failed to initialize the EGL device | Mesa could not draw on that card. CI's virtual display has no GPU behind it, and Mesa draws there only in software, with `LIBGL_ALWAYS_SOFTWARE=1` in the environment; a display with no GPU Mesa can drive needs the same. |

## What CI proves

The job that boots `linux-x64-glibc-drm` on a virtual display — vkms, a DRM device the kernel provides with no hardware behind it — also runs `rmp` the way a console does: in the build image, with the card passed in, no display, `XDG_SESSION_TYPE=tty` and `./rmp run`. It has to decide DRM/KMS by itself, build `build/debug-drm` without touching `build/debug`, link libdrm and no X11 or Wayland, check `/dev` before it starts the game, and boot it, draw and exit cleanly with no sanitizer report. It runs in a game's CI too, when the game ships `linux-x64-glibc-drm`; only the framework's build is held to having been sanitized there, because a game's `[dev] sanitize` is its own.

What it cannot prove follows from where it runs: the container is root, so the groups and sudo are not tried, and it is given no `/dev/input`, so no real keyboard either.
