8 static unsigned long *text; /* buffer of text on terminal screen */
9 static unsigned long *scrtop; /* top of working screen */
10 static unsigned long *disptop; /* top of displayed screen */
11 static unsigned long *sbtop; /* top of scrollback */
12 static unsigned long *cpos; /* cursor position (convenience) */
13 static unsigned long *disptext; /* buffer of text on real screen */
14 static unsigned long *wanttext; /* buffer of text we want on screen */
15 static unsigned long *alttext; /* buffer of text on alt. screen */
17 static unsigned char *selspace; /* buffer for building selections in */
19 #define TSIZE (sizeof(*text))
20 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
22 static unsigned long curr_attr, save_attr;
24 static int curs_x, curs_y; /* cursor */
25 static int save_x, save_y; /* saved cursor position */
26 static int marg_t, marg_b; /* scroll margins */
27 static int dec_om; /* DEC origin mode flag */
28 static int wrap, wrapnext; /* wrap flags */
29 static int insert; /* insert-mode flag */
30 static int cset; /* 0 or 1: which char set */
31 static int save_cset, save_csattr; /* saved with cursor position */
32 static int rvideo; /* global reverse video flag */
34 static unsigned long cset_attr[2];
37 * Saved settings on the alternate screen.
39 static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
40 static int alt_t, alt_b;
43 #define ARGS_MAX 32 /* max # of esc sequence arguments */
44 #define ARG_DEFAULT -1 /* if an arg isn't specified */
45 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
46 static int esc_args[ARGS_MAX];
50 #define OSC_STR_MAX 2048
51 static int osc_strlen;
52 static char osc_string[OSC_STR_MAX+1];
55 static unsigned char *tabs;
60 static int scroll_heuristic;
63 TOPLEVEL, IGNORE_NEXT,
64 SEEN_ESC, SEEN_CSI, SET_GL, SET_GR,
65 SEEN_OSC, SEEN_OSC_P, SEEN_OSC_W, OSC_STRING, OSC_MAYBE_ST,
70 NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
73 SM_CHAR, SM_WORD, SM_LINE
75 static unsigned long *selstart, *selend, *selanchor;
77 static short wordness[256] = {
78 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 */
79 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 */
80 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 */
81 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 */
82 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 */
83 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 */
84 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 */
85 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 */
88 static unsigned char sel_nl[] = SEL_NL;
91 * Internal prototypes.
93 static void do_paint (Context, int);
94 static void erase_lots (int, int, int);
95 static void swap_screen (int);
96 static void update_sbar (void);
97 static void deselect (void);
100 * Set up power-on settings for the terminal.
102 static void power_on(void) {
103 curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
106 alt_b = marg_b = rows - 1;
111 for (i = 0; i < cols; i++)
112 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
114 alt_om = dec_om = cfg.dec_om;
115 alt_wnext = wrapnext = alt_ins = insert = FALSE;
116 alt_wrap = wrap = cfg.wrap_mode;
118 cset_attr[0] = cset_attr[1] = ATTR_ASCII;
120 save_attr = curr_attr = ATTR_DEFAULT;
121 app_cursor_keys = cfg.app_cursor;
122 app_keypad_keys = cfg.app_keypad;
126 for (i = 0; i < 256; i++)
127 wordness[i] = cfg.wordness[i];
131 erase_lots (FALSE, TRUE, TRUE);
133 erase_lots (FALSE, TRUE, TRUE);
138 * Force a screen update.
140 void term_update(void) {
144 do_paint (ctx, TRUE);
147 scroll_heuristic = 0;
152 * Same as power_on(), but an external function.
154 void term_pwron(void) {
163 * Clear the scrollback.
165 void term_clrsb(void) {
166 disptop = sbtop = scrtop;
171 * Initialise the terminal.
173 void term_init(void) {
174 text = sbtop = scrtop = disptop = cpos = NULL;
175 disptext = wanttext = NULL;
185 * Set up the terminal for a given size.
187 void term_size(int newrows, int newcols, int newsavelines) {
188 unsigned long *newtext, *newdisp, *newwant, *newalt;
189 int i, j, crows, ccols;
191 if (newrows == rows && newcols == cols && newsavelines == savelines)
192 return; /* nothing to do */
195 alt_b = marg_b = newrows - 1;
197 newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
198 disptop = newtext + newsavelines*(newcols+1);
199 for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
200 newtext[i] = ERASE_CHAR;
202 crows = rows + (scrtop - sbtop) / (cols+1);
203 if (crows > newrows+newsavelines)
204 crows = newrows+newsavelines;
205 ccols = (cols < newcols ? cols : newcols);
206 for (i=0; i<crows; i++) {
207 int oldidx = (rows + savelines - crows + i) * (cols+1);
208 int newidx = (newrows + newsavelines - crows + i) * (newcols+1);
209 for (j=0; j<ccols; j++)
210 newtext[newidx+j] = text[oldidx+j];
211 newtext[newidx+newcols] =
212 (cols == newcols ? text[oldidx+cols] : 0);
214 sbtop = disptop - (crows - newrows) * (newcols+1);
223 newdisp = smalloc (newrows*(newcols+1)*TSIZE);
224 for (i=0; i<newrows*(newcols+1); i++)
225 newdisp[i] = ATTR_INVALID;
229 newwant = smalloc (newrows*(newcols+1)*TSIZE);
230 for (i=0; i<newrows*(newcols+1); i++)
231 newwant[i] = ATTR_INVALID;
235 newalt = smalloc (newrows*(newcols+1)*TSIZE);
236 for (i=0; i<newrows*(newcols+1); i++)
237 newalt[i] = ERASE_CHAR;
242 selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
244 tabs = srealloc (tabs, newcols*sizeof(*tabs));
247 for (i = (cols > 0 ? cols : 0); i < newcols; i++)
248 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
252 curs_y += newrows - rows;
255 if (curs_y >= newrows)
257 if (curs_x >= newcols)
260 wrapnext = alt_wnext = FALSE;
264 savelines = newsavelines;
275 static void swap_screen (int which) {
279 if (which == alt_which)
284 for (t=0; t<rows*(cols+1); t++) {
285 tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
288 t = curs_x; curs_x = alt_x; alt_x = t;
289 t = curs_y; curs_y = alt_y; alt_y = t;
290 t = marg_t; marg_t = alt_t; alt_t = t;
291 t = marg_b; marg_b = alt_b; alt_b = t;
292 t = dec_om; dec_om = alt_om; alt_om = t;
293 t = wrap; wrap = alt_wrap; alt_wrap = t;
294 t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
295 t = insert; insert = alt_ins; alt_ins = t;
296 t = cset; cset = alt_cset; alt_cset = t;
302 * Retrieve a character from `inbuf'.
304 static int inbuf_getc(void) {
305 if (inbuf_head == inbuf_reap)
309 inbuf_reap = (inbuf_reap+1) & INBUF_MASK;
315 * Update the scroll bar.
317 static void update_sbar(void) {
320 min = (sbtop - text) / (cols+1);
321 set_sbar ((scrtop - text) / (cols+1) + rows - min,
322 (disptop - text) / (cols+1) - min,
327 * Check whether the region bounded by the two pointers intersects
328 * the scroll region, and de-select the on-screen selection if so.
330 static void check_selection (unsigned long *from, unsigned long *to) {
331 if (from < selend && selstart < to)
336 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
337 * for backward.) `sb' is TRUE if the scrolling is permitted to
338 * affect the scrollback buffer.
340 static void scroll (int topline, int botline, int lines, int sb) {
341 unsigned long *scroll_top;
342 int scroll_size, size, i;
344 scroll_top = scrtop + topline*(cols+1);
345 size = (lines < 0 ? -lines : lines) * (cols+1);
346 scroll_size = (botline - topline + 1) * (cols+1) - size;
348 if (lines > 0 && topline == 0 && botline == (rows-1) && sb) {
350 * Since we're going to scroll the whole screen upwards,
351 * let's also affect the scrollback buffer.
353 sbtop -= lines * (cols+1);
356 scroll_size += scroll_top - sbtop;
361 if (scroll_size < 0) {
368 memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
369 for (i = 0; i < size; i++)
370 scroll_top[i+scroll_size] = ERASE_CHAR;
371 if (selstart > scroll_top &&
372 selstart < scroll_top + size + scroll_size) {
374 if (selstart < scroll_top)
375 selstart = scroll_top;
377 if (selend > scroll_top &&
378 selend < scroll_top + size + scroll_size) {
380 if (selend < scroll_top)
385 memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
386 for (i = 0; i < size; i++)
387 scroll_top[i] = ERASE_CHAR;
388 if (selstart > scroll_top &&
389 selstart < scroll_top + size + scroll_size) {
391 if (selstart > scroll_top + size + scroll_size)
392 selstart = scroll_top + size + scroll_size;
394 if (selend > scroll_top &&
395 selend < scroll_top + size + scroll_size) {
397 if (selend > scroll_top + size + scroll_size)
398 selend = scroll_top + size + scroll_size;
402 scroll_heuristic += lines;
406 * Move the cursor to a given position, clipping at boundaries. We
407 * may or may not want to clip at the scroll margin: marg_clip is 0
408 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
409 * even _being_ outside the margins.
411 static void move (int x, int y, int marg_clip) {
417 if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
419 if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
433 * Save or restore the cursor and SGR mode.
435 static void save_cursor(int save) {
439 save_attr = curr_attr;
441 save_csattr = cset_attr[cset];
445 curr_attr = save_attr;
447 cset_attr[cset] = save_csattr;
453 * Erase a large portion of the screen: the whole screen, or the
454 * whole line, or parts thereof.
456 static void erase_lots (int line_only, int from_begin, int to_end) {
457 unsigned long *startpos, *endpos;
460 startpos = cpos - curs_x;
461 endpos = startpos + cols+1;
464 endpos = startpos + rows * (cols+1);
470 check_selection (startpos, endpos);
471 while (startpos < endpos)
472 *startpos++ = ERASE_CHAR;
476 * Insert or delete characters within the current line. n is +ve if
477 * insertion is desired, and -ve for deletion.
479 static void insch (int n) {
480 int dir = (n < 0 ? -1 : +1);
483 n = (n < 0 ? -n : n);
484 if (n > cols - curs_x)
486 m = cols - curs_x - n;
487 check_selection (cpos, cpos+n);
489 memmove (cpos, cpos+n, m*TSIZE);
491 cpos[m++] = ERASE_CHAR;
493 memmove (cpos+n, cpos, m*TSIZE);
495 cpos[n] = ERASE_CHAR;
500 * Toggle terminal mode `mode' to state `state'. (`query' indicates
501 * whether the mode is a DEC private one or a normal one.)
503 static void toggle_mode (int mode, int query, int state) {
504 if (query) switch (mode) {
505 case 1: /* application cursor keys */
506 app_cursor_keys = state;
508 case 3: /* 80/132 columns */
510 request_resize (state ? 132 : 80, rows);
512 case 5: /* reverse video */
516 case 6: /* DEC origin mode */
519 case 7: /* auto wrap */
522 case 47: /* alternate screen */
527 } else switch (mode) {
528 case 4: /* set insert mode */
535 * Process an OSC sequence: set window title or icon name.
537 static void do_osc(void) {
540 wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
542 osc_string[osc_strlen] = '\0';
543 switch (esc_args[0]) {
546 set_icon (osc_string);
547 if (esc_args[0] == 1)
549 /* fall through: parameter 0 means set both */
552 set_title (osc_string);
559 * Remove everything currently in `inbuf' and stick it up on the
560 * in-memory display. There's a big state machine in here to
561 * process escape sequences...
563 void term_out(void) {
565 int must_update = FALSE;
567 while ( (c = inbuf_getc()) != -1) {
570 static FILE *fp = NULL;
571 if (!fp) fp = fopen("putty.log", "wb");
572 if (fp) fputc (c, fp);
579 case '\005': /* terminal type query */
580 back->send ("\033[?1;2c", 7);
588 if (curs_x == 0 && curs_y > 0)
589 curs_x = cols-1, curs_y--;
605 termstate = SEEN_ESC;
608 termstate = SEEN_CSI;
610 esc_args[0] = ARG_DEFAULT;
614 termstate = SEEN_OSC;
627 if (curs_y == marg_b)
628 scroll (marg_t, marg_b, 1, TRUE);
629 else if (curs_y < rows-1)
639 } while (curs_x < cols-1 && !tabs[curs_x]);
643 unsigned long *old_cpos = cpos;
645 check_selection (old_cpos, cpos);
651 if (c >= ' ' && c != 0234) {
653 cpos[1] = ATTR_WRAPPED;
654 if (curs_y == marg_b)
655 scroll (marg_t, marg_b, 1, TRUE);
656 else if (curs_y < rows-1)
665 check_selection (cpos, cpos+1);
666 *cpos++ = c | curr_attr |
667 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
669 if (curs_x == cols) {
679 termstate = TOPLEVEL;
683 * This state is virtually identical to SEEN_ESC, with the
684 * exception that we have an OSC sequence in the pipeline,
685 * and _if_ we see a backslash, we process it.
689 termstate = TOPLEVEL;
692 /* else fall through */
694 termstate = TOPLEVEL;
696 case '\005': case '\007': case '\b': case '\016': case '\017':
697 case '\033': case 0233: case 0234: case 0235: case '\r':
698 case '\013': case '\014': case '\n': case '\t':
699 termstate = TOPLEVEL;
700 goto do_toplevel; /* hack... */
701 case ' ': /* some weird sequence? */
702 termstate = IGNORE_NEXT;
704 case '[': /* enter CSI mode */
705 termstate = SEEN_CSI;
707 esc_args[0] = ARG_DEFAULT;
710 case ']': /* xterm escape sequences */
711 termstate = SEEN_OSC;
714 case '(': /* should set GL */
717 case ')': /* should set GR */
720 case '7': /* save cursor */
723 case '8': /* restore cursor */
729 app_keypad_keys = TRUE;
732 app_keypad_keys = FALSE;
734 case 'D': /* exactly equivalent to LF */
735 if (curs_y == marg_b)
736 scroll (marg_t, marg_b, 1, TRUE);
737 else if (curs_y < rows-1)
744 case 'E': /* exactly equivalent to CR-LF */
747 if (curs_y == marg_b)
748 scroll (marg_t, marg_b, 1, TRUE);
749 else if (curs_y < rows-1)
756 case 'M': /* reverse index - backwards LF */
757 if (curs_y == marg_t)
758 scroll (marg_t, marg_b, -1, TRUE);
766 case 'Z': /* terminal type query */
767 back->send ("\033[?6c", 5);
769 case 'c': /* restore power-on settings */
775 case '#': /* ESC # 8 fills screen with Es :-) */
776 termstate = SEEN_ESCHASH;
778 case 'H': /* set a tab */
784 termstate = TOPLEVEL; /* default */
786 case '\005': case '\007': case '\b': case '\016': case '\017':
787 case '\033': case 0233: case 0234: case 0235: case '\r':
788 case '\013': case '\014': case '\n': case '\t':
789 termstate = TOPLEVEL;
790 goto do_toplevel; /* hack... */
791 case '0': case '1': case '2': case '3': case '4':
792 case '5': case '6': case '7': case '8': case '9':
793 if (esc_nargs <= ARGS_MAX) {
794 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
795 esc_args[esc_nargs-1] = 0;
796 esc_args[esc_nargs-1] =
797 10 * esc_args[esc_nargs-1] + c - '0';
799 termstate = SEEN_CSI;
802 if (++esc_nargs <= ARGS_MAX)
803 esc_args[esc_nargs-1] = ARG_DEFAULT;
804 termstate = SEEN_CSI;
808 termstate = SEEN_CSI;
810 case 'A': /* move up N lines */
811 move (curs_x, curs_y - def(esc_args[0], 1), 1);
815 case 'B': case 'e': /* move down N lines */
816 move (curs_x, curs_y + def(esc_args[0], 1), 1);
820 case 'C': case 'a': /* move right N cols */
821 move (curs_x + def(esc_args[0], 1), curs_y, 1);
825 case 'D': /* move left N cols */
826 move (curs_x - def(esc_args[0], 1), curs_y, 1);
830 case 'E': /* move down N lines and CR */
831 move (0, curs_y + def(esc_args[0], 1), 1);
835 case 'F': /* move up N lines and CR */
836 move (0, curs_y - def(esc_args[0], 1), 1);
840 case 'G': case '`': /* set horizontal posn */
841 move (def(esc_args[0], 1) - 1, curs_y, 0);
845 case 'd': /* set vertical posn */
846 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
851 case 'H': case 'f': /* set horz and vert posns at once */
853 esc_args[1] = ARG_DEFAULT;
854 move (def(esc_args[1], 1) - 1,
855 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
860 case 'J': /* erase screen or parts of it */
862 unsigned int i = def(esc_args[0], 0) + 1;
865 erase_lots(FALSE, !!(i & 2), !!(i & 1));
870 case 'K': /* erase line or parts of it */
872 unsigned int i = def(esc_args[0], 0) + 1;
875 erase_lots(TRUE, !!(i & 2), !!(i & 1));
880 case 'L': /* insert lines */
881 if (curs_y <= marg_b)
882 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
886 case 'M': /* delete lines */
887 if (curs_y <= marg_b)
888 scroll (curs_y, marg_b, def(esc_args[0], 1), FALSE);
892 case '@': /* insert chars */
893 insch (def(esc_args[0], 1));
897 case 'P': /* delete chars */
898 insch (-def(esc_args[0], 1));
902 case 'c': /* terminal type query */
903 back->send ("\033[?6c", 5);
905 case 'n': /* cursor position query */
906 if (esc_args[0] == 6) {
908 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
909 back->send (buf, strlen(buf));
912 case 'h': /* toggle a mode to high */
913 toggle_mode (esc_args[0], esc_query, TRUE);
915 case 'l': /* toggle a mode to low */
916 toggle_mode (esc_args[0], esc_query, FALSE);
918 case 'g': /* clear tabs */
919 if (esc_nargs == 1) {
920 if (esc_args[0] == 0) {
921 tabs[curs_x] = FALSE;
922 } else if (esc_args[0] == 3) {
924 for (i = 0; i < cols; i++)
929 case 'r': /* set scroll margins */
930 if (esc_nargs <= 2) {
932 top = def(esc_args[0], 1) - 1;
935 bot = (esc_nargs == 1 ? rows :
936 def(esc_args[1], rows)) - 1;
944 * I used to think the cursor should be
945 * placed at the top of the newly marginned
946 * area. Apparently not: VMS TPU falls over
956 case 'm': /* set graphics rendition */
959 for (i=0; i<esc_nargs; i++) {
960 switch (def(esc_args[i], 0)) {
961 case 0: /* restore defaults */
962 curr_attr = ATTR_DEFAULT; break;
963 case 1: /* enable bold */
964 curr_attr |= ATTR_BOLD; break;
965 case 4: /* enable underline */
966 case 21: /* (enable double underline) */
967 curr_attr |= ATTR_UNDER; break;
968 case 7: /* enable reverse video */
969 curr_attr |= ATTR_REVERSE; break;
970 case 22: /* disable bold */
971 curr_attr &= ~ATTR_BOLD; break;
972 case 24: /* disable underline */
973 curr_attr &= ~ATTR_UNDER; break;
974 case 27: /* disable reverse video */
975 curr_attr &= ~ATTR_REVERSE; break;
976 case 30: case 31: case 32: case 33:
977 case 34: case 35: case 36: case 37:
979 curr_attr &= ~ATTR_FGMASK;
980 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
982 case 39: /* default-foreground */
983 curr_attr &= ~ATTR_FGMASK;
984 curr_attr |= ATTR_DEFFG;
986 case 40: case 41: case 42: case 43:
987 case 44: case 45: case 46: case 47:
989 curr_attr &= ~ATTR_BGMASK;
990 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
992 case 49: /* default-background */
993 curr_attr &= ~ATTR_BGMASK;
994 curr_attr |= ATTR_DEFBG;
1000 case 's': /* save cursor */
1003 case 'u': /* restore cursor */
1004 save_cursor (FALSE);
1008 case 't': /* set page size - ie window height */
1009 request_resize (cols, def(esc_args[0], 24));
1012 case 'X': /* write N spaces w/o moving cursor */
1014 int n = def(esc_args[0], 1);
1015 unsigned long *p = cpos;
1016 if (n > cols - curs_x)
1018 check_selection (cpos, cpos+n);
1025 case 'x': /* report terminal characteristics */
1028 int i = def(esc_args[0], 0);
1029 if (i == 0 || i == 1) {
1030 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1032 back->send (buf, 20);
1042 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1045 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1047 default: /* specifically, 'B' */
1048 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1051 termstate = TOPLEVEL;
1056 case '\005': case '\007': case '\b': case '\016': case '\017':
1057 case '\033': case 0233: case 0234: case 0235: case '\r':
1058 case '\013': case '\014': case '\n': case '\t':
1059 termstate = TOPLEVEL;
1060 goto do_toplevel; /* hack... */
1061 case 'P': /* Linux palette sequence */
1062 termstate = SEEN_OSC_P;
1065 case 'R': /* Linux palette reset */
1068 termstate = TOPLEVEL;
1070 case 'W': /* word-set */
1071 termstate = SEEN_OSC_W;
1074 case '0': case '1': case '2': case '3': case '4':
1075 case '5': case '6': case '7': case '8': case '9':
1076 esc_args[0] = 10 * esc_args[0] + c - '0';
1080 * Grotty hack to support xterm and DECterm title
1081 * sequences concurrently.
1083 if (esc_args[0] == 2) {
1087 /* else fall through */
1089 termstate = OSC_STRING;
1094 if (c == 0234 || c == '\007') {
1096 * These characters terminate the string; ST and BEL
1097 * terminate the sequence and trigger instant
1098 * processing of it, whereas ESC goes back to SEEN_ESC
1099 * mode unless it is followed by \, in which case it is
1100 * synonymous with ST in the first place.
1103 termstate = TOPLEVEL;
1104 } else if (c == '\033')
1105 termstate = OSC_MAYBE_ST;
1106 else if (osc_strlen < OSC_STR_MAX)
1107 osc_string[osc_strlen++] = c;
1111 int max = (osc_strlen == 0 ? 21 : 16);
1113 if (c >= '0' && c <= '9')
1115 else if (c >= 'A' && c <= 'A'+max-10)
1117 else if (c >= 'a' && c <= 'a'+max-10)
1120 termstate = TOPLEVEL;
1121 osc_string[osc_strlen++] = val;
1122 if (osc_strlen >= 7) {
1123 palette_set (osc_string[0],
1124 osc_string[1] * 16 + osc_string[2],
1125 osc_string[3] * 16 + osc_string[4],
1126 osc_string[5] * 16 + osc_string[6]);
1128 termstate = TOPLEVEL;
1134 case '\005': case '\007': case '\b': case '\016': case '\017':
1135 case '\033': case 0233: case 0234: case 0235: case '\r':
1136 case '\013': case '\014': case '\n': case '\t':
1137 termstate = TOPLEVEL;
1138 goto do_toplevel; /* hack... */
1139 case '0': case '1': case '2': case '3': case '4':
1140 case '5': case '6': case '7': case '8': case '9':
1141 esc_args[0] = 10 * esc_args[0] + c - '0';
1144 termstate = OSC_STRING;
1150 unsigned long *p = scrtop;
1151 int n = rows * (cols+1);
1153 *p++ = ATTR_DEFAULT | 'E';
1156 check_selection (scrtop, scrtop + rows * (cols+1));
1158 termstate = TOPLEVEL;
1161 check_selection (cpos, cpos+1);
1164 if (must_update || nl_count > MAXNL)
1169 * Compare two lines to determine whether they are sufficiently
1170 * alike to scroll-optimise one to the other. Return the degree of
1173 static int linecmp (unsigned long *a, unsigned long *b) {
1176 for (i=n=0; i < cols; i++)
1177 n += (*a++ == *b++);
1182 * Given a context, update the window. Out of paranoia, we don't
1183 * allow WM_PAINT responses to do scrolling optimisations.
1185 static void do_paint (Context ctx, int may_optimise){
1186 int i, j, start, our_curs_y;
1187 unsigned long attr, rv, cursor;
1190 cursor = (has_focus ? ATTR_ACTCURS : ATTR_PASCURS);
1191 rv = (rvideo ? ATTR_REVERSE : 0);
1192 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1194 for (i=0; i<rows; i++) {
1195 int idx = i*(cols+1);
1196 for (j=0; j<=cols; j++,idx++) {
1197 unsigned long *d = disptop+idx;
1198 wanttext[idx] = ((*d ^ rv
1199 ^ (selstart <= d && d < selend ?
1200 ATTR_REVERSE : 0)) |
1201 (i==our_curs_y && j==curs_x ? cursor : 0));
1206 * We would perform scrolling optimisations in here, if they
1207 * didn't have a nasty tendency to cause the whole sodding
1208 * program to hang for a second at speed-critical moments.
1209 * We'll leave it well alone...
1212 for (i=0; i<rows; i++) {
1213 int idx = i*(cols+1);
1215 for (j=0; j<=cols; j++,idx++) {
1216 unsigned long t = wanttext[idx];
1217 int needs_update = (j < cols && t != disptext[idx]);
1218 int keep_going = (start != -1 && needs_update &&
1219 (t & ATTR_MASK) == attr &&
1220 j-start < sizeof(ch));
1221 if (start != -1 && !keep_going) {
1222 do_text (ctx, start, i, ch, j-start, attr);
1228 attr = t & ATTR_MASK;
1230 ch[j-start] = (char) (t & CHAR_MASK);
1238 * Invalidate the whole screen so it will be repainted in full.
1240 void term_invalidate(void) {
1243 for (i=0; i<rows*(cols+1); i++)
1244 disptext[i] = ATTR_INVALID;
1248 * Paint the window in response to a WM_PAINT message.
1250 void term_paint (Context ctx, int l, int t, int r, int b) {
1251 int i, j, left, top, right, bottom;
1253 left = l / font_width;
1254 right = (r - 1) / font_width;
1255 top = t / font_height;
1256 bottom = (b - 1) / font_height;
1257 for (i = top; i <= bottom; i++)
1258 for (j = left; j <= right; j++)
1259 disptext[i*(cols+1)+j] = ATTR_INVALID;
1261 do_paint (ctx, FALSE);
1265 * Attempt to scroll the scrollback. The second parameter gives the
1266 * position we want to scroll to; the first is +1 to denote that
1267 * this position is relative to the beginning of the scrollback, -1
1268 * to denote it is relative to the end, and 0 to denote that it is
1269 * relative to the current position.
1271 void term_scroll (int rel, int where) {
1272 int n = where * (cols+1);
1274 disptop = (rel < 0 ? scrtop :
1275 rel > 0 ? sbtop : disptop) + n;
1276 if (disptop < sbtop)
1278 if (disptop > scrtop)
1285 * Spread the selection outwards according to the selection mode.
1287 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1288 unsigned long *linestart, *lineend;
1292 x = (p - text) % (cols+1);
1294 lineend = linestart + cols;
1299 * In this mode, every character is a separate unit, except
1300 * for runs of spaces at the end of a non-wrapping line.
1302 if (!(linestart[cols] & ATTR_WRAPPED)) {
1303 unsigned long *q = lineend;
1304 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1309 p = (dir == -1 ? q : lineend - 1);
1314 * In this mode, the units are maximal runs of characters
1315 * whose `wordness' has the same value.
1317 wvalue = wordness[*p & CHAR_MASK];
1319 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1322 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1328 * In this mode, every line is a unit.
1330 p = (dir == -1 ? linestart : lineend - 1);
1336 static void sel_spread (void) {
1337 selstart = sel_spread_half (selstart, -1);
1338 selend = sel_spread_half (selend - 1, +1) + 1;
1341 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
1342 unsigned long *selpoint = disptop + y * (cols+1) + x;
1344 if (b == MB_SELECT && a == MA_CLICK) {
1346 selstate = ABOUT_TO;
1347 selanchor = selpoint;
1349 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1351 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1352 selstate = DRAGGING;
1353 selstart = selanchor = selpoint;
1354 selend = selstart + 1;
1356 } else if ((b == MB_SELECT && a == MA_DRAG) ||
1357 (b == MB_EXTEND && a != MA_RELEASE)) {
1358 if (selstate == ABOUT_TO && selanchor == selpoint)
1360 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1361 if (selpoint-selstart < (selend-selstart)/2)
1362 selanchor = selend - 1;
1364 selanchor = selstart;
1365 selstate = DRAGGING;
1367 if (selstate != ABOUT_TO && selstate != DRAGGING)
1368 selanchor = selpoint;
1369 selstate = DRAGGING;
1370 if (selpoint < selanchor) {
1371 selstart = selpoint;
1372 selend = selanchor + 1;
1374 selstart = selanchor;
1375 selend = selpoint + 1;
1378 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
1379 if (selstate == DRAGGING) {
1381 * We've completed a selection. We now transfer the
1382 * data to the clipboard.
1384 unsigned char *p = selspace;
1385 unsigned long *q = selstart;
1387 while (q < selend) {
1389 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1390 unsigned long *nlpos = lineend;
1392 if (!(*nlpos & ATTR_WRAPPED)) {
1393 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1398 while (q < nlpos && q < selend)
1399 *p++ = (unsigned char) (*q++ & CHAR_MASK);
1402 for (i=0; i<sizeof(sel_nl); i++)
1405 q = lineend + 1; /* start of next line */
1407 write_clip (selspace, p - selspace);
1408 selstate = SELECTED;
1410 selstate = NO_SELECTION;
1411 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
1415 get_clip((void **) &data, &len);
1419 while (p < data+len) {
1420 while (p < data+len &&
1421 !(p <= data+len-sizeof(sel_nl) &&
1422 !memcmp(p, sel_nl, sizeof(sel_nl))))
1424 back->send (q, p-q);
1425 if (p <= data+len-sizeof(sel_nl) &&
1426 !memcmp(p, sel_nl, sizeof(sel_nl))) {
1427 back->send ("\r", 1);
1428 p += sizeof(sel_nl);
1433 get_clip(NULL, NULL);
1439 static void deselect (void) {
1440 selstate = NO_SELECTION;
1441 selstart = selend = scrtop;
1444 void term_deselect (void) {