Stability fixes (thanks valgrind).
[u/mdw/putty] / terminal.c
CommitLineData
374330e2 1#include <stdio.h>
2#include <stdlib.h>
49bad831 3#include <ctype.h>
374330e2 4
e1c8e0ed 5#include <time.h>
c83de303 6#include <assert.h>
374330e2 7#include "putty.h"
887035a5 8#include "terminal.h"
9
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 )
14
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 )
18
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) )
374330e2 21
4eeb7d09 22#define VT52_PLUS
23
32874aea 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 */
e14a5a13 36
c9def1b8 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)
43
44#define TM_PUTTY (0xFFFF)
e14a5a13 45
39934deb 46#define UPDATE_DELAY ((TICKSPERSEC+49)/50)/* ticks to defer window update */
47#define TBLINK_DELAY ((TICKSPERSEC*9+19)/20)/* ticks between text blinks*/
48#define CBLINK_DELAY (CURSORBLINK) /* ticks between cursor blinks */
49#define VBELL_DELAY (VBELL_TIMEOUT) /* visual bell timeout in ticks */
50
e14a5a13 51#define compatibility(x) \
887035a5 52 if ( ((CL_##x)&term->compatibility_level) == 0 ) { \
53 term->termstate=TOPLEVEL; \
e14a5a13 54 break; \
55 }
c9def1b8 56#define compatibility2(x,y) \
887035a5 57 if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \
58 term->termstate=TOPLEVEL; \
c9def1b8 59 break; \
60 }
e14a5a13 61
887035a5 62#define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 )
374330e2 63
1b20b972 64const char sco2ansicolour[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
65
4eeb7d09 66#define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
887035a5 67const wchar_t sel_nl[] = SEL_NL;
374330e2 68
69/*
3c7366f8 70 * Fetch the character at a particular position in a line array,
71 * for purposes of `wordtype'. The reason this isn't just a simple
72 * array reference is that if the character we find is UCSWIDE,
73 * then we must look one space further to the left.
74 */
75#define UCSGET(a, x) \
36566009 76 ( (x)>0 && (a)[(x)].chr == UCSWIDE ? (a)[(x)-1].chr : (a)[(x)].chr )
77
78/*
79 * Detect the various aliases of U+0020 SPACE.
80 */
81#define IS_SPACE_CHR(chr) \
82 ((chr) == 0x20 || (DIRECT_CHAR(chr) && ((chr) & 0xFF) == 0x20))
83
84/*
85 * Spot magic CSETs.
86 */
87#define CSET_OF(chr) (DIRECT_CHAR(chr)||DIRECT_FONT(chr) ? (chr)&CSET_MASK : 0)
3c7366f8 88
89/*
374330e2 90 * Internal prototypes.
91 */
36566009 92static void resizeline(Terminal *, termline *, int);
93static termline *lineptr(Terminal *, int, int, int);
94static void unlineptr(termline *);
887035a5 95static void do_paint(Terminal *, Context, int);
96static void erase_lots(Terminal *, int, int, int);
97static void swap_screen(Terminal *, int, int, int);
98static void update_sbar(Terminal *);
99static void deselect(Terminal *);
100static void term_print_finish(Terminal *);
37d2a505 101#ifdef OPTIMISE_SCROLL
102static void scroll_display(Terminal *, int, int, int);
103#endif /* OPTIMISE_SCROLL */
374330e2 104
36566009 105static termline *newline(Terminal *term, int cols, int bce)
106{
107 termline *line;
108 int j;
109
110 line = snew(termline);
111 line->chars = snewn(cols, termchar);
112 for (j = 0; j < cols; j++)
113 line->chars[j] = (bce ? term->erase_char : term->basic_erase_char);
c6958dfe 114 line->cols = line->size = cols;
36566009 115 line->lattr = LATTR_NORM;
116 line->temporary = FALSE;
c6958dfe 117 line->cc_free = 0;
36566009 118
119 return line;
120}
121
122static void freeline(termline *line)
123{
05bea829 124 if (line) {
125 sfree(line->chars);
126 sfree(line);
127 }
36566009 128}
129
130static void unlineptr(termline *line)
131{
132 if (line->temporary)
133 freeline(line);
134}
135
136/*
c6958dfe 137 * Diagnostic function: verify that a termline has a correct
138 * combining character structure.
4fe051a2 139 *
140 * XXX-REMOVE-BEFORE-RELEASE: This is a performance-intensive
141 * check. Although it's currently really useful for getting all the
142 * bugs out of the new cc stuff, it will want to be absent when we
143 * make a proper release.
c6958dfe 144 */
145static void cc_check(termline *line)
146{
147 unsigned char *flags;
148 int i, j;
149
150 assert(line->size >= line->cols);
151
152 flags = snewn(line->size, unsigned char);
153
154 for (i = 0; i < line->size; i++)
155 flags[i] = (i < line->cols);
156
157 for (i = 0; i < line->cols; i++) {
158 j = i;
159 while (line->chars[j].cc_next) {
160 j += line->chars[j].cc_next;
161 assert(j >= line->cols && j < line->size);
162 assert(!flags[j]);
163 flags[j] = TRUE;
164 }
165 }
166
167 j = line->cc_free;
168 if (j) {
169 while (1) {
170 assert(j >= line->cols && j < line->size);
171 assert(!flags[j]);
172 flags[j] = TRUE;
173 if (line->chars[j].cc_next)
174 j += line->chars[j].cc_next;
175 else
176 break;
177 }
178 }
179
180 j = 0;
181 for (i = 0; i < line->size; i++)
182 j += (flags[i] != 0);
183
184 assert(j == line->size);
dac0aee3 185
186 sfree(flags);
c6958dfe 187}
188
189/*
190 * Add a combining character to a character cell.
191 */
192static void add_cc(termline *line, int col, unsigned long chr)
193{
194 int newcc;
195
196 assert(col >= 0 && col < line->cols);
197
198 /*
199 * Start by extending the cols array if the free list is empty.
200 */
201 if (!line->cc_free) {
202 int n = line->size;
203 line->size += 16 + (line->size - line->cols) / 2;
204 line->chars = sresize(line->chars, line->size, termchar);
205 line->cc_free = n;
206 while (n < line->size) {
207 if (n+1 < line->size)
208 line->chars[n].cc_next = 1;
209 else
210 line->chars[n].cc_next = 0;
211 n++;
212 }
213 }
214
215 /*
216 * Now walk the cc list of the cell in question.
217 */
218 while (line->chars[col].cc_next)
219 col += line->chars[col].cc_next;
220
221 /*
222 * `col' now points at the last cc currently in this cell; so
223 * we simply add another one.
224 */
225 newcc = line->cc_free;
226 if (line->chars[newcc].cc_next)
227 line->cc_free = newcc + line->chars[newcc].cc_next;
228 else
229 line->cc_free = 0;
230 line->chars[newcc].cc_next = 0;
231 line->chars[newcc].chr = chr;
232 line->chars[col].cc_next = newcc - col;
233
4fe051a2 234 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
c6958dfe 235}
236
237/*
238 * Clear the combining character list in a character cell.
239 */
240static void clear_cc(termline *line, int col)
241{
242 int oldfree, origcol = col;
243
244 assert(col >= 0 && col < line->cols);
245
246 if (!line->chars[col].cc_next)
247 return; /* nothing needs doing */
248
249 oldfree = line->cc_free;
250 line->cc_free = col + line->chars[col].cc_next;
251 while (line->chars[col].cc_next)
252 col += line->chars[col].cc_next;
253 if (oldfree)
254 line->chars[col].cc_next = oldfree - col;
255 else
256 line->chars[col].cc_next = 0;
257
258 line->chars[origcol].cc_next = 0;
259
4fe051a2 260 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
c6958dfe 261}
262
263/*
264 * Compare two character cells for equality. Special case required
265 * in do_paint() where we override what we expect the chr and attr
266 * fields to be.
267 */
268static int termchars_equal_override(termchar *a, termchar *b,
269 unsigned long bchr, unsigned long battr)
270{
271 /* FULL-TERMCHAR */
272 if (a->chr != bchr)
273 return FALSE;
274 if (a->attr != battr)
275 return FALSE;
276 while (a->cc_next || b->cc_next) {
277 if (!a->cc_next || !b->cc_next)
278 return FALSE; /* one cc-list ends, other does not */
279 a += a->cc_next;
280 b += b->cc_next;
281 if (a->chr != b->chr)
282 return FALSE;
283 }
284 return TRUE;
285}
286
287static int termchars_equal(termchar *a, termchar *b)
288{
289 return termchars_equal_override(a, b, b->chr, b->attr);
290}
291
292/*
293 * Copy a character cell. (Requires a pointer to the destination
294 * termline, so as to access its free list.)
295 */
296static void copy_termchar(termline *destline, int x, termchar *src)
297{
298 clear_cc(destline, x);
299
300 destline->chars[x] = *src; /* copy everything except cc-list */
301 destline->chars[x].cc_next = 0; /* and make sure this is zero */
302
303 while (src->cc_next) {
304 src += src->cc_next;
305 add_cc(destline, x, src->chr);
306 }
4fe051a2 307
308 cc_check(destline); /* XXX-REMOVE-BEFORE-RELEASE */
c6958dfe 309}
310
311/*
312 * Move a character cell within its termline.
313 */
314static void move_termchar(termline *line, termchar *dest, termchar *src)
315{
316 /* First clear the cc list from the original char, just in case. */
317 clear_cc(line, dest - line->chars);
318
319 /* Move the character cell and adjust its cc_next. */
320 *dest = *src; /* copy everything except cc-list */
321 if (src->cc_next)
322 dest->cc_next = src->cc_next - (dest-src);
323
324 /* Ensure the original cell doesn't have a cc list. */
325 src->cc_next = 0;
4fe051a2 326
327 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
c6958dfe 328}
329
330/*
36566009 331 * Compress and decompress a termline into an RLE-based format for
332 * storing in scrollback. (Since scrollback almost never needs to
333 * be modified and exists in huge quantities, this is a sensible
334 * tradeoff, particularly since it allows us to continue adding
335 * features to the main termchar structure without proportionally
336 * bloating the terminal emulator's memory footprint unless those
337 * features are in constant use.)
338 */
339struct buf {
340 unsigned char *data;
341 int len, size;
342};
343static void add(struct buf *b, unsigned char c)
344{
345 if (b->len >= b->size) {
346 b->size = (b->len * 3 / 2) + 512;
347 b->data = sresize(b->data, b->size, unsigned char);
348 }
349 b->data[b->len++] = c;
350}
351static int get(struct buf *b)
352{
353 return b->data[b->len++];
354}
355static void makerle(struct buf *b, termline *ldata,
356 void (*makeliteral)(struct buf *b, termchar *c,
357 unsigned long *state))
358{
359 int hdrpos, hdrsize, n, prevlen, prevpos, thislen, thispos, prev2;
360 termchar *c = ldata->chars;
361 unsigned long state = 0, oldstate;
362
363 n = ldata->cols;
364
365 hdrpos = b->len;
366 hdrsize = 0;
367 add(b, 0);
368 prevlen = prevpos = 0;
369 prev2 = FALSE;
370
371 while (n-- > 0) {
372 thispos = b->len;
373 makeliteral(b, c++, &state);
374 thislen = b->len - thispos;
375 if (thislen == prevlen &&
376 !memcmp(b->data + prevpos, b->data + thispos, thislen)) {
377 /*
378 * This literal precisely matches the previous one.
379 * Turn it into a run if it's worthwhile.
380 *
381 * With one-byte literals, it costs us two bytes to
382 * encode a run, plus another byte to write the header
383 * to resume normal output; so a three-element run is
384 * neutral, and anything beyond that is unconditionally
385 * worthwhile. With two-byte literals or more, even a
386 * 2-run is a win.
387 */
388 if (thislen > 1 || prev2) {
389 int runpos, runlen;
390
391 /*
392 * It's worth encoding a run. Start at prevpos,
393 * unless hdrsize==0 in which case we can back up
394 * another one and start by overwriting hdrpos.
395 */
396
397 hdrsize--; /* remove the literal at prevpos */
398 if (prev2) {
399 assert(hdrsize > 0);
400 hdrsize--;
401 prevpos -= prevlen;/* and possibly another one */
402 }
403
404 if (hdrsize == 0) {
405 assert(prevpos == hdrpos + 1);
406 runpos = hdrpos;
407 b->len = prevpos+prevlen;
408 } else {
409 memmove(b->data + prevpos+1, b->data + prevpos, prevlen);
410 runpos = prevpos;
411 b->len = prevpos+prevlen+1;
412 /*
413 * Terminate the previous run of ordinary
414 * literals.
415 */
416 assert(hdrsize >= 1 && hdrsize <= 128);
417 b->data[hdrpos] = hdrsize - 1;
418 }
419
420 runlen = prev2 ? 3 : 2;
421
422 while (n > 0 && runlen < 129) {
423 int tmppos, tmplen;
424 tmppos = b->len;
425 oldstate = state;
426 makeliteral(b, c, &state);
427 tmplen = b->len - tmppos;
428 b->len = tmppos;
429 if (tmplen != thislen ||
430 memcmp(b->data + runpos+1, b->data + tmppos, tmplen)) {
431 state = oldstate;
432 break; /* run over */
433 }
434 n--, c++, runlen++;
435 }
436
437 assert(runlen >= 2 && runlen <= 129);
438 b->data[runpos] = runlen + 0x80 - 2;
439
440 hdrpos = b->len;
441 hdrsize = 0;
442 add(b, 0);
cc23f40f 443 /* And ensure this run doesn't interfere with the next. */
444 prevlen = prevpos = 0;
445 prev2 = FALSE;
36566009 446
447 continue;
448 } else {
449 /*
450 * Just flag that the previous two literals were
451 * identical, in case we find a third identical one
452 * we want to turn into a run.
453 */
454 prev2 = TRUE;
455 prevlen = thislen;
456 prevpos = thispos;
457 }
458 } else {
459 prev2 = FALSE;
460 prevlen = thislen;
461 prevpos = thispos;
462 }
463
464 /*
465 * This character isn't (yet) part of a run. Add it to
466 * hdrsize.
467 */
468 hdrsize++;
469 if (hdrsize == 128) {
470 b->data[hdrpos] = hdrsize - 1;
471 hdrpos = b->len;
472 hdrsize = 0;
473 add(b, 0);
474 prevlen = prevpos = 0;
475 prev2 = FALSE;
476 }
477 }
478
479 /*
480 * Clean up.
481 */
482 if (hdrsize > 0) {
483 assert(hdrsize <= 128);
484 b->data[hdrpos] = hdrsize - 1;
485 } else {
486 b->len = hdrpos;
487 }
488}
489static void makeliteral_chr(struct buf *b, termchar *c, unsigned long *state)
490{
491 /*
492 * My encoding for characters is UTF-8-like, in that it stores
493 * 7-bit ASCII in one byte and uses high-bit-set bytes as
494 * introducers to indicate a longer sequence. However, it's
495 * unlike UTF-8 in that it doesn't need to be able to
496 * resynchronise, and therefore I don't want to waste two bits
497 * per byte on having recognisable continuation characters.
498 * Also I don't want to rule out the possibility that I may one
499 * day use values 0x80000000-0xFFFFFFFF for interesting
500 * purposes, so unlike UTF-8 I need a full 32-bit range.
501 * Accordingly, here is my encoding:
502 *
503 * 00000000-0000007F: 0xxxxxxx (but see below)
504 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
505 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
506 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
507 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
508 *
509 * (`Z' is like `x' but is always going to be zero since the
510 * values I'm encoding don't go above 2^32. In principle the
511 * five-byte form of the encoding could extend to 2^35, and
512 * there could be six-, seven-, eight- and nine-byte forms as
513 * well to allow up to 64-bit values to be encoded. But that's
514 * completely unnecessary for these purposes!)
515 *
516 * The encoding as written above would be very simple, except
517 * that 7-bit ASCII can occur in several different ways in the
518 * terminal data; sometimes it crops up in the D800 page
519 * (CSET_ASCII) but at other times it's in the 0000 page (real
520 * Unicode). Therefore, this encoding is actually _stateful_:
521 * the one-byte encoding of 00-7F actually indicates `reuse the
522 * upper three bytes of the last character', and to encode an
523 * absolute value of 00-7F you need to use the two-byte form
524 * instead.
525 */
526 if ((c->chr & ~0x7F) == *state) {
527 add(b, (unsigned char)(c->chr & 0x7F));
528 } else if (c->chr < 0x4000) {
529 add(b, (unsigned char)(((c->chr >> 8) & 0x3F) | 0x80));
530 add(b, (unsigned char)(c->chr & 0xFF));
531 } else if (c->chr < 0x200000) {
532 add(b, (unsigned char)(((c->chr >> 16) & 0x1F) | 0xC0));
533 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
534 add(b, (unsigned char)(c->chr & 0xFF));
535 } else if (c->chr < 0x10000000) {
536 add(b, (unsigned char)(((c->chr >> 24) & 0x0F) | 0xE0));
537 add(b, (unsigned char)((c->chr >> 16) & 0xFF));
538 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
539 add(b, (unsigned char)(c->chr & 0xFF));
540 } else {
541 add(b, 0xF0);
542 add(b, (unsigned char)((c->chr >> 24) & 0xFF));
543 add(b, (unsigned char)((c->chr >> 16) & 0xFF));
544 add(b, (unsigned char)((c->chr >> 8) & 0xFF));
545 add(b, (unsigned char)(c->chr & 0xFF));
546 }
547 *state = c->chr & ~0xFF;
548}
549static void makeliteral_attr(struct buf *b, termchar *c, unsigned long *state)
550{
551 /*
552 * My encoding for attributes is 16-bit-granular and assumes
553 * that the top bit of the word is never required. I either
554 * store a two-byte value with the top bit clear (indicating
555 * just that value), or a four-byte value with the top bit set
556 * (indicating the same value with its top bit clear).
557 */
558 if (c->attr < 0x8000) {
559 add(b, (unsigned char)((c->attr >> 8) & 0xFF));
560 add(b, (unsigned char)(c->attr & 0xFF));
561 } else {
562 add(b, (unsigned char)(((c->attr >> 24) & 0x7F) | 0x80));
563 add(b, (unsigned char)((c->attr >> 16) & 0xFF));
564 add(b, (unsigned char)((c->attr >> 8) & 0xFF));
565 add(b, (unsigned char)(c->attr & 0xFF));
566 }
567}
c6958dfe 568static void makeliteral_cc(struct buf *b, termchar *c, unsigned long *state)
569{
570 /*
571 * For combining characters, I just encode a bunch of ordinary
572 * chars using makeliteral_chr, and terminate with a \0
573 * character (which I know won't come up as a combining char
574 * itself).
575 *
576 * I don't use the stateful encoding in makeliteral_chr.
577 */
578 unsigned long zstate;
579 termchar z;
580
581 while (c->cc_next) {
582 c += c->cc_next;
583
584 assert(c->chr != 0);
585
586 zstate = 0;
587 makeliteral_chr(b, c, &zstate);
588 }
589
590 z.chr = 0;
591 zstate = 0;
592 makeliteral_chr(b, &z, &zstate);
593}
36566009 594
595static termline *decompressline(unsigned char *data, int *bytes_used);
596
597static unsigned char *compressline(termline *ldata)
598{
599 struct buf buffer = { NULL, 0, 0 }, *b = &buffer;
600
601 /*
602 * First, store the column count, 7 bits at a time, least
603 * significant `digit' first, with the high bit set on all but
604 * the last.
605 */
606 {
607 int n = ldata->cols;
608 while (n >= 128) {
609 add(b, (unsigned char)((n & 0x7F) | 0x80));
610 n >>= 7;
611 }
612 add(b, (unsigned char)(n));
613 }
614
615 /*
616 * Next store the lattrs; same principle.
617 */
618 {
619 int n = ldata->lattr;
620 while (n >= 128) {
621 add(b, (unsigned char)((n & 0x7F) | 0x80));
622 n >>= 7;
623 }
624 add(b, (unsigned char)(n));
625 }
626
627 /*
628 * Now we store a sequence of separate run-length encoded
629 * fragments, each containing exactly as many symbols as there
630 * are columns in the ldata.
631 *
632 * All of these have a common basic format:
633 *
634 * - a byte 00-7F indicates that X+1 literals follow it
635 * - a byte 80-FF indicates that a single literal follows it
636 * and expects to be repeated (X-0x80)+2 times.
637 *
638 * The format of the `literals' varies between the fragments.
639 */
640 makerle(b, ldata, makeliteral_chr);
641 makerle(b, ldata, makeliteral_attr);
c6958dfe 642 makerle(b, ldata, makeliteral_cc);
36566009 643
644 /*
645 * Diagnostics: ensure that the compressed data really does
646 * decompress to the right thing.
49d46cb5 647 *
648 * XXX-REMOVE-BEFORE-RELEASE: This is a bit performance-heavy
649 * to be leaving in production code.
36566009 650 */
651#ifndef CHECK_SB_COMPRESSION
652 {
653 int dused;
654 termline *dcl;
c6958dfe 655 int i;
36566009 656
657#ifdef DIAGNOSTIC_SB_COMPRESSION
36566009 658 for (i = 0; i < b->len; i++) {
659 printf(" %02x ", b->data[i]);
660 }
661 printf("\n");
662#endif
663
664 dcl = decompressline(b->data, &dused);
665 assert(b->len == dused);
666 assert(ldata->cols == dcl->cols);
667 assert(ldata->lattr == dcl->lattr);
c6958dfe 668 for (i = 0; i < ldata->cols; i++)
669 assert(termchars_equal(&ldata->chars[i], &dcl->chars[i]));
36566009 670
671#ifdef DIAGNOSTIC_SB_COMPRESSION
672 printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",
673 ldata->cols, 4 * ldata->cols, dused,
674 (double)dused / (4 * ldata->cols));
675#endif
676
677 freeline(dcl);
678 }
679#endif
680
681 /*
682 * Trim the allocated memory so we don't waste any, and return.
683 */
684 return sresize(b->data, b->len, unsigned char);
685}
686
687static void readrle(struct buf *b, termline *ldata,
688 void (*readliteral)(struct buf *b, termchar *c,
c6958dfe 689 termline *ldata, unsigned long *state))
36566009 690{
691 int n = 0;
692 unsigned long state = 0;
693
694 while (n < ldata->cols) {
695 int hdr = get(b);
696
697 if (hdr >= 0x80) {
698 /* A run. */
699
700 int pos = b->len, count = hdr + 2 - 0x80;
701 while (count--) {
702 assert(n < ldata->cols);
703 b->len = pos;
c6958dfe 704 readliteral(b, ldata->chars + n, ldata, &state);
36566009 705 n++;
706 }
707 } else {
708 /* Just a sequence of consecutive literals. */
709
710 int count = hdr + 1;
711 while (count--) {
712 assert(n < ldata->cols);
c6958dfe 713 readliteral(b, ldata->chars + n, ldata, &state);
36566009 714 n++;
715 }
716 }
717 }
718
719 assert(n == ldata->cols);
720}
c6958dfe 721static void readliteral_chr(struct buf *b, termchar *c, termline *ldata,
722 unsigned long *state)
36566009 723{
724 int byte;
725
726 /*
727 * 00000000-0000007F: 0xxxxxxx
728 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
729 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
730 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
731 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
732 */
733
734 byte = get(b);
735 if (byte < 0x80) {
736 c->chr = byte | *state;
737 } else if (byte < 0xC0) {
738 c->chr = (byte &~ 0xC0) << 8;
739 c->chr |= get(b);
740 } else if (byte < 0xE0) {
741 c->chr = (byte &~ 0xE0) << 16;
742 c->chr |= get(b) << 8;
743 c->chr |= get(b);
744 } else if (byte < 0xF0) {
745 c->chr = (byte &~ 0xF0) << 24;
746 c->chr |= get(b) << 16;
747 c->chr |= get(b) << 8;
748 c->chr |= get(b);
749 } else {
750 assert(byte == 0xF0);
751 c->chr = get(b) << 24;
752 c->chr |= get(b) << 16;
753 c->chr |= get(b) << 8;
754 c->chr |= get(b);
755 }
756 *state = c->chr & ~0xFF;
757}
c6958dfe 758static void readliteral_attr(struct buf *b, termchar *c, termline *ldata,
759 unsigned long *state)
36566009 760{
761 int val;
762
763 val = get(b) << 8;
764 val |= get(b);
765
766 if (val >= 0x8000) {
767 val <<= 16;
768 val |= get(b) << 8;
769 val |= get(b);
770 }
771
772 c->attr = val;
773}
c6958dfe 774static void readliteral_cc(struct buf *b, termchar *c, termline *ldata,
775 unsigned long *state)
776{
777 termchar n;
778 unsigned long zstate;
779 int x = c - ldata->chars;
780
781 c->cc_next = 0;
782
783 while (1) {
784 zstate = 0;
785 readliteral_chr(b, &n, ldata, &zstate);
786 if (!n.chr)
787 break;
788 add_cc(ldata, x, n.chr);
789 }
790}
36566009 791
792static termline *decompressline(unsigned char *data, int *bytes_used)
793{
794 int ncols, byte, shift;
795 struct buf buffer, *b = &buffer;
796 termline *ldata;
797
798 b->data = data;
799 b->len = 0;
800
801 /*
802 * First read in the column count.
803 */
804 ncols = shift = 0;
805 do {
806 byte = get(b);
807 ncols |= (byte & 0x7F) << shift;
808 shift += 7;
809 } while (byte & 0x80);
810
811 /*
812 * Now create the output termline.
813 */
814 ldata = snew(termline);
815 ldata->chars = snewn(ncols, termchar);
c6958dfe 816 ldata->cols = ldata->size = ncols;
36566009 817 ldata->temporary = TRUE;
c6958dfe 818 ldata->cc_free = 0;
819
820 /*
821 * We must set all the cc pointers in ldata->chars to 0 right
822 * now, so that cc diagnostics that verify the integrity of the
823 * whole line will make sense while we're in the middle of
824 * building it up.
825 */
826 {
827 int i;
828 for (i = 0; i < ldata->cols; i++)
829 ldata->chars[i].cc_next = 0;
830 }
36566009 831
832 /*
833 * Now read in the lattr.
834 */
835 ldata->lattr = shift = 0;
836 do {
837 byte = get(b);
838 ldata->lattr |= (byte & 0x7F) << shift;
839 shift += 7;
840 } while (byte & 0x80);
841
842 /*
843 * Now we read in each of the RLE streams in turn.
844 */
845 readrle(b, ldata, readliteral_chr);
846 readrle(b, ldata, readliteral_attr);
c6958dfe 847 readrle(b, ldata, readliteral_cc);
36566009 848
849 /* Return the number of bytes read, for diagnostic purposes. */
850 if (bytes_used)
851 *bytes_used = b->len;
852
853 return ldata;
854}
855
374330e2 856/*
2371caac 857 * Resize a line to make it `cols' columns wide.
858 */
36566009 859static void resizeline(Terminal *term, termline *line, int cols)
2371caac 860{
fc5a96b6 861 int i, oldcols;
2371caac 862
36566009 863 if (line->cols != cols) {
fc5a96b6 864
865 oldcols = line->cols;
866
2371caac 867 /*
868 * This line is the wrong length, which probably means it
869 * hasn't been accessed since a resize. Resize it now.
c6958dfe 870 *
871 * First, go through all the characters that will be thrown
872 * out in the resize (if we're shrinking the line) and
873 * return their cc lists to the cc free list.
874 */
fc5a96b6 875 for (i = cols; i < oldcols; i++)
c6958dfe 876 clear_cc(line, i);
877
878 /*
fc5a96b6 879 * If we're shrinking the line, we now bodily move the
880 * entire cc section from where it started to where it now
881 * needs to be. (We have to do this before the resize, so
882 * that the data we're copying is still there. However, if
883 * we're expanding, we have to wait until _after_ the
884 * resize so that the space we're copying into is there.)
885 */
886 if (cols < oldcols)
887 memmove(line->chars + cols, line->chars + oldcols,
888 (line->size - line->cols) * TSIZE);
889
890 /*
c6958dfe 891 * Now do the actual resize, leaving the _same_ amount of
892 * cc space as there was to begin with.
2371caac 893 */
fc5a96b6 894 line->size += cols - oldcols;
c6958dfe 895 line->chars = sresize(line->chars, line->size, TTYPE);
36566009 896 line->cols = cols;
c6958dfe 897
898 /*
fc5a96b6 899 * If we're expanding the line, _now_ we move the cc
900 * section.
c6958dfe 901 */
fc5a96b6 902 if (cols > oldcols)
903 memmove(line->chars + cols, line->chars + oldcols,
904 (line->size - line->cols) * TSIZE);
c6958dfe 905
906 /*
907 * Go through what's left of the original line, and adjust
908 * the first cc_next pointer in each list. (All the
909 * subsequent ones are still valid because they are
910 * relative offsets within the cc block.) Also do the same
911 * to the head of the cc_free list.
912 */
fc5a96b6 913 for (i = 0; i < oldcols && i < cols; i++)
c6958dfe 914 if (line->chars[i].cc_next)
fc5a96b6 915 line->chars[i].cc_next += cols - oldcols;
c6958dfe 916 if (line->cc_free)
fc5a96b6 917 line->cc_free += cols - oldcols;
c6958dfe 918
919 /*
920 * And finally fill in the new space with erase chars. (We
921 * don't have to worry about cc lists here, because we
922 * _know_ the erase char doesn't have one.)
923 */
fc5a96b6 924 for (i = oldcols; i < cols; i++)
36566009 925 line->chars[i] = term->basic_erase_char;
c6958dfe 926
4fe051a2 927 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
2371caac 928 }
2371caac 929}
930
931/*
876e5d5e 932 * Get the number of lines in the scrollback.
933 */
934static int sblines(Terminal *term)
935{
936 int sblines = count234(term->scrollback);
937 if (term->cfg.erase_to_scrollback &&
938 term->alt_which && term->alt_screen) {
939 sblines += term->alt_sblines;
940 }
941 return sblines;
942}
943
944/*
c83de303 945 * Retrieve a line of the screen or of the scrollback, according to
946 * whether the y coordinate is non-negative or negative
947 * (respectively).
948 */
36566009 949static termline *lineptr(Terminal *term, int y, int lineno, int screen)
32874aea 950{
36566009 951 termline *line;
c83de303 952 tree234 *whichtree;
2371caac 953 int treeindex;
c83de303 954
955 if (y >= 0) {
887035a5 956 whichtree = term->screen;
c83de303 957 treeindex = y;
958 } else {
876e5d5e 959 int altlines = 0;
36566009 960
961 assert(!screen);
962
876e5d5e 963 if (term->cfg.erase_to_scrollback &&
964 term->alt_which && term->alt_screen) {
965 altlines = term->alt_sblines;
966 }
967 if (y < -altlines) {
968 whichtree = term->scrollback;
969 treeindex = y + altlines + count234(term->scrollback);
970 } else {
971 whichtree = term->alt_screen;
972 treeindex = y + term->alt_sblines;
973 /* treeindex = y + count234(term->alt_screen); */
974 }
c83de303 975 }
36566009 976 if (whichtree == term->scrollback) {
977 unsigned char *cline = index234(whichtree, treeindex);
978 line = decompressline(cline, NULL);
979 } else {
980 line = index234(whichtree, treeindex);
981 }
c83de303 982
983 /* We assume that we don't screw up and retrieve something out of range. */
c83de303 984 assert(line != NULL);
c83de303 985
36566009 986 resizeline(term, line, term->cols);
987 /* FIXME: should we sort the compressed scrollback out here? */
c83de303 988
36566009 989 return line;
c83de303 990}
32874aea 991
36566009 992#define lineptr(x) (lineptr)(term,x,__LINE__,FALSE)
993#define scrlineptr(x) (lineptr)(term,x,__LINE__,TRUE)
887035a5 994
39934deb 995static void term_schedule_tblink(Terminal *term);
996static void term_schedule_cblink(Terminal *term);
997
998static void term_timer(void *ctx, long now)
999{
1000 Terminal *term = (Terminal *)ctx;
1001 int update = FALSE;
1002
1003 if (term->tblink_pending && now - term->next_tblink >= 0) {
1004 term->tblinker = !term->tblinker;
1005 term->tblink_pending = FALSE;
1006 term_schedule_tblink(term);
1007 update = TRUE;
1008 }
1009
1010 if (term->cblink_pending && now - term->next_cblink >= 0) {
1011 term->cblinker = !term->cblinker;
1012 term->cblink_pending = FALSE;
1013 term_schedule_cblink(term);
1014 update = TRUE;
1015 }
1016
1017 if (term->in_vbell && now - term->vbell_end >= 0) {
1018 term->in_vbell = FALSE;
1019 update = TRUE;
1020 }
1021
1022 if (update ||
1023 (term->window_update_pending && now - term->next_update >= 0))
1024 term_update(term);
1025}
1026
1027/*
1028 * Call this whenever the terminal window state changes, to queue
1029 * an update.
1030 */
1031static void seen_disp_event(Terminal *term)
1032{
1033 term->seen_disp_event = TRUE; /* for scrollback-reset-on-activity */
1034 if (!term->window_update_pending) {
1035 term->window_update_pending = TRUE;
1036 term->next_update = schedule_timer(UPDATE_DELAY, term_timer, term);
1037 }
1038}
1039
1040/*
1041 * Call when the terminal's blinking-text settings change, or when
1042 * a text blink has just occurred.
1043 */
1044static void term_schedule_tblink(Terminal *term)
1045{
39934deb 1046 if (term->blink_is_real) {
1cff1320 1047 if (!term->tblink_pending)
1048 term->next_tblink = schedule_timer(TBLINK_DELAY, term_timer, term);
39934deb 1049 term->tblink_pending = TRUE;
1050 } else {
1051 term->tblinker = 1; /* reset when not in use */
1052 term->tblink_pending = FALSE;
1053 }
1054}
1055
1056/*
1057 * Likewise with cursor blinks.
1058 */
1059static void term_schedule_cblink(Terminal *term)
1060{
1cff1320 1061 if (term->cfg.blink_cur && term->has_focus) {
1062 if (!term->cblink_pending)
1063 term->next_cblink = schedule_timer(CBLINK_DELAY, term_timer, term);
39934deb 1064 term->cblink_pending = TRUE;
1065 } else {
1066 term->cblinker = 1; /* reset when not in use */
1067 term->cblink_pending = FALSE;
1068 }
1069}
1070
1071/*
1072 * Call to reset cursor blinking on new output.
1073 */
1074static void term_reset_cblink(Terminal *term)
1075{
1076 seen_disp_event(term);
1077 term->cblinker = 1;
1078 term->cblink_pending = FALSE;
1079 term_schedule_cblink(term);
1080}
1081
1082/*
1083 * Call to begin a visual bell.
1084 */
1085static void term_schedule_vbell(Terminal *term, int already_started,
1086 long startpoint)
1087{
1088 long ticks_already_gone;
1089
1090 if (already_started)
1091 ticks_already_gone = GETTICKCOUNT() - startpoint;
1092 else
1093 ticks_already_gone = 0;
1094
1095 if (ticks_already_gone < VBELL_DELAY) {
1096 term->in_vbell = TRUE;
1097 term->vbell_end = schedule_timer(VBELL_DELAY - ticks_already_gone,
1098 term_timer, term);
1099 } else {
1100 term->in_vbell = FALSE;
1101 }
1102}
1103
c83de303 1104/*
374330e2 1105 * Set up power-on settings for the terminal.
1106 */
887035a5 1107static void power_on(Terminal *term)
32874aea 1108{
887035a5 1109 term->curs.x = term->curs.y = 0;
1110 term->alt_x = term->alt_y = 0;
1111 term->savecurs.x = term->savecurs.y = 0;
1112 term->alt_t = term->marg_t = 0;
1113 if (term->rows != -1)
1114 term->alt_b = term->marg_b = term->rows - 1;
374330e2 1115 else
887035a5 1116 term->alt_b = term->marg_b = 0;
1117 if (term->cols != -1) {
374330e2 1118 int i;
887035a5 1119 for (i = 0; i < term->cols; i++)
1120 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
374330e2 1121 }
64734920 1122 term->alt_om = term->dec_om = term->cfg.dec_om;
f278d6f8 1123 term->alt_ins = term->insert = FALSE;
1124 term->alt_wnext = term->wrapnext = term->save_wnext = FALSE;
64734920 1125 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
f278d6f8 1126 term->alt_cset = term->cset = term->save_cset = 0;
1127 term->alt_utf = term->utf = term->save_utf = 0;
1128 term->utf_state = 0;
1129 term->alt_sco_acs = term->sco_acs = term->save_sco_acs = 0;
36566009 1130 term->cset_attr[0] = term->cset_attr[1] = term->save_csattr = CSET_ASCII;
887035a5 1131 term->rvideo = 0;
1132 term->in_vbell = FALSE;
1133 term->cursor_on = 1;
1134 term->big_cursor = 0;
69761fd5 1135 term->default_attr = term->save_attr = term->curr_attr = ATTR_DEFAULT;
887035a5 1136 term->term_editing = term->term_echoing = FALSE;
64734920 1137 term->app_cursor_keys = term->cfg.app_cursor;
1138 term->app_keypad_keys = term->cfg.app_keypad;
1139 term->use_bce = term->cfg.bce;
1140 term->blink_is_real = term->cfg.blinktext;
36566009 1141 term->erase_char = term->basic_erase_char;
887035a5 1142 term->alt_which = 0;
1143 term_print_finish(term);
374330e2 1144 {
1145 int i;
1146 for (i = 0; i < 256; i++)
64734920 1147 term->wordness[i] = term->cfg.wordness[i];
374330e2 1148 }
887035a5 1149 if (term->screen) {
1150 swap_screen(term, 1, FALSE, FALSE);
1151 erase_lots(term, FALSE, TRUE, TRUE);
1152 swap_screen(term, 0, FALSE, FALSE);
1153 erase_lots(term, FALSE, TRUE, TRUE);
374330e2 1154 }
39934deb 1155 term_schedule_tblink(term);
1156 term_schedule_cblink(term);
374330e2 1157}
1158
1159/*
1160 * Force a screen update.
1161 */
887035a5 1162void term_update(Terminal *term)
32874aea 1163{
374330e2 1164 Context ctx;
39934deb 1165
1166 term->window_update_pending = FALSE;
1167
a8327734 1168 ctx = get_ctx(term->frontend);
374330e2 1169 if (ctx) {
887035a5 1170 int need_sbar_update = term->seen_disp_event;
64734920 1171 if (term->seen_disp_event && term->cfg.scroll_on_disp) {
887035a5 1172 term->disptop = 0; /* return to main screen */
1173 term->seen_disp_event = 0;
4f892125 1174 need_sbar_update = TRUE;
cabfd08c 1175 }
f0fccd51 1176
4f892125 1177 if (need_sbar_update)
887035a5 1178 update_sbar(term);
1179 do_paint(term, ctx, TRUE);
a8327734 1180 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
32874aea 1181 free_ctx(ctx);
374330e2 1182 }
1183}
1184
1185/*
2cb50250 1186 * Called from front end when a keypress occurs, to trigger
1187 * anything magical that needs to happen in that situation.
1188 */
887035a5 1189void term_seen_key_event(Terminal *term)
2cb50250 1190{
1191 /*
1192 * On any keypress, clear the bell overload mechanism
1193 * completely, on the grounds that large numbers of
1194 * beeps coming from deliberate key action are likely
1195 * to be intended (e.g. beeps from filename completion
1196 * blocking repeatedly).
1197 */
887035a5 1198 term->beep_overloaded = FALSE;
1199 while (term->beephead) {
1200 struct beeptime *tmp = term->beephead;
1201 term->beephead = tmp->next;
2cb50250 1202 sfree(tmp);
1203 }
887035a5 1204 term->beeptail = NULL;
1205 term->nbeeps = 0;
2cb50250 1206
1207 /*
1208 * Reset the scrollback on keypress, if we're doing that.
1209 */
64734920 1210 if (term->cfg.scroll_on_key) {
887035a5 1211 term->disptop = 0; /* return to main screen */
39934deb 1212 seen_disp_event(term);
2cb50250 1213 }
1214}
1215
1216/*
374330e2 1217 * Same as power_on(), but an external function.
1218 */
887035a5 1219void term_pwron(Terminal *term)
32874aea 1220{
887035a5 1221 power_on(term);
b9d7bcad 1222 if (term->ldisc) /* cause ldisc to notice changes */
1223 ldisc_send(term->ldisc, NULL, 0, 0);
887035a5 1224 term->disptop = 0;
1225 deselect(term);
1226 term_update(term);
374330e2 1227}
1228
36566009 1229static void set_erase_char(Terminal *term)
1230{
1231 term->erase_char = term->basic_erase_char;
1232 if (term->use_bce)
1233 term->erase_char.attr = (term->curr_attr &
1234 (ATTR_FGMASK | ATTR_BGMASK));
1235}
1236
374330e2 1237/*
0d2086c5 1238 * When the user reconfigures us, we need to check the forbidden-
b44b307a 1239 * alternate-screen config option, disable raw mouse mode if the
1240 * user has disabled mouse reporting, and abandon a print job if
1241 * the user has disabled printing.
0d2086c5 1242 */
64734920 1243void term_reconfig(Terminal *term, Config *cfg)
0d2086c5 1244{
64734920 1245 /*
1246 * Before adopting the new config, check all those terminal
1247 * settings which control power-on defaults; and if they've
1248 * changed, we will modify the current state as well as the
1249 * default one. The full list is: Auto wrap mode, DEC Origin
1250 * Mode, BCE, blinking text, character classes.
1251 */
39934deb 1252 int reset_wrap, reset_decom, reset_bce, reset_tblink, reset_charclass;
64734920 1253 int i;
1254
1255 reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode);
1256 reset_decom = (term->cfg.dec_om != cfg->dec_om);
1257 reset_bce = (term->cfg.bce != cfg->bce);
39934deb 1258 reset_tblink = (term->cfg.blinktext != cfg->blinktext);
64734920 1259 reset_charclass = 0;
1260 for (i = 0; i < lenof(term->cfg.wordness); i++)
1261 if (term->cfg.wordness[i] != cfg->wordness[i])
1262 reset_charclass = 1;
1263
041d8f30 1264 /*
1265 * If the bidi or shaping settings have changed, flush the bidi
1266 * cache completely.
1267 */
1268 if (term->cfg.arabicshaping != cfg->arabicshaping ||
1269 term->cfg.bidi != cfg->bidi) {
1270 for (i = 0; i < term->bidi_cache_size; i++) {
1271 sfree(term->pre_bidi_cache[i].chars);
1272 sfree(term->post_bidi_cache[i].chars);
1273 term->pre_bidi_cache[i].width = -1;
1274 term->pre_bidi_cache[i].chars = NULL;
1275 term->post_bidi_cache[i].width = -1;
1276 term->post_bidi_cache[i].chars = NULL;
1277 }
1278 }
1279
64734920 1280 term->cfg = *cfg; /* STRUCTURE COPY */
1281
1282 if (reset_wrap)
1283 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
1284 if (reset_decom)
1285 term->alt_om = term->dec_om = term->cfg.dec_om;
7d5c3da4 1286 if (reset_bce) {
64734920 1287 term->use_bce = term->cfg.bce;
36566009 1288 set_erase_char(term);
7d5c3da4 1289 }
39934deb 1290 if (reset_tblink) {
64734920 1291 term->blink_is_real = term->cfg.blinktext;
39934deb 1292 }
64734920 1293 if (reset_charclass)
1294 for (i = 0; i < 256; i++)
1295 term->wordness[i] = term->cfg.wordness[i];
1296
1297 if (term->cfg.no_alt_screen)
887035a5 1298 swap_screen(term, 0, FALSE, FALSE);
64734920 1299 if (term->cfg.no_mouse_rep) {
887035a5 1300 term->xterm_mouse = 0;
a8327734 1301 set_raw_mouse_mode(term->frontend, 0);
c0d36a72 1302 }
64734920 1303 if (term->cfg.no_remote_charset) {
36566009 1304 term->cset_attr[0] = term->cset_attr[1] = CSET_ASCII;
887035a5 1305 term->sco_acs = term->alt_sco_acs = 0;
1306 term->utf = 0;
0d2086c5 1307 }
64734920 1308 if (!*term->cfg.printer) {
887035a5 1309 term_print_finish(term);
b44b307a 1310 }
39934deb 1311 term_schedule_tblink(term);
1312 term_schedule_cblink(term);
0d2086c5 1313}
1314
1315/*
374330e2 1316 * Clear the scrollback.
1317 */
887035a5 1318void term_clrsb(Terminal *term)
32874aea 1319{
36566009 1320 termline *line;
887035a5 1321 term->disptop = 0;
1322 while ((line = delpos234(term->scrollback, 0)) != NULL) {
36566009 1323 sfree(line); /* this is compressed data, not a termline */
4facdf84 1324 }
4683b9b6 1325 term->tempsblines = 0;
876e5d5e 1326 term->alt_sblines = 0;
887035a5 1327 update_sbar(term);
374330e2 1328}
1329
1330/*
1331 * Initialise the terminal.
1332 */
21d2b241 1333Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
1334 void *frontend)
32874aea 1335{
887035a5 1336 Terminal *term;
1337
1338 /*
1339 * Allocate a new Terminal structure and initialise the fields
1340 * that need it.
1341 */
3d88e64d 1342 term = snew(Terminal);
a8327734 1343 term->frontend = frontend;
21d2b241 1344 term->ucsdata = ucsdata;
64734920 1345 term->cfg = *mycfg; /* STRUCTURE COPY */
a8327734 1346 term->logctx = NULL;
887035a5 1347 term->compatibility_level = TM_PUTTY;
1348 strcpy(term->id_string, "\033[?6c");
39934deb 1349 term->cblink_pending = term->tblink_pending = FALSE;
887035a5 1350 term->paste_buffer = NULL;
c80a3e8c 1351 term->paste_len = 0;
887035a5 1352 term->last_paste = 0;
1353 bufchain_init(&term->inbuf);
1354 bufchain_init(&term->printer_buf);
f278d6f8 1355 term->printing = term->only_printing = FALSE;
1356 term->print_job = NULL;
1357 term->vt52_mode = FALSE;
1358 term->cr_lf_return = FALSE;
1359 term->seen_disp_event = FALSE;
b9d7bcad 1360 term->xterm_mouse = term->mouse_is_down = FALSE;
f278d6f8 1361 term->reset_132 = FALSE;
39934deb 1362 term->cblinker = term->tblinker = 0;
f278d6f8 1363 term->has_focus = 1;
1364 term->repeat_off = FALSE;
1365 term->termstate = TOPLEVEL;
1366 term->selstate = NO_SELECTION;
093a75b8 1367 term->curstype = 0;
887035a5 1368
1369 term->screen = term->alt_screen = term->scrollback = NULL;
4683b9b6 1370 term->tempsblines = 0;
876e5d5e 1371 term->alt_sblines = 0;
887035a5 1372 term->disptop = 0;
36566009 1373 term->disptext = NULL;
1374 term->dispcursx = term->dispcursy = -1;
887035a5 1375 term->tabs = NULL;
1376 deselect(term);
1377 term->rows = term->cols = -1;
1378 power_on(term);
1379 term->beephead = term->beeptail = NULL;
341eb978 1380#ifdef OPTIMISE_SCROLL
1381 term->scrollhead = term->scrolltail = NULL;
1382#endif /* OPTIMISE_SCROLL */
887035a5 1383 term->nbeeps = 0;
1384 term->lastbeep = FALSE;
1385 term->beep_overloaded = FALSE;
2647b103 1386 term->attr_mask = 0xffffffff;
51470298 1387 term->resize_fn = NULL;
1388 term->resize_ctx = NULL;
74aca06d 1389 term->in_term_out = FALSE;
f0fccd51 1390 term->ltemp = NULL;
c6958dfe 1391 term->ltemp_size = 0;
f0fccd51 1392 term->wcFrom = NULL;
1393 term->wcTo = NULL;
c6958dfe 1394 term->wcFromTo_size = 0;
f0fccd51 1395
39934deb 1396 term->window_update_pending = FALSE;
1397
f0fccd51 1398 term->bidi_cache_size = 0;
1399 term->pre_bidi_cache = term->post_bidi_cache = NULL;
887035a5 1400
c6958dfe 1401 /* FULL-TERMCHAR */
36566009 1402 term->basic_erase_char.chr = CSET_ASCII | ' ';
1403 term->basic_erase_char.attr = ATTR_DEFAULT;
c6958dfe 1404 term->basic_erase_char.cc_next = 0;
1405 term->erase_char = term->basic_erase_char;
36566009 1406
887035a5 1407 return term;
374330e2 1408}
1409
fabd1805 1410void term_free(Terminal *term)
1411{
36566009 1412 termline *line;
fabd1805 1413 struct beeptime *beep;
f0fccd51 1414 int i;
fabd1805 1415
1416 while ((line = delpos234(term->scrollback, 0)) != NULL)
36566009 1417 sfree(line); /* compressed data, not a termline */
fabd1805 1418 freetree234(term->scrollback);
1419 while ((line = delpos234(term->screen, 0)) != NULL)
36566009 1420 freeline(line);
fabd1805 1421 freetree234(term->screen);
1422 while ((line = delpos234(term->alt_screen, 0)) != NULL)
36566009 1423 freeline(line);
fabd1805 1424 freetree234(term->alt_screen);
36566009 1425 if (term->disptext) {
1426 for (i = 0; i < term->rows; i++)
1427 freeline(term->disptext[i]);
1428 }
fabd1805 1429 sfree(term->disptext);
1430 while (term->beephead) {
1431 beep = term->beephead;
1432 term->beephead = beep->next;
1433 sfree(beep);
1434 }
1435 bufchain_clear(&term->inbuf);
1436 if(term->print_job)
1437 printer_finish_job(term->print_job);
1438 bufchain_clear(&term->printer_buf);
1439 sfree(term->paste_buffer);
f0fccd51 1440 sfree(term->ltemp);
1441 sfree(term->wcFrom);
1442 sfree(term->wcTo);
1443
1444 for (i = 0; i < term->bidi_cache_size; i++) {
2caf3cd8 1445 sfree(term->pre_bidi_cache[i].chars);
1446 sfree(term->post_bidi_cache[i].chars);
f0fccd51 1447 }
1448 sfree(term->pre_bidi_cache);
1449 sfree(term->post_bidi_cache);
1450
39934deb 1451 expire_timer_context(term);
1452
fabd1805 1453 sfree(term);
1454}
1455
374330e2 1456/*
1457 * Set up the terminal for a given size.
1458 */
887035a5 1459void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
32874aea 1460{
2d466ffd 1461 tree234 *newalt;
36566009 1462 termline **newdisp, *line;
05bea829 1463 int i, j, oldrows = term->rows;
c83de303 1464 int sblen;
887035a5 1465 int save_alt_which = term->alt_which;
d6832394 1466
887035a5 1467 if (newrows == term->rows && newcols == term->cols &&
1468 newsavelines == term->savelines)
374330e2 1469 return; /* nothing to do */
1470
887035a5 1471 deselect(term);
1472 swap_screen(term, 0, FALSE, FALSE);
d6832394 1473
887035a5 1474 term->alt_t = term->marg_t = 0;
1475 term->alt_b = term->marg_b = newrows - 1;
374330e2 1476
887035a5 1477 if (term->rows == -1) {
1478 term->scrollback = newtree234(NULL);
1479 term->screen = newtree234(NULL);
4683b9b6 1480 term->tempsblines = 0;
887035a5 1481 term->rows = 0;
c83de303 1482 }
1483
1484 /*
1485 * Resize the screen and scrollback. We only need to shift
1486 * lines around within our data structures, because lineptr()
1487 * will take care of resizing each individual line if
1488 * necessary. So:
1489 *
4683b9b6 1490 * - If the new screen is longer, we shunt lines in from temporary
1491 * scrollback if possible, otherwise we add new blank lines at
1492 * the bottom.
1493 *
1494 * - If the new screen is shorter, we remove any blank lines at
1495 * the bottom if possible, otherwise shunt lines above the cursor
1496 * to scrollback if possible, otherwise delete lines below the
1497 * cursor.
c83de303 1498 *
1499 * - Then, if the new scrollback length is less than the
1500 * amount of scrollback we actually have, we must throw some
1501 * away.
1502 */
887035a5 1503 sblen = count234(term->scrollback);
cdd6c586 1504 /* Do this loop to expand the screen if newrows > rows */
4683b9b6 1505 assert(term->rows == count234(term->screen));
1506 while (term->rows < newrows) {
1507 if (term->tempsblines > 0) {
36566009 1508 unsigned char *cline;
4683b9b6 1509 /* Insert a line from the scrollback at the top of the screen. */
1510 assert(sblen >= term->tempsblines);
36566009 1511 cline = delpos234(term->scrollback, --sblen);
1512 line = decompressline(cline, NULL);
1513 sfree(cline);
1514 line->temporary = FALSE; /* reconstituted line is now real */
4683b9b6 1515 term->tempsblines -= 1;
1516 addpos234(term->screen, line, 0);
1517 term->curs.y += 1;
1518 term->savecurs.y += 1;
32874aea 1519 } else {
4683b9b6 1520 /* Add a new blank line at the bottom of the screen. */
36566009 1521 line = newline(term, newcols, FALSE);
4683b9b6 1522 addpos234(term->screen, line, count234(term->screen));
32874aea 1523 }
4683b9b6 1524 term->rows += 1;
cdd6c586 1525 }
1526 /* Do this loop to shrink the screen if newrows < rows */
4683b9b6 1527 while (term->rows > newrows) {
1528 if (term->curs.y < term->rows - 1) {
1529 /* delete bottom row, unless it contains the cursor */
1530 sfree(delpos234(term->screen, term->rows - 1));
1531 } else {
1532 /* push top row to scrollback */
1533 line = delpos234(term->screen, 0);
36566009 1534 addpos234(term->scrollback, compressline(line), sblen++);
1535 freeline(line);
4683b9b6 1536 term->tempsblines += 1;
1537 term->curs.y -= 1;
1538 term->savecurs.y -= 1;
1539 }
1540 term->rows -= 1;
c83de303 1541 }
4683b9b6 1542 assert(term->rows == newrows);
887035a5 1543 assert(count234(term->screen) == newrows);
4683b9b6 1544
1545 /* Delete any excess lines from the scrollback. */
c83de303 1546 while (sblen > newsavelines) {
887035a5 1547 line = delpos234(term->scrollback, 0);
c83de303 1548 sfree(line);
1549 sblen--;
c9def1b8 1550 }
4683b9b6 1551 if (sblen < term->tempsblines)
1552 term->tempsblines = sblen;
887035a5 1553 assert(count234(term->scrollback) <= newsavelines);
4683b9b6 1554 assert(count234(term->scrollback) >= term->tempsblines);
887035a5 1555 term->disptop = 0;
374330e2 1556
4683b9b6 1557 /* Make a new displayed text buffer. */
36566009 1558 newdisp = snewn(newrows, termline *);
1559 for (i = 0; i < newrows; i++) {
1560 newdisp[i] = newline(term, newcols, FALSE);
1561 for (j = 0; j < newcols; j++)
d701235f 1562 newdisp[i]->chars[j].attr = ATTR_INVALID;
36566009 1563 }
1564 if (term->disptext) {
05bea829 1565 for (i = 0; i < oldrows; i++)
36566009 1566 freeline(term->disptext[i]);
1567 }
887035a5 1568 sfree(term->disptext);
1569 term->disptext = newdisp;
36566009 1570 term->dispcursx = term->dispcursy = -1;
374330e2 1571
4683b9b6 1572 /* Make a new alternate screen. */
4facdf84 1573 newalt = newtree234(NULL);
32874aea 1574 for (i = 0; i < newrows; i++) {
36566009 1575 line = newline(term, newcols, TRUE);
4facdf84 1576 addpos234(newalt, line, i);
1577 }
887035a5 1578 if (term->alt_screen) {
1579 while (NULL != (line = delpos234(term->alt_screen, 0)))
36566009 1580 freeline(line);
887035a5 1581 freetree234(term->alt_screen);
4facdf84 1582 }
887035a5 1583 term->alt_screen = newalt;
876e5d5e 1584 term->alt_sblines = 0;
374330e2 1585
3d88e64d 1586 term->tabs = sresize(term->tabs, newcols, unsigned char);
374330e2 1587 {
1588 int i;
887035a5 1589 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
1590 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
374330e2 1591 }
1592
4683b9b6 1593 /* Check that the cursor positions are still valid. */
1594 if (term->savecurs.y < 0)
1595 term->savecurs.y = 0;
1596 if (term->savecurs.y >= newrows)
1597 term->savecurs.y = newrows - 1;
887035a5 1598 if (term->curs.y < 0)
1599 term->curs.y = 0;
1600 if (term->curs.y >= newrows)
1601 term->curs.y = newrows - 1;
1602 if (term->curs.x >= newcols)
1603 term->curs.x = newcols - 1;
1604 term->alt_x = term->alt_y = 0;
1605 term->wrapnext = term->alt_wnext = FALSE;
1606
1607 term->rows = newrows;
1608 term->cols = newcols;
1609 term->savelines = newsavelines;
374330e2 1610
887035a5 1611 swap_screen(term, save_alt_which, FALSE, FALSE);
d6832394 1612
887035a5 1613 update_sbar(term);
1614 term_update(term);
51470298 1615 if (term->resize_fn)
1616 term->resize_fn(term->resize_ctx, term->cols, term->rows);
1617}
1618
1619/*
1620 * Hand a function and context pointer to the terminal which it can
1621 * use to notify a back end of resizes.
1622 */
1623void term_provide_resize_fn(Terminal *term,
1624 void (*resize_fn)(void *, int, int),
1625 void *resize_ctx)
1626{
1627 term->resize_fn = resize_fn;
1628 term->resize_ctx = resize_ctx;
1629 if (term->cols > 0 && term->rows > 0)
1630 resize_fn(resize_ctx, term->cols, term->rows);
374330e2 1631}
1632
876e5d5e 1633/* Find the bottom line on the screen that has any content.
1634 * If only the top line has content, returns 0.
1635 * If no lines have content, return -1.
1636 */
1637static int find_last_nonempty_line(Terminal * term, tree234 * screen)
1638{
1639 int i;
1640 for (i = count234(screen) - 1; i >= 0; i--) {
36566009 1641 termline *line = index234(screen, i);
876e5d5e 1642 int j;
c6958dfe 1643 for (j = 0; j < line->cols; j++)
1644 if (!termchars_equal(&line->chars[j], &term->erase_char))
1645 break;
36566009 1646 if (j != line->cols) break;
876e5d5e 1647 }
1648 return i;
1649}
1650
374330e2 1651/*
8f9b6a1a 1652 * Swap screens. If `reset' is TRUE and we have been asked to
1653 * switch to the alternate screen, we must bring most of its
1654 * configuration from the main screen and erase the contents of the
1655 * alternate screen completely. (This is even true if we're already
1656 * on it! Blame xterm.)
374330e2 1657 */
887035a5 1658static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
32874aea 1659{
374330e2 1660 int t;
4facdf84 1661 tree234 *ttr;
374330e2 1662
8f9b6a1a 1663 if (!which)
1664 reset = FALSE; /* do no weird resetting if which==0 */
1665
887035a5 1666 if (which != term->alt_which) {
1667 term->alt_which = which;
8f9b6a1a 1668
887035a5 1669 ttr = term->alt_screen;
1670 term->alt_screen = term->screen;
1671 term->screen = ttr;
876e5d5e 1672 term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1;
887035a5 1673 t = term->curs.x;
8f9b6a1a 1674 if (!reset && !keep_cur_pos)
887035a5 1675 term->curs.x = term->alt_x;
1676 term->alt_x = t;
1677 t = term->curs.y;
8f9b6a1a 1678 if (!reset && !keep_cur_pos)
887035a5 1679 term->curs.y = term->alt_y;
1680 term->alt_y = t;
1681 t = term->marg_t;
1682 if (!reset) term->marg_t = term->alt_t;
1683 term->alt_t = t;
1684 t = term->marg_b;
1685 if (!reset) term->marg_b = term->alt_b;
1686 term->alt_b = t;
1687 t = term->dec_om;
1688 if (!reset) term->dec_om = term->alt_om;
1689 term->alt_om = t;
1690 t = term->wrap;
1691 if (!reset) term->wrap = term->alt_wrap;
1692 term->alt_wrap = t;
1693 t = term->wrapnext;
1694 if (!reset) term->wrapnext = term->alt_wnext;
1695 term->alt_wnext = t;
1696 t = term->insert;
1697 if (!reset) term->insert = term->alt_ins;
1698 term->alt_ins = t;
1699 t = term->cset;
1700 if (!reset) term->cset = term->alt_cset;
1701 term->alt_cset = t;
1702 t = term->utf;
1703 if (!reset) term->utf = term->alt_utf;
1704 term->alt_utf = t;
1705 t = term->sco_acs;
1706 if (!reset) term->sco_acs = term->alt_sco_acs;
1707 term->alt_sco_acs = t;
8f9b6a1a 1708 }
374330e2 1709
887035a5 1710 if (reset && term->screen) {
8f9b6a1a 1711 /*
1712 * Yes, this _is_ supposed to honour background-colour-erase.
1713 */
887035a5 1714 erase_lots(term, FALSE, TRUE, TRUE);
8f9b6a1a 1715 }
374330e2 1716}
1717
1718/*
374330e2 1719 * Update the scroll bar.
1720 */
887035a5 1721static void update_sbar(Terminal *term)
32874aea 1722{
876e5d5e 1723 int nscroll = sblines(term);
a8327734 1724 set_sbar(term->frontend, nscroll + term->rows,
1725 nscroll + term->disptop, term->rows);
374330e2 1726}
1727
1728/*
1729 * Check whether the region bounded by the two pointers intersects
1730 * the scroll region, and de-select the on-screen selection if so.
1731 */
887035a5 1732static void check_selection(Terminal *term, pos from, pos to)
32874aea 1733{
887035a5 1734 if (poslt(from, term->selend) && poslt(term->selstart, to))
1735 deselect(term);
374330e2 1736}
1737
1738/*
1739 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1740 * for backward.) `sb' is TRUE if the scrolling is permitted to
1741 * affect the scrollback buffer.
1742 */
887035a5 1743static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
32874aea 1744{
36566009 1745 termline *line;
7c66d7d9 1746 int i, seltop, olddisptop, shift;
c9def1b8 1747
887035a5 1748 if (topline != 0 || term->alt_which != 0)
4facdf84 1749 sb = FALSE;
1750
7c66d7d9 1751 olddisptop = term->disptop;
1752 shift = lines;
4facdf84 1753 if (lines < 0) {
1754 while (lines < 0) {
887035a5 1755 line = delpos234(term->screen, botline);
36566009 1756 resizeline(term, line, term->cols);
887035a5 1757 for (i = 0; i < term->cols; i++)
c6958dfe 1758 copy_termchar(line, i, &term->erase_char);
36566009 1759 line->lattr = LATTR_NORM;
887035a5 1760 addpos234(term->screen, line, topline);
1761
1762 if (term->selstart.y >= topline && term->selstart.y <= botline) {
1763 term->selstart.y++;
1764 if (term->selstart.y > botline) {
5a8afc78 1765 term->selstart.y = botline + 1;
887035a5 1766 term->selstart.x = 0;
c9def1b8 1767 }
c9def1b8 1768 }
887035a5 1769 if (term->selend.y >= topline && term->selend.y <= botline) {
1770 term->selend.y++;
1771 if (term->selend.y > botline) {
5a8afc78 1772 term->selend.y = botline + 1;
887035a5 1773 term->selend.x = 0;
c9def1b8 1774 }
c9def1b8 1775 }
c9def1b8 1776
4facdf84 1777 lines++;
c9def1b8 1778 }
374330e2 1779 } else {
4facdf84 1780 while (lines > 0) {
887035a5 1781 line = delpos234(term->screen, topline);
4fe051a2 1782 cc_check(line); /* XXX-REMOVE-BEFORE-RELEASE */
887035a5 1783 if (sb && term->savelines > 0) {
4683b9b6 1784 int sblen = count234(term->scrollback);
4facdf84 1785 /*
1786 * We must add this line to the scrollback. We'll
36566009 1787 * remove a line from the top of the scrollback if
1788 * the scrollback is full.
4facdf84 1789 */
887035a5 1790 if (sblen == term->savelines) {
36566009 1791 unsigned char *cline;
1792
1793 sblen--;
1794 cline = delpos234(term->scrollback, 0);
1795 sfree(cline);
1796 } else
4683b9b6 1797 term->tempsblines += 1;
36566009 1798
1799 addpos234(term->scrollback, compressline(line), sblen);
1800
dac0aee3 1801 /* now `line' itself can be reused as the bottom line */
a792432e 1802
1803 /*
1804 * If the user is currently looking at part of the
1805 * scrollback, and they haven't enabled any options
1806 * that are going to reset the scrollback as a
1807 * result of this movement, then the chances are
1808 * they'd like to keep looking at the same line. So
1809 * we move their viewpoint at the same rate as the
1810 * scroll, at least until their viewpoint hits the
1811 * top end of the scrollback buffer, at which point
1812 * we don't have the choice any more.
1813 *
1814 * Thanks to Jan Holmen Holsten for the idea and
1815 * initial implementation.
1816 */
887035a5 1817 if (term->disptop > -term->savelines && term->disptop < 0)
1818 term->disptop--;
4facdf84 1819 }
36566009 1820 resizeline(term, line, term->cols);
887035a5 1821 for (i = 0; i < term->cols; i++)
c6958dfe 1822 copy_termchar(line, i, &term->erase_char);
36566009 1823 line->lattr = LATTR_NORM;
887035a5 1824 addpos234(term->screen, line, botline);
4facdf84 1825
a792432e 1826 /*
1827 * If the selection endpoints move into the scrollback,
1828 * we keep them moving until they hit the top. However,
1829 * of course, if the line _hasn't_ moved into the
1830 * scrollback then we don't do this, and cut them off
1831 * at the top of the scroll region.
d48db8b4 1832 *
1833 * This applies to selstart and selend (for an existing
1834 * selection), and also selanchor (for one being
1835 * selected as we speak).
a792432e 1836 */
887035a5 1837 seltop = sb ? -term->savelines : topline;
1838
c989dbc4 1839 if (term->selstate != NO_SELECTION) {
1840 if (term->selstart.y >= seltop &&
1841 term->selstart.y <= botline) {
1842 term->selstart.y--;
1843 if (term->selstart.y < seltop) {
1844 term->selstart.y = seltop;
1845 term->selstart.x = 0;
1846 }
4facdf84 1847 }
c989dbc4 1848 if (term->selend.y >= seltop && term->selend.y <= botline) {
1849 term->selend.y--;
1850 if (term->selend.y < seltop) {
1851 term->selend.y = seltop;
1852 term->selend.x = 0;
1853 }
4facdf84 1854 }
c989dbc4 1855 if (term->selanchor.y >= seltop &&
1856 term->selanchor.y <= botline) {
1857 term->selanchor.y--;
1858 if (term->selanchor.y < seltop) {
1859 term->selanchor.y = seltop;
1860 term->selanchor.x = 0;
1861 }
d48db8b4 1862 }
1863 }
4facdf84 1864
1865 lines--;
c9def1b8 1866 }
374330e2 1867 }
7c66d7d9 1868#ifdef OPTIMISE_SCROLL
1869 shift += term->disptop - olddisptop;
1870 if (shift < term->rows && shift > -term->rows && shift != 0)
1871 scroll_display(term, topline, botline, shift);
1872#endif /* OPTIMISE_SCROLL */
374330e2 1873}
1874
37d2a505 1875#ifdef OPTIMISE_SCROLL
1876/*
341eb978 1877 * Add a scroll of a region on the screen into the pending scroll list.
1878 * `lines' is +ve for scrolling forward, -ve for backward.
1879 *
1880 * If the scroll is on the same area as the last scroll in the list,
1881 * merge them.
1882 */
03f60a62 1883static void save_scroll(Terminal *term, int topline, int botline, int lines)
341eb978 1884{
1885 struct scrollregion *newscroll;
1886 if (term->scrolltail &&
1887 term->scrolltail->topline == topline &&
1888 term->scrolltail->botline == botline) {
1889 term->scrolltail->lines += lines;
1890 } else {
3d88e64d 1891 newscroll = snew(struct scrollregion);
341eb978 1892 newscroll->topline = topline;
1893 newscroll->botline = botline;
1894 newscroll->lines = lines;
1895 newscroll->next = NULL;
1896
1897 if (!term->scrollhead)
1898 term->scrollhead = newscroll;
1899 else
1900 term->scrolltail->next = newscroll;
1901 term->scrolltail = newscroll;
1902 }
1903}
1904
1905/*
37d2a505 1906 * Scroll the physical display, and our conception of it in disptext.
1907 */
1908static void scroll_display(Terminal *term, int topline, int botline, int lines)
1909{
c6958dfe 1910 int distance, nlines, i, j;
37d2a505 1911
36566009 1912 distance = lines > 0 ? lines : -lines;
1913 nlines = botline - topline + 1 - distance;
37d2a505 1914 if (lines > 0) {
36566009 1915 for (i = 0; i < nlines; i++)
c6958dfe 1916 for (j = 0; j < term->cols; j++)
1917 copy_termchar(term->disptext[start+i], j,
1918 term->disptext[start+i+distance]->chars+j);
36566009 1919 if (term->dispcursy >= 0 &&
1920 term->dispcursy >= topline + distance &&
1921 term->dispcursy < topline + distance + nlines)
1922 term->dispcursy -= distance;
37d2a505 1923 for (i = 0; i < distance; i++)
36566009 1924 for (j = 0; j < term->cols; j++)
1925 term->disptext[start+nlines+i]->chars[j].attr |= ATTR_INVALID;
37d2a505 1926 } else {
36566009 1927 for (i = nlines; i-- ;)
c6958dfe 1928 for (j = 0; j < term->cols; j++)
1929 copy_termchar(term->disptext[start+i+distance], j,
1930 term->disptext[start+i]->chars+j);
36566009 1931 if (term->dispcursy >= 0 &&
1932 term->dispcursy >= topline &&
1933 term->dispcursy < topline + nlines)
1934 term->dispcursy += distance;
37d2a505 1935 for (i = 0; i < distance; i++)
36566009 1936 for (j = 0; j < term->cols; j++)
1937 term->disptext[start+i]->chars[j].attr |= ATTR_INVALID;
37d2a505 1938 }
341eb978 1939 save_scroll(term, topline, botline, lines);
37d2a505 1940}
1941#endif /* OPTIMISE_SCROLL */
1942
374330e2 1943/*
1944 * Move the cursor to a given position, clipping at boundaries. We
1945 * may or may not want to clip at the scroll margin: marg_clip is 0
1946 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
1947 * even _being_ outside the margins.
1948 */
887035a5 1949static void move(Terminal *term, int x, int y, int marg_clip)
32874aea 1950{
374330e2 1951 if (x < 0)
1952 x = 0;
887035a5 1953 if (x >= term->cols)
1954 x = term->cols - 1;
374330e2 1955 if (marg_clip) {
887035a5 1956 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
1957 y < term->marg_t)
1958 y = term->marg_t;
1959 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
1960 y > term->marg_b)
1961 y = term->marg_b;
374330e2 1962 }
1963 if (y < 0)
1964 y = 0;
887035a5 1965 if (y >= term->rows)
1966 y = term->rows - 1;
1967 term->curs.x = x;
1968 term->curs.y = y;
887035a5 1969 term->wrapnext = FALSE;
374330e2 1970}
1971
1972/*
1973 * Save or restore the cursor and SGR mode.
1974 */
887035a5 1975static void save_cursor(Terminal *term, int save)
32874aea 1976{
374330e2 1977 if (save) {
887035a5 1978 term->savecurs = term->curs;
1979 term->save_attr = term->curr_attr;
1980 term->save_cset = term->cset;
1981 term->save_utf = term->utf;
1982 term->save_wnext = term->wrapnext;
1983 term->save_csattr = term->cset_attr[term->cset];
1984 term->save_sco_acs = term->sco_acs;
374330e2 1985 } else {
887035a5 1986 term->curs = term->savecurs;
c9def1b8 1987 /* Make sure the window hasn't shrunk since the save */
887035a5 1988 if (term->curs.x >= term->cols)
1989 term->curs.x = term->cols - 1;
1990 if (term->curs.y >= term->rows)
1991 term->curs.y = term->rows - 1;
1992
1993 term->curr_attr = term->save_attr;
1994 term->cset = term->save_cset;
1995 term->utf = term->save_utf;
1996 term->wrapnext = term->save_wnext;
8c028e65 1997 /*
1998 * wrapnext might reset to False if the x position is no
1999 * longer at the rightmost edge.
2000 */
887035a5 2001 if (term->wrapnext && term->curs.x < term->cols-1)
2002 term->wrapnext = FALSE;
2003 term->cset_attr[term->cset] = term->save_csattr;
2004 term->sco_acs = term->save_sco_acs;
36566009 2005 set_erase_char(term);
374330e2 2006 }
2007}
2008
2009/*
3c7366f8 2010 * This function is called before doing _anything_ which affects
2011 * only part of a line of text. It is used to mark the boundary
2012 * between two character positions, and it indicates that some sort
2013 * of effect is going to happen on only one side of that boundary.
2014 *
2015 * The effect of this function is to check whether a CJK
2016 * double-width character is straddling the boundary, and to remove
2017 * it and replace it with two spaces if so. (Of course, one or
2018 * other of those spaces is then likely to be replaced with
2019 * something else again, as a result of whatever happens next.)
2020 *
2021 * Also, if the boundary is at the right-hand _edge_ of the screen,
2022 * it implies something deliberate is being done to the rightmost
2023 * column position; hence we must clear LATTR_WRAPPED2.
2024 *
2025 * The input to the function is the coordinates of the _second_
2026 * character of the pair.
2027 */
2028static void check_boundary(Terminal *term, int x, int y)
2029{
36566009 2030 termline *ldata;
3c7366f8 2031
2032 /* Validate input coordinates, just in case. */
2033 if (x == 0 || x > term->cols)
2034 return;
2035
36566009 2036 ldata = scrlineptr(y);
3c7366f8 2037 if (x == term->cols) {
36566009 2038 ldata->lattr &= ~LATTR_WRAPPED2;
3c7366f8 2039 } else {
36566009 2040 if (ldata->chars[x].chr == UCSWIDE) {
c6958dfe 2041 clear_cc(ldata, x-1);
2042 clear_cc(ldata, x);
36566009 2043 ldata->chars[x-1].chr = ' ' | CSET_ASCII;
2044 ldata->chars[x] = ldata->chars[x-1];
3c7366f8 2045 }
2046 }
2047}
2048
2049/*
374330e2 2050 * Erase a large portion of the screen: the whole screen, or the
2051 * whole line, or parts thereof.
2052 */
887035a5 2053static void erase_lots(Terminal *term,
2054 int line_only, int from_begin, int to_end)
32874aea 2055{
260f3dec 2056 pos start, end;
4facdf84 2057 int erase_lattr;
4683b9b6 2058 int erasing_lines_from_top = 0;
374330e2 2059
2060 if (line_only) {
887035a5 2061 start.y = term->curs.y;
4facdf84 2062 start.x = 0;
887035a5 2063 end.y = term->curs.y + 1;
4facdf84 2064 end.x = 0;
2065 erase_lattr = FALSE;
374330e2 2066 } else {
4facdf84 2067 start.y = 0;
2068 start.x = 0;
887035a5 2069 end.y = term->rows;
4facdf84 2070 end.x = 0;
2071 erase_lattr = TRUE;
2072 }
2073 if (!from_begin) {
887035a5 2074 start = term->curs;
374330e2 2075 }
4facdf84 2076 if (!to_end) {
887035a5 2077 end = term->curs;
2a6dfe0e 2078 incpos(end);
4facdf84 2079 }
3c7366f8 2080 if (!from_begin || !to_end)
2081 check_boundary(term, term->curs.x, term->curs.y);
887035a5 2082 check_selection(term, start, end);
e14a5a13 2083
2084 /* Clear screen also forces a full window redraw, just in case. */
887035a5 2085 if (start.y == 0 && start.x == 0 && end.y == term->rows)
2086 term_invalidate(term);
e14a5a13 2087
4683b9b6 2088 /* Lines scrolled away shouldn't be brought back on if the terminal
2089 * resizes. */
2090 if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr)
2091 erasing_lines_from_top = 1;
2092
2093 if (term->cfg.erase_to_scrollback && erasing_lines_from_top) {
876e5d5e 2094 /* If it's a whole number of lines, starting at the top, and
2095 * we're fully erasing them, erase by scrolling and keep the
2096 * lines in the scrollback. */
2097 int scrolllines = end.y;
2098 if (end.y == term->rows) {
2099 /* Shrink until we find a non-empty row.*/
2100 scrolllines = find_last_nonempty_line(term, term->screen) + 1;
2101 }
2102 if (scrolllines > 0)
2103 scroll(term, 0, scrolllines - 1, scrolllines, TRUE);
876e5d5e 2104 } else {
36566009 2105 termline *ldata = scrlineptr(start.y);
876e5d5e 2106 while (poslt(start, end)) {
2107 if (start.x == term->cols) {
2108 if (!erase_lattr)
36566009 2109 ldata->lattr &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
876e5d5e 2110 else
36566009 2111 ldata->lattr = LATTR_NORM;
876e5d5e 2112 } else {
c6958dfe 2113 copy_termchar(ldata, start.x, &term->erase_char);
36566009 2114 }
2115 if (incpos(start) && start.y < term->rows) {
2116 ldata = scrlineptr(start.y);
876e5d5e 2117 }
876e5d5e 2118 }
4facdf84 2119 }
4683b9b6 2120
2121 /* After an erase of lines from the top of the screen, we shouldn't
2122 * bring the lines back again if the terminal enlarges (since the user or
2123 * application has explictly thrown them away). */
2124 if (erasing_lines_from_top && !(term->alt_which))
2125 term->tempsblines = 0;
374330e2 2126}
2127
2128/*
2129 * Insert or delete characters within the current line. n is +ve if
2130 * insertion is desired, and -ve for deletion.
2131 */
887035a5 2132static void insch(Terminal *term, int n)
32874aea 2133{
374330e2 2134 int dir = (n < 0 ? -1 : +1);
c6958dfe 2135 int m, j;
4facdf84 2136 pos cursplus;
36566009 2137 termline *ldata;
374330e2 2138
2139 n = (n < 0 ? -n : n);
887035a5 2140 if (n > term->cols - term->curs.x)
2141 n = term->cols - term->curs.x;
2142 m = term->cols - term->curs.x - n;
2143 cursplus.y = term->curs.y;
2144 cursplus.x = term->curs.x + n;
2145 check_selection(term, term->curs, cursplus);
3c7366f8 2146 check_boundary(term, term->curs.x, term->curs.y);
2147 if (dir < 0)
2148 check_boundary(term, term->curs.x + n, term->curs.y);
36566009 2149 ldata = scrlineptr(term->curs.y);
374330e2 2150 if (dir < 0) {
c6958dfe 2151 for (j = 0; j < m; j++)
2152 move_termchar(ldata,
2153 ldata->chars + term->curs.x + j,
2154 ldata->chars + term->curs.x + j + n);
374330e2 2155 while (n--)
c6958dfe 2156 copy_termchar(ldata, term->curs.x + m++, &term->erase_char);
374330e2 2157 } else {
c6958dfe 2158 for (j = m; j-- ;)
2159 move_termchar(ldata,
2160 ldata->chars + term->curs.x + j + n,
2161 ldata->chars + term->curs.x + j);
374330e2 2162 while (n--)
c6958dfe 2163 copy_termchar(ldata, term->curs.x + n, &term->erase_char);
374330e2 2164 }
2165}
2166
2167/*
2168 * Toggle terminal mode `mode' to state `state'. (`query' indicates
2169 * whether the mode is a DEC private one or a normal one.)
2170 */
887035a5 2171static void toggle_mode(Terminal *term, int mode, int query, int state)
32874aea 2172{
32874aea 2173 if (query)
2174 switch (mode) {
a263e4f6 2175 case 1: /* DECCKM: application cursor keys */
887035a5 2176 term->app_cursor_keys = state;
32874aea 2177 break;
a263e4f6 2178 case 2: /* DECANM: VT52 mode */
887035a5 2179 term->vt52_mode = !state;
2180 if (term->vt52_mode) {
2181 term->blink_is_real = FALSE;
2182 term->vt52_bold = FALSE;
4eeb7d09 2183 } else {
64734920 2184 term->blink_is_real = term->cfg.blinktext;
4eeb7d09 2185 }
39934deb 2186 term_schedule_tblink(term);
32874aea 2187 break;
a263e4f6 2188 case 3: /* DECCOLM: 80/132 columns */
887035a5 2189 deselect(term);
64734920 2190 if (!term->cfg.no_remote_resize)
a8327734 2191 request_resize(term->frontend, state ? 132 : 80, term->rows);
887035a5 2192 term->reset_132 = state;
7ce3b333 2193 term->alt_t = term->marg_t = 0;
2194 term->alt_b = term->marg_b = term->rows - 1;
2195 move(term, 0, 0, 0);
2196 erase_lots(term, FALSE, TRUE, TRUE);
32874aea 2197 break;
a263e4f6 2198 case 5: /* DECSCNM: reverse video */
32874aea 2199 /*
2200 * Toggle reverse video. If we receive an OFF within the
2201 * visual bell timeout period after an ON, we trigger an
2202 * effective visual bell, so that ESC[?5hESC[?5l will
2203 * always be an actually _visible_ visual bell.
2204 */
39934deb 2205 if (term->rvideo && !state) {
2206 /* This is an OFF, so set up a vbell */
2207 term_schedule_vbell(term, TRUE, term->rvbell_startpoint);
887035a5 2208 } else if (!term->rvideo && state) {
32874aea 2209 /* This is an ON, so we notice the time and save it. */
39934deb 2210 term->rvbell_startpoint = GETTICKCOUNT();
32874aea 2211 }
887035a5 2212 term->rvideo = state;
39934deb 2213 seen_disp_event(term);
32874aea 2214 break;
a263e4f6 2215 case 6: /* DECOM: DEC origin mode */
887035a5 2216 term->dec_om = state;
32874aea 2217 break;
a263e4f6 2218 case 7: /* DECAWM: auto wrap */
887035a5 2219 term->wrap = state;
32874aea 2220 break;
a263e4f6 2221 case 8: /* DECARM: auto key repeat */
887035a5 2222 term->repeat_off = !state;
32874aea 2223 break;
a263e4f6 2224 case 10: /* DECEDM: set local edit mode */
887035a5 2225 term->term_editing = state;
b9d7bcad 2226 if (term->ldisc) /* cause ldisc to notice changes */
2227 ldisc_send(term->ldisc, NULL, 0, 0);
32874aea 2228 break;
a263e4f6 2229 case 25: /* DECTCEM: enable/disable cursor */
32874aea 2230 compatibility2(OTHER, VT220);
887035a5 2231 term->cursor_on = state;
39934deb 2232 seen_disp_event(term);
32874aea 2233 break;
2234 case 47: /* alternate screen */
2235 compatibility(OTHER);
887035a5 2236 deselect(term);
64734920 2237 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
887035a5 2238 term->disptop = 0;
32874aea 2239 break;
2240 case 1000: /* xterm mouse 1 */
887035a5 2241 term->xterm_mouse = state ? 1 : 0;
a8327734 2242 set_raw_mouse_mode(term->frontend, state);
32874aea 2243 break;
2244 case 1002: /* xterm mouse 2 */
887035a5 2245 term->xterm_mouse = state ? 2 : 0;
a8327734 2246 set_raw_mouse_mode(term->frontend, state);
32874aea 2247 break;
8f9b6a1a 2248 case 1047: /* alternate screen */
2249 compatibility(OTHER);
887035a5 2250 deselect(term);
64734920 2251 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
887035a5 2252 term->disptop = 0;
8f9b6a1a 2253 break;
2254 case 1048: /* save/restore cursor */
4c80361f 2255 if (!term->cfg.no_alt_screen)
2256 save_cursor(term, state);
39934deb 2257 if (!state) seen_disp_event(term);
8f9b6a1a 2258 break;
2259 case 1049: /* cursor & alternate screen */
4c80361f 2260 if (state && !term->cfg.no_alt_screen)
887035a5 2261 save_cursor(term, state);
39934deb 2262 if (!state) seen_disp_event(term);
8f9b6a1a 2263 compatibility(OTHER);
887035a5 2264 deselect(term);
64734920 2265 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
4c80361f 2266 if (!state && !term->cfg.no_alt_screen)
887035a5 2267 save_cursor(term, state);
2268 term->disptop = 0;
8f9b6a1a 2269 break;
32874aea 2270 } else
2271 switch (mode) {
a263e4f6 2272 case 4: /* IRM: set insert mode */
32874aea 2273 compatibility(VT102);
887035a5 2274 term->insert = state;
32874aea 2275 break;
a263e4f6 2276 case 12: /* SRM: set echo mode */
887035a5 2277 term->term_echoing = !state;
b9d7bcad 2278 if (term->ldisc) /* cause ldisc to notice changes */
2279 ldisc_send(term->ldisc, NULL, 0, 0);
32874aea 2280 break;
a263e4f6 2281 case 20: /* LNM: Return sends ... */
887035a5 2282 term->cr_lf_return = state;
32874aea 2283 break;
a263e4f6 2284 case 34: /* WYULCURM: Make cursor BIG */
4eeb7d09 2285 compatibility2(OTHER, VT220);
887035a5 2286 term->big_cursor = !state;
7594731b 2287 }
374330e2 2288}
2289
2290/*
2291 * Process an OSC sequence: set window title or icon name.
2292 */
887035a5 2293static void do_osc(Terminal *term)
32874aea 2294{
887035a5 2295 if (term->osc_w) {
2296 while (term->osc_strlen--)
2297 term->wordness[(unsigned char)
2298 term->osc_string[term->osc_strlen]] = term->esc_args[0];
374330e2 2299 } else {
887035a5 2300 term->osc_string[term->osc_strlen] = '\0';
2301 switch (term->esc_args[0]) {
374330e2 2302 case 0:
2303 case 1:
64734920 2304 if (!term->cfg.no_remote_wintitle)
a8327734 2305 set_icon(term->frontend, term->osc_string);
887035a5 2306 if (term->esc_args[0] == 1)
374330e2 2307 break;
2308 /* fall through: parameter 0 means set both */
2309 case 2:
2310 case 21:
64734920 2311 if (!term->cfg.no_remote_wintitle)
a8327734 2312 set_title(term->frontend, term->osc_string);
374330e2 2313 break;
2314 }
2315 }
2316}
2317
2318/*
b44b307a 2319 * ANSI printing routines.
2320 */
887035a5 2321static void term_print_setup(Terminal *term)
b44b307a 2322{
887035a5 2323 bufchain_clear(&term->printer_buf);
64734920 2324 term->print_job = printer_start_job(term->cfg.printer);
b44b307a 2325}
887035a5 2326static void term_print_flush(Terminal *term)
b44b307a 2327{
2328 void *data;
2329 int len;
2330 int size;
887035a5 2331 while ((size = bufchain_size(&term->printer_buf)) > 5) {
2332 bufchain_prefix(&term->printer_buf, &data, &len);
b44b307a 2333 if (len > size-5)
2334 len = size-5;
887035a5 2335 printer_job_data(term->print_job, data, len);
2336 bufchain_consume(&term->printer_buf, len);
b44b307a 2337 }
2338}
887035a5 2339static void term_print_finish(Terminal *term)
b44b307a 2340{
2341 void *data;
2342 int len, size;
2343 char c;
2344
887035a5 2345 if (!term->printing && !term->only_printing)
1709795f 2346 return; /* we need do nothing */
2347
887035a5 2348 term_print_flush(term);
2349 while ((size = bufchain_size(&term->printer_buf)) > 0) {
2350 bufchain_prefix(&term->printer_buf, &data, &len);
b44b307a 2351 c = *(char *)data;
2352 if (c == '\033' || c == '\233') {
887035a5 2353 bufchain_consume(&term->printer_buf, size);
b44b307a 2354 break;
2355 } else {
887035a5 2356 printer_job_data(term->print_job, &c, 1);
2357 bufchain_consume(&term->printer_buf, 1);
b44b307a 2358 }
2359 }
887035a5 2360 printer_finish_job(term->print_job);
2361 term->print_job = NULL;
2362 term->printing = term->only_printing = FALSE;
b44b307a 2363}
2364
2365/*
374330e2 2366 * Remove everything currently in `inbuf' and stick it up on the
2367 * in-memory display. There's a big state machine in here to
2368 * process escape sequences...
2369 */
39934deb 2370static void term_out(Terminal *term)
32874aea 2371{
36566009 2372 unsigned long c;
2373 int unget;
a748a096 2374 unsigned char localbuf[256], *chars;
2375 int nchars = 0;
2376
2377 unget = -1;
2378
1709795f 2379 chars = NULL; /* placate compiler warnings */
887035a5 2380 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
a748a096 2381 if (unget == -1) {
2382 if (nchars == 0) {
2383 void *ret;
887035a5 2384 bufchain_prefix(&term->inbuf, &ret, &nchars);
a748a096 2385 if (nchars > sizeof(localbuf))
2386 nchars = sizeof(localbuf);
2387 memcpy(localbuf, ret, nchars);
887035a5 2388 bufchain_consume(&term->inbuf, nchars);
a748a096 2389 chars = localbuf;
2390 assert(chars != NULL);
2391 }
2392 c = *chars++;
2393 nchars--;
c9def1b8 2394
a748a096 2395 /*
2396 * Optionally log the session traffic to a file. Useful for
2397 * debugging and possibly also useful for actual logging.
2398 */
64734920 2399 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
a8327734 2400 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
a748a096 2401 } else {
2402 c = unget;
2403 unget = -1;
5a16642f 2404 }
2405
e14a5a13 2406 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
2407 * be able to display 8-bit characters, but I'll let that go 'cause
2408 * of i18n.
2409 */
4eeb7d09 2410
b44b307a 2411 /*
2412 * If we're printing, add the character to the printer
2413 * buffer.
2414 */
887035a5 2415 if (term->printing) {
2416 bufchain_add(&term->printer_buf, &c, 1);
b44b307a 2417
2418 /*
2419 * If we're in print-only mode, we use a much simpler
2420 * state machine designed only to recognise the ESC[4i
2421 * termination sequence.
2422 */
887035a5 2423 if (term->only_printing) {
b44b307a 2424 if (c == '\033')
887035a5 2425 term->print_state = 1;
b44b307a 2426 else if (c == (unsigned char)'\233')
887035a5 2427 term->print_state = 2;
2428 else if (c == '[' && term->print_state == 1)
2429 term->print_state = 2;
2430 else if (c == '4' && term->print_state == 2)
2431 term->print_state = 3;
2432 else if (c == 'i' && term->print_state == 3)
2433 term->print_state = 4;
b44b307a 2434 else
887035a5 2435 term->print_state = 0;
2436 if (term->print_state == 4) {
2437 term_print_finish(term);
b44b307a 2438 }
2439 continue;
2440 }
2441 }
2442
4eeb7d09 2443 /* First see about all those translations. */
887035a5 2444 if (term->termstate == TOPLEVEL) {
2445 if (in_utf(term))
2446 switch (term->utf_state) {
4eeb7d09 2447 case 0:
2448 if (c < 0x80) {
8f22582c 2449 /* UTF-8 must be stateless so we ignore iso2022. */
21d2b241 2450 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2451 c = term->ucsdata->unitab_ctrl[c];
36566009 2452 else c = ((unsigned char)c) | CSET_ASCII;
8f22582c 2453 break;
4eeb7d09 2454 } else if ((c & 0xe0) == 0xc0) {
887035a5 2455 term->utf_size = term->utf_state = 1;
2456 term->utf_char = (c & 0x1f);
4eeb7d09 2457 } else if ((c & 0xf0) == 0xe0) {
887035a5 2458 term->utf_size = term->utf_state = 2;
2459 term->utf_char = (c & 0x0f);
4eeb7d09 2460 } else if ((c & 0xf8) == 0xf0) {
887035a5 2461 term->utf_size = term->utf_state = 3;
2462 term->utf_char = (c & 0x07);
4eeb7d09 2463 } else if ((c & 0xfc) == 0xf8) {
887035a5 2464 term->utf_size = term->utf_state = 4;
2465 term->utf_char = (c & 0x03);
4eeb7d09 2466 } else if ((c & 0xfe) == 0xfc) {
887035a5 2467 term->utf_size = term->utf_state = 5;
2468 term->utf_char = (c & 0x01);
4eeb7d09 2469 } else {
2470 c = UCSERR;
2471 break;
2472 }
2473 continue;
2474 case 1:
2475 case 2:
2476 case 3:
2477 case 4:
2478 case 5:
2479 if ((c & 0xC0) != 0x80) {
a748a096 2480 unget = c;
5a16642f 2481 c = UCSERR;
887035a5 2482 term->utf_state = 0;
4eeb7d09 2483 break;
2484 }
887035a5 2485 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
2486 if (--term->utf_state)
4eeb7d09 2487 continue;
2488
887035a5 2489 c = term->utf_char;
4eeb7d09 2490
2491 /* Is somebody trying to be evil! */
2492 if (c < 0x80 ||
887035a5 2493 (c < 0x800 && term->utf_size >= 2) ||
2494 (c < 0x10000 && term->utf_size >= 3) ||
2495 (c < 0x200000 && term->utf_size >= 4) ||
2496 (c < 0x4000000 && term->utf_size >= 5))
4eeb7d09 2497 c = UCSERR;
2498
2499 /* Unicode line separator and paragraph separator are CR-LF */
2500 if (c == 0x2028 || c == 0x2029)
2501 c = 0x85;
2502
2503 /* High controls are probably a Baaad idea too. */
2504 if (c < 0xA0)
2505 c = 0xFFFD;
2506
2507 /* The UTF-16 surrogates are not nice either. */
2508 /* The standard give the option of decoding these:
2509 * I don't want to! */
2510 if (c >= 0xD800 && c < 0xE000)
2511 c = UCSERR;
2512
2513 /* ISO 10646 characters now limited to UTF-16 range. */
2514 if (c > 0x10FFFF)
2515 c = UCSERR;
2516
2517 /* This is currently a TagPhobic application.. */
2518 if (c >= 0xE0000 && c <= 0xE007F)
2519 continue;
2520
2521 /* U+FEFF is best seen as a null. */
2522 if (c == 0xFEFF)
2523 continue;
2524 /* But U+FFFE is an error. */
2525 if (c == 0xFFFE || c == 0xFFFF)
2526 c = UCSERR;
2527
4eeb7d09 2528 break;
d3cb5465 2529 }
2530 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
887035a5 2531 else if(term->sco_acs &&
92e23ad9 2532 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
d3cb5465 2533 {
5dc6132d 2534 if (term->sco_acs == 2) c |= 0x80;
36566009 2535 c |= CSET_SCOACS;
4eeb7d09 2536 } else {
887035a5 2537 switch (term->cset_attr[term->cset]) {
4eeb7d09 2538 /*
2539 * Linedraw characters are different from 'ESC ( B'
2540 * only for a small range. For ones outside that
2541 * range, make sure we use the same font as well as
2542 * the same encoding.
2543 */
36566009 2544 case CSET_LINEDRW:
21d2b241 2545 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2546 c = term->ucsdata->unitab_ctrl[c];
4eeb7d09 2547 else
36566009 2548 c = ((unsigned char) c) | CSET_LINEDRW;
4eeb7d09 2549 break;
2550
36566009 2551 case CSET_GBCHR:
4eeb7d09 2552 /* If UK-ASCII, make the '#' a LineDraw Pound */
2553 if (c == '#') {
36566009 2554 c = '}' | CSET_LINEDRW;
4eeb7d09 2555 break;
2556 }
36566009 2557 /*FALLTHROUGH*/ case CSET_ASCII:
21d2b241 2558 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
2559 c = term->ucsdata->unitab_ctrl[c];
4eeb7d09 2560 else
36566009 2561 c = ((unsigned char) c) | CSET_ASCII;
4eeb7d09 2562 break;
36566009 2563 case CSET_SCOACS:
2564 if (c>=' ') c = ((unsigned char)c) | CSET_SCOACS;
d3cb5465 2565 break;
4eeb7d09 2566 }
2567 }
2568 }
2569
2570 /* How about C1 controls ? */
887035a5 2571 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
2572 !term->vt52_mode && has_compat(VT220)) {
2573 term->termstate = SEEN_ESC;
2574 term->esc_query = FALSE;
4eeb7d09 2575 c = '@' + (c & 0x1F);
2576 }
2577
2578 /* Or the GL control. */
887035a5 2579 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
2580 if (term->curs.x && !term->wrapnext)
2581 term->curs.x--;
2582 term->wrapnext = FALSE;
36566009 2583 /* destructive backspace might be disabled */
2584 if (!term->cfg.no_dbackspace) {
cdf249c7 2585 check_boundary(term, term->curs.x, term->curs.y);
2586 check_boundary(term, term->curs.x+1, term->curs.y);
c6958dfe 2587 copy_termchar(scrlineptr(term->curs.y),
2588 term->curs.x, &term->erase_char);
36566009 2589 }
4eeb7d09 2590 } else
2591 /* Or normal C0 controls. */
36566009 2592 if ((c & ~0x1F) == 0 && term->termstate < DO_CTRLS) {
374330e2 2593 switch (c) {
a263e4f6 2594 case '\005': /* ENQ: terminal type query */
e14a5a13 2595 /* Strictly speaking this is VT100 but a VT100 defaults to
2596 * no response. Other terminals respond at their option.
2597 *
2598 * Don't put a CR in the default string as this tends to
2599 * upset some weird software.
2600 *
2601 * An xterm returns "xterm" (5 characters)
2602 */
32874aea 2603 compatibility(ANSIMIN);
b9d7bcad 2604 if (term->ldisc) {
e7fbcdd8 2605 char abuf[256], *s, *d;
32874aea 2606 int state = 0;
64734920 2607 for (s = term->cfg.answerback, d = abuf; *s; s++) {
32874aea 2608 if (state) {
2609 if (*s >= 'a' && *s <= 'z')
2610 *d++ = (*s - ('a' - 1));
2611 else if ((*s >= '@' && *s <= '_') ||
2612 *s == '?' || (*s & 0x80))
2613 *d++ = ('@' ^ *s);
2614 else if (*s == '~')
2615 *d++ = '^';
2616 state = 0;
2617 } else if (*s == '^') {
2618 state = 1;
2619 } else
4eeb7d09 2620 *d++ = *s;
e7fbcdd8 2621 }
b9d7bcad 2622 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
2623 abuf, d - abuf, 0);
e7fbcdd8 2624 }
374330e2 2625 break;
a263e4f6 2626 case '\007': /* BEL: Bell */
156686ef 2627 {
2628 struct beeptime *newbeep;
286c6b86 2629 unsigned long ticks;
156686ef 2630
f7f27309 2631 ticks = GETTICKCOUNT();
156686ef 2632
887035a5 2633 if (!term->beep_overloaded) {
3d88e64d 2634 newbeep = snew(struct beeptime);
156686ef 2635 newbeep->ticks = ticks;
2636 newbeep->next = NULL;
887035a5 2637 if (!term->beephead)
2638 term->beephead = newbeep;
156686ef 2639 else
887035a5 2640 term->beeptail->next = newbeep;
2641 term->beeptail = newbeep;
2642 term->nbeeps++;
156686ef 2643 }
2644
2645 /*
2646 * Throw out any beeps that happened more than
2647 * t seconds ago.
2648 */
887035a5 2649 while (term->beephead &&
64734920 2650 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
887035a5 2651 struct beeptime *tmp = term->beephead;
2652 term->beephead = tmp->next;
156686ef 2653 sfree(tmp);
887035a5 2654 if (!term->beephead)
2655 term->beeptail = NULL;
2656 term->nbeeps--;
156686ef 2657 }
2658
64734920 2659 if (term->cfg.bellovl && term->beep_overloaded &&
2660 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
156686ef 2661 /*
2662 * If we're currently overloaded and the
2663 * last beep was more than s seconds ago,
2664 * leave overload mode.
2665 */
887035a5 2666 term->beep_overloaded = FALSE;
64734920 2667 } else if (term->cfg.bellovl && !term->beep_overloaded &&
2668 term->nbeeps >= term->cfg.bellovl_n) {
156686ef 2669 /*
2670 * Now, if we have n or more beeps
2671 * remaining in the queue, go into overload
2672 * mode.
2673 */
887035a5 2674 term->beep_overloaded = TRUE;
156686ef 2675 }
887035a5 2676 term->lastbeep = ticks;
156686ef 2677
2678 /*
2679 * Perform an actual beep if we're not overloaded.
2680 */
64734920 2681 if (!term->cfg.bellovl || !term->beep_overloaded) {
2682 beep(term->frontend, term->cfg.beep);
39934deb 2683
64734920 2684 if (term->cfg.beep == BELL_VISUAL) {
39934deb 2685 term_schedule_vbell(term, FALSE, 0);
0ce89525 2686 }
156686ef 2687 }
39934deb 2688 seen_disp_event(term);
156686ef 2689 }
374330e2 2690 break;
a263e4f6 2691 case '\b': /* BS: Back space */
887035a5 2692 if (term->curs.x == 0 &&
2693 (term->curs.y == 0 || term->wrap == 0))
2694 /* do nothing */ ;
2695 else if (term->curs.x == 0 && term->curs.y > 0)
2696 term->curs.x = term->cols - 1, term->curs.y--;
2697 else if (term->wrapnext)
2698 term->wrapnext = FALSE;
374330e2 2699 else
887035a5 2700 term->curs.x--;
39934deb 2701 seen_disp_event(term);
374330e2 2702 break;
a263e4f6 2703 case '\016': /* LS1: Locking-shift one */
32874aea 2704 compatibility(VT100);
887035a5 2705 term->cset = 1;
374330e2 2706 break;
a263e4f6 2707 case '\017': /* LS0: Locking-shift zero */
32874aea 2708 compatibility(VT100);
887035a5 2709 term->cset = 0;
374330e2 2710 break;
a263e4f6 2711 case '\033': /* ESC: Escape */
887035a5 2712 if (term->vt52_mode)
2713 term->termstate = VT52_ESC;
e14a5a13 2714 else {
2715 compatibility(ANSIMIN);
887035a5 2716 term->termstate = SEEN_ESC;
2717 term->esc_query = FALSE;
e14a5a13 2718 }
374330e2 2719 break;
a263e4f6 2720 case '\015': /* CR: Carriage return */
887035a5 2721 term->curs.x = 0;
2722 term->wrapnext = FALSE;
39934deb 2723 seen_disp_event(term);
887035a5 2724 term->paste_hold = 0;
a8327734 2725 if (term->logctx)
2726 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
374330e2 2727 break;
a263e4f6 2728 case '\014': /* FF: Form feed */
1a837633 2729 if (has_compat(SCOANSI)) {
887035a5 2730 move(term, 0, 0, 0);
2731 erase_lots(term, FALSE, FALSE, TRUE);
2732 term->disptop = 0;
2733 term->wrapnext = FALSE;
39934deb 2734 seen_disp_event(term);
1a837633 2735 break;
2736 }
a263e4f6 2737 case '\013': /* VT: Line tabulation */
32874aea 2738 compatibility(VT100);
a263e4f6 2739 case '\012': /* LF: Line feed */
887035a5 2740 if (term->curs.y == term->marg_b)
2741 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2742 else if (term->curs.y < term->rows - 1)
2743 term->curs.y++;
64734920 2744 if (term->cfg.lfhascr)
887035a5 2745 term->curs.x = 0;
887035a5 2746 term->wrapnext = FALSE;
39934deb 2747 seen_disp_event(term);
887035a5 2748 term->paste_hold = 0;
a8327734 2749 if (term->logctx)
2750 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
374330e2 2751 break;
a263e4f6 2752 case '\t': /* HT: Character tabulation */
374330e2 2753 {
887035a5 2754 pos old_curs = term->curs;
36566009 2755 termline *ldata = scrlineptr(term->curs.y);
c9def1b8 2756
2757 do {
887035a5 2758 term->curs.x++;
2759 } while (term->curs.x < term->cols - 1 &&
2760 !term->tabs[term->curs.x]);
c9def1b8 2761
36566009 2762 if ((ldata->lattr & LATTR_MODE) != LATTR_NORM) {
887035a5 2763 if (term->curs.x >= term->cols / 2)
2764 term->curs.x = term->cols / 2 - 1;
32874aea 2765 } else {
887035a5 2766 if (term->curs.x >= term->cols)
2767 term->curs.x = term->cols - 1;
c9def1b8 2768 }
2769
887035a5 2770 check_selection(term, old_curs, term->curs);
374330e2 2771 }
39934deb 2772 seen_disp_event(term);
374330e2 2773 break;
cabfd08c 2774 }
32874aea 2775 } else
887035a5 2776 switch (term->termstate) {
32874aea 2777 case TOPLEVEL:
887035a5 2778 /* Only graphic characters get this far;
2779 * ctrls are stripped above */
4eeb7d09 2780 {
c6958dfe 2781 termline *cline = scrlineptr(term->curs.y);
4eeb7d09 2782 int width = 0;
2783 if (DIRECT_CHAR(c))
2784 width = 1;
2785 if (!width)
2786 width = wcwidth((wchar_t) c);
c6958dfe 2787
2788 if (term->wrapnext && term->wrap && width > 0) {
2789 cline->lattr |= LATTR_WRAPPED;
2790 if (term->curs.y == term->marg_b)
2791 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2792 else if (term->curs.y < term->rows - 1)
2793 term->curs.y++;
2794 term->curs.x = 0;
2795 term->wrapnext = FALSE;
1c10e849 2796 cline = scrlineptr(term->curs.y);
c6958dfe 2797 }
2798 if (term->insert && width > 0)
2799 insch(term, width);
2800 if (term->selstate != NO_SELECTION) {
2801 pos cursplus = term->curs;
2802 incpos(cursplus);
2803 check_selection(term, term->curs, cursplus);
2804 }
2805 if (((c & CSET_MASK) == CSET_ASCII ||
2806 (c & CSET_MASK) == 0) &&
2807 term->logctx)
2808 logtraffic(term->logctx, (unsigned char) c,
2809 LGTYP_ASCII);
2810
4eeb7d09 2811 switch (width) {
2812 case 2:
3c7366f8 2813 /*
2814 * If we're about to display a double-width
2815 * character starting in the rightmost
2816 * column, then we do something special
2817 * instead. We must print a space in the
2818 * last column of the screen, then wrap;
2819 * and we also set LATTR_WRAPPED2 which
2820 * instructs subsequent cut-and-pasting not
2821 * only to splice this line to the one
2822 * after it, but to ignore the space in the
2823 * last character position as well.
2824 * (Because what was actually output to the
2825 * terminal was presumably just a sequence
2826 * of CJK characters, and we don't want a
2827 * space to be pasted in the middle of
2828 * those just because they had the
2829 * misfortune to start in the wrong parity
2830 * column. xterm concurs.)
2831 */
2832 check_boundary(term, term->curs.x, term->curs.y);
2833 check_boundary(term, term->curs.x+2, term->curs.y);
2834 if (term->curs.x == term->cols-1) {
c6958dfe 2835 copy_termchar(cline, term->curs.x,
2836 &term->erase_char);
2837 cline->lattr |= LATTR_WRAPPED | LATTR_WRAPPED2;
887035a5 2838 if (term->curs.y == term->marg_b)
2839 scroll(term, term->marg_t, term->marg_b,
2840 1, TRUE);
2841 else if (term->curs.y < term->rows - 1)
2842 term->curs.y++;
2843 term->curs.x = 0;
3c7366f8 2844 /* Now we must check_boundary again, of course. */
2845 check_boundary(term, term->curs.x, term->curs.y);
2846 check_boundary(term, term->curs.x+2, term->curs.y);
4eeb7d09 2847 }
c6958dfe 2848
2849 /* FULL-TERMCHAR */
2850 clear_cc(cline, term->curs.x);
2851 cline->chars[term->curs.x].chr = c;
2852 cline->chars[term->curs.x].attr = term->curr_attr;
2853
3c7366f8 2854 term->curs.x++;
c6958dfe 2855
2856 /* FULL-TERMCHAR */
2857 clear_cc(cline, term->curs.x);
2858 cline->chars[term->curs.x].chr = UCSWIDE;
2859 cline->chars[term->curs.x].attr = term->curr_attr;
2860
5a73255e 2861 break;
4eeb7d09 2862 case 1:
3c7366f8 2863 check_boundary(term, term->curs.x, term->curs.y);
2864 check_boundary(term, term->curs.x+1, term->curs.y);
c6958dfe 2865
2866 /* FULL-TERMCHAR */
2867 clear_cc(cline, term->curs.x);
2868 cline->chars[term->curs.x].chr = c;
2869 cline->chars[term->curs.x].attr = term->curr_attr;
2870
32874aea 2871 break;
c6958dfe 2872 case 0:
f00630be 2873 if (term->curs.x > 0) {
2874 int x = term->curs.x - 1;
2875
2876 /* If we're in wrapnext state, the character
2877 * to combine with is _here_, not to our left. */
2878 if (term->wrapnext)
2879 x++;
2880
2881 /*
2882 * If the previous character is
2883 * UCSWIDE, back up another one.
2884 */
2885 if (cline->chars[x].chr == UCSWIDE) {
2886 assert(x > 0);
2887 x--;
2888 }
2889
2890 add_cc(cline, x, c);
39934deb 2891 seen_disp_event(term);
f00630be 2892 }
c6958dfe 2893 continue;
4eeb7d09 2894 default:
2895 continue;
32874aea 2896 }
c6958dfe 2897 term->curs.x++;
2898 if (term->curs.x == term->cols) {
2899 term->curs.x--;
2900 term->wrapnext = TRUE;
2901 if (term->wrap && term->vt52_mode) {
2902 cline->lattr |= LATTR_WRAPPED;
2903 if (term->curs.y == term->marg_b)
2904 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2905 else if (term->curs.y < term->rows - 1)
2906 term->curs.y++;
2907 term->curs.x = 0;
2908 term->wrapnext = FALSE;
2909 }
4eeb7d09 2910 }
39934deb 2911 seen_disp_event(term);
e14a5a13 2912 }
374330e2 2913 break;
32874aea 2914
32874aea 2915 case OSC_MAYBE_ST:
2916 /*
2917 * This state is virtually identical to SEEN_ESC, with the
2918 * exception that we have an OSC sequence in the pipeline,
2919 * and _if_ we see a backslash, we process it.
2920 */
2921 if (c == '\\') {
887035a5 2922 do_osc(term);
2923 term->termstate = TOPLEVEL;
32874aea 2924 break;
e14a5a13 2925 }
32874aea 2926 /* else fall through */
2927 case SEEN_ESC:
4eeb7d09 2928 if (c >= ' ' && c <= '/') {
887035a5 2929 if (term->esc_query)
2930 term->esc_query = -1;
4eeb7d09 2931 else
887035a5 2932 term->esc_query = c;
32874aea 2933 break;
4eeb7d09 2934 }
887035a5 2935 term->termstate = TOPLEVEL;
2936 switch (ANSI(c, term->esc_query)) {
a263e4f6 2937 case '[': /* enter CSI mode */
887035a5 2938 term->termstate = SEEN_CSI;
2939 term->esc_nargs = 1;
2940 term->esc_args[0] = ARG_DEFAULT;
2941 term->esc_query = FALSE;
32874aea 2942 break;
a263e4f6 2943 case ']': /* OSC: xterm escape sequences */
32874aea 2944 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
2945 compatibility(OTHER);
887035a5 2946 term->termstate = SEEN_OSC;
2947 term->esc_args[0] = 0;
32874aea 2948 break;
a263e4f6 2949 case '7': /* DECSC: save cursor */
32874aea 2950 compatibility(VT100);
887035a5 2951 save_cursor(term, TRUE);
32874aea 2952 break;
a263e4f6 2953 case '8': /* DECRC: restore cursor */
32874aea 2954 compatibility(VT100);
887035a5 2955 save_cursor(term, FALSE);
39934deb 2956 seen_disp_event(term);
32874aea 2957 break;
a263e4f6 2958 case '=': /* DECKPAM: Keypad application mode */
32874aea 2959 compatibility(VT100);
887035a5 2960 term->app_keypad_keys = TRUE;
32874aea 2961 break;
a263e4f6 2962 case '>': /* DECKPNM: Keypad numeric mode */
32874aea 2963 compatibility(VT100);
887035a5 2964 term->app_keypad_keys = FALSE;
32874aea 2965 break;
a263e4f6 2966 case 'D': /* IND: exactly equivalent to LF */
32874aea 2967 compatibility(VT100);
887035a5 2968 if (term->curs.y == term->marg_b)
2969 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2970 else if (term->curs.y < term->rows - 1)
2971 term->curs.y++;
887035a5 2972 term->wrapnext = FALSE;
39934deb 2973 seen_disp_event(term);
32874aea 2974 break;
a263e4f6 2975 case 'E': /* NEL: exactly equivalent to CR-LF */
32874aea 2976 compatibility(VT100);
887035a5 2977 term->curs.x = 0;
2978 if (term->curs.y == term->marg_b)
2979 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
2980 else if (term->curs.y < term->rows - 1)
2981 term->curs.y++;
887035a5 2982 term->wrapnext = FALSE;
39934deb 2983 seen_disp_event(term);
32874aea 2984 break;
a263e4f6 2985 case 'M': /* RI: reverse index - backwards LF */
32874aea 2986 compatibility(VT100);
887035a5 2987 if (term->curs.y == term->marg_t)
2988 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
2989 else if (term->curs.y > 0)
2990 term->curs.y--;
887035a5 2991 term->wrapnext = FALSE;
39934deb 2992 seen_disp_event(term);
32874aea 2993 break;
a263e4f6 2994 case 'Z': /* DECID: terminal type query */
32874aea 2995 compatibility(VT100);
b9d7bcad 2996 if (term->ldisc)
2997 ldisc_send(term->ldisc, term->id_string,
2998 strlen(term->id_string), 0);
32874aea 2999 break;
a263e4f6 3000 case 'c': /* RIS: restore power-on settings */
32874aea 3001 compatibility(VT100);
887035a5 3002 power_on(term);
b9d7bcad 3003 if (term->ldisc) /* cause ldisc to notice changes */
3004 ldisc_send(term->ldisc, NULL, 0, 0);
887035a5 3005 if (term->reset_132) {
64734920 3006 if (!term->cfg.no_remote_resize)
a8327734 3007 request_resize(term->frontend, 80, term->rows);
887035a5 3008 term->reset_132 = 0;
374330e2 3009 }
887035a5 3010 term->disptop = 0;
39934deb 3011 seen_disp_event(term);
32874aea 3012 break;
a263e4f6 3013 case 'H': /* HTS: set a tab */
32874aea 3014 compatibility(VT100);
887035a5 3015 term->tabs[term->curs.x] = TRUE;
32874aea 3016 break;
4eeb7d09 3017
a263e4f6 3018 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
4eeb7d09 3019 compatibility(VT100);
3020 {
36566009 3021 termline *ldata;
4eeb7d09 3022 int i, j;
3023 pos scrtop, scrbot;
3024
887035a5 3025 for (i = 0; i < term->rows; i++) {
36566009 3026 ldata = scrlineptr(i);
3027 for (j = 0; j < term->cols; j++) {
c6958dfe 3028 copy_termchar(ldata, j,
3029 &term->basic_erase_char);
36566009 3030 ldata->chars[j].chr = 'E';
3031 }
3032 ldata->lattr = LATTR_NORM;
4eeb7d09 3033 }
887035a5 3034 term->disptop = 0;
39934deb 3035 seen_disp_event(term);
4eeb7d09 3036 scrtop.x = scrtop.y = 0;
3037 scrbot.x = 0;
887035a5 3038 scrbot.y = term->rows;
3039 check_selection(term, scrtop, scrbot);
4eeb7d09 3040 }
3041 break;
3042
3043 case ANSI('3', '#'):
3044 case ANSI('4', '#'):
3045 case ANSI('5', '#'):
3046 case ANSI('6', '#'):
3047 compatibility(VT100);
3048 {
36566009 3049 int nlattr;
3050
887035a5 3051 switch (ANSI(c, term->esc_query)) {
a263e4f6 3052 case ANSI('3', '#'): /* DECDHL: 2*height, top */
4eeb7d09 3053 nlattr = LATTR_TOP;
3054 break;
a263e4f6 3055 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
4eeb7d09 3056 nlattr = LATTR_BOT;
3057 break;
a263e4f6 3058 case ANSI('5', '#'): /* DECSWL: normal */
4eeb7d09 3059 nlattr = LATTR_NORM;
3060 break;
a263e4f6 3061 default: /* case ANSI('6', '#'): DECDWL: 2*width */
4eeb7d09 3062 nlattr = LATTR_WIDE;
3063 break;
3064 }
36566009 3065 scrlineptr(term->curs.y)->lattr = nlattr;
4eeb7d09 3066 }
3067 break;
a263e4f6 3068 /* GZD4: G0 designate 94-set */
4eeb7d09 3069 case ANSI('A', '('):
3070 compatibility(VT100);
64734920 3071 if (!term->cfg.no_remote_charset)
36566009 3072 term->cset_attr[0] = CSET_GBCHR;
4eeb7d09 3073 break;
3074 case ANSI('B', '('):
3075 compatibility(VT100);
64734920 3076 if (!term->cfg.no_remote_charset)
36566009 3077 term->cset_attr[0] = CSET_ASCII;
4eeb7d09 3078 break;
3079 case ANSI('0', '('):
3080 compatibility(VT100);
64734920 3081 if (!term->cfg.no_remote_charset)
36566009 3082 term->cset_attr[0] = CSET_LINEDRW;
4eeb7d09 3083 break;
d3cb5465 3084 case ANSI('U', '('):
3085 compatibility(OTHER);
64734920 3086 if (!term->cfg.no_remote_charset)
36566009 3087 term->cset_attr[0] = CSET_SCOACS;
d3cb5465 3088 break;
a263e4f6 3089 /* G1D4: G1-designate 94-set */
4eeb7d09 3090 case ANSI('A', ')'):
3091 compatibility(VT100);
64734920 3092 if (!term->cfg.no_remote_charset)
36566009 3093 term->cset_attr[1] = CSET_GBCHR;
4eeb7d09 3094 break;
3095 case ANSI('B', ')'):
3096 compatibility(VT100);
64734920 3097 if (!term->cfg.no_remote_charset)
36566009 3098 term->cset_attr[1] = CSET_ASCII;
4eeb7d09 3099 break;
3100 case ANSI('0', ')'):
3101 compatibility(VT100);
64734920 3102 if (!term->cfg.no_remote_charset)
36566009 3103 term->cset_attr[1] = CSET_LINEDRW;
4eeb7d09 3104 break;
d3cb5465 3105 case ANSI('U', ')'):
3106 compatibility(OTHER);
64734920 3107 if (!term->cfg.no_remote_charset)
36566009 3108 term->cset_attr[1] = CSET_SCOACS;
d3cb5465 3109 break;
a263e4f6 3110 /* DOCS: Designate other coding system */
4eeb7d09 3111 case ANSI('8', '%'): /* Old Linux code */
3112 case ANSI('G', '%'):
3113 compatibility(OTHER);
64734920 3114 if (!term->cfg.no_remote_charset)
887035a5 3115 term->utf = 1;
4eeb7d09 3116 break;
3117 case ANSI('@', '%'):
3118 compatibility(OTHER);
64734920 3119 if (!term->cfg.no_remote_charset)
887035a5 3120 term->utf = 0;
4eeb7d09 3121 break;
374330e2 3122 }
3123 break;
32874aea 3124 case SEEN_CSI:
887035a5 3125 term->termstate = TOPLEVEL; /* default */
32874aea 3126 if (isdigit(c)) {
887035a5 3127 if (term->esc_nargs <= ARGS_MAX) {
3128 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
3129 term->esc_args[term->esc_nargs - 1] = 0;
3130 term->esc_args[term->esc_nargs - 1] =
3131 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
32874aea 3132 }
887035a5 3133 term->termstate = SEEN_CSI;
32874aea 3134 } else if (c == ';') {
887035a5 3135 if (++term->esc_nargs <= ARGS_MAX)
3136 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
3137 term->termstate = SEEN_CSI;
32874aea 3138 } else if (c < '@') {
887035a5 3139 if (term->esc_query)
3140 term->esc_query = -1;
32874aea 3141 else if (c == '?')
887035a5 3142 term->esc_query = TRUE;
32874aea 3143 else
887035a5 3144 term->esc_query = c;
3145 term->termstate = SEEN_CSI;
32874aea 3146 } else
887035a5 3147 switch (ANSI(c, term->esc_query)) {
a263e4f6 3148 case 'A': /* CUU: move up N lines */
887035a5 3149 move(term, term->curs.x,
3150 term->curs.y - def(term->esc_args[0], 1), 1);
39934deb 3151 seen_disp_event(term);
32874aea 3152 break;
a263e4f6 3153 case 'e': /* VPR: move down N lines */
32874aea 3154 compatibility(ANSI);
5442e92f 3155 /* FALLTHROUGH */
a263e4f6 3156 case 'B': /* CUD: Cursor down */
887035a5 3157 move(term, term->curs.x,
3158 term->curs.y + def(term->esc_args[0], 1), 1);
39934deb 3159 seen_disp_event(term);
32874aea 3160 break;
a263e4f6 3161 case ANSI('c', '>'): /* DA: report xterm version */
32874aea 3162 compatibility(OTHER);
3163 /* this reports xterm version 136 so that VIM can
3164 use the drag messages from the mouse reporting */
b9d7bcad 3165 if (term->ldisc)
3166 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
32874aea 3167 break;
a263e4f6 3168 case 'a': /* HPR: move right N cols */
5442e92f 3169 compatibility(ANSI);
3170 /* FALLTHROUGH */
a263e4f6 3171 case 'C': /* CUF: Cursor right */
887035a5 3172 move(term, term->curs.x + def(term->esc_args[0], 1),
3173 term->curs.y, 1);
39934deb 3174 seen_disp_event(term);
32874aea 3175 break;
a263e4f6 3176 case 'D': /* CUB: move left N cols */
887035a5 3177 move(term, term->curs.x - def(term->esc_args[0], 1),
3178 term->curs.y, 1);
39934deb 3179 seen_disp_event(term);
32874aea 3180 break;
a263e4f6 3181 case 'E': /* CNL: move down N lines and CR */
32874aea 3182 compatibility(ANSI);
887035a5 3183 move(term, 0,
3184 term->curs.y + def(term->esc_args[0], 1), 1);
39934deb 3185 seen_disp_event(term);
32874aea 3186 break;
a263e4f6 3187 case 'F': /* CPL: move up N lines and CR */
32874aea 3188 compatibility(ANSI);
887035a5 3189 move(term, 0,
3190 term->curs.y - def(term->esc_args[0], 1), 1);
39934deb 3191 seen_disp_event(term);
32874aea 3192 break;
a263e4f6 3193 case 'G': /* CHA */
3194 case '`': /* HPA: set horizontal posn */
32874aea 3195 compatibility(ANSI);
887035a5 3196 move(term, def(term->esc_args[0], 1) - 1,
3197 term->curs.y, 0);
39934deb 3198 seen_disp_event(term);
32874aea 3199 break;
a263e4f6 3200 case 'd': /* VPA: set vertical posn */
32874aea 3201 compatibility(ANSI);
887035a5 3202 move(term, term->curs.x,
3203 ((term->dec_om ? term->marg_t : 0) +
3204 def(term->esc_args[0], 1) - 1),
3205 (term->dec_om ? 2 : 0));
39934deb 3206 seen_disp_event(term);
32874aea 3207 break;
a263e4f6 3208 case 'H': /* CUP */
3209 case 'f': /* HVP: set horz and vert posns at once */
887035a5 3210 if (term->esc_nargs < 2)
3211 term->esc_args[1] = ARG_DEFAULT;
3212 move(term, def(term->esc_args[1], 1) - 1,
3213 ((term->dec_om ? term->marg_t : 0) +
3214 def(term->esc_args[0], 1) - 1),
3215 (term->dec_om ? 2 : 0));
39934deb 3216 seen_disp_event(term);
32874aea 3217 break;
a263e4f6 3218 case 'J': /* ED: erase screen or parts of it */
32874aea 3219 {
887035a5 3220 unsigned int i = def(term->esc_args[0], 0) + 1;
32874aea 3221 if (i > 3)
3222 i = 0;
887035a5 3223 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
32874aea 3224 }
887035a5 3225 term->disptop = 0;
39934deb 3226 seen_disp_event(term);
32874aea 3227 break;
a263e4f6 3228 case 'K': /* EL: erase line or parts of it */
32874aea 3229 {
887035a5 3230 unsigned int i = def(term->esc_args[0], 0) + 1;
32874aea 3231 if (i > 3)
3232 i = 0;
887035a5 3233 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
32874aea 3234 }
39934deb 3235 seen_disp_event(term);
32874aea 3236 break;
a263e4f6 3237 case 'L': /* IL: insert lines */
32874aea 3238 compatibility(VT102);
887035a5 3239 if (term->curs.y <= term->marg_b)
3240 scroll(term, term->curs.y, term->marg_b,
3241 -def(term->esc_args[0], 1), FALSE);
39934deb 3242 seen_disp_event(term);
32874aea 3243 break;
a263e4f6 3244 case 'M': /* DL: delete lines */
32874aea 3245 compatibility(VT102);
887035a5 3246 if (term->curs.y <= term->marg_b)
3247 scroll(term, term->curs.y, term->marg_b,
3248 def(term->esc_args[0], 1),
32874aea 3249 TRUE);
39934deb 3250 seen_disp_event(term);
32874aea 3251 break;
a263e4f6 3252 case '@': /* ICH: insert chars */
32874aea 3253 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
3254 compatibility(VT102);
887035a5 3255 insch(term, def(term->esc_args[0], 1));
39934deb 3256 seen_disp_event(term);
32874aea 3257 break;
a263e4f6 3258 case 'P': /* DCH: delete chars */
32874aea 3259 compatibility(VT102);
887035a5 3260 insch(term, -def(term->esc_args[0], 1));
39934deb 3261 seen_disp_event(term);
32874aea 3262 break;
a263e4f6 3263 case 'c': /* DA: terminal type query */
32874aea 3264 compatibility(VT100);
3265 /* This is the response for a VT102 */
b9d7bcad 3266 if (term->ldisc)
3267 ldisc_send(term->ldisc, term->id_string,
3268 strlen(term->id_string), 0);
32874aea 3269 break;
a263e4f6 3270 case 'n': /* DSR: cursor position query */
b9d7bcad 3271 if (term->ldisc) {
3272 if (term->esc_args[0] == 6) {
3273 char buf[32];
3274 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
3275 term->curs.x + 1);
3276 ldisc_send(term->ldisc, buf, strlen(buf), 0);
3277 } else if (term->esc_args[0] == 5) {
3278 ldisc_send(term->ldisc, "\033[0n", 4, 0);
3279 }
32874aea 3280 }
3281 break;
a263e4f6 3282 case 'h': /* SM: toggle modes to high */
32874aea 3283 case ANSI_QUE('h'):
3284 compatibility(VT100);
3285 {
3286 int i;
887035a5 3287 for (i = 0; i < term->esc_nargs; i++)
3288 toggle_mode(term, term->esc_args[i],
3289 term->esc_query, TRUE);
32874aea 3290 }
3291 break;
a263e4f6 3292 case 'i': /* MC: Media copy */
b44b307a 3293 case ANSI_QUE('i'):
3294 compatibility(VT100);
3295 {
887035a5 3296 if (term->esc_nargs != 1) break;
64734920 3297 if (term->esc_args[0] == 5 && *term->cfg.printer) {
887035a5 3298 term->printing = TRUE;
3299 term->only_printing = !term->esc_query;
3300 term->print_state = 0;
3301 term_print_setup(term);
3302 } else if (term->esc_args[0] == 4 &&
3303 term->printing) {
3304 term_print_finish(term);
b44b307a 3305 }
3306 }
3307 break;
a263e4f6 3308 case 'l': /* RM: toggle modes to low */
32874aea 3309 case ANSI_QUE('l'):
3310 compatibility(VT100);
3311 {
3312 int i;
887035a5 3313 for (i = 0; i < term->esc_nargs; i++)
3314 toggle_mode(term, term->esc_args[i],
3315 term->esc_query, FALSE);
32874aea 3316 }
3317 break;
a263e4f6 3318 case 'g': /* TBC: clear tabs */
32874aea 3319 compatibility(VT100);
887035a5 3320 if (term->esc_nargs == 1) {
3321 if (term->esc_args[0] == 0) {
3322 term->tabs[term->curs.x] = FALSE;
3323 } else if (term->esc_args[0] == 3) {
32874aea 3324 int i;
887035a5 3325 for (i = 0; i < term->cols; i++)
3326 term->tabs[i] = FALSE;
32874aea 3327 }
3328 }
3329 break;
a263e4f6 3330 case 'r': /* DECSTBM: set scroll margins */
32874aea 3331 compatibility(VT100);
887035a5 3332 if (term->esc_nargs <= 2) {
32874aea 3333 int top, bot;
887035a5 3334 top = def(term->esc_args[0], 1) - 1;
3335 bot = (term->esc_nargs <= 1
3336 || term->esc_args[1] == 0 ?
3337 term->rows :
3338 def(term->esc_args[1], term->rows)) - 1;
3339 if (bot >= term->rows)
3340 bot = term->rows - 1;
32874aea 3341 /* VTTEST Bug 9 - if region is less than 2 lines
3342 * don't change region.
3343 */
3344 if (bot - top > 0) {
887035a5 3345 term->marg_t = top;
3346 term->marg_b = bot;
3347 term->curs.x = 0;
32874aea 3348 /*
3349 * I used to think the cursor should be
3350 * placed at the top of the newly marginned
3351 * area. Apparently not: VMS TPU falls over
3352 * if so.
3353 *
887035a5 3354 * Well actually it should for
3355 * Origin mode - RDB
32874aea 3356 */
887035a5 3357 term->curs.y = (term->dec_om ?
3358 term->marg_t : 0);
39934deb 3359 seen_disp_event(term);
32874aea 3360 }
3361 }
3362 break;
a263e4f6 3363 case 'm': /* SGR: set graphics rendition */
32874aea 3364 {
3365 /*
887035a5 3366 * A VT100 without the AVO only had one
3367 * attribute, either underline or
3368 * reverse video depending on the
3369 * cursor type, this was selected by
3370 * CSI 7m.
32874aea 3371 *
3372 * case 2:
887035a5 3373 * This is sometimes DIM, eg on the
3374 * GIGI and Linux
32874aea 3375 * case 8:
4eeb7d09 3376 * This is sometimes INVIS various ANSI.
32874aea 3377 * case 21:
3378 * This like 22 disables BOLD, DIM and INVIS
3379 *
887035a5 3380 * The ANSI colours appear on any
3381 * terminal that has colour (obviously)
3382 * but the interaction between sgr0 and
3383 * the colours varies but is usually
3384 * related to the background colour
3385 * erase item. The interaction between
3386 * colour attributes and the mono ones
3387 * is also very implementation
3388 * dependent.
32874aea 3389 *
887035a5 3390 * The 39 and 49 attributes are likely
3391 * to be unimplemented.
32874aea 3392 */
3393 int i;
887035a5 3394 for (i = 0; i < term->esc_nargs; i++) {
3395 switch (def(term->esc_args[i], 0)) {
32874aea 3396 case 0: /* restore defaults */
69761fd5 3397 term->curr_attr = term->default_attr;
32874aea 3398 break;
3399 case 1: /* enable bold */
3400 compatibility(VT100AVO);
887035a5 3401 term->curr_attr |= ATTR_BOLD;
32874aea 3402 break;
3403 case 21: /* (enable double underline) */
3404 compatibility(OTHER);
3405 case 4: /* enable underline */
3406 compatibility(VT100AVO);
887035a5 3407 term->curr_attr |= ATTR_UNDER;
32874aea 3408 break;
3409 case 5: /* enable blink */
3410 compatibility(VT100AVO);
887035a5 3411 term->curr_attr |= ATTR_BLINK;
32874aea 3412 break;
1b20b972 3413 case 6: /* SCO light bkgrd */
3414 compatibility(SCOANSI);
3415 term->blink_is_real = FALSE;
3416 term->curr_attr |= ATTR_BLINK;
39934deb 3417 term_schedule_tblink(term);
1b20b972 3418 break;
32874aea 3419 case 7: /* enable reverse video */
887035a5 3420 term->curr_attr |= ATTR_REVERSE;
32874aea 3421 break;
d3cb5465 3422 case 10: /* SCO acs off */
3423 compatibility(SCOANSI);
64734920 3424 if (term->cfg.no_remote_charset) break;
887035a5 3425 term->sco_acs = 0; break;
d3cb5465 3426 case 11: /* SCO acs on */
3427 compatibility(SCOANSI);
64734920 3428 if (term->cfg.no_remote_charset) break;
887035a5 3429 term->sco_acs = 1; break;
5dc6132d 3430 case 12: /* SCO acs on, |0x80 */
d3cb5465 3431 compatibility(SCOANSI);
64734920 3432 if (term->cfg.no_remote_charset) break;
887035a5 3433 term->sco_acs = 2; break;
32874aea 3434 case 22: /* disable bold */
3435 compatibility2(OTHER, VT220);
887035a5 3436 term->curr_attr &= ~ATTR_BOLD;
32874aea 3437 break;
3438 case 24: /* disable underline */
3439 compatibility2(OTHER, VT220);
887035a5 3440 term->curr_attr &= ~ATTR_UNDER;
32874aea 3441 break;
3442 case 25: /* disable blink */
3443 compatibility2(OTHER, VT220);
887035a5 3444 term->curr_attr &= ~ATTR_BLINK;
32874aea 3445 break;
3446 case 27: /* disable reverse video */
3447 compatibility2(OTHER, VT220);
887035a5 3448 term->curr_attr &= ~ATTR_REVERSE;
32874aea 3449 break;
3450 case 30:
3451 case 31:
3452 case 32:
3453 case 33:
3454 case 34:
3455 case 35:
3456 case 36:
3457 case 37:
3458 /* foreground */
887035a5 3459 term->curr_attr &= ~ATTR_FGMASK;
3460 term->curr_attr |=
3461 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
32874aea 3462 break;
37ca32ed 3463 case 90:
3464 case 91:
3465 case 92:
3466 case 93:
3467 case 94:
3468 case 95:
3469 case 96:
3470 case 97:
3471 /* xterm-style bright foreground */
3472 term->curr_attr &= ~ATTR_FGMASK;
3473 term->curr_attr |=
3474 ((term->esc_args[i] - 90 + 16)
3475 << ATTR_FGSHIFT);
3476 break;
32874aea 3477 case 39: /* default-foreground */
887035a5 3478 term->curr_attr &= ~ATTR_FGMASK;
3479 term->curr_attr |= ATTR_DEFFG;
32874aea 3480 break;
3481 case 40:
3482 case 41:
3483 case 42:
3484 case 43:
3485 case 44:
3486 case 45:
3487 case 46:
3488 case 47:
3489 /* background */
887035a5 3490 term->curr_attr &= ~ATTR_BGMASK;
3491 term->curr_attr |=
3492 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
32874aea 3493 break;
37ca32ed 3494 case 100:
3495 case 101:
3496 case 102:
3497 case 103:
3498 case 104:
3499 case 105:
3500 case 106:
3501 case 107:
3502 /* xterm-style bright background */
3503 term->curr_attr &= ~ATTR_BGMASK;
3504 term->curr_attr |=
3505 ((term->esc_args[i] - 100 + 16)
3506 << ATTR_BGSHIFT);
3507 break;
32874aea 3508 case 49: /* default-background */
887035a5 3509 term->curr_attr &= ~ATTR_BGMASK;
3510 term->curr_attr |= ATTR_DEFBG;
32874aea 3511 break;
3512 }
3513 }
36566009 3514 set_erase_char(term);
32874aea 3515 }
3516 break;
3517 case 's': /* save cursor */
887035a5 3518 save_cursor(term, TRUE);
32874aea 3519 break;
3520 case 'u': /* restore cursor */
887035a5 3521 save_cursor(term, FALSE);
39934deb 3522 seen_disp_event(term);
32874aea 3523 break;
a263e4f6 3524 case 't': /* DECSLPP: set page size - ie window height */
374330e2 3525 /*
32874aea 3526 * VT340/VT420 sequence DECSLPP, DEC only allows values
3527 * 24/25/36/48/72/144 other emulators (eg dtterm) use
3528 * illegal values (eg first arg 1..9) for window changing
3529 * and reports.
3530 */
887035a5 3531 if (term->esc_nargs <= 1
3532 && (term->esc_args[0] < 1 ||
3533 term->esc_args[0] >= 24)) {
68f9b3d9 3534 compatibility(VT340TEXT);
64734920 3535 if (!term->cfg.no_remote_resize)
a8327734 3536 request_resize(term->frontend, term->cols,
887035a5 3537 def(term->esc_args[0], 24));
3538 deselect(term);
3539 } else if (term->esc_nargs >= 1 &&
3540 term->esc_args[0] >= 1 &&
3541 term->esc_args[0] < 24) {
68f9b3d9 3542 compatibility(OTHER);
3543
887035a5 3544 switch (term->esc_args[0]) {
68f9b3d9 3545 int x, y, len;
3546 char buf[80], *p;
3547 case 1:
a8327734 3548 set_iconic(term->frontend, FALSE);
68f9b3d9 3549 break;
3550 case 2:
a8327734 3551 set_iconic(term->frontend, TRUE);
68f9b3d9 3552 break;
3553 case 3:
887035a5 3554 if (term->esc_nargs >= 3) {
64734920 3555 if (!term->cfg.no_remote_resize)
a8327734 3556 move_window(term->frontend,
3557 def(term->esc_args[1], 0),
887035a5 3558 def(term->esc_args[2], 0));
68f9b3d9 3559 }
3560 break;
3561 case 4:
3562 /* We should resize the window to a given
3563 * size in pixels here, but currently our
3564 * resizing code isn't healthy enough to
3565 * manage it. */
3566 break;
3567 case 5:
a8327734 3568 /* move to top */
3569 set_zorder(term->frontend, TRUE);
68f9b3d9 3570 break;
3571 case 6:
a8327734 3572 /* move to bottom */
3573 set_zorder(term->frontend, FALSE);
68f9b3d9 3574 break;
3575 case 7:
a8327734 3576 refresh_window(term->frontend);
68f9b3d9 3577 break;
3578 case 8:
887035a5 3579 if (term->esc_nargs >= 3) {
64734920 3580 if (!term->cfg.no_remote_resize)
a8327734 3581 request_resize(term->frontend,
64734920 3582 def(term->esc_args[2], term->cfg.width),
3583 def(term->esc_args[1], term->cfg.height));
68f9b3d9 3584 }
3585 break;
3586 case 9:
887035a5 3587 if (term->esc_nargs >= 2)
a8327734 3588 set_zoomed(term->frontend,
3589 term->esc_args[1] ?
887035a5 3590 TRUE : FALSE);
68f9b3d9 3591 break;
3592 case 11:
b9d7bcad 3593 if (term->ldisc)
3594 ldisc_send(term->ldisc,
a8327734 3595 is_iconic(term->frontend) ?
3596 "\033[1t" : "\033[2t", 4, 0);
68f9b3d9 3597 break;
3598 case 13:
b9d7bcad 3599 if (term->ldisc) {
a8327734 3600 get_window_pos(term->frontend, &x, &y);
b9d7bcad 3601 len = sprintf(buf, "\033[3;%d;%dt", x, y);
3602 ldisc_send(term->ldisc, buf, len, 0);
3603 }
68f9b3d9 3604 break;
3605 case 14:
b9d7bcad 3606 if (term->ldisc) {
a8327734 3607 get_window_pixels(term->frontend, &x, &y);
b9d7bcad 3608 len = sprintf(buf, "\033[4;%d;%dt", x, y);
3609 ldisc_send(term->ldisc, buf, len, 0);
3610 }
68f9b3d9 3611 break;
3612 case 18:
b9d7bcad 3613 if (term->ldisc) {
3614 len = sprintf(buf, "\033[8;%d;%dt",
3615 term->rows, term->cols);
3616 ldisc_send(term->ldisc, buf, len, 0);
3617 }
68f9b3d9 3618 break;
3619 case 19:
3620 /*
3621 * Hmmm. Strictly speaking we
3622 * should return `the size of the
3623 * screen in characters', but
3624 * that's not easy: (a) window
3625 * furniture being what it is it's
3626 * hard to compute, and (b) in
3627 * resize-font mode maximising the
3628 * window wouldn't change the
3629 * number of characters. *shrug*. I
3630 * think we'll ignore it for the
3631 * moment and see if anyone
3632 * complains, and then ask them
3633 * what they would like it to do.
3634 */
3635 break;
3636 case 20:
7fcdebd3 3637 if (term->ldisc &&
3638 !term->cfg.no_remote_qtitle) {
a8327734 3639 p = get_window_title(term->frontend, TRUE);
b9d7bcad 3640 len = strlen(p);
3641 ldisc_send(term->ldisc, "\033]L", 3, 0);
3642 ldisc_send(term->ldisc, p, len, 0);
3643 ldisc_send(term->ldisc, "\033\\", 2, 0);
3644 }
68f9b3d9 3645 break;
3646 case 21:
7fcdebd3 3647 if (term->ldisc &&
3648 !term->cfg.no_remote_qtitle) {
a8327734 3649 p = get_window_title(term->frontend,FALSE);
b9d7bcad 3650 len = strlen(p);
3651 ldisc_send(term->ldisc, "\033]l", 3, 0);
3652 ldisc_send(term->ldisc, p, len, 0);
3653 ldisc_send(term->ldisc, "\033\\", 2, 0);
3654 }
68f9b3d9 3655 break;
3656 }
32874aea 3657 }
3658 break;
a263e4f6 3659 case 'S': /* SU: Scroll up */
1a837633 3660 compatibility(SCOANSI);
887035a5 3661 scroll(term, term->marg_t, term->marg_b,
3662 def(term->esc_args[0], 1), TRUE);
887035a5 3663 term->wrapnext = FALSE;
39934deb 3664 seen_disp_event(term);
1a837633 3665 break;
a263e4f6 3666 case 'T': /* SD: Scroll down */
1a837633 3667 compatibility(SCOANSI);
887035a5 3668 scroll(term, term->marg_t, term->marg_b,
3669 -def(term->esc_args[0], 1), TRUE);
887035a5 3670 term->wrapnext = FALSE;
39934deb 3671 seen_disp_event(term);
1a837633 3672 break;
a263e4f6 3673 case ANSI('|', '*'): /* DECSNLS */
3674 /*
32874aea 3675 * Set number of lines on screen
a263e4f6 3676 * VT420 uses VGA like hardware and can
3677 * support any size in reasonable range
3678 * (24..49 AIUI) with no default specified.
32874aea 3679 */
3680 compatibility(VT420);
887035a5 3681 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
64734920 3682 if (!term->cfg.no_remote_resize)
a8327734 3683 request_resize(term->frontend, term->cols,
887035a5 3684 def(term->esc_args[0],
64734920 3685 term->cfg.height));
887035a5 3686 deselect(term);
32874aea 3687 }
3688 break;
a263e4f6 3689 case ANSI('|', '$'): /* DECSCPP */
3690 /*
32874aea 3691 * Set number of columns per page
a263e4f6 3692 * Docs imply range is only 80 or 132, but
3693 * I'll allow any.
32874aea 3694 */
3695 compatibility(VT340TEXT);
887035a5 3696 if (term->esc_nargs <= 1) {
64734920 3697 if (!term->cfg.no_remote_resize)
a8327734 3698 request_resize(term->frontend,
3699 def(term->esc_args[0],
64734920 3700 term->cfg.width), term->rows);
887035a5 3701 deselect(term);
32874aea 3702 }
3703 break;
a263e4f6 3704 case 'X': /* ECH: write N spaces w/o moving cursor */
3705 /* XXX VTTEST says this is vt220, vt510 manual
3706 * says vt100 */
32874aea 3707 compatibility(ANSIMIN);
3708 {
887035a5 3709 int n = def(term->esc_args[0], 1);
32874aea 3710 pos cursplus;
c6958dfe 3711 int p = term->curs.x;
3712 termline *cline = scrlineptr(term->curs.y);
3713
887035a5 3714 if (n > term->cols - term->curs.x)
3715 n = term->cols - term->curs.x;
3716 cursplus = term->curs;
32874aea 3717 cursplus.x += n;
3c7366f8 3718 check_boundary(term, term->curs.x, term->curs.y);
3719 check_boundary(term, term->curs.x+n, term->curs.y);
887035a5 3720 check_selection(term, term->curs, cursplus);
32874aea 3721 while (n--)
c6958dfe 3722 copy_termchar(cline, p++,
3723 &term->erase_char);
39934deb 3724 seen_disp_event(term);
32874aea 3725 }
3726 break;
a263e4f6 3727 case 'x': /* DECREQTPARM: report terminal characteristics */
32874aea 3728 compatibility(VT100);
b9d7bcad 3729 if (term->ldisc) {
32874aea 3730 char buf[32];
887035a5 3731 int i = def(term->esc_args[0], 0);
32874aea 3732 if (i == 0 || i == 1) {
3733 strcpy(buf, "\033[2;1;1;112;112;1;0x");
3734 buf[2] += i;
b9d7bcad 3735 ldisc_send(term->ldisc, buf, 20, 0);
32874aea 3736 }
3737 }
3738 break;
a263e4f6 3739 case 'Z': /* CBT: BackTab for xterm */
979f6987 3740 compatibility(OTHER);
3741 {
887035a5 3742 int i = def(term->esc_args[0], 1);
3743 pos old_curs = term->curs;
979f6987 3744
887035a5 3745 for(;i>0 && term->curs.x>0; i--) {
979f6987 3746 do {
887035a5 3747 term->curs.x--;
3748 } while (term->curs.x >0 &&
3749 !term->tabs[term->curs.x]);
979f6987 3750 }
887035a5 3751 check_selection(term, old_curs, term->curs);
979f6987 3752 }
3753 break;
1b20b972 3754 case ANSI('c', '='): /* Hide or Show Cursor */
3755 compatibility(SCOANSI);
3756 switch(term->esc_args[0]) {
3757 case 0: /* hide cursor */
3758 term->cursor_on = FALSE;
3759 break;
3760 case 1: /* restore cursor */
3761 term->big_cursor = FALSE;
3762 term->cursor_on = TRUE;
3763 break;
3764 case 2: /* block cursor */
3765 term->big_cursor = TRUE;
3766 term->cursor_on = TRUE;
3767 break;
3768 }
3769 break;
3770 case ANSI('C', '='):
3771 /*
3772 * set cursor start on scanline esc_args[0] and
3773 * end on scanline esc_args[1].If you set
3774 * the bottom scan line to a value less than
3775 * the top scan line, the cursor will disappear.
3776 */
3777 compatibility(SCOANSI);
3778 if (term->esc_nargs >= 2) {
3779 if (term->esc_args[0] > term->esc_args[1])
3780 term->cursor_on = FALSE;
3781 else
3782 term->cursor_on = TRUE;
3783 }
3784 break;
3785 case ANSI('D', '='):
3786 compatibility(SCOANSI);
3787 term->blink_is_real = FALSE;
39934deb 3788 term_schedule_tblink(term);
1b20b972 3789 if (term->esc_args[0]>=1)
3790 term->curr_attr |= ATTR_BLINK;
3791 else
3792 term->curr_attr &= ~ATTR_BLINK;
3793 break;
3794 case ANSI('E', '='):
3795 compatibility(SCOANSI);
3796 term->blink_is_real = (term->esc_args[0] >= 1);
39934deb 3797 term_schedule_tblink(term);
1b20b972 3798 break;
3799 case ANSI('F', '='): /* set normal foreground */
3800 compatibility(SCOANSI);
3801 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
69761fd5 3802 long colour =
3803 (sco2ansicolour[term->esc_args[0] & 0x7] |
1b20b972 3804 ((term->esc_args[0] & 0x8) << 1)) <<
3805 ATTR_FGSHIFT;
69761fd5 3806 term->curr_attr &= ~ATTR_FGMASK;
3807 term->curr_attr |= colour;
3808 term->default_attr &= ~ATTR_FGMASK;
3809 term->default_attr |= colour;
1b20b972 3810 }
3811 break;
3812 case ANSI('G', '='): /* set normal background */
3813 compatibility(SCOANSI);
3814 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
69761fd5 3815 long colour =
3816 (sco2ansicolour[term->esc_args[0] & 0x7] |
1b20b972 3817 ((term->esc_args[0] & 0x8) << 1)) <<
3818 ATTR_BGSHIFT;
69761fd5 3819 term->curr_attr &= ~ATTR_BGMASK;
3820 term->curr_attr |= colour;
3821 term->default_attr &= ~ATTR_BGMASK;
3822 term->default_attr |= colour;
1b20b972 3823 }
3824 break;
32874aea 3825 case ANSI('L', '='):
1b20b972 3826 compatibility(SCOANSI);
887035a5 3827 term->use_bce = (term->esc_args[0] <= 0);
36566009 3828 set_erase_char(term);
32874aea 3829 break;
a263e4f6 3830 case ANSI('p', '"'): /* DECSCL: set compat level */
887035a5 3831 /*
3832 * Allow the host to make this emulator a
3833 * 'perfect' VT102. This first appeared in
3834 * the VT220, but we do need to get back to
3835 * PuTTY mode so I won't check it.
e14a5a13 3836 *
4eeb7d09 3837 * The arg in 40..42,50 are a PuTTY extension.
32874aea 3838 * The 2nd arg, 8bit vs 7bit is not checked.
3839 *
887035a5 3840 * Setting VT102 mode should also change
3841 * the Fkeys to generate PF* codes as a
3842 * real VT102 has no Fkeys. The VT220 does
3843 * this, F11..F13 become ESC,BS,LF other
3844 * Fkeys send nothing.
32874aea 3845 *
3846 * Note ESC c will NOT change this!
374330e2 3847 */
32874aea 3848
887035a5 3849 switch (term->esc_args[0]) {
32874aea 3850 case 61:
887035a5 3851 term->compatibility_level &= ~TM_VTXXX;
3852 term->compatibility_level |= TM_VT102;
374330e2 3853 break;
32874aea 3854 case 62:
887035a5 3855 term->compatibility_level &= ~TM_VTXXX;
3856 term->compatibility_level |= TM_VT220;
32874aea 3857 break;
3858
3859 default:
887035a5 3860 if (term->esc_args[0] > 60 &&
3861 term->esc_args[0] < 70)
3862 term->compatibility_level |= TM_VTXXX;
32874aea 3863 break;
3864
3865 case 40:
887035a5 3866 term->compatibility_level &= TM_VTXXX;
374330e2 3867 break;
32874aea 3868 case 41:
887035a5 3869 term->compatibility_level = TM_PUTTY;
374330e2 3870 break;
32874aea 3871 case 42:
887035a5 3872 term->compatibility_level = TM_SCOANSI;
374330e2 3873 break;
32874aea 3874
3875 case ARG_DEFAULT:
887035a5 3876 term->compatibility_level = TM_PUTTY;
32874aea 3877 break;
3878 case 50:
3879 break;
3880 }
3881
3882 /* Change the response to CSI c */
887035a5 3883 if (term->esc_args[0] == 50) {
32874aea 3884 int i;
3885 char lbuf[64];
887035a5 3886 strcpy(term->id_string, "\033[?");
3887 for (i = 1; i < term->esc_nargs; i++) {
32874aea 3888 if (i != 1)
887035a5 3889 strcat(term->id_string, ";");
3890 sprintf(lbuf, "%d", term->esc_args[i]);
3891 strcat(term->id_string, lbuf);
32874aea 3892 }
887035a5 3893 strcat(term->id_string, "c");
32874aea 3894 }
3895#if 0
3896 /* Is this a good idea ?
3897 * Well we should do a soft reset at this point ...
3898 */
3899 if (!has_compat(VT420) && has_compat(VT100)) {
64734920 3900 if (!term->cfg.no_remote_resize) {
887035a5 3901 if (term->reset_132)
0d2086c5 3902 request_resize(132, 24);
3903 else
3904 request_resize(80, 24);
3905 }
374330e2 3906 }
32874aea 3907#endif
3908 break;
374330e2 3909 }
374330e2 3910 break;
32874aea 3911 case SEEN_OSC:
887035a5 3912 term->osc_w = FALSE;
32874aea 3913 switch (c) {
3914 case 'P': /* Linux palette sequence */
887035a5 3915 term->termstate = SEEN_OSC_P;
3916 term->osc_strlen = 0;
32874aea 3917 break;
3918 case 'R': /* Linux palette reset */
a8327734 3919 palette_reset(term->frontend);
887035a5 3920 term_invalidate(term);
3921 term->termstate = TOPLEVEL;
32874aea 3922 break;
3923 case 'W': /* word-set */
887035a5 3924 term->termstate = SEEN_OSC_W;
3925 term->osc_w = TRUE;
32874aea 3926 break;
3927 case '0':
3928 case '1':
3929 case '2':
3930 case '3':
3931 case '4':
3932 case '5':
3933 case '6':
3934 case '7':
3935 case '8':
3936 case '9':
887035a5 3937 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
32874aea 3938 break;
3939 case 'L':
3940 /*
3941 * Grotty hack to support xterm and DECterm title
3942 * sequences concurrently.
3943 */
887035a5 3944 if (term->esc_args[0] == 2) {
3945 term->esc_args[0] = 1;
32874aea 3946 break;
3947 }
3948 /* else fall through */
3949 default:
887035a5 3950 term->termstate = OSC_STRING;
3951 term->osc_strlen = 0;
e14a5a13 3952 }
3953 break;
32874aea 3954 case OSC_STRING:
3955 /*
3956 * This OSC stuff is EVIL. It takes just one character to get into
3957 * sysline mode and it's not initially obvious how to get out.
3958 * So I've added CR and LF as string aborts.
3959 * This shouldn't effect compatibility as I believe embedded
3960 * control characters are supposed to be interpreted (maybe?)
3961 * and they don't display anything useful anyway.
3962 *
3963 * -- RDB
e14a5a13 3964 */
92e23ad9 3965 if (c == '\012' || c == '\015') {
887035a5 3966 term->termstate = TOPLEVEL;
32874aea 3967 } else if (c == 0234 || c == '\007') {
3968 /*
3969 * These characters terminate the string; ST and BEL
3970 * terminate the sequence and trigger instant
3971 * processing of it, whereas ESC goes back to SEEN_ESC
3972 * mode unless it is followed by \, in which case it is
3973 * synonymous with ST in the first place.
3974 */
887035a5 3975 do_osc(term);
3976 term->termstate = TOPLEVEL;
32874aea 3977 } else if (c == '\033')
887035a5 3978 term->termstate = OSC_MAYBE_ST;
3979 else if (term->osc_strlen < OSC_STR_MAX)
36566009 3980 term->osc_string[term->osc_strlen++] = (char)c;
374330e2 3981 break;
32874aea 3982 case SEEN_OSC_P:
374330e2 3983 {
887035a5 3984 int max = (term->osc_strlen == 0 ? 21 : 16);
32874aea 3985 int val;
36566009 3986 if ((int)c >= '0' && (int)c <= '9')
32874aea 3987 val = c - '0';
36566009 3988 else if ((int)c >= 'A' && (int)c <= 'A' + max - 10)
32874aea 3989 val = c - 'A' + 10;
36566009 3990 else if ((int)c >= 'a' && (int)c <= 'a' + max - 10)
32874aea 3991 val = c - 'a' + 10;
2d466ffd 3992 else {
887035a5 3993 term->termstate = TOPLEVEL;
2d466ffd 3994 break;
3995 }
887035a5 3996 term->osc_string[term->osc_strlen++] = val;
3997 if (term->osc_strlen >= 7) {
a8327734 3998 palette_set(term->frontend, term->osc_string[0],
887035a5 3999 term->osc_string[1] * 16 + term->osc_string[2],
4000 term->osc_string[3] * 16 + term->osc_string[4],
4001 term->osc_string[5] * 16 + term->osc_string[6]);
4002 term_invalidate(term);
4003 term->termstate = TOPLEVEL;
374330e2 4004 }
4005 }
4006 break;
32874aea 4007 case SEEN_OSC_W:
4008 switch (c) {
4009 case '0':
4010 case '1':
4011 case '2':
4012 case '3':
4013 case '4':
4014 case '5':
4015 case '6':
4016 case '7':
4017 case '8':
4018 case '9':
887035a5 4019 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
32874aea 4020 break;
4021 default:
887035a5 4022 term->termstate = OSC_STRING;
4023 term->osc_strlen = 0;
ec55b220 4024 }
32874aea 4025 break;
32874aea 4026 case VT52_ESC:
887035a5 4027 term->termstate = TOPLEVEL;
39934deb 4028 seen_disp_event(term);
c9def1b8 4029 switch (c) {
32874aea 4030 case 'A':
887035a5 4031 move(term, term->curs.x, term->curs.y - 1, 1);
32874aea 4032 break;
4033 case 'B':
887035a5 4034 move(term, term->curs.x, term->curs.y + 1, 1);
32874aea 4035 break;
4036 case 'C':
887035a5 4037 move(term, term->curs.x + 1, term->curs.y, 1);
32874aea 4038 break;
4039 case 'D':
887035a5 4040 move(term, term->curs.x - 1, term->curs.y, 1);
32874aea 4041 break;
4eeb7d09 4042 /*
4043 * From the VT100 Manual
4044 * NOTE: The special graphics characters in the VT100
4045 * are different from those in the VT52
4046 *
4047 * From VT102 manual:
4048 * 137 _ Blank - Same
4049 * 140 ` Reserved - Humm.
4050 * 141 a Solid rectangle - Similar
4051 * 142 b 1/ - Top half of fraction for the
4052 * 143 c 3/ - subscript numbers below.
4053 * 144 d 5/
4054 * 145 e 7/
4055 * 146 f Degrees - Same
4056 * 147 g Plus or minus - Same
4057 * 150 h Right arrow
4058 * 151 i Ellipsis (dots)
4059 * 152 j Divide by
4060 * 153 k Down arrow
4061 * 154 l Bar at scan 0
4062 * 155 m Bar at scan 1
4063 * 156 n Bar at scan 2
4064 * 157 o Bar at scan 3 - Similar
4065 * 160 p Bar at scan 4 - Similar
4066 * 161 q Bar at scan 5 - Similar
4067 * 162 r Bar at scan 6 - Same
4068 * 163 s Bar at scan 7 - Similar
4069 * 164 t Subscript 0
4070 * 165 u Subscript 1
4071 * 166 v Subscript 2
4072 * 167 w Subscript 3
4073 * 170 x Subscript 4
4074 * 171 y Subscript 5
4075 * 172 z Subscript 6
4076 * 173 { Subscript 7
4077 * 174 | Subscript 8
4078 * 175 } Subscript 9
4079 * 176 ~ Paragraph
4080 *
4081 */
32874aea 4082 case 'F':
36566009 4083 term->cset_attr[term->cset = 0] = CSET_LINEDRW;
32874aea 4084 break;
4085 case 'G':
36566009 4086 term->cset_attr[term->cset = 0] = CSET_ASCII;
32874aea 4087 break;
4088 case 'H':
887035a5 4089 move(term, 0, 0, 0);
32874aea 4090 break;
4091 case 'I':
887035a5 4092 if (term->curs.y == 0)
4093 scroll(term, 0, term->rows - 1, -1, TRUE);
4094 else if (term->curs.y > 0)
4095 term->curs.y--;
887035a5 4096 term->wrapnext = FALSE;
32874aea 4097 break;
4098 case 'J':
887035a5 4099 erase_lots(term, FALSE, FALSE, TRUE);
4100 term->disptop = 0;
c9def1b8 4101 break;
32874aea 4102 case 'K':
887035a5 4103 erase_lots(term, TRUE, FALSE, TRUE);
32874aea 4104 break;
4eeb7d09 4105#if 0
32874aea 4106 case 'V':
4107 /* XXX Print cursor line */
4108 break;
4109 case 'W':
4110 /* XXX Start controller mode */
4111 break;
4112 case 'X':
4113 /* XXX Stop controller mode */
4114 break;
4eeb7d09 4115#endif
32874aea 4116 case 'Y':
887035a5 4117 term->termstate = VT52_Y1;
32874aea 4118 break;
4119 case 'Z':
b9d7bcad 4120 if (term->ldisc)
4121 ldisc_send(term->ldisc, "\033/Z", 3, 0);
32874aea 4122 break;
4123 case '=':
887035a5 4124 term->app_keypad_keys = TRUE;
32874aea 4125 break;
4126 case '>':
887035a5 4127 term->app_keypad_keys = FALSE;
32874aea 4128 break;
4129 case '<':
4130 /* XXX This should switch to VT100 mode not current or default
4131 * VT mode. But this will only have effect in a VT220+
4132 * emulation.
4133 */
887035a5 4134 term->vt52_mode = FALSE;
64734920 4135 term->blink_is_real = term->cfg.blinktext;
39934deb 4136 term_schedule_tblink(term);
32874aea 4137 break;
4eeb7d09 4138#if 0
32874aea 4139 case '^':
4140 /* XXX Enter auto print mode */
4141 break;
4142 case '_':
4143 /* XXX Exit auto print mode */
4144 break;
4145 case ']':
4146 /* XXX Print screen */
4147 break;
4eeb7d09 4148#endif
4149
4150#ifdef VT52_PLUS
4151 case 'E':
4152 /* compatibility(ATARI) */
887035a5 4153 move(term, 0, 0, 0);
4154 erase_lots(term, FALSE, FALSE, TRUE);
4155 term->disptop = 0;
4eeb7d09 4156 break;
4157 case 'L':
4158 /* compatibility(ATARI) */
887035a5 4159 if (term->curs.y <= term->marg_b)
4160 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
4eeb7d09 4161 break;
4162 case 'M':
4163 /* compatibility(ATARI) */
887035a5 4164 if (term->curs.y <= term->marg_b)
4165 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
4eeb7d09 4166 break;
4167 case 'b':
4168 /* compatibility(ATARI) */
887035a5 4169 term->termstate = VT52_FG;
4eeb7d09 4170 break;
4171 case 'c':
4172 /* compatibility(ATARI) */
887035a5 4173 term->termstate = VT52_BG;
4eeb7d09 4174 break;
4175 case 'd':
4176 /* compatibility(ATARI) */
887035a5 4177 erase_lots(term, FALSE, TRUE, FALSE);
4178 term->disptop = 0;
4eeb7d09 4179 break;
4180 case 'e':
4181 /* compatibility(ATARI) */
887035a5 4182 term->cursor_on = TRUE;
4eeb7d09 4183 break;
4184 case 'f':
4185 /* compatibility(ATARI) */
887035a5 4186 term->cursor_on = FALSE;
4eeb7d09 4187 break;
4188 /* case 'j': Save cursor position - broken on ST */
4189 /* case 'k': Restore cursor position */
4190 case 'l':
4191 /* compatibility(ATARI) */
887035a5 4192 erase_lots(term, TRUE, TRUE, TRUE);
4193 term->curs.x = 0;
4194 term->wrapnext = FALSE;
4eeb7d09 4195 break;
4196 case 'o':
4197 /* compatibility(ATARI) */
887035a5 4198 erase_lots(term, TRUE, TRUE, FALSE);
4eeb7d09 4199 break;
4200 case 'p':
4201 /* compatibility(ATARI) */
887035a5 4202 term->curr_attr |= ATTR_REVERSE;
4eeb7d09 4203 break;
4204 case 'q':
4205 /* compatibility(ATARI) */
887035a5 4206 term->curr_attr &= ~ATTR_REVERSE;
4eeb7d09 4207 break;
4208 case 'v': /* wrap Autowrap on - Wyse style */
4209 /* compatibility(ATARI) */
887035a5 4210 term->wrap = 1;
4eeb7d09 4211 break;
4212 case 'w': /* Autowrap off */
4213 /* compatibility(ATARI) */
887035a5 4214 term->wrap = 0;
4eeb7d09 4215 break;
4216
4217 case 'R':
4218 /* compatibility(OTHER) */
887035a5 4219 term->vt52_bold = FALSE;
4220 term->curr_attr = ATTR_DEFAULT;
36566009 4221 set_erase_char(term);
4eeb7d09 4222 break;
4223 case 'S':
4224 /* compatibility(VI50) */
887035a5 4225 term->curr_attr |= ATTR_UNDER;
4eeb7d09 4226 break;
4227 case 'W':
4228 /* compatibility(VI50) */
887035a5 4229 term->curr_attr &= ~ATTR_UNDER;
4eeb7d09 4230 break;
4231 case 'U':
4232 /* compatibility(VI50) */
887035a5 4233 term->vt52_bold = TRUE;
4234 term->curr_attr |= ATTR_BOLD;
4eeb7d09 4235 break;
4236 case 'T':
4237 /* compatibility(VI50) */
887035a5 4238 term->vt52_bold = FALSE;
4239 term->curr_attr &= ~ATTR_BOLD;
4eeb7d09 4240 break;
4241#endif
c9def1b8 4242 }
e14a5a13 4243 break;
32874aea 4244 case VT52_Y1:
887035a5 4245 term->termstate = VT52_Y2;
4246 move(term, term->curs.x, c - ' ', 0);
e14a5a13 4247 break;
32874aea 4248 case VT52_Y2:
887035a5 4249 term->termstate = TOPLEVEL;
4250 move(term, c - ' ', term->curs.y, 0);
e14a5a13 4251 break;
4eeb7d09 4252
4253#ifdef VT52_PLUS
4254 case VT52_FG:
887035a5 4255 term->termstate = TOPLEVEL;
4256 term->curr_attr &= ~ATTR_FGMASK;
4257 term->curr_attr &= ~ATTR_BOLD;
4258 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
4259 if ((c & 0x8) || term->vt52_bold)
4260 term->curr_attr |= ATTR_BOLD;
4261
36566009 4262 set_erase_char(term);
4eeb7d09 4263 break;
4264 case VT52_BG:
887035a5 4265 term->termstate = TOPLEVEL;
4266 term->curr_attr &= ~ATTR_BGMASK;
4267 term->curr_attr &= ~ATTR_BLINK;
4268 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
4eeb7d09 4269
4270 /* Note: bold background */
4271 if (c & 0x8)
887035a5 4272 term->curr_attr |= ATTR_BLINK;
4eeb7d09 4273
36566009 4274 set_erase_char(term);
4eeb7d09 4275 break;
4276#endif
2d466ffd 4277 default: break; /* placate gcc warning about enum use */
e14a5a13 4278 }
887035a5 4279 if (term->selstate != NO_SELECTION) {
4280 pos cursplus = term->curs;
4facdf84 4281 incpos(cursplus);
887035a5 4282 check_selection(term, term->curs, cursplus);
4facdf84 4283 }
374330e2 4284 }
b44b307a 4285
887035a5 4286 term_print_flush(term);
11cc5e30 4287 logflush(term->logctx);
374330e2 4288}
4289
374330e2 4290/*
f0fccd51 4291 * To prevent having to run the reasonably tricky bidi algorithm
4292 * too many times, we maintain a cache of the last lineful of data
4293 * fed to the algorithm on each line of the display.
4294 */
4295static int term_bidi_cache_hit(Terminal *term, int line,
36566009 4296 termchar *lbefore, int width)
f0fccd51 4297{
c6958dfe 4298 int i;
4299
f0fccd51 4300 if (!term->pre_bidi_cache)
4301 return FALSE; /* cache doesn't even exist yet! */
4302
4303 if (line >= term->bidi_cache_size)
4304 return FALSE; /* cache doesn't have this many lines */
4305
2caf3cd8 4306 if (!term->pre_bidi_cache[line].chars)
f0fccd51 4307 return FALSE; /* cache doesn't contain _this_ line */
4308
2caf3cd8 4309 if (term->pre_bidi_cache[line].width != width)
4310 return FALSE; /* line is wrong width */
4311
c6958dfe 4312 for (i = 0; i < width; i++)
2caf3cd8 4313 if (!termchars_equal(term->pre_bidi_cache[line].chars+i, lbefore+i))
c6958dfe 4314 return FALSE; /* line doesn't match cache */
f0fccd51 4315
c6958dfe 4316 return TRUE; /* it didn't match. */
f0fccd51 4317}
4318
36566009 4319static void term_bidi_cache_store(Terminal *term, int line, termchar *lbefore,
3c5a620c 4320 termchar *lafter, bidi_char *wcTo,
4321 int width)
f0fccd51 4322{
3c5a620c 4323 int i;
4324
f0fccd51 4325 if (!term->pre_bidi_cache || term->bidi_cache_size <= line) {
4326 int j = term->bidi_cache_size;
4327 term->bidi_cache_size = line+1;
4328 term->pre_bidi_cache = sresize(term->pre_bidi_cache,
4329 term->bidi_cache_size,
2caf3cd8 4330 struct bidi_cache_entry);
f0fccd51 4331 term->post_bidi_cache = sresize(term->post_bidi_cache,
4332 term->bidi_cache_size,
2caf3cd8 4333 struct bidi_cache_entry);
f0fccd51 4334 while (j < term->bidi_cache_size) {
2caf3cd8 4335 term->pre_bidi_cache[j].chars =
4336 term->post_bidi_cache[j].chars = NULL;
4337 term->pre_bidi_cache[j].width =
4338 term->post_bidi_cache[j].width = -1;
f0fccd51 4339 j++;
4340 }
4341 }
4342
2caf3cd8 4343 sfree(term->pre_bidi_cache[line].chars);
4344 sfree(term->post_bidi_cache[line].chars);
f0fccd51 4345
2caf3cd8 4346 term->pre_bidi_cache[line].width = width;
4347 term->pre_bidi_cache[line].chars = snewn(width, termchar);
4348 term->post_bidi_cache[line].width = width;
4349 term->post_bidi_cache[line].chars = snewn(width, termchar);
3c5a620c 4350 term->post_bidi_cache[line].forward = snewn(width, int);
4351 term->post_bidi_cache[line].backward = snewn(width, int);
f0fccd51 4352
2caf3cd8 4353 memcpy(term->pre_bidi_cache[line].chars, lbefore, width * TSIZE);
4354 memcpy(term->post_bidi_cache[line].chars, lafter, width * TSIZE);
3c5a620c 4355 memset(term->post_bidi_cache[line].forward, 0, width * sizeof(int));
4356 memset(term->post_bidi_cache[line].backward, 0, width * sizeof(int));
4357
4358 for (i = 0; i < width; i++) {
4359 int p = wcTo[i].index;
4360
4361 assert(0 <= p && p < width);
4362
4363 term->post_bidi_cache[line].backward[i] = p;
4364 term->post_bidi_cache[line].forward[p] = i;
4365 }
4366}
4367
4368/*
4369 * Prepare the bidi information for a screen line. Returns the
4370 * transformed list of termchars, or NULL if no transformation at
4371 * all took place (because bidi is disabled). If return was
4372 * non-NULL, auxiliary information such as the forward and reverse
4373 * mappings of permutation position are available in
4374 * term->post_bidi_cache[scr_y].*.
4375 */
4376static termchar *term_bidi_line(Terminal *term, struct termline *ldata,
4377 int scr_y)
4378{
4379 termchar *lchars;
4380 int it;
4381
4382 /* Do Arabic shaping and bidi. */
4383 if(!term->cfg.bidi || !term->cfg.arabicshaping) {
4384
4385 if (!term_bidi_cache_hit(term, scr_y, ldata->chars, term->cols)) {
4386
4387 if (term->wcFromTo_size < term->cols) {
4388 term->wcFromTo_size = term->cols;
4389 term->wcFrom = sresize(term->wcFrom, term->wcFromTo_size,
4390 bidi_char);
4391 term->wcTo = sresize(term->wcTo, term->wcFromTo_size,
4392 bidi_char);
4393 }
4394
4395 for(it=0; it<term->cols ; it++)
4396 {
4397 unsigned long uc = (ldata->chars[it].chr);
4398
4399 switch (uc & CSET_MASK) {
4400 case CSET_LINEDRW:
4401 if (!term->cfg.rawcnp) {
4402 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4403 break;
4404 }
4405 case CSET_ASCII:
4406 uc = term->ucsdata->unitab_line[uc & 0xFF];
4407 break;
4408 case CSET_SCOACS:
4409 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4410 break;
4411 }
4412 switch (uc & CSET_MASK) {
4413 case CSET_ACP:
4414 uc = term->ucsdata->unitab_font[uc & 0xFF];
4415 break;
4416 case CSET_OEMCP:
4417 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4418 break;
4419 }
4420
4421 term->wcFrom[it].origwc = term->wcFrom[it].wc =
4422 (wchar_t)uc;
4423 term->wcFrom[it].index = it;
4424 }
4425
4426 if(!term->cfg.bidi)
4427 do_bidi(term->wcFrom, term->cols);
4428
4429 /* this is saved iff done from inside the shaping */
4430 if(!term->cfg.bidi && term->cfg.arabicshaping)
4431 for(it=0; it<term->cols; it++)
4432 term->wcTo[it] = term->wcFrom[it];
4433
4434 if(!term->cfg.arabicshaping)
4435 do_shape(term->wcFrom, term->wcTo, term->cols);
4436
4437 if (term->ltemp_size < ldata->size) {
4438 term->ltemp_size = ldata->size;
4439 term->ltemp = sresize(term->ltemp, term->ltemp_size,
4440 termchar);
4441 }
4442
4443 memcpy(term->ltemp, ldata->chars, ldata->size * TSIZE);
4444
4445 for(it=0; it<term->cols ; it++)
4446 {
4447 term->ltemp[it] = ldata->chars[term->wcTo[it].index];
4448 if (term->ltemp[it].cc_next)
4449 term->ltemp[it].cc_next -=
4450 it - term->wcTo[it].index;
4451
4452 if (term->wcTo[it].origwc != term->wcTo[it].wc)
4453 term->ltemp[it].chr = term->wcTo[it].wc;
4454 }
4455 term_bidi_cache_store(term, scr_y, ldata->chars,
4456 term->ltemp, term->wcTo, ldata->size);
4457
4458 lchars = term->ltemp;
4459 } else {
4460 lchars = term->post_bidi_cache[scr_y].chars;
4461 }
4462 } else {
4463 lchars = NULL;
4464 }
4465
4466 return lchars;
f0fccd51 4467}
4468
4469/*
374330e2 4470 * Given a context, update the window. Out of paranoia, we don't
4471 * allow WM_PAINT responses to do scrolling optimisations.
4472 */
887035a5 4473static void do_paint(Terminal *term, Context ctx, int may_optimise)
32874aea 4474{
3c5a620c 4475 int i, j, our_curs_y, our_curs_x;
36566009 4476 int rv, cursor;
4facdf84 4477 pos scrpos;
c6958dfe 4478 wchar_t *ch;
4479 int chlen;
36566009 4480 termchar cursor_background;
341eb978 4481#ifdef OPTIMISE_SCROLL
4482 struct scrollregion *sr;
4483#endif /* OPTIMISE_SCROLL */
374330e2 4484
36566009 4485 cursor_background = term->basic_erase_char;
4486
c6958dfe 4487 chlen = 1024;
4488 ch = snewn(chlen, wchar_t);
4489
887035a5 4490 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
4eeb7d09 4491
156686ef 4492 /* Depends on:
4493 * screen array, disptop, scrtop,
4494 * selection, rv,
4495 * cfg.blinkpc, blink_is_real, tblinker,
39934deb 4496 * curs.y, curs.x, cblinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
156686ef 4497 */
4eeb7d09 4498
4499 /* Has the cursor position or type changed ? */
887035a5 4500 if (term->cursor_on) {
4501 if (term->has_focus) {
39934deb 4502 if (term->cblinker || !term->cfg.blink_cur)
4eeb7d09 4503 cursor = TATTR_ACTCURS;
32874aea 4504 else
4505 cursor = 0;
4506 } else
4eeb7d09 4507 cursor = TATTR_PASCURS;
887035a5 4508 if (term->wrapnext)
4eeb7d09 4509 cursor |= TATTR_RIGHTCURS;
32874aea 4510 } else
156686ef 4511 cursor = 0;
887035a5 4512 our_curs_y = term->curs.y - term->disptop;
3c7366f8 4513 {
4514 /*
3c5a620c 4515 * Adjust the cursor position:
4516 * - for bidi
4517 * - in the case where it's resting on the right-hand half
4518 * of a CJK wide character. xterm's behaviour here,
4519 * which seems adequate to me, is to display the cursor
4520 * covering the _whole_ character, exactly as if it were
4521 * one space to the left.
3c7366f8 4522 */
36566009 4523 termline *ldata = lineptr(term->curs.y);
3c5a620c 4524 termchar *lchars;
4525
3c7366f8 4526 our_curs_x = term->curs.x;
3c5a620c 4527
4528 if ( (lchars = term_bidi_line(term, ldata, our_curs_y)) != NULL) {
4529 our_curs_x = term->post_bidi_cache[our_curs_y].forward[our_curs_x];
4530 } else
4531 lchars = ldata->chars;
4532
3c7366f8 4533 if (our_curs_x > 0 &&
3c5a620c 4534 lchars[our_curs_x].chr == UCSWIDE)
3c7366f8 4535 our_curs_x--;
3c5a620c 4536
36566009 4537 unlineptr(ldata);
3c7366f8 4538 }
887035a5 4539
36566009 4540 /*
4541 * If the cursor is not where it was last time we painted, and
4542 * its previous position is visible on screen, invalidate its
4543 * previous position.
4544 */
4545 if (term->dispcursy >= 0 &&
4546 (term->curstype != cursor ||
4547 term->dispcursy != our_curs_y ||
4548 term->dispcursx != our_curs_x)) {
4549 termchar *dispcurs = term->disptext[term->dispcursy]->chars +
4550 term->dispcursx;
4551
4552 if (term->dispcursx > 0 && dispcurs->chr == UCSWIDE)
4553 dispcurs[-1].attr |= ATTR_INVALID;
4554 if (term->dispcursx < term->cols-1 && dispcurs[1].chr == UCSWIDE)
4555 dispcurs[1].attr |= ATTR_INVALID;
4556 dispcurs->attr |= ATTR_INVALID;
4557
887035a5 4558 term->curstype = 0;
4eeb7d09 4559 }
36566009 4560 term->dispcursx = term->dispcursy = -1;
4eeb7d09 4561
341eb978 4562#ifdef OPTIMISE_SCROLL
4563 /* Do scrolls */
4564 sr = term->scrollhead;
4565 while (sr) {
4566 struct scrollregion *next = sr->next;
4567 do_scroll(ctx, sr->topline, sr->botline, sr->lines);
4568 sfree(sr);
4569 sr = next;
4570 }
4571 term->scrollhead = term->scrolltail = NULL;
4572#endif /* OPTIMISE_SCROLL */
4573
4eeb7d09 4574 /* The normal screen data */
887035a5 4575 for (i = 0; i < term->rows; i++) {
36566009 4576 termline *ldata;
4577 termchar *lchars;
4578 int dirty_line, dirty_run, selected;
4579 unsigned long attr = 0, cset = 0;
4eeb7d09 4580 int updated_line = 0;
4581 int start = 0;
4582 int ccount = 0;
4583 int last_run_dirty = 0;
3c5a620c 4584 int *backward;
4eeb7d09 4585
887035a5 4586 scrpos.y = i + term->disptop;
4facdf84 4587 ldata = lineptr(scrpos.y);
4eeb7d09 4588
36566009 4589 dirty_run = dirty_line = (ldata->lattr !=
4590 term->disptext[i]->lattr);
4591 term->disptext[i]->lattr = ldata->lattr;
4eeb7d09 4592
f0fccd51 4593 /* Do Arabic shaping and bidi. */
3c5a620c 4594 lchars = term_bidi_line(term, ldata, i);
4595 if (lchars) {
4596 backward = term->post_bidi_cache[i].backward;
4597 } else {
36566009 4598 lchars = ldata->chars;
3c5a620c 4599 backward = NULL;
4600 }
f0fccd51 4601
36566009 4602 for (j = 0; j < term->cols; j++) {
4eeb7d09 4603 unsigned long tattr, tchar;
36566009 4604 termchar *d = lchars + j;
c6958dfe 4605 int break_run, do_copy;
3c5a620c 4606 scrpos.x = backward ? backward[j] : j;
32874aea 4607
36566009 4608 tchar = d->chr;
4609 tattr = d->attr;
4610
c6f1b8ed 4611 if (!term->cfg.ansi_colour)
4612 tattr = (tattr & ~(ATTR_FGMASK | ATTR_BGMASK)) |
4613 ATTR_DEFFG | ATTR_DEFBG;
4614
4eeb7d09 4615 switch (tchar & CSET_MASK) {
36566009 4616 case CSET_ASCII:
21d2b241 4617 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
4eeb7d09 4618 break;
36566009 4619 case CSET_LINEDRW:
21d2b241 4620 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
4eeb7d09 4621 break;
36566009 4622 case CSET_SCOACS:
21d2b241 4623 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
d3cb5465 4624 break;
4eeb7d09 4625 }
36566009 4626 if (j < term->cols-1 && d[1].chr == UCSWIDE)
552b7a5c 4627 tattr |= ATTR_WIDE;
4eeb7d09 4628
4629 /* Video reversing things */
f278d6f8 4630 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
4631 if (term->seltype == LEXICOGRAPHIC)
4632 selected = (posle(term->selstart, scrpos) &&
4633 poslt(scrpos, term->selend));
4634 else
4635 selected = (posPle(term->selstart, scrpos) &&
4636 posPlt(scrpos, term->selend));
4637 } else
4638 selected = FALSE;
4eeb7d09 4639 tattr = (tattr ^ rv
6908fed7 4640 ^ (selected ? ATTR_REVERSE : 0));
4eeb7d09 4641
4642 /* 'Real' blinking ? */
887035a5 4643 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
4644 if (term->has_focus && term->tblinker) {
fc6f0fc2 4645 tchar = term->ucsdata->unitab_line[(unsigned char)' '];
c9def1b8 4646 }
4eeb7d09 4647 tattr &= ~ATTR_BLINK;
c9def1b8 4648 }
374330e2 4649
5a73255e 4650 /*
4651 * Check the font we'll _probably_ be using to see if
4652 * the character is wide when we don't want it to be.
4653 */
36566009 4654 if (tchar != term->disptext[i]->chars[j].chr ||
4655 tattr != (term->disptext[i]->chars[j].attr &~
4656 ATTR_NARROW)) {
4657 if ((tattr & ATTR_WIDE) == 0 && char_width(ctx, tchar) == 2)
5a73255e 4658 tattr |= ATTR_NARROW;
36566009 4659 } else if (term->disptext[i]->chars[j].attr & ATTR_NARROW)
5a73255e 4660 tattr |= ATTR_NARROW;
4661
4eeb7d09 4662 /* Cursor here ? Save the 'background' */
3c7366f8 4663 if (i == our_curs_y && j == our_curs_x) {
c6958dfe 4664 /* FULL-TERMCHAR */
36566009 4665 cursor_background.chr = tchar;
4666 cursor_background.attr = tattr;
c6958dfe 4667 /* For once, this cc_next field is an absolute index in lchars */
4668 if (d->cc_next)
4669 cursor_background.cc_next = d->cc_next + j;
4670 else
4671 cursor_background.cc_next = 0;
36566009 4672 term->dispcursx = j;
4673 term->dispcursy = i;
4eeb7d09 4674 }
374330e2 4675
36566009 4676 if ((term->disptext[i]->chars[j].attr ^ tattr) & ATTR_WIDE)
4eeb7d09 4677 dirty_line = TRUE;
4678
c6958dfe 4679 break_run = ((tattr ^ attr) & term->attr_mask) != 0;
4eeb7d09 4680
4681 /* Special hack for VT100 Linedraw glyphs */
36566009 4682 if (tchar >= 0x23BA && tchar <= 0x23BD)
4683 break_run = TRUE;
4684
4685 /*
4686 * Separate out sequences of characters that have the
4687 * same CSET, if that CSET is a magic one.
4688 */
4689 if (CSET_OF(tchar) != cset)
4690 break_run = TRUE;
4eeb7d09 4691
c6958dfe 4692 /*
4693 * Break on both sides of any combined-character cell.
4694 */
4695 if (d->cc_next != 0 ||
4696 (j > 0 && d[-1].cc_next != 0))
4697 break_run = TRUE;
4698
21d2b241 4699 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
36566009 4700 if (term->disptext[i]->chars[j].chr == tchar &&
4701 term->disptext[i]->chars[j].attr == tattr)
4eeb7d09 4702 break_run = TRUE;
4703 else if (!dirty_run && ccount == 1)
4704 break_run = TRUE;
374330e2 4705 }
4eeb7d09 4706
4707 if (break_run) {
4708 if ((dirty_run || last_run_dirty) && ccount > 0) {
36566009 4709 do_text(ctx, start, i, ch, ccount, attr, ldata->lattr);
4eeb7d09 4710 updated_line = 1;
4711 }
4712 start = j;
4713 ccount = 0;
4714 attr = tattr;
36566009 4715 cset = CSET_OF(tchar);
21d2b241 4716 if (term->ucsdata->dbcs_screenfont)
4eeb7d09 4717 last_run_dirty = dirty_run;
4718 dirty_run = dirty_line;
4719 }
4720
c6958dfe 4721 do_copy = FALSE;
4722 if (!termchars_equal_override(&term->disptext[i]->chars[j],
4723 d, tchar, tattr)) {
4724 do_copy = TRUE;
4eeb7d09 4725 dirty_run = TRUE;
c6958dfe 4726 }
4727
4728 if (ccount >= chlen) {
4729 chlen = ccount + 256;
4730 ch = sresize(ch, chlen, wchar_t);
4731 }
36566009 4732 ch[ccount++] = (wchar_t) tchar;
c6958dfe 4733
4734 if (d->cc_next) {
4735 termchar *dd = d;
4736
4737 while (dd->cc_next) {
4738 unsigned long schar;
4739
4740 dd += dd->cc_next;
4741
4742 schar = dd->chr;
4743 switch (schar & CSET_MASK) {
4744 case CSET_ASCII:
4745 schar = term->ucsdata->unitab_line[schar & 0xFF];
4746 break;
4747 case CSET_LINEDRW:
4748 schar = term->ucsdata->unitab_xterm[schar & 0xFF];
4749 break;
4750 case CSET_SCOACS:
4751 schar = term->ucsdata->unitab_scoacs[schar&0xFF];
4752 break;
4753 }
4754
4755 if (ccount >= chlen) {
4756 chlen = ccount + 256;
4757 ch = sresize(ch, chlen, wchar_t);
4758 }
4759 ch[ccount++] = (wchar_t) schar;
4760 }
4761
4762 attr |= TATTR_COMBINING;
4763 }
4764
4765 if (do_copy) {
4766 copy_termchar(term->disptext[i], j, d);
4767 term->disptext[i]->chars[j].chr = tchar;
4768 term->disptext[i]->chars[j].attr = tattr;
4769 }
4eeb7d09 4770
4771 /* If it's a wide char step along to the next one. */
4772 if (tattr & ATTR_WIDE) {
887035a5 4773 if (++j < term->cols) {
4eeb7d09 4774 d++;
3c7366f8 4775 /*
4776 * By construction above, the cursor should not
4777 * be on the right-hand half of this character.
4778 * Ever.
4779 */
4780 assert(!(i == our_curs_y && j == our_curs_x));
c6958dfe 4781 if (!termchars_equal(&term->disptext[i]->chars[j], d))
4eeb7d09 4782 dirty_run = TRUE;
c6958dfe 4783 copy_termchar(term->disptext[i], j, d);
374330e2 4784 }
374330e2 4785 }
4eeb7d09 4786 }
4787 if (dirty_run && ccount > 0) {
36566009 4788 do_text(ctx, start, i, ch, ccount, attr, ldata->lattr);
4eeb7d09 4789 updated_line = 1;
4790 }
4791
4792 /* Cursor on this line ? (and changed) */
887035a5 4793 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
36566009 4794 ch[0] = (wchar_t) cursor_background.chr;
4795 attr = cursor_background.attr | cursor;
ac059c2e 4796 ccount = 1;
c6958dfe 4797
4798 if (cursor_background.cc_next) {
4799 termchar *dd = ldata->chars + cursor_background.cc_next;
4800
4801 while (1) {
4802 unsigned long schar;
4803
4804 schar = dd->chr;
4805 switch (schar & CSET_MASK) {
4806 case CSET_ASCII:
4807 schar = term->ucsdata->unitab_line[schar & 0xFF];
4808 break;
4809 case CSET_LINEDRW:
4810 schar = term->ucsdata->unitab_xterm[schar & 0xFF];
4811 break;
4812 case CSET_SCOACS:
4813 schar = term->ucsdata->unitab_scoacs[schar&0xFF];
4814 break;
4815 }
4816
4817 if (ccount >= chlen) {
4818 chlen = ccount + 256;
4819 ch = sresize(ch, chlen, wchar_t);
4820 }
4821 ch[ccount++] = (wchar_t) schar;
4822
4823 if (dd->cc_next)
4824 dd += dd->cc_next;
4825 else
4826 break;
4827 }
4828
4829 attr |= TATTR_COMBINING;
4830 }
4831
ac059c2e 4832 do_cursor(ctx, our_curs_x, i, ch, ccount, attr, ldata->lattr);
887035a5 4833 term->curstype = cursor;
374330e2 4834 }
36566009 4835
4836 unlineptr(ldata);
374330e2 4837 }
c6958dfe 4838
4839 sfree(ch);
374330e2 4840}
4841
4842/*
4843 * Invalidate the whole screen so it will be repainted in full.
4844 */
887035a5 4845void term_invalidate(Terminal *term)
32874aea 4846{
36566009 4847 int i, j;
374330e2 4848
36566009 4849 for (i = 0; i < term->rows; i++)
4850 for (j = 0; j < term->cols; j++)
4851 term->disptext[i]->chars[j].attr = ATTR_INVALID;
374330e2 4852}
4853
4854/*
4855 * Paint the window in response to a WM_PAINT message.
4856 */
887035a5 4857void term_paint(Terminal *term, Context ctx,
c1b55581 4858 int left, int top, int right, int bottom, int immediately)
32874aea 4859{
5a73255e 4860 int i, j;
4861 if (left < 0) left = 0;
4862 if (top < 0) top = 0;
887035a5 4863 if (right >= term->cols) right = term->cols-1;
4864 if (bottom >= term->rows) bottom = term->rows-1;
4865
4866 for (i = top; i <= bottom && i < term->rows; i++) {
a0f45c98 4867 if ((term->disptext[i]->lattr & LATTR_MODE) == LATTR_NORM)
887035a5 4868 for (j = left; j <= right && j < term->cols; j++)
36566009 4869 term->disptext[i]->chars[j].attr = ATTR_INVALID;
c9def1b8 4870 else
887035a5 4871 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
36566009 4872 term->disptext[i]->chars[j].attr = ATTR_INVALID;
c9def1b8 4873 }
374330e2 4874
39934deb 4875 if (immediately) {
887035a5 4876 do_paint (term, ctx, FALSE);
39934deb 4877 } else {
4878 if (!term->window_update_pending) {
4879 term->window_update_pending = TRUE;
4880 term->next_update = schedule_timer(UPDATE_DELAY, term_timer, term);
4881 }
4882 }
374330e2 4883}
4884
4885/*
4886 * Attempt to scroll the scrollback. The second parameter gives the
4887 * position we want to scroll to; the first is +1 to denote that
4888 * this position is relative to the beginning of the scrollback, -1
4889 * to denote it is relative to the end, and 0 to denote that it is
4890 * relative to the current position.
4891 */
887035a5 4892void term_scroll(Terminal *term, int rel, int where)
32874aea 4893{
876e5d5e 4894 int sbtop = -sblines(term);
37d2a505 4895#ifdef OPTIMISE_SCROLL
4896 int olddisptop = term->disptop;
4897 int shift;
4898#endif /* OPTIMISE_SCROLL */
887035a5 4899
4900 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
4901 if (term->disptop < sbtop)
4902 term->disptop = sbtop;
4903 if (term->disptop > 0)
4904 term->disptop = 0;
4905 update_sbar(term);
37d2a505 4906#ifdef OPTIMISE_SCROLL
4907 shift = (term->disptop - olddisptop);
4908 if (shift < term->rows && shift > -term->rows)
4909 scroll_display(term, 0, term->rows - 1, shift);
4910#endif /* OPTIMISE_SCROLL */
887035a5 4911 term_update(term);
374330e2 4912}
4913
c076966d 4914static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel)
32874aea 4915{
4eeb7d09 4916 wchar_t *workbuf;
4917 wchar_t *wbptr; /* where next char goes within workbuf */
6908fed7 4918 int old_top_x;
32874aea 4919 int wblen = 0; /* workbuf len */
4920 int buflen; /* amount of memory allocated to workbuf */
bc1235d4 4921
4eeb7d09 4922 buflen = 5120; /* Default size */
3d88e64d 4923 workbuf = snewn(buflen, wchar_t);
4eeb7d09 4924 wbptr = workbuf; /* start filling here */
6908fed7 4925 old_top_x = top.x; /* needed for rect==1 */
bc1235d4 4926
4facdf84 4927 while (poslt(top, bottom)) {
bc1235d4 4928 int nl = FALSE;
36566009 4929 termline *ldata = lineptr(top.y);
260f3dec 4930 pos nlpos;
4facdf84 4931
6908fed7 4932 /*
4933 * nlpos will point at the maximum position on this line we
4934 * should copy up to. So we start it at the end of the
4935 * line...
4936 */
4facdf84 4937 nlpos.y = top.y;
887035a5 4938 nlpos.x = term->cols;
bc1235d4 4939
6908fed7 4940 /*
4941 * ... move it backwards if there's unused space at the end
4942 * of the line (and also set `nl' if this is the case,
4943 * because in normal selection mode this means we need a
4944 * newline at the end)...
4945 */
36566009 4946 if (!(ldata->lattr & LATTR_WRAPPED)) {
4947 while (IS_SPACE_CHR(ldata->chars[nlpos.x - 1].chr) &&
d102f3cf 4948 !ldata->chars[nlpos.x - 1].cc_next &&
36566009 4949 poslt(top, nlpos))
4eeb7d09 4950 decpos(nlpos);
4facdf84 4951 if (poslt(nlpos, bottom))
bc1235d4 4952 nl = TRUE;
36566009 4953 } else if (ldata->lattr & LATTR_WRAPPED2) {
3c7366f8 4954 /* Ignore the last char on the line in a WRAPPED2 line. */
4955 decpos(nlpos);
bc1235d4 4956 }
6908fed7 4957
4958 /*
4959 * ... and then clip it to the terminal x coordinate if
4960 * we're doing rectangular selection. (In this case we
4961 * still did the above, so that copying e.g. the right-hand
4962 * column from a table doesn't fill with spaces on the
4963 * right.)
4964 */
4965 if (rect) {
4966 if (nlpos.x > bottom.x)
4967 nlpos.x = bottom.x;
4968 nl = (top.y < bottom.y);
4969 }
4970
4facdf84 4971 while (poslt(top, bottom) && poslt(top, nlpos)) {
4eeb7d09 4972#if 0
4973 char cbuf[16], *p;
4974 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
4975#else
4976 wchar_t cbuf[16], *p;
4eeb7d09 4977 int set, c;
c6958dfe 4978 int x = top.x;
4eeb7d09 4979
c6958dfe 4980 if (ldata->chars[x].chr == UCSWIDE) {
4eeb7d09 4981 top.x++;
4982 continue;
4983 }
4984
c6958dfe 4985 while (1) {
4986 int uc = ldata->chars[x].chr;
4987
4988 switch (uc & CSET_MASK) {
4989 case CSET_LINEDRW:
4990 if (!term->cfg.rawcnp) {
4991 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4992 break;
4993 }
4994 case CSET_ASCII:
4995 uc = term->ucsdata->unitab_line[uc & 0xFF];
4996 break;
4997 case CSET_SCOACS:
4998 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4999 break;
5000 }
5001 switch (uc & CSET_MASK) {
5002 case CSET_ACP:
5003 uc = term->ucsdata->unitab_font[uc & 0xFF];
5004 break;
5005 case CSET_OEMCP:
5006 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4eeb7d09 5007 break;
32874aea 5008 }
4eeb7d09 5009
c6958dfe 5010 set = (uc & CSET_MASK);
5011 c = (uc & ~CSET_MASK);
5012 cbuf[0] = uc;
5013 cbuf[1] = 0;
5014
5015 if (DIRECT_FONT(uc)) {
5016 if (c >= ' ' && c != 0x7F) {
5017 char buf[4];
5018 WCHAR wbuf[4];
5019 int rv;
5020 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
5021 buf[0] = c;
5022 buf[1] = (char) (0xFF & ldata->chars[top.x + 1].chr);
5023 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
5024 top.x++;
5025 } else {
5026 buf[0] = c;
5027 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
5028 }
bc1235d4 5029
c6958dfe 5030 if (rv > 0) {
5031 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
5032 cbuf[rv] = 0;
5033 }
d3a22f79 5034 }
d3a22f79 5035 }
4eeb7d09 5036#endif
5037
c6958dfe 5038 for (p = cbuf; *p; p++) {
5039 /* Enough overhead for trailing NL and nul */
5040 if (wblen >= buflen - 16) {
5041 buflen += 100;
5042 workbuf = sresize(workbuf, buflen, wchar_t);
5043 wbptr = workbuf + wblen;
5044 }
5045 wblen++;
5046 *wbptr++ = *p;
4eeb7d09 5047 }
c6958dfe 5048
5049 if (ldata->chars[x].cc_next)
5050 x += ldata->chars[x].cc_next;
5051 else
5052 break;
bc1235d4 5053 }
4facdf84 5054 top.x++;
bc1235d4 5055 }
5056 if (nl) {
5057 int i;
4eeb7d09 5058 for (i = 0; i < sel_nl_sz; i++) {
32874aea 5059 wblen++;
bc1235d4 5060 *wbptr++ = sel_nl[i];
5061 }
5062 }
4facdf84 5063 top.y++;
6908fed7 5064 top.x = rect ? old_top_x : 0;
36566009 5065
5066 unlineptr(ldata);
bc1235d4 5067 }
e6346999 5068#if SELECTION_NUL_TERMINATED
4eeb7d09 5069 wblen++;
5070 *wbptr++ = 0;
e6346999 5071#endif
c076966d 5072 write_clip(term->frontend, workbuf, wblen, desel); /* transfer to clipbd */
32874aea 5073 if (buflen > 0) /* indicates we allocated this buffer */
bc1235d4 5074 sfree(workbuf);
bc1235d4 5075}
4eeb7d09 5076
887035a5 5077void term_copyall(Terminal *term)
32874aea 5078{
4facdf84 5079 pos top;
40132dba 5080 pos bottom;
5081 tree234 *screen = term->screen;
876e5d5e 5082 top.y = -sblines(term);
4facdf84 5083 top.x = 0;
40132dba 5084 bottom.y = find_last_nonempty_line(term, screen);
5085 bottom.x = term->cols;
5086 clipme(term, top, bottom, 0, TRUE);
4eeb7d09 5087}
5088
5089/*
887035a5 5090 * The wordness array is mainly for deciding the disposition of the
5091 * US-ASCII characters.
4eeb7d09 5092 */
887035a5 5093static int wordtype(Terminal *term, int uc)
4eeb7d09 5094{
b9d7bcad 5095 struct ucsword {
4eeb7d09 5096 int start, end, ctype;
b9d7bcad 5097 };
5098 static const struct ucsword ucs_words[] = {
4eeb7d09 5099 {
5100 128, 160, 0}, {
5101 161, 191, 1}, {
5102 215, 215, 1}, {
5103 247, 247, 1}, {
5104 0x037e, 0x037e, 1}, /* Greek question mark */
5105 {
5106 0x0387, 0x0387, 1}, /* Greek ano teleia */
5107 {
5108 0x055a, 0x055f, 1}, /* Armenian punctuation */
5109 {
5110 0x0589, 0x0589, 1}, /* Armenian full stop */
5111 {
5112 0x0700, 0x070d, 1}, /* Syriac punctuation */
5113 {
5114 0x104a, 0x104f, 1}, /* Myanmar punctuation */
5115 {
5116 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
5117 {
5118 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
5119 {
5120 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
5121 {
5122 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
5123 {
5124 0x1800, 0x180a, 1}, /* Mongolian punctuation */
5125 {
5126 0x2000, 0x200a, 0}, /* Various spaces */
5127 {
5128 0x2070, 0x207f, 2}, /* superscript */
5129 {
5130 0x2080, 0x208f, 2}, /* subscript */
5131 {
5132 0x200b, 0x27ff, 1}, /* punctuation and symbols */
5133 {
5134 0x3000, 0x3000, 0}, /* ideographic space */
5135 {
5136 0x3001, 0x3020, 1}, /* ideographic punctuation */
5137 {
5138 0x303f, 0x309f, 3}, /* Hiragana */
5139 {
5140 0x30a0, 0x30ff, 3}, /* Katakana */
5141 {
5142 0x3300, 0x9fff, 3}, /* CJK Ideographs */
5143 {
5144 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
5145 {
5146 0xf900, 0xfaff, 3}, /* CJK Ideographs */
5147 {
5148 0xfe30, 0xfe6b, 1}, /* punctuation forms */
5149 {
5150 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
5151 {
5152 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
5153 {
5154 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
5155 {
5156 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
5157 {
5158 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
5159 {
5160 0, 0, 0}
5161 };
b9d7bcad 5162 const struct ucsword *wptr;
4eeb7d09 5163
4eeb7d09 5164 switch (uc & CSET_MASK) {
36566009 5165 case CSET_LINEDRW:
21d2b241 5166 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4eeb7d09 5167 break;
36566009 5168 case CSET_ASCII:
21d2b241 5169 uc = term->ucsdata->unitab_line[uc & 0xFF];
4eeb7d09 5170 break;
36566009 5171 case CSET_SCOACS:
21d2b241 5172 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
d3cb5465 5173 break;
4eeb7d09 5174 }
5175 switch (uc & CSET_MASK) {
36566009 5176 case CSET_ACP:
21d2b241 5177 uc = term->ucsdata->unitab_font[uc & 0xFF];
4eeb7d09 5178 break;
36566009 5179 case CSET_OEMCP:
21d2b241 5180 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4eeb7d09 5181 break;
5182 }
5183
36566009 5184 /* For DBCS fonts I can't do anything useful. Even this will sometimes
5a73255e 5185 * fail as there's such a thing as a double width space. :-(
5186 */
21d2b241 5187 if (term->ucsdata->dbcs_screenfont &&
5188 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
5a73255e 5189 return (uc != ' ');
5190
4eeb7d09 5191 if (uc < 0x80)
887035a5 5192 return term->wordness[uc];
4eeb7d09 5193
5194 for (wptr = ucs_words; wptr->start; wptr++) {
5195 if (uc >= wptr->start && uc <= wptr->end)
5196 return wptr->ctype;
5197 }
5198
5199 return 2;
bc1235d4 5200}
5201
374330e2 5202/*
5203 * Spread the selection outwards according to the selection mode.
5204 */
887035a5 5205static pos sel_spread_half(Terminal *term, pos p, int dir)
32874aea 5206{
36566009 5207 termline *ldata;
374330e2 5208 short wvalue;
876e5d5e 5209 int topy = -sblines(term);
374330e2 5210
4facdf84 5211 ldata = lineptr(p.y);
374330e2 5212
887035a5 5213 switch (term->selmode) {
374330e2 5214 case SM_CHAR:
5215 /*
5216 * In this mode, every character is a separate unit, except
5217 * for runs of spaces at the end of a non-wrapping line.
5218 */
36566009 5219 if (!(ldata->lattr & LATTR_WRAPPED)) {
5220 termchar *q = ldata->chars + term->cols;
d102f3cf 5221 while (q > ldata->chars &&
5222 IS_SPACE_CHR(q[-1].chr) && !q[-1].cc_next)
374330e2 5223 q--;
36566009 5224 if (q == ldata->chars + term->cols)
374330e2 5225 q--;
36566009 5226 if (p.x >= q - ldata->chars)
5227 p.x = (dir == -1 ? q - ldata->chars : term->cols - 1);
374330e2 5228 }
5229 break;
5230 case SM_WORD:
5231 /*
5232 * In this mode, the units are maximal runs of characters
5233 * whose `wordness' has the same value.
5234 */
36566009 5235 wvalue = wordtype(term, UCSGET(ldata->chars, p.x));
374330e2 5236 if (dir == +1) {
50ab4088 5237 while (1) {
36566009 5238 int maxcols = (ldata->lattr & LATTR_WRAPPED2 ?
3c7366f8 5239 term->cols-1 : term->cols);
5240 if (p.x < maxcols-1) {
36566009 5241 if (wordtype(term, UCSGET(ldata->chars, p.x+1)) == wvalue)
50ab4088 5242 p.x++;
5243 else
5244 break;
5245 } else {
36566009 5246 if (ldata->lattr & LATTR_WRAPPED) {
5247 termline *ldata2;
50ab4088 5248 ldata2 = lineptr(p.y+1);
36566009 5249 if (wordtype(term, UCSGET(ldata2->chars, 0))
5250 == wvalue) {
50ab4088 5251 p.x = 0;
5252 p.y++;
36566009 5253 unlineptr(ldata);
50ab4088 5254 ldata = ldata2;
36566009 5255 } else {
5256 unlineptr(ldata2);
50ab4088 5257 break;
36566009 5258 }
50ab4088 5259 } else
5260 break;
5261 }
5262 }
374330e2 5263 } else {
50ab4088 5264 while (1) {
5265 if (p.x > 0) {
36566009 5266 if (wordtype(term, UCSGET(ldata->chars, p.x-1)) == wvalue)
50ab4088 5267 p.x--;
5268 else
5269 break;
5270 } else {
36566009 5271 termline *ldata2;
3c7366f8 5272 int maxcols;
50ab4088 5273 if (p.y <= topy)
5274 break;
5275 ldata2 = lineptr(p.y-1);
36566009 5276 maxcols = (ldata2->lattr & LATTR_WRAPPED2 ?
3c7366f8 5277 term->cols-1 : term->cols);
36566009 5278 if (ldata2->lattr & LATTR_WRAPPED) {
5279 if (wordtype(term, UCSGET(ldata2->chars, maxcols-1))
3c7366f8 5280 == wvalue) {
5281 p.x = maxcols-1;
5282 p.y--;
36566009 5283 unlineptr(ldata);
3c7366f8 5284 ldata = ldata2;
36566009 5285 } else {
5286 unlineptr(ldata2);
3c7366f8 5287 break;
36566009 5288 }
50ab4088 5289 } else
5290 break;
5291 }
5292 }
374330e2 5293 }
5294 break;
5295 case SM_LINE:
5296 /*
5297 * In this mode, every line is a unit.
5298 */
887035a5 5299 p.x = (dir == -1 ? 0 : term->cols - 1);
374330e2 5300 break;
5301 }
36566009 5302
5303 unlineptr(ldata);
374330e2 5304 return p;
5305}
5306
887035a5 5307static void sel_spread(Terminal *term)
32874aea 5308{
887035a5 5309 if (term->seltype == LEXICOGRAPHIC) {
5310 term->selstart = sel_spread_half(term, term->selstart, -1);
5311 decpos(term->selend);
5312 term->selend = sel_spread_half(term, term->selend, +1);
5313 incpos(term->selend);
6908fed7 5314 }
374330e2 5315}
5316
887035a5 5317void term_do_paste(Terminal *term)
568dd02f 5318{
5319 wchar_t *data;
5320 int len;
5321
a8327734 5322 get_clip(term->frontend, &data, &len);
2cb50250 5323 if (data && len > 0) {
568dd02f 5324 wchar_t *p, *q;
5325
887035a5 5326 term_seen_key_event(term); /* pasted data counts */
2cb50250 5327
887035a5 5328 if (term->paste_buffer)
5329 sfree(term->paste_buffer);
5330 term->paste_pos = term->paste_hold = term->paste_len = 0;
3d88e64d 5331 term->paste_buffer = snewn(len, wchar_t);
568dd02f 5332
5333 p = q = data;
5334 while (p < data + len) {
5335 while (p < data + len &&
5336 !(p <= data + len - sel_nl_sz &&
5337 !memcmp(p, sel_nl, sizeof(sel_nl))))
5338 p++;
5339
5340 {
5341 int i;
5342 for (i = 0; i < p - q; i++) {
887035a5 5343 term->paste_buffer[term->paste_len++] = q[i];
568dd02f 5344 }
5345 }
5346
5347 if (p <= data + len - sel_nl_sz &&
5348 !memcmp(p, sel_nl, sizeof(sel_nl))) {
92e23ad9 5349 term->paste_buffer[term->paste_len++] = '\015';
568dd02f 5350 p += sel_nl_sz;
5351 }
5352 q = p;
5353 }
5354
5355 /* Assume a small paste will be OK in one go. */
887035a5 5356 if (term->paste_len < 256) {
b9d7bcad 5357 if (term->ldisc)
5358 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
887035a5 5359 if (term->paste_buffer)
5360 sfree(term->paste_buffer);
5361 term->paste_buffer = 0;
5362 term->paste_pos = term->paste_hold = term->paste_len = 0;
568dd02f 5363 }
5364 }
a8327734 5365 get_clip(term->frontend, NULL, NULL);
568dd02f 5366}
5367
fc5b0934 5368void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,
5369 Mouse_Action a, int x, int y, int shift, int ctrl, int alt)
32874aea 5370{
4facdf84 5371 pos selpoint;
36566009 5372 termline *ldata;
887035a5 5373 int raw_mouse = (term->xterm_mouse &&
64734920 5374 !term->cfg.no_mouse_rep &&
5375 !(term->cfg.mouse_override && shift));
6908fed7 5376 int default_seltype;
32874aea 5377
fff61f25 5378 if (y < 0) {
32874aea 5379 y = 0;
fff61f25 5380 if (a == MA_DRAG && !raw_mouse)
887035a5 5381 term_scroll(term, 0, -1);
fff61f25 5382 }
887035a5 5383 if (y >= term->rows) {
5384 y = term->rows - 1;
fff61f25 5385 if (a == MA_DRAG && !raw_mouse)
887035a5 5386 term_scroll(term, 0, +1);
fff61f25 5387 }
32874aea 5388 if (x < 0) {
5389 if (y > 0) {
887035a5 5390 x = term->cols - 1;
32874aea 5391 y--;
5392 } else
5393 x = 0;
094ed2a6 5394 }
887035a5 5395 if (x >= term->cols)
5396 x = term->cols - 1;
37508af4 5397
887035a5 5398 selpoint.y = y + term->disptop;
4facdf84 5399 ldata = lineptr(selpoint.y);
3c5a620c 5400
36566009 5401 if ((ldata->lattr & LATTR_MODE) != LATTR_NORM)
3c5a620c 5402 x /= 2;
5403
5404 /*
5405 * Transform x through the bidi algorithm to find the _logical_
5406 * click point from the physical one.
5407 */
5408 if (term_bidi_line(term, ldata, y) != NULL) {
5409 x = term->post_bidi_cache[y].backward[x];
5410 }
5411
5412 selpoint.x = x;
36566009 5413 unlineptr(ldata);
374330e2 5414
fff61f25 5415 if (raw_mouse) {
01c034ad 5416 int encstate = 0, r, c;
5417 char abuf[16];
01c034ad 5418
b9d7bcad 5419 if (term->ldisc) {
5420
fc5b0934 5421 switch (braw) {
b9d7bcad 5422 case MBT_LEFT:
5423 encstate = 0x20; /* left button down */
5424 break;
5425 case MBT_MIDDLE:
5426 encstate = 0x21;
5427 break;
5428 case MBT_RIGHT:
5429 encstate = 0x22;
5430 break;
5431 case MBT_WHEEL_UP:
5432 encstate = 0x60;
5433 break;
5434 case MBT_WHEEL_DOWN:
5435 encstate = 0x61;
5436 break;
5437 default: break; /* placate gcc warning about enum use */
5438 }
5439 switch (a) {
5440 case MA_DRAG:
5441 if (term->xterm_mouse == 1)
5442 return;
5443 encstate += 0x20;
5444 break;
5445 case MA_RELEASE:
5446 encstate = 0x23;
5447 term->mouse_is_down = 0;
5448 break;
5449 case MA_CLICK:
fc5b0934 5450 if (term->mouse_is_down == braw)
b9d7bcad 5451 return;
fc5b0934 5452 term->mouse_is_down = braw;
b9d7bcad 5453 break;
5454 default: break; /* placate gcc warning about enum use */
5455 }
5456 if (shift)
5457 encstate += 0x04;
5458 if (ctrl)
5459 encstate += 0x10;
5460 r = y + 33;
5461 c = x + 33;
5462
5463 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
5464 ldisc_send(term->ldisc, abuf, 6, 0);
01c034ad 5465 }
01c034ad 5466 return;
5467 }
5468
6908fed7 5469 /*
5470 * Set the selection type (rectangular or normal) at the start
5471 * of a selection attempt, from the state of Alt.
5472 */
64734920 5473 if (!alt ^ !term->cfg.rect_select)
6908fed7 5474 default_seltype = RECTANGULAR;
5475 else
5476 default_seltype = LEXICOGRAPHIC;
5477
887035a5 5478 if (term->selstate == NO_SELECTION) {
5479 term->seltype = default_seltype;
6908fed7 5480 }
5481
fc5b0934 5482 if (bcooked == MBT_SELECT && a == MA_CLICK) {
887035a5 5483 deselect(term);
5484 term->selstate = ABOUT_TO;
5485 term->seltype = default_seltype;
5486 term->selanchor = selpoint;
5487 term->selmode = SM_CHAR;
fc5b0934 5488 } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
887035a5 5489 deselect(term);
5490 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
5491 term->selstate = DRAGGING;
5492 term->selstart = term->selanchor = selpoint;
5493 term->selend = term->selstart;
5494 incpos(term->selend);
5495 sel_spread(term);
fc5b0934 5496 } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||
5497 (bcooked == MBT_EXTEND && a != MA_RELEASE)) {
887035a5 5498 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
374330e2 5499 return;
fc5b0934 5500 if (bcooked == MBT_EXTEND && a != MA_DRAG &&
5501 term->selstate == SELECTED) {
887035a5 5502 if (term->seltype == LEXICOGRAPHIC) {
6908fed7 5503 /*
5504 * For normal selection, we extend by moving
5505 * whichever end of the current selection is closer
5506 * to the mouse.
5507 */
887035a5 5508 if (posdiff(selpoint, term->selstart) <
5509 posdiff(term->selend, term->selstart) / 2) {
5510 term->selanchor = term->selend;
5511 decpos(term->selanchor);
6908fed7 5512 } else {
887035a5 5513 term->selanchor = term->selstart;
6908fed7 5514 }
4facdf84 5515 } else {
6908fed7 5516 /*
5517 * For rectangular selection, we have a choice of
5518 * _four_ places to put selanchor and selpoint: the
5519 * four corners of the selection.
5520 */
887035a5 5521 if (2*selpoint.x < term->selstart.x + term->selend.x)
5522 term->selanchor.x = term->selend.x-1;
6908fed7 5523 else
887035a5 5524 term->selanchor.x = term->selstart.x;
6908fed7 5525
887035a5 5526 if (2*selpoint.y < term->selstart.y + term->selend.y)
5527 term->selanchor.y = term->selend.y;
6908fed7 5528 else
887035a5 5529 term->selanchor.y = term->selstart.y;
4facdf84 5530 }
887035a5 5531 term->selstate = DRAGGING;
374330e2 5532 }
887035a5 5533 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
5534 term->selanchor = selpoint;
5535 term->selstate = DRAGGING;
5536 if (term->seltype == LEXICOGRAPHIC) {
6908fed7 5537 /*
5538 * For normal selection, we set (selstart,selend) to
5539 * (selpoint,selanchor) in some order.
5540 */
887035a5 5541 if (poslt(selpoint, term->selanchor)) {
5542 term->selstart = selpoint;
5543 term->selend = term->selanchor;
5544 incpos(term->selend);
6908fed7 5545 } else {
887035a5 5546 term->selstart = term->selanchor;
5547 term->selend = selpoint;
5548 incpos(term->selend);
6908fed7 5549 }
374330e2 5550 } else {
6908fed7 5551 /*
5552 * For rectangular selection, we may need to
5553 * interchange x and y coordinates (if the user has
5554 * dragged in the -x and +y directions, or vice versa).
5555 */
887035a5 5556 term->selstart.x = min(term->selanchor.x, selpoint.x);
5557 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
5558 term->selstart.y = min(term->selanchor.y, selpoint.y);
5559 term->selend.y = max(term->selanchor.y, selpoint.y);
374330e2 5560 }
887035a5 5561 sel_spread(term);
fc5b0934 5562 } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&
5563 a == MA_RELEASE) {
887035a5 5564 if (term->selstate == DRAGGING) {
374330e2 5565 /*
5566 * We've completed a selection. We now transfer the
5567 * data to the clipboard.
5568 */
887035a5 5569 clipme(term, term->selstart, term->selend,
c076966d 5570 (term->seltype == RECTANGULAR), FALSE);
887035a5 5571 term->selstate = SELECTED;
374330e2 5572 } else
887035a5 5573 term->selstate = NO_SELECTION;
fc5b0934 5574 } else if (bcooked == MBT_PASTE
e6346999 5575 && (a == MA_CLICK
5576#if MULTICLICK_ONLY_EVENT
5577 || a == MA_2CLK || a == MA_3CLK
5578#endif
5579 )) {
a8327734 5580 request_paste(term->frontend);
374330e2 5581 }
5582
887035a5 5583 term_update(term);
374330e2 5584}
5585
6c50d421 5586void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen,
5587 unsigned int modifiers, unsigned int flags)
5588{
5589 char output[10];
5590 char *p = output;
5591 int prependesc = FALSE;
84b8d085 5592#if 0
6c50d421 5593 int i;
5594
5595 fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen);
5596 for (i = 0; i < tlen; i++)
bf02e82e 5597 fprintf(stderr, " %04x", (unsigned)text[i]);
6c50d421 5598 fprintf(stderr, "\n");
84b8d085 5599#endif
6c50d421 5600
5601 /* XXX Num Lock */
5602 if ((flags & PKF_REPEAT) && term->repeat_off)
5603 return;
5604
5605 /* Currently, Meta always just prefixes everything with ESC. */
5606 if (modifiers & PKM_META)
5607 prependesc = TRUE;
5608 modifiers &= ~PKM_META;
5609
5610 /*
5611 * Alt is only used for Alt+keypad, which isn't supported yet, so
5612 * ignore it.
5613 */
5614 modifiers &= ~PKM_ALT;
5615
5616 /* Standard local function keys */
5617 switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) {
5618 case PKM_SHIFT:
5619 if (keysym == PK_PAGEUP)
5620 /* scroll up one page */;
5621 if (keysym == PK_PAGEDOWN)
5622 /* scroll down on page */;
5623 if (keysym == PK_INSERT)
5624 term_do_paste(term);
5625 break;
5626 case PKM_CONTROL:
5627 if (keysym == PK_PAGEUP)
5628 /* scroll up one line */;
5629 if (keysym == PK_PAGEDOWN)
5630 /* scroll down one line */;
5631 /* Control-Numlock for app-keypad mode switch */
5632 if (keysym == PK_PF1)
5633 term->app_keypad_keys ^= 1;
5634 break;
5635 }
5636
5637 if (modifiers & PKM_ALT) {
5638 /* Alt+F4 (close) */
5639 /* Alt+Return (full screen) */
5640 /* Alt+Space (system menu) */
5641 }
5642
5643 if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 &&
5644 text[0] >= 0x20 && text[0] <= 0x7e) {
5645 /* ASCII chars + Control */
bf02e82e 5646 if ((text[0] >= 0x40 && text[0] <= 0x5f) ||
5647 (text[0] >= 0x61 && text[0] <= 0x7a))
6c50d421 5648 text[0] &= 0x1f;
5649 else {
5650 /*
5651 * Control-2 should return ^@ (0x00), Control-6 should return
5652 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
5653 * the DOS keyboard handling did it, and we have nothing better
5654 * to do with the key combo in question, we'll also map
5655 * Control-Backquote to ^\ (0x1C).
5656 */
5657 switch (text[0]) {
5658 case ' ': text[0] = 0x00; break;
5659 case '-': text[0] = 0x1f; break;
5660 case '/': text[0] = 0x1f; break;
5661 case '2': text[0] = 0x00; break;
5662 case '3': text[0] = 0x1b; break;
5663 case '4': text[0] = 0x1c; break;
5664 case '5': text[0] = 0x1d; break;
5665 case '6': text[0] = 0x1e; break;
5666 case '7': text[0] = 0x1f; break;
5667 case '8': text[0] = 0x7f; break;
5668 case '`': text[0] = 0x1c; break;
5669 }
5670 }
5671 }
5672
5673 /* Nethack keypad */
5674 if (term->cfg.nethack_keypad) {
5675 char c = 0;
5676 switch (keysym) {
5677 case PK_KP1: c = 'b'; break;
5678 case PK_KP2: c = 'j'; break;
5679 case PK_KP3: c = 'n'; break;
5680 case PK_KP4: c = 'h'; break;
5681 case PK_KP5: c = '.'; break;
5682 case PK_KP6: c = 'l'; break;
5683 case PK_KP7: c = 'y'; break;
5684 case PK_KP8: c = 'k'; break;
5685 case PK_KP9: c = 'u'; break;
bf02e82e 5686 default: break; /* else gcc warns `enum value not used' */
6c50d421 5687 }
5688 if (c != 0) {
5689 if (c != '.') {
5690 if (modifiers & PKM_CONTROL)
5691 c &= 0x1f;
5692 else if (modifiers & PKM_SHIFT)
5693 c = toupper(c);
5694 }
5695 *p++ = c;
5696 goto done;
5697 }
5698 }
5699
5700 /* Numeric Keypad */
5701 if (PK_ISKEYPAD(keysym)) {
5702 int xkey = 0;
5703
5704 /*
5705 * In VT400 mode, PFn always emits an escape sequence. In
5706 * Linux and tilde modes, this only happens in app keypad mode.
5707 */
5708 if (term->cfg.funky_type == FUNKY_VT400 ||
5709 ((term->cfg.funky_type == FUNKY_LINUX ||
5710 term->cfg.funky_type == FUNKY_TILDE) &&
5711 term->app_keypad_keys && !term->cfg.no_applic_k)) {
5712 switch (keysym) {
5713 case PK_PF1: xkey = 'P'; break;
5714 case PK_PF2: xkey = 'Q'; break;
5715 case PK_PF3: xkey = 'R'; break;
5716 case PK_PF4: xkey = 'S'; break;
bf02e82e 5717 default: break; /* else gcc warns `enum value not used' */
6c50d421 5718 }
5719 }
5720 if (term->app_keypad_keys && !term->cfg.no_applic_k) {
5721 switch (keysym) {
5722 case PK_KP0: xkey = 'p'; break;
5723 case PK_KP1: xkey = 'q'; break;
5724 case PK_KP2: xkey = 'r'; break;
5725 case PK_KP3: xkey = 's'; break;
5726 case PK_KP4: xkey = 't'; break;
5727 case PK_KP5: xkey = 'u'; break;
5728 case PK_KP6: xkey = 'v'; break;
5729 case PK_KP7: xkey = 'w'; break;
5730 case PK_KP8: xkey = 'x'; break;
5731 case PK_KP9: xkey = 'y'; break;
5732 case PK_KPDECIMAL: xkey = 'n'; break;
5733 case PK_KPENTER: xkey = 'M'; break;
bf02e82e 5734 default: break; /* else gcc warns `enum value not used' */
6c50d421 5735 }
5736 if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) {
5737 /*
5738 * xterm can't see the layout of the keypad, so it has
5739 * to rely on the X keysyms returned by the keys.
5740 * Hence, we look at the strings here, not the PuTTY
5741 * keysyms (which describe the layout).
5742 */
5743 switch (text[0]) {
5744 case '+':
5745 if (modifiers & PKM_SHIFT)
5746 xkey = 'l';
5747 else
5748 xkey = 'k';
5749 break;
5750 case '/': xkey = 'o'; break;
5751 case '*': xkey = 'j'; break;
5752 case '-': xkey = 'm'; break;
5753 }
5754 } else {
5755 /*
5756 * In all other modes, we try to retain the layout of
5757 * the DEC keypad in application mode.
5758 */
5759 switch (keysym) {
5760 case PK_KPBIGPLUS:
5761 /* This key covers the '-' and ',' keys on a VT220 */
5762 if (modifiers & PKM_SHIFT)
5763 xkey = 'm'; /* VT220 '-' */
5764 else
5765 xkey = 'l'; /* VT220 ',' */
5766 break;
5767 case PK_KPMINUS: xkey = 'm'; break;
5768 case PK_KPCOMMA: xkey = 'l'; break;
bf02e82e 5769 default: break; /* else gcc warns `enum value not used' */
6c50d421 5770 }
5771 }
5772 }
5773 if (xkey) {
5774 if (term->vt52_mode) {
5775 if (xkey >= 'P' && xkey <= 'S')
5776 p += sprintf((char *) p, "\x1B%c", xkey);
5777 else
5778 p += sprintf((char *) p, "\x1B?%c", xkey);
5779 } else
5780 p += sprintf((char *) p, "\x1BO%c", xkey);
5781 goto done;
5782 }
5783 /* Not in application mode -- treat the number pad as arrow keys? */
5784 if ((flags & PKF_NUMLOCK) == 0) {
5785 switch (keysym) {
5786 case PK_KP0: keysym = PK_INSERT; break;
5787 case PK_KP1: keysym = PK_END; break;
5788 case PK_KP2: keysym = PK_DOWN; break;
5789 case PK_KP3: keysym = PK_PAGEDOWN; break;
5790 case PK_KP4: keysym = PK_LEFT; break;
5791 case PK_KP5: keysym = PK_REST; break;
5792 case PK_KP6: keysym = PK_RIGHT; break;
5793 case PK_KP7: keysym = PK_HOME; break;
5794 case PK_KP8: keysym = PK_UP; break;
5795 case PK_KP9: keysym = PK_PAGEUP; break;
bf02e82e 5796 default: break; /* else gcc warns `enum value not used' */
6c50d421 5797 }
5798 }
5799 }
5800
5801 /* Miscellaneous keys */
5802 switch (keysym) {
5803 case PK_ESCAPE:
5804 *p++ = 0x1b;
5805 goto done;
5806 case PK_BACKSPACE:
5807 if (modifiers == 0)
5808 *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08);
5809 else if (modifiers == PKM_SHIFT)
5810 /* We do the opposite of what is configured */
5811 *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F);
5812 else break;
5813 goto done;
5814 case PK_TAB:
5815 if (modifiers == 0)
5816 *p++ = 0x09;
5817 else if (modifiers == PKM_SHIFT)
5818 *p++ = 0x1B, *p++ = '[', *p++ = 'Z';
5819 else break;
5820 goto done;
5821 /* XXX window.c has ctrl+shift+space sending 0xa0 */
5822 case PK_PAUSE:
5823 if (modifiers == PKM_CONTROL)
5824 *p++ = 26;
5825 else break;
5826 goto done;
5827 case PK_RETURN:
5828 case PK_KPENTER: /* Odd keypad modes handled above */
5829 if (modifiers == 0) {
5830 *p++ = 0x0d;
5831 if (term->cr_lf_return)
5832 *p++ = 0x0a;
5833 goto done;
5834 }
bf02e82e 5835 default: break; /* else gcc warns `enum value not used' */
6c50d421 5836 }
5837
5838 /* SCO function keys and editing keys */
5839 if (term->cfg.funky_type == FUNKY_SCO) {
5840 if (PK_ISFKEY(keysym) && keysym <= PK_F12) {
5841 static char const codes[] =
5842 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
5843 int index = keysym - PK_F1;
5844
5845 if (modifiers & PKM_SHIFT) index += 12;
5846 if (modifiers & PKM_CONTROL) index += 24;
5847 p += sprintf((char *) p, "\x1B[%c", codes[index]);
5848 goto done;
5849 }
5850 if (PK_ISEDITING(keysym)) {
5851 int xkey = 0;
5852
5853 switch (keysym) {
5854 case PK_DELETE: *p++ = 0x7f; goto done;
5855 case PK_HOME: xkey = 'H'; break;
5856 case PK_INSERT: xkey = 'L'; break;
5857 case PK_END: xkey = 'F'; break;
5858 case PK_PAGEUP: xkey = 'I'; break;
5859 case PK_PAGEDOWN: xkey = 'G'; break;
bf02e82e 5860 default: break; /* else gcc warns `enum value not used' */
6c50d421 5861 }
5862 p += sprintf((char *) p, "\x1B[%c", xkey);
5863 }
5864 }
5865
5866 if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) {
5867 int code;
5868
5869 if (term->cfg.funky_type == FUNKY_XTERM) {
5870 /* Xterm shuffles these keys, apparently. */
5871 switch (keysym) {
5872 case PK_HOME: keysym = PK_INSERT; break;
5873 case PK_INSERT: keysym = PK_HOME; break;
5874 case PK_DELETE: keysym = PK_END; break;
5875 case PK_END: keysym = PK_PAGEUP; break;
5876 case PK_PAGEUP: keysym = PK_DELETE; break;
5877 case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break;
bf02e82e 5878 default: break; /* else gcc warns `enum value not used' */
6c50d421 5879 }
5880 }
5881
5882 /* RXVT Home/End */
5883 if (term->cfg.rxvt_homeend &&
5884 (keysym == PK_HOME || keysym == PK_END)) {
5885 p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw");
5886 goto done;
5887 }
5888
5889 if (term->vt52_mode) {
5890 int xkey;
5891
5892 /*
5893 * A real VT52 doesn't have these, and a VT220 doesn't
5894 * send anything for them in VT52 mode.
5895 */
5896 switch (keysym) {
5897 case PK_HOME: xkey = 'H'; break;
5898 case PK_INSERT: xkey = 'L'; break;
5899 case PK_DELETE: xkey = 'M'; break;
5900 case PK_END: xkey = 'E'; break;
5901 case PK_PAGEUP: xkey = 'I'; break;
5902 case PK_PAGEDOWN: xkey = 'G'; break;
7ffdbc1a 5903 default: xkey=0; break; /* else gcc warns `enum value not used'*/
6c50d421 5904 }
5905 p += sprintf((char *) p, "\x1B%c", xkey);
5906 goto done;
5907 }
5908
5909 switch (keysym) {
5910 case PK_HOME: code = 1; break;
5911 case PK_INSERT: code = 2; break;
5912 case PK_DELETE: code = 3; break;
5913 case PK_END: code = 4; break;
5914 case PK_PAGEUP: code = 5; break;
5915 case PK_PAGEDOWN: code = 6; break;
7ffdbc1a 5916 default: code = 0; break; /* else gcc warns `enum value not used' */
6c50d421 5917 }
5918 p += sprintf((char *) p, "\x1B[%d~", code);
5919 goto done;
5920 }
5921
5922 if (PK_ISFKEY(keysym)) {
5923 /* Map Shift+F1-F10 to F11-F20 */
5924 if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT))
5925 keysym += 10;
5926 if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) &&
5927 keysym <= PK_F14) {
5928 /* XXX This overrides the XTERM/VT52 mode below */
5929 int offt = 0;
5930 if (keysym >= PK_F6) offt++;
5931 if (keysym >= PK_F12) offt++;
5932 p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c",
5933 'P' + keysym - PK_F1 - offt);
5934 goto done;
5935 }
5936 if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) {
5937 p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1);
5938 goto done;
5939 }
5940 if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) {
5941 if (term->vt52_mode)
5942 p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1);
5943 else
5944 p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1);
5945 goto done;
5946 }
5947 p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1);
5948 goto done;
5949 }
5950
5951 if (PK_ISCURSOR(keysym)) {
5952 int xkey;
5953
5954 switch (keysym) {
5955 case PK_UP: xkey = 'A'; break;
5956 case PK_DOWN: xkey = 'B'; break;
5957 case PK_RIGHT: xkey = 'C'; break;
5958 case PK_LEFT: xkey = 'D'; break;
5959 case PK_REST: xkey = 'G'; break; /* centre key on number pad */
5342c096 5960 default: xkey = 0; break; /* else gcc warns `enum value not used' */
6c50d421 5961 }
5962 if (term->vt52_mode)
5963 p += sprintf((char *) p, "\x1B%c", xkey);
5964 else {
5965 int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c);
5966
5967 /* Useful mapping of Ctrl-arrows */
5968 if (modifiers == PKM_CONTROL)
5969 app_flg = !app_flg;
5970
5971 if (app_flg)
5972 p += sprintf((char *) p, "\x1BO%c", xkey);
5973 else
5974 p += sprintf((char *) p, "\x1B[%c", xkey);
5975 }
5976 goto done;
5977 }
5978
5979 done:
5980 if (p > output || tlen > 0) {
5981 /*
5982 * Interrupt an ongoing paste. I'm not sure
5983 * this is sensible, but for the moment it's
5984 * preferable to having to faff about buffering
5985 * things.
5986 */
5987 term_nopaste(term);
5988
5989 /*
5990 * We need not bother about stdin backlogs
5991 * here, because in GUI PuTTY we can't do
5992 * anything about it anyway; there's no means
5993 * of asking Windows to hold off on KEYDOWN
5994 * messages. We _have_ to buffer everything
5995 * we're sent.
5996 */
5997 term_seen_key_event(term);
5998
5999 if (prependesc) {
84b8d085 6000#if 0
6c50d421 6001 fprintf(stderr, "sending ESC\n");
84b8d085 6002#endif
6c50d421 6003 ldisc_send(term->ldisc, "\x1b", 1, 1);
6004 }
6005
6006 if (p > output) {
84b8d085 6007#if 0
6c50d421 6008 fprintf(stderr, "sending %d bytes:", p - output);
6009 for (i = 0; i < p - output; i++)
6010 fprintf(stderr, " %02x", output[i]);
6011 fprintf(stderr, "\n");
84b8d085 6012#endif
6c50d421 6013 ldisc_send(term->ldisc, output, p - output, 1);
6014 } else if (tlen > 0) {
84b8d085 6015#if 0
6c50d421 6016 fprintf(stderr, "sending %d unichars:", tlen);
6017 for (i = 0; i < tlen; i++)
bf02e82e 6018 fprintf(stderr, " %04x", (unsigned) text[i]);
6c50d421 6019 fprintf(stderr, "\n");
84b8d085 6020#endif
6c50d421 6021 luni_send(term->ldisc, text, tlen, 1);
6022 }
6023 }
6024}
6025
887035a5 6026void term_nopaste(Terminal *term)
32874aea 6027{
887035a5 6028 if (term->paste_len == 0)
32874aea 6029 return;
887035a5 6030 sfree(term->paste_buffer);
f278d6f8 6031 term->paste_buffer = NULL;
887035a5 6032 term->paste_len = 0;
c9def1b8 6033}
6034
887035a5 6035int term_paste_pending(Terminal *term)
0f660c8f 6036{
887035a5 6037 return term->paste_len != 0;
0f660c8f 6038}
6039
887035a5 6040void term_paste(Terminal *term)
32874aea 6041{
c9def1b8 6042 long now, paste_diff;
6043
887035a5 6044 if (term->paste_len == 0)
32874aea 6045 return;
c9def1b8 6046
6047 /* Don't wait forever to paste */
887035a5 6048 if (term->paste_hold) {
f7f27309 6049 now = GETTICKCOUNT();
887035a5 6050 paste_diff = now - term->last_paste;
32874aea 6051 if (paste_diff >= 0 && paste_diff < 450)
c9def1b8 6052 return;
6053 }
887035a5 6054 term->paste_hold = 0;
c9def1b8 6055
887035a5 6056 while (term->paste_pos < term->paste_len) {
8df7a775 6057 int n = 0;
887035a5 6058 while (n + term->paste_pos < term->paste_len) {
92e23ad9 6059 if (term->paste_buffer[term->paste_pos + n++] == '\015')
8df7a775 6060 break;
6061 }
b9d7bcad 6062 if (term->ldisc)
6063 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
887035a5 6064 term->paste_pos += n;
c9def1b8 6065
887035a5 6066 if (term->paste_pos < term->paste_len) {
6067 term->paste_hold = 1;
c9def1b8 6068 return;
6069 }
6070 }
887035a5 6071 sfree(term->paste_buffer);
6072 term->paste_buffer = NULL;
6073 term->paste_len = 0;
c9def1b8 6074}
6075
887035a5 6076static void deselect(Terminal *term)
32874aea 6077{
887035a5 6078 term->selstate = NO_SELECTION;
6079 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
374330e2 6080}
6081
887035a5 6082void term_deselect(Terminal *term)
32874aea 6083{
887035a5 6084 deselect(term);
6085 term_update(term);
374330e2 6086}
fe50e814 6087
887035a5 6088int term_ldisc(Terminal *term, int option)
32874aea 6089{
6090 if (option == LD_ECHO)
887035a5 6091 return term->term_echoing;
32874aea 6092 if (option == LD_EDIT)
887035a5 6093 return term->term_editing;
0965bee0 6094 return FALSE;
6095}
6096
fbf6cb3b 6097int term_data(Terminal *term, int is_stderr, const char *data, int len)
32874aea 6098{
887035a5 6099 bufchain_add(&term->inbuf, data, len);
5471d09a 6100
74aca06d 6101 if (!term->in_term_out) {
6102 term->in_term_out = TRUE;
39934deb 6103 term_reset_cblink(term);
6104 /*
6105 * During drag-selects, we do not process terminal input,
6106 * because the user will want the screen to hold still to
6107 * be selected.
6108 */
6109 if (term->selstate != DRAGGING)
6110 term_out(term);
74aca06d 6111 term->in_term_out = FALSE;
6112 }
6113
5471d09a 6114 /*
a748a096 6115 * term_out() always completely empties inbuf. Therefore,
6116 * there's no reason at all to return anything other than zero
6117 * from this function, because there _can't_ be a question of
6118 * the remote side needing to wait until term_out() has cleared
6119 * a backlog.
6120 *
5471d09a 6121 * This is a slightly suboptimal way to deal with SSH2 - in
6122 * principle, the window mechanism would allow us to continue
6123 * to accept data on forwarded ports and X connections even
6124 * while the terminal processing was going slowly - but we
6125 * can't do the 100% right thing without moving the terminal
6126 * processing into a separate thread, and that might hurt
6127 * portability. So we manage stdout buffering the old SSH1 way:
6128 * if the terminal processing goes slowly, the whole SSH
6129 * connection stops accepting data until it's ready.
a748a096 6130 *
5471d09a 6131 * In practice, I can't imagine this causing serious trouble.
6132 */
6133 return 0;
fe50e814 6134}
a8327734 6135
6136void term_provide_logctx(Terminal *term, void *logctx)
6137{
6138 term->logctx = logctx;
6139}
1cff1320 6140
6141void term_set_focus(Terminal *term, int has_focus)
6142{
6143 term->has_focus = has_focus;
6144 term_schedule_cblink(term);
6145}