<?xml version="1.0" encoding="UTF-8"?>
<!-- **************************************************************************
.... For copyright and licensing terms, see the file named COPYING.
.... **************************************************************************
.-->
<?xml-stylesheet href="docbook-xml.css" type="text/css"?>

<refentry id="user-vt">

<refmeta xmlns:xi="http://www.w3.org/2001/XInclude">
<refentrytitle>user-vt</refentrytitle>
<manvolnum>5</manvolnum>
<refmiscinfo class="manual">file formats</refmiscinfo>
<refmiscinfo class="source">nosh</refmiscinfo>
<xi:include href="version.xml" />
</refmeta>

<refnamediv>
<refname>user-vt</refname>
<refpurpose>user-space virtual terminals</refpurpose>
</refnamediv>

<refsection><title>Description</title>

<para>
User-space virtual terminals comprise a set of normal and special files in an enclosing <filename><replaceable>directory</replaceable>/</filename>.
</para>

<para>
Conventionally, <filename><replaceable>directory</replaceable>/</filename> is a subdirectory of <filename>/run/dev/</filename>.
This convention is understood by the <citerefentry><refentrytitle>console-input-method-control</refentrytitle><manvolnum>1</manvolnum></citerefentry>, <citerefentry><refentrytitle>console-multiplexor-control</refentrytitle><manvolnum>1</manvolnum></citerefentry>, and <citerefentry><refentrytitle>vc-get-terminal</refentrytitle><manvolnum>1</manvolnum></citerefentry> commands when it comes to finding user-space virtual terminals from their basenames, and by <citerefentry><refentrytitle>login-process</refentrytitle><manvolnum>1</manvolnum></citerefentry> when it comes to constructing fake terminal IDs for compatibility with obsolete systems.
</para>

<para>
See <ulink url="user-virtual-terminals.html">the <citetitle>user-space virtual terminals</citetitle> section of the nosh Guide</ulink> for how various commands operate in tandem to run user-space VTs.
</para>

</refsection>

<refsection><title>Files</title>

<para>
The files used are as follows:
</para>

<variablelist>
<varlistentry>
<term><filename><replaceable>directory</replaceable>/tty</filename></term>
<listitem><para>
A stable and predictable name for the front end of the pseudo-terminal device.
If <filename><replaceable>directory</replaceable>/</filename> is <filename>/run/dev/vc1</filename>, for example, then the stable name for the terminal, for use in login services, will be <filename>/run/dev/vc1/tty</filename>.
This is a link to the device, if possible, or a symbolic link if not.
</para><note>
It is usually not.
On Linux, the pseudo-terminal devices are on a filesystem of their own, which would make a link a cross-filesystem link; and on BSDs the <code>devfs</code> driver disallows the creation of directories under <filename>/dev</filename> forcing <filename><replaceable>directory</replaceable>/</filename> to be on another filesystem anyway.
</note></listitem>
</varlistentry>
<varlistentry>
<term><filename><replaceable>directory</replaceable>/input</filename></term>
<listitem><para>
The input FIFO, through which realizer processes send keyboard and mouse events.
Events are in a uniform packet format.
</para><note>
Because realizers and other tools connect to and disconnect from the FIFO arbitrarily during the lifetime of a user-space VT, readers must ensure that they avoid the hangup signal behaviour that accompanies writers closing a FIFO.
</note></listitem>
</varlistentry>
<varlistentry>
<term><filename><replaceable>directory</replaceable>/display</filename></term>
<listitem><para>
The UTF-32, 24-bit colour, display buffer that output realizers realize onto some kind of output medium.
</para></listitem>
</varlistentry>
<varlistentry>
<term><filename><replaceable>directory</replaceable>/vcsa</filename></term>
<listitem><para>
An 8-bit character set, 3-bit colour, display buffer that is compatible with the Linux vcsa devices for kernel virtual terminals.
This does not necessarily exist.
</para></listitem>
</varlistentry>
<varlistentry>
<term><filename><replaceable>directory</replaceable>/lock</filename></term>
<listitem><para>
A lock file of the form used by the <citerefentry><refentrytitle>setlock</refentrytitle><manvolnum>1</manvolnum></citerefentry> command.
This is used by <citerefentry><refentrytitle>console-terminal-emulator</refentrytitle><manvolnum>1</manvolnum></citerefentry>, <citerefentry><refentrytitle>console-input-method</refentrytitle><manvolnum>1</manvolnum></citerefentry>, and <citerefentry><refentrytitle>console-multiplexor</refentrytitle><manvolnum>1</manvolnum></citerefentry> to ensure exclusive access.
</para></listitem>
</varlistentry>
</variablelist>

