1 The Host Functions
liquibyte edited this page 2026-11-19 16:23:30 -06:00

The Host Functions

The terminal has no display, no keyboard and no window. When a program asks for something that only the host can do (answer a question, set the title, ring the bell, show a picture), the terminal calls a function the host gave it. This page describes each of these functions, and the settings and permissions a host gives the terminal.

1. How the terminal calls the host

The host functions are the fields of a HoshoTerminalHost, which the host passes to hosho_terminal_create() with a user pointer. Each function gets that pointer first. Each field can be NULL: the terminal then does without, as described for each. The structure must stay valid while the terminal is used, and a host can change its fields between two calls of hosho_terminal_feed() (the widget sets print and text only while its theme asks for a printer or a log).

The terminal calls the functions from hosho_terminal_feed(), while it reads the program's bytes, and a few from the other functions (shell_state from hosho_terminal_typed(), hosho_terminal_shell_forget() and hosho_terminal_reset(), leds from hosho_terminal_reset(), reply from hosho_terminal_pointer() and hosho_terminal_clipboard_reply()). The pointers it gives are good only during the call: a host that keeps a text copies it.

Three functions take apart pictures a program sent: image_decode, sixel_decode and inflate. What a program sends can be hostile, so a host can do these in a process of its own, in a jail, and the widget does. Pictures has the protocols and the jail.

2. The functions

