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);
1545 extern int wcwidth(wchar_t ucs);
1550 width = wcwidth((wchar_t) c);
1553 *term->cpos++ = c | term->curr_attr;
1554 if (++term->curs.x == term->cols) {
1555 *term->cpos |= LATTR_WRAPPED;
1556 if (term->curs.y == term->marg_b)
1557 scroll(term, term->marg_t, term->marg_b,
1559 else if (term->curs.y < term->rows - 1)
1564 *term->cpos++ = UCSWIDE | term->curr_attr;
1567 *term->cpos++ = c | term->curr_attr;
1574 if (term->curs.x == term->cols) {
1577 term->wrapnext = TRUE;
1578 if (term->wrap && term->vt52_mode) {
1579 term->cpos[1] |= LATTR_WRAPPED;
1580 if (term->curs.y == term->marg_b)
1581 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1582 else if (term->curs.y < term->rows - 1)
1586 term->wrapnext = FALSE;
1589 term->seen_disp_event = 1;
1594 * This state is virtually identical to SEEN_ESC, with the
1595 * exception that we have an OSC sequence in the pipeline,
1596 * and _if_ we see a backslash, we process it.
1600 term->termstate = TOPLEVEL;
1603 /* else fall through */
1605 if (c >= ' ' && c <= '/') {
1606 if (term->esc_query)
1607 term->esc_query = -1;
1609 term->esc_query = c;
1612 term->termstate = TOPLEVEL;
1613 switch (ANSI(c, term->esc_query)) {
1614 case '[': /* enter CSI mode */
1615 term->termstate = SEEN_CSI;
1616 term->esc_nargs = 1;
1617 term->esc_args[0] = ARG_DEFAULT;
1618 term->esc_query = FALSE;
1620 case ']': /* xterm escape sequences */
1621 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1622 compatibility(OTHER);
1623 term->termstate = SEEN_OSC;
1624 term->esc_args[0] = 0;
1626 case '7': /* save cursor */
1627 compatibility(VT100);
1628 save_cursor(term, TRUE);
1630 case '8': /* restore cursor */
1631 compatibility(VT100);
1632 save_cursor(term, FALSE);
1633 term->seen_disp_event = TRUE;
1636 compatibility(VT100);
1637 term->app_keypad_keys = TRUE;
1640 compatibility(VT100);
1641 term->app_keypad_keys = FALSE;
1643 case 'D': /* exactly equivalent to LF */
1644 compatibility(VT100);
1645 if (term->curs.y == term->marg_b)
1646 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1647 else if (term->curs.y < term->rows - 1)
1650 term->wrapnext = FALSE;
1651 term->seen_disp_event = TRUE;
1653 case 'E': /* exactly equivalent to CR-LF */
1654 compatibility(VT100);
1656 if (term->curs.y == term->marg_b)
1657 scroll(term, term->marg_t, term->marg_b, 1, TRUE);
1658 else if (term->curs.y < term->rows - 1)
1661 term->wrapnext = FALSE;
1662 term->seen_disp_event = TRUE;
1664 case 'M': /* reverse index - backwards LF */
1665 compatibility(VT100);
1666 if (term->curs.y == term->marg_t)
1667 scroll(term, term->marg_t, term->marg_b, -1, TRUE);
1668 else if (term->curs.y > 0)
1671 term->wrapnext = FALSE;
1672 term->seen_disp_event = TRUE;
1674 case 'Z': /* terminal type query */
1675 compatibility(VT100);
1677 ldisc_send(term->ldisc, term->id_string,
1678 strlen(term->id_string), 0);
1680 case 'c': /* restore power-on settings */
1681 compatibility(VT100);
1683 if (term->ldisc) /* cause ldisc to notice changes */
1684 ldisc_send(term->ldisc, NULL, 0, 0);
1685 if (term->reset_132) {
1686 if (!term->cfg->no_remote_resize)
1687 request_resize(term->frontend, 80, term->rows);
1688 term->reset_132 = 0;
1692 term->seen_disp_event = TRUE;
1694 case 'H': /* set a tab */
1695 compatibility(VT100);
1696 term->tabs[term->curs.x] = TRUE;
1699 case ANSI('8', '#'): /* ESC # 8 fills screen with Es :-) */
1700 compatibility(VT100);
1702 unsigned long *ldata;
1706 for (i = 0; i < term->rows; i++) {
1708 for (j = 0; j < term->cols; j++)
1709 ldata[j] = ATTR_DEFAULT | 'E';
1710 ldata[term->cols] = 0;
1713 term->seen_disp_event = TRUE;
1714 scrtop.x = scrtop.y = 0;
1716 scrbot.y = term->rows;
1717 check_selection(term, scrtop, scrbot);
1721 case ANSI('3', '#'):
1722 case ANSI('4', '#'):
1723 case ANSI('5', '#'):
1724 case ANSI('6', '#'):
1725 compatibility(VT100);
1727 unsigned long nlattr;
1728 unsigned long *ldata;
1729 switch (ANSI(c, term->esc_query)) {
1730 case ANSI('3', '#'):
1733 case ANSI('4', '#'):
1736 case ANSI('5', '#'):
1737 nlattr = LATTR_NORM;
1739 default: /* spiritually case ANSI('6', '#'): */
1740 nlattr = LATTR_WIDE;
1743 ldata = lineptr(term->curs.y);
1744 ldata[term->cols] &= ~LATTR_MODE;
1745 ldata[term->cols] |= nlattr;
1749 case ANSI('A', '('):
1750 compatibility(VT100);
1751 if (!term->cfg->no_remote_charset)
1752 term->cset_attr[0] = ATTR_GBCHR;
1754 case ANSI('B', '('):
1755 compatibility(VT100);
1756 if (!term->cfg->no_remote_charset)
1757 term->cset_attr[0] = ATTR_ASCII;
1759 case ANSI('0', '('):
1760 compatibility(VT100);
1761 if (!term->cfg->no_remote_charset)
1762 term->cset_attr[0] = ATTR_LINEDRW;
1764 case ANSI('U', '('):
1765 compatibility(OTHER);
1766 if (!term->cfg->no_remote_charset)
1767 term->cset_attr[0] = ATTR_SCOACS;
1770 case ANSI('A', ')'):
1771 compatibility(VT100);
1772 if (!term->cfg->no_remote_charset)
1773 term->cset_attr[1] = ATTR_GBCHR;
1775 case ANSI('B', ')'):
1776 compatibility(VT100);
1777 if (!term->cfg->no_remote_charset)
1778 term->cset_attr[1] = ATTR_ASCII;
1780 case ANSI('0', ')'):
1781 compatibility(VT100);
1782 if (!term->cfg->no_remote_charset)
1783 term->cset_attr[1] = ATTR_LINEDRW;
1785 case ANSI('U', ')'):
1786 compatibility(OTHER);
1787 if (!term->cfg->no_remote_charset)
1788 term->cset_attr[1] = ATTR_SCOACS;
1791 case ANSI('8', '%'): /* Old Linux code */
1792 case ANSI('G', '%'):
1793 compatibility(OTHER);
1794 if (!term->cfg->no_remote_charset)
1797 case ANSI('@', '%'):
1798 compatibility(OTHER);
1799 if (!term->cfg->no_remote_charset)
1805 term->termstate = TOPLEVEL; /* default */
1807 if (term->esc_nargs <= ARGS_MAX) {
1808 if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT)
1809 term->esc_args[term->esc_nargs - 1] = 0;
1810 term->esc_args[term->esc_nargs - 1] =
1811 10 * term->esc_args[term->esc_nargs - 1] + c - '0';
1813 term->termstate = SEEN_CSI;
1814 } else if (c == ';') {
1815 if (++term->esc_nargs <= ARGS_MAX)
1816 term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT;
1817 term->termstate = SEEN_CSI;
1818 } else if (c < '@') {
1819 if (term->esc_query)
1820 term->esc_query = -1;
1822 term->esc_query = TRUE;
1824 term->esc_query = c;
1825 term->termstate = SEEN_CSI;
1827 switch (ANSI(c, term->esc_query)) {
1828 case 'A': /* move up N lines */
1829 move(term, term->curs.x,
1830 term->curs.y - def(term->esc_args[0], 1), 1);
1831 term->seen_disp_event = TRUE;
1833 case 'e': /* move down N lines */
1834 compatibility(ANSI);
1837 move(term, term->curs.x,
1838 term->curs.y + def(term->esc_args[0], 1), 1);
1839 term->seen_disp_event = TRUE;
1841 case ANSI('c', '>'): /* report xterm version */
1842 compatibility(OTHER);
1843 /* this reports xterm version 136 so that VIM can
1844 use the drag messages from the mouse reporting */
1846 ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0);
1848 case 'a': /* move right N cols */
1849 compatibility(ANSI);
1852 move(term, term->curs.x + def(term->esc_args[0], 1),
1854 term->seen_disp_event = TRUE;
1856 case 'D': /* move left N cols */
1857 move(term, term->curs.x - def(term->esc_args[0], 1),
1859 term->seen_disp_event = TRUE;
1861 case 'E': /* move down N lines and CR */
1862 compatibility(ANSI);
1864 term->curs.y + def(term->esc_args[0], 1), 1);
1865 term->seen_disp_event = TRUE;
1867 case 'F': /* move up N lines and CR */
1868 compatibility(ANSI);
1870 term->curs.y - def(term->esc_args[0], 1), 1);
1871 term->seen_disp_event = TRUE;
1874 case '`': /* set horizontal posn */
1875 compatibility(ANSI);
1876 move(term, def(term->esc_args[0], 1) - 1,
1878 term->seen_disp_event = TRUE;
1880 case 'd': /* set vertical posn */
1881 compatibility(ANSI);
1882 move(term, term->curs.x,
1883 ((term->dec_om ? term->marg_t : 0) +
1884 def(term->esc_args[0], 1) - 1),
1885 (term->dec_om ? 2 : 0));
1886 term->seen_disp_event = TRUE;
1889 case 'f': /* set horz and vert posns at once */
1890 if (term->esc_nargs < 2)
1891 term->esc_args[1] = ARG_DEFAULT;
1892 move(term, def(term->esc_args[1], 1) - 1,
1893 ((term->dec_om ? term->marg_t : 0) +
1894 def(term->esc_args[0], 1) - 1),
1895 (term->dec_om ? 2 : 0));
1896 term->seen_disp_event = TRUE;
1898 case 'J': /* erase screen or parts of it */
1900 unsigned int i = def(term->esc_args[0], 0) + 1;
1903 erase_lots(term, FALSE, !!(i & 2), !!(i & 1));
1906 term->seen_disp_event = TRUE;
1908 case 'K': /* erase line or parts of it */
1910 unsigned int i = def(term->esc_args[0], 0) + 1;
1913 erase_lots(term, TRUE, !!(i & 2), !!(i & 1));
1915 term->seen_disp_event = TRUE;
1917 case 'L': /* insert lines */
1918 compatibility(VT102);
1919 if (term->curs.y <= term->marg_b)
1920 scroll(term, term->curs.y, term->marg_b,
1921 -def(term->esc_args[0], 1), FALSE);
1923 term->seen_disp_event = TRUE;
1925 case 'M': /* delete lines */
1926 compatibility(VT102);
1927 if (term->curs.y <= term->marg_b)
1928 scroll(term, term->curs.y, term->marg_b,
1929 def(term->esc_args[0], 1),
1932 term->seen_disp_event = TRUE;
1934 case '@': /* insert chars */
1935 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1936 compatibility(VT102);
1937 insch(term, def(term->esc_args[0], 1));
1938 term->seen_disp_event = TRUE;
1940 case 'P': /* delete chars */
1941 compatibility(VT102);
1942 insch(term, -def(term->esc_args[0], 1));
1943 term->seen_disp_event = TRUE;
1945 case 'c': /* terminal type query */
1946 compatibility(VT100);
1947 /* This is the response for a VT102 */
1949 ldisc_send(term->ldisc, term->id_string,
1950 strlen(term->id_string), 0);
1952 case 'n': /* cursor position query */
1954 if (term->esc_args[0] == 6) {
1956 sprintf(buf, "\033[%d;%dR", term->curs.y + 1,
1958 ldisc_send(term->ldisc, buf, strlen(buf), 0);
1959 } else if (term->esc_args[0] == 5) {
1960 ldisc_send(term->ldisc, "\033[0n", 4, 0);
1964 case 'h': /* toggle modes to high */
1966 compatibility(VT100);
1969 for (i = 0; i < term->esc_nargs; i++)
1970 toggle_mode(term, term->esc_args[i],
1971 term->esc_query, TRUE);
1976 compatibility(VT100);
1978 if (term->esc_nargs != 1) break;
1979 if (term->esc_args[0] == 5 && *term->cfg->printer) {
1980 term->printing = TRUE;
1981 term->only_printing = !term->esc_query;
1982 term->print_state = 0;
1983 term_print_setup(term);
1984 } else if (term->esc_args[0] == 4 &&
1986 term_print_finish(term);
1990 case 'l': /* toggle modes to low */
1992 compatibility(VT100);
1995 for (i = 0; i < term->esc_nargs; i++)
1996 toggle_mode(term, term->esc_args[i],
1997 term->esc_query, FALSE);
2000 case 'g': /* clear tabs */
2001 compatibility(VT100);
2002 if (term->esc_nargs == 1) {
2003 if (term->esc_args[0] == 0) {
2004 term->tabs[term->curs.x] = FALSE;
2005 } else if (term->esc_args[0] == 3) {
2007 for (i = 0; i < term->cols; i++)
2008 term->tabs[i] = FALSE;
2012 case 'r': /* set scroll margins */
2013 compatibility(VT100);
2014 if (term->esc_nargs <= 2) {
2016 top = def(term->esc_args[0], 1) - 1;
2017 bot = (term->esc_nargs <= 1
2018 || term->esc_args[1] == 0 ?
2020 def(term->esc_args[1], term->rows)) - 1;
2021 if (bot >= term->rows)
2022 bot = term->rows - 1;
2023 /* VTTEST Bug 9 - if region is less than 2 lines
2024 * don't change region.
2026 if (bot - top > 0) {
2031 * I used to think the cursor should be
2032 * placed at the top of the newly marginned
2033 * area. Apparently not: VMS TPU falls over
2036 * Well actually it should for
2039 term->curs.y = (term->dec_om ?
2042 term->seen_disp_event = TRUE;
2046 case 'm': /* set graphics rendition */
2049 * A VT100 without the AVO only had one
2050 * attribute, either underline or
2051 * reverse video depending on the
2052 * cursor type, this was selected by
2056 * This is sometimes DIM, eg on the
2059 * This is sometimes INVIS various ANSI.
2061 * This like 22 disables BOLD, DIM and INVIS
2063 * The ANSI colours appear on any
2064 * terminal that has colour (obviously)
2065 * but the interaction between sgr0 and
2066 * the colours varies but is usually
2067 * related to the background colour
2068 * erase item. The interaction between
2069 * colour attributes and the mono ones
2070 * is also very implementation
2073 * The 39 and 49 attributes are likely
2074 * to be unimplemented.
2077 for (i = 0; i < term->esc_nargs; i++) {
2078 switch (def(term->esc_args[i], 0)) {
2079 case 0: /* restore defaults */
2080 term->curr_attr = ATTR_DEFAULT;
2082 case 1: /* enable bold */
2083 compatibility(VT100AVO);
2084 term->curr_attr |= ATTR_BOLD;
2086 case 21: /* (enable double underline) */
2087 compatibility(OTHER);
2088 case 4: /* enable underline */
2089 compatibility(VT100AVO);
2090 term->curr_attr |= ATTR_UNDER;
2092 case 5: /* enable blink */
2093 compatibility(VT100AVO);
2094 term->curr_attr |= ATTR_BLINK;
2096 case 7: /* enable reverse video */
2097 term->curr_attr |= ATTR_REVERSE;
2099 case 10: /* SCO acs off */
2100 compatibility(SCOANSI);
2101 if (term->cfg->no_remote_charset) break;
2102 term->sco_acs = 0; break;
2103 case 11: /* SCO acs on */
2104 compatibility(SCOANSI);
2105 if (term->cfg->no_remote_charset) break;
2106 term->sco_acs = 1; break;
2107 case 12: /* SCO acs on flipped */
2108 compatibility(SCOANSI);
2109 if (term->cfg->no_remote_charset) break;
2110 term->sco_acs = 2; break;
2111 case 22: /* disable bold */
2112 compatibility2(OTHER, VT220);
2113 term->curr_attr &= ~ATTR_BOLD;
2115 case 24: /* disable underline */
2116 compatibility2(OTHER, VT220);
2117 term->curr_attr &= ~ATTR_UNDER;
2119 case 25: /* disable blink */
2120 compatibility2(OTHER, VT220);
2121 term->curr_attr &= ~ATTR_BLINK;
2123 case 27: /* disable reverse video */
2124 compatibility2(OTHER, VT220);
2125 term->curr_attr &= ~ATTR_REVERSE;
2136 term->curr_attr &= ~ATTR_FGMASK;
2138 (term->esc_args[i] - 30)<<ATTR_FGSHIFT;
2140 case 39: /* default-foreground */
2141 term->curr_attr &= ~ATTR_FGMASK;
2142 term->curr_attr |= ATTR_DEFFG;
2153 term->curr_attr &= ~ATTR_BGMASK;
2155 (term->esc_args[i] - 40)<<ATTR_BGSHIFT;
2157 case 49: /* default-background */
2158 term->curr_attr &= ~ATTR_BGMASK;
2159 term->curr_attr |= ATTR_DEFBG;
2164 term->erase_char = (' ' | ATTR_ASCII |
2170 case 's': /* save cursor */
2171 save_cursor(term, TRUE);
2173 case 'u': /* restore cursor */
2174 save_cursor(term, FALSE);
2175 term->seen_disp_event = TRUE;
2177 case 't': /* set page size - ie window height */
2179 * VT340/VT420 sequence DECSLPP, DEC only allows values
2180 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2181 * illegal values (eg first arg 1..9) for window changing
2184 if (term->esc_nargs <= 1
2185 && (term->esc_args[0] < 1 ||
2186 term->esc_args[0] >= 24)) {
2187 compatibility(VT340TEXT);
2188 if (!term->cfg->no_remote_resize)
2189 request_resize(term->frontend, term->cols,
2190 def(term->esc_args[0], 24));
2192 } else if (term->esc_nargs >= 1 &&
2193 term->esc_args[0] >= 1 &&
2194 term->esc_args[0] < 24) {
2195 compatibility(OTHER);
2197 switch (term->esc_args[0]) {
2201 set_iconic(term->frontend, FALSE);
2204 set_iconic(term->frontend, TRUE);
2207 if (term->esc_nargs >= 3) {
2208 if (!term->cfg->no_remote_resize)
2209 move_window(term->frontend,
2210 def(term->esc_args[1], 0),
2211 def(term->esc_args[2], 0));
2215 /* We should resize the window to a given
2216 * size in pixels here, but currently our
2217 * resizing code isn't healthy enough to
2222 set_zorder(term->frontend, TRUE);
2225 /* move to bottom */
2226 set_zorder(term->frontend, FALSE);
2229 refresh_window(term->frontend);
2232 if (term->esc_nargs >= 3) {
2233 if (!term->cfg->no_remote_resize)
2234 request_resize(term->frontend,
2235 def(term->esc_args[2], term->cfg->width),
2236 def(term->esc_args[1], term->cfg->height));
2240 if (term->esc_nargs >= 2)
2241 set_zoomed(term->frontend,
2247 ldisc_send(term->ldisc,
2248 is_iconic(term->frontend) ?
2249 "\033[1t" : "\033[2t", 4, 0);
2253 get_window_pos(term->frontend, &x, &y);
2254 len = sprintf(buf, "\033[3;%d;%dt", x, y);
2255 ldisc_send(term->ldisc, buf, len, 0);
2260 get_window_pixels(term->frontend, &x, &y);
2261 len = sprintf(buf, "\033[4;%d;%dt", x, y);
2262 ldisc_send(term->ldisc, buf, len, 0);
2267 len = sprintf(buf, "\033[8;%d;%dt",
2268 term->rows, term->cols);
2269 ldisc_send(term->ldisc, buf, len, 0);
2274 * Hmmm. Strictly speaking we
2275 * should return `the size of the
2276 * screen in characters', but
2277 * that's not easy: (a) window
2278 * furniture being what it is it's
2279 * hard to compute, and (b) in
2280 * resize-font mode maximising the
2281 * window wouldn't change the
2282 * number of characters. *shrug*. I
2283 * think we'll ignore it for the
2284 * moment and see if anyone
2285 * complains, and then ask them
2286 * what they would like it to do.
2291 p = get_window_title(term->frontend, TRUE);
2293 ldisc_send(term->ldisc, "\033]L", 3, 0);
2294 ldisc_send(term->ldisc, p, len, 0);
2295 ldisc_send(term->ldisc, "\033\\", 2, 0);
2300 p = get_window_title(term->frontend,FALSE);
2302 ldisc_send(term->ldisc, "\033]l", 3, 0);
2303 ldisc_send(term->ldisc, p, len, 0);
2304 ldisc_send(term->ldisc, "\033\\", 2, 0);
2311 compatibility(SCOANSI);
2312 scroll(term, term->marg_t, term->marg_b,
2313 def(term->esc_args[0], 1), TRUE);
2315 term->wrapnext = FALSE;
2316 term->seen_disp_event = TRUE;
2319 compatibility(SCOANSI);
2320 scroll(term, term->marg_t, term->marg_b,
2321 -def(term->esc_args[0], 1), TRUE);
2323 term->wrapnext = FALSE;
2324 term->seen_disp_event = TRUE;
2326 case ANSI('|', '*'):
2327 /* VT420 sequence DECSNLS
2328 * Set number of lines on screen
2329 * VT420 uses VGA like hardware and can support any size in
2330 * reasonable range (24..49 AIUI) with no default specified.
2332 compatibility(VT420);
2333 if (term->esc_nargs == 1 && term->esc_args[0] > 0) {
2334 if (!term->cfg->no_remote_resize)
2335 request_resize(term->frontend, term->cols,
2336 def(term->esc_args[0],
2337 term->cfg->height));
2341 case ANSI('|', '$'):
2342 /* VT340/VT420 sequence DECSCPP
2343 * Set number of columns per page
2344 * Docs imply range is only 80 or 132, but I'll allow any.
2346 compatibility(VT340TEXT);
2347 if (term->esc_nargs <= 1) {
2348 if (!term->cfg->no_remote_resize)
2349 request_resize(term->frontend,
2350 def(term->esc_args[0],
2351 term->cfg->width), term->rows);
2355 case 'X': /* write N spaces w/o moving cursor */
2356 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
2357 compatibility(ANSIMIN);
2359 int n = def(term->esc_args[0], 1);
2361 unsigned long *p = term->cpos;
2362 if (n > term->cols - term->curs.x)
2363 n = term->cols - term->curs.x;
2364 cursplus = term->curs;
2366 check_selection(term, term->curs, cursplus);
2368 *p++ = term->erase_char;
2369 term->seen_disp_event = TRUE;
2372 case 'x': /* report terminal characteristics */
2373 compatibility(VT100);
2376 int i = def(term->esc_args[0], 0);
2377 if (i == 0 || i == 1) {
2378 strcpy(buf, "\033[2;1;1;112;112;1;0x");
2380 ldisc_send(term->ldisc, buf, 20, 0);
2384 case 'Z': /* BackTab for xterm */
2385 compatibility(OTHER);
2387 int i = def(term->esc_args[0], 1);
2388 pos old_curs = term->curs;
2390 for(;i>0 && term->curs.x>0; i--) {
2393 } while (term->curs.x >0 &&
2394 !term->tabs[term->curs.x]);
2397 check_selection(term, old_curs, term->curs);
2400 case ANSI('L', '='):
2401 compatibility(OTHER);
2402 term->use_bce = (term->esc_args[0] <= 0);
2403 term->erase_char = ERASE_CHAR;
2405 term->erase_char = (' ' | ATTR_ASCII |
2407 (ATTR_FGMASK | ATTR_BGMASK)));
2409 case ANSI('E', '='):
2410 compatibility(OTHER);
2411 term->blink_is_real = (term->esc_args[0] >= 1);
2413 case ANSI('p', '"'):
2415 * Allow the host to make this emulator a
2416 * 'perfect' VT102. This first appeared in
2417 * the VT220, but we do need to get back to
2418 * PuTTY mode so I won't check it.
2420 * The arg in 40..42,50 are a PuTTY extension.
2421 * The 2nd arg, 8bit vs 7bit is not checked.
2423 * Setting VT102 mode should also change
2424 * the Fkeys to generate PF* codes as a
2425 * real VT102 has no Fkeys. The VT220 does
2426 * this, F11..F13 become ESC,BS,LF other
2427 * Fkeys send nothing.
2429 * Note ESC c will NOT change this!
2432 switch (term->esc_args[0]) {
2434 term->compatibility_level &= ~TM_VTXXX;
2435 term->compatibility_level |= TM_VT102;
2438 term->compatibility_level &= ~TM_VTXXX;
2439 term->compatibility_level |= TM_VT220;
2443 if (term->esc_args[0] > 60 &&
2444 term->esc_args[0] < 70)
2445 term->compatibility_level |= TM_VTXXX;
2449 term->compatibility_level &= TM_VTXXX;
2452 term->compatibility_level = TM_PUTTY;
2455 term->compatibility_level = TM_SCOANSI;
2459 term->compatibility_level = TM_PUTTY;
2465 /* Change the response to CSI c */
2466 if (term->esc_args[0] == 50) {
2469 strcpy(term->id_string, "\033[?");
2470 for (i = 1; i < term->esc_nargs; i++) {
2472 strcat(term->id_string, ";");
2473 sprintf(lbuf, "%d", term->esc_args[i]);
2474 strcat(term->id_string, lbuf);
2476 strcat(term->id_string, "c");
2479 /* Is this a good idea ?
2480 * Well we should do a soft reset at this point ...
2482 if (!has_compat(VT420) && has_compat(VT100)) {
2483 if (!term->cfg->no_remote_resize) {
2484 if (term->reset_132)
2485 request_resize(132, 24);
2487 request_resize(80, 24);
2495 term->osc_w = FALSE;
2497 case 'P': /* Linux palette sequence */
2498 term->termstate = SEEN_OSC_P;
2499 term->osc_strlen = 0;
2501 case 'R': /* Linux palette reset */
2502 palette_reset(term->frontend);
2503 term_invalidate(term);
2504 term->termstate = TOPLEVEL;
2506 case 'W': /* word-set */
2507 term->termstate = SEEN_OSC_W;
2520 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2524 * Grotty hack to support xterm and DECterm title
2525 * sequences concurrently.
2527 if (term->esc_args[0] == 2) {
2528 term->esc_args[0] = 1;
2531 /* else fall through */
2533 term->termstate = OSC_STRING;
2534 term->osc_strlen = 0;
2539 * This OSC stuff is EVIL. It takes just one character to get into
2540 * sysline mode and it's not initially obvious how to get out.
2541 * So I've added CR and LF as string aborts.
2542 * This shouldn't effect compatibility as I believe embedded
2543 * control characters are supposed to be interpreted (maybe?)
2544 * and they don't display anything useful anyway.
2548 if (c == '\012' || c == '\015') {
2549 term->termstate = TOPLEVEL;
2550 } else if (c == 0234 || c == '\007') {
2552 * These characters terminate the string; ST and BEL
2553 * terminate the sequence and trigger instant
2554 * processing of it, whereas ESC goes back to SEEN_ESC
2555 * mode unless it is followed by \, in which case it is
2556 * synonymous with ST in the first place.
2559 term->termstate = TOPLEVEL;
2560 } else if (c == '\033')
2561 term->termstate = OSC_MAYBE_ST;
2562 else if (term->osc_strlen < OSC_STR_MAX)
2563 term->osc_string[term->osc_strlen++] = c;
2567 int max = (term->osc_strlen == 0 ? 21 : 16);
2569 if (c >= '0' && c <= '9')
2571 else if (c >= 'A' && c <= 'A' + max - 10)
2573 else if (c >= 'a' && c <= 'a' + max - 10)
2576 term->termstate = TOPLEVEL;
2579 term->osc_string[term->osc_strlen++] = val;
2580 if (term->osc_strlen >= 7) {
2581 palette_set(term->frontend, term->osc_string[0],
2582 term->osc_string[1] * 16 + term->osc_string[2],
2583 term->osc_string[3] * 16 + term->osc_string[4],
2584 term->osc_string[5] * 16 + term->osc_string[6]);
2585 term_invalidate(term);
2586 term->termstate = TOPLEVEL;
2602 term->esc_args[0] = 10 * term->esc_args[0] + c - '0';
2605 term->termstate = OSC_STRING;
2606 term->osc_strlen = 0;
2610 term->termstate = TOPLEVEL;
2611 term->seen_disp_event = TRUE;
2614 move(term, term->curs.x, term->curs.y - 1, 1);
2617 move(term, term->curs.x, term->curs.y + 1, 1);
2620 move(term, term->curs.x + 1, term->curs.y, 1);
2623 move(term, term->curs.x - 1, term->curs.y, 1);
2626 * From the VT100 Manual
2627 * NOTE: The special graphics characters in the VT100
2628 * are different from those in the VT52
2630 * From VT102 manual:
2631 * 137 _ Blank - Same
2632 * 140 ` Reserved - Humm.
2633 * 141 a Solid rectangle - Similar
2634 * 142 b 1/ - Top half of fraction for the
2635 * 143 c 3/ - subscript numbers below.
2638 * 146 f Degrees - Same
2639 * 147 g Plus or minus - Same
2641 * 151 i Ellipsis (dots)
2644 * 154 l Bar at scan 0
2645 * 155 m Bar at scan 1
2646 * 156 n Bar at scan 2
2647 * 157 o Bar at scan 3 - Similar
2648 * 160 p Bar at scan 4 - Similar
2649 * 161 q Bar at scan 5 - Similar
2650 * 162 r Bar at scan 6 - Same
2651 * 163 s Bar at scan 7 - Similar
2666 term->cset_attr[term->cset = 0] = ATTR_LINEDRW;
2669 term->cset_attr[term->cset = 0] = ATTR_ASCII;
2672 move(term, 0, 0, 0);
2675 if (term->curs.y == 0)
2676 scroll(term, 0, term->rows - 1, -1, TRUE);
2677 else if (term->curs.y > 0)
2680 term->wrapnext = FALSE;
2683 erase_lots(term, FALSE, FALSE, TRUE);
2687 erase_lots(term, TRUE, FALSE, TRUE);
2691 /* XXX Print cursor line */
2694 /* XXX Start controller mode */
2697 /* XXX Stop controller mode */
2701 term->termstate = VT52_Y1;
2705 ldisc_send(term->ldisc, "\033/Z", 3, 0);
2708 term->app_keypad_keys = TRUE;
2711 term->app_keypad_keys = FALSE;
2714 /* XXX This should switch to VT100 mode not current or default
2715 * VT mode. But this will only have effect in a VT220+
2718 term->vt52_mode = FALSE;
2719 term->blink_is_real = term->cfg->blinktext;
2723 /* XXX Enter auto print mode */
2726 /* XXX Exit auto print mode */
2729 /* XXX Print screen */
2735 /* compatibility(ATARI) */
2736 move(term, 0, 0, 0);
2737 erase_lots(term, FALSE, FALSE, TRUE);
2741 /* compatibility(ATARI) */
2742 if (term->curs.y <= term->marg_b)
2743 scroll(term, term->curs.y, term->marg_b, -1, FALSE);
2746 /* compatibility(ATARI) */
2747 if (term->curs.y <= term->marg_b)
2748 scroll(term, term->curs.y, term->marg_b, 1, TRUE);
2751 /* compatibility(ATARI) */
2752 term->termstate = VT52_FG;
2755 /* compatibility(ATARI) */
2756 term->termstate = VT52_BG;
2759 /* compatibility(ATARI) */
2760 erase_lots(term, FALSE, TRUE, FALSE);
2764 /* compatibility(ATARI) */
2765 term->cursor_on = TRUE;
2768 /* compatibility(ATARI) */
2769 term->cursor_on = FALSE;
2771 /* case 'j': Save cursor position - broken on ST */
2772 /* case 'k': Restore cursor position */
2774 /* compatibility(ATARI) */
2775 erase_lots(term, TRUE, TRUE, TRUE);
2777 term->wrapnext = FALSE;
2781 /* compatibility(ATARI) */
2782 erase_lots(term, TRUE, TRUE, FALSE);
2785 /* compatibility(ATARI) */
2786 term->curr_attr |= ATTR_REVERSE;
2789 /* compatibility(ATARI) */
2790 term->curr_attr &= ~ATTR_REVERSE;
2792 case 'v': /* wrap Autowrap on - Wyse style */
2793 /* compatibility(ATARI) */
2796 case 'w': /* Autowrap off */
2797 /* compatibility(ATARI) */
2802 /* compatibility(OTHER) */
2803 term->vt52_bold = FALSE;
2804 term->curr_attr = ATTR_DEFAULT;
2806 term->erase_char = (' ' | ATTR_ASCII |
2808 (ATTR_FGMASK | ATTR_BGMASK)));
2811 /* compatibility(VI50) */
2812 term->curr_attr |= ATTR_UNDER;
2815 /* compatibility(VI50) */
2816 term->curr_attr &= ~ATTR_UNDER;
2819 /* compatibility(VI50) */
2820 term->vt52_bold = TRUE;
2821 term->curr_attr |= ATTR_BOLD;
2824 /* compatibility(VI50) */
2825 term->vt52_bold = FALSE;
2826 term->curr_attr &= ~ATTR_BOLD;
2832 term->termstate = VT52_Y2;
2833 move(term, term->curs.x, c - ' ', 0);
2836 term->termstate = TOPLEVEL;
2837 move(term, c - ' ', term->curs.y, 0);
2842 term->termstate = TOPLEVEL;
2843 term->curr_attr &= ~ATTR_FGMASK;
2844 term->curr_attr &= ~ATTR_BOLD;
2845 term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT;
2846 if ((c & 0x8) || term->vt52_bold)
2847 term->curr_attr |= ATTR_BOLD;
2850 term->erase_char = (' ' | ATTR_ASCII |
2852 (ATTR_FGMASK | ATTR_BGMASK)));
2855 term->termstate = TOPLEVEL;
2856 term->curr_attr &= ~ATTR_BGMASK;
2857 term->curr_attr &= ~ATTR_BLINK;
2858 term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT;
2860 /* Note: bold background */
2862 term->curr_attr |= ATTR_BLINK;
2865 term->erase_char = (' ' | ATTR_ASCII |
2867 (ATTR_FGMASK | ATTR_BGMASK)));
2870 default: break; /* placate gcc warning about enum use */
2872 if (term->selstate != NO_SELECTION) {
2873 pos cursplus = term->curs;
2875 check_selection(term, term->curs, cursplus);
2879 term_print_flush(term);
2884 * Compare two lines to determine whether they are sufficiently
2885 * alike to scroll-optimise one to the other. Return the degree of
2888 static int linecmp(Terminal *term, unsigned long *a, unsigned long *b)
2892 for (i = n = 0; i < term->cols; i++)
2893 n += (*a++ == *b++);
2899 * Given a context, update the window. Out of paranoia, we don't
2900 * allow WM_PAINT responses to do scrolling optimisations.
2902 static void do_paint(Terminal *term, Context ctx, int may_optimise)
2904 int i, j, our_curs_y;
2905 unsigned long rv, cursor;
2908 long cursor_background = ERASE_CHAR;
2909 unsigned long ticks;
2912 * Check the visual bell state.
2914 if (term->in_vbell) {
2915 ticks = GETTICKCOUNT();
2916 if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT)
2917 term->in_vbell = FALSE;
2920 rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0);
2923 * screen array, disptop, scrtop,
2925 * cfg.blinkpc, blink_is_real, tblinker,
2926 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
2929 /* Has the cursor position or type changed ? */
2930 if (term->cursor_on) {
2931 if (term->has_focus) {
2932 if (term->blinker || !term->cfg->blink_cur)
2933 cursor = TATTR_ACTCURS;
2937 cursor = TATTR_PASCURS;
2939 cursor |= TATTR_RIGHTCURS;
2942 our_curs_y = term->curs.y - term->disptop;
2944 if (term->dispcurs && (term->curstype != cursor ||
2946 term->disptext + our_curs_y * (term->cols + 1) +
2948 if (term->dispcurs > term->disptext &&
2949 (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
2950 term->dispcurs[-1] |= ATTR_INVALID;
2951 if ( (term->dispcurs[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
2952 term->dispcurs[1] |= ATTR_INVALID;
2953 *term->dispcurs |= ATTR_INVALID;
2956 term->dispcurs = NULL;
2958 /* The normal screen data */
2959 for (i = 0; i < term->rows; i++) {
2960 unsigned long *ldata;
2962 int idx, dirty_line, dirty_run, selected;
2963 unsigned long attr = 0;
2964 int updated_line = 0;
2967 int last_run_dirty = 0;
2969 scrpos.y = i + term->disptop;
2970 ldata = lineptr(scrpos.y);
2971 lattr = (ldata[term->cols] & LATTR_MODE);
2973 idx = i * (term->cols + 1);
2974 dirty_run = dirty_line = (ldata[term->cols] !=
2975 term->disptext[idx + term->cols]);
2976 term->disptext[idx + term->cols] = ldata[term->cols];
2978 for (j = 0; j < term->cols; j++, idx++) {
2979 unsigned long tattr, tchar;
2980 unsigned long *d = ldata + j;
2984 tchar = (*d & (CHAR_MASK | CSET_MASK));
2985 tattr = (*d & (ATTR_MASK ^ CSET_MASK));
2986 switch (tchar & CSET_MASK) {
2988 tchar = unitab_line[tchar & 0xFF];
2991 tchar = unitab_xterm[tchar & 0xFF];
2994 tchar = unitab_scoacs[tchar&0xFF];
2997 tattr |= (tchar & CSET_MASK);
2999 if ((d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE)
3002 /* Video reversing things */
3003 if (term->selstate == DRAGGING || term->selstate == SELECTED) {
3004 if (term->seltype == LEXICOGRAPHIC)
3005 selected = (posle(term->selstart, scrpos) &&
3006 poslt(scrpos, term->selend));
3008 selected = (posPle(term->selstart, scrpos) &&
3009 posPlt(scrpos, term->selend));
3013 ^ (selected ? ATTR_REVERSE : 0));
3015 /* 'Real' blinking ? */
3016 if (term->blink_is_real && (tattr & ATTR_BLINK)) {
3017 if (term->has_focus && term->tblinker) {
3019 tattr &= ~CSET_MASK;
3022 tattr &= ~ATTR_BLINK;
3026 * Check the font we'll _probably_ be using to see if
3027 * the character is wide when we don't want it to be.
3029 if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) {
3030 if ((tattr & ATTR_WIDE) == 0 &&
3031 char_width(ctx, (tchar | tattr) & 0xFFFF) == 2)
3032 tattr |= ATTR_NARROW;
3033 } else if (term->disptext[idx]&ATTR_NARROW)
3034 tattr |= ATTR_NARROW;
3036 /* Cursor here ? Save the 'background' */
3037 if (i == our_curs_y && j == term->curs.x) {
3038 cursor_background = tattr | tchar;
3039 term->dispcurs = term->disptext + idx;
3042 if ((term->disptext[idx] ^ tattr) & ATTR_WIDE)
3045 break_run = (((tattr ^ attr) & term->attr_mask) ||
3046 j - start >= sizeof(ch));
3048 /* Special hack for VT100 Linedraw glyphs */
3049 if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA
3050 && tchar <= 0xBD) break_run = TRUE;
3052 if (!dbcs_screenfont && !dirty_line) {
3053 if ((tchar | tattr) == term->disptext[idx])
3055 else if (!dirty_run && ccount == 1)
3060 if ((dirty_run || last_run_dirty) && ccount > 0) {
3061 do_text(ctx, start, i, ch, ccount, attr, lattr);
3067 if (dbcs_screenfont)
3068 last_run_dirty = dirty_run;
3069 dirty_run = dirty_line;
3072 if ((tchar | tattr) != term->disptext[idx])
3074 ch[ccount++] = (char) tchar;
3075 term->disptext[idx] = tchar | tattr;
3077 /* If it's a wide char step along to the next one. */
3078 if (tattr & ATTR_WIDE) {
3079 if (++j < term->cols) {
3082 /* Cursor is here ? Ouch! */
3083 if (i == our_curs_y && j == term->curs.x) {
3084 cursor_background = *d;
3085 term->dispcurs = term->disptext + idx;
3087 if (term->disptext[idx] != *d)
3089 term->disptext[idx] = *d;
3093 if (dirty_run && ccount > 0) {
3094 do_text(ctx, start, i, ch, ccount, attr, lattr);
3098 /* Cursor on this line ? (and changed) */
3099 if (i == our_curs_y && (term->curstype != cursor || updated_line)) {
3100 ch[0] = (char) (cursor_background & CHAR_MASK);
3101 attr = (cursor_background & ATTR_MASK) | cursor;
3102 do_cursor(ctx, term->curs.x, i, ch, 1, attr, lattr);
3103 term->curstype = cursor;
3109 * Flick the switch that says if blinking things should be shown or hidden.
3112 void term_blink(Terminal *term, int flg)
3114 long now, blink_diff;
3116 now = GETTICKCOUNT();
3117 blink_diff = now - term->last_tblink;
3119 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3120 if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) {
3121 term->last_tblink = now;
3122 term->tblinker = !term->tblinker;
3127 term->last_blink = now;
3131 blink_diff = now - term->last_blink;
3133 /* Make sure the cursor blinks no faster than system blink rate */
3134 if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK)
3137 term->last_blink = now;
3138 term->blinker = !term->blinker;
3142 * Invalidate the whole screen so it will be repainted in full.
3144 void term_invalidate(Terminal *term)
3148 for (i = 0; i < term->rows * (term->cols + 1); i++)
3149 term->disptext[i] = ATTR_INVALID;
3153 * Paint the window in response to a WM_PAINT message.
3155 void term_paint(Terminal *term, Context ctx,
3156 int left, int top, int right, int bottom, int immediately)
3159 if (left < 0) left = 0;
3160 if (top < 0) top = 0;
3161 if (right >= term->cols) right = term->cols-1;
3162 if (bottom >= term->rows) bottom = term->rows-1;
3164 for (i = top; i <= bottom && i < term->rows; i++) {
3165 if ((term->disptext[i * (term->cols + 1) + term->cols] &
3166 LATTR_MODE) == LATTR_NORM)
3167 for (j = left; j <= right && j < term->cols; j++)
3168 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3170 for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++)
3171 term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID;
3174 /* This should happen soon enough, also for some reason it sometimes
3175 * fails to actually do anything when re-sizing ... painting the wrong
3179 do_paint (term, ctx, FALSE);
3183 * Attempt to scroll the scrollback. The second parameter gives the
3184 * position we want to scroll to; the first is +1 to denote that
3185 * this position is relative to the beginning of the scrollback, -1
3186 * to denote it is relative to the end, and 0 to denote that it is
3187 * relative to the current position.
3189 void term_scroll(Terminal *term, int rel, int where)
3191 int sbtop = -count234(term->scrollback);
3192 #ifdef OPTIMISE_SCROLL
3193 int olddisptop = term->disptop;
3195 #endif /* OPTIMISE_SCROLL */
3197 term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where;
3198 if (term->disptop < sbtop)
3199 term->disptop = sbtop;
3200 if (term->disptop > 0)
3203 #ifdef OPTIMISE_SCROLL
3204 shift = (term->disptop - olddisptop);
3205 if (shift < term->rows && shift > -term->rows)
3206 scroll_display(term, 0, term->rows - 1, shift);
3207 #endif /* OPTIMISE_SCROLL */
3211 static void clipme(Terminal *term, pos top, pos bottom, int rect)
3214 wchar_t *wbptr; /* where next char goes within workbuf */
3216 int wblen = 0; /* workbuf len */
3217 int buflen; /* amount of memory allocated to workbuf */
3219 buflen = 5120; /* Default size */
3220 workbuf = smalloc(buflen * sizeof(wchar_t));
3221 wbptr = workbuf; /* start filling here */
3222 old_top_x = top.x; /* needed for rect==1 */
3224 while (poslt(top, bottom)) {
3226 unsigned long *ldata = lineptr(top.y);
3230 * nlpos will point at the maximum position on this line we
3231 * should copy up to. So we start it at the end of the
3235 nlpos.x = term->cols;
3238 * ... move it backwards if there's unused space at the end
3239 * of the line (and also set `nl' if this is the case,
3240 * because in normal selection mode this means we need a
3241 * newline at the end)...
3243 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3244 while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 ||
3245 (DIRECT_CHAR(ldata[nlpos.x - 1]) &&
3246 (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20))
3247 && poslt(top, nlpos))
3249 if (poslt(nlpos, bottom))
3254 * ... and then clip it to the terminal x coordinate if
3255 * we're doing rectangular selection. (In this case we
3256 * still did the above, so that copying e.g. the right-hand
3257 * column from a table doesn't fill with spaces on the
3261 if (nlpos.x > bottom.x)
3263 nl = (top.y < bottom.y);
3266 while (poslt(top, bottom) && poslt(top, nlpos)) {
3269 sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF));
3271 wchar_t cbuf[16], *p;
3272 int uc = (ldata[top.x] & 0xFFFF);
3275 if (uc == UCSWIDE) {
3280 switch (uc & CSET_MASK) {
3282 if (!term->cfg->rawcnp) {
3283 uc = unitab_xterm[uc & 0xFF];
3287 uc = unitab_line[uc & 0xFF];
3290 uc = unitab_scoacs[uc&0xFF];
3293 switch (uc & CSET_MASK) {
3295 uc = unitab_font[uc & 0xFF];
3298 uc = unitab_oemcp[uc & 0xFF];
3302 set = (uc & CSET_MASK);
3303 c = (uc & CHAR_MASK);
3307 if (DIRECT_FONT(uc)) {
3308 if (c >= ' ' && c != 0x7F) {
3312 if (is_dbcs_leadbyte(font_codepage, (BYTE) c)) {
3314 buf[1] = (char) (0xFF & ldata[top.x + 1]);
3315 rv = mb_to_wc(font_codepage, 0, buf, 2, wbuf, 4);
3319 rv = mb_to_wc(font_codepage, 0, buf, 1, wbuf, 4);
3323 memcpy(cbuf, wbuf, rv * sizeof(wchar_t));
3330 for (p = cbuf; *p; p++) {
3331 /* Enough overhead for trailing NL and nul */
3332 if (wblen >= buflen - 16) {
3335 sizeof(wchar_t) * (buflen += 100));
3336 wbptr = workbuf + wblen;
3345 for (i = 0; i < sel_nl_sz; i++) {
3347 *wbptr++ = sel_nl[i];
3351 top.x = rect ? old_top_x : 0;
3353 #if SELECTION_NUL_TERMINATED
3357 write_clip(term->frontend, workbuf, wblen, FALSE); /* transfer to clipbd */
3358 if (buflen > 0) /* indicates we allocated this buffer */
3362 void term_copyall(Terminal *term)
3365 top.y = -count234(term->scrollback);
3367 clipme(term, top, term->curs, 0);
3371 * The wordness array is mainly for deciding the disposition of the
3372 * US-ASCII characters.
3374 static int wordtype(Terminal *term, int uc)
3377 int start, end, ctype;
3379 static const struct ucsword ucs_words[] = {
3385 0x037e, 0x037e, 1}, /* Greek question mark */
3387 0x0387, 0x0387, 1}, /* Greek ano teleia */
3389 0x055a, 0x055f, 1}, /* Armenian punctuation */
3391 0x0589, 0x0589, 1}, /* Armenian full stop */
3393 0x0700, 0x070d, 1}, /* Syriac punctuation */
3395 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3397 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3399 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3401 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3403 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3405 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3407 0x2000, 0x200a, 0}, /* Various spaces */
3409 0x2070, 0x207f, 2}, /* superscript */
3411 0x2080, 0x208f, 2}, /* subscript */
3413 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3415 0x3000, 0x3000, 0}, /* ideographic space */
3417 0x3001, 0x3020, 1}, /* ideographic punctuation */
3419 0x303f, 0x309f, 3}, /* Hiragana */
3421 0x30a0, 0x30ff, 3}, /* Katakana */
3423 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3425 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3427 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3429 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3431 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3433 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3435 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3437 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3439 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3443 const struct ucsword *wptr;
3445 uc &= (CSET_MASK | CHAR_MASK);
3447 switch (uc & CSET_MASK) {
3449 uc = unitab_xterm[uc & 0xFF];
3452 uc = unitab_line[uc & 0xFF];
3455 uc = unitab_scoacs[uc&0xFF];
3458 switch (uc & CSET_MASK) {
3460 uc = unitab_font[uc & 0xFF];
3463 uc = unitab_oemcp[uc & 0xFF];
3467 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3468 * fail as there's such a thing as a double width space. :-(
3470 if (dbcs_screenfont && font_codepage == line_codepage)
3474 return term->wordness[uc];
3476 for (wptr = ucs_words; wptr->start; wptr++) {
3477 if (uc >= wptr->start && uc <= wptr->end)
3485 * Spread the selection outwards according to the selection mode.
3487 static pos sel_spread_half(Terminal *term, pos p, int dir)
3489 unsigned long *ldata;
3491 int topy = -count234(term->scrollback);
3493 ldata = lineptr(p.y);
3495 switch (term->selmode) {
3498 * In this mode, every character is a separate unit, except
3499 * for runs of spaces at the end of a non-wrapping line.
3501 if (!(ldata[term->cols] & LATTR_WRAPPED)) {
3502 unsigned long *q = ldata + term->cols;
3503 while (q > ldata && (q[-1] & CHAR_MASK) == 0x20)
3505 if (q == ldata + term->cols)
3507 if (p.x >= q - ldata)
3508 p.x = (dir == -1 ? q - ldata : term->cols - 1);
3513 * In this mode, the units are maximal runs of characters
3514 * whose `wordness' has the same value.
3516 wvalue = wordtype(term, ldata[p.x]);
3519 if (p.x < term->cols-1) {
3520 if (wordtype(term, ldata[p.x + 1]) == wvalue)
3525 if (ldata[term->cols] & LATTR_WRAPPED) {
3526 unsigned long *ldata2;
3527 ldata2 = lineptr(p.y+1);
3528 if (wordtype(term, ldata2[0]) == wvalue) {
3541 if (wordtype(term, ldata[p.x - 1]) == wvalue)
3546 unsigned long *ldata2;
3549 ldata2 = lineptr(p.y-1);
3550 if ((ldata2[term->cols] & LATTR_WRAPPED) &&
3551 wordtype(term, ldata2[term->cols-1]) == wvalue) {
3563 * In this mode, every line is a unit.
3565 p.x = (dir == -1 ? 0 : term->cols - 1);
3571 static void sel_spread(Terminal *term)
3573 if (term->seltype == LEXICOGRAPHIC) {
3574 term->selstart = sel_spread_half(term, term->selstart, -1);
3575 decpos(term->selend);
3576 term->selend = sel_spread_half(term, term->selend, +1);
3577 incpos(term->selend);
3581 void term_do_paste(Terminal *term)
3586 get_clip(term->frontend, &data, &len);
3587 if (data && len > 0) {
3590 term_seen_key_event(term); /* pasted data counts */
3592 if (term->paste_buffer)
3593 sfree(term->paste_buffer);
3594 term->paste_pos = term->paste_hold = term->paste_len = 0;
3595 term->paste_buffer = smalloc(len * sizeof(wchar_t));
3598 while (p < data + len) {
3599 while (p < data + len &&
3600 !(p <= data + len - sel_nl_sz &&
3601 !memcmp(p, sel_nl, sizeof(sel_nl))))
3606 for (i = 0; i < p - q; i++) {
3607 term->paste_buffer[term->paste_len++] = q[i];
3611 if (p <= data + len - sel_nl_sz &&
3612 !memcmp(p, sel_nl, sizeof(sel_nl))) {
3613 term->paste_buffer[term->paste_len++] = '\015';
3619 /* Assume a small paste will be OK in one go. */
3620 if (term->paste_len < 256) {
3622 luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0);
3623 if (term->paste_buffer)
3624 sfree(term->paste_buffer);
3625 term->paste_buffer = 0;
3626 term->paste_pos = term->paste_hold = term->paste_len = 0;
3629 get_clip(term->frontend, NULL, NULL);
3632 void term_mouse(Terminal *term, Mouse_Button b, Mouse_Action a, int x, int y,
3633 int shift, int ctrl, int alt)
3636 unsigned long *ldata;
3637 int raw_mouse = (term->xterm_mouse &&
3638 !term->cfg->no_mouse_rep &&
3639 !(term->cfg->mouse_override && shift));
3640 int default_seltype;
3644 if (a == MA_DRAG && !raw_mouse)
3645 term_scroll(term, 0, -1);
3647 if (y >= term->rows) {
3649 if (a == MA_DRAG && !raw_mouse)
3650 term_scroll(term, 0, +1);
3659 if (x >= term->cols)
3662 selpoint.y = y + term->disptop;
3664 ldata = lineptr(selpoint.y);
3665 if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM)
3669 int encstate = 0, r, c;
3676 encstate = 0x20; /* left button down */
3687 case MBT_WHEEL_DOWN:
3690 default: break; /* placate gcc warning about enum use */
3694 if (term->xterm_mouse == 1)
3700 term->mouse_is_down = 0;
3703 if (term->mouse_is_down == b)
3705 term->mouse_is_down = b;
3707 default: break; /* placate gcc warning about enum use */
3716 sprintf(abuf, "\033[M%c%c%c", encstate, c, r);
3717 ldisc_send(term->ldisc, abuf, 6, 0);
3722 b = translate_button(term->frontend, b);
3725 * Set the selection type (rectangular or normal) at the start
3726 * of a selection attempt, from the state of Alt.
3728 if (!alt ^ !term->cfg->rect_select)
3729 default_seltype = RECTANGULAR;
3731 default_seltype = LEXICOGRAPHIC;
3733 if (term->selstate == NO_SELECTION) {
3734 term->seltype = default_seltype;
3737 if (b == MBT_SELECT && a == MA_CLICK) {
3739 term->selstate = ABOUT_TO;
3740 term->seltype = default_seltype;
3741 term->selanchor = selpoint;
3742 term->selmode = SM_CHAR;
3743 } else if (b == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
3745 term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
3746 term->selstate = DRAGGING;
3747 term->selstart = term->selanchor = selpoint;
3748 term->selend = term->selstart;
3749 incpos(term->selend);
3751 } else if ((b == MBT_SELECT && a == MA_DRAG) ||
3752 (b == MBT_EXTEND && a != MA_RELEASE)) {
3753 if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint))
3755 if (b == MBT_EXTEND && a != MA_DRAG && term->selstate == SELECTED) {
3756 if (term->seltype == LEXICOGRAPHIC) {
3758 * For normal selection, we extend by moving
3759 * whichever end of the current selection is closer
3762 if (posdiff(selpoint, term->selstart) <
3763 posdiff(term->selend, term->selstart) / 2) {
3764 term->selanchor = term->selend;
3765 decpos(term->selanchor);
3767 term->selanchor = term->selstart;
3771 * For rectangular selection, we have a choice of
3772 * _four_ places to put selanchor and selpoint: the
3773 * four corners of the selection.
3775 if (2*selpoint.x < term->selstart.x + term->selend.x)
3776 term->selanchor.x = term->selend.x-1;
3778 term->selanchor.x = term->selstart.x;
3780 if (2*selpoint.y < term->selstart.y + term->selend.y)
3781 term->selanchor.y = term->selend.y;
3783 term->selanchor.y = term->selstart.y;
3785 term->selstate = DRAGGING;
3787 if (term->selstate != ABOUT_TO && term->selstate != DRAGGING)
3788 term->selanchor = selpoint;
3789 term->selstate = DRAGGING;
3790 if (term->seltype == LEXICOGRAPHIC) {
3792 * For normal selection, we set (selstart,selend) to
3793 * (selpoint,selanchor) in some order.
3795 if (poslt(selpoint, term->selanchor)) {
3796 term->selstart = selpoint;
3797 term->selend = term->selanchor;
3798 incpos(term->selend);
3800 term->selstart = term->selanchor;
3801 term->selend = selpoint;
3802 incpos(term->selend);
3806 * For rectangular selection, we may need to
3807 * interchange x and y coordinates (if the user has
3808 * dragged in the -x and +y directions, or vice versa).
3810 term->selstart.x = min(term->selanchor.x, selpoint.x);
3811 term->selend.x = 1+max(term->selanchor.x, selpoint.x);
3812 term->selstart.y = min(term->selanchor.y, selpoint.y);
3813 term->selend.y = max(term->selanchor.y, selpoint.y);
3816 } else if ((b == MBT_SELECT || b == MBT_EXTEND) && a == MA_RELEASE) {
3817 if (term->selstate == DRAGGING) {
3819 * We've completed a selection. We now transfer the
3820 * data to the clipboard.
3822 clipme(term, term->selstart, term->selend,
3823 (term->seltype == RECTANGULAR));
3824 term->selstate = SELECTED;
3826 term->selstate = NO_SELECTION;
3827 } else if (b == MBT_PASTE
3829 #if MULTICLICK_ONLY_EVENT
3830 || a == MA_2CLK || a == MA_3CLK
3833 request_paste(term->frontend);
3839 void term_nopaste(Terminal *term)
3841 if (term->paste_len == 0)
3843 sfree(term->paste_buffer);
3844 term->paste_buffer = NULL;
3845 term->paste_len = 0;
3848 int term_paste_pending(Terminal *term)
3850 return term->paste_len != 0;
3853 void term_paste(Terminal *term)
3855 long now, paste_diff;
3857 if (term->paste_len == 0)
3860 /* Don't wait forever to paste */
3861 if (term->paste_hold) {
3862 now = GETTICKCOUNT();
3863 paste_diff = now - term->last_paste;
3864 if (paste_diff >= 0 && paste_diff < 450)
3867 term->paste_hold = 0;
3869 while (term->paste_pos < term->paste_len) {
3871 while (n + term->paste_pos < term->paste_len) {
3872 if (term->paste_buffer[term->paste_pos + n++] == '\015')
3876 luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0);
3877 term->paste_pos += n;
3879 if (term->paste_pos < term->paste_len) {
3880 term->paste_hold = 1;
3884 sfree(term->paste_buffer);
3885 term->paste_buffer = NULL;
3886 term->paste_len = 0;
3889 static void deselect(Terminal *term)
3891 term->selstate = NO_SELECTION;
3892 term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0;
3895 void term_deselect(Terminal *term)
3901 int term_ldisc(Terminal *term, int option)
3903 if (option == LD_ECHO)
3904 return term->term_echoing;
3905 if (option == LD_EDIT)
3906 return term->term_editing;
3911 * from_backend(), to get data from the backend for the terminal.
3913 int from_backend(void *vterm, int is_stderr, char *data, int len)
3915 Terminal *term = (Terminal *)vterm;
3919 bufchain_add(&term->inbuf, data, len);
3922 * term_out() always completely empties inbuf. Therefore,
3923 * there's no reason at all to return anything other than zero
3924 * from this function, because there _can't_ be a question of
3925 * the remote side needing to wait until term_out() has cleared
3928 * This is a slightly suboptimal way to deal with SSH2 - in
3929 * principle, the window mechanism would allow us to continue
3930 * to accept data on forwarded ports and X connections even
3931 * while the terminal processing was going slowly - but we
3932 * can't do the 100% right thing without moving the terminal
3933 * processing into a separate thread, and that might hurt
3934 * portability. So we manage stdout buffering the old SSH1 way:
3935 * if the terminal processing goes slowly, the whole SSH
3936 * connection stops accepting data until it's ready.
3938 * In practice, I can't imagine this causing serious trouble.
3943 void term_provide_logctx(Terminal *term, void *logctx)
3945 term->logctx = logctx;