10 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
11 #define CL_VT100 0x0002 /* VT100 */
12 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
13 #define CL_VT102 0x0008 /* VT102 */
14 #define CL_VT220 0x0010 /* VT220 */
15 #define CL_VT320 0x0020 /* VT320 */
16 #define CL_VT420 0x0040 /* VT420 */
17 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
18 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
19 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
20 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
21 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
23 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
24 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
25 #define TM_VT102 (TM_VT100AVO|CL_VT102)
26 #define TM_VT220 (TM_VT102|CL_VT220)
27 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
28 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
30 #define TM_PUTTY (0xFFFF)
32 #define compatibility(x) \
33 if ( ((CL_##x)&compatibility_level) == 0 ) { \
37 #define compatibility2(x,y) \
38 if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
43 #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
45 static int compatibility_level
= TM_PUTTY
;
48 static unsigned long *text
; /* buffer of text on terminal screen */
49 static unsigned long *scrtop
; /* top of working screen */
50 static unsigned long *disptop
; /* top of displayed screen */
51 static unsigned long *sbtop
; /* top of scrollback */
52 static unsigned long *sbbot
; /* furthest extent of scrollback */
53 static unsigned long *cpos
; /* cursor position (convenience) */
54 static unsigned long *disptext
; /* buffer of text on real screen */
55 static unsigned long *wanttext
; /* buffer of text we want on screen */
56 static unsigned long *alttext
; /* buffer of text on alt. screen */
58 static unsigned char *selspace
; /* buffer for building selections in */
60 #define TSIZE (sizeof(*text))
61 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
63 static unsigned long curr_attr
, save_attr
;
64 static unsigned long erase_char
= ERASE_CHAR
;
66 static int curs_x
, curs_y
; /* cursor */
67 static int save_x
, save_y
; /* saved cursor position */
68 static int marg_t
, marg_b
; /* scroll margins */
69 static int dec_om
; /* DEC origin mode flag */
70 static int wrap
, wrapnext
; /* wrap flags */
71 static int insert
; /* insert-mode flag */
72 static int cset
; /* 0 or 1: which char set */
73 static int save_cset
, save_csattr
; /* saved with cursor position */
74 static int rvideo
; /* global reverse video flag */
75 static int cursor_on
; /* cursor enabled flag */
76 static int reset_132
; /* Flag ESC c resets to 80 cols */
77 static int use_bce
; /* Use Background coloured erase */
78 static int blinker
; /* When blinking is the cursor on ? */
79 static int tblinker
; /* When the blinking text is on */
80 static int blink_is_real
; /* Actually blink blinking text */
82 static unsigned long cset_attr
[2];
85 * Saved settings on the alternate screen.
87 static int alt_x
, alt_y
, alt_om
, alt_wrap
, alt_wnext
, alt_ins
, alt_cset
;
88 static int alt_t
, alt_b
;
91 #define ARGS_MAX 32 /* max # of esc sequence arguments */
92 #define ARG_DEFAULT 0 /* if an arg isn't specified */
93 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
94 static int esc_args
[ARGS_MAX
];
97 #define ANSI(x,y) ((x)+((y)<<8))
98 #define ANSI_QUE(x) ANSI(x,TRUE)
100 #define OSC_STR_MAX 2048
101 static int osc_strlen
;
102 static char osc_string
[OSC_STR_MAX
+1];
105 static char id_string
[1024] = "\033[?6c";
107 static unsigned char *tabs
;
121 OSC_STRING
, OSC_MAYBE_ST
,
129 NO_SELECTION
, ABOUT_TO
, DRAGGING
, SELECTED
132 SM_CHAR
, SM_WORD
, SM_LINE
134 static unsigned long *selstart
, *selend
, *selanchor
;
136 static short wordness
[256] = {
137 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 */
138 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 */
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,2, /* 45 */
140 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 */
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, /* 89 */
142 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 */
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, /* CD */
144 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 */
147 static unsigned char sel_nl
[] = SEL_NL
;
148 static char * paste_buffer
= 0;
149 static int paste_len
, paste_pos
, paste_hold
;
152 * Internal prototypes.
154 static void do_paint (Context
, int);
155 static void erase_lots (int, int, int);
156 static void swap_screen (int);
157 static void update_sbar (void);
158 static void deselect (void);
159 /* log session to file stuff ... */
160 static FILE *lgfp
= NULL
;
161 static void logtraffic(unsigned char c
, int logmode
);
164 * Set up power-on settings for the terminal.
166 static void power_on(void) {
167 curs_x
= curs_y
= alt_x
= alt_y
= save_x
= save_y
= 0;
170 alt_b
= marg_b
= rows
- 1;
175 for (i
= 0; i
< cols
; i
++)
176 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
178 alt_om
= dec_om
= cfg
.dec_om
;
179 alt_wnext
= wrapnext
= alt_ins
= insert
= FALSE
;
180 alt_wrap
= wrap
= cfg
.wrap_mode
;
182 cset_attr
[0] = cset_attr
[1] = ATTR_ASCII
;
185 save_attr
= curr_attr
= ATTR_DEFAULT
;
186 app_cursor_keys
= cfg
.app_cursor
;
187 app_keypad_keys
= cfg
.app_keypad
;
189 blink_is_real
= cfg
.blinktext
;
190 erase_char
= ERASE_CHAR
;
194 for (i
= 0; i
< 256; i
++)
195 wordness
[i
] = cfg
.wordness
[i
];
199 erase_lots (FALSE
, TRUE
, TRUE
);
201 erase_lots (FALSE
, TRUE
, TRUE
);
206 * Force a screen update.
208 void term_update(void) {
212 if ( (seen_key_event
&& (cfg
.scroll_on_key
)) ||
213 (seen_disp_event
&& (cfg
.scroll_on_disp
)) ) {
215 seen_disp_event
= seen_key_event
= 0;
218 do_paint (ctx
, TRUE
);
219 sys_cursor(curs_x
, curs_y
+ (scrtop
- disptop
) / (cols
+1));
225 * Same as power_on(), but an external function.
227 void term_pwron(void) {
236 * Clear the scrollback.
238 void term_clrsb(void) {
239 disptop
= sbtop
= scrtop
;
244 * Initialise the terminal.
246 void term_init(void) {
247 text
= sbtop
= sbbot
= scrtop
= disptop
= cpos
= NULL
;
248 disptext
= wanttext
= NULL
;
257 * Set up the terminal for a given size.
259 void term_size(int newrows
, int newcols
, int newsavelines
) {
260 unsigned long *newtext
, *newdisp
, *newwant
, *newalt
;
261 int i
, j
, crows
, ccols
;
263 int save_alt_which
= alt_which
;
265 if (newrows
== rows
&& newcols
== cols
&& newsavelines
== savelines
)
266 return; /* nothing to do */
272 alt_b
= marg_b
= newrows
- 1;
274 newtext
= smalloc ((newrows
+newsavelines
)*(newcols
+1)*TSIZE
);
275 sbbot
= newtext
+ newsavelines
*(newcols
+1);
276 for (i
=0; i
<(newrows
+newsavelines
)*(newcols
+1); i
++)
277 newtext
[i
] = erase_char
;
279 crows
= rows
+ (scrtop
- sbtop
) / (cols
+1);
280 if (crows
> newrows
+newsavelines
)
281 crows
= newrows
+newsavelines
;
285 disptop
= newtext
+ (crows
-newrows
)*(newcols
+1);
286 ccols
= (cols
< newcols ? cols
: newcols
);
287 for (i
=0; i
<crows
; i
++) {
288 int oldidx
= (rows
- crows
+ i
) * (cols
+1);
289 int newidx
= (newrows
- crows
+ i
) * (newcols
+1);
291 for (j
=0; j
<ccols
; j
++)
292 disptop
[newidx
+j
] = scrtop
[oldidx
+j
];
293 disptop
[newidx
+newcols
] =
294 (cols
== newcols ? scrtop
[oldidx
+cols
]
295 : (scrtop
[oldidx
+cols
]&LATTR_MODE
));
299 sbtop
= disptop
= newtext
;
305 newdisp
= smalloc (newrows
*(newcols
+1)*TSIZE
);
306 for (i
=0; i
<newrows
*(newcols
+1); i
++)
307 newdisp
[i
] = ATTR_INVALID
;
311 newwant
= smalloc (newrows
*(newcols
+1)*TSIZE
);
312 for (i
=0; i
<newrows
*(newcols
+1); i
++)
313 newwant
[i
] = ATTR_INVALID
;
317 newalt
= smalloc (newrows
*(newcols
+1)*TSIZE
);
318 for (i
=0; i
<newrows
*(newcols
+1); i
++)
319 newalt
[i
] = erase_char
;
324 selspace
= smalloc ( (newrows
+newsavelines
) * (newcols
+sizeof(sel_nl
)) );
326 tabs
= srealloc (tabs
, newcols
*sizeof(*tabs
));
329 for (i
= (cols
> 0 ? cols
: 0); i
< newcols
; i
++)
330 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
334 curs_y
+= newrows
- rows
;
337 if (curs_y
>= newrows
)
339 if (curs_x
>= newcols
)
342 wrapnext
= alt_wnext
= FALSE
;
346 savelines
= newsavelines
;
349 swap_screen(save_alt_which
);
358 static void swap_screen (int which
) {
362 if (which
== alt_which
)
367 for (t
=0; t
<rows
*(cols
+1); t
++) {
368 tt
= scrtop
[t
]; scrtop
[t
] = alttext
[t
]; alttext
[t
] = tt
;
371 t
= curs_x
; curs_x
= alt_x
; alt_x
= t
;
372 t
= curs_y
; curs_y
= alt_y
; alt_y
= t
;
373 t
= marg_t
; marg_t
= alt_t
; alt_t
= t
;
374 t
= marg_b
; marg_b
= alt_b
; alt_b
= t
;
375 t
= dec_om
; dec_om
= alt_om
; alt_om
= t
;
376 t
= wrap
; wrap
= alt_wrap
; alt_wrap
= t
;
377 t
= wrapnext
; wrapnext
= alt_wnext
; alt_wnext
= t
;
378 t
= insert
; insert
= alt_ins
; alt_ins
= t
;
379 t
= cset
; cset
= alt_cset
; alt_cset
= t
;
385 * Update the scroll bar.
387 static void update_sbar(void) {
390 min
= (sbtop
- text
) / (cols
+1);
391 set_sbar ((scrtop
- text
) / (cols
+1) + rows
- min
,
392 (disptop
- text
) / (cols
+1) - min
,
397 * Check whether the region bounded by the two pointers intersects
398 * the scroll region, and de-select the on-screen selection if so.
400 static void check_selection (unsigned long *from
, unsigned long *to
) {
401 if (from
< selend
&& selstart
< to
)
406 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
407 * for backward.) `sb' is TRUE if the scrolling is permitted to
408 * affect the scrollback buffer.
410 static void scroll (int topline
, int botline
, int lines
, int sb
) {
411 unsigned long *scroll_top
;
412 unsigned long *newscr
;
413 int scroll_size
, size
, i
;
414 int scrtop_is_disptop
= (scrtop
==disptop
);
415 static int recursive
= 0;
417 /* Only scroll more than the window if we're doing a 10% scroll */
418 if (!recursive
&& lines
> botline
- topline
+ 1)
419 lines
= botline
- topline
+ 1;
421 scroll_top
= scrtop
+ topline
*(cols
+1);
422 size
= (lines
< 0 ?
-lines
: lines
) * (cols
+1);
423 scroll_size
= (botline
- topline
+ 1) * (cols
+1) - size
;
425 if (lines
> 0 && topline
== 0 && alt_which
== 0 && sb
) {
427 * Since we're going to scroll the top line and we're on the
428 * scrolling screen let's also effect the scrollback buffer.
430 * This is normally done by moving the position the screen
431 * painter reads from to reduce the amount of memory copying
434 if (scroll_size
>= 0 && !recursive
) {
435 newscr
= scrtop
+ lines
* (cols
+1);
437 if (newscr
> sbbot
&& botline
== rows
-1) {
438 /* We've hit the bottom of memory, so we have to do a
439 * physical scroll. But instead of just 1 line do it
440 * by 10% of the available memory.
442 * If the scroll region isn't the whole screen then we can't
443 * do this as it stands. We would need to recover the bottom
444 * of the screen from the scroll buffer after being sure that
445 * it doesn't get deleted.
448 i
= (rows
+savelines
)/10;
450 /* Make it simple and ensure safe recursion */
451 if ( i
<savelines
-1) {
453 scroll(topline
, botline
, i
, sb
);
456 newscr
= scrtop
- i
* (cols
+1);
457 if (scrtop_is_disptop
) disptop
= newscr
;
461 newscr
= scrtop
+ lines
* (cols
+1);
464 if (newscr
<= sbbot
) {
465 if (scrtop_is_disptop
) disptop
= newscr
;
468 if (botline
== rows
-1 )
469 for (i
= 0; i
< size
; i
++)
470 scrtop
[i
+scroll_size
] = erase_char
;
475 if (botline
!= rows
-1) {
476 /* This fastscroll only works for full window scrolls.
477 * If the caller wanted a partial one we have to reverse
478 * scroll the bottom of the screen.
480 scroll(botline
-lines
+1, rows
-1, -lines
, 0);
485 /* If we can't scroll by memory remapping do it physically.
486 * But rather than expensivly doing the scroll buffer just
487 * scroll the screen. All it means is that sometimes we choose
488 * to not add lines from a scroll region to the scroll buffer.
491 if (savelines
<= 400) {
492 sbtop
-= lines
* (cols
+1);
495 scroll_size
+= scroll_top
- sbtop
;
501 /* Ho hum, expensive scroll required. */
503 sbtop
-= lines
* (cols
+1);
506 scroll_size
+= scroll_top
- sbtop
;
513 if (scroll_size
< 0) {
520 memmove (scroll_top
, scroll_top
+ size
, scroll_size
*TSIZE
);
521 for (i
= 0; i
< size
; i
++)
522 scroll_top
[i
+scroll_size
] = erase_char
;
523 if (selstart
> scroll_top
&&
524 selstart
< scroll_top
+ size
+ scroll_size
) {
526 if (selstart
< scroll_top
)
527 selstart
= scroll_top
;
529 if (selend
> scroll_top
&&
530 selend
< scroll_top
+ size
+ scroll_size
) {
532 if (selend
< scroll_top
)
535 if (scrtop_is_disptop
)
538 if (disptop
> scroll_top
&&
539 disptop
< scroll_top
+ size
+ scroll_size
) {
541 if (disptop
< scroll_top
)
542 disptop
= scroll_top
;
546 memmove (scroll_top
+ size
, scroll_top
, scroll_size
*TSIZE
);
547 for (i
= 0; i
< size
; i
++)
548 scroll_top
[i
] = erase_char
;
549 if (selstart
> scroll_top
&&
550 selstart
< scroll_top
+ size
+ scroll_size
) {
552 if (selstart
> scroll_top
+ size
+ scroll_size
)
553 selstart
= scroll_top
+ size
+ scroll_size
;
555 if (selend
> scroll_top
&&
556 selend
< scroll_top
+ size
+ scroll_size
) {
558 if (selend
> scroll_top
+ size
+ scroll_size
)
559 selend
= scroll_top
+ size
+ scroll_size
;
561 if (scrtop_is_disptop
)
563 else if (disptop
> scroll_top
&&
564 disptop
< scroll_top
+ size
+ scroll_size
) {
566 if (disptop
> scroll_top
+ size
+ scroll_size
)
567 disptop
= scroll_top
+ size
+ scroll_size
;
573 * Move the cursor to a given position, clipping at boundaries. We
574 * may or may not want to clip at the scroll margin: marg_clip is 0
575 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
576 * even _being_ outside the margins.
578 static void move (int x
, int y
, int marg_clip
) {
584 if ((curs_y
>= marg_t
|| marg_clip
== 2) && y
< marg_t
)
586 if ((curs_y
<= marg_b
|| marg_clip
== 2) && y
> marg_b
)
600 * Save or restore the cursor and SGR mode.
602 static void save_cursor(int save
) {
606 save_attr
= curr_attr
;
608 save_csattr
= cset_attr
[cset
];
612 /* Make sure the window hasn't shrunk since the save */
613 if (curs_x
>= cols
) curs_x
= cols
-1;
614 if (curs_y
>= rows
) curs_y
= rows
-1;
616 curr_attr
= save_attr
;
618 cset_attr
[cset
] = save_csattr
;
620 if (use_bce
) erase_char
= (' ' |(curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
)));
625 * Erase a large portion of the screen: the whole screen, or the
626 * whole line, or parts thereof.
628 static void erase_lots (int line_only
, int from_begin
, int to_end
) {
629 unsigned long *startpos
, *endpos
;
632 startpos
= cpos
- curs_x
;
633 endpos
= startpos
+ cols
;
634 /* I've removed the +1 so that the Wide screen stuff is not
635 * removed when it shouldn't be.
639 endpos
= startpos
+ rows
* (cols
+1);
645 check_selection (startpos
, endpos
);
647 /* Clear screen also forces a full window redraw, just in case. */
648 if (startpos
== scrtop
&& endpos
== scrtop
+ rows
* (cols
+1))
651 while (startpos
< endpos
)
652 *startpos
++ = erase_char
;
656 * Insert or delete characters within the current line. n is +ve if
657 * insertion is desired, and -ve for deletion.
659 static void insch (int n
) {
660 int dir
= (n
< 0 ?
-1 : +1);
663 n
= (n
< 0 ?
-n
: n
);
664 if (n
> cols
- curs_x
)
666 m
= cols
- curs_x
- n
;
667 check_selection (cpos
, cpos
+n
);
669 memmove (cpos
, cpos
+n
, m
*TSIZE
);
671 cpos
[m
++] = erase_char
;
673 memmove (cpos
+n
, cpos
, m
*TSIZE
);
675 cpos
[n
] = erase_char
;
680 * Toggle terminal mode `mode' to state `state'. (`query' indicates
681 * whether the mode is a DEC private one or a normal one.)
683 static void toggle_mode (int mode
, int query
, int state
) {
684 if (query
) switch (mode
) {
685 case 1: /* application cursor keys */
686 app_cursor_keys
= state
;
688 case 2: /* VT52 mode */
691 case 3: /* 80/132 columns */
693 request_resize (state ?
132 : 80, rows
, 1);
696 case 5: /* reverse video */
698 seen_disp_event
= TRUE
;
699 if (state
) term_update();
701 case 6: /* DEC origin mode */
704 case 7: /* auto wrap */
707 case 8: /* auto key repeat */
710 case 25: /* enable/disable cursor */
711 compatibility2(OTHER
,VT220
);
713 seen_disp_event
= TRUE
;
715 case 47: /* alternate screen */
716 compatibility(OTHER
);
721 } else switch (mode
) {
722 case 4: /* set insert mode */
723 compatibility(VT102
);
726 case 12: /* set echo mode */
728 * This may be very good in smcup and rmcup (or smkx & rmkx) if you
729 * have a long RTT and the telnet client/daemon doesn't understand
732 * DONT send TS_RECHO/TS_LECHO; the telnet daemon tries to fix the
733 * tty and _really_ confuses some programs.
735 compatibility2(OTHER
,VT220
);
736 ldisc
= (state?
&ldisc_simple
: &ldisc_term
);
738 case 20: /* Return sends ... */
739 cr_lf_return
= state
;
745 * Process an OSC sequence: set window title or icon name.
747 static void do_osc(void) {
750 wordness
[(unsigned char)osc_string
[osc_strlen
]] = esc_args
[0];
752 osc_string
[osc_strlen
] = '\0';
753 switch (esc_args
[0]) {
756 set_icon (osc_string
);
757 if (esc_args
[0] == 1)
759 /* fall through: parameter 0 means set both */
762 set_title (osc_string
);
769 * Remove everything currently in `inbuf' and stick it up on the
770 * in-memory display. There's a big state machine in here to
771 * process escape sequences...
773 void term_out(void) {
776 static int beep_overload
= 0;
779 for(inbuf_reap
= 0; inbuf_reap
< inbuf_head
; inbuf_reap
++)
781 c
= inbuf
[inbuf_reap
];
784 * Optionally log the session traffic to a file. Useful for
785 * debugging and possibly also useful for actual logging.
787 logtraffic((unsigned char)c
, LGTYP_DEBUG
);
789 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
790 * be able to display 8-bit characters, but I'll let that go 'cause
793 if( ( (c
&0x60) == 0 || c
== '\177') &&
794 termstate
< DO_CTRLS
&&
795 ( (c
&0x80) == 0 || has_compat(VT220
))) {
797 case '\005': /* terminal type query */
798 /* Strictly speaking this is VT100 but a VT100 defaults to
799 * no response. Other terminals respond at their option.
801 * Don't put a CR in the default string as this tends to
802 * upset some weird software.
804 * An xterm returns "xterm" (5 characters)
806 compatibility(OTHER
);
807 ldisc
->send ("PuTTY", 5);
811 if(beep_count
>6) beep_overload
=1;
815 if (curs_x
== 0 && curs_y
== 0)
817 else if (curs_x
== 0 && curs_y
> 0)
818 curs_x
= cols
-1, curs_y
--;
824 seen_disp_event
= TRUE
;
827 compatibility(VT100
);
831 compatibility(VT100
);
836 termstate
= VT52_ESC
;
838 compatibility(ANSIMIN
);
839 termstate
= SEEN_ESC
;
843 compatibility(VT220
);
844 termstate
= SEEN_CSI
;
846 esc_args
[0] = ARG_DEFAULT
;
850 compatibility(VT220
);
851 termstate
= SEEN_OSC
;
858 seen_disp_event
= TRUE
;
860 logtraffic((unsigned char)c
,LGTYP_ASCII
);
864 compatibility(VT100
);
866 if (curs_y
== marg_b
)
867 scroll (marg_t
, marg_b
, 1, TRUE
);
868 else if (curs_y
< rows
-1)
876 logtraffic((unsigned char)c
,LGTYP_ASCII
);
880 unsigned long *old_cpos
= cpos
;
881 unsigned long *p
= scrtop
+ curs_y
* (cols
+1) + cols
;
885 } while (curs_x
< cols
-1 && !tabs
[curs_x
]);
887 if ((*p
& LATTR_MODE
) != LATTR_NORM
)
889 if (curs_x
>= cols
/2)
899 check_selection (old_cpos
, cpos
);
901 seen_disp_event
= TRUE
;
903 case '\177': /* Destructive backspace
904 This does nothing on a real VT100 */
905 compatibility(OTHER
);
906 if (curs_x
&& !wrapnext
) curs_x
--;
909 *cpos
= (' ' | curr_attr
| ATTR_ASCII
);
913 else switch (termstate
) {
915 /* Only graphic characters get this far, ctrls are stripped above */
916 if (wrapnext
&& wrap
) {
917 cpos
[1] |= ATTR_WRAPPED
;
918 if (curs_y
== marg_b
)
919 scroll (marg_t
, marg_b
, 1, TRUE
);
920 else if (curs_y
< rows
-1)
928 if (selstate
!= NO_SELECTION
)
929 check_selection (cpos
, cpos
+1);
930 switch (cset_attr
[cset
]) {
931 /* Linedraw characters are different from 'ESC ( B' only
932 * for a small range, for ones outside that range make sure
933 * we use the same font as well as the same encoding.
936 if (c
<0x5f || c
>0x7F)
937 *cpos
++ = xlat_tty2scr((unsigned char)c
) | curr_attr
|
940 *cpos
++ = ' ' | curr_attr
| ATTR_ASCII
;
942 *cpos
++ = ((unsigned char)c
) | curr_attr
| ATTR_LINEDRW
;
945 /* If UK-ASCII, make the '#' a LineDraw Pound */
947 *cpos
++ = '}' | curr_attr
| ATTR_LINEDRW
;
952 *cpos
= xlat_tty2scr((unsigned char)c
) | curr_attr
|
953 (c
<= 0x7F ? cset_attr
[cset
] : ATTR_ASCII
);
954 logtraffic((unsigned char)c
, LGTYP_ASCII
);
959 if (curs_x
== cols
) {
968 termstate
= TOPLEVEL
;
972 * This state is virtually identical to SEEN_ESC, with the
973 * exception that we have an OSC sequence in the pipeline,
974 * and _if_ we see a backslash, we process it.
978 termstate
= TOPLEVEL
;
981 /* else fall through */
983 termstate
= TOPLEVEL
;
985 case ' ': /* some weird sequence? */
986 compatibility(VT220
);
987 termstate
= IGNORE_NEXT
;
989 case '[': /* enter CSI mode */
990 termstate
= SEEN_CSI
;
992 esc_args
[0] = ARG_DEFAULT
;
995 case ']': /* xterm escape sequences */
996 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
997 compatibility(OTHER
);
998 termstate
= SEEN_OSC
;
1001 case '(': /* should set GL */
1002 compatibility(VT100
);
1005 case ')': /* should set GR */
1006 compatibility(VT100
);
1009 case '7': /* save cursor */
1010 compatibility(VT100
);
1013 case '8': /* restore cursor */
1014 compatibility(VT100
);
1015 save_cursor (FALSE
);
1016 seen_disp_event
= TRUE
;
1019 compatibility(VT100
);
1020 app_keypad_keys
= TRUE
;
1023 compatibility(VT100
);
1024 app_keypad_keys
= FALSE
;
1026 case 'D': /* exactly equivalent to LF */
1027 compatibility(VT100
);
1028 if (curs_y
== marg_b
)
1029 scroll (marg_t
, marg_b
, 1, TRUE
);
1030 else if (curs_y
< rows
-1)
1034 seen_disp_event
= TRUE
;
1036 case 'E': /* exactly equivalent to CR-LF */
1037 compatibility(VT100
);
1039 if (curs_y
== marg_b
)
1040 scroll (marg_t
, marg_b
, 1, TRUE
);
1041 else if (curs_y
< rows
-1)
1045 seen_disp_event
= TRUE
;
1047 case 'M': /* reverse index - backwards LF */
1048 compatibility(VT100
);
1049 if (curs_y
== marg_t
)
1050 scroll (marg_t
, marg_b
, -1, TRUE
);
1051 else if (curs_y
> 0)
1055 seen_disp_event
= TRUE
;
1057 case 'Z': /* terminal type query */
1058 compatibility(VT100
);
1059 ldisc
->send (id_string
, strlen(id_string
));
1061 case 'c': /* restore power-on settings */
1062 compatibility(VT100
);
1065 request_resize (80, rows
, 1);
1070 seen_disp_event
= TRUE
;
1072 case '#': /* ESC # 8 fills screen with Es :-) */
1073 compatibility(VT100
);
1074 termstate
= SEEN_ESCHASH
;
1076 case 'H': /* set a tab */
1077 compatibility(VT100
);
1078 tabs
[curs_x
] = TRUE
;
1083 termstate
= TOPLEVEL
; /* default */
1086 if (esc_nargs
<= ARGS_MAX
) {
1087 if (esc_args
[esc_nargs
-1] == ARG_DEFAULT
)
1088 esc_args
[esc_nargs
-1] = 0;
1089 esc_args
[esc_nargs
-1] =
1090 10 * esc_args
[esc_nargs
-1] + c
- '0';
1092 termstate
= SEEN_CSI
;
1096 if (++esc_nargs
<= ARGS_MAX
)
1097 esc_args
[esc_nargs
-1] = ARG_DEFAULT
;
1098 termstate
= SEEN_CSI
;
1102 if( esc_query
) esc_query
= -1;
1103 else if( c
== '?' ) esc_query
= TRUE
;
1105 termstate
= SEEN_CSI
;
1107 else switch (ANSI(c
,esc_query
)) {
1108 case 'A': /* move up N lines */
1109 move (curs_x
, curs_y
- def(esc_args
[0], 1), 1);
1110 seen_disp_event
= TRUE
;
1112 case 'e': /* move down N lines */
1113 compatibility(ANSI
);
1115 move (curs_x
, curs_y
+ def(esc_args
[0], 1), 1);
1116 seen_disp_event
= TRUE
;
1118 case 'a': /* move right N cols */
1119 compatibility(ANSI
);
1121 move (curs_x
+ def(esc_args
[0], 1), curs_y
, 1);
1122 seen_disp_event
= TRUE
;
1124 case 'D': /* move left N cols */
1125 move (curs_x
- def(esc_args
[0], 1), curs_y
, 1);
1126 seen_disp_event
= TRUE
;
1128 case 'E': /* move down 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 'F': /* move up N lines and CR */
1134 compatibility(ANSI
);
1135 move (0, curs_y
- def(esc_args
[0], 1), 1);
1136 seen_disp_event
= TRUE
;
1138 case 'G': case '`': /* set horizontal posn */
1139 compatibility(ANSI
);
1140 move (def(esc_args
[0], 1) - 1, curs_y
, 0);
1141 seen_disp_event
= TRUE
;
1143 case 'd': /* set vertical posn */
1144 compatibility(ANSI
);
1145 move (curs_x
, (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
1147 seen_disp_event
= TRUE
;
1149 case 'H': case 'f': /* set horz and vert posns at once */
1151 esc_args
[1] = ARG_DEFAULT
;
1152 move (def(esc_args
[1], 1) - 1,
1153 (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
1155 seen_disp_event
= TRUE
;
1157 case 'J': /* erase screen or parts of it */
1159 unsigned int i
= def(esc_args
[0], 0) + 1;
1162 erase_lots(FALSE
, !!(i
& 2), !!(i
& 1));
1165 seen_disp_event
= TRUE
;
1167 case 'K': /* erase line or parts of it */
1169 unsigned int i
= def(esc_args
[0], 0) + 1;
1172 erase_lots(TRUE
, !!(i
& 2), !!(i
& 1));
1174 seen_disp_event
= TRUE
;
1176 case 'L': /* insert lines */
1177 compatibility(VT102
);
1178 if (curs_y
<= marg_b
)
1179 scroll (curs_y
, marg_b
, -def(esc_args
[0], 1), FALSE
);
1180 seen_disp_event
= TRUE
;
1182 case 'M': /* delete lines */
1183 compatibility(VT102
);
1184 if (curs_y
<= marg_b
)
1185 scroll (curs_y
, marg_b
, def(esc_args
[0], 1), TRUE
);
1186 seen_disp_event
= TRUE
;
1188 case '@': /* insert chars */
1189 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1190 compatibility(VT102
);
1191 insch (def(esc_args
[0], 1));
1192 seen_disp_event
= TRUE
;
1194 case 'P': /* delete chars */
1195 compatibility(VT102
);
1196 insch (-def(esc_args
[0], 1));
1197 seen_disp_event
= TRUE
;
1199 case 'c': /* terminal type query */
1200 compatibility(VT100
);
1201 /* This is the response for a VT102 */
1202 ldisc
->send (id_string
, strlen(id_string
));
1204 case 'n': /* cursor position query */
1205 if (esc_args
[0] == 6) {
1207 sprintf (buf
, "\033[%d;%dR", curs_y
+ 1, curs_x
+ 1);
1208 ldisc
->send (buf
, strlen(buf
));
1210 else if (esc_args
[0] == 5) {
1211 ldisc
->send ("\033[0n", 4);
1214 case 'h': /* toggle modes to high */
1216 compatibility(VT100
);
1219 for (i
=0; i
<esc_nargs
; i
++)
1220 toggle_mode (esc_args
[i
], esc_query
, TRUE
);
1223 case 'l': /* toggle modes to low */
1225 compatibility(VT100
);
1228 for (i
=0; i
<esc_nargs
; i
++)
1229 toggle_mode (esc_args
[i
], esc_query
, FALSE
);
1232 case 'g': /* clear tabs */
1233 compatibility(VT100
);
1234 if (esc_nargs
== 1) {
1235 if (esc_args
[0] == 0) {
1236 tabs
[curs_x
] = FALSE
;
1237 } else if (esc_args
[0] == 3) {
1239 for (i
= 0; i
< cols
; i
++)
1244 case 'r': /* set scroll margins */
1245 compatibility(VT100
);
1246 if (esc_nargs
<= 2) {
1248 top
= def(esc_args
[0], 1) - 1;
1249 bot
= (esc_nargs
<= 1 || esc_args
[1] == 0 ? rows
:
1250 def(esc_args
[1], rows
)) - 1;
1253 /* VTTEST Bug 9 - if region is less than 2 lines
1254 * don't change region.
1261 * I used to think the cursor should be
1262 * placed at the top of the newly marginned
1263 * area. Apparently not: VMS TPU falls over
1266 * Well actually it should for Origin mode - RDB
1268 curs_y
= (dec_om ? marg_t
: 0);
1270 seen_disp_event
= TRUE
;
1274 case 'm': /* set graphics rendition */
1277 * A VT100 without the AVO only had one attribute, either
1278 * underline or reverse video depending on the cursor type,
1279 * this was selected by CSI 7m.
1282 * This is DIM on the VT100-AVO and VT102
1284 * This is BLINK on the VT100-AVO and VT102+
1286 * This is INVIS on the VT100-AVO and VT102
1288 * This like 22 disables BOLD, DIM and INVIS
1290 * The ANSI colours appear on any terminal that has colour
1291 * (obviously) but the interaction between sgr0 and the
1292 * colours varies but is usually related to the background
1293 * colour erase item.
1294 * The interaction between colour attributes and the mono
1295 * ones is also very implementation dependent.
1297 * The 39 and 49 attributes are likely to be unimplemented.
1300 for (i
=0; i
<esc_nargs
; i
++) {
1301 switch (def(esc_args
[i
], 0)) {
1302 case 0: /* restore defaults */
1303 curr_attr
= ATTR_DEFAULT
; break;
1304 case 1: /* enable bold */
1305 compatibility(VT100AVO
);
1306 curr_attr
|= ATTR_BOLD
; break;
1307 case 21: /* (enable double underline) */
1308 compatibility(OTHER
);
1309 case 4: /* enable underline */
1310 compatibility(VT100AVO
);
1311 curr_attr
|= ATTR_UNDER
; break;
1312 case 5: /* enable blink */
1313 compatibility(VT100AVO
);
1314 curr_attr
|= ATTR_BLINK
; break;
1315 case 7: /* enable reverse video */
1316 curr_attr
|= ATTR_REVERSE
; break;
1317 case 22: /* disable bold */
1318 compatibility2(OTHER
,VT220
);
1319 curr_attr
&= ~ATTR_BOLD
; break;
1320 case 24: /* disable underline */
1321 compatibility2(OTHER
,VT220
);
1322 curr_attr
&= ~ATTR_UNDER
; break;
1323 case 25: /* disable blink */
1324 compatibility2(OTHER
,VT220
);
1325 curr_attr
&= ~ATTR_BLINK
; break;
1326 case 27: /* disable reverse video */
1327 compatibility2(OTHER
,VT220
);
1328 curr_attr
&= ~ATTR_REVERSE
; break;
1329 case 30: case 31: case 32: case 33:
1330 case 34: case 35: case 36: case 37:
1332 curr_attr
&= ~ATTR_FGMASK
;
1333 curr_attr
|= (esc_args
[i
] - 30) << ATTR_FGSHIFT
;
1335 case 39: /* default-foreground */
1336 curr_attr
&= ~ATTR_FGMASK
;
1337 curr_attr
|= ATTR_DEFFG
;
1339 case 40: case 41: case 42: case 43:
1340 case 44: case 45: case 46: case 47:
1342 curr_attr
&= ~ATTR_BGMASK
;
1343 curr_attr
|= (esc_args
[i
] - 40) << ATTR_BGSHIFT
;
1345 case 49: /* default-background */
1346 curr_attr
&= ~ATTR_BGMASK
;
1347 curr_attr
|= ATTR_DEFBG
;
1354 (curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
|ATTR_BLINK
))
1358 case 's': /* save cursor */
1361 case 'u': /* restore cursor */
1362 save_cursor (FALSE
);
1363 seen_disp_event
= TRUE
;
1365 case 't': /* set page size - ie window height */
1367 * VT340/VT420 sequence DECSLPP, DEC only allows values
1368 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1369 * illegal values (eg first arg 1..9) for window changing
1372 compatibility(VT340TEXT
);
1373 if (esc_nargs
<=1 && (esc_args
[0]<1 || esc_args
[0]>=24)) {
1374 request_resize (cols
, def(esc_args
[0], 24), 0);
1378 case ANSI('|', '*'):
1379 /* VT420 sequence DECSNLS
1380 * Set number of lines on screen
1381 * VT420 uses VGA like hardware and can support any size in
1382 * reasonable range (24..49 AIUI) with no default specified.
1384 compatibility(VT420
);
1385 if (esc_nargs
==1 && esc_args
[0]>0) {
1386 request_resize (cols
, def(esc_args
[0], cfg
.height
), 0);
1390 case ANSI('|', '$'):
1391 /* VT340/VT420 sequence DECSCPP
1392 * Set number of columns per page
1393 * Docs imply range is only 80 or 132, but I'll allow any.
1395 compatibility(VT340TEXT
);
1397 request_resize (def(esc_args
[0], cfg
.width
), rows
, 0);
1401 case 'X': /* write N spaces w/o moving cursor */
1402 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1403 compatibility(ANSIMIN
);
1405 int n
= def(esc_args
[0], 1);
1406 unsigned long *p
= cpos
;
1407 if (n
> cols
- curs_x
)
1409 check_selection (cpos
, cpos
+n
);
1412 seen_disp_event
= TRUE
;
1415 case 'x': /* report terminal characteristics */
1416 compatibility(VT100
);
1419 int i
= def(esc_args
[0], 0);
1420 if (i
== 0 || i
== 1) {
1421 strcpy (buf
, "\033[2;1;1;112;112;1;0x");
1423 ldisc
->send (buf
, 20);
1428 compatibility(OTHER
);
1429 use_bce
= (esc_args
[0]<=0);
1430 erase_char
= ERASE_CHAR
;
1432 erase_char
= (' '|(curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
)));
1435 compatibility(OTHER
);
1436 blink_is_real
= (esc_args
[0]>=1);
1439 /* Allow the host to make this emulator a 'perfect' VT102.
1440 * This first appeared in the VT220, but we do need to get
1441 * back to PuTTY mode so I won't check it.
1443 * The arg in 40..42 are a PuTTY extension.
1444 * The 2nd arg, 8bit vs 7bit is not checked.
1446 * Setting VT102 mode should also change the Fkeys to
1447 * generate PF* codes as a real VT102 has no Fkeys.
1448 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1451 * Note ESC c will NOT change this!
1454 switch (esc_args
[0]) {
1455 case 61: compatibility_level
&= ~TM_VTXXX
;
1456 compatibility_level
|= TM_VT102
; break;
1457 case 62: compatibility_level
&= ~TM_VTXXX
;
1458 compatibility_level
|= TM_VT220
; break;
1460 default: if( esc_args
[0] > 60 && esc_args
[0] < 70 )
1461 compatibility_level
|= TM_VTXXX
;
1464 case 40: compatibility_level
&= TM_VTXXX
; break;
1465 case 41: compatibility_level
= TM_PUTTY
; break;
1466 case 42: compatibility_level
= TM_SCOANSI
; break;
1469 compatibility_level
= TM_PUTTY
; break;
1473 /* Change the response to CSI c */
1474 if (esc_args
[0] == 50) {
1477 strcpy(id_string
, "\033[?");
1478 for (i
=1; i
<esc_nargs
; i
++) {
1479 if (i
!=1) strcat(id_string
, ";");
1480 sprintf(lbuf
, "%d", esc_args
[i
]);
1481 strcat(id_string
, lbuf
);
1483 strcat(id_string
, "c");
1487 /* Is this a good idea ?
1488 * Well we should do a soft reset at this point ...
1490 if (!has_compat(VT420
) && has_compat(VT100
)) {
1491 if (reset_132
) request_resize (132, 24, 1);
1492 else request_resize ( 80, 24, 1);
1500 /* VT100 only here, checked above */
1503 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_GBCHR
;
1506 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_LINEDRW
;
1509 default: /* specifically, 'B' */
1510 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_ASCII
;
1513 if( !has_compat(VT220
) || c
!= '%' )
1514 termstate
= TOPLEVEL
;
1519 case 'P': /* Linux palette sequence */
1520 termstate
= SEEN_OSC_P
;
1523 case 'R': /* Linux palette reset */
1526 termstate
= TOPLEVEL
;
1528 case 'W': /* word-set */
1529 termstate
= SEEN_OSC_W
;
1532 case '0': case '1': case '2': case '3': case '4':
1533 case '5': case '6': case '7': case '8': case '9':
1534 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1538 * Grotty hack to support xterm and DECterm title
1539 * sequences concurrently.
1541 if (esc_args
[0] == 2) {
1545 /* else fall through */
1547 termstate
= OSC_STRING
;
1553 * This OSC stuff is EVIL. It takes just one character to get into
1554 * sysline mode and it's not initially obvious how to get out.
1555 * So I've added CR and LF as string aborts.
1556 * This shouldn't effect compatibility as I believe embedded
1557 * control characters are supposed to be interpreted (maybe?)
1558 * and they don't display anything useful anyway.
1562 if (c
== '\n' || c
== '\r') {
1563 termstate
= TOPLEVEL
;
1564 } else if (c
== 0234 || c
== '\007' ) {
1566 * These characters terminate the string; ST and BEL
1567 * terminate the sequence and trigger instant
1568 * processing of it, whereas ESC goes back to SEEN_ESC
1569 * mode unless it is followed by \, in which case it is
1570 * synonymous with ST in the first place.
1573 termstate
= TOPLEVEL
;
1574 } else if (c
== '\033')
1575 termstate
= OSC_MAYBE_ST
;
1576 else if (osc_strlen
< OSC_STR_MAX
)
1577 osc_string
[osc_strlen
++] = c
;
1581 int max
= (osc_strlen
== 0 ?
21 : 16);
1583 if (c
>= '0' && c
<= '9')
1585 else if (c
>= 'A' && c
<= 'A'+max
-10)
1587 else if (c
>= 'a' && c
<= 'a'+max
-10)
1590 termstate
= TOPLEVEL
;
1591 osc_string
[osc_strlen
++] = val
;
1592 if (osc_strlen
>= 7) {
1593 palette_set (osc_string
[0],
1594 osc_string
[1] * 16 + osc_string
[2],
1595 osc_string
[3] * 16 + osc_string
[4],
1596 osc_string
[5] * 16 + osc_string
[6]);
1598 termstate
= TOPLEVEL
;
1604 case '0': case '1': case '2': case '3': case '4':
1605 case '5': case '6': case '7': case '8': case '9':
1606 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1609 termstate
= OSC_STRING
;
1616 unsigned long nlattr
;
1622 n
= rows
* (cols
+1);
1624 *p
++ = ATTR_DEFAULT
| 'E';
1626 seen_disp_event
= TRUE
;
1627 check_selection (scrtop
, scrtop
+ rows
* (cols
+1));
1630 case '3': nlattr
= LATTR_TOP
; if(0) {
1631 case '4': nlattr
= LATTR_BOT
; } if(0) {
1632 case '5': nlattr
= LATTR_NORM
; } if(0) {
1633 case '6': nlattr
= LATTR_WIDE
; }
1635 p
= scrtop
+ curs_y
* (cols
+1) + cols
;
1640 termstate
= TOPLEVEL
;
1643 termstate
= TOPLEVEL
;
1644 seen_disp_event
= TRUE
;
1647 move (curs_x
, curs_y
- 1, 1);
1650 move (curs_x
, curs_y
+ 1, 1);
1653 move (curs_x
+ 1, curs_y
, 1);
1656 move (curs_x
- 1, curs_y
, 1);
1659 cset_attr
[cset
=0] = ATTR_LINEDRW
;
1662 cset_attr
[cset
=0] = ATTR_ASCII
;
1669 scroll (0, rows
-1, -1, TRUE
);
1670 else if (curs_y
> 0)
1676 erase_lots(FALSE
, FALSE
, TRUE
);
1680 erase_lots(TRUE
, FALSE
, TRUE
);
1683 /* XXX Print cursor line */
1686 /* XXX Start controller mode */
1689 /* XXX Stop controller mode */
1692 termstate
= VT52_Y1
;
1695 ldisc
->send ("\033/Z", 3);
1698 app_keypad_keys
= TRUE
;
1701 app_keypad_keys
= FALSE
;
1704 /* XXX This should switch to VT100 mode not current or default
1705 * VT mode. But this will only have effect in a VT220+
1711 /* XXX Enter auto print mode */
1714 /* XXX Exit auto print mode */
1717 /* XXX Print screen */
1722 termstate
= VT52_Y2
;
1723 move(curs_x
, c
-' ', 0);
1726 termstate
= TOPLEVEL
;
1727 move(c
-' ', curs_y
, 0);
1730 if (selstate
!= NO_SELECTION
)
1731 check_selection (cpos
, cpos
+1);
1737 if(!beep_count
) beep_overload
=0;
1739 else if(beep_count
&& beep_count
<5 && cfg
.beep
)
1744 * Compare two lines to determine whether they are sufficiently
1745 * alike to scroll-optimise one to the other. Return the degree of
1748 static int linecmp (unsigned long *a
, unsigned long *b
) {
1751 for (i
=n
=0; i
< cols
; i
++)
1752 n
+= (*a
++ == *b
++);
1757 * Given a context, update the window. Out of paranoia, we don't
1758 * allow WM_PAINT responses to do scrolling optimisations.
1760 static void do_paint (Context ctx
, int may_optimise
){
1761 int i
, j
, start
, our_curs_y
;
1762 unsigned long attr
, rv
, cursor
;
1767 if (blinker
|| !cfg
.blink_cur
)
1768 cursor
= ATTR_ACTCURS
;
1773 cursor
= ATTR_PASCURS
;
1775 cursor
|= ATTR_RIGHTCURS
;
1778 rv
= (rvideo ? ATTR_REVERSE
: 0);
1779 our_curs_y
= curs_y
+ (scrtop
- disptop
) / (cols
+1);
1781 for (i
=0; i
<rows
; i
++) {
1782 int idx
= i
*(cols
+1);
1783 int lattr
= (disptop
[idx
+cols
] & LATTR_MODE
);
1784 for (j
=0; j
<=cols
; j
++,idx
++) {
1785 unsigned long *d
= disptop
+idx
;
1786 wanttext
[idx
] = lattr
| (((*d
&~ ATTR_WRAPPED
) ^ rv
1787 ^ (selstart
<= d
&& d
< selend ?
1788 ATTR_REVERSE
: 0)) |
1789 (i
==our_curs_y
&& j
==curs_x ? cursor
: 0));
1790 if (blink_is_real
) {
1791 if (has_focus
&& tblinker
&& (wanttext
[idx
]&ATTR_BLINK
) )
1793 wanttext
[idx
] &= ATTR_MASK
;
1794 wanttext
[idx
] += ' ';
1796 wanttext
[idx
] &= ~ATTR_BLINK
;
1802 * We would perform scrolling optimisations in here, if they
1803 * didn't have a nasty tendency to cause the whole sodding
1804 * program to hang for a second at speed-critical moments.
1805 * We'll leave it well alone...
1808 for (i
=0; i
<rows
; i
++) {
1809 int idx
= i
*(cols
+1);
1810 int lattr
= (wanttext
[idx
+cols
] & LATTR_MODE
);
1812 for (j
=0; j
<=cols
; j
++,idx
++) {
1813 unsigned long t
= wanttext
[idx
];
1814 int needs_update
= (j
< cols
&& t
!= disptext
[idx
]);
1815 int keep_going
= (start
!= -1 && needs_update
&&
1816 (t
& ATTR_MASK
) == attr
&&
1817 j
-start
< sizeof(ch
));
1818 if (start
!= -1 && !keep_going
) {
1819 do_text (ctx
, start
, i
, ch
, j
-start
, attr
, lattr
);
1825 attr
= t
& ATTR_MASK
;
1827 ch
[j
-start
] = (char) (t
& CHAR_MASK
);
1835 * Flick the switch that says if blinking things should be shown or hidden.
1838 void term_blink(int flg
) {
1839 static long last_blink
= 0;
1840 static long last_tblink
= 0;
1841 long now
, blink_diff
;
1843 now
= GetTickCount();
1844 blink_diff
= now
-last_tblink
;
1846 /* Make sure the text blinks no more than 2Hz */
1847 if (blink_diff
<0 || blink_diff
>450)
1850 tblinker
= !tblinker
;
1859 blink_diff
= now
-last_blink
;
1861 /* Make sure the cursor blinks no more than 2Hz */
1862 if (blink_diff
>=0 && blink_diff
<450)
1870 * Invalidate the whole screen so it will be repainted in full.
1872 void term_invalidate(void) {
1875 for (i
=0; i
<rows
*(cols
+1); i
++)
1876 disptext
[i
] = ATTR_INVALID
;
1880 * Paint the window in response to a WM_PAINT message.
1882 void term_paint (Context ctx
, int l
, int t
, int r
, int b
) {
1883 int i
, j
, left
, top
, right
, bottom
;
1885 left
= l
/ font_width
;
1886 right
= (r
- 1) / font_width
;
1887 top
= t
/ font_height
;
1888 bottom
= (b
- 1) / font_height
;
1889 for (i
= top
; i
<= bottom
&& i
< rows
; i
++)
1891 if ( (disptext
[i
*(cols
+1)+cols
]&LATTR_MODE
) == LATTR_NORM
)
1892 for (j
= left
; j
<= right
&& j
< cols
; j
++)
1893 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
1895 for (j
= left
/2; j
<= right
/2+1 && j
< cols
; j
++)
1896 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
1899 /* This should happen soon enough, also for some reason it sometimes
1900 * fails to actually do anything when re-sizing ... painting the wrong
1902 do_paint (ctx, FALSE);
1907 * Attempt to scroll the scrollback. The second parameter gives the
1908 * position we want to scroll to; the first is +1 to denote that
1909 * this position is relative to the beginning of the scrollback, -1
1910 * to denote it is relative to the end, and 0 to denote that it is
1911 * relative to the current position.
1913 void term_scroll (int rel
, int where
) {
1914 int n
= where
* (cols
+1);
1916 disptop
= (rel
< 0 ? scrtop
:
1917 rel
> 0 ? sbtop
: disptop
) + n
;
1918 if (disptop
< sbtop
)
1920 if (disptop
> scrtop
)
1926 static void clipme(unsigned long *top
, unsigned long *bottom
, char *workbuf
) {
1927 char *wbptr
; /* where next char goes within workbuf */
1928 int wblen
= 0; /* workbuf len */
1929 int buflen
; /* amount of memory allocated to workbuf */
1931 if ( workbuf
!= NULL
) { /* user supplied buffer? */
1932 buflen
= -1; /* assume buffer passed in is big enough */
1933 wbptr
= workbuf
; /* start filling here */
1936 buflen
= 0; /* No data is available yet */
1938 while (top
< bottom
) {
1940 unsigned long *lineend
= top
- (top
-text
) % (cols
+1) + cols
;
1941 unsigned long *nlpos
= lineend
;
1943 if (!(*nlpos
& ATTR_WRAPPED
)) {
1944 while ((nlpos
[-1] & CHAR_MASK
) == 0x20 && nlpos
> top
)
1949 while (top
< nlpos
&& top
< bottom
)
1952 /* VT Specials -> ISO8859-1 */
1953 static const char poorman2
[] =
1954 "* # HTFFCRLF\xB0 \xB1 NLVT+ + + + + - - - - - + + + + | <=>=PI!=\xA3 \xB7 ";
1957 int ch
= (*top
& CHAR_MASK
);
1960 if ((*top
& ATTR_LINEDRW
) && ch
>= 0x60 && ch
< 0x7F) {
1962 *wbptr
++ = poorman2
[2*(ch
-0x60)];
1963 if ( (x
= poorman2
[2*(ch
-0x60)+1]) != ' ')
1968 if ((*top
& ATTR_GBCHR
) && ch
== '#')
1969 *wbptr
++ = (unsigned char) 0xA3;
1972 if ( wblen
== buflen
)
1974 workbuf
= srealloc(workbuf
, buflen
+= 100);
1975 wbptr
= workbuf
+ wblen
;
1978 *wbptr
++ = (unsigned char) ch
;
1983 for (i
=0; i
<sizeof(sel_nl
); i
++)
1985 if ( wblen
== buflen
)
1987 workbuf
= srealloc(workbuf
, buflen
+= 100);
1988 wbptr
= workbuf
+ wblen
;
1991 *wbptr
++ = sel_nl
[i
];
1994 top
= lineend
+ 1; /* start of next line */
1996 write_clip (workbuf
, wblen
, FALSE
); /* transfer to clipboard */
1997 if ( buflen
> 0 ) /* indicates we allocated this buffer */
2001 void term_copyall (void) {
2002 clipme(sbtop
, cpos
, NULL
/* dynamic allocation */);
2006 * Spread the selection outwards according to the selection mode.
2008 static unsigned long *sel_spread_half (unsigned long *p
, int dir
) {
2009 unsigned long *linestart
, *lineend
;
2013 x
= (p
- text
) % (cols
+1);
2015 lineend
= linestart
+ cols
;
2020 * In this mode, every character is a separate unit, except
2021 * for runs of spaces at the end of a non-wrapping line.
2023 if (!(linestart
[cols
] & ATTR_WRAPPED
)) {
2024 unsigned long *q
= lineend
;
2025 while (q
> linestart
&& (q
[-1] & CHAR_MASK
) == 0x20)
2030 p
= (dir
== -1 ? q
: lineend
- 1);
2035 * In this mode, the units are maximal runs of characters
2036 * whose `wordness' has the same value.
2038 wvalue
= wordness
[*p
& CHAR_MASK
];
2040 while (p
< lineend
&& wordness
[p
[1] & CHAR_MASK
] == wvalue
)
2043 while (p
> linestart
&& wordness
[p
[-1] & CHAR_MASK
] == wvalue
)
2049 * In this mode, every line is a unit.
2051 p
= (dir
== -1 ? linestart
: lineend
- 1);
2057 static void sel_spread (void) {
2058 selstart
= sel_spread_half (selstart
, -1);
2059 selend
= sel_spread_half (selend
- 1, +1) + 1;
2062 void term_mouse (Mouse_Button b
, Mouse_Action a
, int x
, int y
) {
2063 unsigned long *selpoint
;
2066 if (y
>=rows
) y
= rows
-1;
2074 if (x
>=cols
) x
= cols
-1;
2076 selpoint
= disptop
+ y
* (cols
+1);
2077 if ((selpoint
[cols
]&LATTR_MODE
) != LATTR_NORM
)
2082 if (b
== MB_SELECT
&& a
== MA_CLICK
) {
2084 selstate
= ABOUT_TO
;
2085 selanchor
= selpoint
;
2087 } else if (b
== MB_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
2089 selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
2090 selstate
= DRAGGING
;
2091 selstart
= selanchor
= selpoint
;
2092 selend
= selstart
+ 1;
2094 } else if ((b
== MB_SELECT
&& a
== MA_DRAG
) ||
2095 (b
== MB_EXTEND
&& a
!= MA_RELEASE
)) {
2096 if (selstate
== ABOUT_TO
&& selanchor
== selpoint
)
2098 if (b
== MB_EXTEND
&& a
!= MA_DRAG
&& selstate
== SELECTED
) {
2099 if (selpoint
-selstart
< (selend
-selstart
)/2)
2100 selanchor
= selend
- 1;
2102 selanchor
= selstart
;
2103 selstate
= DRAGGING
;
2105 if (selstate
!= ABOUT_TO
&& selstate
!= DRAGGING
)
2106 selanchor
= selpoint
;
2107 selstate
= DRAGGING
;
2108 if (selpoint
< selanchor
) {
2109 selstart
= selpoint
;
2110 selend
= selanchor
+ 1;
2112 selstart
= selanchor
;
2113 selend
= selpoint
+ 1;
2116 } else if ((b
== MB_SELECT
|| b
== MB_EXTEND
) && a
== MA_RELEASE
) {
2117 if (selstate
== DRAGGING
) {
2119 * We've completed a selection. We now transfer the
2120 * data to the clipboard.
2122 clipme(selstart
, selend
, selspace
);
2123 selstate
= SELECTED
;
2125 selstate
= NO_SELECTION
;
2126 } else if (b
== MB_PASTE
&& (a
==MA_CLICK
|| a
==MA_2CLK
|| a
==MA_3CLK
)) {
2130 get_clip((void **) &data
, &len
);
2134 if (paste_buffer
) sfree(paste_buffer
);
2135 paste_pos
= paste_hold
= paste_len
= 0;
2136 paste_buffer
= smalloc(len
);
2139 while (p
< data
+len
) {
2140 while (p
< data
+len
&&
2141 !(p
<= data
+len
-sizeof(sel_nl
) &&
2142 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
2150 c
=xlat_kbd2tty(q
[i
]);
2151 paste_buffer
[paste_len
++] = c
;
2155 if (p
<= data
+len
-sizeof(sel_nl
) &&
2156 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
2157 paste_buffer
[paste_len
++] = '\r';
2158 p
+= sizeof(sel_nl
);
2163 /* Assume a small paste will be OK in one go. */
2164 if (paste_len
<256) {
2165 ldisc
->send (paste_buffer
, paste_len
);
2166 if (paste_buffer
) sfree(paste_buffer
);
2168 paste_pos
= paste_hold
= paste_len
= 0;
2171 get_clip(NULL
, NULL
);
2177 void term_nopaste() {
2178 if(paste_len
== 0) return;
2179 sfree(paste_buffer
);
2185 static long last_paste
= 0;
2186 long now
, paste_diff
;
2188 if(paste_len
== 0) return;
2190 /* Don't wait forever to paste */
2192 now
= GetTickCount();
2193 paste_diff
= now
-last_paste
;
2194 if (paste_diff
>=0 && paste_diff
<450)
2199 while(paste_pos
<paste_len
)
2202 while (n
+ paste_pos
< paste_len
) {
2203 if (paste_buffer
[paste_pos
+ n
++] == '\r')
2206 ldisc
->send (paste_buffer
+paste_pos
, n
);
2209 if (paste_pos
< paste_len
) {
2214 sfree(paste_buffer
);
2219 static void deselect (void) {
2220 selstate
= NO_SELECTION
;
2221 selstart
= selend
= scrtop
;
2224 void term_deselect (void) {
2230 * from_backend(), to get data from the backend for the terminal.
2232 void from_backend(int is_stderr
, char *data
, int len
) {
2234 if (inbuf_head
>= INBUF_SIZE
)
2236 inbuf
[inbuf_head
++] = *data
++;
2241 * Log session traffic.
2243 void logtraffic(unsigned char c
, int logmode
) {
2244 if (cfg
.logtype
> 0) {
2245 if (cfg
.logtype
== logmode
) {
2246 /* deferred open file from pgm start? */
2247 if (!lgfp
) logfopen();
2248 if (lgfp
) fputc (c
, lgfp
);
2253 /* open log file append/overwrite mode */
2254 void logfopen(void) {
2262 sprintf (writemod
, "wb"); /* default to rewrite */
2263 lgfp
= fopen(cfg
.logfilename
, "r"); /* file already present? */
2267 i
= askappend(cfg
.logfilename
);
2269 writemod
[0] = 'a'; /* set append mode */
2270 else if (i
== 0) { /* cancelled */
2272 cfg
.logtype
= 0; /* disable logging */
2277 lgfp
= fopen(cfg
.logfilename
, writemod
);
2278 if (lgfp
) { /* enter into event log */
2279 sprintf(buf
, "%s session log (%s mode) to file : ",
2280 (writemod
[0] == 'a') ?
"Appending" : "Writing new",
2281 (cfg
.logtype
== LGTYP_ASCII ?
"ASCII" :
2282 cfg
.logtype
== LGTYP_DEBUG ?
"raw" : "<ukwn>") );
2283 /* Make sure we do not exceed the output buffer size */
2284 strncat (buf
, cfg
.logfilename
, 128);
2285 buf
[strlen(buf
)] = '\0';
2288 /* --- write header line iinto log file */
2289 fputs ("=~=~=~=~=~=~=~=~=~=~=~= PuTTY log ", lgfp
);
2292 strftime(buf
, 24, "%Y.%m.%d %H:%M:%S", tm
);
2294 fputs (" =~=~=~=~=~=~=~=~=~=~=~=\r\n", lgfp
);
2298 void logfclose (void) {
2299 if (lgfp
) { fclose(lgfp
); lgfp
= NULL
; }