9 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
10 #define CL_VT100 0x0002 /* VT100 */
11 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
12 #define CL_VT102 0x0008 /* VT102 */
13 #define CL_VT220 0x0010 /* VT220 */
14 #define CL_VT320 0x0020 /* VT320 */
15 #define CL_VT420 0x0040 /* VT420 */
16 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
17 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
18 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
19 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
20 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
22 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
23 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
24 #define TM_VT102 (TM_VT100AVO|CL_VT102)
25 #define TM_VT220 (TM_VT102|CL_VT220)
26 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
27 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
29 #define TM_PUTTY (0xFFFF)
31 #define compatibility(x) \
32 if ( ((CL_##x)&compatibility_level) == 0 ) { \
36 #define compatibility2(x,y) \
37 if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
42 #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
44 static int compatibility_level
= TM_PUTTY
;
47 static unsigned long *text
; /* buffer of text on terminal screen */
48 static unsigned long *scrtop
; /* top of working screen */
49 static unsigned long *disptop
; /* top of displayed screen */
50 static unsigned long *sbtop
; /* top of scrollback */
51 static unsigned long *sbbot
; /* furthest extent of scrollback */
52 static unsigned long *cpos
; /* cursor position (convenience) */
53 static unsigned long *disptext
; /* buffer of text on real screen */
54 static unsigned long *wanttext
; /* buffer of text we want on screen */
55 static unsigned long *alttext
; /* buffer of text on alt. screen */
57 static unsigned char *selspace
; /* buffer for building selections in */
59 #define TSIZE (sizeof(*text))
60 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
62 static unsigned long curr_attr
, save_attr
;
63 static unsigned long erase_char
= ERASE_CHAR
;
65 static int curs_x
, curs_y
; /* cursor */
66 static int save_x
, save_y
; /* saved cursor position */
67 static int marg_t
, marg_b
; /* scroll margins */
68 static int dec_om
; /* DEC origin mode flag */
69 static int wrap
, wrapnext
; /* wrap flags */
70 static int insert
; /* insert-mode flag */
71 static int cset
; /* 0 or 1: which char set */
72 static int save_cset
, save_csattr
; /* saved with cursor position */
73 static int rvideo
; /* global reverse video flag */
74 static int cursor_on
; /* cursor enabled flag */
75 static int reset_132
; /* Flag ESC c resets to 80 cols */
76 static int use_bce
; /* Use Background coloured erase */
77 static int blinker
; /* When blinking is the cursor on ? */
78 static int tblinker
; /* When the blinking text is on */
79 static int blink_is_real
; /* Actually blink blinking text */
81 static unsigned long cset_attr
[2];
84 * Saved settings on the alternate screen.
86 static int alt_x
, alt_y
, alt_om
, alt_wrap
, alt_wnext
, alt_ins
, alt_cset
;
87 static int alt_t
, alt_b
;
90 #define ARGS_MAX 32 /* max # of esc sequence arguments */
91 #define ARG_DEFAULT 0 /* if an arg isn't specified */
92 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
93 static int esc_args
[ARGS_MAX
];
96 #define ANSI(x,y) ((x)+((y)<<8))
97 #define ANSI_QUE(x) ANSI(x,TRUE)
99 #define OSC_STR_MAX 2048
100 static int osc_strlen
;
101 static char osc_string
[OSC_STR_MAX
+1];
104 static char id_string
[1024] = "\033[?6c";
106 static unsigned char *tabs
;
120 OSC_STRING
, OSC_MAYBE_ST
,
128 NO_SELECTION
, ABOUT_TO
, DRAGGING
, SELECTED
131 SM_CHAR
, SM_WORD
, SM_LINE
133 static unsigned long *selstart
, *selend
, *selanchor
;
135 static short wordness
[256] = {
136 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 */
137 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 */
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,2, /* 45 */
139 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 */
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, /* 89 */
141 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 */
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, /* CD */
143 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 */
146 static unsigned char sel_nl
[] = SEL_NL
;
147 static char * paste_buffer
= 0;
148 static int paste_len
, paste_pos
, paste_hold
;
151 * Internal prototypes.
153 static void do_paint (Context
, int);
154 static void erase_lots (int, int, int);
155 static void swap_screen (int);
156 static void update_sbar (void);
157 static void deselect (void);
160 * Set up power-on settings for the terminal.
162 static void power_on(void) {
163 curs_x
= curs_y
= alt_x
= alt_y
= save_x
= save_y
= 0;
166 alt_b
= marg_b
= rows
- 1;
171 for (i
= 0; i
< cols
; i
++)
172 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
174 alt_om
= dec_om
= cfg
.dec_om
;
175 alt_wnext
= wrapnext
= alt_ins
= insert
= FALSE
;
176 alt_wrap
= wrap
= cfg
.wrap_mode
;
178 cset_attr
[0] = cset_attr
[1] = ATTR_ASCII
;
181 save_attr
= curr_attr
= ATTR_DEFAULT
;
182 app_cursor_keys
= cfg
.app_cursor
;
183 app_keypad_keys
= cfg
.app_keypad
;
185 blink_is_real
= cfg
.blinktext
;
186 erase_char
= ERASE_CHAR
;
190 for (i
= 0; i
< 256; i
++)
191 wordness
[i
] = cfg
.wordness
[i
];
195 erase_lots (FALSE
, TRUE
, TRUE
);
197 erase_lots (FALSE
, TRUE
, TRUE
);
202 * Force a screen update.
204 void term_update(void) {
208 if ( (seen_key_event
&& (cfg
.scroll_on_key
)) ||
209 (seen_disp_event
&& (cfg
.scroll_on_disp
)) ) {
211 seen_disp_event
= seen_key_event
= 0;
214 do_paint (ctx
, TRUE
);
215 sys_cursor(curs_x
, curs_y
+ (scrtop
- disptop
) / (cols
+1));
221 * Same as power_on(), but an external function.
223 void term_pwron(void) {
232 * Clear the scrollback.
234 void term_clrsb(void) {
235 disptop
= sbtop
= scrtop
;
240 * Initialise the terminal.
242 void term_init(void) {
243 text
= sbtop
= sbbot
= scrtop
= disptop
= cpos
= NULL
;
244 disptext
= wanttext
= NULL
;
253 * Set up the terminal for a given size.
255 void term_size(int newrows
, int newcols
, int newsavelines
) {
256 unsigned long *newtext
, *newdisp
, *newwant
, *newalt
;
257 int i
, j
, crows
, ccols
;
259 int save_alt_which
= alt_which
;
261 if (newrows
== rows
&& newcols
== cols
&& newsavelines
== savelines
)
262 return; /* nothing to do */
268 alt_b
= marg_b
= newrows
- 1;
270 newtext
= smalloc ((newrows
+newsavelines
)*(newcols
+1)*TSIZE
);
271 sbbot
= newtext
+ newsavelines
*(newcols
+1);
272 for (i
=0; i
<(newrows
+newsavelines
)*(newcols
+1); i
++)
273 newtext
[i
] = erase_char
;
275 crows
= rows
+ (scrtop
- sbtop
) / (cols
+1);
276 if (crows
> newrows
+newsavelines
)
277 crows
= newrows
+newsavelines
;
281 disptop
= newtext
+ (crows
-newrows
)*(newcols
+1);
282 ccols
= (cols
< newcols ? cols
: newcols
);
283 for (i
=0; i
<crows
; i
++) {
284 int oldidx
= (rows
- crows
+ i
) * (cols
+1);
285 int newidx
= (newrows
- crows
+ i
) * (newcols
+1);
287 for (j
=0; j
<ccols
; j
++)
288 disptop
[newidx
+j
] = scrtop
[oldidx
+j
];
289 disptop
[newidx
+newcols
] =
290 (cols
== newcols ? scrtop
[oldidx
+cols
]
291 : (scrtop
[oldidx
+cols
]&LATTR_MODE
));
295 sbtop
= disptop
= newtext
;
301 newdisp
= smalloc (newrows
*(newcols
+1)*TSIZE
);
302 for (i
=0; i
<newrows
*(newcols
+1); i
++)
303 newdisp
[i
] = ATTR_INVALID
;
307 newwant
= smalloc (newrows
*(newcols
+1)*TSIZE
);
308 for (i
=0; i
<newrows
*(newcols
+1); i
++)
309 newwant
[i
] = ATTR_INVALID
;
313 newalt
= smalloc (newrows
*(newcols
+1)*TSIZE
);
314 for (i
=0; i
<newrows
*(newcols
+1); i
++)
315 newalt
[i
] = erase_char
;
320 selspace
= smalloc ( (newrows
+newsavelines
) * (newcols
+sizeof(sel_nl
)) );
322 tabs
= srealloc (tabs
, newcols
*sizeof(*tabs
));
325 for (i
= (cols
> 0 ? cols
: 0); i
< newcols
; i
++)
326 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
330 curs_y
+= newrows
- rows
;
333 if (curs_y
>= newrows
)
335 if (curs_x
>= newcols
)
338 wrapnext
= alt_wnext
= FALSE
;
342 savelines
= newsavelines
;
345 swap_screen(save_alt_which
);
354 static void swap_screen (int which
) {
358 if (which
== alt_which
)
363 for (t
=0; t
<rows
*(cols
+1); t
++) {
364 tt
= scrtop
[t
]; scrtop
[t
] = alttext
[t
]; alttext
[t
] = tt
;
367 t
= curs_x
; curs_x
= alt_x
; alt_x
= t
;
368 t
= curs_y
; curs_y
= alt_y
; alt_y
= t
;
369 t
= marg_t
; marg_t
= alt_t
; alt_t
= t
;
370 t
= marg_b
; marg_b
= alt_b
; alt_b
= t
;
371 t
= dec_om
; dec_om
= alt_om
; alt_om
= t
;
372 t
= wrap
; wrap
= alt_wrap
; alt_wrap
= t
;
373 t
= wrapnext
; wrapnext
= alt_wnext
; alt_wnext
= t
;
374 t
= insert
; insert
= alt_ins
; alt_ins
= t
;
375 t
= cset
; cset
= alt_cset
; alt_cset
= t
;
381 * Update the scroll bar.
383 static void update_sbar(void) {
386 min
= (sbtop
- text
) / (cols
+1);
387 set_sbar ((scrtop
- text
) / (cols
+1) + rows
- min
,
388 (disptop
- text
) / (cols
+1) - min
,
393 * Check whether the region bounded by the two pointers intersects
394 * the scroll region, and de-select the on-screen selection if so.
396 static void check_selection (unsigned long *from
, unsigned long *to
) {
397 if (from
< selend
&& selstart
< to
)
402 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
403 * for backward.) `sb' is TRUE if the scrolling is permitted to
404 * affect the scrollback buffer.
406 static void scroll (int topline
, int botline
, int lines
, int sb
) {
407 unsigned long *scroll_top
;
408 unsigned long *newscr
;
409 int scroll_size
, size
, i
;
410 int scrtop_is_disptop
= (scrtop
==disptop
);
411 static int recursive
= 0;
413 /* Only scroll more than the window if we're doing a 10% scroll */
414 if (!recursive
&& lines
> botline
- topline
+ 1)
415 lines
= botline
- topline
+ 1;
417 scroll_top
= scrtop
+ topline
*(cols
+1);
418 size
= (lines
< 0 ?
-lines
: lines
) * (cols
+1);
419 scroll_size
= (botline
- topline
+ 1) * (cols
+1) - size
;
421 if (lines
> 0 && topline
== 0 && alt_which
== 0 && sb
) {
423 * Since we're going to scroll the top line and we're on the
424 * scrolling screen let's also effect the scrollback buffer.
426 * This is normally done by moving the position the screen
427 * painter reads from to reduce the amount of memory copying
430 if (scroll_size
>= 0 && !recursive
) {
431 newscr
= scrtop
+ lines
* (cols
+1);
433 if (newscr
> sbbot
&& botline
== rows
-1) {
434 /* We've hit the bottom of memory, so we have to do a
435 * physical scroll. But instead of just 1 line do it
436 * by 10% of the available memory.
438 * If the scroll region isn't the whole screen then we can't
439 * do this as it stands. We would need to recover the bottom
440 * of the screen from the scroll buffer after being sure that
441 * it doesn't get deleted.
444 i
= (rows
+savelines
)/10;
446 /* Make it simple and ensure safe recursion */
447 if ( i
<savelines
-1) {
449 scroll(topline
, botline
, i
, sb
);
452 newscr
= scrtop
- i
* (cols
+1);
453 if (scrtop_is_disptop
) disptop
= newscr
;
457 newscr
= scrtop
+ lines
* (cols
+1);
460 if (newscr
<= sbbot
) {
461 if (scrtop_is_disptop
) disptop
= newscr
;
464 if (botline
== rows
-1 )
465 for (i
= 0; i
< size
; i
++)
466 scrtop
[i
+scroll_size
] = erase_char
;
471 if (botline
!= rows
-1) {
472 /* This fastscroll only works for full window scrolls.
473 * If the caller wanted a partial one we have to reverse
474 * scroll the bottom of the screen.
476 scroll(botline
-lines
+1, rows
-1, -lines
, 0);
481 /* If we can't scroll by memory remapping do it physically.
482 * But rather than expensivly doing the scroll buffer just
483 * scroll the screen. All it means is that sometimes we choose
484 * to not add lines from a scroll region to the scroll buffer.
487 if (savelines
<= 400) {
488 sbtop
-= lines
* (cols
+1);
491 scroll_size
+= scroll_top
- sbtop
;
497 /* Ho hum, expensive scroll required. */
499 sbtop
-= lines
* (cols
+1);
502 scroll_size
+= scroll_top
- sbtop
;
509 if (scroll_size
< 0) {
516 memmove (scroll_top
, scroll_top
+ size
, scroll_size
*TSIZE
);
517 for (i
= 0; i
< size
; i
++)
518 scroll_top
[i
+scroll_size
] = erase_char
;
519 if (selstart
> scroll_top
&&
520 selstart
< scroll_top
+ size
+ scroll_size
) {
522 if (selstart
< scroll_top
)
523 selstart
= scroll_top
;
525 if (selend
> scroll_top
&&
526 selend
< scroll_top
+ size
+ scroll_size
) {
528 if (selend
< scroll_top
)
531 if (scrtop_is_disptop
)
534 if (disptop
> scroll_top
&&
535 disptop
< scroll_top
+ size
+ scroll_size
) {
537 if (disptop
< scroll_top
)
538 disptop
= scroll_top
;
542 memmove (scroll_top
+ size
, scroll_top
, scroll_size
*TSIZE
);
543 for (i
= 0; i
< size
; i
++)
544 scroll_top
[i
] = erase_char
;
545 if (selstart
> scroll_top
&&
546 selstart
< scroll_top
+ size
+ scroll_size
) {
548 if (selstart
> scroll_top
+ size
+ scroll_size
)
549 selstart
= scroll_top
+ size
+ scroll_size
;
551 if (selend
> scroll_top
&&
552 selend
< scroll_top
+ size
+ scroll_size
) {
554 if (selend
> scroll_top
+ size
+ scroll_size
)
555 selend
= scroll_top
+ size
+ scroll_size
;
557 if (scrtop_is_disptop
)
559 else if (disptop
> scroll_top
&&
560 disptop
< scroll_top
+ size
+ scroll_size
) {
562 if (disptop
> scroll_top
+ size
+ scroll_size
)
563 disptop
= scroll_top
+ size
+ scroll_size
;
569 * Move the cursor to a given position, clipping at boundaries. We
570 * may or may not want to clip at the scroll margin: marg_clip is 0
571 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
572 * even _being_ outside the margins.
574 static void move (int x
, int y
, int marg_clip
) {
580 if ((curs_y
>= marg_t
|| marg_clip
== 2) && y
< marg_t
)
582 if ((curs_y
<= marg_b
|| marg_clip
== 2) && y
> marg_b
)
596 * Save or restore the cursor and SGR mode.
598 static void save_cursor(int save
) {
602 save_attr
= curr_attr
;
604 save_csattr
= cset_attr
[cset
];
608 /* Make sure the window hasn't shrunk since the save */
609 if (curs_x
>= cols
) curs_x
= cols
-1;
610 if (curs_y
>= rows
) curs_y
= rows
-1;
612 curr_attr
= save_attr
;
614 cset_attr
[cset
] = save_csattr
;
616 if (use_bce
) erase_char
= (' ' |(curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
)));
621 * Erase a large portion of the screen: the whole screen, or the
622 * whole line, or parts thereof.
624 static void erase_lots (int line_only
, int from_begin
, int to_end
) {
625 unsigned long *startpos
, *endpos
;
628 startpos
= cpos
- curs_x
;
629 endpos
= startpos
+ cols
;
630 /* I've removed the +1 so that the Wide screen stuff is not
631 * removed when it shouldn't be.
635 endpos
= startpos
+ rows
* (cols
+1);
641 check_selection (startpos
, endpos
);
643 /* Clear screen also forces a full window redraw, just in case. */
644 if (startpos
== scrtop
&& endpos
== scrtop
+ rows
* (cols
+1))
647 while (startpos
< endpos
)
648 *startpos
++ = erase_char
;
652 * Insert or delete characters within the current line. n is +ve if
653 * insertion is desired, and -ve for deletion.
655 static void insch (int n
) {
656 int dir
= (n
< 0 ?
-1 : +1);
659 n
= (n
< 0 ?
-n
: n
);
660 if (n
> cols
- curs_x
)
662 m
= cols
- curs_x
- n
;
663 check_selection (cpos
, cpos
+n
);
665 memmove (cpos
, cpos
+n
, m
*TSIZE
);
667 cpos
[m
++] = erase_char
;
669 memmove (cpos
+n
, cpos
, m
*TSIZE
);
671 cpos
[n
] = erase_char
;
676 * Toggle terminal mode `mode' to state `state'. (`query' indicates
677 * whether the mode is a DEC private one or a normal one.)
679 static void toggle_mode (int mode
, int query
, int state
) {
680 if (query
) switch (mode
) {
681 case 1: /* application cursor keys */
682 app_cursor_keys
= state
;
684 case 2: /* VT52 mode */
687 case 3: /* 80/132 columns */
689 request_resize (state ?
132 : 80, rows
, 1);
692 case 5: /* reverse video */
694 seen_disp_event
= TRUE
;
695 if (state
) term_update();
697 case 6: /* DEC origin mode */
700 case 7: /* auto wrap */
703 case 8: /* auto key repeat */
706 case 25: /* enable/disable cursor */
707 compatibility2(OTHER
,VT220
);
709 seen_disp_event
= TRUE
;
711 case 47: /* alternate screen */
712 compatibility(OTHER
);
717 } else switch (mode
) {
718 case 4: /* set insert mode */
719 compatibility(VT102
);
722 case 12: /* set echo mode */
724 * This may be very good in smcup and rmcup (or smkx & rmkx) if you
725 * have a long RTT and the telnet client/daemon doesn't understand
728 * DONT send TS_RECHO/TS_LECHO; the telnet daemon tries to fix the
729 * tty and _really_ confuses some programs.
731 compatibility2(OTHER
,VT220
);
732 ldisc
= (state?
&ldisc_simple
: &ldisc_term
);
734 case 20: /* Return sends ... */
735 cr_lf_return
= state
;
741 * Process an OSC sequence: set window title or icon name.
743 static void do_osc(void) {
746 wordness
[(unsigned char)osc_string
[osc_strlen
]] = esc_args
[0];
748 osc_string
[osc_strlen
] = '\0';
749 switch (esc_args
[0]) {
752 set_icon (osc_string
);
753 if (esc_args
[0] == 1)
755 /* fall through: parameter 0 means set both */
758 set_title (osc_string
);
765 * Remove everything currently in `inbuf' and stick it up on the
766 * in-memory display. There's a big state machine in here to
767 * process escape sequences...
769 void term_out(void) {
772 static int beep_overload
= 0;
775 for(inbuf_reap
= 0; inbuf_reap
< inbuf_head
; inbuf_reap
++)
777 c
= inbuf
[inbuf_reap
];
780 * Optionally log the session traffic to a file. Useful for
781 * debugging and possibly also useful for actual logging.
784 static FILE *fp
= NULL
;
785 if (!fp
) fp
= fopen(logfile
, "wb");
786 if (fp
) fputc (c
, fp
);
788 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
789 * be able to display 8-bit characters, but I'll let that go 'cause
792 if( ( (c
&0x60) == 0 || c
== '\177') &&
793 termstate
< DO_CTRLS
&&
794 ( (c
&0x80) == 0 || has_compat(VT220
))) {
796 case '\005': /* terminal type query */
797 /* Strictly speaking this is VT100 but a VT100 defaults to
798 * no response. Other terminals respond at their option.
800 * Don't put a CR in the default string as this tends to
801 * upset some weird software.
803 * An xterm returns "xterm" (5 characters)
805 compatibility(OTHER
);
806 ldisc
->send ("PuTTY", 5);
810 if(beep_count
>6) beep_overload
=1;
814 if (curs_x
== 0 && curs_y
== 0)
816 else if (curs_x
== 0 && curs_y
> 0)
817 curs_x
= cols
-1, curs_y
--;
823 seen_disp_event
= TRUE
;
826 compatibility(VT100
);
830 compatibility(VT100
);
835 termstate
= VT52_ESC
;
837 compatibility(ANSIMIN
);
838 termstate
= SEEN_ESC
;
842 compatibility(VT220
);
843 termstate
= SEEN_CSI
;
845 esc_args
[0] = ARG_DEFAULT
;
849 compatibility(VT220
);
850 termstate
= SEEN_OSC
;
857 seen_disp_event
= TRUE
;
862 compatibility(VT100
);
864 if (curs_y
== marg_b
)
865 scroll (marg_t
, marg_b
, 1, TRUE
);
866 else if (curs_y
< rows
-1)
877 unsigned long *old_cpos
= cpos
;
878 unsigned long *p
= scrtop
+ curs_y
* (cols
+1) + cols
;
882 } while (curs_x
< cols
-1 && !tabs
[curs_x
]);
884 if ((*p
& LATTR_MODE
) != LATTR_NORM
)
886 if (curs_x
>= cols
/2)
896 check_selection (old_cpos
, cpos
);
898 seen_disp_event
= TRUE
;
900 case '\177': /* Destructive backspace
901 This does nothing on a real VT100 */
902 compatibility(OTHER
);
903 if (curs_x
&& !wrapnext
) curs_x
--;
906 *cpos
= (' ' | curr_attr
| ATTR_ASCII
);
910 else switch (termstate
) {
912 /* Only graphic characters get this far, ctrls are stripped above */
914 cpos
[1] |= ATTR_WRAPPED
;
915 if (curs_y
== marg_b
)
916 scroll (marg_t
, marg_b
, 1, TRUE
);
917 else if (curs_y
< rows
-1)
925 if (selstate
!= NO_SELECTION
)
926 check_selection (cpos
, cpos
+1);
927 switch (cset_attr
[cset
]) {
928 /* Linedraw characters are different from 'ESC ( B' only
929 * for a small range, for ones outside that range make sure
930 * we use the same font as well as the same encoding.
933 if (c
<0x5f || c
>0x7F)
934 *cpos
++ = xlat_tty2scr((unsigned char)c
) | curr_attr
|
937 *cpos
++ = ' ' | curr_attr
| ATTR_ASCII
;
939 *cpos
++ = ((unsigned char)c
) | curr_attr
| ATTR_LINEDRW
;
942 /* If UK-ASCII, make the '#' a LineDraw Pound */
944 *cpos
++ = '}' | curr_attr
| ATTR_LINEDRW
;
949 *cpos
++ = xlat_tty2scr((unsigned char)c
) | curr_attr
|
950 (c
<= 0x7F ? cset_attr
[cset
] : ATTR_ASCII
);
954 if (curs_x
== cols
) {
963 termstate
= TOPLEVEL
;
967 * This state is virtually identical to SEEN_ESC, with the
968 * exception that we have an OSC sequence in the pipeline,
969 * and _if_ we see a backslash, we process it.
973 termstate
= TOPLEVEL
;
976 /* else fall through */
978 termstate
= TOPLEVEL
;
980 case ' ': /* some weird sequence? */
981 compatibility(VT220
);
982 termstate
= IGNORE_NEXT
;
984 case '[': /* enter CSI mode */
985 termstate
= SEEN_CSI
;
987 esc_args
[0] = ARG_DEFAULT
;
990 case ']': /* xterm escape sequences */
991 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
992 compatibility(OTHER
);
993 termstate
= SEEN_OSC
;
996 case '(': /* should set GL */
997 compatibility(VT100
);
1000 case ')': /* should set GR */
1001 compatibility(VT100
);
1004 case '7': /* save cursor */
1005 compatibility(VT100
);
1008 case '8': /* restore cursor */
1009 compatibility(VT100
);
1010 save_cursor (FALSE
);
1011 seen_disp_event
= TRUE
;
1014 compatibility(VT100
);
1015 app_keypad_keys
= TRUE
;
1018 compatibility(VT100
);
1019 app_keypad_keys
= FALSE
;
1021 case 'D': /* exactly equivalent to LF */
1022 compatibility(VT100
);
1023 if (curs_y
== marg_b
)
1024 scroll (marg_t
, marg_b
, 1, TRUE
);
1025 else if (curs_y
< rows
-1)
1029 seen_disp_event
= TRUE
;
1031 case 'E': /* exactly equivalent to CR-LF */
1032 compatibility(VT100
);
1034 if (curs_y
== marg_b
)
1035 scroll (marg_t
, marg_b
, 1, TRUE
);
1036 else if (curs_y
< rows
-1)
1040 seen_disp_event
= TRUE
;
1042 case 'M': /* reverse index - backwards LF */
1043 compatibility(VT100
);
1044 if (curs_y
== marg_t
)
1045 scroll (marg_t
, marg_b
, -1, TRUE
);
1046 else if (curs_y
> 0)
1050 seen_disp_event
= TRUE
;
1052 case 'Z': /* terminal type query */
1053 compatibility(VT100
);
1054 ldisc
->send (id_string
, strlen(id_string
));
1056 case 'c': /* restore power-on settings */
1057 compatibility(VT100
);
1060 request_resize (80, rows
, 1);
1065 seen_disp_event
= TRUE
;
1067 case '#': /* ESC # 8 fills screen with Es :-) */
1068 compatibility(VT100
);
1069 termstate
= SEEN_ESCHASH
;
1071 case 'H': /* set a tab */
1072 compatibility(VT100
);
1073 tabs
[curs_x
] = TRUE
;
1078 termstate
= TOPLEVEL
; /* default */
1081 if (esc_nargs
<= ARGS_MAX
) {
1082 if (esc_args
[esc_nargs
-1] == ARG_DEFAULT
)
1083 esc_args
[esc_nargs
-1] = 0;
1084 esc_args
[esc_nargs
-1] =
1085 10 * esc_args
[esc_nargs
-1] + c
- '0';
1087 termstate
= SEEN_CSI
;
1091 if (++esc_nargs
<= ARGS_MAX
)
1092 esc_args
[esc_nargs
-1] = ARG_DEFAULT
;
1093 termstate
= SEEN_CSI
;
1097 if( esc_query
) esc_query
= -1;
1098 else if( c
== '?' ) esc_query
= TRUE
;
1100 termstate
= SEEN_CSI
;
1102 else switch (ANSI(c
,esc_query
)) {
1103 case 'A': /* move up N lines */
1104 move (curs_x
, curs_y
- def(esc_args
[0], 1), 1);
1105 seen_disp_event
= TRUE
;
1107 case 'e': /* move down N lines */
1108 compatibility(ANSI
);
1110 move (curs_x
, curs_y
+ def(esc_args
[0], 1), 1);
1111 seen_disp_event
= TRUE
;
1113 case 'a': /* move right N cols */
1114 compatibility(ANSI
);
1116 move (curs_x
+ def(esc_args
[0], 1), curs_y
, 1);
1117 seen_disp_event
= TRUE
;
1119 case 'D': /* move left N cols */
1120 move (curs_x
- def(esc_args
[0], 1), curs_y
, 1);
1121 seen_disp_event
= TRUE
;
1123 case 'E': /* move down N lines and CR */
1124 compatibility(ANSI
);
1125 move (0, curs_y
+ def(esc_args
[0], 1), 1);
1126 seen_disp_event
= TRUE
;
1128 case 'F': /* move up N lines and CR */
1129 compatibility(ANSI
);
1130 move (0, curs_y
- def(esc_args
[0], 1), 1);
1131 seen_disp_event
= TRUE
;
1133 case 'G': case '`': /* set horizontal posn */
1134 compatibility(ANSI
);
1135 move (def(esc_args
[0], 1) - 1, curs_y
, 0);
1136 seen_disp_event
= TRUE
;
1138 case 'd': /* set vertical posn */
1139 compatibility(ANSI
);
1140 move (curs_x
, (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
1142 seen_disp_event
= TRUE
;
1144 case 'H': case 'f': /* set horz and vert posns at once */
1146 esc_args
[1] = ARG_DEFAULT
;
1147 move (def(esc_args
[1], 1) - 1,
1148 (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
1150 seen_disp_event
= TRUE
;
1152 case 'J': /* erase screen or parts of it */
1154 unsigned int i
= def(esc_args
[0], 0) + 1;
1157 erase_lots(FALSE
, !!(i
& 2), !!(i
& 1));
1160 seen_disp_event
= TRUE
;
1162 case 'K': /* erase line or parts of it */
1164 unsigned int i
= def(esc_args
[0], 0) + 1;
1167 erase_lots(TRUE
, !!(i
& 2), !!(i
& 1));
1169 seen_disp_event
= TRUE
;
1171 case 'L': /* insert lines */
1172 compatibility(VT102
);
1173 if (curs_y
<= marg_b
)
1174 scroll (curs_y
, marg_b
, -def(esc_args
[0], 1), FALSE
);
1175 seen_disp_event
= TRUE
;
1177 case 'M': /* delete lines */
1178 compatibility(VT102
);
1179 if (curs_y
<= marg_b
)
1180 scroll (curs_y
, marg_b
, def(esc_args
[0], 1), TRUE
);
1181 seen_disp_event
= TRUE
;
1183 case '@': /* insert chars */
1184 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1185 compatibility(VT102
);
1186 insch (def(esc_args
[0], 1));
1187 seen_disp_event
= TRUE
;
1189 case 'P': /* delete chars */
1190 compatibility(VT102
);
1191 insch (-def(esc_args
[0], 1));
1192 seen_disp_event
= TRUE
;
1194 case 'c': /* terminal type query */
1195 compatibility(VT100
);
1196 /* This is the response for a VT102 */
1197 ldisc
->send (id_string
, strlen(id_string
));
1199 case 'n': /* cursor position query */
1200 if (esc_args
[0] == 6) {
1202 sprintf (buf
, "\033[%d;%dR", curs_y
+ 1, curs_x
+ 1);
1203 ldisc
->send (buf
, strlen(buf
));
1205 else if (esc_args
[0] == 5) {
1206 ldisc
->send ("\033[0n", 4);
1209 case 'h': /* toggle modes to high */
1211 compatibility(VT100
);
1214 for (i
=0; i
<esc_nargs
; i
++)
1215 toggle_mode (esc_args
[i
], esc_query
, TRUE
);
1218 case 'l': /* toggle modes to low */
1220 compatibility(VT100
);
1223 for (i
=0; i
<esc_nargs
; i
++)
1224 toggle_mode (esc_args
[i
], esc_query
, FALSE
);
1227 case 'g': /* clear tabs */
1228 compatibility(VT100
);
1229 if (esc_nargs
== 1) {
1230 if (esc_args
[0] == 0) {
1231 tabs
[curs_x
] = FALSE
;
1232 } else if (esc_args
[0] == 3) {
1234 for (i
= 0; i
< cols
; i
++)
1239 case 'r': /* set scroll margins */
1240 compatibility(VT100
);
1241 if (esc_nargs
<= 2) {
1243 top
= def(esc_args
[0], 1) - 1;
1244 bot
= (esc_nargs
<= 1 || esc_args
[1] == 0 ? rows
:
1245 def(esc_args
[1], rows
)) - 1;
1248 /* VTTEST Bug 9 - if region is less than 2 lines
1249 * don't change region.
1256 * I used to think the cursor should be
1257 * placed at the top of the newly marginned
1258 * area. Apparently not: VMS TPU falls over
1261 * Well actually it should for Origin mode - RDB
1263 curs_y
= (dec_om ? marg_t
: 0);
1265 seen_disp_event
= TRUE
;
1269 case 'm': /* set graphics rendition */
1272 * A VT100 without the AVO only had one attribute, either
1273 * underline or reverse video depending on the cursor type,
1274 * this was selected by CSI 7m.
1277 * This is DIM on the VT100-AVO and VT102
1279 * This is BLINK on the VT100-AVO and VT102+
1281 * This is INVIS on the VT100-AVO and VT102
1283 * This like 22 disables BOLD, DIM and INVIS
1285 * The ANSI colours appear on any terminal that has colour
1286 * (obviously) but the interaction between sgr0 and the
1287 * colours varies but is usually related to the background
1288 * colour erase item.
1289 * The interaction between colour attributes and the mono
1290 * ones is also very implementation dependent.
1292 * The 39 and 49 attributes are likely to be unimplemented.
1295 for (i
=0; i
<esc_nargs
; i
++) {
1296 switch (def(esc_args
[i
], 0)) {
1297 case 0: /* restore defaults */
1298 curr_attr
= ATTR_DEFAULT
; break;
1299 case 1: /* enable bold */
1300 compatibility(VT100AVO
);
1301 curr_attr
|= ATTR_BOLD
; break;
1302 case 21: /* (enable double underline) */
1303 compatibility(OTHER
);
1304 case 4: /* enable underline */
1305 compatibility(VT100AVO
);
1306 curr_attr
|= ATTR_UNDER
; break;
1307 case 5: /* enable blink */
1308 compatibility(VT100AVO
);
1309 curr_attr
|= ATTR_BLINK
; break;
1310 case 7: /* enable reverse video */
1311 curr_attr
|= ATTR_REVERSE
; break;
1312 case 22: /* disable bold */
1313 compatibility2(OTHER
,VT220
);
1314 curr_attr
&= ~ATTR_BOLD
; break;
1315 case 24: /* disable underline */
1316 compatibility2(OTHER
,VT220
);
1317 curr_attr
&= ~ATTR_UNDER
; break;
1318 case 25: /* disable blink */
1319 compatibility2(OTHER
,VT220
);
1320 curr_attr
&= ~ATTR_BLINK
; break;
1321 case 27: /* disable reverse video */
1322 compatibility2(OTHER
,VT220
);
1323 curr_attr
&= ~ATTR_REVERSE
; break;
1324 case 30: case 31: case 32: case 33:
1325 case 34: case 35: case 36: case 37:
1327 curr_attr
&= ~ATTR_FGMASK
;
1328 curr_attr
|= (esc_args
[i
] - 30) << ATTR_FGSHIFT
;
1330 case 39: /* default-foreground */
1331 curr_attr
&= ~ATTR_FGMASK
;
1332 curr_attr
|= ATTR_DEFFG
;
1334 case 40: case 41: case 42: case 43:
1335 case 44: case 45: case 46: case 47:
1337 curr_attr
&= ~ATTR_BGMASK
;
1338 curr_attr
|= (esc_args
[i
] - 40) << ATTR_BGSHIFT
;
1340 case 49: /* default-background */
1341 curr_attr
&= ~ATTR_BGMASK
;
1342 curr_attr
|= ATTR_DEFBG
;
1349 (curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
|ATTR_BLINK
))
1353 case 's': /* save cursor */
1356 case 'u': /* restore cursor */
1357 save_cursor (FALSE
);
1358 seen_disp_event
= TRUE
;
1360 case 't': /* set page size - ie window height */
1362 * VT340/VT420 sequence DECSLPP, DEC only allows values
1363 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1364 * illegal values (eg first arg 1..9) for window changing
1367 compatibility(VT340TEXT
);
1368 if (esc_nargs
<=1 && (esc_args
[0]<1 || esc_args
[0]>=24)) {
1369 request_resize (cols
, def(esc_args
[0], 24), 0);
1373 case ANSI('|', '*'):
1374 /* VT420 sequence DECSNLS
1375 * Set number of lines on screen
1376 * VT420 uses VGA like hardware and can support any size in
1377 * reasonable range (24..49 AIUI) with no default specified.
1379 compatibility(VT420
);
1380 if (esc_nargs
==1 && esc_args
[0]>0) {
1381 request_resize (cols
, def(esc_args
[0], cfg
.height
), 0);
1385 case ANSI('|', '$'):
1386 /* VT340/VT420 sequence DECSCPP
1387 * Set number of columns per page
1388 * Docs imply range is only 80 or 132, but I'll allow any.
1390 compatibility(VT340TEXT
);
1392 request_resize (def(esc_args
[0], cfg
.width
), rows
, 0);
1396 case 'X': /* write N spaces w/o moving cursor */
1397 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1398 compatibility(ANSIMIN
);
1400 int n
= def(esc_args
[0], 1);
1401 unsigned long *p
= cpos
;
1402 if (n
> cols
- curs_x
)
1404 check_selection (cpos
, cpos
+n
);
1407 seen_disp_event
= TRUE
;
1410 case 'x': /* report terminal characteristics */
1411 compatibility(VT100
);
1414 int i
= def(esc_args
[0], 0);
1415 if (i
== 0 || i
== 1) {
1416 strcpy (buf
, "\033[2;1;1;112;112;1;0x");
1418 ldisc
->send (buf
, 20);
1423 compatibility(OTHER
);
1424 use_bce
= (esc_args
[0]<=0);
1425 erase_char
= ERASE_CHAR
;
1427 erase_char
= (' '|(curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
)));
1430 compatibility(OTHER
);
1431 blink_is_real
= (esc_args
[0]>=1);
1434 /* Allow the host to make this emulator a 'perfect' VT102.
1435 * This first appeared in the VT220, but we do need to get
1436 * back to PuTTY mode so I won't check it.
1438 * The arg in 40..42 are a PuTTY extension.
1439 * The 2nd arg, 8bit vs 7bit is not checked.
1441 * Setting VT102 mode should also change the Fkeys to
1442 * generate PF* codes as a real VT102 has no Fkeys.
1443 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1446 * Note ESC c will NOT change this!
1449 switch (esc_args
[0]) {
1450 case 61: compatibility_level
&= ~TM_VTXXX
;
1451 compatibility_level
|= TM_VT102
; break;
1452 case 62: compatibility_level
&= ~TM_VTXXX
;
1453 compatibility_level
|= TM_VT220
; break;
1455 default: if( esc_args
[0] > 60 && esc_args
[0] < 70 )
1456 compatibility_level
|= TM_VTXXX
;
1459 case 40: compatibility_level
&= TM_VTXXX
; break;
1460 case 41: compatibility_level
= TM_PUTTY
; break;
1461 case 42: compatibility_level
= TM_SCOANSI
; break;
1464 compatibility_level
= TM_PUTTY
; break;
1468 /* Change the response to CSI c */
1469 if (esc_args
[0] == 50) {
1472 strcpy(id_string
, "\033[?");
1473 for (i
=1; i
<esc_nargs
; i
++) {
1474 if (i
!=1) strcat(id_string
, ";");
1475 sprintf(lbuf
, "%d", esc_args
[i
]);
1476 strcat(id_string
, lbuf
);
1478 strcat(id_string
, "c");
1482 /* Is this a good idea ?
1483 * Well we should do a soft reset at this point ...
1485 if (!has_compat(VT420
) && has_compat(VT100
)) {
1486 if (reset_132
) request_resize (132, 24, 1);
1487 else request_resize ( 80, 24, 1);
1495 /* VT100 only here, checked above */
1498 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_GBCHR
;
1501 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_LINEDRW
;
1504 default: /* specifically, 'B' */
1505 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_ASCII
;
1508 if( !has_compat(VT220
) || c
!= '%' )
1509 termstate
= TOPLEVEL
;
1514 case 'P': /* Linux palette sequence */
1515 termstate
= SEEN_OSC_P
;
1518 case 'R': /* Linux palette reset */
1521 termstate
= TOPLEVEL
;
1523 case 'W': /* word-set */
1524 termstate
= SEEN_OSC_W
;
1527 case '0': case '1': case '2': case '3': case '4':
1528 case '5': case '6': case '7': case '8': case '9':
1529 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1533 * Grotty hack to support xterm and DECterm title
1534 * sequences concurrently.
1536 if (esc_args
[0] == 2) {
1540 /* else fall through */
1542 termstate
= OSC_STRING
;
1548 * This OSC stuff is EVIL. It takes just one character to get into
1549 * sysline mode and it's not initially obvious how to get out.
1550 * So I've added CR and LF as string aborts.
1551 * This shouldn't effect compatibility as I believe embedded
1552 * control characters are supposed to be interpreted (maybe?)
1553 * and they don't display anything useful anyway.
1557 if (c
== '\n' || c
== '\r') {
1558 termstate
= TOPLEVEL
;
1559 } else if (c
== 0234 || c
== '\007' ) {
1561 * These characters terminate the string; ST and BEL
1562 * terminate the sequence and trigger instant
1563 * processing of it, whereas ESC goes back to SEEN_ESC
1564 * mode unless it is followed by \, in which case it is
1565 * synonymous with ST in the first place.
1568 termstate
= TOPLEVEL
;
1569 } else if (c
== '\033')
1570 termstate
= OSC_MAYBE_ST
;
1571 else if (osc_strlen
< OSC_STR_MAX
)
1572 osc_string
[osc_strlen
++] = c
;
1576 int max
= (osc_strlen
== 0 ?
21 : 16);
1578 if (c
>= '0' && c
<= '9')
1580 else if (c
>= 'A' && c
<= 'A'+max
-10)
1582 else if (c
>= 'a' && c
<= 'a'+max
-10)
1585 termstate
= TOPLEVEL
;
1586 osc_string
[osc_strlen
++] = val
;
1587 if (osc_strlen
>= 7) {
1588 palette_set (osc_string
[0],
1589 osc_string
[1] * 16 + osc_string
[2],
1590 osc_string
[3] * 16 + osc_string
[4],
1591 osc_string
[5] * 16 + osc_string
[6]);
1593 termstate
= TOPLEVEL
;
1599 case '0': case '1': case '2': case '3': case '4':
1600 case '5': case '6': case '7': case '8': case '9':
1601 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1604 termstate
= OSC_STRING
;
1611 unsigned long nlattr
;
1617 n
= rows
* (cols
+1);
1619 *p
++ = ATTR_DEFAULT
| 'E';
1621 seen_disp_event
= TRUE
;
1622 check_selection (scrtop
, scrtop
+ rows
* (cols
+1));
1625 case '3': nlattr
= LATTR_TOP
; if(0) {
1626 case '4': nlattr
= LATTR_BOT
; } if(0) {
1627 case '5': nlattr
= LATTR_NORM
; } if(0) {
1628 case '6': nlattr
= LATTR_WIDE
; }
1630 p
= scrtop
+ curs_y
* (cols
+1) + cols
;
1635 termstate
= TOPLEVEL
;
1638 termstate
= TOPLEVEL
;
1639 seen_disp_event
= TRUE
;
1642 move (curs_x
, curs_y
- 1, 1);
1645 move (curs_x
, curs_y
+ 1, 1);
1648 move (curs_x
+ 1, curs_y
, 1);
1651 move (curs_x
- 1, curs_y
, 1);
1654 cset_attr
[cset
=0] = ATTR_LINEDRW
;
1657 cset_attr
[cset
=0] = ATTR_ASCII
;
1664 scroll (0, rows
-1, -1, TRUE
);
1665 else if (curs_y
> 0)
1671 erase_lots(FALSE
, FALSE
, TRUE
);
1675 erase_lots(TRUE
, FALSE
, TRUE
);
1678 /* XXX Print cursor line */
1681 /* XXX Start controller mode */
1684 /* XXX Stop controller mode */
1687 termstate
= VT52_Y1
;
1690 ldisc
->send ("\033/Z", 3);
1693 app_keypad_keys
= TRUE
;
1696 app_keypad_keys
= FALSE
;
1699 /* XXX This should switch to VT100 mode not current or default
1700 * VT mode. But this will only have effect in a VT220+
1706 /* XXX Enter auto print mode */
1709 /* XXX Exit auto print mode */
1712 /* XXX Print screen */
1717 termstate
= VT52_Y2
;
1718 move(curs_x
, c
-' ', 0);
1721 termstate
= TOPLEVEL
;
1722 move(c
-' ', curs_y
, 0);
1725 if (selstate
!= NO_SELECTION
)
1726 check_selection (cpos
, cpos
+1);
1732 if(!beep_count
) beep_overload
=0;
1734 else if(beep_count
&& beep_count
<5 && cfg
.beep
)
1739 * Compare two lines to determine whether they are sufficiently
1740 * alike to scroll-optimise one to the other. Return the degree of
1743 static int linecmp (unsigned long *a
, unsigned long *b
) {
1746 for (i
=n
=0; i
< cols
; i
++)
1747 n
+= (*a
++ == *b
++);
1752 * Given a context, update the window. Out of paranoia, we don't
1753 * allow WM_PAINT responses to do scrolling optimisations.
1755 static void do_paint (Context ctx
, int may_optimise
){
1756 int i
, j
, start
, our_curs_y
;
1757 unsigned long attr
, rv
, cursor
;
1762 if (blinker
|| !cfg
.blink_cur
)
1763 cursor
= ATTR_ACTCURS
;
1768 cursor
= ATTR_PASCURS
;
1771 rv
= (rvideo ? ATTR_REVERSE
: 0);
1772 our_curs_y
= curs_y
+ (scrtop
- disptop
) / (cols
+1);
1774 for (i
=0; i
<rows
; i
++) {
1775 int idx
= i
*(cols
+1);
1776 int lattr
= (disptop
[idx
+cols
] & LATTR_MODE
);
1777 for (j
=0; j
<=cols
; j
++,idx
++) {
1778 unsigned long *d
= disptop
+idx
;
1779 wanttext
[idx
] = lattr
| ((*d
^ rv
1780 ^ (selstart
<= d
&& d
< selend ?
1781 ATTR_REVERSE
: 0)) |
1782 (i
==our_curs_y
&& j
==curs_x ? cursor
: 0));
1784 if (blink_is_real
) {
1785 if (has_focus
&& tblinker
&& (wanttext
[idx
]&ATTR_BLINK
) )
1787 wanttext
[idx
] &= ATTR_MASK
;
1788 wanttext
[idx
] += ' ';
1790 wanttext
[idx
] &= ~ATTR_BLINK
;
1796 * We would perform scrolling optimisations in here, if they
1797 * didn't have a nasty tendency to cause the whole sodding
1798 * program to hang for a second at speed-critical moments.
1799 * We'll leave it well alone...
1802 for (i
=0; i
<rows
; i
++) {
1803 int idx
= i
*(cols
+1);
1804 int lattr
= (wanttext
[idx
+cols
] & LATTR_MODE
);
1806 for (j
=0; j
<=cols
; j
++,idx
++) {
1807 unsigned long t
= wanttext
[idx
];
1808 int needs_update
= (j
< cols
&& t
!= disptext
[idx
]);
1809 int keep_going
= (start
!= -1 && needs_update
&&
1810 (t
& ATTR_MASK
) == attr
&&
1811 j
-start
< sizeof(ch
));
1812 if (start
!= -1 && !keep_going
) {
1813 do_text (ctx
, start
, i
, ch
, j
-start
, attr
, lattr
);
1819 attr
= t
& ATTR_MASK
;
1821 ch
[j
-start
] = (char) (t
& CHAR_MASK
);
1829 * Flick the switch that says if blinking things should be shown or hidden.
1832 void term_blink(int flg
) {
1833 static long last_blink
= 0;
1834 static long last_tblink
= 0;
1835 long now
, blink_diff
;
1837 now
= GetTickCount();
1838 blink_diff
= now
-last_tblink
;
1840 /* Make sure the text blinks no more than 2Hz */
1841 if (blink_diff
<0 || blink_diff
>450)
1844 tblinker
= !tblinker
;
1853 blink_diff
= now
-last_blink
;
1855 /* Make sure the cursor blinks no more than 2Hz */
1856 if (blink_diff
>=0 && blink_diff
<450)
1864 * Invalidate the whole screen so it will be repainted in full.
1866 void term_invalidate(void) {
1869 for (i
=0; i
<rows
*(cols
+1); i
++)
1870 disptext
[i
] = ATTR_INVALID
;
1874 * Paint the window in response to a WM_PAINT message.
1876 void term_paint (Context ctx
, int l
, int t
, int r
, int b
) {
1877 int i
, j
, left
, top
, right
, bottom
;
1879 left
= l
/ font_width
;
1880 right
= (r
- 1) / font_width
;
1881 top
= t
/ font_height
;
1882 bottom
= (b
- 1) / font_height
;
1883 for (i
= top
; i
<= bottom
&& i
< rows
; i
++)
1885 if ( (disptext
[i
*(cols
+1)+cols
]&LATTR_MODE
) == LATTR_NORM
)
1886 for (j
= left
; j
<= right
&& j
< cols
; j
++)
1887 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
1889 for (j
= left
/2; j
<= right
/2+1 && j
< cols
; j
++)
1890 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
1893 /* This should happen soon enough, also for some reason it sometimes
1894 * fails to actually do anything when re-sizing ... painting the wrong
1896 do_paint (ctx, FALSE);
1901 * Attempt to scroll the scrollback. The second parameter gives the
1902 * position we want to scroll to; the first is +1 to denote that
1903 * this position is relative to the beginning of the scrollback, -1
1904 * to denote it is relative to the end, and 0 to denote that it is
1905 * relative to the current position.
1907 void term_scroll (int rel
, int where
) {
1908 int n
= where
* (cols
+1);
1910 disptop
= (rel
< 0 ? scrtop
:
1911 rel
> 0 ? sbtop
: disptop
) + n
;
1912 if (disptop
< sbtop
)
1914 if (disptop
> scrtop
)
1921 * Spread the selection outwards according to the selection mode.
1923 static unsigned long *sel_spread_half (unsigned long *p
, int dir
) {
1924 unsigned long *linestart
, *lineend
;
1928 x
= (p
- text
) % (cols
+1);
1930 lineend
= linestart
+ cols
;
1935 * In this mode, every character is a separate unit, except
1936 * for runs of spaces at the end of a non-wrapping line.
1938 if (!(linestart
[cols
] & ATTR_WRAPPED
)) {
1939 unsigned long *q
= lineend
;
1940 while (q
> linestart
&& (q
[-1] & CHAR_MASK
) == 0x20)
1945 p
= (dir
== -1 ? q
: lineend
- 1);
1950 * In this mode, the units are maximal runs of characters
1951 * whose `wordness' has the same value.
1953 wvalue
= wordness
[*p
& CHAR_MASK
];
1955 while (p
< lineend
&& wordness
[p
[1] & CHAR_MASK
] == wvalue
)
1958 while (p
> linestart
&& wordness
[p
[-1] & CHAR_MASK
] == wvalue
)
1964 * In this mode, every line is a unit.
1966 p
= (dir
== -1 ? linestart
: lineend
- 1);
1972 static void sel_spread (void) {
1973 selstart
= sel_spread_half (selstart
, -1);
1974 selend
= sel_spread_half (selend
- 1, +1) + 1;
1977 void term_mouse (Mouse_Button b
, Mouse_Action a
, int x
, int y
) {
1978 unsigned long *selpoint
;
1981 if (y
>=rows
) y
= rows
-1;
1989 if (x
>=cols
) x
= cols
-1;
1991 selpoint
= disptop
+ y
* (cols
+1);
1992 if ((selpoint
[cols
]&LATTR_MODE
) != LATTR_NORM
)
1997 if (b
== MB_SELECT
&& a
== MA_CLICK
) {
1999 selstate
= ABOUT_TO
;
2000 selanchor
= selpoint
;
2002 } else if (b
== MB_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
2004 selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
2005 selstate
= DRAGGING
;
2006 selstart
= selanchor
= selpoint
;
2007 selend
= selstart
+ 1;
2009 } else if ((b
== MB_SELECT
&& a
== MA_DRAG
) ||
2010 (b
== MB_EXTEND
&& a
!= MA_RELEASE
)) {
2011 if (selstate
== ABOUT_TO
&& selanchor
== selpoint
)
2013 if (b
== MB_EXTEND
&& a
!= MA_DRAG
&& selstate
== SELECTED
) {
2014 if (selpoint
-selstart
< (selend
-selstart
)/2)
2015 selanchor
= selend
- 1;
2017 selanchor
= selstart
;
2018 selstate
= DRAGGING
;
2020 if (selstate
!= ABOUT_TO
&& selstate
!= DRAGGING
)
2021 selanchor
= selpoint
;
2022 selstate
= DRAGGING
;
2023 if (selpoint
< selanchor
) {
2024 selstart
= selpoint
;
2025 selend
= selanchor
+ 1;
2027 selstart
= selanchor
;
2028 selend
= selpoint
+ 1;
2031 } else if ((b
== MB_SELECT
|| b
== MB_EXTEND
) && a
== MA_RELEASE
) {
2032 if (selstate
== DRAGGING
) {
2034 * We've completed a selection. We now transfer the
2035 * data to the clipboard.
2037 unsigned char *p
= selspace
;
2038 unsigned long *q
= selstart
;
2040 while (q
< selend
) {
2042 unsigned long *lineend
= q
- (q
-text
) % (cols
+1) + cols
;
2043 unsigned long *nlpos
= lineend
;
2045 if (!(*nlpos
& ATTR_WRAPPED
)) {
2046 while ((nlpos
[-1] & CHAR_MASK
) == 0x20 && nlpos
> q
)
2051 while (q
< nlpos
&& q
< selend
)
2054 /* VT Specials -> ISO8859-1 */
2055 static const char poorman2
[] =
2056 "* # HTFFCRLF\xB0 \xB1 NLVT+ + + + + - - - - - + + + + | <=>=PI!=\xA3 \xB7 ";
2059 int ch
= (*q
& CHAR_MASK
);
2062 if ((*q
& ATTR_LINEDRW
) && ch
>= 0x60 && ch
< 0x7F) {
2064 *p
++ = poorman2
[2*(ch
-0x60)];
2065 if ( (x
= poorman2
[2*(ch
-0x60)+1]) != ' ')
2070 if ((*q
& ATTR_GBCHR
) && ch
== '#')
2071 *p
++ = (unsigned char) 0xA3;
2074 *p
++ = (unsigned char) ch
;
2079 for (i
=0; i
<sizeof(sel_nl
); i
++)
2082 q
= lineend
+ 1; /* start of next line */
2084 write_clip (selspace
, p
- selspace
, FALSE
);
2085 selstate
= SELECTED
;
2087 selstate
= NO_SELECTION
;
2088 } else if (b
== MB_PASTE
&& (a
==MA_CLICK
|| a
==MA_2CLK
|| a
==MA_3CLK
)) {
2092 get_clip((void **) &data
, &len
);
2096 if (paste_buffer
) sfree(paste_buffer
);
2097 paste_pos
= paste_hold
= paste_len
= 0;
2098 paste_buffer
= smalloc(len
);
2101 while (p
< data
+len
) {
2102 while (p
< data
+len
&&
2103 !(p
<= data
+len
-sizeof(sel_nl
) &&
2104 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
2112 c
=xlat_kbd2tty(q
[i
]);
2113 paste_buffer
[paste_len
++] = c
;
2117 if (p
<= data
+len
-sizeof(sel_nl
) &&
2118 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
2119 paste_buffer
[paste_len
++] = '\r';
2120 p
+= sizeof(sel_nl
);
2125 /* Assume a small paste will be OK in one go. */
2126 if (paste_len
<256) {
2127 ldisc
->send (paste_buffer
, paste_len
);
2128 if (paste_buffer
) sfree(paste_buffer
);
2130 paste_pos
= paste_hold
= paste_len
= 0;
2133 get_clip(NULL
, NULL
);
2139 void term_nopaste() {
2140 if(paste_len
== 0) return;
2141 sfree(paste_buffer
);
2147 static long last_paste
= 0;
2148 long now
, paste_diff
;
2150 if(paste_len
== 0) return;
2152 /* Don't wait forever to paste */
2154 now
= GetTickCount();
2155 paste_diff
= now
-last_paste
;
2156 if (paste_diff
>=0 && paste_diff
<450)
2161 while(paste_pos
<paste_len
)
2164 while (n
+ paste_pos
< paste_len
) {
2165 if (paste_buffer
[paste_pos
+ n
++] == '\r')
2168 ldisc
->send (paste_buffer
+paste_pos
, n
);
2171 if (paste_pos
< paste_len
) {
2176 sfree(paste_buffer
);
2181 static void deselect (void) {
2182 selstate
= NO_SELECTION
;
2183 selstart
= selend
= scrtop
;
2186 void term_deselect (void) {
2192 * from_backend(), to get data from the backend for the terminal.
2194 void from_backend(int is_stderr
, char *data
, int len
) {
2196 if (inbuf_head
>= INBUF_SIZE
)
2198 inbuf
[inbuf_head
++] = *data
++;