</refsection>

<refsection><title>vcsa buffer</title>

<para>
This file is structured as per the Linux character device file of that name, as a 4-byte header giving size and cursor position information followed by a series of 2-byte character and attribute pairs in IBM PC CGA format.
It is implemented mainly as a compatibility mechanism for the benefits of terminal realizing softwares that expect Linux vcsa devices, such as screen readers for the blind or partially sighted.
</para>

<para>
The vcsa screen buffer is always ISO 8859-1; with Unicode code points outwith that range converted to code point 255.
To handle a wider range of characters, terminal realizing softwares should use the Unicode screen buffer.
Code points in the ranges 0x00 to 0x1F and 0x80 to 0x9F may appear, and terminal realizing softwares are expected to render these with some form of ordinary printing graphic.
</para>

<para>
The 24-bit RGB foreground and background colour values for character cells are mapped to 3-bit CGA by comparing the relative intensities of red, green, and blue.
The "bright foreground" and "bright background" CGA attribute bits are taken from the boldface and blink terminal attributes, respectively.
No attempt is made to provide MDA or VGA in monochrome mode attributes, such as underline.
To handle the full range of attributes, terminal realizing softwares should use the Unicode screen buffer.
</para>

</refsection>

<refsection><title>Unicode buffer</title>

<para>
This file begins with a 16-byte header:
</para>
<orderedlist>
<listitem><para>4-byte UCS-4 Byte Order Mark in host byte order.</para></listitem>
<listitem><para>2-byte width in host byte order.</para></listitem>
<listitem><para>2-byte height in host byte order.</para></listitem>
<listitem><para>2-byte cursor X position in host byte order.</para></listitem>
<listitem><para>2-byte cursor Y position in host byte order.</para></listitem>
<listitem><para>cursor glyph type byte (upper nybble reserved).</para></listitem>
<listitem><para>cursor attributes byte (upper nybble reserved).</para></listitem>
<listitem><para>screen flags and pointer attributes byte, upper and lower nybble.</para></listitem>
<listitem><para>Reserved byte.</para></listitem>
</orderedlist>
<para>
That is followed by a series of 16-byte records, one per character cell, containing:
</para>
<orderedlist>
<listitem><para>Foreground alpha value byte.</para></listitem>
<listitem><para>Foreground red value byte.</para></listitem>
<listitem><para>Foreground green value byte.</para></listitem>
<listitem><para>Foreground blue value byte.</para></listitem>
<listitem><para>Background alpha value byte.</para></listitem>
<listitem><para>Background red value byte.</para></listitem>
<listitem><para>Background green value byte.</para></listitem>
<listitem><para>Background blue value byte.</para></listitem>
<listitem><para>4-byte UCS-4 value in host byte order.</para></listitem>
<listitem><para>2-byte attributes.</para></listitem>
<listitem><para>Reserved bytes.</para></listitem>
</orderedlist>

<para>
Unassigned code points, reserved code points, control code points, combining code points, and zero-width code points may appear, and terminal realizing softwares are expected to render these with some form of ordinary printing graphic.
For forwards and backwards compatibility, reserved bits in records should be written as zeroes, ignored when read, and preserved when copied.
</para>

</refsection>

<refsection><title>FIFO input protocol</title>

<para>
The input FIFO receives a sequence of 4-byte messages.
To avoid message tearing, realizers must ensure that they do not write messages using multiple system calls.
A message is a 32-bit word in host byte order.
The most significant byte denotes the message type and the interpretation of the remainder of the message.
</para>