Function When the terminal calls it, and what the host does
reply(user, bytes, length) The terminal answers a program: a report (where the cursor is, what the terminal is, the state of a mode, a color), the answerback message for ENQ, a locator report, the clipboard's text. The host sends the bytes to the program, as if typed (hosho_pseudoterminal_write()), and keeps what the program does not take at once. With 8-bit controls set (S8C1T), the bytes already have the one-byte C1 controls. Without this function no program gets an answer, and a program that waits for one can hang for a while.
title(user, title) The program set the window title (OSC 0 or OSC 2, or a title taken back from the title stack with CSI 23 t), as UTF-8. The terminal keeps a copy in terminal->title.
icon_name(user, name) The program set the name of the window's icon (OSC 1, and OSC 0, which sets both). The terminal keeps it in terminal->icon_name for CSI 20 t.
bell(user) The program sent BEL. The host makes a sound or a sign.
resize(user, rows, columns) The screen has another size, or the program asks for one. For 132 or 80 columns (DECCOLM, only after the program set mode 40) and for a page size the program chose (DECSCPP, DECSLPP, DECSNLS: 1 to 512 rows, 2 to 1024 columns) the terminal has the new size already, with long lines cut and not wrapped again, and the host's window follows. A page size and a request in characters come only when the host permits window functions (section 3). For a request in characters (CSI 8 ; rows ; columns t) nothing changed: a host that agrees calls hosho_terminal_resize(). A 0 in such a request is "what fits the display", when the host tells the display's size through window_report.
clipboard_set(user, selection, text, length) The program sets a selection's text (OSC 52), only when the host permits it (HOSHO_CLIPBOARD_WRITE). selection is 'p' for PRIMARY, when the program named PRIMARY and not the clipboard, and 'c' for the clipboard otherwise. The text is decoded already.
clipboard_request(user, selection) The program asks for a selection's text (OSC 52 ; c ; ?), only when the host permits it (HOSHO_CLIPBOARD_READ). The host answers, at once or later, with hosho_terminal_clipboard_reply(terminal, selection, text, length), which sends the text in base64 (NULL for none). Permit this only for programs you trust: a program that reads the clipboard sees what the user copied elsewhere.
leds(user, leds) The program switched keyboard lights (DECLL): leds has the HOSHO_LED_NUM_LOCK, HOSHO_LED_CAPS_LOCK and HOSHO_LED_SCROLL_LOCK bits that are on now. Called only when they change; a reset switches them off.
print(user, bytes, length) Bytes for the printer (the media copy functions, MC): the text of the lines a program prints, with a line feed after each, or in printer controller mode the program's bytes as they are. A call with length 0 says the print job ended. With no such function the terminal has no printer: it tells programs that ask that there is none, and drops printed text. The widget collects a job and gives it to the theme's print-command.
text(user, bytes, length) Text for a log: each character the program shows on the screen, as UTF-8, and a line feed for each line feed (LF, VT, FF). The widget writes it to the theme's log-file. Leave it NULL when there is no log: the terminal then does no work for it.
notify(user, title, body) The program asks for the user's attention: OSC 9 ; text (no title), OSC 777 ; notify ; title ; body, or kitty's OSC 99 (a title and a body can come in two parts; text in base64 and the keys for buttons, icons and sounds are not read). title can be NULL; the texts are UTF-8.
progress(user, state, percent) The program tells how far a job is (OSC 9 ; 4 ; state ; percent): state 0 for no job, 1 for a job at percent (0 to 100), 2 for an error, 3 for a job with no known end, 4 for a job that waits.
pointer_shape(user, name) The program asks for a pointer shape by its name (OSC 22), for example text or pointer; "" for the host's own pointer.
command_end(user, status, seconds) A command of the shell ended (shell integration: OSC 133 ; D ; status after OSC 133 ; C), with its exit status and how many seconds it ran, on a clock that does not go back. The widget uses it to tell the user when a long command ends.
shell_state(user, state) The shell's state changed (hosho_terminal_shell_state()): told after the bytes that changed it, after typed bytes did (hosho_terminal_typed()), and after hosho_terminal_shell_forget() and a reset.
window(user, function, first, second) The program asks for a window function (XTWINOPS, CSI function ; first ; second t), only after hosho_terminal_set_window_access(). See section 3.
window_report(user, report, &first, &second) The program asks about the window, only after hosho_terminal_set_window_access(). The host fills the two numbers and returns 1, or returns 0 when it does not know; the terminal then gives no answer. See section 3.
image_decode(user, data, length, &width, &height) Decode a picture file a program sent: an iTerm2 inline image, or a Kitty graphics picture with f=100. PNG at least, and any other format the host knows. The host returns width times height pixels from malloc(), each 0xAARRGGBB, and sets the size; or NULL. The terminal frees the pixels. With no such function the terminal shows only pictures that come as pixels (sixel, and kitty's f=24 and f=32).
sixel_decode(user, data, length, colors, fill_background, &width, &height) Read a sixel picture's data in place of the terminal, as hosho_sixel_decode() does. colors is the terminal's 256 color registers: the picture can change them, and the host puts the changed ones back, because a program can define colors in one picture and use them in the next. fill_background is 1 when the pixels the data does not draw take register 0's color, 0 when they stay transparent. The host returns pixels as for image_decode, at most HOSHO_SIXEL_SIZE_LIMIT (4096) each way, or NULL; a larger answer is thrown away. With no such function the terminal reads the data itself.
inflate(user, data, length, &result_length) Make zlib-compressed picture data whole (kitty's o=z) in place of the terminal, as hosho_inflate() does. The host returns the bytes from malloc(), at most HOSHO_INFLATE_LIMIT (64 MiB), or NULL; a longer answer is thrown away. With no such function the terminal does it itself.

3. The window functions

A program can ask to move, size, raise or iconify the window, and read its title and position. A terminal starts with this refused; hosho_terminal_set_window_access(terminal, 1) permits it (the theme's window-ops = 1 does this through hosho_theme_apply()). Then window gets these functions:

Function Number first, second
HOSHO_WINDOW_SHOW 1 Show the window again after an iconify.
HOSHO_WINDOW_ICONIFY 2 Make the window an icon.
HOSHO_WINDOW_MOVE 3 Move the window to x, y in pixels.
HOSHO_WINDOW_SIZE_PIXELS 4 Make the text area first pixels high and second wide; -1 (left out) keeps a value, 0 is the screen's.
HOSHO_WINDOW_RAISE, HOSHO_WINDOW_LOWER 5, 6 Put the window above or below the others.
HOSHO_WINDOW_REFRESH 7 Paint the window again.
HOSHO_WINDOW_MAXIMIZE 9 first is 1 to make the window as large as the screen, 0 to put it back.
HOSHO_WINDOW_FULL_SCREEN 10 first is 1 for full screen, 0 for not, 2 for the other state.

And window_report gets these questions: HOSHO_WINDOW_REPORT_ICONIFIED (11: first is 1 when the window is an icon), HOSHO_WINDOW_REPORT_POSITION (13: x and y of the window in pixels) and HOSHO_WINDOW_REPORT_SCREEN (15: the height and width of the screen in pixels). With the permission the terminal also reports the icon's name (CSI 20 t; the title when no program named the icon) and the title (CSI 21 t), without control characters, and in hexadecimal when the program set that title mode. It also gives a request in characters (CSI 8 t) to resize, with a 0 as "what fits the display" when window_report tells the screen's size, and takes a page size: CSI rows t with 24 rows or more (DECSLPP), DECSCPP and DECSNLS. These change the window's size, so they need the permission.

Without the permission the terminal still answers the size reports (CSI 14 t the text area in pixels, CSI 16 t the cell in pixels, CSI 18 t and CSI 19 t the screen in characters) and keeps the title stack (CSI 22 t saves the title, CSI 23 t takes it back; at most HOSHO_TITLE_STACK_LIMIT, 10, and one more drops the oldest; CSI 22 ; 1 t and CSI 23 ; 1 t, which are for the icon's name alone, do nothing): these change nothing and tell nothing of the desktop. Every other window function and report is ignored. DECCOLM (132 or 80 columns) has its own permission, mode 40, which a program sets itself.

4. What the host sets in the terminal

These functions give the terminal the host's defaults and permissions. hosho_theme_apply() calls most of them with a theme's values (see Themes).

Function What it sets
hosho_terminal_set_default_colors(terminal, foreground, background, cursor) The default text, background and cursor colors, each 0xRRGGBB. A program can change them (OSC 10, 11, 12) and a reset (or OSC 110, 111, 112) puts these back.
hosho_terminal_set_palette_color(terminal, index, color) One of the 256 palette colors (0 to 255). A program can change it (OSC 4) and a reset (or OSC 104) puts this back.
hosho_terminal_set_default_cursor(terminal, shape, blink) The cursor's shape and blink. A program can change them (DECSCUSR, mode 12); CSI 0 SP q and a reset give these back, and a program that resets mode 12 gets this blink back. A shape or blink a program chose stays when the host changes its default; one that is still the default follows.
hosho_terminal_set_cell_size(terminal, width, height) The size of a cell in pixels, for pictures and the size reports.
hosho_terminal_set_clipboard_access(terminal, access) What a program can do with the clipboard: HOSHO_CLIPBOARD_READ, HOSHO_CLIPBOARD_WRITE, both, or 0 (the start).
hosho_terminal_set_window_access(terminal, allowed) Whether a program can use the window functions and reports (section 3).
hosho_terminal_set_answerback(terminal, text) The answerback message: what the terminal sends when a program sends ENQ, at most 63 bytes; NULL or "" for nothing.
hosho_terminal_set_default_keyboard(terminal, vt220) A PC's keyboard (0, the start) or a VT220's (1). A program can change it (mode 1061) and a reset puts this back.
hosho_terminal_set_default_key_set(terminal, key_set) The codes of the function and editing keys: HOSHO_KEY_SET_PC (the start), SUN, HP, SCO, RXVT or LINUX. A program can choose the first three (modes 1051 to 1053) and a reset puts this back. See Keyboard, Mouse and Paste.

5. What the host asks the terminal

Function or field What it gives
hosho_terminal_mode(terminal, mode) 1 when a HoshoMode is set. The list of modes is on the page Control Functions.
hosho_terminal_color(terminal, color, is_background) The 0xRRGGBB of a cell's color, with the palette and default colors the program set.
hosho_terminal_shell_state(terminal) What the shell does, from its marks.
hosho_terminal_kitty_flags(terminal) The Kitty keyboard protocol's flags of the screen in use, 0 when the program did not ask for the protocol.
hosho_terminal_user_key(terminal, number, &length) What a function key sends with Shift, when a program defined it (DECUDK), by its DEC number: 11 to 15 for F1 to F5, 17 to 21 for F6 to F10, 23 to 26 for F11 to F14, 28 and 29 for F15 and F16, 31 to 34 for F17 to F20. hosho_input_key() uses it for F1 to F12.
hosho_terminal_status_line(terminal, &columns) The cells of the status line, or NULL.
hosho_terminal_pointer(terminal, event, button, row, column, x, y) The host tells the terminal what the pointer did, with the cell and the pixel. With the DEC locator on, the terminal sends the program the locator reports, and returns 1: the event is then the program's. The SGR mouse form in pixels (mode 1016) also uses the position told here.
terminal->title, terminal->directory The window title, and the shell's current directory (OSC 7), or NULL when not known.