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 #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
60 const wchar_t sel_nl[] = SEL_NL;
63 * Internal prototypes.
65 static void do_paint(Terminal *, Context, int);
66 static void erase_lots(Terminal *, int, int, int);
67 static void swap_screen(Terminal *, int, int, int);
68 static void update_sbar(Terminal *);
69 static void deselect(Terminal *);
70 static void term_print_finish(Terminal *);
71 #ifdef OPTIMISE_SCROLL
72 static void scroll_display(Terminal *, int, int, int);
73 #endif /* OPTIMISE_SCROLL */
76 * Resize a line to make it `cols' columns wide.
78 unsigned long *resizeline(unsigned long *line, int cols)
81 unsigned long lineattrs;
83 if (line[0] != (unsigned long)cols) {
85 * This line is the wrong length, which probably means it
86 * hasn't been accessed since a resize. Resize it now.
89 lineattrs = line[oldlen + 1];
90 line = srealloc(line, TSIZE * (2 + cols));
92 for (i = oldlen; i < cols; i++)
93 line[i + 1] = ERASE_CHAR;
94 line[cols + 1] = lineattrs & LATTR_MODE;
101 * Retrieve a line of the screen or of the scrollback, according to
102 * whether the y coordinate is non-negative or negative
105 unsigned long *lineptr(Terminal *term, int y, int lineno)
107 unsigned long *line, *newline;
112 whichtree = term->screen;
115 whichtree = term->scrollback;
116 treeindex = y + count234(term->scrollback);
118 line = index234(whichtree, treeindex);
120 /* We assume that we don't screw up and retrieve something out of range. */
121 assert(line != NULL);
123 newline = resizeline(line, term->cols);
124 if (newline != line) {
125 delpos234(whichtree, treeindex);
126 addpos234(whichtree, newline, treeindex);
133 #define lineptr(x) lineptr(term,x,__LINE__)
136 * Set up power-on settings for the terminal.
138 static void power_on(Terminal *term)
140 term->curs.x = term->curs.y = 0;
141 term->alt_x = term->alt_y = 0;
142 term->savecurs.x = term->savecurs.y = 0;
143 term->alt_t = term->marg_t = 0;
144 if (term->rows != -1)
145 term->alt_b = term->marg_b = term->rows - 1;
147 term->alt_b = term->marg_b = 0;
148 if (term->cols != -1) {
150 for (i = 0; i < term->cols; i++)
151 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
153 term->alt_om = term->dec_om = term->cfg->dec_om;
154 term->alt_ins = term->insert = FALSE;
155 term->alt_wnext = term->wrapnext = term->save_wnext = FALSE;
156 term->alt_wrap = term->wrap = term->cfg->wrap_mode;
157 term->alt_cset = term->cset = term->save_cset = 0;
158 term->alt_utf = term->utf = term->save_utf = 0;
160 term->alt_sco_acs = term->sco_acs = term->save_sco_acs = 0;
161 term->cset_attr[0] = term->cset_attr[1] = term->save_csattr = ATTR_ASCII;
163 term->in_vbell = FALSE;
165 term->big_cursor = 0;
166 term->save_attr = term->curr_attr = ATTR_DEFAULT;
167 term->term_editing = term->term_echoing = FALSE;
168 term->app_cursor_keys = term->cfg->app_cursor;
169 term->app_keypad_keys = term->cfg->app_keypad;
170 term->use_bce = term->cfg->bce;
171 term->blink_is_real = term->cfg->blinktext;
172 term->erase_char = ERASE_CHAR;
174 term_print_finish(term);
177 for (i = 0; i < 256; i++)
178 term->wordness[i] = term->cfg->wordness[i];
181 swap_screen(term, 1, FALSE, FALSE);
182 erase_lots(term, FALSE, TRUE, TRUE);
183 swap_screen(term, 0, FALSE, FALSE);
184 erase_lots(term, FALSE, TRUE, TRUE);
189 * Force a screen update.
191 void term_update(Terminal *term)
194 ctx = get_ctx(term->frontend);
196 int need_sbar_update = term->seen_disp_event;
197 if (term->seen_disp_event && term->cfg->scroll_on_disp) {
198 term->disptop = 0; /* return to main screen */
199 term->seen_disp_event = 0;
200 need_sbar_update = TRUE;
202 if (need_sbar_update)
204 do_paint(term, ctx, TRUE);
205 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
211 * Called from front end when a keypress occurs, to trigger
212 * anything magical that needs to happen in that situation.
214 void term_seen_key_event(Terminal *term)
217 * On any keypress, clear the bell overload mechanism
218 * completely, on the grounds that large numbers of
219 * beeps coming from deliberate key action are likely
220 * to be intended (e.g. beeps from filename completion
221 * blocking repeatedly).
223 term->beep_overloaded = FALSE;
224 while (term->beephead) {
225 struct beeptime *tmp = term->beephead;
226 term->beephead = tmp->next;
229 term->beeptail = NULL;
233 * Reset the scrollback on keypress, if we're doing that.
235 if (term->cfg->scroll_on_key) {
236 term->disptop = 0; /* return to main screen */
237 term->seen_disp_event = 1;
242 * Same as power_on(), but an external function.
244 void term_pwron(Terminal *term)
247 if (term->ldisc) /* cause ldisc to notice changes */
248 ldisc_send(term->ldisc, NULL, 0, 0);
256 * When the user reconfigures us, we need to check the forbidden-
257 * alternate-screen config option, disable raw mouse mode if the
258 * user has disabled mouse reporting, and abandon a print job if
259 * the user has disabled printing.
261 void term_reconfig(Terminal *term)
263 if (term->cfg->no_alt_screen)
264 swap_screen(term, 0, FALSE, FALSE);
265 if (term->cfg->no_mouse_rep) {
266 term->xterm_mouse = 0;
267 set_raw_mouse_mode(term->frontend, 0);
269 if (term->cfg->no_remote_charset) {
270 term->cset_attr[0] = term->cset_attr[1] = ATTR_ASCII;
271 term->sco_acs = term->alt_sco_acs = 0;
274 if (!*term->cfg->printer) {
275 term_print_finish(term);
280 * Clear the scrollback.
282 void term_clrsb(Terminal *term)
286 while ((line = delpos234(term->scrollback, 0)) != NULL) {
293 * Initialise the terminal.
295 Terminal *term_init(Config *mycfg, void *frontend)
300 * Allocate a new Terminal structure and initialise the fields
303 term = smalloc(sizeof(Terminal));
304 term->frontend = frontend;
307 term->compatibility_level = TM_PUTTY;
308 strcpy(term->id_string, "\033[?6c");
309 term->last_blink = term->last_tblink = 0;
310 term->paste_buffer = NULL;
311 term->last_paste = 0;
312 bufchain_init(&term->inbuf);
313 bufchain_init(&term->printer_buf);
314 term->printing = term->only_printing = FALSE;
315 term->print_job = NULL;
316 term->vt52_mode = FALSE;
317 term->cr_lf_return = FALSE;
318 term->seen_disp_event = FALSE;
319 term->xterm_mouse = term->mouse_is_down = FALSE;
320 term->reset_132 = FALSE;
321 term->blinker = term->tblinker = 0;
323 term->repeat_off = FALSE;
324 term->termstate = TOPLEVEL;
325 term->selstate = NO_SELECTION;
328 term->screen = term->alt_screen = term->scrollback = NULL;
330 term->disptext = term->dispcurs = NULL;
333 term->rows = term->cols = -1;
335 term->beephead = term->beeptail = NULL;
337 term->lastbeep = FALSE;
338 term->beep_overloaded = FALSE;
339 term->resize_fn = NULL;
340 term->resize_ctx = NULL;
346 * Set up the terminal for a given size.
348 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
351 unsigned long *newdisp, *line;
354 int save_alt_which = term->alt_which;
356 if (newrows == term->rows && newcols == term->cols &&
357 newsavelines == term->savelines)
358 return; /* nothing to do */
361 swap_screen(term, 0, FALSE, FALSE);
363 term->alt_t = term->marg_t = 0;
364 term->alt_b = term->marg_b = newrows - 1;
366 if (term->rows == -1) {
367 term->scrollback = newtree234(NULL);
368 term->screen = newtree234(NULL);
373 * Resize the screen and scrollback. We only need to shift
374 * lines around within our data structures, because lineptr()
375 * will take care of resizing each individual line if
378 * - If the new screen and the old screen differ in length, we
379 * must shunt some lines in from the scrollback or out to
382 * - If doing that fails to provide us with enough material to
383 * fill the new screen (i.e. the number of rows needed in
384 * the new screen exceeds the total number in the previous
385 * screen+scrollback), we must invent some blank lines to
388 * - Then, if the new scrollback length is less than the
389 * amount of scrollback we actually have, we must throw some
392 sblen = count234(term->scrollback);
393 /* Do this loop to expand the screen if newrows > rows */
394 for (i = term->rows; i < newrows; i++) {
396 line = delpos234(term->scrollback, --sblen);
398 line = smalloc(TSIZE * (newcols + 2));
400 for (j = 0; j <= newcols; j++)
401 line[j + 1] = ERASE_CHAR;
403 addpos234(term->screen, line, 0);
405 /* Do this loop to shrink the screen if newrows < rows */
406 for (i = newrows; i < term->rows; i++) {
407 line = delpos234(term->screen, 0);
408 addpos234(term->scrollback, line, sblen++);
410 assert(count234(term->screen) == newrows);
411 while (sblen > newsavelines) {
412 line = delpos234(term->scrollback, 0);
416 assert(count234(term->scrollback) <= newsavelines);
419 newdisp = smalloc(newrows * (newcols + 1) * TSIZE);
420 for (i = 0; i < newrows * (newcols + 1); i++)
421 newdisp[i] = ATTR_INVALID;
422 sfree(term->disptext);
423 term->disptext = newdisp;
424 term->dispcurs = NULL;
426 newalt = newtree234(NULL);
427 for (i = 0; i < newrows; i++) {
428 line = smalloc(TSIZE * (newcols + 2));
430 for (j = 0; j <= newcols; j++)
431 line[j + 1] = term->erase_char;
432 addpos234(newalt, line, i);
434 if (term->alt_screen) {
435 while (NULL != (line = delpos234(term->alt_screen, 0)))
437 freetree234(term->alt_screen);
439 term->alt_screen = newalt;
441 term->tabs = srealloc(term->tabs, newcols * sizeof(*term->tabs));
444 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
445 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
449 term->curs.y += newrows - term->rows;
450 if (term->curs.y < 0)
452 if (term->curs.y >= newrows)
453 term->curs.y = newrows - 1;
454 if (term->curs.x >= newcols)
455 term->curs.x = newcols - 1;
456 term->alt_x = term->alt_y = 0;
457 term->wrapnext = term->alt_wnext = FALSE;
459 term->rows = newrows;
460 term->cols = newcols;
461 term->savelines = newsavelines;
464 swap_screen(term, save_alt_which, FALSE, FALSE);
469 term->resize_fn(term->resize_ctx, term->cols, term->rows);
473 * Hand a function and context pointer to the terminal which it can
474 * use to notify a back end of resizes.
476 void term_provide_resize_fn(Terminal *term,
477 void (*resize_fn)(void *, int, int),
480 term->resize_fn = resize_fn;
481 term->resize_ctx = resize_ctx;
482 if (term->cols > 0 && term->rows > 0)
483 resize_fn(resize_ctx, term->cols, term->rows);
487 * Swap screens. If `reset' is TRUE and we have been asked to
488 * switch to the alternate screen, we must bring most of its
489 * configuration from the main screen and erase the contents of the
490 * alternate screen completely. (This is even true if we're already
491 * on it! Blame xterm.)
493 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
499 reset = FALSE; /* do no weird resetting if which==0 */
501 if (which != term->alt_which) {
502 term->alt_which = which;
504 ttr = term->alt_screen;
505 term->alt_screen = term->screen;
508 if (!reset && !keep_cur_pos)
509 term->curs.x = term->alt_x;
512 if (!reset && !keep_cur_pos)
513 term->curs.y = term->alt_y;
516 if (!reset) term->marg_t = term->alt_t;
519 if (!reset) term->marg_b = term->alt_b;
522 if (!reset) term->dec_om = term->alt_om;
525 if (!reset) term->wrap = term->alt_wrap;
528 if (!reset) term->wrapnext = term->alt_wnext;
531 if (!reset) term->insert = term->alt_ins;
534 if (!reset) term->cset = term->alt_cset;
537 if (!reset) term->utf = term->alt_utf;
540 if (!reset) term->sco_acs = term->alt_sco_acs;
541 term->alt_sco_acs = t;
544 if (reset && term->screen) {
546 * Yes, this _is_ supposed to honour background-colour-erase.
548 erase_lots(term, FALSE, TRUE, TRUE);
552 * This might not be possible if we're called during
560 * Update the scroll bar.
562 static void update_sbar(Terminal *term)
566 nscroll = count234(term->scrollback);
568 set_sbar(term->frontend, nscroll + term->rows,
569 nscroll + term->disptop, term->rows);
573 * Check whether the region bounded by the two pointers intersects
574 * the scroll region, and de-select the on-screen selection if so.
576 static void check_selection(Terminal *term, pos from, pos to)
578 if (poslt(from, term->selend) && poslt(term->selstart, to))
583 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
584 * for backward.) `sb' is TRUE if the scrolling is permitted to
585 * affect the scrollback buffer.
587 * NB this function invalidates all pointers into lines of the
588 * screen data structures. In particular, you MUST call fix_cpos
589 * after calling scroll() and before doing anything else that
590 * uses the cpos shortcut pointer.
592 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
594 unsigned long *line, *line2;
597 if (topline != 0 || term->alt_which != 0)
602 line = delpos234(term->screen, botline);
603 line = resizeline(line, term->cols);
604 for (i = 0; i < term->cols; i++)
605 line[i + 1] = term->erase_char;
606 line[term->cols + 1] = 0;
607 addpos234(term->screen, line, topline);
609 if (term->selstart.y >= topline && term->selstart.y <= botline) {
611 if (term->selstart.y > botline) {
612 term->selstart.y = botline;
613 term->selstart.x = 0;
616 if (term->selend.y >= topline && term->selend.y <= botline) {
618 if (term->selend.y > botline) {
619 term->selend.y = botline;
628 line = delpos234(term->screen, topline);
629 if (sb && term->savelines > 0) {
630 int sblen = count234(term->scrollback);
632 * We must add this line to the scrollback. We'll
633 * remove a line from the top of the scrollback to
634 * replace it, or allocate a new one if the
635 * scrollback isn't full.
637 if (sblen == term->savelines) {
638 sblen--, line2 = delpos234(term->scrollback, 0);
640 line2 = smalloc(TSIZE * (term->cols + 2));
641 line2[0] = term->cols;
643 addpos234(term->scrollback, line, sblen);
647 * If the user is currently looking at part of the
648 * scrollback, and they haven't enabled any options
649 * that are going to reset the scrollback as a
650 * result of this movement, then the chances are
651 * they'd like to keep looking at the same line. So
652 * we move their viewpoint at the same rate as the
653 * scroll, at least until their viewpoint hits the
654 * top end of the scrollback buffer, at which point
655 * we don't have the choice any more.
657 * Thanks to Jan Holmen Holsten for the idea and
658 * initial implementation.
660 if (term->disptop > -term->savelines && term->disptop < 0)
663 line = resizeline(line, term->cols);
664 for (i = 0; i < term->cols; i++)
665 line[i + 1] = term->erase_char;
666 line[term->cols + 1] = 0;
667 addpos234(term->screen, line, botline);
670 * If the selection endpoints move into the scrollback,
671 * we keep them moving until they hit the top. However,
672 * of course, if the line _hasn't_ moved into the
673 * scrollback then we don't do this, and cut them off
674 * at the top of the scroll region.
676 * This applies to selstart and selend (for an existing
677 * selection), and also selanchor (for one being
678 * selected as we speak).
680 seltop = sb ? -term->savelines : topline;
682 if (term->selstart.y >= seltop &&
683 term->selstart.y <= botline) {
685 if (term->selstart.y < seltop) {
686 term->selstart.y = seltop;
687 term->selstart.x = 0;
690 if (term->selend.y >= seltop && term->selend.y <= botline) {
692 if (term->selend.y < seltop) {
693 term->selend.y = seltop;
697 if (term->selanchor.y >= seltop && term->selanchor.y <= botline) {
699 if (term->selanchor.y < seltop) {
700 term->selanchor.y = seltop;
701 term->selanchor.x = 0;
710 #ifdef OPTIMISE_SCROLL
712 * Scroll the physical display, and our conception of it in disptext.
714 static void scroll_display(Terminal *term, int topline, int botline, int lines)
716 unsigned long *start, *end;
717 int distance, size, i;
719 start = term->disptext + topline * (term->cols + 1);
720 end = term->disptext + (botline + 1) * (term->cols + 1);
721 distance = (lines > 0 ? lines : -lines) * (term->cols + 1);
722 size = end - start - distance;
724 memmove(start, start + distance, size * TSIZE);
725 for (i = 0; i < distance; i++)
726 (start + size)[i] |= ATTR_INVALID;
728 memmove(start + distance, start, size * TSIZE);
729 for (i = 0; i < distance; i++)
730 start[i] |= ATTR_INVALID;
732 do_scroll(term->frontend, topline, botline, lines);
734 #endif /* OPTIMISE_SCROLL */
737 * Move the cursor to a given position, clipping at boundaries. We
738 * may or may not want to clip at the scroll margin: marg_clip is 0
739 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
740 * even _being_ outside the margins.
742 static void move(Terminal *term, int x, int y, int marg_clip)
749 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
752 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
763 term->wrapnext = FALSE;
767 * Save or restore the cursor and SGR mode.
769 static void save_cursor(Terminal *term, int save)
772 term->savecurs = term->curs;
773 term->save_attr = term->curr_attr;
774 term->save_cset = term->cset;
775 term->save_utf = term->utf;
776 term->save_wnext = term->wrapnext;
777 term->save_csattr = term->cset_attr[term->cset];
778 term->save_sco_acs = term->sco_acs;
780 term->curs = term->savecurs;
781 /* Make sure the window hasn't shrunk since the save */
782 if (term->curs.x >= term->cols)
783 term->curs.x = term->cols - 1;
784 if (term->curs.y >= term->rows)
785 term->curs.y = term->rows - 1;
787 term->curr_attr = term->save_attr;
788 term->cset = term->save_cset;
789 term->utf = term->save_utf;
790 term->wrapnext = term->save_wnext;
792 * wrapnext might reset to False if the x position is no
793 * longer at the rightmost edge.
795 if (term->wrapnext && term->curs.x < term->cols-1)
796 term->wrapnext = FALSE;
797 term->cset_attr[term->cset] = term->save_csattr;
798 term->sco_acs = term->save_sco_acs;
801 term->erase_char = (' ' | ATTR_ASCII |
803 (ATTR_FGMASK | ATTR_BGMASK)));
808 * Erase a large portion of the screen: the whole screen, or the
809 * whole line, or parts thereof.
811 static void erase_lots(Terminal *term,
812 int line_only, int from_begin, int to_end)
816 unsigned long *ldata;
819 start.y = term->curs.y;
821 end.y = term->curs.y + 1;
838 check_selection(term, start, end);
840 /* Clear screen also forces a full window redraw, just in case. */
841 if (start.y == 0 && start.x == 0 && end.y == term->rows)
842 term_invalidate(term);
844 ldata = lineptr(start.y);
845 while (poslt(start, end)) {
846 if (start.x == term->cols && !erase_lattr)
847 ldata[start.x] &= ~LATTR_WRAPPED;
849 ldata[start.x] = term->erase_char;
850 if (incpos(start) && start.y < term->rows)
851 ldata = lineptr(start.y);
856 * Insert or delete characters within the current line. n is +ve if
857 * insertion is desired, and -ve for deletion.
859 static void insch(Terminal *term, int n)
861 int dir = (n < 0 ? -1 : +1);
864 unsigned long *ldata;
866 n = (n < 0 ? -n : n);
867 if (n > term->cols - term->curs.x)
868 n = term->cols - term->curs.x;
869 m = term->cols - term->curs.x - n;
870 cursplus.y = term->curs.y;
871 cursplus.x = term->curs.x + n;
872 check_selection(term, term->curs, cursplus);
873 ldata = lineptr(term->curs.y);
875 memmove(ldata + term->curs.x, ldata + term->curs.x + n, m * TSIZE);
877 ldata[term->curs.x + m++] = term->erase_char;
879 memmove(ldata + term->curs.x + n, ldata + term->curs.x, m * TSIZE);
881 ldata[term->curs.x + n] = term->erase_char;
886 * Toggle terminal mode `mode' to state `state'. (`query' indicates
887 * whether the mode is a DEC private one or a normal one.)
889 static void toggle_mode(Terminal *term, int mode, int query, int state)
895 case 1: /* application cursor keys */
896 term->app_cursor_keys = state;
898 case 2: /* VT52 mode */
899 term->vt52_mode = !state;
900 if (term->vt52_mode) {
901 term->blink_is_real = FALSE;
902 term->vt52_bold = FALSE;
904 term->blink_is_real = term->cfg->blinktext;
907 case 3: /* 80/132 columns */
909 if (!term->cfg->no_remote_resize)
910 request_resize(term->frontend, state ? 132 : 80, term->rows);
911 term->reset_132 = state;
913 case 5: /* reverse video */
915 * Toggle reverse video. If we receive an OFF within the
916 * visual bell timeout period after an ON, we trigger an
917 * effective visual bell, so that ESC[?5hESC[?5l will
918 * always be an actually _visible_ visual bell.
920 ticks = GETTICKCOUNT();
921 /* turn off a previous vbell to avoid inconsistencies */
922 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
923 term->in_vbell = FALSE;
924 if (term->rvideo && !state && /* we're turning it off... */
925 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
926 /* If there's no vbell timeout already, or this one lasts
927 * longer, replace vbell_timeout with ours. */
928 if (!term->in_vbell ||
929 (term->rvbell_startpoint - term->vbell_startpoint <
931 term->vbell_startpoint = term->rvbell_startpoint;
932 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
933 } else if (!term->rvideo && state) {
934 /* This is an ON, so we notice the time and save it. */
935 term->rvbell_startpoint = ticks;
937 term->rvideo = state;
938 term->seen_disp_event = TRUE;
942 case 6: /* DEC origin mode */
943 term->dec_om = state;
945 case 7: /* auto wrap */
948 case 8: /* auto key repeat */
949 term->repeat_off = !state;
951 case 10: /* set local edit mode */
952 term->term_editing = state;
953 if (term->ldisc) /* cause ldisc to notice changes */
954 ldisc_send(term->ldisc, NULL, 0, 0);
956 case 25: /* enable/disable cursor */
957 compatibility2(OTHER, VT220);
958 term->cursor_on = state;
959 term->seen_disp_event = TRUE;
961 case 47: /* alternate screen */
962 compatibility(OTHER);
964 swap_screen(term, term->cfg->no_alt_screen ? 0 : state, FALSE, FALSE);
967 case 1000: /* xterm mouse 1 */
968 term->xterm_mouse = state ? 1 : 0;
969 set_raw_mouse_mode(term->frontend, state);
971 case 1002: /* xterm mouse 2 */
972 term->xterm_mouse = state ? 2 : 0;
973 set_raw_mouse_mode(term->frontend, state);
975 case 1047: /* alternate screen */
976 compatibility(OTHER);
978 swap_screen(term, term->cfg->no_alt_screen ? 0 : state, TRUE, TRUE);
981 case 1048: /* save/restore cursor */
982 save_cursor(term, state);
983 if (!state) term->seen_disp_event = TRUE;
985 case 1049: /* cursor & alternate screen */
987 save_cursor(term, state);
988 if (!state) term->seen_disp_event = TRUE;
989 compatibility(OTHER);
991 swap_screen(term, term->cfg->no_alt_screen ? 0 : state, TRUE, FALSE);
993 save_cursor(term, state);
998 case 4: /* set insert mode */
999 compatibility(VT102);
1000 term->insert = state;
1002 case 12: /* set echo mode */
1003 term->term_echoing = !state;
1004 if (term->ldisc) /* cause ldisc to notice changes */
1005 ldisc_send(term->ldisc, NULL, 0, 0);
1007 case 20: /* Return sends ... */
1008 term->cr_lf_return = state;
1010 case 34: /* Make cursor BIG */
1011 compatibility2(OTHER, VT220);
1012 term->big_cursor = !state;
1017 * Process an OSC sequence: set window title or icon name.
1019 static void do_osc(Terminal *term)
1022 while (term->osc_strlen--)
1023 term->wordness[(unsigned char)
1024 term->osc_string[term->osc_strlen]] = term->esc_args[0];
1026 term->osc_string[term->osc_strlen] = '\0';
1027 switch (term->esc_args[0]) {
1030 if (!term->cfg->no_remote_wintitle)
1031 set_icon(term->frontend, term->osc_string);
1032 if (term->esc_args[0] == 1)
1034 /* fall through: parameter 0 means set both */
1037 if (!term->cfg->no_remote_wintitle)
1038 set_title(term->frontend, term->osc_string);
1045 * ANSI printing routines.
1047 static void term_print_setup(Terminal *term)
1049 bufchain_clear(&term->printer_buf);
1050 term->print_job = printer_start_job(term->cfg->printer);
1052 static void term_print_flush(Terminal *term)
1057 while ((size = bufchain_size(&term->printer_buf)) > 5) {
1058 bufchain_prefix(&term->printer_buf, &data, &len);
1061 printer_job_data(term->print_job, data, len);
1062 bufchain_consume(&term->printer_buf, len);
1065 static void term_print_finish(Terminal *term)
1071 if (!term->printing && !term->only_printing)
1072 return; /* we need do nothing */
1074 term_print_flush(term);
1075 while ((size = bufchain_size(&term->printer_buf)) > 0) {
1076 bufchain_prefix(&term->printer_buf, &data, &len);
1078 if (c == '\033' || c == '\233') {
1079 bufchain_consume(&term->printer_buf, size);
1082 printer_job_data(term->print_job, &c, 1);
1083 bufchain_consume(&term->printer_buf, 1);
1086 printer_finish_job(term->print_job);
1087 term->print_job = NULL;
1088 term->printing = term->only_printing = FALSE;
1092 * Remove everything currently in `inbuf' and stick it up on the
1093 * in-memory display. There's a big state machine in here to
1094 * process escape sequences...
1096 void term_out(Terminal *term)
1099 unsigned char localbuf[256], *chars;
1104 chars = NULL; /* placate compiler warnings */
1105 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
1109 bufchain_prefix(&term->inbuf, &ret, &nchars);
1110 if (nchars > sizeof(localbuf))
1111 nchars = sizeof(localbuf);
1112 memcpy(localbuf, ret, nchars);
1113 bufchain_consume(&term->inbuf, nchars);
1115 assert(chars != NULL);
1121 * Optionally log the session traffic to a file. Useful for
1122 * debugging and possibly also useful for actual logging.
1124 if (term->cfg->logtype == LGTYP_DEBUG && term->logctx)
1125 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
1131 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1132 * be able to display 8-bit characters, but I'll let that go 'cause
1137 * If we're printing, add the character to the printer
1140 if (term->printing) {
1141 bufchain_add(&term->printer_buf, &c, 1);
1144 * If we're in print-only mode, we use a much simpler
1145 * state machine designed only to recognise the ESC[4i
1146 * termination sequence.
1148 if (term->only_printing) {
1150 term->print_state = 1;
1151 else if (c == (unsigned char)'\233')
1152 term->print_state = 2;
1153 else if (c == '[' && term->print_state == 1)
1154 term->print_state = 2;
1155 else if (c == '4' && term->print_state == 2)
1156 term->print_state = 3;
1157 else if (c == 'i' && term->print_state == 3)
1158 term->print_state = 4;
1160 term->print_state = 0;
1161 if (term->print_state == 4) {
1162 term_print_finish(term);
1168 /* First see about all those translations. */
1169 if (term->termstate == TOPLEVEL) {
1171 switch (term->utf_state) {
1174 /* UTF-8 must be stateless so we ignore iso2022. */
1175 if (unitab_ctrl[c] != 0xFF)
1177 else c = ((unsigned char)c) | ATTR_ASCII;
1179 } else if ((c & 0xe0) == 0xc0) {
1180 term->utf_size = term->utf_state = 1;
1181 term->utf_char = (c & 0x1f);
1182 } else if ((c & 0xf0) == 0xe0) {
1183 term->utf_size = term->utf_state = 2;
1184 term->utf_char = (c & 0x0f);
1185 } else if ((c & 0xf8) == 0xf0) {
1186 term->utf_size = term->utf_state = 3;
1187 term->utf_char = (c & 0x07);
1188 } else if ((c & 0xfc) == 0xf8) {
1189 term->utf_size = term->utf_state = 4;
1190 term->utf_char = (c & 0x03);
1191 } else if ((c & 0xfe) == 0xfc) {
1192 term->utf_size = term->utf_state = 5;
1193 term->utf_char = (c & 0x01);
1204 if ((c & 0xC0) != 0x80) {
1207 term->utf_state = 0;
1210 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
1211 if (--term->utf_state)
1216 /* Is somebody trying to be evil! */
1218 (c < 0x800 && term->utf_size >= 2) ||
1219 (c < 0x10000 && term->utf_size >= 3) ||
1220 (c < 0x200000 && term->utf_size >= 4) ||
1221 (c < 0x4000000 && term->utf_size >= 5))
1224 /* Unicode line separator and paragraph separator are CR-LF */
1225 if (c == 0x2028 || c == 0x2029)
1228 /* High controls are probably a Baaad idea too. */
1232 /* The UTF-16 surrogates are not nice either. */
1233 /* The standard give the option of decoding these:
1234 * I don't want to! */
1235 if (c >= 0xD800 && c < 0xE000)
1238 /* ISO 10646 characters now limited to UTF-16 range. */
1242 /* This is currently a TagPhobic application.. */
1243 if (c >= 0xE0000 && c <= 0xE007F)
1246 /* U+FEFF is best seen as a null. */
1249 /* But U+FFFE is an error. */
1250 if (c == 0xFFFE || c == 0xFFFF)
1253 /* Oops this is a 16bit implementation */
1258 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1259 else if(term->sco_acs &&
1260 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
1262 if (term->sco_acs == 2) c ^= 0x80;
1265 switch (term->cset_attr[term->cset]) {
1267 * Linedraw characters are different from 'ESC ( B'
1268 * only for a small range. For ones outside that
1269 * range, make sure we use the same font as well as
1270 * the same encoding.
1273 if (unitab_ctrl[c] != 0xFF)
1276 c = ((unsigned char) c) | ATTR_LINEDRW;
1280 /* If UK-ASCII, make the '#' a LineDraw Pound */
1282 c = '}' | ATTR_LINEDRW;
1285 /*FALLTHROUGH*/ case ATTR_ASCII:
1286 if (unitab_ctrl[c] != 0xFF)
1289 c = ((unsigned char) c) | ATTR_ASCII;
1292 if (c>=' ') c = ((unsigned char)c) | ATTR_SCOACS;
1298 /* How about C1 controls ? */
1299 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
1300 !term->vt52_mode && has_compat(VT220)) {
1301 term->termstate = SEEN_ESC;
1302 term->esc_query = FALSE;
1303 c = '@' + (c & 0x1F);
1306 /* Or the GL control. */
1307 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
1308 if (term->curs.x && !term->wrapnext)
1310 term->wrapnext = FALSE;
1312 if (!term->cfg->no_dbackspace) /* destructive bksp might be disabled */
1313 *term->cpos = (' ' | term->curr_attr | ATTR_ASCII);
1315 /* Or normal C0 controls. */
1316 if ((c & -32) == 0 && term->termstate < DO_CTRLS) {
1318 case '\005': /* terminal type query */
1319 /* Strictly speaking this is VT100 but a VT100 defaults to
1320 * no response. Other terminals respond at their option.
1322 * Don't put a CR in the default string as this tends to
1323 * upset some weird software.
1325 * An xterm returns "xterm" (5 characters)
1327 compatibility(ANSIMIN);
1329 char abuf[256], *s, *d;
1331 for (s = term->cfg->answerback, d = abuf; *s; s++) {
1333 if (*s >= 'a' && *s <= 'z')
1334 *d++ = (*s - ('a' - 1));
1335 else if ((*s >= '@' && *s <= '_') ||
1336 *s == '?' || (*s & 0x80))
1341 } else if (*s == '^') {
1346 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
1352 struct beeptime *newbeep;
1353 unsigned long ticks;
1355 ticks = GETTICKCOUNT();
1357 if (!term->beep_overloaded) {
1358 newbeep = smalloc(sizeof(struct beeptime));
1359 newbeep->ticks = ticks;
1360 newbeep->next = NULL;
1361 if (!term->beephead)
1362 term->beephead = newbeep;
1364 term->beeptail->next = newbeep;
1365 term->beeptail = newbeep;
1370 * Throw out any beeps that happened more than
1373 while (term->beephead &&
1374 term->beephead->ticks < ticks - term->cfg->bellovl_t) {
1375 struct beeptime *tmp = term->beephead;
1376 term->beephead = tmp->next;
1378 if (!term->beephead)
1379 term->beeptail = NULL;
1383 if (term->cfg->bellovl && term->beep_overloaded &&
1384 ticks - term->lastbeep >= (unsigned)term->cfg->bellovl_s) {
1386 * If we're currently overloaded and the
1387 * last beep was more than s seconds ago,
1388 * leave overload mode.
1390 term->beep_overloaded = FALSE;
1391 } else if (term->cfg->bellovl && !term->beep_overloaded &&
1392 term->nbeeps >= term->cfg->bellovl_n) {
1394 * Now, if we have n or more beeps
1395 * remaining in the queue, go into overload
1398 term->beep_overloaded = TRUE;
1400 term->lastbeep = ticks;
1403 * Perform an actual beep if we're not overloaded.
1405 if (!term->cfg->bellovl || !term->beep_overloaded) {
1406 beep(term->frontend, term->cfg->beep);
1407 if (term->cfg->beep == BELL_VISUAL) {
1408 term->in_vbell = TRUE;
1409 term->vbell_startpoint = ticks;
1417 if (term->curs.x == 0 &&
1418 (term->curs.y == 0 || term->wrap == 0))
1420 else if (term->curs.x == 0 && term->curs.y > 0)
1421 term->curs.x = term->cols - 1, term->curs.y--;
1422 else if (term->wrapnext)
1423 term->wrapnext = FALSE;
1427 term->seen_disp_event = TRUE;
1430 compatibility(VT100);
1434 compatibility(VT100);
1438 if (term->vt52_mode)
1439 term->termstate = VT52_ESC;
1441 compatibility(ANSIMIN);
1442 term->termstate = SEEN_ESC;
1443 term->esc_query = FALSE;
1448 term->wrapnext = FALSE;
1450 term->seen_disp_event = TRUE;
1451 term->paste_hold = 0;
1453 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1456 if (has_compat(SCOANSI)) {
1457 move(term, 0, 0, 0);
1458 erase_lots(term, FALSE, FALSE, TRUE);
1460 term->wrapnext = FALSE;
1461 term->seen_disp_event = 1;
1465 compatibility(VT100);
1467 if (term->curs.y == term->marg_b)
1468 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1469 else if (term->curs.y < term->rows - 1)
1471 if (term->cfg->lfhascr)
1474 term->wrapnext = FALSE;
1475 term->seen_disp_event = 1;
1476 term->paste_hold = 0;
1478 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1482 pos old_curs = term->curs;
1483 unsigned long *ldata = lineptr(term->curs.y);
1487 } while (term->curs.x < term->cols - 1 &&
1488 !term->tabs[term->curs.x]);
1490 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM) {
1491 if (term->curs.x >= term->cols / 2)
1492 term->curs.x = term->cols / 2 - 1;
1494 if (term->curs.x >= term->cols)
1495 term->curs.x = term->cols - 1;
1499 check_selection(term, old_curs, term->curs);
1501 term->seen_disp_event = TRUE;
1505 switch (term->termstate) {
1507 /* Only graphic characters get this far;
1508 * ctrls are stripped above */
1509 if (term->wrapnext && term->wrap) {
1510 term->cpos[1] |= LATTR_WRAPPED;
1511 if (term->curs.y == term->marg_b)
1512 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1513 else if (term->curs.y < term->rows - 1)
1517 term->wrapnext = FALSE;
1521 if (term->selstate != NO_SELECTION) {
1522 pos cursplus = term->curs;
1524 check_selection(term, term->curs, cursplus);
1526 if (((c & CSET_MASK) == ATTR_ASCII || (c & CSET_MASK) == 0) &&
1528 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1530 extern int wcwidth(wchar_t ucs);
1535 width = wcwidth((wchar_t) c);
1538 *term->cpos++ = c | term->curr_attr;
1539 if (++term->curs.x == term->cols) {
1540 *term->cpos |= LATTR_WRAPPED;
1541 if (term->curs.y == term->marg_b)
1542 scroll(term, term->marg_t, term->marg_b,
1544 else if (term->curs.y < term->rows - 1)
1549 *term->cpos++ = UCSWIDE | term->curr_attr;
1552 *term->cpos++ = c | term->curr_attr;
1559 if (term->curs.x == term->cols) {
1562 term->wrapnext = TRUE;
1563 if (term->wrap && term->vt52_mode) {
1564 term->cpos[1] |= LATTR_WRAPPED;
1565 if (term->curs.y == term->marg_b)
1566 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1567 else if (term->curs.y < term->rows - 1)
1571 term->wrapnext = FALSE;
1574 term->seen_disp_event = 1;
1579 * This state is virtually identical to SEEN_ESC, with the
1580 * exception that we have an OSC sequence in the pipeline,
1581 * and _if_ we see a backslash, we process it.
1585 term->termstate = TOPLEVEL;
1588 /* else fall through */
1590 if (c >= ' ' && c <= '/') {
1591 if (term->esc_query)
1592 term->esc_query = -1;
1594 term->esc_query = c;
1597 term->termstate = TOPLEVEL;
1598 switch (ANSI(c, term->esc_query)) {
1599 case '[': /* enter CSI mode */
1600 term->termstate = SEEN_CSI;
1601 term->esc_nargs = 1;
1602 term->esc_args[0] = ARG_DEFAULT;
1603 term->esc_query = FALSE;
1605 case ']': /* xterm escape sequences */
1606 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1607 compatibility(OTHER);
1608 term->termstate = SEEN_OSC;
1609 term->esc_args[0] = 0;
1611 case '7': /* save cursor */
1612 compatibility(VT100);
1613 save_cursor(term, TRUE);
1615 case '8': /* restore cursor */
1616 compatibility(VT100);
1617 save_cursor(term, FALSE);
1618 term->seen_disp_event = TRUE;
1621 compatibility(VT100);
1622 term->app_keypad_keys = TRUE;
1625 compatibility(VT100);
1626 term->app_keypad_keys = FALSE;
1628 case 'D': /* exactly equivalent to LF */
1629 compatibility(VT100);
1630 if (term->curs.y == term->marg_b)
1631 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1632 else if (term->curs.y < term->rows - 1)
1635 term->wrapnext = FALSE;
1636 term->seen_disp_event = TRUE;
1638 case 'E': /* exactly equivalent to CR-LF */
1639 compatibility(VT100);
1641 if (term->curs.y == term->marg_b)
1642 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1643 else if (term->curs.y < term->rows - 1)
1646 term->wrapnext = FALSE;
1647 term->seen_disp_event = TRUE;
1649 case 'M': /* reverse index - backwards LF */
1650 compatibility(VT100);
1651 if (term->curs.y == term->marg_t)
1652 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
1653 else if (term->curs.y > 0)
1656 term->wrapnext = FALSE;
1657 term->seen_disp_event = TRUE;
1659 case 'Z': /* terminal type query */
1660 compatibility(VT100);
1662 ldisc_send(term->ldisc, term->id_string,
1663 strlen(term->id_string), 0);
1665 case 'c': /* restore power-on settings */
1666 compatibility(VT100);
1668 if (term->ldisc) /* cause ldisc to notice changes */
1669 ldisc_send(term->ldisc, NULL, 0, 0);
1670 if (term->reset_132) {
1671 if (!term->cfg->no_remote_resize)
1672 request_resize(term->frontend, 80, term->rows);
1673 term->reset_132 = 0;
1677 term->seen_disp_event = TRUE;
1679 case 'H': /* set a tab */
1680 compatibility(VT100);
1681 term->tabs[term->curs.x] = TRUE;
1684 case ANSI('8', '#'): /* ESC # 8 fills screen with Es :-) */
1685 compatibility(VT100);
1687 unsigned long *ldata;
1691 for (i = 0; i < term->rows; i++) {
1693 for (j = 0; j < term->cols; j++)
1694 ldata[j] = ATTR_DEFAULT | 'E';
1695 ldata[term->cols] = 0;
1698 term->seen_disp_event = TRUE;
1699 scrtop.x = scrtop.y = 0;
1701 scrbot.y = term->rows;
1702 check_selection(term, scrtop, scrbot);
1706 case ANSI('3', '#'):
1707 case ANSI('4', '#'):
1708 case ANSI('5', '#'):
1709 case ANSI('6', '#'):
1710 compatibility(VT100);
1712 unsigned long nlattr;
1713 unsigned long *ldata;
1714 switch (ANSI(c, term->esc_query)) {
1715 case ANSI('3', '#'):
1718 case ANSI('4', '#'):
1721 case ANSI('5', '#'):
1722 nlattr = LATTR_NORM;
1724 default: /* spiritually case ANSI('6', '#'): */
1725 nlattr = LATTR_WIDE;
1728 ldata = lineptr(term->curs.y);
1729 ldata[term->cols] &= ~LATTR_MODE;
1730 ldata[term->cols] |= nlattr;
1734 case ANSI('A', '('):
1735 compatibility(VT100);
1736 if (!term->cfg->no_remote_charset)
1737 term->cset_attr[0] = ATTR_GBCHR;
1739 case ANSI('B', '('):
1740 compatibility(VT100);
1741 if (!term->cfg->no_remote_charset)
1742 term->cset_attr[0] = ATTR_ASCII;
1744 case ANSI('0', '('):
1745 compatibility(VT100);
1746 if (!term->cfg->no_remote_charset)
1747 term->cset_attr[0] = ATTR_LINEDRW;
1749 case ANSI('U', '('):
1750 compatibility(OTHER);
1751 if (!term->cfg->no_remote_charset)
1752 term->cset_attr[0] = ATTR_SCOACS;
1755 case ANSI('A', ')'):
1756 compatibility(VT100);
1757 if (!term->cfg->no_remote_charset)
1758 term->cset_attr[1] = ATTR_GBCHR;
1760 case ANSI('B', ')'):
1761 compatibility(VT100);
1762 if (!term->cfg->no_remote_charset)
1763 term->cset_attr[1] = ATTR_ASCII;
1765 case ANSI('0', ')'):
1766 compatibility(VT100);
1767 if (!term->cfg->no_remote_charset)
1768 term->cset_attr[1] = ATTR_LINEDRW;
1770 case ANSI('U', ')'):
1771 compatibility(OTHER);
1772 if (!term->cfg->no_remote_charset)
1773 term->cset_attr[1] = ATTR_SCOACS;
1776 case ANSI('8', '%'): /* Old Linux code */
1777 case ANSI('G', '%'):
1778 compatibility(OTHER);
1779 if (!term->cfg->no_remote_charset)
1782 case ANSI('@', '%'):
1783 compatibility(OTHER);
1784 if (!term->cfg->no_remote_charset)
1790 term->termstate = TOPLEVEL; /* default */
1792 if (term->esc_nargs <= ARGS_MAX) {
1793 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
1794 term->esc_args[term->esc_nargs - 1] = 0;
1795 term->esc_args[term->esc_nargs - 1] =
1796 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
1798 term->termstate = SEEN_CSI;
1799 } else if (c == ';') {
1800 if (++term->esc_nargs <= ARGS_MAX)
1801 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
1802 term->termstate = SEEN_CSI;
1803 } else if (c < '@') {
1804 if (term->esc_query)
1805 term->esc_query = -1;
1807 term->esc_query = TRUE;
1809 term->esc_query = c;
1810 term->termstate = SEEN_CSI;
1812 switch (ANSI(c, term->esc_query)) {
1813 case 'A': /* move up N lines */
1814 move(term, term->curs.x,
1815 term->curs.y - def(term->esc_args[0], 1), 1);
1816 term->seen_disp_event = TRUE;
1818 case 'e': /* move down N lines */
1819 compatibility(ANSI);
1822 move(term, term->curs.x,
1823 term->curs.y + def(term->esc_args[0], 1), 1);
1824 term->seen_disp_event = TRUE;
1826 case ANSI('c', '>'): /* report xterm version */
1827 compatibility(OTHER);
1828 /* this reports xterm version 136 so that VIM can
1829 use the drag messages from the mouse reporting */
1831 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
1833 case 'a': /* move right N cols */
1834 compatibility(ANSI);
1837 move(term, term->curs.x + def(term->esc_args[0], 1),
1839 term->seen_disp_event = TRUE;
1841 case 'D': /* move left N cols */
1842 move(term, term->curs.x - def(term->esc_args[0], 1),
1844 term->seen_disp_event = TRUE;
1846 case 'E': /* move down N lines and CR */
1847 compatibility(ANSI);
1849 term->curs.y + def(term->esc_args[0], 1), 1);
1850 term->seen_disp_event = TRUE;
1852 case 'F': /* move up N lines and CR */
1853 compatibility(ANSI);
1855 term->curs.y - def(term->esc_args[0], 1), 1);
1856 term->seen_disp_event = TRUE;
1859 case '`': /* set horizontal posn */
1860 compatibility(ANSI);
1861 move(term, def(term->esc_args[0], 1) - 1,
1863 term->seen_disp_event = TRUE;
1865 case 'd': /* set vertical posn */
1866 compatibility(ANSI);
1867 move(term, term->curs.x,
1868 ((term->dec_om ? term->marg_t : 0) +
1869 def(term->esc_args[0], 1) - 1),
1870 (term->dec_om ? 2 : 0));
1871 term->seen_disp_event = TRUE;
1874 case 'f': /* set horz and vert posns at once */
1875 if (term->esc_nargs < 2)
1876 term->esc_args[1] = ARG_DEFAULT;
1877 move(term, def(term->esc_args[1], 1) - 1,
1878 ((term->dec_om ? term->marg_t : 0) +
1879 def(term->esc_args[0], 1) - 1),
1880 (term->dec_om ? 2 : 0));
1881 term->seen_disp_event = TRUE;
1883 case 'J': /* erase screen or parts of it */
1885 unsigned int i = def(term->esc_args[0], 0) + 1;
1888 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
1891 term->seen_disp_event = TRUE;
1893 case 'K': /* erase line or parts of it */
1895 unsigned int i = def(term->esc_args[0], 0) + 1;
1898 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
1900 term->seen_disp_event = TRUE;
1902 case 'L': /* insert lines */
1903 compatibility(VT102);
1904 if (term->curs.y <= term->marg_b)
1905 scroll(term, term->curs.y, term->marg_b,
1906 -def(term->esc_args[0], 1), FALSE);
1908 term->seen_disp_event = TRUE;
1910 case 'M': /* delete lines */
1911 compatibility(VT102);
1912 if (term->curs.y <= term->marg_b)
1913 scroll(term, term->curs.y, term->marg_b,
1914 def(term->esc_args[0], 1),
1917 term->seen_disp_event = TRUE;
1919 case '@': /* insert chars */
1920 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1921 compatibility(VT102);
1922 insch(term, def(term->esc_args[0], 1));
1923 term->seen_disp_event = TRUE;
1925 case 'P': /* delete chars */
1926 compatibility(VT102);
1927 insch(term, -def(term->esc_args[0], 1));
1928 term->seen_disp_event = TRUE;
1930 case 'c': /* terminal type query */
1931 compatibility(VT100);
1932 /* This is the response for a VT102 */
1934 ldisc_send(term->ldisc, term->id_string,
1935 strlen(term->id_string), 0);
1937 case 'n': /* cursor position query */
1939 if (term->esc_args[0] == 6) {
1941 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
1943 ldisc_send(term->ldisc, buf, strlen(buf), 0);
1944 } else if (term->esc_args[0] == 5) {
1945 ldisc_send(term->ldisc, "\033[0n", 4, 0);
1949 case 'h': /* toggle modes to high */
1951 compatibility(VT100);
1954 for (i = 0; i < term->esc_nargs; i++)
1955 toggle_mode(term, term->esc_args[i],
1956 term->esc_query, TRUE);
1961 compatibility(VT100);
1963 if (term->esc_nargs != 1) break;
1964 if (term->esc_args[0] == 5 && *term->cfg->printer) {
1965 term->printing = TRUE;
1966 term->only_printing = !term->esc_query;
1967 term->print_state = 0;
1968 term_print_setup(term);
1969 } else if (term->esc_args[0] == 4 &&
1971 term_print_finish(term);
1975 case 'l': /* toggle modes to low */
1977 compatibility(VT100);
1980 for (i = 0; i < term->esc_nargs; i++)
1981 toggle_mode(term, term->esc_args[i],
1982 term->esc_query, FALSE);
1985 case 'g': /* clear tabs */
1986 compatibility(VT100);
1987 if (term->esc_nargs == 1) {
1988 if (term->esc_args[0] == 0) {
1989 term->tabs[term->curs.x] = FALSE;
1990 } else if (term->esc_args[0] == 3) {
1992 for (i = 0; i < term->cols; i++)
1993 term->tabs[i] = FALSE;
1997 case 'r': /* set scroll margins */
1998 compatibility(VT100);
1999 if (term->esc_nargs <= 2) {
2001 top = def(term->esc_args[0], 1) - 1;
2002 bot = (term->esc_nargs <= 1
2003 || term->esc_args[1] == 0 ?
2005 def(term->esc_args[1], term->rows)) - 1;
2006 if (bot >= term->rows)
2007 bot = term->rows - 1;
2008 /* VTTEST Bug 9 - if region is less than 2 lines
2009 * don't change region.
2011 if (bot - top > 0) {
2016 * I used to think the cursor should be
2017 * placed at the top of the newly marginned
2018 * area. Apparently not: VMS TPU falls over
2021 * Well actually it should for
2024 term->curs.y = (term->dec_om ?
2027 term->seen_disp_event = TRUE;
2031 case 'm': /* set graphics rendition */
2034 * A VT100 without the AVO only had one
2035 * attribute, either underline or
2036 * reverse video depending on the
2037 * cursor type, this was selected by
2041 * This is sometimes DIM, eg on the
2044 * This is sometimes INVIS various ANSI.
2046 * This like 22 disables BOLD, DIM and INVIS
2048 * The ANSI colours appear on any
2049 * terminal that has colour (obviously)
2050 * but the interaction between sgr0 and
2051 * the colours varies but is usually
2052 * related to the background colour
2053 * erase item. The interaction between
2054 * colour attributes and the mono ones
2055 * is also very implementation
2058 * The 39 and 49 attributes are likely
2059 * to be unimplemented.
2062 for (i = 0; i < term->esc_nargs; i++) {
2063 switch (def(term->esc_args[i], 0)) {
2064 case 0: /* restore defaults */
2065 term->curr_attr = ATTR_DEFAULT;
2067 case 1: /* enable bold */
2068 compatibility(VT100AVO);
2069 term->curr_attr |= ATTR_BOLD;
2071 case 21: /* (enable double underline) */
2072 compatibility(OTHER);
2073 case 4: /* enable underline */
2074 compatibility(VT100AVO);
2075 term->curr_attr |= ATTR_UNDER;
2077 case 5: /* enable blink */
2078 compatibility(VT100AVO);
2079 term->curr_attr |= ATTR_BLINK;
2081 case 7: /* enable reverse video */
2082 term->curr_attr |= ATTR_REVERSE;
2084 case 10: /* SCO acs off */
2085 compatibility(SCOANSI);
2086 if (term->cfg->no_remote_charset) break;
2087 term->sco_acs = 0; break;
2088 case 11: /* SCO acs on */
2089 compatibility(SCOANSI);
2090 if (term->cfg->no_remote_charset) break;
2091 term->sco_acs = 1; break;
2092 case 12: /* SCO acs on flipped */
2093 compatibility(SCOANSI);
2094 if (term->cfg->no_remote_charset) break;
2095 term->sco_acs = 2; break;
2096 case 22: /* disable bold */
2097 compatibility2(OTHER, VT220);
2098 term->curr_attr &= ~ATTR_BOLD;
2100 case 24: /* disable underline */
2101 compatibility2(OTHER, VT220);
2102 term->curr_attr &= ~ATTR_UNDER;
2104 case 25: /* disable blink */
2105 compatibility2(OTHER, VT220);
2106 term->curr_attr &= ~ATTR_BLINK;
2108 case 27: /* disable reverse video */
2109 compatibility2(OTHER, VT220);
2110 term->curr_attr &= ~ATTR_REVERSE;
2121 term->curr_attr &= ~ATTR_FGMASK;
2123 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
2125 case 39: /* default-foreground */
2126 term->curr_attr &= ~ATTR_FGMASK;
2127 term->curr_attr |= ATTR_DEFFG;
2138 term->curr_attr &= ~ATTR_BGMASK;
2140 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
2142 case 49: /* default-background */
2143 term->curr_attr &= ~ATTR_BGMASK;
2144 term->curr_attr |= ATTR_DEFBG;
2149 term->erase_char = (' ' | ATTR_ASCII |
2155 case 's': /* save cursor */
2156 save_cursor(term, TRUE);
2158 case 'u': /* restore cursor */
2159 save_cursor(term, FALSE);
2160 term->seen_disp_event = TRUE;
2162 case 't': /* set page size - ie window height */
2164 * VT340/VT420 sequence DECSLPP, DEC only allows values
2165 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2166 * illegal values (eg first arg 1..9) for window changing
2169 if (term->esc_nargs <= 1
2170 && (term->esc_args[0] < 1 ||
2171 term->esc_args[0] >= 24)) {
2172 compatibility(VT340TEXT);
2173 if (!term->cfg->no_remote_resize)
2174 request_resize(term->frontend, term->cols,
2175 def(term->esc_args[0], 24));
2177 } else if (term->esc_nargs >= 1 &&
2178 term->esc_args[0] >= 1 &&
2179 term->esc_args[0] < 24) {
2180 compatibility(OTHER);
2182 switch (term->esc_args[0]) {
2186 set_iconic(term->frontend, FALSE);
2189 set_iconic(term->frontend, TRUE);
2192 if (term->esc_nargs >= 3) {
2193 if (!term->cfg->no_remote_resize)
2194 move_window(term->frontend,
2195 def(term->esc_args[1], 0),
2196 def(term->esc_args[2], 0));
2200 /* We should resize the window to a given
2201 * size in pixels here, but currently our
2202 * resizing code isn't healthy enough to
2207 set_zorder(term->frontend, TRUE);
2210 /* move to bottom */
2211 set_zorder(term->frontend, FALSE);
2214 refresh_window(term->frontend);
2217 if (term->esc_nargs >= 3) {
2218 if (!term->cfg->no_remote_resize)
2219 request_resize(term->frontend,
2220 def(term->esc_args[2], term->cfg->width),
2221 def(term->esc_args[1], term->cfg->height));
2225 if (term->esc_nargs >= 2)
2226 set_zoomed(term->frontend,
2232 ldisc_send(term->ldisc,
2233 is_iconic(term->frontend) ?
2234 "\033[1t" : "\033[2t", 4, 0);
2238 get_window_pos(term->frontend, &x, &y);
2239 len = sprintf(buf, "\033[3;%d;%dt", x, y);
2240 ldisc_send(term->ldisc, buf, len, 0);
2245 get_window_pixels(term->frontend, &x, &y);
2246 len = sprintf(buf, "\033[4;%d;%dt", x, y);
2247 ldisc_send(term->ldisc, buf, len, 0);
2252 len = sprintf(buf, "\033[8;%d;%dt",
2253 term->rows, term->cols);
2254 ldisc_send(term->ldisc, buf, len, 0);
2259 * Hmmm. Strictly speaking we
2260 * should return `the size of the
2261 * screen in characters', but
2262 * that's not easy: (a) window
2263 * furniture being what it is it's
2264 * hard to compute, and (b) in
2265 * resize-font mode maximising the
2266 * window wouldn't change the
2267 * number of characters. *shrug*. I
2268 * think we'll ignore it for the
2269 * moment and see if anyone
2270 * complains, and then ask them
2271 * what they would like it to do.
2276 p = get_window_title(term->frontend, TRUE);
2278 ldisc_send(term->ldisc, "\033]L", 3, 0);
2279 ldisc_send(term->ldisc, p, len, 0);
2280 ldisc_send(term->ldisc, "\033\\", 2, 0);
2285 p = get_window_title(term->frontend,FALSE);
2287 ldisc_send(term->ldisc, "\033]l", 3, 0);
2288 ldisc_send(term->ldisc, p, len, 0);
2289 ldisc_send(term->ldisc, "\033\\", 2, 0);
2296 compatibility(SCOANSI);
2297 scroll(term, term->marg_t, term->marg_b,
2298 def(term->esc_args[0], 1), TRUE);
2300 term->wrapnext = FALSE;
2301 term->seen_disp_event = TRUE;
2304 compatibility(SCOANSI);
2305 scroll(term, term->marg_t, term->marg_b,
2306 -def(term->esc_args[0], 1), TRUE);
2308 term->wrapnext = FALSE;
2309 term->seen_disp_event = TRUE;
2311 case ANSI('|', '*'):
2312 /* VT420 sequence DECSNLS
2313 * Set number of lines on screen
2314 * VT420 uses VGA like hardware and can support any size in
2315 * reasonable range (24..49 AIUI) with no default specified.
2317 compatibility(VT420);
2318 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
2319 if (!term->cfg->no_remote_resize)
2320 request_resize(term->frontend, term->cols,
2321 def(term->esc_args[0],
2322 term->cfg->height));
2326 case ANSI('|', '$'):
2327 /* VT340/VT420 sequence DECSCPP
2328 * Set number of columns per page
2329 * Docs imply range is only 80 or 132, but I'll allow any.
2331 compatibility(VT340TEXT);
2332 if (term->esc_nargs <= 1) {
2333 if (!term->cfg->no_remote_resize)
2334 request_resize(term->frontend,
2335 def(term->esc_args[0],
2336 term->cfg->width), term->rows);
2340 case 'X': /* write N spaces w/o moving cursor */
2341 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
2342 compatibility(ANSIMIN);
2344 int n = def(term->esc_args[0], 1);
2346 unsigned long *p = term->cpos;
2347 if (n > term->cols - term->curs.x)
2348 n = term->cols - term->curs.x;
2349 cursplus = term->curs;
2351 check_selection(term, term->curs, cursplus);
2353 *p++ = term->erase_char;
2354 term->seen_disp_event = TRUE;
2357 case 'x': /* report terminal characteristics */
2358 compatibility(VT100);
2361 int i = def(term->esc_args[0], 0);
2362 if (i == 0 || i == 1) {
2363 strcpy(buf, "\033[2;1;1;112;112;1;0x");
2365 ldisc_send(term->ldisc, buf, 20, 0);
2369 case 'Z': /* BackTab for xterm */
2370 compatibility(OTHER);
2372 int i = def(term->esc_args[0], 1);
2373 pos old_curs = term->curs;
2375 for(;i>0 && term->curs.x>0; i--) {
2378 } while (term->curs.x >0 &&
2379 !term->tabs[term->curs.x]);
2382 check_selection(term, old_curs, term->curs);
2385 case ANSI('L', '='):
2386 compatibility(OTHER);
2387 term->use_bce = (term->esc_args[0] <= 0);
2388 term->erase_char = ERASE_CHAR;
2390 term->erase_char = (' ' | ATTR_ASCII |
2392 (ATTR_FGMASK | ATTR_BGMASK)));
2394 case ANSI('E', '='):
2395 compatibility(OTHER);
2396 term->blink_is_real = (term->esc_args[0] >= 1);
2398 case ANSI('p', '"'):
2400 * Allow the host to make this emulator a
2401 * 'perfect' VT102. This first appeared in
2402 * the VT220, but we do need to get back to
2403 * PuTTY mode so I won't check it.
2405 * The arg in 40..42,50 are a PuTTY extension.
2406 * The 2nd arg, 8bit vs 7bit is not checked.
2408 * Setting VT102 mode should also change
2409 * the Fkeys to generate PF* codes as a
2410 * real VT102 has no Fkeys. The VT220 does
2411 * this, F11..F13 become ESC,BS,LF other
2412 * Fkeys send nothing.
2414 * Note ESC c will NOT change this!
2417 switch (term->esc_args[0]) {
2419 term->compatibility_level &= ~TM_VTXXX;
2420 term->compatibility_level |= TM_VT102;
2423 term->compatibility_level &= ~TM_VTXXX;
2424 term->compatibility_level |= TM_VT220;
2428 if (term->esc_args[0] > 60 &&
2429 term->esc_args[0] < 70)
2430 term->compatibility_level |= TM_VTXXX;
2434 term->compatibility_level &= TM_VTXXX;
2437 term->compatibility_level = TM_PUTTY;
2440 term->compatibility_level = TM_SCOANSI;
2444 term->compatibility_level = TM_PUTTY;
2450 /* Change the response to CSI c */
2451 if (term->esc_args[0] == 50) {
2454 strcpy(term->id_string, "\033[?");
2455 for (i = 1; i < term->esc_nargs; i++) {
2457 strcat(term->id_string, ";");
2458 sprintf(lbuf, "%d", term->esc_args[i]);
2459 strcat(term->id_string, lbuf);
2461 strcat(term->id_string, "c");
2464 /* Is this a good idea ?
2465 * Well we should do a soft reset at this point ...
2467 if (!has_compat(VT420) && has_compat(VT100)) {
2468 if (!term->cfg->no_remote_resize) {
2469 if (term->reset_132)
2470 request_resize(132, 24);
2472 request_resize(80, 24);
2480 term->osc_w = FALSE;
2482 case 'P': /* Linux palette sequence */
2483 term->termstate = SEEN_OSC_P;
2484 term->osc_strlen = 0;
2486 case 'R': /* Linux palette reset */
2487 palette_reset(term->frontend);
2488 term_invalidate(term);
2489 term->termstate = TOPLEVEL;
2491 case 'W': /* word-set */
2492 term->termstate = SEEN_OSC_W;
2505 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2509 * Grotty hack to support xterm and DECterm title
2510 * sequences concurrently.
2512 if (term->esc_args[0] == 2) {
2513 term->esc_args[0] = 1;
2516 /* else fall through */
2518 term->termstate = OSC_STRING;
2519 term->osc_strlen = 0;
2524 * This OSC stuff is EVIL. It takes just one character to get into
2525 * sysline mode and it's not initially obvious how to get out.
2526 * So I've added CR and LF as string aborts.
2527 * This shouldn't effect compatibility as I believe embedded
2528 * control characters are supposed to be interpreted (maybe?)
2529 * and they don't display anything useful anyway.
2533 if (c == '\012' || c == '\015') {
2534 term->termstate = TOPLEVEL;
2535 } else if (c == 0234 || c == '\007') {
2537 * These characters terminate the string; ST and BEL
2538 * terminate the sequence and trigger instant
2539 * processing of it, whereas ESC goes back to SEEN_ESC
2540 * mode unless it is followed by \, in which case it is
2541 * synonymous with ST in the first place.
2544 term->termstate = TOPLEVEL;
2545 } else if (c == '\033')
2546 term->termstate = OSC_MAYBE_ST;
2547 else if (term->osc_strlen < OSC_STR_MAX)
2548 term->osc_string[term->osc_strlen++] = c;
2552 int max = (term->osc_strlen == 0 ? 21 : 16);
2554 if (c >= '0' && c <= '9')
2556 else if (c >= 'A' && c <= 'A' + max - 10)
2558 else if (c >= 'a' && c <= 'a' + max - 10)
2561 term->termstate = TOPLEVEL;
2564 term->osc_string[term->osc_strlen++] = val;
2565 if (term->osc_strlen >= 7) {
2566 palette_set(term->frontend, term->osc_string[0],
2567 term->osc_string[1] * 16 + term->osc_string[2],
2568 term->osc_string[3] * 16 + term->osc_string[4],
2569 term->osc_string[5] * 16 + term->osc_string[6]);
2570 term_invalidate(term);
2571 term->termstate = TOPLEVEL;
2587 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2590 term->termstate = OSC_STRING;
2591 term->osc_strlen = 0;
2595 term->termstate = TOPLEVEL;
2596 term->seen_disp_event = TRUE;
2599 move(term, term->curs.x, term->curs.y - 1, 1);
2602 move(term, term->curs.x, term->curs.y + 1, 1);
2605 move(term, term->curs.x + 1, term->curs.y, 1);
2608 move(term, term->curs.x - 1, term->curs.y, 1);
2611 * From the VT100 Manual
2612 * NOTE: The special graphics characters in the VT100
2613 * are different from those in the VT52
2615 * From VT102 manual:
2616 * 137 _ Blank - Same
2617 * 140 ` Reserved - Humm.
2618 * 141 a Solid rectangle - Similar
2619 * 142 b 1/ - Top half of fraction for the
2620 * 143 c 3/ - subscript numbers below.
2623 * 146 f Degrees - Same
2624 * 147 g Plus or minus - Same
2626 * 151 i Ellipsis (dots)
2629 * 154 l Bar at scan 0
2630 * 155 m Bar at scan 1
2631 * 156 n Bar at scan 2
2632 * 157 o Bar at scan 3 - Similar
2633 * 160 p Bar at scan 4 - Similar
2634 * 161 q Bar at scan 5 - Similar
2635 * 162 r Bar at scan 6 - Same
2636 * 163 s Bar at scan 7 - Similar
2651 term->cset_attr[term->cset = 0] = ATTR_LINEDRW;
2654 term->cset_attr[term->cset = 0] = ATTR_ASCII;
2657 move(term, 0, 0, 0);
2660 if (term->curs.y == 0)
2661 scroll(term, 0, term->rows - 1, -1, TRUE);
2662 else if (term->curs.y > 0)
2665 term->wrapnext = FALSE;
2668 erase_lots(term, FALSE, FALSE, TRUE);
2672 erase_lots(term, TRUE, FALSE, TRUE);
2676 /* XXX Print cursor line */
2679 /* XXX Start controller mode */
2682 /* XXX Stop controller mode */
2686 term->termstate = VT52_Y1;
2690 ldisc_send(term->ldisc, "\033/Z", 3, 0);
2693 term->app_keypad_keys = TRUE;
2696 term->app_keypad_keys = FALSE;
2699 /* XXX This should switch to VT100 mode not current or default
2700 * VT mode. But this will only have effect in a VT220+
2703 term->vt52_mode = FALSE;
2704 term->blink_is_real = term->cfg->blinktext;
2708 /* XXX Enter auto print mode */
2711 /* XXX Exit auto print mode */
2714 /* XXX Print screen */
2720 /* compatibility(ATARI) */
2721 move(term, 0, 0, 0);
2722 erase_lots(term, FALSE, FALSE, TRUE);
2726 /* compatibility(ATARI) */
2727 if (term->curs.y <= term->marg_b)
2728 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
2731 /* compatibility(ATARI) */
2732 if (term->curs.y <= term->marg_b)
2733 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
2736 /* compatibility(ATARI) */
2737 term->termstate = VT52_FG;
2740 /* compatibility(ATARI) */
2741 term->termstate = VT52_BG;
2744 /* compatibility(ATARI) */
2745 erase_lots(term, FALSE, TRUE, FALSE);
2749 /* compatibility(ATARI) */
2750 term->cursor_on = TRUE;
2753 /* compatibility(ATARI) */
2754 term->cursor_on = FALSE;
2756 /* case 'j': Save cursor position - broken on ST */
2757 /* case 'k': Restore cursor position */
2759 /* compatibility(ATARI) */
2760 erase_lots(term, TRUE, TRUE, TRUE);
2762 term->wrapnext = FALSE;
2766 /* compatibility(ATARI) */
2767 erase_lots(term, TRUE, TRUE, FALSE);
2770 /* compatibility(ATARI) */
2771 term->curr_attr |= ATTR_REVERSE;
2774 /* compatibility(ATARI) */
2775 term->curr_attr &= ~ATTR_REVERSE;
2777 case 'v': /* wrap Autowrap on - Wyse style */
2778 /* compatibility(ATARI) */
2781 case 'w': /* Autowrap off */
2782 /* compatibility(ATARI) */
2787 /* compatibility(OTHER) */
2788 term->vt52_bold = FALSE;
2789 term->curr_attr = ATTR_DEFAULT;
2791 term->erase_char = (' ' | ATTR_ASCII |
2793 (ATTR_FGMASK | ATTR_BGMASK)));
2796 /* compatibility(VI50) */
2797 term->curr_attr |= ATTR_UNDER;
2800 /* compatibility(VI50) */
2801 term->curr_attr &= ~ATTR_UNDER;
2804 /* compatibility(VI50) */
2805 term->vt52_bold = TRUE;
2806 term->curr_attr |= ATTR_BOLD;
2809 /* compatibility(VI50) */
2810 term->vt52_bold = FALSE;
2811 term->curr_attr &= ~ATTR_BOLD;
2817 term->termstate = VT52_Y2;
2818 move(term, term->curs.x, c - ' ', 0);
2821 term->termstate = TOPLEVEL;
2822 move(term, c - ' ', term->curs.y, 0);
2827 term->termstate = TOPLEVEL;
2828 term->curr_attr &= ~ATTR_FGMASK;
2829 term->curr_attr &= ~ATTR_BOLD;
2830 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
2831 if ((c & 0x8) || term->vt52_bold)
2832 term->curr_attr |= ATTR_BOLD;
2835 term->erase_char = (' ' | ATTR_ASCII |
2837 (ATTR_FGMASK | ATTR_BGMASK)));
2840 term->termstate = TOPLEVEL;
2841 term->curr_attr &= ~ATTR_BGMASK;
2842 term->curr_attr &= ~ATTR_BLINK;
2843 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
2845 /* Note: bold background */
2847 term->curr_attr |= ATTR_BLINK;
2850 term->erase_char = (' ' | ATTR_ASCII |
2852 (ATTR_FGMASK | ATTR_BGMASK)));
2855 default: break; /* placate gcc warning about enum use */
2857 if (term->selstate != NO_SELECTION) {
2858 pos cursplus = term->curs;
2860 check_selection(term, term->curs, cursplus);
2864 term_print_flush(term);
2869 * Compare two lines to determine whether they are sufficiently
2870 * alike to scroll-optimise one to the other. Return the degree of
2873 static int linecmp(Terminal *term, unsigned long *a, unsigned long *b)
2877 for (i = n = 0; i < term->cols; i++)
2878 n += (*a++ == *b++);
2884 * Given a context, update the window. Out of paranoia, we don't
2885 * allow WM_PAINT responses to do scrolling optimisations.
2887 static void do_paint(Terminal *term, Context ctx, int may_optimise)
2889 int i, j, our_curs_y;
2890 unsigned long rv, cursor;
2893 long cursor_background = ERASE_CHAR;
2894 unsigned long ticks;
2897 * Check the visual bell state.
2899 if (term->in_vbell) {
2900 ticks = GETTICKCOUNT();
2901 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
2902 term->in_vbell = FALSE;
2905 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
2908 * screen array, disptop, scrtop,
2910 * cfg.blinkpc, blink_is_real, tblinker,
2911 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
2914 /* Has the cursor position or type changed ? */
2915 if (term->cursor_on) {
2916 if (term->has_focus) {
2917 if (term->blinker || !term->cfg->blink_cur)
2918 cursor = TATTR_ACTCURS;
2922 cursor = TATTR_PASCURS;
2924 cursor |= TATTR_RIGHTCURS;
2927 our_curs_y = term->curs.y - term->disptop;
2929 if (term->dispcurs && (term->curstype != cursor ||
2931 term->disptext + our_curs_y * (term->cols + 1) +
2933 if (term->dispcurs > term->disptext &&
2934 (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
2935 term->dispcurs[-1] |= ATTR_INVALID;
2936 if ( (term->dispcurs[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
2937 term->dispcurs[1] |= ATTR_INVALID;
2938 *term->dispcurs |= ATTR_INVALID;
2941 term->dispcurs = NULL;
2943 /* The normal screen data */
2944 for (i = 0; i < term->rows; i++) {
2945 unsigned long *ldata;
2947 int idx, dirty_line, dirty_run, selected;
2948 unsigned long attr = 0;
2949 int updated_line = 0;
2952 int last_run_dirty = 0;
2954 scrpos.y = i + term->disptop;
2955 ldata = lineptr(scrpos.y);
2956 lattr = (ldata[term->cols] & LATTR_MODE);
2958 idx = i * (term->cols + 1);
2959 dirty_run = dirty_line = (ldata[term->cols] !=
2960 term->disptext[idx + term->cols]);
2961 term->disptext[idx + term->cols] = ldata[term->cols];
2963 for (j = 0; j < term->cols; j++, idx++) {
2964 unsigned long tattr, tchar;
2965 unsigned long *d = ldata + j;
2969 tchar = (*d & (CHAR_MASK | CSET_MASK));
2970 tattr = (*d & (ATTR_MASK ^ CSET_MASK));
2971 switch (tchar & CSET_MASK) {
2973 tchar = unitab_line[tchar & 0xFF];
2976 tchar = unitab_xterm[tchar & 0xFF];
2979 tchar = unitab_scoacs[tchar&0xFF];
2982 tattr |= (tchar & CSET_MASK);
2984 if ((d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
2987 /* Video reversing things */
2988 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
2989 if (term->seltype == LEXICOGRAPHIC)
2990 selected = (posle(term->selstart, scrpos) &&
2991 poslt(scrpos, term->selend));
2993 selected = (posPle(term->selstart, scrpos) &&
2994 posPlt(scrpos, term->selend));
2998 ^ (selected ? ATTR_REVERSE : 0));
3000 /* 'Real' blinking ? */
3001 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
3002 if (term->has_focus && term->tblinker) {
3004 tattr &= ~CSET_MASK;
3007 tattr &= ~ATTR_BLINK;
3011 * Check the font we'll _probably_ be using to see if
3012 * the character is wide when we don't want it to be.
3014 if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) {
3015 if ((tattr & ATTR_WIDE) == 0 &&
3016 char_width(ctx, (tchar | tattr) & 0xFFFF) == 2)
3017 tattr |= ATTR_NARROW;
3018 } else if (term->disptext[idx]&ATTR_NARROW)
3019 tattr |= ATTR_NARROW;
3021 /* Cursor here ? Save the 'background' */
3022 if (i == our_curs_y && j == term->curs.x) {
3023 cursor_background = tattr | tchar;
3024 term->dispcurs = term->disptext + idx;
3027 if ((term->disptext[idx] ^ tattr) & ATTR_WIDE)
3030 break_run = (tattr != attr || j - start >= sizeof(ch));
3032 /* Special hack for VT100 Linedraw glyphs */
3033 if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA
3034 && tchar <= 0xBD) break_run = TRUE;
3036 if (!dbcs_screenfont && !dirty_line) {
3037 if ((tchar | tattr) == term->disptext[idx])
3039 else if (!dirty_run && ccount == 1)
3044 if ((dirty_run || last_run_dirty) && ccount > 0) {
3045 do_text(ctx, start, i, ch, ccount, attr, lattr);
3051 if (dbcs_screenfont)
3052 last_run_dirty = dirty_run;
3053 dirty_run = dirty_line;
3056 if ((tchar | tattr) != term->disptext[idx])
3058 ch[ccount++] = (char) tchar;
3059 term->disptext[idx] = tchar | tattr;
3061 /* If it's a wide char step along to the next one. */
3062 if (tattr & ATTR_WIDE) {
3063 if (++j < term->cols) {
3066 /* Cursor is here ? Ouch! */
3067 if (i == our_curs_y && j == term->curs.x) {
3068 cursor_background = *d;
3069 term->dispcurs = term->disptext + idx;
3071 if (term->disptext[idx] != *d)
3073 term->disptext[idx] = *d;
3077 if (dirty_run && ccount > 0) {
3078 do_text(ctx, start, i, ch, ccount, attr, lattr);
3082 /* Cursor on this line ? (and changed) */
3083 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
3084 ch[0] = (char) (cursor_background & CHAR_MASK);
3085 attr = (cursor_background & ATTR_MASK) | cursor;
3086 do_cursor(ctx, term->curs.x, i, ch, 1, attr, lattr);
3087 term->curstype = cursor;
3093 * Flick the switch that says if blinking things should be shown or hidden.
3096 void term_blink(Terminal *term, int flg)
3098 long now, blink_diff;
3100 now = GETTICKCOUNT();
3101 blink_diff = now - term->last_tblink;
3103 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3104 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
3105 term->last_tblink = now;
3106 term->tblinker = !term->tblinker;
3111 term->last_blink = now;
3115 blink_diff = now - term->last_blink;
3117 /* Make sure the cursor blinks no faster than system blink rate */
3118 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
3121 term->last_blink = now;
3122 term->blinker = !term->blinker;
3126 * Invalidate the whole screen so it will be repainted in full.
3128 void term_invalidate(Terminal *term)
3132 for (i = 0; i < term->rows * (term->cols + 1); i++)
3133 term->disptext[i] = ATTR_INVALID;
3137 * Paint the window in response to a WM_PAINT message.
3139 void term_paint(Terminal *term, Context ctx,
3140 int left, int top, int right, int bottom, int immediately)
3143 if (left < 0) left = 0;
3144 if (top < 0) top = 0;
3145 if (right >= term->cols) right = term->cols-1;
3146 if (bottom >= term->rows) bottom = term->rows-1;
3148 for (i = top; i <= bottom && i < term->rows; i++) {
3149 if ((term->disptext[i * (term->cols + 1) + term->cols] &
3150 LATTR_MODE) == LATTR_NORM)
3151 for (j = left; j <= right && j < term->cols; j++)
3152 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3154 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
3155 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3158 /* This should happen soon enough, also for some reason it sometimes
3159 * fails to actually do anything when re-sizing ... painting the wrong
3163 do_paint (term, ctx, FALSE);
3167 * Attempt to scroll the scrollback. The second parameter gives the
3168 * position we want to scroll to; the first is +1 to denote that
3169 * this position is relative to the beginning of the scrollback, -1
3170 * to denote it is relative to the end, and 0 to denote that it is
3171 * relative to the current position.
3173 void term_scroll(Terminal *term, int rel, int where)
3175 int sbtop = -count234(term->scrollback);
3176 #ifdef OPTIMISE_SCROLL
3177 int olddisptop = term->disptop;
3179 #endif /* OPTIMISE_SCROLL */
3181 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
3182 if (term->disptop < sbtop)
3183 term->disptop = sbtop;
3184 if (term->disptop > 0)
3187 #ifdef OPTIMISE_SCROLL
3188 shift = (term->disptop - olddisptop);
3189 if (shift < term->rows && shift > -term->rows)
3190 scroll_display(term, 0, term->rows - 1, shift);
3191 #endif /* OPTIMISE_SCROLL */
3195 static void clipme(Terminal *term, pos top, pos bottom, int rect)
3198 wchar_t *wbptr; /* where next char goes within workbuf */
3200 int wblen = 0; /* workbuf len */
3201 int buflen; /* amount of memory allocated to workbuf */
3203 buflen = 5120; /* Default size */
3204 workbuf = smalloc(buflen * sizeof(wchar_t));
3205 wbptr = workbuf; /* start filling here */
3206 old_top_x = top.x; /* needed for rect==1 */
3208 while (poslt(top, bottom)) {
3210 unsigned long *ldata = lineptr(top.y);
3214 * nlpos will point at the maximum position on this line we
3215 * should copy up to. So we start it at the end of the
3219 nlpos.x = term->cols;
3222 * ... move it backwards if there's unused space at the end
3223 * of the line (and also set `nl' if this is the case,
3224 * because in normal selection mode this means we need a
3225 * newline at the end)...
3227 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3228 while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 ||
3229 (DIRECT_CHAR(ldata[nlpos.x - 1]) &&
3230 (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20))
3231 && poslt(top, nlpos))
3233 if (poslt(nlpos, bottom))
3238 * ... and then clip it to the terminal x coordinate if
3239 * we're doing rectangular selection. (In this case we
3240 * still did the above, so that copying e.g. the right-hand
3241 * column from a table doesn't fill with spaces on the
3245 if (nlpos.x > bottom.x)
3247 nl = (top.y < bottom.y);
3250 while (poslt(top, bottom) && poslt(top, nlpos)) {
3253 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
3255 wchar_t cbuf[16], *p;
3256 int uc = (ldata[top.x] & 0xFFFF);
3259 if (uc == UCSWIDE) {
3264 switch (uc & CSET_MASK) {
3266 if (!term->cfg->rawcnp) {
3267 uc = unitab_xterm[uc & 0xFF];
3271 uc = unitab_line[uc & 0xFF];
3274 uc = unitab_scoacs[uc&0xFF];
3277 switch (uc & CSET_MASK) {
3279 uc = unitab_font[uc & 0xFF];
3282 uc = unitab_oemcp[uc & 0xFF];
3286 set = (uc & CSET_MASK);
3287 c = (uc & CHAR_MASK);
3291 if (DIRECT_FONT(uc)) {
3292 if (c >= ' ' && c != 0x7F) {
3296 if (is_dbcs_leadbyte(font_codepage, (BYTE) c)) {
3298 buf[1] = ldata[top.x + 1];
3299 rv = mb_to_wc(font_codepage, 0, buf, 2, wbuf, 4);
3303 rv = mb_to_wc(font_codepage, 0, buf, 1, wbuf, 4);
3307 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
3314 for (p = cbuf; *p; p++) {
3315 /* Enough overhead for trailing NL and nul */
3316 if (wblen >= buflen - 16) {
3319 sizeof(wchar_t) * (buflen += 100));
3320 wbptr = workbuf + wblen;
3329 for (i = 0; i < sel_nl_sz; i++) {
3331 *wbptr++ = sel_nl[i];
3335 top.x = rect ? old_top_x : 0;
3337 #if SELECTION_NUL_TERMINATED
3341 write_clip(term->frontend, workbuf, wblen, FALSE); /* transfer to clipbd */
3342 if (buflen > 0) /* indicates we allocated this buffer */
3346 void term_copyall(Terminal *term)
3349 top.y = -count234(term->scrollback);
3351 clipme(term, top, term->curs, 0);
3355 * The wordness array is mainly for deciding the disposition of the
3356 * US-ASCII characters.
3358 static int wordtype(Terminal *term, int uc)
3361 int start, end, ctype;
3363 static const struct ucsword ucs_words[] = {
3369 0x037e, 0x037e, 1}, /* Greek question mark */
3371 0x0387, 0x0387, 1}, /* Greek ano teleia */
3373 0x055a, 0x055f, 1}, /* Armenian punctuation */
3375 0x0589, 0x0589, 1}, /* Armenian full stop */
3377 0x0700, 0x070d, 1}, /* Syriac punctuation */
3379 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3381 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3383 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3385 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3387 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3389 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3391 0x2000, 0x200a, 0}, /* Various spaces */
3393 0x2070, 0x207f, 2}, /* superscript */
3395 0x2080, 0x208f, 2}, /* subscript */
3397 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3399 0x3000, 0x3000, 0}, /* ideographic space */
3401 0x3001, 0x3020, 1}, /* ideographic punctuation */
3403 0x303f, 0x309f, 3}, /* Hiragana */
3405 0x30a0, 0x30ff, 3}, /* Katakana */
3407 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3409 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3411 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3413 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3415 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3417 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3419 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3421 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3423 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3427 const struct ucsword *wptr;
3429 uc &= (CSET_MASK | CHAR_MASK);
3431 switch (uc & CSET_MASK) {
3433 uc = unitab_xterm[uc & 0xFF];
3436 uc = unitab_line[uc & 0xFF];
3439 uc = unitab_scoacs[uc&0xFF];
3442 switch (uc & CSET_MASK) {
3444 uc = unitab_font[uc & 0xFF];
3447 uc = unitab_oemcp[uc & 0xFF];
3451 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3452 * fail as there's such a thing as a double width space. :-(
3454 if (dbcs_screenfont && font_codepage == line_codepage)
3458 return term->wordness[uc];
3460 for (wptr = ucs_words; wptr->start; wptr++) {
3461 if (uc >= wptr->start && uc <= wptr->end)
3469 * Spread the selection outwards according to the selection mode.
3471 static pos sel_spread_half(Terminal *term, pos p, int dir)
3473 unsigned long *ldata;
3475 int topy = -count234(term->scrollback);
3477 ldata = lineptr(p.y);
3479 switch (term->selmode) {
3482 * In this mode, every character is a separate unit, except
3483 * for runs of spaces at the end of a non-wrapping line.
3485 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3486 unsigned long *q = ldata + term->cols;
3487 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
3489 if (q == ldata + term->cols)
3491 if (p.x >= q - ldata)
3492 p.x = (dir == -1 ? q - ldata : term->cols - 1);
3497 * In this mode, the units are maximal runs of characters
3498 * whose `wordness' has the same value.
3500 wvalue = wordtype(term, ldata[p.x]);
3503 if (p.x < term->cols-1) {
3504 if (wordtype(term, ldata[p.x + 1]) == wvalue)
3509 if (ldata[term->cols] & LATTR_WRAPPED) {
3510 unsigned long *ldata2;
3511 ldata2 = lineptr(p.y+1);
3512 if (wordtype(term, ldata2[0]) == wvalue) {
3525 if (wordtype(term, ldata[p.x - 1]) == wvalue)
3530 unsigned long *ldata2;
3533 ldata2 = lineptr(p.y-1);
3534 if ((ldata2[term->cols] & LATTR_WRAPPED) &&
3535 wordtype(term, ldata2[term->cols-1]) == wvalue) {
3547 * In this mode, every line is a unit.
3549 p.x = (dir == -1 ? 0 : term->cols - 1);
3555 static void sel_spread(Terminal *term)
3557 if (term->seltype == LEXICOGRAPHIC) {
3558 term->selstart = sel_spread_half(term, term->selstart, -1);
3559 decpos(term->selend);
3560 term->selend = sel_spread_half(term, term->selend, +1);
3561 incpos(term->selend);
3565 void term_do_paste(Terminal *term)
3570 get_clip(term->frontend, &data, &len);
3571 if (data && len > 0) {
3574 term_seen_key_event(term); /* pasted data counts */
3576 if (term->paste_buffer)
3577 sfree(term->paste_buffer);
3578 term->paste_pos = term->paste_hold = term->paste_len = 0;
3579 term->paste_buffer = smalloc(len * sizeof(wchar_t));
3582 while (p < data + len) {
3583 while (p < data + len &&
3584 !(p <= data + len - sel_nl_sz &&
3585 !memcmp(p, sel_nl, sizeof(sel_nl))))
3590 for (i = 0; i < p - q; i++) {
3591 term->paste_buffer[term->paste_len++] = q[i];
3595 if (p <= data + len - sel_nl_sz &&
3596 !memcmp(p, sel_nl, sizeof(sel_nl))) {
3597 term->paste_buffer[term->paste_len++] = '\015';
3603 /* Assume a small paste will be OK in one go. */
3604 if (term->paste_len < 256) {
3606 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
3607 if (term->paste_buffer)
3608 sfree(term->paste_buffer);
3609 term->paste_buffer = 0;
3610 term->paste_pos = term->paste_hold = term->paste_len = 0;
3613 get_clip(term->frontend, NULL, NULL);
3616 void term_mouse(Terminal *term, Mouse_Button b, Mouse_Action a, int x, int y,
3617 int shift, int ctrl, int alt)
3620 unsigned long *ldata;
3621 int raw_mouse = (term->xterm_mouse &&
3622 !term->cfg->no_mouse_rep &&
3623 !(term->cfg->mouse_override && shift));
3624 int default_seltype;
3628 if (a == MA_DRAG && !raw_mouse)
3629 term_scroll(term, 0, -1);
3631 if (y >= term->rows) {
3633 if (a == MA_DRAG && !raw_mouse)
3634 term_scroll(term, 0, +1);
3643 if (x >= term->cols)
3646 selpoint.y = y + term->disptop;
3648 ldata = lineptr(selpoint.y);
3649 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM)
3653 int encstate = 0, r, c;
3660 encstate = 0x20; /* left button down */
3671 case MBT_WHEEL_DOWN:
3674 default: break; /* placate gcc warning about enum use */
3678 if (term->xterm_mouse == 1)
3684 term->mouse_is_down = 0;
3687 if (term->mouse_is_down == b)
3689 term->mouse_is_down = b;
3691 default: break; /* placate gcc warning about enum use */
3700 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
3701 ldisc_send(term->ldisc, abuf, 6, 0);
3706 b = translate_button(term->frontend, b);
3709 * Set the selection type (rectangular or normal) at the start
3710 * of a selection attempt, from the state of Alt.
3712 if (!alt ^ !term->cfg->rect_select)
3713 default_seltype = RECTANGULAR;
3715 default_seltype = LEXICOGRAPHIC;
3717 if (term->selstate == NO_SELECTION) {
3718 term->seltype = default_seltype;
3721 if (b == MBT_SELECT && a == MA_CLICK) {
3723 term->selstate = ABOUT_TO;
3724 term->seltype = default_seltype;
3725 term->selanchor = selpoint;
3726 term->selmode = SM_CHAR;
3727 } else if (b == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
3729 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
3730 term->selstate = DRAGGING;
3731 term->selstart = term->selanchor = selpoint;
3732 term->selend = term->selstart;
3733 incpos(term->selend);
3735 } else if ((b == MBT_SELECT && a == MA_DRAG) ||
3736 (b == MBT_EXTEND && a != MA_RELEASE)) {
3737 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
3739 if (b == MBT_EXTEND && a != MA_DRAG && term->selstate == SELECTED) {
3740 if (term->seltype == LEXICOGRAPHIC) {
3742 * For normal selection, we extend by moving
3743 * whichever end of the current selection is closer
3746 if (posdiff(selpoint, term->selstart) <
3747 posdiff(term->selend, term->selstart) / 2) {
3748 term->selanchor = term->selend;
3749 decpos(term->selanchor);
3751 term->selanchor = term->selstart;
3755 * For rectangular selection, we have a choice of
3756 * _four_ places to put selanchor and selpoint: the
3757 * four corners of the selection.
3759 if (2*selpoint.x < term->selstart.x + term->selend.x)
3760 term->selanchor.x = term->selend.x-1;
3762 term->selanchor.x = term->selstart.x;
3764 if (2*selpoint.y < term->selstart.y + term->selend.y)
3765 term->selanchor.y = term->selend.y;
3767 term->selanchor.y = term->selstart.y;
3769 term->selstate = DRAGGING;
3771 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
3772 term->selanchor = selpoint;
3773 term->selstate = DRAGGING;
3774 if (term->seltype == LEXICOGRAPHIC) {
3776 * For normal selection, we set (selstart,selend) to
3777 * (selpoint,selanchor) in some order.
3779 if (poslt(selpoint, term->selanchor)) {
3780 term->selstart = selpoint;
3781 term->selend = term->selanchor;
3782 incpos(term->selend);
3784 term->selstart = term->selanchor;
3785 term->selend = selpoint;
3786 incpos(term->selend);
3790 * For rectangular selection, we may need to
3791 * interchange x and y coordinates (if the user has
3792 * dragged in the -x and +y directions, or vice versa).
3794 term->selstart.x = min(term->selanchor.x, selpoint.x);
3795 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
3796 term->selstart.y = min(term->selanchor.y, selpoint.y);
3797 term->selend.y = max(term->selanchor.y, selpoint.y);
3800 } else if ((b == MBT_SELECT || b == MBT_EXTEND) && a == MA_RELEASE) {
3801 if (term->selstate == DRAGGING) {
3803 * We've completed a selection. We now transfer the
3804 * data to the clipboard.
3806 clipme(term, term->selstart, term->selend,
3807 (term->seltype == RECTANGULAR));
3808 term->selstate = SELECTED;
3810 term->selstate = NO_SELECTION;
3811 } else if (b == MBT_PASTE
3813 #if MULTICLICK_ONLY_EVENT
3814 || a == MA_2CLK || a == MA_3CLK
3817 request_paste(term->frontend);
3823 void term_nopaste(Terminal *term)
3825 if (term->paste_len == 0)
3827 sfree(term->paste_buffer);
3828 term->paste_buffer = NULL;
3829 term->paste_len = 0;
3832 int term_paste_pending(Terminal *term)
3834 return term->paste_len != 0;
3837 void term_paste(Terminal *term)
3839 long now, paste_diff;
3841 if (term->paste_len == 0)
3844 /* Don't wait forever to paste */
3845 if (term->paste_hold) {
3846 now = GETTICKCOUNT();
3847 paste_diff = now - term->last_paste;
3848 if (paste_diff >= 0 && paste_diff < 450)
3851 term->paste_hold = 0;
3853 while (term->paste_pos < term->paste_len) {
3855 while (n + term->paste_pos < term->paste_len) {
3856 if (term->paste_buffer[term->paste_pos + n++] == '\015')
3860 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
3861 term->paste_pos += n;
3863 if (term->paste_pos < term->paste_len) {
3864 term->paste_hold = 1;
3868 sfree(term->paste_buffer);
3869 term->paste_buffer = NULL;
3870 term->paste_len = 0;
3873 static void deselect(Terminal *term)
3875 term->selstate = NO_SELECTION;
3876 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
3879 void term_deselect(Terminal *term)
3885 int term_ldisc(Terminal *term, int option)
3887 if (option == LD_ECHO)
3888 return term->term_echoing;
3889 if (option == LD_EDIT)
3890 return term->term_editing;
3895 * from_backend(), to get data from the backend for the terminal.
3897 int from_backend(void *vterm, int is_stderr, char *data, int len)
3899 Terminal *term = (Terminal *)vterm;
3903 bufchain_add(&term->inbuf, data, len);
3906 * term_out() always completely empties inbuf. Therefore,
3907 * there's no reason at all to return anything other than zero
3908 * from this function, because there _can't_ be a question of
3909 * the remote side needing to wait until term_out() has cleared
3912 * This is a slightly suboptimal way to deal with SSH2 - in
3913 * principle, the window mechanism would allow us to continue
3914 * to accept data on forwarded ports and X connections even
3915 * while the terminal processing was going slowly - but we
3916 * can't do the 100% right thing without moving the terminal
3917 * processing into a separate thread, and that might hurt
3918 * portability. So we manage stdout buffering the old SSH1 way:
3919 * if the terminal processing goes slowly, the whole SSH
3920 * connection stops accepting data until it's ready.
3922 * In practice, I can't imagine this causing serious trouble.
3927 void term_provide_logctx(Terminal *term, void *logctx)
3929 term->logctx = logctx;