term: wait for input if not available
[fbpad.git] / term.c
blobbbd05123806371dc0cac3c40f57a8b020b249399
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_NOAUTOCR 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),
291 nc * sizeof(*screen));
292 if (n > 0)
293 memset(SQRADDR(row, sc), 0, n * sizeof(*screen));
294 else
295 memset(SQRADDR(row, pad_rows() + n), 0, -n * sizeof(*screen));
296 lazy_drawcols(row, MIN(sc, sc + n), pad_cols());
297 lazy_cursor(1);
300 static void delete_chars(int n)
302 move_chars(col + n, pad_cols(), -n);
305 static void insert_chars(int n)
307 int nc = pad_cols() - col - n;
308 move_chars(col, nc, n);
311 static void term_blank(void)
313 memset(screen, 0, MAXCHARS * sizeof(*screen));
314 if (visible) {
315 pad_blank(bg);
316 lazy_clean();
320 static void ctlseq(void);
321 void term_read(void)
323 ctlseq();
324 lazy = 1;
325 while (ptycur < ptylen)
326 ctlseq();
327 lazy_flush();
328 lazy = 0;
331 void term_exec(char *cmd)
333 bot = pad_rows();
334 if ((term->pid = forkpty(&term->fd, NULL, NULL, NULL)) == -1) {
335 perror("failed to fork");
336 term->fd = 0;
337 return;
339 if (!term->pid) {
340 setenv("TERM", "linux", 1);
341 execlp(cmd, cmd, NULL);
342 exit(1);
344 fcntl(term->fd, F_SETFD, fcntl(term->fd, F_GETFD) | FD_CLOEXEC);
345 fcntl(term->fd, F_SETFL, fcntl(term->fd, F_GETFL) | O_NONBLOCK);
346 setsize();
347 setmode(0);
348 term_blank();
351 static void misc_save(struct term_state *state)
353 state->row = row;
354 state->col = col;
355 state->fg = fg;
356 state->bg = bg;
357 state->top = top;
358 state->bot = bot;
359 state->mode = mode;
362 static void misc_load(struct term_state *state)
364 row = state->row;
365 col = state->col;
366 fg = state->fg;
367 bg = state->bg;
368 top = state->top;
369 bot = state->bot;
370 mode = state->mode;
373 void term_save(struct term *term)
375 visible = 0;
376 misc_save(&term->cur);
379 void term_load(struct term *t, int flags)
381 term = t;
382 misc_load(&term->cur);
383 screen = term->screen;
384 visible = flags;
385 if (term->fd && flags == TERM_REDRAW) {
386 lazy_draw(0, pad_rows());
387 lazy_cursor(1);
389 if (!term->fd && flags)
390 pad_blank(0);
393 void term_end(void)
395 term->fd = 0;
396 row = col = 0;
397 fg = 0;
398 bg = 0;
399 term_blank();
402 void set_region(int t, int b)
404 top = MIN(pad_rows(), MAX(0, t - 1));
405 bot = MIN(pad_rows(), MAX(0, b ? b : pad_rows()));
406 if (origin())
407 move_cursor(top, 0);
410 #include "vt102.c"