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
, Config
*cfg
)
276 * Before adopting the new config, check all those terminal
277 * settings which control power-on defaults; and if they've
278 * changed, we will modify the current state as well as the
279 * default one. The full list is: Auto wrap mode, DEC Origin
280 * Mode, BCE, blinking text, character classes.
282 int reset_wrap
, reset_decom
, reset_bce
, reset_blink
, reset_charclass
;
285 reset_wrap
= (term
->cfg
.wrap_mode
!= cfg
->wrap_mode
);
286 reset_decom
= (term
->cfg
.dec_om
!= cfg
->dec_om
);
287 reset_bce
= (term
->cfg
.bce
!= cfg
->bce
);
288 reset_blink
= (term
->cfg
.blinktext
!= cfg
->blinktext
);
290 for (i
= 0; i
< lenof(term
->cfg
.wordness
); i
++)
291 if (term
->cfg
.wordness
[i
] != cfg
->wordness
[i
])
294 term
->cfg
= *cfg
; /* STRUCTURE COPY */
297 term
->alt_wrap
= term
->wrap
= term
->cfg
.wrap_mode
;
299 term
->alt_om
= term
->dec_om
= term
->cfg
.dec_om
;
301 term
->use_bce
= term
->cfg
.bce
;
303 term
->blink_is_real
= term
->cfg
.blinktext
;
305 for (i
= 0; i
< 256; i
++)
306 term
->wordness
[i
] = term
->cfg
.wordness
[i
];
308 if (term
->cfg
.no_alt_screen
)
309 swap_screen(term
, 0, FALSE
, FALSE
);
310 if (term
->cfg
.no_mouse_rep
) {
311 term
->xterm_mouse
= 0;
312 set_raw_mouse_mode(term
->frontend
, 0);
314 if (term
->cfg
.no_remote_charset
) {
315 term
->cset_attr
[0] = term
->cset_attr
[1] = ATTR_ASCII
;
316 term
->sco_acs
= term
->alt_sco_acs
= 0;
319 if (!*term
->cfg
.printer
) {
320 term_print_finish(term
);
325 * Clear the scrollback.
327 void term_clrsb(Terminal
*term
)
331 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
) {
338 * Initialise the terminal.
340 Terminal
*term_init(Config
*mycfg
, struct unicode_data
*ucsdata
,
346 * Allocate a new Terminal structure and initialise the fields
349 term
= smalloc(sizeof(Terminal
));
350 term
->frontend
= frontend
;
351 term
->ucsdata
= ucsdata
;
352 term
->cfg
= *mycfg
; /* STRUCTURE COPY */
354 term
->compatibility_level
= TM_PUTTY
;
355 strcpy(term
->id_string
, "\033[?6c");
356 term
->last_blink
= term
->last_tblink
= 0;
357 term
->paste_buffer
= NULL
;
359 term
->last_paste
= 0;
360 bufchain_init(&term
->inbuf
);
361 bufchain_init(&term
->printer_buf
);
362 term
->printing
= term
->only_printing
= FALSE
;
363 term
->print_job
= NULL
;
364 term
->vt52_mode
= FALSE
;
365 term
->cr_lf_return
= FALSE
;
366 term
->seen_disp_event
= FALSE
;
367 term
->xterm_mouse
= term
->mouse_is_down
= FALSE
;
368 term
->reset_132
= FALSE
;
369 term
->blinker
= term
->tblinker
= 0;
371 term
->repeat_off
= FALSE
;
372 term
->termstate
= TOPLEVEL
;
373 term
->selstate
= NO_SELECTION
;
376 term
->screen
= term
->alt_screen
= term
->scrollback
= NULL
;
378 term
->disptext
= term
->dispcurs
= NULL
;
381 term
->rows
= term
->cols
= -1;
383 term
->beephead
= term
->beeptail
= NULL
;
385 term
->lastbeep
= FALSE
;
386 term
->beep_overloaded
= FALSE
;
387 term
->attr_mask
= 0xffffffff;
388 term
->resize_fn
= NULL
;
389 term
->resize_ctx
= NULL
;
394 void term_free(Terminal
*term
)
397 struct beeptime
*beep
;
399 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
)
401 freetree234(term
->scrollback
);
402 while ((line
= delpos234(term
->screen
, 0)) != NULL
)
404 freetree234(term
->screen
);
405 while ((line
= delpos234(term
->alt_screen
, 0)) != NULL
)
407 freetree234(term
->alt_screen
);
408 sfree(term
->disptext
);
409 while (term
->beephead
) {
410 beep
= term
->beephead
;
411 term
->beephead
= beep
->next
;
414 bufchain_clear(&term
->inbuf
);
416 printer_finish_job(term
->print_job
);
417 bufchain_clear(&term
->printer_buf
);
418 sfree(term
->paste_buffer
);
423 * Set up the terminal for a given size.
425 void term_size(Terminal
*term
, int newrows
, int newcols
, int newsavelines
)
428 unsigned long *newdisp
, *line
;
431 int save_alt_which
= term
->alt_which
;
433 if (newrows
== term
->rows
&& newcols
== term
->cols
&&
434 newsavelines
== term
->savelines
)
435 return; /* nothing to do */
438 swap_screen(term
, 0, FALSE
, FALSE
);
440 term
->alt_t
= term
->marg_t
= 0;
441 term
->alt_b
= term
->marg_b
= newrows
- 1;
443 if (term
->rows
== -1) {
444 term
->scrollback
= newtree234(NULL
);
445 term
->screen
= newtree234(NULL
);
450 * Resize the screen and scrollback. We only need to shift
451 * lines around within our data structures, because lineptr()
452 * will take care of resizing each individual line if
455 * - If the new screen and the old screen differ in length, we
456 * must shunt some lines in from the scrollback or out to
459 * - If doing that fails to provide us with enough material to
460 * fill the new screen (i.e. the number of rows needed in
461 * the new screen exceeds the total number in the previous
462 * screen+scrollback), we must invent some blank lines to
465 * - Then, if the new scrollback length is less than the
466 * amount of scrollback we actually have, we must throw some
469 sblen
= count234(term
->scrollback
);
470 /* Do this loop to expand the screen if newrows > rows */
471 for (i
= term
->rows
; i
< newrows
; i
++) {
473 line
= delpos234(term
->scrollback
, --sblen
);
475 line
= smalloc(TSIZE
* (newcols
+ 2));
477 for (j
= 0; j
< newcols
; j
++)
478 line
[j
+ 1] = ERASE_CHAR
;
479 line
[newcols
] = LATTR_NORM
;
481 addpos234(term
->screen
, line
, 0);
483 /* Do this loop to shrink the screen if newrows < rows */
484 for (i
= newrows
; i
< term
->rows
; i
++) {
485 line
= delpos234(term
->screen
, 0);
486 addpos234(term
->scrollback
, line
, sblen
++);
488 assert(count234(term
->screen
) == newrows
);
489 while (sblen
> newsavelines
) {
490 line
= delpos234(term
->scrollback
, 0);
494 assert(count234(term
->scrollback
) <= newsavelines
);
497 newdisp
= smalloc(newrows
* (newcols
+ 1) * TSIZE
);
498 for (i
= 0; i
< newrows
* (newcols
+ 1); i
++)
499 newdisp
[i
] = ATTR_INVALID
;
500 sfree(term
->disptext
);
501 term
->disptext
= newdisp
;
502 term
->dispcurs
= NULL
;
504 newalt
= newtree234(NULL
);
505 for (i
= 0; i
< newrows
; i
++) {
506 line
= smalloc(TSIZE
* (newcols
+ 2));
508 for (j
= 0; j
< newcols
; j
++)
509 line
[j
+ 1] = term
->erase_char
;
510 line
[newcols
+ 1] = LATTR_NORM
;
511 addpos234(newalt
, line
, i
);
513 if (term
->alt_screen
) {
514 while (NULL
!= (line
= delpos234(term
->alt_screen
, 0)))
516 freetree234(term
->alt_screen
);
518 term
->alt_screen
= newalt
;
520 term
->tabs
= srealloc(term
->tabs
, newcols
* sizeof(*term
->tabs
));
523 for (i
= (term
->cols
> 0 ? term
->cols
: 0); i
< newcols
; i
++)
524 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
528 term
->curs
.y
+= newrows
- term
->rows
;
529 if (term
->curs
.y
< 0)
531 if (term
->curs
.y
>= newrows
)
532 term
->curs
.y
= newrows
- 1;
533 if (term
->curs
.x
>= newcols
)
534 term
->curs
.x
= newcols
- 1;
535 term
->alt_x
= term
->alt_y
= 0;
536 term
->wrapnext
= term
->alt_wnext
= FALSE
;
538 term
->rows
= newrows
;
539 term
->cols
= newcols
;
540 term
->savelines
= newsavelines
;
543 swap_screen(term
, save_alt_which
, FALSE
, FALSE
);
548 term
->resize_fn(term
->resize_ctx
, term
->cols
, term
->rows
);
552 * Hand a function and context pointer to the terminal which it can
553 * use to notify a back end of resizes.
555 void term_provide_resize_fn(Terminal
*term
,
556 void (*resize_fn
)(void *, int, int),
559 term
->resize_fn
= resize_fn
;
560 term
->resize_ctx
= resize_ctx
;
561 if (term
->cols
> 0 && term
->rows
> 0)
562 resize_fn(resize_ctx
, term
->cols
, term
->rows
);
566 * Swap screens. If `reset' is TRUE and we have been asked to
567 * switch to the alternate screen, we must bring most of its
568 * configuration from the main screen and erase the contents of the
569 * alternate screen completely. (This is even true if we're already
570 * on it! Blame xterm.)
572 static void swap_screen(Terminal
*term
, int which
, int reset
, int keep_cur_pos
)
578 reset
= FALSE
; /* do no weird resetting if which==0 */
580 if (which
!= term
->alt_which
) {
581 term
->alt_which
= which
;
583 ttr
= term
->alt_screen
;
584 term
->alt_screen
= term
->screen
;
587 if (!reset
&& !keep_cur_pos
)
588 term
->curs
.x
= term
->alt_x
;
591 if (!reset
&& !keep_cur_pos
)
592 term
->curs
.y
= term
->alt_y
;
595 if (!reset
) term
->marg_t
= term
->alt_t
;
598 if (!reset
) term
->marg_b
= term
->alt_b
;
601 if (!reset
) term
->dec_om
= term
->alt_om
;
604 if (!reset
) term
->wrap
= term
->alt_wrap
;
607 if (!reset
) term
->wrapnext
= term
->alt_wnext
;
610 if (!reset
) term
->insert
= term
->alt_ins
;
613 if (!reset
) term
->cset
= term
->alt_cset
;
616 if (!reset
) term
->utf
= term
->alt_utf
;
619 if (!reset
) term
->sco_acs
= term
->alt_sco_acs
;
620 term
->alt_sco_acs
= t
;
623 if (reset
&& term
->screen
) {
625 * Yes, this _is_ supposed to honour background-colour-erase.
627 erase_lots(term
, FALSE
, TRUE
, TRUE
);
631 * This might not be possible if we're called during
639 * Update the scroll bar.
641 static void update_sbar(Terminal
*term
)
645 nscroll
= count234(term
->scrollback
);
647 set_sbar(term
->frontend
, nscroll
+ term
->rows
,
648 nscroll
+ term
->disptop
, term
->rows
);
652 * Check whether the region bounded by the two pointers intersects
653 * the scroll region, and de-select the on-screen selection if so.
655 static void check_selection(Terminal
*term
, pos from
, pos to
)
657 if (poslt(from
, term
->selend
) && poslt(term
->selstart
, to
))
662 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
663 * for backward.) `sb' is TRUE if the scrolling is permitted to
664 * affect the scrollback buffer.
666 * NB this function invalidates all pointers into lines of the
667 * screen data structures. In particular, you MUST call fix_cpos
668 * after calling scroll() and before doing anything else that
669 * uses the cpos shortcut pointer.
671 static void scroll(Terminal
*term
, int topline
, int botline
, int lines
, int sb
)
673 unsigned long *line
, *line2
;
674 int i
, seltop
, olddisptop
, shift
;
676 if (topline
!= 0 || term
->alt_which
!= 0)
679 olddisptop
= term
->disptop
;
683 line
= delpos234(term
->screen
, botline
);
684 line
= resizeline(line
, term
->cols
);
685 for (i
= 0; i
< term
->cols
; i
++)
686 line
[i
+ 1] = term
->erase_char
;
687 line
[term
->cols
+ 1] = 0;
688 addpos234(term
->screen
, line
, topline
);
690 if (term
->selstart
.y
>= topline
&& term
->selstart
.y
<= botline
) {
692 if (term
->selstart
.y
> botline
) {
693 term
->selstart
.y
= botline
;
694 term
->selstart
.x
= 0;
697 if (term
->selend
.y
>= topline
&& term
->selend
.y
<= botline
) {
699 if (term
->selend
.y
> botline
) {
700 term
->selend
.y
= botline
;
709 line
= delpos234(term
->screen
, topline
);
710 if (sb
&& term
->savelines
> 0) {
711 int sblen
= count234(term
->scrollback
);
713 * We must add this line to the scrollback. We'll
714 * remove a line from the top of the scrollback to
715 * replace it, or allocate a new one if the
716 * scrollback isn't full.
718 if (sblen
== term
->savelines
) {
719 sblen
--, line2
= delpos234(term
->scrollback
, 0);
721 line2
= smalloc(TSIZE
* (term
->cols
+ 2));
722 line2
[0] = term
->cols
;
724 addpos234(term
->scrollback
, line
, sblen
);
728 * If the user is currently looking at part of the
729 * scrollback, and they haven't enabled any options
730 * that are going to reset the scrollback as a
731 * result of this movement, then the chances are
732 * they'd like to keep looking at the same line. So
733 * we move their viewpoint at the same rate as the
734 * scroll, at least until their viewpoint hits the
735 * top end of the scrollback buffer, at which point
736 * we don't have the choice any more.
738 * Thanks to Jan Holmen Holsten for the idea and
739 * initial implementation.
741 if (term
->disptop
> -term
->savelines
&& term
->disptop
< 0)
744 line
= resizeline(line
, term
->cols
);
745 for (i
= 0; i
< term
->cols
; i
++)
746 line
[i
+ 1] = term
->erase_char
;
747 line
[term
->cols
+ 1] = LATTR_NORM
;
748 addpos234(term
->screen
, line
, botline
);
751 * If the selection endpoints move into the scrollback,
752 * we keep them moving until they hit the top. However,
753 * of course, if the line _hasn't_ moved into the
754 * scrollback then we don't do this, and cut them off
755 * at the top of the scroll region.
757 * This applies to selstart and selend (for an existing
758 * selection), and also selanchor (for one being
759 * selected as we speak).
761 seltop
= sb ?
-term
->savelines
: topline
;
763 if (term
->selstart
.y
>= seltop
&&
764 term
->selstart
.y
<= botline
) {
766 if (term
->selstart
.y
< seltop
) {
767 term
->selstart
.y
= seltop
;
768 term
->selstart
.x
= 0;
771 if (term
->selend
.y
>= seltop
&& term
->selend
.y
<= botline
) {
773 if (term
->selend
.y
< seltop
) {
774 term
->selend
.y
= seltop
;
778 if (term
->selanchor
.y
>= seltop
&& term
->selanchor
.y
<= botline
) {
780 if (term
->selanchor
.y
< seltop
) {
781 term
->selanchor
.y
= seltop
;
782 term
->selanchor
.x
= 0;
789 #ifdef OPTIMISE_SCROLL
790 shift
+= term
->disptop
- olddisptop
;
791 if (shift
< term
->rows
&& shift
> -term
->rows
&& shift
!= 0)
792 scroll_display(term
, topline
, botline
, shift
);
793 #endif /* OPTIMISE_SCROLL */
796 #ifdef OPTIMISE_SCROLL
798 * Scroll the physical display, and our conception of it in disptext.
800 static void scroll_display(Terminal
*term
, int topline
, int botline
, int lines
)
802 unsigned long *start
, *end
;
803 int distance
, size
, i
;
805 start
= term
->disptext
+ topline
* (term
->cols
+ 1);
806 end
= term
->disptext
+ (botline
+ 1) * (term
->cols
+ 1);
807 distance
= (lines
> 0 ? lines
: -lines
) * (term
->cols
+ 1);
808 size
= end
- start
- distance
;
810 memmove(start
, start
+ distance
, size
* TSIZE
);
811 if (term
->dispcurs
>= start
+ distance
&&
812 term
->dispcurs
<= start
+ distance
+ size
)
813 term
->dispcurs
-= distance
;
814 for (i
= 0; i
< distance
; i
++)
815 (start
+ size
)[i
] |= ATTR_INVALID
;
817 memmove(start
+ distance
, start
, size
* TSIZE
);
818 if (term
->dispcurs
>= start
&& term
->dispcurs
<= start
+ size
)
819 term
->dispcurs
+= distance
;
820 for (i
= 0; i
< distance
; i
++)
821 start
[i
] |= ATTR_INVALID
;
823 do_scroll(term
->frontend
, topline
, botline
, lines
);
825 #endif /* OPTIMISE_SCROLL */
828 * Move the cursor to a given position, clipping at boundaries. We
829 * may or may not want to clip at the scroll margin: marg_clip is 0
830 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
831 * even _being_ outside the margins.
833 static void move(Terminal
*term
, int x
, int y
, int marg_clip
)
840 if ((term
->curs
.y
>= term
->marg_t
|| marg_clip
== 2) &&
843 if ((term
->curs
.y
<= term
->marg_b
|| marg_clip
== 2) &&
854 term
->wrapnext
= FALSE
;
858 * Save or restore the cursor and SGR mode.
860 static void save_cursor(Terminal
*term
, int save
)
863 term
->savecurs
= term
->curs
;
864 term
->save_attr
= term
->curr_attr
;
865 term
->save_cset
= term
->cset
;
866 term
->save_utf
= term
->utf
;
867 term
->save_wnext
= term
->wrapnext
;
868 term
->save_csattr
= term
->cset_attr
[term
->cset
];
869 term
->save_sco_acs
= term
->sco_acs
;
871 term
->curs
= term
->savecurs
;
872 /* Make sure the window hasn't shrunk since the save */
873 if (term
->curs
.x
>= term
->cols
)
874 term
->curs
.x
= term
->cols
- 1;
875 if (term
->curs
.y
>= term
->rows
)
876 term
->curs
.y
= term
->rows
- 1;
878 term
->curr_attr
= term
->save_attr
;
879 term
->cset
= term
->save_cset
;
880 term
->utf
= term
->save_utf
;
881 term
->wrapnext
= term
->save_wnext
;
883 * wrapnext might reset to False if the x position is no
884 * longer at the rightmost edge.
886 if (term
->wrapnext
&& term
->curs
.x
< term
->cols
-1)
887 term
->wrapnext
= FALSE
;
888 term
->cset_attr
[term
->cset
] = term
->save_csattr
;
889 term
->sco_acs
= term
->save_sco_acs
;
892 term
->erase_char
= (' ' | ATTR_ASCII
|
894 (ATTR_FGMASK
| ATTR_BGMASK
)));
899 * This function is called before doing _anything_ which affects
900 * only part of a line of text. It is used to mark the boundary
901 * between two character positions, and it indicates that some sort
902 * of effect is going to happen on only one side of that boundary.
904 * The effect of this function is to check whether a CJK
905 * double-width character is straddling the boundary, and to remove
906 * it and replace it with two spaces if so. (Of course, one or
907 * other of those spaces is then likely to be replaced with
908 * something else again, as a result of whatever happens next.)
910 * Also, if the boundary is at the right-hand _edge_ of the screen,
911 * it implies something deliberate is being done to the rightmost
912 * column position; hence we must clear LATTR_WRAPPED2.
914 * The input to the function is the coordinates of the _second_
915 * character of the pair.
917 static void check_boundary(Terminal
*term
, int x
, int y
)
919 unsigned long *ldata
;
921 /* Validate input coordinates, just in case. */
922 if (x
== 0 || x
> term
->cols
)
926 if (x
== term
->cols
) {
927 ldata
[x
] &= ~LATTR_WRAPPED2
;
929 if ((ldata
[x
] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
) {
930 ldata
[x
-1] = ldata
[x
] =
931 (ldata
[x
-1] &~ (CHAR_MASK
| CSET_MASK
)) | ATTR_ASCII
| ' ';
937 * Erase a large portion of the screen: the whole screen, or the
938 * whole line, or parts thereof.
940 static void erase_lots(Terminal
*term
,
941 int line_only
, int from_begin
, int to_end
)
945 unsigned long *ldata
;
948 start
.y
= term
->curs
.y
;
950 end
.y
= term
->curs
.y
+ 1;
967 if (!from_begin
|| !to_end
)
968 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
969 check_selection(term
, start
, end
);
971 /* Clear screen also forces a full window redraw, just in case. */
972 if (start
.y
== 0 && start
.x
== 0 && end
.y
== term
->rows
)
973 term_invalidate(term
);
975 ldata
= lineptr(start
.y
);
976 while (poslt(start
, end
)) {
977 if (start
.x
== term
->cols
) {
979 ldata
[start
.x
] &= ~(LATTR_WRAPPED
| LATTR_WRAPPED2
);
981 ldata
[start
.x
] = term
->erase_char
;
983 if (incpos(start
) && start
.y
< term
->rows
)
984 ldata
= lineptr(start
.y
);
989 * Insert or delete characters within the current line. n is +ve if
990 * insertion is desired, and -ve for deletion.
992 static void insch(Terminal
*term
, int n
)
994 int dir
= (n
< 0 ?
-1 : +1);
997 unsigned long *ldata
;
999 n
= (n
< 0 ?
-n
: n
);
1000 if (n
> term
->cols
- term
->curs
.x
)
1001 n
= term
->cols
- term
->curs
.x
;
1002 m
= term
->cols
- term
->curs
.x
- n
;
1003 cursplus
.y
= term
->curs
.y
;
1004 cursplus
.x
= term
->curs
.x
+ n
;
1005 check_selection(term
, term
->curs
, cursplus
);
1006 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1008 check_boundary(term
, term
->curs
.x
+ n
, term
->curs
.y
);
1009 ldata
= lineptr(term
->curs
.y
);
1011 memmove(ldata
+ term
->curs
.x
, ldata
+ term
->curs
.x
+ n
, m
* TSIZE
);
1013 ldata
[term
->curs
.x
+ m
++] = term
->erase_char
;
1015 memmove(ldata
+ term
->curs
.x
+ n
, ldata
+ term
->curs
.x
, m
* TSIZE
);
1017 ldata
[term
->curs
.x
+ n
] = term
->erase_char
;
1022 * Toggle terminal mode `mode' to state `state'. (`query' indicates
1023 * whether the mode is a DEC private one or a normal one.)
1025 static void toggle_mode(Terminal
*term
, int mode
, int query
, int state
)
1027 unsigned long ticks
;
1031 case 1: /* application cursor keys */
1032 term
->app_cursor_keys
= state
;
1034 case 2: /* VT52 mode */
1035 term
->vt52_mode
= !state
;
1036 if (term
->vt52_mode
) {
1037 term
->blink_is_real
= FALSE
;
1038 term
->vt52_bold
= FALSE
;
1040 term
->blink_is_real
= term
->cfg
.blinktext
;
1043 case 3: /* 80/132 columns */
1045 if (!term
->cfg
.no_remote_resize
)
1046 request_resize(term
->frontend
, state ?
132 : 80, term
->rows
);
1047 term
->reset_132
= state
;
1049 case 5: /* reverse video */
1051 * Toggle reverse video. If we receive an OFF within the
1052 * visual bell timeout period after an ON, we trigger an
1053 * effective visual bell, so that ESC[?5hESC[?5l will
1054 * always be an actually _visible_ visual bell.
1056 ticks
= GETTICKCOUNT();
1057 /* turn off a previous vbell to avoid inconsistencies */
1058 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
1059 term
->in_vbell
= FALSE
;
1060 if (term
->rvideo
&& !state
&& /* we're turning it off... */
1061 (ticks
- term
->rvbell_startpoint
) < VBELL_TIMEOUT
) {/*...soon*/
1062 /* If there's no vbell timeout already, or this one lasts
1063 * longer, replace vbell_timeout with ours. */
1064 if (!term
->in_vbell
||
1065 (term
->rvbell_startpoint
- term
->vbell_startpoint
<
1067 term
->vbell_startpoint
= term
->rvbell_startpoint
;
1068 term
->in_vbell
= TRUE
; /* may clear rvideo but set in_vbell */
1069 } else if (!term
->rvideo
&& state
) {
1070 /* This is an ON, so we notice the time and save it. */
1071 term
->rvbell_startpoint
= ticks
;
1073 term
->rvideo
= state
;
1074 term
->seen_disp_event
= TRUE
;
1078 case 6: /* DEC origin mode */
1079 term
->dec_om
= state
;
1081 case 7: /* auto wrap */
1084 case 8: /* auto key repeat */
1085 term
->repeat_off
= !state
;
1087 case 10: /* set local edit mode */
1088 term
->term_editing
= state
;
1089 if (term
->ldisc
) /* cause ldisc to notice changes */
1090 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1092 case 25: /* enable/disable cursor */
1093 compatibility2(OTHER
, VT220
);
1094 term
->cursor_on
= state
;
1095 term
->seen_disp_event
= TRUE
;
1097 case 47: /* alternate screen */
1098 compatibility(OTHER
);
1100 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, FALSE
, FALSE
);
1103 case 1000: /* xterm mouse 1 */
1104 term
->xterm_mouse
= state ?
1 : 0;
1105 set_raw_mouse_mode(term
->frontend
, state
);
1107 case 1002: /* xterm mouse 2 */
1108 term
->xterm_mouse
= state ?
2 : 0;
1109 set_raw_mouse_mode(term
->frontend
, state
);
1111 case 1047: /* alternate screen */
1112 compatibility(OTHER
);
1114 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, TRUE
, TRUE
);
1117 case 1048: /* save/restore cursor */
1118 save_cursor(term
, state
);
1119 if (!state
) term
->seen_disp_event
= TRUE
;
1121 case 1049: /* cursor & alternate screen */
1123 save_cursor(term
, state
);
1124 if (!state
) term
->seen_disp_event
= TRUE
;
1125 compatibility(OTHER
);
1127 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, TRUE
, FALSE
);
1129 save_cursor(term
, state
);
1134 case 4: /* set insert mode */
1135 compatibility(VT102
);
1136 term
->insert
= state
;
1138 case 12: /* set echo mode */
1139 term
->term_echoing
= !state
;
1140 if (term
->ldisc
) /* cause ldisc to notice changes */
1141 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1143 case 20: /* Return sends ... */
1144 term
->cr_lf_return
= state
;
1146 case 34: /* Make cursor BIG */
1147 compatibility2(OTHER
, VT220
);
1148 term
->big_cursor
= !state
;
1153 * Process an OSC sequence: set window title or icon name.
1155 static void do_osc(Terminal
*term
)
1158 while (term
->osc_strlen
--)
1159 term
->wordness
[(unsigned char)
1160 term
->osc_string
[term
->osc_strlen
]] = term
->esc_args
[0];
1162 term
->osc_string
[term
->osc_strlen
] = '\0';
1163 switch (term
->esc_args
[0]) {
1166 if (!term
->cfg
.no_remote_wintitle
)
1167 set_icon(term
->frontend
, term
->osc_string
);
1168 if (term
->esc_args
[0] == 1)
1170 /* fall through: parameter 0 means set both */
1173 if (!term
->cfg
.no_remote_wintitle
)
1174 set_title(term
->frontend
, term
->osc_string
);
1181 * ANSI printing routines.
1183 static void term_print_setup(Terminal
*term
)
1185 bufchain_clear(&term
->printer_buf
);
1186 term
->print_job
= printer_start_job(term
->cfg
.printer
);
1188 static void term_print_flush(Terminal
*term
)
1193 while ((size
= bufchain_size(&term
->printer_buf
)) > 5) {
1194 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
1197 printer_job_data(term
->print_job
, data
, len
);
1198 bufchain_consume(&term
->printer_buf
, len
);
1201 static void term_print_finish(Terminal
*term
)
1207 if (!term
->printing
&& !term
->only_printing
)
1208 return; /* we need do nothing */
1210 term_print_flush(term
);
1211 while ((size
= bufchain_size(&term
->printer_buf
)) > 0) {
1212 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
1214 if (c
== '\033' || c
== '\233') {
1215 bufchain_consume(&term
->printer_buf
, size
);
1218 printer_job_data(term
->print_job
, &c
, 1);
1219 bufchain_consume(&term
->printer_buf
, 1);
1222 printer_finish_job(term
->print_job
);
1223 term
->print_job
= NULL
;
1224 term
->printing
= term
->only_printing
= FALSE
;
1228 * Remove everything currently in `inbuf' and stick it up on the
1229 * in-memory display. There's a big state machine in here to
1230 * process escape sequences...
1232 void term_out(Terminal
*term
)
1235 unsigned char localbuf
[256], *chars
;
1240 chars
= NULL
; /* placate compiler warnings */
1241 while (nchars
> 0 || bufchain_size(&term
->inbuf
) > 0) {
1245 bufchain_prefix(&term
->inbuf
, &ret
, &nchars
);
1246 if (nchars
> sizeof(localbuf
))
1247 nchars
= sizeof(localbuf
);
1248 memcpy(localbuf
, ret
, nchars
);
1249 bufchain_consume(&term
->inbuf
, nchars
);
1251 assert(chars
!= NULL
);
1257 * Optionally log the session traffic to a file. Useful for
1258 * debugging and possibly also useful for actual logging.
1260 if (term
->cfg
.logtype
== LGTYP_DEBUG
&& term
->logctx
)
1261 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_DEBUG
);
1267 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1268 * be able to display 8-bit characters, but I'll let that go 'cause
1273 * If we're printing, add the character to the printer
1276 if (term
->printing
) {
1277 bufchain_add(&term
->printer_buf
, &c
, 1);
1280 * If we're in print-only mode, we use a much simpler
1281 * state machine designed only to recognise the ESC[4i
1282 * termination sequence.
1284 if (term
->only_printing
) {
1286 term
->print_state
= 1;
1287 else if (c
== (unsigned char)'\233')
1288 term
->print_state
= 2;
1289 else if (c
== '[' && term
->print_state
== 1)
1290 term
->print_state
= 2;
1291 else if (c
== '4' && term
->print_state
== 2)
1292 term
->print_state
= 3;
1293 else if (c
== 'i' && term
->print_state
== 3)
1294 term
->print_state
= 4;
1296 term
->print_state
= 0;
1297 if (term
->print_state
== 4) {
1298 term_print_finish(term
);
1304 /* First see about all those translations. */
1305 if (term
->termstate
== TOPLEVEL
) {
1307 switch (term
->utf_state
) {
1310 /* UTF-8 must be stateless so we ignore iso2022. */
1311 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
1312 c
= term
->ucsdata
->unitab_ctrl
[c
];
1313 else c
= ((unsigned char)c
) | ATTR_ASCII
;
1315 } else if ((c
& 0xe0) == 0xc0) {
1316 term
->utf_size
= term
->utf_state
= 1;
1317 term
->utf_char
= (c
& 0x1f);
1318 } else if ((c
& 0xf0) == 0xe0) {
1319 term
->utf_size
= term
->utf_state
= 2;
1320 term
->utf_char
= (c
& 0x0f);
1321 } else if ((c
& 0xf8) == 0xf0) {
1322 term
->utf_size
= term
->utf_state
= 3;
1323 term
->utf_char
= (c
& 0x07);
1324 } else if ((c
& 0xfc) == 0xf8) {
1325 term
->utf_size
= term
->utf_state
= 4;
1326 term
->utf_char
= (c
& 0x03);
1327 } else if ((c
& 0xfe) == 0xfc) {
1328 term
->utf_size
= term
->utf_state
= 5;
1329 term
->utf_char
= (c
& 0x01);
1340 if ((c
& 0xC0) != 0x80) {
1343 term
->utf_state
= 0;
1346 term
->utf_char
= (term
->utf_char
<< 6) | (c
& 0x3f);
1347 if (--term
->utf_state
)
1352 /* Is somebody trying to be evil! */
1354 (c
< 0x800 && term
->utf_size
>= 2) ||
1355 (c
< 0x10000 && term
->utf_size
>= 3) ||
1356 (c
< 0x200000 && term
->utf_size
>= 4) ||
1357 (c
< 0x4000000 && term
->utf_size
>= 5))
1360 /* Unicode line separator and paragraph separator are CR-LF */
1361 if (c
== 0x2028 || c
== 0x2029)
1364 /* High controls are probably a Baaad idea too. */
1368 /* The UTF-16 surrogates are not nice either. */
1369 /* The standard give the option of decoding these:
1370 * I don't want to! */
1371 if (c
>= 0xD800 && c
< 0xE000)
1374 /* ISO 10646 characters now limited to UTF-16 range. */
1378 /* This is currently a TagPhobic application.. */
1379 if (c
>= 0xE0000 && c
<= 0xE007F)
1382 /* U+FEFF is best seen as a null. */
1385 /* But U+FFFE is an error. */
1386 if (c
== 0xFFFE || c
== 0xFFFF)
1389 /* Oops this is a 16bit implementation */
1394 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1395 else if(term
->sco_acs
&&
1396 (c
!='\033' && c
!='\012' && c
!='\015' && c
!='\b'))
1398 if (term
->sco_acs
== 2) c
|= 0x80;
1401 switch (term
->cset_attr
[term
->cset
]) {
1403 * Linedraw characters are different from 'ESC ( B'
1404 * only for a small range. For ones outside that
1405 * range, make sure we use the same font as well as
1406 * the same encoding.
1409 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
1410 c
= term
->ucsdata
->unitab_ctrl
[c
];
1412 c
= ((unsigned char) c
) | ATTR_LINEDRW
;
1416 /* If UK-ASCII, make the '#' a LineDraw Pound */
1418 c
= '}' | ATTR_LINEDRW
;
1421 /*FALLTHROUGH*/ case ATTR_ASCII
:
1422 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
1423 c
= term
->ucsdata
->unitab_ctrl
[c
];
1425 c
= ((unsigned char) c
) | ATTR_ASCII
;
1428 if (c
>=' ') c
= ((unsigned char)c
) | ATTR_SCOACS
;
1434 /* How about C1 controls ? */
1435 if ((c
& -32) == 0x80 && term
->termstate
< DO_CTRLS
&&
1436 !term
->vt52_mode
&& has_compat(VT220
)) {
1437 term
->termstate
= SEEN_ESC
;
1438 term
->esc_query
= FALSE
;
1439 c
= '@' + (c
& 0x1F);
1442 /* Or the GL control. */
1443 if (c
== '\177' && term
->termstate
< DO_CTRLS
&& has_compat(OTHER
)) {
1444 if (term
->curs
.x
&& !term
->wrapnext
)
1446 term
->wrapnext
= FALSE
;
1448 if (!term
->cfg
.no_dbackspace
) /* destructive bksp might be disabled */
1449 *term
->cpos
= (' ' | term
->curr_attr
| ATTR_ASCII
);
1451 /* Or normal C0 controls. */
1452 if ((c
& -32) == 0 && term
->termstate
< DO_CTRLS
) {
1454 case '\005': /* terminal type query */
1455 /* Strictly speaking this is VT100 but a VT100 defaults to
1456 * no response. Other terminals respond at their option.
1458 * Don't put a CR in the default string as this tends to
1459 * upset some weird software.
1461 * An xterm returns "xterm" (5 characters)
1463 compatibility(ANSIMIN
);
1465 char abuf
[256], *s
, *d
;
1467 for (s
= term
->cfg
.answerback
, d
= abuf
; *s
; s
++) {
1469 if (*s
>= 'a' && *s
<= 'z')
1470 *d
++ = (*s
- ('a' - 1));
1471 else if ((*s
>= '@' && *s
<= '_') ||
1472 *s
== '?' || (*s
& 0x80))
1477 } else if (*s
== '^') {
1482 lpage_send(term
->ldisc
, DEFAULT_CODEPAGE
,
1488 struct beeptime
*newbeep
;
1489 unsigned long ticks
;
1491 ticks
= GETTICKCOUNT();
1493 if (!term
->beep_overloaded
) {
1494 newbeep
= smalloc(sizeof(struct beeptime
));
1495 newbeep
->ticks
= ticks
;
1496 newbeep
->next
= NULL
;
1497 if (!term
->beephead
)
1498 term
->beephead
= newbeep
;
1500 term
->beeptail
->next
= newbeep
;
1501 term
->beeptail
= newbeep
;
1506 * Throw out any beeps that happened more than
1509 while (term
->beephead
&&
1510 term
->beephead
->ticks
< ticks
- term
->cfg
.bellovl_t
) {
1511 struct beeptime
*tmp
= term
->beephead
;
1512 term
->beephead
= tmp
->next
;
1514 if (!term
->beephead
)
1515 term
->beeptail
= NULL
;
1519 if (term
->cfg
.bellovl
&& term
->beep_overloaded
&&
1520 ticks
- term
->lastbeep
>= (unsigned)term
->cfg
.bellovl_s
) {
1522 * If we're currently overloaded and the
1523 * last beep was more than s seconds ago,
1524 * leave overload mode.
1526 term
->beep_overloaded
= FALSE
;
1527 } else if (term
->cfg
.bellovl
&& !term
->beep_overloaded
&&
1528 term
->nbeeps
>= term
->cfg
.bellovl_n
) {
1530 * Now, if we have n or more beeps
1531 * remaining in the queue, go into overload
1534 term
->beep_overloaded
= TRUE
;
1536 term
->lastbeep
= ticks
;
1539 * Perform an actual beep if we're not overloaded.
1541 if (!term
->cfg
.bellovl
|| !term
->beep_overloaded
) {
1542 beep(term
->frontend
, term
->cfg
.beep
);
1543 if (term
->cfg
.beep
== BELL_VISUAL
) {
1544 term
->in_vbell
= TRUE
;
1545 term
->vbell_startpoint
= ticks
;
1553 if (term
->curs
.x
== 0 &&
1554 (term
->curs
.y
== 0 || term
->wrap
== 0))
1556 else if (term
->curs
.x
== 0 && term
->curs
.y
> 0)
1557 term
->curs
.x
= term
->cols
- 1, term
->curs
.y
--;
1558 else if (term
->wrapnext
)
1559 term
->wrapnext
= FALSE
;
1563 term
->seen_disp_event
= TRUE
;
1566 compatibility(VT100
);
1570 compatibility(VT100
);
1574 if (term
->vt52_mode
)
1575 term
->termstate
= VT52_ESC
;
1577 compatibility(ANSIMIN
);
1578 term
->termstate
= SEEN_ESC
;
1579 term
->esc_query
= FALSE
;
1584 term
->wrapnext
= FALSE
;
1586 term
->seen_disp_event
= TRUE
;
1587 term
->paste_hold
= 0;
1589 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
1592 if (has_compat(SCOANSI
)) {
1593 move(term
, 0, 0, 0);
1594 erase_lots(term
, FALSE
, FALSE
, TRUE
);
1596 term
->wrapnext
= FALSE
;
1597 term
->seen_disp_event
= 1;
1601 compatibility(VT100
);
1603 if (term
->curs
.y
== term
->marg_b
)
1604 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1605 else if (term
->curs
.y
< term
->rows
- 1)
1607 if (term
->cfg
.lfhascr
)
1610 term
->wrapnext
= FALSE
;
1611 term
->seen_disp_event
= 1;
1612 term
->paste_hold
= 0;
1614 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
1618 pos old_curs
= term
->curs
;
1619 unsigned long *ldata
= lineptr(term
->curs
.y
);
1623 } while (term
->curs
.x
< term
->cols
- 1 &&
1624 !term
->tabs
[term
->curs
.x
]);
1626 if ((ldata
[term
->cols
] & LATTR_MODE
) != LATTR_NORM
) {
1627 if (term
->curs
.x
>= term
->cols
/ 2)
1628 term
->curs
.x
= term
->cols
/ 2 - 1;
1630 if (term
->curs
.x
>= term
->cols
)
1631 term
->curs
.x
= term
->cols
- 1;
1635 check_selection(term
, old_curs
, term
->curs
);
1637 term
->seen_disp_event
= TRUE
;
1641 switch (term
->termstate
) {
1643 /* Only graphic characters get this far;
1644 * ctrls are stripped above */
1645 if (term
->wrapnext
&& term
->wrap
) {
1646 term
->cpos
[1] |= LATTR_WRAPPED
;
1647 if (term
->curs
.y
== term
->marg_b
)
1648 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1649 else if (term
->curs
.y
< term
->rows
- 1)
1653 term
->wrapnext
= FALSE
;
1657 if (term
->selstate
!= NO_SELECTION
) {
1658 pos cursplus
= term
->curs
;
1660 check_selection(term
, term
->curs
, cursplus
);
1662 if (((c
& CSET_MASK
) == ATTR_ASCII
|| (c
& CSET_MASK
) == 0) &&
1664 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
1670 width
= wcwidth((wchar_t) c
);
1674 * If we're about to display a double-width
1675 * character starting in the rightmost
1676 * column, then we do something special
1677 * instead. We must print a space in the
1678 * last column of the screen, then wrap;
1679 * and we also set LATTR_WRAPPED2 which
1680 * instructs subsequent cut-and-pasting not
1681 * only to splice this line to the one
1682 * after it, but to ignore the space in the
1683 * last character position as well.
1684 * (Because what was actually output to the
1685 * terminal was presumably just a sequence
1686 * of CJK characters, and we don't want a
1687 * space to be pasted in the middle of
1688 * those just because they had the
1689 * misfortune to start in the wrong parity
1690 * column. xterm concurs.)
1692 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1693 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
1694 if (term
->curs
.x
== term
->cols
-1) {
1695 *term
->cpos
++ = ATTR_ASCII
| ' ' | term
->curr_attr
;
1696 *term
->cpos
|= LATTR_WRAPPED
| LATTR_WRAPPED2
;
1697 if (term
->curs
.y
== term
->marg_b
)
1698 scroll(term
, term
->marg_t
, term
->marg_b
,
1700 else if (term
->curs
.y
< term
->rows
- 1)
1704 /* Now we must check_boundary again, of course. */
1705 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1706 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
1708 *term
->cpos
++ = c
| term
->curr_attr
;
1709 *term
->cpos
++ = UCSWIDE
| term
->curr_attr
;
1713 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1714 check_boundary(term
, term
->curs
.x
+1, term
->curs
.y
);
1715 *term
->cpos
++ = c
| term
->curr_attr
;
1722 if (term
->curs
.x
== term
->cols
) {
1725 term
->wrapnext
= TRUE
;
1726 if (term
->wrap
&& term
->vt52_mode
) {
1727 term
->cpos
[1] |= LATTR_WRAPPED
;
1728 if (term
->curs
.y
== term
->marg_b
)
1729 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1730 else if (term
->curs
.y
< term
->rows
- 1)
1734 term
->wrapnext
= FALSE
;
1737 term
->seen_disp_event
= 1;
1742 * This state is virtually identical to SEEN_ESC, with the
1743 * exception that we have an OSC sequence in the pipeline,
1744 * and _if_ we see a backslash, we process it.
1748 term
->termstate
= TOPLEVEL
;
1751 /* else fall through */
1753 if (c
>= ' ' && c
<= '/') {
1754 if (term
->esc_query
)
1755 term
->esc_query
= -1;
1757 term
->esc_query
= c
;
1760 term
->termstate
= TOPLEVEL
;
1761 switch (ANSI(c
, term
->esc_query
)) {
1762 case '[': /* enter CSI mode */
1763 term
->termstate
= SEEN_CSI
;
1764 term
->esc_nargs
= 1;
1765 term
->esc_args
[0] = ARG_DEFAULT
;
1766 term
->esc_query
= FALSE
;
1768 case ']': /* xterm escape sequences */
1769 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1770 compatibility(OTHER
);
1771 term
->termstate
= SEEN_OSC
;
1772 term
->esc_args
[0] = 0;
1774 case '7': /* save cursor */
1775 compatibility(VT100
);
1776 save_cursor(term
, TRUE
);
1778 case '8': /* restore cursor */
1779 compatibility(VT100
);
1780 save_cursor(term
, FALSE
);
1781 term
->seen_disp_event
= TRUE
;
1784 compatibility(VT100
);
1785 term
->app_keypad_keys
= TRUE
;
1788 compatibility(VT100
);
1789 term
->app_keypad_keys
= FALSE
;
1791 case 'D': /* exactly equivalent to LF */
1792 compatibility(VT100
);
1793 if (term
->curs
.y
== term
->marg_b
)
1794 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1795 else if (term
->curs
.y
< term
->rows
- 1)
1798 term
->wrapnext
= FALSE
;
1799 term
->seen_disp_event
= TRUE
;
1801 case 'E': /* exactly equivalent to CR-LF */
1802 compatibility(VT100
);
1804 if (term
->curs
.y
== term
->marg_b
)
1805 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1806 else if (term
->curs
.y
< term
->rows
- 1)
1809 term
->wrapnext
= FALSE
;
1810 term
->seen_disp_event
= TRUE
;
1812 case 'M': /* reverse index - backwards LF */
1813 compatibility(VT100
);
1814 if (term
->curs
.y
== term
->marg_t
)
1815 scroll(term
, term
->marg_t
, term
->marg_b
, -1, TRUE
);
1816 else if (term
->curs
.y
> 0)
1819 term
->wrapnext
= FALSE
;
1820 term
->seen_disp_event
= TRUE
;
1822 case 'Z': /* terminal type query */
1823 compatibility(VT100
);
1825 ldisc_send(term
->ldisc
, term
->id_string
,
1826 strlen(term
->id_string
), 0);
1828 case 'c': /* restore power-on settings */
1829 compatibility(VT100
);
1831 if (term
->ldisc
) /* cause ldisc to notice changes */
1832 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1833 if (term
->reset_132
) {
1834 if (!term
->cfg
.no_remote_resize
)
1835 request_resize(term
->frontend
, 80, term
->rows
);
1836 term
->reset_132
= 0;
1840 term
->seen_disp_event
= TRUE
;
1842 case 'H': /* set a tab */
1843 compatibility(VT100
);
1844 term
->tabs
[term
->curs
.x
] = TRUE
;
1847 case ANSI('8', '#'): /* ESC # 8 fills screen with Es :-) */
1848 compatibility(VT100
);
1850 unsigned long *ldata
;
1854 for (i
= 0; i
< term
->rows
; i
++) {
1856 for (j
= 0; j
< term
->cols
; j
++)
1857 ldata
[j
] = ATTR_DEFAULT
| 'E';
1858 ldata
[term
->cols
] = 0;
1861 term
->seen_disp_event
= TRUE
;
1862 scrtop
.x
= scrtop
.y
= 0;
1864 scrbot
.y
= term
->rows
;
1865 check_selection(term
, scrtop
, scrbot
);
1869 case ANSI('3', '#'):
1870 case ANSI('4', '#'):
1871 case ANSI('5', '#'):
1872 case ANSI('6', '#'):
1873 compatibility(VT100
);
1875 unsigned long nlattr
;
1876 unsigned long *ldata
;
1877 switch (ANSI(c
, term
->esc_query
)) {
1878 case ANSI('3', '#'):
1881 case ANSI('4', '#'):
1884 case ANSI('5', '#'):
1885 nlattr
= LATTR_NORM
;
1887 default: /* spiritually case ANSI('6', '#'): */
1888 nlattr
= LATTR_WIDE
;
1891 ldata
= lineptr(term
->curs
.y
);
1892 ldata
[term
->cols
] &= ~LATTR_MODE
;
1893 ldata
[term
->cols
] |= nlattr
;
1897 case ANSI('A', '('):
1898 compatibility(VT100
);
1899 if (!term
->cfg
.no_remote_charset
)
1900 term
->cset_attr
[0] = ATTR_GBCHR
;
1902 case ANSI('B', '('):
1903 compatibility(VT100
);
1904 if (!term
->cfg
.no_remote_charset
)
1905 term
->cset_attr
[0] = ATTR_ASCII
;
1907 case ANSI('0', '('):
1908 compatibility(VT100
);
1909 if (!term
->cfg
.no_remote_charset
)
1910 term
->cset_attr
[0] = ATTR_LINEDRW
;
1912 case ANSI('U', '('):
1913 compatibility(OTHER
);
1914 if (!term
->cfg
.no_remote_charset
)
1915 term
->cset_attr
[0] = ATTR_SCOACS
;
1918 case ANSI('A', ')'):
1919 compatibility(VT100
);
1920 if (!term
->cfg
.no_remote_charset
)
1921 term
->cset_attr
[1] = ATTR_GBCHR
;
1923 case ANSI('B', ')'):
1924 compatibility(VT100
);
1925 if (!term
->cfg
.no_remote_charset
)
1926 term
->cset_attr
[1] = ATTR_ASCII
;
1928 case ANSI('0', ')'):
1929 compatibility(VT100
);
1930 if (!term
->cfg
.no_remote_charset
)
1931 term
->cset_attr
[1] = ATTR_LINEDRW
;
1933 case ANSI('U', ')'):
1934 compatibility(OTHER
);
1935 if (!term
->cfg
.no_remote_charset
)
1936 term
->cset_attr
[1] = ATTR_SCOACS
;
1939 case ANSI('8', '%'): /* Old Linux code */
1940 case ANSI('G', '%'):
1941 compatibility(OTHER
);
1942 if (!term
->cfg
.no_remote_charset
)
1945 case ANSI('@', '%'):
1946 compatibility(OTHER
);
1947 if (!term
->cfg
.no_remote_charset
)
1953 term
->termstate
= TOPLEVEL
; /* default */
1955 if (term
->esc_nargs
<= ARGS_MAX
) {
1956 if (term
->esc_args
[term
->esc_nargs
- 1] == ARG_DEFAULT
)
1957 term
->esc_args
[term
->esc_nargs
- 1] = 0;
1958 term
->esc_args
[term
->esc_nargs
- 1] =
1959 10 * term
->esc_args
[term
->esc_nargs
- 1] + c
- '0';
1961 term
->termstate
= SEEN_CSI
;
1962 } else if (c
== ';') {
1963 if (++term
->esc_nargs
<= ARGS_MAX
)
1964 term
->esc_args
[term
->esc_nargs
- 1] = ARG_DEFAULT
;
1965 term
->termstate
= SEEN_CSI
;
1966 } else if (c
< '@') {
1967 if (term
->esc_query
)
1968 term
->esc_query
= -1;
1970 term
->esc_query
= TRUE
;
1972 term
->esc_query
= c
;
1973 term
->termstate
= SEEN_CSI
;
1975 switch (ANSI(c
, term
->esc_query
)) {
1976 case 'A': /* move up N lines */
1977 move(term
, term
->curs
.x
,
1978 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
1979 term
->seen_disp_event
= TRUE
;
1981 case 'e': /* move down N lines */
1982 compatibility(ANSI
);
1985 move(term
, term
->curs
.x
,
1986 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
1987 term
->seen_disp_event
= TRUE
;
1989 case ANSI('c', '>'): /* report xterm version */
1990 compatibility(OTHER
);
1991 /* this reports xterm version 136 so that VIM can
1992 use the drag messages from the mouse reporting */
1994 ldisc_send(term
->ldisc
, "\033[>0;136;0c", 11, 0);
1996 case 'a': /* move right N cols */
1997 compatibility(ANSI
);
2000 move(term
, term
->curs
.x
+ def(term
->esc_args
[0], 1),
2002 term
->seen_disp_event
= TRUE
;
2004 case 'D': /* move left N cols */
2005 move(term
, term
->curs
.x
- def(term
->esc_args
[0], 1),
2007 term
->seen_disp_event
= TRUE
;
2009 case 'E': /* move down N lines and CR */
2010 compatibility(ANSI
);
2012 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
2013 term
->seen_disp_event
= TRUE
;
2015 case 'F': /* move up N lines and CR */
2016 compatibility(ANSI
);
2018 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
2019 term
->seen_disp_event
= TRUE
;
2022 case '`': /* set horizontal posn */
2023 compatibility(ANSI
);
2024 move(term
, def(term
->esc_args
[0], 1) - 1,
2026 term
->seen_disp_event
= TRUE
;
2028 case 'd': /* set vertical posn */
2029 compatibility(ANSI
);
2030 move(term
, term
->curs
.x
,
2031 ((term
->dec_om ? term
->marg_t
: 0) +
2032 def(term
->esc_args
[0], 1) - 1),
2033 (term
->dec_om ?
2 : 0));
2034 term
->seen_disp_event
= TRUE
;
2037 case 'f': /* set horz and vert posns at once */
2038 if (term
->esc_nargs
< 2)
2039 term
->esc_args
[1] = ARG_DEFAULT
;
2040 move(term
, def(term
->esc_args
[1], 1) - 1,
2041 ((term
->dec_om ? term
->marg_t
: 0) +
2042 def(term
->esc_args
[0], 1) - 1),
2043 (term
->dec_om ?
2 : 0));
2044 term
->seen_disp_event
= TRUE
;
2046 case 'J': /* erase screen or parts of it */
2048 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
2051 erase_lots(term
, FALSE
, !!(i
& 2), !!(i
& 1));
2054 term
->seen_disp_event
= TRUE
;
2056 case 'K': /* erase line or parts of it */
2058 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
2061 erase_lots(term
, TRUE
, !!(i
& 2), !!(i
& 1));
2063 term
->seen_disp_event
= TRUE
;
2065 case 'L': /* insert lines */
2066 compatibility(VT102
);
2067 if (term
->curs
.y
<= term
->marg_b
)
2068 scroll(term
, term
->curs
.y
, term
->marg_b
,
2069 -def(term
->esc_args
[0], 1), FALSE
);
2071 term
->seen_disp_event
= TRUE
;
2073 case 'M': /* delete lines */
2074 compatibility(VT102
);
2075 if (term
->curs
.y
<= term
->marg_b
)
2076 scroll(term
, term
->curs
.y
, term
->marg_b
,
2077 def(term
->esc_args
[0], 1),
2080 term
->seen_disp_event
= TRUE
;
2082 case '@': /* insert chars */
2083 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2084 compatibility(VT102
);
2085 insch(term
, def(term
->esc_args
[0], 1));
2086 term
->seen_disp_event
= TRUE
;
2088 case 'P': /* delete chars */
2089 compatibility(VT102
);
2090 insch(term
, -def(term
->esc_args
[0], 1));
2091 term
->seen_disp_event
= TRUE
;
2093 case 'c': /* terminal type query */
2094 compatibility(VT100
);
2095 /* This is the response for a VT102 */
2097 ldisc_send(term
->ldisc
, term
->id_string
,
2098 strlen(term
->id_string
), 0);
2100 case 'n': /* cursor position query */
2102 if (term
->esc_args
[0] == 6) {
2104 sprintf(buf
, "\033[%d;%dR", term
->curs
.y
+ 1,
2106 ldisc_send(term
->ldisc
, buf
, strlen(buf
), 0);
2107 } else if (term
->esc_args
[0] == 5) {
2108 ldisc_send(term
->ldisc
, "\033[0n", 4, 0);
2112 case 'h': /* toggle modes to high */
2114 compatibility(VT100
);
2117 for (i
= 0; i
< term
->esc_nargs
; i
++)
2118 toggle_mode(term
, term
->esc_args
[i
],
2119 term
->esc_query
, TRUE
);
2124 compatibility(VT100
);
2126 if (term
->esc_nargs
!= 1) break;
2127 if (term
->esc_args
[0] == 5 && *term
->cfg
.printer
) {
2128 term
->printing
= TRUE
;
2129 term
->only_printing
= !term
->esc_query
;
2130 term
->print_state
= 0;
2131 term_print_setup(term
);
2132 } else if (term
->esc_args
[0] == 4 &&
2134 term_print_finish(term
);
2138 case 'l': /* toggle modes to low */
2140 compatibility(VT100
);
2143 for (i
= 0; i
< term
->esc_nargs
; i
++)
2144 toggle_mode(term
, term
->esc_args
[i
],
2145 term
->esc_query
, FALSE
);
2148 case 'g': /* clear tabs */
2149 compatibility(VT100
);
2150 if (term
->esc_nargs
== 1) {
2151 if (term
->esc_args
[0] == 0) {
2152 term
->tabs
[term
->curs
.x
] = FALSE
;
2153 } else if (term
->esc_args
[0] == 3) {
2155 for (i
= 0; i
< term
->cols
; i
++)
2156 term
->tabs
[i
] = FALSE
;
2160 case 'r': /* set scroll margins */
2161 compatibility(VT100
);
2162 if (term
->esc_nargs
<= 2) {
2164 top
= def(term
->esc_args
[0], 1) - 1;
2165 bot
= (term
->esc_nargs
<= 1
2166 || term
->esc_args
[1] == 0 ?
2168 def(term
->esc_args
[1], term
->rows
)) - 1;
2169 if (bot
>= term
->rows
)
2170 bot
= term
->rows
- 1;
2171 /* VTTEST Bug 9 - if region is less than 2 lines
2172 * don't change region.
2174 if (bot
- top
> 0) {
2179 * I used to think the cursor should be
2180 * placed at the top of the newly marginned
2181 * area. Apparently not: VMS TPU falls over
2184 * Well actually it should for
2187 term
->curs
.y
= (term
->dec_om ?
2190 term
->seen_disp_event
= TRUE
;
2194 case 'm': /* set graphics rendition */
2197 * A VT100 without the AVO only had one
2198 * attribute, either underline or
2199 * reverse video depending on the
2200 * cursor type, this was selected by
2204 * This is sometimes DIM, eg on the
2207 * This is sometimes INVIS various ANSI.
2209 * This like 22 disables BOLD, DIM and INVIS
2211 * The ANSI colours appear on any
2212 * terminal that has colour (obviously)
2213 * but the interaction between sgr0 and
2214 * the colours varies but is usually
2215 * related to the background colour
2216 * erase item. The interaction between
2217 * colour attributes and the mono ones
2218 * is also very implementation
2221 * The 39 and 49 attributes are likely
2222 * to be unimplemented.
2225 for (i
= 0; i
< term
->esc_nargs
; i
++) {
2226 switch (def(term
->esc_args
[i
], 0)) {
2227 case 0: /* restore defaults */
2228 term
->curr_attr
= ATTR_DEFAULT
;
2230 case 1: /* enable bold */
2231 compatibility(VT100AVO
);
2232 term
->curr_attr
|= ATTR_BOLD
;
2234 case 21: /* (enable double underline) */
2235 compatibility(OTHER
);
2236 case 4: /* enable underline */
2237 compatibility(VT100AVO
);
2238 term
->curr_attr
|= ATTR_UNDER
;
2240 case 5: /* enable blink */
2241 compatibility(VT100AVO
);
2242 term
->curr_attr
|= ATTR_BLINK
;
2244 case 7: /* enable reverse video */
2245 term
->curr_attr
|= ATTR_REVERSE
;
2247 case 10: /* SCO acs off */
2248 compatibility(SCOANSI
);
2249 if (term
->cfg
.no_remote_charset
) break;
2250 term
->sco_acs
= 0; break;
2251 case 11: /* SCO acs on */
2252 compatibility(SCOANSI
);
2253 if (term
->cfg
.no_remote_charset
) break;
2254 term
->sco_acs
= 1; break;
2255 case 12: /* SCO acs on, |0x80 */
2256 compatibility(SCOANSI
);
2257 if (term
->cfg
.no_remote_charset
) break;
2258 term
->sco_acs
= 2; break;
2259 case 22: /* disable bold */
2260 compatibility2(OTHER
, VT220
);
2261 term
->curr_attr
&= ~ATTR_BOLD
;
2263 case 24: /* disable underline */
2264 compatibility2(OTHER
, VT220
);
2265 term
->curr_attr
&= ~ATTR_UNDER
;
2267 case 25: /* disable blink */
2268 compatibility2(OTHER
, VT220
);
2269 term
->curr_attr
&= ~ATTR_BLINK
;
2271 case 27: /* disable reverse video */
2272 compatibility2(OTHER
, VT220
);
2273 term
->curr_attr
&= ~ATTR_REVERSE
;
2284 term
->curr_attr
&= ~ATTR_FGMASK
;
2286 (term
->esc_args
[i
] - 30)<<ATTR_FGSHIFT
;
2296 /* xterm-style bright foreground */
2297 term
->curr_attr
&= ~ATTR_FGMASK
;
2299 ((term
->esc_args
[i
] - 90 + 16)
2302 case 39: /* default-foreground */
2303 term
->curr_attr
&= ~ATTR_FGMASK
;
2304 term
->curr_attr
|= ATTR_DEFFG
;
2315 term
->curr_attr
&= ~ATTR_BGMASK
;
2317 (term
->esc_args
[i
] - 40)<<ATTR_BGSHIFT
;
2327 /* xterm-style bright background */
2328 term
->curr_attr
&= ~ATTR_BGMASK
;
2330 ((term
->esc_args
[i
] - 100 + 16)
2333 case 49: /* default-background */
2334 term
->curr_attr
&= ~ATTR_BGMASK
;
2335 term
->curr_attr
|= ATTR_DEFBG
;
2340 term
->erase_char
= (' ' | ATTR_ASCII
|
2346 case 's': /* save cursor */
2347 save_cursor(term
, TRUE
);
2349 case 'u': /* restore cursor */
2350 save_cursor(term
, FALSE
);
2351 term
->seen_disp_event
= TRUE
;
2353 case 't': /* set page size - ie window height */
2355 * VT340/VT420 sequence DECSLPP, DEC only allows values
2356 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2357 * illegal values (eg first arg 1..9) for window changing
2360 if (term
->esc_nargs
<= 1
2361 && (term
->esc_args
[0] < 1 ||
2362 term
->esc_args
[0] >= 24)) {
2363 compatibility(VT340TEXT
);
2364 if (!term
->cfg
.no_remote_resize
)
2365 request_resize(term
->frontend
, term
->cols
,
2366 def(term
->esc_args
[0], 24));
2368 } else if (term
->esc_nargs
>= 1 &&
2369 term
->esc_args
[0] >= 1 &&
2370 term
->esc_args
[0] < 24) {
2371 compatibility(OTHER
);
2373 switch (term
->esc_args
[0]) {
2377 set_iconic(term
->frontend
, FALSE
);
2380 set_iconic(term
->frontend
, TRUE
);
2383 if (term
->esc_nargs
>= 3) {
2384 if (!term
->cfg
.no_remote_resize
)
2385 move_window(term
->frontend
,
2386 def(term
->esc_args
[1], 0),
2387 def(term
->esc_args
[2], 0));
2391 /* We should resize the window to a given
2392 * size in pixels here, but currently our
2393 * resizing code isn't healthy enough to
2398 set_zorder(term
->frontend
, TRUE
);
2401 /* move to bottom */
2402 set_zorder(term
->frontend
, FALSE
);
2405 refresh_window(term
->frontend
);
2408 if (term
->esc_nargs
>= 3) {
2409 if (!term
->cfg
.no_remote_resize
)
2410 request_resize(term
->frontend
,
2411 def(term
->esc_args
[2], term
->cfg
.width
),
2412 def(term
->esc_args
[1], term
->cfg
.height
));
2416 if (term
->esc_nargs
>= 2)
2417 set_zoomed(term
->frontend
,
2423 ldisc_send(term
->ldisc
,
2424 is_iconic(term
->frontend
) ?
2425 "\033[1t" : "\033[2t", 4, 0);
2429 get_window_pos(term
->frontend
, &x
, &y
);
2430 len
= sprintf(buf
, "\033[3;%d;%dt", x
, y
);
2431 ldisc_send(term
->ldisc
, buf
, len
, 0);
2436 get_window_pixels(term
->frontend
, &x
, &y
);
2437 len
= sprintf(buf
, "\033[4;%d;%dt", x
, y
);
2438 ldisc_send(term
->ldisc
, buf
, len
, 0);
2443 len
= sprintf(buf
, "\033[8;%d;%dt",
2444 term
->rows
, term
->cols
);
2445 ldisc_send(term
->ldisc
, buf
, len
, 0);
2450 * Hmmm. Strictly speaking we
2451 * should return `the size of the
2452 * screen in characters', but
2453 * that's not easy: (a) window
2454 * furniture being what it is it's
2455 * hard to compute, and (b) in
2456 * resize-font mode maximising the
2457 * window wouldn't change the
2458 * number of characters. *shrug*. I
2459 * think we'll ignore it for the
2460 * moment and see if anyone
2461 * complains, and then ask them
2462 * what they would like it to do.
2467 p
= get_window_title(term
->frontend
, TRUE
);
2469 ldisc_send(term
->ldisc
, "\033]L", 3, 0);
2470 ldisc_send(term
->ldisc
, p
, len
, 0);
2471 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
2476 p
= get_window_title(term
->frontend
,FALSE
);
2478 ldisc_send(term
->ldisc
, "\033]l", 3, 0);
2479 ldisc_send(term
->ldisc
, p
, len
, 0);
2480 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
2487 compatibility(SCOANSI
);
2488 scroll(term
, term
->marg_t
, term
->marg_b
,
2489 def(term
->esc_args
[0], 1), TRUE
);
2491 term
->wrapnext
= FALSE
;
2492 term
->seen_disp_event
= TRUE
;
2495 compatibility(SCOANSI
);
2496 scroll(term
, term
->marg_t
, term
->marg_b
,
2497 -def(term
->esc_args
[0], 1), TRUE
);
2499 term
->wrapnext
= FALSE
;
2500 term
->seen_disp_event
= TRUE
;
2502 case ANSI('|', '*'):
2503 /* VT420 sequence DECSNLS
2504 * Set number of lines on screen
2505 * VT420 uses VGA like hardware and can support any size in
2506 * reasonable range (24..49 AIUI) with no default specified.
2508 compatibility(VT420
);
2509 if (term
->esc_nargs
== 1 && term
->esc_args
[0] > 0) {
2510 if (!term
->cfg
.no_remote_resize
)
2511 request_resize(term
->frontend
, term
->cols
,
2512 def(term
->esc_args
[0],
2517 case ANSI('|', '$'):
2518 /* VT340/VT420 sequence DECSCPP
2519 * Set number of columns per page
2520 * Docs imply range is only 80 or 132, but I'll allow any.
2522 compatibility(VT340TEXT
);
2523 if (term
->esc_nargs
<= 1) {
2524 if (!term
->cfg
.no_remote_resize
)
2525 request_resize(term
->frontend
,
2526 def(term
->esc_args
[0],
2527 term
->cfg
.width
), term
->rows
);
2531 case 'X': /* write N spaces w/o moving cursor */
2532 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
2533 compatibility(ANSIMIN
);
2535 int n
= def(term
->esc_args
[0], 1);
2537 unsigned long *p
= term
->cpos
;
2538 if (n
> term
->cols
- term
->curs
.x
)
2539 n
= term
->cols
- term
->curs
.x
;
2540 cursplus
= term
->curs
;
2542 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2543 check_boundary(term
, term
->curs
.x
+n
, term
->curs
.y
);
2544 check_selection(term
, term
->curs
, cursplus
);
2546 *p
++ = term
->erase_char
;
2547 term
->seen_disp_event
= TRUE
;
2550 case 'x': /* report terminal characteristics */
2551 compatibility(VT100
);
2554 int i
= def(term
->esc_args
[0], 0);
2555 if (i
== 0 || i
== 1) {
2556 strcpy(buf
, "\033[2;1;1;112;112;1;0x");
2558 ldisc_send(term
->ldisc
, buf
, 20, 0);
2562 case 'Z': /* BackTab for xterm */
2563 compatibility(OTHER
);
2565 int i
= def(term
->esc_args
[0], 1);
2566 pos old_curs
= term
->curs
;
2568 for(;i
>0 && term
->curs
.x
>0; i
--) {
2571 } while (term
->curs
.x
>0 &&
2572 !term
->tabs
[term
->curs
.x
]);
2575 check_selection(term
, old_curs
, term
->curs
);
2578 case ANSI('L', '='):
2579 compatibility(OTHER
);
2580 term
->use_bce
= (term
->esc_args
[0] <= 0);
2581 term
->erase_char
= ERASE_CHAR
;
2583 term
->erase_char
= (' ' | ATTR_ASCII
|
2585 (ATTR_FGMASK
| ATTR_BGMASK
)));
2587 case ANSI('E', '='):
2588 compatibility(OTHER
);
2589 term
->blink_is_real
= (term
->esc_args
[0] >= 1);
2591 case ANSI('p', '"'):
2593 * Allow the host to make this emulator a
2594 * 'perfect' VT102. This first appeared in
2595 * the VT220, but we do need to get back to
2596 * PuTTY mode so I won't check it.
2598 * The arg in 40..42,50 are a PuTTY extension.
2599 * The 2nd arg, 8bit vs 7bit is not checked.
2601 * Setting VT102 mode should also change
2602 * the Fkeys to generate PF* codes as a
2603 * real VT102 has no Fkeys. The VT220 does
2604 * this, F11..F13 become ESC,BS,LF other
2605 * Fkeys send nothing.
2607 * Note ESC c will NOT change this!
2610 switch (term
->esc_args
[0]) {
2612 term
->compatibility_level
&= ~TM_VTXXX
;
2613 term
->compatibility_level
|= TM_VT102
;
2616 term
->compatibility_level
&= ~TM_VTXXX
;
2617 term
->compatibility_level
|= TM_VT220
;
2621 if (term
->esc_args
[0] > 60 &&
2622 term
->esc_args
[0] < 70)
2623 term
->compatibility_level
|= TM_VTXXX
;
2627 term
->compatibility_level
&= TM_VTXXX
;
2630 term
->compatibility_level
= TM_PUTTY
;
2633 term
->compatibility_level
= TM_SCOANSI
;
2637 term
->compatibility_level
= TM_PUTTY
;
2643 /* Change the response to CSI c */
2644 if (term
->esc_args
[0] == 50) {
2647 strcpy(term
->id_string
, "\033[?");
2648 for (i
= 1; i
< term
->esc_nargs
; i
++) {
2650 strcat(term
->id_string
, ";");
2651 sprintf(lbuf
, "%d", term
->esc_args
[i
]);
2652 strcat(term
->id_string
, lbuf
);
2654 strcat(term
->id_string
, "c");
2657 /* Is this a good idea ?
2658 * Well we should do a soft reset at this point ...
2660 if (!has_compat(VT420
) && has_compat(VT100
)) {
2661 if (!term
->cfg
.no_remote_resize
) {
2662 if (term
->reset_132
)
2663 request_resize(132, 24);
2665 request_resize(80, 24);
2673 term
->osc_w
= FALSE
;
2675 case 'P': /* Linux palette sequence */
2676 term
->termstate
= SEEN_OSC_P
;
2677 term
->osc_strlen
= 0;
2679 case 'R': /* Linux palette reset */
2680 palette_reset(term
->frontend
);
2681 term_invalidate(term
);
2682 term
->termstate
= TOPLEVEL
;
2684 case 'W': /* word-set */
2685 term
->termstate
= SEEN_OSC_W
;
2698 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
2702 * Grotty hack to support xterm and DECterm title
2703 * sequences concurrently.
2705 if (term
->esc_args
[0] == 2) {
2706 term
->esc_args
[0] = 1;
2709 /* else fall through */
2711 term
->termstate
= OSC_STRING
;
2712 term
->osc_strlen
= 0;
2717 * This OSC stuff is EVIL. It takes just one character to get into
2718 * sysline mode and it's not initially obvious how to get out.
2719 * So I've added CR and LF as string aborts.
2720 * This shouldn't effect compatibility as I believe embedded
2721 * control characters are supposed to be interpreted (maybe?)
2722 * and they don't display anything useful anyway.
2726 if (c
== '\012' || c
== '\015') {
2727 term
->termstate
= TOPLEVEL
;
2728 } else if (c
== 0234 || c
== '\007') {
2730 * These characters terminate the string; ST and BEL
2731 * terminate the sequence and trigger instant
2732 * processing of it, whereas ESC goes back to SEEN_ESC
2733 * mode unless it is followed by \, in which case it is
2734 * synonymous with ST in the first place.
2737 term
->termstate
= TOPLEVEL
;
2738 } else if (c
== '\033')
2739 term
->termstate
= OSC_MAYBE_ST
;
2740 else if (term
->osc_strlen
< OSC_STR_MAX
)
2741 term
->osc_string
[term
->osc_strlen
++] = c
;
2745 int max
= (term
->osc_strlen
== 0 ?
21 : 16);
2747 if (c
>= '0' && c
<= '9')
2749 else if (c
>= 'A' && c
<= 'A' + max
- 10)
2751 else if (c
>= 'a' && c
<= 'a' + max
- 10)
2754 term
->termstate
= TOPLEVEL
;
2757 term
->osc_string
[term
->osc_strlen
++] = val
;
2758 if (term
->osc_strlen
>= 7) {
2759 palette_set(term
->frontend
, term
->osc_string
[0],
2760 term
->osc_string
[1] * 16 + term
->osc_string
[2],
2761 term
->osc_string
[3] * 16 + term
->osc_string
[4],
2762 term
->osc_string
[5] * 16 + term
->osc_string
[6]);
2763 term_invalidate(term
);
2764 term
->termstate
= TOPLEVEL
;
2780 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
2783 term
->termstate
= OSC_STRING
;
2784 term
->osc_strlen
= 0;
2788 term
->termstate
= TOPLEVEL
;
2789 term
->seen_disp_event
= TRUE
;
2792 move(term
, term
->curs
.x
, term
->curs
.y
- 1, 1);
2795 move(term
, term
->curs
.x
, term
->curs
.y
+ 1, 1);
2798 move(term
, term
->curs
.x
+ 1, term
->curs
.y
, 1);
2801 move(term
, term
->curs
.x
- 1, term
->curs
.y
, 1);
2804 * From the VT100 Manual
2805 * NOTE: The special graphics characters in the VT100
2806 * are different from those in the VT52
2808 * From VT102 manual:
2809 * 137 _ Blank - Same
2810 * 140 ` Reserved - Humm.
2811 * 141 a Solid rectangle - Similar
2812 * 142 b 1/ - Top half of fraction for the
2813 * 143 c 3/ - subscript numbers below.
2816 * 146 f Degrees - Same
2817 * 147 g Plus or minus - Same
2819 * 151 i Ellipsis (dots)
2822 * 154 l Bar at scan 0
2823 * 155 m Bar at scan 1
2824 * 156 n Bar at scan 2
2825 * 157 o Bar at scan 3 - Similar
2826 * 160 p Bar at scan 4 - Similar
2827 * 161 q Bar at scan 5 - Similar
2828 * 162 r Bar at scan 6 - Same
2829 * 163 s Bar at scan 7 - Similar
2844 term
->cset_attr
[term
->cset
= 0] = ATTR_LINEDRW
;
2847 term
->cset_attr
[term
->cset
= 0] = ATTR_ASCII
;
2850 move(term
, 0, 0, 0);
2853 if (term
->curs
.y
== 0)
2854 scroll(term
, 0, term
->rows
- 1, -1, TRUE
);
2855 else if (term
->curs
.y
> 0)
2858 term
->wrapnext
= FALSE
;
2861 erase_lots(term
, FALSE
, FALSE
, TRUE
);
2865 erase_lots(term
, TRUE
, FALSE
, TRUE
);
2869 /* XXX Print cursor line */
2872 /* XXX Start controller mode */
2875 /* XXX Stop controller mode */
2879 term
->termstate
= VT52_Y1
;
2883 ldisc_send(term
->ldisc
, "\033/Z", 3, 0);
2886 term
->app_keypad_keys
= TRUE
;
2889 term
->app_keypad_keys
= FALSE
;
2892 /* XXX This should switch to VT100 mode not current or default
2893 * VT mode. But this will only have effect in a VT220+
2896 term
->vt52_mode
= FALSE
;
2897 term
->blink_is_real
= term
->cfg
.blinktext
;
2901 /* XXX Enter auto print mode */
2904 /* XXX Exit auto print mode */
2907 /* XXX Print screen */
2913 /* compatibility(ATARI) */
2914 move(term
, 0, 0, 0);
2915 erase_lots(term
, FALSE
, FALSE
, TRUE
);
2919 /* compatibility(ATARI) */
2920 if (term
->curs
.y
<= term
->marg_b
)
2921 scroll(term
, term
->curs
.y
, term
->marg_b
, -1, FALSE
);
2924 /* compatibility(ATARI) */
2925 if (term
->curs
.y
<= term
->marg_b
)
2926 scroll(term
, term
->curs
.y
, term
->marg_b
, 1, TRUE
);
2929 /* compatibility(ATARI) */
2930 term
->termstate
= VT52_FG
;
2933 /* compatibility(ATARI) */
2934 term
->termstate
= VT52_BG
;
2937 /* compatibility(ATARI) */
2938 erase_lots(term
, FALSE
, TRUE
, FALSE
);
2942 /* compatibility(ATARI) */
2943 term
->cursor_on
= TRUE
;
2946 /* compatibility(ATARI) */
2947 term
->cursor_on
= FALSE
;
2949 /* case 'j': Save cursor position - broken on ST */
2950 /* case 'k': Restore cursor position */
2952 /* compatibility(ATARI) */
2953 erase_lots(term
, TRUE
, TRUE
, TRUE
);
2955 term
->wrapnext
= FALSE
;
2959 /* compatibility(ATARI) */
2960 erase_lots(term
, TRUE
, TRUE
, FALSE
);
2963 /* compatibility(ATARI) */
2964 term
->curr_attr
|= ATTR_REVERSE
;
2967 /* compatibility(ATARI) */
2968 term
->curr_attr
&= ~ATTR_REVERSE
;
2970 case 'v': /* wrap Autowrap on - Wyse style */
2971 /* compatibility(ATARI) */
2974 case 'w': /* Autowrap off */
2975 /* compatibility(ATARI) */
2980 /* compatibility(OTHER) */
2981 term
->vt52_bold
= FALSE
;
2982 term
->curr_attr
= ATTR_DEFAULT
;
2984 term
->erase_char
= (' ' | ATTR_ASCII
|
2986 (ATTR_FGMASK
| ATTR_BGMASK
)));
2989 /* compatibility(VI50) */
2990 term
->curr_attr
|= ATTR_UNDER
;
2993 /* compatibility(VI50) */
2994 term
->curr_attr
&= ~ATTR_UNDER
;
2997 /* compatibility(VI50) */
2998 term
->vt52_bold
= TRUE
;
2999 term
->curr_attr
|= ATTR_BOLD
;
3002 /* compatibility(VI50) */
3003 term
->vt52_bold
= FALSE
;
3004 term
->curr_attr
&= ~ATTR_BOLD
;
3010 term
->termstate
= VT52_Y2
;
3011 move(term
, term
->curs
.x
, c
- ' ', 0);
3014 term
->termstate
= TOPLEVEL
;
3015 move(term
, c
- ' ', term
->curs
.y
, 0);
3020 term
->termstate
= TOPLEVEL
;
3021 term
->curr_attr
&= ~ATTR_FGMASK
;
3022 term
->curr_attr
&= ~ATTR_BOLD
;
3023 term
->curr_attr
|= (c
& 0x7) << ATTR_FGSHIFT
;
3024 if ((c
& 0x8) || term
->vt52_bold
)
3025 term
->curr_attr
|= ATTR_BOLD
;
3028 term
->erase_char
= (' ' | ATTR_ASCII
|
3030 (ATTR_FGMASK
| ATTR_BGMASK
)));
3033 term
->termstate
= TOPLEVEL
;
3034 term
->curr_attr
&= ~ATTR_BGMASK
;
3035 term
->curr_attr
&= ~ATTR_BLINK
;
3036 term
->curr_attr
|= (c
& 0x7) << ATTR_BGSHIFT
;
3038 /* Note: bold background */
3040 term
->curr_attr
|= ATTR_BLINK
;
3043 term
->erase_char
= (' ' | ATTR_ASCII
|
3045 (ATTR_FGMASK
| ATTR_BGMASK
)));
3048 default: break; /* placate gcc warning about enum use */
3050 if (term
->selstate
!= NO_SELECTION
) {
3051 pos cursplus
= term
->curs
;
3053 check_selection(term
, term
->curs
, cursplus
);
3057 term_print_flush(term
);
3062 * Compare two lines to determine whether they are sufficiently
3063 * alike to scroll-optimise one to the other. Return the degree of
3066 static int linecmp(Terminal
*term
, unsigned long *a
, unsigned long *b
)
3070 for (i
= n
= 0; i
< term
->cols
; i
++)
3071 n
+= (*a
++ == *b
++);
3077 * Given a context, update the window. Out of paranoia, we don't
3078 * allow WM_PAINT responses to do scrolling optimisations.
3080 static void do_paint(Terminal
*term
, Context ctx
, int may_optimise
)
3082 int i
, j
, our_curs_y
, our_curs_x
;
3083 unsigned long rv
, cursor
;
3086 long cursor_background
= ERASE_CHAR
;
3087 unsigned long ticks
;
3090 * Check the visual bell state.
3092 if (term
->in_vbell
) {
3093 ticks
= GETTICKCOUNT();
3094 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
3095 term
->in_vbell
= FALSE
;
3098 rv
= (!term
->rvideo
^ !term
->in_vbell ? ATTR_REVERSE
: 0);
3101 * screen array, disptop, scrtop,
3103 * cfg.blinkpc, blink_is_real, tblinker,
3104 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
3107 /* Has the cursor position or type changed ? */
3108 if (term
->cursor_on
) {
3109 if (term
->has_focus
) {
3110 if (term
->blinker
|| !term
->cfg
.blink_cur
)
3111 cursor
= TATTR_ACTCURS
;
3115 cursor
= TATTR_PASCURS
;
3117 cursor
|= TATTR_RIGHTCURS
;
3120 our_curs_y
= term
->curs
.y
- term
->disptop
;
3123 * Adjust the cursor position in the case where it's
3124 * resting on the right-hand half of a CJK wide character.
3125 * xterm's behaviour here, which seems adequate to me, is
3126 * to display the cursor covering the _whole_ character,
3127 * exactly as if it were one space to the left.
3129 unsigned long *ldata
= lineptr(term
->curs
.y
);
3130 our_curs_x
= term
->curs
.x
;
3131 if (our_curs_x
> 0 &&
3132 (ldata
[our_curs_x
] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
3136 if (term
->dispcurs
&& (term
->curstype
!= cursor
||
3138 term
->disptext
+ our_curs_y
* (term
->cols
+ 1) +
3140 if (term
->dispcurs
> term
->disptext
&&
3141 (*term
->dispcurs
& (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
3142 term
->dispcurs
[-1] |= ATTR_INVALID
;
3143 if ( (term
->dispcurs
[1] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
3144 term
->dispcurs
[1] |= ATTR_INVALID
;
3145 *term
->dispcurs
|= ATTR_INVALID
;
3148 term
->dispcurs
= NULL
;
3150 /* The normal screen data */
3151 for (i
= 0; i
< term
->rows
; i
++) {
3152 unsigned long *ldata
;
3154 int idx
, dirty_line
, dirty_run
, selected
;
3155 unsigned long attr
= 0;
3156 int updated_line
= 0;
3159 int last_run_dirty
= 0;
3161 scrpos
.y
= i
+ term
->disptop
;
3162 ldata
= lineptr(scrpos
.y
);
3163 lattr
= (ldata
[term
->cols
] & LATTR_MODE
);
3165 idx
= i
* (term
->cols
+ 1);
3166 dirty_run
= dirty_line
= (ldata
[term
->cols
] !=
3167 term
->disptext
[idx
+ term
->cols
]);
3168 term
->disptext
[idx
+ term
->cols
] = ldata
[term
->cols
];
3170 for (j
= 0; j
< term
->cols
; j
++, idx
++) {
3171 unsigned long tattr
, tchar
;
3172 unsigned long *d
= ldata
+ j
;
3176 tchar
= (*d
& (CHAR_MASK
| CSET_MASK
));
3177 tattr
= (*d
& (ATTR_MASK
^ CSET_MASK
));
3178 switch (tchar
& CSET_MASK
) {
3180 tchar
= term
->ucsdata
->unitab_line
[tchar
& 0xFF];
3183 tchar
= term
->ucsdata
->unitab_xterm
[tchar
& 0xFF];
3186 tchar
= term
->ucsdata
->unitab_scoacs
[tchar
&0xFF];
3189 tattr
|= (tchar
& CSET_MASK
);
3191 if ((d
[1] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
3194 /* Video reversing things */
3195 if (term
->selstate
== DRAGGING
|| term
->selstate
== SELECTED
) {
3196 if (term
->seltype
== LEXICOGRAPHIC
)
3197 selected
= (posle(term
->selstart
, scrpos
) &&
3198 poslt(scrpos
, term
->selend
));
3200 selected
= (posPle(term
->selstart
, scrpos
) &&
3201 posPlt(scrpos
, term
->selend
));
3205 ^ (selected ? ATTR_REVERSE
: 0));
3207 /* 'Real' blinking ? */
3208 if (term
->blink_is_real
&& (tattr
& ATTR_BLINK
)) {
3209 if (term
->has_focus
&& term
->tblinker
) {
3210 tchar
= term
->ucsdata
->unitab_line
[(unsigned char)' '];
3212 tattr
&= ~ATTR_BLINK
;
3216 * Check the font we'll _probably_ be using to see if
3217 * the character is wide when we don't want it to be.
3219 if ((tchar
| tattr
) != (term
->disptext
[idx
]& ~ATTR_NARROW
)) {
3220 if ((tattr
& ATTR_WIDE
) == 0 &&
3221 char_width(ctx
, (tchar
| tattr
) & 0xFFFF) == 2)
3222 tattr
|= ATTR_NARROW
;
3223 } else if (term
->disptext
[idx
]&ATTR_NARROW
)
3224 tattr
|= ATTR_NARROW
;
3226 /* Cursor here ? Save the 'background' */
3227 if (i
== our_curs_y
&& j
== our_curs_x
) {
3228 cursor_background
= tattr
| tchar
;
3229 term
->dispcurs
= term
->disptext
+ idx
;
3232 if ((term
->disptext
[idx
] ^ tattr
) & ATTR_WIDE
)
3235 break_run
= (((tattr
^ attr
) & term
->attr_mask
) ||
3236 j
- start
>= sizeof(ch
));
3238 /* Special hack for VT100 Linedraw glyphs */
3239 if ((attr
& CSET_MASK
) == 0x2300 && tchar
>= 0xBA
3240 && tchar
<= 0xBD) break_run
= TRUE
;
3242 if (!term
->ucsdata
->dbcs_screenfont
&& !dirty_line
) {
3243 if ((tchar
| tattr
) == term
->disptext
[idx
])
3245 else if (!dirty_run
&& ccount
== 1)
3250 if ((dirty_run
|| last_run_dirty
) && ccount
> 0) {
3251 do_text(ctx
, start
, i
, ch
, ccount
, attr
, lattr
);
3257 if (term
->ucsdata
->dbcs_screenfont
)
3258 last_run_dirty
= dirty_run
;
3259 dirty_run
= dirty_line
;
3262 if ((tchar
| tattr
) != term
->disptext
[idx
])
3264 ch
[ccount
++] = (char) tchar
;
3265 term
->disptext
[idx
] = tchar
| tattr
;
3267 /* If it's a wide char step along to the next one. */
3268 if (tattr
& ATTR_WIDE
) {
3269 if (++j
< term
->cols
) {
3273 * By construction above, the cursor should not
3274 * be on the right-hand half of this character.
3277 assert(!(i
== our_curs_y
&& j
== our_curs_x
));
3278 if (term
->disptext
[idx
] != *d
)
3280 term
->disptext
[idx
] = *d
;
3284 if (dirty_run
&& ccount
> 0) {
3285 do_text(ctx
, start
, i
, ch
, ccount
, attr
, lattr
);
3289 /* Cursor on this line ? (and changed) */
3290 if (i
== our_curs_y
&& (term
->curstype
!= cursor
|| updated_line
)) {
3291 ch
[0] = (char) (cursor_background
& CHAR_MASK
);
3292 attr
= (cursor_background
& ATTR_MASK
) | cursor
;
3293 do_cursor(ctx
, our_curs_x
, i
, ch
, 1, attr
, lattr
);
3294 term
->curstype
= cursor
;
3300 * Flick the switch that says if blinking things should be shown or hidden.
3303 void term_blink(Terminal
*term
, int flg
)
3305 long now
, blink_diff
;
3307 now
= GETTICKCOUNT();
3308 blink_diff
= now
- term
->last_tblink
;
3310 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3311 if (blink_diff
< 0 || blink_diff
> (TICKSPERSEC
* 9 / 20)) {
3312 term
->last_tblink
= now
;
3313 term
->tblinker
= !term
->tblinker
;
3318 term
->last_blink
= now
;
3322 blink_diff
= now
- term
->last_blink
;
3324 /* Make sure the cursor blinks no faster than system blink rate */
3325 if (blink_diff
>= 0 && blink_diff
< (long) CURSORBLINK
)
3328 term
->last_blink
= now
;
3329 term
->blinker
= !term
->blinker
;
3333 * Invalidate the whole screen so it will be repainted in full.
3335 void term_invalidate(Terminal
*term
)
3339 for (i
= 0; i
< term
->rows
* (term
->cols
+ 1); i
++)
3340 term
->disptext
[i
] = ATTR_INVALID
;
3344 * Paint the window in response to a WM_PAINT message.
3346 void term_paint(Terminal
*term
, Context ctx
,
3347 int left
, int top
, int right
, int bottom
, int immediately
)
3350 if (left
< 0) left
= 0;
3351 if (top
< 0) top
= 0;
3352 if (right
>= term
->cols
) right
= term
->cols
-1;
3353 if (bottom
>= term
->rows
) bottom
= term
->rows
-1;
3355 for (i
= top
; i
<= bottom
&& i
< term
->rows
; i
++) {
3356 if ((term
->disptext
[i
* (term
->cols
+ 1) + term
->cols
] &
3357 LATTR_MODE
) == LATTR_NORM
)
3358 for (j
= left
; j
<= right
&& j
< term
->cols
; j
++)
3359 term
->disptext
[i
* (term
->cols
+ 1) + j
] = ATTR_INVALID
;
3361 for (j
= left
/ 2; j
<= right
/ 2 + 1 && j
< term
->cols
; j
++)
3362 term
->disptext
[i
* (term
->cols
+ 1) + j
] = ATTR_INVALID
;
3365 /* This should happen soon enough, also for some reason it sometimes
3366 * fails to actually do anything when re-sizing ... painting the wrong
3370 do_paint (term
, ctx
, FALSE
);
3374 * Attempt to scroll the scrollback. The second parameter gives the
3375 * position we want to scroll to; the first is +1 to denote that
3376 * this position is relative to the beginning of the scrollback, -1
3377 * to denote it is relative to the end, and 0 to denote that it is
3378 * relative to the current position.
3380 void term_scroll(Terminal
*term
, int rel
, int where
)
3382 int sbtop
= -count234(term
->scrollback
);
3383 #ifdef OPTIMISE_SCROLL
3384 int olddisptop
= term
->disptop
;
3386 #endif /* OPTIMISE_SCROLL */
3388 term
->disptop
= (rel
< 0 ?
0 : rel
> 0 ? sbtop
: term
->disptop
) + where
;
3389 if (term
->disptop
< sbtop
)
3390 term
->disptop
= sbtop
;
3391 if (term
->disptop
> 0)
3394 #ifdef OPTIMISE_SCROLL
3395 shift
= (term
->disptop
- olddisptop
);
3396 if (shift
< term
->rows
&& shift
> -term
->rows
)
3397 scroll_display(term
, 0, term
->rows
- 1, shift
);
3398 #endif /* OPTIMISE_SCROLL */
3402 static void clipme(Terminal
*term
, pos top
, pos bottom
, int rect
)
3405 wchar_t *wbptr
; /* where next char goes within workbuf */
3407 int wblen
= 0; /* workbuf len */
3408 int buflen
; /* amount of memory allocated to workbuf */
3410 buflen
= 5120; /* Default size */
3411 workbuf
= smalloc(buflen
* sizeof(wchar_t));
3412 wbptr
= workbuf
; /* start filling here */
3413 old_top_x
= top
.x
; /* needed for rect==1 */
3415 while (poslt(top
, bottom
)) {
3417 unsigned long *ldata
= lineptr(top
.y
);
3421 * nlpos will point at the maximum position on this line we
3422 * should copy up to. So we start it at the end of the
3426 nlpos
.x
= term
->cols
;
3429 * ... move it backwards if there's unused space at the end
3430 * of the line (and also set `nl' if this is the case,
3431 * because in normal selection mode this means we need a
3432 * newline at the end)...
3434 if (!(ldata
[term
->cols
] & LATTR_WRAPPED
)) {
3435 while (((ldata
[nlpos
.x
- 1] & 0xFF) == 0x20 ||
3436 (DIRECT_CHAR(ldata
[nlpos
.x
- 1]) &&
3437 (ldata
[nlpos
.x
- 1] & CHAR_MASK
) == 0x20))
3438 && poslt(top
, nlpos
))
3440 if (poslt(nlpos
, bottom
))
3442 } else if (ldata
[term
->cols
] & LATTR_WRAPPED2
) {
3443 /* Ignore the last char on the line in a WRAPPED2 line. */
3448 * ... and then clip it to the terminal x coordinate if
3449 * we're doing rectangular selection. (In this case we
3450 * still did the above, so that copying e.g. the right-hand
3451 * column from a table doesn't fill with spaces on the
3455 if (nlpos
.x
> bottom
.x
)
3457 nl
= (top
.y
< bottom
.y
);
3460 while (poslt(top
, bottom
) && poslt(top
, nlpos
)) {
3463 sprintf(cbuf
, "<U+%04x>", (ldata
[top
.x
] & 0xFFFF));
3465 wchar_t cbuf
[16], *p
;
3466 int uc
= (ldata
[top
.x
] & 0xFFFF);
3469 if (uc
== UCSWIDE
) {
3474 switch (uc
& CSET_MASK
) {
3476 if (!term
->cfg
.rawcnp
) {
3477 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
3481 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
3484 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
3487 switch (uc
& CSET_MASK
) {
3489 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
3492 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
3496 set
= (uc
& CSET_MASK
);
3497 c
= (uc
& CHAR_MASK
);
3501 if (DIRECT_FONT(uc
)) {
3502 if (c
>= ' ' && c
!= 0x7F) {
3506 if (is_dbcs_leadbyte(term
->ucsdata
->font_codepage
, (BYTE
) c
)) {
3508 buf
[1] = (char) (0xFF & ldata
[top
.x
+ 1]);
3509 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 2, wbuf
, 4);
3513 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 1, wbuf
, 4);
3517 memcpy(cbuf
, wbuf
, rv
* sizeof(wchar_t));
3524 for (p
= cbuf
; *p
; p
++) {
3525 /* Enough overhead for trailing NL and nul */
3526 if (wblen
>= buflen
- 16) {
3529 sizeof(wchar_t) * (buflen
+= 100));
3530 wbptr
= workbuf
+ wblen
;
3539 for (i
= 0; i
< sel_nl_sz
; i
++) {
3541 *wbptr
++ = sel_nl
[i
];
3545 top
.x
= rect ? old_top_x
: 0;
3547 #if SELECTION_NUL_TERMINATED
3551 write_clip(term
->frontend
, workbuf
, wblen
, FALSE
); /* transfer to clipbd */
3552 if (buflen
> 0) /* indicates we allocated this buffer */
3556 void term_copyall(Terminal
*term
)
3559 top
.y
= -count234(term
->scrollback
);
3561 clipme(term
, top
, term
->curs
, 0);
3565 * The wordness array is mainly for deciding the disposition of the
3566 * US-ASCII characters.
3568 static int wordtype(Terminal
*term
, int uc
)
3571 int start
, end
, ctype
;
3573 static const struct ucsword ucs_words
[] = {
3579 0x037e, 0x037e, 1}, /* Greek question mark */
3581 0x0387, 0x0387, 1}, /* Greek ano teleia */
3583 0x055a, 0x055f, 1}, /* Armenian punctuation */
3585 0x0589, 0x0589, 1}, /* Armenian full stop */
3587 0x0700, 0x070d, 1}, /* Syriac punctuation */
3589 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3591 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3593 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3595 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3597 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3599 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3601 0x2000, 0x200a, 0}, /* Various spaces */
3603 0x2070, 0x207f, 2}, /* superscript */
3605 0x2080, 0x208f, 2}, /* subscript */
3607 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3609 0x3000, 0x3000, 0}, /* ideographic space */
3611 0x3001, 0x3020, 1}, /* ideographic punctuation */
3613 0x303f, 0x309f, 3}, /* Hiragana */
3615 0x30a0, 0x30ff, 3}, /* Katakana */
3617 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3619 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3621 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3623 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3625 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3627 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3629 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3631 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3633 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3637 const struct ucsword
*wptr
;
3639 uc
&= (CSET_MASK
| CHAR_MASK
);
3641 switch (uc
& CSET_MASK
) {
3643 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
3646 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
3649 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
3652 switch (uc
& CSET_MASK
) {
3654 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
3657 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
3661 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3662 * fail as there's such a thing as a double width space. :-(
3664 if (term
->ucsdata
->dbcs_screenfont
&&
3665 term
->ucsdata
->font_codepage
== term
->ucsdata
->line_codepage
)
3669 return term
->wordness
[uc
];
3671 for (wptr
= ucs_words
; wptr
->start
; wptr
++) {
3672 if (uc
>= wptr
->start
&& uc
<= wptr
->end
)
3680 * Spread the selection outwards according to the selection mode.
3682 static pos
sel_spread_half(Terminal
*term
, pos p
, int dir
)
3684 unsigned long *ldata
;
3686 int topy
= -count234(term
->scrollback
);
3688 ldata
= lineptr(p
.y
);
3690 switch (term
->selmode
) {
3693 * In this mode, every character is a separate unit, except
3694 * for runs of spaces at the end of a non-wrapping line.
3696 if (!(ldata
[term
->cols
] & LATTR_WRAPPED
)) {
3697 unsigned long *q
= ldata
+ term
->cols
;
3698 while (q
> ldata
&& (q
[-1] & CHAR_MASK
) == 0x20)
3700 if (q
== ldata
+ term
->cols
)
3702 if (p
.x
>= q
- ldata
)
3703 p
.x
= (dir
== -1 ? q
- ldata
: term
->cols
- 1);
3708 * In this mode, the units are maximal runs of characters
3709 * whose `wordness' has the same value.
3711 wvalue
= wordtype(term
, UCSGET(ldata
, p
.x
));
3714 int maxcols
= (ldata
[term
->cols
] & LATTR_WRAPPED2 ?
3715 term
->cols
-1 : term
->cols
);
3716 if (p
.x
< maxcols
-1) {
3717 if (wordtype(term
, UCSGET(ldata
, p
.x
+ 1)) == wvalue
)
3722 if (ldata
[term
->cols
] & LATTR_WRAPPED
) {
3723 unsigned long *ldata2
;
3724 ldata2
= lineptr(p
.y
+1);
3725 if (wordtype(term
, UCSGET(ldata2
, 0)) == wvalue
) {
3738 if (wordtype(term
, UCSGET(ldata
, p
.x
- 1)) == wvalue
)
3743 unsigned long *ldata2
;
3747 ldata2
= lineptr(p
.y
-1);
3748 maxcols
= (ldata2
[term
->cols
] & LATTR_WRAPPED2 ?
3749 term
->cols
-1 : term
->cols
);
3750 if (ldata2
[term
->cols
] & LATTR_WRAPPED
) {
3751 if (wordtype(term
, UCSGET(ldata2
, maxcols
-1))
3766 * In this mode, every line is a unit.
3768 p
.x
= (dir
== -1 ?
0 : term
->cols
- 1);
3774 static void sel_spread(Terminal
*term
)
3776 if (term
->seltype
== LEXICOGRAPHIC
) {
3777 term
->selstart
= sel_spread_half(term
, term
->selstart
, -1);
3778 decpos(term
->selend
);
3779 term
->selend
= sel_spread_half(term
, term
->selend
, +1);
3780 incpos(term
->selend
);
3784 void term_do_paste(Terminal
*term
)
3789 get_clip(term
->frontend
, &data
, &len
);
3790 if (data
&& len
> 0) {
3793 term_seen_key_event(term
); /* pasted data counts */
3795 if (term
->paste_buffer
)
3796 sfree(term
->paste_buffer
);
3797 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
3798 term
->paste_buffer
= smalloc(len
* sizeof(wchar_t));
3801 while (p
< data
+ len
) {
3802 while (p
< data
+ len
&&
3803 !(p
<= data
+ len
- sel_nl_sz
&&
3804 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
3809 for (i
= 0; i
< p
- q
; i
++) {
3810 term
->paste_buffer
[term
->paste_len
++] = q
[i
];
3814 if (p
<= data
+ len
- sel_nl_sz
&&
3815 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
3816 term
->paste_buffer
[term
->paste_len
++] = '\015';
3822 /* Assume a small paste will be OK in one go. */
3823 if (term
->paste_len
< 256) {
3825 luni_send(term
->ldisc
, term
->paste_buffer
, term
->paste_len
, 0);
3826 if (term
->paste_buffer
)
3827 sfree(term
->paste_buffer
);
3828 term
->paste_buffer
= 0;
3829 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
3832 get_clip(term
->frontend
, NULL
, NULL
);
3835 void term_mouse(Terminal
*term
, Mouse_Button braw
, Mouse_Button bcooked
,
3836 Mouse_Action a
, int x
, int y
, int shift
, int ctrl
, int alt
)
3839 unsigned long *ldata
;
3840 int raw_mouse
= (term
->xterm_mouse
&&
3841 !term
->cfg
.no_mouse_rep
&&
3842 !(term
->cfg
.mouse_override
&& shift
));
3843 int default_seltype
;
3847 if (a
== MA_DRAG
&& !raw_mouse
)
3848 term_scroll(term
, 0, -1);
3850 if (y
>= term
->rows
) {
3852 if (a
== MA_DRAG
&& !raw_mouse
)
3853 term_scroll(term
, 0, +1);
3862 if (x
>= term
->cols
)
3865 selpoint
.y
= y
+ term
->disptop
;
3867 ldata
= lineptr(selpoint
.y
);
3868 if ((ldata
[term
->cols
] & LATTR_MODE
) != LATTR_NORM
)
3872 int encstate
= 0, r
, c
;
3879 encstate
= 0x20; /* left button down */
3890 case MBT_WHEEL_DOWN
:
3893 default: break; /* placate gcc warning about enum use */
3897 if (term
->xterm_mouse
== 1)
3903 term
->mouse_is_down
= 0;
3906 if (term
->mouse_is_down
== braw
)
3908 term
->mouse_is_down
= braw
;
3910 default: break; /* placate gcc warning about enum use */
3919 sprintf(abuf
, "\033[M%c%c%c", encstate
, c
, r
);
3920 ldisc_send(term
->ldisc
, abuf
, 6, 0);
3926 * Set the selection type (rectangular or normal) at the start
3927 * of a selection attempt, from the state of Alt.
3929 if (!alt
^ !term
->cfg
.rect_select
)
3930 default_seltype
= RECTANGULAR
;
3932 default_seltype
= LEXICOGRAPHIC
;
3934 if (term
->selstate
== NO_SELECTION
) {
3935 term
->seltype
= default_seltype
;
3938 if (bcooked
== MBT_SELECT
&& a
== MA_CLICK
) {
3940 term
->selstate
= ABOUT_TO
;
3941 term
->seltype
= default_seltype
;
3942 term
->selanchor
= selpoint
;
3943 term
->selmode
= SM_CHAR
;
3944 } else if (bcooked
== MBT_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
3946 term
->selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
3947 term
->selstate
= DRAGGING
;
3948 term
->selstart
= term
->selanchor
= selpoint
;
3949 term
->selend
= term
->selstart
;
3950 incpos(term
->selend
);
3952 } else if ((bcooked
== MBT_SELECT
&& a
== MA_DRAG
) ||
3953 (bcooked
== MBT_EXTEND
&& a
!= MA_RELEASE
)) {
3954 if (term
->selstate
== ABOUT_TO
&& poseq(term
->selanchor
, selpoint
))
3956 if (bcooked
== MBT_EXTEND
&& a
!= MA_DRAG
&&
3957 term
->selstate
== SELECTED
) {
3958 if (term
->seltype
== LEXICOGRAPHIC
) {
3960 * For normal selection, we extend by moving
3961 * whichever end of the current selection is closer
3964 if (posdiff(selpoint
, term
->selstart
) <
3965 posdiff(term
->selend
, term
->selstart
) / 2) {
3966 term
->selanchor
= term
->selend
;
3967 decpos(term
->selanchor
);
3969 term
->selanchor
= term
->selstart
;
3973 * For rectangular selection, we have a choice of
3974 * _four_ places to put selanchor and selpoint: the
3975 * four corners of the selection.
3977 if (2*selpoint
.x
< term
->selstart
.x
+ term
->selend
.x
)
3978 term
->selanchor
.x
= term
->selend
.x
-1;
3980 term
->selanchor
.x
= term
->selstart
.x
;
3982 if (2*selpoint
.y
< term
->selstart
.y
+ term
->selend
.y
)
3983 term
->selanchor
.y
= term
->selend
.y
;
3985 term
->selanchor
.y
= term
->selstart
.y
;
3987 term
->selstate
= DRAGGING
;
3989 if (term
->selstate
!= ABOUT_TO
&& term
->selstate
!= DRAGGING
)
3990 term
->selanchor
= selpoint
;
3991 term
->selstate
= DRAGGING
;
3992 if (term
->seltype
== LEXICOGRAPHIC
) {
3994 * For normal selection, we set (selstart,selend) to
3995 * (selpoint,selanchor) in some order.
3997 if (poslt(selpoint
, term
->selanchor
)) {
3998 term
->selstart
= selpoint
;
3999 term
->selend
= term
->selanchor
;
4000 incpos(term
->selend
);
4002 term
->selstart
= term
->selanchor
;
4003 term
->selend
= selpoint
;
4004 incpos(term
->selend
);
4008 * For rectangular selection, we may need to
4009 * interchange x and y coordinates (if the user has
4010 * dragged in the -x and +y directions, or vice versa).
4012 term
->selstart
.x
= min(term
->selanchor
.x
, selpoint
.x
);
4013 term
->selend
.x
= 1+max(term
->selanchor
.x
, selpoint
.x
);
4014 term
->selstart
.y
= min(term
->selanchor
.y
, selpoint
.y
);
4015 term
->selend
.y
= max(term
->selanchor
.y
, selpoint
.y
);
4018 } else if ((bcooked
== MBT_SELECT
|| bcooked
== MBT_EXTEND
) &&
4020 if (term
->selstate
== DRAGGING
) {
4022 * We've completed a selection. We now transfer the
4023 * data to the clipboard.
4025 clipme(term
, term
->selstart
, term
->selend
,
4026 (term
->seltype
== RECTANGULAR
));
4027 term
->selstate
= SELECTED
;
4029 term
->selstate
= NO_SELECTION
;
4030 } else if (bcooked
== MBT_PASTE
4032 #if MULTICLICK_ONLY_EVENT
4033 || a
== MA_2CLK
|| a
== MA_3CLK
4036 request_paste(term
->frontend
);
4042 void term_key(Terminal
*term
, Key_Sym keysym
, wchar_t *text
, size_t tlen
,
4043 unsigned int modifiers
, unsigned int flags
)
4047 int prependesc
= FALSE
;
4051 fprintf(stderr
, "keysym = %d, %d chars:", keysym
, tlen
);
4052 for (i
= 0; i
< tlen
; i
++)
4053 fprintf(stderr
, " %04x", text
[i
]);
4054 fprintf(stderr
, "\n");
4058 if ((flags
& PKF_REPEAT
) && term
->repeat_off
)
4061 /* Currently, Meta always just prefixes everything with ESC. */
4062 if (modifiers
& PKM_META
)
4064 modifiers
&= ~PKM_META
;
4067 * Alt is only used for Alt+keypad, which isn't supported yet, so
4070 modifiers
&= ~PKM_ALT
;
4072 /* Standard local function keys */
4073 switch (modifiers
& (PKM_SHIFT
| PKM_CONTROL
)) {
4075 if (keysym
== PK_PAGEUP
)
4076 /* scroll up one page */;
4077 if (keysym
== PK_PAGEDOWN
)
4078 /* scroll down on page */;
4079 if (keysym
== PK_INSERT
)
4080 term_do_paste(term
);
4083 if (keysym
== PK_PAGEUP
)
4084 /* scroll up one line */;
4085 if (keysym
== PK_PAGEDOWN
)
4086 /* scroll down one line */;
4087 /* Control-Numlock for app-keypad mode switch */
4088 if (keysym
== PK_PF1
)
4089 term
->app_keypad_keys
^= 1;
4093 if (modifiers
& PKM_ALT
) {
4094 /* Alt+F4 (close) */
4095 /* Alt+Return (full screen) */
4096 /* Alt+Space (system menu) */
4099 if (keysym
== PK_NULL
&& (modifiers
& PKM_CONTROL
) && tlen
== 1 &&
4100 text
[0] >= 0x20 && text
[0] <= 0x7e) {
4101 /* ASCII chars + Control */
4102 if (text
[0] >= 0x40 && text
[0] <= 0x5f ||
4103 text
[0] >= 0x61 && text
[0] <= 0x7a)
4107 * Control-2 should return ^@ (0x00), Control-6 should return
4108 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
4109 * the DOS keyboard handling did it, and we have nothing better
4110 * to do with the key combo in question, we'll also map
4111 * Control-Backquote to ^\ (0x1C).
4114 case ' ': text
[0] = 0x00; break;
4115 case '-': text
[0] = 0x1f; break;
4116 case '/': text
[0] = 0x1f; break;
4117 case '2': text
[0] = 0x00; break;
4118 case '3': text
[0] = 0x1b; break;
4119 case '4': text
[0] = 0x1c; break;
4120 case '5': text
[0] = 0x1d; break;
4121 case '6': text
[0] = 0x1e; break;
4122 case '7': text
[0] = 0x1f; break;
4123 case '8': text
[0] = 0x7f; break;
4124 case '`': text
[0] = 0x1c; break;
4129 /* Nethack keypad */
4130 if (term
->cfg
.nethack_keypad
) {
4133 case PK_KP1
: c
= 'b'; break;
4134 case PK_KP2
: c
= 'j'; break;
4135 case PK_KP3
: c
= 'n'; break;
4136 case PK_KP4
: c
= 'h'; break;
4137 case PK_KP5
: c
= '.'; break;
4138 case PK_KP6
: c
= 'l'; break;
4139 case PK_KP7
: c
= 'y'; break;
4140 case PK_KP8
: c
= 'k'; break;
4141 case PK_KP9
: c
= 'u'; break;
4145 if (modifiers
& PKM_CONTROL
)
4147 else if (modifiers
& PKM_SHIFT
)
4155 /* Numeric Keypad */
4156 if (PK_ISKEYPAD(keysym
)) {
4160 * In VT400 mode, PFn always emits an escape sequence. In
4161 * Linux and tilde modes, this only happens in app keypad mode.
4163 if (term
->cfg
.funky_type
== FUNKY_VT400
||
4164 ((term
->cfg
.funky_type
== FUNKY_LINUX
||
4165 term
->cfg
.funky_type
== FUNKY_TILDE
) &&
4166 term
->app_keypad_keys
&& !term
->cfg
.no_applic_k
)) {
4168 case PK_PF1
: xkey
= 'P'; break;
4169 case PK_PF2
: xkey
= 'Q'; break;
4170 case PK_PF3
: xkey
= 'R'; break;
4171 case PK_PF4
: xkey
= 'S'; break;
4174 if (term
->app_keypad_keys
&& !term
->cfg
.no_applic_k
) {
4176 case PK_KP0
: xkey
= 'p'; break;
4177 case PK_KP1
: xkey
= 'q'; break;
4178 case PK_KP2
: xkey
= 'r'; break;
4179 case PK_KP3
: xkey
= 's'; break;
4180 case PK_KP4
: xkey
= 't'; break;
4181 case PK_KP5
: xkey
= 'u'; break;
4182 case PK_KP6
: xkey
= 'v'; break;
4183 case PK_KP7
: xkey
= 'w'; break;
4184 case PK_KP8
: xkey
= 'x'; break;
4185 case PK_KP9
: xkey
= 'y'; break;
4186 case PK_KPDECIMAL
: xkey
= 'n'; break;
4187 case PK_KPENTER
: xkey
= 'M'; break;
4189 if (term
->cfg
.funky_type
== FUNKY_XTERM
&& tlen
> 0) {
4191 * xterm can't see the layout of the keypad, so it has
4192 * to rely on the X keysyms returned by the keys.
4193 * Hence, we look at the strings here, not the PuTTY
4194 * keysyms (which describe the layout).
4198 if (modifiers
& PKM_SHIFT
)
4203 case '/': xkey
= 'o'; break;
4204 case '*': xkey
= 'j'; break;
4205 case '-': xkey
= 'm'; break;
4209 * In all other modes, we try to retain the layout of
4210 * the DEC keypad in application mode.
4214 /* This key covers the '-' and ',' keys on a VT220 */
4215 if (modifiers
& PKM_SHIFT
)
4216 xkey
= 'm'; /* VT220 '-' */
4218 xkey
= 'l'; /* VT220 ',' */
4220 case PK_KPMINUS
: xkey
= 'm'; break;
4221 case PK_KPCOMMA
: xkey
= 'l'; break;
4226 if (term
->vt52_mode
) {
4227 if (xkey
>= 'P' && xkey
<= 'S')
4228 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
4230 p
+= sprintf((char *) p
, "\x1B?%c", xkey
);
4232 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
4235 /* Not in application mode -- treat the number pad as arrow keys? */
4236 if ((flags
& PKF_NUMLOCK
) == 0) {
4238 case PK_KP0
: keysym
= PK_INSERT
; break;
4239 case PK_KP1
: keysym
= PK_END
; break;
4240 case PK_KP2
: keysym
= PK_DOWN
; break;
4241 case PK_KP3
: keysym
= PK_PAGEDOWN
; break;
4242 case PK_KP4
: keysym
= PK_LEFT
; break;
4243 case PK_KP5
: keysym
= PK_REST
; break;
4244 case PK_KP6
: keysym
= PK_RIGHT
; break;
4245 case PK_KP7
: keysym
= PK_HOME
; break;
4246 case PK_KP8
: keysym
= PK_UP
; break;
4247 case PK_KP9
: keysym
= PK_PAGEUP
; break;
4252 /* Miscellaneous keys */
4259 *p
++ = (term
->cfg
.bksp_is_delete ?
0x7F : 0x08);
4260 else if (modifiers
== PKM_SHIFT
)
4261 /* We do the opposite of what is configured */
4262 *p
++ = (term
->cfg
.bksp_is_delete ?
0x08 : 0x7F);
4268 else if (modifiers
== PKM_SHIFT
)
4269 *p
++ = 0x1B, *p
++ = '[', *p
++ = 'Z';
4272 /* XXX window.c has ctrl+shift+space sending 0xa0 */
4274 if (modifiers
== PKM_CONTROL
)
4279 case PK_KPENTER
: /* Odd keypad modes handled above */
4280 if (modifiers
== 0) {
4282 if (term
->cr_lf_return
)
4288 /* SCO function keys and editing keys */
4289 if (term
->cfg
.funky_type
== FUNKY_SCO
) {
4290 if (PK_ISFKEY(keysym
) && keysym
<= PK_F12
) {
4291 static char const codes
[] =
4292 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
4293 int index
= keysym
- PK_F1
;
4295 if (modifiers
& PKM_SHIFT
) index
+= 12;
4296 if (modifiers
& PKM_CONTROL
) index
+= 24;
4297 p
+= sprintf((char *) p
, "\x1B[%c", codes
[index
]);
4300 if (PK_ISEDITING(keysym
)) {
4304 case PK_DELETE
: *p
++ = 0x7f; goto done
;
4305 case PK_HOME
: xkey
= 'H'; break;
4306 case PK_INSERT
: xkey
= 'L'; break;
4307 case PK_END
: xkey
= 'F'; break;
4308 case PK_PAGEUP
: xkey
= 'I'; break;
4309 case PK_PAGEDOWN
: xkey
= 'G'; break;
4311 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
4315 if (PK_ISEDITING(keysym
) && (modifiers
& PKM_SHIFT
) == 0) {
4318 if (term
->cfg
.funky_type
== FUNKY_XTERM
) {
4319 /* Xterm shuffles these keys, apparently. */
4321 case PK_HOME
: keysym
= PK_INSERT
; break;
4322 case PK_INSERT
: keysym
= PK_HOME
; break;
4323 case PK_DELETE
: keysym
= PK_END
; break;
4324 case PK_END
: keysym
= PK_PAGEUP
; break;
4325 case PK_PAGEUP
: keysym
= PK_DELETE
; break;
4326 case PK_PAGEDOWN
: keysym
= PK_PAGEDOWN
; break;
4331 if (term
->cfg
.rxvt_homeend
&&
4332 (keysym
== PK_HOME
|| keysym
== PK_END
)) {
4333 p
+= sprintf((char *) p
, keysym
== PK_HOME ?
"\x1B[H" : "\x1BOw");
4337 if (term
->vt52_mode
) {
4341 * A real VT52 doesn't have these, and a VT220 doesn't
4342 * send anything for them in VT52 mode.
4345 case PK_HOME
: xkey
= 'H'; break;
4346 case PK_INSERT
: xkey
= 'L'; break;
4347 case PK_DELETE
: xkey
= 'M'; break;
4348 case PK_END
: xkey
= 'E'; break;
4349 case PK_PAGEUP
: xkey
= 'I'; break;
4350 case PK_PAGEDOWN
: xkey
= 'G'; break;
4352 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
4357 case PK_HOME
: code
= 1; break;
4358 case PK_INSERT
: code
= 2; break;
4359 case PK_DELETE
: code
= 3; break;
4360 case PK_END
: code
= 4; break;
4361 case PK_PAGEUP
: code
= 5; break;
4362 case PK_PAGEDOWN
: code
= 6; break;
4364 p
+= sprintf((char *) p
, "\x1B[%d~", code
);
4368 if (PK_ISFKEY(keysym
)) {
4369 /* Map Shift+F1-F10 to F11-F20 */
4370 if (keysym
>= PK_F1
&& keysym
<= PK_F10
&& (modifiers
& PKM_SHIFT
))
4372 if ((term
->vt52_mode
|| term
->cfg
.funky_type
== FUNKY_VT100P
) &&
4374 /* XXX This overrides the XTERM/VT52 mode below */
4376 if (keysym
>= PK_F6
) offt
++;
4377 if (keysym
>= PK_F12
) offt
++;
4378 p
+= sprintf((char *) p
, term
->vt52_mode ?
"\x1B%c" : "\x1BO%c",
4379 'P' + keysym
- PK_F1
- offt
);
4382 if (term
->cfg
.funky_type
== FUNKY_LINUX
&& keysym
<= PK_F5
) {
4383 p
+= sprintf((char *) p
, "\x1B[[%c", 'A' + keysym
- PK_F1
);
4386 if (term
->cfg
.funky_type
== FUNKY_XTERM
&& keysym
<= PK_F4
) {
4387 if (term
->vt52_mode
)
4388 p
+= sprintf((char *) p
, "\x1B%c", 'P' + keysym
- PK_F1
);
4390 p
+= sprintf((char *) p
, "\x1BO%c", 'P' + keysym
- PK_F1
);
4393 p
+= sprintf((char *) p
, "\x1B[%d~", 11 + keysym
- PK_F1
);
4397 if (PK_ISCURSOR(keysym
)) {
4401 case PK_UP
: xkey
= 'A'; break;
4402 case PK_DOWN
: xkey
= 'B'; break;
4403 case PK_RIGHT
: xkey
= 'C'; break;
4404 case PK_LEFT
: xkey
= 'D'; break;
4405 case PK_REST
: xkey
= 'G'; break; /* centre key on number pad */
4407 if (term
->vt52_mode
)
4408 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
4410 int app_flg
= (term
->app_cursor_keys
&& !term
->cfg
.no_applic_c
);
4412 /* Useful mapping of Ctrl-arrows */
4413 if (modifiers
== PKM_CONTROL
)
4417 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
4419 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
4425 if (p
> output
|| tlen
> 0) {
4427 * Interrupt an ongoing paste. I'm not sure
4428 * this is sensible, but for the moment it's
4429 * preferable to having to faff about buffering
4435 * We need not bother about stdin backlogs
4436 * here, because in GUI PuTTY we can't do
4437 * anything about it anyway; there's no means
4438 * of asking Windows to hold off on KEYDOWN
4439 * messages. We _have_ to buffer everything
4442 term_seen_key_event(term
);
4446 fprintf(stderr
, "sending ESC\n");
4448 ldisc_send(term
->ldisc
, "\x1b", 1, 1);
4453 fprintf(stderr
, "sending %d bytes:", p
- output
);
4454 for (i
= 0; i
< p
- output
; i
++)
4455 fprintf(stderr
, " %02x", output
[i
]);
4456 fprintf(stderr
, "\n");
4458 ldisc_send(term
->ldisc
, output
, p
- output
, 1);
4459 } else if (tlen
> 0) {
4461 fprintf(stderr
, "sending %d unichars:", tlen
);
4462 for (i
= 0; i
< tlen
; i
++)
4463 fprintf(stderr
, " %04x", text
[i
]);
4464 fprintf(stderr
, "\n");
4466 luni_send(term
->ldisc
, text
, tlen
, 1);
4471 void term_nopaste(Terminal
*term
)
4473 if (term
->paste_len
== 0)
4475 sfree(term
->paste_buffer
);
4476 term
->paste_buffer
= NULL
;
4477 term
->paste_len
= 0;
4480 int term_paste_pending(Terminal
*term
)
4482 return term
->paste_len
!= 0;
4485 void term_paste(Terminal
*term
)
4487 long now
, paste_diff
;
4489 if (term
->paste_len
== 0)
4492 /* Don't wait forever to paste */
4493 if (term
->paste_hold
) {
4494 now
= GETTICKCOUNT();
4495 paste_diff
= now
- term
->last_paste
;
4496 if (paste_diff
>= 0 && paste_diff
< 450)
4499 term
->paste_hold
= 0;
4501 while (term
->paste_pos
< term
->paste_len
) {
4503 while (n
+ term
->paste_pos
< term
->paste_len
) {
4504 if (term
->paste_buffer
[term
->paste_pos
+ n
++] == '\015')
4508 luni_send(term
->ldisc
, term
->paste_buffer
+ term
->paste_pos
, n
, 0);
4509 term
->paste_pos
+= n
;
4511 if (term
->paste_pos
< term
->paste_len
) {
4512 term
->paste_hold
= 1;
4516 sfree(term
->paste_buffer
);
4517 term
->paste_buffer
= NULL
;
4518 term
->paste_len
= 0;
4521 static void deselect(Terminal
*term
)
4523 term
->selstate
= NO_SELECTION
;
4524 term
->selstart
.x
= term
->selstart
.y
= term
->selend
.x
= term
->selend
.y
= 0;
4527 void term_deselect(Terminal
*term
)
4533 int term_ldisc(Terminal
*term
, int option
)
4535 if (option
== LD_ECHO
)
4536 return term
->term_echoing
;
4537 if (option
== LD_EDIT
)
4538 return term
->term_editing
;
4543 * from_backend(), to get data from the backend for the terminal.
4545 int from_backend(void *vterm
, int is_stderr
, char *data
, int len
)
4547 Terminal
*term
= (Terminal
*)vterm
;
4551 bufchain_add(&term
->inbuf
, data
, len
);
4554 * term_out() always completely empties inbuf. Therefore,
4555 * there's no reason at all to return anything other than zero
4556 * from this function, because there _can't_ be a question of
4557 * the remote side needing to wait until term_out() has cleared
4560 * This is a slightly suboptimal way to deal with SSH2 - in
4561 * principle, the window mechanism would allow us to continue
4562 * to accept data on forwarded ports and X connections even
4563 * while the terminal processing was going slowly - but we
4564 * can't do the 100% right thing without moving the terminal
4565 * processing into a separate thread, and that might hurt
4566 * portability. So we manage stdout buffering the old SSH1 way:
4567 * if the terminal processing goes slowly, the whole SSH
4568 * connection stops accepting data until it's ready.
4570 * In practice, I can't imagine this causing serious trouble.
4575 void term_provide_logctx(Terminal
*term
, void *logctx
)
4577 term
->logctx
= logctx
;