4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.6 2008/02/06 17:53:28 james
14 * *** empty log message ***
16 * Revision 1.5 2008/02/06 15:53:22 james
17 * *** empty log message ***
19 * Revision 1.4 2008/02/04 20:23:55 james
20 * *** empty log message ***
22 * Revision 1.3 2008/02/04 05:45:55 james
25 * Revision 1.2 2008/02/04 02:05:06 james
26 * *** empty log message ***
28 * Revision 1.1 2008/02/03 23:31:25 james
29 * *** empty log message ***
35 ansi_write (ANSI * a, char *buf, int n)
37 write (a->fd, buf, n);
41 ansi_getsize (ANSI * a)
43 struct winsize sz = { 0 };
44 if (ioctl (a->fd, TIOCGWINSZ, &sz))
51 a->size.x = sz.ws_col;
52 a->size.y = sz.ws_row;
59 ansi_move (ANSI * a, CRT_Pos p)
63 int dx = p.x - a->pos.x;
64 int dy = p.y - a->pos.y;
66 // a->pos.x = ANSI_INVAL;
68 if (a->pos.x != ANSI_INVAL)
78 ansi_write (a, "\033[C", 3);
82 ansi_write (a, "\033[D", 3);
86 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
87 ansi_write (a, buf, n);
94 ansi_write (a, "\033[A", 3);
98 ansi_write (a, "\033[B", 3);
102 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
103 ansi_write (a, buf, n);
107 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
108 ansi_write (a, buf, n);
115 ansi_write (a, "\033[E", 3);
119 ansi_write (a, "\033[F", 3);
123 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
124 ansi_write (a, buf, n);
128 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
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);
140 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
141 ansi_write (a, buf, n);
149 ansi_showhide_cursor (ANSI * a, int hide)
151 if (a->hide_cursor == hide)
156 ansi_write (a, "\033[?25l", 6);
160 ansi_write (a, "\033[?25h", 6);
163 a->hide_cursor = hide;
168 ansi_force_attr_normal (ANSI * a)
170 ansi_write (a, "\033[0m", 4);
171 a->attr = CRT_ATTR_NORMAL;
175 ansi_set_attr (ANSI * a, int attr)
179 dif = attr ^ a->attr;
186 if (attr == CRT_ATTR_NORMAL)
188 ansi_force_attr_normal (a);
192 if (dif & CRT_ATTR_UNDERLINE)
194 if (attr & CRT_ATTR_UNDERLINE)
196 ansi_write (a, "\033[4m", 4);
200 ansi_write (a, "\033[24m", 5);
203 if (dif & CRT_ATTR_REVERSE)
205 if (attr & CRT_ATTR_REVERSE)
207 ansi_write (a, "\033[7m", 4);
211 ansi_write (a, "\033[27m", 5);
214 if (dif & CRT_ATTR_BOLD)
216 if (attr & CRT_ATTR_BOLD)
218 ansi_write (a, "\033[1m", 4);
222 ansi_write (a, "\033[21m", 5);
223 ansi_write (a, "\033[22m", 5);
231 ansi_render (ANSI * a, CRT_CA ca)
240 ansi_set_attr (a, ca.attr);
242 ansi_write (a, &ca.chr, 1);
246 /*Can't easily wrap round here as don't know size of destination screen*/
247 /*so invalidate the cached cursor position*/
249 if (a->pos.x >= CRT_COLS)
250 a->pos.x = ANSI_INVAL;
261 ansi_force_attr_normal (a);
263 ansi_write (a, "\033[2J", 4);
264 /*different emulators leave cursor in different places after cls differently*/
265 a->pos.x = ANSI_INVAL;
270 ansi_draw (ANSI * a, CRT * c)
279 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
281 if (p.y >= a->size.y)
283 o = CRT_ADDR (p.y, 0);
284 for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
286 if (p.x >= a->size.x)
288 if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
290 ansi_showhide_cursor (a, 1);
291 a->crt.screen[o] = c->screen[o];
294 ansi_render (a, a->crt.screen[o]);
300 if ((CRT_COLS>a->size.x) || (CRT_ROWS>a->size.y)) {
301 char msg[]="Window is too small";
305 ansi_showhide_cursor (a, 1);
306 ansi_set_attr (a, CRT_ATTR_REVERSE);
309 ansi_write (a, msg, sizeof(msg));
310 a->pos.x = ANSI_INVAL;
314 if ((c->pos.x>=a->size.x)
315 || (c->pos.y>=a->size.y)) {
316 ansi_showhide_cursor (a, 1);
321 ansi_move (a, a->crt.pos);
323 a->crt.hide_cursor = c->hide_cursor;
324 ansi_showhide_cursor (a, a->crt.hide_cursor);
328 ansi_reset (ANSI * a)
330 // FIXME: -- echos back crap?
331 // ansi_write (a, "\033[c", 3);
334 a->pos.x = ANSI_INVAL;
335 a->hide_cursor = ANSI_INVAL;
340 ansi_draw (a, &a->crt);