Xinqi Bao's Git

Factor out equivalent code from ttyread/ttysend
[st.git] / st.h
1 /* See LICENSE for license details. */
2
3 /* Arbitrary sizes */
4 #define UTF_SIZ 4
5
6 /* macros */
7 #define MIN(a, b) ((a) < (b) ? (a) : (b))
8 #define MAX(a, b) ((a) < (b) ? (b) : (a))
9 #define LEN(a) (sizeof(a) / sizeof(a)[0])
10 #define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
11 #define DIVCEIL(n, d) (((n) + ((d) - 1)) / (d))
12 #define DEFAULT(a, b) (a) = (a) ? (a) : (b)
13 #define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
14 #define ATTRCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || \
15 (a).bg != (b).bg)
16 #define IS_SET(flag) ((term.mode & (flag)) != 0)
17 #define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \
18 (t1.tv_nsec-t2.tv_nsec)/1E6)
19 #define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
20
21 #define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
22 #define IS_TRUECOL(x) (1 << 24 & (x))
23
24 enum glyph_attribute {
25 ATTR_NULL = 0,
26 ATTR_BOLD = 1 << 0,
27 ATTR_FAINT = 1 << 1,
28 ATTR_ITALIC = 1 << 2,
29 ATTR_UNDERLINE = 1 << 3,
30 ATTR_BLINK = 1 << 4,
31 ATTR_REVERSE = 1 << 5,
32 ATTR_INVISIBLE = 1 << 6,
33 ATTR_STRUCK = 1 << 7,
34 ATTR_WRAP = 1 << 8,
35 ATTR_WIDE = 1 << 9,
36 ATTR_WDUMMY = 1 << 10,
37 ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT,
38 };
39
40 enum term_mode {
41 MODE_WRAP = 1 << 0,
42 MODE_INSERT = 1 << 1,
43 MODE_APPKEYPAD = 1 << 2,
44 MODE_ALTSCREEN = 1 << 3,
45 MODE_CRLF = 1 << 4,
46 MODE_MOUSEBTN = 1 << 5,
47 MODE_MOUSEMOTION = 1 << 6,
48 MODE_REVERSE = 1 << 7,
49 MODE_KBDLOCK = 1 << 8,
50 MODE_HIDE = 1 << 9,
51 MODE_ECHO = 1 << 10,
52 MODE_APPCURSOR = 1 << 11,
53 MODE_MOUSESGR = 1 << 12,
54 MODE_8BIT = 1 << 13,
55 MODE_BLINK = 1 << 14,
56 MODE_FBLINK = 1 << 15,
57 MODE_FOCUS = 1 << 16,
58 MODE_MOUSEX10 = 1 << 17,
59 MODE_MOUSEMANY = 1 << 18,
60 MODE_BRCKTPASTE = 1 << 19,
61 MODE_PRINT = 1 << 20,
62 MODE_UTF8 = 1 << 21,
63 MODE_SIXEL = 1 << 22,
64 MODE_MOUSE = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\
65 |MODE_MOUSEMANY,
66 };
67
68 enum selection_mode {
69 SEL_IDLE = 0,
70 SEL_EMPTY = 1,
71 SEL_READY = 2
72 };
73
74 enum selection_type {
75 SEL_REGULAR = 1,
76 SEL_RECTANGULAR = 2
77 };
78
79 enum selection_snap {
80 SNAP_WORD = 1,
81 SNAP_LINE = 2
82 };
83
84 typedef unsigned char uchar;
85 typedef unsigned int uint;
86 typedef unsigned long ulong;
87 typedef unsigned short ushort;
88
89 typedef uint_least32_t Rune;
90
91 #define Glyph Glyph_
92 typedef struct {
93 Rune u; /* character code */
94 ushort mode; /* attribute flags */
95 uint32_t fg; /* foreground */
96 uint32_t bg; /* background */
97 } Glyph;
98
99 typedef Glyph *Line;
100
101 typedef struct {
102 Glyph attr; /* current char attributes */
103 int x;
104 int y;
105 char state;
106 } TCursor;
107
108 /* Internal representation of the screen */
109 typedef struct {
110 int row; /* nb row */
111 int col; /* nb col */
112 Line *line; /* screen */
113 Line *alt; /* alternate screen */
114 int *dirty; /* dirtyness of lines */
115 TCursor c; /* cursor */
116 int top; /* top scroll limit */
117 int bot; /* bottom scroll limit */
118 int mode; /* terminal mode flags */
119 int esc; /* escape state flags */
120 char trantbl[4]; /* charset table translation */
121 int charset; /* current charset */
122 int icharset; /* selected charset for sequence */
123 int numlock; /* lock numbers in keyboard */
124 int *tabs;
125 } Term;
126
127 /* Purely graphic info */
128 typedef struct {
129 int tw, th; /* tty width and height */
130 int w, h; /* window width and height */
131 int ch; /* char height */
132 int cw; /* char width */
133 char state; /* focus, redraw, visible */
134 int cursor; /* cursor style */
135 } TermWindow;
136
137 typedef struct {
138 uint b;
139 uint mask;
140 char *s;
141 } MouseShortcut;
142
143 typedef struct {
144 int mode;
145 int type;
146 int snap;
147 /*
148 * Selection variables:
149 * nb – normalized coordinates of the beginning of the selection
150 * ne – normalized coordinates of the end of the selection
151 * ob – original coordinates of the beginning of the selection
152 * oe – original coordinates of the end of the selection
153 */
154 struct {
155 int x, y;
156 } nb, ne, ob, oe;
157
158 char *primary, *clipboard;
159 int alt;
160 struct timespec tclick1;
161 struct timespec tclick2;
162
163 //Atom xtarget;
164 } Selection;
165
166 typedef union {
167 int i;
168 uint ui;
169 float f;
170 const void *v;
171 } Arg;
172
173 typedef struct {
174 uint mod;
175 KeySym keysym;
176 void (*func)(const Arg *);
177 const Arg arg;
178 } Shortcut;
179
180 typedef struct {
181 KeySym k;
182 uint mask;
183 char *s;
184 /* three valued logic variables: 0 indifferent, 1 on, -1 off */
185 signed char appkey; /* application keypad */
186 signed char appcursor; /* application cursor */
187 signed char crlf; /* crlf mode */
188 } Key;
189
190 void die(const char *, ...);
191 void redraw(void);
192
193 int tattrset(int);
194 void tnew(int, int);
195 void tresize(int, int);
196 void tsetdirt(int, int);
197 void tsetdirtattr(int);
198 void ttynew(char *, char *, char **);
199 size_t ttyread(void);
200 void ttyresize(int, int);
201 void ttysend(char *, size_t);
202 void ttywrite(const char *, size_t);
203
204 void resettitle(void);
205
206 void selclear(void);
207 void selinit(void);
208 void selnormalize(void);
209 int selected(int, int);
210 char *getsel(void);
211
212 size_t utf8decode(const char *, Rune *, size_t);
213 size_t utf8encode(Rune, char *);
214
215 void *xmalloc(size_t);
216 void *xrealloc(void *, size_t);
217 char *xstrdup(char *);
218
219 /* Globals */
220 extern TermWindow win;
221 extern Term term;
222 extern Selection sel;
223 extern int cmdfd;
224 extern pid_t pid;
225 extern int oldbutton;
226
227 /* config.h globals */
228 extern char font[];
229 extern int borderpx;
230 extern float cwscale;
231 extern float chscale;
232 extern unsigned int doubleclicktimeout;
233 extern unsigned int tripleclicktimeout;
234 extern int allowaltscreen;
235 extern unsigned int xfps;
236 extern unsigned int actionfps;
237 extern unsigned int cursorthickness;
238 extern int bellvolume;
239 extern unsigned int blinktimeout;
240 extern char termname[];
241 extern const char *colorname[];
242 extern size_t colornamelen;
243 extern unsigned int defaultfg;
244 extern unsigned int defaultbg;
245 extern unsigned int defaultcs;
246 extern unsigned int defaultrcs;
247 extern unsigned int cursorshape;
248 extern unsigned int cols;
249 extern unsigned int rows;
250 extern unsigned int mouseshape;
251 extern unsigned int mousefg;
252 extern unsigned int mousebg;
253 extern unsigned int defaultattr;
254 extern MouseShortcut mshortcuts[];
255 extern size_t mshortcutslen;
256 extern Shortcut shortcuts[];
257 extern size_t shortcutslen;
258 extern KeySym mappedkeys[];
259 extern size_t mappedkeyslen;
260 extern uint ignoremod;
261 extern uint forceselmod;
262 extern Key key[];
263 extern size_t keyslen;
264 extern uint selmasks[];
265 extern size_t selmaskslen;
266 extern char ascii_printable[];