1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
/* See LICENSE for license details. */
#pragma once
#include <u.h>
#include <libn.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/wait.h>
// -----------------------------------------------------------------------
// macros
#define BETWEEN(x, a, b) ((a) <= (x) && (x) <= (b))
#define DIVCEIL(n, d) (((n) + ((d) - 1)) / (d))
#define DEFAULT(a, b) (a) = (a) ? (a) : (b)
#define LIMIT(x, a, b) (x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)
#define GLYPHCMP(a, b) ((a).mode != (b).mode || (a).fg != (b).fg || (a).bg != (b).bg)
#define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + (t1.tv_nsec-t2.tv_nsec)/1E6)
#define MODBIT(x, set, bit) ((set) ? ((x) |= (bit)) : ((x) &= ~(bit)))
#define TRUECOLOR(r,g,b) (1 << 24 | (r) << 16 | (g) << 8 | (b))
#define IS_TRUECOL(x) (1 << 24 & (x))
#define iota(x) 1 << (x)
/* arbitrary sizes */
#define ESC_BUF_SIZ (128*UTFmax)
#define ESC_ARG_SIZ 16
#define STR_BUF_SIZ ESC_BUF_SIZ
#define STR_ARG_SIZ ESC_ARG_SIZ
// -----------------------------------------------------------------------
// constants
enum {
Gnil,
Gbold = iota(0),
Gfaint = iota(1),
Gitalic = iota(2),
Gunline = iota(3),
Gblink = iota(4),
Greverse = iota(5),
Ginvisible = iota(6),
Gstruck = iota(7),
Gwrap = iota(8),
Gwide = iota(9),
Gwdummy = iota(10),
Gboldfaint = Gbold | Gfaint,
};
enum {
SelIdle = 0,
SelEmpty = 1,
SelReady = 2
};
enum {
SelRegular = 1,
SelRectangular = 2
};
enum {
SnapWord = 1,
SnapLine = 2
};
/* cursor state */
enum {
CursorSave,
CursorLoad
};
/* cursor mode */
enum {
CursorDefault = 0,
CursorWrap = 1,
CursorOrigin = 2
};
/* character set */
enum {
CSgfx0,
CSgfx1,
CSuk,
CSusa,
CSmulti,
CSger,
CSfin,
};
/* escape sequences */
enum {
Xstart = 1,
Xcsi = 2,
Xstr = 4, /* OSC, PM, APC */
Xaltcs = 8,
Xstrend = 16, /* a final string was encountered */
Xtest = 32, /* Enter in test mode */
Xutf8 = 64,
Xdcs =128,
};
/* terminal mode */
enum {
Twrap = iota(0),
Tinsert = iota(1),
Taltscreen = iota(2),
Tcrlf = iota(3),
Techo = iota(4),
Tprint = iota(5),
Tutf8 = iota(6),
Tsixel = iota(7),
};
/* window mode */
enum {
Wvisible = iota(0),
Wfocused = iota(1),
Wappkeypad = iota(2),
Wmousebtn = iota(3),
Wmousemotion = iota(4),
Wreverse = iota(5),
Wkbdblock = iota(6),
Whide = iota(7),
Wappcursor = iota(8),
Wmousesgr = iota(9),
W8bit = iota(10),
Wblink = iota(11),
Wbflink = iota(12),
Wfocus = iota(13),
Wmousex10 = iota(14),
Wmousemany = iota(15),
Wbrcktpaste = iota(16),
Wnumlock = iota(17),
Wmouse = Wmousebtn|Wmousemotion|Wmousex10|Wmousemany,
};
// -----------------------------------------------------------------------
// types
/* term.c */
typedef struct Letter Letter;
typedef struct Dot Dot;
typedef struct Selection Selection;
typedef struct Terminal Terminal;
typedef union Arg Arg;
struct Letter {
rune u; /* character code */
ushort mode; /* attribute flags */
uint32 fg; /* foreground */
uint32 bg; /* background */
};
struct Dot {
Letter attr; /* current char attributes */
int x;
int y;
char state;
};
struct Selection {
int mode;
int type;
int snap;
/*
* Selection variables:
* nb – normalized coordinates of the beginning of the selection
* ne – normalized coordinates of the end of the selection
* ob – original coordinates of the beginning of the selection
* oe – original coordinates of the end of the selection
*/
struct {
int x, y;
} nb, ne, ob, oe;
int alt;
};
/* Internal representation of the screen */
struct Terminal {
int row; /* nb row */
int col; /* nb col */
Letter **line; /* screen */
Letter **alt; /* alternate screen */
int *dirty; /* dirtyness of lines */
Dot c; /* cursor */
int ocx; /* old cursor col */
int ocy; /* old cursor row */
int top; /* top scroll limit */
int bot; /* bottom scroll limit */
int mode; /* terminal mode flags */
int esc; /* escape state flags */
char trantbl[4];/* charset table translation */
int charset; /* current charset */
int icharset; /* selected charset for sequence */
int *tabs;
rune lastc; /* last printed char outside of sequence, 0 if control */
};
/* CSI Escape sequence structs */
/* ESC '[' [[ [<priv>] <arg> [;]] <mode> [<mode>]] */
typedef struct {
char buf[ESC_BUF_SIZ]; /* raw string */
ulong len; /* raw string length */
char priv;
int arg[ESC_ARG_SIZ];
int narg; /* nb of args */
char mode[2];
} CSIEscape;
/* STR Escape sequence structs */
/* ESC type [[ [<priv>] <arg> [;]] <mode>] ESC '\' */
typedef struct {
char type; /* ESC type ... */
char *buf; /* allocated raw string */
size_t siz; /* allocation size */
size_t len; /* raw string length */
char *args[STR_ARG_SIZ];
int narg; /* nb of args */
} STREscape;
/* x.c */
typedef struct TermWindow TermWindow;
struct TermWindow {
int tw, th; /* tty width and height */
int w, h; /* window width and height */
int hb, vb; /* horizontal and vertical border (in pix) */
int ch; /* char height */
int cw; /* char width */
int mode; /* window state/mode flags */
int cursor; /* cursor style */
};
/* used for user hooks */
union Arg {
int i;
uint ui;
float f;
void *v;
char *s;
};
// -----------------------------------------------------------------------
// x.c (backend functions)
void xbell(void);
void xclipcopy(void);
void xdrawcursor(int, int, Letter, int, int, Letter);
void xdrawline(Letter*, int, int, int);
void xfinishdraw(void);
void xloadcols(void);
int xsetcolorname(int, char *);
void xsettitle(char *);
int xsetcursor(int);
void xsetmode(int, uint);
void xsetpointermotion(int);
void xsetsel(char *);
int xstartdraw(void);
void xximspot(int, int);
void fatal( char *, ...);
void redraw(void);
void draw(void);
void printscreen(Arg *);
void printsel(Arg *);
void sendbreak(Arg *);
void toggleprinter(Arg *);
int tattrset(int);
void tnew(int, int);
void tresize(int, int);
void tsetdirtattr(int);
void ttyhangup(void);
int ttynew(char *, char *, char *, char **);
ulong ttyread(void);
void ttyresize(int, int);
void ttywrite( char *, size_t, int);
void resettitle(void);
void selclear(void);
void selinit(void);
void selstart(int, int, int);
void selextend(int, int, int, int);
int selected(int, int);
char *getsel(void);
void *xmalloc(size_t);
void *xrealloc(void *, size_t);
char *xstrdup(char *);
/* config.h globals */
extern char *utmp;
extern char *scroll;
extern char *stty_args;
extern char *vtiden;
extern wchar *worddelimiters;
extern int allowaltscreen;
extern int allowwindowops;
extern char *termname;
extern uint tabspaces;
extern uint defaultfg;
extern uint defaultbg;
extern float alpha;
|