4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.8 2008/02/07 00:39:13 james
14 * *** empty log message ***
16 * Revision 1.7 2008/02/06 20:26:57 james
17 * *** empty log message ***
19 * Revision 1.6 2008/02/06 17:53:28 james
20 * *** empty log message ***
22 * Revision 1.5 2008/02/06 15:53:22 james
23 * *** empty log message ***
25 * Revision 1.4 2008/02/04 20:23:55 james
26 * *** empty log message ***
28 * Revision 1.3 2008/02/04 05:45:55 james
31 * Revision 1.2 2008/02/04 02:05:06 james
32 * *** empty log message ***
34 * Revision 1.1 2008/02/03 23:31:25 james
35 * *** empty log message ***
41 set_nonblocking (int fd)
44 arg = fcntl (fd, F_GETFL, arg);
46 fcntl (fd, F_SETFL, arg);
53 arg = fcntl (fd, F_GETFL, arg);
55 fcntl (fd, F_SETFL, arg);
59 int ansi_read(ANSI *a,void *buf,int n)
63 set_nonblocking(a->fd);
64 red=read(a->fd,buf,n);
68 if ((red==-1) && (errno==EAGAIN)) {
76 ansi_write (ANSI * a, char *buf, int n)
79 write (a->fd, buf, n);
83 ansi_getsize (ANSI * a)
85 struct winsize sz = { 0 };
86 if (ioctl (a->fd, TIOCGWINSZ, &sz))
93 a->size.x = sz.ws_col;
94 a->size.y = sz.ws_row;
101 ansi_move (ANSI * a, CRT_Pos p)
105 int dx = p.x - a->pos.x;
106 int dy = p.y - a->pos.y;
108 // a->pos.x = ANSI_INVAL;
110 if (a->pos.x != ANSI_INVAL)
120 ansi_write (a, "\033[C", 3);
124 ansi_write (a, "\033[D", 3);
128 n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
129 ansi_write (a, buf, n);
136 ansi_write (a, "\033[A", 3);
140 ansi_write (a, "\033[B", 3);
144 n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
145 ansi_write (a, buf, n);
149 n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
150 ansi_write (a, buf, n);
157 ansi_write (a, "\033[E", 3);
161 ansi_write (a, "\033[F", 3);
165 n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
166 ansi_write (a, buf, n);
170 n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
171 ansi_write (a, buf, n);
176 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
177 ansi_write (a, buf, n);
182 n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
183 ansi_write (a, buf, n);
191 ansi_showhide_cursor (ANSI * a, int hide)
193 if (a->hide_cursor == hide)
198 ansi_write (a, "\033[?25l", 6);
202 ansi_write (a, "\033[?25h", 6);
205 a->hide_cursor = hide;
210 ansi_force_attr_normal (ANSI * a)
212 ansi_write (a, "\033[0m", 4);
213 a->attr = CRT_ATTR_NORMAL;
217 ansi_set_attr (ANSI * a, int attr)
221 dif = attr ^ a->attr;
228 if (attr == CRT_ATTR_NORMAL)
230 ansi_force_attr_normal (a);
234 if (dif & CRT_ATTR_UNDERLINE)
236 if (attr & CRT_ATTR_UNDERLINE)
238 ansi_write (a, "\033[4m", 4);
242 ansi_write (a, "\033[24m", 5);
245 if (dif & CRT_ATTR_REVERSE)
247 if (attr & CRT_ATTR_REVERSE)
249 ansi_write (a, "\033[7m", 4);
253 ansi_write (a, "\033[27m", 5);
256 if (dif & CRT_ATTR_BOLD)
258 if (attr & CRT_ATTR_BOLD)
260 ansi_write (a, "\033[1m", 4);
264 ansi_write (a, "\033[21m", 5);
265 ansi_write (a, "\033[22m", 5);
273 ansi_render (ANSI * a, CRT_CA ca)
282 ansi_set_attr (a, ca.attr);
284 ansi_write (a, &ca.chr, 1);
288 /*Can't easily wrap round here as don't know size of destination screen*/
289 /*so invalidate the cached cursor position*/
291 if (a->pos.x >= CRT_COLS)
292 a->pos.x = ANSI_INVAL;
303 ansi_force_attr_normal (a);
305 ansi_write (a, "\033[2J", 4);
306 /*different emulators leave cursor in different places after cls differently*/
307 a->pos.x = ANSI_INVAL;
312 ansi_draw (ANSI * a, CRT * c)
316 int hidden_cursor = 0;
318 for (p.y = 0; p.y < CRT_ROWS; ++p.y)
320 if (p.y >= a->size.y)
322 o = CRT_ADDR (p.y, 0);
323 for (p.x = 0; p.x < CRT_COLS; ++p.x, ++o)
325 if (p.x >= a->size.x)
327 if (crt_ca_cmp (a->crt.screen[o], c->screen[o]))
329 ansi_showhide_cursor (a, 1);
330 a->crt.screen[o] = c->screen[o];
333 ansi_render (a, a->crt.screen[o]);
339 if ((CRT_COLS > a->size.x) || (CRT_ROWS > a->size.y))
341 char msg[] = "Window is too small";
345 ansi_showhide_cursor (a, 1);
346 ansi_set_attr (a, CRT_ATTR_REVERSE);
349 ansi_write (a, msg, sizeof (msg));
350 a->pos.x = ANSI_INVAL;
354 if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
356 ansi_showhide_cursor (a, 1);
361 ansi_move (a, a->crt.pos);
363 a->crt.hide_cursor = c->hide_cursor;
364 ansi_showhide_cursor (a, a->crt.hide_cursor);
368 ansi_reset (ANSI * a)
370 // FIXME: -- echos back crap?
371 // ansi_write (a, "\033[c", 3);
374 a->pos.x = ANSI_INVAL;
375 a->hide_cursor = ANSI_INVAL;
380 ansi_write(a,"\033=",2);
381 ansi_draw (a, &a->crt);
383 void ansi_flush_escape(ANSI *a,VT102 *v)
385 ANSI_Parser *p=&a->parser;
388 for (i=0;i<p->escape_ptr;++i) {
389 vt102_send(v,p->escape_buf[i]);
396 void ansi_parse_deckey(ANSI *a,VT102 *v)
398 ANSI_Parser *p=&a->parser;
399 if ((p->escape_buf[1]!='[') && (p->escape_buf[1]!='O')) {
400 ansi_flush_escape(a,v);
404 if ((p->escape_buf[2]>='A') || (p->escape_buf[2]<='Z')){
405 vt102_send(v,KEY_UP+(p->escape_buf[2]-'A'));
406 }else if ((p->escape_buf[2]>='a') || (p->escape_buf[2]<='z')){
407 vt102_send(v,KEY_154+(p->escape_buf[2]-'a'));
409 ansi_flush_escape(a,v);
416 void ansi_parse_ansikey(ANSI *a,VT102 *v)
418 ANSI_Parser *p=&a->parser;
420 if ((p->escape_buf[1]!='[') ||(p->escape_buf[3]!='~')) {
421 ansi_flush_escape(a,v);
424 if ((p->escape_buf[2]>='0') || (p->escape_buf[2]<='9')){
425 vt102_send(v,KEY_180+(p->escape_buf[2]-'0'));
427 ansi_flush_escape(a,v);
437 void ansi_parse_escape(ANSI *a,VT102 *v)
439 ANSI_Parser *p=&a->parser;
440 switch(p->escape_ptr) {
445 switch (p->escape_buf[1]) {
450 ansi_flush_escape(a,v);
454 switch(p->escape_buf[1]) {
456 ansi_parse_deckey(a,v);
459 if ((p->escape_buf[2]>='A') &&
460 (p->escape_buf[2]<='Z'))
461 ansi_parse_deckey(a,v);
464 ansi_flush_escape(a,v);
468 switch(p->escape_buf[1]) {
470 ansi_parse_ansikey(a,v);
473 ansi_flush_escape(a,v);
477 ansi_flush_escape(a,v);
482 void ansi_check_escape(ANSI *a,VT102 *v)
484 ANSI_Parser *p=&a->parser;
485 struct timeval now,diff;
486 gettimeofday(&now,NULL);
487 timersub(&now,&p->last_escape,&diff);
490 fprintf(stderr,"ie %d tl %d.%06d eb %d\n",
491 p->in_escape,diff.tv_sec,diff.tv_usec,p->escape_ptr);
494 if (!p->in_escape) return;
498 if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
499 ansi_flush_escape(a,v);
504 void ansi_parse_char(ANSI *a,int c,VT102 *v)
506 ANSI_Parser *p=&a->parser;
509 /*See if it's time to flush the escape*/
510 ansi_check_escape(a,v);
514 ansi_flush_escape(a,v);
518 gettimeofday(&p->last_escape,NULL);
522 p->escape_buf[p->escape_ptr++]=c;
523 ansi_parse_escape(a,v);
530 void ansi_parse(ANSI *a,char *buf,int len,VT102 *v)
533 ansi_parse_char(a,*(buf++),v);
536 int ansi_dispatch(ANSI *a,VT102 *v)
541 ansi_check_escape(a,v);
544 red=ansi_read(a,buf,sizeof(buf));
545 if (red<=0) return red;
547 if (*buf==3) return -1;
549 ansi_parse(a,buf,red,v);