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;
241 /* Allocate temporary buffers for Arabic shaping and bidi. */
242 if (!term->cfg.arabicshaping || !term->cfg.bidi)
244 term->wcFrom = sresize(term->wcFrom, term->cols, bidi_char);
245 term->ltemp = sresize(term->ltemp, term->cols+1, unsigned long);
246 term->wcTo = sresize(term->wcTo, term->cols, bidi_char);
249 if (need_sbar_update)
251 do_paint(term, ctx, TRUE);
252 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
258 * Called from front end when a keypress occurs, to trigger
259 * anything magical that needs to happen in that situation.
261 void term_seen_key_event(Terminal *term)
264 * On any keypress, clear the bell overload mechanism
265 * completely, on the grounds that large numbers of
266 * beeps coming from deliberate key action are likely
267 * to be intended (e.g. beeps from filename completion
268 * blocking repeatedly).
270 term->beep_overloaded = FALSE;
271 while (term->beephead) {
272 struct beeptime *tmp = term->beephead;
273 term->beephead = tmp->next;
276 term->beeptail = NULL;
280 * Reset the scrollback on keypress, if we're doing that.
282 if (term->cfg.scroll_on_key) {
283 term->disptop = 0; /* return to main screen */
284 term->seen_disp_event = 1;
289 * Same as power_on(), but an external function.
291 void term_pwron(Terminal *term)
294 if (term->ldisc) /* cause ldisc to notice changes */
295 ldisc_send(term->ldisc, NULL, 0, 0);
303 * When the user reconfigures us, we need to check the forbidden-
304 * alternate-screen config option, disable raw mouse mode if the
305 * user has disabled mouse reporting, and abandon a print job if
306 * the user has disabled printing.
308 void term_reconfig(Terminal *term, Config *cfg)
311 * Before adopting the new config, check all those terminal
312 * settings which control power-on defaults; and if they've
313 * changed, we will modify the current state as well as the
314 * default one. The full list is: Auto wrap mode, DEC Origin
315 * Mode, BCE, blinking text, character classes.
317 int reset_wrap, reset_decom, reset_bce, reset_blink, reset_charclass;
320 reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode);
321 reset_decom = (term->cfg.dec_om != cfg->dec_om);
322 reset_bce = (term->cfg.bce != cfg->bce);
323 reset_blink = (term->cfg.blinktext != cfg->blinktext);
325 for (i = 0; i < lenof(term->cfg.wordness); i++)
326 if (term->cfg.wordness[i] != cfg->wordness[i])
329 term->cfg = *cfg; /* STRUCTURE COPY */
332 term->alt_wrap = term->wrap = term->cfg.wrap_mode;
334 term->alt_om = term->dec_om = term->cfg.dec_om;
336 term->use_bce = term->cfg.bce;
338 term->erase_char = (' ' | ATTR_ASCII |
340 (ATTR_FGMASK | ATTR_BGMASK)));
342 term->erase_char = ERASE_CHAR;
345 term->blink_is_real = term->cfg.blinktext;
347 for (i = 0; i < 256; i++)
348 term->wordness[i] = term->cfg.wordness[i];
350 if (term->cfg.no_alt_screen)
351 swap_screen(term, 0, FALSE, FALSE);
352 if (term->cfg.no_mouse_rep) {
353 term->xterm_mouse = 0;
354 set_raw_mouse_mode(term->frontend, 0);
356 if (term->cfg.no_remote_charset) {
357 term->cset_attr[0] = term->cset_attr[1] = ATTR_ASCII;
358 term->sco_acs = term->alt_sco_acs = 0;
361 if (!*term->cfg.printer) {
362 term_print_finish(term);
367 * Clear the scrollback.
369 void term_clrsb(Terminal *term)
373 while ((line = delpos234(term->scrollback, 0)) != NULL) {
376 term->tempsblines = 0;
377 term->alt_sblines = 0;
382 * Initialise the terminal.
384 Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata,
390 * Allocate a new Terminal structure and initialise the fields
393 term = snew(Terminal);
394 term->frontend = frontend;
395 term->ucsdata = ucsdata;
396 term->cfg = *mycfg; /* STRUCTURE COPY */
398 term->compatibility_level = TM_PUTTY;
399 strcpy(term->id_string, "\033[?6c");
400 term->last_blink = term->last_tblink = 0;
401 term->paste_buffer = NULL;
403 term->last_paste = 0;
404 bufchain_init(&term->inbuf);
405 bufchain_init(&term->printer_buf);
406 term->printing = term->only_printing = FALSE;
407 term->print_job = NULL;
408 term->vt52_mode = FALSE;
409 term->cr_lf_return = FALSE;
410 term->seen_disp_event = FALSE;
411 term->xterm_mouse = term->mouse_is_down = FALSE;
412 term->reset_132 = FALSE;
413 term->blinker = term->tblinker = 0;
415 term->repeat_off = FALSE;
416 term->termstate = TOPLEVEL;
417 term->selstate = NO_SELECTION;
420 term->screen = term->alt_screen = term->scrollback = NULL;
421 term->tempsblines = 0;
422 term->alt_sblines = 0;
424 term->disptext = term->dispcurs = NULL;
427 term->rows = term->cols = -1;
429 term->beephead = term->beeptail = NULL;
430 #ifdef OPTIMISE_SCROLL
431 term->scrollhead = term->scrolltail = NULL;
432 #endif /* OPTIMISE_SCROLL */
434 term->lastbeep = FALSE;
435 term->beep_overloaded = FALSE;
436 term->attr_mask = 0xffffffff;
437 term->resize_fn = NULL;
438 term->resize_ctx = NULL;
439 term->in_term_out = FALSE;
444 term->bidi_cache_size = 0;
445 term->pre_bidi_cache = term->post_bidi_cache = NULL;
450 void term_free(Terminal *term)
453 struct beeptime *beep;
456 while ((line = delpos234(term->scrollback, 0)) != NULL)
458 freetree234(term->scrollback);
459 while ((line = delpos234(term->screen, 0)) != NULL)
461 freetree234(term->screen);
462 while ((line = delpos234(term->alt_screen, 0)) != NULL)
464 freetree234(term->alt_screen);
465 sfree(term->disptext);
466 while (term->beephead) {
467 beep = term->beephead;
468 term->beephead = beep->next;
471 bufchain_clear(&term->inbuf);
473 printer_finish_job(term->print_job);
474 bufchain_clear(&term->printer_buf);
475 sfree(term->paste_buffer);
480 for (i = 0; i < term->bidi_cache_size; i++) {
481 sfree(term->pre_bidi_cache[i]);
482 sfree(term->post_bidi_cache[i]);
484 sfree(term->pre_bidi_cache);
485 sfree(term->post_bidi_cache);
491 * Set up the terminal for a given size.
493 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
496 unsigned long *newdisp, *line;
499 int save_alt_which = term->alt_which;
501 if (newrows == term->rows && newcols == term->cols &&
502 newsavelines == term->savelines)
503 return; /* nothing to do */
506 swap_screen(term, 0, FALSE, FALSE);
508 term->alt_t = term->marg_t = 0;
509 term->alt_b = term->marg_b = newrows - 1;
511 if (term->rows == -1) {
512 term->scrollback = newtree234(NULL);
513 term->screen = newtree234(NULL);
514 term->tempsblines = 0;
519 * Resize the screen and scrollback. We only need to shift
520 * lines around within our data structures, because lineptr()
521 * will take care of resizing each individual line if
524 * - If the new screen is longer, we shunt lines in from temporary
525 * scrollback if possible, otherwise we add new blank lines at
528 * - If the new screen is shorter, we remove any blank lines at
529 * the bottom if possible, otherwise shunt lines above the cursor
530 * to scrollback if possible, otherwise delete lines below the
533 * - Then, if the new scrollback length is less than the
534 * amount of scrollback we actually have, we must throw some
537 sblen = count234(term->scrollback);
538 /* Do this loop to expand the screen if newrows > rows */
539 assert(term->rows == count234(term->screen));
540 while (term->rows < newrows) {
541 if (term->tempsblines > 0) {
542 /* Insert a line from the scrollback at the top of the screen. */
543 assert(sblen >= term->tempsblines);
544 line = delpos234(term->scrollback, --sblen);
545 term->tempsblines -= 1;
546 addpos234(term->screen, line, 0);
548 term->savecurs.y += 1;
550 /* Add a new blank line at the bottom of the screen. */
551 line = snewn(newcols + 2, TTYPE);
553 for (j = 0; j < newcols; j++)
554 line[j + 1] = ERASE_CHAR;
555 line[newcols + 1] = LATTR_NORM;
556 addpos234(term->screen, line, count234(term->screen));
560 /* Do this loop to shrink the screen if newrows < rows */
561 while (term->rows > newrows) {
562 if (term->curs.y < term->rows - 1) {
563 /* delete bottom row, unless it contains the cursor */
564 sfree(delpos234(term->screen, term->rows - 1));
566 /* push top row to scrollback */
567 line = delpos234(term->screen, 0);
568 addpos234(term->scrollback, line, sblen++);
569 term->tempsblines += 1;
571 term->savecurs.y -= 1;
575 assert(term->rows == newrows);
576 assert(count234(term->screen) == newrows);
578 /* Delete any excess lines from the scrollback. */
579 while (sblen > newsavelines) {
580 line = delpos234(term->scrollback, 0);
584 if (sblen < term->tempsblines)
585 term->tempsblines = sblen;
586 assert(count234(term->scrollback) <= newsavelines);
587 assert(count234(term->scrollback) >= term->tempsblines);
590 /* Make a new displayed text buffer. */
591 newdisp = snewn(newrows * (newcols + 1), TTYPE);
592 for (i = 0; i < newrows * (newcols + 1); i++)
593 newdisp[i] = ATTR_INVALID;
594 sfree(term->disptext);
595 term->disptext = newdisp;
596 term->dispcurs = NULL;
598 /* Make a new alternate screen. */
599 newalt = newtree234(NULL);
600 for (i = 0; i < newrows; i++) {
601 line = snewn(newcols + 2, TTYPE);
603 for (j = 0; j < newcols; j++)
604 line[j + 1] = term->erase_char;
605 line[newcols + 1] = LATTR_NORM;
606 addpos234(newalt, line, i);
608 if (term->alt_screen) {
609 while (NULL != (line = delpos234(term->alt_screen, 0)))
611 freetree234(term->alt_screen);
613 term->alt_screen = newalt;
614 term->alt_sblines = 0;
616 term->tabs = sresize(term->tabs, newcols, unsigned char);
619 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
620 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
623 /* Check that the cursor positions are still valid. */
624 if (term->savecurs.y < 0)
625 term->savecurs.y = 0;
626 if (term->savecurs.y >= newrows)
627 term->savecurs.y = newrows - 1;
628 if (term->curs.y < 0)
630 if (term->curs.y >= newrows)
631 term->curs.y = newrows - 1;
632 if (term->curs.x >= newcols)
633 term->curs.x = newcols - 1;
634 term->alt_x = term->alt_y = 0;
635 term->wrapnext = term->alt_wnext = FALSE;
637 term->rows = newrows;
638 term->cols = newcols;
639 term->savelines = newsavelines;
642 swap_screen(term, save_alt_which, FALSE, FALSE);
647 term->resize_fn(term->resize_ctx, term->cols, term->rows);
651 * Hand a function and context pointer to the terminal which it can
652 * use to notify a back end of resizes.
654 void term_provide_resize_fn(Terminal *term,
655 void (*resize_fn)(void *, int, int),
658 term->resize_fn = resize_fn;
659 term->resize_ctx = resize_ctx;
660 if (term->cols > 0 && term->rows > 0)
661 resize_fn(resize_ctx, term->cols, term->rows);
664 /* Find the bottom line on the screen that has any content.
665 * If only the top line has content, returns 0.
666 * If no lines have content, return -1.
668 static int find_last_nonempty_line(Terminal * term, tree234 * screen)
671 for (i = count234(screen) - 1; i >= 0; i--) {
672 unsigned long *line = index234(screen, i);
675 for (j = 0; j < cols; j++) {
676 if (line[j + 1] != term->erase_char) break;
678 if (j != cols) break;
684 * Swap screens. If `reset' is TRUE and we have been asked to
685 * switch to the alternate screen, we must bring most of its
686 * configuration from the main screen and erase the contents of the
687 * alternate screen completely. (This is even true if we're already
688 * on it! Blame xterm.)
690 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
696 reset = FALSE; /* do no weird resetting if which==0 */
698 if (which != term->alt_which) {
699 term->alt_which = which;
701 ttr = term->alt_screen;
702 term->alt_screen = term->screen;
704 term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1;
706 if (!reset && !keep_cur_pos)
707 term->curs.x = term->alt_x;
710 if (!reset && !keep_cur_pos)
711 term->curs.y = term->alt_y;
714 if (!reset) term->marg_t = term->alt_t;
717 if (!reset) term->marg_b = term->alt_b;
720 if (!reset) term->dec_om = term->alt_om;
723 if (!reset) term->wrap = term->alt_wrap;
726 if (!reset) term->wrapnext = term->alt_wnext;
729 if (!reset) term->insert = term->alt_ins;
732 if (!reset) term->cset = term->alt_cset;
735 if (!reset) term->utf = term->alt_utf;
738 if (!reset) term->sco_acs = term->alt_sco_acs;
739 term->alt_sco_acs = t;
742 if (reset && term->screen) {
744 * Yes, this _is_ supposed to honour background-colour-erase.
746 erase_lots(term, FALSE, TRUE, TRUE);
750 * This might not be possible if we're called during
758 * Update the scroll bar.
760 static void update_sbar(Terminal *term)
762 int nscroll = sblines(term);
763 set_sbar(term->frontend, nscroll + term->rows,
764 nscroll + term->disptop, term->rows);
768 * Check whether the region bounded by the two pointers intersects
769 * the scroll region, and de-select the on-screen selection if so.
771 static void check_selection(Terminal *term, pos from, pos to)
773 if (poslt(from, term->selend) && poslt(term->selstart, to))
778 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
779 * for backward.) `sb' is TRUE if the scrolling is permitted to
780 * affect the scrollback buffer.
782 * NB this function invalidates all pointers into lines of the
783 * screen data structures. In particular, you MUST call fix_cpos
784 * after calling scroll() and before doing anything else that
785 * uses the cpos shortcut pointer.
787 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
789 unsigned long *line, *line2;
790 int i, seltop, olddisptop, shift;
792 if (topline != 0 || term->alt_which != 0)
795 olddisptop = term->disptop;
799 line = delpos234(term->screen, botline);
800 line = resizeline(line, term->cols);
801 for (i = 0; i < term->cols; i++)
802 line[i + 1] = term->erase_char;
803 line[term->cols + 1] = 0;
804 addpos234(term->screen, line, topline);
806 if (term->selstart.y >= topline && term->selstart.y <= botline) {
808 if (term->selstart.y > botline) {
809 term->selstart.y = botline + 1;
810 term->selstart.x = 0;
813 if (term->selend.y >= topline && term->selend.y <= botline) {
815 if (term->selend.y > botline) {
816 term->selend.y = botline + 1;
825 line = delpos234(term->screen, topline);
826 if (sb && term->savelines > 0) {
827 int sblen = count234(term->scrollback);
829 * We must add this line to the scrollback. We'll
830 * remove a line from the top of the scrollback to
831 * replace it, or allocate a new one if the
832 * scrollback isn't full.
834 if (sblen == term->savelines) {
835 sblen--, line2 = delpos234(term->scrollback, 0);
837 line2 = snewn(term->cols + 2, TTYPE);
838 line2[0] = term->cols;
839 term->tempsblines += 1;
841 addpos234(term->scrollback, line, sblen);
845 * If the user is currently looking at part of the
846 * scrollback, and they haven't enabled any options
847 * that are going to reset the scrollback as a
848 * result of this movement, then the chances are
849 * they'd like to keep looking at the same line. So
850 * we move their viewpoint at the same rate as the
851 * scroll, at least until their viewpoint hits the
852 * top end of the scrollback buffer, at which point
853 * we don't have the choice any more.
855 * Thanks to Jan Holmen Holsten for the idea and
856 * initial implementation.
858 if (term->disptop > -term->savelines && term->disptop < 0)
861 line = resizeline(line, term->cols);
862 for (i = 0; i < term->cols; i++)
863 line[i + 1] = term->erase_char;
864 line[term->cols + 1] = LATTR_NORM;
865 addpos234(term->screen, line, botline);
868 * If the selection endpoints move into the scrollback,
869 * we keep them moving until they hit the top. However,
870 * of course, if the line _hasn't_ moved into the
871 * scrollback then we don't do this, and cut them off
872 * at the top of the scroll region.
874 * This applies to selstart and selend (for an existing
875 * selection), and also selanchor (for one being
876 * selected as we speak).
878 seltop = sb ? -term->savelines : topline;
880 if (term->selstate != NO_SELECTION) {
881 if (term->selstart.y >= seltop &&
882 term->selstart.y <= botline) {
884 if (term->selstart.y < seltop) {
885 term->selstart.y = seltop;
886 term->selstart.x = 0;
889 if (term->selend.y >= seltop && term->selend.y <= botline) {
891 if (term->selend.y < seltop) {
892 term->selend.y = seltop;
896 if (term->selanchor.y >= seltop &&
897 term->selanchor.y <= botline) {
899 if (term->selanchor.y < seltop) {
900 term->selanchor.y = seltop;
901 term->selanchor.x = 0;
909 #ifdef OPTIMISE_SCROLL
910 shift += term->disptop - olddisptop;
911 if (shift < term->rows && shift > -term->rows && shift != 0)
912 scroll_display(term, topline, botline, shift);
913 #endif /* OPTIMISE_SCROLL */
916 #ifdef OPTIMISE_SCROLL
918 * Add a scroll of a region on the screen into the pending scroll list.
919 * `lines' is +ve for scrolling forward, -ve for backward.
921 * If the scroll is on the same area as the last scroll in the list,
924 static void save_scroll(Terminal *term, int topline, int botline, int lines)
926 struct scrollregion *newscroll;
927 if (term->scrolltail &&
928 term->scrolltail->topline == topline &&
929 term->scrolltail->botline == botline) {
930 term->scrolltail->lines += lines;
932 newscroll = snew(struct scrollregion);
933 newscroll->topline = topline;
934 newscroll->botline = botline;
935 newscroll->lines = lines;
936 newscroll->next = NULL;
938 if (!term->scrollhead)
939 term->scrollhead = newscroll;
941 term->scrolltail->next = newscroll;
942 term->scrolltail = newscroll;
947 * Scroll the physical display, and our conception of it in disptext.
949 static void scroll_display(Terminal *term, int topline, int botline, int lines)
951 unsigned long *start, *end;
952 int distance, size, i;
954 start = term->disptext + topline * (term->cols + 1);
955 end = term->disptext + (botline + 1) * (term->cols + 1);
956 distance = (lines > 0 ? lines : -lines) * (term->cols + 1);
957 size = end - start - distance;
959 memmove(start, start + distance, size * TSIZE);
960 if (term->dispcurs >= start + distance &&
961 term->dispcurs <= start + distance + size)
962 term->dispcurs -= distance;
963 for (i = 0; i < distance; i++)
964 (start + size)[i] |= ATTR_INVALID;
966 memmove(start + distance, start, size * TSIZE);
967 if (term->dispcurs >= start && term->dispcurs <= start + size)
968 term->dispcurs += distance;
969 for (i = 0; i < distance; i++)
970 start[i] |= ATTR_INVALID;
972 save_scroll(term, topline, botline, lines);
974 #endif /* OPTIMISE_SCROLL */
977 * Move the cursor to a given position, clipping at boundaries. We
978 * may or may not want to clip at the scroll margin: marg_clip is 0
979 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
980 * even _being_ outside the margins.
982 static void move(Terminal *term, int x, int y, int marg_clip)
989 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
992 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
1003 term->wrapnext = FALSE;
1007 * Save or restore the cursor and SGR mode.
1009 static void save_cursor(Terminal *term, int save)
1012 term->savecurs = term->curs;
1013 term->save_attr = term->curr_attr;
1014 term->save_cset = term->cset;
1015 term->save_utf = term->utf;
1016 term->save_wnext = term->wrapnext;
1017 term->save_csattr = term->cset_attr[term->cset];
1018 term->save_sco_acs = term->sco_acs;
1020 term->curs = term->savecurs;
1021 /* Make sure the window hasn't shrunk since the save */
1022 if (term->curs.x >= term->cols)
1023 term->curs.x = term->cols - 1;
1024 if (term->curs.y >= term->rows)
1025 term->curs.y = term->rows - 1;
1027 term->curr_attr = term->save_attr;
1028 term->cset = term->save_cset;
1029 term->utf = term->save_utf;
1030 term->wrapnext = term->save_wnext;
1032 * wrapnext might reset to False if the x position is no
1033 * longer at the rightmost edge.
1035 if (term->wrapnext && term->curs.x < term->cols-1)
1036 term->wrapnext = FALSE;
1037 term->cset_attr[term->cset] = term->save_csattr;
1038 term->sco_acs = term->save_sco_acs;
1041 term->erase_char = (' ' | ATTR_ASCII |
1043 (ATTR_FGMASK | ATTR_BGMASK)));
1048 * This function is called before doing _anything_ which affects
1049 * only part of a line of text. It is used to mark the boundary
1050 * between two character positions, and it indicates that some sort
1051 * of effect is going to happen on only one side of that boundary.
1053 * The effect of this function is to check whether a CJK
1054 * double-width character is straddling the boundary, and to remove
1055 * it and replace it with two spaces if so. (Of course, one or
1056 * other of those spaces is then likely to be replaced with
1057 * something else again, as a result of whatever happens next.)
1059 * Also, if the boundary is at the right-hand _edge_ of the screen,
1060 * it implies something deliberate is being done to the rightmost
1061 * column position; hence we must clear LATTR_WRAPPED2.
1063 * The input to the function is the coordinates of the _second_
1064 * character of the pair.
1066 static void check_boundary(Terminal *term, int x, int y)
1068 unsigned long *ldata;
1070 /* Validate input coordinates, just in case. */
1071 if (x == 0 || x > term->cols)
1075 if (x == term->cols) {
1076 ldata[x] &= ~LATTR_WRAPPED2;
1078 if ((ldata[x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE) {
1079 ldata[x-1] = ldata[x] =
1080 (ldata[x-1] &~ (CHAR_MASK | CSET_MASK)) | ATTR_ASCII | ' ';
1086 * Erase a large portion of the screen: the whole screen, or the
1087 * whole line, or parts thereof.
1089 static void erase_lots(Terminal *term,
1090 int line_only, int from_begin, int to_end)
1094 int erasing_lines_from_top = 0;
1097 start.y = term->curs.y;
1099 end.y = term->curs.y + 1;
1101 erase_lattr = FALSE;
1116 if (!from_begin || !to_end)
1117 check_boundary(term, term->curs.x, term->curs.y);
1118 check_selection(term, start, end);
1120 /* Clear screen also forces a full window redraw, just in case. */
1121 if (start.y == 0 && start.x == 0 && end.y == term->rows)
1122 term_invalidate(term);
1124 /* Lines scrolled away shouldn't be brought back on if the terminal
1126 if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr)
1127 erasing_lines_from_top = 1;
1129 if (term->cfg.erase_to_scrollback && erasing_lines_from_top) {
1130 /* If it's a whole number of lines, starting at the top, and
1131 * we're fully erasing them, erase by scrolling and keep the
1132 * lines in the scrollback. */
1133 int scrolllines = end.y;
1134 if (end.y == term->rows) {
1135 /* Shrink until we find a non-empty row.*/
1136 scrolllines = find_last_nonempty_line(term, term->screen) + 1;
1138 if (scrolllines > 0)
1139 scroll(term, 0, scrolllines - 1, scrolllines, TRUE);
1142 unsigned long *ldata = lineptr(start.y);
1143 while (poslt(start, end)) {
1144 if (start.x == term->cols) {
1146 ldata[start.x] &= ~(LATTR_WRAPPED | LATTR_WRAPPED2);
1148 ldata[start.x] = LATTR_NORM;
1150 ldata[start.x] = term->erase_char;
1152 if (incpos(start) && start.y < term->rows)
1153 ldata = lineptr(start.y);
1157 /* After an erase of lines from the top of the screen, we shouldn't
1158 * bring the lines back again if the terminal enlarges (since the user or
1159 * application has explictly thrown them away). */
1160 if (erasing_lines_from_top && !(term->alt_which))
1161 term->tempsblines = 0;
1165 * Insert or delete characters within the current line. n is +ve if
1166 * insertion is desired, and -ve for deletion.
1168 static void insch(Terminal *term, int n)
1170 int dir = (n < 0 ? -1 : +1);
1173 unsigned long *ldata;
1175 n = (n < 0 ? -n : n);
1176 if (n > term->cols - term->curs.x)
1177 n = term->cols - term->curs.x;
1178 m = term->cols - term->curs.x - n;
1179 cursplus.y = term->curs.y;
1180 cursplus.x = term->curs.x + n;
1181 check_selection(term, term->curs, cursplus);
1182 check_boundary(term, term->curs.x, term->curs.y);
1184 check_boundary(term, term->curs.x + n, term->curs.y);
1185 ldata = lineptr(term->curs.y);
1187 memmove(ldata + term->curs.x, ldata + term->curs.x + n, m * TSIZE);
1189 ldata[term->curs.x + m++] = term->erase_char;
1191 memmove(ldata + term->curs.x + n, ldata + term->curs.x, m * TSIZE);
1193 ldata[term->curs.x + n] = term->erase_char;
1198 * Toggle terminal mode `mode' to state `state'. (`query' indicates
1199 * whether the mode is a DEC private one or a normal one.)
1201 static void toggle_mode(Terminal *term, int mode, int query, int state)
1203 unsigned long ticks;
1207 case 1: /* DECCKM: application cursor keys */
1208 term->app_cursor_keys = state;
1210 case 2: /* DECANM: VT52 mode */
1211 term->vt52_mode = !state;
1212 if (term->vt52_mode) {
1213 term->blink_is_real = FALSE;
1214 term->vt52_bold = FALSE;
1216 term->blink_is_real = term->cfg.blinktext;
1219 case 3: /* DECCOLM: 80/132 columns */
1221 if (!term->cfg.no_remote_resize)
1222 request_resize(term->frontend, state ? 132 : 80, term->rows);
1223 term->reset_132 = state;
1224 term->alt_t = term->marg_t = 0;
1225 term->alt_b = term->marg_b = term->rows - 1;
1226 move(term, 0, 0, 0);
1227 erase_lots(term, FALSE, TRUE, TRUE);
1229 case 5: /* DECSCNM: reverse video */
1231 * Toggle reverse video. If we receive an OFF within the
1232 * visual bell timeout period after an ON, we trigger an
1233 * effective visual bell, so that ESC[?5hESC[?5l will
1234 * always be an actually _visible_ visual bell.
1236 ticks = GETTICKCOUNT();
1237 /* turn off a previous vbell to avoid inconsistencies */
1238 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
1239 term->in_vbell = FALSE;
1240 if (term->rvideo && !state && /* we're turning it off... */
1241 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
1242 /* If there's no vbell timeout already, or this one lasts
1243 * longer, replace vbell_timeout with ours. */
1244 if (!term->in_vbell ||
1245 (term->rvbell_startpoint - term->vbell_startpoint <
1247 term->vbell_startpoint = term->rvbell_startpoint;
1248 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
1249 } else if (!term->rvideo && state) {
1250 /* This is an ON, so we notice the time and save it. */
1251 term->rvbell_startpoint = ticks;
1253 term->rvideo = state;
1254 term->seen_disp_event = TRUE;
1258 case 6: /* DECOM: DEC origin mode */
1259 term->dec_om = state;
1261 case 7: /* DECAWM: auto wrap */
1264 case 8: /* DECARM: auto key repeat */
1265 term->repeat_off = !state;
1267 case 10: /* DECEDM: set local edit mode */
1268 term->term_editing = state;
1269 if (term->ldisc) /* cause ldisc to notice changes */
1270 ldisc_send(term->ldisc, NULL, 0, 0);
1272 case 25: /* DECTCEM: enable/disable cursor */
1273 compatibility2(OTHER, VT220);
1274 term->cursor_on = state;
1275 term->seen_disp_event = TRUE;
1277 case 47: /* alternate screen */
1278 compatibility(OTHER);
1280 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE);
1283 case 1000: /* xterm mouse 1 */
1284 term->xterm_mouse = state ? 1 : 0;
1285 set_raw_mouse_mode(term->frontend, state);
1287 case 1002: /* xterm mouse 2 */
1288 term->xterm_mouse = state ? 2 : 0;
1289 set_raw_mouse_mode(term->frontend, state);
1291 case 1047: /* alternate screen */
1292 compatibility(OTHER);
1294 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE);
1297 case 1048: /* save/restore cursor */
1298 if (!term->cfg.no_alt_screen)
1299 save_cursor(term, state);
1300 if (!state) term->seen_disp_event = TRUE;
1302 case 1049: /* cursor & alternate screen */
1303 if (state && !term->cfg.no_alt_screen)
1304 save_cursor(term, state);
1305 if (!state) term->seen_disp_event = TRUE;
1306 compatibility(OTHER);
1308 swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE);
1309 if (!state && !term->cfg.no_alt_screen)
1310 save_cursor(term, state);
1315 case 4: /* IRM: set insert mode */
1316 compatibility(VT102);
1317 term->insert = state;
1319 case 12: /* SRM: set echo mode */
1320 term->term_echoing = !state;
1321 if (term->ldisc) /* cause ldisc to notice changes */
1322 ldisc_send(term->ldisc, NULL, 0, 0);
1324 case 20: /* LNM: Return sends ... */
1325 term->cr_lf_return = state;
1327 case 34: /* WYULCURM: Make cursor BIG */
1328 compatibility2(OTHER, VT220);
1329 term->big_cursor = !state;
1334 * Process an OSC sequence: set window title or icon name.
1336 static void do_osc(Terminal *term)
1339 while (term->osc_strlen--)
1340 term->wordness[(unsigned char)
1341 term->osc_string[term->osc_strlen]] = term->esc_args[0];
1343 term->osc_string[term->osc_strlen] = '\0';
1344 switch (term->esc_args[0]) {
1347 if (!term->cfg.no_remote_wintitle)
1348 set_icon(term->frontend, term->osc_string);
1349 if (term->esc_args[0] == 1)
1351 /* fall through: parameter 0 means set both */
1354 if (!term->cfg.no_remote_wintitle)
1355 set_title(term->frontend, term->osc_string);
1362 * ANSI printing routines.
1364 static void term_print_setup(Terminal *term)
1366 bufchain_clear(&term->printer_buf);
1367 term->print_job = printer_start_job(term->cfg.printer);
1369 static void term_print_flush(Terminal *term)
1374 while ((size = bufchain_size(&term->printer_buf)) > 5) {
1375 bufchain_prefix(&term->printer_buf, &data, &len);
1378 printer_job_data(term->print_job, data, len);
1379 bufchain_consume(&term->printer_buf, len);
1382 static void term_print_finish(Terminal *term)
1388 if (!term->printing && !term->only_printing)
1389 return; /* we need do nothing */
1391 term_print_flush(term);
1392 while ((size = bufchain_size(&term->printer_buf)) > 0) {
1393 bufchain_prefix(&term->printer_buf, &data, &len);
1395 if (c == '\033' || c == '\233') {
1396 bufchain_consume(&term->printer_buf, size);
1399 printer_job_data(term->print_job, &c, 1);
1400 bufchain_consume(&term->printer_buf, 1);
1403 printer_finish_job(term->print_job);
1404 term->print_job = NULL;
1405 term->printing = term->only_printing = FALSE;
1409 * Remove everything currently in `inbuf' and stick it up on the
1410 * in-memory display. There's a big state machine in here to
1411 * process escape sequences...
1413 void term_out(Terminal *term)
1416 unsigned char localbuf[256], *chars;
1421 chars = NULL; /* placate compiler warnings */
1422 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
1426 bufchain_prefix(&term->inbuf, &ret, &nchars);
1427 if (nchars > sizeof(localbuf))
1428 nchars = sizeof(localbuf);
1429 memcpy(localbuf, ret, nchars);
1430 bufchain_consume(&term->inbuf, nchars);
1432 assert(chars != NULL);
1438 * Optionally log the session traffic to a file. Useful for
1439 * debugging and possibly also useful for actual logging.
1441 if (term->cfg.logtype == LGTYP_DEBUG && term->logctx)
1442 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
1448 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1449 * be able to display 8-bit characters, but I'll let that go 'cause
1454 * If we're printing, add the character to the printer
1457 if (term->printing) {
1458 bufchain_add(&term->printer_buf, &c, 1);
1461 * If we're in print-only mode, we use a much simpler
1462 * state machine designed only to recognise the ESC[4i
1463 * termination sequence.
1465 if (term->only_printing) {
1467 term->print_state = 1;
1468 else if (c == (unsigned char)'\233')
1469 term->print_state = 2;
1470 else if (c == '[' && term->print_state == 1)
1471 term->print_state = 2;
1472 else if (c == '4' && term->print_state == 2)
1473 term->print_state = 3;
1474 else if (c == 'i' && term->print_state == 3)
1475 term->print_state = 4;
1477 term->print_state = 0;
1478 if (term->print_state == 4) {
1479 term_print_finish(term);
1485 /* First see about all those translations. */
1486 if (term->termstate == TOPLEVEL) {
1488 switch (term->utf_state) {
1491 /* UTF-8 must be stateless so we ignore iso2022. */
1492 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1493 c = term->ucsdata->unitab_ctrl[c];
1494 else c = ((unsigned char)c) | ATTR_ASCII;
1496 } else if ((c & 0xe0) == 0xc0) {
1497 term->utf_size = term->utf_state = 1;
1498 term->utf_char = (c & 0x1f);
1499 } else if ((c & 0xf0) == 0xe0) {
1500 term->utf_size = term->utf_state = 2;
1501 term->utf_char = (c & 0x0f);
1502 } else if ((c & 0xf8) == 0xf0) {
1503 term->utf_size = term->utf_state = 3;
1504 term->utf_char = (c & 0x07);
1505 } else if ((c & 0xfc) == 0xf8) {
1506 term->utf_size = term->utf_state = 4;
1507 term->utf_char = (c & 0x03);
1508 } else if ((c & 0xfe) == 0xfc) {
1509 term->utf_size = term->utf_state = 5;
1510 term->utf_char = (c & 0x01);
1521 if ((c & 0xC0) != 0x80) {
1524 term->utf_state = 0;
1527 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
1528 if (--term->utf_state)
1533 /* Is somebody trying to be evil! */
1535 (c < 0x800 && term->utf_size >= 2) ||
1536 (c < 0x10000 && term->utf_size >= 3) ||
1537 (c < 0x200000 && term->utf_size >= 4) ||
1538 (c < 0x4000000 && term->utf_size >= 5))
1541 /* Unicode line separator and paragraph separator are CR-LF */
1542 if (c == 0x2028 || c == 0x2029)
1545 /* High controls are probably a Baaad idea too. */
1549 /* The UTF-16 surrogates are not nice either. */
1550 /* The standard give the option of decoding these:
1551 * I don't want to! */
1552 if (c >= 0xD800 && c < 0xE000)
1555 /* ISO 10646 characters now limited to UTF-16 range. */
1559 /* This is currently a TagPhobic application.. */
1560 if (c >= 0xE0000 && c <= 0xE007F)
1563 /* U+FEFF is best seen as a null. */
1566 /* But U+FFFE is an error. */
1567 if (c == 0xFFFE || c == 0xFFFF)
1570 /* Oops this is a 16bit implementation */
1575 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1576 else if(term->sco_acs &&
1577 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
1579 if (term->sco_acs == 2) c |= 0x80;
1582 switch (term->cset_attr[term->cset]) {
1584 * Linedraw characters are different from 'ESC ( B'
1585 * only for a small range. For ones outside that
1586 * range, make sure we use the same font as well as
1587 * the same encoding.
1590 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1591 c = term->ucsdata->unitab_ctrl[c];
1593 c = ((unsigned char) c) | ATTR_LINEDRW;
1597 /* If UK-ASCII, make the '#' a LineDraw Pound */
1599 c = '}' | ATTR_LINEDRW;
1602 /*FALLTHROUGH*/ case ATTR_ASCII:
1603 if (term->ucsdata->unitab_ctrl[c] != 0xFF)
1604 c = term->ucsdata->unitab_ctrl[c];
1606 c = ((unsigned char) c) | ATTR_ASCII;
1609 if (c>=' ') c = ((unsigned char)c) | ATTR_SCOACS;
1615 /* How about C1 controls ? */
1616 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
1617 !term->vt52_mode && has_compat(VT220)) {
1618 term->termstate = SEEN_ESC;
1619 term->esc_query = FALSE;
1620 c = '@' + (c & 0x1F);
1623 /* Or the GL control. */
1624 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
1625 if (term->curs.x && !term->wrapnext)
1627 term->wrapnext = FALSE;
1629 if (!term->cfg.no_dbackspace) /* destructive bksp might be disabled */
1630 *term->cpos = (' ' | term->curr_attr | ATTR_ASCII);
1632 /* Or normal C0 controls. */
1633 if ((c & -32) == 0 && term->termstate < DO_CTRLS) {
1635 case '\005': /* ENQ: terminal type query */
1636 /* Strictly speaking this is VT100 but a VT100 defaults to
1637 * no response. Other terminals respond at their option.
1639 * Don't put a CR in the default string as this tends to
1640 * upset some weird software.
1642 * An xterm returns "xterm" (5 characters)
1644 compatibility(ANSIMIN);
1646 char abuf[256], *s, *d;
1648 for (s = term->cfg.answerback, d = abuf; *s; s++) {
1650 if (*s >= 'a' && *s <= 'z')
1651 *d++ = (*s - ('a' - 1));
1652 else if ((*s >= '@' && *s <= '_') ||
1653 *s == '?' || (*s & 0x80))
1658 } else if (*s == '^') {
1663 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
1667 case '\007': /* BEL: Bell */
1669 struct beeptime *newbeep;
1670 unsigned long ticks;
1672 ticks = GETTICKCOUNT();
1674 if (!term->beep_overloaded) {
1675 newbeep = snew(struct beeptime);
1676 newbeep->ticks = ticks;
1677 newbeep->next = NULL;
1678 if (!term->beephead)
1679 term->beephead = newbeep;
1681 term->beeptail->next = newbeep;
1682 term->beeptail = newbeep;
1687 * Throw out any beeps that happened more than
1690 while (term->beephead &&
1691 term->beephead->ticks < ticks - term->cfg.bellovl_t) {
1692 struct beeptime *tmp = term->beephead;
1693 term->beephead = tmp->next;
1695 if (!term->beephead)
1696 term->beeptail = NULL;
1700 if (term->cfg.bellovl && term->beep_overloaded &&
1701 ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) {
1703 * If we're currently overloaded and the
1704 * last beep was more than s seconds ago,
1705 * leave overload mode.
1707 term->beep_overloaded = FALSE;
1708 } else if (term->cfg.bellovl && !term->beep_overloaded &&
1709 term->nbeeps >= term->cfg.bellovl_n) {
1711 * Now, if we have n or more beeps
1712 * remaining in the queue, go into overload
1715 term->beep_overloaded = TRUE;
1717 term->lastbeep = ticks;
1720 * Perform an actual beep if we're not overloaded.
1722 if (!term->cfg.bellovl || !term->beep_overloaded) {
1723 beep(term->frontend, term->cfg.beep);
1724 if (term->cfg.beep == BELL_VISUAL) {
1725 term->in_vbell = TRUE;
1726 term->vbell_startpoint = ticks;
1730 term->seen_disp_event = TRUE;
1733 case '\b': /* BS: Back space */
1734 if (term->curs.x == 0 &&
1735 (term->curs.y == 0 || term->wrap == 0))
1737 else if (term->curs.x == 0 && term->curs.y > 0)
1738 term->curs.x = term->cols - 1, term->curs.y--;
1739 else if (term->wrapnext)
1740 term->wrapnext = FALSE;
1744 term->seen_disp_event = TRUE;
1746 case '\016': /* LS1: Locking-shift one */
1747 compatibility(VT100);
1750 case '\017': /* LS0: Locking-shift zero */
1751 compatibility(VT100);
1754 case '\033': /* ESC: Escape */
1755 if (term->vt52_mode)
1756 term->termstate = VT52_ESC;
1758 compatibility(ANSIMIN);
1759 term->termstate = SEEN_ESC;
1760 term->esc_query = FALSE;
1763 case '\015': /* CR: Carriage return */
1765 term->wrapnext = FALSE;
1767 term->seen_disp_event = TRUE;
1768 term->paste_hold = 0;
1770 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1772 case '\014': /* FF: Form feed */
1773 if (has_compat(SCOANSI)) {
1774 move(term, 0, 0, 0);
1775 erase_lots(term, FALSE, FALSE, TRUE);
1777 term->wrapnext = FALSE;
1778 term->seen_disp_event = 1;
1781 case '\013': /* VT: Line tabulation */
1782 compatibility(VT100);
1783 case '\012': /* LF: Line feed */
1784 if (term->curs.y == term->marg_b)
1785 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1786 else if (term->curs.y < term->rows - 1)
1788 if (term->cfg.lfhascr)
1791 term->wrapnext = FALSE;
1792 term->seen_disp_event = 1;
1793 term->paste_hold = 0;
1795 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1797 case '\t': /* HT: Character tabulation */
1799 pos old_curs = term->curs;
1800 unsigned long *ldata = lineptr(term->curs.y);
1804 } while (term->curs.x < term->cols - 1 &&
1805 !term->tabs[term->curs.x]);
1807 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM) {
1808 if (term->curs.x >= term->cols / 2)
1809 term->curs.x = term->cols / 2 - 1;
1811 if (term->curs.x >= term->cols)
1812 term->curs.x = term->cols - 1;
1816 check_selection(term, old_curs, term->curs);
1818 term->seen_disp_event = TRUE;
1822 switch (term->termstate) {
1824 /* Only graphic characters get this far;
1825 * ctrls are stripped above */
1826 if (term->wrapnext && term->wrap) {
1827 term->cpos[1] |= LATTR_WRAPPED;
1828 if (term->curs.y == term->marg_b)
1829 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1830 else if (term->curs.y < term->rows - 1)
1834 term->wrapnext = FALSE;
1838 if (term->selstate != NO_SELECTION) {
1839 pos cursplus = term->curs;
1841 check_selection(term, term->curs, cursplus);
1843 if (((c & CSET_MASK) == ATTR_ASCII || (c & CSET_MASK) == 0) &&
1845 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1851 width = wcwidth((wchar_t) c);
1855 * If we're about to display a double-width
1856 * character starting in the rightmost
1857 * column, then we do something special
1858 * instead. We must print a space in the
1859 * last column of the screen, then wrap;
1860 * and we also set LATTR_WRAPPED2 which
1861 * instructs subsequent cut-and-pasting not
1862 * only to splice this line to the one
1863 * after it, but to ignore the space in the
1864 * last character position as well.
1865 * (Because what was actually output to the
1866 * terminal was presumably just a sequence
1867 * of CJK characters, and we don't want a
1868 * space to be pasted in the middle of
1869 * those just because they had the
1870 * misfortune to start in the wrong parity
1871 * column. xterm concurs.)
1873 check_boundary(term, term->curs.x, term->curs.y);
1874 check_boundary(term, term->curs.x+2, term->curs.y);
1875 if (term->curs.x == term->cols-1) {
1876 *term->cpos++ = ATTR_ASCII | ' ' | term->curr_attr;
1877 *term->cpos |= LATTR_WRAPPED | LATTR_WRAPPED2;
1878 if (term->curs.y == term->marg_b)
1879 scroll(term, term->marg_t, term->marg_b,
1881 else if (term->curs.y < term->rows - 1)
1885 /* Now we must check_boundary again, of course. */
1886 check_boundary(term, term->curs.x, term->curs.y);
1887 check_boundary(term, term->curs.x+2, term->curs.y);
1889 *term->cpos++ = c | term->curr_attr;
1890 *term->cpos++ = UCSWIDE | term->curr_attr;
1894 check_boundary(term, term->curs.x, term->curs.y);
1895 check_boundary(term, term->curs.x+1, term->curs.y);
1896 *term->cpos++ = c | term->curr_attr;
1903 if (term->curs.x == term->cols) {
1906 term->wrapnext = TRUE;
1907 if (term->wrap && term->vt52_mode) {
1908 term->cpos[1] |= LATTR_WRAPPED;
1909 if (term->curs.y == term->marg_b)
1910 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1911 else if (term->curs.y < term->rows - 1)
1915 term->wrapnext = FALSE;
1918 term->seen_disp_event = 1;
1923 * This state is virtually identical to SEEN_ESC, with the
1924 * exception that we have an OSC sequence in the pipeline,
1925 * and _if_ we see a backslash, we process it.
1929 term->termstate = TOPLEVEL;
1932 /* else fall through */
1934 if (c >= ' ' && c <= '/') {
1935 if (term->esc_query)
1936 term->esc_query = -1;
1938 term->esc_query = c;
1941 term->termstate = TOPLEVEL;
1942 switch (ANSI(c, term->esc_query)) {
1943 case '[': /* enter CSI mode */
1944 term->termstate = SEEN_CSI;
1945 term->esc_nargs = 1;
1946 term->esc_args[0] = ARG_DEFAULT;
1947 term->esc_query = FALSE;
1949 case ']': /* OSC: xterm escape sequences */
1950 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1951 compatibility(OTHER);
1952 term->termstate = SEEN_OSC;
1953 term->esc_args[0] = 0;
1955 case '7': /* DECSC: save cursor */
1956 compatibility(VT100);
1957 save_cursor(term, TRUE);
1959 case '8': /* DECRC: restore cursor */
1960 compatibility(VT100);
1961 save_cursor(term, FALSE);
1962 term->seen_disp_event = TRUE;
1964 case '=': /* DECKPAM: Keypad application mode */
1965 compatibility(VT100);
1966 term->app_keypad_keys = TRUE;
1968 case '>': /* DECKPNM: Keypad numeric mode */
1969 compatibility(VT100);
1970 term->app_keypad_keys = FALSE;
1972 case 'D': /* IND: exactly equivalent to LF */
1973 compatibility(VT100);
1974 if (term->curs.y == term->marg_b)
1975 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1976 else if (term->curs.y < term->rows - 1)
1979 term->wrapnext = FALSE;
1980 term->seen_disp_event = TRUE;
1982 case 'E': /* NEL: exactly equivalent to CR-LF */
1983 compatibility(VT100);
1985 if (term->curs.y == term->marg_b)
1986 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1987 else if (term->curs.y < term->rows - 1)
1990 term->wrapnext = FALSE;
1991 term->seen_disp_event = TRUE;
1993 case 'M': /* RI: reverse index - backwards LF */
1994 compatibility(VT100);
1995 if (term->curs.y == term->marg_t)
1996 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
1997 else if (term->curs.y > 0)
2000 term->wrapnext = FALSE;
2001 term->seen_disp_event = TRUE;
2003 case 'Z': /* DECID: terminal type query */
2004 compatibility(VT100);
2006 ldisc_send(term->ldisc, term->id_string,
2007 strlen(term->id_string), 0);
2009 case 'c': /* RIS: restore power-on settings */
2010 compatibility(VT100);
2012 if (term->ldisc) /* cause ldisc to notice changes */
2013 ldisc_send(term->ldisc, NULL, 0, 0);
2014 if (term->reset_132) {
2015 if (!term->cfg.no_remote_resize)
2016 request_resize(term->frontend, 80, term->rows);
2017 term->reset_132 = 0;
2021 term->seen_disp_event = TRUE;
2023 case 'H': /* HTS: set a tab */
2024 compatibility(VT100);
2025 term->tabs[term->curs.x] = TRUE;
2028 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
2029 compatibility(VT100);
2031 unsigned long *ldata;
2035 for (i = 0; i < term->rows; i++) {
2037 for (j = 0; j < term->cols; j++)
2038 ldata[j] = ATTR_DEFAULT | 'E';
2039 ldata[term->cols] = 0;
2042 term->seen_disp_event = TRUE;
2043 scrtop.x = scrtop.y = 0;
2045 scrbot.y = term->rows;
2046 check_selection(term, scrtop, scrbot);
2050 case ANSI('3', '#'):
2051 case ANSI('4', '#'):
2052 case ANSI('5', '#'):
2053 case ANSI('6', '#'):
2054 compatibility(VT100);
2056 unsigned long nlattr;
2057 unsigned long *ldata;
2058 switch (ANSI(c, term->esc_query)) {
2059 case ANSI('3', '#'): /* DECDHL: 2*height, top */
2062 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
2065 case ANSI('5', '#'): /* DECSWL: normal */
2066 nlattr = LATTR_NORM;
2068 default: /* case ANSI('6', '#'): DECDWL: 2*width */
2069 nlattr = LATTR_WIDE;
2072 ldata = lineptr(term->curs.y);
2073 ldata[term->cols] &= ~LATTR_MODE;
2074 ldata[term->cols] |= nlattr;
2077 /* GZD4: G0 designate 94-set */
2078 case ANSI('A', '('):
2079 compatibility(VT100);
2080 if (!term->cfg.no_remote_charset)
2081 term->cset_attr[0] = ATTR_GBCHR;
2083 case ANSI('B', '('):
2084 compatibility(VT100);
2085 if (!term->cfg.no_remote_charset)
2086 term->cset_attr[0] = ATTR_ASCII;
2088 case ANSI('0', '('):
2089 compatibility(VT100);
2090 if (!term->cfg.no_remote_charset)
2091 term->cset_attr[0] = ATTR_LINEDRW;
2093 case ANSI('U', '('):
2094 compatibility(OTHER);
2095 if (!term->cfg.no_remote_charset)
2096 term->cset_attr[0] = ATTR_SCOACS;
2098 /* G1D4: G1-designate 94-set */
2099 case ANSI('A', ')'):
2100 compatibility(VT100);
2101 if (!term->cfg.no_remote_charset)
2102 term->cset_attr[1] = ATTR_GBCHR;
2104 case ANSI('B', ')'):
2105 compatibility(VT100);
2106 if (!term->cfg.no_remote_charset)
2107 term->cset_attr[1] = ATTR_ASCII;
2109 case ANSI('0', ')'):
2110 compatibility(VT100);
2111 if (!term->cfg.no_remote_charset)
2112 term->cset_attr[1] = ATTR_LINEDRW;
2114 case ANSI('U', ')'):
2115 compatibility(OTHER);
2116 if (!term->cfg.no_remote_charset)
2117 term->cset_attr[1] = ATTR_SCOACS;
2119 /* DOCS: Designate other coding system */
2120 case ANSI('8', '%'): /* Old Linux code */
2121 case ANSI('G', '%'):
2122 compatibility(OTHER);
2123 if (!term->cfg.no_remote_charset)
2126 case ANSI('@', '%'):
2127 compatibility(OTHER);
2128 if (!term->cfg.no_remote_charset)
2134 term->termstate = TOPLEVEL; /* default */
2136 if (term->esc_nargs <= ARGS_MAX) {
2137 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
2138 term->esc_args[term->esc_nargs - 1] = 0;
2139 term->esc_args[term->esc_nargs - 1] =
2140 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
2142 term->termstate = SEEN_CSI;
2143 } else if (c == ';') {
2144 if (++term->esc_nargs <= ARGS_MAX)
2145 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
2146 term->termstate = SEEN_CSI;
2147 } else if (c < '@') {
2148 if (term->esc_query)
2149 term->esc_query = -1;
2151 term->esc_query = TRUE;
2153 term->esc_query = c;
2154 term->termstate = SEEN_CSI;
2156 switch (ANSI(c, term->esc_query)) {
2157 case 'A': /* CUU: move up N lines */
2158 move(term, term->curs.x,
2159 term->curs.y - def(term->esc_args[0], 1), 1);
2160 term->seen_disp_event = TRUE;
2162 case 'e': /* VPR: move down N lines */
2163 compatibility(ANSI);
2165 case 'B': /* CUD: Cursor down */
2166 move(term, term->curs.x,
2167 term->curs.y + def(term->esc_args[0], 1), 1);
2168 term->seen_disp_event = TRUE;
2170 case ANSI('c', '>'): /* DA: report xterm version */
2171 compatibility(OTHER);
2172 /* this reports xterm version 136 so that VIM can
2173 use the drag messages from the mouse reporting */
2175 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
2177 case 'a': /* HPR: move right N cols */
2178 compatibility(ANSI);
2180 case 'C': /* CUF: Cursor right */
2181 move(term, term->curs.x + def(term->esc_args[0], 1),
2183 term->seen_disp_event = TRUE;
2185 case 'D': /* CUB: move left N cols */
2186 move(term, term->curs.x - def(term->esc_args[0], 1),
2188 term->seen_disp_event = TRUE;
2190 case 'E': /* CNL: move down N lines and CR */
2191 compatibility(ANSI);
2193 term->curs.y + def(term->esc_args[0], 1), 1);
2194 term->seen_disp_event = TRUE;
2196 case 'F': /* CPL: move up N lines and CR */
2197 compatibility(ANSI);
2199 term->curs.y - def(term->esc_args[0], 1), 1);
2200 term->seen_disp_event = TRUE;
2203 case '`': /* HPA: set horizontal posn */
2204 compatibility(ANSI);
2205 move(term, def(term->esc_args[0], 1) - 1,
2207 term->seen_disp_event = TRUE;
2209 case 'd': /* VPA: set vertical posn */
2210 compatibility(ANSI);
2211 move(term, term->curs.x,
2212 ((term->dec_om ? term->marg_t : 0) +
2213 def(term->esc_args[0], 1) - 1),
2214 (term->dec_om ? 2 : 0));
2215 term->seen_disp_event = TRUE;
2218 case 'f': /* HVP: set horz and vert posns at once */
2219 if (term->esc_nargs < 2)
2220 term->esc_args[1] = ARG_DEFAULT;
2221 move(term, def(term->esc_args[1], 1) - 1,
2222 ((term->dec_om ? term->marg_t : 0) +
2223 def(term->esc_args[0], 1) - 1),
2224 (term->dec_om ? 2 : 0));
2225 term->seen_disp_event = TRUE;
2227 case 'J': /* ED: erase screen or parts of it */
2229 unsigned int i = def(term->esc_args[0], 0) + 1;
2232 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
2235 term->seen_disp_event = TRUE;
2237 case 'K': /* EL: erase line or parts of it */
2239 unsigned int i = def(term->esc_args[0], 0) + 1;
2242 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
2244 term->seen_disp_event = TRUE;
2246 case 'L': /* IL: insert lines */
2247 compatibility(VT102);
2248 if (term->curs.y <= term->marg_b)
2249 scroll(term, term->curs.y, term->marg_b,
2250 -def(term->esc_args[0], 1), FALSE);
2252 term->seen_disp_event = TRUE;
2254 case 'M': /* DL: delete lines */
2255 compatibility(VT102);
2256 if (term->curs.y <= term->marg_b)
2257 scroll(term, term->curs.y, term->marg_b,
2258 def(term->esc_args[0], 1),
2261 term->seen_disp_event = TRUE;
2263 case '@': /* ICH: insert chars */
2264 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2265 compatibility(VT102);
2266 insch(term, def(term->esc_args[0], 1));
2267 term->seen_disp_event = TRUE;
2269 case 'P': /* DCH: delete chars */
2270 compatibility(VT102);
2271 insch(term, -def(term->esc_args[0], 1));
2272 term->seen_disp_event = TRUE;
2274 case 'c': /* DA: terminal type query */
2275 compatibility(VT100);
2276 /* This is the response for a VT102 */
2278 ldisc_send(term->ldisc, term->id_string,
2279 strlen(term->id_string), 0);
2281 case 'n': /* DSR: cursor position query */
2283 if (term->esc_args[0] == 6) {
2285 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
2287 ldisc_send(term->ldisc, buf, strlen(buf), 0);
2288 } else if (term->esc_args[0] == 5) {
2289 ldisc_send(term->ldisc, "\033[0n", 4, 0);
2293 case 'h': /* SM: toggle modes to high */
2295 compatibility(VT100);
2298 for (i = 0; i < term->esc_nargs; i++)
2299 toggle_mode(term, term->esc_args[i],
2300 term->esc_query, TRUE);
2303 case 'i': /* MC: Media copy */
2305 compatibility(VT100);
2307 if (term->esc_nargs != 1) break;
2308 if (term->esc_args[0] == 5 && *term->cfg.printer) {
2309 term->printing = TRUE;
2310 term->only_printing = !term->esc_query;
2311 term->print_state = 0;
2312 term_print_setup(term);
2313 } else if (term->esc_args[0] == 4 &&
2315 term_print_finish(term);
2319 case 'l': /* RM: toggle modes to low */
2321 compatibility(VT100);
2324 for (i = 0; i < term->esc_nargs; i++)
2325 toggle_mode(term, term->esc_args[i],
2326 term->esc_query, FALSE);
2329 case 'g': /* TBC: clear tabs */
2330 compatibility(VT100);
2331 if (term->esc_nargs == 1) {
2332 if (term->esc_args[0] == 0) {
2333 term->tabs[term->curs.x] = FALSE;
2334 } else if (term->esc_args[0] == 3) {
2336 for (i = 0; i < term->cols; i++)
2337 term->tabs[i] = FALSE;
2341 case 'r': /* DECSTBM: set scroll margins */
2342 compatibility(VT100);
2343 if (term->esc_nargs <= 2) {
2345 top = def(term->esc_args[0], 1) - 1;
2346 bot = (term->esc_nargs <= 1
2347 || term->esc_args[1] == 0 ?
2349 def(term->esc_args[1], term->rows)) - 1;
2350 if (bot >= term->rows)
2351 bot = term->rows - 1;
2352 /* VTTEST Bug 9 - if region is less than 2 lines
2353 * don't change region.
2355 if (bot - top > 0) {
2360 * I used to think the cursor should be
2361 * placed at the top of the newly marginned
2362 * area. Apparently not: VMS TPU falls over
2365 * Well actually it should for
2368 term->curs.y = (term->dec_om ?
2371 term->seen_disp_event = TRUE;
2375 case 'm': /* SGR: set graphics rendition */
2378 * A VT100 without the AVO only had one
2379 * attribute, either underline or
2380 * reverse video depending on the
2381 * cursor type, this was selected by
2385 * This is sometimes DIM, eg on the
2388 * This is sometimes INVIS various ANSI.
2390 * This like 22 disables BOLD, DIM and INVIS
2392 * The ANSI colours appear on any
2393 * terminal that has colour (obviously)
2394 * but the interaction between sgr0 and
2395 * the colours varies but is usually
2396 * related to the background colour
2397 * erase item. The interaction between
2398 * colour attributes and the mono ones
2399 * is also very implementation
2402 * The 39 and 49 attributes are likely
2403 * to be unimplemented.
2406 for (i = 0; i < term->esc_nargs; i++) {
2407 switch (def(term->esc_args[i], 0)) {
2408 case 0: /* restore defaults */
2409 term->curr_attr = term->default_attr;
2411 case 1: /* enable bold */
2412 compatibility(VT100AVO);
2413 term->curr_attr |= ATTR_BOLD;
2415 case 21: /* (enable double underline) */
2416 compatibility(OTHER);
2417 case 4: /* enable underline */
2418 compatibility(VT100AVO);
2419 term->curr_attr |= ATTR_UNDER;
2421 case 5: /* enable blink */
2422 compatibility(VT100AVO);
2423 term->curr_attr |= ATTR_BLINK;
2425 case 6: /* SCO light bkgrd */
2426 compatibility(SCOANSI);
2427 term->blink_is_real = FALSE;
2428 term->curr_attr |= ATTR_BLINK;
2430 case 7: /* enable reverse video */
2431 term->curr_attr |= ATTR_REVERSE;
2433 case 10: /* SCO acs off */
2434 compatibility(SCOANSI);
2435 if (term->cfg.no_remote_charset) break;
2436 term->sco_acs = 0; break;
2437 case 11: /* SCO acs on */
2438 compatibility(SCOANSI);
2439 if (term->cfg.no_remote_charset) break;
2440 term->sco_acs = 1; break;
2441 case 12: /* SCO acs on, |0x80 */
2442 compatibility(SCOANSI);
2443 if (term->cfg.no_remote_charset) break;
2444 term->sco_acs = 2; break;
2445 case 22: /* disable bold */
2446 compatibility2(OTHER, VT220);
2447 term->curr_attr &= ~ATTR_BOLD;
2449 case 24: /* disable underline */
2450 compatibility2(OTHER, VT220);
2451 term->curr_attr &= ~ATTR_UNDER;
2453 case 25: /* disable blink */
2454 compatibility2(OTHER, VT220);
2455 term->curr_attr &= ~ATTR_BLINK;
2457 case 27: /* disable reverse video */
2458 compatibility2(OTHER, VT220);
2459 term->curr_attr &= ~ATTR_REVERSE;
2470 term->curr_attr &= ~ATTR_FGMASK;
2472 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
2482 /* xterm-style bright foreground */
2483 term->curr_attr &= ~ATTR_FGMASK;
2485 ((term->esc_args[i] - 90 + 16)
2488 case 39: /* default-foreground */
2489 term->curr_attr &= ~ATTR_FGMASK;
2490 term->curr_attr |= ATTR_DEFFG;
2501 term->curr_attr &= ~ATTR_BGMASK;
2503 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
2513 /* xterm-style bright background */
2514 term->curr_attr &= ~ATTR_BGMASK;
2516 ((term->esc_args[i] - 100 + 16)
2519 case 49: /* default-background */
2520 term->curr_attr &= ~ATTR_BGMASK;
2521 term->curr_attr |= ATTR_DEFBG;
2526 term->erase_char = (' ' | ATTR_ASCII |
2532 case 's': /* save cursor */
2533 save_cursor(term, TRUE);
2535 case 'u': /* restore cursor */
2536 save_cursor(term, FALSE);
2537 term->seen_disp_event = TRUE;
2539 case 't': /* DECSLPP: set page size - ie window height */
2541 * VT340/VT420 sequence DECSLPP, DEC only allows values
2542 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2543 * illegal values (eg first arg 1..9) for window changing
2546 if (term->esc_nargs <= 1
2547 && (term->esc_args[0] < 1 ||
2548 term->esc_args[0] >= 24)) {
2549 compatibility(VT340TEXT);
2550 if (!term->cfg.no_remote_resize)
2551 request_resize(term->frontend, term->cols,
2552 def(term->esc_args[0], 24));
2554 } else if (term->esc_nargs >= 1 &&
2555 term->esc_args[0] >= 1 &&
2556 term->esc_args[0] < 24) {
2557 compatibility(OTHER);
2559 switch (term->esc_args[0]) {
2563 set_iconic(term->frontend, FALSE);
2566 set_iconic(term->frontend, TRUE);
2569 if (term->esc_nargs >= 3) {
2570 if (!term->cfg.no_remote_resize)
2571 move_window(term->frontend,
2572 def(term->esc_args[1], 0),
2573 def(term->esc_args[2], 0));
2577 /* We should resize the window to a given
2578 * size in pixels here, but currently our
2579 * resizing code isn't healthy enough to
2584 set_zorder(term->frontend, TRUE);
2587 /* move to bottom */
2588 set_zorder(term->frontend, FALSE);
2591 refresh_window(term->frontend);
2594 if (term->esc_nargs >= 3) {
2595 if (!term->cfg.no_remote_resize)
2596 request_resize(term->frontend,
2597 def(term->esc_args[2], term->cfg.width),
2598 def(term->esc_args[1], term->cfg.height));
2602 if (term->esc_nargs >= 2)
2603 set_zoomed(term->frontend,
2609 ldisc_send(term->ldisc,
2610 is_iconic(term->frontend) ?
2611 "\033[1t" : "\033[2t", 4, 0);
2615 get_window_pos(term->frontend, &x, &y);
2616 len = sprintf(buf, "\033[3;%d;%dt", x, y);
2617 ldisc_send(term->ldisc, buf, len, 0);
2622 get_window_pixels(term->frontend, &x, &y);
2623 len = sprintf(buf, "\033[4;%d;%dt", x, y);
2624 ldisc_send(term->ldisc, buf, len, 0);
2629 len = sprintf(buf, "\033[8;%d;%dt",
2630 term->rows, term->cols);
2631 ldisc_send(term->ldisc, buf, len, 0);
2636 * Hmmm. Strictly speaking we
2637 * should return `the size of the
2638 * screen in characters', but
2639 * that's not easy: (a) window
2640 * furniture being what it is it's
2641 * hard to compute, and (b) in
2642 * resize-font mode maximising the
2643 * window wouldn't change the
2644 * number of characters. *shrug*. I
2645 * think we'll ignore it for the
2646 * moment and see if anyone
2647 * complains, and then ask them
2648 * what they would like it to do.
2653 !term->cfg.no_remote_qtitle) {
2654 p = get_window_title(term->frontend, TRUE);
2656 ldisc_send(term->ldisc, "\033]L", 3, 0);
2657 ldisc_send(term->ldisc, p, len, 0);
2658 ldisc_send(term->ldisc, "\033\\", 2, 0);
2663 !term->cfg.no_remote_qtitle) {
2664 p = get_window_title(term->frontend,FALSE);
2666 ldisc_send(term->ldisc, "\033]l", 3, 0);
2667 ldisc_send(term->ldisc, p, len, 0);
2668 ldisc_send(term->ldisc, "\033\\", 2, 0);
2674 case 'S': /* SU: Scroll up */
2675 compatibility(SCOANSI);
2676 scroll(term, term->marg_t, term->marg_b,
2677 def(term->esc_args[0], 1), TRUE);
2679 term->wrapnext = FALSE;
2680 term->seen_disp_event = TRUE;
2682 case 'T': /* SD: Scroll down */
2683 compatibility(SCOANSI);
2684 scroll(term, term->marg_t, term->marg_b,
2685 -def(term->esc_args[0], 1), TRUE);
2687 term->wrapnext = FALSE;
2688 term->seen_disp_event = TRUE;
2690 case ANSI('|', '*'): /* DECSNLS */
2692 * Set number of lines on screen
2693 * VT420 uses VGA like hardware and can
2694 * support any size in reasonable range
2695 * (24..49 AIUI) with no default specified.
2697 compatibility(VT420);
2698 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
2699 if (!term->cfg.no_remote_resize)
2700 request_resize(term->frontend, term->cols,
2701 def(term->esc_args[0],
2706 case ANSI('|', '$'): /* DECSCPP */
2708 * Set number of columns per page
2709 * Docs imply range is only 80 or 132, but
2712 compatibility(VT340TEXT);
2713 if (term->esc_nargs <= 1) {
2714 if (!term->cfg.no_remote_resize)
2715 request_resize(term->frontend,
2716 def(term->esc_args[0],
2717 term->cfg.width), term->rows);
2721 case 'X': /* ECH: write N spaces w/o moving cursor */
2722 /* XXX VTTEST says this is vt220, vt510 manual
2724 compatibility(ANSIMIN);
2726 int n = def(term->esc_args[0], 1);
2728 unsigned long *p = term->cpos;
2729 if (n > term->cols - term->curs.x)
2730 n = term->cols - term->curs.x;
2731 cursplus = term->curs;
2733 check_boundary(term, term->curs.x, term->curs.y);
2734 check_boundary(term, term->curs.x+n, term->curs.y);
2735 check_selection(term, term->curs, cursplus);
2737 *p++ = term->erase_char;
2738 term->seen_disp_event = TRUE;
2741 case 'x': /* DECREQTPARM: report terminal characteristics */
2742 compatibility(VT100);
2745 int i = def(term->esc_args[0], 0);
2746 if (i == 0 || i == 1) {
2747 strcpy(buf, "\033[2;1;1;112;112;1;0x");
2749 ldisc_send(term->ldisc, buf, 20, 0);
2753 case 'Z': /* CBT: BackTab for xterm */
2754 compatibility(OTHER);
2756 int i = def(term->esc_args[0], 1);
2757 pos old_curs = term->curs;
2759 for(;i>0 && term->curs.x>0; i--) {
2762 } while (term->curs.x >0 &&
2763 !term->tabs[term->curs.x]);
2766 check_selection(term, old_curs, term->curs);
2769 case ANSI('c', '='): /* Hide or Show Cursor */
2770 compatibility(SCOANSI);
2771 switch(term->esc_args[0]) {
2772 case 0: /* hide cursor */
2773 term->cursor_on = FALSE;
2775 case 1: /* restore cursor */
2776 term->big_cursor = FALSE;
2777 term->cursor_on = TRUE;
2779 case 2: /* block cursor */
2780 term->big_cursor = TRUE;
2781 term->cursor_on = TRUE;
2785 case ANSI('C', '='):
2787 * set cursor start on scanline esc_args[0] and
2788 * end on scanline esc_args[1].If you set
2789 * the bottom scan line to a value less than
2790 * the top scan line, the cursor will disappear.
2792 compatibility(SCOANSI);
2793 if (term->esc_nargs >= 2) {
2794 if (term->esc_args[0] > term->esc_args[1])
2795 term->cursor_on = FALSE;
2797 term->cursor_on = TRUE;
2800 case ANSI('D', '='):
2801 compatibility(SCOANSI);
2802 term->blink_is_real = FALSE;
2803 if (term->esc_args[0]>=1)
2804 term->curr_attr |= ATTR_BLINK;
2806 term->curr_attr &= ~ATTR_BLINK;
2808 case ANSI('E', '='):
2809 compatibility(SCOANSI);
2810 term->blink_is_real = (term->esc_args[0] >= 1);
2812 case ANSI('F', '='): /* set normal foreground */
2813 compatibility(SCOANSI);
2814 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
2816 (sco2ansicolour[term->esc_args[0] & 0x7] |
2817 ((term->esc_args[0] & 0x8) << 1)) <<
2819 term->curr_attr &= ~ATTR_FGMASK;
2820 term->curr_attr |= colour;
2821 term->default_attr &= ~ATTR_FGMASK;
2822 term->default_attr |= colour;
2825 case ANSI('G', '='): /* set normal background */
2826 compatibility(SCOANSI);
2827 if (term->esc_args[0] >= 0 && term->esc_args[0] < 16) {
2829 (sco2ansicolour[term->esc_args[0] & 0x7] |
2830 ((term->esc_args[0] & 0x8) << 1)) <<
2832 term->curr_attr &= ~ATTR_BGMASK;
2833 term->curr_attr |= colour;
2834 term->default_attr &= ~ATTR_BGMASK;
2835 term->default_attr |= colour;
2838 case ANSI('L', '='):
2839 compatibility(SCOANSI);
2840 term->use_bce = (term->esc_args[0] <= 0);
2841 term->erase_char = ERASE_CHAR;
2843 term->erase_char = (' ' | ATTR_ASCII |
2845 (ATTR_FGMASK | ATTR_BGMASK)));
2847 case ANSI('p', '"'): /* DECSCL: set compat level */
2849 * Allow the host to make this emulator a
2850 * 'perfect' VT102. This first appeared in
2851 * the VT220, but we do need to get back to
2852 * PuTTY mode so I won't check it.
2854 * The arg in 40..42,50 are a PuTTY extension.
2855 * The 2nd arg, 8bit vs 7bit is not checked.
2857 * Setting VT102 mode should also change
2858 * the Fkeys to generate PF* codes as a
2859 * real VT102 has no Fkeys. The VT220 does
2860 * this, F11..F13 become ESC,BS,LF other
2861 * Fkeys send nothing.
2863 * Note ESC c will NOT change this!
2866 switch (term->esc_args[0]) {
2868 term->compatibility_level &= ~TM_VTXXX;
2869 term->compatibility_level |= TM_VT102;
2872 term->compatibility_level &= ~TM_VTXXX;
2873 term->compatibility_level |= TM_VT220;
2877 if (term->esc_args[0] > 60 &&
2878 term->esc_args[0] < 70)
2879 term->compatibility_level |= TM_VTXXX;
2883 term->compatibility_level &= TM_VTXXX;
2886 term->compatibility_level = TM_PUTTY;
2889 term->compatibility_level = TM_SCOANSI;
2893 term->compatibility_level = TM_PUTTY;
2899 /* Change the response to CSI c */
2900 if (term->esc_args[0] == 50) {
2903 strcpy(term->id_string, "\033[?");
2904 for (i = 1; i < term->esc_nargs; i++) {
2906 strcat(term->id_string, ";");
2907 sprintf(lbuf, "%d", term->esc_args[i]);
2908 strcat(term->id_string, lbuf);
2910 strcat(term->id_string, "c");
2913 /* Is this a good idea ?
2914 * Well we should do a soft reset at this point ...
2916 if (!has_compat(VT420) && has_compat(VT100)) {
2917 if (!term->cfg.no_remote_resize) {
2918 if (term->reset_132)
2919 request_resize(132, 24);
2921 request_resize(80, 24);
2929 term->osc_w = FALSE;
2931 case 'P': /* Linux palette sequence */
2932 term->termstate = SEEN_OSC_P;
2933 term->osc_strlen = 0;
2935 case 'R': /* Linux palette reset */
2936 palette_reset(term->frontend);
2937 term_invalidate(term);
2938 term->termstate = TOPLEVEL;
2940 case 'W': /* word-set */
2941 term->termstate = SEEN_OSC_W;
2954 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2958 * Grotty hack to support xterm and DECterm title
2959 * sequences concurrently.
2961 if (term->esc_args[0] == 2) {
2962 term->esc_args[0] = 1;
2965 /* else fall through */
2967 term->termstate = OSC_STRING;
2968 term->osc_strlen = 0;
2973 * This OSC stuff is EVIL. It takes just one character to get into
2974 * sysline mode and it's not initially obvious how to get out.
2975 * So I've added CR and LF as string aborts.
2976 * This shouldn't effect compatibility as I believe embedded
2977 * control characters are supposed to be interpreted (maybe?)
2978 * and they don't display anything useful anyway.
2982 if (c == '\012' || c == '\015') {
2983 term->termstate = TOPLEVEL;
2984 } else if (c == 0234 || c == '\007') {
2986 * These characters terminate the string; ST and BEL
2987 * terminate the sequence and trigger instant
2988 * processing of it, whereas ESC goes back to SEEN_ESC
2989 * mode unless it is followed by \, in which case it is
2990 * synonymous with ST in the first place.
2993 term->termstate = TOPLEVEL;
2994 } else if (c == '\033')
2995 term->termstate = OSC_MAYBE_ST;
2996 else if (term->osc_strlen < OSC_STR_MAX)
2997 term->osc_string[term->osc_strlen++] = c;
3001 int max = (term->osc_strlen == 0 ? 21 : 16);
3003 if (c >= '0' && c <= '9')
3005 else if (c >= 'A' && c <= 'A' + max - 10)
3007 else if (c >= 'a' && c <= 'a' + max - 10)
3010 term->termstate = TOPLEVEL;
3013 term->osc_string[term->osc_strlen++] = val;
3014 if (term->osc_strlen >= 7) {
3015 palette_set(term->frontend, term->osc_string[0],
3016 term->osc_string[1] * 16 + term->osc_string[2],
3017 term->osc_string[3] * 16 + term->osc_string[4],
3018 term->osc_string[5] * 16 + term->osc_string[6]);
3019 term_invalidate(term);
3020 term->termstate = TOPLEVEL;
3036 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
3039 term->termstate = OSC_STRING;
3040 term->osc_strlen = 0;
3044 term->termstate = TOPLEVEL;
3045 term->seen_disp_event = TRUE;
3048 move(term, term->curs.x, term->curs.y - 1, 1);
3051 move(term, term->curs.x, term->curs.y + 1, 1);
3054 move(term, term->curs.x + 1, term->curs.y, 1);
3057 move(term, term->curs.x - 1, term->curs.y, 1);
3060 * From the VT100 Manual
3061 * NOTE: The special graphics characters in the VT100
3062 * are different from those in the VT52
3064 * From VT102 manual:
3065 * 137 _ Blank - Same
3066 * 140 ` Reserved - Humm.
3067 * 141 a Solid rectangle - Similar
3068 * 142 b 1/ - Top half of fraction for the
3069 * 143 c 3/ - subscript numbers below.
3072 * 146 f Degrees - Same
3073 * 147 g Plus or minus - Same
3075 * 151 i Ellipsis (dots)
3078 * 154 l Bar at scan 0
3079 * 155 m Bar at scan 1
3080 * 156 n Bar at scan 2
3081 * 157 o Bar at scan 3 - Similar
3082 * 160 p Bar at scan 4 - Similar
3083 * 161 q Bar at scan 5 - Similar
3084 * 162 r Bar at scan 6 - Same
3085 * 163 s Bar at scan 7 - Similar
3100 term->cset_attr[term->cset = 0] = ATTR_LINEDRW;
3103 term->cset_attr[term->cset = 0] = ATTR_ASCII;
3106 move(term, 0, 0, 0);
3109 if (term->curs.y == 0)
3110 scroll(term, 0, term->rows - 1, -1, TRUE);
3111 else if (term->curs.y > 0)
3114 term->wrapnext = FALSE;
3117 erase_lots(term, FALSE, FALSE, TRUE);
3121 erase_lots(term, TRUE, FALSE, TRUE);
3125 /* XXX Print cursor line */
3128 /* XXX Start controller mode */
3131 /* XXX Stop controller mode */
3135 term->termstate = VT52_Y1;
3139 ldisc_send(term->ldisc, "\033/Z", 3, 0);
3142 term->app_keypad_keys = TRUE;
3145 term->app_keypad_keys = FALSE;
3148 /* XXX This should switch to VT100 mode not current or default
3149 * VT mode. But this will only have effect in a VT220+
3152 term->vt52_mode = FALSE;
3153 term->blink_is_real = term->cfg.blinktext;
3157 /* XXX Enter auto print mode */
3160 /* XXX Exit auto print mode */
3163 /* XXX Print screen */
3169 /* compatibility(ATARI) */
3170 move(term, 0, 0, 0);
3171 erase_lots(term, FALSE, FALSE, TRUE);
3175 /* compatibility(ATARI) */
3176 if (term->curs.y <= term->marg_b)
3177 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
3180 /* compatibility(ATARI) */
3181 if (term->curs.y <= term->marg_b)
3182 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
3185 /* compatibility(ATARI) */
3186 term->termstate = VT52_FG;
3189 /* compatibility(ATARI) */
3190 term->termstate = VT52_BG;
3193 /* compatibility(ATARI) */
3194 erase_lots(term, FALSE, TRUE, FALSE);
3198 /* compatibility(ATARI) */
3199 term->cursor_on = TRUE;
3202 /* compatibility(ATARI) */
3203 term->cursor_on = FALSE;
3205 /* case 'j': Save cursor position - broken on ST */
3206 /* case 'k': Restore cursor position */
3208 /* compatibility(ATARI) */
3209 erase_lots(term, TRUE, TRUE, TRUE);
3211 term->wrapnext = FALSE;
3215 /* compatibility(ATARI) */
3216 erase_lots(term, TRUE, TRUE, FALSE);
3219 /* compatibility(ATARI) */
3220 term->curr_attr |= ATTR_REVERSE;
3223 /* compatibility(ATARI) */
3224 term->curr_attr &= ~ATTR_REVERSE;
3226 case 'v': /* wrap Autowrap on - Wyse style */
3227 /* compatibility(ATARI) */
3230 case 'w': /* Autowrap off */
3231 /* compatibility(ATARI) */
3236 /* compatibility(OTHER) */
3237 term->vt52_bold = FALSE;
3238 term->curr_attr = ATTR_DEFAULT;
3240 term->erase_char = (' ' | ATTR_ASCII |
3242 (ATTR_FGMASK | ATTR_BGMASK)));
3245 /* compatibility(VI50) */
3246 term->curr_attr |= ATTR_UNDER;
3249 /* compatibility(VI50) */
3250 term->curr_attr &= ~ATTR_UNDER;
3253 /* compatibility(VI50) */
3254 term->vt52_bold = TRUE;
3255 term->curr_attr |= ATTR_BOLD;
3258 /* compatibility(VI50) */
3259 term->vt52_bold = FALSE;
3260 term->curr_attr &= ~ATTR_BOLD;
3266 term->termstate = VT52_Y2;
3267 move(term, term->curs.x, c - ' ', 0);
3270 term->termstate = TOPLEVEL;
3271 move(term, c - ' ', term->curs.y, 0);
3276 term->termstate = TOPLEVEL;
3277 term->curr_attr &= ~ATTR_FGMASK;
3278 term->curr_attr &= ~ATTR_BOLD;
3279 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
3280 if ((c & 0x8) || term->vt52_bold)
3281 term->curr_attr |= ATTR_BOLD;
3284 term->erase_char = (' ' | ATTR_ASCII |
3286 (ATTR_FGMASK | ATTR_BGMASK)));
3289 term->termstate = TOPLEVEL;
3290 term->curr_attr &= ~ATTR_BGMASK;
3291 term->curr_attr &= ~ATTR_BLINK;
3292 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
3294 /* Note: bold background */
3296 term->curr_attr |= ATTR_BLINK;
3299 term->erase_char = (' ' | ATTR_ASCII |
3301 (ATTR_FGMASK | ATTR_BGMASK)));
3304 default: break; /* placate gcc warning about enum use */
3306 if (term->selstate != NO_SELECTION) {
3307 pos cursplus = term->curs;
3309 check_selection(term, term->curs, cursplus);
3313 term_print_flush(term);
3318 * Compare two lines to determine whether they are sufficiently
3319 * alike to scroll-optimise one to the other. Return the degree of
3322 static int linecmp(Terminal *term, unsigned long *a, unsigned long *b)
3326 for (i = n = 0; i < term->cols; i++)
3327 n += (*a++ == *b++);
3333 * To prevent having to run the reasonably tricky bidi algorithm
3334 * too many times, we maintain a cache of the last lineful of data
3335 * fed to the algorithm on each line of the display.
3337 static int term_bidi_cache_hit(Terminal *term, int line,
3338 unsigned long *lbefore, int width)
3340 if (!term->pre_bidi_cache)
3341 return FALSE; /* cache doesn't even exist yet! */
3343 if (line >= term->bidi_cache_size)
3344 return FALSE; /* cache doesn't have this many lines */
3346 if (!term->pre_bidi_cache[line])
3347 return FALSE; /* cache doesn't contain _this_ line */
3349 if (!memcmp(term->pre_bidi_cache[line], lbefore,
3350 width * sizeof(unsigned long)))
3351 return TRUE; /* aha! the line matches the cache */
3353 return FALSE; /* it didn't match. */
3356 static void term_bidi_cache_store(Terminal *term, int line,
3357 unsigned long *lbefore,
3358 unsigned long *lafter, int width)
3360 if (!term->pre_bidi_cache || term->bidi_cache_size <= line) {
3361 int j = term->bidi_cache_size;
3362 term->bidi_cache_size = line+1;
3363 term->pre_bidi_cache = sresize(term->pre_bidi_cache,
3364 term->bidi_cache_size,
3366 term->post_bidi_cache = sresize(term->post_bidi_cache,
3367 term->bidi_cache_size,
3369 while (j < term->bidi_cache_size) {
3370 term->pre_bidi_cache[j] = term->post_bidi_cache[j] = NULL;
3375 sfree(term->pre_bidi_cache[line]);
3376 sfree(term->post_bidi_cache[line]);
3378 term->pre_bidi_cache[line] = snewn(width, unsigned long);
3379 term->post_bidi_cache[line] = snewn(width, unsigned long);
3381 memcpy(term->pre_bidi_cache[line], lbefore, width * sizeof(unsigned long));
3382 memcpy(term->post_bidi_cache[line], lafter, width * sizeof(unsigned long));
3386 * Given a context, update the window. Out of paranoia, we don't
3387 * allow WM_PAINT responses to do scrolling optimisations.
3389 static void do_paint(Terminal *term, Context ctx, int may_optimise)
3391 int i, it, j, our_curs_y, our_curs_x;
3392 unsigned long rv, cursor;
3395 long cursor_background = ERASE_CHAR;
3396 unsigned long ticks;
3397 #ifdef OPTIMISE_SCROLL
3398 struct scrollregion *sr;
3399 #endif /* OPTIMISE_SCROLL */
3402 * Check the visual bell state.
3404 if (term->in_vbell) {
3405 ticks = GETTICKCOUNT();
3406 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
3407 term->in_vbell = FALSE;
3410 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
3413 * screen array, disptop, scrtop,
3415 * cfg.blinkpc, blink_is_real, tblinker,
3416 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
3419 /* Has the cursor position or type changed ? */
3420 if (term->cursor_on) {
3421 if (term->has_focus) {
3422 if (term->blinker || !term->cfg.blink_cur)
3423 cursor = TATTR_ACTCURS;
3427 cursor = TATTR_PASCURS;
3429 cursor |= TATTR_RIGHTCURS;
3432 our_curs_y = term->curs.y - term->disptop;
3435 * Adjust the cursor position in the case where it's
3436 * resting on the right-hand half of a CJK wide character.
3437 * xterm's behaviour here, which seems adequate to me, is
3438 * to display the cursor covering the _whole_ character,
3439 * exactly as if it were one space to the left.
3441 unsigned long *ldata = lineptr(term->curs.y);
3442 our_curs_x = term->curs.x;
3443 if (our_curs_x > 0 &&
3444 (ldata[our_curs_x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3448 if (term->dispcurs && (term->curstype != cursor ||
3450 term->disptext + our_curs_y * (term->cols + 1) +
3452 if (term->dispcurs > term->disptext &&
3453 (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3454 term->dispcurs[-1] |= ATTR_INVALID;
3455 if ( (term->dispcurs[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3456 term->dispcurs[1] |= ATTR_INVALID;
3457 *term->dispcurs |= ATTR_INVALID;
3460 term->dispcurs = NULL;
3462 #ifdef OPTIMISE_SCROLL
3464 sr = term->scrollhead;
3466 struct scrollregion *next = sr->next;
3467 do_scroll(ctx, sr->topline, sr->botline, sr->lines);
3471 term->scrollhead = term->scrolltail = NULL;
3472 #endif /* OPTIMISE_SCROLL */
3474 /* The normal screen data */
3475 for (i = 0; i < term->rows; i++) {
3476 unsigned long *ldata;
3478 int idx, dirty_line, dirty_run, selected;
3479 unsigned long attr = 0;
3480 int updated_line = 0;
3483 int last_run_dirty = 0;
3485 scrpos.y = i + term->disptop;
3486 ldata = lineptr(scrpos.y);
3487 lattr = (ldata[term->cols] & LATTR_MODE);
3489 idx = i * (term->cols + 1);
3490 dirty_run = dirty_line = (ldata[term->cols] !=
3491 term->disptext[idx + term->cols]);
3492 term->disptext[idx + term->cols] = ldata[term->cols];
3494 /* Do Arabic shaping and bidi. */
3495 if(!term->cfg.bidi || !term->cfg.arabicshaping) {
3497 if (!term_bidi_cache_hit(term, i, ldata, term->cols)) {
3499 for(it=0; it<term->cols ; it++)
3501 int uc = (ldata[it] & 0xFFFF);
3503 switch (uc & CSET_MASK) {
3505 if (!term->cfg.rawcnp) {
3506 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3510 uc = term->ucsdata->unitab_line[uc & 0xFF];
3513 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3516 switch (uc & CSET_MASK) {
3518 uc = term->ucsdata->unitab_font[uc & 0xFF];
3521 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3525 term->wcFrom[it].origwc = term->wcFrom[it].wc = uc;
3526 term->wcFrom[it].index = it;
3530 do_bidi(term->wcFrom, term->cols);
3532 /* this is saved iff done from inside the shaping */
3533 if(!term->cfg.bidi && term->cfg.arabicshaping)
3534 for(it=0; it<term->cols; it++)
3535 term->wcTo[it] = term->wcFrom[it];
3537 if(!term->cfg.arabicshaping)
3538 do_shape(term->wcFrom, term->wcTo, term->cols);
3540 for(it=0; it<term->cols ; it++)
3542 term->ltemp[it] = ldata[term->wcTo[it].index];
3544 if (term->wcTo[it].origwc != term->wcTo[it].wc)
3545 term->ltemp[it] = ((term->ltemp[it] & 0xFFFF0000) |
3548 term_bidi_cache_store(term, i, ldata, term->ltemp, term->cols);
3549 ldata = term->ltemp;
3551 ldata = term->post_bidi_cache[i];
3555 for (j = 0; j < term->cols; j++, idx++) {
3556 unsigned long tattr, tchar;
3557 unsigned long *d = ldata + j;
3561 tchar = (*d & (CHAR_MASK | CSET_MASK));
3562 tattr = (*d & (ATTR_MASK ^ CSET_MASK));
3563 switch (tchar & CSET_MASK) {
3565 tchar = term->ucsdata->unitab_line[tchar & 0xFF];
3568 tchar = term->ucsdata->unitab_xterm[tchar & 0xFF];
3571 tchar = term->ucsdata->unitab_scoacs[tchar&0xFF];
3574 tattr |= (tchar & CSET_MASK);
3576 if ((d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3579 /* Video reversing things */
3580 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
3581 if (term->seltype == LEXICOGRAPHIC)
3582 selected = (posle(term->selstart, scrpos) &&
3583 poslt(scrpos, term->selend));
3585 selected = (posPle(term->selstart, scrpos) &&
3586 posPlt(scrpos, term->selend));
3590 ^ (selected ? ATTR_REVERSE : 0));
3592 /* 'Real' blinking ? */
3593 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
3594 if (term->has_focus && term->tblinker) {
3595 tchar = term->ucsdata->unitab_line[(unsigned char)' '];
3597 tattr &= ~ATTR_BLINK;
3601 * Check the font we'll _probably_ be using to see if
3602 * the character is wide when we don't want it to be.
3604 if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) {
3605 if ((tattr & ATTR_WIDE) == 0 &&
3606 char_width(ctx, (tchar | tattr) & 0xFFFF) == 2)
3607 tattr |= ATTR_NARROW;
3608 } else if (term->disptext[idx]&ATTR_NARROW)
3609 tattr |= ATTR_NARROW;
3611 /* Cursor here ? Save the 'background' */
3612 if (i == our_curs_y && j == our_curs_x) {
3613 cursor_background = tattr | tchar;
3614 term->dispcurs = term->disptext + idx;
3617 if ((term->disptext[idx] ^ tattr) & ATTR_WIDE)
3620 break_run = (((tattr ^ attr) & term->attr_mask) ||
3621 j - start >= sizeof(ch));
3623 /* Special hack for VT100 Linedraw glyphs */
3624 if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA
3625 && tchar <= 0xBD) break_run = TRUE;
3627 if (!term->ucsdata->dbcs_screenfont && !dirty_line) {
3628 if ((tchar | tattr) == term->disptext[idx])
3630 else if (!dirty_run && ccount == 1)
3635 if ((dirty_run || last_run_dirty) && ccount > 0) {
3636 do_text(ctx, start, i, ch, ccount, attr, lattr);
3642 if (term->ucsdata->dbcs_screenfont)
3643 last_run_dirty = dirty_run;
3644 dirty_run = dirty_line;
3647 if ((tchar | tattr) != term->disptext[idx])
3649 ch[ccount++] = (char) tchar;
3650 term->disptext[idx] = tchar | tattr;
3652 /* If it's a wide char step along to the next one. */
3653 if (tattr & ATTR_WIDE) {
3654 if (++j < term->cols) {
3658 * By construction above, the cursor should not
3659 * be on the right-hand half of this character.
3662 assert(!(i == our_curs_y && j == our_curs_x));
3663 if (term->disptext[idx] != *d)
3665 term->disptext[idx] = *d;
3669 if (dirty_run && ccount > 0) {
3670 do_text(ctx, start, i, ch, ccount, attr, lattr);
3674 /* Cursor on this line ? (and changed) */
3675 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
3676 ch[0] = (char) (cursor_background & CHAR_MASK);
3677 attr = (cursor_background & ATTR_MASK) | cursor;
3678 do_cursor(ctx, our_curs_x, i, ch, 1, attr, lattr);
3679 term->curstype = cursor;
3685 * Flick the switch that says if blinking things should be shown or hidden.
3688 void term_blink(Terminal *term, int flg)
3690 long now, blink_diff;
3692 now = GETTICKCOUNT();
3693 blink_diff = now - term->last_tblink;
3695 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3696 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
3697 term->last_tblink = now;
3698 term->tblinker = !term->tblinker;
3703 term->last_blink = now;
3707 blink_diff = now - term->last_blink;
3709 /* Make sure the cursor blinks no faster than system blink rate */
3710 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
3713 term->last_blink = now;
3714 term->blinker = !term->blinker;
3718 * Invalidate the whole screen so it will be repainted in full.
3720 void term_invalidate(Terminal *term)
3724 for (i = 0; i < term->rows * (term->cols + 1); i++)
3725 term->disptext[i] = ATTR_INVALID;
3729 * Paint the window in response to a WM_PAINT message.
3731 void term_paint(Terminal *term, Context ctx,
3732 int left, int top, int right, int bottom, int immediately)
3735 if (left < 0) left = 0;
3736 if (top < 0) top = 0;
3737 if (right >= term->cols) right = term->cols-1;
3738 if (bottom >= term->rows) bottom = term->rows-1;
3740 for (i = top; i <= bottom && i < term->rows; i++) {
3741 if ((term->disptext[i * (term->cols + 1) + term->cols] &
3742 LATTR_MODE) == LATTR_NORM)
3743 for (j = left; j <= right && j < term->cols; j++)
3744 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3746 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
3747 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3750 /* This should happen soon enough, also for some reason it sometimes
3751 * fails to actually do anything when re-sizing ... painting the wrong
3755 do_paint (term, ctx, FALSE);
3759 * Attempt to scroll the scrollback. The second parameter gives the
3760 * position we want to scroll to; the first is +1 to denote that
3761 * this position is relative to the beginning of the scrollback, -1
3762 * to denote it is relative to the end, and 0 to denote that it is
3763 * relative to the current position.
3765 void term_scroll(Terminal *term, int rel, int where)
3767 int sbtop = -sblines(term);
3768 #ifdef OPTIMISE_SCROLL
3769 int olddisptop = term->disptop;
3771 #endif /* OPTIMISE_SCROLL */
3773 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
3774 if (term->disptop < sbtop)
3775 term->disptop = sbtop;
3776 if (term->disptop > 0)
3779 #ifdef OPTIMISE_SCROLL
3780 shift = (term->disptop - olddisptop);
3781 if (shift < term->rows && shift > -term->rows)
3782 scroll_display(term, 0, term->rows - 1, shift);
3783 #endif /* OPTIMISE_SCROLL */
3787 static void clipme(Terminal *term, pos top, pos bottom, int rect, int desel)
3790 wchar_t *wbptr; /* where next char goes within workbuf */
3792 int wblen = 0; /* workbuf len */
3793 int buflen; /* amount of memory allocated to workbuf */
3795 buflen = 5120; /* Default size */
3796 workbuf = snewn(buflen, wchar_t);
3797 wbptr = workbuf; /* start filling here */
3798 old_top_x = top.x; /* needed for rect==1 */
3800 while (poslt(top, bottom)) {
3802 unsigned long *ldata = lineptr(top.y);
3806 * nlpos will point at the maximum position on this line we
3807 * should copy up to. So we start it at the end of the
3811 nlpos.x = term->cols;
3814 * ... move it backwards if there's unused space at the end
3815 * of the line (and also set `nl' if this is the case,
3816 * because in normal selection mode this means we need a
3817 * newline at the end)...
3819 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3820 while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 ||
3821 (DIRECT_CHAR(ldata[nlpos.x - 1]) &&
3822 (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20))
3823 && poslt(top, nlpos))
3825 if (poslt(nlpos, bottom))
3827 } else if (ldata[term->cols] & LATTR_WRAPPED2) {
3828 /* Ignore the last char on the line in a WRAPPED2 line. */
3833 * ... and then clip it to the terminal x coordinate if
3834 * we're doing rectangular selection. (In this case we
3835 * still did the above, so that copying e.g. the right-hand
3836 * column from a table doesn't fill with spaces on the
3840 if (nlpos.x > bottom.x)
3842 nl = (top.y < bottom.y);
3845 while (poslt(top, bottom) && poslt(top, nlpos)) {
3848 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
3850 wchar_t cbuf[16], *p;
3851 int uc = (ldata[top.x] & 0xFFFF);
3854 if (uc == UCSWIDE) {
3859 switch (uc & CSET_MASK) {
3861 if (!term->cfg.rawcnp) {
3862 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
3866 uc = term->ucsdata->unitab_line[uc & 0xFF];
3869 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
3872 switch (uc & CSET_MASK) {
3874 uc = term->ucsdata->unitab_font[uc & 0xFF];
3877 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
3881 set = (uc & CSET_MASK);
3882 c = (uc & CHAR_MASK);
3886 if (DIRECT_FONT(uc)) {
3887 if (c >= ' ' && c != 0x7F) {
3891 if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) {
3893 buf[1] = (char) (0xFF & ldata[top.x + 1]);
3894 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4);
3898 rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4);
3902 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
3909 for (p = cbuf; *p; p++) {
3910 /* Enough overhead for trailing NL and nul */
3911 if (wblen >= buflen - 16) {
3913 workbuf = sresize(workbuf, buflen, wchar_t);
3914 wbptr = workbuf + wblen;
3923 for (i = 0; i < sel_nl_sz; i++) {
3925 *wbptr++ = sel_nl[i];
3929 top.x = rect ? old_top_x : 0;
3931 #if SELECTION_NUL_TERMINATED
3935 write_clip(term->frontend, workbuf, wblen, desel); /* transfer to clipbd */
3936 if (buflen > 0) /* indicates we allocated this buffer */
3940 void term_copyall(Terminal *term)
3944 tree234 *screen = term->screen;
3945 top.y = -sblines(term);
3947 bottom.y = find_last_nonempty_line(term, screen);
3948 bottom.x = term->cols;
3949 clipme(term, top, bottom, 0, TRUE);
3953 * The wordness array is mainly for deciding the disposition of the
3954 * US-ASCII characters.
3956 static int wordtype(Terminal *term, int uc)
3959 int start, end, ctype;
3961 static const struct ucsword ucs_words[] = {
3967 0x037e, 0x037e, 1}, /* Greek question mark */
3969 0x0387, 0x0387, 1}, /* Greek ano teleia */
3971 0x055a, 0x055f, 1}, /* Armenian punctuation */
3973 0x0589, 0x0589, 1}, /* Armenian full stop */
3975 0x0700, 0x070d, 1}, /* Syriac punctuation */
3977 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3979 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3981 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3983 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3985 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3987 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3989 0x2000, 0x200a, 0}, /* Various spaces */
3991 0x2070, 0x207f, 2}, /* superscript */
3993 0x2080, 0x208f, 2}, /* subscript */
3995 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3997 0x3000, 0x3000, 0}, /* ideographic space */
3999 0x3001, 0x3020, 1}, /* ideographic punctuation */
4001 0x303f, 0x309f, 3}, /* Hiragana */
4003 0x30a0, 0x30ff, 3}, /* Katakana */
4005 0x3300, 0x9fff, 3}, /* CJK Ideographs */
4007 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
4009 0xf900, 0xfaff, 3}, /* CJK Ideographs */
4011 0xfe30, 0xfe6b, 1}, /* punctuation forms */
4013 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
4015 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
4017 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
4019 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
4021 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
4025 const struct ucsword *wptr;
4027 uc &= (CSET_MASK | CHAR_MASK);
4029 switch (uc & CSET_MASK) {
4031 uc = term->ucsdata->unitab_xterm[uc & 0xFF];
4034 uc = term->ucsdata->unitab_line[uc & 0xFF];
4037 uc = term->ucsdata->unitab_scoacs[uc&0xFF];
4040 switch (uc & CSET_MASK) {
4042 uc = term->ucsdata->unitab_font[uc & 0xFF];
4045 uc = term->ucsdata->unitab_oemcp[uc & 0xFF];
4049 /* For DBCS font's I can't do anything usefull. Even this will sometimes
4050 * fail as there's such a thing as a double width space. :-(
4052 if (term->ucsdata->dbcs_screenfont &&
4053 term->ucsdata->font_codepage == term->ucsdata->line_codepage)
4057 return term->wordness[uc];
4059 for (wptr = ucs_words; wptr->start; wptr++) {
4060 if (uc >= wptr->start && uc <= wptr->end)
4068 * Spread the selection outwards according to the selection mode.
4070 static pos sel_spread_half(Terminal *term, pos p, int dir)
4072 unsigned long *ldata;
4074 int topy = -sblines(term);
4076 ldata = lineptr(p.y);
4078 switch (term->selmode) {
4081 * In this mode, every character is a separate unit, except
4082 * for runs of spaces at the end of a non-wrapping line.
4084 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
4085 unsigned long *q = ldata + term->cols;
4086 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
4088 if (q == ldata + term->cols)
4090 if (p.x >= q - ldata)
4091 p.x = (dir == -1 ? q - ldata : term->cols - 1);
4096 * In this mode, the units are maximal runs of characters
4097 * whose `wordness' has the same value.
4099 wvalue = wordtype(term, UCSGET(ldata, p.x));
4102 int maxcols = (ldata[term->cols] & LATTR_WRAPPED2 ?
4103 term->cols-1 : term->cols);
4104 if (p.x < maxcols-1) {
4105 if (wordtype(term, UCSGET(ldata, p.x + 1)) == wvalue)
4110 if (ldata[term->cols] & LATTR_WRAPPED) {
4111 unsigned long *ldata2;
4112 ldata2 = lineptr(p.y+1);
4113 if (wordtype(term, UCSGET(ldata2, 0)) == wvalue) {
4126 if (wordtype(term, UCSGET(ldata, p.x - 1)) == wvalue)
4131 unsigned long *ldata2;
4135 ldata2 = lineptr(p.y-1);
4136 maxcols = (ldata2[term->cols] & LATTR_WRAPPED2 ?
4137 term->cols-1 : term->cols);
4138 if (ldata2[term->cols] & LATTR_WRAPPED) {
4139 if (wordtype(term, UCSGET(ldata2, maxcols-1))
4154 * In this mode, every line is a unit.
4156 p.x = (dir == -1 ? 0 : term->cols - 1);
4162 static void sel_spread(Terminal *term)
4164 if (term->seltype == LEXICOGRAPHIC) {
4165 term->selstart = sel_spread_half(term, term->selstart, -1);
4166 decpos(term->selend);
4167 term->selend = sel_spread_half(term, term->selend, +1);
4168 incpos(term->selend);
4172 void term_do_paste(Terminal *term)
4177 get_clip(term->frontend, &data, &len);
4178 if (data && len > 0) {
4181 term_seen_key_event(term); /* pasted data counts */
4183 if (term->paste_buffer)
4184 sfree(term->paste_buffer);
4185 term->paste_pos = term->paste_hold = term->paste_len = 0;
4186 term->paste_buffer = snewn(len, wchar_t);
4189 while (p < data + len) {
4190 while (p < data + len &&
4191 !(p <= data + len - sel_nl_sz &&
4192 !memcmp(p, sel_nl, sizeof(sel_nl))))
4197 for (i = 0; i < p - q; i++) {
4198 term->paste_buffer[term->paste_len++] = q[i];
4202 if (p <= data + len - sel_nl_sz &&
4203 !memcmp(p, sel_nl, sizeof(sel_nl))) {
4204 term->paste_buffer[term->paste_len++] = '\015';
4210 /* Assume a small paste will be OK in one go. */
4211 if (term->paste_len < 256) {
4213 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
4214 if (term->paste_buffer)
4215 sfree(term->paste_buffer);
4216 term->paste_buffer = 0;
4217 term->paste_pos = term->paste_hold = term->paste_len = 0;
4220 get_clip(term->frontend, NULL, NULL);
4223 void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked,
4224 Mouse_Action a, int x, int y, int shift, int ctrl, int alt)
4227 unsigned long *ldata;
4228 int raw_mouse = (term->xterm_mouse &&
4229 !term->cfg.no_mouse_rep &&
4230 !(term->cfg.mouse_override && shift));
4231 int default_seltype;
4235 if (a == MA_DRAG && !raw_mouse)
4236 term_scroll(term, 0, -1);
4238 if (y >= term->rows) {
4240 if (a == MA_DRAG && !raw_mouse)
4241 term_scroll(term, 0, +1);
4250 if (x >= term->cols)
4253 selpoint.y = y + term->disptop;
4255 ldata = lineptr(selpoint.y);
4256 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM)
4260 int encstate = 0, r, c;
4267 encstate = 0x20; /* left button down */
4278 case MBT_WHEEL_DOWN:
4281 default: break; /* placate gcc warning about enum use */
4285 if (term->xterm_mouse == 1)
4291 term->mouse_is_down = 0;
4294 if (term->mouse_is_down == braw)
4296 term->mouse_is_down = braw;
4298 default: break; /* placate gcc warning about enum use */
4307 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
4308 ldisc_send(term->ldisc, abuf, 6, 0);
4314 * Set the selection type (rectangular or normal) at the start
4315 * of a selection attempt, from the state of Alt.
4317 if (!alt ^ !term->cfg.rect_select)
4318 default_seltype = RECTANGULAR;
4320 default_seltype = LEXICOGRAPHIC;
4322 if (term->selstate == NO_SELECTION) {
4323 term->seltype = default_seltype;
4326 if (bcooked == MBT_SELECT && a == MA_CLICK) {
4328 term->selstate = ABOUT_TO;
4329 term->seltype = default_seltype;
4330 term->selanchor = selpoint;
4331 term->selmode = SM_CHAR;
4332 } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
4334 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
4335 term->selstate = DRAGGING;
4336 term->selstart = term->selanchor = selpoint;
4337 term->selend = term->selstart;
4338 incpos(term->selend);
4340 } else if ((bcooked == MBT_SELECT && a == MA_DRAG) ||
4341 (bcooked == MBT_EXTEND && a != MA_RELEASE)) {
4342 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
4344 if (bcooked == MBT_EXTEND && a != MA_DRAG &&
4345 term->selstate == SELECTED) {
4346 if (term->seltype == LEXICOGRAPHIC) {
4348 * For normal selection, we extend by moving
4349 * whichever end of the current selection is closer
4352 if (posdiff(selpoint, term->selstart) <
4353 posdiff(term->selend, term->selstart) / 2) {
4354 term->selanchor = term->selend;
4355 decpos(term->selanchor);
4357 term->selanchor = term->selstart;
4361 * For rectangular selection, we have a choice of
4362 * _four_ places to put selanchor and selpoint: the
4363 * four corners of the selection.
4365 if (2*selpoint.x < term->selstart.x + term->selend.x)
4366 term->selanchor.x = term->selend.x-1;
4368 term->selanchor.x = term->selstart.x;
4370 if (2*selpoint.y < term->selstart.y + term->selend.y)
4371 term->selanchor.y = term->selend.y;
4373 term->selanchor.y = term->selstart.y;
4375 term->selstate = DRAGGING;
4377 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
4378 term->selanchor = selpoint;
4379 term->selstate = DRAGGING;
4380 if (term->seltype == LEXICOGRAPHIC) {
4382 * For normal selection, we set (selstart,selend) to
4383 * (selpoint,selanchor) in some order.
4385 if (poslt(selpoint, term->selanchor)) {
4386 term->selstart = selpoint;
4387 term->selend = term->selanchor;
4388 incpos(term->selend);
4390 term->selstart = term->selanchor;
4391 term->selend = selpoint;
4392 incpos(term->selend);
4396 * For rectangular selection, we may need to
4397 * interchange x and y coordinates (if the user has
4398 * dragged in the -x and +y directions, or vice versa).
4400 term->selstart.x = min(term->selanchor.x, selpoint.x);
4401 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
4402 term->selstart.y = min(term->selanchor.y, selpoint.y);
4403 term->selend.y = max(term->selanchor.y, selpoint.y);
4406 } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) &&
4408 if (term->selstate == DRAGGING) {
4410 * We've completed a selection. We now transfer the
4411 * data to the clipboard.
4413 clipme(term, term->selstart, term->selend,
4414 (term->seltype == RECTANGULAR), FALSE);
4415 term->selstate = SELECTED;
4417 term->selstate = NO_SELECTION;
4418 } else if (bcooked == MBT_PASTE
4420 #if MULTICLICK_ONLY_EVENT
4421 || a == MA_2CLK || a == MA_3CLK
4424 request_paste(term->frontend);
4430 void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen,
4431 unsigned int modifiers, unsigned int flags)
4435 int prependesc = FALSE;
4439 fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen);
4440 for (i = 0; i < tlen; i++)
4441 fprintf(stderr, " %04x", (unsigned)text[i]);
4442 fprintf(stderr, "\n");
4446 if ((flags & PKF_REPEAT) && term->repeat_off)
4449 /* Currently, Meta always just prefixes everything with ESC. */
4450 if (modifiers & PKM_META)
4452 modifiers &= ~PKM_META;
4455 * Alt is only used for Alt+keypad, which isn't supported yet, so
4458 modifiers &= ~PKM_ALT;
4460 /* Standard local function keys */
4461 switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) {
4463 if (keysym == PK_PAGEUP)
4464 /* scroll up one page */;
4465 if (keysym == PK_PAGEDOWN)
4466 /* scroll down on page */;
4467 if (keysym == PK_INSERT)
4468 term_do_paste(term);
4471 if (keysym == PK_PAGEUP)
4472 /* scroll up one line */;
4473 if (keysym == PK_PAGEDOWN)
4474 /* scroll down one line */;
4475 /* Control-Numlock for app-keypad mode switch */
4476 if (keysym == PK_PF1)
4477 term->app_keypad_keys ^= 1;
4481 if (modifiers & PKM_ALT) {
4482 /* Alt+F4 (close) */
4483 /* Alt+Return (full screen) */
4484 /* Alt+Space (system menu) */
4487 if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 &&
4488 text[0] >= 0x20 && text[0] <= 0x7e) {
4489 /* ASCII chars + Control */
4490 if ((text[0] >= 0x40 && text[0] <= 0x5f) ||
4491 (text[0] >= 0x61 && text[0] <= 0x7a))
4495 * Control-2 should return ^@ (0x00), Control-6 should return
4496 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
4497 * the DOS keyboard handling did it, and we have nothing better
4498 * to do with the key combo in question, we'll also map
4499 * Control-Backquote to ^\ (0x1C).
4502 case ' ': text[0] = 0x00; break;
4503 case '-': text[0] = 0x1f; break;
4504 case '/': text[0] = 0x1f; break;
4505 case '2': text[0] = 0x00; break;
4506 case '3': text[0] = 0x1b; break;
4507 case '4': text[0] = 0x1c; break;
4508 case '5': text[0] = 0x1d; break;
4509 case '6': text[0] = 0x1e; break;
4510 case '7': text[0] = 0x1f; break;
4511 case '8': text[0] = 0x7f; break;
4512 case '`': text[0] = 0x1c; break;
4517 /* Nethack keypad */
4518 if (term->cfg.nethack_keypad) {
4521 case PK_KP1: c = 'b'; break;
4522 case PK_KP2: c = 'j'; break;
4523 case PK_KP3: c = 'n'; break;
4524 case PK_KP4: c = 'h'; break;
4525 case PK_KP5: c = '.'; break;
4526 case PK_KP6: c = 'l'; break;
4527 case PK_KP7: c = 'y'; break;
4528 case PK_KP8: c = 'k'; break;
4529 case PK_KP9: c = 'u'; break;
4530 default: break; /* else gcc warns `enum value not used' */
4534 if (modifiers & PKM_CONTROL)
4536 else if (modifiers & PKM_SHIFT)
4544 /* Numeric Keypad */
4545 if (PK_ISKEYPAD(keysym)) {
4549 * In VT400 mode, PFn always emits an escape sequence. In
4550 * Linux and tilde modes, this only happens in app keypad mode.
4552 if (term->cfg.funky_type == FUNKY_VT400 ||
4553 ((term->cfg.funky_type == FUNKY_LINUX ||
4554 term->cfg.funky_type == FUNKY_TILDE) &&
4555 term->app_keypad_keys && !term->cfg.no_applic_k)) {
4557 case PK_PF1: xkey = 'P'; break;
4558 case PK_PF2: xkey = 'Q'; break;
4559 case PK_PF3: xkey = 'R'; break;
4560 case PK_PF4: xkey = 'S'; break;
4561 default: break; /* else gcc warns `enum value not used' */
4564 if (term->app_keypad_keys && !term->cfg.no_applic_k) {
4566 case PK_KP0: xkey = 'p'; break;
4567 case PK_KP1: xkey = 'q'; break;
4568 case PK_KP2: xkey = 'r'; break;
4569 case PK_KP3: xkey = 's'; break;
4570 case PK_KP4: xkey = 't'; break;
4571 case PK_KP5: xkey = 'u'; break;
4572 case PK_KP6: xkey = 'v'; break;
4573 case PK_KP7: xkey = 'w'; break;
4574 case PK_KP8: xkey = 'x'; break;
4575 case PK_KP9: xkey = 'y'; break;
4576 case PK_KPDECIMAL: xkey = 'n'; break;
4577 case PK_KPENTER: xkey = 'M'; break;
4578 default: break; /* else gcc warns `enum value not used' */
4580 if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) {
4582 * xterm can't see the layout of the keypad, so it has
4583 * to rely on the X keysyms returned by the keys.
4584 * Hence, we look at the strings here, not the PuTTY
4585 * keysyms (which describe the layout).
4589 if (modifiers & PKM_SHIFT)
4594 case '/': xkey = 'o'; break;
4595 case '*': xkey = 'j'; break;
4596 case '-': xkey = 'm'; break;
4600 * In all other modes, we try to retain the layout of
4601 * the DEC keypad in application mode.
4605 /* This key covers the '-' and ',' keys on a VT220 */
4606 if (modifiers & PKM_SHIFT)
4607 xkey = 'm'; /* VT220 '-' */
4609 xkey = 'l'; /* VT220 ',' */
4611 case PK_KPMINUS: xkey = 'm'; break;
4612 case PK_KPCOMMA: xkey = 'l'; break;
4613 default: break; /* else gcc warns `enum value not used' */
4618 if (term->vt52_mode) {
4619 if (xkey >= 'P' && xkey <= 'S')
4620 p += sprintf((char *) p, "\x1B%c", xkey);
4622 p += sprintf((char *) p, "\x1B?%c", xkey);
4624 p += sprintf((char *) p, "\x1BO%c", xkey);
4627 /* Not in application mode -- treat the number pad as arrow keys? */
4628 if ((flags & PKF_NUMLOCK) == 0) {
4630 case PK_KP0: keysym = PK_INSERT; break;
4631 case PK_KP1: keysym = PK_END; break;
4632 case PK_KP2: keysym = PK_DOWN; break;
4633 case PK_KP3: keysym = PK_PAGEDOWN; break;
4634 case PK_KP4: keysym = PK_LEFT; break;
4635 case PK_KP5: keysym = PK_REST; break;
4636 case PK_KP6: keysym = PK_RIGHT; break;
4637 case PK_KP7: keysym = PK_HOME; break;
4638 case PK_KP8: keysym = PK_UP; break;
4639 case PK_KP9: keysym = PK_PAGEUP; break;
4640 default: break; /* else gcc warns `enum value not used' */
4645 /* Miscellaneous keys */
4652 *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08);
4653 else if (modifiers == PKM_SHIFT)
4654 /* We do the opposite of what is configured */
4655 *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F);
4661 else if (modifiers == PKM_SHIFT)
4662 *p++ = 0x1B, *p++ = '[', *p++ = 'Z';
4665 /* XXX window.c has ctrl+shift+space sending 0xa0 */
4667 if (modifiers == PKM_CONTROL)
4672 case PK_KPENTER: /* Odd keypad modes handled above */
4673 if (modifiers == 0) {
4675 if (term->cr_lf_return)
4679 default: break; /* else gcc warns `enum value not used' */
4682 /* SCO function keys and editing keys */
4683 if (term->cfg.funky_type == FUNKY_SCO) {
4684 if (PK_ISFKEY(keysym) && keysym <= PK_F12) {
4685 static char const codes[] =
4686 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
4687 int index = keysym - PK_F1;
4689 if (modifiers & PKM_SHIFT) index += 12;
4690 if (modifiers & PKM_CONTROL) index += 24;
4691 p += sprintf((char *) p, "\x1B[%c", codes[index]);
4694 if (PK_ISEDITING(keysym)) {
4698 case PK_DELETE: *p++ = 0x7f; goto done;
4699 case PK_HOME: xkey = 'H'; break;
4700 case PK_INSERT: xkey = 'L'; break;
4701 case PK_END: xkey = 'F'; break;
4702 case PK_PAGEUP: xkey = 'I'; break;
4703 case PK_PAGEDOWN: xkey = 'G'; break;
4704 default: break; /* else gcc warns `enum value not used' */
4706 p += sprintf((char *) p, "\x1B[%c", xkey);
4710 if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) {
4713 if (term->cfg.funky_type == FUNKY_XTERM) {
4714 /* Xterm shuffles these keys, apparently. */
4716 case PK_HOME: keysym = PK_INSERT; break;
4717 case PK_INSERT: keysym = PK_HOME; break;
4718 case PK_DELETE: keysym = PK_END; break;
4719 case PK_END: keysym = PK_PAGEUP; break;
4720 case PK_PAGEUP: keysym = PK_DELETE; break;
4721 case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break;
4722 default: break; /* else gcc warns `enum value not used' */
4727 if (term->cfg.rxvt_homeend &&
4728 (keysym == PK_HOME || keysym == PK_END)) {
4729 p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw");
4733 if (term->vt52_mode) {
4737 * A real VT52 doesn't have these, and a VT220 doesn't
4738 * send anything for them in VT52 mode.
4741 case PK_HOME: xkey = 'H'; break;
4742 case PK_INSERT: xkey = 'L'; break;
4743 case PK_DELETE: xkey = 'M'; break;
4744 case PK_END: xkey = 'E'; break;
4745 case PK_PAGEUP: xkey = 'I'; break;
4746 case PK_PAGEDOWN: xkey = 'G'; break;
4747 default: xkey=0; break; /* else gcc warns `enum value not used'*/
4749 p += sprintf((char *) p, "\x1B%c", xkey);
4754 case PK_HOME: code = 1; break;
4755 case PK_INSERT: code = 2; break;
4756 case PK_DELETE: code = 3; break;
4757 case PK_END: code = 4; break;
4758 case PK_PAGEUP: code = 5; break;
4759 case PK_PAGEDOWN: code = 6; break;
4760 default: code = 0; break; /* else gcc warns `enum value not used' */
4762 p += sprintf((char *) p, "\x1B[%d~", code);
4766 if (PK_ISFKEY(keysym)) {
4767 /* Map Shift+F1-F10 to F11-F20 */
4768 if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT))
4770 if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) &&
4772 /* XXX This overrides the XTERM/VT52 mode below */
4774 if (keysym >= PK_F6) offt++;
4775 if (keysym >= PK_F12) offt++;
4776 p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c",
4777 'P' + keysym - PK_F1 - offt);
4780 if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) {
4781 p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1);
4784 if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) {
4785 if (term->vt52_mode)
4786 p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1);
4788 p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1);
4791 p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1);
4795 if (PK_ISCURSOR(keysym)) {
4799 case PK_UP: xkey = 'A'; break;
4800 case PK_DOWN: xkey = 'B'; break;
4801 case PK_RIGHT: xkey = 'C'; break;
4802 case PK_LEFT: xkey = 'D'; break;
4803 case PK_REST: xkey = 'G'; break; /* centre key on number pad */
4804 default: xkey = 0; break; /* else gcc warns `enum value not used' */
4806 if (term->vt52_mode)
4807 p += sprintf((char *) p, "\x1B%c", xkey);
4809 int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c);
4811 /* Useful mapping of Ctrl-arrows */
4812 if (modifiers == PKM_CONTROL)
4816 p += sprintf((char *) p, "\x1BO%c", xkey);
4818 p += sprintf((char *) p, "\x1B[%c", xkey);
4824 if (p > output || tlen > 0) {
4826 * Interrupt an ongoing paste. I'm not sure
4827 * this is sensible, but for the moment it's
4828 * preferable to having to faff about buffering
4834 * We need not bother about stdin backlogs
4835 * here, because in GUI PuTTY we can't do
4836 * anything about it anyway; there's no means
4837 * of asking Windows to hold off on KEYDOWN
4838 * messages. We _have_ to buffer everything
4841 term_seen_key_event(term);
4845 fprintf(stderr, "sending ESC\n");
4847 ldisc_send(term->ldisc, "\x1b", 1, 1);
4852 fprintf(stderr, "sending %d bytes:", p - output);
4853 for (i = 0; i < p - output; i++)
4854 fprintf(stderr, " %02x", output[i]);
4855 fprintf(stderr, "\n");
4857 ldisc_send(term->ldisc, output, p - output, 1);
4858 } else if (tlen > 0) {
4860 fprintf(stderr, "sending %d unichars:", tlen);
4861 for (i = 0; i < tlen; i++)
4862 fprintf(stderr, " %04x", (unsigned) text[i]);
4863 fprintf(stderr, "\n");
4865 luni_send(term->ldisc, text, tlen, 1);
4870 void term_nopaste(Terminal *term)
4872 if (term->paste_len == 0)
4874 sfree(term->paste_buffer);
4875 term->paste_buffer = NULL;
4876 term->paste_len = 0;
4879 int term_paste_pending(Terminal *term)
4881 return term->paste_len != 0;
4884 void term_paste(Terminal *term)
4886 long now, paste_diff;
4888 if (term->paste_len == 0)
4891 /* Don't wait forever to paste */
4892 if (term->paste_hold) {
4893 now = GETTICKCOUNT();
4894 paste_diff = now - term->last_paste;
4895 if (paste_diff >= 0 && paste_diff < 450)
4898 term->paste_hold = 0;
4900 while (term->paste_pos < term->paste_len) {
4902 while (n + term->paste_pos < term->paste_len) {
4903 if (term->paste_buffer[term->paste_pos + n++] == '\015')
4907 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
4908 term->paste_pos += n;
4910 if (term->paste_pos < term->paste_len) {
4911 term->paste_hold = 1;
4915 sfree(term->paste_buffer);
4916 term->paste_buffer = NULL;
4917 term->paste_len = 0;
4920 static void deselect(Terminal *term)
4922 term->selstate = NO_SELECTION;
4923 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
4926 void term_deselect(Terminal *term)
4932 int term_ldisc(Terminal *term, int option)
4934 if (option == LD_ECHO)
4935 return term->term_echoing;
4936 if (option == LD_EDIT)
4937 return term->term_editing;
4941 int term_data(Terminal *term, int is_stderr, const char *data, int len)
4943 bufchain_add(&term->inbuf, data, len);
4945 if (!term->in_term_out) {
4946 term->in_term_out = TRUE;
4947 term_blink(term, 1);
4949 term->in_term_out = FALSE;
4953 * term_out() always completely empties inbuf. Therefore,
4954 * there's no reason at all to return anything other than zero
4955 * from this function, because there _can't_ be a question of
4956 * the remote side needing to wait until term_out() has cleared
4959 * This is a slightly suboptimal way to deal with SSH2 - in
4960 * principle, the window mechanism would allow us to continue
4961 * to accept data on forwarded ports and X connections even
4962 * while the terminal processing was going slowly - but we
4963 * can't do the 100% right thing without moving the terminal
4964 * processing into a separate thread, and that might hurt
4965 * portability. So we manage stdout buffering the old SSH1 way:
4966 * if the terminal processing goes slowly, the whole SSH
4967 * connection stops accepting data until it's ready.
4969 * In practice, I can't imagine this causing serious trouble.
4974 void term_provide_logctx(Terminal *term, void *logctx)
4976 term->logctx = logctx;