15 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
16 #define CL_VT100 0x0002 /* VT100 */
17 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
18 #define CL_VT102 0x0008 /* VT102 */
19 #define CL_VT220 0x0010 /* VT220 */
20 #define CL_VT320 0x0020 /* VT320 */
21 #define CL_VT420 0x0040 /* VT420 */
22 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
23 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
24 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
25 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
26 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
28 #define TM_ANSIMIN (CL_ANSIMIN)
29 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
30 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
31 #define TM_VT102 (TM_VT100AVO|CL_VT102)
32 #define TM_VT220 (TM_VT102|CL_VT220)
33 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
34 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
36 #define TM_PUTTY (0xFFFF)
38 #define compatibility(x) \
39 if ( ((CL_##x)&compatibility_level) == 0 ) { \
43 #define compatibility2(x,y) \
44 if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \
49 #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
51 static int compatibility_level
= TM_PUTTY
;
54 static unsigned long *text
; /* buffer of text on terminal screen */
55 static unsigned long *scrtop
; /* top of working screen */
56 static unsigned long *disptop
; /* top of displayed screen */
57 static unsigned long *sbtop
; /* top of scrollback */
58 static unsigned long *sbbot
; /* furthest extent of scrollback */
59 static unsigned long *cpos
; /* cursor position (convenience) */
60 static unsigned long *disptext
; /* buffer of text on real screen */
61 static unsigned long *wanttext
; /* buffer of text we want on screen */
62 static unsigned long *alttext
; /* buffer of text on alt. screen */
64 static unsigned char *selspace
; /* buffer for building selections in */
66 #define TSIZE (sizeof(*text))
67 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
69 static unsigned long curr_attr
, save_attr
;
70 static unsigned long erase_char
= ERASE_CHAR
;
72 static int curs_x
, curs_y
; /* cursor */
73 static int save_x
, save_y
; /* saved cursor position */
74 static int marg_t
, marg_b
; /* scroll margins */
75 static int dec_om
; /* DEC origin mode flag */
76 static int wrap
, wrapnext
; /* wrap flags */
77 static int insert
; /* insert-mode flag */
78 static int cset
; /* 0 or 1: which char set */
79 static int save_cset
, save_csattr
; /* saved with cursor position */
80 static int rvideo
; /* global reverse video flag */
81 static int cursor_on
; /* cursor enabled flag */
82 static int reset_132
; /* Flag ESC c resets to 80 cols */
83 static int use_bce
; /* Use Background coloured erase */
84 static int blinker
; /* When blinking is the cursor on ? */
85 static int tblinker
; /* When the blinking text is on */
86 static int blink_is_real
; /* Actually blink blinking text */
88 static unsigned long cset_attr
[2];
91 * Saved settings on the alternate screen.
93 static int alt_x
, alt_y
, alt_om
, alt_wrap
, alt_wnext
, alt_ins
, alt_cset
;
94 static int alt_t
, alt_b
;
97 #define ARGS_MAX 32 /* max # of esc sequence arguments */
98 #define ARG_DEFAULT 0 /* if an arg isn't specified */
99 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
100 static int esc_args
[ARGS_MAX
];
101 static int esc_nargs
;
102 static int esc_query
;
103 #define ANSI(x,y) ((x)+((y)<<8))
104 #define ANSI_QUE(x) ANSI(x,TRUE)
106 #define OSC_STR_MAX 2048
107 static int osc_strlen
;
108 static char osc_string
[OSC_STR_MAX
+1];
111 static char id_string
[1024] = "\033[?6c";
113 static unsigned char *tabs
;
127 OSC_STRING
, OSC_MAYBE_ST
,
135 NO_SELECTION
, ABOUT_TO
, DRAGGING
, SELECTED
138 SM_CHAR
, SM_WORD
, SM_LINE
140 static unsigned long *selstart
, *selend
, *selanchor
;
142 static short wordness
[256] = {
143 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 */
144 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 */
145 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 */
146 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 */
147 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 */
148 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 */
149 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 */
150 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 */
153 static unsigned char sel_nl
[] = SEL_NL
;
154 static char * paste_buffer
= 0;
155 static int paste_len
, paste_pos
, paste_hold
;
158 * Internal prototypes.
160 static void do_paint (Context
, int);
161 static void erase_lots (int, int, int);
162 static void swap_screen (int);
163 static void update_sbar (void);
164 static void deselect (void);
167 * Set up power-on settings for the terminal.
169 static void power_on(void) {
170 curs_x
= curs_y
= alt_x
= alt_y
= save_x
= save_y
= 0;
173 alt_b
= marg_b
= rows
- 1;
178 for (i
= 0; i
< cols
; i
++)
179 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
181 alt_om
= dec_om
= cfg
.dec_om
;
182 alt_wnext
= wrapnext
= alt_ins
= insert
= FALSE
;
183 alt_wrap
= wrap
= cfg
.wrap_mode
;
185 cset_attr
[0] = cset_attr
[1] = ATTR_ASCII
;
188 save_attr
= curr_attr
= ATTR_DEFAULT
;
189 app_cursor_keys
= cfg
.app_cursor
;
190 app_keypad_keys
= cfg
.app_keypad
;
192 blink_is_real
= cfg
.blinktext
;
193 erase_char
= ERASE_CHAR
;
197 for (i
= 0; i
< 256; i
++)
198 wordness
[i
] = cfg
.wordness
[i
];
202 erase_lots (FALSE
, TRUE
, TRUE
);
204 erase_lots (FALSE
, TRUE
, TRUE
);
209 * Force a screen update.
211 void term_update(void) {
215 if ( (seen_key_event
&& (cfg
.scroll_on_key
)) ||
216 (seen_disp_event
&& (!cfg
.scroll_on_key
)) ) {
218 seen_disp_event
= seen_key_event
= 0;
220 do_paint (ctx
, TRUE
);
221 sys_cursor(curs_x
, curs_y
+ (scrtop
- disptop
) / (cols
+1));
227 * Same as power_on(), but an external function.
229 void term_pwron(void) {
238 * Clear the scrollback.
240 void term_clrsb(void) {
241 disptop
= sbtop
= scrtop
;
246 * Initialise the terminal.
248 void term_init(void) {
249 text
= sbtop
= sbbot
= scrtop
= disptop
= cpos
= NULL
;
250 disptext
= wanttext
= NULL
;
259 * Set up the terminal for a given size.
261 void term_size(int newrows
, int newcols
, int newsavelines
) {
262 unsigned long *newtext
, *newdisp
, *newwant
, *newalt
;
263 int i
, j
, crows
, ccols
;
265 int save_alt_which
= alt_which
;
267 if (newrows
== rows
&& newcols
== cols
&& newsavelines
== savelines
)
268 return; /* nothing to do */
274 alt_b
= marg_b
= newrows
- 1;
276 newtext
= smalloc ((newrows
+newsavelines
)*(newcols
+1)*TSIZE
);
277 sbbot
= newtext
+ newsavelines
*(newcols
+1);
278 for (i
=0; i
<(newrows
+newsavelines
)*(newcols
+1); i
++)
279 newtext
[i
] = erase_char
;
281 crows
= rows
+ (scrtop
- sbtop
) / (cols
+1);
282 if (crows
> newrows
+newsavelines
)
283 crows
= newrows
+newsavelines
;
287 disptop
= newtext
+ (crows
-newrows
)*(newcols
+1);
288 ccols
= (cols
< newcols ? cols
: newcols
);
289 for (i
=0; i
<crows
; i
++) {
290 int oldidx
= (rows
- crows
+ i
) * (cols
+1);
291 int newidx
= (newrows
- crows
+ i
) * (newcols
+1);
293 for (j
=0; j
<ccols
; j
++)
294 disptop
[newidx
+j
] = scrtop
[oldidx
+j
];
295 disptop
[newidx
+newcols
] =
296 (cols
== newcols ? scrtop
[oldidx
+cols
]
297 : (scrtop
[oldidx
+cols
]&LATTR_MODE
));
301 sbtop
= disptop
= newtext
;
307 newdisp
= smalloc (newrows
*(newcols
+1)*TSIZE
);
308 for (i
=0; i
<newrows
*(newcols
+1); i
++)
309 newdisp
[i
] = ATTR_INVALID
;
313 newwant
= smalloc (newrows
*(newcols
+1)*TSIZE
);
314 for (i
=0; i
<newrows
*(newcols
+1); i
++)
315 newwant
[i
] = ATTR_INVALID
;
319 newalt
= smalloc (newrows
*(newcols
+1)*TSIZE
);
320 for (i
=0; i
<newrows
*(newcols
+1); i
++)
321 newalt
[i
] = erase_char
;
326 selspace
= smalloc ( (newrows
+newsavelines
) * (newcols
+sizeof(sel_nl
)) );
328 tabs
= srealloc (tabs
, newcols
*sizeof(*tabs
));
331 for (i
= (cols
> 0 ? cols
: 0); i
< newcols
; i
++)
332 tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
336 curs_y
+= newrows
- rows
;
339 if (curs_y
>= newrows
)
341 if (curs_x
>= newcols
)
344 wrapnext
= alt_wnext
= FALSE
;
348 savelines
= newsavelines
;
351 swap_screen(save_alt_which
);
360 static void swap_screen (int which
) {
364 if (which
== alt_which
)
369 for (t
=0; t
<rows
*(cols
+1); t
++) {
370 tt
= scrtop
[t
]; scrtop
[t
] = alttext
[t
]; alttext
[t
] = tt
;
373 t
= curs_x
; curs_x
= alt_x
; alt_x
= t
;
374 t
= curs_y
; curs_y
= alt_y
; alt_y
= t
;
375 t
= marg_t
; marg_t
= alt_t
; alt_t
= t
;
376 t
= marg_b
; marg_b
= alt_b
; alt_b
= t
;
377 t
= dec_om
; dec_om
= alt_om
; alt_om
= t
;
378 t
= wrap
; wrap
= alt_wrap
; alt_wrap
= t
;
379 t
= wrapnext
; wrapnext
= alt_wnext
; alt_wnext
= t
;
380 t
= insert
; insert
= alt_ins
; alt_ins
= t
;
381 t
= cset
; cset
= alt_cset
; alt_cset
= t
;
387 * Update the scroll bar.
389 static void update_sbar(void) {
392 min
= (sbtop
- text
) / (cols
+1);
393 set_sbar ((scrtop
- text
) / (cols
+1) + rows
- min
,
394 (disptop
- text
) / (cols
+1) - min
,
399 * Check whether the region bounded by the two pointers intersects
400 * the scroll region, and de-select the on-screen selection if so.
402 static void check_selection (unsigned long *from
, unsigned long *to
) {
403 if (from
< selend
&& selstart
< to
)
408 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
409 * for backward.) `sb' is TRUE if the scrolling is permitted to
410 * affect the scrollback buffer.
412 static void scroll (int topline
, int botline
, int lines
, int sb
) {
413 unsigned long *scroll_top
;
414 unsigned long *newscr
;
415 int scroll_size
, size
, i
;
416 int scrtop_is_disptop
= (scrtop
==disptop
);
417 static int recursive
= 0;
419 /* Only scroll more than the window if we're doing a 10% scroll */
420 if (!recursive
&& lines
> botline
- topline
+ 1)
421 lines
= botline
- topline
+ 1;
423 scroll_top
= scrtop
+ topline
*(cols
+1);
424 size
= (lines
< 0 ?
-lines
: lines
) * (cols
+1);
425 scroll_size
= (botline
- topline
+ 1) * (cols
+1) - size
;
427 if (lines
> 0 && topline
== 0 && alt_which
== 0 && sb
) {
429 * Since we're going to scroll the top line and we're on the
430 * scrolling screen let's also effect the scrollback buffer.
432 * This is normally done by moving the position the screen
433 * painter reads from to reduce the amount of memory copying
436 if (scroll_size
>= 0 && !recursive
) {
437 newscr
= scrtop
+ lines
* (cols
+1);
439 if (newscr
> sbbot
&& botline
== rows
-1) {
440 /* We've hit the bottom of memory, so we have to do a
441 * physical scroll. But instead of just 1 line do it
442 * by 10% of the available memory.
444 * If the scroll region isn't the whole screen then we can't
445 * do this as it stands. We would need to recover the bottom
446 * of the screen from the scroll buffer after being sure that
447 * it doesn't get deleted.
450 i
= (rows
+savelines
)/10;
452 /* Make it simple and ensure safe recursion */
453 if ( i
<savelines
-1) {
455 scroll(topline
, botline
, i
, sb
);
458 newscr
= scrtop
- i
* (cols
+1);
459 if (scrtop_is_disptop
) disptop
= newscr
;
463 newscr
= scrtop
+ lines
* (cols
+1);
466 if (newscr
<= sbbot
) {
467 if (scrtop_is_disptop
) disptop
= newscr
;
470 if (botline
== rows
-1 )
471 for (i
= 0; i
< size
; i
++)
472 scrtop
[i
+scroll_size
] = erase_char
;
477 if (botline
!= rows
-1) {
478 /* This fastscroll only works for full window scrolls.
479 * If the caller wanted a partial one we have to reverse
480 * scroll the bottom of the screen.
482 scroll(botline
-lines
+1, rows
-1, -lines
, 0);
487 /* If we can't scroll by memory remapping do it physically.
488 * But rather than expensivly doing the scroll buffer just
489 * scroll the screen. All it means is that sometimes we choose
490 * to not add lines from a scroll region to the scroll buffer.
493 if (savelines
<= 400) {
494 sbtop
-= lines
* (cols
+1);
497 scroll_size
+= scroll_top
- sbtop
;
503 /* Ho hum, expensive scroll required. */
505 sbtop
-= lines
* (cols
+1);
508 scroll_size
+= scroll_top
- sbtop
;
515 if (scroll_size
< 0) {
522 memmove (scroll_top
, scroll_top
+ size
, scroll_size
*TSIZE
);
523 for (i
= 0; i
< size
; i
++)
524 scroll_top
[i
+scroll_size
] = erase_char
;
525 if (selstart
> scroll_top
&&
526 selstart
< scroll_top
+ size
+ scroll_size
) {
528 if (selstart
< scroll_top
)
529 selstart
= scroll_top
;
531 if (selend
> scroll_top
&&
532 selend
< scroll_top
+ size
+ scroll_size
) {
534 if (selend
< scroll_top
)
537 if (scrtop_is_disptop
)
540 if (disptop
> scroll_top
&&
541 disptop
< scroll_top
+ size
+ scroll_size
) {
543 if (disptop
< scroll_top
)
544 disptop
= scroll_top
;
548 memmove (scroll_top
+ size
, scroll_top
, scroll_size
*TSIZE
);
549 for (i
= 0; i
< size
; i
++)
550 scroll_top
[i
] = erase_char
;
551 if (selstart
> scroll_top
&&
552 selstart
< scroll_top
+ size
+ scroll_size
) {
554 if (selstart
> scroll_top
+ size
+ scroll_size
)
555 selstart
= scroll_top
+ size
+ scroll_size
;
557 if (selend
> scroll_top
&&
558 selend
< scroll_top
+ size
+ scroll_size
) {
560 if (selend
> scroll_top
+ size
+ scroll_size
)
561 selend
= scroll_top
+ size
+ scroll_size
;
563 if (scrtop_is_disptop
)
565 else if (disptop
> scroll_top
&&
566 disptop
< scroll_top
+ size
+ scroll_size
) {
568 if (disptop
> scroll_top
+ size
+ scroll_size
)
569 disptop
= scroll_top
+ size
+ scroll_size
;
575 * Move the cursor to a given position, clipping at boundaries. We
576 * may or may not want to clip at the scroll margin: marg_clip is 0
577 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
578 * even _being_ outside the margins.
580 static void move (int x
, int y
, int marg_clip
) {
586 if ((curs_y
>= marg_t
|| marg_clip
== 2) && y
< marg_t
)
588 if ((curs_y
<= marg_b
|| marg_clip
== 2) && y
> marg_b
)
602 * Save or restore the cursor and SGR mode.
604 static void save_cursor(int save
) {
608 save_attr
= curr_attr
;
610 save_csattr
= cset_attr
[cset
];
614 /* Make sure the window hasn't shrunk since the save */
615 if (curs_x
>= cols
) curs_x
= cols
-1;
616 if (curs_y
>= rows
) curs_y
= rows
-1;
618 curr_attr
= save_attr
;
620 cset_attr
[cset
] = save_csattr
;
622 if (use_bce
) erase_char
= (' ' |(curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
)));
627 * Erase a large portion of the screen: the whole screen, or the
628 * whole line, or parts thereof.
630 static void erase_lots (int line_only
, int from_begin
, int to_end
) {
631 unsigned long *startpos
, *endpos
;
634 startpos
= cpos
- curs_x
;
635 endpos
= startpos
+ cols
;
636 /* I've removed the +1 so that the Wide screen stuff is not
637 * removed when it shouldn't be.
641 endpos
= startpos
+ rows
* (cols
+1);
647 check_selection (startpos
, endpos
);
649 /* Clear screen also forces a full window redraw, just in case. */
650 if (startpos
== scrtop
&& endpos
== scrtop
+ rows
* (cols
+1))
653 while (startpos
< endpos
)
654 *startpos
++ = erase_char
;
658 * Insert or delete characters within the current line. n is +ve if
659 * insertion is desired, and -ve for deletion.
661 static void insch (int n
) {
662 int dir
= (n
< 0 ?
-1 : +1);
665 n
= (n
< 0 ?
-n
: n
);
666 if (n
> cols
- curs_x
)
668 m
= cols
- curs_x
- n
;
669 check_selection (cpos
, cpos
+n
);
671 memmove (cpos
, cpos
+n
, m
*TSIZE
);
673 cpos
[m
++] = erase_char
;
675 memmove (cpos
+n
, cpos
, m
*TSIZE
);
677 cpos
[n
] = erase_char
;
682 * Toggle terminal mode `mode' to state `state'. (`query' indicates
683 * whether the mode is a DEC private one or a normal one.)
685 static void toggle_mode (int mode
, int query
, int state
) {
686 if (query
) switch (mode
) {
687 case 1: /* application cursor keys */
688 app_cursor_keys
= state
;
690 case 2: /* VT52 mode */
693 case 3: /* 80/132 columns */
695 request_resize (state ?
132 : 80, rows
, 1);
698 case 5: /* reverse video */
700 seen_disp_event
= TRUE
;
701 if (state
) term_update();
703 case 6: /* DEC origin mode */
706 case 7: /* auto wrap */
709 case 8: /* auto key repeat */
712 case 25: /* enable/disable cursor */
713 compatibility(VT220
);
715 seen_disp_event
= TRUE
;
717 case 47: /* alternate screen */
718 compatibility(OTHER
);
723 } else switch (mode
) {
724 case 4: /* set insert mode */
725 compatibility(VT102
);
728 case 12: /* set echo mode */
730 * This may be very good in smcup and rmcup (or smkx & rmkx) if you
731 * have a long RTT and the telnet client/daemon doesn't understand
734 * DONT send TS_RECHO/TS_LECHO; the telnet daemon tries to fix the
735 * tty and _really_ confuses some programs.
737 compatibility(VT220
);
738 ldisc
= (state?
&ldisc_simple
: &ldisc_term
);
740 case 20: /* Return sends ... */
741 cr_lf_return
= state
;
747 * Process an OSC sequence: set window title or icon name.
749 static void do_osc(void) {
752 wordness
[(unsigned char)osc_string
[osc_strlen
]] = esc_args
[0];
754 osc_string
[osc_strlen
] = '\0';
755 switch (esc_args
[0]) {
758 set_icon (osc_string
);
759 if (esc_args
[0] == 1)
761 /* fall through: parameter 0 means set both */
764 set_title (osc_string
);
771 * Remove everything currently in `inbuf' and stick it up on the
772 * in-memory display. There's a big state machine in here to
773 * process escape sequences...
775 void term_out(void) {
778 static int beep_overload
= 0;
781 for(inbuf_reap
= 0; inbuf_reap
< inbuf_head
; inbuf_reap
++)
783 c
= inbuf
[inbuf_reap
];
786 * Optionally log the session traffic to a file. Useful for
787 * debugging and possibly also useful for actual logging.
790 static FILE *fp
= NULL
;
791 if (!fp
) fp
= fopen(logfile
, "wb");
792 if (fp
) fputc (c
, fp
);
794 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
795 * be able to display 8-bit characters, but I'll let that go 'cause
798 if( ( (c
&0x60) == 0 || c
== '\177') &&
799 termstate
< DO_CTRLS
&&
800 ( (c
&0x80) == 0 || has_compat(VT220
))) {
802 case '\005': /* terminal type query */
803 /* Strictly speaking this is VT100 but a VT100 defaults to
804 * no response. Other terminals respond at their option.
806 * Don't put a CR in the default string as this tends to
807 * upset some weird software.
809 * An xterm returns "xterm" (5 characters)
811 compatibility(OTHER
);
812 ldisc
->send ("PuTTY", 5);
816 if(beep_count
>6) beep_overload
=1;
820 if (curs_x
== 0 && curs_y
== 0)
822 else if (curs_x
== 0 && curs_y
> 0)
823 curs_x
= cols
-1, curs_y
--;
829 seen_disp_event
= TRUE
;
832 compatibility(VT100
);
836 compatibility(VT100
);
841 termstate
= VT52_ESC
;
843 compatibility(ANSIMIN
);
844 termstate
= SEEN_ESC
;
848 compatibility(VT220
);
849 termstate
= SEEN_CSI
;
851 esc_args
[0] = ARG_DEFAULT
;
855 compatibility(VT220
);
856 termstate
= SEEN_OSC
;
863 seen_disp_event
= TRUE
;
868 compatibility(VT100
);
870 if (curs_y
== marg_b
)
871 scroll (marg_t
, marg_b
, 1, TRUE
);
872 else if (curs_y
< rows
-1)
883 unsigned long *old_cpos
= cpos
;
884 unsigned long *p
= scrtop
+ curs_y
* (cols
+1) + cols
;
888 } while (curs_x
< cols
-1 && !tabs
[curs_x
]);
890 if ((*p
& LATTR_MODE
) != LATTR_NORM
)
892 if (curs_x
>= cols
/2)
902 check_selection (old_cpos
, cpos
);
904 seen_disp_event
= TRUE
;
908 else switch (termstate
) {
910 /* Only graphic characters get this far, ctrls are stripped above */
912 cpos
[1] |= ATTR_WRAPPED
;
913 if (curs_y
== marg_b
)
914 scroll (marg_t
, marg_b
, 1, TRUE
);
915 else if (curs_y
< rows
-1)
923 if (selstate
!= NO_SELECTION
)
924 check_selection (cpos
, cpos
+1);
925 switch (cset_attr
[cset
]) {
926 /* Linedraw characters are different from 'ESC ( B' only
927 * for a small range, for ones outside that range make sure
928 * we use the same font as well as the same encoding.
931 if (c
<0x60 || c
>0x7F)
932 *cpos
++ = xlat_tty2scr((unsigned char)c
) | curr_attr
|
935 *cpos
++ = ((unsigned char)c
) | curr_attr
| ATTR_LINEDRW
;
938 *cpos
++ = xlat_tty2scr((unsigned char)c
) | curr_attr
|
939 (c
<= 0x7F ? cset_attr
[cset
] : ATTR_ASCII
);
943 if (curs_x
== cols
) {
952 termstate
= TOPLEVEL
;
956 * This state is virtually identical to SEEN_ESC, with the
957 * exception that we have an OSC sequence in the pipeline,
958 * and _if_ we see a backslash, we process it.
962 termstate
= TOPLEVEL
;
965 /* else fall through */
967 termstate
= TOPLEVEL
;
969 case ' ': /* some weird sequence? */
970 compatibility(VT220
);
971 termstate
= IGNORE_NEXT
;
973 case '[': /* enter CSI mode */
974 termstate
= SEEN_CSI
;
976 esc_args
[0] = ARG_DEFAULT
;
979 case ']': /* xterm escape sequences */
980 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
981 compatibility(OTHER
);
982 termstate
= SEEN_OSC
;
985 case '(': /* should set GL */
986 compatibility(VT100
);
989 case ')': /* should set GR */
990 compatibility(VT100
);
993 case '7': /* save cursor */
994 compatibility(VT100
);
997 case '8': /* restore cursor */
998 compatibility(VT100
);
1000 seen_disp_event
= TRUE
;
1003 compatibility(VT100
);
1004 app_keypad_keys
= TRUE
;
1007 compatibility(VT100
);
1008 app_keypad_keys
= FALSE
;
1010 case 'D': /* exactly equivalent to LF */
1011 compatibility(VT100
);
1012 if (curs_y
== marg_b
)
1013 scroll (marg_t
, marg_b
, 1, TRUE
);
1014 else if (curs_y
< rows
-1)
1018 seen_disp_event
= TRUE
;
1020 case 'E': /* exactly equivalent to CR-LF */
1021 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 'M': /* reverse index - backwards LF */
1032 compatibility(VT100
);
1033 if (curs_y
== marg_t
)
1034 scroll (marg_t
, marg_b
, -1, TRUE
);
1035 else if (curs_y
> 0)
1039 seen_disp_event
= TRUE
;
1041 case 'Z': /* terminal type query */
1042 compatibility(VT100
);
1043 ldisc
->send (id_string
, strlen(id_string
));
1045 case 'c': /* restore power-on settings */
1046 compatibility(VT100
);
1049 request_resize (80, rows
, 1);
1054 seen_disp_event
= TRUE
;
1056 case '#': /* ESC # 8 fills screen with Es :-) */
1057 compatibility(VT100
);
1058 termstate
= SEEN_ESCHASH
;
1060 case 'H': /* set a tab */
1061 compatibility(VT100
);
1062 tabs
[curs_x
] = TRUE
;
1067 termstate
= TOPLEVEL
; /* default */
1070 if (esc_nargs
<= ARGS_MAX
) {
1071 if (esc_args
[esc_nargs
-1] == ARG_DEFAULT
)
1072 esc_args
[esc_nargs
-1] = 0;
1073 esc_args
[esc_nargs
-1] =
1074 10 * esc_args
[esc_nargs
-1] + c
- '0';
1076 termstate
= SEEN_CSI
;
1080 if (++esc_nargs
<= ARGS_MAX
)
1081 esc_args
[esc_nargs
-1] = ARG_DEFAULT
;
1082 termstate
= SEEN_CSI
;
1086 if( esc_query
) esc_query
= -1;
1087 else if( c
== '?' ) esc_query
= TRUE
;
1089 termstate
= SEEN_CSI
;
1091 else switch (ANSI(c
,esc_query
)) {
1092 case 'A': /* move up N lines */
1093 move (curs_x
, curs_y
- def(esc_args
[0], 1), 1);
1094 seen_disp_event
= TRUE
;
1096 case 'e': /* move down N lines */
1097 compatibility(ANSI
);
1099 move (curs_x
, curs_y
+ def(esc_args
[0], 1), 1);
1100 seen_disp_event
= TRUE
;
1102 case 'a': /* move right N cols */
1103 compatibility(ANSI
);
1105 move (curs_x
+ def(esc_args
[0], 1), curs_y
, 1);
1106 seen_disp_event
= TRUE
;
1108 case 'D': /* move left N cols */
1109 move (curs_x
- def(esc_args
[0], 1), curs_y
, 1);
1110 seen_disp_event
= TRUE
;
1112 case 'E': /* move down N lines and CR */
1113 compatibility(ANSI
);
1114 move (0, curs_y
+ def(esc_args
[0], 1), 1);
1115 seen_disp_event
= TRUE
;
1117 case 'F': /* move up N lines and CR */
1118 compatibility(ANSI
);
1119 move (0, curs_y
- def(esc_args
[0], 1), 1);
1120 seen_disp_event
= TRUE
;
1122 case 'G': case '`': /* set horizontal posn */
1123 compatibility(ANSI
);
1124 move (def(esc_args
[0], 1) - 1, curs_y
, 0);
1125 seen_disp_event
= TRUE
;
1127 case 'd': /* set vertical posn */
1128 compatibility(ANSI
);
1129 move (curs_x
, (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
1131 seen_disp_event
= TRUE
;
1133 case 'H': case 'f': /* set horz and vert posns at once */
1135 esc_args
[1] = ARG_DEFAULT
;
1136 move (def(esc_args
[1], 1) - 1,
1137 (dec_om ? marg_t
: 0) + def(esc_args
[0], 1) - 1,
1139 seen_disp_event
= TRUE
;
1141 case 'J': /* erase screen or parts of it */
1143 unsigned int i
= def(esc_args
[0], 0) + 1;
1146 erase_lots(FALSE
, !!(i
& 2), !!(i
& 1));
1149 seen_disp_event
= TRUE
;
1151 case 'K': /* erase line or parts of it */
1153 unsigned int i
= def(esc_args
[0], 0) + 1;
1156 erase_lots(TRUE
, !!(i
& 2), !!(i
& 1));
1158 seen_disp_event
= TRUE
;
1160 case 'L': /* insert lines */
1161 compatibility(VT102
);
1162 if (curs_y
<= marg_b
)
1163 scroll (curs_y
, marg_b
, -def(esc_args
[0], 1), FALSE
);
1164 seen_disp_event
= TRUE
;
1166 case 'M': /* delete lines */
1167 compatibility(VT102
);
1168 if (curs_y
<= marg_b
)
1169 scroll (curs_y
, marg_b
, def(esc_args
[0], 1), TRUE
);
1170 seen_disp_event
= TRUE
;
1172 case '@': /* insert chars */
1173 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1174 compatibility(VT102
);
1175 insch (def(esc_args
[0], 1));
1176 seen_disp_event
= TRUE
;
1178 case 'P': /* delete chars */
1179 compatibility(VT102
);
1180 insch (-def(esc_args
[0], 1));
1181 seen_disp_event
= TRUE
;
1183 case 'c': /* terminal type query */
1184 compatibility(VT100
);
1185 /* This is the response for a VT102 */
1186 ldisc
->send (id_string
, strlen(id_string
));
1188 case 'n': /* cursor position query */
1189 if (esc_args
[0] == 6) {
1191 sprintf (buf
, "\033[%d;%dR", curs_y
+ 1, curs_x
+ 1);
1192 ldisc
->send (buf
, strlen(buf
));
1194 else if (esc_args
[0] == 5) {
1195 ldisc
->send ("\033[0n", 4);
1198 case 'h': /* toggle modes to high */
1200 compatibility(VT100
);
1203 for (i
=0; i
<esc_nargs
; i
++)
1204 toggle_mode (esc_args
[i
], esc_query
, TRUE
);
1207 case 'l': /* toggle modes to low */
1209 compatibility(VT100
);
1212 for (i
=0; i
<esc_nargs
; i
++)
1213 toggle_mode (esc_args
[i
], esc_query
, FALSE
);
1216 case 'g': /* clear tabs */
1217 compatibility(VT100
);
1218 if (esc_nargs
== 1) {
1219 if (esc_args
[0] == 0) {
1220 tabs
[curs_x
] = FALSE
;
1221 } else if (esc_args
[0] == 3) {
1223 for (i
= 0; i
< cols
; i
++)
1228 case 'r': /* set scroll margins */
1229 compatibility(VT100
);
1230 if (esc_nargs
<= 2) {
1232 top
= def(esc_args
[0], 1) - 1;
1233 bot
= (esc_nargs
<= 1 || esc_args
[1] == 0 ? rows
:
1234 def(esc_args
[1], rows
)) - 1;
1237 /* VTTEST Bug 9 - if region is less than 2 lines
1238 * don't change region.
1245 * I used to think the cursor should be
1246 * placed at the top of the newly marginned
1247 * area. Apparently not: VMS TPU falls over
1250 * Well actually it should for Origin mode - RDB
1252 curs_y
= (dec_om ? marg_t
: 0);
1254 seen_disp_event
= TRUE
;
1258 case 'm': /* set graphics rendition */
1261 * A VT100 without the AVO only had one attribute, either
1262 * underline or reverse video depending on the cursor type,
1263 * this was selected by CSI 7m.
1266 * This is DIM on the VT100-AVO and VT102
1268 * This is BLINK on the VT100-AVO and VT102+
1270 * This is INVIS on the VT100-AVO and VT102
1272 * This like 22 disables BOLD, DIM and INVIS
1274 * The ANSI colours appear on any terminal that has colour
1275 * (obviously) but the interaction between sgr0 and the
1276 * colours varies but is usually related to the background
1277 * colour erase item.
1278 * The interaction between colour attributes and the mono
1279 * ones is also very implementation dependent.
1281 * The 39 and 49 attributes are likely to be unimplemented.
1284 for (i
=0; i
<esc_nargs
; i
++) {
1285 switch (def(esc_args
[i
], 0)) {
1286 case 0: /* restore defaults */
1287 curr_attr
= ATTR_DEFAULT
; break;
1288 case 1: /* enable bold */
1289 compatibility(VT100AVO
);
1290 curr_attr
|= ATTR_BOLD
; break;
1291 case 21: /* (enable double underline) */
1292 compatibility(OTHER
);
1293 case 4: /* enable underline */
1294 compatibility(VT100AVO
);
1295 curr_attr
|= ATTR_UNDER
; break;
1296 case 5: /* enable blink */
1297 compatibility(VT100AVO
);
1298 curr_attr
|= ATTR_BLINK
; break;
1299 case 7: /* enable reverse video */
1300 curr_attr
|= ATTR_REVERSE
; break;
1301 case 22: /* disable bold */
1302 compatibility(VT220
);
1303 curr_attr
&= ~ATTR_BOLD
; break;
1304 case 24: /* disable underline */
1305 compatibility(VT220
);
1306 curr_attr
&= ~ATTR_UNDER
; break;
1307 case 25: /* disable blink */
1308 compatibility(VT220
);
1309 curr_attr
&= ~ATTR_BLINK
; break;
1310 case 27: /* disable reverse video */
1311 compatibility(VT220
);
1312 curr_attr
&= ~ATTR_REVERSE
; break;
1313 case 30: case 31: case 32: case 33:
1314 case 34: case 35: case 36: case 37:
1316 curr_attr
&= ~ATTR_FGMASK
;
1317 curr_attr
|= (esc_args
[i
] - 30) << ATTR_FGSHIFT
;
1319 case 39: /* default-foreground */
1320 curr_attr
&= ~ATTR_FGMASK
;
1321 curr_attr
|= ATTR_DEFFG
;
1323 case 40: case 41: case 42: case 43:
1324 case 44: case 45: case 46: case 47:
1326 curr_attr
&= ~ATTR_BGMASK
;
1327 curr_attr
|= (esc_args
[i
] - 40) << ATTR_BGSHIFT
;
1329 case 49: /* default-background */
1330 curr_attr
&= ~ATTR_BGMASK
;
1331 curr_attr
|= ATTR_DEFBG
;
1338 (curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
|ATTR_BLINK
))
1342 case 's': /* save cursor */
1345 case 'u': /* restore cursor */
1346 save_cursor (FALSE
);
1347 seen_disp_event
= TRUE
;
1349 case 't': /* set page size - ie window height */
1351 * VT340/VT420 sequence DECSLPP, DEC only allows values
1352 * 24/25/36/48/72/144 other emulators (eg dtterm) use
1353 * illegal values (eg first arg 1..9) for window changing
1356 compatibility(VT340TEXT
);
1357 if (esc_nargs
<=1 && (esc_args
[0]<1 || esc_args
[0]>=24)) {
1358 request_resize (cols
, def(esc_args
[0], 24), 0);
1362 case ANSI('|', '*'):
1363 /* VT420 sequence DECSNLS
1364 * Set number of lines on screen
1365 * VT420 uses VGA like hardware and can support any size in
1366 * reasonable range (24..49 AIUI) with no default specified.
1368 compatibility(VT420
);
1369 if (esc_nargs
==1 && esc_args
[0]>0) {
1370 request_resize (cols
, def(esc_args
[0], cfg
.height
), 0);
1374 case ANSI('|', '$'):
1375 /* VT340/VT420 sequence DECSCPP
1376 * Set number of columns per page
1377 * Docs imply range is only 80 or 132, but I'll allow any.
1379 compatibility(VT340TEXT
);
1381 request_resize (def(esc_args
[0], cfg
.width
), rows
, 0);
1385 case 'X': /* write N spaces w/o moving cursor */
1386 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
1387 compatibility(ANSIMIN
);
1389 int n
= def(esc_args
[0], 1);
1390 unsigned long *p
= cpos
;
1391 if (n
> cols
- curs_x
)
1393 check_selection (cpos
, cpos
+n
);
1396 seen_disp_event
= TRUE
;
1399 case 'x': /* report terminal characteristics */
1400 compatibility(VT100
);
1403 int i
= def(esc_args
[0], 0);
1404 if (i
== 0 || i
== 1) {
1405 strcpy (buf
, "\033[2;1;1;112;112;1;0x");
1407 ldisc
->send (buf
, 20);
1412 compatibility(OTHER
);
1413 use_bce
= (esc_args
[0]<=0);
1414 erase_char
= ERASE_CHAR
;
1416 erase_char
= (' '|(curr_attr
&(ATTR_FGMASK
|ATTR_BGMASK
)));
1419 compatibility(OTHER
);
1420 blink_is_real
= (esc_args
[0]>=1);
1423 /* Allow the host to make this emulator a 'perfect' VT102.
1424 * This first appeared in the VT220, but we do need to get
1425 * back to PuTTY mode so I won't check it.
1427 * The arg == 60 is a PuTTY extension.
1428 * The 2nd arg, 8bit vs 7bit is not obeyed.
1430 * Setting VT102 mode should also change the Fkeys to
1431 * generate PF* codes as a real VT102 has no Fkeys.
1432 * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
1435 * Note ESC c will NOT change this!
1438 if (esc_args
[0] == 61) compatibility_level
= TM_VT102
;
1439 else if (esc_args
[0] == 60) compatibility_level
= TM_ANSIMIN
;
1440 else compatibility_level
= TM_PUTTY
;
1446 /* VT100 only here, checked above */
1449 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_GBCHR
;
1452 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_LINEDRW
;
1455 default: /* specifically, 'B' */
1456 cset_attr
[termstate
== SET_GL ?
0 : 1] = ATTR_ASCII
;
1459 if( !has_compat(VT220
) || c
!= '%' )
1460 termstate
= TOPLEVEL
;
1465 case 'P': /* Linux palette sequence */
1466 termstate
= SEEN_OSC_P
;
1469 case 'R': /* Linux palette reset */
1472 termstate
= TOPLEVEL
;
1474 case 'W': /* word-set */
1475 termstate
= SEEN_OSC_W
;
1478 case '0': case '1': case '2': case '3': case '4':
1479 case '5': case '6': case '7': case '8': case '9':
1480 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1484 * Grotty hack to support xterm and DECterm title
1485 * sequences concurrently.
1487 if (esc_args
[0] == 2) {
1491 /* else fall through */
1493 termstate
= OSC_STRING
;
1499 * This OSC stuff is EVIL. It takes just one character to get into
1500 * sysline mode and it's not initially obvious how to get out.
1501 * So I've added CR and LF as string aborts.
1502 * This shouldn't effect compatibility as I believe embedded
1503 * control characters are supposed to be interpreted (maybe?)
1504 * and they don't display anything useful anyway.
1508 if (c
== '\n' || c
== '\r') {
1509 termstate
= TOPLEVEL
;
1510 } else if (c
== 0234 || c
== '\007' ) {
1512 * These characters terminate the string; ST and BEL
1513 * terminate the sequence and trigger instant
1514 * processing of it, whereas ESC goes back to SEEN_ESC
1515 * mode unless it is followed by \, in which case it is
1516 * synonymous with ST in the first place.
1519 termstate
= TOPLEVEL
;
1520 } else if (c
== '\033')
1521 termstate
= OSC_MAYBE_ST
;
1522 else if (osc_strlen
< OSC_STR_MAX
)
1523 osc_string
[osc_strlen
++] = c
;
1527 int max
= (osc_strlen
== 0 ?
21 : 16);
1529 if (c
>= '0' && c
<= '9')
1531 else if (c
>= 'A' && c
<= 'A'+max
-10)
1533 else if (c
>= 'a' && c
<= 'a'+max
-10)
1536 termstate
= TOPLEVEL
;
1537 osc_string
[osc_strlen
++] = val
;
1538 if (osc_strlen
>= 7) {
1539 palette_set (osc_string
[0],
1540 osc_string
[1] * 16 + osc_string
[2],
1541 osc_string
[3] * 16 + osc_string
[4],
1542 osc_string
[5] * 16 + osc_string
[6]);
1544 termstate
= TOPLEVEL
;
1550 case '0': case '1': case '2': case '3': case '4':
1551 case '5': case '6': case '7': case '8': case '9':
1552 esc_args
[0] = 10 * esc_args
[0] + c
- '0';
1555 termstate
= OSC_STRING
;
1562 unsigned long nlattr
;
1568 n
= rows
* (cols
+1);
1570 *p
++ = ATTR_DEFAULT
| 'E';
1572 seen_disp_event
= TRUE
;
1573 check_selection (scrtop
, scrtop
+ rows
* (cols
+1));
1576 case '3': nlattr
= LATTR_TOP
; if(0) {
1577 case '4': nlattr
= LATTR_BOT
; } if(0) {
1578 case '5': nlattr
= LATTR_NORM
; } if(0) {
1579 case '6': nlattr
= LATTR_WIDE
; }
1581 p
= scrtop
+ curs_y
* (cols
+1) + cols
;
1586 termstate
= TOPLEVEL
;
1589 termstate
= TOPLEVEL
;
1590 seen_disp_event
= TRUE
;
1593 move (curs_x
, curs_y
- 1, 1);
1596 move (curs_x
, curs_y
+ 1, 1);
1599 move (curs_x
+ 1, curs_y
, 1);
1602 move (curs_x
- 1, curs_y
, 1);
1605 cset_attr
[cset
=0] = ATTR_LINEDRW
;
1608 cset_attr
[cset
=0] = ATTR_ASCII
;
1615 scroll (0, rows
-1, -1, TRUE
);
1616 else if (curs_y
> 0)
1622 erase_lots(FALSE
, FALSE
, TRUE
);
1626 erase_lots(TRUE
, FALSE
, TRUE
);
1629 /* XXX Print cursor line */
1632 /* XXX Start controller mode */
1635 /* XXX Stop controller mode */
1638 termstate
= VT52_Y1
;
1641 ldisc
->send ("\033/Z", 3);
1644 app_cursor_keys
= TRUE
;
1647 app_cursor_keys
= FALSE
;
1650 /* XXX This should switch to VT100 mode not current or default
1651 * VT mode. But this will only have effect in a VT220+
1657 /* XXX Enter auto print mode */
1660 /* XXX Exit auto print mode */
1663 /* XXX Print screen */
1668 termstate
= VT52_Y2
;
1669 move(curs_x
, c
-' ', 0);
1672 termstate
= TOPLEVEL
;
1673 move(c
-' ', curs_y
, 0);
1676 if (selstate
!= NO_SELECTION
)
1677 check_selection (cpos
, cpos
+1);
1683 if(!beep_count
) beep_overload
=0;
1685 else if(beep_count
&& beep_count
<5 && cfg
.beep
)
1690 * Compare two lines to determine whether they are sufficiently
1691 * alike to scroll-optimise one to the other. Return the degree of
1694 static int linecmp (unsigned long *a
, unsigned long *b
) {
1697 for (i
=n
=0; i
< cols
; i
++)
1698 n
+= (*a
++ == *b
++);
1703 * Given a context, update the window. Out of paranoia, we don't
1704 * allow WM_PAINT responses to do scrolling optimisations.
1706 static void do_paint (Context ctx
, int may_optimise
){
1707 int i
, j
, start
, our_curs_y
;
1708 unsigned long attr
, rv
, cursor
;
1713 if (blinker
|| !cfg
.blink_cur
)
1714 cursor
= ATTR_ACTCURS
;
1719 cursor
= ATTR_PASCURS
;
1722 rv
= (rvideo ? ATTR_REVERSE
: 0);
1723 our_curs_y
= curs_y
+ (scrtop
- disptop
) / (cols
+1);
1725 for (i
=0; i
<rows
; i
++) {
1726 int idx
= i
*(cols
+1);
1727 int lattr
= (disptop
[idx
+cols
] & LATTR_MODE
);
1728 for (j
=0; j
<=cols
; j
++,idx
++) {
1729 unsigned long *d
= disptop
+idx
;
1730 wanttext
[idx
] = lattr
| ((*d
^ rv
1731 ^ (selstart
<= d
&& d
< selend ?
1732 ATTR_REVERSE
: 0)) |
1733 (i
==our_curs_y
&& j
==curs_x ? cursor
: 0));
1735 if (blink_is_real
) {
1736 if (has_focus
&& tblinker
&& (wanttext
[idx
]&ATTR_BLINK
) )
1738 wanttext
[idx
] &= ATTR_MASK
;
1739 wanttext
[idx
] += ' ';
1741 wanttext
[idx
] &= ~ATTR_BLINK
;
1747 * We would perform scrolling optimisations in here, if they
1748 * didn't have a nasty tendency to cause the whole sodding
1749 * program to hang for a second at speed-critical moments.
1750 * We'll leave it well alone...
1753 for (i
=0; i
<rows
; i
++) {
1754 int idx
= i
*(cols
+1);
1755 int lattr
= (wanttext
[idx
+cols
] & LATTR_MODE
);
1757 for (j
=0; j
<=cols
; j
++,idx
++) {
1758 unsigned long t
= wanttext
[idx
];
1759 int needs_update
= (j
< cols
&& t
!= disptext
[idx
]);
1760 int keep_going
= (start
!= -1 && needs_update
&&
1761 (t
& ATTR_MASK
) == attr
&&
1762 j
-start
< sizeof(ch
));
1763 if (start
!= -1 && !keep_going
) {
1764 do_text (ctx
, start
, i
, ch
, j
-start
, attr
, lattr
);
1770 attr
= t
& ATTR_MASK
;
1772 ch
[j
-start
] = (char) (t
& CHAR_MASK
);
1780 * Flick the switch that says if blinking things should be shown or hidden.
1783 void term_blink(int flg
) {
1784 static long last_blink
= 0;
1785 static long last_tblink
= 0;
1786 long now
, blink_diff
;
1788 now
= GetTickCount();
1789 blink_diff
= now
-last_tblink
;
1791 /* Make sure the text blinks no more than 2Hz */
1792 if (blink_diff
<0 || blink_diff
>450)
1795 tblinker
= !tblinker
;
1804 blink_diff
= now
-last_blink
;
1806 /* Make sure the cursor blinks no more than 2Hz */
1807 if (blink_diff
>=0 && blink_diff
<450)
1815 * Invalidate the whole screen so it will be repainted in full.
1817 void term_invalidate(void) {
1820 for (i
=0; i
<rows
*(cols
+1); i
++)
1821 disptext
[i
] = ATTR_INVALID
;
1825 * Paint the window in response to a WM_PAINT message.
1827 void term_paint (Context ctx
, int l
, int t
, int r
, int b
) {
1828 int i
, j
, left
, top
, right
, bottom
;
1830 left
= l
/ font_width
;
1831 right
= (r
- 1) / font_width
;
1832 top
= t
/ font_height
;
1833 bottom
= (b
- 1) / font_height
;
1834 for (i
= top
; i
<= bottom
&& i
< rows
; i
++)
1836 if ( (disptext
[i
*(cols
+1)+cols
]&LATTR_MODE
) == LATTR_NORM
)
1837 for (j
= left
; j
<= right
&& j
< cols
; j
++)
1838 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
1840 for (j
= left
/2; j
<= right
/2+1 && j
< cols
; j
++)
1841 disptext
[i
*(cols
+1)+j
] = ATTR_INVALID
;
1844 /* This should happen soon enough, also for some reason it sometimes
1845 * fails to actually do anything when re-sizing ... painting the wrong
1847 do_paint (ctx, FALSE);
1852 * Attempt to scroll the scrollback. The second parameter gives the
1853 * position we want to scroll to; the first is +1 to denote that
1854 * this position is relative to the beginning of the scrollback, -1
1855 * to denote it is relative to the end, and 0 to denote that it is
1856 * relative to the current position.
1858 void term_scroll (int rel
, int where
) {
1859 int n
= where
* (cols
+1);
1861 disptop
= (rel
< 0 ? scrtop
:
1862 rel
> 0 ? sbtop
: disptop
) + n
;
1863 if (disptop
< sbtop
)
1865 if (disptop
> scrtop
)
1872 * Spread the selection outwards according to the selection mode.
1874 static unsigned long *sel_spread_half (unsigned long *p
, int dir
) {
1875 unsigned long *linestart
, *lineend
;
1879 x
= (p
- text
) % (cols
+1);
1881 lineend
= linestart
+ cols
;
1886 * In this mode, every character is a separate unit, except
1887 * for runs of spaces at the end of a non-wrapping line.
1889 if (!(linestart
[cols
] & ATTR_WRAPPED
)) {
1890 unsigned long *q
= lineend
;
1891 while (q
> linestart
&& (q
[-1] & CHAR_MASK
) == 0x20)
1896 p
= (dir
== -1 ? q
: lineend
- 1);
1901 * In this mode, the units are maximal runs of characters
1902 * whose `wordness' has the same value.
1904 wvalue
= wordness
[*p
& CHAR_MASK
];
1906 while (p
< lineend
&& wordness
[p
[1] & CHAR_MASK
] == wvalue
)
1909 while (p
> linestart
&& wordness
[p
[-1] & CHAR_MASK
] == wvalue
)
1915 * In this mode, every line is a unit.
1917 p
= (dir
== -1 ? linestart
: lineend
- 1);
1923 static void sel_spread (void) {
1924 selstart
= sel_spread_half (selstart
, -1);
1925 selend
= sel_spread_half (selend
- 1, +1) + 1;
1928 void term_mouse (Mouse_Button b
, Mouse_Action a
, int x
, int y
) {
1929 unsigned long *selpoint
;
1932 if (y
>=rows
) y
= rows
-1;
1940 if (x
>=cols
) x
= cols
-1;
1942 selpoint
= disptop
+ y
* (cols
+1);
1943 if ((selpoint
[cols
]&LATTR_MODE
) != LATTR_NORM
)
1948 if (b
== MB_SELECT
&& a
== MA_CLICK
) {
1950 selstate
= ABOUT_TO
;
1951 selanchor
= selpoint
;
1953 } else if (b
== MB_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
1955 selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
1956 selstate
= DRAGGING
;
1957 selstart
= selanchor
= selpoint
;
1958 selend
= selstart
+ 1;
1960 } else if ((b
== MB_SELECT
&& a
== MA_DRAG
) ||
1961 (b
== MB_EXTEND
&& a
!= MA_RELEASE
)) {
1962 if (selstate
== ABOUT_TO
&& selanchor
== selpoint
)
1964 if (b
== MB_EXTEND
&& a
!= MA_DRAG
&& selstate
== SELECTED
) {
1965 if (selpoint
-selstart
< (selend
-selstart
)/2)
1966 selanchor
= selend
- 1;
1968 selanchor
= selstart
;
1969 selstate
= DRAGGING
;
1971 if (selstate
!= ABOUT_TO
&& selstate
!= DRAGGING
)
1972 selanchor
= selpoint
;
1973 selstate
= DRAGGING
;
1974 if (selpoint
< selanchor
) {
1975 selstart
= selpoint
;
1976 selend
= selanchor
+ 1;
1978 selstart
= selanchor
;
1979 selend
= selpoint
+ 1;
1982 } else if ((b
== MB_SELECT
|| b
== MB_EXTEND
) && a
== MA_RELEASE
) {
1983 if (selstate
== DRAGGING
) {
1985 * We've completed a selection. We now transfer the
1986 * data to the clipboard.
1988 unsigned char *p
= selspace
;
1989 unsigned long *q
= selstart
;
1991 while (q
< selend
) {
1993 unsigned long *lineend
= q
- (q
-text
) % (cols
+1) + cols
;
1994 unsigned long *nlpos
= lineend
;
1996 if (!(*nlpos
& ATTR_WRAPPED
)) {
1997 while ((nlpos
[-1] & CHAR_MASK
) == 0x20 && nlpos
> q
)
2002 while (q
< nlpos
&& q
< selend
)
2003 *p
++ = (unsigned char) (*q
++ & CHAR_MASK
);
2006 for (i
=0; i
<sizeof(sel_nl
); i
++)
2009 q
= lineend
+ 1; /* start of next line */
2011 write_clip (selspace
, p
- selspace
, FALSE
);
2012 selstate
= SELECTED
;
2014 selstate
= NO_SELECTION
;
2015 } else if (b
== MB_PASTE
&& (a
==MA_CLICK
|| a
==MA_2CLK
|| a
==MA_3CLK
)) {
2019 get_clip((void **) &data
, &len
);
2023 if (paste_buffer
) sfree(paste_buffer
);
2024 paste_pos
= paste_hold
= paste_len
= 0;
2025 paste_buffer
= smalloc(len
);
2028 while (p
< data
+len
) {
2029 while (p
< data
+len
&&
2030 !(p
<= data
+len
-sizeof(sel_nl
) &&
2031 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
2039 c
=xlat_kbd2tty(q
[i
]);
2040 paste_buffer
[paste_len
++] = c
;
2044 if (p
<= data
+len
-sizeof(sel_nl
) &&
2045 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
2046 paste_buffer
[paste_len
++] = '\r';
2047 p
+= sizeof(sel_nl
);
2052 /* Assume a small paste will be OK in one go. */
2053 if (paste_len
<256) {
2054 ldisc
->send (paste_buffer
, paste_len
);
2055 if (paste_buffer
) sfree(paste_buffer
);
2057 paste_pos
= paste_hold
= paste_len
= 0;
2060 get_clip(NULL
, NULL
);
2066 void term_nopaste() {
2067 if(paste_len
== 0) return;
2068 sfree(paste_buffer
);
2074 static long last_paste
= 0;
2075 long now
, paste_diff
;
2077 if(paste_len
== 0) return;
2079 /* Don't wait forever to paste */
2081 now
= GetTickCount();
2082 paste_diff
= now
-last_paste
;
2083 if (paste_diff
>=0 && paste_diff
<450)
2088 while(paste_pos
<paste_len
)
2090 char c
= paste_buffer
[paste_pos
++];
2091 ldisc
->send (&c
, 1);
2098 sfree(paste_buffer
);
2103 static void deselect (void) {
2104 selstate
= NO_SELECTION
;
2105 selstart
= selend
= scrtop
;
2108 void term_deselect (void) {