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
];
49 #define ANSI(x,y) ((x)+((y)<<8))
50 #define ANSI_QUE(x) ANSI(x,TRUE)
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
;
76 OSC_STRING
, OSC_MAYBE_ST
,
81 NO_SELECTION
, ABOUT_TO
, DRAGGING
, SELECTED
84 SM_CHAR
, SM_WORD
, SM_LINE
86 static unsigned long *selstart
, *selend
, *selanchor
;
88 static short wordness
[256] = {
89 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 */
90 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 */
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,2, /* 45 */
92 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 */
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, /* 89 */
94 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 */
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, /* CD */
96 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 */
99 static unsigned char sel_nl
[] = SEL_NL
;
102 * Internal prototypes.
104 static void do_paint (Context
, int);
105 static void erase_lots (int, int, int);
106 static void swap_screen (int);
107 static void update_sbar (void);
108 static void deselect (void);
111 * Set up power-on settings for the terminal.
113 static void power_on(void) {
114 curs_x
= curs_y
= alt_x
= alt_y
= save_x
= save_y
= 0;
117 alt_b
= marg_b
= rows
- 1;
122 for (i
= 0; i
< cols
; i
++)
123 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
125 alt_om
= dec_om
= cfg
.dec_om
;
126 alt_wnext
= wrapnext
= alt_ins
= insert
= FALSE
;
127 alt_wrap
= wrap
= cfg
.wrap_mode
;
129 cset_attr
[0] = cset_attr
[1] = ATTR_ASCII
;
131 save_attr
= curr_attr
= ATTR_DEFAULT
;
132 app_cursor_keys
= cfg
.app_cursor
;
133 app_keypad_keys
= cfg
.app_keypad
;
137 for (i
= 0; i
< 256; i
++)
138 wordness
[i
] = cfg
.wordness
[i
];
142 erase_lots (FALSE
, TRUE
, TRUE
);
144 erase_lots (FALSE
, TRUE
, TRUE
);
149 * Force a screen update.
151 void term_update(void) {
155 if ( (seen_key_event
&& (unscroll_event
& US_KEY
)) ||
156 (seen_disp_event
&& (unscroll_event
& US_DISP
)) ) {
158 seen_disp_event
= seen_key_event
= 0;
160 do_paint (ctx
, TRUE
);
163 scroll_heuristic
= 0;
168 * Same as power_on(), but an external function.
170 void term_pwron(void) {
179 * Clear the scrollback.
181 void term_clrsb(void) {
182 disptop
= sbtop
= scrtop
;
187 * Initialise the terminal.
189 void term_init(void) {
190 text
= sbtop
= scrtop
= disptop
= cpos
= NULL
;
191 disptext
= wanttext
= NULL
;
201 * Set up the terminal for a given size.
203 void term_size(int newrows
, int newcols
, int newsavelines
) {
204 unsigned long *newtext
, *newdisp
, *newwant
, *newalt
;
205 int i
, j
, crows
, ccols
;
207 if (newrows
== rows
&& newcols
== cols
&& newsavelines
== savelines
)
208 return; /* nothing to do */
211 alt_b
= marg_b
= newrows
- 1;
213 newtext
= smalloc ((newrows
+newsavelines
)*(newcols
+1)*TSIZE
);
214 disptop
= newtext
+ newsavelines
*(newcols
+1);
215 for (i
=0; i
<(newrows
+newsavelines
)*(newcols
+1); i
++)
216 newtext
[i
] = ERASE_CHAR
;
218 crows
= rows
+ (scrtop
- sbtop
) / (cols
+1);
219 if (crows
> newrows
+newsavelines
)
220 crows
= newrows
+newsavelines
;
221 ccols
= (cols
< newcols ? cols
: newcols
);
222 for (i
=0; i
<crows
; i
++) {
223 int oldidx
= (rows
+ savelines
- crows
+ i
) * (cols
+1);
224 int newidx
= (newrows
+ newsavelines
- crows
+ i
) * (newcols
+1);
225 for (j
=0; j
<ccols
; j
++)
226 newtext
[newidx
+j
] = text
[oldidx
+j
];
227 newtext
[newidx
+newcols
] =
228 (cols
== newcols ? text
[oldidx
+cols
] : 0);
230 sbtop
= disptop
- (crows
- newrows
) * (newcols
+1);
239 newdisp
= smalloc (newrows
*(newcols
+1)*TSIZE
);
240 for (i
=0; i
<newrows
*(newcols
+1); i
++)
241 newdisp
[i
] = ATTR_INVALID
;
245 newwant
= smalloc (newrows
*(newcols
+1)*TSIZE
);
246 for (i
=0; i
<newrows
*(newcols
+1); i
++)
247 newwant
[i
] = ATTR_INVALID
;
251 newalt
= smalloc (newrows
*(newcols
+1)*TSIZE
);
252 for (i
=0; i
<newrows
*(newcols
+1); i
++)
253 newalt
[i
] = ERASE_CHAR
;
258 selspace
= smalloc ( (newrows
+newsavelines
) * (newcols
+sizeof(sel_nl
)) );
260 tabs
= srealloc (tabs
, newcols
*sizeof(*tabs
));
263 for (i
= (cols
> 0 ? cols
: 0); i
< newcols
; i
++)
264 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
268 curs_y
+= newrows
- rows
;
271 if (curs_y
>= newrows
)
273 if (curs_x
>= newcols
)
276 wrapnext
= alt_wnext
= FALSE
;
280 savelines
= newsavelines
;
291 static void swap_screen (int which
) {
295 if (which
== alt_which
)
300 for (t
=0; t
<rows
*(cols
+1); t
++) {
301 tt
= scrtop
[t
]; scrtop
[t
] = alttext
[t
]; alttext
[t
] = tt
;
304 t
= curs_x
; curs_x
= alt_x
; alt_x
= t
;
305 t
= curs_y
; curs_y
= alt_y
; alt_y
= t
;
306 t
= marg_t
; marg_t
= alt_t
; alt_t
= t
;
307 t
= marg_b
; marg_b
= alt_b
; alt_b
= t
;
308 t
= dec_om
; dec_om
= alt_om
; alt_om
= t
;
309 t
= wrap
; wrap
= alt_wrap
; alt_wrap
= t
;
310 t
= wrapnext
; wrapnext
= alt_wnext
; alt_wnext
= t
;
311 t
= insert
; insert
= alt_ins
; alt_ins
= t
;
312 t
= cset
; cset
= alt_cset
; alt_cset
= t
;
318 * Retrieve a character from `inbuf'.
320 static int inbuf_getc(void) {
321 if (inbuf_head
== inbuf_reap
)
325 inbuf_reap
= (inbuf_reap
+1) & INBUF_MASK
;
331 * Update the scroll bar.
333 static void update_sbar(void) {
336 min
= (sbtop
- text
) / (cols
+1);
337 set_sbar ((scrtop
- text
) / (cols
+1) + rows
- min
,
338 (disptop
- text
) / (cols
+1) - min
,
343 * Check whether the region bounded by the two pointers intersects
344 * the scroll region, and de-select the on-screen selection if so.
346 static void check_selection (unsigned long *from
, unsigned long *to
) {
347 if (from
< selend
&& selstart
< to
)
352 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
353 * for backward.) `sb' is TRUE if the scrolling is permitted to
354 * affect the scrollback buffer.
356 static void scroll (int topline
, int botline
, int lines
, int sb
) {
357 unsigned long *scroll_top
;
358 int scroll_size
, size
, i
;
360 scroll_top
= scrtop
+ topline
*(cols
+1);
361 size
= (lines
< 0 ?
-lines
: lines
) * (cols
+1);
362 scroll_size
= (botline
- topline
+ 1) * (cols
+1) - size
;
364 if (lines
> 0 && topline
== 0 && alt_which
== 0 && sb
) {
366 * Since we're going to scroll the top line and we're on the
367 * scrolling screen let's also affect the scrollback buffer.
369 sbtop
-= lines
* (cols
+1);
372 scroll_size
+= scroll_top
- sbtop
;
377 if (scroll_size
< 0) {
384 memmove (scroll_top
, scroll_top
+ size
, scroll_size
*TSIZE
);
385 for (i
= 0; i
< size
; i
++)
386 scroll_top
[i
+scroll_size
] = ERASE_CHAR
;
387 if (selstart
> scroll_top
&&
388 selstart
< scroll_top
+ size
+ scroll_size
) {
390 if (selstart
< scroll_top
)
391 selstart
= scroll_top
;
393 if (selend
> scroll_top
&&
394 selend
< scroll_top
+ size
+ scroll_size
) {
396 if (selend
< scroll_top
)
401 memmove (scroll_top
+ size
, scroll_top
, scroll_size
*TSIZE
);
402 for (i
= 0; i
< size
; i
++)
403 scroll_top
[i
] = ERASE_CHAR
;
404 if (selstart
> scroll_top
&&
405 selstart
< scroll_top
+ size
+ scroll_size
) {
407 if (selstart
> scroll_top
+ size
+ scroll_size
)
408 selstart
= scroll_top
+ size
+ scroll_size
;
410 if (selend
> scroll_top
&&
411 selend
< scroll_top
+ size
+ scroll_size
) {
413 if (selend
> scroll_top
+ size
+ scroll_size
)
414 selend
= scroll_top
+ size
+ scroll_size
;
418 scroll_heuristic
+= lines
;
422 * Move the cursor to a given position, clipping at boundaries. We
423 * may or may not want to clip at the scroll margin: marg_clip is 0
424 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
425 * even _being_ outside the margins.
427 static void move (int x
, int y
, int marg_clip
) {
433 if ((curs_y
>= marg_t
|| marg_clip
== 2) && y
< marg_t
)
435 if ((curs_y
<= marg_b
|| marg_clip
== 2) && y
> marg_b
)
449 * Save or restore the cursor and SGR mode.
451 static void save_cursor(int save
) {
455 save_attr
= curr_attr
;
457 save_csattr
= cset_attr
[cset
];
461 curr_attr
= save_attr
;
463 cset_attr
[cset
] = save_csattr
;
469 * Erase a large portion of the screen: the whole screen, or the
470 * whole line, or parts thereof.
472 static void erase_lots (int line_only
, int from_begin
, int to_end
) {
473 unsigned long *startpos
, *endpos
;
476 startpos
= cpos
- curs_x
;
477 endpos
= startpos
+ cols
+1;
480 endpos
= startpos
+ rows
* (cols
+1);
486 check_selection (startpos
, endpos
);
487 while (startpos
< endpos
)
488 *startpos
++ = ERASE_CHAR
;
492 * Insert or delete characters within the current line. n is +ve if
493 * insertion is desired, and -ve for deletion.
495 static void insch (int n
) {
496 int dir
= (n
< 0 ?
-1 : +1);
499 n
= (n
< 0 ?
-n
: n
);
500 if (n
> cols
- curs_x
)
502 m
= cols
- curs_x
- n
;
503 check_selection (cpos
, cpos
+n
);
505 memmove (cpos
, cpos
+n
, m
*TSIZE
);
507 cpos
[m
++] = ERASE_CHAR
;
509 memmove (cpos
+n
, cpos
, m
*TSIZE
);
511 cpos
[n
] = ERASE_CHAR
;
516 * Toggle terminal mode `mode' to state `state'. (`query' indicates
517 * whether the mode is a DEC private one or a normal one.)
519 static void toggle_mode (int mode
, int query
, int state
) {
520 if (query
) switch (mode
) {
521 case 1: /* application cursor keys */
522 app_cursor_keys
= state
;
524 case 3: /* 80/132 columns */
526 request_resize (state ?
132 : 80, rows
);
528 case 5: /* reverse video */
530 seen_disp_event
= TRUE
;
532 case 6: /* DEC origin mode */
535 case 7: /* auto wrap */
538 case 47: /* alternate screen */
543 } else switch (mode
) {
544 case 4: /* set insert mode */
551 * Process an OSC sequence: set window title or icon name.
553 static void do_osc(void) {
556 wordness
[(unsigned char)osc_string
[osc_strlen
]] = esc_args
[0];
558 osc_string
[osc_strlen
] = '\0';
559 switch (esc_args
[0]) {
562 set_icon (osc_string
);
563 if (esc_args
[0] == 1)
565 /* fall through: parameter 0 means set both */
568 set_title (osc_string
);
575 * Remove everything currently in `inbuf' and stick it up on the
576 * in-memory display. There's a big state machine in here to
577 * process escape sequences...
579 void term_out(void) {
582 while ( (c
= inbuf_getc()) != -1) {
584 * Optionally log the session traffic to a file. Useful for
585 * debugging and possibly also useful for actual logging.
588 static FILE *fp
= NULL
;
589 if (!fp
) fp
= fopen(logfile
, "wb");
590 if (fp
) fputc (c
, fp
);
592 if( termstate
< DO_CTRLS
&& (c
&0x60) == 0 ) {
594 case '\005': /* terminal type query */
595 ldisc
->send ("PuTTY\r", 6);
602 if (curs_x
== 0 && curs_y
> 0)
603 curs_x
= cols
-1, curs_y
--;
609 seen_disp_event
= TRUE
;
618 termstate
= SEEN_ESC
;
621 termstate
= SEEN_CSI
;
623 esc_args
[0] = ARG_DEFAULT
;
627 termstate
= SEEN_OSC
;
634 seen_disp_event
= TRUE
;
639 if (curs_y
== marg_b
)
640 scroll (marg_t
, marg_b
, 1, TRUE
);
641 else if (curs_y
< rows
-1)
653 } while (curs_x
< cols
-1 && !tabs
[curs_x
]);
657 unsigned long *old_cpos
= cpos
;
659 check_selection (old_cpos
, cpos
);
661 seen_disp_event
= TRUE
;
665 else switch (termstate
) {
667 /* Only graphic characters get this far, ctrls are stripped above */
669 cpos
[1] = ATTR_WRAPPED
;
670 if (curs_y
== marg_b
)
671 scroll (marg_t
, marg_b
, 1, TRUE
);
672 else if (curs_y
< rows
-1)
681 check_selection (cpos
, cpos
+1);
682 *cpos
++ = xlat_tty2scr((unsigned char)c
) | curr_attr
|
683 (c
<= 0x7F ? cset_attr
[cset
] : ATTR_ASCII
);
685 if (curs_x
== cols
) {
694 termstate
= TOPLEVEL
;
698 * This state is virtually identical to SEEN_ESC, with the
699 * exception that we have an OSC sequence in the pipeline,
700 * and _if_ we see a backslash, we process it.
704 termstate
= TOPLEVEL
;
707 /* else fall through */
709 termstate
= TOPLEVEL
;
711 case ' ': /* some weird sequence? */
712 termstate
= IGNORE_NEXT
;
714 case '[': /* enter CSI mode */
715 termstate
= SEEN_CSI
;
717 esc_args
[0] = ARG_DEFAULT
;
720 case ']': /* xterm escape sequences */
721 termstate
= SEEN_OSC
;
724 case '(': /* should set GL */
727 case ')': /* should set GR */
730 case '7': /* save cursor */
733 case '8': /* restore cursor */
735 seen_disp_event
= TRUE
;
738 app_keypad_keys
= TRUE
;
741 app_keypad_keys
= FALSE
;
743 case 'D': /* exactly equivalent to LF */
744 if (curs_y
== marg_b
)
745 scroll (marg_t
, marg_b
, 1, TRUE
);
746 else if (curs_y
< rows
-1)
750 seen_disp_event
= TRUE
;
753 case 'E': /* exactly equivalent to CR-LF */
756 if (curs_y
== marg_b
)
757 scroll (marg_t
, marg_b
, 1, TRUE
);
758 else if (curs_y
< rows
-1)
763 seen_disp_event
= TRUE
;
765 case 'M': /* reverse index - backwards LF */
766 if (curs_y
== marg_t
)
767 scroll (marg_t
, marg_b
, -1, TRUE
);
772 seen_disp_event
= TRUE
;
774 case 'Z': /* terminal type query */
775 ldisc
->send ("\033[?6c", 5);
777 case 'c': /* restore power-on settings */
781 seen_disp_event
= TRUE
;
783 case '#': /* ESC # 8 fills screen with Es :-) */
784 termstate
= SEEN_ESCHASH
;
786 case 'H': /* set a tab */
792 termstate
= TOPLEVEL
; /* default */
795 if (esc_nargs
<= ARGS_MAX
) {
796 if (esc_args
[esc_nargs
-1] == ARG_DEFAULT
)
797 esc_args
[esc_nargs
-1] = 0;
798 esc_args
[esc_nargs
-1] =
799 10 * esc_args
[esc_nargs
-1] + c
- '0';
801 termstate
= SEEN_CSI
;
805 if (++esc_nargs
<= ARGS_MAX
)
806 esc_args
[esc_nargs
-1] = ARG_DEFAULT
;
807 termstate
= SEEN_CSI
;
811 if( esc_query
) esc_query
= -1;
812 else if( c
== '?' ) esc_query
= TRUE
;
814 termstate
= SEEN_CSI
;
816 else switch (ANSI(c
,esc_query
)) {
817 case 'A': /* move up N lines */
818 move (curs_x
, curs_y
- def(esc_args
[0], 1), 1);
819 seen_disp_event
= TRUE
;
821 case 'B': case 'e': /* move down N lines */
822 move (curs_x
, curs_y
+ def(esc_args
[0], 1), 1);
823 seen_disp_event
= TRUE
;
825 case 'C': case 'a': /* move right N cols */
826 move (curs_x
+ def(esc_args
[0], 1), curs_y
, 1);
827 seen_disp_event
= TRUE
;
829 case 'D': /* move left N cols */
830 move (curs_x
- def(esc_args
[0], 1), curs_y
, 1);
831 seen_disp_event
= TRUE
;
833 case 'E': /* move down N lines and CR */
834 move (0, curs_y
+ def(esc_args
[0], 1), 1);
835 seen_disp_event
= TRUE
;
837 case 'F': /* move up N lines and CR */
838 move (0, curs_y
- def(esc_args
[0], 1), 1);
839 seen_disp_event
= TRUE
;
841 case 'G': case '`': /* set horizontal posn */
842 move (def(esc_args
[0], 1) - 1, curs_y
, 0);
843 seen_disp_event
= TRUE
;
845 case 'd': /* set vertical posn */
846 move (curs_x
, (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
848 seen_disp_event
= TRUE
;
850 case 'H': case 'f': /* set horz and vert posns at once */
852 esc_args
[1] = ARG_DEFAULT
;
853 move (def(esc_args
[1], 1) - 1,
854 (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
856 seen_disp_event
= TRUE
;
858 case 'J': /* erase screen or parts of it */
860 unsigned int i
= def(esc_args
[0], 0) + 1;
863 erase_lots(FALSE
, !!(i
& 2), !!(i
& 1));
866 seen_disp_event
= TRUE
;
868 case 'K': /* erase line or parts of it */
870 unsigned int i
= def(esc_args
[0], 0) + 1;
873 erase_lots(TRUE
, !!(i
& 2), !!(i
& 1));
875 seen_disp_event
= TRUE
;
877 case 'L': /* insert lines */
878 if (curs_y
<= marg_b
)
879 scroll (curs_y
, marg_b
, -def(esc_args
[0], 1), FALSE
);
880 seen_disp_event
= TRUE
;
882 case 'M': /* delete lines */
883 if (curs_y
<= marg_b
)
884 scroll (curs_y
, marg_b
, def(esc_args
[0], 1), TRUE
);
885 seen_disp_event
= TRUE
;
887 case '@': /* insert chars */
888 insch (def(esc_args
[0], 1));
889 seen_disp_event
= TRUE
;
891 case 'P': /* delete chars */
892 insch (-def(esc_args
[0], 1));
893 seen_disp_event
= TRUE
;
895 case 'c': /* terminal type query */
896 ldisc
->send ("\033[?6c", 5);
898 case 'n': /* cursor position query */
899 if (esc_args
[0] == 6) {
901 sprintf (buf
, "\033[%d;%dR", curs_y
+ 1, curs_x
+ 1);
902 ldisc
->send (buf
, strlen(buf
));
905 case 'h': /* toggle a mode to high */
907 toggle_mode (esc_args
[0], esc_query
, TRUE
);
909 case 'l': /* toggle a mode to low */
911 toggle_mode (esc_args
[0], esc_query
, FALSE
);
913 case 'g': /* clear tabs */
914 if (esc_nargs
== 1) {
915 if (esc_args
[0] == 0) {
916 tabs
[curs_x
] = FALSE
;
917 } else if (esc_args
[0] == 3) {
919 for (i
= 0; i
< cols
; i
++)
924 case 'r': /* set scroll margins */
925 if (esc_nargs
<= 2) {
927 top
= def(esc_args
[0], 1) - 1;
930 bot
= (esc_nargs
<= 1 || esc_args
[1] == 0 ? rows
:
931 def(esc_args
[1], rows
)) - 1;
939 * I used to think the cursor should be
940 * placed at the top of the newly marginned
941 * area. Apparently not: VMS TPU falls over
946 seen_disp_event
= TRUE
;
950 case 'm': /* set graphics rendition */
953 for (i
=0; i
<esc_nargs
; i
++) {
954 switch (def(esc_args
[i
], 0)) {
955 case 0: /* restore defaults */
956 curr_attr
= ATTR_DEFAULT
; break;
957 case 1: /* enable bold */
958 curr_attr
|= ATTR_BOLD
; break;
959 case 4: /* enable underline */
960 case 21: /* (enable double underline) */
961 curr_attr
|= ATTR_UNDER
; break;
962 case 7: /* enable reverse video */
963 curr_attr
|= ATTR_REVERSE
; break;
964 case 22: /* disable bold */
965 curr_attr
&= ~ATTR_BOLD
; break;
966 case 24: /* disable underline */
967 curr_attr
&= ~ATTR_UNDER
; break;
968 case 27: /* disable reverse video */
969 curr_attr
&= ~ATTR_REVERSE
; break;
970 case 30: case 31: case 32: case 33:
971 case 34: case 35: case 36: case 37:
973 curr_attr
&= ~ATTR_FGMASK
;
974 curr_attr
|= (esc_args
[i
] - 30) << ATTR_FGSHIFT
;
976 case 39: /* default-foreground */
977 curr_attr
&= ~ATTR_FGMASK
;
978 curr_attr
|= ATTR_DEFFG
;
980 case 40: case 41: case 42: case 43:
981 case 44: case 45: case 46: case 47:
983 curr_attr
&= ~ATTR_BGMASK
;
984 curr_attr
|= (esc_args
[i
] - 40) << ATTR_BGSHIFT
;
986 case 49: /* default-background */
987 curr_attr
&= ~ATTR_BGMASK
;
988 curr_attr
|= ATTR_DEFBG
;
994 case 's': /* save cursor */
997 case 'u': /* restore cursor */
999 seen_disp_event
= TRUE
;
1001 case 't': /* set page size - ie window height */
1002 request_resize (cols
, def(esc_args
[0], 24));
1005 case 'X': /* write N spaces w/o moving cursor */
1007 int n
= def(esc_args
[0], 1);
1008 unsigned long *p
= cpos
;
1009 if (n
> cols
- curs_x
)
1011 check_selection (cpos
, cpos
+n
);
1014 seen_disp_event
= TRUE
;
1017 case 'x': /* report terminal characteristics */
1020 int i
= def(esc_args
[0], 0);
1021 if (i
== 0 || i
== 1) {
1022 strcpy (buf
, "\033[2;1;1;112;112;1;0x");
1024 ldisc
->send (buf
, 20);
1034 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_GBCHR
;
1037 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_LINEDRW
;
1039 default: /* specifically, 'B' */
1040 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_ASCII
;
1044 termstate
= TOPLEVEL
;
1049 case 'P': /* Linux palette sequence */
1050 termstate
= SEEN_OSC_P
;
1053 case 'R': /* Linux palette reset */
1056 termstate
= TOPLEVEL
;
1058 case 'W': /* word-set */
1059 termstate
= SEEN_OSC_W
;
1062 case '0': case '1': case '2': case '3': case '4':
1063 case '5': case '6': case '7': case '8': case '9':
1064 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1068 * Grotty hack to support xterm and DECterm title
1069 * sequences concurrently.
1071 if (esc_args
[0] == 2) {
1075 /* else fall through */
1077 termstate
= OSC_STRING
;
1083 * This OSC stuff is EVIL. It takes just one character to get into
1084 * sysline mode and it's not initially obvious how to get out.
1085 * So I've added CR and LF as string aborts.
1086 * This shouldn't effect compatibility as I believe embedded
1087 * control characters are supposed to be interpreted (maybe?)
1088 * and they don't display anything useful anyway.
1092 if (c
== '\n' || c
== '\r') {
1093 termstate
= TOPLEVEL
;
1094 } else if (c
== 0234 || c
== '\007' ) {
1096 * These characters terminate the string; ST and BEL
1097 * terminate the sequence and trigger instant
1098 * processing of it, whereas ESC goes back to SEEN_ESC
1099 * mode unless it is followed by \, in which case it is
1100 * synonymous with ST in the first place.
1103 termstate
= TOPLEVEL
;
1104 } else if (c
== '\033')
1105 termstate
= OSC_MAYBE_ST
;
1106 else if (osc_strlen
< OSC_STR_MAX
)
1107 osc_string
[osc_strlen
++] = c
;
1111 int max
= (osc_strlen
== 0 ?
21 : 16);
1113 if (c
>= '0' && c
<= '9')
1115 else if (c
>= 'A' && c
<= 'A'+max
-10)
1117 else if (c
>= 'a' && c
<= 'a'+max
-10)
1120 termstate
= TOPLEVEL
;
1121 osc_string
[osc_strlen
++] = val
;
1122 if (osc_strlen
>= 7) {
1123 palette_set (osc_string
[0],
1124 osc_string
[1] * 16 + osc_string
[2],
1125 osc_string
[3] * 16 + osc_string
[4],
1126 osc_string
[5] * 16 + osc_string
[6]);
1128 termstate
= TOPLEVEL
;
1134 case '0': case '1': case '2': case '3': case '4':
1135 case '5': case '6': case '7': case '8': case '9':
1136 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1139 termstate
= OSC_STRING
;
1145 unsigned long *p
= scrtop
;
1146 int n
= rows
* (cols
+1);
1148 *p
++ = ATTR_DEFAULT
| 'E';
1150 seen_disp_event
= TRUE
;
1151 check_selection (scrtop
, scrtop
+ rows
* (cols
+1));
1153 termstate
= TOPLEVEL
;
1156 check_selection (cpos
, cpos
+1);
1159 if (nl_count
> MAXNL
)
1164 * Compare two lines to determine whether they are sufficiently
1165 * alike to scroll-optimise one to the other. Return the degree of
1168 static int linecmp (unsigned long *a
, unsigned long *b
) {
1171 for (i
=n
=0; i
< cols
; i
++)
1172 n
+= (*a
++ == *b
++);
1177 * Given a context, update the window. Out of paranoia, we don't
1178 * allow WM_PAINT responses to do scrolling optimisations.
1180 static void do_paint (Context ctx
, int may_optimise
){
1181 int i
, j
, start
, our_curs_y
;
1182 unsigned long attr
, rv
, cursor
;
1185 cursor
= (has_focus ? ATTR_ACTCURS
: ATTR_PASCURS
);
1186 rv
= (rvideo ? ATTR_REVERSE
: 0);
1187 our_curs_y
= curs_y
+ (scrtop
- disptop
) / (cols
+1);
1189 for (i
=0; i
<rows
; i
++) {
1190 int idx
= i
*(cols
+1);
1191 for (j
=0; j
<=cols
; j
++,idx
++) {
1192 unsigned long *d
= disptop
+idx
;
1193 wanttext
[idx
] = ((*d
^ rv
1194 ^ (selstart
<= d
&& d
< selend ?
1195 ATTR_REVERSE
: 0)) |
1196 (i
==our_curs_y
&& j
==curs_x ? cursor
: 0));
1201 * We would perform scrolling optimisations in here, if they
1202 * didn't have a nasty tendency to cause the whole sodding
1203 * program to hang for a second at speed-critical moments.
1204 * We'll leave it well alone...
1207 for (i
=0; i
<rows
; i
++) {
1208 int idx
= i
*(cols
+1);
1210 for (j
=0; j
<=cols
; j
++,idx
++) {
1211 unsigned long t
= wanttext
[idx
];
1212 int needs_update
= (j
< cols
&& t
!= disptext
[idx
]);
1213 int keep_going
= (start
!= -1 && needs_update
&&
1214 (t
& ATTR_MASK
) == attr
&&
1215 j
-start
< sizeof(ch
));
1216 if (start
!= -1 && !keep_going
) {
1217 do_text (ctx
, start
, i
, ch
, j
-start
, attr
);
1223 attr
= t
& ATTR_MASK
;
1225 ch
[j
-start
] = (char) (t
& CHAR_MASK
);
1233 * Invalidate the whole screen so it will be repainted in full.
1235 void term_invalidate(void) {
1238 for (i
=0; i
<rows
*(cols
+1); i
++)
1239 disptext
[i
] = ATTR_INVALID
;
1243 * Paint the window in response to a WM_PAINT message.
1245 void term_paint (Context ctx
, int l
, int t
, int r
, int b
) {
1246 int i
, j
, left
, top
, right
, bottom
;
1248 left
= l
/ font_width
;
1249 right
= (r
- 1) / font_width
;
1250 top
= t
/ font_height
;
1251 bottom
= (b
- 1) / font_height
;
1252 for (i
= top
; i
<= bottom
&& i
< rows
; i
++)
1253 for (j
= left
; j
<= right
&& j
< cols
; j
++)
1254 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
1256 do_paint (ctx
, FALSE
);
1260 * Attempt to scroll the scrollback. The second parameter gives the
1261 * position we want to scroll to; the first is +1 to denote that
1262 * this position is relative to the beginning of the scrollback, -1
1263 * to denote it is relative to the end, and 0 to denote that it is
1264 * relative to the current position.
1266 void term_scroll (int rel
, int where
) {
1267 int n
= where
* (cols
+1);
1269 disptop
= (rel
< 0 ? scrtop
:
1270 rel
> 0 ? sbtop
: disptop
) + n
;
1271 if (disptop
< sbtop
)
1273 if (disptop
> scrtop
)
1280 * Spread the selection outwards according to the selection mode.
1282 static unsigned long *sel_spread_half (unsigned long *p
, int dir
) {
1283 unsigned long *linestart
, *lineend
;
1287 x
= (p
- text
) % (cols
+1);
1289 lineend
= linestart
+ cols
;
1294 * In this mode, every character is a separate unit, except
1295 * for runs of spaces at the end of a non-wrapping line.
1297 if (!(linestart
[cols
] & ATTR_WRAPPED
)) {
1298 unsigned long *q
= lineend
;
1299 while (q
> linestart
&& (q
[-1] & CHAR_MASK
) == 0x20)
1304 p
= (dir
== -1 ? q
: lineend
- 1);
1309 * In this mode, the units are maximal runs of characters
1310 * whose `wordness' has the same value.
1312 wvalue
= wordness
[*p
& CHAR_MASK
];
1314 while (p
< lineend
&& wordness
[p
[1] & CHAR_MASK
] == wvalue
)
1317 while (p
> linestart
&& wordness
[p
[-1] & CHAR_MASK
] == wvalue
)
1323 * In this mode, every line is a unit.
1325 p
= (dir
== -1 ? linestart
: lineend
- 1);
1331 static void sel_spread (void) {
1332 selstart
= sel_spread_half (selstart
, -1);
1333 selend
= sel_spread_half (selend
- 1, +1) + 1;
1336 void term_mouse (Mouse_Button b
, Mouse_Action a
, int x
, int y
) {
1337 unsigned long *selpoint
;
1340 if (y
>=rows
) y
= rows
-1;
1348 if (x
>=cols
) x
= cols
-1;
1350 selpoint
= disptop
+ y
* (cols
+1) + x
;
1352 if (b
== MB_SELECT
&& a
== MA_CLICK
) {
1354 selstate
= ABOUT_TO
;
1355 selanchor
= selpoint
;
1357 } else if (b
== MB_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
1359 selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
1360 selstate
= DRAGGING
;
1361 selstart
= selanchor
= selpoint
;
1362 selend
= selstart
+ 1;
1364 } else if ((b
== MB_SELECT
&& a
== MA_DRAG
) ||
1365 (b
== MB_EXTEND
&& a
!= MA_RELEASE
)) {
1366 if (selstate
== ABOUT_TO
&& selanchor
== selpoint
)
1368 if (b
== MB_EXTEND
&& a
!= MA_DRAG
&& selstate
== SELECTED
) {
1369 if (selpoint
-selstart
< (selend
-selstart
)/2)
1370 selanchor
= selend
- 1;
1372 selanchor
= selstart
;
1373 selstate
= DRAGGING
;
1375 if (selstate
!= ABOUT_TO
&& selstate
!= DRAGGING
)
1376 selanchor
= selpoint
;
1377 selstate
= DRAGGING
;
1378 if (selpoint
< selanchor
) {
1379 selstart
= selpoint
;
1380 selend
= selanchor
+ 1;
1382 selstart
= selanchor
;
1383 selend
= selpoint
+ 1;
1386 } else if ((b
== MB_SELECT
|| b
== MB_EXTEND
) && a
== MA_RELEASE
) {
1387 if (selstate
== DRAGGING
) {
1389 * We've completed a selection. We now transfer the
1390 * data to the clipboard.
1392 unsigned char *p
= selspace
;
1393 unsigned long *q
= selstart
;
1395 while (q
< selend
) {
1397 unsigned long *lineend
= q
- (q
-text
) % (cols
+1) + cols
;
1398 unsigned long *nlpos
= lineend
;
1400 if (!(*nlpos
& ATTR_WRAPPED
)) {
1401 while ((nlpos
[-1] & CHAR_MASK
) == 0x20 && nlpos
> q
)
1406 while (q
< nlpos
&& q
< selend
)
1407 *p
++ = (unsigned char) (*q
++ & CHAR_MASK
);
1410 for (i
=0; i
<sizeof(sel_nl
); i
++)
1413 q
= lineend
+ 1; /* start of next line */
1415 write_clip (selspace
, p
- selspace
);
1416 selstate
= SELECTED
;
1418 selstate
= NO_SELECTION
;
1419 } else if (b
== MB_PASTE
&& (a
==MA_CLICK
|| a
==MA_2CLK
|| a
==MA_3CLK
)) {
1423 get_clip((void **) &data
, &len
);
1427 while (p
< data
+len
) {
1428 while (p
< data
+len
&&
1429 !(p
<= data
+len
-sizeof(sel_nl
) &&
1430 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
1438 c
=xlat_kbd2tty(q
[i
]);
1443 if (p
<= data
+len
-sizeof(sel_nl
) &&
1444 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
1445 ldisc
->send ("\r", 1);
1446 p
+= sizeof(sel_nl
);
1451 get_clip(NULL
, NULL
);
1457 static void deselect (void) {
1458 selstate
= NO_SELECTION
;
1459 selstart
= selend
= scrtop
;
1462 void term_deselect (void) {