10 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
11 #define CL_VT100 0x0002 /* VT100 */
12 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
13 #define CL_VT102 0x0008 /* VT102 */
14 #define CL_VT220 0x0010 /* VT220 */
15 #define CL_VT320 0x0020 /* VT320 */
16 #define CL_VT420 0x0040 /* VT420 */
17 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
18 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
19 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
20 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
21 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
23 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
24 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
25 #define TM_VT102 (TM_VT100AVO|CL_VT102)
26 #define TM_VT220 (TM_VT102|CL_VT220)
27 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
28 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
30 #define TM_PUTTY (0xFFFF)
32 #define compatibility(x) \
33 if ( ((CL_##x)&compatibility_level) == 0 ) { \
37 #define compatibility2(x,y) \
38 if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
43 #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
45 static int compatibility_level = TM_PUTTY;
48 static unsigned long *text; /* buffer of text on terminal screen */
49 static unsigned long *scrtop; /* top of working screen */
50 static unsigned long *disptop; /* top of displayed screen */
51 static unsigned long *sbtop; /* top of scrollback */
52 static unsigned long *sbbot; /* furthest extent of scrollback */
53 static unsigned long *cpos; /* cursor position (convenience) */
54 static unsigned long *disptext; /* buffer of text on real screen */
55 static unsigned long *wanttext; /* buffer of text we want on screen */
56 static unsigned long *alttext; /* buffer of text on alt. screen */
58 #define VBELL_TIMEOUT 100 /* millisecond duration of visual bell */
61 struct beeptime *next;
64 static struct beeptime *beephead, *beeptail;
69 static unsigned char *selspace; /* buffer for building selections in */
71 #define TSIZE (sizeof(*text))
72 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
74 static unsigned long curr_attr, save_attr;
75 static unsigned long erase_char = ERASE_CHAR;
77 static int curs_x, curs_y; /* cursor */
78 static int save_x, save_y; /* saved cursor position */
79 static int marg_t, marg_b; /* scroll margins */
80 static int dec_om; /* DEC origin mode flag */
81 static int wrap, wrapnext; /* wrap flags */
82 static int insert; /* insert-mode flag */
83 static int cset; /* 0 or 1: which char set */
84 static int save_cset, save_csattr; /* saved with cursor position */
85 static int rvideo; /* global reverse video flag */
86 static int cursor_on; /* cursor enabled flag */
87 static int reset_132; /* Flag ESC c resets to 80 cols */
88 static int use_bce; /* Use Background coloured erase */
89 static int blinker; /* When blinking is the cursor on ? */
90 static int tblinker; /* When the blinking text is on */
91 static int blink_is_real; /* Actually blink blinking text */
92 static int term_echoing; /* Does terminal want local echo? */
93 static int term_editing; /* Does terminal want local edit? */
95 static unsigned long cset_attr[2];
98 * Saved settings on the alternate screen.
100 static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
101 static int alt_t, alt_b;
102 static int alt_which;
104 #define ARGS_MAX 32 /* max # of esc sequence arguments */
105 #define ARG_DEFAULT 0 /* if an arg isn't specified */
106 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
107 static int esc_args[ARGS_MAX];
108 static int esc_nargs;
109 static int esc_query;
110 #define ANSI(x,y) ((x)+((y)<<8))
111 #define ANSI_QUE(x) ANSI(x,TRUE)
113 #define OSC_STR_MAX 2048
114 static int osc_strlen;
115 static char osc_string[OSC_STR_MAX+1];
118 static char id_string[1024] = "\033[?6c";
120 static unsigned char *tabs;
134 OSC_STRING, OSC_MAYBE_ST,
142 NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
145 SM_CHAR, SM_WORD, SM_LINE
147 static unsigned long *selstart, *selend, *selanchor;
149 static short wordness[256] = {
150 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 */
151 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 */
152 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 */
153 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 */
154 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 */
155 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 */
156 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 */
157 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 */
160 static unsigned char sel_nl[] = SEL_NL;
161 static char * paste_buffer = 0;
162 static int paste_len, paste_pos, paste_hold;
165 * Internal prototypes.
167 static void do_paint (Context, int);
168 static void erase_lots (int, int, int);
169 static void swap_screen (int);
170 static void update_sbar (void);
171 static void deselect (void);
172 /* log session to file stuff ... */
173 static FILE *lgfp = NULL;
174 static void logtraffic(unsigned char c, int logmode);
177 * Set up power-on settings for the terminal.
179 static void power_on(void) {
180 curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
183 alt_b = marg_b = rows - 1;
188 for (i = 0; i < cols; i++)
189 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
191 alt_om = dec_om = cfg.dec_om;
192 alt_wnext = wrapnext = alt_ins = insert = FALSE;
193 alt_wrap = wrap = cfg.wrap_mode;
195 cset_attr[0] = cset_attr[1] = ATTR_ASCII;
199 save_attr = curr_attr = ATTR_DEFAULT;
200 term_editing = term_echoing = FALSE;
201 ldisc_send(NULL, 0); /* cause ldisc to notice changes */
202 app_cursor_keys = cfg.app_cursor;
203 app_keypad_keys = cfg.app_keypad;
205 blink_is_real = cfg.blinktext;
206 erase_char = ERASE_CHAR;
210 for (i = 0; i < 256; i++)
211 wordness[i] = cfg.wordness[i];
215 erase_lots (FALSE, TRUE, TRUE);
217 erase_lots (FALSE, TRUE, TRUE);
222 * Force a screen update.
224 void term_update(void) {
228 if ( (seen_key_event && (cfg.scroll_on_key)) ||
229 (seen_disp_event && (cfg.scroll_on_disp)) ) {
231 seen_disp_event = seen_key_event = 0;
234 do_paint (ctx, TRUE);
235 sys_cursor(curs_x, curs_y + (scrtop - disptop) / (cols+1));
241 * Same as power_on(), but an external function.
243 void term_pwron(void) {
252 * Clear the scrollback.
254 void term_clrsb(void) {
255 disptop = sbtop = scrtop;
260 * Initialise the terminal.
262 void term_init(void) {
263 text = sbtop = sbbot = scrtop = disptop = cpos = NULL;
264 disptext = wanttext = NULL;
270 beephead = beeptail = NULL;
273 beep_overloaded = FALSE;
277 * Set up the terminal for a given size.
279 void term_size(int newrows, int newcols, int newsavelines) {
280 unsigned long *newtext, *newdisp, *newwant, *newalt;
281 int i, j, crows, ccols;
283 int save_alt_which = alt_which;
285 if (newrows == rows && newcols == cols && newsavelines == savelines)
286 return; /* nothing to do */
292 alt_b = marg_b = newrows - 1;
294 newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
295 sbbot = newtext + newsavelines*(newcols+1);
296 for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
297 newtext[i] = erase_char;
299 crows = rows + (scrtop - sbtop) / (cols+1);
300 if (crows > newrows+newsavelines)
301 crows = newrows+newsavelines;
305 disptop = newtext + (crows-newrows)*(newcols+1);
306 ccols = (cols < newcols ? cols : newcols);
307 for (i=0; i<crows; i++) {
308 int oldidx = (rows - crows + i) * (cols+1);
309 int newidx = (newrows - crows + i) * (newcols+1);
311 for (j=0; j<ccols; j++)
312 disptop[newidx+j] = scrtop[oldidx+j];
313 disptop[newidx+newcols] =
314 (cols == newcols ? scrtop[oldidx+cols]
315 : (scrtop[oldidx+cols]&LATTR_MODE));
319 sbtop = disptop = newtext;
325 newdisp = smalloc (newrows*(newcols+1)*TSIZE);
326 for (i=0; i<newrows*(newcols+1); i++)
327 newdisp[i] = ATTR_INVALID;
331 newwant = smalloc (newrows*(newcols+1)*TSIZE);
332 for (i=0; i<newrows*(newcols+1); i++)
333 newwant[i] = ATTR_INVALID;
337 newalt = smalloc (newrows*(newcols+1)*TSIZE);
338 for (i=0; i<newrows*(newcols+1); i++)
339 newalt[i] = erase_char;
344 selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
346 tabs = srealloc (tabs, newcols*sizeof(*tabs));
349 for (i = (cols > 0 ? cols : 0); i < newcols; i++)
350 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
354 curs_y += newrows - rows;
357 if (curs_y >= newrows)
359 if (curs_x >= newcols)
362 wrapnext = alt_wnext = FALSE;
366 savelines = newsavelines;
369 swap_screen(save_alt_which);
378 static void swap_screen (int which) {
382 if (which == alt_which)
387 for (t=0; t<rows*(cols+1); t++) {
388 tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
391 t = curs_x; curs_x = alt_x; alt_x = t;
392 t = curs_y; curs_y = alt_y; alt_y = t;
393 t = marg_t; marg_t = alt_t; alt_t = t;
394 t = marg_b; marg_b = alt_b; alt_b = t;
395 t = dec_om; dec_om = alt_om; alt_om = t;
396 t = wrap; wrap = alt_wrap; alt_wrap = t;
397 t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
398 t = insert; insert = alt_ins; alt_ins = t;
399 t = cset; cset = alt_cset; alt_cset = t;
405 * Update the scroll bar.
407 static void update_sbar(void) {
410 min = (sbtop - text) / (cols+1);
411 set_sbar ((scrtop - text) / (cols+1) + rows - min,
412 (disptop - text) / (cols+1) - min,
417 * Check whether the region bounded by the two pointers intersects
418 * the scroll region, and de-select the on-screen selection if so.
420 static void check_selection (unsigned long *from, unsigned long *to) {
421 if (from < selend && selstart < to)
426 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
427 * for backward.) `sb' is TRUE if the scrolling is permitted to
428 * affect the scrollback buffer.
430 static void scroll (int topline, int botline, int lines, int sb) {
431 unsigned long *scroll_top;
432 unsigned long *newscr;
433 int scroll_size, size, i;
434 int scrtop_is_disptop = (scrtop==disptop);
435 static int recursive = 0;
437 /* Only scroll more than the window if we're doing a 10% scroll */
438 if (!recursive && lines > botline - topline + 1)
439 lines = botline - topline + 1;
441 scroll_top = scrtop + topline*(cols+1);
442 size = (lines < 0 ? -lines : lines) * (cols+1);
443 scroll_size = (botline - topline + 1) * (cols+1) - size;
445 if (lines > 0 && topline == 0 && alt_which == 0 && sb) {
447 * Since we're going to scroll the top line and we're on the
448 * scrolling screen let's also effect the scrollback buffer.
450 * This is normally done by moving the position the screen
451 * painter reads from to reduce the amount of memory copying
454 if (scroll_size >= 0 && !recursive) {
455 newscr = scrtop + lines * (cols+1);
457 if (newscr > sbbot && botline == rows-1) {
458 /* We've hit the bottom of memory, so we have to do a
459 * physical scroll. But instead of just 1 line do it
460 * by 10% of the available memory.
462 * If the scroll region isn't the whole screen then we can't
463 * do this as it stands. We would need to recover the bottom
464 * of the screen from the scroll buffer after being sure that
465 * it doesn't get deleted.
468 i = (rows+savelines)/10;
470 /* Make it simple and ensure safe recursion */
471 if ( i<savelines-1) {
473 scroll(topline, botline, i, sb);
476 newscr = scrtop - i * (cols+1);
477 if (scrtop_is_disptop) disptop = newscr;
481 newscr = scrtop + lines * (cols+1);
484 if (newscr <= sbbot) {
485 if (scrtop_is_disptop) disptop = newscr;
488 if (botline == rows-1 )
489 for (i = 0; i < size; i++)
490 scrtop[i+scroll_size] = erase_char;
495 if (botline != rows-1) {
496 /* This fastscroll only works for full window scrolls.
497 * If the caller wanted a partial one we have to reverse
498 * scroll the bottom of the screen.
500 scroll(botline-lines+1, rows-1, -lines, 0);
505 /* If we can't scroll by memory remapping do it physically.
506 * But rather than expensivly doing the scroll buffer just
507 * scroll the screen. All it means is that sometimes we choose
508 * to not add lines from a scroll region to the scroll buffer.
511 if (savelines <= 400) {
512 sbtop -= lines * (cols+1);
515 scroll_size += scroll_top - sbtop;
521 /* Ho hum, expensive scroll required. */
523 sbtop -= lines * (cols+1);
526 scroll_size += scroll_top - sbtop;
533 if (scroll_size < 0) {
540 memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
541 for (i = 0; i < size; i++)
542 scroll_top[i+scroll_size] = erase_char;
543 if (selstart > scroll_top &&
544 selstart < scroll_top + size + scroll_size) {
546 if (selstart < scroll_top)
547 selstart = scroll_top;
549 if (selend > scroll_top &&
550 selend < scroll_top + size + scroll_size) {
552 if (selend < scroll_top)
555 if (scrtop_is_disptop)
558 if (disptop > scroll_top &&
559 disptop < scroll_top + size + scroll_size) {
561 if (disptop < scroll_top)
562 disptop = scroll_top;
566 memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
567 for (i = 0; i < size; i++)
568 scroll_top[i] = erase_char;
569 if (selstart > scroll_top &&
570 selstart < scroll_top + size + scroll_size) {
572 if (selstart > scroll_top + size + scroll_size)
573 selstart = scroll_top + size + scroll_size;
575 if (selend > scroll_top &&
576 selend < scroll_top + size + scroll_size) {
578 if (selend > scroll_top + size + scroll_size)
579 selend = scroll_top + size + scroll_size;
581 if (scrtop_is_disptop)
583 else if (disptop > scroll_top &&
584 disptop < scroll_top + size + scroll_size) {
586 if (disptop > scroll_top + size + scroll_size)
587 disptop = scroll_top + size + scroll_size;
593 * Move the cursor to a given position, clipping at boundaries. We
594 * may or may not want to clip at the scroll margin: marg_clip is 0
595 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
596 * even _being_ outside the margins.
598 static void move (int x, int y, int marg_clip) {
604 if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
606 if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
620 * Save or restore the cursor and SGR mode.
622 static void save_cursor(int save) {
626 save_attr = curr_attr;
628 save_csattr = cset_attr[cset];
632 /* Make sure the window hasn't shrunk since the save */
633 if (curs_x >= cols) curs_x = cols-1;
634 if (curs_y >= rows) curs_y = rows-1;
636 curr_attr = save_attr;
638 cset_attr[cset] = save_csattr;
640 if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
645 * Erase a large portion of the screen: the whole screen, or the
646 * whole line, or parts thereof.
648 static void erase_lots (int line_only, int from_begin, int to_end) {
649 unsigned long *startpos, *endpos;
652 startpos = cpos - curs_x;
653 endpos = startpos + cols;
654 /* I've removed the +1 so that the Wide screen stuff is not
655 * removed when it shouldn't be.
659 endpos = startpos + rows * (cols+1);
665 check_selection (startpos, endpos);
667 /* Clear screen also forces a full window redraw, just in case. */
668 if (startpos == scrtop && endpos == scrtop + rows * (cols+1))
671 while (startpos < endpos)
672 *startpos++ = erase_char;
676 * Insert or delete characters within the current line. n is +ve if
677 * insertion is desired, and -ve for deletion.
679 static void insch (int n) {
680 int dir = (n < 0 ? -1 : +1);
683 n = (n < 0 ? -n : n);
684 if (n > cols - curs_x)
686 m = cols - curs_x - n;
687 check_selection (cpos, cpos+n);
689 memmove (cpos, cpos+n, m*TSIZE);
691 cpos[m++] = erase_char;
693 memmove (cpos+n, cpos, m*TSIZE);
695 cpos[n] = erase_char;
700 * Toggle terminal mode `mode' to state `state'. (`query' indicates
701 * whether the mode is a DEC private one or a normal one.)
703 static void toggle_mode (int mode, int query, int state) {
704 if (query) switch (mode) {
705 case 1: /* application cursor keys */
706 app_cursor_keys = state;
708 case 2: /* VT52 mode */
711 case 3: /* 80/132 columns */
713 request_resize (state ? 132 : 80, rows, 1);
716 case 5: /* reverse video */
718 seen_disp_event = TRUE;
719 if (state) term_update();
721 case 6: /* DEC origin mode */
724 case 7: /* auto wrap */
727 case 8: /* auto key repeat */
730 case 10: /* set local edit mode */
731 term_editing = state;
732 ldisc_send(NULL, 0); /* cause ldisc to notice changes */
734 case 25: /* enable/disable cursor */
735 compatibility2(OTHER,VT220);
737 seen_disp_event = TRUE;
739 case 47: /* alternate screen */
740 compatibility(OTHER);
745 } else switch (mode) {
746 case 4: /* set insert mode */
747 compatibility(VT102);
750 case 12: /* set echo mode */
751 term_echoing = !state;
752 ldisc_send(NULL, 0); /* cause ldisc to notice changes */
754 case 20: /* Return sends ... */
755 cr_lf_return = state;
761 * Process an OSC sequence: set window title or icon name.
763 static void do_osc(void) {
766 wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
768 osc_string[osc_strlen] = '\0';
769 switch (esc_args[0]) {
772 set_icon (osc_string);
773 if (esc_args[0] == 1)
775 /* fall through: parameter 0 means set both */
778 set_title (osc_string);
785 * Remove everything currently in `inbuf' and stick it up on the
786 * in-memory display. There's a big state machine in here to
787 * process escape sequences...
789 void term_out(void) {
792 for(inbuf_reap = 0; inbuf_reap < inbuf_head; inbuf_reap++)
794 c = inbuf[inbuf_reap];
797 * Optionally log the session traffic to a file. Useful for
798 * debugging and possibly also useful for actual logging.
800 logtraffic((unsigned char)c, LGTYP_DEBUG);
802 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
803 * be able to display 8-bit characters, but I'll let that go 'cause
806 if( ( (c&0x60) == 0 || c == '\177') &&
807 termstate < DO_CTRLS &&
808 ( (c&0x80) == 0 || has_compat(VT220))) {
810 case '\005': /* terminal type query */
811 /* Strictly speaking this is VT100 but a VT100 defaults to
812 * no response. Other terminals respond at their option.
814 * Don't put a CR in the default string as this tends to
815 * upset some weird software.
817 * An xterm returns "xterm" (5 characters)
819 compatibility(ANSIMIN);
821 char abuf[256], *s, *d;
823 for(s=cfg.answerback, d=abuf; *s; s++) {
826 if (*s >= 'a' && *s <= 'z')
827 *d++ = (*s - ('a'-1));
828 else if ((*s >='@' && *s<='_') ||
829 *s == '?' || (*s&0x80))
835 else if (*s == '^') {
839 *d++ = xlat_kbd2tty((unsigned char)*s);
841 ldisc_send (abuf, d-abuf);
846 struct beeptime *newbeep;
849 ticks = GetTickCount();
851 if (!beep_overloaded) {
852 newbeep = smalloc(sizeof(struct beeptime));
853 newbeep->ticks = ticks;
854 newbeep->next = NULL;
858 beeptail->next = newbeep;
864 * Throw out any beeps that happened more than
868 beephead->ticks < ticks - cfg.bellovl_t*1000) {
869 struct beeptime *tmp = beephead;
870 beephead = tmp->next;
877 if (cfg.bellovl && beep_overloaded &&
878 ticks-lastbeep >= cfg.bellovl_s * 1000) {
880 * If we're currently overloaded and the
881 * last beep was more than s seconds ago,
882 * leave overload mode.
884 beep_overloaded = FALSE;
885 } else if (cfg.bellovl && !beep_overloaded &&
886 nbeeps >= cfg.bellovl_n) {
888 * Now, if we have n or more beeps
889 * remaining in the queue, go into overload
892 beep_overloaded = TRUE;
897 * Perform an actual beep if we're not overloaded.
899 if ((!cfg.bellovl || !beep_overloaded) && cfg.beep != 0) {
902 else if(cfg.beep == 2) {
904 vbell_timeout = ticks + VBELL_TIMEOUT;
912 if (curs_x == 0 && curs_y == 0)
914 else if (curs_x == 0 && curs_y > 0)
915 curs_x = cols-1, curs_y--;
921 seen_disp_event = TRUE;
924 compatibility(VT100);
928 compatibility(VT100);
933 termstate = VT52_ESC;
935 compatibility(ANSIMIN);
936 termstate = SEEN_ESC;
940 compatibility(VT220);
941 termstate = SEEN_CSI;
943 esc_args[0] = ARG_DEFAULT;
947 compatibility(VT220);
948 termstate = SEEN_OSC;
955 seen_disp_event = TRUE;
957 logtraffic((unsigned char)c,LGTYP_ASCII);
961 compatibility(VT100);
963 if (curs_y == marg_b)
964 scroll (marg_t, marg_b, 1, TRUE);
965 else if (curs_y < rows-1)
973 logtraffic((unsigned char)c,LGTYP_ASCII);
977 unsigned long *old_cpos = cpos;
978 unsigned long *p = scrtop + curs_y * (cols+1) + cols;
982 } while (curs_x < cols-1 && !tabs[curs_x]);
984 if ((*p & LATTR_MODE) != LATTR_NORM)
986 if (curs_x >= cols/2)
996 check_selection (old_cpos, cpos);
998 seen_disp_event = TRUE;
1000 case '\177': /* Destructive backspace
1001 This does nothing on a real VT100 */
1002 compatibility(OTHER);
1003 if (curs_x && !wrapnext) curs_x--;
1006 *cpos = (' ' | curr_attr | ATTR_ASCII);
1010 else switch (termstate) {
1012 /* Only graphic characters get this far, ctrls are stripped above */
1013 if (wrapnext && wrap) {
1014 cpos[1] |= ATTR_WRAPPED;
1015 if (curs_y == marg_b)
1016 scroll (marg_t, marg_b, 1, TRUE);
1017 else if (curs_y < rows-1)
1025 if (selstate != NO_SELECTION)
1026 check_selection (cpos, cpos+1);
1027 switch (cset_attr[cset]) {
1028 /* Linedraw characters are different from 'ESC ( B' only
1029 * for a small range, for ones outside that range make sure
1030 * we use the same font as well as the same encoding.
1033 if (c<0x5f || c>0x7F)
1034 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
1037 *cpos++ = ' ' | curr_attr | ATTR_ASCII;
1039 *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW;
1042 /* If UK-ASCII, make the '#' a LineDraw Pound */
1044 *cpos++ = '}' | curr_attr | ATTR_LINEDRW;
1049 *cpos = xlat_tty2scr((unsigned char)c) | curr_attr |
1050 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
1051 logtraffic((unsigned char)c, LGTYP_ASCII);
1056 if (curs_x == cols) {
1061 seen_disp_event = 1;
1065 termstate = TOPLEVEL;
1069 * This state is virtually identical to SEEN_ESC, with the
1070 * exception that we have an OSC sequence in the pipeline,
1071 * and _if_ we see a backslash, we process it.
1075 termstate = TOPLEVEL;
1078 /* else fall through */
1080 termstate = TOPLEVEL;
1082 case ' ': /* some weird sequence? */
1083 compatibility(VT220);
1084 termstate = IGNORE_NEXT;
1086 case '[': /* enter CSI mode */
1087 termstate = SEEN_CSI;
1089 esc_args[0] = ARG_DEFAULT;
1092 case ']': /* xterm escape sequences */
1093 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1094 compatibility(OTHER);
1095 termstate = SEEN_OSC;
1098 case '(': /* should set GL */
1099 compatibility(VT100);
1102 case ')': /* should set GR */
1103 compatibility(VT100);
1106 case '7': /* save cursor */
1107 compatibility(VT100);
1110 case '8': /* restore cursor */
1111 compatibility(VT100);
1112 save_cursor (FALSE);
1113 seen_disp_event = TRUE;
1116 compatibility(VT100);
1117 app_keypad_keys = TRUE;
1120 compatibility(VT100);
1121 app_keypad_keys = FALSE;
1123 case 'D': /* exactly equivalent to LF */
1124 compatibility(VT100);
1125 if (curs_y == marg_b)
1126 scroll (marg_t, marg_b, 1, TRUE);
1127 else if (curs_y < rows-1)
1131 seen_disp_event = TRUE;
1133 case 'E': /* exactly equivalent to CR-LF */
1134 compatibility(VT100);
1136 if (curs_y == marg_b)
1137 scroll (marg_t, marg_b, 1, TRUE);
1138 else if (curs_y < rows-1)
1142 seen_disp_event = TRUE;
1144 case 'M': /* reverse index - backwards LF */
1145 compatibility(VT100);
1146 if (curs_y == marg_t)
1147 scroll (marg_t, marg_b, -1, TRUE);
1148 else if (curs_y > 0)
1152 seen_disp_event = TRUE;
1154 case 'Z': /* terminal type query */
1155 compatibility(VT100);
1156 ldisc_send (id_string, strlen(id_string));
1158 case 'c': /* restore power-on settings */
1159 compatibility(VT100);
1162 request_resize (80, rows, 1);
1167 seen_disp_event = TRUE;
1169 case '#': /* ESC # 8 fills screen with Es :-) */
1170 compatibility(VT100);
1171 termstate = SEEN_ESCHASH;
1173 case 'H': /* set a tab */
1174 compatibility(VT100);
1175 tabs[curs_x] = TRUE;
1180 termstate = TOPLEVEL; /* default */
1183 if (esc_nargs <= ARGS_MAX) {
1184 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
1185 esc_args[esc_nargs-1] = 0;
1186 esc_args[esc_nargs-1] =
1187 10 * esc_args[esc_nargs-1] + c - '0';
1189 termstate = SEEN_CSI;
1193 if (++esc_nargs <= ARGS_MAX)
1194 esc_args[esc_nargs-1] = ARG_DEFAULT;
1195 termstate = SEEN_CSI;
1199 if( esc_query ) esc_query = -1;
1200 else if( c == '?' ) esc_query = TRUE;
1202 termstate = SEEN_CSI;
1204 else switch (ANSI(c,esc_query)) {
1205 case 'A': /* move up N lines */
1206 move (curs_x, curs_y - def(esc_args[0], 1), 1);
1207 seen_disp_event = TRUE;
1209 case 'e': /* move down N lines */
1210 compatibility(ANSI);
1212 move (curs_x, curs_y + def(esc_args[0], 1), 1);
1213 seen_disp_event = TRUE;
1215 case 'a': /* move right N cols */
1216 compatibility(ANSI);
1218 move (curs_x + def(esc_args[0], 1), curs_y, 1);
1219 seen_disp_event = TRUE;
1221 case 'D': /* move left N cols */
1222 move (curs_x - def(esc_args[0], 1), curs_y, 1);
1223 seen_disp_event = TRUE;
1225 case 'E': /* move down N lines and CR */
1226 compatibility(ANSI);
1227 move (0, curs_y + def(esc_args[0], 1), 1);
1228 seen_disp_event = TRUE;
1230 case 'F': /* move up N lines and CR */
1231 compatibility(ANSI);
1232 move (0, curs_y - def(esc_args[0], 1), 1);
1233 seen_disp_event = TRUE;
1235 case 'G': case '`': /* set horizontal posn */
1236 compatibility(ANSI);
1237 move (def(esc_args[0], 1) - 1, curs_y, 0);
1238 seen_disp_event = TRUE;
1240 case 'd': /* set vertical posn */
1241 compatibility(ANSI);
1242 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1244 seen_disp_event = TRUE;
1246 case 'H': case 'f': /* set horz and vert posns at once */
1248 esc_args[1] = ARG_DEFAULT;
1249 move (def(esc_args[1], 1) - 1,
1250 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1252 seen_disp_event = TRUE;
1254 case 'J': /* erase screen or parts of it */
1256 unsigned int i = def(esc_args[0], 0) + 1;
1259 erase_lots(FALSE, !!(i & 2), !!(i & 1));
1262 seen_disp_event = TRUE;
1264 case 'K': /* erase line or parts of it */
1266 unsigned int i = def(esc_args[0], 0) + 1;
1269 erase_lots(TRUE, !!(i & 2), !!(i & 1));
1271 seen_disp_event = TRUE;
1273 case 'L': /* insert lines */
1274 compatibility(VT102);
1275 if (curs_y <= marg_b)
1276 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
1277 seen_disp_event = TRUE;
1279 case 'M': /* delete lines */
1280 compatibility(VT102);
1281 if (curs_y <= marg_b)
1282 scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
1283 seen_disp_event = TRUE;
1285 case '@': /* insert chars */
1286 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1287 compatibility(VT102);
1288 insch (def(esc_args[0], 1));
1289 seen_disp_event = TRUE;
1291 case 'P': /* delete chars */
1292 compatibility(VT102);
1293 insch (-def(esc_args[0], 1));
1294 seen_disp_event = TRUE;
1296 case 'c': /* terminal type query */
1297 compatibility(VT100);
1298 /* This is the response for a VT102 */
1299 ldisc_send (id_string, strlen(id_string));
1301 case 'n': /* cursor position query */
1302 if (esc_args[0] == 6) {
1304 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
1305 ldisc_send (buf, strlen(buf));
1307 else if (esc_args[0] == 5) {
1308 ldisc_send ("\033[0n", 4);
1311 case 'h': /* toggle modes to high */
1313 compatibility(VT100);
1316 for (i=0; i<esc_nargs; i++)
1317 toggle_mode (esc_args[i], esc_query, TRUE);
1320 case 'l': /* toggle modes to low */
1322 compatibility(VT100);
1325 for (i=0; i<esc_nargs; i++)
1326 toggle_mode (esc_args[i], esc_query, FALSE);
1329 case 'g': /* clear tabs */
1330 compatibility(VT100);
1331 if (esc_nargs == 1) {
1332 if (esc_args[0] == 0) {
1333 tabs[curs_x] = FALSE;
1334 } else if (esc_args[0] == 3) {
1336 for (i = 0; i < cols; i++)
1341 case 'r': /* set scroll margins */
1342 compatibility(VT100);
1343 if (esc_nargs <= 2) {
1345 top = def(esc_args[0], 1) - 1;
1346 bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
1347 def(esc_args[1], rows)) - 1;
1350 /* VTTEST Bug 9 - if region is less than 2 lines
1351 * don't change region.
1358 * I used to think the cursor should be
1359 * placed at the top of the newly marginned
1360 * area. Apparently not: VMS TPU falls over
1363 * Well actually it should for Origin mode - RDB
1365 curs_y = (dec_om ? marg_t : 0);
1367 seen_disp_event = TRUE;
1371 case 'm': /* set graphics rendition */
1374 * A VT100 without the AVO only had one attribute, either
1375 * underline or reverse video depending on the cursor type,
1376 * this was selected by CSI 7m.
1379 * This is DIM on the VT100-AVO and VT102
1381 * This is BLINK on the VT100-AVO and VT102+
1383 * This is INVIS on the VT100-AVO and VT102
1385 * This like 22 disables BOLD, DIM and INVIS
1387 * The ANSI colours appear on any terminal that has colour
1388 * (obviously) but the interaction between sgr0 and the
1389 * colours varies but is usually related to the background
1390 * colour erase item.
1391 * The interaction between colour attributes and the mono
1392 * ones is also very implementation dependent.
1394 * The 39 and 49 attributes are likely to be unimplemented.
1397 for (i=0; i<esc_nargs; i++) {
1398 switch (def(esc_args[i], 0)) {
1399 case 0: /* restore defaults */
1400 curr_attr = ATTR_DEFAULT; break;
1401 case 1: /* enable bold */
1402 compatibility(VT100AVO);
1403 curr_attr |= ATTR_BOLD; break;
1404 case 21: /* (enable double underline) */
1405 compatibility(OTHER);
1406 case 4: /* enable underline */
1407 compatibility(VT100AVO);
1408 curr_attr |= ATTR_UNDER; break;
1409 case 5: /* enable blink */
1410 compatibility(VT100AVO);
1411 curr_attr |= ATTR_BLINK; break;
1412 case 7: /* enable reverse video */
1413 curr_attr |= ATTR_REVERSE; break;
1414 case 22: /* disable bold */
1415 compatibility2(OTHER,VT220);
1416 curr_attr &= ~ATTR_BOLD; break;
1417 case 24: /* disable underline */
1418 compatibility2(OTHER,VT220);
1419 curr_attr &= ~ATTR_UNDER; break;
1420 case 25: /* disable blink */
1421 compatibility2(OTHER,VT220);
1422 curr_attr &= ~ATTR_BLINK; break;
1423 case 27: /* disable reverse video */
1424 compatibility2(OTHER,VT220);
1425 curr_attr &= ~ATTR_REVERSE; break;
1426 case 30: case 31: case 32: case 33:
1427 case 34: case 35: case 36: case 37:
1429 curr_attr &= ~ATTR_FGMASK;
1430 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
1432 case 39: /* default-foreground */
1433 curr_attr &= ~ATTR_FGMASK;
1434 curr_attr |= ATTR_DEFFG;
1436 case 40: case 41: case 42: case 43:
1437 case 44: case 45: case 46: case 47:
1439 curr_attr &= ~ATTR_BGMASK;
1440 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
1442 case 49: /* default-background */
1443 curr_attr &= ~ATTR_BGMASK;
1444 curr_attr |= ATTR_DEFBG;
1451 (curr_attr&(ATTR_FGMASK|ATTR_BGMASK|ATTR_BLINK))
1455 case 's': /* save cursor */
1458 case 'u': /* restore cursor */
1459 save_cursor (FALSE);
1460 seen_disp_event = TRUE;
1462 case 't': /* set page size - ie window height */
1464 * VT340/VT420 sequence DECSLPP, DEC only allows values
1465 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1466 * illegal values (eg first arg 1..9) for window changing
1469 compatibility(VT340TEXT);
1470 if (esc_nargs<=1 && (esc_args[0]<1 || esc_args[0]>=24)) {
1471 request_resize (cols, def(esc_args[0], 24), 0);
1475 case ANSI('|', '*'):
1476 /* VT420 sequence DECSNLS
1477 * Set number of lines on screen
1478 * VT420 uses VGA like hardware and can support any size in
1479 * reasonable range (24..49 AIUI) with no default specified.
1481 compatibility(VT420);
1482 if (esc_nargs==1 && esc_args[0]>0) {
1483 request_resize (cols, def(esc_args[0], cfg.height), 0);
1487 case ANSI('|', '$'):
1488 /* VT340/VT420 sequence DECSCPP
1489 * Set number of columns per page
1490 * Docs imply range is only 80 or 132, but I'll allow any.
1492 compatibility(VT340TEXT);
1494 request_resize (def(esc_args[0], cfg.width), rows, 0);
1498 case 'X': /* write N spaces w/o moving cursor */
1499 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1500 compatibility(ANSIMIN);
1502 int n = def(esc_args[0], 1);
1503 unsigned long *p = cpos;
1504 if (n > cols - curs_x)
1506 check_selection (cpos, cpos+n);
1509 seen_disp_event = TRUE;
1512 case 'x': /* report terminal characteristics */
1513 compatibility(VT100);
1516 int i = def(esc_args[0], 0);
1517 if (i == 0 || i == 1) {
1518 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1520 ldisc_send (buf, 20);
1525 compatibility(OTHER);
1526 use_bce = (esc_args[0]<=0);
1527 erase_char = ERASE_CHAR;
1529 erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
1532 compatibility(OTHER);
1533 blink_is_real = (esc_args[0]>=1);
1536 /* Allow the host to make this emulator a 'perfect' VT102.
1537 * This first appeared in the VT220, but we do need to get
1538 * back to PuTTY mode so I won't check it.
1540 * The arg in 40..42 are a PuTTY extension.
1541 * The 2nd arg, 8bit vs 7bit is not checked.
1543 * Setting VT102 mode should also change the Fkeys to
1544 * generate PF* codes as a real VT102 has no Fkeys.
1545 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1548 * Note ESC c will NOT change this!
1551 switch (esc_args[0]) {
1552 case 61: compatibility_level &= ~TM_VTXXX;
1553 compatibility_level |= TM_VT102; break;
1554 case 62: compatibility_level &= ~TM_VTXXX;
1555 compatibility_level |= TM_VT220; break;
1557 default: if( esc_args[0] > 60 && esc_args[0] < 70 )
1558 compatibility_level |= TM_VTXXX;
1561 case 40: compatibility_level &= TM_VTXXX; break;
1562 case 41: compatibility_level = TM_PUTTY; break;
1563 case 42: compatibility_level = TM_SCOANSI; break;
1566 compatibility_level = TM_PUTTY; break;
1570 /* Change the response to CSI c */
1571 if (esc_args[0] == 50) {
1574 strcpy(id_string, "\033[?");
1575 for (i=1; i<esc_nargs; i++) {
1576 if (i!=1) strcat(id_string, ";");
1577 sprintf(lbuf, "%d", esc_args[i]);
1578 strcat(id_string, lbuf);
1580 strcat(id_string, "c");
1584 /* Is this a good idea ?
1585 * Well we should do a soft reset at this point ...
1587 if (!has_compat(VT420) && has_compat(VT100)) {
1588 if (reset_132) request_resize (132, 24, 1);
1589 else request_resize ( 80, 24, 1);
1597 /* VT100 only here, checked above */
1600 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1603 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1606 default: /* specifically, 'B' */
1607 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1610 if( !has_compat(VT220) || c != '%' )
1611 termstate = TOPLEVEL;
1616 case 'P': /* Linux palette sequence */
1617 termstate = SEEN_OSC_P;
1620 case 'R': /* Linux palette reset */
1623 termstate = TOPLEVEL;
1625 case 'W': /* word-set */
1626 termstate = SEEN_OSC_W;
1629 case '0': case '1': case '2': case '3': case '4':
1630 case '5': case '6': case '7': case '8': case '9':
1631 esc_args[0] = 10 * esc_args[0] + c - '0';
1635 * Grotty hack to support xterm and DECterm title
1636 * sequences concurrently.
1638 if (esc_args[0] == 2) {
1642 /* else fall through */
1644 termstate = OSC_STRING;
1650 * This OSC stuff is EVIL. It takes just one character to get into
1651 * sysline mode and it's not initially obvious how to get out.
1652 * So I've added CR and LF as string aborts.
1653 * This shouldn't effect compatibility as I believe embedded
1654 * control characters are supposed to be interpreted (maybe?)
1655 * and they don't display anything useful anyway.
1659 if (c == '\n' || c == '\r') {
1660 termstate = TOPLEVEL;
1661 } else if (c == 0234 || c == '\007' ) {
1663 * These characters terminate the string; ST and BEL
1664 * terminate the sequence and trigger instant
1665 * processing of it, whereas ESC goes back to SEEN_ESC
1666 * mode unless it is followed by \, in which case it is
1667 * synonymous with ST in the first place.
1670 termstate = TOPLEVEL;
1671 } else if (c == '\033')
1672 termstate = OSC_MAYBE_ST;
1673 else if (osc_strlen < OSC_STR_MAX)
1674 osc_string[osc_strlen++] = c;
1678 int max = (osc_strlen == 0 ? 21 : 16);
1680 if (c >= '0' && c <= '9')
1682 else if (c >= 'A' && c <= 'A'+max-10)
1684 else if (c >= 'a' && c <= 'a'+max-10)
1687 termstate = TOPLEVEL;
1688 osc_string[osc_strlen++] = val;
1689 if (osc_strlen >= 7) {
1690 palette_set (osc_string[0],
1691 osc_string[1] * 16 + osc_string[2],
1692 osc_string[3] * 16 + osc_string[4],
1693 osc_string[5] * 16 + osc_string[6]);
1695 termstate = TOPLEVEL;
1701 case '0': case '1': case '2': case '3': case '4':
1702 case '5': case '6': case '7': case '8': case '9':
1703 esc_args[0] = 10 * esc_args[0] + c - '0';
1706 termstate = OSC_STRING;
1713 unsigned long nlattr;
1719 n = rows * (cols+1);
1721 *p++ = ATTR_DEFAULT | 'E';
1723 seen_disp_event = TRUE;
1724 check_selection (scrtop, scrtop + rows * (cols+1));
1727 case '3': nlattr = LATTR_TOP; if(0) {
1728 case '4': nlattr = LATTR_BOT; } if(0) {
1729 case '5': nlattr = LATTR_NORM; } if(0) {
1730 case '6': nlattr = LATTR_WIDE; }
1732 p = scrtop + curs_y * (cols+1) + cols;
1737 termstate = TOPLEVEL;
1740 termstate = TOPLEVEL;
1741 seen_disp_event = TRUE;
1744 move (curs_x, curs_y - 1, 1);
1747 move (curs_x, curs_y + 1, 1);
1750 move (curs_x + 1, curs_y, 1);
1753 move (curs_x - 1, curs_y, 1);
1756 cset_attr[cset=0] = ATTR_LINEDRW;
1759 cset_attr[cset=0] = ATTR_ASCII;
1766 scroll (0, rows-1, -1, TRUE);
1767 else if (curs_y > 0)
1773 erase_lots(FALSE, FALSE, TRUE);
1777 erase_lots(TRUE, FALSE, TRUE);
1780 /* XXX Print cursor line */
1783 /* XXX Start controller mode */
1786 /* XXX Stop controller mode */
1789 termstate = VT52_Y1;
1792 ldisc_send ("\033/Z", 3);
1795 app_keypad_keys = TRUE;
1798 app_keypad_keys = FALSE;
1801 /* XXX This should switch to VT100 mode not current or default
1802 * VT mode. But this will only have effect in a VT220+
1808 /* XXX Enter auto print mode */
1811 /* XXX Exit auto print mode */
1814 /* XXX Print screen */
1819 termstate = VT52_Y2;
1820 move(curs_x, c-' ', 0);
1823 termstate = TOPLEVEL;
1824 move(c-' ', curs_y, 0);
1827 if (selstate != NO_SELECTION)
1828 check_selection (cpos, cpos+1);
1834 * Compare two lines to determine whether they are sufficiently
1835 * alike to scroll-optimise one to the other. Return the degree of
1838 static int linecmp (unsigned long *a, unsigned long *b) {
1841 for (i=n=0; i < cols; i++)
1842 n += (*a++ == *b++);
1847 * Given a context, update the window. Out of paranoia, we don't
1848 * allow WM_PAINT responses to do scrolling optimisations.
1850 static void do_paint (Context ctx, int may_optimise){
1851 int i, j, start, our_curs_y;
1852 unsigned long attr, rv, cursor;
1857 * Check the visual bell state.
1860 ticks = GetTickCount();
1861 if (ticks - vbell_timeout >= 0)
1866 * screen array, disptop, scrtop,
1868 * cfg.blinkpc, blink_is_real, tblinker,
1869 * curs_y, curs_x, blinker, cfg.blink_cur, cursor_on, has_focus
1873 if (blinker || !cfg.blink_cur)
1874 cursor = ATTR_ACTCURS;
1879 cursor = ATTR_PASCURS;
1881 cursor |= ATTR_RIGHTCURS;
1885 rv = (!rvideo ^ !in_vbell ? ATTR_REVERSE : 0);
1886 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1888 for (i=0; i<rows; i++) {
1889 int idx = i*(cols+1);
1890 int lattr = (disptop[idx+cols] & LATTR_MODE);
1891 for (j=0; j<=cols; j++,idx++) {
1892 unsigned long *d = disptop+idx;
1893 wanttext[idx] = lattr | (((*d &~ ATTR_WRAPPED) ^ rv
1894 ^ (selstart <= d && d < selend ?
1895 ATTR_REVERSE : 0)) |
1896 (i==our_curs_y && j==curs_x ? cursor : 0));
1897 if (blink_is_real) {
1898 if (has_focus && tblinker && (wanttext[idx]&ATTR_BLINK) )
1900 wanttext[idx] &= ATTR_MASK;
1901 wanttext[idx] += ' ';
1903 wanttext[idx] &= ~ATTR_BLINK;
1909 * We would perform scrolling optimisations in here, if they
1910 * didn't have a nasty tendency to cause the whole sodding
1911 * program to hang for a second at speed-critical moments.
1912 * We'll leave it well alone...
1915 for (i=0; i<rows; i++) {
1916 int idx = i*(cols+1);
1917 int lattr = (wanttext[idx+cols] & LATTR_MODE);
1919 for (j=0; j<=cols; j++,idx++) {
1920 unsigned long t = wanttext[idx];
1921 int needs_update = (j < cols && t != disptext[idx]);
1922 int keep_going = (start != -1 && needs_update &&
1923 (t & ATTR_MASK) == attr &&
1924 j-start < sizeof(ch));
1925 if (start != -1 && !keep_going) {
1926 do_text (ctx, start, i, ch, j-start, attr, lattr);
1932 attr = t & ATTR_MASK;
1934 ch[j-start] = (char) (t & CHAR_MASK);
1942 * Flick the switch that says if blinking things should be shown or hidden.
1945 void term_blink(int flg) {
1946 static long last_blink = 0;
1947 static long last_tblink = 0;
1948 long now, blink_diff;
1950 now = GetTickCount();
1951 blink_diff = now-last_tblink;
1953 /* Make sure the text blinks no more than 2Hz */
1954 if (blink_diff<0 || blink_diff>450)
1957 tblinker = !tblinker;
1966 blink_diff = now-last_blink;
1968 /* Make sure the cursor blinks no faster than GetCaretBlinkTime() */
1969 if (blink_diff>=0 && blink_diff<(long)GetCaretBlinkTime())
1977 * Invalidate the whole screen so it will be repainted in full.
1979 void term_invalidate(void) {
1982 for (i=0; i<rows*(cols+1); i++)
1983 disptext[i] = ATTR_INVALID;
1987 * Paint the window in response to a WM_PAINT message.
1989 void term_paint (Context ctx, int l, int t, int r, int b) {
1990 int i, j, left, top, right, bottom;
1992 left = l / font_width;
1993 right = (r - 1) / font_width;
1994 top = t / font_height;
1995 bottom = (b - 1) / font_height;
1996 for (i = top; i <= bottom && i < rows ; i++)
1998 if ( (disptext[i*(cols+1)+cols]&LATTR_MODE) == LATTR_NORM)
1999 for (j = left; j <= right && j < cols ; j++)
2000 disptext[i*(cols+1)+j] = ATTR_INVALID;
2002 for (j = left/2; j <= right/2+1 && j < cols ; j++)
2003 disptext[i*(cols+1)+j] = ATTR_INVALID;
2006 /* This should happen soon enough, also for some reason it sometimes
2007 * fails to actually do anything when re-sizing ... painting the wrong
2009 do_paint (ctx, FALSE);
2014 * Attempt to scroll the scrollback. The second parameter gives the
2015 * position we want to scroll to; the first is +1 to denote that
2016 * this position is relative to the beginning of the scrollback, -1
2017 * to denote it is relative to the end, and 0 to denote that it is
2018 * relative to the current position.
2020 void term_scroll (int rel, int where) {
2021 int n = where * (cols+1);
2023 disptop = (rel < 0 ? scrtop :
2024 rel > 0 ? sbtop : disptop) + n;
2025 if (disptop < sbtop)
2027 if (disptop > scrtop)
2033 static void clipme(unsigned long *top, unsigned long *bottom, char *workbuf) {
2034 char *wbptr; /* where next char goes within workbuf */
2035 int wblen = 0; /* workbuf len */
2036 int buflen; /* amount of memory allocated to workbuf */
2038 if ( workbuf != NULL ) { /* user supplied buffer? */
2039 buflen = -1; /* assume buffer passed in is big enough */
2040 wbptr = workbuf; /* start filling here */
2043 buflen = 0; /* No data is available yet */
2045 while (top < bottom) {
2047 unsigned long *lineend = top - (top-text) % (cols+1) + cols;
2048 unsigned long *nlpos = lineend;
2050 if (!(*nlpos & ATTR_WRAPPED)) {
2051 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > top)
2056 while (top < nlpos && top < bottom)
2058 int ch = (*top & CHAR_MASK);
2059 int set = (*top & CSET_MASK);
2061 /* VT Specials -> ISO8859-1 for Cut&Paste */
2062 static const unsigned char poorman2[] =
2063 "* # HTFFCRLF\xB0 \xB1 NLVT+ + + + + - - - - - + + + + | <=>=PI!=\xA3 \xB7 ";
2065 if (set && !cfg.rawcnp) {
2066 if (set == ATTR_LINEDRW && ch >= 0x60 && ch < 0x7F) {
2068 if ((x = poorman2[2*(ch-0x60)+1]) == ' ')
2070 ch = (x<<8) + poorman2[2*(ch-0x60)];
2075 if (cfg.rawcnp || !!(ch&0xE0)) {
2076 if ( wblen == buflen )
2078 workbuf = srealloc(workbuf, buflen += 100);
2079 wbptr = workbuf + wblen;
2082 *wbptr++ = (unsigned char) ch;
2090 for (i=0; i<sizeof(sel_nl); i++)
2092 if ( wblen == buflen )
2094 workbuf = srealloc(workbuf, buflen += 100);
2095 wbptr = workbuf + wblen;
2098 *wbptr++ = sel_nl[i];
2101 top = lineend + 1; /* start of next line */
2103 write_clip (workbuf, wblen, FALSE); /* transfer to clipboard */
2104 if ( buflen > 0 ) /* indicates we allocated this buffer */
2108 void term_copyall (void) {
2109 clipme(sbtop, cpos, NULL /* dynamic allocation */);
2113 * Spread the selection outwards according to the selection mode.
2115 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
2116 unsigned long *linestart, *lineend;
2120 x = (p - text) % (cols+1);
2122 lineend = linestart + cols;
2127 * In this mode, every character is a separate unit, except
2128 * for runs of spaces at the end of a non-wrapping line.
2130 if (!(linestart[cols] & ATTR_WRAPPED)) {
2131 unsigned long *q = lineend;
2132 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
2137 p = (dir == -1 ? q : lineend - 1);
2142 * In this mode, the units are maximal runs of characters
2143 * whose `wordness' has the same value.
2145 wvalue = wordness[*p & CHAR_MASK];
2147 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
2150 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
2156 * In this mode, every line is a unit.
2158 p = (dir == -1 ? linestart : lineend - 1);
2164 static void sel_spread (void) {
2165 selstart = sel_spread_half (selstart, -1);
2166 selend = sel_spread_half (selend - 1, +1) + 1;
2169 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
2170 unsigned long *selpoint;
2173 if (y>=rows) y = rows-1;
2181 if (x>=cols) x = cols-1;
2183 selpoint = disptop + y * (cols+1);
2184 if ((selpoint[cols]&LATTR_MODE) != LATTR_NORM)
2189 if (b == MB_SELECT && a == MA_CLICK) {
2191 selstate = ABOUT_TO;
2192 selanchor = selpoint;
2194 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
2196 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
2197 selstate = DRAGGING;
2198 selstart = selanchor = selpoint;
2199 selend = selstart + 1;
2201 } else if ((b == MB_SELECT && a == MA_DRAG) ||
2202 (b == MB_EXTEND && a != MA_RELEASE)) {
2203 if (selstate == ABOUT_TO && selanchor == selpoint)
2205 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
2206 if (selpoint-selstart < (selend-selstart)/2)
2207 selanchor = selend - 1;
2209 selanchor = selstart;
2210 selstate = DRAGGING;
2212 if (selstate != ABOUT_TO && selstate != DRAGGING)
2213 selanchor = selpoint;
2214 selstate = DRAGGING;
2215 if (selpoint < selanchor) {
2216 selstart = selpoint;
2217 selend = selanchor + 1;
2219 selstart = selanchor;
2220 selend = selpoint + 1;
2223 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
2224 if (selstate == DRAGGING) {
2226 * We've completed a selection. We now transfer the
2227 * data to the clipboard.
2229 clipme(selstart, selend, selspace);
2230 selstate = SELECTED;
2232 selstate = NO_SELECTION;
2233 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
2237 get_clip((void **) &data, &len);
2241 if (paste_buffer) sfree(paste_buffer);
2242 paste_pos = paste_hold = paste_len = 0;
2243 paste_buffer = smalloc(len);
2246 while (p < data+len) {
2247 while (p < data+len &&
2248 !(p <= data+len-sizeof(sel_nl) &&
2249 !memcmp(p, sel_nl, sizeof(sel_nl))))
2257 c=xlat_kbd2tty(q[i]);
2258 paste_buffer[paste_len++] = c;
2262 if (p <= data+len-sizeof(sel_nl) &&
2263 !memcmp(p, sel_nl, sizeof(sel_nl))) {
2264 paste_buffer[paste_len++] = '\r';
2265 p += sizeof(sel_nl);
2270 /* Assume a small paste will be OK in one go. */
2271 if (paste_len<256) {
2272 ldisc_send (paste_buffer, paste_len);
2273 if (paste_buffer) sfree(paste_buffer);
2275 paste_pos = paste_hold = paste_len = 0;
2278 get_clip(NULL, NULL);
2284 void term_nopaste() {
2285 if(paste_len == 0) return;
2286 sfree(paste_buffer);
2292 static long last_paste = 0;
2293 long now, paste_diff;
2295 if(paste_len == 0) return;
2297 /* Don't wait forever to paste */
2299 now = GetTickCount();
2300 paste_diff = now-last_paste;
2301 if (paste_diff>=0 && paste_diff<450)
2306 while(paste_pos<paste_len)
2309 while (n + paste_pos < paste_len) {
2310 if (paste_buffer[paste_pos + n++] == '\r')
2313 ldisc_send (paste_buffer+paste_pos, n);
2316 if (paste_pos < paste_len) {
2321 sfree(paste_buffer);
2326 static void deselect (void) {
2327 selstate = NO_SELECTION;
2328 selstart = selend = scrtop;
2331 void term_deselect (void) {
2336 int term_ldisc(int option) {
2337 if (option == LD_ECHO) return term_echoing;
2338 if (option == LD_EDIT) return term_editing;
2343 * from_backend(), to get data from the backend for the terminal.
2345 void from_backend(int is_stderr, char *data, int len) {
2347 if (inbuf_head >= INBUF_SIZE)
2349 inbuf[inbuf_head++] = *data++;
2354 * Log session traffic.
2356 void logtraffic(unsigned char c, int logmode) {
2357 if (cfg.logtype > 0) {
2358 if (cfg.logtype == logmode) {
2359 /* deferred open file from pgm start? */
2360 if (!lgfp) logfopen();
2361 if (lgfp) fputc (c, lgfp);
2366 /* open log file append/overwrite mode */
2367 void logfopen(void) {
2375 sprintf (writemod, "wb"); /* default to rewrite */
2376 lgfp = fopen(cfg.logfilename, "r"); /* file already present? */
2380 i = askappend(cfg.logfilename);
2382 writemod[0] = 'a'; /* set append mode */
2383 else if (i == 0) { /* cancelled */
2385 cfg.logtype = 0; /* disable logging */
2390 lgfp = fopen(cfg.logfilename, writemod);
2391 if (lgfp) { /* enter into event log */
2392 sprintf(buf, "%s session log (%s mode) to file : ",
2393 (writemod[0] == 'a') ? "Appending" : "Writing new",
2394 (cfg.logtype == LGTYP_ASCII ? "ASCII" :
2395 cfg.logtype == LGTYP_DEBUG ? "raw" : "<ukwn>") );
2396 /* Make sure we do not exceed the output buffer size */
2397 strncat (buf, cfg.logfilename, 128);
2398 buf[strlen(buf)] = '\0';
2401 /* --- write header line iinto log file */
2402 fputs ("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp);
2405 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", tm);
2407 fputs (" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp);
2411 void logfclose (void) {
2412 if (lgfp) { fclose(lgfp); lgfp = NULL; }