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