4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.4 2008/02/04 20:23:55 james
14 * *** empty log message ***
16 * Revision 1.3 2008/02/04 05:45:55 james
19 * Revision 1.2 2008/02/04 02:05:06 james
20 * *** empty log message ***
22 * Revision 1.1 2008/02/03 23:31:25 james
23 * *** empty log message ***
29 ansi_write (ANSI * a, char *buf, int n)
31 write (a->fd, buf, n);
35 ansi_getsize (ANSI * a)
37 struct winsize sz = { 0 };
38 if (ioctl (a->fd, TIOCGWINSZ, &sz))
45 a->size.x = sz.ws_col;
46 a->size.y = sz.ws_row;
53 ansi_move (ANSI * a, CRT_Pos p)
57 int dx = p.x - a->pos.x;
58 int dy = p.y - a->pos.y;
60 // a->pos.x = ANSI_INVAL;
62 if (a->pos.x != ANSI_INVAL)
72 ansi_write (a, "\033[C", 3);
76 ansi_write (a, "\033[D", 3);
80 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
81 ansi_write (a, buf, n);
88 ansi_write (a, "\033[A", 3);
92 ansi_write (a, "\033[B", 3);
96 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
97 ansi_write (a, buf, n);
101 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
102 ansi_write (a, buf, n);
109 ansi_write (a, "\033[E", 3);
113 ansi_write (a, "\033[F", 3);
117 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
118 ansi_write (a, buf, n);
122 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
123 ansi_write (a, buf, n);
128 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
129 ansi_write (a, buf, n);
134 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
135 ansi_write (a, buf, n);
143 ansi_showhide_cursor (ANSI * a, int hide)
145 if (a->hide_cursor == hide)
150 ansi_write (a, "\033[?25l", 6);
154 ansi_write (a, "\033[?25h", 6);
157 a->hide_cursor = hide;
162 ansi_force_attr_normal (ANSI * a)
164 ansi_write (a, "\033[0m", 4);
165 a->attr = CRT_ATTR_NORMAL;
169 ansi_set_attr (ANSI * a, int attr)
173 dif = attr ^ a->attr;
180 if (attr == CRT_ATTR_NORMAL)
182 ansi_force_attr_normal (a);
186 if (dif & CRT_ATTR_UNDERLINE)
188 if (attr & CRT_ATTR_UNDERLINE)
190 ansi_write (a, "\033[4m", 4);
194 ansi_write (a, "\033[24m", 5);
197 if (dif & CRT_ATTR_REVERSE)
199 if (attr & CRT_ATTR_REVERSE)
201 ansi_write (a, "\033[7m", 4);
205 ansi_write (a, "\033[27m", 5);
208 if (dif & CRT_ATTR_BOLD)
210 if (attr & CRT_ATTR_REVERSE)
212 ansi_write (a, "\033[1m", 4);
216 ansi_write (a, "\033[22m", 5);
224 ansi_render (ANSI * a, CRT_CA ca)
233 ansi_set_attr (a, ca.attr);
235 ansi_write (a, &ca.chr, 1);
239 /*Can't easily wrap round here as don't know size of destination screen*/
240 /*so invalidate the cached cursor position*/
242 if (a->pos.x >= CRT_COLS)
243 a->pos.x = ANSI_INVAL;
254 ansi_force_attr_normal (a);
256 ansi_write (a, "\033[2J", 4);
257 /*different emulators leave cursor in different places after cls differently*/
258 a->pos.x = ANSI_INVAL;
263 ansi_draw (ANSI * a, CRT * c)
268 ansi_showhide_cursor (a, 1);
270 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
272 if (p.y >= a->size.y)
274 o = CRT_ADDR (p.y, 0);
275 for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
277 if (p.x >= a->size.x)
279 if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
281 a->crt.screen[o] = c->screen[o];
284 ansi_render (a, a->crt.screen[o]);
290 ansi_move (a, a->crt.pos);
292 a->crt.hide_cursor = c->hide_cursor;
293 ansi_showhide_cursor (a, a->crt.hide_cursor);
297 ansi_reset (ANSI * a)
299 // FIXME: -- echos back crap?
300 // ansi_write (a, "\033[c", 3);
303 a->pos.x = ANSI_INVAL;
304 a->hide_cursor = ANSI_INVAL;
309 ansi_draw (a, &a->crt);