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