15 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
16 #define CL_VT100 0x0002 /* VT100 */
17 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
18 #define CL_VT102 0x0008 /* VT102 */
19 #define CL_VT220 0x0010 /* VT220 */
20 #define CL_VT320 0x0020 /* VT320 */
21 #define CL_VT420 0x0040 /* VT420 */
22 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
23 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
24 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
25 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
26 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
28 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
29 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
30 #define TM_VT102 (TM_VT100AVO|CL_VT102)
31 #define TM_VT220 (TM_VT102|CL_VT220)
32 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
33 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
35 #define TM_PUTTY (0xFFFF)
37 #define compatibility(x) \
38 if ( ((CL_##x)&compatibility_level) == 0 ) { \
42 #define compatibility2(x,y) \
43 if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
48 #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
50 static int compatibility_level = TM_PUTTY;
53 static unsigned long *text; /* buffer of text on terminal screen */
54 static unsigned long *scrtop; /* top of working screen */
55 static unsigned long *disptop; /* top of displayed screen */
56 static unsigned long *sbtop; /* top of scrollback */
57 static unsigned long *sbbot; /* furthest extent of scrollback */
58 static unsigned long *cpos; /* cursor position (convenience) */
59 static unsigned long *disptext; /* buffer of text on real screen */
60 static unsigned long *wanttext; /* buffer of text we want on screen */
61 static unsigned long *alttext; /* buffer of text on alt. screen */
63 static unsigned char *selspace; /* buffer for building selections in */
65 #define TSIZE (sizeof(*text))
66 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
68 static unsigned long curr_attr, save_attr;
69 static unsigned long erase_char = ERASE_CHAR;
71 static int curs_x, curs_y; /* cursor */
72 static int save_x, save_y; /* saved cursor position */
73 static int marg_t, marg_b; /* scroll margins */
74 static int dec_om; /* DEC origin mode flag */
75 static int wrap, wrapnext; /* wrap flags */
76 static int insert; /* insert-mode flag */
77 static int cset; /* 0 or 1: which char set */
78 static int save_cset, save_csattr; /* saved with cursor position */
79 static int rvideo; /* global reverse video flag */
80 static int cursor_on; /* cursor enabled flag */
81 static int reset_132; /* Flag ESC c resets to 80 cols */
82 static int use_bce; /* Use Background coloured erase */
83 static int blinker; /* When blinking is the cursor on ? */
84 static int tblinker; /* When the blinking text is on */
85 static int blink_is_real; /* Actually blink blinking text */
87 static unsigned long cset_attr[2];
90 * Saved settings on the alternate screen.
92 static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
93 static int alt_t, alt_b;
96 #define ARGS_MAX 32 /* max # of esc sequence arguments */
97 #define ARG_DEFAULT 0 /* if an arg isn't specified */
98 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
99 static int esc_args[ARGS_MAX];
100 static int esc_nargs;
101 static int esc_query;
102 #define ANSI(x,y) ((x)+((y)<<8))
103 #define ANSI_QUE(x) ANSI(x,TRUE)
105 #define OSC_STR_MAX 2048
106 static int osc_strlen;
107 static char osc_string[OSC_STR_MAX+1];
110 static char id_string[1024] = "\033[?6c";
112 static unsigned char *tabs;
126 OSC_STRING, OSC_MAYBE_ST,
134 NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
137 SM_CHAR, SM_WORD, SM_LINE
139 static unsigned long *selstart, *selend, *selanchor;
141 static short wordness[256] = {
142 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 */
143 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 */
144 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 */
145 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 */
146 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 */
147 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 */
148 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 */
149 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 */
152 static unsigned char sel_nl[] = SEL_NL;
153 static char * paste_buffer = 0;
154 static int paste_len, paste_pos, paste_hold;
157 * Internal prototypes.
159 static void do_paint (Context, int);
160 static void erase_lots (int, int, int);
161 static void swap_screen (int);
162 static void update_sbar (void);
163 static void deselect (void);
166 * Set up power-on settings for the terminal.
168 static void power_on(void) {
169 curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
172 alt_b = marg_b = rows - 1;
177 for (i = 0; i < cols; i++)
178 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
180 alt_om = dec_om = cfg.dec_om;
181 alt_wnext = wrapnext = alt_ins = insert = FALSE;
182 alt_wrap = wrap = cfg.wrap_mode;
184 cset_attr[0] = cset_attr[1] = ATTR_ASCII;
187 save_attr = curr_attr = ATTR_DEFAULT;
188 app_cursor_keys = cfg.app_cursor;
189 app_keypad_keys = cfg.app_keypad;
191 blink_is_real = cfg.blinktext;
192 erase_char = ERASE_CHAR;
196 for (i = 0; i < 256; i++)
197 wordness[i] = cfg.wordness[i];
201 erase_lots (FALSE, TRUE, TRUE);
203 erase_lots (FALSE, TRUE, TRUE);
208 * Force a screen update.
210 void term_update(void) {
214 if ( (seen_key_event && (cfg.scroll_on_key)) ||
215 (seen_disp_event && (!cfg.scroll_on_key)) ) {
217 seen_disp_event = seen_key_event = 0;
220 do_paint (ctx, TRUE);
221 sys_cursor(curs_x, curs_y + (scrtop - disptop) / (cols+1));
227 * Same as power_on(), but an external function.
229 void term_pwron(void) {
238 * Clear the scrollback.
240 void term_clrsb(void) {
241 disptop = sbtop = scrtop;
246 * Initialise the terminal.
248 void term_init(void) {
249 text = sbtop = sbbot = scrtop = disptop = cpos = NULL;
250 disptext = wanttext = NULL;
259 * Set up the terminal for a given size.
261 void term_size(int newrows, int newcols, int newsavelines) {
262 unsigned long *newtext, *newdisp, *newwant, *newalt;
263 int i, j, crows, ccols;
265 int save_alt_which = alt_which;
267 if (newrows == rows && newcols == cols && newsavelines == savelines)
268 return; /* nothing to do */
274 alt_b = marg_b = newrows - 1;
276 newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
277 sbbot = newtext + newsavelines*(newcols+1);
278 for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
279 newtext[i] = erase_char;
281 crows = rows + (scrtop - sbtop) / (cols+1);
282 if (crows > newrows+newsavelines)
283 crows = newrows+newsavelines;
287 disptop = newtext + (crows-newrows)*(newcols+1);
288 ccols = (cols < newcols ? cols : newcols);
289 for (i=0; i<crows; i++) {
290 int oldidx = (rows - crows + i) * (cols+1);
291 int newidx = (newrows - crows + i) * (newcols+1);
293 for (j=0; j<ccols; j++)
294 disptop[newidx+j] = scrtop[oldidx+j];
295 disptop[newidx+newcols] =
296 (cols == newcols ? scrtop[oldidx+cols]
297 : (scrtop[oldidx+cols]&LATTR_MODE));
301 sbtop = disptop = newtext;
307 newdisp = smalloc (newrows*(newcols+1)*TSIZE);
308 for (i=0; i<newrows*(newcols+1); i++)
309 newdisp[i] = ATTR_INVALID;
313 newwant = smalloc (newrows*(newcols+1)*TSIZE);
314 for (i=0; i<newrows*(newcols+1); i++)
315 newwant[i] = ATTR_INVALID;
319 newalt = smalloc (newrows*(newcols+1)*TSIZE);
320 for (i=0; i<newrows*(newcols+1); i++)
321 newalt[i] = erase_char;
326 selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
328 tabs = srealloc (tabs, newcols*sizeof(*tabs));
331 for (i = (cols > 0 ? cols : 0); i < newcols; i++)
332 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
336 curs_y += newrows - rows;
339 if (curs_y >= newrows)
341 if (curs_x >= newcols)
344 wrapnext = alt_wnext = FALSE;
348 savelines = newsavelines;
351 swap_screen(save_alt_which);
360 static void swap_screen (int which) {
364 if (which == alt_which)
369 for (t=0; t<rows*(cols+1); t++) {
370 tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
373 t = curs_x; curs_x = alt_x; alt_x = t;
374 t = curs_y; curs_y = alt_y; alt_y = t;
375 t = marg_t; marg_t = alt_t; alt_t = t;
376 t = marg_b; marg_b = alt_b; alt_b = t;
377 t = dec_om; dec_om = alt_om; alt_om = t;
378 t = wrap; wrap = alt_wrap; alt_wrap = t;
379 t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
380 t = insert; insert = alt_ins; alt_ins = t;
381 t = cset; cset = alt_cset; alt_cset = t;
387 * Update the scroll bar.
389 static void update_sbar(void) {
392 min = (sbtop - text) / (cols+1);
393 set_sbar ((scrtop - text) / (cols+1) + rows - min,
394 (disptop - text) / (cols+1) - min,
399 * Check whether the region bounded by the two pointers intersects
400 * the scroll region, and de-select the on-screen selection if so.
402 static void check_selection (unsigned long *from, unsigned long *to) {
403 if (from < selend && selstart < to)
408 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
409 * for backward.) `sb' is TRUE if the scrolling is permitted to
410 * affect the scrollback buffer.
412 static void scroll (int topline, int botline, int lines, int sb) {
413 unsigned long *scroll_top;
414 unsigned long *newscr;
415 int scroll_size, size, i;
416 int scrtop_is_disptop = (scrtop==disptop);
417 static int recursive = 0;
419 /* Only scroll more than the window if we're doing a 10% scroll */
420 if (!recursive && lines > botline - topline + 1)
421 lines = botline - topline + 1;
423 scroll_top = scrtop + topline*(cols+1);
424 size = (lines < 0 ? -lines : lines) * (cols+1);
425 scroll_size = (botline - topline + 1) * (cols+1) - size;
427 if (lines > 0 && topline == 0 && alt_which == 0 && sb) {
429 * Since we're going to scroll the top line and we're on the
430 * scrolling screen let's also effect the scrollback buffer.
432 * This is normally done by moving the position the screen
433 * painter reads from to reduce the amount of memory copying
436 if (scroll_size >= 0 && !recursive) {
437 newscr = scrtop + lines * (cols+1);
439 if (newscr > sbbot && botline == rows-1) {
440 /* We've hit the bottom of memory, so we have to do a
441 * physical scroll. But instead of just 1 line do it
442 * by 10% of the available memory.
444 * If the scroll region isn't the whole screen then we can't
445 * do this as it stands. We would need to recover the bottom
446 * of the screen from the scroll buffer after being sure that
447 * it doesn't get deleted.
450 i = (rows+savelines)/10;
452 /* Make it simple and ensure safe recursion */
453 if ( i<savelines-1) {
455 scroll(topline, botline, i, sb);
458 newscr = scrtop - i * (cols+1);
459 if (scrtop_is_disptop) disptop = newscr;
463 newscr = scrtop + lines * (cols+1);
466 if (newscr <= sbbot) {
467 if (scrtop_is_disptop) disptop = newscr;
470 if (botline == rows-1 )
471 for (i = 0; i < size; i++)
472 scrtop[i+scroll_size] = erase_char;
477 if (botline != rows-1) {
478 /* This fastscroll only works for full window scrolls.
479 * If the caller wanted a partial one we have to reverse
480 * scroll the bottom of the screen.
482 scroll(botline-lines+1, rows-1, -lines, 0);
487 /* If we can't scroll by memory remapping do it physically.
488 * But rather than expensivly doing the scroll buffer just
489 * scroll the screen. All it means is that sometimes we choose
490 * to not add lines from a scroll region to the scroll buffer.
493 if (savelines <= 400) {
494 sbtop -= lines * (cols+1);
497 scroll_size += scroll_top - sbtop;
503 /* Ho hum, expensive scroll required. */
505 sbtop -= lines * (cols+1);
508 scroll_size += scroll_top - sbtop;
515 if (scroll_size < 0) {
522 memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
523 for (i = 0; i < size; i++)
524 scroll_top[i+scroll_size] = erase_char;
525 if (selstart > scroll_top &&
526 selstart < scroll_top + size + scroll_size) {
528 if (selstart < scroll_top)
529 selstart = scroll_top;
531 if (selend > scroll_top &&
532 selend < scroll_top + size + scroll_size) {
534 if (selend < scroll_top)
537 if (scrtop_is_disptop)
540 if (disptop > scroll_top &&
541 disptop < scroll_top + size + scroll_size) {
543 if (disptop < scroll_top)
544 disptop = scroll_top;
548 memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
549 for (i = 0; i < size; i++)
550 scroll_top[i] = erase_char;
551 if (selstart > scroll_top &&
552 selstart < scroll_top + size + scroll_size) {
554 if (selstart > scroll_top + size + scroll_size)
555 selstart = scroll_top + size + scroll_size;
557 if (selend > scroll_top &&
558 selend < scroll_top + size + scroll_size) {
560 if (selend > scroll_top + size + scroll_size)
561 selend = scroll_top + size + scroll_size;
563 if (scrtop_is_disptop)
565 else if (disptop > scroll_top &&
566 disptop < scroll_top + size + scroll_size) {
568 if (disptop > scroll_top + size + scroll_size)
569 disptop = scroll_top + size + scroll_size;
575 * Move the cursor to a given position, clipping at boundaries. We
576 * may or may not want to clip at the scroll margin: marg_clip is 0
577 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
578 * even _being_ outside the margins.
580 static void move (int x, int y, int marg_clip) {
586 if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
588 if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
602 * Save or restore the cursor and SGR mode.
604 static void save_cursor(int save) {
608 save_attr = curr_attr;
610 save_csattr = cset_attr[cset];
614 /* Make sure the window hasn't shrunk since the save */
615 if (curs_x >= cols) curs_x = cols-1;
616 if (curs_y >= rows) curs_y = rows-1;
618 curr_attr = save_attr;
620 cset_attr[cset] = save_csattr;
622 if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
627 * Erase a large portion of the screen: the whole screen, or the
628 * whole line, or parts thereof.
630 static void erase_lots (int line_only, int from_begin, int to_end) {
631 unsigned long *startpos, *endpos;
634 startpos = cpos - curs_x;
635 endpos = startpos + cols;
636 /* I've removed the +1 so that the Wide screen stuff is not
637 * removed when it shouldn't be.
641 endpos = startpos + rows * (cols+1);
647 check_selection (startpos, endpos);
649 /* Clear screen also forces a full window redraw, just in case. */
650 if (startpos == scrtop && endpos == scrtop + rows * (cols+1))
653 while (startpos < endpos)
654 *startpos++ = erase_char;
658 * Insert or delete characters within the current line. n is +ve if
659 * insertion is desired, and -ve for deletion.
661 static void insch (int n) {
662 int dir = (n < 0 ? -1 : +1);
665 n = (n < 0 ? -n : n);
666 if (n > cols - curs_x)
668 m = cols - curs_x - n;
669 check_selection (cpos, cpos+n);
671 memmove (cpos, cpos+n, m*TSIZE);
673 cpos[m++] = erase_char;
675 memmove (cpos+n, cpos, m*TSIZE);
677 cpos[n] = erase_char;
682 * Toggle terminal mode `mode' to state `state'. (`query' indicates
683 * whether the mode is a DEC private one or a normal one.)
685 static void toggle_mode (int mode, int query, int state) {
686 if (query) switch (mode) {
687 case 1: /* application cursor keys */
688 app_cursor_keys = state;
690 case 2: /* VT52 mode */
693 case 3: /* 80/132 columns */
695 request_resize (state ? 132 : 80, rows, 1);
698 case 5: /* reverse video */
700 seen_disp_event = TRUE;
701 if (state) term_update();
703 case 6: /* DEC origin mode */
706 case 7: /* auto wrap */
709 case 8: /* auto key repeat */
712 case 25: /* enable/disable cursor */
713 compatibility2(OTHER,VT220);
715 seen_disp_event = TRUE;
717 case 47: /* alternate screen */
718 compatibility(OTHER);
723 } else switch (mode) {
724 case 4: /* set insert mode */
725 compatibility(VT102);
728 case 12: /* set echo mode */
730 * This may be very good in smcup and rmcup (or smkx & rmkx) if you
731 * have a long RTT and the telnet client/daemon doesn't understand
734 * DONT send TS_RECHO/TS_LECHO; the telnet daemon tries to fix the
735 * tty and _really_ confuses some programs.
737 compatibility2(OTHER,VT220);
738 ldisc = (state? &ldisc_simple : &ldisc_term);
740 case 20: /* Return sends ... */
741 cr_lf_return = state;
747 * Process an OSC sequence: set window title or icon name.
749 static void do_osc(void) {
752 wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
754 osc_string[osc_strlen] = '\0';
755 switch (esc_args[0]) {
758 set_icon (osc_string);
759 if (esc_args[0] == 1)
761 /* fall through: parameter 0 means set both */
764 set_title (osc_string);
771 * Remove everything currently in `inbuf' and stick it up on the
772 * in-memory display. There's a big state machine in here to
773 * process escape sequences...
775 void term_out(void) {
778 static int beep_overload = 0;
781 for(inbuf_reap = 0; inbuf_reap < inbuf_head; inbuf_reap++)
783 c = inbuf[inbuf_reap];
786 * Optionally log the session traffic to a file. Useful for
787 * debugging and possibly also useful for actual logging.
790 static FILE *fp = NULL;
791 if (!fp) fp = fopen(logfile, "wb");
792 if (fp) fputc (c, fp);
794 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
795 * be able to display 8-bit characters, but I'll let that go 'cause
798 if( ( (c&0x60) == 0 || c == '\177') &&
799 termstate < DO_CTRLS &&
800 ( (c&0x80) == 0 || has_compat(VT220))) {
802 case '\005': /* terminal type query */
803 /* Strictly speaking this is VT100 but a VT100 defaults to
804 * no response. Other terminals respond at their option.
806 * Don't put a CR in the default string as this tends to
807 * upset some weird software.
809 * An xterm returns "xterm" (5 characters)
811 compatibility(OTHER);
812 ldisc->send ("PuTTY", 5);
816 if(beep_count>6) beep_overload=1;
820 if (curs_x == 0 && curs_y == 0)
822 else if (curs_x == 0 && curs_y > 0)
823 curs_x = cols-1, curs_y--;
829 seen_disp_event = TRUE;
832 compatibility(VT100);
836 compatibility(VT100);
841 termstate = VT52_ESC;
843 compatibility(ANSIMIN);
844 termstate = SEEN_ESC;
848 compatibility(VT220);
849 termstate = SEEN_CSI;
851 esc_args[0] = ARG_DEFAULT;
855 compatibility(VT220);
856 termstate = SEEN_OSC;
863 seen_disp_event = TRUE;
868 compatibility(VT100);
870 if (curs_y == marg_b)
871 scroll (marg_t, marg_b, 1, TRUE);
872 else if (curs_y < rows-1)
883 unsigned long *old_cpos = cpos;
884 unsigned long *p = scrtop + curs_y * (cols+1) + cols;
888 } while (curs_x < cols-1 && !tabs[curs_x]);
890 if ((*p & LATTR_MODE) != LATTR_NORM)
892 if (curs_x >= cols/2)
902 check_selection (old_cpos, cpos);
904 seen_disp_event = TRUE;
906 case '\177': /* Destructive backspace
907 This does nothing on a real VT100 */
908 compatibility(OTHER);
909 if (curs_x && !wrapnext) curs_x--;
912 *cpos = (' ' | curr_attr | ATTR_ASCII);
916 else switch (termstate) {
918 /* Only graphic characters get this far, ctrls are stripped above */
920 cpos[1] |= ATTR_WRAPPED;
921 if (curs_y == marg_b)
922 scroll (marg_t, marg_b, 1, TRUE);
923 else if (curs_y < rows-1)
931 if (selstate != NO_SELECTION)
932 check_selection (cpos, cpos+1);
933 switch (cset_attr[cset]) {
934 /* Linedraw characters are different from 'ESC ( B' only
935 * for a small range, for ones outside that range make sure
936 * we use the same font as well as the same encoding.
939 if (c<0x5f || c>0x7F)
940 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
943 *cpos++ = ' ' | curr_attr | ATTR_ASCII;
945 *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW;
948 /* If UK-ASCII, make the '#' a LineDraw Pound */
950 *cpos++ = '}' | curr_attr | ATTR_LINEDRW;
955 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
956 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
960 if (curs_x == cols) {
969 termstate = TOPLEVEL;
973 * This state is virtually identical to SEEN_ESC, with the
974 * exception that we have an OSC sequence in the pipeline,
975 * and _if_ we see a backslash, we process it.
979 termstate = TOPLEVEL;
982 /* else fall through */
984 termstate = TOPLEVEL;
986 case ' ': /* some weird sequence? */
987 compatibility(VT220);
988 termstate = IGNORE_NEXT;
990 case '[': /* enter CSI mode */
991 termstate = SEEN_CSI;
993 esc_args[0] = ARG_DEFAULT;
996 case ']': /* xterm escape sequences */
997 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
998 compatibility(OTHER);
999 termstate = SEEN_OSC;
1002 case '(': /* should set GL */
1003 compatibility(VT100);
1006 case ')': /* should set GR */
1007 compatibility(VT100);
1010 case '7': /* save cursor */
1011 compatibility(VT100);
1014 case '8': /* restore cursor */
1015 compatibility(VT100);
1016 save_cursor (FALSE);
1017 seen_disp_event = TRUE;
1020 compatibility(VT100);
1021 app_keypad_keys = TRUE;
1024 compatibility(VT100);
1025 app_keypad_keys = FALSE;
1027 case 'D': /* exactly equivalent to LF */
1028 compatibility(VT100);
1029 if (curs_y == marg_b)
1030 scroll (marg_t, marg_b, 1, TRUE);
1031 else if (curs_y < rows-1)
1035 seen_disp_event = TRUE;
1037 case 'E': /* exactly equivalent to CR-LF */
1038 compatibility(VT100);
1040 if (curs_y == marg_b)
1041 scroll (marg_t, marg_b, 1, TRUE);
1042 else if (curs_y < rows-1)
1046 seen_disp_event = TRUE;
1048 case 'M': /* reverse index - backwards LF */
1049 compatibility(VT100);
1050 if (curs_y == marg_t)
1051 scroll (marg_t, marg_b, -1, TRUE);
1052 else if (curs_y > 0)
1056 seen_disp_event = TRUE;
1058 case 'Z': /* terminal type query */
1059 compatibility(VT100);
1060 ldisc->send (id_string, strlen(id_string));
1062 case 'c': /* restore power-on settings */
1063 compatibility(VT100);
1066 request_resize (80, rows, 1);
1071 seen_disp_event = TRUE;
1073 case '#': /* ESC # 8 fills screen with Es :-) */
1074 compatibility(VT100);
1075 termstate = SEEN_ESCHASH;
1077 case 'H': /* set a tab */
1078 compatibility(VT100);
1079 tabs[curs_x] = TRUE;
1084 termstate = TOPLEVEL; /* default */
1087 if (esc_nargs <= ARGS_MAX) {
1088 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
1089 esc_args[esc_nargs-1] = 0;
1090 esc_args[esc_nargs-1] =
1091 10 * esc_args[esc_nargs-1] + c - '0';
1093 termstate = SEEN_CSI;
1097 if (++esc_nargs <= ARGS_MAX)
1098 esc_args[esc_nargs-1] = ARG_DEFAULT;
1099 termstate = SEEN_CSI;
1103 if( esc_query ) esc_query = -1;
1104 else if( c == '?' ) esc_query = TRUE;
1106 termstate = SEEN_CSI;
1108 else switch (ANSI(c,esc_query)) {
1109 case 'A': /* move up N lines */
1110 move (curs_x, curs_y - def(esc_args[0], 1), 1);
1111 seen_disp_event = TRUE;
1113 case 'e': /* move down N lines */
1114 compatibility(ANSI);
1116 move (curs_x, curs_y + def(esc_args[0], 1), 1);
1117 seen_disp_event = TRUE;
1119 case 'a': /* move right N cols */
1120 compatibility(ANSI);
1122 move (curs_x + def(esc_args[0], 1), curs_y, 1);
1123 seen_disp_event = TRUE;
1125 case 'D': /* move left N cols */
1126 move (curs_x - def(esc_args[0], 1), curs_y, 1);
1127 seen_disp_event = TRUE;
1129 case 'E': /* move down N lines and CR */
1130 compatibility(ANSI);
1131 move (0, curs_y + def(esc_args[0], 1), 1);
1132 seen_disp_event = TRUE;
1134 case 'F': /* move up N lines and CR */
1135 compatibility(ANSI);
1136 move (0, curs_y - def(esc_args[0], 1), 1);
1137 seen_disp_event = TRUE;
1139 case 'G': case '`': /* set horizontal posn */
1140 compatibility(ANSI);
1141 move (def(esc_args[0], 1) - 1, curs_y, 0);
1142 seen_disp_event = TRUE;
1144 case 'd': /* set vertical posn */
1145 compatibility(ANSI);
1146 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1148 seen_disp_event = TRUE;
1150 case 'H': case 'f': /* set horz and vert posns at once */
1152 esc_args[1] = ARG_DEFAULT;
1153 move (def(esc_args[1], 1) - 1,
1154 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1156 seen_disp_event = TRUE;
1158 case 'J': /* erase screen or parts of it */
1160 unsigned int i = def(esc_args[0], 0) + 1;
1163 erase_lots(FALSE, !!(i & 2), !!(i & 1));
1166 seen_disp_event = TRUE;
1168 case 'K': /* erase line or parts of it */
1170 unsigned int i = def(esc_args[0], 0) + 1;
1173 erase_lots(TRUE, !!(i & 2), !!(i & 1));
1175 seen_disp_event = TRUE;
1177 case 'L': /* insert lines */
1178 compatibility(VT102);
1179 if (curs_y <= marg_b)
1180 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
1181 seen_disp_event = TRUE;
1183 case 'M': /* delete lines */
1184 compatibility(VT102);
1185 if (curs_y <= marg_b)
1186 scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
1187 seen_disp_event = TRUE;
1189 case '@': /* insert chars */
1190 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1191 compatibility(VT102);
1192 insch (def(esc_args[0], 1));
1193 seen_disp_event = TRUE;
1195 case 'P': /* delete chars */
1196 compatibility(VT102);
1197 insch (-def(esc_args[0], 1));
1198 seen_disp_event = TRUE;
1200 case 'c': /* terminal type query */
1201 compatibility(VT100);
1202 /* This is the response for a VT102 */
1203 ldisc->send (id_string, strlen(id_string));
1205 case 'n': /* cursor position query */
1206 if (esc_args[0] == 6) {
1208 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
1209 ldisc->send (buf, strlen(buf));
1211 else if (esc_args[0] == 5) {
1212 ldisc->send ("\033[0n", 4);
1215 case 'h': /* toggle modes to high */
1217 compatibility(VT100);
1220 for (i=0; i<esc_nargs; i++)
1221 toggle_mode (esc_args[i], esc_query, TRUE);
1224 case 'l': /* toggle modes to low */
1226 compatibility(VT100);
1229 for (i=0; i<esc_nargs; i++)
1230 toggle_mode (esc_args[i], esc_query, FALSE);
1233 case 'g': /* clear tabs */
1234 compatibility(VT100);
1235 if (esc_nargs == 1) {
1236 if (esc_args[0] == 0) {
1237 tabs[curs_x] = FALSE;
1238 } else if (esc_args[0] == 3) {
1240 for (i = 0; i < cols; i++)
1245 case 'r': /* set scroll margins */
1246 compatibility(VT100);
1247 if (esc_nargs <= 2) {
1249 top = def(esc_args[0], 1) - 1;
1250 bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
1251 def(esc_args[1], rows)) - 1;
1254 /* VTTEST Bug 9 - if region is less than 2 lines
1255 * don't change region.
1262 * I used to think the cursor should be
1263 * placed at the top of the newly marginned
1264 * area. Apparently not: VMS TPU falls over
1267 * Well actually it should for Origin mode - RDB
1269 curs_y = (dec_om ? marg_t : 0);
1271 seen_disp_event = TRUE;
1275 case 'm': /* set graphics rendition */
1278 * A VT100 without the AVO only had one attribute, either
1279 * underline or reverse video depending on the cursor type,
1280 * this was selected by CSI 7m.
1283 * This is DIM on the VT100-AVO and VT102
1285 * This is BLINK on the VT100-AVO and VT102+
1287 * This is INVIS on the VT100-AVO and VT102
1289 * This like 22 disables BOLD, DIM and INVIS
1291 * The ANSI colours appear on any terminal that has colour
1292 * (obviously) but the interaction between sgr0 and the
1293 * colours varies but is usually related to the background
1294 * colour erase item.
1295 * The interaction between colour attributes and the mono
1296 * ones is also very implementation dependent.
1298 * The 39 and 49 attributes are likely to be unimplemented.
1301 for (i=0; i<esc_nargs; i++) {
1302 switch (def(esc_args[i], 0)) {
1303 case 0: /* restore defaults */
1304 curr_attr = ATTR_DEFAULT; break;
1305 case 1: /* enable bold */
1306 compatibility(VT100AVO);
1307 curr_attr |= ATTR_BOLD; break;
1308 case 21: /* (enable double underline) */
1309 compatibility(OTHER);
1310 case 4: /* enable underline */
1311 compatibility(VT100AVO);
1312 curr_attr |= ATTR_UNDER; break;
1313 case 5: /* enable blink */
1314 compatibility(VT100AVO);
1315 curr_attr |= ATTR_BLINK; break;
1316 case 7: /* enable reverse video */
1317 curr_attr |= ATTR_REVERSE; break;
1318 case 22: /* disable bold */
1319 compatibility2(OTHER,VT220);
1320 curr_attr &= ~ATTR_BOLD; break;
1321 case 24: /* disable underline */
1322 compatibility2(OTHER,VT220);
1323 curr_attr &= ~ATTR_UNDER; break;
1324 case 25: /* disable blink */
1325 compatibility2(OTHER,VT220);
1326 curr_attr &= ~ATTR_BLINK; break;
1327 case 27: /* disable reverse video */
1328 compatibility2(OTHER,VT220);
1329 curr_attr &= ~ATTR_REVERSE; break;
1330 case 30: case 31: case 32: case 33:
1331 case 34: case 35: case 36: case 37:
1333 curr_attr &= ~ATTR_FGMASK;
1334 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
1336 case 39: /* default-foreground */
1337 curr_attr &= ~ATTR_FGMASK;
1338 curr_attr |= ATTR_DEFFG;
1340 case 40: case 41: case 42: case 43:
1341 case 44: case 45: case 46: case 47:
1343 curr_attr &= ~ATTR_BGMASK;
1344 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
1346 case 49: /* default-background */
1347 curr_attr &= ~ATTR_BGMASK;
1348 curr_attr |= ATTR_DEFBG;
1355 (curr_attr&(ATTR_FGMASK|ATTR_BGMASK|ATTR_BLINK))
1359 case 's': /* save cursor */
1362 case 'u': /* restore cursor */
1363 save_cursor (FALSE);
1364 seen_disp_event = TRUE;
1366 case 't': /* set page size - ie window height */
1368 * VT340/VT420 sequence DECSLPP, DEC only allows values
1369 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1370 * illegal values (eg first arg 1..9) for window changing
1373 compatibility(VT340TEXT);
1374 if (esc_nargs<=1 && (esc_args[0]<1 || esc_args[0]>=24)) {
1375 request_resize (cols, def(esc_args[0], 24), 0);
1379 case ANSI('|', '*'):
1380 /* VT420 sequence DECSNLS
1381 * Set number of lines on screen
1382 * VT420 uses VGA like hardware and can support any size in
1383 * reasonable range (24..49 AIUI) with no default specified.
1385 compatibility(VT420);
1386 if (esc_nargs==1 && esc_args[0]>0) {
1387 request_resize (cols, def(esc_args[0], cfg.height), 0);
1391 case ANSI('|', '$'):
1392 /* VT340/VT420 sequence DECSCPP
1393 * Set number of columns per page
1394 * Docs imply range is only 80 or 132, but I'll allow any.
1396 compatibility(VT340TEXT);
1398 request_resize (def(esc_args[0], cfg.width), rows, 0);
1402 case 'X': /* write N spaces w/o moving cursor */
1403 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1404 compatibility(ANSIMIN);
1406 int n = def(esc_args[0], 1);
1407 unsigned long *p = cpos;
1408 if (n > cols - curs_x)
1410 check_selection (cpos, cpos+n);
1413 seen_disp_event = TRUE;
1416 case 'x': /* report terminal characteristics */
1417 compatibility(VT100);
1420 int i = def(esc_args[0], 0);
1421 if (i == 0 || i == 1) {
1422 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1424 ldisc->send (buf, 20);
1429 compatibility(OTHER);
1430 use_bce = (esc_args[0]<=0);
1431 erase_char = ERASE_CHAR;
1433 erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
1436 compatibility(OTHER);
1437 blink_is_real = (esc_args[0]>=1);
1440 /* Allow the host to make this emulator a 'perfect' VT102.
1441 * This first appeared in the VT220, but we do need to get
1442 * back to PuTTY mode so I won't check it.
1444 * The arg in 40..42 are a PuTTY extension.
1445 * The 2nd arg, 8bit vs 7bit is not checked.
1447 * Setting VT102 mode should also change the Fkeys to
1448 * generate PF* codes as a real VT102 has no Fkeys.
1449 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1452 * Note ESC c will NOT change this!
1455 switch (esc_args[0]) {
1456 case 61: compatibility_level &= ~TM_VTXXX;
1457 compatibility_level |= TM_VT102; break;
1458 case 62: compatibility_level &= ~TM_VTXXX;
1459 compatibility_level |= TM_VT220; break;
1461 default: if( esc_args[0] > 60 && esc_args[0] < 70 )
1462 compatibility_level |= TM_VTXXX;
1465 case 40: compatibility_level &= TM_VTXXX; break;
1466 case 41: compatibility_level = TM_PUTTY; break;
1467 case 42: compatibility_level = TM_SCOANSI; break;
1470 compatibility_level = TM_PUTTY; break;
1474 /* Change the response to CSI c */
1475 if (esc_args[0] == 50) {
1478 strcpy(id_string, "\033[?");
1479 for (i=1; i<esc_nargs; i++) {
1480 if (i!=1) strcat(id_string, ";");
1481 sprintf(lbuf, "%d", esc_args[i]);
1482 strcat(id_string, lbuf);
1484 strcat(id_string, "c");
1488 /* Is this a good idea ?
1489 * Well we should do a soft reset at this point ...
1491 if (!has_compat(VT420) && has_compat(VT100)) {
1492 if (reset_132) request_resize (132, 24, 1);
1493 else request_resize ( 80, 24, 1);
1501 /* VT100 only here, checked above */
1504 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1507 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1510 default: /* specifically, 'B' */
1511 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1514 if( !has_compat(VT220) || c != '%' )
1515 termstate = TOPLEVEL;
1520 case 'P': /* Linux palette sequence */
1521 termstate = SEEN_OSC_P;
1524 case 'R': /* Linux palette reset */
1527 termstate = TOPLEVEL;
1529 case 'W': /* word-set */
1530 termstate = SEEN_OSC_W;
1533 case '0': case '1': case '2': case '3': case '4':
1534 case '5': case '6': case '7': case '8': case '9':
1535 esc_args[0] = 10 * esc_args[0] + c - '0';
1539 * Grotty hack to support xterm and DECterm title
1540 * sequences concurrently.
1542 if (esc_args[0] == 2) {
1546 /* else fall through */
1548 termstate = OSC_STRING;
1554 * This OSC stuff is EVIL. It takes just one character to get into
1555 * sysline mode and it's not initially obvious how to get out.
1556 * So I've added CR and LF as string aborts.
1557 * This shouldn't effect compatibility as I believe embedded
1558 * control characters are supposed to be interpreted (maybe?)
1559 * and they don't display anything useful anyway.
1563 if (c == '\n' || c == '\r') {
1564 termstate = TOPLEVEL;
1565 } else if (c == 0234 || c == '\007' ) {
1567 * These characters terminate the string; ST and BEL
1568 * terminate the sequence and trigger instant
1569 * processing of it, whereas ESC goes back to SEEN_ESC
1570 * mode unless it is followed by \, in which case it is
1571 * synonymous with ST in the first place.
1574 termstate = TOPLEVEL;
1575 } else if (c == '\033')
1576 termstate = OSC_MAYBE_ST;
1577 else if (osc_strlen < OSC_STR_MAX)
1578 osc_string[osc_strlen++] = c;
1582 int max = (osc_strlen == 0 ? 21 : 16);
1584 if (c >= '0' && c <= '9')
1586 else if (c >= 'A' && c <= 'A'+max-10)
1588 else if (c >= 'a' && c <= 'a'+max-10)
1591 termstate = TOPLEVEL;
1592 osc_string[osc_strlen++] = val;
1593 if (osc_strlen >= 7) {
1594 palette_set (osc_string[0],
1595 osc_string[1] * 16 + osc_string[2],
1596 osc_string[3] * 16 + osc_string[4],
1597 osc_string[5] * 16 + osc_string[6]);
1599 termstate = TOPLEVEL;
1605 case '0': case '1': case '2': case '3': case '4':
1606 case '5': case '6': case '7': case '8': case '9':
1607 esc_args[0] = 10 * esc_args[0] + c - '0';
1610 termstate = OSC_STRING;
1617 unsigned long nlattr;
1623 n = rows * (cols+1);
1625 *p++ = ATTR_DEFAULT | 'E';
1627 seen_disp_event = TRUE;
1628 check_selection (scrtop, scrtop + rows * (cols+1));
1631 case '3': nlattr = LATTR_TOP; if(0) {
1632 case '4': nlattr = LATTR_BOT; } if(0) {
1633 case '5': nlattr = LATTR_NORM; } if(0) {
1634 case '6': nlattr = LATTR_WIDE; }
1636 p = scrtop + curs_y * (cols+1) + cols;
1641 termstate = TOPLEVEL;
1644 termstate = TOPLEVEL;
1645 seen_disp_event = TRUE;
1648 move (curs_x, curs_y - 1, 1);
1651 move (curs_x, curs_y + 1, 1);
1654 move (curs_x + 1, curs_y, 1);
1657 move (curs_x - 1, curs_y, 1);
1660 cset_attr[cset=0] = ATTR_LINEDRW;
1663 cset_attr[cset=0] = ATTR_ASCII;
1670 scroll (0, rows-1, -1, TRUE);
1671 else if (curs_y > 0)
1677 erase_lots(FALSE, FALSE, TRUE);
1681 erase_lots(TRUE, FALSE, TRUE);
1684 /* XXX Print cursor line */
1687 /* XXX Start controller mode */
1690 /* XXX Stop controller mode */
1693 termstate = VT52_Y1;
1696 ldisc->send ("\033/Z", 3);
1699 app_keypad_keys = TRUE;
1702 app_keypad_keys = FALSE;
1705 /* XXX This should switch to VT100 mode not current or default
1706 * VT mode. But this will only have effect in a VT220+
1712 /* XXX Enter auto print mode */
1715 /* XXX Exit auto print mode */
1718 /* XXX Print screen */
1723 termstate = VT52_Y2;
1724 move(curs_x, c-' ', 0);
1727 termstate = TOPLEVEL;
1728 move(c-' ', curs_y, 0);
1731 if (selstate != NO_SELECTION)
1732 check_selection (cpos, cpos+1);
1738 if(!beep_count) beep_overload=0;
1740 else if(beep_count && beep_count<5 && cfg.beep)
1745 * Compare two lines to determine whether they are sufficiently
1746 * alike to scroll-optimise one to the other. Return the degree of
1749 static int linecmp (unsigned long *a, unsigned long *b) {
1752 for (i=n=0; i < cols; i++)
1753 n += (*a++ == *b++);
1758 * Given a context, update the window. Out of paranoia, we don't
1759 * allow WM_PAINT responses to do scrolling optimisations.
1761 static void do_paint (Context ctx, int may_optimise){
1762 int i, j, start, our_curs_y;
1763 unsigned long attr, rv, cursor;
1768 if (blinker || !cfg.blink_cur)
1769 cursor = ATTR_ACTCURS;
1774 cursor = ATTR_PASCURS;
1777 rv = (rvideo ? ATTR_REVERSE : 0);
1778 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1780 for (i=0; i<rows; i++) {
1781 int idx = i*(cols+1);
1782 int lattr = (disptop[idx+cols] & LATTR_MODE);
1783 for (j=0; j<=cols; j++,idx++) {
1784 unsigned long *d = disptop+idx;
1785 wanttext[idx] = lattr | ((*d ^ rv
1786 ^ (selstart <= d && d < selend ?
1787 ATTR_REVERSE : 0)) |
1788 (i==our_curs_y && j==curs_x ? cursor : 0));
1790 if (blink_is_real) {
1791 if (has_focus && tblinker && (wanttext[idx]&ATTR_BLINK) )
1793 wanttext[idx] &= ATTR_MASK;
1794 wanttext[idx] += ' ';
1796 wanttext[idx] &= ~ATTR_BLINK;
1802 * We would perform scrolling optimisations in here, if they
1803 * didn't have a nasty tendency to cause the whole sodding
1804 * program to hang for a second at speed-critical moments.
1805 * We'll leave it well alone...
1808 for (i=0; i<rows; i++) {
1809 int idx = i*(cols+1);
1810 int lattr = (wanttext[idx+cols] & LATTR_MODE);
1812 for (j=0; j<=cols; j++,idx++) {
1813 unsigned long t = wanttext[idx];
1814 int needs_update = (j < cols && t != disptext[idx]);
1815 int keep_going = (start != -1 && needs_update &&
1816 (t & ATTR_MASK) == attr &&
1817 j-start < sizeof(ch));
1818 if (start != -1 && !keep_going) {
1819 do_text (ctx, start, i, ch, j-start, attr, lattr);
1825 attr = t & ATTR_MASK;
1827 ch[j-start] = (char) (t & CHAR_MASK);
1835 * Flick the switch that says if blinking things should be shown or hidden.
1838 void term_blink(int flg) {
1839 static long last_blink = 0;
1840 static long last_tblink = 0;
1841 long now, blink_diff;
1843 now = GetTickCount();
1844 blink_diff = now-last_tblink;
1846 /* Make sure the text blinks no more than 2Hz */
1847 if (blink_diff<0 || blink_diff>450)
1850 tblinker = !tblinker;
1859 blink_diff = now-last_blink;
1861 /* Make sure the cursor blinks no more than 2Hz */
1862 if (blink_diff>=0 && blink_diff<450)
1870 * Invalidate the whole screen so it will be repainted in full.
1872 void term_invalidate(void) {
1875 for (i=0; i<rows*(cols+1); i++)
1876 disptext[i] = ATTR_INVALID;
1880 * Paint the window in response to a WM_PAINT message.
1882 void term_paint (Context ctx, int l, int t, int r, int b) {
1883 int i, j, left, top, right, bottom;
1885 left = l / font_width;
1886 right = (r - 1) / font_width;
1887 top = t / font_height;
1888 bottom = (b - 1) / font_height;
1889 for (i = top; i <= bottom && i < rows ; i++)
1891 if ( (disptext[i*(cols+1)+cols]&LATTR_MODE) == LATTR_NORM)
1892 for (j = left; j <= right && j < cols ; j++)
1893 disptext[i*(cols+1)+j] = ATTR_INVALID;
1895 for (j = left/2; j <= right/2+1 && j < cols ; j++)
1896 disptext[i*(cols+1)+j] = ATTR_INVALID;
1899 /* This should happen soon enough, also for some reason it sometimes
1900 * fails to actually do anything when re-sizing ... painting the wrong
1902 do_paint (ctx, FALSE);
1907 * Attempt to scroll the scrollback. The second parameter gives the
1908 * position we want to scroll to; the first is +1 to denote that
1909 * this position is relative to the beginning of the scrollback, -1
1910 * to denote it is relative to the end, and 0 to denote that it is
1911 * relative to the current position.
1913 void term_scroll (int rel, int where) {
1914 int n = where * (cols+1);
1916 disptop = (rel < 0 ? scrtop :
1917 rel > 0 ? sbtop : disptop) + n;
1918 if (disptop < sbtop)
1920 if (disptop > scrtop)
1927 * Spread the selection outwards according to the selection mode.
1929 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1930 unsigned long *linestart, *lineend;
1934 x = (p - text) % (cols+1);
1936 lineend = linestart + cols;
1941 * In this mode, every character is a separate unit, except
1942 * for runs of spaces at the end of a non-wrapping line.
1944 if (!(linestart[cols] & ATTR_WRAPPED)) {
1945 unsigned long *q = lineend;
1946 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1951 p = (dir == -1 ? q : lineend - 1);
1956 * In this mode, the units are maximal runs of characters
1957 * whose `wordness' has the same value.
1959 wvalue = wordness[*p & CHAR_MASK];
1961 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1964 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1970 * In this mode, every line is a unit.
1972 p = (dir == -1 ? linestart : lineend - 1);
1978 static void sel_spread (void) {
1979 selstart = sel_spread_half (selstart, -1);
1980 selend = sel_spread_half (selend - 1, +1) + 1;
1983 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
1984 unsigned long *selpoint;
1987 if (y>=rows) y = rows-1;
1995 if (x>=cols) x = cols-1;
1997 selpoint = disptop + y * (cols+1);
1998 if ((selpoint[cols]&LATTR_MODE) != LATTR_NORM)
2003 if (b == MB_SELECT && a == MA_CLICK) {
2005 selstate = ABOUT_TO;
2006 selanchor = selpoint;
2008 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
2010 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
2011 selstate = DRAGGING;
2012 selstart = selanchor = selpoint;
2013 selend = selstart + 1;
2015 } else if ((b == MB_SELECT && a == MA_DRAG) ||
2016 (b == MB_EXTEND && a != MA_RELEASE)) {
2017 if (selstate == ABOUT_TO && selanchor == selpoint)
2019 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
2020 if (selpoint-selstart < (selend-selstart)/2)
2021 selanchor = selend - 1;
2023 selanchor = selstart;
2024 selstate = DRAGGING;
2026 if (selstate != ABOUT_TO && selstate != DRAGGING)
2027 selanchor = selpoint;
2028 selstate = DRAGGING;
2029 if (selpoint < selanchor) {
2030 selstart = selpoint;
2031 selend = selanchor + 1;
2033 selstart = selanchor;
2034 selend = selpoint + 1;
2037 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
2038 if (selstate == DRAGGING) {
2040 * We've completed a selection. We now transfer the
2041 * data to the clipboard.
2043 unsigned char *p = selspace;
2044 unsigned long *q = selstart;
2046 while (q < selend) {
2048 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
2049 unsigned long *nlpos = lineend;
2051 if (!(*nlpos & ATTR_WRAPPED)) {
2052 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
2057 while (q < nlpos && q < selend)
2060 /* VT Specials -> ISO8859-1 */
2061 static const char poorman2[] =
2062 "* # HTFFCRLF\xB0 \xB1 NLVT+ + + + + - - - - - + + + + | <=>=PI!=\xA3 \xB7 ";
2065 int ch = (*q & CHAR_MASK);
2068 if ((*q & ATTR_LINEDRW) && ch >= 0x60 && ch < 0x7F) {
2070 *p++ = poorman2[2*(ch-0x60)];
2071 if ( (x = poorman2[2*(ch-0x60)+1]) != ' ')
2076 if ((*q & ATTR_GBCHR) && ch == '#')
2077 *p++ = (unsigned char) 0xA3;
2080 *p++ = (unsigned char) ch;
2085 for (i=0; i<sizeof(sel_nl); i++)
2088 q = lineend + 1; /* start of next line */
2090 write_clip (selspace, p - selspace, FALSE);
2091 selstate = SELECTED;
2093 selstate = NO_SELECTION;
2094 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
2098 get_clip((void **) &data, &len);
2102 if (paste_buffer) sfree(paste_buffer);
2103 paste_pos = paste_hold = paste_len = 0;
2104 paste_buffer = smalloc(len);
2107 while (p < data+len) {
2108 while (p < data+len &&
2109 !(p <= data+len-sizeof(sel_nl) &&
2110 !memcmp(p, sel_nl, sizeof(sel_nl))))
2118 c=xlat_kbd2tty(q[i]);
2119 paste_buffer[paste_len++] = c;
2123 if (p <= data+len-sizeof(sel_nl) &&
2124 !memcmp(p, sel_nl, sizeof(sel_nl))) {
2125 paste_buffer[paste_len++] = '\r';
2126 p += sizeof(sel_nl);
2131 /* Assume a small paste will be OK in one go. */
2132 if (paste_len<256) {
2133 ldisc->send (paste_buffer, paste_len);
2134 if (paste_buffer) sfree(paste_buffer);
2136 paste_pos = paste_hold = paste_len = 0;
2139 get_clip(NULL, NULL);
2145 void term_nopaste() {
2146 if(paste_len == 0) return;
2147 sfree(paste_buffer);
2153 static long last_paste = 0;
2154 long now, paste_diff;
2156 if(paste_len == 0) return;
2158 /* Don't wait forever to paste */
2160 now = GetTickCount();
2161 paste_diff = now-last_paste;
2162 if (paste_diff>=0 && paste_diff<450)
2167 while(paste_pos<paste_len)
2169 char c = paste_buffer[paste_pos++];
2170 ldisc->send (&c, 1);
2177 sfree(paste_buffer);
2182 static void deselect (void) {
2183 selstate = NO_SELECTION;
2184 selstart = selend = scrtop;
2187 void term_deselect (void) {
2193 * from_backend(), to get data from the backend for the terminal.
2195 void from_backend(int is_stderr, char *data, int len) {
2197 if (inbuf_head >= INBUF_SIZE)
2199 inbuf[inbuf_head++] = *data++;