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