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