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();
850 debug(("beep received at %ld, last was %ld, nbeeps=%d\n", ticks, lastbeep, nbeeps));
852 if (!beep_overloaded) {
853 newbeep = smalloc(sizeof(struct beeptime));
854 newbeep->ticks = ticks;
855 newbeep->next = NULL;
859 beeptail->next = newbeep;
865 * Throw out any beeps that happened more than
869 beephead->ticks < ticks - cfg.bellovl_t*1000) {
870 struct beeptime *tmp = beephead;
871 beephead = tmp->next;
872 debug(("throwing out beep received at %ld\n", tmp->ticks));
879 if (cfg.bellovl && beep_overloaded &&
880 ticks-lastbeep >= cfg.bellovl_s * 1000) {
882 * If we're currently overloaded and the
883 * last beep was more than s seconds ago,
884 * leave overload mode.
886 debug(("silence reigns, leaving overload mode\n"));
887 beep_overloaded = FALSE;
888 } else if (cfg.bellovl && !beep_overloaded &&
889 nbeeps >= cfg.bellovl_n) {
891 * Now, if we have n or more beeps
892 * remaining in the queue, go into overload
895 debug(("%d beeps between times %ld and %ld, overload!\n",
896 nbeeps, beephead->ticks, ticks));
897 beep_overloaded = TRUE;
902 * Perform an actual beep if we're not overloaded.
904 if ((!cfg.bellovl || !beep_overloaded) && cfg.beep != 0) {
905 debug(("not overloaded; performing a beep\n"));
908 else if(cfg.beep == 2) {
910 vbell_timeout = ticks + VBELL_TIMEOUT;
918 if (curs_x == 0 && curs_y == 0)
920 else if (curs_x == 0 && curs_y > 0)
921 curs_x = cols-1, curs_y--;
927 seen_disp_event = TRUE;
930 compatibility(VT100);
934 compatibility(VT100);
939 termstate = VT52_ESC;
941 compatibility(ANSIMIN);
942 termstate = SEEN_ESC;
946 compatibility(VT220);
947 termstate = SEEN_CSI;
949 esc_args[0] = ARG_DEFAULT;
953 compatibility(VT220);
954 termstate = SEEN_OSC;
961 seen_disp_event = TRUE;
963 logtraffic((unsigned char)c,LGTYP_ASCII);
967 compatibility(VT100);
969 if (curs_y == marg_b)
970 scroll (marg_t, marg_b, 1, TRUE);
971 else if (curs_y < rows-1)
979 logtraffic((unsigned char)c,LGTYP_ASCII);
983 unsigned long *old_cpos = cpos;
984 unsigned long *p = scrtop + curs_y * (cols+1) + cols;
988 } while (curs_x < cols-1 && !tabs[curs_x]);
990 if ((*p & LATTR_MODE) != LATTR_NORM)
992 if (curs_x >= cols/2)
1002 check_selection (old_cpos, cpos);
1004 seen_disp_event = TRUE;
1006 case '\177': /* Destructive backspace
1007 This does nothing on a real VT100 */
1008 compatibility(OTHER);
1009 if (curs_x && !wrapnext) curs_x--;
1012 *cpos = (' ' | curr_attr | ATTR_ASCII);
1016 else switch (termstate) {
1018 /* Only graphic characters get this far, ctrls are stripped above */
1019 if (wrapnext && wrap) {
1020 cpos[1] |= ATTR_WRAPPED;
1021 if (curs_y == marg_b)
1022 scroll (marg_t, marg_b, 1, TRUE);
1023 else if (curs_y < rows-1)
1031 if (selstate != NO_SELECTION)
1032 check_selection (cpos, cpos+1);
1033 switch (cset_attr[cset]) {
1034 /* Linedraw characters are different from 'ESC ( B' only
1035 * for a small range, for ones outside that range make sure
1036 * we use the same font as well as the same encoding.
1039 if (c<0x5f || c>0x7F)
1040 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
1043 *cpos++ = ' ' | curr_attr | ATTR_ASCII;
1045 *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW;
1048 /* If UK-ASCII, make the '#' a LineDraw Pound */
1050 *cpos++ = '}' | curr_attr | ATTR_LINEDRW;
1055 *cpos = xlat_tty2scr((unsigned char)c) | curr_attr |
1056 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
1057 logtraffic((unsigned char)c, LGTYP_ASCII);
1062 if (curs_x == cols) {
1067 seen_disp_event = 1;
1071 termstate = TOPLEVEL;
1075 * This state is virtually identical to SEEN_ESC, with the
1076 * exception that we have an OSC sequence in the pipeline,
1077 * and _if_ we see a backslash, we process it.
1081 termstate = TOPLEVEL;
1084 /* else fall through */
1086 termstate = TOPLEVEL;
1088 case ' ': /* some weird sequence? */
1089 compatibility(VT220);
1090 termstate = IGNORE_NEXT;
1092 case '[': /* enter CSI mode */
1093 termstate = SEEN_CSI;
1095 esc_args[0] = ARG_DEFAULT;
1098 case ']': /* xterm escape sequences */
1099 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1100 compatibility(OTHER);
1101 termstate = SEEN_OSC;
1104 case '(': /* should set GL */
1105 compatibility(VT100);
1108 case ')': /* should set GR */
1109 compatibility(VT100);
1112 case '7': /* save cursor */
1113 compatibility(VT100);
1116 case '8': /* restore cursor */
1117 compatibility(VT100);
1118 save_cursor (FALSE);
1119 seen_disp_event = TRUE;
1122 compatibility(VT100);
1123 app_keypad_keys = TRUE;
1126 compatibility(VT100);
1127 app_keypad_keys = FALSE;
1129 case 'D': /* exactly equivalent to LF */
1130 compatibility(VT100);
1131 if (curs_y == marg_b)
1132 scroll (marg_t, marg_b, 1, TRUE);
1133 else if (curs_y < rows-1)
1137 seen_disp_event = TRUE;
1139 case 'E': /* exactly equivalent to CR-LF */
1140 compatibility(VT100);
1142 if (curs_y == marg_b)
1143 scroll (marg_t, marg_b, 1, TRUE);
1144 else if (curs_y < rows-1)
1148 seen_disp_event = TRUE;
1150 case 'M': /* reverse index - backwards LF */
1151 compatibility(VT100);
1152 if (curs_y == marg_t)
1153 scroll (marg_t, marg_b, -1, TRUE);
1154 else if (curs_y > 0)
1158 seen_disp_event = TRUE;
1160 case 'Z': /* terminal type query */
1161 compatibility(VT100);
1162 ldisc_send (id_string, strlen(id_string));
1164 case 'c': /* restore power-on settings */
1165 compatibility(VT100);
1168 request_resize (80, rows, 1);
1173 seen_disp_event = TRUE;
1175 case '#': /* ESC # 8 fills screen with Es :-) */
1176 compatibility(VT100);
1177 termstate = SEEN_ESCHASH;
1179 case 'H': /* set a tab */
1180 compatibility(VT100);
1181 tabs[curs_x] = TRUE;
1186 termstate = TOPLEVEL; /* default */
1189 if (esc_nargs <= ARGS_MAX) {
1190 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
1191 esc_args[esc_nargs-1] = 0;
1192 esc_args[esc_nargs-1] =
1193 10 * esc_args[esc_nargs-1] + c - '0';
1195 termstate = SEEN_CSI;
1199 if (++esc_nargs <= ARGS_MAX)
1200 esc_args[esc_nargs-1] = ARG_DEFAULT;
1201 termstate = SEEN_CSI;
1205 if( esc_query ) esc_query = -1;
1206 else if( c == '?' ) esc_query = TRUE;
1208 termstate = SEEN_CSI;
1210 else switch (ANSI(c,esc_query)) {
1211 case 'A': /* move up N lines */
1212 move (curs_x, curs_y - def(esc_args[0], 1), 1);
1213 seen_disp_event = TRUE;
1215 case 'e': /* move down N lines */
1216 compatibility(ANSI);
1218 move (curs_x, curs_y + def(esc_args[0], 1), 1);
1219 seen_disp_event = TRUE;
1221 case 'a': /* move right N cols */
1222 compatibility(ANSI);
1224 move (curs_x + def(esc_args[0], 1), curs_y, 1);
1225 seen_disp_event = TRUE;
1227 case 'D': /* move left N cols */
1228 move (curs_x - def(esc_args[0], 1), curs_y, 1);
1229 seen_disp_event = TRUE;
1231 case 'E': /* move down N lines and CR */
1232 compatibility(ANSI);
1233 move (0, curs_y + def(esc_args[0], 1), 1);
1234 seen_disp_event = TRUE;
1236 case 'F': /* move up N lines and CR */
1237 compatibility(ANSI);
1238 move (0, curs_y - def(esc_args[0], 1), 1);
1239 seen_disp_event = TRUE;
1241 case 'G': case '`': /* set horizontal posn */
1242 compatibility(ANSI);
1243 move (def(esc_args[0], 1) - 1, curs_y, 0);
1244 seen_disp_event = TRUE;
1246 case 'd': /* set vertical posn */
1247 compatibility(ANSI);
1248 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1250 seen_disp_event = TRUE;
1252 case 'H': case 'f': /* set horz and vert posns at once */
1254 esc_args[1] = ARG_DEFAULT;
1255 move (def(esc_args[1], 1) - 1,
1256 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
1258 seen_disp_event = TRUE;
1260 case 'J': /* erase screen or parts of it */
1262 unsigned int i = def(esc_args[0], 0) + 1;
1265 erase_lots(FALSE, !!(i & 2), !!(i & 1));
1268 seen_disp_event = TRUE;
1270 case 'K': /* erase line or parts of it */
1272 unsigned int i = def(esc_args[0], 0) + 1;
1275 erase_lots(TRUE, !!(i & 2), !!(i & 1));
1277 seen_disp_event = TRUE;
1279 case 'L': /* insert lines */
1280 compatibility(VT102);
1281 if (curs_y <= marg_b)
1282 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
1283 seen_disp_event = TRUE;
1285 case 'M': /* delete lines */
1286 compatibility(VT102);
1287 if (curs_y <= marg_b)
1288 scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
1289 seen_disp_event = TRUE;
1291 case '@': /* insert chars */
1292 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1293 compatibility(VT102);
1294 insch (def(esc_args[0], 1));
1295 seen_disp_event = TRUE;
1297 case 'P': /* delete chars */
1298 compatibility(VT102);
1299 insch (-def(esc_args[0], 1));
1300 seen_disp_event = TRUE;
1302 case 'c': /* terminal type query */
1303 compatibility(VT100);
1304 /* This is the response for a VT102 */
1305 ldisc_send (id_string, strlen(id_string));
1307 case 'n': /* cursor position query */
1308 if (esc_args[0] == 6) {
1310 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
1311 ldisc_send (buf, strlen(buf));
1313 else if (esc_args[0] == 5) {
1314 ldisc_send ("\033[0n", 4);
1317 case 'h': /* toggle modes to high */
1319 compatibility(VT100);
1322 for (i=0; i<esc_nargs; i++)
1323 toggle_mode (esc_args[i], esc_query, TRUE);
1326 case 'l': /* toggle modes to low */
1328 compatibility(VT100);
1331 for (i=0; i<esc_nargs; i++)
1332 toggle_mode (esc_args[i], esc_query, FALSE);
1335 case 'g': /* clear tabs */
1336 compatibility(VT100);
1337 if (esc_nargs == 1) {
1338 if (esc_args[0] == 0) {
1339 tabs[curs_x] = FALSE;
1340 } else if (esc_args[0] == 3) {
1342 for (i = 0; i < cols; i++)
1347 case 'r': /* set scroll margins */
1348 compatibility(VT100);
1349 if (esc_nargs <= 2) {
1351 top = def(esc_args[0], 1) - 1;
1352 bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
1353 def(esc_args[1], rows)) - 1;
1356 /* VTTEST Bug 9 - if region is less than 2 lines
1357 * don't change region.
1364 * I used to think the cursor should be
1365 * placed at the top of the newly marginned
1366 * area. Apparently not: VMS TPU falls over
1369 * Well actually it should for Origin mode - RDB
1371 curs_y = (dec_om ? marg_t : 0);
1373 seen_disp_event = TRUE;
1377 case 'm': /* set graphics rendition */
1380 * A VT100 without the AVO only had one attribute, either
1381 * underline or reverse video depending on the cursor type,
1382 * this was selected by CSI 7m.
1385 * This is DIM on the VT100-AVO and VT102
1387 * This is BLINK on the VT100-AVO and VT102+
1389 * This is INVIS on the VT100-AVO and VT102
1391 * This like 22 disables BOLD, DIM and INVIS
1393 * The ANSI colours appear on any terminal that has colour
1394 * (obviously) but the interaction between sgr0 and the
1395 * colours varies but is usually related to the background
1396 * colour erase item.
1397 * The interaction between colour attributes and the mono
1398 * ones is also very implementation dependent.
1400 * The 39 and 49 attributes are likely to be unimplemented.
1403 for (i=0; i<esc_nargs; i++) {
1404 switch (def(esc_args[i], 0)) {
1405 case 0: /* restore defaults */
1406 curr_attr = ATTR_DEFAULT; break;
1407 case 1: /* enable bold */
1408 compatibility(VT100AVO);
1409 curr_attr |= ATTR_BOLD; break;
1410 case 21: /* (enable double underline) */
1411 compatibility(OTHER);
1412 case 4: /* enable underline */
1413 compatibility(VT100AVO);
1414 curr_attr |= ATTR_UNDER; break;
1415 case 5: /* enable blink */
1416 compatibility(VT100AVO);
1417 curr_attr |= ATTR_BLINK; break;
1418 case 7: /* enable reverse video */
1419 curr_attr |= ATTR_REVERSE; break;
1420 case 22: /* disable bold */
1421 compatibility2(OTHER,VT220);
1422 curr_attr &= ~ATTR_BOLD; break;
1423 case 24: /* disable underline */
1424 compatibility2(OTHER,VT220);
1425 curr_attr &= ~ATTR_UNDER; break;
1426 case 25: /* disable blink */
1427 compatibility2(OTHER,VT220);
1428 curr_attr &= ~ATTR_BLINK; break;
1429 case 27: /* disable reverse video */
1430 compatibility2(OTHER,VT220);
1431 curr_attr &= ~ATTR_REVERSE; break;
1432 case 30: case 31: case 32: case 33:
1433 case 34: case 35: case 36: case 37:
1435 curr_attr &= ~ATTR_FGMASK;
1436 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
1438 case 39: /* default-foreground */
1439 curr_attr &= ~ATTR_FGMASK;
1440 curr_attr |= ATTR_DEFFG;
1442 case 40: case 41: case 42: case 43:
1443 case 44: case 45: case 46: case 47:
1445 curr_attr &= ~ATTR_BGMASK;
1446 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
1448 case 49: /* default-background */
1449 curr_attr &= ~ATTR_BGMASK;
1450 curr_attr |= ATTR_DEFBG;
1457 (curr_attr&(ATTR_FGMASK|ATTR_BGMASK|ATTR_BLINK))
1461 case 's': /* save cursor */
1464 case 'u': /* restore cursor */
1465 save_cursor (FALSE);
1466 seen_disp_event = TRUE;
1468 case 't': /* set page size - ie window height */
1470 * VT340/VT420 sequence DECSLPP, DEC only allows values
1471 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1472 * illegal values (eg first arg 1..9) for window changing
1475 compatibility(VT340TEXT);
1476 if (esc_nargs<=1 && (esc_args[0]<1 || esc_args[0]>=24)) {
1477 request_resize (cols, def(esc_args[0], 24), 0);
1481 case ANSI('|', '*'):
1482 /* VT420 sequence DECSNLS
1483 * Set number of lines on screen
1484 * VT420 uses VGA like hardware and can support any size in
1485 * reasonable range (24..49 AIUI) with no default specified.
1487 compatibility(VT420);
1488 if (esc_nargs==1 && esc_args[0]>0) {
1489 request_resize (cols, def(esc_args[0], cfg.height), 0);
1493 case ANSI('|', '$'):
1494 /* VT340/VT420 sequence DECSCPP
1495 * Set number of columns per page
1496 * Docs imply range is only 80 or 132, but I'll allow any.
1498 compatibility(VT340TEXT);
1500 request_resize (def(esc_args[0], cfg.width), rows, 0);
1504 case 'X': /* write N spaces w/o moving cursor */
1505 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1506 compatibility(ANSIMIN);
1508 int n = def(esc_args[0], 1);
1509 unsigned long *p = cpos;
1510 if (n > cols - curs_x)
1512 check_selection (cpos, cpos+n);
1515 seen_disp_event = TRUE;
1518 case 'x': /* report terminal characteristics */
1519 compatibility(VT100);
1522 int i = def(esc_args[0], 0);
1523 if (i == 0 || i == 1) {
1524 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1526 ldisc_send (buf, 20);
1531 compatibility(OTHER);
1532 use_bce = (esc_args[0]<=0);
1533 erase_char = ERASE_CHAR;
1535 erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
1538 compatibility(OTHER);
1539 blink_is_real = (esc_args[0]>=1);
1542 /* Allow the host to make this emulator a 'perfect' VT102.
1543 * This first appeared in the VT220, but we do need to get
1544 * back to PuTTY mode so I won't check it.
1546 * The arg in 40..42 are a PuTTY extension.
1547 * The 2nd arg, 8bit vs 7bit is not checked.
1549 * Setting VT102 mode should also change the Fkeys to
1550 * generate PF* codes as a real VT102 has no Fkeys.
1551 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1554 * Note ESC c will NOT change this!
1557 switch (esc_args[0]) {
1558 case 61: compatibility_level &= ~TM_VTXXX;
1559 compatibility_level |= TM_VT102; break;
1560 case 62: compatibility_level &= ~TM_VTXXX;
1561 compatibility_level |= TM_VT220; break;
1563 default: if( esc_args[0] > 60 && esc_args[0] < 70 )
1564 compatibility_level |= TM_VTXXX;
1567 case 40: compatibility_level &= TM_VTXXX; break;
1568 case 41: compatibility_level = TM_PUTTY; break;
1569 case 42: compatibility_level = TM_SCOANSI; break;
1572 compatibility_level = TM_PUTTY; break;
1576 /* Change the response to CSI c */
1577 if (esc_args[0] == 50) {
1580 strcpy(id_string, "\033[?");
1581 for (i=1; i<esc_nargs; i++) {
1582 if (i!=1) strcat(id_string, ";");
1583 sprintf(lbuf, "%d", esc_args[i]);
1584 strcat(id_string, lbuf);
1586 strcat(id_string, "c");
1590 /* Is this a good idea ?
1591 * Well we should do a soft reset at this point ...
1593 if (!has_compat(VT420) && has_compat(VT100)) {
1594 if (reset_132) request_resize (132, 24, 1);
1595 else request_resize ( 80, 24, 1);
1603 /* VT100 only here, checked above */
1606 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1609 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1612 default: /* specifically, 'B' */
1613 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1616 if( !has_compat(VT220) || c != '%' )
1617 termstate = TOPLEVEL;
1622 case 'P': /* Linux palette sequence */
1623 termstate = SEEN_OSC_P;
1626 case 'R': /* Linux palette reset */
1629 termstate = TOPLEVEL;
1631 case 'W': /* word-set */
1632 termstate = SEEN_OSC_W;
1635 case '0': case '1': case '2': case '3': case '4':
1636 case '5': case '6': case '7': case '8': case '9':
1637 esc_args[0] = 10 * esc_args[0] + c - '0';
1641 * Grotty hack to support xterm and DECterm title
1642 * sequences concurrently.
1644 if (esc_args[0] == 2) {
1648 /* else fall through */
1650 termstate = OSC_STRING;
1656 * This OSC stuff is EVIL. It takes just one character to get into
1657 * sysline mode and it's not initially obvious how to get out.
1658 * So I've added CR and LF as string aborts.
1659 * This shouldn't effect compatibility as I believe embedded
1660 * control characters are supposed to be interpreted (maybe?)
1661 * and they don't display anything useful anyway.
1665 if (c == '\n' || c == '\r') {
1666 termstate = TOPLEVEL;
1667 } else if (c == 0234 || c == '\007' ) {
1669 * These characters terminate the string; ST and BEL
1670 * terminate the sequence and trigger instant
1671 * processing of it, whereas ESC goes back to SEEN_ESC
1672 * mode unless it is followed by \, in which case it is
1673 * synonymous with ST in the first place.
1676 termstate = TOPLEVEL;
1677 } else if (c == '\033')
1678 termstate = OSC_MAYBE_ST;
1679 else if (osc_strlen < OSC_STR_MAX)
1680 osc_string[osc_strlen++] = c;
1684 int max = (osc_strlen == 0 ? 21 : 16);
1686 if (c >= '0' && c <= '9')
1688 else if (c >= 'A' && c <= 'A'+max-10)
1690 else if (c >= 'a' && c <= 'a'+max-10)
1693 termstate = TOPLEVEL;
1694 osc_string[osc_strlen++] = val;
1695 if (osc_strlen >= 7) {
1696 palette_set (osc_string[0],
1697 osc_string[1] * 16 + osc_string[2],
1698 osc_string[3] * 16 + osc_string[4],
1699 osc_string[5] * 16 + osc_string[6]);
1701 termstate = TOPLEVEL;
1707 case '0': case '1': case '2': case '3': case '4':
1708 case '5': case '6': case '7': case '8': case '9':
1709 esc_args[0] = 10 * esc_args[0] + c - '0';
1712 termstate = OSC_STRING;
1719 unsigned long nlattr;
1725 n = rows * (cols+1);
1727 *p++ = ATTR_DEFAULT | 'E';
1729 seen_disp_event = TRUE;
1730 check_selection (scrtop, scrtop + rows * (cols+1));
1733 case '3': nlattr = LATTR_TOP; if(0) {
1734 case '4': nlattr = LATTR_BOT; } if(0) {
1735 case '5': nlattr = LATTR_NORM; } if(0) {
1736 case '6': nlattr = LATTR_WIDE; }
1738 p = scrtop + curs_y * (cols+1) + cols;
1743 termstate = TOPLEVEL;
1746 termstate = TOPLEVEL;
1747 seen_disp_event = TRUE;
1750 move (curs_x, curs_y - 1, 1);
1753 move (curs_x, curs_y + 1, 1);
1756 move (curs_x + 1, curs_y, 1);
1759 move (curs_x - 1, curs_y, 1);
1762 cset_attr[cset=0] = ATTR_LINEDRW;
1765 cset_attr[cset=0] = ATTR_ASCII;
1772 scroll (0, rows-1, -1, TRUE);
1773 else if (curs_y > 0)
1779 erase_lots(FALSE, FALSE, TRUE);
1783 erase_lots(TRUE, FALSE, TRUE);
1786 /* XXX Print cursor line */
1789 /* XXX Start controller mode */
1792 /* XXX Stop controller mode */
1795 termstate = VT52_Y1;
1798 ldisc_send ("\033/Z", 3);
1801 app_keypad_keys = TRUE;
1804 app_keypad_keys = FALSE;
1807 /* XXX This should switch to VT100 mode not current or default
1808 * VT mode. But this will only have effect in a VT220+
1814 /* XXX Enter auto print mode */
1817 /* XXX Exit auto print mode */
1820 /* XXX Print screen */
1825 termstate = VT52_Y2;
1826 move(curs_x, c-' ', 0);
1829 termstate = TOPLEVEL;
1830 move(c-' ', curs_y, 0);
1833 if (selstate != NO_SELECTION)
1834 check_selection (cpos, cpos+1);
1840 * Compare two lines to determine whether they are sufficiently
1841 * alike to scroll-optimise one to the other. Return the degree of
1844 static int linecmp (unsigned long *a, unsigned long *b) {
1847 for (i=n=0; i < cols; i++)
1848 n += (*a++ == *b++);
1853 * Given a context, update the window. Out of paranoia, we don't
1854 * allow WM_PAINT responses to do scrolling optimisations.
1856 static void do_paint (Context ctx, int may_optimise){
1857 int i, j, start, our_curs_y;
1858 unsigned long attr, rv, cursor;
1863 * Check the visual bell state.
1866 ticks = GetTickCount();
1867 if (ticks - vbell_timeout >= 0)
1872 * screen array, disptop, scrtop,
1874 * cfg.blinkpc, blink_is_real, tblinker,
1875 * curs_y, curs_x, blinker, cfg.blink_cur, cursor_on, has_focus
1879 if (blinker || !cfg.blink_cur)
1880 cursor = ATTR_ACTCURS;
1885 cursor = ATTR_PASCURS;
1887 cursor |= ATTR_RIGHTCURS;
1891 rv = (!rvideo ^ !in_vbell ? ATTR_REVERSE : 0);
1892 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1894 for (i=0; i<rows; i++) {
1895 int idx = i*(cols+1);
1896 int lattr = (disptop[idx+cols] & LATTR_MODE);
1897 for (j=0; j<=cols; j++,idx++) {
1898 unsigned long *d = disptop+idx;
1899 wanttext[idx] = lattr | (((*d &~ ATTR_WRAPPED) ^ rv
1900 ^ (selstart <= d && d < selend ?
1901 ATTR_REVERSE : 0)) |
1902 (i==our_curs_y && j==curs_x ? cursor : 0));
1903 if (blink_is_real) {
1904 if (has_focus && tblinker && (wanttext[idx]&ATTR_BLINK) )
1906 wanttext[idx] &= ATTR_MASK;
1907 wanttext[idx] += ' ';
1909 wanttext[idx] &= ~ATTR_BLINK;
1915 * We would perform scrolling optimisations in here, if they
1916 * didn't have a nasty tendency to cause the whole sodding
1917 * program to hang for a second at speed-critical moments.
1918 * We'll leave it well alone...
1921 for (i=0; i<rows; i++) {
1922 int idx = i*(cols+1);
1923 int lattr = (wanttext[idx+cols] & LATTR_MODE);
1925 for (j=0; j<=cols; j++,idx++) {
1926 unsigned long t = wanttext[idx];
1927 int needs_update = (j < cols && t != disptext[idx]);
1928 int keep_going = (start != -1 && needs_update &&
1929 (t & ATTR_MASK) == attr &&
1930 j-start < sizeof(ch));
1931 if (start != -1 && !keep_going) {
1932 do_text (ctx, start, i, ch, j-start, attr, lattr);
1938 attr = t & ATTR_MASK;
1940 ch[j-start] = (char) (t & CHAR_MASK);
1948 * Flick the switch that says if blinking things should be shown or hidden.
1951 void term_blink(int flg) {
1952 static long last_blink = 0;
1953 static long last_tblink = 0;
1954 long now, blink_diff;
1956 now = GetTickCount();
1957 blink_diff = now-last_tblink;
1959 /* Make sure the text blinks no more than 2Hz */
1960 if (blink_diff<0 || blink_diff>450)
1963 tblinker = !tblinker;
1972 blink_diff = now-last_blink;
1974 /* Make sure the cursor blinks no faster than GetCaretBlinkTime() */
1975 if (blink_diff>=0 && blink_diff<(long)GetCaretBlinkTime())
1983 * Invalidate the whole screen so it will be repainted in full.
1985 void term_invalidate(void) {
1988 for (i=0; i<rows*(cols+1); i++)
1989 disptext[i] = ATTR_INVALID;
1993 * Paint the window in response to a WM_PAINT message.
1995 void term_paint (Context ctx, int l, int t, int r, int b) {
1996 int i, j, left, top, right, bottom;
1998 left = l / font_width;
1999 right = (r - 1) / font_width;
2000 top = t / font_height;
2001 bottom = (b - 1) / font_height;
2002 for (i = top; i <= bottom && i < rows ; i++)
2004 if ( (disptext[i*(cols+1)+cols]&LATTR_MODE) == LATTR_NORM)
2005 for (j = left; j <= right && j < cols ; j++)
2006 disptext[i*(cols+1)+j] = ATTR_INVALID;
2008 for (j = left/2; j <= right/2+1 && j < cols ; j++)
2009 disptext[i*(cols+1)+j] = ATTR_INVALID;
2012 /* This should happen soon enough, also for some reason it sometimes
2013 * fails to actually do anything when re-sizing ... painting the wrong
2015 do_paint (ctx, FALSE);
2020 * Attempt to scroll the scrollback. The second parameter gives the
2021 * position we want to scroll to; the first is +1 to denote that
2022 * this position is relative to the beginning of the scrollback, -1
2023 * to denote it is relative to the end, and 0 to denote that it is
2024 * relative to the current position.
2026 void term_scroll (int rel, int where) {
2027 int n = where * (cols+1);
2029 disptop = (rel < 0 ? scrtop :
2030 rel > 0 ? sbtop : disptop) + n;
2031 if (disptop < sbtop)
2033 if (disptop > scrtop)
2039 static void clipme(unsigned long *top, unsigned long *bottom, char *workbuf) {
2040 char *wbptr; /* where next char goes within workbuf */
2041 int wblen = 0; /* workbuf len */
2042 int buflen; /* amount of memory allocated to workbuf */
2044 if ( workbuf != NULL ) { /* user supplied buffer? */
2045 buflen = -1; /* assume buffer passed in is big enough */
2046 wbptr = workbuf; /* start filling here */
2049 buflen = 0; /* No data is available yet */
2051 while (top < bottom) {
2053 unsigned long *lineend = top - (top-text) % (cols+1) + cols;
2054 unsigned long *nlpos = lineend;
2056 if (!(*nlpos & ATTR_WRAPPED)) {
2057 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > top)
2062 while (top < nlpos && top < bottom)
2064 int ch = (*top & CHAR_MASK);
2065 int set = (*top & CSET_MASK);
2067 /* VT Specials -> ISO8859-1 for Cut&Paste */
2068 static const unsigned char poorman2[] =
2069 "* # HTFFCRLF\xB0 \xB1 NLVT+ + + + + - - - - - + + + + | <=>=PI!=\xA3 \xB7 ";
2071 if (set && !cfg.rawcnp) {
2072 if (set == ATTR_LINEDRW && ch >= 0x60 && ch < 0x7F) {
2074 if ((x = poorman2[2*(ch-0x60)+1]) == ' ')
2076 ch = (x<<8) + poorman2[2*(ch-0x60)];
2081 if (cfg.rawcnp || !!(ch&0xE0)) {
2082 if ( wblen == buflen )
2084 workbuf = srealloc(workbuf, buflen += 100);
2085 wbptr = workbuf + wblen;
2088 *wbptr++ = (unsigned char) ch;
2096 for (i=0; i<sizeof(sel_nl); i++)
2098 if ( wblen == buflen )
2100 workbuf = srealloc(workbuf, buflen += 100);
2101 wbptr = workbuf + wblen;
2104 *wbptr++ = sel_nl[i];
2107 top = lineend + 1; /* start of next line */
2109 write_clip (workbuf, wblen, FALSE); /* transfer to clipboard */
2110 if ( buflen > 0 ) /* indicates we allocated this buffer */
2114 void term_copyall (void) {
2115 clipme(sbtop, cpos, NULL /* dynamic allocation */);
2119 * Spread the selection outwards according to the selection mode.
2121 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
2122 unsigned long *linestart, *lineend;
2126 x = (p - text) % (cols+1);
2128 lineend = linestart + cols;
2133 * In this mode, every character is a separate unit, except
2134 * for runs of spaces at the end of a non-wrapping line.
2136 if (!(linestart[cols] & ATTR_WRAPPED)) {
2137 unsigned long *q = lineend;
2138 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
2143 p = (dir == -1 ? q : lineend - 1);
2148 * In this mode, the units are maximal runs of characters
2149 * whose `wordness' has the same value.
2151 wvalue = wordness[*p & CHAR_MASK];
2153 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
2156 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
2162 * In this mode, every line is a unit.
2164 p = (dir == -1 ? linestart : lineend - 1);
2170 static void sel_spread (void) {
2171 selstart = sel_spread_half (selstart, -1);
2172 selend = sel_spread_half (selend - 1, +1) + 1;
2175 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
2176 unsigned long *selpoint;
2179 if (y>=rows) y = rows-1;
2187 if (x>=cols) x = cols-1;
2189 selpoint = disptop + y * (cols+1);
2190 if ((selpoint[cols]&LATTR_MODE) != LATTR_NORM)
2195 if (b == MB_SELECT && a == MA_CLICK) {
2197 selstate = ABOUT_TO;
2198 selanchor = selpoint;
2200 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
2202 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
2203 selstate = DRAGGING;
2204 selstart = selanchor = selpoint;
2205 selend = selstart + 1;
2207 } else if ((b == MB_SELECT && a == MA_DRAG) ||
2208 (b == MB_EXTEND && a != MA_RELEASE)) {
2209 if (selstate == ABOUT_TO && selanchor == selpoint)
2211 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
2212 if (selpoint-selstart < (selend-selstart)/2)
2213 selanchor = selend - 1;
2215 selanchor = selstart;
2216 selstate = DRAGGING;
2218 if (selstate != ABOUT_TO && selstate != DRAGGING)
2219 selanchor = selpoint;
2220 selstate = DRAGGING;
2221 if (selpoint < selanchor) {
2222 selstart = selpoint;
2223 selend = selanchor + 1;
2225 selstart = selanchor;
2226 selend = selpoint + 1;
2229 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
2230 if (selstate == DRAGGING) {
2232 * We've completed a selection. We now transfer the
2233 * data to the clipboard.
2235 clipme(selstart, selend, selspace);
2236 selstate = SELECTED;
2238 selstate = NO_SELECTION;
2239 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
2243 get_clip((void **) &data, &len);
2247 if (paste_buffer) sfree(paste_buffer);
2248 paste_pos = paste_hold = paste_len = 0;
2249 paste_buffer = smalloc(len);
2252 while (p < data+len) {
2253 while (p < data+len &&
2254 !(p <= data+len-sizeof(sel_nl) &&
2255 !memcmp(p, sel_nl, sizeof(sel_nl))))
2263 c=xlat_kbd2tty(q[i]);
2264 paste_buffer[paste_len++] = c;
2268 if (p <= data+len-sizeof(sel_nl) &&
2269 !memcmp(p, sel_nl, sizeof(sel_nl))) {
2270 paste_buffer[paste_len++] = '\r';
2271 p += sizeof(sel_nl);
2276 /* Assume a small paste will be OK in one go. */
2277 if (paste_len<256) {
2278 ldisc_send (paste_buffer, paste_len);
2279 if (paste_buffer) sfree(paste_buffer);
2281 paste_pos = paste_hold = paste_len = 0;
2284 get_clip(NULL, NULL);
2290 void term_nopaste() {
2291 if(paste_len == 0) return;
2292 sfree(paste_buffer);
2298 static long last_paste = 0;
2299 long now, paste_diff;
2301 if(paste_len == 0) return;
2303 /* Don't wait forever to paste */
2305 now = GetTickCount();
2306 paste_diff = now-last_paste;
2307 if (paste_diff>=0 && paste_diff<450)
2312 while(paste_pos<paste_len)
2315 while (n + paste_pos < paste_len) {
2316 if (paste_buffer[paste_pos + n++] == '\r')
2319 ldisc_send (paste_buffer+paste_pos, n);
2322 if (paste_pos < paste_len) {
2327 sfree(paste_buffer);
2332 static void deselect (void) {
2333 selstate = NO_SELECTION;
2334 selstart = selend = scrtop;
2337 void term_deselect (void) {
2342 int term_ldisc(int option) {
2343 if (option == LD_ECHO) return term_echoing;
2344 if (option == LD_EDIT) return term_editing;
2349 * from_backend(), to get data from the backend for the terminal.
2351 void from_backend(int is_stderr, char *data, int len) {
2353 if (inbuf_head >= INBUF_SIZE)
2355 inbuf[inbuf_head++] = *data++;
2360 * Log session traffic.
2362 void logtraffic(unsigned char c, int logmode) {
2363 if (cfg.logtype > 0) {
2364 if (cfg.logtype == logmode) {
2365 /* deferred open file from pgm start? */
2366 if (!lgfp) logfopen();
2367 if (lgfp) fputc (c, lgfp);
2372 /* open log file append/overwrite mode */
2373 void logfopen(void) {
2381 sprintf (writemod, "wb"); /* default to rewrite */
2382 lgfp = fopen(cfg.logfilename, "r"); /* file already present? */
2386 i = askappend(cfg.logfilename);
2388 writemod[0] = 'a'; /* set append mode */
2389 else if (i == 0) { /* cancelled */
2391 cfg.logtype = 0; /* disable logging */
2396 lgfp = fopen(cfg.logfilename, writemod);
2397 if (lgfp) { /* enter into event log */
2398 sprintf(buf, "%s session log (%s mode) to file : ",
2399 (writemod[0] == 'a') ? "Appending" : "Writing new",
2400 (cfg.logtype == LGTYP_ASCII ? "ASCII" :
2401 cfg.logtype == LGTYP_DEBUG ? "raw" : "<ukwn>") );
2402 /* Make sure we do not exceed the output buffer size */
2403 strncat (buf, cfg.logfilename, 128);
2404 buf[strlen(buf)] = '\0';
2407 /* --- write header line iinto log file */
2408 fputs ("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp);
2411 strftime(buf, 24, "%Y.%m.%d %H:%M:%S", tm);
2413 fputs (" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp);
2417 void logfclose (void) {
2418 if (lgfp) { fclose(lgfp); lgfp = NULL; }