3 #endif /* not macintosh */
11 #define back (s->back)
12 #define rows (s->rows)
13 #define cols (s->cols)
14 #define savelines (s->savelines)
15 #define font_width (s->font_width)
16 #define font_height (s->font_height)
17 #define has_focus (s->has_focus)
18 #define inbuf (s->inbuf)
19 #define inbuf_head (s->inbuf_head)
20 #define inbuf_reap (s->inbuf_reap)
21 #define app_cursor_keys (s->app_cursor_keys)
22 #define app_keypad_keys (s->app_keypad_keys)
23 #define attr_mask (s->attr_mask)
25 #define text s->ts.text
26 #define scrtop s->ts.scrtop
27 #define disptop s->ts.disptop
28 #define sbtop s->ts.sbtop
29 #define cpos s->ts.cpos
30 #define disptext s->ts.disptext
31 #define wanttext s->ts.wanttext
32 #define alttext s->ts.alttext
34 #define selspace s->ts.selspace
36 #define TSIZE (sizeof(*text))
37 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
39 #define curr_attr s->ts.curr_attr
40 #define save_attr s->ts.save_attr
42 #define curs_x s->ts.curs_x
43 #define curs_y s->ts.curs_y
44 #define save_x s->ts.save_x
45 #define save_y s->ts.save_y
46 #define marg_t s->ts.marg_t
47 #define marg_b s->ts.marg_b
48 #define curr_dec_om s->ts.dec_om
49 #define wrap s->ts.wrap
50 #define wrapnext s->ts.wrapnext
51 #define insert s->ts.insert
52 #define cset s->ts.cset
53 #define save_cset s->ts.save_cset
54 #define save_csattr s->ts.save_csattr
55 #define rvideo s->ts.rvideo
56 #define cset_attr s->ts.cset_attr
58 #define alt_x s->ts.alt_x
59 #define alt_y s->ts.alt_y
60 #define alt_om s->ts.alt_om
61 #define alt_wrap s->ts.alt_wrap
62 #define alt_wnext s->ts.alt_wnext
63 #define alt_ins s->ts.alt_ins
64 #define alt_cset s->ts.alt_cset
65 #define alt_t s->ts.alt_t
66 #define alt_b s->ts.alt_b
67 #define alt_which s->ts.alt_which
69 #define ARG_DEFAULT -1 /* if an arg isn't specified */
70 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
72 #define esc_args s->ts.esc_args
73 #define esc_nargs s->ts.esc_nargs
74 #define esc_query s->ts.esc_query
76 #define osc_strlen s->ts.osc_strlen
77 #define osc_string s->ts.osc_string
78 #define osc_w s->ts.osc_w
80 #define tabs s->ts.tabs
83 #define nl_count s->ts.nl_count
85 #define termstate (s->ts.termstate)
86 #define selstate (s->ts.selstate)
87 #define selmode (s->ts.selmode)
88 #define selstart (s->ts.selstart)
89 #define selend (s->ts.selend)
90 #define selanchor (s->ts.selanchor)
92 #define curr_wordness s->ts.wordness
94 static unsigned char sel_nl[] = SEL_NL;
97 * Internal prototypes.
99 static void do_paint (Session *, int);
100 static void erase_lots (Session *, int, int, int);
101 static void swap_screen (Session *, int);
102 static void update_sbar (Session *);
103 static void deselect (Session *);
104 static void scroll_display(Session *, int, int, int);
107 * Set up power-on settings for the terminal.
109 static void power_on(Session *s) {
110 curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
113 alt_b = marg_b = rows - 1;
118 for (i = 0; i < cols; i++)
119 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
121 alt_om = curr_dec_om = cfg.dec_om;
122 alt_wnext = wrapnext = alt_ins = insert = FALSE;
123 alt_wrap = wrap = cfg.wrap_mode;
125 cset_attr[0] = cset_attr[1] = ATTR_ASCII;
127 save_attr = curr_attr = ATTR_DEFAULT;
128 app_cursor_keys = cfg.app_cursor;
129 app_keypad_keys = cfg.app_keypad;
133 for (i = 0; i < 256; i++)
134 curr_wordness[i] = cfg.wordness[i];
138 erase_lots (s, FALSE, TRUE, TRUE);
140 erase_lots (s, FALSE, TRUE, TRUE);
145 * Force a screen update.
147 void term_update(Session *s) {
156 * Same as power_on(), but an external function.
158 void term_pwron(Session *s) {
167 * Clear the scrollback.
169 void term_clrsb(Session *s) {
170 disptop = sbtop = scrtop;
175 * Initialise the terminal.
177 void term_init(Session *s) {
178 text = sbtop = scrtop = disptop = cpos = NULL;
179 disptext = wanttext = NULL;
189 * Set up the terminal for a given size.
191 void term_size(Session *s, int newrows, int newcols, int newsavelines) {
192 unsigned long *newtext, *newdisp, *newwant, *newalt;
193 int i, j, crows, ccols;
195 if (newrows == rows && newcols == cols && newsavelines == savelines)
196 return; /* nothing to do */
199 alt_b = marg_b = newrows - 1;
201 newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
202 disptop = newtext + newsavelines*(newcols+1);
203 for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
204 newtext[i] = ERASE_CHAR;
206 crows = rows + (scrtop - sbtop) / (cols+1);
207 if (crows > newrows+newsavelines)
208 crows = newrows+newsavelines;
209 ccols = (cols < newcols ? cols : newcols);
210 for (i=0; i<crows; i++) {
211 int oldidx = (rows + savelines - crows + i) * (cols+1);
212 int newidx = (newrows + newsavelines - crows + i) * (newcols+1);
213 for (j=0; j<ccols; j++)
214 newtext[newidx+j] = text[oldidx+j];
215 newtext[newidx+newcols] =
216 (cols == newcols ? text[oldidx+cols] : 0);
218 sbtop = disptop - (crows - newrows) * (newcols+1);
227 newdisp = smalloc (newrows*(newcols+1)*TSIZE);
228 for (i=0; i<newrows*(newcols+1); i++)
229 newdisp[i] = ATTR_INVALID;
233 newwant = smalloc (newrows*(newcols+1)*TSIZE);
234 for (i=0; i<newrows*(newcols+1); i++)
235 newwant[i] = ATTR_INVALID;
239 newalt = smalloc (newrows*(newcols+1)*TSIZE);
240 for (i=0; i<newrows*(newcols+1); i++)
241 newalt[i] = ERASE_CHAR;
246 selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
248 tabs = srealloc (tabs, newcols*sizeof(*tabs));
251 for (i = (cols > 0 ? cols : 0); i < newcols; i++)
252 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
256 curs_y += newrows - rows;
259 if (curs_y >= newrows)
261 if (curs_x >= newcols)
264 wrapnext = alt_wnext = FALSE;
268 savelines = newsavelines;
279 static void swap_screen (Session *s, int which) {
283 if (which == alt_which)
288 for (t=0; t<rows*(cols+1); t++) {
289 tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
292 t = curs_x; curs_x = alt_x; alt_x = t;
293 t = curs_y; curs_y = alt_y; alt_y = t;
294 t = marg_t; marg_t = alt_t; alt_t = t;
295 t = marg_b; marg_b = alt_b; alt_b = t;
296 t = curr_dec_om; curr_dec_om = alt_om; alt_om = t;
297 t = wrap; wrap = alt_wrap; alt_wrap = t;
298 t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
299 t = insert; insert = alt_ins; alt_ins = t;
300 t = cset; cset = alt_cset; alt_cset = t;
306 * Retrieve a character from `inbuf'.
308 static int inbuf_getc(Session *s) {
309 if (inbuf_head == inbuf_reap)
313 inbuf_reap = (inbuf_reap+1) & INBUF_MASK;
319 * Update the scroll bar.
321 static void update_sbar(Session *s) {
324 min = (sbtop - text) / (cols+1);
325 set_sbar(s, (scrtop - text) / (cols+1) + rows - min,
326 (disptop - text) / (cols+1) - min,
331 * Check whether the region bounded by the two pointers intersects
332 * the selection, and de-select the on-screen selection if so.
334 static void check_selection(Session *s,
335 unsigned long *from, unsigned long *to) {
336 if (from < selend && selstart < to)
341 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
342 * for backward.) `sb' is TRUE if the scrolling is permitted to
343 * affect the scrollback buffer.
345 static void scroll (Session *s, int topline, int botline, int lines, int sb) {
346 unsigned long *scroll_top;
347 int scroll_size, size, i;
349 scroll_top = scrtop + topline*(cols+1);
350 size = (lines < 0 ? -lines : lines) * (cols+1);
351 scroll_size = (botline - topline + 1) * (cols+1) - size;
353 if (lines > 0 && topline == 0 && botline == (rows-1) && sb) {
355 * Since we're going to scroll the whole screen upwards,
356 * let's also affect the scrollback buffer.
358 sbtop -= lines * (cols+1);
361 scroll_size += scroll_top - sbtop;
366 if (scroll_size < 0) {
373 memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
374 for (i = 0; i < size; i++)
375 scroll_top[i+scroll_size] = ERASE_CHAR;
376 if (selstart > scroll_top &&
377 selstart < scroll_top + size + scroll_size) {
379 if (selstart < scroll_top)
380 selstart = scroll_top;
382 if (selend > scroll_top &&
383 selend < scroll_top + size + scroll_size) {
385 if (selend < scroll_top)
390 memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
391 for (i = 0; i < size; i++)
392 scroll_top[i] = ERASE_CHAR;
393 if (selstart > scroll_top &&
394 selstart < scroll_top + size + scroll_size) {
396 if (selstart > scroll_top + size + scroll_size)
397 selstart = scroll_top + size + scroll_size;
399 if (selend > scroll_top &&
400 selend < scroll_top + size + scroll_size) {
402 if (selend > scroll_top + size + scroll_size)
403 selend = scroll_top + size + scroll_size;
406 #ifdef OPTIMISE_SCROLL
407 scroll_display(s, topline, botline, lines);
411 #ifdef OPTIMISE_SCROLL
412 static void scroll_display(Session *s, int topline, int botline, int lines) {
413 unsigned long *start, *end;
414 int distance, size, i;
416 start = disptext + topline * (cols + 1);
417 end = disptext + (botline + 1) * (cols + 1);
418 distance = (lines > 0 ? lines : -lines) * (cols + 1);
419 size = end - start - distance;
421 memmove(start, start + distance, size * TSIZE);
422 for (i = 0; i < distance; i++)
423 (start + size)[i] |= ATTR_INVALID;
425 memmove(start + distance, start, size * TSIZE);
426 for (i = 0; i < distance; i++)
427 start[i] |= ATTR_INVALID;
429 do_scroll(s, topline, botline, lines);
431 #endif /* OPTIMISE_SCROLL */
435 * Move the cursor to a given position, clipping at boundaries. We
436 * may or may not want to clip at the scroll margin: marg_clip is 0
437 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
438 * even _being_ outside the margins.
440 static void move (Session *s, int x, int y, int marg_clip) {
446 if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
448 if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
462 * Save or restore the cursor and SGR mode.
464 static void save_cursor(Session *s, int save) {
468 save_attr = curr_attr;
470 save_csattr = cset_attr[cset];
474 curr_attr = save_attr;
476 cset_attr[cset] = save_csattr;
482 * Erase a large portion of the screen: the whole screen, or the
483 * whole line, or parts thereof.
485 static void erase_lots(Session *s, int line_only, int from_begin, int to_end) {
486 unsigned long *startpos, *endpos;
489 startpos = cpos - curs_x;
490 endpos = startpos + cols+1;
493 endpos = startpos + rows * (cols+1);
499 check_selection(s, startpos, endpos);
500 while (startpos < endpos)
501 *startpos++ = ERASE_CHAR;
505 * Insert or delete characters within the current line. n is +ve if
506 * insertion is desired, and -ve for deletion.
508 static void insch(Session *s, int n) {
509 int dir = (n < 0 ? -1 : +1);
512 n = (n < 0 ? -n : n);
513 if (n > cols - curs_x)
515 m = cols - curs_x - n;
516 check_selection(s, cpos, cpos+n);
518 memmove (cpos, cpos+n, m*TSIZE);
520 cpos[m++] = ERASE_CHAR;
522 memmove (cpos+n, cpos, m*TSIZE);
524 cpos[n] = ERASE_CHAR;
529 * Toggle terminal mode `mode' to state `state'. (`query' indicates
530 * whether the mode is a DEC private one or a normal one.)
532 static void toggle_mode(Session *s, int mode, int query, int state) {
533 if (query) switch (mode) {
534 case 1: /* application cursor keys */
535 app_cursor_keys = state;
537 case 3: /* 80/132 columns */
539 request_resize(s, state ? 132 : 80, rows);
541 case 5: /* reverse video */
545 case 6: /* DEC origin mode */
548 case 7: /* auto wrap */
551 case 47: /* alternate screen */
553 swap_screen(s, state);
556 } else switch (mode) {
557 case 4: /* set insert mode */
564 * Process an OSC sequence: set window title or icon name.
566 static void do_osc(Session *s) {
569 curr_wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
571 osc_string[osc_strlen] = '\0';
572 switch (esc_args[0]) {
575 set_icon (s, osc_string);
576 if (esc_args[0] == 1)
578 /* fall through: parameter 0 means set both */
581 set_title(s, osc_string);
588 * Remove everything currently in `inbuf' and stick it up on the
589 * in-memory display. There's a big state machine in here to
590 * process escape sequences...
592 void term_out(Session *s) {
594 int must_update = FALSE;
596 while ( (c = inbuf_getc(s)) != -1) {
599 static FILE *fp = NULL;
600 if (!fp) fp = fopen("putty.log", "wb");
601 if (fp) fputc (c, fp);
608 case '\005': /* terminal type query */
609 back->send(s, "\033[?1;2c", 7);
617 if (curs_x == 0 && curs_y > 0)
618 curs_x = cols-1, curs_y--;
634 termstate = SEEN_ESC;
637 termstate = SEEN_CSI;
639 esc_args[0] = ARG_DEFAULT;
643 termstate = SEEN_OSC;
656 if (curs_y == marg_b)
657 scroll(s, marg_t, marg_b, 1, TRUE);
658 else if (curs_y < rows-1)
670 } while (curs_x < cols-1 && !tabs[curs_x]);
674 unsigned long *old_cpos = cpos;
676 check_selection(s, old_cpos, cpos);
682 if (c >= ' ' && c != 0234) {
684 cpos[1] = ATTR_WRAPPED;
685 if (curs_y == marg_b)
686 scroll(s, marg_t, marg_b, 1, TRUE);
687 else if (curs_y < rows-1)
696 check_selection(s, cpos, cpos+1);
697 *cpos++ = c | curr_attr |
698 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
700 if (curs_x == cols) {
710 termstate = TOPLEVEL;
714 * This state is virtually identical to SEEN_ESC, with the
715 * exception that we have an OSC sequence in the pipeline,
716 * and _if_ we see a backslash, we process it.
720 termstate = TOPLEVEL;
723 /* else fall through */
725 termstate = TOPLEVEL;
727 case '\005': case '\007': case '\b': case '\016': case '\017':
728 case '\033': case 0233: case 0234: case 0235: case '\015':
729 case '\013': case '\014': case '\012': case '\t':
730 termstate = TOPLEVEL;
731 goto do_toplevel; /* hack... */
732 case ' ': /* some weird sequence? */
733 termstate = IGNORE_NEXT;
735 case '[': /* enter CSI mode */
736 termstate = SEEN_CSI;
738 esc_args[0] = ARG_DEFAULT;
741 case ']': /* xterm escape sequences */
742 termstate = SEEN_OSC;
745 case '(': /* should set GL */
748 case ')': /* should set GR */
751 case '7': /* save cursor */
752 save_cursor(s, TRUE);
754 case '8': /* restore cursor */
755 save_cursor(s, FALSE);
760 app_keypad_keys = TRUE;
763 app_keypad_keys = FALSE;
765 case 'D': /* exactly equivalent to LF */
766 if (curs_y == marg_b)
767 scroll(s, marg_t, marg_b, 1, TRUE);
768 else if (curs_y < rows-1)
775 case 'E': /* exactly equivalent to CR-LF */
778 if (curs_y == marg_b)
779 scroll(s, marg_t, marg_b, 1, TRUE);
780 else if (curs_y < rows-1)
787 case 'M': /* reverse index - backwards LF */
788 if (curs_y == marg_t)
789 scroll(s, marg_t, marg_b, -1, TRUE);
797 case 'Z': /* terminal type query */
798 back->send(s, "\033[?6c", 5);
800 case 'c': /* restore power-on settings */
806 case '#': /* ESC # 8 fills screen with Es :-) */
807 termstate = SEEN_ESCHASH;
809 case 'H': /* set a tab */
815 termstate = TOPLEVEL; /* default */
817 case '\005': case '\007': case '\b': case '\016': case '\017':
818 case '\033': case 0233: case 0234: case 0235: case '\015':
819 case '\013': case '\014': case '\012': case '\t':
820 termstate = TOPLEVEL;
821 goto do_toplevel; /* hack... */
822 case '0': case '1': case '2': case '3': case '4':
823 case '5': case '6': case '7': case '8': case '9':
824 if (esc_nargs <= ARGS_MAX) {
825 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
826 esc_args[esc_nargs-1] = 0;
827 esc_args[esc_nargs-1] =
828 10 * esc_args[esc_nargs-1] + c - '0';
830 termstate = SEEN_CSI;
833 if (++esc_nargs <= ARGS_MAX)
834 esc_args[esc_nargs-1] = ARG_DEFAULT;
835 termstate = SEEN_CSI;
839 termstate = SEEN_CSI;
841 case 'A': /* move up N lines */
842 move(s, curs_x, curs_y - def(esc_args[0], 1), 1);
846 case 'B': case 'e': /* move down N lines */
847 move(s, curs_x, curs_y + def(esc_args[0], 1), 1);
851 case 'C': case 'a': /* move right N cols */
852 move(s, curs_x + def(esc_args[0], 1), curs_y, 1);
856 case 'D': /* move left N cols */
857 move(s, curs_x - def(esc_args[0], 1), curs_y, 1);
861 case 'E': /* move down N lines and CR */
862 move(s, 0, curs_y + def(esc_args[0], 1), 1);
866 case 'F': /* move up N lines and CR */
867 move(s, 0, curs_y - def(esc_args[0], 1), 1);
871 case 'G': case '`': /* set horizontal posn */
872 move(s, def(esc_args[0], 1) - 1, curs_y, 0);
876 case 'd': /* set vertical posn */
877 move(s, curs_x, (curr_dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
878 (curr_dec_om ? 2 : 0));
882 case 'H': case 'f': /* set horz and vert posns at once */
884 esc_args[1] = ARG_DEFAULT;
885 move(s, def(esc_args[1], 1) - 1,
886 (curr_dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
887 (curr_dec_om ? 2 : 0));
891 case 'J': /* erase screen or parts of it */
893 unsigned int i = def(esc_args[0], 0) + 1;
896 erase_lots(s, FALSE, !!(i & 2), !!(i & 1));
901 case 'K': /* erase line or parts of it */
903 unsigned int i = def(esc_args[0], 0) + 1;
906 erase_lots(s, TRUE, !!(i & 2), !!(i & 1));
911 case 'L': /* insert lines */
912 if (curs_y <= marg_b)
913 scroll(s, curs_y, marg_b, -def(esc_args[0], 1), FALSE);
917 case 'M': /* delete lines */
918 if (curs_y <= marg_b)
919 scroll(s, curs_y, marg_b, def(esc_args[0], 1), FALSE);
923 case '@': /* insert chars */
924 insch(s, def(esc_args[0], 1));
928 case 'P': /* delete chars */
929 insch(s, -def(esc_args[0], 1));
933 case 'c': /* terminal type query */
934 back->send(s, "\033[?6c", 5);
936 case 'n': /* cursor position query */
937 if (esc_args[0] == 6) {
939 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
940 back->send(s, buf, strlen(buf));
943 case 'h': /* toggle a mode to high */
944 toggle_mode(s, esc_args[0], esc_query, TRUE);
946 case 'l': /* toggle a mode to low */
947 toggle_mode(s, esc_args[0], esc_query, FALSE);
949 case 'g': /* clear tabs */
950 if (esc_nargs == 1) {
951 if (esc_args[0] == 0) {
952 tabs[curs_x] = FALSE;
953 } else if (esc_args[0] == 3) {
955 for (i = 0; i < cols; i++)
960 case 'r': /* set scroll margins */
961 if (esc_nargs <= 2) {
963 top = def(esc_args[0], 1) - 1;
966 bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
967 def(esc_args[1], rows)) - 1;
975 * I used to think the cursor should be
976 * placed at the top of the newly marginned
977 * area. Apparently not: VMS TPU falls over
987 case 'm': /* set graphics rendition */
990 for (i=0; i<esc_nargs; i++) {
991 switch (def(esc_args[i], 0)) {
992 case 0: /* restore defaults */
993 curr_attr = ATTR_DEFAULT; break;
994 case 1: /* enable bold */
995 curr_attr |= ATTR_BOLD; break;
996 case 4: /* enable underline */
997 case 21: /* (enable double underline) */
998 curr_attr |= ATTR_UNDER; break;
999 case 7: /* enable reverse video */
1000 curr_attr |= ATTR_REVERSE; break;
1001 case 22: /* disable bold */
1002 curr_attr &= ~ATTR_BOLD; break;
1003 case 24: /* disable underline */
1004 curr_attr &= ~ATTR_UNDER; break;
1005 case 27: /* disable reverse video */
1006 curr_attr &= ~ATTR_REVERSE; break;
1007 case 30: case 31: case 32: case 33:
1008 case 34: case 35: case 36: case 37:
1010 curr_attr &= ~ATTR_FGMASK;
1011 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
1013 case 39: /* default-foreground */
1014 curr_attr &= ~ATTR_FGMASK;
1015 curr_attr |= ATTR_DEFFG;
1017 case 40: case 41: case 42: case 43:
1018 case 44: case 45: case 46: case 47:
1020 curr_attr &= ~ATTR_BGMASK;
1021 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
1023 case 49: /* default-background */
1024 curr_attr &= ~ATTR_BGMASK;
1025 curr_attr |= ATTR_DEFBG;
1031 case 's': /* save cursor */
1032 save_cursor(s, TRUE);
1034 case 'u': /* restore cursor */
1035 save_cursor(s, FALSE);
1039 case 't': /* set page size - ie window height */
1040 request_resize(s, cols, def(esc_args[0], 24));
1043 case 'X': /* write N spaces w/o moving cursor */
1045 int n = def(esc_args[0], 1);
1046 unsigned long *p = cpos;
1047 if (n > cols - curs_x)
1049 check_selection(s, cpos, cpos+n);
1056 case 'x': /* report terminal characteristics */
1059 int i = def(esc_args[0], 0);
1060 if (i == 0 || i == 1) {
1061 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1063 back->send(s, buf, 20);
1073 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1076 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1078 default: /* specifically, 'B' */
1079 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1082 termstate = TOPLEVEL;
1087 case '\005': case '\007': case '\b': case '\016': case '\017':
1088 case '\033': case 0233: case 0234: case 0235: case '\015':
1089 case '\013': case '\014': case '\012': case '\t':
1090 termstate = TOPLEVEL;
1091 goto do_toplevel; /* hack... */
1092 case 'P': /* Linux palette sequence */
1093 termstate = SEEN_OSC_P;
1096 case 'R': /* Linux palette reset */
1099 termstate = TOPLEVEL;
1101 case 'W': /* word-set */
1102 termstate = SEEN_OSC_W;
1105 case '0': case '1': case '2': case '3': case '4':
1106 case '5': case '6': case '7': case '8': case '9':
1107 esc_args[0] = 10 * esc_args[0] + c - '0';
1111 * Grotty hack to support xterm and DECterm title
1112 * sequences concurrently.
1114 if (esc_args[0] == 2) {
1118 /* else fall through */
1120 termstate = OSC_STRING;
1125 if (c == 0234 || c == '\007') {
1127 * These characters terminate the string; ST and BEL
1128 * terminate the sequence and trigger instant
1129 * processing of it, whereas ESC goes back to SEEN_ESC
1130 * mode unless it is followed by \, in which case it is
1131 * synonymous with ST in the first place.
1134 termstate = TOPLEVEL;
1135 } else if (c == '\033')
1136 termstate = OSC_MAYBE_ST;
1137 else if (osc_strlen < OSC_STR_MAX)
1138 osc_string[osc_strlen++] = c;
1142 int max = (osc_strlen == 0 ? 21 : 16);
1144 if (c >= '0' && c <= '9')
1146 else if (c >= 'A' && c <= 'A'+max-10)
1148 else if (c >= 'a' && c <= 'a'+max-10)
1151 termstate = TOPLEVEL;
1152 osc_string[osc_strlen++] = val;
1153 if (osc_strlen >= 7) {
1154 palette_set (s, osc_string[0],
1155 osc_string[1] * 16 + osc_string[2],
1156 osc_string[3] * 16 + osc_string[4],
1157 osc_string[5] * 16 + osc_string[6]);
1158 termstate = TOPLEVEL;
1164 case '\005': case '\007': case '\b': case '\016': case '\017':
1165 case '\033': case 0233: case 0234: case 0235: case '\015':
1166 case '\013': case '\014': case '\012': case '\t':
1167 termstate = TOPLEVEL;
1168 goto do_toplevel; /* hack... */
1169 case '0': case '1': case '2': case '3': case '4':
1170 case '5': case '6': case '7': case '8': case '9':
1171 esc_args[0] = 10 * esc_args[0] + c - '0';
1174 termstate = OSC_STRING;
1180 unsigned long *p = scrtop;
1181 int n = rows * (cols+1);
1183 *p++ = ATTR_DEFAULT | 'E';
1186 check_selection(s, scrtop, scrtop + rows * (cols+1));
1188 termstate = TOPLEVEL;
1191 check_selection(s, cpos, cpos+1);
1194 if (must_update || nl_count > MAXNL) {
1201 * Given a context, update the window. Out of paranoia, we don't
1202 * allow WM_PAINT responses to do scrolling optimisations.
1204 static void do_paint (Session *s, int may_optimise){
1205 int i, j, start, our_curs_y;
1206 unsigned long attr, rv, cursor;
1209 cursor = (has_focus ? ATTR_ACTCURS : ATTR_PASCURS);
1210 rv = (rvideo ? ATTR_REVERSE : 0);
1211 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1213 for (i=0; i<rows; i++) {
1214 int idx = i*(cols+1);
1215 for (j=0; j<=cols; j++,idx++) {
1216 unsigned long *d = disptop+idx;
1217 wanttext[idx] = ((*d ^ rv
1218 ^ (selstart <= d && d < selend ?
1219 ATTR_REVERSE : 0)) |
1220 (i==our_curs_y && j==curs_x ? cursor : 0));
1225 * We would perform scrolling optimisations in here, if they
1226 * didn't have a nasty tendency to cause the whole sodding
1227 * program to hang for a second at speed-critical moments.
1228 * We'll leave it well alone...
1231 for (i=0; i<rows; i++) {
1232 int idx = i*(cols+1);
1234 for (j=0; j<=cols; j++,idx++) {
1235 unsigned long t = wanttext[idx];
1236 int needs_update = (j < cols && t != disptext[idx]);
1237 int keep_going = (start != -1 && needs_update &&
1238 (t & attr_mask) == attr &&
1239 j-start < sizeof(ch));
1240 if (start != -1 && !keep_going) {
1241 do_text(s, start, i, ch, j-start, attr);
1247 attr = t & attr_mask;
1249 ch[j-start] = (char) (t & CHAR_MASK);
1257 * Invalidate the whole screen so it will be repainted in full.
1259 void term_invalidate(Session *s) {
1262 for (i=0; i<rows*(cols+1); i++)
1263 disptext[i] = ATTR_INVALID;
1267 * Paint the window in response to a WM_PAINT message.
1269 void term_paint(Session *s, int l, int t, int r, int b) {
1270 int i, j, left, top, right, bottom;
1272 left = l / font_width;
1273 right = (r - 1) / font_width;
1274 top = t / font_height;
1275 bottom = (b - 1) / font_height;
1276 for (i = top; i <= bottom && i < rows ; i++)
1277 for (j = left; j <= right && j < cols ; j++)
1278 disptext[i*(cols+1)+j] = ATTR_INVALID;
1280 do_paint (s, FALSE);
1284 * Attempt to scroll the scrollback. The second parameter gives the
1285 * position we want to scroll to; the first is +1 to denote that
1286 * this position is relative to the beginning of the scrollback, -1
1287 * to denote it is relative to the end, and 0 to denote that it is
1288 * relative to the current position.
1290 void term_scroll(Session *s, int rel, int where) {
1291 int n = where * (cols+1);
1292 #ifdef OPTIMISE_SCROLL
1293 unsigned long *olddisptop = disptop;
1295 #endif /* OPTIMISE_SCROLL */
1297 disptop = (rel < 0 ? scrtop :
1298 rel > 0 ? sbtop : disptop) + n;
1299 if (disptop < sbtop)
1301 if (disptop > scrtop)
1304 #ifdef OPTIMISE_SCROLL
1305 shift = (disptop - olddisptop) / (cols + 1);
1306 if (shift < rows && shift > -rows)
1307 scroll_display(s, 0, rows - 1, shift);
1308 #endif /* OPTIMISE_SCROLL */
1313 * Spread the selection outwards according to the selection mode.
1315 static unsigned long *sel_spread_half(Session *s, unsigned long *p, int dir) {
1316 unsigned long *linestart, *lineend;
1320 x = (p - text) % (cols+1);
1322 lineend = linestart + cols;
1327 * In this mode, every character is a separate unit, except
1328 * for runs of spaces at the end of a non-wrapping line.
1330 if (!(linestart[cols] & ATTR_WRAPPED)) {
1331 unsigned long *q = lineend;
1332 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1337 p = (dir == -1 ? q : lineend - 1);
1342 * In this mode, the units are maximal runs of characters
1343 * whose `wordness' has the same value.
1345 wvalue = curr_wordness[*p & CHAR_MASK];
1347 while (p < lineend && curr_wordness[p[1] & CHAR_MASK] == wvalue)
1350 while (p > linestart && curr_wordness[p[-1] & CHAR_MASK] == wvalue)
1356 * In this mode, every line is a unit.
1358 p = (dir == -1 ? linestart : lineend - 1);
1364 static void sel_spread(Session *s) {
1365 selstart = sel_spread_half(s, selstart, -1);
1366 selend = sel_spread_half(s, selend - 1, +1) + 1;
1369 void term_mouse(Session *s, Mouse_Button b, Mouse_Action a, int x, int y) {
1370 unsigned long *selpoint;
1375 } else if (x >= cols)
1378 selpoint = disptop + y * (cols + 1) + x;
1379 if (selpoint < sbtop)
1381 else if (selpoint > scrtop + rows * (cols + 1) - 1)
1382 /* XXX put this in a variable? */
1383 selpoint = scrtop + rows * (cols + 1) - 1;
1385 if (b == MB_SELECT && a == MA_CLICK) {
1387 selstate = ABOUT_TO;
1388 selanchor = selpoint;
1390 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1392 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1393 selstate = DRAGGING;
1394 selstart = selanchor = selpoint;
1395 selend = selstart + 1;
1397 } else if ((b == MB_SELECT && a == MA_DRAG) ||
1398 (b == MB_EXTEND && a != MA_RELEASE)) {
1399 if (selstate == ABOUT_TO && selanchor == selpoint)
1401 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1402 if (selpoint-selstart < (selend-selstart)/2)
1403 selanchor = selend - 1;
1405 selanchor = selstart;
1406 selstate = DRAGGING;
1408 if (selstate != ABOUT_TO && selstate != DRAGGING)
1409 selanchor = selpoint;
1410 selstate = DRAGGING;
1411 if (selpoint < selanchor) {
1412 selstart = selpoint;
1413 selend = selanchor + 1;
1415 selstart = selanchor;
1416 selend = selpoint + 1;
1419 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE)
1420 if (selstate == DRAGGING) {
1421 if (cfg.implicit_copy)
1423 selstate = SELECTED;
1425 selstate = NO_SELECTION;
1426 else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK))
1432 * We've completed a selection. We now transfer the
1433 * data to the clipboard.
1435 void term_copy(Session *s) {
1436 unsigned char *p = selspace;
1437 unsigned long *q = selstart;
1439 while (q < selend) {
1441 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1442 unsigned long *nlpos = lineend;
1444 if (!(*nlpos & ATTR_WRAPPED)) {
1445 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1450 while (q < nlpos && q < selend)
1451 *p++ = (unsigned char) (*q++ & CHAR_MASK);
1454 for (i=0; i<sizeof(sel_nl); i++)
1457 q = lineend + 1; /* start of next line */
1459 write_clip(selspace, p - selspace);
1462 void term_paste(Session *s) {
1466 get_clip((void **) &data, &len);
1470 while (p < data+len) {
1471 while (p < data+len &&
1472 !(p <= data+len-sizeof(sel_nl) &&
1473 !memcmp(p, sel_nl, sizeof(sel_nl))))
1475 back->send(s, q, p-q);
1476 if (p <= data+len-sizeof(sel_nl) &&
1477 !memcmp(p, sel_nl, sizeof(sel_nl))) {
1478 back->send(s, "\015", 1);
1479 p += sizeof(sel_nl);
1484 get_clip(NULL, NULL);
1488 * Find out if there's a selection.
1490 int term_hasselection(Session *s) {
1492 return selstate == SELECTED;
1495 static void deselect(Session *s) {
1496 selstate = NO_SELECTION;
1497 selstart = selend = scrtop;
1500 void term_deselect(Session *s) {