From 8ba373fecb91b8f276c754f63a5a7f1490f76dcc Mon Sep 17 00:00:00 2001 From: james Date: Mon, 4 Feb 2008 20:23:55 +0000 Subject: [PATCH] *** empty log message *** --- src/ansi.c | 41 ++++--- src/crt.c | 88 ++++++++++++++- src/crt.h | 7 +- src/libsympathy.c | 42 +++---- src/prototypes.h | 39 +++++++ src/vt102.c | 274 ++++++++++++++++++++++++++++++++++++++++------ src/vt102.h | 9 ++ 7 files changed, 428 insertions(+), 72 deletions(-) diff --git a/src/ansi.c b/src/ansi.c index e7cd934..5e70634 100644 --- a/src/ansi.c +++ b/src/ansi.c @@ -10,6 +10,9 @@ static char rcsid[] = "$Id$"; /* * $Log$ + * Revision 1.4 2008/02/04 20:23:55 james + * *** empty log message *** + * * Revision 1.3 2008/02/04 05:45:55 james * :: * @@ -28,17 +31,20 @@ ansi_write (ANSI * a, char *buf, int n) write (a->fd, buf, n); } -void -ansi_getsize(ANSI *a) +void +ansi_getsize (ANSI * a) { -struct winsize sz={0}; -if (ioctl(a->fd,TIOCGWINSZ,&sz)) { - a->size.x=CRT_COLS; - a->size.y=CRT_ROWS; -} else { - a->size.x=sz.ws_col; - a->size.y=sz.ws_row; -} + struct winsize sz = { 0 }; + if (ioctl (a->fd, TIOCGWINSZ, &sz)) + { + a->size.x = CRT_COLS; + a->size.y = CRT_ROWS; + } + else + { + a->size.x = sz.ws_col; + a->size.y = sz.ws_row; + } } @@ -48,9 +54,10 @@ ansi_move (ANSI * a, CRT_Pos p) { char buf[16]; int n; - int dx = a->pos.x - p.x; - int dy = a->pos.y - p.y; + int dx = p.x - a->pos.x; + int dy = p.y - a->pos.y; +// a->pos.x = ANSI_INVAL; if (a->pos.x != ANSI_INVAL) { @@ -107,12 +114,12 @@ ansi_move (ANSI * a, CRT_Pos p) } else if (dy > 0) { - n = snprintf (buf, sizeof (buf), "\033[%dE", -dy); + n = snprintf (buf, sizeof (buf), "\033[%dE", dy); ansi_write (a, buf, n); } else { - n = snprintf (buf, sizeof (buf), "\033[%dF", dy); + n = snprintf (buf, sizeof (buf), "\033[%dF", -dy); ansi_write (a, buf, n); } } @@ -168,6 +175,8 @@ ansi_set_attr (ANSI * a, int attr) if (!dif) return; + a->attr = attr; + if (attr == CRT_ATTR_NORMAL) { ansi_force_attr_normal (a); @@ -207,6 +216,7 @@ ansi_set_attr (ANSI * a, int attr) ansi_write (a, "\033[22m", 5); } } + } @@ -240,6 +250,7 @@ ansi_cls (ANSI * a) CRT_Pos p = { 0 }; crt_cls (&a->crt); + ansi_force_attr_normal (a); ansi_move (a, p); ansi_write (a, "\033[2J", 4); @@ -287,7 +298,7 @@ ansi_reset (ANSI * a) { // FIXME: -- echos back crap? // ansi_write (a, "\033[c", 3); - ansi_getsize(a); + ansi_getsize (a); a->pos.x = ANSI_INVAL; a->hide_cursor = ANSI_INVAL; diff --git a/src/crt.c b/src/crt.c index 8b7f9d5..36011a9 100644 --- a/src/crt.c +++ b/src/crt.c @@ -10,6 +10,9 @@ static char rcsid[] = "$Id$"; /* * $Log$ + * Revision 1.3 2008/02/04 20:23:55 james + * *** empty log message *** + * * Revision 1.2 2008/02/04 02:05:06 james * *** empty log message *** * @@ -20,16 +23,95 @@ static char rcsid[] = "$Id$"; #include "project.h" +void +crt_erase (CRT * c, CRT_Pos s, CRT_Pos e,int ea) +{ + CRT_CA *ps = &c->screen[CRT_ADDR_POS (&s)]; + CRT_CA *pe = &c->screen[CRT_ADDR_POS (&e)]; + + while (ps <= pe) + { + ps->chr = ' '; + if (ea) + ps->attr = CRT_ATTR_NORMAL; + ps++; + } + +} + void crt_cls (CRT * c) { + CRT_Pos s = { 0, 0 }; + CRT_Pos e = { CRT_COLS - 1, CRT_ROWS - 1 }; int i; - for (i = 0; i < CRT_CELS; ++i) + crt_erase (c, s, e,1); + +#if 0 + for (i = 0; i < CRT_ROWS; ++i) + { + c->screen[CRT_ADDR (i, i)].chr = '@' + i; + c->screen[CRT_ADDR (i, i)].attr = CRT_ATTR_NORMAL; + } +#endif + +} + +void +crt_scroll_up (CRT * c, CRT_Pos s, CRT_Pos e,int ea) +{ + int l, n; + int p; + + s.x=0; + e.x=CRT_COLS-1; + + l = e.x - s.x; + l++; + l *= sizeof (CRT_CA); + + n = e.y - s.y; + + p = CRT_ADDR_POS (&s); + + while (n--) { - c->screen[i].chr = ' '; - c->screen[i].chr = CRT_ATTR_NORMAL; + memcpy (&c->screen[p], &c->screen[p + CRT_COLS], l); + p += CRT_COLS; } + + s.y = e.y; + crt_erase (c, s, e,ea); + +} + +void +crt_scroll_down (CRT * c, CRT_Pos s, CRT_Pos e,int ea) +{ + int l, n; + int p; + + s.x=0; + e.x=CRT_COLS-1; + + l = e.x - s.x; + l++; + l *= sizeof (CRT_CA); + + n = e.y - s.y; + + p = CRT_ADDR_POS (&e); + + while (n--) + { + memcpy (&c->screen[p], &c->screen[p + CRT_COLS], l); + p -= CRT_COLS; + } + + e.y = s.y; + crt_erase (c, s, e,ea); + } void diff --git a/src/crt.h b/src/crt.h index a46affa..ca3fc00 100644 --- a/src/crt.h +++ b/src/crt.h @@ -12,6 +12,9 @@ /* * $Log$ + * Revision 1.2 2008/02/04 20:23:55 james + * *** empty log message *** + * * Revision 1.1 2008/02/03 23:31:25 james * *** empty log message *** * @@ -24,8 +27,8 @@ #define CRT_COLS 80 #define CRT_CELS (CRT_ROWS*CRT_COLS) -#define CRT_ADDR(r,c) (((r)*CRT_ROWS)+(c)) -#define CRT_ADDR_POS(p) ((((p)->y)*CRT_ROWS)+((p)->x)) +#define CRT_ADDR(r,c) (((r)*CRT_COLS)+(c)) +#define CRT_ADDR_POS(p) ((((p)->y)*CRT_COLS)+((p)->x)) #define CRT_ATTR_NORMAL 0x0 #define CRT_ATTR_UNDERLINE 0x1 diff --git a/src/libsympathy.c b/src/libsympathy.c index 013afc7..e86c088 100644 --- a/src/libsympathy.c +++ b/src/libsympathy.c @@ -11,6 +11,9 @@ static char rcsid[] = /* * $Log$ + * Revision 1.4 2008/02/04 20:23:55 james + * *** empty log message *** + * * Revision 1.3 2008/02/04 05:45:55 james * :: * @@ -25,36 +28,37 @@ static char rcsid[] = #include "project.h" -struct termios old={0}; +struct termios old = { 0 }; -static void foo(int not) +static void +foo (int not) { - tcsetattr(0,TCSANOW,&old); - exit(1); + tcsetattr (0, TCSANOW, &old); + exit (1); } void testy (void) { - struct termios raw={0}; + struct termios raw = { 0 }; VT102 v = { 0 }; ANSI a = { 0 }; fd_set rfd; int fd; char c; - - signal(SIGINT,foo); - tcgetattr(0,&old); - tcgetattr(0,&raw); - cfmakeraw(&raw); - tcsetattr(0,TCSANOW,&raw); + signal (SIGINT, foo); + + tcgetattr (0, &old); + tcgetattr (0, &raw); + cfmakeraw (&raw); + tcsetattr (0, TCSANOW, &raw); a.fd = 1; -//vt102_reset(&v); + vt102_reset (&v); ansi_reset (&a); @@ -71,20 +75,20 @@ testy (void) continue; if (FD_ISSET (0, &rfd)) { - read (0, &c, 1); - if (c==3) { - tcsetattr(0,TCSANOW,&old); - exit(1); - } - + if ((read (0, &c, 1) <= 0) || (c == 3)) + break; + write (fd, &c, 1); } if (FD_ISSET (fd, &rfd)) { - read (fd, &c, 1); + if ((read (fd, &c, 1) <= 0)) + break; //write (1, &c, 1); vt102_parse_char (&v, c); ansi_draw (&a, &v.crt); } } + tcsetattr (0, TCSANOW, &old); + printf ("QUAT\n"); } diff --git a/src/prototypes.h b/src/prototypes.h index e69de29..67e3aaf 100644 --- a/src/prototypes.h +++ b/src/prototypes.h @@ -0,0 +1,39 @@ +/* ansi.c */ +void ansi_write(ANSI *a, char *buf, int n); +void ansi_getsize(ANSI *a); +void ansi_move(ANSI *a, CRT_Pos p); +void ansi_showhide_cursor(ANSI *a, int hide); +void ansi_force_attr_normal(ANSI *a); +void ansi_set_attr(ANSI *a, int attr); +void ansi_render(ANSI *a, CRT_CA ca); +void ansi_cls(ANSI *a); +void ansi_draw(ANSI *a, CRT *c); +void ansi_reset(ANSI *a); +/* crt.c */ +void crt_erase(CRT *c, CRT_Pos s, CRT_Pos e, int ea); +void crt_cls(CRT *c); +void crt_scroll_up(CRT *c, CRT_Pos s, CRT_Pos e, int ea); +void crt_scroll_down(CRT *c, CRT_Pos s, CRT_Pos e, int ea); +void crt_reset(CRT *c); +void crt_insert(CRT *c, CRT_CA ca); +/* html.c */ +void html_entity(FILE *f, int c); +void html_render(FILE *f, CRT_CA c); +void html_draw(FILE *f, CRT *c); +/* libsympathy.c */ +struct termios old; +void testy(void); +/* render.c */ +/* testtty.c */ +int open_fd_to_bash(void); +/* version.c */ +/* vt102.c */ +void vt102_cursor_normalize(VT102 *v, int do_wrapscroll, int use_margins); +void vt102_cursor_motion(VT102 *v, int x, int y, int wrapscroll); +void vt102_delete_from_line(VT102 *v, CRT_Pos p); +void vt102_parse_esc(VT102 *v, int c); +void vt102_parse_csi(VT102 *v, char *buf, int len); +void vt102_status_line(VT102 *v, char *str); +void vt102_parse_char(VT102 *v, int c); +void vt102_parser_reset(VT102_parser *p); +void vt102_reset(VT102 *v); diff --git a/src/vt102.c b/src/vt102.c index d29e708..4fb48c5 100644 --- a/src/vt102.c +++ b/src/vt102.c @@ -10,6 +10,9 @@ static char rcsid[] = "$Id$"; /* * $Log$ + * Revision 1.5 2008/02/04 20:23:55 james + * *** empty log message *** + * * Revision 1.4 2008/02/04 05:45:55 james * :: * @@ -136,91 +139,267 @@ csi_starter (int c) return 0; } -void vt102_scroll(VT102 *v,int start,int end) -{ - -} void -vt102_cursor_normalize (VT102 * v, int wrap, int scroll) +vt102_cursor_normalize (VT102 * v, int do_wrapscroll, int use_margins) { - if (v->pos.x < -1) /*don't wrap backwards */ - v->pos.x = 0; + int wrap = do_wrapscroll ? 1 : 0; + int scroll = do_wrapscroll ? 1 : 0; + CRT_Pos *top, *bottom; - if (v->pos.x >= CRT_COLS) + if (use_margins) + { + top = &v->top_margin; + bottom = &v->bottom_margin; + } + else + { + top = &v->screen_start; + bottom = &v->screen_end; + } + + if (v->pos.x < top->x) /*don't wrap backwards */ + v->pos.x = top->x; + + if (v->pos.x > bottom->x) { if (wrap) { - v->pos.x = 0; + v->pos.x = top->x; v->pos.y++; } else { - v->pos.x = CRT_COLS - 1; + v->pos.x = bottom->x; } } - if (v->pos.y < 0) - v->pos.y = 0; + if (v->pos.y < top->y) + v->pos.y = top->y; - if (v->pos.y >= CRT_ROWS) + if (v->pos.y > bottom->y) { if (scroll) - vt102_scroll (v, 0, CRT_ROWS - 1); - v->pos.y = CRT_ROWS - 1; + crt_scroll_up (&v->crt, *top, *bottom, 1); + v->pos.y = bottom->y; } } void -vt102_cursor_motion (VT102 * v, int x, int y, int wrap, int scroll) +vt102_cursor_motion (VT102 * v, int x, int y, int wrapscroll) { while (x > 0) { x--; v->pos.x++; - vt102_cursor_normalize (v, wrap, scroll); + vt102_cursor_normalize (v, wrapscroll, 1); } while (x < 0) { x++; v->pos.x--; - vt102_cursor_normalize (v, wrap, scroll); + vt102_cursor_normalize (v, wrapscroll, 1); } while (y > 0) { y--; v->pos.y++; - vt102_cursor_normalize (v, wrap, scroll); + vt102_cursor_normalize (v, wrapscroll, 1); } while (y < 0) { y++; v->pos.y--; - vt102_cursor_normalize (v, wrap, scroll); + vt102_cursor_normalize (v, wrapscroll, 1); + } +} + +void +vt102_delete_from_line (VT102 * v, CRT_Pos p) +{ + int n = v->bottom_margin.x - p.x; + + if (n < 0) + return; + + if (n) + { + + memmove (&v->crt.screen[CRT_ADDR_POS (&p)], + &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n); } + + v->crt.screen[CRT_ADDR (v->bottom_margin.x, p.y)].chr = ' '; +/*But not attr due to vt102 bug*/ } -void vt102_parse_esc(VT102 *v,int c) + +void +vt102_parse_esc (VT102 * v, int c) { -fprintf(stderr, "ESC %d(%c)\n",c,c); + fprintf (stderr, "ESC 0%o(%c)\n", c, c); +} + +void +vt102_parse_csi (VT102 * v, char *buf, int len) +{ + char last; + char *ptr; + char *arg = buf + 1; + int narg; + + buf[len] = 0; + + last = buf[len - 1]; +#if 0 + buf[len - 1] = 0; +#endif + + if (len>2) + { + narg = atoi (arg); + } + else + { + narg = 1; + } + + switch (buf[0]) + { + case '[': + switch (last) + { + case 'A': + vt102_cursor_motion (v, 0, -narg, 0); + break; + case 'B': + vt102_cursor_motion (v, 0, narg, 0); + break; + case 'C': + vt102_cursor_motion (v, narg, 0, 0); + break; + case 'D': + vt102_cursor_motion (v, -narg, 0, 0); + break; + case 'H': + v->pos.y = narg - 1; + + ptr = index (arg, ';'); + if (ptr) + v->pos.x = atoi (ptr + 1) - 1; + else + v->pos.x = 0; + + vt102_cursor_normalize (v, 0, 0); + break; + case 'J': + fprintf(stderr,"OCTOPUS %d\n",narg); + switch (narg) + { + case 1: + crt_erase (&v->crt, v->pos, v->screen_end, 1); + break; + case 2: + crt_erase (&v->crt, v->screen_start, v->screen_end, 1); + break; + } + break; + case 'K': + { + CRT_Pos ls = { 0, v->pos.y }; + CRT_Pos le = { VT102_COLS - 1, v->pos.y }; + if (len==2) + narg = 0; /*Different default */ + + switch (narg) + { + case 0: + fprintf(stderr,"FISH %d %d -> %d %d\n", + v->pos.x,v->pos.y, + le.x,le.y); + crt_erase (&v->crt, v->pos, le, 1); + break; + case 1: + fprintf(stderr,"SOUP %d %d -> %d %d\n", + ls.x,ls.y, + v->pos.x,v->pos.y); + crt_erase (&v->crt, ls, v->pos, 1); + break; + case 2: + fprintf(stderr,"TREE %d %d -> %d %d\n", + ls.x,ls.y, + le.x,le.y); + crt_erase (&v->crt, ls, le, 1); + break; + } + } + break; + + case 'P': + while (narg--) + vt102_delete_from_line (v, v->pos); + break; + case 'L': + if ((v->pos.y >= v->top_margin.y) + && (v->pos.y <= v->bottom_margin.y)) + { + while (narg--) + crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1); + } + break; + + case 'M': + if ((v->pos.y >= v->top_margin.y) + && (v->pos.y <= v->bottom_margin.y)) + { + while (narg--) + crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0); + } + break; + + default: + fprintf (stderr, "A: CSI %s buf[0]=%c\n", buf,buf[0]); + } + break; + default: + fprintf (stderr, "B: CSI %s buf[0]=%c\n", buf,buf[0]); + } + + + } -void vt102_parse_csi(VT102 *v,char *buf,int len) +void +vt102_status_line (VT102 * v, char *str) { -buf[len]=0; -fprintf(stderr, "CSI %s\n",buf); + int i = VT102_COLS; + CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)]; + + while (i--) + { + ca->attr = CRT_ATTR_REVERSE; + ca->chr = *str; + if (*str) + str++; + ca++; + } + } + void vt102_parse_char (VT102 * v, int c) { VT102_parser *p = &v->parser; - fprintf(stderr,"%c pc %d %d %d %d %d\n",c,c,p->in_csi,p->in_escape,v->pos.x,v->pos.y); +#if 0 + fprintf (stderr, "%c pc %d %d %d %d %d\n", c, c, p->in_csi, p->in_escape, + v->pos.x, v->pos.y); +#endif if (p->in_csi) { p->csi_buf[p->csi_ptr++] = c; @@ -259,19 +438,17 @@ vt102_parse_char (VT102 * v, int c) /*ACK*/ case 6: /*BEL*/ case 7: /*BS*/ case 8: - vt102_cursor_motion (v, -1, 0, 0, 0); + vt102_cursor_motion (v, -1, 0, 1); break; /*HT*/ case 9: break; /*LF*/ case 10: - vt102_cursor_motion (v, 0, 1, 0, 1); - break; /*VT*/ case 11: - break; /*FF*/ case 12: + vt102_cursor_motion (v, 0, 1, 1); break; /*CR*/ case 13: - v->pos.x = 0; + v->pos.x = v->top_margin.x; break; /*SO*/ case 14: /*SI*/ case 15: @@ -299,9 +476,40 @@ vt102_parse_char (VT102 * v, int c) /*regular character */ default: v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c; v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr; - vt102_cursor_motion (v, 1, 0, 1, 1); + vt102_cursor_motion (v, 1, 0, 1); } } - v->crt.pos=v->pos; + v->crt.pos = v->pos; + + vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby"); +} + +void +vt102_parser_reset (VT102_parser * p) +{ + p->in_csi = 0; + p->in_escape = 0; + p->csi_ptr = 0; +} + +void +vt102_reset (VT102 * v) +{ + VT102_parser *p = &v->parser; + + vt102_parser_reset (p); + crt_cls (&v->crt); + + + v->screen_start.x = 0; + v->screen_start.y = 0; + v->screen_end.x = VT102_COLS - 1; + v->screen_end.y = VT102_ROWS - 1; + + v->top_margin = v->screen_start; + v->bottom_margin = v->screen_end; + + v->pos = v->screen_start; + } diff --git a/src/vt102.h b/src/vt102.h index 5a2d686..6942175 100644 --- a/src/vt102.h +++ b/src/vt102.h @@ -12,6 +12,9 @@ /* * $Log$ + * Revision 1.3 2008/02/04 20:23:55 james + * *** empty log message *** + * * Revision 1.2 2008/02/04 02:05:06 james * *** empty log message *** * @@ -25,6 +28,10 @@ #define VT102_CSI_LEN 128 +#define VT102_ROWS 24 +#define VT102_COLS 80 +#define VT102_STATUS_ROW 24 + typedef struct { int in_escape; @@ -35,6 +42,8 @@ typedef struct typedef struct { + CRT_Pos top_margin,bottom_margin; + CRT_Pos screen_start,screen_end; VT102_parser parser; int attr; CRT crt; -- 2.30.2