8 #define CL_ANSIMIN 0x0001 /* Everybody has these even MSDOS */
9 #define CL_VT100 0x0002 /* VT100 */
10 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
11 #define CL_VT102 0x0008 /* VT102 */
12 #define CL_VT220 0x0010 /* VT220 */
13 #define CL_VT320 0x0020 /* VT320 */
14 #define CL_VT420 0x0040 /* VT420 */
15 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
16 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
17 #define CL_ANSI 0x1000 /* ANSI ECMA-48 not in the VT100..VT420 */
18 #define CL_OTHER 0x2000 /* Others, Xterm, linux, putty, dunno, etc */
20 #define TM_ANSIMIN (CL_ANSIMIN)
21 #define TM_VT100 (CL_ANSIMIN+CL_VT100)
22 #define TM_VT100AVO (TM_VT100+CL_VT100AVO)
23 #define TM_VT102 (TM_VT100AVO+CL_VT102)
26 #define compatibility(x) \
27 if ( ((CL_##x)&compatibility_level) == 0 ) { \
32 #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
34 static int compatibility_level = TM_PUTTY;
37 static unsigned long *text; /* buffer of text on terminal screen */
38 static unsigned long *scrtop; /* top of working screen */
39 static unsigned long *disptop; /* top of displayed screen */
40 static unsigned long *sbtop; /* top of scrollback */
41 static unsigned long *cpos; /* cursor position (convenience) */
42 static unsigned long *disptext; /* buffer of text on real screen */
43 static unsigned long *wanttext; /* buffer of text we want on screen */
44 static unsigned long *alttext; /* buffer of text on alt. screen */
46 static unsigned char *selspace; /* buffer for building selections in */
48 #define TSIZE (sizeof(*text))
49 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
51 static unsigned long curr_attr, save_attr;
52 static unsigned long erase_char = ERASE_CHAR;
54 static int curs_x, curs_y; /* cursor */
55 static int save_x, save_y; /* saved cursor position */
56 static int marg_t, marg_b; /* scroll margins */
57 static int dec_om; /* DEC origin mode flag */
58 static int wrap, wrapnext; /* wrap flags */
59 static int insert; /* insert-mode flag */
60 static int cset; /* 0 or 1: which char set */
61 static int save_cset, save_csattr; /* saved with cursor position */
62 static int rvideo; /* global reverse video flag */
63 static int cursor_on; /* cursor enabled flag */
64 static int reset_132; /* Flag ESC c resets to 80 cols */
65 static int use_bce; /* Use Background coloured erase */
66 static int blinker; /* When blinking is the cursor on ? */
67 static int vt52_mode; /* Is VT100+ in vt52 mode ? */
69 static unsigned long cset_attr[2];
72 * Saved settings on the alternate screen.
74 static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
75 static int alt_t, alt_b;
78 #define ARGS_MAX 32 /* max # of esc sequence arguments */
79 #define ARG_DEFAULT 0 /* if an arg isn't specified */
80 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
81 static int esc_args[ARGS_MAX];
84 #define ANSI(x,y) ((x)+((y)<<8))
85 #define ANSI_QUE(x) ANSI(x,TRUE)
87 #define OSC_STR_MAX 2048
88 static int osc_strlen;
89 static char osc_string[OSC_STR_MAX+1];
92 static unsigned char *tabs;
106 OSC_STRING, OSC_MAYBE_ST,
114 NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
117 SM_CHAR, SM_WORD, SM_LINE
119 static unsigned long *selstart, *selend, *selanchor;
121 static short wordness[256] = {
122 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 01 */
123 0,1,2,1,1,1,1,1,1,1,1,1,1,2,2,2, 2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1, /* 23 */
124 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2, /* 45 */
125 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1, /* 67 */
126 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 89 */
127 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* AB */
128 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* CD */
129 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* EF */
132 static unsigned char sel_nl[] = SEL_NL;
135 * Internal prototypes.
137 static void do_paint (Context, int);
138 static void erase_lots (int, int, int);
139 static void swap_screen (int);
140 static void update_sbar (void);
141 static void deselect (void);
144 * Set up power-on settings for the terminal.
146 static void power_on(void) {
147 curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
150 alt_b = marg_b = rows - 1;
155 for (i = 0; i < cols; i++)
156 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
158 alt_om = dec_om = cfg.dec_om;
159 alt_wnext = wrapnext = alt_ins = insert = FALSE;
160 alt_wrap = wrap = cfg.wrap_mode;
162 cset_attr[0] = cset_attr[1] = ATTR_ASCII;
165 save_attr = curr_attr = ATTR_DEFAULT;
166 app_cursor_keys = cfg.app_cursor;
167 app_keypad_keys = cfg.app_keypad;
169 erase_char = ERASE_CHAR;
173 for (i = 0; i < 256; i++)
174 wordness[i] = cfg.wordness[i];
178 erase_lots (FALSE, TRUE, TRUE);
180 erase_lots (FALSE, TRUE, TRUE);
185 * Force a screen update.
187 void term_update(void) {
191 if ( (seen_key_event && (unscroll_event & US_KEY)) ||
192 (seen_disp_event && (unscroll_event & US_DISP)) ) {
194 seen_disp_event = seen_key_event = 0;
196 do_paint (ctx, TRUE);
202 * Same as power_on(), but an external function.
204 void term_pwron(void) {
213 * Clear the scrollback.
215 void term_clrsb(void) {
216 disptop = sbtop = scrtop;
221 * Initialise the terminal.
223 void term_init(void) {
224 text = sbtop = scrtop = disptop = cpos = NULL;
225 disptext = wanttext = NULL;
234 * Set up the terminal for a given size.
236 void term_size(int newrows, int newcols, int newsavelines) {
237 unsigned long *newtext, *newdisp, *newwant, *newalt;
238 int i, j, crows, ccols;
240 int save_alt_which = alt_which;
242 if (newrows == rows && newcols == cols && newsavelines == savelines)
243 return; /* nothing to do */
249 alt_b = marg_b = newrows - 1;
251 newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
252 disptop = newtext + newsavelines*(newcols+1);
253 for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
254 newtext[i] = ERASE_CHAR;
256 crows = rows + (scrtop - sbtop) / (cols+1);
257 if (crows > newrows+newsavelines)
258 crows = newrows+newsavelines;
259 ccols = (cols < newcols ? cols : newcols);
260 for (i=0; i<crows; i++) {
261 int oldidx = (rows + savelines - crows + i) * (cols+1);
262 int newidx = (newrows + newsavelines - crows + i) * (newcols+1);
263 for (j=0; j<ccols; j++)
264 newtext[newidx+j] = text[oldidx+j];
265 newtext[newidx+newcols] =
266 (cols == newcols ? text[oldidx+cols] : 0);
268 sbtop = disptop - (crows - newrows) * (newcols+1);
277 newdisp = smalloc (newrows*(newcols+1)*TSIZE);
278 for (i=0; i<newrows*(newcols+1); i++)
279 newdisp[i] = ATTR_INVALID;
283 newwant = smalloc (newrows*(newcols+1)*TSIZE);
284 for (i=0; i<newrows*(newcols+1); i++)
285 newwant[i] = ATTR_INVALID;
289 newalt = smalloc (newrows*(newcols+1)*TSIZE);
290 for (i=0; i<newrows*(newcols+1); i++)
291 newalt[i] = erase_char;
296 selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
298 tabs = srealloc (tabs, newcols*sizeof(*tabs));
301 for (i = (cols > 0 ? cols : 0); i < newcols; i++)
302 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
306 curs_y += newrows - rows;
309 if (curs_y >= newrows)
311 if (curs_x >= newcols)
314 wrapnext = alt_wnext = FALSE;
318 savelines = newsavelines;
321 swap_screen(save_alt_which);
330 static void swap_screen (int which) {
334 if (which == alt_which)
339 for (t=0; t<rows*(cols+1); t++) {
340 tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
343 t = curs_x; curs_x = alt_x; alt_x = t;
344 t = curs_y; curs_y = alt_y; alt_y = t;
345 t = marg_t; marg_t = alt_t; alt_t = t;
346 t = marg_b; marg_b = alt_b; alt_b = t;
347 t = dec_om; dec_om = alt_om; alt_om = t;
348 t = wrap; wrap = alt_wrap; alt_wrap = t;
349 t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
350 t = insert; insert = alt_ins; alt_ins = t;
351 t = cset; cset = alt_cset; alt_cset = t;
357 * Retrieve a character from `inbuf'.
359 static int inbuf_getc(void) {
360 if (inbuf_head == inbuf_reap)
364 inbuf_reap = (inbuf_reap+1) & INBUF_MASK;
370 * Update the scroll bar.
372 static void update_sbar(void) {
375 min = (sbtop - text) / (cols+1);
376 set_sbar ((scrtop - text) / (cols+1) + rows - min,
377 (disptop - text) / (cols+1) - min,
382 * Check whether the region bounded by the two pointers intersects
383 * the scroll region, and de-select the on-screen selection if so.
385 static void check_selection (unsigned long *from, unsigned long *to) {
386 if (from < selend && selstart < to)
391 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
392 * for backward.) `sb' is TRUE if the scrolling is permitted to
393 * affect the scrollback buffer.
395 static void scroll (int topline, int botline, int lines, int sb) {
396 unsigned long *scroll_top;
397 int scroll_size, size, i;
399 scroll_top = scrtop + topline*(cols+1);
400 size = (lines < 0 ? -lines : lines) * (cols+1);
401 scroll_size = (botline - topline + 1) * (cols+1) - size;
403 if (lines > 0 && topline == 0 && alt_which == 0 && sb) {
405 * Since we're going to scroll the top line and we're on the
406 * scrolling screen let's also affect the scrollback buffer.
408 sbtop -= lines * (cols+1);
411 scroll_size += scroll_top - sbtop;
416 if (scroll_size < 0) {
423 memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
424 for (i = 0; i < size; i++)
425 scroll_top[i+scroll_size] = erase_char;
426 if (selstart > scroll_top &&
427 selstart < scroll_top + size + scroll_size) {
429 if (selstart < scroll_top)
430 selstart = scroll_top;
432 if (selend > scroll_top &&
433 selend < scroll_top + size + scroll_size) {
435 if (selend < scroll_top)
440 memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
441 for (i = 0; i < size; i++)
442 scroll_top[i] = erase_char;
443 if (selstart > scroll_top &&
444 selstart < scroll_top + size + scroll_size) {
446 if (selstart > scroll_top + size + scroll_size)
447 selstart = scroll_top + size + scroll_size;
449 if (selend > scroll_top &&
450 selend < scroll_top + size + scroll_size) {
452 if (selend > scroll_top + size + scroll_size)
453 selend = scroll_top + size + scroll_size;
459 * Move the cursor to a given position, clipping at boundaries. We
460 * may or may not want to clip at the scroll margin: marg_clip is 0
461 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
462 * even _being_ outside the margins.
464 static void move (int x, int y, int marg_clip) {
470 if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
472 if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
486 * Save or restore the cursor and SGR mode.
488 static void save_cursor(int save) {
492 save_attr = curr_attr;
494 save_csattr = cset_attr[cset];
498 curr_attr = save_attr;
500 cset_attr[cset] = save_csattr;
502 if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
507 * Erase a large portion of the screen: the whole screen, or the
508 * whole line, or parts thereof.
510 static void erase_lots (int line_only, int from_begin, int to_end) {
511 unsigned long *startpos, *endpos;
514 startpos = cpos - curs_x;
515 endpos = startpos + cols+1;
518 endpos = startpos + rows * (cols+1);
524 check_selection (startpos, endpos);
526 /* Clear screen also forces a full window redraw, just in case. */
527 if (startpos == scrtop && endpos == scrtop + rows * (cols+1))
530 while (startpos < endpos)
531 *startpos++ = erase_char;
535 * Insert or delete characters within the current line. n is +ve if
536 * insertion is desired, and -ve for deletion.
538 static void insch (int n) {
539 int dir = (n < 0 ? -1 : +1);
542 n = (n < 0 ? -n : n);
543 if (n > cols - curs_x)
545 m = cols - curs_x - n;
546 check_selection (cpos, cpos+n);
548 memmove (cpos, cpos+n, m*TSIZE);
550 cpos[m++] = erase_char;
552 memmove (cpos+n, cpos, m*TSIZE);
554 cpos[n] = erase_char;
559 * Toggle terminal mode `mode' to state `state'. (`query' indicates
560 * whether the mode is a DEC private one or a normal one.)
562 static void toggle_mode (int mode, int query, int state) {
563 if (query) switch (mode) {
564 case 1: /* application cursor keys */
565 app_cursor_keys = state;
567 case 2: /* VT52 mode */
570 case 3: /* 80/132 columns */
572 request_resize (state ? 132 : 80, rows, 1);
575 case 5: /* reverse video */
577 seen_disp_event = TRUE;
578 if (state) term_update();
580 case 6: /* DEC origin mode */
583 case 7: /* auto wrap */
586 case 25: /* enable/disable cursor */
587 compatibility(VT220);
589 seen_disp_event = TRUE;
591 case 47: /* alternate screen */
592 compatibility(OTHER);
597 } else switch (mode) {
598 case 4: /* set insert mode */
599 compatibility(VT102);
602 case 12: /* set echo mode */
604 * This may be very good in smcup and rmcup (or smkx & rmkx) if you
605 * have a long RTT and the telnet client/daemon doesn't understand
608 * DONT send TS_RECHO/TS_LECHO; the telnet daemon tries to fix the
609 * tty and _really_ confuses some programs.
611 compatibility(VT220);
612 ldisc = (state? &ldisc_simple : &ldisc_term);
618 * Process an OSC sequence: set window title or icon name.
620 static void do_osc(void) {
623 wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
625 osc_string[osc_strlen] = '\0';
626 switch (esc_args[0]) {
629 set_icon (osc_string);
630 if (esc_args[0] == 1)
632 /* fall through: parameter 0 means set both */
635 set_title (osc_string);
642 * Remove everything currently in `inbuf' and stick it up on the
643 * in-memory display. There's a big state machine in here to
644 * process escape sequences...
646 void term_out(void) {
649 while ( (c = inbuf_getc()) != -1) {
651 * Optionally log the session traffic to a file. Useful for
652 * debugging and possibly also useful for actual logging.
655 static FILE *fp = NULL;
656 if (!fp) fp = fopen(logfile, "wb");
657 if (fp) fputc (c, fp);
659 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
660 * be able to display 8-bit characters, but I'll let that go 'cause
663 if( (c&0x60) == 0 && termstate < DO_CTRLS &&
664 ( (c&0x80) == 0 || has_compat(VT220))) {
666 case '\005': /* terminal type query */
667 /* Strictly speaking this is VT100 but a VT100 defaults to
668 * no response. Other terminals respond at their option.
670 * Don't put a CR in the default string as this tends to
671 * upset some weird software.
673 * An xterm returns "xterm" (5 characters)
675 compatibility(OTHER);
676 ldisc->send ("PuTTY", 5);
683 if (curs_x == 0 && curs_y > 0)
684 curs_x = cols-1, curs_y--;
690 seen_disp_event = TRUE;
693 compatibility(VT100);
697 compatibility(VT100);
702 termstate = VT52_ESC;
704 compatibility(ANSIMIN);
705 termstate = SEEN_ESC;
709 compatibility(VT220);
710 termstate = SEEN_CSI;
712 esc_args[0] = ARG_DEFAULT;
716 compatibility(VT220);
717 termstate = SEEN_OSC;
724 seen_disp_event = TRUE;
728 compatibility(VT100);
730 if (curs_y == marg_b)
731 scroll (marg_t, marg_b, 1, TRUE);
732 else if (curs_y < rows-1)
743 } while (curs_x < cols-1 && !tabs[curs_x]);
747 unsigned long *old_cpos = cpos;
749 check_selection (old_cpos, cpos);
751 seen_disp_event = TRUE;
755 else switch (termstate) {
757 /* Only graphic characters get this far, ctrls are stripped above */
759 cpos[1] = ATTR_WRAPPED;
760 if (curs_y == marg_b)
761 scroll (marg_t, marg_b, 1, TRUE);
762 else if (curs_y < rows-1)
770 if (selstate != NO_SELECTION)
771 check_selection (cpos, cpos+1);
772 switch (cset_attr[cset]) {
773 /* Linedraw characters are different from 'ESC ( B' only
774 * for a small range, for ones outside that range make sure
775 * we use the same font as well as the same encoding.
778 if (c<0x60 || c>0x7F)
779 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
782 *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW;
785 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
786 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
790 if (curs_x == cols) {
799 termstate = TOPLEVEL;
803 * This state is virtually identical to SEEN_ESC, with the
804 * exception that we have an OSC sequence in the pipeline,
805 * and _if_ we see a backslash, we process it.
809 termstate = TOPLEVEL;
812 /* else fall through */
814 termstate = TOPLEVEL;
816 case ' ': /* some weird sequence? */
817 compatibility(VT220);
818 termstate = IGNORE_NEXT;
820 case '[': /* enter CSI mode */
821 termstate = SEEN_CSI;
823 esc_args[0] = ARG_DEFAULT;
826 case ']': /* xterm escape sequences */
827 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
828 compatibility(OTHER);
829 termstate = SEEN_OSC;
832 case '(': /* should set GL */
833 compatibility(VT100);
836 case ')': /* should set GR */
837 compatibility(VT100);
840 case '7': /* save cursor */
841 compatibility(VT100);
844 case '8': /* restore cursor */
845 compatibility(VT100);
847 seen_disp_event = TRUE;
850 compatibility(VT100);
851 app_keypad_keys = TRUE;
854 compatibility(VT100);
855 app_keypad_keys = FALSE;
857 case 'D': /* exactly equivalent to LF */
858 compatibility(VT100);
859 if (curs_y == marg_b)
860 scroll (marg_t, marg_b, 1, TRUE);
861 else if (curs_y < rows-1)
865 seen_disp_event = TRUE;
867 case 'E': /* exactly equivalent to CR-LF */
868 compatibility(VT100);
871 if (curs_y == marg_b)
872 scroll (marg_t, marg_b, 1, TRUE);
873 else if (curs_y < rows-1)
877 seen_disp_event = TRUE;
879 case 'M': /* reverse index - backwards LF */
880 compatibility(VT100);
881 if (curs_y == marg_t)
882 scroll (marg_t, marg_b, -1, TRUE);
887 seen_disp_event = TRUE;
889 case 'Z': /* terminal type query */
890 compatibility(VT100);
891 ldisc->send ("\033[?6c", 5);
893 case 'c': /* restore power-on settings */
894 compatibility(VT100);
897 request_resize (80, rows, 1);
902 seen_disp_event = TRUE;
904 case '#': /* ESC # 8 fills screen with Es :-) */
905 compatibility(VT100);
906 termstate = SEEN_ESCHASH;
908 case 'H': /* set a tab */
909 compatibility(VT100);
915 termstate = TOPLEVEL; /* default */
918 if (esc_nargs <= ARGS_MAX) {
919 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
920 esc_args[esc_nargs-1] = 0;
921 esc_args[esc_nargs-1] =
922 10 * esc_args[esc_nargs-1] + c - '0';
924 termstate = SEEN_CSI;
928 if (++esc_nargs <= ARGS_MAX)
929 esc_args[esc_nargs-1] = ARG_DEFAULT;
930 termstate = SEEN_CSI;
934 if( esc_query ) esc_query = -1;
935 else if( c == '?' ) esc_query = TRUE;
937 termstate = SEEN_CSI;
939 else switch (ANSI(c,esc_query)) {
940 case 'A': /* move up N lines */
941 move (curs_x, curs_y - def(esc_args[0], 1), 1);
942 seen_disp_event = TRUE;
944 case 'e': /* move down N lines */
947 move (curs_x, curs_y + def(esc_args[0], 1), 1);
948 seen_disp_event = TRUE;
950 case 'a': /* move right N cols */
953 move (curs_x + def(esc_args[0], 1), curs_y, 1);
954 seen_disp_event = TRUE;
956 case 'D': /* move left N cols */
957 move (curs_x - def(esc_args[0], 1), curs_y, 1);
958 seen_disp_event = TRUE;
960 case 'E': /* move down N lines and CR */
962 move (0, curs_y + def(esc_args[0], 1), 1);
963 seen_disp_event = TRUE;
965 case 'F': /* move up N lines and CR */
967 move (0, curs_y - def(esc_args[0], 1), 1);
968 seen_disp_event = TRUE;
970 case 'G': case '`': /* set horizontal posn */
972 move (def(esc_args[0], 1) - 1, curs_y, 0);
973 seen_disp_event = TRUE;
975 case 'd': /* set vertical posn */
977 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
979 seen_disp_event = TRUE;
981 case 'H': case 'f': /* set horz and vert posns at once */
983 esc_args[1] = ARG_DEFAULT;
984 move (def(esc_args[1], 1) - 1,
985 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
987 seen_disp_event = TRUE;
989 case 'J': /* erase screen or parts of it */
991 unsigned int i = def(esc_args[0], 0) + 1;
994 erase_lots(FALSE, !!(i & 2), !!(i & 1));
997 seen_disp_event = TRUE;
999 case 'K': /* erase line or parts of it */
1001 unsigned int i = def(esc_args[0], 0) + 1;
1004 erase_lots(TRUE, !!(i & 2), !!(i & 1));
1006 seen_disp_event = TRUE;
1008 case 'L': /* insert lines */
1009 compatibility(VT102);
1010 if (curs_y <= marg_b)
1011 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
1012 seen_disp_event = TRUE;
1014 case 'M': /* delete lines */
1015 compatibility(VT102);
1016 if (curs_y <= marg_b)
1017 scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
1018 seen_disp_event = TRUE;
1020 case '@': /* insert chars */
1021 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1022 compatibility(VT102);
1023 insch (def(esc_args[0], 1));
1024 seen_disp_event = TRUE;
1026 case 'P': /* delete chars */
1027 compatibility(VT102);
1028 insch (-def(esc_args[0], 1));
1029 seen_disp_event = TRUE;
1031 case 'c': /* terminal type query */
1032 compatibility(VT100);
1033 /* This is the response for a VT102 */
1034 ldisc->send ("\033[?6c", 5);
1036 case 'n': /* cursor position query */
1037 if (esc_args[0] == 6) {
1038 /* Wonder of wonders ANSI.SYS has this!! */
1040 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
1041 ldisc->send (buf, strlen(buf));
1043 else if (esc_args[0] == 5) {
1044 /* Are we working ... well I suppose so :-) */
1045 /* But ANSI.SYS doesn't say it's working :-) :-) */
1046 compatibility(VT100);
1047 ldisc->send ("\033[0n", 4);
1050 case 'h': /* toggle modes to high */
1052 compatibility(VT100);
1055 for (i=0; i<esc_nargs; i++)
1056 toggle_mode (esc_args[i], esc_query, TRUE);
1059 case 'l': /* toggle modes to low */
1061 compatibility(VT100);
1064 for (i=0; i<esc_nargs; i++)
1065 toggle_mode (esc_args[i], esc_query, FALSE);
1068 case 'g': /* clear tabs */
1069 compatibility(VT100);
1070 if (esc_nargs == 1) {
1071 if (esc_args[0] == 0) {
1072 tabs[curs_x] = FALSE;
1073 } else if (esc_args[0] == 3) {
1075 for (i = 0; i < cols; i++)
1080 case 'r': /* set scroll margins */
1081 compatibility(VT100);
1082 if (esc_nargs <= 2) {
1084 /* VTTEST Bug 9 if the first arg is default _or_ zero
1085 * this is a full screen region irrespective of 2nd arg.
1087 if (esc_args[0] <= 0) {
1091 top = def(esc_args[0], 1) - 1;
1092 bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
1093 def(esc_args[1], rows)) - 1;
1102 * I used to think the cursor should be
1103 * placed at the top of the newly marginned
1104 * area. Apparently not: VMS TPU falls over
1107 * Well actually it should for Origin mode - RDB
1109 curs_y = (dec_om ? marg_t : 0);
1111 seen_disp_event = TRUE;
1115 case 'm': /* set graphics rendition */
1118 * A VT100 without the AVO only had one attribute, either
1119 * underline or reverse video depending on the cursor type,
1120 * this was selected by CSI 7m.
1123 * This is DIM on the VT100-AVO and VT102
1125 * This is BLINK on the VT100-AVO and VT102+
1127 * This is INVIS on the VT100-AVO and VT102
1129 * This like 22 disables BOLD, DIM and INVIS
1131 * The ANSI colours appear on any terminal that has colour
1132 * (obviously) but the interaction between sgr0 and the
1133 * colours varies but is usually related to the background
1134 * colour erase item.
1135 * The interaction between colour attributes and the mono
1136 * ones is also very implementation dependent.
1138 * The 39 and 49 attributes are likely to be unimplemented.
1141 for (i=0; i<esc_nargs; i++) {
1142 switch (def(esc_args[i], 0)) {
1143 case 0: /* restore defaults */
1144 curr_attr = ATTR_DEFAULT; break;
1145 case 1: /* enable bold */
1146 compatibility(VT100AVO);
1147 curr_attr |= ATTR_BOLD; break;
1148 case 21: /* (enable double underline) */
1149 compatibility(OTHER);
1150 case 4: /* enable underline */
1151 compatibility(VT100AVO);
1152 curr_attr |= ATTR_UNDER; break;
1153 case 5: /* enable blink */
1154 compatibility(VT100AVO);
1155 curr_attr |= ATTR_BLINK; break;
1156 case 7: /* enable reverse video */
1157 curr_attr |= ATTR_REVERSE; break;
1158 case 22: /* disable bold */
1159 compatibility(VT220);
1160 curr_attr &= ~ATTR_BOLD; break;
1161 case 24: /* disable underline */
1162 compatibility(VT220);
1163 curr_attr &= ~ATTR_UNDER; break;
1164 case 25: /* disable blink */
1165 compatibility(VT220);
1166 curr_attr &= ~ATTR_BLINK; break;
1167 case 27: /* disable reverse video */
1168 compatibility(VT220);
1169 curr_attr &= ~ATTR_REVERSE; break;
1170 case 30: case 31: case 32: case 33:
1171 case 34: case 35: case 36: case 37:
1173 curr_attr &= ~ATTR_FGMASK;
1174 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
1176 case 39: /* default-foreground */
1177 curr_attr &= ~ATTR_FGMASK;
1178 curr_attr |= ATTR_DEFFG;
1180 case 40: case 41: case 42: case 43:
1181 case 44: case 45: case 46: case 47:
1183 curr_attr &= ~ATTR_BGMASK;
1184 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
1186 case 49: /* default-background */
1187 curr_attr &= ~ATTR_BGMASK;
1188 curr_attr |= ATTR_DEFBG;
1193 erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
1196 case 's': /* save cursor */
1199 case 'u': /* restore cursor */
1200 save_cursor (FALSE);
1201 seen_disp_event = TRUE;
1203 case 't': /* set page size - ie window height */
1205 * VT340/VT420 sequence DECSLPP, DEC only allows values
1206 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1207 * illegal values (eg first arg 1..9) for window changing
1210 compatibility(VT340TEXT);
1211 if (esc_nargs<=1 && (esc_args[0]<1 || esc_args[0]>=24)) {
1212 request_resize (cols, def(esc_args[0], 24), 0);
1216 case ANSI('|', '*'):
1217 /* VT420 sequence DECSNLS
1218 * Set number of lines on screen
1219 * VT420 uses VGA like hardware and can support any size in
1220 * reasonable range (24..49 AIUI) with no default specified.
1222 compatibility(VT420);
1223 if (esc_nargs==1 && esc_args[0]>=24) {
1224 request_resize (cols, def(esc_args[0], cfg.height), 0);
1228 case ANSI('|', '$'):
1229 /* VT340/VT420 sequence DECSCPP
1230 * Set number of columns per page
1231 * Docs imply range is only 80 or 132, but I'll allow any.
1233 compatibility(VT340TEXT);
1235 request_resize (cols, def(esc_args[0], cfg.width), 0);
1239 case 'X': /* write N spaces w/o moving cursor */
1240 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1241 compatibility(VT100);
1243 int n = def(esc_args[0], 1);
1244 unsigned long *p = cpos;
1245 if (n > cols - curs_x)
1247 check_selection (cpos, cpos+n);
1250 seen_disp_event = TRUE;
1253 case 'x': /* report terminal characteristics */
1254 compatibility(VT100);
1257 int i = def(esc_args[0], 0);
1258 if (i == 0 || i == 1) {
1259 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1261 ldisc->send (buf, 20);
1266 compatibility(OTHER);
1267 use_bce = (esc_args[0]<=0);
1268 erase_char = ERASE_CHAR;
1270 erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
1273 /* Allow the host to make this emulator a 'perfect' VT102.
1274 * This first appeared in the VT220, but we do need to get
1275 * back to PuTTY mode so I won't check it.
1277 * The arg == 60 is a PuTTY extension.
1278 * The 2nd arg, 8bit vs 7bit is not obeyed.
1280 * Setting VT102 mode should also change the Fkeys to
1281 * generate PF* codes as a real VT102 has no Fkeys.
1282 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1285 * Note ESC c will NOT change this!
1288 if (esc_args[0] == 61) compatibility_level = TM_VT102;
1289 else if (esc_args[0] == 60) compatibility_level = TM_ANSIMIN;
1290 else compatibility_level = TM_PUTTY;
1296 /* VT100 only here, checked above */
1299 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1302 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1305 default: /* specifically, 'B' */
1306 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1309 if( !has_compat(VT220) || c != '%' )
1310 termstate = TOPLEVEL;
1315 case 'P': /* Linux palette sequence */
1316 termstate = SEEN_OSC_P;
1319 case 'R': /* Linux palette reset */
1322 termstate = TOPLEVEL;
1324 case 'W': /* word-set */
1325 termstate = SEEN_OSC_W;
1328 case '0': case '1': case '2': case '3': case '4':
1329 case '5': case '6': case '7': case '8': case '9':
1330 esc_args[0] = 10 * esc_args[0] + c - '0';
1334 * Grotty hack to support xterm and DECterm title
1335 * sequences concurrently.
1337 if (esc_args[0] == 2) {
1341 /* else fall through */
1343 termstate = OSC_STRING;
1349 * This OSC stuff is EVIL. It takes just one character to get into
1350 * sysline mode and it's not initially obvious how to get out.
1351 * So I've added CR and LF as string aborts.
1352 * This shouldn't effect compatibility as I believe embedded
1353 * control characters are supposed to be interpreted (maybe?)
1354 * and they don't display anything useful anyway.
1358 if (c == '\n' || c == '\r') {
1359 termstate = TOPLEVEL;
1360 } else if (c == 0234 || c == '\007' ) {
1362 * These characters terminate the string; ST and BEL
1363 * terminate the sequence and trigger instant
1364 * processing of it, whereas ESC goes back to SEEN_ESC
1365 * mode unless it is followed by \, in which case it is
1366 * synonymous with ST in the first place.
1369 termstate = TOPLEVEL;
1370 } else if (c == '\033')
1371 termstate = OSC_MAYBE_ST;
1372 else if (osc_strlen < OSC_STR_MAX)
1373 osc_string[osc_strlen++] = c;
1377 int max = (osc_strlen == 0 ? 21 : 16);
1379 if (c >= '0' && c <= '9')
1381 else if (c >= 'A' && c <= 'A'+max-10)
1383 else if (c >= 'a' && c <= 'a'+max-10)
1386 termstate = TOPLEVEL;
1387 osc_string[osc_strlen++] = val;
1388 if (osc_strlen >= 7) {
1389 palette_set (osc_string[0],
1390 osc_string[1] * 16 + osc_string[2],
1391 osc_string[3] * 16 + osc_string[4],
1392 osc_string[5] * 16 + osc_string[6]);
1394 termstate = TOPLEVEL;
1400 case '0': case '1': case '2': case '3': case '4':
1401 case '5': case '6': case '7': case '8': case '9':
1402 esc_args[0] = 10 * esc_args[0] + c - '0';
1405 termstate = OSC_STRING;
1411 unsigned long *p = scrtop;
1412 int n = rows * (cols+1);
1414 *p++ = ATTR_DEFAULT | 'E';
1416 seen_disp_event = TRUE;
1417 check_selection (scrtop, scrtop + rows * (cols+1));
1419 termstate = TOPLEVEL;
1422 termstate = TOPLEVEL;
1423 seen_disp_event = TRUE;
1426 move (curs_x, curs_y - 1, 1);
1429 move (curs_x, curs_y + 1, 1);
1432 move (curs_x + 1, curs_y, 1);
1435 move (curs_x - 1, curs_y, 1);
1438 cset_attr[cset=0] = ATTR_LINEDRW;
1441 cset_attr[cset=0] = ATTR_ASCII;
1448 scroll (0, rows-1, -1, TRUE);
1449 else if (curs_y > 0)
1455 erase_lots(FALSE, FALSE, TRUE);
1459 erase_lots(TRUE, FALSE, TRUE);
1462 /* XXX Print cursor line */
1465 /* XXX Start controller mode */
1468 /* XXX Stop controller mode */
1471 termstate = VT52_Y1;
1474 ldisc->send ("\033/Z", 3);
1477 app_cursor_keys = TRUE;
1480 app_cursor_keys = FALSE;
1483 /* XXX This should switch to VT100 mode not current or default
1484 * VT mode. But this will only have effect in a VT220+
1490 /* XXX Enter auto print mode */
1493 /* XXX Exit auto print mode */
1496 /* XXX Print screen */
1501 termstate = VT52_Y2;
1502 move(curs_x, c-' ', 0);
1505 termstate = TOPLEVEL;
1506 move(c-' ', curs_y, 0);
1509 if (selstate != NO_SELECTION)
1510 check_selection (cpos, cpos+1);
1515 * Compare two lines to determine whether they are sufficiently
1516 * alike to scroll-optimise one to the other. Return the degree of
1519 static int linecmp (unsigned long *a, unsigned long *b) {
1522 for (i=n=0; i < cols; i++)
1523 n += (*a++ == *b++);
1528 * Given a context, update the window. Out of paranoia, we don't
1529 * allow WM_PAINT responses to do scrolling optimisations.
1531 static void do_paint (Context ctx, int may_optimise){
1532 int i, j, start, our_curs_y;
1533 unsigned long attr, rv, cursor;
1538 if (blinker) cursor = ATTR_ACTCURS;
1542 cursor = ATTR_PASCURS;
1545 rv = (rvideo ? ATTR_REVERSE : 0);
1546 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1548 for (i=0; i<rows; i++) {
1549 int idx = i*(cols+1);
1550 for (j=0; j<=cols; j++,idx++) {
1551 unsigned long *d = disptop+idx;
1552 wanttext[idx] = ((*d ^ rv
1553 ^ (selstart <= d && d < selend ?
1554 ATTR_REVERSE : 0)) |
1555 (i==our_curs_y && j==curs_x ? cursor : 0));
1560 * We would perform scrolling optimisations in here, if they
1561 * didn't have a nasty tendency to cause the whole sodding
1562 * program to hang for a second at speed-critical moments.
1563 * We'll leave it well alone...
1566 for (i=0; i<rows; i++) {
1567 int idx = i*(cols+1);
1569 for (j=0; j<=cols; j++,idx++) {
1570 unsigned long t = wanttext[idx];
1571 int needs_update = (j < cols && t != disptext[idx]);
1572 int keep_going = (start != -1 && needs_update &&
1573 (t & ATTR_MASK) == attr &&
1574 j-start < sizeof(ch));
1575 if (start != -1 && !keep_going) {
1576 do_text (ctx, start, i, ch, j-start, attr);
1582 attr = t & ATTR_MASK;
1584 ch[j-start] = (char) (t & CHAR_MASK);
1592 * Flick the switch that says if blinking things should be shown or hidden.
1595 void term_blink(int flg) {
1596 static long last_blink = 0;
1597 long now, blink_diff;
1601 last_blink = GetTickCount();
1604 now = GetTickCount();
1606 blink_diff = now-last_blink;
1608 /* Make sure the cursor blinks no more than 2Hz */
1609 if (blink_diff>=0 && blink_diff<450)
1617 * Invalidate the whole screen so it will be repainted in full.
1619 void term_invalidate(void) {
1622 for (i=0; i<rows*(cols+1); i++)
1623 disptext[i] = ATTR_INVALID;
1627 * Paint the window in response to a WM_PAINT message.
1629 void term_paint (Context ctx, int l, int t, int r, int b) {
1630 int i, j, left, top, right, bottom;
1632 left = l / font_width;
1633 right = (r - 1) / font_width;
1634 top = t / font_height;
1635 bottom = (b - 1) / font_height;
1636 for (i = top; i <= bottom && i < rows ; i++)
1637 for (j = left; j <= right && j < cols ; j++)
1638 disptext[i*(cols+1)+j] = ATTR_INVALID;
1640 /* This should happen soon enough, also for some reason it sometimes
1641 * fails to actually do anything when re-sizing ... painting the wrong
1643 do_paint (ctx, FALSE);
1648 * Attempt to scroll the scrollback. The second parameter gives the
1649 * position we want to scroll to; the first is +1 to denote that
1650 * this position is relative to the beginning of the scrollback, -1
1651 * to denote it is relative to the end, and 0 to denote that it is
1652 * relative to the current position.
1654 void term_scroll (int rel, int where) {
1655 int n = where * (cols+1);
1657 disptop = (rel < 0 ? scrtop :
1658 rel > 0 ? sbtop : disptop) + n;
1659 if (disptop < sbtop)
1661 if (disptop > scrtop)
1668 * Spread the selection outwards according to the selection mode.
1670 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1671 unsigned long *linestart, *lineend;
1675 x = (p - text) % (cols+1);
1677 lineend = linestart + cols;
1682 * In this mode, every character is a separate unit, except
1683 * for runs of spaces at the end of a non-wrapping line.
1685 if (!(linestart[cols] & ATTR_WRAPPED)) {
1686 unsigned long *q = lineend;
1687 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1692 p = (dir == -1 ? q : lineend - 1);
1697 * In this mode, the units are maximal runs of characters
1698 * whose `wordness' has the same value.
1700 wvalue = wordness[*p & CHAR_MASK];
1702 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1705 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1711 * In this mode, every line is a unit.
1713 p = (dir == -1 ? linestart : lineend - 1);
1719 static void sel_spread (void) {
1720 selstart = sel_spread_half (selstart, -1);
1721 selend = sel_spread_half (selend - 1, +1) + 1;
1724 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
1725 unsigned long *selpoint;
1728 if (y>=rows) y = rows-1;
1736 if (x>=cols) x = cols-1;
1738 selpoint = disptop + y * (cols+1) + x;
1740 if (b == MB_SELECT && a == MA_CLICK) {
1742 selstate = ABOUT_TO;
1743 selanchor = selpoint;
1745 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1747 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1748 selstate = DRAGGING;
1749 selstart = selanchor = selpoint;
1750 selend = selstart + 1;
1752 } else if ((b == MB_SELECT && a == MA_DRAG) ||
1753 (b == MB_EXTEND && a != MA_RELEASE)) {
1754 if (selstate == ABOUT_TO && selanchor == selpoint)
1756 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1757 if (selpoint-selstart < (selend-selstart)/2)
1758 selanchor = selend - 1;
1760 selanchor = selstart;
1761 selstate = DRAGGING;
1763 if (selstate != ABOUT_TO && selstate != DRAGGING)
1764 selanchor = selpoint;
1765 selstate = DRAGGING;
1766 if (selpoint < selanchor) {
1767 selstart = selpoint;
1768 selend = selanchor + 1;
1770 selstart = selanchor;
1771 selend = selpoint + 1;
1774 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
1775 if (selstate == DRAGGING) {
1777 * We've completed a selection. We now transfer the
1778 * data to the clipboard.
1780 unsigned char *p = selspace;
1781 unsigned long *q = selstart;
1783 while (q < selend) {
1785 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1786 unsigned long *nlpos = lineend;
1788 if (!(*nlpos & ATTR_WRAPPED)) {
1789 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1794 while (q < nlpos && q < selend)
1795 *p++ = (unsigned char) (*q++ & CHAR_MASK);
1798 for (i=0; i<sizeof(sel_nl); i++)
1801 q = lineend + 1; /* start of next line */
1803 write_clip (selspace, p - selspace);
1804 selstate = SELECTED;
1806 selstate = NO_SELECTION;
1807 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
1811 get_clip((void **) &data, &len);
1815 while (p < data+len) {
1816 while (p < data+len &&
1817 !(p <= data+len-sizeof(sel_nl) &&
1818 !memcmp(p, sel_nl, sizeof(sel_nl))))
1826 c=xlat_kbd2tty(q[i]);
1831 if (p <= data+len-sizeof(sel_nl) &&
1832 !memcmp(p, sel_nl, sizeof(sel_nl))) {
1833 ldisc->send ("\r", 1);
1834 p += sizeof(sel_nl);
1839 get_clip(NULL, NULL);
1845 static void deselect (void) {
1846 selstate = NO_SELECTION;
1847 selstart = selend = scrtop;
1850 void term_deselect (void) {