<variablelist>
<varlistentry>
<term><code>0x00nnnnnn</code></term>
<listitem><para>A null message.  This must be ignored.</para></listitem>
</varlistentry>
<varlistentry>
<term><code>0x01cccccc</code></term>
<listitem> <para>
Unicode character.
The UCS-4 code point is <code>cccccc</code>.
</para></listitem>
</varlistentry>
<varlistentry>
<term><code>0x02kkkkmm</code></term>
<listitem><para>
A system keypad key.
The key number is <code>kkkk</code> and <code>mm</code> is a set of bitflags indicating the current state of modifier keys.
</para></listitem>
</varlistentry>
<varlistentry>
<term><code>0x04kkkkmm</code></term>
<listitem><para>
The absolute horizontal (x axis) position of the mouse.
The column number is <code>kkkk</code> and <code>mm</code> is a set of bitflags indicating the current state of modifier keys.
</para></listitem>
</varlistentry>
<varlistentry>
<term><code>0x05kkkkmm</code></term>
<listitem><para>
The absolute vertical (y axis) position of the mouse.
The row number is <code>kkkk</code> and <code>mm</code> is a set of bitflags indicating the current state of modifier keys.
</para></listitem>
</varlistentry>
<varlistentry>
<term><code>0x06kknnmm</code></term>
<listitem><para>
The absolute depth (z axis) position of the mouse.
The row number is <code>kkkk</code> and <code>mm</code> is a set of bitflags indicating the current state of modifier keys.
</para></listitem>
</varlistentry>
<varlistentry>
<term><code>0x07kkbbmm</code></term>
<listitem><para>
A mouse button.
The button number is <code>kk</code>, its state is <code>bb</code>, and <code>mm</code> is a set of bitflags indicating the current state of modifier keys.
The well-known buttons are numbered in DEC VT order: left, middle, right, side.
</para></listitem>
</varlistentry>
<varlistentry>
<term><code>0x08kknnmm</code></term>
<listitem><para>
A mouse wheel motion.
The wheel number is <code>kk</code>, the (signed) amount by which the wheel is scrolled is <code>nn</code>, and <code>mm</code> is a set of bitflags indicating the current state of modifier keys.
</para></listitem>
</varlistentry>
<varlistentry>
<term><code>0x09cccccc</code></term>
<listitem> <para>
Pasted Unicode character.
The UCS-4 code point is <code>cccccc</code>.
</para></listitem>
</varlistentry>
<varlistentry>
<term><code>0x0Annnnmm</code></term>
<listitem><para>
A session switch request.
The session number is <code>nnnn</code> and <code>mm</code> is a set of bitflags indicating the current state of modifier keys.
Sessions are numbered from 0, as per the FreeBSD and NetBSD console conventions.
</para></listitem>
</varlistentry>
<varlistentry>
<term><code>0x0Ckkkkmm</code></term>
<listitem><para>
A consumer device key.
The key number is <code>kkkk</code> and <code>mm</code> is a set of bitflags indicating the current state of modifier keys.
</para></listitem>
</varlistentry>
<varlistentry>
<term><code>0x0Ekkkkmm</code></term>
<listitem><para>
An extended key.
The key number is <code>kkkk</code> and <code>mm</code> is a set of bitflags indicating the current state of modifier keys.
</para></listitem>
</varlistentry>
<varlistentry>
<term><code>0x0Fkkkkmm</code></term>
<listitem><para>
A function key.
The function key number is <code>kkkk</code> and <code>mm</code> is a set of bitflags indicating the current state of modifier keys.
</para></listitem>
</varlistentry>
<varlistentry>
<term><code>0x11cccccc</code></term>
<listitem><para>
Unicode accelerator character.
The UCS-4 code point is <code>cccccc</code>.
</para></listitem>
</varlistentry>
</variablelist>

<note>
Input messages are intentionally similar to the action codes employed in keyboard maps (c.f. <citerefentry><refentrytitle>console-keyboard-map</refentrytitle><manvolnum>5</manvolnum></citerefentry>).
Because of this, codes <code>0x03nnnnnn</code>, <code>0x1Ennnnnn</code>, and <code>0x1Fnnnnnn</code> are reserved in input messages, and must be ignored.
</note>

