/* * moves the cursor left n, no wrap around */ void CursorBack(XtermWidget xw, int n) { #define WRAP_MASK (REVERSEWRAP | WRAPAROUND) TScreen *screen = TScreenOf(xw); int offset, in_row, length, rev; int left = ScrnLeftMargin(xw); int before = screen->cur_col; if ((rev = (xw->flags & WRAP_MASK) == WRAP_MASK) != 0 && screen->do_wrap) { n--; } /* if the cursor is already before the left-margin, we have to let it go */ if (before < left) left = 0; if ((screen->cur_col -= n) < left) { if (rev) { in_row = ScrnRightMargin(xw) - left + 1; offset = (in_row * screen->cur_row) + screen->cur_col - left; if (offset < 0) { length = in_row * MaxRows(screen); offset += ((-offset) / length + 1) * length; } set_cur_row(screen, (offset / in_row)); set_cur_col(screen, (offset % in_row) + left); do_xevents(); } else { set_cur_col(screen, left); } } ResetWrap(screen); }
/* * Moves the cursor to the specified position, checking for bounds. * (this includes scrolling regions) * The origin is considered to be 0, 0 for this procedure. */ void CursorSet(TScreen * screen, int row, int col, unsigned flags) { int use_row = row; int use_col = col; int max_col = screen->max_col; int max_row = screen->max_row; if (flags & ORIGIN) { use_col += screen->lft_marg; max_col = screen->rgt_marg; } use_col = (use_col < 0 ? 0 : use_col); set_cur_col(screen, (use_col <= max_col ? use_col : max_col)); if (flags & ORIGIN) { use_row += screen->top_marg; max_row = screen->bot_marg; } use_row = (use_row < 0 ? 0 : use_row); set_cur_row(screen, (use_row <= max_row ? use_row : max_row)); ResetWrap(screen); TRACE(("CursorSet(%d,%d) margins V[%d..%d] H[%d..%d] -> %d,%d %s\n", row, col, screen->top_marg, screen->bot_marg, screen->lft_marg, screen->rgt_marg, screen->cur_row, screen->cur_col, (flags & ORIGIN ? "origin" : "normal"))); }
/* * Moves the cursor to the specified position, checking for bounds. * (this includes scrolling regions) * The origin is considered to be 0, 0 for this procedure. */ void CursorSet(TScreen * screen, int row, int col, unsigned flags) { int use_row = row; int max_row; col = (col < 0 ? 0 : col); set_cur_col(screen, (col <= screen->max_col ? col : screen->max_col)); max_row = screen->max_row; if (flags & ORIGIN) { use_row += screen->top_marg; max_row = screen->bot_marg; } use_row = (use_row < 0 ? 0 : use_row); set_cur_row(screen, (use_row <= max_row ? use_row : max_row)); screen->do_wrap = 0; TRACE(("CursorSet(%d,%d) margins [%d..%d] -> %d,%d %s\n", row, col, screen->top_marg, screen->bot_marg, screen->cur_row, screen->cur_col, (flags & ORIGIN ? "origin" : "normal"))); }
/* * moves the cursor down n, no scrolling. * Won't pass bottom margin or bottom of screen. */ void CursorDown(TScreen * screen, int n) { int max; int next = screen->cur_row + n; max = (screen->cur_row > screen->bot_marg ? screen->max_row : screen->bot_marg); if (next > max) next = max; if (next > screen->max_row) next = screen->max_row; set_cur_row(screen, next); ResetWrap(screen); }
/* * moves the cursor up n, no linestarving. * Won't pass top margin or top of screen. */ void CursorUp(TScreen * screen, int n) { int min; int next = screen->cur_row - n; min = ((screen->cur_row < screen->top_marg) ? 0 : screen->top_marg); if (next < min) next = min; if (next < 0) next = 0; set_cur_row(screen, next); ResetWrap(screen); }
/* * moves the cursor up n, no linestarving. * Won't pass top margin or top of screen. */ void CursorUp(TScreen * screen, int n) { int min; int next = screen->cur_row - n; min = ((screen->cur_row < screen->top_marg) ? 0 : screen->top_marg); if (next < min) next = min; if (next < 0) next = 0; set_cur_row(screen, next); screen->do_wrap = False; }
/* * moves the cursor left n, no wrap around */ void CursorBack(TScreen * screen, int n) { int i, j, k, rev; if ((rev = (term->flags & (REVERSEWRAP | WRAPAROUND)) == (REVERSEWRAP | WRAPAROUND)) != 0 && screen->do_wrap) n--; if ((screen->cur_col -= n) < 0) { if (rev) { if ((i = ((j = MaxCols(screen)) * screen->cur_row) + screen->cur_col) < 0) { k = j * MaxRows(screen); i += ((-i) / k + 1) * k; } set_cur_row(screen, i / j); set_cur_col(screen, i % j); } else set_cur_col(screen, 0); } screen->do_wrap = 0; }
/* * Interpret sixel graphics sequences. * * Resources: * http://en.wikipedia.org/wiki/Sixel * http://vt100.net/docs/vt3xx-gp/chapter14.html * ftp://ftp.cs.utk.edu/pub/shuford/terminal/sixel_graphics_news.txt * ftp://ftp.cs.utk.edu/pub/shuford/terminal/all_about_sixels.txt */ void parse_sixel(XtermWidget xw, ANSI *params, char const *string) { TScreen *screen = TScreenOf(xw); Graphic *graphic; SixelContext context; Char ch; switch (screen->terminal_id) { case 240: case 241: case 330: case 340: context.aspect_vertical = 2; context.aspect_horizontal = 1; break; case 382: context.aspect_vertical = 1; context.aspect_horizontal = 1; break; default: context.aspect_vertical = 2; context.aspect_horizontal = 1; break; } context.declared_width = 0; context.declared_height = 0; context.row = 0; context.col = 0; /* default isn't white on the VT240, but not sure what it is */ context.current_register = 3; /* FIXME: using green, but not sure what it should be */ if (xw->keyboard.flags & MODE_DECSDM) { TRACE(("sixel scrolling enabled: inline positioning for graphic at %d,%d\n", screen->cur_row, screen->cur_col)); graphic = get_new_graphic(xw, screen->cur_row, screen->cur_col, 0U); } else { TRACE(("sixel scrolling disabled: inline positioning for graphic at %d,%d\n", 0, 0)); graphic = get_new_graphic(xw, 0, 0, 0U); } { int Pmacro = params->a_param[0]; int Pbgmode = params->a_param[1]; int Phgrid = params->a_param[2]; int Pan = params->a_param[3]; int Pad = params->a_param[4]; int Ph = params->a_param[5]; int Pv = params->a_param[6]; (void) Phgrid; TRACE(("sixel bitmap graphics sequence: params=%d (Pmacro=%d Pbgmode=%d Phgrid=%d) scroll_amt=%d\n", params->a_nparam, Pmacro, Pbgmode, Phgrid, screen->scroll_amt)); switch (params->a_nparam) { case 7: if (Pan == 0 || Pad == 0) { TRACE(("DATA_ERROR: invalid raster ratio %d/%d\n", Pan, Pad)); return; } context.aspect_vertical = Pan; context.aspect_horizontal = Pad; if (Ph == 0 || Pv == 0) { TRACE(("DATA_ERROR: raster image dimensions are invalid %dx%d\n", Ph, Pv)); return; } if (Ph > graphic->max_width || Pv > graphic->max_height) { TRACE(("DATA_ERROR: raster image dimensions are too large %dx%d\n", Ph, Pv)); return; } context.declared_width = Ph; context.declared_height = Pv; if (context.declared_width > graphic->actual_width) { graphic->actual_width = context.declared_width; } if (context.declared_height > graphic->actual_height) { graphic->actual_height = context.declared_height; } break; case 3: case 2: case 1: switch (Pmacro) { case 0: /* keep default aspect settings */ break; case 1: case 5: case 6: context.aspect_vertical = 2; context.aspect_horizontal = 1; break; case 2: context.aspect_vertical = 5; context.aspect_horizontal = 1; break; case 3: case 4: context.aspect_vertical = 3; context.aspect_horizontal = 1; break; case 7: case 8: case 9: context.aspect_vertical = 1; context.aspect_horizontal = 1; break; default: TRACE(("DATA_ERROR: unknown sixel macro mode parameter\n")); return; } break; case 0: break; default: TRACE(("DATA_ERROR: unexpected parameter count (found %d)\n", params->a_nparam)); return; } if (Pbgmode == 1) { context.background = COLOR_HOLE; } else { /* FIXME: is the default background register always zero? what about in light background mode? */ context.background = 0; } /* Ignore the grid parameter because it seems only printers paid attention to it. * The VT3xx was always 0.0195 cm. */ } update_sixel_aspect(&context, graphic); for (;;) { ch = CharOf(*string); if (ch == '\0') break; if (ch >= 0x3f && ch <= 0x7e) { int sixel = ch - 0x3f; TRACE(("sixel=%x (%c)\n", sixel, (char) ch)); if (!graphic->valid) { init_sixel_background(graphic, &context); graphic->valid = 1; } set_sixel(graphic, &context, sixel); context.col++; } else if (ch == '$') { /* DECGCR */ /* ignore DECCRNLM in sixel mode */ TRACE(("sixel CR\n")); context.col = 0; } else if (ch == '-') { /* DECGNL */ int scroll_lines; TRACE(("sixel NL\n")); scroll_lines = 0; while (graphic->charrow - scroll_lines + (((context.row + 6) * graphic->pixh + FontHeight(screen) - 1) / FontHeight(screen)) > screen->bot_marg) { scroll_lines++; } context.col = 0; context.row += 6; /* If we hit the bottom margin on the graphics page (well, we just use the * text margin for now), the behavior is to either scroll or to discard * the remainder of the graphic depending on this setting. */ if (scroll_lines > 0) { if (xw->keyboard.flags & MODE_DECSDM) { Display *display = screen->display; xtermScroll(xw, scroll_lines); XSync(display, False); TRACE(("graphic scrolled the screen %d lines. screen->scroll_amt=%d screen->topline=%d, now starting row is %d\n", scroll_lines, screen->scroll_amt, screen->topline, graphic->charrow)); } else { break; } } } else if (ch == '!') { /* DECGRI */ int Pcount; const char *start; int sixel; int i; start = ++string; for (;;) { ch = CharOf(*string); if (ch != '0' && ch != '1' && ch != '2' && ch != '3' && ch != '4' && ch != '5' && ch != '6' && ch != '7' && ch != '8' && ch != '9' && ch != ' ' && ch != '\r' && ch != '\n') break; string++; } if (ch == '\0') { TRACE(("DATA_ERROR: sixel data string terminated in the middle of a repeat operator\n")); return; } if (string == start) { TRACE(("DATA_ERROR: sixel data string contains a repeat operator with empty count\n")); return; } Pcount = atoi(start); sixel = ch - 0x3f; TRACE(("sixel repeat operator: sixel=%d (%c), count=%d\n", sixel, (char) ch, Pcount)); if (!graphic->valid) { init_sixel_background(graphic, &context); graphic->valid = 1; } for (i = 0; i < Pcount; i++) { set_sixel(graphic, &context, sixel); context.col++; } } else if (ch == '#') { /* DECGCI */ ANSI color_params; int Pregister; parse_prefixedtype_params(&color_params, &string); Pregister = color_params.a_param[0]; if (Pregister >= (int) graphic->valid_registers) { TRACE(("DATA_WARNING: sixel color operator uses out-of-range register %d\n", Pregister)); /* FIXME: supposedly the DEC terminals wrapped register indicies -- verify */ while (Pregister >= (int) graphic->valid_registers) Pregister -= (int) graphic->valid_registers; TRACE(("DATA_WARNING: converted to %d\n", Pregister)); } if (color_params.a_nparam > 2 && color_params.a_nparam <= 5) { int Pspace = color_params.a_param[1]; int Pc1 = color_params.a_param[2]; int Pc2 = color_params.a_param[3]; int Pc3 = color_params.a_param[4]; short r, g, b; TRACE(("sixel set color register=%d space=%d color=[%d,%d,%d] (nparams=%d)\n", Pregister, Pspace, Pc1, Pc2, Pc3, color_params.a_nparam)); switch (Pspace) { case 1: /* HLS */ if (Pc1 > 360 || Pc2 > 100 || Pc3 > 100) { TRACE(("DATA_ERROR: sixel set color operator uses out-of-range HLS color coordinates %d,%d,%d\n", Pc1, Pc2, Pc3)); return; } hls2rgb(Pc1, Pc2, Pc3, &r, &g, &b); break; case 2: /* RGB */ if (Pc1 > 100 || Pc2 > 100 || Pc3 > 100) { TRACE(("DATA_ERROR: sixel set color operator uses out-of-range RGB color coordinates %d,%d,%d\n", Pc1, Pc2, Pc3)); return; } r = (short) Pc1; g = (short) Pc2; b = (short) Pc3; break; default: /* unknown */ TRACE(("DATA_ERROR: sixel set color operator uses unknown color space %d\n", Pspace)); return; } update_color_register(graphic, (RegisterNum) Pregister, r, g, b); } else if (color_params.a_nparam == 1) { TRACE(("sixel switch to color register=%d (nparams=%d)\n", Pregister, color_params.a_nparam)); context.current_register = (RegisterNum) Pregister; } else { TRACE(("DATA_ERROR: sixel switch color operator with unexpected parameter count (nparams=%d)\n", color_params.a_nparam)); return; } continue; } else if (ch == '"') /* DECGRA */ { ANSI raster_params; parse_prefixedtype_params(&raster_params, &string); if (raster_params.a_nparam < 2) { TRACE(("DATA_ERROR: sixel raster attribute operator with incomplete parameters (found %d, expected 2 or 4)\n", raster_params.a_nparam)); return; } { int Pan = raster_params.a_param[0]; int Pad = raster_params.a_param[1]; TRACE(("sixel raster attribute with h:w=%d:%d\n", Pan, Pad)); if (Pan == 0 || Pad == 0) { TRACE(("DATA_ERROR: invalid raster ratio %d/%d\n", Pan, Pad)); return; } context.aspect_vertical = Pan; context.aspect_horizontal = Pad; update_sixel_aspect(&context, graphic); } if (raster_params.a_nparam >= 4) { int Ph = raster_params.a_param[2]; int Pv = raster_params.a_param[3]; TRACE(("sixel raster attribute with h=%d v=%d\n", Ph, Pv)); if (Ph == 0 || Pv == 0) { TRACE(("DATA_ERROR: raster image dimensions are invalid %dx%d\n", Ph, Pv)); return; } if (Ph > graphic->max_width || Pv > graphic->max_height) { TRACE(("DATA_ERROR: raster image dimensions are too large %dx%d\n", Ph, Pv)); return; } context.declared_width = Ph; context.declared_height = Pv; if (context.declared_width > graphic->actual_width) { graphic->actual_width = context.declared_width; } if (context.declared_height > graphic->actual_height) { graphic->actual_height = context.declared_height; } } continue; } else if (ch == ' ' || ch == '\r' || ch == '\n') { /* EMPTY */ ; } else { TRACE(("DATA_ERROR: unknown sixel command %04x (%c)\n", (int) ch, ch)); } string++; } /* update the screen */ if (screen->scroll_amt) FlushScroll(xw); if (xw->keyboard.flags & MODE_DECSDM) { int new_row, new_col; if (screen->sixel_scrolls_right) { new_row = (graphic->charrow + (((graphic->actual_height * graphic->pixh) + FontHeight(screen) - 1) / FontHeight(screen)) - 1); new_col = (graphic->charcol + (((graphic->actual_width * graphic->pixw) + FontWidth(screen) - 1) / FontWidth(screen))); } else { /* FIXME: At least of the VT382 the vertical position appears to be * truncated (rounded toward zero after converting to character row. * This code rounds up, which seems more useful, but it would be * better to be compatible. Verify this is true on a VT3[34]0 as * well. */ new_row = (graphic->charrow + (((graphic->actual_height * graphic->pixh) + FontHeight(screen) - 1) / FontHeight(screen))); new_col = 0; } TRACE(("setting text position after %dx%d graphic starting on row=%d col=%d: cursor new_row=%d new_col=%d\n", graphic->actual_width * graphic->pixw, graphic->actual_height * graphic->pixh, graphic->charrow, graphic->charcol, new_row, new_col)); if (new_col > screen->rgt_marg) { new_col = screen->lft_marg; new_row++; TRACE(("column past left margin, overriding to row=%d col=%d\n", new_row, new_col)); } while (new_row > screen->bot_marg) { xtermScroll(xw, 1); new_row--; TRACE(("bottom row was past screen. new start row=%d, cursor row=%d\n", graphic->charrow, new_row)); } if (new_row < 0) { TRACE(("new row is going to be negative (%d)!", new_row)); /* FIXME: this was triggering, now it isn't */ goto finis; } set_cur_row(screen, new_row); set_cur_col(screen, new_col <= screen->rgt_marg ? new_col : screen->rgt_marg); } finis: refresh_modified_displayed_graphics(screen); TRACE(("DONE successfully parsed sixel data\n")); dump_graphic(graphic); }