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