1 hosho_screen
liquibyte edited this page 2026-11-19 16:23:30 -06:00

hosho_screen.h

Screen Model: the Grid of Cells and the Scrollback.

Copyright (c) 2016-2026 Michael Grey (liquibyte) liquibyte@gmail.com SPDX-License-Identifier: Artistic-2.0 Distributed under the Artistic License 2.0; see LICENSE for terms.

Holds what a terminal shows: rows of cells, each cell with a character, two colors and attributes. It has the primary buffer, the alternate buffer (for full-screen programs) and the lines that scrolled off the top of the primary buffer.

Features:

  • Primary and alternate buffer of the same size
  • Scrollback with a limit, oldest line dropped first
  • Scroll of a range of rows up or down; insert, delete and erase of cells
  • Wide characters in two cells; a change to one half clears the other
  • Combining marks kept with their base character
  • A "dirty" mark on each line that changed, for a display to read
  • A mark on each line where a shell's prompt or a command's output starts
  • Resize that wraps the text again for a new width
  • Hyperlinks: a cell has the number of its link
  • Pictures: a cell shows one cell-sized part of a picture, so a picture moves, scrolls and is erased as text is

Internal design notes:

  • A cell is 20 bytes. A cell of all zero bits is an empty cell with the default colors, so calloc() gives empty lines
  • HoshoCell.character is a code point, or HOSHO_CELL_CLUSTER plus the place of a base character with its combining marks in the screen's cluster store. Equal clusters share one place
  • Each line of the two buffers has screen.columns cells or more. A scrollback line has only the cells up to its last one in use; a reader takes the cells after HoshoLine.length as empty
  • The screen does not know the cursor, the modes or the sequences: that is hosho_terminal.h
#include "hosho_screen.h"

Types: HoshoColor, HOSHO_COLOR_KIND_DEFAULT and the other values, HOSHO_ATTRIBUTE_BOLD and the other values, HOSHO_UNDERLINE_NONE and the other values, HoshoCell, HOSHO_LINE_SINGLE and the other values, HoshoImage, HOSHO_MARK_NONE and the other values, HOSHO_STATUS_NONE and the other values, HoshoLine, HoshoScreen

Constants: HOSHO_COLOR_DEFAULT, HOSHO_COLOR_INDEXED, HOSHO_COLOR_DIRECT, HOSHO_COLOR_KIND, HOSHO_COLOR_VALUE, HOSHO_CELL_CLUSTER, HOSHO_CELL_IMAGE, HOSHO_CELL_IMAGE_MAKE, HOSHO_CELL_IMAGE_ID, HOSHO_CELL_IMAGE_COLUMN, HOSHO_CELL_IMAGE_ROW, HOSHO_CELL_IS_IMAGE, HOSHO_IMAGE_LIMIT, HOSHO_IMAGE_CELL_LIMIT, HOSHO_LINK_LIMIT, HOSHO_CELL_CODEPOINT_LIMIT, HOSHO_CELL_TEXT_SIZE

Functions: hosho_screen_init, hosho_screen_free, hosho_screen_resize, hosho_screen_switch, hosho_screen_line, hosho_screen_write, hosho_screen_erase, hosho_screen_insert_cells, hosho_screen_delete_cells, hosho_screen_scroll_rectangle, hosho_screen_copy_rectangle, hosho_screen_set_line_size, hosho_screen_set_folded, hosho_screen_link_add, hosho_screen_link_address, hosho_screen_image_add, hosho_screen_image, hosho_screen_scroll_up, hosho_screen_scroll_down, hosho_screen_set_mark, hosho_screen_clear_scrollback, hosho_screen_widen, hosho_screen_set_input_column, hosho_screen_set_scrollback_limit, hosho_screen_combine, hosho_screen_cell_codepoints, hosho_screen_cell_text, hosho_screen_line_text, hosho_screen_mark_dirty, hosho_screen_clear_dirty

Types

HoshoColor

typedef uint32_t HoshoColor;

A color of a cell: the default, one of the 256 palette colors, or red, green and blue.

Make one with HOSHO_COLOR_DEFAULT, HOSHO_COLOR_INDEXED() or HOSHO_COLOR_DIRECT(); read it with HOSHO_COLOR_KIND() and HOSHO_COLOR_VALUE().

