3 #endif /* not macintosh */
10 static unsigned long *text; /* buffer of text on terminal screen */
11 static unsigned long *scrtop; /* top of working screen */
12 static unsigned long *disptop; /* top of displayed screen */
13 static unsigned long *sbtop; /* top of scrollback */
14 static unsigned long *cpos; /* cursor position (convenience) */
15 static unsigned long *disptext; /* buffer of text on real screen */
16 static unsigned long *wanttext; /* buffer of text we want on screen */
17 static unsigned long *alttext; /* buffer of text on alt. screen */
19 static unsigned char *selspace; /* buffer for building selections in */
21 #define TSIZE (sizeof(*text))
22 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
24 static unsigned long curr_attr, save_attr;
26 static int curs_x, curs_y; /* cursor */
27 static int save_x, save_y; /* saved cursor position */
28 static int marg_t, marg_b; /* scroll margins */
29 static int dec_om; /* DEC origin mode flag */
30 static int wrap, wrapnext; /* wrap flags */
31 static int insert; /* insert-mode flag */
32 static int cset; /* 0 or 1: which char set */
33 static int save_cset, save_csattr; /* saved with cursor position */
34 static int rvideo; /* global reverse video flag */
36 static unsigned long cset_attr[2];
39 * Saved settings on the alternate screen.
41 static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
42 static int alt_t, alt_b;
45 #define ARGS_MAX 32 /* max # of esc sequence arguments */
46 #define ARG_DEFAULT -1 /* if an arg isn't specified */
47 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
48 static int esc_args[ARGS_MAX];
52 #define OSC_STR_MAX 2048
53 static int osc_strlen;
54 static char osc_string[OSC_STR_MAX+1];
57 static unsigned char *tabs;
62 static int scroll_heuristic;
65 TOPLEVEL, IGNORE_NEXT,
66 SEEN_ESC, SEEN_CSI, SET_GL, SET_GR,
67 SEEN_OSC, SEEN_OSC_P, SEEN_OSC_W, OSC_STRING, OSC_MAYBE_ST,
72 NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
75 SM_CHAR, SM_WORD, SM_LINE
77 static unsigned long *selstart, *selend, *selanchor;
79 static short wordness[256] = {
80 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 */
81 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 */
82 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 */
83 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 */
84 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 */
85 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 */
86 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 */
87 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 */
90 static unsigned char sel_nl[] = SEL_NL;
93 * Internal prototypes.
95 static void do_paint (Context, int);
96 static void erase_lots (int, int, int);
97 static void swap_screen (int);
98 static void update_sbar (void);
99 static void deselect (void);
102 * Set up power-on settings for the terminal.
104 static void power_on(void) {
105 curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
108 alt_b = marg_b = rows - 1;
113 for (i = 0; i < cols; i++)
114 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
116 alt_om = dec_om = cfg.dec_om;
117 alt_wnext = wrapnext = alt_ins = insert = FALSE;
118 alt_wrap = wrap = cfg.wrap_mode;
120 cset_attr[0] = cset_attr[1] = ATTR_ASCII;
122 save_attr = curr_attr = ATTR_DEFAULT;
123 app_cursor_keys = cfg.app_cursor;
124 app_keypad_keys = cfg.app_keypad;
128 for (i = 0; i < 256; i++)
129 wordness[i] = cfg.wordness[i];
133 erase_lots (FALSE, TRUE, TRUE);
135 erase_lots (FALSE, TRUE, TRUE);
140 * Force a screen update.
142 void term_update(void) {
146 do_paint (ctx, TRUE);
149 scroll_heuristic = 0;
154 * Same as power_on(), but an external function.
156 void term_pwron(void) {
165 * Clear the scrollback.
167 void term_clrsb(void) {
168 disptop = sbtop = scrtop;
173 * Initialise the terminal.
175 void term_init(void) {
176 text = sbtop = scrtop = disptop = cpos = NULL;
177 disptext = wanttext = NULL;
187 * Set up the terminal for a given size.
189 void term_size(int newrows, int newcols, int newsavelines) {
190 unsigned long *newtext, *newdisp, *newwant, *newalt;
191 int i, j, crows, ccols;
193 if (newrows == rows && newcols == cols && newsavelines == savelines)
194 return; /* nothing to do */
197 alt_b = marg_b = newrows - 1;
199 newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
200 disptop = newtext + newsavelines*(newcols+1);
201 for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
202 newtext[i] = ERASE_CHAR;
204 crows = rows + (scrtop - sbtop) / (cols+1);
205 if (crows > newrows+newsavelines)
206 crows = newrows+newsavelines;
207 ccols = (cols < newcols ? cols : newcols);
208 for (i=0; i<crows; i++) {
209 int oldidx = (rows + savelines - crows + i) * (cols+1);
210 int newidx = (newrows + newsavelines - crows + i) * (newcols+1);
211 for (j=0; j<ccols; j++)
212 newtext[newidx+j] = text[oldidx+j];
213 newtext[newidx+newcols] =
214 (cols == newcols ? text[oldidx+cols] : 0);
216 sbtop = disptop - (crows - newrows) * (newcols+1);
225 newdisp = smalloc (newrows*(newcols+1)*TSIZE);
226 for (i=0; i<newrows*(newcols+1); i++)
227 newdisp[i] = ATTR_INVALID;
231 newwant = smalloc (newrows*(newcols+1)*TSIZE);
232 for (i=0; i<newrows*(newcols+1); i++)
233 newwant[i] = ATTR_INVALID;
237 newalt = smalloc (newrows*(newcols+1)*TSIZE);
238 for (i=0; i<newrows*(newcols+1); i++)
239 newalt[i] = ERASE_CHAR;
244 selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
246 tabs = srealloc (tabs, newcols*sizeof(*tabs));
249 for (i = (cols > 0 ? cols : 0); i < newcols; i++)
250 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
254 curs_y += newrows - rows;
257 if (curs_y >= newrows)
259 if (curs_x >= newcols)
262 wrapnext = alt_wnext = FALSE;
266 savelines = newsavelines;
277 static void swap_screen (int which) {
281 if (which == alt_which)
286 for (t=0; t<rows*(cols+1); t++) {
287 tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
290 t = curs_x; curs_x = alt_x; alt_x = t;
291 t = curs_y; curs_y = alt_y; alt_y = t;
292 t = marg_t; marg_t = alt_t; alt_t = t;
293 t = marg_b; marg_b = alt_b; alt_b = t;
294 t = dec_om; dec_om = alt_om; alt_om = t;
295 t = wrap; wrap = alt_wrap; alt_wrap = t;
296 t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
297 t = insert; insert = alt_ins; alt_ins = t;
298 t = cset; cset = alt_cset; alt_cset = t;
304 * Retrieve a character from `inbuf'.
306 static int inbuf_getc(void) {
307 if (inbuf_head == inbuf_reap)
311 inbuf_reap = (inbuf_reap+1) & INBUF_MASK;
317 * Update the scroll bar.
319 static void update_sbar(void) {
322 min = (sbtop - text) / (cols+1);
323 set_sbar ((scrtop - text) / (cols+1) + rows - min,
324 (disptop - text) / (cols+1) - min,
329 * Check whether the region bounded by the two pointers intersects
330 * the scroll region, and de-select the on-screen selection if so.
332 static void check_selection (unsigned long *from, unsigned long *to) {
333 if (from < selend && selstart < to)
338 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
339 * for backward.) `sb' is TRUE if the scrolling is permitted to
340 * affect the scrollback buffer.
342 static void scroll (int topline, int botline, int lines, int sb) {
343 unsigned long *scroll_top;
344 int scroll_size, size, i;
346 scroll_top = scrtop + topline*(cols+1);
347 size = (lines < 0 ? -lines : lines) * (cols+1);
348 scroll_size = (botline - topline + 1) * (cols+1) - size;
350 if (lines > 0 && topline == 0 && botline == (rows-1) && sb) {
352 * Since we're going to scroll the whole screen upwards,
353 * let's also affect the scrollback buffer.
355 sbtop -= lines * (cols+1);
358 scroll_size += scroll_top - sbtop;
363 if (scroll_size < 0) {
370 memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
371 for (i = 0; i < size; i++)
372 scroll_top[i+scroll_size] = ERASE_CHAR;
373 if (selstart > scroll_top &&
374 selstart < scroll_top + size + scroll_size) {
376 if (selstart < scroll_top)
377 selstart = scroll_top;
379 if (selend > scroll_top &&
380 selend < scroll_top + size + scroll_size) {
382 if (selend < scroll_top)
387 memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
388 for (i = 0; i < size; i++)
389 scroll_top[i] = ERASE_CHAR;
390 if (selstart > scroll_top &&
391 selstart < scroll_top + size + scroll_size) {
393 if (selstart > scroll_top + size + scroll_size)
394 selstart = scroll_top + size + scroll_size;
396 if (selend > scroll_top &&
397 selend < scroll_top + size + scroll_size) {
399 if (selend > scroll_top + size + scroll_size)
400 selend = scroll_top + size + scroll_size;
404 scroll_heuristic += lines;
408 * Move the cursor to a given position, clipping at boundaries. We
409 * may or may not want to clip at the scroll margin: marg_clip is 0
410 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
411 * even _being_ outside the margins.
413 static void move (int x, int y, int marg_clip) {
419 if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
421 if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
435 * Save or restore the cursor and SGR mode.
437 static void save_cursor(int save) {
441 save_attr = curr_attr;
443 save_csattr = cset_attr[cset];
447 curr_attr = save_attr;
449 cset_attr[cset] = save_csattr;
455 * Erase a large portion of the screen: the whole screen, or the
456 * whole line, or parts thereof.
458 static void erase_lots (int line_only, int from_begin, int to_end) {
459 unsigned long *startpos, *endpos;
462 startpos = cpos - curs_x;
463 endpos = startpos + cols+1;
466 endpos = startpos + rows * (cols+1);
472 check_selection (startpos, endpos);
473 while (startpos < endpos)
474 *startpos++ = ERASE_CHAR;
478 * Insert or delete characters within the current line. n is +ve if
479 * insertion is desired, and -ve for deletion.
481 static void insch (int n) {
482 int dir = (n < 0 ? -1 : +1);
485 n = (n < 0 ? -n : n);
486 if (n > cols - curs_x)
488 m = cols - curs_x - n;
489 check_selection (cpos, cpos+n);
491 memmove (cpos, cpos+n, m*TSIZE);
493 cpos[m++] = ERASE_CHAR;
495 memmove (cpos+n, cpos, m*TSIZE);
497 cpos[n] = ERASE_CHAR;
502 * Toggle terminal mode `mode' to state `state'. (`query' indicates
503 * whether the mode is a DEC private one or a normal one.)
505 static void toggle_mode (int mode, int query, int state) {
506 if (query) switch (mode) {
507 case 1: /* application cursor keys */
508 app_cursor_keys = state;
510 case 3: /* 80/132 columns */
512 request_resize (state ? 132 : 80, rows);
514 case 5: /* reverse video */
518 case 6: /* DEC origin mode */
521 case 7: /* auto wrap */
524 case 47: /* alternate screen */
529 } else switch (mode) {
530 case 4: /* set insert mode */
537 * Process an OSC sequence: set window title or icon name.
539 static void do_osc(void) {
542 wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
544 osc_string[osc_strlen] = '\0';
545 switch (esc_args[0]) {
548 set_icon (osc_string);
549 if (esc_args[0] == 1)
551 /* fall through: parameter 0 means set both */
554 set_title (osc_string);
561 * Remove everything currently in `inbuf' and stick it up on the
562 * in-memory display. There's a big state machine in here to
563 * process escape sequences...
565 void term_out(void) {
567 int must_update = FALSE;
569 while ( (c = inbuf_getc()) != -1) {
572 static FILE *fp = NULL;
573 if (!fp) fp = fopen("putty.log", "wb");
574 if (fp) fputc (c, fp);
581 case '\005': /* terminal type query */
582 back->send ("\033[?1;2c", 7);
590 if (curs_x == 0 && curs_y > 0)
591 curs_x = cols-1, curs_y--;
607 termstate = SEEN_ESC;
610 termstate = SEEN_CSI;
612 esc_args[0] = ARG_DEFAULT;
616 termstate = SEEN_OSC;
629 if (curs_y == marg_b)
630 scroll (marg_t, marg_b, 1, TRUE);
631 else if (curs_y < rows-1)
643 } while (curs_x < cols-1 && !tabs[curs_x]);
647 unsigned long *old_cpos = cpos;
649 check_selection (old_cpos, cpos);
655 if (c >= ' ' && c != 0234) {
657 cpos[1] = ATTR_WRAPPED;
658 if (curs_y == marg_b)
659 scroll (marg_t, marg_b, 1, TRUE);
660 else if (curs_y < rows-1)
669 check_selection (cpos, cpos+1);
670 *cpos++ = c | curr_attr |
671 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
673 if (curs_x == cols) {
683 termstate = TOPLEVEL;
687 * This state is virtually identical to SEEN_ESC, with the
688 * exception that we have an OSC sequence in the pipeline,
689 * and _if_ we see a backslash, we process it.
693 termstate = TOPLEVEL;
696 /* else fall through */
698 termstate = TOPLEVEL;
700 case '\005': case '\007': case '\b': case '\016': case '\017':
701 case '\033': case 0233: case 0234: case 0235: case '\r':
702 case '\013': case '\014': case '\n': case '\t':
703 termstate = TOPLEVEL;
704 goto do_toplevel; /* hack... */
705 case ' ': /* some weird sequence? */
706 termstate = IGNORE_NEXT;
708 case '[': /* enter CSI mode */
709 termstate = SEEN_CSI;
711 esc_args[0] = ARG_DEFAULT;
714 case ']': /* xterm escape sequences */
715 termstate = SEEN_OSC;
718 case '(': /* should set GL */
721 case ')': /* should set GR */
724 case '7': /* save cursor */
727 case '8': /* restore cursor */
733 app_keypad_keys = TRUE;
736 app_keypad_keys = FALSE;
738 case 'D': /* exactly equivalent to LF */
739 if (curs_y == marg_b)
740 scroll (marg_t, marg_b, 1, TRUE);
741 else if (curs_y < rows-1)
748 case 'E': /* exactly equivalent to CR-LF */
751 if (curs_y == marg_b)
752 scroll (marg_t, marg_b, 1, TRUE);
753 else if (curs_y < rows-1)
760 case 'M': /* reverse index - backwards LF */
761 if (curs_y == marg_t)
762 scroll (marg_t, marg_b, -1, TRUE);
770 case 'Z': /* terminal type query */
771 back->send ("\033[?6c", 5);
773 case 'c': /* restore power-on settings */
779 case '#': /* ESC # 8 fills screen with Es :-) */
780 termstate = SEEN_ESCHASH;
782 case 'H': /* set a tab */
788 termstate = TOPLEVEL; /* default */
790 case '\005': case '\007': case '\b': case '\016': case '\017':
791 case '\033': case 0233: case 0234: case 0235: case '\r':
792 case '\013': case '\014': case '\n': case '\t':
793 termstate = TOPLEVEL;
794 goto do_toplevel; /* hack... */
795 case '0': case '1': case '2': case '3': case '4':
796 case '5': case '6': case '7': case '8': case '9':
797 if (esc_nargs <= ARGS_MAX) {
798 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
799 esc_args[esc_nargs-1] = 0;
800 esc_args[esc_nargs-1] =
801 10 * esc_args[esc_nargs-1] + c - '0';
803 termstate = SEEN_CSI;
806 if (++esc_nargs <= ARGS_MAX)
807 esc_args[esc_nargs-1] = ARG_DEFAULT;
808 termstate = SEEN_CSI;
812 termstate = SEEN_CSI;
814 case 'A': /* move up N lines */
815 move (curs_x, curs_y - def(esc_args[0], 1), 1);
819 case 'B': case 'e': /* move down N lines */
820 move (curs_x, curs_y + def(esc_args[0], 1), 1);
824 case 'C': case 'a': /* move right N cols */
825 move (curs_x + def(esc_args[0], 1), curs_y, 1);
829 case 'D': /* move left N cols */
830 move (curs_x - def(esc_args[0], 1), curs_y, 1);
834 case 'E': /* move down N lines and CR */
835 move (0, curs_y + def(esc_args[0], 1), 1);
839 case 'F': /* move up N lines and CR */
840 move (0, curs_y - def(esc_args[0], 1), 1);
844 case 'G': case '`': /* set horizontal posn */
845 move (def(esc_args[0], 1) - 1, curs_y, 0);
849 case 'd': /* set vertical posn */
850 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
855 case 'H': case 'f': /* set horz and vert posns at once */
857 esc_args[1] = ARG_DEFAULT;
858 move (def(esc_args[1], 1) - 1,
859 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
864 case 'J': /* erase screen or parts of it */
866 unsigned int i = def(esc_args[0], 0) + 1;
869 erase_lots(FALSE, !!(i & 2), !!(i & 1));
874 case 'K': /* erase line or parts of it */
876 unsigned int i = def(esc_args[0], 0) + 1;
879 erase_lots(TRUE, !!(i & 2), !!(i & 1));
884 case 'L': /* insert lines */
885 if (curs_y <= marg_b)
886 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
890 case 'M': /* delete lines */
891 if (curs_y <= marg_b)
892 scroll (curs_y, marg_b, def(esc_args[0], 1), FALSE);
896 case '@': /* insert chars */
897 insch (def(esc_args[0], 1));
901 case 'P': /* delete chars */
902 insch (-def(esc_args[0], 1));
906 case 'c': /* terminal type query */
907 back->send ("\033[?6c", 5);
909 case 'n': /* cursor position query */
910 if (esc_args[0] == 6) {
912 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
913 back->send (buf, strlen(buf));
916 case 'h': /* toggle a mode to high */
917 toggle_mode (esc_args[0], esc_query, TRUE);
919 case 'l': /* toggle a mode to low */
920 toggle_mode (esc_args[0], esc_query, FALSE);
922 case 'g': /* clear tabs */
923 if (esc_nargs == 1) {
924 if (esc_args[0] == 0) {
925 tabs[curs_x] = FALSE;
926 } else if (esc_args[0] == 3) {
928 for (i = 0; i < cols; i++)
933 case 'r': /* set scroll margins */
934 if (esc_nargs <= 2) {
936 top = def(esc_args[0], 1) - 1;
939 bot = (esc_nargs == 1 ? rows :
940 def(esc_args[1], rows)) - 1;
948 * I used to think the cursor should be
949 * placed at the top of the newly marginned
950 * area. Apparently not: VMS TPU falls over
960 case 'm': /* set graphics rendition */
963 for (i=0; i<esc_nargs; i++) {
964 switch (def(esc_args[i], 0)) {
965 case 0: /* restore defaults */
966 curr_attr = ATTR_DEFAULT; break;
967 case 1: /* enable bold */
968 curr_attr |= ATTR_BOLD; break;
969 case 4: /* enable underline */
970 case 21: /* (enable double underline) */
971 curr_attr |= ATTR_UNDER; break;
972 case 7: /* enable reverse video */
973 curr_attr |= ATTR_REVERSE; break;
974 case 22: /* disable bold */
975 curr_attr &= ~ATTR_BOLD; break;
976 case 24: /* disable underline */
977 curr_attr &= ~ATTR_UNDER; break;
978 case 27: /* disable reverse video */
979 curr_attr &= ~ATTR_REVERSE; break;
980 case 30: case 31: case 32: case 33:
981 case 34: case 35: case 36: case 37:
983 curr_attr &= ~ATTR_FGMASK;
984 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
986 case 39: /* default-foreground */
987 curr_attr &= ~ATTR_FGMASK;
988 curr_attr |= ATTR_DEFFG;
990 case 40: case 41: case 42: case 43:
991 case 44: case 45: case 46: case 47:
993 curr_attr &= ~ATTR_BGMASK;
994 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
996 case 49: /* default-background */
997 curr_attr &= ~ATTR_BGMASK;
998 curr_attr |= ATTR_DEFBG;
1004 case 's': /* save cursor */
1007 case 'u': /* restore cursor */
1008 save_cursor (FALSE);
1012 case 't': /* set page size - ie window height */
1013 request_resize (cols, def(esc_args[0], 24));
1016 case 'X': /* write N spaces w/o moving cursor */
1018 int n = def(esc_args[0], 1);
1019 unsigned long *p = cpos;
1020 if (n > cols - curs_x)
1022 check_selection (cpos, cpos+n);
1029 case 'x': /* report terminal characteristics */
1032 int i = def(esc_args[0], 0);
1033 if (i == 0 || i == 1) {
1034 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1036 back->send (buf, 20);
1046 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1049 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1051 default: /* specifically, 'B' */
1052 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1055 termstate = TOPLEVEL;
1060 case '\005': case '\007': case '\b': case '\016': case '\017':
1061 case '\033': case 0233: case 0234: case 0235: case '\r':
1062 case '\013': case '\014': case '\n': case '\t':
1063 termstate = TOPLEVEL;
1064 goto do_toplevel; /* hack... */
1065 case 'P': /* Linux palette sequence */
1066 termstate = SEEN_OSC_P;
1069 case 'R': /* Linux palette reset */
1072 termstate = TOPLEVEL;
1074 case 'W': /* word-set */
1075 termstate = SEEN_OSC_W;
1078 case '0': case '1': case '2': case '3': case '4':
1079 case '5': case '6': case '7': case '8': case '9':
1080 esc_args[0] = 10 * esc_args[0] + c - '0';
1084 * Grotty hack to support xterm and DECterm title
1085 * sequences concurrently.
1087 if (esc_args[0] == 2) {
1091 /* else fall through */
1093 termstate = OSC_STRING;
1098 if (c == 0234 || c == '\007') {
1100 * These characters terminate the string; ST and BEL
1101 * terminate the sequence and trigger instant
1102 * processing of it, whereas ESC goes back to SEEN_ESC
1103 * mode unless it is followed by \, in which case it is
1104 * synonymous with ST in the first place.
1107 termstate = TOPLEVEL;
1108 } else if (c == '\033')
1109 termstate = OSC_MAYBE_ST;
1110 else if (osc_strlen < OSC_STR_MAX)
1111 osc_string[osc_strlen++] = c;
1115 int max = (osc_strlen == 0 ? 21 : 16);
1117 if (c >= '0' && c <= '9')
1119 else if (c >= 'A' && c <= 'A'+max-10)
1121 else if (c >= 'a' && c <= 'a'+max-10)
1124 termstate = TOPLEVEL;
1125 osc_string[osc_strlen++] = val;
1126 if (osc_strlen >= 7) {
1127 palette_set (osc_string[0],
1128 osc_string[1] * 16 + osc_string[2],
1129 osc_string[3] * 16 + osc_string[4],
1130 osc_string[5] * 16 + osc_string[6]);
1132 termstate = TOPLEVEL;
1138 case '\005': case '\007': case '\b': case '\016': case '\017':
1139 case '\033': case 0233: case 0234: case 0235: case '\r':
1140 case '\013': case '\014': case '\n': case '\t':
1141 termstate = TOPLEVEL;
1142 goto do_toplevel; /* hack... */
1143 case '0': case '1': case '2': case '3': case '4':
1144 case '5': case '6': case '7': case '8': case '9':
1145 esc_args[0] = 10 * esc_args[0] + c - '0';
1148 termstate = OSC_STRING;
1154 unsigned long *p = scrtop;
1155 int n = rows * (cols+1);
1157 *p++ = ATTR_DEFAULT | 'E';
1160 check_selection (scrtop, scrtop + rows * (cols+1));
1162 termstate = TOPLEVEL;
1165 check_selection (cpos, cpos+1);
1168 if (must_update || nl_count > MAXNL)
1173 * Compare two lines to determine whether they are sufficiently
1174 * alike to scroll-optimise one to the other. Return the degree of
1177 static int linecmp (unsigned long *a, unsigned long *b) {
1180 for (i=n=0; i < cols; i++)
1181 n += (*a++ == *b++);
1186 * Given a context, update the window. Out of paranoia, we don't
1187 * allow WM_PAINT responses to do scrolling optimisations.
1189 static void do_paint (Context ctx, int may_optimise){
1190 int i, j, start, our_curs_y;
1191 unsigned long attr, rv, cursor;
1194 cursor = (has_focus ? ATTR_ACTCURS : ATTR_PASCURS);
1195 rv = (rvideo ? ATTR_REVERSE : 0);
1196 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1198 for (i=0; i<rows; i++) {
1199 int idx = i*(cols+1);
1200 for (j=0; j<=cols; j++,idx++) {
1201 unsigned long *d = disptop+idx;
1202 wanttext[idx] = ((*d ^ rv
1203 ^ (selstart <= d && d < selend ?
1204 ATTR_REVERSE : 0)) |
1205 (i==our_curs_y && j==curs_x ? cursor : 0));
1210 * We would perform scrolling optimisations in here, if they
1211 * didn't have a nasty tendency to cause the whole sodding
1212 * program to hang for a second at speed-critical moments.
1213 * We'll leave it well alone...
1216 for (i=0; i<rows; i++) {
1217 int idx = i*(cols+1);
1219 for (j=0; j<=cols; j++,idx++) {
1220 unsigned long t = wanttext[idx];
1221 int needs_update = (j < cols && t != disptext[idx]);
1222 int keep_going = (start != -1 && needs_update &&
1223 (t & ATTR_MASK) == attr &&
1224 j-start < sizeof(ch));
1225 if (start != -1 && !keep_going) {
1226 do_text (ctx, start, i, ch, j-start, attr);
1232 attr = t & ATTR_MASK;
1234 ch[j-start] = (char) (t & CHAR_MASK);
1242 * Invalidate the whole screen so it will be repainted in full.
1244 void term_invalidate(void) {
1247 for (i=0; i<rows*(cols+1); i++)
1248 disptext[i] = ATTR_INVALID;
1252 * Paint the window in response to a WM_PAINT message.
1254 void term_paint (Context ctx, int l, int t, int r, int b) {
1255 int i, j, left, top, right, bottom;
1257 left = l / font_width;
1258 right = (r - 1) / font_width;
1259 top = t / font_height;
1260 bottom = (b - 1) / font_height;
1261 for (i = top; i <= bottom && i < rows ; i++)
1262 for (j = left; j <= right && j < cols ; j++)
1263 disptext[i*(cols+1)+j] = ATTR_INVALID;
1265 do_paint (ctx, FALSE);
1269 * Attempt to scroll the scrollback. The second parameter gives the
1270 * position we want to scroll to; the first is +1 to denote that
1271 * this position is relative to the beginning of the scrollback, -1
1272 * to denote it is relative to the end, and 0 to denote that it is
1273 * relative to the current position.
1275 void term_scroll (int rel, int where) {
1276 int n = where * (cols+1);
1278 disptop = (rel < 0 ? scrtop :
1279 rel > 0 ? sbtop : disptop) + n;
1280 if (disptop < sbtop)
1282 if (disptop > scrtop)
1289 * Spread the selection outwards according to the selection mode.
1291 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1292 unsigned long *linestart, *lineend;
1296 x = (p - text) % (cols+1);
1298 lineend = linestart + cols;
1303 * In this mode, every character is a separate unit, except
1304 * for runs of spaces at the end of a non-wrapping line.
1306 if (!(linestart[cols] & ATTR_WRAPPED)) {
1307 unsigned long *q = lineend;
1308 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1313 p = (dir == -1 ? q : lineend - 1);
1318 * In this mode, the units are maximal runs of characters
1319 * whose `wordness' has the same value.
1321 wvalue = wordness[*p & CHAR_MASK];
1323 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1326 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1332 * In this mode, every line is a unit.
1334 p = (dir == -1 ? linestart : lineend - 1);
1340 static void sel_spread (void) {
1341 selstart = sel_spread_half (selstart, -1);
1342 selend = sel_spread_half (selend - 1, +1) + 1;
1345 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
1346 unsigned long *selpoint;
1349 if (y>=rows) y = rows-1;
1357 if (x>=cols) x = cols-1;
1359 selpoint = disptop + y * (cols+1) + x;
1361 if (b == MB_SELECT && a == MA_CLICK) {
1363 selstate = ABOUT_TO;
1364 selanchor = selpoint;
1366 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1368 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1369 selstate = DRAGGING;
1370 selstart = selanchor = selpoint;
1371 selend = selstart + 1;
1373 } else if ((b == MB_SELECT && a == MA_DRAG) ||
1374 (b == MB_EXTEND && a != MA_RELEASE)) {
1375 if (selstate == ABOUT_TO && selanchor == selpoint)
1377 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1378 if (selpoint-selstart < (selend-selstart)/2)
1379 selanchor = selend - 1;
1381 selanchor = selstart;
1382 selstate = DRAGGING;
1384 if (selstate != ABOUT_TO && selstate != DRAGGING)
1385 selanchor = selpoint;
1386 selstate = DRAGGING;
1387 if (selpoint < selanchor) {
1388 selstart = selpoint;
1389 selend = selanchor + 1;
1391 selstart = selanchor;
1392 selend = selpoint + 1;
1395 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
1396 if (selstate == DRAGGING) {
1398 * We've completed a selection. We now transfer the
1399 * data to the clipboard.
1401 unsigned char *p = selspace;
1402 unsigned long *q = selstart;
1404 while (q < selend) {
1406 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1407 unsigned long *nlpos = lineend;
1409 if (!(*nlpos & ATTR_WRAPPED)) {
1410 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1415 while (q < nlpos && q < selend)
1416 *p++ = (unsigned char) (*q++ & CHAR_MASK);
1419 for (i=0; i<sizeof(sel_nl); i++)
1422 q = lineend + 1; /* start of next line */
1424 write_clip (selspace, p - selspace);
1425 selstate = SELECTED;
1427 selstate = NO_SELECTION;
1428 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
1432 get_clip((void **) &data, &len);
1436 while (p < data+len) {
1437 while (p < data+len &&
1438 !(p <= data+len-sizeof(sel_nl) &&
1439 !memcmp(p, sel_nl, sizeof(sel_nl))))
1441 back->send (q, p-q);
1442 if (p <= data+len-sizeof(sel_nl) &&
1443 !memcmp(p, sel_nl, sizeof(sel_nl))) {
1444 back->send ("\r", 1);
1445 p += sizeof(sel_nl);
1450 get_clip(NULL, NULL);
1456 static void deselect (void) {
1457 selstate = NO_SELECTION;
1458 selstart = selend = scrtop;
1461 void term_deselect (void) {