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 | { |
36566009 |
1387 | termline *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 | |
887035a5 |
1538 | deselect(term); |
1539 | swap_screen(term, 0, FALSE, FALSE); |
d6832394 |
1540 | |
887035a5 |
1541 | term->alt_t = term->marg_t = 0; |
1542 | term->alt_b = term->marg_b = newrows - 1; |
374330e2 |
1543 | |
887035a5 |
1544 | if (term->rows == -1) { |
1545 | term->scrollback = newtree234(NULL); |
1546 | term->screen = newtree234(NULL); |
4683b9b6 |
1547 | term->tempsblines = 0; |
887035a5 |
1548 | term->rows = 0; |
c83de303 |
1549 | } |
1550 | |
1551 | /* |
1552 | * Resize the screen and scrollback. We only need to shift |
1553 | * lines around within our data structures, because lineptr() |
1554 | * will take care of resizing each individual line if |
1555 | * necessary. So: |
1556 | * |
4683b9b6 |
1557 | * - If the new screen is longer, we shunt lines in from temporary |
1558 | * scrollback if possible, otherwise we add new blank lines at |
1559 | * the bottom. |
1560 | * |
1561 | * - If the new screen is shorter, we remove any blank lines at |
1562 | * the bottom if possible, otherwise shunt lines above the cursor |
1563 | * to scrollback if possible, otherwise delete lines below the |
1564 | * cursor. |
c83de303 |
1565 | * |
1566 | * - Then, if the new scrollback length is less than the |
1567 | * amount of scrollback we actually have, we must throw some |
1568 | * away. |
1569 | */ |
887035a5 |
1570 | sblen = count234(term->scrollback); |
cdd6c586 |
1571 | /* Do this loop to expand the screen if newrows > rows */ |
4683b9b6 |
1572 | assert(term->rows == count234(term->screen)); |
1573 | while (term->rows < newrows) { |
1574 | if (term->tempsblines > 0) { |
36566009 |
1575 | unsigned char *cline; |
4683b9b6 |
1576 | /* Insert a line from the scrollback at the top of the screen. */ |
1577 | assert(sblen >= term->tempsblines); |
36566009 |
1578 | cline = delpos234(term->scrollback, --sblen); |
1579 | line = decompressline(cline, NULL); |
1580 | sfree(cline); |
1581 | line->temporary = FALSE; /* reconstituted line is now real */ |
4683b9b6 |
1582 | term->tempsblines -= 1; |
1583 | addpos234(term->screen, line, 0); |
1584 | term->curs.y += 1; |
1585 | term->savecurs.y += 1; |
32874aea |
1586 | } else { |
4683b9b6 |
1587 | /* Add a new blank line at the bottom of the screen. */ |
36566009 |
1588 | line = newline(term, newcols, FALSE); |
4683b9b6 |
1589 | addpos234(term->screen, line, count234(term->screen)); |
32874aea |
1590 | } |
4683b9b6 |
1591 | term->rows += 1; |
cdd6c586 |
1592 | } |
1593 | /* Do this loop to shrink the screen if newrows < rows */ |
4683b9b6 |
1594 | while (term->rows > newrows) { |
1595 | if (term->curs.y < term->rows - 1) { |
1596 | /* delete bottom row, unless it contains the cursor */ |
1597 | sfree(delpos234(term->screen, term->rows - 1)); |
1598 | } else { |
1599 | /* push top row to scrollback */ |
1600 | line = delpos234(term->screen, 0); |
36566009 |
1601 | addpos234(term->scrollback, compressline(line), sblen++); |
1602 | freeline(line); |
4683b9b6 |
1603 | term->tempsblines += 1; |
1604 | term->curs.y -= 1; |
1605 | term->savecurs.y -= 1; |
1606 | } |
1607 | term->rows -= 1; |
c83de303 |
1608 | } |
4683b9b6 |
1609 | assert(term->rows == newrows); |
887035a5 |
1610 | assert(count234(term->screen) == newrows); |
4683b9b6 |
1611 | |
1612 | /* Delete any excess lines from the scrollback. */ |
c83de303 |
1613 | while (sblen > newsavelines) { |
887035a5 |
1614 | line = delpos234(term->scrollback, 0); |
c83de303 |
1615 | sfree(line); |
1616 | sblen--; |
c9def1b8 |
1617 | } |
4683b9b6 |
1618 | if (sblen < term->tempsblines) |
1619 | term->tempsblines = sblen; |
887035a5 |
1620 | assert(count234(term->scrollback) <= newsavelines); |
4683b9b6 |
1621 | assert(count234(term->scrollback) >= term->tempsblines); |
887035a5 |
1622 | term->disptop = 0; |
374330e2 |
1623 | |
4683b9b6 |
1624 | /* Make a new displayed text buffer. */ |
36566009 |
1625 | newdisp = snewn(newrows, termline *); |
1626 | for (i = 0; i < newrows; i++) { |
1627 | newdisp[i] = newline(term, newcols, FALSE); |
1628 | for (j = 0; j < newcols; j++) |
d701235f |
1629 | newdisp[i]->chars[j].attr = ATTR_INVALID; |
36566009 |
1630 | } |
1631 | if (term->disptext) { |
05bea829 |
1632 | for (i = 0; i < oldrows; i++) |
36566009 |
1633 | freeline(term->disptext[i]); |
1634 | } |
887035a5 |
1635 | sfree(term->disptext); |
1636 | term->disptext = newdisp; |
36566009 |
1637 | term->dispcursx = term->dispcursy = -1; |
374330e2 |
1638 | |
4683b9b6 |
1639 | /* Make a new alternate screen. */ |
4facdf84 |
1640 | newalt = newtree234(NULL); |
32874aea |
1641 | for (i = 0; i < newrows; i++) { |
36566009 |
1642 | line = newline(term, newcols, TRUE); |
4facdf84 |
1643 | addpos234(newalt, line, i); |
1644 | } |
887035a5 |
1645 | if (term->alt_screen) { |
1646 | while (NULL != (line = delpos234(term->alt_screen, 0))) |
36566009 |
1647 | freeline(line); |
887035a5 |
1648 | freetree234(term->alt_screen); |
4facdf84 |
1649 | } |
887035a5 |
1650 | term->alt_screen = newalt; |
876e5d5e |
1651 | term->alt_sblines = 0; |
374330e2 |
1652 | |
3d88e64d |
1653 | term->tabs = sresize(term->tabs, newcols, unsigned char); |
374330e2 |
1654 | { |
1655 | int i; |
887035a5 |
1656 | for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++) |
1657 | term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE); |
374330e2 |
1658 | } |
1659 | |
4683b9b6 |
1660 | /* Check that the cursor positions are still valid. */ |
1661 | if (term->savecurs.y < 0) |
1662 | term->savecurs.y = 0; |
1663 | if (term->savecurs.y >= newrows) |
1664 | term->savecurs.y = newrows - 1; |
887035a5 |
1665 | if (term->curs.y < 0) |
1666 | term->curs.y = 0; |
1667 | if (term->curs.y >= newrows) |
1668 | term->curs.y = newrows - 1; |
1669 | if (term->curs.x >= newcols) |
1670 | term->curs.x = newcols - 1; |
1671 | term->alt_x = term->alt_y = 0; |
1672 | term->wrapnext = term->alt_wnext = FALSE; |
1673 | |
1674 | term->rows = newrows; |
1675 | term->cols = newcols; |
1676 | term->savelines = newsavelines; |
374330e2 |
1677 | |
887035a5 |
1678 | swap_screen(term, save_alt_which, FALSE, FALSE); |
d6832394 |
1679 | |
887035a5 |
1680 | update_sbar(term); |
1681 | term_update(term); |
51470298 |
1682 | if (term->resize_fn) |
1683 | term->resize_fn(term->resize_ctx, term->cols, term->rows); |
1684 | } |
1685 | |
1686 | /* |
1687 | * Hand a function and context pointer to the terminal which it can |
1688 | * use to notify a back end of resizes. |
1689 | */ |
1690 | void term_provide_resize_fn(Terminal *term, |
1691 | void (*resize_fn)(void *, int, int), |
1692 | void *resize_ctx) |
1693 | { |
1694 | term->resize_fn = resize_fn; |
1695 | term->resize_ctx = resize_ctx; |
1696 | if (term->cols > 0 && term->rows > 0) |
1697 | resize_fn(resize_ctx, term->cols, term->rows); |
374330e2 |
1698 | } |
1699 | |
876e5d5e |
1700 | /* Find the bottom line on the screen that has any content. |
1701 | * If only the top line has content, returns 0. |
1702 | * If no lines have content, return -1. |
1703 | */ |
1704 | static int find_last_nonempty_line(Terminal * term, tree234 * screen) |
1705 | { |
1706 | int i; |
1707 | for (i = count234(screen) - 1; i >= 0; i--) { |
36566009 |
1708 | termline *line = index234(screen, i); |
876e5d5e |
1709 | int j; |
c6958dfe |
1710 | for (j = 0; j < line->cols; j++) |
1711 | if (!termchars_equal(&line->chars[j], &term->erase_char)) |
1712 | break; |
36566009 |
1713 | if (j != line->cols) break; |
876e5d5e |
1714 | } |
1715 | return i; |
1716 | } |
1717 | |
374330e2 |
1718 | /* |
8f9b6a1a |
1719 | * Swap screens. If `reset' is TRUE and we have been asked to |
1720 | * switch to the alternate screen, we must bring most of its |
1721 | * configuration from the main screen and erase the contents of the |
1722 | * alternate screen completely. (This is even true if we're already |
1723 | * on it! Blame xterm.) |
374330e2 |
1724 | */ |
887035a5 |
1725 | static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos) |
32874aea |
1726 | { |
374330e2 |
1727 | int t; |
4facdf84 |
1728 | tree234 *ttr; |
374330e2 |
1729 | |
8f9b6a1a |
1730 | if (!which) |
1731 | reset = FALSE; /* do no weird resetting if which==0 */ |
1732 | |
887035a5 |
1733 | if (which != term->alt_which) { |
1734 | term->alt_which = which; |
8f9b6a1a |
1735 | |
887035a5 |
1736 | ttr = term->alt_screen; |
1737 | term->alt_screen = term->screen; |
1738 | term->screen = ttr; |
876e5d5e |
1739 | term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1; |
887035a5 |
1740 | t = term->curs.x; |
8f9b6a1a |
1741 | if (!reset && !keep_cur_pos) |
887035a5 |
1742 | term->curs.x = term->alt_x; |
1743 | term->alt_x = t; |
1744 | t = term->curs.y; |
8f9b6a1a |
1745 | if (!reset && !keep_cur_pos) |
887035a5 |
1746 | term->curs.y = term->alt_y; |
1747 | term->alt_y = t; |
1748 | t = term->marg_t; |
1749 | if (!reset) term->marg_t = term->alt_t; |
1750 | term->alt_t = t; |
1751 | t = term->marg_b; |
1752 | if (!reset) term->marg_b = term->alt_b; |
1753 | term->alt_b = t; |
1754 | t = term->dec_om; |
1755 | if (!reset) term->dec_om = term->alt_om; |
1756 | term->alt_om = t; |
1757 | t = term->wrap; |
1758 | if (!reset) term->wrap = term->alt_wrap; |
1759 | term->alt_wrap = t; |
1760 | t = term->wrapnext; |
1761 | if (!reset) term->wrapnext = term->alt_wnext; |
1762 | term->alt_wnext = t; |
1763 | t = term->insert; |
1764 | if (!reset) term->insert = term->alt_ins; |
1765 | term->alt_ins = t; |
1766 | t = term->cset; |
1767 | if (!reset) term->cset = term->alt_cset; |
1768 | term->alt_cset = t; |
1769 | t = term->utf; |
1770 | if (!reset) term->utf = term->alt_utf; |
1771 | term->alt_utf = t; |
1772 | t = term->sco_acs; |
1773 | if (!reset) term->sco_acs = term->alt_sco_acs; |
1774 | term->alt_sco_acs = t; |
8f9b6a1a |
1775 | } |
374330e2 |
1776 | |
887035a5 |
1777 | if (reset && term->screen) { |
8f9b6a1a |
1778 | /* |
1779 | * Yes, this _is_ supposed to honour background-colour-erase. |
1780 | */ |
887035a5 |
1781 | erase_lots(term, FALSE, TRUE, TRUE); |
8f9b6a1a |
1782 | } |
374330e2 |
1783 | } |
1784 | |
1785 | /* |
374330e2 |
1786 | * Update the scroll bar. |
1787 | */ |
887035a5 |
1788 | static void update_sbar(Terminal *term) |
32874aea |
1789 | { |
876e5d5e |
1790 | int nscroll = sblines(term); |
a8327734 |
1791 | set_sbar(term->frontend, nscroll + term->rows, |
1792 | nscroll + term->disptop, term->rows); |
374330e2 |
1793 | } |
1794 | |
1795 | /* |
1796 | * Check whether the region bounded by the two pointers intersects |
1797 | * the scroll region, and de-select the on-screen selection if so. |
1798 | */ |
887035a5 |
1799 | static void check_selection(Terminal *term, pos from, pos to) |
32874aea |
1800 | { |
887035a5 |
1801 | if (poslt(from, term->selend) && poslt(term->selstart, to)) |
1802 | deselect(term); |
374330e2 |
1803 | } |
1804 | |
1805 | /* |
1806 | * Scroll the screen. (`lines' is +ve for scrolling forward, -ve |
1807 | * for backward.) `sb' is TRUE if the scrolling is permitted to |
1808 | * affect the scrollback buffer. |
1809 | */ |
887035a5 |
1810 | static void scroll(Terminal *term, int topline, int botline, int lines, int sb) |
32874aea |
1811 | { |
36566009 |
1812 | termline *line; |
7c66d7d9 |
1813 | int i, seltop, olddisptop, shift; |
c9def1b8 |
1814 | |
887035a5 |
1815 | if (topline != 0 || term->alt_which != 0) |
4facdf84 |
1816 | sb = FALSE; |
1817 | |
7c66d7d9 |
1818 | olddisptop = term->disptop; |
1819 | shift = lines; |
4facdf84 |
1820 | if (lines < 0) { |
1821 | while (lines < 0) { |
887035a5 |
1822 | line = delpos234(term->screen, botline); |
36566009 |
1823 | resizeline(term, line, term->cols); |
887035a5 |
1824 | for (i = 0; i < term->cols; i++) |
c6958dfe |
1825 | copy_termchar(line, i, &term->erase_char); |
36566009 |
1826 | line->lattr = LATTR_NORM; |
887035a5 |
1827 | addpos234(term->screen, line, topline); |
1828 | |
1829 | if (term->selstart.y >= topline && term->selstart.y <= botline) { |
1830 | term->selstart.y++; |
1831 | if (term->selstart.y > botline) { |
5a8afc78 |
1832 | term->selstart.y = botline + 1; |
887035a5 |
1833 | term->selstart.x = 0; |
c9def1b8 |
1834 | } |
c9def1b8 |
1835 | } |
887035a5 |
1836 | if (term->selend.y >= topline && term->selend.y <= botline) { |
1837 | term->selend.y++; |
1838 | if (term->selend.y > botline) { |
5a8afc78 |
1839 | term->selend.y = botline + 1; |
887035a5 |
1840 | term->selend.x = 0; |
c9def1b8 |
1841 | } |
c9def1b8 |
1842 | } |
c9def1b8 |
1843 | |
4facdf84 |
1844 | lines++; |
c9def1b8 |
1845 | } |
374330e2 |
1846 | } else { |
4facdf84 |
1847 | while (lines > 0) { |
887035a5 |
1848 | line = delpos234(term->screen, topline); |
96cfc9b9 |
1849 | #ifdef TERM_CC_DIAGS |
1850 | cc_check(line); |
1851 | #endif |
887035a5 |
1852 | if (sb && term->savelines > 0) { |
4683b9b6 |
1853 | int sblen = count234(term->scrollback); |
4facdf84 |
1854 | /* |
1855 | * We must add this line to the scrollback. We'll |
36566009 |
1856 | * remove a line from the top of the scrollback if |
1857 | * the scrollback is full. |
4facdf84 |
1858 | */ |
887035a5 |
1859 | if (sblen == term->savelines) { |
36566009 |
1860 | unsigned char *cline; |
1861 | |
1862 | sblen--; |
1863 | cline = delpos234(term->scrollback, 0); |
1864 | sfree(cline); |
1865 | } else |
4683b9b6 |
1866 | term->tempsblines += 1; |
36566009 |
1867 | |
1868 | addpos234(term->scrollback, compressline(line), sblen); |
1869 | |
dac0aee3 |
1870 | /* now `line' itself can be reused as the bottom line */ |
a792432e |
1871 | |
1872 | /* |
1873 | * If the user is currently looking at part of the |
1874 | * scrollback, and they haven't enabled any options |
1875 | * that are going to reset the scrollback as a |
1876 | * result of this movement, then the chances are |
1877 | * they'd like to keep looking at the same line. So |
1878 | * we move their viewpoint at the same rate as the |
1879 | * scroll, at least until their viewpoint hits the |
1880 | * top end of the scrollback buffer, at which point |
1881 | * we don't have the choice any more. |
1882 | * |
1883 | * Thanks to Jan Holmen Holsten for the idea and |
1884 | * initial implementation. |
1885 | */ |
887035a5 |
1886 | if (term->disptop > -term->savelines && term->disptop < 0) |
1887 | term->disptop--; |
4facdf84 |
1888 | } |
36566009 |
1889 | resizeline(term, line, term->cols); |
887035a5 |
1890 | for (i = 0; i < term->cols; i++) |
c6958dfe |
1891 | copy_termchar(line, i, &term->erase_char); |
36566009 |
1892 | line->lattr = LATTR_NORM; |
887035a5 |
1893 | addpos234(term->screen, line, botline); |
4facdf84 |
1894 | |
a792432e |
1895 | /* |
1896 | * If the selection endpoints move into the scrollback, |
1897 | * we keep them moving until they hit the top. However, |
1898 | * of course, if the line _hasn't_ moved into the |
1899 | * scrollback then we don't do this, and cut them off |
1900 | * at the top of the scroll region. |
d48db8b4 |
1901 | * |
1902 | * This applies to selstart and selend (for an existing |
1903 | * selection), and also selanchor (for one being |
1904 | * selected as we speak). |
a792432e |
1905 | */ |
887035a5 |
1906 | seltop = sb ? -term->savelines : topline; |
1907 | |
c989dbc4 |
1908 | if (term->selstate != NO_SELECTION) { |
1909 | if (term->selstart.y >= seltop && |
1910 | term->selstart.y <= botline) { |
1911 | term->selstart.y--; |
1912 | if (term->selstart.y < seltop) { |
1913 | term->selstart.y = seltop; |
1914 | term->selstart.x = 0; |
1915 | } |
4facdf84 |
1916 | } |
c989dbc4 |
1917 | if (term->selend.y >= seltop && term->selend.y <= botline) { |
1918 | term->selend.y--; |
1919 | if (term->selend.y < seltop) { |
1920 | term->selend.y = seltop; |
1921 | term->selend.x = 0; |
1922 | } |
4facdf84 |
1923 | } |
c989dbc4 |
1924 | if (term->selanchor.y >= seltop && |
1925 | term->selanchor.y <= botline) { |
1926 | term->selanchor.y--; |
1927 | if (term->selanchor.y < seltop) { |
1928 | term->selanchor.y = seltop; |
1929 | term->selanchor.x = 0; |
1930 | } |
d48db8b4 |
1931 | } |
1932 | } |
4facdf84 |
1933 | |
1934 | lines--; |
c9def1b8 |
1935 | } |
374330e2 |
1936 | } |
7c66d7d9 |
1937 | #ifdef OPTIMISE_SCROLL |
1938 | shift += term->disptop - olddisptop; |
1939 | if (shift < term->rows && shift > -term->rows && shift != 0) |
1940 | scroll_display(term, topline, botline, shift); |
1941 | #endif /* OPTIMISE_SCROLL */ |
374330e2 |
1942 | } |
1943 | |
37d2a505 |
1944 | #ifdef OPTIMISE_SCROLL |
1945 | /* |
341eb978 |
1946 | * Add a scroll of a region on the screen into the pending scroll list. |
1947 | * `lines' is +ve for scrolling forward, -ve for backward. |
1948 | * |
1949 | * If the scroll is on the same area as the last scroll in the list, |
1950 | * merge them. |
1951 | */ |
03f60a62 |
1952 | static void save_scroll(Terminal *term, int topline, int botline, int lines) |
341eb978 |
1953 | { |
1954 | struct scrollregion *newscroll; |
1955 | if (term->scrolltail && |
1956 | term->scrolltail->topline == topline && |
1957 | term->scrolltail->botline == botline) { |
1958 | term->scrolltail->lines += lines; |
1959 | } else { |
3d88e64d |
1960 | newscroll = snew(struct scrollregion); |
341eb978 |
1961 | newscroll->topline = topline; |
1962 | newscroll->botline = botline; |
1963 | newscroll->lines = lines; |
1964 | newscroll->next = NULL; |
1965 | |
1966 | if (!term->scrollhead) |
1967 | term->scrollhead = newscroll; |
1968 | else |
1969 | term->scrolltail->next = newscroll; |
1970 | term->scrolltail = newscroll; |
1971 | } |
1972 | } |
1973 | |
1974 | /* |
37d2a505 |
1975 | * Scroll the physical display, and our conception of it in disptext. |
1976 | */ |
1977 | static void scroll_display(Terminal *term, int topline, int botline, int lines) |
1978 | { |
c6958dfe |
1979 | int distance, nlines, i, j; |
37d2a505 |
1980 | |
36566009 |
1981 | distance = lines > 0 ? lines : -lines; |
1982 | nlines = botline - topline + 1 - distance; |
37d2a505 |
1983 | if (lines > 0) { |
36566009 |
1984 | for (i = 0; i < nlines; i++) |
c6958dfe |
1985 | for (j = 0; j < term->cols; j++) |
828bb999 |
1986 | copy_termchar(term->disptext[i], j, |
1987 | term->disptext[i+distance]->chars+j); |
36566009 |
1988 | if (term->dispcursy >= 0 && |
1989 | term->dispcursy >= topline + distance && |
1990 | term->dispcursy < topline + distance + nlines) |
1991 | term->dispcursy -= distance; |
37d2a505 |
1992 | for (i = 0; i < distance; i++) |
36566009 |
1993 | for (j = 0; j < term->cols; j++) |
828bb999 |
1994 | term->disptext[nlines+i]->chars[j].attr |= ATTR_INVALID; |
37d2a505 |
1995 | } else { |
36566009 |
1996 | for (i = nlines; i-- ;) |
c6958dfe |
1997 | for (j = 0; j < term->cols; j++) |
828bb999 |
1998 | copy_termchar(term->disptext[i+distance], j, |
1999 | term->disptext[i]->chars+j); |
36566009 |
2000 | if (term->dispcursy >= 0 && |
2001 | term->dispcursy >= topline && |
2002 | term->dispcursy < topline + nlines) |
2003 | term->dispcursy += distance; |
37d2a505 |
2004 | for (i = 0; i < distance; i++) |
36566009 |
2005 | for (j = 0; j < term->cols; j++) |
828bb999 |
2006 | term->disptext[i]->chars[j].attr |= ATTR_INVALID; |
37d2a505 |
2007 | } |
341eb978 |
2008 | save_scroll(term, topline, botline, lines); |
37d2a505 |
2009 | } |
2010 | #endif /* OPTIMISE_SCROLL */ |
2011 | |
374330e2 |
2012 | /* |
2013 | * Move the cursor to a given position, clipping at boundaries. We |
2014 | * may or may not want to clip at the scroll margin: marg_clip is 0 |
2015 | * not to, 1 to disallow _passing_ the margins, and 2 to disallow |
2016 | * even _being_ outside the margins. |
2017 | */ |
887035a5 |
2018 | static void move(Terminal *term, int x, int y, int marg_clip) |
32874aea |
2019 | { |
374330e2 |
2020 | if (x < 0) |
2021 | x = 0; |
887035a5 |
2022 | if (x >= term->cols) |
2023 | x = term->cols - 1; |
374330e2 |
2024 | if (marg_clip) { |
887035a5 |
2025 | if ((term->curs.y >= term->marg_t || marg_clip == 2) && |
2026 | y < term->marg_t) |
2027 | y = term->marg_t; |
2028 | if ((term->curs.y <= term->marg_b || marg_clip == 2) && |
2029 | y > term->marg_b) |
2030 | y = term->marg_b; |
374330e2 |
2031 | } |
2032 | if (y < 0) |
2033 | y = 0; |
887035a5 |
2034 | if (y >= term->rows) |
2035 | y = term->rows - 1; |
2036 | term->curs.x = x; |
2037 | term->curs.y = y; |
887035a5 |
2038 | term->wrapnext = FALSE; |
374330e2 |
2039 | } |
2040 | |
2041 | /* |
2042 | * Save or restore the cursor and SGR mode. |
2043 | */ |
887035a5 |
2044 | static void save_cursor(Terminal *term, int save) |
32874aea |
2045 | { |
374330e2 |
2046 | if (save) { |
887035a5 |
2047 | term->savecurs = term->curs; |
2048 | term->save_attr = term->curr_attr; |
2049 | term->save_cset = term->cset; |
2050 | term->save_utf = term->utf; |
2051 | term->save_wnext = term->wrapnext; |
2052 | term->save_csattr = term->cset_attr[term->cset]; |
2053 | term->save_sco_acs = term->sco_acs; |
374330e2 |
2054 | } else { |
887035a5 |
2055 | term->curs = term->savecurs; |
c9def1b8 |
2056 | /* Make sure the window hasn't shrunk since the save */ |
887035a5 |
2057 | if (term->curs.x >= term->cols) |
2058 | term->curs.x = term->cols - 1; |
2059 | if (term->curs.y >= term->rows) |
2060 | term->curs.y = term->rows - 1; |
2061 | |
2062 | term->curr_attr = term->save_attr; |
2063 | term->cset = term->save_cset; |
2064 | term->utf = term->save_utf; |
2065 | term->wrapnext = term->save_wnext; |
8c028e65 |
2066 | /* |
2067 | * wrapnext might reset to False if the x position is no |
2068 | * longer at the rightmost edge. |
2069 | */ |
887035a5 |
2070 | if (term->wrapnext && term->curs.x < term->cols-1) |
2071 | term->wrapnext = FALSE; |
2072 | term->cset_attr[term->cset] = term->save_csattr; |
2073 | term->sco_acs = term->save_sco_acs; |
36566009 |
2074 | set_erase_char(term); |
374330e2 |
2075 | } |
2076 | } |
2077 | |
2078 | /* |
3c7366f8 |
2079 | * This function is called before doing _anything_ which affects |
2080 | * only part of a line of text. It is used to mark the boundary |
2081 | * between two character positions, and it indicates that some sort |
2082 | * of effect is going to happen on only one side of that boundary. |
2083 | * |
2084 | * The effect of this function is to check whether a CJK |
2085 | * double-width character is straddling the boundary, and to remove |
2086 | * it and replace it with two spaces if so. (Of course, one or |
2087 | * other of those spaces is then likely to be replaced with |
2088 | * something else again, as a result of whatever happens next.) |
2089 | * |
2090 | * Also, if the boundary is at the right-hand _edge_ of the screen, |
2091 | * it implies something deliberate is being done to the rightmost |
2092 | * column position; hence we must clear LATTR_WRAPPED2. |
2093 | * |
2094 | * The input to the function is the coordinates of the _second_ |
2095 | * character of the pair. |
2096 | */ |
2097 | static void check_boundary(Terminal *term, int x, int y) |
2098 | { |
36566009 |
2099 | termline *ldata; |
3c7366f8 |
2100 | |
2101 | /* Validate input coordinates, just in case. */ |
2102 | if (x == 0 || x > term->cols) |
2103 | return; |
2104 | |
36566009 |
2105 | ldata = scrlineptr(y); |
3c7366f8 |
2106 | if (x == term->cols) { |
36566009 |
2107 | ldata->lattr &= ~LATTR_WRAPPED2; |
3c7366f8 |
2108 | } else { |
36566009 |
2109 | if (ldata->chars[x].chr == UCSWIDE) { |
c6958dfe |
2110 | clear_cc(ldata, x-1); |
2111 | clear_cc(ldata, x); |
36566009 |
2112 | ldata->chars[x-1].chr = ' ' | CSET_ASCII; |
2113 | ldata->chars[x] = ldata->chars[x-1]; |
3c7366f8 |
2114 | } |
2115 | } |
2116 | } |
2117 | |
2118 | /* |
374330e2 |
2119 | * Erase a large portion of the screen: the whole screen, or the |
2120 | * whole line, or parts thereof. |
2121 | */ |
887035a5 |
2122 | static void erase_lots(Terminal *term, |
2123 | int line_only, int from_begin, int to_end) |
32874aea |
2124 | { |
260f3dec |
2125 | pos start, end; |
4facdf84 |
2126 | int erase_lattr; |
4683b9b6 |
2127 | int erasing_lines_from_top = 0; |
374330e2 |
2128 | |
2129 | if (line_only) { |
887035a5 |
2130 | start.y = term->curs.y; |
4facdf84 |
2131 | start.x = 0; |
887035a5 |
2132 | end.y = term->curs.y + 1; |
4facdf84 |
2133 | end.x = 0; |
2134 | erase_lattr = FALSE; |
374330e2 |
2135 | } else { |
4facdf84 |
2136 | start.y = 0; |
2137 | start.x = 0; |
887035a5 |
2138 | end.y = term->rows; |
4facdf84 |
2139 | end.x = 0; |
2140 | erase_lattr = TRUE; |
2141 | } |
2142 | if (!from_begin) { |
887035a5 |
2143 | start = term->curs; |
374330e2 |
2144 | } |
4facdf84 |
2145 | if (!to_end) { |
887035a5 |
2146 | end = term->curs; |
2a6dfe0e |
2147 | incpos(end); |
4facdf84 |
2148 | } |
3c7366f8 |
2149 | if (!from_begin || !to_end) |
2150 | check_boundary(term, term->curs.x, term->curs.y); |
887035a5 |
2151 | check_selection(term, start, end); |
e14a5a13 |
2152 | |
2153 | /* Clear screen also forces a full window redraw, just in case. */ |
887035a5 |
2154 | if (start.y == 0 && start.x == 0 && end.y == term->rows) |
2155 | term_invalidate(term); |
e14a5a13 |
2156 | |
4683b9b6 |
2157 | /* Lines scrolled away shouldn't be brought back on if the terminal |
2158 | * resizes. */ |
2159 | if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr) |
2160 | erasing_lines_from_top = 1; |
2161 | |
2162 | if (term->cfg.erase_to_scrollback && erasing_lines_from_top) { |
876e5d5e |
2163 | /* If it's a whole number of lines, starting at the top, and |
2164 | * we're fully erasing them, erase by scrolling and keep the |
2165 | * lines in the scrollback. */ |
2166 | int scrolllines = end.y; |
2167 | if (end.y == term->rows) { |
2168 | /* Shrink until we find a non-empty row.*/ |
2169 | scrolllines = find_last_nonempty_line(term, term->screen) + 1; |
2170 | } |
2171 | if (scrolllines > 0) |
2172 | scroll(term, 0, scrolllines - 1, scrolllines, TRUE); |
876e5d5e |
2173 | } else { |
36566009 |
2174 | termline *ldata = scrlineptr(start.y); |
876e5d5e |
2175 | while (poslt(start, end)) { |
2176 | if (start.x == term->cols) { |
2177 | if (!erase_lattr) |
36566009 |
2178 | ldata->lattr &= ~(LATTR_WRAPPED | LATTR_WRAPPED2); |
876e5d5e |
2179 | else |
36566009 |
2180 | ldata->lattr = LATTR_NORM; |
876e5d5e |
2181 | } else { |
c6958dfe |
2182 | copy_termchar(ldata, start.x, &term->erase_char); |
36566009 |
2183 | } |
2184 | if (incpos(start) && start.y < term->rows) { |
2185 | ldata = scrlineptr(start.y); |
876e5d5e |
2186 | } |
876e5d5e |
2187 | } |
4facdf84 |
2188 | } |
4683b9b6 |
2189 | |
2190 | /* After an erase of lines from the top of the screen, we shouldn't |
2191 | * bring the lines back again if the terminal enlarges (since the user or |
2192 | * application has explictly thrown them away). */ |
2193 | if (erasing_lines_from_top && !(term->alt_which)) |
2194 | term->tempsblines = 0; |
374330e2 |
2195 | } |
2196 | |
2197 | /* |
2198 | * Insert or delete characters within the current line. n is +ve if |
2199 | * insertion is desired, and -ve for deletion. |
2200 | */ |
887035a5 |
2201 | static void insch(Terminal *term, int n) |
32874aea |
2202 | { |
374330e2 |
2203 | int dir = (n < 0 ? -1 : +1); |
c6958dfe |
2204 | int m, j; |
4facdf84 |
2205 | pos cursplus; |
36566009 |
2206 | termline *ldata; |
374330e2 |
2207 | |
2208 | n = (n < 0 ? -n : n); |
887035a5 |
2209 | if (n > term->cols - term->curs.x) |
2210 | n = term->cols - term->curs.x; |
2211 | m = term->cols - term->curs.x - n; |
2212 | cursplus.y = term->curs.y; |
2213 | cursplus.x = term->curs.x + n; |
2214 | check_selection(term, term->curs, cursplus); |
3c7366f8 |
2215 | check_boundary(term, term->curs.x, term->curs.y); |
2216 | if (dir < 0) |
2217 | check_boundary(term, term->curs.x + n, term->curs.y); |
36566009 |
2218 | ldata = scrlineptr(term->curs.y); |
374330e2 |
2219 | if (dir < 0) { |
c6958dfe |
2220 | for (j = 0; j < m; j++) |
2221 | move_termchar(ldata, |
2222 | ldata->chars + term->curs.x + j, |
2223 | ldata->chars + term->curs.x + j + n); |
374330e2 |
2224 | while (n--) |
c6958dfe |
2225 | copy_termchar(ldata, term->curs.x + m++, &term->erase_char); |
374330e2 |
2226 | } else { |
c6958dfe |
2227 | for (j = m; j-- ;) |
2228 | move_termchar(ldata, |
2229 | ldata->chars + term->curs.x + j + n, |
2230 | ldata->chars + term->curs.x + j); |
374330e2 |
2231 | while (n--) |
c6958dfe |
2232 | copy_termchar(ldata, term->curs.x + n, &term->erase_char); |
374330e2 |
2233 | } |
2234 | } |
2235 | |
2236 | /* |
2237 | * Toggle terminal mode `mode' to state `state'. (`query' indicates |
2238 | * whether the mode is a DEC private one or a normal one.) |
2239 | */ |
887035a5 |
2240 | static void toggle_mode(Terminal *term, int mode, int query, int state) |
32874aea |
2241 | { |
32874aea |
2242 | if (query) |
2243 | switch (mode) { |
a263e4f6 |
2244 | case 1: /* DECCKM: application cursor keys */ |
887035a5 |
2245 | term->app_cursor_keys = state; |
32874aea |
2246 | break; |
a263e4f6 |
2247 | case 2: /* DECANM: VT52 mode */ |
887035a5 |
2248 | term->vt52_mode = !state; |
2249 | if (term->vt52_mode) { |
2250 | term->blink_is_real = FALSE; |
2251 | term->vt52_bold = FALSE; |
4eeb7d09 |
2252 | } else { |
64734920 |
2253 | term->blink_is_real = term->cfg.blinktext; |
4eeb7d09 |
2254 | } |
39934deb |
2255 | term_schedule_tblink(term); |
32874aea |
2256 | break; |
a263e4f6 |
2257 | case 3: /* DECCOLM: 80/132 columns */ |
887035a5 |
2258 | deselect(term); |
64734920 |
2259 | if (!term->cfg.no_remote_resize) |
a8327734 |
2260 | request_resize(term->frontend, state ? 132 : 80, term->rows); |
887035a5 |
2261 | term->reset_132 = state; |
7ce3b333 |
2262 | term->alt_t = term->marg_t = 0; |
2263 | term->alt_b = term->marg_b = term->rows - 1; |
2264 | move(term, 0, 0, 0); |
2265 | erase_lots(term, FALSE, TRUE, TRUE); |
32874aea |
2266 | break; |
a263e4f6 |
2267 | case 5: /* DECSCNM: reverse video */ |
32874aea |
2268 | /* |
2269 | * Toggle reverse video. If we receive an OFF within the |
2270 | * visual bell timeout period after an ON, we trigger an |
2271 | * effective visual bell, so that ESC[?5hESC[?5l will |
2272 | * always be an actually _visible_ visual bell. |
2273 | */ |
39934deb |
2274 | if (term->rvideo && !state) { |
2275 | /* This is an OFF, so set up a vbell */ |
2276 | term_schedule_vbell(term, TRUE, term->rvbell_startpoint); |
887035a5 |
2277 | } else if (!term->rvideo && state) { |
32874aea |
2278 | /* This is an ON, so we notice the time and save it. */ |
39934deb |
2279 | term->rvbell_startpoint = GETTICKCOUNT(); |
32874aea |
2280 | } |
887035a5 |
2281 | term->rvideo = state; |
39934deb |
2282 | seen_disp_event(term); |
32874aea |
2283 | break; |
a263e4f6 |
2284 | case 6: /* DECOM: DEC origin mode */ |
887035a5 |
2285 | term->dec_om = state; |
32874aea |
2286 | break; |
a263e4f6 |
2287 | case 7: /* DECAWM: auto wrap */ |
887035a5 |
2288 | term->wrap = state; |
32874aea |
2289 | break; |
a263e4f6 |
2290 | case 8: /* DECARM: auto key repeat */ |
887035a5 |
2291 | term->repeat_off = !state; |
32874aea |
2292 | break; |
a263e4f6 |
2293 | case 10: /* DECEDM: set local edit mode */ |
887035a5 |
2294 | term->term_editing = state; |
b9d7bcad |
2295 | if (term->ldisc) /* cause ldisc to notice changes */ |
2296 | ldisc_send(term->ldisc, NULL, 0, 0); |
32874aea |
2297 | break; |
a263e4f6 |
2298 | case 25: /* DECTCEM: enable/disable cursor */ |
32874aea |
2299 | compatibility2(OTHER, VT220); |
887035a5 |
2300 | term->cursor_on = state; |
39934deb |
2301 | seen_disp_event(term); |
32874aea |
2302 | break; |
2303 | case 47: /* alternate screen */ |
2304 | compatibility(OTHER); |
887035a5 |
2305 | deselect(term); |
64734920 |
2306 | swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE); |
887035a5 |
2307 | term->disptop = 0; |
32874aea |
2308 | break; |
2309 | case 1000: /* xterm mouse 1 */ |
887035a5 |
2310 | term->xterm_mouse = state ? 1 : 0; |
a8327734 |
2311 | set_raw_mouse_mode(term->frontend, state); |
32874aea |
2312 | break; |
2313 | case 1002: /* xterm mouse 2 */ |
887035a5 |
2314 | term->xterm_mouse = state ? 2 : 0; |
a8327734 |
2315 | set_raw_mouse_mode(term->frontend, state); |
32874aea |
2316 | break; |
8f9b6a1a |
2317 | case 1047: /* alternate screen */ |
2318 | compatibility(OTHER); |
887035a5 |
2319 | deselect(term); |
64734920 |
2320 | swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE); |
887035a5 |
2321 | term->disptop = 0; |
8f9b6a1a |
2322 | break; |
2323 | case 1048: /* save/restore cursor */ |
4c80361f |
2324 | if (!term->cfg.no_alt_screen) |
2325 | save_cursor(term, state); |
39934deb |
2326 | if (!state) seen_disp_event(term); |
8f9b6a1a |
2327 | break; |
2328 | case 1049: /* cursor & alternate screen */ |
4c80361f |
2329 | if (state && !term->cfg.no_alt_screen) |
887035a5 |
2330 | save_cursor(term, state); |
39934deb |
2331 | if (!state) seen_disp_event(term); |
8f9b6a1a |
2332 | compatibility(OTHER); |
887035a5 |
2333 | deselect(term); |
64734920 |
2334 | swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE); |
4c80361f |
2335 | if (!state && !term->cfg.no_alt_screen) |
887035a5 |
2336 | save_cursor(term, state); |
2337 | term->disptop = 0; |
8f9b6a1a |
2338 | break; |
32874aea |
2339 | } else |
2340 | switch (mode) { |
a263e4f6 |
2341 | case 4: /* IRM: set insert mode */ |
32874aea |
2342 | compatibility(VT102); |
887035a5 |
2343 | term->insert = state; |
32874aea |
2344 | break; |
a263e4f6 |
2345 | case 12: /* SRM: set echo mode */ |
887035a5 |
2346 | term->term_echoing = !state; |
b9d7bcad |
2347 | if (term->ldisc) /* cause ldisc to notice changes */ |
2348 | ldisc_send(term->ldisc, NULL, 0, 0); |
32874aea |
2349 | break; |
a263e4f6 |
2350 | case 20: /* LNM: Return sends ... */ |
887035a5 |
2351 | term->cr_lf_return = state; |
32874aea |
2352 | break; |
a263e4f6 |
2353 | case 34: /* WYULCURM: Make cursor BIG */ |
4eeb7d09 |
2354 | compatibility2(OTHER, VT220); |
887035a5 |
2355 | term->big_cursor = !state; |
7594731b |
2356 | } |
374330e2 |
2357 | } |
2358 | |
2359 | /* |
2360 | * Process an OSC sequence: set window title or icon name. |
2361 | */ |
887035a5 |
2362 | static void do_osc(Terminal *term) |
32874aea |
2363 | { |
887035a5 |
2364 | if (term->osc_w) { |
2365 | while (term->osc_strlen--) |
2366 | term->wordness[(unsigned char) |
2367 | term->osc_string[term->osc_strlen]] = term->esc_args[0]; |
374330e2 |
2368 | } else { |
887035a5 |
2369 | term->osc_string[term->osc_strlen] = '\0'; |
2370 | switch (term->esc_args[0]) { |
374330e2 |
2371 | case 0: |
2372 | case 1: |
64734920 |
2373 | if (!term->cfg.no_remote_wintitle) |
a8327734 |
2374 | set_icon(term->frontend, term->osc_string); |
887035a5 |
2375 | if (term->esc_args[0] == 1) |
374330e2 |
2376 | break; |
2377 | /* fall through: parameter 0 means set both */ |
2378 | case 2: |
2379 | case 21: |
64734920 |
2380 | if (!term->cfg.no_remote_wintitle) |
a8327734 |
2381 | set_title(term->frontend, term->osc_string); |
374330e2 |
2382 | break; |
2383 | } |
2384 | } |
2385 | } |
2386 | |
2387 | /* |
b44b307a |
2388 | * ANSI printing routines. |
2389 | */ |
887035a5 |
2390 | static void term_print_setup(Terminal *term) |
b44b307a |
2391 | { |
887035a5 |
2392 | bufchain_clear(&term->printer_buf); |
64734920 |
2393 | term->print_job = printer_start_job(term->cfg.printer); |
b44b307a |
2394 | } |
887035a5 |
2395 | static void term_print_flush(Terminal *term) |
b44b307a |
2396 | { |
2397 | void *data; |
2398 | int len; |
2399 | int size; |
887035a5 |
2400 | while ((size = bufchain_size(&term->printer_buf)) > 5) { |
2401 | bufchain_prefix(&term->printer_buf, &data, &len); |
b44b307a |
2402 | if (len > size-5) |
2403 | len = size-5; |
887035a5 |
2404 | printer_job_data(term->print_job, data, len); |
2405 | bufchain_consume(&term->printer_buf, len); |
b44b307a |
2406 | } |
2407 | } |
887035a5 |
2408 | static void term_print_finish(Terminal *term) |
b44b307a |
2409 | { |
2410 | void *data; |
2411 | int len, size; |
2412 | char c; |
2413 | |
887035a5 |
2414 | if (!term->printing && !term->only_printing) |
1709795f |
2415 | return; /* we need do nothing */ |
2416 | |
887035a5 |
2417 | term_print_flush(term); |
2418 | while ((size = bufchain_size(&term->printer_buf)) > 0) { |
2419 | bufchain_prefix(&term->printer_buf, &data, &len); |
b44b307a |
2420 | c = *(char *)data; |
2421 | if (c == '\033' || c == '\233') { |
887035a5 |
2422 | bufchain_consume(&term->printer_buf, size); |
b44b307a |
2423 | break; |
2424 | } else { |
887035a5 |
2425 | printer_job_data(term->print_job, &c, 1); |
2426 | bufchain_consume(&term->printer_buf, 1); |
b44b307a |
2427 | } |
2428 | } |
887035a5 |
2429 | printer_finish_job(term->print_job); |
2430 | term->print_job = NULL; |
2431 | term->printing = term->only_printing = FALSE; |
b44b307a |
2432 | } |
2433 | |
2434 | /* |
374330e2 |
2435 | * Remove everything currently in `inbuf' and stick it up on the |
2436 | * in-memory display. There's a big state machine in here to |
2437 | * process escape sequences... |
2438 | */ |
39934deb |
2439 | static void term_out(Terminal *term) |
32874aea |
2440 | { |
36566009 |
2441 | unsigned long c; |
2442 | int unget; |
a748a096 |
2443 | unsigned char localbuf[256], *chars; |
2444 | int nchars = 0; |
2445 | |
2446 | unget = -1; |
2447 | |
1709795f |
2448 | chars = NULL; /* placate compiler warnings */ |
7ecefce8 |
2449 | while (nchars > 0 || unget != -1 || bufchain_size(&term->inbuf) > 0) { |
a748a096 |
2450 | if (unget == -1) { |
2451 | if (nchars == 0) { |
2452 | void *ret; |
887035a5 |
2453 | bufchain_prefix(&term->inbuf, &ret, &nchars); |
a748a096 |
2454 | if (nchars > sizeof(localbuf)) |
2455 | nchars = sizeof(localbuf); |
2456 | memcpy(localbuf, ret, nchars); |
887035a5 |
2457 | bufchain_consume(&term->inbuf, nchars); |
a748a096 |
2458 | chars = localbuf; |
2459 | assert(chars != NULL); |
2460 | } |
2461 | c = *chars++; |
2462 | nchars--; |
c9def1b8 |
2463 | |
a748a096 |
2464 | /* |
2465 | * Optionally log the session traffic to a file. Useful for |
2466 | * debugging and possibly also useful for actual logging. |
2467 | */ |
64734920 |
2468 | if (term->cfg.logtype == LGTYP_DEBUG && term->logctx) |
a8327734 |
2469 | logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG); |
a748a096 |
2470 | } else { |
2471 | c = unget; |
2472 | unget = -1; |
5a16642f |
2473 | } |
2474 | |
e14a5a13 |
2475 | /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even |
2476 | * be able to display 8-bit characters, but I'll let that go 'cause |
2477 | * of i18n. |
2478 | */ |
4eeb7d09 |
2479 | |
b44b307a |
2480 | /* |
2481 | * If we're printing, add the character to the printer |
2482 | * buffer. |
2483 | */ |
887035a5 |
2484 | if (term->printing) { |
2485 | bufchain_add(&term->printer_buf, &c, 1); |
b44b307a |
2486 | |
2487 | /* |
2488 | * If we're in print-only mode, we use a much simpler |
2489 | * state machine designed only to recognise the ESC[4i |
2490 | * termination sequence. |
2491 | */ |
887035a5 |
2492 | if (term->only_printing) { |
b44b307a |
2493 | if (c == '\033') |
887035a5 |
2494 | term->print_state = 1; |
b44b307a |
2495 | else if (c == (unsigned char)'\233') |
887035a5 |
2496 | term->print_state = 2; |
2497 | else if (c == '[' && term->print_state == 1) |
2498 | term->print_state = 2; |
2499 | else if (c == '4' && term->print_state == 2) |
2500 | term->print_state = 3; |
2501 | else if (c == 'i' && term->print_state == 3) |
2502 | term->print_state = 4; |
b44b307a |
2503 | else |
887035a5 |
2504 | term->print_state = 0; |
2505 | if (term->print_state == 4) { |
2506 | term_print_finish(term); |
b44b307a |
2507 | } |
2508 | continue; |
2509 | } |
2510 | } |
2511 | |
4eeb7d09 |
2512 | /* First see about all those translations. */ |
887035a5 |
2513 | if (term->termstate == TOPLEVEL) { |
2514 | if (in_utf(term)) |
2515 | switch (term->utf_state) { |
4eeb7d09 |
2516 | case 0: |
2517 | if (c < 0x80) { |
8f22582c |
2518 | /* UTF-8 must be stateless so we ignore iso2022. */ |
21d2b241 |
2519 | if (term->ucsdata->unitab_ctrl[c] != 0xFF) |
2520 | c = term->ucsdata->unitab_ctrl[c]; |
36566009 |
2521 | else c = ((unsigned char)c) | CSET_ASCII; |
8f22582c |
2522 | break; |
4eeb7d09 |
2523 | } else if ((c & 0xe0) == 0xc0) { |
887035a5 |
2524 | term->utf_size = term->utf_state = 1; |
2525 | term->utf_char = (c & 0x1f); |
4eeb7d09 |
2526 | } else if ((c & 0xf0) == 0xe0) { |
887035a5 |
2527 | term->utf_size = term->utf_state = 2; |
2528 | term->utf_char = (c & 0x0f); |
4eeb7d09 |
2529 | } else if ((c & 0xf8) == 0xf0) { |
887035a5 |
2530 | term->utf_size = term->utf_state = 3; |
2531 | term->utf_char = (c & 0x07); |
4eeb7d09 |
2532 | } else if ((c & 0xfc) == 0xf8) { |
887035a5 |
2533 | term->utf_size = term->utf_state = 4; |
2534 | term->utf_char = (c & 0x03); |
4eeb7d09 |
2535 | } else if ((c & 0xfe) == 0xfc) { |
887035a5 |
2536 | term->utf_size = term->utf_state = 5; |
2537 | term->utf_char = (c & 0x01); |
4eeb7d09 |
2538 | } else { |
2539 | c = UCSERR; |
2540 | break; |
2541 | } |
2542 | continue; |
2543 | case 1: |
2544 | case 2: |
2545 | case 3: |
2546 | case 4: |
2547 | case 5: |
2548 | if ((c & 0xC0) != 0x80) { |
a748a096 |
2549 | unget = c; |
5a16642f |
2550 | c = UCSERR; |
887035a5 |
2551 | term->utf_state = 0; |
4eeb7d09 |
2552 | break; |
2553 | } |
887035a5 |
2554 | term->utf_char = (term->utf_char << 6) | (c & 0x3f); |
2555 | if (--term->utf_state) |
4eeb7d09 |
2556 | continue; |
2557 | |
887035a5 |
2558 | c = term->utf_char; |
4eeb7d09 |
2559 | |
2560 | /* Is somebody trying to be evil! */ |
2561 | if (c < 0x80 || |
887035a5 |
2562 | (c < 0x800 && term->utf_size >= 2) || |
2563 | (c < 0x10000 && term->utf_size >= 3) || |
2564 | (c < 0x200000 && term->utf_size >= 4) || |
2565 | (c < 0x4000000 && term->utf_size >= 5)) |
4eeb7d09 |
2566 | c = UCSERR; |
2567 | |
2568 | /* Unicode line separator and paragraph separator are CR-LF */ |
2569 | if (c == 0x2028 || c == 0x2029) |
2570 | c = 0x85; |
2571 | |
2572 | /* High controls are probably a Baaad idea too. */ |
2573 | if (c < 0xA0) |
2574 | c = 0xFFFD; |
2575 | |
2576 | /* The UTF-16 surrogates are not nice either. */ |
2577 | /* The standard give the option of decoding these: |
2578 | * I don't want to! */ |
2579 | if (c >= 0xD800 && c < 0xE000) |
2580 | c = UCSERR; |
2581 | |
2582 | /* ISO 10646 characters now limited to UTF-16 range. */ |
2583 | if (c > 0x10FFFF) |
2584 | c = UCSERR; |
2585 | |
2586 | /* This is currently a TagPhobic application.. */ |
2587 | if (c >= 0xE0000 && c <= 0xE007F) |
2588 | continue; |
2589 | |
2590 | /* U+FEFF is best seen as a null. */ |
2591 | if (c == 0xFEFF) |
2592 | continue; |
2593 | /* But U+FFFE is an error. */ |
2594 | if (c == 0xFFFE || c == 0xFFFF) |
2595 | c = UCSERR; |
2596 | |
4eeb7d09 |
2597 | break; |
d3cb5465 |
2598 | } |
2599 | /* Are we in the nasty ACS mode? Note: no sco in utf mode. */ |
887035a5 |
2600 | else if(term->sco_acs && |
92e23ad9 |
2601 | (c!='\033' && c!='\012' && c!='\015' && c!='\b')) |
d3cb5465 |
2602 | { |
5dc6132d |
2603 | if (term->sco_acs == 2) c |= 0x80; |
36566009 |
2604 | c |= CSET_SCOACS; |
4eeb7d09 |
2605 | } else { |
887035a5 |
2606 | switch (term->cset_attr[term->cset]) { |
4eeb7d09 |
2607 | /* |
2608 | * Linedraw characters are different from 'ESC ( B' |
2609 | * only for a small range. For ones outside that |
2610 | * range, make sure we use the same font as well as |
2611 | * the same encoding. |
2612 | */ |
36566009 |
2613 | case CSET_LINEDRW: |
21d2b241 |
2614 | if (term->ucsdata->unitab_ctrl[c] != 0xFF) |
2615 | c = term->ucsdata->unitab_ctrl[c]; |
4eeb7d09 |
2616 | else |
36566009 |
2617 | c = ((unsigned char) c) | CSET_LINEDRW; |
4eeb7d09 |
2618 | break; |
2619 | |
36566009 |
2620 | case CSET_GBCHR: |
4eeb7d09 |
2621 | /* If UK-ASCII, make the '#' a LineDraw Pound */ |
2622 | if (c == '#') { |
36566009 |
2623 | c = '}' | CSET_LINEDRW; |
4eeb7d09 |
2624 | break; |
2625 | } |
36566009 |
2626 | /*FALLTHROUGH*/ case CSET_ASCII: |
21d2b241 |
2627 | if (term->ucsdata->unitab_ctrl[c] != 0xFF) |
2628 | c = term->ucsdata->unitab_ctrl[c]; |
4eeb7d09 |
2629 | else |
36566009 |
2630 | c = ((unsigned char) c) | CSET_ASCII; |
4eeb7d09 |
2631 | break; |
36566009 |
2632 | case CSET_SCOACS: |
2633 | if (c>=' ') c = ((unsigned char)c) | CSET_SCOACS; |
d3cb5465 |
2634 | break; |
4eeb7d09 |
2635 | } |
2636 | } |
2637 | } |
2638 | |
7f9f7200 |
2639 | /* |
2640 | * How about C1 controls? |
2641 | * Explicitly ignore SCI (0x9a), which we don't translate to DECID. |
2642 | */ |
887035a5 |
2643 | if ((c & -32) == 0x80 && term->termstate < DO_CTRLS && |
2644 | !term->vt52_mode && has_compat(VT220)) { |
7f9f7200 |
2645 | if (c == 0x9a) |
2646 | c = 0; |
2647 | else { |
2648 | term->termstate = SEEN_ESC; |
2649 | term->esc_query = FALSE; |
2650 | c = '@' + (c & 0x1F); |
2651 | } |
4eeb7d09 |
2652 | } |
2653 | |
2654 | /* Or the GL control. */ |
887035a5 |
2655 | if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) { |
2656 | if (term->curs.x && !term->wrapnext) |
2657 | term->curs.x--; |
2658 | term->wrapnext = FALSE; |
36566009 |
2659 | /* destructive backspace might be disabled */ |
2660 | if (!term->cfg.no_dbackspace) { |
cdf249c7 |
2661 | check_boundary(term, term->curs.x, term->curs.y); |
2662 | check_boundary(term, term->curs.x+1, term->curs.y); |
c6958dfe |
2663 | copy_termchar(scrlineptr(term->curs.y), |
2664 | term->curs.x, &term->erase_char); |
36566009 |
2665 | } |
4eeb7d09 |
2666 | } else |
2667 | /* Or normal C0 controls. */ |
36566009 |
2668 | if ((c & ~0x1F) == 0 && term->termstate < DO_CTRLS) { |
374330e2 |
2669 | switch (c) { |
a263e4f6 |
2670 | case '\005': /* ENQ: terminal type query */ |
372a19d4 |
2671 | /* |
2672 | * Strictly speaking this is VT100 but a VT100 defaults to |
e14a5a13 |
2673 | * no response. Other terminals respond at their option. |
2674 | * |
2675 | * Don't put a CR in the default string as this tends to |
2676 | * upset some weird software. |
e14a5a13 |
2677 | */ |
32874aea |
2678 | compatibility(ANSIMIN); |
b9d7bcad |
2679 | if (term->ldisc) { |
d45d4c07 |
2680 | char abuf[lenof(term->cfg.answerback)], *s, *d; |
2681 | for (s = term->cfg.answerback, d = abuf; *s;) { |
2682 | char *n; |
2683 | char c = ctrlparse(s, &n); |
2684 | if (n) { |
2685 | *d++ = c; |
2686 | s = n; |
2687 | } else { |
2688 | *d++ = *s++; |
2689 | } |
e7fbcdd8 |
2690 | } |
b9d7bcad |
2691 | lpage_send(term->ldisc, DEFAULT_CODEPAGE, |
2692 | abuf, d - abuf, 0); |
e7fbcdd8 |
2693 | } |
374330e2 |
2694 | break; |
a263e4f6 |
2695 | case '\007': /* BEL: Bell */ |
156686ef |
2696 | { |
2697 | struct beeptime *newbeep; |
286c6b86 |
2698 | unsigned long ticks; |
156686ef |
2699 | |
f7f27309 |
2700 | ticks = GETTICKCOUNT(); |
156686ef |
2701 | |
887035a5 |
2702 | if (!term->beep_overloaded) { |
3d88e64d |
2703 | newbeep = snew(struct beeptime); |
156686ef |
2704 | newbeep->ticks = ticks; |
2705 | newbeep->next = NULL; |
887035a5 |
2706 | if (!term->beephead) |
2707 | term->beephead = newbeep; |
156686ef |
2708 | else |
887035a5 |
2709 | term->beeptail->next = newbeep; |
2710 | term->beeptail = newbeep; |
2711 | term->nbeeps++; |
156686ef |
2712 | } |
2713 | |
2714 | /* |
2715 | * Throw out any beeps that happened more than |
2716 | * t seconds ago. |
2717 | */ |
887035a5 |
2718 | while (term->beephead && |
64734920 |
2719 | term->beephead->ticks < ticks - term->cfg.bellovl_t) { |
887035a5 |
2720 | struct beeptime *tmp = term->beephead; |
2721 | term->beephead = tmp->next; |
156686ef |
2722 | sfree(tmp); |
887035a5 |
2723 | if (!term->beephead) |
2724 | term->beeptail = NULL; |
2725 | term->nbeeps--; |
156686ef |
2726 | } |
2727 | |
64734920 |
2728 | if (term->cfg.bellovl && term->beep_overloaded && |
2729 | ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) { |
156686ef |
2730 | /* |
2731 | * If we're currently overloaded and the |
2732 | * last beep was more than s seconds ago, |
2733 | * leave overload mode. |
2734 | */ |
887035a5 |
2735 | term->beep_overloaded = FALSE; |
64734920 |
2736 | } else if (term->cfg.bellovl && !term->beep_overloaded && |
2737 | term->nbeeps >= term->cfg.bellovl_n) { |
156686ef |
2738 | /* |
2739 | * Now, if we have n or more beeps |
2740 | * remaining in the queue, go into overload |
2741 | * mode. |
2742 | */ |
887035a5 |
2743 | term->beep_overloaded = TRUE; |
156686ef |
2744 | } |
887035a5 |
2745 | term->lastbeep = ticks; |
156686ef |
2746 | |
2747 | /* |
2748 | * Perform an actual beep if we're not overloaded. |
2749 | */ |
64734920 |
2750 | if (!term->cfg.bellovl || !term->beep_overloaded) { |
860a34f8 |
2751 | do_beep(term->frontend, term->cfg.beep); |
39934deb |
2752 | |
64734920 |
2753 | if (term->cfg.beep == BELL_VISUAL) { |
39934deb |
2754 | term_schedule_vbell(term, FALSE, 0); |
0ce89525 |
2755 | } |
156686ef |
2756 | } |
39934deb |
2757 | seen_disp_event(term); |
156686ef |
2758 | } |
374330e2 |
2759 | break; |
a263e4f6 |
2760 | case '\b': /* BS: Back space */ |
887035a5 |
2761 | if (term->curs.x == 0 && |
2762 | (term->curs.y == 0 || term->wrap == 0)) |
2763 | /* do nothing */ ; |
2764 | else if (term->curs.x == 0 && term->curs.y > 0) |
2765 | term->curs.x = term->cols - 1, term->curs.y--; |
2766 | else if (term->wrapnext) |
2767 | term->wrapnext = FALSE; |
374330e2 |
2768 | else |
887035a5 |
2769 | term->curs.x--; |
39934deb |
2770 | seen_disp_event(term); |
374330e2 |
2771 | break; |
a263e4f6 |
2772 | case '\016': /* LS1: Locking-shift one */ |
32874aea |
2773 | compatibility(VT100); |
887035a5 |
2774 | term->cset = 1; |
374330e2 |
2775 | break; |
a263e4f6 |
2776 | case '\017': /* LS0: Locking-shift zero */ |
32874aea |
2777 | compatibility(VT100); |
887035a5 |
2778 | term->cset = 0; |
374330e2 |
2779 | break; |
a263e4f6 |
2780 | case '\033': /* ESC: Escape */ |
887035a5 |
2781 | if (term->vt52_mode) |
2782 | term->termstate = VT52_ESC; |
e14a5a13 |
2783 | else { |
2784 | compatibility(ANSIMIN); |
887035a5 |
2785 | term->termstate = SEEN_ESC; |
2786 | term->esc_query = FALSE; |
e14a5a13 |
2787 | } |
374330e2 |
2788 | break; |
a263e4f6 |
2789 | case '\015': /* CR: Carriage return */ |
887035a5 |
2790 | term->curs.x = 0; |
2791 | term->wrapnext = FALSE; |
39934deb |
2792 | seen_disp_event(term); |
887035a5 |
2793 | term->paste_hold = 0; |
a8327734 |
2794 | if (term->logctx) |
2795 | logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII); |
374330e2 |
2796 | break; |
a263e4f6 |
2797 | case '\014': /* FF: Form feed */ |
1a837633 |
2798 | if (has_compat(SCOANSI)) { |
887035a5 |
2799 | move(term, 0, 0, 0); |
2800 | erase_lots(term, FALSE, FALSE, TRUE); |
2801 | term->disptop = 0; |
2802 | term->wrapnext = FALSE; |
39934deb |
2803 | seen_disp_event(term); |
1a837633 |
2804 | break; |
2805 | } |
a263e4f6 |
2806 | case '\013': /* VT: Line tabulation */ |
32874aea |
2807 | compatibility(VT100); |
a263e4f6 |
2808 | case '\012': /* LF: Line feed */ |
887035a5 |
2809 | if (term->curs.y == term->marg_b) |
2810 | scroll(term, term->marg_t, term->marg_b, 1, TRUE); |
2811 | else if (term->curs.y < term->rows - 1) |
2812 | term->curs.y++; |
64734920 |
2813 | if (term->cfg.lfhascr) |
887035a5 |
2814 | term->curs.x = 0; |
887035a5 |
2815 | term->wrapnext = FALSE; |
39934deb |
2816 | seen_disp_event(term); |
887035a5 |
2817 | term->paste_hold = 0; |
a8327734 |
2818 | if (term->logctx) |
2819 | logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII); |
374330e2 |
2820 | break; |
a263e4f6 |
2821 | case '\t': /* HT: Character tabulation */ |
374330e2 |
2822 | { |
887035a5 |
2823 | pos old_curs = term->curs; |
36566009 |
2824 | termline *ldata = scrlineptr(term->curs.y); |
c9def1b8 |
2825 | |
2826 | do { |
887035a5 |
2827 | term->curs.x++; |
2828 | } while (term->curs.x < term->cols - 1 && |
2829 | !term->tabs[term->curs.x]); |
c9def1b8 |
2830 | |
36566009 |
2831 | if ((ldata->lattr & LATTR_MODE) != LATTR_NORM) { |
887035a5 |
2832 | if (term->curs.x >= term->cols / 2) |
2833 | term->curs.x = term->cols / 2 - 1; |
32874aea |
2834 | } else { |
887035a5 |
2835 | if (term->curs.x >= term->cols) |
2836 | term->curs.x = term->cols - 1; |
c9def1b8 |
2837 | } |
2838 | |
887035a5 |
2839 | check_selection(term, old_curs, term->curs); |
374330e2 |
2840 | } |
39934deb |
2841 | seen_disp_event(term); |
374330e2 |
2842 | break; |
cabfd08c |
2843 | } |
32874aea |
2844 | } else |
887035a5 |
2845 | switch (term->termstate) { |
32874aea |
2846 | case TOPLEVEL: |
887035a5 |
2847 | /* Only graphic characters get this far; |
2848 | * ctrls are stripped above */ |
4eeb7d09 |
2849 | { |
c6958dfe |
2850 | termline *cline = scrlineptr(term->curs.y); |
4eeb7d09 |
2851 | int width = 0; |
2852 | if (DIRECT_CHAR(c)) |
2853 | width = 1; |
2854 | if (!width) |
74790953 |
2855 | width = (term->cfg.cjk_ambig_wide ? |
94debd8c |
2856 | mk_wcwidth_cjk((wchar_t) c) : |
2857 | mk_wcwidth((wchar_t) c)); |
c6958dfe |
2858 | |
2859 | if (term->wrapnext && term->wrap && width > 0) { |
2860 | cline->lattr |= LATTR_WRAPPED; |
2861 | if (term->curs.y == term->marg_b) |
2862 | scroll(term, term->marg_t, term->marg_b, 1, TRUE); |
2863 | else if (term->curs.y < term->rows - 1) |
2864 | term->curs.y++; |
2865 | term->curs.x = 0; |
2866 | term->wrapnext = FALSE; |
1c10e849 |
2867 | cline = scrlineptr(term->curs.y); |
c6958dfe |
2868 | } |
2869 | if (term->insert && width > 0) |
2870 | insch(term, width); |
2871 | if (term->selstate != NO_SELECTION) { |
2872 | pos cursplus = term->curs; |
2873 | incpos(cursplus); |
2874 | check_selection(term, term->curs, cursplus); |
2875 | } |
2876 | if (((c & CSET_MASK) == CSET_ASCII || |
2877 | (c & CSET_MASK) == 0) && |
2878 | term->logctx) |
2879 | logtraffic(term->logctx, (unsigned char) c, |
2880 | LGTYP_ASCII); |
2881 | |
4eeb7d09 |
2882 | switch (width) { |
2883 | case 2: |
3c7366f8 |
2884 | /* |
2885 | * If we're about to display a double-width |
2886 | * character starting in the rightmost |
2887 | * column, then we do something special |
2888 | * instead. We must print a space in the |
2889 | * last column of the screen, then wrap; |
2890 | * and we also set LATTR_WRAPPED2 which |
2891 | * instructs subsequent cut-and-pasting not |
2892 | * only to splice this line to the one |
2893 | * after it, but to ignore the space in the |
2894 | * last character position as well. |
2895 | * (Because what was actually output to the |
2896 | * terminal was presumably just a sequence |
2897 | * of CJK characters, and we don't want a |
2898 | * space to be pasted in the middle of |
2899 | * those just because they had the |
2900 | * misfortune to start in the wrong parity |
2901 | * column. xterm concurs.) |
2902 | */ |
2903 | check_boundary(term, term->curs.x, term->curs.y); |
2904 | check_boundary(term, term->curs.x+2, term->curs.y); |
2905 | if (term->curs.x == term->cols-1) { |
c6958dfe |
2906 | copy_termchar(cline, term->curs.x, |
2907 | &term->erase_char); |
2908 | cline->lattr |= LATTR_WRAPPED | LATTR_WRAPPED2; |
887035a5 |
2909 | if (term->curs.y == term->marg_b) |
2910 | scroll(term, term->marg_t, term->marg_b, |
2911 | 1, TRUE); |
2912 | else if (term->curs.y < term->rows - 1) |
2913 | term->curs.y++; |
2914 | term->curs.x = 0; |
9a6ebcc9 |
2915 | cline = scrlineptr(term->curs.y); |
3c7366f8 |
2916 | /* Now we must check_boundary again, of course. */ |
2917 | check_boundary(term, term->curs.x, term->curs.y); |
2918 | check_boundary(term, term->curs.x+2, term->curs.y); |
4eeb7d09 |
2919 | } |
c6958dfe |
2920 | |
2921 | /* FULL-TERMCHAR */ |
2922 | clear_cc(cline, term->curs.x); |
2923 | cline->chars[term->curs.x].chr = c; |
2924 | cline->chars[term->curs.x].attr = term->curr_attr; |
2925 | |
3c7366f8 |
2926 | term->curs.x++; |
c6958dfe |
2927 | |
2928 | /* FULL-TERMCHAR */ |
2929 | clear_cc(cline, term->curs.x); |
2930 | cline->chars[term->curs.x].chr = UCSWIDE; |
2931 | cline->chars[term->curs.x].attr = term->curr_attr; |
2932 | |
5a73255e |
2933 | break; |
4eeb7d09 |
2934 | case 1: |
3c7366f8 |
2935 | check_boundary(term, term->curs.x, term->curs.y); |
2936 | check_boundary(term, term->curs.x+1, term->curs.y); |
c6958dfe |
2937 | |
2938 | /* FULL-TERMCHAR */ |
2939 | clear_cc(cline, term->curs.x); |
2940 | cline->chars[term->curs.x].chr = c; |
2941 | cline->chars[term->curs.x].attr = term->curr_attr; |
2942 | |
32874aea |
2943 | break; |
c6958dfe |
2944 | case 0: |
f00630be |
2945 | if (term->curs.x > 0) { |
2946 | int x = term->curs.x - 1; |
2947 | |
2948 | /* If we're in wrapnext state, the character |
2949 | * to combine with is _here_, not to our left. */ |
2950 | if (term->wrapnext) |
2951 | x++; |
2952 | |
2953 | /* |
2954 | * If the previous character is |
2955 | * UCSWIDE, back up another one. |
2956 | */ |
2957 | if (cline->chars[x].chr == UCSWIDE) { |
2958 | assert(x > 0); |
2959 | x--; |
2960 | } |
2961 | |
2962 | add_cc(cline, x, c); |
39934deb |
2963 | seen_disp_event(term); |
f00630be |
2964 | } |
c6958dfe |
2965 | continue; |
4eeb7d09 |
2966 | default: |
2967 | continue; |
32874aea |
2968 | } |
c6958dfe |
2969 | term->curs.x++; |
2970 | if (term->curs.x == term->cols) { |
2971 | term->curs.x--; |
2972 | term->wrapnext = TRUE; |
2973 | if (term->wrap && term->vt52_mode) { |
2974 | cline->lattr |= LATTR_WRAPPED; |
2975 | if (term->curs.y == term->marg_b) |
2976 | scroll(term, term->marg_t, term->marg_b, 1, TRUE); |
2977 | else if (term->curs.y < term->rows - 1) |
2978 | term->curs.y++; |
2979 | term->curs.x = 0; |
2980 | term->wrapnext = FALSE; |
2981 | } |
4eeb7d09 |
2982 | } |
39934deb |
2983 | seen_disp_event(term); |
e14a5a13 |
2984 | } |
374330e2 |
2985 | break; |
32874aea |
2986 | |
32874aea |
2987 | case OSC_MAYBE_ST: |
2988 | /* |
2989 | * This state is virtually identical to SEEN_ESC, with the |
2990 | * exception that we have an OSC sequence in the pipeline, |
2991 | * and _if_ we see a backslash, we process it. |
2992 | */ |
2993 | if (c == '\\') { |
887035a5 |
2994 | do_osc(term); |
2995 | term->termstate = TOPLEVEL; |
32874aea |
2996 | break; |
e14a5a13 |
2997 | } |
32874aea |
2998 | /* else fall through */ |
2999 | case SEEN_ESC: |
4eeb7d09 |
3000 | if (c >= ' ' && c <= '/') { |
887035a5 |
3001 | if (term->esc_query) |
3002 | term->esc_query = -1; |
4eeb7d09 |
3003 | else |
887035a5 |
3004 | term->esc_query = c; |
32874aea |
3005 | break; |
4eeb7d09 |
3006 | } |
887035a5 |
3007 | term->termstate = TOPLEVEL; |
3008 | switch (ANSI(c, term->esc_query)) { |
a263e4f6 |
3009 | case '[': /* enter CSI mode */ |
887035a5 |
3010 | term->termstate = SEEN_CSI; |
3011 | term->esc_nargs = 1; |
3012 | term->esc_args[0] = ARG_DEFAULT; |
3013 | term->esc_query = FALSE; |
32874aea |
3014 | break; |
a263e4f6 |
3015 | case ']': /* OSC: xterm escape sequences */ |
32874aea |
3016 | /* Compatibility is nasty here, xterm, linux, decterm yuk! */ |
3017 | compatibility(OTHER); |
887035a5 |
3018 | term->termstate = SEEN_OSC; |
3019 | term->esc_args[0] = 0; |
32874aea |
3020 | break; |
a263e4f6 |
3021 | case '7': /* DECSC: save cursor */ |
32874aea |
3022 | compatibility(VT100); |
887035a5 |
3023 | save_cursor(term, TRUE); |
32874aea |
3024 | break; |
a263e4f6 |
3025 | case '8': /* DECRC: restore cursor */ |
32874aea |
3026 | compatibility(VT100); |
887035a5 |
3027 | save_cursor(term, FALSE); |
39934deb |
3028 | seen_disp_event(term); |
32874aea |
3029 | break; |
a263e4f6 |
3030 | case '=': /* DECKPAM: Keypad application mode */ |
32874aea |
3031 | compatibility(VT100); |
887035a5 |
3032 | term->app_keypad_keys = TRUE; |
32874aea |
3033 | break; |
a263e4f6 |
3034 | case '>': /* DECKPNM: Keypad numeric mode */ |
32874aea |
3035 | compatibility(VT100); |
887035a5 |
3036 | term->app_keypad_keys = FALSE; |
32874aea |
3037 | break; |
a263e4f6 |
3038 | case 'D': /* IND: exactly equivalent to LF */ |
32874aea |
3039 | compatibility(VT100); |
887035a5 |
3040 | if (term->curs.y == term->marg_b) |
3041 | scroll(term, term->marg_t, term->marg_b, 1, TRUE); |
3042 | else if (term->curs.y < term->rows - 1) |
3043 | term->curs.y++; |
887035a5 |
3044 | term->wrapnext = FALSE; |
39934deb |
3045 | seen_disp_event(term); |
32874aea |
3046 | break; |
a263e4f6 |
3047 | case 'E': /* NEL: exactly equivalent to CR-LF */ |
32874aea |
3048 | compatibility(VT100); |
887035a5 |
3049 | term->curs.x = 0; |
3050 | if (term->curs.y == term->marg_b) |
3051 | scroll(term, term->marg_t, term->marg_b, 1, TRUE); |
3052 | else if (term->curs.y < term->rows - 1) |
3053 | term->curs.y++; |
887035a5 |
3054 | term->wrapnext = FALSE; |
39934deb |
3055 | seen_disp_event(term); |
32874aea |
3056 | break; |
a263e4f6 |
3057 | case 'M': /* RI: reverse index - backwards LF */ |
32874aea |
3058 | compatibility(VT100); |
887035a5 |
3059 | if (term->curs.y == term->marg_t) |
3060 | scroll(term, term->marg_t, term->marg_b, -1, TRUE); |
3061 | else if (term->curs.y > 0) |
3062 | term->curs.y--; |
887035a5 |
3063 | term->wrapnext = FALSE; |
39934deb |
3064 | seen_disp_event(term); |
32874aea |
3065 | break; |
a263e4f6 |
3066 | case 'Z': /* DECID: terminal type query */ |
32874aea |
3067 | compatibility(VT100); |
b9d7bcad |
3068 | if (term->ldisc) |
3069 | ldisc_send(term->ldisc, term->id_string, |
3070 | strlen(term->id_string), 0); |
32874aea |
3071 | break; |
a263e4f6 |
3072 | case 'c': /* RIS: restore power-on settings */ |
32874aea |
3073 | compatibility(VT100); |
887035a5 |
3074 | power_on(term); |
b9d7bcad |
3075 | if (term->ldisc) /* cause ldisc to notice changes */ |
3076 | ldisc_send(term->ldisc, NULL, 0, 0); |
887035a5 |
3077 | if (term->reset_132) { |
64734920 |
3078 | if (!term->cfg.no_remote_resize) |
a8327734 |
3079 | request_resize(term->frontend, 80, term->rows); |
887035a5 |
3080 | term->reset_132 = 0; |
374330e2 |
3081 | } |
887035a5 |
3082 | term->disptop = 0; |
39934deb |
3083 | seen_disp_event(term); |
32874aea |
3084 | break; |
a263e4f6 |
3085 | case 'H': /* HTS: set a tab */ |
32874aea |
3086 | compatibility(VT100); |
887035a5 |
3087 | term->tabs[term->curs.x] = TRUE; |
32874aea |
3088 | break; |
4eeb7d09 |
3089 | |
a263e4f6 |
3090 | case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */ |
4eeb7d09 |
3091 | compatibility(VT100); |
3092 | { |
36566009 |
3093 | termline *ldata; |
4eeb7d09 |
3094 | int i, j; |
3095 | pos scrtop, scrbot; |
3096 | |
887035a5 |
3097 | for (i = 0; i < term->rows; i++) { |
36566009 |
3098 | ldata = scrlineptr(i); |
3099 | for (j = 0; j < term->cols; j++) { |
c6958dfe |
3100 | copy_termchar(ldata, j, |
3101 | &term->basic_erase_char); |
36566009 |
3102 | ldata->chars[j].chr = 'E'; |
3103 | } |
3104 | ldata->lattr = LATTR_NORM; |
4eeb7d09 |
3105 | } |
887035a5 |
3106 | term->disptop = 0; |
39934deb |
3107 | seen_disp_event(term); |
4eeb7d09 |
3108 | scrtop.x = scrtop.y = 0; |
3109 | scrbot.x = 0; |
887035a5 |
3110 | scrbot.y = term->rows; |
3111 | check_selection(term, scrtop, scrbot); |
4eeb7d09 |
3112 | } |
3113 | break; |
3114 | |
3115 | case ANSI('3', '#'): |
3116 | case ANSI('4', '#'): |
3117 | case ANSI('5', '#'): |
3118 | case ANSI('6', '#'): |
3119 | compatibility(VT100); |
3120 | { |
36566009 |
3121 | int nlattr; |
3122 | |
887035a5 |
3123 | switch (ANSI(c, term->esc_query)) { |
a263e4f6 |
3124 | case ANSI('3', '#'): /* DECDHL: 2*height, top */ |
4eeb7d09 |
3125 | nlattr = LATTR_TOP; |
3126 | break; |
a263e4f6 |
3127 | case ANSI('4', '#'): /* DECDHL: 2*height, bottom */ |
4eeb7d09 |
3128 | nlattr = LATTR_BOT; |
3129 | break; |
a263e4f6 |
3130 | case ANSI('5', '#'): /* DECSWL: normal */ |
4eeb7d09 |
3131 | nlattr = LATTR_NORM; |
3132 | break; |
a263e4f6 |
3133 | default: /* case ANSI('6', '#'): DECDWL: 2*width */ |
4eeb7d09 |
3134 | nlattr = LATTR_WIDE; |
3135 | break; |
3136 | } |
36566009 |
3137 | scrlineptr(term->curs.y)->lattr = nlattr; |
4eeb7d09 |
3138 | } |
3139 | break; |
a263e4f6 |
3140 | /* GZD4: G0 designate 94-set */ |
4eeb7d09 |
3141 | case ANSI('A', '('): |
3142 | compatibility(VT100); |
64734920 |
3143 | if (!term->cfg.no_remote_charset) |
36566009 |
3144 | term->cset_attr[0] = CSET_GBCHR; |
4eeb7d09 |
3145 | break; |
3146 | case ANSI('B', '('): |
3147 | compatibility(VT100); |
64734920 |
3148 | if (!term->cfg.no_remote_charset) |
36566009 |
3149 | term->cset_attr[0] = CSET_ASCII; |
4eeb7d09 |
3150 | break; |
3151 | case ANSI('0', '('): |
3152 | compatibility(VT100); |
64734920 |
3153 | if (!term->cfg.no_remote_charset) |
36566009 |
3154 | term->cset_attr[0] = CSET_LINEDRW; |
4eeb7d09 |
3155 | break; |
d3cb5465 |
3156 | case ANSI('U', '('): |
3157 | compatibility(OTHER); |
64734920 |
3158 | if (!term->cfg.no_remote_charset) |
36566009 |
3159 | term->cset_attr[0] = CSET_SCOACS; |
d3cb5465 |
3160 | break; |
a263e4f6 |
3161 | /* G1D4: G1-designate 94-set */ |
4eeb7d09 |
3162 | case ANSI('A', ')'): |
3163 | compatibility(VT100); |
64734920 |
3164 | if (!term->cfg.no_remote_charset) |
36566009 |
3165 | term->cset_attr[1] = CSET_GBCHR; |
4eeb7d09 |
3166 | break; |
3167 | case ANSI('B', ')'): |
3168 | compatibility(VT100); |
64734920 |
3169 | if (!term->cfg.no_remote_charset) |
36566009 |
3170 | term->cset_attr[1] = CSET_ASCII; |
4eeb7d09 |
3171 | break; |
3172 | case ANSI('0', ')'): |
3173 | compatibility(VT100); |
64734920 |
3174 | if (!term->cfg.no_remote_charset) |
36566009 |
3175 | term->cset_attr[1] = CSET_LINEDRW; |
4eeb7d09 |
3176 | break; |
d3cb5465 |
3177 | case ANSI('U', ')'): |
3178 | compatibility(OTHER); |
64734920 |
3179 | if (!term->cfg.no_remote_charset) |
36566009 |
3180 | term->cset_attr[1] = CSET_SCOACS; |
d3cb5465 |
3181 | break; |
a263e4f6 |
3182 | /* DOCS: Designate other coding system */ |
4eeb7d09 |
3183 | case ANSI('8', '%'): /* Old Linux code */ |
3184 | case ANSI('G', '%'): |
3185 | compatibility(OTHER); |
64734920 |
3186 | if (!term->cfg.no_remote_charset) |
887035a5 |
3187 | term->utf = 1; |
4eeb7d09 |
3188 | break; |
3189 | case ANSI('@', '%'): |
3190 | compatibility(OTHER); |
64734920 |
3191 | if (!term->cfg.no_remote_charset) |
887035a5 |
3192 | term->utf = 0; |
4eeb7d09 |
3193 | break; |
374330e2 |
3194 | } |
3195 | break; |
32874aea |
3196 | case SEEN_CSI: |
887035a5 |
3197 | term->termstate = TOPLEVEL; /* default */ |
32874aea |
3198 | if (isdigit(c)) { |
887035a5 |
3199 | if (term->esc_nargs <= ARGS_MAX) { |
3200 | if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT) |
3201 | term->esc_args[term->esc_nargs - 1] = 0; |
3202 | term->esc_args[term->esc_nargs - 1] = |
3203 | 10 * term->esc_args[term->esc_nargs - 1] + c - '0'; |
32874aea |
3204 | } |
887035a5 |
3205 | term->termstate = SEEN_CSI; |
32874aea |
3206 | } else if (c == ';') { |
887035a5 |
3207 | if (++term->esc_nargs <= ARGS_MAX) |
3208 | term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT; |
3209 | term->termstate = SEEN_CSI; |
32874aea |
3210 | } else if (c < '@') { |
887035a5 |
3211 | if (term->esc_query) |
3212 | term->esc_query = -1; |
32874aea |
3213 | else if (c == '?') |
887035a5 |
3214 | term->esc_query = TRUE; |
32874aea |
3215 | else |
887035a5 |
3216 | term->esc_query = c; |
3217 | term->termstate = SEEN_CSI; |
32874aea |
3218 | } else |
887035a5 |
3219 | switch (ANSI(c, term->esc_query)) { |
a263e4f6 |
3220 | case 'A': /* CUU: move up N lines */ |
887035a5 |
3221 | move(term, term->curs.x, |
3222 | term->curs.y - def(term->esc_args[0], 1), 1); |
39934deb |
3223 | seen_disp_event(term); |
32874aea |
3224 | break; |
a263e4f6 |
3225 | case 'e': /* VPR: move down N lines */ |
32874aea |
3226 | compatibility(ANSI); |
5442e92f |
3227 | /* FALLTHROUGH */ |
a263e4f6 |
3228 | case 'B': /* CUD: Cursor down */ |
887035a5 |
3229 | move(term, term->curs.x, |
3230 | term->curs.y + def(term->esc_args[0], 1), 1); |
39934deb |
3231 | seen_disp_event(term); |
32874aea |
3232 | break; |
a263e4f6 |
3233 | case ANSI('c', '>'): /* DA: report xterm version */ |
32874aea |
3234 | compatibility(OTHER); |
3235 | /* this reports xterm version 136 so that VIM can |
3236 | use the drag messages from the mouse reporting */ |
b9d7bcad |
3237 | if (term->ldisc) |
3238 | ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0); |
32874aea |
3239 | break; |
a263e4f6 |
3240 | case 'a': /* HPR: move right N cols */ |
5442e92f |
3241 | compatibility(ANSI); |
3242 | /* FALLTHROUGH */ |
a263e4f6 |
3243 | case 'C': /* CUF: Cursor right */ |
887035a5 |
3244 | move(term, term->curs.x + def(term->esc_args[0], 1), |
3245 | term->curs.y, 1); |
39934deb |
3246 | seen_disp_event(term); |
32874aea |
3247 | break; |
a263e4f6 |
3248 | case 'D': /* CUB: move left N cols */ |
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 'E': /* CNL: move down N lines and CR */ |
32874aea |
3254 | compatibility(ANSI); |
887035a5 |
3255 | move(term, 0, |
3256 | term->curs.y + def(term->esc_args[0], 1), 1); |
39934deb |
3257 | seen_disp_event(term); |
32874aea |
3258 | break; |
a263e4f6 |
3259 | case 'F': /* CPL: move up N lines and CR */ |
32874aea |
3260 | compatibility(ANSI); |
887035a5 |
3261 | move(term, 0, |
3262 | term->curs.y - def(term->esc_args[0], 1), 1); |
39934deb |
3263 | seen_disp_event(term); |
32874aea |
3264 | break; |
a263e4f6 |
3265 | case 'G': /* CHA */ |
3266 | case '`': /* HPA: set horizontal posn */ |
32874aea |
3267 | compatibility(ANSI); |
887035a5 |
3268 | move(term, def(term->esc_args[0], 1) - 1, |
3269 | term->curs.y, 0); |
39934deb |
3270 | seen_disp_event(term); |
32874aea |
3271 | break; |
a263e4f6 |
3272 | case 'd': /* VPA: set vertical posn */ |
32874aea |
3273 | compatibility(ANSI); |
887035a5 |
3274 | move(term, term->curs.x, |
3275 | ((term->dec_om ? term->marg_t : 0) + |
3276 | def(term->esc_args[0], 1) - 1), |
3277 | (term->dec_om ? 2 : 0)); |
39934deb |
3278 | seen_disp_event(term); |
32874aea |
3279 | break; |
a263e4f6 |
3280 | case 'H': /* CUP */ |
3281 | case 'f': /* HVP: set horz and vert posns at once */ |
887035a5 |
3282 | if (term->esc_nargs < 2) |
3283 | term->esc_args[1] = ARG_DEFAULT; |
3284 | move(term, def(term->esc_args[1], 1) - 1, |
3285 | ((term->dec_om ? term->marg_t : 0) + |
3286 | def(term->esc_args[0], 1) - 1), |
3287 | (term->dec_om ? 2 : 0)); |
39934deb |
3288 | seen_disp_event(term); |
32874aea |
3289 | break; |
a263e4f6 |
3290 | case 'J': /* ED: erase screen or parts of it */ |
32874aea |
3291 | { |
887035a5 |
3292 | unsigned int i = def(term->esc_args[0], 0) + 1; |
32874aea |
3293 | if (i > 3) |
3294 | i = 0; |
887035a5 |
3295 | erase_lots(term, FALSE, !!(i & 2), !!(i & 1)); |
32874aea |
3296 | } |
887035a5 |
3297 | term->disptop = 0; |
39934deb |
3298 | seen_disp_event(term); |
32874aea |
3299 | break; |
a263e4f6 |
3300 | case 'K': /* EL: erase line or parts of it */ |
32874aea |
3301 | { |
887035a5 |
3302 | unsigned int i = def(term->esc_args[0], 0) + 1; |
32874aea |
3303 | if (i > 3) |
3304 | i = 0; |
887035a5 |
3305 | erase_lots(term, TRUE, !!(i & 2), !!(i & 1)); |
32874aea |
3306 | } |
39934deb |
3307 | seen_disp_event(term); |
32874aea |
3308 | break; |
a263e4f6 |
3309 | case 'L': /* IL: insert lines */ |
32874aea |
3310 | compatibility(VT102); |
887035a5 |
3311 | if (term->curs.y <= term->marg_b) |
3312 | scroll(term, term->curs.y, term->marg_b, |
3313 | -def(term->esc_args[0], 1), FALSE); |
39934deb |
3314 | seen_disp_event(term); |
32874aea |
3315 | break; |
a263e4f6 |
3316 | case 'M': /* DL: delete lines */ |
32874aea |
3317 | compatibility(VT102); |
887035a5 |
3318 | if (term->curs.y <= term->marg_b) |
3319 | scroll(term, term->curs.y, term->marg_b, |
3320 | def(term->esc_args[0], 1), |
32874aea |
3321 | TRUE); |
39934deb |
3322 | seen_disp_event(term); |
32874aea |
3323 | break; |
a263e4f6 |
3324 | case '@': /* ICH: insert chars */ |
32874aea |
3325 | /* XXX VTTEST says this is vt220, vt510 manual says vt102 */ |
3326 | compatibility(VT102); |
887035a5 |
3327 | insch(term, def(term->esc_args[0], 1)); |
39934deb |
3328 | seen_disp_event(term); |
32874aea |
3329 | break; |
a263e4f6 |
3330 | case 'P': /* DCH: delete chars */ |
32874aea |
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 'c': /* DA: terminal type query */ |
32874aea |
3336 | compatibility(VT100); |
3337 | /* This is the response for a VT102 */ |
b9d7bcad |
3338 | if (term->ldisc) |
3339 | ldisc_send(term->ldisc, term->id_string, |
3340 | strlen(term->id_string), 0); |
32874aea |
3341 | break; |
a263e4f6 |
3342 | case 'n': /* DSR: cursor position query */ |
b9d7bcad |
3343 | if (term->ldisc) { |
3344 | if (term->esc_args[0] == 6) { |
3345 | char buf[32]; |
3346 | sprintf(buf, "\033[%d;%dR", term->curs.y + 1, |
3347 | term->curs.x + 1); |
3348 | ldisc_send(term->ldisc, buf, strlen(buf), 0); |
3349 | } else if (term->esc_args[0] == 5) { |
3350 | ldisc_send(term->ldisc, "\033[0n", 4, 0); |
3351 | } |
32874aea |
3352 | } |
3353 | break; |
a263e4f6 |
3354 | case 'h': /* SM: toggle modes to high */ |
32874aea |
3355 | case ANSI_QUE('h'): |
3356 | compatibility(VT100); |
3357 | { |
3358 | int i; |
887035a5 |
3359 | for (i = 0; i < term->esc_nargs; i++) |
3360 | toggle_mode(term, term->esc_args[i], |
3361 | term->esc_query, TRUE); |
32874aea |
3362 | } |
3363 | break; |
a263e4f6 |
3364 | case 'i': /* MC: Media copy */ |
b44b307a |
3365 | case ANSI_QUE('i'): |
3366 | compatibility(VT100); |
3367 | { |
887035a5 |
3368 | if (term->esc_nargs != 1) break; |
64734920 |
3369 | if (term->esc_args[0] == 5 && *term->cfg.printer) { |
887035a5 |
3370 | term->printing = TRUE; |
3371 | term->only_printing = !term->esc_query; |
3372 | term->print_state = 0; |
3373 | term_print_setup(term); |
3374 | } else if (term->esc_args[0] == 4 && |
3375 | term->printing) { |
3376 | term_print_finish(term); |
b44b307a |
3377 | } |
3378 | } |
3379 | break; |
a263e4f6 |
3380 | case 'l': /* RM: toggle modes to low */ |
32874aea |
3381 | case ANSI_QUE('l'): |
3382 | compatibility(VT100); |
3383 | { |
3384 | int i; |
887035a5 |
3385 | for (i = 0; i < term->esc_nargs; i++) |
3386 | toggle_mode(term, term->esc_args[i], |
3387 | term->esc_query, FALSE); |
32874aea |
3388 | } |
3389 | break; |
a263e4f6 |
3390 | case 'g': /* TBC: clear tabs */ |
32874aea |
3391 | compatibility(VT100); |
887035a5 |
3392 | if (term->esc_nargs == 1) { |
3393 | if (term->esc_args[0] == 0) { |
3394 | term->tabs[term->curs.x] = FALSE; |
3395 | } else if (term->esc_args[0] == 3) { |
32874aea |
3396 | int i; |
887035a5 |
3397 | for (i = 0; i < term->cols; i++) |
3398 | term->tabs[i] = FALSE; |
32874aea |
3399 | } |
3400 | } |
3401 | break; |
a263e4f6 |
3402 | case 'r': /* DECSTBM: set scroll margins */ |
32874aea |
3403 | compatibility(VT100); |
887035a5 |
3404 | if (term->esc_nargs <= 2) { |
32874aea |
3405 | int top, bot; |
887035a5 |
3406 | top = def(term->esc_args[0], 1) - 1; |
3407 | bot = (term->esc_nargs <= 1 |
3408 | || term->esc_args[1] == 0 ? |
3409 | term->rows : |
3410 | def(term->esc_args[1], term->rows)) - 1; |
3411 | if (bot >= term->rows) |
3412 | bot = term->rows - 1; |
32874aea |
3413 | /* VTTEST Bug 9 - if region is less than 2 lines |
3414 | * don't change region. |
3415 | */ |
3416 | if (bot - top > 0) { |
887035a5 |
3417 | term->marg_t = top; |
3418 | term->marg_b = bot; |
3419 | term->curs.x = 0; |
32874aea |
3420 | /* |
3421 | * I used to think the cursor should be |
3422 | * placed at the top of the newly marginned |
3423 | * area. Apparently not: VMS TPU falls over |
3424 | * if so. |
3425 | * |
887035a5 |
3426 | * Well actually it should for |
3427 | * Origin mode - RDB |
32874aea |
3428 | */ |
887035a5 |
3429 | term->curs.y = (term->dec_om ? |
3430 | term->marg_t : 0); |
39934deb |
3431 | seen_disp_event(term); |
32874aea |
3432 | } |
3433 | } |
3434 | break; |
a263e4f6 |
3435 | case 'm': /* SGR: set graphics rendition */ |
32874aea |
3436 | { |
3437 | /* |
887035a5 |
3438 | * A VT100 without the AVO only had one |
3439 | * attribute, either underline or |
3440 | * reverse video depending on the |
3441 | * cursor type, this was selected by |
3442 | * CSI 7m. |
32874aea |
3443 | * |
3444 | * case 2: |
887035a5 |
3445 | * This is sometimes DIM, eg on the |
3446 | * GIGI and Linux |
32874aea |
3447 | * case 8: |
4eeb7d09 |
3448 | * This is sometimes INVIS various ANSI. |
32874aea |
3449 | * case 21: |
3450 | * This like 22 disables BOLD, DIM and INVIS |
3451 | * |
887035a5 |
3452 | * The ANSI colours appear on any |
3453 | * terminal that has colour (obviously) |
3454 | * but the interaction between sgr0 and |
3455 | * the colours varies but is usually |
3456 | * related to the background colour |
3457 | * erase item. The interaction between |
3458 | * colour attributes and the mono ones |
3459 | * is also very implementation |
3460 | * dependent. |
32874aea |
3461 | * |
887035a5 |
3462 | * The 39 and 49 attributes are likely |
3463 | * to be unimplemented. |
32874aea |
3464 | */ |
3465 | int i; |
887035a5 |
3466 | for (i = 0; i < term->esc_nargs; i++) { |
3467 | switch (def(term->esc_args[i], 0)) { |
32874aea |
3468 | case 0: /* restore defaults */ |
69761fd5 |
3469 | term->curr_attr = term->default_attr; |
32874aea |
3470 | break; |
3471 | case 1: /* enable bold */ |
3472 | compatibility(VT100AVO); |
887035a5 |
3473 | term->curr_attr |= ATTR_BOLD; |
32874aea |
3474 | break; |
3475 | case 21: /* (enable double underline) */ |
3476 | compatibility(OTHER); |
3477 | case 4: /* enable underline */ |
3478 | compatibility(VT100AVO); |
887035a5 |
3479 | term->curr_attr |= ATTR_UNDER; |
32874aea |
3480 | break; |
3481 | case 5: /* enable blink */ |
3482 | compatibility(VT100AVO); |
887035a5 |
3483 | term->curr_attr |= ATTR_BLINK; |
32874aea |
3484 | break; |
1b20b972 |
3485 | case 6: /* SCO light bkgrd */ |
3486 | compatibility(SCOANSI); |
3487 | term->blink_is_real = FALSE; |
3488 | term->curr_attr |= ATTR_BLINK; |
39934deb |
3489 | term_schedule_tblink(term); |
1b20b972 |
3490 | break; |
32874aea |
3491 | case 7: /* enable reverse video */ |
887035a5 |
3492 | term->curr_attr |= ATTR_REVERSE; |
32874aea |
3493 | break; |
d3cb5465 |
3494 | case 10: /* SCO acs off */ |
3495 | compatibility(SCOANSI); |
64734920 |
3496 | if (term->cfg.no_remote_charset) break; |
887035a5 |
3497 | term->sco_acs = 0; break; |
d3cb5465 |
3498 | case 11: /* SCO acs on */ |
3499 | compatibility(SCOANSI); |
64734920 |
3500 | if (term->cfg.no_remote_charset) break; |
887035a5 |
3501 | term->sco_acs = 1; break; |
5dc6132d |
3502 | case 12: /* SCO acs on, |0x80 */ |
d3cb5465 |
3503 | compatibility(SCOANSI); |
64734920 |
3504 | if (term->cfg.no_remote_charset) break; |
887035a5 |
3505 | term->sco_acs = 2; break; |
32874aea |
3506 | case 22: /* disable bold */ |
3507 | compatibility2(OTHER, VT220); |
887035a5 |
3508 | term->curr_attr &= ~ATTR_BOLD; |
32874aea |
3509 | break; |
3510 | case 24: /* disable underline */ |
3511 | compatibility2(OTHER, VT220); |
887035a5 |
3512 | term->curr_attr &= ~ATTR_UNDER; |
32874aea |
3513 | break; |
3514 | case 25: /* disable blink */ |
3515 | compatibility2(OTHER, VT220); |
887035a5 |
3516 | term->curr_attr &= ~ATTR_BLINK; |
32874aea |
3517 | break; |
3518 | case 27: /* disable reverse video */ |
3519 | compatibility2(OTHER, VT220); |
887035a5 |
3520 | term->curr_attr &= ~ATTR_REVERSE; |
32874aea |
3521 | break; |
3522 | case 30: |
3523 | case 31: |
3524 | case 32: |
3525 | case 33: |
3526 | case 34: |
3527 | case 35: |
3528 | case 36: |
3529 | case 37: |
3530 | /* foreground */ |
887035a5 |
3531 | term->curr_attr &= ~ATTR_FGMASK; |
3532 | term->curr_attr |= |
3533 | (term->esc_args[i] - 30)<<ATTR_FGSHIFT; |
32874aea |
3534 | break; |
37ca32ed |
3535 | case 90: |
3536 | case 91: |
3537 | case 92: |
3538 | case 93: |
3539 | case 94: |
3540 | case 95: |
3541 | case 96: |
3542 | case 97: |
372a19d4 |
3543 | /* aixterm-style bright foreground */ |
37ca32ed |
3544 | term->curr_attr &= ~ATTR_FGMASK; |
3545 | term->curr_attr |= |
cecb13f6 |
3546 | ((term->esc_args[i] - 90 + 8) |
37ca32ed |
3547 | << ATTR_FGSHIFT); |
3548 | break; |
32874aea |
3549 | case 39: /* default-foreground */ |
887035a5 |
3550 | term->curr_attr &= ~ATTR_FGMASK; |
3551 | term->curr_attr |= ATTR_DEFFG; |
32874aea |
3552 | break; |
3553 | case 40: |
3554 | case 41: |
3555 | case 42: |
3556 | case 43: |
3557 | case 44: |
3558 | case 45: |
3559 | case 46: |
3560 | case 47: |
3561 | /* background */ |
887035a5 |
3562 | term->curr_attr &= ~ATTR_BGMASK; |
3563 | term->curr_attr |= |
3564 | (term->esc_args[i] - 40)<<ATTR_BGSHIFT; |
32874aea |
3565 | break; |
37ca32ed |
3566 | case 100: |
3567 | case 101: |
3568 | case 102: |
3569 | case 103: |
3570 | case 104: |
3571 | case 105: |
3572 | case 106: |
3573 | case 107: |
372a19d4 |
3574 | /* aixterm-style bright background */ |
37ca32ed |
3575 | term->curr_attr &= ~ATTR_BGMASK; |
3576 | term->curr_attr |= |
cecb13f6 |
3577 | ((term->esc_args[i] - 100 + 8) |
37ca32ed |
3578 | << ATTR_BGSHIFT); |
3579 | break; |
32874aea |
3580 | case 49: /* default-background */ |
887035a5 |
3581 | term->curr_attr &= ~ATTR_BGMASK; |
3582 | term->curr_attr |= ATTR_DEFBG; |
32874aea |
3583 | break; |
cecb13f6 |
3584 | case 38: /* xterm 256-colour mode */ |
3585 | if (i+2 < term->esc_nargs && |
3586 | term->esc_args[i+1] == 5) { |
3587 | term->curr_attr &= ~ATTR_FGMASK; |
3588 | term->curr_attr |= |
3589 | ((term->esc_args[i+2] & 0xFF) |
3590 | << ATTR_FGSHIFT); |
3591 | i += 2; |
3592 | } |
3593 | break; |
3594 | case 48: /* xterm 256-colour mode */ |
3595 | if (i+2 < term->esc_nargs && |
3596 | term->esc_args[i+1] == 5) { |
3597 | term->curr_attr &= ~ATTR_BGMASK; |
3598 | term->curr_attr |= |
3599 | ((term->esc_args[i+2] & 0xFF) |
3600 | << ATTR_BGSHIFT); |
3601 | i += 2; |
3602 | } |
3603 | break; |
32874aea |
3604 | } |
3605 | } |
36566009 |
3606 | set_erase_char(term); |
32874aea |
3607 | } |
3608 | break; |
3609 | case 's': /* save cursor */ |
887035a5 |
3610 | save_cursor(term, TRUE); |
32874aea |
3611 | break; |
3612 | case 'u': /* restore cursor */ |
887035a5 |
3613 | save_cursor(term, FALSE); |
39934deb |
3614 | seen_disp_event(term); |
32874aea |
3615 | break; |
a263e4f6 |
3616 | case 't': /* DECSLPP: set page size - ie window height */ |
374330e2 |
3617 | /* |
32874aea |
3618 | * VT340/VT420 sequence DECSLPP, DEC only allows values |
3619 | * 24/25/36/48/72/144 other emulators (eg dtterm) use |
3620 | * illegal values (eg first arg 1..9) for window changing |
3621 | * and reports. |
3622 | */ |
887035a5 |
3623 | if (term->esc_nargs <= 1 |
3624 | && (term->esc_args[0] < 1 || |
3625 | term->esc_args[0] >= 24)) { |
68f9b3d9 |
3626 | compatibility(VT340TEXT); |
64734920 |
3627 | if (!term->cfg.no_remote_resize) |
a8327734 |
3628 | request_resize(term->frontend, term->cols, |
887035a5 |
3629 | def(term->esc_args[0], 24)); |
3630 | deselect(term); |
3631 | } else if (term->esc_nargs >= 1 && |
3632 | term->esc_args[0] >= 1 && |
3633 | term->esc_args[0] < 24) { |
68f9b3d9 |
3634 | compatibility(OTHER); |
3635 | |
887035a5 |
3636 | switch (term->esc_args[0]) { |
68f9b3d9 |
3637 | int x, y, len; |
3638 | char buf[80], *p; |
3639 | case 1: |
a8327734 |
3640 | set_iconic(term->frontend, FALSE); |
68f9b3d9 |
3641 | break; |
3642 | case 2: |
a8327734 |
3643 | set_iconic(term->frontend, TRUE); |
68f9b3d9 |
3644 | break; |
3645 | case 3: |
887035a5 |
3646 | if (term->esc_nargs >= 3) { |
64734920 |
3647 | if (!term->cfg.no_remote_resize) |
a8327734 |
3648 | move_window(term->frontend, |
3649 | def(term->esc_args[1], 0), |
887035a5 |
3650 | def(term->esc_args[2], 0)); |
68f9b3d9 |
3651 | } |
3652 | break; |
3653 | case 4: |
3654 | /* We should resize the window to a given |
3655 | * size in pixels here, but currently our |
3656 | * resizing code isn't healthy enough to |
3657 | * manage it. */ |
3658 | break; |
3659 | case 5: |
a8327734 |
3660 | /* move to top */ |
3661 | set_zorder(term->frontend, TRUE); |
68f9b3d9 |
3662 | break; |
3663 | case 6: |
a8327734 |
3664 | /* move to bottom */ |
3665 | set_zorder(term->frontend, FALSE); |
68f9b3d9 |
3666 | break; |
3667 | case 7: |
a8327734 |
3668 | refresh_window(term->frontend); |
68f9b3d9 |
3669 | break; |
3670 | case 8: |
887035a5 |
3671 | if (term->esc_nargs >= 3) { |
64734920 |
3672 | if (!term->cfg.no_remote_resize) |
a8327734 |
3673 | request_resize(term->frontend, |
64734920 |
3674 | def(term->esc_args[2], term->cfg.width), |
3675 | def(term->esc_args[1], term->cfg.height)); |
68f9b3d9 |
3676 | } |
3677 | break; |
3678 | case 9: |
887035a5 |
3679 | if (term->esc_nargs >= 2) |
a8327734 |
3680 | set_zoomed(term->frontend, |
3681 | term->esc_args[1] ? |
887035a5 |
3682 | TRUE : FALSE); |
68f9b3d9 |
3683 | break; |
3684 | case 11: |
b9d7bcad |
3685 | if (term->ldisc) |
3686 | ldisc_send(term->ldisc, |
a8327734 |
3687 | is_iconic(term->frontend) ? |
3688 | "\033[1t" : "\033[2t", 4, 0); |
68f9b3d9 |
3689 | break; |
3690 | case 13: |
b9d7bcad |
3691 | if (term->ldisc) { |
a8327734 |
3692 | get_window_pos(term->frontend, &x, &y); |
b9d7bcad |
3693 | len = sprintf(buf, "\033[3;%d;%dt", x, y); |
3694 | ldisc_send(term->ldisc, buf, len, 0); |
3695 | } |
68f9b3d9 |
3696 | break; |
3697 | case 14: |
b9d7bcad |
3698 | if (term->ldisc) { |
a8327734 |
3699 | get_window_pixels(term->frontend, &x, &y); |
b9d7bcad |
3700 | len = sprintf(buf, "\033[4;%d;%dt", x, y); |
3701 | ldisc_send(term->ldisc, buf, len, 0); |
3702 | } |
68f9b3d9 |
3703 | break; |
3704 | case 18: |
b9d7bcad |
3705 | if (term->ldisc) { |
3706 | len = sprintf(buf, "\033[8;%d;%dt", |
3707 | term->rows, term->cols); |
3708 | ldisc_send(term->ldisc, buf, len, 0); |
3709 | } |
68f9b3d9 |
3710 | break; |
3711 | case 19: |
3712 | /* |
3713 | * Hmmm. Strictly speaking we |
3714 | * should return `the size of the |
3715 | * screen in characters', but |
3716 | * that's not easy: (a) window |
3717 | * furniture being what it is it's |
3718 | * hard to compute, and (b) in |
3719 | * resize-font mode maximising the |
3720 | * window wouldn't change the |
3721 | * number of characters. *shrug*. I |
3722 | * think we'll ignore it for the |
3723 | * moment and see if anyone |
3724 | * complains, and then ask them |
3725 | * what they would like it to do. |
3726 | */ |
3727 | break; |
3728 | case 20: |
7fcdebd3 |
3729 | if (term->ldisc && |
3730 | !term->cfg.no_remote_qtitle) { |
a8327734 |
3731 | p = get_window_title(term->frontend, TRUE); |
b9d7bcad |
3732 | len = strlen(p); |
3733 | ldisc_send(term->ldisc, "\033]L", 3, 0); |
3734 | ldisc_send(term->ldisc, p, len, 0); |
3735 | ldisc_send(term->ldisc, "\033\\", 2, 0); |
3736 | } |
68f9b3d9 |
3737 | break; |
3738 | case 21: |
7fcdebd3 |
3739 | if (term->ldisc && |
3740 | !term->cfg.no_remote_qtitle) { |
a8327734 |
3741 | p = get_window_title(term->frontend,FALSE); |
b9d7bcad |
3742 | len = strlen(p); |
3743 | ldisc_send(term->ldisc, "\033]l", 3, 0); |
3744 | ldisc_send(term->ldisc, p, len, 0); |
3745 | ldisc_send(term->ldisc, "\033\\", 2, 0); |
3746 | } |
68f9b3d9 |
3747 | break; |
3748 | } |
32874aea |
3749 | } |
3750 | break; |
a263e4f6 |
3751 | case 'S': /* SU: Scroll up */ |
1a837633 |
3752 | compatibility(SCOANSI); |
887035a5 |
3753 | scroll(term, term->marg_t, term->marg_b, |
3754 | def(term->esc_args[0], 1), TRUE); |
887035a5 |
3755 | term->wrapnext = FALSE; |
39934deb |
3756 | seen_disp_event(term); |
1a837633 |
3757 | break; |
a263e4f6 |
3758 | case 'T': /* SD: Scroll down */ |
1a837633 |
3759 | compatibility(SCOANSI); |
887035a5 |
3760 | scroll(term, term->marg_t, term->marg_b, |
3761 | -def(term->esc_args[0], 1), TRUE); |
887035a5 |
3762 | term->wrapnext = FALSE; |
39934deb |
3763 | seen_disp_event(term); |
1a837633 |
3764 | break; |
a263e4f6 |
3765 | case ANSI('|', '*'): /* DECSNLS */ |
3766 | /* |
32874aea |
3767 | * Set number of lines on screen |
a263e4f6 |
3768 | * VT420 uses VGA like hardware and can |
3769 | * support any size in reasonable range |
3770 | * (24..49 AIUI) with no default specified. |
32874aea |
3771 | */ |
3772 | compatibility(VT420); |
887035a5 |
3773 | if (term->esc_nargs == 1 && term->esc_args[0] > 0) { |
64734920 |
3774 | if (!term->cfg.no_remote_resize) |
a8327734 |
3775 | request_resize(term->frontend, term->cols, |
887035a5 |
3776 | def(term->esc_args[0], |
64734920 |
3777 | term->cfg.height)); |
887035a5 |
3778 | deselect(term); |
32874aea |
3779 | } |
3780 | break; |
a263e4f6 |
3781 | case ANSI('|', '$'): /* DECSCPP */ |
3782 | /* |
32874aea |
3783 | * Set number of columns per page |
a263e4f6 |
3784 | * Docs imply range is only 80 or 132, but |
3785 | * I'll allow any. |
32874aea |
3786 | */ |
3787 | compatibility(VT340TEXT); |
887035a5 |
3788 | if (term->esc_nargs <= 1) { |
64734920 |
3789 | if (!term->cfg.no_remote_resize) |
a8327734 |
3790 | request_resize(term->frontend, |
3791 | def(term->esc_args[0], |
64734920 |
3792 | term->cfg.width), term->rows); |
887035a5 |
3793 | deselect(term); |
32874aea |
3794 | } |
3795 | break; |
a263e4f6 |
3796 | case 'X': /* ECH: write N spaces w/o moving cursor */ |
3797 | /* XXX VTTEST says this is vt220, vt510 manual |
3798 | * says vt100 */ |
32874aea |
3799 | compatibility(ANSIMIN); |
3800 | { |
887035a5 |
3801 | int n = def(term->esc_args[0], 1); |
32874aea |
3802 | pos cursplus; |
c6958dfe |
3803 | int p = term->curs.x; |
3804 | termline *cline = scrlineptr(term->curs.y); |
3805 | |
887035a5 |
3806 | if (n > term->cols - term->curs.x) |
3807 | n = term->cols - term->curs.x; |
3808 | cursplus = term->curs; |
32874aea |
3809 | cursplus.x += n; |
3c7366f8 |
3810 | check_boundary(term, term->curs.x, term->curs.y); |
3811 | check_boundary(term, term->curs.x+n, term->curs.y); |
887035a5 |
3812 | check_selection(term, term->curs, cursplus); |
32874aea |
3813 | while (n--) |
c6958dfe |
3814 | copy_termchar(cline, p++, |
3815 | &term->erase_char); |
39934deb |
3816 | seen_disp_event(term); |
32874aea |
3817 | } |
3818 | break; |
a263e4f6 |
3819 | case 'x': /* DECREQTPARM: report terminal characteristics */ |
32874aea |
3820 | compatibility(VT100); |
b9d7bcad |
3821 | if (term->ldisc) { |
32874aea |
3822 | char buf[32]; |
887035a5 |
3823 | int i = def(term->esc_args[0], 0); |
32874aea |
3824 | if (i == 0 || i == 1) { |
3825 | strcpy(buf, "\033[2;1;1;112;112;1;0x"); |
3826 | buf[2] += i; |
b9d7bcad |
3827 | ldisc_send(term->ldisc, buf, 20, 0); |
32874aea |
3828 | } |
3829 | } |
3830 | break; |
372a19d4 |
3831 | case 'Z': /* CBT */ |
979f6987 |
3832 | compatibility(OTHER); |
3833 | { |
887035a5 |
3834 | int i = def(term->esc_args[0], 1); |
3835 | pos old_curs = term->curs; |
979f6987 |
3836 | |
887035a5 |
3837 | for(;i>0 && term->curs.x>0; i--) { |
979f6987 |
3838 | do { |
887035a5 |
3839 | term->curs.x--; |
3840 | } while (term->curs.x >0 && |
3841 | !term->tabs[term->curs.x]); |
979f6987 |
3842 | } |
887035a5 |
3843 | check_selection(term, old_curs, term->curs); |
979f6987 |
3844 | } |
3845 | break; |
1b20b972 |
3846 | case ANSI('c', '='): /* Hide or Show Cursor */ |
3847 | compatibility(SCOANSI); |
3848 | switch(term->esc_args[0]) { |
3849 | case 0: /* hide cursor */ |
3850 | term->cursor_on = FALSE; |
3851 | break; |
3852 | case 1: /* restore cursor */ |
3853 | term->big_cursor = FALSE; |
3854 | term->cursor_on = TRUE; |
3855 | break; |
3856 | case 2: /* block cursor */ |
3857 | term->big_cursor = TRUE; |
3858 | term->cursor_on = TRUE; |
3859 | break; |
3860 | } |
3861 | break; |
3862 | case ANSI('C', '='): |
3863 | /* |
3864 | * set cursor start on scanline esc_args[0] and |
3865 | * end on scanline esc_args[1].If you set |
3866 | * the bottom scan line to a value less than |
3867 | * the top scan line, the cursor will disappear. |
3868 | */ |
3869 | compatibility(SCOANSI); |
3870 | if (term->esc_nargs >= 2) { |
3871 | if (term->esc_args[0] > term->esc_args[1]) |
3872 | term->cursor_on = FALSE; |
3873 | else |
3874 | term->cursor_on = TRUE; |
3875 | } |
3876 | break; |
3877 | case ANSI('D', '='): |
3878 | compatibility(SCOANSI); |
3879 | term->blink_is_real = FALSE; |
39934deb |
3880 | term_schedule_tblink(term); |
1b20b972 |
3881 | if (term->esc_args[0]>=1) |
3882 | term->curr_attr |= ATTR_BLINK; |
3883 | else |
3884 | term->curr_attr &= ~ATTR_BLINK; |
3885 | break; |
3886 | case ANSI('E', '='): |
3887 | compatibility(SCOANSI); |
3888 | term->blink_is_real = (term->esc_args[0] >= 1); |
39934deb |
3889 | term_schedule_tblink(term); |
1b20b972 |
3890 | break; |
3891 | case ANSI('F', '='): /* set normal foreground */ |
3892 | compatibility(SCOANSI); |
3893 | if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) { |
69761fd5 |
3894 | long colour = |
3895 | (sco2ansicolour[term->esc_args[0] & 0x7] | |
cecb13f6 |
3896 | (term->esc_args[0] & 0x8)) << |
1b20b972 |
3897 | ATTR_FGSHIFT; |
69761fd5 |
3898 | term->curr_attr &= ~ATTR_FGMASK; |
3899 | term->curr_attr |= colour; |
3900 | term->default_attr &= ~ATTR_FGMASK; |
3901 | term->default_attr |= colour; |
f2e12161 |
3902 | set_erase_char(term); |
1b20b972 |
3903 | } |
3904 | break; |
3905 | case ANSI('G', '='): /* set normal background */ |
3906 | compatibility(SCOANSI); |
3907 | if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) { |
69761fd5 |
3908 | long colour = |
3909 | (sco2ansicolour[term->esc_args[0] & 0x7] | |
cecb13f6 |
3910 | (term->esc_args[0] & 0x8)) << |
1b20b972 |
3911 | ATTR_BGSHIFT; |
69761fd5 |
3912 | term->curr_attr &= ~ATTR_BGMASK; |
3913 | term->curr_attr |= colour; |
3914 | term->default_attr &= ~ATTR_BGMASK; |
3915 | term->default_attr |= colour; |
f2e12161 |
3916 | set_erase_char(term); |
1b20b972 |
3917 | } |
3918 | break; |
32874aea |
3919 | case ANSI('L', '='): |
1b20b972 |
3920 | compatibility(SCOANSI); |
887035a5 |
3921 | term->use_bce = (term->esc_args[0] <= 0); |
36566009 |
3922 | set_erase_char(term); |
32874aea |
3923 | break; |
a263e4f6 |
3924 | case ANSI('p', '"'): /* DECSCL: set compat level */ |
887035a5 |
3925 | /* |
3926 | * Allow the host to make this emulator a |
3927 | * 'perfect' VT102. This first appeared in |
3928 | * the VT220, but we do need to get back to |
3929 | * PuTTY mode so I won't check it. |
e14a5a13 |
3930 | * |
4eeb7d09 |
3931 | * The arg in 40..42,50 are a PuTTY extension. |
32874aea |
3932 | * The 2nd arg, 8bit vs 7bit is not checked. |
3933 | * |
887035a5 |
3934 | * Setting VT102 mode should also change |
3935 | * the Fkeys to generate PF* codes as a |
3936 | * real VT102 has no Fkeys. The VT220 does |
3937 | * this, F11..F13 become ESC,BS,LF other |
3938 | * Fkeys send nothing. |
32874aea |
3939 | * |
3940 | * Note ESC c will NOT change this! |
374330e2 |
3941 | */ |
32874aea |
3942 | |
887035a5 |
3943 | switch (term->esc_args[0]) { |
32874aea |
3944 | case 61: |
887035a5 |
3945 | term->compatibility_level &= ~TM_VTXXX; |
3946 | term->compatibility_level |= TM_VT102; |
374330e2 |
3947 | break; |
32874aea |
3948 | case 62: |
887035a5 |
3949 | term->compatibility_level &= ~TM_VTXXX; |
3950 | term->compatibility_level |= TM_VT220; |
32874aea |
3951 | break; |
3952 | |
3953 | default: |
887035a5 |
3954 | if (term->esc_args[0] > 60 && |
3955 | term->esc_args[0] < 70) |
3956 | term->compatibility_level |= TM_VTXXX; |
32874aea |
3957 | break; |
3958 | |
3959 | case 40: |
887035a5 |
3960 | term->compatibility_level &= TM_VTXXX; |
374330e2 |
3961 | break; |
32874aea |
3962 | case 41: |
887035a5 |
3963 | term->compatibility_level = TM_PUTTY; |
374330e2 |
3964 | break; |
32874aea |
3965 | case 42: |
887035a5 |
3966 | term->compatibility_level = TM_SCOANSI; |
374330e2 |
3967 | break; |
32874aea |
3968 | |
3969 | case ARG_DEFAULT: |
887035a5 |
3970 | term->compatibility_level = TM_PUTTY; |
32874aea |
3971 | break; |
3972 | case 50: |
3973 | break; |
3974 | } |
3975 | |
3976 | /* Change the response to CSI c */ |
887035a5 |
3977 | if (term->esc_args[0] == 50) { |
32874aea |
3978 | int i; |
3979 | char lbuf[64]; |
887035a5 |
3980 | strcpy(term->id_string, "\033[?"); |
3981 | for (i = 1; i < term->esc_nargs; i++) { |
32874aea |
3982 | if (i != 1) |
887035a5 |
3983 | strcat(term->id_string, ";"); |
3984 | sprintf(lbuf, "%d", term->esc_args[i]); |
3985 | strcat(term->id_string, lbuf); |
32874aea |
3986 | } |
887035a5 |
3987 | strcat(term->id_string, "c"); |
32874aea |
3988 | } |
3989 | #if 0 |
3990 | /* Is this a good idea ? |
3991 | * Well we should do a soft reset at this point ... |
3992 | */ |
3993 | if (!has_compat(VT420) && has_compat(VT100)) { |
64734920 |
3994 | if (!term->cfg.no_remote_resize) { |
887035a5 |
3995 | if (term->reset_132) |
0d2086c5 |
3996 | request_resize(132, 24); |
3997 | else |
3998 | request_resize(80, 24); |
3999 | } |
374330e2 |
4000 | } |
32874aea |
4001 | #endif |
4002 | break; |
374330e2 |
4003 | } |
374330e2 |
4004 | break; |
32874aea |
4005 | case SEEN_OSC: |
887035a5 |
4006 | term->osc_w = FALSE; |
32874aea |
4007 | switch (c) { |
4008 | case 'P': /* Linux palette sequence */ |
887035a5 |
4009 | term->termstate = SEEN_OSC_P; |
4010 | term->osc_strlen = 0; |
32874aea |
4011 | break; |
4012 | case 'R': /* Linux palette reset */ |
a8327734 |
4013 | palette_reset(term->frontend); |
887035a5 |
4014 | term_invalidate(term); |
4015 | term->termstate = TOPLEVEL; |
32874aea |
4016 | break; |
4017 | case 'W': /* word-set */ |
887035a5 |
4018 | term->termstate = SEEN_OSC_W; |
4019 | term->osc_w = TRUE; |
32874aea |
4020 | break; |
4021 | case '0': |
4022 | case '1': |
4023 | case '2': |
4024 | case '3': |
4025 | case '4': |
4026 | case '5': |
4027 | case '6': |
4028 | case '7': |
4029 | case '8': |
4030 | case '9': |
887035a5 |
4031 | term->esc_args[0] = 10 * term->esc_args[0] + c - '0'; |
32874aea |
4032 | break; |
4033 | case 'L': |
4034 | /* |
4035 | * Grotty hack to support xterm and DECterm title |
4036 | * sequences concurrently. |
4037 | */ |
887035a5 |
4038 | if (term->esc_args[0] == 2) { |
4039 | term->esc_args[0] = 1; |
32874aea |
4040 | break; |
4041 | } |
4042 | /* else fall through */ |
4043 | default: |
887035a5 |
4044 | term->termstate = OSC_STRING; |
4045 | term->osc_strlen = 0; |
e14a5a13 |
4046 | } |
4047 | break; |
32874aea |
4048 | case OSC_STRING: |
4049 | /* |
4050 | * This OSC stuff is EVIL. It takes just one character to get into |
4051 | * sysline mode and it's not initially obvious how to get out. |
4052 | * So I've added CR and LF as string aborts. |
4053 | * This shouldn't effect compatibility as I believe embedded |
4054 | * control characters are supposed to be interpreted (maybe?) |
4055 | * and they don't display anything useful anyway. |
4056 | * |
4057 | * -- RDB |
e14a5a13 |
4058 | */ |
92e23ad9 |
4059 | if (c == '\012' || c == '\015') { |
887035a5 |
4060 | term->termstate = TOPLEVEL; |
32874aea |
4061 | } else if (c == 0234 || c == '\007') { |
4062 | /* |
4063 | * These characters terminate the string; ST and BEL |
4064 | * terminate the sequence and trigger instant |
4065 | * processing of it, whereas ESC goes back to SEEN_ESC |
4066 | * mode unless it is followed by \, in which case it is |
4067 | * synonymous with ST in the first place. |
4068 | */ |
887035a5 |
4069 | do_osc(term); |
4070 | term->termstate = TOPLEVEL; |
32874aea |
4071 | } else if (c == '\033') |
887035a5 |
4072 | term->termstate = OSC_MAYBE_ST; |
4073 | else if (term->osc_strlen < OSC_STR_MAX) |
36566009 |
4074 | term->osc_string[term->osc_strlen++] = (char)c; |
374330e2 |
4075 | break; |
32874aea |
4076 | case SEEN_OSC_P: |
374330e2 |
4077 | { |
887035a5 |
4078 | int max = (term->osc_strlen == 0 ? 21 : 16); |
32874aea |
4079 | int val; |
36566009 |
4080 | if ((int)c >= '0' && (int)c <= '9') |
32874aea |
4081 | val = c - '0'; |
36566009 |
4082 | else if ((int)c >= 'A' && (int)c <= 'A' + max - 10) |
32874aea |
4083 | val = c - 'A' + 10; |
36566009 |
4084 | else if ((int)c >= 'a' && (int)c <= 'a' + max - 10) |
32874aea |
4085 | val = c - 'a' + 10; |
2d466ffd |
4086 | else { |
887035a5 |
4087 | term->termstate = TOPLEVEL; |
2d466ffd |
4088 | break; |
4089 | } |
887035a5 |
4090 | term->osc_string[term->osc_strlen++] = val; |
4091 | if (term->osc_strlen >= 7) { |
a8327734 |
4092 | palette_set(term->frontend, term->osc_string[0], |
887035a5 |
4093 | term->osc_string[1] * 16 + term->osc_string[2], |
4094 | term->osc_string[3] * 16 + term->osc_string[4], |
4095 | term->osc_string[5] * 16 + term->osc_string[6]); |
4096 | term_invalidate(term); |
4097 | term->termstate = TOPLEVEL; |
374330e2 |
4098 | } |
4099 | } |
4100 | break; |
32874aea |
4101 | case SEEN_OSC_W: |
4102 | switch (c) { |
4103 | case '0': |
4104 | case '1': |
4105 | case '2': |
4106 | case '3': |
4107 | case '4': |
4108 | case '5': |
4109 | case '6': |
4110 | case '7': |
4111 | case '8': |
4112 | case '9': |
887035a5 |
4113 | term->esc_args[0] = 10 * term->esc_args[0] + c - '0'; |
32874aea |
4114 | break; |
4115 | default: |
887035a5 |
4116 | term->termstate = OSC_STRING; |
4117 | term->osc_strlen = 0; |
ec55b220 |
4118 | } |
32874aea |
4119 | break; |
32874aea |
4120 | case VT52_ESC: |
887035a5 |
4121 | term->termstate = TOPLEVEL; |
39934deb |
4122 | seen_disp_event(term); |
c9def1b8 |
4123 | switch (c) { |
32874aea |
4124 | case 'A': |
887035a5 |
4125 | move(term, term->curs.x, term->curs.y - 1, 1); |
32874aea |
4126 | break; |
4127 | case 'B': |
887035a5 |
4128 | move(term, term->curs.x, term->curs.y + 1, 1); |
32874aea |
4129 | break; |
4130 | case 'C': |
887035a5 |
4131 | move(term, term->curs.x + 1, term->curs.y, 1); |
32874aea |
4132 | break; |
4133 | case 'D': |
887035a5 |
4134 | move(term, term->curs.x - 1, term->curs.y, 1); |
32874aea |
4135 | break; |
4eeb7d09 |
4136 | /* |
4137 | * From the VT100 Manual |
4138 | * NOTE: The special graphics characters in the VT100 |
4139 | * are different from those in the VT52 |
4140 | * |
4141 | * From VT102 manual: |
4142 | * 137 _ Blank - Same |
4143 | * 140 ` Reserved - Humm. |
4144 | * 141 a Solid rectangle - Similar |
4145 | * 142 b 1/ - Top half of fraction for the |
4146 | * 143 c 3/ - subscript numbers below. |
4147 | * 144 d 5/ |
4148 | * 145 e 7/ |
4149 | * 146 f Degrees - Same |
4150 | * 147 g Plus or minus - Same |
4151 | * 150 h Right arrow |
4152 | * 151 i Ellipsis (dots) |
4153 | * 152 j Divide by |
4154 | * 153 k Down arrow |
4155 | * 154 l Bar at scan 0 |
4156 | * 155 m Bar at scan 1 |
4157 | * 156 n Bar at scan 2 |
4158 | * 157 o Bar at scan 3 - Similar |
4159 | * 160 p Bar at scan 4 - Similar |
4160 | * 161 q Bar at scan 5 - Similar |
4161 | * 162 r Bar at scan 6 - Same |
4162 | * 163 s Bar at scan 7 - Similar |
4163 | * 164 t Subscript 0 |
4164 | * 165 u Subscript 1 |
4165 | * 166 v Subscript 2 |
4166 | * 167 w Subscript 3 |
4167 | * 170 x Subscript 4 |
4168 | * 171 y Subscript 5 |
4169 | * 172 z Subscript 6 |
4170 | * 173 { Subscript 7 |
4171 | * 174 | Subscript 8 |
4172 | * 175 } Subscript 9 |
4173 | * 176 ~ Paragraph |
4174 | * |
4175 | */ |
32874aea |
4176 | case 'F': |
36566009 |
4177 | term->cset_attr[term->cset = 0] = CSET_LINEDRW; |
32874aea |
4178 | break; |
4179 | case 'G': |
36566009 |
4180 | term->cset_attr[term->cset = 0] = CSET_ASCII; |
32874aea |
4181 | break; |
4182 | case 'H': |
887035a5 |
4183 | move(term, 0, 0, 0); |
32874aea |
4184 | break; |
4185 | case 'I': |
887035a5 |
4186 | if (term->curs.y == 0) |
4187 | scroll(term, 0, term->rows - 1, -1, TRUE); |
4188 | else if (term->curs.y > 0) |
4189 | term->curs.y--; |
887035a5 |
4190 | term->wrapnext = FALSE; |
32874aea |
4191 | break; |
4192 | case 'J': |
887035a5 |
4193 | erase_lots(term, FALSE, FALSE, TRUE); |
4194 | term->disptop = 0; |
c9def1b8 |
4195 | break; |
32874aea |
4196 | case 'K': |
887035a5 |
4197 | erase_lots(term, TRUE, FALSE, TRUE); |
32874aea |
4198 | break; |
4eeb7d09 |
4199 | #if 0 |
32874aea |
4200 | case 'V': |
4201 | /* XXX Print cursor line */ |
4202 | break; |
4203 | case 'W': |
4204 | /* XXX Start controller mode */ |
4205 | break; |
4206 | case 'X': |
4207 | /* XXX Stop controller mode */ |
4208 | break; |
4eeb7d09 |
4209 | #endif |
32874aea |
4210 | case 'Y': |
887035a5 |
4211 | term->termstate = VT52_Y1; |
32874aea |
4212 | break; |
4213 | case 'Z': |
b9d7bcad |
4214 | if (term->ldisc) |
4215 | ldisc_send(term->ldisc, "\033/Z", 3, 0); |
32874aea |
4216 | break; |
4217 | case '=': |
887035a5 |
4218 | term->app_keypad_keys = TRUE; |
32874aea |
4219 | break; |
4220 | case '>': |
887035a5 |
4221 | term->app_keypad_keys = FALSE; |
32874aea |
4222 | break; |
4223 | case '<': |
4224 | /* XXX This should switch to VT100 mode not current or default |
4225 | * VT mode. But this will only have effect in a VT220+ |
4226 | * emulation. |
4227 | */ |
887035a5 |
4228 | term->vt52_mode = FALSE; |
64734920 |
4229 | term->blink_is_real = term->cfg.blinktext; |
39934deb |
4230 | term_schedule_tblink(term); |
32874aea |
4231 | break; |
4eeb7d09 |
4232 | #if 0 |
32874aea |
4233 | case '^': |
4234 | /* XXX Enter auto print mode */ |
4235 | break; |
4236 | case '_': |
4237 | /* XXX Exit auto print mode */ |
4238 | break; |
4239 | case ']': |
4240 | /* XXX Print screen */ |
4241 | break; |
4eeb7d09 |
4242 | #endif |
4243 | |
4244 | #ifdef VT52_PLUS |
4245 | case 'E': |
4246 | /* compatibility(ATARI) */ |
887035a5 |
4247 | move(term, 0, 0, 0); |
4248 | erase_lots(term, FALSE, FALSE, TRUE); |
4249 | term->disptop = 0; |
4eeb7d09 |
4250 | break; |
4251 | case 'L': |
4252 | /* compatibility(ATARI) */ |
887035a5 |
4253 | if (term->curs.y <= term->marg_b) |
4254 | scroll(term, term->curs.y, term->marg_b, -1, FALSE); |
4eeb7d09 |
4255 | break; |
4256 | case 'M': |
4257 | /* compatibility(ATARI) */ |
887035a5 |
4258 | if (term->curs.y <= term->marg_b) |
4259 | scroll(term, term->curs.y, term->marg_b, 1, TRUE); |
4eeb7d09 |
4260 | break; |
4261 | case 'b': |
4262 | /* compatibility(ATARI) */ |
887035a5 |
4263 | term->termstate = VT52_FG; |
4eeb7d09 |
4264 | break; |
4265 | case 'c': |
4266 | /* compatibility(ATARI) */ |
887035a5 |
4267 | term->termstate = VT52_BG; |
4eeb7d09 |
4268 | break; |
4269 | case 'd': |
4270 | /* compatibility(ATARI) */ |
887035a5 |
4271 | erase_lots(term, FALSE, TRUE, FALSE); |
4272 | term->disptop = 0; |
4eeb7d09 |
4273 | break; |
4274 | case 'e': |
4275 | /* compatibility(ATARI) */ |
887035a5 |
4276 | term->cursor_on = TRUE; |
4eeb7d09 |
4277 | break; |
4278 | case 'f': |
4279 | /* compatibility(ATARI) */ |
887035a5 |
4280 | term->cursor_on = FALSE; |
4eeb7d09 |
4281 | break; |
4282 | /* case 'j': Save cursor position - broken on ST */ |
4283 | /* case 'k': Restore cursor position */ |
4284 | case 'l': |
4285 | /* compatibility(ATARI) */ |
887035a5 |
4286 | erase_lots(term, TRUE, TRUE, TRUE); |
4287 | term->curs.x = 0; |
4288 | term->wrapnext = FALSE; |
4eeb7d09 |
4289 | break; |
4290 | case 'o': |
4291 | /* compatibility(ATARI) */ |
887035a5 |
4292 | erase_lots(term, TRUE, TRUE, FALSE); |
4eeb7d09 |
4293 | break; |
4294 | case 'p': |
4295 | /* compatibility(ATARI) */ |
887035a5 |
4296 | term->curr_attr |= ATTR_REVERSE; |
4eeb7d09 |
4297 | break; |
4298 | case 'q': |
4299 | /* compatibility(ATARI) */ |
887035a5 |
4300 | term->curr_attr &= ~ATTR_REVERSE; |
4eeb7d09 |
4301 | break; |
4302 | case 'v': /* wrap Autowrap on - Wyse style */ |
4303 | /* compatibility(ATARI) */ |
887035a5 |
4304 | term->wrap = 1; |
4eeb7d09 |
4305 | break; |
4306 | case 'w': /* Autowrap off */ |
4307 | /* compatibility(ATARI) */ |
887035a5 |
4308 | term->wrap = 0; |
4eeb7d09 |
4309 | break; |
4310 | |
4311 | case 'R': |
4312 | /* compatibility(OTHER) */ |
887035a5 |
4313 | term->vt52_bold = FALSE; |
4314 | term->curr_attr = ATTR_DEFAULT; |
36566009 |
4315 | set_erase_char(term); |
4eeb7d09 |
4316 | break; |
4317 | case 'S': |
4318 | /* compatibility(VI50) */ |
887035a5 |
4319 | term->curr_attr |= ATTR_UNDER; |
4eeb7d09 |
4320 | break; |
4321 | case 'W': |
4322 | /* compatibility(VI50) */ |
887035a5 |
4323 | term->curr_attr &= ~ATTR_UNDER; |
4eeb7d09 |
4324 | break; |
4325 | case 'U': |
4326 | /* compatibility(VI50) */ |
887035a5 |
4327 | term->vt52_bold = TRUE; |
4328 | term->curr_attr |= ATTR_BOLD; |
4eeb7d09 |
4329 | break; |
4330 | case 'T': |
4331 | /* compatibility(VI50) */ |
887035a5 |
4332 | term->vt52_bold = FALSE; |
4333 | term->curr_attr &= ~ATTR_BOLD; |
4eeb7d09 |
4334 | break; |
4335 | #endif |
c9def1b8 |
4336 | } |
e14a5a13 |
4337 | break; |
32874aea |
4338 | case VT52_Y1: |
887035a5 |
4339 | term->termstate = VT52_Y2; |
4340 | move(term, term->curs.x, c - ' ', 0); |
e14a5a13 |
4341 | break; |
32874aea |
4342 | case VT52_Y2: |
887035a5 |
4343 | term->termstate = TOPLEVEL; |
4344 | move(term, c - ' ', term->curs.y, 0); |
e14a5a13 |
4345 | break; |
4eeb7d09 |
4346 | |
4347 | #ifdef VT52_PLUS |
4348 | case VT52_FG: |
887035a5 |
4349 | term->termstate = TOPLEVEL; |
4350 | term->curr_attr &= ~ATTR_FGMASK; |
4351 | term->curr_attr &= ~ATTR_BOLD; |
cecb13f6 |
4352 | term->curr_attr |= (c & 0xF) << ATTR_FGSHIFT; |
36566009 |
4353 | set_erase_char(term); |
4eeb7d09 |
4354 | break; |
4355 | case VT52_BG: |
887035a5 |
4356 | term->termstate = TOPLEVEL; |
4357 | term->curr_attr &= ~ATTR_BGMASK; |
4358 | term->curr_attr &= ~ATTR_BLINK; |
cecb13f6 |
4359 | term->curr_attr |= (c & 0xF) << ATTR_BGSHIFT; |
36566009 |
4360 | set_erase_char(term); |
4eeb7d09 |
4361 | break; |
4362 | #endif |
2d466ffd |
4363 | default: break; /* placate gcc warning about enum use */ |
e14a5a13 |
4364 | } |
887035a5 |
4365 | if (term->selstate != NO_SELECTION) { |
4366 | pos cursplus = term->curs; |
4facdf84 |
4367 | incpos(cursplus); |
887035a5 |
4368 | check_selection(term, term->curs, cursplus); |
4facdf84 |
4369 | } |
374330e2 |
4370 | } |
b44b307a |
4371 | |
887035a5 |
4372 | term_print_flush(term); |
6d60c791 |
4373 | if (term->cfg.logflush) |
4374 | logflush(term->logctx); |
374330e2 |
4375 | } |
4376 | |
374330e2 |
4377 | /* |
f0fccd51 |
4378 | * To prevent having to run the reasonably tricky bidi algorithm |
4379 | * too many times, we maintain a cache of the last lineful of data |
4380 | * fed to the algorithm on each line of the display. |
4381 | */ |
4382 | static int term_bidi_cache_hit(Terminal *term, int line, |
36566009 |
4383 | termchar *lbefore, int width) |
f0fccd51 |
4384 | { |
c6958dfe |
4385 | int i; |
4386 | |
f0fccd51 |
4387 | if (!term->pre_bidi_cache) |
4388 | return FALSE; /* cache doesn't even exist yet! */ |
4389 | |
4390 | if (line >= term->bidi_cache_size) |
4391 | return FALSE; /* cache doesn't have this many lines */ |
4392 | |
2caf3cd8 |
4393 | if (!term->pre_bidi_cache[line].chars) |
f0fccd51 |
4394 | return FALSE; /* cache doesn't contain _this_ line */ |
4395 | |
2caf3cd8 |
4396 | if (term->pre_bidi_cache[line].width != width) |
4397 | return FALSE; /* line is wrong width */ |
4398 | |
c6958dfe |
4399 | for (i = 0; i < width; i++) |
2caf3cd8 |
4400 | if (!termchars_equal(term->pre_bidi_cache[line].chars+i, lbefore+i)) |
c6958dfe |
4401 | return FALSE; /* line doesn't match cache */ |
f0fccd51 |
4402 | |
c6958dfe |
4403 | return TRUE; /* it didn't match. */ |
f0fccd51 |
4404 | } |
4405 | |
36566009 |
4406 | static void term_bidi_cache_store(Terminal *term, int line, termchar *lbefore, |
3c5a620c |
4407 | termchar *lafter, bidi_char *wcTo, |
6ed98665 |
4408 | int width, int size) |
f0fccd51 |
4409 | { |
3c5a620c |
4410 | int i; |
4411 | |
f0fccd51 |
4412 | if (!term->pre_bidi_cache || term->bidi_cache_size <= line) { |
4413 | int j = term->bidi_cache_size; |
4414 | term->bidi_cache_size = line+1; |
4415 | term->pre_bidi_cache = sresize(term->pre_bidi_cache, |
4416 | term->bidi_cache_size, |
2caf3cd8 |
4417 | struct bidi_cache_entry); |
f0fccd51 |
4418 | term->post_bidi_cache = sresize(term->post_bidi_cache, |
4419 | term->bidi_cache_size, |
2caf3cd8 |
4420 | struct bidi_cache_entry); |
f0fccd51 |
4421 | while (j < term->bidi_cache_size) { |
2caf3cd8 |
4422 | term->pre_bidi_cache[j].chars = |
4423 | term->post_bidi_cache[j].chars = NULL; |
4424 | term->pre_bidi_cache[j].width = |
4425 | term->post_bidi_cache[j].width = -1; |
9e7c372c |
4426 | term->pre_bidi_cache[j].forward = |
4427 | term->post_bidi_cache[j].forward = NULL; |
4428 | term->pre_bidi_cache[j].backward = |
4429 | term->post_bidi_cache[j].backward = NULL; |
f0fccd51 |
4430 | j++; |
4431 | } |
4432 | } |
4433 | |
2caf3cd8 |
4434 | sfree(term->pre_bidi_cache[line].chars); |
4435 | sfree(term->post_bidi_cache[line].chars); |
9e7c372c |
4436 | sfree(term->post_bidi_cache[line].forward); |
4437 | sfree(term->post_bidi_cache[line].backward); |
f0fccd51 |
4438 | |
2caf3cd8 |
4439 | term->pre_bidi_cache[line].width = width; |
6ed98665 |
4440 | term->pre_bidi_cache[line].chars = snewn(size, termchar); |
2caf3cd8 |
4441 | term->post_bidi_cache[line].width = width; |
6ed98665 |
4442 | term->post_bidi_cache[line].chars = snewn(size, termchar); |
3c5a620c |
4443 | term->post_bidi_cache[line].forward = snewn(width, int); |
4444 | term->post_bidi_cache[line].backward = snewn(width, int); |
f0fccd51 |
4445 | |
6ed98665 |
4446 | memcpy(term->pre_bidi_cache[line].chars, lbefore, size * TSIZE); |
4447 | memcpy(term->post_bidi_cache[line].chars, lafter, size * TSIZE); |
3c5a620c |
4448 | memset(term->post_bidi_cache[line].forward, 0, width * sizeof(int)); |
4449 | memset(term->post_bidi_cache[line].backward, 0, width * sizeof(int)); |
4450 | |
4451 | for (i = 0; i < width; i++) { |
4452 | int p = wcTo[i].index; |
4453 | |
4454 | assert(0 <= p && p < width); |
4455 | |
4456 | term->post_bidi_cache[line].backward[i] = p; |
4457 | term->post_bidi_cache[line].forward[p] = i; |
4458 | } |
4459 | } |
4460 | |
4461 | /* |
4462 | * Prepare the bidi information for a screen line. Returns the |
4463 | * transformed list of termchars, or NULL if no transformation at |
4464 | * all took place (because bidi is disabled). If return was |
4465 | * non-NULL, auxiliary information such as the forward and reverse |
4466 | * mappings of permutation position are available in |
4467 | * term->post_bidi_cache[scr_y].*. |
4468 | */ |
4469 | static termchar *term_bidi_line(Terminal *term, struct termline *ldata, |
4470 | int scr_y) |
4471 | { |
4472 | termchar *lchars; |
4473 | int it; |
4474 | |
4475 | /* Do Arabic shaping and bidi. */ |
4476 | if(!term->cfg.bidi || !term->cfg.arabicshaping) { |
4477 | |
4478 | if (!term_bidi_cache_hit(term, scr_y, ldata->chars, term->cols)) { |
4479 | |
4480 | if (term->wcFromTo_size < term->cols) { |
4481 | term->wcFromTo_size = term->cols; |
4482 | term->wcFrom = sresize(term->wcFrom, term->wcFromTo_size, |
4483 | bidi_char); |
4484 | term->wcTo = sresize(term->wcTo, term->wcFromTo_size, |
4485 | bidi_char); |
4486 | } |
4487 | |
4488 | for(it=0; it<term->cols ; it++) |
4489 | { |
4490 | unsigned long uc = (ldata->chars[it].chr); |
4491 | |
4492 | switch (uc & CSET_MASK) { |
4493 | case CSET_LINEDRW: |
4494 | if (!term->cfg.rawcnp) { |
4495 | uc = term->ucsdata->unitab_xterm[uc & 0xFF]; |
4496 | break; |
4497 | } |
4498 | case CSET_ASCII: |
4499 | uc = term->ucsdata->unitab_line[uc & 0xFF]; |
4500 | break; |
4501 | case CSET_SCOACS: |
4502 | uc = term->ucsdata->unitab_scoacs[uc&0xFF]; |
4503 | break; |
4504 | } |
4505 | switch (uc & CSET_MASK) { |
4506 | case CSET_ACP: |
4507 | uc = term->ucsdata->unitab_font[uc & 0xFF]; |
4508 | break; |
4509 | case CSET_OEMCP: |
4510 | uc = term->ucsdata->unitab_oemcp[uc & 0xFF]; |
4511 | break; |
4512 | } |
4513 | |
4514 | term->wcFrom[it].origwc = term->wcFrom[it].wc = |
4515 | (wchar_t)uc; |
4516 | term->wcFrom[it].index = it; |
4517 | } |
4518 | |
4519 | if(!term->cfg.bidi) |
4520 | do_bidi(term->wcFrom, term->cols); |
4521 | |
4522 | /* this is saved iff done from inside the shaping */ |
4523 | if(!term->cfg.bidi && term->cfg.arabicshaping) |
4524 | for(it=0; it<term->cols; it++) |
4525 | term->wcTo[it] = term->wcFrom[it]; |
4526 | |
4527 | if(!term->cfg.arabicshaping) |
4528 | do_shape(term->wcFrom, term->wcTo, term->cols); |
4529 | |
4530 | if (term->ltemp_size < ldata->size) { |
4531 | term->ltemp_size = ldata->size; |
4532 | term->ltemp = sresize(term->ltemp, term->ltemp_size, |
4533 | termchar); |
4534 | } |
4535 | |
4536 | memcpy(term->ltemp, ldata->chars, ldata->size * TSIZE); |
4537 | |
4538 | for(it=0; it<term->cols ; it++) |
4539 | { |
4540 | term->ltemp[it] = ldata->chars[term->wcTo[it].index]; |
4541 | if (term->ltemp[it].cc_next) |
4542 | term->ltemp[it].cc_next -= |
4543 | it - term->wcTo[it].index; |
4544 | |
4545 | if (term->wcTo[it].origwc != term->wcTo[it].wc) |
4546 | term->ltemp[it].chr = term->wcTo[it].wc; |
4547 | } |
4548 | term_bidi_cache_store(term, scr_y, ldata->chars, |
6ed98665 |
4549 | term->ltemp, term->wcTo, |
4550 | term->cols, ldata->size); |
3c5a620c |
4551 | |
4552 | lchars = term->ltemp; |
4553 | } else { |
4554 | lchars = term->post_bidi_cache[scr_y].chars; |
4555 | } |
4556 | } else { |
4557 | lchars = NULL; |
4558 | } |
4559 | |
4560 | return lchars; |
f0fccd51 |
4561 | } |
4562 | |
4563 | /* |
374330e2 |
4564 | * Given a context, update the window. Out of paranoia, we don't |
4565 | * allow WM_PAINT responses to do scrolling optimisations. |
4566 | */ |
887035a5 |
4567 | static void do_paint(Terminal *term, Context ctx, int may_optimise) |
32874aea |
4568 | { |
3c5a620c |
4569 | int i, j, our_curs_y, our_curs_x; |
36566009 |
4570 | int rv, cursor; |
4facdf84 |
4571 | pos scrpos; |
c6958dfe |
4572 | wchar_t *ch; |
4573 | int chlen; |
341eb978 |
4574 | #ifdef OPTIMISE_SCROLL |
4575 | struct scrollregion *sr; |
4576 | #endif /* OPTIMISE_SCROLL */ |
efc411b3 |
4577 | termchar *newline; |
374330e2 |
4578 | |
c6958dfe |
4579 | chlen = 1024; |
4580 | ch = snewn(chlen, wchar_t); |
4581 | |
efc411b3 |
4582 | newline = snewn(term->cols, termchar); |
4583 | |
887035a5 |
4584 | rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0); |
4eeb7d09 |
4585 | |
156686ef |
4586 | /* Depends on: |
4587 | * screen array, disptop, scrtop, |
4588 | * selection, rv, |
4589 | * cfg.blinkpc, blink_is_real, tblinker, |
39934deb |
4590 | * curs.y, curs.x, cblinker, cfg.blink_cur, cursor_on, has_focus, wrapnext |
156686ef |
4591 | */ |
4eeb7d09 |
4592 | |
4593 | /* Has the cursor position or type changed ? */ |
887035a5 |
4594 | if (term->cursor_on) { |
4595 | if (term->has_focus) { |
39934deb |
4596 | if (term->cblinker || !term->cfg.blink_cur) |
4eeb7d09 |
4597 | cursor = TATTR_ACTCURS; |
32874aea |
4598 | else |
4599 | cursor = 0; |
4600 | } else |
4eeb7d09 |
4601 | cursor = TATTR_PASCURS; |
887035a5 |
4602 | if (term->wrapnext) |
4eeb7d09 |
4603 | cursor |= TATTR_RIGHTCURS; |
32874aea |
4604 | } else |
156686ef |
4605 | cursor = 0; |
887035a5 |
4606 | our_curs_y = term->curs.y - term->disptop; |
3c7366f8 |
4607 | { |
4608 | /* |
3c5a620c |
4609 | * Adjust the cursor position: |
4610 | * - for bidi |
4611 | * - in the case where it's resting on the right-hand half |
4612 | * of a CJK wide character. xterm's behaviour here, |
4613 | * which seems adequate to me, is to display the cursor |
4614 | * covering the _whole_ character, exactly as if it were |
4615 | * one space to the left. |
3c7366f8 |
4616 | */ |
36566009 |
4617 | termline *ldata = lineptr(term->curs.y); |
3c5a620c |
4618 | termchar *lchars; |
4619 | |
3c7366f8 |
4620 | our_curs_x = term->curs.x; |
3c5a620c |
4621 | |
4622 | if ( (lchars = term_bidi_line(term, ldata, our_curs_y)) != NULL) { |
4623 | our_curs_x = term->post_bidi_cache[our_curs_y].forward[our_curs_x]; |
4624 | } else |
4625 | lchars = ldata->chars; |
4626 | |
3c7366f8 |
4627 | if (our_curs_x > 0 && |
3c5a620c |
4628 | lchars[our_curs_x].chr == UCSWIDE) |
3c7366f8 |
4629 | our_curs_x--; |
3c5a620c |
4630 | |
36566009 |
4631 | unlineptr(ldata); |
3c7366f8 |
4632 | } |
887035a5 |
4633 | |
36566009 |
4634 | /* |
4635 | * If the cursor is not where it was last time we painted, and |
4636 | * its previous position is visible on screen, invalidate its |
4637 | * previous position. |
4638 | */ |
4639 | if (term->dispcursy >= 0 && |
4640 | (term->curstype != cursor || |
4641 | term->dispcursy != our_curs_y || |
4642 | term->dispcursx != our_curs_x)) { |
4643 | termchar *dispcurs = term->disptext[term->dispcursy]->chars + |
4644 | term->dispcursx; |
4645 | |
4646 | if (term->dispcursx > 0 && dispcurs->chr == UCSWIDE) |
4647 | dispcurs[-1].attr |= ATTR_INVALID; |
4648 | if (term->dispcursx < term->cols-1 && dispcurs[1].chr == UCSWIDE) |
4649 | dispcurs[1].attr |= ATTR_INVALID; |
4650 | dispcurs->attr |= ATTR_INVALID; |
4651 | |
887035a5 |
4652 | term->curstype = 0; |
4eeb7d09 |
4653 | } |
36566009 |
4654 | term->dispcursx = term->dispcursy = -1; |
4eeb7d09 |
4655 | |
341eb978 |
4656 | #ifdef OPTIMISE_SCROLL |
4657 | /* Do scrolls */ |
4658 | sr = term->scrollhead; |
4659 | while (sr) { |
4660 | struct scrollregion *next = sr->next; |
4661 | do_scroll(ctx, sr->topline, sr->botline, sr->lines); |
4662 | sfree(sr); |
4663 | sr = next; |
4664 | } |
4665 | term->scrollhead = term->scrolltail = NULL; |
4666 | #endif /* OPTIMISE_SCROLL */ |
4667 | |
4eeb7d09 |
4668 | /* The normal screen data */ |
887035a5 |
4669 | for (i = 0; i < term->rows; i++) { |
36566009 |
4670 | termline *ldata; |
4671 | termchar *lchars; |
4672 | int dirty_line, dirty_run, selected; |
4673 | unsigned long attr = 0, cset = 0; |
4eeb7d09 |
4674 | int updated_line = 0; |
4675 | int start = 0; |
4676 | int ccount = 0; |
4677 | int last_run_dirty = 0; |
fd59420d |
4678 | int laststart, dirtyrect; |
3c5a620c |
4679 | int *backward; |
4eeb7d09 |
4680 | |
887035a5 |
4681 | scrpos.y = i + term->disptop; |
4facdf84 |
4682 | ldata = lineptr(scrpos.y); |
4eeb7d09 |
4683 | |
f0fccd51 |
4684 | /* Do Arabic shaping and bidi. */ |
3c5a620c |
4685 | lchars = term_bidi_line(term, ldata, i); |
4686 | if (lchars) { |
4687 | backward = term->post_bidi_cache[i].backward; |
4688 | } else { |
36566009 |
4689 | lchars = ldata->chars; |
3c5a620c |
4690 | backward = NULL; |
4691 | } |
f0fccd51 |
4692 | |
efc411b3 |
4693 | /* |
4694 | * First loop: work along the line deciding what we want |
4695 | * each character cell to look like. |
4696 | */ |
36566009 |
4697 | for (j = 0; j < term->cols; j++) { |
4eeb7d09 |
4698 | unsigned long tattr, tchar; |
36566009 |
4699 | termchar *d = lchars + j; |
3c5a620c |
4700 | scrpos.x = backward ? backward[j] : j; |
32874aea |
4701 | |
36566009 |
4702 | tchar = d->chr; |
4703 | tattr = d->attr; |
4704 | |
c6f1b8ed |
4705 | if (!term->cfg.ansi_colour) |
4706 | tattr = (tattr & ~(ATTR_FGMASK | ATTR_BGMASK)) | |
4707 | ATTR_DEFFG | ATTR_DEFBG; |
4708 | |
cecb13f6 |
4709 | if (!term->cfg.xterm_256_colour) { |
4710 | int colour; |
4711 | colour = (tattr & ATTR_FGMASK) >> ATTR_FGSHIFT; |
4712 | if (colour >= 16 && colour < 256) |
4713 | tattr = (tattr &~ ATTR_FGMASK) | ATTR_DEFFG; |
4714 | colour = (tattr & ATTR_BGMASK) >> ATTR_BGSHIFT; |
4715 | if (colour >= 16 && colour < 256) |
4716 | tattr = (tattr &~ ATTR_BGMASK) | ATTR_DEFBG; |
4717 | } |
4718 | |
4eeb7d09 |
4719 | switch (tchar & CSET_MASK) { |
36566009 |
4720 | case CSET_ASCII: |
21d2b241 |
4721 | tchar = term->ucsdata->unitab_line[tchar & 0xFF]; |
4eeb7d09 |
4722 | break; |
36566009 |
4723 | case CSET_LINEDRW: |
21d2b241 |
4724 | tchar = term->ucsdata->unitab_xterm[tchar & 0xFF]; |
4eeb7d09 |
4725 | break; |
36566009 |
4726 | case CSET_SCOACS: |
21d2b241 |
4727 | tchar = term->ucsdata->unitab_scoacs[tchar&0xFF]; |
d3cb5465 |
4728 | break; |
4eeb7d09 |
4729 | } |
36566009 |
4730 | if (j < term->cols-1 && d[1].chr == UCSWIDE) |
552b7a5c |
4731 | tattr |= ATTR_WIDE; |
4eeb7d09 |
4732 | |
4733 | /* Video reversing things */ |
f278d6f8 |
4734 | if (term->selstate == DRAGGING || term->selstate == SELECTED) { |
4735 | if (term->seltype == LEXICOGRAPHIC) |
4736 | selected = (posle(term->selstart, scrpos) && |
4737 | poslt(scrpos, term->selend)); |
4738 | else |
4739 | selected = (posPle(term->selstart, scrpos) && |
4740 | posPlt(scrpos, term->selend)); |
4741 | } else |
4742 | selected = FALSE; |
4eeb7d09 |
4743 | tattr = (tattr ^ rv |
6908fed7 |
4744 | ^ (selected ? ATTR_REVERSE : 0)); |
4eeb7d09 |
4745 | |
4746 | /* 'Real' blinking ? */ |
887035a5 |
4747 | if (term->blink_is_real && (tattr & ATTR_BLINK)) { |
4748 | if (term->has_focus && term->tblinker) { |
fc6f0fc2 |
4749 | tchar = term->ucsdata->unitab_line[(unsigned char)' ']; |
c9def1b8 |
4750 | } |
4eeb7d09 |
4751 | tattr &= ~ATTR_BLINK; |
c9def1b8 |
4752 | } |
374330e2 |
4753 | |
5a73255e |
4754 | /* |
4755 | * Check the font we'll _probably_ be using to see if |
4756 | * the character is wide when we don't want it to be. |
4757 | */ |
36566009 |
4758 | if (tchar != term->disptext[i]->chars[j].chr || |
4759 | tattr != (term->disptext[i]->chars[j].attr &~ |
fd59420d |
4760 | (ATTR_NARROW | DATTR_MASK))) { |
36566009 |
4761 | if ((tattr & ATTR_WIDE) == 0 && char_width(ctx, tchar) == 2) |
5a73255e |
4762 | tattr |= ATTR_NARROW; |
36566009 |
4763 | } else if (term->disptext[i]->chars[j].attr & ATTR_NARROW) |
5a73255e |
4764 | tattr |= ATTR_NARROW; |
4765 | |
3c7366f8 |
4766 | if (i == our_curs_y && j == our_curs_x) { |
fd59420d |
4767 | tattr |= cursor; |
4768 | term->curstype = cursor; |
36566009 |
4769 | term->dispcursx = j; |
4770 | term->dispcursy = i; |
4eeb7d09 |
4771 | } |
374330e2 |
4772 | |
efc411b3 |
4773 | /* FULL-TERMCHAR */ |
4774 | newline[j].attr = tattr; |
4775 | newline[j].chr = tchar; |
4776 | /* Combining characters are still read from lchars */ |
4777 | newline[j].cc_next = 0; |
4778 | } |
4779 | |
4780 | /* |
4781 | * Now loop over the line again, noting where things have |
4782 | * changed. |
fd59420d |
4783 | * |
4784 | * During this loop, we keep track of where we last saw |
4785 | * DATTR_STARTRUN. Any mismatch automatically invalidates |
4786 | * _all_ of the containing run that was last printed: that |
4787 | * is, any rectangle that was drawn in one go in the |
4788 | * previous update should be either left completely alone |
4789 | * or overwritten in its entirety. This, along with the |
4790 | * expectation that front ends clip all text runs to their |
4791 | * bounding rectangle, should solve any possible problems |
4792 | * with fonts that overflow their character cells. |
efc411b3 |
4793 | */ |
fd59420d |
4794 | laststart = 0; |
4795 | dirtyrect = FALSE; |
efc411b3 |
4796 | for (j = 0; j < term->cols; j++) { |
fd59420d |
4797 | if (term->disptext[i]->chars[j].attr & DATTR_STARTRUN) { |
4798 | laststart = j; |
4799 | dirtyrect = FALSE; |
4800 | } |
4801 | |
efc411b3 |
4802 | if (term->disptext[i]->chars[j].chr != newline[j].chr || |
fd59420d |
4803 | (term->disptext[i]->chars[j].attr &~ DATTR_MASK) |
4804 | != newline[j].attr) { |
4805 | int k; |
4806 | |
4807 | for (k = laststart; k < j; k++) |
4808 | term->disptext[i]->chars[k].attr |= ATTR_INVALID; |
4809 | |
4810 | dirtyrect = TRUE; |
efc411b3 |
4811 | } |
fd59420d |
4812 | |
4813 | if (dirtyrect) |
4814 | term->disptext[i]->chars[j].attr |= ATTR_INVALID; |
efc411b3 |
4815 | } |
4816 | |
4817 | /* |
4818 | * Finally, loop once more and actually do the drawing. |
4819 | */ |
4820 | dirty_run = dirty_line = (ldata->lattr != |
4821 | term->disptext[i]->lattr); |
4822 | term->disptext[i]->lattr = ldata->lattr; |
4823 | |
4824 | for (j = 0; j < term->cols; j++) { |
4825 | unsigned long tattr, tchar; |
4826 | int break_run, do_copy; |
4827 | termchar *d = lchars + j; |
4828 | |
4829 | tattr = newline[j].attr; |
4830 | tchar = newline[j].chr; |
4831 | |
36566009 |
4832 | if ((term->disptext[i]->chars[j].attr ^ tattr) & ATTR_WIDE) |
4eeb7d09 |
4833 | dirty_line = TRUE; |
4834 | |
c6958dfe |
4835 | break_run = ((tattr ^ attr) & term->attr_mask) != 0; |
4eeb7d09 |
4836 | |
4837 | /* Special hack for VT100 Linedraw glyphs */ |
36566009 |
4838 | if (tchar >= 0x23BA && tchar <= 0x23BD) |
4839 | break_run = TRUE; |
4840 | |
4841 | /* |
4842 | * Separate out sequences of characters that have the |
4843 | * same CSET, if that CSET is a magic one. |
4844 | */ |
4845 | if (CSET_OF(tchar) != cset) |
4846 | break_run = TRUE; |
4eeb7d09 |
4847 | |
c6958dfe |
4848 | /* |
4849 | * Break on both sides of any combined-character cell. |
4850 | */ |
4851 | if (d->cc_next != 0 || |
4852 | (j > 0 && d[-1].cc_next != 0)) |
4853 | break_run = TRUE; |
4854 | |
21d2b241 |
4855 | if (!term->ucsdata->dbcs_screenfont && !dirty_line) { |
36566009 |
4856 | if (term->disptext[i]->chars[j].chr == tchar && |
fd59420d |
4857 | (term->disptext[i]->chars[j].attr &~ DATTR_MASK) == tattr) |
4eeb7d09 |
4858 | break_run = TRUE; |
4859 | else if (!dirty_run && ccount == 1) |
4860 | break_run = TRUE; |
374330e2 |
4861 | } |
4eeb7d09 |
4862 | |
4863 | if (break_run) { |
4864 | if ((dirty_run || last_run_dirty) && ccount > 0) { |
cb1e17e9 |
4865 | do_text(ctx, start, i, ch, ccount, attr, |
4866 | ldata->lattr); |
fd59420d |
4867 | if (attr & (TATTR_ACTCURS | TATTR_PASCURS)) |
cb1e17e9 |
4868 | do_cursor(ctx, start, i, ch, ccount, attr, |
fd59420d |
4869 | ldata->lattr); |
fd59420d |
4870 | |
4eeb7d09 |
4871 | updated_line = 1; |
4872 | } |
4873 | start = j; |
4874 | ccount = 0; |
4875 | attr = tattr; |
36566009 |
4876 | cset = CSET_OF(tchar); |
21d2b241 |
4877 | if (term->ucsdata->dbcs_screenfont) |
4eeb7d09 |
4878 | last_run_dirty = dirty_run; |
4879 | dirty_run = dirty_line; |
4880 | } |
4881 | |
c6958dfe |
4882 | do_copy = FALSE; |
4883 | if (!termchars_equal_override(&term->disptext[i]->chars[j], |
4884 | d, tchar, tattr)) { |
4885 | do_copy = TRUE; |
4eeb7d09 |
4886 | dirty_run = TRUE; |
c6958dfe |
4887 | } |
4888 | |
4889 | if (ccount >= chlen) { |
4890 | chlen = ccount + 256; |
4891 | ch = sresize(ch, chlen, wchar_t); |
4892 | } |
36566009 |
4893 | ch[ccount++] = (wchar_t) tchar; |
c6958dfe |
4894 | |
4895 | if (d->cc_next) { |
4896 | termchar *dd = d; |
4897 | |
4898 | while (dd->cc_next) { |
4899 | unsigned long schar; |
4900 | |
4901 | dd += dd->cc_next; |
4902 | |
4903 | schar = dd->chr; |
4904 | switch (schar & CSET_MASK) { |
4905 | case CSET_ASCII: |
4906 | schar = term->ucsdata->unitab_line[schar & 0xFF]; |
4907 | break; |
4908 | case CSET_LINEDRW: |
4909 | schar = term->ucsdata->unitab_xterm[schar & 0xFF]; |
4910 | break; |
4911 | case CSET_SCOACS: |
4912 | schar = term->ucsdata->unitab_scoacs[schar&0xFF]; |
4913 | break; |
4914 | } |
4915 | |
4916 | if (ccount >= chlen) { |
4917 | chlen = ccount + 256; |
4918 | ch = sresize(ch, chlen, wchar_t); |
4919 | } |
4920 | ch[ccount++] = (wchar_t) schar; |
4921 | } |
4922 | |
4923 | attr |= TATTR_COMBINING; |
4924 | } |
4925 | |
4926 | if (do_copy) { |
4927 | copy_termchar(term->disptext[i], j, d); |
4928 | term->disptext[i]->chars[j].chr = tchar; |
4929 | term->disptext[i]->chars[j].attr = tattr; |
fd59420d |
4930 | if (start == j) |
4931 | term->disptext[i]->chars[j].attr |= DATTR_STARTRUN; |
c6958dfe |
4932 | } |
4eeb7d09 |
4933 | |
4934 | /* If it's a wide char step along to the next one. */ |
4935 | if (tattr & ATTR_WIDE) { |
887035a5 |
4936 | if (++j < term->cols) { |
4eeb7d09 |
4937 | d++; |
3c7366f8 |
4938 | /* |
4939 | * By construction above, the cursor should not |
4940 | * be on the right-hand half of this character. |
4941 | * Ever. |
4942 | */ |
4943 | assert(!(i == our_curs_y && j == our_curs_x)); |
c6958dfe |
4944 | if (!termchars_equal(&term->disptext[i]->chars[j], d)) |
4eeb7d09 |
4945 | dirty_run = TRUE; |
c6958dfe |
4946 | copy_termchar(term->disptext[i], j, d); |
374330e2 |
4947 | } |
374330e2 |
4948 | } |
4eeb7d09 |
4949 | } |
4950 | if (dirty_run && ccount > 0) { |
cb1e17e9 |
4951 | do_text(ctx, start, i, ch, ccount, attr, |
4952 | ldata->lattr); |
fd59420d |
4953 | if (attr & (TATTR_ACTCURS | TATTR_PASCURS)) |
cb1e17e9 |
4954 | do_cursor(ctx, start, i, ch, ccount, attr, |
fd59420d |
4955 | ldata->lattr); |
c6958dfe |
4956 | |
fd59420d |
4957 | updated_line = 1; |
374330e2 |
4958 | } |
36566009 |
4959 | |
4960 | unlineptr(ldata); |
374330e2 |
4961 | } |
c6958dfe |
4962 | |
efc411b3 |
4963 | sfree(newline); |
c6958dfe |
4964 | sfree(ch); |
374330e2 |
4965 | } |
4966 | |
4967 | /* |
4968 | * Invalidate the whole screen so it will be repainted in full. |
4969 | */ |
887035a5 |
4970 | void term_invalidate(Terminal *term) |
32874aea |
4971 | { |
36566009 |
4972 | int i, j; |
374330e2 |
4973 | |
36566009 |
4974 | for (i = 0; i < term->rows; i++) |
4975 | for (j = 0; j < term->cols; j++) |
efc411b3 |
4976 | term->disptext[i]->chars[j].attr |= ATTR_INVALID; |
cecb13f6 |
4977 | |
4978 | term_schedule_update(term); |
374330e2 |
4979 | } |
4980 | |
4981 | /* |
4982 | * Paint the window in response to a WM_PAINT message. |
4983 | */ |
887035a5 |
4984 | void term_paint(Terminal *term, Context ctx, |
c1b55581 |
4985 | int left, int top, int right, int bottom, int immediately) |
32874aea |
4986 | { |
5a73255e |
4987 | int i, j; |
4988 | if (left < 0) left = 0; |
4989 | if (top < 0) top = 0; |
887035a5 |
4990 | if (right >= term->cols) right = term->cols-1; |
4991 | if (bottom >= term->rows) bottom = term->rows-1; |
4992 | |
4993 | for (i = top; i <= bottom && i < term->rows; i++) { |
a0f45c98 |
4994 | if ((term->disptext[i]->lattr & LATTR_MODE) == LATTR_NORM) |
887035a5 |
4995 | for (j = left; j <= right && j < term->cols; j++) |
efc411b3 |
4996 | term->disptext[i]->chars[j].attr |= ATTR_INVALID; |
c9def1b8 |
4997 | else |
887035a5 |
4998 | for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++) |
efc411b3 |
4999 | term->disptext[i]->chars[j].attr |= ATTR_INVALID; |
c9def1b8 |
5000 | } |
374330e2 |
5001 | |
39934deb |
5002 | if (immediately) { |
887035a5 |
5003 | do_paint (term, ctx, FALSE); |
39934deb |
5004 | } else { |
cecb13f6 |
5005 | term_schedule_update(term); |
39934deb |
5006 | } |
374330e2 |
5007 | } |
5008 | |
5009 | /* |
5010 | * Attempt to scroll the scrollback. The second parameter gives the |
5011 | * position we want to scroll to; the first is +1 to denote that |
5012 | * this position is relative to the beginning of the scrollback, -1 |
5013 | * to denote it is relative to the end, and 0 to denote that it is |
5014 | * relative to the current position. |
5015 | */ |
887035a5 |
5016 | void term_scroll(Terminal *term, int rel, int where) |
32874aea |
5017 | { |
876e5d5e |
5018 | int sbtop = -sblines(term); |
37d2a505 |
5019 | #ifdef OPTIMISE_SCROLL |
5020 | int olddisptop = term->disptop; |
5021 | int shift; |
5022 | #endif /* OPTIMISE_SCROLL */ |
887035a5 |
5023 | |
5024 | term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where; |
5025 | if (term->disptop < sbtop) |
5026 | term->disptop = sbtop; |
5027 | if (term->disptop > 0) |
5028 | term->disptop = 0; |
5029 | update_sbar(term); |
37d2a505 |
5030 | #ifdef OPTIMISE_SCROLL |
5031 | shift = (term->disptop - olddisptop); |
5032 | if (shift < term->rows && shift > -term->rows) |
5033 | scroll_display(term, 0, term->rows - 1, shift); |
5034 | #endif /* OPTIMISE_SCROLL */ |
887035a5 |
5035 | term_update(term); |
374330e2 |
5036 | } |
5037 | |
c076966d |
5038 | static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel) |
32874aea |
5039 | { |
4eeb7d09 |
5040 | wchar_t *workbuf; |
5041 | wchar_t *wbptr; /* where next char goes within workbuf */ |
6908fed7 |
5042 | int old_top_x; |
32874aea |
5043 | int wblen = 0; /* workbuf len */ |
5044 | int buflen; /* amount of memory allocated to workbuf */ |
bc1235d4 |
5045 | |
4eeb7d09 |
5046 | buflen = 5120; /* Default size */ |
3d88e64d |
5047 | workbuf = snewn(buflen, wchar_t); |
4eeb7d09 |
5048 | wbptr = workbuf; /* start filling here */ |
6908fed7 |
5049 | old_top_x = top.x; /* needed for rect==1 */ |
bc1235d4 |
5050 | |
4facdf84 |
5051 | while (poslt(top, bottom)) { |
bc1235d4 |
5052 | int nl = FALSE; |
36566009 |
5053 | termline *ldata = lineptr(top.y); |
260f3dec |
5054 | pos nlpos; |
4facdf84 |
5055 | |
6908fed7 |
5056 | /* |
5057 | * nlpos will point at the maximum position on this line we |
5058 | * should copy up to. So we start it at the end of the |
5059 | * line... |
5060 | */ |
4facdf84 |
5061 | nlpos.y = top.y; |
887035a5 |
5062 | nlpos.x = term->cols; |
bc1235d4 |
5063 | |
6908fed7 |
5064 | /* |
5065 | * ... move it backwards if there's unused space at the end |
5066 | * of the line (and also set `nl' if this is the case, |
5067 | * because in normal selection mode this means we need a |
5068 | * newline at the end)... |
5069 | */ |
36566009 |
5070 | if (!(ldata->lattr & LATTR_WRAPPED)) { |
5071 | while (IS_SPACE_CHR(ldata->chars[nlpos.x - 1].chr) && |
d102f3cf |
5072 | !ldata->chars[nlpos.x - 1].cc_next && |
36566009 |
5073 | poslt(top, nlpos)) |
4eeb7d09 |
5074 | decpos(nlpos); |
4facdf84 |
5075 | if (poslt(nlpos, bottom)) |
bc1235d4 |
5076 | nl = TRUE; |
36566009 |
5077 | } else if (ldata->lattr & LATTR_WRAPPED2) { |
3c7366f8 |
5078 | /* Ignore the last char on the line in a WRAPPED2 line. */ |
5079 | decpos(nlpos); |
bc1235d4 |
5080 | } |
6908fed7 |
5081 | |
5082 | /* |
5083 | * ... and then clip it to the terminal x coordinate if |
5084 | * we're doing rectangular selection. (In this case we |
5085 | * still did the above, so that copying e.g. the right-hand |
5086 | * column from a table doesn't fill with spaces on the |
5087 | * right.) |
5088 | */ |
5089 | if (rect) { |
5090 | if (nlpos.x > bottom.x) |
5091 | nlpos.x = bottom.x; |
5092 | nl = (top.y < bottom.y); |
5093 | } |
5094 | |
4facdf84 |
5095 | while (poslt(top, bottom) && poslt(top, nlpos)) { |
4eeb7d09 |
5096 | #if 0 |
5097 | char cbuf[16], *p; |
5098 | sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF)); |
5099 | #else |
5100 | wchar_t cbuf[16], *p; |
4eeb7d09 |
5101 | int set, c; |
c6958dfe |
5102 | int x = top.x; |
4eeb7d09 |
5103 | |
c6958dfe |
5104 | if (ldata->chars[x].chr == UCSWIDE) { |
4eeb7d09 |
5105 | top.x++; |
5106 | continue; |
5107 | } |
5108 | |
c6958dfe |
5109 | while (1) { |
5110 | int uc = ldata->chars[x].chr; |
5111 | |
5112 | switch (uc & CSET_MASK) { |
5113 | case CSET_LINEDRW: |
5114 | if (!term->cfg.rawcnp) { |
5115 | uc = term->ucsdata->unitab_xterm[uc & 0xFF]; |
5116 | break; |
5117 | } |
5118 | case CSET_ASCII: |
5119 | uc = term->ucsdata->unitab_line[uc & 0xFF]; |
5120 | break; |
5121 | case CSET_SCOACS: |
5122 | uc = term->ucsdata->unitab_scoacs[uc&0xFF]; |
5123 | break; |
5124 | } |
5125 | switch (uc & CSET_MASK) { |
5126 | case CSET_ACP: |
5127 | uc = term->ucsdata->unitab_font[uc & 0xFF]; |
5128 | break; |
5129 | case CSET_OEMCP: |
5130 | uc = term->ucsdata->unitab_oemcp[uc & 0xFF]; |
4eeb7d09 |
5131 | break; |
32874aea |
5132 | } |
4eeb7d09 |
5133 | |
c6958dfe |
5134 | set = (uc & CSET_MASK); |
5135 | c = (uc & ~CSET_MASK); |
5136 | cbuf[0] = uc; |
5137 | cbuf[1] = 0; |
5138 | |
5139 | if (DIRECT_FONT(uc)) { |
5140 | if (c >= ' ' && c != 0x7F) { |
5141 | char buf[4]; |
5142 | WCHAR wbuf[4]; |
5143 | int rv; |
5144 | if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) { |
5145 | buf[0] = c; |
5146 | buf[1] = (char) (0xFF & ldata->chars[top.x + 1].chr); |
5147 | rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4); |
5148 | top.x++; |
5149 | } else { |
5150 | buf[0] = c; |
5151 | rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4); |
5152 | } |
bc1235d4 |
5153 | |
c6958dfe |
5154 | if (rv > 0) { |
5155 | memcpy(cbuf, wbuf, rv * sizeof(wchar_t)); |
5156 | cbuf[rv] = 0; |
5157 | } |
d3a22f79 |
5158 | } |
d3a22f79 |
5159 | } |
4eeb7d09 |
5160 | #endif |
5161 | |
c6958dfe |
5162 | for (p = cbuf; *p; p++) { |
5163 | /* Enough overhead for trailing NL and nul */ |
5164 | if (wblen >= buflen - 16) { |
5165 | buflen += 100; |
5166 | workbuf = sresize(workbuf, buflen, wchar_t); |
5167 | wbptr = workbuf + wblen; |
5168 | } |
5169 | wblen++; |
5170 | *wbptr++ = *p; |
4eeb7d09 |
5171 | } |
c6958dfe |
5172 | |
5173 | if (ldata->chars[x].cc_next) |
5174 | x += ldata->chars[x].cc_next; |
5175 | else |
5176 | break; |
bc1235d4 |
5177 | } |
4facdf84 |
5178 | top.x++; |
bc1235d4 |
5179 | } |
5180 | if (nl) { |
5181 | int i; |
4eeb7d09 |
5182 | for (i = 0; i < sel_nl_sz; i++) { |
32874aea |
5183 | wblen++; |
bc1235d4 |
5184 | *wbptr++ = sel_nl[i]; |
5185 | } |
5186 | } |
4facdf84 |
5187 | top.y++; |
6908fed7 |
5188 | top.x = rect ? old_top_x : 0; |
36566009 |
5189 | |
5190 | unlineptr(ldata); |
bc1235d4 |
5191 | } |
e6346999 |
5192 | #if SELECTION_NUL_TERMINATED |
4eeb7d09 |
5193 | wblen++; |
5194 | *wbptr++ = 0; |
e6346999 |
5195 | #endif |
c076966d |
5196 | write_clip(term->frontend, workbuf, wblen, desel); /* transfer to clipbd */ |
32874aea |
5197 | if (buflen > 0) /* indicates we allocated this buffer */ |
bc1235d4 |
5198 | sfree(workbuf); |
bc1235d4 |
5199 | } |
4eeb7d09 |
5200 | |
887035a5 |
5201 | void term_copyall(Terminal *term) |
32874aea |
5202 | { |
4facdf84 |
5203 | pos top; |
40132dba |
5204 | pos bottom; |
5205 | tree234 *screen = term->screen; |
876e5d5e |
5206 | top.y = -sblines(term); |
4facdf84 |
5207 | top.x = 0; |
40132dba |
5208 | bottom.y = find_last_nonempty_line(term, screen); |
5209 | bottom.x = term->cols; |
5210 | clipme(term, top, bottom, 0, TRUE); |
4eeb7d09 |
5211 | } |
5212 | |
5213 | /* |
887035a5 |
5214 | * The wordness array is mainly for deciding the disposition of the |
5215 | * US-ASCII characters. |
4eeb7d09 |
5216 | */ |
887035a5 |
5217 | static int wordtype(Terminal *term, int uc) |
4eeb7d09 |
5218 | { |
b9d7bcad |
5219 | struct ucsword { |
4eeb7d09 |
5220 | int start, end, ctype; |
b9d7bcad |
5221 | }; |
5222 | static const struct ucsword ucs_words[] = { |
4eeb7d09 |
5223 | { |
5224 | 128, 160, 0}, { |
5225 | 161, 191, 1}, { |
5226 | 215, 215, 1}, { |
5227 | 247, 247, 1}, { |
5228 | 0x037e, 0x037e, 1}, /* Greek question mark */ |
5229 | { |
5230 | 0x0387, 0x0387, 1}, /* Greek ano teleia */ |
5231 | { |
5232 | 0x055a, 0x055f, 1}, /* Armenian punctuation */ |
5233 | { |
5234 | 0x0589, 0x0589, 1}, /* Armenian full stop */ |
5235 | { |
5236 | 0x0700, 0x070d, 1}, /* Syriac punctuation */ |
5237 | { |
5238 | 0x104a, 0x104f, 1}, /* Myanmar punctuation */ |
5239 | { |
5240 | 0x10fb, 0x10fb, 1}, /* Georgian punctuation */ |
5241 | { |
5242 | 0x1361, 0x1368, 1}, /* Ethiopic punctuation */ |
5243 | { |
5244 | 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */ |
5245 | { |
5246 | 0x17d4, 0x17dc, 1}, /* Khmer punctuation */ |
5247 | { |
5248 | 0x1800, 0x180a, 1}, /* Mongolian punctuation */ |
5249 | { |
5250 | 0x2000, 0x200a, 0}, /* Various spaces */ |
5251 | { |
5252 | 0x2070, 0x207f, 2}, /* superscript */ |
5253 | { |
5254 | 0x2080, 0x208f, 2}, /* subscript */ |
5255 | { |
5256 | 0x200b, 0x27ff, 1}, /* punctuation and symbols */ |
5257 | { |
5258 | 0x3000, 0x3000, 0}, /* ideographic space */ |
5259 | { |
5260 | 0x3001, 0x3020, 1}, /* ideographic punctuation */ |
5261 | { |
5262 | 0x303f, 0x309f, 3}, /* Hiragana */ |
5263 | { |
5264 | 0x30a0, 0x30ff, 3}, /* Katakana */ |
5265 | { |
5266 | 0x3300, 0x9fff, 3}, /* CJK Ideographs */ |
5267 | { |
5268 | 0xac00, 0xd7a3, 3}, /* Hangul Syllables */ |
5269 | { |
5270 | 0xf900, 0xfaff, 3}, /* CJK Ideographs */ |
5271 | { |
5272 | 0xfe30, 0xfe6b, 1}, /* punctuation forms */ |
5273 | { |
5274 | 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */ |
5275 | { |
5276 | 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */ |
5277 | { |
5278 | 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */ |
5279 | { |
5280 | 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */ |
5281 | { |
5282 | 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */ |
5283 | { |
5284 | 0, 0, 0} |
5285 | }; |
b9d7bcad |
5286 | const struct ucsword *wptr; |
4eeb7d09 |
5287 | |
4eeb7d09 |
5288 | switch (uc & CSET_MASK) { |
36566009 |
5289 | case CSET_LINEDRW: |
21d2b241 |
5290 | uc = term->ucsdata->unitab_xterm[uc & 0xFF]; |
4eeb7d09 |
5291 | break; |
36566009 |
5292 | case CSET_ASCII: |
21d2b241 |
5293 | uc = term->ucsdata->unitab_line[uc & 0xFF]; |
4eeb7d09 |
5294 | break; |
36566009 |
5295 | case CSET_SCOACS: |
21d2b241 |
5296 | uc = term->ucsdata->unitab_scoacs[uc&0xFF]; |
d3cb5465 |
5297 | break; |
4eeb7d09 |
5298 | } |
5299 | switch (uc & CSET_MASK) { |
36566009 |
5300 | case CSET_ACP: |
21d2b241 |
5301 | uc = term->ucsdata->unitab_font[uc & 0xFF]; |
4eeb7d09 |
5302 | break; |
36566009 |
5303 | case CSET_OEMCP: |
21d2b241 |
5304 | uc = term->ucsdata->unitab_oemcp[uc & 0xFF]; |
4eeb7d09 |
5305 | break; |
5306 | } |
5307 | |
36566009 |
5308 | /* For DBCS fonts I can't do anything useful. Even this will sometimes |
5a73255e |
5309 | * fail as there's such a thing as a double width space. :-( |
5310 | */ |
21d2b241 |
5311 | if (term->ucsdata->dbcs_screenfont && |
5312 | term->ucsdata->font_codepage == term->ucsdata->line_codepage) |
5a73255e |
5313 | return (uc != ' '); |
5314 | |
4eeb7d09 |
5315 | if (uc < 0x80) |
887035a5 |
5316 | return term->wordness[uc]; |
4eeb7d09 |
5317 | |
5318 | for (wptr = ucs_words; wptr->start; wptr++) { |
5319 | if (uc >= wptr->start && uc <= wptr->end) |
5320 | return wptr->ctype; |
5321 | } |
5322 | |
5323 | return 2; |
bc1235d4 |
5324 | } |
5325 | |
374330e2 |
5326 | /* |
5327 | * Spread the selection outwards according to the selection mode. |
5328 | */ |
887035a5 |
5329 | static pos sel_spread_half(Terminal *term, pos p, int dir) |
32874aea |
5330 | { |
36566009 |
5331 | termline *ldata; |
374330e2 |
5332 | short wvalue; |
876e5d5e |
5333 | int topy = -sblines(term); |
374330e2 |
5334 | |
4facdf84 |
5335 | ldata = lineptr(p.y); |
374330e2 |
5336 | |
887035a5 |
5337 | switch (term->selmode) { |
374330e2 |
5338 | case SM_CHAR: |
5339 | /* |
5340 | * In this mode, every character is a separate unit, except |
5341 | * for runs of spaces at the end of a non-wrapping line. |
5342 | */ |
36566009 |
5343 | if (!(ldata->lattr & LATTR_WRAPPED)) { |
5344 | termchar *q = ldata->chars + term->cols; |
d102f3cf |
5345 | while (q > ldata->chars && |
5346 | IS_SPACE_CHR(q[-1].chr) && !q[-1].cc_next) |
374330e2 |
5347 | q--; |
36566009 |
5348 | if (q == ldata->chars + term->cols) |
374330e2 |
5349 | q--; |
36566009 |
5350 | if (p.x >= q - ldata->chars) |
5351 | p.x = (dir == -1 ? q - ldata->chars : term->cols - 1); |
374330e2 |
5352 | } |
5353 | break; |
5354 | case SM_WORD: |
5355 | /* |
5356 | * In this mode, the units are maximal runs of characters |
5357 | * whose `wordness' has the same value. |
5358 | */ |
36566009 |
5359 | wvalue = wordtype(term, UCSGET(ldata->chars, p.x)); |
374330e2 |
5360 | if (dir == +1) { |
50ab4088 |
5361 | while (1) { |
36566009 |
5362 | int maxcols = (ldata->lattr & LATTR_WRAPPED2 ? |
3c7366f8 |
5363 | term->cols-1 : term->cols); |
5364 | if (p.x < maxcols-1) { |
36566009 |
5365 | if (wordtype(term, UCSGET(ldata->chars, p.x+1)) == wvalue) |
50ab4088 |
5366 | p.x++; |
5367 | else |
5368 | break; |
5369 | } else { |
36566009 |
5370 | if (ldata->lattr & LATTR_WRAPPED) { |
5371 | termline *ldata2; |
50ab4088 |
5372 | ldata2 = lineptr(p.y+1); |
36566009 |
5373 | if (wordtype(term, UCSGET(ldata2->chars, 0)) |
5374 | == wvalue) { |
50ab4088 |
5375 | p.x = 0; |
5376 | p.y++; |
36566009 |
5377 | unlineptr(ldata); |
50ab4088 |
5378 | ldata = ldata2; |
36566009 |
5379 | } else { |
5380 | unlineptr(ldata2); |
50ab4088 |
5381 | break; |
36566009 |
5382 | } |
50ab4088 |
5383 | } else |
5384 | break; |
5385 | } |
5386 | } |
374330e2 |
5387 | } else { |
50ab4088 |
5388 | while (1) { |
5389 | if (p.x > 0) { |
36566009 |
5390 | if (wordtype(term, UCSGET(ldata->chars, p.x-1)) == wvalue) |
50ab4088 |
5391 | p.x--; |
5392 | else |
5393 | break; |
5394 | } else { |
36566009 |
5395 | termline *ldata2; |
3c7366f8 |
5396 | int maxcols; |
50ab4088 |
5397 | if (p.y <= topy) |
5398 | break; |
5399 | ldata2 = lineptr(p.y-1); |
36566009 |
5400 | maxcols = (ldata2->lattr & LATTR_WRAPPED2 ? |
3c7366f8 |
5401 | term->cols-1 : term->cols); |
36566009 |
5402 | if (ldata2->lattr & LATTR_WRAPPED) { |
5403 | if (wordtype(term, UCSGET(ldata2->chars, maxcols-1)) |
3c7366f8 |
5404 | == wvalue) { |
5405 | p.x = maxcols-1; |
5406 | p.y--; |
36566009 |
5407 | unlineptr(ldata); |
3c7366f8 |
5408 | ldata = ldata2; |
36566009 |
5409 | } else { |
5410 | unlineptr(ldata2); |
3c7366f8 |
5411 | break; |
36566009 |
5412 | } |
50ab4088 |
5413 | } else |
5414 | break; |
5415 | } |
5416 | } |
374330e2 |
5417 | } |
5418 | break; |
5419 | case SM_LINE: |
5420 | /* |
5421 | * In this mode, every line is a unit. |
5422 | */ |
887035a5 |
5423 | p.x = (dir == -1 ? 0 : term->cols - 1); |
374330e2 |
5424 | break; |
5425 | } |
36566009 |
5426 | |
5427 | unlineptr(ldata); |
374330e2 |
5428 | return p; |
5429 | } |
5430 | |
887035a5 |
5431 | static void sel_spread(Terminal *term) |
32874aea |
5432 | { |
887035a5 |
5433 | if (term->seltype == LEXICOGRAPHIC) { |
5434 | term->selstart = sel_spread_half(term, term->selstart, -1); |
5435 | decpos(term->selend); |
5436 | term->selend = sel_spread_half(term, term->selend, +1); |
5437 | incpos(term->selend); |
6908fed7 |
5438 | } |
374330e2 |
5439 | } |
5440 | |
887035a5 |
5441 | void term_do_paste(Terminal *term) |
568dd02f |
5442 | { |
5443 | wchar_t *data; |
5444 | int len; |
5445 | |
a8327734 |
5446 | get_clip(term->frontend, &data, &len); |
2cb50250 |
5447 | if (data && len > 0) { |
568dd02f |
5448 | wchar_t *p, *q; |
5449 | |
887035a5 |
5450 | term_seen_key_event(term); /* pasted data counts */ |
2cb50250 |
5451 | |
887035a5 |
5452 | if (term->paste_buffer) |
5453 | sfree(term->paste_buffer); |
5454 | term->paste_pos = term->paste_hold = term->paste_len = 0; |
3d88e64d |
5455 | term->paste_buffer = snewn(len, wchar_t); |
568dd02f |
5456 | |
5457 | p = q = data; |
5458 | while (p < data + len) { |
5459 | while (p < data + len && |
5460 | !(p <= data + len - sel_nl_sz && |
5461 | !memcmp(p, sel_nl, sizeof(sel_nl)))) |
5462 | p++; |
5463 | |
5464 | { |
5465 | int i; |
5466 | for (i = 0; i < p - q; i++) { |
887035a5 |
5467 | term->paste_buffer[term->paste_len++] = q[i]; |
568dd02f |
5468 | } |
5469 | } |
5470 | |
5471 | if (p <= data + len - sel_nl_sz && |
5472 | !memcmp(p, sel_nl, sizeof(sel_nl))) { |
92e23ad9 |
5473 | term->paste_buffer[term->paste_len++] = '\015'; |
568dd02f |
5474 | p += sel_nl_sz; |
5475 | } |
5476 | q = p; |
5477 | } |
5478 | |
5479 | /* Assume a small paste will be OK in one go. */ |
887035a5 |
5480 | if (term->paste_len < 256) { |
b9d7bcad |
5481 | if (term->ldisc) |
5482 | luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0); |
887035a5 |
5483 | if (term->paste_buffer) |
5484 | sfree(term->paste_buffer); |
5485 | term->paste_buffer = 0; |
5486 | term->paste_pos = term->paste_hold = term->paste_len = 0; |
568dd02f |
5487 | } |
5488 | } |
a8327734 |
5489 | get_clip(term->frontend, NULL, NULL); |
568dd02f |
5490 | } |
5491 | |
fc5b0934 |
5492 | void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked, |
5493 | Mouse_Action a, int x, int y, int shift, int ctrl, int alt) |
32874aea |
5494 | { |
4facdf84 |
5495 | pos selpoint; |
36566009 |
5496 | termline *ldata; |
887035a5 |
5497 | int raw_mouse = (term->xterm_mouse && |
64734920 |
5498 | !term->cfg.no_mouse_rep && |
5499 | !(term->cfg.mouse_override && shift)); |
6908fed7 |
5500 | int default_seltype; |
32874aea |
5501 | |
fff61f25 |
5502 | if (y < 0) { |
32874aea |
5503 | y = 0; |
fff61f25 |
5504 | if (a == MA_DRAG && !raw_mouse) |
887035a5 |
5505 | term_scroll(term, 0, -1); |
fff61f25 |
5506 | } |
887035a5 |
5507 | if (y >= term->rows) { |
5508 | y = term->rows - 1; |
fff61f25 |
5509 | if (a == MA_DRAG && !raw_mouse) |
887035a5 |
5510 | term_scroll(term, 0, +1); |
fff61f25 |
5511 | } |
32874aea |
5512 | if (x < 0) { |
5513 | if (y > 0) { |
887035a5 |
5514 | x = term->cols - 1; |
32874aea |
5515 | y--; |
5516 | } else |
5517 | x = 0; |
094ed2a6 |
5518 | } |
887035a5 |
5519 | if (x >= term->cols) |
5520 | x = term->cols - 1; |
37508af4 |
5521 | |
887035a5 |
5522 | selpoint.y = y + term->disptop; |
4facdf84 |
5523 | ldata = lineptr(selpoint.y); |
3c5a620c |
5524 | |
36566009 |
5525 | if ((ldata->lattr & LATTR_MODE) != LATTR_NORM) |
3c5a620c |
5526 | x /= 2; |
5527 | |
5528 | /* |
5529 | * Transform x through the bidi algorithm to find the _logical_ |
5530 | * click point from the physical one. |
5531 | */ |
5532 | if (term_bidi_line(term, ldata, y) != NULL) { |
5533 | x = term->post_bidi_cache[y].backward[x]; |
5534 | } |
5535 | |
5536 | selpoint.x = x; |
36566009 |
5537 | unlineptr(ldata); |
374330e2 |
5538 | |
fff61f25 |
5539 | if (raw_mouse) { |
01c034ad |
5540 | int encstate = 0, r, c; |
5541 | char abuf[16]; |
01c034ad |
5542 | |
b9d7bcad |
5543 | if (term->ldisc) { |
5544 | |
fc5b0934 |
5545 | switch (braw) { |
b9d7bcad |
5546 | case MBT_LEFT: |
5547 | encstate = 0x20; /* left button down */ |
5548 | break; |
5549 | case MBT_MIDDLE: |
5550 | encstate = 0x21; |
5551 | break; |
5552 | case MBT_RIGHT: |
5553 | encstate = 0x22; |
5554 | break; |
5555 | case MBT_WHEEL_UP: |
5556 | encstate = 0x60; |
5557 | break; |
5558 | case MBT_WHEEL_DOWN: |
5559 | encstate = 0x61; |
5560 | break; |
5561 | default: break; /* placate gcc warning about enum use */ |
5562 | } |
5563 | switch (a) { |
5564 | case MA_DRAG: |
5565 | if (term->xterm_mouse == 1) |
5566 | return; |
5567 | encstate += 0x20; |
5568 | break; |
5569 | case MA_RELEASE: |
5570 | encstate = 0x23; |
5571 | term->mouse_is_down = 0; |
5572 | break; |
5573 | case MA_CLICK: |
fc5b0934 |
5574 | if (term->mouse_is_down == braw) |
b9d7bcad |
5575 | return; |
fc5b0934 |
5576 | term->mouse_is_down = braw; |
b9d7bcad |
5577 | break; |
5578 | default: break; /* placate gcc warning about enum use */ |
5579 | } |
5580 | if (shift) |
5581 | encstate += 0x04; |
5582 | if (ctrl) |
5583 | encstate += 0x10; |
5584 | r = y + 33; |
5585 | c = x + 33; |
5586 | |
5587 | sprintf(abuf, "\033[M%c%c%c", encstate, c, r); |
5588 | ldisc_send(term->ldisc, abuf, 6, 0); |
01c034ad |
5589 | } |
01c034ad |
5590 | return; |
5591 | } |
5592 | |
6908fed7 |
5593 | /* |
5594 | * Set the selection type (rectangular or normal) at the start |
5595 | * of a selection attempt, from the state of Alt. |
5596 | */ |
64734920 |
5597 | if (!alt ^ !term->cfg.rect_select) |
6908fed7 |
5598 | default_seltype = RECTANGULAR; |
5599 | else |
5600 | default_seltype = LEXICOGRAPHIC; |
5601 | |
887035a5 |
5602 | if (term->selstate == NO_SELECTION) { |
5603 | term->seltype = default_seltype; |
6908fed7 |
5604 | } |
5605 | |
fc5b0934 |
5606 | if (bcooked == MBT_SELECT && a == MA_CLICK) { |
887035a5 |
5607 | deselect(term); |
5608 | term->selstate = ABOUT_TO; |
5609 | term->seltype = default_seltype; |
5610 | term->selanchor = selpoint; |
5611 | term->selmode = SM_CHAR; |
fc5b0934 |
5612 | } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) { |
887035a5 |
5613 | deselect(term); |
5614 | term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE); |
5615 | term->selstate = DRAGGING; |
5616 | term->selstart = term->selanchor = selpoint; |
5617 | term->selend = term->selstart; |
5618 | incpos(term->selend); |
5619 | sel_spread(term); |
fc5b0934 |
5620 | } else if ((bcooked == MBT_SELECT && a == MA_DRAG) || |
5621 | (bcooked == MBT_EXTEND && a != MA_RELEASE)) { |
887035a5 |
5622 | if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint)) |
374330e2 |
5623 | return; |
fc5b0934 |
5624 | if (bcooked == MBT_EXTEND && a != MA_DRAG && |
5625 | term->selstate == SELECTED) { |
887035a5 |
5626 | if (term->seltype == LEXICOGRAPHIC) { |
6908fed7 |
5627 | /* |
5628 | * For normal selection, we extend by moving |
5629 | * whichever end of the current selection is closer |
5630 | * to the mouse. |
5631 | */ |
887035a5 |
5632 | if (posdiff(selpoint, term->selstart) < |
5633 | posdiff(term->selend, term->selstart) / 2) { |
5634 | term->selanchor = term->selend; |
5635 | decpos(term->selanchor); |
6908fed7 |
5636 | } else { |
887035a5 |
5637 | term->selanchor = term->selstart; |
6908fed7 |
5638 | } |
4facdf84 |
5639 | } else { |
6908fed7 |
5640 | /* |
5641 | * For rectangular selection, we have a choice of |
5642 | * _four_ places to put selanchor and selpoint: the |
5643 | * four corners of the selection. |
5644 | */ |
887035a5 |
5645 | if (2*selpoint.x < term->selstart.x + term->selend.x) |
5646 | term->selanchor.x = term->selend.x-1; |
6908fed7 |
5647 | else |
887035a5 |
5648 | term->selanchor.x = term->selstart.x; |
6908fed7 |
5649 | |
887035a5 |
5650 | if (2*selpoint.y < term->selstart.y + term->selend.y) |
5651 | term->selanchor.y = term->selend.y; |
6908fed7 |
5652 | else |
887035a5 |
5653 | term->selanchor.y = term->selstart.y; |
4facdf84 |
5654 | } |
887035a5 |
5655 | term->selstate = DRAGGING; |
374330e2 |
5656 | } |
887035a5 |
5657 | if (term->selstate != ABOUT_TO && term->selstate != DRAGGING) |
5658 | term->selanchor = selpoint; |
5659 | term->selstate = DRAGGING; |
5660 | if (term->seltype == LEXICOGRAPHIC) { |
6908fed7 |
5661 | /* |
5662 | * For normal selection, we set (selstart,selend) to |
5663 | * (selpoint,selanchor) in some order. |
5664 | */ |
887035a5 |
5665 | if (poslt(selpoint, term->selanchor)) { |
5666 | term->selstart = selpoint; |
5667 | term->selend = term->selanchor; |
5668 | incpos(term->selend); |
6908fed7 |
5669 | } else { |
887035a5 |
5670 | term->selstart = term->selanchor; |
5671 | term->selend = selpoint; |
5672 | incpos(term->selend); |
6908fed7 |
5673 | } |
374330e2 |
5674 | } else { |
6908fed7 |
5675 | /* |
5676 | * For rectangular selection, we may need to |
5677 | * interchange x and y coordinates (if the user has |
5678 | * dragged in the -x and +y directions, or vice versa). |
5679 | */ |
887035a5 |
5680 | term->selstart.x = min(term->selanchor.x, selpoint.x); |
5681 | term->selend.x = 1+max(term->selanchor.x, selpoint.x); |
5682 | term->selstart.y = min(term->selanchor.y, selpoint.y); |
5683 | term->selend.y = max(term->selanchor.y, selpoint.y); |
374330e2 |
5684 | } |
887035a5 |
5685 | sel_spread(term); |
fc5b0934 |
5686 | } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) && |
5687 | a == MA_RELEASE) { |
887035a5 |
5688 | if (term->selstate == DRAGGING) { |
374330e2 |
5689 | /* |
5690 | * We've completed a selection. We now transfer the |
5691 | * data to the clipboard. |
5692 | */ |
887035a5 |
5693 | clipme(term, term->selstart, term->selend, |
c076966d |
5694 | (term->seltype == RECTANGULAR), FALSE); |
887035a5 |
5695 | term->selstate = SELECTED; |
374330e2 |
5696 | } else |
887035a5 |
5697 | term->selstate = NO_SELECTION; |
fc5b0934 |
5698 | } else if (bcooked == MBT_PASTE |
e6346999 |
5699 | && (a == MA_CLICK |
5700 | #if MULTICLICK_ONLY_EVENT |
5701 | || a == MA_2CLK || a == MA_3CLK |
5702 | #endif |
5703 | )) { |
a8327734 |
5704 | request_paste(term->frontend); |
374330e2 |
5705 | } |
5706 | |
887035a5 |
5707 | term_update(term); |
374330e2 |
5708 | } |
5709 | |
6c50d421 |
5710 | void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen, |
5711 | unsigned int modifiers, unsigned int flags) |
5712 | { |
5713 | char output[10]; |
5714 | char *p = output; |
5715 | int prependesc = FALSE; |
84b8d085 |
5716 | #if 0 |
6c50d421 |
5717 | int i; |
5718 | |
5719 | fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen); |
5720 | for (i = 0; i < tlen; i++) |
bf02e82e |
5721 | fprintf(stderr, " %04x", (unsigned)text[i]); |
6c50d421 |
5722 | fprintf(stderr, "\n"); |
84b8d085 |
5723 | #endif |
6c50d421 |
5724 | |
5725 | /* XXX Num Lock */ |
5726 | if ((flags & PKF_REPEAT) && term->repeat_off) |
5727 | return; |
5728 | |
5729 | /* Currently, Meta always just prefixes everything with ESC. */ |
5730 | if (modifiers & PKM_META) |
5731 | prependesc = TRUE; |
5732 | modifiers &= ~PKM_META; |
5733 | |
5734 | /* |
5735 | * Alt is only used for Alt+keypad, which isn't supported yet, so |
5736 | * ignore it. |
5737 | */ |
5738 | modifiers &= ~PKM_ALT; |
5739 | |
5740 | /* Standard local function keys */ |
5741 | switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) { |
5742 | case PKM_SHIFT: |
5743 | if (keysym == PK_PAGEUP) |
5744 | /* scroll up one page */; |
5745 | if (keysym == PK_PAGEDOWN) |
5746 | /* scroll down on page */; |
5747 | if (keysym == PK_INSERT) |
5748 | term_do_paste(term); |
5749 | break; |
5750 | case PKM_CONTROL: |
5751 | if (keysym == PK_PAGEUP) |
5752 | /* scroll up one line */; |
5753 | if (keysym == PK_PAGEDOWN) |
5754 | /* scroll down one line */; |
5755 | /* Control-Numlock for app-keypad mode switch */ |
5756 | if (keysym == PK_PF1) |
5757 | term->app_keypad_keys ^= 1; |
5758 | break; |
5759 | } |
5760 | |
5761 | if (modifiers & PKM_ALT) { |
5762 | /* Alt+F4 (close) */ |
5763 | /* Alt+Return (full screen) */ |
5764 | /* Alt+Space (system menu) */ |
5765 | } |
5766 | |
5767 | if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 && |
5768 | text[0] >= 0x20 && text[0] <= 0x7e) { |
5769 | /* ASCII chars + Control */ |
bf02e82e |
5770 | if ((text[0] >= 0x40 && text[0] <= 0x5f) || |
5771 | (text[0] >= 0x61 && text[0] <= 0x7a)) |
6c50d421 |
5772 | text[0] &= 0x1f; |
5773 | else { |
5774 | /* |
5775 | * Control-2 should return ^@ (0x00), Control-6 should return |
5776 | * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since |
5777 | * the DOS keyboard handling did it, and we have nothing better |
5778 | * to do with the key combo in question, we'll also map |
5779 | * Control-Backquote to ^\ (0x1C). |
5780 | */ |
5781 | switch (text[0]) { |
5782 | case ' ': text[0] = 0x00; break; |
5783 | case '-': text[0] = 0x1f; break; |
5784 | case '/': text[0] = 0x1f; break; |
5785 | case '2': text[0] = 0x00; break; |
5786 | case '3': text[0] = 0x1b; break; |
5787 | case '4': text[0] = 0x1c; break; |
5788 | case '5': text[0] = 0x1d; break; |
5789 | case '6': text[0] = 0x1e; break; |
5790 | case '7': text[0] = 0x1f; break; |
5791 | case '8': text[0] = 0x7f; break; |
5792 | case '`': text[0] = 0x1c; break; |
5793 | } |
5794 | } |
5795 | } |
5796 | |
5797 | /* Nethack keypad */ |
5798 | if (term->cfg.nethack_keypad) { |
5799 | char c = 0; |
5800 | switch (keysym) { |
5801 | case PK_KP1: c = 'b'; break; |
5802 | case PK_KP2: c = 'j'; break; |
5803 | case PK_KP3: c = 'n'; break; |
5804 | case PK_KP4: c = 'h'; break; |
5805 | case PK_KP5: c = '.'; break; |
5806 | case PK_KP6: c = 'l'; break; |
5807 | case PK_KP7: c = 'y'; break; |
5808 | case PK_KP8: c = 'k'; break; |
5809 | case PK_KP9: c = 'u'; break; |
bf02e82e |
5810 | default: break; /* else gcc warns `enum value not used' */ |
6c50d421 |
5811 | } |
5812 | if (c != 0) { |
5813 | if (c != '.') { |
5814 | if (modifiers & PKM_CONTROL) |
5815 | c &= 0x1f; |
5816 | else if (modifiers & PKM_SHIFT) |
5817 | c = toupper(c); |
5818 | } |
5819 | *p++ = c; |
5820 | goto done; |
5821 | } |
5822 | } |
5823 | |
5824 | /* Numeric Keypad */ |
5825 | if (PK_ISKEYPAD(keysym)) { |
5826 | int xkey = 0; |
5827 | |
5828 | /* |
5829 | * In VT400 mode, PFn always emits an escape sequence. In |
5830 | * Linux and tilde modes, this only happens in app keypad mode. |
5831 | */ |
5832 | if (term->cfg.funky_type == FUNKY_VT400 || |
5833 | ((term->cfg.funky_type == FUNKY_LINUX || |
5834 | term->cfg.funky_type == FUNKY_TILDE) && |
5835 | term->app_keypad_keys && !term->cfg.no_applic_k)) { |
5836 | switch (keysym) { |
5837 | case PK_PF1: xkey = 'P'; break; |
5838 | case PK_PF2: xkey = 'Q'; break; |
5839 | case PK_PF3: xkey = 'R'; break; |
5840 | case PK_PF4: xkey = 'S'; break; |
bf02e82e |
5841 | default: break; /* else gcc warns `enum value not used' */ |
6c50d421 |
5842 | } |
5843 | } |
5844 | if (term->app_keypad_keys && !term->cfg.no_applic_k) { |
5845 | switch (keysym) { |
5846 | case PK_KP0: xkey = 'p'; break; |
5847 | case PK_KP1: xkey = 'q'; break; |
5848 | case PK_KP2: xkey = 'r'; break; |
5849 | case PK_KP3: xkey = 's'; break; |
5850 | case PK_KP4: xkey = 't'; break; |
5851 | case PK_KP5: xkey = 'u'; break; |
5852 | case PK_KP6: xkey = 'v'; break; |
5853 | case PK_KP7: xkey = 'w'; break; |
5854 | case PK_KP8: xkey = 'x'; break; |
5855 | case PK_KP9: xkey = 'y'; break; |
5856 | case PK_KPDECIMAL: xkey = 'n'; break; |
5857 | case PK_KPENTER: xkey = 'M'; break; |
bf02e82e |
5858 | default: break; /* else gcc warns `enum value not used' */ |
6c50d421 |
5859 | } |
5860 | if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) { |
5861 | /* |
5862 | * xterm can't see the layout of the keypad, so it has |
5863 | * to rely on the X keysyms returned by the keys. |
5864 | * Hence, we look at the strings here, not the PuTTY |
5865 | * keysyms (which describe the layout). |
5866 | */ |
5867 | switch (text[0]) { |
5868 | case '+': |
5869 | if (modifiers & PKM_SHIFT) |
5870 | xkey = 'l'; |
5871 | else |
5872 | xkey = 'k'; |
5873 | break; |
5874 | case '/': xkey = 'o'; break; |
5875 | case '*': xkey = 'j'; break; |
5876 | case '-': xkey = 'm'; break; |
5877 | } |
5878 | } else { |
5879 | /* |
5880 | * In all other modes, we try to retain the layout of |
5881 | * the DEC keypad in application mode. |
5882 | */ |
5883 | switch (keysym) { |
5884 | case PK_KPBIGPLUS: |
5885 | /* This key covers the '-' and ',' keys on a VT220 */ |
5886 | if (modifiers & PKM_SHIFT) |
5887 | xkey = 'm'; /* VT220 '-' */ |
5888 | else |
5889 | xkey = 'l'; /* VT220 ',' */ |
5890 | break; |
5891 | case PK_KPMINUS: xkey = 'm'; break; |
5892 | case PK_KPCOMMA: xkey = 'l'; break; |
bf02e82e |
5893 | default: break; /* else gcc warns `enum value not used' */ |
6c50d421 |
5894 | } |
5895 | } |
5896 | } |
5897 | if (xkey) { |
5898 | if (term->vt52_mode) { |
5899 | if (xkey >= 'P' && xkey <= 'S') |
5900 | p += sprintf((char *) p, "\x1B%c", xkey); |
5901 | else |
5902 | p += sprintf((char *) p, "\x1B?%c", xkey); |
5903 | } else |
5904 | p += sprintf((char *) p, "\x1BO%c", xkey); |
5905 | goto done; |
5906 | } |
5907 | /* Not in application mode -- treat the number pad as arrow keys? */ |
5908 | if ((flags & PKF_NUMLOCK) == 0) { |
5909 | switch (keysym) { |
5910 | case PK_KP0: keysym = PK_INSERT; break; |
5911 | case PK_KP1: keysym = PK_END; break; |
5912 | case PK_KP2: keysym = PK_DOWN; break; |
5913 | case PK_KP3: keysym = PK_PAGEDOWN; break; |
5914 | case PK_KP4: keysym = PK_LEFT; break; |
5915 | case PK_KP5: keysym = PK_REST; break; |
5916 | case PK_KP6: keysym = PK_RIGHT; break; |
5917 | case PK_KP7: keysym = PK_HOME; break; |
5918 | case PK_KP8: keysym = PK_UP; break; |
5919 | case PK_KP9: keysym = PK_PAGEUP; break; |
bf02e82e |
5920 | default: break; /* else gcc warns `enum value not used' */ |
6c50d421 |
5921 | } |
5922 | } |
5923 | } |
5924 | |
5925 | /* Miscellaneous keys */ |
5926 | switch (keysym) { |
5927 | case PK_ESCAPE: |
5928 | *p++ = 0x1b; |
5929 | goto done; |
5930 | case PK_BACKSPACE: |
5931 | if (modifiers == 0) |
5932 | *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08); |
5933 | else if (modifiers == PKM_SHIFT) |
5934 | /* We do the opposite of what is configured */ |
5935 | *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F); |
5936 | else break; |
5937 | goto done; |
5938 | case PK_TAB: |
5939 | if (modifiers == 0) |
5940 | *p++ = 0x09; |
5941 | else if (modifiers == PKM_SHIFT) |
5942 | *p++ = 0x1B, *p++ = '[', *p++ = 'Z'; |
5943 | else break; |
5944 | goto done; |
5945 | /* XXX window.c has ctrl+shift+space sending 0xa0 */ |
5946 | case PK_PAUSE: |
5947 | if (modifiers == PKM_CONTROL) |
5948 | *p++ = 26; |
5949 | else break; |
5950 | goto done; |
5951 | case PK_RETURN: |
5952 | case PK_KPENTER: /* Odd keypad modes handled above */ |
5953 | if (modifiers == 0) { |
5954 | *p++ = 0x0d; |
5955 | if (term->cr_lf_return) |
5956 | *p++ = 0x0a; |
5957 | goto done; |
5958 | } |
bf02e82e |
5959 | default: break; /* else gcc warns `enum value not used' */ |
6c50d421 |
5960 | } |
5961 | |
5962 | /* SCO function keys and editing keys */ |
5963 | if (term->cfg.funky_type == FUNKY_SCO) { |
5964 | if (PK_ISFKEY(keysym) && keysym <= PK_F12) { |
5965 | static char const codes[] = |
5966 | "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{"; |
5967 | int index = keysym - PK_F1; |
5968 | |
5969 | if (modifiers & PKM_SHIFT) index += 12; |
5970 | if (modifiers & PKM_CONTROL) index += 24; |
5971 | p += sprintf((char *) p, "\x1B[%c", codes[index]); |
5972 | goto done; |
5973 | } |
5974 | if (PK_ISEDITING(keysym)) { |
5975 | int xkey = 0; |
5976 | |
5977 | switch (keysym) { |
5978 | case PK_DELETE: *p++ = 0x7f; goto done; |
5979 | case PK_HOME: xkey = 'H'; break; |
5980 | case PK_INSERT: xkey = 'L'; break; |
5981 | case PK_END: xkey = 'F'; break; |
5982 | case PK_PAGEUP: xkey = 'I'; break; |
5983 | case PK_PAGEDOWN: xkey = 'G'; break; |
bf02e82e |
5984 | default: break; /* else gcc warns `enum value not used' */ |
6c50d421 |
5985 | } |
5986 | p += sprintf((char *) p, "\x1B[%c", xkey); |
5987 | } |
5988 | } |
5989 | |
5990 | if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) { |
5991 | int code; |
5992 | |
5993 | if (term->cfg.funky_type == FUNKY_XTERM) { |
5994 | /* Xterm shuffles these keys, apparently. */ |
5995 | switch (keysym) { |
5996 | case PK_HOME: keysym = PK_INSERT; break; |
5997 | case PK_INSERT: keysym = PK_HOME; break; |
5998 | case PK_DELETE: keysym = PK_END; break; |
5999 | case PK_END: keysym = PK_PAGEUP; break; |
6000 | case PK_PAGEUP: keysym = PK_DELETE; break; |
6001 | case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break; |
bf02e82e |
6002 | default: break; /* else gcc warns `enum value not used' */ |
6c50d421 |
6003 | } |
6004 | } |
6005 | |
6006 | /* RXVT Home/End */ |
6007 | if (term->cfg.rxvt_homeend && |
6008 | (keysym == PK_HOME || keysym == PK_END)) { |
6009 | p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw"); |
6010 | goto done; |
6011 | } |
6012 | |
6013 | if (term->vt52_mode) { |
6014 | int xkey; |
6015 | |
6016 | /* |
6017 | * A real VT52 doesn't have these, and a VT220 doesn't |
6018 | * send anything for them in VT52 mode. |
6019 | */ |
6020 | switch (keysym) { |
6021 | case PK_HOME: xkey = 'H'; break; |
6022 | case PK_INSERT: xkey = 'L'; break; |
6023 | case PK_DELETE: xkey = 'M'; break; |
6024 | case PK_END: xkey = 'E'; break; |
6025 | case PK_PAGEUP: xkey = 'I'; break; |
6026 | case PK_PAGEDOWN: xkey = 'G'; break; |
7ffdbc1a |
6027 | default: xkey=0; break; /* else gcc warns `enum value not used'*/ |
6c50d421 |
6028 | } |
6029 | p += sprintf((char *) p, "\x1B%c", xkey); |
6030 | goto done; |
6031 | } |
6032 | |
6033 | switch (keysym) { |
6034 | case PK_HOME: code = 1; break; |
6035 | case PK_INSERT: code = 2; break; |
6036 | case PK_DELETE: code = 3; break; |
6037 | case PK_END: code = 4; break; |
6038 | case PK_PAGEUP: code = 5; break; |
6039 | case PK_PAGEDOWN: code = 6; break; |
7ffdbc1a |
6040 | default: code = 0; break; /* else gcc warns `enum value not used' */ |
6c50d421 |
6041 | } |
6042 | p += sprintf((char *) p, "\x1B[%d~", code); |
6043 | goto done; |
6044 | } |
6045 | |
6046 | if (PK_ISFKEY(keysym)) { |
6047 | /* Map Shift+F1-F10 to F11-F20 */ |
6048 | if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT)) |
6049 | keysym += 10; |
6050 | if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) && |
6051 | keysym <= PK_F14) { |
6052 | /* XXX This overrides the XTERM/VT52 mode below */ |
6053 | int offt = 0; |
6054 | if (keysym >= PK_F6) offt++; |
6055 | if (keysym >= PK_F12) offt++; |
6056 | p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c", |
6057 | 'P' + keysym - PK_F1 - offt); |
6058 | goto done; |
6059 | } |
6060 | if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) { |
6061 | p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1); |
6062 | goto done; |
6063 | } |
6064 | if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) { |
6065 | if (term->vt52_mode) |
6066 | p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1); |
6067 | else |
6068 | p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1); |
6069 | goto done; |
6070 | } |
6071 | p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1); |
6072 | goto done; |
6073 | } |
6074 | |
6075 | if (PK_ISCURSOR(keysym)) { |
6076 | int xkey; |
6077 | |
6078 | switch (keysym) { |
6079 | case PK_UP: xkey = 'A'; break; |
6080 | case PK_DOWN: xkey = 'B'; break; |
6081 | case PK_RIGHT: xkey = 'C'; break; |
6082 | case PK_LEFT: xkey = 'D'; break; |
6083 | case PK_REST: xkey = 'G'; break; /* centre key on number pad */ |
5342c096 |
6084 | default: xkey = 0; break; /* else gcc warns `enum value not used' */ |
6c50d421 |
6085 | } |
6086 | if (term->vt52_mode) |
6087 | p += sprintf((char *) p, "\x1B%c", xkey); |
6088 | else { |
6089 | int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c); |
6090 | |
6091 | /* Useful mapping of Ctrl-arrows */ |
6092 | if (modifiers == PKM_CONTROL) |
6093 | app_flg = !app_flg; |
6094 | |
6095 | if (app_flg) |
6096 | p += sprintf((char *) p, "\x1BO%c", xkey); |
6097 | else |
6098 | p += sprintf((char *) p, "\x1B[%c", xkey); |
6099 | } |
6100 | goto done; |
6101 | } |
6102 | |
6103 | done: |
6104 | if (p > output || tlen > 0) { |
6105 | /* |
6106 | * Interrupt an ongoing paste. I'm not sure |
6107 | * this is sensible, but for the moment it's |
6108 | * preferable to having to faff about buffering |
6109 | * things. |
6110 | */ |
6111 | term_nopaste(term); |
6112 | |
6113 | /* |
6114 | * We need not bother about stdin backlogs |
6115 | * here, because in GUI PuTTY we can't do |
6116 | * anything about it anyway; there's no means |
6117 | * of asking Windows to hold off on KEYDOWN |
6118 | * messages. We _have_ to buffer everything |
6119 | * we're sent. |
6120 | */ |
6121 | term_seen_key_event(term); |
6122 | |
6123 | if (prependesc) { |
84b8d085 |
6124 | #if 0 |
6c50d421 |
6125 | fprintf(stderr, "sending ESC\n"); |
84b8d085 |
6126 | #endif |
6c50d421 |
6127 | ldisc_send(term->ldisc, "\x1b", 1, 1); |
6128 | } |
6129 | |
6130 | if (p > output) { |
84b8d085 |
6131 | #if 0 |
6c50d421 |
6132 | fprintf(stderr, "sending %d bytes:", p - output); |
6133 | for (i = 0; i < p - output; i++) |
6134 | fprintf(stderr, " %02x", output[i]); |
6135 | fprintf(stderr, "\n"); |
84b8d085 |
6136 | #endif |
6c50d421 |
6137 | ldisc_send(term->ldisc, output, p - output, 1); |
6138 | } else if (tlen > 0) { |
84b8d085 |
6139 | #if 0 |
6c50d421 |
6140 | fprintf(stderr, "sending %d unichars:", tlen); |
6141 | for (i = 0; i < tlen; i++) |
bf02e82e |
6142 | fprintf(stderr, " %04x", (unsigned) text[i]); |
6c50d421 |
6143 | fprintf(stderr, "\n"); |
84b8d085 |
6144 | #endif |
6c50d421 |
6145 | luni_send(term->ldisc, text, tlen, 1); |
6146 | } |
6147 | } |
6148 | } |
6149 | |
887035a5 |
6150 | void term_nopaste(Terminal *term) |
32874aea |
6151 | { |
887035a5 |
6152 | if (term->paste_len == 0) |
32874aea |
6153 | return; |
887035a5 |
6154 | sfree(term->paste_buffer); |
f278d6f8 |
6155 | term->paste_buffer = NULL; |
887035a5 |
6156 | term->paste_len = 0; |
c9def1b8 |
6157 | } |
6158 | |
887035a5 |
6159 | int term_paste_pending(Terminal *term) |
0f660c8f |
6160 | { |
887035a5 |
6161 | return term->paste_len != 0; |
0f660c8f |
6162 | } |
6163 | |
887035a5 |
6164 | void term_paste(Terminal *term) |
32874aea |
6165 | { |
c9def1b8 |
6166 | long now, paste_diff; |
6167 | |
887035a5 |
6168 | if (term->paste_len == 0) |
32874aea |
6169 | return; |
c9def1b8 |
6170 | |
6171 | /* Don't wait forever to paste */ |
887035a5 |
6172 | if (term->paste_hold) { |
f7f27309 |
6173 | now = GETTICKCOUNT(); |
887035a5 |
6174 | paste_diff = now - term->last_paste; |
32874aea |
6175 | if (paste_diff >= 0 && paste_diff < 450) |
c9def1b8 |
6176 | return; |
6177 | } |
887035a5 |
6178 | term->paste_hold = 0; |
c9def1b8 |
6179 | |
887035a5 |
6180 | while (term->paste_pos < term->paste_len) { |
8df7a775 |
6181 | int n = 0; |
887035a5 |
6182 | while (n + term->paste_pos < term->paste_len) { |
92e23ad9 |
6183 | if (term->paste_buffer[term->paste_pos + n++] == '\015') |
8df7a775 |
6184 | break; |
6185 | } |
b9d7bcad |
6186 | if (term->ldisc) |
6187 | luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0); |
887035a5 |
6188 | term->paste_pos += n; |
c9def1b8 |
6189 | |
887035a5 |
6190 | if (term->paste_pos < term->paste_len) { |
6191 | term->paste_hold = 1; |
c9def1b8 |
6192 | return; |
6193 | } |
6194 | } |
887035a5 |
6195 | sfree(term->paste_buffer); |
6196 | term->paste_buffer = NULL; |
6197 | term->paste_len = 0; |
c9def1b8 |
6198 | } |
6199 | |
887035a5 |
6200 | static void deselect(Terminal *term) |
32874aea |
6201 | { |
887035a5 |
6202 | term->selstate = NO_SELECTION; |
6203 | term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0; |
374330e2 |
6204 | } |
6205 | |
887035a5 |
6206 | void term_deselect(Terminal *term) |
32874aea |
6207 | { |
887035a5 |
6208 | deselect(term); |
6209 | term_update(term); |
374330e2 |
6210 | } |
fe50e814 |
6211 | |
887035a5 |
6212 | int term_ldisc(Terminal *term, int option) |
32874aea |
6213 | { |
6214 | if (option == LD_ECHO) |
887035a5 |
6215 | return term->term_echoing; |
32874aea |
6216 | if (option == LD_EDIT) |
887035a5 |
6217 | return term->term_editing; |
0965bee0 |
6218 | return FALSE; |
6219 | } |
6220 | |
fbf6cb3b |
6221 | int term_data(Terminal *term, int is_stderr, const char *data, int len) |
32874aea |
6222 | { |
887035a5 |
6223 | bufchain_add(&term->inbuf, data, len); |
5471d09a |
6224 | |
74aca06d |
6225 | if (!term->in_term_out) { |
6226 | term->in_term_out = TRUE; |
39934deb |
6227 | term_reset_cblink(term); |
6228 | /* |
6229 | * During drag-selects, we do not process terminal input, |
6230 | * because the user will want the screen to hold still to |
6231 | * be selected. |
6232 | */ |
6233 | if (term->selstate != DRAGGING) |
6234 | term_out(term); |
74aca06d |
6235 | term->in_term_out = FALSE; |
6236 | } |
6237 | |
5471d09a |
6238 | /* |
a748a096 |
6239 | * term_out() always completely empties inbuf. Therefore, |
6240 | * there's no reason at all to return anything other than zero |
6241 | * from this function, because there _can't_ be a question of |
6242 | * the remote side needing to wait until term_out() has cleared |
6243 | * a backlog. |
6244 | * |
2e85c969 |
6245 | * This is a slightly suboptimal way to deal with SSH-2 - in |
5471d09a |
6246 | * principle, the window mechanism would allow us to continue |
6247 | * to accept data on forwarded ports and X connections even |
6248 | * while the terminal processing was going slowly - but we |
6249 | * can't do the 100% right thing without moving the terminal |
6250 | * processing into a separate thread, and that might hurt |
2e85c969 |
6251 | * portability. So we manage stdout buffering the old SSH-1 way: |
5471d09a |
6252 | * if the terminal processing goes slowly, the whole SSH |
6253 | * connection stops accepting data until it's ready. |
a748a096 |
6254 | * |
5471d09a |
6255 | * In practice, I can't imagine this causing serious trouble. |
6256 | */ |
6257 | return 0; |
fe50e814 |
6258 | } |
a8327734 |
6259 | |
edd0cb8a |
6260 | /* |
6261 | * Write untrusted data to the terminal. |
6262 | * The only control character that should be honoured is \n (which |
6263 | * will behave as a CRLF). |
6264 | */ |
6265 | int term_data_untrusted(Terminal *term, const char *data, int len) |
6266 | { |
6267 | int i; |
6268 | /* FIXME: more sophisticated checking? */ |
6269 | for (i = 0; i < len; i++) { |
6270 | if (data[i] == '\n') |
6271 | term_data(term, 1, "\r\n", 2); |
6272 | else if (data[i] & 0x60) |
6273 | term_data(term, 1, data + i, 1); |
6274 | } |
6275 | return 0; /* assumes that term_data() always returns 0 */ |
6276 | } |
6277 | |
a8327734 |
6278 | void term_provide_logctx(Terminal *term, void *logctx) |
6279 | { |
6280 | term->logctx = logctx; |
6281 | } |
1cff1320 |
6282 | |
6283 | void term_set_focus(Terminal *term, int has_focus) |
6284 | { |
6285 | term->has_focus = has_focus; |
6286 | term_schedule_cblink(term); |
6287 | } |
c6ccd5c2 |
6288 | |
6289 | /* |
6290 | * Provide "auto" settings for remote tty modes, suitable for an |
6291 | * application with a terminal window. |
6292 | */ |
6293 | char *term_get_ttymode(Terminal *term, const char *mode) |
6294 | { |
6295 | char *val = NULL; |
6296 | if (strcmp(mode, "ERASE") == 0) { |
6297 | val = term->cfg.bksp_is_delete ? "^?" : "^H"; |
6298 | } |
6299 | /* FIXME: perhaps we should set ONLCR based on cfg.lfhascr as well? */ |
6300 | return dupstr(val); |
6301 | } |
edd0cb8a |
6302 | |
6303 | struct term_userpass_state { |
6304 | size_t curr_prompt; |
6305 | int done_prompt; /* printed out prompt yet? */ |
6306 | size_t pos; /* cursor position */ |
6307 | }; |
6308 | |
6309 | /* |
6310 | * Process some terminal data in the course of username/password |
6311 | * input. |
6312 | */ |
6313 | int term_get_userpass_input(Terminal *term, prompts_t *p, |
6314 | unsigned char *in, int inlen) |
6315 | { |
6316 | struct term_userpass_state *s = (struct term_userpass_state *)p->data; |
6317 | if (!s) { |
6318 | /* |
6319 | * First call. Set some stuff up. |
6320 | */ |
6321 | p->data = s = snew(struct term_userpass_state); |
6322 | s->curr_prompt = 0; |
6323 | s->done_prompt = 0; |
6324 | /* We only print the `name' caption if we have to... */ |
6325 | if (p->name_reqd && p->name) { |
6326 | size_t l = strlen(p->name); |
6327 | term_data_untrusted(term, p->name, l); |
6328 | if (p->name[l-1] != '\n') |
6329 | term_data_untrusted(term, "\n", 1); |
6330 | } |
6331 | /* ...but we always print any `instruction'. */ |
6332 | if (p->instruction) { |
6333 | size_t l = strlen(p->instruction); |
6334 | term_data_untrusted(term, p->instruction, l); |
6335 | if (p->instruction[l-1] != '\n') |
6336 | term_data_untrusted(term, "\n", 1); |
6337 | } |
6338 | /* |
6339 | * Zero all the results, in case we abort half-way through. |
6340 | */ |
6341 | { |
6342 | int i; |
6343 | for (i = 0; i < p->n_prompts; i++) |
6344 | memset(p->prompts[i]->result, 0, p->prompts[i]->result_len); |
6345 | } |
6346 | } |
6347 | |
6348 | while (s->curr_prompt < p->n_prompts) { |
6349 | |
6350 | prompt_t *pr = p->prompts[s->curr_prompt]; |
6351 | int finished_prompt = 0; |
6352 | |
6353 | if (!s->done_prompt) { |
6354 | term_data_untrusted(term, pr->prompt, strlen(pr->prompt)); |
6355 | s->done_prompt = 1; |
6356 | s->pos = 0; |
6357 | } |
6358 | |
6359 | /* Breaking out here ensures that the prompt is printed even |
6360 | * if we're now waiting for user data. */ |
6361 | if (!in || !inlen) break; |
6362 | |
6363 | /* FIXME: should we be using local-line-editing code instead? */ |
6364 | while (!finished_prompt && inlen) { |
6365 | char c = *in++; |
6366 | inlen--; |
6367 | switch (c) { |
6368 | case 10: |
6369 | case 13: |
6370 | term_data(term, 0, "\r\n", 2); |
6371 | pr->result[s->pos] = '\0'; |
6372 | pr->result[pr->result_len - 1] = '\0'; |
6373 | /* go to next prompt, if any */ |
6374 | s->curr_prompt++; |
6375 | s->done_prompt = 0; |
6376 | finished_prompt = 1; /* break out */ |
6377 | break; |
6378 | case 8: |
6379 | case 127: |
6380 | if (s->pos > 0) { |
6381 | if (pr->echo) |
6382 | term_data(term, 0, "\b \b", 3); |
6383 | s->pos--; |
6384 | } |
6385 | break; |
6386 | case 21: |
6387 | case 27: |
6388 | while (s->pos > 0) { |
6389 | if (pr->echo) |
6390 | term_data(term, 0, "\b \b", 3); |
6391 | s->pos--; |
6392 | } |
6393 | break; |
6394 | case 3: |
6395 | case 4: |
6396 | /* Immediate abort. */ |
6397 | term_data(term, 0, "\r\n", 2); |
6398 | sfree(s); |
d8b4b829 |
6399 | p->data = NULL; |
edd0cb8a |
6400 | return 0; /* user abort */ |
6401 | default: |
6402 | /* |
6403 | * This simplistic check for printability is disabled |
6404 | * when we're doing password input, because some people |
6405 | * have control characters in their passwords. |
6406 | */ |
6407 | if ((!pr->echo || |
6408 | (c >= ' ' && c <= '~') || |
6409 | ((unsigned char) c >= 160)) |
6410 | && s->pos < pr->result_len - 1) { |
6411 | pr->result[s->pos++] = c; |
6412 | if (pr->echo) |
6413 | term_data(term, 0, &c, 1); |
6414 | } |
6415 | break; |
6416 | } |
6417 | } |
6418 | |
6419 | } |
6420 | |
6421 | if (s->curr_prompt < p->n_prompts) { |
6422 | return -1; /* more data required */ |
6423 | } else { |
6424 | sfree(s); |
d8b4b829 |
6425 | p->data = NULL; |
edd0cb8a |
6426 | return +1; /* all done */ |
6427 | } |
6428 | } |