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