/* The terminal: raw mode and key decoding (term.c). */
#ifndef PIC_TUI_TERM_H
#define PIC_TUI_TERM_H
#include <stddef.h>
typedef enum {
TUI_KEY_CHAR, /* text: one UTF-8 character */
TUI_KEY_ENTER, /* CR */
TUI_KEY_NEWLINE, /* Ctrl+J (LF), Shift+Enter (CSI 13;2u or CSI 27;2;13~) */
TUI_KEY_TAB,
TUI_KEY_BACKSPACE, /* DEL or Ctrl+H */
TUI_KEY_DELETE, /* CSI 3~ */
TUI_KEY_LEFT, TUI_KEY_RIGHT, TUI_KEY_UP, TUI_KEY_DOWN,
TUI_KEY_HOME, /* CSI H, CSI 1~, SS3 H, Ctrl+A */
TUI_KEY_END, /* CSI F, CSI 4~, SS3 F, Ctrl+E */
TUI_KEY_PAGEUP, TUI_KEY_PAGEDOWN,
TUI_KEY_WORD_LEFT, /* Alt+B, Alt/Ctrl+Left (CSI 1;3D, CSI 1;5D) */
TUI_KEY_WORD_RIGHT, /* Alt+F, Alt/Ctrl+Right */
TUI_KEY_DELETE_WORD, /* Ctrl+W, Alt+Backspace */
TUI_KEY_KILL_LINE, /* Ctrl+K: to the end of the line */
TUI_KEY_KILL_START, /* Ctrl+U: to the start of the line */
TUI_KEY_ESC, /* a lone Escape (see tui_keys_flush) */
TUI_KEY_CTRL_C, TUI_KEY_CTRL_D, TUI_KEY_CTRL_L, TUI_KEY_CTRL_O,
TUI_KEY_PASTE, /* bracketed paste: paste/paste_len, CR and CRLF turned into LF */
TUI_KEY_CTRL_S, TUI_KEY_CTRL_P,
TUI_KEY_BACKTAB, /* Shift+Tab: CSI Z */
TUI_KEY_CTRL_T,
TUI_KEY_DELETE_WORD_FWD, /* Alt+D, Alt+Delete */
TUI_KEY_YANK, /* Ctrl+Y */
TUI_KEY_YANK_POP, /* Alt+Y */
TUI_KEY_UNDO, /* Ctrl+- (0x1F), kitty CSI 45;5u */
TUI_KEY_JUMP, /* Ctrl+]: jump forward to the next typed character */
TUI_KEY_JUMP_BACK, /* Ctrl+Alt+]: backward */
TUI_KEY_CTRL_G, /* external editor */
TUI_KEY_CTRL_X, /* copy the last reply */
TUI_KEY_CTRL_Z, /* suspend */
TUI_KEY_ALT_UP, /* CSI 1;3A: restore queued messages */
TUI_KEY_CTRL_P_BACK, /* Shift+Ctrl+P: CSI 112;6u or CSI 27;6;112~ (needs a terminal that reports modifiers) */
TUI_KEY_ALT_ENTER, /* ESC CR, CSI 13;3u, CSI 27;3;13~: queue a follow-up (Pi's app.message.followUp) */
TUI_KEY_CTRL_HOME, /* CSI 1;5H: fullscreen, the top of the transcript */
TUI_KEY_CTRL_END, /* CSI 1;5F: fullscreen, the bottom */
TUI_KEY_CTRL_V, /* paste from the system clipboard: an image, else text (Pi's app.clipboard.pasteImage) */
TUI_KEY_MOUSE, /* SGR mouse report (CSI < b;x;y M or m): the fields below */
TUI_KEY_OTHER, /* a key with no meaning of its own: only its modifiers and base (keymap.h may bind it) */
TUI_KEY_ACTION, /* what a keybinding asks for beyond the kinds above: tui_key.action (keymap.h) */
TUI_KEY_HISTORY_PREV, TUI_KEY_HISTORY_NEXT, /* Pi's tui.editor.historyPrevious / historyNext */
} tui_key_kind;
/* Modifier bits of tui_key.mods, as the Kitty and xterm protocols number them. */
enum { TUI_MOD_SHIFT = 1, TUI_MOD_ALT = 2, TUI_MOD_CTRL = 4, TUI_MOD_SUPER = 8 };
/* What a TUI_KEY_MOUSE reports. */
enum { TUI_MOUSE_PRESS, TUI_MOUSE_DRAG, TUI_MOUSE_RELEASE, TUI_MOUSE_WHEEL };
typedef struct {
tui_key_kind kind;
char text[8]; /* TUI_KEY_CHAR: the character, NUL-terminated */
char *paste; /* TUI_KEY_PASTE: malloc'd; the caller frees it */
size_t paste_len;
/* TUI_KEY_MOUSE: TUI_MOUSE_*, the button (0 left, 1 middle, 2 right; wheel:
* -1 up, 1 down), 0-based column and row, and Alt held. */
int mouse, button, mx, my, alt;
/* The key as Pi names it, for keybindings (keymap.h): TUI_MOD_* and the base key ("up", "pageUp", "enter",
* "a", "]"); empty for text, pastes and mouse reports. */
unsigned mods;
char base[12];
int action; /* TUI_KEY_ACTION: TUI_ACT_* */
} tui_key;
/* An incremental decoder: bytes in, keys out. Unknown escape sequences are
* consumed whole and dropped; other control bytes are dropped. */
typedef struct tui_keys tui_keys;
tui_keys *tui_keys_new(void);
void tui_keys_free(tui_keys *k);
void tui_keys_feed(tui_keys *k, const char *buf, size_t n);
/* 1 with the next decoded key in *out, 0 when none is complete yet. */
int tui_keys_next(tui_keys *k, tui_key *out);
/* Alt plus one of these characters is a key (tui_key.mods/base) instead of Escape followed by the character;
* Alt+B, F, D and Y always are. Replaces the earlier set. */
void tui_keys_alt_letters(tui_keys *k, const char *letters);
/* A lone ESC (or an unfinished sequence) is waiting for more bytes; the
* caller waits ~50 ms for more input, then calls tui_keys_flush, which
* turns a lone ESC into TUI_KEY_ESC. An unfinished CSI/SS3 or UTF-8
* sequence stays pending (a slow link may deliver the rest later) and is
* dropped only after 3 consecutive flushes with no new input. */
int tui_keys_waiting(const tui_keys *k);
void tui_keys_flush(tui_keys *k);
/* 1 while decoded keys wait for tui_keys_next. */
int tui_keys_queued(const tui_keys *k);
/* What the terminal said in answer to TUI_TERM_PROBE, and the focus reports DECSET 1004 asks for. Every
* reply is consumed, never delivered as keys; seq grows with each one the decoder recognises. */
typedef struct {
unsigned seq;
int da1; /* a primary device attributes reply came: every query sent before it has been answered */
int light; /* the OSC 11 background: 1 light, 0 dark, -1 no answer */
int focus; /* 1 focused, 0 not, -1 never reported */
} tui_term_replies;
const tui_term_replies *tui_keys_replies(const tui_keys *k);
/* While on, ESC ] (OSC) and ESC P, _, ^, X (DCS, APC, PM, SOS) open a terminal reply that is consumed up
* to its BEL or ESC \ instead of being Escape and a key, and an unfinished one is kept across flushes.
* Turned on after the probe is written; a DA1 reply turns it off, as does the caller's timeout. Off, a
* typed Alt+] or Esc then P keeps its meaning. */
void tui_keys_expect(tui_keys *k, int on);
/* An X11 colour as OSC 10/11 answers carry it (rgb:R/G/B with 1 to 4 hex digits a channel, rgba:R/G/B/A,
* #RGB to #RRRRGGGGBBBB): 1 when its perceived luminance is over a half, 0 when not, -1 when unreadable. */
int tui_color_spec_light(const char *spec, size_t n);
/* Asked once raw mode is on: the background colour, then DA1, which every terminal answers, in order, so
* its reply means nothing more is coming. */
#define TUI_TERM_PROBE "\x1b]11;?\x1b\\\x1b[c"
/* A turn shorter than this, with focus never reported, gets no done signal: the user is likely watching. */
#define TUI_DONE_UNKNOWN_FOCUS_MS 10000
/* The bytes that tell the terminal a turn is over and pic waits for the user, "what" the notification
* text: BEL (a dock bounce or tab mark) and a desktop notification, OSC 9, or OSC 777 in foot and VTE
* terminals, which have that one instead. Nothing (0) while the window has focus (1), or when focus is
* unknown (-1) and the turn took under TUI_DONE_UNKNOWN_FOCUS_MS. The length written, NUL not counted. */
size_t tui_term_done_signal(char *out, size_t cap, int focus, long turn_ms, const char *what);
/* Raw mode on fd (no echo, no canonical input, no signals from keys, no
* CR translation) plus bracketed paste and focus reports (DECSET 1004); the
* previous state is saved. tui_term_restore puts it back, turns both off and leaves
* fullscreen (tui_term_alt); it is safe
* to call more than once and from a signal handler. */
int tui_term_raw(int fd);
void tui_term_restore(void);
/* Fullscreen on fd: the alternate screen, autowrap off, SGR mouse reporting
* (buttons and drags), the screen cleared; off puts all of that back and
* shows the cursor. tui_term_restore leaves it too, so an exit or a fatal
* signal never strands the terminal on the alternate screen. */
void tui_term_alt(int fd, int on);
/* Columns and rows of the terminal on fd; 80x24 when unknown. */
void tui_term_size(int fd, int *cols, int *rows);
#endif