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