1 ++ terminal.c.new Mon Nov 22 15:02:03 1999
9 static unsigned long *text; /* buffer of text on terminal screen */
10 static unsigned long *scrtop; /* top of working screen */
11 static unsigned long *disptop; /* top of displayed screen */
12 static unsigned long *sbtop; /* top of scrollback */
13 static unsigned long *cpos; /* cursor position (convenience) */
14 static unsigned long *disptext; /* buffer of text on real screen */
15 static unsigned long *wanttext; /* buffer of text we want on screen */
16 static unsigned long *alttext; /* buffer of text on alt. screen */
18 static unsigned char *selspace; /* buffer for building selections in */
20 #define TSIZE (sizeof(*text))
21 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
23 static unsigned long curr_attr, save_attr;
25 static int curs_x, curs_y; /* cursor */
26 static int save_x, save_y; /* saved cursor position */
27 static int marg_t, marg_b; /* scroll margins */
28 static int dec_om; /* DEC origin mode flag */
29 static int wrap, wrapnext; /* wrap flags */
30 static int insert; /* insert-mode flag */
31 static int cset; /* 0 or 1: which char set */
32 static int save_cset, save_csattr; /* saved with cursor position */
33 static int rvideo; /* global reverse video flag */
35 static unsigned long cset_attr[2];
38 * Saved settings on the alternate screen.
40 static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
41 static int alt_t, alt_b;
44 #define ARGS_MAX 32 /* max # of esc sequence arguments */
45 #define ARG_DEFAULT -1 /* if an arg isn't specified */
46 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
47 static int esc_args[ARGS_MAX];
51 #define OSC_STR_MAX 2048
52 static int osc_strlen;
53 static char osc_string[OSC_STR_MAX+1];
56 static unsigned char *tabs;
61 static int scroll_heuristic;
75 OSC_STRING, OSC_MAYBE_ST,
80 NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
83 SM_CHAR, SM_WORD, SM_LINE
85 static unsigned long *selstart, *selend, *selanchor;
87 static short wordness[256] = {
88 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 */
89 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 */
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,2, /* 45 */
91 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 */
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, /* 89 */
93 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 */
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, /* CD */
95 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 */
98 static unsigned char sel_nl[] = SEL_NL;
101 * Internal prototypes.
103 static void do_paint (Context, int);
104 static void erase_lots (int, int, int);
105 static void swap_screen (int);
106 static void update_sbar (void);
107 static void deselect (void);
110 * Set up power-on settings for the terminal.
112 static void power_on(void) {
113 curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
116 alt_b = marg_b = rows - 1;
121 for (i = 0; i < cols; i++)
122 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
124 alt_om = dec_om = cfg.dec_om;
125 alt_wnext = wrapnext = alt_ins = insert = FALSE;
126 alt_wrap = wrap = cfg.wrap_mode;
128 cset_attr[0] = cset_attr[1] = ATTR_ASCII;
130 save_attr = curr_attr = ATTR_DEFAULT;
131 app_cursor_keys = cfg.app_cursor;
132 app_keypad_keys = cfg.app_keypad;
136 for (i = 0; i < 256; i++)
137 wordness[i] = cfg.wordness[i];
141 erase_lots (FALSE, TRUE, TRUE);
143 erase_lots (FALSE, TRUE, TRUE);
148 * Force a screen update.
150 void term_update(void) {
154 if ( (seen_key_event && (unscroll_event & US_KEY)) &&
155 (seen_disp_event && (unscroll_event & US_DISP)) ) {
157 seen_disp_event = seen_key_event = 0;
159 do_paint (ctx, TRUE);
162 scroll_heuristic = 0;
167 * Same as power_on(), but an external function.
169 void term_pwron(void) {
178 * Clear the scrollback.
180 void term_clrsb(void) {
181 disptop = sbtop = scrtop;
186 * Initialise the terminal.
188 void term_init(void) {
189 text = sbtop = scrtop = disptop = cpos = NULL;
190 disptext = wanttext = NULL;
200 * Set up the terminal for a given size.
202 void term_size(int newrows, int newcols, int newsavelines) {
203 unsigned long *newtext, *newdisp, *newwant, *newalt;
204 int i, j, crows, ccols;
206 if (newrows == rows && newcols == cols && newsavelines == savelines)
207 return; /* nothing to do */
210 alt_b = marg_b = newrows - 1;
212 newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
213 disptop = newtext + newsavelines*(newcols+1);
214 for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
215 newtext[i] = ERASE_CHAR;
217 crows = rows + (scrtop - sbtop) / (cols+1);
218 if (crows > newrows+newsavelines)
219 crows = newrows+newsavelines;
220 ccols = (cols < newcols ? cols : newcols);
221 for (i=0; i<crows; i++) {
222 int oldidx = (rows + savelines - crows + i) * (cols+1);
223 int newidx = (newrows + newsavelines - crows + i) * (newcols+1);
224 for (j=0; j<ccols; j++)
225 newtext[newidx+j] = text[oldidx+j];
226 newtext[newidx+newcols] =
227 (cols == newcols ? text[oldidx+cols] : 0);
229 sbtop = disptop - (crows - newrows) * (newcols+1);
238 newdisp = smalloc (newrows*(newcols+1)*TSIZE);
239 for (i=0; i<newrows*(newcols+1); i++)
240 newdisp[i] = ATTR_INVALID;
244 newwant = smalloc (newrows*(newcols+1)*TSIZE);
245 for (i=0; i<newrows*(newcols+1); i++)
246 newwant[i] = ATTR_INVALID;
250 newalt = smalloc (newrows*(newcols+1)*TSIZE);
251 for (i=0; i<newrows*(newcols+1); i++)
252 newalt[i] = ERASE_CHAR;
257 selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
259 tabs = srealloc (tabs, newcols*sizeof(*tabs));
262 for (i = (cols > 0 ? cols : 0); i < newcols; i++)
263 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
267 curs_y += newrows - rows;
270 if (curs_y >= newrows)
272 if (curs_x >= newcols)
275 wrapnext = alt_wnext = FALSE;
279 savelines = newsavelines;
290 static void swap_screen (int which) {
294 if (which == alt_which)
299 for (t=0; t<rows*(cols+1); t++) {
300 tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
303 t = curs_x; curs_x = alt_x; alt_x = t;
304 t = curs_y; curs_y = alt_y; alt_y = t;
305 t = marg_t; marg_t = alt_t; alt_t = t;
306 t = marg_b; marg_b = alt_b; alt_b = t;
307 t = dec_om; dec_om = alt_om; alt_om = t;
308 t = wrap; wrap = alt_wrap; alt_wrap = t;
309 t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
310 t = insert; insert = alt_ins; alt_ins = t;
311 t = cset; cset = alt_cset; alt_cset = t;
317 * Retrieve a character from `inbuf'.
319 static int inbuf_getc(void) {
320 if (inbuf_head == inbuf_reap)
324 inbuf_reap = (inbuf_reap+1) & INBUF_MASK;
330 * Update the scroll bar.
332 static void update_sbar(void) {
335 min = (sbtop - text) / (cols+1);
336 set_sbar ((scrtop - text) / (cols+1) + rows - min,
337 (disptop - text) / (cols+1) - min,
342 * Check whether the region bounded by the two pointers intersects
343 * the scroll region, and de-select the on-screen selection if so.
345 static void check_selection (unsigned long *from, unsigned long *to) {
346 if (from < selend && selstart < to)
351 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
352 * for backward.) `sb' is TRUE if the scrolling is permitted to
353 * affect the scrollback buffer.
355 static void scroll (int topline, int botline, int lines, int sb) {
356 unsigned long *scroll_top;
357 int scroll_size, size, i;
359 scroll_top = scrtop + topline*(cols+1);
360 size = (lines < 0 ? -lines : lines) * (cols+1);
361 scroll_size = (botline - topline + 1) * (cols+1) - size;
363 if (lines > 0 && topline == 0 && alt_which == 0 && sb) {
365 * Since we're going to scroll the top line and we're on the
366 * scrolling screen let's also affect the scrollback buffer.
368 sbtop -= lines * (cols+1);
371 scroll_size += scroll_top - sbtop;
376 if (scroll_size < 0) {
383 memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
384 for (i = 0; i < size; i++)
385 scroll_top[i+scroll_size] = ERASE_CHAR;
386 if (selstart > scroll_top &&
387 selstart < scroll_top + size + scroll_size) {
389 if (selstart < scroll_top)
390 selstart = scroll_top;
392 if (selend > scroll_top &&
393 selend < scroll_top + size + scroll_size) {
395 if (selend < scroll_top)
400 memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
401 for (i = 0; i < size; i++)
402 scroll_top[i] = ERASE_CHAR;
403 if (selstart > scroll_top &&
404 selstart < scroll_top + size + scroll_size) {
406 if (selstart > scroll_top + size + scroll_size)
407 selstart = scroll_top + size + scroll_size;
409 if (selend > scroll_top &&
410 selend < scroll_top + size + scroll_size) {
412 if (selend > scroll_top + size + scroll_size)
413 selend = scroll_top + size + scroll_size;
417 scroll_heuristic += lines;
421 * Move the cursor to a given position, clipping at boundaries. We
422 * may or may not want to clip at the scroll margin: marg_clip is 0
423 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
424 * even _being_ outside the margins.
426 static void move (int x, int y, int marg_clip) {
432 if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
434 if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
448 * Save or restore the cursor and SGR mode.
450 static void save_cursor(int save) {
454 save_attr = curr_attr;
456 save_csattr = cset_attr[cset];
460 curr_attr = save_attr;
462 cset_attr[cset] = save_csattr;
468 * Erase a large portion of the screen: the whole screen, or the
469 * whole line, or parts thereof.
471 static void erase_lots (int line_only, int from_begin, int to_end) {
472 unsigned long *startpos, *endpos;
475 startpos = cpos - curs_x;
476 endpos = startpos + cols+1;
479 endpos = startpos + rows * (cols+1);
485 check_selection (startpos, endpos);
486 while (startpos < endpos)
487 *startpos++ = ERASE_CHAR;
491 * Insert or delete characters within the current line. n is +ve if
492 * insertion is desired, and -ve for deletion.
494 static void insch (int n) {
495 int dir = (n < 0 ? -1 : +1);
498 n = (n < 0 ? -n : n);
499 if (n > cols - curs_x)
501 m = cols - curs_x - n;
502 check_selection (cpos, cpos+n);
504 memmove (cpos, cpos+n, m*TSIZE);
506 cpos[m++] = ERASE_CHAR;
508 memmove (cpos+n, cpos, m*TSIZE);
510 cpos[n] = ERASE_CHAR;
515 * Toggle terminal mode `mode' to state `state'. (`query' indicates
516 * whether the mode is a DEC private one or a normal one.)
518 static void toggle_mode (int mode, int query, int state) {
519 if (query) switch (mode) {
520 case 1: /* application cursor keys */
521 app_cursor_keys = state;
523 case 3: /* 80/132 columns */
525 request_resize (state ? 132 : 80, rows);
527 case 5: /* reverse video */
529 seen_disp_event = TRUE;
531 case 6: /* DEC origin mode */
534 case 7: /* auto wrap */
537 case 47: /* alternate screen */
542 } else switch (mode) {
543 case 4: /* set insert mode */
550 * Process an OSC sequence: set window title or icon name.
552 static void do_osc(void) {
555 wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
557 osc_string[osc_strlen] = '\0';
558 switch (esc_args[0]) {
561 set_icon (osc_string);
562 if (esc_args[0] == 1)
564 /* fall through: parameter 0 means set both */
567 set_title (osc_string);
574 * Remove everything currently in `inbuf' and stick it up on the
575 * in-memory display. There's a big state machine in here to
576 * process escape sequences...
578 void term_out(void) {
581 while ( (c = inbuf_getc()) != -1) {
584 static FILE *fp = NULL;
585 if (!fp) fp = fopen("putty.log", "wb");
586 if (fp) fputc (c, fp);
589 if( termstate < DO_CTRLS && (c&0x60) == 0 ) {
591 case '\005': /* terminal type query */
592 ldisc->send ("PuTTY\r", 6);
599 if (curs_x == 0 && curs_y > 0)
600 curs_x = cols-1, curs_y--;
606 seen_disp_event = TRUE;
615 termstate = SEEN_ESC;
618 termstate = SEEN_CSI;
620 esc_args[0] = ARG_DEFAULT;
624 termstate = SEEN_OSC;
631 seen_disp_event = TRUE;
636 if (curs_y == marg_b)
637 scroll (marg_t, marg_b, 1, TRUE);
638 else if (curs_y < rows-1)
650 } while (curs_x < cols-1 && !tabs[curs_x]);
654 unsigned long *old_cpos = cpos;
656 check_selection (old_cpos, cpos);
658 seen_disp_event = TRUE;
662 else switch (termstate) {
664 if (c >= ' ' && c != 0234) {
666 cpos[1] = ATTR_WRAPPED;
667 if (curs_y == marg_b)
668 scroll (marg_t, marg_b, 1, TRUE);
669 else if (curs_y < rows-1)
678 check_selection (cpos, cpos+1);
679 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
680 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
682 if (curs_x == cols) {
692 termstate = TOPLEVEL;
696 * This state is virtually identical to SEEN_ESC, with the
697 * exception that we have an OSC sequence in the pipeline,
698 * and _if_ we see a backslash, we process it.
702 termstate = TOPLEVEL;
705 /* else fall through */
707 termstate = TOPLEVEL;
709 case ' ': /* some weird sequence? */
710 termstate = IGNORE_NEXT;
712 case '[': /* enter CSI mode */
713 termstate = SEEN_CSI;
715 esc_args[0] = ARG_DEFAULT;
718 case ']': /* xterm escape sequences */
719 termstate = SEEN_OSC;
722 case '(': /* should set GL */
725 case ')': /* should set GR */
728 case '7': /* save cursor */
731 case '8': /* restore cursor */
733 seen_disp_event = TRUE;
736 app_keypad_keys = TRUE;
739 app_keypad_keys = FALSE;
741 case 'D': /* exactly equivalent to LF */
742 if (curs_y == marg_b)
743 scroll (marg_t, marg_b, 1, TRUE);
744 else if (curs_y < rows-1)
748 seen_disp_event = TRUE;
751 case 'E': /* exactly equivalent to CR-LF */
754 if (curs_y == marg_b)
755 scroll (marg_t, marg_b, 1, TRUE);
756 else if (curs_y < rows-1)
761 seen_disp_event = TRUE;
763 case 'M': /* reverse index - backwards LF */
764 if (curs_y == marg_t)
765 scroll (marg_t, marg_b, -1, TRUE);
770 seen_disp_event = TRUE;
772 case 'Z': /* terminal type query */
773 ldisc->send ("\033[?6c", 5);
775 case 'c': /* restore power-on settings */
779 seen_disp_event = TRUE;
781 case '#': /* ESC # 8 fills screen with Es :-) */
782 termstate = SEEN_ESCHASH;
784 case 'H': /* set a tab */
790 termstate = TOPLEVEL; /* default */
792 case '0': case '1': case '2': case '3': case '4':
793 case '5': case '6': case '7': case '8': case '9':
794 if (esc_nargs <= ARGS_MAX) {
795 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
796 esc_args[esc_nargs-1] = 0;
797 esc_args[esc_nargs-1] =
798 10 * esc_args[esc_nargs-1] + c - '0';
800 termstate = SEEN_CSI;
803 if (++esc_nargs <= ARGS_MAX)
804 esc_args[esc_nargs-1] = ARG_DEFAULT;
805 termstate = SEEN_CSI;
809 termstate = SEEN_CSI;
811 case 'A': /* move up N lines */
812 move (curs_x, curs_y - def(esc_args[0], 1), 1);
813 seen_disp_event = TRUE;
815 case 'B': case 'e': /* move down N lines */
816 move (curs_x, curs_y + def(esc_args[0], 1), 1);
817 seen_disp_event = TRUE;
819 case 'C': case 'a': /* move right N cols */
820 move (curs_x + def(esc_args[0], 1), curs_y, 1);
821 seen_disp_event = TRUE;
823 case 'D': /* move left N cols */
824 move (curs_x - def(esc_args[0], 1), curs_y, 1);
825 seen_disp_event = TRUE;
827 case 'E': /* move down N lines and CR */
828 move (0, curs_y + def(esc_args[0], 1), 1);
829 seen_disp_event = TRUE;
831 case 'F': /* move up N lines and CR */
832 move (0, curs_y - def(esc_args[0], 1), 1);
833 seen_disp_event = TRUE;
835 case 'G': case '`': /* set horizontal posn */
836 move (def(esc_args[0], 1) - 1, curs_y, 0);
837 seen_disp_event = TRUE;
839 case 'd': /* set vertical posn */
840 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
842 seen_disp_event = TRUE;
844 case 'H': case 'f': /* set horz and vert posns at once */
846 esc_args[1] = ARG_DEFAULT;
847 move (def(esc_args[1], 1) - 1,
848 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
850 seen_disp_event = TRUE;
852 case 'J': /* erase screen or parts of it */
854 unsigned int i = def(esc_args[0], 0) + 1;
857 erase_lots(FALSE, !!(i & 2), !!(i & 1));
860 seen_disp_event = TRUE;
862 case 'K': /* erase line or parts of it */
864 unsigned int i = def(esc_args[0], 0) + 1;
867 erase_lots(TRUE, !!(i & 2), !!(i & 1));
869 seen_disp_event = TRUE;
871 case 'L': /* insert lines */
872 if (curs_y <= marg_b)
873 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
874 seen_disp_event = TRUE;
876 case 'M': /* delete lines */
877 if (curs_y <= marg_b)
878 scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
879 seen_disp_event = TRUE;
881 case '@': /* insert chars */
882 insch (def(esc_args[0], 1));
883 seen_disp_event = TRUE;
885 case 'P': /* delete chars */
886 insch (-def(esc_args[0], 1));
887 seen_disp_event = TRUE;
889 case 'c': /* terminal type query */
890 ldisc->send ("\033[?6c", 5);
892 case 'n': /* cursor position query */
893 if (esc_args[0] == 6) {
895 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
896 ldisc->send (buf, strlen(buf));
899 case 'h': /* toggle a mode to high */
900 toggle_mode (esc_args[0], esc_query, TRUE);
902 case 'l': /* toggle a mode to low */
903 toggle_mode (esc_args[0], esc_query, FALSE);
905 case 'g': /* clear tabs */
906 if (esc_nargs == 1) {
907 if (esc_args[0] == 0) {
908 tabs[curs_x] = FALSE;
909 } else if (esc_args[0] == 3) {
911 for (i = 0; i < cols; i++)
916 case 'r': /* set scroll margins */
917 if (!esc_query && esc_nargs <= 2) {
919 top = def(esc_args[0], 1) - 1;
922 bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
923 def(esc_args[1], rows)) - 1;
931 * I used to think the cursor should be
932 * placed at the top of the newly marginned
933 * area. Apparently not: VMS TPU falls over
938 seen_disp_event = TRUE;
942 case 'm': /* set graphics rendition */
945 for (i=0; i<esc_nargs; i++) {
946 switch (def(esc_args[i], 0)) {
947 case 0: /* restore defaults */
948 curr_attr = ATTR_DEFAULT; break;
949 case 1: /* enable bold */
950 curr_attr |= ATTR_BOLD; break;
951 case 4: /* enable underline */
952 case 21: /* (enable double underline) */
953 curr_attr |= ATTR_UNDER; break;
954 case 7: /* enable reverse video */
955 curr_attr |= ATTR_REVERSE; break;
956 case 22: /* disable bold */
957 curr_attr &= ~ATTR_BOLD; break;
958 case 24: /* disable underline */
959 curr_attr &= ~ATTR_UNDER; break;
960 case 27: /* disable reverse video */
961 curr_attr &= ~ATTR_REVERSE; break;
962 case 30: case 31: case 32: case 33:
963 case 34: case 35: case 36: case 37:
965 curr_attr &= ~ATTR_FGMASK;
966 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
968 case 39: /* default-foreground */
969 curr_attr &= ~ATTR_FGMASK;
970 curr_attr |= ATTR_DEFFG;
972 case 40: case 41: case 42: case 43:
973 case 44: case 45: case 46: case 47:
975 curr_attr &= ~ATTR_BGMASK;
976 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
978 case 49: /* default-background */
979 curr_attr &= ~ATTR_BGMASK;
980 curr_attr |= ATTR_DEFBG;
986 case 's': /* save cursor */
989 case 'u': /* restore cursor */
991 seen_disp_event = TRUE;
993 case 't': /* set page size - ie window height */
994 request_resize (cols, def(esc_args[0], 24));
997 case 'X': /* write N spaces w/o moving cursor */
999 int n = def(esc_args[0], 1);
1000 unsigned long *p = cpos;
1001 if (n > cols - curs_x)
1003 check_selection (cpos, cpos+n);
1006 seen_disp_event = TRUE;
1009 case 'x': /* report terminal characteristics */
1012 int i = def(esc_args[0], 0);
1013 if (i == 0 || i == 1) {
1014 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1016 ldisc->send (buf, 20);
1026 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1029 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1031 default: /* specifically, 'B' */
1032 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1035 termstate = TOPLEVEL;
1040 case 'P': /* Linux palette sequence */
1041 termstate = SEEN_OSC_P;
1044 case 'R': /* Linux palette reset */
1047 termstate = TOPLEVEL;
1049 case 'W': /* word-set */
1050 termstate = SEEN_OSC_W;
1053 case '0': case '1': case '2': case '3': case '4':
1054 case '5': case '6': case '7': case '8': case '9':
1055 esc_args[0] = 10 * esc_args[0] + c - '0';
1059 * Grotty hack to support xterm and DECterm title
1060 * sequences concurrently.
1062 if (esc_args[0] == 2) {
1066 /* else fall through */
1068 termstate = OSC_STRING;
1073 if (c == 0234 || c == '\007') {
1075 * These characters terminate the string; ST and BEL
1076 * terminate the sequence and trigger instant
1077 * processing of it, whereas ESC goes back to SEEN_ESC
1078 * mode unless it is followed by \, in which case it is
1079 * synonymous with ST in the first place.
1082 termstate = TOPLEVEL;
1083 } else if (c == '\033')
1084 termstate = OSC_MAYBE_ST;
1085 else if (osc_strlen < OSC_STR_MAX)
1086 osc_string[osc_strlen++] = c;
1090 int max = (osc_strlen == 0 ? 21 : 16);
1092 if (c >= '0' && c <= '9')
1094 else if (c >= 'A' && c <= 'A'+max-10)
1096 else if (c >= 'a' && c <= 'a'+max-10)
1099 termstate = TOPLEVEL;
1100 osc_string[osc_strlen++] = val;
1101 if (osc_strlen >= 7) {
1102 palette_set (osc_string[0],
1103 osc_string[1] * 16 + osc_string[2],
1104 osc_string[3] * 16 + osc_string[4],
1105 osc_string[5] * 16 + osc_string[6]);
1107 termstate = TOPLEVEL;
1113 case '0': case '1': case '2': case '3': case '4':
1114 case '5': case '6': case '7': case '8': case '9':
1115 esc_args[0] = 10 * esc_args[0] + c - '0';
1118 termstate = OSC_STRING;
1124 unsigned long *p = scrtop;
1125 int n = rows * (cols+1);
1127 *p++ = ATTR_DEFAULT | 'E';
1129 seen_disp_event = TRUE;
1130 check_selection (scrtop, scrtop + rows * (cols+1));
1132 termstate = TOPLEVEL;
1135 check_selection (cpos, cpos+1);
1138 if (nl_count > MAXNL)
1143 * Compare two lines to determine whether they are sufficiently
1144 * alike to scroll-optimise one to the other. Return the degree of
1147 static int linecmp (unsigned long *a, unsigned long *b) {
1150 for (i=n=0; i < cols; i++)
1151 n += (*a++ == *b++);
1156 * Given a context, update the window. Out of paranoia, we don't
1157 * allow WM_PAINT responses to do scrolling optimisations.
1159 static void do_paint (Context ctx, int may_optimise){
1160 int i, j, start, our_curs_y;
1161 unsigned long attr, rv, cursor;
1164 cursor = (has_focus ? ATTR_ACTCURS : ATTR_PASCURS);
1165 rv = (rvideo ? ATTR_REVERSE : 0);
1166 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1168 for (i=0; i<rows; i++) {
1169 int idx = i*(cols+1);
1170 for (j=0; j<=cols; j++,idx++) {
1171 unsigned long *d = disptop+idx;
1172 wanttext[idx] = ((*d ^ rv
1173 ^ (selstart <= d && d < selend ?
1174 ATTR_REVERSE : 0)) |
1175 (i==our_curs_y && j==curs_x ? cursor : 0));
1180 * We would perform scrolling optimisations in here, if they
1181 * didn't have a nasty tendency to cause the whole sodding
1182 * program to hang for a second at speed-critical moments.
1183 * We'll leave it well alone...
1186 for (i=0; i<rows; i++) {
1187 int idx = i*(cols+1);
1189 for (j=0; j<=cols; j++,idx++) {
1190 unsigned long t = wanttext[idx];
1191 int needs_update = (j < cols && t != disptext[idx]);
1192 int keep_going = (start != -1 && needs_update &&
1193 (t & ATTR_MASK) == attr &&
1194 j-start < sizeof(ch));
1195 if (start != -1 && !keep_going) {
1196 do_text (ctx, start, i, ch, j-start, attr);
1202 attr = t & ATTR_MASK;
1204 ch[j-start] = (char) (t & CHAR_MASK);
1212 * Invalidate the whole screen so it will be repainted in full.
1214 void term_invalidate(void) {
1217 for (i=0; i<rows*(cols+1); i++)
1218 disptext[i] = ATTR_INVALID;
1222 * Paint the window in response to a WM_PAINT message.
1224 void term_paint (Context ctx, int l, int t, int r, int b) {
1225 int i, j, left, top, right, bottom;
1227 left = l / font_width;
1228 right = (r - 1) / font_width;
1229 top = t / font_height;
1230 bottom = (b - 1) / font_height;
1231 for (i = top; i <= bottom && i < rows ; i++)
1232 for (j = left; j <= right && j < cols ; j++)
1233 disptext[i*(cols+1)+j] = ATTR_INVALID;
1235 do_paint (ctx, FALSE);
1239 * Attempt to scroll the scrollback. The second parameter gives the
1240 * position we want to scroll to; the first is +1 to denote that
1241 * this position is relative to the beginning of the scrollback, -1
1242 * to denote it is relative to the end, and 0 to denote that it is
1243 * relative to the current position.
1245 void term_scroll (int rel, int where) {
1246 int n = where * (cols+1);
1248 disptop = (rel < 0 ? scrtop :
1249 rel > 0 ? sbtop : disptop) + n;
1250 if (disptop < sbtop)
1252 if (disptop > scrtop)
1259 * Spread the selection outwards according to the selection mode.
1261 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1262 unsigned long *linestart, *lineend;
1266 x = (p - text) % (cols+1);
1268 lineend = linestart + cols;
1273 * In this mode, every character is a separate unit, except
1274 * for runs of spaces at the end of a non-wrapping line.
1276 if (!(linestart[cols] & ATTR_WRAPPED)) {
1277 unsigned long *q = lineend;
1278 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1283 p = (dir == -1 ? q : lineend - 1);
1288 * In this mode, the units are maximal runs of characters
1289 * whose `wordness' has the same value.
1291 wvalue = wordness[*p & CHAR_MASK];
1293 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1296 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1302 * In this mode, every line is a unit.
1304 p = (dir == -1 ? linestart : lineend - 1);
1310 static void sel_spread (void) {
1311 selstart = sel_spread_half (selstart, -1);
1312 selend = sel_spread_half (selend - 1, +1) + 1;
1315 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
1316 unsigned long *selpoint;
1319 if (y>=rows) y = rows-1;
1327 if (x>=cols) x = cols-1;
1329 selpoint = disptop + y * (cols+1) + x;
1331 if (b == MB_SELECT && a == MA_CLICK) {
1333 selstate = ABOUT_TO;
1334 selanchor = selpoint;
1336 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1338 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1339 selstate = DRAGGING;
1340 selstart = selanchor = selpoint;
1341 selend = selstart + 1;
1343 } else if ((b == MB_SELECT && a == MA_DRAG) ||
1344 (b == MB_EXTEND && a != MA_RELEASE)) {
1345 if (selstate == ABOUT_TO && selanchor == selpoint)
1347 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1348 if (selpoint-selstart < (selend-selstart)/2)
1349 selanchor = selend - 1;
1351 selanchor = selstart;
1352 selstate = DRAGGING;
1354 if (selstate != ABOUT_TO && selstate != DRAGGING)
1355 selanchor = selpoint;
1356 selstate = DRAGGING;
1357 if (selpoint < selanchor) {
1358 selstart = selpoint;
1359 selend = selanchor + 1;
1361 selstart = selanchor;
1362 selend = selpoint + 1;
1365 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
1366 if (selstate == DRAGGING) {
1368 * We've completed a selection. We now transfer the
1369 * data to the clipboard.
1371 unsigned char *p = selspace;
1372 unsigned long *q = selstart;
1374 while (q < selend) {
1376 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1377 unsigned long *nlpos = lineend;
1379 if (!(*nlpos & ATTR_WRAPPED)) {
1380 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1385 while (q < nlpos && q < selend)
1386 *p++ = (unsigned char) (*q++ & CHAR_MASK);
1389 for (i=0; i<sizeof(sel_nl); i++)
1392 q = lineend + 1; /* start of next line */
1394 write_clip (selspace, p - selspace);
1395 selstate = SELECTED;
1397 selstate = NO_SELECTION;
1398 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
1402 get_clip((void **) &data, &len);
1406 while (p < data+len) {
1407 while (p < data+len &&
1408 !(p <= data+len-sizeof(sel_nl) &&
1409 !memcmp(p, sel_nl, sizeof(sel_nl))))
1417 c=xlat_kbd2tty(q[i]);
1422 if (p <= data+len-sizeof(sel_nl) &&
1423 !memcmp(p, sel_nl, sizeof(sel_nl))) {
1424 ldisc->send ("\r", 1);
1425 p += sizeof(sel_nl);
1430 get_clip(NULL, NULL);
1436 static void deselect (void) {
1437 selstate = NO_SELECTION;
1438 selstart = selend = scrtop;
1441 void term_deselect (void) {