8 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
9 #define CL_VT100 0x0002 /* VT100 */
10 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
11 #define CL_VT102 0x0008 /* VT102 */
12 #define CL_VT220 0x0010 /* VT220 */
13 #define CL_VT320 0x0020 /* VT320 */
14 #define CL_VT420 0x0040 /* VT420 */
15 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
16 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
17 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
18 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
19 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
21 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
22 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
23 #define TM_VT102 (TM_VT100AVO|CL_VT102)
24 #define TM_VT220 (TM_VT102|CL_VT220)
25 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
26 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
28 #define TM_PUTTY (0xFFFF)
30 #define compatibility(x) \
31 if ( ((CL_##x)&compatibility_level) == 0 ) { \
35 #define compatibility2(x,y) \
36 if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
41 #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
43 static int compatibility_level
= TM_PUTTY
;
46 static unsigned long *text
; /* buffer of text on terminal screen */
47 static unsigned long *scrtop
; /* top of working screen */
48 static unsigned long *disptop
; /* top of displayed screen */
49 static unsigned long *sbtop
; /* top of scrollback */
50 static unsigned long *sbbot
; /* furthest extent of scrollback */
51 static unsigned long *cpos
; /* cursor position (convenience) */
52 static unsigned long *disptext
; /* buffer of text on real screen */
53 static unsigned long *wanttext
; /* buffer of text we want on screen */
54 static unsigned long *alttext
; /* buffer of text on alt. screen */
56 static unsigned char *selspace
; /* buffer for building selections in */
58 #define TSIZE (sizeof(*text))
59 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
61 static unsigned long curr_attr
, save_attr
;
62 static unsigned long erase_char
= ERASE_CHAR
;
64 static int curs_x
, curs_y
; /* cursor */
65 static int save_x
, save_y
; /* saved cursor position */
66 static int marg_t
, marg_b
; /* scroll margins */
67 static int dec_om
; /* DEC origin mode flag */
68 static int wrap
, wrapnext
; /* wrap flags */
69 static int insert
; /* insert-mode flag */
70 static int cset
; /* 0 or 1: which char set */
71 static int save_cset
, save_csattr
; /* saved with cursor position */
72 static int rvideo
; /* global reverse video flag */
73 static int cursor_on
; /* cursor enabled flag */
74 static int reset_132
; /* Flag ESC c resets to 80 cols */
75 static int use_bce
; /* Use Background coloured erase */
76 static int blinker
; /* When blinking is the cursor on ? */
77 static int tblinker
; /* When the blinking text is on */
78 static int blink_is_real
; /* Actually blink blinking text */
80 static unsigned long cset_attr
[2];
83 * Saved settings on the alternate screen.
85 static int alt_x
, alt_y
, alt_om
, alt_wrap
, alt_wnext
, alt_ins
, alt_cset
;
86 static int alt_t
, alt_b
;
89 #define ARGS_MAX 32 /* max # of esc sequence arguments */
90 #define ARG_DEFAULT 0 /* if an arg isn't specified */
91 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
92 static int esc_args
[ARGS_MAX
];
95 #define ANSI(x,y) ((x)+((y)<<8))
96 #define ANSI_QUE(x) ANSI(x,TRUE)
98 #define OSC_STR_MAX 2048
99 static int osc_strlen
;
100 static char osc_string
[OSC_STR_MAX
+1];
103 static char id_string
[1024] = "\033[?6c";
105 static unsigned char *tabs
;
119 OSC_STRING
, OSC_MAYBE_ST
,
127 NO_SELECTION
, ABOUT_TO
, DRAGGING
, SELECTED
130 SM_CHAR
, SM_WORD
, SM_LINE
132 static unsigned long *selstart
, *selend
, *selanchor
;
134 static short wordness
[256] = {
135 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 */
136 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 */
137 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 */
138 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 */
139 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 */
140 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 */
141 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 */
142 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 */
145 static unsigned char sel_nl
[] = SEL_NL
;
146 static char * paste_buffer
= 0;
147 static int paste_len
, paste_pos
, paste_hold
;
150 * Internal prototypes.
152 static void do_paint (Context
, int);
153 static void erase_lots (int, int, int);
154 static void swap_screen (int);
155 static void update_sbar (void);
156 static void deselect (void);
159 * Set up power-on settings for the terminal.
161 static void power_on(void) {
162 curs_x
= curs_y
= alt_x
= alt_y
= save_x
= save_y
= 0;
165 alt_b
= marg_b
= rows
- 1;
170 for (i
= 0; i
< cols
; i
++)
171 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
173 alt_om
= dec_om
= cfg
.dec_om
;
174 alt_wnext
= wrapnext
= alt_ins
= insert
= FALSE
;
175 alt_wrap
= wrap
= cfg
.wrap_mode
;
177 cset_attr
[0] = cset_attr
[1] = ATTR_ASCII
;
180 save_attr
= curr_attr
= ATTR_DEFAULT
;
181 app_cursor_keys
= cfg
.app_cursor
;
182 app_keypad_keys
= cfg
.app_keypad
;
184 blink_is_real
= cfg
.blinktext
;
185 erase_char
= ERASE_CHAR
;
189 for (i
= 0; i
< 256; i
++)
190 wordness
[i
] = cfg
.wordness
[i
];
194 erase_lots (FALSE
, TRUE
, TRUE
);
196 erase_lots (FALSE
, TRUE
, TRUE
);
201 * Force a screen update.
203 void term_update(void) {
207 if ( (seen_key_event
&& (cfg
.scroll_on_key
)) ||
208 (seen_disp_event
&& (cfg
.scroll_on_disp
)) ) {
210 seen_disp_event
= seen_key_event
= 0;
213 do_paint (ctx
, TRUE
);
214 sys_cursor(curs_x
, curs_y
+ (scrtop
- disptop
) / (cols
+1));
220 * Same as power_on(), but an external function.
222 void term_pwron(void) {
231 * Clear the scrollback.
233 void term_clrsb(void) {
234 disptop
= sbtop
= scrtop
;
239 * Initialise the terminal.
241 void term_init(void) {
242 text
= sbtop
= sbbot
= scrtop
= disptop
= cpos
= NULL
;
243 disptext
= wanttext
= NULL
;
252 * Set up the terminal for a given size.
254 void term_size(int newrows
, int newcols
, int newsavelines
) {
255 unsigned long *newtext
, *newdisp
, *newwant
, *newalt
;
256 int i
, j
, crows
, ccols
;
258 int save_alt_which
= alt_which
;
260 if (newrows
== rows
&& newcols
== cols
&& newsavelines
== savelines
)
261 return; /* nothing to do */
267 alt_b
= marg_b
= newrows
- 1;
269 newtext
= smalloc ((newrows
+newsavelines
)*(newcols
+1)*TSIZE
);
270 sbbot
= newtext
+ newsavelines
*(newcols
+1);
271 for (i
=0; i
<(newrows
+newsavelines
)*(newcols
+1); i
++)
272 newtext
[i
] = erase_char
;
274 crows
= rows
+ (scrtop
- sbtop
) / (cols
+1);
275 if (crows
> newrows
+newsavelines
)
276 crows
= newrows
+newsavelines
;
280 disptop
= newtext
+ (crows
-newrows
)*(newcols
+1);
281 ccols
= (cols
< newcols ? cols
: newcols
);
282 for (i
=0; i
<crows
; i
++) {
283 int oldidx
= (rows
- crows
+ i
) * (cols
+1);
284 int newidx
= (newrows
- crows
+ i
) * (newcols
+1);
286 for (j
=0; j
<ccols
; j
++)
287 disptop
[newidx
+j
] = scrtop
[oldidx
+j
];
288 disptop
[newidx
+newcols
] =
289 (cols
== newcols ? scrtop
[oldidx
+cols
]
290 : (scrtop
[oldidx
+cols
]&LATTR_MODE
));
294 sbtop
= disptop
= newtext
;
300 newdisp
= smalloc (newrows
*(newcols
+1)*TSIZE
);
301 for (i
=0; i
<newrows
*(newcols
+1); i
++)
302 newdisp
[i
] = ATTR_INVALID
;
306 newwant
= smalloc (newrows
*(newcols
+1)*TSIZE
);
307 for (i
=0; i
<newrows
*(newcols
+1); i
++)
308 newwant
[i
] = ATTR_INVALID
;
312 newalt
= smalloc (newrows
*(newcols
+1)*TSIZE
);
313 for (i
=0; i
<newrows
*(newcols
+1); i
++)
314 newalt
[i
] = erase_char
;
319 selspace
= smalloc ( (newrows
+newsavelines
) * (newcols
+sizeof(sel_nl
)) );
321 tabs
= srealloc (tabs
, newcols
*sizeof(*tabs
));
324 for (i
= (cols
> 0 ? cols
: 0); i
< newcols
; i
++)
325 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
329 curs_y
+= newrows
- rows
;
332 if (curs_y
>= newrows
)
334 if (curs_x
>= newcols
)
337 wrapnext
= alt_wnext
= FALSE
;
341 savelines
= newsavelines
;
344 swap_screen(save_alt_which
);
353 static void swap_screen (int which
) {
357 if (which
== alt_which
)
362 for (t
=0; t
<rows
*(cols
+1); t
++) {
363 tt
= scrtop
[t
]; scrtop
[t
] = alttext
[t
]; alttext
[t
] = tt
;
366 t
= curs_x
; curs_x
= alt_x
; alt_x
= t
;
367 t
= curs_y
; curs_y
= alt_y
; alt_y
= t
;
368 t
= marg_t
; marg_t
= alt_t
; alt_t
= t
;
369 t
= marg_b
; marg_b
= alt_b
; alt_b
= t
;
370 t
= dec_om
; dec_om
= alt_om
; alt_om
= t
;
371 t
= wrap
; wrap
= alt_wrap
; alt_wrap
= t
;
372 t
= wrapnext
; wrapnext
= alt_wnext
; alt_wnext
= t
;
373 t
= insert
; insert
= alt_ins
; alt_ins
= t
;
374 t
= cset
; cset
= alt_cset
; alt_cset
= t
;
380 * Update the scroll bar.
382 static void update_sbar(void) {
385 min
= (sbtop
- text
) / (cols
+1);
386 set_sbar ((scrtop
- text
) / (cols
+1) + rows
- min
,
387 (disptop
- text
) / (cols
+1) - min
,
392 * Check whether the region bounded by the two pointers intersects
393 * the scroll region, and de-select the on-screen selection if so.
395 static void check_selection (unsigned long *from
, unsigned long *to
) {
396 if (from
< selend
&& selstart
< to
)
401 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
402 * for backward.) `sb' is TRUE if the scrolling is permitted to
403 * affect the scrollback buffer.
405 static void scroll (int topline
, int botline
, int lines
, int sb
) {
406 unsigned long *scroll_top
;
407 unsigned long *newscr
;
408 int scroll_size
, size
, i
;
409 int scrtop_is_disptop
= (scrtop
==disptop
);
410 static int recursive
= 0;
412 /* Only scroll more than the window if we're doing a 10% scroll */
413 if (!recursive
&& lines
> botline
- topline
+ 1)
414 lines
= botline
- topline
+ 1;
416 scroll_top
= scrtop
+ topline
*(cols
+1);
417 size
= (lines
< 0 ?
-lines
: lines
) * (cols
+1);
418 scroll_size
= (botline
- topline
+ 1) * (cols
+1) - size
;
420 if (lines
> 0 && topline
== 0 && alt_which
== 0 && sb
) {
422 * Since we're going to scroll the top line and we're on the
423 * scrolling screen let's also effect the scrollback buffer.
425 * This is normally done by moving the position the screen
426 * painter reads from to reduce the amount of memory copying
429 if (scroll_size
>= 0 && !recursive
) {
430 newscr
= scrtop
+ lines
* (cols
+1);
432 if (newscr
> sbbot
&& botline
== rows
-1) {
433 /* We've hit the bottom of memory, so we have to do a
434 * physical scroll. But instead of just 1 line do it
435 * by 10% of the available memory.
437 * If the scroll region isn't the whole screen then we can't
438 * do this as it stands. We would need to recover the bottom
439 * of the screen from the scroll buffer after being sure that
440 * it doesn't get deleted.
443 i
= (rows
+savelines
)/10;
445 /* Make it simple and ensure safe recursion */
446 if ( i
<savelines
-1) {
448 scroll(topline
, botline
, i
, sb
);
451 newscr
= scrtop
- i
* (cols
+1);
452 if (scrtop_is_disptop
) disptop
= newscr
;
456 newscr
= scrtop
+ lines
* (cols
+1);
459 if (newscr
<= sbbot
) {
460 if (scrtop_is_disptop
) disptop
= newscr
;
463 if (botline
== rows
-1 )
464 for (i
= 0; i
< size
; i
++)
465 scrtop
[i
+scroll_size
] = erase_char
;
470 if (botline
!= rows
-1) {
471 /* This fastscroll only works for full window scrolls.
472 * If the caller wanted a partial one we have to reverse
473 * scroll the bottom of the screen.
475 scroll(botline
-lines
+1, rows
-1, -lines
, 0);
480 /* If we can't scroll by memory remapping do it physically.
481 * But rather than expensivly doing the scroll buffer just
482 * scroll the screen. All it means is that sometimes we choose
483 * to not add lines from a scroll region to the scroll buffer.
486 if (savelines
<= 400) {
487 sbtop
-= lines
* (cols
+1);
490 scroll_size
+= scroll_top
- sbtop
;
496 /* Ho hum, expensive scroll required. */
498 sbtop
-= lines
* (cols
+1);
501 scroll_size
+= scroll_top
- sbtop
;
508 if (scroll_size
< 0) {
515 memmove (scroll_top
, scroll_top
+ size
, scroll_size
*TSIZE
);
516 for (i
= 0; i
< size
; i
++)
517 scroll_top
[i
+scroll_size
] = erase_char
;
518 if (selstart
> scroll_top
&&
519 selstart
< scroll_top
+ size
+ scroll_size
) {
521 if (selstart
< scroll_top
)
522 selstart
= scroll_top
;
524 if (selend
> scroll_top
&&
525 selend
< scroll_top
+ size
+ scroll_size
) {
527 if (selend
< scroll_top
)
530 if (scrtop_is_disptop
)
533 if (disptop
> scroll_top
&&
534 disptop
< scroll_top
+ size
+ scroll_size
) {
536 if (disptop
< scroll_top
)
537 disptop
= scroll_top
;
541 memmove (scroll_top
+ size
, scroll_top
, scroll_size
*TSIZE
);
542 for (i
= 0; i
< size
; i
++)
543 scroll_top
[i
] = erase_char
;
544 if (selstart
> scroll_top
&&
545 selstart
< scroll_top
+ size
+ scroll_size
) {
547 if (selstart
> scroll_top
+ size
+ scroll_size
)
548 selstart
= scroll_top
+ size
+ scroll_size
;
550 if (selend
> scroll_top
&&
551 selend
< scroll_top
+ size
+ scroll_size
) {
553 if (selend
> scroll_top
+ size
+ scroll_size
)
554 selend
= scroll_top
+ size
+ scroll_size
;
556 if (scrtop_is_disptop
)
558 else if (disptop
> scroll_top
&&
559 disptop
< scroll_top
+ size
+ scroll_size
) {
561 if (disptop
> scroll_top
+ size
+ scroll_size
)
562 disptop
= scroll_top
+ size
+ scroll_size
;
568 * Move the cursor to a given position, clipping at boundaries. We
569 * may or may not want to clip at the scroll margin: marg_clip is 0
570 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
571 * even _being_ outside the margins.
573 static void move (int x
, int y
, int marg_clip
) {
579 if ((curs_y
>= marg_t
|| marg_clip
== 2) && y
< marg_t
)
581 if ((curs_y
<= marg_b
|| marg_clip
== 2) && y
> marg_b
)
595 * Save or restore the cursor and SGR mode.
597 static void save_cursor(int save
) {
601 save_attr
= curr_attr
;
603 save_csattr
= cset_attr
[cset
];
607 /* Make sure the window hasn't shrunk since the save */
608 if (curs_x
>= cols
) curs_x
= cols
-1;
609 if (curs_y
>= rows
) curs_y
= rows
-1;
611 curr_attr
= save_attr
;
613 cset_attr
[cset
] = save_csattr
;
615 if (use_bce
) erase_char
= (' ' |(curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
)));
620 * Erase a large portion of the screen: the whole screen, or the
621 * whole line, or parts thereof.
623 static void erase_lots (int line_only
, int from_begin
, int to_end
) {
624 unsigned long *startpos
, *endpos
;
627 startpos
= cpos
- curs_x
;
628 endpos
= startpos
+ cols
;
629 /* I've removed the +1 so that the Wide screen stuff is not
630 * removed when it shouldn't be.
634 endpos
= startpos
+ rows
* (cols
+1);
640 check_selection (startpos
, endpos
);
642 /* Clear screen also forces a full window redraw, just in case. */
643 if (startpos
== scrtop
&& endpos
== scrtop
+ rows
* (cols
+1))
646 while (startpos
< endpos
)
647 *startpos
++ = erase_char
;
651 * Insert or delete characters within the current line. n is +ve if
652 * insertion is desired, and -ve for deletion.
654 static void insch (int n
) {
655 int dir
= (n
< 0 ?
-1 : +1);
658 n
= (n
< 0 ?
-n
: n
);
659 if (n
> cols
- curs_x
)
661 m
= cols
- curs_x
- n
;
662 check_selection (cpos
, cpos
+n
);
664 memmove (cpos
, cpos
+n
, m
*TSIZE
);
666 cpos
[m
++] = erase_char
;
668 memmove (cpos
+n
, cpos
, m
*TSIZE
);
670 cpos
[n
] = erase_char
;
675 * Toggle terminal mode `mode' to state `state'. (`query' indicates
676 * whether the mode is a DEC private one or a normal one.)
678 static void toggle_mode (int mode
, int query
, int state
) {
679 if (query
) switch (mode
) {
680 case 1: /* application cursor keys */
681 app_cursor_keys
= state
;
683 case 2: /* VT52 mode */
686 case 3: /* 80/132 columns */
688 request_resize (state ?
132 : 80, rows
, 1);
691 case 5: /* reverse video */
693 seen_disp_event
= TRUE
;
694 if (state
) term_update();
696 case 6: /* DEC origin mode */
699 case 7: /* auto wrap */
702 case 8: /* auto key repeat */
705 case 25: /* enable/disable cursor */
706 compatibility2(OTHER
,VT220
);
708 seen_disp_event
= TRUE
;
710 case 47: /* alternate screen */
711 compatibility(OTHER
);
716 } else switch (mode
) {
717 case 4: /* set insert mode */
718 compatibility(VT102
);
721 case 12: /* set echo mode */
723 * This may be very good in smcup and rmcup (or smkx & rmkx) if you
724 * have a long RTT and the telnet client/daemon doesn't understand
727 * DONT send TS_RECHO/TS_LECHO; the telnet daemon tries to fix the
728 * tty and _really_ confuses some programs.
730 compatibility2(OTHER
,VT220
);
731 ldisc
= (state?
&ldisc_simple
: &ldisc_term
);
733 case 20: /* Return sends ... */
734 cr_lf_return
= state
;
740 * Process an OSC sequence: set window title or icon name.
742 static void do_osc(void) {
745 wordness
[(unsigned char)osc_string
[osc_strlen
]] = esc_args
[0];
747 osc_string
[osc_strlen
] = '\0';
748 switch (esc_args
[0]) {
751 set_icon (osc_string
);
752 if (esc_args
[0] == 1)
754 /* fall through: parameter 0 means set both */
757 set_title (osc_string
);
764 * Remove everything currently in `inbuf' and stick it up on the
765 * in-memory display. There's a big state machine in here to
766 * process escape sequences...
768 void term_out(void) {
771 static int beep_overload
= 0;
774 for(inbuf_reap
= 0; inbuf_reap
< inbuf_head
; inbuf_reap
++)
776 c
= inbuf
[inbuf_reap
];
779 * Optionally log the session traffic to a file. Useful for
780 * debugging and possibly also useful for actual logging.
783 static FILE *fp
= NULL
;
784 if (!fp
) fp
= fopen(logfile
, "wb");
785 if (fp
) fputc (c
, fp
);
787 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
788 * be able to display 8-bit characters, but I'll let that go 'cause
791 if( ( (c
&0x60) == 0 || c
== '\177') &&
792 termstate
< DO_CTRLS
&&
793 ( (c
&0x80) == 0 || has_compat(VT220
))) {
795 case '\005': /* terminal type query */
796 /* Strictly speaking this is VT100 but a VT100 defaults to
797 * no response. Other terminals respond at their option.
799 * Don't put a CR in the default string as this tends to
800 * upset some weird software.
802 * An xterm returns "xterm" (5 characters)
804 compatibility(OTHER
);
805 ldisc
->send ("PuTTY", 5);
809 if(beep_count
>6) beep_overload
=1;
813 if (curs_x
== 0 && curs_y
== 0)
815 else if (curs_x
== 0 && curs_y
> 0)
816 curs_x
= cols
-1, curs_y
--;
822 seen_disp_event
= TRUE
;
825 compatibility(VT100
);
829 compatibility(VT100
);
834 termstate
= VT52_ESC
;
836 compatibility(ANSIMIN
);
837 termstate
= SEEN_ESC
;
841 compatibility(VT220
);
842 termstate
= SEEN_CSI
;
844 esc_args
[0] = ARG_DEFAULT
;
848 compatibility(VT220
);
849 termstate
= SEEN_OSC
;
856 seen_disp_event
= TRUE
;
861 compatibility(VT100
);
863 if (curs_y
== marg_b
)
864 scroll (marg_t
, marg_b
, 1, TRUE
);
865 else if (curs_y
< rows
-1)
876 unsigned long *old_cpos
= cpos
;
877 unsigned long *p
= scrtop
+ curs_y
* (cols
+1) + cols
;
881 } while (curs_x
< cols
-1 && !tabs
[curs_x
]);
883 if ((*p
& LATTR_MODE
) != LATTR_NORM
)
885 if (curs_x
>= cols
/2)
895 check_selection (old_cpos
, cpos
);
897 seen_disp_event
= TRUE
;
899 case '\177': /* Destructive backspace
900 This does nothing on a real VT100 */
901 compatibility(OTHER
);
902 if (curs_x
&& !wrapnext
) curs_x
--;
905 *cpos
= (' ' | curr_attr
| ATTR_ASCII
);
909 else switch (termstate
) {
911 /* Only graphic characters get this far, ctrls are stripped above */
913 cpos
[1] |= ATTR_WRAPPED
;
914 if (curs_y
== marg_b
)
915 scroll (marg_t
, marg_b
, 1, TRUE
);
916 else if (curs_y
< rows
-1)
924 if (selstate
!= NO_SELECTION
)
925 check_selection (cpos
, cpos
+1);
926 switch (cset_attr
[cset
]) {
927 /* Linedraw characters are different from 'ESC ( B' only
928 * for a small range, for ones outside that range make sure
929 * we use the same font as well as the same encoding.
932 if (c
<0x5f || c
>0x7F)
933 *cpos
++ = xlat_tty2scr((unsigned char)c
) | curr_attr
|
936 *cpos
++ = ' ' | curr_attr
| ATTR_ASCII
;
938 *cpos
++ = ((unsigned char)c
) | curr_attr
| ATTR_LINEDRW
;
941 /* If UK-ASCII, make the '#' a LineDraw Pound */
943 *cpos
++ = '}' | curr_attr
| ATTR_LINEDRW
;
948 *cpos
++ = xlat_tty2scr((unsigned char)c
) | curr_attr
|
949 (c
<= 0x7F ? cset_attr
[cset
] : ATTR_ASCII
);
953 if (curs_x
== cols
) {
962 termstate
= TOPLEVEL
;
966 * This state is virtually identical to SEEN_ESC, with the
967 * exception that we have an OSC sequence in the pipeline,
968 * and _if_ we see a backslash, we process it.
972 termstate
= TOPLEVEL
;
975 /* else fall through */
977 termstate
= TOPLEVEL
;
979 case ' ': /* some weird sequence? */
980 compatibility(VT220
);
981 termstate
= IGNORE_NEXT
;
983 case '[': /* enter CSI mode */
984 termstate
= SEEN_CSI
;
986 esc_args
[0] = ARG_DEFAULT
;
989 case ']': /* xterm escape sequences */
990 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
991 compatibility(OTHER
);
992 termstate
= SEEN_OSC
;
995 case '(': /* should set GL */
996 compatibility(VT100
);
999 case ')': /* should set GR */
1000 compatibility(VT100
);
1003 case '7': /* save cursor */
1004 compatibility(VT100
);
1007 case '8': /* restore cursor */
1008 compatibility(VT100
);
1009 save_cursor (FALSE
);
1010 seen_disp_event
= TRUE
;
1013 compatibility(VT100
);
1014 app_keypad_keys
= TRUE
;
1017 compatibility(VT100
);
1018 app_keypad_keys
= FALSE
;
1020 case 'D': /* exactly equivalent to LF */
1021 compatibility(VT100
);
1022 if (curs_y
== marg_b
)
1023 scroll (marg_t
, marg_b
, 1, TRUE
);
1024 else if (curs_y
< rows
-1)
1028 seen_disp_event
= TRUE
;
1030 case 'E': /* exactly equivalent to CR-LF */
1031 compatibility(VT100
);
1033 if (curs_y
== marg_b
)
1034 scroll (marg_t
, marg_b
, 1, TRUE
);
1035 else if (curs_y
< rows
-1)
1039 seen_disp_event
= TRUE
;
1041 case 'M': /* reverse index - backwards LF */
1042 compatibility(VT100
);
1043 if (curs_y
== marg_t
)
1044 scroll (marg_t
, marg_b
, -1, TRUE
);
1045 else if (curs_y
> 0)
1049 seen_disp_event
= TRUE
;
1051 case 'Z': /* terminal type query */
1052 compatibility(VT100
);
1053 ldisc
->send (id_string
, strlen(id_string
));
1055 case 'c': /* restore power-on settings */
1056 compatibility(VT100
);
1059 request_resize (80, rows
, 1);
1064 seen_disp_event
= TRUE
;
1066 case '#': /* ESC # 8 fills screen with Es :-) */
1067 compatibility(VT100
);
1068 termstate
= SEEN_ESCHASH
;
1070 case 'H': /* set a tab */
1071 compatibility(VT100
);
1072 tabs
[curs_x
] = TRUE
;
1077 termstate
= TOPLEVEL
; /* default */
1080 if (esc_nargs
<= ARGS_MAX
) {
1081 if (esc_args
[esc_nargs
-1] == ARG_DEFAULT
)
1082 esc_args
[esc_nargs
-1] = 0;
1083 esc_args
[esc_nargs
-1] =
1084 10 * esc_args
[esc_nargs
-1] + c
- '0';
1086 termstate
= SEEN_CSI
;
1090 if (++esc_nargs
<= ARGS_MAX
)
1091 esc_args
[esc_nargs
-1] = ARG_DEFAULT
;
1092 termstate
= SEEN_CSI
;
1096 if( esc_query
) esc_query
= -1;
1097 else if( c
== '?' ) esc_query
= TRUE
;
1099 termstate
= SEEN_CSI
;
1101 else switch (ANSI(c
,esc_query
)) {
1102 case 'A': /* move up N lines */
1103 move (curs_x
, curs_y
- def(esc_args
[0], 1), 1);
1104 seen_disp_event
= TRUE
;
1106 case 'e': /* move down N lines */
1107 compatibility(ANSI
);
1109 move (curs_x
, curs_y
+ def(esc_args
[0], 1), 1);
1110 seen_disp_event
= TRUE
;
1112 case 'a': /* move right N cols */
1113 compatibility(ANSI
);
1115 move (curs_x
+ def(esc_args
[0], 1), curs_y
, 1);
1116 seen_disp_event
= TRUE
;
1118 case 'D': /* move left N cols */
1119 move (curs_x
- def(esc_args
[0], 1), curs_y
, 1);
1120 seen_disp_event
= TRUE
;
1122 case 'E': /* move down N lines and CR */
1123 compatibility(ANSI
);
1124 move (0, curs_y
+ def(esc_args
[0], 1), 1);
1125 seen_disp_event
= TRUE
;
1127 case 'F': /* move up N lines and CR */
1128 compatibility(ANSI
);
1129 move (0, curs_y
- def(esc_args
[0], 1), 1);
1130 seen_disp_event
= TRUE
;
1132 case 'G': case '`': /* set horizontal posn */
1133 compatibility(ANSI
);
1134 move (def(esc_args
[0], 1) - 1, curs_y
, 0);
1135 seen_disp_event
= TRUE
;
1137 case 'd': /* set vertical posn */
1138 compatibility(ANSI
);
1139 move (curs_x
, (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
1141 seen_disp_event
= TRUE
;
1143 case 'H': case 'f': /* set horz and vert posns at once */
1145 esc_args
[1] = ARG_DEFAULT
;
1146 move (def(esc_args
[1], 1) - 1,
1147 (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
1149 seen_disp_event
= TRUE
;
1151 case 'J': /* erase screen or parts of it */
1153 unsigned int i
= def(esc_args
[0], 0) + 1;
1156 erase_lots(FALSE
, !!(i
& 2), !!(i
& 1));
1159 seen_disp_event
= TRUE
;
1161 case 'K': /* erase line or parts of it */
1163 unsigned int i
= def(esc_args
[0], 0) + 1;
1166 erase_lots(TRUE
, !!(i
& 2), !!(i
& 1));
1168 seen_disp_event
= TRUE
;
1170 case 'L': /* insert lines */
1171 compatibility(VT102
);
1172 if (curs_y
<= marg_b
)
1173 scroll (curs_y
, marg_b
, -def(esc_args
[0], 1), FALSE
);
1174 seen_disp_event
= TRUE
;
1176 case 'M': /* delete lines */
1177 compatibility(VT102
);
1178 if (curs_y
<= marg_b
)
1179 scroll (curs_y
, marg_b
, def(esc_args
[0], 1), TRUE
);
1180 seen_disp_event
= TRUE
;
1182 case '@': /* insert chars */
1183 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1184 compatibility(VT102
);
1185 insch (def(esc_args
[0], 1));
1186 seen_disp_event
= TRUE
;
1188 case 'P': /* delete chars */
1189 compatibility(VT102
);
1190 insch (-def(esc_args
[0], 1));
1191 seen_disp_event
= TRUE
;
1193 case 'c': /* terminal type query */
1194 compatibility(VT100
);
1195 /* This is the response for a VT102 */
1196 ldisc
->send (id_string
, strlen(id_string
));
1198 case 'n': /* cursor position query */
1199 if (esc_args
[0] == 6) {
1201 sprintf (buf
, "\033[%d;%dR", curs_y
+ 1, curs_x
+ 1);
1202 ldisc
->send (buf
, strlen(buf
));
1204 else if (esc_args
[0] == 5) {
1205 ldisc
->send ("\033[0n", 4);
1208 case 'h': /* toggle modes to high */
1210 compatibility(VT100
);
1213 for (i
=0; i
<esc_nargs
; i
++)
1214 toggle_mode (esc_args
[i
], esc_query
, TRUE
);
1217 case 'l': /* toggle modes to low */
1219 compatibility(VT100
);
1222 for (i
=0; i
<esc_nargs
; i
++)
1223 toggle_mode (esc_args
[i
], esc_query
, FALSE
);
1226 case 'g': /* clear tabs */
1227 compatibility(VT100
);
1228 if (esc_nargs
== 1) {
1229 if (esc_args
[0] == 0) {
1230 tabs
[curs_x
] = FALSE
;
1231 } else if (esc_args
[0] == 3) {
1233 for (i
= 0; i
< cols
; i
++)
1238 case 'r': /* set scroll margins */
1239 compatibility(VT100
);
1240 if (esc_nargs
<= 2) {
1242 top
= def(esc_args
[0], 1) - 1;
1243 bot
= (esc_nargs
<= 1 || esc_args
[1] == 0 ? rows
:
1244 def(esc_args
[1], rows
)) - 1;
1247 /* VTTEST Bug 9 - if region is less than 2 lines
1248 * don't change region.
1255 * I used to think the cursor should be
1256 * placed at the top of the newly marginned
1257 * area. Apparently not: VMS TPU falls over
1260 * Well actually it should for Origin mode - RDB
1262 curs_y
= (dec_om ? marg_t
: 0);
1264 seen_disp_event
= TRUE
;
1268 case 'm': /* set graphics rendition */
1271 * A VT100 without the AVO only had one attribute, either
1272 * underline or reverse video depending on the cursor type,
1273 * this was selected by CSI 7m.
1276 * This is DIM on the VT100-AVO and VT102
1278 * This is BLINK on the VT100-AVO and VT102+
1280 * This is INVIS on the VT100-AVO and VT102
1282 * This like 22 disables BOLD, DIM and INVIS
1284 * The ANSI colours appear on any terminal that has colour
1285 * (obviously) but the interaction between sgr0 and the
1286 * colours varies but is usually related to the background
1287 * colour erase item.
1288 * The interaction between colour attributes and the mono
1289 * ones is also very implementation dependent.
1291 * The 39 and 49 attributes are likely to be unimplemented.
1294 for (i
=0; i
<esc_nargs
; i
++) {
1295 switch (def(esc_args
[i
], 0)) {
1296 case 0: /* restore defaults */
1297 curr_attr
= ATTR_DEFAULT
; break;
1298 case 1: /* enable bold */
1299 compatibility(VT100AVO
);
1300 curr_attr
|= ATTR_BOLD
; break;
1301 case 21: /* (enable double underline) */
1302 compatibility(OTHER
);
1303 case 4: /* enable underline */
1304 compatibility(VT100AVO
);
1305 curr_attr
|= ATTR_UNDER
; break;
1306 case 5: /* enable blink */
1307 compatibility(VT100AVO
);
1308 curr_attr
|= ATTR_BLINK
; break;
1309 case 7: /* enable reverse video */
1310 curr_attr
|= ATTR_REVERSE
; break;
1311 case 22: /* disable bold */
1312 compatibility2(OTHER
,VT220
);
1313 curr_attr
&= ~ATTR_BOLD
; break;
1314 case 24: /* disable underline */
1315 compatibility2(OTHER
,VT220
);
1316 curr_attr
&= ~ATTR_UNDER
; break;
1317 case 25: /* disable blink */
1318 compatibility2(OTHER
,VT220
);
1319 curr_attr
&= ~ATTR_BLINK
; break;
1320 case 27: /* disable reverse video */
1321 compatibility2(OTHER
,VT220
);
1322 curr_attr
&= ~ATTR_REVERSE
; break;
1323 case 30: case 31: case 32: case 33:
1324 case 34: case 35: case 36: case 37:
1326 curr_attr
&= ~ATTR_FGMASK
;
1327 curr_attr
|= (esc_args
[i
] - 30) << ATTR_FGSHIFT
;
1329 case 39: /* default-foreground */
1330 curr_attr
&= ~ATTR_FGMASK
;
1331 curr_attr
|= ATTR_DEFFG
;
1333 case 40: case 41: case 42: case 43:
1334 case 44: case 45: case 46: case 47:
1336 curr_attr
&= ~ATTR_BGMASK
;
1337 curr_attr
|= (esc_args
[i
] - 40) << ATTR_BGSHIFT
;
1339 case 49: /* default-background */
1340 curr_attr
&= ~ATTR_BGMASK
;
1341 curr_attr
|= ATTR_DEFBG
;
1348 (curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
|ATTR_BLINK
))
1352 case 's': /* save cursor */
1355 case 'u': /* restore cursor */
1356 save_cursor (FALSE
);
1357 seen_disp_event
= TRUE
;
1359 case 't': /* set page size - ie window height */
1361 * VT340/VT420 sequence DECSLPP, DEC only allows values
1362 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1363 * illegal values (eg first arg 1..9) for window changing
1366 compatibility(VT340TEXT
);
1367 if (esc_nargs
<=1 && (esc_args
[0]<1 || esc_args
[0]>=24)) {
1368 request_resize (cols
, def(esc_args
[0], 24), 0);
1372 case ANSI('|', '*'):
1373 /* VT420 sequence DECSNLS
1374 * Set number of lines on screen
1375 * VT420 uses VGA like hardware and can support any size in
1376 * reasonable range (24..49 AIUI) with no default specified.
1378 compatibility(VT420
);
1379 if (esc_nargs
==1 && esc_args
[0]>0) {
1380 request_resize (cols
, def(esc_args
[0], cfg
.height
), 0);
1384 case ANSI('|', '$'):
1385 /* VT340/VT420 sequence DECSCPP
1386 * Set number of columns per page
1387 * Docs imply range is only 80 or 132, but I'll allow any.
1389 compatibility(VT340TEXT
);
1391 request_resize (def(esc_args
[0], cfg
.width
), rows
, 0);
1395 case 'X': /* write N spaces w/o moving cursor */
1396 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1397 compatibility(ANSIMIN
);
1399 int n
= def(esc_args
[0], 1);
1400 unsigned long *p
= cpos
;
1401 if (n
> cols
- curs_x
)
1403 check_selection (cpos
, cpos
+n
);
1406 seen_disp_event
= TRUE
;
1409 case 'x': /* report terminal characteristics */
1410 compatibility(VT100
);
1413 int i
= def(esc_args
[0], 0);
1414 if (i
== 0 || i
== 1) {
1415 strcpy (buf
, "\033[2;1;1;112;112;1;0x");
1417 ldisc
->send (buf
, 20);
1422 compatibility(OTHER
);
1423 use_bce
= (esc_args
[0]<=0);
1424 erase_char
= ERASE_CHAR
;
1426 erase_char
= (' '|(curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
)));
1429 compatibility(OTHER
);
1430 blink_is_real
= (esc_args
[0]>=1);
1433 /* Allow the host to make this emulator a 'perfect' VT102.
1434 * This first appeared in the VT220, but we do need to get
1435 * back to PuTTY mode so I won't check it.
1437 * The arg in 40..42 are a PuTTY extension.
1438 * The 2nd arg, 8bit vs 7bit is not checked.
1440 * Setting VT102 mode should also change the Fkeys to
1441 * generate PF* codes as a real VT102 has no Fkeys.
1442 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1445 * Note ESC c will NOT change this!
1448 switch (esc_args
[0]) {
1449 case 61: compatibility_level
&= ~TM_VTXXX
;
1450 compatibility_level
|= TM_VT102
; break;
1451 case 62: compatibility_level
&= ~TM_VTXXX
;
1452 compatibility_level
|= TM_VT220
; break;
1454 default: if( esc_args
[0] > 60 && esc_args
[0] < 70 )
1455 compatibility_level
|= TM_VTXXX
;
1458 case 40: compatibility_level
&= TM_VTXXX
; break;
1459 case 41: compatibility_level
= TM_PUTTY
; break;
1460 case 42: compatibility_level
= TM_SCOANSI
; break;
1463 compatibility_level
= TM_PUTTY
; break;
1467 /* Change the response to CSI c */
1468 if (esc_args
[0] == 50) {
1471 strcpy(id_string
, "\033[?");
1472 for (i
=1; i
<esc_nargs
; i
++) {
1473 if (i
!=1) strcat(id_string
, ";");
1474 sprintf(lbuf
, "%d", esc_args
[i
]);
1475 strcat(id_string
, lbuf
);
1477 strcat(id_string
, "c");
1481 /* Is this a good idea ?
1482 * Well we should do a soft reset at this point ...
1484 if (!has_compat(VT420
) && has_compat(VT100
)) {
1485 if (reset_132
) request_resize (132, 24, 1);
1486 else request_resize ( 80, 24, 1);
1494 /* VT100 only here, checked above */
1497 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_GBCHR
;
1500 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_LINEDRW
;
1503 default: /* specifically, 'B' */
1504 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_ASCII
;
1507 if( !has_compat(VT220
) || c
!= '%' )
1508 termstate
= TOPLEVEL
;
1513 case 'P': /* Linux palette sequence */
1514 termstate
= SEEN_OSC_P
;
1517 case 'R': /* Linux palette reset */
1520 termstate
= TOPLEVEL
;
1522 case 'W': /* word-set */
1523 termstate
= SEEN_OSC_W
;
1526 case '0': case '1': case '2': case '3': case '4':
1527 case '5': case '6': case '7': case '8': case '9':
1528 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1532 * Grotty hack to support xterm and DECterm title
1533 * sequences concurrently.
1535 if (esc_args
[0] == 2) {
1539 /* else fall through */
1541 termstate
= OSC_STRING
;
1547 * This OSC stuff is EVIL. It takes just one character to get into
1548 * sysline mode and it's not initially obvious how to get out.
1549 * So I've added CR and LF as string aborts.
1550 * This shouldn't effect compatibility as I believe embedded
1551 * control characters are supposed to be interpreted (maybe?)
1552 * and they don't display anything useful anyway.
1556 if (c
== '\n' || c
== '\r') {
1557 termstate
= TOPLEVEL
;
1558 } else if (c
== 0234 || c
== '\007' ) {
1560 * These characters terminate the string; ST and BEL
1561 * terminate the sequence and trigger instant
1562 * processing of it, whereas ESC goes back to SEEN_ESC
1563 * mode unless it is followed by \, in which case it is
1564 * synonymous with ST in the first place.
1567 termstate
= TOPLEVEL
;
1568 } else if (c
== '\033')
1569 termstate
= OSC_MAYBE_ST
;
1570 else if (osc_strlen
< OSC_STR_MAX
)
1571 osc_string
[osc_strlen
++] = c
;
1575 int max
= (osc_strlen
== 0 ?
21 : 16);
1577 if (c
>= '0' && c
<= '9')
1579 else if (c
>= 'A' && c
<= 'A'+max
-10)
1581 else if (c
>= 'a' && c
<= 'a'+max
-10)
1584 termstate
= TOPLEVEL
;
1585 osc_string
[osc_strlen
++] = val
;
1586 if (osc_strlen
>= 7) {
1587 palette_set (osc_string
[0],
1588 osc_string
[1] * 16 + osc_string
[2],
1589 osc_string
[3] * 16 + osc_string
[4],
1590 osc_string
[5] * 16 + osc_string
[6]);
1592 termstate
= TOPLEVEL
;
1598 case '0': case '1': case '2': case '3': case '4':
1599 case '5': case '6': case '7': case '8': case '9':
1600 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1603 termstate
= OSC_STRING
;
1610 unsigned long nlattr
;
1616 n
= rows
* (cols
+1);
1618 *p
++ = ATTR_DEFAULT
| 'E';
1620 seen_disp_event
= TRUE
;
1621 check_selection (scrtop
, scrtop
+ rows
* (cols
+1));
1624 case '3': nlattr
= LATTR_TOP
; if(0) {
1625 case '4': nlattr
= LATTR_BOT
; } if(0) {
1626 case '5': nlattr
= LATTR_NORM
; } if(0) {
1627 case '6': nlattr
= LATTR_WIDE
; }
1629 p
= scrtop
+ curs_y
* (cols
+1) + cols
;
1634 termstate
= TOPLEVEL
;
1637 termstate
= TOPLEVEL
;
1638 seen_disp_event
= TRUE
;
1641 move (curs_x
, curs_y
- 1, 1);
1644 move (curs_x
, curs_y
+ 1, 1);
1647 move (curs_x
+ 1, curs_y
, 1);
1650 move (curs_x
- 1, curs_y
, 1);
1653 cset_attr
[cset
=0] = ATTR_LINEDRW
;
1656 cset_attr
[cset
=0] = ATTR_ASCII
;
1663 scroll (0, rows
-1, -1, TRUE
);
1664 else if (curs_y
> 0)
1670 erase_lots(FALSE
, FALSE
, TRUE
);
1674 erase_lots(TRUE
, FALSE
, TRUE
);
1677 /* XXX Print cursor line */
1680 /* XXX Start controller mode */
1683 /* XXX Stop controller mode */
1686 termstate
= VT52_Y1
;
1689 ldisc
->send ("\033/Z", 3);
1692 app_keypad_keys
= TRUE
;
1695 app_keypad_keys
= FALSE
;
1698 /* XXX This should switch to VT100 mode not current or default
1699 * VT mode. But this will only have effect in a VT220+
1705 /* XXX Enter auto print mode */
1708 /* XXX Exit auto print mode */
1711 /* XXX Print screen */
1716 termstate
= VT52_Y2
;
1717 move(curs_x
, c
-' ', 0);
1720 termstate
= TOPLEVEL
;
1721 move(c
-' ', curs_y
, 0);
1724 if (selstate
!= NO_SELECTION
)
1725 check_selection (cpos
, cpos
+1);
1731 if(!beep_count
) beep_overload
=0;
1733 else if(beep_count
&& beep_count
<5 && cfg
.beep
)
1738 * Compare two lines to determine whether they are sufficiently
1739 * alike to scroll-optimise one to the other. Return the degree of
1742 static int linecmp (unsigned long *a
, unsigned long *b
) {
1745 for (i
=n
=0; i
< cols
; i
++)
1746 n
+= (*a
++ == *b
++);
1751 * Given a context, update the window. Out of paranoia, we don't
1752 * allow WM_PAINT responses to do scrolling optimisations.
1754 static void do_paint (Context ctx
, int may_optimise
){
1755 int i
, j
, start
, our_curs_y
;
1756 unsigned long attr
, rv
, cursor
;
1761 if (blinker
|| !cfg
.blink_cur
)
1762 cursor
= ATTR_ACTCURS
;
1767 cursor
= ATTR_PASCURS
;
1770 rv
= (rvideo ? ATTR_REVERSE
: 0);
1771 our_curs_y
= curs_y
+ (scrtop
- disptop
) / (cols
+1);
1773 for (i
=0; i
<rows
; i
++) {
1774 int idx
= i
*(cols
+1);
1775 int lattr
= (disptop
[idx
+cols
] & LATTR_MODE
);
1776 for (j
=0; j
<=cols
; j
++,idx
++) {
1777 unsigned long *d
= disptop
+idx
;
1778 wanttext
[idx
] = lattr
| ((*d
^ rv
1779 ^ (selstart
<= d
&& d
< selend ?
1780 ATTR_REVERSE
: 0)) |
1781 (i
==our_curs_y
&& j
==curs_x ? cursor
: 0));
1783 if (blink_is_real
) {
1784 if (has_focus
&& tblinker
&& (wanttext
[idx
]&ATTR_BLINK
) )
1786 wanttext
[idx
] &= ATTR_MASK
;
1787 wanttext
[idx
] += ' ';
1789 wanttext
[idx
] &= ~ATTR_BLINK
;
1795 * We would perform scrolling optimisations in here, if they
1796 * didn't have a nasty tendency to cause the whole sodding
1797 * program to hang for a second at speed-critical moments.
1798 * We'll leave it well alone...
1801 for (i
=0; i
<rows
; i
++) {
1802 int idx
= i
*(cols
+1);
1803 int lattr
= (wanttext
[idx
+cols
] & LATTR_MODE
);
1805 for (j
=0; j
<=cols
; j
++,idx
++) {
1806 unsigned long t
= wanttext
[idx
];
1807 int needs_update
= (j
< cols
&& t
!= disptext
[idx
]);
1808 int keep_going
= (start
!= -1 && needs_update
&&
1809 (t
& ATTR_MASK
) == attr
&&
1810 j
-start
< sizeof(ch
));
1811 if (start
!= -1 && !keep_going
) {
1812 do_text (ctx
, start
, i
, ch
, j
-start
, attr
, lattr
);
1818 attr
= t
& ATTR_MASK
;
1820 ch
[j
-start
] = (char) (t
& CHAR_MASK
);
1828 * Flick the switch that says if blinking things should be shown or hidden.
1831 void term_blink(int flg
) {
1832 static long last_blink
= 0;
1833 static long last_tblink
= 0;
1834 long now
, blink_diff
;
1836 now
= GetTickCount();
1837 blink_diff
= now
-last_tblink
;
1839 /* Make sure the text blinks no more than 2Hz */
1840 if (blink_diff
<0 || blink_diff
>450)
1843 tblinker
= !tblinker
;
1852 blink_diff
= now
-last_blink
;
1854 /* Make sure the cursor blinks no more than 2Hz */
1855 if (blink_diff
>=0 && blink_diff
<450)
1863 * Invalidate the whole screen so it will be repainted in full.
1865 void term_invalidate(void) {
1868 for (i
=0; i
<rows
*(cols
+1); i
++)
1869 disptext
[i
] = ATTR_INVALID
;
1873 * Paint the window in response to a WM_PAINT message.
1875 void term_paint (Context ctx
, int l
, int t
, int r
, int b
) {
1876 int i
, j
, left
, top
, right
, bottom
;
1878 left
= l
/ font_width
;
1879 right
= (r
- 1) / font_width
;
1880 top
= t
/ font_height
;
1881 bottom
= (b
- 1) / font_height
;
1882 for (i
= top
; i
<= bottom
&& i
< rows
; i
++)
1884 if ( (disptext
[i
*(cols
+1)+cols
]&LATTR_MODE
) == LATTR_NORM
)
1885 for (j
= left
; j
<= right
&& j
< cols
; j
++)
1886 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
1888 for (j
= left
/2; j
<= right
/2+1 && j
< cols
; j
++)
1889 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
1892 /* This should happen soon enough, also for some reason it sometimes
1893 * fails to actually do anything when re-sizing ... painting the wrong
1895 do_paint (ctx, FALSE);
1900 * Attempt to scroll the scrollback. The second parameter gives the
1901 * position we want to scroll to; the first is +1 to denote that
1902 * this position is relative to the beginning of the scrollback, -1
1903 * to denote it is relative to the end, and 0 to denote that it is
1904 * relative to the current position.
1906 void term_scroll (int rel
, int where
) {
1907 int n
= where
* (cols
+1);
1909 disptop
= (rel
< 0 ? scrtop
:
1910 rel
> 0 ? sbtop
: disptop
) + n
;
1911 if (disptop
< sbtop
)
1913 if (disptop
> scrtop
)
1920 * Spread the selection outwards according to the selection mode.
1922 static unsigned long *sel_spread_half (unsigned long *p
, int dir
) {
1923 unsigned long *linestart
, *lineend
;
1927 x
= (p
- text
) % (cols
+1);
1929 lineend
= linestart
+ cols
;
1934 * In this mode, every character is a separate unit, except
1935 * for runs of spaces at the end of a non-wrapping line.
1937 if (!(linestart
[cols
] & ATTR_WRAPPED
)) {
1938 unsigned long *q
= lineend
;
1939 while (q
> linestart
&& (q
[-1] & CHAR_MASK
) == 0x20)
1944 p
= (dir
== -1 ? q
: lineend
- 1);
1949 * In this mode, the units are maximal runs of characters
1950 * whose `wordness' has the same value.
1952 wvalue
= wordness
[*p
& CHAR_MASK
];
1954 while (p
< lineend
&& wordness
[p
[1] & CHAR_MASK
] == wvalue
)
1957 while (p
> linestart
&& wordness
[p
[-1] & CHAR_MASK
] == wvalue
)
1963 * In this mode, every line is a unit.
1965 p
= (dir
== -1 ? linestart
: lineend
- 1);
1971 static void sel_spread (void) {
1972 selstart
= sel_spread_half (selstart
, -1);
1973 selend
= sel_spread_half (selend
- 1, +1) + 1;
1976 void term_mouse (Mouse_Button b
, Mouse_Action a
, int x
, int y
) {
1977 unsigned long *selpoint
;
1980 if (y
>=rows
) y
= rows
-1;
1988 if (x
>=cols
) x
= cols
-1;
1990 selpoint
= disptop
+ y
* (cols
+1);
1991 if ((selpoint
[cols
]&LATTR_MODE
) != LATTR_NORM
)
1996 if (b
== MB_SELECT
&& a
== MA_CLICK
) {
1998 selstate
= ABOUT_TO
;
1999 selanchor
= selpoint
;
2001 } else if (b
== MB_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
2003 selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
2004 selstate
= DRAGGING
;
2005 selstart
= selanchor
= selpoint
;
2006 selend
= selstart
+ 1;
2008 } else if ((b
== MB_SELECT
&& a
== MA_DRAG
) ||
2009 (b
== MB_EXTEND
&& a
!= MA_RELEASE
)) {
2010 if (selstate
== ABOUT_TO
&& selanchor
== selpoint
)
2012 if (b
== MB_EXTEND
&& a
!= MA_DRAG
&& selstate
== SELECTED
) {
2013 if (selpoint
-selstart
< (selend
-selstart
)/2)
2014 selanchor
= selend
- 1;
2016 selanchor
= selstart
;
2017 selstate
= DRAGGING
;
2019 if (selstate
!= ABOUT_TO
&& selstate
!= DRAGGING
)
2020 selanchor
= selpoint
;
2021 selstate
= DRAGGING
;
2022 if (selpoint
< selanchor
) {
2023 selstart
= selpoint
;
2024 selend
= selanchor
+ 1;
2026 selstart
= selanchor
;
2027 selend
= selpoint
+ 1;
2030 } else if ((b
== MB_SELECT
|| b
== MB_EXTEND
) && a
== MA_RELEASE
) {
2031 if (selstate
== DRAGGING
) {
2033 * We've completed a selection. We now transfer the
2034 * data to the clipboard.
2036 unsigned char *p
= selspace
;
2037 unsigned long *q
= selstart
;
2039 while (q
< selend
) {
2041 unsigned long *lineend
= q
- (q
-text
) % (cols
+1) + cols
;
2042 unsigned long *nlpos
= lineend
;
2044 if (!(*nlpos
& ATTR_WRAPPED
)) {
2045 while ((nlpos
[-1] & CHAR_MASK
) == 0x20 && nlpos
> q
)
2050 while (q
< nlpos
&& q
< selend
)
2053 /* VT Specials -> ISO8859-1 */
2054 static const char poorman2
[] =
2055 "* # HTFFCRLF\xB0 \xB1 NLVT+ + + + + - - - - - + + + + | <=>=PI!=\xA3 \xB7 ";
2058 int ch
= (*q
& CHAR_MASK
);
2061 if ((*q
& ATTR_LINEDRW
) && ch
>= 0x60 && ch
< 0x7F) {
2063 *p
++ = poorman2
[2*(ch
-0x60)];
2064 if ( (x
= poorman2
[2*(ch
-0x60)+1]) != ' ')
2069 if ((*q
& ATTR_GBCHR
) && ch
== '#')
2070 *p
++ = (unsigned char) 0xA3;
2073 *p
++ = (unsigned char) ch
;
2078 for (i
=0; i
<sizeof(sel_nl
); i
++)
2081 q
= lineend
+ 1; /* start of next line */
2083 write_clip (selspace
, p
- selspace
, FALSE
);
2084 selstate
= SELECTED
;
2086 selstate
= NO_SELECTION
;
2087 } else if (b
== MB_PASTE
&& (a
==MA_CLICK
|| a
==MA_2CLK
|| a
==MA_3CLK
)) {
2091 get_clip((void **) &data
, &len
);
2095 if (paste_buffer
) sfree(paste_buffer
);
2096 paste_pos
= paste_hold
= paste_len
= 0;
2097 paste_buffer
= smalloc(len
);
2100 while (p
< data
+len
) {
2101 while (p
< data
+len
&&
2102 !(p
<= data
+len
-sizeof(sel_nl
) &&
2103 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
2111 c
=xlat_kbd2tty(q
[i
]);
2112 paste_buffer
[paste_len
++] = c
;
2116 if (p
<= data
+len
-sizeof(sel_nl
) &&
2117 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
2118 paste_buffer
[paste_len
++] = '\r';
2119 p
+= sizeof(sel_nl
);
2124 /* Assume a small paste will be OK in one go. */
2125 if (paste_len
<256) {
2126 ldisc
->send (paste_buffer
, paste_len
);
2127 if (paste_buffer
) sfree(paste_buffer
);
2129 paste_pos
= paste_hold
= paste_len
= 0;
2132 get_clip(NULL
, NULL
);
2138 void term_nopaste() {
2139 if(paste_len
== 0) return;
2140 sfree(paste_buffer
);
2146 static long last_paste
= 0;
2147 long now
, paste_diff
;
2149 if(paste_len
== 0) return;
2151 /* Don't wait forever to paste */
2153 now
= GetTickCount();
2154 paste_diff
= now
-last_paste
;
2155 if (paste_diff
>=0 && paste_diff
<450)
2160 while(paste_pos
<paste_len
)
2163 while (n
+ paste_pos
< paste_len
) {
2164 if (paste_buffer
[paste_pos
+ n
++] == '\r')
2167 ldisc
->send (paste_buffer
+paste_pos
, n
);
2170 if (paste_pos
< paste_len
) {
2175 sfree(paste_buffer
);
2180 static void deselect (void) {
2181 selstate
= NO_SELECTION
;
2182 selstart
= selend
= scrtop
;
2185 void term_deselect (void) {
2191 * from_backend(), to get data from the backend for the terminal.
2193 void from_backend(int is_stderr
, char *data
, int len
) {
2195 if (inbuf_head
>= INBUF_SIZE
)
2197 inbuf
[inbuf_head
++] = *data
++;