skip to content
==> include/tui/input.h <==
1/* @ajax/tui-input: crossterm's key decoding for the keys a TUI uses: printable characters (UTF-8),
2 * Ctrl+letter, Enter, Tab, Shift-Tab, Backspace, Esc, the arrows, Home/End/Insert/Delete,
3 * PageUp/PageDown and F1-F12, from both CSI and SS3 sequences. A lone ESC is ambiguous until no
4 * more bytes come: the caller says when input has gone quiet. */
5#ifndef TUI_INPUT_H
6#define TUI_INPUT_H
7
8#include <stdbool.h>
9#include <stddef.h>
10#include <stdint.h>
11
12typedef enum {
13    TUI_KEY_NONE,
14    TUI_KEY_CHAR,
15    TUI_KEY_ENTER,
16    TUI_KEY_ESC,
17    TUI_KEY_BACKSPACE,
18    TUI_KEY_TAB,
19    TUI_KEY_BACKTAB,
20    TUI_KEY_UP,
21    TUI_KEY_DOWN,
22    TUI_KEY_LEFT,
23    TUI_KEY_RIGHT,
24    TUI_KEY_HOME,
25    TUI_KEY_END,
26    TUI_KEY_PAGE_UP,
27    TUI_KEY_PAGE_DOWN,
28    TUI_KEY_INSERT,
29    TUI_KEY_DELETE,
30    TUI_KEY_F,
31} tui_key_code;
32
33enum { TUI_MOD_SHIFT = 1, TUI_MOD_CONTROL = 2, TUI_MOD_ALT = 4 };
34enum { TUI_KEY_PRESS, TUI_KEY_REPEAT, TUI_KEY_RELEASE };
35
36typedef struct {
37    tui_key_code code;
38    uint32_t ch;  /* TUI_KEY_CHAR: the character */
39    uint8_t f;    /* TUI_KEY_F: 1..12 */
40    uint8_t mods; /* TUI_MOD_* */
41    uint8_t kind; /* TUI_KEY_PRESS */
42} tui_key;
43
44static inline tui_key tui_key_char(uint32_t c) { return (tui_key){TUI_KEY_CHAR, c, 0, 0, TUI_KEY_PRESS}; }
45static inline tui_key tui_key_of(tui_key_code code) { return (tui_key){code, 0, 0, 0, TUI_KEY_PRESS}; }
46
47/* Decodes one key from in[0..len). Returns the bytes it used: 0 when more bytes are needed
48 * (a partial sequence, or a lone ESC while quiet is false), and *key is TUI_KEY_NONE when the
49 * bytes were consumed but meant nothing to us (unknown sequences, mouse reports). */
50size_t tui_input_parse(const char *in, size_t len, bool quiet, tui_key *key);
51
52#endif