14 #define poslt(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x < (p2).x ) )
15 #define posle(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x <= (p2).x ) )
16 #define poseq(p1,p2) ( (p1).y == (p2).y && (p1).x == (p2).x )
17 #define posdiff(p1,p2) ( ((p1).y - (p2).y) * (term->cols+1) + (p1).x - (p2).x )
19 /* Product-order comparisons for rectangular block selection. */
20 #define posPlt(p1,p2) ( (p1).y <= (p2).y && (p1).x < (p2).x )
21 #define posPle(p1,p2) ( (p1).y <= (p2).y && (p1).x <= (p2).x )
23 #define incpos(p) ( (p).x == term->cols ? ((p).x = 0, (p).y++, 1) : ((p).x++, 0) )
24 #define decpos(p) ( (p).x == 0 ? ((p).x = term->cols, (p).y--, 1) : ((p).x--, 0) )
28 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
29 #define CL_VT100 0x0002 /* VT100 */
30 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
31 #define CL_VT102 0x0008 /* VT102 */
32 #define CL_VT220 0x0010 /* VT220 */
33 #define CL_VT320 0x0020 /* VT320 */
34 #define CL_VT420 0x0040 /* VT420 */
35 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
36 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
37 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
38 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
39 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
41 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
42 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
43 #define TM_VT102 (TM_VT100AVO|CL_VT102)
44 #define TM_VT220 (TM_VT102|CL_VT220)
45 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
46 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
48 #define TM_PUTTY (0xFFFF)
50 #define UPDATE_DELAY ((TICKSPERSEC+49)/50)/* ticks to defer window update */
51 #define TBLINK_DELAY ((TICKSPERSEC*9+19)/20)/* ticks between text blinks*/
52 #define CBLINK_DELAY (CURSORBLINK) /* ticks between cursor blinks */
53 #define VBELL_DELAY (VBELL_TIMEOUT) /* visual bell timeout in ticks */
55 #define compatibility(x) \
56 if ( ((CL_##x)&term->compatibility_level) == 0 ) { \
57 term->termstate=TOPLEVEL; \
60 #define compatibility2(x,y) \
61 if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \
62 term->termstate=TOPLEVEL; \
66 #define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 )
68 const char sco2ansicolour
[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
70 #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
71 const wchar_t sel_nl
[] = SEL_NL
;
74 * Fetch the character at a particular position in a line array,
75 * for purposes of `wordtype'. The reason this isn't just a simple
76 * array reference is that if the character we find is UCSWIDE,
77 * then we must look one space further to the left.
79 #define UCSGET(a, x) \
80 ( (x)>0 && (a)[(x)].chr == UCSWIDE ? (a)[(x)-1].chr : (a)[(x)].chr )
83 * Detect the various aliases of U+0020 SPACE.
85 #define IS_SPACE_CHR(chr) \
86 ((chr) == 0x20 || (DIRECT_CHAR(chr) && ((chr) & 0xFF) == 0x20))
91 #define CSET_OF(chr) (DIRECT_CHAR(chr)||DIRECT_FONT(chr) ? (chr)&CSET_MASK : 0)
94 * Internal prototypes.
96 static void resizeline(Terminal
*, termline
*, int);
97 static termline
*lineptr(Terminal
*, int, int, int);
98 static void unlineptr(termline
*);
99 static void do_paint(Terminal
*, Context
, int);
100 static void erase_lots(Terminal
*, int, int, int);
101 static int find_last_nonempty_line(Terminal
*, tree234
*);
102 static void swap_screen(Terminal
*, int, int, int);
103 static void update_sbar(Terminal
*);
104 static void deselect(Terminal
*);
105 static void term_print_finish(Terminal
*);
106 static void scroll(Terminal
*, int, int, int, int);
107 #ifdef OPTIMISE_SCROLL
108 static void scroll_display(Terminal
*, int, int, int);
109 #endif /* OPTIMISE_SCROLL */
111 static termline
*newline(Terminal
*term
, int cols
, int bce
)
116 line
= snew(termline
);
117 line
->chars
= snewn(cols
, termchar
);
118 for (j
= 0; j
< cols
; j
++)
119 line
->chars
[j
] = (bce ? term
->erase_char
: term
->basic_erase_char
);
120 line
->cols
= line
->size
= cols
;
121 line
->lattr
= LATTR_NORM
;
122 line
->temporary
= FALSE
;
128 static void freeline(termline
*line
)
136 static void unlineptr(termline
*line
)
144 * Diagnostic function: verify that a termline has a correct
145 * combining character structure.
147 * This is a performance-intensive check, so it's no longer enabled
150 static void cc_check(termline
*line
)
152 unsigned char *flags
;
155 assert(line
->size
>= line
->cols
);
157 flags
= snewn(line
->size
, unsigned char);
159 for (i
= 0; i
< line
->size
; i
++)
160 flags
[i
] = (i
< line
->cols
);
162 for (i
= 0; i
< line
->cols
; i
++) {
164 while (line
->chars
[j
].cc_next
) {
165 j
+= line
->chars
[j
].cc_next
;
166 assert(j
>= line
->cols
&& j
< line
->size
);
175 assert(j
>= line
->cols
&& j
< line
->size
);
178 if (line
->chars
[j
].cc_next
)
179 j
+= line
->chars
[j
].cc_next
;
186 for (i
= 0; i
< line
->size
; i
++)
187 j
+= (flags
[i
] != 0);
189 assert(j
== line
->size
);
196 * Add a combining character to a character cell.
198 static void add_cc(termline
*line
, int col
, unsigned long chr
)
202 assert(col
>= 0 && col
< line
->cols
);
205 * Start by extending the cols array if the free list is empty.
207 if (!line
->cc_free
) {
209 line
->size
+= 16 + (line
->size
- line
->cols
) / 2;
210 line
->chars
= sresize(line
->chars
, line
->size
, termchar
);
212 while (n
< line
->size
) {
213 if (n
+1 < line
->size
)
214 line
->chars
[n
].cc_next
= 1;
216 line
->chars
[n
].cc_next
= 0;
222 * Now walk the cc list of the cell in question.
224 while (line
->chars
[col
].cc_next
)
225 col
+= line
->chars
[col
].cc_next
;
228 * `col' now points at the last cc currently in this cell; so
229 * we simply add another one.
231 newcc
= line
->cc_free
;
232 if (line
->chars
[newcc
].cc_next
)
233 line
->cc_free
= newcc
+ line
->chars
[newcc
].cc_next
;
236 line
->chars
[newcc
].cc_next
= 0;
237 line
->chars
[newcc
].chr
= chr
;
238 line
->chars
[col
].cc_next
= newcc
- col
;
246 * Clear the combining character list in a character cell.
248 static void clear_cc(termline
*line
, int col
)
250 int oldfree
, origcol
= col
;
252 assert(col
>= 0 && col
< line
->cols
);
254 if (!line
->chars
[col
].cc_next
)
255 return; /* nothing needs doing */
257 oldfree
= line
->cc_free
;
258 line
->cc_free
= col
+ line
->chars
[col
].cc_next
;
259 while (line
->chars
[col
].cc_next
)
260 col
+= line
->chars
[col
].cc_next
;
262 line
->chars
[col
].cc_next
= oldfree
- col
;
264 line
->chars
[col
].cc_next
= 0;
266 line
->chars
[origcol
].cc_next
= 0;
274 * Compare two character cells for equality. Special case required
275 * in do_paint() where we override what we expect the chr and attr
278 static int termchars_equal_override(termchar
*a
, termchar
*b
,
279 unsigned long bchr
, unsigned long battr
)
284 if ((a
->attr
&~ DATTR_MASK
) != (battr
&~ DATTR_MASK
))
286 while (a
->cc_next
|| b
->cc_next
) {
287 if (!a
->cc_next
|| !b
->cc_next
)
288 return FALSE
; /* one cc-list ends, other does not */
291 if (a
->chr
!= b
->chr
)
297 static int termchars_equal(termchar
*a
, termchar
*b
)
299 return termchars_equal_override(a
, b
, b
->chr
, b
->attr
);
303 * Copy a character cell. (Requires a pointer to the destination
304 * termline, so as to access its free list.)
306 static void copy_termchar(termline
*destline
, int x
, termchar
*src
)
308 clear_cc(destline
, x
);
310 destline
->chars
[x
] = *src
; /* copy everything except cc-list */
311 destline
->chars
[x
].cc_next
= 0; /* and make sure this is zero */
313 while (src
->cc_next
) {
315 add_cc(destline
, x
, src
->chr
);
324 * Move a character cell within its termline.
326 static void move_termchar(termline
*line
, termchar
*dest
, termchar
*src
)
328 /* First clear the cc list from the original char, just in case. */
329 clear_cc(line
, dest
- line
->chars
);
331 /* Move the character cell and adjust its cc_next. */
332 *dest
= *src
; /* copy everything except cc-list */
334 dest
->cc_next
= src
->cc_next
- (dest
-src
);
336 /* Ensure the original cell doesn't have a cc list. */
345 * Compress and decompress a termline into an RLE-based format for
346 * storing in scrollback. (Since scrollback almost never needs to
347 * be modified and exists in huge quantities, this is a sensible
348 * tradeoff, particularly since it allows us to continue adding
349 * features to the main termchar structure without proportionally
350 * bloating the terminal emulator's memory footprint unless those
351 * features are in constant use.)
357 static void add(struct buf
*b
, unsigned char c
)
359 if (b
->len
>= b
->size
) {
360 b
->size
= (b
->len
* 3 / 2) + 512;
361 b
->data
= sresize(b
->data
, b
->size
, unsigned char);
363 b
->data
[b
->len
++] = c
;
365 static int get(struct buf
*b
)
367 return b
->data
[b
->len
++];
369 static void makerle(struct buf
*b
, termline
*ldata
,
370 void (*makeliteral
)(struct buf
*b
, termchar
*c
,
371 unsigned long *state
))
373 int hdrpos
, hdrsize
, n
, prevlen
, prevpos
, thislen
, thispos
, prev2
;
374 termchar
*c
= ldata
->chars
;
375 unsigned long state
= 0, oldstate
;
382 prevlen
= prevpos
= 0;
387 makeliteral(b
, c
++, &state
);
388 thislen
= b
->len
- thispos
;
389 if (thislen
== prevlen
&&
390 !memcmp(b
->data
+ prevpos
, b
->data
+ thispos
, thislen
)) {
392 * This literal precisely matches the previous one.
393 * Turn it into a run if it's worthwhile.
395 * With one-byte literals, it costs us two bytes to
396 * encode a run, plus another byte to write the header
397 * to resume normal output; so a three-element run is
398 * neutral, and anything beyond that is unconditionally
399 * worthwhile. With two-byte literals or more, even a
402 if (thislen
> 1 || prev2
) {
406 * It's worth encoding a run. Start at prevpos,
407 * unless hdrsize==0 in which case we can back up
408 * another one and start by overwriting hdrpos.
411 hdrsize
--; /* remove the literal at prevpos */
415 prevpos
-= prevlen
;/* and possibly another one */
419 assert(prevpos
== hdrpos
+ 1);
421 b
->len
= prevpos
+prevlen
;
423 memmove(b
->data
+ prevpos
+1, b
->data
+ prevpos
, prevlen
);
425 b
->len
= prevpos
+prevlen
+1;
427 * Terminate the previous run of ordinary
430 assert(hdrsize
>= 1 && hdrsize
<= 128);
431 b
->data
[hdrpos
] = hdrsize
- 1;
434 runlen
= prev2 ?
3 : 2;
436 while (n
> 0 && runlen
< 129) {
440 makeliteral(b
, c
, &state
);
441 tmplen
= b
->len
- tmppos
;
443 if (tmplen
!= thislen
||
444 memcmp(b
->data
+ runpos
+1, b
->data
+ tmppos
, tmplen
)) {
446 break; /* run over */
451 assert(runlen
>= 2 && runlen
<= 129);
452 b
->data
[runpos
] = runlen
+ 0x80 - 2;
457 /* And ensure this run doesn't interfere with the next. */
458 prevlen
= prevpos
= 0;
464 * Just flag that the previous two literals were
465 * identical, in case we find a third identical one
466 * we want to turn into a run.
479 * This character isn't (yet) part of a run. Add it to
483 if (hdrsize
== 128) {
484 b
->data
[hdrpos
] = hdrsize
- 1;
488 prevlen
= prevpos
= 0;
497 assert(hdrsize
<= 128);
498 b
->data
[hdrpos
] = hdrsize
- 1;
503 static void makeliteral_chr(struct buf
*b
, termchar
*c
, unsigned long *state
)
506 * My encoding for characters is UTF-8-like, in that it stores
507 * 7-bit ASCII in one byte and uses high-bit-set bytes as
508 * introducers to indicate a longer sequence. However, it's
509 * unlike UTF-8 in that it doesn't need to be able to
510 * resynchronise, and therefore I don't want to waste two bits
511 * per byte on having recognisable continuation characters.
512 * Also I don't want to rule out the possibility that I may one
513 * day use values 0x80000000-0xFFFFFFFF for interesting
514 * purposes, so unlike UTF-8 I need a full 32-bit range.
515 * Accordingly, here is my encoding:
517 * 00000000-0000007F: 0xxxxxxx (but see below)
518 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
519 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
520 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
521 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
523 * (`Z' is like `x' but is always going to be zero since the
524 * values I'm encoding don't go above 2^32. In principle the
525 * five-byte form of the encoding could extend to 2^35, and
526 * there could be six-, seven-, eight- and nine-byte forms as
527 * well to allow up to 64-bit values to be encoded. But that's
528 * completely unnecessary for these purposes!)
530 * The encoding as written above would be very simple, except
531 * that 7-bit ASCII can occur in several different ways in the
532 * terminal data; sometimes it crops up in the D800 page
533 * (CSET_ASCII) but at other times it's in the 0000 page (real
534 * Unicode). Therefore, this encoding is actually _stateful_:
535 * the one-byte encoding of 00-7F actually indicates `reuse the
536 * upper three bytes of the last character', and to encode an
537 * absolute value of 00-7F you need to use the two-byte form
540 if ((c
->chr
& ~0x7F) == *state
) {
541 add(b
, (unsigned char)(c
->chr
& 0x7F));
542 } else if (c
->chr
< 0x4000) {
543 add(b
, (unsigned char)(((c
->chr
>> 8) & 0x3F) | 0x80));
544 add(b
, (unsigned char)(c
->chr
& 0xFF));
545 } else if (c
->chr
< 0x200000) {
546 add(b
, (unsigned char)(((c
->chr
>> 16) & 0x1F) | 0xC0));
547 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
548 add(b
, (unsigned char)(c
->chr
& 0xFF));
549 } else if (c
->chr
< 0x10000000) {
550 add(b
, (unsigned char)(((c
->chr
>> 24) & 0x0F) | 0xE0));
551 add(b
, (unsigned char)((c
->chr
>> 16) & 0xFF));
552 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
553 add(b
, (unsigned char)(c
->chr
& 0xFF));
556 add(b
, (unsigned char)((c
->chr
>> 24) & 0xFF));
557 add(b
, (unsigned char)((c
->chr
>> 16) & 0xFF));
558 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
559 add(b
, (unsigned char)(c
->chr
& 0xFF));
561 *state
= c
->chr
& ~0xFF;
563 static void makeliteral_attr(struct buf
*b
, termchar
*c
, unsigned long *state
)
566 * My encoding for attributes is 16-bit-granular and assumes
567 * that the top bit of the word is never required. I either
568 * store a two-byte value with the top bit clear (indicating
569 * just that value), or a four-byte value with the top bit set
570 * (indicating the same value with its top bit clear).
572 * However, first I permute the bits of the attribute value, so
573 * that the eight bits of colour (four in each of fg and bg)
574 * which are never non-zero unless xterm 256-colour mode is in
575 * use are placed higher up the word than everything else. This
576 * ensures that attribute values remain 16-bit _unless_ the
577 * user uses extended colour.
579 unsigned attr
, colourbits
;
583 assert(ATTR_BGSHIFT
> ATTR_FGSHIFT
);
585 colourbits
= (attr
>> (ATTR_BGSHIFT
+ 4)) & 0xF;
587 colourbits
|= (attr
>> (ATTR_FGSHIFT
+ 4)) & 0xF;
589 attr
= (((attr
>> (ATTR_BGSHIFT
+ 8)) << (ATTR_BGSHIFT
+ 4)) |
590 (attr
& ((1 << (ATTR_BGSHIFT
+ 4))-1)));
591 attr
= (((attr
>> (ATTR_FGSHIFT
+ 8)) << (ATTR_FGSHIFT
+ 4)) |
592 (attr
& ((1 << (ATTR_FGSHIFT
+ 4))-1)));
594 attr
|= (colourbits
<< (32-9));
597 add(b
, (unsigned char)((attr
>> 8) & 0xFF));
598 add(b
, (unsigned char)(attr
& 0xFF));
600 add(b
, (unsigned char)(((attr
>> 24) & 0x7F) | 0x80));
601 add(b
, (unsigned char)((attr
>> 16) & 0xFF));
602 add(b
, (unsigned char)((attr
>> 8) & 0xFF));
603 add(b
, (unsigned char)(attr
& 0xFF));
606 static void makeliteral_cc(struct buf
*b
, termchar
*c
, unsigned long *state
)
609 * For combining characters, I just encode a bunch of ordinary
610 * chars using makeliteral_chr, and terminate with a \0
611 * character (which I know won't come up as a combining char
614 * I don't use the stateful encoding in makeliteral_chr.
616 unsigned long zstate
;
625 makeliteral_chr(b
, c
, &zstate
);
630 makeliteral_chr(b
, &z
, &zstate
);
633 static termline
*decompressline(unsigned char *data
, int *bytes_used
);
635 static unsigned char *compressline(termline
*ldata
)
637 struct buf buffer
= { NULL
, 0, 0 }, *b
= &buffer
;
640 * First, store the column count, 7 bits at a time, least
641 * significant `digit' first, with the high bit set on all but
647 add(b
, (unsigned char)((n
& 0x7F) | 0x80));
650 add(b
, (unsigned char)(n
));
654 * Next store the lattrs; same principle.
657 int n
= ldata
->lattr
;
659 add(b
, (unsigned char)((n
& 0x7F) | 0x80));
662 add(b
, (unsigned char)(n
));
666 * Now we store a sequence of separate run-length encoded
667 * fragments, each containing exactly as many symbols as there
668 * are columns in the ldata.
670 * All of these have a common basic format:
672 * - a byte 00-7F indicates that X+1 literals follow it
673 * - a byte 80-FF indicates that a single literal follows it
674 * and expects to be repeated (X-0x80)+2 times.
676 * The format of the `literals' varies between the fragments.
678 makerle(b
, ldata
, makeliteral_chr
);
679 makerle(b
, ldata
, makeliteral_attr
);
680 makerle(b
, ldata
, makeliteral_cc
);
683 * Diagnostics: ensure that the compressed data really does
684 * decompress to the right thing.
686 * This is a bit performance-heavy for production code.
689 #ifndef CHECK_SB_COMPRESSION
695 #ifdef DIAGNOSTIC_SB_COMPRESSION
696 for (i
= 0; i
< b
->len
; i
++) {
697 printf(" %02x ", b
->data
[i
]);
702 dcl
= decompressline(b
->data
, &dused
);
703 assert(b
->len
== dused
);
704 assert(ldata
->cols
== dcl
->cols
);
705 assert(ldata
->lattr
== dcl
->lattr
);
706 for (i
= 0; i
< ldata
->cols
; i
++)
707 assert(termchars_equal(&ldata
->chars
[i
], &dcl
->chars
[i
]));
709 #ifdef DIAGNOSTIC_SB_COMPRESSION
710 printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",
711 ldata
->cols
, 4 * ldata
->cols
, dused
,
712 (double)dused
/ (4 * ldata
->cols
));
718 #endif /* TERM_CC_DIAGS */
721 * Trim the allocated memory so we don't waste any, and return.
723 return sresize(b
->data
, b
->len
, unsigned char);
726 static void readrle(struct buf
*b
, termline
*ldata
,
727 void (*readliteral
)(struct buf
*b
, termchar
*c
,
728 termline
*ldata
, unsigned long *state
))
731 unsigned long state
= 0;
733 while (n
< ldata
->cols
) {
739 int pos
= b
->len
, count
= hdr
+ 2 - 0x80;
741 assert(n
< ldata
->cols
);
743 readliteral(b
, ldata
->chars
+ n
, ldata
, &state
);
747 /* Just a sequence of consecutive literals. */
751 assert(n
< ldata
->cols
);
752 readliteral(b
, ldata
->chars
+ n
, ldata
, &state
);
758 assert(n
== ldata
->cols
);
760 static void readliteral_chr(struct buf
*b
, termchar
*c
, termline
*ldata
,
761 unsigned long *state
)
766 * 00000000-0000007F: 0xxxxxxx
767 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
768 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
769 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
770 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
775 c
->chr
= byte
| *state
;
776 } else if (byte
< 0xC0) {
777 c
->chr
= (byte
&~ 0xC0) << 8;
779 } else if (byte
< 0xE0) {
780 c
->chr
= (byte
&~ 0xE0) << 16;
781 c
->chr
|= get(b
) << 8;
783 } else if (byte
< 0xF0) {
784 c
->chr
= (byte
&~ 0xF0) << 24;
785 c
->chr
|= get(b
) << 16;
786 c
->chr
|= get(b
) << 8;
789 assert(byte
== 0xF0);
790 c
->chr
= get(b
) << 24;
791 c
->chr
|= get(b
) << 16;
792 c
->chr
|= get(b
) << 8;
795 *state
= c
->chr
& ~0xFF;
797 static void readliteral_attr(struct buf
*b
, termchar
*c
, termline
*ldata
,
798 unsigned long *state
)
800 unsigned val
, attr
, colourbits
;
812 colourbits
= (val
>> (32-9)) & 0xFF;
813 attr
= (val
& ((1<<(32-9))-1));
815 attr
= (((attr
>> (ATTR_FGSHIFT
+ 4)) << (ATTR_FGSHIFT
+ 8)) |
816 (attr
& ((1 << (ATTR_FGSHIFT
+ 4))-1)));
817 attr
= (((attr
>> (ATTR_BGSHIFT
+ 4)) << (ATTR_BGSHIFT
+ 8)) |
818 (attr
& ((1 << (ATTR_BGSHIFT
+ 4))-1)));
820 attr
|= (colourbits
>> 4) << (ATTR_BGSHIFT
+ 4);
821 attr
|= (colourbits
& 0xF) << (ATTR_FGSHIFT
+ 4);
825 static void readliteral_cc(struct buf
*b
, termchar
*c
, termline
*ldata
,
826 unsigned long *state
)
829 unsigned long zstate
;
830 int x
= c
- ldata
->chars
;
836 readliteral_chr(b
, &n
, ldata
, &zstate
);
839 add_cc(ldata
, x
, n
.chr
);
843 static termline
*decompressline(unsigned char *data
, int *bytes_used
)
845 int ncols
, byte
, shift
;
846 struct buf buffer
, *b
= &buffer
;
853 * First read in the column count.
858 ncols
|= (byte
& 0x7F) << shift
;
860 } while (byte
& 0x80);
863 * Now create the output termline.
865 ldata
= snew(termline
);
866 ldata
->chars
= snewn(ncols
, termchar
);
867 ldata
->cols
= ldata
->size
= ncols
;
868 ldata
->temporary
= TRUE
;
872 * We must set all the cc pointers in ldata->chars to 0 right
873 * now, so that cc diagnostics that verify the integrity of the
874 * whole line will make sense while we're in the middle of
879 for (i
= 0; i
< ldata
->cols
; i
++)
880 ldata
->chars
[i
].cc_next
= 0;
884 * Now read in the lattr.
886 ldata
->lattr
= shift
= 0;
889 ldata
->lattr
|= (byte
& 0x7F) << shift
;
891 } while (byte
& 0x80);
894 * Now we read in each of the RLE streams in turn.
896 readrle(b
, ldata
, readliteral_chr
);
897 readrle(b
, ldata
, readliteral_attr
);
898 readrle(b
, ldata
, readliteral_cc
);
900 /* Return the number of bytes read, for diagnostic purposes. */
902 *bytes_used
= b
->len
;
908 * Resize a line to make it `cols' columns wide.
910 static void resizeline(Terminal
*term
, termline
*line
, int cols
)
914 if (line
->cols
!= cols
) {
916 oldcols
= line
->cols
;
919 * This line is the wrong length, which probably means it
920 * hasn't been accessed since a resize. Resize it now.
922 * First, go through all the characters that will be thrown
923 * out in the resize (if we're shrinking the line) and
924 * return their cc lists to the cc free list.
926 for (i
= cols
; i
< oldcols
; i
++)
930 * If we're shrinking the line, we now bodily move the
931 * entire cc section from where it started to where it now
932 * needs to be. (We have to do this before the resize, so
933 * that the data we're copying is still there. However, if
934 * we're expanding, we have to wait until _after_ the
935 * resize so that the space we're copying into is there.)
938 memmove(line
->chars
+ cols
, line
->chars
+ oldcols
,
939 (line
->size
- line
->cols
) * TSIZE
);
942 * Now do the actual resize, leaving the _same_ amount of
943 * cc space as there was to begin with.
945 line
->size
+= cols
- oldcols
;
946 line
->chars
= sresize(line
->chars
, line
->size
, TTYPE
);
950 * If we're expanding the line, _now_ we move the cc
954 memmove(line
->chars
+ cols
, line
->chars
+ oldcols
,
955 (line
->size
- line
->cols
) * TSIZE
);
958 * Go through what's left of the original line, and adjust
959 * the first cc_next pointer in each list. (All the
960 * subsequent ones are still valid because they are
961 * relative offsets within the cc block.) Also do the same
962 * to the head of the cc_free list.
964 for (i
= 0; i
< oldcols
&& i
< cols
; i
++)
965 if (line
->chars
[i
].cc_next
)
966 line
->chars
[i
].cc_next
+= cols
- oldcols
;
968 line
->cc_free
+= cols
- oldcols
;
971 * And finally fill in the new space with erase chars. (We
972 * don't have to worry about cc lists here, because we
973 * _know_ the erase char doesn't have one.)
975 for (i
= oldcols
; i
< cols
; i
++)
976 line
->chars
[i
] = term
->basic_erase_char
;
985 * Get the number of lines in the scrollback.
987 static int sblines(Terminal
*term
)
989 int sblines
= count234(term
->scrollback
);
990 if (term
->cfg
.erase_to_scrollback
&&
991 term
->alt_which
&& term
->alt_screen
) {
992 sblines
+= term
->alt_sblines
;
998 * Retrieve a line of the screen or of the scrollback, according to
999 * whether the y coordinate is non-negative or negative
1002 static termline
*lineptr(Terminal
*term
, int y
, int lineno
, int screen
)
1009 whichtree
= term
->screen
;
1016 if (term
->cfg
.erase_to_scrollback
&&
1017 term
->alt_which
&& term
->alt_screen
) {
1018 altlines
= term
->alt_sblines
;
1020 if (y
< -altlines
) {
1021 whichtree
= term
->scrollback
;
1022 treeindex
= y
+ altlines
+ count234(term
->scrollback
);
1024 whichtree
= term
->alt_screen
;
1025 treeindex
= y
+ term
->alt_sblines
;
1026 /* treeindex = y + count234(term->alt_screen); */
1029 if (whichtree
== term
->scrollback
) {
1030 unsigned char *cline
= index234(whichtree
, treeindex
);
1031 line
= decompressline(cline
, NULL
);
1033 line
= index234(whichtree
, treeindex
);
1036 /* We assume that we don't screw up and retrieve something out of range. */
1038 fatalbox("line==NULL in terminal.c\n"
1039 "lineno=%d y=%d w=%d h=%d\n"
1040 "count(scrollback=%p)=%d\n"
1041 "count(screen=%p)=%d\n"
1042 "count(alt=%p)=%d alt_sblines=%d\n"
1043 "whichtree=%p treeindex=%d\n\n"
1044 "Please contact <putty@projects.tartarus.org> "
1045 "and pass on the above information.",
1046 lineno
, y
, term
->cols
, term
->rows
,
1047 term
->scrollback
, count234(term
->scrollback
),
1048 term
->screen
, count234(term
->screen
),
1049 term
->alt_screen
, count234(term
->alt_screen
), term
->alt_sblines
,
1050 whichtree
, treeindex
);
1052 assert(line
!= NULL
);
1054 resizeline(term
, line
, term
->cols
);
1055 /* FIXME: should we sort the compressed scrollback out here? */
1060 #define lineptr(x) (lineptr)(term,x,__LINE__,FALSE)
1061 #define scrlineptr(x) (lineptr)(term,x,__LINE__,TRUE)
1063 static void term_schedule_tblink(Terminal
*term
);
1064 static void term_schedule_cblink(Terminal
*term
);
1066 static void term_timer(void *ctx
, long now
)
1068 Terminal
*term
= (Terminal
*)ctx
;
1071 if (term
->tblink_pending
&& now
- term
->next_tblink
>= 0) {
1072 term
->tblinker
= !term
->tblinker
;
1073 term
->tblink_pending
= FALSE
;
1074 term_schedule_tblink(term
);
1078 if (term
->cblink_pending
&& now
- term
->next_cblink
>= 0) {
1079 term
->cblinker
= !term
->cblinker
;
1080 term
->cblink_pending
= FALSE
;
1081 term_schedule_cblink(term
);
1085 if (term
->in_vbell
&& now
- term
->vbell_end
>= 0) {
1086 term
->in_vbell
= FALSE
;
1091 (term
->window_update_pending
&& now
- term
->next_update
>= 0))
1095 static void term_schedule_update(Terminal
*term
)
1097 if (!term
->window_update_pending
) {
1098 term
->window_update_pending
= TRUE
;
1099 term
->next_update
= schedule_timer(UPDATE_DELAY
, term_timer
, term
);
1104 * Call this whenever the terminal window state changes, to queue
1107 static void seen_disp_event(Terminal
*term
)
1109 term
->seen_disp_event
= TRUE
; /* for scrollback-reset-on-activity */
1110 term_schedule_update(term
);
1114 * Call when the terminal's blinking-text settings change, or when
1115 * a text blink has just occurred.
1117 static void term_schedule_tblink(Terminal
*term
)
1119 if (term
->blink_is_real
) {
1120 if (!term
->tblink_pending
)
1121 term
->next_tblink
= schedule_timer(TBLINK_DELAY
, term_timer
, term
);
1122 term
->tblink_pending
= TRUE
;
1124 term
->tblinker
= 1; /* reset when not in use */
1125 term
->tblink_pending
= FALSE
;
1130 * Likewise with cursor blinks.
1132 static void term_schedule_cblink(Terminal
*term
)
1134 if (term
->cfg
.blink_cur
&& term
->has_focus
) {
1135 if (!term
->cblink_pending
)
1136 term
->next_cblink
= schedule_timer(CBLINK_DELAY
, term_timer
, term
);
1137 term
->cblink_pending
= TRUE
;
1139 term
->cblinker
= 1; /* reset when not in use */
1140 term
->cblink_pending
= FALSE
;
1145 * Call to reset cursor blinking on new output.
1147 static void term_reset_cblink(Terminal
*term
)
1149 seen_disp_event(term
);
1151 term
->cblink_pending
= FALSE
;
1152 term_schedule_cblink(term
);
1156 * Call to begin a visual bell.
1158 static void term_schedule_vbell(Terminal
*term
, int already_started
,
1161 long ticks_already_gone
;
1163 if (already_started
)
1164 ticks_already_gone
= GETTICKCOUNT() - startpoint
;
1166 ticks_already_gone
= 0;
1168 if (ticks_already_gone
< VBELL_DELAY
) {
1169 term
->in_vbell
= TRUE
;
1170 term
->vbell_end
= schedule_timer(VBELL_DELAY
- ticks_already_gone
,
1173 term
->in_vbell
= FALSE
;
1178 * Set up power-on settings for the terminal.
1179 * If 'clear' is false, don't actually clear the primary screen, and
1180 * position the cursor below the last non-blank line (scrolling if
1183 static void power_on(Terminal
*term
, int clear
)
1185 term
->alt_x
= term
->alt_y
= 0;
1186 term
->savecurs
.x
= term
->savecurs
.y
= 0;
1187 term
->alt_savecurs
.x
= term
->alt_savecurs
.y
= 0;
1188 term
->alt_t
= term
->marg_t
= 0;
1189 if (term
->rows
!= -1)
1190 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
1192 term
->alt_b
= term
->marg_b
= 0;
1193 if (term
->cols
!= -1) {
1195 for (i
= 0; i
< term
->cols
; i
++)
1196 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
1198 term
->alt_om
= term
->dec_om
= term
->cfg
.dec_om
;
1199 term
->alt_ins
= term
->insert
= FALSE
;
1200 term
->alt_wnext
= term
->wrapnext
=
1201 term
->save_wnext
= term
->alt_save_wnext
= FALSE
;
1202 term
->alt_wrap
= term
->wrap
= term
->cfg
.wrap_mode
;
1203 term
->alt_cset
= term
->cset
= term
->save_cset
= term
->alt_save_cset
= 0;
1204 term
->alt_utf
= term
->utf
= term
->save_utf
= term
->alt_save_utf
= 0;
1205 term
->utf_state
= 0;
1206 term
->alt_sco_acs
= term
->sco_acs
=
1207 term
->save_sco_acs
= term
->alt_save_sco_acs
= 0;
1208 term
->cset_attr
[0] = term
->cset_attr
[1] =
1209 term
->save_csattr
= term
->alt_save_csattr
= CSET_ASCII
;
1211 term
->in_vbell
= FALSE
;
1212 term
->cursor_on
= 1;
1213 term
->big_cursor
= 0;
1214 term
->default_attr
= term
->save_attr
=
1215 term
->alt_save_attr
= term
->curr_attr
= ATTR_DEFAULT
;
1216 term
->term_editing
= term
->term_echoing
= FALSE
;
1217 term
->app_cursor_keys
= term
->cfg
.app_cursor
;
1218 term
->app_keypad_keys
= term
->cfg
.app_keypad
;
1219 term
->use_bce
= term
->cfg
.bce
;
1220 term
->blink_is_real
= term
->cfg
.blinktext
;
1221 term
->erase_char
= term
->basic_erase_char
;
1222 term
->alt_which
= 0;
1223 term_print_finish(term
);
1226 for (i
= 0; i
< 256; i
++)
1227 term
->wordness
[i
] = term
->cfg
.wordness
[i
];
1230 swap_screen(term
, 1, FALSE
, FALSE
);
1231 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1232 swap_screen(term
, 0, FALSE
, FALSE
);
1234 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1235 term
->curs
.y
= find_last_nonempty_line(term
, term
->screen
) + 1;
1236 if (term
->curs
.y
== term
->rows
) {
1238 scroll(term
, 0, term
->rows
- 1, 1, TRUE
);
1244 term_schedule_tblink(term
);
1245 term_schedule_cblink(term
);
1249 * Force a screen update.
1251 void term_update(Terminal
*term
)
1255 term
->window_update_pending
= FALSE
;
1257 ctx
= get_ctx(term
->frontend
);
1259 int need_sbar_update
= term
->seen_disp_event
;
1260 if (term
->seen_disp_event
&& term
->cfg
.scroll_on_disp
) {
1261 term
->disptop
= 0; /* return to main screen */
1262 term
->seen_disp_event
= 0;
1263 need_sbar_update
= TRUE
;
1266 if (need_sbar_update
)
1268 do_paint(term
, ctx
, TRUE
);
1269 sys_cursor(term
->frontend
, term
->curs
.x
, term
->curs
.y
- term
->disptop
);
1275 * Called from front end when a keypress occurs, to trigger
1276 * anything magical that needs to happen in that situation.
1278 void term_seen_key_event(Terminal
*term
)
1281 * On any keypress, clear the bell overload mechanism
1282 * completely, on the grounds that large numbers of
1283 * beeps coming from deliberate key action are likely
1284 * to be intended (e.g. beeps from filename completion
1285 * blocking repeatedly).
1287 term
->beep_overloaded
= FALSE
;
1288 while (term
->beephead
) {
1289 struct beeptime
*tmp
= term
->beephead
;
1290 term
->beephead
= tmp
->next
;
1293 term
->beeptail
= NULL
;
1297 * Reset the scrollback on keypress, if we're doing that.
1299 if (term
->cfg
.scroll_on_key
) {
1300 term
->disptop
= 0; /* return to main screen */
1301 seen_disp_event(term
);
1306 * Same as power_on(), but an external function.
1308 void term_pwron(Terminal
*term
, int clear
)
1310 power_on(term
, clear
);
1311 if (term
->ldisc
) /* cause ldisc to notice changes */
1312 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1318 static void set_erase_char(Terminal
*term
)
1320 term
->erase_char
= term
->basic_erase_char
;
1322 term
->erase_char
.attr
= (term
->curr_attr
&
1323 (ATTR_FGMASK
| ATTR_BGMASK
));
1327 * When the user reconfigures us, we need to check the forbidden-
1328 * alternate-screen config option, disable raw mouse mode if the
1329 * user has disabled mouse reporting, and abandon a print job if
1330 * the user has disabled printing.
1332 void term_reconfig(Terminal
*term
, Config
*cfg
)
1335 * Before adopting the new config, check all those terminal
1336 * settings which control power-on defaults; and if they've
1337 * changed, we will modify the current state as well as the
1338 * default one. The full list is: Auto wrap mode, DEC Origin
1339 * Mode, BCE, blinking text, character classes.
1341 int reset_wrap
, reset_decom
, reset_bce
, reset_tblink
, reset_charclass
;
1344 reset_wrap
= (term
->cfg
.wrap_mode
!= cfg
->wrap_mode
);
1345 reset_decom
= (term
->cfg
.dec_om
!= cfg
->dec_om
);
1346 reset_bce
= (term
->cfg
.bce
!= cfg
->bce
);
1347 reset_tblink
= (term
->cfg
.blinktext
!= cfg
->blinktext
);
1348 reset_charclass
= 0;
1349 for (i
= 0; i
< lenof(term
->cfg
.wordness
); i
++)
1350 if (term
->cfg
.wordness
[i
] != cfg
->wordness
[i
])
1351 reset_charclass
= 1;
1354 * If the bidi or shaping settings have changed, flush the bidi
1357 if (term
->cfg
.arabicshaping
!= cfg
->arabicshaping
||
1358 term
->cfg
.bidi
!= cfg
->bidi
) {
1359 for (i
= 0; i
< term
->bidi_cache_size
; i
++) {
1360 sfree(term
->pre_bidi_cache
[i
].chars
);
1361 sfree(term
->post_bidi_cache
[i
].chars
);
1362 term
->pre_bidi_cache
[i
].width
= -1;
1363 term
->pre_bidi_cache
[i
].chars
= NULL
;
1364 term
->post_bidi_cache
[i
].width
= -1;
1365 term
->post_bidi_cache
[i
].chars
= NULL
;
1369 term
->cfg
= *cfg
; /* STRUCTURE COPY */
1372 term
->alt_wrap
= term
->wrap
= term
->cfg
.wrap_mode
;
1374 term
->alt_om
= term
->dec_om
= term
->cfg
.dec_om
;
1376 term
->use_bce
= term
->cfg
.bce
;
1377 set_erase_char(term
);
1380 term
->blink_is_real
= term
->cfg
.blinktext
;
1382 if (reset_charclass
)
1383 for (i
= 0; i
< 256; i
++)
1384 term
->wordness
[i
] = term
->cfg
.wordness
[i
];
1386 if (term
->cfg
.no_alt_screen
)
1387 swap_screen(term
, 0, FALSE
, FALSE
);
1388 if (term
->cfg
.no_mouse_rep
) {
1389 term
->xterm_mouse
= 0;
1390 set_raw_mouse_mode(term
->frontend
, 0);
1392 if (term
->cfg
.no_remote_charset
) {
1393 term
->cset_attr
[0] = term
->cset_attr
[1] = CSET_ASCII
;
1394 term
->sco_acs
= term
->alt_sco_acs
= 0;
1397 if (!*term
->cfg
.printer
) {
1398 term_print_finish(term
);
1400 term_schedule_tblink(term
);
1401 term_schedule_cblink(term
);
1405 * Clear the scrollback.
1407 void term_clrsb(Terminal
*term
)
1409 unsigned char *line
;
1411 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
) {
1412 sfree(line
); /* this is compressed data, not a termline */
1414 term
->tempsblines
= 0;
1415 term
->alt_sblines
= 0;
1420 * Initialise the terminal.
1422 Terminal
*term_init(Config
*mycfg
, struct unicode_data
*ucsdata
,
1428 * Allocate a new Terminal structure and initialise the fields
1431 term
= snew(Terminal
);
1432 term
->frontend
= frontend
;
1433 term
->ucsdata
= ucsdata
;
1434 term
->cfg
= *mycfg
; /* STRUCTURE COPY */
1435 term
->logctx
= NULL
;
1436 term
->compatibility_level
= TM_PUTTY
;
1437 strcpy(term
->id_string
, "\033[?6c");
1438 term
->cblink_pending
= term
->tblink_pending
= FALSE
;
1439 term
->paste_buffer
= NULL
;
1440 term
->paste_len
= 0;
1441 term
->last_paste
= 0;
1442 bufchain_init(&term
->inbuf
);
1443 bufchain_init(&term
->printer_buf
);
1444 term
->printing
= term
->only_printing
= FALSE
;
1445 term
->print_job
= NULL
;
1446 term
->vt52_mode
= FALSE
;
1447 term
->cr_lf_return
= FALSE
;
1448 term
->seen_disp_event
= FALSE
;
1449 term
->xterm_mouse
= term
->mouse_is_down
= FALSE
;
1450 term
->reset_132
= FALSE
;
1451 term
->cblinker
= term
->tblinker
= 0;
1452 term
->has_focus
= 1;
1453 term
->repeat_off
= FALSE
;
1454 term
->termstate
= TOPLEVEL
;
1455 term
->selstate
= NO_SELECTION
;
1458 term
->screen
= term
->alt_screen
= term
->scrollback
= NULL
;
1459 term
->tempsblines
= 0;
1460 term
->alt_sblines
= 0;
1462 term
->disptext
= NULL
;
1463 term
->dispcursx
= term
->dispcursy
= -1;
1466 term
->rows
= term
->cols
= -1;
1467 power_on(term
, TRUE
);
1468 term
->beephead
= term
->beeptail
= NULL
;
1469 #ifdef OPTIMISE_SCROLL
1470 term
->scrollhead
= term
->scrolltail
= NULL
;
1471 #endif /* OPTIMISE_SCROLL */
1473 term
->lastbeep
= FALSE
;
1474 term
->beep_overloaded
= FALSE
;
1475 term
->attr_mask
= 0xffffffff;
1476 term
->resize_fn
= NULL
;
1477 term
->resize_ctx
= NULL
;
1478 term
->in_term_out
= FALSE
;
1480 term
->ltemp_size
= 0;
1481 term
->wcFrom
= NULL
;
1483 term
->wcFromTo_size
= 0;
1485 term
->window_update_pending
= FALSE
;
1487 term
->bidi_cache_size
= 0;
1488 term
->pre_bidi_cache
= term
->post_bidi_cache
= NULL
;
1491 term
->basic_erase_char
.chr
= CSET_ASCII
| ' ';
1492 term
->basic_erase_char
.attr
= ATTR_DEFAULT
;
1493 term
->basic_erase_char
.cc_next
= 0;
1494 term
->erase_char
= term
->basic_erase_char
;
1499 void term_free(Terminal
*term
)
1502 struct beeptime
*beep
;
1505 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
)
1506 sfree(line
); /* compressed data, not a termline */
1507 freetree234(term
->scrollback
);
1508 while ((line
= delpos234(term
->screen
, 0)) != NULL
)
1510 freetree234(term
->screen
);
1511 while ((line
= delpos234(term
->alt_screen
, 0)) != NULL
)
1513 freetree234(term
->alt_screen
);
1514 if (term
->disptext
) {
1515 for (i
= 0; i
< term
->rows
; i
++)
1516 freeline(term
->disptext
[i
]);
1518 sfree(term
->disptext
);
1519 while (term
->beephead
) {
1520 beep
= term
->beephead
;
1521 term
->beephead
= beep
->next
;
1524 bufchain_clear(&term
->inbuf
);
1526 printer_finish_job(term
->print_job
);
1527 bufchain_clear(&term
->printer_buf
);
1528 sfree(term
->paste_buffer
);
1530 sfree(term
->wcFrom
);
1533 for (i
= 0; i
< term
->bidi_cache_size
; i
++) {
1534 sfree(term
->pre_bidi_cache
[i
].chars
);
1535 sfree(term
->post_bidi_cache
[i
].chars
);
1537 sfree(term
->pre_bidi_cache
);
1538 sfree(term
->post_bidi_cache
);
1540 expire_timer_context(term
);
1546 * Set up the terminal for a given size.
1548 void term_size(Terminal
*term
, int newrows
, int newcols
, int newsavelines
)
1551 termline
**newdisp
, *line
;
1552 int i
, j
, oldrows
= term
->rows
;
1554 int save_alt_which
= term
->alt_which
;
1556 if (newrows
== term
->rows
&& newcols
== term
->cols
&&
1557 newsavelines
== term
->savelines
)
1558 return; /* nothing to do */
1560 /* Behave sensibly if we're given zero (or negative) rows/cols */
1562 if (newrows
< 1) newrows
= 1;
1563 if (newcols
< 1) newcols
= 1;
1566 swap_screen(term
, 0, FALSE
, FALSE
);
1568 term
->alt_t
= term
->marg_t
= 0;
1569 term
->alt_b
= term
->marg_b
= newrows
- 1;
1571 if (term
->rows
== -1) {
1572 term
->scrollback
= newtree234(NULL
);
1573 term
->screen
= newtree234(NULL
);
1574 term
->tempsblines
= 0;
1579 * Resize the screen and scrollback. We only need to shift
1580 * lines around within our data structures, because lineptr()
1581 * will take care of resizing each individual line if
1584 * - If the new screen is longer, we shunt lines in from temporary
1585 * scrollback if possible, otherwise we add new blank lines at
1588 * - If the new screen is shorter, we remove any blank lines at
1589 * the bottom if possible, otherwise shunt lines above the cursor
1590 * to scrollback if possible, otherwise delete lines below the
1593 * - Then, if the new scrollback length is less than the
1594 * amount of scrollback we actually have, we must throw some
1597 sblen
= count234(term
->scrollback
);
1598 /* Do this loop to expand the screen if newrows > rows */
1599 assert(term
->rows
== count234(term
->screen
));
1600 while (term
->rows
< newrows
) {
1601 if (term
->tempsblines
> 0) {
1602 unsigned char *cline
;
1603 /* Insert a line from the scrollback at the top of the screen. */
1604 assert(sblen
>= term
->tempsblines
);
1605 cline
= delpos234(term
->scrollback
, --sblen
);
1606 line
= decompressline(cline
, NULL
);
1608 line
->temporary
= FALSE
; /* reconstituted line is now real */
1609 term
->tempsblines
-= 1;
1610 addpos234(term
->screen
, line
, 0);
1612 term
->savecurs
.y
+= 1;
1614 /* Add a new blank line at the bottom of the screen. */
1615 line
= newline(term
, newcols
, FALSE
);
1616 addpos234(term
->screen
, line
, count234(term
->screen
));
1620 /* Do this loop to shrink the screen if newrows < rows */
1621 while (term
->rows
> newrows
) {
1622 if (term
->curs
.y
< term
->rows
- 1) {
1623 /* delete bottom row, unless it contains the cursor */
1624 sfree(delpos234(term
->screen
, term
->rows
- 1));
1626 /* push top row to scrollback */
1627 line
= delpos234(term
->screen
, 0);
1628 addpos234(term
->scrollback
, compressline(line
), sblen
++);
1630 term
->tempsblines
+= 1;
1632 term
->savecurs
.y
-= 1;
1636 assert(term
->rows
== newrows
);
1637 assert(count234(term
->screen
) == newrows
);
1639 /* Delete any excess lines from the scrollback. */
1640 while (sblen
> newsavelines
) {
1641 line
= delpos234(term
->scrollback
, 0);
1645 if (sblen
< term
->tempsblines
)
1646 term
->tempsblines
= sblen
;
1647 assert(count234(term
->scrollback
) <= newsavelines
);
1648 assert(count234(term
->scrollback
) >= term
->tempsblines
);
1651 /* Make a new displayed text buffer. */
1652 newdisp
= snewn(newrows
, termline
*);
1653 for (i
= 0; i
< newrows
; i
++) {
1654 newdisp
[i
] = newline(term
, newcols
, FALSE
);
1655 for (j
= 0; j
< newcols
; j
++)
1656 newdisp
[i
]->chars
[j
].attr
= ATTR_INVALID
;
1658 if (term
->disptext
) {
1659 for (i
= 0; i
< oldrows
; i
++)
1660 freeline(term
->disptext
[i
]);
1662 sfree(term
->disptext
);
1663 term
->disptext
= newdisp
;
1664 term
->dispcursx
= term
->dispcursy
= -1;
1666 /* Make a new alternate screen. */
1667 newalt
= newtree234(NULL
);
1668 for (i
= 0; i
< newrows
; i
++) {
1669 line
= newline(term
, newcols
, TRUE
);
1670 addpos234(newalt
, line
, i
);
1672 if (term
->alt_screen
) {
1673 while (NULL
!= (line
= delpos234(term
->alt_screen
, 0)))
1675 freetree234(term
->alt_screen
);
1677 term
->alt_screen
= newalt
;
1678 term
->alt_sblines
= 0;
1680 term
->tabs
= sresize(term
->tabs
, newcols
, unsigned char);
1683 for (i
= (term
->cols
> 0 ? term
->cols
: 0); i
< newcols
; i
++)
1684 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
1687 /* Check that the cursor positions are still valid. */
1688 if (term
->savecurs
.y
< 0)
1689 term
->savecurs
.y
= 0;
1690 if (term
->savecurs
.y
>= newrows
)
1691 term
->savecurs
.y
= newrows
- 1;
1692 if (term
->curs
.y
< 0)
1694 if (term
->curs
.y
>= newrows
)
1695 term
->curs
.y
= newrows
- 1;
1696 if (term
->curs
.x
>= newcols
)
1697 term
->curs
.x
= newcols
- 1;
1698 term
->alt_x
= term
->alt_y
= 0;
1699 term
->wrapnext
= term
->alt_wnext
= FALSE
;
1701 term
->rows
= newrows
;
1702 term
->cols
= newcols
;
1703 term
->savelines
= newsavelines
;
1705 swap_screen(term
, save_alt_which
, FALSE
, FALSE
);
1709 if (term
->resize_fn
)
1710 term
->resize_fn(term
->resize_ctx
, term
->cols
, term
->rows
);
1714 * Hand a function and context pointer to the terminal which it can
1715 * use to notify a back end of resizes.
1717 void term_provide_resize_fn(Terminal
*term
,
1718 void (*resize_fn
)(void *, int, int),
1721 term
->resize_fn
= resize_fn
;
1722 term
->resize_ctx
= resize_ctx
;
1723 if (term
->cols
> 0 && term
->rows
> 0)
1724 resize_fn(resize_ctx
, term
->cols
, term
->rows
);
1727 /* Find the bottom line on the screen that has any content.
1728 * If only the top line has content, returns 0.
1729 * If no lines have content, return -1.
1731 static int find_last_nonempty_line(Terminal
* term
, tree234
* screen
)
1734 for (i
= count234(screen
) - 1; i
>= 0; i
--) {
1735 termline
*line
= index234(screen
, i
);
1737 for (j
= 0; j
< line
->cols
; j
++)
1738 if (!termchars_equal(&line
->chars
[j
], &term
->erase_char
))
1740 if (j
!= line
->cols
) break;
1746 * Swap screens. If `reset' is TRUE and we have been asked to
1747 * switch to the alternate screen, we must bring most of its
1748 * configuration from the main screen and erase the contents of the
1749 * alternate screen completely. (This is even true if we're already
1750 * on it! Blame xterm.)
1752 static void swap_screen(Terminal
*term
, int which
, int reset
, int keep_cur_pos
)
1759 reset
= FALSE
; /* do no weird resetting if which==0 */
1761 if (which
!= term
->alt_which
) {
1762 term
->alt_which
= which
;
1764 ttr
= term
->alt_screen
;
1765 term
->alt_screen
= term
->screen
;
1767 term
->alt_sblines
= find_last_nonempty_line(term
, term
->alt_screen
) + 1;
1769 if (!reset
&& !keep_cur_pos
)
1770 term
->curs
.x
= term
->alt_x
;
1773 if (!reset
&& !keep_cur_pos
)
1774 term
->curs
.y
= term
->alt_y
;
1777 if (!reset
) term
->marg_t
= term
->alt_t
;
1780 if (!reset
) term
->marg_b
= term
->alt_b
;
1783 if (!reset
) term
->dec_om
= term
->alt_om
;
1786 if (!reset
) term
->wrap
= term
->alt_wrap
;
1789 if (!reset
) term
->wrapnext
= term
->alt_wnext
;
1790 term
->alt_wnext
= t
;
1792 if (!reset
) term
->insert
= term
->alt_ins
;
1795 if (!reset
) term
->cset
= term
->alt_cset
;
1798 if (!reset
) term
->utf
= term
->alt_utf
;
1801 if (!reset
) term
->sco_acs
= term
->alt_sco_acs
;
1802 term
->alt_sco_acs
= t
;
1804 tp
= term
->savecurs
;
1805 if (!reset
&& !keep_cur_pos
)
1806 term
->savecurs
= term
->alt_savecurs
;
1807 term
->alt_savecurs
= tp
;
1808 t
= term
->save_cset
;
1809 if (!reset
&& !keep_cur_pos
)
1810 term
->save_cset
= term
->alt_save_cset
;
1811 term
->alt_save_cset
= t
;
1812 t
= term
->save_csattr
;
1813 if (!reset
&& !keep_cur_pos
)
1814 term
->save_csattr
= term
->alt_save_csattr
;
1815 term
->alt_save_csattr
= t
;
1816 t
= term
->save_attr
;
1817 if (!reset
&& !keep_cur_pos
)
1818 term
->save_attr
= term
->alt_save_attr
;
1819 term
->alt_save_attr
= t
;
1821 if (!reset
&& !keep_cur_pos
)
1822 term
->save_utf
= term
->alt_save_utf
;
1823 term
->alt_save_utf
= t
;
1824 t
= term
->save_wnext
;
1825 if (!reset
&& !keep_cur_pos
)
1826 term
->save_wnext
= term
->alt_save_wnext
;
1827 term
->alt_save_wnext
= t
;
1828 t
= term
->save_sco_acs
;
1829 if (!reset
&& !keep_cur_pos
)
1830 term
->save_sco_acs
= term
->alt_save_sco_acs
;
1831 term
->alt_save_sco_acs
= t
;
1834 if (reset
&& term
->screen
) {
1836 * Yes, this _is_ supposed to honour background-colour-erase.
1838 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1843 * Update the scroll bar.
1845 static void update_sbar(Terminal
*term
)
1847 int nscroll
= sblines(term
);
1848 set_sbar(term
->frontend
, nscroll
+ term
->rows
,
1849 nscroll
+ term
->disptop
, term
->rows
);
1853 * Check whether the region bounded by the two pointers intersects
1854 * the scroll region, and de-select the on-screen selection if so.
1856 static void check_selection(Terminal
*term
, pos from
, pos to
)
1858 if (poslt(from
, term
->selend
) && poslt(term
->selstart
, to
))
1863 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1864 * for backward.) `sb' is TRUE if the scrolling is permitted to
1865 * affect the scrollback buffer.
1867 static void scroll(Terminal
*term
, int topline
, int botline
, int lines
, int sb
)
1870 int i
, seltop
, olddisptop
, shift
;
1872 if (topline
!= 0 || term
->alt_which
!= 0)
1875 olddisptop
= term
->disptop
;
1879 line
= delpos234(term
->screen
, botline
);
1880 resizeline(term
, line
, term
->cols
);
1881 for (i
= 0; i
< term
->cols
; i
++)
1882 copy_termchar(line
, i
, &term
->erase_char
);
1883 line
->lattr
= LATTR_NORM
;
1884 addpos234(term
->screen
, line
, topline
);
1886 if (term
->selstart
.y
>= topline
&& term
->selstart
.y
<= botline
) {
1888 if (term
->selstart
.y
> botline
) {
1889 term
->selstart
.y
= botline
+ 1;
1890 term
->selstart
.x
= 0;
1893 if (term
->selend
.y
>= topline
&& term
->selend
.y
<= botline
) {
1895 if (term
->selend
.y
> botline
) {
1896 term
->selend
.y
= botline
+ 1;
1905 line
= delpos234(term
->screen
, topline
);
1906 #ifdef TERM_CC_DIAGS
1909 if (sb
&& term
->savelines
> 0) {
1910 int sblen
= count234(term
->scrollback
);
1912 * We must add this line to the scrollback. We'll
1913 * remove a line from the top of the scrollback if
1914 * the scrollback is full.
1916 if (sblen
== term
->savelines
) {
1917 unsigned char *cline
;
1920 cline
= delpos234(term
->scrollback
, 0);
1923 term
->tempsblines
+= 1;
1925 addpos234(term
->scrollback
, compressline(line
), sblen
);
1927 /* now `line' itself can be reused as the bottom line */
1930 * If the user is currently looking at part of the
1931 * scrollback, and they haven't enabled any options
1932 * that are going to reset the scrollback as a
1933 * result of this movement, then the chances are
1934 * they'd like to keep looking at the same line. So
1935 * we move their viewpoint at the same rate as the
1936 * scroll, at least until their viewpoint hits the
1937 * top end of the scrollback buffer, at which point
1938 * we don't have the choice any more.
1940 * Thanks to Jan Holmen Holsten for the idea and
1941 * initial implementation.
1943 if (term
->disptop
> -term
->savelines
&& term
->disptop
< 0)
1946 resizeline(term
, line
, term
->cols
);
1947 for (i
= 0; i
< term
->cols
; i
++)
1948 copy_termchar(line
, i
, &term
->erase_char
);
1949 line
->lattr
= LATTR_NORM
;
1950 addpos234(term
->screen
, line
, botline
);
1953 * If the selection endpoints move into the scrollback,
1954 * we keep them moving until they hit the top. However,
1955 * of course, if the line _hasn't_ moved into the
1956 * scrollback then we don't do this, and cut them off
1957 * at the top of the scroll region.
1959 * This applies to selstart and selend (for an existing
1960 * selection), and also selanchor (for one being
1961 * selected as we speak).
1963 seltop
= sb ?
-term
->savelines
: topline
;
1965 if (term
->selstate
!= NO_SELECTION
) {
1966 if (term
->selstart
.y
>= seltop
&&
1967 term
->selstart
.y
<= botline
) {
1969 if (term
->selstart
.y
< seltop
) {
1970 term
->selstart
.y
= seltop
;
1971 term
->selstart
.x
= 0;
1974 if (term
->selend
.y
>= seltop
&& term
->selend
.y
<= botline
) {
1976 if (term
->selend
.y
< seltop
) {
1977 term
->selend
.y
= seltop
;
1981 if (term
->selanchor
.y
>= seltop
&&
1982 term
->selanchor
.y
<= botline
) {
1983 term
->selanchor
.y
--;
1984 if (term
->selanchor
.y
< seltop
) {
1985 term
->selanchor
.y
= seltop
;
1986 term
->selanchor
.x
= 0;
1994 #ifdef OPTIMISE_SCROLL
1995 shift
+= term
->disptop
- olddisptop
;
1996 if (shift
< term
->rows
&& shift
> -term
->rows
&& shift
!= 0)
1997 scroll_display(term
, topline
, botline
, shift
);
1998 #endif /* OPTIMISE_SCROLL */
2001 #ifdef OPTIMISE_SCROLL
2003 * Add a scroll of a region on the screen into the pending scroll list.
2004 * `lines' is +ve for scrolling forward, -ve for backward.
2006 * If the scroll is on the same area as the last scroll in the list,
2009 static void save_scroll(Terminal
*term
, int topline
, int botline
, int lines
)
2011 struct scrollregion
*newscroll
;
2012 if (term
->scrolltail
&&
2013 term
->scrolltail
->topline
== topline
&&
2014 term
->scrolltail
->botline
== botline
) {
2015 term
->scrolltail
->lines
+= lines
;
2017 newscroll
= snew(struct scrollregion
);
2018 newscroll
->topline
= topline
;
2019 newscroll
->botline
= botline
;
2020 newscroll
->lines
= lines
;
2021 newscroll
->next
= NULL
;
2023 if (!term
->scrollhead
)
2024 term
->scrollhead
= newscroll
;
2026 term
->scrolltail
->next
= newscroll
;
2027 term
->scrolltail
= newscroll
;
2032 * Scroll the physical display, and our conception of it in disptext.
2034 static void scroll_display(Terminal
*term
, int topline
, int botline
, int lines
)
2036 int distance
, nlines
, i
, j
;
2038 distance
= lines
> 0 ? lines
: -lines
;
2039 nlines
= botline
- topline
+ 1 - distance
;
2041 for (i
= 0; i
< nlines
; i
++)
2042 for (j
= 0; j
< term
->cols
; j
++)
2043 copy_termchar(term
->disptext
[i
], j
,
2044 term
->disptext
[i
+distance
]->chars
+j
);
2045 if (term
->dispcursy
>= 0 &&
2046 term
->dispcursy
>= topline
+ distance
&&
2047 term
->dispcursy
< topline
+ distance
+ nlines
)
2048 term
->dispcursy
-= distance
;
2049 for (i
= 0; i
< distance
; i
++)
2050 for (j
= 0; j
< term
->cols
; j
++)
2051 term
->disptext
[nlines
+i
]->chars
[j
].attr
|= ATTR_INVALID
;
2053 for (i
= nlines
; i
-- ;)
2054 for (j
= 0; j
< term
->cols
; j
++)
2055 copy_termchar(term
->disptext
[i
+distance
], j
,
2056 term
->disptext
[i
]->chars
+j
);
2057 if (term
->dispcursy
>= 0 &&
2058 term
->dispcursy
>= topline
&&
2059 term
->dispcursy
< topline
+ nlines
)
2060 term
->dispcursy
+= distance
;
2061 for (i
= 0; i
< distance
; i
++)
2062 for (j
= 0; j
< term
->cols
; j
++)
2063 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
2065 save_scroll(term
, topline
, botline
, lines
);
2067 #endif /* OPTIMISE_SCROLL */
2070 * Move the cursor to a given position, clipping at boundaries. We
2071 * may or may not want to clip at the scroll margin: marg_clip is 0
2072 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
2073 * even _being_ outside the margins.
2075 static void move(Terminal
*term
, int x
, int y
, int marg_clip
)
2079 if (x
>= term
->cols
)
2082 if ((term
->curs
.y
>= term
->marg_t
|| marg_clip
== 2) &&
2085 if ((term
->curs
.y
<= term
->marg_b
|| marg_clip
== 2) &&
2091 if (y
>= term
->rows
)
2095 term
->wrapnext
= FALSE
;
2099 * Save or restore the cursor and SGR mode.
2101 static void save_cursor(Terminal
*term
, int save
)
2104 term
->savecurs
= term
->curs
;
2105 term
->save_attr
= term
->curr_attr
;
2106 term
->save_cset
= term
->cset
;
2107 term
->save_utf
= term
->utf
;
2108 term
->save_wnext
= term
->wrapnext
;
2109 term
->save_csattr
= term
->cset_attr
[term
->cset
];
2110 term
->save_sco_acs
= term
->sco_acs
;
2112 term
->curs
= term
->savecurs
;
2113 /* Make sure the window hasn't shrunk since the save */
2114 if (term
->curs
.x
>= term
->cols
)
2115 term
->curs
.x
= term
->cols
- 1;
2116 if (term
->curs
.y
>= term
->rows
)
2117 term
->curs
.y
= term
->rows
- 1;
2119 term
->curr_attr
= term
->save_attr
;
2120 term
->cset
= term
->save_cset
;
2121 term
->utf
= term
->save_utf
;
2122 term
->wrapnext
= term
->save_wnext
;
2124 * wrapnext might reset to False if the x position is no
2125 * longer at the rightmost edge.
2127 if (term
->wrapnext
&& term
->curs
.x
< term
->cols
-1)
2128 term
->wrapnext
= FALSE
;
2129 term
->cset_attr
[term
->cset
] = term
->save_csattr
;
2130 term
->sco_acs
= term
->save_sco_acs
;
2131 set_erase_char(term
);
2136 * This function is called before doing _anything_ which affects
2137 * only part of a line of text. It is used to mark the boundary
2138 * between two character positions, and it indicates that some sort
2139 * of effect is going to happen on only one side of that boundary.
2141 * The effect of this function is to check whether a CJK
2142 * double-width character is straddling the boundary, and to remove
2143 * it and replace it with two spaces if so. (Of course, one or
2144 * other of those spaces is then likely to be replaced with
2145 * something else again, as a result of whatever happens next.)
2147 * Also, if the boundary is at the right-hand _edge_ of the screen,
2148 * it implies something deliberate is being done to the rightmost
2149 * column position; hence we must clear LATTR_WRAPPED2.
2151 * The input to the function is the coordinates of the _second_
2152 * character of the pair.
2154 static void check_boundary(Terminal
*term
, int x
, int y
)
2158 /* Validate input coordinates, just in case. */
2159 if (x
== 0 || x
> term
->cols
)
2162 ldata
= scrlineptr(y
);
2163 if (x
== term
->cols
) {
2164 ldata
->lattr
&= ~LATTR_WRAPPED2
;
2166 if (ldata
->chars
[x
].chr
== UCSWIDE
) {
2167 clear_cc(ldata
, x
-1);
2169 ldata
->chars
[x
-1].chr
= ' ' | CSET_ASCII
;
2170 ldata
->chars
[x
] = ldata
->chars
[x
-1];
2176 * Erase a large portion of the screen: the whole screen, or the
2177 * whole line, or parts thereof.
2179 static void erase_lots(Terminal
*term
,
2180 int line_only
, int from_begin
, int to_end
)
2184 int erasing_lines_from_top
= 0;
2187 start
.y
= term
->curs
.y
;
2189 end
.y
= term
->curs
.y
+ 1;
2191 erase_lattr
= FALSE
;
2206 if (!from_begin
|| !to_end
)
2207 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2208 check_selection(term
, start
, end
);
2210 /* Clear screen also forces a full window redraw, just in case. */
2211 if (start
.y
== 0 && start
.x
== 0 && end
.y
== term
->rows
)
2212 term_invalidate(term
);
2214 /* Lines scrolled away shouldn't be brought back on if the terminal
2216 if (start
.y
== 0 && start
.x
== 0 && end
.x
== 0 && erase_lattr
)
2217 erasing_lines_from_top
= 1;
2219 if (term
->cfg
.erase_to_scrollback
&& erasing_lines_from_top
) {
2220 /* If it's a whole number of lines, starting at the top, and
2221 * we're fully erasing them, erase by scrolling and keep the
2222 * lines in the scrollback. */
2223 int scrolllines
= end
.y
;
2224 if (end
.y
== term
->rows
) {
2225 /* Shrink until we find a non-empty row.*/
2226 scrolllines
= find_last_nonempty_line(term
, term
->screen
) + 1;
2228 if (scrolllines
> 0)
2229 scroll(term
, 0, scrolllines
- 1, scrolllines
, TRUE
);
2231 termline
*ldata
= scrlineptr(start
.y
);
2232 while (poslt(start
, end
)) {
2233 if (start
.x
== term
->cols
) {
2235 ldata
->lattr
&= ~(LATTR_WRAPPED
| LATTR_WRAPPED2
);
2237 ldata
->lattr
= LATTR_NORM
;
2239 copy_termchar(ldata
, start
.x
, &term
->erase_char
);
2241 if (incpos(start
) && start
.y
< term
->rows
) {
2242 ldata
= scrlineptr(start
.y
);
2247 /* After an erase of lines from the top of the screen, we shouldn't
2248 * bring the lines back again if the terminal enlarges (since the user or
2249 * application has explictly thrown them away). */
2250 if (erasing_lines_from_top
&& !(term
->alt_which
))
2251 term
->tempsblines
= 0;
2255 * Insert or delete characters within the current line. n is +ve if
2256 * insertion is desired, and -ve for deletion.
2258 static void insch(Terminal
*term
, int n
)
2260 int dir
= (n
< 0 ?
-1 : +1);
2265 n
= (n
< 0 ?
-n
: n
);
2266 if (n
> term
->cols
- term
->curs
.x
)
2267 n
= term
->cols
- term
->curs
.x
;
2268 m
= term
->cols
- term
->curs
.x
- n
;
2269 cursplus
.y
= term
->curs
.y
;
2270 cursplus
.x
= term
->curs
.x
+ n
;
2271 check_selection(term
, term
->curs
, cursplus
);
2272 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2274 check_boundary(term
, term
->curs
.x
+ n
, term
->curs
.y
);
2275 ldata
= scrlineptr(term
->curs
.y
);
2277 for (j
= 0; j
< m
; j
++)
2278 move_termchar(ldata
,
2279 ldata
->chars
+ term
->curs
.x
+ j
,
2280 ldata
->chars
+ term
->curs
.x
+ j
+ n
);
2282 copy_termchar(ldata
, term
->curs
.x
+ m
++, &term
->erase_char
);
2285 move_termchar(ldata
,
2286 ldata
->chars
+ term
->curs
.x
+ j
+ n
,
2287 ldata
->chars
+ term
->curs
.x
+ j
);
2289 copy_termchar(ldata
, term
->curs
.x
+ n
, &term
->erase_char
);
2294 * Toggle terminal mode `mode' to state `state'. (`query' indicates
2295 * whether the mode is a DEC private one or a normal one.)
2297 static void toggle_mode(Terminal
*term
, int mode
, int query
, int state
)
2301 case 1: /* DECCKM: application cursor keys */
2302 term
->app_cursor_keys
= state
;
2304 case 2: /* DECANM: VT52 mode */
2305 term
->vt52_mode
= !state
;
2306 if (term
->vt52_mode
) {
2307 term
->blink_is_real
= FALSE
;
2308 term
->vt52_bold
= FALSE
;
2310 term
->blink_is_real
= term
->cfg
.blinktext
;
2312 term_schedule_tblink(term
);
2314 case 3: /* DECCOLM: 80/132 columns */
2316 if (!term
->cfg
.no_remote_resize
)
2317 request_resize(term
->frontend
, state ?
132 : 80, term
->rows
);
2318 term
->reset_132
= state
;
2319 term
->alt_t
= term
->marg_t
= 0;
2320 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
2321 move(term
, 0, 0, 0);
2322 erase_lots(term
, FALSE
, TRUE
, TRUE
);
2324 case 5: /* DECSCNM: reverse video */
2326 * Toggle reverse video. If we receive an OFF within the
2327 * visual bell timeout period after an ON, we trigger an
2328 * effective visual bell, so that ESC[?5hESC[?5l will
2329 * always be an actually _visible_ visual bell.
2331 if (term
->rvideo
&& !state
) {
2332 /* This is an OFF, so set up a vbell */
2333 term_schedule_vbell(term
, TRUE
, term
->rvbell_startpoint
);
2334 } else if (!term
->rvideo
&& state
) {
2335 /* This is an ON, so we notice the time and save it. */
2336 term
->rvbell_startpoint
= GETTICKCOUNT();
2338 term
->rvideo
= state
;
2339 seen_disp_event(term
);
2341 case 6: /* DECOM: DEC origin mode */
2342 term
->dec_om
= state
;
2344 case 7: /* DECAWM: auto wrap */
2347 case 8: /* DECARM: auto key repeat */
2348 term
->repeat_off
= !state
;
2350 case 10: /* DECEDM: set local edit mode */
2351 term
->term_editing
= state
;
2352 if (term
->ldisc
) /* cause ldisc to notice changes */
2353 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2355 case 25: /* DECTCEM: enable/disable cursor */
2356 compatibility2(OTHER
, VT220
);
2357 term
->cursor_on
= state
;
2358 seen_disp_event(term
);
2360 case 47: /* alternate screen */
2361 compatibility(OTHER
);
2363 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, FALSE
, FALSE
);
2366 case 1000: /* xterm mouse 1 */
2367 term
->xterm_mouse
= state ?
1 : 0;
2368 set_raw_mouse_mode(term
->frontend
, state
);
2370 case 1002: /* xterm mouse 2 */
2371 term
->xterm_mouse
= state ?
2 : 0;
2372 set_raw_mouse_mode(term
->frontend
, state
);
2374 case 1047: /* alternate screen */
2375 compatibility(OTHER
);
2377 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, TRUE
, TRUE
);
2380 case 1048: /* save/restore cursor */
2381 if (!term
->cfg
.no_alt_screen
)
2382 save_cursor(term
, state
);
2383 if (!state
) seen_disp_event(term
);
2385 case 1049: /* cursor & alternate screen */
2386 if (state
&& !term
->cfg
.no_alt_screen
)
2387 save_cursor(term
, state
);
2388 if (!state
) seen_disp_event(term
);
2389 compatibility(OTHER
);
2391 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, TRUE
, FALSE
);
2392 if (!state
&& !term
->cfg
.no_alt_screen
)
2393 save_cursor(term
, state
);
2398 case 4: /* IRM: set insert mode */
2399 compatibility(VT102
);
2400 term
->insert
= state
;
2402 case 12: /* SRM: set echo mode */
2403 term
->term_echoing
= !state
;
2404 if (term
->ldisc
) /* cause ldisc to notice changes */
2405 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2407 case 20: /* LNM: Return sends ... */
2408 term
->cr_lf_return
= state
;
2410 case 34: /* WYULCURM: Make cursor BIG */
2411 compatibility2(OTHER
, VT220
);
2412 term
->big_cursor
= !state
;
2417 * Process an OSC sequence: set window title or icon name.
2419 static void do_osc(Terminal
*term
)
2422 while (term
->osc_strlen
--)
2423 term
->wordness
[(unsigned char)
2424 term
->osc_string
[term
->osc_strlen
]] = term
->esc_args
[0];
2426 term
->osc_string
[term
->osc_strlen
] = '\0';
2427 switch (term
->esc_args
[0]) {
2430 if (!term
->cfg
.no_remote_wintitle
)
2431 set_icon(term
->frontend
, term
->osc_string
);
2432 if (term
->esc_args
[0] == 1)
2434 /* fall through: parameter 0 means set both */
2437 if (!term
->cfg
.no_remote_wintitle
)
2438 set_title(term
->frontend
, term
->osc_string
);
2445 * ANSI printing routines.
2447 static void term_print_setup(Terminal
*term
)
2449 bufchain_clear(&term
->printer_buf
);
2450 term
->print_job
= printer_start_job(term
->cfg
.printer
);
2452 static void term_print_flush(Terminal
*term
)
2457 while ((size
= bufchain_size(&term
->printer_buf
)) > 5) {
2458 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
2461 printer_job_data(term
->print_job
, data
, len
);
2462 bufchain_consume(&term
->printer_buf
, len
);
2465 static void term_print_finish(Terminal
*term
)
2471 if (!term
->printing
&& !term
->only_printing
)
2472 return; /* we need do nothing */
2474 term_print_flush(term
);
2475 while ((size
= bufchain_size(&term
->printer_buf
)) > 0) {
2476 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
2478 if (c
== '\033' || c
== '\233') {
2479 bufchain_consume(&term
->printer_buf
, size
);
2482 printer_job_data(term
->print_job
, &c
, 1);
2483 bufchain_consume(&term
->printer_buf
, 1);
2486 printer_finish_job(term
->print_job
);
2487 term
->print_job
= NULL
;
2488 term
->printing
= term
->only_printing
= FALSE
;
2492 * Remove everything currently in `inbuf' and stick it up on the
2493 * in-memory display. There's a big state machine in here to
2494 * process escape sequences...
2496 static void term_out(Terminal
*term
)
2500 unsigned char localbuf
[256], *chars
;
2505 chars
= NULL
; /* placate compiler warnings */
2506 while (nchars
> 0 || unget
!= -1 || bufchain_size(&term
->inbuf
) > 0) {
2510 bufchain_prefix(&term
->inbuf
, &ret
, &nchars
);
2511 if (nchars
> sizeof(localbuf
))
2512 nchars
= sizeof(localbuf
);
2513 memcpy(localbuf
, ret
, nchars
);
2514 bufchain_consume(&term
->inbuf
, nchars
);
2516 assert(chars
!= NULL
);
2522 * Optionally log the session traffic to a file. Useful for
2523 * debugging and possibly also useful for actual logging.
2525 if (term
->cfg
.logtype
== LGTYP_DEBUG
&& term
->logctx
)
2526 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_DEBUG
);
2532 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
2533 * be able to display 8-bit characters, but I'll let that go 'cause
2538 * If we're printing, add the character to the printer
2541 if (term
->printing
) {
2542 bufchain_add(&term
->printer_buf
, &c
, 1);
2545 * If we're in print-only mode, we use a much simpler
2546 * state machine designed only to recognise the ESC[4i
2547 * termination sequence.
2549 if (term
->only_printing
) {
2551 term
->print_state
= 1;
2552 else if (c
== (unsigned char)'\233')
2553 term
->print_state
= 2;
2554 else if (c
== '[' && term
->print_state
== 1)
2555 term
->print_state
= 2;
2556 else if (c
== '4' && term
->print_state
== 2)
2557 term
->print_state
= 3;
2558 else if (c
== 'i' && term
->print_state
== 3)
2559 term
->print_state
= 4;
2561 term
->print_state
= 0;
2562 if (term
->print_state
== 4) {
2563 term_print_finish(term
);
2569 /* First see about all those translations. */
2570 if (term
->termstate
== TOPLEVEL
) {
2572 switch (term
->utf_state
) {
2575 /* UTF-8 must be stateless so we ignore iso2022. */
2576 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2577 c
= term
->ucsdata
->unitab_ctrl
[c
];
2578 else c
= ((unsigned char)c
) | CSET_ASCII
;
2580 } else if ((c
& 0xe0) == 0xc0) {
2581 term
->utf_size
= term
->utf_state
= 1;
2582 term
->utf_char
= (c
& 0x1f);
2583 } else if ((c
& 0xf0) == 0xe0) {
2584 term
->utf_size
= term
->utf_state
= 2;
2585 term
->utf_char
= (c
& 0x0f);
2586 } else if ((c
& 0xf8) == 0xf0) {
2587 term
->utf_size
= term
->utf_state
= 3;
2588 term
->utf_char
= (c
& 0x07);
2589 } else if ((c
& 0xfc) == 0xf8) {
2590 term
->utf_size
= term
->utf_state
= 4;
2591 term
->utf_char
= (c
& 0x03);
2592 } else if ((c
& 0xfe) == 0xfc) {
2593 term
->utf_size
= term
->utf_state
= 5;
2594 term
->utf_char
= (c
& 0x01);
2605 if ((c
& 0xC0) != 0x80) {
2608 term
->utf_state
= 0;
2611 term
->utf_char
= (term
->utf_char
<< 6) | (c
& 0x3f);
2612 if (--term
->utf_state
)
2617 /* Is somebody trying to be evil! */
2619 (c
< 0x800 && term
->utf_size
>= 2) ||
2620 (c
< 0x10000 && term
->utf_size
>= 3) ||
2621 (c
< 0x200000 && term
->utf_size
>= 4) ||
2622 (c
< 0x4000000 && term
->utf_size
>= 5))
2625 /* Unicode line separator and paragraph separator are CR-LF */
2626 if (c
== 0x2028 || c
== 0x2029)
2629 /* High controls are probably a Baaad idea too. */
2633 /* The UTF-16 surrogates are not nice either. */
2634 /* The standard give the option of decoding these:
2635 * I don't want to! */
2636 if (c
>= 0xD800 && c
< 0xE000)
2639 /* ISO 10646 characters now limited to UTF-16 range. */
2643 /* This is currently a TagPhobic application.. */
2644 if (c
>= 0xE0000 && c
<= 0xE007F)
2647 /* U+FEFF is best seen as a null. */
2650 /* But U+FFFE is an error. */
2651 if (c
== 0xFFFE || c
== 0xFFFF)
2656 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
2657 else if(term
->sco_acs
&&
2658 (c
!='\033' && c
!='\012' && c
!='\015' && c
!='\b'))
2660 if (term
->sco_acs
== 2) c
|= 0x80;
2663 switch (term
->cset_attr
[term
->cset
]) {
2665 * Linedraw characters are different from 'ESC ( B'
2666 * only for a small range. For ones outside that
2667 * range, make sure we use the same font as well as
2668 * the same encoding.
2671 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2672 c
= term
->ucsdata
->unitab_ctrl
[c
];
2674 c
= ((unsigned char) c
) | CSET_LINEDRW
;
2678 /* If UK-ASCII, make the '#' a LineDraw Pound */
2680 c
= '}' | CSET_LINEDRW
;
2683 /*FALLTHROUGH*/ case CSET_ASCII
:
2684 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2685 c
= term
->ucsdata
->unitab_ctrl
[c
];
2687 c
= ((unsigned char) c
) | CSET_ASCII
;
2690 if (c
>=' ') c
= ((unsigned char)c
) | CSET_SCOACS
;
2697 * How about C1 controls?
2698 * Explicitly ignore SCI (0x9a), which we don't translate to DECID.
2700 if ((c
& -32) == 0x80 && term
->termstate
< DO_CTRLS
&&
2701 !term
->vt52_mode
&& has_compat(VT220
)) {
2705 term
->termstate
= SEEN_ESC
;
2706 term
->esc_query
= FALSE
;
2707 c
= '@' + (c
& 0x1F);
2711 /* Or the GL control. */
2712 if (c
== '\177' && term
->termstate
< DO_CTRLS
&& has_compat(OTHER
)) {
2713 if (term
->curs
.x
&& !term
->wrapnext
)
2715 term
->wrapnext
= FALSE
;
2716 /* destructive backspace might be disabled */
2717 if (!term
->cfg
.no_dbackspace
) {
2718 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2719 check_boundary(term
, term
->curs
.x
+1, term
->curs
.y
);
2720 copy_termchar(scrlineptr(term
->curs
.y
),
2721 term
->curs
.x
, &term
->erase_char
);
2724 /* Or normal C0 controls. */
2725 if ((c
& ~0x1F) == 0 && term
->termstate
< DO_CTRLS
) {
2727 case '\005': /* ENQ: terminal type query */
2729 * Strictly speaking this is VT100 but a VT100 defaults to
2730 * no response. Other terminals respond at their option.
2732 * Don't put a CR in the default string as this tends to
2733 * upset some weird software.
2735 compatibility(ANSIMIN
);
2737 char abuf
[lenof(term
->cfg
.answerback
)], *s
, *d
;
2738 for (s
= term
->cfg
.answerback
, d
= abuf
; *s
;) {
2740 char c
= ctrlparse(s
, &n
);
2748 lpage_send(term
->ldisc
, DEFAULT_CODEPAGE
,
2752 case '\007': /* BEL: Bell */
2754 struct beeptime
*newbeep
;
2755 unsigned long ticks
;
2757 ticks
= GETTICKCOUNT();
2759 if (!term
->beep_overloaded
) {
2760 newbeep
= snew(struct beeptime
);
2761 newbeep
->ticks
= ticks
;
2762 newbeep
->next
= NULL
;
2763 if (!term
->beephead
)
2764 term
->beephead
= newbeep
;
2766 term
->beeptail
->next
= newbeep
;
2767 term
->beeptail
= newbeep
;
2772 * Throw out any beeps that happened more than
2775 while (term
->beephead
&&
2776 term
->beephead
->ticks
< ticks
- term
->cfg
.bellovl_t
) {
2777 struct beeptime
*tmp
= term
->beephead
;
2778 term
->beephead
= tmp
->next
;
2780 if (!term
->beephead
)
2781 term
->beeptail
= NULL
;
2785 if (term
->cfg
.bellovl
&& term
->beep_overloaded
&&
2786 ticks
- term
->lastbeep
>= (unsigned)term
->cfg
.bellovl_s
) {
2788 * If we're currently overloaded and the
2789 * last beep was more than s seconds ago,
2790 * leave overload mode.
2792 term
->beep_overloaded
= FALSE
;
2793 } else if (term
->cfg
.bellovl
&& !term
->beep_overloaded
&&
2794 term
->nbeeps
>= term
->cfg
.bellovl_n
) {
2796 * Now, if we have n or more beeps
2797 * remaining in the queue, go into overload
2800 term
->beep_overloaded
= TRUE
;
2802 term
->lastbeep
= ticks
;
2805 * Perform an actual beep if we're not overloaded.
2807 if (!term
->cfg
.bellovl
|| !term
->beep_overloaded
) {
2808 do_beep(term
->frontend
, term
->cfg
.beep
);
2810 if (term
->cfg
.beep
== BELL_VISUAL
) {
2811 term_schedule_vbell(term
, FALSE
, 0);
2814 seen_disp_event(term
);
2817 case '\b': /* BS: Back space */
2818 if (term
->curs
.x
== 0 &&
2819 (term
->curs
.y
== 0 || term
->wrap
== 0))
2821 else if (term
->curs
.x
== 0 && term
->curs
.y
> 0)
2822 term
->curs
.x
= term
->cols
- 1, term
->curs
.y
--;
2823 else if (term
->wrapnext
)
2824 term
->wrapnext
= FALSE
;
2827 seen_disp_event(term
);
2829 case '\016': /* LS1: Locking-shift one */
2830 compatibility(VT100
);
2833 case '\017': /* LS0: Locking-shift zero */
2834 compatibility(VT100
);
2837 case '\033': /* ESC: Escape */
2838 if (term
->vt52_mode
)
2839 term
->termstate
= VT52_ESC
;
2841 compatibility(ANSIMIN
);
2842 term
->termstate
= SEEN_ESC
;
2843 term
->esc_query
= FALSE
;
2846 case '\015': /* CR: Carriage return */
2848 term
->wrapnext
= FALSE
;
2849 seen_disp_event(term
);
2850 term
->paste_hold
= 0;
2852 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
2854 case '\014': /* FF: Form feed */
2855 if (has_compat(SCOANSI
)) {
2856 move(term
, 0, 0, 0);
2857 erase_lots(term
, FALSE
, FALSE
, TRUE
);
2859 term
->wrapnext
= FALSE
;
2860 seen_disp_event(term
);
2863 case '\013': /* VT: Line tabulation */
2864 compatibility(VT100
);
2865 case '\012': /* LF: Line feed */
2866 if (term
->curs
.y
== term
->marg_b
)
2867 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2868 else if (term
->curs
.y
< term
->rows
- 1)
2870 if (term
->cfg
.lfhascr
)
2872 term
->wrapnext
= FALSE
;
2873 seen_disp_event(term
);
2874 term
->paste_hold
= 0;
2876 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
2878 case '\t': /* HT: Character tabulation */
2880 pos old_curs
= term
->curs
;
2881 termline
*ldata
= scrlineptr(term
->curs
.y
);
2885 } while (term
->curs
.x
< term
->cols
- 1 &&
2886 !term
->tabs
[term
->curs
.x
]);
2888 if ((ldata
->lattr
& LATTR_MODE
) != LATTR_NORM
) {
2889 if (term
->curs
.x
>= term
->cols
/ 2)
2890 term
->curs
.x
= term
->cols
/ 2 - 1;
2892 if (term
->curs
.x
>= term
->cols
)
2893 term
->curs
.x
= term
->cols
- 1;
2896 check_selection(term
, old_curs
, term
->curs
);
2898 seen_disp_event(term
);
2902 switch (term
->termstate
) {
2904 /* Only graphic characters get this far;
2905 * ctrls are stripped above */
2907 termline
*cline
= scrlineptr(term
->curs
.y
);
2912 width
= (term
->cfg
.cjk_ambig_wide ?
2913 mk_wcwidth_cjk((wchar_t) c
) :
2914 mk_wcwidth((wchar_t) c
));
2916 if (term
->wrapnext
&& term
->wrap
&& width
> 0) {
2917 cline
->lattr
|= LATTR_WRAPPED
;
2918 if (term
->curs
.y
== term
->marg_b
)
2919 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2920 else if (term
->curs
.y
< term
->rows
- 1)
2923 term
->wrapnext
= FALSE
;
2924 cline
= scrlineptr(term
->curs
.y
);
2926 if (term
->insert
&& width
> 0)
2928 if (term
->selstate
!= NO_SELECTION
) {
2929 pos cursplus
= term
->curs
;
2931 check_selection(term
, term
->curs
, cursplus
);
2933 if (((c
& CSET_MASK
) == CSET_ASCII
||
2934 (c
& CSET_MASK
) == 0) &&
2936 logtraffic(term
->logctx
, (unsigned char) c
,
2942 * If we're about to display a double-width
2943 * character starting in the rightmost
2944 * column, then we do something special
2945 * instead. We must print a space in the
2946 * last column of the screen, then wrap;
2947 * and we also set LATTR_WRAPPED2 which
2948 * instructs subsequent cut-and-pasting not
2949 * only to splice this line to the one
2950 * after it, but to ignore the space in the
2951 * last character position as well.
2952 * (Because what was actually output to the
2953 * terminal was presumably just a sequence
2954 * of CJK characters, and we don't want a
2955 * space to be pasted in the middle of
2956 * those just because they had the
2957 * misfortune to start in the wrong parity
2958 * column. xterm concurs.)
2960 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2961 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
2962 if (term
->curs
.x
== term
->cols
-1) {
2963 copy_termchar(cline
, term
->curs
.x
,
2965 cline
->lattr
|= LATTR_WRAPPED
| LATTR_WRAPPED2
;
2966 if (term
->curs
.y
== term
->marg_b
)
2967 scroll(term
, term
->marg_t
, term
->marg_b
,
2969 else if (term
->curs
.y
< term
->rows
- 1)
2972 cline
= scrlineptr(term
->curs
.y
);
2973 /* Now we must check_boundary again, of course. */
2974 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2975 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
2979 clear_cc(cline
, term
->curs
.x
);
2980 cline
->chars
[term
->curs
.x
].chr
= c
;
2981 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
2986 clear_cc(cline
, term
->curs
.x
);
2987 cline
->chars
[term
->curs
.x
].chr
= UCSWIDE
;
2988 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
2992 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2993 check_boundary(term
, term
->curs
.x
+1, term
->curs
.y
);
2996 clear_cc(cline
, term
->curs
.x
);
2997 cline
->chars
[term
->curs
.x
].chr
= c
;
2998 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
3002 if (term
->curs
.x
> 0) {
3003 int x
= term
->curs
.x
- 1;
3005 /* If we're in wrapnext state, the character
3006 * to combine with is _here_, not to our left. */
3011 * If the previous character is
3012 * UCSWIDE, back up another one.
3014 if (cline
->chars
[x
].chr
== UCSWIDE
) {
3019 add_cc(cline
, x
, c
);
3020 seen_disp_event(term
);
3027 if (term
->curs
.x
== term
->cols
) {
3029 term
->wrapnext
= TRUE
;
3030 if (term
->wrap
&& term
->vt52_mode
) {
3031 cline
->lattr
|= LATTR_WRAPPED
;
3032 if (term
->curs
.y
== term
->marg_b
)
3033 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
3034 else if (term
->curs
.y
< term
->rows
- 1)
3037 term
->wrapnext
= FALSE
;
3040 seen_disp_event(term
);
3046 * This state is virtually identical to SEEN_ESC, with the
3047 * exception that we have an OSC sequence in the pipeline,
3048 * and _if_ we see a backslash, we process it.
3052 term
->termstate
= TOPLEVEL
;
3055 /* else fall through */
3057 if (c
>= ' ' && c
<= '/') {
3058 if (term
->esc_query
)
3059 term
->esc_query
= -1;
3061 term
->esc_query
= c
;
3064 term
->termstate
= TOPLEVEL
;
3065 switch (ANSI(c
, term
->esc_query
)) {
3066 case '[': /* enter CSI mode */
3067 term
->termstate
= SEEN_CSI
;
3068 term
->esc_nargs
= 1;
3069 term
->esc_args
[0] = ARG_DEFAULT
;
3070 term
->esc_query
= FALSE
;
3072 case ']': /* OSC: xterm escape sequences */
3073 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
3074 compatibility(OTHER
);
3075 term
->termstate
= SEEN_OSC
;
3076 term
->esc_args
[0] = 0;
3078 case '7': /* DECSC: save cursor */
3079 compatibility(VT100
);
3080 save_cursor(term
, TRUE
);
3082 case '8': /* DECRC: restore cursor */
3083 compatibility(VT100
);
3084 save_cursor(term
, FALSE
);
3085 seen_disp_event(term
);
3087 case '=': /* DECKPAM: Keypad application mode */
3088 compatibility(VT100
);
3089 term
->app_keypad_keys
= TRUE
;
3091 case '>': /* DECKPNM: Keypad numeric mode */
3092 compatibility(VT100
);
3093 term
->app_keypad_keys
= FALSE
;
3095 case 'D': /* IND: exactly equivalent to LF */
3096 compatibility(VT100
);
3097 if (term
->curs
.y
== term
->marg_b
)
3098 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
3099 else if (term
->curs
.y
< term
->rows
- 1)
3101 term
->wrapnext
= FALSE
;
3102 seen_disp_event(term
);
3104 case 'E': /* NEL: exactly equivalent to CR-LF */
3105 compatibility(VT100
);
3107 if (term
->curs
.y
== term
->marg_b
)
3108 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
3109 else if (term
->curs
.y
< term
->rows
- 1)
3111 term
->wrapnext
= FALSE
;
3112 seen_disp_event(term
);
3114 case 'M': /* RI: reverse index - backwards LF */
3115 compatibility(VT100
);
3116 if (term
->curs
.y
== term
->marg_t
)
3117 scroll(term
, term
->marg_t
, term
->marg_b
, -1, TRUE
);
3118 else if (term
->curs
.y
> 0)
3120 term
->wrapnext
= FALSE
;
3121 seen_disp_event(term
);
3123 case 'Z': /* DECID: terminal type query */
3124 compatibility(VT100
);
3126 ldisc_send(term
->ldisc
, term
->id_string
,
3127 strlen(term
->id_string
), 0);
3129 case 'c': /* RIS: restore power-on settings */
3130 compatibility(VT100
);
3131 power_on(term
, TRUE
);
3132 if (term
->ldisc
) /* cause ldisc to notice changes */
3133 ldisc_send(term
->ldisc
, NULL
, 0, 0);
3134 if (term
->reset_132
) {
3135 if (!term
->cfg
.no_remote_resize
)
3136 request_resize(term
->frontend
, 80, term
->rows
);
3137 term
->reset_132
= 0;
3140 seen_disp_event(term
);
3142 case 'H': /* HTS: set a tab */
3143 compatibility(VT100
);
3144 term
->tabs
[term
->curs
.x
] = TRUE
;
3147 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
3148 compatibility(VT100
);
3154 for (i
= 0; i
< term
->rows
; i
++) {
3155 ldata
= scrlineptr(i
);
3156 for (j
= 0; j
< term
->cols
; j
++) {
3157 copy_termchar(ldata
, j
,
3158 &term
->basic_erase_char
);
3159 ldata
->chars
[j
].chr
= 'E';
3161 ldata
->lattr
= LATTR_NORM
;
3164 seen_disp_event(term
);
3165 scrtop
.x
= scrtop
.y
= 0;
3167 scrbot
.y
= term
->rows
;
3168 check_selection(term
, scrtop
, scrbot
);
3172 case ANSI('3', '#'):
3173 case ANSI('4', '#'):
3174 case ANSI('5', '#'):
3175 case ANSI('6', '#'):
3176 compatibility(VT100
);
3180 switch (ANSI(c
, term
->esc_query
)) {
3181 case ANSI('3', '#'): /* DECDHL: 2*height, top */
3184 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
3187 case ANSI('5', '#'): /* DECSWL: normal */
3188 nlattr
= LATTR_NORM
;
3190 default: /* case ANSI('6', '#'): DECDWL: 2*width */
3191 nlattr
= LATTR_WIDE
;
3194 scrlineptr(term
->curs
.y
)->lattr
= nlattr
;
3197 /* GZD4: G0 designate 94-set */
3198 case ANSI('A', '('):
3199 compatibility(VT100
);
3200 if (!term
->cfg
.no_remote_charset
)
3201 term
->cset_attr
[0] = CSET_GBCHR
;
3203 case ANSI('B', '('):
3204 compatibility(VT100
);
3205 if (!term
->cfg
.no_remote_charset
)
3206 term
->cset_attr
[0] = CSET_ASCII
;
3208 case ANSI('0', '('):
3209 compatibility(VT100
);
3210 if (!term
->cfg
.no_remote_charset
)
3211 term
->cset_attr
[0] = CSET_LINEDRW
;
3213 case ANSI('U', '('):
3214 compatibility(OTHER
);
3215 if (!term
->cfg
.no_remote_charset
)
3216 term
->cset_attr
[0] = CSET_SCOACS
;
3218 /* G1D4: G1-designate 94-set */
3219 case ANSI('A', ')'):
3220 compatibility(VT100
);
3221 if (!term
->cfg
.no_remote_charset
)
3222 term
->cset_attr
[1] = CSET_GBCHR
;
3224 case ANSI('B', ')'):
3225 compatibility(VT100
);
3226 if (!term
->cfg
.no_remote_charset
)
3227 term
->cset_attr
[1] = CSET_ASCII
;
3229 case ANSI('0', ')'):
3230 compatibility(VT100
);
3231 if (!term
->cfg
.no_remote_charset
)
3232 term
->cset_attr
[1] = CSET_LINEDRW
;
3234 case ANSI('U', ')'):
3235 compatibility(OTHER
);
3236 if (!term
->cfg
.no_remote_charset
)
3237 term
->cset_attr
[1] = CSET_SCOACS
;
3239 /* DOCS: Designate other coding system */
3240 case ANSI('8', '%'): /* Old Linux code */
3241 case ANSI('G', '%'):
3242 compatibility(OTHER
);
3243 if (!term
->cfg
.no_remote_charset
)
3246 case ANSI('@', '%'):
3247 compatibility(OTHER
);
3248 if (!term
->cfg
.no_remote_charset
)
3254 term
->termstate
= TOPLEVEL
; /* default */
3256 if (term
->esc_nargs
<= ARGS_MAX
) {
3257 if (term
->esc_args
[term
->esc_nargs
- 1] == ARG_DEFAULT
)
3258 term
->esc_args
[term
->esc_nargs
- 1] = 0;
3259 term
->esc_args
[term
->esc_nargs
- 1] =
3260 10 * term
->esc_args
[term
->esc_nargs
- 1] + c
- '0';
3262 term
->termstate
= SEEN_CSI
;
3263 } else if (c
== ';') {
3264 if (++term
->esc_nargs
<= ARGS_MAX
)
3265 term
->esc_args
[term
->esc_nargs
- 1] = ARG_DEFAULT
;
3266 term
->termstate
= SEEN_CSI
;
3267 } else if (c
< '@') {
3268 if (term
->esc_query
)
3269 term
->esc_query
= -1;
3271 term
->esc_query
= TRUE
;
3273 term
->esc_query
= c
;
3274 term
->termstate
= SEEN_CSI
;
3276 switch (ANSI(c
, term
->esc_query
)) {
3277 case 'A': /* CUU: move up N lines */
3278 move(term
, term
->curs
.x
,
3279 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
3280 seen_disp_event(term
);
3282 case 'e': /* VPR: move down N lines */
3283 compatibility(ANSI
);
3285 case 'B': /* CUD: Cursor down */
3286 move(term
, term
->curs
.x
,
3287 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
3288 seen_disp_event(term
);
3290 case ANSI('c', '>'): /* DA: report xterm version */
3291 compatibility(OTHER
);
3292 /* this reports xterm version 136 so that VIM can
3293 use the drag messages from the mouse reporting */
3295 ldisc_send(term
->ldisc
, "\033[>0;136;0c", 11, 0);
3297 case 'a': /* HPR: move right N cols */
3298 compatibility(ANSI
);
3300 case 'C': /* CUF: Cursor right */
3301 move(term
, term
->curs
.x
+ def(term
->esc_args
[0], 1),
3303 seen_disp_event(term
);
3305 case 'D': /* CUB: move left N cols */
3306 move(term
, term
->curs
.x
- def(term
->esc_args
[0], 1),
3308 seen_disp_event(term
);
3310 case 'E': /* CNL: move down N lines and CR */
3311 compatibility(ANSI
);
3313 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
3314 seen_disp_event(term
);
3316 case 'F': /* CPL: move up N lines and CR */
3317 compatibility(ANSI
);
3319 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
3320 seen_disp_event(term
);
3323 case '`': /* HPA: set horizontal posn */
3324 compatibility(ANSI
);
3325 move(term
, def(term
->esc_args
[0], 1) - 1,
3327 seen_disp_event(term
);
3329 case 'd': /* VPA: set vertical posn */
3330 compatibility(ANSI
);
3331 move(term
, term
->curs
.x
,
3332 ((term
->dec_om ? term
->marg_t
: 0) +
3333 def(term
->esc_args
[0], 1) - 1),
3334 (term
->dec_om ?
2 : 0));
3335 seen_disp_event(term
);
3338 case 'f': /* HVP: set horz and vert posns at once */
3339 if (term
->esc_nargs
< 2)
3340 term
->esc_args
[1] = ARG_DEFAULT
;
3341 move(term
, def(term
->esc_args
[1], 1) - 1,
3342 ((term
->dec_om ? term
->marg_t
: 0) +
3343 def(term
->esc_args
[0], 1) - 1),
3344 (term
->dec_om ?
2 : 0));
3345 seen_disp_event(term
);
3347 case 'J': /* ED: erase screen or parts of it */
3349 unsigned int i
= def(term
->esc_args
[0], 0);
3351 /* Erase Saved Lines (xterm)
3352 * This follows Thomas Dickey's xterm. */
3358 erase_lots(term
, FALSE
, !!(i
& 2), !!(i
& 1));
3362 seen_disp_event(term
);
3364 case 'K': /* EL: erase line or parts of it */
3366 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
3369 erase_lots(term
, TRUE
, !!(i
& 2), !!(i
& 1));
3371 seen_disp_event(term
);
3373 case 'L': /* IL: insert lines */
3374 compatibility(VT102
);
3375 if (term
->curs
.y
<= term
->marg_b
)
3376 scroll(term
, term
->curs
.y
, term
->marg_b
,
3377 -def(term
->esc_args
[0], 1), FALSE
);
3378 seen_disp_event(term
);
3380 case 'M': /* DL: delete lines */
3381 compatibility(VT102
);
3382 if (term
->curs
.y
<= term
->marg_b
)
3383 scroll(term
, term
->curs
.y
, term
->marg_b
,
3384 def(term
->esc_args
[0], 1),
3386 seen_disp_event(term
);
3388 case '@': /* ICH: insert chars */
3389 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
3390 compatibility(VT102
);
3391 insch(term
, def(term
->esc_args
[0], 1));
3392 seen_disp_event(term
);
3394 case 'P': /* DCH: delete chars */
3395 compatibility(VT102
);
3396 insch(term
, -def(term
->esc_args
[0], 1));
3397 seen_disp_event(term
);
3399 case 'c': /* DA: terminal type query */
3400 compatibility(VT100
);
3401 /* This is the response for a VT102 */
3403 ldisc_send(term
->ldisc
, term
->id_string
,
3404 strlen(term
->id_string
), 0);
3406 case 'n': /* DSR: cursor position query */
3408 if (term
->esc_args
[0] == 6) {
3410 sprintf(buf
, "\033[%d;%dR", term
->curs
.y
+ 1,
3412 ldisc_send(term
->ldisc
, buf
, strlen(buf
), 0);
3413 } else if (term
->esc_args
[0] == 5) {
3414 ldisc_send(term
->ldisc
, "\033[0n", 4, 0);
3418 case 'h': /* SM: toggle modes to high */
3420 compatibility(VT100
);
3423 for (i
= 0; i
< term
->esc_nargs
; i
++)
3424 toggle_mode(term
, term
->esc_args
[i
],
3425 term
->esc_query
, TRUE
);
3428 case 'i': /* MC: Media copy */
3430 compatibility(VT100
);
3432 if (term
->esc_nargs
!= 1) break;
3433 if (term
->esc_args
[0] == 5 && *term
->cfg
.printer
) {
3434 term
->printing
= TRUE
;
3435 term
->only_printing
= !term
->esc_query
;
3436 term
->print_state
= 0;
3437 term_print_setup(term
);
3438 } else if (term
->esc_args
[0] == 4 &&
3440 term_print_finish(term
);
3444 case 'l': /* RM: toggle modes to low */
3446 compatibility(VT100
);
3449 for (i
= 0; i
< term
->esc_nargs
; i
++)
3450 toggle_mode(term
, term
->esc_args
[i
],
3451 term
->esc_query
, FALSE
);
3454 case 'g': /* TBC: clear tabs */
3455 compatibility(VT100
);
3456 if (term
->esc_nargs
== 1) {
3457 if (term
->esc_args
[0] == 0) {
3458 term
->tabs
[term
->curs
.x
] = FALSE
;
3459 } else if (term
->esc_args
[0] == 3) {
3461 for (i
= 0; i
< term
->cols
; i
++)
3462 term
->tabs
[i
] = FALSE
;
3466 case 'r': /* DECSTBM: set scroll margins */
3467 compatibility(VT100
);
3468 if (term
->esc_nargs
<= 2) {
3470 top
= def(term
->esc_args
[0], 1) - 1;
3471 bot
= (term
->esc_nargs
<= 1
3472 || term
->esc_args
[1] == 0 ?
3474 def(term
->esc_args
[1], term
->rows
)) - 1;
3475 if (bot
>= term
->rows
)
3476 bot
= term
->rows
- 1;
3477 /* VTTEST Bug 9 - if region is less than 2 lines
3478 * don't change region.
3480 if (bot
- top
> 0) {
3485 * I used to think the cursor should be
3486 * placed at the top of the newly marginned
3487 * area. Apparently not: VMS TPU falls over
3490 * Well actually it should for
3493 term
->curs
.y
= (term
->dec_om ?
3495 seen_disp_event(term
);
3499 case 'm': /* SGR: set graphics rendition */
3502 * A VT100 without the AVO only had one
3503 * attribute, either underline or
3504 * reverse video depending on the
3505 * cursor type, this was selected by
3509 * This is sometimes DIM, eg on the
3512 * This is sometimes INVIS various ANSI.
3514 * This like 22 disables BOLD, DIM and INVIS
3516 * The ANSI colours appear on any
3517 * terminal that has colour (obviously)
3518 * but the interaction between sgr0 and
3519 * the colours varies but is usually
3520 * related to the background colour
3521 * erase item. The interaction between
3522 * colour attributes and the mono ones
3523 * is also very implementation
3526 * The 39 and 49 attributes are likely
3527 * to be unimplemented.
3530 for (i
= 0; i
< term
->esc_nargs
; i
++) {
3531 switch (def(term
->esc_args
[i
], 0)) {
3532 case 0: /* restore defaults */
3533 term
->curr_attr
= term
->default_attr
;
3535 case 1: /* enable bold */
3536 compatibility(VT100AVO
);
3537 term
->curr_attr
|= ATTR_BOLD
;
3539 case 21: /* (enable double underline) */
3540 compatibility(OTHER
);
3541 case 4: /* enable underline */
3542 compatibility(VT100AVO
);
3543 term
->curr_attr
|= ATTR_UNDER
;
3545 case 5: /* enable blink */
3546 compatibility(VT100AVO
);
3547 term
->curr_attr
|= ATTR_BLINK
;
3549 case 6: /* SCO light bkgrd */
3550 compatibility(SCOANSI
);
3551 term
->blink_is_real
= FALSE
;
3552 term
->curr_attr
|= ATTR_BLINK
;
3553 term_schedule_tblink(term
);
3555 case 7: /* enable reverse video */
3556 term
->curr_attr
|= ATTR_REVERSE
;
3558 case 10: /* SCO acs off */
3559 compatibility(SCOANSI
);
3560 if (term
->cfg
.no_remote_charset
) break;
3561 term
->sco_acs
= 0; break;
3562 case 11: /* SCO acs on */
3563 compatibility(SCOANSI
);
3564 if (term
->cfg
.no_remote_charset
) break;
3565 term
->sco_acs
= 1; break;
3566 case 12: /* SCO acs on, |0x80 */
3567 compatibility(SCOANSI
);
3568 if (term
->cfg
.no_remote_charset
) break;
3569 term
->sco_acs
= 2; break;
3570 case 22: /* disable bold */
3571 compatibility2(OTHER
, VT220
);
3572 term
->curr_attr
&= ~ATTR_BOLD
;
3574 case 24: /* disable underline */
3575 compatibility2(OTHER
, VT220
);
3576 term
->curr_attr
&= ~ATTR_UNDER
;
3578 case 25: /* disable blink */
3579 compatibility2(OTHER
, VT220
);
3580 term
->curr_attr
&= ~ATTR_BLINK
;
3582 case 27: /* disable reverse video */
3583 compatibility2(OTHER
, VT220
);
3584 term
->curr_attr
&= ~ATTR_REVERSE
;
3595 term
->curr_attr
&= ~ATTR_FGMASK
;
3597 (term
->esc_args
[i
] - 30)<<ATTR_FGSHIFT
;
3607 /* aixterm-style bright foreground */
3608 term
->curr_attr
&= ~ATTR_FGMASK
;
3610 ((term
->esc_args
[i
] - 90 + 8)
3613 case 39: /* default-foreground */
3614 term
->curr_attr
&= ~ATTR_FGMASK
;
3615 term
->curr_attr
|= ATTR_DEFFG
;
3626 term
->curr_attr
&= ~ATTR_BGMASK
;
3628 (term
->esc_args
[i
] - 40)<<ATTR_BGSHIFT
;
3638 /* aixterm-style bright background */
3639 term
->curr_attr
&= ~ATTR_BGMASK
;
3641 ((term
->esc_args
[i
] - 100 + 8)
3644 case 49: /* default-background */
3645 term
->curr_attr
&= ~ATTR_BGMASK
;
3646 term
->curr_attr
|= ATTR_DEFBG
;
3648 case 38: /* xterm 256-colour mode */
3649 if (i
+2 < term
->esc_nargs
&&
3650 term
->esc_args
[i
+1] == 5) {
3651 term
->curr_attr
&= ~ATTR_FGMASK
;
3653 ((term
->esc_args
[i
+2] & 0xFF)
3658 case 48: /* xterm 256-colour mode */
3659 if (i
+2 < term
->esc_nargs
&&
3660 term
->esc_args
[i
+1] == 5) {
3661 term
->curr_attr
&= ~ATTR_BGMASK
;
3663 ((term
->esc_args
[i
+2] & 0xFF)
3670 set_erase_char(term
);
3673 case 's': /* save cursor */
3674 save_cursor(term
, TRUE
);
3676 case 'u': /* restore cursor */
3677 save_cursor(term
, FALSE
);
3678 seen_disp_event(term
);
3680 case 't': /* DECSLPP: set page size - ie window height */
3682 * VT340/VT420 sequence DECSLPP, DEC only allows values
3683 * 24/25/36/48/72/144 other emulators (eg dtterm) use
3684 * illegal values (eg first arg 1..9) for window changing
3687 if (term
->esc_nargs
<= 1
3688 && (term
->esc_args
[0] < 1 ||
3689 term
->esc_args
[0] >= 24)) {
3690 compatibility(VT340TEXT
);
3691 if (!term
->cfg
.no_remote_resize
)
3692 request_resize(term
->frontend
, term
->cols
,
3693 def(term
->esc_args
[0], 24));
3695 } else if (term
->esc_nargs
>= 1 &&
3696 term
->esc_args
[0] >= 1 &&
3697 term
->esc_args
[0] < 24) {
3698 compatibility(OTHER
);
3700 switch (term
->esc_args
[0]) {
3704 set_iconic(term
->frontend
, FALSE
);
3707 set_iconic(term
->frontend
, TRUE
);
3710 if (term
->esc_nargs
>= 3) {
3711 if (!term
->cfg
.no_remote_resize
)
3712 move_window(term
->frontend
,
3713 def(term
->esc_args
[1], 0),
3714 def(term
->esc_args
[2], 0));
3718 /* We should resize the window to a given
3719 * size in pixels here, but currently our
3720 * resizing code isn't healthy enough to
3725 set_zorder(term
->frontend
, TRUE
);
3728 /* move to bottom */
3729 set_zorder(term
->frontend
, FALSE
);
3732 refresh_window(term
->frontend
);
3735 if (term
->esc_nargs
>= 3) {
3736 if (!term
->cfg
.no_remote_resize
)
3737 request_resize(term
->frontend
,
3738 def(term
->esc_args
[2], term
->cfg
.width
),
3739 def(term
->esc_args
[1], term
->cfg
.height
));
3743 if (term
->esc_nargs
>= 2)
3744 set_zoomed(term
->frontend
,
3750 ldisc_send(term
->ldisc
,
3751 is_iconic(term
->frontend
) ?
3752 "\033[1t" : "\033[2t", 4, 0);
3756 get_window_pos(term
->frontend
, &x
, &y
);
3757 len
= sprintf(buf
, "\033[3;%d;%dt", x
, y
);
3758 ldisc_send(term
->ldisc
, buf
, len
, 0);
3763 get_window_pixels(term
->frontend
, &x
, &y
);
3764 len
= sprintf(buf
, "\033[4;%d;%dt", x
, y
);
3765 ldisc_send(term
->ldisc
, buf
, len
, 0);
3770 len
= sprintf(buf
, "\033[8;%d;%dt",
3771 term
->rows
, term
->cols
);
3772 ldisc_send(term
->ldisc
, buf
, len
, 0);
3777 * Hmmm. Strictly speaking we
3778 * should return `the size of the
3779 * screen in characters', but
3780 * that's not easy: (a) window
3781 * furniture being what it is it's
3782 * hard to compute, and (b) in
3783 * resize-font mode maximising the
3784 * window wouldn't change the
3785 * number of characters. *shrug*. I
3786 * think we'll ignore it for the
3787 * moment and see if anyone
3788 * complains, and then ask them
3789 * what they would like it to do.
3794 !term
->cfg
.no_remote_qtitle
) {
3795 p
= get_window_title(term
->frontend
, TRUE
);
3797 ldisc_send(term
->ldisc
, "\033]L", 3, 0);
3798 ldisc_send(term
->ldisc
, p
, len
, 0);
3799 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
3804 !term
->cfg
.no_remote_qtitle
) {
3805 p
= get_window_title(term
->frontend
,FALSE
);
3807 ldisc_send(term
->ldisc
, "\033]l", 3, 0);
3808 ldisc_send(term
->ldisc
, p
, len
, 0);
3809 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
3815 case 'S': /* SU: Scroll up */
3816 compatibility(SCOANSI
);
3817 scroll(term
, term
->marg_t
, term
->marg_b
,
3818 def(term
->esc_args
[0], 1), TRUE
);
3819 term
->wrapnext
= FALSE
;
3820 seen_disp_event(term
);
3822 case 'T': /* SD: Scroll down */
3823 compatibility(SCOANSI
);
3824 scroll(term
, term
->marg_t
, term
->marg_b
,
3825 -def(term
->esc_args
[0], 1), TRUE
);
3826 term
->wrapnext
= FALSE
;
3827 seen_disp_event(term
);
3829 case ANSI('|', '*'): /* DECSNLS */
3831 * Set number of lines on screen
3832 * VT420 uses VGA like hardware and can
3833 * support any size in reasonable range
3834 * (24..49 AIUI) with no default specified.
3836 compatibility(VT420
);
3837 if (term
->esc_nargs
== 1 && term
->esc_args
[0] > 0) {
3838 if (!term
->cfg
.no_remote_resize
)
3839 request_resize(term
->frontend
, term
->cols
,
3840 def(term
->esc_args
[0],
3845 case ANSI('|', '$'): /* DECSCPP */
3847 * Set number of columns per page
3848 * Docs imply range is only 80 or 132, but
3851 compatibility(VT340TEXT
);
3852 if (term
->esc_nargs
<= 1) {
3853 if (!term
->cfg
.no_remote_resize
)
3854 request_resize(term
->frontend
,
3855 def(term
->esc_args
[0],
3856 term
->cfg
.width
), term
->rows
);
3860 case 'X': /* ECH: write N spaces w/o moving cursor */
3861 /* XXX VTTEST says this is vt220, vt510 manual
3863 compatibility(ANSIMIN
);
3865 int n
= def(term
->esc_args
[0], 1);
3867 int p
= term
->curs
.x
;
3868 termline
*cline
= scrlineptr(term
->curs
.y
);
3870 if (n
> term
->cols
- term
->curs
.x
)
3871 n
= term
->cols
- term
->curs
.x
;
3872 cursplus
= term
->curs
;
3874 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
3875 check_boundary(term
, term
->curs
.x
+n
, term
->curs
.y
);
3876 check_selection(term
, term
->curs
, cursplus
);
3878 copy_termchar(cline
, p
++,
3880 seen_disp_event(term
);
3883 case 'x': /* DECREQTPARM: report terminal characteristics */
3884 compatibility(VT100
);
3887 int i
= def(term
->esc_args
[0], 0);
3888 if (i
== 0 || i
== 1) {
3889 strcpy(buf
, "\033[2;1;1;112;112;1;0x");
3891 ldisc_send(term
->ldisc
, buf
, 20, 0);
3896 compatibility(OTHER
);
3898 int i
= def(term
->esc_args
[0], 1);
3899 pos old_curs
= term
->curs
;
3901 for(;i
>0 && term
->curs
.x
>0; i
--) {
3904 } while (term
->curs
.x
>0 &&
3905 !term
->tabs
[term
->curs
.x
]);
3907 check_selection(term
, old_curs
, term
->curs
);
3910 case ANSI('c', '='): /* Hide or Show Cursor */
3911 compatibility(SCOANSI
);
3912 switch(term
->esc_args
[0]) {
3913 case 0: /* hide cursor */
3914 term
->cursor_on
= FALSE
;
3916 case 1: /* restore cursor */
3917 term
->big_cursor
= FALSE
;
3918 term
->cursor_on
= TRUE
;
3920 case 2: /* block cursor */
3921 term
->big_cursor
= TRUE
;
3922 term
->cursor_on
= TRUE
;
3926 case ANSI('C', '='):
3928 * set cursor start on scanline esc_args[0] and
3929 * end on scanline esc_args[1].If you set
3930 * the bottom scan line to a value less than
3931 * the top scan line, the cursor will disappear.
3933 compatibility(SCOANSI
);
3934 if (term
->esc_nargs
>= 2) {
3935 if (term
->esc_args
[0] > term
->esc_args
[1])
3936 term
->cursor_on
= FALSE
;
3938 term
->cursor_on
= TRUE
;
3941 case ANSI('D', '='):
3942 compatibility(SCOANSI
);
3943 term
->blink_is_real
= FALSE
;
3944 term_schedule_tblink(term
);
3945 if (term
->esc_args
[0]>=1)
3946 term
->curr_attr
|= ATTR_BLINK
;
3948 term
->curr_attr
&= ~ATTR_BLINK
;
3950 case ANSI('E', '='):
3951 compatibility(SCOANSI
);
3952 term
->blink_is_real
= (term
->esc_args
[0] >= 1);
3953 term_schedule_tblink(term
);
3955 case ANSI('F', '='): /* set normal foreground */
3956 compatibility(SCOANSI
);
3957 if (term
->esc_args
[0] >= 0 && term
->esc_args
[0] < 16) {
3959 (sco2ansicolour
[term
->esc_args
[0] & 0x7] |
3960 (term
->esc_args
[0] & 0x8)) <<
3962 term
->curr_attr
&= ~ATTR_FGMASK
;
3963 term
->curr_attr
|= colour
;
3964 term
->default_attr
&= ~ATTR_FGMASK
;
3965 term
->default_attr
|= colour
;
3966 set_erase_char(term
);
3969 case ANSI('G', '='): /* set normal background */
3970 compatibility(SCOANSI
);
3971 if (term
->esc_args
[0] >= 0 && term
->esc_args
[0] < 16) {
3973 (sco2ansicolour
[term
->esc_args
[0] & 0x7] |
3974 (term
->esc_args
[0] & 0x8)) <<
3976 term
->curr_attr
&= ~ATTR_BGMASK
;
3977 term
->curr_attr
|= colour
;
3978 term
->default_attr
&= ~ATTR_BGMASK
;
3979 term
->default_attr
|= colour
;
3980 set_erase_char(term
);
3983 case ANSI('L', '='):
3984 compatibility(SCOANSI
);
3985 term
->use_bce
= (term
->esc_args
[0] <= 0);
3986 set_erase_char(term
);
3988 case ANSI('p', '"'): /* DECSCL: set compat level */
3990 * Allow the host to make this emulator a
3991 * 'perfect' VT102. This first appeared in
3992 * the VT220, but we do need to get back to
3993 * PuTTY mode so I won't check it.
3995 * The arg in 40..42,50 are a PuTTY extension.
3996 * The 2nd arg, 8bit vs 7bit is not checked.
3998 * Setting VT102 mode should also change
3999 * the Fkeys to generate PF* codes as a
4000 * real VT102 has no Fkeys. The VT220 does
4001 * this, F11..F13 become ESC,BS,LF other
4002 * Fkeys send nothing.
4004 * Note ESC c will NOT change this!
4007 switch (term
->esc_args
[0]) {
4009 term
->compatibility_level
&= ~TM_VTXXX
;
4010 term
->compatibility_level
|= TM_VT102
;
4013 term
->compatibility_level
&= ~TM_VTXXX
;
4014 term
->compatibility_level
|= TM_VT220
;
4018 if (term
->esc_args
[0] > 60 &&
4019 term
->esc_args
[0] < 70)
4020 term
->compatibility_level
|= TM_VTXXX
;
4024 term
->compatibility_level
&= TM_VTXXX
;
4027 term
->compatibility_level
= TM_PUTTY
;
4030 term
->compatibility_level
= TM_SCOANSI
;
4034 term
->compatibility_level
= TM_PUTTY
;
4040 /* Change the response to CSI c */
4041 if (term
->esc_args
[0] == 50) {
4044 strcpy(term
->id_string
, "\033[?");
4045 for (i
= 1; i
< term
->esc_nargs
; i
++) {
4047 strcat(term
->id_string
, ";");
4048 sprintf(lbuf
, "%d", term
->esc_args
[i
]);
4049 strcat(term
->id_string
, lbuf
);
4051 strcat(term
->id_string
, "c");
4054 /* Is this a good idea ?
4055 * Well we should do a soft reset at this point ...
4057 if (!has_compat(VT420
) && has_compat(VT100
)) {
4058 if (!term
->cfg
.no_remote_resize
) {
4059 if (term
->reset_132
)
4060 request_resize(132, 24);
4062 request_resize(80, 24);
4070 term
->osc_w
= FALSE
;
4072 case 'P': /* Linux palette sequence */
4073 term
->termstate
= SEEN_OSC_P
;
4074 term
->osc_strlen
= 0;
4076 case 'R': /* Linux palette reset */
4077 palette_reset(term
->frontend
);
4078 term_invalidate(term
);
4079 term
->termstate
= TOPLEVEL
;
4081 case 'W': /* word-set */
4082 term
->termstate
= SEEN_OSC_W
;
4095 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
4099 * Grotty hack to support xterm and DECterm title
4100 * sequences concurrently.
4102 if (term
->esc_args
[0] == 2) {
4103 term
->esc_args
[0] = 1;
4106 /* else fall through */
4108 term
->termstate
= OSC_STRING
;
4109 term
->osc_strlen
= 0;
4114 * This OSC stuff is EVIL. It takes just one character to get into
4115 * sysline mode and it's not initially obvious how to get out.
4116 * So I've added CR and LF as string aborts.
4117 * This shouldn't effect compatibility as I believe embedded
4118 * control characters are supposed to be interpreted (maybe?)
4119 * and they don't display anything useful anyway.
4123 if (c
== '\012' || c
== '\015') {
4124 term
->termstate
= TOPLEVEL
;
4125 } else if (c
== 0234 || c
== '\007') {
4127 * These characters terminate the string; ST and BEL
4128 * terminate the sequence and trigger instant
4129 * processing of it, whereas ESC goes back to SEEN_ESC
4130 * mode unless it is followed by \, in which case it is
4131 * synonymous with ST in the first place.
4134 term
->termstate
= TOPLEVEL
;
4135 } else if (c
== '\033')
4136 term
->termstate
= OSC_MAYBE_ST
;
4137 else if (term
->osc_strlen
< OSC_STR_MAX
)
4138 term
->osc_string
[term
->osc_strlen
++] = (char)c
;
4142 int max
= (term
->osc_strlen
== 0 ?
21 : 15);
4144 if ((int)c
>= '0' && (int)c
<= '9')
4146 else if ((int)c
>= 'A' && (int)c
<= 'A' + max
- 10)
4148 else if ((int)c
>= 'a' && (int)c
<= 'a' + max
- 10)
4151 term
->termstate
= TOPLEVEL
;
4154 term
->osc_string
[term
->osc_strlen
++] = val
;
4155 if (term
->osc_strlen
>= 7) {
4156 palette_set(term
->frontend
, term
->osc_string
[0],
4157 term
->osc_string
[1] * 16 + term
->osc_string
[2],
4158 term
->osc_string
[3] * 16 + term
->osc_string
[4],
4159 term
->osc_string
[5] * 16 + term
->osc_string
[6]);
4160 term_invalidate(term
);
4161 term
->termstate
= TOPLEVEL
;
4177 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
4180 term
->termstate
= OSC_STRING
;
4181 term
->osc_strlen
= 0;
4185 term
->termstate
= TOPLEVEL
;
4186 seen_disp_event(term
);
4189 move(term
, term
->curs
.x
, term
->curs
.y
- 1, 1);
4192 move(term
, term
->curs
.x
, term
->curs
.y
+ 1, 1);
4195 move(term
, term
->curs
.x
+ 1, term
->curs
.y
, 1);
4198 move(term
, term
->curs
.x
- 1, term
->curs
.y
, 1);
4201 * From the VT100 Manual
4202 * NOTE: The special graphics characters in the VT100
4203 * are different from those in the VT52
4205 * From VT102 manual:
4206 * 137 _ Blank - Same
4207 * 140 ` Reserved - Humm.
4208 * 141 a Solid rectangle - Similar
4209 * 142 b 1/ - Top half of fraction for the
4210 * 143 c 3/ - subscript numbers below.
4213 * 146 f Degrees - Same
4214 * 147 g Plus or minus - Same
4216 * 151 i Ellipsis (dots)
4219 * 154 l Bar at scan 0
4220 * 155 m Bar at scan 1
4221 * 156 n Bar at scan 2
4222 * 157 o Bar at scan 3 - Similar
4223 * 160 p Bar at scan 4 - Similar
4224 * 161 q Bar at scan 5 - Similar
4225 * 162 r Bar at scan 6 - Same
4226 * 163 s Bar at scan 7 - Similar
4241 term
->cset_attr
[term
->cset
= 0] = CSET_LINEDRW
;
4244 term
->cset_attr
[term
->cset
= 0] = CSET_ASCII
;
4247 move(term
, 0, 0, 0);
4250 if (term
->curs
.y
== 0)
4251 scroll(term
, 0, term
->rows
- 1, -1, TRUE
);
4252 else if (term
->curs
.y
> 0)
4254 term
->wrapnext
= FALSE
;
4257 erase_lots(term
, FALSE
, FALSE
, TRUE
);
4261 erase_lots(term
, TRUE
, FALSE
, TRUE
);
4265 /* XXX Print cursor line */
4268 /* XXX Start controller mode */
4271 /* XXX Stop controller mode */
4275 term
->termstate
= VT52_Y1
;
4279 ldisc_send(term
->ldisc
, "\033/Z", 3, 0);
4282 term
->app_keypad_keys
= TRUE
;
4285 term
->app_keypad_keys
= FALSE
;
4288 /* XXX This should switch to VT100 mode not current or default
4289 * VT mode. But this will only have effect in a VT220+
4292 term
->vt52_mode
= FALSE
;
4293 term
->blink_is_real
= term
->cfg
.blinktext
;
4294 term_schedule_tblink(term
);
4298 /* XXX Enter auto print mode */
4301 /* XXX Exit auto print mode */
4304 /* XXX Print screen */
4310 /* compatibility(ATARI) */
4311 move(term
, 0, 0, 0);
4312 erase_lots(term
, FALSE
, FALSE
, TRUE
);
4316 /* compatibility(ATARI) */
4317 if (term
->curs
.y
<= term
->marg_b
)
4318 scroll(term
, term
->curs
.y
, term
->marg_b
, -1, FALSE
);
4321 /* compatibility(ATARI) */
4322 if (term
->curs
.y
<= term
->marg_b
)
4323 scroll(term
, term
->curs
.y
, term
->marg_b
, 1, TRUE
);
4326 /* compatibility(ATARI) */
4327 term
->termstate
= VT52_FG
;
4330 /* compatibility(ATARI) */
4331 term
->termstate
= VT52_BG
;
4334 /* compatibility(ATARI) */
4335 erase_lots(term
, FALSE
, TRUE
, FALSE
);
4339 /* compatibility(ATARI) */
4340 term
->cursor_on
= TRUE
;
4343 /* compatibility(ATARI) */
4344 term
->cursor_on
= FALSE
;
4346 /* case 'j': Save cursor position - broken on ST */
4347 /* case 'k': Restore cursor position */
4349 /* compatibility(ATARI) */
4350 erase_lots(term
, TRUE
, TRUE
, TRUE
);
4352 term
->wrapnext
= FALSE
;
4355 /* compatibility(ATARI) */
4356 erase_lots(term
, TRUE
, TRUE
, FALSE
);
4359 /* compatibility(ATARI) */
4360 term
->curr_attr
|= ATTR_REVERSE
;
4363 /* compatibility(ATARI) */
4364 term
->curr_attr
&= ~ATTR_REVERSE
;
4366 case 'v': /* wrap Autowrap on - Wyse style */
4367 /* compatibility(ATARI) */
4370 case 'w': /* Autowrap off */
4371 /* compatibility(ATARI) */
4376 /* compatibility(OTHER) */
4377 term
->vt52_bold
= FALSE
;
4378 term
->curr_attr
= ATTR_DEFAULT
;
4379 set_erase_char(term
);
4382 /* compatibility(VI50) */
4383 term
->curr_attr
|= ATTR_UNDER
;
4386 /* compatibility(VI50) */
4387 term
->curr_attr
&= ~ATTR_UNDER
;
4390 /* compatibility(VI50) */
4391 term
->vt52_bold
= TRUE
;
4392 term
->curr_attr
|= ATTR_BOLD
;
4395 /* compatibility(VI50) */
4396 term
->vt52_bold
= FALSE
;
4397 term
->curr_attr
&= ~ATTR_BOLD
;
4403 term
->termstate
= VT52_Y2
;
4404 move(term
, term
->curs
.x
, c
- ' ', 0);
4407 term
->termstate
= TOPLEVEL
;
4408 move(term
, c
- ' ', term
->curs
.y
, 0);
4413 term
->termstate
= TOPLEVEL
;
4414 term
->curr_attr
&= ~ATTR_FGMASK
;
4415 term
->curr_attr
&= ~ATTR_BOLD
;
4416 term
->curr_attr
|= (c
& 0xF) << ATTR_FGSHIFT
;
4417 set_erase_char(term
);
4420 term
->termstate
= TOPLEVEL
;
4421 term
->curr_attr
&= ~ATTR_BGMASK
;
4422 term
->curr_attr
&= ~ATTR_BLINK
;
4423 term
->curr_attr
|= (c
& 0xF) << ATTR_BGSHIFT
;
4424 set_erase_char(term
);
4427 default: break; /* placate gcc warning about enum use */
4429 if (term
->selstate
!= NO_SELECTION
) {
4430 pos cursplus
= term
->curs
;
4432 check_selection(term
, term
->curs
, cursplus
);
4436 term_print_flush(term
);
4437 if (term
->cfg
.logflush
)
4438 logflush(term
->logctx
);
4442 * To prevent having to run the reasonably tricky bidi algorithm
4443 * too many times, we maintain a cache of the last lineful of data
4444 * fed to the algorithm on each line of the display.
4446 static int term_bidi_cache_hit(Terminal
*term
, int line
,
4447 termchar
*lbefore
, int width
)
4451 if (!term
->pre_bidi_cache
)
4452 return FALSE
; /* cache doesn't even exist yet! */
4454 if (line
>= term
->bidi_cache_size
)
4455 return FALSE
; /* cache doesn't have this many lines */
4457 if (!term
->pre_bidi_cache
[line
].chars
)
4458 return FALSE
; /* cache doesn't contain _this_ line */
4460 if (term
->pre_bidi_cache
[line
].width
!= width
)
4461 return FALSE
; /* line is wrong width */
4463 for (i
= 0; i
< width
; i
++)
4464 if (!termchars_equal(term
->pre_bidi_cache
[line
].chars
+i
, lbefore
+i
))
4465 return FALSE
; /* line doesn't match cache */
4467 return TRUE
; /* it didn't match. */
4470 static void term_bidi_cache_store(Terminal
*term
, int line
, termchar
*lbefore
,
4471 termchar
*lafter
, bidi_char
*wcTo
,
4472 int width
, int size
)
4476 if (!term
->pre_bidi_cache
|| term
->bidi_cache_size
<= line
) {
4477 int j
= term
->bidi_cache_size
;
4478 term
->bidi_cache_size
= line
+1;
4479 term
->pre_bidi_cache
= sresize(term
->pre_bidi_cache
,
4480 term
->bidi_cache_size
,
4481 struct bidi_cache_entry
);
4482 term
->post_bidi_cache
= sresize(term
->post_bidi_cache
,
4483 term
->bidi_cache_size
,
4484 struct bidi_cache_entry
);
4485 while (j
< term
->bidi_cache_size
) {
4486 term
->pre_bidi_cache
[j
].chars
=
4487 term
->post_bidi_cache
[j
].chars
= NULL
;
4488 term
->pre_bidi_cache
[j
].width
=
4489 term
->post_bidi_cache
[j
].width
= -1;
4490 term
->pre_bidi_cache
[j
].forward
=
4491 term
->post_bidi_cache
[j
].forward
= NULL
;
4492 term
->pre_bidi_cache
[j
].backward
=
4493 term
->post_bidi_cache
[j
].backward
= NULL
;
4498 sfree(term
->pre_bidi_cache
[line
].chars
);
4499 sfree(term
->post_bidi_cache
[line
].chars
);
4500 sfree(term
->post_bidi_cache
[line
].forward
);
4501 sfree(term
->post_bidi_cache
[line
].backward
);
4503 term
->pre_bidi_cache
[line
].width
= width
;
4504 term
->pre_bidi_cache
[line
].chars
= snewn(size
, termchar
);
4505 term
->post_bidi_cache
[line
].width
= width
;
4506 term
->post_bidi_cache
[line
].chars
= snewn(size
, termchar
);
4507 term
->post_bidi_cache
[line
].forward
= snewn(width
, int);
4508 term
->post_bidi_cache
[line
].backward
= snewn(width
, int);
4510 memcpy(term
->pre_bidi_cache
[line
].chars
, lbefore
, size
* TSIZE
);
4511 memcpy(term
->post_bidi_cache
[line
].chars
, lafter
, size
* TSIZE
);
4512 memset(term
->post_bidi_cache
[line
].forward
, 0, width
* sizeof(int));
4513 memset(term
->post_bidi_cache
[line
].backward
, 0, width
* sizeof(int));
4515 for (i
= 0; i
< width
; i
++) {
4516 int p
= wcTo
[i
].index
;
4518 assert(0 <= p
&& p
< width
);
4520 term
->post_bidi_cache
[line
].backward
[i
] = p
;
4521 term
->post_bidi_cache
[line
].forward
[p
] = i
;
4526 * Prepare the bidi information for a screen line. Returns the
4527 * transformed list of termchars, or NULL if no transformation at
4528 * all took place (because bidi is disabled). If return was
4529 * non-NULL, auxiliary information such as the forward and reverse
4530 * mappings of permutation position are available in
4531 * term->post_bidi_cache[scr_y].*.
4533 static termchar
*term_bidi_line(Terminal
*term
, struct termline
*ldata
,
4539 /* Do Arabic shaping and bidi. */
4540 if(!term
->cfg
.bidi
|| !term
->cfg
.arabicshaping
) {
4542 if (!term_bidi_cache_hit(term
, scr_y
, ldata
->chars
, term
->cols
)) {
4544 if (term
->wcFromTo_size
< term
->cols
) {
4545 term
->wcFromTo_size
= term
->cols
;
4546 term
->wcFrom
= sresize(term
->wcFrom
, term
->wcFromTo_size
,
4548 term
->wcTo
= sresize(term
->wcTo
, term
->wcFromTo_size
,
4552 for(it
=0; it
<term
->cols
; it
++)
4554 unsigned long uc
= (ldata
->chars
[it
].chr
);
4556 switch (uc
& CSET_MASK
) {
4558 if (!term
->cfg
.rawcnp
) {
4559 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
4563 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
4566 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
4569 switch (uc
& CSET_MASK
) {
4571 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
4574 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
4578 term
->wcFrom
[it
].origwc
= term
->wcFrom
[it
].wc
=
4580 term
->wcFrom
[it
].index
= it
;
4584 do_bidi(term
->wcFrom
, term
->cols
);
4586 /* this is saved iff done from inside the shaping */
4587 if(!term
->cfg
.bidi
&& term
->cfg
.arabicshaping
)
4588 for(it
=0; it
<term
->cols
; it
++)
4589 term
->wcTo
[it
] = term
->wcFrom
[it
];
4591 if(!term
->cfg
.arabicshaping
)
4592 do_shape(term
->wcFrom
, term
->wcTo
, term
->cols
);
4594 if (term
->ltemp_size
< ldata
->size
) {
4595 term
->ltemp_size
= ldata
->size
;
4596 term
->ltemp
= sresize(term
->ltemp
, term
->ltemp_size
,
4600 memcpy(term
->ltemp
, ldata
->chars
, ldata
->size
* TSIZE
);
4602 for(it
=0; it
<term
->cols
; it
++)
4604 term
->ltemp
[it
] = ldata
->chars
[term
->wcTo
[it
].index
];
4605 if (term
->ltemp
[it
].cc_next
)
4606 term
->ltemp
[it
].cc_next
-=
4607 it
- term
->wcTo
[it
].index
;
4609 if (term
->wcTo
[it
].origwc
!= term
->wcTo
[it
].wc
)
4610 term
->ltemp
[it
].chr
= term
->wcTo
[it
].wc
;
4612 term_bidi_cache_store(term
, scr_y
, ldata
->chars
,
4613 term
->ltemp
, term
->wcTo
,
4614 term
->cols
, ldata
->size
);
4616 lchars
= term
->ltemp
;
4618 lchars
= term
->post_bidi_cache
[scr_y
].chars
;
4628 * Given a context, update the window. Out of paranoia, we don't
4629 * allow WM_PAINT responses to do scrolling optimisations.
4631 static void do_paint(Terminal
*term
, Context ctx
, int may_optimise
)
4633 int i
, j
, our_curs_y
, our_curs_x
;
4638 #ifdef OPTIMISE_SCROLL
4639 struct scrollregion
*sr
;
4640 #endif /* OPTIMISE_SCROLL */
4644 ch
= snewn(chlen
, wchar_t);
4646 newline
= snewn(term
->cols
, termchar
);
4648 rv
= (!term
->rvideo
^ !term
->in_vbell ? ATTR_REVERSE
: 0);
4651 * screen array, disptop, scrtop,
4653 * cfg.blinkpc, blink_is_real, tblinker,
4654 * curs.y, curs.x, cblinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
4657 /* Has the cursor position or type changed ? */
4658 if (term
->cursor_on
) {
4659 if (term
->has_focus
) {
4660 if (term
->cblinker
|| !term
->cfg
.blink_cur
)
4661 cursor
= TATTR_ACTCURS
;
4665 cursor
= TATTR_PASCURS
;
4667 cursor
|= TATTR_RIGHTCURS
;
4670 our_curs_y
= term
->curs
.y
- term
->disptop
;
4673 * Adjust the cursor position:
4675 * - in the case where it's resting on the right-hand half
4676 * of a CJK wide character. xterm's behaviour here,
4677 * which seems adequate to me, is to display the cursor
4678 * covering the _whole_ character, exactly as if it were
4679 * one space to the left.
4681 termline
*ldata
= lineptr(term
->curs
.y
);
4684 our_curs_x
= term
->curs
.x
;
4686 if ( (lchars
= term_bidi_line(term
, ldata
, our_curs_y
)) != NULL
) {
4687 our_curs_x
= term
->post_bidi_cache
[our_curs_y
].forward
[our_curs_x
];
4689 lchars
= ldata
->chars
;
4691 if (our_curs_x
> 0 &&
4692 lchars
[our_curs_x
].chr
== UCSWIDE
)
4699 * If the cursor is not where it was last time we painted, and
4700 * its previous position is visible on screen, invalidate its
4701 * previous position.
4703 if (term
->dispcursy
>= 0 &&
4704 (term
->curstype
!= cursor
||
4705 term
->dispcursy
!= our_curs_y
||
4706 term
->dispcursx
!= our_curs_x
)) {
4707 termchar
*dispcurs
= term
->disptext
[term
->dispcursy
]->chars
+
4710 if (term
->dispcursx
> 0 && dispcurs
->chr
== UCSWIDE
)
4711 dispcurs
[-1].attr
|= ATTR_INVALID
;
4712 if (term
->dispcursx
< term
->cols
-1 && dispcurs
[1].chr
== UCSWIDE
)
4713 dispcurs
[1].attr
|= ATTR_INVALID
;
4714 dispcurs
->attr
|= ATTR_INVALID
;
4718 term
->dispcursx
= term
->dispcursy
= -1;
4720 #ifdef OPTIMISE_SCROLL
4722 sr
= term
->scrollhead
;
4724 struct scrollregion
*next
= sr
->next
;
4725 do_scroll(ctx
, sr
->topline
, sr
->botline
, sr
->lines
);
4729 term
->scrollhead
= term
->scrolltail
= NULL
;
4730 #endif /* OPTIMISE_SCROLL */
4732 /* The normal screen data */
4733 for (i
= 0; i
< term
->rows
; i
++) {
4736 int dirty_line
, dirty_run
, selected
;
4737 unsigned long attr
= 0, cset
= 0;
4738 int updated_line
= 0;
4741 int last_run_dirty
= 0;
4742 int laststart
, dirtyrect
;
4745 scrpos
.y
= i
+ term
->disptop
;
4746 ldata
= lineptr(scrpos
.y
);
4748 /* Do Arabic shaping and bidi. */
4749 lchars
= term_bidi_line(term
, ldata
, i
);
4751 backward
= term
->post_bidi_cache
[i
].backward
;
4753 lchars
= ldata
->chars
;
4758 * First loop: work along the line deciding what we want
4759 * each character cell to look like.
4761 for (j
= 0; j
< term
->cols
; j
++) {
4762 unsigned long tattr
, tchar
;
4763 termchar
*d
= lchars
+ j
;
4764 scrpos
.x
= backward ? backward
[j
] : j
;
4769 if (!term
->cfg
.ansi_colour
)
4770 tattr
= (tattr
& ~(ATTR_FGMASK
| ATTR_BGMASK
)) |
4771 ATTR_DEFFG
| ATTR_DEFBG
;
4773 if (!term
->cfg
.xterm_256_colour
) {
4775 colour
= (tattr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
;
4776 if (colour
>= 16 && colour
< 256)
4777 tattr
= (tattr
&~ ATTR_FGMASK
) | ATTR_DEFFG
;
4778 colour
= (tattr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
;
4779 if (colour
>= 16 && colour
< 256)
4780 tattr
= (tattr
&~ ATTR_BGMASK
) | ATTR_DEFBG
;
4783 switch (tchar
& CSET_MASK
) {
4785 tchar
= term
->ucsdata
->unitab_line
[tchar
& 0xFF];
4788 tchar
= term
->ucsdata
->unitab_xterm
[tchar
& 0xFF];
4791 tchar
= term
->ucsdata
->unitab_scoacs
[tchar
&0xFF];
4794 if (j
< term
->cols
-1 && d
[1].chr
== UCSWIDE
)
4797 /* Video reversing things */
4798 if (term
->selstate
== DRAGGING
|| term
->selstate
== SELECTED
) {
4799 if (term
->seltype
== LEXICOGRAPHIC
)
4800 selected
= (posle(term
->selstart
, scrpos
) &&
4801 poslt(scrpos
, term
->selend
));
4803 selected
= (posPle(term
->selstart
, scrpos
) &&
4804 posPlt(scrpos
, term
->selend
));
4808 ^ (selected ? ATTR_REVERSE
: 0));
4810 /* 'Real' blinking ? */
4811 if (term
->blink_is_real
&& (tattr
& ATTR_BLINK
)) {
4812 if (term
->has_focus
&& term
->tblinker
) {
4813 tchar
= term
->ucsdata
->unitab_line
[(unsigned char)' '];
4815 tattr
&= ~ATTR_BLINK
;
4819 * Check the font we'll _probably_ be using to see if
4820 * the character is wide when we don't want it to be.
4822 if (tchar
!= term
->disptext
[i
]->chars
[j
].chr
||
4823 tattr
!= (term
->disptext
[i
]->chars
[j
].attr
&~
4824 (ATTR_NARROW
| DATTR_MASK
))) {
4825 if ((tattr
& ATTR_WIDE
) == 0 && char_width(ctx
, tchar
) == 2)
4826 tattr
|= ATTR_NARROW
;
4827 } else if (term
->disptext
[i
]->chars
[j
].attr
& ATTR_NARROW
)
4828 tattr
|= ATTR_NARROW
;
4830 if (i
== our_curs_y
&& j
== our_curs_x
) {
4832 term
->curstype
= cursor
;
4833 term
->dispcursx
= j
;
4834 term
->dispcursy
= i
;
4838 newline
[j
].attr
= tattr
;
4839 newline
[j
].chr
= tchar
;
4840 /* Combining characters are still read from lchars */
4841 newline
[j
].cc_next
= 0;
4845 * Now loop over the line again, noting where things have
4848 * During this loop, we keep track of where we last saw
4849 * DATTR_STARTRUN. Any mismatch automatically invalidates
4850 * _all_ of the containing run that was last printed: that
4851 * is, any rectangle that was drawn in one go in the
4852 * previous update should be either left completely alone
4853 * or overwritten in its entirety. This, along with the
4854 * expectation that front ends clip all text runs to their
4855 * bounding rectangle, should solve any possible problems
4856 * with fonts that overflow their character cells.
4860 for (j
= 0; j
< term
->cols
; j
++) {
4861 if (term
->disptext
[i
]->chars
[j
].attr
& DATTR_STARTRUN
) {
4866 if (term
->disptext
[i
]->chars
[j
].chr
!= newline
[j
].chr
||
4867 (term
->disptext
[i
]->chars
[j
].attr
&~ DATTR_MASK
)
4868 != newline
[j
].attr
) {
4872 for (k
= laststart
; k
< j
; k
++)
4873 term
->disptext
[i
]->chars
[k
].attr
|= ATTR_INVALID
;
4880 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
4884 * Finally, loop once more and actually do the drawing.
4886 dirty_run
= dirty_line
= (ldata
->lattr
!=
4887 term
->disptext
[i
]->lattr
);
4888 term
->disptext
[i
]->lattr
= ldata
->lattr
;
4890 for (j
= 0; j
< term
->cols
; j
++) {
4891 unsigned long tattr
, tchar
;
4892 int break_run
, do_copy
;
4893 termchar
*d
= lchars
+ j
;
4895 tattr
= newline
[j
].attr
;
4896 tchar
= newline
[j
].chr
;
4898 if ((term
->disptext
[i
]->chars
[j
].attr
^ tattr
) & ATTR_WIDE
)
4901 break_run
= ((tattr
^ attr
) & term
->attr_mask
) != 0;
4903 /* Special hack for VT100 Linedraw glyphs */
4904 if (tchar
>= 0x23BA && tchar
<= 0x23BD)
4908 * Separate out sequences of characters that have the
4909 * same CSET, if that CSET is a magic one.
4911 if (CSET_OF(tchar
) != cset
)
4915 * Break on both sides of any combined-character cell.
4917 if (d
->cc_next
!= 0 ||
4918 (j
> 0 && d
[-1].cc_next
!= 0))
4921 if (!term
->ucsdata
->dbcs_screenfont
&& !dirty_line
) {
4922 if (term
->disptext
[i
]->chars
[j
].chr
== tchar
&&
4923 (term
->disptext
[i
]->chars
[j
].attr
&~ DATTR_MASK
) == tattr
)
4925 else if (!dirty_run
&& ccount
== 1)
4930 if ((dirty_run
|| last_run_dirty
) && ccount
> 0) {
4931 do_text(ctx
, start
, i
, ch
, ccount
, attr
,
4933 if (attr
& (TATTR_ACTCURS
| TATTR_PASCURS
))
4934 do_cursor(ctx
, start
, i
, ch
, ccount
, attr
,
4942 cset
= CSET_OF(tchar
);
4943 if (term
->ucsdata
->dbcs_screenfont
)
4944 last_run_dirty
= dirty_run
;
4945 dirty_run
= dirty_line
;
4949 if (!termchars_equal_override(&term
->disptext
[i
]->chars
[j
],
4955 if (ccount
>= chlen
) {
4956 chlen
= ccount
+ 256;
4957 ch
= sresize(ch
, chlen
, wchar_t);
4959 ch
[ccount
++] = (wchar_t) tchar
;
4964 while (dd
->cc_next
) {
4965 unsigned long schar
;
4970 switch (schar
& CSET_MASK
) {
4972 schar
= term
->ucsdata
->unitab_line
[schar
& 0xFF];
4975 schar
= term
->ucsdata
->unitab_xterm
[schar
& 0xFF];
4978 schar
= term
->ucsdata
->unitab_scoacs
[schar
&0xFF];
4982 if (ccount
>= chlen
) {
4983 chlen
= ccount
+ 256;
4984 ch
= sresize(ch
, chlen
, wchar_t);
4986 ch
[ccount
++] = (wchar_t) schar
;
4989 attr
|= TATTR_COMBINING
;
4993 copy_termchar(term
->disptext
[i
], j
, d
);
4994 term
->disptext
[i
]->chars
[j
].chr
= tchar
;
4995 term
->disptext
[i
]->chars
[j
].attr
= tattr
;
4997 term
->disptext
[i
]->chars
[j
].attr
|= DATTR_STARTRUN
;
5000 /* If it's a wide char step along to the next one. */
5001 if (tattr
& ATTR_WIDE
) {
5002 if (++j
< term
->cols
) {
5005 * By construction above, the cursor should not
5006 * be on the right-hand half of this character.
5009 assert(!(i
== our_curs_y
&& j
== our_curs_x
));
5010 if (!termchars_equal(&term
->disptext
[i
]->chars
[j
], d
))
5012 copy_termchar(term
->disptext
[i
], j
, d
);
5016 if (dirty_run
&& ccount
> 0) {
5017 do_text(ctx
, start
, i
, ch
, ccount
, attr
,
5019 if (attr
& (TATTR_ACTCURS
| TATTR_PASCURS
))
5020 do_cursor(ctx
, start
, i
, ch
, ccount
, attr
,
5034 * Invalidate the whole screen so it will be repainted in full.
5036 void term_invalidate(Terminal
*term
)
5040 for (i
= 0; i
< term
->rows
; i
++)
5041 for (j
= 0; j
< term
->cols
; j
++)
5042 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
5044 term_schedule_update(term
);
5048 * Paint the window in response to a WM_PAINT message.
5050 void term_paint(Terminal
*term
, Context ctx
,
5051 int left
, int top
, int right
, int bottom
, int immediately
)
5054 if (left
< 0) left
= 0;
5055 if (top
< 0) top
= 0;
5056 if (right
>= term
->cols
) right
= term
->cols
-1;
5057 if (bottom
>= term
->rows
) bottom
= term
->rows
-1;
5059 for (i
= top
; i
<= bottom
&& i
< term
->rows
; i
++) {
5060 if ((term
->disptext
[i
]->lattr
& LATTR_MODE
) == LATTR_NORM
)
5061 for (j
= left
; j
<= right
&& j
< term
->cols
; j
++)
5062 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
5064 for (j
= left
/ 2; j
<= right
/ 2 + 1 && j
< term
->cols
; j
++)
5065 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
5069 do_paint (term
, ctx
, FALSE
);
5071 term_schedule_update(term
);
5076 * Attempt to scroll the scrollback. The second parameter gives the
5077 * position we want to scroll to; the first is +1 to denote that
5078 * this position is relative to the beginning of the scrollback, -1
5079 * to denote it is relative to the end, and 0 to denote that it is
5080 * relative to the current position.
5082 void term_scroll(Terminal
*term
, int rel
, int where
)
5084 int sbtop
= -sblines(term
);
5085 #ifdef OPTIMISE_SCROLL
5086 int olddisptop
= term
->disptop
;
5088 #endif /* OPTIMISE_SCROLL */
5090 term
->disptop
= (rel
< 0 ?
0 : rel
> 0 ? sbtop
: term
->disptop
) + where
;
5091 if (term
->disptop
< sbtop
)
5092 term
->disptop
= sbtop
;
5093 if (term
->disptop
> 0)
5096 #ifdef OPTIMISE_SCROLL
5097 shift
= (term
->disptop
- olddisptop
);
5098 if (shift
< term
->rows
&& shift
> -term
->rows
)
5099 scroll_display(term
, 0, term
->rows
- 1, shift
);
5100 #endif /* OPTIMISE_SCROLL */
5105 * Helper routine for clipme(): growing buffer.
5108 int buflen
; /* amount of allocated space in textbuf/attrbuf */
5109 int bufpos
; /* amount of actual data */
5110 wchar_t *textbuf
; /* buffer for copied text */
5111 wchar_t *textptr
; /* = textbuf + bufpos (current insertion point) */
5112 int *attrbuf
; /* buffer for copied attributes */
5113 int *attrptr
; /* = attrbuf + bufpos */
5116 static void clip_addchar(clip_workbuf
*b
, wchar_t chr
, int attr
)
5118 if (b
->bufpos
>= b
->buflen
) {
5120 b
->textbuf
= sresize(b
->textbuf
, b
->buflen
, wchar_t);
5121 b
->textptr
= b
->textbuf
+ b
->bufpos
;
5122 b
->attrbuf
= sresize(b
->attrbuf
, b
->buflen
, int);
5123 b
->attrptr
= b
->attrbuf
+ b
->bufpos
;
5125 *b
->textptr
++ = chr
;
5126 *b
->attrptr
++ = attr
;
5130 static void clipme(Terminal
*term
, pos top
, pos bottom
, int rect
, int desel
)
5138 buf
.textptr
= buf
.textbuf
= snewn(buf
.buflen
, wchar_t);
5139 buf
.attrptr
= buf
.attrbuf
= snewn(buf
.buflen
, int);
5141 old_top_x
= top
.x
; /* needed for rect==1 */
5143 while (poslt(top
, bottom
)) {
5145 termline
*ldata
= lineptr(top
.y
);
5149 * nlpos will point at the maximum position on this line we
5150 * should copy up to. So we start it at the end of the
5154 nlpos
.x
= term
->cols
;
5157 * ... move it backwards if there's unused space at the end
5158 * of the line (and also set `nl' if this is the case,
5159 * because in normal selection mode this means we need a
5160 * newline at the end)...
5162 if (!(ldata
->lattr
& LATTR_WRAPPED
)) {
5164 IS_SPACE_CHR(ldata
->chars
[nlpos
.x
- 1].chr
) &&
5165 !ldata
->chars
[nlpos
.x
- 1].cc_next
&&
5168 if (poslt(nlpos
, bottom
))
5170 } else if (ldata
->lattr
& LATTR_WRAPPED2
) {
5171 /* Ignore the last char on the line in a WRAPPED2 line. */
5176 * ... and then clip it to the terminal x coordinate if
5177 * we're doing rectangular selection. (In this case we
5178 * still did the above, so that copying e.g. the right-hand
5179 * column from a table doesn't fill with spaces on the
5183 if (nlpos
.x
> bottom
.x
)
5185 nl
= (top
.y
< bottom
.y
);
5188 while (poslt(top
, bottom
) && poslt(top
, nlpos
)) {
5191 sprintf(cbuf
, "<U+%04x>", (ldata
[top
.x
] & 0xFFFF));
5193 wchar_t cbuf
[16], *p
;
5197 if (ldata
->chars
[x
].chr
== UCSWIDE
) {
5203 int uc
= ldata
->chars
[x
].chr
;
5204 attr
= ldata
->chars
[x
].attr
;
5206 switch (uc
& CSET_MASK
) {
5208 if (!term
->cfg
.rawcnp
) {
5209 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
5213 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
5216 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
5219 switch (uc
& CSET_MASK
) {
5221 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
5224 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
5228 set
= (uc
& CSET_MASK
);
5229 c
= (uc
& ~CSET_MASK
);
5233 if (DIRECT_FONT(uc
)) {
5234 if (c
>= ' ' && c
!= 0x7F) {
5238 if (is_dbcs_leadbyte(term
->ucsdata
->font_codepage
, (BYTE
) c
)) {
5240 buf
[1] = (char) (0xFF & ldata
->chars
[top
.x
+ 1].chr
);
5241 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 2, wbuf
, 4);
5245 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 1, wbuf
, 4);
5249 memcpy(cbuf
, wbuf
, rv
* sizeof(wchar_t));
5256 for (p
= cbuf
; *p
; p
++)
5257 clip_addchar(&buf
, *p
, attr
);
5259 if (ldata
->chars
[x
].cc_next
)
5260 x
+= ldata
->chars
[x
].cc_next
;
5268 for (i
= 0; i
< sel_nl_sz
; i
++)
5269 clip_addchar(&buf
, sel_nl
[i
], 0);
5272 top
.x
= rect ? old_top_x
: 0;
5276 #if SELECTION_NUL_TERMINATED
5277 clip_addchar(&buf
, 0, 0);
5279 /* Finally, transfer all that to the clipboard. */
5280 write_clip(term
->frontend
, buf
.textbuf
, buf
.attrbuf
, buf
.bufpos
, desel
);
5285 void term_copyall(Terminal
*term
)
5289 tree234
*screen
= term
->screen
;
5290 top
.y
= -sblines(term
);
5292 bottom
.y
= find_last_nonempty_line(term
, screen
);
5293 bottom
.x
= term
->cols
;
5294 clipme(term
, top
, bottom
, 0, TRUE
);
5298 * The wordness array is mainly for deciding the disposition of the
5299 * US-ASCII characters.
5301 static int wordtype(Terminal
*term
, int uc
)
5304 int start
, end
, ctype
;
5306 static const struct ucsword ucs_words
[] = {
5312 0x037e, 0x037e, 1}, /* Greek question mark */
5314 0x0387, 0x0387, 1}, /* Greek ano teleia */
5316 0x055a, 0x055f, 1}, /* Armenian punctuation */
5318 0x0589, 0x0589, 1}, /* Armenian full stop */
5320 0x0700, 0x070d, 1}, /* Syriac punctuation */
5322 0x104a, 0x104f, 1}, /* Myanmar punctuation */
5324 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
5326 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
5328 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
5330 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
5332 0x1800, 0x180a, 1}, /* Mongolian punctuation */
5334 0x2000, 0x200a, 0}, /* Various spaces */
5336 0x2070, 0x207f, 2}, /* superscript */
5338 0x2080, 0x208f, 2}, /* subscript */
5340 0x200b, 0x27ff, 1}, /* punctuation and symbols */
5342 0x3000, 0x3000, 0}, /* ideographic space */
5344 0x3001, 0x3020, 1}, /* ideographic punctuation */
5346 0x303f, 0x309f, 3}, /* Hiragana */
5348 0x30a0, 0x30ff, 3}, /* Katakana */
5350 0x3300, 0x9fff, 3}, /* CJK Ideographs */
5352 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
5354 0xf900, 0xfaff, 3}, /* CJK Ideographs */
5356 0xfe30, 0xfe6b, 1}, /* punctuation forms */
5358 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
5360 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
5362 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
5364 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
5366 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
5370 const struct ucsword
*wptr
;
5372 switch (uc
& CSET_MASK
) {
5374 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
5377 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
5380 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
5383 switch (uc
& CSET_MASK
) {
5385 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
5388 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
5392 /* For DBCS fonts I can't do anything useful. Even this will sometimes
5393 * fail as there's such a thing as a double width space. :-(
5395 if (term
->ucsdata
->dbcs_screenfont
&&
5396 term
->ucsdata
->font_codepage
== term
->ucsdata
->line_codepage
)
5400 return term
->wordness
[uc
];
5402 for (wptr
= ucs_words
; wptr
->start
; wptr
++) {
5403 if (uc
>= wptr
->start
&& uc
<= wptr
->end
)
5411 * Spread the selection outwards according to the selection mode.
5413 static pos
sel_spread_half(Terminal
*term
, pos p
, int dir
)
5417 int topy
= -sblines(term
);
5419 ldata
= lineptr(p
.y
);
5421 switch (term
->selmode
) {
5424 * In this mode, every character is a separate unit, except
5425 * for runs of spaces at the end of a non-wrapping line.
5427 if (!(ldata
->lattr
& LATTR_WRAPPED
)) {
5428 termchar
*q
= ldata
->chars
+ term
->cols
;
5429 while (q
> ldata
->chars
&&
5430 IS_SPACE_CHR(q
[-1].chr
) && !q
[-1].cc_next
)
5432 if (q
== ldata
->chars
+ term
->cols
)
5434 if (p
.x
>= q
- ldata
->chars
)
5435 p
.x
= (dir
== -1 ? q
- ldata
->chars
: term
->cols
- 1);
5440 * In this mode, the units are maximal runs of characters
5441 * whose `wordness' has the same value.
5443 wvalue
= wordtype(term
, UCSGET(ldata
->chars
, p
.x
));
5446 int maxcols
= (ldata
->lattr
& LATTR_WRAPPED2 ?
5447 term
->cols
-1 : term
->cols
);
5448 if (p
.x
< maxcols
-1) {
5449 if (wordtype(term
, UCSGET(ldata
->chars
, p
.x
+1)) == wvalue
)
5454 if (ldata
->lattr
& LATTR_WRAPPED
) {
5456 ldata2
= lineptr(p
.y
+1);
5457 if (wordtype(term
, UCSGET(ldata2
->chars
, 0))
5474 if (wordtype(term
, UCSGET(ldata
->chars
, p
.x
-1)) == wvalue
)
5483 ldata2
= lineptr(p
.y
-1);
5484 maxcols
= (ldata2
->lattr
& LATTR_WRAPPED2 ?
5485 term
->cols
-1 : term
->cols
);
5486 if (ldata2
->lattr
& LATTR_WRAPPED
) {
5487 if (wordtype(term
, UCSGET(ldata2
->chars
, maxcols
-1))
5505 * In this mode, every line is a unit.
5507 p
.x
= (dir
== -1 ?
0 : term
->cols
- 1);
5515 static void sel_spread(Terminal
*term
)
5517 if (term
->seltype
== LEXICOGRAPHIC
) {
5518 term
->selstart
= sel_spread_half(term
, term
->selstart
, -1);
5519 decpos(term
->selend
);
5520 term
->selend
= sel_spread_half(term
, term
->selend
, +1);
5521 incpos(term
->selend
);
5525 void term_do_paste(Terminal
*term
)
5530 get_clip(term
->frontend
, &data
, &len
);
5531 if (data
&& len
> 0) {
5534 term_seen_key_event(term
); /* pasted data counts */
5536 if (term
->paste_buffer
)
5537 sfree(term
->paste_buffer
);
5538 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
5539 term
->paste_buffer
= snewn(len
, wchar_t);
5542 while (p
< data
+ len
) {
5543 while (p
< data
+ len
&&
5544 !(p
<= data
+ len
- sel_nl_sz
&&
5545 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
5550 for (i
= 0; i
< p
- q
; i
++) {
5551 term
->paste_buffer
[term
->paste_len
++] = q
[i
];
5555 if (p
<= data
+ len
- sel_nl_sz
&&
5556 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
5557 term
->paste_buffer
[term
->paste_len
++] = '\015';
5563 /* Assume a small paste will be OK in one go. */
5564 if (term
->paste_len
< 256) {
5566 luni_send(term
->ldisc
, term
->paste_buffer
, term
->paste_len
, 0);
5567 if (term
->paste_buffer
)
5568 sfree(term
->paste_buffer
);
5569 term
->paste_buffer
= 0;
5570 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
5573 get_clip(term
->frontend
, NULL
, NULL
);
5576 void term_mouse(Terminal
*term
, Mouse_Button braw
, Mouse_Button bcooked
,
5577 Mouse_Action a
, int x
, int y
, int shift
, int ctrl
, int alt
)
5581 int raw_mouse
= (term
->xterm_mouse
&&
5582 !term
->cfg
.no_mouse_rep
&&
5583 !(term
->cfg
.mouse_override
&& shift
));
5584 int default_seltype
;
5588 if (a
== MA_DRAG
&& !raw_mouse
)
5589 term_scroll(term
, 0, -1);
5591 if (y
>= term
->rows
) {
5593 if (a
== MA_DRAG
&& !raw_mouse
)
5594 term_scroll(term
, 0, +1);
5603 if (x
>= term
->cols
)
5606 selpoint
.y
= y
+ term
->disptop
;
5607 ldata
= lineptr(selpoint
.y
);
5609 if ((ldata
->lattr
& LATTR_MODE
) != LATTR_NORM
)
5613 * Transform x through the bidi algorithm to find the _logical_
5614 * click point from the physical one.
5616 if (term_bidi_line(term
, ldata
, y
) != NULL
) {
5617 x
= term
->post_bidi_cache
[y
].backward
[x
];
5624 * If we're in the middle of a selection operation, we ignore raw
5625 * mouse mode until it's done (we must have been not in raw mouse
5626 * mode when it started).
5627 * This makes use of Shift for selection reliable, and avoids the
5628 * host seeing mouse releases for which they never saw corresponding
5632 (term
->selstate
!= ABOUT_TO
) && (term
->selstate
!= DRAGGING
)) {
5633 int encstate
= 0, r
, c
;
5640 encstate
= 0x20; /* left button down */
5651 case MBT_WHEEL_DOWN
:
5654 default: break; /* placate gcc warning about enum use */
5658 if (term
->xterm_mouse
== 1)
5664 term
->mouse_is_down
= 0;
5667 if (term
->mouse_is_down
== braw
)
5669 term
->mouse_is_down
= braw
;
5671 default: break; /* placate gcc warning about enum use */
5680 sprintf(abuf
, "\033[M%c%c%c", encstate
, c
, r
);
5681 ldisc_send(term
->ldisc
, abuf
, 6, 0);
5687 * Set the selection type (rectangular or normal) at the start
5688 * of a selection attempt, from the state of Alt.
5690 if (!alt
^ !term
->cfg
.rect_select
)
5691 default_seltype
= RECTANGULAR
;
5693 default_seltype
= LEXICOGRAPHIC
;
5695 if (term
->selstate
== NO_SELECTION
) {
5696 term
->seltype
= default_seltype
;
5699 if (bcooked
== MBT_SELECT
&& a
== MA_CLICK
) {
5701 term
->selstate
= ABOUT_TO
;
5702 term
->seltype
= default_seltype
;
5703 term
->selanchor
= selpoint
;
5704 term
->selmode
= SM_CHAR
;
5705 } else if (bcooked
== MBT_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
5707 term
->selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
5708 term
->selstate
= DRAGGING
;
5709 term
->selstart
= term
->selanchor
= selpoint
;
5710 term
->selend
= term
->selstart
;
5711 incpos(term
->selend
);
5713 } else if ((bcooked
== MBT_SELECT
&& a
== MA_DRAG
) ||
5714 (bcooked
== MBT_EXTEND
&& a
!= MA_RELEASE
)) {
5715 if (term
->selstate
== ABOUT_TO
&& poseq(term
->selanchor
, selpoint
))
5717 if (bcooked
== MBT_EXTEND
&& a
!= MA_DRAG
&&
5718 term
->selstate
== SELECTED
) {
5719 if (term
->seltype
== LEXICOGRAPHIC
) {
5721 * For normal selection, we extend by moving
5722 * whichever end of the current selection is closer
5725 if (posdiff(selpoint
, term
->selstart
) <
5726 posdiff(term
->selend
, term
->selstart
) / 2) {
5727 term
->selanchor
= term
->selend
;
5728 decpos(term
->selanchor
);
5730 term
->selanchor
= term
->selstart
;
5734 * For rectangular selection, we have a choice of
5735 * _four_ places to put selanchor and selpoint: the
5736 * four corners of the selection.
5738 if (2*selpoint
.x
< term
->selstart
.x
+ term
->selend
.x
)
5739 term
->selanchor
.x
= term
->selend
.x
-1;
5741 term
->selanchor
.x
= term
->selstart
.x
;
5743 if (2*selpoint
.y
< term
->selstart
.y
+ term
->selend
.y
)
5744 term
->selanchor
.y
= term
->selend
.y
;
5746 term
->selanchor
.y
= term
->selstart
.y
;
5748 term
->selstate
= DRAGGING
;
5750 if (term
->selstate
!= ABOUT_TO
&& term
->selstate
!= DRAGGING
)
5751 term
->selanchor
= selpoint
;
5752 term
->selstate
= DRAGGING
;
5753 if (term
->seltype
== LEXICOGRAPHIC
) {
5755 * For normal selection, we set (selstart,selend) to
5756 * (selpoint,selanchor) in some order.
5758 if (poslt(selpoint
, term
->selanchor
)) {
5759 term
->selstart
= selpoint
;
5760 term
->selend
= term
->selanchor
;
5761 incpos(term
->selend
);
5763 term
->selstart
= term
->selanchor
;
5764 term
->selend
= selpoint
;
5765 incpos(term
->selend
);
5769 * For rectangular selection, we may need to
5770 * interchange x and y coordinates (if the user has
5771 * dragged in the -x and +y directions, or vice versa).
5773 term
->selstart
.x
= min(term
->selanchor
.x
, selpoint
.x
);
5774 term
->selend
.x
= 1+max(term
->selanchor
.x
, selpoint
.x
);
5775 term
->selstart
.y
= min(term
->selanchor
.y
, selpoint
.y
);
5776 term
->selend
.y
= max(term
->selanchor
.y
, selpoint
.y
);
5779 } else if ((bcooked
== MBT_SELECT
|| bcooked
== MBT_EXTEND
) &&
5781 if (term
->selstate
== DRAGGING
) {
5783 * We've completed a selection. We now transfer the
5784 * data to the clipboard.
5786 clipme(term
, term
->selstart
, term
->selend
,
5787 (term
->seltype
== RECTANGULAR
), FALSE
);
5788 term
->selstate
= SELECTED
;
5790 term
->selstate
= NO_SELECTION
;
5791 } else if (bcooked
== MBT_PASTE
5793 #if MULTICLICK_ONLY_EVENT
5794 || a
== MA_2CLK
|| a
== MA_3CLK
5797 request_paste(term
->frontend
);
5803 void term_key(Terminal
*term
, Key_Sym keysym
, wchar_t *text
, size_t tlen
,
5804 unsigned int modifiers
, unsigned int flags
)
5808 int prependesc
= FALSE
;
5812 fprintf(stderr
, "keysym = %d, %d chars:", keysym
, tlen
);
5813 for (i
= 0; i
< tlen
; i
++)
5814 fprintf(stderr
, " %04x", (unsigned)text
[i
]);
5815 fprintf(stderr
, "\n");
5819 if ((flags
& PKF_REPEAT
) && term
->repeat_off
)
5822 /* Currently, Meta always just prefixes everything with ESC. */
5823 if (modifiers
& PKM_META
)
5825 modifiers
&= ~PKM_META
;
5828 * Alt is only used for Alt+keypad, which isn't supported yet, so
5831 modifiers
&= ~PKM_ALT
;
5833 /* Standard local function keys */
5834 switch (modifiers
& (PKM_SHIFT
| PKM_CONTROL
)) {
5836 if (keysym
== PK_PAGEUP
)
5837 /* scroll up one page */;
5838 if (keysym
== PK_PAGEDOWN
)
5839 /* scroll down on page */;
5840 if (keysym
== PK_INSERT
)
5841 term_do_paste(term
);
5844 if (keysym
== PK_PAGEUP
)
5845 /* scroll up one line */;
5846 if (keysym
== PK_PAGEDOWN
)
5847 /* scroll down one line */;
5848 /* Control-Numlock for app-keypad mode switch */
5849 if (keysym
== PK_PF1
)
5850 term
->app_keypad_keys
^= 1;
5854 if (modifiers
& PKM_ALT
) {
5855 /* Alt+F4 (close) */
5856 /* Alt+Return (full screen) */
5857 /* Alt+Space (system menu) */
5860 if (keysym
== PK_NULL
&& (modifiers
& PKM_CONTROL
) && tlen
== 1 &&
5861 text
[0] >= 0x20 && text
[0] <= 0x7e) {
5862 /* ASCII chars + Control */
5863 if ((text
[0] >= 0x40 && text
[0] <= 0x5f) ||
5864 (text
[0] >= 0x61 && text
[0] <= 0x7a))
5868 * Control-2 should return ^@ (0x00), Control-6 should return
5869 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
5870 * the DOS keyboard handling did it, and we have nothing better
5871 * to do with the key combo in question, we'll also map
5872 * Control-Backquote to ^\ (0x1C).
5875 case ' ': text
[0] = 0x00; break;
5876 case '-': text
[0] = 0x1f; break;
5877 case '/': text
[0] = 0x1f; break;
5878 case '2': text
[0] = 0x00; break;
5879 case '3': text
[0] = 0x1b; break;
5880 case '4': text
[0] = 0x1c; break;
5881 case '5': text
[0] = 0x1d; break;
5882 case '6': text
[0] = 0x1e; break;
5883 case '7': text
[0] = 0x1f; break;
5884 case '8': text
[0] = 0x7f; break;
5885 case '`': text
[0] = 0x1c; break;
5890 /* Nethack keypad */
5891 if (term
->cfg
.nethack_keypad
) {
5894 case PK_KP1
: c
= 'b'; break;
5895 case PK_KP2
: c
= 'j'; break;
5896 case PK_KP3
: c
= 'n'; break;
5897 case PK_KP4
: c
= 'h'; break;
5898 case PK_KP5
: c
= '.'; break;
5899 case PK_KP6
: c
= 'l'; break;
5900 case PK_KP7
: c
= 'y'; break;
5901 case PK_KP8
: c
= 'k'; break;
5902 case PK_KP9
: c
= 'u'; break;
5903 default: break; /* else gcc warns `enum value not used' */
5907 if (modifiers
& PKM_CONTROL
)
5909 else if (modifiers
& PKM_SHIFT
)
5917 /* Numeric Keypad */
5918 if (PK_ISKEYPAD(keysym
)) {
5922 * In VT400 mode, PFn always emits an escape sequence. In
5923 * Linux and tilde modes, this only happens in app keypad mode.
5925 if (term
->cfg
.funky_type
== FUNKY_VT400
||
5926 ((term
->cfg
.funky_type
== FUNKY_LINUX
||
5927 term
->cfg
.funky_type
== FUNKY_TILDE
) &&
5928 term
->app_keypad_keys
&& !term
->cfg
.no_applic_k
)) {
5930 case PK_PF1
: xkey
= 'P'; break;
5931 case PK_PF2
: xkey
= 'Q'; break;
5932 case PK_PF3
: xkey
= 'R'; break;
5933 case PK_PF4
: xkey
= 'S'; break;
5934 default: break; /* else gcc warns `enum value not used' */
5937 if (term
->app_keypad_keys
&& !term
->cfg
.no_applic_k
) {
5939 case PK_KP0
: xkey
= 'p'; break;
5940 case PK_KP1
: xkey
= 'q'; break;
5941 case PK_KP2
: xkey
= 'r'; break;
5942 case PK_KP3
: xkey
= 's'; break;
5943 case PK_KP4
: xkey
= 't'; break;
5944 case PK_KP5
: xkey
= 'u'; break;
5945 case PK_KP6
: xkey
= 'v'; break;
5946 case PK_KP7
: xkey
= 'w'; break;
5947 case PK_KP8
: xkey
= 'x'; break;
5948 case PK_KP9
: xkey
= 'y'; break;
5949 case PK_KPDECIMAL
: xkey
= 'n'; break;
5950 case PK_KPENTER
: xkey
= 'M'; break;
5951 default: break; /* else gcc warns `enum value not used' */
5953 if (term
->cfg
.funky_type
== FUNKY_XTERM
&& tlen
> 0) {
5955 * xterm can't see the layout of the keypad, so it has
5956 * to rely on the X keysyms returned by the keys.
5957 * Hence, we look at the strings here, not the PuTTY
5958 * keysyms (which describe the layout).
5962 if (modifiers
& PKM_SHIFT
)
5967 case '/': xkey
= 'o'; break;
5968 case '*': xkey
= 'j'; break;
5969 case '-': xkey
= 'm'; break;
5973 * In all other modes, we try to retain the layout of
5974 * the DEC keypad in application mode.
5978 /* This key covers the '-' and ',' keys on a VT220 */
5979 if (modifiers
& PKM_SHIFT
)
5980 xkey
= 'm'; /* VT220 '-' */
5982 xkey
= 'l'; /* VT220 ',' */
5984 case PK_KPMINUS
: xkey
= 'm'; break;
5985 case PK_KPCOMMA
: xkey
= 'l'; break;
5986 default: break; /* else gcc warns `enum value not used' */
5991 if (term
->vt52_mode
) {
5992 if (xkey
>= 'P' && xkey
<= 'S')
5993 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
5995 p
+= sprintf((char *) p
, "\x1B?%c", xkey
);
5997 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
6000 /* Not in application mode -- treat the number pad as arrow keys? */
6001 if ((flags
& PKF_NUMLOCK
) == 0) {
6003 case PK_KP0
: keysym
= PK_INSERT
; break;
6004 case PK_KP1
: keysym
= PK_END
; break;
6005 case PK_KP2
: keysym
= PK_DOWN
; break;
6006 case PK_KP3
: keysym
= PK_PAGEDOWN
; break;
6007 case PK_KP4
: keysym
= PK_LEFT
; break;
6008 case PK_KP5
: keysym
= PK_REST
; break;
6009 case PK_KP6
: keysym
= PK_RIGHT
; break;
6010 case PK_KP7
: keysym
= PK_HOME
; break;
6011 case PK_KP8
: keysym
= PK_UP
; break;
6012 case PK_KP9
: keysym
= PK_PAGEUP
; break;
6013 default: break; /* else gcc warns `enum value not used' */
6018 /* Miscellaneous keys */
6025 *p
++ = (term
->cfg
.bksp_is_delete ?
0x7F : 0x08);
6026 else if (modifiers
== PKM_SHIFT
)
6027 /* We do the opposite of what is configured */
6028 *p
++ = (term
->cfg
.bksp_is_delete ?
0x08 : 0x7F);
6034 else if (modifiers
== PKM_SHIFT
)
6035 *p
++ = 0x1B, *p
++ = '[', *p
++ = 'Z';
6038 /* XXX window.c has ctrl+shift+space sending 0xa0 */
6040 if (modifiers
== PKM_CONTROL
)
6045 case PK_KPENTER
: /* Odd keypad modes handled above */
6046 if (modifiers
== 0) {
6048 if (term
->cr_lf_return
)
6052 default: break; /* else gcc warns `enum value not used' */
6055 /* SCO function keys and editing keys */
6056 if (term
->cfg
.funky_type
== FUNKY_SCO
) {
6057 if (PK_ISFKEY(keysym
) && keysym
<= PK_F12
) {
6058 static char const codes
[] =
6059 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
6060 int index
= keysym
- PK_F1
;
6062 if (modifiers
& PKM_SHIFT
) index
+= 12;
6063 if (modifiers
& PKM_CONTROL
) index
+= 24;
6064 p
+= sprintf((char *) p
, "\x1B[%c", codes
[index
]);
6067 if (PK_ISEDITING(keysym
)) {
6071 case PK_DELETE
: *p
++ = 0x7f; goto done
;
6072 case PK_HOME
: xkey
= 'H'; break;
6073 case PK_INSERT
: xkey
= 'L'; break;
6074 case PK_END
: xkey
= 'F'; break;
6075 case PK_PAGEUP
: xkey
= 'I'; break;
6076 case PK_PAGEDOWN
: xkey
= 'G'; break;
6077 default: break; /* else gcc warns `enum value not used' */
6079 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
6083 if (PK_ISEDITING(keysym
) && (modifiers
& PKM_SHIFT
) == 0) {
6086 if (term
->cfg
.funky_type
== FUNKY_XTERM
) {
6087 /* Xterm shuffles these keys, apparently. */
6089 case PK_HOME
: keysym
= PK_INSERT
; break;
6090 case PK_INSERT
: keysym
= PK_HOME
; break;
6091 case PK_DELETE
: keysym
= PK_END
; break;
6092 case PK_END
: keysym
= PK_PAGEUP
; break;
6093 case PK_PAGEUP
: keysym
= PK_DELETE
; break;
6094 case PK_PAGEDOWN
: keysym
= PK_PAGEDOWN
; break;
6095 default: break; /* else gcc warns `enum value not used' */
6100 if (term
->cfg
.rxvt_homeend
&&
6101 (keysym
== PK_HOME
|| keysym
== PK_END
)) {
6102 p
+= sprintf((char *) p
, keysym
== PK_HOME ?
"\x1B[H" : "\x1BOw");
6106 if (term
->vt52_mode
) {
6110 * A real VT52 doesn't have these, and a VT220 doesn't
6111 * send anything for them in VT52 mode.
6114 case PK_HOME
: xkey
= 'H'; break;
6115 case PK_INSERT
: xkey
= 'L'; break;
6116 case PK_DELETE
: xkey
= 'M'; break;
6117 case PK_END
: xkey
= 'E'; break;
6118 case PK_PAGEUP
: xkey
= 'I'; break;
6119 case PK_PAGEDOWN
: xkey
= 'G'; break;
6120 default: xkey
=0; break; /* else gcc warns `enum value not used'*/
6122 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
6127 case PK_HOME
: code
= 1; break;
6128 case PK_INSERT
: code
= 2; break;
6129 case PK_DELETE
: code
= 3; break;
6130 case PK_END
: code
= 4; break;
6131 case PK_PAGEUP
: code
= 5; break;
6132 case PK_PAGEDOWN
: code
= 6; break;
6133 default: code
= 0; break; /* else gcc warns `enum value not used' */
6135 p
+= sprintf((char *) p
, "\x1B[%d~", code
);
6139 if (PK_ISFKEY(keysym
)) {
6140 /* Map Shift+F1-F10 to F11-F20 */
6141 if (keysym
>= PK_F1
&& keysym
<= PK_F10
&& (modifiers
& PKM_SHIFT
))
6143 if ((term
->vt52_mode
|| term
->cfg
.funky_type
== FUNKY_VT100P
) &&
6145 /* XXX This overrides the XTERM/VT52 mode below */
6147 if (keysym
>= PK_F6
) offt
++;
6148 if (keysym
>= PK_F12
) offt
++;
6149 p
+= sprintf((char *) p
, term
->vt52_mode ?
"\x1B%c" : "\x1BO%c",
6150 'P' + keysym
- PK_F1
- offt
);
6153 if (term
->cfg
.funky_type
== FUNKY_LINUX
&& keysym
<= PK_F5
) {
6154 p
+= sprintf((char *) p
, "\x1B[[%c", 'A' + keysym
- PK_F1
);
6157 if (term
->cfg
.funky_type
== FUNKY_XTERM
&& keysym
<= PK_F4
) {
6158 if (term
->vt52_mode
)
6159 p
+= sprintf((char *) p
, "\x1B%c", 'P' + keysym
- PK_F1
);
6161 p
+= sprintf((char *) p
, "\x1BO%c", 'P' + keysym
- PK_F1
);
6164 p
+= sprintf((char *) p
, "\x1B[%d~", 11 + keysym
- PK_F1
);
6168 if (PK_ISCURSOR(keysym
)) {
6172 case PK_UP
: xkey
= 'A'; break;
6173 case PK_DOWN
: xkey
= 'B'; break;
6174 case PK_RIGHT
: xkey
= 'C'; break;
6175 case PK_LEFT
: xkey
= 'D'; break;
6176 case PK_REST
: xkey
= 'G'; break; /* centre key on number pad */
6177 default: xkey
= 0; break; /* else gcc warns `enum value not used' */
6179 if (term
->vt52_mode
)
6180 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
6182 int app_flg
= (term
->app_cursor_keys
&& !term
->cfg
.no_applic_c
);
6184 /* Useful mapping of Ctrl-arrows */
6185 if (modifiers
== PKM_CONTROL
)
6189 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
6191 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
6197 if (p
> output
|| tlen
> 0) {
6199 * Interrupt an ongoing paste. I'm not sure
6200 * this is sensible, but for the moment it's
6201 * preferable to having to faff about buffering
6207 * We need not bother about stdin backlogs
6208 * here, because in GUI PuTTY we can't do
6209 * anything about it anyway; there's no means
6210 * of asking Windows to hold off on KEYDOWN
6211 * messages. We _have_ to buffer everything
6214 term_seen_key_event(term
);
6218 fprintf(stderr
, "sending ESC\n");
6220 ldisc_send(term
->ldisc
, "\x1b", 1, 1);
6225 fprintf(stderr
, "sending %d bytes:", p
- output
);
6226 for (i
= 0; i
< p
- output
; i
++)
6227 fprintf(stderr
, " %02x", output
[i
]);
6228 fprintf(stderr
, "\n");
6230 ldisc_send(term
->ldisc
, output
, p
- output
, 1);
6231 } else if (tlen
> 0) {
6233 fprintf(stderr
, "sending %d unichars:", tlen
);
6234 for (i
= 0; i
< tlen
; i
++)
6235 fprintf(stderr
, " %04x", (unsigned) text
[i
]);
6236 fprintf(stderr
, "\n");
6238 luni_send(term
->ldisc
, text
, tlen
, 1);
6243 void term_nopaste(Terminal
*term
)
6245 if (term
->paste_len
== 0)
6247 sfree(term
->paste_buffer
);
6248 term
->paste_buffer
= NULL
;
6249 term
->paste_len
= 0;
6252 int term_paste_pending(Terminal
*term
)
6254 return term
->paste_len
!= 0;
6257 void term_paste(Terminal
*term
)
6259 long now
, paste_diff
;
6261 if (term
->paste_len
== 0)
6264 /* Don't wait forever to paste */
6265 if (term
->paste_hold
) {
6266 now
= GETTICKCOUNT();
6267 paste_diff
= now
- term
->last_paste
;
6268 if (paste_diff
>= 0 && paste_diff
< 450)
6271 term
->paste_hold
= 0;
6273 while (term
->paste_pos
< term
->paste_len
) {
6275 while (n
+ term
->paste_pos
< term
->paste_len
) {
6276 if (term
->paste_buffer
[term
->paste_pos
+ n
++] == '\015')
6280 luni_send(term
->ldisc
, term
->paste_buffer
+ term
->paste_pos
, n
, 0);
6281 term
->paste_pos
+= n
;
6283 if (term
->paste_pos
< term
->paste_len
) {
6284 term
->paste_hold
= 1;
6288 sfree(term
->paste_buffer
);
6289 term
->paste_buffer
= NULL
;
6290 term
->paste_len
= 0;
6293 static void deselect(Terminal
*term
)
6295 term
->selstate
= NO_SELECTION
;
6296 term
->selstart
.x
= term
->selstart
.y
= term
->selend
.x
= term
->selend
.y
= 0;
6299 void term_deselect(Terminal
*term
)
6305 int term_ldisc(Terminal
*term
, int option
)
6307 if (option
== LD_ECHO
)
6308 return term
->term_echoing
;
6309 if (option
== LD_EDIT
)
6310 return term
->term_editing
;
6314 int term_data(Terminal
*term
, int is_stderr
, const char *data
, int len
)
6316 bufchain_add(&term
->inbuf
, data
, len
);
6318 if (!term
->in_term_out
) {
6319 term
->in_term_out
= TRUE
;
6320 term_reset_cblink(term
);
6322 * During drag-selects, we do not process terminal input,
6323 * because the user will want the screen to hold still to
6326 if (term
->selstate
!= DRAGGING
)
6328 term
->in_term_out
= FALSE
;
6332 * term_out() always completely empties inbuf. Therefore,
6333 * there's no reason at all to return anything other than zero
6334 * from this function, because there _can't_ be a question of
6335 * the remote side needing to wait until term_out() has cleared
6338 * This is a slightly suboptimal way to deal with SSH-2 - in
6339 * principle, the window mechanism would allow us to continue
6340 * to accept data on forwarded ports and X connections even
6341 * while the terminal processing was going slowly - but we
6342 * can't do the 100% right thing without moving the terminal
6343 * processing into a separate thread, and that might hurt
6344 * portability. So we manage stdout buffering the old SSH-1 way:
6345 * if the terminal processing goes slowly, the whole SSH
6346 * connection stops accepting data until it's ready.
6348 * In practice, I can't imagine this causing serious trouble.
6354 * Write untrusted data to the terminal.
6355 * The only control character that should be honoured is \n (which
6356 * will behave as a CRLF).
6358 int term_data_untrusted(Terminal
*term
, const char *data
, int len
)
6361 /* FIXME: more sophisticated checking? */
6362 for (i
= 0; i
< len
; i
++) {
6363 if (data
[i
] == '\n')
6364 term_data(term
, 1, "\r\n", 2);
6365 else if (data
[i
] & 0x60)
6366 term_data(term
, 1, data
+ i
, 1);
6368 return 0; /* assumes that term_data() always returns 0 */
6371 void term_provide_logctx(Terminal
*term
, void *logctx
)
6373 term
->logctx
= logctx
;
6376 void term_set_focus(Terminal
*term
, int has_focus
)
6378 term
->has_focus
= has_focus
;
6379 term_schedule_cblink(term
);
6383 * Provide "auto" settings for remote tty modes, suitable for an
6384 * application with a terminal window.
6386 char *term_get_ttymode(Terminal
*term
, const char *mode
)
6389 if (strcmp(mode
, "ERASE") == 0) {
6390 val
= term
->cfg
.bksp_is_delete ?
"^?" : "^H";
6392 /* FIXME: perhaps we should set ONLCR based on cfg.lfhascr as well? */
6396 struct term_userpass_state
{
6398 int done_prompt
; /* printed out prompt yet? */
6399 size_t pos
; /* cursor position */
6403 * Process some terminal data in the course of username/password
6406 int term_get_userpass_input(Terminal
*term
, prompts_t
*p
,
6407 unsigned char *in
, int inlen
)
6409 struct term_userpass_state
*s
= (struct term_userpass_state
*)p
->data
;
6412 * First call. Set some stuff up.
6414 p
->data
= s
= snew(struct term_userpass_state
);
6417 /* We only print the `name' caption if we have to... */
6418 if (p
->name_reqd
&& p
->name
) {
6419 size_t l
= strlen(p
->name
);
6420 term_data_untrusted(term
, p
->name
, l
);
6421 if (p
->name
[l
-1] != '\n')
6422 term_data_untrusted(term
, "\n", 1);
6424 /* ...but we always print any `instruction'. */
6425 if (p
->instruction
) {
6426 size_t l
= strlen(p
->instruction
);
6427 term_data_untrusted(term
, p
->instruction
, l
);
6428 if (p
->instruction
[l
-1] != '\n')
6429 term_data_untrusted(term
, "\n", 1);
6432 * Zero all the results, in case we abort half-way through.
6436 for (i
= 0; i
< p
->n_prompts
; i
++)
6437 memset(p
->prompts
[i
]->result
, 0, p
->prompts
[i
]->result_len
);
6441 while (s
->curr_prompt
< p
->n_prompts
) {
6443 prompt_t
*pr
= p
->prompts
[s
->curr_prompt
];
6444 int finished_prompt
= 0;
6446 if (!s
->done_prompt
) {
6447 term_data_untrusted(term
, pr
->prompt
, strlen(pr
->prompt
));
6452 /* Breaking out here ensures that the prompt is printed even
6453 * if we're now waiting for user data. */
6454 if (!in
|| !inlen
) break;
6456 /* FIXME: should we be using local-line-editing code instead? */
6457 while (!finished_prompt
&& inlen
) {
6463 term_data(term
, 0, "\r\n", 2);
6464 pr
->result
[s
->pos
] = '\0';
6465 pr
->result
[pr
->result_len
- 1] = '\0';
6466 /* go to next prompt, if any */
6469 finished_prompt
= 1; /* break out */
6475 term_data(term
, 0, "\b \b", 3);
6481 while (s
->pos
> 0) {
6483 term_data(term
, 0, "\b \b", 3);
6489 /* Immediate abort. */
6490 term_data(term
, 0, "\r\n", 2);
6493 return 0; /* user abort */
6496 * This simplistic check for printability is disabled
6497 * when we're doing password input, because some people
6498 * have control characters in their passwords.
6501 (c
>= ' ' && c
<= '~') ||
6502 ((unsigned char) c
>= 160))
6503 && s
->pos
< pr
->result_len
- 1) {
6504 pr
->result
[s
->pos
++] = c
;
6506 term_data(term
, 0, &c
, 1);
6514 if (s
->curr_prompt
< p
->n_prompts
) {
6515 return -1; /* more data required */
6519 return +1; /* all done */