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;
74 OSC_STRING, OSC_MAYBE_ST,
79 NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
82 SM_CHAR, SM_WORD, SM_LINE
84 static unsigned long *selstart, *selend, *selanchor;
86 static short wordness[256] = {
87 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 */
88 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 */
89 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 */
90 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 */
91 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 */
92 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 */
93 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 */
94 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 */
97 static unsigned char sel_nl[] = SEL_NL;
100 * Internal prototypes.
102 static void do_paint (Context, int);
103 static void erase_lots (int, int, int);
104 static void swap_screen (int);
105 static void update_sbar (void);
106 static void deselect (void);
109 * Set up power-on settings for the terminal.
111 static void power_on(void) {
112 curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
115 alt_b = marg_b = rows - 1;
120 for (i = 0; i < cols; i++)
121 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
123 alt_om = dec_om = cfg.dec_om;
124 alt_wnext = wrapnext = alt_ins = insert = FALSE;
125 alt_wrap = wrap = cfg.wrap_mode;
127 cset_attr[0] = cset_attr[1] = ATTR_ASCII;
129 save_attr = curr_attr = ATTR_DEFAULT;
130 app_cursor_keys = cfg.app_cursor;
131 app_keypad_keys = cfg.app_keypad;
135 for (i = 0; i < 256; i++)
136 wordness[i] = cfg.wordness[i];
140 erase_lots (FALSE, TRUE, TRUE);
142 erase_lots (FALSE, TRUE, TRUE);
147 * Force a screen update.
149 void term_update(void) {
153 if ( (seen_key_event && (unscroll_event & US_KEY)) &&
154 (seen_disp_event && (unscroll_event & US_DISP)) ) {
156 seen_disp_event = seen_key_event = 0;
158 do_paint (ctx, TRUE);
161 scroll_heuristic = 0;
166 * Same as power_on(), but an external function.
168 void term_pwron(void) {
177 * Clear the scrollback.
179 void term_clrsb(void) {
180 disptop = sbtop = scrtop;
185 * Initialise the terminal.
187 void term_init(void) {
188 text = sbtop = scrtop = disptop = cpos = NULL;
189 disptext = wanttext = NULL;
199 * Set up the terminal for a given size.
201 void term_size(int newrows, int newcols, int newsavelines) {
202 unsigned long *newtext, *newdisp, *newwant, *newalt;
203 int i, j, crows, ccols;
205 if (newrows == rows && newcols == cols && newsavelines == savelines)
206 return; /* nothing to do */
209 alt_b = marg_b = newrows - 1;
211 newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
212 disptop = newtext + newsavelines*(newcols+1);
213 for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
214 newtext[i] = ERASE_CHAR;
216 crows = rows + (scrtop - sbtop) / (cols+1);
217 if (crows > newrows+newsavelines)
218 crows = newrows+newsavelines;
219 ccols = (cols < newcols ? cols : newcols);
220 for (i=0; i<crows; i++) {
221 int oldidx = (rows + savelines - crows + i) * (cols+1);
222 int newidx = (newrows + newsavelines - crows + i) * (newcols+1);
223 for (j=0; j<ccols; j++)
224 newtext[newidx+j] = text[oldidx+j];
225 newtext[newidx+newcols] =
226 (cols == newcols ? text[oldidx+cols] : 0);
228 sbtop = disptop - (crows - newrows) * (newcols+1);
237 newdisp = smalloc (newrows*(newcols+1)*TSIZE);
238 for (i=0; i<newrows*(newcols+1); i++)
239 newdisp[i] = ATTR_INVALID;
243 newwant = smalloc (newrows*(newcols+1)*TSIZE);
244 for (i=0; i<newrows*(newcols+1); i++)
245 newwant[i] = ATTR_INVALID;
249 newalt = smalloc (newrows*(newcols+1)*TSIZE);
250 for (i=0; i<newrows*(newcols+1); i++)
251 newalt[i] = ERASE_CHAR;
256 selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
258 tabs = srealloc (tabs, newcols*sizeof(*tabs));
261 for (i = (cols > 0 ? cols : 0); i < newcols; i++)
262 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
266 curs_y += newrows - rows;
269 if (curs_y >= newrows)
271 if (curs_x >= newcols)
274 wrapnext = alt_wnext = FALSE;
278 savelines = newsavelines;
289 static void swap_screen (int which) {
293 if (which == alt_which)
298 for (t=0; t<rows*(cols+1); t++) {
299 tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
302 t = curs_x; curs_x = alt_x; alt_x = t;
303 t = curs_y; curs_y = alt_y; alt_y = t;
304 t = marg_t; marg_t = alt_t; alt_t = t;
305 t = marg_b; marg_b = alt_b; alt_b = t;
306 t = dec_om; dec_om = alt_om; alt_om = t;
307 t = wrap; wrap = alt_wrap; alt_wrap = t;
308 t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
309 t = insert; insert = alt_ins; alt_ins = t;
310 t = cset; cset = alt_cset; alt_cset = t;
316 * Retrieve a character from `inbuf'.
318 static int inbuf_getc(void) {
319 if (inbuf_head == inbuf_reap)
323 inbuf_reap = (inbuf_reap+1) & INBUF_MASK;
329 * Update the scroll bar.
331 static void update_sbar(void) {
334 min = (sbtop - text) / (cols+1);
335 set_sbar ((scrtop - text) / (cols+1) + rows - min,
336 (disptop - text) / (cols+1) - min,
341 * Check whether the region bounded by the two pointers intersects
342 * the scroll region, and de-select the on-screen selection if so.
344 static void check_selection (unsigned long *from, unsigned long *to) {
345 if (from < selend && selstart < to)
350 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
351 * for backward.) `sb' is TRUE if the scrolling is permitted to
352 * affect the scrollback buffer.
354 static void scroll (int topline, int botline, int lines, int sb) {
355 unsigned long *scroll_top;
356 int scroll_size, size, i;
358 scroll_top = scrtop + topline*(cols+1);
359 size = (lines < 0 ? -lines : lines) * (cols+1);
360 scroll_size = (botline - topline + 1) * (cols+1) - size;
362 if (lines > 0 && topline == 0 && alt_which == 0 && sb) {
364 * Since we're going to scroll the top line and we're on the
365 * scrolling screen let's also affect the scrollback buffer.
367 sbtop -= lines * (cols+1);
370 scroll_size += scroll_top - sbtop;
375 if (scroll_size < 0) {
382 memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
383 for (i = 0; i < size; i++)
384 scroll_top[i+scroll_size] = ERASE_CHAR;
385 if (selstart > scroll_top &&
386 selstart < scroll_top + size + scroll_size) {
388 if (selstart < scroll_top)
389 selstart = scroll_top;
391 if (selend > scroll_top &&
392 selend < scroll_top + size + scroll_size) {
394 if (selend < scroll_top)
399 memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
400 for (i = 0; i < size; i++)
401 scroll_top[i] = ERASE_CHAR;
402 if (selstart > scroll_top &&
403 selstart < scroll_top + size + scroll_size) {
405 if (selstart > scroll_top + size + scroll_size)
406 selstart = scroll_top + size + scroll_size;
408 if (selend > scroll_top &&
409 selend < scroll_top + size + scroll_size) {
411 if (selend > scroll_top + size + scroll_size)
412 selend = scroll_top + size + scroll_size;
416 scroll_heuristic += lines;
420 * Move the cursor to a given position, clipping at boundaries. We
421 * may or may not want to clip at the scroll margin: marg_clip is 0
422 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
423 * even _being_ outside the margins.
425 static void move (int x, int y, int marg_clip) {
431 if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
433 if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
447 * Save or restore the cursor and SGR mode.
449 static void save_cursor(int save) {
453 save_attr = curr_attr;
455 save_csattr = cset_attr[cset];
459 curr_attr = save_attr;
461 cset_attr[cset] = save_csattr;
467 * Erase a large portion of the screen: the whole screen, or the
468 * whole line, or parts thereof.
470 static void erase_lots (int line_only, int from_begin, int to_end) {
471 unsigned long *startpos, *endpos;
474 startpos = cpos - curs_x;
475 endpos = startpos + cols+1;
478 endpos = startpos + rows * (cols+1);
484 check_selection (startpos, endpos);
485 while (startpos < endpos)
486 *startpos++ = ERASE_CHAR;
490 * Insert or delete characters within the current line. n is +ve if
491 * insertion is desired, and -ve for deletion.
493 static void insch (int n) {
494 int dir = (n < 0 ? -1 : +1);
497 n = (n < 0 ? -n : n);
498 if (n > cols - curs_x)
500 m = cols - curs_x - n;
501 check_selection (cpos, cpos+n);
503 memmove (cpos, cpos+n, m*TSIZE);
505 cpos[m++] = ERASE_CHAR;
507 memmove (cpos+n, cpos, m*TSIZE);
509 cpos[n] = ERASE_CHAR;
514 * Toggle terminal mode `mode' to state `state'. (`query' indicates
515 * whether the mode is a DEC private one or a normal one.)
517 static void toggle_mode (int mode, int query, int state) {
518 if (query) switch (mode) {
519 case 1: /* application cursor keys */
520 app_cursor_keys = state;
522 case 3: /* 80/132 columns */
524 request_resize (state ? 132 : 80, rows);
526 case 5: /* reverse video */
528 seen_disp_event = TRUE;
530 case 6: /* DEC origin mode */
533 case 7: /* auto wrap */
536 case 47: /* alternate screen */
541 } else switch (mode) {
542 case 4: /* set insert mode */
549 * Process an OSC sequence: set window title or icon name.
551 static void do_osc(void) {
554 wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
556 osc_string[osc_strlen] = '\0';
557 switch (esc_args[0]) {
560 set_icon (osc_string);
561 if (esc_args[0] == 1)
563 /* fall through: parameter 0 means set both */
566 set_title (osc_string);
573 * Remove everything currently in `inbuf' and stick it up on the
574 * in-memory display. There's a big state machine in here to
575 * process escape sequences...
577 void term_out(void) {
580 while ( (c = inbuf_getc()) != -1) {
583 static FILE *fp = NULL;
584 if (!fp) fp = fopen("putty.log", "wb");
585 if (fp) fputc (c, fp);
588 if( termstate < DO_CTRLS && (c&0x60) == 0 ) {
590 case '\005': /* terminal type query */
591 ldisc->send ("PuTTY\r", 6);
598 if (curs_x == 0 && curs_y > 0)
599 curs_x = cols-1, curs_y--;
605 seen_disp_event = TRUE;
614 termstate = SEEN_ESC;
617 termstate = SEEN_CSI;
619 esc_args[0] = ARG_DEFAULT;
623 termstate = SEEN_OSC;
630 seen_disp_event = TRUE;
635 if (curs_y == marg_b)
636 scroll (marg_t, marg_b, 1, TRUE);
637 else if (curs_y < rows-1)
649 } while (curs_x < cols-1 && !tabs[curs_x]);
653 unsigned long *old_cpos = cpos;
655 check_selection (old_cpos, cpos);
657 seen_disp_event = TRUE;
661 else switch (termstate) {
663 if (c >= ' ' && c != 0234) {
665 cpos[1] = ATTR_WRAPPED;
666 if (curs_y == marg_b)
667 scroll (marg_t, marg_b, 1, TRUE);
668 else if (curs_y < rows-1)
677 check_selection (cpos, cpos+1);
678 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
679 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
681 if (curs_x == cols) {
691 termstate = TOPLEVEL;
695 * This state is virtually identical to SEEN_ESC, with the
696 * exception that we have an OSC sequence in the pipeline,
697 * and _if_ we see a backslash, we process it.
701 termstate = TOPLEVEL;
704 /* else fall through */
706 termstate = TOPLEVEL;
708 case ' ': /* some weird sequence? */
709 termstate = IGNORE_NEXT;
711 case '[': /* enter CSI mode */
712 termstate = SEEN_CSI;
714 esc_args[0] = ARG_DEFAULT;
717 case ']': /* xterm escape sequences */
718 termstate = SEEN_OSC;
721 case '(': /* should set GL */
724 case ')': /* should set GR */
727 case '7': /* save cursor */
730 case '8': /* restore cursor */
732 seen_disp_event = TRUE;
735 app_keypad_keys = TRUE;
738 app_keypad_keys = FALSE;
740 case 'D': /* exactly equivalent to LF */
741 if (curs_y == marg_b)
742 scroll (marg_t, marg_b, 1, TRUE);
743 else if (curs_y < rows-1)
747 seen_disp_event = TRUE;
750 case 'E': /* exactly equivalent to CR-LF */
753 if (curs_y == marg_b)
754 scroll (marg_t, marg_b, 1, TRUE);
755 else if (curs_y < rows-1)
760 seen_disp_event = TRUE;
762 case 'M': /* reverse index - backwards LF */
763 if (curs_y == marg_t)
764 scroll (marg_t, marg_b, -1, TRUE);
769 seen_disp_event = TRUE;
771 case 'Z': /* terminal type query */
772 ldisc->send ("\033[?6c", 5);
774 case 'c': /* restore power-on settings */
778 seen_disp_event = TRUE;
780 case '#': /* ESC # 8 fills screen with Es :-) */
781 termstate = SEEN_ESCHASH;
783 case 'H': /* set a tab */
789 termstate = TOPLEVEL; /* default */
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);
812 seen_disp_event = TRUE;
814 case 'B': case 'e': /* move down N lines */
815 move (curs_x, curs_y + def(esc_args[0], 1), 1);
816 seen_disp_event = TRUE;
818 case 'C': case 'a': /* move right N cols */
819 move (curs_x + def(esc_args[0], 1), curs_y, 1);
820 seen_disp_event = TRUE;
822 case 'D': /* move left N cols */
823 move (curs_x - def(esc_args[0], 1), curs_y, 1);
824 seen_disp_event = TRUE;
826 case 'E': /* move down N lines and CR */
827 move (0, curs_y + def(esc_args[0], 1), 1);
828 seen_disp_event = TRUE;
830 case 'F': /* move up N lines and CR */
831 move (0, curs_y - def(esc_args[0], 1), 1);
832 seen_disp_event = TRUE;
834 case 'G': case '`': /* set horizontal posn */
835 move (def(esc_args[0], 1) - 1, curs_y, 0);
836 seen_disp_event = TRUE;
838 case 'd': /* set vertical posn */
839 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
841 seen_disp_event = TRUE;
843 case 'H': case 'f': /* set horz and vert posns at once */
845 esc_args[1] = ARG_DEFAULT;
846 move (def(esc_args[1], 1) - 1,
847 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
849 seen_disp_event = TRUE;
851 case 'J': /* erase screen or parts of it */
853 unsigned int i = def(esc_args[0], 0) + 1;
856 erase_lots(FALSE, !!(i & 2), !!(i & 1));
859 seen_disp_event = TRUE;
861 case 'K': /* erase line or parts of it */
863 unsigned int i = def(esc_args[0], 0) + 1;
866 erase_lots(TRUE, !!(i & 2), !!(i & 1));
868 seen_disp_event = TRUE;
870 case 'L': /* insert lines */
871 if (curs_y <= marg_b)
872 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
873 seen_disp_event = TRUE;
875 case 'M': /* delete lines */
876 if (curs_y <= marg_b)
877 scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
878 seen_disp_event = TRUE;
880 case '@': /* insert chars */
881 insch (def(esc_args[0], 1));
882 seen_disp_event = TRUE;
884 case 'P': /* delete chars */
885 insch (-def(esc_args[0], 1));
886 seen_disp_event = TRUE;
888 case 'c': /* terminal type query */
889 ldisc->send ("\033[?6c", 5);
891 case 'n': /* cursor position query */
892 if (esc_args[0] == 6) {
894 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
895 ldisc->send (buf, strlen(buf));
898 case 'h': /* toggle a mode to high */
899 toggle_mode (esc_args[0], esc_query, TRUE);
901 case 'l': /* toggle a mode to low */
902 toggle_mode (esc_args[0], esc_query, FALSE);
904 case 'g': /* clear tabs */
905 if (esc_nargs == 1) {
906 if (esc_args[0] == 0) {
907 tabs[curs_x] = FALSE;
908 } else if (esc_args[0] == 3) {
910 for (i = 0; i < cols; i++)
915 case 'r': /* set scroll margins */
916 if (!esc_query && esc_nargs <= 2) {
918 top = def(esc_args[0], 1) - 1;
921 bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
922 def(esc_args[1], rows)) - 1;
930 * I used to think the cursor should be
931 * placed at the top of the newly marginned
932 * area. Apparently not: VMS TPU falls over
937 seen_disp_event = TRUE;
941 case 'm': /* set graphics rendition */
944 for (i=0; i<esc_nargs; i++) {
945 switch (def(esc_args[i], 0)) {
946 case 0: /* restore defaults */
947 curr_attr = ATTR_DEFAULT; break;
948 case 1: /* enable bold */
949 curr_attr |= ATTR_BOLD; break;
950 case 4: /* enable underline */
951 case 21: /* (enable double underline) */
952 curr_attr |= ATTR_UNDER; break;
953 case 7: /* enable reverse video */
954 curr_attr |= ATTR_REVERSE; break;
955 case 22: /* disable bold */
956 curr_attr &= ~ATTR_BOLD; break;
957 case 24: /* disable underline */
958 curr_attr &= ~ATTR_UNDER; break;
959 case 27: /* disable reverse video */
960 curr_attr &= ~ATTR_REVERSE; break;
961 case 30: case 31: case 32: case 33:
962 case 34: case 35: case 36: case 37:
964 curr_attr &= ~ATTR_FGMASK;
965 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
967 case 39: /* default-foreground */
968 curr_attr &= ~ATTR_FGMASK;
969 curr_attr |= ATTR_DEFFG;
971 case 40: case 41: case 42: case 43:
972 case 44: case 45: case 46: case 47:
974 curr_attr &= ~ATTR_BGMASK;
975 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
977 case 49: /* default-background */
978 curr_attr &= ~ATTR_BGMASK;
979 curr_attr |= ATTR_DEFBG;
985 case 's': /* save cursor */
988 case 'u': /* restore cursor */
990 seen_disp_event = TRUE;
992 case 't': /* set page size - ie window height */
993 request_resize (cols, def(esc_args[0], 24));
996 case 'X': /* write N spaces w/o moving cursor */
998 int n = def(esc_args[0], 1);
999 unsigned long *p = cpos;
1000 if (n > cols - curs_x)
1002 check_selection (cpos, cpos+n);
1005 seen_disp_event = TRUE;
1008 case 'x': /* report terminal characteristics */
1011 int i = def(esc_args[0], 0);
1012 if (i == 0 || i == 1) {
1013 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1015 ldisc->send (buf, 20);
1025 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1028 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1030 default: /* specifically, 'B' */
1031 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1034 termstate = TOPLEVEL;
1039 case 'P': /* Linux palette sequence */
1040 termstate = SEEN_OSC_P;
1043 case 'R': /* Linux palette reset */
1046 termstate = TOPLEVEL;
1048 case 'W': /* word-set */
1049 termstate = SEEN_OSC_W;
1052 case '0': case '1': case '2': case '3': case '4':
1053 case '5': case '6': case '7': case '8': case '9':
1054 esc_args[0] = 10 * esc_args[0] + c - '0';
1058 * Grotty hack to support xterm and DECterm title
1059 * sequences concurrently.
1061 if (esc_args[0] == 2) {
1065 /* else fall through */
1067 termstate = OSC_STRING;
1072 if (c == 0234 || c == '\007') {
1074 * These characters terminate the string; ST and BEL
1075 * terminate the sequence and trigger instant
1076 * processing of it, whereas ESC goes back to SEEN_ESC
1077 * mode unless it is followed by \, in which case it is
1078 * synonymous with ST in the first place.
1081 termstate = TOPLEVEL;
1082 } else if (c == '\033')
1083 termstate = OSC_MAYBE_ST;
1084 else if (osc_strlen < OSC_STR_MAX)
1085 osc_string[osc_strlen++] = c;
1089 int max = (osc_strlen == 0 ? 21 : 16);
1091 if (c >= '0' && c <= '9')
1093 else if (c >= 'A' && c <= 'A'+max-10)
1095 else if (c >= 'a' && c <= 'a'+max-10)
1098 termstate = TOPLEVEL;
1099 osc_string[osc_strlen++] = val;
1100 if (osc_strlen >= 7) {
1101 palette_set (osc_string[0],
1102 osc_string[1] * 16 + osc_string[2],
1103 osc_string[3] * 16 + osc_string[4],
1104 osc_string[5] * 16 + osc_string[6]);
1106 termstate = TOPLEVEL;
1112 case '0': case '1': case '2': case '3': case '4':
1113 case '5': case '6': case '7': case '8': case '9':
1114 esc_args[0] = 10 * esc_args[0] + c - '0';
1117 termstate = OSC_STRING;
1123 unsigned long *p = scrtop;
1124 int n = rows * (cols+1);
1126 *p++ = ATTR_DEFAULT | 'E';
1128 seen_disp_event = TRUE;
1129 check_selection (scrtop, scrtop + rows * (cols+1));
1131 termstate = TOPLEVEL;
1134 check_selection (cpos, cpos+1);
1137 if (nl_count > MAXNL)
1142 * Compare two lines to determine whether they are sufficiently
1143 * alike to scroll-optimise one to the other. Return the degree of
1146 static int linecmp (unsigned long *a, unsigned long *b) {
1149 for (i=n=0; i < cols; i++)
1150 n += (*a++ == *b++);
1155 * Given a context, update the window. Out of paranoia, we don't
1156 * allow WM_PAINT responses to do scrolling optimisations.
1158 static void do_paint (Context ctx, int may_optimise){
1159 int i, j, start, our_curs_y;
1160 unsigned long attr, rv, cursor;
1163 cursor = (has_focus ? ATTR_ACTCURS : ATTR_PASCURS);
1164 rv = (rvideo ? ATTR_REVERSE : 0);
1165 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1167 for (i=0; i<rows; i++) {
1168 int idx = i*(cols+1);
1169 for (j=0; j<=cols; j++,idx++) {
1170 unsigned long *d = disptop+idx;
1171 wanttext[idx] = ((*d ^ rv
1172 ^ (selstart <= d && d < selend ?
1173 ATTR_REVERSE : 0)) |
1174 (i==our_curs_y && j==curs_x ? cursor : 0));
1179 * We would perform scrolling optimisations in here, if they
1180 * didn't have a nasty tendency to cause the whole sodding
1181 * program to hang for a second at speed-critical moments.
1182 * We'll leave it well alone...
1185 for (i=0; i<rows; i++) {
1186 int idx = i*(cols+1);
1188 for (j=0; j<=cols; j++,idx++) {
1189 unsigned long t = wanttext[idx];
1190 int needs_update = (j < cols && t != disptext[idx]);
1191 int keep_going = (start != -1 && needs_update &&
1192 (t & ATTR_MASK) == attr &&
1193 j-start < sizeof(ch));
1194 if (start != -1 && !keep_going) {
1195 do_text (ctx, start, i, ch, j-start, attr);
1201 attr = t & ATTR_MASK;
1203 ch[j-start] = (char) (t & CHAR_MASK);
1211 * Invalidate the whole screen so it will be repainted in full.
1213 void term_invalidate(void) {
1216 for (i=0; i<rows*(cols+1); i++)
1217 disptext[i] = ATTR_INVALID;
1221 * Paint the window in response to a WM_PAINT message.
1223 void term_paint (Context ctx, int l, int t, int r, int b) {
1224 int i, j, left, top, right, bottom;
1226 left = l / font_width;
1227 right = (r - 1) / font_width;
1228 top = t / font_height;
1229 bottom = (b - 1) / font_height;
1230 for (i = top; i <= bottom && i < rows ; i++)
1231 for (j = left; j <= right && j < cols ; j++)
1232 disptext[i*(cols+1)+j] = ATTR_INVALID;
1234 do_paint (ctx, FALSE);
1238 * Attempt to scroll the scrollback. The second parameter gives the
1239 * position we want to scroll to; the first is +1 to denote that
1240 * this position is relative to the beginning of the scrollback, -1
1241 * to denote it is relative to the end, and 0 to denote that it is
1242 * relative to the current position.
1244 void term_scroll (int rel, int where) {
1245 int n = where * (cols+1);
1247 disptop = (rel < 0 ? scrtop :
1248 rel > 0 ? sbtop : disptop) + n;
1249 if (disptop < sbtop)
1251 if (disptop > scrtop)
1258 * Spread the selection outwards according to the selection mode.
1260 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1261 unsigned long *linestart, *lineend;
1265 x = (p - text) % (cols+1);
1267 lineend = linestart + cols;
1272 * In this mode, every character is a separate unit, except
1273 * for runs of spaces at the end of a non-wrapping line.
1275 if (!(linestart[cols] & ATTR_WRAPPED)) {
1276 unsigned long *q = lineend;
1277 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1282 p = (dir == -1 ? q : lineend - 1);
1287 * In this mode, the units are maximal runs of characters
1288 * whose `wordness' has the same value.
1290 wvalue = wordness[*p & CHAR_MASK];
1292 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1295 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1301 * In this mode, every line is a unit.
1303 p = (dir == -1 ? linestart : lineend - 1);
1309 static void sel_spread (void) {
1310 selstart = sel_spread_half (selstart, -1);
1311 selend = sel_spread_half (selend - 1, +1) + 1;
1314 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
1315 unsigned long *selpoint;
1318 if (y>=rows) y = rows-1;
1326 if (x>=cols) x = cols-1;
1328 selpoint = disptop + y * (cols+1) + x;
1330 if (b == MB_SELECT && a == MA_CLICK) {
1332 selstate = ABOUT_TO;
1333 selanchor = selpoint;
1335 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1337 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1338 selstate = DRAGGING;
1339 selstart = selanchor = selpoint;
1340 selend = selstart + 1;
1342 } else if ((b == MB_SELECT && a == MA_DRAG) ||
1343 (b == MB_EXTEND && a != MA_RELEASE)) {
1344 if (selstate == ABOUT_TO && selanchor == selpoint)
1346 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1347 if (selpoint-selstart < (selend-selstart)/2)
1348 selanchor = selend - 1;
1350 selanchor = selstart;
1351 selstate = DRAGGING;
1353 if (selstate != ABOUT_TO && selstate != DRAGGING)
1354 selanchor = selpoint;
1355 selstate = DRAGGING;
1356 if (selpoint < selanchor) {
1357 selstart = selpoint;
1358 selend = selanchor + 1;
1360 selstart = selanchor;
1361 selend = selpoint + 1;
1364 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
1365 if (selstate == DRAGGING) {
1367 * We've completed a selection. We now transfer the
1368 * data to the clipboard.
1370 unsigned char *p = selspace;
1371 unsigned long *q = selstart;
1373 while (q < selend) {
1375 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1376 unsigned long *nlpos = lineend;
1378 if (!(*nlpos & ATTR_WRAPPED)) {
1379 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1384 while (q < nlpos && q < selend)
1385 *p++ = (unsigned char) (*q++ & CHAR_MASK);
1388 for (i=0; i<sizeof(sel_nl); i++)
1391 q = lineend + 1; /* start of next line */
1393 write_clip (selspace, p - selspace);
1394 selstate = SELECTED;
1396 selstate = NO_SELECTION;
1397 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
1401 get_clip((void **) &data, &len);
1405 while (p < data+len) {
1406 while (p < data+len &&
1407 !(p <= data+len-sizeof(sel_nl) &&
1408 !memcmp(p, sel_nl, sizeof(sel_nl))))
1416 c=xlat_kbd2tty(q[i]);
1421 if (p <= data+len-sizeof(sel_nl) &&
1422 !memcmp(p, sel_nl, sizeof(sel_nl))) {
1423 ldisc->send ("\r", 1);
1424 p += sizeof(sel_nl);
1429 get_clip(NULL, NULL);
1435 static void deselect (void) {
1436 selstate = NO_SELECTION;
1437 selstart = selend = scrtop;
1440 void term_deselect (void) {