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 check_boundary(term, term->curs.x, term->curs.y);
1604 check_boundary(term, term->curs.x+1, term->curs.y);
1605 *term->cpos = (' ' | term->curr_attr | ATTR_ASCII);
1608 /* Or normal C0 controls. */
1609 if ((c & -32) == 0 && term->termstate < DO_CTRLS) {
1611 case '\005': /* ENQ: terminal type query */
1612 /* Strictly speaking this is VT100 but a VT100 defaults to
1613 * no response. Other terminals respond at their option.
1615 * Don't put a CR in the default string as this tends to
1616 * upset some weird software.
1618 * An xterm returns "xterm" (5 characters)
1620 compatibility(ANSIMIN);
1622 char abuf[256], *s, *d;
1624 for (s = term->cfg.answerback, d = abuf; *s; s++) {
1626 if (*s >= 'a' && *s <= 'z')
1627 *d++ = (*s - ('a' - 1));
1628 else if ((*s >= '@' && *s <= '_') ||
1629 *s == '?' || (*s & 0x80))
1634 } else if (*s == '^') {
1639 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
1643 case '\007': /* BEL: Bell */
1645 struct beeptime *newbeep;
1646 unsigned long ticks;
1648 ticks = GETTICKCOUNT();
1650 if (!term->beep_overloaded) {
1651 newbeep = snew(struct beeptime);
1652 newbeep->ticks = ticks;
1653 newbeep->next = NULL;
1654 if (!term->beephead)
1655 term->beephead = newbeep;
1657 term->beeptail->next = newbeep;
1658 term->beeptail = newbeep;
1663 * Throw out any beeps that happened more than
1666 while (term->beephead &&
1667 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
1668 struct beeptime *tmp = term->beephead;
1669 term->beephead = tmp->next;
1671 if (!term->beephead)
1672 term->beeptail = NULL;
1676 if (term->cfg.bellovl && term->beep_overloaded &&
1677 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
1679 * If we're currently overloaded and the
1680 * last beep was more than s seconds ago,
1681 * leave overload mode.
1683 term->beep_overloaded = FALSE;
1684 } else if (term->cfg.bellovl && !term->beep_overloaded &&
1685 term->nbeeps >= term->cfg.bellovl_n) {
1687 * Now, if we have n or more beeps
1688 * remaining in the queue, go into overload
1691 term->beep_overloaded = TRUE;
1693 term->lastbeep = ticks;
1696 * Perform an actual beep if we're not overloaded.
1698 if (!term->cfg.bellovl || !term->beep_overloaded) {
1699 beep(term->frontend, term->cfg.beep);
1700 if (term->cfg.beep == BELL_VISUAL) {
1701 term->in_vbell = TRUE;
1702 term->vbell_startpoint = ticks;
1706 term->seen_disp_event = TRUE;
1709 case '\b': /* BS: Back space */
1710 if (term->curs.x == 0 &&
1711 (term->curs.y == 0 || term->wrap == 0))
1713 else if (term->curs.x == 0 && term->curs.y > 0)
1714 term->curs.x = term->cols - 1, term->curs.y--;
1715 else if (term->wrapnext)
1716 term->wrapnext = FALSE;
1720 term->seen_disp_event = TRUE;
1722 case '\016': /* LS1: Locking-shift one */
1723 compatibility(VT100);
1726 case '\017': /* LS0: Locking-shift zero */
1727 compatibility(VT100);
1730 case '\033': /* ESC: Escape */
1731 if (term->vt52_mode)
1732 term->termstate = VT52_ESC;
1734 compatibility(ANSIMIN);
1735 term->termstate = SEEN_ESC;
1736 term->esc_query = FALSE;
1739 case '\015': /* CR: Carriage return */
1741 term->wrapnext = FALSE;
1743 term->seen_disp_event = TRUE;
1744 term->paste_hold = 0;
1746 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1748 case '\014': /* FF: Form feed */
1749 if (has_compat(SCOANSI)) {
1750 move(term, 0, 0, 0);
1751 erase_lots(term, FALSE, FALSE, TRUE);
1753 term->wrapnext = FALSE;
1754 term->seen_disp_event = 1;
1757 case '\013': /* VT: Line tabulation */
1758 compatibility(VT100);
1759 case '\012': /* LF: Line feed */
1760 if (term->curs.y == term->marg_b)
1761 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1762 else if (term->curs.y < term->rows - 1)
1764 if (term->cfg.lfhascr)
1767 term->wrapnext = FALSE;
1768 term->seen_disp_event = 1;
1769 term->paste_hold = 0;
1771 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1773 case '\t': /* HT: Character tabulation */
1775 pos old_curs = term->curs;
1776 unsigned long *ldata = lineptr(term->curs.y);
1780 } while (term->curs.x < term->cols - 1 &&
1781 !term->tabs[term->curs.x]);
1783 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM) {
1784 if (term->curs.x >= term->cols / 2)
1785 term->curs.x = term->cols / 2 - 1;
1787 if (term->curs.x >= term->cols)
1788 term->curs.x = term->cols - 1;
1792 check_selection(term, old_curs, term->curs);
1794 term->seen_disp_event = TRUE;
1798 switch (term->termstate) {
1800 /* Only graphic characters get this far;
1801 * ctrls are stripped above */
1802 if (term->wrapnext && term->wrap) {
1803 term->cpos[1] |= LATTR_WRAPPED;
1804 if (term->curs.y == term->marg_b)
1805 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1806 else if (term->curs.y < term->rows - 1)
1810 term->wrapnext = FALSE;
1814 if (term->selstate != NO_SELECTION) {
1815 pos cursplus = term->curs;
1817 check_selection(term, term->curs, cursplus);
1819 if (((c & CSET_MASK) == ATTR_ASCII || (c & CSET_MASK) == 0) &&
1821 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1827 width = wcwidth((wchar_t) c);
1831 * If we're about to display a double-width
1832 * character starting in the rightmost
1833 * column, then we do something special
1834 * instead. We must print a space in the
1835 * last column of the screen, then wrap;
1836 * and we also set LATTR_WRAPPED2 which
1837 * instructs subsequent cut-and-pasting not
1838 * only to splice this line to the one
1839 * after it, but to ignore the space in the
1840 * last character position as well.
1841 * (Because what was actually output to the
1842 * terminal was presumably just a sequence
1843 * of CJK characters, and we don't want a
1844 * space to be pasted in the middle of
1845 * those just because they had the
1846 * misfortune to start in the wrong parity
1847 * column. xterm concurs.)
1849 check_boundary(term, term->curs.x, term->curs.y);
1850 check_boundary(term, term->curs.x+2, term->curs.y);
1851 if (term->curs.x == term->cols-1) {
1852 *term->cpos++ = ATTR_ASCII | ' ' | term->curr_attr;
1853 *term->cpos |= LATTR_WRAPPED | LATTR_WRAPPED2;
1854 if (term->curs.y == term->marg_b)
1855 scroll(term, term->marg_t, term->marg_b,
1857 else if (term->curs.y < term->rows - 1)
1861 /* Now we must check_boundary again, of course. */
1862 check_boundary(term, term->curs.x, term->curs.y);
1863 check_boundary(term, term->curs.x+2, term->curs.y);
1865 *term->cpos++ = c | term->curr_attr;
1866 *term->cpos++ = UCSWIDE | term->curr_attr;
1870 check_boundary(term, term->curs.x, term->curs.y);
1871 check_boundary(term, term->curs.x+1, term->curs.y);
1872 *term->cpos++ = c | term->curr_attr;
1879 if (term->curs.x == term->cols) {
1882 term->wrapnext = TRUE;
1883 if (term->wrap && term->vt52_mode) {
1884 term->cpos[1] |= LATTR_WRAPPED;
1885 if (term->curs.y == term->marg_b)
1886 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1887 else if (term->curs.y < term->rows - 1)
1891 term->wrapnext = FALSE;
1894 term->seen_disp_event = 1;
1899 * This state is virtually identical to SEEN_ESC, with the
1900 * exception that we have an OSC sequence in the pipeline,
1901 * and _if_ we see a backslash, we process it.
1905 term->termstate = TOPLEVEL;
1908 /* else fall through */
1910 if (c >= ' ' && c <= '/') {
1911 if (term->esc_query)
1912 term->esc_query = -1;
1914 term->esc_query = c;
1917 term->termstate = TOPLEVEL;
1918 switch (ANSI(c, term->esc_query)) {
1919 case '[': /* enter CSI mode */
1920 term->termstate = SEEN_CSI;
1921 term->esc_nargs = 1;
1922 term->esc_args[0] = ARG_DEFAULT;
1923 term->esc_query = FALSE;
1925 case ']': /* OSC: xterm escape sequences */
1926 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1927 compatibility(OTHER);
1928 term->termstate = SEEN_OSC;
1929 term->esc_args[0] = 0;
1931 case '7': /* DECSC: save cursor */
1932 compatibility(VT100);
1933 save_cursor(term, TRUE);
1935 case '8': /* DECRC: restore cursor */
1936 compatibility(VT100);
1937 save_cursor(term, FALSE);
1938 term->seen_disp_event = TRUE;
1940 case '=': /* DECKPAM: Keypad application mode */
1941 compatibility(VT100);
1942 term->app_keypad_keys = TRUE;
1944 case '>': /* DECKPNM: Keypad numeric mode */
1945 compatibility(VT100);
1946 term->app_keypad_keys = FALSE;
1948 case 'D': /* IND: exactly equivalent to LF */
1949 compatibility(VT100);
1950 if (term->curs.y == term->marg_b)
1951 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1952 else if (term->curs.y < term->rows - 1)
1955 term->wrapnext = FALSE;
1956 term->seen_disp_event = TRUE;
1958 case 'E': /* NEL: exactly equivalent to CR-LF */
1959 compatibility(VT100);
1961 if (term->curs.y == term->marg_b)
1962 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1963 else if (term->curs.y < term->rows - 1)
1966 term->wrapnext = FALSE;
1967 term->seen_disp_event = TRUE;
1969 case 'M': /* RI: reverse index - backwards LF */
1970 compatibility(VT100);
1971 if (term->curs.y == term->marg_t)
1972 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
1973 else if (term->curs.y > 0)
1976 term->wrapnext = FALSE;
1977 term->seen_disp_event = TRUE;
1979 case 'Z': /* DECID: terminal type query */
1980 compatibility(VT100);
1982 ldisc_send(term->ldisc, term->id_string,
1983 strlen(term->id_string), 0);
1985 case 'c': /* RIS: restore power-on settings */
1986 compatibility(VT100);
1988 if (term->ldisc) /* cause ldisc to notice changes */
1989 ldisc_send(term->ldisc, NULL, 0, 0);
1990 if (term->reset_132) {
1991 if (!term->cfg.no_remote_resize)
1992 request_resize(term->frontend, 80, term->rows);
1993 term->reset_132 = 0;
1997 term->seen_disp_event = TRUE;
1999 case 'H': /* HTS: set a tab */
2000 compatibility(VT100);
2001 term->tabs[term->curs.x] = TRUE;
2004 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
2005 compatibility(VT100);
2007 unsigned long *ldata;
2011 for (i = 0; i < term->rows; i++) {
2013 for (j = 0; j < term->cols; j++)
2014 ldata[j] = ATTR_DEFAULT | 'E';
2015 ldata[term->cols] = 0;
2018 term->seen_disp_event = TRUE;
2019 scrtop.x = scrtop.y = 0;
2021 scrbot.y = term->rows;
2022 check_selection(term, scrtop, scrbot);
2026 case ANSI('3', '#'):
2027 case ANSI('4', '#'):
2028 case ANSI('5', '#'):
2029 case ANSI('6', '#'):
2030 compatibility(VT100);
2032 unsigned long nlattr;
2033 unsigned long *ldata;
2034 switch (ANSI(c, term->esc_query)) {
2035 case ANSI('3', '#'): /* DECDHL: 2*height, top */
2038 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
2041 case ANSI('5', '#'): /* DECSWL: normal */
2042 nlattr = LATTR_NORM;
2044 default: /* case ANSI('6', '#'): DECDWL: 2*width */
2045 nlattr = LATTR_WIDE;
2048 ldata = lineptr(term->curs.y);
2049 ldata[term->cols] &= ~LATTR_MODE;
2050 ldata[term->cols] |= nlattr;
2053 /* GZD4: G0 designate 94-set */
2054 case ANSI('A', '('):
2055 compatibility(VT100);
2056 if (!term->cfg.no_remote_charset)
2057 term->cset_attr[0] = ATTR_GBCHR;
2059 case ANSI('B', '('):
2060 compatibility(VT100);
2061 if (!term->cfg.no_remote_charset)
2062 term->cset_attr[0] = ATTR_ASCII;
2064 case ANSI('0', '('):
2065 compatibility(VT100);
2066 if (!term->cfg.no_remote_charset)
2067 term->cset_attr[0] = ATTR_LINEDRW;
2069 case ANSI('U', '('):
2070 compatibility(OTHER);
2071 if (!term->cfg.no_remote_charset)
2072 term->cset_attr[0] = ATTR_SCOACS;
2074 /* G1D4: G1-designate 94-set */
2075 case ANSI('A', ')'):
2076 compatibility(VT100);
2077 if (!term->cfg.no_remote_charset)
2078 term->cset_attr[1] = ATTR_GBCHR;
2080 case ANSI('B', ')'):
2081 compatibility(VT100);
2082 if (!term->cfg.no_remote_charset)
2083 term->cset_attr[1] = ATTR_ASCII;
2085 case ANSI('0', ')'):
2086 compatibility(VT100);
2087 if (!term->cfg.no_remote_charset)
2088 term->cset_attr[1] = ATTR_LINEDRW;
2090 case ANSI('U', ')'):
2091 compatibility(OTHER);
2092 if (!term->cfg.no_remote_charset)
2093 term->cset_attr[1] = ATTR_SCOACS;
2095 /* DOCS: Designate other coding system */
2096 case ANSI('8', '%'): /* Old Linux code */
2097 case ANSI('G', '%'):
2098 compatibility(OTHER);
2099 if (!term->cfg.no_remote_charset)
2102 case ANSI('@', '%'):
2103 compatibility(OTHER);
2104 if (!term->cfg.no_remote_charset)
2110 term->termstate = TOPLEVEL; /* default */
2112 if (term->esc_nargs <= ARGS_MAX) {
2113 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
2114 term->esc_args[term->esc_nargs - 1] = 0;
2115 term->esc_args[term->esc_nargs - 1] =
2116 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
2118 term->termstate = SEEN_CSI;
2119 } else if (c == ';') {
2120 if (++term->esc_nargs <= ARGS_MAX)
2121 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
2122 term->termstate = SEEN_CSI;
2123 } else if (c < '@') {
2124 if (term->esc_query)
2125 term->esc_query = -1;
2127 term->esc_query = TRUE;
2129 term->esc_query = c;
2130 term->termstate = SEEN_CSI;
2132 switch (ANSI(c, term->esc_query)) {
2133 case 'A': /* CUU: move up N lines */
2134 move(term, term->curs.x,
2135 term->curs.y - def(term->esc_args[0], 1), 1);
2136 term->seen_disp_event = TRUE;
2138 case 'e': /* VPR: move down N lines */
2139 compatibility(ANSI);
2141 case 'B': /* CUD: Cursor down */
2142 move(term, term->curs.x,
2143 term->curs.y + def(term->esc_args[0], 1), 1);
2144 term->seen_disp_event = TRUE;
2146 case ANSI('c', '>'): /* DA: report xterm version */
2147 compatibility(OTHER);
2148 /* this reports xterm version 136 so that VIM can
2149 use the drag messages from the mouse reporting */
2151 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
2153 case 'a': /* HPR: move right N cols */
2154 compatibility(ANSI);
2156 case 'C': /* CUF: Cursor right */
2157 move(term, term->curs.x + def(term->esc_args[0], 1),
2159 term->seen_disp_event = TRUE;
2161 case 'D': /* CUB: move left N cols */
2162 move(term, term->curs.x - def(term->esc_args[0], 1),
2164 term->seen_disp_event = TRUE;
2166 case 'E': /* CNL: move down N lines and CR */
2167 compatibility(ANSI);
2169 term->curs.y + def(term->esc_args[0], 1), 1);
2170 term->seen_disp_event = TRUE;
2172 case 'F': /* CPL: move up N lines and CR */
2173 compatibility(ANSI);
2175 term->curs.y - def(term->esc_args[0], 1), 1);
2176 term->seen_disp_event = TRUE;
2179 case '`': /* HPA: set horizontal posn */
2180 compatibility(ANSI);
2181 move(term, def(term->esc_args[0], 1) - 1,
2183 term->seen_disp_event = TRUE;
2185 case 'd': /* VPA: set vertical posn */
2186 compatibility(ANSI);
2187 move(term, term->curs.x,
2188 ((term->dec_om ? term->marg_t : 0) +
2189 def(term->esc_args[0], 1) - 1),
2190 (term->dec_om ? 2 : 0));
2191 term->seen_disp_event = TRUE;
2194 case 'f': /* HVP: set horz and vert posns at once */
2195 if (term->esc_nargs < 2)
2196 term->esc_args[1] = ARG_DEFAULT;
2197 move(term, def(term->esc_args[1], 1) - 1,
2198 ((term->dec_om ? term->marg_t : 0) +
2199 def(term->esc_args[0], 1) - 1),
2200 (term->dec_om ? 2 : 0));
2201 term->seen_disp_event = TRUE;
2203 case 'J': /* ED: erase screen or parts of it */
2205 unsigned int i = def(term->esc_args[0], 0) + 1;
2208 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
2211 term->seen_disp_event = TRUE;
2213 case 'K': /* EL: erase line or parts of it */
2215 unsigned int i = def(term->esc_args[0], 0) + 1;
2218 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
2220 term->seen_disp_event = TRUE;
2222 case 'L': /* IL: insert lines */
2223 compatibility(VT102);
2224 if (term->curs.y <= term->marg_b)
2225 scroll(term, term->curs.y, term->marg_b,
2226 -def(term->esc_args[0], 1), FALSE);
2228 term->seen_disp_event = TRUE;
2230 case 'M': /* DL: delete lines */
2231 compatibility(VT102);
2232 if (term->curs.y <= term->marg_b)
2233 scroll(term, term->curs.y, term->marg_b,
2234 def(term->esc_args[0], 1),
2237 term->seen_disp_event = TRUE;
2239 case '@': /* ICH: insert chars */
2240 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2241 compatibility(VT102);
2242 insch(term, def(term->esc_args[0], 1));
2243 term->seen_disp_event = TRUE;
2245 case 'P': /* DCH: delete chars */
2246 compatibility(VT102);
2247 insch(term, -def(term->esc_args[0], 1));
2248 term->seen_disp_event = TRUE;
2250 case 'c': /* DA: terminal type query */
2251 compatibility(VT100);
2252 /* This is the response for a VT102 */
2254 ldisc_send(term->ldisc, term->id_string,
2255 strlen(term->id_string), 0);
2257 case 'n': /* DSR: cursor position query */
2259 if (term->esc_args[0] == 6) {
2261 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
2263 ldisc_send(term->ldisc, buf, strlen(buf), 0);
2264 } else if (term->esc_args[0] == 5) {
2265 ldisc_send(term->ldisc, "\033[0n", 4, 0);
2269 case 'h': /* SM: toggle modes to high */
2271 compatibility(VT100);
2274 for (i = 0; i < term->esc_nargs; i++)
2275 toggle_mode(term, term->esc_args[i],
2276 term->esc_query, TRUE);
2279 case 'i': /* MC: Media copy */
2281 compatibility(VT100);
2283 if (term->esc_nargs != 1) break;
2284 if (term->esc_args[0] == 5 && *term->cfg.printer) {
2285 term->printing = TRUE;
2286 term->only_printing = !term->esc_query;
2287 term->print_state = 0;
2288 term_print_setup(term);
2289 } else if (term->esc_args[0] == 4 &&
2291 term_print_finish(term);
2295 case 'l': /* RM: toggle modes to low */
2297 compatibility(VT100);
2300 for (i = 0; i < term->esc_nargs; i++)
2301 toggle_mode(term, term->esc_args[i],
2302 term->esc_query, FALSE);
2305 case 'g': /* TBC: clear tabs */
2306 compatibility(VT100);
2307 if (term->esc_nargs == 1) {
2308 if (term->esc_args[0] == 0) {
2309 term->tabs[term->curs.x] = FALSE;
2310 } else if (term->esc_args[0] == 3) {
2312 for (i = 0; i < term->cols; i++)
2313 term->tabs[i] = FALSE;
2317 case 'r': /* DECSTBM: set scroll margins */
2318 compatibility(VT100);
2319 if (term->esc_nargs <= 2) {
2321 top = def(term->esc_args[0], 1) - 1;
2322 bot = (term->esc_nargs <= 1
2323 || term->esc_args[1] == 0 ?
2325 def(term->esc_args[1], term->rows)) - 1;
2326 if (bot >= term->rows)
2327 bot = term->rows - 1;
2328 /* VTTEST Bug 9 - if region is less than 2 lines
2329 * don't change region.
2331 if (bot - top > 0) {
2336 * I used to think the cursor should be
2337 * placed at the top of the newly marginned
2338 * area. Apparently not: VMS TPU falls over
2341 * Well actually it should for
2344 term->curs.y = (term->dec_om ?
2347 term->seen_disp_event = TRUE;
2351 case 'm': /* SGR: set graphics rendition */
2354 * A VT100 without the AVO only had one
2355 * attribute, either underline or
2356 * reverse video depending on the
2357 * cursor type, this was selected by
2361 * This is sometimes DIM, eg on the
2364 * This is sometimes INVIS various ANSI.
2366 * This like 22 disables BOLD, DIM and INVIS
2368 * The ANSI colours appear on any
2369 * terminal that has colour (obviously)
2370 * but the interaction between sgr0 and
2371 * the colours varies but is usually
2372 * related to the background colour
2373 * erase item. The interaction between
2374 * colour attributes and the mono ones
2375 * is also very implementation
2378 * The 39 and 49 attributes are likely
2379 * to be unimplemented.
2382 for (i = 0; i < term->esc_nargs; i++) {
2383 switch (def(term->esc_args[i], 0)) {
2384 case 0: /* restore defaults */
2385 term->curr_attr = term->default_attr;
2387 case 1: /* enable bold */
2388 compatibility(VT100AVO);
2389 term->curr_attr |= ATTR_BOLD;
2391 case 21: /* (enable double underline) */
2392 compatibility(OTHER);
2393 case 4: /* enable underline */
2394 compatibility(VT100AVO);
2395 term->curr_attr |= ATTR_UNDER;
2397 case 5: /* enable blink */
2398 compatibility(VT100AVO);
2399 term->curr_attr |= ATTR_BLINK;
2401 case 6: /* SCO light bkgrd */
2402 compatibility(SCOANSI);
2403 term->blink_is_real = FALSE;
2404 term->curr_attr |= ATTR_BLINK;
2406 case 7: /* enable reverse video */
2407 term->curr_attr |= ATTR_REVERSE;
2409 case 10: /* SCO acs off */
2410 compatibility(SCOANSI);
2411 if (term->cfg.no_remote_charset) break;
2412 term->sco_acs = 0; break;
2413 case 11: /* SCO acs on */
2414 compatibility(SCOANSI);
2415 if (term->cfg.no_remote_charset) break;
2416 term->sco_acs = 1; break;
2417 case 12: /* SCO acs on, |0x80 */
2418 compatibility(SCOANSI);
2419 if (term->cfg.no_remote_charset) break;
2420 term->sco_acs = 2; break;
2421 case 22: /* disable bold */
2422 compatibility2(OTHER, VT220);
2423 term->curr_attr &= ~ATTR_BOLD;
2425 case 24: /* disable underline */
2426 compatibility2(OTHER, VT220);
2427 term->curr_attr &= ~ATTR_UNDER;
2429 case 25: /* disable blink */
2430 compatibility2(OTHER, VT220);
2431 term->curr_attr &= ~ATTR_BLINK;
2433 case 27: /* disable reverse video */
2434 compatibility2(OTHER, VT220);
2435 term->curr_attr &= ~ATTR_REVERSE;
2446 term->curr_attr &= ~ATTR_FGMASK;
2448 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
2458 /* xterm-style bright foreground */
2459 term->curr_attr &= ~ATTR_FGMASK;
2461 ((term->esc_args[i] - 90 + 16)
2464 case 39: /* default-foreground */
2465 term->curr_attr &= ~ATTR_FGMASK;
2466 term->curr_attr |= ATTR_DEFFG;
2477 term->curr_attr &= ~ATTR_BGMASK;
2479 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
2489 /* xterm-style bright background */
2490 term->curr_attr &= ~ATTR_BGMASK;
2492 ((term->esc_args[i] - 100 + 16)
2495 case 49: /* default-background */
2496 term->curr_attr &= ~ATTR_BGMASK;
2497 term->curr_attr |= ATTR_DEFBG;
2502 term->erase_char = (' ' | ATTR_ASCII |
2508 case 's': /* save cursor */
2509 save_cursor(term, TRUE);
2511 case 'u': /* restore cursor */
2512 save_cursor(term, FALSE);
2513 term->seen_disp_event = TRUE;
2515 case 't': /* DECSLPP: set page size - ie window height */
2517 * VT340/VT420 sequence DECSLPP, DEC only allows values
2518 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2519 * illegal values (eg first arg 1..9) for window changing
2522 if (term->esc_nargs <= 1
2523 && (term->esc_args[0] < 1 ||
2524 term->esc_args[0] >= 24)) {
2525 compatibility(VT340TEXT);
2526 if (!term->cfg.no_remote_resize)
2527 request_resize(term->frontend, term->cols,
2528 def(term->esc_args[0], 24));
2530 } else if (term->esc_nargs >= 1 &&
2531 term->esc_args[0] >= 1 &&
2532 term->esc_args[0] < 24) {
2533 compatibility(OTHER);
2535 switch (term->esc_args[0]) {
2539 set_iconic(term->frontend, FALSE);
2542 set_iconic(term->frontend, TRUE);
2545 if (term->esc_nargs >= 3) {
2546 if (!term->cfg.no_remote_resize)
2547 move_window(term->frontend,
2548 def(term->esc_args[1], 0),
2549 def(term->esc_args[2], 0));
2553 /* We should resize the window to a given
2554 * size in pixels here, but currently our
2555 * resizing code isn't healthy enough to
2560 set_zorder(term->frontend, TRUE);
2563 /* move to bottom */
2564 set_zorder(term->frontend, FALSE);
2567 refresh_window(term->frontend);
2570 if (term->esc_nargs >= 3) {
2571 if (!term->cfg.no_remote_resize)
2572 request_resize(term->frontend,
2573 def(term->esc_args[2], term->cfg.width),
2574 def(term->esc_args[1], term->cfg.height));
2578 if (term->esc_nargs >= 2)
2579 set_zoomed(term->frontend,
2585 ldisc_send(term->ldisc,
2586 is_iconic(term->frontend) ?
2587 "\033[1t" : "\033[2t", 4, 0);
2591 get_window_pos(term->frontend, &x, &y);
2592 len = sprintf(buf, "\033[3;%d;%dt", x, y);
2593 ldisc_send(term->ldisc, buf, len, 0);
2598 get_window_pixels(term->frontend, &x, &y);
2599 len = sprintf(buf, "\033[4;%d;%dt", x, y);
2600 ldisc_send(term->ldisc, buf, len, 0);
2605 len = sprintf(buf, "\033[8;%d;%dt",
2606 term->rows, term->cols);
2607 ldisc_send(term->ldisc, buf, len, 0);
2612 * Hmmm. Strictly speaking we
2613 * should return `the size of the
2614 * screen in characters', but
2615 * that's not easy: (a) window
2616 * furniture being what it is it's
2617 * hard to compute, and (b) in
2618 * resize-font mode maximising the
2619 * window wouldn't change the
2620 * number of characters. *shrug*. I
2621 * think we'll ignore it for the
2622 * moment and see if anyone
2623 * complains, and then ask them
2624 * what they would like it to do.
2629 !term->cfg.no_remote_qtitle) {
2630 p = get_window_title(term->frontend, TRUE);
2632 ldisc_send(term->ldisc, "\033]L", 3, 0);
2633 ldisc_send(term->ldisc, p, len, 0);
2634 ldisc_send(term->ldisc, "\033\\", 2, 0);
2639 !term->cfg.no_remote_qtitle) {
2640 p = get_window_title(term->frontend,FALSE);
2642 ldisc_send(term->ldisc, "\033]l", 3, 0);
2643 ldisc_send(term->ldisc, p, len, 0);
2644 ldisc_send(term->ldisc, "\033\\", 2, 0);
2650 case 'S': /* SU: Scroll up */
2651 compatibility(SCOANSI);
2652 scroll(term, term->marg_t, term->marg_b,
2653 def(term->esc_args[0], 1), TRUE);
2655 term->wrapnext = FALSE;
2656 term->seen_disp_event = TRUE;
2658 case 'T': /* SD: Scroll down */
2659 compatibility(SCOANSI);
2660 scroll(term, term->marg_t, term->marg_b,
2661 -def(term->esc_args[0], 1), TRUE);
2663 term->wrapnext = FALSE;
2664 term->seen_disp_event = TRUE;
2666 case ANSI('|', '*'): /* DECSNLS */
2668 * Set number of lines on screen
2669 * VT420 uses VGA like hardware and can
2670 * support any size in reasonable range
2671 * (24..49 AIUI) with no default specified.
2673 compatibility(VT420);
2674 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
2675 if (!term->cfg.no_remote_resize)
2676 request_resize(term->frontend, term->cols,
2677 def(term->esc_args[0],
2682 case ANSI('|', '$'): /* DECSCPP */
2684 * Set number of columns per page
2685 * Docs imply range is only 80 or 132, but
2688 compatibility(VT340TEXT);
2689 if (term->esc_nargs <= 1) {
2690 if (!term->cfg.no_remote_resize)
2691 request_resize(term->frontend,
2692 def(term->esc_args[0],
2693 term->cfg.width), term->rows);
2697 case 'X': /* ECH: write N spaces w/o moving cursor */
2698 /* XXX VTTEST says this is vt220, vt510 manual
2700 compatibility(ANSIMIN);
2702 int n = def(term->esc_args[0], 1);
2704 unsigned long *p = term->cpos;
2705 if (n > term->cols - term->curs.x)
2706 n = term->cols - term->curs.x;
2707 cursplus = term->curs;
2709 check_boundary(term, term->curs.x, term->curs.y);
2710 check_boundary(term, term->curs.x+n, term->curs.y);
2711 check_selection(term, term->curs, cursplus);
2713 *p++ = term->erase_char;
2714 term->seen_disp_event = TRUE;
2717 case 'x': /* DECREQTPARM: report terminal characteristics */
2718 compatibility(VT100);
2721 int i = def(term->esc_args[0], 0);
2722 if (i == 0 || i == 1) {
2723 strcpy(buf, "\033[2;1;1;112;112;1;0x");
2725 ldisc_send(term->ldisc, buf, 20, 0);
2729 case 'Z': /* CBT: BackTab for xterm */
2730 compatibility(OTHER);
2732 int i = def(term->esc_args[0], 1);
2733 pos old_curs = term->curs;
2735 for(;i>0 && term->curs.x>0; i--) {
2738 } while (term->curs.x >0 &&
2739 !term->tabs[term->curs.x]);
2742 check_selection(term, old_curs, term->curs);
2745 case ANSI('c', '='): /* Hide or Show Cursor */
2746 compatibility(SCOANSI);
2747 switch(term->esc_args[0]) {
2748 case 0: /* hide cursor */
2749 term->cursor_on = FALSE;
2751 case 1: /* restore cursor */
2752 term->big_cursor = FALSE;
2753 term->cursor_on = TRUE;
2755 case 2: /* block cursor */
2756 term->big_cursor = TRUE;
2757 term->cursor_on = TRUE;
2761 case ANSI('C', '='):
2763 * set cursor start on scanline esc_args[0] and
2764 * end on scanline esc_args[1].If you set
2765 * the bottom scan line to a value less than
2766 * the top scan line, the cursor will disappear.
2768 compatibility(SCOANSI);
2769 if (term->esc_nargs >= 2) {
2770 if (term->esc_args[0] > term->esc_args[1])
2771 term->cursor_on = FALSE;
2773 term->cursor_on = TRUE;
2776 case ANSI('D', '='):
2777 compatibility(SCOANSI);
2778 term->blink_is_real = FALSE;
2779 if (term->esc_args[0]>=1)
2780 term->curr_attr |= ATTR_BLINK;
2782 term->curr_attr &= ~ATTR_BLINK;
2784 case ANSI('E', '='):
2785 compatibility(SCOANSI);
2786 term->blink_is_real = (term->esc_args[0] >= 1);
2788 case ANSI('F', '='): /* set normal foreground */
2789 compatibility(SCOANSI);
2790 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
2792 (sco2ansicolour[term->esc_args[0] & 0x7] |
2793 ((term->esc_args[0] & 0x8) << 1)) <<
2795 term->curr_attr &= ~ATTR_FGMASK;
2796 term->curr_attr |= colour;
2797 term->default_attr &= ~ATTR_FGMASK;
2798 term->default_attr |= colour;
2801 case ANSI('G', '='): /* set normal background */
2802 compatibility(SCOANSI);
2803 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
2805 (sco2ansicolour[term->esc_args[0] & 0x7] |
2806 ((term->esc_args[0] & 0x8) << 1)) <<
2808 term->curr_attr &= ~ATTR_BGMASK;
2809 term->curr_attr |= colour;
2810 term->default_attr &= ~ATTR_BGMASK;
2811 term->default_attr |= colour;
2814 case ANSI('L', '='):
2815 compatibility(SCOANSI);
2816 term->use_bce = (term->esc_args[0] <= 0);
2817 term->erase_char = ERASE_CHAR;
2819 term->erase_char = (' ' | ATTR_ASCII |
2821 (ATTR_FGMASK | ATTR_BGMASK)));
2823 case ANSI('p', '"'): /* DECSCL: set compat level */
2825 * Allow the host to make this emulator a
2826 * 'perfect' VT102. This first appeared in
2827 * the VT220, but we do need to get back to
2828 * PuTTY mode so I won't check it.
2830 * The arg in 40..42,50 are a PuTTY extension.
2831 * The 2nd arg, 8bit vs 7bit is not checked.
2833 * Setting VT102 mode should also change
2834 * the Fkeys to generate PF* codes as a
2835 * real VT102 has no Fkeys. The VT220 does
2836 * this, F11..F13 become ESC,BS,LF other
2837 * Fkeys send nothing.
2839 * Note ESC c will NOT change this!
2842 switch (term->esc_args[0]) {
2844 term->compatibility_level &= ~TM_VTXXX;
2845 term->compatibility_level |= TM_VT102;
2848 term->compatibility_level &= ~TM_VTXXX;
2849 term->compatibility_level |= TM_VT220;
2853 if (term->esc_args[0] > 60 &&
2854 term->esc_args[0] < 70)
2855 term->compatibility_level |= TM_VTXXX;
2859 term->compatibility_level &= TM_VTXXX;
2862 term->compatibility_level = TM_PUTTY;
2865 term->compatibility_level = TM_SCOANSI;
2869 term->compatibility_level = TM_PUTTY;
2875 /* Change the response to CSI c */
2876 if (term->esc_args[0] == 50) {
2879 strcpy(term->id_string, "\033[?");
2880 for (i = 1; i < term->esc_nargs; i++) {
2882 strcat(term->id_string, ";");
2883 sprintf(lbuf, "%d", term->esc_args[i]);
2884 strcat(term->id_string, lbuf);
2886 strcat(term->id_string, "c");
2889 /* Is this a good idea ?
2890 * Well we should do a soft reset at this point ...
2892 if (!has_compat(VT420) && has_compat(VT100)) {
2893 if (!term->cfg.no_remote_resize) {
2894 if (term->reset_132)
2895 request_resize(132, 24);
2897 request_resize(80, 24);
2905 term->osc_w = FALSE;
2907 case 'P': /* Linux palette sequence */
2908 term->termstate = SEEN_OSC_P;
2909 term->osc_strlen = 0;
2911 case 'R': /* Linux palette reset */
2912 palette_reset(term->frontend);
2913 term_invalidate(term);
2914 term->termstate = TOPLEVEL;
2916 case 'W': /* word-set */
2917 term->termstate = SEEN_OSC_W;
2930 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2934 * Grotty hack to support xterm and DECterm title
2935 * sequences concurrently.
2937 if (term->esc_args[0] == 2) {
2938 term->esc_args[0] = 1;
2941 /* else fall through */
2943 term->termstate = OSC_STRING;
2944 term->osc_strlen = 0;
2949 * This OSC stuff is EVIL. It takes just one character to get into
2950 * sysline mode and it's not initially obvious how to get out.
2951 * So I've added CR and LF as string aborts.
2952 * This shouldn't effect compatibility as I believe embedded
2953 * control characters are supposed to be interpreted (maybe?)
2954 * and they don't display anything useful anyway.
2958 if (c == '\012' || c == '\015') {
2959 term->termstate = TOPLEVEL;
2960 } else if (c == 0234 || c == '\007') {
2962 * These characters terminate the string; ST and BEL
2963 * terminate the sequence and trigger instant
2964 * processing of it, whereas ESC goes back to SEEN_ESC
2965 * mode unless it is followed by \, in which case it is
2966 * synonymous with ST in the first place.
2969 term->termstate = TOPLEVEL;
2970 } else if (c == '\033')
2971 term->termstate = OSC_MAYBE_ST;
2972 else if (term->osc_strlen < OSC_STR_MAX)
2973 term->osc_string[term->osc_strlen++] = c;
2977 int max = (term->osc_strlen == 0 ? 21 : 16);
2979 if (c >= '0' && c <= '9')
2981 else if (c >= 'A' && c <= 'A' + max - 10)
2983 else if (c >= 'a' && c <= 'a' + max - 10)
2986 term->termstate = TOPLEVEL;
2989 term->osc_string[term->osc_strlen++] = val;
2990 if (term->osc_strlen >= 7) {
2991 palette_set(term->frontend, term->osc_string[0],
2992 term->osc_string[1] * 16 + term->osc_string[2],
2993 term->osc_string[3] * 16 + term->osc_string[4],
2994 term->osc_string[5] * 16 + term->osc_string[6]);
2995 term_invalidate(term);
2996 term->termstate = TOPLEVEL;
3012 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
3015 term->termstate = OSC_STRING;
3016 term->osc_strlen = 0;
3020 term->termstate = TOPLEVEL;
3021 term->seen_disp_event = TRUE;
3024 move(term, term->curs.x, term->curs.y - 1, 1);
3027 move(term, term->curs.x, term->curs.y + 1, 1);
3030 move(term, term->curs.x + 1, term->curs.y, 1);
3033 move(term, term->curs.x - 1, term->curs.y, 1);
3036 * From the VT100 Manual
3037 * NOTE: The special graphics characters in the VT100
3038 * are different from those in the VT52
3040 * From VT102 manual:
3041 * 137 _ Blank - Same
3042 * 140 ` Reserved - Humm.
3043 * 141 a Solid rectangle - Similar
3044 * 142 b 1/ - Top half of fraction for the
3045 * 143 c 3/ - subscript numbers below.
3048 * 146 f Degrees - Same
3049 * 147 g Plus or minus - Same
3051 * 151 i Ellipsis (dots)
3054 * 154 l Bar at scan 0
3055 * 155 m Bar at scan 1
3056 * 156 n Bar at scan 2
3057 * 157 o Bar at scan 3 - Similar
3058 * 160 p Bar at scan 4 - Similar
3059 * 161 q Bar at scan 5 - Similar
3060 * 162 r Bar at scan 6 - Same
3061 * 163 s Bar at scan 7 - Similar
3076 term->cset_attr[term->cset = 0] = ATTR_LINEDRW;
3079 term->cset_attr[term->cset = 0] = ATTR_ASCII;
3082 move(term, 0, 0, 0);
3085 if (term->curs.y == 0)
3086 scroll(term, 0, term->rows - 1, -1, TRUE);
3087 else if (term->curs.y > 0)
3090 term->wrapnext = FALSE;
3093 erase_lots(term, FALSE, FALSE, TRUE);
3097 erase_lots(term, TRUE, FALSE, TRUE);
3101 /* XXX Print cursor line */
3104 /* XXX Start controller mode */
3107 /* XXX Stop controller mode */
3111 term->termstate = VT52_Y1;
3115 ldisc_send(term->ldisc, "\033/Z", 3, 0);
3118 term->app_keypad_keys = TRUE;
3121 term->app_keypad_keys = FALSE;
3124 /* XXX This should switch to VT100 mode not current or default
3125 * VT mode. But this will only have effect in a VT220+
3128 term->vt52_mode = FALSE;
3129 term->blink_is_real = term->cfg.blinktext;
3133 /* XXX Enter auto print mode */
3136 /* XXX Exit auto print mode */
3139 /* XXX Print screen */
3145 /* compatibility(ATARI) */
3146 move(term, 0, 0, 0);
3147 erase_lots(term, FALSE, FALSE, TRUE);
3151 /* compatibility(ATARI) */
3152 if (term->curs.y <= term->marg_b)
3153 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
3156 /* compatibility(ATARI) */
3157 if (term->curs.y <= term->marg_b)
3158 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
3161 /* compatibility(ATARI) */
3162 term->termstate = VT52_FG;
3165 /* compatibility(ATARI) */
3166 term->termstate = VT52_BG;
3169 /* compatibility(ATARI) */
3170 erase_lots(term, FALSE, TRUE, FALSE);
3174 /* compatibility(ATARI) */
3175 term->cursor_on = TRUE;
3178 /* compatibility(ATARI) */
3179 term->cursor_on = FALSE;
3181 /* case 'j': Save cursor position - broken on ST */
3182 /* case 'k': Restore cursor position */
3184 /* compatibility(ATARI) */
3185 erase_lots(term, TRUE, TRUE, TRUE);
3187 term->wrapnext = FALSE;
3191 /* compatibility(ATARI) */
3192 erase_lots(term, TRUE, TRUE, FALSE);
3195 /* compatibility(ATARI) */
3196 term->curr_attr |= ATTR_REVERSE;
3199 /* compatibility(ATARI) */
3200 term->curr_attr &= ~ATTR_REVERSE;
3202 case 'v': /* wrap Autowrap on - Wyse style */
3203 /* compatibility(ATARI) */
3206 case 'w': /* Autowrap off */
3207 /* compatibility(ATARI) */
3212 /* compatibility(OTHER) */
3213 term->vt52_bold = FALSE;
3214 term->curr_attr = ATTR_DEFAULT;
3216 term->erase_char = (' ' | ATTR_ASCII |
3218 (ATTR_FGMASK | ATTR_BGMASK)));
3221 /* compatibility(VI50) */
3222 term->curr_attr |= ATTR_UNDER;
3225 /* compatibility(VI50) */
3226 term->curr_attr &= ~ATTR_UNDER;
3229 /* compatibility(VI50) */
3230 term->vt52_bold = TRUE;
3231 term->curr_attr |= ATTR_BOLD;
3234 /* compatibility(VI50) */
3235 term->vt52_bold = FALSE;
3236 term->curr_attr &= ~ATTR_BOLD;
3242 term->termstate = VT52_Y2;
3243 move(term, term->curs.x, c - ' ', 0);
3246 term->termstate = TOPLEVEL;
3247 move(term, c - ' ', term->curs.y, 0);
3252 term->termstate = TOPLEVEL;
3253 term->curr_attr &= ~ATTR_FGMASK;
3254 term->curr_attr &= ~ATTR_BOLD;
3255 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
3256 if ((c & 0x8) || term->vt52_bold)
3257 term->curr_attr |= ATTR_BOLD;
3260 term->erase_char = (' ' | ATTR_ASCII |
3262 (ATTR_FGMASK | ATTR_BGMASK)));
3265 term->termstate = TOPLEVEL;
3266 term->curr_attr &= ~ATTR_BGMASK;
3267 term->curr_attr &= ~ATTR_BLINK;
3268 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
3270 /* Note: bold background */
3272 term->curr_attr |= ATTR_BLINK;
3275 term->erase_char = (' ' | ATTR_ASCII |
3277 (ATTR_FGMASK | ATTR_BGMASK)));
3280 default: break; /* placate gcc warning about enum use */
3282 if (term->selstate != NO_SELECTION) {
3283 pos cursplus = term->curs;
3285 check_selection(term, term->curs, cursplus);
3289 term_print_flush(term);
3290 logflush(term->logctx);
3295 * Compare two lines to determine whether they are sufficiently
3296 * alike to scroll-optimise one to the other. Return the degree of
3299 static int linecmp(Terminal *term, unsigned long *a, unsigned long *b)
3303 for (i = n = 0; i < term->cols; i++)
3304 n += (*a++ == *b++);
3310 * Given a context, update the window. Out of paranoia, we don't
3311 * allow WM_PAINT responses to do scrolling optimisations.
3313 static void do_paint(Terminal *term, Context ctx, int may_optimise)
3315 int i, j, our_curs_y, our_curs_x;
3316 unsigned long rv, cursor;
3319 long cursor_background = ERASE_CHAR;
3320 unsigned long ticks;
3321 #ifdef OPTIMISE_SCROLL
3322 struct scrollregion *sr;
3323 #endif /* OPTIMISE_SCROLL */
3326 * Check the visual bell state.
3328 if (term->in_vbell) {
3329 ticks = GETTICKCOUNT();
3330 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
3331 term->in_vbell = FALSE;
3334 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
3337 * screen array, disptop, scrtop,
3339 * cfg.blinkpc, blink_is_real, tblinker,
3340 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
3343 /* Has the cursor position or type changed ? */
3344 if (term->cursor_on) {
3345 if (term->has_focus) {
3346 if (term->blinker || !term->cfg.blink_cur)
3347 cursor = TATTR_ACTCURS;
3351 cursor = TATTR_PASCURS;
3353 cursor |= TATTR_RIGHTCURS;
3356 our_curs_y = term->curs.y - term->disptop;
3359 * Adjust the cursor position in the case where it's
3360 * resting on the right-hand half of a CJK wide character.
3361 * xterm's behaviour here, which seems adequate to me, is
3362 * to display the cursor covering the _whole_ character,
3363 * exactly as if it were one space to the left.
3365 unsigned long *ldata = lineptr(term->curs.y);
3366 our_curs_x = term->curs.x;
3367 if (our_curs_x > 0 &&
3368 (ldata[our_curs_x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3372 if (term->dispcurs && (term->curstype != cursor ||
3374 term->disptext + our_curs_y * (term->cols + 1) +
3376 if (term->dispcurs > term->disptext &&
3377 (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3378 term->dispcurs[-1] |= ATTR_INVALID;
3379 if ( (term->dispcurs[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3380 term->dispcurs[1] |= ATTR_INVALID;
3381 *term->dispcurs |= ATTR_INVALID;
3384 term->dispcurs = NULL;
3386 #ifdef OPTIMISE_SCROLL
3388 sr = term->scrollhead;
3390 struct scrollregion *next = sr->next;
3391 do_scroll(ctx, sr->topline, sr->botline, sr->lines);
3395 term->scrollhead = term->scrolltail = NULL;
3396 #endif /* OPTIMISE_SCROLL */
3398 /* The normal screen data */
3399 for (i = 0; i < term->rows; i++) {
3400 unsigned long *ldata;
3402 int idx, dirty_line, dirty_run, selected;
3403 unsigned long attr = 0;
3404 int updated_line = 0;
3407 int last_run_dirty = 0;
3409 scrpos.y = i + term->disptop;
3410 ldata = lineptr(scrpos.y);
3411 lattr = (ldata[term->cols] & LATTR_MODE);
3413 idx = i * (term->cols + 1);
3414 dirty_run = dirty_line = (ldata[term->cols] !=
3415 term->disptext[idx + term->cols]);
3416 term->disptext[idx + term->cols] = ldata[term->cols];
3418 for (j = 0; j < term->cols; j++, idx++) {
3419 unsigned long tattr, tchar;
3420 unsigned long *d = ldata + j;
3424 tchar = (*d & (CHAR_MASK | CSET_MASK));
3425 tattr = (*d & (ATTR_MASK ^ CSET_MASK));
3426 switch (tchar & CSET_MASK) {
3428 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
3431 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
3434 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
3437 tattr |= (tchar & CSET_MASK);
3439 if (j < term->cols-1 &&
3440 (d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3443 /* Video reversing things */
3444 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
3445 if (term->seltype == LEXICOGRAPHIC)
3446 selected = (posle(term->selstart, scrpos) &&
3447 poslt(scrpos, term->selend));
3449 selected = (posPle(term->selstart, scrpos) &&
3450 posPlt(scrpos, term->selend));
3454 ^ (selected ? ATTR_REVERSE : 0));
3456 /* 'Real' blinking ? */
3457 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
3458 if (term->has_focus && term->tblinker) {
3459 tchar = term->ucsdata->unitab_line[(unsigned char)' '];
3461 tattr &= ~ATTR_BLINK;
3465 * Check the font we'll _probably_ be using to see if
3466 * the character is wide when we don't want it to be.
3468 if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) {
3469 if ((tattr & ATTR_WIDE) == 0 &&
3470 char_width(ctx, (tchar | tattr) & 0xFFFF) == 2)
3471 tattr |= ATTR_NARROW;
3472 } else if (term->disptext[idx]&ATTR_NARROW)
3473 tattr |= ATTR_NARROW;
3475 /* Cursor here ? Save the 'background' */
3476 if (i == our_curs_y && j == our_curs_x) {
3477 cursor_background = tattr | tchar;
3478 term->dispcurs = term->disptext + idx;
3481 if ((term->disptext[idx] ^ tattr) & ATTR_WIDE)
3484 break_run = (((tattr ^ attr) & term->attr_mask) ||
3485 j - start >= sizeof(ch));
3487 /* Special hack for VT100 Linedraw glyphs */
3488 if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA
3489 && tchar <= 0xBD) break_run = TRUE;
3491 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
3492 if ((tchar | tattr) == term->disptext[idx])
3494 else if (!dirty_run && ccount == 1)
3499 if ((dirty_run || last_run_dirty) && ccount > 0) {
3500 do_text(ctx, start, i, ch, ccount, attr, lattr);
3506 if (term->ucsdata->dbcs_screenfont)
3507 last_run_dirty = dirty_run;
3508 dirty_run = dirty_line;
3511 if ((tchar | tattr) != term->disptext[idx])
3513 ch[ccount++] = (char) tchar;
3514 term->disptext[idx] = tchar | tattr;
3516 /* If it's a wide char step along to the next one. */
3517 if (tattr & ATTR_WIDE) {
3518 if (++j < term->cols) {
3522 * By construction above, the cursor should not
3523 * be on the right-hand half of this character.
3526 assert(!(i == our_curs_y && j == our_curs_x));
3527 if (term->disptext[idx] != *d)
3529 term->disptext[idx] = *d;
3533 if (dirty_run && ccount > 0) {
3534 do_text(ctx, start, i, ch, ccount, attr, lattr);
3538 /* Cursor on this line ? (and changed) */
3539 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
3540 ch[0] = (char) (cursor_background & CHAR_MASK);
3541 attr = (cursor_background & ATTR_MASK) | cursor;
3542 do_cursor(ctx, our_curs_x, i, ch, 1, attr, lattr);
3543 term->curstype = cursor;
3549 * Flick the switch that says if blinking things should be shown or hidden.
3552 void term_blink(Terminal *term, int flg)
3554 long now, blink_diff;
3556 now = GETTICKCOUNT();
3557 blink_diff = now - term->last_tblink;
3559 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3560 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
3561 term->last_tblink = now;
3562 term->tblinker = !term->tblinker;
3567 term->last_blink = now;
3571 blink_diff = now - term->last_blink;
3573 /* Make sure the cursor blinks no faster than system blink rate */
3574 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
3577 term->last_blink = now;
3578 term->blinker = !term->blinker;
3582 * Invalidate the whole screen so it will be repainted in full.
3584 void term_invalidate(Terminal *term)
3588 for (i = 0; i < term->rows * (term->cols + 1); i++)
3589 term->disptext[i] = ATTR_INVALID;
3593 * Paint the window in response to a WM_PAINT message.
3595 void term_paint(Terminal *term, Context ctx,
3596 int left, int top, int right, int bottom, int immediately)
3599 if (left < 0) left = 0;
3600 if (top < 0) top = 0;
3601 if (right >= term->cols) right = term->cols-1;
3602 if (bottom >= term->rows) bottom = term->rows-1;
3604 for (i = top; i <= bottom && i < term->rows; i++) {
3605 if ((term->disptext[i * (term->cols + 1) + term->cols] &
3606 LATTR_MODE) == LATTR_NORM)
3607 for (j = left; j <= right && j < term->cols; j++)
3608 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3610 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
3611 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3614 /* This should happen soon enough, also for some reason it sometimes
3615 * fails to actually do anything when re-sizing ... painting the wrong
3619 do_paint (term, ctx, FALSE);
3623 * Attempt to scroll the scrollback. The second parameter gives the
3624 * position we want to scroll to; the first is +1 to denote that
3625 * this position is relative to the beginning of the scrollback, -1
3626 * to denote it is relative to the end, and 0 to denote that it is
3627 * relative to the current position.
3629 void term_scroll(Terminal *term, int rel, int where)
3631 int sbtop = -sblines(term);
3632 #ifdef OPTIMISE_SCROLL
3633 int olddisptop = term->disptop;
3635 #endif /* OPTIMISE_SCROLL */
3637 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
3638 if (term->disptop < sbtop)
3639 term->disptop = sbtop;
3640 if (term->disptop > 0)
3643 #ifdef OPTIMISE_SCROLL
3644 shift = (term->disptop - olddisptop);
3645 if (shift < term->rows && shift > -term->rows)
3646 scroll_display(term, 0, term->rows - 1, shift);
3647 #endif /* OPTIMISE_SCROLL */
3651 static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel)
3654 wchar_t *wbptr; /* where next char goes within workbuf */
3656 int wblen = 0; /* workbuf len */
3657 int buflen; /* amount of memory allocated to workbuf */
3659 buflen = 5120; /* Default size */
3660 workbuf = snewn(buflen, wchar_t);
3661 wbptr = workbuf; /* start filling here */
3662 old_top_x = top.x; /* needed for rect==1 */
3664 while (poslt(top, bottom)) {
3666 unsigned long *ldata = lineptr(top.y);
3670 * nlpos will point at the maximum position on this line we
3671 * should copy up to. So we start it at the end of the
3675 nlpos.x = term->cols;
3678 * ... move it backwards if there's unused space at the end
3679 * of the line (and also set `nl' if this is the case,
3680 * because in normal selection mode this means we need a
3681 * newline at the end)...
3683 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3684 while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 ||
3685 (DIRECT_CHAR(ldata[nlpos.x - 1]) &&
3686 (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20))
3687 && poslt(top, nlpos))
3689 if (poslt(nlpos, bottom))
3691 } else if (ldata[term->cols] & LATTR_WRAPPED2) {
3692 /* Ignore the last char on the line in a WRAPPED2 line. */
3697 * ... and then clip it to the terminal x coordinate if
3698 * we're doing rectangular selection. (In this case we
3699 * still did the above, so that copying e.g. the right-hand
3700 * column from a table doesn't fill with spaces on the
3704 if (nlpos.x > bottom.x)
3706 nl = (top.y < bottom.y);
3709 while (poslt(top, bottom) && poslt(top, nlpos)) {
3712 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
3714 wchar_t cbuf[16], *p;
3715 int uc = (ldata[top.x] & 0xFFFF);
3718 if (uc == UCSWIDE) {
3723 switch (uc & CSET_MASK) {
3725 if (!term->cfg.rawcnp) {
3726 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3730 uc = term->ucsdata->unitab_line[uc & 0xFF];
3733 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3736 switch (uc & CSET_MASK) {
3738 uc = term->ucsdata->unitab_font[uc & 0xFF];
3741 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3745 set = (uc & CSET_MASK);
3746 c = (uc & CHAR_MASK);
3750 if (DIRECT_FONT(uc)) {
3751 if (c >= ' ' && c != 0x7F) {
3755 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
3757 buf[1] = (char) (0xFF & ldata[top.x + 1]);
3758 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
3762 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
3766 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
3773 for (p = cbuf; *p; p++) {
3774 /* Enough overhead for trailing NL and nul */
3775 if (wblen >= buflen - 16) {
3777 workbuf = sresize(workbuf, buflen, wchar_t);
3778 wbptr = workbuf + wblen;
3787 for (i = 0; i < sel_nl_sz; i++) {
3789 *wbptr++ = sel_nl[i];
3793 top.x = rect ? old_top_x : 0;
3795 #if SELECTION_NUL_TERMINATED
3799 write_clip(term->frontend, workbuf, wblen, desel); /* transfer to clipbd */
3800 if (buflen > 0) /* indicates we allocated this buffer */
3804 void term_copyall(Terminal *term)
3808 tree234 *screen = term->screen;
3809 top.y = -sblines(term);
3811 bottom.y = find_last_nonempty_line(term, screen);
3812 bottom.x = term->cols;
3813 clipme(term, top, bottom, 0, TRUE);
3817 * The wordness array is mainly for deciding the disposition of the
3818 * US-ASCII characters.
3820 static int wordtype(Terminal *term, int uc)
3823 int start, end, ctype;
3825 static const struct ucsword ucs_words[] = {
3831 0x037e, 0x037e, 1}, /* Greek question mark */
3833 0x0387, 0x0387, 1}, /* Greek ano teleia */
3835 0x055a, 0x055f, 1}, /* Armenian punctuation */
3837 0x0589, 0x0589, 1}, /* Armenian full stop */
3839 0x0700, 0x070d, 1}, /* Syriac punctuation */
3841 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3843 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3845 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3847 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3849 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3851 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3853 0x2000, 0x200a, 0}, /* Various spaces */
3855 0x2070, 0x207f, 2}, /* superscript */
3857 0x2080, 0x208f, 2}, /* subscript */
3859 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3861 0x3000, 0x3000, 0}, /* ideographic space */
3863 0x3001, 0x3020, 1}, /* ideographic punctuation */
3865 0x303f, 0x309f, 3}, /* Hiragana */
3867 0x30a0, 0x30ff, 3}, /* Katakana */
3869 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3871 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3873 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3875 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3877 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3879 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3881 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3883 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3885 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3889 const struct ucsword *wptr;
3891 uc &= (CSET_MASK | CHAR_MASK);
3893 switch (uc & CSET_MASK) {
3895 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3898 uc = term->ucsdata->unitab_line[uc & 0xFF];
3901 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3904 switch (uc & CSET_MASK) {
3906 uc = term->ucsdata->unitab_font[uc & 0xFF];
3909 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3913 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3914 * fail as there's such a thing as a double width space. :-(
3916 if (term->ucsdata->dbcs_screenfont &&
3917 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
3921 return term->wordness[uc];
3923 for (wptr = ucs_words; wptr->start; wptr++) {
3924 if (uc >= wptr->start && uc <= wptr->end)
3932 * Spread the selection outwards according to the selection mode.
3934 static pos sel_spread_half(Terminal *term, pos p, int dir)
3936 unsigned long *ldata;
3938 int topy = -sblines(term);
3940 ldata = lineptr(p.y);
3942 switch (term->selmode) {
3945 * In this mode, every character is a separate unit, except
3946 * for runs of spaces at the end of a non-wrapping line.
3948 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3949 unsigned long *q = ldata + term->cols;
3950 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
3952 if (q == ldata + term->cols)
3954 if (p.x >= q - ldata)
3955 p.x = (dir == -1 ? q - ldata : term->cols - 1);
3960 * In this mode, the units are maximal runs of characters
3961 * whose `wordness' has the same value.
3963 wvalue = wordtype(term, UCSGET(ldata, p.x));
3966 int maxcols = (ldata[term->cols] & LATTR_WRAPPED2 ?
3967 term->cols-1 : term->cols);
3968 if (p.x < maxcols-1) {
3969 if (wordtype(term, UCSGET(ldata, p.x + 1)) == wvalue)
3974 if (ldata[term->cols] & LATTR_WRAPPED) {
3975 unsigned long *ldata2;
3976 ldata2 = lineptr(p.y+1);
3977 if (wordtype(term, UCSGET(ldata2, 0)) == wvalue) {
3990 if (wordtype(term, UCSGET(ldata, p.x - 1)) == wvalue)
3995 unsigned long *ldata2;
3999 ldata2 = lineptr(p.y-1);
4000 maxcols = (ldata2[term->cols] & LATTR_WRAPPED2 ?
4001 term->cols-1 : term->cols);
4002 if (ldata2[term->cols] & LATTR_WRAPPED) {
4003 if (wordtype(term, UCSGET(ldata2, maxcols-1))
4018 * In this mode, every line is a unit.
4020 p.x = (dir == -1 ? 0 : term->cols - 1);
4026 static void sel_spread(Terminal *term)
4028 if (term->seltype == LEXICOGRAPHIC) {
4029 term->selstart = sel_spread_half(term, term->selstart, -1);
4030 decpos(term->selend);
4031 term->selend = sel_spread_half(term, term->selend, +1);
4032 incpos(term->selend);
4036 void term_do_paste(Terminal *term)
4041 get_clip(term->frontend, &data, &len);
4042 if (data && len > 0) {
4045 term_seen_key_event(term); /* pasted data counts */
4047 if (term->paste_buffer)
4048 sfree(term->paste_buffer);
4049 term->paste_pos = term->paste_hold = term->paste_len = 0;
4050 term->paste_buffer = snewn(len, wchar_t);
4053 while (p < data + len) {
4054 while (p < data + len &&
4055 !(p <= data + len - sel_nl_sz &&
4056 !memcmp(p, sel_nl, sizeof(sel_nl))))
4061 for (i = 0; i < p - q; i++) {
4062 term->paste_buffer[term->paste_len++] = q[i];
4066 if (p <= data + len - sel_nl_sz &&
4067 !memcmp(p, sel_nl, sizeof(sel_nl))) {
4068 term->paste_buffer[term->paste_len++] = '\015';
4074 /* Assume a small paste will be OK in one go. */
4075 if (term->paste_len < 256) {
4077 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
4078 if (term->paste_buffer)
4079 sfree(term->paste_buffer);
4080 term->paste_buffer = 0;
4081 term->paste_pos = term->paste_hold = term->paste_len = 0;
4084 get_clip(term->frontend, NULL, NULL);
4087 void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,
4088 Mouse_Action a, int x, int y, int shift, int ctrl, int alt)
4091 unsigned long *ldata;
4092 int raw_mouse = (term->xterm_mouse &&
4093 !term->cfg.no_mouse_rep &&
4094 !(term->cfg.mouse_override && shift));
4095 int default_seltype;
4099 if (a == MA_DRAG && !raw_mouse)
4100 term_scroll(term, 0, -1);
4102 if (y >= term->rows) {
4104 if (a == MA_DRAG && !raw_mouse)
4105 term_scroll(term, 0, +1);
4114 if (x >= term->cols)
4117 selpoint.y = y + term->disptop;
4119 ldata = lineptr(selpoint.y);
4120 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM)
4124 int encstate = 0, r, c;
4131 encstate = 0x20; /* left button down */
4142 case MBT_WHEEL_DOWN:
4145 default: break; /* placate gcc warning about enum use */
4149 if (term->xterm_mouse == 1)
4155 term->mouse_is_down = 0;
4158 if (term->mouse_is_down == braw)
4160 term->mouse_is_down = braw;
4162 default: break; /* placate gcc warning about enum use */
4171 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
4172 ldisc_send(term->ldisc, abuf, 6, 0);
4178 * Set the selection type (rectangular or normal) at the start
4179 * of a selection attempt, from the state of Alt.
4181 if (!alt ^ !term->cfg.rect_select)
4182 default_seltype = RECTANGULAR;
4184 default_seltype = LEXICOGRAPHIC;
4186 if (term->selstate == NO_SELECTION) {
4187 term->seltype = default_seltype;
4190 if (bcooked == MBT_SELECT && a == MA_CLICK) {
4192 term->selstate = ABOUT_TO;
4193 term->seltype = default_seltype;
4194 term->selanchor = selpoint;
4195 term->selmode = SM_CHAR;
4196 } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
4198 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
4199 term->selstate = DRAGGING;
4200 term->selstart = term->selanchor = selpoint;
4201 term->selend = term->selstart;
4202 incpos(term->selend);
4204 } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||
4205 (bcooked == MBT_EXTEND && a != MA_RELEASE)) {
4206 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
4208 if (bcooked == MBT_EXTEND && a != MA_DRAG &&
4209 term->selstate == SELECTED) {
4210 if (term->seltype == LEXICOGRAPHIC) {
4212 * For normal selection, we extend by moving
4213 * whichever end of the current selection is closer
4216 if (posdiff(selpoint, term->selstart) <
4217 posdiff(term->selend, term->selstart) / 2) {
4218 term->selanchor = term->selend;
4219 decpos(term->selanchor);
4221 term->selanchor = term->selstart;
4225 * For rectangular selection, we have a choice of
4226 * _four_ places to put selanchor and selpoint: the
4227 * four corners of the selection.
4229 if (2*selpoint.x < term->selstart.x + term->selend.x)
4230 term->selanchor.x = term->selend.x-1;
4232 term->selanchor.x = term->selstart.x;
4234 if (2*selpoint.y < term->selstart.y + term->selend.y)
4235 term->selanchor.y = term->selend.y;
4237 term->selanchor.y = term->selstart.y;
4239 term->selstate = DRAGGING;
4241 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
4242 term->selanchor = selpoint;
4243 term->selstate = DRAGGING;
4244 if (term->seltype == LEXICOGRAPHIC) {
4246 * For normal selection, we set (selstart,selend) to
4247 * (selpoint,selanchor) in some order.
4249 if (poslt(selpoint, term->selanchor)) {
4250 term->selstart = selpoint;
4251 term->selend = term->selanchor;
4252 incpos(term->selend);
4254 term->selstart = term->selanchor;
4255 term->selend = selpoint;
4256 incpos(term->selend);
4260 * For rectangular selection, we may need to
4261 * interchange x and y coordinates (if the user has
4262 * dragged in the -x and +y directions, or vice versa).
4264 term->selstart.x = min(term->selanchor.x, selpoint.x);
4265 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
4266 term->selstart.y = min(term->selanchor.y, selpoint.y);
4267 term->selend.y = max(term->selanchor.y, selpoint.y);
4270 } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&
4272 if (term->selstate == DRAGGING) {
4274 * We've completed a selection. We now transfer the
4275 * data to the clipboard.
4277 clipme(term, term->selstart, term->selend,
4278 (term->seltype == RECTANGULAR), FALSE);
4279 term->selstate = SELECTED;
4281 term->selstate = NO_SELECTION;
4282 } else if (bcooked == MBT_PASTE
4284 #if MULTICLICK_ONLY_EVENT
4285 || a == MA_2CLK || a == MA_3CLK
4288 request_paste(term->frontend);
4294 void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen,
4295 unsigned int modifiers, unsigned int flags)
4299 int prependesc = FALSE;
4303 fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen);
4304 for (i = 0; i < tlen; i++)
4305 fprintf(stderr, " %04x", (unsigned)text[i]);
4306 fprintf(stderr, "\n");
4310 if ((flags & PKF_REPEAT) && term->repeat_off)
4313 /* Currently, Meta always just prefixes everything with ESC. */
4314 if (modifiers & PKM_META)
4316 modifiers &= ~PKM_META;
4319 * Alt is only used for Alt+keypad, which isn't supported yet, so
4322 modifiers &= ~PKM_ALT;
4324 /* Standard local function keys */
4325 switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) {
4327 if (keysym == PK_PAGEUP)
4328 /* scroll up one page */;
4329 if (keysym == PK_PAGEDOWN)
4330 /* scroll down on page */;
4331 if (keysym == PK_INSERT)
4332 term_do_paste(term);
4335 if (keysym == PK_PAGEUP)
4336 /* scroll up one line */;
4337 if (keysym == PK_PAGEDOWN)
4338 /* scroll down one line */;
4339 /* Control-Numlock for app-keypad mode switch */
4340 if (keysym == PK_PF1)
4341 term->app_keypad_keys ^= 1;
4345 if (modifiers & PKM_ALT) {
4346 /* Alt+F4 (close) */
4347 /* Alt+Return (full screen) */
4348 /* Alt+Space (system menu) */
4351 if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 &&
4352 text[0] >= 0x20 && text[0] <= 0x7e) {
4353 /* ASCII chars + Control */
4354 if ((text[0] >= 0x40 && text[0] <= 0x5f) ||
4355 (text[0] >= 0x61 && text[0] <= 0x7a))
4359 * Control-2 should return ^@ (0x00), Control-6 should return
4360 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
4361 * the DOS keyboard handling did it, and we have nothing better
4362 * to do with the key combo in question, we'll also map
4363 * Control-Backquote to ^\ (0x1C).
4366 case ' ': text[0] = 0x00; break;
4367 case '-': text[0] = 0x1f; break;
4368 case '/': text[0] = 0x1f; break;
4369 case '2': text[0] = 0x00; break;
4370 case '3': text[0] = 0x1b; break;
4371 case '4': text[0] = 0x1c; break;
4372 case '5': text[0] = 0x1d; break;
4373 case '6': text[0] = 0x1e; break;
4374 case '7': text[0] = 0x1f; break;
4375 case '8': text[0] = 0x7f; break;
4376 case '`': text[0] = 0x1c; break;
4381 /* Nethack keypad */
4382 if (term->cfg.nethack_keypad) {
4385 case PK_KP1: c = 'b'; break;
4386 case PK_KP2: c = 'j'; break;
4387 case PK_KP3: c = 'n'; break;
4388 case PK_KP4: c = 'h'; break;
4389 case PK_KP5: c = '.'; break;
4390 case PK_KP6: c = 'l'; break;
4391 case PK_KP7: c = 'y'; break;
4392 case PK_KP8: c = 'k'; break;
4393 case PK_KP9: c = 'u'; break;
4394 default: break; /* else gcc warns `enum value not used' */
4398 if (modifiers & PKM_CONTROL)
4400 else if (modifiers & PKM_SHIFT)
4408 /* Numeric Keypad */
4409 if (PK_ISKEYPAD(keysym)) {
4413 * In VT400 mode, PFn always emits an escape sequence. In
4414 * Linux and tilde modes, this only happens in app keypad mode.
4416 if (term->cfg.funky_type == FUNKY_VT400 ||
4417 ((term->cfg.funky_type == FUNKY_LINUX ||
4418 term->cfg.funky_type == FUNKY_TILDE) &&
4419 term->app_keypad_keys && !term->cfg.no_applic_k)) {
4421 case PK_PF1: xkey = 'P'; break;
4422 case PK_PF2: xkey = 'Q'; break;
4423 case PK_PF3: xkey = 'R'; break;
4424 case PK_PF4: xkey = 'S'; break;
4425 default: break; /* else gcc warns `enum value not used' */
4428 if (term->app_keypad_keys && !term->cfg.no_applic_k) {
4430 case PK_KP0: xkey = 'p'; break;
4431 case PK_KP1: xkey = 'q'; break;
4432 case PK_KP2: xkey = 'r'; break;
4433 case PK_KP3: xkey = 's'; break;
4434 case PK_KP4: xkey = 't'; break;
4435 case PK_KP5: xkey = 'u'; break;
4436 case PK_KP6: xkey = 'v'; break;
4437 case PK_KP7: xkey = 'w'; break;
4438 case PK_KP8: xkey = 'x'; break;
4439 case PK_KP9: xkey = 'y'; break;
4440 case PK_KPDECIMAL: xkey = 'n'; break;
4441 case PK_KPENTER: xkey = 'M'; break;
4442 default: break; /* else gcc warns `enum value not used' */
4444 if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) {
4446 * xterm can't see the layout of the keypad, so it has
4447 * to rely on the X keysyms returned by the keys.
4448 * Hence, we look at the strings here, not the PuTTY
4449 * keysyms (which describe the layout).
4453 if (modifiers & PKM_SHIFT)
4458 case '/': xkey = 'o'; break;
4459 case '*': xkey = 'j'; break;
4460 case '-': xkey = 'm'; break;
4464 * In all other modes, we try to retain the layout of
4465 * the DEC keypad in application mode.
4469 /* This key covers the '-' and ',' keys on a VT220 */
4470 if (modifiers & PKM_SHIFT)
4471 xkey = 'm'; /* VT220 '-' */
4473 xkey = 'l'; /* VT220 ',' */
4475 case PK_KPMINUS: xkey = 'm'; break;
4476 case PK_KPCOMMA: xkey = 'l'; break;
4477 default: break; /* else gcc warns `enum value not used' */
4482 if (term->vt52_mode) {
4483 if (xkey >= 'P' && xkey <= 'S')
4484 p += sprintf((char *) p, "\x1B%c", xkey);
4486 p += sprintf((char *) p, "\x1B?%c", xkey);
4488 p += sprintf((char *) p, "\x1BO%c", xkey);
4491 /* Not in application mode -- treat the number pad as arrow keys? */
4492 if ((flags & PKF_NUMLOCK) == 0) {
4494 case PK_KP0: keysym = PK_INSERT; break;
4495 case PK_KP1: keysym = PK_END; break;
4496 case PK_KP2: keysym = PK_DOWN; break;
4497 case PK_KP3: keysym = PK_PAGEDOWN; break;
4498 case PK_KP4: keysym = PK_LEFT; break;
4499 case PK_KP5: keysym = PK_REST; break;
4500 case PK_KP6: keysym = PK_RIGHT; break;
4501 case PK_KP7: keysym = PK_HOME; break;
4502 case PK_KP8: keysym = PK_UP; break;
4503 case PK_KP9: keysym = PK_PAGEUP; break;
4504 default: break; /* else gcc warns `enum value not used' */
4509 /* Miscellaneous keys */
4516 *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08);
4517 else if (modifiers == PKM_SHIFT)
4518 /* We do the opposite of what is configured */
4519 *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F);
4525 else if (modifiers == PKM_SHIFT)
4526 *p++ = 0x1B, *p++ = '[', *p++ = 'Z';
4529 /* XXX window.c has ctrl+shift+space sending 0xa0 */
4531 if (modifiers == PKM_CONTROL)
4536 case PK_KPENTER: /* Odd keypad modes handled above */
4537 if (modifiers == 0) {
4539 if (term->cr_lf_return)
4543 default: break; /* else gcc warns `enum value not used' */
4546 /* SCO function keys and editing keys */
4547 if (term->cfg.funky_type == FUNKY_SCO) {
4548 if (PK_ISFKEY(keysym) && keysym <= PK_F12) {
4549 static char const codes[] =
4550 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
4551 int index = keysym - PK_F1;
4553 if (modifiers & PKM_SHIFT) index += 12;
4554 if (modifiers & PKM_CONTROL) index += 24;
4555 p += sprintf((char *) p, "\x1B[%c", codes[index]);
4558 if (PK_ISEDITING(keysym)) {
4562 case PK_DELETE: *p++ = 0x7f; goto done;
4563 case PK_HOME: xkey = 'H'; break;
4564 case PK_INSERT: xkey = 'L'; break;
4565 case PK_END: xkey = 'F'; break;
4566 case PK_PAGEUP: xkey = 'I'; break;
4567 case PK_PAGEDOWN: xkey = 'G'; break;
4568 default: break; /* else gcc warns `enum value not used' */
4570 p += sprintf((char *) p, "\x1B[%c", xkey);
4574 if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) {
4577 if (term->cfg.funky_type == FUNKY_XTERM) {
4578 /* Xterm shuffles these keys, apparently. */
4580 case PK_HOME: keysym = PK_INSERT; break;
4581 case PK_INSERT: keysym = PK_HOME; break;
4582 case PK_DELETE: keysym = PK_END; break;
4583 case PK_END: keysym = PK_PAGEUP; break;
4584 case PK_PAGEUP: keysym = PK_DELETE; break;
4585 case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break;
4586 default: break; /* else gcc warns `enum value not used' */
4591 if (term->cfg.rxvt_homeend &&
4592 (keysym == PK_HOME || keysym == PK_END)) {
4593 p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw");
4597 if (term->vt52_mode) {
4601 * A real VT52 doesn't have these, and a VT220 doesn't
4602 * send anything for them in VT52 mode.
4605 case PK_HOME: xkey = 'H'; break;
4606 case PK_INSERT: xkey = 'L'; break;
4607 case PK_DELETE: xkey = 'M'; break;
4608 case PK_END: xkey = 'E'; break;
4609 case PK_PAGEUP: xkey = 'I'; break;
4610 case PK_PAGEDOWN: xkey = 'G'; break;
4611 default: xkey=0; break; /* else gcc warns `enum value not used'*/
4613 p += sprintf((char *) p, "\x1B%c", xkey);
4618 case PK_HOME: code = 1; break;
4619 case PK_INSERT: code = 2; break;
4620 case PK_DELETE: code = 3; break;
4621 case PK_END: code = 4; break;
4622 case PK_PAGEUP: code = 5; break;
4623 case PK_PAGEDOWN: code = 6; break;
4624 default: code = 0; break; /* else gcc warns `enum value not used' */
4626 p += sprintf((char *) p, "\x1B[%d~", code);
4630 if (PK_ISFKEY(keysym)) {
4631 /* Map Shift+F1-F10 to F11-F20 */
4632 if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT))
4634 if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) &&
4636 /* XXX This overrides the XTERM/VT52 mode below */
4638 if (keysym >= PK_F6) offt++;
4639 if (keysym >= PK_F12) offt++;
4640 p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c",
4641 'P' + keysym - PK_F1 - offt);
4644 if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) {
4645 p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1);
4648 if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) {
4649 if (term->vt52_mode)
4650 p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1);
4652 p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1);
4655 p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1);
4659 if (PK_ISCURSOR(keysym)) {
4663 case PK_UP: xkey = 'A'; break;
4664 case PK_DOWN: xkey = 'B'; break;
4665 case PK_RIGHT: xkey = 'C'; break;
4666 case PK_LEFT: xkey = 'D'; break;
4667 case PK_REST: xkey = 'G'; break; /* centre key on number pad */
4668 default: xkey = 0; break; /* else gcc warns `enum value not used' */
4670 if (term->vt52_mode)
4671 p += sprintf((char *) p, "\x1B%c", xkey);
4673 int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c);
4675 /* Useful mapping of Ctrl-arrows */
4676 if (modifiers == PKM_CONTROL)
4680 p += sprintf((char *) p, "\x1BO%c", xkey);
4682 p += sprintf((char *) p, "\x1B[%c", xkey);
4688 if (p > output || tlen > 0) {
4690 * Interrupt an ongoing paste. I'm not sure
4691 * this is sensible, but for the moment it's
4692 * preferable to having to faff about buffering
4698 * We need not bother about stdin backlogs
4699 * here, because in GUI PuTTY we can't do
4700 * anything about it anyway; there's no means
4701 * of asking Windows to hold off on KEYDOWN
4702 * messages. We _have_ to buffer everything
4705 term_seen_key_event(term);
4709 fprintf(stderr, "sending ESC\n");
4711 ldisc_send(term->ldisc, "\x1b", 1, 1);
4716 fprintf(stderr, "sending %d bytes:", p - output);
4717 for (i = 0; i < p - output; i++)
4718 fprintf(stderr, " %02x", output[i]);
4719 fprintf(stderr, "\n");
4721 ldisc_send(term->ldisc, output, p - output, 1);
4722 } else if (tlen > 0) {
4724 fprintf(stderr, "sending %d unichars:", tlen);
4725 for (i = 0; i < tlen; i++)
4726 fprintf(stderr, " %04x", (unsigned) text[i]);
4727 fprintf(stderr, "\n");
4729 luni_send(term->ldisc, text, tlen, 1);
4734 void term_nopaste(Terminal *term)
4736 if (term->paste_len == 0)
4738 sfree(term->paste_buffer);
4739 term->paste_buffer = NULL;
4740 term->paste_len = 0;
4743 int term_paste_pending(Terminal *term)
4745 return term->paste_len != 0;
4748 void term_paste(Terminal *term)
4750 long now, paste_diff;
4752 if (term->paste_len == 0)
4755 /* Don't wait forever to paste */
4756 if (term->paste_hold) {
4757 now = GETTICKCOUNT();
4758 paste_diff = now - term->last_paste;
4759 if (paste_diff >= 0 && paste_diff < 450)
4762 term->paste_hold = 0;
4764 while (term->paste_pos < term->paste_len) {
4766 while (n + term->paste_pos < term->paste_len) {
4767 if (term->paste_buffer[term->paste_pos + n++] == '\015')
4771 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
4772 term->paste_pos += n;
4774 if (term->paste_pos < term->paste_len) {
4775 term->paste_hold = 1;
4779 sfree(term->paste_buffer);
4780 term->paste_buffer = NULL;
4781 term->paste_len = 0;
4784 static void deselect(Terminal *term)
4786 term->selstate = NO_SELECTION;
4787 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
4790 void term_deselect(Terminal *term)
4796 int term_ldisc(Terminal *term, int option)
4798 if (option == LD_ECHO)
4799 return term->term_echoing;
4800 if (option == LD_EDIT)
4801 return term->term_editing;
4805 int term_data(Terminal *term, int is_stderr, const char *data, int len)
4807 bufchain_add(&term->inbuf, data, len);
4809 if (!term->in_term_out) {
4810 term->in_term_out = TRUE;
4811 term_blink(term, 1);
4813 term->in_term_out = FALSE;
4817 * term_out() always completely empties inbuf. Therefore,
4818 * there's no reason at all to return anything other than zero
4819 * from this function, because there _can't_ be a question of
4820 * the remote side needing to wait until term_out() has cleared
4823 * This is a slightly suboptimal way to deal with SSH2 - in
4824 * principle, the window mechanism would allow us to continue
4825 * to accept data on forwarded ports and X connections even
4826 * while the terminal processing was going slowly - but we
4827 * can't do the 100% right thing without moving the terminal
4828 * processing into a separate thread, and that might hurt
4829 * portability. So we manage stdout buffering the old SSH1 way:
4830 * if the terminal processing goes slowly, the whole SSH
4831 * connection stops accepting data until it's ready.
4833 * In practice, I can't imagine this causing serious trouble.
4838 void term_provide_logctx(Terminal *term, void *logctx)
4840 term->logctx = logctx;