term: reset term state before exec
[fbpad.git] / term.c
blob2980d0820fa552124c52ba77f4f6503e47b126d8
1 #include <ctype.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <poll.h>
5 #include <pty.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11 #include "config.h"
12 #include "pad.h"
13 #include "util.h"
14 #include "term.h"
16 #define MODE_NOCURSOR 0x01
17 #define MODE_NOWRAP 0x02
18 #define MODE_ORIGIN 0x04
19 #define MODE_AUTOCR 0x08
20 #define BIT_SET(i, b, val) ((val) ? ((i) | (b)) : ((i) & ~(b)))
21 #define SQRADDR(r, c) (screen + (r) * pad_cols() + (c))
23 static struct term *term;
24 static struct square *screen;
25 static int row, col;
26 static int fg, bg;
27 static int top, bot;
28 static int mode;
29 static int visible;
31 #define MAXLINES (1 << 13)
32 static int dirty[MAXLINES];
33 static int lazy;
35 static void _term_show(int r, int c, int cursor)
37 struct square *sqr = SQRADDR(r, c);
38 int fgcolor = sqr->c ? sqr->fg : fg;
39 int bgcolor = sqr->c ? sqr->bg : bg;
40 if (cursor && !(mode & MODE_NOCURSOR)) {
41 int t = fgcolor;
42 fgcolor = bgcolor;
43 bgcolor = t;
45 if (visible)
46 pad_put(sqr->c, r, c, fgcolor, bgcolor);
49 static void _draw_row(int r)
51 int i;
52 for (i = 0; i < pad_cols(); i++)
53 _term_show(r, i, 0);
56 static void lazy_draw(int sr, int er)
58 int i;
59 if (!visible)
60 return;
61 for (i = sr; i < er; i++) {
62 if (lazy)
63 dirty[i] = 1;
64 else
65 _draw_row(i);
69 static void lazy_drawcols(int r, int sc, int ec)
71 int i;
72 if (!visible)
73 return;
74 if (lazy) {
75 dirty[r] = 1;
76 } else {
77 for (i = sc; i < ec; i++)
78 _term_show(r, i, 0);
82 static void lazy_put(int ch, int r, int c)
84 struct square *sqr = SQRADDR(r, c);
85 sqr->c = ch;
86 sqr->fg = fg;
87 sqr->bg = bg;
88 if (!visible)
89 return;
90 if (lazy)
91 dirty[r] = 1;
92 else
93 _term_show(r, c, 0);
96 static void lazy_cursor(int put)
98 if (!visible)
99 return;
100 if (lazy)
101 dirty[row] = 1;
102 else
103 _term_show(row, col, put);
106 static void lazy_clean()
108 if (visible)
109 memset(dirty, 0, sizeof(*dirty) * MAXLINES);
112 static void lazy_flush()
114 int i;
115 if (!visible)
116 return;
117 _term_show(row, col, 0);
118 for (i = 0; i < pad_rows(); i++)
119 if (dirty[i])
120 _draw_row(i);
121 lazy_clean();
122 _term_show(row, col, 1);
125 static int origin(void)
127 return mode & MODE_ORIGIN;
130 static void setsize(void)
132 struct winsize size;
133 size.ws_col = pad_cols();
134 size.ws_row = pad_rows();
135 size.ws_xpixel = 0;
136 size.ws_ypixel = 0;
137 ioctl(term->fd, TIOCSWINSZ, &size);
140 #define PTYBUFSIZE (1 << 13)
141 static char ptybuf[PTYBUFSIZE];
142 static int ptylen;
143 static int ptycur;
144 static void waitpty(void)
146 struct pollfd ufds[1];
147 ufds[0].fd = term->fd;
148 ufds[0].events = POLLIN;
149 poll(ufds, 1, -1);
152 static int readpty(void)
154 if (ptycur < ptylen)
155 return ptybuf[ptycur++];
156 ptylen = read(term->fd, ptybuf, PTYBUFSIZE);
157 if (ptylen == -1 && errno == EAGAIN) {
158 waitpty();
159 ptylen = read(term->fd, ptybuf, PTYBUFSIZE);
161 if (ptylen > 0) {
162 ptycur = 1;
163 return ptybuf[0];
165 return -1;
168 static void empty_rows(int sr, int er)
170 memset(SQRADDR(sr, 0), 0, (er - sr) * sizeof(*screen) * pad_cols());
173 static void blank_rows(int sr, int er)
175 empty_rows(sr, er);
176 lazy_draw(sr, er);
177 lazy_cursor(1);
180 static void scroll_screen(int sr, int nr, int n)
182 lazy_cursor(0);
183 memmove(SQRADDR(sr + n, 0), SQRADDR(sr, 0),
184 nr * pad_cols() * sizeof(*screen));
185 if (n > 0)
186 empty_rows(sr, sr + n);
187 else
188 empty_rows(sr + nr + n, sr + nr);
189 lazy_draw(MIN(sr, sr + n), MAX(sr + nr, sr + nr + n));
190 lazy_cursor(1);
193 static void insert_lines(int n)
195 int sr = MAX(top, row);
196 int nr = bot - row - n;
197 if (nr > 0)
198 scroll_screen(sr, nr, n);
201 static void delete_lines(int n)
203 int r = MAX(top, row);
204 int sr = r + n;
205 int nr = bot - r - n;
206 if (nr > 0)
207 scroll_screen(sr, nr, -n);
210 static void move_cursor(int r, int c)
212 int t, b;
213 lazy_cursor(0);
214 t = origin() ? top : 0;
215 b = origin() ? bot : pad_rows();
216 row = MAX(t, MIN(r, b - 1));
217 col = MAX(0, MIN(c, pad_cols() - 1));
218 lazy_cursor(1);
221 static void advance(int dr, int dc, int scrl)
223 int r = row + dr;
224 int c = col + dc;
225 if (c >= pad_cols()) {
226 if (!scrl || (mode & MODE_NOWRAP)) {
227 c = pad_cols() - 1;
228 } else {
229 r++;
230 c = 0;
233 if (r >= bot && scrl) {
234 int n = bot - r - 1;
235 int nr = (bot - top) + n;
236 scroll_screen(top + -n, nr, n);
238 if (r < top && scrl) {
239 int n = top - r;
240 int nr = (bot - top) - n;
241 scroll_screen(top, nr, n);
243 r = MIN(bot - 1, MAX(top, r));
244 move_cursor(r, MAX(0, c));
247 void term_send(int c)
249 unsigned char b = (unsigned char) c;
250 if (term->fd)
251 write(term->fd, &b, 1);
254 void term_sendstr(char *s)
256 if (term->fd)
257 write(term->fd, s, strlen(s));
260 static void setmode(int m)
262 if (m == 0) {
263 fg = FGCOLOR;
264 bg = BGCOLOR;
266 if (m == 1)
267 fg = fg | 0x08;
268 if (m == 7) {
269 int t = fg;
270 fg = bg;
271 bg = t;
273 if (m >= 30 && m <= 37)
274 fg = m - 30;
275 if (m >= 40 && m <= 47)
276 bg = m - 40;
279 static void kill_chars(int sc, int ec)
281 int i;
282 for (i = sc; i < ec; i++)
283 lazy_put(' ', row, i);
284 lazy_cursor(1);
287 static void move_chars(int sc, int nc, int n)
289 lazy_cursor(0);
290 memmove(SQRADDR(row, sc + n), SQRADDR(row, sc), nc * sizeof(*screen));
291 if (n > 0)
292 memset(SQRADDR(row, sc), 0, n * sizeof(*screen));
293 else
294 memset(SQRADDR(row, pad_cols() + n), 0, -n * sizeof(*screen));
295 lazy_drawcols(row, MIN(sc, sc + n), pad_cols());
296 lazy_cursor(1);
299 static void delete_chars(int n)
301 int sc = col + n;
302 int nc = pad_cols() - sc;
303 move_chars(sc, nc, -n);
306 static void insert_chars(int n)
308 int nc = pad_cols() - col - n;
309 move_chars(col, nc, n);
312 static void term_blank(void)
314 memset(screen, 0, MAXCHARS * sizeof(*screen));
315 if (visible) {
316 pad_blank(bg);
317 lazy_clean();
321 static void ctlseq(void);
322 void term_read(void)
324 ctlseq();
325 lazy = 1;
326 while (ptycur < ptylen)
327 ctlseq();
328 lazy_flush();
329 lazy = 0;
332 void term_exec(char *cmd)
334 memset(term, 0, sizeof(*term));
335 term->cur.bot = term->sav.bot = pad_rows();
336 term_load(term, visible);
337 if ((term->pid = forkpty(&term->fd, NULL, NULL, NULL)) == -1) {
338 perror("failed to fork");
339 term->fd = 0;
340 return;
342 if (!term->pid) {
343 setenv("TERM", "linux", 1);
344 execlp(cmd, cmd, NULL);
345 exit(1);
347 fcntl(term->fd, F_SETFD, fcntl(term->fd, F_GETFD) | FD_CLOEXEC);
348 fcntl(term->fd, F_SETFL, fcntl(term->fd, F_GETFL) | O_NONBLOCK);
349 setsize();
350 setmode(0);
351 term_blank();
354 static void misc_save(struct term_state *state)
356 state->row = row;
357 state->col = col;
358 state->fg = fg;
359 state->bg = bg;
360 state->top = top;
361 state->bot = bot;
362 state->mode = mode;
365 static void misc_load(struct term_state *state)
367 row = state->row;
368 col = state->col;
369 fg = state->fg;
370 bg = state->bg;
371 top = state->top;
372 bot = state->bot;
373 mode = state->mode;
376 void term_save(struct term *term)
378 visible = 0;
379 misc_save(&term->cur);
382 void term_load(struct term *t, int flags)
384 term = t;
385 misc_load(&term->cur);
386 screen = term->screen;
387 visible = flags;
388 if (term->fd && flags == TERM_REDRAW) {
389 lazy_draw(0, pad_rows());
390 lazy_cursor(1);
392 if (!term->fd && flags)
393 pad_blank(0);
396 void term_end(void)
398 term->fd = 0;
399 row = col = 0;
400 fg = 0;
401 bg = 0;
402 term_blank();
405 void set_region(int t, int b)
407 top = MIN(pad_rows(), MAX(0, t - 1));
408 bot = MIN(pad_rows(), MAX(0, b ? b : pad_rows()));
409 if (origin())
410 move_cursor(top, 0);
413 #include "vt102.c"