4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.7 2008/02/06 20:26:57 james
14 * *** empty log message ***
16 * Revision 1.6 2008/02/06 17:53:28 james
17 * *** empty log message ***
19 * Revision 1.5 2008/02/06 15:53:22 james
20 * *** empty log message ***
22 * Revision 1.4 2008/02/04 20:23:55 james
23 * *** empty log message ***
25 * Revision 1.3 2008/02/04 05:45:55 james
28 * Revision 1.2 2008/02/04 02:05:06 james
29 * *** empty log message ***
31 * Revision 1.1 2008/02/03 23:31:25 james
32 * *** empty log message ***
38 ansi_write (ANSI * a, char *buf, int n)
40 write (a->fd, buf, n);
44 ansi_getsize (ANSI * a)
46 struct winsize sz = { 0 };
47 if (ioctl (a->fd, TIOCGWINSZ, &sz))
54 a->size.x = sz.ws_col;
55 a->size.y = sz.ws_row;
62 ansi_move (ANSI * a, CRT_Pos p)
66 int dx = p.x - a->pos.x;
67 int dy = p.y - a->pos.y;
69 // a->pos.x = ANSI_INVAL;
71 if (a->pos.x != ANSI_INVAL)
81 ansi_write (a, "\033[C", 3);
85 ansi_write (a, "\033[D", 3);
89 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
90 ansi_write (a, buf, n);
97 ansi_write (a, "\033[A", 3);
101 ansi_write (a, "\033[B", 3);
105 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
106 ansi_write (a, buf, n);
110 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
111 ansi_write (a, buf, n);
118 ansi_write (a, "\033[E", 3);
122 ansi_write (a, "\033[F", 3);
126 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
127 ansi_write (a, buf, n);
131 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
132 ansi_write (a, buf, n);
137 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
138 ansi_write (a, buf, n);
143 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
144 ansi_write (a, buf, n);
152 ansi_showhide_cursor (ANSI * a, int hide)
154 if (a->hide_cursor == hide)
159 ansi_write (a, "\033[?25l", 6);
163 ansi_write (a, "\033[?25h", 6);
166 a->hide_cursor = hide;
171 ansi_force_attr_normal (ANSI * a)
173 ansi_write (a, "\033[0m", 4);
174 a->attr = CRT_ATTR_NORMAL;
178 ansi_set_attr (ANSI * a, int attr)
182 dif = attr ^ a->attr;
189 if (attr == CRT_ATTR_NORMAL)
191 ansi_force_attr_normal (a);
195 if (dif & CRT_ATTR_UNDERLINE)
197 if (attr & CRT_ATTR_UNDERLINE)
199 ansi_write (a, "\033[4m", 4);
203 ansi_write (a, "\033[24m", 5);
206 if (dif & CRT_ATTR_REVERSE)
208 if (attr & CRT_ATTR_REVERSE)
210 ansi_write (a, "\033[7m", 4);
214 ansi_write (a, "\033[27m", 5);
217 if (dif & CRT_ATTR_BOLD)
219 if (attr & CRT_ATTR_BOLD)
221 ansi_write (a, "\033[1m", 4);
225 ansi_write (a, "\033[21m", 5);
226 ansi_write (a, "\033[22m", 5);
234 ansi_render (ANSI * a, CRT_CA ca)
243 ansi_set_attr (a, ca.attr);
245 ansi_write (a, &ca.chr, 1);
249 /*Can't easily wrap round here as don't know size of destination screen*/
250 /*so invalidate the cached cursor position*/
252 if (a->pos.x >= CRT_COLS)
253 a->pos.x = ANSI_INVAL;
264 ansi_force_attr_normal (a);
266 ansi_write (a, "\033[2J", 4);
267 /*different emulators leave cursor in different places after cls differently*/
268 a->pos.x = ANSI_INVAL;
273 ansi_draw (ANSI * a, CRT * c)
277 int hidden_cursor = 0;
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))
302 char msg[] = "Window is too small";
306 ansi_showhide_cursor (a, 1);
307 ansi_set_attr (a, CRT_ATTR_REVERSE);
310 ansi_write (a, msg, sizeof (msg));
311 a->pos.x = ANSI_INVAL;
315 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
317 ansi_showhide_cursor (a, 1);
322 ansi_move (a, a->crt.pos);
324 a->crt.hide_cursor = c->hide_cursor;
325 ansi_showhide_cursor (a, a->crt.hide_cursor);
329 ansi_reset (ANSI * a)
331 // FIXME: -- echos back crap?
332 // ansi_write (a, "\033[c", 3);
335 a->pos.x = ANSI_INVAL;
336 a->hide_cursor = ANSI_INVAL;
341 ansi_draw (a, &a->crt);
344 void ansi_parse_char(ANSI *a,int c,VT102 *v)
349 void ansi_parse(ANSI *a,char *buf,int len,VT102 *v)
352 ansi_parse_char(a,*(buf++),v);
355 int ansi_dispatch(ANSI *a,VT102 *v)
360 red=read(a.fd,buf,sizeof(buf));
361 if (red<0) return -1;
364 ansi_parse(a,buf,red,v);