/* The terminal: raw mode and key decoding (term.c). */ #ifndef PIC_TUI_TERM_H #define PIC_TUI_TERM_H #include 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 */ unsigned focus_in; /* focus-in reports so far */ int alt_scroll; /* the alternate scroll mode (DECRQM 1007): 1 on, 0 off, -1 not reported */ int cluster; /* grapheme clustering (DECRQM 2027): 1 set, 2 reset, 3 always on, 0 not supported, -1 no answer */ int kitty; /* the kitty keyboard flags in force (CSI ? u): >= 0 when the terminal speaks the protocol, -1 no answer */ } 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 alternate scroll mode (put back on exit), the background colour, then DA1, which every terminal answers, in order, so * its reply means nothing more is coming. */ #define TUI_TERM_PROBE "\x1b[?1007$p\x1b[?2027$p\x1b[?u\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); /* OSC 9;4 terminal progress indicator: active != 0 starts indeterminate progress, active == 0 * clears it. Returns 0 without writing when TERM is unset or "dumb". */ size_t tui_term_progress(char *out, size_t cap, int active); /* Writes tui_term_progress to fd when fd is a terminal and the state changes, and ensures * tui_term_restore clears any active progress bar on exit or signal. */ void tui_term_progress_write(int fd, int active); /* 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) as tui_term_mouse wants it, 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); /* Whether fullscreen tracks the mouse (on at first); while fullscreen, the * change is written at once. Off also turns off the terminal's alternate * scroll mode (DECSET 1007), which would make the wheel Up and Down keys; * leaving fullscreen turns it back on when tui_term_alt_scroll_was said the * terminal had it on. */ void tui_term_mouse(int on); int tui_term_mouse_wanted(void); void tui_term_alt_scroll_was(int on); /* What the probe found out. Clusters: the DECRPM state of mode 2027; a terminal that has it (1, 2, 3) gets it set * (put back to reset on restore when it was 2), and tui_width_clusters follows, so widths agree with where the * terminal puts the cursor; any other answer leaves code points measured alone. Kitty: whether the terminal * answered the keyboard flags query; if so flag 1 (disambiguate escape codes) is pushed and popped on restore. * Both are written again when raw mode is entered after a suspend. */ void tui_term_clusters(int state); void tui_term_kitty(int answered); /* Text over this many bytes is not sent as OSC 52: terminals and tmux drop * or cut long ones. */ #define TUI_OSC52_MAX 75000 /* T[0..n) as an OSC 52 clipboard write (BEL-terminated), wrapped in tmux's * passthrough (DCS tmux; ...) when TMUX; malloc'd, its length in *len. NULL * over TUI_OSC52_MAX. */ char *tui_term_osc52(const char *t, size_t n, int tmux, size_t *len); /* Columns and rows of the terminal on fd; 80x24 when unknown. */ void tui_term_size(int fd, int *cols, int *rows); #endif