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 * Fetch the character at a particular position in a line array,
64 * for purposes of `wordtype'. The reason this isn't just a simple
65 * array reference is that if the character we find is UCSWIDE,
66 * then we must look one space further to the left.
68 #define UCSGET(a, x) \
69 ( (x)>0 && ((a)[(x)] & (CHAR_MASK | CSET_MASK)) == UCSWIDE ? \
70 (a)[(x)-1] : (a)[(x)] )
73 * Internal prototypes.
75 static unsigned long *resizeline(unsigned long *, int);
76 static unsigned long *lineptr(Terminal
*, int, int);
77 static void do_paint(Terminal
*, Context
, int);
78 static void erase_lots(Terminal
*, int, int, int);
79 static void swap_screen(Terminal
*, int, int, int);
80 static void update_sbar(Terminal
*);
81 static void deselect(Terminal
*);
82 static void term_print_finish(Terminal
*);
83 #ifdef OPTIMISE_SCROLL
84 static void scroll_display(Terminal
*, int, int, int);
85 #endif /* OPTIMISE_SCROLL */
88 * Resize a line to make it `cols' columns wide.
90 static unsigned long *resizeline(unsigned long *line
, int cols
)
93 unsigned long lineattrs
;
95 if (line
[0] != (unsigned long)cols
) {
97 * This line is the wrong length, which probably means it
98 * hasn't been accessed since a resize. Resize it now.
101 lineattrs
= line
[oldlen
+ 1];
102 line
= srealloc(line
, TSIZE
* (2 + cols
));
104 for (i
= oldlen
; i
< cols
; i
++)
105 line
[i
+ 1] = ERASE_CHAR
;
106 line
[cols
+ 1] = lineattrs
& LATTR_MODE
;
113 * Retrieve a line of the screen or of the scrollback, according to
114 * whether the y coordinate is non-negative or negative
117 static unsigned long *lineptr(Terminal
*term
, int y
, int lineno
)
119 unsigned long *line
, *newline
;
124 whichtree
= term
->screen
;
127 whichtree
= term
->scrollback
;
128 treeindex
= y
+ count234(term
->scrollback
);
130 line
= index234(whichtree
, treeindex
);
132 /* We assume that we don't screw up and retrieve something out of range. */
133 assert(line
!= NULL
);
135 newline
= resizeline(line
, term
->cols
);
136 if (newline
!= line
) {
137 delpos234(whichtree
, treeindex
);
138 addpos234(whichtree
, newline
, treeindex
);
145 #define lineptr(x) lineptr(term,x,__LINE__)
148 * Set up power-on settings for the terminal.
150 static void power_on(Terminal
*term
)
152 term
->curs
.x
= term
->curs
.y
= 0;
153 term
->alt_x
= term
->alt_y
= 0;
154 term
->savecurs
.x
= term
->savecurs
.y
= 0;
155 term
->alt_t
= term
->marg_t
= 0;
156 if (term
->rows
!= -1)
157 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
159 term
->alt_b
= term
->marg_b
= 0;
160 if (term
->cols
!= -1) {
162 for (i
= 0; i
< term
->cols
; i
++)
163 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
165 term
->alt_om
= term
->dec_om
= term
->cfg
->dec_om
;
166 term
->alt_ins
= term
->insert
= FALSE
;
167 term
->alt_wnext
= term
->wrapnext
= term
->save_wnext
= FALSE
;
168 term
->alt_wrap
= term
->wrap
= term
->cfg
->wrap_mode
;
169 term
->alt_cset
= term
->cset
= term
->save_cset
= 0;
170 term
->alt_utf
= term
->utf
= term
->save_utf
= 0;
172 term
->alt_sco_acs
= term
->sco_acs
= term
->save_sco_acs
= 0;
173 term
->cset_attr
[0] = term
->cset_attr
[1] = term
->save_csattr
= ATTR_ASCII
;
175 term
->in_vbell
= FALSE
;
177 term
->big_cursor
= 0;
178 term
->save_attr
= term
->curr_attr
= ATTR_DEFAULT
;
179 term
->term_editing
= term
->term_echoing
= FALSE
;
180 term
->app_cursor_keys
= term
->cfg
->app_cursor
;
181 term
->app_keypad_keys
= term
->cfg
->app_keypad
;
182 term
->use_bce
= term
->cfg
->bce
;
183 term
->blink_is_real
= term
->cfg
->blinktext
;
184 term
->erase_char
= ERASE_CHAR
;
186 term_print_finish(term
);
189 for (i
= 0; i
< 256; i
++)
190 term
->wordness
[i
] = term
->cfg
->wordness
[i
];
193 swap_screen(term
, 1, FALSE
, FALSE
);
194 erase_lots(term
, FALSE
, TRUE
, TRUE
);
195 swap_screen(term
, 0, FALSE
, FALSE
);
196 erase_lots(term
, FALSE
, TRUE
, TRUE
);
201 * Force a screen update.
203 void term_update(Terminal
*term
)
206 ctx
= get_ctx(term
->frontend
);
208 int need_sbar_update
= term
->seen_disp_event
;
209 if (term
->seen_disp_event
&& term
->cfg
->scroll_on_disp
) {
210 term
->disptop
= 0; /* return to main screen */
211 term
->seen_disp_event
= 0;
212 need_sbar_update
= TRUE
;
214 if (need_sbar_update
)
216 do_paint(term
, ctx
, TRUE
);
217 sys_cursor(term
->frontend
, term
->curs
.x
, term
->curs
.y
- term
->disptop
);
223 * Called from front end when a keypress occurs, to trigger
224 * anything magical that needs to happen in that situation.
226 void term_seen_key_event(Terminal
*term
)
229 * On any keypress, clear the bell overload mechanism
230 * completely, on the grounds that large numbers of
231 * beeps coming from deliberate key action are likely
232 * to be intended (e.g. beeps from filename completion
233 * blocking repeatedly).
235 term
->beep_overloaded
= FALSE
;
236 while (term
->beephead
) {
237 struct beeptime
*tmp
= term
->beephead
;
238 term
->beephead
= tmp
->next
;
241 term
->beeptail
= NULL
;
245 * Reset the scrollback on keypress, if we're doing that.
247 if (term
->cfg
->scroll_on_key
) {
248 term
->disptop
= 0; /* return to main screen */
249 term
->seen_disp_event
= 1;
254 * Same as power_on(), but an external function.
256 void term_pwron(Terminal
*term
)
259 if (term
->ldisc
) /* cause ldisc to notice changes */
260 ldisc_send(term
->ldisc
, NULL
, 0, 0);
268 * When the user reconfigures us, we need to check the forbidden-
269 * alternate-screen config option, disable raw mouse mode if the
270 * user has disabled mouse reporting, and abandon a print job if
271 * the user has disabled printing.
273 void term_reconfig(Terminal
*term
)
275 if (term
->cfg
->no_alt_screen
)
276 swap_screen(term
, 0, FALSE
, FALSE
);
277 if (term
->cfg
->no_mouse_rep
) {
278 term
->xterm_mouse
= 0;
279 set_raw_mouse_mode(term
->frontend
, 0);
281 if (term
->cfg
->no_remote_charset
) {
282 term
->cset_attr
[0] = term
->cset_attr
[1] = ATTR_ASCII
;
283 term
->sco_acs
= term
->alt_sco_acs
= 0;
286 if (!*term
->cfg
->printer
) {
287 term_print_finish(term
);
292 * Clear the scrollback.
294 void term_clrsb(Terminal
*term
)
298 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
) {
305 * Initialise the terminal.
307 Terminal
*term_init(Config
*mycfg
, void *frontend
)
312 * Allocate a new Terminal structure and initialise the fields
315 term
= smalloc(sizeof(Terminal
));
316 term
->frontend
= frontend
;
319 term
->compatibility_level
= TM_PUTTY
;
320 strcpy(term
->id_string
, "\033[?6c");
321 term
->last_blink
= term
->last_tblink
= 0;
322 term
->paste_buffer
= NULL
;
323 term
->last_paste
= 0;
324 bufchain_init(&term
->inbuf
);
325 bufchain_init(&term
->printer_buf
);
326 term
->printing
= term
->only_printing
= FALSE
;
327 term
->print_job
= NULL
;
328 term
->vt52_mode
= FALSE
;
329 term
->cr_lf_return
= FALSE
;
330 term
->seen_disp_event
= FALSE
;
331 term
->xterm_mouse
= term
->mouse_is_down
= FALSE
;
332 term
->reset_132
= FALSE
;
333 term
->blinker
= term
->tblinker
= 0;
335 term
->repeat_off
= FALSE
;
336 term
->termstate
= TOPLEVEL
;
337 term
->selstate
= NO_SELECTION
;
340 term
->screen
= term
->alt_screen
= term
->scrollback
= NULL
;
342 term
->disptext
= term
->dispcurs
= NULL
;
345 term
->rows
= term
->cols
= -1;
347 term
->beephead
= term
->beeptail
= NULL
;
349 term
->lastbeep
= FALSE
;
350 term
->beep_overloaded
= FALSE
;
351 term
->attr_mask
= 0xffffffff;
352 term
->resize_fn
= NULL
;
353 term
->resize_ctx
= NULL
;
359 * Set up the terminal for a given size.
361 void term_size(Terminal
*term
, int newrows
, int newcols
, int newsavelines
)
364 unsigned long *newdisp
, *line
;
367 int save_alt_which
= term
->alt_which
;
369 if (newrows
== term
->rows
&& newcols
== term
->cols
&&
370 newsavelines
== term
->savelines
)
371 return; /* nothing to do */
374 swap_screen(term
, 0, FALSE
, FALSE
);
376 term
->alt_t
= term
->marg_t
= 0;
377 term
->alt_b
= term
->marg_b
= newrows
- 1;
379 if (term
->rows
== -1) {
380 term
->scrollback
= newtree234(NULL
);
381 term
->screen
= newtree234(NULL
);
386 * Resize the screen and scrollback. We only need to shift
387 * lines around within our data structures, because lineptr()
388 * will take care of resizing each individual line if
391 * - If the new screen and the old screen differ in length, we
392 * must shunt some lines in from the scrollback or out to
395 * - If doing that fails to provide us with enough material to
396 * fill the new screen (i.e. the number of rows needed in
397 * the new screen exceeds the total number in the previous
398 * screen+scrollback), we must invent some blank lines to
401 * - Then, if the new scrollback length is less than the
402 * amount of scrollback we actually have, we must throw some
405 sblen
= count234(term
->scrollback
);
406 /* Do this loop to expand the screen if newrows > rows */
407 for (i
= term
->rows
; i
< newrows
; i
++) {
409 line
= delpos234(term
->scrollback
, --sblen
);
411 line
= smalloc(TSIZE
* (newcols
+ 2));
413 for (j
= 0; j
<= newcols
; j
++)
414 line
[j
+ 1] = ERASE_CHAR
;
416 addpos234(term
->screen
, line
, 0);
418 /* Do this loop to shrink the screen if newrows < rows */
419 for (i
= newrows
; i
< term
->rows
; i
++) {
420 line
= delpos234(term
->screen
, 0);
421 addpos234(term
->scrollback
, line
, sblen
++);
423 assert(count234(term
->screen
) == newrows
);
424 while (sblen
> newsavelines
) {
425 line
= delpos234(term
->scrollback
, 0);
429 assert(count234(term
->scrollback
) <= newsavelines
);
432 newdisp
= smalloc(newrows
* (newcols
+ 1) * TSIZE
);
433 for (i
= 0; i
< newrows
* (newcols
+ 1); i
++)
434 newdisp
[i
] = ATTR_INVALID
;
435 sfree(term
->disptext
);
436 term
->disptext
= newdisp
;
437 term
->dispcurs
= NULL
;
439 newalt
= newtree234(NULL
);
440 for (i
= 0; i
< newrows
; i
++) {
441 line
= smalloc(TSIZE
* (newcols
+ 2));
443 for (j
= 0; j
<= newcols
; j
++)
444 line
[j
+ 1] = term
->erase_char
;
445 addpos234(newalt
, line
, i
);
447 if (term
->alt_screen
) {
448 while (NULL
!= (line
= delpos234(term
->alt_screen
, 0)))
450 freetree234(term
->alt_screen
);
452 term
->alt_screen
= newalt
;
454 term
->tabs
= srealloc(term
->tabs
, newcols
* sizeof(*term
->tabs
));
457 for (i
= (term
->cols
> 0 ? term
->cols
: 0); i
< newcols
; i
++)
458 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
462 term
->curs
.y
+= newrows
- term
->rows
;
463 if (term
->curs
.y
< 0)
465 if (term
->curs
.y
>= newrows
)
466 term
->curs
.y
= newrows
- 1;
467 if (term
->curs
.x
>= newcols
)
468 term
->curs
.x
= newcols
- 1;
469 term
->alt_x
= term
->alt_y
= 0;
470 term
->wrapnext
= term
->alt_wnext
= FALSE
;
472 term
->rows
= newrows
;
473 term
->cols
= newcols
;
474 term
->savelines
= newsavelines
;
477 swap_screen(term
, save_alt_which
, FALSE
, FALSE
);
482 term
->resize_fn(term
->resize_ctx
, term
->cols
, term
->rows
);
486 * Hand a function and context pointer to the terminal which it can
487 * use to notify a back end of resizes.
489 void term_provide_resize_fn(Terminal
*term
,
490 void (*resize_fn
)(void *, int, int),
493 term
->resize_fn
= resize_fn
;
494 term
->resize_ctx
= resize_ctx
;
495 if (term
->cols
> 0 && term
->rows
> 0)
496 resize_fn(resize_ctx
, term
->cols
, term
->rows
);
500 * Swap screens. If `reset' is TRUE and we have been asked to
501 * switch to the alternate screen, we must bring most of its
502 * configuration from the main screen and erase the contents of the
503 * alternate screen completely. (This is even true if we're already
504 * on it! Blame xterm.)
506 static void swap_screen(Terminal
*term
, int which
, int reset
, int keep_cur_pos
)
512 reset
= FALSE
; /* do no weird resetting if which==0 */
514 if (which
!= term
->alt_which
) {
515 term
->alt_which
= which
;
517 ttr
= term
->alt_screen
;
518 term
->alt_screen
= term
->screen
;
521 if (!reset
&& !keep_cur_pos
)
522 term
->curs
.x
= term
->alt_x
;
525 if (!reset
&& !keep_cur_pos
)
526 term
->curs
.y
= term
->alt_y
;
529 if (!reset
) term
->marg_t
= term
->alt_t
;
532 if (!reset
) term
->marg_b
= term
->alt_b
;
535 if (!reset
) term
->dec_om
= term
->alt_om
;
538 if (!reset
) term
->wrap
= term
->alt_wrap
;
541 if (!reset
) term
->wrapnext
= term
->alt_wnext
;
544 if (!reset
) term
->insert
= term
->alt_ins
;
547 if (!reset
) term
->cset
= term
->alt_cset
;
550 if (!reset
) term
->utf
= term
->alt_utf
;
553 if (!reset
) term
->sco_acs
= term
->alt_sco_acs
;
554 term
->alt_sco_acs
= t
;
557 if (reset
&& term
->screen
) {
559 * Yes, this _is_ supposed to honour background-colour-erase.
561 erase_lots(term
, FALSE
, TRUE
, TRUE
);
565 * This might not be possible if we're called during
573 * Update the scroll bar.
575 static void update_sbar(Terminal
*term
)
579 nscroll
= count234(term
->scrollback
);
581 set_sbar(term
->frontend
, nscroll
+ term
->rows
,
582 nscroll
+ term
->disptop
, term
->rows
);
586 * Check whether the region bounded by the two pointers intersects
587 * the scroll region, and de-select the on-screen selection if so.
589 static void check_selection(Terminal
*term
, pos from
, pos to
)
591 if (poslt(from
, term
->selend
) && poslt(term
->selstart
, to
))
596 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
597 * for backward.) `sb' is TRUE if the scrolling is permitted to
598 * affect the scrollback buffer.
600 * NB this function invalidates all pointers into lines of the
601 * screen data structures. In particular, you MUST call fix_cpos
602 * after calling scroll() and before doing anything else that
603 * uses the cpos shortcut pointer.
605 static void scroll(Terminal
*term
, int topline
, int botline
, int lines
, int sb
)
607 unsigned long *line
, *line2
;
608 int i
, seltop
, olddisptop
, shift
;
610 if (topline
!= 0 || term
->alt_which
!= 0)
613 olddisptop
= term
->disptop
;
617 line
= delpos234(term
->screen
, botline
);
618 line
= resizeline(line
, term
->cols
);
619 for (i
= 0; i
< term
->cols
; i
++)
620 line
[i
+ 1] = term
->erase_char
;
621 line
[term
->cols
+ 1] = 0;
622 addpos234(term
->screen
, line
, topline
);
624 if (term
->selstart
.y
>= topline
&& term
->selstart
.y
<= botline
) {
626 if (term
->selstart
.y
> botline
) {
627 term
->selstart
.y
= botline
;
628 term
->selstart
.x
= 0;
631 if (term
->selend
.y
>= topline
&& term
->selend
.y
<= botline
) {
633 if (term
->selend
.y
> botline
) {
634 term
->selend
.y
= botline
;
643 line
= delpos234(term
->screen
, topline
);
644 if (sb
&& term
->savelines
> 0) {
645 int sblen
= count234(term
->scrollback
);
647 * We must add this line to the scrollback. We'll
648 * remove a line from the top of the scrollback to
649 * replace it, or allocate a new one if the
650 * scrollback isn't full.
652 if (sblen
== term
->savelines
) {
653 sblen
--, line2
= delpos234(term
->scrollback
, 0);
655 line2
= smalloc(TSIZE
* (term
->cols
+ 2));
656 line2
[0] = term
->cols
;
658 addpos234(term
->scrollback
, line
, sblen
);
662 * If the user is currently looking at part of the
663 * scrollback, and they haven't enabled any options
664 * that are going to reset the scrollback as a
665 * result of this movement, then the chances are
666 * they'd like to keep looking at the same line. So
667 * we move their viewpoint at the same rate as the
668 * scroll, at least until their viewpoint hits the
669 * top end of the scrollback buffer, at which point
670 * we don't have the choice any more.
672 * Thanks to Jan Holmen Holsten for the idea and
673 * initial implementation.
675 if (term
->disptop
> -term
->savelines
&& term
->disptop
< 0)
678 line
= resizeline(line
, term
->cols
);
679 for (i
= 0; i
< term
->cols
; i
++)
680 line
[i
+ 1] = term
->erase_char
;
681 line
[term
->cols
+ 1] = 0;
682 addpos234(term
->screen
, line
, botline
);
685 * If the selection endpoints move into the scrollback,
686 * we keep them moving until they hit the top. However,
687 * of course, if the line _hasn't_ moved into the
688 * scrollback then we don't do this, and cut them off
689 * at the top of the scroll region.
691 * This applies to selstart and selend (for an existing
692 * selection), and also selanchor (for one being
693 * selected as we speak).
695 seltop
= sb ?
-term
->savelines
: topline
;
697 if (term
->selstart
.y
>= seltop
&&
698 term
->selstart
.y
<= botline
) {
700 if (term
->selstart
.y
< seltop
) {
701 term
->selstart
.y
= seltop
;
702 term
->selstart
.x
= 0;
705 if (term
->selend
.y
>= seltop
&& term
->selend
.y
<= botline
) {
707 if (term
->selend
.y
< seltop
) {
708 term
->selend
.y
= seltop
;
712 if (term
->selanchor
.y
>= seltop
&& term
->selanchor
.y
<= botline
) {
714 if (term
->selanchor
.y
< seltop
) {
715 term
->selanchor
.y
= seltop
;
716 term
->selanchor
.x
= 0;
723 #ifdef OPTIMISE_SCROLL
724 shift
+= term
->disptop
- olddisptop
;
725 if (shift
< term
->rows
&& shift
> -term
->rows
&& shift
!= 0)
726 scroll_display(term
, topline
, botline
, shift
);
727 #endif /* OPTIMISE_SCROLL */
730 #ifdef OPTIMISE_SCROLL
732 * Scroll the physical display, and our conception of it in disptext.
734 static void scroll_display(Terminal
*term
, int topline
, int botline
, int lines
)
736 unsigned long *start
, *end
;
737 int distance
, size
, i
;
739 start
= term
->disptext
+ topline
* (term
->cols
+ 1);
740 end
= term
->disptext
+ (botline
+ 1) * (term
->cols
+ 1);
741 distance
= (lines
> 0 ? lines
: -lines
) * (term
->cols
+ 1);
742 size
= end
- start
- distance
;
744 memmove(start
, start
+ distance
, size
* TSIZE
);
745 if (term
->dispcurs
>= start
+ distance
&&
746 term
->dispcurs
<= start
+ distance
+ size
)
747 term
->dispcurs
-= distance
;
748 for (i
= 0; i
< distance
; i
++)
749 (start
+ size
)[i
] |= ATTR_INVALID
;
751 memmove(start
+ distance
, start
, size
* TSIZE
);
752 if (term
->dispcurs
>= start
&& term
->dispcurs
<= start
+ size
)
753 term
->dispcurs
+= distance
;
754 for (i
= 0; i
< distance
; i
++)
755 start
[i
] |= ATTR_INVALID
;
757 do_scroll(term
->frontend
, topline
, botline
, lines
);
759 #endif /* OPTIMISE_SCROLL */
762 * Move the cursor to a given position, clipping at boundaries. We
763 * may or may not want to clip at the scroll margin: marg_clip is 0
764 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
765 * even _being_ outside the margins.
767 static void move(Terminal
*term
, int x
, int y
, int marg_clip
)
774 if ((term
->curs
.y
>= term
->marg_t
|| marg_clip
== 2) &&
777 if ((term
->curs
.y
<= term
->marg_b
|| marg_clip
== 2) &&
788 term
->wrapnext
= FALSE
;
792 * Save or restore the cursor and SGR mode.
794 static void save_cursor(Terminal
*term
, int save
)
797 term
->savecurs
= term
->curs
;
798 term
->save_attr
= term
->curr_attr
;
799 term
->save_cset
= term
->cset
;
800 term
->save_utf
= term
->utf
;
801 term
->save_wnext
= term
->wrapnext
;
802 term
->save_csattr
= term
->cset_attr
[term
->cset
];
803 term
->save_sco_acs
= term
->sco_acs
;
805 term
->curs
= term
->savecurs
;
806 /* Make sure the window hasn't shrunk since the save */
807 if (term
->curs
.x
>= term
->cols
)
808 term
->curs
.x
= term
->cols
- 1;
809 if (term
->curs
.y
>= term
->rows
)
810 term
->curs
.y
= term
->rows
- 1;
812 term
->curr_attr
= term
->save_attr
;
813 term
->cset
= term
->save_cset
;
814 term
->utf
= term
->save_utf
;
815 term
->wrapnext
= term
->save_wnext
;
817 * wrapnext might reset to False if the x position is no
818 * longer at the rightmost edge.
820 if (term
->wrapnext
&& term
->curs
.x
< term
->cols
-1)
821 term
->wrapnext
= FALSE
;
822 term
->cset_attr
[term
->cset
] = term
->save_csattr
;
823 term
->sco_acs
= term
->save_sco_acs
;
826 term
->erase_char
= (' ' | ATTR_ASCII
|
828 (ATTR_FGMASK
| ATTR_BGMASK
)));
833 * This function is called before doing _anything_ which affects
834 * only part of a line of text. It is used to mark the boundary
835 * between two character positions, and it indicates that some sort
836 * of effect is going to happen on only one side of that boundary.
838 * The effect of this function is to check whether a CJK
839 * double-width character is straddling the boundary, and to remove
840 * it and replace it with two spaces if so. (Of course, one or
841 * other of those spaces is then likely to be replaced with
842 * something else again, as a result of whatever happens next.)
844 * Also, if the boundary is at the right-hand _edge_ of the screen,
845 * it implies something deliberate is being done to the rightmost
846 * column position; hence we must clear LATTR_WRAPPED2.
848 * The input to the function is the coordinates of the _second_
849 * character of the pair.
851 static void check_boundary(Terminal
*term
, int x
, int y
)
853 unsigned long *ldata
;
855 /* Validate input coordinates, just in case. */
856 if (x
== 0 || x
> term
->cols
)
860 if (x
== term
->cols
) {
861 ldata
[x
] &= ~LATTR_WRAPPED2
;
863 if ((ldata
[x
] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
) {
864 ldata
[x
-1] = ldata
[x
] =
865 (ldata
[x
-1] &~ (CHAR_MASK
| CSET_MASK
)) | ATTR_ASCII
| ' ';
871 * Erase a large portion of the screen: the whole screen, or the
872 * whole line, or parts thereof.
874 static void erase_lots(Terminal
*term
,
875 int line_only
, int from_begin
, int to_end
)
879 unsigned long *ldata
;
882 start
.y
= term
->curs
.y
;
884 end
.y
= term
->curs
.y
+ 1;
901 if (!from_begin
|| !to_end
)
902 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
903 check_selection(term
, start
, end
);
905 /* Clear screen also forces a full window redraw, just in case. */
906 if (start
.y
== 0 && start
.x
== 0 && end
.y
== term
->rows
)
907 term_invalidate(term
);
909 ldata
= lineptr(start
.y
);
910 while (poslt(start
, end
)) {
911 if (start
.x
== term
->cols
&& !erase_lattr
)
912 ldata
[start
.x
] &= ~(LATTR_WRAPPED
| LATTR_WRAPPED2
);
914 ldata
[start
.x
] = term
->erase_char
;
915 if (incpos(start
) && start
.y
< term
->rows
)
916 ldata
= lineptr(start
.y
);
921 * Insert or delete characters within the current line. n is +ve if
922 * insertion is desired, and -ve for deletion.
924 static void insch(Terminal
*term
, int n
)
926 int dir
= (n
< 0 ?
-1 : +1);
929 unsigned long *ldata
;
931 n
= (n
< 0 ?
-n
: n
);
932 if (n
> term
->cols
- term
->curs
.x
)
933 n
= term
->cols
- term
->curs
.x
;
934 m
= term
->cols
- term
->curs
.x
- n
;
935 cursplus
.y
= term
->curs
.y
;
936 cursplus
.x
= term
->curs
.x
+ n
;
937 check_selection(term
, term
->curs
, cursplus
);
938 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
940 check_boundary(term
, term
->curs
.x
+ n
, term
->curs
.y
);
941 ldata
= lineptr(term
->curs
.y
);
943 memmove(ldata
+ term
->curs
.x
, ldata
+ term
->curs
.x
+ n
, m
* TSIZE
);
945 ldata
[term
->curs
.x
+ m
++] = term
->erase_char
;
947 memmove(ldata
+ term
->curs
.x
+ n
, ldata
+ term
->curs
.x
, m
* TSIZE
);
949 ldata
[term
->curs
.x
+ n
] = term
->erase_char
;
954 * Toggle terminal mode `mode' to state `state'. (`query' indicates
955 * whether the mode is a DEC private one or a normal one.)
957 static void toggle_mode(Terminal
*term
, int mode
, int query
, int state
)
963 case 1: /* application cursor keys */
964 term
->app_cursor_keys
= state
;
966 case 2: /* VT52 mode */
967 term
->vt52_mode
= !state
;
968 if (term
->vt52_mode
) {
969 term
->blink_is_real
= FALSE
;
970 term
->vt52_bold
= FALSE
;
972 term
->blink_is_real
= term
->cfg
->blinktext
;
975 case 3: /* 80/132 columns */
977 if (!term
->cfg
->no_remote_resize
)
978 request_resize(term
->frontend
, state ?
132 : 80, term
->rows
);
979 term
->reset_132
= state
;
981 case 5: /* reverse video */
983 * Toggle reverse video. If we receive an OFF within the
984 * visual bell timeout period after an ON, we trigger an
985 * effective visual bell, so that ESC[?5hESC[?5l will
986 * always be an actually _visible_ visual bell.
988 ticks
= GETTICKCOUNT();
989 /* turn off a previous vbell to avoid inconsistencies */
990 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
991 term
->in_vbell
= FALSE
;
992 if (term
->rvideo
&& !state
&& /* we're turning it off... */
993 (ticks
- term
->rvbell_startpoint
) < VBELL_TIMEOUT
) {/*...soon*/
994 /* If there's no vbell timeout already, or this one lasts
995 * longer, replace vbell_timeout with ours. */
996 if (!term
->in_vbell
||
997 (term
->rvbell_startpoint
- term
->vbell_startpoint
<
999 term
->vbell_startpoint
= term
->rvbell_startpoint
;
1000 term
->in_vbell
= TRUE
; /* may clear rvideo but set in_vbell */
1001 } else if (!term
->rvideo
&& state
) {
1002 /* This is an ON, so we notice the time and save it. */
1003 term
->rvbell_startpoint
= ticks
;
1005 term
->rvideo
= state
;
1006 term
->seen_disp_event
= TRUE
;
1010 case 6: /* DEC origin mode */
1011 term
->dec_om
= state
;
1013 case 7: /* auto wrap */
1016 case 8: /* auto key repeat */
1017 term
->repeat_off
= !state
;
1019 case 10: /* set local edit mode */
1020 term
->term_editing
= state
;
1021 if (term
->ldisc
) /* cause ldisc to notice changes */
1022 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1024 case 25: /* enable/disable cursor */
1025 compatibility2(OTHER
, VT220
);
1026 term
->cursor_on
= state
;
1027 term
->seen_disp_event
= TRUE
;
1029 case 47: /* alternate screen */
1030 compatibility(OTHER
);
1032 swap_screen(term
, term
->cfg
->no_alt_screen ?
0 : state
, FALSE
, FALSE
);
1035 case 1000: /* xterm mouse 1 */
1036 term
->xterm_mouse
= state ?
1 : 0;
1037 set_raw_mouse_mode(term
->frontend
, state
);
1039 case 1002: /* xterm mouse 2 */
1040 term
->xterm_mouse
= state ?
2 : 0;
1041 set_raw_mouse_mode(term
->frontend
, state
);
1043 case 1047: /* alternate screen */
1044 compatibility(OTHER
);
1046 swap_screen(term
, term
->cfg
->no_alt_screen ?
0 : state
, TRUE
, TRUE
);
1049 case 1048: /* save/restore cursor */
1050 save_cursor(term
, state
);
1051 if (!state
) term
->seen_disp_event
= TRUE
;
1053 case 1049: /* cursor & alternate screen */
1055 save_cursor(term
, state
);
1056 if (!state
) term
->seen_disp_event
= TRUE
;
1057 compatibility(OTHER
);
1059 swap_screen(term
, term
->cfg
->no_alt_screen ?
0 : state
, TRUE
, FALSE
);
1061 save_cursor(term
, state
);
1066 case 4: /* set insert mode */
1067 compatibility(VT102
);
1068 term
->insert
= state
;
1070 case 12: /* set echo mode */
1071 term
->term_echoing
= !state
;
1072 if (term
->ldisc
) /* cause ldisc to notice changes */
1073 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1075 case 20: /* Return sends ... */
1076 term
->cr_lf_return
= state
;
1078 case 34: /* Make cursor BIG */
1079 compatibility2(OTHER
, VT220
);
1080 term
->big_cursor
= !state
;
1085 * Process an OSC sequence: set window title or icon name.
1087 static void do_osc(Terminal
*term
)
1090 while (term
->osc_strlen
--)
1091 term
->wordness
[(unsigned char)
1092 term
->osc_string
[term
->osc_strlen
]] = term
->esc_args
[0];
1094 term
->osc_string
[term
->osc_strlen
] = '\0';
1095 switch (term
->esc_args
[0]) {
1098 if (!term
->cfg
->no_remote_wintitle
)
1099 set_icon(term
->frontend
, term
->osc_string
);
1100 if (term
->esc_args
[0] == 1)
1102 /* fall through: parameter 0 means set both */
1105 if (!term
->cfg
->no_remote_wintitle
)
1106 set_title(term
->frontend
, term
->osc_string
);
1113 * ANSI printing routines.
1115 static void term_print_setup(Terminal
*term
)
1117 bufchain_clear(&term
->printer_buf
);
1118 term
->print_job
= printer_start_job(term
->cfg
->printer
);
1120 static void term_print_flush(Terminal
*term
)
1125 while ((size
= bufchain_size(&term
->printer_buf
)) > 5) {
1126 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
1129 printer_job_data(term
->print_job
, data
, len
);
1130 bufchain_consume(&term
->printer_buf
, len
);
1133 static void term_print_finish(Terminal
*term
)
1139 if (!term
->printing
&& !term
->only_printing
)
1140 return; /* we need do nothing */
1142 term_print_flush(term
);
1143 while ((size
= bufchain_size(&term
->printer_buf
)) > 0) {
1144 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
1146 if (c
== '\033' || c
== '\233') {
1147 bufchain_consume(&term
->printer_buf
, size
);
1150 printer_job_data(term
->print_job
, &c
, 1);
1151 bufchain_consume(&term
->printer_buf
, 1);
1154 printer_finish_job(term
->print_job
);
1155 term
->print_job
= NULL
;
1156 term
->printing
= term
->only_printing
= FALSE
;
1160 * Remove everything currently in `inbuf' and stick it up on the
1161 * in-memory display. There's a big state machine in here to
1162 * process escape sequences...
1164 void term_out(Terminal
*term
)
1167 unsigned char localbuf
[256], *chars
;
1172 chars
= NULL
; /* placate compiler warnings */
1173 while (nchars
> 0 || bufchain_size(&term
->inbuf
) > 0) {
1177 bufchain_prefix(&term
->inbuf
, &ret
, &nchars
);
1178 if (nchars
> sizeof(localbuf
))
1179 nchars
= sizeof(localbuf
);
1180 memcpy(localbuf
, ret
, nchars
);
1181 bufchain_consume(&term
->inbuf
, nchars
);
1183 assert(chars
!= NULL
);
1189 * Optionally log the session traffic to a file. Useful for
1190 * debugging and possibly also useful for actual logging.
1192 if (term
->cfg
->logtype
== LGTYP_DEBUG
&& term
->logctx
)
1193 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_DEBUG
);
1199 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1200 * be able to display 8-bit characters, but I'll let that go 'cause
1205 * If we're printing, add the character to the printer
1208 if (term
->printing
) {
1209 bufchain_add(&term
->printer_buf
, &c
, 1);
1212 * If we're in print-only mode, we use a much simpler
1213 * state machine designed only to recognise the ESC[4i
1214 * termination sequence.
1216 if (term
->only_printing
) {
1218 term
->print_state
= 1;
1219 else if (c
== (unsigned char)'\233')
1220 term
->print_state
= 2;
1221 else if (c
== '[' && term
->print_state
== 1)
1222 term
->print_state
= 2;
1223 else if (c
== '4' && term
->print_state
== 2)
1224 term
->print_state
= 3;
1225 else if (c
== 'i' && term
->print_state
== 3)
1226 term
->print_state
= 4;
1228 term
->print_state
= 0;
1229 if (term
->print_state
== 4) {
1230 term_print_finish(term
);
1236 /* First see about all those translations. */
1237 if (term
->termstate
== TOPLEVEL
) {
1239 switch (term
->utf_state
) {
1242 /* UTF-8 must be stateless so we ignore iso2022. */
1243 if (unitab_ctrl
[c
] != 0xFF)
1245 else c
= ((unsigned char)c
) | ATTR_ASCII
;
1247 } else if ((c
& 0xe0) == 0xc0) {
1248 term
->utf_size
= term
->utf_state
= 1;
1249 term
->utf_char
= (c
& 0x1f);
1250 } else if ((c
& 0xf0) == 0xe0) {
1251 term
->utf_size
= term
->utf_state
= 2;
1252 term
->utf_char
= (c
& 0x0f);
1253 } else if ((c
& 0xf8) == 0xf0) {
1254 term
->utf_size
= term
->utf_state
= 3;
1255 term
->utf_char
= (c
& 0x07);
1256 } else if ((c
& 0xfc) == 0xf8) {
1257 term
->utf_size
= term
->utf_state
= 4;
1258 term
->utf_char
= (c
& 0x03);
1259 } else if ((c
& 0xfe) == 0xfc) {
1260 term
->utf_size
= term
->utf_state
= 5;
1261 term
->utf_char
= (c
& 0x01);
1272 if ((c
& 0xC0) != 0x80) {
1275 term
->utf_state
= 0;
1278 term
->utf_char
= (term
->utf_char
<< 6) | (c
& 0x3f);
1279 if (--term
->utf_state
)
1284 /* Is somebody trying to be evil! */
1286 (c
< 0x800 && term
->utf_size
>= 2) ||
1287 (c
< 0x10000 && term
->utf_size
>= 3) ||
1288 (c
< 0x200000 && term
->utf_size
>= 4) ||
1289 (c
< 0x4000000 && term
->utf_size
>= 5))
1292 /* Unicode line separator and paragraph separator are CR-LF */
1293 if (c
== 0x2028 || c
== 0x2029)
1296 /* High controls are probably a Baaad idea too. */
1300 /* The UTF-16 surrogates are not nice either. */
1301 /* The standard give the option of decoding these:
1302 * I don't want to! */
1303 if (c
>= 0xD800 && c
< 0xE000)
1306 /* ISO 10646 characters now limited to UTF-16 range. */
1310 /* This is currently a TagPhobic application.. */
1311 if (c
>= 0xE0000 && c
<= 0xE007F)
1314 /* U+FEFF is best seen as a null. */
1317 /* But U+FFFE is an error. */
1318 if (c
== 0xFFFE || c
== 0xFFFF)
1321 /* Oops this is a 16bit implementation */
1326 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1327 else if(term
->sco_acs
&&
1328 (c
!='\033' && c
!='\012' && c
!='\015' && c
!='\b'))
1330 if (term
->sco_acs
== 2) c
|= 0x80;
1333 switch (term
->cset_attr
[term
->cset
]) {
1335 * Linedraw characters are different from 'ESC ( B'
1336 * only for a small range. For ones outside that
1337 * range, make sure we use the same font as well as
1338 * the same encoding.
1341 if (unitab_ctrl
[c
] != 0xFF)
1344 c
= ((unsigned char) c
) | ATTR_LINEDRW
;
1348 /* If UK-ASCII, make the '#' a LineDraw Pound */
1350 c
= '}' | ATTR_LINEDRW
;
1353 /*FALLTHROUGH*/ case ATTR_ASCII
:
1354 if (unitab_ctrl
[c
] != 0xFF)
1357 c
= ((unsigned char) c
) | ATTR_ASCII
;
1360 if (c
>=' ') c
= ((unsigned char)c
) | ATTR_SCOACS
;
1366 /* How about C1 controls ? */
1367 if ((c
& -32) == 0x80 && term
->termstate
< DO_CTRLS
&&
1368 !term
->vt52_mode
&& has_compat(VT220
)) {
1369 term
->termstate
= SEEN_ESC
;
1370 term
->esc_query
= FALSE
;
1371 c
= '@' + (c
& 0x1F);
1374 /* Or the GL control. */
1375 if (c
== '\177' && term
->termstate
< DO_CTRLS
&& has_compat(OTHER
)) {
1376 if (term
->curs
.x
&& !term
->wrapnext
)
1378 term
->wrapnext
= FALSE
;
1380 if (!term
->cfg
->no_dbackspace
) /* destructive bksp might be disabled */
1381 *term
->cpos
= (' ' | term
->curr_attr
| ATTR_ASCII
);
1383 /* Or normal C0 controls. */
1384 if ((c
& -32) == 0 && term
->termstate
< DO_CTRLS
) {
1386 case '\005': /* terminal type query */
1387 /* Strictly speaking this is VT100 but a VT100 defaults to
1388 * no response. Other terminals respond at their option.
1390 * Don't put a CR in the default string as this tends to
1391 * upset some weird software.
1393 * An xterm returns "xterm" (5 characters)
1395 compatibility(ANSIMIN
);
1397 char abuf
[256], *s
, *d
;
1399 for (s
= term
->cfg
->answerback
, d
= abuf
; *s
; s
++) {
1401 if (*s
>= 'a' && *s
<= 'z')
1402 *d
++ = (*s
- ('a' - 1));
1403 else if ((*s
>= '@' && *s
<= '_') ||
1404 *s
== '?' || (*s
& 0x80))
1409 } else if (*s
== '^') {
1414 lpage_send(term
->ldisc
, DEFAULT_CODEPAGE
,
1420 struct beeptime
*newbeep
;
1421 unsigned long ticks
;
1423 ticks
= GETTICKCOUNT();
1425 if (!term
->beep_overloaded
) {
1426 newbeep
= smalloc(sizeof(struct beeptime
));
1427 newbeep
->ticks
= ticks
;
1428 newbeep
->next
= NULL
;
1429 if (!term
->beephead
)
1430 term
->beephead
= newbeep
;
1432 term
->beeptail
->next
= newbeep
;
1433 term
->beeptail
= newbeep
;
1438 * Throw out any beeps that happened more than
1441 while (term
->beephead
&&
1442 term
->beephead
->ticks
< ticks
- term
->cfg
->bellovl_t
) {
1443 struct beeptime
*tmp
= term
->beephead
;
1444 term
->beephead
= tmp
->next
;
1446 if (!term
->beephead
)
1447 term
->beeptail
= NULL
;
1451 if (term
->cfg
->bellovl
&& term
->beep_overloaded
&&
1452 ticks
- term
->lastbeep
>= (unsigned)term
->cfg
->bellovl_s
) {
1454 * If we're currently overloaded and the
1455 * last beep was more than s seconds ago,
1456 * leave overload mode.
1458 term
->beep_overloaded
= FALSE
;
1459 } else if (term
->cfg
->bellovl
&& !term
->beep_overloaded
&&
1460 term
->nbeeps
>= term
->cfg
->bellovl_n
) {
1462 * Now, if we have n or more beeps
1463 * remaining in the queue, go into overload
1466 term
->beep_overloaded
= TRUE
;
1468 term
->lastbeep
= ticks
;
1471 * Perform an actual beep if we're not overloaded.
1473 if (!term
->cfg
->bellovl
|| !term
->beep_overloaded
) {
1474 beep(term
->frontend
, term
->cfg
->beep
);
1475 if (term
->cfg
->beep
== BELL_VISUAL
) {
1476 term
->in_vbell
= TRUE
;
1477 term
->vbell_startpoint
= ticks
;
1485 if (term
->curs
.x
== 0 &&
1486 (term
->curs
.y
== 0 || term
->wrap
== 0))
1488 else if (term
->curs
.x
== 0 && term
->curs
.y
> 0)
1489 term
->curs
.x
= term
->cols
- 1, term
->curs
.y
--;
1490 else if (term
->wrapnext
)
1491 term
->wrapnext
= FALSE
;
1495 term
->seen_disp_event
= TRUE
;
1498 compatibility(VT100
);
1502 compatibility(VT100
);
1506 if (term
->vt52_mode
)
1507 term
->termstate
= VT52_ESC
;
1509 compatibility(ANSIMIN
);
1510 term
->termstate
= SEEN_ESC
;
1511 term
->esc_query
= FALSE
;
1516 term
->wrapnext
= FALSE
;
1518 term
->seen_disp_event
= TRUE
;
1519 term
->paste_hold
= 0;
1521 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
1524 if (has_compat(SCOANSI
)) {
1525 move(term
, 0, 0, 0);
1526 erase_lots(term
, FALSE
, FALSE
, TRUE
);
1528 term
->wrapnext
= FALSE
;
1529 term
->seen_disp_event
= 1;
1533 compatibility(VT100
);
1535 if (term
->curs
.y
== term
->marg_b
)
1536 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1537 else if (term
->curs
.y
< term
->rows
- 1)
1539 if (term
->cfg
->lfhascr
)
1542 term
->wrapnext
= FALSE
;
1543 term
->seen_disp_event
= 1;
1544 term
->paste_hold
= 0;
1546 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
1550 pos old_curs
= term
->curs
;
1551 unsigned long *ldata
= lineptr(term
->curs
.y
);
1555 } while (term
->curs
.x
< term
->cols
- 1 &&
1556 !term
->tabs
[term
->curs
.x
]);
1558 if ((ldata
[term
->cols
] & LATTR_MODE
) != LATTR_NORM
) {
1559 if (term
->curs
.x
>= term
->cols
/ 2)
1560 term
->curs
.x
= term
->cols
/ 2 - 1;
1562 if (term
->curs
.x
>= term
->cols
)
1563 term
->curs
.x
= term
->cols
- 1;
1567 check_selection(term
, old_curs
, term
->curs
);
1569 term
->seen_disp_event
= TRUE
;
1573 switch (term
->termstate
) {
1575 /* Only graphic characters get this far;
1576 * ctrls are stripped above */
1577 if (term
->wrapnext
&& term
->wrap
) {
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
;
1589 if (term
->selstate
!= NO_SELECTION
) {
1590 pos cursplus
= term
->curs
;
1592 check_selection(term
, term
->curs
, cursplus
);
1594 if (((c
& CSET_MASK
) == ATTR_ASCII
|| (c
& CSET_MASK
) == 0) &&
1596 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
1602 width
= wcwidth((wchar_t) c
);
1606 * If we're about to display a double-width
1607 * character starting in the rightmost
1608 * column, then we do something special
1609 * instead. We must print a space in the
1610 * last column of the screen, then wrap;
1611 * and we also set LATTR_WRAPPED2 which
1612 * instructs subsequent cut-and-pasting not
1613 * only to splice this line to the one
1614 * after it, but to ignore the space in the
1615 * last character position as well.
1616 * (Because what was actually output to the
1617 * terminal was presumably just a sequence
1618 * of CJK characters, and we don't want a
1619 * space to be pasted in the middle of
1620 * those just because they had the
1621 * misfortune to start in the wrong parity
1622 * column. xterm concurs.)
1624 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1625 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
1626 if (term
->curs
.x
== term
->cols
-1) {
1627 *term
->cpos
++ = ATTR_ASCII
| ' ' | term
->curr_attr
;
1628 *term
->cpos
|= LATTR_WRAPPED
| LATTR_WRAPPED2
;
1629 if (term
->curs
.y
== term
->marg_b
)
1630 scroll(term
, term
->marg_t
, term
->marg_b
,
1632 else if (term
->curs
.y
< term
->rows
- 1)
1636 /* Now we must check_boundary again, of course. */
1637 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1638 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
1640 *term
->cpos
++ = c
| term
->curr_attr
;
1641 *term
->cpos
++ = UCSWIDE
| term
->curr_attr
;
1645 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1646 check_boundary(term
, term
->curs
.x
+1, term
->curs
.y
);
1647 *term
->cpos
++ = c
| term
->curr_attr
;
1654 if (term
->curs
.x
== term
->cols
) {
1657 term
->wrapnext
= TRUE
;
1658 if (term
->wrap
&& term
->vt52_mode
) {
1659 term
->cpos
[1] |= LATTR_WRAPPED
;
1660 if (term
->curs
.y
== term
->marg_b
)
1661 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1662 else if (term
->curs
.y
< term
->rows
- 1)
1666 term
->wrapnext
= FALSE
;
1669 term
->seen_disp_event
= 1;
1674 * This state is virtually identical to SEEN_ESC, with the
1675 * exception that we have an OSC sequence in the pipeline,
1676 * and _if_ we see a backslash, we process it.
1680 term
->termstate
= TOPLEVEL
;
1683 /* else fall through */
1685 if (c
>= ' ' && c
<= '/') {
1686 if (term
->esc_query
)
1687 term
->esc_query
= -1;
1689 term
->esc_query
= c
;
1692 term
->termstate
= TOPLEVEL
;
1693 switch (ANSI(c
, term
->esc_query
)) {
1694 case '[': /* enter CSI mode */
1695 term
->termstate
= SEEN_CSI
;
1696 term
->esc_nargs
= 1;
1697 term
->esc_args
[0] = ARG_DEFAULT
;
1698 term
->esc_query
= FALSE
;
1700 case ']': /* xterm escape sequences */
1701 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1702 compatibility(OTHER
);
1703 term
->termstate
= SEEN_OSC
;
1704 term
->esc_args
[0] = 0;
1706 case '7': /* save cursor */
1707 compatibility(VT100
);
1708 save_cursor(term
, TRUE
);
1710 case '8': /* restore cursor */
1711 compatibility(VT100
);
1712 save_cursor(term
, FALSE
);
1713 term
->seen_disp_event
= TRUE
;
1716 compatibility(VT100
);
1717 term
->app_keypad_keys
= TRUE
;
1720 compatibility(VT100
);
1721 term
->app_keypad_keys
= FALSE
;
1723 case 'D': /* exactly equivalent to LF */
1724 compatibility(VT100
);
1725 if (term
->curs
.y
== term
->marg_b
)
1726 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1727 else if (term
->curs
.y
< term
->rows
- 1)
1730 term
->wrapnext
= FALSE
;
1731 term
->seen_disp_event
= TRUE
;
1733 case 'E': /* exactly equivalent to CR-LF */
1734 compatibility(VT100
);
1736 if (term
->curs
.y
== term
->marg_b
)
1737 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1738 else if (term
->curs
.y
< term
->rows
- 1)
1741 term
->wrapnext
= FALSE
;
1742 term
->seen_disp_event
= TRUE
;
1744 case 'M': /* reverse index - backwards LF */
1745 compatibility(VT100
);
1746 if (term
->curs
.y
== term
->marg_t
)
1747 scroll(term
, term
->marg_t
, term
->marg_b
, -1, TRUE
);
1748 else if (term
->curs
.y
> 0)
1751 term
->wrapnext
= FALSE
;
1752 term
->seen_disp_event
= TRUE
;
1754 case 'Z': /* terminal type query */
1755 compatibility(VT100
);
1757 ldisc_send(term
->ldisc
, term
->id_string
,
1758 strlen(term
->id_string
), 0);
1760 case 'c': /* restore power-on settings */
1761 compatibility(VT100
);
1763 if (term
->ldisc
) /* cause ldisc to notice changes */
1764 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1765 if (term
->reset_132
) {
1766 if (!term
->cfg
->no_remote_resize
)
1767 request_resize(term
->frontend
, 80, term
->rows
);
1768 term
->reset_132
= 0;
1772 term
->seen_disp_event
= TRUE
;
1774 case 'H': /* set a tab */
1775 compatibility(VT100
);
1776 term
->tabs
[term
->curs
.x
] = TRUE
;
1779 case ANSI('8', '#'): /* ESC # 8 fills screen with Es :-) */
1780 compatibility(VT100
);
1782 unsigned long *ldata
;
1786 for (i
= 0; i
< term
->rows
; i
++) {
1788 for (j
= 0; j
< term
->cols
; j
++)
1789 ldata
[j
] = ATTR_DEFAULT
| 'E';
1790 ldata
[term
->cols
] = 0;
1793 term
->seen_disp_event
= TRUE
;
1794 scrtop
.x
= scrtop
.y
= 0;
1796 scrbot
.y
= term
->rows
;
1797 check_selection(term
, scrtop
, scrbot
);
1801 case ANSI('3', '#'):
1802 case ANSI('4', '#'):
1803 case ANSI('5', '#'):
1804 case ANSI('6', '#'):
1805 compatibility(VT100
);
1807 unsigned long nlattr
;
1808 unsigned long *ldata
;
1809 switch (ANSI(c
, term
->esc_query
)) {
1810 case ANSI('3', '#'):
1813 case ANSI('4', '#'):
1816 case ANSI('5', '#'):
1817 nlattr
= LATTR_NORM
;
1819 default: /* spiritually case ANSI('6', '#'): */
1820 nlattr
= LATTR_WIDE
;
1823 ldata
= lineptr(term
->curs
.y
);
1824 ldata
[term
->cols
] &= ~LATTR_MODE
;
1825 ldata
[term
->cols
] |= nlattr
;
1829 case ANSI('A', '('):
1830 compatibility(VT100
);
1831 if (!term
->cfg
->no_remote_charset
)
1832 term
->cset_attr
[0] = ATTR_GBCHR
;
1834 case ANSI('B', '('):
1835 compatibility(VT100
);
1836 if (!term
->cfg
->no_remote_charset
)
1837 term
->cset_attr
[0] = ATTR_ASCII
;
1839 case ANSI('0', '('):
1840 compatibility(VT100
);
1841 if (!term
->cfg
->no_remote_charset
)
1842 term
->cset_attr
[0] = ATTR_LINEDRW
;
1844 case ANSI('U', '('):
1845 compatibility(OTHER
);
1846 if (!term
->cfg
->no_remote_charset
)
1847 term
->cset_attr
[0] = ATTR_SCOACS
;
1850 case ANSI('A', ')'):
1851 compatibility(VT100
);
1852 if (!term
->cfg
->no_remote_charset
)
1853 term
->cset_attr
[1] = ATTR_GBCHR
;
1855 case ANSI('B', ')'):
1856 compatibility(VT100
);
1857 if (!term
->cfg
->no_remote_charset
)
1858 term
->cset_attr
[1] = ATTR_ASCII
;
1860 case ANSI('0', ')'):
1861 compatibility(VT100
);
1862 if (!term
->cfg
->no_remote_charset
)
1863 term
->cset_attr
[1] = ATTR_LINEDRW
;
1865 case ANSI('U', ')'):
1866 compatibility(OTHER
);
1867 if (!term
->cfg
->no_remote_charset
)
1868 term
->cset_attr
[1] = ATTR_SCOACS
;
1871 case ANSI('8', '%'): /* Old Linux code */
1872 case ANSI('G', '%'):
1873 compatibility(OTHER
);
1874 if (!term
->cfg
->no_remote_charset
)
1877 case ANSI('@', '%'):
1878 compatibility(OTHER
);
1879 if (!term
->cfg
->no_remote_charset
)
1885 term
->termstate
= TOPLEVEL
; /* default */
1887 if (term
->esc_nargs
<= ARGS_MAX
) {
1888 if (term
->esc_args
[term
->esc_nargs
- 1] == ARG_DEFAULT
)
1889 term
->esc_args
[term
->esc_nargs
- 1] = 0;
1890 term
->esc_args
[term
->esc_nargs
- 1] =
1891 10 * term
->esc_args
[term
->esc_nargs
- 1] + c
- '0';
1893 term
->termstate
= SEEN_CSI
;
1894 } else if (c
== ';') {
1895 if (++term
->esc_nargs
<= ARGS_MAX
)
1896 term
->esc_args
[term
->esc_nargs
- 1] = ARG_DEFAULT
;
1897 term
->termstate
= SEEN_CSI
;
1898 } else if (c
< '@') {
1899 if (term
->esc_query
)
1900 term
->esc_query
= -1;
1902 term
->esc_query
= TRUE
;
1904 term
->esc_query
= c
;
1905 term
->termstate
= SEEN_CSI
;
1907 switch (ANSI(c
, term
->esc_query
)) {
1908 case 'A': /* move up N lines */
1909 move(term
, term
->curs
.x
,
1910 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
1911 term
->seen_disp_event
= TRUE
;
1913 case 'e': /* move down N lines */
1914 compatibility(ANSI
);
1917 move(term
, term
->curs
.x
,
1918 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
1919 term
->seen_disp_event
= TRUE
;
1921 case ANSI('c', '>'): /* report xterm version */
1922 compatibility(OTHER
);
1923 /* this reports xterm version 136 so that VIM can
1924 use the drag messages from the mouse reporting */
1926 ldisc_send(term
->ldisc
, "\033[>0;136;0c", 11, 0);
1928 case 'a': /* move right N cols */
1929 compatibility(ANSI
);
1932 move(term
, term
->curs
.x
+ def(term
->esc_args
[0], 1),
1934 term
->seen_disp_event
= TRUE
;
1936 case 'D': /* move left N cols */
1937 move(term
, term
->curs
.x
- def(term
->esc_args
[0], 1),
1939 term
->seen_disp_event
= TRUE
;
1941 case 'E': /* move down N lines and CR */
1942 compatibility(ANSI
);
1944 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
1945 term
->seen_disp_event
= TRUE
;
1947 case 'F': /* move up N lines and CR */
1948 compatibility(ANSI
);
1950 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
1951 term
->seen_disp_event
= TRUE
;
1954 case '`': /* set horizontal posn */
1955 compatibility(ANSI
);
1956 move(term
, def(term
->esc_args
[0], 1) - 1,
1958 term
->seen_disp_event
= TRUE
;
1960 case 'd': /* set vertical posn */
1961 compatibility(ANSI
);
1962 move(term
, term
->curs
.x
,
1963 ((term
->dec_om ? term
->marg_t
: 0) +
1964 def(term
->esc_args
[0], 1) - 1),
1965 (term
->dec_om ?
2 : 0));
1966 term
->seen_disp_event
= TRUE
;
1969 case 'f': /* set horz and vert posns at once */
1970 if (term
->esc_nargs
< 2)
1971 term
->esc_args
[1] = ARG_DEFAULT
;
1972 move(term
, def(term
->esc_args
[1], 1) - 1,
1973 ((term
->dec_om ? term
->marg_t
: 0) +
1974 def(term
->esc_args
[0], 1) - 1),
1975 (term
->dec_om ?
2 : 0));
1976 term
->seen_disp_event
= TRUE
;
1978 case 'J': /* erase screen or parts of it */
1980 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
1983 erase_lots(term
, FALSE
, !!(i
& 2), !!(i
& 1));
1986 term
->seen_disp_event
= TRUE
;
1988 case 'K': /* erase line or parts of it */
1990 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
1993 erase_lots(term
, TRUE
, !!(i
& 2), !!(i
& 1));
1995 term
->seen_disp_event
= TRUE
;
1997 case 'L': /* insert lines */
1998 compatibility(VT102
);
1999 if (term
->curs
.y
<= term
->marg_b
)
2000 scroll(term
, term
->curs
.y
, term
->marg_b
,
2001 -def(term
->esc_args
[0], 1), FALSE
);
2003 term
->seen_disp_event
= TRUE
;
2005 case 'M': /* delete lines */
2006 compatibility(VT102
);
2007 if (term
->curs
.y
<= term
->marg_b
)
2008 scroll(term
, term
->curs
.y
, term
->marg_b
,
2009 def(term
->esc_args
[0], 1),
2012 term
->seen_disp_event
= TRUE
;
2014 case '@': /* insert chars */
2015 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2016 compatibility(VT102
);
2017 insch(term
, def(term
->esc_args
[0], 1));
2018 term
->seen_disp_event
= TRUE
;
2020 case 'P': /* delete chars */
2021 compatibility(VT102
);
2022 insch(term
, -def(term
->esc_args
[0], 1));
2023 term
->seen_disp_event
= TRUE
;
2025 case 'c': /* terminal type query */
2026 compatibility(VT100
);
2027 /* This is the response for a VT102 */
2029 ldisc_send(term
->ldisc
, term
->id_string
,
2030 strlen(term
->id_string
), 0);
2032 case 'n': /* cursor position query */
2034 if (term
->esc_args
[0] == 6) {
2036 sprintf(buf
, "\033[%d;%dR", term
->curs
.y
+ 1,
2038 ldisc_send(term
->ldisc
, buf
, strlen(buf
), 0);
2039 } else if (term
->esc_args
[0] == 5) {
2040 ldisc_send(term
->ldisc
, "\033[0n", 4, 0);
2044 case 'h': /* toggle modes to high */
2046 compatibility(VT100
);
2049 for (i
= 0; i
< term
->esc_nargs
; i
++)
2050 toggle_mode(term
, term
->esc_args
[i
],
2051 term
->esc_query
, TRUE
);
2056 compatibility(VT100
);
2058 if (term
->esc_nargs
!= 1) break;
2059 if (term
->esc_args
[0] == 5 && *term
->cfg
->printer
) {
2060 term
->printing
= TRUE
;
2061 term
->only_printing
= !term
->esc_query
;
2062 term
->print_state
= 0;
2063 term_print_setup(term
);
2064 } else if (term
->esc_args
[0] == 4 &&
2066 term_print_finish(term
);
2070 case 'l': /* toggle modes to low */
2072 compatibility(VT100
);
2075 for (i
= 0; i
< term
->esc_nargs
; i
++)
2076 toggle_mode(term
, term
->esc_args
[i
],
2077 term
->esc_query
, FALSE
);
2080 case 'g': /* clear tabs */
2081 compatibility(VT100
);
2082 if (term
->esc_nargs
== 1) {
2083 if (term
->esc_args
[0] == 0) {
2084 term
->tabs
[term
->curs
.x
] = FALSE
;
2085 } else if (term
->esc_args
[0] == 3) {
2087 for (i
= 0; i
< term
->cols
; i
++)
2088 term
->tabs
[i
] = FALSE
;
2092 case 'r': /* set scroll margins */
2093 compatibility(VT100
);
2094 if (term
->esc_nargs
<= 2) {
2096 top
= def(term
->esc_args
[0], 1) - 1;
2097 bot
= (term
->esc_nargs
<= 1
2098 || term
->esc_args
[1] == 0 ?
2100 def(term
->esc_args
[1], term
->rows
)) - 1;
2101 if (bot
>= term
->rows
)
2102 bot
= term
->rows
- 1;
2103 /* VTTEST Bug 9 - if region is less than 2 lines
2104 * don't change region.
2106 if (bot
- top
> 0) {
2111 * I used to think the cursor should be
2112 * placed at the top of the newly marginned
2113 * area. Apparently not: VMS TPU falls over
2116 * Well actually it should for
2119 term
->curs
.y
= (term
->dec_om ?
2122 term
->seen_disp_event
= TRUE
;
2126 case 'm': /* set graphics rendition */
2129 * A VT100 without the AVO only had one
2130 * attribute, either underline or
2131 * reverse video depending on the
2132 * cursor type, this was selected by
2136 * This is sometimes DIM, eg on the
2139 * This is sometimes INVIS various ANSI.
2141 * This like 22 disables BOLD, DIM and INVIS
2143 * The ANSI colours appear on any
2144 * terminal that has colour (obviously)
2145 * but the interaction between sgr0 and
2146 * the colours varies but is usually
2147 * related to the background colour
2148 * erase item. The interaction between
2149 * colour attributes and the mono ones
2150 * is also very implementation
2153 * The 39 and 49 attributes are likely
2154 * to be unimplemented.
2157 for (i
= 0; i
< term
->esc_nargs
; i
++) {
2158 switch (def(term
->esc_args
[i
], 0)) {
2159 case 0: /* restore defaults */
2160 term
->curr_attr
= ATTR_DEFAULT
;
2162 case 1: /* enable bold */
2163 compatibility(VT100AVO
);
2164 term
->curr_attr
|= ATTR_BOLD
;
2166 case 21: /* (enable double underline) */
2167 compatibility(OTHER
);
2168 case 4: /* enable underline */
2169 compatibility(VT100AVO
);
2170 term
->curr_attr
|= ATTR_UNDER
;
2172 case 5: /* enable blink */
2173 compatibility(VT100AVO
);
2174 term
->curr_attr
|= ATTR_BLINK
;
2176 case 7: /* enable reverse video */
2177 term
->curr_attr
|= ATTR_REVERSE
;
2179 case 10: /* SCO acs off */
2180 compatibility(SCOANSI
);
2181 if (term
->cfg
->no_remote_charset
) break;
2182 term
->sco_acs
= 0; break;
2183 case 11: /* SCO acs on */
2184 compatibility(SCOANSI
);
2185 if (term
->cfg
->no_remote_charset
) break;
2186 term
->sco_acs
= 1; break;
2187 case 12: /* SCO acs on, |0x80 */
2188 compatibility(SCOANSI
);
2189 if (term
->cfg
->no_remote_charset
) break;
2190 term
->sco_acs
= 2; break;
2191 case 22: /* disable bold */
2192 compatibility2(OTHER
, VT220
);
2193 term
->curr_attr
&= ~ATTR_BOLD
;
2195 case 24: /* disable underline */
2196 compatibility2(OTHER
, VT220
);
2197 term
->curr_attr
&= ~ATTR_UNDER
;
2199 case 25: /* disable blink */
2200 compatibility2(OTHER
, VT220
);
2201 term
->curr_attr
&= ~ATTR_BLINK
;
2203 case 27: /* disable reverse video */
2204 compatibility2(OTHER
, VT220
);
2205 term
->curr_attr
&= ~ATTR_REVERSE
;
2216 term
->curr_attr
&= ~ATTR_FGMASK
;
2218 (term
->esc_args
[i
] - 30)<<ATTR_FGSHIFT
;
2220 case 39: /* default-foreground */
2221 term
->curr_attr
&= ~ATTR_FGMASK
;
2222 term
->curr_attr
|= ATTR_DEFFG
;
2233 term
->curr_attr
&= ~ATTR_BGMASK
;
2235 (term
->esc_args
[i
] - 40)<<ATTR_BGSHIFT
;
2237 case 49: /* default-background */
2238 term
->curr_attr
&= ~ATTR_BGMASK
;
2239 term
->curr_attr
|= ATTR_DEFBG
;
2244 term
->erase_char
= (' ' | ATTR_ASCII
|
2250 case 's': /* save cursor */
2251 save_cursor(term
, TRUE
);
2253 case 'u': /* restore cursor */
2254 save_cursor(term
, FALSE
);
2255 term
->seen_disp_event
= TRUE
;
2257 case 't': /* set page size - ie window height */
2259 * VT340/VT420 sequence DECSLPP, DEC only allows values
2260 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2261 * illegal values (eg first arg 1..9) for window changing
2264 if (term
->esc_nargs
<= 1
2265 && (term
->esc_args
[0] < 1 ||
2266 term
->esc_args
[0] >= 24)) {
2267 compatibility(VT340TEXT
);
2268 if (!term
->cfg
->no_remote_resize
)
2269 request_resize(term
->frontend
, term
->cols
,
2270 def(term
->esc_args
[0], 24));
2272 } else if (term
->esc_nargs
>= 1 &&
2273 term
->esc_args
[0] >= 1 &&
2274 term
->esc_args
[0] < 24) {
2275 compatibility(OTHER
);
2277 switch (term
->esc_args
[0]) {
2281 set_iconic(term
->frontend
, FALSE
);
2284 set_iconic(term
->frontend
, TRUE
);
2287 if (term
->esc_nargs
>= 3) {
2288 if (!term
->cfg
->no_remote_resize
)
2289 move_window(term
->frontend
,
2290 def(term
->esc_args
[1], 0),
2291 def(term
->esc_args
[2], 0));
2295 /* We should resize the window to a given
2296 * size in pixels here, but currently our
2297 * resizing code isn't healthy enough to
2302 set_zorder(term
->frontend
, TRUE
);
2305 /* move to bottom */
2306 set_zorder(term
->frontend
, FALSE
);
2309 refresh_window(term
->frontend
);
2312 if (term
->esc_nargs
>= 3) {
2313 if (!term
->cfg
->no_remote_resize
)
2314 request_resize(term
->frontend
,
2315 def(term
->esc_args
[2], term
->cfg
->width
),
2316 def(term
->esc_args
[1], term
->cfg
->height
));
2320 if (term
->esc_nargs
>= 2)
2321 set_zoomed(term
->frontend
,
2327 ldisc_send(term
->ldisc
,
2328 is_iconic(term
->frontend
) ?
2329 "\033[1t" : "\033[2t", 4, 0);
2333 get_window_pos(term
->frontend
, &x
, &y
);
2334 len
= sprintf(buf
, "\033[3;%d;%dt", x
, y
);
2335 ldisc_send(term
->ldisc
, buf
, len
, 0);
2340 get_window_pixels(term
->frontend
, &x
, &y
);
2341 len
= sprintf(buf
, "\033[4;%d;%dt", x
, y
);
2342 ldisc_send(term
->ldisc
, buf
, len
, 0);
2347 len
= sprintf(buf
, "\033[8;%d;%dt",
2348 term
->rows
, term
->cols
);
2349 ldisc_send(term
->ldisc
, buf
, len
, 0);
2354 * Hmmm. Strictly speaking we
2355 * should return `the size of the
2356 * screen in characters', but
2357 * that's not easy: (a) window
2358 * furniture being what it is it's
2359 * hard to compute, and (b) in
2360 * resize-font mode maximising the
2361 * window wouldn't change the
2362 * number of characters. *shrug*. I
2363 * think we'll ignore it for the
2364 * moment and see if anyone
2365 * complains, and then ask them
2366 * what they would like it to do.
2371 p
= get_window_title(term
->frontend
, TRUE
);
2373 ldisc_send(term
->ldisc
, "\033]L", 3, 0);
2374 ldisc_send(term
->ldisc
, p
, len
, 0);
2375 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
2380 p
= get_window_title(term
->frontend
,FALSE
);
2382 ldisc_send(term
->ldisc
, "\033]l", 3, 0);
2383 ldisc_send(term
->ldisc
, p
, len
, 0);
2384 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
2391 compatibility(SCOANSI
);
2392 scroll(term
, term
->marg_t
, term
->marg_b
,
2393 def(term
->esc_args
[0], 1), TRUE
);
2395 term
->wrapnext
= FALSE
;
2396 term
->seen_disp_event
= TRUE
;
2399 compatibility(SCOANSI
);
2400 scroll(term
, term
->marg_t
, term
->marg_b
,
2401 -def(term
->esc_args
[0], 1), TRUE
);
2403 term
->wrapnext
= FALSE
;
2404 term
->seen_disp_event
= TRUE
;
2406 case ANSI('|', '*'):
2407 /* VT420 sequence DECSNLS
2408 * Set number of lines on screen
2409 * VT420 uses VGA like hardware and can support any size in
2410 * reasonable range (24..49 AIUI) with no default specified.
2412 compatibility(VT420
);
2413 if (term
->esc_nargs
== 1 && term
->esc_args
[0] > 0) {
2414 if (!term
->cfg
->no_remote_resize
)
2415 request_resize(term
->frontend
, term
->cols
,
2416 def(term
->esc_args
[0],
2417 term
->cfg
->height
));
2421 case ANSI('|', '$'):
2422 /* VT340/VT420 sequence DECSCPP
2423 * Set number of columns per page
2424 * Docs imply range is only 80 or 132, but I'll allow any.
2426 compatibility(VT340TEXT
);
2427 if (term
->esc_nargs
<= 1) {
2428 if (!term
->cfg
->no_remote_resize
)
2429 request_resize(term
->frontend
,
2430 def(term
->esc_args
[0],
2431 term
->cfg
->width
), term
->rows
);
2435 case 'X': /* write N spaces w/o moving cursor */
2436 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
2437 compatibility(ANSIMIN
);
2439 int n
= def(term
->esc_args
[0], 1);
2441 unsigned long *p
= term
->cpos
;
2442 if (n
> term
->cols
- term
->curs
.x
)
2443 n
= term
->cols
- term
->curs
.x
;
2444 cursplus
= term
->curs
;
2446 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2447 check_boundary(term
, term
->curs
.x
+n
, term
->curs
.y
);
2448 check_selection(term
, term
->curs
, cursplus
);
2450 *p
++ = term
->erase_char
;
2451 term
->seen_disp_event
= TRUE
;
2454 case 'x': /* report terminal characteristics */
2455 compatibility(VT100
);
2458 int i
= def(term
->esc_args
[0], 0);
2459 if (i
== 0 || i
== 1) {
2460 strcpy(buf
, "\033[2;1;1;112;112;1;0x");
2462 ldisc_send(term
->ldisc
, buf
, 20, 0);
2466 case 'Z': /* BackTab for xterm */
2467 compatibility(OTHER
);
2469 int i
= def(term
->esc_args
[0], 1);
2470 pos old_curs
= term
->curs
;
2472 for(;i
>0 && term
->curs
.x
>0; i
--) {
2475 } while (term
->curs
.x
>0 &&
2476 !term
->tabs
[term
->curs
.x
]);
2479 check_selection(term
, old_curs
, term
->curs
);
2482 case ANSI('L', '='):
2483 compatibility(OTHER
);
2484 term
->use_bce
= (term
->esc_args
[0] <= 0);
2485 term
->erase_char
= ERASE_CHAR
;
2487 term
->erase_char
= (' ' | ATTR_ASCII
|
2489 (ATTR_FGMASK
| ATTR_BGMASK
)));
2491 case ANSI('E', '='):
2492 compatibility(OTHER
);
2493 term
->blink_is_real
= (term
->esc_args
[0] >= 1);
2495 case ANSI('p', '"'):
2497 * Allow the host to make this emulator a
2498 * 'perfect' VT102. This first appeared in
2499 * the VT220, but we do need to get back to
2500 * PuTTY mode so I won't check it.
2502 * The arg in 40..42,50 are a PuTTY extension.
2503 * The 2nd arg, 8bit vs 7bit is not checked.
2505 * Setting VT102 mode should also change
2506 * the Fkeys to generate PF* codes as a
2507 * real VT102 has no Fkeys. The VT220 does
2508 * this, F11..F13 become ESC,BS,LF other
2509 * Fkeys send nothing.
2511 * Note ESC c will NOT change this!
2514 switch (term
->esc_args
[0]) {
2516 term
->compatibility_level
&= ~TM_VTXXX
;
2517 term
->compatibility_level
|= TM_VT102
;
2520 term
->compatibility_level
&= ~TM_VTXXX
;
2521 term
->compatibility_level
|= TM_VT220
;
2525 if (term
->esc_args
[0] > 60 &&
2526 term
->esc_args
[0] < 70)
2527 term
->compatibility_level
|= TM_VTXXX
;
2531 term
->compatibility_level
&= TM_VTXXX
;
2534 term
->compatibility_level
= TM_PUTTY
;
2537 term
->compatibility_level
= TM_SCOANSI
;
2541 term
->compatibility_level
= TM_PUTTY
;
2547 /* Change the response to CSI c */
2548 if (term
->esc_args
[0] == 50) {
2551 strcpy(term
->id_string
, "\033[?");
2552 for (i
= 1; i
< term
->esc_nargs
; i
++) {
2554 strcat(term
->id_string
, ";");
2555 sprintf(lbuf
, "%d", term
->esc_args
[i
]);
2556 strcat(term
->id_string
, lbuf
);
2558 strcat(term
->id_string
, "c");
2561 /* Is this a good idea ?
2562 * Well we should do a soft reset at this point ...
2564 if (!has_compat(VT420
) && has_compat(VT100
)) {
2565 if (!term
->cfg
->no_remote_resize
) {
2566 if (term
->reset_132
)
2567 request_resize(132, 24);
2569 request_resize(80, 24);
2577 term
->osc_w
= FALSE
;
2579 case 'P': /* Linux palette sequence */
2580 term
->termstate
= SEEN_OSC_P
;
2581 term
->osc_strlen
= 0;
2583 case 'R': /* Linux palette reset */
2584 palette_reset(term
->frontend
);
2585 term_invalidate(term
);
2586 term
->termstate
= TOPLEVEL
;
2588 case 'W': /* word-set */
2589 term
->termstate
= SEEN_OSC_W
;
2602 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
2606 * Grotty hack to support xterm and DECterm title
2607 * sequences concurrently.
2609 if (term
->esc_args
[0] == 2) {
2610 term
->esc_args
[0] = 1;
2613 /* else fall through */
2615 term
->termstate
= OSC_STRING
;
2616 term
->osc_strlen
= 0;
2621 * This OSC stuff is EVIL. It takes just one character to get into
2622 * sysline mode and it's not initially obvious how to get out.
2623 * So I've added CR and LF as string aborts.
2624 * This shouldn't effect compatibility as I believe embedded
2625 * control characters are supposed to be interpreted (maybe?)
2626 * and they don't display anything useful anyway.
2630 if (c
== '\012' || c
== '\015') {
2631 term
->termstate
= TOPLEVEL
;
2632 } else if (c
== 0234 || c
== '\007') {
2634 * These characters terminate the string; ST and BEL
2635 * terminate the sequence and trigger instant
2636 * processing of it, whereas ESC goes back to SEEN_ESC
2637 * mode unless it is followed by \, in which case it is
2638 * synonymous with ST in the first place.
2641 term
->termstate
= TOPLEVEL
;
2642 } else if (c
== '\033')
2643 term
->termstate
= OSC_MAYBE_ST
;
2644 else if (term
->osc_strlen
< OSC_STR_MAX
)
2645 term
->osc_string
[term
->osc_strlen
++] = c
;
2649 int max
= (term
->osc_strlen
== 0 ?
21 : 16);
2651 if (c
>= '0' && c
<= '9')
2653 else if (c
>= 'A' && c
<= 'A' + max
- 10)
2655 else if (c
>= 'a' && c
<= 'a' + max
- 10)
2658 term
->termstate
= TOPLEVEL
;
2661 term
->osc_string
[term
->osc_strlen
++] = val
;
2662 if (term
->osc_strlen
>= 7) {
2663 palette_set(term
->frontend
, term
->osc_string
[0],
2664 term
->osc_string
[1] * 16 + term
->osc_string
[2],
2665 term
->osc_string
[3] * 16 + term
->osc_string
[4],
2666 term
->osc_string
[5] * 16 + term
->osc_string
[6]);
2667 term_invalidate(term
);
2668 term
->termstate
= TOPLEVEL
;
2684 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
2687 term
->termstate
= OSC_STRING
;
2688 term
->osc_strlen
= 0;
2692 term
->termstate
= TOPLEVEL
;
2693 term
->seen_disp_event
= TRUE
;
2696 move(term
, term
->curs
.x
, term
->curs
.y
- 1, 1);
2699 move(term
, term
->curs
.x
, term
->curs
.y
+ 1, 1);
2702 move(term
, term
->curs
.x
+ 1, term
->curs
.y
, 1);
2705 move(term
, term
->curs
.x
- 1, term
->curs
.y
, 1);
2708 * From the VT100 Manual
2709 * NOTE: The special graphics characters in the VT100
2710 * are different from those in the VT52
2712 * From VT102 manual:
2713 * 137 _ Blank - Same
2714 * 140 ` Reserved - Humm.
2715 * 141 a Solid rectangle - Similar
2716 * 142 b 1/ - Top half of fraction for the
2717 * 143 c 3/ - subscript numbers below.
2720 * 146 f Degrees - Same
2721 * 147 g Plus or minus - Same
2723 * 151 i Ellipsis (dots)
2726 * 154 l Bar at scan 0
2727 * 155 m Bar at scan 1
2728 * 156 n Bar at scan 2
2729 * 157 o Bar at scan 3 - Similar
2730 * 160 p Bar at scan 4 - Similar
2731 * 161 q Bar at scan 5 - Similar
2732 * 162 r Bar at scan 6 - Same
2733 * 163 s Bar at scan 7 - Similar
2748 term
->cset_attr
[term
->cset
= 0] = ATTR_LINEDRW
;
2751 term
->cset_attr
[term
->cset
= 0] = ATTR_ASCII
;
2754 move(term
, 0, 0, 0);
2757 if (term
->curs
.y
== 0)
2758 scroll(term
, 0, term
->rows
- 1, -1, TRUE
);
2759 else if (term
->curs
.y
> 0)
2762 term
->wrapnext
= FALSE
;
2765 erase_lots(term
, FALSE
, FALSE
, TRUE
);
2769 erase_lots(term
, TRUE
, FALSE
, TRUE
);
2773 /* XXX Print cursor line */
2776 /* XXX Start controller mode */
2779 /* XXX Stop controller mode */
2783 term
->termstate
= VT52_Y1
;
2787 ldisc_send(term
->ldisc
, "\033/Z", 3, 0);
2790 term
->app_keypad_keys
= TRUE
;
2793 term
->app_keypad_keys
= FALSE
;
2796 /* XXX This should switch to VT100 mode not current or default
2797 * VT mode. But this will only have effect in a VT220+
2800 term
->vt52_mode
= FALSE
;
2801 term
->blink_is_real
= term
->cfg
->blinktext
;
2805 /* XXX Enter auto print mode */
2808 /* XXX Exit auto print mode */
2811 /* XXX Print screen */
2817 /* compatibility(ATARI) */
2818 move(term
, 0, 0, 0);
2819 erase_lots(term
, FALSE
, FALSE
, TRUE
);
2823 /* compatibility(ATARI) */
2824 if (term
->curs
.y
<= term
->marg_b
)
2825 scroll(term
, term
->curs
.y
, term
->marg_b
, -1, FALSE
);
2828 /* compatibility(ATARI) */
2829 if (term
->curs
.y
<= term
->marg_b
)
2830 scroll(term
, term
->curs
.y
, term
->marg_b
, 1, TRUE
);
2833 /* compatibility(ATARI) */
2834 term
->termstate
= VT52_FG
;
2837 /* compatibility(ATARI) */
2838 term
->termstate
= VT52_BG
;
2841 /* compatibility(ATARI) */
2842 erase_lots(term
, FALSE
, TRUE
, FALSE
);
2846 /* compatibility(ATARI) */
2847 term
->cursor_on
= TRUE
;
2850 /* compatibility(ATARI) */
2851 term
->cursor_on
= FALSE
;
2853 /* case 'j': Save cursor position - broken on ST */
2854 /* case 'k': Restore cursor position */
2856 /* compatibility(ATARI) */
2857 erase_lots(term
, TRUE
, TRUE
, TRUE
);
2859 term
->wrapnext
= FALSE
;
2863 /* compatibility(ATARI) */
2864 erase_lots(term
, TRUE
, TRUE
, FALSE
);
2867 /* compatibility(ATARI) */
2868 term
->curr_attr
|= ATTR_REVERSE
;
2871 /* compatibility(ATARI) */
2872 term
->curr_attr
&= ~ATTR_REVERSE
;
2874 case 'v': /* wrap Autowrap on - Wyse style */
2875 /* compatibility(ATARI) */
2878 case 'w': /* Autowrap off */
2879 /* compatibility(ATARI) */
2884 /* compatibility(OTHER) */
2885 term
->vt52_bold
= FALSE
;
2886 term
->curr_attr
= ATTR_DEFAULT
;
2888 term
->erase_char
= (' ' | ATTR_ASCII
|
2890 (ATTR_FGMASK
| ATTR_BGMASK
)));
2893 /* compatibility(VI50) */
2894 term
->curr_attr
|= ATTR_UNDER
;
2897 /* compatibility(VI50) */
2898 term
->curr_attr
&= ~ATTR_UNDER
;
2901 /* compatibility(VI50) */
2902 term
->vt52_bold
= TRUE
;
2903 term
->curr_attr
|= ATTR_BOLD
;
2906 /* compatibility(VI50) */
2907 term
->vt52_bold
= FALSE
;
2908 term
->curr_attr
&= ~ATTR_BOLD
;
2914 term
->termstate
= VT52_Y2
;
2915 move(term
, term
->curs
.x
, c
- ' ', 0);
2918 term
->termstate
= TOPLEVEL
;
2919 move(term
, c
- ' ', term
->curs
.y
, 0);
2924 term
->termstate
= TOPLEVEL
;
2925 term
->curr_attr
&= ~ATTR_FGMASK
;
2926 term
->curr_attr
&= ~ATTR_BOLD
;
2927 term
->curr_attr
|= (c
& 0x7) << ATTR_FGSHIFT
;
2928 if ((c
& 0x8) || term
->vt52_bold
)
2929 term
->curr_attr
|= ATTR_BOLD
;
2932 term
->erase_char
= (' ' | ATTR_ASCII
|
2934 (ATTR_FGMASK
| ATTR_BGMASK
)));
2937 term
->termstate
= TOPLEVEL
;
2938 term
->curr_attr
&= ~ATTR_BGMASK
;
2939 term
->curr_attr
&= ~ATTR_BLINK
;
2940 term
->curr_attr
|= (c
& 0x7) << ATTR_BGSHIFT
;
2942 /* Note: bold background */
2944 term
->curr_attr
|= ATTR_BLINK
;
2947 term
->erase_char
= (' ' | ATTR_ASCII
|
2949 (ATTR_FGMASK
| ATTR_BGMASK
)));
2952 default: break; /* placate gcc warning about enum use */
2954 if (term
->selstate
!= NO_SELECTION
) {
2955 pos cursplus
= term
->curs
;
2957 check_selection(term
, term
->curs
, cursplus
);
2961 term_print_flush(term
);
2966 * Compare two lines to determine whether they are sufficiently
2967 * alike to scroll-optimise one to the other. Return the degree of
2970 static int linecmp(Terminal
*term
, unsigned long *a
, unsigned long *b
)
2974 for (i
= n
= 0; i
< term
->cols
; i
++)
2975 n
+= (*a
++ == *b
++);
2981 * Given a context, update the window. Out of paranoia, we don't
2982 * allow WM_PAINT responses to do scrolling optimisations.
2984 static void do_paint(Terminal
*term
, Context ctx
, int may_optimise
)
2986 int i
, j
, our_curs_y
, our_curs_x
;
2987 unsigned long rv
, cursor
;
2990 long cursor_background
= ERASE_CHAR
;
2991 unsigned long ticks
;
2994 * Check the visual bell state.
2996 if (term
->in_vbell
) {
2997 ticks
= GETTICKCOUNT();
2998 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
2999 term
->in_vbell
= FALSE
;
3002 rv
= (!term
->rvideo
^ !term
->in_vbell ? ATTR_REVERSE
: 0);
3005 * screen array, disptop, scrtop,
3007 * cfg.blinkpc, blink_is_real, tblinker,
3008 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
3011 /* Has the cursor position or type changed ? */
3012 if (term
->cursor_on
) {
3013 if (term
->has_focus
) {
3014 if (term
->blinker
|| !term
->cfg
->blink_cur
)
3015 cursor
= TATTR_ACTCURS
;
3019 cursor
= TATTR_PASCURS
;
3021 cursor
|= TATTR_RIGHTCURS
;
3024 our_curs_y
= term
->curs
.y
- term
->disptop
;
3027 * Adjust the cursor position in the case where it's
3028 * resting on the right-hand half of a CJK wide character.
3029 * xterm's behaviour here, which seems adequate to me, is
3030 * to display the cursor covering the _whole_ character,
3031 * exactly as if it were one space to the left.
3033 unsigned long *ldata
= lineptr(term
->curs
.y
);
3034 our_curs_x
= term
->curs
.x
;
3035 if (our_curs_x
> 0 &&
3036 (ldata
[our_curs_x
] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
3040 if (term
->dispcurs
&& (term
->curstype
!= cursor
||
3042 term
->disptext
+ our_curs_y
* (term
->cols
+ 1) +
3044 if (term
->dispcurs
> term
->disptext
&&
3045 (*term
->dispcurs
& (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
3046 term
->dispcurs
[-1] |= ATTR_INVALID
;
3047 if ( (term
->dispcurs
[1] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
3048 term
->dispcurs
[1] |= ATTR_INVALID
;
3049 *term
->dispcurs
|= ATTR_INVALID
;
3052 term
->dispcurs
= NULL
;
3054 /* The normal screen data */
3055 for (i
= 0; i
< term
->rows
; i
++) {
3056 unsigned long *ldata
;
3058 int idx
, dirty_line
, dirty_run
, selected
;
3059 unsigned long attr
= 0;
3060 int updated_line
= 0;
3063 int last_run_dirty
= 0;
3065 scrpos
.y
= i
+ term
->disptop
;
3066 ldata
= lineptr(scrpos
.y
);
3067 lattr
= (ldata
[term
->cols
] & LATTR_MODE
);
3069 idx
= i
* (term
->cols
+ 1);
3070 dirty_run
= dirty_line
= (ldata
[term
->cols
] !=
3071 term
->disptext
[idx
+ term
->cols
]);
3072 term
->disptext
[idx
+ term
->cols
] = ldata
[term
->cols
];
3074 for (j
= 0; j
< term
->cols
; j
++, idx
++) {
3075 unsigned long tattr
, tchar
;
3076 unsigned long *d
= ldata
+ j
;
3080 tchar
= (*d
& (CHAR_MASK
| CSET_MASK
));
3081 tattr
= (*d
& (ATTR_MASK
^ CSET_MASK
));
3082 switch (tchar
& CSET_MASK
) {
3084 tchar
= unitab_line
[tchar
& 0xFF];
3087 tchar
= unitab_xterm
[tchar
& 0xFF];
3090 tchar
= unitab_scoacs
[tchar
&0xFF];
3093 tattr
|= (tchar
& CSET_MASK
);
3095 if ((d
[1] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
3098 /* Video reversing things */
3099 if (term
->selstate
== DRAGGING
|| term
->selstate
== SELECTED
) {
3100 if (term
->seltype
== LEXICOGRAPHIC
)
3101 selected
= (posle(term
->selstart
, scrpos
) &&
3102 poslt(scrpos
, term
->selend
));
3104 selected
= (posPle(term
->selstart
, scrpos
) &&
3105 posPlt(scrpos
, term
->selend
));
3109 ^ (selected ? ATTR_REVERSE
: 0));
3111 /* 'Real' blinking ? */
3112 if (term
->blink_is_real
&& (tattr
& ATTR_BLINK
)) {
3113 if (term
->has_focus
&& term
->tblinker
) {
3115 tattr
&= ~CSET_MASK
;
3118 tattr
&= ~ATTR_BLINK
;
3122 * Check the font we'll _probably_ be using to see if
3123 * the character is wide when we don't want it to be.
3125 if ((tchar
| tattr
) != (term
->disptext
[idx
]& ~ATTR_NARROW
)) {
3126 if ((tattr
& ATTR_WIDE
) == 0 &&
3127 char_width(ctx
, (tchar
| tattr
) & 0xFFFF) == 2)
3128 tattr
|= ATTR_NARROW
;
3129 } else if (term
->disptext
[idx
]&ATTR_NARROW
)
3130 tattr
|= ATTR_NARROW
;
3132 /* Cursor here ? Save the 'background' */
3133 if (i
== our_curs_y
&& j
== our_curs_x
) {
3134 cursor_background
= tattr
| tchar
;
3135 term
->dispcurs
= term
->disptext
+ idx
;
3138 if ((term
->disptext
[idx
] ^ tattr
) & ATTR_WIDE
)
3141 break_run
= (((tattr
^ attr
) & term
->attr_mask
) ||
3142 j
- start
>= sizeof(ch
));
3144 /* Special hack for VT100 Linedraw glyphs */
3145 if ((attr
& CSET_MASK
) == 0x2300 && tchar
>= 0xBA
3146 && tchar
<= 0xBD) break_run
= TRUE
;
3148 if (!dbcs_screenfont
&& !dirty_line
) {
3149 if ((tchar
| tattr
) == term
->disptext
[idx
])
3151 else if (!dirty_run
&& ccount
== 1)
3156 if ((dirty_run
|| last_run_dirty
) && ccount
> 0) {
3157 do_text(ctx
, start
, i
, ch
, ccount
, attr
, lattr
);
3163 if (dbcs_screenfont
)
3164 last_run_dirty
= dirty_run
;
3165 dirty_run
= dirty_line
;
3168 if ((tchar
| tattr
) != term
->disptext
[idx
])
3170 ch
[ccount
++] = (char) tchar
;
3171 term
->disptext
[idx
] = tchar
| tattr
;
3173 /* If it's a wide char step along to the next one. */
3174 if (tattr
& ATTR_WIDE
) {
3175 if (++j
< term
->cols
) {
3179 * By construction above, the cursor should not
3180 * be on the right-hand half of this character.
3183 assert(!(i
== our_curs_y
&& j
== our_curs_x
));
3184 if (term
->disptext
[idx
] != *d
)
3186 term
->disptext
[idx
] = *d
;
3190 if (dirty_run
&& ccount
> 0) {
3191 do_text(ctx
, start
, i
, ch
, ccount
, attr
, lattr
);
3195 /* Cursor on this line ? (and changed) */
3196 if (i
== our_curs_y
&& (term
->curstype
!= cursor
|| updated_line
)) {
3197 ch
[0] = (char) (cursor_background
& CHAR_MASK
);
3198 attr
= (cursor_background
& ATTR_MASK
) | cursor
;
3199 do_cursor(ctx
, our_curs_x
, i
, ch
, 1, attr
, lattr
);
3200 term
->curstype
= cursor
;
3206 * Flick the switch that says if blinking things should be shown or hidden.
3209 void term_blink(Terminal
*term
, int flg
)
3211 long now
, blink_diff
;
3213 now
= GETTICKCOUNT();
3214 blink_diff
= now
- term
->last_tblink
;
3216 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3217 if (blink_diff
< 0 || blink_diff
> (TICKSPERSEC
* 9 / 20)) {
3218 term
->last_tblink
= now
;
3219 term
->tblinker
= !term
->tblinker
;
3224 term
->last_blink
= now
;
3228 blink_diff
= now
- term
->last_blink
;
3230 /* Make sure the cursor blinks no faster than system blink rate */
3231 if (blink_diff
>= 0 && blink_diff
< (long) CURSORBLINK
)
3234 term
->last_blink
= now
;
3235 term
->blinker
= !term
->blinker
;
3239 * Invalidate the whole screen so it will be repainted in full.
3241 void term_invalidate(Terminal
*term
)
3245 for (i
= 0; i
< term
->rows
* (term
->cols
+ 1); i
++)
3246 term
->disptext
[i
] = ATTR_INVALID
;
3250 * Paint the window in response to a WM_PAINT message.
3252 void term_paint(Terminal
*term
, Context ctx
,
3253 int left
, int top
, int right
, int bottom
, int immediately
)
3256 if (left
< 0) left
= 0;
3257 if (top
< 0) top
= 0;
3258 if (right
>= term
->cols
) right
= term
->cols
-1;
3259 if (bottom
>= term
->rows
) bottom
= term
->rows
-1;
3261 for (i
= top
; i
<= bottom
&& i
< term
->rows
; i
++) {
3262 if ((term
->disptext
[i
* (term
->cols
+ 1) + term
->cols
] &
3263 LATTR_MODE
) == LATTR_NORM
)
3264 for (j
= left
; j
<= right
&& j
< term
->cols
; j
++)
3265 term
->disptext
[i
* (term
->cols
+ 1) + j
] = ATTR_INVALID
;
3267 for (j
= left
/ 2; j
<= right
/ 2 + 1 && j
< term
->cols
; j
++)
3268 term
->disptext
[i
* (term
->cols
+ 1) + j
] = ATTR_INVALID
;
3271 /* This should happen soon enough, also for some reason it sometimes
3272 * fails to actually do anything when re-sizing ... painting the wrong
3276 do_paint (term
, ctx
, FALSE
);
3280 * Attempt to scroll the scrollback. The second parameter gives the
3281 * position we want to scroll to; the first is +1 to denote that
3282 * this position is relative to the beginning of the scrollback, -1
3283 * to denote it is relative to the end, and 0 to denote that it is
3284 * relative to the current position.
3286 void term_scroll(Terminal
*term
, int rel
, int where
)
3288 int sbtop
= -count234(term
->scrollback
);
3289 #ifdef OPTIMISE_SCROLL
3290 int olddisptop
= term
->disptop
;
3292 #endif /* OPTIMISE_SCROLL */
3294 term
->disptop
= (rel
< 0 ?
0 : rel
> 0 ? sbtop
: term
->disptop
) + where
;
3295 if (term
->disptop
< sbtop
)
3296 term
->disptop
= sbtop
;
3297 if (term
->disptop
> 0)
3300 #ifdef OPTIMISE_SCROLL
3301 shift
= (term
->disptop
- olddisptop
);
3302 if (shift
< term
->rows
&& shift
> -term
->rows
)
3303 scroll_display(term
, 0, term
->rows
- 1, shift
);
3304 #endif /* OPTIMISE_SCROLL */
3308 static void clipme(Terminal
*term
, pos top
, pos bottom
, int rect
)
3311 wchar_t *wbptr
; /* where next char goes within workbuf */
3313 int wblen
= 0; /* workbuf len */
3314 int buflen
; /* amount of memory allocated to workbuf */
3316 buflen
= 5120; /* Default size */
3317 workbuf
= smalloc(buflen
* sizeof(wchar_t));
3318 wbptr
= workbuf
; /* start filling here */
3319 old_top_x
= top
.x
; /* needed for rect==1 */
3321 while (poslt(top
, bottom
)) {
3323 unsigned long *ldata
= lineptr(top
.y
);
3327 * nlpos will point at the maximum position on this line we
3328 * should copy up to. So we start it at the end of the
3332 nlpos
.x
= term
->cols
;
3335 * ... move it backwards if there's unused space at the end
3336 * of the line (and also set `nl' if this is the case,
3337 * because in normal selection mode this means we need a
3338 * newline at the end)...
3340 if (!(ldata
[term
->cols
] & LATTR_WRAPPED
)) {
3341 while (((ldata
[nlpos
.x
- 1] & 0xFF) == 0x20 ||
3342 (DIRECT_CHAR(ldata
[nlpos
.x
- 1]) &&
3343 (ldata
[nlpos
.x
- 1] & CHAR_MASK
) == 0x20))
3344 && poslt(top
, nlpos
))
3346 if (poslt(nlpos
, bottom
))
3348 } else if (ldata
[term
->cols
] & LATTR_WRAPPED2
) {
3349 /* Ignore the last char on the line in a WRAPPED2 line. */
3354 * ... and then clip it to the terminal x coordinate if
3355 * we're doing rectangular selection. (In this case we
3356 * still did the above, so that copying e.g. the right-hand
3357 * column from a table doesn't fill with spaces on the
3361 if (nlpos
.x
> bottom
.x
)
3363 nl
= (top
.y
< bottom
.y
);
3366 while (poslt(top
, bottom
) && poslt(top
, nlpos
)) {
3369 sprintf(cbuf
, "<U+%04x>", (ldata
[top
.x
] & 0xFFFF));
3371 wchar_t cbuf
[16], *p
;
3372 int uc
= (ldata
[top
.x
] & 0xFFFF);
3375 if (uc
== UCSWIDE
) {
3380 switch (uc
& CSET_MASK
) {
3382 if (!term
->cfg
->rawcnp
) {
3383 uc
= unitab_xterm
[uc
& 0xFF];
3387 uc
= unitab_line
[uc
& 0xFF];
3390 uc
= unitab_scoacs
[uc
&0xFF];
3393 switch (uc
& CSET_MASK
) {
3395 uc
= unitab_font
[uc
& 0xFF];
3398 uc
= unitab_oemcp
[uc
& 0xFF];
3402 set
= (uc
& CSET_MASK
);
3403 c
= (uc
& CHAR_MASK
);
3407 if (DIRECT_FONT(uc
)) {
3408 if (c
>= ' ' && c
!= 0x7F) {
3412 if (is_dbcs_leadbyte(font_codepage
, (BYTE
) c
)) {
3414 buf
[1] = (char) (0xFF & ldata
[top
.x
+ 1]);
3415 rv
= mb_to_wc(font_codepage
, 0, buf
, 2, wbuf
, 4);
3419 rv
= mb_to_wc(font_codepage
, 0, buf
, 1, wbuf
, 4);
3423 memcpy(cbuf
, wbuf
, rv
* sizeof(wchar_t));
3430 for (p
= cbuf
; *p
; p
++) {
3431 /* Enough overhead for trailing NL and nul */
3432 if (wblen
>= buflen
- 16) {
3435 sizeof(wchar_t) * (buflen
+= 100));
3436 wbptr
= workbuf
+ wblen
;
3445 for (i
= 0; i
< sel_nl_sz
; i
++) {
3447 *wbptr
++ = sel_nl
[i
];
3451 top
.x
= rect ? old_top_x
: 0;
3453 #if SELECTION_NUL_TERMINATED
3457 write_clip(term
->frontend
, workbuf
, wblen
, FALSE
); /* transfer to clipbd */
3458 if (buflen
> 0) /* indicates we allocated this buffer */
3462 void term_copyall(Terminal
*term
)
3465 top
.y
= -count234(term
->scrollback
);
3467 clipme(term
, top
, term
->curs
, 0);
3471 * The wordness array is mainly for deciding the disposition of the
3472 * US-ASCII characters.
3474 static int wordtype(Terminal
*term
, int uc
)
3477 int start
, end
, ctype
;
3479 static const struct ucsword ucs_words
[] = {
3485 0x037e, 0x037e, 1}, /* Greek question mark */
3487 0x0387, 0x0387, 1}, /* Greek ano teleia */
3489 0x055a, 0x055f, 1}, /* Armenian punctuation */
3491 0x0589, 0x0589, 1}, /* Armenian full stop */
3493 0x0700, 0x070d, 1}, /* Syriac punctuation */
3495 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3497 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3499 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3501 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3503 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3505 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3507 0x2000, 0x200a, 0}, /* Various spaces */
3509 0x2070, 0x207f, 2}, /* superscript */
3511 0x2080, 0x208f, 2}, /* subscript */
3513 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3515 0x3000, 0x3000, 0}, /* ideographic space */
3517 0x3001, 0x3020, 1}, /* ideographic punctuation */
3519 0x303f, 0x309f, 3}, /* Hiragana */
3521 0x30a0, 0x30ff, 3}, /* Katakana */
3523 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3525 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3527 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3529 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3531 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3533 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3535 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3537 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3539 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3543 const struct ucsword
*wptr
;
3545 uc
&= (CSET_MASK
| CHAR_MASK
);
3547 switch (uc
& CSET_MASK
) {
3549 uc
= unitab_xterm
[uc
& 0xFF];
3552 uc
= unitab_line
[uc
& 0xFF];
3555 uc
= unitab_scoacs
[uc
&0xFF];
3558 switch (uc
& CSET_MASK
) {
3560 uc
= unitab_font
[uc
& 0xFF];
3563 uc
= unitab_oemcp
[uc
& 0xFF];
3567 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3568 * fail as there's such a thing as a double width space. :-(
3570 if (dbcs_screenfont
&& font_codepage
== line_codepage
)
3574 return term
->wordness
[uc
];
3576 for (wptr
= ucs_words
; wptr
->start
; wptr
++) {
3577 if (uc
>= wptr
->start
&& uc
<= wptr
->end
)
3585 * Spread the selection outwards according to the selection mode.
3587 static pos
sel_spread_half(Terminal
*term
, pos p
, int dir
)
3589 unsigned long *ldata
;
3591 int topy
= -count234(term
->scrollback
);
3593 ldata
= lineptr(p
.y
);
3595 switch (term
->selmode
) {
3598 * In this mode, every character is a separate unit, except
3599 * for runs of spaces at the end of a non-wrapping line.
3601 if (!(ldata
[term
->cols
] & LATTR_WRAPPED
)) {
3602 unsigned long *q
= ldata
+ term
->cols
;
3603 while (q
> ldata
&& (q
[-1] & CHAR_MASK
) == 0x20)
3605 if (q
== ldata
+ term
->cols
)
3607 if (p
.x
>= q
- ldata
)
3608 p
.x
= (dir
== -1 ? q
- ldata
: term
->cols
- 1);
3613 * In this mode, the units are maximal runs of characters
3614 * whose `wordness' has the same value.
3616 wvalue
= wordtype(term
, UCSGET(ldata
, p
.x
));
3619 int maxcols
= (ldata
[term
->cols
] & LATTR_WRAPPED2 ?
3620 term
->cols
-1 : term
->cols
);
3621 if (p
.x
< maxcols
-1) {
3622 if (wordtype(term
, UCSGET(ldata
, p
.x
+ 1)) == wvalue
)
3627 if (ldata
[term
->cols
] & LATTR_WRAPPED
) {
3628 unsigned long *ldata2
;
3629 ldata2
= lineptr(p
.y
+1);
3630 if (wordtype(term
, UCSGET(ldata2
, 0)) == wvalue
) {
3643 if (wordtype(term
, UCSGET(ldata
, p
.x
- 1)) == wvalue
)
3648 unsigned long *ldata2
;
3652 ldata2
= lineptr(p
.y
-1);
3653 maxcols
= (ldata2
[term
->cols
] & LATTR_WRAPPED2 ?
3654 term
->cols
-1 : term
->cols
);
3655 if (ldata2
[term
->cols
] & LATTR_WRAPPED
) {
3656 if (wordtype(term
, UCSGET(ldata2
, maxcols
-1))
3671 * In this mode, every line is a unit.
3673 p
.x
= (dir
== -1 ?
0 : term
->cols
- 1);
3679 static void sel_spread(Terminal
*term
)
3681 if (term
->seltype
== LEXICOGRAPHIC
) {
3682 term
->selstart
= sel_spread_half(term
, term
->selstart
, -1);
3683 decpos(term
->selend
);
3684 term
->selend
= sel_spread_half(term
, term
->selend
, +1);
3685 incpos(term
->selend
);
3689 void term_do_paste(Terminal
*term
)
3694 get_clip(term
->frontend
, &data
, &len
);
3695 if (data
&& len
> 0) {
3698 term_seen_key_event(term
); /* pasted data counts */
3700 if (term
->paste_buffer
)
3701 sfree(term
->paste_buffer
);
3702 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
3703 term
->paste_buffer
= smalloc(len
* sizeof(wchar_t));
3706 while (p
< data
+ len
) {
3707 while (p
< data
+ len
&&
3708 !(p
<= data
+ len
- sel_nl_sz
&&
3709 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
3714 for (i
= 0; i
< p
- q
; i
++) {
3715 term
->paste_buffer
[term
->paste_len
++] = q
[i
];
3719 if (p
<= data
+ len
- sel_nl_sz
&&
3720 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
3721 term
->paste_buffer
[term
->paste_len
++] = '\015';
3727 /* Assume a small paste will be OK in one go. */
3728 if (term
->paste_len
< 256) {
3730 luni_send(term
->ldisc
, term
->paste_buffer
, term
->paste_len
, 0);
3731 if (term
->paste_buffer
)
3732 sfree(term
->paste_buffer
);
3733 term
->paste_buffer
= 0;
3734 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
3737 get_clip(term
->frontend
, NULL
, NULL
);
3740 void term_mouse(Terminal
*term
, Mouse_Button b
, Mouse_Action a
, int x
, int y
,
3741 int shift
, int ctrl
, int alt
)
3744 unsigned long *ldata
;
3745 int raw_mouse
= (term
->xterm_mouse
&&
3746 !term
->cfg
->no_mouse_rep
&&
3747 !(term
->cfg
->mouse_override
&& shift
));
3748 int default_seltype
;
3752 if (a
== MA_DRAG
&& !raw_mouse
)
3753 term_scroll(term
, 0, -1);
3755 if (y
>= term
->rows
) {
3757 if (a
== MA_DRAG
&& !raw_mouse
)
3758 term_scroll(term
, 0, +1);
3767 if (x
>= term
->cols
)
3770 selpoint
.y
= y
+ term
->disptop
;
3772 ldata
= lineptr(selpoint
.y
);
3773 if ((ldata
[term
->cols
] & LATTR_MODE
) != LATTR_NORM
)
3777 int encstate
= 0, r
, c
;
3784 encstate
= 0x20; /* left button down */
3795 case MBT_WHEEL_DOWN
:
3798 default: break; /* placate gcc warning about enum use */
3802 if (term
->xterm_mouse
== 1)
3808 term
->mouse_is_down
= 0;
3811 if (term
->mouse_is_down
== b
)
3813 term
->mouse_is_down
= b
;
3815 default: break; /* placate gcc warning about enum use */
3824 sprintf(abuf
, "\033[M%c%c%c", encstate
, c
, r
);
3825 ldisc_send(term
->ldisc
, abuf
, 6, 0);
3830 b
= translate_button(term
->frontend
, b
);
3833 * Set the selection type (rectangular or normal) at the start
3834 * of a selection attempt, from the state of Alt.
3836 if (!alt
^ !term
->cfg
->rect_select
)
3837 default_seltype
= RECTANGULAR
;
3839 default_seltype
= LEXICOGRAPHIC
;
3841 if (term
->selstate
== NO_SELECTION
) {
3842 term
->seltype
= default_seltype
;
3845 if (b
== MBT_SELECT
&& a
== MA_CLICK
) {
3847 term
->selstate
= ABOUT_TO
;
3848 term
->seltype
= default_seltype
;
3849 term
->selanchor
= selpoint
;
3850 term
->selmode
= SM_CHAR
;
3851 } else if (b
== MBT_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
3853 term
->selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
3854 term
->selstate
= DRAGGING
;
3855 term
->selstart
= term
->selanchor
= selpoint
;
3856 term
->selend
= term
->selstart
;
3857 incpos(term
->selend
);
3859 } else if ((b
== MBT_SELECT
&& a
== MA_DRAG
) ||
3860 (b
== MBT_EXTEND
&& a
!= MA_RELEASE
)) {
3861 if (term
->selstate
== ABOUT_TO
&& poseq(term
->selanchor
, selpoint
))
3863 if (b
== MBT_EXTEND
&& a
!= MA_DRAG
&& term
->selstate
== SELECTED
) {
3864 if (term
->seltype
== LEXICOGRAPHIC
) {
3866 * For normal selection, we extend by moving
3867 * whichever end of the current selection is closer
3870 if (posdiff(selpoint
, term
->selstart
) <
3871 posdiff(term
->selend
, term
->selstart
) / 2) {
3872 term
->selanchor
= term
->selend
;
3873 decpos(term
->selanchor
);
3875 term
->selanchor
= term
->selstart
;
3879 * For rectangular selection, we have a choice of
3880 * _four_ places to put selanchor and selpoint: the
3881 * four corners of the selection.
3883 if (2*selpoint
.x
< term
->selstart
.x
+ term
->selend
.x
)
3884 term
->selanchor
.x
= term
->selend
.x
-1;
3886 term
->selanchor
.x
= term
->selstart
.x
;
3888 if (2*selpoint
.y
< term
->selstart
.y
+ term
->selend
.y
)
3889 term
->selanchor
.y
= term
->selend
.y
;
3891 term
->selanchor
.y
= term
->selstart
.y
;
3893 term
->selstate
= DRAGGING
;
3895 if (term
->selstate
!= ABOUT_TO
&& term
->selstate
!= DRAGGING
)
3896 term
->selanchor
= selpoint
;
3897 term
->selstate
= DRAGGING
;
3898 if (term
->seltype
== LEXICOGRAPHIC
) {
3900 * For normal selection, we set (selstart,selend) to
3901 * (selpoint,selanchor) in some order.
3903 if (poslt(selpoint
, term
->selanchor
)) {
3904 term
->selstart
= selpoint
;
3905 term
->selend
= term
->selanchor
;
3906 incpos(term
->selend
);
3908 term
->selstart
= term
->selanchor
;
3909 term
->selend
= selpoint
;
3910 incpos(term
->selend
);
3914 * For rectangular selection, we may need to
3915 * interchange x and y coordinates (if the user has
3916 * dragged in the -x and +y directions, or vice versa).
3918 term
->selstart
.x
= min(term
->selanchor
.x
, selpoint
.x
);
3919 term
->selend
.x
= 1+max(term
->selanchor
.x
, selpoint
.x
);
3920 term
->selstart
.y
= min(term
->selanchor
.y
, selpoint
.y
);
3921 term
->selend
.y
= max(term
->selanchor
.y
, selpoint
.y
);
3924 } else if ((b
== MBT_SELECT
|| b
== MBT_EXTEND
) && a
== MA_RELEASE
) {
3925 if (term
->selstate
== DRAGGING
) {
3927 * We've completed a selection. We now transfer the
3928 * data to the clipboard.
3930 clipme(term
, term
->selstart
, term
->selend
,
3931 (term
->seltype
== RECTANGULAR
));
3932 term
->selstate
= SELECTED
;
3934 term
->selstate
= NO_SELECTION
;
3935 } else if (b
== MBT_PASTE
3937 #if MULTICLICK_ONLY_EVENT
3938 || a
== MA_2CLK
|| a
== MA_3CLK
3941 request_paste(term
->frontend
);
3947 void term_nopaste(Terminal
*term
)
3949 if (term
->paste_len
== 0)
3951 sfree(term
->paste_buffer
);
3952 term
->paste_buffer
= NULL
;
3953 term
->paste_len
= 0;
3956 int term_paste_pending(Terminal
*term
)
3958 return term
->paste_len
!= 0;
3961 void term_paste(Terminal
*term
)
3963 long now
, paste_diff
;
3965 if (term
->paste_len
== 0)
3968 /* Don't wait forever to paste */
3969 if (term
->paste_hold
) {
3970 now
= GETTICKCOUNT();
3971 paste_diff
= now
- term
->last_paste
;
3972 if (paste_diff
>= 0 && paste_diff
< 450)
3975 term
->paste_hold
= 0;
3977 while (term
->paste_pos
< term
->paste_len
) {
3979 while (n
+ term
->paste_pos
< term
->paste_len
) {
3980 if (term
->paste_buffer
[term
->paste_pos
+ n
++] == '\015')
3984 luni_send(term
->ldisc
, term
->paste_buffer
+ term
->paste_pos
, n
, 0);
3985 term
->paste_pos
+= n
;
3987 if (term
->paste_pos
< term
->paste_len
) {
3988 term
->paste_hold
= 1;
3992 sfree(term
->paste_buffer
);
3993 term
->paste_buffer
= NULL
;
3994 term
->paste_len
= 0;
3997 static void deselect(Terminal
*term
)
3999 term
->selstate
= NO_SELECTION
;
4000 term
->selstart
.x
= term
->selstart
.y
= term
->selend
.x
= term
->selend
.y
= 0;
4003 void term_deselect(Terminal
*term
)
4009 int term_ldisc(Terminal
*term
, int option
)
4011 if (option
== LD_ECHO
)
4012 return term
->term_echoing
;
4013 if (option
== LD_EDIT
)
4014 return term
->term_editing
;
4019 * from_backend(), to get data from the backend for the terminal.
4021 int from_backend(void *vterm
, int is_stderr
, char *data
, int len
)
4023 Terminal
*term
= (Terminal
*)vterm
;
4027 bufchain_add(&term
->inbuf
, data
, len
);
4030 * term_out() always completely empties inbuf. Therefore,
4031 * there's no reason at all to return anything other than zero
4032 * from this function, because there _can't_ be a question of
4033 * the remote side needing to wait until term_out() has cleared
4036 * This is a slightly suboptimal way to deal with SSH2 - in
4037 * principle, the window mechanism would allow us to continue
4038 * to accept data on forwarded ports and X connections even
4039 * while the terminal processing was going slowly - but we
4040 * can't do the 100% right thing without moving the terminal
4041 * processing into a separate thread, and that might hurt
4042 * portability. So we manage stdout buffering the old SSH1 way:
4043 * if the terminal processing goes slowly, the whole SSH
4044 * connection stops accepting data until it's ready.
4046 * In practice, I can't imagine this causing serious trouble.
4051 void term_provide_logctx(Terminal
*term
, void *logctx
)
4053 term
->logctx
= logctx
;