Text UI

From OSDev.wiki
Jump to navigation Jump to search

A text user interface is a user interface where by all output is presented in the form of text, in contrast with a Graphical UI that uses graphics along with text to display output.

Popularity and Use

With the rise of graphical UI's, text based user interfaces still remain practical in hobbyist operating system projects, as they are the most easier type of interface to implement, and are still in use in larger and commerical operating systems where a graphical user interface is unneccesary, for example, in large servers. A text user interface is also easy for quickly outputing results or debug data, and inputing commands to test new features, rather than writing complicated graphical applications.

Input

Input in a text user interface primarily involves the use of a command line shell, usually through entering commands via a keyboard. Other methods do exist, such as text-based menus that can scroll up and down, and prompts asking a user to press a specific key for a specific event to occur.

Video Mode

The most video mode for a text UI is text mode, or mode 0. This is the most commonly used, as it allows direct memory access to a linear address containing each character and it's associated attributes. Text mode 0 provides a text interface 80 characters wide and 25 characters lines per screen.

Video Memory

In text mode 0, the linear text buffer is located in physical at 0xB8000. Reading and writing to and from this address will provide direct manipulation of on screen text. To access a particular character on the screen from X and Y coordinates is simple using the following formula:

position = (y_position * characters_per_line) + x_position;

Each character takes up two bytes of space in memory. The first byte is split into two segments, the forecolour, and the backcolour. The second byte is a n 8-bit ASCII value of the character to print.

Colours

In text mode 0, the following colour pallete is avaliable for use:

Number Colour Name
0 Black
1 Blue
2 Green
3 Cyan
4 Red
5 Magenta
6 Brown
7 Light Gray
8 Dark Gray
9 Light Blue
10 Light Green
11 Light Cyan
12 Light Red
13 Light Magenta
14 Yellow
15 White

Each character has a forecolour and a backcolour. The default display colours set by the BIOS upon booting are are 7 for the foreground, and 0 for the background.

Example Console Class

A heavily documented and easy to understand example of a text console class is taken from Brandon's Kernel Tutorial, avaliable at http://osdever.net/bkerndev/Docs/printing.htm

C Code for Print a Character

The following C code will print a character at X and Y coordinates:

void WriteCharacter(unsigned char c, unsigned char forecolour, unsigned char backcolour, int x, int y)
{
     unsigned short attrib = (backcolour << 4) | (forecolour & 0x0F);
     volatile unsigned short *where;
     where = 0xB8000 + (y * 80 + x);
     *where = c | (attrib << 8);
}

Scrolling

Scrolling is achieved through copying the second line of characters onto the first, the third onto the second, etc. Then clearing the last line of text with null characters (usually a blank space with the fore/back colours of 7 and 0, respectively). In text mode 0, this is accomplished by copying characters 80-159 in to memory position 0-79, 160-239 into 80-159, etc.

Virtual Terminals

Virtual terminals are much the same as physical screens, but instead of writing to the linear text buffer referring to the screen, text is written to a linear text buffer stored in memory. Each virtual terminal has it's own text buffer, and the terminal currently being displayed on screen has it's buffer copied into the video buffer to display on screen. Having more than one virtual text buffer has other advantages, such as only having parts of multiple buffer being outputed onto the screen at the same time.