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 )
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 )
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) )
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 */
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)
44 #define TM_PUTTY (0xFFFF)
46 #define compatibility(x) \
47 if ( ((CL_##x)&term->compatibility_level) == 0 ) { \
48 term->termstate=TOPLEVEL; \
51 #define compatibility2(x,y) \
52 if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \
53 term->termstate=TOPLEVEL; \
57 #define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 )
59 const char sco2ansicolour[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
61 #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
62 const wchar_t sel_nl[] = SEL_NL;
65 * Fetch the character at a particular position in a line array,
66 * for purposes of `wordtype'. The reason this isn't just a simple
67 * array reference is that if the character we find is UCSWIDE,
68 * then we must look one space further to the left.
70 #define UCSGET(a, x) \
71 ( (x)>0 && ((a)[(x)] & (CHAR_MASK | CSET_MASK)) == UCSWIDE ? \
72 (a)[(x)-1] : (a)[(x)] )
75 * Internal prototypes.
77 static unsigned long *resizeline(unsigned long *, int);
78 static unsigned long *lineptr(Terminal *, int, int);
79 static void do_paint(Terminal *, Context, int);
80 static void erase_lots(Terminal *, int, int, int);
81 static void swap_screen(Terminal *, int, int, int);
82 static void update_sbar(Terminal *);
83 static void deselect(Terminal *);
84 static void term_print_finish(Terminal *);
85 #ifdef OPTIMISE_SCROLL
86 static void scroll_display(Terminal *, int, int, int);
87 #endif /* OPTIMISE_SCROLL */
90 * Resize a line to make it `cols' columns wide.
92 static unsigned long *resizeline(unsigned long *line, int cols)
95 unsigned long lineattrs;
97 if (line[0] != (unsigned long)cols) {
99 * This line is the wrong length, which probably means it
100 * hasn't been accessed since a resize. Resize it now.
103 lineattrs = line[oldlen + 1];
104 line = sresize(line, 2 + cols, TTYPE);
106 for (i = oldlen; i < cols; i++)
107 line[i + 1] = ERASE_CHAR;
108 line[cols + 1] = lineattrs & LATTR_MODE;
115 * Get the number of lines in the scrollback.
117 static int sblines(Terminal *term)
119 int sblines = count234(term->scrollback);
120 if (term->cfg.erase_to_scrollback &&
121 term->alt_which && term->alt_screen) {
122 sblines += term->alt_sblines;
128 * Retrieve a line of the screen or of the scrollback, according to
129 * whether the y coordinate is non-negative or negative
132 static unsigned long *lineptr(Terminal *term, int y, int lineno)
134 unsigned long *line, *newline;
139 whichtree = term->screen;
143 if (term->cfg.erase_to_scrollback &&
144 term->alt_which && term->alt_screen) {
145 altlines = term->alt_sblines;
148 whichtree = term->scrollback;
149 treeindex = y + altlines + count234(term->scrollback);
151 whichtree = term->alt_screen;
152 treeindex = y + term->alt_sblines;
153 /* treeindex = y + count234(term->alt_screen); */
156 line = index234(whichtree, treeindex);
158 /* We assume that we don't screw up and retrieve something out of range. */
159 assert(line != NULL);
161 newline = resizeline(line, term->cols);
162 if (newline != line) {
163 delpos234(whichtree, treeindex);
164 addpos234(whichtree, newline, treeindex);
171 #define lineptr(x) lineptr(term,x,__LINE__)
174 * Set up power-on settings for the terminal.
176 static void power_on(Terminal *term)
178 term->curs.x = term->curs.y = 0;
179 term->alt_x = term->alt_y = 0;
180 term->savecurs.x = term->savecurs.y = 0;
181 term->alt_t = term->marg_t = 0;
182 if (term->rows != -1)
183 term->alt_b = term->marg_b = term->rows - 1;
185 term->alt_b = term->marg_b = 0;
186 if (term->cols != -1) {
188 for (i = 0; i < term->cols; i++)
189 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
191 term->alt_om = term->dec_om = term->cfg.dec_om;
192 term->alt_ins = term->insert = FALSE;
193 term->alt_wnext = term->wrapnext = term->save_wnext = FALSE;
194 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
195 term->alt_cset = term->cset = term->save_cset = 0;
196 term->alt_utf = term->utf = term->save_utf = 0;
198 term->alt_sco_acs = term->sco_acs = term->save_sco_acs = 0;
199 term->cset_attr[0] = term->cset_attr[1] = term->save_csattr = ATTR_ASCII;
201 term->in_vbell = FALSE;
203 term->big_cursor = 0;
204 term->default_attr = term->save_attr = term->curr_attr = ATTR_DEFAULT;
205 term->term_editing = term->term_echoing = FALSE;
206 term->app_cursor_keys = term->cfg.app_cursor;
207 term->app_keypad_keys = term->cfg.app_keypad;
208 term->use_bce = term->cfg.bce;
209 term->blink_is_real = term->cfg.blinktext;
210 term->erase_char = ERASE_CHAR;
212 term_print_finish(term);
215 for (i = 0; i < 256; i++)
216 term->wordness[i] = term->cfg.wordness[i];
219 swap_screen(term, 1, FALSE, FALSE);
220 erase_lots(term, FALSE, TRUE, TRUE);
221 swap_screen(term, 0, FALSE, FALSE);
222 erase_lots(term, FALSE, TRUE, TRUE);
227 * Force a screen update.
229 void term_update(Terminal *term)
232 ctx = get_ctx(term->frontend);
234 int need_sbar_update = term->seen_disp_event;
235 if (term->seen_disp_event && term->cfg.scroll_on_disp) {
236 term->disptop = 0; /* return to main screen */
237 term->seen_disp_event = 0;
238 need_sbar_update = TRUE;
240 if (need_sbar_update)
242 do_paint(term, ctx, TRUE);
243 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
249 * Called from front end when a keypress occurs, to trigger
250 * anything magical that needs to happen in that situation.
252 void term_seen_key_event(Terminal *term)
255 * On any keypress, clear the bell overload mechanism
256 * completely, on the grounds that large numbers of
257 * beeps coming from deliberate key action are likely
258 * to be intended (e.g. beeps from filename completion
259 * blocking repeatedly).
261 term->beep_overloaded = FALSE;
262 while (term->beephead) {
263 struct beeptime *tmp = term->beephead;
264 term->beephead = tmp->next;
267 term->beeptail = NULL;
271 * Reset the scrollback on keypress, if we're doing that.
273 if (term->cfg.scroll_on_key) {
274 term->disptop = 0; /* return to main screen */
275 term->seen_disp_event = 1;
280 * Same as power_on(), but an external function.
282 void term_pwron(Terminal *term)
285 if (term->ldisc) /* cause ldisc to notice changes */
286 ldisc_send(term->ldisc, NULL, 0, 0);
294 * When the user reconfigures us, we need to check the forbidden-
295 * alternate-screen config option, disable raw mouse mode if the
296 * user has disabled mouse reporting, and abandon a print job if
297 * the user has disabled printing.
299 void term_reconfig(Terminal *term, Config *cfg)
302 * Before adopting the new config, check all those terminal
303 * settings which control power-on defaults; and if they've
304 * changed, we will modify the current state as well as the
305 * default one. The full list is: Auto wrap mode, DEC Origin
306 * Mode, BCE, blinking text, character classes.
308 int reset_wrap, reset_decom, reset_bce, reset_blink, reset_charclass;
311 reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode);
312 reset_decom = (term->cfg.dec_om != cfg->dec_om);
313 reset_bce = (term->cfg.bce != cfg->bce);
314 reset_blink = (term->cfg.blinktext != cfg->blinktext);
316 for (i = 0; i < lenof(term->cfg.wordness); i++)
317 if (term->cfg.wordness[i] != cfg->wordness[i])
320 term->cfg = *cfg; /* STRUCTURE COPY */
323 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
325 term->alt_om = term->dec_om = term->cfg.dec_om;
327 term->use_bce = term->cfg.bce;
329 term->erase_char = (' ' | ATTR_ASCII |
331 (ATTR_FGMASK | ATTR_BGMASK)));
333 term->erase_char = ERASE_CHAR;
336 term->blink_is_real = term->cfg.blinktext;
338 for (i = 0; i < 256; i++)
339 term->wordness[i] = term->cfg.wordness[i];
341 if (term->cfg.no_alt_screen)
342 swap_screen(term, 0, FALSE, FALSE);
343 if (term->cfg.no_mouse_rep) {
344 term->xterm_mouse = 0;
345 set_raw_mouse_mode(term->frontend, 0);
347 if (term->cfg.no_remote_charset) {
348 term->cset_attr[0] = term->cset_attr[1] = ATTR_ASCII;
349 term->sco_acs = term->alt_sco_acs = 0;
352 if (!*term->cfg.printer) {
353 term_print_finish(term);
358 * Clear the scrollback.
360 void term_clrsb(Terminal *term)
364 while ((line = delpos234(term->scrollback, 0)) != NULL) {
367 term->tempsblines = 0;
368 term->alt_sblines = 0;
373 * Initialise the terminal.
375 Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
381 * Allocate a new Terminal structure and initialise the fields
384 term = snew(Terminal);
385 term->frontend = frontend;
386 term->ucsdata = ucsdata;
387 term->cfg = *mycfg; /* STRUCTURE COPY */
389 term->compatibility_level = TM_PUTTY;
390 strcpy(term->id_string, "\033[?6c");
391 term->last_blink = term->last_tblink = 0;
392 term->paste_buffer = NULL;
394 term->last_paste = 0;
395 bufchain_init(&term->inbuf);
396 bufchain_init(&term->printer_buf);
397 term->printing = term->only_printing = FALSE;
398 term->print_job = NULL;
399 term->vt52_mode = FALSE;
400 term->cr_lf_return = FALSE;
401 term->seen_disp_event = FALSE;
402 term->xterm_mouse = term->mouse_is_down = FALSE;
403 term->reset_132 = FALSE;
404 term->blinker = term->tblinker = 0;
406 term->repeat_off = FALSE;
407 term->termstate = TOPLEVEL;
408 term->selstate = NO_SELECTION;
411 term->screen = term->alt_screen = term->scrollback = NULL;
412 term->tempsblines = 0;
413 term->alt_sblines = 0;
415 term->disptext = term->dispcurs = NULL;
418 term->rows = term->cols = -1;
420 term->beephead = term->beeptail = NULL;
421 #ifdef OPTIMISE_SCROLL
422 term->scrollhead = term->scrolltail = NULL;
423 #endif /* OPTIMISE_SCROLL */
425 term->lastbeep = FALSE;
426 term->beep_overloaded = FALSE;
427 term->attr_mask = 0xffffffff;
428 term->resize_fn = NULL;
429 term->resize_ctx = NULL;
430 term->in_term_out = FALSE;
435 void term_free(Terminal *term)
438 struct beeptime *beep;
440 while ((line = delpos234(term->scrollback, 0)) != NULL)
442 freetree234(term->scrollback);
443 while ((line = delpos234(term->screen, 0)) != NULL)
445 freetree234(term->screen);
446 while ((line = delpos234(term->alt_screen, 0)) != NULL)
448 freetree234(term->alt_screen);
449 sfree(term->disptext);
450 while (term->beephead) {
451 beep = term->beephead;
452 term->beephead = beep->next;
455 bufchain_clear(&term->inbuf);
457 printer_finish_job(term->print_job);
458 bufchain_clear(&term->printer_buf);
459 sfree(term->paste_buffer);
464 * Set up the terminal for a given size.
466 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
469 unsigned long *newdisp, *line;
472 int save_alt_which = term->alt_which;
474 if (newrows == term->rows && newcols == term->cols &&
475 newsavelines == term->savelines)
476 return; /* nothing to do */
479 swap_screen(term, 0, FALSE, FALSE);
481 term->alt_t = term->marg_t = 0;
482 term->alt_b = term->marg_b = newrows - 1;
484 if (term->rows == -1) {
485 term->scrollback = newtree234(NULL);
486 term->screen = newtree234(NULL);
487 term->tempsblines = 0;
492 * Resize the screen and scrollback. We only need to shift
493 * lines around within our data structures, because lineptr()
494 * will take care of resizing each individual line if
497 * - If the new screen is longer, we shunt lines in from temporary
498 * scrollback if possible, otherwise we add new blank lines at
501 * - If the new screen is shorter, we remove any blank lines at
502 * the bottom if possible, otherwise shunt lines above the cursor
503 * to scrollback if possible, otherwise delete lines below the
506 * - Then, if the new scrollback length is less than the
507 * amount of scrollback we actually have, we must throw some
510 sblen = count234(term->scrollback);
511 /* Do this loop to expand the screen if newrows > rows */
512 assert(term->rows == count234(term->screen));
513 while (term->rows < newrows) {
514 if (term->tempsblines > 0) {
515 /* Insert a line from the scrollback at the top of the screen. */
516 assert(sblen >= term->tempsblines);
517 line = delpos234(term->scrollback, --sblen);
518 term->tempsblines -= 1;
519 addpos234(term->screen, line, 0);
521 term->savecurs.y += 1;
523 /* Add a new blank line at the bottom of the screen. */
524 line = snewn(newcols + 2, TTYPE);
526 for (j = 0; j < newcols; j++)
527 line[j + 1] = ERASE_CHAR;
528 line[newcols + 1] = LATTR_NORM;
529 addpos234(term->screen, line, count234(term->screen));
533 /* Do this loop to shrink the screen if newrows < rows */
534 while (term->rows > newrows) {
535 if (term->curs.y < term->rows - 1) {
536 /* delete bottom row, unless it contains the cursor */
537 sfree(delpos234(term->screen, term->rows - 1));
539 /* push top row to scrollback */
540 line = delpos234(term->screen, 0);
541 addpos234(term->scrollback, line, sblen++);
542 term->tempsblines += 1;
544 term->savecurs.y -= 1;
548 assert(term->rows == newrows);
549 assert(count234(term->screen) == newrows);
551 /* Delete any excess lines from the scrollback. */
552 while (sblen > newsavelines) {
553 line = delpos234(term->scrollback, 0);
557 if (sblen < term->tempsblines)
558 term->tempsblines = sblen;
559 assert(count234(term->scrollback) <= newsavelines);
560 assert(count234(term->scrollback) >= term->tempsblines);
563 /* Make a new displayed text buffer. */
564 newdisp = snewn(newrows * (newcols + 1), TTYPE);
565 for (i = 0; i < newrows * (newcols + 1); i++)
566 newdisp[i] = ATTR_INVALID;
567 sfree(term->disptext);
568 term->disptext = newdisp;
569 term->dispcurs = NULL;
571 /* Make a new alternate screen. */
572 newalt = newtree234(NULL);
573 for (i = 0; i < newrows; i++) {
574 line = snewn(newcols + 2, TTYPE);
576 for (j = 0; j < newcols; j++)
577 line[j + 1] = term->erase_char;
578 line[newcols + 1] = LATTR_NORM;
579 addpos234(newalt, line, i);
581 if (term->alt_screen) {
582 while (NULL != (line = delpos234(term->alt_screen, 0)))
584 freetree234(term->alt_screen);
586 term->alt_screen = newalt;
587 term->alt_sblines = 0;
589 term->tabs = sresize(term->tabs, newcols, unsigned char);
592 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
593 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
596 /* Check that the cursor positions are still valid. */
597 if (term->savecurs.y < 0)
598 term->savecurs.y = 0;
599 if (term->savecurs.y >= newrows)
600 term->savecurs.y = newrows - 1;
601 if (term->curs.y < 0)
603 if (term->curs.y >= newrows)
604 term->curs.y = newrows - 1;
605 if (term->curs.x >= newcols)
606 term->curs.x = newcols - 1;
607 term->alt_x = term->alt_y = 0;
608 term->wrapnext = term->alt_wnext = FALSE;
610 term->rows = newrows;
611 term->cols = newcols;
612 term->savelines = newsavelines;
615 swap_screen(term, save_alt_which, FALSE, FALSE);
620 term->resize_fn(term->resize_ctx, term->cols, term->rows);
624 * Hand a function and context pointer to the terminal which it can
625 * use to notify a back end of resizes.
627 void term_provide_resize_fn(Terminal *term,
628 void (*resize_fn)(void *, int, int),
631 term->resize_fn = resize_fn;
632 term->resize_ctx = resize_ctx;
633 if (term->cols > 0 && term->rows > 0)
634 resize_fn(resize_ctx, term->cols, term->rows);
637 /* Find the bottom line on the screen that has any content.
638 * If only the top line has content, returns 0.
639 * If no lines have content, return -1.
641 static int find_last_nonempty_line(Terminal * term, tree234 * screen)
644 for (i = count234(screen) - 1; i >= 0; i--) {
645 unsigned long *line = index234(screen, i);
648 for (j = 0; j < cols; j++) {
649 if (line[j + 1] != term->erase_char) break;
651 if (j != cols) break;
657 * Swap screens. If `reset' is TRUE and we have been asked to
658 * switch to the alternate screen, we must bring most of its
659 * configuration from the main screen and erase the contents of the
660 * alternate screen completely. (This is even true if we're already
661 * on it! Blame xterm.)
663 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
669 reset = FALSE; /* do no weird resetting if which==0 */
671 if (which != term->alt_which) {
672 term->alt_which = which;
674 ttr = term->alt_screen;
675 term->alt_screen = term->screen;
677 term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1;
679 if (!reset && !keep_cur_pos)
680 term->curs.x = term->alt_x;
683 if (!reset && !keep_cur_pos)
684 term->curs.y = term->alt_y;
687 if (!reset) term->marg_t = term->alt_t;
690 if (!reset) term->marg_b = term->alt_b;
693 if (!reset) term->dec_om = term->alt_om;
696 if (!reset) term->wrap = term->alt_wrap;
699 if (!reset) term->wrapnext = term->alt_wnext;
702 if (!reset) term->insert = term->alt_ins;
705 if (!reset) term->cset = term->alt_cset;
708 if (!reset) term->utf = term->alt_utf;
711 if (!reset) term->sco_acs = term->alt_sco_acs;
712 term->alt_sco_acs = t;
715 if (reset && term->screen) {
717 * Yes, this _is_ supposed to honour background-colour-erase.
719 erase_lots(term, FALSE, TRUE, TRUE);
723 * This might not be possible if we're called during
731 * Update the scroll bar.
733 static void update_sbar(Terminal *term)
735 int nscroll = sblines(term);
736 set_sbar(term->frontend, nscroll + term->rows,
737 nscroll + term->disptop, term->rows);
741 * Check whether the region bounded by the two pointers intersects
742 * the scroll region, and de-select the on-screen selection if so.
744 static void check_selection(Terminal *term, pos from, pos to)
746 if (poslt(from, term->selend) && poslt(term->selstart, to))
751 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
752 * for backward.) `sb' is TRUE if the scrolling is permitted to
753 * affect the scrollback buffer.
755 * NB this function invalidates all pointers into lines of the
756 * screen data structures. In particular, you MUST call fix_cpos
757 * after calling scroll() and before doing anything else that
758 * uses the cpos shortcut pointer.
760 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
762 unsigned long *line, *line2;
763 int i, seltop, olddisptop, shift;
765 if (topline != 0 || term->alt_which != 0)
768 olddisptop = term->disptop;
772 line = delpos234(term->screen, botline);
773 line = resizeline(line, term->cols);
774 for (i = 0; i < term->cols; i++)
775 line[i + 1] = term->erase_char;
776 line[term->cols + 1] = 0;
777 addpos234(term->screen, line, topline);
779 if (term->selstart.y >= topline && term->selstart.y <= botline) {
781 if (term->selstart.y > botline) {
782 term->selstart.y = botline + 1;
783 term->selstart.x = 0;
786 if (term->selend.y >= topline && term->selend.y <= botline) {
788 if (term->selend.y > botline) {
789 term->selend.y = botline + 1;
798 line = delpos234(term->screen, topline);
799 if (sb && term->savelines > 0) {
800 int sblen = count234(term->scrollback);
802 * We must add this line to the scrollback. We'll
803 * remove a line from the top of the scrollback to
804 * replace it, or allocate a new one if the
805 * scrollback isn't full.
807 if (sblen == term->savelines) {
808 sblen--, line2 = delpos234(term->scrollback, 0);
810 line2 = snewn(term->cols + 2, TTYPE);
811 line2[0] = term->cols;
812 term->tempsblines += 1;
814 addpos234(term->scrollback, line, sblen);
818 * If the user is currently looking at part of the
819 * scrollback, and they haven't enabled any options
820 * that are going to reset the scrollback as a
821 * result of this movement, then the chances are
822 * they'd like to keep looking at the same line. So
823 * we move their viewpoint at the same rate as the
824 * scroll, at least until their viewpoint hits the
825 * top end of the scrollback buffer, at which point
826 * we don't have the choice any more.
828 * Thanks to Jan Holmen Holsten for the idea and
829 * initial implementation.
831 if (term->disptop > -term->savelines && term->disptop < 0)
834 line = resizeline(line, term->cols);
835 for (i = 0; i < term->cols; i++)
836 line[i + 1] = term->erase_char;
837 line[term->cols + 1] = LATTR_NORM;
838 addpos234(term->screen, line, botline);
841 * If the selection endpoints move into the scrollback,
842 * we keep them moving until they hit the top. However,
843 * of course, if the line _hasn't_ moved into the
844 * scrollback then we don't do this, and cut them off
845 * at the top of the scroll region.
847 * This applies to selstart and selend (for an existing
848 * selection), and also selanchor (for one being
849 * selected as we speak).
851 seltop = sb ? -term->savelines : topline;
853 if (term->selstate != NO_SELECTION) {
854 if (term->selstart.y >= seltop &&
855 term->selstart.y <= botline) {
857 if (term->selstart.y < seltop) {
858 term->selstart.y = seltop;
859 term->selstart.x = 0;
862 if (term->selend.y >= seltop && term->selend.y <= botline) {
864 if (term->selend.y < seltop) {
865 term->selend.y = seltop;
869 if (term->selanchor.y >= seltop &&
870 term->selanchor.y <= botline) {
872 if (term->selanchor.y < seltop) {
873 term->selanchor.y = seltop;
874 term->selanchor.x = 0;
882 #ifdef OPTIMISE_SCROLL
883 shift += term->disptop - olddisptop;
884 if (shift < term->rows && shift > -term->rows && shift != 0)
885 scroll_display(term, topline, botline, shift);
886 #endif /* OPTIMISE_SCROLL */
889 #ifdef OPTIMISE_SCROLL
891 * Add a scroll of a region on the screen into the pending scroll list.
892 * `lines' is +ve for scrolling forward, -ve for backward.
894 * If the scroll is on the same area as the last scroll in the list,
897 static void save_scroll(Terminal *term, int topline, int botline, int lines)
899 struct scrollregion *newscroll;
900 if (term->scrolltail &&
901 term->scrolltail->topline == topline &&
902 term->scrolltail->botline == botline) {
903 term->scrolltail->lines += lines;
905 newscroll = snew(struct scrollregion);
906 newscroll->topline = topline;
907 newscroll->botline = botline;
908 newscroll->lines = lines;
909 newscroll->next = NULL;
911 if (!term->scrollhead)
912 term->scrollhead = newscroll;
914 term->scrolltail->next = newscroll;
915 term->scrolltail = newscroll;
920 * Scroll the physical display, and our conception of it in disptext.
922 static void scroll_display(Terminal *term, int topline, int botline, int lines)
924 unsigned long *start, *end;
925 int distance, size, i;
927 start = term->disptext + topline * (term->cols + 1);
928 end = term->disptext + (botline + 1) * (term->cols + 1);
929 distance = (lines > 0 ? lines : -lines) * (term->cols + 1);
930 size = end - start - distance;
932 memmove(start, start + distance, size * TSIZE);
933 if (term->dispcurs >= start + distance &&
934 term->dispcurs <= start + distance + size)
935 term->dispcurs -= distance;
936 for (i = 0; i < distance; i++)
937 (start + size)[i] |= ATTR_INVALID;
939 memmove(start + distance, start, size * TSIZE);
940 if (term->dispcurs >= start && term->dispcurs <= start + size)
941 term->dispcurs += distance;
942 for (i = 0; i < distance; i++)
943 start[i] |= ATTR_INVALID;
945 save_scroll(term, topline, botline, lines);
947 #endif /* OPTIMISE_SCROLL */
950 * Move the cursor to a given position, clipping at boundaries. We
951 * may or may not want to clip at the scroll margin: marg_clip is 0
952 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
953 * even _being_ outside the margins.
955 static void move(Terminal *term, int x, int y, int marg_clip)
962 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
965 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
976 term->wrapnext = FALSE;
980 * Save or restore the cursor and SGR mode.
982 static void save_cursor(Terminal *term, int save)
985 term->savecurs = term->curs;
986 term->save_attr = term->curr_attr;
987 term->save_cset = term->cset;
988 term->save_utf = term->utf;
989 term->save_wnext = term->wrapnext;
990 term->save_csattr = term->cset_attr[term->cset];
991 term->save_sco_acs = term->sco_acs;
993 term->curs = term->savecurs;
994 /* Make sure the window hasn't shrunk since the save */
995 if (term->curs.x >= term->cols)
996 term->curs.x = term->cols - 1;
997 if (term->curs.y >= term->rows)
998 term->curs.y = term->rows - 1;
1000 term->curr_attr = term->save_attr;
1001 term->cset = term->save_cset;
1002 term->utf = term->save_utf;
1003 term->wrapnext = term->save_wnext;
1005 * wrapnext might reset to False if the x position is no
1006 * longer at the rightmost edge.
1008 if (term->wrapnext && term->curs.x < term->cols-1)
1009 term->wrapnext = FALSE;
1010 term->cset_attr[term->cset] = term->save_csattr;
1011 term->sco_acs = term->save_sco_acs;
1014 term->erase_char = (' ' | ATTR_ASCII |
1016 (ATTR_FGMASK | ATTR_BGMASK)));
1021 * This function is called before doing _anything_ which affects
1022 * only part of a line of text. It is used to mark the boundary
1023 * between two character positions, and it indicates that some sort
1024 * of effect is going to happen on only one side of that boundary.
1026 * The effect of this function is to check whether a CJK
1027 * double-width character is straddling the boundary, and to remove
1028 * it and replace it with two spaces if so. (Of course, one or
1029 * other of those spaces is then likely to be replaced with
1030 * something else again, as a result of whatever happens next.)
1032 * Also, if the boundary is at the right-hand _edge_ of the screen,
1033 * it implies something deliberate is being done to the rightmost
1034 * column position; hence we must clear LATTR_WRAPPED2.
1036 * The input to the function is the coordinates of the _second_
1037 * character of the pair.
1039 static void check_boundary(Terminal *term, int x, int y)
1041 unsigned long *ldata;
1043 /* Validate input coordinates, just in case. */
1044 if (x == 0 || x > term->cols)
1048 if (x == term->cols) {
1049 ldata[x] &= ~LATTR_WRAPPED2;
1051 if ((ldata[x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE) {
1052 ldata[x-1] = ldata[x] =
1053 (ldata[x-1] &~ (CHAR_MASK | CSET_MASK)) | ATTR_ASCII | ' ';
1059 * Erase a large portion of the screen: the whole screen, or the
1060 * whole line, or parts thereof.
1062 static void erase_lots(Terminal *term,
1063 int line_only, int from_begin, int to_end)
1067 int erasing_lines_from_top = 0;
1070 start.y = term->curs.y;
1072 end.y = term->curs.y + 1;
1074 erase_lattr = FALSE;
1089 if (!from_begin || !to_end)
1090 check_boundary(term, term->curs.x, term->curs.y);
1091 check_selection(term, start, end);
1093 /* Clear screen also forces a full window redraw, just in case. */
1094 if (start.y == 0 && start.x == 0 && end.y == term->rows)
1095 term_invalidate(term);
1097 /* Lines scrolled away shouldn't be brought back on if the terminal
1099 if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr)
1100 erasing_lines_from_top = 1;
1102 if (term->cfg.erase_to_scrollback && erasing_lines_from_top) {
1103 /* If it's a whole number of lines, starting at the top, and
1104 * we're fully erasing them, erase by scrolling and keep the
1105 * lines in the scrollback. */
1106 int scrolllines = end.y;
1107 if (end.y == term->rows) {
1108 /* Shrink until we find a non-empty row.*/
1109 scrolllines = find_last_nonempty_line(term, term->screen) + 1;
1111 if (scrolllines > 0)
1112 scroll(term, 0, scrolllines - 1, scrolllines, TRUE);
1115 unsigned long *ldata = lineptr(start.y);
1116 while (poslt(start, end)) {
1117 if (start.x == term->cols) {
1119 ldata[start.x] &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
1121 ldata[start.x] = LATTR_NORM;
1123 ldata[start.x] = term->erase_char;
1125 if (incpos(start) && start.y < term->rows)
1126 ldata = lineptr(start.y);
1130 /* After an erase of lines from the top of the screen, we shouldn't
1131 * bring the lines back again if the terminal enlarges (since the user or
1132 * application has explictly thrown them away). */
1133 if (erasing_lines_from_top && !(term->alt_which))
1134 term->tempsblines = 0;
1138 * Insert or delete characters within the current line. n is +ve if
1139 * insertion is desired, and -ve for deletion.
1141 static void insch(Terminal *term, int n)
1143 int dir = (n < 0 ? -1 : +1);
1146 unsigned long *ldata;
1148 n = (n < 0 ? -n : n);
1149 if (n > term->cols - term->curs.x)
1150 n = term->cols - term->curs.x;
1151 m = term->cols - term->curs.x - n;
1152 cursplus.y = term->curs.y;
1153 cursplus.x = term->curs.x + n;
1154 check_selection(term, term->curs, cursplus);
1155 check_boundary(term, term->curs.x, term->curs.y);
1157 check_boundary(term, term->curs.x + n, term->curs.y);
1158 ldata = lineptr(term->curs.y);
1160 memmove(ldata + term->curs.x, ldata + term->curs.x + n, m * TSIZE);
1162 ldata[term->curs.x + m++] = term->erase_char;
1164 memmove(ldata + term->curs.x + n, ldata + term->curs.x, m * TSIZE);
1166 ldata[term->curs.x + n] = term->erase_char;
1171 * Toggle terminal mode `mode' to state `state'. (`query' indicates
1172 * whether the mode is a DEC private one or a normal one.)
1174 static void toggle_mode(Terminal *term, int mode, int query, int state)
1176 unsigned long ticks;
1180 case 1: /* DECCKM: application cursor keys */
1181 term->app_cursor_keys = state;
1183 case 2: /* DECANM: VT52 mode */
1184 term->vt52_mode = !state;
1185 if (term->vt52_mode) {
1186 term->blink_is_real = FALSE;
1187 term->vt52_bold = FALSE;
1189 term->blink_is_real = term->cfg.blinktext;
1192 case 3: /* DECCOLM: 80/132 columns */
1194 if (!term->cfg.no_remote_resize)
1195 request_resize(term->frontend, state ? 132 : 80, term->rows);
1196 term->reset_132 = state;
1197 term->alt_t = term->marg_t = 0;
1198 term->alt_b = term->marg_b = term->rows - 1;
1199 move(term, 0, 0, 0);
1200 erase_lots(term, FALSE, TRUE, TRUE);
1202 case 5: /* DECSCNM: reverse video */
1204 * Toggle reverse video. If we receive an OFF within the
1205 * visual bell timeout period after an ON, we trigger an
1206 * effective visual bell, so that ESC[?5hESC[?5l will
1207 * always be an actually _visible_ visual bell.
1209 ticks = GETTICKCOUNT();
1210 /* turn off a previous vbell to avoid inconsistencies */
1211 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
1212 term->in_vbell = FALSE;
1213 if (term->rvideo && !state && /* we're turning it off... */
1214 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
1215 /* If there's no vbell timeout already, or this one lasts
1216 * longer, replace vbell_timeout with ours. */
1217 if (!term->in_vbell ||
1218 (term->rvbell_startpoint - term->vbell_startpoint <
1220 term->vbell_startpoint = term->rvbell_startpoint;
1221 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
1222 } else if (!term->rvideo && state) {
1223 /* This is an ON, so we notice the time and save it. */
1224 term->rvbell_startpoint = ticks;
1226 term->rvideo = state;
1227 term->seen_disp_event = TRUE;
1231 case 6: /* DECOM: DEC origin mode */
1232 term->dec_om = state;
1234 case 7: /* DECAWM: auto wrap */
1237 case 8: /* DECARM: auto key repeat */
1238 term->repeat_off = !state;
1240 case 10: /* DECEDM: set local edit mode */
1241 term->term_editing = state;
1242 if (term->ldisc) /* cause ldisc to notice changes */
1243 ldisc_send(term->ldisc, NULL, 0, 0);
1245 case 25: /* DECTCEM: enable/disable cursor */
1246 compatibility2(OTHER, VT220);
1247 term->cursor_on = state;
1248 term->seen_disp_event = TRUE;
1250 case 47: /* alternate screen */
1251 compatibility(OTHER);
1253 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
1256 case 1000: /* xterm mouse 1 */
1257 term->xterm_mouse = state ? 1 : 0;
1258 set_raw_mouse_mode(term->frontend, state);
1260 case 1002: /* xterm mouse 2 */
1261 term->xterm_mouse = state ? 2 : 0;
1262 set_raw_mouse_mode(term->frontend, state);
1264 case 1047: /* alternate screen */
1265 compatibility(OTHER);
1267 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
1270 case 1048: /* save/restore cursor */
1271 if (!term->cfg.no_alt_screen)
1272 save_cursor(term, state);
1273 if (!state) term->seen_disp_event = TRUE;
1275 case 1049: /* cursor & alternate screen */
1276 if (state && !term->cfg.no_alt_screen)
1277 save_cursor(term, state);
1278 if (!state) term->seen_disp_event = TRUE;
1279 compatibility(OTHER);
1281 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
1282 if (!state && !term->cfg.no_alt_screen)
1283 save_cursor(term, state);
1288 case 4: /* IRM: set insert mode */
1289 compatibility(VT102);
1290 term->insert = state;
1292 case 12: /* SRM: set echo mode */
1293 term->term_echoing = !state;
1294 if (term->ldisc) /* cause ldisc to notice changes */
1295 ldisc_send(term->ldisc, NULL, 0, 0);
1297 case 20: /* LNM: Return sends ... */
1298 term->cr_lf_return = state;
1300 case 34: /* WYULCURM: Make cursor BIG */
1301 compatibility2(OTHER, VT220);
1302 term->big_cursor = !state;
1307 * Process an OSC sequence: set window title or icon name.
1309 static void do_osc(Terminal *term)
1312 while (term->osc_strlen--)
1313 term->wordness[(unsigned char)
1314 term->osc_string[term->osc_strlen]] = term->esc_args[0];
1316 term->osc_string[term->osc_strlen] = '\0';
1317 switch (term->esc_args[0]) {
1320 if (!term->cfg.no_remote_wintitle)
1321 set_icon(term->frontend, term->osc_string);
1322 if (term->esc_args[0] == 1)
1324 /* fall through: parameter 0 means set both */
1327 if (!term->cfg.no_remote_wintitle)
1328 set_title(term->frontend, term->osc_string);
1335 * ANSI printing routines.
1337 static void term_print_setup(Terminal *term)
1339 bufchain_clear(&term->printer_buf);
1340 term->print_job = printer_start_job(term->cfg.printer);
1342 static void term_print_flush(Terminal *term)
1347 while ((size = bufchain_size(&term->printer_buf)) > 5) {
1348 bufchain_prefix(&term->printer_buf, &data, &len);
1351 printer_job_data(term->print_job, data, len);
1352 bufchain_consume(&term->printer_buf, len);
1355 static void term_print_finish(Terminal *term)
1361 if (!term->printing && !term->only_printing)
1362 return; /* we need do nothing */
1364 term_print_flush(term);
1365 while ((size = bufchain_size(&term->printer_buf)) > 0) {
1366 bufchain_prefix(&term->printer_buf, &data, &len);
1368 if (c == '\033' || c == '\233') {
1369 bufchain_consume(&term->printer_buf, size);
1372 printer_job_data(term->print_job, &c, 1);
1373 bufchain_consume(&term->printer_buf, 1);
1376 printer_finish_job(term->print_job);
1377 term->print_job = NULL;
1378 term->printing = term->only_printing = FALSE;
1382 * Remove everything currently in `inbuf' and stick it up on the
1383 * in-memory display. There's a big state machine in here to
1384 * process escape sequences...
1386 void term_out(Terminal *term)
1389 unsigned char localbuf[256], *chars;
1394 chars = NULL; /* placate compiler warnings */
1395 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
1399 bufchain_prefix(&term->inbuf, &ret, &nchars);
1400 if (nchars > sizeof(localbuf))
1401 nchars = sizeof(localbuf);
1402 memcpy(localbuf, ret, nchars);
1403 bufchain_consume(&term->inbuf, nchars);
1405 assert(chars != NULL);
1411 * Optionally log the session traffic to a file. Useful for
1412 * debugging and possibly also useful for actual logging.
1414 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
1415 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
1421 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1422 * be able to display 8-bit characters, but I'll let that go 'cause
1427 * If we're printing, add the character to the printer
1430 if (term->printing) {
1431 bufchain_add(&term->printer_buf, &c, 1);
1434 * If we're in print-only mode, we use a much simpler
1435 * state machine designed only to recognise the ESC[4i
1436 * termination sequence.
1438 if (term->only_printing) {
1440 term->print_state = 1;
1441 else if (c == (unsigned char)'\233')
1442 term->print_state = 2;
1443 else if (c == '[' && term->print_state == 1)
1444 term->print_state = 2;
1445 else if (c == '4' && term->print_state == 2)
1446 term->print_state = 3;
1447 else if (c == 'i' && term->print_state == 3)
1448 term->print_state = 4;
1450 term->print_state = 0;
1451 if (term->print_state == 4) {
1452 term_print_finish(term);
1458 /* First see about all those translations. */
1459 if (term->termstate == TOPLEVEL) {
1461 switch (term->utf_state) {
1464 /* UTF-8 must be stateless so we ignore iso2022. */
1465 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1466 c = term->ucsdata->unitab_ctrl[c];
1467 else c = ((unsigned char)c) | ATTR_ASCII;
1469 } else if ((c & 0xe0) == 0xc0) {
1470 term->utf_size = term->utf_state = 1;
1471 term->utf_char = (c & 0x1f);
1472 } else if ((c & 0xf0) == 0xe0) {
1473 term->utf_size = term->utf_state = 2;
1474 term->utf_char = (c & 0x0f);
1475 } else if ((c & 0xf8) == 0xf0) {
1476 term->utf_size = term->utf_state = 3;
1477 term->utf_char = (c & 0x07);
1478 } else if ((c & 0xfc) == 0xf8) {
1479 term->utf_size = term->utf_state = 4;
1480 term->utf_char = (c & 0x03);
1481 } else if ((c & 0xfe) == 0xfc) {
1482 term->utf_size = term->utf_state = 5;
1483 term->utf_char = (c & 0x01);
1494 if ((c & 0xC0) != 0x80) {
1497 term->utf_state = 0;
1500 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
1501 if (--term->utf_state)
1506 /* Is somebody trying to be evil! */
1508 (c < 0x800 && term->utf_size >= 2) ||
1509 (c < 0x10000 && term->utf_size >= 3) ||
1510 (c < 0x200000 && term->utf_size >= 4) ||
1511 (c < 0x4000000 && term->utf_size >= 5))
1514 /* Unicode line separator and paragraph separator are CR-LF */
1515 if (c == 0x2028 || c == 0x2029)
1518 /* High controls are probably a Baaad idea too. */
1522 /* The UTF-16 surrogates are not nice either. */
1523 /* The standard give the option of decoding these:
1524 * I don't want to! */
1525 if (c >= 0xD800 && c < 0xE000)
1528 /* ISO 10646 characters now limited to UTF-16 range. */
1532 /* This is currently a TagPhobic application.. */
1533 if (c >= 0xE0000 && c <= 0xE007F)
1536 /* U+FEFF is best seen as a null. */
1539 /* But U+FFFE is an error. */
1540 if (c == 0xFFFE || c == 0xFFFF)
1543 /* Oops this is a 16bit implementation */
1548 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1549 else if(term->sco_acs &&
1550 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
1552 if (term->sco_acs == 2) c |= 0x80;
1555 switch (term->cset_attr[term->cset]) {
1557 * Linedraw characters are different from 'ESC ( B'
1558 * only for a small range. For ones outside that
1559 * range, make sure we use the same font as well as
1560 * the same encoding.
1563 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1564 c = term->ucsdata->unitab_ctrl[c];
1566 c = ((unsigned char) c) | ATTR_LINEDRW;
1570 /* If UK-ASCII, make the '#' a LineDraw Pound */
1572 c = '}' | ATTR_LINEDRW;
1575 /*FALLTHROUGH*/ case ATTR_ASCII:
1576 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1577 c = term->ucsdata->unitab_ctrl[c];
1579 c = ((unsigned char) c) | ATTR_ASCII;
1582 if (c>=' ') c = ((unsigned char)c) | ATTR_SCOACS;
1588 /* How about C1 controls ? */
1589 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
1590 !term->vt52_mode && has_compat(VT220)) {
1591 term->termstate = SEEN_ESC;
1592 term->esc_query = FALSE;
1593 c = '@' + (c & 0x1F);
1596 /* Or the GL control. */
1597 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
1598 if (term->curs.x && !term->wrapnext)
1600 term->wrapnext = FALSE;
1602 if (!term->cfg.no_dbackspace) /* destructive bksp might be disabled */
1603 *term->cpos = (' ' | term->curr_attr | ATTR_ASCII);
1605 /* Or normal C0 controls. */
1606 if ((c & -32) == 0 && term->termstate < DO_CTRLS) {
1608 case '\005': /* ENQ: terminal type query */
1609 /* Strictly speaking this is VT100 but a VT100 defaults to
1610 * no response. Other terminals respond at their option.
1612 * Don't put a CR in the default string as this tends to
1613 * upset some weird software.
1615 * An xterm returns "xterm" (5 characters)
1617 compatibility(ANSIMIN);
1619 char abuf[256], *s, *d;
1621 for (s = term->cfg.answerback, d = abuf; *s; s++) {
1623 if (*s >= 'a' && *s <= 'z')
1624 *d++ = (*s - ('a' - 1));
1625 else if ((*s >= '@' && *s <= '_') ||
1626 *s == '?' || (*s & 0x80))
1631 } else if (*s == '^') {
1636 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
1640 case '\007': /* BEL: Bell */
1642 struct beeptime *newbeep;
1643 unsigned long ticks;
1645 ticks = GETTICKCOUNT();
1647 if (!term->beep_overloaded) {
1648 newbeep = snew(struct beeptime);
1649 newbeep->ticks = ticks;
1650 newbeep->next = NULL;
1651 if (!term->beephead)
1652 term->beephead = newbeep;
1654 term->beeptail->next = newbeep;
1655 term->beeptail = newbeep;
1660 * Throw out any beeps that happened more than
1663 while (term->beephead &&
1664 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
1665 struct beeptime *tmp = term->beephead;
1666 term->beephead = tmp->next;
1668 if (!term->beephead)
1669 term->beeptail = NULL;
1673 if (term->cfg.bellovl && term->beep_overloaded &&
1674 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
1676 * If we're currently overloaded and the
1677 * last beep was more than s seconds ago,
1678 * leave overload mode.
1680 term->beep_overloaded = FALSE;
1681 } else if (term->cfg.bellovl && !term->beep_overloaded &&
1682 term->nbeeps >= term->cfg.bellovl_n) {
1684 * Now, if we have n or more beeps
1685 * remaining in the queue, go into overload
1688 term->beep_overloaded = TRUE;
1690 term->lastbeep = ticks;
1693 * Perform an actual beep if we're not overloaded.
1695 if (!term->cfg.bellovl || !term->beep_overloaded) {
1696 beep(term->frontend, term->cfg.beep);
1697 if (term->cfg.beep == BELL_VISUAL) {
1698 term->in_vbell = TRUE;
1699 term->vbell_startpoint = ticks;
1703 term->seen_disp_event = TRUE;
1706 case '\b': /* BS: Back space */
1707 if (term->curs.x == 0 &&
1708 (term->curs.y == 0 || term->wrap == 0))
1710 else if (term->curs.x == 0 && term->curs.y > 0)
1711 term->curs.x = term->cols - 1, term->curs.y--;
1712 else if (term->wrapnext)
1713 term->wrapnext = FALSE;
1717 term->seen_disp_event = TRUE;
1719 case '\016': /* LS1: Locking-shift one */
1720 compatibility(VT100);
1723 case '\017': /* LS0: Locking-shift zero */
1724 compatibility(VT100);
1727 case '\033': /* ESC: Escape */
1728 if (term->vt52_mode)
1729 term->termstate = VT52_ESC;
1731 compatibility(ANSIMIN);
1732 term->termstate = SEEN_ESC;
1733 term->esc_query = FALSE;
1736 case '\015': /* CR: Carriage return */
1738 term->wrapnext = FALSE;
1740 term->seen_disp_event = TRUE;
1741 term->paste_hold = 0;
1743 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1745 case '\014': /* FF: Form feed */
1746 if (has_compat(SCOANSI)) {
1747 move(term, 0, 0, 0);
1748 erase_lots(term, FALSE, FALSE, TRUE);
1750 term->wrapnext = FALSE;
1751 term->seen_disp_event = 1;
1754 case '\013': /* VT: Line tabulation */
1755 compatibility(VT100);
1756 case '\012': /* LF: Line feed */
1757 if (term->curs.y == term->marg_b)
1758 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1759 else if (term->curs.y < term->rows - 1)
1761 if (term->cfg.lfhascr)
1764 term->wrapnext = FALSE;
1765 term->seen_disp_event = 1;
1766 term->paste_hold = 0;
1768 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1770 case '\t': /* HT: Character tabulation */
1772 pos old_curs = term->curs;
1773 unsigned long *ldata = lineptr(term->curs.y);
1777 } while (term->curs.x < term->cols - 1 &&
1778 !term->tabs[term->curs.x]);
1780 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM) {
1781 if (term->curs.x >= term->cols / 2)
1782 term->curs.x = term->cols / 2 - 1;
1784 if (term->curs.x >= term->cols)
1785 term->curs.x = term->cols - 1;
1789 check_selection(term, old_curs, term->curs);
1791 term->seen_disp_event = TRUE;
1795 switch (term->termstate) {
1797 /* Only graphic characters get this far;
1798 * ctrls are stripped above */
1799 if (term->wrapnext && term->wrap) {
1800 term->cpos[1] |= LATTR_WRAPPED;
1801 if (term->curs.y == term->marg_b)
1802 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1803 else if (term->curs.y < term->rows - 1)
1807 term->wrapnext = FALSE;
1811 if (term->selstate != NO_SELECTION) {
1812 pos cursplus = term->curs;
1814 check_selection(term, term->curs, cursplus);
1816 if (((c & CSET_MASK) == ATTR_ASCII || (c & CSET_MASK) == 0) &&
1818 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1824 width = wcwidth((wchar_t) c);
1828 * If we're about to display a double-width
1829 * character starting in the rightmost
1830 * column, then we do something special
1831 * instead. We must print a space in the
1832 * last column of the screen, then wrap;
1833 * and we also set LATTR_WRAPPED2 which
1834 * instructs subsequent cut-and-pasting not
1835 * only to splice this line to the one
1836 * after it, but to ignore the space in the
1837 * last character position as well.
1838 * (Because what was actually output to the
1839 * terminal was presumably just a sequence
1840 * of CJK characters, and we don't want a
1841 * space to be pasted in the middle of
1842 * those just because they had the
1843 * misfortune to start in the wrong parity
1844 * column. xterm concurs.)
1846 check_boundary(term, term->curs.x, term->curs.y);
1847 check_boundary(term, term->curs.x+2, term->curs.y);
1848 if (term->curs.x == term->cols-1) {
1849 *term->cpos++ = ATTR_ASCII | ' ' | term->curr_attr;
1850 *term->cpos |= LATTR_WRAPPED | LATTR_WRAPPED2;
1851 if (term->curs.y == term->marg_b)
1852 scroll(term, term->marg_t, term->marg_b,
1854 else if (term->curs.y < term->rows - 1)
1858 /* Now we must check_boundary again, of course. */
1859 check_boundary(term, term->curs.x, term->curs.y);
1860 check_boundary(term, term->curs.x+2, term->curs.y);
1862 *term->cpos++ = c | term->curr_attr;
1863 *term->cpos++ = UCSWIDE | term->curr_attr;
1867 check_boundary(term, term->curs.x, term->curs.y);
1868 check_boundary(term, term->curs.x+1, term->curs.y);
1869 *term->cpos++ = c | term->curr_attr;
1876 if (term->curs.x == term->cols) {
1879 term->wrapnext = TRUE;
1880 if (term->wrap && term->vt52_mode) {
1881 term->cpos[1] |= LATTR_WRAPPED;
1882 if (term->curs.y == term->marg_b)
1883 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1884 else if (term->curs.y < term->rows - 1)
1888 term->wrapnext = FALSE;
1891 term->seen_disp_event = 1;
1896 * This state is virtually identical to SEEN_ESC, with the
1897 * exception that we have an OSC sequence in the pipeline,
1898 * and _if_ we see a backslash, we process it.
1902 term->termstate = TOPLEVEL;
1905 /* else fall through */
1907 if (c >= ' ' && c <= '/') {
1908 if (term->esc_query)
1909 term->esc_query = -1;
1911 term->esc_query = c;
1914 term->termstate = TOPLEVEL;
1915 switch (ANSI(c, term->esc_query)) {
1916 case '[': /* enter CSI mode */
1917 term->termstate = SEEN_CSI;
1918 term->esc_nargs = 1;
1919 term->esc_args[0] = ARG_DEFAULT;
1920 term->esc_query = FALSE;
1922 case ']': /* OSC: xterm escape sequences */
1923 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1924 compatibility(OTHER);
1925 term->termstate = SEEN_OSC;
1926 term->esc_args[0] = 0;
1928 case '7': /* DECSC: save cursor */
1929 compatibility(VT100);
1930 save_cursor(term, TRUE);
1932 case '8': /* DECRC: restore cursor */
1933 compatibility(VT100);
1934 save_cursor(term, FALSE);
1935 term->seen_disp_event = TRUE;
1937 case '=': /* DECKPAM: Keypad application mode */
1938 compatibility(VT100);
1939 term->app_keypad_keys = TRUE;
1941 case '>': /* DECKPNM: Keypad numeric mode */
1942 compatibility(VT100);
1943 term->app_keypad_keys = FALSE;
1945 case 'D': /* IND: exactly equivalent to LF */
1946 compatibility(VT100);
1947 if (term->curs.y == term->marg_b)
1948 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1949 else if (term->curs.y < term->rows - 1)
1952 term->wrapnext = FALSE;
1953 term->seen_disp_event = TRUE;
1955 case 'E': /* NEL: exactly equivalent to CR-LF */
1956 compatibility(VT100);
1958 if (term->curs.y == term->marg_b)
1959 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1960 else if (term->curs.y < term->rows - 1)
1963 term->wrapnext = FALSE;
1964 term->seen_disp_event = TRUE;
1966 case 'M': /* RI: reverse index - backwards LF */
1967 compatibility(VT100);
1968 if (term->curs.y == term->marg_t)
1969 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
1970 else if (term->curs.y > 0)
1973 term->wrapnext = FALSE;
1974 term->seen_disp_event = TRUE;
1976 case 'Z': /* DECID: terminal type query */
1977 compatibility(VT100);
1979 ldisc_send(term->ldisc, term->id_string,
1980 strlen(term->id_string), 0);
1982 case 'c': /* RIS: restore power-on settings */
1983 compatibility(VT100);
1985 if (term->ldisc) /* cause ldisc to notice changes */
1986 ldisc_send(term->ldisc, NULL, 0, 0);
1987 if (term->reset_132) {
1988 if (!term->cfg.no_remote_resize)
1989 request_resize(term->frontend, 80, term->rows);
1990 term->reset_132 = 0;
1994 term->seen_disp_event = TRUE;
1996 case 'H': /* HTS: set a tab */
1997 compatibility(VT100);
1998 term->tabs[term->curs.x] = TRUE;
2001 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
2002 compatibility(VT100);
2004 unsigned long *ldata;
2008 for (i = 0; i < term->rows; i++) {
2010 for (j = 0; j < term->cols; j++)
2011 ldata[j] = ATTR_DEFAULT | 'E';
2012 ldata[term->cols] = 0;
2015 term->seen_disp_event = TRUE;
2016 scrtop.x = scrtop.y = 0;
2018 scrbot.y = term->rows;
2019 check_selection(term, scrtop, scrbot);
2023 case ANSI('3', '#'):
2024 case ANSI('4', '#'):
2025 case ANSI('5', '#'):
2026 case ANSI('6', '#'):
2027 compatibility(VT100);
2029 unsigned long nlattr;
2030 unsigned long *ldata;
2031 switch (ANSI(c, term->esc_query)) {
2032 case ANSI('3', '#'): /* DECDHL: 2*height, top */
2035 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
2038 case ANSI('5', '#'): /* DECSWL: normal */
2039 nlattr = LATTR_NORM;
2041 default: /* case ANSI('6', '#'): DECDWL: 2*width */
2042 nlattr = LATTR_WIDE;
2045 ldata = lineptr(term->curs.y);
2046 ldata[term->cols] &= ~LATTR_MODE;
2047 ldata[term->cols] |= nlattr;
2050 /* GZD4: G0 designate 94-set */
2051 case ANSI('A', '('):
2052 compatibility(VT100);
2053 if (!term->cfg.no_remote_charset)
2054 term->cset_attr[0] = ATTR_GBCHR;
2056 case ANSI('B', '('):
2057 compatibility(VT100);
2058 if (!term->cfg.no_remote_charset)
2059 term->cset_attr[0] = ATTR_ASCII;
2061 case ANSI('0', '('):
2062 compatibility(VT100);
2063 if (!term->cfg.no_remote_charset)
2064 term->cset_attr[0] = ATTR_LINEDRW;
2066 case ANSI('U', '('):
2067 compatibility(OTHER);
2068 if (!term->cfg.no_remote_charset)
2069 term->cset_attr[0] = ATTR_SCOACS;
2071 /* G1D4: G1-designate 94-set */
2072 case ANSI('A', ')'):
2073 compatibility(VT100);
2074 if (!term->cfg.no_remote_charset)
2075 term->cset_attr[1] = ATTR_GBCHR;
2077 case ANSI('B', ')'):
2078 compatibility(VT100);
2079 if (!term->cfg.no_remote_charset)
2080 term->cset_attr[1] = ATTR_ASCII;
2082 case ANSI('0', ')'):
2083 compatibility(VT100);
2084 if (!term->cfg.no_remote_charset)
2085 term->cset_attr[1] = ATTR_LINEDRW;
2087 case ANSI('U', ')'):
2088 compatibility(OTHER);
2089 if (!term->cfg.no_remote_charset)
2090 term->cset_attr[1] = ATTR_SCOACS;
2092 /* DOCS: Designate other coding system */
2093 case ANSI('8', '%'): /* Old Linux code */
2094 case ANSI('G', '%'):
2095 compatibility(OTHER);
2096 if (!term->cfg.no_remote_charset)
2099 case ANSI('@', '%'):
2100 compatibility(OTHER);
2101 if (!term->cfg.no_remote_charset)
2107 term->termstate = TOPLEVEL; /* default */
2109 if (term->esc_nargs <= ARGS_MAX) {
2110 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
2111 term->esc_args[term->esc_nargs - 1] = 0;
2112 term->esc_args[term->esc_nargs - 1] =
2113 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
2115 term->termstate = SEEN_CSI;
2116 } else if (c == ';') {
2117 if (++term->esc_nargs <= ARGS_MAX)
2118 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
2119 term->termstate = SEEN_CSI;
2120 } else if (c < '@') {
2121 if (term->esc_query)
2122 term->esc_query = -1;
2124 term->esc_query = TRUE;
2126 term->esc_query = c;
2127 term->termstate = SEEN_CSI;
2129 switch (ANSI(c, term->esc_query)) {
2130 case 'A': /* CUU: move up N lines */
2131 move(term, term->curs.x,
2132 term->curs.y - def(term->esc_args[0], 1), 1);
2133 term->seen_disp_event = TRUE;
2135 case 'e': /* VPR: move down N lines */
2136 compatibility(ANSI);
2138 case 'B': /* CUD: Cursor down */
2139 move(term, term->curs.x,
2140 term->curs.y + def(term->esc_args[0], 1), 1);
2141 term->seen_disp_event = TRUE;
2143 case ANSI('c', '>'): /* DA: report xterm version */
2144 compatibility(OTHER);
2145 /* this reports xterm version 136 so that VIM can
2146 use the drag messages from the mouse reporting */
2148 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
2150 case 'a': /* HPR: move right N cols */
2151 compatibility(ANSI);
2153 case 'C': /* CUF: Cursor right */
2154 move(term, term->curs.x + def(term->esc_args[0], 1),
2156 term->seen_disp_event = TRUE;
2158 case 'D': /* CUB: move left N cols */
2159 move(term, term->curs.x - def(term->esc_args[0], 1),
2161 term->seen_disp_event = TRUE;
2163 case 'E': /* CNL: move down N lines and CR */
2164 compatibility(ANSI);
2166 term->curs.y + def(term->esc_args[0], 1), 1);
2167 term->seen_disp_event = TRUE;
2169 case 'F': /* CPL: move up N lines and CR */
2170 compatibility(ANSI);
2172 term->curs.y - def(term->esc_args[0], 1), 1);
2173 term->seen_disp_event = TRUE;
2176 case '`': /* HPA: set horizontal posn */
2177 compatibility(ANSI);
2178 move(term, def(term->esc_args[0], 1) - 1,
2180 term->seen_disp_event = TRUE;
2182 case 'd': /* VPA: set vertical posn */
2183 compatibility(ANSI);
2184 move(term, term->curs.x,
2185 ((term->dec_om ? term->marg_t : 0) +
2186 def(term->esc_args[0], 1) - 1),
2187 (term->dec_om ? 2 : 0));
2188 term->seen_disp_event = TRUE;
2191 case 'f': /* HVP: set horz and vert posns at once */
2192 if (term->esc_nargs < 2)
2193 term->esc_args[1] = ARG_DEFAULT;
2194 move(term, def(term->esc_args[1], 1) - 1,
2195 ((term->dec_om ? term->marg_t : 0) +
2196 def(term->esc_args[0], 1) - 1),
2197 (term->dec_om ? 2 : 0));
2198 term->seen_disp_event = TRUE;
2200 case 'J': /* ED: erase screen or parts of it */
2202 unsigned int i = def(term->esc_args[0], 0) + 1;
2205 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
2208 term->seen_disp_event = TRUE;
2210 case 'K': /* EL: erase line or parts of it */
2212 unsigned int i = def(term->esc_args[0], 0) + 1;
2215 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
2217 term->seen_disp_event = TRUE;
2219 case 'L': /* IL: insert lines */
2220 compatibility(VT102);
2221 if (term->curs.y <= term->marg_b)
2222 scroll(term, term->curs.y, term->marg_b,
2223 -def(term->esc_args[0], 1), FALSE);
2225 term->seen_disp_event = TRUE;
2227 case 'M': /* DL: delete lines */
2228 compatibility(VT102);
2229 if (term->curs.y <= term->marg_b)
2230 scroll(term, term->curs.y, term->marg_b,
2231 def(term->esc_args[0], 1),
2234 term->seen_disp_event = TRUE;
2236 case '@': /* ICH: insert chars */
2237 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2238 compatibility(VT102);
2239 insch(term, def(term->esc_args[0], 1));
2240 term->seen_disp_event = TRUE;
2242 case 'P': /* DCH: delete chars */
2243 compatibility(VT102);
2244 insch(term, -def(term->esc_args[0], 1));
2245 term->seen_disp_event = TRUE;
2247 case 'c': /* DA: terminal type query */
2248 compatibility(VT100);
2249 /* This is the response for a VT102 */
2251 ldisc_send(term->ldisc, term->id_string,
2252 strlen(term->id_string), 0);
2254 case 'n': /* DSR: cursor position query */
2256 if (term->esc_args[0] == 6) {
2258 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
2260 ldisc_send(term->ldisc, buf, strlen(buf), 0);
2261 } else if (term->esc_args[0] == 5) {
2262 ldisc_send(term->ldisc, "\033[0n", 4, 0);
2266 case 'h': /* SM: toggle modes to high */
2268 compatibility(VT100);
2271 for (i = 0; i < term->esc_nargs; i++)
2272 toggle_mode(term, term->esc_args[i],
2273 term->esc_query, TRUE);
2276 case 'i': /* MC: Media copy */
2278 compatibility(VT100);
2280 if (term->esc_nargs != 1) break;
2281 if (term->esc_args[0] == 5 && *term->cfg.printer) {
2282 term->printing = TRUE;
2283 term->only_printing = !term->esc_query;
2284 term->print_state = 0;
2285 term_print_setup(term);
2286 } else if (term->esc_args[0] == 4 &&
2288 term_print_finish(term);
2292 case 'l': /* RM: toggle modes to low */
2294 compatibility(VT100);
2297 for (i = 0; i < term->esc_nargs; i++)
2298 toggle_mode(term, term->esc_args[i],
2299 term->esc_query, FALSE);
2302 case 'g': /* TBC: clear tabs */
2303 compatibility(VT100);
2304 if (term->esc_nargs == 1) {
2305 if (term->esc_args[0] == 0) {
2306 term->tabs[term->curs.x] = FALSE;
2307 } else if (term->esc_args[0] == 3) {
2309 for (i = 0; i < term->cols; i++)
2310 term->tabs[i] = FALSE;
2314 case 'r': /* DECSTBM: set scroll margins */
2315 compatibility(VT100);
2316 if (term->esc_nargs <= 2) {
2318 top = def(term->esc_args[0], 1) - 1;
2319 bot = (term->esc_nargs <= 1
2320 || term->esc_args[1] == 0 ?
2322 def(term->esc_args[1], term->rows)) - 1;
2323 if (bot >= term->rows)
2324 bot = term->rows - 1;
2325 /* VTTEST Bug 9 - if region is less than 2 lines
2326 * don't change region.
2328 if (bot - top > 0) {
2333 * I used to think the cursor should be
2334 * placed at the top of the newly marginned
2335 * area. Apparently not: VMS TPU falls over
2338 * Well actually it should for
2341 term->curs.y = (term->dec_om ?
2344 term->seen_disp_event = TRUE;
2348 case 'm': /* SGR: set graphics rendition */
2351 * A VT100 without the AVO only had one
2352 * attribute, either underline or
2353 * reverse video depending on the
2354 * cursor type, this was selected by
2358 * This is sometimes DIM, eg on the
2361 * This is sometimes INVIS various ANSI.
2363 * This like 22 disables BOLD, DIM and INVIS
2365 * The ANSI colours appear on any
2366 * terminal that has colour (obviously)
2367 * but the interaction between sgr0 and
2368 * the colours varies but is usually
2369 * related to the background colour
2370 * erase item. The interaction between
2371 * colour attributes and the mono ones
2372 * is also very implementation
2375 * The 39 and 49 attributes are likely
2376 * to be unimplemented.
2379 for (i = 0; i < term->esc_nargs; i++) {
2380 switch (def(term->esc_args[i], 0)) {
2381 case 0: /* restore defaults */
2382 term->curr_attr = term->default_attr;
2384 case 1: /* enable bold */
2385 compatibility(VT100AVO);
2386 term->curr_attr |= ATTR_BOLD;
2388 case 21: /* (enable double underline) */
2389 compatibility(OTHER);
2390 case 4: /* enable underline */
2391 compatibility(VT100AVO);
2392 term->curr_attr |= ATTR_UNDER;
2394 case 5: /* enable blink */
2395 compatibility(VT100AVO);
2396 term->curr_attr |= ATTR_BLINK;
2398 case 6: /* SCO light bkgrd */
2399 compatibility(SCOANSI);
2400 term->blink_is_real = FALSE;
2401 term->curr_attr |= ATTR_BLINK;
2403 case 7: /* enable reverse video */
2404 term->curr_attr |= ATTR_REVERSE;
2406 case 10: /* SCO acs off */
2407 compatibility(SCOANSI);
2408 if (term->cfg.no_remote_charset) break;
2409 term->sco_acs = 0; break;
2410 case 11: /* SCO acs on */
2411 compatibility(SCOANSI);
2412 if (term->cfg.no_remote_charset) break;
2413 term->sco_acs = 1; break;
2414 case 12: /* SCO acs on, |0x80 */
2415 compatibility(SCOANSI);
2416 if (term->cfg.no_remote_charset) break;
2417 term->sco_acs = 2; break;
2418 case 22: /* disable bold */
2419 compatibility2(OTHER, VT220);
2420 term->curr_attr &= ~ATTR_BOLD;
2422 case 24: /* disable underline */
2423 compatibility2(OTHER, VT220);
2424 term->curr_attr &= ~ATTR_UNDER;
2426 case 25: /* disable blink */
2427 compatibility2(OTHER, VT220);
2428 term->curr_attr &= ~ATTR_BLINK;
2430 case 27: /* disable reverse video */
2431 compatibility2(OTHER, VT220);
2432 term->curr_attr &= ~ATTR_REVERSE;
2443 term->curr_attr &= ~ATTR_FGMASK;
2445 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
2455 /* xterm-style bright foreground */
2456 term->curr_attr &= ~ATTR_FGMASK;
2458 ((term->esc_args[i] - 90 + 16)
2461 case 39: /* default-foreground */
2462 term->curr_attr &= ~ATTR_FGMASK;
2463 term->curr_attr |= ATTR_DEFFG;
2474 term->curr_attr &= ~ATTR_BGMASK;
2476 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
2486 /* xterm-style bright background */
2487 term->curr_attr &= ~ATTR_BGMASK;
2489 ((term->esc_args[i] - 100 + 16)
2492 case 49: /* default-background */
2493 term->curr_attr &= ~ATTR_BGMASK;
2494 term->curr_attr |= ATTR_DEFBG;
2499 term->erase_char = (' ' | ATTR_ASCII |
2505 case 's': /* save cursor */
2506 save_cursor(term, TRUE);
2508 case 'u': /* restore cursor */
2509 save_cursor(term, FALSE);
2510 term->seen_disp_event = TRUE;
2512 case 't': /* DECSLPP: set page size - ie window height */
2514 * VT340/VT420 sequence DECSLPP, DEC only allows values
2515 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2516 * illegal values (eg first arg 1..9) for window changing
2519 if (term->esc_nargs <= 1
2520 && (term->esc_args[0] < 1 ||
2521 term->esc_args[0] >= 24)) {
2522 compatibility(VT340TEXT);
2523 if (!term->cfg.no_remote_resize)
2524 request_resize(term->frontend, term->cols,
2525 def(term->esc_args[0], 24));
2527 } else if (term->esc_nargs >= 1 &&
2528 term->esc_args[0] >= 1 &&
2529 term->esc_args[0] < 24) {
2530 compatibility(OTHER);
2532 switch (term->esc_args[0]) {
2536 set_iconic(term->frontend, FALSE);
2539 set_iconic(term->frontend, TRUE);
2542 if (term->esc_nargs >= 3) {
2543 if (!term->cfg.no_remote_resize)
2544 move_window(term->frontend,
2545 def(term->esc_args[1], 0),
2546 def(term->esc_args[2], 0));
2550 /* We should resize the window to a given
2551 * size in pixels here, but currently our
2552 * resizing code isn't healthy enough to
2557 set_zorder(term->frontend, TRUE);
2560 /* move to bottom */
2561 set_zorder(term->frontend, FALSE);
2564 refresh_window(term->frontend);
2567 if (term->esc_nargs >= 3) {
2568 if (!term->cfg.no_remote_resize)
2569 request_resize(term->frontend,
2570 def(term->esc_args[2], term->cfg.width),
2571 def(term->esc_args[1], term->cfg.height));
2575 if (term->esc_nargs >= 2)
2576 set_zoomed(term->frontend,
2582 ldisc_send(term->ldisc,
2583 is_iconic(term->frontend) ?
2584 "\033[1t" : "\033[2t", 4, 0);
2588 get_window_pos(term->frontend, &x, &y);
2589 len = sprintf(buf, "\033[3;%d;%dt", x, y);
2590 ldisc_send(term->ldisc, buf, len, 0);
2595 get_window_pixels(term->frontend, &x, &y);
2596 len = sprintf(buf, "\033[4;%d;%dt", x, y);
2597 ldisc_send(term->ldisc, buf, len, 0);
2602 len = sprintf(buf, "\033[8;%d;%dt",
2603 term->rows, term->cols);
2604 ldisc_send(term->ldisc, buf, len, 0);
2609 * Hmmm. Strictly speaking we
2610 * should return `the size of the
2611 * screen in characters', but
2612 * that's not easy: (a) window
2613 * furniture being what it is it's
2614 * hard to compute, and (b) in
2615 * resize-font mode maximising the
2616 * window wouldn't change the
2617 * number of characters. *shrug*. I
2618 * think we'll ignore it for the
2619 * moment and see if anyone
2620 * complains, and then ask them
2621 * what they would like it to do.
2626 !term->cfg.no_remote_qtitle) {
2627 p = get_window_title(term->frontend, TRUE);
2629 ldisc_send(term->ldisc, "\033]L", 3, 0);
2630 ldisc_send(term->ldisc, p, len, 0);
2631 ldisc_send(term->ldisc, "\033\\", 2, 0);
2636 !term->cfg.no_remote_qtitle) {
2637 p = get_window_title(term->frontend,FALSE);
2639 ldisc_send(term->ldisc, "\033]l", 3, 0);
2640 ldisc_send(term->ldisc, p, len, 0);
2641 ldisc_send(term->ldisc, "\033\\", 2, 0);
2647 case 'S': /* SU: Scroll up */
2648 compatibility(SCOANSI);
2649 scroll(term, term->marg_t, term->marg_b,
2650 def(term->esc_args[0], 1), TRUE);
2652 term->wrapnext = FALSE;
2653 term->seen_disp_event = TRUE;
2655 case 'T': /* SD: Scroll down */
2656 compatibility(SCOANSI);
2657 scroll(term, term->marg_t, term->marg_b,
2658 -def(term->esc_args[0], 1), TRUE);
2660 term->wrapnext = FALSE;
2661 term->seen_disp_event = TRUE;
2663 case ANSI('|', '*'): /* DECSNLS */
2665 * Set number of lines on screen
2666 * VT420 uses VGA like hardware and can
2667 * support any size in reasonable range
2668 * (24..49 AIUI) with no default specified.
2670 compatibility(VT420);
2671 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
2672 if (!term->cfg.no_remote_resize)
2673 request_resize(term->frontend, term->cols,
2674 def(term->esc_args[0],
2679 case ANSI('|', '$'): /* DECSCPP */
2681 * Set number of columns per page
2682 * Docs imply range is only 80 or 132, but
2685 compatibility(VT340TEXT);
2686 if (term->esc_nargs <= 1) {
2687 if (!term->cfg.no_remote_resize)
2688 request_resize(term->frontend,
2689 def(term->esc_args[0],
2690 term->cfg.width), term->rows);
2694 case 'X': /* ECH: write N spaces w/o moving cursor */
2695 /* XXX VTTEST says this is vt220, vt510 manual
2697 compatibility(ANSIMIN);
2699 int n = def(term->esc_args[0], 1);
2701 unsigned long *p = term->cpos;
2702 if (n > term->cols - term->curs.x)
2703 n = term->cols - term->curs.x;
2704 cursplus = term->curs;
2706 check_boundary(term, term->curs.x, term->curs.y);
2707 check_boundary(term, term->curs.x+n, term->curs.y);
2708 check_selection(term, term->curs, cursplus);
2710 *p++ = term->erase_char;
2711 term->seen_disp_event = TRUE;
2714 case 'x': /* DECREQTPARM: report terminal characteristics */
2715 compatibility(VT100);
2718 int i = def(term->esc_args[0], 0);
2719 if (i == 0 || i == 1) {
2720 strcpy(buf, "\033[2;1;1;112;112;1;0x");
2722 ldisc_send(term->ldisc, buf, 20, 0);
2726 case 'Z': /* CBT: BackTab for xterm */
2727 compatibility(OTHER);
2729 int i = def(term->esc_args[0], 1);
2730 pos old_curs = term->curs;
2732 for(;i>0 && term->curs.x>0; i--) {
2735 } while (term->curs.x >0 &&
2736 !term->tabs[term->curs.x]);
2739 check_selection(term, old_curs, term->curs);
2742 case ANSI('c', '='): /* Hide or Show Cursor */
2743 compatibility(SCOANSI);
2744 switch(term->esc_args[0]) {
2745 case 0: /* hide cursor */
2746 term->cursor_on = FALSE;
2748 case 1: /* restore cursor */
2749 term->big_cursor = FALSE;
2750 term->cursor_on = TRUE;
2752 case 2: /* block cursor */
2753 term->big_cursor = TRUE;
2754 term->cursor_on = TRUE;
2758 case ANSI('C', '='):
2760 * set cursor start on scanline esc_args[0] and
2761 * end on scanline esc_args[1].If you set
2762 * the bottom scan line to a value less than
2763 * the top scan line, the cursor will disappear.
2765 compatibility(SCOANSI);
2766 if (term->esc_nargs >= 2) {
2767 if (term->esc_args[0] > term->esc_args[1])
2768 term->cursor_on = FALSE;
2770 term->cursor_on = TRUE;
2773 case ANSI('D', '='):
2774 compatibility(SCOANSI);
2775 term->blink_is_real = FALSE;
2776 if (term->esc_args[0]>=1)
2777 term->curr_attr |= ATTR_BLINK;
2779 term->curr_attr &= ~ATTR_BLINK;
2781 case ANSI('E', '='):
2782 compatibility(SCOANSI);
2783 term->blink_is_real = (term->esc_args[0] >= 1);
2785 case ANSI('F', '='): /* set normal foreground */
2786 compatibility(SCOANSI);
2787 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
2789 (sco2ansicolour[term->esc_args[0] & 0x7] |
2790 ((term->esc_args[0] & 0x8) << 1)) <<
2792 term->curr_attr &= ~ATTR_FGMASK;
2793 term->curr_attr |= colour;
2794 term->default_attr &= ~ATTR_FGMASK;
2795 term->default_attr |= colour;
2798 case ANSI('G', '='): /* set normal background */
2799 compatibility(SCOANSI);
2800 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
2802 (sco2ansicolour[term->esc_args[0] & 0x7] |
2803 ((term->esc_args[0] & 0x8) << 1)) <<
2805 term->curr_attr &= ~ATTR_BGMASK;
2806 term->curr_attr |= colour;
2807 term->default_attr &= ~ATTR_BGMASK;
2808 term->default_attr |= colour;
2811 case ANSI('L', '='):
2812 compatibility(SCOANSI);
2813 term->use_bce = (term->esc_args[0] <= 0);
2814 term->erase_char = ERASE_CHAR;
2816 term->erase_char = (' ' | ATTR_ASCII |
2818 (ATTR_FGMASK | ATTR_BGMASK)));
2820 case ANSI('p', '"'): /* DECSCL: set compat level */
2822 * Allow the host to make this emulator a
2823 * 'perfect' VT102. This first appeared in
2824 * the VT220, but we do need to get back to
2825 * PuTTY mode so I won't check it.
2827 * The arg in 40..42,50 are a PuTTY extension.
2828 * The 2nd arg, 8bit vs 7bit is not checked.
2830 * Setting VT102 mode should also change
2831 * the Fkeys to generate PF* codes as a
2832 * real VT102 has no Fkeys. The VT220 does
2833 * this, F11..F13 become ESC,BS,LF other
2834 * Fkeys send nothing.
2836 * Note ESC c will NOT change this!
2839 switch (term->esc_args[0]) {
2841 term->compatibility_level &= ~TM_VTXXX;
2842 term->compatibility_level |= TM_VT102;
2845 term->compatibility_level &= ~TM_VTXXX;
2846 term->compatibility_level |= TM_VT220;
2850 if (term->esc_args[0] > 60 &&
2851 term->esc_args[0] < 70)
2852 term->compatibility_level |= TM_VTXXX;
2856 term->compatibility_level &= TM_VTXXX;
2859 term->compatibility_level = TM_PUTTY;
2862 term->compatibility_level = TM_SCOANSI;
2866 term->compatibility_level = TM_PUTTY;
2872 /* Change the response to CSI c */
2873 if (term->esc_args[0] == 50) {
2876 strcpy(term->id_string, "\033[?");
2877 for (i = 1; i < term->esc_nargs; i++) {
2879 strcat(term->id_string, ";");
2880 sprintf(lbuf, "%d", term->esc_args[i]);
2881 strcat(term->id_string, lbuf);
2883 strcat(term->id_string, "c");
2886 /* Is this a good idea ?
2887 * Well we should do a soft reset at this point ...
2889 if (!has_compat(VT420) && has_compat(VT100)) {
2890 if (!term->cfg.no_remote_resize) {
2891 if (term->reset_132)
2892 request_resize(132, 24);
2894 request_resize(80, 24);
2902 term->osc_w = FALSE;
2904 case 'P': /* Linux palette sequence */
2905 term->termstate = SEEN_OSC_P;
2906 term->osc_strlen = 0;
2908 case 'R': /* Linux palette reset */
2909 palette_reset(term->frontend);
2910 term_invalidate(term);
2911 term->termstate = TOPLEVEL;
2913 case 'W': /* word-set */
2914 term->termstate = SEEN_OSC_W;
2927 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2931 * Grotty hack to support xterm and DECterm title
2932 * sequences concurrently.
2934 if (term->esc_args[0] == 2) {
2935 term->esc_args[0] = 1;
2938 /* else fall through */
2940 term->termstate = OSC_STRING;
2941 term->osc_strlen = 0;
2946 * This OSC stuff is EVIL. It takes just one character to get into
2947 * sysline mode and it's not initially obvious how to get out.
2948 * So I've added CR and LF as string aborts.
2949 * This shouldn't effect compatibility as I believe embedded
2950 * control characters are supposed to be interpreted (maybe?)
2951 * and they don't display anything useful anyway.
2955 if (c == '\012' || c == '\015') {
2956 term->termstate = TOPLEVEL;
2957 } else if (c == 0234 || c == '\007') {
2959 * These characters terminate the string; ST and BEL
2960 * terminate the sequence and trigger instant
2961 * processing of it, whereas ESC goes back to SEEN_ESC
2962 * mode unless it is followed by \, in which case it is
2963 * synonymous with ST in the first place.
2966 term->termstate = TOPLEVEL;
2967 } else if (c == '\033')
2968 term->termstate = OSC_MAYBE_ST;
2969 else if (term->osc_strlen < OSC_STR_MAX)
2970 term->osc_string[term->osc_strlen++] = c;
2974 int max = (term->osc_strlen == 0 ? 21 : 16);
2976 if (c >= '0' && c <= '9')
2978 else if (c >= 'A' && c <= 'A' + max - 10)
2980 else if (c >= 'a' && c <= 'a' + max - 10)
2983 term->termstate = TOPLEVEL;
2986 term->osc_string[term->osc_strlen++] = val;
2987 if (term->osc_strlen >= 7) {
2988 palette_set(term->frontend, term->osc_string[0],
2989 term->osc_string[1] * 16 + term->osc_string[2],
2990 term->osc_string[3] * 16 + term->osc_string[4],
2991 term->osc_string[5] * 16 + term->osc_string[6]);
2992 term_invalidate(term);
2993 term->termstate = TOPLEVEL;
3009 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
3012 term->termstate = OSC_STRING;
3013 term->osc_strlen = 0;
3017 term->termstate = TOPLEVEL;
3018 term->seen_disp_event = TRUE;
3021 move(term, term->curs.x, term->curs.y - 1, 1);
3024 move(term, term->curs.x, term->curs.y + 1, 1);
3027 move(term, term->curs.x + 1, term->curs.y, 1);
3030 move(term, term->curs.x - 1, term->curs.y, 1);
3033 * From the VT100 Manual
3034 * NOTE: The special graphics characters in the VT100
3035 * are different from those in the VT52
3037 * From VT102 manual:
3038 * 137 _ Blank - Same
3039 * 140 ` Reserved - Humm.
3040 * 141 a Solid rectangle - Similar
3041 * 142 b 1/ - Top half of fraction for the
3042 * 143 c 3/ - subscript numbers below.
3045 * 146 f Degrees - Same
3046 * 147 g Plus or minus - Same
3048 * 151 i Ellipsis (dots)
3051 * 154 l Bar at scan 0
3052 * 155 m Bar at scan 1
3053 * 156 n Bar at scan 2
3054 * 157 o Bar at scan 3 - Similar
3055 * 160 p Bar at scan 4 - Similar
3056 * 161 q Bar at scan 5 - Similar
3057 * 162 r Bar at scan 6 - Same
3058 * 163 s Bar at scan 7 - Similar
3073 term->cset_attr[term->cset = 0] = ATTR_LINEDRW;
3076 term->cset_attr[term->cset = 0] = ATTR_ASCII;
3079 move(term, 0, 0, 0);
3082 if (term->curs.y == 0)
3083 scroll(term, 0, term->rows - 1, -1, TRUE);
3084 else if (term->curs.y > 0)
3087 term->wrapnext = FALSE;
3090 erase_lots(term, FALSE, FALSE, TRUE);
3094 erase_lots(term, TRUE, FALSE, TRUE);
3098 /* XXX Print cursor line */
3101 /* XXX Start controller mode */
3104 /* XXX Stop controller mode */
3108 term->termstate = VT52_Y1;
3112 ldisc_send(term->ldisc, "\033/Z", 3, 0);
3115 term->app_keypad_keys = TRUE;
3118 term->app_keypad_keys = FALSE;
3121 /* XXX This should switch to VT100 mode not current or default
3122 * VT mode. But this will only have effect in a VT220+
3125 term->vt52_mode = FALSE;
3126 term->blink_is_real = term->cfg.blinktext;
3130 /* XXX Enter auto print mode */
3133 /* XXX Exit auto print mode */
3136 /* XXX Print screen */
3142 /* compatibility(ATARI) */
3143 move(term, 0, 0, 0);
3144 erase_lots(term, FALSE, FALSE, TRUE);
3148 /* compatibility(ATARI) */
3149 if (term->curs.y <= term->marg_b)
3150 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
3153 /* compatibility(ATARI) */
3154 if (term->curs.y <= term->marg_b)
3155 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
3158 /* compatibility(ATARI) */
3159 term->termstate = VT52_FG;
3162 /* compatibility(ATARI) */
3163 term->termstate = VT52_BG;
3166 /* compatibility(ATARI) */
3167 erase_lots(term, FALSE, TRUE, FALSE);
3171 /* compatibility(ATARI) */
3172 term->cursor_on = TRUE;
3175 /* compatibility(ATARI) */
3176 term->cursor_on = FALSE;
3178 /* case 'j': Save cursor position - broken on ST */
3179 /* case 'k': Restore cursor position */
3181 /* compatibility(ATARI) */
3182 erase_lots(term, TRUE, TRUE, TRUE);
3184 term->wrapnext = FALSE;
3188 /* compatibility(ATARI) */
3189 erase_lots(term, TRUE, TRUE, FALSE);
3192 /* compatibility(ATARI) */
3193 term->curr_attr |= ATTR_REVERSE;
3196 /* compatibility(ATARI) */
3197 term->curr_attr &= ~ATTR_REVERSE;
3199 case 'v': /* wrap Autowrap on - Wyse style */
3200 /* compatibility(ATARI) */
3203 case 'w': /* Autowrap off */
3204 /* compatibility(ATARI) */
3209 /* compatibility(OTHER) */
3210 term->vt52_bold = FALSE;
3211 term->curr_attr = ATTR_DEFAULT;
3213 term->erase_char = (' ' | ATTR_ASCII |
3215 (ATTR_FGMASK | ATTR_BGMASK)));
3218 /* compatibility(VI50) */
3219 term->curr_attr |= ATTR_UNDER;
3222 /* compatibility(VI50) */
3223 term->curr_attr &= ~ATTR_UNDER;
3226 /* compatibility(VI50) */
3227 term->vt52_bold = TRUE;
3228 term->curr_attr |= ATTR_BOLD;
3231 /* compatibility(VI50) */
3232 term->vt52_bold = FALSE;
3233 term->curr_attr &= ~ATTR_BOLD;
3239 term->termstate = VT52_Y2;
3240 move(term, term->curs.x, c - ' ', 0);
3243 term->termstate = TOPLEVEL;
3244 move(term, c - ' ', term->curs.y, 0);
3249 term->termstate = TOPLEVEL;
3250 term->curr_attr &= ~ATTR_FGMASK;
3251 term->curr_attr &= ~ATTR_BOLD;
3252 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
3253 if ((c & 0x8) || term->vt52_bold)
3254 term->curr_attr |= ATTR_BOLD;
3257 term->erase_char = (' ' | ATTR_ASCII |
3259 (ATTR_FGMASK | ATTR_BGMASK)));
3262 term->termstate = TOPLEVEL;
3263 term->curr_attr &= ~ATTR_BGMASK;
3264 term->curr_attr &= ~ATTR_BLINK;
3265 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
3267 /* Note: bold background */
3269 term->curr_attr |= ATTR_BLINK;
3272 term->erase_char = (' ' | ATTR_ASCII |
3274 (ATTR_FGMASK | ATTR_BGMASK)));
3277 default: break; /* placate gcc warning about enum use */
3279 if (term->selstate != NO_SELECTION) {
3280 pos cursplus = term->curs;
3282 check_selection(term, term->curs, cursplus);
3286 term_print_flush(term);
3291 * Compare two lines to determine whether they are sufficiently
3292 * alike to scroll-optimise one to the other. Return the degree of
3295 static int linecmp(Terminal *term, unsigned long *a, unsigned long *b)
3299 for (i = n = 0; i < term->cols; i++)
3300 n += (*a++ == *b++);
3306 * Given a context, update the window. Out of paranoia, we don't
3307 * allow WM_PAINT responses to do scrolling optimisations.
3309 static void do_paint(Terminal *term, Context ctx, int may_optimise)
3311 int i, j, our_curs_y, our_curs_x;
3312 unsigned long rv, cursor;
3315 long cursor_background = ERASE_CHAR;
3316 unsigned long ticks;
3317 #ifdef OPTIMISE_SCROLL
3318 struct scrollregion *sr;
3319 #endif /* OPTIMISE_SCROLL */
3322 * Check the visual bell state.
3324 if (term->in_vbell) {
3325 ticks = GETTICKCOUNT();
3326 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
3327 term->in_vbell = FALSE;
3330 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
3333 * screen array, disptop, scrtop,
3335 * cfg.blinkpc, blink_is_real, tblinker,
3336 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
3339 /* Has the cursor position or type changed ? */
3340 if (term->cursor_on) {
3341 if (term->has_focus) {
3342 if (term->blinker || !term->cfg.blink_cur)
3343 cursor = TATTR_ACTCURS;
3347 cursor = TATTR_PASCURS;
3349 cursor |= TATTR_RIGHTCURS;
3352 our_curs_y = term->curs.y - term->disptop;
3355 * Adjust the cursor position in the case where it's
3356 * resting on the right-hand half of a CJK wide character.
3357 * xterm's behaviour here, which seems adequate to me, is
3358 * to display the cursor covering the _whole_ character,
3359 * exactly as if it were one space to the left.
3361 unsigned long *ldata = lineptr(term->curs.y);
3362 our_curs_x = term->curs.x;
3363 if (our_curs_x > 0 &&
3364 (ldata[our_curs_x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3368 if (term->dispcurs && (term->curstype != cursor ||
3370 term->disptext + our_curs_y * (term->cols + 1) +
3372 if (term->dispcurs > term->disptext &&
3373 (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3374 term->dispcurs[-1] |= ATTR_INVALID;
3375 if ( (term->dispcurs[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3376 term->dispcurs[1] |= ATTR_INVALID;
3377 *term->dispcurs |= ATTR_INVALID;
3380 term->dispcurs = NULL;
3382 #ifdef OPTIMISE_SCROLL
3384 sr = term->scrollhead;
3386 struct scrollregion *next = sr->next;
3387 do_scroll(ctx, sr->topline, sr->botline, sr->lines);
3391 term->scrollhead = term->scrolltail = NULL;
3392 #endif /* OPTIMISE_SCROLL */
3394 /* The normal screen data */
3395 for (i = 0; i < term->rows; i++) {
3396 unsigned long *ldata;
3398 int idx, dirty_line, dirty_run, selected;
3399 unsigned long attr = 0;
3400 int updated_line = 0;
3403 int last_run_dirty = 0;
3405 scrpos.y = i + term->disptop;
3406 ldata = lineptr(scrpos.y);
3407 lattr = (ldata[term->cols] & LATTR_MODE);
3409 idx = i * (term->cols + 1);
3410 dirty_run = dirty_line = (ldata[term->cols] !=
3411 term->disptext[idx + term->cols]);
3412 term->disptext[idx + term->cols] = ldata[term->cols];
3414 for (j = 0; j < term->cols; j++, idx++) {
3415 unsigned long tattr, tchar;
3416 unsigned long *d = ldata + j;
3420 tchar = (*d & (CHAR_MASK | CSET_MASK));
3421 tattr = (*d & (ATTR_MASK ^ CSET_MASK));
3422 switch (tchar & CSET_MASK) {
3424 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
3427 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
3430 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
3433 tattr |= (tchar & CSET_MASK);
3435 if ((d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3438 /* Video reversing things */
3439 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
3440 if (term->seltype == LEXICOGRAPHIC)
3441 selected = (posle(term->selstart, scrpos) &&
3442 poslt(scrpos, term->selend));
3444 selected = (posPle(term->selstart, scrpos) &&
3445 posPlt(scrpos, term->selend));
3449 ^ (selected ? ATTR_REVERSE : 0));
3451 /* 'Real' blinking ? */
3452 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
3453 if (term->has_focus && term->tblinker) {
3454 tchar = term->ucsdata->unitab_line[(unsigned char)' '];
3456 tattr &= ~ATTR_BLINK;
3460 * Check the font we'll _probably_ be using to see if
3461 * the character is wide when we don't want it to be.
3463 if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) {
3464 if ((tattr & ATTR_WIDE) == 0 &&
3465 char_width(ctx, (tchar | tattr) & 0xFFFF) == 2)
3466 tattr |= ATTR_NARROW;
3467 } else if (term->disptext[idx]&ATTR_NARROW)
3468 tattr |= ATTR_NARROW;
3470 /* Cursor here ? Save the 'background' */
3471 if (i == our_curs_y && j == our_curs_x) {
3472 cursor_background = tattr | tchar;
3473 term->dispcurs = term->disptext + idx;
3476 if ((term->disptext[idx] ^ tattr) & ATTR_WIDE)
3479 break_run = (((tattr ^ attr) & term->attr_mask) ||
3480 j - start >= sizeof(ch));
3482 /* Special hack for VT100 Linedraw glyphs */
3483 if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA
3484 && tchar <= 0xBD) break_run = TRUE;
3486 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
3487 if ((tchar | tattr) == term->disptext[idx])
3489 else if (!dirty_run && ccount == 1)
3494 if ((dirty_run || last_run_dirty) && ccount > 0) {
3495 do_text(ctx, start, i, ch, ccount, attr, lattr);
3501 if (term->ucsdata->dbcs_screenfont)
3502 last_run_dirty = dirty_run;
3503 dirty_run = dirty_line;
3506 if ((tchar | tattr) != term->disptext[idx])
3508 ch[ccount++] = (char) tchar;
3509 term->disptext[idx] = tchar | tattr;
3511 /* If it's a wide char step along to the next one. */
3512 if (tattr & ATTR_WIDE) {
3513 if (++j < term->cols) {
3517 * By construction above, the cursor should not
3518 * be on the right-hand half of this character.
3521 assert(!(i == our_curs_y && j == our_curs_x));
3522 if (term->disptext[idx] != *d)
3524 term->disptext[idx] = *d;
3528 if (dirty_run && ccount > 0) {
3529 do_text(ctx, start, i, ch, ccount, attr, lattr);
3533 /* Cursor on this line ? (and changed) */
3534 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
3535 ch[0] = (char) (cursor_background & CHAR_MASK);
3536 attr = (cursor_background & ATTR_MASK) | cursor;
3537 do_cursor(ctx, our_curs_x, i, ch, 1, attr, lattr);
3538 term->curstype = cursor;
3544 * Flick the switch that says if blinking things should be shown or hidden.
3547 void term_blink(Terminal *term, int flg)
3549 long now, blink_diff;
3551 now = GETTICKCOUNT();
3552 blink_diff = now - term->last_tblink;
3554 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3555 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
3556 term->last_tblink = now;
3557 term->tblinker = !term->tblinker;
3562 term->last_blink = now;
3566 blink_diff = now - term->last_blink;
3568 /* Make sure the cursor blinks no faster than system blink rate */
3569 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
3572 term->last_blink = now;
3573 term->blinker = !term->blinker;
3577 * Invalidate the whole screen so it will be repainted in full.
3579 void term_invalidate(Terminal *term)
3583 for (i = 0; i < term->rows * (term->cols + 1); i++)
3584 term->disptext[i] = ATTR_INVALID;
3588 * Paint the window in response to a WM_PAINT message.
3590 void term_paint(Terminal *term, Context ctx,
3591 int left, int top, int right, int bottom, int immediately)
3594 if (left < 0) left = 0;
3595 if (top < 0) top = 0;
3596 if (right >= term->cols) right = term->cols-1;
3597 if (bottom >= term->rows) bottom = term->rows-1;
3599 for (i = top; i <= bottom && i < term->rows; i++) {
3600 if ((term->disptext[i * (term->cols + 1) + term->cols] &
3601 LATTR_MODE) == LATTR_NORM)
3602 for (j = left; j <= right && j < term->cols; j++)
3603 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3605 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
3606 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3609 /* This should happen soon enough, also for some reason it sometimes
3610 * fails to actually do anything when re-sizing ... painting the wrong
3614 do_paint (term, ctx, FALSE);
3618 * Attempt to scroll the scrollback. The second parameter gives the
3619 * position we want to scroll to; the first is +1 to denote that
3620 * this position is relative to the beginning of the scrollback, -1
3621 * to denote it is relative to the end, and 0 to denote that it is
3622 * relative to the current position.
3624 void term_scroll(Terminal *term, int rel, int where)
3626 int sbtop = -sblines(term);
3627 #ifdef OPTIMISE_SCROLL
3628 int olddisptop = term->disptop;
3630 #endif /* OPTIMISE_SCROLL */
3632 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
3633 if (term->disptop < sbtop)
3634 term->disptop = sbtop;
3635 if (term->disptop > 0)
3638 #ifdef OPTIMISE_SCROLL
3639 shift = (term->disptop - olddisptop);
3640 if (shift < term->rows && shift > -term->rows)
3641 scroll_display(term, 0, term->rows - 1, shift);
3642 #endif /* OPTIMISE_SCROLL */
3646 static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel)
3649 wchar_t *wbptr; /* where next char goes within workbuf */
3651 int wblen = 0; /* workbuf len */
3652 int buflen; /* amount of memory allocated to workbuf */
3654 buflen = 5120; /* Default size */
3655 workbuf = snewn(buflen, wchar_t);
3656 wbptr = workbuf; /* start filling here */
3657 old_top_x = top.x; /* needed for rect==1 */
3659 while (poslt(top, bottom)) {
3661 unsigned long *ldata = lineptr(top.y);
3665 * nlpos will point at the maximum position on this line we
3666 * should copy up to. So we start it at the end of the
3670 nlpos.x = term->cols;
3673 * ... move it backwards if there's unused space at the end
3674 * of the line (and also set `nl' if this is the case,
3675 * because in normal selection mode this means we need a
3676 * newline at the end)...
3678 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3679 while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 ||
3680 (DIRECT_CHAR(ldata[nlpos.x - 1]) &&
3681 (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20))
3682 && poslt(top, nlpos))
3684 if (poslt(nlpos, bottom))
3686 } else if (ldata[term->cols] & LATTR_WRAPPED2) {
3687 /* Ignore the last char on the line in a WRAPPED2 line. */
3692 * ... and then clip it to the terminal x coordinate if
3693 * we're doing rectangular selection. (In this case we
3694 * still did the above, so that copying e.g. the right-hand
3695 * column from a table doesn't fill with spaces on the
3699 if (nlpos.x > bottom.x)
3701 nl = (top.y < bottom.y);
3704 while (poslt(top, bottom) && poslt(top, nlpos)) {
3707 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
3709 wchar_t cbuf[16], *p;
3710 int uc = (ldata[top.x] & 0xFFFF);
3713 if (uc == UCSWIDE) {
3718 switch (uc & CSET_MASK) {
3720 if (!term->cfg.rawcnp) {
3721 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3725 uc = term->ucsdata->unitab_line[uc & 0xFF];
3728 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3731 switch (uc & CSET_MASK) {
3733 uc = term->ucsdata->unitab_font[uc & 0xFF];
3736 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3740 set = (uc & CSET_MASK);
3741 c = (uc & CHAR_MASK);
3745 if (DIRECT_FONT(uc)) {
3746 if (c >= ' ' && c != 0x7F) {
3750 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
3752 buf[1] = (char) (0xFF & ldata[top.x + 1]);
3753 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
3757 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
3761 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
3768 for (p = cbuf; *p; p++) {
3769 /* Enough overhead for trailing NL and nul */
3770 if (wblen >= buflen - 16) {
3772 workbuf = sresize(workbuf, buflen, wchar_t);
3773 wbptr = workbuf + wblen;
3782 for (i = 0; i < sel_nl_sz; i++) {
3784 *wbptr++ = sel_nl[i];
3788 top.x = rect ? old_top_x : 0;
3790 #if SELECTION_NUL_TERMINATED
3794 write_clip(term->frontend, workbuf, wblen, desel); /* transfer to clipbd */
3795 if (buflen > 0) /* indicates we allocated this buffer */
3799 void term_copyall(Terminal *term)
3802 top.y = -sblines(term);
3804 clipme(term, top, term->curs, 0, TRUE);
3808 * The wordness array is mainly for deciding the disposition of the
3809 * US-ASCII characters.
3811 static int wordtype(Terminal *term, int uc)
3814 int start, end, ctype;
3816 static const struct ucsword ucs_words[] = {
3822 0x037e, 0x037e, 1}, /* Greek question mark */
3824 0x0387, 0x0387, 1}, /* Greek ano teleia */
3826 0x055a, 0x055f, 1}, /* Armenian punctuation */
3828 0x0589, 0x0589, 1}, /* Armenian full stop */
3830 0x0700, 0x070d, 1}, /* Syriac punctuation */
3832 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3834 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3836 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3838 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3840 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3842 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3844 0x2000, 0x200a, 0}, /* Various spaces */
3846 0x2070, 0x207f, 2}, /* superscript */
3848 0x2080, 0x208f, 2}, /* subscript */
3850 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3852 0x3000, 0x3000, 0}, /* ideographic space */
3854 0x3001, 0x3020, 1}, /* ideographic punctuation */
3856 0x303f, 0x309f, 3}, /* Hiragana */
3858 0x30a0, 0x30ff, 3}, /* Katakana */
3860 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3862 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3864 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3866 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3868 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3870 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3872 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3874 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3876 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3880 const struct ucsword *wptr;
3882 uc &= (CSET_MASK | CHAR_MASK);
3884 switch (uc & CSET_MASK) {
3886 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3889 uc = term->ucsdata->unitab_line[uc & 0xFF];
3892 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3895 switch (uc & CSET_MASK) {
3897 uc = term->ucsdata->unitab_font[uc & 0xFF];
3900 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3904 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3905 * fail as there's such a thing as a double width space. :-(
3907 if (term->ucsdata->dbcs_screenfont &&
3908 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
3912 return term->wordness[uc];
3914 for (wptr = ucs_words; wptr->start; wptr++) {
3915 if (uc >= wptr->start && uc <= wptr->end)
3923 * Spread the selection outwards according to the selection mode.
3925 static pos sel_spread_half(Terminal *term, pos p, int dir)
3927 unsigned long *ldata;
3929 int topy = -sblines(term);
3931 ldata = lineptr(p.y);
3933 switch (term->selmode) {
3936 * In this mode, every character is a separate unit, except
3937 * for runs of spaces at the end of a non-wrapping line.
3939 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3940 unsigned long *q = ldata + term->cols;
3941 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
3943 if (q == ldata + term->cols)
3945 if (p.x >= q - ldata)
3946 p.x = (dir == -1 ? q - ldata : term->cols - 1);
3951 * In this mode, the units are maximal runs of characters
3952 * whose `wordness' has the same value.
3954 wvalue = wordtype(term, UCSGET(ldata, p.x));
3957 int maxcols = (ldata[term->cols] & LATTR_WRAPPED2 ?
3958 term->cols-1 : term->cols);
3959 if (p.x < maxcols-1) {
3960 if (wordtype(term, UCSGET(ldata, p.x + 1)) == wvalue)
3965 if (ldata[term->cols] & LATTR_WRAPPED) {
3966 unsigned long *ldata2;
3967 ldata2 = lineptr(p.y+1);
3968 if (wordtype(term, UCSGET(ldata2, 0)) == wvalue) {
3981 if (wordtype(term, UCSGET(ldata, p.x - 1)) == wvalue)
3986 unsigned long *ldata2;
3990 ldata2 = lineptr(p.y-1);
3991 maxcols = (ldata2[term->cols] & LATTR_WRAPPED2 ?
3992 term->cols-1 : term->cols);
3993 if (ldata2[term->cols] & LATTR_WRAPPED) {
3994 if (wordtype(term, UCSGET(ldata2, maxcols-1))
4009 * In this mode, every line is a unit.
4011 p.x = (dir == -1 ? 0 : term->cols - 1);
4017 static void sel_spread(Terminal *term)
4019 if (term->seltype == LEXICOGRAPHIC) {
4020 term->selstart = sel_spread_half(term, term->selstart, -1);
4021 decpos(term->selend);
4022 term->selend = sel_spread_half(term, term->selend, +1);
4023 incpos(term->selend);
4027 void term_do_paste(Terminal *term)
4032 get_clip(term->frontend, &data, &len);
4033 if (data && len > 0) {
4036 term_seen_key_event(term); /* pasted data counts */
4038 if (term->paste_buffer)
4039 sfree(term->paste_buffer);
4040 term->paste_pos = term->paste_hold = term->paste_len = 0;
4041 term->paste_buffer = snewn(len, wchar_t);
4044 while (p < data + len) {
4045 while (p < data + len &&
4046 !(p <= data + len - sel_nl_sz &&
4047 !memcmp(p, sel_nl, sizeof(sel_nl))))
4052 for (i = 0; i < p - q; i++) {
4053 term->paste_buffer[term->paste_len++] = q[i];
4057 if (p <= data + len - sel_nl_sz &&
4058 !memcmp(p, sel_nl, sizeof(sel_nl))) {
4059 term->paste_buffer[term->paste_len++] = '\015';
4065 /* Assume a small paste will be OK in one go. */
4066 if (term->paste_len < 256) {
4068 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
4069 if (term->paste_buffer)
4070 sfree(term->paste_buffer);
4071 term->paste_buffer = 0;
4072 term->paste_pos = term->paste_hold = term->paste_len = 0;
4075 get_clip(term->frontend, NULL, NULL);
4078 void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,
4079 Mouse_Action a, int x, int y, int shift, int ctrl, int alt)
4082 unsigned long *ldata;
4083 int raw_mouse = (term->xterm_mouse &&
4084 !term->cfg.no_mouse_rep &&
4085 !(term->cfg.mouse_override && shift));
4086 int default_seltype;
4090 if (a == MA_DRAG && !raw_mouse)
4091 term_scroll(term, 0, -1);
4093 if (y >= term->rows) {
4095 if (a == MA_DRAG && !raw_mouse)
4096 term_scroll(term, 0, +1);
4105 if (x >= term->cols)
4108 selpoint.y = y + term->disptop;
4110 ldata = lineptr(selpoint.y);
4111 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM)
4115 int encstate = 0, r, c;
4122 encstate = 0x20; /* left button down */
4133 case MBT_WHEEL_DOWN:
4136 default: break; /* placate gcc warning about enum use */
4140 if (term->xterm_mouse == 1)
4146 term->mouse_is_down = 0;
4149 if (term->mouse_is_down == braw)
4151 term->mouse_is_down = braw;
4153 default: break; /* placate gcc warning about enum use */
4162 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
4163 ldisc_send(term->ldisc, abuf, 6, 0);
4169 * Set the selection type (rectangular or normal) at the start
4170 * of a selection attempt, from the state of Alt.
4172 if (!alt ^ !term->cfg.rect_select)
4173 default_seltype = RECTANGULAR;
4175 default_seltype = LEXICOGRAPHIC;
4177 if (term->selstate == NO_SELECTION) {
4178 term->seltype = default_seltype;
4181 if (bcooked == MBT_SELECT && a == MA_CLICK) {
4183 term->selstate = ABOUT_TO;
4184 term->seltype = default_seltype;
4185 term->selanchor = selpoint;
4186 term->selmode = SM_CHAR;
4187 } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
4189 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
4190 term->selstate = DRAGGING;
4191 term->selstart = term->selanchor = selpoint;
4192 term->selend = term->selstart;
4193 incpos(term->selend);
4195 } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||
4196 (bcooked == MBT_EXTEND && a != MA_RELEASE)) {
4197 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
4199 if (bcooked == MBT_EXTEND && a != MA_DRAG &&
4200 term->selstate == SELECTED) {
4201 if (term->seltype == LEXICOGRAPHIC) {
4203 * For normal selection, we extend by moving
4204 * whichever end of the current selection is closer
4207 if (posdiff(selpoint, term->selstart) <
4208 posdiff(term->selend, term->selstart) / 2) {
4209 term->selanchor = term->selend;
4210 decpos(term->selanchor);
4212 term->selanchor = term->selstart;
4216 * For rectangular selection, we have a choice of
4217 * _four_ places to put selanchor and selpoint: the
4218 * four corners of the selection.
4220 if (2*selpoint.x < term->selstart.x + term->selend.x)
4221 term->selanchor.x = term->selend.x-1;
4223 term->selanchor.x = term->selstart.x;
4225 if (2*selpoint.y < term->selstart.y + term->selend.y)
4226 term->selanchor.y = term->selend.y;
4228 term->selanchor.y = term->selstart.y;
4230 term->selstate = DRAGGING;
4232 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
4233 term->selanchor = selpoint;
4234 term->selstate = DRAGGING;
4235 if (term->seltype == LEXICOGRAPHIC) {
4237 * For normal selection, we set (selstart,selend) to
4238 * (selpoint,selanchor) in some order.
4240 if (poslt(selpoint, term->selanchor)) {
4241 term->selstart = selpoint;
4242 term->selend = term->selanchor;
4243 incpos(term->selend);
4245 term->selstart = term->selanchor;
4246 term->selend = selpoint;
4247 incpos(term->selend);
4251 * For rectangular selection, we may need to
4252 * interchange x and y coordinates (if the user has
4253 * dragged in the -x and +y directions, or vice versa).
4255 term->selstart.x = min(term->selanchor.x, selpoint.x);
4256 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
4257 term->selstart.y = min(term->selanchor.y, selpoint.y);
4258 term->selend.y = max(term->selanchor.y, selpoint.y);
4261 } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&
4263 if (term->selstate == DRAGGING) {
4265 * We've completed a selection. We now transfer the
4266 * data to the clipboard.
4268 clipme(term, term->selstart, term->selend,
4269 (term->seltype == RECTANGULAR), FALSE);
4270 term->selstate = SELECTED;
4272 term->selstate = NO_SELECTION;
4273 } else if (bcooked == MBT_PASTE
4275 #if MULTICLICK_ONLY_EVENT
4276 || a == MA_2CLK || a == MA_3CLK
4279 request_paste(term->frontend);
4285 void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen,
4286 unsigned int modifiers, unsigned int flags)
4290 int prependesc = FALSE;
4294 fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen);
4295 for (i = 0; i < tlen; i++)
4296 fprintf(stderr, " %04x", (unsigned)text[i]);
4297 fprintf(stderr, "\n");
4301 if ((flags & PKF_REPEAT) && term->repeat_off)
4304 /* Currently, Meta always just prefixes everything with ESC. */
4305 if (modifiers & PKM_META)
4307 modifiers &= ~PKM_META;
4310 * Alt is only used for Alt+keypad, which isn't supported yet, so
4313 modifiers &= ~PKM_ALT;
4315 /* Standard local function keys */
4316 switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) {
4318 if (keysym == PK_PAGEUP)
4319 /* scroll up one page */;
4320 if (keysym == PK_PAGEDOWN)
4321 /* scroll down on page */;
4322 if (keysym == PK_INSERT)
4323 term_do_paste(term);
4326 if (keysym == PK_PAGEUP)
4327 /* scroll up one line */;
4328 if (keysym == PK_PAGEDOWN)
4329 /* scroll down one line */;
4330 /* Control-Numlock for app-keypad mode switch */
4331 if (keysym == PK_PF1)
4332 term->app_keypad_keys ^= 1;
4336 if (modifiers & PKM_ALT) {
4337 /* Alt+F4 (close) */
4338 /* Alt+Return (full screen) */
4339 /* Alt+Space (system menu) */
4342 if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 &&
4343 text[0] >= 0x20 && text[0] <= 0x7e) {
4344 /* ASCII chars + Control */
4345 if ((text[0] >= 0x40 && text[0] <= 0x5f) ||
4346 (text[0] >= 0x61 && text[0] <= 0x7a))
4350 * Control-2 should return ^@ (0x00), Control-6 should return
4351 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
4352 * the DOS keyboard handling did it, and we have nothing better
4353 * to do with the key combo in question, we'll also map
4354 * Control-Backquote to ^\ (0x1C).
4357 case ' ': text[0] = 0x00; break;
4358 case '-': text[0] = 0x1f; break;
4359 case '/': text[0] = 0x1f; break;
4360 case '2': text[0] = 0x00; break;
4361 case '3': text[0] = 0x1b; break;
4362 case '4': text[0] = 0x1c; break;
4363 case '5': text[0] = 0x1d; break;
4364 case '6': text[0] = 0x1e; break;
4365 case '7': text[0] = 0x1f; break;
4366 case '8': text[0] = 0x7f; break;
4367 case '`': text[0] = 0x1c; break;
4372 /* Nethack keypad */
4373 if (term->cfg.nethack_keypad) {
4376 case PK_KP1: c = 'b'; break;
4377 case PK_KP2: c = 'j'; break;
4378 case PK_KP3: c = 'n'; break;
4379 case PK_KP4: c = 'h'; break;
4380 case PK_KP5: c = '.'; break;
4381 case PK_KP6: c = 'l'; break;
4382 case PK_KP7: c = 'y'; break;
4383 case PK_KP8: c = 'k'; break;
4384 case PK_KP9: c = 'u'; break;
4385 default: break; /* else gcc warns `enum value not used' */
4389 if (modifiers & PKM_CONTROL)
4391 else if (modifiers & PKM_SHIFT)
4399 /* Numeric Keypad */
4400 if (PK_ISKEYPAD(keysym)) {
4404 * In VT400 mode, PFn always emits an escape sequence. In
4405 * Linux and tilde modes, this only happens in app keypad mode.
4407 if (term->cfg.funky_type == FUNKY_VT400 ||
4408 ((term->cfg.funky_type == FUNKY_LINUX ||
4409 term->cfg.funky_type == FUNKY_TILDE) &&
4410 term->app_keypad_keys && !term->cfg.no_applic_k)) {
4412 case PK_PF1: xkey = 'P'; break;
4413 case PK_PF2: xkey = 'Q'; break;
4414 case PK_PF3: xkey = 'R'; break;
4415 case PK_PF4: xkey = 'S'; break;
4416 default: break; /* else gcc warns `enum value not used' */
4419 if (term->app_keypad_keys && !term->cfg.no_applic_k) {
4421 case PK_KP0: xkey = 'p'; break;
4422 case PK_KP1: xkey = 'q'; break;
4423 case PK_KP2: xkey = 'r'; break;
4424 case PK_KP3: xkey = 's'; break;
4425 case PK_KP4: xkey = 't'; break;
4426 case PK_KP5: xkey = 'u'; break;
4427 case PK_KP6: xkey = 'v'; break;
4428 case PK_KP7: xkey = 'w'; break;
4429 case PK_KP8: xkey = 'x'; break;
4430 case PK_KP9: xkey = 'y'; break;
4431 case PK_KPDECIMAL: xkey = 'n'; break;
4432 case PK_KPENTER: xkey = 'M'; break;
4433 default: break; /* else gcc warns `enum value not used' */
4435 if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) {
4437 * xterm can't see the layout of the keypad, so it has
4438 * to rely on the X keysyms returned by the keys.
4439 * Hence, we look at the strings here, not the PuTTY
4440 * keysyms (which describe the layout).
4444 if (modifiers & PKM_SHIFT)
4449 case '/': xkey = 'o'; break;
4450 case '*': xkey = 'j'; break;
4451 case '-': xkey = 'm'; break;
4455 * In all other modes, we try to retain the layout of
4456 * the DEC keypad in application mode.
4460 /* This key covers the '-' and ',' keys on a VT220 */
4461 if (modifiers & PKM_SHIFT)
4462 xkey = 'm'; /* VT220 '-' */
4464 xkey = 'l'; /* VT220 ',' */
4466 case PK_KPMINUS: xkey = 'm'; break;
4467 case PK_KPCOMMA: xkey = 'l'; break;
4468 default: break; /* else gcc warns `enum value not used' */
4473 if (term->vt52_mode) {
4474 if (xkey >= 'P' && xkey <= 'S')
4475 p += sprintf((char *) p, "\x1B%c", xkey);
4477 p += sprintf((char *) p, "\x1B?%c", xkey);
4479 p += sprintf((char *) p, "\x1BO%c", xkey);
4482 /* Not in application mode -- treat the number pad as arrow keys? */
4483 if ((flags & PKF_NUMLOCK) == 0) {
4485 case PK_KP0: keysym = PK_INSERT; break;
4486 case PK_KP1: keysym = PK_END; break;
4487 case PK_KP2: keysym = PK_DOWN; break;
4488 case PK_KP3: keysym = PK_PAGEDOWN; break;
4489 case PK_KP4: keysym = PK_LEFT; break;
4490 case PK_KP5: keysym = PK_REST; break;
4491 case PK_KP6: keysym = PK_RIGHT; break;
4492 case PK_KP7: keysym = PK_HOME; break;
4493 case PK_KP8: keysym = PK_UP; break;
4494 case PK_KP9: keysym = PK_PAGEUP; break;
4495 default: break; /* else gcc warns `enum value not used' */
4500 /* Miscellaneous keys */
4507 *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08);
4508 else if (modifiers == PKM_SHIFT)
4509 /* We do the opposite of what is configured */
4510 *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F);
4516 else if (modifiers == PKM_SHIFT)
4517 *p++ = 0x1B, *p++ = '[', *p++ = 'Z';
4520 /* XXX window.c has ctrl+shift+space sending 0xa0 */
4522 if (modifiers == PKM_CONTROL)
4527 case PK_KPENTER: /* Odd keypad modes handled above */
4528 if (modifiers == 0) {
4530 if (term->cr_lf_return)
4534 default: break; /* else gcc warns `enum value not used' */
4537 /* SCO function keys and editing keys */
4538 if (term->cfg.funky_type == FUNKY_SCO) {
4539 if (PK_ISFKEY(keysym) && keysym <= PK_F12) {
4540 static char const codes[] =
4541 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
4542 int index = keysym - PK_F1;
4544 if (modifiers & PKM_SHIFT) index += 12;
4545 if (modifiers & PKM_CONTROL) index += 24;
4546 p += sprintf((char *) p, "\x1B[%c", codes[index]);
4549 if (PK_ISEDITING(keysym)) {
4553 case PK_DELETE: *p++ = 0x7f; goto done;
4554 case PK_HOME: xkey = 'H'; break;
4555 case PK_INSERT: xkey = 'L'; break;
4556 case PK_END: xkey = 'F'; break;
4557 case PK_PAGEUP: xkey = 'I'; break;
4558 case PK_PAGEDOWN: xkey = 'G'; break;
4559 default: break; /* else gcc warns `enum value not used' */
4561 p += sprintf((char *) p, "\x1B[%c", xkey);
4565 if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) {
4568 if (term->cfg.funky_type == FUNKY_XTERM) {
4569 /* Xterm shuffles these keys, apparently. */
4571 case PK_HOME: keysym = PK_INSERT; break;
4572 case PK_INSERT: keysym = PK_HOME; break;
4573 case PK_DELETE: keysym = PK_END; break;
4574 case PK_END: keysym = PK_PAGEUP; break;
4575 case PK_PAGEUP: keysym = PK_DELETE; break;
4576 case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break;
4577 default: break; /* else gcc warns `enum value not used' */
4582 if (term->cfg.rxvt_homeend &&
4583 (keysym == PK_HOME || keysym == PK_END)) {
4584 p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw");
4588 if (term->vt52_mode) {
4592 * A real VT52 doesn't have these, and a VT220 doesn't
4593 * send anything for them in VT52 mode.
4596 case PK_HOME: xkey = 'H'; break;
4597 case PK_INSERT: xkey = 'L'; break;
4598 case PK_DELETE: xkey = 'M'; break;
4599 case PK_END: xkey = 'E'; break;
4600 case PK_PAGEUP: xkey = 'I'; break;
4601 case PK_PAGEDOWN: xkey = 'G'; break;
4602 default: xkey=0; break; /* else gcc warns `enum value not used'*/
4604 p += sprintf((char *) p, "\x1B%c", xkey);
4609 case PK_HOME: code = 1; break;
4610 case PK_INSERT: code = 2; break;
4611 case PK_DELETE: code = 3; break;
4612 case PK_END: code = 4; break;
4613 case PK_PAGEUP: code = 5; break;
4614 case PK_PAGEDOWN: code = 6; break;
4615 default: code = 0; break; /* else gcc warns `enum value not used' */
4617 p += sprintf((char *) p, "\x1B[%d~", code);
4621 if (PK_ISFKEY(keysym)) {
4622 /* Map Shift+F1-F10 to F11-F20 */
4623 if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT))
4625 if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) &&
4627 /* XXX This overrides the XTERM/VT52 mode below */
4629 if (keysym >= PK_F6) offt++;
4630 if (keysym >= PK_F12) offt++;
4631 p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c",
4632 'P' + keysym - PK_F1 - offt);
4635 if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) {
4636 p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1);
4639 if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) {
4640 if (term->vt52_mode)
4641 p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1);
4643 p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1);
4646 p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1);
4650 if (PK_ISCURSOR(keysym)) {
4654 case PK_UP: xkey = 'A'; break;
4655 case PK_DOWN: xkey = 'B'; break;
4656 case PK_RIGHT: xkey = 'C'; break;
4657 case PK_LEFT: xkey = 'D'; break;
4658 case PK_REST: xkey = 'G'; break; /* centre key on number pad */
4659 default: xkey = 0; break; /* else gcc warns `enum value not used' */
4661 if (term->vt52_mode)
4662 p += sprintf((char *) p, "\x1B%c", xkey);
4664 int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c);
4666 /* Useful mapping of Ctrl-arrows */
4667 if (modifiers == PKM_CONTROL)
4671 p += sprintf((char *) p, "\x1BO%c", xkey);
4673 p += sprintf((char *) p, "\x1B[%c", xkey);
4679 if (p > output || tlen > 0) {
4681 * Interrupt an ongoing paste. I'm not sure
4682 * this is sensible, but for the moment it's
4683 * preferable to having to faff about buffering
4689 * We need not bother about stdin backlogs
4690 * here, because in GUI PuTTY we can't do
4691 * anything about it anyway; there's no means
4692 * of asking Windows to hold off on KEYDOWN
4693 * messages. We _have_ to buffer everything
4696 term_seen_key_event(term);
4700 fprintf(stderr, "sending ESC\n");
4702 ldisc_send(term->ldisc, "\x1b", 1, 1);
4707 fprintf(stderr, "sending %d bytes:", p - output);
4708 for (i = 0; i < p - output; i++)
4709 fprintf(stderr, " %02x", output[i]);
4710 fprintf(stderr, "\n");
4712 ldisc_send(term->ldisc, output, p - output, 1);
4713 } else if (tlen > 0) {
4715 fprintf(stderr, "sending %d unichars:", tlen);
4716 for (i = 0; i < tlen; i++)
4717 fprintf(stderr, " %04x", (unsigned) text[i]);
4718 fprintf(stderr, "\n");
4720 luni_send(term->ldisc, text, tlen, 1);
4725 void term_nopaste(Terminal *term)
4727 if (term->paste_len == 0)
4729 sfree(term->paste_buffer);
4730 term->paste_buffer = NULL;
4731 term->paste_len = 0;
4734 int term_paste_pending(Terminal *term)
4736 return term->paste_len != 0;
4739 void term_paste(Terminal *term)
4741 long now, paste_diff;
4743 if (term->paste_len == 0)
4746 /* Don't wait forever to paste */
4747 if (term->paste_hold) {
4748 now = GETTICKCOUNT();
4749 paste_diff = now - term->last_paste;
4750 if (paste_diff >= 0 && paste_diff < 450)
4753 term->paste_hold = 0;
4755 while (term->paste_pos < term->paste_len) {
4757 while (n + term->paste_pos < term->paste_len) {
4758 if (term->paste_buffer[term->paste_pos + n++] == '\015')
4762 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
4763 term->paste_pos += n;
4765 if (term->paste_pos < term->paste_len) {
4766 term->paste_hold = 1;
4770 sfree(term->paste_buffer);
4771 term->paste_buffer = NULL;
4772 term->paste_len = 0;
4775 static void deselect(Terminal *term)
4777 term->selstate = NO_SELECTION;
4778 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
4781 void term_deselect(Terminal *term)
4787 int term_ldisc(Terminal *term, int option)
4789 if (option == LD_ECHO)
4790 return term->term_echoing;
4791 if (option == LD_EDIT)
4792 return term->term_editing;
4796 int term_data(Terminal *term, int is_stderr, const char *data, int len)
4798 bufchain_add(&term->inbuf, data, len);
4800 if (!term->in_term_out) {
4801 term->in_term_out = TRUE;
4802 term_blink(term, 1);
4804 term->in_term_out = FALSE;
4808 * term_out() always completely empties inbuf. Therefore,
4809 * there's no reason at all to return anything other than zero
4810 * from this function, because there _can't_ be a question of
4811 * the remote side needing to wait until term_out() has cleared
4814 * This is a slightly suboptimal way to deal with SSH2 - in
4815 * principle, the window mechanism would allow us to continue
4816 * to accept data on forwarded ports and X connections even
4817 * while the terminal processing was going slowly - but we
4818 * can't do the 100% right thing without moving the terminal
4819 * processing into a separate thread, and that might hurt
4820 * portability. So we manage stdout buffering the old SSH1 way:
4821 * if the terminal processing goes slowly, the whole SSH
4822 * connection stops accepting data until it's ready.
4824 * In practice, I can't imagine this causing serious trouble.
4829 void term_provide_logctx(Terminal *term, void *logctx)
4831 term->logctx = logctx;