8 static unsigned long *text
; /* buffer of text on terminal screen */
9 static unsigned long *scrtop
; /* top of working screen */
10 static unsigned long *disptop
; /* top of displayed screen */
11 static unsigned long *sbtop
; /* top of scrollback */
12 static unsigned long *cpos
; /* cursor position (convenience) */
13 static unsigned long *disptext
; /* buffer of text on real screen */
14 static unsigned long *wanttext
; /* buffer of text we want on screen */
15 static unsigned long *alttext
; /* buffer of text on alt. screen */
17 static unsigned char *selspace
; /* buffer for building selections in */
19 #define TSIZE (sizeof(*text))
20 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
22 static unsigned long curr_attr
, save_attr
;
24 static int curs_x
, curs_y
; /* cursor */
25 static int save_x
, save_y
; /* saved cursor position */
26 static int marg_t
, marg_b
; /* scroll margins */
27 static int dec_om
; /* DEC origin mode flag */
28 static int wrap
, wrapnext
; /* wrap flags */
29 static int insert
; /* insert-mode flag */
30 static int cset
; /* 0 or 1: which char set */
31 static int save_cset
, save_csattr
; /* saved with cursor position */
32 static int rvideo
; /* global reverse video flag */
34 static unsigned long cset_attr
[2];
37 * Saved settings on the alternate screen.
39 static int alt_x
, alt_y
, alt_om
, alt_wrap
, alt_wnext
, alt_ins
, alt_cset
;
40 static int alt_t
, alt_b
;
43 #define ARGS_MAX 32 /* max # of esc sequence arguments */
44 #define ARG_DEFAULT -1 /* if an arg isn't specified */
45 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
46 static int esc_args
[ARGS_MAX
];
50 #define OSC_STR_MAX 2048
51 static int osc_strlen
;
52 static char osc_string
[OSC_STR_MAX
+1];
55 static unsigned char *tabs
;
60 static int scroll_heuristic
;
74 OSC_STRING
, OSC_MAYBE_ST
,
79 NO_SELECTION
, ABOUT_TO
, DRAGGING
, SELECTED
82 SM_CHAR
, SM_WORD
, SM_LINE
84 static unsigned long *selstart
, *selend
, *selanchor
;
86 static short wordness
[256] = {
87 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 01 */
88 0,1,2,1,1,1,1,1,1,1,1,1,1,2,2,2, 2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1, /* 23 */
89 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2, /* 45 */
90 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1, /* 67 */
91 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 89 */
92 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* AB */
93 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* CD */
94 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* EF */
97 static unsigned char sel_nl
[] = SEL_NL
;
100 * Internal prototypes.
102 static void do_paint (Context
, int);
103 static void erase_lots (int, int, int);
104 static void swap_screen (int);
105 static void update_sbar (void);
106 static void deselect (void);
109 * Set up power-on settings for the terminal.
111 static void power_on(void) {
112 curs_x
= curs_y
= alt_x
= alt_y
= save_x
= save_y
= 0;
115 alt_b
= marg_b
= rows
- 1;
120 for (i
= 0; i
< cols
; i
++)
121 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
123 alt_om
= dec_om
= cfg
.dec_om
;
124 alt_wnext
= wrapnext
= alt_ins
= insert
= FALSE
;
125 alt_wrap
= wrap
= cfg
.wrap_mode
;
127 cset_attr
[0] = cset_attr
[1] = ATTR_ASCII
;
129 save_attr
= curr_attr
= ATTR_DEFAULT
;
130 app_cursor_keys
= cfg
.app_cursor
;
131 app_keypad_keys
= cfg
.app_keypad
;
135 for (i
= 0; i
< 256; i
++)
136 wordness
[i
] = cfg
.wordness
[i
];
140 erase_lots (FALSE
, TRUE
, TRUE
);
142 erase_lots (FALSE
, TRUE
, TRUE
);
147 * Force a screen update.
149 void term_update(void) {
153 if ( (seen_key_event
&& (unscroll_event
& US_KEY
)) &&
154 (seen_disp_event
&& (unscroll_event
& US_DISP
)) ) {
156 seen_disp_event
= seen_key_event
= 0;
158 do_paint (ctx
, TRUE
);
161 scroll_heuristic
= 0;
166 * Same as power_on(), but an external function.
168 void term_pwron(void) {
177 * Clear the scrollback.
179 void term_clrsb(void) {
180 disptop
= sbtop
= scrtop
;
185 * Initialise the terminal.
187 void term_init(void) {
188 text
= sbtop
= scrtop
= disptop
= cpos
= NULL
;
189 disptext
= wanttext
= NULL
;
199 * Set up the terminal for a given size.
201 void term_size(int newrows
, int newcols
, int newsavelines
) {
202 unsigned long *newtext
, *newdisp
, *newwant
, *newalt
;
203 int i
, j
, crows
, ccols
;
205 if (newrows
== rows
&& newcols
== cols
&& newsavelines
== savelines
)
206 return; /* nothing to do */
209 alt_b
= marg_b
= newrows
- 1;
211 newtext
= smalloc ((newrows
+newsavelines
)*(newcols
+1)*TSIZE
);
212 disptop
= newtext
+ newsavelines
*(newcols
+1);
213 for (i
=0; i
<(newrows
+newsavelines
)*(newcols
+1); i
++)
214 newtext
[i
] = ERASE_CHAR
;
216 crows
= rows
+ (scrtop
- sbtop
) / (cols
+1);
217 if (crows
> newrows
+newsavelines
)
218 crows
= newrows
+newsavelines
;
219 ccols
= (cols
< newcols ? cols
: newcols
);
220 for (i
=0; i
<crows
; i
++) {
221 int oldidx
= (rows
+ savelines
- crows
+ i
) * (cols
+1);
222 int newidx
= (newrows
+ newsavelines
- crows
+ i
) * (newcols
+1);
223 for (j
=0; j
<ccols
; j
++)
224 newtext
[newidx
+j
] = text
[oldidx
+j
];
225 newtext
[newidx
+newcols
] =
226 (cols
== newcols ? text
[oldidx
+cols
] : 0);
228 sbtop
= disptop
- (crows
- newrows
) * (newcols
+1);
237 newdisp
= smalloc (newrows
*(newcols
+1)*TSIZE
);
238 for (i
=0; i
<newrows
*(newcols
+1); i
++)
239 newdisp
[i
] = ATTR_INVALID
;
243 newwant
= smalloc (newrows
*(newcols
+1)*TSIZE
);
244 for (i
=0; i
<newrows
*(newcols
+1); i
++)
245 newwant
[i
] = ATTR_INVALID
;
249 newalt
= smalloc (newrows
*(newcols
+1)*TSIZE
);
250 for (i
=0; i
<newrows
*(newcols
+1); i
++)
251 newalt
[i
] = ERASE_CHAR
;
256 selspace
= smalloc ( (newrows
+newsavelines
) * (newcols
+sizeof(sel_nl
)) );
258 tabs
= srealloc (tabs
, newcols
*sizeof(*tabs
));
261 for (i
= (cols
> 0 ? cols
: 0); i
< newcols
; i
++)
262 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
266 curs_y
+= newrows
- rows
;
269 if (curs_y
>= newrows
)
271 if (curs_x
>= newcols
)
274 wrapnext
= alt_wnext
= FALSE
;
278 savelines
= newsavelines
;
289 static void swap_screen (int which
) {
293 if (which
== alt_which
)
298 for (t
=0; t
<rows
*(cols
+1); t
++) {
299 tt
= scrtop
[t
]; scrtop
[t
] = alttext
[t
]; alttext
[t
] = tt
;
302 t
= curs_x
; curs_x
= alt_x
; alt_x
= t
;
303 t
= curs_y
; curs_y
= alt_y
; alt_y
= t
;
304 t
= marg_t
; marg_t
= alt_t
; alt_t
= t
;
305 t
= marg_b
; marg_b
= alt_b
; alt_b
= t
;
306 t
= dec_om
; dec_om
= alt_om
; alt_om
= t
;
307 t
= wrap
; wrap
= alt_wrap
; alt_wrap
= t
;
308 t
= wrapnext
; wrapnext
= alt_wnext
; alt_wnext
= t
;
309 t
= insert
; insert
= alt_ins
; alt_ins
= t
;
310 t
= cset
; cset
= alt_cset
; alt_cset
= t
;
316 * Retrieve a character from `inbuf'.
318 static int inbuf_getc(void) {
319 if (inbuf_head
== inbuf_reap
)
323 inbuf_reap
= (inbuf_reap
+1) & INBUF_MASK
;
329 * Update the scroll bar.
331 static void update_sbar(void) {
334 min
= (sbtop
- text
) / (cols
+1);
335 set_sbar ((scrtop
- text
) / (cols
+1) + rows
- min
,
336 (disptop
- text
) / (cols
+1) - min
,
341 * Check whether the region bounded by the two pointers intersects
342 * the scroll region, and de-select the on-screen selection if so.
344 static void check_selection (unsigned long *from
, unsigned long *to
) {
345 if (from
< selend
&& selstart
< to
)
350 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
351 * for backward.) `sb' is TRUE if the scrolling is permitted to
352 * affect the scrollback buffer.
354 static void scroll (int topline
, int botline
, int lines
, int sb
) {
355 unsigned long *scroll_top
;
356 int scroll_size
, size
, i
;
358 scroll_top
= scrtop
+ topline
*(cols
+1);
359 size
= (lines
< 0 ?
-lines
: lines
) * (cols
+1);
360 scroll_size
= (botline
- topline
+ 1) * (cols
+1) - size
;
362 if (lines
> 0 && topline
== 0 && alt_which
== 0 && sb
) {
364 * Since we're going to scroll the top line and we're on the
365 * scrolling screen let's also affect the scrollback buffer.
367 sbtop
-= lines
* (cols
+1);
370 scroll_size
+= scroll_top
- sbtop
;
375 if (scroll_size
< 0) {
382 memmove (scroll_top
, scroll_top
+ size
, scroll_size
*TSIZE
);
383 for (i
= 0; i
< size
; i
++)
384 scroll_top
[i
+scroll_size
] = ERASE_CHAR
;
385 if (selstart
> scroll_top
&&
386 selstart
< scroll_top
+ size
+ scroll_size
) {
388 if (selstart
< scroll_top
)
389 selstart
= scroll_top
;
391 if (selend
> scroll_top
&&
392 selend
< scroll_top
+ size
+ scroll_size
) {
394 if (selend
< scroll_top
)
399 memmove (scroll_top
+ size
, scroll_top
, scroll_size
*TSIZE
);
400 for (i
= 0; i
< size
; i
++)
401 scroll_top
[i
] = ERASE_CHAR
;
402 if (selstart
> scroll_top
&&
403 selstart
< scroll_top
+ size
+ scroll_size
) {
405 if (selstart
> scroll_top
+ size
+ scroll_size
)
406 selstart
= scroll_top
+ size
+ scroll_size
;
408 if (selend
> scroll_top
&&
409 selend
< scroll_top
+ size
+ scroll_size
) {
411 if (selend
> scroll_top
+ size
+ scroll_size
)
412 selend
= scroll_top
+ size
+ scroll_size
;
416 scroll_heuristic
+= lines
;
420 * Move the cursor to a given position, clipping at boundaries. We
421 * may or may not want to clip at the scroll margin: marg_clip is 0
422 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
423 * even _being_ outside the margins.
425 static void move (int x
, int y
, int marg_clip
) {
431 if ((curs_y
>= marg_t
|| marg_clip
== 2) && y
< marg_t
)
433 if ((curs_y
<= marg_b
|| marg_clip
== 2) && y
> marg_b
)
447 * Save or restore the cursor and SGR mode.
449 static void save_cursor(int save
) {
453 save_attr
= curr_attr
;
455 save_csattr
= cset_attr
[cset
];
459 curr_attr
= save_attr
;
461 cset_attr
[cset
] = save_csattr
;
467 * Erase a large portion of the screen: the whole screen, or the
468 * whole line, or parts thereof.
470 static void erase_lots (int line_only
, int from_begin
, int to_end
) {
471 unsigned long *startpos
, *endpos
;
474 startpos
= cpos
- curs_x
;
475 endpos
= startpos
+ cols
+1;
478 endpos
= startpos
+ rows
* (cols
+1);
484 check_selection (startpos
, endpos
);
485 while (startpos
< endpos
)
486 *startpos
++ = ERASE_CHAR
;
490 * Insert or delete characters within the current line. n is +ve if
491 * insertion is desired, and -ve for deletion.
493 static void insch (int n
) {
494 int dir
= (n
< 0 ?
-1 : +1);
497 n
= (n
< 0 ?
-n
: n
);
498 if (n
> cols
- curs_x
)
500 m
= cols
- curs_x
- n
;
501 check_selection (cpos
, cpos
+n
);
503 memmove (cpos
, cpos
+n
, m
*TSIZE
);
505 cpos
[m
++] = ERASE_CHAR
;
507 memmove (cpos
+n
, cpos
, m
*TSIZE
);
509 cpos
[n
] = ERASE_CHAR
;
514 * Toggle terminal mode `mode' to state `state'. (`query' indicates
515 * whether the mode is a DEC private one or a normal one.)
517 static void toggle_mode (int mode
, int query
, int state
) {
518 if (query
) switch (mode
) {
519 case 1: /* application cursor keys */
520 app_cursor_keys
= state
;
522 case 3: /* 80/132 columns */
524 request_resize (state ?
132 : 80, rows
);
526 case 5: /* reverse video */
528 seen_disp_event
= TRUE
;
530 case 6: /* DEC origin mode */
533 case 7: /* auto wrap */
536 case 47: /* alternate screen */
541 } else switch (mode
) {
542 case 4: /* set insert mode */
549 * Process an OSC sequence: set window title or icon name.
551 static void do_osc(void) {
554 wordness
[(unsigned char)osc_string
[osc_strlen
]] = esc_args
[0];
556 osc_string
[osc_strlen
] = '\0';
557 switch (esc_args
[0]) {
560 set_icon (osc_string
);
561 if (esc_args
[0] == 1)
563 /* fall through: parameter 0 means set both */
566 set_title (osc_string
);
573 * Remove everything currently in `inbuf' and stick it up on the
574 * in-memory display. There's a big state machine in here to
575 * process escape sequences...
577 void term_out(void) {
580 while ( (c
= inbuf_getc()) != -1) {
582 * Optionally log the session traffic to a file. Useful for
583 * debugging and possibly also useful for actual logging.
586 static FILE *fp
= NULL
;
587 if (!fp
) fp
= fopen(logfile
, "wb");
588 if (fp
) fputc (c
, fp
);
590 if( termstate
< DO_CTRLS
&& (c
&0x60) == 0 ) {
592 case '\005': /* terminal type query */
593 ldisc
->send ("PuTTY\r", 6);
600 if (curs_x
== 0 && curs_y
> 0)
601 curs_x
= cols
-1, curs_y
--;
607 seen_disp_event
= TRUE
;
616 termstate
= SEEN_ESC
;
619 termstate
= SEEN_CSI
;
621 esc_args
[0] = ARG_DEFAULT
;
625 termstate
= SEEN_OSC
;
632 seen_disp_event
= TRUE
;
637 if (curs_y
== marg_b
)
638 scroll (marg_t
, marg_b
, 1, TRUE
);
639 else if (curs_y
< rows
-1)
651 } while (curs_x
< cols
-1 && !tabs
[curs_x
]);
655 unsigned long *old_cpos
= cpos
;
657 check_selection (old_cpos
, cpos
);
659 seen_disp_event
= TRUE
;
663 else switch (termstate
) {
665 if (c
>= ' ' && c
!= 0234) {
667 cpos
[1] = ATTR_WRAPPED
;
668 if (curs_y
== marg_b
)
669 scroll (marg_t
, marg_b
, 1, TRUE
);
670 else if (curs_y
< rows
-1)
679 check_selection (cpos
, cpos
+1);
680 *cpos
++ = xlat_tty2scr((unsigned char)c
) | curr_attr
|
681 (c
<= 0x7F ? cset_attr
[cset
] : ATTR_ASCII
);
683 if (curs_x
== cols
) {
693 termstate
= TOPLEVEL
;
697 * This state is virtually identical to SEEN_ESC, with the
698 * exception that we have an OSC sequence in the pipeline,
699 * and _if_ we see a backslash, we process it.
703 termstate
= TOPLEVEL
;
706 /* else fall through */
708 termstate
= TOPLEVEL
;
710 case ' ': /* some weird sequence? */
711 termstate
= IGNORE_NEXT
;
713 case '[': /* enter CSI mode */
714 termstate
= SEEN_CSI
;
716 esc_args
[0] = ARG_DEFAULT
;
719 case ']': /* xterm escape sequences */
720 termstate
= SEEN_OSC
;
723 case '(': /* should set GL */
726 case ')': /* should set GR */
729 case '7': /* save cursor */
732 case '8': /* restore cursor */
734 seen_disp_event
= TRUE
;
737 app_keypad_keys
= TRUE
;
740 app_keypad_keys
= FALSE
;
742 case 'D': /* exactly equivalent to LF */
743 if (curs_y
== marg_b
)
744 scroll (marg_t
, marg_b
, 1, TRUE
);
745 else if (curs_y
< rows
-1)
749 seen_disp_event
= TRUE
;
752 case 'E': /* exactly equivalent to CR-LF */
755 if (curs_y
== marg_b
)
756 scroll (marg_t
, marg_b
, 1, TRUE
);
757 else if (curs_y
< rows
-1)
762 seen_disp_event
= TRUE
;
764 case 'M': /* reverse index - backwards LF */
765 if (curs_y
== marg_t
)
766 scroll (marg_t
, marg_b
, -1, TRUE
);
771 seen_disp_event
= TRUE
;
773 case 'Z': /* terminal type query */
774 ldisc
->send ("\033[?6c", 5);
776 case 'c': /* restore power-on settings */
780 seen_disp_event
= TRUE
;
782 case '#': /* ESC # 8 fills screen with Es :-) */
783 termstate
= SEEN_ESCHASH
;
785 case 'H': /* set a tab */
791 termstate
= TOPLEVEL
; /* default */
793 case '0': case '1': case '2': case '3': case '4':
794 case '5': case '6': case '7': case '8': case '9':
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
;
804 if (++esc_nargs
<= ARGS_MAX
)
805 esc_args
[esc_nargs
-1] = ARG_DEFAULT
;
806 termstate
= SEEN_CSI
;
810 termstate
= SEEN_CSI
;
812 case 'A': /* move up N lines */
813 move (curs_x
, curs_y
- def(esc_args
[0], 1), 1);
814 seen_disp_event
= TRUE
;
816 case 'B': case 'e': /* move down N lines */
817 move (curs_x
, curs_y
+ def(esc_args
[0], 1), 1);
818 seen_disp_event
= TRUE
;
820 case 'C': case 'a': /* move right N cols */
821 move (curs_x
+ def(esc_args
[0], 1), curs_y
, 1);
822 seen_disp_event
= TRUE
;
824 case 'D': /* move left N cols */
825 move (curs_x
- def(esc_args
[0], 1), curs_y
, 1);
826 seen_disp_event
= TRUE
;
828 case 'E': /* move down N lines and CR */
829 move (0, curs_y
+ def(esc_args
[0], 1), 1);
830 seen_disp_event
= TRUE
;
832 case 'F': /* move up N lines and CR */
833 move (0, curs_y
- def(esc_args
[0], 1), 1);
834 seen_disp_event
= TRUE
;
836 case 'G': case '`': /* set horizontal posn */
837 move (def(esc_args
[0], 1) - 1, curs_y
, 0);
838 seen_disp_event
= TRUE
;
840 case 'd': /* set vertical posn */
841 move (curs_x
, (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
843 seen_disp_event
= TRUE
;
845 case 'H': case 'f': /* set horz and vert posns at once */
847 esc_args
[1] = ARG_DEFAULT
;
848 move (def(esc_args
[1], 1) - 1,
849 (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
851 seen_disp_event
= TRUE
;
853 case 'J': /* erase screen or parts of it */
855 unsigned int i
= def(esc_args
[0], 0) + 1;
858 erase_lots(FALSE
, !!(i
& 2), !!(i
& 1));
861 seen_disp_event
= TRUE
;
863 case 'K': /* erase line or parts of it */
865 unsigned int i
= def(esc_args
[0], 0) + 1;
868 erase_lots(TRUE
, !!(i
& 2), !!(i
& 1));
870 seen_disp_event
= TRUE
;
872 case 'L': /* insert lines */
873 if (curs_y
<= marg_b
)
874 scroll (curs_y
, marg_b
, -def(esc_args
[0], 1), FALSE
);
875 seen_disp_event
= TRUE
;
877 case 'M': /* delete lines */
878 if (curs_y
<= marg_b
)
879 scroll (curs_y
, marg_b
, def(esc_args
[0], 1), TRUE
);
880 seen_disp_event
= TRUE
;
882 case '@': /* insert chars */
883 insch (def(esc_args
[0], 1));
884 seen_disp_event
= TRUE
;
886 case 'P': /* delete chars */
887 insch (-def(esc_args
[0], 1));
888 seen_disp_event
= TRUE
;
890 case 'c': /* terminal type query */
891 ldisc
->send ("\033[?6c", 5);
893 case 'n': /* cursor position query */
894 if (esc_args
[0] == 6) {
896 sprintf (buf
, "\033[%d;%dR", curs_y
+ 1, curs_x
+ 1);
897 ldisc
->send (buf
, strlen(buf
));
900 case 'h': /* toggle a mode to high */
901 toggle_mode (esc_args
[0], esc_query
, TRUE
);
903 case 'l': /* toggle a mode to low */
904 toggle_mode (esc_args
[0], esc_query
, FALSE
);
906 case 'g': /* clear tabs */
907 if (esc_nargs
== 1) {
908 if (esc_args
[0] == 0) {
909 tabs
[curs_x
] = FALSE
;
910 } else if (esc_args
[0] == 3) {
912 for (i
= 0; i
< cols
; i
++)
917 case 'r': /* set scroll margins */
918 if (!esc_query
&& esc_nargs
<= 2) {
920 top
= def(esc_args
[0], 1) - 1;
923 bot
= (esc_nargs
<= 1 || esc_args
[1] == 0 ? rows
:
924 def(esc_args
[1], rows
)) - 1;
932 * I used to think the cursor should be
933 * placed at the top of the newly marginned
934 * area. Apparently not: VMS TPU falls over
939 seen_disp_event
= TRUE
;
943 case 'm': /* set graphics rendition */
946 for (i
=0; i
<esc_nargs
; i
++) {
947 switch (def(esc_args
[i
], 0)) {
948 case 0: /* restore defaults */
949 curr_attr
= ATTR_DEFAULT
; break;
950 case 1: /* enable bold */
951 curr_attr
|= ATTR_BOLD
; break;
952 case 4: /* enable underline */
953 case 21: /* (enable double underline) */
954 curr_attr
|= ATTR_UNDER
; break;
955 case 7: /* enable reverse video */
956 curr_attr
|= ATTR_REVERSE
; break;
957 case 22: /* disable bold */
958 curr_attr
&= ~ATTR_BOLD
; break;
959 case 24: /* disable underline */
960 curr_attr
&= ~ATTR_UNDER
; break;
961 case 27: /* disable reverse video */
962 curr_attr
&= ~ATTR_REVERSE
; break;
963 case 30: case 31: case 32: case 33:
964 case 34: case 35: case 36: case 37:
966 curr_attr
&= ~ATTR_FGMASK
;
967 curr_attr
|= (esc_args
[i
] - 30) << ATTR_FGSHIFT
;
969 case 39: /* default-foreground */
970 curr_attr
&= ~ATTR_FGMASK
;
971 curr_attr
|= ATTR_DEFFG
;
973 case 40: case 41: case 42: case 43:
974 case 44: case 45: case 46: case 47:
976 curr_attr
&= ~ATTR_BGMASK
;
977 curr_attr
|= (esc_args
[i
] - 40) << ATTR_BGSHIFT
;
979 case 49: /* default-background */
980 curr_attr
&= ~ATTR_BGMASK
;
981 curr_attr
|= ATTR_DEFBG
;
987 case 's': /* save cursor */
990 case 'u': /* restore cursor */
992 seen_disp_event
= TRUE
;
994 case 't': /* set page size - ie window height */
995 request_resize (cols
, def(esc_args
[0], 24));
998 case 'X': /* write N spaces w/o moving cursor */
1000 int n
= def(esc_args
[0], 1);
1001 unsigned long *p
= cpos
;
1002 if (n
> cols
- curs_x
)
1004 check_selection (cpos
, cpos
+n
);
1007 seen_disp_event
= TRUE
;
1010 case 'x': /* report terminal characteristics */
1013 int i
= def(esc_args
[0], 0);
1014 if (i
== 0 || i
== 1) {
1015 strcpy (buf
, "\033[2;1;1;112;112;1;0x");
1017 ldisc
->send (buf
, 20);
1027 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_GBCHR
;
1030 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_LINEDRW
;
1032 default: /* specifically, 'B' */
1033 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_ASCII
;
1036 termstate
= TOPLEVEL
;
1041 case 'P': /* Linux palette sequence */
1042 termstate
= SEEN_OSC_P
;
1045 case 'R': /* Linux palette reset */
1048 termstate
= TOPLEVEL
;
1050 case 'W': /* word-set */
1051 termstate
= SEEN_OSC_W
;
1054 case '0': case '1': case '2': case '3': case '4':
1055 case '5': case '6': case '7': case '8': case '9':
1056 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1060 * Grotty hack to support xterm and DECterm title
1061 * sequences concurrently.
1063 if (esc_args
[0] == 2) {
1067 /* else fall through */
1069 termstate
= OSC_STRING
;
1074 if (c
== 0234 || c
== '\007') {
1076 * These characters terminate the string; ST and BEL
1077 * terminate the sequence and trigger instant
1078 * processing of it, whereas ESC goes back to SEEN_ESC
1079 * mode unless it is followed by \, in which case it is
1080 * synonymous with ST in the first place.
1083 termstate
= TOPLEVEL
;
1084 } else if (c
== '\033')
1085 termstate
= OSC_MAYBE_ST
;
1086 else if (osc_strlen
< OSC_STR_MAX
)
1087 osc_string
[osc_strlen
++] = c
;
1091 int max
= (osc_strlen
== 0 ?
21 : 16);
1093 if (c
>= '0' && c
<= '9')
1095 else if (c
>= 'A' && c
<= 'A'+max
-10)
1097 else if (c
>= 'a' && c
<= 'a'+max
-10)
1100 termstate
= TOPLEVEL
;
1101 osc_string
[osc_strlen
++] = val
;
1102 if (osc_strlen
>= 7) {
1103 palette_set (osc_string
[0],
1104 osc_string
[1] * 16 + osc_string
[2],
1105 osc_string
[3] * 16 + osc_string
[4],
1106 osc_string
[5] * 16 + osc_string
[6]);
1108 termstate
= TOPLEVEL
;
1114 case '0': case '1': case '2': case '3': case '4':
1115 case '5': case '6': case '7': case '8': case '9':
1116 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1119 termstate
= OSC_STRING
;
1125 unsigned long *p
= scrtop
;
1126 int n
= rows
* (cols
+1);
1128 *p
++ = ATTR_DEFAULT
| 'E';
1130 seen_disp_event
= TRUE
;
1131 check_selection (scrtop
, scrtop
+ rows
* (cols
+1));
1133 termstate
= TOPLEVEL
;
1136 check_selection (cpos
, cpos
+1);
1139 if (nl_count
> MAXNL
)
1144 * Compare two lines to determine whether they are sufficiently
1145 * alike to scroll-optimise one to the other. Return the degree of
1148 static int linecmp (unsigned long *a
, unsigned long *b
) {
1151 for (i
=n
=0; i
< cols
; i
++)
1152 n
+= (*a
++ == *b
++);
1157 * Given a context, update the window. Out of paranoia, we don't
1158 * allow WM_PAINT responses to do scrolling optimisations.
1160 static void do_paint (Context ctx
, int may_optimise
){
1161 int i
, j
, start
, our_curs_y
;
1162 unsigned long attr
, rv
, cursor
;
1165 cursor
= (has_focus ? ATTR_ACTCURS
: ATTR_PASCURS
);
1166 rv
= (rvideo ? ATTR_REVERSE
: 0);
1167 our_curs_y
= curs_y
+ (scrtop
- disptop
) / (cols
+1);
1169 for (i
=0; i
<rows
; i
++) {
1170 int idx
= i
*(cols
+1);
1171 for (j
=0; j
<=cols
; j
++,idx
++) {
1172 unsigned long *d
= disptop
+idx
;
1173 wanttext
[idx
] = ((*d
^ rv
1174 ^ (selstart
<= d
&& d
< selend ?
1175 ATTR_REVERSE
: 0)) |
1176 (i
==our_curs_y
&& j
==curs_x ? cursor
: 0));
1181 * We would perform scrolling optimisations in here, if they
1182 * didn't have a nasty tendency to cause the whole sodding
1183 * program to hang for a second at speed-critical moments.
1184 * We'll leave it well alone...
1187 for (i
=0; i
<rows
; i
++) {
1188 int idx
= i
*(cols
+1);
1190 for (j
=0; j
<=cols
; j
++,idx
++) {
1191 unsigned long t
= wanttext
[idx
];
1192 int needs_update
= (j
< cols
&& t
!= disptext
[idx
]);
1193 int keep_going
= (start
!= -1 && needs_update
&&
1194 (t
& ATTR_MASK
) == attr
&&
1195 j
-start
< sizeof(ch
));
1196 if (start
!= -1 && !keep_going
) {
1197 do_text (ctx
, start
, i
, ch
, j
-start
, attr
);
1203 attr
= t
& ATTR_MASK
;
1205 ch
[j
-start
] = (char) (t
& CHAR_MASK
);
1213 * Invalidate the whole screen so it will be repainted in full.
1215 void term_invalidate(void) {
1218 for (i
=0; i
<rows
*(cols
+1); i
++)
1219 disptext
[i
] = ATTR_INVALID
;
1223 * Paint the window in response to a WM_PAINT message.
1225 void term_paint (Context ctx
, int l
, int t
, int r
, int b
) {
1226 int i
, j
, left
, top
, right
, bottom
;
1228 left
= l
/ font_width
;
1229 right
= (r
- 1) / font_width
;
1230 top
= t
/ font_height
;
1231 bottom
= (b
- 1) / font_height
;
1232 for (i
= top
; i
<= bottom
&& i
< rows
; i
++)
1233 for (j
= left
; j
<= right
&& j
< cols
; j
++)
1234 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
1236 do_paint (ctx
, FALSE
);
1240 * Attempt to scroll the scrollback. The second parameter gives the
1241 * position we want to scroll to; the first is +1 to denote that
1242 * this position is relative to the beginning of the scrollback, -1
1243 * to denote it is relative to the end, and 0 to denote that it is
1244 * relative to the current position.
1246 void term_scroll (int rel
, int where
) {
1247 int n
= where
* (cols
+1);
1249 disptop
= (rel
< 0 ? scrtop
:
1250 rel
> 0 ? sbtop
: disptop
) + n
;
1251 if (disptop
< sbtop
)
1253 if (disptop
> scrtop
)
1260 * Spread the selection outwards according to the selection mode.
1262 static unsigned long *sel_spread_half (unsigned long *p
, int dir
) {
1263 unsigned long *linestart
, *lineend
;
1267 x
= (p
- text
) % (cols
+1);
1269 lineend
= linestart
+ cols
;
1274 * In this mode, every character is a separate unit, except
1275 * for runs of spaces at the end of a non-wrapping line.
1277 if (!(linestart
[cols
] & ATTR_WRAPPED
)) {
1278 unsigned long *q
= lineend
;
1279 while (q
> linestart
&& (q
[-1] & CHAR_MASK
) == 0x20)
1284 p
= (dir
== -1 ? q
: lineend
- 1);
1289 * In this mode, the units are maximal runs of characters
1290 * whose `wordness' has the same value.
1292 wvalue
= wordness
[*p
& CHAR_MASK
];
1294 while (p
< lineend
&& wordness
[p
[1] & CHAR_MASK
] == wvalue
)
1297 while (p
> linestart
&& wordness
[p
[-1] & CHAR_MASK
] == wvalue
)
1303 * In this mode, every line is a unit.
1305 p
= (dir
== -1 ? linestart
: lineend
- 1);
1311 static void sel_spread (void) {
1312 selstart
= sel_spread_half (selstart
, -1);
1313 selend
= sel_spread_half (selend
- 1, +1) + 1;
1316 void term_mouse (Mouse_Button b
, Mouse_Action a
, int x
, int y
) {
1317 unsigned long *selpoint
;
1320 if (y
>=rows
) y
= rows
-1;
1328 if (x
>=cols
) x
= cols
-1;
1330 selpoint
= disptop
+ y
* (cols
+1) + x
;
1332 if (b
== MB_SELECT
&& a
== MA_CLICK
) {
1334 selstate
= ABOUT_TO
;
1335 selanchor
= selpoint
;
1337 } else if (b
== MB_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
1339 selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
1340 selstate
= DRAGGING
;
1341 selstart
= selanchor
= selpoint
;
1342 selend
= selstart
+ 1;
1344 } else if ((b
== MB_SELECT
&& a
== MA_DRAG
) ||
1345 (b
== MB_EXTEND
&& a
!= MA_RELEASE
)) {
1346 if (selstate
== ABOUT_TO
&& selanchor
== selpoint
)
1348 if (b
== MB_EXTEND
&& a
!= MA_DRAG
&& selstate
== SELECTED
) {
1349 if (selpoint
-selstart
< (selend
-selstart
)/2)
1350 selanchor
= selend
- 1;
1352 selanchor
= selstart
;
1353 selstate
= DRAGGING
;
1355 if (selstate
!= ABOUT_TO
&& selstate
!= DRAGGING
)
1356 selanchor
= selpoint
;
1357 selstate
= DRAGGING
;
1358 if (selpoint
< selanchor
) {
1359 selstart
= selpoint
;
1360 selend
= selanchor
+ 1;
1362 selstart
= selanchor
;
1363 selend
= selpoint
+ 1;
1366 } else if ((b
== MB_SELECT
|| b
== MB_EXTEND
) && a
== MA_RELEASE
) {
1367 if (selstate
== DRAGGING
) {
1369 * We've completed a selection. We now transfer the
1370 * data to the clipboard.
1372 unsigned char *p
= selspace
;
1373 unsigned long *q
= selstart
;
1375 while (q
< selend
) {
1377 unsigned long *lineend
= q
- (q
-text
) % (cols
+1) + cols
;
1378 unsigned long *nlpos
= lineend
;
1380 if (!(*nlpos
& ATTR_WRAPPED
)) {
1381 while ((nlpos
[-1] & CHAR_MASK
) == 0x20 && nlpos
> q
)
1386 while (q
< nlpos
&& q
< selend
)
1387 *p
++ = (unsigned char) (*q
++ & CHAR_MASK
);
1390 for (i
=0; i
<sizeof(sel_nl
); i
++)
1393 q
= lineend
+ 1; /* start of next line */
1395 write_clip (selspace
, p
- selspace
);
1396 selstate
= SELECTED
;
1398 selstate
= NO_SELECTION
;
1399 } else if (b
== MB_PASTE
&& (a
==MA_CLICK
|| a
==MA_2CLK
|| a
==MA_3CLK
)) {
1403 get_clip((void **) &data
, &len
);
1407 while (p
< data
+len
) {
1408 while (p
< data
+len
&&
1409 !(p
<= data
+len
-sizeof(sel_nl
) &&
1410 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
1418 c
=xlat_kbd2tty(q
[i
]);
1423 if (p
<= data
+len
-sizeof(sel_nl
) &&
1424 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
1425 ldisc
->send ("\r", 1);
1426 p
+= sizeof(sel_nl
);
1431 get_clip(NULL
, NULL
);
1437 static void deselect (void) {
1438 selstate
= NO_SELECTION
;
1439 selstart
= selend
= scrtop
;
1442 void term_deselect (void) {