1#ifndef TERM_H
2#define TERM_H
3
4#include <stdbool.h>
5#include <stdint.h>
6#include <sys/types.h>
7
8/* resolve 1D rune from 2D input */
9#define RUNE(t, x, y) ((t)->runes[(y) * (t)->cols + (x)])
10#define CSI_PARAMS_MAX (16)
11#define TERM_DEFAULT_FG 0xffffffffu
12#define TERM_DEFAULT_BG 0xff222222u
13#define TERM_ATTR_REVERSE (1 << 0)
14
15typedef enum {
16 TSTATE_NORMAL,
17 TSTATE_ESC,
18 TSTATE_CSI,
19 TSTATE_OSC,
20 TSTATE_OSC_ESC,
21 TSTATE_CHARSET,
22 TSTATE_CHARSET_SKIP,
23} TState;
24
25typedef struct {
26 uint32_t cp; /* codepoint */
27 uint32_t fg;
28 uint32_t bg;
29 uint8_t attr;
30 uint8_t width;
31 bool dmg; /* rune damaged */
32} Rune;
33
34typedef struct {
35 int x;
36 int y;
37} Caret;
38
39typedef struct {
40 Rune* runes;
41 Rune* alt;
42 Caret saved;
43 uint32_t fg;
44 uint32_t bg;
45 uint8_t attr;
46
47 int cols;
48 int rows;
49 int scroll_top;
50 int scroll_bot;
51
52 int ptyfd;
53 pid_t ptypid;
54
55 TState state;
56 bool acs[2];
57 int charset;
58 int charset_target;
59 bool wrapnext;
60 bool cursor_visible;
61 bool bracketed_paste;
62 int csi_params[CSI_PARAMS_MAX];
63 int csi_idx;
64} Term;
65
66/* clear screen by filling runes with ' ' and reseting
67 to default terminal foreground and background */
68void term_clear(Term* t, Caret* c);
69
70/* damage all the runes on terminal */
71void term_damage_all(Term* t);
72
73/* damage a single cell on terminal */
74void term_damage_rune(Term* t, int x, int y);
75
76/* put a character on terminal, handle placing special
77 and normal characters */
78void term_putc(Term* t, Caret* c, uint32_t cp);
79
80/* destroy old terminal and create create new terminal
81 with specified dimensions */
82int term_resize(Term* t, Caret* c, int cols, int rows);
83
84/* scroll viewport up by one */
85void term_scroll(Term* t);
86
87/* processes terminal input, parse escape sequences, update
88 caret, screen state, and damage status. */
89bool term_write(Term* t, Caret* c, const char* s, size_t n);
90
91#endif /* TERM_H */
92