HOSHO_COLOR_KIND_DEFAULT and the other values

The kinds of HoshoColor.

Name Value Description
HOSHO_COLOR_KIND_DEFAULT 0 the terminal's default foreground or background
HOSHO_COLOR_KIND_INDEXED 1 a palette color; the value is its number, 0..255
HOSHO_COLOR_KIND_DIRECT 2 the value is 0xRRGGBB

HOSHO_ATTRIBUTE_BOLD and the other values

Bits of HoshoCell.attributes.

Name Value Description
HOSHO_ATTRIBUTE_BOLD 1 << 0 bold or increased intensity
HOSHO_ATTRIBUTE_FAINT 1 << 1 faint or decreased intensity
HOSHO_ATTRIBUTE_ITALIC 1 << 2 italic
HOSHO_ATTRIBUTE_BLINK 1 << 3 blinks
HOSHO_ATTRIBUTE_INVERSE 1 << 4 foreground and background exchanged
HOSHO_ATTRIBUTE_INVISIBLE 1 << 5 the character is not shown
HOSHO_ATTRIBUTE_STRIKE 1 << 6 a line through the character
HOSHO_ATTRIBUTE_OVERLINE 1 << 7 a line above the character
HOSHO_ATTRIBUTE_WIDE 1 << 8 the first cell of a wide character
HOSHO_ATTRIBUTE_WIDE_TAIL 1 << 9 the second cell of a wide character; its character is 0
HOSHO_ATTRIBUTE_PROTECTED 1 << 10 a selective erase does not erase the cell (DECSCA)
HOSHO_ATTRIBUTE_UNDERLINE_COLOR 1 << 11 the underline has its own color: underline_red, underline_green, underline_blue

HOSHO_UNDERLINE_NONE and the other values

Values of HoshoCell.underline.

Name Value Description
HOSHO_UNDERLINE_NONE 0 no underline
HOSHO_UNDERLINE_SINGLE 1 one line
HOSHO_UNDERLINE_DOUBLE 2 two lines
HOSHO_UNDERLINE_CURLY 3 a wave
HOSHO_UNDERLINE_DOTTED 4 dots
HOSHO_UNDERLINE_DASHED 5 dashes

HoshoCell

One cell of the grid.

Field Type Description
character uint32_t a code point; 0 when nothing was written here; or HOSHO_CELL_CLUSTER and a cluster
foreground HoshoColor the color of the character
background HoshoColor the color behind it
attributes uint16_t HOSHO_ATTRIBUTE_* bits.
underline uint8_t HOSHO_UNDERLINE_*.
underline_red uint8_t the red of the underline's color, with HOSHO_ATTRIBUTE_UNDERLINE_COLOR
link uint16_t the hyperlink the cell is a part of (hosho_screen_link_address()), or 0
underline_green uint8_t the green of the underline's color
underline_blue uint8_t the blue of the underline's color

HOSHO_LINE_SINGLE and the other values

Values of HoshoLine.size: the size of a line's characters (DECDWL, DECDHL).

Name Value Description
HOSHO_LINE_SINGLE 0 the normal size
HOSHO_LINE_DOUBLE_WIDTH 1 each character is two cells wide; the line holds half the columns
HOSHO_LINE_DOUBLE_TOP 2 double width and height: the top halves of the characters
HOSHO_LINE_DOUBLE_BOTTOM 3 double width and height: the bottom halves

HoshoImage

