10 #define poslt(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x < (p2).x ) )
11 #define posle(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x <= (p2).x ) )
12 #define poseq(p1,p2) ( (p1).y == (p2).y && (p1).x == (p2).x )
13 #define posdiff(p1,p2) ( ((p1).y - (p2).y) * (term->cols+1) + (p1).x - (p2).x )
15 /* Product-order comparisons for rectangular block selection. */
16 #define posPlt(p1,p2) ( (p1).y <= (p2).y && (p1).x < (p2).x )
17 #define posPle(p1,p2) ( (p1).y <= (p2).y && (p1).x <= (p2).x )
19 #define incpos(p) ( (p).x == term->cols ? ((p).x = 0, (p).y++, 1) : ((p).x++, 0) )
20 #define decpos(p) ( (p).x == 0 ? ((p).x = term->cols, (p).y--, 1) : ((p).x--, 0) )
24 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
25 #define CL_VT100 0x0002 /* VT100 */
26 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
27 #define CL_VT102 0x0008 /* VT102 */
28 #define CL_VT220 0x0010 /* VT220 */
29 #define CL_VT320 0x0020 /* VT320 */
30 #define CL_VT420 0x0040 /* VT420 */
31 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
32 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
33 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
34 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
35 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
37 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
38 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
39 #define TM_VT102 (TM_VT100AVO|CL_VT102)
40 #define TM_VT220 (TM_VT102|CL_VT220)
41 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
42 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
44 #define TM_PUTTY (0xFFFF)
46 #define compatibility(x) \
47 if ( ((CL_##x)&term->compatibility_level) == 0 ) { \
48 term->termstate=TOPLEVEL; \
51 #define compatibility2(x,y) \
52 if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \
53 term->termstate=TOPLEVEL; \
57 #define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 )
59 const char sco2ansicolour
[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
61 #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
62 const wchar_t sel_nl
[] = SEL_NL
;
65 * Fetch the character at a particular position in a line array,
66 * for purposes of `wordtype'. The reason this isn't just a simple
67 * array reference is that if the character we find is UCSWIDE,
68 * then we must look one space further to the left.
70 #define UCSGET(a, x) \
71 ( (x)>0 && (a)[(x)].chr == UCSWIDE ? (a)[(x)-1].chr : (a)[(x)].chr )
74 * Detect the various aliases of U+0020 SPACE.
76 #define IS_SPACE_CHR(chr) \
77 ((chr) == 0x20 || (DIRECT_CHAR(chr) && ((chr) & 0xFF) == 0x20))
82 #define CSET_OF(chr) (DIRECT_CHAR(chr)||DIRECT_FONT(chr) ? (chr)&CSET_MASK : 0)
85 * Internal prototypes.
87 static void resizeline(Terminal
*, termline
*, int);
88 static termline
*lineptr(Terminal
*, int, int, int);
89 static void unlineptr(termline
*);
90 static void do_paint(Terminal
*, Context
, int);
91 static void erase_lots(Terminal
*, int, int, int);
92 static void swap_screen(Terminal
*, int, int, int);
93 static void update_sbar(Terminal
*);
94 static void deselect(Terminal
*);
95 static void term_print_finish(Terminal
*);
96 #ifdef OPTIMISE_SCROLL
97 static void scroll_display(Terminal
*, int, int, int);
98 #endif /* OPTIMISE_SCROLL */
100 static termline
*newline(Terminal
*term
, int cols
, int bce
)
105 line
= snew(termline
);
106 line
->chars
= snewn(cols
, termchar
);
107 for (j
= 0; j
< cols
; j
++)
108 line
->chars
[j
] = (bce ? term
->erase_char
: term
->basic_erase_char
);
109 line
->cols
= line
->size
= cols
;
110 line
->lattr
= LATTR_NORM
;
111 line
->temporary
= FALSE
;
117 static void freeline(termline
*line
)
125 static void unlineptr(termline
*line
)
132 * Diagnostic function: verify that a termline has a correct
133 * combining character structure.
135 * XXX-REMOVE-BEFORE-RELEASE: This is a performance-intensive
136 * check. Although it's currently really useful for getting all the
137 * bugs out of the new cc stuff, it will want to be absent when we
138 * make a proper release.
140 static void cc_check(termline
*line
)
142 unsigned char *flags
;
145 assert(line
->size
>= line
->cols
);
147 flags
= snewn(line
->size
, unsigned char);
149 for (i
= 0; i
< line
->size
; i
++)
150 flags
[i
] = (i
< line
->cols
);
152 for (i
= 0; i
< line
->cols
; i
++) {
154 while (line
->chars
[j
].cc_next
) {
155 j
+= line
->chars
[j
].cc_next
;
156 assert(j
>= line
->cols
&& j
< line
->size
);
165 assert(j
>= line
->cols
&& j
< line
->size
);
168 if (line
->chars
[j
].cc_next
)
169 j
+= line
->chars
[j
].cc_next
;
176 for (i
= 0; i
< line
->size
; i
++)
177 j
+= (flags
[i
] != 0);
179 assert(j
== line
->size
);
185 * Add a combining character to a character cell.
187 static void add_cc(termline
*line
, int col
, unsigned long chr
)
191 assert(col
>= 0 && col
< line
->cols
);
194 * Start by extending the cols array if the free list is empty.
196 if (!line
->cc_free
) {
198 line
->size
+= 16 + (line
->size
- line
->cols
) / 2;
199 line
->chars
= sresize(line
->chars
, line
->size
, termchar
);
201 while (n
< line
->size
) {
202 if (n
+1 < line
->size
)
203 line
->chars
[n
].cc_next
= 1;
205 line
->chars
[n
].cc_next
= 0;
211 * Now walk the cc list of the cell in question.
213 while (line
->chars
[col
].cc_next
)
214 col
+= line
->chars
[col
].cc_next
;
217 * `col' now points at the last cc currently in this cell; so
218 * we simply add another one.
220 newcc
= line
->cc_free
;
221 if (line
->chars
[newcc
].cc_next
)
222 line
->cc_free
= newcc
+ line
->chars
[newcc
].cc_next
;
225 line
->chars
[newcc
].cc_next
= 0;
226 line
->chars
[newcc
].chr
= chr
;
227 line
->chars
[col
].cc_next
= newcc
- col
;
229 cc_check(line
); /* XXX-REMOVE-BEFORE-RELEASE */
233 * Clear the combining character list in a character cell.
235 static void clear_cc(termline
*line
, int col
)
237 int oldfree
, origcol
= col
;
239 assert(col
>= 0 && col
< line
->cols
);
241 if (!line
->chars
[col
].cc_next
)
242 return; /* nothing needs doing */
244 oldfree
= line
->cc_free
;
245 line
->cc_free
= col
+ line
->chars
[col
].cc_next
;
246 while (line
->chars
[col
].cc_next
)
247 col
+= line
->chars
[col
].cc_next
;
249 line
->chars
[col
].cc_next
= oldfree
- col
;
251 line
->chars
[col
].cc_next
= 0;
253 line
->chars
[origcol
].cc_next
= 0;
255 cc_check(line
); /* XXX-REMOVE-BEFORE-RELEASE */
259 * Compare two character cells for equality. Special case required
260 * in do_paint() where we override what we expect the chr and attr
263 static int termchars_equal_override(termchar
*a
, termchar
*b
,
264 unsigned long bchr
, unsigned long battr
)
269 if (a
->attr
!= battr
)
271 while (a
->cc_next
|| b
->cc_next
) {
272 if (!a
->cc_next
|| !b
->cc_next
)
273 return FALSE
; /* one cc-list ends, other does not */
276 if (a
->chr
!= b
->chr
)
282 static int termchars_equal(termchar
*a
, termchar
*b
)
284 return termchars_equal_override(a
, b
, b
->chr
, b
->attr
);
288 * Copy a character cell. (Requires a pointer to the destination
289 * termline, so as to access its free list.)
291 static void copy_termchar(termline
*destline
, int x
, termchar
*src
)
293 clear_cc(destline
, x
);
295 destline
->chars
[x
] = *src
; /* copy everything except cc-list */
296 destline
->chars
[x
].cc_next
= 0; /* and make sure this is zero */
298 while (src
->cc_next
) {
300 add_cc(destline
, x
, src
->chr
);
303 cc_check(destline
); /* XXX-REMOVE-BEFORE-RELEASE */
307 * Move a character cell within its termline.
309 static void move_termchar(termline
*line
, termchar
*dest
, termchar
*src
)
311 /* First clear the cc list from the original char, just in case. */
312 clear_cc(line
, dest
- line
->chars
);
314 /* Move the character cell and adjust its cc_next. */
315 *dest
= *src
; /* copy everything except cc-list */
317 dest
->cc_next
= src
->cc_next
- (dest
-src
);
319 /* Ensure the original cell doesn't have a cc list. */
322 cc_check(line
); /* XXX-REMOVE-BEFORE-RELEASE */
326 * Compress and decompress a termline into an RLE-based format for
327 * storing in scrollback. (Since scrollback almost never needs to
328 * be modified and exists in huge quantities, this is a sensible
329 * tradeoff, particularly since it allows us to continue adding
330 * features to the main termchar structure without proportionally
331 * bloating the terminal emulator's memory footprint unless those
332 * features are in constant use.)
338 static void add(struct buf
*b
, unsigned char c
)
340 if (b
->len
>= b
->size
) {
341 b
->size
= (b
->len
* 3 / 2) + 512;
342 b
->data
= sresize(b
->data
, b
->size
, unsigned char);
344 b
->data
[b
->len
++] = c
;
346 static int get(struct buf
*b
)
348 return b
->data
[b
->len
++];
350 static void makerle(struct buf
*b
, termline
*ldata
,
351 void (*makeliteral
)(struct buf
*b
, termchar
*c
,
352 unsigned long *state
))
354 int hdrpos
, hdrsize
, n
, prevlen
, prevpos
, thislen
, thispos
, prev2
;
355 termchar
*c
= ldata
->chars
;
356 unsigned long state
= 0, oldstate
;
363 prevlen
= prevpos
= 0;
368 makeliteral(b
, c
++, &state
);
369 thislen
= b
->len
- thispos
;
370 if (thislen
== prevlen
&&
371 !memcmp(b
->data
+ prevpos
, b
->data
+ thispos
, thislen
)) {
373 * This literal precisely matches the previous one.
374 * Turn it into a run if it's worthwhile.
376 * With one-byte literals, it costs us two bytes to
377 * encode a run, plus another byte to write the header
378 * to resume normal output; so a three-element run is
379 * neutral, and anything beyond that is unconditionally
380 * worthwhile. With two-byte literals or more, even a
383 if (thislen
> 1 || prev2
) {
387 * It's worth encoding a run. Start at prevpos,
388 * unless hdrsize==0 in which case we can back up
389 * another one and start by overwriting hdrpos.
392 hdrsize
--; /* remove the literal at prevpos */
396 prevpos
-= prevlen
;/* and possibly another one */
400 assert(prevpos
== hdrpos
+ 1);
402 b
->len
= prevpos
+prevlen
;
404 memmove(b
->data
+ prevpos
+1, b
->data
+ prevpos
, prevlen
);
406 b
->len
= prevpos
+prevlen
+1;
408 * Terminate the previous run of ordinary
411 assert(hdrsize
>= 1 && hdrsize
<= 128);
412 b
->data
[hdrpos
] = hdrsize
- 1;
415 runlen
= prev2 ?
3 : 2;
417 while (n
> 0 && runlen
< 129) {
421 makeliteral(b
, c
, &state
);
422 tmplen
= b
->len
- tmppos
;
424 if (tmplen
!= thislen
||
425 memcmp(b
->data
+ runpos
+1, b
->data
+ tmppos
, tmplen
)) {
427 break; /* run over */
432 assert(runlen
>= 2 && runlen
<= 129);
433 b
->data
[runpos
] = runlen
+ 0x80 - 2;
442 * Just flag that the previous two literals were
443 * identical, in case we find a third identical one
444 * we want to turn into a run.
457 * This character isn't (yet) part of a run. Add it to
461 if (hdrsize
== 128) {
462 b
->data
[hdrpos
] = hdrsize
- 1;
466 prevlen
= prevpos
= 0;
475 assert(hdrsize
<= 128);
476 b
->data
[hdrpos
] = hdrsize
- 1;
481 static void makeliteral_chr(struct buf
*b
, termchar
*c
, unsigned long *state
)
484 * My encoding for characters is UTF-8-like, in that it stores
485 * 7-bit ASCII in one byte and uses high-bit-set bytes as
486 * introducers to indicate a longer sequence. However, it's
487 * unlike UTF-8 in that it doesn't need to be able to
488 * resynchronise, and therefore I don't want to waste two bits
489 * per byte on having recognisable continuation characters.
490 * Also I don't want to rule out the possibility that I may one
491 * day use values 0x80000000-0xFFFFFFFF for interesting
492 * purposes, so unlike UTF-8 I need a full 32-bit range.
493 * Accordingly, here is my encoding:
495 * 00000000-0000007F: 0xxxxxxx (but see below)
496 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
497 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
498 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
499 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
501 * (`Z' is like `x' but is always going to be zero since the
502 * values I'm encoding don't go above 2^32. In principle the
503 * five-byte form of the encoding could extend to 2^35, and
504 * there could be six-, seven-, eight- and nine-byte forms as
505 * well to allow up to 64-bit values to be encoded. But that's
506 * completely unnecessary for these purposes!)
508 * The encoding as written above would be very simple, except
509 * that 7-bit ASCII can occur in several different ways in the
510 * terminal data; sometimes it crops up in the D800 page
511 * (CSET_ASCII) but at other times it's in the 0000 page (real
512 * Unicode). Therefore, this encoding is actually _stateful_:
513 * the one-byte encoding of 00-7F actually indicates `reuse the
514 * upper three bytes of the last character', and to encode an
515 * absolute value of 00-7F you need to use the two-byte form
518 if ((c
->chr
& ~0x7F) == *state
) {
519 add(b
, (unsigned char)(c
->chr
& 0x7F));
520 } else if (c
->chr
< 0x4000) {
521 add(b
, (unsigned char)(((c
->chr
>> 8) & 0x3F) | 0x80));
522 add(b
, (unsigned char)(c
->chr
& 0xFF));
523 } else if (c
->chr
< 0x200000) {
524 add(b
, (unsigned char)(((c
->chr
>> 16) & 0x1F) | 0xC0));
525 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
526 add(b
, (unsigned char)(c
->chr
& 0xFF));
527 } else if (c
->chr
< 0x10000000) {
528 add(b
, (unsigned char)(((c
->chr
>> 24) & 0x0F) | 0xE0));
529 add(b
, (unsigned char)((c
->chr
>> 16) & 0xFF));
530 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
531 add(b
, (unsigned char)(c
->chr
& 0xFF));
534 add(b
, (unsigned char)((c
->chr
>> 24) & 0xFF));
535 add(b
, (unsigned char)((c
->chr
>> 16) & 0xFF));
536 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
537 add(b
, (unsigned char)(c
->chr
& 0xFF));
539 *state
= c
->chr
& ~0xFF;
541 static void makeliteral_attr(struct buf
*b
, termchar
*c
, unsigned long *state
)
544 * My encoding for attributes is 16-bit-granular and assumes
545 * that the top bit of the word is never required. I either
546 * store a two-byte value with the top bit clear (indicating
547 * just that value), or a four-byte value with the top bit set
548 * (indicating the same value with its top bit clear).
550 if (c
->attr
< 0x8000) {
551 add(b
, (unsigned char)((c
->attr
>> 8) & 0xFF));
552 add(b
, (unsigned char)(c
->attr
& 0xFF));
554 add(b
, (unsigned char)(((c
->attr
>> 24) & 0x7F) | 0x80));
555 add(b
, (unsigned char)((c
->attr
>> 16) & 0xFF));
556 add(b
, (unsigned char)((c
->attr
>> 8) & 0xFF));
557 add(b
, (unsigned char)(c
->attr
& 0xFF));
560 static void makeliteral_cc(struct buf
*b
, termchar
*c
, unsigned long *state
)
563 * For combining characters, I just encode a bunch of ordinary
564 * chars using makeliteral_chr, and terminate with a \0
565 * character (which I know won't come up as a combining char
568 * I don't use the stateful encoding in makeliteral_chr.
570 unsigned long zstate
;
579 makeliteral_chr(b
, c
, &zstate
);
584 makeliteral_chr(b
, &z
, &zstate
);
587 static termline
*decompressline(unsigned char *data
, int *bytes_used
);
589 static unsigned char *compressline(termline
*ldata
)
591 struct buf buffer
= { NULL
, 0, 0 }, *b
= &buffer
;
594 * First, store the column count, 7 bits at a time, least
595 * significant `digit' first, with the high bit set on all but
601 add(b
, (unsigned char)((n
& 0x7F) | 0x80));
604 add(b
, (unsigned char)(n
));
608 * Next store the lattrs; same principle.
611 int n
= ldata
->lattr
;
613 add(b
, (unsigned char)((n
& 0x7F) | 0x80));
616 add(b
, (unsigned char)(n
));
620 * Now we store a sequence of separate run-length encoded
621 * fragments, each containing exactly as many symbols as there
622 * are columns in the ldata.
624 * All of these have a common basic format:
626 * - a byte 00-7F indicates that X+1 literals follow it
627 * - a byte 80-FF indicates that a single literal follows it
628 * and expects to be repeated (X-0x80)+2 times.
630 * The format of the `literals' varies between the fragments.
632 makerle(b
, ldata
, makeliteral_chr
);
633 makerle(b
, ldata
, makeliteral_attr
);
634 makerle(b
, ldata
, makeliteral_cc
);
637 * Diagnostics: ensure that the compressed data really does
638 * decompress to the right thing.
640 * XXX-REMOVE-BEFORE-RELEASE: This is a bit performance-heavy
641 * to be leaving in production code.
643 #ifndef CHECK_SB_COMPRESSION
649 #ifdef DIAGNOSTIC_SB_COMPRESSION
650 for (i
= 0; i
< b
->len
; i
++) {
651 printf(" %02x ", b
->data
[i
]);
656 dcl
= decompressline(b
->data
, &dused
);
657 assert(b
->len
== dused
);
658 assert(ldata
->cols
== dcl
->cols
);
659 assert(ldata
->lattr
== dcl
->lattr
);
660 for (i
= 0; i
< ldata
->cols
; i
++)
661 assert(termchars_equal(&ldata
->chars
[i
], &dcl
->chars
[i
]));
663 #ifdef DIAGNOSTIC_SB_COMPRESSION
664 printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",
665 ldata
->cols
, 4 * ldata
->cols
, dused
,
666 (double)dused
/ (4 * ldata
->cols
));
674 * Trim the allocated memory so we don't waste any, and return.
676 return sresize(b
->data
, b
->len
, unsigned char);
679 static void readrle(struct buf
*b
, termline
*ldata
,
680 void (*readliteral
)(struct buf
*b
, termchar
*c
,
681 termline
*ldata
, unsigned long *state
))
684 unsigned long state
= 0;
686 while (n
< ldata
->cols
) {
692 int pos
= b
->len
, count
= hdr
+ 2 - 0x80;
694 assert(n
< ldata
->cols
);
696 readliteral(b
, ldata
->chars
+ n
, ldata
, &state
);
700 /* Just a sequence of consecutive literals. */
704 assert(n
< ldata
->cols
);
705 readliteral(b
, ldata
->chars
+ n
, ldata
, &state
);
711 assert(n
== ldata
->cols
);
713 static void readliteral_chr(struct buf
*b
, termchar
*c
, termline
*ldata
,
714 unsigned long *state
)
719 * 00000000-0000007F: 0xxxxxxx
720 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
721 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
722 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
723 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
728 c
->chr
= byte
| *state
;
729 } else if (byte
< 0xC0) {
730 c
->chr
= (byte
&~ 0xC0) << 8;
732 } else if (byte
< 0xE0) {
733 c
->chr
= (byte
&~ 0xE0) << 16;
734 c
->chr
|= get(b
) << 8;
736 } else if (byte
< 0xF0) {
737 c
->chr
= (byte
&~ 0xF0) << 24;
738 c
->chr
|= get(b
) << 16;
739 c
->chr
|= get(b
) << 8;
742 assert(byte
== 0xF0);
743 c
->chr
= get(b
) << 24;
744 c
->chr
|= get(b
) << 16;
745 c
->chr
|= get(b
) << 8;
748 *state
= c
->chr
& ~0xFF;
750 static void readliteral_attr(struct buf
*b
, termchar
*c
, termline
*ldata
,
751 unsigned long *state
)
766 static void readliteral_cc(struct buf
*b
, termchar
*c
, termline
*ldata
,
767 unsigned long *state
)
770 unsigned long zstate
;
771 int x
= c
- ldata
->chars
;
777 readliteral_chr(b
, &n
, ldata
, &zstate
);
780 add_cc(ldata
, x
, n
.chr
);
784 static termline
*decompressline(unsigned char *data
, int *bytes_used
)
786 int ncols
, byte
, shift
;
787 struct buf buffer
, *b
= &buffer
;
794 * First read in the column count.
799 ncols
|= (byte
& 0x7F) << shift
;
801 } while (byte
& 0x80);
804 * Now create the output termline.
806 ldata
= snew(termline
);
807 ldata
->chars
= snewn(ncols
, termchar
);
808 ldata
->cols
= ldata
->size
= ncols
;
809 ldata
->temporary
= TRUE
;
813 * We must set all the cc pointers in ldata->chars to 0 right
814 * now, so that cc diagnostics that verify the integrity of the
815 * whole line will make sense while we're in the middle of
820 for (i
= 0; i
< ldata
->cols
; i
++)
821 ldata
->chars
[i
].cc_next
= 0;
825 * Now read in the lattr.
827 ldata
->lattr
= shift
= 0;
830 ldata
->lattr
|= (byte
& 0x7F) << shift
;
832 } while (byte
& 0x80);
835 * Now we read in each of the RLE streams in turn.
837 readrle(b
, ldata
, readliteral_chr
);
838 readrle(b
, ldata
, readliteral_attr
);
839 readrle(b
, ldata
, readliteral_cc
);
841 /* Return the number of bytes read, for diagnostic purposes. */
843 *bytes_used
= b
->len
;
849 * Resize a line to make it `cols' columns wide.
851 static void resizeline(Terminal
*term
, termline
*line
, int cols
)
855 if (line
->cols
!= cols
) {
857 oldcols
= line
->cols
;
860 * This line is the wrong length, which probably means it
861 * hasn't been accessed since a resize. Resize it now.
863 * First, go through all the characters that will be thrown
864 * out in the resize (if we're shrinking the line) and
865 * return their cc lists to the cc free list.
867 for (i
= cols
; i
< oldcols
; i
++)
871 * If we're shrinking the line, we now bodily move the
872 * entire cc section from where it started to where it now
873 * needs to be. (We have to do this before the resize, so
874 * that the data we're copying is still there. However, if
875 * we're expanding, we have to wait until _after_ the
876 * resize so that the space we're copying into is there.)
879 memmove(line
->chars
+ cols
, line
->chars
+ oldcols
,
880 (line
->size
- line
->cols
) * TSIZE
);
883 * Now do the actual resize, leaving the _same_ amount of
884 * cc space as there was to begin with.
886 line
->size
+= cols
- oldcols
;
887 line
->chars
= sresize(line
->chars
, line
->size
, TTYPE
);
891 * If we're expanding the line, _now_ we move the cc
895 memmove(line
->chars
+ cols
, line
->chars
+ oldcols
,
896 (line
->size
- line
->cols
) * TSIZE
);
899 * Go through what's left of the original line, and adjust
900 * the first cc_next pointer in each list. (All the
901 * subsequent ones are still valid because they are
902 * relative offsets within the cc block.) Also do the same
903 * to the head of the cc_free list.
905 for (i
= 0; i
< oldcols
&& i
< cols
; i
++)
906 if (line
->chars
[i
].cc_next
)
907 line
->chars
[i
].cc_next
+= cols
- oldcols
;
909 line
->cc_free
+= cols
- oldcols
;
912 * And finally fill in the new space with erase chars. (We
913 * don't have to worry about cc lists here, because we
914 * _know_ the erase char doesn't have one.)
916 for (i
= oldcols
; i
< cols
; i
++)
917 line
->chars
[i
] = term
->basic_erase_char
;
919 cc_check(line
); /* XXX-REMOVE-BEFORE-RELEASE */
924 * Get the number of lines in the scrollback.
926 static int sblines(Terminal
*term
)
928 int sblines
= count234(term
->scrollback
);
929 if (term
->cfg
.erase_to_scrollback
&&
930 term
->alt_which
&& term
->alt_screen
) {
931 sblines
+= term
->alt_sblines
;
937 * Retrieve a line of the screen or of the scrollback, according to
938 * whether the y coordinate is non-negative or negative
941 static termline
*lineptr(Terminal
*term
, int y
, int lineno
, int screen
)
948 whichtree
= term
->screen
;
955 if (term
->cfg
.erase_to_scrollback
&&
956 term
->alt_which
&& term
->alt_screen
) {
957 altlines
= term
->alt_sblines
;
960 whichtree
= term
->scrollback
;
961 treeindex
= y
+ altlines
+ count234(term
->scrollback
);
963 whichtree
= term
->alt_screen
;
964 treeindex
= y
+ term
->alt_sblines
;
965 /* treeindex = y + count234(term->alt_screen); */
968 if (whichtree
== term
->scrollback
) {
969 unsigned char *cline
= index234(whichtree
, treeindex
);
970 line
= decompressline(cline
, NULL
);
972 line
= index234(whichtree
, treeindex
);
975 /* We assume that we don't screw up and retrieve something out of range. */
976 assert(line
!= NULL
);
978 resizeline(term
, line
, term
->cols
);
979 /* FIXME: should we sort the compressed scrollback out here? */
984 #define lineptr(x) (lineptr)(term,x,__LINE__,FALSE)
985 #define scrlineptr(x) (lineptr)(term,x,__LINE__,TRUE)
988 * Set up power-on settings for the terminal.
990 static void power_on(Terminal
*term
)
992 term
->curs
.x
= term
->curs
.y
= 0;
993 term
->alt_x
= term
->alt_y
= 0;
994 term
->savecurs
.x
= term
->savecurs
.y
= 0;
995 term
->alt_t
= term
->marg_t
= 0;
996 if (term
->rows
!= -1)
997 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
999 term
->alt_b
= term
->marg_b
= 0;
1000 if (term
->cols
!= -1) {
1002 for (i
= 0; i
< term
->cols
; i
++)
1003 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
1005 term
->alt_om
= term
->dec_om
= term
->cfg
.dec_om
;
1006 term
->alt_ins
= term
->insert
= FALSE
;
1007 term
->alt_wnext
= term
->wrapnext
= term
->save_wnext
= FALSE
;
1008 term
->alt_wrap
= term
->wrap
= term
->cfg
.wrap_mode
;
1009 term
->alt_cset
= term
->cset
= term
->save_cset
= 0;
1010 term
->alt_utf
= term
->utf
= term
->save_utf
= 0;
1011 term
->utf_state
= 0;
1012 term
->alt_sco_acs
= term
->sco_acs
= term
->save_sco_acs
= 0;
1013 term
->cset_attr
[0] = term
->cset_attr
[1] = term
->save_csattr
= CSET_ASCII
;
1015 term
->in_vbell
= FALSE
;
1016 term
->cursor_on
= 1;
1017 term
->big_cursor
= 0;
1018 term
->default_attr
= term
->save_attr
= term
->curr_attr
= ATTR_DEFAULT
;
1019 term
->term_editing
= term
->term_echoing
= FALSE
;
1020 term
->app_cursor_keys
= term
->cfg
.app_cursor
;
1021 term
->app_keypad_keys
= term
->cfg
.app_keypad
;
1022 term
->use_bce
= term
->cfg
.bce
;
1023 term
->blink_is_real
= term
->cfg
.blinktext
;
1024 term
->erase_char
= term
->basic_erase_char
;
1025 term
->alt_which
= 0;
1026 term_print_finish(term
);
1029 for (i
= 0; i
< 256; i
++)
1030 term
->wordness
[i
] = term
->cfg
.wordness
[i
];
1033 swap_screen(term
, 1, FALSE
, FALSE
);
1034 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1035 swap_screen(term
, 0, FALSE
, FALSE
);
1036 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1041 * Force a screen update.
1043 void term_update(Terminal
*term
)
1046 ctx
= get_ctx(term
->frontend
);
1048 int need_sbar_update
= term
->seen_disp_event
;
1049 if (term
->seen_disp_event
&& term
->cfg
.scroll_on_disp
) {
1050 term
->disptop
= 0; /* return to main screen */
1051 term
->seen_disp_event
= 0;
1052 need_sbar_update
= TRUE
;
1055 if (need_sbar_update
)
1057 do_paint(term
, ctx
, TRUE
);
1058 sys_cursor(term
->frontend
, term
->curs
.x
, term
->curs
.y
- term
->disptop
);
1064 * Called from front end when a keypress occurs, to trigger
1065 * anything magical that needs to happen in that situation.
1067 void term_seen_key_event(Terminal
*term
)
1070 * On any keypress, clear the bell overload mechanism
1071 * completely, on the grounds that large numbers of
1072 * beeps coming from deliberate key action are likely
1073 * to be intended (e.g. beeps from filename completion
1074 * blocking repeatedly).
1076 term
->beep_overloaded
= FALSE
;
1077 while (term
->beephead
) {
1078 struct beeptime
*tmp
= term
->beephead
;
1079 term
->beephead
= tmp
->next
;
1082 term
->beeptail
= NULL
;
1086 * Reset the scrollback on keypress, if we're doing that.
1088 if (term
->cfg
.scroll_on_key
) {
1089 term
->disptop
= 0; /* return to main screen */
1090 term
->seen_disp_event
= 1;
1095 * Same as power_on(), but an external function.
1097 void term_pwron(Terminal
*term
)
1100 if (term
->ldisc
) /* cause ldisc to notice changes */
1101 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1107 static void set_erase_char(Terminal
*term
)
1109 term
->erase_char
= term
->basic_erase_char
;
1111 term
->erase_char
.attr
= (term
->curr_attr
&
1112 (ATTR_FGMASK
| ATTR_BGMASK
));
1116 * When the user reconfigures us, we need to check the forbidden-
1117 * alternate-screen config option, disable raw mouse mode if the
1118 * user has disabled mouse reporting, and abandon a print job if
1119 * the user has disabled printing.
1121 void term_reconfig(Terminal
*term
, Config
*cfg
)
1124 * Before adopting the new config, check all those terminal
1125 * settings which control power-on defaults; and if they've
1126 * changed, we will modify the current state as well as the
1127 * default one. The full list is: Auto wrap mode, DEC Origin
1128 * Mode, BCE, blinking text, character classes.
1130 int reset_wrap
, reset_decom
, reset_bce
, reset_blink
, reset_charclass
;
1133 reset_wrap
= (term
->cfg
.wrap_mode
!= cfg
->wrap_mode
);
1134 reset_decom
= (term
->cfg
.dec_om
!= cfg
->dec_om
);
1135 reset_bce
= (term
->cfg
.bce
!= cfg
->bce
);
1136 reset_blink
= (term
->cfg
.blinktext
!= cfg
->blinktext
);
1137 reset_charclass
= 0;
1138 for (i
= 0; i
< lenof(term
->cfg
.wordness
); i
++)
1139 if (term
->cfg
.wordness
[i
] != cfg
->wordness
[i
])
1140 reset_charclass
= 1;
1143 * If the bidi or shaping settings have changed, flush the bidi
1146 if (term
->cfg
.arabicshaping
!= cfg
->arabicshaping
||
1147 term
->cfg
.bidi
!= cfg
->bidi
) {
1148 for (i
= 0; i
< term
->bidi_cache_size
; i
++) {
1149 sfree(term
->pre_bidi_cache
[i
].chars
);
1150 sfree(term
->post_bidi_cache
[i
].chars
);
1151 term
->pre_bidi_cache
[i
].width
= -1;
1152 term
->pre_bidi_cache
[i
].chars
= NULL
;
1153 term
->post_bidi_cache
[i
].width
= -1;
1154 term
->post_bidi_cache
[i
].chars
= NULL
;
1158 term
->cfg
= *cfg
; /* STRUCTURE COPY */
1161 term
->alt_wrap
= term
->wrap
= term
->cfg
.wrap_mode
;
1163 term
->alt_om
= term
->dec_om
= term
->cfg
.dec_om
;
1165 term
->use_bce
= term
->cfg
.bce
;
1166 set_erase_char(term
);
1169 term
->blink_is_real
= term
->cfg
.blinktext
;
1170 if (reset_charclass
)
1171 for (i
= 0; i
< 256; i
++)
1172 term
->wordness
[i
] = term
->cfg
.wordness
[i
];
1174 if (term
->cfg
.no_alt_screen
)
1175 swap_screen(term
, 0, FALSE
, FALSE
);
1176 if (term
->cfg
.no_mouse_rep
) {
1177 term
->xterm_mouse
= 0;
1178 set_raw_mouse_mode(term
->frontend
, 0);
1180 if (term
->cfg
.no_remote_charset
) {
1181 term
->cset_attr
[0] = term
->cset_attr
[1] = CSET_ASCII
;
1182 term
->sco_acs
= term
->alt_sco_acs
= 0;
1185 if (!*term
->cfg
.printer
) {
1186 term_print_finish(term
);
1191 * Clear the scrollback.
1193 void term_clrsb(Terminal
*term
)
1197 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
) {
1198 sfree(line
); /* this is compressed data, not a termline */
1200 term
->tempsblines
= 0;
1201 term
->alt_sblines
= 0;
1206 * Initialise the terminal.
1208 Terminal
*term_init(Config
*mycfg
, struct unicode_data
*ucsdata
,
1214 * Allocate a new Terminal structure and initialise the fields
1217 term
= snew(Terminal
);
1218 term
->frontend
= frontend
;
1219 term
->ucsdata
= ucsdata
;
1220 term
->cfg
= *mycfg
; /* STRUCTURE COPY */
1221 term
->logctx
= NULL
;
1222 term
->compatibility_level
= TM_PUTTY
;
1223 strcpy(term
->id_string
, "\033[?6c");
1224 term
->last_blink
= term
->last_tblink
= 0;
1225 term
->paste_buffer
= NULL
;
1226 term
->paste_len
= 0;
1227 term
->last_paste
= 0;
1228 bufchain_init(&term
->inbuf
);
1229 bufchain_init(&term
->printer_buf
);
1230 term
->printing
= term
->only_printing
= FALSE
;
1231 term
->print_job
= NULL
;
1232 term
->vt52_mode
= FALSE
;
1233 term
->cr_lf_return
= FALSE
;
1234 term
->seen_disp_event
= FALSE
;
1235 term
->xterm_mouse
= term
->mouse_is_down
= FALSE
;
1236 term
->reset_132
= FALSE
;
1237 term
->blinker
= term
->tblinker
= 0;
1238 term
->has_focus
= 1;
1239 term
->repeat_off
= FALSE
;
1240 term
->termstate
= TOPLEVEL
;
1241 term
->selstate
= NO_SELECTION
;
1244 term
->screen
= term
->alt_screen
= term
->scrollback
= NULL
;
1245 term
->tempsblines
= 0;
1246 term
->alt_sblines
= 0;
1248 term
->disptext
= NULL
;
1249 term
->dispcursx
= term
->dispcursy
= -1;
1252 term
->rows
= term
->cols
= -1;
1254 term
->beephead
= term
->beeptail
= NULL
;
1255 #ifdef OPTIMISE_SCROLL
1256 term
->scrollhead
= term
->scrolltail
= NULL
;
1257 #endif /* OPTIMISE_SCROLL */
1259 term
->lastbeep
= FALSE
;
1260 term
->beep_overloaded
= FALSE
;
1261 term
->attr_mask
= 0xffffffff;
1262 term
->resize_fn
= NULL
;
1263 term
->resize_ctx
= NULL
;
1264 term
->in_term_out
= FALSE
;
1266 term
->ltemp_size
= 0;
1267 term
->wcFrom
= NULL
;
1269 term
->wcFromTo_size
= 0;
1271 term
->bidi_cache_size
= 0;
1272 term
->pre_bidi_cache
= term
->post_bidi_cache
= NULL
;
1275 term
->basic_erase_char
.chr
= CSET_ASCII
| ' ';
1276 term
->basic_erase_char
.attr
= ATTR_DEFAULT
;
1277 term
->basic_erase_char
.cc_next
= 0;
1278 term
->erase_char
= term
->basic_erase_char
;
1283 void term_free(Terminal
*term
)
1286 struct beeptime
*beep
;
1289 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
)
1290 sfree(line
); /* compressed data, not a termline */
1291 freetree234(term
->scrollback
);
1292 while ((line
= delpos234(term
->screen
, 0)) != NULL
)
1294 freetree234(term
->screen
);
1295 while ((line
= delpos234(term
->alt_screen
, 0)) != NULL
)
1297 freetree234(term
->alt_screen
);
1298 if (term
->disptext
) {
1299 for (i
= 0; i
< term
->rows
; i
++)
1300 freeline(term
->disptext
[i
]);
1302 sfree(term
->disptext
);
1303 while (term
->beephead
) {
1304 beep
= term
->beephead
;
1305 term
->beephead
= beep
->next
;
1308 bufchain_clear(&term
->inbuf
);
1310 printer_finish_job(term
->print_job
);
1311 bufchain_clear(&term
->printer_buf
);
1312 sfree(term
->paste_buffer
);
1314 sfree(term
->wcFrom
);
1317 for (i
= 0; i
< term
->bidi_cache_size
; i
++) {
1318 sfree(term
->pre_bidi_cache
[i
].chars
);
1319 sfree(term
->post_bidi_cache
[i
].chars
);
1321 sfree(term
->pre_bidi_cache
);
1322 sfree(term
->post_bidi_cache
);
1328 * Set up the terminal for a given size.
1330 void term_size(Terminal
*term
, int newrows
, int newcols
, int newsavelines
)
1333 termline
**newdisp
, *line
;
1334 int i
, j
, oldrows
= term
->rows
;
1336 int save_alt_which
= term
->alt_which
;
1338 if (newrows
== term
->rows
&& newcols
== term
->cols
&&
1339 newsavelines
== term
->savelines
)
1340 return; /* nothing to do */
1343 swap_screen(term
, 0, FALSE
, FALSE
);
1345 term
->alt_t
= term
->marg_t
= 0;
1346 term
->alt_b
= term
->marg_b
= newrows
- 1;
1348 if (term
->rows
== -1) {
1349 term
->scrollback
= newtree234(NULL
);
1350 term
->screen
= newtree234(NULL
);
1351 term
->tempsblines
= 0;
1356 * Resize the screen and scrollback. We only need to shift
1357 * lines around within our data structures, because lineptr()
1358 * will take care of resizing each individual line if
1361 * - If the new screen is longer, we shunt lines in from temporary
1362 * scrollback if possible, otherwise we add new blank lines at
1365 * - If the new screen is shorter, we remove any blank lines at
1366 * the bottom if possible, otherwise shunt lines above the cursor
1367 * to scrollback if possible, otherwise delete lines below the
1370 * - Then, if the new scrollback length is less than the
1371 * amount of scrollback we actually have, we must throw some
1374 sblen
= count234(term
->scrollback
);
1375 /* Do this loop to expand the screen if newrows > rows */
1376 assert(term
->rows
== count234(term
->screen
));
1377 while (term
->rows
< newrows
) {
1378 if (term
->tempsblines
> 0) {
1379 unsigned char *cline
;
1380 /* Insert a line from the scrollback at the top of the screen. */
1381 assert(sblen
>= term
->tempsblines
);
1382 cline
= delpos234(term
->scrollback
, --sblen
);
1383 line
= decompressline(cline
, NULL
);
1385 line
->temporary
= FALSE
; /* reconstituted line is now real */
1386 term
->tempsblines
-= 1;
1387 addpos234(term
->screen
, line
, 0);
1389 term
->savecurs
.y
+= 1;
1391 /* Add a new blank line at the bottom of the screen. */
1392 line
= newline(term
, newcols
, FALSE
);
1393 addpos234(term
->screen
, line
, count234(term
->screen
));
1397 /* Do this loop to shrink the screen if newrows < rows */
1398 while (term
->rows
> newrows
) {
1399 if (term
->curs
.y
< term
->rows
- 1) {
1400 /* delete bottom row, unless it contains the cursor */
1401 sfree(delpos234(term
->screen
, term
->rows
- 1));
1403 /* push top row to scrollback */
1404 line
= delpos234(term
->screen
, 0);
1405 addpos234(term
->scrollback
, compressline(line
), sblen
++);
1407 term
->tempsblines
+= 1;
1409 term
->savecurs
.y
-= 1;
1413 assert(term
->rows
== newrows
);
1414 assert(count234(term
->screen
) == newrows
);
1416 /* Delete any excess lines from the scrollback. */
1417 while (sblen
> newsavelines
) {
1418 line
= delpos234(term
->scrollback
, 0);
1422 if (sblen
< term
->tempsblines
)
1423 term
->tempsblines
= sblen
;
1424 assert(count234(term
->scrollback
) <= newsavelines
);
1425 assert(count234(term
->scrollback
) >= term
->tempsblines
);
1428 /* Make a new displayed text buffer. */
1429 newdisp
= snewn(newrows
, termline
*);
1430 for (i
= 0; i
< newrows
; i
++) {
1431 newdisp
[i
] = newline(term
, newcols
, FALSE
);
1432 for (j
= 0; j
< newcols
; j
++)
1433 newdisp
[i
]->chars
[i
].attr
= ATTR_INVALID
;
1435 if (term
->disptext
) {
1436 for (i
= 0; i
< oldrows
; i
++)
1437 freeline(term
->disptext
[i
]);
1439 sfree(term
->disptext
);
1440 term
->disptext
= newdisp
;
1441 term
->dispcursx
= term
->dispcursy
= -1;
1443 /* Make a new alternate screen. */
1444 newalt
= newtree234(NULL
);
1445 for (i
= 0; i
< newrows
; i
++) {
1446 line
= newline(term
, newcols
, TRUE
);
1447 addpos234(newalt
, line
, i
);
1449 if (term
->alt_screen
) {
1450 while (NULL
!= (line
= delpos234(term
->alt_screen
, 0)))
1452 freetree234(term
->alt_screen
);
1454 term
->alt_screen
= newalt
;
1455 term
->alt_sblines
= 0;
1457 term
->tabs
= sresize(term
->tabs
, newcols
, unsigned char);
1460 for (i
= (term
->cols
> 0 ? term
->cols
: 0); i
< newcols
; i
++)
1461 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
1464 /* Check that the cursor positions are still valid. */
1465 if (term
->savecurs
.y
< 0)
1466 term
->savecurs
.y
= 0;
1467 if (term
->savecurs
.y
>= newrows
)
1468 term
->savecurs
.y
= newrows
- 1;
1469 if (term
->curs
.y
< 0)
1471 if (term
->curs
.y
>= newrows
)
1472 term
->curs
.y
= newrows
- 1;
1473 if (term
->curs
.x
>= newcols
)
1474 term
->curs
.x
= newcols
- 1;
1475 term
->alt_x
= term
->alt_y
= 0;
1476 term
->wrapnext
= term
->alt_wnext
= FALSE
;
1478 term
->rows
= newrows
;
1479 term
->cols
= newcols
;
1480 term
->savelines
= newsavelines
;
1482 swap_screen(term
, save_alt_which
, FALSE
, FALSE
);
1486 if (term
->resize_fn
)
1487 term
->resize_fn(term
->resize_ctx
, term
->cols
, term
->rows
);
1491 * Hand a function and context pointer to the terminal which it can
1492 * use to notify a back end of resizes.
1494 void term_provide_resize_fn(Terminal
*term
,
1495 void (*resize_fn
)(void *, int, int),
1498 term
->resize_fn
= resize_fn
;
1499 term
->resize_ctx
= resize_ctx
;
1500 if (term
->cols
> 0 && term
->rows
> 0)
1501 resize_fn(resize_ctx
, term
->cols
, term
->rows
);
1504 /* Find the bottom line on the screen that has any content.
1505 * If only the top line has content, returns 0.
1506 * If no lines have content, return -1.
1508 static int find_last_nonempty_line(Terminal
* term
, tree234
* screen
)
1511 for (i
= count234(screen
) - 1; i
>= 0; i
--) {
1512 termline
*line
= index234(screen
, i
);
1514 for (j
= 0; j
< line
->cols
; j
++)
1515 if (!termchars_equal(&line
->chars
[j
], &term
->erase_char
))
1517 if (j
!= line
->cols
) break;
1523 * Swap screens. If `reset' is TRUE and we have been asked to
1524 * switch to the alternate screen, we must bring most of its
1525 * configuration from the main screen and erase the contents of the
1526 * alternate screen completely. (This is even true if we're already
1527 * on it! Blame xterm.)
1529 static void swap_screen(Terminal
*term
, int which
, int reset
, int keep_cur_pos
)
1535 reset
= FALSE
; /* do no weird resetting if which==0 */
1537 if (which
!= term
->alt_which
) {
1538 term
->alt_which
= which
;
1540 ttr
= term
->alt_screen
;
1541 term
->alt_screen
= term
->screen
;
1543 term
->alt_sblines
= find_last_nonempty_line(term
, term
->alt_screen
) + 1;
1545 if (!reset
&& !keep_cur_pos
)
1546 term
->curs
.x
= term
->alt_x
;
1549 if (!reset
&& !keep_cur_pos
)
1550 term
->curs
.y
= term
->alt_y
;
1553 if (!reset
) term
->marg_t
= term
->alt_t
;
1556 if (!reset
) term
->marg_b
= term
->alt_b
;
1559 if (!reset
) term
->dec_om
= term
->alt_om
;
1562 if (!reset
) term
->wrap
= term
->alt_wrap
;
1565 if (!reset
) term
->wrapnext
= term
->alt_wnext
;
1566 term
->alt_wnext
= t
;
1568 if (!reset
) term
->insert
= term
->alt_ins
;
1571 if (!reset
) term
->cset
= term
->alt_cset
;
1574 if (!reset
) term
->utf
= term
->alt_utf
;
1577 if (!reset
) term
->sco_acs
= term
->alt_sco_acs
;
1578 term
->alt_sco_acs
= t
;
1581 if (reset
&& term
->screen
) {
1583 * Yes, this _is_ supposed to honour background-colour-erase.
1585 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1590 * Update the scroll bar.
1592 static void update_sbar(Terminal
*term
)
1594 int nscroll
= sblines(term
);
1595 set_sbar(term
->frontend
, nscroll
+ term
->rows
,
1596 nscroll
+ term
->disptop
, term
->rows
);
1600 * Check whether the region bounded by the two pointers intersects
1601 * the scroll region, and de-select the on-screen selection if so.
1603 static void check_selection(Terminal
*term
, pos from
, pos to
)
1605 if (poslt(from
, term
->selend
) && poslt(term
->selstart
, to
))
1610 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1611 * for backward.) `sb' is TRUE if the scrolling is permitted to
1612 * affect the scrollback buffer.
1614 static void scroll(Terminal
*term
, int topline
, int botline
, int lines
, int sb
)
1617 int i
, seltop
, olddisptop
, shift
;
1619 if (topline
!= 0 || term
->alt_which
!= 0)
1622 olddisptop
= term
->disptop
;
1626 line
= delpos234(term
->screen
, botline
);
1627 resizeline(term
, line
, term
->cols
);
1628 for (i
= 0; i
< term
->cols
; i
++)
1629 copy_termchar(line
, i
, &term
->erase_char
);
1630 line
->lattr
= LATTR_NORM
;
1631 addpos234(term
->screen
, line
, topline
);
1633 if (term
->selstart
.y
>= topline
&& term
->selstart
.y
<= botline
) {
1635 if (term
->selstart
.y
> botline
) {
1636 term
->selstart
.y
= botline
+ 1;
1637 term
->selstart
.x
= 0;
1640 if (term
->selend
.y
>= topline
&& term
->selend
.y
<= botline
) {
1642 if (term
->selend
.y
> botline
) {
1643 term
->selend
.y
= botline
+ 1;
1652 line
= delpos234(term
->screen
, topline
);
1653 cc_check(line
); /* XXX-REMOVE-BEFORE-RELEASE */
1654 if (sb
&& term
->savelines
> 0) {
1655 int sblen
= count234(term
->scrollback
);
1657 * We must add this line to the scrollback. We'll
1658 * remove a line from the top of the scrollback if
1659 * the scrollback is full.
1661 if (sblen
== term
->savelines
) {
1662 unsigned char *cline
;
1665 cline
= delpos234(term
->scrollback
, 0);
1668 term
->tempsblines
+= 1;
1670 addpos234(term
->scrollback
, compressline(line
), sblen
);
1672 /* now `line' itself can be reused as the bottom line */
1675 * If the user is currently looking at part of the
1676 * scrollback, and they haven't enabled any options
1677 * that are going to reset the scrollback as a
1678 * result of this movement, then the chances are
1679 * they'd like to keep looking at the same line. So
1680 * we move their viewpoint at the same rate as the
1681 * scroll, at least until their viewpoint hits the
1682 * top end of the scrollback buffer, at which point
1683 * we don't have the choice any more.
1685 * Thanks to Jan Holmen Holsten for the idea and
1686 * initial implementation.
1688 if (term
->disptop
> -term
->savelines
&& term
->disptop
< 0)
1691 resizeline(term
, line
, term
->cols
);
1692 for (i
= 0; i
< term
->cols
; i
++)
1693 copy_termchar(line
, i
, &term
->erase_char
);
1694 line
->lattr
= LATTR_NORM
;
1695 addpos234(term
->screen
, line
, botline
);
1698 * If the selection endpoints move into the scrollback,
1699 * we keep them moving until they hit the top. However,
1700 * of course, if the line _hasn't_ moved into the
1701 * scrollback then we don't do this, and cut them off
1702 * at the top of the scroll region.
1704 * This applies to selstart and selend (for an existing
1705 * selection), and also selanchor (for one being
1706 * selected as we speak).
1708 seltop
= sb ?
-term
->savelines
: topline
;
1710 if (term
->selstate
!= NO_SELECTION
) {
1711 if (term
->selstart
.y
>= seltop
&&
1712 term
->selstart
.y
<= botline
) {
1714 if (term
->selstart
.y
< seltop
) {
1715 term
->selstart
.y
= seltop
;
1716 term
->selstart
.x
= 0;
1719 if (term
->selend
.y
>= seltop
&& term
->selend
.y
<= botline
) {
1721 if (term
->selend
.y
< seltop
) {
1722 term
->selend
.y
= seltop
;
1726 if (term
->selanchor
.y
>= seltop
&&
1727 term
->selanchor
.y
<= botline
) {
1728 term
->selanchor
.y
--;
1729 if (term
->selanchor
.y
< seltop
) {
1730 term
->selanchor
.y
= seltop
;
1731 term
->selanchor
.x
= 0;
1739 #ifdef OPTIMISE_SCROLL
1740 shift
+= term
->disptop
- olddisptop
;
1741 if (shift
< term
->rows
&& shift
> -term
->rows
&& shift
!= 0)
1742 scroll_display(term
, topline
, botline
, shift
);
1743 #endif /* OPTIMISE_SCROLL */
1746 #ifdef OPTIMISE_SCROLL
1748 * Add a scroll of a region on the screen into the pending scroll list.
1749 * `lines' is +ve for scrolling forward, -ve for backward.
1751 * If the scroll is on the same area as the last scroll in the list,
1754 static void save_scroll(Terminal
*term
, int topline
, int botline
, int lines
)
1756 struct scrollregion
*newscroll
;
1757 if (term
->scrolltail
&&
1758 term
->scrolltail
->topline
== topline
&&
1759 term
->scrolltail
->botline
== botline
) {
1760 term
->scrolltail
->lines
+= lines
;
1762 newscroll
= snew(struct scrollregion
);
1763 newscroll
->topline
= topline
;
1764 newscroll
->botline
= botline
;
1765 newscroll
->lines
= lines
;
1766 newscroll
->next
= NULL
;
1768 if (!term
->scrollhead
)
1769 term
->scrollhead
= newscroll
;
1771 term
->scrolltail
->next
= newscroll
;
1772 term
->scrolltail
= newscroll
;
1777 * Scroll the physical display, and our conception of it in disptext.
1779 static void scroll_display(Terminal
*term
, int topline
, int botline
, int lines
)
1781 int distance
, nlines
, i
, j
;
1783 distance
= lines
> 0 ? lines
: -lines
;
1784 nlines
= botline
- topline
+ 1 - distance
;
1786 for (i
= 0; i
< nlines
; i
++)
1787 for (j
= 0; j
< term
->cols
; j
++)
1788 copy_termchar(term
->disptext
[start
+i
], j
,
1789 term
->disptext
[start
+i
+distance
]->chars
+j
);
1790 if (term
->dispcursy
>= 0 &&
1791 term
->dispcursy
>= topline
+ distance
&&
1792 term
->dispcursy
< topline
+ distance
+ nlines
)
1793 term
->dispcursy
-= distance
;
1794 for (i
= 0; i
< distance
; i
++)
1795 for (j
= 0; j
< term
->cols
; j
++)
1796 term
->disptext
[start
+nlines
+i
]->chars
[j
].attr
|= ATTR_INVALID
;
1798 for (i
= nlines
; i
-- ;)
1799 for (j
= 0; j
< term
->cols
; j
++)
1800 copy_termchar(term
->disptext
[start
+i
+distance
], j
,
1801 term
->disptext
[start
+i
]->chars
+j
);
1802 if (term
->dispcursy
>= 0 &&
1803 term
->dispcursy
>= topline
&&
1804 term
->dispcursy
< topline
+ nlines
)
1805 term
->dispcursy
+= distance
;
1806 for (i
= 0; i
< distance
; i
++)
1807 for (j
= 0; j
< term
->cols
; j
++)
1808 term
->disptext
[start
+i
]->chars
[j
].attr
|= ATTR_INVALID
;
1810 save_scroll(term
, topline
, botline
, lines
);
1812 #endif /* OPTIMISE_SCROLL */
1815 * Move the cursor to a given position, clipping at boundaries. We
1816 * may or may not want to clip at the scroll margin: marg_clip is 0
1817 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
1818 * even _being_ outside the margins.
1820 static void move(Terminal
*term
, int x
, int y
, int marg_clip
)
1824 if (x
>= term
->cols
)
1827 if ((term
->curs
.y
>= term
->marg_t
|| marg_clip
== 2) &&
1830 if ((term
->curs
.y
<= term
->marg_b
|| marg_clip
== 2) &&
1836 if (y
>= term
->rows
)
1840 term
->wrapnext
= FALSE
;
1844 * Save or restore the cursor and SGR mode.
1846 static void save_cursor(Terminal
*term
, int save
)
1849 term
->savecurs
= term
->curs
;
1850 term
->save_attr
= term
->curr_attr
;
1851 term
->save_cset
= term
->cset
;
1852 term
->save_utf
= term
->utf
;
1853 term
->save_wnext
= term
->wrapnext
;
1854 term
->save_csattr
= term
->cset_attr
[term
->cset
];
1855 term
->save_sco_acs
= term
->sco_acs
;
1857 term
->curs
= term
->savecurs
;
1858 /* Make sure the window hasn't shrunk since the save */
1859 if (term
->curs
.x
>= term
->cols
)
1860 term
->curs
.x
= term
->cols
- 1;
1861 if (term
->curs
.y
>= term
->rows
)
1862 term
->curs
.y
= term
->rows
- 1;
1864 term
->curr_attr
= term
->save_attr
;
1865 term
->cset
= term
->save_cset
;
1866 term
->utf
= term
->save_utf
;
1867 term
->wrapnext
= term
->save_wnext
;
1869 * wrapnext might reset to False if the x position is no
1870 * longer at the rightmost edge.
1872 if (term
->wrapnext
&& term
->curs
.x
< term
->cols
-1)
1873 term
->wrapnext
= FALSE
;
1874 term
->cset_attr
[term
->cset
] = term
->save_csattr
;
1875 term
->sco_acs
= term
->save_sco_acs
;
1876 set_erase_char(term
);
1881 * This function is called before doing _anything_ which affects
1882 * only part of a line of text. It is used to mark the boundary
1883 * between two character positions, and it indicates that some sort
1884 * of effect is going to happen on only one side of that boundary.
1886 * The effect of this function is to check whether a CJK
1887 * double-width character is straddling the boundary, and to remove
1888 * it and replace it with two spaces if so. (Of course, one or
1889 * other of those spaces is then likely to be replaced with
1890 * something else again, as a result of whatever happens next.)
1892 * Also, if the boundary is at the right-hand _edge_ of the screen,
1893 * it implies something deliberate is being done to the rightmost
1894 * column position; hence we must clear LATTR_WRAPPED2.
1896 * The input to the function is the coordinates of the _second_
1897 * character of the pair.
1899 static void check_boundary(Terminal
*term
, int x
, int y
)
1903 /* Validate input coordinates, just in case. */
1904 if (x
== 0 || x
> term
->cols
)
1907 ldata
= scrlineptr(y
);
1908 if (x
== term
->cols
) {
1909 ldata
->lattr
&= ~LATTR_WRAPPED2
;
1911 if (ldata
->chars
[x
].chr
== UCSWIDE
) {
1912 clear_cc(ldata
, x
-1);
1914 ldata
->chars
[x
-1].chr
= ' ' | CSET_ASCII
;
1915 ldata
->chars
[x
] = ldata
->chars
[x
-1];
1921 * Erase a large portion of the screen: the whole screen, or the
1922 * whole line, or parts thereof.
1924 static void erase_lots(Terminal
*term
,
1925 int line_only
, int from_begin
, int to_end
)
1929 int erasing_lines_from_top
= 0;
1932 start
.y
= term
->curs
.y
;
1934 end
.y
= term
->curs
.y
+ 1;
1936 erase_lattr
= FALSE
;
1951 if (!from_begin
|| !to_end
)
1952 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1953 check_selection(term
, start
, end
);
1955 /* Clear screen also forces a full window redraw, just in case. */
1956 if (start
.y
== 0 && start
.x
== 0 && end
.y
== term
->rows
)
1957 term_invalidate(term
);
1959 /* Lines scrolled away shouldn't be brought back on if the terminal
1961 if (start
.y
== 0 && start
.x
== 0 && end
.x
== 0 && erase_lattr
)
1962 erasing_lines_from_top
= 1;
1964 if (term
->cfg
.erase_to_scrollback
&& erasing_lines_from_top
) {
1965 /* If it's a whole number of lines, starting at the top, and
1966 * we're fully erasing them, erase by scrolling and keep the
1967 * lines in the scrollback. */
1968 int scrolllines
= end
.y
;
1969 if (end
.y
== term
->rows
) {
1970 /* Shrink until we find a non-empty row.*/
1971 scrolllines
= find_last_nonempty_line(term
, term
->screen
) + 1;
1973 if (scrolllines
> 0)
1974 scroll(term
, 0, scrolllines
- 1, scrolllines
, TRUE
);
1976 termline
*ldata
= scrlineptr(start
.y
);
1977 while (poslt(start
, end
)) {
1978 if (start
.x
== term
->cols
) {
1980 ldata
->lattr
&= ~(LATTR_WRAPPED
| LATTR_WRAPPED2
);
1982 ldata
->lattr
= LATTR_NORM
;
1984 copy_termchar(ldata
, start
.x
, &term
->erase_char
);
1986 if (incpos(start
) && start
.y
< term
->rows
) {
1987 ldata
= scrlineptr(start
.y
);
1992 /* After an erase of lines from the top of the screen, we shouldn't
1993 * bring the lines back again if the terminal enlarges (since the user or
1994 * application has explictly thrown them away). */
1995 if (erasing_lines_from_top
&& !(term
->alt_which
))
1996 term
->tempsblines
= 0;
2000 * Insert or delete characters within the current line. n is +ve if
2001 * insertion is desired, and -ve for deletion.
2003 static void insch(Terminal
*term
, int n
)
2005 int dir
= (n
< 0 ?
-1 : +1);
2010 n
= (n
< 0 ?
-n
: n
);
2011 if (n
> term
->cols
- term
->curs
.x
)
2012 n
= term
->cols
- term
->curs
.x
;
2013 m
= term
->cols
- term
->curs
.x
- n
;
2014 cursplus
.y
= term
->curs
.y
;
2015 cursplus
.x
= term
->curs
.x
+ n
;
2016 check_selection(term
, term
->curs
, cursplus
);
2017 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2019 check_boundary(term
, term
->curs
.x
+ n
, term
->curs
.y
);
2020 ldata
= scrlineptr(term
->curs
.y
);
2022 for (j
= 0; j
< m
; j
++)
2023 move_termchar(ldata
,
2024 ldata
->chars
+ term
->curs
.x
+ j
,
2025 ldata
->chars
+ term
->curs
.x
+ j
+ n
);
2027 copy_termchar(ldata
, term
->curs
.x
+ m
++, &term
->erase_char
);
2030 move_termchar(ldata
,
2031 ldata
->chars
+ term
->curs
.x
+ j
+ n
,
2032 ldata
->chars
+ term
->curs
.x
+ j
);
2034 copy_termchar(ldata
, term
->curs
.x
+ n
, &term
->erase_char
);
2039 * Toggle terminal mode `mode' to state `state'. (`query' indicates
2040 * whether the mode is a DEC private one or a normal one.)
2042 static void toggle_mode(Terminal
*term
, int mode
, int query
, int state
)
2044 unsigned long ticks
;
2048 case 1: /* DECCKM: application cursor keys */
2049 term
->app_cursor_keys
= state
;
2051 case 2: /* DECANM: VT52 mode */
2052 term
->vt52_mode
= !state
;
2053 if (term
->vt52_mode
) {
2054 term
->blink_is_real
= FALSE
;
2055 term
->vt52_bold
= FALSE
;
2057 term
->blink_is_real
= term
->cfg
.blinktext
;
2060 case 3: /* DECCOLM: 80/132 columns */
2062 if (!term
->cfg
.no_remote_resize
)
2063 request_resize(term
->frontend
, state ?
132 : 80, term
->rows
);
2064 term
->reset_132
= state
;
2065 term
->alt_t
= term
->marg_t
= 0;
2066 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
2067 move(term
, 0, 0, 0);
2068 erase_lots(term
, FALSE
, TRUE
, TRUE
);
2070 case 5: /* DECSCNM: reverse video */
2072 * Toggle reverse video. If we receive an OFF within the
2073 * visual bell timeout period after an ON, we trigger an
2074 * effective visual bell, so that ESC[?5hESC[?5l will
2075 * always be an actually _visible_ visual bell.
2077 ticks
= GETTICKCOUNT();
2078 /* turn off a previous vbell to avoid inconsistencies */
2079 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
2080 term
->in_vbell
= FALSE
;
2081 if (term
->rvideo
&& !state
&& /* we're turning it off... */
2082 (ticks
- term
->rvbell_startpoint
) < VBELL_TIMEOUT
) {/*...soon*/
2083 /* If there's no vbell timeout already, or this one lasts
2084 * longer, replace vbell_timeout with ours. */
2085 if (!term
->in_vbell
||
2086 (term
->rvbell_startpoint
- term
->vbell_startpoint
<
2088 term
->vbell_startpoint
= term
->rvbell_startpoint
;
2089 term
->in_vbell
= TRUE
; /* may clear rvideo but set in_vbell */
2090 } else if (!term
->rvideo
&& state
) {
2091 /* This is an ON, so we notice the time and save it. */
2092 term
->rvbell_startpoint
= ticks
;
2094 term
->rvideo
= state
;
2095 term
->seen_disp_event
= TRUE
;
2099 case 6: /* DECOM: DEC origin mode */
2100 term
->dec_om
= state
;
2102 case 7: /* DECAWM: auto wrap */
2105 case 8: /* DECARM: auto key repeat */
2106 term
->repeat_off
= !state
;
2108 case 10: /* DECEDM: set local edit mode */
2109 term
->term_editing
= state
;
2110 if (term
->ldisc
) /* cause ldisc to notice changes */
2111 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2113 case 25: /* DECTCEM: enable/disable cursor */
2114 compatibility2(OTHER
, VT220
);
2115 term
->cursor_on
= state
;
2116 term
->seen_disp_event
= TRUE
;
2118 case 47: /* alternate screen */
2119 compatibility(OTHER
);
2121 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, FALSE
, FALSE
);
2124 case 1000: /* xterm mouse 1 */
2125 term
->xterm_mouse
= state ?
1 : 0;
2126 set_raw_mouse_mode(term
->frontend
, state
);
2128 case 1002: /* xterm mouse 2 */
2129 term
->xterm_mouse
= state ?
2 : 0;
2130 set_raw_mouse_mode(term
->frontend
, state
);
2132 case 1047: /* alternate screen */
2133 compatibility(OTHER
);
2135 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, TRUE
, TRUE
);
2138 case 1048: /* save/restore cursor */
2139 if (!term
->cfg
.no_alt_screen
)
2140 save_cursor(term
, state
);
2141 if (!state
) term
->seen_disp_event
= TRUE
;
2143 case 1049: /* cursor & alternate screen */
2144 if (state
&& !term
->cfg
.no_alt_screen
)
2145 save_cursor(term
, state
);
2146 if (!state
) term
->seen_disp_event
= TRUE
;
2147 compatibility(OTHER
);
2149 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, TRUE
, FALSE
);
2150 if (!state
&& !term
->cfg
.no_alt_screen
)
2151 save_cursor(term
, state
);
2156 case 4: /* IRM: set insert mode */
2157 compatibility(VT102
);
2158 term
->insert
= state
;
2160 case 12: /* SRM: set echo mode */
2161 term
->term_echoing
= !state
;
2162 if (term
->ldisc
) /* cause ldisc to notice changes */
2163 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2165 case 20: /* LNM: Return sends ... */
2166 term
->cr_lf_return
= state
;
2168 case 34: /* WYULCURM: Make cursor BIG */
2169 compatibility2(OTHER
, VT220
);
2170 term
->big_cursor
= !state
;
2175 * Process an OSC sequence: set window title or icon name.
2177 static void do_osc(Terminal
*term
)
2180 while (term
->osc_strlen
--)
2181 term
->wordness
[(unsigned char)
2182 term
->osc_string
[term
->osc_strlen
]] = term
->esc_args
[0];
2184 term
->osc_string
[term
->osc_strlen
] = '\0';
2185 switch (term
->esc_args
[0]) {
2188 if (!term
->cfg
.no_remote_wintitle
)
2189 set_icon(term
->frontend
, term
->osc_string
);
2190 if (term
->esc_args
[0] == 1)
2192 /* fall through: parameter 0 means set both */
2195 if (!term
->cfg
.no_remote_wintitle
)
2196 set_title(term
->frontend
, term
->osc_string
);
2203 * ANSI printing routines.
2205 static void term_print_setup(Terminal
*term
)
2207 bufchain_clear(&term
->printer_buf
);
2208 term
->print_job
= printer_start_job(term
->cfg
.printer
);
2210 static void term_print_flush(Terminal
*term
)
2215 while ((size
= bufchain_size(&term
->printer_buf
)) > 5) {
2216 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
2219 printer_job_data(term
->print_job
, data
, len
);
2220 bufchain_consume(&term
->printer_buf
, len
);
2223 static void term_print_finish(Terminal
*term
)
2229 if (!term
->printing
&& !term
->only_printing
)
2230 return; /* we need do nothing */
2232 term_print_flush(term
);
2233 while ((size
= bufchain_size(&term
->printer_buf
)) > 0) {
2234 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
2236 if (c
== '\033' || c
== '\233') {
2237 bufchain_consume(&term
->printer_buf
, size
);
2240 printer_job_data(term
->print_job
, &c
, 1);
2241 bufchain_consume(&term
->printer_buf
, 1);
2244 printer_finish_job(term
->print_job
);
2245 term
->print_job
= NULL
;
2246 term
->printing
= term
->only_printing
= FALSE
;
2250 * Remove everything currently in `inbuf' and stick it up on the
2251 * in-memory display. There's a big state machine in here to
2252 * process escape sequences...
2254 void term_out(Terminal
*term
)
2258 unsigned char localbuf
[256], *chars
;
2263 chars
= NULL
; /* placate compiler warnings */
2264 while (nchars
> 0 || bufchain_size(&term
->inbuf
) > 0) {
2268 bufchain_prefix(&term
->inbuf
, &ret
, &nchars
);
2269 if (nchars
> sizeof(localbuf
))
2270 nchars
= sizeof(localbuf
);
2271 memcpy(localbuf
, ret
, nchars
);
2272 bufchain_consume(&term
->inbuf
, nchars
);
2274 assert(chars
!= NULL
);
2280 * Optionally log the session traffic to a file. Useful for
2281 * debugging and possibly also useful for actual logging.
2283 if (term
->cfg
.logtype
== LGTYP_DEBUG
&& term
->logctx
)
2284 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_DEBUG
);
2290 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
2291 * be able to display 8-bit characters, but I'll let that go 'cause
2296 * If we're printing, add the character to the printer
2299 if (term
->printing
) {
2300 bufchain_add(&term
->printer_buf
, &c
, 1);
2303 * If we're in print-only mode, we use a much simpler
2304 * state machine designed only to recognise the ESC[4i
2305 * termination sequence.
2307 if (term
->only_printing
) {
2309 term
->print_state
= 1;
2310 else if (c
== (unsigned char)'\233')
2311 term
->print_state
= 2;
2312 else if (c
== '[' && term
->print_state
== 1)
2313 term
->print_state
= 2;
2314 else if (c
== '4' && term
->print_state
== 2)
2315 term
->print_state
= 3;
2316 else if (c
== 'i' && term
->print_state
== 3)
2317 term
->print_state
= 4;
2319 term
->print_state
= 0;
2320 if (term
->print_state
== 4) {
2321 term_print_finish(term
);
2327 /* First see about all those translations. */
2328 if (term
->termstate
== TOPLEVEL
) {
2330 switch (term
->utf_state
) {
2333 /* UTF-8 must be stateless so we ignore iso2022. */
2334 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2335 c
= term
->ucsdata
->unitab_ctrl
[c
];
2336 else c
= ((unsigned char)c
) | CSET_ASCII
;
2338 } else if ((c
& 0xe0) == 0xc0) {
2339 term
->utf_size
= term
->utf_state
= 1;
2340 term
->utf_char
= (c
& 0x1f);
2341 } else if ((c
& 0xf0) == 0xe0) {
2342 term
->utf_size
= term
->utf_state
= 2;
2343 term
->utf_char
= (c
& 0x0f);
2344 } else if ((c
& 0xf8) == 0xf0) {
2345 term
->utf_size
= term
->utf_state
= 3;
2346 term
->utf_char
= (c
& 0x07);
2347 } else if ((c
& 0xfc) == 0xf8) {
2348 term
->utf_size
= term
->utf_state
= 4;
2349 term
->utf_char
= (c
& 0x03);
2350 } else if ((c
& 0xfe) == 0xfc) {
2351 term
->utf_size
= term
->utf_state
= 5;
2352 term
->utf_char
= (c
& 0x01);
2363 if ((c
& 0xC0) != 0x80) {
2366 term
->utf_state
= 0;
2369 term
->utf_char
= (term
->utf_char
<< 6) | (c
& 0x3f);
2370 if (--term
->utf_state
)
2375 /* Is somebody trying to be evil! */
2377 (c
< 0x800 && term
->utf_size
>= 2) ||
2378 (c
< 0x10000 && term
->utf_size
>= 3) ||
2379 (c
< 0x200000 && term
->utf_size
>= 4) ||
2380 (c
< 0x4000000 && term
->utf_size
>= 5))
2383 /* Unicode line separator and paragraph separator are CR-LF */
2384 if (c
== 0x2028 || c
== 0x2029)
2387 /* High controls are probably a Baaad idea too. */
2391 /* The UTF-16 surrogates are not nice either. */
2392 /* The standard give the option of decoding these:
2393 * I don't want to! */
2394 if (c
>= 0xD800 && c
< 0xE000)
2397 /* ISO 10646 characters now limited to UTF-16 range. */
2401 /* This is currently a TagPhobic application.. */
2402 if (c
>= 0xE0000 && c
<= 0xE007F)
2405 /* U+FEFF is best seen as a null. */
2408 /* But U+FFFE is an error. */
2409 if (c
== 0xFFFE || c
== 0xFFFF)
2414 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
2415 else if(term
->sco_acs
&&
2416 (c
!='\033' && c
!='\012' && c
!='\015' && c
!='\b'))
2418 if (term
->sco_acs
== 2) c
|= 0x80;
2421 switch (term
->cset_attr
[term
->cset
]) {
2423 * Linedraw characters are different from 'ESC ( B'
2424 * only for a small range. For ones outside that
2425 * range, make sure we use the same font as well as
2426 * the same encoding.
2429 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2430 c
= term
->ucsdata
->unitab_ctrl
[c
];
2432 c
= ((unsigned char) c
) | CSET_LINEDRW
;
2436 /* If UK-ASCII, make the '#' a LineDraw Pound */
2438 c
= '}' | CSET_LINEDRW
;
2441 /*FALLTHROUGH*/ case CSET_ASCII
:
2442 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2443 c
= term
->ucsdata
->unitab_ctrl
[c
];
2445 c
= ((unsigned char) c
) | CSET_ASCII
;
2448 if (c
>=' ') c
= ((unsigned char)c
) | CSET_SCOACS
;
2454 /* How about C1 controls ? */
2455 if ((c
& -32) == 0x80 && term
->termstate
< DO_CTRLS
&&
2456 !term
->vt52_mode
&& has_compat(VT220
)) {
2457 term
->termstate
= SEEN_ESC
;
2458 term
->esc_query
= FALSE
;
2459 c
= '@' + (c
& 0x1F);
2462 /* Or the GL control. */
2463 if (c
== '\177' && term
->termstate
< DO_CTRLS
&& has_compat(OTHER
)) {
2464 if (term
->curs
.x
&& !term
->wrapnext
)
2466 term
->wrapnext
= FALSE
;
2467 /* destructive backspace might be disabled */
2468 if (!term
->cfg
.no_dbackspace
) {
2469 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2470 check_boundary(term
, term
->curs
.x
+1, term
->curs
.y
);
2471 copy_termchar(scrlineptr(term
->curs
.y
),
2472 term
->curs
.x
, &term
->erase_char
);
2475 /* Or normal C0 controls. */
2476 if ((c
& ~0x1F) == 0 && term
->termstate
< DO_CTRLS
) {
2478 case '\005': /* ENQ: terminal type query */
2479 /* Strictly speaking this is VT100 but a VT100 defaults to
2480 * no response. Other terminals respond at their option.
2482 * Don't put a CR in the default string as this tends to
2483 * upset some weird software.
2485 * An xterm returns "xterm" (5 characters)
2487 compatibility(ANSIMIN
);
2489 char abuf
[256], *s
, *d
;
2491 for (s
= term
->cfg
.answerback
, d
= abuf
; *s
; s
++) {
2493 if (*s
>= 'a' && *s
<= 'z')
2494 *d
++ = (*s
- ('a' - 1));
2495 else if ((*s
>= '@' && *s
<= '_') ||
2496 *s
== '?' || (*s
& 0x80))
2501 } else if (*s
== '^') {
2506 lpage_send(term
->ldisc
, DEFAULT_CODEPAGE
,
2510 case '\007': /* BEL: Bell */
2512 struct beeptime
*newbeep
;
2513 unsigned long ticks
;
2515 ticks
= GETTICKCOUNT();
2517 if (!term
->beep_overloaded
) {
2518 newbeep
= snew(struct beeptime
);
2519 newbeep
->ticks
= ticks
;
2520 newbeep
->next
= NULL
;
2521 if (!term
->beephead
)
2522 term
->beephead
= newbeep
;
2524 term
->beeptail
->next
= newbeep
;
2525 term
->beeptail
= newbeep
;
2530 * Throw out any beeps that happened more than
2533 while (term
->beephead
&&
2534 term
->beephead
->ticks
< ticks
- term
->cfg
.bellovl_t
) {
2535 struct beeptime
*tmp
= term
->beephead
;
2536 term
->beephead
= tmp
->next
;
2538 if (!term
->beephead
)
2539 term
->beeptail
= NULL
;
2543 if (term
->cfg
.bellovl
&& term
->beep_overloaded
&&
2544 ticks
- term
->lastbeep
>= (unsigned)term
->cfg
.bellovl_s
) {
2546 * If we're currently overloaded and the
2547 * last beep was more than s seconds ago,
2548 * leave overload mode.
2550 term
->beep_overloaded
= FALSE
;
2551 } else if (term
->cfg
.bellovl
&& !term
->beep_overloaded
&&
2552 term
->nbeeps
>= term
->cfg
.bellovl_n
) {
2554 * Now, if we have n or more beeps
2555 * remaining in the queue, go into overload
2558 term
->beep_overloaded
= TRUE
;
2560 term
->lastbeep
= ticks
;
2563 * Perform an actual beep if we're not overloaded.
2565 if (!term
->cfg
.bellovl
|| !term
->beep_overloaded
) {
2566 beep(term
->frontend
, term
->cfg
.beep
);
2567 if (term
->cfg
.beep
== BELL_VISUAL
) {
2568 term
->in_vbell
= TRUE
;
2569 term
->vbell_startpoint
= ticks
;
2573 term
->seen_disp_event
= TRUE
;
2576 case '\b': /* BS: Back space */
2577 if (term
->curs
.x
== 0 &&
2578 (term
->curs
.y
== 0 || term
->wrap
== 0))
2580 else if (term
->curs
.x
== 0 && term
->curs
.y
> 0)
2581 term
->curs
.x
= term
->cols
- 1, term
->curs
.y
--;
2582 else if (term
->wrapnext
)
2583 term
->wrapnext
= FALSE
;
2586 term
->seen_disp_event
= TRUE
;
2588 case '\016': /* LS1: Locking-shift one */
2589 compatibility(VT100
);
2592 case '\017': /* LS0: Locking-shift zero */
2593 compatibility(VT100
);
2596 case '\033': /* ESC: Escape */
2597 if (term
->vt52_mode
)
2598 term
->termstate
= VT52_ESC
;
2600 compatibility(ANSIMIN
);
2601 term
->termstate
= SEEN_ESC
;
2602 term
->esc_query
= FALSE
;
2605 case '\015': /* CR: Carriage return */
2607 term
->wrapnext
= FALSE
;
2608 term
->seen_disp_event
= TRUE
;
2609 term
->paste_hold
= 0;
2611 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
2613 case '\014': /* FF: Form feed */
2614 if (has_compat(SCOANSI
)) {
2615 move(term
, 0, 0, 0);
2616 erase_lots(term
, FALSE
, FALSE
, TRUE
);
2618 term
->wrapnext
= FALSE
;
2619 term
->seen_disp_event
= 1;
2622 case '\013': /* VT: Line tabulation */
2623 compatibility(VT100
);
2624 case '\012': /* LF: Line feed */
2625 if (term
->curs
.y
== term
->marg_b
)
2626 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2627 else if (term
->curs
.y
< term
->rows
- 1)
2629 if (term
->cfg
.lfhascr
)
2631 term
->wrapnext
= FALSE
;
2632 term
->seen_disp_event
= 1;
2633 term
->paste_hold
= 0;
2635 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
2637 case '\t': /* HT: Character tabulation */
2639 pos old_curs
= term
->curs
;
2640 termline
*ldata
= scrlineptr(term
->curs
.y
);
2644 } while (term
->curs
.x
< term
->cols
- 1 &&
2645 !term
->tabs
[term
->curs
.x
]);
2647 if ((ldata
->lattr
& LATTR_MODE
) != LATTR_NORM
) {
2648 if (term
->curs
.x
>= term
->cols
/ 2)
2649 term
->curs
.x
= term
->cols
/ 2 - 1;
2651 if (term
->curs
.x
>= term
->cols
)
2652 term
->curs
.x
= term
->cols
- 1;
2655 check_selection(term
, old_curs
, term
->curs
);
2657 term
->seen_disp_event
= TRUE
;
2661 switch (term
->termstate
) {
2663 /* Only graphic characters get this far;
2664 * ctrls are stripped above */
2666 termline
*cline
= scrlineptr(term
->curs
.y
);
2671 width
= wcwidth((wchar_t) c
);
2673 if (term
->wrapnext
&& term
->wrap
&& width
> 0) {
2674 cline
->lattr
|= LATTR_WRAPPED
;
2675 if (term
->curs
.y
== term
->marg_b
)
2676 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2677 else if (term
->curs
.y
< term
->rows
- 1)
2680 term
->wrapnext
= FALSE
;
2681 cline
= scrlineptr(term
->curs
.y
);
2683 if (term
->insert
&& width
> 0)
2685 if (term
->selstate
!= NO_SELECTION
) {
2686 pos cursplus
= term
->curs
;
2688 check_selection(term
, term
->curs
, cursplus
);
2690 if (((c
& CSET_MASK
) == CSET_ASCII
||
2691 (c
& CSET_MASK
) == 0) &&
2693 logtraffic(term
->logctx
, (unsigned char) c
,
2699 * If we're about to display a double-width
2700 * character starting in the rightmost
2701 * column, then we do something special
2702 * instead. We must print a space in the
2703 * last column of the screen, then wrap;
2704 * and we also set LATTR_WRAPPED2 which
2705 * instructs subsequent cut-and-pasting not
2706 * only to splice this line to the one
2707 * after it, but to ignore the space in the
2708 * last character position as well.
2709 * (Because what was actually output to the
2710 * terminal was presumably just a sequence
2711 * of CJK characters, and we don't want a
2712 * space to be pasted in the middle of
2713 * those just because they had the
2714 * misfortune to start in the wrong parity
2715 * column. xterm concurs.)
2717 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2718 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
2719 if (term
->curs
.x
== term
->cols
-1) {
2720 copy_termchar(cline
, term
->curs
.x
,
2722 cline
->lattr
|= LATTR_WRAPPED
| LATTR_WRAPPED2
;
2723 if (term
->curs
.y
== term
->marg_b
)
2724 scroll(term
, term
->marg_t
, term
->marg_b
,
2726 else if (term
->curs
.y
< term
->rows
- 1)
2729 /* Now we must check_boundary again, of course. */
2730 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2731 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
2735 clear_cc(cline
, term
->curs
.x
);
2736 cline
->chars
[term
->curs
.x
].chr
= c
;
2737 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
2742 clear_cc(cline
, term
->curs
.x
);
2743 cline
->chars
[term
->curs
.x
].chr
= UCSWIDE
;
2744 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
2748 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2749 check_boundary(term
, term
->curs
.x
+1, term
->curs
.y
);
2752 clear_cc(cline
, term
->curs
.x
);
2753 cline
->chars
[term
->curs
.x
].chr
= c
;
2754 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
2758 if (term
->curs
.x
> 0) {
2759 int x
= term
->curs
.x
- 1;
2761 /* If we're in wrapnext state, the character
2762 * to combine with is _here_, not to our left. */
2767 * If the previous character is
2768 * UCSWIDE, back up another one.
2770 if (cline
->chars
[x
].chr
== UCSWIDE
) {
2775 add_cc(cline
, x
, c
);
2776 term
->seen_disp_event
= 1;
2783 if (term
->curs
.x
== term
->cols
) {
2785 term
->wrapnext
= TRUE
;
2786 if (term
->wrap
&& term
->vt52_mode
) {
2787 cline
->lattr
|= LATTR_WRAPPED
;
2788 if (term
->curs
.y
== term
->marg_b
)
2789 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2790 else if (term
->curs
.y
< term
->rows
- 1)
2793 term
->wrapnext
= FALSE
;
2796 term
->seen_disp_event
= 1;
2802 * This state is virtually identical to SEEN_ESC, with the
2803 * exception that we have an OSC sequence in the pipeline,
2804 * and _if_ we see a backslash, we process it.
2808 term
->termstate
= TOPLEVEL
;
2811 /* else fall through */
2813 if (c
>= ' ' && c
<= '/') {
2814 if (term
->esc_query
)
2815 term
->esc_query
= -1;
2817 term
->esc_query
= c
;
2820 term
->termstate
= TOPLEVEL
;
2821 switch (ANSI(c
, term
->esc_query
)) {
2822 case '[': /* enter CSI mode */
2823 term
->termstate
= SEEN_CSI
;
2824 term
->esc_nargs
= 1;
2825 term
->esc_args
[0] = ARG_DEFAULT
;
2826 term
->esc_query
= FALSE
;
2828 case ']': /* OSC: xterm escape sequences */
2829 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
2830 compatibility(OTHER
);
2831 term
->termstate
= SEEN_OSC
;
2832 term
->esc_args
[0] = 0;
2834 case '7': /* DECSC: save cursor */
2835 compatibility(VT100
);
2836 save_cursor(term
, TRUE
);
2838 case '8': /* DECRC: restore cursor */
2839 compatibility(VT100
);
2840 save_cursor(term
, FALSE
);
2841 term
->seen_disp_event
= TRUE
;
2843 case '=': /* DECKPAM: Keypad application mode */
2844 compatibility(VT100
);
2845 term
->app_keypad_keys
= TRUE
;
2847 case '>': /* DECKPNM: Keypad numeric mode */
2848 compatibility(VT100
);
2849 term
->app_keypad_keys
= FALSE
;
2851 case 'D': /* IND: exactly equivalent to LF */
2852 compatibility(VT100
);
2853 if (term
->curs
.y
== term
->marg_b
)
2854 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2855 else if (term
->curs
.y
< term
->rows
- 1)
2857 term
->wrapnext
= FALSE
;
2858 term
->seen_disp_event
= TRUE
;
2860 case 'E': /* NEL: exactly equivalent to CR-LF */
2861 compatibility(VT100
);
2863 if (term
->curs
.y
== term
->marg_b
)
2864 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2865 else if (term
->curs
.y
< term
->rows
- 1)
2867 term
->wrapnext
= FALSE
;
2868 term
->seen_disp_event
= TRUE
;
2870 case 'M': /* RI: reverse index - backwards LF */
2871 compatibility(VT100
);
2872 if (term
->curs
.y
== term
->marg_t
)
2873 scroll(term
, term
->marg_t
, term
->marg_b
, -1, TRUE
);
2874 else if (term
->curs
.y
> 0)
2876 term
->wrapnext
= FALSE
;
2877 term
->seen_disp_event
= TRUE
;
2879 case 'Z': /* DECID: terminal type query */
2880 compatibility(VT100
);
2882 ldisc_send(term
->ldisc
, term
->id_string
,
2883 strlen(term
->id_string
), 0);
2885 case 'c': /* RIS: restore power-on settings */
2886 compatibility(VT100
);
2888 if (term
->ldisc
) /* cause ldisc to notice changes */
2889 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2890 if (term
->reset_132
) {
2891 if (!term
->cfg
.no_remote_resize
)
2892 request_resize(term
->frontend
, 80, term
->rows
);
2893 term
->reset_132
= 0;
2896 term
->seen_disp_event
= TRUE
;
2898 case 'H': /* HTS: set a tab */
2899 compatibility(VT100
);
2900 term
->tabs
[term
->curs
.x
] = TRUE
;
2903 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
2904 compatibility(VT100
);
2910 for (i
= 0; i
< term
->rows
; i
++) {
2911 ldata
= scrlineptr(i
);
2912 for (j
= 0; j
< term
->cols
; j
++) {
2913 copy_termchar(ldata
, j
,
2914 &term
->basic_erase_char
);
2915 ldata
->chars
[j
].chr
= 'E';
2917 ldata
->lattr
= LATTR_NORM
;
2920 term
->seen_disp_event
= TRUE
;
2921 scrtop
.x
= scrtop
.y
= 0;
2923 scrbot
.y
= term
->rows
;
2924 check_selection(term
, scrtop
, scrbot
);
2928 case ANSI('3', '#'):
2929 case ANSI('4', '#'):
2930 case ANSI('5', '#'):
2931 case ANSI('6', '#'):
2932 compatibility(VT100
);
2936 switch (ANSI(c
, term
->esc_query
)) {
2937 case ANSI('3', '#'): /* DECDHL: 2*height, top */
2940 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
2943 case ANSI('5', '#'): /* DECSWL: normal */
2944 nlattr
= LATTR_NORM
;
2946 default: /* case ANSI('6', '#'): DECDWL: 2*width */
2947 nlattr
= LATTR_WIDE
;
2950 scrlineptr(term
->curs
.y
)->lattr
= nlattr
;
2953 /* GZD4: G0 designate 94-set */
2954 case ANSI('A', '('):
2955 compatibility(VT100
);
2956 if (!term
->cfg
.no_remote_charset
)
2957 term
->cset_attr
[0] = CSET_GBCHR
;
2959 case ANSI('B', '('):
2960 compatibility(VT100
);
2961 if (!term
->cfg
.no_remote_charset
)
2962 term
->cset_attr
[0] = CSET_ASCII
;
2964 case ANSI('0', '('):
2965 compatibility(VT100
);
2966 if (!term
->cfg
.no_remote_charset
)
2967 term
->cset_attr
[0] = CSET_LINEDRW
;
2969 case ANSI('U', '('):
2970 compatibility(OTHER
);
2971 if (!term
->cfg
.no_remote_charset
)
2972 term
->cset_attr
[0] = CSET_SCOACS
;
2974 /* G1D4: G1-designate 94-set */
2975 case ANSI('A', ')'):
2976 compatibility(VT100
);
2977 if (!term
->cfg
.no_remote_charset
)
2978 term
->cset_attr
[1] = CSET_GBCHR
;
2980 case ANSI('B', ')'):
2981 compatibility(VT100
);
2982 if (!term
->cfg
.no_remote_charset
)
2983 term
->cset_attr
[1] = CSET_ASCII
;
2985 case ANSI('0', ')'):
2986 compatibility(VT100
);
2987 if (!term
->cfg
.no_remote_charset
)
2988 term
->cset_attr
[1] = CSET_LINEDRW
;
2990 case ANSI('U', ')'):
2991 compatibility(OTHER
);
2992 if (!term
->cfg
.no_remote_charset
)
2993 term
->cset_attr
[1] = CSET_SCOACS
;
2995 /* DOCS: Designate other coding system */
2996 case ANSI('8', '%'): /* Old Linux code */
2997 case ANSI('G', '%'):
2998 compatibility(OTHER
);
2999 if (!term
->cfg
.no_remote_charset
)
3002 case ANSI('@', '%'):
3003 compatibility(OTHER
);
3004 if (!term
->cfg
.no_remote_charset
)
3010 term
->termstate
= TOPLEVEL
; /* default */
3012 if (term
->esc_nargs
<= ARGS_MAX
) {
3013 if (term
->esc_args
[term
->esc_nargs
- 1] == ARG_DEFAULT
)
3014 term
->esc_args
[term
->esc_nargs
- 1] = 0;
3015 term
->esc_args
[term
->esc_nargs
- 1] =
3016 10 * term
->esc_args
[term
->esc_nargs
- 1] + c
- '0';
3018 term
->termstate
= SEEN_CSI
;
3019 } else if (c
== ';') {
3020 if (++term
->esc_nargs
<= ARGS_MAX
)
3021 term
->esc_args
[term
->esc_nargs
- 1] = ARG_DEFAULT
;
3022 term
->termstate
= SEEN_CSI
;
3023 } else if (c
< '@') {
3024 if (term
->esc_query
)
3025 term
->esc_query
= -1;
3027 term
->esc_query
= TRUE
;
3029 term
->esc_query
= c
;
3030 term
->termstate
= SEEN_CSI
;
3032 switch (ANSI(c
, term
->esc_query
)) {
3033 case 'A': /* CUU: move up N lines */
3034 move(term
, term
->curs
.x
,
3035 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
3036 term
->seen_disp_event
= TRUE
;
3038 case 'e': /* VPR: move down N lines */
3039 compatibility(ANSI
);
3041 case 'B': /* CUD: Cursor down */
3042 move(term
, term
->curs
.x
,
3043 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
3044 term
->seen_disp_event
= TRUE
;
3046 case ANSI('c', '>'): /* DA: report xterm version */
3047 compatibility(OTHER
);
3048 /* this reports xterm version 136 so that VIM can
3049 use the drag messages from the mouse reporting */
3051 ldisc_send(term
->ldisc
, "\033[>0;136;0c", 11, 0);
3053 case 'a': /* HPR: move right N cols */
3054 compatibility(ANSI
);
3056 case 'C': /* CUF: Cursor right */
3057 move(term
, term
->curs
.x
+ def(term
->esc_args
[0], 1),
3059 term
->seen_disp_event
= TRUE
;
3061 case 'D': /* CUB: move left N cols */
3062 move(term
, term
->curs
.x
- def(term
->esc_args
[0], 1),
3064 term
->seen_disp_event
= TRUE
;
3066 case 'E': /* CNL: move down N lines and CR */
3067 compatibility(ANSI
);
3069 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
3070 term
->seen_disp_event
= TRUE
;
3072 case 'F': /* CPL: move up N lines and CR */
3073 compatibility(ANSI
);
3075 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
3076 term
->seen_disp_event
= TRUE
;
3079 case '`': /* HPA: set horizontal posn */
3080 compatibility(ANSI
);
3081 move(term
, def(term
->esc_args
[0], 1) - 1,
3083 term
->seen_disp_event
= TRUE
;
3085 case 'd': /* VPA: set vertical posn */
3086 compatibility(ANSI
);
3087 move(term
, term
->curs
.x
,
3088 ((term
->dec_om ? term
->marg_t
: 0) +
3089 def(term
->esc_args
[0], 1) - 1),
3090 (term
->dec_om ?
2 : 0));
3091 term
->seen_disp_event
= TRUE
;
3094 case 'f': /* HVP: set horz and vert posns at once */
3095 if (term
->esc_nargs
< 2)
3096 term
->esc_args
[1] = ARG_DEFAULT
;
3097 move(term
, def(term
->esc_args
[1], 1) - 1,
3098 ((term
->dec_om ? term
->marg_t
: 0) +
3099 def(term
->esc_args
[0], 1) - 1),
3100 (term
->dec_om ?
2 : 0));
3101 term
->seen_disp_event
= TRUE
;
3103 case 'J': /* ED: erase screen or parts of it */
3105 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
3108 erase_lots(term
, FALSE
, !!(i
& 2), !!(i
& 1));
3111 term
->seen_disp_event
= TRUE
;
3113 case 'K': /* EL: erase line or parts of it */
3115 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
3118 erase_lots(term
, TRUE
, !!(i
& 2), !!(i
& 1));
3120 term
->seen_disp_event
= TRUE
;
3122 case 'L': /* IL: insert lines */
3123 compatibility(VT102
);
3124 if (term
->curs
.y
<= term
->marg_b
)
3125 scroll(term
, term
->curs
.y
, term
->marg_b
,
3126 -def(term
->esc_args
[0], 1), FALSE
);
3127 term
->seen_disp_event
= TRUE
;
3129 case 'M': /* DL: delete lines */
3130 compatibility(VT102
);
3131 if (term
->curs
.y
<= term
->marg_b
)
3132 scroll(term
, term
->curs
.y
, term
->marg_b
,
3133 def(term
->esc_args
[0], 1),
3135 term
->seen_disp_event
= TRUE
;
3137 case '@': /* ICH: insert chars */
3138 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
3139 compatibility(VT102
);
3140 insch(term
, def(term
->esc_args
[0], 1));
3141 term
->seen_disp_event
= TRUE
;
3143 case 'P': /* DCH: delete chars */
3144 compatibility(VT102
);
3145 insch(term
, -def(term
->esc_args
[0], 1));
3146 term
->seen_disp_event
= TRUE
;
3148 case 'c': /* DA: terminal type query */
3149 compatibility(VT100
);
3150 /* This is the response for a VT102 */
3152 ldisc_send(term
->ldisc
, term
->id_string
,
3153 strlen(term
->id_string
), 0);
3155 case 'n': /* DSR: cursor position query */
3157 if (term
->esc_args
[0] == 6) {
3159 sprintf(buf
, "\033[%d;%dR", term
->curs
.y
+ 1,
3161 ldisc_send(term
->ldisc
, buf
, strlen(buf
), 0);
3162 } else if (term
->esc_args
[0] == 5) {
3163 ldisc_send(term
->ldisc
, "\033[0n", 4, 0);
3167 case 'h': /* SM: toggle modes to high */
3169 compatibility(VT100
);
3172 for (i
= 0; i
< term
->esc_nargs
; i
++)
3173 toggle_mode(term
, term
->esc_args
[i
],
3174 term
->esc_query
, TRUE
);
3177 case 'i': /* MC: Media copy */
3179 compatibility(VT100
);
3181 if (term
->esc_nargs
!= 1) break;
3182 if (term
->esc_args
[0] == 5 && *term
->cfg
.printer
) {
3183 term
->printing
= TRUE
;
3184 term
->only_printing
= !term
->esc_query
;
3185 term
->print_state
= 0;
3186 term_print_setup(term
);
3187 } else if (term
->esc_args
[0] == 4 &&
3189 term_print_finish(term
);
3193 case 'l': /* RM: toggle modes to low */
3195 compatibility(VT100
);
3198 for (i
= 0; i
< term
->esc_nargs
; i
++)
3199 toggle_mode(term
, term
->esc_args
[i
],
3200 term
->esc_query
, FALSE
);
3203 case 'g': /* TBC: clear tabs */
3204 compatibility(VT100
);
3205 if (term
->esc_nargs
== 1) {
3206 if (term
->esc_args
[0] == 0) {
3207 term
->tabs
[term
->curs
.x
] = FALSE
;
3208 } else if (term
->esc_args
[0] == 3) {
3210 for (i
= 0; i
< term
->cols
; i
++)
3211 term
->tabs
[i
] = FALSE
;
3215 case 'r': /* DECSTBM: set scroll margins */
3216 compatibility(VT100
);
3217 if (term
->esc_nargs
<= 2) {
3219 top
= def(term
->esc_args
[0], 1) - 1;
3220 bot
= (term
->esc_nargs
<= 1
3221 || term
->esc_args
[1] == 0 ?
3223 def(term
->esc_args
[1], term
->rows
)) - 1;
3224 if (bot
>= term
->rows
)
3225 bot
= term
->rows
- 1;
3226 /* VTTEST Bug 9 - if region is less than 2 lines
3227 * don't change region.
3229 if (bot
- top
> 0) {
3234 * I used to think the cursor should be
3235 * placed at the top of the newly marginned
3236 * area. Apparently not: VMS TPU falls over
3239 * Well actually it should for
3242 term
->curs
.y
= (term
->dec_om ?
3244 term
->seen_disp_event
= TRUE
;
3248 case 'm': /* SGR: set graphics rendition */
3251 * A VT100 without the AVO only had one
3252 * attribute, either underline or
3253 * reverse video depending on the
3254 * cursor type, this was selected by
3258 * This is sometimes DIM, eg on the
3261 * This is sometimes INVIS various ANSI.
3263 * This like 22 disables BOLD, DIM and INVIS
3265 * The ANSI colours appear on any
3266 * terminal that has colour (obviously)
3267 * but the interaction between sgr0 and
3268 * the colours varies but is usually
3269 * related to the background colour
3270 * erase item. The interaction between
3271 * colour attributes and the mono ones
3272 * is also very implementation
3275 * The 39 and 49 attributes are likely
3276 * to be unimplemented.
3279 for (i
= 0; i
< term
->esc_nargs
; i
++) {
3280 switch (def(term
->esc_args
[i
], 0)) {
3281 case 0: /* restore defaults */
3282 term
->curr_attr
= term
->default_attr
;
3284 case 1: /* enable bold */
3285 compatibility(VT100AVO
);
3286 term
->curr_attr
|= ATTR_BOLD
;
3288 case 21: /* (enable double underline) */
3289 compatibility(OTHER
);
3290 case 4: /* enable underline */
3291 compatibility(VT100AVO
);
3292 term
->curr_attr
|= ATTR_UNDER
;
3294 case 5: /* enable blink */
3295 compatibility(VT100AVO
);
3296 term
->curr_attr
|= ATTR_BLINK
;
3298 case 6: /* SCO light bkgrd */
3299 compatibility(SCOANSI
);
3300 term
->blink_is_real
= FALSE
;
3301 term
->curr_attr
|= ATTR_BLINK
;
3303 case 7: /* enable reverse video */
3304 term
->curr_attr
|= ATTR_REVERSE
;
3306 case 10: /* SCO acs off */
3307 compatibility(SCOANSI
);
3308 if (term
->cfg
.no_remote_charset
) break;
3309 term
->sco_acs
= 0; break;
3310 case 11: /* SCO acs on */
3311 compatibility(SCOANSI
);
3312 if (term
->cfg
.no_remote_charset
) break;
3313 term
->sco_acs
= 1; break;
3314 case 12: /* SCO acs on, |0x80 */
3315 compatibility(SCOANSI
);
3316 if (term
->cfg
.no_remote_charset
) break;
3317 term
->sco_acs
= 2; break;
3318 case 22: /* disable bold */
3319 compatibility2(OTHER
, VT220
);
3320 term
->curr_attr
&= ~ATTR_BOLD
;
3322 case 24: /* disable underline */
3323 compatibility2(OTHER
, VT220
);
3324 term
->curr_attr
&= ~ATTR_UNDER
;
3326 case 25: /* disable blink */
3327 compatibility2(OTHER
, VT220
);
3328 term
->curr_attr
&= ~ATTR_BLINK
;
3330 case 27: /* disable reverse video */
3331 compatibility2(OTHER
, VT220
);
3332 term
->curr_attr
&= ~ATTR_REVERSE
;
3343 term
->curr_attr
&= ~ATTR_FGMASK
;
3345 (term
->esc_args
[i
] - 30)<<ATTR_FGSHIFT
;
3355 /* xterm-style bright foreground */
3356 term
->curr_attr
&= ~ATTR_FGMASK
;
3358 ((term
->esc_args
[i
] - 90 + 16)
3361 case 39: /* default-foreground */
3362 term
->curr_attr
&= ~ATTR_FGMASK
;
3363 term
->curr_attr
|= ATTR_DEFFG
;
3374 term
->curr_attr
&= ~ATTR_BGMASK
;
3376 (term
->esc_args
[i
] - 40)<<ATTR_BGSHIFT
;
3386 /* xterm-style bright background */
3387 term
->curr_attr
&= ~ATTR_BGMASK
;
3389 ((term
->esc_args
[i
] - 100 + 16)
3392 case 49: /* default-background */
3393 term
->curr_attr
&= ~ATTR_BGMASK
;
3394 term
->curr_attr
|= ATTR_DEFBG
;
3398 set_erase_char(term
);
3401 case 's': /* save cursor */
3402 save_cursor(term
, TRUE
);
3404 case 'u': /* restore cursor */
3405 save_cursor(term
, FALSE
);
3406 term
->seen_disp_event
= TRUE
;
3408 case 't': /* DECSLPP: set page size - ie window height */
3410 * VT340/VT420 sequence DECSLPP, DEC only allows values
3411 * 24/25/36/48/72/144 other emulators (eg dtterm) use
3412 * illegal values (eg first arg 1..9) for window changing
3415 if (term
->esc_nargs
<= 1
3416 && (term
->esc_args
[0] < 1 ||
3417 term
->esc_args
[0] >= 24)) {
3418 compatibility(VT340TEXT
);
3419 if (!term
->cfg
.no_remote_resize
)
3420 request_resize(term
->frontend
, term
->cols
,
3421 def(term
->esc_args
[0], 24));
3423 } else if (term
->esc_nargs
>= 1 &&
3424 term
->esc_args
[0] >= 1 &&
3425 term
->esc_args
[0] < 24) {
3426 compatibility(OTHER
);
3428 switch (term
->esc_args
[0]) {
3432 set_iconic(term
->frontend
, FALSE
);
3435 set_iconic(term
->frontend
, TRUE
);
3438 if (term
->esc_nargs
>= 3) {
3439 if (!term
->cfg
.no_remote_resize
)
3440 move_window(term
->frontend
,
3441 def(term
->esc_args
[1], 0),
3442 def(term
->esc_args
[2], 0));
3446 /* We should resize the window to a given
3447 * size in pixels here, but currently our
3448 * resizing code isn't healthy enough to
3453 set_zorder(term
->frontend
, TRUE
);
3456 /* move to bottom */
3457 set_zorder(term
->frontend
, FALSE
);
3460 refresh_window(term
->frontend
);
3463 if (term
->esc_nargs
>= 3) {
3464 if (!term
->cfg
.no_remote_resize
)
3465 request_resize(term
->frontend
,
3466 def(term
->esc_args
[2], term
->cfg
.width
),
3467 def(term
->esc_args
[1], term
->cfg
.height
));
3471 if (term
->esc_nargs
>= 2)
3472 set_zoomed(term
->frontend
,
3478 ldisc_send(term
->ldisc
,
3479 is_iconic(term
->frontend
) ?
3480 "\033[1t" : "\033[2t", 4, 0);
3484 get_window_pos(term
->frontend
, &x
, &y
);
3485 len
= sprintf(buf
, "\033[3;%d;%dt", x
, y
);
3486 ldisc_send(term
->ldisc
, buf
, len
, 0);
3491 get_window_pixels(term
->frontend
, &x
, &y
);
3492 len
= sprintf(buf
, "\033[4;%d;%dt", x
, y
);
3493 ldisc_send(term
->ldisc
, buf
, len
, 0);
3498 len
= sprintf(buf
, "\033[8;%d;%dt",
3499 term
->rows
, term
->cols
);
3500 ldisc_send(term
->ldisc
, buf
, len
, 0);
3505 * Hmmm. Strictly speaking we
3506 * should return `the size of the
3507 * screen in characters', but
3508 * that's not easy: (a) window
3509 * furniture being what it is it's
3510 * hard to compute, and (b) in
3511 * resize-font mode maximising the
3512 * window wouldn't change the
3513 * number of characters. *shrug*. I
3514 * think we'll ignore it for the
3515 * moment and see if anyone
3516 * complains, and then ask them
3517 * what they would like it to do.
3522 !term
->cfg
.no_remote_qtitle
) {
3523 p
= get_window_title(term
->frontend
, TRUE
);
3525 ldisc_send(term
->ldisc
, "\033]L", 3, 0);
3526 ldisc_send(term
->ldisc
, p
, len
, 0);
3527 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
3532 !term
->cfg
.no_remote_qtitle
) {
3533 p
= get_window_title(term
->frontend
,FALSE
);
3535 ldisc_send(term
->ldisc
, "\033]l", 3, 0);
3536 ldisc_send(term
->ldisc
, p
, len
, 0);
3537 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
3543 case 'S': /* SU: Scroll up */
3544 compatibility(SCOANSI
);
3545 scroll(term
, term
->marg_t
, term
->marg_b
,
3546 def(term
->esc_args
[0], 1), TRUE
);
3547 term
->wrapnext
= FALSE
;
3548 term
->seen_disp_event
= TRUE
;
3550 case 'T': /* SD: Scroll down */
3551 compatibility(SCOANSI
);
3552 scroll(term
, term
->marg_t
, term
->marg_b
,
3553 -def(term
->esc_args
[0], 1), TRUE
);
3554 term
->wrapnext
= FALSE
;
3555 term
->seen_disp_event
= TRUE
;
3557 case ANSI('|', '*'): /* DECSNLS */
3559 * Set number of lines on screen
3560 * VT420 uses VGA like hardware and can
3561 * support any size in reasonable range
3562 * (24..49 AIUI) with no default specified.
3564 compatibility(VT420
);
3565 if (term
->esc_nargs
== 1 && term
->esc_args
[0] > 0) {
3566 if (!term
->cfg
.no_remote_resize
)
3567 request_resize(term
->frontend
, term
->cols
,
3568 def(term
->esc_args
[0],
3573 case ANSI('|', '$'): /* DECSCPP */
3575 * Set number of columns per page
3576 * Docs imply range is only 80 or 132, but
3579 compatibility(VT340TEXT
);
3580 if (term
->esc_nargs
<= 1) {
3581 if (!term
->cfg
.no_remote_resize
)
3582 request_resize(term
->frontend
,
3583 def(term
->esc_args
[0],
3584 term
->cfg
.width
), term
->rows
);
3588 case 'X': /* ECH: write N spaces w/o moving cursor */
3589 /* XXX VTTEST says this is vt220, vt510 manual
3591 compatibility(ANSIMIN
);
3593 int n
= def(term
->esc_args
[0], 1);
3595 int p
= term
->curs
.x
;
3596 termline
*cline
= scrlineptr(term
->curs
.y
);
3598 if (n
> term
->cols
- term
->curs
.x
)
3599 n
= term
->cols
- term
->curs
.x
;
3600 cursplus
= term
->curs
;
3602 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
3603 check_boundary(term
, term
->curs
.x
+n
, term
->curs
.y
);
3604 check_selection(term
, term
->curs
, cursplus
);
3606 copy_termchar(cline
, p
++,
3608 term
->seen_disp_event
= TRUE
;
3611 case 'x': /* DECREQTPARM: report terminal characteristics */
3612 compatibility(VT100
);
3615 int i
= def(term
->esc_args
[0], 0);
3616 if (i
== 0 || i
== 1) {
3617 strcpy(buf
, "\033[2;1;1;112;112;1;0x");
3619 ldisc_send(term
->ldisc
, buf
, 20, 0);
3623 case 'Z': /* CBT: BackTab for xterm */
3624 compatibility(OTHER
);
3626 int i
= def(term
->esc_args
[0], 1);
3627 pos old_curs
= term
->curs
;
3629 for(;i
>0 && term
->curs
.x
>0; i
--) {
3632 } while (term
->curs
.x
>0 &&
3633 !term
->tabs
[term
->curs
.x
]);
3635 check_selection(term
, old_curs
, term
->curs
);
3638 case ANSI('c', '='): /* Hide or Show Cursor */
3639 compatibility(SCOANSI
);
3640 switch(term
->esc_args
[0]) {
3641 case 0: /* hide cursor */
3642 term
->cursor_on
= FALSE
;
3644 case 1: /* restore cursor */
3645 term
->big_cursor
= FALSE
;
3646 term
->cursor_on
= TRUE
;
3648 case 2: /* block cursor */
3649 term
->big_cursor
= TRUE
;
3650 term
->cursor_on
= TRUE
;
3654 case ANSI('C', '='):
3656 * set cursor start on scanline esc_args[0] and
3657 * end on scanline esc_args[1].If you set
3658 * the bottom scan line to a value less than
3659 * the top scan line, the cursor will disappear.
3661 compatibility(SCOANSI
);
3662 if (term
->esc_nargs
>= 2) {
3663 if (term
->esc_args
[0] > term
->esc_args
[1])
3664 term
->cursor_on
= FALSE
;
3666 term
->cursor_on
= TRUE
;
3669 case ANSI('D', '='):
3670 compatibility(SCOANSI
);
3671 term
->blink_is_real
= FALSE
;
3672 if (term
->esc_args
[0]>=1)
3673 term
->curr_attr
|= ATTR_BLINK
;
3675 term
->curr_attr
&= ~ATTR_BLINK
;
3677 case ANSI('E', '='):
3678 compatibility(SCOANSI
);
3679 term
->blink_is_real
= (term
->esc_args
[0] >= 1);
3681 case ANSI('F', '='): /* set normal foreground */
3682 compatibility(SCOANSI
);
3683 if (term
->esc_args
[0] >= 0 && term
->esc_args
[0] < 16) {
3685 (sco2ansicolour
[term
->esc_args
[0] & 0x7] |
3686 ((term
->esc_args
[0] & 0x8) << 1)) <<
3688 term
->curr_attr
&= ~ATTR_FGMASK
;
3689 term
->curr_attr
|= colour
;
3690 term
->default_attr
&= ~ATTR_FGMASK
;
3691 term
->default_attr
|= colour
;
3694 case ANSI('G', '='): /* set normal background */
3695 compatibility(SCOANSI
);
3696 if (term
->esc_args
[0] >= 0 && term
->esc_args
[0] < 16) {
3698 (sco2ansicolour
[term
->esc_args
[0] & 0x7] |
3699 ((term
->esc_args
[0] & 0x8) << 1)) <<
3701 term
->curr_attr
&= ~ATTR_BGMASK
;
3702 term
->curr_attr
|= colour
;
3703 term
->default_attr
&= ~ATTR_BGMASK
;
3704 term
->default_attr
|= colour
;
3707 case ANSI('L', '='):
3708 compatibility(SCOANSI
);
3709 term
->use_bce
= (term
->esc_args
[0] <= 0);
3710 set_erase_char(term
);
3712 case ANSI('p', '"'): /* DECSCL: set compat level */
3714 * Allow the host to make this emulator a
3715 * 'perfect' VT102. This first appeared in
3716 * the VT220, but we do need to get back to
3717 * PuTTY mode so I won't check it.
3719 * The arg in 40..42,50 are a PuTTY extension.
3720 * The 2nd arg, 8bit vs 7bit is not checked.
3722 * Setting VT102 mode should also change
3723 * the Fkeys to generate PF* codes as a
3724 * real VT102 has no Fkeys. The VT220 does
3725 * this, F11..F13 become ESC,BS,LF other
3726 * Fkeys send nothing.
3728 * Note ESC c will NOT change this!
3731 switch (term
->esc_args
[0]) {
3733 term
->compatibility_level
&= ~TM_VTXXX
;
3734 term
->compatibility_level
|= TM_VT102
;
3737 term
->compatibility_level
&= ~TM_VTXXX
;
3738 term
->compatibility_level
|= TM_VT220
;
3742 if (term
->esc_args
[0] > 60 &&
3743 term
->esc_args
[0] < 70)
3744 term
->compatibility_level
|= TM_VTXXX
;
3748 term
->compatibility_level
&= TM_VTXXX
;
3751 term
->compatibility_level
= TM_PUTTY
;
3754 term
->compatibility_level
= TM_SCOANSI
;
3758 term
->compatibility_level
= TM_PUTTY
;
3764 /* Change the response to CSI c */
3765 if (term
->esc_args
[0] == 50) {
3768 strcpy(term
->id_string
, "\033[?");
3769 for (i
= 1; i
< term
->esc_nargs
; i
++) {
3771 strcat(term
->id_string
, ";");
3772 sprintf(lbuf
, "%d", term
->esc_args
[i
]);
3773 strcat(term
->id_string
, lbuf
);
3775 strcat(term
->id_string
, "c");
3778 /* Is this a good idea ?
3779 * Well we should do a soft reset at this point ...
3781 if (!has_compat(VT420
) && has_compat(VT100
)) {
3782 if (!term
->cfg
.no_remote_resize
) {
3783 if (term
->reset_132
)
3784 request_resize(132, 24);
3786 request_resize(80, 24);
3794 term
->osc_w
= FALSE
;
3796 case 'P': /* Linux palette sequence */
3797 term
->termstate
= SEEN_OSC_P
;
3798 term
->osc_strlen
= 0;
3800 case 'R': /* Linux palette reset */
3801 palette_reset(term
->frontend
);
3802 term_invalidate(term
);
3803 term
->termstate
= TOPLEVEL
;
3805 case 'W': /* word-set */
3806 term
->termstate
= SEEN_OSC_W
;
3819 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
3823 * Grotty hack to support xterm and DECterm title
3824 * sequences concurrently.
3826 if (term
->esc_args
[0] == 2) {
3827 term
->esc_args
[0] = 1;
3830 /* else fall through */
3832 term
->termstate
= OSC_STRING
;
3833 term
->osc_strlen
= 0;
3838 * This OSC stuff is EVIL. It takes just one character to get into
3839 * sysline mode and it's not initially obvious how to get out.
3840 * So I've added CR and LF as string aborts.
3841 * This shouldn't effect compatibility as I believe embedded
3842 * control characters are supposed to be interpreted (maybe?)
3843 * and they don't display anything useful anyway.
3847 if (c
== '\012' || c
== '\015') {
3848 term
->termstate
= TOPLEVEL
;
3849 } else if (c
== 0234 || c
== '\007') {
3851 * These characters terminate the string; ST and BEL
3852 * terminate the sequence and trigger instant
3853 * processing of it, whereas ESC goes back to SEEN_ESC
3854 * mode unless it is followed by \, in which case it is
3855 * synonymous with ST in the first place.
3858 term
->termstate
= TOPLEVEL
;
3859 } else if (c
== '\033')
3860 term
->termstate
= OSC_MAYBE_ST
;
3861 else if (term
->osc_strlen
< OSC_STR_MAX
)
3862 term
->osc_string
[term
->osc_strlen
++] = (char)c
;
3866 int max
= (term
->osc_strlen
== 0 ?
21 : 16);
3868 if ((int)c
>= '0' && (int)c
<= '9')
3870 else if ((int)c
>= 'A' && (int)c
<= 'A' + max
- 10)
3872 else if ((int)c
>= 'a' && (int)c
<= 'a' + max
- 10)
3875 term
->termstate
= TOPLEVEL
;
3878 term
->osc_string
[term
->osc_strlen
++] = val
;
3879 if (term
->osc_strlen
>= 7) {
3880 palette_set(term
->frontend
, term
->osc_string
[0],
3881 term
->osc_string
[1] * 16 + term
->osc_string
[2],
3882 term
->osc_string
[3] * 16 + term
->osc_string
[4],
3883 term
->osc_string
[5] * 16 + term
->osc_string
[6]);
3884 term_invalidate(term
);
3885 term
->termstate
= TOPLEVEL
;
3901 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
3904 term
->termstate
= OSC_STRING
;
3905 term
->osc_strlen
= 0;
3909 term
->termstate
= TOPLEVEL
;
3910 term
->seen_disp_event
= TRUE
;
3913 move(term
, term
->curs
.x
, term
->curs
.y
- 1, 1);
3916 move(term
, term
->curs
.x
, term
->curs
.y
+ 1, 1);
3919 move(term
, term
->curs
.x
+ 1, term
->curs
.y
, 1);
3922 move(term
, term
->curs
.x
- 1, term
->curs
.y
, 1);
3925 * From the VT100 Manual
3926 * NOTE: The special graphics characters in the VT100
3927 * are different from those in the VT52
3929 * From VT102 manual:
3930 * 137 _ Blank - Same
3931 * 140 ` Reserved - Humm.
3932 * 141 a Solid rectangle - Similar
3933 * 142 b 1/ - Top half of fraction for the
3934 * 143 c 3/ - subscript numbers below.
3937 * 146 f Degrees - Same
3938 * 147 g Plus or minus - Same
3940 * 151 i Ellipsis (dots)
3943 * 154 l Bar at scan 0
3944 * 155 m Bar at scan 1
3945 * 156 n Bar at scan 2
3946 * 157 o Bar at scan 3 - Similar
3947 * 160 p Bar at scan 4 - Similar
3948 * 161 q Bar at scan 5 - Similar
3949 * 162 r Bar at scan 6 - Same
3950 * 163 s Bar at scan 7 - Similar
3965 term
->cset_attr
[term
->cset
= 0] = CSET_LINEDRW
;
3968 term
->cset_attr
[term
->cset
= 0] = CSET_ASCII
;
3971 move(term
, 0, 0, 0);
3974 if (term
->curs
.y
== 0)
3975 scroll(term
, 0, term
->rows
- 1, -1, TRUE
);
3976 else if (term
->curs
.y
> 0)
3978 term
->wrapnext
= FALSE
;
3981 erase_lots(term
, FALSE
, FALSE
, TRUE
);
3985 erase_lots(term
, TRUE
, FALSE
, TRUE
);
3989 /* XXX Print cursor line */
3992 /* XXX Start controller mode */
3995 /* XXX Stop controller mode */
3999 term
->termstate
= VT52_Y1
;
4003 ldisc_send(term
->ldisc
, "\033/Z", 3, 0);
4006 term
->app_keypad_keys
= TRUE
;
4009 term
->app_keypad_keys
= FALSE
;
4012 /* XXX This should switch to VT100 mode not current or default
4013 * VT mode. But this will only have effect in a VT220+
4016 term
->vt52_mode
= FALSE
;
4017 term
->blink_is_real
= term
->cfg
.blinktext
;
4021 /* XXX Enter auto print mode */
4024 /* XXX Exit auto print mode */
4027 /* XXX Print screen */
4033 /* compatibility(ATARI) */
4034 move(term
, 0, 0, 0);
4035 erase_lots(term
, FALSE
, FALSE
, TRUE
);
4039 /* compatibility(ATARI) */
4040 if (term
->curs
.y
<= term
->marg_b
)
4041 scroll(term
, term
->curs
.y
, term
->marg_b
, -1, FALSE
);
4044 /* compatibility(ATARI) */
4045 if (term
->curs
.y
<= term
->marg_b
)
4046 scroll(term
, term
->curs
.y
, term
->marg_b
, 1, TRUE
);
4049 /* compatibility(ATARI) */
4050 term
->termstate
= VT52_FG
;
4053 /* compatibility(ATARI) */
4054 term
->termstate
= VT52_BG
;
4057 /* compatibility(ATARI) */
4058 erase_lots(term
, FALSE
, TRUE
, FALSE
);
4062 /* compatibility(ATARI) */
4063 term
->cursor_on
= TRUE
;
4066 /* compatibility(ATARI) */
4067 term
->cursor_on
= FALSE
;
4069 /* case 'j': Save cursor position - broken on ST */
4070 /* case 'k': Restore cursor position */
4072 /* compatibility(ATARI) */
4073 erase_lots(term
, TRUE
, TRUE
, TRUE
);
4075 term
->wrapnext
= FALSE
;
4078 /* compatibility(ATARI) */
4079 erase_lots(term
, TRUE
, TRUE
, FALSE
);
4082 /* compatibility(ATARI) */
4083 term
->curr_attr
|= ATTR_REVERSE
;
4086 /* compatibility(ATARI) */
4087 term
->curr_attr
&= ~ATTR_REVERSE
;
4089 case 'v': /* wrap Autowrap on - Wyse style */
4090 /* compatibility(ATARI) */
4093 case 'w': /* Autowrap off */
4094 /* compatibility(ATARI) */
4099 /* compatibility(OTHER) */
4100 term
->vt52_bold
= FALSE
;
4101 term
->curr_attr
= ATTR_DEFAULT
;
4102 set_erase_char(term
);
4105 /* compatibility(VI50) */
4106 term
->curr_attr
|= ATTR_UNDER
;
4109 /* compatibility(VI50) */
4110 term
->curr_attr
&= ~ATTR_UNDER
;
4113 /* compatibility(VI50) */
4114 term
->vt52_bold
= TRUE
;
4115 term
->curr_attr
|= ATTR_BOLD
;
4118 /* compatibility(VI50) */
4119 term
->vt52_bold
= FALSE
;
4120 term
->curr_attr
&= ~ATTR_BOLD
;
4126 term
->termstate
= VT52_Y2
;
4127 move(term
, term
->curs
.x
, c
- ' ', 0);
4130 term
->termstate
= TOPLEVEL
;
4131 move(term
, c
- ' ', term
->curs
.y
, 0);
4136 term
->termstate
= TOPLEVEL
;
4137 term
->curr_attr
&= ~ATTR_FGMASK
;
4138 term
->curr_attr
&= ~ATTR_BOLD
;
4139 term
->curr_attr
|= (c
& 0x7) << ATTR_FGSHIFT
;
4140 if ((c
& 0x8) || term
->vt52_bold
)
4141 term
->curr_attr
|= ATTR_BOLD
;
4143 set_erase_char(term
);
4146 term
->termstate
= TOPLEVEL
;
4147 term
->curr_attr
&= ~ATTR_BGMASK
;
4148 term
->curr_attr
&= ~ATTR_BLINK
;
4149 term
->curr_attr
|= (c
& 0x7) << ATTR_BGSHIFT
;
4151 /* Note: bold background */
4153 term
->curr_attr
|= ATTR_BLINK
;
4155 set_erase_char(term
);
4158 default: break; /* placate gcc warning about enum use */
4160 if (term
->selstate
!= NO_SELECTION
) {
4161 pos cursplus
= term
->curs
;
4163 check_selection(term
, term
->curs
, cursplus
);
4167 term_print_flush(term
);
4168 logflush(term
->logctx
);
4172 * To prevent having to run the reasonably tricky bidi algorithm
4173 * too many times, we maintain a cache of the last lineful of data
4174 * fed to the algorithm on each line of the display.
4176 static int term_bidi_cache_hit(Terminal
*term
, int line
,
4177 termchar
*lbefore
, int width
)
4181 if (!term
->pre_bidi_cache
)
4182 return FALSE
; /* cache doesn't even exist yet! */
4184 if (line
>= term
->bidi_cache_size
)
4185 return FALSE
; /* cache doesn't have this many lines */
4187 if (!term
->pre_bidi_cache
[line
].chars
)
4188 return FALSE
; /* cache doesn't contain _this_ line */
4190 if (term
->pre_bidi_cache
[line
].width
!= width
)
4191 return FALSE
; /* line is wrong width */
4193 for (i
= 0; i
< width
; i
++)
4194 if (!termchars_equal(term
->pre_bidi_cache
[line
].chars
+i
, lbefore
+i
))
4195 return FALSE
; /* line doesn't match cache */
4197 return TRUE
; /* it didn't match. */
4200 static void term_bidi_cache_store(Terminal
*term
, int line
, termchar
*lbefore
,
4201 termchar
*lafter
, int width
)
4203 if (!term
->pre_bidi_cache
|| term
->bidi_cache_size
<= line
) {
4204 int j
= term
->bidi_cache_size
;
4205 term
->bidi_cache_size
= line
+1;
4206 term
->pre_bidi_cache
= sresize(term
->pre_bidi_cache
,
4207 term
->bidi_cache_size
,
4208 struct bidi_cache_entry
);
4209 term
->post_bidi_cache
= sresize(term
->post_bidi_cache
,
4210 term
->bidi_cache_size
,
4211 struct bidi_cache_entry
);
4212 while (j
< term
->bidi_cache_size
) {
4213 term
->pre_bidi_cache
[j
].chars
=
4214 term
->post_bidi_cache
[j
].chars
= NULL
;
4215 term
->pre_bidi_cache
[j
].width
=
4216 term
->post_bidi_cache
[j
].width
= -1;
4221 sfree(term
->pre_bidi_cache
[line
].chars
);
4222 sfree(term
->post_bidi_cache
[line
].chars
);
4224 term
->pre_bidi_cache
[line
].width
= width
;
4225 term
->pre_bidi_cache
[line
].chars
= snewn(width
, termchar
);
4226 term
->post_bidi_cache
[line
].width
= width
;
4227 term
->post_bidi_cache
[line
].chars
= snewn(width
, termchar
);
4229 memcpy(term
->pre_bidi_cache
[line
].chars
, lbefore
, width
* TSIZE
);
4230 memcpy(term
->post_bidi_cache
[line
].chars
, lafter
, width
* TSIZE
);
4234 * Given a context, update the window. Out of paranoia, we don't
4235 * allow WM_PAINT responses to do scrolling optimisations.
4237 static void do_paint(Terminal
*term
, Context ctx
, int may_optimise
)
4239 int i
, it
, j
, our_curs_y
, our_curs_x
;
4244 termchar cursor_background
;
4245 unsigned long ticks
;
4246 #ifdef OPTIMISE_SCROLL
4247 struct scrollregion
*sr
;
4248 #endif /* OPTIMISE_SCROLL */
4250 cursor_background
= term
->basic_erase_char
;
4253 ch
= snewn(chlen
, wchar_t);
4256 * Check the visual bell state.
4258 if (term
->in_vbell
) {
4259 ticks
= GETTICKCOUNT();
4260 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
4261 term
->in_vbell
= FALSE
;
4264 rv
= (!term
->rvideo
^ !term
->in_vbell ? ATTR_REVERSE
: 0);
4267 * screen array, disptop, scrtop,
4269 * cfg.blinkpc, blink_is_real, tblinker,
4270 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
4273 /* Has the cursor position or type changed ? */
4274 if (term
->cursor_on
) {
4275 if (term
->has_focus
) {
4276 if (term
->blinker
|| !term
->cfg
.blink_cur
)
4277 cursor
= TATTR_ACTCURS
;
4281 cursor
= TATTR_PASCURS
;
4283 cursor
|= TATTR_RIGHTCURS
;
4286 our_curs_y
= term
->curs
.y
- term
->disptop
;
4289 * Adjust the cursor position in the case where it's
4290 * resting on the right-hand half of a CJK wide character.
4291 * xterm's behaviour here, which seems adequate to me, is
4292 * to display the cursor covering the _whole_ character,
4293 * exactly as if it were one space to the left.
4295 termline
*ldata
= lineptr(term
->curs
.y
);
4296 our_curs_x
= term
->curs
.x
;
4297 if (our_curs_x
> 0 &&
4298 ldata
->chars
[our_curs_x
].chr
== UCSWIDE
)
4304 * If the cursor is not where it was last time we painted, and
4305 * its previous position is visible on screen, invalidate its
4306 * previous position.
4308 if (term
->dispcursy
>= 0 &&
4309 (term
->curstype
!= cursor
||
4310 term
->dispcursy
!= our_curs_y
||
4311 term
->dispcursx
!= our_curs_x
)) {
4312 termchar
*dispcurs
= term
->disptext
[term
->dispcursy
]->chars
+
4315 if (term
->dispcursx
> 0 && dispcurs
->chr
== UCSWIDE
)
4316 dispcurs
[-1].attr
|= ATTR_INVALID
;
4317 if (term
->dispcursx
< term
->cols
-1 && dispcurs
[1].chr
== UCSWIDE
)
4318 dispcurs
[1].attr
|= ATTR_INVALID
;
4319 dispcurs
->attr
|= ATTR_INVALID
;
4323 term
->dispcursx
= term
->dispcursy
= -1;
4325 #ifdef OPTIMISE_SCROLL
4327 sr
= term
->scrollhead
;
4329 struct scrollregion
*next
= sr
->next
;
4330 do_scroll(ctx
, sr
->topline
, sr
->botline
, sr
->lines
);
4334 term
->scrollhead
= term
->scrolltail
= NULL
;
4335 #endif /* OPTIMISE_SCROLL */
4337 /* The normal screen data */
4338 for (i
= 0; i
< term
->rows
; i
++) {
4341 int dirty_line
, dirty_run
, selected
;
4342 unsigned long attr
= 0, cset
= 0;
4343 int updated_line
= 0;
4346 int last_run_dirty
= 0;
4348 scrpos
.y
= i
+ term
->disptop
;
4349 ldata
= lineptr(scrpos
.y
);
4351 dirty_run
= dirty_line
= (ldata
->lattr
!=
4352 term
->disptext
[i
]->lattr
);
4353 term
->disptext
[i
]->lattr
= ldata
->lattr
;
4355 /* Do Arabic shaping and bidi. */
4356 if(!term
->cfg
.bidi
|| !term
->cfg
.arabicshaping
) {
4358 if (!term_bidi_cache_hit(term
, i
, ldata
->chars
, term
->cols
)) {
4360 if (term
->wcFromTo_size
< term
->cols
) {
4361 term
->wcFromTo_size
= term
->cols
;
4362 term
->wcFrom
= sresize(term
->wcFrom
, term
->wcFromTo_size
,
4364 term
->wcTo
= sresize(term
->wcTo
, term
->wcFromTo_size
,
4368 for(it
=0; it
<term
->cols
; it
++)
4370 unsigned long uc
= (ldata
->chars
[it
].chr
);
4372 switch (uc
& CSET_MASK
) {
4374 if (!term
->cfg
.rawcnp
) {
4375 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
4379 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
4382 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
4385 switch (uc
& CSET_MASK
) {
4387 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
4390 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
4394 term
->wcFrom
[it
].origwc
= term
->wcFrom
[it
].wc
=
4396 term
->wcFrom
[it
].index
= it
;
4400 do_bidi(term
->wcFrom
, term
->cols
);
4402 /* this is saved iff done from inside the shaping */
4403 if(!term
->cfg
.bidi
&& term
->cfg
.arabicshaping
)
4404 for(it
=0; it
<term
->cols
; it
++)
4405 term
->wcTo
[it
] = term
->wcFrom
[it
];
4407 if(!term
->cfg
.arabicshaping
)
4408 do_shape(term
->wcFrom
, term
->wcTo
, term
->cols
);
4410 if (term
->ltemp_size
< ldata
->size
) {
4411 term
->ltemp_size
= ldata
->size
;
4412 term
->ltemp
= sresize(term
->ltemp
, term
->ltemp_size
,
4416 memcpy(term
->ltemp
, ldata
->chars
, ldata
->size
* TSIZE
);
4418 for(it
=0; it
<term
->cols
; it
++)
4420 term
->ltemp
[it
] = ldata
->chars
[term
->wcTo
[it
].index
];
4421 if (term
->ltemp
[it
].cc_next
)
4422 term
->ltemp
[it
].cc_next
-=
4423 it
- term
->wcTo
[it
].index
;
4425 if (term
->wcTo
[it
].origwc
!= term
->wcTo
[it
].wc
)
4426 term
->ltemp
[it
].chr
= term
->wcTo
[it
].wc
;
4428 term_bidi_cache_store(term
, i
, ldata
->chars
,
4429 term
->ltemp
, ldata
->size
);
4431 lchars
= term
->ltemp
;
4433 lchars
= term
->post_bidi_cache
[i
].chars
;
4436 lchars
= ldata
->chars
;
4438 for (j
= 0; j
< term
->cols
; j
++) {
4439 unsigned long tattr
, tchar
;
4440 termchar
*d
= lchars
+ j
;
4441 int break_run
, do_copy
;
4447 switch (tchar
& CSET_MASK
) {
4449 tchar
= term
->ucsdata
->unitab_line
[tchar
& 0xFF];
4452 tchar
= term
->ucsdata
->unitab_xterm
[tchar
& 0xFF];
4455 tchar
= term
->ucsdata
->unitab_scoacs
[tchar
&0xFF];
4458 if (j
< term
->cols
-1 && d
[1].chr
== UCSWIDE
)
4461 /* Video reversing things */
4462 if (term
->selstate
== DRAGGING
|| term
->selstate
== SELECTED
) {
4463 if (term
->seltype
== LEXICOGRAPHIC
)
4464 selected
= (posle(term
->selstart
, scrpos
) &&
4465 poslt(scrpos
, term
->selend
));
4467 selected
= (posPle(term
->selstart
, scrpos
) &&
4468 posPlt(scrpos
, term
->selend
));
4472 ^ (selected ? ATTR_REVERSE
: 0));
4474 /* 'Real' blinking ? */
4475 if (term
->blink_is_real
&& (tattr
& ATTR_BLINK
)) {
4476 if (term
->has_focus
&& term
->tblinker
) {
4477 tchar
= term
->ucsdata
->unitab_line
[(unsigned char)' '];
4479 tattr
&= ~ATTR_BLINK
;
4483 * Check the font we'll _probably_ be using to see if
4484 * the character is wide when we don't want it to be.
4486 if (tchar
!= term
->disptext
[i
]->chars
[j
].chr
||
4487 tattr
!= (term
->disptext
[i
]->chars
[j
].attr
&~
4489 if ((tattr
& ATTR_WIDE
) == 0 && char_width(ctx
, tchar
) == 2)
4490 tattr
|= ATTR_NARROW
;
4491 } else if (term
->disptext
[i
]->chars
[j
].attr
& ATTR_NARROW
)
4492 tattr
|= ATTR_NARROW
;
4494 /* Cursor here ? Save the 'background' */
4495 if (i
== our_curs_y
&& j
== our_curs_x
) {
4497 cursor_background
.chr
= tchar
;
4498 cursor_background
.attr
= tattr
;
4499 /* For once, this cc_next field is an absolute index in lchars */
4501 cursor_background
.cc_next
= d
->cc_next
+ j
;
4503 cursor_background
.cc_next
= 0;
4504 term
->dispcursx
= j
;
4505 term
->dispcursy
= i
;
4508 if ((term
->disptext
[i
]->chars
[j
].attr
^ tattr
) & ATTR_WIDE
)
4511 break_run
= ((tattr
^ attr
) & term
->attr_mask
) != 0;
4513 /* Special hack for VT100 Linedraw glyphs */
4514 if (tchar
>= 0x23BA && tchar
<= 0x23BD)
4518 * Separate out sequences of characters that have the
4519 * same CSET, if that CSET is a magic one.
4521 if (CSET_OF(tchar
) != cset
)
4525 * Break on both sides of any combined-character cell.
4527 if (d
->cc_next
!= 0 ||
4528 (j
> 0 && d
[-1].cc_next
!= 0))
4531 if (!term
->ucsdata
->dbcs_screenfont
&& !dirty_line
) {
4532 if (term
->disptext
[i
]->chars
[j
].chr
== tchar
&&
4533 term
->disptext
[i
]->chars
[j
].attr
== tattr
)
4535 else if (!dirty_run
&& ccount
== 1)
4540 if ((dirty_run
|| last_run_dirty
) && ccount
> 0) {
4541 do_text(ctx
, start
, i
, ch
, ccount
, attr
, ldata
->lattr
);
4547 cset
= CSET_OF(tchar
);
4548 if (term
->ucsdata
->dbcs_screenfont
)
4549 last_run_dirty
= dirty_run
;
4550 dirty_run
= dirty_line
;
4554 if (!termchars_equal_override(&term
->disptext
[i
]->chars
[j
],
4560 if (ccount
>= chlen
) {
4561 chlen
= ccount
+ 256;
4562 ch
= sresize(ch
, chlen
, wchar_t);
4564 ch
[ccount
++] = (wchar_t) tchar
;
4569 while (dd
->cc_next
) {
4570 unsigned long schar
;
4575 switch (schar
& CSET_MASK
) {
4577 schar
= term
->ucsdata
->unitab_line
[schar
& 0xFF];
4580 schar
= term
->ucsdata
->unitab_xterm
[schar
& 0xFF];
4583 schar
= term
->ucsdata
->unitab_scoacs
[schar
&0xFF];
4587 if (ccount
>= chlen
) {
4588 chlen
= ccount
+ 256;
4589 ch
= sresize(ch
, chlen
, wchar_t);
4591 ch
[ccount
++] = (wchar_t) schar
;
4594 attr
|= TATTR_COMBINING
;
4598 copy_termchar(term
->disptext
[i
], j
, d
);
4599 term
->disptext
[i
]->chars
[j
].chr
= tchar
;
4600 term
->disptext
[i
]->chars
[j
].attr
= tattr
;
4603 /* If it's a wide char step along to the next one. */
4604 if (tattr
& ATTR_WIDE
) {
4605 if (++j
< term
->cols
) {
4608 * By construction above, the cursor should not
4609 * be on the right-hand half of this character.
4612 assert(!(i
== our_curs_y
&& j
== our_curs_x
));
4613 if (!termchars_equal(&term
->disptext
[i
]->chars
[j
], d
))
4615 copy_termchar(term
->disptext
[i
], j
, d
);
4619 if (dirty_run
&& ccount
> 0) {
4620 do_text(ctx
, start
, i
, ch
, ccount
, attr
, ldata
->lattr
);
4624 /* Cursor on this line ? (and changed) */
4625 if (i
== our_curs_y
&& (term
->curstype
!= cursor
|| updated_line
)) {
4626 ch
[0] = (wchar_t) cursor_background
.chr
;
4627 attr
= cursor_background
.attr
| cursor
;
4630 if (cursor_background
.cc_next
) {
4631 termchar
*dd
= ldata
->chars
+ cursor_background
.cc_next
;
4634 unsigned long schar
;
4637 switch (schar
& CSET_MASK
) {
4639 schar
= term
->ucsdata
->unitab_line
[schar
& 0xFF];
4642 schar
= term
->ucsdata
->unitab_xterm
[schar
& 0xFF];
4645 schar
= term
->ucsdata
->unitab_scoacs
[schar
&0xFF];
4649 if (ccount
>= chlen
) {
4650 chlen
= ccount
+ 256;
4651 ch
= sresize(ch
, chlen
, wchar_t);
4653 ch
[ccount
++] = (wchar_t) schar
;
4661 attr
|= TATTR_COMBINING
;
4664 do_cursor(ctx
, our_curs_x
, i
, ch
, ccount
, attr
, ldata
->lattr
);
4665 term
->curstype
= cursor
;
4675 * Flick the switch that says if blinking things should be shown or hidden.
4678 void term_blink(Terminal
*term
, int flg
)
4680 long now
, blink_diff
;
4682 now
= GETTICKCOUNT();
4683 blink_diff
= now
- term
->last_tblink
;
4685 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
4686 if (blink_diff
< 0 || blink_diff
> (TICKSPERSEC
* 9 / 20)) {
4687 term
->last_tblink
= now
;
4688 term
->tblinker
= !term
->tblinker
;
4693 term
->last_blink
= now
;
4697 blink_diff
= now
- term
->last_blink
;
4699 /* Make sure the cursor blinks no faster than system blink rate */
4700 if (blink_diff
>= 0 && blink_diff
< (long) CURSORBLINK
)
4703 term
->last_blink
= now
;
4704 term
->blinker
= !term
->blinker
;
4708 * Invalidate the whole screen so it will be repainted in full.
4710 void term_invalidate(Terminal
*term
)
4714 for (i
= 0; i
< term
->rows
; i
++)
4715 for (j
= 0; j
< term
->cols
; j
++)
4716 term
->disptext
[i
]->chars
[j
].attr
= ATTR_INVALID
;
4720 * Paint the window in response to a WM_PAINT message.
4722 void term_paint(Terminal
*term
, Context ctx
,
4723 int left
, int top
, int right
, int bottom
, int immediately
)
4726 if (left
< 0) left
= 0;
4727 if (top
< 0) top
= 0;
4728 if (right
>= term
->cols
) right
= term
->cols
-1;
4729 if (bottom
>= term
->rows
) bottom
= term
->rows
-1;
4731 for (i
= top
; i
<= bottom
&& i
< term
->rows
; i
++) {
4732 if ((term
->disptext
[i
]->lattr
& LATTR_MODE
) == LATTR_NORM
)
4733 for (j
= left
; j
<= right
&& j
< term
->cols
; j
++)
4734 term
->disptext
[i
]->chars
[j
].attr
= ATTR_INVALID
;
4736 for (j
= left
/ 2; j
<= right
/ 2 + 1 && j
< term
->cols
; j
++)
4737 term
->disptext
[i
]->chars
[j
].attr
= ATTR_INVALID
;
4740 /* This should happen soon enough, also for some reason it sometimes
4741 * fails to actually do anything when re-sizing ... painting the wrong
4745 do_paint (term
, ctx
, FALSE
);
4749 * Attempt to scroll the scrollback. The second parameter gives the
4750 * position we want to scroll to; the first is +1 to denote that
4751 * this position is relative to the beginning of the scrollback, -1
4752 * to denote it is relative to the end, and 0 to denote that it is
4753 * relative to the current position.
4755 void term_scroll(Terminal
*term
, int rel
, int where
)
4757 int sbtop
= -sblines(term
);
4758 #ifdef OPTIMISE_SCROLL
4759 int olddisptop
= term
->disptop
;
4761 #endif /* OPTIMISE_SCROLL */
4763 term
->disptop
= (rel
< 0 ?
0 : rel
> 0 ? sbtop
: term
->disptop
) + where
;
4764 if (term
->disptop
< sbtop
)
4765 term
->disptop
= sbtop
;
4766 if (term
->disptop
> 0)
4769 #ifdef OPTIMISE_SCROLL
4770 shift
= (term
->disptop
- olddisptop
);
4771 if (shift
< term
->rows
&& shift
> -term
->rows
)
4772 scroll_display(term
, 0, term
->rows
- 1, shift
);
4773 #endif /* OPTIMISE_SCROLL */
4777 static void clipme(Terminal
*term
, pos top
, pos bottom
, int rect
, int desel
)
4780 wchar_t *wbptr
; /* where next char goes within workbuf */
4782 int wblen
= 0; /* workbuf len */
4783 int buflen
; /* amount of memory allocated to workbuf */
4785 buflen
= 5120; /* Default size */
4786 workbuf
= snewn(buflen
, wchar_t);
4787 wbptr
= workbuf
; /* start filling here */
4788 old_top_x
= top
.x
; /* needed for rect==1 */
4790 while (poslt(top
, bottom
)) {
4792 termline
*ldata
= lineptr(top
.y
);
4796 * nlpos will point at the maximum position on this line we
4797 * should copy up to. So we start it at the end of the
4801 nlpos
.x
= term
->cols
;
4804 * ... move it backwards if there's unused space at the end
4805 * of the line (and also set `nl' if this is the case,
4806 * because in normal selection mode this means we need a
4807 * newline at the end)...
4809 if (!(ldata
->lattr
& LATTR_WRAPPED
)) {
4810 while (IS_SPACE_CHR(ldata
->chars
[nlpos
.x
- 1].chr
) &&
4811 !ldata
->chars
[nlpos
.x
- 1].cc_next
&&
4814 if (poslt(nlpos
, bottom
))
4816 } else if (ldata
->lattr
& LATTR_WRAPPED2
) {
4817 /* Ignore the last char on the line in a WRAPPED2 line. */
4822 * ... and then clip it to the terminal x coordinate if
4823 * we're doing rectangular selection. (In this case we
4824 * still did the above, so that copying e.g. the right-hand
4825 * column from a table doesn't fill with spaces on the
4829 if (nlpos
.x
> bottom
.x
)
4831 nl
= (top
.y
< bottom
.y
);
4834 while (poslt(top
, bottom
) && poslt(top
, nlpos
)) {
4837 sprintf(cbuf
, "<U+%04x>", (ldata
[top
.x
] & 0xFFFF));
4839 wchar_t cbuf
[16], *p
;
4843 if (ldata
->chars
[x
].chr
== UCSWIDE
) {
4849 int uc
= ldata
->chars
[x
].chr
;
4851 switch (uc
& CSET_MASK
) {
4853 if (!term
->cfg
.rawcnp
) {
4854 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
4858 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
4861 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
4864 switch (uc
& CSET_MASK
) {
4866 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
4869 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
4873 set
= (uc
& CSET_MASK
);
4874 c
= (uc
& ~CSET_MASK
);
4878 if (DIRECT_FONT(uc
)) {
4879 if (c
>= ' ' && c
!= 0x7F) {
4883 if (is_dbcs_leadbyte(term
->ucsdata
->font_codepage
, (BYTE
) c
)) {
4885 buf
[1] = (char) (0xFF & ldata
->chars
[top
.x
+ 1].chr
);
4886 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 2, wbuf
, 4);
4890 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 1, wbuf
, 4);
4894 memcpy(cbuf
, wbuf
, rv
* sizeof(wchar_t));
4901 for (p
= cbuf
; *p
; p
++) {
4902 /* Enough overhead for trailing NL and nul */
4903 if (wblen
>= buflen
- 16) {
4905 workbuf
= sresize(workbuf
, buflen
, wchar_t);
4906 wbptr
= workbuf
+ wblen
;
4912 if (ldata
->chars
[x
].cc_next
)
4913 x
+= ldata
->chars
[x
].cc_next
;
4921 for (i
= 0; i
< sel_nl_sz
; i
++) {
4923 *wbptr
++ = sel_nl
[i
];
4927 top
.x
= rect ? old_top_x
: 0;
4931 #if SELECTION_NUL_TERMINATED
4935 write_clip(term
->frontend
, workbuf
, wblen
, desel
); /* transfer to clipbd */
4936 if (buflen
> 0) /* indicates we allocated this buffer */
4940 void term_copyall(Terminal
*term
)
4944 tree234
*screen
= term
->screen
;
4945 top
.y
= -sblines(term
);
4947 bottom
.y
= find_last_nonempty_line(term
, screen
);
4948 bottom
.x
= term
->cols
;
4949 clipme(term
, top
, bottom
, 0, TRUE
);
4953 * The wordness array is mainly for deciding the disposition of the
4954 * US-ASCII characters.
4956 static int wordtype(Terminal
*term
, int uc
)
4959 int start
, end
, ctype
;
4961 static const struct ucsword ucs_words
[] = {
4967 0x037e, 0x037e, 1}, /* Greek question mark */
4969 0x0387, 0x0387, 1}, /* Greek ano teleia */
4971 0x055a, 0x055f, 1}, /* Armenian punctuation */
4973 0x0589, 0x0589, 1}, /* Armenian full stop */
4975 0x0700, 0x070d, 1}, /* Syriac punctuation */
4977 0x104a, 0x104f, 1}, /* Myanmar punctuation */
4979 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
4981 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
4983 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
4985 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
4987 0x1800, 0x180a, 1}, /* Mongolian punctuation */
4989 0x2000, 0x200a, 0}, /* Various spaces */
4991 0x2070, 0x207f, 2}, /* superscript */
4993 0x2080, 0x208f, 2}, /* subscript */
4995 0x200b, 0x27ff, 1}, /* punctuation and symbols */
4997 0x3000, 0x3000, 0}, /* ideographic space */
4999 0x3001, 0x3020, 1}, /* ideographic punctuation */
5001 0x303f, 0x309f, 3}, /* Hiragana */
5003 0x30a0, 0x30ff, 3}, /* Katakana */
5005 0x3300, 0x9fff, 3}, /* CJK Ideographs */
5007 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
5009 0xf900, 0xfaff, 3}, /* CJK Ideographs */
5011 0xfe30, 0xfe6b, 1}, /* punctuation forms */
5013 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
5015 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
5017 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
5019 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
5021 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
5025 const struct ucsword
*wptr
;
5027 switch (uc
& CSET_MASK
) {
5029 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
5032 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
5035 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
5038 switch (uc
& CSET_MASK
) {
5040 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
5043 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
5047 /* For DBCS fonts I can't do anything useful. Even this will sometimes
5048 * fail as there's such a thing as a double width space. :-(
5050 if (term
->ucsdata
->dbcs_screenfont
&&
5051 term
->ucsdata
->font_codepage
== term
->ucsdata
->line_codepage
)
5055 return term
->wordness
[uc
];
5057 for (wptr
= ucs_words
; wptr
->start
; wptr
++) {
5058 if (uc
>= wptr
->start
&& uc
<= wptr
->end
)
5066 * Spread the selection outwards according to the selection mode.
5068 static pos
sel_spread_half(Terminal
*term
, pos p
, int dir
)
5072 int topy
= -sblines(term
);
5074 ldata
= lineptr(p
.y
);
5076 switch (term
->selmode
) {
5079 * In this mode, every character is a separate unit, except
5080 * for runs of spaces at the end of a non-wrapping line.
5082 if (!(ldata
->lattr
& LATTR_WRAPPED
)) {
5083 termchar
*q
= ldata
->chars
+ term
->cols
;
5084 while (q
> ldata
->chars
&&
5085 IS_SPACE_CHR(q
[-1].chr
) && !q
[-1].cc_next
)
5087 if (q
== ldata
->chars
+ term
->cols
)
5089 if (p
.x
>= q
- ldata
->chars
)
5090 p
.x
= (dir
== -1 ? q
- ldata
->chars
: term
->cols
- 1);
5095 * In this mode, the units are maximal runs of characters
5096 * whose `wordness' has the same value.
5098 wvalue
= wordtype(term
, UCSGET(ldata
->chars
, p
.x
));
5101 int maxcols
= (ldata
->lattr
& LATTR_WRAPPED2 ?
5102 term
->cols
-1 : term
->cols
);
5103 if (p
.x
< maxcols
-1) {
5104 if (wordtype(term
, UCSGET(ldata
->chars
, p
.x
+1)) == wvalue
)
5109 if (ldata
->lattr
& LATTR_WRAPPED
) {
5111 ldata2
= lineptr(p
.y
+1);
5112 if (wordtype(term
, UCSGET(ldata2
->chars
, 0))
5129 if (wordtype(term
, UCSGET(ldata
->chars
, p
.x
-1)) == wvalue
)
5138 ldata2
= lineptr(p
.y
-1);
5139 maxcols
= (ldata2
->lattr
& LATTR_WRAPPED2 ?
5140 term
->cols
-1 : term
->cols
);
5141 if (ldata2
->lattr
& LATTR_WRAPPED
) {
5142 if (wordtype(term
, UCSGET(ldata2
->chars
, maxcols
-1))
5160 * In this mode, every line is a unit.
5162 p
.x
= (dir
== -1 ?
0 : term
->cols
- 1);
5170 static void sel_spread(Terminal
*term
)
5172 if (term
->seltype
== LEXICOGRAPHIC
) {
5173 term
->selstart
= sel_spread_half(term
, term
->selstart
, -1);
5174 decpos(term
->selend
);
5175 term
->selend
= sel_spread_half(term
, term
->selend
, +1);
5176 incpos(term
->selend
);
5180 void term_do_paste(Terminal
*term
)
5185 get_clip(term
->frontend
, &data
, &len
);
5186 if (data
&& len
> 0) {
5189 term_seen_key_event(term
); /* pasted data counts */
5191 if (term
->paste_buffer
)
5192 sfree(term
->paste_buffer
);
5193 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
5194 term
->paste_buffer
= snewn(len
, wchar_t);
5197 while (p
< data
+ len
) {
5198 while (p
< data
+ len
&&
5199 !(p
<= data
+ len
- sel_nl_sz
&&
5200 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
5205 for (i
= 0; i
< p
- q
; i
++) {
5206 term
->paste_buffer
[term
->paste_len
++] = q
[i
];
5210 if (p
<= data
+ len
- sel_nl_sz
&&
5211 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
5212 term
->paste_buffer
[term
->paste_len
++] = '\015';
5218 /* Assume a small paste will be OK in one go. */
5219 if (term
->paste_len
< 256) {
5221 luni_send(term
->ldisc
, term
->paste_buffer
, term
->paste_len
, 0);
5222 if (term
->paste_buffer
)
5223 sfree(term
->paste_buffer
);
5224 term
->paste_buffer
= 0;
5225 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
5228 get_clip(term
->frontend
, NULL
, NULL
);
5231 void term_mouse(Terminal
*term
, Mouse_Button braw
, Mouse_Button bcooked
,
5232 Mouse_Action a
, int x
, int y
, int shift
, int ctrl
, int alt
)
5236 int raw_mouse
= (term
->xterm_mouse
&&
5237 !term
->cfg
.no_mouse_rep
&&
5238 !(term
->cfg
.mouse_override
&& shift
));
5239 int default_seltype
;
5243 if (a
== MA_DRAG
&& !raw_mouse
)
5244 term_scroll(term
, 0, -1);
5246 if (y
>= term
->rows
) {
5248 if (a
== MA_DRAG
&& !raw_mouse
)
5249 term_scroll(term
, 0, +1);
5258 if (x
>= term
->cols
)
5261 selpoint
.y
= y
+ term
->disptop
;
5263 ldata
= lineptr(selpoint
.y
);
5264 if ((ldata
->lattr
& LATTR_MODE
) != LATTR_NORM
)
5269 int encstate
= 0, r
, c
;
5276 encstate
= 0x20; /* left button down */
5287 case MBT_WHEEL_DOWN
:
5290 default: break; /* placate gcc warning about enum use */
5294 if (term
->xterm_mouse
== 1)
5300 term
->mouse_is_down
= 0;
5303 if (term
->mouse_is_down
== braw
)
5305 term
->mouse_is_down
= braw
;
5307 default: break; /* placate gcc warning about enum use */
5316 sprintf(abuf
, "\033[M%c%c%c", encstate
, c
, r
);
5317 ldisc_send(term
->ldisc
, abuf
, 6, 0);
5323 * Set the selection type (rectangular or normal) at the start
5324 * of a selection attempt, from the state of Alt.
5326 if (!alt
^ !term
->cfg
.rect_select
)
5327 default_seltype
= RECTANGULAR
;
5329 default_seltype
= LEXICOGRAPHIC
;
5331 if (term
->selstate
== NO_SELECTION
) {
5332 term
->seltype
= default_seltype
;
5335 if (bcooked
== MBT_SELECT
&& a
== MA_CLICK
) {
5337 term
->selstate
= ABOUT_TO
;
5338 term
->seltype
= default_seltype
;
5339 term
->selanchor
= selpoint
;
5340 term
->selmode
= SM_CHAR
;
5341 } else if (bcooked
== MBT_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
5343 term
->selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
5344 term
->selstate
= DRAGGING
;
5345 term
->selstart
= term
->selanchor
= selpoint
;
5346 term
->selend
= term
->selstart
;
5347 incpos(term
->selend
);
5349 } else if ((bcooked
== MBT_SELECT
&& a
== MA_DRAG
) ||
5350 (bcooked
== MBT_EXTEND
&& a
!= MA_RELEASE
)) {
5351 if (term
->selstate
== ABOUT_TO
&& poseq(term
->selanchor
, selpoint
))
5353 if (bcooked
== MBT_EXTEND
&& a
!= MA_DRAG
&&
5354 term
->selstate
== SELECTED
) {
5355 if (term
->seltype
== LEXICOGRAPHIC
) {
5357 * For normal selection, we extend by moving
5358 * whichever end of the current selection is closer
5361 if (posdiff(selpoint
, term
->selstart
) <
5362 posdiff(term
->selend
, term
->selstart
) / 2) {
5363 term
->selanchor
= term
->selend
;
5364 decpos(term
->selanchor
);
5366 term
->selanchor
= term
->selstart
;
5370 * For rectangular selection, we have a choice of
5371 * _four_ places to put selanchor and selpoint: the
5372 * four corners of the selection.
5374 if (2*selpoint
.x
< term
->selstart
.x
+ term
->selend
.x
)
5375 term
->selanchor
.x
= term
->selend
.x
-1;
5377 term
->selanchor
.x
= term
->selstart
.x
;
5379 if (2*selpoint
.y
< term
->selstart
.y
+ term
->selend
.y
)
5380 term
->selanchor
.y
= term
->selend
.y
;
5382 term
->selanchor
.y
= term
->selstart
.y
;
5384 term
->selstate
= DRAGGING
;
5386 if (term
->selstate
!= ABOUT_TO
&& term
->selstate
!= DRAGGING
)
5387 term
->selanchor
= selpoint
;
5388 term
->selstate
= DRAGGING
;
5389 if (term
->seltype
== LEXICOGRAPHIC
) {
5391 * For normal selection, we set (selstart,selend) to
5392 * (selpoint,selanchor) in some order.
5394 if (poslt(selpoint
, term
->selanchor
)) {
5395 term
->selstart
= selpoint
;
5396 term
->selend
= term
->selanchor
;
5397 incpos(term
->selend
);
5399 term
->selstart
= term
->selanchor
;
5400 term
->selend
= selpoint
;
5401 incpos(term
->selend
);
5405 * For rectangular selection, we may need to
5406 * interchange x and y coordinates (if the user has
5407 * dragged in the -x and +y directions, or vice versa).
5409 term
->selstart
.x
= min(term
->selanchor
.x
, selpoint
.x
);
5410 term
->selend
.x
= 1+max(term
->selanchor
.x
, selpoint
.x
);
5411 term
->selstart
.y
= min(term
->selanchor
.y
, selpoint
.y
);
5412 term
->selend
.y
= max(term
->selanchor
.y
, selpoint
.y
);
5415 } else if ((bcooked
== MBT_SELECT
|| bcooked
== MBT_EXTEND
) &&
5417 if (term
->selstate
== DRAGGING
) {
5419 * We've completed a selection. We now transfer the
5420 * data to the clipboard.
5422 clipme(term
, term
->selstart
, term
->selend
,
5423 (term
->seltype
== RECTANGULAR
), FALSE
);
5424 term
->selstate
= SELECTED
;
5426 term
->selstate
= NO_SELECTION
;
5427 } else if (bcooked
== MBT_PASTE
5429 #if MULTICLICK_ONLY_EVENT
5430 || a
== MA_2CLK
|| a
== MA_3CLK
5433 request_paste(term
->frontend
);
5439 void term_key(Terminal
*term
, Key_Sym keysym
, wchar_t *text
, size_t tlen
,
5440 unsigned int modifiers
, unsigned int flags
)
5444 int prependesc
= FALSE
;
5448 fprintf(stderr
, "keysym = %d, %d chars:", keysym
, tlen
);
5449 for (i
= 0; i
< tlen
; i
++)
5450 fprintf(stderr
, " %04x", (unsigned)text
[i
]);
5451 fprintf(stderr
, "\n");
5455 if ((flags
& PKF_REPEAT
) && term
->repeat_off
)
5458 /* Currently, Meta always just prefixes everything with ESC. */
5459 if (modifiers
& PKM_META
)
5461 modifiers
&= ~PKM_META
;
5464 * Alt is only used for Alt+keypad, which isn't supported yet, so
5467 modifiers
&= ~PKM_ALT
;
5469 /* Standard local function keys */
5470 switch (modifiers
& (PKM_SHIFT
| PKM_CONTROL
)) {
5472 if (keysym
== PK_PAGEUP
)
5473 /* scroll up one page */;
5474 if (keysym
== PK_PAGEDOWN
)
5475 /* scroll down on page */;
5476 if (keysym
== PK_INSERT
)
5477 term_do_paste(term
);
5480 if (keysym
== PK_PAGEUP
)
5481 /* scroll up one line */;
5482 if (keysym
== PK_PAGEDOWN
)
5483 /* scroll down one line */;
5484 /* Control-Numlock for app-keypad mode switch */
5485 if (keysym
== PK_PF1
)
5486 term
->app_keypad_keys
^= 1;
5490 if (modifiers
& PKM_ALT
) {
5491 /* Alt+F4 (close) */
5492 /* Alt+Return (full screen) */
5493 /* Alt+Space (system menu) */
5496 if (keysym
== PK_NULL
&& (modifiers
& PKM_CONTROL
) && tlen
== 1 &&
5497 text
[0] >= 0x20 && text
[0] <= 0x7e) {
5498 /* ASCII chars + Control */
5499 if ((text
[0] >= 0x40 && text
[0] <= 0x5f) ||
5500 (text
[0] >= 0x61 && text
[0] <= 0x7a))
5504 * Control-2 should return ^@ (0x00), Control-6 should return
5505 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
5506 * the DOS keyboard handling did it, and we have nothing better
5507 * to do with the key combo in question, we'll also map
5508 * Control-Backquote to ^\ (0x1C).
5511 case ' ': text
[0] = 0x00; break;
5512 case '-': text
[0] = 0x1f; break;
5513 case '/': text
[0] = 0x1f; break;
5514 case '2': text
[0] = 0x00; break;
5515 case '3': text
[0] = 0x1b; break;
5516 case '4': text
[0] = 0x1c; break;
5517 case '5': text
[0] = 0x1d; break;
5518 case '6': text
[0] = 0x1e; break;
5519 case '7': text
[0] = 0x1f; break;
5520 case '8': text
[0] = 0x7f; break;
5521 case '`': text
[0] = 0x1c; break;
5526 /* Nethack keypad */
5527 if (term
->cfg
.nethack_keypad
) {
5530 case PK_KP1
: c
= 'b'; break;
5531 case PK_KP2
: c
= 'j'; break;
5532 case PK_KP3
: c
= 'n'; break;
5533 case PK_KP4
: c
= 'h'; break;
5534 case PK_KP5
: c
= '.'; break;
5535 case PK_KP6
: c
= 'l'; break;
5536 case PK_KP7
: c
= 'y'; break;
5537 case PK_KP8
: c
= 'k'; break;
5538 case PK_KP9
: c
= 'u'; break;
5539 default: break; /* else gcc warns `enum value not used' */
5543 if (modifiers
& PKM_CONTROL
)
5545 else if (modifiers
& PKM_SHIFT
)
5553 /* Numeric Keypad */
5554 if (PK_ISKEYPAD(keysym
)) {
5558 * In VT400 mode, PFn always emits an escape sequence. In
5559 * Linux and tilde modes, this only happens in app keypad mode.
5561 if (term
->cfg
.funky_type
== FUNKY_VT400
||
5562 ((term
->cfg
.funky_type
== FUNKY_LINUX
||
5563 term
->cfg
.funky_type
== FUNKY_TILDE
) &&
5564 term
->app_keypad_keys
&& !term
->cfg
.no_applic_k
)) {
5566 case PK_PF1
: xkey
= 'P'; break;
5567 case PK_PF2
: xkey
= 'Q'; break;
5568 case PK_PF3
: xkey
= 'R'; break;
5569 case PK_PF4
: xkey
= 'S'; break;
5570 default: break; /* else gcc warns `enum value not used' */
5573 if (term
->app_keypad_keys
&& !term
->cfg
.no_applic_k
) {
5575 case PK_KP0
: xkey
= 'p'; break;
5576 case PK_KP1
: xkey
= 'q'; break;
5577 case PK_KP2
: xkey
= 'r'; break;
5578 case PK_KP3
: xkey
= 's'; break;
5579 case PK_KP4
: xkey
= 't'; break;
5580 case PK_KP5
: xkey
= 'u'; break;
5581 case PK_KP6
: xkey
= 'v'; break;
5582 case PK_KP7
: xkey
= 'w'; break;
5583 case PK_KP8
: xkey
= 'x'; break;
5584 case PK_KP9
: xkey
= 'y'; break;
5585 case PK_KPDECIMAL
: xkey
= 'n'; break;
5586 case PK_KPENTER
: xkey
= 'M'; break;
5587 default: break; /* else gcc warns `enum value not used' */
5589 if (term
->cfg
.funky_type
== FUNKY_XTERM
&& tlen
> 0) {
5591 * xterm can't see the layout of the keypad, so it has
5592 * to rely on the X keysyms returned by the keys.
5593 * Hence, we look at the strings here, not the PuTTY
5594 * keysyms (which describe the layout).
5598 if (modifiers
& PKM_SHIFT
)
5603 case '/': xkey
= 'o'; break;
5604 case '*': xkey
= 'j'; break;
5605 case '-': xkey
= 'm'; break;
5609 * In all other modes, we try to retain the layout of
5610 * the DEC keypad in application mode.
5614 /* This key covers the '-' and ',' keys on a VT220 */
5615 if (modifiers
& PKM_SHIFT
)
5616 xkey
= 'm'; /* VT220 '-' */
5618 xkey
= 'l'; /* VT220 ',' */
5620 case PK_KPMINUS
: xkey
= 'm'; break;
5621 case PK_KPCOMMA
: xkey
= 'l'; break;
5622 default: break; /* else gcc warns `enum value not used' */
5627 if (term
->vt52_mode
) {
5628 if (xkey
>= 'P' && xkey
<= 'S')
5629 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
5631 p
+= sprintf((char *) p
, "\x1B?%c", xkey
);
5633 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
5636 /* Not in application mode -- treat the number pad as arrow keys? */
5637 if ((flags
& PKF_NUMLOCK
) == 0) {
5639 case PK_KP0
: keysym
= PK_INSERT
; break;
5640 case PK_KP1
: keysym
= PK_END
; break;
5641 case PK_KP2
: keysym
= PK_DOWN
; break;
5642 case PK_KP3
: keysym
= PK_PAGEDOWN
; break;
5643 case PK_KP4
: keysym
= PK_LEFT
; break;
5644 case PK_KP5
: keysym
= PK_REST
; break;
5645 case PK_KP6
: keysym
= PK_RIGHT
; break;
5646 case PK_KP7
: keysym
= PK_HOME
; break;
5647 case PK_KP8
: keysym
= PK_UP
; break;
5648 case PK_KP9
: keysym
= PK_PAGEUP
; break;
5649 default: break; /* else gcc warns `enum value not used' */
5654 /* Miscellaneous keys */
5661 *p
++ = (term
->cfg
.bksp_is_delete ?
0x7F : 0x08);
5662 else if (modifiers
== PKM_SHIFT
)
5663 /* We do the opposite of what is configured */
5664 *p
++ = (term
->cfg
.bksp_is_delete ?
0x08 : 0x7F);
5670 else if (modifiers
== PKM_SHIFT
)
5671 *p
++ = 0x1B, *p
++ = '[', *p
++ = 'Z';
5674 /* XXX window.c has ctrl+shift+space sending 0xa0 */
5676 if (modifiers
== PKM_CONTROL
)
5681 case PK_KPENTER
: /* Odd keypad modes handled above */
5682 if (modifiers
== 0) {
5684 if (term
->cr_lf_return
)
5688 default: break; /* else gcc warns `enum value not used' */
5691 /* SCO function keys and editing keys */
5692 if (term
->cfg
.funky_type
== FUNKY_SCO
) {
5693 if (PK_ISFKEY(keysym
) && keysym
<= PK_F12
) {
5694 static char const codes
[] =
5695 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
5696 int index
= keysym
- PK_F1
;
5698 if (modifiers
& PKM_SHIFT
) index
+= 12;
5699 if (modifiers
& PKM_CONTROL
) index
+= 24;
5700 p
+= sprintf((char *) p
, "\x1B[%c", codes
[index
]);
5703 if (PK_ISEDITING(keysym
)) {
5707 case PK_DELETE
: *p
++ = 0x7f; goto done
;
5708 case PK_HOME
: xkey
= 'H'; break;
5709 case PK_INSERT
: xkey
= 'L'; break;
5710 case PK_END
: xkey
= 'F'; break;
5711 case PK_PAGEUP
: xkey
= 'I'; break;
5712 case PK_PAGEDOWN
: xkey
= 'G'; break;
5713 default: break; /* else gcc warns `enum value not used' */
5715 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
5719 if (PK_ISEDITING(keysym
) && (modifiers
& PKM_SHIFT
) == 0) {
5722 if (term
->cfg
.funky_type
== FUNKY_XTERM
) {
5723 /* Xterm shuffles these keys, apparently. */
5725 case PK_HOME
: keysym
= PK_INSERT
; break;
5726 case PK_INSERT
: keysym
= PK_HOME
; break;
5727 case PK_DELETE
: keysym
= PK_END
; break;
5728 case PK_END
: keysym
= PK_PAGEUP
; break;
5729 case PK_PAGEUP
: keysym
= PK_DELETE
; break;
5730 case PK_PAGEDOWN
: keysym
= PK_PAGEDOWN
; break;
5731 default: break; /* else gcc warns `enum value not used' */
5736 if (term
->cfg
.rxvt_homeend
&&
5737 (keysym
== PK_HOME
|| keysym
== PK_END
)) {
5738 p
+= sprintf((char *) p
, keysym
== PK_HOME ?
"\x1B[H" : "\x1BOw");
5742 if (term
->vt52_mode
) {
5746 * A real VT52 doesn't have these, and a VT220 doesn't
5747 * send anything for them in VT52 mode.
5750 case PK_HOME
: xkey
= 'H'; break;
5751 case PK_INSERT
: xkey
= 'L'; break;
5752 case PK_DELETE
: xkey
= 'M'; break;
5753 case PK_END
: xkey
= 'E'; break;
5754 case PK_PAGEUP
: xkey
= 'I'; break;
5755 case PK_PAGEDOWN
: xkey
= 'G'; break;
5756 default: xkey
=0; break; /* else gcc warns `enum value not used'*/
5758 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
5763 case PK_HOME
: code
= 1; break;
5764 case PK_INSERT
: code
= 2; break;
5765 case PK_DELETE
: code
= 3; break;
5766 case PK_END
: code
= 4; break;
5767 case PK_PAGEUP
: code
= 5; break;
5768 case PK_PAGEDOWN
: code
= 6; break;
5769 default: code
= 0; break; /* else gcc warns `enum value not used' */
5771 p
+= sprintf((char *) p
, "\x1B[%d~", code
);
5775 if (PK_ISFKEY(keysym
)) {
5776 /* Map Shift+F1-F10 to F11-F20 */
5777 if (keysym
>= PK_F1
&& keysym
<= PK_F10
&& (modifiers
& PKM_SHIFT
))
5779 if ((term
->vt52_mode
|| term
->cfg
.funky_type
== FUNKY_VT100P
) &&
5781 /* XXX This overrides the XTERM/VT52 mode below */
5783 if (keysym
>= PK_F6
) offt
++;
5784 if (keysym
>= PK_F12
) offt
++;
5785 p
+= sprintf((char *) p
, term
->vt52_mode ?
"\x1B%c" : "\x1BO%c",
5786 'P' + keysym
- PK_F1
- offt
);
5789 if (term
->cfg
.funky_type
== FUNKY_LINUX
&& keysym
<= PK_F5
) {
5790 p
+= sprintf((char *) p
, "\x1B[[%c", 'A' + keysym
- PK_F1
);
5793 if (term
->cfg
.funky_type
== FUNKY_XTERM
&& keysym
<= PK_F4
) {
5794 if (term
->vt52_mode
)
5795 p
+= sprintf((char *) p
, "\x1B%c", 'P' + keysym
- PK_F1
);
5797 p
+= sprintf((char *) p
, "\x1BO%c", 'P' + keysym
- PK_F1
);
5800 p
+= sprintf((char *) p
, "\x1B[%d~", 11 + keysym
- PK_F1
);
5804 if (PK_ISCURSOR(keysym
)) {
5808 case PK_UP
: xkey
= 'A'; break;
5809 case PK_DOWN
: xkey
= 'B'; break;
5810 case PK_RIGHT
: xkey
= 'C'; break;
5811 case PK_LEFT
: xkey
= 'D'; break;
5812 case PK_REST
: xkey
= 'G'; break; /* centre key on number pad */
5813 default: xkey
= 0; break; /* else gcc warns `enum value not used' */
5815 if (term
->vt52_mode
)
5816 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
5818 int app_flg
= (term
->app_cursor_keys
&& !term
->cfg
.no_applic_c
);
5820 /* Useful mapping of Ctrl-arrows */
5821 if (modifiers
== PKM_CONTROL
)
5825 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
5827 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
5833 if (p
> output
|| tlen
> 0) {
5835 * Interrupt an ongoing paste. I'm not sure
5836 * this is sensible, but for the moment it's
5837 * preferable to having to faff about buffering
5843 * We need not bother about stdin backlogs
5844 * here, because in GUI PuTTY we can't do
5845 * anything about it anyway; there's no means
5846 * of asking Windows to hold off on KEYDOWN
5847 * messages. We _have_ to buffer everything
5850 term_seen_key_event(term
);
5854 fprintf(stderr
, "sending ESC\n");
5856 ldisc_send(term
->ldisc
, "\x1b", 1, 1);
5861 fprintf(stderr
, "sending %d bytes:", p
- output
);
5862 for (i
= 0; i
< p
- output
; i
++)
5863 fprintf(stderr
, " %02x", output
[i
]);
5864 fprintf(stderr
, "\n");
5866 ldisc_send(term
->ldisc
, output
, p
- output
, 1);
5867 } else if (tlen
> 0) {
5869 fprintf(stderr
, "sending %d unichars:", tlen
);
5870 for (i
= 0; i
< tlen
; i
++)
5871 fprintf(stderr
, " %04x", (unsigned) text
[i
]);
5872 fprintf(stderr
, "\n");
5874 luni_send(term
->ldisc
, text
, tlen
, 1);
5879 void term_nopaste(Terminal
*term
)
5881 if (term
->paste_len
== 0)
5883 sfree(term
->paste_buffer
);
5884 term
->paste_buffer
= NULL
;
5885 term
->paste_len
= 0;
5888 int term_paste_pending(Terminal
*term
)
5890 return term
->paste_len
!= 0;
5893 void term_paste(Terminal
*term
)
5895 long now
, paste_diff
;
5897 if (term
->paste_len
== 0)
5900 /* Don't wait forever to paste */
5901 if (term
->paste_hold
) {
5902 now
= GETTICKCOUNT();
5903 paste_diff
= now
- term
->last_paste
;
5904 if (paste_diff
>= 0 && paste_diff
< 450)
5907 term
->paste_hold
= 0;
5909 while (term
->paste_pos
< term
->paste_len
) {
5911 while (n
+ term
->paste_pos
< term
->paste_len
) {
5912 if (term
->paste_buffer
[term
->paste_pos
+ n
++] == '\015')
5916 luni_send(term
->ldisc
, term
->paste_buffer
+ term
->paste_pos
, n
, 0);
5917 term
->paste_pos
+= n
;
5919 if (term
->paste_pos
< term
->paste_len
) {
5920 term
->paste_hold
= 1;
5924 sfree(term
->paste_buffer
);
5925 term
->paste_buffer
= NULL
;
5926 term
->paste_len
= 0;
5929 static void deselect(Terminal
*term
)
5931 term
->selstate
= NO_SELECTION
;
5932 term
->selstart
.x
= term
->selstart
.y
= term
->selend
.x
= term
->selend
.y
= 0;
5935 void term_deselect(Terminal
*term
)
5941 int term_ldisc(Terminal
*term
, int option
)
5943 if (option
== LD_ECHO
)
5944 return term
->term_echoing
;
5945 if (option
== LD_EDIT
)
5946 return term
->term_editing
;
5950 int term_data(Terminal
*term
, int is_stderr
, const char *data
, int len
)
5952 bufchain_add(&term
->inbuf
, data
, len
);
5954 if (!term
->in_term_out
) {
5955 term
->in_term_out
= TRUE
;
5956 term_blink(term
, 1);
5958 term
->in_term_out
= FALSE
;
5962 * term_out() always completely empties inbuf. Therefore,
5963 * there's no reason at all to return anything other than zero
5964 * from this function, because there _can't_ be a question of
5965 * the remote side needing to wait until term_out() has cleared
5968 * This is a slightly suboptimal way to deal with SSH2 - in
5969 * principle, the window mechanism would allow us to continue
5970 * to accept data on forwarded ports and X connections even
5971 * while the terminal processing was going slowly - but we
5972 * can't do the 100% right thing without moving the terminal
5973 * processing into a separate thread, and that might hurt
5974 * portability. So we manage stdout buffering the old SSH1 way:
5975 * if the terminal processing goes slowly, the whole SSH
5976 * connection stops accepting data until it's ready.
5978 * In practice, I can't imagine this causing serious trouble.
5983 void term_provide_logctx(Terminal
*term
, void *logctx
)
5985 term
->logctx
= logctx
;