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