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