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 /* Make sure the window hasn't shrunk since the save */
499 if (curs_x >= cols) curs_x = cols-1;
500 if (curs_y >= rows) curs_y = rows-1;
501 curr_attr = save_attr;
503 cset_attr[cset] = save_csattr;
505 if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
510 * Erase a large portion of the screen: the whole screen, or the
511 * whole line, or parts thereof.
513 static void erase_lots (int line_only, int from_begin, int to_end) {
514 unsigned long *startpos, *endpos;
517 startpos = cpos - curs_x;
518 endpos = startpos + cols+1;
521 endpos = startpos + rows * (cols+1);
527 check_selection (startpos, endpos);
529 /* Clear screen also forces a full window redraw, just in case. */
530 if (startpos == scrtop && endpos == scrtop + rows * (cols+1))
533 while (startpos < endpos)
534 *startpos++ = erase_char;
538 * Insert or delete characters within the current line. n is +ve if
539 * insertion is desired, and -ve for deletion.
541 static void insch (int n) {
542 int dir = (n < 0 ? -1 : +1);
545 n = (n < 0 ? -n : n);
546 if (n > cols - curs_x)
548 m = cols - curs_x - n;
549 check_selection (cpos, cpos+n);
551 memmove (cpos, cpos+n, m*TSIZE);
553 cpos[m++] = erase_char;
555 memmove (cpos+n, cpos, m*TSIZE);
557 cpos[n] = erase_char;
562 * Toggle terminal mode `mode' to state `state'. (`query' indicates
563 * whether the mode is a DEC private one or a normal one.)
565 static void toggle_mode (int mode, int query, int state) {
566 if (query) switch (mode) {
567 case 1: /* application cursor keys */
568 app_cursor_keys = state;
570 case 2: /* VT52 mode */
573 case 3: /* 80/132 columns */
575 request_resize (state ? 132 : 80, rows, 1);
578 case 5: /* reverse video */
580 seen_disp_event = TRUE;
581 if (state) term_update();
583 case 6: /* DEC origin mode */
586 case 7: /* auto wrap */
589 case 25: /* enable/disable cursor */
590 compatibility(VT220);
592 seen_disp_event = TRUE;
594 case 47: /* alternate screen */
595 compatibility(OTHER);
600 } else switch (mode) {
601 case 4: /* set insert mode */
602 compatibility(VT102);
605 case 12: /* set echo mode */
607 * This may be very good in smcup and rmcup (or smkx & rmkx) if you
608 * have a long RTT and the telnet client/daemon doesn't understand
611 * DONT send TS_RECHO/TS_LECHO; the telnet daemon tries to fix the
612 * tty and _really_ confuses some programs.
614 compatibility(VT220);
615 ldisc = (state? &ldisc_simple : &ldisc_term);
621 * Process an OSC sequence: set window title or icon name.
623 static void do_osc(void) {
626 wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
628 osc_string[osc_strlen] = '\0';
629 switch (esc_args[0]) {
632 set_icon (osc_string);
633 if (esc_args[0] == 1)
635 /* fall through: parameter 0 means set both */
638 set_title (osc_string);
645 * Remove everything currently in `inbuf' and stick it up on the
646 * in-memory display. There's a big state machine in here to
647 * process escape sequences...
649 void term_out(void) {
652 while ( (c = inbuf_getc()) != -1) {
654 * Optionally log the session traffic to a file. Useful for
655 * debugging and possibly also useful for actual logging.
658 static FILE *fp = NULL;
659 if (!fp) fp = fopen(logfile, "wb");
660 if (fp) fputc (c, fp);
662 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
663 * be able to display 8-bit characters, but I'll let that go 'cause
666 if( (c&0x60) == 0 && termstate < DO_CTRLS &&
667 ( (c&0x80) == 0 || has_compat(VT220))) {
669 case '\005': /* terminal type query */
670 /* Strictly speaking this is VT100 but a VT100 defaults to
671 * no response. Other terminals respond at their option.
673 * Don't put a CR in the default string as this tends to
674 * upset some weird software.
676 * An xterm returns "xterm" (5 characters)
678 compatibility(OTHER);
679 ldisc->send ("PuTTY", 5);
686 if (curs_x == 0 && curs_y > 0)
687 curs_x = cols-1, curs_y--;
693 seen_disp_event = TRUE;
696 compatibility(VT100);
700 compatibility(VT100);
705 termstate = VT52_ESC;
707 compatibility(ANSIMIN);
708 termstate = SEEN_ESC;
712 compatibility(VT220);
713 termstate = SEEN_CSI;
715 esc_args[0] = ARG_DEFAULT;
719 compatibility(VT220);
720 termstate = SEEN_OSC;
727 seen_disp_event = TRUE;
731 compatibility(VT100);
733 if (curs_y == marg_b)
734 scroll (marg_t, marg_b, 1, TRUE);
735 else if (curs_y < rows-1)
746 } while (curs_x < cols-1 && !tabs[curs_x]);
750 unsigned long *old_cpos = cpos;
752 check_selection (old_cpos, cpos);
754 seen_disp_event = TRUE;
758 else switch (termstate) {
760 /* Only graphic characters get this far, ctrls are stripped above */
762 cpos[1] = ATTR_WRAPPED;
763 if (curs_y == marg_b)
764 scroll (marg_t, marg_b, 1, TRUE);
765 else if (curs_y < rows-1)
773 if (selstate != NO_SELECTION)
774 check_selection (cpos, cpos+1);
775 switch (cset_attr[cset]) {
776 /* Linedraw characters are different from 'ESC ( B' only
777 * for a small range, for ones outside that range make sure
778 * we use the same font as well as the same encoding.
781 if (c<0x60 || c>0x7F)
782 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
785 *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW;
788 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
789 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
793 if (curs_x == cols) {
802 termstate = TOPLEVEL;
806 * This state is virtually identical to SEEN_ESC, with the
807 * exception that we have an OSC sequence in the pipeline,
808 * and _if_ we see a backslash, we process it.
812 termstate = TOPLEVEL;
815 /* else fall through */
817 termstate = TOPLEVEL;
819 case ' ': /* some weird sequence? */
820 compatibility(VT220);
821 termstate = IGNORE_NEXT;
823 case '[': /* enter CSI mode */
824 termstate = SEEN_CSI;
826 esc_args[0] = ARG_DEFAULT;
829 case ']': /* xterm escape sequences */
830 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
831 compatibility(OTHER);
832 termstate = SEEN_OSC;
835 case '(': /* should set GL */
836 compatibility(VT100);
839 case ')': /* should set GR */
840 compatibility(VT100);
843 case '7': /* save cursor */
844 compatibility(VT100);
847 case '8': /* restore cursor */
848 compatibility(VT100);
850 seen_disp_event = TRUE;
853 compatibility(VT100);
854 app_keypad_keys = TRUE;
857 compatibility(VT100);
858 app_keypad_keys = FALSE;
860 case 'D': /* exactly equivalent to LF */
861 compatibility(VT100);
862 if (curs_y == marg_b)
863 scroll (marg_t, marg_b, 1, TRUE);
864 else if (curs_y < rows-1)
868 seen_disp_event = TRUE;
870 case 'E': /* exactly equivalent to CR-LF */
871 compatibility(VT100);
874 if (curs_y == marg_b)
875 scroll (marg_t, marg_b, 1, TRUE);
876 else if (curs_y < rows-1)
880 seen_disp_event = TRUE;
882 case 'M': /* reverse index - backwards LF */
883 compatibility(VT100);
884 if (curs_y == marg_t)
885 scroll (marg_t, marg_b, -1, TRUE);
890 seen_disp_event = TRUE;
892 case 'Z': /* terminal type query */
893 compatibility(VT100);
894 ldisc->send ("\033[?6c", 5);
896 case 'c': /* restore power-on settings */
897 compatibility(VT100);
900 request_resize (80, rows, 1);
905 seen_disp_event = TRUE;
907 case '#': /* ESC # 8 fills screen with Es :-) */
908 compatibility(VT100);
909 termstate = SEEN_ESCHASH;
911 case 'H': /* set a tab */
912 compatibility(VT100);
918 termstate = TOPLEVEL; /* default */
921 if (esc_nargs <= ARGS_MAX) {
922 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
923 esc_args[esc_nargs-1] = 0;
924 esc_args[esc_nargs-1] =
925 10 * esc_args[esc_nargs-1] + c - '0';
927 termstate = SEEN_CSI;
931 if (++esc_nargs <= ARGS_MAX)
932 esc_args[esc_nargs-1] = ARG_DEFAULT;
933 termstate = SEEN_CSI;
937 if( esc_query ) esc_query = -1;
938 else if( c == '?' ) esc_query = TRUE;
940 termstate = SEEN_CSI;
942 else switch (ANSI(c,esc_query)) {
943 case 'A': /* move up N lines */
944 move (curs_x, curs_y - def(esc_args[0], 1), 1);
945 seen_disp_event = TRUE;
947 case 'e': /* move down N lines */
950 move (curs_x, curs_y + def(esc_args[0], 1), 1);
951 seen_disp_event = TRUE;
953 case 'a': /* move right N cols */
956 move (curs_x + def(esc_args[0], 1), curs_y, 1);
957 seen_disp_event = TRUE;
959 case 'D': /* move left N cols */
960 move (curs_x - def(esc_args[0], 1), curs_y, 1);
961 seen_disp_event = TRUE;
963 case 'E': /* move down N lines and CR */
965 move (0, curs_y + def(esc_args[0], 1), 1);
966 seen_disp_event = TRUE;
968 case 'F': /* move up N lines and CR */
970 move (0, curs_y - def(esc_args[0], 1), 1);
971 seen_disp_event = TRUE;
973 case 'G': case '`': /* set horizontal posn */
975 move (def(esc_args[0], 1) - 1, curs_y, 0);
976 seen_disp_event = TRUE;
978 case 'd': /* set vertical posn */
980 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
982 seen_disp_event = TRUE;
984 case 'H': case 'f': /* set horz and vert posns at once */
986 esc_args[1] = ARG_DEFAULT;
987 move (def(esc_args[1], 1) - 1,
988 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
990 seen_disp_event = TRUE;
992 case 'J': /* erase screen or parts of it */
994 unsigned int i = def(esc_args[0], 0) + 1;
997 erase_lots(FALSE, !!(i & 2), !!(i & 1));
1000 seen_disp_event = TRUE;
1002 case 'K': /* erase line or parts of it */
1004 unsigned int i = def(esc_args[0], 0) + 1;
1007 erase_lots(TRUE, !!(i & 2), !!(i & 1));
1009 seen_disp_event = TRUE;
1011 case 'L': /* insert lines */
1012 compatibility(VT102);
1013 if (curs_y <= marg_b)
1014 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
1015 seen_disp_event = TRUE;
1017 case 'M': /* delete lines */
1018 compatibility(VT102);
1019 if (curs_y <= marg_b)
1020 scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
1021 seen_disp_event = TRUE;
1023 case '@': /* insert chars */
1024 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1025 compatibility(VT102);
1026 insch (def(esc_args[0], 1));
1027 seen_disp_event = TRUE;
1029 case 'P': /* delete chars */
1030 compatibility(VT102);
1031 insch (-def(esc_args[0], 1));
1032 seen_disp_event = TRUE;
1034 case 'c': /* terminal type query */
1035 compatibility(VT100);
1036 /* This is the response for a VT102 */
1037 ldisc->send ("\033[?6c", 5);
1039 case 'n': /* cursor position query */
1040 if (esc_args[0] == 6) {
1041 /* Wonder of wonders ANSI.SYS has this!! */
1043 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
1044 ldisc->send (buf, strlen(buf));
1046 else if (esc_args[0] == 5) {
1047 /* Are we working ... well I suppose so :-) */
1048 /* But ANSI.SYS doesn't say it's working :-) :-) */
1049 compatibility(VT100);
1050 ldisc->send ("\033[0n", 4);
1053 case 'h': /* toggle modes to high */
1055 compatibility(VT100);
1058 for (i=0; i<esc_nargs; i++)
1059 toggle_mode (esc_args[i], esc_query, TRUE);
1062 case 'l': /* toggle modes to low */
1064 compatibility(VT100);
1067 for (i=0; i<esc_nargs; i++)
1068 toggle_mode (esc_args[i], esc_query, FALSE);
1071 case 'g': /* clear tabs */
1072 compatibility(VT100);
1073 if (esc_nargs == 1) {
1074 if (esc_args[0] == 0) {
1075 tabs[curs_x] = FALSE;
1076 } else if (esc_args[0] == 3) {
1078 for (i = 0; i < cols; i++)
1083 case 'r': /* set scroll margins */
1084 compatibility(VT100);
1085 if (esc_nargs <= 2) {
1087 /* VTTEST Bug 9 if the first arg is default _or_ zero
1088 * this is a full screen region irrespective of 2nd arg.
1090 if (esc_args[0] <= 0) {
1094 top = def(esc_args[0], 1) - 1;
1095 bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
1096 def(esc_args[1], rows)) - 1;
1105 * I used to think the cursor should be
1106 * placed at the top of the newly marginned
1107 * area. Apparently not: VMS TPU falls over
1110 * Well actually it should for Origin mode - RDB
1112 curs_y = (dec_om ? marg_t : 0);
1114 seen_disp_event = TRUE;
1118 case 'm': /* set graphics rendition */
1121 * A VT100 without the AVO only had one attribute, either
1122 * underline or reverse video depending on the cursor type,
1123 * this was selected by CSI 7m.
1126 * This is DIM on the VT100-AVO and VT102
1128 * This is BLINK on the VT100-AVO and VT102+
1130 * This is INVIS on the VT100-AVO and VT102
1132 * This like 22 disables BOLD, DIM and INVIS
1134 * The ANSI colours appear on any terminal that has colour
1135 * (obviously) but the interaction between sgr0 and the
1136 * colours varies but is usually related to the background
1137 * colour erase item.
1138 * The interaction between colour attributes and the mono
1139 * ones is also very implementation dependent.
1141 * The 39 and 49 attributes are likely to be unimplemented.
1144 for (i=0; i<esc_nargs; i++) {
1145 switch (def(esc_args[i], 0)) {
1146 case 0: /* restore defaults */
1147 curr_attr = ATTR_DEFAULT; break;
1148 case 1: /* enable bold */
1149 compatibility(VT100AVO);
1150 curr_attr |= ATTR_BOLD; break;
1151 case 21: /* (enable double underline) */
1152 compatibility(OTHER);
1153 case 4: /* enable underline */
1154 compatibility(VT100AVO);
1155 curr_attr |= ATTR_UNDER; break;
1156 case 5: /* enable blink */
1157 compatibility(VT100AVO);
1158 curr_attr |= ATTR_BLINK; break;
1159 case 7: /* enable reverse video */
1160 curr_attr |= ATTR_REVERSE; break;
1161 case 22: /* disable bold */
1162 compatibility(VT220);
1163 curr_attr &= ~ATTR_BOLD; break;
1164 case 24: /* disable underline */
1165 compatibility(VT220);
1166 curr_attr &= ~ATTR_UNDER; break;
1167 case 25: /* disable blink */
1168 compatibility(VT220);
1169 curr_attr &= ~ATTR_BLINK; break;
1170 case 27: /* disable reverse video */
1171 compatibility(VT220);
1172 curr_attr &= ~ATTR_REVERSE; break;
1173 case 30: case 31: case 32: case 33:
1174 case 34: case 35: case 36: case 37:
1176 curr_attr &= ~ATTR_FGMASK;
1177 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
1179 case 39: /* default-foreground */
1180 curr_attr &= ~ATTR_FGMASK;
1181 curr_attr |= ATTR_DEFFG;
1183 case 40: case 41: case 42: case 43:
1184 case 44: case 45: case 46: case 47:
1186 curr_attr &= ~ATTR_BGMASK;
1187 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
1189 case 49: /* default-background */
1190 curr_attr &= ~ATTR_BGMASK;
1191 curr_attr |= ATTR_DEFBG;
1196 erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
1199 case 's': /* save cursor */
1202 case 'u': /* restore cursor */
1203 save_cursor (FALSE);
1204 seen_disp_event = TRUE;
1206 case 't': /* set page size - ie window height */
1208 * VT340/VT420 sequence DECSLPP, DEC only allows values
1209 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1210 * illegal values (eg first arg 1..9) for window changing
1213 compatibility(VT340TEXT);
1214 if (esc_nargs<=1 && (esc_args[0]<1 || esc_args[0]>=24)) {
1215 unsigned int newrows = def(esc_args[0], 24);
1216 /* Hack: prevent big-resize DoS attack. */
1217 if (newrows > max(512, cfg.height))
1218 newrows = max(512, cfg.height);
1219 request_resize (cols, newrows, 0);
1223 case ANSI('|', '*'):
1224 /* VT420 sequence DECSNLS
1225 * Set number of lines on screen
1226 * VT420 uses VGA like hardware and can support any size in
1227 * reasonable range (24..49 AIUI) with no default specified.
1229 compatibility(VT420);
1230 if (esc_nargs==1 && esc_args[0]>=24) {
1231 unsigned int newrows = def(esc_args[0], cfg.height);
1232 /* Hack: prevent big-resize DoS attack. */
1233 if (newrows > max(512, cfg.height))
1234 newrows = max(512, cfg.height);
1235 request_resize (cols, newrows, 0);
1239 case ANSI('|', '$'):
1240 /* VT340/VT420 sequence DECSCPP
1241 * Set number of columns per page
1242 * Docs imply range is only 80 or 132, but I'll allow any.
1244 compatibility(VT340TEXT);
1246 unsigned int newcols = def(esc_args[0], cfg.width);
1247 /* Hack: prevent big-resize DoS attack. */
1248 if (newcols > max(512, cfg.width))
1249 newcols = max(512, cfg.width);
1250 request_resize (newcols, rows, 0);
1254 case 'X': /* write N spaces w/o moving cursor */
1255 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1256 compatibility(VT100);
1258 int n = def(esc_args[0], 1);
1259 unsigned long *p = cpos;
1260 if (n > cols - curs_x)
1262 check_selection (cpos, cpos+n);
1265 seen_disp_event = TRUE;
1268 case 'x': /* report terminal characteristics */
1269 compatibility(VT100);
1272 int i = def(esc_args[0], 0);
1273 if (i == 0 || i == 1) {
1274 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1276 ldisc->send (buf, 20);
1281 compatibility(OTHER);
1282 use_bce = (esc_args[0]<=0);
1283 erase_char = ERASE_CHAR;
1285 erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
1288 /* Allow the host to make this emulator a 'perfect' VT102.
1289 * This first appeared in the VT220, but we do need to get
1290 * back to PuTTY mode so I won't check it.
1292 * The arg == 60 is a PuTTY extension.
1293 * The 2nd arg, 8bit vs 7bit is not obeyed.
1295 * Setting VT102 mode should also change the Fkeys to
1296 * generate PF* codes as a real VT102 has no Fkeys.
1297 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1300 * Note ESC c will NOT change this!
1303 if (esc_args[0] == 61) compatibility_level = TM_VT102;
1304 else if (esc_args[0] == 60) compatibility_level = TM_ANSIMIN;
1305 else compatibility_level = TM_PUTTY;
1311 /* VT100 only here, checked above */
1314 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1317 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1320 default: /* specifically, 'B' */
1321 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1324 if( !has_compat(VT220) || c != '%' )
1325 termstate = TOPLEVEL;
1330 case 'P': /* Linux palette sequence */
1331 termstate = SEEN_OSC_P;
1334 case 'R': /* Linux palette reset */
1337 termstate = TOPLEVEL;
1339 case 'W': /* word-set */
1340 termstate = SEEN_OSC_W;
1343 case '0': case '1': case '2': case '3': case '4':
1344 case '5': case '6': case '7': case '8': case '9':
1345 esc_args[0] = 10 * esc_args[0] + c - '0';
1349 * Grotty hack to support xterm and DECterm title
1350 * sequences concurrently.
1352 if (esc_args[0] == 2) {
1356 /* else fall through */
1358 termstate = OSC_STRING;
1364 * This OSC stuff is EVIL. It takes just one character to get into
1365 * sysline mode and it's not initially obvious how to get out.
1366 * So I've added CR and LF as string aborts.
1367 * This shouldn't effect compatibility as I believe embedded
1368 * control characters are supposed to be interpreted (maybe?)
1369 * and they don't display anything useful anyway.
1373 if (c == '\n' || c == '\r') {
1374 termstate = TOPLEVEL;
1375 } else if (c == 0234 || c == '\007' ) {
1377 * These characters terminate the string; ST and BEL
1378 * terminate the sequence and trigger instant
1379 * processing of it, whereas ESC goes back to SEEN_ESC
1380 * mode unless it is followed by \, in which case it is
1381 * synonymous with ST in the first place.
1384 termstate = TOPLEVEL;
1385 } else if (c == '\033')
1386 termstate = OSC_MAYBE_ST;
1387 else if (osc_strlen < OSC_STR_MAX)
1388 osc_string[osc_strlen++] = c;
1392 int max = (osc_strlen == 0 ? 21 : 16);
1394 if (c >= '0' && c <= '9')
1396 else if (c >= 'A' && c <= 'A'+max-10)
1398 else if (c >= 'a' && c <= 'a'+max-10)
1401 termstate = TOPLEVEL;
1402 osc_string[osc_strlen++] = val;
1403 if (osc_strlen >= 7) {
1404 palette_set (osc_string[0],
1405 osc_string[1] * 16 + osc_string[2],
1406 osc_string[3] * 16 + osc_string[4],
1407 osc_string[5] * 16 + osc_string[6]);
1409 termstate = TOPLEVEL;
1415 case '0': case '1': case '2': case '3': case '4':
1416 case '5': case '6': case '7': case '8': case '9':
1417 esc_args[0] = 10 * esc_args[0] + c - '0';
1420 termstate = OSC_STRING;
1426 unsigned long *p = scrtop;
1427 int n = rows * (cols+1);
1429 *p++ = ATTR_DEFAULT | 'E';
1431 seen_disp_event = TRUE;
1432 check_selection (scrtop, scrtop + rows * (cols+1));
1434 termstate = TOPLEVEL;
1437 termstate = TOPLEVEL;
1438 seen_disp_event = TRUE;
1441 move (curs_x, curs_y - 1, 1);
1444 move (curs_x, curs_y + 1, 1);
1447 move (curs_x + 1, curs_y, 1);
1450 move (curs_x - 1, curs_y, 1);
1453 cset_attr[cset=0] = ATTR_LINEDRW;
1456 cset_attr[cset=0] = ATTR_ASCII;
1463 scroll (0, rows-1, -1, TRUE);
1464 else if (curs_y > 0)
1470 erase_lots(FALSE, FALSE, TRUE);
1474 erase_lots(TRUE, FALSE, TRUE);
1477 /* XXX Print cursor line */
1480 /* XXX Start controller mode */
1483 /* XXX Stop controller mode */
1486 termstate = VT52_Y1;
1489 ldisc->send ("\033/Z", 3);
1492 app_cursor_keys = TRUE;
1495 app_cursor_keys = FALSE;
1498 /* XXX This should switch to VT100 mode not current or default
1499 * VT mode. But this will only have effect in a VT220+
1505 /* XXX Enter auto print mode */
1508 /* XXX Exit auto print mode */
1511 /* XXX Print screen */
1516 termstate = VT52_Y2;
1517 move(curs_x, c-' ', 0);
1520 termstate = TOPLEVEL;
1521 move(c-' ', curs_y, 0);
1524 if (selstate != NO_SELECTION)
1525 check_selection (cpos, cpos+1);
1530 * Compare two lines to determine whether they are sufficiently
1531 * alike to scroll-optimise one to the other. Return the degree of
1534 static int linecmp (unsigned long *a, unsigned long *b) {
1537 for (i=n=0; i < cols; i++)
1538 n += (*a++ == *b++);
1543 * Given a context, update the window. Out of paranoia, we don't
1544 * allow WM_PAINT responses to do scrolling optimisations.
1546 static void do_paint (Context ctx, int may_optimise){
1547 int i, j, start, our_curs_y;
1548 unsigned long attr, rv, cursor;
1553 if (blinker || !cfg.blink_cur)
1554 cursor = ATTR_ACTCURS;
1559 cursor = ATTR_PASCURS;
1562 rv = (rvideo ? ATTR_REVERSE : 0);
1563 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1565 for (i=0; i<rows; i++) {
1566 int idx = i*(cols+1);
1567 for (j=0; j<=cols; j++,idx++) {
1568 unsigned long *d = disptop+idx;
1569 wanttext[idx] = ((*d ^ rv
1570 ^ (selstart <= d && d < selend ?
1571 ATTR_REVERSE : 0)) |
1572 (i==our_curs_y && j==curs_x ? cursor : 0));
1577 * We would perform scrolling optimisations in here, if they
1578 * didn't have a nasty tendency to cause the whole sodding
1579 * program to hang for a second at speed-critical moments.
1580 * We'll leave it well alone...
1583 for (i=0; i<rows; i++) {
1584 int idx = i*(cols+1);
1586 for (j=0; j<=cols; j++,idx++) {
1587 unsigned long t = wanttext[idx];
1588 int needs_update = (j < cols && t != disptext[idx]);
1589 int keep_going = (start != -1 && needs_update &&
1590 (t & ATTR_MASK) == attr &&
1591 j-start < sizeof(ch));
1592 if (start != -1 && !keep_going) {
1593 do_text (ctx, start, i, ch, j-start, attr);
1599 attr = t & ATTR_MASK;
1601 ch[j-start] = (char) (t & CHAR_MASK);
1609 * Flick the switch that says if blinking things should be shown or hidden.
1612 void term_blink(int flg) {
1613 static long last_blink = 0;
1614 long now, blink_diff;
1618 last_blink = GetTickCount();
1621 now = GetTickCount();
1623 blink_diff = now-last_blink;
1625 /* Make sure the cursor blinks no more than 2Hz */
1626 if (blink_diff>=0 && blink_diff<450)
1634 * Invalidate the whole screen so it will be repainted in full.
1636 void term_invalidate(void) {
1639 for (i=0; i<rows*(cols+1); i++)
1640 disptext[i] = ATTR_INVALID;
1644 * Paint the window in response to a WM_PAINT message.
1646 void term_paint (Context ctx, int l, int t, int r, int b) {
1647 int i, j, left, top, right, bottom;
1649 left = l / font_width;
1650 right = (r - 1) / font_width;
1651 top = t / font_height;
1652 bottom = (b - 1) / font_height;
1653 for (i = top; i <= bottom && i < rows ; i++)
1654 for (j = left; j <= right && j < cols ; j++)
1655 disptext[i*(cols+1)+j] = ATTR_INVALID;
1657 /* This should happen soon enough, also for some reason it sometimes
1658 * fails to actually do anything when re-sizing ... painting the wrong
1660 do_paint (ctx, FALSE);
1665 * Attempt to scroll the scrollback. The second parameter gives the
1666 * position we want to scroll to; the first is +1 to denote that
1667 * this position is relative to the beginning of the scrollback, -1
1668 * to denote it is relative to the end, and 0 to denote that it is
1669 * relative to the current position.
1671 void term_scroll (int rel, int where) {
1672 int n = where * (cols+1);
1674 disptop = (rel < 0 ? scrtop :
1675 rel > 0 ? sbtop : disptop) + n;
1676 if (disptop < sbtop)
1678 if (disptop > scrtop)
1685 * Spread the selection outwards according to the selection mode.
1687 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1688 unsigned long *linestart, *lineend;
1692 x = (p - text) % (cols+1);
1694 lineend = linestart + cols;
1699 * In this mode, every character is a separate unit, except
1700 * for runs of spaces at the end of a non-wrapping line.
1702 if (!(linestart[cols] & ATTR_WRAPPED)) {
1703 unsigned long *q = lineend;
1704 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1709 p = (dir == -1 ? q : lineend - 1);
1714 * In this mode, the units are maximal runs of characters
1715 * whose `wordness' has the same value.
1717 wvalue = wordness[*p & CHAR_MASK];
1719 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1722 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1728 * In this mode, every line is a unit.
1730 p = (dir == -1 ? linestart : lineend - 1);
1736 static void sel_spread (void) {
1737 selstart = sel_spread_half (selstart, -1);
1738 selend = sel_spread_half (selend - 1, +1) + 1;
1741 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
1742 unsigned long *selpoint;
1745 if (y>=rows) y = rows-1;
1753 if (x>=cols) x = cols-1;
1755 selpoint = disptop + y * (cols+1) + x;
1757 if (b == MB_SELECT && a == MA_CLICK) {
1759 selstate = ABOUT_TO;
1760 selanchor = selpoint;
1762 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1764 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1765 selstate = DRAGGING;
1766 selstart = selanchor = selpoint;
1767 selend = selstart + 1;
1769 } else if ((b == MB_SELECT && a == MA_DRAG) ||
1770 (b == MB_EXTEND && a != MA_RELEASE)) {
1771 if (selstate == ABOUT_TO && selanchor == selpoint)
1773 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1774 if (selpoint-selstart < (selend-selstart)/2)
1775 selanchor = selend - 1;
1777 selanchor = selstart;
1778 selstate = DRAGGING;
1780 if (selstate != ABOUT_TO && selstate != DRAGGING)
1781 selanchor = selpoint;
1782 selstate = DRAGGING;
1783 if (selpoint < selanchor) {
1784 selstart = selpoint;
1785 selend = selanchor + 1;
1787 selstart = selanchor;
1788 selend = selpoint + 1;
1791 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
1792 if (selstate == DRAGGING) {
1794 * We've completed a selection. We now transfer the
1795 * data to the clipboard.
1797 unsigned char *p = selspace;
1798 unsigned long *q = selstart;
1800 while (q < selend) {
1802 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1803 unsigned long *nlpos = lineend;
1805 if (!(*nlpos & ATTR_WRAPPED)) {
1806 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1811 while (q < nlpos && q < selend)
1812 *p++ = (unsigned char) (*q++ & CHAR_MASK);
1815 for (i=0; i<sizeof(sel_nl); i++)
1818 q = lineend + 1; /* start of next line */
1820 write_clip (selspace, p - selspace);
1821 selstate = SELECTED;
1823 selstate = NO_SELECTION;
1824 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
1828 get_clip((void **) &data, &len);
1832 while (p < data+len) {
1833 while (p < data+len &&
1834 !(p <= data+len-sizeof(sel_nl) &&
1835 !memcmp(p, sel_nl, sizeof(sel_nl))))
1843 c=xlat_kbd2tty(q[i]);
1848 if (p <= data+len-sizeof(sel_nl) &&
1849 !memcmp(p, sel_nl, sizeof(sel_nl))) {
1850 ldisc->send ("\r", 1);
1851 p += sizeof(sel_nl);
1856 get_clip(NULL, NULL);
1862 static void deselect (void) {
1863 selstate = NO_SELECTION;
1864 selstart = selend = scrtop;
1867 void term_deselect (void) {