4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.5 2008/02/06 15:53:22 james
14 * *** empty log message ***
16 * Revision 1.4 2008/02/04 20:23:55 james
17 * *** empty log message ***
19 * Revision 1.3 2008/02/04 05:45:55 james
22 * Revision 1.2 2008/02/04 02:05:06 james
23 * *** empty log message ***
25 * Revision 1.1 2008/02/03 23:31:25 james
26 * *** empty log message ***
32 ansi_write (ANSI * a, char *buf, int n)
34 write (a->fd, buf, n);
38 ansi_getsize (ANSI * a)
40 struct winsize sz = { 0 };
41 if (ioctl (a->fd, TIOCGWINSZ, &sz))
48 a->size.x = sz.ws_col;
49 a->size.y = sz.ws_row;
56 ansi_move (ANSI * a, CRT_Pos p)
60 int dx = p.x - a->pos.x;
61 int dy = p.y - a->pos.y;
63 // a->pos.x = ANSI_INVAL;
65 if (a->pos.x != ANSI_INVAL)
75 ansi_write (a, "\033[C", 3);
79 ansi_write (a, "\033[D", 3);
83 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
84 ansi_write (a, buf, n);
91 ansi_write (a, "\033[A", 3);
95 ansi_write (a, "\033[B", 3);
99 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
100 ansi_write (a, buf, n);
104 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
105 ansi_write (a, buf, n);
112 ansi_write (a, "\033[E", 3);
116 ansi_write (a, "\033[F", 3);
120 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
121 ansi_write (a, buf, n);
125 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
126 ansi_write (a, buf, n);
131 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
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);
146 ansi_showhide_cursor (ANSI * a, int hide)
148 if (a->hide_cursor == hide)
153 ansi_write (a, "\033[?25l", 6);
157 ansi_write (a, "\033[?25h", 6);
160 a->hide_cursor = hide;
165 ansi_force_attr_normal (ANSI * a)
167 ansi_write (a, "\033[0m", 4);
168 a->attr = CRT_ATTR_NORMAL;
172 ansi_set_attr (ANSI * a, int attr)
176 dif = attr ^ a->attr;
183 if (attr == CRT_ATTR_NORMAL)
185 ansi_force_attr_normal (a);
189 if (dif & CRT_ATTR_UNDERLINE)
191 if (attr & CRT_ATTR_UNDERLINE)
193 ansi_write (a, "\033[4m", 4);
197 ansi_write (a, "\033[24m", 5);
200 if (dif & CRT_ATTR_REVERSE)
202 if (attr & CRT_ATTR_REVERSE)
204 ansi_write (a, "\033[7m", 4);
208 ansi_write (a, "\033[27m", 5);
211 if (dif & CRT_ATTR_BOLD)
213 if (attr & CRT_ATTR_BOLD)
215 ansi_write (a, "\033[1m", 4);
219 ansi_write (a, "\033[22m", 5);
227 ansi_render (ANSI * a, CRT_CA ca)
236 ansi_set_attr (a, ca.attr);
238 ansi_write (a, &ca.chr, 1);
242 /*Can't easily wrap round here as don't know size of destination screen*/
243 /*so invalidate the cached cursor position*/
245 if (a->pos.x >= CRT_COLS)
246 a->pos.x = ANSI_INVAL;
257 ansi_force_attr_normal (a);
259 ansi_write (a, "\033[2J", 4);
260 /*different emulators leave cursor in different places after cls differently*/
261 a->pos.x = ANSI_INVAL;
266 ansi_draw (ANSI * a, CRT * c)
271 ansi_showhide_cursor (a, 1);
273 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
275 if (p.y >= a->size.y)
277 o = CRT_ADDR (p.y, 0);
278 for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
280 if (p.x >= a->size.x)
282 if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
284 a->crt.screen[o] = c->screen[o];
287 ansi_render (a, a->crt.screen[o]);
293 ansi_move (a, a->crt.pos);
295 a->crt.hide_cursor = c->hide_cursor;
296 ansi_showhide_cursor (a, a->crt.hide_cursor);
300 ansi_reset (ANSI * a)
302 // FIXME: -- echos back crap?
303 // ansi_write (a, "\033[c", 3);
306 a->pos.x = ANSI_INVAL;
307 a->hide_cursor = ANSI_INVAL;
312 ansi_draw (a, &a->crt);