Skip to content

lib_panda

pf4 edited this page Dec 14, 2024 · 5 revisions

panda Terminal and Library

panda is a terminal emulator kernel module supporting ANSI escape codes. The file /dev/panda exists to have basic terminal interaction without using the panda library.

Related files

Index

Important notes

Color codes

8-bit color codes are used to set the color of the text. The color codes are not the same as the ones used in the ANSI escape codes but the VGA-text mode codes. The color codes have 4 bits for the background (0x00-0xF0) and 4 bits for the foreground (0x00-0x0F).

0x00 - Black
0x01 - Blue
0x02 - Green
0x03 - Cyan
0x04 - Red
0x05 - Magenta
0x06 - Orange
0x07 - Light Grey
0x08 - Dark Grey
0x09 - Light Blue
0x0A - Light Green
0x0B - Light Cyan
0x0C - Light Red
0x0D - Light Magenta
0x0E - Yellow
0x0F - White

Font

Panda can load psf fonts with 0x864ab572 magic number using the panda_change_font function. If the font has more than 256 characters, the other characters will be ignored.

ANSI support

Panda supports some ANSI escape codes. The following codes are supported:

  • \033[2J - Clear screen
  • \033[0m - Reset color
  • \033[3*m - Set text color (* is the color)
  • \033[4*m - Set background (* is the color)
  • \033[9*m - Set text bright color (* is the color)
  • \033[10*m - Set background bright color (* is the color)
  • \033[H - Move cursor to top left
  • \033[*A - Move cursor up (* is lines count)
  • \033[*B - Move cursor down (* is lines count)
  • \033[*C - Move cursor right (* is columns count)
  • \033[*D - Move cursor left (* is columns count)
  • \033[?25l - Hide cursor
  • \033[?25h - Show cursor
  • \033[s - Save cursor position
  • \033[u - Restore cursor position

Functions

panda_set_char

Set a character at a specific position on the screen,

void panda_set_char (
    uint32_t x,
    uint32_t y,
    uint8_t  c,
    uint8_t  color
);
  • x: X position
  • y: Y position
  • c: Character
  • color: Color

panda_print_string

Print a string at the current cursor position with ansi support. If the length is less than 0, the function will print characters until it finds a null character. The temporary color is used to print the string with a different color than the current one, if the color is -1, the current color will be used.

uint8_t panda_print_string (
    const char *string,
    int         len,
    int         tmp_color,
    uint8_t     default_color
);
  • string: String to print
  • len: Length of the string
  • tmp_color: Temporary color
  • default_color: Color of \e[0m
  • Returns: The last color used

panda_set_start

Function used at boot to avoid writing over the kernel messages. You probably don't need to use this function.

void panda_set_start (
    int kernel_cursor
);
  • kernel_cursor: Cursor position

panda_get_cursor

Save the current cursor position in tow variables.

void panda_get_cursor(
    uint32_t *x,
    uint32_t *y
);
  • x: Pointer to the x position
  • y: Pointer to the y position

panda_draw_cursor

Draw the cursor at given position.

void panda_draw_cursor (
    uint32_t x,
    uint32_t y
);
  • x: X position
  • y: Y position

panda_get_size

Get the screen size or set to zero if panda is not initialized.

void panda_get_size (
    uint32_t *x,
    uint32_t *y
);
  • x: Pointer to the x size
  • y: Pointer to the y size

panda_change_font

Change the font of the terminal.

int panda_change_font (
    const char *font
);
  • font: Font path
  • Returns: 0 on success, 1 on failure

panda_screen_backup

Generate a backup of the screen, the function will allocate memory for the backup and return a pointer to it. You need to free the memory with panda_screen_free.

void *panda_screen_backup (void);
  • Returns: Pointer to the backup

panda_screen_restore

Restore the screen from a backup (does not free the backup).

void panda_screen_restore (
    void *backup
);
  • backup: Pointer to the backup

panda_screen_free

Free the memory allocated by panda_screen_backup.

void panda_screen_free (
    void *backup
);
  • backup: Pointer to the backup