8 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
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_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
18 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
19 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
21 #define TM_ANSIMIN (CL_ANSIMIN)
22 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
23 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
24 #define TM_VT102 (TM_VT100AVO|CL_VT102)
25 #define TM_VT220 (TM_VT102|CL_VT220)
26 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
27 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
29 #define TM_PUTTY (0xFFFF)
31 #define compatibility(x) \
32 if ( ((CL_##x)&compatibility_level) == 0 ) { \
36 #define compatibility2(x,y) \
37 if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
42 #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
44 static int compatibility_level = TM_PUTTY;
47 static unsigned long *text; /* buffer of text on terminal screen */
48 static unsigned long *scrtop; /* top of working screen */
49 static unsigned long *disptop; /* top of displayed screen */
50 static unsigned long *sbtop; /* top of scrollback */
51 static unsigned long *sbbot; /* furthest extent of scrollback */
52 static unsigned long *cpos; /* cursor position (convenience) */
53 static unsigned long *disptext; /* buffer of text on real screen */
54 static unsigned long *wanttext; /* buffer of text we want on screen */
55 static unsigned long *alttext; /* buffer of text on alt. screen */
57 static unsigned char *selspace; /* buffer for building selections in */
59 #define TSIZE (sizeof(*text))
60 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
62 static unsigned long curr_attr, save_attr;
63 static unsigned long erase_char = ERASE_CHAR;
65 static int curs_x, curs_y; /* cursor */
66 static int save_x, save_y; /* saved cursor position */
67 static int marg_t, marg_b; /* scroll margins */
68 static int dec_om; /* DEC origin mode flag */
69 static int wrap, wrapnext; /* wrap flags */
70 static int insert; /* insert-mode flag */
71 static int cset; /* 0 or 1: which char set */
72 static int save_cset, save_csattr; /* saved with cursor position */
73 static int rvideo; /* global reverse video flag */
74 static int cursor_on; /* cursor enabled flag */
75 static int reset_132; /* Flag ESC c resets to 80 cols */
76 static int use_bce; /* Use Background coloured erase */
77 static int blinker; /* When blinking is the cursor on ? */
78 static int tblinker; /* When the blinking text is on */
79 static int blink_is_real; /* Actually blink blinking text */
81 static unsigned long cset_attr[2];
84 * Saved settings on the alternate screen.
86 static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
87 static int alt_t, alt_b;
90 #define ARGS_MAX 32 /* max # of esc sequence arguments */
91 #define ARG_DEFAULT 0 /* if an arg isn't specified */
92 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
93 static int esc_args[ARGS_MAX];
96 #define ANSI(x,y) ((x)+((y)<<8))
97 #define ANSI_QUE(x) ANSI(x,TRUE)
99 #define OSC_STR_MAX 2048
100 static int osc_strlen;
101 static char osc_string[OSC_STR_MAX+1];
104 static char id_string[1024] = "\033[?6c";
106 static unsigned char *tabs;
120 OSC_STRING, OSC_MAYBE_ST,
128 NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
131 SM_CHAR, SM_WORD, SM_LINE
133 static unsigned long *selstart, *selend, *selanchor;
135 static short wordness[256] = {
136 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 */
137 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 */
138 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 */
139 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 */
140 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 */
141 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 */
142 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 */
143 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 */
146 static unsigned char sel_nl[] = SEL_NL;
147 static char * paste_buffer = 0;
148 static int paste_len, paste_pos, paste_hold;
151 * Internal prototypes.
153 static void do_paint (Context, int);
154 static void erase_lots (int, int, int);
155 static void swap_screen (int);
156 static void update_sbar (void);
157 static void deselect (void);
160 * Set up power-on settings for the terminal.
162 static void power_on(void) {
163 curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
166 alt_b = marg_b = rows - 1;
171 for (i = 0; i < cols; i++)
172 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
174 alt_om = dec_om = cfg.dec_om;
175 alt_wnext = wrapnext = alt_ins = insert = FALSE;
176 alt_wrap = wrap = cfg.wrap_mode;
178 cset_attr[0] = cset_attr[1] = ATTR_ASCII;
181 save_attr = curr_attr = ATTR_DEFAULT;
182 app_cursor_keys = cfg.app_cursor;
183 app_keypad_keys = cfg.app_keypad;
185 blink_is_real = cfg.blinktext;
186 erase_char = ERASE_CHAR;
190 for (i = 0; i < 256; i++)
191 wordness[i] = cfg.wordness[i];
195 erase_lots (FALSE, TRUE, TRUE);
197 erase_lots (FALSE, TRUE, TRUE);
202 * Force a screen update.
204 void term_update(void) {
208 if ( (seen_key_event && (cfg.scroll_on_key)) ||
209 (seen_disp_event && (!cfg.scroll_on_key)) ) {
211 seen_disp_event = seen_key_event = 0;
213 do_paint (ctx, TRUE);
219 * Same as power_on(), but an external function.
221 void term_pwron(void) {
230 * Clear the scrollback.
232 void term_clrsb(void) {
233 disptop = sbtop = scrtop;
238 * Initialise the terminal.
240 void term_init(void) {
241 text = sbtop = sbbot = scrtop = disptop = cpos = NULL;
242 disptext = wanttext = NULL;
251 * Set up the terminal for a given size.
253 void term_size(int newrows, int newcols, int newsavelines) {
254 unsigned long *newtext, *newdisp, *newwant, *newalt;
255 int i, j, crows, ccols;
257 int save_alt_which = alt_which;
259 if (newrows == rows && newcols == cols && newsavelines == savelines)
260 return; /* nothing to do */
266 alt_b = marg_b = newrows - 1;
268 newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
269 sbbot = newtext + newsavelines*(newcols+1);
270 for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
271 newtext[i] = erase_char;
273 crows = rows + (scrtop - sbtop) / (cols+1);
274 if (crows > newrows+newsavelines)
275 crows = newrows+newsavelines;
279 disptop = newtext + (crows-newrows)*(newcols+1);
280 ccols = (cols < newcols ? cols : newcols);
281 for (i=0; i<crows; i++) {
282 int oldidx = (rows - crows + i) * (cols+1);
283 int newidx = (newrows - crows + i) * (newcols+1);
285 for (j=0; j<ccols; j++)
286 disptop[newidx+j] = scrtop[oldidx+j];
287 disptop[newidx+newcols] =
288 (cols == newcols ? scrtop[oldidx+cols]
289 : (scrtop[oldidx+cols]&LATTR_MODE));
293 sbtop = disptop = newtext;
299 newdisp = smalloc (newrows*(newcols+1)*TSIZE);
300 for (i=0; i<newrows*(newcols+1); i++)
301 newdisp[i] = ATTR_INVALID;
305 newwant = smalloc (newrows*(newcols+1)*TSIZE);
306 for (i=0; i<newrows*(newcols+1); i++)
307 newwant[i] = ATTR_INVALID;
311 newalt = smalloc (newrows*(newcols+1)*TSIZE);
312 for (i=0; i<newrows*(newcols+1); i++)
313 newalt[i] = erase_char;
318 selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
320 tabs = srealloc (tabs, newcols*sizeof(*tabs));
323 for (i = (cols > 0 ? cols : 0); i < newcols; i++)
324 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
328 curs_y += newrows - rows;
331 if (curs_y >= newrows)
333 if (curs_x >= newcols)
336 wrapnext = alt_wnext = FALSE;
340 savelines = newsavelines;
343 swap_screen(save_alt_which);
352 static void swap_screen (int which) {
356 if (which == alt_which)
361 for (t=0; t<rows*(cols+1); t++) {
362 tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
365 t = curs_x; curs_x = alt_x; alt_x = t;
366 t = curs_y; curs_y = alt_y; alt_y = t;
367 t = marg_t; marg_t = alt_t; alt_t = t;
368 t = marg_b; marg_b = alt_b; alt_b = t;
369 t = dec_om; dec_om = alt_om; alt_om = t;
370 t = wrap; wrap = alt_wrap; alt_wrap = t;
371 t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
372 t = insert; insert = alt_ins; alt_ins = t;
373 t = cset; cset = alt_cset; alt_cset = t;
379 * Update the scroll bar.
381 static void update_sbar(void) {
384 min = (sbtop - text) / (cols+1);
385 set_sbar ((scrtop - text) / (cols+1) + rows - min,
386 (disptop - text) / (cols+1) - min,
391 * Check whether the region bounded by the two pointers intersects
392 * the scroll region, and de-select the on-screen selection if so.
394 static void check_selection (unsigned long *from, unsigned long *to) {
395 if (from < selend && selstart < to)
400 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
401 * for backward.) `sb' is TRUE if the scrolling is permitted to
402 * affect the scrollback buffer.
404 static void scroll (int topline, int botline, int lines, int sb) {
405 unsigned long *scroll_top;
406 unsigned long *newscr;
407 int scroll_size, size, i;
408 int scrtop_is_disptop = (scrtop==disptop);
409 static int recursive = 0;
411 /* Only scroll more than the window if we're doing a 10% scroll */
412 if (!recursive && lines > botline - topline + 1)
413 lines = botline - topline + 1;
415 scroll_top = scrtop + topline*(cols+1);
416 size = (lines < 0 ? -lines : lines) * (cols+1);
417 scroll_size = (botline - topline + 1) * (cols+1) - size;
419 if (lines > 0 && topline == 0 && alt_which == 0 && sb) {
421 * Since we're going to scroll the top line and we're on the
422 * scrolling screen let's also effect the scrollback buffer.
424 * This is normally done by moving the position the screen
425 * painter reads from to reduce the amount of memory copying
428 if (scroll_size >= 0 && !recursive) {
429 newscr = scrtop + lines * (cols+1);
431 if (newscr > sbbot && botline == rows-1) {
432 /* We've hit the bottom of memory, so we have to do a
433 * physical scroll. But instead of just 1 line do it
434 * by 10% of the available memory.
436 * If the scroll region isn't the whole screen then we can't
437 * do this as it stands. We would need to recover the bottom
438 * of the screen from the scroll buffer after being sure that
439 * it doesn't get deleted.
442 i = (rows+savelines)/10;
444 /* Make it simple and ensure safe recursion */
445 if ( i<savelines-1) {
447 scroll(topline, botline, i, sb);
450 newscr = scrtop - i * (cols+1);
451 if (scrtop_is_disptop) disptop = newscr;
455 newscr = scrtop + lines * (cols+1);
458 if (newscr <= sbbot) {
459 if (scrtop_is_disptop) disptop = newscr;
462 if (botline == rows-1 )
463 for (i = 0; i < size; i++)
464 scrtop[i+scroll_size] = erase_char;
469 if (botline != rows-1) {
470 /* This fastscroll only works for full window scrolls.
471 * If the caller wanted a partial one we have to reverse
472 * scroll the bottom of the screen.
474 scroll(botline-lines+1, rows-1, -lines, 0);
479 /* If we can't scroll by memory remapping do it physically.
480 * But rather than expensivly doing the scroll buffer just
481 * scroll the screen. All it means is that sometimes we choose
482 * to not add lines from a scroll region to the scroll buffer.
485 if (savelines <= 400) {
486 sbtop -= lines * (cols+1);
489 scroll_size += scroll_top - sbtop;
495 /* Ho hum, expensive scroll required. */
497 sbtop -= lines * (cols+1);
500 scroll_size += scroll_top - sbtop;
507 if (scroll_size < 0) {
514 memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
515 for (i = 0; i < size; i++)
516 scroll_top[i+scroll_size] = erase_char;
517 if (selstart > scroll_top &&
518 selstart < scroll_top + size + scroll_size) {
520 if (selstart < scroll_top)
521 selstart = scroll_top;
523 if (selend > scroll_top &&
524 selend < scroll_top + size + scroll_size) {
526 if (selend < scroll_top)
529 if (scrtop_is_disptop)
532 if (disptop > scroll_top &&
533 disptop < scroll_top + size + scroll_size) {
535 if (disptop < scroll_top)
536 disptop = scroll_top;
540 memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
541 for (i = 0; i < size; i++)
542 scroll_top[i] = erase_char;
543 if (selstart > scroll_top &&
544 selstart < scroll_top + size + scroll_size) {
546 if (selstart > scroll_top + size + scroll_size)
547 selstart = scroll_top + size + scroll_size;
549 if (selend > scroll_top &&
550 selend < scroll_top + size + scroll_size) {
552 if (selend > scroll_top + size + scroll_size)
553 selend = scroll_top + size + scroll_size;
555 if (scrtop_is_disptop)
557 else if (disptop > scroll_top &&
558 disptop < scroll_top + size + scroll_size) {
560 if (disptop > scroll_top + size + scroll_size)
561 disptop = scroll_top + size + scroll_size;
567 * Move the cursor to a given position, clipping at boundaries. We
568 * may or may not want to clip at the scroll margin: marg_clip is 0
569 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
570 * even _being_ outside the margins.
572 static void move (int x, int y, int marg_clip) {
578 if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
580 if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
594 * Save or restore the cursor and SGR mode.
596 static void save_cursor(int save) {
600 save_attr = curr_attr;
602 save_csattr = cset_attr[cset];
606 /* Make sure the window hasn't shrunk since the save */
607 if (curs_x >= cols) curs_x = cols-1;
608 if (curs_y >= rows) curs_y = rows-1;
610 curr_attr = save_attr;
612 cset_attr[cset] = save_csattr;
614 if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
619 * Erase a large portion of the screen: the whole screen, or the
620 * whole line, or parts thereof.
622 static void erase_lots (int line_only, int from_begin, int to_end) {
623 unsigned long *startpos, *endpos;
626 startpos = cpos - curs_x;
627 endpos = startpos + cols;
628 /* I've removed the +1 so that the Wide screen stuff is not
629 * removed when it shouldn't be.
633 endpos = startpos + rows * (cols+1);
639 check_selection (startpos, endpos);
641 /* Clear screen also forces a full window redraw, just in case. */
642 if (startpos == scrtop && endpos == scrtop + rows * (cols+1))
645 while (startpos < endpos)
646 *startpos++ = erase_char;
650 * Insert or delete characters within the current line. n is +ve if
651 * insertion is desired, and -ve for deletion.
653 static void insch (int n) {
654 int dir = (n < 0 ? -1 : +1);
657 n = (n < 0 ? -n : n);
658 if (n > cols - curs_x)
660 m = cols - curs_x - n;
661 check_selection (cpos, cpos+n);
663 memmove (cpos, cpos+n, m*TSIZE);
665 cpos[m++] = erase_char;
667 memmove (cpos+n, cpos, m*TSIZE);
669 cpos[n] = erase_char;
674 * Toggle terminal mode `mode' to state `state'. (`query' indicates
675 * whether the mode is a DEC private one or a normal one.)
677 static void toggle_mode (int mode, int query, int state) {
678 if (query) switch (mode) {
679 case 1: /* application cursor keys */
680 app_cursor_keys = state;
682 case 2: /* VT52 mode */
685 case 3: /* 80/132 columns */
687 request_resize (state ? 132 : 80, rows, 1);
690 case 5: /* reverse video */
692 seen_disp_event = TRUE;
693 if (state) term_update();
695 case 6: /* DEC origin mode */
698 case 7: /* auto wrap */
701 case 8: /* auto key repeat */
704 case 25: /* enable/disable cursor */
705 compatibility(VT220);
707 seen_disp_event = TRUE;
709 case 47: /* alternate screen */
710 compatibility(OTHER);
715 } else switch (mode) {
716 case 4: /* set insert mode */
717 compatibility(VT102);
720 case 12: /* set echo mode */
722 * This may be very good in smcup and rmcup (or smkx & rmkx) if you
723 * have a long RTT and the telnet client/daemon doesn't understand
726 * DONT send TS_RECHO/TS_LECHO; the telnet daemon tries to fix the
727 * tty and _really_ confuses some programs.
729 compatibility(VT220);
730 ldisc = (state? &ldisc_simple : &ldisc_term);
732 case 20: /* Return sends ... */
733 cr_lf_return = state;
739 * Process an OSC sequence: set window title or icon name.
741 static void do_osc(void) {
744 wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
746 osc_string[osc_strlen] = '\0';
747 switch (esc_args[0]) {
750 set_icon (osc_string);
751 if (esc_args[0] == 1)
753 /* fall through: parameter 0 means set both */
756 set_title (osc_string);
763 * Remove everything currently in `inbuf' and stick it up on the
764 * in-memory display. There's a big state machine in here to
765 * process escape sequences...
767 void term_out(void) {
770 static int beep_overload = 0;
773 for(inbuf_reap = 0; inbuf_reap < inbuf_head; inbuf_reap++)
775 c = inbuf[inbuf_reap];
778 * Optionally log the session traffic to a file. Useful for
779 * debugging and possibly also useful for actual logging.
782 static FILE *fp = NULL;
783 if (!fp) fp = fopen(logfile, "wb");
784 if (fp) fputc (c, fp);
786 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
787 * be able to display 8-bit characters, but I'll let that go 'cause
790 if( ( (c&0x60) == 0 || c == '\177') &&
791 termstate < DO_CTRLS &&
792 ( (c&0x80) == 0 || has_compat(VT220))) {
794 case '\005': /* terminal type query */
795 /* Strictly speaking this is VT100 but a VT100 defaults to
796 * no response. Other terminals respond at their option.
798 * Don't put a CR in the default string as this tends to
799 * upset some weird software.
801 * An xterm returns "xterm" (5 characters)
803 compatibility(OTHER);
804 ldisc->send ("PuTTY", 5);
808 if(beep_count>6) beep_overload=1;
812 if (curs_x == 0 && curs_y == 0)
814 else if (curs_x == 0 && curs_y > 0)
815 curs_x = cols-1, curs_y--;
821 seen_disp_event = TRUE;
824 compatibility(VT100);
828 compatibility(VT100);
833 termstate = VT52_ESC;
835 compatibility(ANSIMIN);
836 termstate = SEEN_ESC;
840 compatibility(VT220);
841 termstate = SEEN_CSI;
843 esc_args[0] = ARG_DEFAULT;
847 compatibility(VT220);
848 termstate = SEEN_OSC;
855 seen_disp_event = TRUE;
860 compatibility(VT100);
862 if (curs_y == marg_b)
863 scroll (marg_t, marg_b, 1, TRUE);
864 else if (curs_y < rows-1)
875 unsigned long *old_cpos = cpos;
876 unsigned long *p = scrtop + curs_y * (cols+1) + cols;
880 } while (curs_x < cols-1 && !tabs[curs_x]);
882 if ((*p & LATTR_MODE) != LATTR_NORM)
884 if (curs_x >= cols/2)
894 check_selection (old_cpos, cpos);
896 seen_disp_event = TRUE;
900 else switch (termstate) {
902 /* Only graphic characters get this far, ctrls are stripped above */
904 cpos[1] |= ATTR_WRAPPED;
905 if (curs_y == marg_b)
906 scroll (marg_t, marg_b, 1, TRUE);
907 else if (curs_y < rows-1)
915 if (selstate != NO_SELECTION)
916 check_selection (cpos, cpos+1);
917 switch (cset_attr[cset]) {
918 /* Linedraw characters are different from 'ESC ( B' only
919 * for a small range, for ones outside that range make sure
920 * we use the same font as well as the same encoding.
923 if (c<0x60 || c>0x7F)
924 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
927 *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW;
930 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
931 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
935 if (curs_x == cols) {
944 termstate = TOPLEVEL;
948 * This state is virtually identical to SEEN_ESC, with the
949 * exception that we have an OSC sequence in the pipeline,
950 * and _if_ we see a backslash, we process it.
954 termstate = TOPLEVEL;
957 /* else fall through */
959 termstate = TOPLEVEL;
961 case ' ': /* some weird sequence? */
962 compatibility(VT220);
963 termstate = IGNORE_NEXT;
965 case '[': /* enter CSI mode */
966 termstate = SEEN_CSI;
968 esc_args[0] = ARG_DEFAULT;
971 case ']': /* xterm escape sequences */
972 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
973 compatibility(OTHER);
974 termstate = SEEN_OSC;
977 case '(': /* should set GL */
978 compatibility(VT100);
981 case ')': /* should set GR */
982 compatibility(VT100);
985 case '7': /* save cursor */
986 compatibility(VT100);
989 case '8': /* restore cursor */
990 compatibility(VT100);
992 seen_disp_event = TRUE;
995 compatibility(VT100);
996 app_keypad_keys = TRUE;
999 compatibility(VT100);
1000 app_keypad_keys = FALSE;
1002 case 'D': /* exactly equivalent to LF */
1003 compatibility(VT100);
1004 if (curs_y == marg_b)
1005 scroll (marg_t, marg_b, 1, TRUE);
1006 else if (curs_y < rows-1)
1010 seen_disp_event = TRUE;
1012 case 'E': /* exactly equivalent to CR-LF */
1013 compatibility(VT100);
1015 if (curs_y == marg_b)
1016 scroll (marg_t, marg_b, 1, TRUE);
1017 else if (curs_y < rows-1)
1021 seen_disp_event = TRUE;
1023 case 'M': /* reverse index - backwards LF */
1024 compatibility(VT100);
1025 if (curs_y == marg_t)
1026 scroll (marg_t, marg_b, -1, TRUE);
1027 else if (curs_y > 0)
1031 seen_disp_event = TRUE;
1033 case 'Z': /* terminal type query */
1034 compatibility(VT100);
1035 ldisc->send (id_string, strlen(id_string));
1037 case 'c': /* restore power-on settings */
1038 compatibility(VT100);
1041 request_resize (80, rows, 1);
1046 seen_disp_event = TRUE;
1048 case '#': /* ESC # 8 fills screen with Es :-) */
1049 compatibility(VT100);
1050 termstate = SEEN_ESCHASH;
1052 case 'H': /* set a tab */
1053 compatibility(VT100);
1054 tabs[curs_x] = TRUE;
1059 termstate = TOPLEVEL; /* default */
1062 if (esc_nargs <= ARGS_MAX) {
1063 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
1064 esc_args[esc_nargs-1] = 0;
1065 esc_args[esc_nargs-1] =
1066 10 * esc_args[esc_nargs-1] + c - '0';
1068 termstate = SEEN_CSI;
1072 if (++esc_nargs <= ARGS_MAX)
1073 esc_args[esc_nargs-1] = ARG_DEFAULT;
1074 termstate = SEEN_CSI;
1078 if( esc_query ) esc_query = -1;
1079 else if( c == '?' ) esc_query = TRUE;
1081 termstate = SEEN_CSI;
1083 else switch (ANSI(c,esc_query)) {
1084 case 'A': /* move up N lines */
1085 move (curs_x, curs_y - def(esc_args[0], 1), 1);
1086 seen_disp_event = TRUE;
1088 case 'e': /* move down N lines */
1089 compatibility(ANSI);
1091 move (curs_x, curs_y + def(esc_args[0], 1), 1);
1092 seen_disp_event = TRUE;
1094 case 'a': /* move right N cols */
1095 compatibility(ANSI);
1097 move (curs_x + def(esc_args[0], 1), curs_y, 1);
1098 seen_disp_event = TRUE;
1100 case 'D': /* move left N cols */
1101 move (curs_x - def(esc_args[0], 1), curs_y, 1);
1102 seen_disp_event = TRUE;
1104 case 'E': /* move down N lines and CR */
1105 compatibility(ANSI);
1106 move (0, curs_y + def(esc_args[0], 1), 1);
1107 seen_disp_event = TRUE;
1109 case 'F': /* move up N lines and CR */
1110 compatibility(ANSI);
1111 move (0, curs_y - def(esc_args[0], 1), 1);
1112 seen_disp_event = TRUE;
1114 case 'G': case '`': /* set horizontal posn */
1115 compatibility(ANSI);
1116 move (def(esc_args[0], 1) - 1, curs_y, 0);
1117 seen_disp_event = TRUE;
1119 case 'd': /* set vertical posn */
1120 compatibility(ANSI);
1121 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1123 seen_disp_event = TRUE;
1125 case 'H': case 'f': /* set horz and vert posns at once */
1127 esc_args[1] = ARG_DEFAULT;
1128 move (def(esc_args[1], 1) - 1,
1129 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1131 seen_disp_event = TRUE;
1133 case 'J': /* erase screen or parts of it */
1135 unsigned int i = def(esc_args[0], 0) + 1;
1138 erase_lots(FALSE, !!(i & 2), !!(i & 1));
1141 seen_disp_event = TRUE;
1143 case 'K': /* erase line or parts of it */
1145 unsigned int i = def(esc_args[0], 0) + 1;
1148 erase_lots(TRUE, !!(i & 2), !!(i & 1));
1150 seen_disp_event = TRUE;
1152 case 'L': /* insert lines */
1153 compatibility(VT102);
1154 if (curs_y <= marg_b)
1155 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
1156 seen_disp_event = TRUE;
1158 case 'M': /* delete lines */
1159 compatibility(VT102);
1160 if (curs_y <= marg_b)
1161 scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
1162 seen_disp_event = TRUE;
1164 case '@': /* insert chars */
1165 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1166 compatibility(VT102);
1167 insch (def(esc_args[0], 1));
1168 seen_disp_event = TRUE;
1170 case 'P': /* delete chars */
1171 compatibility(VT102);
1172 insch (-def(esc_args[0], 1));
1173 seen_disp_event = TRUE;
1175 case 'c': /* terminal type query */
1176 compatibility(VT100);
1177 /* This is the response for a VT102 */
1178 ldisc->send (id_string, strlen(id_string));
1180 case 'n': /* cursor position query */
1181 if (esc_args[0] == 6) {
1183 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
1184 ldisc->send (buf, strlen(buf));
1186 else if (esc_args[0] == 5) {
1187 ldisc->send ("\033[0n", 4);
1190 case 'h': /* toggle modes to high */
1192 compatibility(VT100);
1195 for (i=0; i<esc_nargs; i++)
1196 toggle_mode (esc_args[i], esc_query, TRUE);
1199 case 'l': /* toggle modes to low */
1201 compatibility(VT100);
1204 for (i=0; i<esc_nargs; i++)
1205 toggle_mode (esc_args[i], esc_query, FALSE);
1208 case 'g': /* clear tabs */
1209 compatibility(VT100);
1210 if (esc_nargs == 1) {
1211 if (esc_args[0] == 0) {
1212 tabs[curs_x] = FALSE;
1213 } else if (esc_args[0] == 3) {
1215 for (i = 0; i < cols; i++)
1220 case 'r': /* set scroll margins */
1221 compatibility(VT100);
1222 if (esc_nargs <= 2) {
1224 top = def(esc_args[0], 1) - 1;
1225 bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
1226 def(esc_args[1], rows)) - 1;
1229 /* VTTEST Bug 9 - if region is less than 2 lines
1230 * don't change region.
1237 * I used to think the cursor should be
1238 * placed at the top of the newly marginned
1239 * area. Apparently not: VMS TPU falls over
1242 * Well actually it should for Origin mode - RDB
1244 curs_y = (dec_om ? marg_t : 0);
1246 seen_disp_event = TRUE;
1250 case 'm': /* set graphics rendition */
1253 * A VT100 without the AVO only had one attribute, either
1254 * underline or reverse video depending on the cursor type,
1255 * this was selected by CSI 7m.
1258 * This is DIM on the VT100-AVO and VT102
1260 * This is BLINK on the VT100-AVO and VT102+
1262 * This is INVIS on the VT100-AVO and VT102
1264 * This like 22 disables BOLD, DIM and INVIS
1266 * The ANSI colours appear on any terminal that has colour
1267 * (obviously) but the interaction between sgr0 and the
1268 * colours varies but is usually related to the background
1269 * colour erase item.
1270 * The interaction between colour attributes and the mono
1271 * ones is also very implementation dependent.
1273 * The 39 and 49 attributes are likely to be unimplemented.
1276 for (i=0; i<esc_nargs; i++) {
1277 switch (def(esc_args[i], 0)) {
1278 case 0: /* restore defaults */
1279 curr_attr = ATTR_DEFAULT; break;
1280 case 1: /* enable bold */
1281 compatibility(VT100AVO);
1282 curr_attr |= ATTR_BOLD; break;
1283 case 21: /* (enable double underline) */
1284 compatibility(OTHER);
1285 case 4: /* enable underline */
1286 compatibility(VT100AVO);
1287 curr_attr |= ATTR_UNDER; break;
1288 case 5: /* enable blink */
1289 compatibility(VT100AVO);
1290 curr_attr |= ATTR_BLINK; break;
1291 case 7: /* enable reverse video */
1292 curr_attr |= ATTR_REVERSE; break;
1293 case 22: /* disable bold */
1294 compatibility(VT220);
1295 curr_attr &= ~ATTR_BOLD; break;
1296 case 24: /* disable underline */
1297 compatibility(VT220);
1298 curr_attr &= ~ATTR_UNDER; break;
1299 case 25: /* disable blink */
1300 compatibility(VT220);
1301 curr_attr &= ~ATTR_BLINK; break;
1302 case 27: /* disable reverse video */
1303 compatibility(VT220);
1304 curr_attr &= ~ATTR_REVERSE; break;
1305 case 30: case 31: case 32: case 33:
1306 case 34: case 35: case 36: case 37:
1308 curr_attr &= ~ATTR_FGMASK;
1309 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
1311 case 39: /* default-foreground */
1312 curr_attr &= ~ATTR_FGMASK;
1313 curr_attr |= ATTR_DEFFG;
1315 case 40: case 41: case 42: case 43:
1316 case 44: case 45: case 46: case 47:
1318 curr_attr &= ~ATTR_BGMASK;
1319 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
1321 case 49: /* default-background */
1322 curr_attr &= ~ATTR_BGMASK;
1323 curr_attr |= ATTR_DEFBG;
1330 (curr_attr&(ATTR_FGMASK|ATTR_BGMASK|ATTR_BLINK))
1334 case 's': /* save cursor */
1337 case 'u': /* restore cursor */
1338 save_cursor (FALSE);
1339 seen_disp_event = TRUE;
1341 case 't': /* set page size - ie window height */
1343 * VT340/VT420 sequence DECSLPP, DEC only allows values
1344 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1345 * illegal values (eg first arg 1..9) for window changing
1348 compatibility(VT340TEXT);
1349 if (esc_nargs<=1 && (esc_args[0]<1 || esc_args[0]>=24)) {
1350 request_resize (cols, def(esc_args[0], 24), 0);
1354 case ANSI('|', '*'):
1355 /* VT420 sequence DECSNLS
1356 * Set number of lines on screen
1357 * VT420 uses VGA like hardware and can support any size in
1358 * reasonable range (24..49 AIUI) with no default specified.
1360 compatibility(VT420);
1361 if (esc_nargs==1 && esc_args[0]>0) {
1362 request_resize (cols, def(esc_args[0], cfg.height), 0);
1366 case ANSI('|', '$'):
1367 /* VT340/VT420 sequence DECSCPP
1368 * Set number of columns per page
1369 * Docs imply range is only 80 or 132, but I'll allow any.
1371 compatibility(VT340TEXT);
1373 request_resize (def(esc_args[0], cfg.width), rows, 0);
1377 case 'X': /* write N spaces w/o moving cursor */
1378 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1379 compatibility(ANSIMIN);
1381 int n = def(esc_args[0], 1);
1382 unsigned long *p = cpos;
1383 if (n > cols - curs_x)
1385 check_selection (cpos, cpos+n);
1388 seen_disp_event = TRUE;
1391 case 'x': /* report terminal characteristics */
1392 compatibility(VT100);
1395 int i = def(esc_args[0], 0);
1396 if (i == 0 || i == 1) {
1397 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1399 ldisc->send (buf, 20);
1404 compatibility(OTHER);
1405 use_bce = (esc_args[0]<=0);
1406 erase_char = ERASE_CHAR;
1408 erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
1411 compatibility(OTHER);
1412 blink_is_real = (esc_args[0]>=1);
1415 /* Allow the host to make this emulator a 'perfect' VT102.
1416 * This first appeared in the VT220, but we do need to get
1417 * back to PuTTY mode so I won't check it.
1419 * The arg == 60 is a PuTTY extension.
1420 * The 2nd arg, 8bit vs 7bit is not obeyed.
1422 * Setting VT102 mode should also change the Fkeys to
1423 * generate PF* codes as a real VT102 has no Fkeys.
1424 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1427 * Note ESC c will NOT change this!
1430 if (esc_args[0] == 61) compatibility_level = TM_VT102;
1431 else if (esc_args[0] == 60) compatibility_level = TM_ANSIMIN;
1432 else compatibility_level = TM_PUTTY;
1438 /* VT100 only here, checked above */
1441 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1444 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1447 default: /* specifically, 'B' */
1448 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1451 if( !has_compat(VT220) || c != '%' )
1452 termstate = TOPLEVEL;
1457 case 'P': /* Linux palette sequence */
1458 termstate = SEEN_OSC_P;
1461 case 'R': /* Linux palette reset */
1464 termstate = TOPLEVEL;
1466 case 'W': /* word-set */
1467 termstate = SEEN_OSC_W;
1470 case '0': case '1': case '2': case '3': case '4':
1471 case '5': case '6': case '7': case '8': case '9':
1472 esc_args[0] = 10 * esc_args[0] + c - '0';
1476 * Grotty hack to support xterm and DECterm title
1477 * sequences concurrently.
1479 if (esc_args[0] == 2) {
1483 /* else fall through */
1485 termstate = OSC_STRING;
1491 * This OSC stuff is EVIL. It takes just one character to get into
1492 * sysline mode and it's not initially obvious how to get out.
1493 * So I've added CR and LF as string aborts.
1494 * This shouldn't effect compatibility as I believe embedded
1495 * control characters are supposed to be interpreted (maybe?)
1496 * and they don't display anything useful anyway.
1500 if (c == '\n' || c == '\r') {
1501 termstate = TOPLEVEL;
1502 } else if (c == 0234 || c == '\007' ) {
1504 * These characters terminate the string; ST and BEL
1505 * terminate the sequence and trigger instant
1506 * processing of it, whereas ESC goes back to SEEN_ESC
1507 * mode unless it is followed by \, in which case it is
1508 * synonymous with ST in the first place.
1511 termstate = TOPLEVEL;
1512 } else if (c == '\033')
1513 termstate = OSC_MAYBE_ST;
1514 else if (osc_strlen < OSC_STR_MAX)
1515 osc_string[osc_strlen++] = c;
1519 int max = (osc_strlen == 0 ? 21 : 16);
1521 if (c >= '0' && c <= '9')
1523 else if (c >= 'A' && c <= 'A'+max-10)
1525 else if (c >= 'a' && c <= 'a'+max-10)
1528 termstate = TOPLEVEL;
1529 osc_string[osc_strlen++] = val;
1530 if (osc_strlen >= 7) {
1531 palette_set (osc_string[0],
1532 osc_string[1] * 16 + osc_string[2],
1533 osc_string[3] * 16 + osc_string[4],
1534 osc_string[5] * 16 + osc_string[6]);
1536 termstate = TOPLEVEL;
1542 case '0': case '1': case '2': case '3': case '4':
1543 case '5': case '6': case '7': case '8': case '9':
1544 esc_args[0] = 10 * esc_args[0] + c - '0';
1547 termstate = OSC_STRING;
1554 unsigned long nlattr;
1560 n = rows * (cols+1);
1562 *p++ = ATTR_DEFAULT | 'E';
1564 seen_disp_event = TRUE;
1565 check_selection (scrtop, scrtop + rows * (cols+1));
1568 case '3': nlattr = LATTR_TOP; if(0) {
1569 case '4': nlattr = LATTR_BOT; } if(0) {
1570 case '5': nlattr = LATTR_NORM; } if(0) {
1571 case '6': nlattr = LATTR_WIDE; }
1573 p = scrtop + curs_y * (cols+1) + cols;
1578 termstate = TOPLEVEL;
1581 termstate = TOPLEVEL;
1582 seen_disp_event = TRUE;
1585 move (curs_x, curs_y - 1, 1);
1588 move (curs_x, curs_y + 1, 1);
1591 move (curs_x + 1, curs_y, 1);
1594 move (curs_x - 1, curs_y, 1);
1597 cset_attr[cset=0] = ATTR_LINEDRW;
1600 cset_attr[cset=0] = ATTR_ASCII;
1607 scroll (0, rows-1, -1, TRUE);
1608 else if (curs_y > 0)
1614 erase_lots(FALSE, FALSE, TRUE);
1618 erase_lots(TRUE, FALSE, TRUE);
1621 /* XXX Print cursor line */
1624 /* XXX Start controller mode */
1627 /* XXX Stop controller mode */
1630 termstate = VT52_Y1;
1633 ldisc->send ("\033/Z", 3);
1636 app_cursor_keys = TRUE;
1639 app_cursor_keys = FALSE;
1642 /* XXX This should switch to VT100 mode not current or default
1643 * VT mode. But this will only have effect in a VT220+
1649 /* XXX Enter auto print mode */
1652 /* XXX Exit auto print mode */
1655 /* XXX Print screen */
1660 termstate = VT52_Y2;
1661 move(curs_x, c-' ', 0);
1664 termstate = TOPLEVEL;
1665 move(c-' ', curs_y, 0);
1668 if (selstate != NO_SELECTION)
1669 check_selection (cpos, cpos+1);
1675 if(!beep_count) beep_overload=0;
1677 else if(beep_count && beep_count<5 && cfg.beep)
1682 * Compare two lines to determine whether they are sufficiently
1683 * alike to scroll-optimise one to the other. Return the degree of
1686 static int linecmp (unsigned long *a, unsigned long *b) {
1689 for (i=n=0; i < cols; i++)
1690 n += (*a++ == *b++);
1695 * Given a context, update the window. Out of paranoia, we don't
1696 * allow WM_PAINT responses to do scrolling optimisations.
1698 static void do_paint (Context ctx, int may_optimise){
1699 int i, j, start, our_curs_y;
1700 unsigned long attr, rv, cursor;
1705 if (blinker || !cfg.blink_cur)
1706 cursor = ATTR_ACTCURS;
1711 cursor = ATTR_PASCURS;
1714 rv = (rvideo ? ATTR_REVERSE : 0);
1715 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1717 for (i=0; i<rows; i++) {
1718 int idx = i*(cols+1);
1719 int lattr = (disptop[idx+cols] & LATTR_MODE);
1720 for (j=0; j<=cols; j++,idx++) {
1721 unsigned long *d = disptop+idx;
1722 wanttext[idx] = lattr | ((*d ^ rv
1723 ^ (selstart <= d && d < selend ?
1724 ATTR_REVERSE : 0)) |
1725 (i==our_curs_y && j==curs_x ? cursor : 0));
1727 if (blink_is_real) {
1728 if (has_focus && tblinker && (wanttext[idx]&ATTR_BLINK) )
1730 wanttext[idx] &= ATTR_MASK;
1731 wanttext[idx] += ' ';
1733 wanttext[idx] &= ~ATTR_BLINK;
1739 * We would perform scrolling optimisations in here, if they
1740 * didn't have a nasty tendency to cause the whole sodding
1741 * program to hang for a second at speed-critical moments.
1742 * We'll leave it well alone...
1745 for (i=0; i<rows; i++) {
1746 int idx = i*(cols+1);
1747 int lattr = (wanttext[idx+cols] & LATTR_MODE);
1749 for (j=0; j<=cols; j++,idx++) {
1750 unsigned long t = wanttext[idx];
1751 int needs_update = (j < cols && t != disptext[idx]);
1752 int keep_going = (start != -1 && needs_update &&
1753 (t & ATTR_MASK) == attr &&
1754 j-start < sizeof(ch));
1755 if (start != -1 && !keep_going) {
1756 do_text (ctx, start, i, ch, j-start, attr, lattr);
1762 attr = t & ATTR_MASK;
1764 ch[j-start] = (char) (t & CHAR_MASK);
1772 * Flick the switch that says if blinking things should be shown or hidden.
1775 void term_blink(int flg) {
1776 static long last_blink = 0;
1777 static long last_tblink = 0;
1778 long now, blink_diff;
1780 now = GetTickCount();
1781 blink_diff = now-last_tblink;
1783 /* Make sure the text blinks no more than 2Hz */
1784 if (blink_diff<0 || blink_diff>450)
1787 tblinker = !tblinker;
1796 blink_diff = now-last_blink;
1798 /* Make sure the cursor blinks no more than 2Hz */
1799 if (blink_diff>=0 && blink_diff<450)
1807 * Invalidate the whole screen so it will be repainted in full.
1809 void term_invalidate(void) {
1812 for (i=0; i<rows*(cols+1); i++)
1813 disptext[i] = ATTR_INVALID;
1817 * Paint the window in response to a WM_PAINT message.
1819 void term_paint (Context ctx, int l, int t, int r, int b) {
1820 int i, j, left, top, right, bottom;
1822 left = l / font_width;
1823 right = (r - 1) / font_width;
1824 top = t / font_height;
1825 bottom = (b - 1) / font_height;
1826 for (i = top; i <= bottom && i < rows ; i++)
1828 if ( (disptext[i*(cols+1)+cols]&LATTR_MODE) == LATTR_NORM)
1829 for (j = left; j <= right && j < cols ; j++)
1830 disptext[i*(cols+1)+j] = ATTR_INVALID;
1832 for (j = left/2; j <= right/2+1 && j < cols ; j++)
1833 disptext[i*(cols+1)+j] = ATTR_INVALID;
1836 /* This should happen soon enough, also for some reason it sometimes
1837 * fails to actually do anything when re-sizing ... painting the wrong
1839 do_paint (ctx, FALSE);
1844 * Attempt to scroll the scrollback. The second parameter gives the
1845 * position we want to scroll to; the first is +1 to denote that
1846 * this position is relative to the beginning of the scrollback, -1
1847 * to denote it is relative to the end, and 0 to denote that it is
1848 * relative to the current position.
1850 void term_scroll (int rel, int where) {
1851 int n = where * (cols+1);
1853 disptop = (rel < 0 ? scrtop :
1854 rel > 0 ? sbtop : disptop) + n;
1855 if (disptop < sbtop)
1857 if (disptop > scrtop)
1864 * Spread the selection outwards according to the selection mode.
1866 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1867 unsigned long *linestart, *lineend;
1871 x = (p - text) % (cols+1);
1873 lineend = linestart + cols;
1878 * In this mode, every character is a separate unit, except
1879 * for runs of spaces at the end of a non-wrapping line.
1881 if (!(linestart[cols] & ATTR_WRAPPED)) {
1882 unsigned long *q = lineend;
1883 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1888 p = (dir == -1 ? q : lineend - 1);
1893 * In this mode, the units are maximal runs of characters
1894 * whose `wordness' has the same value.
1896 wvalue = wordness[*p & CHAR_MASK];
1898 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1901 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1907 * In this mode, every line is a unit.
1909 p = (dir == -1 ? linestart : lineend - 1);
1915 static void sel_spread (void) {
1916 selstart = sel_spread_half (selstart, -1);
1917 selend = sel_spread_half (selend - 1, +1) + 1;
1920 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
1921 unsigned long *selpoint;
1924 if (y>=rows) y = rows-1;
1932 if (x>=cols) x = cols-1;
1934 selpoint = disptop + y * (cols+1);
1935 if ((selpoint[cols]&LATTR_MODE) != LATTR_NORM)
1940 if (b == MB_SELECT && a == MA_CLICK) {
1942 selstate = ABOUT_TO;
1943 selanchor = selpoint;
1945 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1947 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1948 selstate = DRAGGING;
1949 selstart = selanchor = selpoint;
1950 selend = selstart + 1;
1952 } else if ((b == MB_SELECT && a == MA_DRAG) ||
1953 (b == MB_EXTEND && a != MA_RELEASE)) {
1954 if (selstate == ABOUT_TO && selanchor == selpoint)
1956 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1957 if (selpoint-selstart < (selend-selstart)/2)
1958 selanchor = selend - 1;
1960 selanchor = selstart;
1961 selstate = DRAGGING;
1963 if (selstate != ABOUT_TO && selstate != DRAGGING)
1964 selanchor = selpoint;
1965 selstate = DRAGGING;
1966 if (selpoint < selanchor) {
1967 selstart = selpoint;
1968 selend = selanchor + 1;
1970 selstart = selanchor;
1971 selend = selpoint + 1;
1974 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
1975 if (selstate == DRAGGING) {
1977 * We've completed a selection. We now transfer the
1978 * data to the clipboard.
1980 unsigned char *p = selspace;
1981 unsigned long *q = selstart;
1983 while (q < selend) {
1985 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1986 unsigned long *nlpos = lineend;
1988 if (!(*nlpos & ATTR_WRAPPED)) {
1989 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1994 while (q < nlpos && q < selend)
1995 *p++ = (unsigned char) (*q++ & CHAR_MASK);
1998 for (i=0; i<sizeof(sel_nl); i++)
2001 q = lineend + 1; /* start of next line */
2003 write_clip (selspace, p - selspace);
2004 selstate = SELECTED;
2006 selstate = NO_SELECTION;
2007 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
2011 get_clip((void **) &data, &len);
2015 if (paste_buffer) sfree(paste_buffer);
2016 paste_pos = paste_hold = paste_len = 0;
2017 paste_buffer = smalloc(len);
2020 while (p < data+len) {
2021 while (p < data+len &&
2022 !(p <= data+len-sizeof(sel_nl) &&
2023 !memcmp(p, sel_nl, sizeof(sel_nl))))
2031 c=xlat_kbd2tty(q[i]);
2032 paste_buffer[paste_len++] = c;
2036 if (p <= data+len-sizeof(sel_nl) &&
2037 !memcmp(p, sel_nl, sizeof(sel_nl))) {
2038 paste_buffer[paste_len++] = '\r';
2039 p += sizeof(sel_nl);
2044 /* Assume a small paste will be OK in one go. */
2045 if (paste_len<256) {
2046 ldisc->send (paste_buffer, paste_len);
2047 if (paste_buffer) sfree(paste_buffer);
2049 paste_pos = paste_hold = paste_len = 0;
2052 get_clip(NULL, NULL);
2058 void term_nopaste() {
2059 if(paste_len == 0) return;
2060 sfree(paste_buffer);
2066 static long last_paste = 0;
2067 long now, paste_diff;
2069 if(paste_len == 0) return;
2071 /* Don't wait forever to paste */
2073 now = GetTickCount();
2074 paste_diff = now-last_paste;
2075 if (paste_diff>=0 && paste_diff<450)
2080 while(paste_pos<paste_len)
2082 char c = paste_buffer[paste_pos++];
2083 ldisc->send (&c, 1);
2090 sfree(paste_buffer);
2095 static void deselect (void) {
2096 selstate = NO_SELECTION;
2097 selstart = selend = scrtop;
2100 void term_deselect (void) {