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 unsigned long *resizeline(unsigned long *, int);
66 static unsigned long *lineptr(Terminal *, int, int);
67 static void do_paint(Terminal *, Context, int);
68 static void erase_lots(Terminal *, int, int, int);
69 static void swap_screen(Terminal *, int, int, int);
70 static void update_sbar(Terminal *);
71 static void deselect(Terminal *);
72 static void term_print_finish(Terminal *);
73 #ifdef OPTIMISE_SCROLL
74 static void scroll_display(Terminal *, int, int, int);
75 #endif /* OPTIMISE_SCROLL */
78 * Resize a line to make it `cols' columns wide.
80 static unsigned long *resizeline(unsigned long *line, int cols)
83 unsigned long lineattrs;
85 if (line[0] != (unsigned long)cols) {
87 * This line is the wrong length, which probably means it
88 * hasn't been accessed since a resize. Resize it now.
91 lineattrs = line[oldlen + 1];
92 line = srealloc(line, TSIZE * (2 + cols));
94 for (i = oldlen; i < cols; i++)
95 line[i + 1] = ERASE_CHAR;
96 line[cols + 1] = lineattrs & LATTR_MODE;
103 * Retrieve a line of the screen or of the scrollback, according to
104 * whether the y coordinate is non-negative or negative
107 static unsigned long *lineptr(Terminal *term, int y, int lineno)
109 unsigned long *line, *newline;
114 whichtree = term->screen;
117 whichtree = term->scrollback;
118 treeindex = y + count234(term->scrollback);
120 line = index234(whichtree, treeindex);
122 /* We assume that we don't screw up and retrieve something out of range. */
123 assert(line != NULL);
125 newline = resizeline(line, term->cols);
126 if (newline != line) {
127 delpos234(whichtree, treeindex);
128 addpos234(whichtree, newline, treeindex);
135 #define lineptr(x) lineptr(term,x,__LINE__)
138 * Set up power-on settings for the terminal.
140 static void power_on(Terminal *term)
142 term->curs.x = term->curs.y = 0;
143 term->alt_x = term->alt_y = 0;
144 term->savecurs.x = term->savecurs.y = 0;
145 term->alt_t = term->marg_t = 0;
146 if (term->rows != -1)
147 term->alt_b = term->marg_b = term->rows - 1;
149 term->alt_b = term->marg_b = 0;
150 if (term->cols != -1) {
152 for (i = 0; i < term->cols; i++)
153 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
155 term->alt_om = term->dec_om = term->cfg->dec_om;
156 term->alt_ins = term->insert = FALSE;
157 term->alt_wnext = term->wrapnext = term->save_wnext = FALSE;
158 term->alt_wrap = term->wrap = term->cfg->wrap_mode;
159 term->alt_cset = term->cset = term->save_cset = 0;
160 term->alt_utf = term->utf = term->save_utf = 0;
162 term->alt_sco_acs = term->sco_acs = term->save_sco_acs = 0;
163 term->cset_attr[0] = term->cset_attr[1] = term->save_csattr = ATTR_ASCII;
165 term->in_vbell = FALSE;
167 term->big_cursor = 0;
168 term->save_attr = term->curr_attr = ATTR_DEFAULT;
169 term->term_editing = term->term_echoing = FALSE;
170 term->app_cursor_keys = term->cfg->app_cursor;
171 term->app_keypad_keys = term->cfg->app_keypad;
172 term->use_bce = term->cfg->bce;
173 term->blink_is_real = term->cfg->blinktext;
174 term->erase_char = ERASE_CHAR;
176 term_print_finish(term);
179 for (i = 0; i < 256; i++)
180 term->wordness[i] = term->cfg->wordness[i];
183 swap_screen(term, 1, FALSE, FALSE);
184 erase_lots(term, FALSE, TRUE, TRUE);
185 swap_screen(term, 0, FALSE, FALSE);
186 erase_lots(term, FALSE, TRUE, TRUE);
191 * Force a screen update.
193 void term_update(Terminal *term)
196 ctx = get_ctx(term->frontend);
198 int need_sbar_update = term->seen_disp_event;
199 if (term->seen_disp_event && term->cfg->scroll_on_disp) {
200 term->disptop = 0; /* return to main screen */
201 term->seen_disp_event = 0;
202 need_sbar_update = TRUE;
204 if (need_sbar_update)
206 do_paint(term, ctx, TRUE);
207 sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop);
213 * Called from front end when a keypress occurs, to trigger
214 * anything magical that needs to happen in that situation.
216 void term_seen_key_event(Terminal *term)
219 * On any keypress, clear the bell overload mechanism
220 * completely, on the grounds that large numbers of
221 * beeps coming from deliberate key action are likely
222 * to be intended (e.g. beeps from filename completion
223 * blocking repeatedly).
225 term->beep_overloaded = FALSE;
226 while (term->beephead) {
227 struct beeptime *tmp = term->beephead;
228 term->beephead = tmp->next;
231 term->beeptail = NULL;
235 * Reset the scrollback on keypress, if we're doing that.
237 if (term->cfg->scroll_on_key) {
238 term->disptop = 0; /* return to main screen */
239 term->seen_disp_event = 1;
244 * Same as power_on(), but an external function.
246 void term_pwron(Terminal *term)
249 if (term->ldisc) /* cause ldisc to notice changes */
250 ldisc_send(term->ldisc, NULL, 0, 0);
258 * When the user reconfigures us, we need to check the forbidden-
259 * alternate-screen config option, disable raw mouse mode if the
260 * user has disabled mouse reporting, and abandon a print job if
261 * the user has disabled printing.
263 void term_reconfig(Terminal *term)
265 if (term->cfg->no_alt_screen)
266 swap_screen(term, 0, FALSE, FALSE);
267 if (term->cfg->no_mouse_rep) {
268 term->xterm_mouse = 0;
269 set_raw_mouse_mode(term->frontend, 0);
271 if (term->cfg->no_remote_charset) {
272 term->cset_attr[0] = term->cset_attr[1] = ATTR_ASCII;
273 term->sco_acs = term->alt_sco_acs = 0;
276 if (!*term->cfg->printer) {
277 term_print_finish(term);
282 * Clear the scrollback.
284 void term_clrsb(Terminal *term)
288 while ((line = delpos234(term->scrollback, 0)) != NULL) {
295 * Initialise the terminal.
297 Terminal *term_init(Config *mycfg, void *frontend)
302 * Allocate a new Terminal structure and initialise the fields
305 term = smalloc(sizeof(Terminal));
306 term->frontend = frontend;
309 term->compatibility_level = TM_PUTTY;
310 strcpy(term->id_string, "\033[?6c");
311 term->last_blink = term->last_tblink = 0;
312 term->paste_buffer = NULL;
313 term->last_paste = 0;
314 bufchain_init(&term->inbuf);
315 bufchain_init(&term->printer_buf);
316 term->printing = term->only_printing = FALSE;
317 term->print_job = NULL;
318 term->vt52_mode = FALSE;
319 term->cr_lf_return = FALSE;
320 term->seen_disp_event = FALSE;
321 term->xterm_mouse = term->mouse_is_down = FALSE;
322 term->reset_132 = FALSE;
323 term->blinker = term->tblinker = 0;
325 term->repeat_off = FALSE;
326 term->termstate = TOPLEVEL;
327 term->selstate = NO_SELECTION;
330 term->screen = term->alt_screen = term->scrollback = NULL;
332 term->disptext = term->dispcurs = NULL;
335 term->rows = term->cols = -1;
337 term->beephead = term->beeptail = NULL;
339 term->lastbeep = FALSE;
340 term->beep_overloaded = FALSE;
341 term->attr_mask = 0xffffffff;
342 term->resize_fn = NULL;
343 term->resize_ctx = NULL;
349 * Set up the terminal for a given size.
351 void term_size(Terminal *term, int newrows, int newcols, int newsavelines)
354 unsigned long *newdisp, *line;
357 int save_alt_which = term->alt_which;
359 if (newrows == term->rows && newcols == term->cols &&
360 newsavelines == term->savelines)
361 return; /* nothing to do */
364 swap_screen(term, 0, FALSE, FALSE);
366 term->alt_t = term->marg_t = 0;
367 term->alt_b = term->marg_b = newrows - 1;
369 if (term->rows == -1) {
370 term->scrollback = newtree234(NULL);
371 term->screen = newtree234(NULL);
376 * Resize the screen and scrollback. We only need to shift
377 * lines around within our data structures, because lineptr()
378 * will take care of resizing each individual line if
381 * - If the new screen and the old screen differ in length, we
382 * must shunt some lines in from the scrollback or out to
385 * - If doing that fails to provide us with enough material to
386 * fill the new screen (i.e. the number of rows needed in
387 * the new screen exceeds the total number in the previous
388 * screen+scrollback), we must invent some blank lines to
391 * - Then, if the new scrollback length is less than the
392 * amount of scrollback we actually have, we must throw some
395 sblen = count234(term->scrollback);
396 /* Do this loop to expand the screen if newrows > rows */
397 for (i = term->rows; i < newrows; i++) {
399 line = delpos234(term->scrollback, --sblen);
401 line = smalloc(TSIZE * (newcols + 2));
403 for (j = 0; j <= newcols; j++)
404 line[j + 1] = ERASE_CHAR;
406 addpos234(term->screen, line, 0);
408 /* Do this loop to shrink the screen if newrows < rows */
409 for (i = newrows; i < term->rows; i++) {
410 line = delpos234(term->screen, 0);
411 addpos234(term->scrollback, line, sblen++);
413 assert(count234(term->screen) == newrows);
414 while (sblen > newsavelines) {
415 line = delpos234(term->scrollback, 0);
419 assert(count234(term->scrollback) <= newsavelines);
422 newdisp = smalloc(newrows * (newcols + 1) * TSIZE);
423 for (i = 0; i < newrows * (newcols + 1); i++)
424 newdisp[i] = ATTR_INVALID;
425 sfree(term->disptext);
426 term->disptext = newdisp;
427 term->dispcurs = NULL;
429 newalt = newtree234(NULL);
430 for (i = 0; i < newrows; i++) {
431 line = smalloc(TSIZE * (newcols + 2));
433 for (j = 0; j <= newcols; j++)
434 line[j + 1] = term->erase_char;
435 addpos234(newalt, line, i);
437 if (term->alt_screen) {
438 while (NULL != (line = delpos234(term->alt_screen, 0)))
440 freetree234(term->alt_screen);
442 term->alt_screen = newalt;
444 term->tabs = srealloc(term->tabs, newcols * sizeof(*term->tabs));
447 for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++)
448 term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
452 term->curs.y += newrows - term->rows;
453 if (term->curs.y < 0)
455 if (term->curs.y >= newrows)
456 term->curs.y = newrows - 1;
457 if (term->curs.x >= newcols)
458 term->curs.x = newcols - 1;
459 term->alt_x = term->alt_y = 0;
460 term->wrapnext = term->alt_wnext = FALSE;
462 term->rows = newrows;
463 term->cols = newcols;
464 term->savelines = newsavelines;
467 swap_screen(term, save_alt_which, FALSE, FALSE);
472 term->resize_fn(term->resize_ctx, term->cols, term->rows);
476 * Hand a function and context pointer to the terminal which it can
477 * use to notify a back end of resizes.
479 void term_provide_resize_fn(Terminal *term,
480 void (*resize_fn)(void *, int, int),
483 term->resize_fn = resize_fn;
484 term->resize_ctx = resize_ctx;
485 if (term->cols > 0 && term->rows > 0)
486 resize_fn(resize_ctx, term->cols, term->rows);
490 * Swap screens. If `reset' is TRUE and we have been asked to
491 * switch to the alternate screen, we must bring most of its
492 * configuration from the main screen and erase the contents of the
493 * alternate screen completely. (This is even true if we're already
494 * on it! Blame xterm.)
496 static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos)
502 reset = FALSE; /* do no weird resetting if which==0 */
504 if (which != term->alt_which) {
505 term->alt_which = which;
507 ttr = term->alt_screen;
508 term->alt_screen = term->screen;
511 if (!reset && !keep_cur_pos)
512 term->curs.x = term->alt_x;
515 if (!reset && !keep_cur_pos)
516 term->curs.y = term->alt_y;
519 if (!reset) term->marg_t = term->alt_t;
522 if (!reset) term->marg_b = term->alt_b;
525 if (!reset) term->dec_om = term->alt_om;
528 if (!reset) term->wrap = term->alt_wrap;
531 if (!reset) term->wrapnext = term->alt_wnext;
534 if (!reset) term->insert = term->alt_ins;
537 if (!reset) term->cset = term->alt_cset;
540 if (!reset) term->utf = term->alt_utf;
543 if (!reset) term->sco_acs = term->alt_sco_acs;
544 term->alt_sco_acs = t;
547 if (reset && term->screen) {
549 * Yes, this _is_ supposed to honour background-colour-erase.
551 erase_lots(term, FALSE, TRUE, TRUE);
555 * This might not be possible if we're called during
563 * Update the scroll bar.
565 static void update_sbar(Terminal *term)
569 nscroll = count234(term->scrollback);
571 set_sbar(term->frontend, nscroll + term->rows,
572 nscroll + term->disptop, term->rows);
576 * Check whether the region bounded by the two pointers intersects
577 * the scroll region, and de-select the on-screen selection if so.
579 static void check_selection(Terminal *term, pos from, pos to)
581 if (poslt(from, term->selend) && poslt(term->selstart, to))
586 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
587 * for backward.) `sb' is TRUE if the scrolling is permitted to
588 * affect the scrollback buffer.
590 * NB this function invalidates all pointers into lines of the
591 * screen data structures. In particular, you MUST call fix_cpos
592 * after calling scroll() and before doing anything else that
593 * uses the cpos shortcut pointer.
595 static void scroll(Terminal *term, int topline, int botline, int lines, int sb)
597 unsigned long *line, *line2;
598 int i, seltop, olddisptop, shift;
600 if (topline != 0 || term->alt_which != 0)
603 olddisptop = term->disptop;
607 line = delpos234(term->screen, botline);
608 line = resizeline(line, term->cols);
609 for (i = 0; i < term->cols; i++)
610 line[i + 1] = term->erase_char;
611 line[term->cols + 1] = 0;
612 addpos234(term->screen, line, topline);
614 if (term->selstart.y >= topline && term->selstart.y <= botline) {
616 if (term->selstart.y > botline) {
617 term->selstart.y = botline;
618 term->selstart.x = 0;
621 if (term->selend.y >= topline && term->selend.y <= botline) {
623 if (term->selend.y > botline) {
624 term->selend.y = botline;
633 line = delpos234(term->screen, topline);
634 if (sb && term->savelines > 0) {
635 int sblen = count234(term->scrollback);
637 * We must add this line to the scrollback. We'll
638 * remove a line from the top of the scrollback to
639 * replace it, or allocate a new one if the
640 * scrollback isn't full.
642 if (sblen == term->savelines) {
643 sblen--, line2 = delpos234(term->scrollback, 0);
645 line2 = smalloc(TSIZE * (term->cols + 2));
646 line2[0] = term->cols;
648 addpos234(term->scrollback, line, sblen);
652 * If the user is currently looking at part of the
653 * scrollback, and they haven't enabled any options
654 * that are going to reset the scrollback as a
655 * result of this movement, then the chances are
656 * they'd like to keep looking at the same line. So
657 * we move their viewpoint at the same rate as the
658 * scroll, at least until their viewpoint hits the
659 * top end of the scrollback buffer, at which point
660 * we don't have the choice any more.
662 * Thanks to Jan Holmen Holsten for the idea and
663 * initial implementation.
665 if (term->disptop > -term->savelines && term->disptop < 0)
668 line = resizeline(line, term->cols);
669 for (i = 0; i < term->cols; i++)
670 line[i + 1] = term->erase_char;
671 line[term->cols + 1] = 0;
672 addpos234(term->screen, line, botline);
675 * If the selection endpoints move into the scrollback,
676 * we keep them moving until they hit the top. However,
677 * of course, if the line _hasn't_ moved into the
678 * scrollback then we don't do this, and cut them off
679 * at the top of the scroll region.
681 * This applies to selstart and selend (for an existing
682 * selection), and also selanchor (for one being
683 * selected as we speak).
685 seltop = sb ? -term->savelines : topline;
687 if (term->selstart.y >= seltop &&
688 term->selstart.y <= botline) {
690 if (term->selstart.y < seltop) {
691 term->selstart.y = seltop;
692 term->selstart.x = 0;
695 if (term->selend.y >= seltop && term->selend.y <= botline) {
697 if (term->selend.y < seltop) {
698 term->selend.y = seltop;
702 if (term->selanchor.y >= seltop && term->selanchor.y <= botline) {
704 if (term->selanchor.y < seltop) {
705 term->selanchor.y = seltop;
706 term->selanchor.x = 0;
713 #ifdef OPTIMISE_SCROLL
714 shift += term->disptop - olddisptop;
715 if (shift < term->rows && shift > -term->rows && shift != 0)
716 scroll_display(term, topline, botline, shift);
717 #endif /* OPTIMISE_SCROLL */
720 #ifdef OPTIMISE_SCROLL
722 * Scroll the physical display, and our conception of it in disptext.
724 static void scroll_display(Terminal *term, int topline, int botline, int lines)
726 unsigned long *start, *end;
727 int distance, size, i;
729 start = term->disptext + topline * (term->cols + 1);
730 end = term->disptext + (botline + 1) * (term->cols + 1);
731 distance = (lines > 0 ? lines : -lines) * (term->cols + 1);
732 size = end - start - distance;
734 memmove(start, start + distance, size * TSIZE);
735 if (term->dispcurs >= start + distance &&
736 term->dispcurs <= start + distance + size)
737 term->dispcurs -= distance;
738 for (i = 0; i < distance; i++)
739 (start + size)[i] |= ATTR_INVALID;
741 memmove(start + distance, start, size * TSIZE);
742 if (term->dispcurs >= start && term->dispcurs <= start + size)
743 term->dispcurs += distance;
744 for (i = 0; i < distance; i++)
745 start[i] |= ATTR_INVALID;
747 do_scroll(term->frontend, topline, botline, lines);
749 #endif /* OPTIMISE_SCROLL */
752 * Move the cursor to a given position, clipping at boundaries. We
753 * may or may not want to clip at the scroll margin: marg_clip is 0
754 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
755 * even _being_ outside the margins.
757 static void move(Terminal *term, int x, int y, int marg_clip)
764 if ((term->curs.y >= term->marg_t || marg_clip == 2) &&
767 if ((term->curs.y <= term->marg_b || marg_clip == 2) &&
778 term->wrapnext = FALSE;
782 * Save or restore the cursor and SGR mode.
784 static void save_cursor(Terminal *term, int save)
787 term->savecurs = term->curs;
788 term->save_attr = term->curr_attr;
789 term->save_cset = term->cset;
790 term->save_utf = term->utf;
791 term->save_wnext = term->wrapnext;
792 term->save_csattr = term->cset_attr[term->cset];
793 term->save_sco_acs = term->sco_acs;
795 term->curs = term->savecurs;
796 /* Make sure the window hasn't shrunk since the save */
797 if (term->curs.x >= term->cols)
798 term->curs.x = term->cols - 1;
799 if (term->curs.y >= term->rows)
800 term->curs.y = term->rows - 1;
802 term->curr_attr = term->save_attr;
803 term->cset = term->save_cset;
804 term->utf = term->save_utf;
805 term->wrapnext = term->save_wnext;
807 * wrapnext might reset to False if the x position is no
808 * longer at the rightmost edge.
810 if (term->wrapnext && term->curs.x < term->cols-1)
811 term->wrapnext = FALSE;
812 term->cset_attr[term->cset] = term->save_csattr;
813 term->sco_acs = term->save_sco_acs;
816 term->erase_char = (' ' | ATTR_ASCII |
818 (ATTR_FGMASK | ATTR_BGMASK)));
823 * Erase a large portion of the screen: the whole screen, or the
824 * whole line, or parts thereof.
826 static void erase_lots(Terminal *term,
827 int line_only, int from_begin, int to_end)
831 unsigned long *ldata;
834 start.y = term->curs.y;
836 end.y = term->curs.y + 1;
853 check_selection(term, start, end);
855 /* Clear screen also forces a full window redraw, just in case. */
856 if (start.y == 0 && start.x == 0 && end.y == term->rows)
857 term_invalidate(term);
859 ldata = lineptr(start.y);
860 while (poslt(start, end)) {
861 if (start.x == term->cols && !erase_lattr)
862 ldata[start.x] &= ~LATTR_WRAPPED;
864 ldata[start.x] = term->erase_char;
865 if (incpos(start) && start.y < term->rows)
866 ldata = lineptr(start.y);
871 * Insert or delete characters within the current line. n is +ve if
872 * insertion is desired, and -ve for deletion.
874 static void insch(Terminal *term, int n)
876 int dir = (n < 0 ? -1 : +1);
879 unsigned long *ldata;
881 n = (n < 0 ? -n : n);
882 if (n > term->cols - term->curs.x)
883 n = term->cols - term->curs.x;
884 m = term->cols - term->curs.x - n;
885 cursplus.y = term->curs.y;
886 cursplus.x = term->curs.x + n;
887 check_selection(term, term->curs, cursplus);
888 ldata = lineptr(term->curs.y);
890 memmove(ldata + term->curs.x, ldata + term->curs.x + n, m * TSIZE);
892 ldata[term->curs.x + m++] = term->erase_char;
894 memmove(ldata + term->curs.x + n, ldata + term->curs.x, m * TSIZE);
896 ldata[term->curs.x + n] = term->erase_char;
901 * Toggle terminal mode `mode' to state `state'. (`query' indicates
902 * whether the mode is a DEC private one or a normal one.)
904 static void toggle_mode(Terminal *term, int mode, int query, int state)
910 case 1: /* application cursor keys */
911 term->app_cursor_keys = state;
913 case 2: /* VT52 mode */
914 term->vt52_mode = !state;
915 if (term->vt52_mode) {
916 term->blink_is_real = FALSE;
917 term->vt52_bold = FALSE;
919 term->blink_is_real = term->cfg->blinktext;
922 case 3: /* 80/132 columns */
924 if (!term->cfg->no_remote_resize)
925 request_resize(term->frontend, state ? 132 : 80, term->rows);
926 term->reset_132 = state;
928 case 5: /* reverse video */
930 * Toggle reverse video. If we receive an OFF within the
931 * visual bell timeout period after an ON, we trigger an
932 * effective visual bell, so that ESC[?5hESC[?5l will
933 * always be an actually _visible_ visual bell.
935 ticks = GETTICKCOUNT();
936 /* turn off a previous vbell to avoid inconsistencies */
937 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
938 term->in_vbell = FALSE;
939 if (term->rvideo && !state && /* we're turning it off... */
940 (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/
941 /* If there's no vbell timeout already, or this one lasts
942 * longer, replace vbell_timeout with ours. */
943 if (!term->in_vbell ||
944 (term->rvbell_startpoint - term->vbell_startpoint <
946 term->vbell_startpoint = term->rvbell_startpoint;
947 term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */
948 } else if (!term->rvideo && state) {
949 /* This is an ON, so we notice the time and save it. */
950 term->rvbell_startpoint = ticks;
952 term->rvideo = state;
953 term->seen_disp_event = TRUE;
957 case 6: /* DEC origin mode */
958 term->dec_om = state;
960 case 7: /* auto wrap */
963 case 8: /* auto key repeat */
964 term->repeat_off = !state;
966 case 10: /* set local edit mode */
967 term->term_editing = state;
968 if (term->ldisc) /* cause ldisc to notice changes */
969 ldisc_send(term->ldisc, NULL, 0, 0);
971 case 25: /* enable/disable cursor */
972 compatibility2(OTHER, VT220);
973 term->cursor_on = state;
974 term->seen_disp_event = TRUE;
976 case 47: /* alternate screen */
977 compatibility(OTHER);
979 swap_screen(term, term->cfg->no_alt_screen ? 0 : state, FALSE, FALSE);
982 case 1000: /* xterm mouse 1 */
983 term->xterm_mouse = state ? 1 : 0;
984 set_raw_mouse_mode(term->frontend, state);
986 case 1002: /* xterm mouse 2 */
987 term->xterm_mouse = state ? 2 : 0;
988 set_raw_mouse_mode(term->frontend, state);
990 case 1047: /* alternate screen */
991 compatibility(OTHER);
993 swap_screen(term, term->cfg->no_alt_screen ? 0 : state, TRUE, TRUE);
996 case 1048: /* save/restore cursor */
997 save_cursor(term, state);
998 if (!state) term->seen_disp_event = TRUE;
1000 case 1049: /* cursor & alternate screen */
1002 save_cursor(term, state);
1003 if (!state) term->seen_disp_event = TRUE;
1004 compatibility(OTHER);
1006 swap_screen(term, term->cfg->no_alt_screen ? 0 : state, TRUE, FALSE);
1008 save_cursor(term, state);
1013 case 4: /* set insert mode */
1014 compatibility(VT102);
1015 term->insert = state;
1017 case 12: /* set echo mode */
1018 term->term_echoing = !state;
1019 if (term->ldisc) /* cause ldisc to notice changes */
1020 ldisc_send(term->ldisc, NULL, 0, 0);
1022 case 20: /* Return sends ... */
1023 term->cr_lf_return = state;
1025 case 34: /* Make cursor BIG */
1026 compatibility2(OTHER, VT220);
1027 term->big_cursor = !state;
1032 * Process an OSC sequence: set window title or icon name.
1034 static void do_osc(Terminal *term)
1037 while (term->osc_strlen--)
1038 term->wordness[(unsigned char)
1039 term->osc_string[term->osc_strlen]] = term->esc_args[0];
1041 term->osc_string[term->osc_strlen] = '\0';
1042 switch (term->esc_args[0]) {
1045 if (!term->cfg->no_remote_wintitle)
1046 set_icon(term->frontend, term->osc_string);
1047 if (term->esc_args[0] == 1)
1049 /* fall through: parameter 0 means set both */
1052 if (!term->cfg->no_remote_wintitle)
1053 set_title(term->frontend, term->osc_string);
1060 * ANSI printing routines.
1062 static void term_print_setup(Terminal *term)
1064 bufchain_clear(&term->printer_buf);
1065 term->print_job = printer_start_job(term->cfg->printer);
1067 static void term_print_flush(Terminal *term)
1072 while ((size = bufchain_size(&term->printer_buf)) > 5) {
1073 bufchain_prefix(&term->printer_buf, &data, &len);
1076 printer_job_data(term->print_job, data, len);
1077 bufchain_consume(&term->printer_buf, len);
1080 static void term_print_finish(Terminal *term)
1086 if (!term->printing && !term->only_printing)
1087 return; /* we need do nothing */
1089 term_print_flush(term);
1090 while ((size = bufchain_size(&term->printer_buf)) > 0) {
1091 bufchain_prefix(&term->printer_buf, &data, &len);
1093 if (c == '\033' || c == '\233') {
1094 bufchain_consume(&term->printer_buf, size);
1097 printer_job_data(term->print_job, &c, 1);
1098 bufchain_consume(&term->printer_buf, 1);
1101 printer_finish_job(term->print_job);
1102 term->print_job = NULL;
1103 term->printing = term->only_printing = FALSE;
1107 * Remove everything currently in `inbuf' and stick it up on the
1108 * in-memory display. There's a big state machine in here to
1109 * process escape sequences...
1111 void term_out(Terminal *term)
1114 unsigned char localbuf[256], *chars;
1119 chars = NULL; /* placate compiler warnings */
1120 while (nchars > 0 || bufchain_size(&term->inbuf) > 0) {
1124 bufchain_prefix(&term->inbuf, &ret, &nchars);
1125 if (nchars > sizeof(localbuf))
1126 nchars = sizeof(localbuf);
1127 memcpy(localbuf, ret, nchars);
1128 bufchain_consume(&term->inbuf, nchars);
1130 assert(chars != NULL);
1136 * Optionally log the session traffic to a file. Useful for
1137 * debugging and possibly also useful for actual logging.
1139 if (term->cfg->logtype == LGTYP_DEBUG && term->logctx)
1140 logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG);
1146 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1147 * be able to display 8-bit characters, but I'll let that go 'cause
1152 * If we're printing, add the character to the printer
1155 if (term->printing) {
1156 bufchain_add(&term->printer_buf, &c, 1);
1159 * If we're in print-only mode, we use a much simpler
1160 * state machine designed only to recognise the ESC[4i
1161 * termination sequence.
1163 if (term->only_printing) {
1165 term->print_state = 1;
1166 else if (c == (unsigned char)'\233')
1167 term->print_state = 2;
1168 else if (c == '[' && term->print_state == 1)
1169 term->print_state = 2;
1170 else if (c == '4' && term->print_state == 2)
1171 term->print_state = 3;
1172 else if (c == 'i' && term->print_state == 3)
1173 term->print_state = 4;
1175 term->print_state = 0;
1176 if (term->print_state == 4) {
1177 term_print_finish(term);
1183 /* First see about all those translations. */
1184 if (term->termstate == TOPLEVEL) {
1186 switch (term->utf_state) {
1189 /* UTF-8 must be stateless so we ignore iso2022. */
1190 if (unitab_ctrl[c] != 0xFF)
1192 else c = ((unsigned char)c) | ATTR_ASCII;
1194 } else if ((c & 0xe0) == 0xc0) {
1195 term->utf_size = term->utf_state = 1;
1196 term->utf_char = (c & 0x1f);
1197 } else if ((c & 0xf0) == 0xe0) {
1198 term->utf_size = term->utf_state = 2;
1199 term->utf_char = (c & 0x0f);
1200 } else if ((c & 0xf8) == 0xf0) {
1201 term->utf_size = term->utf_state = 3;
1202 term->utf_char = (c & 0x07);
1203 } else if ((c & 0xfc) == 0xf8) {
1204 term->utf_size = term->utf_state = 4;
1205 term->utf_char = (c & 0x03);
1206 } else if ((c & 0xfe) == 0xfc) {
1207 term->utf_size = term->utf_state = 5;
1208 term->utf_char = (c & 0x01);
1219 if ((c & 0xC0) != 0x80) {
1222 term->utf_state = 0;
1225 term->utf_char = (term->utf_char << 6) | (c & 0x3f);
1226 if (--term->utf_state)
1231 /* Is somebody trying to be evil! */
1233 (c < 0x800 && term->utf_size >= 2) ||
1234 (c < 0x10000 && term->utf_size >= 3) ||
1235 (c < 0x200000 && term->utf_size >= 4) ||
1236 (c < 0x4000000 && term->utf_size >= 5))
1239 /* Unicode line separator and paragraph separator are CR-LF */
1240 if (c == 0x2028 || c == 0x2029)
1243 /* High controls are probably a Baaad idea too. */
1247 /* The UTF-16 surrogates are not nice either. */
1248 /* The standard give the option of decoding these:
1249 * I don't want to! */
1250 if (c >= 0xD800 && c < 0xE000)
1253 /* ISO 10646 characters now limited to UTF-16 range. */
1257 /* This is currently a TagPhobic application.. */
1258 if (c >= 0xE0000 && c <= 0xE007F)
1261 /* U+FEFF is best seen as a null. */
1264 /* But U+FFFE is an error. */
1265 if (c == 0xFFFE || c == 0xFFFF)
1268 /* Oops this is a 16bit implementation */
1273 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1274 else if(term->sco_acs &&
1275 (c!='\033' && c!='\012' && c!='\015' && c!='\b'))
1277 if (term->sco_acs == 2) c |= 0x80;
1280 switch (term->cset_attr[term->cset]) {
1282 * Linedraw characters are different from 'ESC ( B'
1283 * only for a small range. For ones outside that
1284 * range, make sure we use the same font as well as
1285 * the same encoding.
1288 if (unitab_ctrl[c] != 0xFF)
1291 c = ((unsigned char) c) | ATTR_LINEDRW;
1295 /* If UK-ASCII, make the '#' a LineDraw Pound */
1297 c = '}' | ATTR_LINEDRW;
1300 /*FALLTHROUGH*/ case ATTR_ASCII:
1301 if (unitab_ctrl[c] != 0xFF)
1304 c = ((unsigned char) c) | ATTR_ASCII;
1307 if (c>=' ') c = ((unsigned char)c) | ATTR_SCOACS;
1313 /* How about C1 controls ? */
1314 if ((c & -32) == 0x80 && term->termstate < DO_CTRLS &&
1315 !term->vt52_mode && has_compat(VT220)) {
1316 term->termstate = SEEN_ESC;
1317 term->esc_query = FALSE;
1318 c = '@' + (c & 0x1F);
1321 /* Or the GL control. */
1322 if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) {
1323 if (term->curs.x && !term->wrapnext)
1325 term->wrapnext = FALSE;
1327 if (!term->cfg->no_dbackspace) /* destructive bksp might be disabled */
1328 *term->cpos = (' ' | term->curr_attr | ATTR_ASCII);
1330 /* Or normal C0 controls. */
1331 if ((c & -32) == 0 && term->termstate < DO_CTRLS) {
1333 case '\005': /* terminal type query */
1334 /* Strictly speaking this is VT100 but a VT100 defaults to
1335 * no response. Other terminals respond at their option.
1337 * Don't put a CR in the default string as this tends to
1338 * upset some weird software.
1340 * An xterm returns "xterm" (5 characters)
1342 compatibility(ANSIMIN);
1344 char abuf[256], *s, *d;
1346 for (s = term->cfg->answerback, d = abuf; *s; s++) {
1348 if (*s >= 'a' && *s <= 'z')
1349 *d++ = (*s - ('a' - 1));
1350 else if ((*s >= '@' && *s <= '_') ||
1351 *s == '?' || (*s & 0x80))
1356 } else if (*s == '^') {
1361 lpage_send(term->ldisc, DEFAULT_CODEPAGE,
1367 struct beeptime *newbeep;
1368 unsigned long ticks;
1370 ticks = GETTICKCOUNT();
1372 if (!term->beep_overloaded) {
1373 newbeep = smalloc(sizeof(struct beeptime));
1374 newbeep->ticks = ticks;
1375 newbeep->next = NULL;
1376 if (!term->beephead)
1377 term->beephead = newbeep;
1379 term->beeptail->next = newbeep;
1380 term->beeptail = newbeep;
1385 * Throw out any beeps that happened more than
1388 while (term->beephead &&
1389 term->beephead->ticks < ticks - term->cfg->bellovl_t) {
1390 struct beeptime *tmp = term->beephead;
1391 term->beephead = tmp->next;
1393 if (!term->beephead)
1394 term->beeptail = NULL;
1398 if (term->cfg->bellovl && term->beep_overloaded &&
1399 ticks - term->lastbeep >= (unsigned)term->cfg->bellovl_s) {
1401 * If we're currently overloaded and the
1402 * last beep was more than s seconds ago,
1403 * leave overload mode.
1405 term->beep_overloaded = FALSE;
1406 } else if (term->cfg->bellovl && !term->beep_overloaded &&
1407 term->nbeeps >= term->cfg->bellovl_n) {
1409 * Now, if we have n or more beeps
1410 * remaining in the queue, go into overload
1413 term->beep_overloaded = TRUE;
1415 term->lastbeep = ticks;
1418 * Perform an actual beep if we're not overloaded.
1420 if (!term->cfg->bellovl || !term->beep_overloaded) {
1421 beep(term->frontend, term->cfg->beep);
1422 if (term->cfg->beep == BELL_VISUAL) {
1423 term->in_vbell = TRUE;
1424 term->vbell_startpoint = ticks;
1432 if (term->curs.x == 0 &&
1433 (term->curs.y == 0 || term->wrap == 0))
1435 else if (term->curs.x == 0 && term->curs.y > 0)
1436 term->curs.x = term->cols - 1, term->curs.y--;
1437 else if (term->wrapnext)
1438 term->wrapnext = FALSE;
1442 term->seen_disp_event = TRUE;
1445 compatibility(VT100);
1449 compatibility(VT100);
1453 if (term->vt52_mode)
1454 term->termstate = VT52_ESC;
1456 compatibility(ANSIMIN);
1457 term->termstate = SEEN_ESC;
1458 term->esc_query = FALSE;
1463 term->wrapnext = FALSE;
1465 term->seen_disp_event = TRUE;
1466 term->paste_hold = 0;
1468 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1471 if (has_compat(SCOANSI)) {
1472 move(term, 0, 0, 0);
1473 erase_lots(term, FALSE, FALSE, TRUE);
1475 term->wrapnext = FALSE;
1476 term->seen_disp_event = 1;
1480 compatibility(VT100);
1482 if (term->curs.y == term->marg_b)
1483 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1484 else if (term->curs.y < term->rows - 1)
1486 if (term->cfg->lfhascr)
1489 term->wrapnext = FALSE;
1490 term->seen_disp_event = 1;
1491 term->paste_hold = 0;
1493 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1497 pos old_curs = term->curs;
1498 unsigned long *ldata = lineptr(term->curs.y);
1502 } while (term->curs.x < term->cols - 1 &&
1503 !term->tabs[term->curs.x]);
1505 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM) {
1506 if (term->curs.x >= term->cols / 2)
1507 term->curs.x = term->cols / 2 - 1;
1509 if (term->curs.x >= term->cols)
1510 term->curs.x = term->cols - 1;
1514 check_selection(term, old_curs, term->curs);
1516 term->seen_disp_event = TRUE;
1520 switch (term->termstate) {
1522 /* Only graphic characters get this far;
1523 * ctrls are stripped above */
1524 if (term->wrapnext && term->wrap) {
1525 term->cpos[1] |= LATTR_WRAPPED;
1526 if (term->curs.y == term->marg_b)
1527 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1528 else if (term->curs.y < term->rows - 1)
1532 term->wrapnext = FALSE;
1536 if (term->selstate != NO_SELECTION) {
1537 pos cursplus = term->curs;
1539 check_selection(term, term->curs, cursplus);
1541 if (((c & CSET_MASK) == ATTR_ASCII || (c & CSET_MASK) == 0) &&
1543 logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII);
1549 width = wcwidth((wchar_t) c);
1552 *term->cpos++ = c | term->curr_attr;
1553 if (++term->curs.x == term->cols) {
1554 *term->cpos |= LATTR_WRAPPED;
1555 if (term->curs.y == term->marg_b)
1556 scroll(term, term->marg_t, term->marg_b,
1558 else if (term->curs.y < term->rows - 1)
1563 *term->cpos++ = UCSWIDE | term->curr_attr;
1566 *term->cpos++ = c | term->curr_attr;
1573 if (term->curs.x == term->cols) {
1576 term->wrapnext = TRUE;
1577 if (term->wrap && term->vt52_mode) {
1578 term->cpos[1] |= LATTR_WRAPPED;
1579 if (term->curs.y == term->marg_b)
1580 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1581 else if (term->curs.y < term->rows - 1)
1585 term->wrapnext = FALSE;
1588 term->seen_disp_event = 1;
1593 * This state is virtually identical to SEEN_ESC, with the
1594 * exception that we have an OSC sequence in the pipeline,
1595 * and _if_ we see a backslash, we process it.
1599 term->termstate = TOPLEVEL;
1602 /* else fall through */
1604 if (c >= ' ' && c <= '/') {
1605 if (term->esc_query)
1606 term->esc_query = -1;
1608 term->esc_query = c;
1611 term->termstate = TOPLEVEL;
1612 switch (ANSI(c, term->esc_query)) {
1613 case '[': /* enter CSI mode */
1614 term->termstate = SEEN_CSI;
1615 term->esc_nargs = 1;
1616 term->esc_args[0] = ARG_DEFAULT;
1617 term->esc_query = FALSE;
1619 case ']': /* xterm escape sequences */
1620 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1621 compatibility(OTHER);
1622 term->termstate = SEEN_OSC;
1623 term->esc_args[0] = 0;
1625 case '7': /* save cursor */
1626 compatibility(VT100);
1627 save_cursor(term, TRUE);
1629 case '8': /* restore cursor */
1630 compatibility(VT100);
1631 save_cursor(term, FALSE);
1632 term->seen_disp_event = TRUE;
1635 compatibility(VT100);
1636 term->app_keypad_keys = TRUE;
1639 compatibility(VT100);
1640 term->app_keypad_keys = FALSE;
1642 case 'D': /* exactly equivalent to LF */
1643 compatibility(VT100);
1644 if (term->curs.y == term->marg_b)
1645 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1646 else if (term->curs.y < term->rows - 1)
1649 term->wrapnext = FALSE;
1650 term->seen_disp_event = TRUE;
1652 case 'E': /* exactly equivalent to CR-LF */
1653 compatibility(VT100);
1655 if (term->curs.y == term->marg_b)
1656 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1657 else if (term->curs.y < term->rows - 1)
1660 term->wrapnext = FALSE;
1661 term->seen_disp_event = TRUE;
1663 case 'M': /* reverse index - backwards LF */
1664 compatibility(VT100);
1665 if (term->curs.y == term->marg_t)
1666 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
1667 else if (term->curs.y > 0)
1670 term->wrapnext = FALSE;
1671 term->seen_disp_event = TRUE;
1673 case 'Z': /* terminal type query */
1674 compatibility(VT100);
1676 ldisc_send(term->ldisc, term->id_string,
1677 strlen(term->id_string), 0);
1679 case 'c': /* restore power-on settings */
1680 compatibility(VT100);
1682 if (term->ldisc) /* cause ldisc to notice changes */
1683 ldisc_send(term->ldisc, NULL, 0, 0);
1684 if (term->reset_132) {
1685 if (!term->cfg->no_remote_resize)
1686 request_resize(term->frontend, 80, term->rows);
1687 term->reset_132 = 0;
1691 term->seen_disp_event = TRUE;
1693 case 'H': /* set a tab */
1694 compatibility(VT100);
1695 term->tabs[term->curs.x] = TRUE;
1698 case ANSI('8', '#'): /* ESC # 8 fills screen with Es :-) */
1699 compatibility(VT100);
1701 unsigned long *ldata;
1705 for (i = 0; i < term->rows; i++) {
1707 for (j = 0; j < term->cols; j++)
1708 ldata[j] = ATTR_DEFAULT | 'E';
1709 ldata[term->cols] = 0;
1712 term->seen_disp_event = TRUE;
1713 scrtop.x = scrtop.y = 0;
1715 scrbot.y = term->rows;
1716 check_selection(term, scrtop, scrbot);
1720 case ANSI('3', '#'):
1721 case ANSI('4', '#'):
1722 case ANSI('5', '#'):
1723 case ANSI('6', '#'):
1724 compatibility(VT100);
1726 unsigned long nlattr;
1727 unsigned long *ldata;
1728 switch (ANSI(c, term->esc_query)) {
1729 case ANSI('3', '#'):
1732 case ANSI('4', '#'):
1735 case ANSI('5', '#'):
1736 nlattr = LATTR_NORM;
1738 default: /* spiritually case ANSI('6', '#'): */
1739 nlattr = LATTR_WIDE;
1742 ldata = lineptr(term->curs.y);
1743 ldata[term->cols] &= ~LATTR_MODE;
1744 ldata[term->cols] |= nlattr;
1748 case ANSI('A', '('):
1749 compatibility(VT100);
1750 if (!term->cfg->no_remote_charset)
1751 term->cset_attr[0] = ATTR_GBCHR;
1753 case ANSI('B', '('):
1754 compatibility(VT100);
1755 if (!term->cfg->no_remote_charset)
1756 term->cset_attr[0] = ATTR_ASCII;
1758 case ANSI('0', '('):
1759 compatibility(VT100);
1760 if (!term->cfg->no_remote_charset)
1761 term->cset_attr[0] = ATTR_LINEDRW;
1763 case ANSI('U', '('):
1764 compatibility(OTHER);
1765 if (!term->cfg->no_remote_charset)
1766 term->cset_attr[0] = ATTR_SCOACS;
1769 case ANSI('A', ')'):
1770 compatibility(VT100);
1771 if (!term->cfg->no_remote_charset)
1772 term->cset_attr[1] = ATTR_GBCHR;
1774 case ANSI('B', ')'):
1775 compatibility(VT100);
1776 if (!term->cfg->no_remote_charset)
1777 term->cset_attr[1] = ATTR_ASCII;
1779 case ANSI('0', ')'):
1780 compatibility(VT100);
1781 if (!term->cfg->no_remote_charset)
1782 term->cset_attr[1] = ATTR_LINEDRW;
1784 case ANSI('U', ')'):
1785 compatibility(OTHER);
1786 if (!term->cfg->no_remote_charset)
1787 term->cset_attr[1] = ATTR_SCOACS;
1790 case ANSI('8', '%'): /* Old Linux code */
1791 case ANSI('G', '%'):
1792 compatibility(OTHER);
1793 if (!term->cfg->no_remote_charset)
1796 case ANSI('@', '%'):
1797 compatibility(OTHER);
1798 if (!term->cfg->no_remote_charset)
1804 term->termstate = TOPLEVEL; /* default */
1806 if (term->esc_nargs <= ARGS_MAX) {
1807 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
1808 term->esc_args[term->esc_nargs - 1] = 0;
1809 term->esc_args[term->esc_nargs - 1] =
1810 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
1812 term->termstate = SEEN_CSI;
1813 } else if (c == ';') {
1814 if (++term->esc_nargs <= ARGS_MAX)
1815 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
1816 term->termstate = SEEN_CSI;
1817 } else if (c < '@') {
1818 if (term->esc_query)
1819 term->esc_query = -1;
1821 term->esc_query = TRUE;
1823 term->esc_query = c;
1824 term->termstate = SEEN_CSI;
1826 switch (ANSI(c, term->esc_query)) {
1827 case 'A': /* move up N lines */
1828 move(term, term->curs.x,
1829 term->curs.y - def(term->esc_args[0], 1), 1);
1830 term->seen_disp_event = TRUE;
1832 case 'e': /* move down N lines */
1833 compatibility(ANSI);
1836 move(term, term->curs.x,
1837 term->curs.y + def(term->esc_args[0], 1), 1);
1838 term->seen_disp_event = TRUE;
1840 case ANSI('c', '>'): /* report xterm version */
1841 compatibility(OTHER);
1842 /* this reports xterm version 136 so that VIM can
1843 use the drag messages from the mouse reporting */
1845 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
1847 case 'a': /* move right N cols */
1848 compatibility(ANSI);
1851 move(term, term->curs.x + def(term->esc_args[0], 1),
1853 term->seen_disp_event = TRUE;
1855 case 'D': /* move left N cols */
1856 move(term, term->curs.x - def(term->esc_args[0], 1),
1858 term->seen_disp_event = TRUE;
1860 case 'E': /* move down N lines and CR */
1861 compatibility(ANSI);
1863 term->curs.y + def(term->esc_args[0], 1), 1);
1864 term->seen_disp_event = TRUE;
1866 case 'F': /* move up N lines and CR */
1867 compatibility(ANSI);
1869 term->curs.y - def(term->esc_args[0], 1), 1);
1870 term->seen_disp_event = TRUE;
1873 case '`': /* set horizontal posn */
1874 compatibility(ANSI);
1875 move(term, def(term->esc_args[0], 1) - 1,
1877 term->seen_disp_event = TRUE;
1879 case 'd': /* set vertical posn */
1880 compatibility(ANSI);
1881 move(term, term->curs.x,
1882 ((term->dec_om ? term->marg_t : 0) +
1883 def(term->esc_args[0], 1) - 1),
1884 (term->dec_om ? 2 : 0));
1885 term->seen_disp_event = TRUE;
1888 case 'f': /* set horz and vert posns at once */
1889 if (term->esc_nargs < 2)
1890 term->esc_args[1] = ARG_DEFAULT;
1891 move(term, def(term->esc_args[1], 1) - 1,
1892 ((term->dec_om ? term->marg_t : 0) +
1893 def(term->esc_args[0], 1) - 1),
1894 (term->dec_om ? 2 : 0));
1895 term->seen_disp_event = TRUE;
1897 case 'J': /* erase screen or parts of it */
1899 unsigned int i = def(term->esc_args[0], 0) + 1;
1902 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
1905 term->seen_disp_event = TRUE;
1907 case 'K': /* erase line or parts of it */
1909 unsigned int i = def(term->esc_args[0], 0) + 1;
1912 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
1914 term->seen_disp_event = TRUE;
1916 case 'L': /* insert lines */
1917 compatibility(VT102);
1918 if (term->curs.y <= term->marg_b)
1919 scroll(term, term->curs.y, term->marg_b,
1920 -def(term->esc_args[0], 1), FALSE);
1922 term->seen_disp_event = TRUE;
1924 case 'M': /* delete lines */
1925 compatibility(VT102);
1926 if (term->curs.y <= term->marg_b)
1927 scroll(term, term->curs.y, term->marg_b,
1928 def(term->esc_args[0], 1),
1931 term->seen_disp_event = TRUE;
1933 case '@': /* insert chars */
1934 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1935 compatibility(VT102);
1936 insch(term, def(term->esc_args[0], 1));
1937 term->seen_disp_event = TRUE;
1939 case 'P': /* delete chars */
1940 compatibility(VT102);
1941 insch(term, -def(term->esc_args[0], 1));
1942 term->seen_disp_event = TRUE;
1944 case 'c': /* terminal type query */
1945 compatibility(VT100);
1946 /* This is the response for a VT102 */
1948 ldisc_send(term->ldisc, term->id_string,
1949 strlen(term->id_string), 0);
1951 case 'n': /* cursor position query */
1953 if (term->esc_args[0] == 6) {
1955 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
1957 ldisc_send(term->ldisc, buf, strlen(buf), 0);
1958 } else if (term->esc_args[0] == 5) {
1959 ldisc_send(term->ldisc, "\033[0n", 4, 0);
1963 case 'h': /* toggle modes to high */
1965 compatibility(VT100);
1968 for (i = 0; i < term->esc_nargs; i++)
1969 toggle_mode(term, term->esc_args[i],
1970 term->esc_query, TRUE);
1975 compatibility(VT100);
1977 if (term->esc_nargs != 1) break;
1978 if (term->esc_args[0] == 5 && *term->cfg->printer) {
1979 term->printing = TRUE;
1980 term->only_printing = !term->esc_query;
1981 term->print_state = 0;
1982 term_print_setup(term);
1983 } else if (term->esc_args[0] == 4 &&
1985 term_print_finish(term);
1989 case 'l': /* toggle modes to low */
1991 compatibility(VT100);
1994 for (i = 0; i < term->esc_nargs; i++)
1995 toggle_mode(term, term->esc_args[i],
1996 term->esc_query, FALSE);
1999 case 'g': /* clear tabs */
2000 compatibility(VT100);
2001 if (term->esc_nargs == 1) {
2002 if (term->esc_args[0] == 0) {
2003 term->tabs[term->curs.x] = FALSE;
2004 } else if (term->esc_args[0] == 3) {
2006 for (i = 0; i < term->cols; i++)
2007 term->tabs[i] = FALSE;
2011 case 'r': /* set scroll margins */
2012 compatibility(VT100);
2013 if (term->esc_nargs <= 2) {
2015 top = def(term->esc_args[0], 1) - 1;
2016 bot = (term->esc_nargs <= 1
2017 || term->esc_args[1] == 0 ?
2019 def(term->esc_args[1], term->rows)) - 1;
2020 if (bot >= term->rows)
2021 bot = term->rows - 1;
2022 /* VTTEST Bug 9 - if region is less than 2 lines
2023 * don't change region.
2025 if (bot - top > 0) {
2030 * I used to think the cursor should be
2031 * placed at the top of the newly marginned
2032 * area. Apparently not: VMS TPU falls over
2035 * Well actually it should for
2038 term->curs.y = (term->dec_om ?
2041 term->seen_disp_event = TRUE;
2045 case 'm': /* set graphics rendition */
2048 * A VT100 without the AVO only had one
2049 * attribute, either underline or
2050 * reverse video depending on the
2051 * cursor type, this was selected by
2055 * This is sometimes DIM, eg on the
2058 * This is sometimes INVIS various ANSI.
2060 * This like 22 disables BOLD, DIM and INVIS
2062 * The ANSI colours appear on any
2063 * terminal that has colour (obviously)
2064 * but the interaction between sgr0 and
2065 * the colours varies but is usually
2066 * related to the background colour
2067 * erase item. The interaction between
2068 * colour attributes and the mono ones
2069 * is also very implementation
2072 * The 39 and 49 attributes are likely
2073 * to be unimplemented.
2076 for (i = 0; i < term->esc_nargs; i++) {
2077 switch (def(term->esc_args[i], 0)) {
2078 case 0: /* restore defaults */
2079 term->curr_attr = ATTR_DEFAULT;
2081 case 1: /* enable bold */
2082 compatibility(VT100AVO);
2083 term->curr_attr |= ATTR_BOLD;
2085 case 21: /* (enable double underline) */
2086 compatibility(OTHER);
2087 case 4: /* enable underline */
2088 compatibility(VT100AVO);
2089 term->curr_attr |= ATTR_UNDER;
2091 case 5: /* enable blink */
2092 compatibility(VT100AVO);
2093 term->curr_attr |= ATTR_BLINK;
2095 case 7: /* enable reverse video */
2096 term->curr_attr |= ATTR_REVERSE;
2098 case 10: /* SCO acs off */
2099 compatibility(SCOANSI);
2100 if (term->cfg->no_remote_charset) break;
2101 term->sco_acs = 0; break;
2102 case 11: /* SCO acs on */
2103 compatibility(SCOANSI);
2104 if (term->cfg->no_remote_charset) break;
2105 term->sco_acs = 1; break;
2106 case 12: /* SCO acs on, |0x80 */
2107 compatibility(SCOANSI);
2108 if (term->cfg->no_remote_charset) break;
2109 term->sco_acs = 2; break;
2110 case 22: /* disable bold */
2111 compatibility2(OTHER, VT220);
2112 term->curr_attr &= ~ATTR_BOLD;
2114 case 24: /* disable underline */
2115 compatibility2(OTHER, VT220);
2116 term->curr_attr &= ~ATTR_UNDER;
2118 case 25: /* disable blink */
2119 compatibility2(OTHER, VT220);
2120 term->curr_attr &= ~ATTR_BLINK;
2122 case 27: /* disable reverse video */
2123 compatibility2(OTHER, VT220);
2124 term->curr_attr &= ~ATTR_REVERSE;
2135 term->curr_attr &= ~ATTR_FGMASK;
2137 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
2139 case 39: /* default-foreground */
2140 term->curr_attr &= ~ATTR_FGMASK;
2141 term->curr_attr |= ATTR_DEFFG;
2152 term->curr_attr &= ~ATTR_BGMASK;
2154 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
2156 case 49: /* default-background */
2157 term->curr_attr &= ~ATTR_BGMASK;
2158 term->curr_attr |= ATTR_DEFBG;
2163 term->erase_char = (' ' | ATTR_ASCII |
2169 case 's': /* save cursor */
2170 save_cursor(term, TRUE);
2172 case 'u': /* restore cursor */
2173 save_cursor(term, FALSE);
2174 term->seen_disp_event = TRUE;
2176 case 't': /* set page size - ie window height */
2178 * VT340/VT420 sequence DECSLPP, DEC only allows values
2179 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2180 * illegal values (eg first arg 1..9) for window changing
2183 if (term->esc_nargs <= 1
2184 && (term->esc_args[0] < 1 ||
2185 term->esc_args[0] >= 24)) {
2186 compatibility(VT340TEXT);
2187 if (!term->cfg->no_remote_resize)
2188 request_resize(term->frontend, term->cols,
2189 def(term->esc_args[0], 24));
2191 } else if (term->esc_nargs >= 1 &&
2192 term->esc_args[0] >= 1 &&
2193 term->esc_args[0] < 24) {
2194 compatibility(OTHER);
2196 switch (term->esc_args[0]) {
2200 set_iconic(term->frontend, FALSE);
2203 set_iconic(term->frontend, TRUE);
2206 if (term->esc_nargs >= 3) {
2207 if (!term->cfg->no_remote_resize)
2208 move_window(term->frontend,
2209 def(term->esc_args[1], 0),
2210 def(term->esc_args[2], 0));
2214 /* We should resize the window to a given
2215 * size in pixels here, but currently our
2216 * resizing code isn't healthy enough to
2221 set_zorder(term->frontend, TRUE);
2224 /* move to bottom */
2225 set_zorder(term->frontend, FALSE);
2228 refresh_window(term->frontend);
2231 if (term->esc_nargs >= 3) {
2232 if (!term->cfg->no_remote_resize)
2233 request_resize(term->frontend,
2234 def(term->esc_args[2], term->cfg->width),
2235 def(term->esc_args[1], term->cfg->height));
2239 if (term->esc_nargs >= 2)
2240 set_zoomed(term->frontend,
2246 ldisc_send(term->ldisc,
2247 is_iconic(term->frontend) ?
2248 "\033[1t" : "\033[2t", 4, 0);
2252 get_window_pos(term->frontend, &x, &y);
2253 len = sprintf(buf, "\033[3;%d;%dt", x, y);
2254 ldisc_send(term->ldisc, buf, len, 0);
2259 get_window_pixels(term->frontend, &x, &y);
2260 len = sprintf(buf, "\033[4;%d;%dt", x, y);
2261 ldisc_send(term->ldisc, buf, len, 0);
2266 len = sprintf(buf, "\033[8;%d;%dt",
2267 term->rows, term->cols);
2268 ldisc_send(term->ldisc, buf, len, 0);
2273 * Hmmm. Strictly speaking we
2274 * should return `the size of the
2275 * screen in characters', but
2276 * that's not easy: (a) window
2277 * furniture being what it is it's
2278 * hard to compute, and (b) in
2279 * resize-font mode maximising the
2280 * window wouldn't change the
2281 * number of characters. *shrug*. I
2282 * think we'll ignore it for the
2283 * moment and see if anyone
2284 * complains, and then ask them
2285 * what they would like it to do.
2290 p = get_window_title(term->frontend, TRUE);
2292 ldisc_send(term->ldisc, "\033]L", 3, 0);
2293 ldisc_send(term->ldisc, p, len, 0);
2294 ldisc_send(term->ldisc, "\033\\", 2, 0);
2299 p = get_window_title(term->frontend,FALSE);
2301 ldisc_send(term->ldisc, "\033]l", 3, 0);
2302 ldisc_send(term->ldisc, p, len, 0);
2303 ldisc_send(term->ldisc, "\033\\", 2, 0);
2310 compatibility(SCOANSI);
2311 scroll(term, term->marg_t, term->marg_b,
2312 def(term->esc_args[0], 1), TRUE);
2314 term->wrapnext = FALSE;
2315 term->seen_disp_event = TRUE;
2318 compatibility(SCOANSI);
2319 scroll(term, term->marg_t, term->marg_b,
2320 -def(term->esc_args[0], 1), TRUE);
2322 term->wrapnext = FALSE;
2323 term->seen_disp_event = TRUE;
2325 case ANSI('|', '*'):
2326 /* VT420 sequence DECSNLS
2327 * Set number of lines on screen
2328 * VT420 uses VGA like hardware and can support any size in
2329 * reasonable range (24..49 AIUI) with no default specified.
2331 compatibility(VT420);
2332 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
2333 if (!term->cfg->no_remote_resize)
2334 request_resize(term->frontend, term->cols,
2335 def(term->esc_args[0],
2336 term->cfg->height));
2340 case ANSI('|', '$'):
2341 /* VT340/VT420 sequence DECSCPP
2342 * Set number of columns per page
2343 * Docs imply range is only 80 or 132, but I'll allow any.
2345 compatibility(VT340TEXT);
2346 if (term->esc_nargs <= 1) {
2347 if (!term->cfg->no_remote_resize)
2348 request_resize(term->frontend,
2349 def(term->esc_args[0],
2350 term->cfg->width), term->rows);
2354 case 'X': /* write N spaces w/o moving cursor */
2355 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
2356 compatibility(ANSIMIN);
2358 int n = def(term->esc_args[0], 1);
2360 unsigned long *p = term->cpos;
2361 if (n > term->cols - term->curs.x)
2362 n = term->cols - term->curs.x;
2363 cursplus = term->curs;
2365 check_selection(term, term->curs, cursplus);
2367 *p++ = term->erase_char;
2368 term->seen_disp_event = TRUE;
2371 case 'x': /* report terminal characteristics */
2372 compatibility(VT100);
2375 int i = def(term->esc_args[0], 0);
2376 if (i == 0 || i == 1) {
2377 strcpy(buf, "\033[2;1;1;112;112;1;0x");
2379 ldisc_send(term->ldisc, buf, 20, 0);
2383 case 'Z': /* BackTab for xterm */
2384 compatibility(OTHER);
2386 int i = def(term->esc_args[0], 1);
2387 pos old_curs = term->curs;
2389 for(;i>0 && term->curs.x>0; i--) {
2392 } while (term->curs.x >0 &&
2393 !term->tabs[term->curs.x]);
2396 check_selection(term, old_curs, term->curs);
2399 case ANSI('L', '='):
2400 compatibility(OTHER);
2401 term->use_bce = (term->esc_args[0] <= 0);
2402 term->erase_char = ERASE_CHAR;
2404 term->erase_char = (' ' | ATTR_ASCII |
2406 (ATTR_FGMASK | ATTR_BGMASK)));
2408 case ANSI('E', '='):
2409 compatibility(OTHER);
2410 term->blink_is_real = (term->esc_args[0] >= 1);
2412 case ANSI('p', '"'):
2414 * Allow the host to make this emulator a
2415 * 'perfect' VT102. This first appeared in
2416 * the VT220, but we do need to get back to
2417 * PuTTY mode so I won't check it.
2419 * The arg in 40..42,50 are a PuTTY extension.
2420 * The 2nd arg, 8bit vs 7bit is not checked.
2422 * Setting VT102 mode should also change
2423 * the Fkeys to generate PF* codes as a
2424 * real VT102 has no Fkeys. The VT220 does
2425 * this, F11..F13 become ESC,BS,LF other
2426 * Fkeys send nothing.
2428 * Note ESC c will NOT change this!
2431 switch (term->esc_args[0]) {
2433 term->compatibility_level &= ~TM_VTXXX;
2434 term->compatibility_level |= TM_VT102;
2437 term->compatibility_level &= ~TM_VTXXX;
2438 term->compatibility_level |= TM_VT220;
2442 if (term->esc_args[0] > 60 &&
2443 term->esc_args[0] < 70)
2444 term->compatibility_level |= TM_VTXXX;
2448 term->compatibility_level &= TM_VTXXX;
2451 term->compatibility_level = TM_PUTTY;
2454 term->compatibility_level = TM_SCOANSI;
2458 term->compatibility_level = TM_PUTTY;
2464 /* Change the response to CSI c */
2465 if (term->esc_args[0] == 50) {
2468 strcpy(term->id_string, "\033[?");
2469 for (i = 1; i < term->esc_nargs; i++) {
2471 strcat(term->id_string, ";");
2472 sprintf(lbuf, "%d", term->esc_args[i]);
2473 strcat(term->id_string, lbuf);
2475 strcat(term->id_string, "c");
2478 /* Is this a good idea ?
2479 * Well we should do a soft reset at this point ...
2481 if (!has_compat(VT420) && has_compat(VT100)) {
2482 if (!term->cfg->no_remote_resize) {
2483 if (term->reset_132)
2484 request_resize(132, 24);
2486 request_resize(80, 24);
2494 term->osc_w = FALSE;
2496 case 'P': /* Linux palette sequence */
2497 term->termstate = SEEN_OSC_P;
2498 term->osc_strlen = 0;
2500 case 'R': /* Linux palette reset */
2501 palette_reset(term->frontend);
2502 term_invalidate(term);
2503 term->termstate = TOPLEVEL;
2505 case 'W': /* word-set */
2506 term->termstate = SEEN_OSC_W;
2519 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2523 * Grotty hack to support xterm and DECterm title
2524 * sequences concurrently.
2526 if (term->esc_args[0] == 2) {
2527 term->esc_args[0] = 1;
2530 /* else fall through */
2532 term->termstate = OSC_STRING;
2533 term->osc_strlen = 0;
2538 * This OSC stuff is EVIL. It takes just one character to get into
2539 * sysline mode and it's not initially obvious how to get out.
2540 * So I've added CR and LF as string aborts.
2541 * This shouldn't effect compatibility as I believe embedded
2542 * control characters are supposed to be interpreted (maybe?)
2543 * and they don't display anything useful anyway.
2547 if (c == '\012' || c == '\015') {
2548 term->termstate = TOPLEVEL;
2549 } else if (c == 0234 || c == '\007') {
2551 * These characters terminate the string; ST and BEL
2552 * terminate the sequence and trigger instant
2553 * processing of it, whereas ESC goes back to SEEN_ESC
2554 * mode unless it is followed by \, in which case it is
2555 * synonymous with ST in the first place.
2558 term->termstate = TOPLEVEL;
2559 } else if (c == '\033')
2560 term->termstate = OSC_MAYBE_ST;
2561 else if (term->osc_strlen < OSC_STR_MAX)
2562 term->osc_string[term->osc_strlen++] = c;
2566 int max = (term->osc_strlen == 0 ? 21 : 16);
2568 if (c >= '0' && c <= '9')
2570 else if (c >= 'A' && c <= 'A' + max - 10)
2572 else if (c >= 'a' && c <= 'a' + max - 10)
2575 term->termstate = TOPLEVEL;
2578 term->osc_string[term->osc_strlen++] = val;
2579 if (term->osc_strlen >= 7) {
2580 palette_set(term->frontend, term->osc_string[0],
2581 term->osc_string[1] * 16 + term->osc_string[2],
2582 term->osc_string[3] * 16 + term->osc_string[4],
2583 term->osc_string[5] * 16 + term->osc_string[6]);
2584 term_invalidate(term);
2585 term->termstate = TOPLEVEL;
2601 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2604 term->termstate = OSC_STRING;
2605 term->osc_strlen = 0;
2609 term->termstate = TOPLEVEL;
2610 term->seen_disp_event = TRUE;
2613 move(term, term->curs.x, term->curs.y - 1, 1);
2616 move(term, term->curs.x, term->curs.y + 1, 1);
2619 move(term, term->curs.x + 1, term->curs.y, 1);
2622 move(term, term->curs.x - 1, term->curs.y, 1);
2625 * From the VT100 Manual
2626 * NOTE: The special graphics characters in the VT100
2627 * are different from those in the VT52
2629 * From VT102 manual:
2630 * 137 _ Blank - Same
2631 * 140 ` Reserved - Humm.
2632 * 141 a Solid rectangle - Similar
2633 * 142 b 1/ - Top half of fraction for the
2634 * 143 c 3/ - subscript numbers below.
2637 * 146 f Degrees - Same
2638 * 147 g Plus or minus - Same
2640 * 151 i Ellipsis (dots)
2643 * 154 l Bar at scan 0
2644 * 155 m Bar at scan 1
2645 * 156 n Bar at scan 2
2646 * 157 o Bar at scan 3 - Similar
2647 * 160 p Bar at scan 4 - Similar
2648 * 161 q Bar at scan 5 - Similar
2649 * 162 r Bar at scan 6 - Same
2650 * 163 s Bar at scan 7 - Similar
2665 term->cset_attr[term->cset = 0] = ATTR_LINEDRW;
2668 term->cset_attr[term->cset = 0] = ATTR_ASCII;
2671 move(term, 0, 0, 0);
2674 if (term->curs.y == 0)
2675 scroll(term, 0, term->rows - 1, -1, TRUE);
2676 else if (term->curs.y > 0)
2679 term->wrapnext = FALSE;
2682 erase_lots(term, FALSE, FALSE, TRUE);
2686 erase_lots(term, TRUE, FALSE, TRUE);
2690 /* XXX Print cursor line */
2693 /* XXX Start controller mode */
2696 /* XXX Stop controller mode */
2700 term->termstate = VT52_Y1;
2704 ldisc_send(term->ldisc, "\033/Z", 3, 0);
2707 term->app_keypad_keys = TRUE;
2710 term->app_keypad_keys = FALSE;
2713 /* XXX This should switch to VT100 mode not current or default
2714 * VT mode. But this will only have effect in a VT220+
2717 term->vt52_mode = FALSE;
2718 term->blink_is_real = term->cfg->blinktext;
2722 /* XXX Enter auto print mode */
2725 /* XXX Exit auto print mode */
2728 /* XXX Print screen */
2734 /* compatibility(ATARI) */
2735 move(term, 0, 0, 0);
2736 erase_lots(term, FALSE, FALSE, TRUE);
2740 /* compatibility(ATARI) */
2741 if (term->curs.y <= term->marg_b)
2742 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
2745 /* compatibility(ATARI) */
2746 if (term->curs.y <= term->marg_b)
2747 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
2750 /* compatibility(ATARI) */
2751 term->termstate = VT52_FG;
2754 /* compatibility(ATARI) */
2755 term->termstate = VT52_BG;
2758 /* compatibility(ATARI) */
2759 erase_lots(term, FALSE, TRUE, FALSE);
2763 /* compatibility(ATARI) */
2764 term->cursor_on = TRUE;
2767 /* compatibility(ATARI) */
2768 term->cursor_on = FALSE;
2770 /* case 'j': Save cursor position - broken on ST */
2771 /* case 'k': Restore cursor position */
2773 /* compatibility(ATARI) */
2774 erase_lots(term, TRUE, TRUE, TRUE);
2776 term->wrapnext = FALSE;
2780 /* compatibility(ATARI) */
2781 erase_lots(term, TRUE, TRUE, FALSE);
2784 /* compatibility(ATARI) */
2785 term->curr_attr |= ATTR_REVERSE;
2788 /* compatibility(ATARI) */
2789 term->curr_attr &= ~ATTR_REVERSE;
2791 case 'v': /* wrap Autowrap on - Wyse style */
2792 /* compatibility(ATARI) */
2795 case 'w': /* Autowrap off */
2796 /* compatibility(ATARI) */
2801 /* compatibility(OTHER) */
2802 term->vt52_bold = FALSE;
2803 term->curr_attr = ATTR_DEFAULT;
2805 term->erase_char = (' ' | ATTR_ASCII |
2807 (ATTR_FGMASK | ATTR_BGMASK)));
2810 /* compatibility(VI50) */
2811 term->curr_attr |= ATTR_UNDER;
2814 /* compatibility(VI50) */
2815 term->curr_attr &= ~ATTR_UNDER;
2818 /* compatibility(VI50) */
2819 term->vt52_bold = TRUE;
2820 term->curr_attr |= ATTR_BOLD;
2823 /* compatibility(VI50) */
2824 term->vt52_bold = FALSE;
2825 term->curr_attr &= ~ATTR_BOLD;
2831 term->termstate = VT52_Y2;
2832 move(term, term->curs.x, c - ' ', 0);
2835 term->termstate = TOPLEVEL;
2836 move(term, c - ' ', term->curs.y, 0);
2841 term->termstate = TOPLEVEL;
2842 term->curr_attr &= ~ATTR_FGMASK;
2843 term->curr_attr &= ~ATTR_BOLD;
2844 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
2845 if ((c & 0x8) || term->vt52_bold)
2846 term->curr_attr |= ATTR_BOLD;
2849 term->erase_char = (' ' | ATTR_ASCII |
2851 (ATTR_FGMASK | ATTR_BGMASK)));
2854 term->termstate = TOPLEVEL;
2855 term->curr_attr &= ~ATTR_BGMASK;
2856 term->curr_attr &= ~ATTR_BLINK;
2857 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
2859 /* Note: bold background */
2861 term->curr_attr |= ATTR_BLINK;
2864 term->erase_char = (' ' | ATTR_ASCII |
2866 (ATTR_FGMASK | ATTR_BGMASK)));
2869 default: break; /* placate gcc warning about enum use */
2871 if (term->selstate != NO_SELECTION) {
2872 pos cursplus = term->curs;
2874 check_selection(term, term->curs, cursplus);
2878 term_print_flush(term);
2883 * Compare two lines to determine whether they are sufficiently
2884 * alike to scroll-optimise one to the other. Return the degree of
2887 static int linecmp(Terminal *term, unsigned long *a, unsigned long *b)
2891 for (i = n = 0; i < term->cols; i++)
2892 n += (*a++ == *b++);
2898 * Given a context, update the window. Out of paranoia, we don't
2899 * allow WM_PAINT responses to do scrolling optimisations.
2901 static void do_paint(Terminal *term, Context ctx, int may_optimise)
2903 int i, j, our_curs_y;
2904 unsigned long rv, cursor;
2907 long cursor_background = ERASE_CHAR;
2908 unsigned long ticks;
2911 * Check the visual bell state.
2913 if (term->in_vbell) {
2914 ticks = GETTICKCOUNT();
2915 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
2916 term->in_vbell = FALSE;
2919 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
2922 * screen array, disptop, scrtop,
2924 * cfg.blinkpc, blink_is_real, tblinker,
2925 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
2928 /* Has the cursor position or type changed ? */
2929 if (term->cursor_on) {
2930 if (term->has_focus) {
2931 if (term->blinker || !term->cfg->blink_cur)
2932 cursor = TATTR_ACTCURS;
2936 cursor = TATTR_PASCURS;
2938 cursor |= TATTR_RIGHTCURS;
2941 our_curs_y = term->curs.y - term->disptop;
2943 if (term->dispcurs && (term->curstype != cursor ||
2945 term->disptext + our_curs_y * (term->cols + 1) +
2947 if (term->dispcurs > term->disptext &&
2948 (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
2949 term->dispcurs[-1] |= ATTR_INVALID;
2950 if ( (term->dispcurs[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
2951 term->dispcurs[1] |= ATTR_INVALID;
2952 *term->dispcurs |= ATTR_INVALID;
2955 term->dispcurs = NULL;
2957 /* The normal screen data */
2958 for (i = 0; i < term->rows; i++) {
2959 unsigned long *ldata;
2961 int idx, dirty_line, dirty_run, selected;
2962 unsigned long attr = 0;
2963 int updated_line = 0;
2966 int last_run_dirty = 0;
2968 scrpos.y = i + term->disptop;
2969 ldata = lineptr(scrpos.y);
2970 lattr = (ldata[term->cols] & LATTR_MODE);
2972 idx = i * (term->cols + 1);
2973 dirty_run = dirty_line = (ldata[term->cols] !=
2974 term->disptext[idx + term->cols]);
2975 term->disptext[idx + term->cols] = ldata[term->cols];
2977 for (j = 0; j < term->cols; j++, idx++) {
2978 unsigned long tattr, tchar;
2979 unsigned long *d = ldata + j;
2983 tchar = (*d & (CHAR_MASK | CSET_MASK));
2984 tattr = (*d & (ATTR_MASK ^ CSET_MASK));
2985 switch (tchar & CSET_MASK) {
2987 tchar = unitab_line[tchar & 0xFF];
2990 tchar = unitab_xterm[tchar & 0xFF];
2993 tchar = unitab_scoacs[tchar&0xFF];
2996 tattr |= (tchar & CSET_MASK);
2998 if ((d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3001 /* Video reversing things */
3002 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
3003 if (term->seltype == LEXICOGRAPHIC)
3004 selected = (posle(term->selstart, scrpos) &&
3005 poslt(scrpos, term->selend));
3007 selected = (posPle(term->selstart, scrpos) &&
3008 posPlt(scrpos, term->selend));
3012 ^ (selected ? ATTR_REVERSE : 0));
3014 /* 'Real' blinking ? */
3015 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
3016 if (term->has_focus && term->tblinker) {
3018 tattr &= ~CSET_MASK;
3021 tattr &= ~ATTR_BLINK;
3025 * Check the font we'll _probably_ be using to see if
3026 * the character is wide when we don't want it to be.
3028 if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) {
3029 if ((tattr & ATTR_WIDE) == 0 &&
3030 char_width(ctx, (tchar | tattr) & 0xFFFF) == 2)
3031 tattr |= ATTR_NARROW;
3032 } else if (term->disptext[idx]&ATTR_NARROW)
3033 tattr |= ATTR_NARROW;
3035 /* Cursor here ? Save the 'background' */
3036 if (i == our_curs_y && j == term->curs.x) {
3037 cursor_background = tattr | tchar;
3038 term->dispcurs = term->disptext + idx;
3041 if ((term->disptext[idx] ^ tattr) & ATTR_WIDE)
3044 break_run = (((tattr ^ attr) & term->attr_mask) ||
3045 j - start >= sizeof(ch));
3047 /* Special hack for VT100 Linedraw glyphs */
3048 if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA
3049 && tchar <= 0xBD) break_run = TRUE;
3051 if (!dbcs_screenfont && !dirty_line) {
3052 if ((tchar | tattr) == term->disptext[idx])
3054 else if (!dirty_run && ccount == 1)
3059 if ((dirty_run || last_run_dirty) && ccount > 0) {
3060 do_text(ctx, start, i, ch, ccount, attr, lattr);
3066 if (dbcs_screenfont)
3067 last_run_dirty = dirty_run;
3068 dirty_run = dirty_line;
3071 if ((tchar | tattr) != term->disptext[idx])
3073 ch[ccount++] = (char) tchar;
3074 term->disptext[idx] = tchar | tattr;
3076 /* If it's a wide char step along to the next one. */
3077 if (tattr & ATTR_WIDE) {
3078 if (++j < term->cols) {
3081 /* Cursor is here ? Ouch! */
3082 if (i == our_curs_y && j == term->curs.x) {
3083 cursor_background = *d;
3084 term->dispcurs = term->disptext + idx;
3086 if (term->disptext[idx] != *d)
3088 term->disptext[idx] = *d;
3092 if (dirty_run && ccount > 0) {
3093 do_text(ctx, start, i, ch, ccount, attr, lattr);
3097 /* Cursor on this line ? (and changed) */
3098 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
3099 ch[0] = (char) (cursor_background & CHAR_MASK);
3100 attr = (cursor_background & ATTR_MASK) | cursor;
3101 do_cursor(ctx, term->curs.x, i, ch, 1, attr, lattr);
3102 term->curstype = cursor;
3108 * Flick the switch that says if blinking things should be shown or hidden.
3111 void term_blink(Terminal *term, int flg)
3113 long now, blink_diff;
3115 now = GETTICKCOUNT();
3116 blink_diff = now - term->last_tblink;
3118 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3119 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
3120 term->last_tblink = now;
3121 term->tblinker = !term->tblinker;
3126 term->last_blink = now;
3130 blink_diff = now - term->last_blink;
3132 /* Make sure the cursor blinks no faster than system blink rate */
3133 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
3136 term->last_blink = now;
3137 term->blinker = !term->blinker;
3141 * Invalidate the whole screen so it will be repainted in full.
3143 void term_invalidate(Terminal *term)
3147 for (i = 0; i < term->rows * (term->cols + 1); i++)
3148 term->disptext[i] = ATTR_INVALID;
3152 * Paint the window in response to a WM_PAINT message.
3154 void term_paint(Terminal *term, Context ctx,
3155 int left, int top, int right, int bottom, int immediately)
3158 if (left < 0) left = 0;
3159 if (top < 0) top = 0;
3160 if (right >= term->cols) right = term->cols-1;
3161 if (bottom >= term->rows) bottom = term->rows-1;
3163 for (i = top; i <= bottom && i < term->rows; i++) {
3164 if ((term->disptext[i * (term->cols + 1) + term->cols] &
3165 LATTR_MODE) == LATTR_NORM)
3166 for (j = left; j <= right && j < term->cols; j++)
3167 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3169 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
3170 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3173 /* This should happen soon enough, also for some reason it sometimes
3174 * fails to actually do anything when re-sizing ... painting the wrong
3178 do_paint (term, ctx, FALSE);
3182 * Attempt to scroll the scrollback. The second parameter gives the
3183 * position we want to scroll to; the first is +1 to denote that
3184 * this position is relative to the beginning of the scrollback, -1
3185 * to denote it is relative to the end, and 0 to denote that it is
3186 * relative to the current position.
3188 void term_scroll(Terminal *term, int rel, int where)
3190 int sbtop = -count234(term->scrollback);
3191 #ifdef OPTIMISE_SCROLL
3192 int olddisptop = term->disptop;
3194 #endif /* OPTIMISE_SCROLL */
3196 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
3197 if (term->disptop < sbtop)
3198 term->disptop = sbtop;
3199 if (term->disptop > 0)
3202 #ifdef OPTIMISE_SCROLL
3203 shift = (term->disptop - olddisptop);
3204 if (shift < term->rows && shift > -term->rows)
3205 scroll_display(term, 0, term->rows - 1, shift);
3206 #endif /* OPTIMISE_SCROLL */
3210 static void clipme(Terminal *term, pos top, pos bottom, int rect)
3213 wchar_t *wbptr; /* where next char goes within workbuf */
3215 int wblen = 0; /* workbuf len */
3216 int buflen; /* amount of memory allocated to workbuf */
3218 buflen = 5120; /* Default size */
3219 workbuf = smalloc(buflen * sizeof(wchar_t));
3220 wbptr = workbuf; /* start filling here */
3221 old_top_x = top.x; /* needed for rect==1 */
3223 while (poslt(top, bottom)) {
3225 unsigned long *ldata = lineptr(top.y);
3229 * nlpos will point at the maximum position on this line we
3230 * should copy up to. So we start it at the end of the
3234 nlpos.x = term->cols;
3237 * ... move it backwards if there's unused space at the end
3238 * of the line (and also set `nl' if this is the case,
3239 * because in normal selection mode this means we need a
3240 * newline at the end)...
3242 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3243 while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 ||
3244 (DIRECT_CHAR(ldata[nlpos.x - 1]) &&
3245 (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20))
3246 && poslt(top, nlpos))
3248 if (poslt(nlpos, bottom))
3253 * ... and then clip it to the terminal x coordinate if
3254 * we're doing rectangular selection. (In this case we
3255 * still did the above, so that copying e.g. the right-hand
3256 * column from a table doesn't fill with spaces on the
3260 if (nlpos.x > bottom.x)
3262 nl = (top.y < bottom.y);
3265 while (poslt(top, bottom) && poslt(top, nlpos)) {
3268 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
3270 wchar_t cbuf[16], *p;
3271 int uc = (ldata[top.x] & 0xFFFF);
3274 if (uc == UCSWIDE) {
3279 switch (uc & CSET_MASK) {
3281 if (!term->cfg->rawcnp) {
3282 uc = unitab_xterm[uc & 0xFF];
3286 uc = unitab_line[uc & 0xFF];
3289 uc = unitab_scoacs[uc&0xFF];
3292 switch (uc & CSET_MASK) {
3294 uc = unitab_font[uc & 0xFF];
3297 uc = unitab_oemcp[uc & 0xFF];
3301 set = (uc & CSET_MASK);
3302 c = (uc & CHAR_MASK);
3306 if (DIRECT_FONT(uc)) {
3307 if (c >= ' ' && c != 0x7F) {
3311 if (is_dbcs_leadbyte(font_codepage, (BYTE) c)) {
3313 buf[1] = (char) (0xFF & ldata[top.x + 1]);
3314 rv = mb_to_wc(font_codepage, 0, buf, 2, wbuf, 4);
3318 rv = mb_to_wc(font_codepage, 0, buf, 1, wbuf, 4);
3322 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
3329 for (p = cbuf; *p; p++) {
3330 /* Enough overhead for trailing NL and nul */
3331 if (wblen >= buflen - 16) {
3334 sizeof(wchar_t) * (buflen += 100));
3335 wbptr = workbuf + wblen;
3344 for (i = 0; i < sel_nl_sz; i++) {
3346 *wbptr++ = sel_nl[i];
3350 top.x = rect ? old_top_x : 0;
3352 #if SELECTION_NUL_TERMINATED
3356 write_clip(term->frontend, workbuf, wblen, FALSE); /* transfer to clipbd */
3357 if (buflen > 0) /* indicates we allocated this buffer */
3361 void term_copyall(Terminal *term)
3364 top.y = -count234(term->scrollback);
3366 clipme(term, top, term->curs, 0);
3370 * The wordness array is mainly for deciding the disposition of the
3371 * US-ASCII characters.
3373 static int wordtype(Terminal *term, int uc)
3376 int start, end, ctype;
3378 static const struct ucsword ucs_words[] = {
3384 0x037e, 0x037e, 1}, /* Greek question mark */
3386 0x0387, 0x0387, 1}, /* Greek ano teleia */
3388 0x055a, 0x055f, 1}, /* Armenian punctuation */
3390 0x0589, 0x0589, 1}, /* Armenian full stop */
3392 0x0700, 0x070d, 1}, /* Syriac punctuation */
3394 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3396 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3398 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3400 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3402 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3404 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3406 0x2000, 0x200a, 0}, /* Various spaces */
3408 0x2070, 0x207f, 2}, /* superscript */
3410 0x2080, 0x208f, 2}, /* subscript */
3412 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3414 0x3000, 0x3000, 0}, /* ideographic space */
3416 0x3001, 0x3020, 1}, /* ideographic punctuation */
3418 0x303f, 0x309f, 3}, /* Hiragana */
3420 0x30a0, 0x30ff, 3}, /* Katakana */
3422 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3424 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3426 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3428 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3430 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3432 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3434 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3436 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3438 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3442 const struct ucsword *wptr;
3444 uc &= (CSET_MASK | CHAR_MASK);
3446 switch (uc & CSET_MASK) {
3448 uc = unitab_xterm[uc & 0xFF];
3451 uc = unitab_line[uc & 0xFF];
3454 uc = unitab_scoacs[uc&0xFF];
3457 switch (uc & CSET_MASK) {
3459 uc = unitab_font[uc & 0xFF];
3462 uc = unitab_oemcp[uc & 0xFF];
3466 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3467 * fail as there's such a thing as a double width space. :-(
3469 if (dbcs_screenfont && font_codepage == line_codepage)
3473 return term->wordness[uc];
3475 for (wptr = ucs_words; wptr->start; wptr++) {
3476 if (uc >= wptr->start && uc <= wptr->end)
3484 * Spread the selection outwards according to the selection mode.
3486 static pos sel_spread_half(Terminal *term, pos p, int dir)
3488 unsigned long *ldata;
3490 int topy = -count234(term->scrollback);
3492 ldata = lineptr(p.y);
3494 switch (term->selmode) {
3497 * In this mode, every character is a separate unit, except
3498 * for runs of spaces at the end of a non-wrapping line.
3500 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3501 unsigned long *q = ldata + term->cols;
3502 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
3504 if (q == ldata + term->cols)
3506 if (p.x >= q - ldata)
3507 p.x = (dir == -1 ? q - ldata : term->cols - 1);
3512 * In this mode, the units are maximal runs of characters
3513 * whose `wordness' has the same value.
3515 wvalue = wordtype(term, ldata[p.x]);
3518 if (p.x < term->cols-1) {
3519 if (wordtype(term, ldata[p.x + 1]) == wvalue)
3524 if (ldata[term->cols] & LATTR_WRAPPED) {
3525 unsigned long *ldata2;
3526 ldata2 = lineptr(p.y+1);
3527 if (wordtype(term, ldata2[0]) == wvalue) {
3540 if (wordtype(term, ldata[p.x - 1]) == wvalue)
3545 unsigned long *ldata2;
3548 ldata2 = lineptr(p.y-1);
3549 if ((ldata2[term->cols] & LATTR_WRAPPED) &&
3550 wordtype(term, ldata2[term->cols-1]) == wvalue) {
3562 * In this mode, every line is a unit.
3564 p.x = (dir == -1 ? 0 : term->cols - 1);
3570 static void sel_spread(Terminal *term)
3572 if (term->seltype == LEXICOGRAPHIC) {
3573 term->selstart = sel_spread_half(term, term->selstart, -1);
3574 decpos(term->selend);
3575 term->selend = sel_spread_half(term, term->selend, +1);
3576 incpos(term->selend);
3580 void term_do_paste(Terminal *term)
3585 get_clip(term->frontend, &data, &len);
3586 if (data && len > 0) {
3589 term_seen_key_event(term); /* pasted data counts */
3591 if (term->paste_buffer)
3592 sfree(term->paste_buffer);
3593 term->paste_pos = term->paste_hold = term->paste_len = 0;
3594 term->paste_buffer = smalloc(len * sizeof(wchar_t));
3597 while (p < data + len) {
3598 while (p < data + len &&
3599 !(p <= data + len - sel_nl_sz &&
3600 !memcmp(p, sel_nl, sizeof(sel_nl))))
3605 for (i = 0; i < p - q; i++) {
3606 term->paste_buffer[term->paste_len++] = q[i];
3610 if (p <= data + len - sel_nl_sz &&
3611 !memcmp(p, sel_nl, sizeof(sel_nl))) {
3612 term->paste_buffer[term->paste_len++] = '\015';
3618 /* Assume a small paste will be OK in one go. */
3619 if (term->paste_len < 256) {
3621 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
3622 if (term->paste_buffer)
3623 sfree(term->paste_buffer);
3624 term->paste_buffer = 0;
3625 term->paste_pos = term->paste_hold = term->paste_len = 0;
3628 get_clip(term->frontend, NULL, NULL);
3631 void term_mouse(Terminal *term, Mouse_Button b, Mouse_Action a, int x, int y,
3632 int shift, int ctrl, int alt)
3635 unsigned long *ldata;
3636 int raw_mouse = (term->xterm_mouse &&
3637 !term->cfg->no_mouse_rep &&
3638 !(term->cfg->mouse_override && shift));
3639 int default_seltype;
3643 if (a == MA_DRAG && !raw_mouse)
3644 term_scroll(term, 0, -1);
3646 if (y >= term->rows) {
3648 if (a == MA_DRAG && !raw_mouse)
3649 term_scroll(term, 0, +1);
3658 if (x >= term->cols)
3661 selpoint.y = y + term->disptop;
3663 ldata = lineptr(selpoint.y);
3664 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM)
3668 int encstate = 0, r, c;
3675 encstate = 0x20; /* left button down */
3686 case MBT_WHEEL_DOWN:
3689 default: break; /* placate gcc warning about enum use */
3693 if (term->xterm_mouse == 1)
3699 term->mouse_is_down = 0;
3702 if (term->mouse_is_down == b)
3704 term->mouse_is_down = b;
3706 default: break; /* placate gcc warning about enum use */
3715 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
3716 ldisc_send(term->ldisc, abuf, 6, 0);
3721 b = translate_button(term->frontend, b);
3724 * Set the selection type (rectangular or normal) at the start
3725 * of a selection attempt, from the state of Alt.
3727 if (!alt ^ !term->cfg->rect_select)
3728 default_seltype = RECTANGULAR;
3730 default_seltype = LEXICOGRAPHIC;
3732 if (term->selstate == NO_SELECTION) {
3733 term->seltype = default_seltype;
3736 if (b == MBT_SELECT && a == MA_CLICK) {
3738 term->selstate = ABOUT_TO;
3739 term->seltype = default_seltype;
3740 term->selanchor = selpoint;
3741 term->selmode = SM_CHAR;
3742 } else if (b == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
3744 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
3745 term->selstate = DRAGGING;
3746 term->selstart = term->selanchor = selpoint;
3747 term->selend = term->selstart;
3748 incpos(term->selend);
3750 } else if ((b == MBT_SELECT && a == MA_DRAG) ||
3751 (b == MBT_EXTEND && a != MA_RELEASE)) {
3752 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
3754 if (b == MBT_EXTEND && a != MA_DRAG && term->selstate == SELECTED) {
3755 if (term->seltype == LEXICOGRAPHIC) {
3757 * For normal selection, we extend by moving
3758 * whichever end of the current selection is closer
3761 if (posdiff(selpoint, term->selstart) <
3762 posdiff(term->selend, term->selstart) / 2) {
3763 term->selanchor = term->selend;
3764 decpos(term->selanchor);
3766 term->selanchor = term->selstart;
3770 * For rectangular selection, we have a choice of
3771 * _four_ places to put selanchor and selpoint: the
3772 * four corners of the selection.
3774 if (2*selpoint.x < term->selstart.x + term->selend.x)
3775 term->selanchor.x = term->selend.x-1;
3777 term->selanchor.x = term->selstart.x;
3779 if (2*selpoint.y < term->selstart.y + term->selend.y)
3780 term->selanchor.y = term->selend.y;
3782 term->selanchor.y = term->selstart.y;
3784 term->selstate = DRAGGING;
3786 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
3787 term->selanchor = selpoint;
3788 term->selstate = DRAGGING;
3789 if (term->seltype == LEXICOGRAPHIC) {
3791 * For normal selection, we set (selstart,selend) to
3792 * (selpoint,selanchor) in some order.
3794 if (poslt(selpoint, term->selanchor)) {
3795 term->selstart = selpoint;
3796 term->selend = term->selanchor;
3797 incpos(term->selend);
3799 term->selstart = term->selanchor;
3800 term->selend = selpoint;
3801 incpos(term->selend);
3805 * For rectangular selection, we may need to
3806 * interchange x and y coordinates (if the user has
3807 * dragged in the -x and +y directions, or vice versa).
3809 term->selstart.x = min(term->selanchor.x, selpoint.x);
3810 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
3811 term->selstart.y = min(term->selanchor.y, selpoint.y);
3812 term->selend.y = max(term->selanchor.y, selpoint.y);
3815 } else if ((b == MBT_SELECT || b == MBT_EXTEND) && a == MA_RELEASE) {
3816 if (term->selstate == DRAGGING) {
3818 * We've completed a selection. We now transfer the
3819 * data to the clipboard.
3821 clipme(term, term->selstart, term->selend,
3822 (term->seltype == RECTANGULAR));
3823 term->selstate = SELECTED;
3825 term->selstate = NO_SELECTION;
3826 } else if (b == MBT_PASTE
3828 #if MULTICLICK_ONLY_EVENT
3829 || a == MA_2CLK || a == MA_3CLK
3832 request_paste(term->frontend);
3838 void term_nopaste(Terminal *term)
3840 if (term->paste_len == 0)
3842 sfree(term->paste_buffer);
3843 term->paste_buffer = NULL;
3844 term->paste_len = 0;
3847 int term_paste_pending(Terminal *term)
3849 return term->paste_len != 0;
3852 void term_paste(Terminal *term)
3854 long now, paste_diff;
3856 if (term->paste_len == 0)
3859 /* Don't wait forever to paste */
3860 if (term->paste_hold) {
3861 now = GETTICKCOUNT();
3862 paste_diff = now - term->last_paste;
3863 if (paste_diff >= 0 && paste_diff < 450)
3866 term->paste_hold = 0;
3868 while (term->paste_pos < term->paste_len) {
3870 while (n + term->paste_pos < term->paste_len) {
3871 if (term->paste_buffer[term->paste_pos + n++] == '\015')
3875 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
3876 term->paste_pos += n;
3878 if (term->paste_pos < term->paste_len) {
3879 term->paste_hold = 1;
3883 sfree(term->paste_buffer);
3884 term->paste_buffer = NULL;
3885 term->paste_len = 0;
3888 static void deselect(Terminal *term)
3890 term->selstate = NO_SELECTION;
3891 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
3894 void term_deselect(Terminal *term)
3900 int term_ldisc(Terminal *term, int option)
3902 if (option == LD_ECHO)
3903 return term->term_echoing;
3904 if (option == LD_EDIT)
3905 return term->term_editing;
3910 * from_backend(), to get data from the backend for the terminal.
3912 int from_backend(void *vterm, int is_stderr, char *data, int len)
3914 Terminal *term = (Terminal *)vterm;
3918 bufchain_add(&term->inbuf, data, len);
3921 * term_out() always completely empties inbuf. Therefore,
3922 * there's no reason at all to return anything other than zero
3923 * from this function, because there _can't_ be a question of
3924 * the remote side needing to wait until term_out() has cleared
3927 * This is a slightly suboptimal way to deal with SSH2 - in
3928 * principle, the window mechanism would allow us to continue
3929 * to accept data on forwarded ports and X connections even
3930 * while the terminal processing was going slowly - but we
3931 * can't do the 100% right thing without moving the terminal
3932 * processing into a separate thread, and that might hurt
3933 * portability. So we manage stdout buffering the old SSH1 way:
3934 * if the terminal processing goes slowly, the whole SSH
3935 * connection stops accepting data until it's ready.
3937 * In practice, I can't imagine this causing serious trouble.
3942 void term_provide_logctx(Terminal *term, void *logctx)
3944 term->logctx = logctx;