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