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