One picture a program sent (sixel, iTerm2's inline images or the Kitty graphics protocol), as the screen keeps it.

Field Type Description
pixels uint32_t * width times height pixels, each 0xAARRGGBB; alpha 0 is "nothing drawn"
width int pixels in a row
height int rows of pixels
cell_width int the width of a cell in pixels when the picture was put on the screen
cell_height int the height of a cell then
serial uint32_t a number no other picture of this screen has had, so a display knows a new picture in an old place

HOSHO_MARK_NONE and the other values

Values of HoshoLine.mark: what a shell said starts on the line (shell integration, OSC 133).

Name Value Description
HOSHO_MARK_NONE 0 nothing starts here
HOSHO_MARK_PROMPT 1 a prompt of the shell starts on this line
HOSHO_MARK_OUTPUT 2 the output of a command starts on this line

HOSHO_STATUS_NONE and the other values

Values of HoshoLine.status: how the command of a prompt line ended.

Name Value Description
HOSHO_STATUS_NONE 0 not known: the command still runs, or the shell did not say
HOSHO_STATUS_SUCCESS 1 the command ended with status 0
HOSHO_STATUS_FAILURE 2 the command ended with another status

HoshoLine

One line of cells.

Field Type Description
cells HoshoCell * the cells; NULL when length is 0
length int how many cells are there; cells after them are empty
wrapped uint8_t the text continues on the next line: the line ended because it was full
dirty uint8_t the line changed after the last hosho_screen_clear_dirty()
mark uint8_t HOSHO_MARK_*.
status uint8_t HOSHO_STATUS_*, for a line with HOSHO_MARK_PROMPT.
folded uint8_t for a line with HOSHO_MARK_PROMPT: a display hides the output of its command
size uint8_t HOSHO_LINE_*.
input_column uint16_t for a line with HOSHO_MARK_PROMPT: one more than the column where the prompt ends and the user's command starts (OSC 133;B); 0 when the shell did not say

HoshoScreen

The grid and the scrollback. Set it up with hosho_screen_init(); read the fields, change them only through the functions.

Field Type Description
rows int lines on the screen
columns int cells in each line
lines HoshoLine * the lines of the buffer in use: primary or alternate
primary HoshoLine * the lines of the primary buffer
alternate HoshoLine * the lines of the alternate buffer
alternate_active int 1 when the alternate buffer is in use
keep_lines uint8_t 1: a change of width cuts or pads the lines of the primary buffer and does not wrap its text again; a program that chooses the width itself (DECCOLM, DECSCPP) expects that
scrollback HoshoLine * a ring of the lines that scrolled off the primary buffer
scrollback_limit int the most lines the scrollback keeps
scrollback_count int how many lines it has now
scrollback_first int where in the ring the oldest line is
scrollback_total uint64_t how many lines went to the scrollback in all; a display that shows old lines uses it to stay on them
cluster_data uint32_t * the clusters: for each, its count of code points and then the code points
cluster_length size_t how much of cluster_data is in use
cluster_capacity size_t how much is allocated
cluster_table uint32_t * hash table of places in cluster_data; 0 is an empty slot
cluster_table_size size_t its slots, a power of two
cluster_count size_t how many clusters there are
links char ** the addresses of the hyperlinks; links[n - 1] is that of link n
link_count int how many links have an address
link_next int the link the next new address gets, from 0
images HoshoImage * the pictures; images[n] is picture n; pixels is NULL for a free place
image_serial uint32_t the serial the last new picture got

Constants

HOSHO_COLOR_DEFAULT

#define HOSHO_COLOR_DEFAULT ((HoshoColor)0)

The default foreground or background color.

HOSHO_COLOR_INDEXED

#define HOSHO_COLOR_INDEXED(index) ((HoshoColor)(0x01000000u | ((uint32_t)(index) & 0xFFu)))

Palette color number index (0..255).

HOSHO_COLOR_DIRECT

#define HOSHO_COLOR_DIRECT(red, green, blue) 	((HoshoColor)(0x02000000u | (((uint32_t)(red) & 0xFFu) << 16) | (((uint32_t)(green) & 0xFFu) << 8) | ((uint32_t)(blue) & 0xFFu)))

The color with these red, green and blue values (each 0..255).

HOSHO_COLOR_KIND

#define HOSHO_COLOR_KIND(color) ((color) >> 24)

The kind of a color: HOSHO_COLOR_KIND_*.

HOSHO_COLOR_VALUE

#define HOSHO_COLOR_VALUE(color) ((color) & 0x00FFFFFFu)

The palette number or the 0xRRGGBB of a color.

HOSHO_CELL_CLUSTER

#define HOSHO_CELL_CLUSTER 0x80000000u

Set in HoshoCell.character when the cell holds a base character with combining marks.

HOSHO_CELL_IMAGE

#define HOSHO_CELL_IMAGE 0x40000000u

Set in HoshoCell.character when the cell shows a part of a picture: see HOSHO_CELL_IMAGE_ID().

HOSHO_CELL_IMAGE_MAKE

#define HOSHO_CELL_IMAGE_MAKE(image, column, row) 	(HOSHO_CELL_IMAGE | (((uint32_t)(image) & 0x3FFu) << 20) | (((uint32_t)(column) & 0x3FFu) << 10) | ((uint32_t)(row) & 0x3FFu))

The character of a picture cell: the picture's number (1..1023) and which cell of the picture it is.

HOSHO_CELL_IMAGE_ID

#define HOSHO_CELL_IMAGE_ID(character) (((character) >> 20) & 0x3FFu)

The picture's number of a picture cell.

HOSHO_CELL_IMAGE_COLUMN

#define HOSHO_CELL_IMAGE_COLUMN(character) (((character) >> 10) & 0x3FFu)

Which column of the picture's cells a picture cell shows, from 0.

HOSHO_CELL_IMAGE_ROW

#define HOSHO_CELL_IMAGE_ROW(character) ((character) & 0x3FFu)

Which row of the picture's cells a picture cell shows, from 0.

HOSHO_CELL_IS_IMAGE

#define HOSHO_CELL_IS_IMAGE(character) (((character) & 0xC0000000u) == HOSHO_CELL_IMAGE)

1 when the character of a cell is a part of a picture.

HOSHO_IMAGE_LIMIT

#define HOSHO_IMAGE_LIMIT 1024

One more than the most pictures a screen holds: they have the numbers 1 to HOSHO_IMAGE_LIMIT - 1. One more replaces a picture no cell shows.

HOSHO_IMAGE_CELL_LIMIT

#define HOSHO_IMAGE_CELL_LIMIT 1023

The most cells a picture has in each direction.

#define HOSHO_LINK_LIMIT 65535

The most hyperlinks a screen holds; one more starts again at the first.

HOSHO_CELL_CODEPOINT_LIMIT

#define HOSHO_CELL_CODEPOINT_LIMIT 16

The most code points one cell holds: the base character and its marks. More marks are dropped.

HOSHO_CELL_TEXT_SIZE

#define HOSHO_CELL_TEXT_SIZE (HOSHO_CELL_CODEPOINT_LIMIT * 4 + 1)

The size of a buffer that holds the UTF-8 text of any one cell, with its NUL.

Functions

hosho_screen_init

int hosho_screen_init(HoshoScreen *screen, int rows, int columns, int scrollback_limit);

Set a screen up with empty lines.

Parameter Description
screen The screen
rows Lines on the screen, 1 or more
columns Cells in each line, 1 or more
scrollback_limit The most lines the scrollback keeps; 0 for no scrollback

Returns: 0, or -1 when memory ran out or a size is not correct

hosho_screen_free

void hosho_screen_free(HoshoScreen *screen);

Release the screen's memory.

Parameter Description
screen The screen

hosho_screen_resize

int hosho_screen_resize(
    HoshoScreen *screen,
    int rows,
    int columns,
    int *primary_cursor_row,
    int *primary_cursor_column,
    int *alternate_cursor_row);

Change the size of the two buffers.

When the width changes, the text of the primary buffer and of the scrollback is wrapped again: lines that continued on the next line are joined and cut at the new width. When only the height changes, empty lines below the cursor go first, then lines from the top of the primary buffer move to the scrollback; when it gets taller, lines come back from the scrollback. The alternate buffer is cut or padded: a full-screen program draws its screen again.

Parameter Description
screen The screen
rows The new number of lines, 1 or more
columns The new number of cells in a line, 1 or more
primary_cursor_row The row of the primary buffer's cursor; changed to the row the same text has afterwards
primary_cursor_column The column of the primary buffer's cursor; changed in the same way
alternate_cursor_row The row of the alternate buffer's cursor; changed to the row the same line has afterwards

Returns: 0, or -1 when memory ran out; the screen is then still correct, with the size in its fields

hosho_screen_switch

void hosho_screen_switch(HoshoScreen *screen, int alternate);

Use the primary or the alternate buffer.

Parameter Description
screen The screen
alternate 1 for the alternate buffer, 0 for the primary buffer

hosho_screen_line

const HoshoLine *hosho_screen_line(const HoshoScreen *screen, int row);

Get a line of the screen or of the scrollback.

Parameter Description
screen The screen
row 0 and up for a line of the buffer in use; -1 for the newest scrollback line, -2 for the one before it, and so on

Returns: The line, or NULL when there is no such line

hosho_screen_write

void hosho_screen_write(
    HoshoScreen *screen,
    int row,
    int column,
    const HoshoCell *cell,
    int width);

Write one character.

A wide character fills the cell and the one after it. A wide character that the write cuts in two is cleared.

Parameter Description
screen The screen
row The line
column The cell; for a wide character, column + 1 must be on the line too
cell The character with its colors and attributes
width 1, or 2 for a wide character

hosho_screen_erase

void hosho_screen_erase(
    HoshoScreen *screen,
    int row,
    int first,
    int end,
    const HoshoCell *blank);

Erase cells of one line.

Parameter Description
screen The screen
row The line
first The first cell to erase
end The cell after the last one to erase
blank What each erased cell becomes

hosho_screen_insert_cells

void hosho_screen_insert_cells(
    HoshoScreen *screen,
    int row,
    int column,
    int count,
    int end,
    const HoshoCell *blank);

Open room for cells in a line; the cells from column to end move right and those pushed past end are lost.

Parameter Description
screen The screen
row The line
column Where the new cells start
count How many cells
end The cell after the last one that moves: the number of columns, or less with a right margin
blank What each new cell is

hosho_screen_delete_cells

void hosho_screen_delete_cells(
    HoshoScreen *screen,
    int row,
    int column,
    int count,
    int end,
    const HoshoCell *blank);

Remove cells from a line; the cells after them, up to end, move left.

Parameter Description
screen The screen
row The line
column The first cell to remove
count How many cells
end The cell after the last one that moves: the number of columns, or less with a right margin
blank What each cell that comes in before end is

hosho_screen_scroll_rectangle

void hosho_screen_scroll_rectangle(
    HoshoScreen *screen,
    int top,
    int bottom,
    int left,
    int right,
    int count,
    const HoshoCell *blank);

Move the cells of a rectangle up or down in it; empty cells come in.

Parameter Description
screen The screen
top The first row of the rectangle
bottom The last row
left The first column
right The last column
count How many lines to move by: more than 0 moves the text up, less than 0 moves it down
blank What each cell that comes in is

hosho_screen_copy_rectangle

void hosho_screen_copy_rectangle(
    HoshoScreen *screen,
    int top,
    int bottom,
    int left,
    int right,
    int to_row,
    int to_column);

Copy the cells of a rectangle to another place of the screen; the two can overlap.

Parameter Description
screen The screen
top The first row of the rectangle
bottom The last row
left The first column
right The last column
to_row The row its first row goes to
to_column The column its first column goes to

hosho_screen_set_line_size

void hosho_screen_set_line_size(HoshoScreen *screen, int row, int size);

Set the size of a line's characters.

Parameter Description
screen The screen
row The line of the buffer in use
size HOSHO_LINE_*

hosho_screen_set_folded

void hosho_screen_set_folded(HoshoScreen *screen, int row, int folded);

Hide or show the output of a prompt line's command.

Parameter Description
screen The screen
row The line, as for hosho_screen_line(): negative for the scrollback
folded 1 to hide, 0 to show
uint16_t hosho_screen_link_add(HoshoScreen *screen, const char *address);

Keep the address of a hyperlink.

Parameter Description
screen The screen
address The address (a URI)

Returns: The link's number for HoshoCell.link, or 0 when memory ran out

const char *hosho_screen_link_address(const HoshoScreen *screen, uint16_t link);

The address of a hyperlink.

Parameter Description
screen The screen
link HoshoCell.link

Returns: The address, or NULL when the cell has no link

hosho_screen_image_add

int hosho_screen_image_add(
    HoshoScreen *screen,
    uint32_t *pixels,
    int width,
    int height,
    int cell_width,
    int cell_height);

Keep a picture.

Parameter Description
screen The screen
pixels width times height pixels from malloc(), each 0xAARRGGBB; the screen takes them, also when it gives 0
width Pixels in a row
height Rows of pixels
cell_width The width of a cell in pixels
cell_height The height of a cell in pixels

Returns: The picture's number for HOSHO_CELL_IMAGE_MAKE(), or 0 when there is no room

hosho_screen_image

const HoshoImage *hosho_screen_image(const HoshoScreen *screen, int image);

Get a picture.

Parameter Description
screen The screen
image The picture's number

Returns: The picture, or NULL when there is none with that number

hosho_screen_scroll_up

void hosho_screen_scroll_up(
    HoshoScreen *screen,
    int top,
    int bottom,
    int count,
    const HoshoCell *blank,
    int keep);

Move the lines of a range of rows up; empty lines come in at the bottom of the range.

Parameter Description
screen The screen
top The first row of the range
bottom The last row of the range
count How many lines to move by
blank What each cell of a new line is
keep 1 to put the lines that leave the range in the scrollback (only the primary buffer has one)

hosho_screen_scroll_down

void hosho_screen_scroll_down(
    HoshoScreen *screen,
    int top,
    int bottom,
    int count,
    const HoshoCell *blank);

Move the lines of a range of rows down; empty lines come in at the top of the range.

Parameter Description
screen The screen
top The first row of the range
bottom The last row of the range
count How many lines to move by
blank What each cell of a new line is

hosho_screen_set_mark

void hosho_screen_set_mark(HoshoScreen *screen, int row, int mark, int status);

Say what starts on a line, and how the command of a prompt line ended.

Parameter Description
screen The screen
row The line, as for hosho_screen_line(): negative for the scrollback
mark HOSHO_MARK_*
status HOSHO_STATUS_*

hosho_screen_clear_scrollback

void hosho_screen_clear_scrollback(HoshoScreen *screen);

Remove all lines of the scrollback.

Parameter Description
screen The screen

hosho_screen_widen

int hosho_screen_widen(HoshoScreen *screen, int row, int column);

Make a character of one cell two cells wide: the cell after it becomes its second cell.

Parameter Description
screen The screen
row The line
column The character's cell; the cell after it must be on the line

Returns: 1 when the character is wide now, 0 when it was wide before or there is no cell after it

hosho_screen_set_input_column

void hosho_screen_set_input_column(HoshoScreen *screen, int row, int column);

Say where on a prompt's line the user's command starts.

Parameter Description
screen The screen
row The line
column The column

hosho_screen_set_scrollback_limit

int hosho_screen_set_scrollback_limit(HoshoScreen *screen, int limit);

Change how many lines the scrollback keeps; the newest lines stay.

Parameter Description
screen The screen
limit The most lines; 0 for no scrollback

Returns: 0, or -1 when memory ran out (then nothing changed)

hosho_screen_combine

void hosho_screen_combine(HoshoScreen *screen, int row, int column, uint32_t mark);

Add a combining mark to the character of a cell.

Parameter Description
screen The screen
row The line
column The cell; the first cell of a wide character
mark The combining mark's code point

hosho_screen_cell_codepoints

size_t hosho_screen_cell_codepoints(
    const HoshoScreen *screen,
    const HoshoCell *cell,
    uint32_t *codepoints);

Get the code points of a cell: the base character and its combining marks.

Parameter Description
screen The screen
cell The cell
codepoints Room for HOSHO_CELL_CODEPOINT_LIMIT code points

Returns: How many code points the cell has; 0 for an empty cell or the second cell of a wide character

hosho_screen_cell_text

size_t hosho_screen_cell_text(
    const HoshoScreen *screen,
    const HoshoCell *cell,
    char *text);

Get the text of a cell as UTF-8.

Parameter Description
screen The screen
cell The cell
text Room for HOSHO_CELL_TEXT_SIZE bytes; gets the text and a NUL

Returns: The bytes of the text, without the NUL

hosho_screen_line_text

size_t hosho_screen_line_text(
    const HoshoScreen *screen,
    int row,
    char *text,
    size_t size);

Get the text of a line as UTF-8, without the empty cells at its end.

Parameter Description
screen The screen
row The line, as for hosho_screen_line()
text Where the text goes, with a NUL after it; text that does not fit is cut at a cell
size The bytes of room in text

Returns: The bytes of the text, without the NUL

hosho_screen_mark_dirty

void hosho_screen_mark_dirty(HoshoScreen *screen);

Mark each line of the buffer in use as changed.

Parameter Description
screen The screen

hosho_screen_clear_dirty

void hosho_screen_clear_dirty(HoshoScreen *screen);

Remove the "changed" mark from each line, after a display has drawn them.

Parameter Description
screen The screen