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

hosho_input.h

Input Encoder: Keys, Mouse, Focus and Paste as the Bytes a Program Reads.

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.

A program on a terminal reads bytes. This turns what the user does (a key, a mouse button, a paste) into the bytes an xterm-compatible terminal sends for it. The bytes depend on the modes the program set, so each function reads the terminal's modes.

Features:

  • Function, cursor and editing keys, with Shift, Alt and Control
  • Typed characters with Control (Control+C is 0x03) and Alt (ESC first, or the eighth bit with mode 1034 when mode 1036 is reset)
  • F1 to F20
  • Application cursor keys (DECCKM) and application keypad (DECKPAM)
  • A PC's keyboard by default, as in xterm: Home and End send CSI H and CSI F, and the keypad's digits are digits. With the VT220 keyboard (mode 1061, or the host's default) Home and End are Find and Select (CSI 1 ~, CSI 4 ~) and the keypad sends its own codes in application mode
  • modifyOtherKeys levels 1 and 2: keys such as Control+I, Control+Shift+A and Control+Enter say what they are, so a program can tell them from Tab, Control+A and Enter
  • Mouse reports in the X10, normal, UTF-8, SGR and urxvt forms, and the SGR form in pixels (mode 1016, a form of its own)
  • The Kitty keyboard protocol, as far as the program asked for it
  • Focus reports
  • Pasted text, made safe and put in the bracketed paste marks

Internal design notes:

  • The sequences come from xterm's ctlseqs ("PC-Style Function Keys", "Mouse Tracking", "Bracketed Paste Mode") and agree with the terminfo entry xterm-256color, which is the TERM a program gets
  • No function here keeps state or knows a toolkit: the host turns its own key codes into HoshoKey and sends the bytes it gets
  • A function gives 0 bytes when the terminal sends nothing for the event: a mouse press when the program did not ask for the mouse
#include "hosho_input.h"

Types: HoshoKey, HOSHO_MODIFIER_SHIFT and the other values, HoshoKeyAction, HoshoKeyEvent, HoshoMouseAction, HOSHO_BUTTON_NONE and the other values

Constants: HOSHO_INPUT_SIZE, HOSHO_INPUT_LEGACY

Functions: hosho_input_key_event, hosho_input_key, hosho_input_character, hosho_input_mouse_wanted, hosho_input_mouse, hosho_input_focus, hosho_input_paste

Types

HoshoKey

The keys that send a sequence. A key that types a character goes to hosho_input_character().

Name Value Description
HOSHO_KEY_NONE no key
HOSHO_KEY_UP cursor up
HOSHO_KEY_DOWN cursor down
HOSHO_KEY_RIGHT cursor right
HOSHO_KEY_LEFT cursor left
HOSHO_KEY_HOME Home.
HOSHO_KEY_END End.
HOSHO_KEY_BEGIN the middle key of the keypad (5) with NumLock off
HOSHO_KEY_INSERT Insert.
HOSHO_KEY_DELETE Delete.
HOSHO_KEY_PAGE_UP Page Up.
HOSHO_KEY_PAGE_DOWN Page Down.
HOSHO_KEY_BACKSPACE Backspace.
HOSHO_KEY_TAB Tab.
HOSHO_KEY_ENTER Enter or Return.
HOSHO_KEY_ESCAPE Escape.
HOSHO_KEY_KEYPAD_ENTER Enter of the numeric keypad.
HOSHO_KEY_F1 F1; F2 to F20 follow in order.
HOSHO_KEY_F2 F2.
HOSHO_KEY_F3 F3.
HOSHO_KEY_F4 F4.
HOSHO_KEY_F5 F5.
HOSHO_KEY_F6 F6.
HOSHO_KEY_F7 F7.
HOSHO_KEY_F8 F8.
HOSHO_KEY_F9 F9.
HOSHO_KEY_F10 F10.
HOSHO_KEY_F11 F11.
HOSHO_KEY_F12 F12.
HOSHO_KEY_F13 F13: with a PC's keyboard what Shift+F1 sends, with a VT220's CSI 25 ~.
HOSHO_KEY_F14 F14.
HOSHO_KEY_F15 F15.
HOSHO_KEY_F16 F16.
HOSHO_KEY_F17 F17.
HOSHO_KEY_F18 F18.
HOSHO_KEY_F19 F19.
HOSHO_KEY_F20 F20.
HOSHO_KEY_KEYPAD_0 0 of the numeric keypad; 1 to 9 follow in order
HOSHO_KEY_KEYPAD_1 1 of the keypad
HOSHO_KEY_KEYPAD_2 2 of the keypad
HOSHO_KEY_KEYPAD_3 3 of the keypad
HOSHO_KEY_KEYPAD_4 4 of the keypad
HOSHO_KEY_KEYPAD_5 5 of the keypad
HOSHO_KEY_KEYPAD_6 6 of the keypad
HOSHO_KEY_KEYPAD_7 7 of the keypad
HOSHO_KEY_KEYPAD_8 8 of the keypad
HOSHO_KEY_KEYPAD_9 9 of the keypad
HOSHO_KEY_KEYPAD_DECIMAL the decimal point of the keypad
HOSHO_KEY_KEYPAD_ADD plus of the keypad (the comma key of a VT220's keypad)
HOSHO_KEY_KEYPAD_SUBTRACT minus of the keypad
HOSHO_KEY_KEYPAD_MULTIPLY the multiply key of the keypad
HOSHO_KEY_KEYPAD_DIVIDE the divide key of the keypad
HOSHO_KEY_KEYPAD_EQUAL the equal key of the keypad

HOSHO_MODIFIER_SHIFT and the other values

The modifier keys held with a key or a mouse button.

Name Value Description
HOSHO_MODIFIER_SHIFT 1 << 0 Shift.
HOSHO_MODIFIER_ALT 1 << 1 Alt (Meta)
HOSHO_MODIFIER_CONTROL 1 << 2 Control.
HOSHO_MODIFIER_SUPER 1 << 3 Super (the key with a logo); only hosho_input_key_event() uses it.
HOSHO_MODIFIER_CAPS_LOCK 1 << 6 Caps Lock is on; only hosho_input_key_event() uses it.
HOSHO_MODIFIER_NUM_LOCK 1 << 7 Num Lock is on; only hosho_input_key_event() uses it.

HoshoKeyAction

What a key did, for hosho_input_key_event().

Name Value Description
HOSHO_KEY_PRESS 1 the key went down
HOSHO_KEY_REPEAT 2 the key is held and repeats
HOSHO_KEY_RELEASE 3 the key went up

HoshoKeyEvent

One key event with all that the Kitty keyboard protocol can report.

Field Type Description
key HoshoKey the key, when it is one of HoshoKey; HOSHO_KEY_NONE for a key that types a character and for a modifier key
codepoint uint32_t for a key that types: the character of the key with no modifier (for the A key: 'a'). For a modifier key: its number in the protocol (57441 for the left Shift). If not: 0.
shifted uint32_t the character of the key with Shift, or 0 when not known
base uint32_t the character of the same key in the US layout, or 0 when not known
modifiers int HOSHO_MODIFIER_* bits.
action HoshoKeyAction press, repeat or release
text const char * the UTF-8 text the key types with its modifiers, or NULL for none

HoshoMouseAction

What the mouse did.

Name Value Description
HOSHO_MOUSE_PRESS a button went down, or the wheel turned one step
HOSHO_MOUSE_RELEASE a button went up
HOSHO_MOUSE_MOTION the pointer moved to another cell

HOSHO_BUTTON_NONE and the other values

The mouse buttons, numbered as X11 numbers them.

Name Value Description
HOSHO_BUTTON_NONE 0 no button: a motion with no button down
HOSHO_BUTTON_LEFT 1 left button
HOSHO_BUTTON_MIDDLE 2 middle button
HOSHO_BUTTON_RIGHT 3 right button
HOSHO_BUTTON_WHEEL_UP 4 wheel turned away from the user
HOSHO_BUTTON_WHEEL_DOWN 5 wheel turned to the user
HOSHO_BUTTON_WHEEL_LEFT 6 wheel pushed left
HOSHO_BUTTON_WHEEL_RIGHT 7 wheel pushed right

Constants

HOSHO_INPUT_SIZE

#define HOSHO_INPUT_SIZE 128

The size of a buffer that holds the bytes of any one key, mouse or focus event.

HOSHO_INPUT_LEGACY

#define HOSHO_INPUT_LEGACY ((size_t)-1)

What hosho_input_key_event() gives for a key that the usual functions must encode: hosho_input_key() or hosho_input_character().

Functions

hosho_input_key_event

size_t hosho_input_key_event(
    const HoshoTerminal *terminal,
    const HoshoKeyEvent *event,
    char *bytes);

The bytes of a key event in the Kitty keyboard protocol, when the program asked for that protocol.

Call it first for each key event. When it gives HOSHO_INPUT_LEGACY, the program did not ask for the protocol, or the protocol sends this key as always: for a press or a repeat use hosho_input_key() or hosho_input_character(), and send nothing for a release.

Parameter Description
terminal The terminal, for its modes
event The key event
bytes Room for HOSHO_INPUT_SIZE bytes

Returns: How many bytes to send (0 for an event the program did not ask for), or HOSHO_INPUT_LEGACY

hosho_input_key

size_t hosho_input_key(
    const HoshoTerminal *terminal,
    HoshoKey key,
    int modifiers,
    char *bytes);

The bytes of a key that sends a sequence.

The bytes follow the terminal's state: application cursor keys and keypad, the VT220 keyboard, the function keys' set (HoshoKeySet, for a key with no modifier), VT52 mode, modifyOtherKeys, and 8-bit controls (S8C1T). Shift and one of F1 to F20 sends what a program defined for that key (DECUDK), when it defined something. F13 to F20 send what xterm sends: with a PC's keyboard, F1 to F8 with Shift added (CSI 1 ; 2 P for F13, as the terminfo entry's kf13 says); with a VT220's, CSI 25 ~ to CSI 34 ~.

Parameter Description
terminal The terminal, for its modes
key The key
modifiers HOSHO_MODIFIER_* bits
bytes Room for HOSHO_INPUT_SIZE bytes

Returns: How many bytes to send; 0 when the key is not known

hosho_input_character

size_t hosho_input_character(
    const HoshoTerminal *terminal,
    uint32_t codepoint,
    int modifiers,
    char *bytes);

The bytes of a key that types a character.

Give the character the key types without Control and Alt (for Control+C: 'c' or 'C'). Control makes the control character when the character has one. Alt puts ESC in front while HOSHO_MODE_META_ESCAPE (mode 1036) is set, as after a reset; with it reset and HOSHO_MODE_META_EIGHTH_BIT (mode 1034) set, a character below 0x80 gets its eighth bit and goes as UTF-8; with both reset Alt changes nothing. With modifyOtherKeys a key that the old form cannot tell apart is sent in that protocol's form instead, with one C1 control for CSI under S8C1T.

Parameter Description
terminal The terminal
codepoint The character
modifiers HOSHO_MODIFIER_* bits; Shift is already in the character
bytes Room for HOSHO_INPUT_SIZE bytes

Returns: How many bytes to send

hosho_input_mouse_wanted

int hosho_input_mouse_wanted(const HoshoTerminal *terminal);

Say whether the program asked for mouse reports.

Parameter Description
terminal The terminal

Returns: 1 when a mouse mode is set: the mouse then belongs to the program, not to the host's selection

hosho_input_mouse

size_t hosho_input_mouse(
    const HoshoTerminal *terminal,
    HoshoMouseAction action,
    int button,
    int modifiers,
    int row,
    int column,
    char *bytes);

The bytes of a mouse event.

Parameter Description
terminal The terminal, for its mouse modes
action Press, release or motion
button HOSHO_BUTTON_*: the button of a press or release, the button held in a motion; 8 to 11 for the buttons after the wheel's
modifiers HOSHO_MODIFIER_* bits
row The cell's row, from 0
column The cell's column, from 0
bytes Room for HOSHO_INPUT_SIZE bytes

Returns: How many bytes to send; 0 when the program did not ask for this event or the form cannot hold the position

hosho_input_focus

size_t hosho_input_focus(const HoshoTerminal *terminal, int focused, char *bytes);

The bytes that say the terminal got or lost the keyboard.

Parameter Description
terminal The terminal
focused 1 when it got the keyboard, 0 when it lost it
bytes Room for HOSHO_INPUT_SIZE bytes

Returns: How many bytes to send; 0 when the program did not ask for focus reports

hosho_input_paste

char *hosho_input_paste(
    const HoshoTerminal *terminal,
    const char *text,
    size_t length,
    size_t *result_length);

The bytes of pasted text.

Each line end (LF, or CR LF) becomes a carriage return, as the Enter key sends. Control characters other than tab and carriage return are removed, DEL and the C1 controls written as UTF-8 too, so pasted text cannot end the bracketed paste or send a control sequence. With bracketed paste mode the text goes between the paste marks.

Parameter Description
terminal The terminal
text The text (UTF-8)
length Its bytes
result_length Gets the number of bytes to send

Returns: The bytes to send, which the caller must free(); NULL when memory ran out