<para>
The modifier flags represent abstract level2, level3, control, group2, and super modifiers.
(See ISO 9995 for the concepts of levels and groups; super is an abstract "Command" or "GUI" modifier.)
The input protocol does not comprise standalone events for modifier keys or associated modifier lock keys.
</para>

<para>
The numbering of system, extended, and consumer keys largely follows the ID numbers used for keys in the USB HID protocol, with some exceptions, and a number of additions for keys that are not present in USB.
Importantly, keys that generate ordinary Unicode characters are sent as a Unicode character message and <emphasis>not</emphasis> as an extended key with the USB keycode; with the exceptions of soft-switchable keys.
</para>

<para>
All mouse positioning is absolute, in cells; and there is no requirement that a positioning event be sent if the value on its particular axis has not changed.
All positioning events must precede any button events at the eventual position.
</para>

</refsection>

<refsection><title>Security</title>

<para>
No superuser privileges are required and user-space virtual terminals are designed to be managed entirely under the aegis of unprivileged user accounts.
</para>

<refsection><title>permissions and ownerships</title>

<para>
The creator of a user-space virtual terminal only requires write and search access to <filename><replaceable>directory</replaceable>/</filename> and need not have owner access to it.
A program attaching to an existing user-space virtual terminal only requires search access to <filename><replaceable>directory</replaceable>/</filename>.
</para>

<para>
All created display buffer files should have permissions rw- for the user or a group of any account that can modify the display (e.g. the effective UID of a process running <citerefentry><refentrytitle>console-terminal-emulator</refentrytitle><manvolnum>1</manvolnum></citerefentry>, <citerefentry><refentrytitle>console-multiplexor</refentrytitle><manvolnum>1</manvolnum></citerefentry>, or <citerefentry><refentrytitle>console-input-method</refentrytitle><manvolnum>1</manvolnum></citerefentry>) with r-- permission for the user or a group of any account that can realize the display output somewhere.
All other users and groups should have --- permissions.
</para>

<para>
All created input FIFO files should have permissions rw- for the user or a group of any account that can process the input (same examples as aforegiven) with -w- permission for the user or a group of any account that can realize the input somewhere.
All other users and groups should have --- permissions.
</para>

<para>
Usually <filename><replaceable>directory</replaceable>/</filename> itself will be set-group-ID to a group different to any of the aforegiven groups, allowing group access to the display buffers and input FIFO, without (thereby) granting (group) access to anything else in <filename><replaceable>directory</replaceable>/</filename>.
</para>

</refsection>

<refsection><title>privacy and non-persistence of display data</title>

<para>
The display buffer files must be erased at (non-abend) process termination.
This ensures that (absent system backups, log-structured filesystems, and low-level data recovery) old terminal display content cannot be read out of a display buffer.
For best results, place these files on a temporary filesystem, set whatever options the temporary filesystem has (if any) for erasing backing storage at unmount, and exclude the temporary filesystem from backups.
</para>

</refsection>

<refsection><title>programmatic input message injection</title>

<para>
Input method and multiplexor control under programmatic control with a command tool is done by injecting messages into the input stream.
Permission to inject things into the input stream is implicitly permission to generate arbitrary keyboard and mouse input to a session.
Usually the only accounts with such privileges are the superuser and the accounts used to run the d&#xe6;mons that form the user-space virtual terminal subsystem.
</para>

<para>
Such input method and multiplexor switching can unexpectedly switch users away from password prompts and the like.
Be careful about granting users the privilege of performing these actions.
For best results, only the user physically at the user station(s) where the virtual terminal is being realized should be able to generate input method and multiplexor switching commands, with actual keyboard actions.
</para>

<para>
(The kernel virtual terminal system shares these caveats and risks.
They are simply rarely discussed.)
</para>

</refsection>

</refsection>

<refsection><title>Author</title>
<para><author><personname><firstname>Jonathan</firstname> <surname>de Boyne Pollard</surname></personname></author></para>
</refsection>

</refentry>
