10 #define poslt(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x < (p2).x ) )
11 #define posle(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x <= (p2).x ) )
12 #define poseq(p1,p2) ( (p1).y == (p2).y && (p1).x == (p2).x )
13 #define posdiff(p1,p2) ( ((p1).y - (p2).y) * (term->cols+1) + (p1).x - (p2).x )
15 /* Product-order comparisons for rectangular block selection. */
16 #define posPlt(p1,p2) ( (p1).y <= (p2).y && (p1).x < (p2).x )
17 #define posPle(p1,p2) ( (p1).y <= (p2).y && (p1).x <= (p2).x )
19 #define incpos(p) ( (p).x == term->cols ? ((p).x = 0, (p).y++, 1) : ((p).x++, 0) )
20 #define decpos(p) ( (p).x == 0 ? ((p).x = term->cols, (p).y--, 1) : ((p).x--, 0) )
24 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
25 #define CL_VT100 0x0002 /* VT100 */
26 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
27 #define CL_VT102 0x0008 /* VT102 */
28 #define CL_VT220 0x0010 /* VT220 */
29 #define CL_VT320 0x0020 /* VT320 */
30 #define CL_VT420 0x0040 /* VT420 */
31 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
32 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
33 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
34 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
35 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
37 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
38 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
39 #define TM_VT102 (TM_VT100AVO|CL_VT102)
40 #define TM_VT220 (TM_VT102|CL_VT220)
41 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
42 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
44 #define TM_PUTTY (0xFFFF)
46 #define UPDATE_DELAY ((TICKSPERSEC+49)/50)/* ticks to defer window update */
47 #define TBLINK_DELAY ((TICKSPERSEC*9+19)/20)/* ticks between text blinks*/
48 #define CBLINK_DELAY (CURSORBLINK) /* ticks between cursor blinks */
49 #define VBELL_DELAY (VBELL_TIMEOUT) /* visual bell timeout in ticks */
51 #define compatibility(x) \
52 if ( ((CL_##x)&term->compatibility_level) == 0 ) { \
53 term->termstate=TOPLEVEL; \
56 #define compatibility2(x,y) \
57 if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \
58 term->termstate=TOPLEVEL; \
62 #define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 )
64 const char sco2ansicolour
[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
66 #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
67 const wchar_t sel_nl
[] = SEL_NL
;
70 * Fetch the character at a particular position in a line array,
71 * for purposes of `wordtype'. The reason this isn't just a simple
72 * array reference is that if the character we find is UCSWIDE,
73 * then we must look one space further to the left.
75 #define UCSGET(a, x) \
76 ( (x)>0 && (a)[(x)].chr == UCSWIDE ? (a)[(x)-1].chr : (a)[(x)].chr )
79 * Detect the various aliases of U+0020 SPACE.
81 #define IS_SPACE_CHR(chr) \
82 ((chr) == 0x20 || (DIRECT_CHAR(chr) && ((chr) & 0xFF) == 0x20))
87 #define CSET_OF(chr) (DIRECT_CHAR(chr)||DIRECT_FONT(chr) ? (chr)&CSET_MASK : 0)
90 * Internal prototypes.
92 static void resizeline(Terminal
*, termline
*, int);
93 static termline
*lineptr(Terminal
*, int, int, int);
94 static void unlineptr(termline
*);
95 static void do_paint(Terminal
*, Context
, int);
96 static void erase_lots(Terminal
*, int, int, int);
97 static int find_last_nonempty_line(Terminal
*, tree234
*);
98 static void swap_screen(Terminal
*, int, int, int);
99 static void update_sbar(Terminal
*);
100 static void deselect(Terminal
*);
101 static void term_print_finish(Terminal
*);
102 static void scroll(Terminal
*, int, int, int, int);
103 #ifdef OPTIMISE_SCROLL
104 static void scroll_display(Terminal
*, int, int, int);
105 #endif /* OPTIMISE_SCROLL */
107 static termline
*newline(Terminal
*term
, int cols
, int bce
)
112 line
= snew(termline
);
113 line
->chars
= snewn(cols
, termchar
);
114 for (j
= 0; j
< cols
; j
++)
115 line
->chars
[j
] = (bce ? term
->erase_char
: term
->basic_erase_char
);
116 line
->cols
= line
->size
= cols
;
117 line
->lattr
= LATTR_NORM
;
118 line
->temporary
= FALSE
;
124 static void freeline(termline
*line
)
132 static void unlineptr(termline
*line
)
140 * Diagnostic function: verify that a termline has a correct
141 * combining character structure.
143 * This is a performance-intensive check, so it's no longer enabled
146 static void cc_check(termline
*line
)
148 unsigned char *flags
;
151 assert(line
->size
>= line
->cols
);
153 flags
= snewn(line
->size
, unsigned char);
155 for (i
= 0; i
< line
->size
; i
++)
156 flags
[i
] = (i
< line
->cols
);
158 for (i
= 0; i
< line
->cols
; i
++) {
160 while (line
->chars
[j
].cc_next
) {
161 j
+= line
->chars
[j
].cc_next
;
162 assert(j
>= line
->cols
&& j
< line
->size
);
171 assert(j
>= line
->cols
&& j
< line
->size
);
174 if (line
->chars
[j
].cc_next
)
175 j
+= line
->chars
[j
].cc_next
;
182 for (i
= 0; i
< line
->size
; i
++)
183 j
+= (flags
[i
] != 0);
185 assert(j
== line
->size
);
192 * Add a combining character to a character cell.
194 static void add_cc(termline
*line
, int col
, unsigned long chr
)
198 assert(col
>= 0 && col
< line
->cols
);
201 * Start by extending the cols array if the free list is empty.
203 if (!line
->cc_free
) {
205 line
->size
+= 16 + (line
->size
- line
->cols
) / 2;
206 line
->chars
= sresize(line
->chars
, line
->size
, termchar
);
208 while (n
< line
->size
) {
209 if (n
+1 < line
->size
)
210 line
->chars
[n
].cc_next
= 1;
212 line
->chars
[n
].cc_next
= 0;
218 * Now walk the cc list of the cell in question.
220 while (line
->chars
[col
].cc_next
)
221 col
+= line
->chars
[col
].cc_next
;
224 * `col' now points at the last cc currently in this cell; so
225 * we simply add another one.
227 newcc
= line
->cc_free
;
228 if (line
->chars
[newcc
].cc_next
)
229 line
->cc_free
= newcc
+ line
->chars
[newcc
].cc_next
;
232 line
->chars
[newcc
].cc_next
= 0;
233 line
->chars
[newcc
].chr
= chr
;
234 line
->chars
[col
].cc_next
= newcc
- col
;
242 * Clear the combining character list in a character cell.
244 static void clear_cc(termline
*line
, int col
)
246 int oldfree
, origcol
= col
;
248 assert(col
>= 0 && col
< line
->cols
);
250 if (!line
->chars
[col
].cc_next
)
251 return; /* nothing needs doing */
253 oldfree
= line
->cc_free
;
254 line
->cc_free
= col
+ line
->chars
[col
].cc_next
;
255 while (line
->chars
[col
].cc_next
)
256 col
+= line
->chars
[col
].cc_next
;
258 line
->chars
[col
].cc_next
= oldfree
- col
;
260 line
->chars
[col
].cc_next
= 0;
262 line
->chars
[origcol
].cc_next
= 0;
270 * Compare two character cells for equality. Special case required
271 * in do_paint() where we override what we expect the chr and attr
274 static int termchars_equal_override(termchar
*a
, termchar
*b
,
275 unsigned long bchr
, unsigned long battr
)
280 if ((a
->attr
&~ DATTR_MASK
) != (battr
&~ DATTR_MASK
))
282 while (a
->cc_next
|| b
->cc_next
) {
283 if (!a
->cc_next
|| !b
->cc_next
)
284 return FALSE
; /* one cc-list ends, other does not */
287 if (a
->chr
!= b
->chr
)
293 static int termchars_equal(termchar
*a
, termchar
*b
)
295 return termchars_equal_override(a
, b
, b
->chr
, b
->attr
);
299 * Copy a character cell. (Requires a pointer to the destination
300 * termline, so as to access its free list.)
302 static void copy_termchar(termline
*destline
, int x
, termchar
*src
)
304 clear_cc(destline
, x
);
306 destline
->chars
[x
] = *src
; /* copy everything except cc-list */
307 destline
->chars
[x
].cc_next
= 0; /* and make sure this is zero */
309 while (src
->cc_next
) {
311 add_cc(destline
, x
, src
->chr
);
320 * Move a character cell within its termline.
322 static void move_termchar(termline
*line
, termchar
*dest
, termchar
*src
)
324 /* First clear the cc list from the original char, just in case. */
325 clear_cc(line
, dest
- line
->chars
);
327 /* Move the character cell and adjust its cc_next. */
328 *dest
= *src
; /* copy everything except cc-list */
330 dest
->cc_next
= src
->cc_next
- (dest
-src
);
332 /* Ensure the original cell doesn't have a cc list. */
341 * Compress and decompress a termline into an RLE-based format for
342 * storing in scrollback. (Since scrollback almost never needs to
343 * be modified and exists in huge quantities, this is a sensible
344 * tradeoff, particularly since it allows us to continue adding
345 * features to the main termchar structure without proportionally
346 * bloating the terminal emulator's memory footprint unless those
347 * features are in constant use.)
353 static void add(struct buf
*b
, unsigned char c
)
355 if (b
->len
>= b
->size
) {
356 b
->size
= (b
->len
* 3 / 2) + 512;
357 b
->data
= sresize(b
->data
, b
->size
, unsigned char);
359 b
->data
[b
->len
++] = c
;
361 static int get(struct buf
*b
)
363 return b
->data
[b
->len
++];
365 static void makerle(struct buf
*b
, termline
*ldata
,
366 void (*makeliteral
)(struct buf
*b
, termchar
*c
,
367 unsigned long *state
))
369 int hdrpos
, hdrsize
, n
, prevlen
, prevpos
, thislen
, thispos
, prev2
;
370 termchar
*c
= ldata
->chars
;
371 unsigned long state
= 0, oldstate
;
378 prevlen
= prevpos
= 0;
383 makeliteral(b
, c
++, &state
);
384 thislen
= b
->len
- thispos
;
385 if (thislen
== prevlen
&&
386 !memcmp(b
->data
+ prevpos
, b
->data
+ thispos
, thislen
)) {
388 * This literal precisely matches the previous one.
389 * Turn it into a run if it's worthwhile.
391 * With one-byte literals, it costs us two bytes to
392 * encode a run, plus another byte to write the header
393 * to resume normal output; so a three-element run is
394 * neutral, and anything beyond that is unconditionally
395 * worthwhile. With two-byte literals or more, even a
398 if (thislen
> 1 || prev2
) {
402 * It's worth encoding a run. Start at prevpos,
403 * unless hdrsize==0 in which case we can back up
404 * another one and start by overwriting hdrpos.
407 hdrsize
--; /* remove the literal at prevpos */
411 prevpos
-= prevlen
;/* and possibly another one */
415 assert(prevpos
== hdrpos
+ 1);
417 b
->len
= prevpos
+prevlen
;
419 memmove(b
->data
+ prevpos
+1, b
->data
+ prevpos
, prevlen
);
421 b
->len
= prevpos
+prevlen
+1;
423 * Terminate the previous run of ordinary
426 assert(hdrsize
>= 1 && hdrsize
<= 128);
427 b
->data
[hdrpos
] = hdrsize
- 1;
430 runlen
= prev2 ?
3 : 2;
432 while (n
> 0 && runlen
< 129) {
436 makeliteral(b
, c
, &state
);
437 tmplen
= b
->len
- tmppos
;
439 if (tmplen
!= thislen
||
440 memcmp(b
->data
+ runpos
+1, b
->data
+ tmppos
, tmplen
)) {
442 break; /* run over */
447 assert(runlen
>= 2 && runlen
<= 129);
448 b
->data
[runpos
] = runlen
+ 0x80 - 2;
453 /* And ensure this run doesn't interfere with the next. */
454 prevlen
= prevpos
= 0;
460 * Just flag that the previous two literals were
461 * identical, in case we find a third identical one
462 * we want to turn into a run.
475 * This character isn't (yet) part of a run. Add it to
479 if (hdrsize
== 128) {
480 b
->data
[hdrpos
] = hdrsize
- 1;
484 prevlen
= prevpos
= 0;
493 assert(hdrsize
<= 128);
494 b
->data
[hdrpos
] = hdrsize
- 1;
499 static void makeliteral_chr(struct buf
*b
, termchar
*c
, unsigned long *state
)
502 * My encoding for characters is UTF-8-like, in that it stores
503 * 7-bit ASCII in one byte and uses high-bit-set bytes as
504 * introducers to indicate a longer sequence. However, it's
505 * unlike UTF-8 in that it doesn't need to be able to
506 * resynchronise, and therefore I don't want to waste two bits
507 * per byte on having recognisable continuation characters.
508 * Also I don't want to rule out the possibility that I may one
509 * day use values 0x80000000-0xFFFFFFFF for interesting
510 * purposes, so unlike UTF-8 I need a full 32-bit range.
511 * Accordingly, here is my encoding:
513 * 00000000-0000007F: 0xxxxxxx (but see below)
514 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
515 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
516 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
517 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
519 * (`Z' is like `x' but is always going to be zero since the
520 * values I'm encoding don't go above 2^32. In principle the
521 * five-byte form of the encoding could extend to 2^35, and
522 * there could be six-, seven-, eight- and nine-byte forms as
523 * well to allow up to 64-bit values to be encoded. But that's
524 * completely unnecessary for these purposes!)
526 * The encoding as written above would be very simple, except
527 * that 7-bit ASCII can occur in several different ways in the
528 * terminal data; sometimes it crops up in the D800 page
529 * (CSET_ASCII) but at other times it's in the 0000 page (real
530 * Unicode). Therefore, this encoding is actually _stateful_:
531 * the one-byte encoding of 00-7F actually indicates `reuse the
532 * upper three bytes of the last character', and to encode an
533 * absolute value of 00-7F you need to use the two-byte form
536 if ((c
->chr
& ~0x7F) == *state
) {
537 add(b
, (unsigned char)(c
->chr
& 0x7F));
538 } else if (c
->chr
< 0x4000) {
539 add(b
, (unsigned char)(((c
->chr
>> 8) & 0x3F) | 0x80));
540 add(b
, (unsigned char)(c
->chr
& 0xFF));
541 } else if (c
->chr
< 0x200000) {
542 add(b
, (unsigned char)(((c
->chr
>> 16) & 0x1F) | 0xC0));
543 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
544 add(b
, (unsigned char)(c
->chr
& 0xFF));
545 } else if (c
->chr
< 0x10000000) {
546 add(b
, (unsigned char)(((c
->chr
>> 24) & 0x0F) | 0xE0));
547 add(b
, (unsigned char)((c
->chr
>> 16) & 0xFF));
548 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
549 add(b
, (unsigned char)(c
->chr
& 0xFF));
552 add(b
, (unsigned char)((c
->chr
>> 24) & 0xFF));
553 add(b
, (unsigned char)((c
->chr
>> 16) & 0xFF));
554 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
555 add(b
, (unsigned char)(c
->chr
& 0xFF));
557 *state
= c
->chr
& ~0xFF;
559 static void makeliteral_attr(struct buf
*b
, termchar
*c
, unsigned long *state
)
562 * My encoding for attributes is 16-bit-granular and assumes
563 * that the top bit of the word is never required. I either
564 * store a two-byte value with the top bit clear (indicating
565 * just that value), or a four-byte value with the top bit set
566 * (indicating the same value with its top bit clear).
568 * However, first I permute the bits of the attribute value, so
569 * that the eight bits of colour (four in each of fg and bg)
570 * which are never non-zero unless xterm 256-colour mode is in
571 * use are placed higher up the word than everything else. This
572 * ensures that attribute values remain 16-bit _unless_ the
573 * user uses extended colour.
575 unsigned attr
, colourbits
;
579 assert(ATTR_BGSHIFT
> ATTR_FGSHIFT
);
581 colourbits
= (attr
>> (ATTR_BGSHIFT
+ 4)) & 0xF;
583 colourbits
|= (attr
>> (ATTR_FGSHIFT
+ 4)) & 0xF;
585 attr
= (((attr
>> (ATTR_BGSHIFT
+ 8)) << (ATTR_BGSHIFT
+ 4)) |
586 (attr
& ((1 << (ATTR_BGSHIFT
+ 4))-1)));
587 attr
= (((attr
>> (ATTR_FGSHIFT
+ 8)) << (ATTR_FGSHIFT
+ 4)) |
588 (attr
& ((1 << (ATTR_FGSHIFT
+ 4))-1)));
590 attr
|= (colourbits
<< (32-9));
593 add(b
, (unsigned char)((attr
>> 8) & 0xFF));
594 add(b
, (unsigned char)(attr
& 0xFF));
596 add(b
, (unsigned char)(((attr
>> 24) & 0x7F) | 0x80));
597 add(b
, (unsigned char)((attr
>> 16) & 0xFF));
598 add(b
, (unsigned char)((attr
>> 8) & 0xFF));
599 add(b
, (unsigned char)(attr
& 0xFF));
602 static void makeliteral_cc(struct buf
*b
, termchar
*c
, unsigned long *state
)
605 * For combining characters, I just encode a bunch of ordinary
606 * chars using makeliteral_chr, and terminate with a \0
607 * character (which I know won't come up as a combining char
610 * I don't use the stateful encoding in makeliteral_chr.
612 unsigned long zstate
;
621 makeliteral_chr(b
, c
, &zstate
);
626 makeliteral_chr(b
, &z
, &zstate
);
629 static termline
*decompressline(unsigned char *data
, int *bytes_used
);
631 static unsigned char *compressline(termline
*ldata
)
633 struct buf buffer
= { NULL
, 0, 0 }, *b
= &buffer
;
636 * First, store the column count, 7 bits at a time, least
637 * significant `digit' first, with the high bit set on all but
643 add(b
, (unsigned char)((n
& 0x7F) | 0x80));
646 add(b
, (unsigned char)(n
));
650 * Next store the lattrs; same principle.
653 int n
= ldata
->lattr
;
655 add(b
, (unsigned char)((n
& 0x7F) | 0x80));
658 add(b
, (unsigned char)(n
));
662 * Now we store a sequence of separate run-length encoded
663 * fragments, each containing exactly as many symbols as there
664 * are columns in the ldata.
666 * All of these have a common basic format:
668 * - a byte 00-7F indicates that X+1 literals follow it
669 * - a byte 80-FF indicates that a single literal follows it
670 * and expects to be repeated (X-0x80)+2 times.
672 * The format of the `literals' varies between the fragments.
674 makerle(b
, ldata
, makeliteral_chr
);
675 makerle(b
, ldata
, makeliteral_attr
);
676 makerle(b
, ldata
, makeliteral_cc
);
679 * Diagnostics: ensure that the compressed data really does
680 * decompress to the right thing.
682 * This is a bit performance-heavy for production code.
685 #ifndef CHECK_SB_COMPRESSION
691 #ifdef DIAGNOSTIC_SB_COMPRESSION
692 for (i
= 0; i
< b
->len
; i
++) {
693 printf(" %02x ", b
->data
[i
]);
698 dcl
= decompressline(b
->data
, &dused
);
699 assert(b
->len
== dused
);
700 assert(ldata
->cols
== dcl
->cols
);
701 assert(ldata
->lattr
== dcl
->lattr
);
702 for (i
= 0; i
< ldata
->cols
; i
++)
703 assert(termchars_equal(&ldata
->chars
[i
], &dcl
->chars
[i
]));
705 #ifdef DIAGNOSTIC_SB_COMPRESSION
706 printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",
707 ldata
->cols
, 4 * ldata
->cols
, dused
,
708 (double)dused
/ (4 * ldata
->cols
));
714 #endif /* TERM_CC_DIAGS */
717 * Trim the allocated memory so we don't waste any, and return.
719 return sresize(b
->data
, b
->len
, unsigned char);
722 static void readrle(struct buf
*b
, termline
*ldata
,
723 void (*readliteral
)(struct buf
*b
, termchar
*c
,
724 termline
*ldata
, unsigned long *state
))
727 unsigned long state
= 0;
729 while (n
< ldata
->cols
) {
735 int pos
= b
->len
, count
= hdr
+ 2 - 0x80;
737 assert(n
< ldata
->cols
);
739 readliteral(b
, ldata
->chars
+ n
, ldata
, &state
);
743 /* Just a sequence of consecutive literals. */
747 assert(n
< ldata
->cols
);
748 readliteral(b
, ldata
->chars
+ n
, ldata
, &state
);
754 assert(n
== ldata
->cols
);
756 static void readliteral_chr(struct buf
*b
, termchar
*c
, termline
*ldata
,
757 unsigned long *state
)
762 * 00000000-0000007F: 0xxxxxxx
763 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
764 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
765 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
766 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
771 c
->chr
= byte
| *state
;
772 } else if (byte
< 0xC0) {
773 c
->chr
= (byte
&~ 0xC0) << 8;
775 } else if (byte
< 0xE0) {
776 c
->chr
= (byte
&~ 0xE0) << 16;
777 c
->chr
|= get(b
) << 8;
779 } else if (byte
< 0xF0) {
780 c
->chr
= (byte
&~ 0xF0) << 24;
781 c
->chr
|= get(b
) << 16;
782 c
->chr
|= get(b
) << 8;
785 assert(byte
== 0xF0);
786 c
->chr
= get(b
) << 24;
787 c
->chr
|= get(b
) << 16;
788 c
->chr
|= get(b
) << 8;
791 *state
= c
->chr
& ~0xFF;
793 static void readliteral_attr(struct buf
*b
, termchar
*c
, termline
*ldata
,
794 unsigned long *state
)
796 unsigned val
, attr
, colourbits
;
808 colourbits
= (val
>> (32-9)) & 0xFF;
809 attr
= (val
& ((1<<(32-9))-1));
811 attr
= (((attr
>> (ATTR_FGSHIFT
+ 4)) << (ATTR_FGSHIFT
+ 8)) |
812 (attr
& ((1 << (ATTR_FGSHIFT
+ 4))-1)));
813 attr
= (((attr
>> (ATTR_BGSHIFT
+ 4)) << (ATTR_BGSHIFT
+ 8)) |
814 (attr
& ((1 << (ATTR_BGSHIFT
+ 4))-1)));
816 attr
|= (colourbits
>> 4) << (ATTR_BGSHIFT
+ 4);
817 attr
|= (colourbits
& 0xF) << (ATTR_FGSHIFT
+ 4);
821 static void readliteral_cc(struct buf
*b
, termchar
*c
, termline
*ldata
,
822 unsigned long *state
)
825 unsigned long zstate
;
826 int x
= c
- ldata
->chars
;
832 readliteral_chr(b
, &n
, ldata
, &zstate
);
835 add_cc(ldata
, x
, n
.chr
);
839 static termline
*decompressline(unsigned char *data
, int *bytes_used
)
841 int ncols
, byte
, shift
;
842 struct buf buffer
, *b
= &buffer
;
849 * First read in the column count.
854 ncols
|= (byte
& 0x7F) << shift
;
856 } while (byte
& 0x80);
859 * Now create the output termline.
861 ldata
= snew(termline
);
862 ldata
->chars
= snewn(ncols
, termchar
);
863 ldata
->cols
= ldata
->size
= ncols
;
864 ldata
->temporary
= TRUE
;
868 * We must set all the cc pointers in ldata->chars to 0 right
869 * now, so that cc diagnostics that verify the integrity of the
870 * whole line will make sense while we're in the middle of
875 for (i
= 0; i
< ldata
->cols
; i
++)
876 ldata
->chars
[i
].cc_next
= 0;
880 * Now read in the lattr.
882 ldata
->lattr
= shift
= 0;
885 ldata
->lattr
|= (byte
& 0x7F) << shift
;
887 } while (byte
& 0x80);
890 * Now we read in each of the RLE streams in turn.
892 readrle(b
, ldata
, readliteral_chr
);
893 readrle(b
, ldata
, readliteral_attr
);
894 readrle(b
, ldata
, readliteral_cc
);
896 /* Return the number of bytes read, for diagnostic purposes. */
898 *bytes_used
= b
->len
;
904 * Resize a line to make it `cols' columns wide.
906 static void resizeline(Terminal
*term
, termline
*line
, int cols
)
910 if (line
->cols
!= cols
) {
912 oldcols
= line
->cols
;
915 * This line is the wrong length, which probably means it
916 * hasn't been accessed since a resize. Resize it now.
918 * First, go through all the characters that will be thrown
919 * out in the resize (if we're shrinking the line) and
920 * return their cc lists to the cc free list.
922 for (i
= cols
; i
< oldcols
; i
++)
926 * If we're shrinking the line, we now bodily move the
927 * entire cc section from where it started to where it now
928 * needs to be. (We have to do this before the resize, so
929 * that the data we're copying is still there. However, if
930 * we're expanding, we have to wait until _after_ the
931 * resize so that the space we're copying into is there.)
934 memmove(line
->chars
+ cols
, line
->chars
+ oldcols
,
935 (line
->size
- line
->cols
) * TSIZE
);
938 * Now do the actual resize, leaving the _same_ amount of
939 * cc space as there was to begin with.
941 line
->size
+= cols
- oldcols
;
942 line
->chars
= sresize(line
->chars
, line
->size
, TTYPE
);
946 * If we're expanding the line, _now_ we move the cc
950 memmove(line
->chars
+ cols
, line
->chars
+ oldcols
,
951 (line
->size
- line
->cols
) * TSIZE
);
954 * Go through what's left of the original line, and adjust
955 * the first cc_next pointer in each list. (All the
956 * subsequent ones are still valid because they are
957 * relative offsets within the cc block.) Also do the same
958 * to the head of the cc_free list.
960 for (i
= 0; i
< oldcols
&& i
< cols
; i
++)
961 if (line
->chars
[i
].cc_next
)
962 line
->chars
[i
].cc_next
+= cols
- oldcols
;
964 line
->cc_free
+= cols
- oldcols
;
967 * And finally fill in the new space with erase chars. (We
968 * don't have to worry about cc lists here, because we
969 * _know_ the erase char doesn't have one.)
971 for (i
= oldcols
; i
< cols
; i
++)
972 line
->chars
[i
] = term
->basic_erase_char
;
981 * Get the number of lines in the scrollback.
983 static int sblines(Terminal
*term
)
985 int sblines
= count234(term
->scrollback
);
986 if (term
->cfg
.erase_to_scrollback
&&
987 term
->alt_which
&& term
->alt_screen
) {
988 sblines
+= term
->alt_sblines
;
994 * Retrieve a line of the screen or of the scrollback, according to
995 * whether the y coordinate is non-negative or negative
998 static termline
*lineptr(Terminal
*term
, int y
, int lineno
, int screen
)
1005 whichtree
= term
->screen
;
1012 if (term
->cfg
.erase_to_scrollback
&&
1013 term
->alt_which
&& term
->alt_screen
) {
1014 altlines
= term
->alt_sblines
;
1016 if (y
< -altlines
) {
1017 whichtree
= term
->scrollback
;
1018 treeindex
= y
+ altlines
+ count234(term
->scrollback
);
1020 whichtree
= term
->alt_screen
;
1021 treeindex
= y
+ term
->alt_sblines
;
1022 /* treeindex = y + count234(term->alt_screen); */
1025 if (whichtree
== term
->scrollback
) {
1026 unsigned char *cline
= index234(whichtree
, treeindex
);
1027 line
= decompressline(cline
, NULL
);
1029 line
= index234(whichtree
, treeindex
);
1032 /* We assume that we don't screw up and retrieve something out of range. */
1034 fatalbox("line==NULL in terminal.c\n"
1035 "lineno=%d y=%d w=%d h=%d\n"
1036 "count(scrollback=%p)=%d\n"
1037 "count(screen=%p)=%d\n"
1038 "count(alt=%p)=%d alt_sblines=%d\n"
1039 "whichtree=%p treeindex=%d\n\n"
1040 "Please contact <putty@projects.tartarus.org> "
1041 "and pass on the above information.",
1042 lineno
, y
, term
->cols
, term
->rows
,
1043 term
->scrollback
, count234(term
->scrollback
),
1044 term
->screen
, count234(term
->screen
),
1045 term
->alt_screen
, count234(term
->alt_screen
), term
->alt_sblines
,
1046 whichtree
, treeindex
);
1048 assert(line
!= NULL
);
1050 resizeline(term
, line
, term
->cols
);
1051 /* FIXME: should we sort the compressed scrollback out here? */
1056 #define lineptr(x) (lineptr)(term,x,__LINE__,FALSE)
1057 #define scrlineptr(x) (lineptr)(term,x,__LINE__,TRUE)
1059 static void term_schedule_tblink(Terminal
*term
);
1060 static void term_schedule_cblink(Terminal
*term
);
1062 static void term_timer(void *ctx
, long now
)
1064 Terminal
*term
= (Terminal
*)ctx
;
1067 if (term
->tblink_pending
&& now
- term
->next_tblink
>= 0) {
1068 term
->tblinker
= !term
->tblinker
;
1069 term
->tblink_pending
= FALSE
;
1070 term_schedule_tblink(term
);
1074 if (term
->cblink_pending
&& now
- term
->next_cblink
>= 0) {
1075 term
->cblinker
= !term
->cblinker
;
1076 term
->cblink_pending
= FALSE
;
1077 term_schedule_cblink(term
);
1081 if (term
->in_vbell
&& now
- term
->vbell_end
>= 0) {
1082 term
->in_vbell
= FALSE
;
1087 (term
->window_update_pending
&& now
- term
->next_update
>= 0))
1091 static void term_schedule_update(Terminal
*term
)
1093 if (!term
->window_update_pending
) {
1094 term
->window_update_pending
= TRUE
;
1095 term
->next_update
= schedule_timer(UPDATE_DELAY
, term_timer
, term
);
1100 * Call this whenever the terminal window state changes, to queue
1103 static void seen_disp_event(Terminal
*term
)
1105 term
->seen_disp_event
= TRUE
; /* for scrollback-reset-on-activity */
1106 term_schedule_update(term
);
1110 * Call when the terminal's blinking-text settings change, or when
1111 * a text blink has just occurred.
1113 static void term_schedule_tblink(Terminal
*term
)
1115 if (term
->blink_is_real
) {
1116 if (!term
->tblink_pending
)
1117 term
->next_tblink
= schedule_timer(TBLINK_DELAY
, term_timer
, term
);
1118 term
->tblink_pending
= TRUE
;
1120 term
->tblinker
= 1; /* reset when not in use */
1121 term
->tblink_pending
= FALSE
;
1126 * Likewise with cursor blinks.
1128 static void term_schedule_cblink(Terminal
*term
)
1130 if (term
->cfg
.blink_cur
&& term
->has_focus
) {
1131 if (!term
->cblink_pending
)
1132 term
->next_cblink
= schedule_timer(CBLINK_DELAY
, term_timer
, term
);
1133 term
->cblink_pending
= TRUE
;
1135 term
->cblinker
= 1; /* reset when not in use */
1136 term
->cblink_pending
= FALSE
;
1141 * Call to reset cursor blinking on new output.
1143 static void term_reset_cblink(Terminal
*term
)
1145 seen_disp_event(term
);
1147 term
->cblink_pending
= FALSE
;
1148 term_schedule_cblink(term
);
1152 * Call to begin a visual bell.
1154 static void term_schedule_vbell(Terminal
*term
, int already_started
,
1157 long ticks_already_gone
;
1159 if (already_started
)
1160 ticks_already_gone
= GETTICKCOUNT() - startpoint
;
1162 ticks_already_gone
= 0;
1164 if (ticks_already_gone
< VBELL_DELAY
) {
1165 term
->in_vbell
= TRUE
;
1166 term
->vbell_end
= schedule_timer(VBELL_DELAY
- ticks_already_gone
,
1169 term
->in_vbell
= FALSE
;
1174 * Set up power-on settings for the terminal.
1175 * If 'clear' is false, don't actually clear the primary screen, and
1176 * position the cursor below the last non-blank line (scrolling if
1179 static void power_on(Terminal
*term
, int clear
)
1181 term
->alt_x
= term
->alt_y
= 0;
1182 term
->savecurs
.x
= term
->savecurs
.y
= 0;
1183 term
->alt_t
= term
->marg_t
= 0;
1184 if (term
->rows
!= -1)
1185 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
1187 term
->alt_b
= term
->marg_b
= 0;
1188 if (term
->cols
!= -1) {
1190 for (i
= 0; i
< term
->cols
; i
++)
1191 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
1193 term
->alt_om
= term
->dec_om
= term
->cfg
.dec_om
;
1194 term
->alt_ins
= term
->insert
= FALSE
;
1195 term
->alt_wnext
= term
->wrapnext
= term
->save_wnext
= FALSE
;
1196 term
->alt_wrap
= term
->wrap
= term
->cfg
.wrap_mode
;
1197 term
->alt_cset
= term
->cset
= term
->save_cset
= 0;
1198 term
->alt_utf
= term
->utf
= term
->save_utf
= 0;
1199 term
->utf_state
= 0;
1200 term
->alt_sco_acs
= term
->sco_acs
= term
->save_sco_acs
= 0;
1201 term
->cset_attr
[0] = term
->cset_attr
[1] = term
->save_csattr
= CSET_ASCII
;
1203 term
->in_vbell
= FALSE
;
1204 term
->cursor_on
= 1;
1205 term
->big_cursor
= 0;
1206 term
->default_attr
= term
->save_attr
= term
->curr_attr
= ATTR_DEFAULT
;
1207 term
->term_editing
= term
->term_echoing
= FALSE
;
1208 term
->app_cursor_keys
= term
->cfg
.app_cursor
;
1209 term
->app_keypad_keys
= term
->cfg
.app_keypad
;
1210 term
->use_bce
= term
->cfg
.bce
;
1211 term
->blink_is_real
= term
->cfg
.blinktext
;
1212 term
->erase_char
= term
->basic_erase_char
;
1213 term
->alt_which
= 0;
1214 term_print_finish(term
);
1217 for (i
= 0; i
< 256; i
++)
1218 term
->wordness
[i
] = term
->cfg
.wordness
[i
];
1221 swap_screen(term
, 1, FALSE
, FALSE
);
1222 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1223 swap_screen(term
, 0, FALSE
, FALSE
);
1225 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1226 term
->curs
.y
= find_last_nonempty_line(term
, term
->screen
) + 1;
1227 if (term
->curs
.y
== term
->rows
) {
1229 scroll(term
, 0, term
->rows
- 1, 1, TRUE
);
1235 term_schedule_tblink(term
);
1236 term_schedule_cblink(term
);
1240 * Force a screen update.
1242 void term_update(Terminal
*term
)
1246 term
->window_update_pending
= FALSE
;
1248 ctx
= get_ctx(term
->frontend
);
1250 int need_sbar_update
= term
->seen_disp_event
;
1251 if (term
->seen_disp_event
&& term
->cfg
.scroll_on_disp
) {
1252 term
->disptop
= 0; /* return to main screen */
1253 term
->seen_disp_event
= 0;
1254 need_sbar_update
= TRUE
;
1257 if (need_sbar_update
)
1259 do_paint(term
, ctx
, TRUE
);
1260 sys_cursor(term
->frontend
, term
->curs
.x
, term
->curs
.y
- term
->disptop
);
1266 * Called from front end when a keypress occurs, to trigger
1267 * anything magical that needs to happen in that situation.
1269 void term_seen_key_event(Terminal
*term
)
1272 * On any keypress, clear the bell overload mechanism
1273 * completely, on the grounds that large numbers of
1274 * beeps coming from deliberate key action are likely
1275 * to be intended (e.g. beeps from filename completion
1276 * blocking repeatedly).
1278 term
->beep_overloaded
= FALSE
;
1279 while (term
->beephead
) {
1280 struct beeptime
*tmp
= term
->beephead
;
1281 term
->beephead
= tmp
->next
;
1284 term
->beeptail
= NULL
;
1288 * Reset the scrollback on keypress, if we're doing that.
1290 if (term
->cfg
.scroll_on_key
) {
1291 term
->disptop
= 0; /* return to main screen */
1292 seen_disp_event(term
);
1297 * Same as power_on(), but an external function.
1299 void term_pwron(Terminal
*term
, int clear
)
1301 power_on(term
, clear
);
1302 if (term
->ldisc
) /* cause ldisc to notice changes */
1303 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1309 static void set_erase_char(Terminal
*term
)
1311 term
->erase_char
= term
->basic_erase_char
;
1313 term
->erase_char
.attr
= (term
->curr_attr
&
1314 (ATTR_FGMASK
| ATTR_BGMASK
));
1318 * When the user reconfigures us, we need to check the forbidden-
1319 * alternate-screen config option, disable raw mouse mode if the
1320 * user has disabled mouse reporting, and abandon a print job if
1321 * the user has disabled printing.
1323 void term_reconfig(Terminal
*term
, Config
*cfg
)
1326 * Before adopting the new config, check all those terminal
1327 * settings which control power-on defaults; and if they've
1328 * changed, we will modify the current state as well as the
1329 * default one. The full list is: Auto wrap mode, DEC Origin
1330 * Mode, BCE, blinking text, character classes.
1332 int reset_wrap
, reset_decom
, reset_bce
, reset_tblink
, reset_charclass
;
1335 reset_wrap
= (term
->cfg
.wrap_mode
!= cfg
->wrap_mode
);
1336 reset_decom
= (term
->cfg
.dec_om
!= cfg
->dec_om
);
1337 reset_bce
= (term
->cfg
.bce
!= cfg
->bce
);
1338 reset_tblink
= (term
->cfg
.blinktext
!= cfg
->blinktext
);
1339 reset_charclass
= 0;
1340 for (i
= 0; i
< lenof(term
->cfg
.wordness
); i
++)
1341 if (term
->cfg
.wordness
[i
] != cfg
->wordness
[i
])
1342 reset_charclass
= 1;
1345 * If the bidi or shaping settings have changed, flush the bidi
1348 if (term
->cfg
.arabicshaping
!= cfg
->arabicshaping
||
1349 term
->cfg
.bidi
!= cfg
->bidi
) {
1350 for (i
= 0; i
< term
->bidi_cache_size
; i
++) {
1351 sfree(term
->pre_bidi_cache
[i
].chars
);
1352 sfree(term
->post_bidi_cache
[i
].chars
);
1353 term
->pre_bidi_cache
[i
].width
= -1;
1354 term
->pre_bidi_cache
[i
].chars
= NULL
;
1355 term
->post_bidi_cache
[i
].width
= -1;
1356 term
->post_bidi_cache
[i
].chars
= NULL
;
1360 term
->cfg
= *cfg
; /* STRUCTURE COPY */
1363 term
->alt_wrap
= term
->wrap
= term
->cfg
.wrap_mode
;
1365 term
->alt_om
= term
->dec_om
= term
->cfg
.dec_om
;
1367 term
->use_bce
= term
->cfg
.bce
;
1368 set_erase_char(term
);
1371 term
->blink_is_real
= term
->cfg
.blinktext
;
1373 if (reset_charclass
)
1374 for (i
= 0; i
< 256; i
++)
1375 term
->wordness
[i
] = term
->cfg
.wordness
[i
];
1377 if (term
->cfg
.no_alt_screen
)
1378 swap_screen(term
, 0, FALSE
, FALSE
);
1379 if (term
->cfg
.no_mouse_rep
) {
1380 term
->xterm_mouse
= 0;
1381 set_raw_mouse_mode(term
->frontend
, 0);
1383 if (term
->cfg
.no_remote_charset
) {
1384 term
->cset_attr
[0] = term
->cset_attr
[1] = CSET_ASCII
;
1385 term
->sco_acs
= term
->alt_sco_acs
= 0;
1388 if (!*term
->cfg
.printer
) {
1389 term_print_finish(term
);
1391 term_schedule_tblink(term
);
1392 term_schedule_cblink(term
);
1396 * Clear the scrollback.
1398 void term_clrsb(Terminal
*term
)
1400 unsigned char *line
;
1402 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
) {
1403 sfree(line
); /* this is compressed data, not a termline */
1405 term
->tempsblines
= 0;
1406 term
->alt_sblines
= 0;
1411 * Initialise the terminal.
1413 Terminal
*term_init(Config
*mycfg
, struct unicode_data
*ucsdata
,
1419 * Allocate a new Terminal structure and initialise the fields
1422 term
= snew(Terminal
);
1423 term
->frontend
= frontend
;
1424 term
->ucsdata
= ucsdata
;
1425 term
->cfg
= *mycfg
; /* STRUCTURE COPY */
1426 term
->logctx
= NULL
;
1427 term
->compatibility_level
= TM_PUTTY
;
1428 strcpy(term
->id_string
, "\033[?6c");
1429 term
->cblink_pending
= term
->tblink_pending
= FALSE
;
1430 term
->paste_buffer
= NULL
;
1431 term
->paste_len
= 0;
1432 term
->last_paste
= 0;
1433 bufchain_init(&term
->inbuf
);
1434 bufchain_init(&term
->printer_buf
);
1435 term
->printing
= term
->only_printing
= FALSE
;
1436 term
->print_job
= NULL
;
1437 term
->vt52_mode
= FALSE
;
1438 term
->cr_lf_return
= FALSE
;
1439 term
->seen_disp_event
= FALSE
;
1440 term
->xterm_mouse
= term
->mouse_is_down
= FALSE
;
1441 term
->reset_132
= FALSE
;
1442 term
->cblinker
= term
->tblinker
= 0;
1443 term
->has_focus
= 1;
1444 term
->repeat_off
= FALSE
;
1445 term
->termstate
= TOPLEVEL
;
1446 term
->selstate
= NO_SELECTION
;
1449 term
->screen
= term
->alt_screen
= term
->scrollback
= NULL
;
1450 term
->tempsblines
= 0;
1451 term
->alt_sblines
= 0;
1453 term
->disptext
= NULL
;
1454 term
->dispcursx
= term
->dispcursy
= -1;
1457 term
->rows
= term
->cols
= -1;
1458 power_on(term
, TRUE
);
1459 term
->beephead
= term
->beeptail
= NULL
;
1460 #ifdef OPTIMISE_SCROLL
1461 term
->scrollhead
= term
->scrolltail
= NULL
;
1462 #endif /* OPTIMISE_SCROLL */
1464 term
->lastbeep
= FALSE
;
1465 term
->beep_overloaded
= FALSE
;
1466 term
->attr_mask
= 0xffffffff;
1467 term
->resize_fn
= NULL
;
1468 term
->resize_ctx
= NULL
;
1469 term
->in_term_out
= FALSE
;
1471 term
->ltemp_size
= 0;
1472 term
->wcFrom
= NULL
;
1474 term
->wcFromTo_size
= 0;
1476 term
->window_update_pending
= FALSE
;
1478 term
->bidi_cache_size
= 0;
1479 term
->pre_bidi_cache
= term
->post_bidi_cache
= NULL
;
1482 term
->basic_erase_char
.chr
= CSET_ASCII
| ' ';
1483 term
->basic_erase_char
.attr
= ATTR_DEFAULT
;
1484 term
->basic_erase_char
.cc_next
= 0;
1485 term
->erase_char
= term
->basic_erase_char
;
1490 void term_free(Terminal
*term
)
1493 struct beeptime
*beep
;
1496 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
)
1497 sfree(line
); /* compressed data, not a termline */
1498 freetree234(term
->scrollback
);
1499 while ((line
= delpos234(term
->screen
, 0)) != NULL
)
1501 freetree234(term
->screen
);
1502 while ((line
= delpos234(term
->alt_screen
, 0)) != NULL
)
1504 freetree234(term
->alt_screen
);
1505 if (term
->disptext
) {
1506 for (i
= 0; i
< term
->rows
; i
++)
1507 freeline(term
->disptext
[i
]);
1509 sfree(term
->disptext
);
1510 while (term
->beephead
) {
1511 beep
= term
->beephead
;
1512 term
->beephead
= beep
->next
;
1515 bufchain_clear(&term
->inbuf
);
1517 printer_finish_job(term
->print_job
);
1518 bufchain_clear(&term
->printer_buf
);
1519 sfree(term
->paste_buffer
);
1521 sfree(term
->wcFrom
);
1524 for (i
= 0; i
< term
->bidi_cache_size
; i
++) {
1525 sfree(term
->pre_bidi_cache
[i
].chars
);
1526 sfree(term
->post_bidi_cache
[i
].chars
);
1528 sfree(term
->pre_bidi_cache
);
1529 sfree(term
->post_bidi_cache
);
1531 expire_timer_context(term
);
1537 * Set up the terminal for a given size.
1539 void term_size(Terminal
*term
, int newrows
, int newcols
, int newsavelines
)
1542 termline
**newdisp
, *line
;
1543 int i
, j
, oldrows
= term
->rows
;
1545 int save_alt_which
= term
->alt_which
;
1547 if (newrows
== term
->rows
&& newcols
== term
->cols
&&
1548 newsavelines
== term
->savelines
)
1549 return; /* nothing to do */
1551 /* Behave sensibly if we're given zero (or negative) rows/cols */
1553 if (newrows
< 1) newrows
= 1;
1554 if (newcols
< 1) newcols
= 1;
1557 swap_screen(term
, 0, FALSE
, FALSE
);
1559 term
->alt_t
= term
->marg_t
= 0;
1560 term
->alt_b
= term
->marg_b
= newrows
- 1;
1562 if (term
->rows
== -1) {
1563 term
->scrollback
= newtree234(NULL
);
1564 term
->screen
= newtree234(NULL
);
1565 term
->tempsblines
= 0;
1570 * Resize the screen and scrollback. We only need to shift
1571 * lines around within our data structures, because lineptr()
1572 * will take care of resizing each individual line if
1575 * - If the new screen is longer, we shunt lines in from temporary
1576 * scrollback if possible, otherwise we add new blank lines at
1579 * - If the new screen is shorter, we remove any blank lines at
1580 * the bottom if possible, otherwise shunt lines above the cursor
1581 * to scrollback if possible, otherwise delete lines below the
1584 * - Then, if the new scrollback length is less than the
1585 * amount of scrollback we actually have, we must throw some
1588 sblen
= count234(term
->scrollback
);
1589 /* Do this loop to expand the screen if newrows > rows */
1590 assert(term
->rows
== count234(term
->screen
));
1591 while (term
->rows
< newrows
) {
1592 if (term
->tempsblines
> 0) {
1593 unsigned char *cline
;
1594 /* Insert a line from the scrollback at the top of the screen. */
1595 assert(sblen
>= term
->tempsblines
);
1596 cline
= delpos234(term
->scrollback
, --sblen
);
1597 line
= decompressline(cline
, NULL
);
1599 line
->temporary
= FALSE
; /* reconstituted line is now real */
1600 term
->tempsblines
-= 1;
1601 addpos234(term
->screen
, line
, 0);
1603 term
->savecurs
.y
+= 1;
1605 /* Add a new blank line at the bottom of the screen. */
1606 line
= newline(term
, newcols
, FALSE
);
1607 addpos234(term
->screen
, line
, count234(term
->screen
));
1611 /* Do this loop to shrink the screen if newrows < rows */
1612 while (term
->rows
> newrows
) {
1613 if (term
->curs
.y
< term
->rows
- 1) {
1614 /* delete bottom row, unless it contains the cursor */
1615 sfree(delpos234(term
->screen
, term
->rows
- 1));
1617 /* push top row to scrollback */
1618 line
= delpos234(term
->screen
, 0);
1619 addpos234(term
->scrollback
, compressline(line
), sblen
++);
1621 term
->tempsblines
+= 1;
1623 term
->savecurs
.y
-= 1;
1627 assert(term
->rows
== newrows
);
1628 assert(count234(term
->screen
) == newrows
);
1630 /* Delete any excess lines from the scrollback. */
1631 while (sblen
> newsavelines
) {
1632 line
= delpos234(term
->scrollback
, 0);
1636 if (sblen
< term
->tempsblines
)
1637 term
->tempsblines
= sblen
;
1638 assert(count234(term
->scrollback
) <= newsavelines
);
1639 assert(count234(term
->scrollback
) >= term
->tempsblines
);
1642 /* Make a new displayed text buffer. */
1643 newdisp
= snewn(newrows
, termline
*);
1644 for (i
= 0; i
< newrows
; i
++) {
1645 newdisp
[i
] = newline(term
, newcols
, FALSE
);
1646 for (j
= 0; j
< newcols
; j
++)
1647 newdisp
[i
]->chars
[j
].attr
= ATTR_INVALID
;
1649 if (term
->disptext
) {
1650 for (i
= 0; i
< oldrows
; i
++)
1651 freeline(term
->disptext
[i
]);
1653 sfree(term
->disptext
);
1654 term
->disptext
= newdisp
;
1655 term
->dispcursx
= term
->dispcursy
= -1;
1657 /* Make a new alternate screen. */
1658 newalt
= newtree234(NULL
);
1659 for (i
= 0; i
< newrows
; i
++) {
1660 line
= newline(term
, newcols
, TRUE
);
1661 addpos234(newalt
, line
, i
);
1663 if (term
->alt_screen
) {
1664 while (NULL
!= (line
= delpos234(term
->alt_screen
, 0)))
1666 freetree234(term
->alt_screen
);
1668 term
->alt_screen
= newalt
;
1669 term
->alt_sblines
= 0;
1671 term
->tabs
= sresize(term
->tabs
, newcols
, unsigned char);
1674 for (i
= (term
->cols
> 0 ? term
->cols
: 0); i
< newcols
; i
++)
1675 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
1678 /* Check that the cursor positions are still valid. */
1679 if (term
->savecurs
.y
< 0)
1680 term
->savecurs
.y
= 0;
1681 if (term
->savecurs
.y
>= newrows
)
1682 term
->savecurs
.y
= newrows
- 1;
1683 if (term
->curs
.y
< 0)
1685 if (term
->curs
.y
>= newrows
)
1686 term
->curs
.y
= newrows
- 1;
1687 if (term
->curs
.x
>= newcols
)
1688 term
->curs
.x
= newcols
- 1;
1689 term
->alt_x
= term
->alt_y
= 0;
1690 term
->wrapnext
= term
->alt_wnext
= FALSE
;
1692 term
->rows
= newrows
;
1693 term
->cols
= newcols
;
1694 term
->savelines
= newsavelines
;
1696 swap_screen(term
, save_alt_which
, FALSE
, FALSE
);
1700 if (term
->resize_fn
)
1701 term
->resize_fn(term
->resize_ctx
, term
->cols
, term
->rows
);
1705 * Hand a function and context pointer to the terminal which it can
1706 * use to notify a back end of resizes.
1708 void term_provide_resize_fn(Terminal
*term
,
1709 void (*resize_fn
)(void *, int, int),
1712 term
->resize_fn
= resize_fn
;
1713 term
->resize_ctx
= resize_ctx
;
1714 if (term
->cols
> 0 && term
->rows
> 0)
1715 resize_fn(resize_ctx
, term
->cols
, term
->rows
);
1718 /* Find the bottom line on the screen that has any content.
1719 * If only the top line has content, returns 0.
1720 * If no lines have content, return -1.
1722 static int find_last_nonempty_line(Terminal
* term
, tree234
* screen
)
1725 for (i
= count234(screen
) - 1; i
>= 0; i
--) {
1726 termline
*line
= index234(screen
, i
);
1728 for (j
= 0; j
< line
->cols
; j
++)
1729 if (!termchars_equal(&line
->chars
[j
], &term
->erase_char
))
1731 if (j
!= line
->cols
) break;
1737 * Swap screens. If `reset' is TRUE and we have been asked to
1738 * switch to the alternate screen, we must bring most of its
1739 * configuration from the main screen and erase the contents of the
1740 * alternate screen completely. (This is even true if we're already
1741 * on it! Blame xterm.)
1743 static void swap_screen(Terminal
*term
, int which
, int reset
, int keep_cur_pos
)
1749 reset
= FALSE
; /* do no weird resetting if which==0 */
1751 if (which
!= term
->alt_which
) {
1752 term
->alt_which
= which
;
1754 ttr
= term
->alt_screen
;
1755 term
->alt_screen
= term
->screen
;
1757 term
->alt_sblines
= find_last_nonempty_line(term
, term
->alt_screen
) + 1;
1759 if (!reset
&& !keep_cur_pos
)
1760 term
->curs
.x
= term
->alt_x
;
1763 if (!reset
&& !keep_cur_pos
)
1764 term
->curs
.y
= term
->alt_y
;
1767 if (!reset
) term
->marg_t
= term
->alt_t
;
1770 if (!reset
) term
->marg_b
= term
->alt_b
;
1773 if (!reset
) term
->dec_om
= term
->alt_om
;
1776 if (!reset
) term
->wrap
= term
->alt_wrap
;
1779 if (!reset
) term
->wrapnext
= term
->alt_wnext
;
1780 term
->alt_wnext
= t
;
1782 if (!reset
) term
->insert
= term
->alt_ins
;
1785 if (!reset
) term
->cset
= term
->alt_cset
;
1788 if (!reset
) term
->utf
= term
->alt_utf
;
1791 if (!reset
) term
->sco_acs
= term
->alt_sco_acs
;
1792 term
->alt_sco_acs
= t
;
1795 if (reset
&& term
->screen
) {
1797 * Yes, this _is_ supposed to honour background-colour-erase.
1799 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1804 * Update the scroll bar.
1806 static void update_sbar(Terminal
*term
)
1808 int nscroll
= sblines(term
);
1809 set_sbar(term
->frontend
, nscroll
+ term
->rows
,
1810 nscroll
+ term
->disptop
, term
->rows
);
1814 * Check whether the region bounded by the two pointers intersects
1815 * the scroll region, and de-select the on-screen selection if so.
1817 static void check_selection(Terminal
*term
, pos from
, pos to
)
1819 if (poslt(from
, term
->selend
) && poslt(term
->selstart
, to
))
1824 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1825 * for backward.) `sb' is TRUE if the scrolling is permitted to
1826 * affect the scrollback buffer.
1828 static void scroll(Terminal
*term
, int topline
, int botline
, int lines
, int sb
)
1831 int i
, seltop
, olddisptop
, shift
;
1833 if (topline
!= 0 || term
->alt_which
!= 0)
1836 olddisptop
= term
->disptop
;
1840 line
= delpos234(term
->screen
, botline
);
1841 resizeline(term
, line
, term
->cols
);
1842 for (i
= 0; i
< term
->cols
; i
++)
1843 copy_termchar(line
, i
, &term
->erase_char
);
1844 line
->lattr
= LATTR_NORM
;
1845 addpos234(term
->screen
, line
, topline
);
1847 if (term
->selstart
.y
>= topline
&& term
->selstart
.y
<= botline
) {
1849 if (term
->selstart
.y
> botline
) {
1850 term
->selstart
.y
= botline
+ 1;
1851 term
->selstart
.x
= 0;
1854 if (term
->selend
.y
>= topline
&& term
->selend
.y
<= botline
) {
1856 if (term
->selend
.y
> botline
) {
1857 term
->selend
.y
= botline
+ 1;
1866 line
= delpos234(term
->screen
, topline
);
1867 #ifdef TERM_CC_DIAGS
1870 if (sb
&& term
->savelines
> 0) {
1871 int sblen
= count234(term
->scrollback
);
1873 * We must add this line to the scrollback. We'll
1874 * remove a line from the top of the scrollback if
1875 * the scrollback is full.
1877 if (sblen
== term
->savelines
) {
1878 unsigned char *cline
;
1881 cline
= delpos234(term
->scrollback
, 0);
1884 term
->tempsblines
+= 1;
1886 addpos234(term
->scrollback
, compressline(line
), sblen
);
1888 /* now `line' itself can be reused as the bottom line */
1891 * If the user is currently looking at part of the
1892 * scrollback, and they haven't enabled any options
1893 * that are going to reset the scrollback as a
1894 * result of this movement, then the chances are
1895 * they'd like to keep looking at the same line. So
1896 * we move their viewpoint at the same rate as the
1897 * scroll, at least until their viewpoint hits the
1898 * top end of the scrollback buffer, at which point
1899 * we don't have the choice any more.
1901 * Thanks to Jan Holmen Holsten for the idea and
1902 * initial implementation.
1904 if (term
->disptop
> -term
->savelines
&& term
->disptop
< 0)
1907 resizeline(term
, line
, term
->cols
);
1908 for (i
= 0; i
< term
->cols
; i
++)
1909 copy_termchar(line
, i
, &term
->erase_char
);
1910 line
->lattr
= LATTR_NORM
;
1911 addpos234(term
->screen
, line
, botline
);
1914 * If the selection endpoints move into the scrollback,
1915 * we keep them moving until they hit the top. However,
1916 * of course, if the line _hasn't_ moved into the
1917 * scrollback then we don't do this, and cut them off
1918 * at the top of the scroll region.
1920 * This applies to selstart and selend (for an existing
1921 * selection), and also selanchor (for one being
1922 * selected as we speak).
1924 seltop
= sb ?
-term
->savelines
: topline
;
1926 if (term
->selstate
!= NO_SELECTION
) {
1927 if (term
->selstart
.y
>= seltop
&&
1928 term
->selstart
.y
<= botline
) {
1930 if (term
->selstart
.y
< seltop
) {
1931 term
->selstart
.y
= seltop
;
1932 term
->selstart
.x
= 0;
1935 if (term
->selend
.y
>= seltop
&& term
->selend
.y
<= botline
) {
1937 if (term
->selend
.y
< seltop
) {
1938 term
->selend
.y
= seltop
;
1942 if (term
->selanchor
.y
>= seltop
&&
1943 term
->selanchor
.y
<= botline
) {
1944 term
->selanchor
.y
--;
1945 if (term
->selanchor
.y
< seltop
) {
1946 term
->selanchor
.y
= seltop
;
1947 term
->selanchor
.x
= 0;
1955 #ifdef OPTIMISE_SCROLL
1956 shift
+= term
->disptop
- olddisptop
;
1957 if (shift
< term
->rows
&& shift
> -term
->rows
&& shift
!= 0)
1958 scroll_display(term
, topline
, botline
, shift
);
1959 #endif /* OPTIMISE_SCROLL */
1962 #ifdef OPTIMISE_SCROLL
1964 * Add a scroll of a region on the screen into the pending scroll list.
1965 * `lines' is +ve for scrolling forward, -ve for backward.
1967 * If the scroll is on the same area as the last scroll in the list,
1970 static void save_scroll(Terminal
*term
, int topline
, int botline
, int lines
)
1972 struct scrollregion
*newscroll
;
1973 if (term
->scrolltail
&&
1974 term
->scrolltail
->topline
== topline
&&
1975 term
->scrolltail
->botline
== botline
) {
1976 term
->scrolltail
->lines
+= lines
;
1978 newscroll
= snew(struct scrollregion
);
1979 newscroll
->topline
= topline
;
1980 newscroll
->botline
= botline
;
1981 newscroll
->lines
= lines
;
1982 newscroll
->next
= NULL
;
1984 if (!term
->scrollhead
)
1985 term
->scrollhead
= newscroll
;
1987 term
->scrolltail
->next
= newscroll
;
1988 term
->scrolltail
= newscroll
;
1993 * Scroll the physical display, and our conception of it in disptext.
1995 static void scroll_display(Terminal
*term
, int topline
, int botline
, int lines
)
1997 int distance
, nlines
, i
, j
;
1999 distance
= lines
> 0 ? lines
: -lines
;
2000 nlines
= botline
- topline
+ 1 - distance
;
2002 for (i
= 0; i
< nlines
; i
++)
2003 for (j
= 0; j
< term
->cols
; j
++)
2004 copy_termchar(term
->disptext
[i
], j
,
2005 term
->disptext
[i
+distance
]->chars
+j
);
2006 if (term
->dispcursy
>= 0 &&
2007 term
->dispcursy
>= topline
+ distance
&&
2008 term
->dispcursy
< topline
+ distance
+ nlines
)
2009 term
->dispcursy
-= distance
;
2010 for (i
= 0; i
< distance
; i
++)
2011 for (j
= 0; j
< term
->cols
; j
++)
2012 term
->disptext
[nlines
+i
]->chars
[j
].attr
|= ATTR_INVALID
;
2014 for (i
= nlines
; i
-- ;)
2015 for (j
= 0; j
< term
->cols
; j
++)
2016 copy_termchar(term
->disptext
[i
+distance
], j
,
2017 term
->disptext
[i
]->chars
+j
);
2018 if (term
->dispcursy
>= 0 &&
2019 term
->dispcursy
>= topline
&&
2020 term
->dispcursy
< topline
+ nlines
)
2021 term
->dispcursy
+= distance
;
2022 for (i
= 0; i
< distance
; i
++)
2023 for (j
= 0; j
< term
->cols
; j
++)
2024 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
2026 save_scroll(term
, topline
, botline
, lines
);
2028 #endif /* OPTIMISE_SCROLL */
2031 * Move the cursor to a given position, clipping at boundaries. We
2032 * may or may not want to clip at the scroll margin: marg_clip is 0
2033 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
2034 * even _being_ outside the margins.
2036 static void move(Terminal
*term
, int x
, int y
, int marg_clip
)
2040 if (x
>= term
->cols
)
2043 if ((term
->curs
.y
>= term
->marg_t
|| marg_clip
== 2) &&
2046 if ((term
->curs
.y
<= term
->marg_b
|| marg_clip
== 2) &&
2052 if (y
>= term
->rows
)
2056 term
->wrapnext
= FALSE
;
2060 * Save or restore the cursor and SGR mode.
2062 static void save_cursor(Terminal
*term
, int save
)
2065 term
->savecurs
= term
->curs
;
2066 term
->save_attr
= term
->curr_attr
;
2067 term
->save_cset
= term
->cset
;
2068 term
->save_utf
= term
->utf
;
2069 term
->save_wnext
= term
->wrapnext
;
2070 term
->save_csattr
= term
->cset_attr
[term
->cset
];
2071 term
->save_sco_acs
= term
->sco_acs
;
2073 term
->curs
= term
->savecurs
;
2074 /* Make sure the window hasn't shrunk since the save */
2075 if (term
->curs
.x
>= term
->cols
)
2076 term
->curs
.x
= term
->cols
- 1;
2077 if (term
->curs
.y
>= term
->rows
)
2078 term
->curs
.y
= term
->rows
- 1;
2080 term
->curr_attr
= term
->save_attr
;
2081 term
->cset
= term
->save_cset
;
2082 term
->utf
= term
->save_utf
;
2083 term
->wrapnext
= term
->save_wnext
;
2085 * wrapnext might reset to False if the x position is no
2086 * longer at the rightmost edge.
2088 if (term
->wrapnext
&& term
->curs
.x
< term
->cols
-1)
2089 term
->wrapnext
= FALSE
;
2090 term
->cset_attr
[term
->cset
] = term
->save_csattr
;
2091 term
->sco_acs
= term
->save_sco_acs
;
2092 set_erase_char(term
);
2097 * This function is called before doing _anything_ which affects
2098 * only part of a line of text. It is used to mark the boundary
2099 * between two character positions, and it indicates that some sort
2100 * of effect is going to happen on only one side of that boundary.
2102 * The effect of this function is to check whether a CJK
2103 * double-width character is straddling the boundary, and to remove
2104 * it and replace it with two spaces if so. (Of course, one or
2105 * other of those spaces is then likely to be replaced with
2106 * something else again, as a result of whatever happens next.)
2108 * Also, if the boundary is at the right-hand _edge_ of the screen,
2109 * it implies something deliberate is being done to the rightmost
2110 * column position; hence we must clear LATTR_WRAPPED2.
2112 * The input to the function is the coordinates of the _second_
2113 * character of the pair.
2115 static void check_boundary(Terminal
*term
, int x
, int y
)
2119 /* Validate input coordinates, just in case. */
2120 if (x
== 0 || x
> term
->cols
)
2123 ldata
= scrlineptr(y
);
2124 if (x
== term
->cols
) {
2125 ldata
->lattr
&= ~LATTR_WRAPPED2
;
2127 if (ldata
->chars
[x
].chr
== UCSWIDE
) {
2128 clear_cc(ldata
, x
-1);
2130 ldata
->chars
[x
-1].chr
= ' ' | CSET_ASCII
;
2131 ldata
->chars
[x
] = ldata
->chars
[x
-1];
2137 * Erase a large portion of the screen: the whole screen, or the
2138 * whole line, or parts thereof.
2140 static void erase_lots(Terminal
*term
,
2141 int line_only
, int from_begin
, int to_end
)
2145 int erasing_lines_from_top
= 0;
2148 start
.y
= term
->curs
.y
;
2150 end
.y
= term
->curs
.y
+ 1;
2152 erase_lattr
= FALSE
;
2167 if (!from_begin
|| !to_end
)
2168 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2169 check_selection(term
, start
, end
);
2171 /* Clear screen also forces a full window redraw, just in case. */
2172 if (start
.y
== 0 && start
.x
== 0 && end
.y
== term
->rows
)
2173 term_invalidate(term
);
2175 /* Lines scrolled away shouldn't be brought back on if the terminal
2177 if (start
.y
== 0 && start
.x
== 0 && end
.x
== 0 && erase_lattr
)
2178 erasing_lines_from_top
= 1;
2180 if (term
->cfg
.erase_to_scrollback
&& erasing_lines_from_top
) {
2181 /* If it's a whole number of lines, starting at the top, and
2182 * we're fully erasing them, erase by scrolling and keep the
2183 * lines in the scrollback. */
2184 int scrolllines
= end
.y
;
2185 if (end
.y
== term
->rows
) {
2186 /* Shrink until we find a non-empty row.*/
2187 scrolllines
= find_last_nonempty_line(term
, term
->screen
) + 1;
2189 if (scrolllines
> 0)
2190 scroll(term
, 0, scrolllines
- 1, scrolllines
, TRUE
);
2192 termline
*ldata
= scrlineptr(start
.y
);
2193 while (poslt(start
, end
)) {
2194 if (start
.x
== term
->cols
) {
2196 ldata
->lattr
&= ~(LATTR_WRAPPED
| LATTR_WRAPPED2
);
2198 ldata
->lattr
= LATTR_NORM
;
2200 copy_termchar(ldata
, start
.x
, &term
->erase_char
);
2202 if (incpos(start
) && start
.y
< term
->rows
) {
2203 ldata
= scrlineptr(start
.y
);
2208 /* After an erase of lines from the top of the screen, we shouldn't
2209 * bring the lines back again if the terminal enlarges (since the user or
2210 * application has explictly thrown them away). */
2211 if (erasing_lines_from_top
&& !(term
->alt_which
))
2212 term
->tempsblines
= 0;
2216 * Insert or delete characters within the current line. n is +ve if
2217 * insertion is desired, and -ve for deletion.
2219 static void insch(Terminal
*term
, int n
)
2221 int dir
= (n
< 0 ?
-1 : +1);
2226 n
= (n
< 0 ?
-n
: n
);
2227 if (n
> term
->cols
- term
->curs
.x
)
2228 n
= term
->cols
- term
->curs
.x
;
2229 m
= term
->cols
- term
->curs
.x
- n
;
2230 cursplus
.y
= term
->curs
.y
;
2231 cursplus
.x
= term
->curs
.x
+ n
;
2232 check_selection(term
, term
->curs
, cursplus
);
2233 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2235 check_boundary(term
, term
->curs
.x
+ n
, term
->curs
.y
);
2236 ldata
= scrlineptr(term
->curs
.y
);
2238 for (j
= 0; j
< m
; j
++)
2239 move_termchar(ldata
,
2240 ldata
->chars
+ term
->curs
.x
+ j
,
2241 ldata
->chars
+ term
->curs
.x
+ j
+ n
);
2243 copy_termchar(ldata
, term
->curs
.x
+ m
++, &term
->erase_char
);
2246 move_termchar(ldata
,
2247 ldata
->chars
+ term
->curs
.x
+ j
+ n
,
2248 ldata
->chars
+ term
->curs
.x
+ j
);
2250 copy_termchar(ldata
, term
->curs
.x
+ n
, &term
->erase_char
);
2255 * Toggle terminal mode `mode' to state `state'. (`query' indicates
2256 * whether the mode is a DEC private one or a normal one.)
2258 static void toggle_mode(Terminal
*term
, int mode
, int query
, int state
)
2262 case 1: /* DECCKM: application cursor keys */
2263 term
->app_cursor_keys
= state
;
2265 case 2: /* DECANM: VT52 mode */
2266 term
->vt52_mode
= !state
;
2267 if (term
->vt52_mode
) {
2268 term
->blink_is_real
= FALSE
;
2269 term
->vt52_bold
= FALSE
;
2271 term
->blink_is_real
= term
->cfg
.blinktext
;
2273 term_schedule_tblink(term
);
2275 case 3: /* DECCOLM: 80/132 columns */
2277 if (!term
->cfg
.no_remote_resize
)
2278 request_resize(term
->frontend
, state ?
132 : 80, term
->rows
);
2279 term
->reset_132
= state
;
2280 term
->alt_t
= term
->marg_t
= 0;
2281 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
2282 move(term
, 0, 0, 0);
2283 erase_lots(term
, FALSE
, TRUE
, TRUE
);
2285 case 5: /* DECSCNM: reverse video */
2287 * Toggle reverse video. If we receive an OFF within the
2288 * visual bell timeout period after an ON, we trigger an
2289 * effective visual bell, so that ESC[?5hESC[?5l will
2290 * always be an actually _visible_ visual bell.
2292 if (term
->rvideo
&& !state
) {
2293 /* This is an OFF, so set up a vbell */
2294 term_schedule_vbell(term
, TRUE
, term
->rvbell_startpoint
);
2295 } else if (!term
->rvideo
&& state
) {
2296 /* This is an ON, so we notice the time and save it. */
2297 term
->rvbell_startpoint
= GETTICKCOUNT();
2299 term
->rvideo
= state
;
2300 seen_disp_event(term
);
2302 case 6: /* DECOM: DEC origin mode */
2303 term
->dec_om
= state
;
2305 case 7: /* DECAWM: auto wrap */
2308 case 8: /* DECARM: auto key repeat */
2309 term
->repeat_off
= !state
;
2311 case 10: /* DECEDM: set local edit mode */
2312 term
->term_editing
= state
;
2313 if (term
->ldisc
) /* cause ldisc to notice changes */
2314 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2316 case 25: /* DECTCEM: enable/disable cursor */
2317 compatibility2(OTHER
, VT220
);
2318 term
->cursor_on
= state
;
2319 seen_disp_event(term
);
2321 case 47: /* alternate screen */
2322 compatibility(OTHER
);
2324 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, FALSE
, FALSE
);
2327 case 1000: /* xterm mouse 1 */
2328 term
->xterm_mouse
= state ?
1 : 0;
2329 set_raw_mouse_mode(term
->frontend
, state
);
2331 case 1002: /* xterm mouse 2 */
2332 term
->xterm_mouse
= state ?
2 : 0;
2333 set_raw_mouse_mode(term
->frontend
, state
);
2335 case 1047: /* alternate screen */
2336 compatibility(OTHER
);
2338 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, TRUE
, TRUE
);
2341 case 1048: /* save/restore cursor */
2342 if (!term
->cfg
.no_alt_screen
)
2343 save_cursor(term
, state
);
2344 if (!state
) seen_disp_event(term
);
2346 case 1049: /* cursor & alternate screen */
2347 if (state
&& !term
->cfg
.no_alt_screen
)
2348 save_cursor(term
, state
);
2349 if (!state
) seen_disp_event(term
);
2350 compatibility(OTHER
);
2352 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, TRUE
, FALSE
);
2353 if (!state
&& !term
->cfg
.no_alt_screen
)
2354 save_cursor(term
, state
);
2359 case 4: /* IRM: set insert mode */
2360 compatibility(VT102
);
2361 term
->insert
= state
;
2363 case 12: /* SRM: set echo mode */
2364 term
->term_echoing
= !state
;
2365 if (term
->ldisc
) /* cause ldisc to notice changes */
2366 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2368 case 20: /* LNM: Return sends ... */
2369 term
->cr_lf_return
= state
;
2371 case 34: /* WYULCURM: Make cursor BIG */
2372 compatibility2(OTHER
, VT220
);
2373 term
->big_cursor
= !state
;
2378 * Process an OSC sequence: set window title or icon name.
2380 static void do_osc(Terminal
*term
)
2383 while (term
->osc_strlen
--)
2384 term
->wordness
[(unsigned char)
2385 term
->osc_string
[term
->osc_strlen
]] = term
->esc_args
[0];
2387 term
->osc_string
[term
->osc_strlen
] = '\0';
2388 switch (term
->esc_args
[0]) {
2391 if (!term
->cfg
.no_remote_wintitle
)
2392 set_icon(term
->frontend
, term
->osc_string
);
2393 if (term
->esc_args
[0] == 1)
2395 /* fall through: parameter 0 means set both */
2398 if (!term
->cfg
.no_remote_wintitle
)
2399 set_title(term
->frontend
, term
->osc_string
);
2406 * ANSI printing routines.
2408 static void term_print_setup(Terminal
*term
)
2410 bufchain_clear(&term
->printer_buf
);
2411 term
->print_job
= printer_start_job(term
->cfg
.printer
);
2413 static void term_print_flush(Terminal
*term
)
2418 while ((size
= bufchain_size(&term
->printer_buf
)) > 5) {
2419 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
2422 printer_job_data(term
->print_job
, data
, len
);
2423 bufchain_consume(&term
->printer_buf
, len
);
2426 static void term_print_finish(Terminal
*term
)
2432 if (!term
->printing
&& !term
->only_printing
)
2433 return; /* we need do nothing */
2435 term_print_flush(term
);
2436 while ((size
= bufchain_size(&term
->printer_buf
)) > 0) {
2437 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
2439 if (c
== '\033' || c
== '\233') {
2440 bufchain_consume(&term
->printer_buf
, size
);
2443 printer_job_data(term
->print_job
, &c
, 1);
2444 bufchain_consume(&term
->printer_buf
, 1);
2447 printer_finish_job(term
->print_job
);
2448 term
->print_job
= NULL
;
2449 term
->printing
= term
->only_printing
= FALSE
;
2453 * Remove everything currently in `inbuf' and stick it up on the
2454 * in-memory display. There's a big state machine in here to
2455 * process escape sequences...
2457 static void term_out(Terminal
*term
)
2461 unsigned char localbuf
[256], *chars
;
2466 chars
= NULL
; /* placate compiler warnings */
2467 while (nchars
> 0 || unget
!= -1 || bufchain_size(&term
->inbuf
) > 0) {
2471 bufchain_prefix(&term
->inbuf
, &ret
, &nchars
);
2472 if (nchars
> sizeof(localbuf
))
2473 nchars
= sizeof(localbuf
);
2474 memcpy(localbuf
, ret
, nchars
);
2475 bufchain_consume(&term
->inbuf
, nchars
);
2477 assert(chars
!= NULL
);
2483 * Optionally log the session traffic to a file. Useful for
2484 * debugging and possibly also useful for actual logging.
2486 if (term
->cfg
.logtype
== LGTYP_DEBUG
&& term
->logctx
)
2487 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_DEBUG
);
2493 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
2494 * be able to display 8-bit characters, but I'll let that go 'cause
2499 * If we're printing, add the character to the printer
2502 if (term
->printing
) {
2503 bufchain_add(&term
->printer_buf
, &c
, 1);
2506 * If we're in print-only mode, we use a much simpler
2507 * state machine designed only to recognise the ESC[4i
2508 * termination sequence.
2510 if (term
->only_printing
) {
2512 term
->print_state
= 1;
2513 else if (c
== (unsigned char)'\233')
2514 term
->print_state
= 2;
2515 else if (c
== '[' && term
->print_state
== 1)
2516 term
->print_state
= 2;
2517 else if (c
== '4' && term
->print_state
== 2)
2518 term
->print_state
= 3;
2519 else if (c
== 'i' && term
->print_state
== 3)
2520 term
->print_state
= 4;
2522 term
->print_state
= 0;
2523 if (term
->print_state
== 4) {
2524 term_print_finish(term
);
2530 /* First see about all those translations. */
2531 if (term
->termstate
== TOPLEVEL
) {
2533 switch (term
->utf_state
) {
2536 /* UTF-8 must be stateless so we ignore iso2022. */
2537 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2538 c
= term
->ucsdata
->unitab_ctrl
[c
];
2539 else c
= ((unsigned char)c
) | CSET_ASCII
;
2541 } else if ((c
& 0xe0) == 0xc0) {
2542 term
->utf_size
= term
->utf_state
= 1;
2543 term
->utf_char
= (c
& 0x1f);
2544 } else if ((c
& 0xf0) == 0xe0) {
2545 term
->utf_size
= term
->utf_state
= 2;
2546 term
->utf_char
= (c
& 0x0f);
2547 } else if ((c
& 0xf8) == 0xf0) {
2548 term
->utf_size
= term
->utf_state
= 3;
2549 term
->utf_char
= (c
& 0x07);
2550 } else if ((c
& 0xfc) == 0xf8) {
2551 term
->utf_size
= term
->utf_state
= 4;
2552 term
->utf_char
= (c
& 0x03);
2553 } else if ((c
& 0xfe) == 0xfc) {
2554 term
->utf_size
= term
->utf_state
= 5;
2555 term
->utf_char
= (c
& 0x01);
2566 if ((c
& 0xC0) != 0x80) {
2569 term
->utf_state
= 0;
2572 term
->utf_char
= (term
->utf_char
<< 6) | (c
& 0x3f);
2573 if (--term
->utf_state
)
2578 /* Is somebody trying to be evil! */
2580 (c
< 0x800 && term
->utf_size
>= 2) ||
2581 (c
< 0x10000 && term
->utf_size
>= 3) ||
2582 (c
< 0x200000 && term
->utf_size
>= 4) ||
2583 (c
< 0x4000000 && term
->utf_size
>= 5))
2586 /* Unicode line separator and paragraph separator are CR-LF */
2587 if (c
== 0x2028 || c
== 0x2029)
2590 /* High controls are probably a Baaad idea too. */
2594 /* The UTF-16 surrogates are not nice either. */
2595 /* The standard give the option of decoding these:
2596 * I don't want to! */
2597 if (c
>= 0xD800 && c
< 0xE000)
2600 /* ISO 10646 characters now limited to UTF-16 range. */
2604 /* This is currently a TagPhobic application.. */
2605 if (c
>= 0xE0000 && c
<= 0xE007F)
2608 /* U+FEFF is best seen as a null. */
2611 /* But U+FFFE is an error. */
2612 if (c
== 0xFFFE || c
== 0xFFFF)
2617 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
2618 else if(term
->sco_acs
&&
2619 (c
!='\033' && c
!='\012' && c
!='\015' && c
!='\b'))
2621 if (term
->sco_acs
== 2) c
|= 0x80;
2624 switch (term
->cset_attr
[term
->cset
]) {
2626 * Linedraw characters are different from 'ESC ( B'
2627 * only for a small range. For ones outside that
2628 * range, make sure we use the same font as well as
2629 * the same encoding.
2632 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2633 c
= term
->ucsdata
->unitab_ctrl
[c
];
2635 c
= ((unsigned char) c
) | CSET_LINEDRW
;
2639 /* If UK-ASCII, make the '#' a LineDraw Pound */
2641 c
= '}' | CSET_LINEDRW
;
2644 /*FALLTHROUGH*/ case CSET_ASCII
:
2645 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2646 c
= term
->ucsdata
->unitab_ctrl
[c
];
2648 c
= ((unsigned char) c
) | CSET_ASCII
;
2651 if (c
>=' ') c
= ((unsigned char)c
) | CSET_SCOACS
;
2658 * How about C1 controls?
2659 * Explicitly ignore SCI (0x9a), which we don't translate to DECID.
2661 if ((c
& -32) == 0x80 && term
->termstate
< DO_CTRLS
&&
2662 !term
->vt52_mode
&& has_compat(VT220
)) {
2666 term
->termstate
= SEEN_ESC
;
2667 term
->esc_query
= FALSE
;
2668 c
= '@' + (c
& 0x1F);
2672 /* Or the GL control. */
2673 if (c
== '\177' && term
->termstate
< DO_CTRLS
&& has_compat(OTHER
)) {
2674 if (term
->curs
.x
&& !term
->wrapnext
)
2676 term
->wrapnext
= FALSE
;
2677 /* destructive backspace might be disabled */
2678 if (!term
->cfg
.no_dbackspace
) {
2679 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2680 check_boundary(term
, term
->curs
.x
+1, term
->curs
.y
);
2681 copy_termchar(scrlineptr(term
->curs
.y
),
2682 term
->curs
.x
, &term
->erase_char
);
2685 /* Or normal C0 controls. */
2686 if ((c
& ~0x1F) == 0 && term
->termstate
< DO_CTRLS
) {
2688 case '\005': /* ENQ: terminal type query */
2690 * Strictly speaking this is VT100 but a VT100 defaults to
2691 * no response. Other terminals respond at their option.
2693 * Don't put a CR in the default string as this tends to
2694 * upset some weird software.
2696 compatibility(ANSIMIN
);
2698 char abuf
[lenof(term
->cfg
.answerback
)], *s
, *d
;
2699 for (s
= term
->cfg
.answerback
, d
= abuf
; *s
;) {
2701 char c
= ctrlparse(s
, &n
);
2709 lpage_send(term
->ldisc
, DEFAULT_CODEPAGE
,
2713 case '\007': /* BEL: Bell */
2715 struct beeptime
*newbeep
;
2716 unsigned long ticks
;
2718 ticks
= GETTICKCOUNT();
2720 if (!term
->beep_overloaded
) {
2721 newbeep
= snew(struct beeptime
);
2722 newbeep
->ticks
= ticks
;
2723 newbeep
->next
= NULL
;
2724 if (!term
->beephead
)
2725 term
->beephead
= newbeep
;
2727 term
->beeptail
->next
= newbeep
;
2728 term
->beeptail
= newbeep
;
2733 * Throw out any beeps that happened more than
2736 while (term
->beephead
&&
2737 term
->beephead
->ticks
< ticks
- term
->cfg
.bellovl_t
) {
2738 struct beeptime
*tmp
= term
->beephead
;
2739 term
->beephead
= tmp
->next
;
2741 if (!term
->beephead
)
2742 term
->beeptail
= NULL
;
2746 if (term
->cfg
.bellovl
&& term
->beep_overloaded
&&
2747 ticks
- term
->lastbeep
>= (unsigned)term
->cfg
.bellovl_s
) {
2749 * If we're currently overloaded and the
2750 * last beep was more than s seconds ago,
2751 * leave overload mode.
2753 term
->beep_overloaded
= FALSE
;
2754 } else if (term
->cfg
.bellovl
&& !term
->beep_overloaded
&&
2755 term
->nbeeps
>= term
->cfg
.bellovl_n
) {
2757 * Now, if we have n or more beeps
2758 * remaining in the queue, go into overload
2761 term
->beep_overloaded
= TRUE
;
2763 term
->lastbeep
= ticks
;
2766 * Perform an actual beep if we're not overloaded.
2768 if (!term
->cfg
.bellovl
|| !term
->beep_overloaded
) {
2769 do_beep(term
->frontend
, term
->cfg
.beep
);
2771 if (term
->cfg
.beep
== BELL_VISUAL
) {
2772 term_schedule_vbell(term
, FALSE
, 0);
2775 seen_disp_event(term
);
2778 case '\b': /* BS: Back space */
2779 if (term
->curs
.x
== 0 &&
2780 (term
->curs
.y
== 0 || term
->wrap
== 0))
2782 else if (term
->curs
.x
== 0 && term
->curs
.y
> 0)
2783 term
->curs
.x
= term
->cols
- 1, term
->curs
.y
--;
2784 else if (term
->wrapnext
)
2785 term
->wrapnext
= FALSE
;
2788 seen_disp_event(term
);
2790 case '\016': /* LS1: Locking-shift one */
2791 compatibility(VT100
);
2794 case '\017': /* LS0: Locking-shift zero */
2795 compatibility(VT100
);
2798 case '\033': /* ESC: Escape */
2799 if (term
->vt52_mode
)
2800 term
->termstate
= VT52_ESC
;
2802 compatibility(ANSIMIN
);
2803 term
->termstate
= SEEN_ESC
;
2804 term
->esc_query
= FALSE
;
2807 case '\015': /* CR: Carriage return */
2809 term
->wrapnext
= FALSE
;
2810 seen_disp_event(term
);
2811 term
->paste_hold
= 0;
2813 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
2815 case '\014': /* FF: Form feed */
2816 if (has_compat(SCOANSI
)) {
2817 move(term
, 0, 0, 0);
2818 erase_lots(term
, FALSE
, FALSE
, TRUE
);
2820 term
->wrapnext
= FALSE
;
2821 seen_disp_event(term
);
2824 case '\013': /* VT: Line tabulation */
2825 compatibility(VT100
);
2826 case '\012': /* LF: Line feed */
2827 if (term
->curs
.y
== term
->marg_b
)
2828 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2829 else if (term
->curs
.y
< term
->rows
- 1)
2831 if (term
->cfg
.lfhascr
)
2833 term
->wrapnext
= FALSE
;
2834 seen_disp_event(term
);
2835 term
->paste_hold
= 0;
2837 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
2839 case '\t': /* HT: Character tabulation */
2841 pos old_curs
= term
->curs
;
2842 termline
*ldata
= scrlineptr(term
->curs
.y
);
2846 } while (term
->curs
.x
< term
->cols
- 1 &&
2847 !term
->tabs
[term
->curs
.x
]);
2849 if ((ldata
->lattr
& LATTR_MODE
) != LATTR_NORM
) {
2850 if (term
->curs
.x
>= term
->cols
/ 2)
2851 term
->curs
.x
= term
->cols
/ 2 - 1;
2853 if (term
->curs
.x
>= term
->cols
)
2854 term
->curs
.x
= term
->cols
- 1;
2857 check_selection(term
, old_curs
, term
->curs
);
2859 seen_disp_event(term
);
2863 switch (term
->termstate
) {
2865 /* Only graphic characters get this far;
2866 * ctrls are stripped above */
2868 termline
*cline
= scrlineptr(term
->curs
.y
);
2873 width
= (term
->cfg
.cjk_ambig_wide ?
2874 mk_wcwidth_cjk((wchar_t) c
) :
2875 mk_wcwidth((wchar_t) c
));
2877 if (term
->wrapnext
&& term
->wrap
&& width
> 0) {
2878 cline
->lattr
|= LATTR_WRAPPED
;
2879 if (term
->curs
.y
== term
->marg_b
)
2880 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2881 else if (term
->curs
.y
< term
->rows
- 1)
2884 term
->wrapnext
= FALSE
;
2885 cline
= scrlineptr(term
->curs
.y
);
2887 if (term
->insert
&& width
> 0)
2889 if (term
->selstate
!= NO_SELECTION
) {
2890 pos cursplus
= term
->curs
;
2892 check_selection(term
, term
->curs
, cursplus
);
2894 if (((c
& CSET_MASK
) == CSET_ASCII
||
2895 (c
& CSET_MASK
) == 0) &&
2897 logtraffic(term
->logctx
, (unsigned char) c
,
2903 * If we're about to display a double-width
2904 * character starting in the rightmost
2905 * column, then we do something special
2906 * instead. We must print a space in the
2907 * last column of the screen, then wrap;
2908 * and we also set LATTR_WRAPPED2 which
2909 * instructs subsequent cut-and-pasting not
2910 * only to splice this line to the one
2911 * after it, but to ignore the space in the
2912 * last character position as well.
2913 * (Because what was actually output to the
2914 * terminal was presumably just a sequence
2915 * of CJK characters, and we don't want a
2916 * space to be pasted in the middle of
2917 * those just because they had the
2918 * misfortune to start in the wrong parity
2919 * column. xterm concurs.)
2921 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2922 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
2923 if (term
->curs
.x
== term
->cols
-1) {
2924 copy_termchar(cline
, term
->curs
.x
,
2926 cline
->lattr
|= LATTR_WRAPPED
| LATTR_WRAPPED2
;
2927 if (term
->curs
.y
== term
->marg_b
)
2928 scroll(term
, term
->marg_t
, term
->marg_b
,
2930 else if (term
->curs
.y
< term
->rows
- 1)
2933 cline
= scrlineptr(term
->curs
.y
);
2934 /* Now we must check_boundary again, of course. */
2935 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2936 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
2940 clear_cc(cline
, term
->curs
.x
);
2941 cline
->chars
[term
->curs
.x
].chr
= c
;
2942 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
2947 clear_cc(cline
, term
->curs
.x
);
2948 cline
->chars
[term
->curs
.x
].chr
= UCSWIDE
;
2949 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
2953 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2954 check_boundary(term
, term
->curs
.x
+1, term
->curs
.y
);
2957 clear_cc(cline
, term
->curs
.x
);
2958 cline
->chars
[term
->curs
.x
].chr
= c
;
2959 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
2963 if (term
->curs
.x
> 0) {
2964 int x
= term
->curs
.x
- 1;
2966 /* If we're in wrapnext state, the character
2967 * to combine with is _here_, not to our left. */
2972 * If the previous character is
2973 * UCSWIDE, back up another one.
2975 if (cline
->chars
[x
].chr
== UCSWIDE
) {
2980 add_cc(cline
, x
, c
);
2981 seen_disp_event(term
);
2988 if (term
->curs
.x
== term
->cols
) {
2990 term
->wrapnext
= TRUE
;
2991 if (term
->wrap
&& term
->vt52_mode
) {
2992 cline
->lattr
|= LATTR_WRAPPED
;
2993 if (term
->curs
.y
== term
->marg_b
)
2994 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2995 else if (term
->curs
.y
< term
->rows
- 1)
2998 term
->wrapnext
= FALSE
;
3001 seen_disp_event(term
);
3007 * This state is virtually identical to SEEN_ESC, with the
3008 * exception that we have an OSC sequence in the pipeline,
3009 * and _if_ we see a backslash, we process it.
3013 term
->termstate
= TOPLEVEL
;
3016 /* else fall through */
3018 if (c
>= ' ' && c
<= '/') {
3019 if (term
->esc_query
)
3020 term
->esc_query
= -1;
3022 term
->esc_query
= c
;
3025 term
->termstate
= TOPLEVEL
;
3026 switch (ANSI(c
, term
->esc_query
)) {
3027 case '[': /* enter CSI mode */
3028 term
->termstate
= SEEN_CSI
;
3029 term
->esc_nargs
= 1;
3030 term
->esc_args
[0] = ARG_DEFAULT
;
3031 term
->esc_query
= FALSE
;
3033 case ']': /* OSC: xterm escape sequences */
3034 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
3035 compatibility(OTHER
);
3036 term
->termstate
= SEEN_OSC
;
3037 term
->esc_args
[0] = 0;
3039 case '7': /* DECSC: save cursor */
3040 compatibility(VT100
);
3041 save_cursor(term
, TRUE
);
3043 case '8': /* DECRC: restore cursor */
3044 compatibility(VT100
);
3045 save_cursor(term
, FALSE
);
3046 seen_disp_event(term
);
3048 case '=': /* DECKPAM: Keypad application mode */
3049 compatibility(VT100
);
3050 term
->app_keypad_keys
= TRUE
;
3052 case '>': /* DECKPNM: Keypad numeric mode */
3053 compatibility(VT100
);
3054 term
->app_keypad_keys
= FALSE
;
3056 case 'D': /* IND: exactly equivalent to LF */
3057 compatibility(VT100
);
3058 if (term
->curs
.y
== term
->marg_b
)
3059 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
3060 else if (term
->curs
.y
< term
->rows
- 1)
3062 term
->wrapnext
= FALSE
;
3063 seen_disp_event(term
);
3065 case 'E': /* NEL: exactly equivalent to CR-LF */
3066 compatibility(VT100
);
3068 if (term
->curs
.y
== term
->marg_b
)
3069 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
3070 else if (term
->curs
.y
< term
->rows
- 1)
3072 term
->wrapnext
= FALSE
;
3073 seen_disp_event(term
);
3075 case 'M': /* RI: reverse index - backwards LF */
3076 compatibility(VT100
);
3077 if (term
->curs
.y
== term
->marg_t
)
3078 scroll(term
, term
->marg_t
, term
->marg_b
, -1, TRUE
);
3079 else if (term
->curs
.y
> 0)
3081 term
->wrapnext
= FALSE
;
3082 seen_disp_event(term
);
3084 case 'Z': /* DECID: terminal type query */
3085 compatibility(VT100
);
3087 ldisc_send(term
->ldisc
, term
->id_string
,
3088 strlen(term
->id_string
), 0);
3090 case 'c': /* RIS: restore power-on settings */
3091 compatibility(VT100
);
3092 power_on(term
, TRUE
);
3093 if (term
->ldisc
) /* cause ldisc to notice changes */
3094 ldisc_send(term
->ldisc
, NULL
, 0, 0);
3095 if (term
->reset_132
) {
3096 if (!term
->cfg
.no_remote_resize
)
3097 request_resize(term
->frontend
, 80, term
->rows
);
3098 term
->reset_132
= 0;
3101 seen_disp_event(term
);
3103 case 'H': /* HTS: set a tab */
3104 compatibility(VT100
);
3105 term
->tabs
[term
->curs
.x
] = TRUE
;
3108 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
3109 compatibility(VT100
);
3115 for (i
= 0; i
< term
->rows
; i
++) {
3116 ldata
= scrlineptr(i
);
3117 for (j
= 0; j
< term
->cols
; j
++) {
3118 copy_termchar(ldata
, j
,
3119 &term
->basic_erase_char
);
3120 ldata
->chars
[j
].chr
= 'E';
3122 ldata
->lattr
= LATTR_NORM
;
3125 seen_disp_event(term
);
3126 scrtop
.x
= scrtop
.y
= 0;
3128 scrbot
.y
= term
->rows
;
3129 check_selection(term
, scrtop
, scrbot
);
3133 case ANSI('3', '#'):
3134 case ANSI('4', '#'):
3135 case ANSI('5', '#'):
3136 case ANSI('6', '#'):
3137 compatibility(VT100
);
3141 switch (ANSI(c
, term
->esc_query
)) {
3142 case ANSI('3', '#'): /* DECDHL: 2*height, top */
3145 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
3148 case ANSI('5', '#'): /* DECSWL: normal */
3149 nlattr
= LATTR_NORM
;
3151 default: /* case ANSI('6', '#'): DECDWL: 2*width */
3152 nlattr
= LATTR_WIDE
;
3155 scrlineptr(term
->curs
.y
)->lattr
= nlattr
;
3158 /* GZD4: G0 designate 94-set */
3159 case ANSI('A', '('):
3160 compatibility(VT100
);
3161 if (!term
->cfg
.no_remote_charset
)
3162 term
->cset_attr
[0] = CSET_GBCHR
;
3164 case ANSI('B', '('):
3165 compatibility(VT100
);
3166 if (!term
->cfg
.no_remote_charset
)
3167 term
->cset_attr
[0] = CSET_ASCII
;
3169 case ANSI('0', '('):
3170 compatibility(VT100
);
3171 if (!term
->cfg
.no_remote_charset
)
3172 term
->cset_attr
[0] = CSET_LINEDRW
;
3174 case ANSI('U', '('):
3175 compatibility(OTHER
);
3176 if (!term
->cfg
.no_remote_charset
)
3177 term
->cset_attr
[0] = CSET_SCOACS
;
3179 /* G1D4: G1-designate 94-set */
3180 case ANSI('A', ')'):
3181 compatibility(VT100
);
3182 if (!term
->cfg
.no_remote_charset
)
3183 term
->cset_attr
[1] = CSET_GBCHR
;
3185 case ANSI('B', ')'):
3186 compatibility(VT100
);
3187 if (!term
->cfg
.no_remote_charset
)
3188 term
->cset_attr
[1] = CSET_ASCII
;
3190 case ANSI('0', ')'):
3191 compatibility(VT100
);
3192 if (!term
->cfg
.no_remote_charset
)
3193 term
->cset_attr
[1] = CSET_LINEDRW
;
3195 case ANSI('U', ')'):
3196 compatibility(OTHER
);
3197 if (!term
->cfg
.no_remote_charset
)
3198 term
->cset_attr
[1] = CSET_SCOACS
;
3200 /* DOCS: Designate other coding system */
3201 case ANSI('8', '%'): /* Old Linux code */
3202 case ANSI('G', '%'):
3203 compatibility(OTHER
);
3204 if (!term
->cfg
.no_remote_charset
)
3207 case ANSI('@', '%'):
3208 compatibility(OTHER
);
3209 if (!term
->cfg
.no_remote_charset
)
3215 term
->termstate
= TOPLEVEL
; /* default */
3217 if (term
->esc_nargs
<= ARGS_MAX
) {
3218 if (term
->esc_args
[term
->esc_nargs
- 1] == ARG_DEFAULT
)
3219 term
->esc_args
[term
->esc_nargs
- 1] = 0;
3220 term
->esc_args
[term
->esc_nargs
- 1] =
3221 10 * term
->esc_args
[term
->esc_nargs
- 1] + c
- '0';
3223 term
->termstate
= SEEN_CSI
;
3224 } else if (c
== ';') {
3225 if (++term
->esc_nargs
<= ARGS_MAX
)
3226 term
->esc_args
[term
->esc_nargs
- 1] = ARG_DEFAULT
;
3227 term
->termstate
= SEEN_CSI
;
3228 } else if (c
< '@') {
3229 if (term
->esc_query
)
3230 term
->esc_query
= -1;
3232 term
->esc_query
= TRUE
;
3234 term
->esc_query
= c
;
3235 term
->termstate
= SEEN_CSI
;
3237 switch (ANSI(c
, term
->esc_query
)) {
3238 case 'A': /* CUU: move up N lines */
3239 move(term
, term
->curs
.x
,
3240 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
3241 seen_disp_event(term
);
3243 case 'e': /* VPR: move down N lines */
3244 compatibility(ANSI
);
3246 case 'B': /* CUD: Cursor down */
3247 move(term
, term
->curs
.x
,
3248 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
3249 seen_disp_event(term
);
3251 case ANSI('c', '>'): /* DA: report xterm version */
3252 compatibility(OTHER
);
3253 /* this reports xterm version 136 so that VIM can
3254 use the drag messages from the mouse reporting */
3256 ldisc_send(term
->ldisc
, "\033[>0;136;0c", 11, 0);
3258 case 'a': /* HPR: move right N cols */
3259 compatibility(ANSI
);
3261 case 'C': /* CUF: Cursor right */
3262 move(term
, term
->curs
.x
+ def(term
->esc_args
[0], 1),
3264 seen_disp_event(term
);
3266 case 'D': /* CUB: move left N cols */
3267 move(term
, term
->curs
.x
- def(term
->esc_args
[0], 1),
3269 seen_disp_event(term
);
3271 case 'E': /* CNL: move down N lines and CR */
3272 compatibility(ANSI
);
3274 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
3275 seen_disp_event(term
);
3277 case 'F': /* CPL: move up N lines and CR */
3278 compatibility(ANSI
);
3280 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
3281 seen_disp_event(term
);
3284 case '`': /* HPA: set horizontal posn */
3285 compatibility(ANSI
);
3286 move(term
, def(term
->esc_args
[0], 1) - 1,
3288 seen_disp_event(term
);
3290 case 'd': /* VPA: set vertical posn */
3291 compatibility(ANSI
);
3292 move(term
, term
->curs
.x
,
3293 ((term
->dec_om ? term
->marg_t
: 0) +
3294 def(term
->esc_args
[0], 1) - 1),
3295 (term
->dec_om ?
2 : 0));
3296 seen_disp_event(term
);
3299 case 'f': /* HVP: set horz and vert posns at once */
3300 if (term
->esc_nargs
< 2)
3301 term
->esc_args
[1] = ARG_DEFAULT
;
3302 move(term
, def(term
->esc_args
[1], 1) - 1,
3303 ((term
->dec_om ? term
->marg_t
: 0) +
3304 def(term
->esc_args
[0], 1) - 1),
3305 (term
->dec_om ?
2 : 0));
3306 seen_disp_event(term
);
3308 case 'J': /* ED: erase screen or parts of it */
3310 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
3313 erase_lots(term
, FALSE
, !!(i
& 2), !!(i
& 1));
3316 seen_disp_event(term
);
3318 case 'K': /* EL: erase line or parts of it */
3320 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
3323 erase_lots(term
, TRUE
, !!(i
& 2), !!(i
& 1));
3325 seen_disp_event(term
);
3327 case 'L': /* IL: insert lines */
3328 compatibility(VT102
);
3329 if (term
->curs
.y
<= term
->marg_b
)
3330 scroll(term
, term
->curs
.y
, term
->marg_b
,
3331 -def(term
->esc_args
[0], 1), FALSE
);
3332 seen_disp_event(term
);
3334 case 'M': /* DL: delete lines */
3335 compatibility(VT102
);
3336 if (term
->curs
.y
<= term
->marg_b
)
3337 scroll(term
, term
->curs
.y
, term
->marg_b
,
3338 def(term
->esc_args
[0], 1),
3340 seen_disp_event(term
);
3342 case '@': /* ICH: insert chars */
3343 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
3344 compatibility(VT102
);
3345 insch(term
, def(term
->esc_args
[0], 1));
3346 seen_disp_event(term
);
3348 case 'P': /* DCH: delete chars */
3349 compatibility(VT102
);
3350 insch(term
, -def(term
->esc_args
[0], 1));
3351 seen_disp_event(term
);
3353 case 'c': /* DA: terminal type query */
3354 compatibility(VT100
);
3355 /* This is the response for a VT102 */
3357 ldisc_send(term
->ldisc
, term
->id_string
,
3358 strlen(term
->id_string
), 0);
3360 case 'n': /* DSR: cursor position query */
3362 if (term
->esc_args
[0] == 6) {
3364 sprintf(buf
, "\033[%d;%dR", term
->curs
.y
+ 1,
3366 ldisc_send(term
->ldisc
, buf
, strlen(buf
), 0);
3367 } else if (term
->esc_args
[0] == 5) {
3368 ldisc_send(term
->ldisc
, "\033[0n", 4, 0);
3372 case 'h': /* SM: toggle modes to high */
3374 compatibility(VT100
);
3377 for (i
= 0; i
< term
->esc_nargs
; i
++)
3378 toggle_mode(term
, term
->esc_args
[i
],
3379 term
->esc_query
, TRUE
);
3382 case 'i': /* MC: Media copy */
3384 compatibility(VT100
);
3386 if (term
->esc_nargs
!= 1) break;
3387 if (term
->esc_args
[0] == 5 && *term
->cfg
.printer
) {
3388 term
->printing
= TRUE
;
3389 term
->only_printing
= !term
->esc_query
;
3390 term
->print_state
= 0;
3391 term_print_setup(term
);
3392 } else if (term
->esc_args
[0] == 4 &&
3394 term_print_finish(term
);
3398 case 'l': /* RM: toggle modes to low */
3400 compatibility(VT100
);
3403 for (i
= 0; i
< term
->esc_nargs
; i
++)
3404 toggle_mode(term
, term
->esc_args
[i
],
3405 term
->esc_query
, FALSE
);
3408 case 'g': /* TBC: clear tabs */
3409 compatibility(VT100
);
3410 if (term
->esc_nargs
== 1) {
3411 if (term
->esc_args
[0] == 0) {
3412 term
->tabs
[term
->curs
.x
] = FALSE
;
3413 } else if (term
->esc_args
[0] == 3) {
3415 for (i
= 0; i
< term
->cols
; i
++)
3416 term
->tabs
[i
] = FALSE
;
3420 case 'r': /* DECSTBM: set scroll margins */
3421 compatibility(VT100
);
3422 if (term
->esc_nargs
<= 2) {
3424 top
= def(term
->esc_args
[0], 1) - 1;
3425 bot
= (term
->esc_nargs
<= 1
3426 || term
->esc_args
[1] == 0 ?
3428 def(term
->esc_args
[1], term
->rows
)) - 1;
3429 if (bot
>= term
->rows
)
3430 bot
= term
->rows
- 1;
3431 /* VTTEST Bug 9 - if region is less than 2 lines
3432 * don't change region.
3434 if (bot
- top
> 0) {
3439 * I used to think the cursor should be
3440 * placed at the top of the newly marginned
3441 * area. Apparently not: VMS TPU falls over
3444 * Well actually it should for
3447 term
->curs
.y
= (term
->dec_om ?
3449 seen_disp_event(term
);
3453 case 'm': /* SGR: set graphics rendition */
3456 * A VT100 without the AVO only had one
3457 * attribute, either underline or
3458 * reverse video depending on the
3459 * cursor type, this was selected by
3463 * This is sometimes DIM, eg on the
3466 * This is sometimes INVIS various ANSI.
3468 * This like 22 disables BOLD, DIM and INVIS
3470 * The ANSI colours appear on any
3471 * terminal that has colour (obviously)
3472 * but the interaction between sgr0 and
3473 * the colours varies but is usually
3474 * related to the background colour
3475 * erase item. The interaction between
3476 * colour attributes and the mono ones
3477 * is also very implementation
3480 * The 39 and 49 attributes are likely
3481 * to be unimplemented.
3484 for (i
= 0; i
< term
->esc_nargs
; i
++) {
3485 switch (def(term
->esc_args
[i
], 0)) {
3486 case 0: /* restore defaults */
3487 term
->curr_attr
= term
->default_attr
;
3489 case 1: /* enable bold */
3490 compatibility(VT100AVO
);
3491 term
->curr_attr
|= ATTR_BOLD
;
3493 case 21: /* (enable double underline) */
3494 compatibility(OTHER
);
3495 case 4: /* enable underline */
3496 compatibility(VT100AVO
);
3497 term
->curr_attr
|= ATTR_UNDER
;
3499 case 5: /* enable blink */
3500 compatibility(VT100AVO
);
3501 term
->curr_attr
|= ATTR_BLINK
;
3503 case 6: /* SCO light bkgrd */
3504 compatibility(SCOANSI
);
3505 term
->blink_is_real
= FALSE
;
3506 term
->curr_attr
|= ATTR_BLINK
;
3507 term_schedule_tblink(term
);
3509 case 7: /* enable reverse video */
3510 term
->curr_attr
|= ATTR_REVERSE
;
3512 case 10: /* SCO acs off */
3513 compatibility(SCOANSI
);
3514 if (term
->cfg
.no_remote_charset
) break;
3515 term
->sco_acs
= 0; break;
3516 case 11: /* SCO acs on */
3517 compatibility(SCOANSI
);
3518 if (term
->cfg
.no_remote_charset
) break;
3519 term
->sco_acs
= 1; break;
3520 case 12: /* SCO acs on, |0x80 */
3521 compatibility(SCOANSI
);
3522 if (term
->cfg
.no_remote_charset
) break;
3523 term
->sco_acs
= 2; break;
3524 case 22: /* disable bold */
3525 compatibility2(OTHER
, VT220
);
3526 term
->curr_attr
&= ~ATTR_BOLD
;
3528 case 24: /* disable underline */
3529 compatibility2(OTHER
, VT220
);
3530 term
->curr_attr
&= ~ATTR_UNDER
;
3532 case 25: /* disable blink */
3533 compatibility2(OTHER
, VT220
);
3534 term
->curr_attr
&= ~ATTR_BLINK
;
3536 case 27: /* disable reverse video */
3537 compatibility2(OTHER
, VT220
);
3538 term
->curr_attr
&= ~ATTR_REVERSE
;
3549 term
->curr_attr
&= ~ATTR_FGMASK
;
3551 (term
->esc_args
[i
] - 30)<<ATTR_FGSHIFT
;
3561 /* aixterm-style bright foreground */
3562 term
->curr_attr
&= ~ATTR_FGMASK
;
3564 ((term
->esc_args
[i
] - 90 + 8)
3567 case 39: /* default-foreground */
3568 term
->curr_attr
&= ~ATTR_FGMASK
;
3569 term
->curr_attr
|= ATTR_DEFFG
;
3580 term
->curr_attr
&= ~ATTR_BGMASK
;
3582 (term
->esc_args
[i
] - 40)<<ATTR_BGSHIFT
;
3592 /* aixterm-style bright background */
3593 term
->curr_attr
&= ~ATTR_BGMASK
;
3595 ((term
->esc_args
[i
] - 100 + 8)
3598 case 49: /* default-background */
3599 term
->curr_attr
&= ~ATTR_BGMASK
;
3600 term
->curr_attr
|= ATTR_DEFBG
;
3602 case 38: /* xterm 256-colour mode */
3603 if (i
+2 < term
->esc_nargs
&&
3604 term
->esc_args
[i
+1] == 5) {
3605 term
->curr_attr
&= ~ATTR_FGMASK
;
3607 ((term
->esc_args
[i
+2] & 0xFF)
3612 case 48: /* xterm 256-colour mode */
3613 if (i
+2 < term
->esc_nargs
&&
3614 term
->esc_args
[i
+1] == 5) {
3615 term
->curr_attr
&= ~ATTR_BGMASK
;
3617 ((term
->esc_args
[i
+2] & 0xFF)
3624 set_erase_char(term
);
3627 case 's': /* save cursor */
3628 save_cursor(term
, TRUE
);
3630 case 'u': /* restore cursor */
3631 save_cursor(term
, FALSE
);
3632 seen_disp_event(term
);
3634 case 't': /* DECSLPP: set page size - ie window height */
3636 * VT340/VT420 sequence DECSLPP, DEC only allows values
3637 * 24/25/36/48/72/144 other emulators (eg dtterm) use
3638 * illegal values (eg first arg 1..9) for window changing
3641 if (term
->esc_nargs
<= 1
3642 && (term
->esc_args
[0] < 1 ||
3643 term
->esc_args
[0] >= 24)) {
3644 compatibility(VT340TEXT
);
3645 if (!term
->cfg
.no_remote_resize
)
3646 request_resize(term
->frontend
, term
->cols
,
3647 def(term
->esc_args
[0], 24));
3649 } else if (term
->esc_nargs
>= 1 &&
3650 term
->esc_args
[0] >= 1 &&
3651 term
->esc_args
[0] < 24) {
3652 compatibility(OTHER
);
3654 switch (term
->esc_args
[0]) {
3658 set_iconic(term
->frontend
, FALSE
);
3661 set_iconic(term
->frontend
, TRUE
);
3664 if (term
->esc_nargs
>= 3) {
3665 if (!term
->cfg
.no_remote_resize
)
3666 move_window(term
->frontend
,
3667 def(term
->esc_args
[1], 0),
3668 def(term
->esc_args
[2], 0));
3672 /* We should resize the window to a given
3673 * size in pixels here, but currently our
3674 * resizing code isn't healthy enough to
3679 set_zorder(term
->frontend
, TRUE
);
3682 /* move to bottom */
3683 set_zorder(term
->frontend
, FALSE
);
3686 refresh_window(term
->frontend
);
3689 if (term
->esc_nargs
>= 3) {
3690 if (!term
->cfg
.no_remote_resize
)
3691 request_resize(term
->frontend
,
3692 def(term
->esc_args
[2], term
->cfg
.width
),
3693 def(term
->esc_args
[1], term
->cfg
.height
));
3697 if (term
->esc_nargs
>= 2)
3698 set_zoomed(term
->frontend
,
3704 ldisc_send(term
->ldisc
,
3705 is_iconic(term
->frontend
) ?
3706 "\033[1t" : "\033[2t", 4, 0);
3710 get_window_pos(term
->frontend
, &x
, &y
);
3711 len
= sprintf(buf
, "\033[3;%d;%dt", x
, y
);
3712 ldisc_send(term
->ldisc
, buf
, len
, 0);
3717 get_window_pixels(term
->frontend
, &x
, &y
);
3718 len
= sprintf(buf
, "\033[4;%d;%dt", x
, y
);
3719 ldisc_send(term
->ldisc
, buf
, len
, 0);
3724 len
= sprintf(buf
, "\033[8;%d;%dt",
3725 term
->rows
, term
->cols
);
3726 ldisc_send(term
->ldisc
, buf
, len
, 0);
3731 * Hmmm. Strictly speaking we
3732 * should return `the size of the
3733 * screen in characters', but
3734 * that's not easy: (a) window
3735 * furniture being what it is it's
3736 * hard to compute, and (b) in
3737 * resize-font mode maximising the
3738 * window wouldn't change the
3739 * number of characters. *shrug*. I
3740 * think we'll ignore it for the
3741 * moment and see if anyone
3742 * complains, and then ask them
3743 * what they would like it to do.
3748 !term
->cfg
.no_remote_qtitle
) {
3749 p
= get_window_title(term
->frontend
, TRUE
);
3751 ldisc_send(term
->ldisc
, "\033]L", 3, 0);
3752 ldisc_send(term
->ldisc
, p
, len
, 0);
3753 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
3758 !term
->cfg
.no_remote_qtitle
) {
3759 p
= get_window_title(term
->frontend
,FALSE
);
3761 ldisc_send(term
->ldisc
, "\033]l", 3, 0);
3762 ldisc_send(term
->ldisc
, p
, len
, 0);
3763 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
3769 case 'S': /* SU: Scroll up */
3770 compatibility(SCOANSI
);
3771 scroll(term
, term
->marg_t
, term
->marg_b
,
3772 def(term
->esc_args
[0], 1), TRUE
);
3773 term
->wrapnext
= FALSE
;
3774 seen_disp_event(term
);
3776 case 'T': /* SD: Scroll down */
3777 compatibility(SCOANSI
);
3778 scroll(term
, term
->marg_t
, term
->marg_b
,
3779 -def(term
->esc_args
[0], 1), TRUE
);
3780 term
->wrapnext
= FALSE
;
3781 seen_disp_event(term
);
3783 case ANSI('|', '*'): /* DECSNLS */
3785 * Set number of lines on screen
3786 * VT420 uses VGA like hardware and can
3787 * support any size in reasonable range
3788 * (24..49 AIUI) with no default specified.
3790 compatibility(VT420
);
3791 if (term
->esc_nargs
== 1 && term
->esc_args
[0] > 0) {
3792 if (!term
->cfg
.no_remote_resize
)
3793 request_resize(term
->frontend
, term
->cols
,
3794 def(term
->esc_args
[0],
3799 case ANSI('|', '$'): /* DECSCPP */
3801 * Set number of columns per page
3802 * Docs imply range is only 80 or 132, but
3805 compatibility(VT340TEXT
);
3806 if (term
->esc_nargs
<= 1) {
3807 if (!term
->cfg
.no_remote_resize
)
3808 request_resize(term
->frontend
,
3809 def(term
->esc_args
[0],
3810 term
->cfg
.width
), term
->rows
);
3814 case 'X': /* ECH: write N spaces w/o moving cursor */
3815 /* XXX VTTEST says this is vt220, vt510 manual
3817 compatibility(ANSIMIN
);
3819 int n
= def(term
->esc_args
[0], 1);
3821 int p
= term
->curs
.x
;
3822 termline
*cline
= scrlineptr(term
->curs
.y
);
3824 if (n
> term
->cols
- term
->curs
.x
)
3825 n
= term
->cols
- term
->curs
.x
;
3826 cursplus
= term
->curs
;
3828 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
3829 check_boundary(term
, term
->curs
.x
+n
, term
->curs
.y
);
3830 check_selection(term
, term
->curs
, cursplus
);
3832 copy_termchar(cline
, p
++,
3834 seen_disp_event(term
);
3837 case 'x': /* DECREQTPARM: report terminal characteristics */
3838 compatibility(VT100
);
3841 int i
= def(term
->esc_args
[0], 0);
3842 if (i
== 0 || i
== 1) {
3843 strcpy(buf
, "\033[2;1;1;112;112;1;0x");
3845 ldisc_send(term
->ldisc
, buf
, 20, 0);
3850 compatibility(OTHER
);
3852 int i
= def(term
->esc_args
[0], 1);
3853 pos old_curs
= term
->curs
;
3855 for(;i
>0 && term
->curs
.x
>0; i
--) {
3858 } while (term
->curs
.x
>0 &&
3859 !term
->tabs
[term
->curs
.x
]);
3861 check_selection(term
, old_curs
, term
->curs
);
3864 case ANSI('c', '='): /* Hide or Show Cursor */
3865 compatibility(SCOANSI
);
3866 switch(term
->esc_args
[0]) {
3867 case 0: /* hide cursor */
3868 term
->cursor_on
= FALSE
;
3870 case 1: /* restore cursor */
3871 term
->big_cursor
= FALSE
;
3872 term
->cursor_on
= TRUE
;
3874 case 2: /* block cursor */
3875 term
->big_cursor
= TRUE
;
3876 term
->cursor_on
= TRUE
;
3880 case ANSI('C', '='):
3882 * set cursor start on scanline esc_args[0] and
3883 * end on scanline esc_args[1].If you set
3884 * the bottom scan line to a value less than
3885 * the top scan line, the cursor will disappear.
3887 compatibility(SCOANSI
);
3888 if (term
->esc_nargs
>= 2) {
3889 if (term
->esc_args
[0] > term
->esc_args
[1])
3890 term
->cursor_on
= FALSE
;
3892 term
->cursor_on
= TRUE
;
3895 case ANSI('D', '='):
3896 compatibility(SCOANSI
);
3897 term
->blink_is_real
= FALSE
;
3898 term_schedule_tblink(term
);
3899 if (term
->esc_args
[0]>=1)
3900 term
->curr_attr
|= ATTR_BLINK
;
3902 term
->curr_attr
&= ~ATTR_BLINK
;
3904 case ANSI('E', '='):
3905 compatibility(SCOANSI
);
3906 term
->blink_is_real
= (term
->esc_args
[0] >= 1);
3907 term_schedule_tblink(term
);
3909 case ANSI('F', '='): /* set normal foreground */
3910 compatibility(SCOANSI
);
3911 if (term
->esc_args
[0] >= 0 && term
->esc_args
[0] < 16) {
3913 (sco2ansicolour
[term
->esc_args
[0] & 0x7] |
3914 (term
->esc_args
[0] & 0x8)) <<
3916 term
->curr_attr
&= ~ATTR_FGMASK
;
3917 term
->curr_attr
|= colour
;
3918 term
->default_attr
&= ~ATTR_FGMASK
;
3919 term
->default_attr
|= colour
;
3920 set_erase_char(term
);
3923 case ANSI('G', '='): /* set normal background */
3924 compatibility(SCOANSI
);
3925 if (term
->esc_args
[0] >= 0 && term
->esc_args
[0] < 16) {
3927 (sco2ansicolour
[term
->esc_args
[0] & 0x7] |
3928 (term
->esc_args
[0] & 0x8)) <<
3930 term
->curr_attr
&= ~ATTR_BGMASK
;
3931 term
->curr_attr
|= colour
;
3932 term
->default_attr
&= ~ATTR_BGMASK
;
3933 term
->default_attr
|= colour
;
3934 set_erase_char(term
);
3937 case ANSI('L', '='):
3938 compatibility(SCOANSI
);
3939 term
->use_bce
= (term
->esc_args
[0] <= 0);
3940 set_erase_char(term
);
3942 case ANSI('p', '"'): /* DECSCL: set compat level */
3944 * Allow the host to make this emulator a
3945 * 'perfect' VT102. This first appeared in
3946 * the VT220, but we do need to get back to
3947 * PuTTY mode so I won't check it.
3949 * The arg in 40..42,50 are a PuTTY extension.
3950 * The 2nd arg, 8bit vs 7bit is not checked.
3952 * Setting VT102 mode should also change
3953 * the Fkeys to generate PF* codes as a
3954 * real VT102 has no Fkeys. The VT220 does
3955 * this, F11..F13 become ESC,BS,LF other
3956 * Fkeys send nothing.
3958 * Note ESC c will NOT change this!
3961 switch (term
->esc_args
[0]) {
3963 term
->compatibility_level
&= ~TM_VTXXX
;
3964 term
->compatibility_level
|= TM_VT102
;
3967 term
->compatibility_level
&= ~TM_VTXXX
;
3968 term
->compatibility_level
|= TM_VT220
;
3972 if (term
->esc_args
[0] > 60 &&
3973 term
->esc_args
[0] < 70)
3974 term
->compatibility_level
|= TM_VTXXX
;
3978 term
->compatibility_level
&= TM_VTXXX
;
3981 term
->compatibility_level
= TM_PUTTY
;
3984 term
->compatibility_level
= TM_SCOANSI
;
3988 term
->compatibility_level
= TM_PUTTY
;
3994 /* Change the response to CSI c */
3995 if (term
->esc_args
[0] == 50) {
3998 strcpy(term
->id_string
, "\033[?");
3999 for (i
= 1; i
< term
->esc_nargs
; i
++) {
4001 strcat(term
->id_string
, ";");
4002 sprintf(lbuf
, "%d", term
->esc_args
[i
]);
4003 strcat(term
->id_string
, lbuf
);
4005 strcat(term
->id_string
, "c");
4008 /* Is this a good idea ?
4009 * Well we should do a soft reset at this point ...
4011 if (!has_compat(VT420
) && has_compat(VT100
)) {
4012 if (!term
->cfg
.no_remote_resize
) {
4013 if (term
->reset_132
)
4014 request_resize(132, 24);
4016 request_resize(80, 24);
4024 term
->osc_w
= FALSE
;
4026 case 'P': /* Linux palette sequence */
4027 term
->termstate
= SEEN_OSC_P
;
4028 term
->osc_strlen
= 0;
4030 case 'R': /* Linux palette reset */
4031 palette_reset(term
->frontend
);
4032 term_invalidate(term
);
4033 term
->termstate
= TOPLEVEL
;
4035 case 'W': /* word-set */
4036 term
->termstate
= SEEN_OSC_W
;
4049 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
4053 * Grotty hack to support xterm and DECterm title
4054 * sequences concurrently.
4056 if (term
->esc_args
[0] == 2) {
4057 term
->esc_args
[0] = 1;
4060 /* else fall through */
4062 term
->termstate
= OSC_STRING
;
4063 term
->osc_strlen
= 0;
4068 * This OSC stuff is EVIL. It takes just one character to get into
4069 * sysline mode and it's not initially obvious how to get out.
4070 * So I've added CR and LF as string aborts.
4071 * This shouldn't effect compatibility as I believe embedded
4072 * control characters are supposed to be interpreted (maybe?)
4073 * and they don't display anything useful anyway.
4077 if (c
== '\012' || c
== '\015') {
4078 term
->termstate
= TOPLEVEL
;
4079 } else if (c
== 0234 || c
== '\007') {
4081 * These characters terminate the string; ST and BEL
4082 * terminate the sequence and trigger instant
4083 * processing of it, whereas ESC goes back to SEEN_ESC
4084 * mode unless it is followed by \, in which case it is
4085 * synonymous with ST in the first place.
4088 term
->termstate
= TOPLEVEL
;
4089 } else if (c
== '\033')
4090 term
->termstate
= OSC_MAYBE_ST
;
4091 else if (term
->osc_strlen
< OSC_STR_MAX
)
4092 term
->osc_string
[term
->osc_strlen
++] = (char)c
;
4096 int max
= (term
->osc_strlen
== 0 ?
21 : 15);
4098 if ((int)c
>= '0' && (int)c
<= '9')
4100 else if ((int)c
>= 'A' && (int)c
<= 'A' + max
- 10)
4102 else if ((int)c
>= 'a' && (int)c
<= 'a' + max
- 10)
4105 term
->termstate
= TOPLEVEL
;
4108 term
->osc_string
[term
->osc_strlen
++] = val
;
4109 if (term
->osc_strlen
>= 7) {
4110 palette_set(term
->frontend
, term
->osc_string
[0],
4111 term
->osc_string
[1] * 16 + term
->osc_string
[2],
4112 term
->osc_string
[3] * 16 + term
->osc_string
[4],
4113 term
->osc_string
[5] * 16 + term
->osc_string
[6]);
4114 term_invalidate(term
);
4115 term
->termstate
= TOPLEVEL
;
4131 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
4134 term
->termstate
= OSC_STRING
;
4135 term
->osc_strlen
= 0;
4139 term
->termstate
= TOPLEVEL
;
4140 seen_disp_event(term
);
4143 move(term
, term
->curs
.x
, term
->curs
.y
- 1, 1);
4146 move(term
, term
->curs
.x
, term
->curs
.y
+ 1, 1);
4149 move(term
, term
->curs
.x
+ 1, term
->curs
.y
, 1);
4152 move(term
, term
->curs
.x
- 1, term
->curs
.y
, 1);
4155 * From the VT100 Manual
4156 * NOTE: The special graphics characters in the VT100
4157 * are different from those in the VT52
4159 * From VT102 manual:
4160 * 137 _ Blank - Same
4161 * 140 ` Reserved - Humm.
4162 * 141 a Solid rectangle - Similar
4163 * 142 b 1/ - Top half of fraction for the
4164 * 143 c 3/ - subscript numbers below.
4167 * 146 f Degrees - Same
4168 * 147 g Plus or minus - Same
4170 * 151 i Ellipsis (dots)
4173 * 154 l Bar at scan 0
4174 * 155 m Bar at scan 1
4175 * 156 n Bar at scan 2
4176 * 157 o Bar at scan 3 - Similar
4177 * 160 p Bar at scan 4 - Similar
4178 * 161 q Bar at scan 5 - Similar
4179 * 162 r Bar at scan 6 - Same
4180 * 163 s Bar at scan 7 - Similar
4195 term
->cset_attr
[term
->cset
= 0] = CSET_LINEDRW
;
4198 term
->cset_attr
[term
->cset
= 0] = CSET_ASCII
;
4201 move(term
, 0, 0, 0);
4204 if (term
->curs
.y
== 0)
4205 scroll(term
, 0, term
->rows
- 1, -1, TRUE
);
4206 else if (term
->curs
.y
> 0)
4208 term
->wrapnext
= FALSE
;
4211 erase_lots(term
, FALSE
, FALSE
, TRUE
);
4215 erase_lots(term
, TRUE
, FALSE
, TRUE
);
4219 /* XXX Print cursor line */
4222 /* XXX Start controller mode */
4225 /* XXX Stop controller mode */
4229 term
->termstate
= VT52_Y1
;
4233 ldisc_send(term
->ldisc
, "\033/Z", 3, 0);
4236 term
->app_keypad_keys
= TRUE
;
4239 term
->app_keypad_keys
= FALSE
;
4242 /* XXX This should switch to VT100 mode not current or default
4243 * VT mode. But this will only have effect in a VT220+
4246 term
->vt52_mode
= FALSE
;
4247 term
->blink_is_real
= term
->cfg
.blinktext
;
4248 term_schedule_tblink(term
);
4252 /* XXX Enter auto print mode */
4255 /* XXX Exit auto print mode */
4258 /* XXX Print screen */
4264 /* compatibility(ATARI) */
4265 move(term
, 0, 0, 0);
4266 erase_lots(term
, FALSE
, FALSE
, TRUE
);
4270 /* compatibility(ATARI) */
4271 if (term
->curs
.y
<= term
->marg_b
)
4272 scroll(term
, term
->curs
.y
, term
->marg_b
, -1, FALSE
);
4275 /* compatibility(ATARI) */
4276 if (term
->curs
.y
<= term
->marg_b
)
4277 scroll(term
, term
->curs
.y
, term
->marg_b
, 1, TRUE
);
4280 /* compatibility(ATARI) */
4281 term
->termstate
= VT52_FG
;
4284 /* compatibility(ATARI) */
4285 term
->termstate
= VT52_BG
;
4288 /* compatibility(ATARI) */
4289 erase_lots(term
, FALSE
, TRUE
, FALSE
);
4293 /* compatibility(ATARI) */
4294 term
->cursor_on
= TRUE
;
4297 /* compatibility(ATARI) */
4298 term
->cursor_on
= FALSE
;
4300 /* case 'j': Save cursor position - broken on ST */
4301 /* case 'k': Restore cursor position */
4303 /* compatibility(ATARI) */
4304 erase_lots(term
, TRUE
, TRUE
, TRUE
);
4306 term
->wrapnext
= FALSE
;
4309 /* compatibility(ATARI) */
4310 erase_lots(term
, TRUE
, TRUE
, FALSE
);
4313 /* compatibility(ATARI) */
4314 term
->curr_attr
|= ATTR_REVERSE
;
4317 /* compatibility(ATARI) */
4318 term
->curr_attr
&= ~ATTR_REVERSE
;
4320 case 'v': /* wrap Autowrap on - Wyse style */
4321 /* compatibility(ATARI) */
4324 case 'w': /* Autowrap off */
4325 /* compatibility(ATARI) */
4330 /* compatibility(OTHER) */
4331 term
->vt52_bold
= FALSE
;
4332 term
->curr_attr
= ATTR_DEFAULT
;
4333 set_erase_char(term
);
4336 /* compatibility(VI50) */
4337 term
->curr_attr
|= ATTR_UNDER
;
4340 /* compatibility(VI50) */
4341 term
->curr_attr
&= ~ATTR_UNDER
;
4344 /* compatibility(VI50) */
4345 term
->vt52_bold
= TRUE
;
4346 term
->curr_attr
|= ATTR_BOLD
;
4349 /* compatibility(VI50) */
4350 term
->vt52_bold
= FALSE
;
4351 term
->curr_attr
&= ~ATTR_BOLD
;
4357 term
->termstate
= VT52_Y2
;
4358 move(term
, term
->curs
.x
, c
- ' ', 0);
4361 term
->termstate
= TOPLEVEL
;
4362 move(term
, c
- ' ', term
->curs
.y
, 0);
4367 term
->termstate
= TOPLEVEL
;
4368 term
->curr_attr
&= ~ATTR_FGMASK
;
4369 term
->curr_attr
&= ~ATTR_BOLD
;
4370 term
->curr_attr
|= (c
& 0xF) << ATTR_FGSHIFT
;
4371 set_erase_char(term
);
4374 term
->termstate
= TOPLEVEL
;
4375 term
->curr_attr
&= ~ATTR_BGMASK
;
4376 term
->curr_attr
&= ~ATTR_BLINK
;
4377 term
->curr_attr
|= (c
& 0xF) << ATTR_BGSHIFT
;
4378 set_erase_char(term
);
4381 default: break; /* placate gcc warning about enum use */
4383 if (term
->selstate
!= NO_SELECTION
) {
4384 pos cursplus
= term
->curs
;
4386 check_selection(term
, term
->curs
, cursplus
);
4390 term_print_flush(term
);
4391 if (term
->cfg
.logflush
)
4392 logflush(term
->logctx
);
4396 * To prevent having to run the reasonably tricky bidi algorithm
4397 * too many times, we maintain a cache of the last lineful of data
4398 * fed to the algorithm on each line of the display.
4400 static int term_bidi_cache_hit(Terminal
*term
, int line
,
4401 termchar
*lbefore
, int width
)
4405 if (!term
->pre_bidi_cache
)
4406 return FALSE
; /* cache doesn't even exist yet! */
4408 if (line
>= term
->bidi_cache_size
)
4409 return FALSE
; /* cache doesn't have this many lines */
4411 if (!term
->pre_bidi_cache
[line
].chars
)
4412 return FALSE
; /* cache doesn't contain _this_ line */
4414 if (term
->pre_bidi_cache
[line
].width
!= width
)
4415 return FALSE
; /* line is wrong width */
4417 for (i
= 0; i
< width
; i
++)
4418 if (!termchars_equal(term
->pre_bidi_cache
[line
].chars
+i
, lbefore
+i
))
4419 return FALSE
; /* line doesn't match cache */
4421 return TRUE
; /* it didn't match. */
4424 static void term_bidi_cache_store(Terminal
*term
, int line
, termchar
*lbefore
,
4425 termchar
*lafter
, bidi_char
*wcTo
,
4426 int width
, int size
)
4430 if (!term
->pre_bidi_cache
|| term
->bidi_cache_size
<= line
) {
4431 int j
= term
->bidi_cache_size
;
4432 term
->bidi_cache_size
= line
+1;
4433 term
->pre_bidi_cache
= sresize(term
->pre_bidi_cache
,
4434 term
->bidi_cache_size
,
4435 struct bidi_cache_entry
);
4436 term
->post_bidi_cache
= sresize(term
->post_bidi_cache
,
4437 term
->bidi_cache_size
,
4438 struct bidi_cache_entry
);
4439 while (j
< term
->bidi_cache_size
) {
4440 term
->pre_bidi_cache
[j
].chars
=
4441 term
->post_bidi_cache
[j
].chars
= NULL
;
4442 term
->pre_bidi_cache
[j
].width
=
4443 term
->post_bidi_cache
[j
].width
= -1;
4444 term
->pre_bidi_cache
[j
].forward
=
4445 term
->post_bidi_cache
[j
].forward
= NULL
;
4446 term
->pre_bidi_cache
[j
].backward
=
4447 term
->post_bidi_cache
[j
].backward
= NULL
;
4452 sfree(term
->pre_bidi_cache
[line
].chars
);
4453 sfree(term
->post_bidi_cache
[line
].chars
);
4454 sfree(term
->post_bidi_cache
[line
].forward
);
4455 sfree(term
->post_bidi_cache
[line
].backward
);
4457 term
->pre_bidi_cache
[line
].width
= width
;
4458 term
->pre_bidi_cache
[line
].chars
= snewn(size
, termchar
);
4459 term
->post_bidi_cache
[line
].width
= width
;
4460 term
->post_bidi_cache
[line
].chars
= snewn(size
, termchar
);
4461 term
->post_bidi_cache
[line
].forward
= snewn(width
, int);
4462 term
->post_bidi_cache
[line
].backward
= snewn(width
, int);
4464 memcpy(term
->pre_bidi_cache
[line
].chars
, lbefore
, size
* TSIZE
);
4465 memcpy(term
->post_bidi_cache
[line
].chars
, lafter
, size
* TSIZE
);
4466 memset(term
->post_bidi_cache
[line
].forward
, 0, width
* sizeof(int));
4467 memset(term
->post_bidi_cache
[line
].backward
, 0, width
* sizeof(int));
4469 for (i
= 0; i
< width
; i
++) {
4470 int p
= wcTo
[i
].index
;
4472 assert(0 <= p
&& p
< width
);
4474 term
->post_bidi_cache
[line
].backward
[i
] = p
;
4475 term
->post_bidi_cache
[line
].forward
[p
] = i
;
4480 * Prepare the bidi information for a screen line. Returns the
4481 * transformed list of termchars, or NULL if no transformation at
4482 * all took place (because bidi is disabled). If return was
4483 * non-NULL, auxiliary information such as the forward and reverse
4484 * mappings of permutation position are available in
4485 * term->post_bidi_cache[scr_y].*.
4487 static termchar
*term_bidi_line(Terminal
*term
, struct termline
*ldata
,
4493 /* Do Arabic shaping and bidi. */
4494 if(!term
->cfg
.bidi
|| !term
->cfg
.arabicshaping
) {
4496 if (!term_bidi_cache_hit(term
, scr_y
, ldata
->chars
, term
->cols
)) {
4498 if (term
->wcFromTo_size
< term
->cols
) {
4499 term
->wcFromTo_size
= term
->cols
;
4500 term
->wcFrom
= sresize(term
->wcFrom
, term
->wcFromTo_size
,
4502 term
->wcTo
= sresize(term
->wcTo
, term
->wcFromTo_size
,
4506 for(it
=0; it
<term
->cols
; it
++)
4508 unsigned long uc
= (ldata
->chars
[it
].chr
);
4510 switch (uc
& CSET_MASK
) {
4512 if (!term
->cfg
.rawcnp
) {
4513 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
4517 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
4520 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
4523 switch (uc
& CSET_MASK
) {
4525 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
4528 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
4532 term
->wcFrom
[it
].origwc
= term
->wcFrom
[it
].wc
=
4534 term
->wcFrom
[it
].index
= it
;
4538 do_bidi(term
->wcFrom
, term
->cols
);
4540 /* this is saved iff done from inside the shaping */
4541 if(!term
->cfg
.bidi
&& term
->cfg
.arabicshaping
)
4542 for(it
=0; it
<term
->cols
; it
++)
4543 term
->wcTo
[it
] = term
->wcFrom
[it
];
4545 if(!term
->cfg
.arabicshaping
)
4546 do_shape(term
->wcFrom
, term
->wcTo
, term
->cols
);
4548 if (term
->ltemp_size
< ldata
->size
) {
4549 term
->ltemp_size
= ldata
->size
;
4550 term
->ltemp
= sresize(term
->ltemp
, term
->ltemp_size
,
4554 memcpy(term
->ltemp
, ldata
->chars
, ldata
->size
* TSIZE
);
4556 for(it
=0; it
<term
->cols
; it
++)
4558 term
->ltemp
[it
] = ldata
->chars
[term
->wcTo
[it
].index
];
4559 if (term
->ltemp
[it
].cc_next
)
4560 term
->ltemp
[it
].cc_next
-=
4561 it
- term
->wcTo
[it
].index
;
4563 if (term
->wcTo
[it
].origwc
!= term
->wcTo
[it
].wc
)
4564 term
->ltemp
[it
].chr
= term
->wcTo
[it
].wc
;
4566 term_bidi_cache_store(term
, scr_y
, ldata
->chars
,
4567 term
->ltemp
, term
->wcTo
,
4568 term
->cols
, ldata
->size
);
4570 lchars
= term
->ltemp
;
4572 lchars
= term
->post_bidi_cache
[scr_y
].chars
;
4582 * Given a context, update the window. Out of paranoia, we don't
4583 * allow WM_PAINT responses to do scrolling optimisations.
4585 static void do_paint(Terminal
*term
, Context ctx
, int may_optimise
)
4587 int i
, j
, our_curs_y
, our_curs_x
;
4592 #ifdef OPTIMISE_SCROLL
4593 struct scrollregion
*sr
;
4594 #endif /* OPTIMISE_SCROLL */
4598 ch
= snewn(chlen
, wchar_t);
4600 newline
= snewn(term
->cols
, termchar
);
4602 rv
= (!term
->rvideo
^ !term
->in_vbell ? ATTR_REVERSE
: 0);
4605 * screen array, disptop, scrtop,
4607 * cfg.blinkpc, blink_is_real, tblinker,
4608 * curs.y, curs.x, cblinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
4611 /* Has the cursor position or type changed ? */
4612 if (term
->cursor_on
) {
4613 if (term
->has_focus
) {
4614 if (term
->cblinker
|| !term
->cfg
.blink_cur
)
4615 cursor
= TATTR_ACTCURS
;
4619 cursor
= TATTR_PASCURS
;
4621 cursor
|= TATTR_RIGHTCURS
;
4624 our_curs_y
= term
->curs
.y
- term
->disptop
;
4627 * Adjust the cursor position:
4629 * - in the case where it's resting on the right-hand half
4630 * of a CJK wide character. xterm's behaviour here,
4631 * which seems adequate to me, is to display the cursor
4632 * covering the _whole_ character, exactly as if it were
4633 * one space to the left.
4635 termline
*ldata
= lineptr(term
->curs
.y
);
4638 our_curs_x
= term
->curs
.x
;
4640 if ( (lchars
= term_bidi_line(term
, ldata
, our_curs_y
)) != NULL
) {
4641 our_curs_x
= term
->post_bidi_cache
[our_curs_y
].forward
[our_curs_x
];
4643 lchars
= ldata
->chars
;
4645 if (our_curs_x
> 0 &&
4646 lchars
[our_curs_x
].chr
== UCSWIDE
)
4653 * If the cursor is not where it was last time we painted, and
4654 * its previous position is visible on screen, invalidate its
4655 * previous position.
4657 if (term
->dispcursy
>= 0 &&
4658 (term
->curstype
!= cursor
||
4659 term
->dispcursy
!= our_curs_y
||
4660 term
->dispcursx
!= our_curs_x
)) {
4661 termchar
*dispcurs
= term
->disptext
[term
->dispcursy
]->chars
+
4664 if (term
->dispcursx
> 0 && dispcurs
->chr
== UCSWIDE
)
4665 dispcurs
[-1].attr
|= ATTR_INVALID
;
4666 if (term
->dispcursx
< term
->cols
-1 && dispcurs
[1].chr
== UCSWIDE
)
4667 dispcurs
[1].attr
|= ATTR_INVALID
;
4668 dispcurs
->attr
|= ATTR_INVALID
;
4672 term
->dispcursx
= term
->dispcursy
= -1;
4674 #ifdef OPTIMISE_SCROLL
4676 sr
= term
->scrollhead
;
4678 struct scrollregion
*next
= sr
->next
;
4679 do_scroll(ctx
, sr
->topline
, sr
->botline
, sr
->lines
);
4683 term
->scrollhead
= term
->scrolltail
= NULL
;
4684 #endif /* OPTIMISE_SCROLL */
4686 /* The normal screen data */
4687 for (i
= 0; i
< term
->rows
; i
++) {
4690 int dirty_line
, dirty_run
, selected
;
4691 unsigned long attr
= 0, cset
= 0;
4692 int updated_line
= 0;
4695 int last_run_dirty
= 0;
4696 int laststart
, dirtyrect
;
4699 scrpos
.y
= i
+ term
->disptop
;
4700 ldata
= lineptr(scrpos
.y
);
4702 /* Do Arabic shaping and bidi. */
4703 lchars
= term_bidi_line(term
, ldata
, i
);
4705 backward
= term
->post_bidi_cache
[i
].backward
;
4707 lchars
= ldata
->chars
;
4712 * First loop: work along the line deciding what we want
4713 * each character cell to look like.
4715 for (j
= 0; j
< term
->cols
; j
++) {
4716 unsigned long tattr
, tchar
;
4717 termchar
*d
= lchars
+ j
;
4718 scrpos
.x
= backward ? backward
[j
] : j
;
4723 if (!term
->cfg
.ansi_colour
)
4724 tattr
= (tattr
& ~(ATTR_FGMASK
| ATTR_BGMASK
)) |
4725 ATTR_DEFFG
| ATTR_DEFBG
;
4727 if (!term
->cfg
.xterm_256_colour
) {
4729 colour
= (tattr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
;
4730 if (colour
>= 16 && colour
< 256)
4731 tattr
= (tattr
&~ ATTR_FGMASK
) | ATTR_DEFFG
;
4732 colour
= (tattr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
;
4733 if (colour
>= 16 && colour
< 256)
4734 tattr
= (tattr
&~ ATTR_BGMASK
) | ATTR_DEFBG
;
4737 switch (tchar
& CSET_MASK
) {
4739 tchar
= term
->ucsdata
->unitab_line
[tchar
& 0xFF];
4742 tchar
= term
->ucsdata
->unitab_xterm
[tchar
& 0xFF];
4745 tchar
= term
->ucsdata
->unitab_scoacs
[tchar
&0xFF];
4748 if (j
< term
->cols
-1 && d
[1].chr
== UCSWIDE
)
4751 /* Video reversing things */
4752 if (term
->selstate
== DRAGGING
|| term
->selstate
== SELECTED
) {
4753 if (term
->seltype
== LEXICOGRAPHIC
)
4754 selected
= (posle(term
->selstart
, scrpos
) &&
4755 poslt(scrpos
, term
->selend
));
4757 selected
= (posPle(term
->selstart
, scrpos
) &&
4758 posPlt(scrpos
, term
->selend
));
4762 ^ (selected ? ATTR_REVERSE
: 0));
4764 /* 'Real' blinking ? */
4765 if (term
->blink_is_real
&& (tattr
& ATTR_BLINK
)) {
4766 if (term
->has_focus
&& term
->tblinker
) {
4767 tchar
= term
->ucsdata
->unitab_line
[(unsigned char)' '];
4769 tattr
&= ~ATTR_BLINK
;
4773 * Check the font we'll _probably_ be using to see if
4774 * the character is wide when we don't want it to be.
4776 if (tchar
!= term
->disptext
[i
]->chars
[j
].chr
||
4777 tattr
!= (term
->disptext
[i
]->chars
[j
].attr
&~
4778 (ATTR_NARROW
| DATTR_MASK
))) {
4779 if ((tattr
& ATTR_WIDE
) == 0 && char_width(ctx
, tchar
) == 2)
4780 tattr
|= ATTR_NARROW
;
4781 } else if (term
->disptext
[i
]->chars
[j
].attr
& ATTR_NARROW
)
4782 tattr
|= ATTR_NARROW
;
4784 if (i
== our_curs_y
&& j
== our_curs_x
) {
4786 term
->curstype
= cursor
;
4787 term
->dispcursx
= j
;
4788 term
->dispcursy
= i
;
4792 newline
[j
].attr
= tattr
;
4793 newline
[j
].chr
= tchar
;
4794 /* Combining characters are still read from lchars */
4795 newline
[j
].cc_next
= 0;
4799 * Now loop over the line again, noting where things have
4802 * During this loop, we keep track of where we last saw
4803 * DATTR_STARTRUN. Any mismatch automatically invalidates
4804 * _all_ of the containing run that was last printed: that
4805 * is, any rectangle that was drawn in one go in the
4806 * previous update should be either left completely alone
4807 * or overwritten in its entirety. This, along with the
4808 * expectation that front ends clip all text runs to their
4809 * bounding rectangle, should solve any possible problems
4810 * with fonts that overflow their character cells.
4814 for (j
= 0; j
< term
->cols
; j
++) {
4815 if (term
->disptext
[i
]->chars
[j
].attr
& DATTR_STARTRUN
) {
4820 if (term
->disptext
[i
]->chars
[j
].chr
!= newline
[j
].chr
||
4821 (term
->disptext
[i
]->chars
[j
].attr
&~ DATTR_MASK
)
4822 != newline
[j
].attr
) {
4826 for (k
= laststart
; k
< j
; k
++)
4827 term
->disptext
[i
]->chars
[k
].attr
|= ATTR_INVALID
;
4834 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
4838 * Finally, loop once more and actually do the drawing.
4840 dirty_run
= dirty_line
= (ldata
->lattr
!=
4841 term
->disptext
[i
]->lattr
);
4842 term
->disptext
[i
]->lattr
= ldata
->lattr
;
4844 for (j
= 0; j
< term
->cols
; j
++) {
4845 unsigned long tattr
, tchar
;
4846 int break_run
, do_copy
;
4847 termchar
*d
= lchars
+ j
;
4849 tattr
= newline
[j
].attr
;
4850 tchar
= newline
[j
].chr
;
4852 if ((term
->disptext
[i
]->chars
[j
].attr
^ tattr
) & ATTR_WIDE
)
4855 break_run
= ((tattr
^ attr
) & term
->attr_mask
) != 0;
4857 /* Special hack for VT100 Linedraw glyphs */
4858 if (tchar
>= 0x23BA && tchar
<= 0x23BD)
4862 * Separate out sequences of characters that have the
4863 * same CSET, if that CSET is a magic one.
4865 if (CSET_OF(tchar
) != cset
)
4869 * Break on both sides of any combined-character cell.
4871 if (d
->cc_next
!= 0 ||
4872 (j
> 0 && d
[-1].cc_next
!= 0))
4875 if (!term
->ucsdata
->dbcs_screenfont
&& !dirty_line
) {
4876 if (term
->disptext
[i
]->chars
[j
].chr
== tchar
&&
4877 (term
->disptext
[i
]->chars
[j
].attr
&~ DATTR_MASK
) == tattr
)
4879 else if (!dirty_run
&& ccount
== 1)
4884 if ((dirty_run
|| last_run_dirty
) && ccount
> 0) {
4885 do_text(ctx
, start
, i
, ch
, ccount
, attr
,
4887 if (attr
& (TATTR_ACTCURS
| TATTR_PASCURS
))
4888 do_cursor(ctx
, start
, i
, ch
, ccount
, attr
,
4896 cset
= CSET_OF(tchar
);
4897 if (term
->ucsdata
->dbcs_screenfont
)
4898 last_run_dirty
= dirty_run
;
4899 dirty_run
= dirty_line
;
4903 if (!termchars_equal_override(&term
->disptext
[i
]->chars
[j
],
4909 if (ccount
>= chlen
) {
4910 chlen
= ccount
+ 256;
4911 ch
= sresize(ch
, chlen
, wchar_t);
4913 ch
[ccount
++] = (wchar_t) tchar
;
4918 while (dd
->cc_next
) {
4919 unsigned long schar
;
4924 switch (schar
& CSET_MASK
) {
4926 schar
= term
->ucsdata
->unitab_line
[schar
& 0xFF];
4929 schar
= term
->ucsdata
->unitab_xterm
[schar
& 0xFF];
4932 schar
= term
->ucsdata
->unitab_scoacs
[schar
&0xFF];
4936 if (ccount
>= chlen
) {
4937 chlen
= ccount
+ 256;
4938 ch
= sresize(ch
, chlen
, wchar_t);
4940 ch
[ccount
++] = (wchar_t) schar
;
4943 attr
|= TATTR_COMBINING
;
4947 copy_termchar(term
->disptext
[i
], j
, d
);
4948 term
->disptext
[i
]->chars
[j
].chr
= tchar
;
4949 term
->disptext
[i
]->chars
[j
].attr
= tattr
;
4951 term
->disptext
[i
]->chars
[j
].attr
|= DATTR_STARTRUN
;
4954 /* If it's a wide char step along to the next one. */
4955 if (tattr
& ATTR_WIDE
) {
4956 if (++j
< term
->cols
) {
4959 * By construction above, the cursor should not
4960 * be on the right-hand half of this character.
4963 assert(!(i
== our_curs_y
&& j
== our_curs_x
));
4964 if (!termchars_equal(&term
->disptext
[i
]->chars
[j
], d
))
4966 copy_termchar(term
->disptext
[i
], j
, d
);
4970 if (dirty_run
&& ccount
> 0) {
4971 do_text(ctx
, start
, i
, ch
, ccount
, attr
,
4973 if (attr
& (TATTR_ACTCURS
| TATTR_PASCURS
))
4974 do_cursor(ctx
, start
, i
, ch
, ccount
, attr
,
4988 * Invalidate the whole screen so it will be repainted in full.
4990 void term_invalidate(Terminal
*term
)
4994 for (i
= 0; i
< term
->rows
; i
++)
4995 for (j
= 0; j
< term
->cols
; j
++)
4996 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
4998 term_schedule_update(term
);
5002 * Paint the window in response to a WM_PAINT message.
5004 void term_paint(Terminal
*term
, Context ctx
,
5005 int left
, int top
, int right
, int bottom
, int immediately
)
5008 if (left
< 0) left
= 0;
5009 if (top
< 0) top
= 0;
5010 if (right
>= term
->cols
) right
= term
->cols
-1;
5011 if (bottom
>= term
->rows
) bottom
= term
->rows
-1;
5013 for (i
= top
; i
<= bottom
&& i
< term
->rows
; i
++) {
5014 if ((term
->disptext
[i
]->lattr
& LATTR_MODE
) == LATTR_NORM
)
5015 for (j
= left
; j
<= right
&& j
< term
->cols
; j
++)
5016 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
5018 for (j
= left
/ 2; j
<= right
/ 2 + 1 && j
< term
->cols
; j
++)
5019 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
5023 do_paint (term
, ctx
, FALSE
);
5025 term_schedule_update(term
);
5030 * Attempt to scroll the scrollback. The second parameter gives the
5031 * position we want to scroll to; the first is +1 to denote that
5032 * this position is relative to the beginning of the scrollback, -1
5033 * to denote it is relative to the end, and 0 to denote that it is
5034 * relative to the current position.
5036 void term_scroll(Terminal
*term
, int rel
, int where
)
5038 int sbtop
= -sblines(term
);
5039 #ifdef OPTIMISE_SCROLL
5040 int olddisptop
= term
->disptop
;
5042 #endif /* OPTIMISE_SCROLL */
5044 term
->disptop
= (rel
< 0 ?
0 : rel
> 0 ? sbtop
: term
->disptop
) + where
;
5045 if (term
->disptop
< sbtop
)
5046 term
->disptop
= sbtop
;
5047 if (term
->disptop
> 0)
5050 #ifdef OPTIMISE_SCROLL
5051 shift
= (term
->disptop
- olddisptop
);
5052 if (shift
< term
->rows
&& shift
> -term
->rows
)
5053 scroll_display(term
, 0, term
->rows
- 1, shift
);
5054 #endif /* OPTIMISE_SCROLL */
5059 * Helper routine for clipme(): growing buffer.
5062 int buflen
; /* amount of allocated space in textbuf/attrbuf */
5063 int bufpos
; /* amount of actual data */
5064 wchar_t *textbuf
; /* buffer for copied text */
5065 wchar_t *textptr
; /* = textbuf + bufpos (current insertion point) */
5066 int *attrbuf
; /* buffer for copied attributes */
5067 int *attrptr
; /* = attrbuf + bufpos */
5070 static void clip_addchar(clip_workbuf
*b
, wchar_t chr
, int attr
)
5072 if (b
->bufpos
>= b
->buflen
) {
5074 b
->textbuf
= sresize(b
->textbuf
, b
->buflen
, wchar_t);
5075 b
->textptr
= b
->textbuf
+ b
->bufpos
;
5076 b
->attrbuf
= sresize(b
->attrbuf
, b
->buflen
, int);
5077 b
->attrptr
= b
->attrbuf
+ b
->bufpos
;
5079 *b
->textptr
++ = chr
;
5080 *b
->attrptr
++ = attr
;
5084 static void clipme(Terminal
*term
, pos top
, pos bottom
, int rect
, int desel
)
5092 buf
.textptr
= buf
.textbuf
= snewn(buf
.buflen
, wchar_t);
5093 buf
.attrptr
= buf
.attrbuf
= snewn(buf
.buflen
, int);
5095 old_top_x
= top
.x
; /* needed for rect==1 */
5097 while (poslt(top
, bottom
)) {
5099 termline
*ldata
= lineptr(top
.y
);
5103 * nlpos will point at the maximum position on this line we
5104 * should copy up to. So we start it at the end of the
5108 nlpos
.x
= term
->cols
;
5111 * ... move it backwards if there's unused space at the end
5112 * of the line (and also set `nl' if this is the case,
5113 * because in normal selection mode this means we need a
5114 * newline at the end)...
5116 if (!(ldata
->lattr
& LATTR_WRAPPED
)) {
5118 IS_SPACE_CHR(ldata
->chars
[nlpos
.x
- 1].chr
) &&
5119 !ldata
->chars
[nlpos
.x
- 1].cc_next
&&
5122 if (poslt(nlpos
, bottom
))
5124 } else if (ldata
->lattr
& LATTR_WRAPPED2
) {
5125 /* Ignore the last char on the line in a WRAPPED2 line. */
5130 * ... and then clip it to the terminal x coordinate if
5131 * we're doing rectangular selection. (In this case we
5132 * still did the above, so that copying e.g. the right-hand
5133 * column from a table doesn't fill with spaces on the
5137 if (nlpos
.x
> bottom
.x
)
5139 nl
= (top
.y
< bottom
.y
);
5142 while (poslt(top
, bottom
) && poslt(top
, nlpos
)) {
5145 sprintf(cbuf
, "<U+%04x>", (ldata
[top
.x
] & 0xFFFF));
5147 wchar_t cbuf
[16], *p
;
5151 if (ldata
->chars
[x
].chr
== UCSWIDE
) {
5157 int uc
= ldata
->chars
[x
].chr
;
5158 attr
= ldata
->chars
[x
].attr
;
5160 switch (uc
& CSET_MASK
) {
5162 if (!term
->cfg
.rawcnp
) {
5163 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
5167 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
5170 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
5173 switch (uc
& CSET_MASK
) {
5175 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
5178 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
5182 set
= (uc
& CSET_MASK
);
5183 c
= (uc
& ~CSET_MASK
);
5187 if (DIRECT_FONT(uc
)) {
5188 if (c
>= ' ' && c
!= 0x7F) {
5192 if (is_dbcs_leadbyte(term
->ucsdata
->font_codepage
, (BYTE
) c
)) {
5194 buf
[1] = (char) (0xFF & ldata
->chars
[top
.x
+ 1].chr
);
5195 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 2, wbuf
, 4);
5199 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 1, wbuf
, 4);
5203 memcpy(cbuf
, wbuf
, rv
* sizeof(wchar_t));
5210 for (p
= cbuf
; *p
; p
++)
5211 clip_addchar(&buf
, *p
, attr
);
5213 if (ldata
->chars
[x
].cc_next
)
5214 x
+= ldata
->chars
[x
].cc_next
;
5222 for (i
= 0; i
< sel_nl_sz
; i
++)
5223 clip_addchar(&buf
, sel_nl
[i
], 0);
5226 top
.x
= rect ? old_top_x
: 0;
5230 #if SELECTION_NUL_TERMINATED
5231 clip_addchar(&buf
, 0, 0);
5233 /* Finally, transfer all that to the clipboard. */
5234 write_clip(term
->frontend
, buf
.textbuf
, buf
.attrbuf
, buf
.bufpos
, desel
);
5239 void term_copyall(Terminal
*term
)
5243 tree234
*screen
= term
->screen
;
5244 top
.y
= -sblines(term
);
5246 bottom
.y
= find_last_nonempty_line(term
, screen
);
5247 bottom
.x
= term
->cols
;
5248 clipme(term
, top
, bottom
, 0, TRUE
);
5252 * The wordness array is mainly for deciding the disposition of the
5253 * US-ASCII characters.
5255 static int wordtype(Terminal
*term
, int uc
)
5258 int start
, end
, ctype
;
5260 static const struct ucsword ucs_words
[] = {
5266 0x037e, 0x037e, 1}, /* Greek question mark */
5268 0x0387, 0x0387, 1}, /* Greek ano teleia */
5270 0x055a, 0x055f, 1}, /* Armenian punctuation */
5272 0x0589, 0x0589, 1}, /* Armenian full stop */
5274 0x0700, 0x070d, 1}, /* Syriac punctuation */
5276 0x104a, 0x104f, 1}, /* Myanmar punctuation */
5278 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
5280 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
5282 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
5284 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
5286 0x1800, 0x180a, 1}, /* Mongolian punctuation */
5288 0x2000, 0x200a, 0}, /* Various spaces */
5290 0x2070, 0x207f, 2}, /* superscript */
5292 0x2080, 0x208f, 2}, /* subscript */
5294 0x200b, 0x27ff, 1}, /* punctuation and symbols */
5296 0x3000, 0x3000, 0}, /* ideographic space */
5298 0x3001, 0x3020, 1}, /* ideographic punctuation */
5300 0x303f, 0x309f, 3}, /* Hiragana */
5302 0x30a0, 0x30ff, 3}, /* Katakana */
5304 0x3300, 0x9fff, 3}, /* CJK Ideographs */
5306 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
5308 0xf900, 0xfaff, 3}, /* CJK Ideographs */
5310 0xfe30, 0xfe6b, 1}, /* punctuation forms */
5312 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
5314 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
5316 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
5318 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
5320 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
5324 const struct ucsword
*wptr
;
5326 switch (uc
& CSET_MASK
) {
5328 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
5331 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
5334 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
5337 switch (uc
& CSET_MASK
) {
5339 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
5342 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
5346 /* For DBCS fonts I can't do anything useful. Even this will sometimes
5347 * fail as there's such a thing as a double width space. :-(
5349 if (term
->ucsdata
->dbcs_screenfont
&&
5350 term
->ucsdata
->font_codepage
== term
->ucsdata
->line_codepage
)
5354 return term
->wordness
[uc
];
5356 for (wptr
= ucs_words
; wptr
->start
; wptr
++) {
5357 if (uc
>= wptr
->start
&& uc
<= wptr
->end
)
5365 * Spread the selection outwards according to the selection mode.
5367 static pos
sel_spread_half(Terminal
*term
, pos p
, int dir
)
5371 int topy
= -sblines(term
);
5373 ldata
= lineptr(p
.y
);
5375 switch (term
->selmode
) {
5378 * In this mode, every character is a separate unit, except
5379 * for runs of spaces at the end of a non-wrapping line.
5381 if (!(ldata
->lattr
& LATTR_WRAPPED
)) {
5382 termchar
*q
= ldata
->chars
+ term
->cols
;
5383 while (q
> ldata
->chars
&&
5384 IS_SPACE_CHR(q
[-1].chr
) && !q
[-1].cc_next
)
5386 if (q
== ldata
->chars
+ term
->cols
)
5388 if (p
.x
>= q
- ldata
->chars
)
5389 p
.x
= (dir
== -1 ? q
- ldata
->chars
: term
->cols
- 1);
5394 * In this mode, the units are maximal runs of characters
5395 * whose `wordness' has the same value.
5397 wvalue
= wordtype(term
, UCSGET(ldata
->chars
, p
.x
));
5400 int maxcols
= (ldata
->lattr
& LATTR_WRAPPED2 ?
5401 term
->cols
-1 : term
->cols
);
5402 if (p
.x
< maxcols
-1) {
5403 if (wordtype(term
, UCSGET(ldata
->chars
, p
.x
+1)) == wvalue
)
5408 if (ldata
->lattr
& LATTR_WRAPPED
) {
5410 ldata2
= lineptr(p
.y
+1);
5411 if (wordtype(term
, UCSGET(ldata2
->chars
, 0))
5428 if (wordtype(term
, UCSGET(ldata
->chars
, p
.x
-1)) == wvalue
)
5437 ldata2
= lineptr(p
.y
-1);
5438 maxcols
= (ldata2
->lattr
& LATTR_WRAPPED2 ?
5439 term
->cols
-1 : term
->cols
);
5440 if (ldata2
->lattr
& LATTR_WRAPPED
) {
5441 if (wordtype(term
, UCSGET(ldata2
->chars
, maxcols
-1))
5459 * In this mode, every line is a unit.
5461 p
.x
= (dir
== -1 ?
0 : term
->cols
- 1);
5469 static void sel_spread(Terminal
*term
)
5471 if (term
->seltype
== LEXICOGRAPHIC
) {
5472 term
->selstart
= sel_spread_half(term
, term
->selstart
, -1);
5473 decpos(term
->selend
);
5474 term
->selend
= sel_spread_half(term
, term
->selend
, +1);
5475 incpos(term
->selend
);
5479 void term_do_paste(Terminal
*term
)
5484 get_clip(term
->frontend
, &data
, &len
);
5485 if (data
&& len
> 0) {
5488 term_seen_key_event(term
); /* pasted data counts */
5490 if (term
->paste_buffer
)
5491 sfree(term
->paste_buffer
);
5492 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
5493 term
->paste_buffer
= snewn(len
, wchar_t);
5496 while (p
< data
+ len
) {
5497 while (p
< data
+ len
&&
5498 !(p
<= data
+ len
- sel_nl_sz
&&
5499 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
5504 for (i
= 0; i
< p
- q
; i
++) {
5505 term
->paste_buffer
[term
->paste_len
++] = q
[i
];
5509 if (p
<= data
+ len
- sel_nl_sz
&&
5510 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
5511 term
->paste_buffer
[term
->paste_len
++] = '\015';
5517 /* Assume a small paste will be OK in one go. */
5518 if (term
->paste_len
< 256) {
5520 luni_send(term
->ldisc
, term
->paste_buffer
, term
->paste_len
, 0);
5521 if (term
->paste_buffer
)
5522 sfree(term
->paste_buffer
);
5523 term
->paste_buffer
= 0;
5524 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
5527 get_clip(term
->frontend
, NULL
, NULL
);
5530 void term_mouse(Terminal
*term
, Mouse_Button braw
, Mouse_Button bcooked
,
5531 Mouse_Action a
, int x
, int y
, int shift
, int ctrl
, int alt
)
5535 int raw_mouse
= (term
->xterm_mouse
&&
5536 !term
->cfg
.no_mouse_rep
&&
5537 !(term
->cfg
.mouse_override
&& shift
));
5538 int default_seltype
;
5542 if (a
== MA_DRAG
&& !raw_mouse
)
5543 term_scroll(term
, 0, -1);
5545 if (y
>= term
->rows
) {
5547 if (a
== MA_DRAG
&& !raw_mouse
)
5548 term_scroll(term
, 0, +1);
5557 if (x
>= term
->cols
)
5560 selpoint
.y
= y
+ term
->disptop
;
5561 ldata
= lineptr(selpoint
.y
);
5563 if ((ldata
->lattr
& LATTR_MODE
) != LATTR_NORM
)
5567 * Transform x through the bidi algorithm to find the _logical_
5568 * click point from the physical one.
5570 if (term_bidi_line(term
, ldata
, y
) != NULL
) {
5571 x
= term
->post_bidi_cache
[y
].backward
[x
];
5578 int encstate
= 0, r
, c
;
5585 encstate
= 0x20; /* left button down */
5596 case MBT_WHEEL_DOWN
:
5599 default: break; /* placate gcc warning about enum use */
5603 if (term
->xterm_mouse
== 1)
5609 term
->mouse_is_down
= 0;
5612 if (term
->mouse_is_down
== braw
)
5614 term
->mouse_is_down
= braw
;
5616 default: break; /* placate gcc warning about enum use */
5625 sprintf(abuf
, "\033[M%c%c%c", encstate
, c
, r
);
5626 ldisc_send(term
->ldisc
, abuf
, 6, 0);
5632 * Set the selection type (rectangular or normal) at the start
5633 * of a selection attempt, from the state of Alt.
5635 if (!alt
^ !term
->cfg
.rect_select
)
5636 default_seltype
= RECTANGULAR
;
5638 default_seltype
= LEXICOGRAPHIC
;
5640 if (term
->selstate
== NO_SELECTION
) {
5641 term
->seltype
= default_seltype
;
5644 if (bcooked
== MBT_SELECT
&& a
== MA_CLICK
) {
5646 term
->selstate
= ABOUT_TO
;
5647 term
->seltype
= default_seltype
;
5648 term
->selanchor
= selpoint
;
5649 term
->selmode
= SM_CHAR
;
5650 } else if (bcooked
== MBT_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
5652 term
->selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
5653 term
->selstate
= DRAGGING
;
5654 term
->selstart
= term
->selanchor
= selpoint
;
5655 term
->selend
= term
->selstart
;
5656 incpos(term
->selend
);
5658 } else if ((bcooked
== MBT_SELECT
&& a
== MA_DRAG
) ||
5659 (bcooked
== MBT_EXTEND
&& a
!= MA_RELEASE
)) {
5660 if (term
->selstate
== ABOUT_TO
&& poseq(term
->selanchor
, selpoint
))
5662 if (bcooked
== MBT_EXTEND
&& a
!= MA_DRAG
&&
5663 term
->selstate
== SELECTED
) {
5664 if (term
->seltype
== LEXICOGRAPHIC
) {
5666 * For normal selection, we extend by moving
5667 * whichever end of the current selection is closer
5670 if (posdiff(selpoint
, term
->selstart
) <
5671 posdiff(term
->selend
, term
->selstart
) / 2) {
5672 term
->selanchor
= term
->selend
;
5673 decpos(term
->selanchor
);
5675 term
->selanchor
= term
->selstart
;
5679 * For rectangular selection, we have a choice of
5680 * _four_ places to put selanchor and selpoint: the
5681 * four corners of the selection.
5683 if (2*selpoint
.x
< term
->selstart
.x
+ term
->selend
.x
)
5684 term
->selanchor
.x
= term
->selend
.x
-1;
5686 term
->selanchor
.x
= term
->selstart
.x
;
5688 if (2*selpoint
.y
< term
->selstart
.y
+ term
->selend
.y
)
5689 term
->selanchor
.y
= term
->selend
.y
;
5691 term
->selanchor
.y
= term
->selstart
.y
;
5693 term
->selstate
= DRAGGING
;
5695 if (term
->selstate
!= ABOUT_TO
&& term
->selstate
!= DRAGGING
)
5696 term
->selanchor
= selpoint
;
5697 term
->selstate
= DRAGGING
;
5698 if (term
->seltype
== LEXICOGRAPHIC
) {
5700 * For normal selection, we set (selstart,selend) to
5701 * (selpoint,selanchor) in some order.
5703 if (poslt(selpoint
, term
->selanchor
)) {
5704 term
->selstart
= selpoint
;
5705 term
->selend
= term
->selanchor
;
5706 incpos(term
->selend
);
5708 term
->selstart
= term
->selanchor
;
5709 term
->selend
= selpoint
;
5710 incpos(term
->selend
);
5714 * For rectangular selection, we may need to
5715 * interchange x and y coordinates (if the user has
5716 * dragged in the -x and +y directions, or vice versa).
5718 term
->selstart
.x
= min(term
->selanchor
.x
, selpoint
.x
);
5719 term
->selend
.x
= 1+max(term
->selanchor
.x
, selpoint
.x
);
5720 term
->selstart
.y
= min(term
->selanchor
.y
, selpoint
.y
);
5721 term
->selend
.y
= max(term
->selanchor
.y
, selpoint
.y
);
5724 } else if ((bcooked
== MBT_SELECT
|| bcooked
== MBT_EXTEND
) &&
5726 if (term
->selstate
== DRAGGING
) {
5728 * We've completed a selection. We now transfer the
5729 * data to the clipboard.
5731 clipme(term
, term
->selstart
, term
->selend
,
5732 (term
->seltype
== RECTANGULAR
), FALSE
);
5733 term
->selstate
= SELECTED
;
5735 term
->selstate
= NO_SELECTION
;
5736 } else if (bcooked
== MBT_PASTE
5738 #if MULTICLICK_ONLY_EVENT
5739 || a
== MA_2CLK
|| a
== MA_3CLK
5742 request_paste(term
->frontend
);
5748 void term_key(Terminal
*term
, Key_Sym keysym
, wchar_t *text
, size_t tlen
,
5749 unsigned int modifiers
, unsigned int flags
)
5753 int prependesc
= FALSE
;
5757 fprintf(stderr
, "keysym = %d, %d chars:", keysym
, tlen
);
5758 for (i
= 0; i
< tlen
; i
++)
5759 fprintf(stderr
, " %04x", (unsigned)text
[i
]);
5760 fprintf(stderr
, "\n");
5764 if ((flags
& PKF_REPEAT
) && term
->repeat_off
)
5767 /* Currently, Meta always just prefixes everything with ESC. */
5768 if (modifiers
& PKM_META
)
5770 modifiers
&= ~PKM_META
;
5773 * Alt is only used for Alt+keypad, which isn't supported yet, so
5776 modifiers
&= ~PKM_ALT
;
5778 /* Standard local function keys */
5779 switch (modifiers
& (PKM_SHIFT
| PKM_CONTROL
)) {
5781 if (keysym
== PK_PAGEUP
)
5782 /* scroll up one page */;
5783 if (keysym
== PK_PAGEDOWN
)
5784 /* scroll down on page */;
5785 if (keysym
== PK_INSERT
)
5786 term_do_paste(term
);
5789 if (keysym
== PK_PAGEUP
)
5790 /* scroll up one line */;
5791 if (keysym
== PK_PAGEDOWN
)
5792 /* scroll down one line */;
5793 /* Control-Numlock for app-keypad mode switch */
5794 if (keysym
== PK_PF1
)
5795 term
->app_keypad_keys
^= 1;
5799 if (modifiers
& PKM_ALT
) {
5800 /* Alt+F4 (close) */
5801 /* Alt+Return (full screen) */
5802 /* Alt+Space (system menu) */
5805 if (keysym
== PK_NULL
&& (modifiers
& PKM_CONTROL
) && tlen
== 1 &&
5806 text
[0] >= 0x20 && text
[0] <= 0x7e) {
5807 /* ASCII chars + Control */
5808 if ((text
[0] >= 0x40 && text
[0] <= 0x5f) ||
5809 (text
[0] >= 0x61 && text
[0] <= 0x7a))
5813 * Control-2 should return ^@ (0x00), Control-6 should return
5814 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
5815 * the DOS keyboard handling did it, and we have nothing better
5816 * to do with the key combo in question, we'll also map
5817 * Control-Backquote to ^\ (0x1C).
5820 case ' ': text
[0] = 0x00; break;
5821 case '-': text
[0] = 0x1f; break;
5822 case '/': text
[0] = 0x1f; break;
5823 case '2': text
[0] = 0x00; break;
5824 case '3': text
[0] = 0x1b; break;
5825 case '4': text
[0] = 0x1c; break;
5826 case '5': text
[0] = 0x1d; break;
5827 case '6': text
[0] = 0x1e; break;
5828 case '7': text
[0] = 0x1f; break;
5829 case '8': text
[0] = 0x7f; break;
5830 case '`': text
[0] = 0x1c; break;
5835 /* Nethack keypad */
5836 if (term
->cfg
.nethack_keypad
) {
5839 case PK_KP1
: c
= 'b'; break;
5840 case PK_KP2
: c
= 'j'; break;
5841 case PK_KP3
: c
= 'n'; break;
5842 case PK_KP4
: c
= 'h'; break;
5843 case PK_KP5
: c
= '.'; break;
5844 case PK_KP6
: c
= 'l'; break;
5845 case PK_KP7
: c
= 'y'; break;
5846 case PK_KP8
: c
= 'k'; break;
5847 case PK_KP9
: c
= 'u'; break;
5848 default: break; /* else gcc warns `enum value not used' */
5852 if (modifiers
& PKM_CONTROL
)
5854 else if (modifiers
& PKM_SHIFT
)
5862 /* Numeric Keypad */
5863 if (PK_ISKEYPAD(keysym
)) {
5867 * In VT400 mode, PFn always emits an escape sequence. In
5868 * Linux and tilde modes, this only happens in app keypad mode.
5870 if (term
->cfg
.funky_type
== FUNKY_VT400
||
5871 ((term
->cfg
.funky_type
== FUNKY_LINUX
||
5872 term
->cfg
.funky_type
== FUNKY_TILDE
) &&
5873 term
->app_keypad_keys
&& !term
->cfg
.no_applic_k
)) {
5875 case PK_PF1
: xkey
= 'P'; break;
5876 case PK_PF2
: xkey
= 'Q'; break;
5877 case PK_PF3
: xkey
= 'R'; break;
5878 case PK_PF4
: xkey
= 'S'; break;
5879 default: break; /* else gcc warns `enum value not used' */
5882 if (term
->app_keypad_keys
&& !term
->cfg
.no_applic_k
) {
5884 case PK_KP0
: xkey
= 'p'; break;
5885 case PK_KP1
: xkey
= 'q'; break;
5886 case PK_KP2
: xkey
= 'r'; break;
5887 case PK_KP3
: xkey
= 's'; break;
5888 case PK_KP4
: xkey
= 't'; break;
5889 case PK_KP5
: xkey
= 'u'; break;
5890 case PK_KP6
: xkey
= 'v'; break;
5891 case PK_KP7
: xkey
= 'w'; break;
5892 case PK_KP8
: xkey
= 'x'; break;
5893 case PK_KP9
: xkey
= 'y'; break;
5894 case PK_KPDECIMAL
: xkey
= 'n'; break;
5895 case PK_KPENTER
: xkey
= 'M'; break;
5896 default: break; /* else gcc warns `enum value not used' */
5898 if (term
->cfg
.funky_type
== FUNKY_XTERM
&& tlen
> 0) {
5900 * xterm can't see the layout of the keypad, so it has
5901 * to rely on the X keysyms returned by the keys.
5902 * Hence, we look at the strings here, not the PuTTY
5903 * keysyms (which describe the layout).
5907 if (modifiers
& PKM_SHIFT
)
5912 case '/': xkey
= 'o'; break;
5913 case '*': xkey
= 'j'; break;
5914 case '-': xkey
= 'm'; break;
5918 * In all other modes, we try to retain the layout of
5919 * the DEC keypad in application mode.
5923 /* This key covers the '-' and ',' keys on a VT220 */
5924 if (modifiers
& PKM_SHIFT
)
5925 xkey
= 'm'; /* VT220 '-' */
5927 xkey
= 'l'; /* VT220 ',' */
5929 case PK_KPMINUS
: xkey
= 'm'; break;
5930 case PK_KPCOMMA
: xkey
= 'l'; break;
5931 default: break; /* else gcc warns `enum value not used' */
5936 if (term
->vt52_mode
) {
5937 if (xkey
>= 'P' && xkey
<= 'S')
5938 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
5940 p
+= sprintf((char *) p
, "\x1B?%c", xkey
);
5942 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
5945 /* Not in application mode -- treat the number pad as arrow keys? */
5946 if ((flags
& PKF_NUMLOCK
) == 0) {
5948 case PK_KP0
: keysym
= PK_INSERT
; break;
5949 case PK_KP1
: keysym
= PK_END
; break;
5950 case PK_KP2
: keysym
= PK_DOWN
; break;
5951 case PK_KP3
: keysym
= PK_PAGEDOWN
; break;
5952 case PK_KP4
: keysym
= PK_LEFT
; break;
5953 case PK_KP5
: keysym
= PK_REST
; break;
5954 case PK_KP6
: keysym
= PK_RIGHT
; break;
5955 case PK_KP7
: keysym
= PK_HOME
; break;
5956 case PK_KP8
: keysym
= PK_UP
; break;
5957 case PK_KP9
: keysym
= PK_PAGEUP
; break;
5958 default: break; /* else gcc warns `enum value not used' */
5963 /* Miscellaneous keys */
5970 *p
++ = (term
->cfg
.bksp_is_delete ?
0x7F : 0x08);
5971 else if (modifiers
== PKM_SHIFT
)
5972 /* We do the opposite of what is configured */
5973 *p
++ = (term
->cfg
.bksp_is_delete ?
0x08 : 0x7F);
5979 else if (modifiers
== PKM_SHIFT
)
5980 *p
++ = 0x1B, *p
++ = '[', *p
++ = 'Z';
5983 /* XXX window.c has ctrl+shift+space sending 0xa0 */
5985 if (modifiers
== PKM_CONTROL
)
5990 case PK_KPENTER
: /* Odd keypad modes handled above */
5991 if (modifiers
== 0) {
5993 if (term
->cr_lf_return
)
5997 default: break; /* else gcc warns `enum value not used' */
6000 /* SCO function keys and editing keys */
6001 if (term
->cfg
.funky_type
== FUNKY_SCO
) {
6002 if (PK_ISFKEY(keysym
) && keysym
<= PK_F12
) {
6003 static char const codes
[] =
6004 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
6005 int index
= keysym
- PK_F1
;
6007 if (modifiers
& PKM_SHIFT
) index
+= 12;
6008 if (modifiers
& PKM_CONTROL
) index
+= 24;
6009 p
+= sprintf((char *) p
, "\x1B[%c", codes
[index
]);
6012 if (PK_ISEDITING(keysym
)) {
6016 case PK_DELETE
: *p
++ = 0x7f; goto done
;
6017 case PK_HOME
: xkey
= 'H'; break;
6018 case PK_INSERT
: xkey
= 'L'; break;
6019 case PK_END
: xkey
= 'F'; break;
6020 case PK_PAGEUP
: xkey
= 'I'; break;
6021 case PK_PAGEDOWN
: xkey
= 'G'; break;
6022 default: break; /* else gcc warns `enum value not used' */
6024 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
6028 if (PK_ISEDITING(keysym
) && (modifiers
& PKM_SHIFT
) == 0) {
6031 if (term
->cfg
.funky_type
== FUNKY_XTERM
) {
6032 /* Xterm shuffles these keys, apparently. */
6034 case PK_HOME
: keysym
= PK_INSERT
; break;
6035 case PK_INSERT
: keysym
= PK_HOME
; break;
6036 case PK_DELETE
: keysym
= PK_END
; break;
6037 case PK_END
: keysym
= PK_PAGEUP
; break;
6038 case PK_PAGEUP
: keysym
= PK_DELETE
; break;
6039 case PK_PAGEDOWN
: keysym
= PK_PAGEDOWN
; break;
6040 default: break; /* else gcc warns `enum value not used' */
6045 if (term
->cfg
.rxvt_homeend
&&
6046 (keysym
== PK_HOME
|| keysym
== PK_END
)) {
6047 p
+= sprintf((char *) p
, keysym
== PK_HOME ?
"\x1B[H" : "\x1BOw");
6051 if (term
->vt52_mode
) {
6055 * A real VT52 doesn't have these, and a VT220 doesn't
6056 * send anything for them in VT52 mode.
6059 case PK_HOME
: xkey
= 'H'; break;
6060 case PK_INSERT
: xkey
= 'L'; break;
6061 case PK_DELETE
: xkey
= 'M'; break;
6062 case PK_END
: xkey
= 'E'; break;
6063 case PK_PAGEUP
: xkey
= 'I'; break;
6064 case PK_PAGEDOWN
: xkey
= 'G'; break;
6065 default: xkey
=0; break; /* else gcc warns `enum value not used'*/
6067 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
6072 case PK_HOME
: code
= 1; break;
6073 case PK_INSERT
: code
= 2; break;
6074 case PK_DELETE
: code
= 3; break;
6075 case PK_END
: code
= 4; break;
6076 case PK_PAGEUP
: code
= 5; break;
6077 case PK_PAGEDOWN
: code
= 6; break;
6078 default: code
= 0; break; /* else gcc warns `enum value not used' */
6080 p
+= sprintf((char *) p
, "\x1B[%d~", code
);
6084 if (PK_ISFKEY(keysym
)) {
6085 /* Map Shift+F1-F10 to F11-F20 */
6086 if (keysym
>= PK_F1
&& keysym
<= PK_F10
&& (modifiers
& PKM_SHIFT
))
6088 if ((term
->vt52_mode
|| term
->cfg
.funky_type
== FUNKY_VT100P
) &&
6090 /* XXX This overrides the XTERM/VT52 mode below */
6092 if (keysym
>= PK_F6
) offt
++;
6093 if (keysym
>= PK_F12
) offt
++;
6094 p
+= sprintf((char *) p
, term
->vt52_mode ?
"\x1B%c" : "\x1BO%c",
6095 'P' + keysym
- PK_F1
- offt
);
6098 if (term
->cfg
.funky_type
== FUNKY_LINUX
&& keysym
<= PK_F5
) {
6099 p
+= sprintf((char *) p
, "\x1B[[%c", 'A' + keysym
- PK_F1
);
6102 if (term
->cfg
.funky_type
== FUNKY_XTERM
&& keysym
<= PK_F4
) {
6103 if (term
->vt52_mode
)
6104 p
+= sprintf((char *) p
, "\x1B%c", 'P' + keysym
- PK_F1
);
6106 p
+= sprintf((char *) p
, "\x1BO%c", 'P' + keysym
- PK_F1
);
6109 p
+= sprintf((char *) p
, "\x1B[%d~", 11 + keysym
- PK_F1
);
6113 if (PK_ISCURSOR(keysym
)) {
6117 case PK_UP
: xkey
= 'A'; break;
6118 case PK_DOWN
: xkey
= 'B'; break;
6119 case PK_RIGHT
: xkey
= 'C'; break;
6120 case PK_LEFT
: xkey
= 'D'; break;
6121 case PK_REST
: xkey
= 'G'; break; /* centre key on number pad */
6122 default: xkey
= 0; break; /* else gcc warns `enum value not used' */
6124 if (term
->vt52_mode
)
6125 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
6127 int app_flg
= (term
->app_cursor_keys
&& !term
->cfg
.no_applic_c
);
6129 /* Useful mapping of Ctrl-arrows */
6130 if (modifiers
== PKM_CONTROL
)
6134 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
6136 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
6142 if (p
> output
|| tlen
> 0) {
6144 * Interrupt an ongoing paste. I'm not sure
6145 * this is sensible, but for the moment it's
6146 * preferable to having to faff about buffering
6152 * We need not bother about stdin backlogs
6153 * here, because in GUI PuTTY we can't do
6154 * anything about it anyway; there's no means
6155 * of asking Windows to hold off on KEYDOWN
6156 * messages. We _have_ to buffer everything
6159 term_seen_key_event(term
);
6163 fprintf(stderr
, "sending ESC\n");
6165 ldisc_send(term
->ldisc
, "\x1b", 1, 1);
6170 fprintf(stderr
, "sending %d bytes:", p
- output
);
6171 for (i
= 0; i
< p
- output
; i
++)
6172 fprintf(stderr
, " %02x", output
[i
]);
6173 fprintf(stderr
, "\n");
6175 ldisc_send(term
->ldisc
, output
, p
- output
, 1);
6176 } else if (tlen
> 0) {
6178 fprintf(stderr
, "sending %d unichars:", tlen
);
6179 for (i
= 0; i
< tlen
; i
++)
6180 fprintf(stderr
, " %04x", (unsigned) text
[i
]);
6181 fprintf(stderr
, "\n");
6183 luni_send(term
->ldisc
, text
, tlen
, 1);
6188 void term_nopaste(Terminal
*term
)
6190 if (term
->paste_len
== 0)
6192 sfree(term
->paste_buffer
);
6193 term
->paste_buffer
= NULL
;
6194 term
->paste_len
= 0;
6197 int term_paste_pending(Terminal
*term
)
6199 return term
->paste_len
!= 0;
6202 void term_paste(Terminal
*term
)
6204 long now
, paste_diff
;
6206 if (term
->paste_len
== 0)
6209 /* Don't wait forever to paste */
6210 if (term
->paste_hold
) {
6211 now
= GETTICKCOUNT();
6212 paste_diff
= now
- term
->last_paste
;
6213 if (paste_diff
>= 0 && paste_diff
< 450)
6216 term
->paste_hold
= 0;
6218 while (term
->paste_pos
< term
->paste_len
) {
6220 while (n
+ term
->paste_pos
< term
->paste_len
) {
6221 if (term
->paste_buffer
[term
->paste_pos
+ n
++] == '\015')
6225 luni_send(term
->ldisc
, term
->paste_buffer
+ term
->paste_pos
, n
, 0);
6226 term
->paste_pos
+= n
;
6228 if (term
->paste_pos
< term
->paste_len
) {
6229 term
->paste_hold
= 1;
6233 sfree(term
->paste_buffer
);
6234 term
->paste_buffer
= NULL
;
6235 term
->paste_len
= 0;
6238 static void deselect(Terminal
*term
)
6240 term
->selstate
= NO_SELECTION
;
6241 term
->selstart
.x
= term
->selstart
.y
= term
->selend
.x
= term
->selend
.y
= 0;
6244 void term_deselect(Terminal
*term
)
6250 int term_ldisc(Terminal
*term
, int option
)
6252 if (option
== LD_ECHO
)
6253 return term
->term_echoing
;
6254 if (option
== LD_EDIT
)
6255 return term
->term_editing
;
6259 int term_data(Terminal
*term
, int is_stderr
, const char *data
, int len
)
6261 bufchain_add(&term
->inbuf
, data
, len
);
6263 if (!term
->in_term_out
) {
6264 term
->in_term_out
= TRUE
;
6265 term_reset_cblink(term
);
6267 * During drag-selects, we do not process terminal input,
6268 * because the user will want the screen to hold still to
6271 if (term
->selstate
!= DRAGGING
)
6273 term
->in_term_out
= FALSE
;
6277 * term_out() always completely empties inbuf. Therefore,
6278 * there's no reason at all to return anything other than zero
6279 * from this function, because there _can't_ be a question of
6280 * the remote side needing to wait until term_out() has cleared
6283 * This is a slightly suboptimal way to deal with SSH-2 - in
6284 * principle, the window mechanism would allow us to continue
6285 * to accept data on forwarded ports and X connections even
6286 * while the terminal processing was going slowly - but we
6287 * can't do the 100% right thing without moving the terminal
6288 * processing into a separate thread, and that might hurt
6289 * portability. So we manage stdout buffering the old SSH-1 way:
6290 * if the terminal processing goes slowly, the whole SSH
6291 * connection stops accepting data until it's ready.
6293 * In practice, I can't imagine this causing serious trouble.
6299 * Write untrusted data to the terminal.
6300 * The only control character that should be honoured is \n (which
6301 * will behave as a CRLF).
6303 int term_data_untrusted(Terminal
*term
, const char *data
, int len
)
6306 /* FIXME: more sophisticated checking? */
6307 for (i
= 0; i
< len
; i
++) {
6308 if (data
[i
] == '\n')
6309 term_data(term
, 1, "\r\n", 2);
6310 else if (data
[i
] & 0x60)
6311 term_data(term
, 1, data
+ i
, 1);
6313 return 0; /* assumes that term_data() always returns 0 */
6316 void term_provide_logctx(Terminal
*term
, void *logctx
)
6318 term
->logctx
= logctx
;
6321 void term_set_focus(Terminal
*term
, int has_focus
)
6323 term
->has_focus
= has_focus
;
6324 term_schedule_cblink(term
);
6328 * Provide "auto" settings for remote tty modes, suitable for an
6329 * application with a terminal window.
6331 char *term_get_ttymode(Terminal
*term
, const char *mode
)
6334 if (strcmp(mode
, "ERASE") == 0) {
6335 val
= term
->cfg
.bksp_is_delete ?
"^?" : "^H";
6337 /* FIXME: perhaps we should set ONLCR based on cfg.lfhascr as well? */
6341 struct term_userpass_state
{
6343 int done_prompt
; /* printed out prompt yet? */
6344 size_t pos
; /* cursor position */
6348 * Process some terminal data in the course of username/password
6351 int term_get_userpass_input(Terminal
*term
, prompts_t
*p
,
6352 unsigned char *in
, int inlen
)
6354 struct term_userpass_state
*s
= (struct term_userpass_state
*)p
->data
;
6357 * First call. Set some stuff up.
6359 p
->data
= s
= snew(struct term_userpass_state
);
6362 /* We only print the `name' caption if we have to... */
6363 if (p
->name_reqd
&& p
->name
) {
6364 size_t l
= strlen(p
->name
);
6365 term_data_untrusted(term
, p
->name
, l
);
6366 if (p
->name
[l
-1] != '\n')
6367 term_data_untrusted(term
, "\n", 1);
6369 /* ...but we always print any `instruction'. */
6370 if (p
->instruction
) {
6371 size_t l
= strlen(p
->instruction
);
6372 term_data_untrusted(term
, p
->instruction
, l
);
6373 if (p
->instruction
[l
-1] != '\n')
6374 term_data_untrusted(term
, "\n", 1);
6377 * Zero all the results, in case we abort half-way through.
6381 for (i
= 0; i
< p
->n_prompts
; i
++)
6382 memset(p
->prompts
[i
]->result
, 0, p
->prompts
[i
]->result_len
);
6386 while (s
->curr_prompt
< p
->n_prompts
) {
6388 prompt_t
*pr
= p
->prompts
[s
->curr_prompt
];
6389 int finished_prompt
= 0;
6391 if (!s
->done_prompt
) {
6392 term_data_untrusted(term
, pr
->prompt
, strlen(pr
->prompt
));
6397 /* Breaking out here ensures that the prompt is printed even
6398 * if we're now waiting for user data. */
6399 if (!in
|| !inlen
) break;
6401 /* FIXME: should we be using local-line-editing code instead? */
6402 while (!finished_prompt
&& inlen
) {
6408 term_data(term
, 0, "\r\n", 2);
6409 pr
->result
[s
->pos
] = '\0';
6410 pr
->result
[pr
->result_len
- 1] = '\0';
6411 /* go to next prompt, if any */
6414 finished_prompt
= 1; /* break out */
6420 term_data(term
, 0, "\b \b", 3);
6426 while (s
->pos
> 0) {
6428 term_data(term
, 0, "\b \b", 3);
6434 /* Immediate abort. */
6435 term_data(term
, 0, "\r\n", 2);
6438 return 0; /* user abort */
6441 * This simplistic check for printability is disabled
6442 * when we're doing password input, because some people
6443 * have control characters in their passwords.
6446 (c
>= ' ' && c
<= '~') ||
6447 ((unsigned char) c
>= 160))
6448 && s
->pos
< pr
->result_len
- 1) {
6449 pr
->result
[s
->pos
++] = c
;
6451 term_data(term
, 0, &c
, 1);
6459 if (s
->curr_prompt
< p
->n_prompts
) {
6460 return -1; /* more data required */
6464 return +1; /* all done */