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