4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.2 2008/02/04 02:05:06 james
14 * *** empty log message ***
16 * Revision 1.1 2008/02/03 23:31:25 james
17 * *** empty log message ***
23 ansi_write (ANSI * a, char *buf, int n)
25 write (a->fd, buf, n);
31 ansi_move (ANSI * a, CRT_Pos p)
35 int dx = a->pos.x - p.x;
36 int dy = a->pos.y - p.y;
39 if (a->pos.x != ANSI_INVAL)
49 ansi_write (a, "\033[C", 3);
53 ansi_write (a, "\033[D", 3);
57 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
58 ansi_write (a, buf, n);
65 ansi_write (a, "\033[A", 3);
69 ansi_write (a, "\033[B", 3);
73 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
74 ansi_write (a, buf, n);
78 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
79 ansi_write (a, buf, n);
86 ansi_write (a, "\033[E", 3);
90 ansi_write (a, "\033[F", 3);
94 n = snprintf (buf, sizeof (buf), "\033[%dE", -dy);
95 ansi_write (a, buf, n);
99 n = snprintf (buf, sizeof (buf), "\033[%dF", dy);
100 ansi_write (a, buf, n);
105 n = snprintf (buf, sizeof (buf), "\033[%d;%dHF", p.y + 1, p.x + 1);
106 ansi_write (a, buf, n);
111 n = snprintf (buf, sizeof (buf), "\033[%d;%dHF", p.y + 1, p.x + 1);
112 ansi_write (a, buf, n);
120 ansi_showhide_cursor (ANSI * a, int hide)
122 if (a->hide_cursor == hide)
127 ansi_write (a, "\033[?25h", 6);
131 ansi_write (a, "\033[?25l", 6);
134 a->hide_cursor = hide;
139 ansi_force_attr_normal (ANSI * a)
141 ansi_write (a, "\033[0m", 4);
142 a->attr = CRT_ATTR_NORMAL;
146 ansi_set_attr (ANSI * a, int attr)
150 dif = attr ^ a->attr;
155 if (attr == CRT_ATTR_NORMAL)
157 ansi_force_attr_normal (a);
161 if (dif & CRT_ATTR_UNDERLINE)
163 if (attr & CRT_ATTR_UNDERLINE)
165 ansi_write (a, "\033[4m", 4);
169 ansi_write (a, "\033[24m", 5);
172 if (dif & CRT_ATTR_REVERSE)
174 if (attr & CRT_ATTR_REVERSE)
176 ansi_write (a, "\033[7m", 4);
180 ansi_write (a, "\033[27m", 5);
183 if (dif & CRT_ATTR_BOLD)
185 if (attr & CRT_ATTR_REVERSE)
187 ansi_write (a, "\033[1m", 4);
191 ansi_write (a, "\033[22m", 5);
198 ansi_render (ANSI * a, CRT_CA ca)
207 ansi_set_attr (a, ca.attr);
209 ansi_write (a, &ca.chr, 1);
213 /*Can't easily wrap round here as don't know size of destination screen*/
214 /*so invalidate the cached cursor position*/
216 if (a->pos.x >= CRT_COLS)
217 a->pos.x = ANSI_INVAL;
227 ansi_force_attr_normal (a);
229 ansi_write (a, "\033[2J", 4);
230 /*different emulators leave cursor in different places after cls differently*/
231 a->pos.x = ANSI_INVAL;
236 ansi_draw (ANSI * a, CRT * c)
241 ansi_showhide_cursor (a, 1);
243 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
245 if (p.y >= a->size.y)
247 o = CRT_ADDR (p.y, 0);
248 for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
250 if (p.x >= a->size.x)
252 if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
254 a->crt.screen[o] = c->screen[o];
257 ansi_render (a, a->crt.screen[o]);
263 ansi_move (a, a->crt.pos);
265 a->crt.hide_cursor = c->hide_cursor;
266 ansi_showhide_cursor (a, a->crt.hide_cursor);
270 ansi_reset (ANSI * a)
272 ansi_write (a, "\033[c", 3);
274 a->pos.x = ANSI_INVAL;
275 a->hide_cursor = ANSI_INVAL;
280 ansi_draw (a, &a->crt);