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
= sresize(line
, 2 + cols
, TTYPE
);
104 for (i
= oldlen
; i
< cols
; i
++)
105 line
[i
+ 1] = ERASE_CHAR
;
106 line
[cols
+ 1] = lineattrs
& LATTR_MODE
;
113 * Get the number of lines in the scrollback.
115 static int sblines(Terminal
*term
)
117 int sblines
= count234(term
->scrollback
);
118 if (term
->cfg
.erase_to_scrollback
&&
119 term
->alt_which
&& term
->alt_screen
) {
120 sblines
+= term
->alt_sblines
;
126 * Retrieve a line of the screen or of the scrollback, according to
127 * whether the y coordinate is non-negative or negative
130 static unsigned long *lineptr(Terminal
*term
, int y
, int lineno
)
132 unsigned long *line
, *newline
;
137 whichtree
= term
->screen
;
141 if (term
->cfg
.erase_to_scrollback
&&
142 term
->alt_which
&& term
->alt_screen
) {
143 altlines
= term
->alt_sblines
;
146 whichtree
= term
->scrollback
;
147 treeindex
= y
+ altlines
+ count234(term
->scrollback
);
149 whichtree
= term
->alt_screen
;
150 treeindex
= y
+ term
->alt_sblines
;
151 /* treeindex = y + count234(term->alt_screen); */
154 line
= index234(whichtree
, treeindex
);
156 /* We assume that we don't screw up and retrieve something out of range. */
157 assert(line
!= NULL
);
159 newline
= resizeline(line
, term
->cols
);
160 if (newline
!= line
) {
161 delpos234(whichtree
, treeindex
);
162 addpos234(whichtree
, newline
, treeindex
);
169 #define lineptr(x) lineptr(term,x,__LINE__)
172 * Set up power-on settings for the terminal.
174 static void power_on(Terminal
*term
)
176 term
->curs
.x
= term
->curs
.y
= 0;
177 term
->alt_x
= term
->alt_y
= 0;
178 term
->savecurs
.x
= term
->savecurs
.y
= 0;
179 term
->alt_t
= term
->marg_t
= 0;
180 if (term
->rows
!= -1)
181 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
183 term
->alt_b
= term
->marg_b
= 0;
184 if (term
->cols
!= -1) {
186 for (i
= 0; i
< term
->cols
; i
++)
187 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
189 term
->alt_om
= term
->dec_om
= term
->cfg
.dec_om
;
190 term
->alt_ins
= term
->insert
= FALSE
;
191 term
->alt_wnext
= term
->wrapnext
= term
->save_wnext
= FALSE
;
192 term
->alt_wrap
= term
->wrap
= term
->cfg
.wrap_mode
;
193 term
->alt_cset
= term
->cset
= term
->save_cset
= 0;
194 term
->alt_utf
= term
->utf
= term
->save_utf
= 0;
196 term
->alt_sco_acs
= term
->sco_acs
= term
->save_sco_acs
= 0;
197 term
->cset_attr
[0] = term
->cset_attr
[1] = term
->save_csattr
= ATTR_ASCII
;
199 term
->in_vbell
= FALSE
;
201 term
->big_cursor
= 0;
202 term
->save_attr
= term
->curr_attr
= ATTR_DEFAULT
;
203 term
->term_editing
= term
->term_echoing
= FALSE
;
204 term
->app_cursor_keys
= term
->cfg
.app_cursor
;
205 term
->app_keypad_keys
= term
->cfg
.app_keypad
;
206 term
->use_bce
= term
->cfg
.bce
;
207 term
->blink_is_real
= term
->cfg
.blinktext
;
208 term
->erase_char
= ERASE_CHAR
;
210 term_print_finish(term
);
213 for (i
= 0; i
< 256; i
++)
214 term
->wordness
[i
] = term
->cfg
.wordness
[i
];
217 swap_screen(term
, 1, FALSE
, FALSE
);
218 erase_lots(term
, FALSE
, TRUE
, TRUE
);
219 swap_screen(term
, 0, FALSE
, FALSE
);
220 erase_lots(term
, FALSE
, TRUE
, TRUE
);
225 * Force a screen update.
227 void term_update(Terminal
*term
)
230 ctx
= get_ctx(term
->frontend
);
232 int need_sbar_update
= term
->seen_disp_event
;
233 if (term
->seen_disp_event
&& term
->cfg
.scroll_on_disp
) {
234 term
->disptop
= 0; /* return to main screen */
235 term
->seen_disp_event
= 0;
236 need_sbar_update
= TRUE
;
238 if (need_sbar_update
)
240 do_paint(term
, ctx
, TRUE
);
241 sys_cursor(term
->frontend
, term
->curs
.x
, term
->curs
.y
- term
->disptop
);
247 * Called from front end when a keypress occurs, to trigger
248 * anything magical that needs to happen in that situation.
250 void term_seen_key_event(Terminal
*term
)
253 * On any keypress, clear the bell overload mechanism
254 * completely, on the grounds that large numbers of
255 * beeps coming from deliberate key action are likely
256 * to be intended (e.g. beeps from filename completion
257 * blocking repeatedly).
259 term
->beep_overloaded
= FALSE
;
260 while (term
->beephead
) {
261 struct beeptime
*tmp
= term
->beephead
;
262 term
->beephead
= tmp
->next
;
265 term
->beeptail
= NULL
;
269 * Reset the scrollback on keypress, if we're doing that.
271 if (term
->cfg
.scroll_on_key
) {
272 term
->disptop
= 0; /* return to main screen */
273 term
->seen_disp_event
= 1;
278 * Same as power_on(), but an external function.
280 void term_pwron(Terminal
*term
)
283 if (term
->ldisc
) /* cause ldisc to notice changes */
284 ldisc_send(term
->ldisc
, NULL
, 0, 0);
292 * When the user reconfigures us, we need to check the forbidden-
293 * alternate-screen config option, disable raw mouse mode if the
294 * user has disabled mouse reporting, and abandon a print job if
295 * the user has disabled printing.
297 void term_reconfig(Terminal
*term
, Config
*cfg
)
300 * Before adopting the new config, check all those terminal
301 * settings which control power-on defaults; and if they've
302 * changed, we will modify the current state as well as the
303 * default one. The full list is: Auto wrap mode, DEC Origin
304 * Mode, BCE, blinking text, character classes.
306 int reset_wrap
, reset_decom
, reset_bce
, reset_blink
, reset_charclass
;
309 reset_wrap
= (term
->cfg
.wrap_mode
!= cfg
->wrap_mode
);
310 reset_decom
= (term
->cfg
.dec_om
!= cfg
->dec_om
);
311 reset_bce
= (term
->cfg
.bce
!= cfg
->bce
);
312 reset_blink
= (term
->cfg
.blinktext
!= cfg
->blinktext
);
314 for (i
= 0; i
< lenof(term
->cfg
.wordness
); i
++)
315 if (term
->cfg
.wordness
[i
] != cfg
->wordness
[i
])
318 term
->cfg
= *cfg
; /* STRUCTURE COPY */
321 term
->alt_wrap
= term
->wrap
= term
->cfg
.wrap_mode
;
323 term
->alt_om
= term
->dec_om
= term
->cfg
.dec_om
;
325 term
->use_bce
= term
->cfg
.bce
;
327 term
->blink_is_real
= term
->cfg
.blinktext
;
329 for (i
= 0; i
< 256; i
++)
330 term
->wordness
[i
] = term
->cfg
.wordness
[i
];
332 if (term
->cfg
.no_alt_screen
)
333 swap_screen(term
, 0, FALSE
, FALSE
);
334 if (term
->cfg
.no_mouse_rep
) {
335 term
->xterm_mouse
= 0;
336 set_raw_mouse_mode(term
->frontend
, 0);
338 if (term
->cfg
.no_remote_charset
) {
339 term
->cset_attr
[0] = term
->cset_attr
[1] = ATTR_ASCII
;
340 term
->sco_acs
= term
->alt_sco_acs
= 0;
343 if (!*term
->cfg
.printer
) {
344 term_print_finish(term
);
349 * Clear the scrollback.
351 void term_clrsb(Terminal
*term
)
355 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
) {
358 term
->tempsblines
= 0;
359 term
->alt_sblines
= 0;
364 * Initialise the terminal.
366 Terminal
*term_init(Config
*mycfg
, struct unicode_data
*ucsdata
,
372 * Allocate a new Terminal structure and initialise the fields
375 term
= snew(Terminal
);
376 term
->frontend
= frontend
;
377 term
->ucsdata
= ucsdata
;
378 term
->cfg
= *mycfg
; /* STRUCTURE COPY */
380 term
->compatibility_level
= TM_PUTTY
;
381 strcpy(term
->id_string
, "\033[?6c");
382 term
->last_blink
= term
->last_tblink
= 0;
383 term
->paste_buffer
= NULL
;
385 term
->last_paste
= 0;
386 bufchain_init(&term
->inbuf
);
387 bufchain_init(&term
->printer_buf
);
388 term
->printing
= term
->only_printing
= FALSE
;
389 term
->print_job
= NULL
;
390 term
->vt52_mode
= FALSE
;
391 term
->cr_lf_return
= FALSE
;
392 term
->seen_disp_event
= FALSE
;
393 term
->xterm_mouse
= term
->mouse_is_down
= FALSE
;
394 term
->reset_132
= FALSE
;
395 term
->blinker
= term
->tblinker
= 0;
397 term
->repeat_off
= FALSE
;
398 term
->termstate
= TOPLEVEL
;
399 term
->selstate
= NO_SELECTION
;
402 term
->screen
= term
->alt_screen
= term
->scrollback
= NULL
;
403 term
->tempsblines
= 0;
404 term
->alt_sblines
= 0;
406 term
->disptext
= term
->dispcurs
= NULL
;
409 term
->rows
= term
->cols
= -1;
411 term
->beephead
= term
->beeptail
= NULL
;
412 #ifdef OPTIMISE_SCROLL
413 term
->scrollhead
= term
->scrolltail
= NULL
;
414 #endif /* OPTIMISE_SCROLL */
416 term
->lastbeep
= FALSE
;
417 term
->beep_overloaded
= FALSE
;
418 term
->attr_mask
= 0xffffffff;
419 term
->resize_fn
= NULL
;
420 term
->resize_ctx
= NULL
;
425 void term_free(Terminal
*term
)
428 struct beeptime
*beep
;
430 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
)
432 freetree234(term
->scrollback
);
433 while ((line
= delpos234(term
->screen
, 0)) != NULL
)
435 freetree234(term
->screen
);
436 while ((line
= delpos234(term
->alt_screen
, 0)) != NULL
)
438 freetree234(term
->alt_screen
);
439 sfree(term
->disptext
);
440 while (term
->beephead
) {
441 beep
= term
->beephead
;
442 term
->beephead
= beep
->next
;
445 bufchain_clear(&term
->inbuf
);
447 printer_finish_job(term
->print_job
);
448 bufchain_clear(&term
->printer_buf
);
449 sfree(term
->paste_buffer
);
454 * Set up the terminal for a given size.
456 void term_size(Terminal
*term
, int newrows
, int newcols
, int newsavelines
)
459 unsigned long *newdisp
, *line
;
462 int save_alt_which
= term
->alt_which
;
464 if (newrows
== term
->rows
&& newcols
== term
->cols
&&
465 newsavelines
== term
->savelines
)
466 return; /* nothing to do */
469 swap_screen(term
, 0, FALSE
, FALSE
);
471 term
->alt_t
= term
->marg_t
= 0;
472 term
->alt_b
= term
->marg_b
= newrows
- 1;
474 if (term
->rows
== -1) {
475 term
->scrollback
= newtree234(NULL
);
476 term
->screen
= newtree234(NULL
);
477 term
->tempsblines
= 0;
482 * Resize the screen and scrollback. We only need to shift
483 * lines around within our data structures, because lineptr()
484 * will take care of resizing each individual line if
487 * - If the new screen is longer, we shunt lines in from temporary
488 * scrollback if possible, otherwise we add new blank lines at
491 * - If the new screen is shorter, we remove any blank lines at
492 * the bottom if possible, otherwise shunt lines above the cursor
493 * to scrollback if possible, otherwise delete lines below the
496 * - Then, if the new scrollback length is less than the
497 * amount of scrollback we actually have, we must throw some
500 sblen
= count234(term
->scrollback
);
501 /* Do this loop to expand the screen if newrows > rows */
502 assert(term
->rows
== count234(term
->screen
));
503 while (term
->rows
< newrows
) {
504 if (term
->tempsblines
> 0) {
505 /* Insert a line from the scrollback at the top of the screen. */
506 assert(sblen
>= term
->tempsblines
);
507 line
= delpos234(term
->scrollback
, --sblen
);
508 term
->tempsblines
-= 1;
509 addpos234(term
->screen
, line
, 0);
511 term
->savecurs
.y
+= 1;
513 /* Add a new blank line at the bottom of the screen. */
514 line
= snewn(newcols
+ 2, TTYPE
);
516 for (j
= 0; j
< newcols
; j
++)
517 line
[j
+ 1] = ERASE_CHAR
;
518 line
[newcols
+ 1] = LATTR_NORM
;
519 addpos234(term
->screen
, line
, count234(term
->screen
));
523 /* Do this loop to shrink the screen if newrows < rows */
524 while (term
->rows
> newrows
) {
525 if (term
->curs
.y
< term
->rows
- 1) {
526 /* delete bottom row, unless it contains the cursor */
527 sfree(delpos234(term
->screen
, term
->rows
- 1));
529 /* push top row to scrollback */
530 line
= delpos234(term
->screen
, 0);
531 addpos234(term
->scrollback
, line
, sblen
++);
532 term
->tempsblines
+= 1;
534 term
->savecurs
.y
-= 1;
538 assert(term
->rows
== newrows
);
539 assert(count234(term
->screen
) == newrows
);
541 /* Delete any excess lines from the scrollback. */
542 while (sblen
> newsavelines
) {
543 line
= delpos234(term
->scrollback
, 0);
547 if (sblen
< term
->tempsblines
)
548 term
->tempsblines
= sblen
;
549 assert(count234(term
->scrollback
) <= newsavelines
);
550 assert(count234(term
->scrollback
) >= term
->tempsblines
);
553 /* Make a new displayed text buffer. */
554 newdisp
= snewn(newrows
* (newcols
+ 1), TTYPE
);
555 for (i
= 0; i
< newrows
* (newcols
+ 1); i
++)
556 newdisp
[i
] = ATTR_INVALID
;
557 sfree(term
->disptext
);
558 term
->disptext
= newdisp
;
559 term
->dispcurs
= NULL
;
561 /* Make a new alternate screen. */
562 newalt
= newtree234(NULL
);
563 for (i
= 0; i
< newrows
; i
++) {
564 line
= snewn(newcols
+ 2, TTYPE
);
566 for (j
= 0; j
< newcols
; j
++)
567 line
[j
+ 1] = term
->erase_char
;
568 line
[newcols
+ 1] = LATTR_NORM
;
569 addpos234(newalt
, line
, i
);
571 if (term
->alt_screen
) {
572 while (NULL
!= (line
= delpos234(term
->alt_screen
, 0)))
574 freetree234(term
->alt_screen
);
576 term
->alt_screen
= newalt
;
577 term
->alt_sblines
= 0;
579 term
->tabs
= sresize(term
->tabs
, newcols
, unsigned char);
582 for (i
= (term
->cols
> 0 ? term
->cols
: 0); i
< newcols
; i
++)
583 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
586 /* Check that the cursor positions are still valid. */
587 if (term
->savecurs
.y
< 0)
588 term
->savecurs
.y
= 0;
589 if (term
->savecurs
.y
>= newrows
)
590 term
->savecurs
.y
= newrows
- 1;
591 if (term
->curs
.y
< 0)
593 if (term
->curs
.y
>= newrows
)
594 term
->curs
.y
= newrows
- 1;
595 if (term
->curs
.x
>= newcols
)
596 term
->curs
.x
= newcols
- 1;
597 term
->alt_x
= term
->alt_y
= 0;
598 term
->wrapnext
= term
->alt_wnext
= FALSE
;
600 term
->rows
= newrows
;
601 term
->cols
= newcols
;
602 term
->savelines
= newsavelines
;
605 swap_screen(term
, save_alt_which
, FALSE
, FALSE
);
610 term
->resize_fn(term
->resize_ctx
, term
->cols
, term
->rows
);
614 * Hand a function and context pointer to the terminal which it can
615 * use to notify a back end of resizes.
617 void term_provide_resize_fn(Terminal
*term
,
618 void (*resize_fn
)(void *, int, int),
621 term
->resize_fn
= resize_fn
;
622 term
->resize_ctx
= resize_ctx
;
623 if (term
->cols
> 0 && term
->rows
> 0)
624 resize_fn(resize_ctx
, term
->cols
, term
->rows
);
627 /* Find the bottom line on the screen that has any content.
628 * If only the top line has content, returns 0.
629 * If no lines have content, return -1.
631 static int find_last_nonempty_line(Terminal
* term
, tree234
* screen
)
634 for (i
= count234(screen
) - 1; i
>= 0; i
--) {
635 unsigned long *line
= index234(screen
, i
);
638 for (j
= 0; j
< cols
; j
++) {
639 if (line
[j
+ 1] != term
->erase_char
) break;
641 if (j
!= cols
) break;
647 * Swap screens. If `reset' is TRUE and we have been asked to
648 * switch to the alternate screen, we must bring most of its
649 * configuration from the main screen and erase the contents of the
650 * alternate screen completely. (This is even true if we're already
651 * on it! Blame xterm.)
653 static void swap_screen(Terminal
*term
, int which
, int reset
, int keep_cur_pos
)
659 reset
= FALSE
; /* do no weird resetting if which==0 */
661 if (which
!= term
->alt_which
) {
662 term
->alt_which
= which
;
664 ttr
= term
->alt_screen
;
665 term
->alt_screen
= term
->screen
;
667 term
->alt_sblines
= find_last_nonempty_line(term
, term
->alt_screen
) + 1;
669 if (!reset
&& !keep_cur_pos
)
670 term
->curs
.x
= term
->alt_x
;
673 if (!reset
&& !keep_cur_pos
)
674 term
->curs
.y
= term
->alt_y
;
677 if (!reset
) term
->marg_t
= term
->alt_t
;
680 if (!reset
) term
->marg_b
= term
->alt_b
;
683 if (!reset
) term
->dec_om
= term
->alt_om
;
686 if (!reset
) term
->wrap
= term
->alt_wrap
;
689 if (!reset
) term
->wrapnext
= term
->alt_wnext
;
692 if (!reset
) term
->insert
= term
->alt_ins
;
695 if (!reset
) term
->cset
= term
->alt_cset
;
698 if (!reset
) term
->utf
= term
->alt_utf
;
701 if (!reset
) term
->sco_acs
= term
->alt_sco_acs
;
702 term
->alt_sco_acs
= t
;
705 if (reset
&& term
->screen
) {
707 * Yes, this _is_ supposed to honour background-colour-erase.
709 erase_lots(term
, FALSE
, TRUE
, TRUE
);
713 * This might not be possible if we're called during
721 * Update the scroll bar.
723 static void update_sbar(Terminal
*term
)
725 int nscroll
= sblines(term
);
726 set_sbar(term
->frontend
, nscroll
+ term
->rows
,
727 nscroll
+ term
->disptop
, term
->rows
);
731 * Check whether the region bounded by the two pointers intersects
732 * the scroll region, and de-select the on-screen selection if so.
734 static void check_selection(Terminal
*term
, pos from
, pos to
)
736 if (poslt(from
, term
->selend
) && poslt(term
->selstart
, to
))
741 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
742 * for backward.) `sb' is TRUE if the scrolling is permitted to
743 * affect the scrollback buffer.
745 * NB this function invalidates all pointers into lines of the
746 * screen data structures. In particular, you MUST call fix_cpos
747 * after calling scroll() and before doing anything else that
748 * uses the cpos shortcut pointer.
750 static void scroll(Terminal
*term
, int topline
, int botline
, int lines
, int sb
)
752 unsigned long *line
, *line2
;
753 int i
, seltop
, olddisptop
, shift
;
755 if (topline
!= 0 || term
->alt_which
!= 0)
758 olddisptop
= term
->disptop
;
762 line
= delpos234(term
->screen
, botline
);
763 line
= resizeline(line
, term
->cols
);
764 for (i
= 0; i
< term
->cols
; i
++)
765 line
[i
+ 1] = term
->erase_char
;
766 line
[term
->cols
+ 1] = 0;
767 addpos234(term
->screen
, line
, topline
);
769 if (term
->selstart
.y
>= topline
&& term
->selstart
.y
<= botline
) {
771 if (term
->selstart
.y
> botline
) {
772 term
->selstart
.y
= botline
+ 1;
773 term
->selstart
.x
= 0;
776 if (term
->selend
.y
>= topline
&& term
->selend
.y
<= botline
) {
778 if (term
->selend
.y
> botline
) {
779 term
->selend
.y
= botline
+ 1;
788 line
= delpos234(term
->screen
, topline
);
789 if (sb
&& term
->savelines
> 0) {
790 int sblen
= count234(term
->scrollback
);
792 * We must add this line to the scrollback. We'll
793 * remove a line from the top of the scrollback to
794 * replace it, or allocate a new one if the
795 * scrollback isn't full.
797 if (sblen
== term
->savelines
) {
798 sblen
--, line2
= delpos234(term
->scrollback
, 0);
800 line2
= snewn(term
->cols
+ 2, TTYPE
);
801 line2
[0] = term
->cols
;
802 term
->tempsblines
+= 1;
804 addpos234(term
->scrollback
, line
, sblen
);
808 * If the user is currently looking at part of the
809 * scrollback, and they haven't enabled any options
810 * that are going to reset the scrollback as a
811 * result of this movement, then the chances are
812 * they'd like to keep looking at the same line. So
813 * we move their viewpoint at the same rate as the
814 * scroll, at least until their viewpoint hits the
815 * top end of the scrollback buffer, at which point
816 * we don't have the choice any more.
818 * Thanks to Jan Holmen Holsten for the idea and
819 * initial implementation.
821 if (term
->disptop
> -term
->savelines
&& term
->disptop
< 0)
824 line
= resizeline(line
, term
->cols
);
825 for (i
= 0; i
< term
->cols
; i
++)
826 line
[i
+ 1] = term
->erase_char
;
827 line
[term
->cols
+ 1] = LATTR_NORM
;
828 addpos234(term
->screen
, line
, botline
);
831 * If the selection endpoints move into the scrollback,
832 * we keep them moving until they hit the top. However,
833 * of course, if the line _hasn't_ moved into the
834 * scrollback then we don't do this, and cut them off
835 * at the top of the scroll region.
837 * This applies to selstart and selend (for an existing
838 * selection), and also selanchor (for one being
839 * selected as we speak).
841 seltop
= sb ?
-term
->savelines
: topline
;
843 if (term
->selstart
.y
>= seltop
&&
844 term
->selstart
.y
<= botline
) {
846 if (term
->selstart
.y
< seltop
) {
847 term
->selstart
.y
= seltop
;
848 term
->selstart
.x
= 0;
851 if (term
->selend
.y
>= seltop
&& term
->selend
.y
<= botline
) {
853 if (term
->selend
.y
< seltop
) {
854 term
->selend
.y
= seltop
;
858 if (term
->selanchor
.y
>= seltop
&& term
->selanchor
.y
<= botline
) {
860 if (term
->selanchor
.y
< seltop
) {
861 term
->selanchor
.y
= seltop
;
862 term
->selanchor
.x
= 0;
869 #ifdef OPTIMISE_SCROLL
870 shift
+= term
->disptop
- olddisptop
;
871 if (shift
< term
->rows
&& shift
> -term
->rows
&& shift
!= 0)
872 scroll_display(term
, topline
, botline
, shift
);
873 #endif /* OPTIMISE_SCROLL */
876 #ifdef OPTIMISE_SCROLL
878 * Add a scroll of a region on the screen into the pending scroll list.
879 * `lines' is +ve for scrolling forward, -ve for backward.
881 * If the scroll is on the same area as the last scroll in the list,
884 static void save_scroll(Terminal
*term
, int topline
, int botline
, int lines
)
886 struct scrollregion
*newscroll
;
887 if (term
->scrolltail
&&
888 term
->scrolltail
->topline
== topline
&&
889 term
->scrolltail
->botline
== botline
) {
890 term
->scrolltail
->lines
+= lines
;
892 newscroll
= snew(struct scrollregion
);
893 newscroll
->topline
= topline
;
894 newscroll
->botline
= botline
;
895 newscroll
->lines
= lines
;
896 newscroll
->next
= NULL
;
898 if (!term
->scrollhead
)
899 term
->scrollhead
= newscroll
;
901 term
->scrolltail
->next
= newscroll
;
902 term
->scrolltail
= newscroll
;
907 * Scroll the physical display, and our conception of it in disptext.
909 static void scroll_display(Terminal
*term
, int topline
, int botline
, int lines
)
911 unsigned long *start
, *end
;
912 int distance
, size
, i
;
914 start
= term
->disptext
+ topline
* (term
->cols
+ 1);
915 end
= term
->disptext
+ (botline
+ 1) * (term
->cols
+ 1);
916 distance
= (lines
> 0 ? lines
: -lines
) * (term
->cols
+ 1);
917 size
= end
- start
- distance
;
919 memmove(start
, start
+ distance
, size
* TSIZE
);
920 if (term
->dispcurs
>= start
+ distance
&&
921 term
->dispcurs
<= start
+ distance
+ size
)
922 term
->dispcurs
-= distance
;
923 for (i
= 0; i
< distance
; i
++)
924 (start
+ size
)[i
] |= ATTR_INVALID
;
926 memmove(start
+ distance
, start
, size
* TSIZE
);
927 if (term
->dispcurs
>= start
&& term
->dispcurs
<= start
+ size
)
928 term
->dispcurs
+= distance
;
929 for (i
= 0; i
< distance
; i
++)
930 start
[i
] |= ATTR_INVALID
;
932 save_scroll(term
, topline
, botline
, lines
);
934 #endif /* OPTIMISE_SCROLL */
937 * Move the cursor to a given position, clipping at boundaries. We
938 * may or may not want to clip at the scroll margin: marg_clip is 0
939 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
940 * even _being_ outside the margins.
942 static void move(Terminal
*term
, int x
, int y
, int marg_clip
)
949 if ((term
->curs
.y
>= term
->marg_t
|| marg_clip
== 2) &&
952 if ((term
->curs
.y
<= term
->marg_b
|| marg_clip
== 2) &&
963 term
->wrapnext
= FALSE
;
967 * Save or restore the cursor and SGR mode.
969 static void save_cursor(Terminal
*term
, int save
)
972 term
->savecurs
= term
->curs
;
973 term
->save_attr
= term
->curr_attr
;
974 term
->save_cset
= term
->cset
;
975 term
->save_utf
= term
->utf
;
976 term
->save_wnext
= term
->wrapnext
;
977 term
->save_csattr
= term
->cset_attr
[term
->cset
];
978 term
->save_sco_acs
= term
->sco_acs
;
980 term
->curs
= term
->savecurs
;
981 /* Make sure the window hasn't shrunk since the save */
982 if (term
->curs
.x
>= term
->cols
)
983 term
->curs
.x
= term
->cols
- 1;
984 if (term
->curs
.y
>= term
->rows
)
985 term
->curs
.y
= term
->rows
- 1;
987 term
->curr_attr
= term
->save_attr
;
988 term
->cset
= term
->save_cset
;
989 term
->utf
= term
->save_utf
;
990 term
->wrapnext
= term
->save_wnext
;
992 * wrapnext might reset to False if the x position is no
993 * longer at the rightmost edge.
995 if (term
->wrapnext
&& term
->curs
.x
< term
->cols
-1)
996 term
->wrapnext
= FALSE
;
997 term
->cset_attr
[term
->cset
] = term
->save_csattr
;
998 term
->sco_acs
= term
->save_sco_acs
;
1001 term
->erase_char
= (' ' | ATTR_ASCII
|
1003 (ATTR_FGMASK
| ATTR_BGMASK
)));
1008 * This function is called before doing _anything_ which affects
1009 * only part of a line of text. It is used to mark the boundary
1010 * between two character positions, and it indicates that some sort
1011 * of effect is going to happen on only one side of that boundary.
1013 * The effect of this function is to check whether a CJK
1014 * double-width character is straddling the boundary, and to remove
1015 * it and replace it with two spaces if so. (Of course, one or
1016 * other of those spaces is then likely to be replaced with
1017 * something else again, as a result of whatever happens next.)
1019 * Also, if the boundary is at the right-hand _edge_ of the screen,
1020 * it implies something deliberate is being done to the rightmost
1021 * column position; hence we must clear LATTR_WRAPPED2.
1023 * The input to the function is the coordinates of the _second_
1024 * character of the pair.
1026 static void check_boundary(Terminal
*term
, int x
, int y
)
1028 unsigned long *ldata
;
1030 /* Validate input coordinates, just in case. */
1031 if (x
== 0 || x
> term
->cols
)
1035 if (x
== term
->cols
) {
1036 ldata
[x
] &= ~LATTR_WRAPPED2
;
1038 if ((ldata
[x
] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
) {
1039 ldata
[x
-1] = ldata
[x
] =
1040 (ldata
[x
-1] &~ (CHAR_MASK
| CSET_MASK
)) | ATTR_ASCII
| ' ';
1046 * Erase a large portion of the screen: the whole screen, or the
1047 * whole line, or parts thereof.
1049 static void erase_lots(Terminal
*term
,
1050 int line_only
, int from_begin
, int to_end
)
1054 int erasing_lines_from_top
= 0;
1057 start
.y
= term
->curs
.y
;
1059 end
.y
= term
->curs
.y
+ 1;
1061 erase_lattr
= FALSE
;
1076 if (!from_begin
|| !to_end
)
1077 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1078 check_selection(term
, start
, end
);
1080 /* Clear screen also forces a full window redraw, just in case. */
1081 if (start
.y
== 0 && start
.x
== 0 && end
.y
== term
->rows
)
1082 term_invalidate(term
);
1084 /* Lines scrolled away shouldn't be brought back on if the terminal
1086 if (start
.y
== 0 && start
.x
== 0 && end
.x
== 0 && erase_lattr
)
1087 erasing_lines_from_top
= 1;
1089 if (term
->cfg
.erase_to_scrollback
&& erasing_lines_from_top
) {
1090 /* If it's a whole number of lines, starting at the top, and
1091 * we're fully erasing them, erase by scrolling and keep the
1092 * lines in the scrollback. */
1093 int scrolllines
= end
.y
;
1094 if (end
.y
== term
->rows
) {
1095 /* Shrink until we find a non-empty row.*/
1096 scrolllines
= find_last_nonempty_line(term
, term
->screen
) + 1;
1098 if (scrolllines
> 0)
1099 scroll(term
, 0, scrolllines
- 1, scrolllines
, TRUE
);
1102 unsigned long *ldata
= lineptr(start
.y
);
1103 while (poslt(start
, end
)) {
1104 if (start
.x
== term
->cols
) {
1106 ldata
[start
.x
] &= ~(LATTR_WRAPPED
| LATTR_WRAPPED2
);
1108 ldata
[start
.x
] = LATTR_NORM
;
1110 ldata
[start
.x
] = term
->erase_char
;
1112 if (incpos(start
) && start
.y
< term
->rows
)
1113 ldata
= lineptr(start
.y
);
1117 /* After an erase of lines from the top of the screen, we shouldn't
1118 * bring the lines back again if the terminal enlarges (since the user or
1119 * application has explictly thrown them away). */
1120 if (erasing_lines_from_top
&& !(term
->alt_which
))
1121 term
->tempsblines
= 0;
1125 * Insert or delete characters within the current line. n is +ve if
1126 * insertion is desired, and -ve for deletion.
1128 static void insch(Terminal
*term
, int n
)
1130 int dir
= (n
< 0 ?
-1 : +1);
1133 unsigned long *ldata
;
1135 n
= (n
< 0 ?
-n
: n
);
1136 if (n
> term
->cols
- term
->curs
.x
)
1137 n
= term
->cols
- term
->curs
.x
;
1138 m
= term
->cols
- term
->curs
.x
- n
;
1139 cursplus
.y
= term
->curs
.y
;
1140 cursplus
.x
= term
->curs
.x
+ n
;
1141 check_selection(term
, term
->curs
, cursplus
);
1142 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1144 check_boundary(term
, term
->curs
.x
+ n
, term
->curs
.y
);
1145 ldata
= lineptr(term
->curs
.y
);
1147 memmove(ldata
+ term
->curs
.x
, ldata
+ term
->curs
.x
+ n
, m
* TSIZE
);
1149 ldata
[term
->curs
.x
+ m
++] = term
->erase_char
;
1151 memmove(ldata
+ term
->curs
.x
+ n
, ldata
+ term
->curs
.x
, m
* TSIZE
);
1153 ldata
[term
->curs
.x
+ n
] = term
->erase_char
;
1158 * Toggle terminal mode `mode' to state `state'. (`query' indicates
1159 * whether the mode is a DEC private one or a normal one.)
1161 static void toggle_mode(Terminal
*term
, int mode
, int query
, int state
)
1163 unsigned long ticks
;
1167 case 1: /* application cursor keys */
1168 term
->app_cursor_keys
= state
;
1170 case 2: /* VT52 mode */
1171 term
->vt52_mode
= !state
;
1172 if (term
->vt52_mode
) {
1173 term
->blink_is_real
= FALSE
;
1174 term
->vt52_bold
= FALSE
;
1176 term
->blink_is_real
= term
->cfg
.blinktext
;
1179 case 3: /* 80/132 columns */
1181 if (!term
->cfg
.no_remote_resize
)
1182 request_resize(term
->frontend
, state ?
132 : 80, term
->rows
);
1183 term
->reset_132
= state
;
1184 term
->alt_t
= term
->marg_t
= 0;
1185 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
1186 move(term
, 0, 0, 0);
1187 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1189 case 5: /* reverse video */
1191 * Toggle reverse video. If we receive an OFF within the
1192 * visual bell timeout period after an ON, we trigger an
1193 * effective visual bell, so that ESC[?5hESC[?5l will
1194 * always be an actually _visible_ visual bell.
1196 ticks
= GETTICKCOUNT();
1197 /* turn off a previous vbell to avoid inconsistencies */
1198 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
1199 term
->in_vbell
= FALSE
;
1200 if (term
->rvideo
&& !state
&& /* we're turning it off... */
1201 (ticks
- term
->rvbell_startpoint
) < VBELL_TIMEOUT
) {/*...soon*/
1202 /* If there's no vbell timeout already, or this one lasts
1203 * longer, replace vbell_timeout with ours. */
1204 if (!term
->in_vbell
||
1205 (term
->rvbell_startpoint
- term
->vbell_startpoint
<
1207 term
->vbell_startpoint
= term
->rvbell_startpoint
;
1208 term
->in_vbell
= TRUE
; /* may clear rvideo but set in_vbell */
1209 } else if (!term
->rvideo
&& state
) {
1210 /* This is an ON, so we notice the time and save it. */
1211 term
->rvbell_startpoint
= ticks
;
1213 term
->rvideo
= state
;
1214 term
->seen_disp_event
= TRUE
;
1218 case 6: /* DEC origin mode */
1219 term
->dec_om
= state
;
1221 case 7: /* auto wrap */
1224 case 8: /* auto key repeat */
1225 term
->repeat_off
= !state
;
1227 case 10: /* set local edit mode */
1228 term
->term_editing
= state
;
1229 if (term
->ldisc
) /* cause ldisc to notice changes */
1230 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1232 case 25: /* enable/disable cursor */
1233 compatibility2(OTHER
, VT220
);
1234 term
->cursor_on
= state
;
1235 term
->seen_disp_event
= TRUE
;
1237 case 47: /* alternate screen */
1238 compatibility(OTHER
);
1240 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, FALSE
, FALSE
);
1243 case 1000: /* xterm mouse 1 */
1244 term
->xterm_mouse
= state ?
1 : 0;
1245 set_raw_mouse_mode(term
->frontend
, state
);
1247 case 1002: /* xterm mouse 2 */
1248 term
->xterm_mouse
= state ?
2 : 0;
1249 set_raw_mouse_mode(term
->frontend
, state
);
1251 case 1047: /* alternate screen */
1252 compatibility(OTHER
);
1254 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, TRUE
, TRUE
);
1257 case 1048: /* save/restore cursor */
1258 save_cursor(term
, state
);
1259 if (!state
) term
->seen_disp_event
= TRUE
;
1261 case 1049: /* cursor & alternate screen */
1263 save_cursor(term
, state
);
1264 if (!state
) term
->seen_disp_event
= TRUE
;
1265 compatibility(OTHER
);
1267 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, TRUE
, FALSE
);
1269 save_cursor(term
, state
);
1274 case 4: /* set insert mode */
1275 compatibility(VT102
);
1276 term
->insert
= state
;
1278 case 12: /* set echo mode */
1279 term
->term_echoing
= !state
;
1280 if (term
->ldisc
) /* cause ldisc to notice changes */
1281 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1283 case 20: /* Return sends ... */
1284 term
->cr_lf_return
= state
;
1286 case 34: /* Make cursor BIG */
1287 compatibility2(OTHER
, VT220
);
1288 term
->big_cursor
= !state
;
1293 * Process an OSC sequence: set window title or icon name.
1295 static void do_osc(Terminal
*term
)
1298 while (term
->osc_strlen
--)
1299 term
->wordness
[(unsigned char)
1300 term
->osc_string
[term
->osc_strlen
]] = term
->esc_args
[0];
1302 term
->osc_string
[term
->osc_strlen
] = '\0';
1303 switch (term
->esc_args
[0]) {
1306 if (!term
->cfg
.no_remote_wintitle
)
1307 set_icon(term
->frontend
, term
->osc_string
);
1308 if (term
->esc_args
[0] == 1)
1310 /* fall through: parameter 0 means set both */
1313 if (!term
->cfg
.no_remote_wintitle
)
1314 set_title(term
->frontend
, term
->osc_string
);
1321 * ANSI printing routines.
1323 static void term_print_setup(Terminal
*term
)
1325 bufchain_clear(&term
->printer_buf
);
1326 term
->print_job
= printer_start_job(term
->cfg
.printer
);
1328 static void term_print_flush(Terminal
*term
)
1333 while ((size
= bufchain_size(&term
->printer_buf
)) > 5) {
1334 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
1337 printer_job_data(term
->print_job
, data
, len
);
1338 bufchain_consume(&term
->printer_buf
, len
);
1341 static void term_print_finish(Terminal
*term
)
1347 if (!term
->printing
&& !term
->only_printing
)
1348 return; /* we need do nothing */
1350 term_print_flush(term
);
1351 while ((size
= bufchain_size(&term
->printer_buf
)) > 0) {
1352 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
1354 if (c
== '\033' || c
== '\233') {
1355 bufchain_consume(&term
->printer_buf
, size
);
1358 printer_job_data(term
->print_job
, &c
, 1);
1359 bufchain_consume(&term
->printer_buf
, 1);
1362 printer_finish_job(term
->print_job
);
1363 term
->print_job
= NULL
;
1364 term
->printing
= term
->only_printing
= FALSE
;
1368 * Remove everything currently in `inbuf' and stick it up on the
1369 * in-memory display. There's a big state machine in here to
1370 * process escape sequences...
1372 void term_out(Terminal
*term
)
1375 unsigned char localbuf
[256], *chars
;
1380 chars
= NULL
; /* placate compiler warnings */
1381 while (nchars
> 0 || bufchain_size(&term
->inbuf
) > 0) {
1385 bufchain_prefix(&term
->inbuf
, &ret
, &nchars
);
1386 if (nchars
> sizeof(localbuf
))
1387 nchars
= sizeof(localbuf
);
1388 memcpy(localbuf
, ret
, nchars
);
1389 bufchain_consume(&term
->inbuf
, nchars
);
1391 assert(chars
!= NULL
);
1397 * Optionally log the session traffic to a file. Useful for
1398 * debugging and possibly also useful for actual logging.
1400 if (term
->cfg
.logtype
== LGTYP_DEBUG
&& term
->logctx
)
1401 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_DEBUG
);
1407 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1408 * be able to display 8-bit characters, but I'll let that go 'cause
1413 * If we're printing, add the character to the printer
1416 if (term
->printing
) {
1417 bufchain_add(&term
->printer_buf
, &c
, 1);
1420 * If we're in print-only mode, we use a much simpler
1421 * state machine designed only to recognise the ESC[4i
1422 * termination sequence.
1424 if (term
->only_printing
) {
1426 term
->print_state
= 1;
1427 else if (c
== (unsigned char)'\233')
1428 term
->print_state
= 2;
1429 else if (c
== '[' && term
->print_state
== 1)
1430 term
->print_state
= 2;
1431 else if (c
== '4' && term
->print_state
== 2)
1432 term
->print_state
= 3;
1433 else if (c
== 'i' && term
->print_state
== 3)
1434 term
->print_state
= 4;
1436 term
->print_state
= 0;
1437 if (term
->print_state
== 4) {
1438 term_print_finish(term
);
1444 /* First see about all those translations. */
1445 if (term
->termstate
== TOPLEVEL
) {
1447 switch (term
->utf_state
) {
1450 /* UTF-8 must be stateless so we ignore iso2022. */
1451 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
1452 c
= term
->ucsdata
->unitab_ctrl
[c
];
1453 else c
= ((unsigned char)c
) | ATTR_ASCII
;
1455 } else if ((c
& 0xe0) == 0xc0) {
1456 term
->utf_size
= term
->utf_state
= 1;
1457 term
->utf_char
= (c
& 0x1f);
1458 } else if ((c
& 0xf0) == 0xe0) {
1459 term
->utf_size
= term
->utf_state
= 2;
1460 term
->utf_char
= (c
& 0x0f);
1461 } else if ((c
& 0xf8) == 0xf0) {
1462 term
->utf_size
= term
->utf_state
= 3;
1463 term
->utf_char
= (c
& 0x07);
1464 } else if ((c
& 0xfc) == 0xf8) {
1465 term
->utf_size
= term
->utf_state
= 4;
1466 term
->utf_char
= (c
& 0x03);
1467 } else if ((c
& 0xfe) == 0xfc) {
1468 term
->utf_size
= term
->utf_state
= 5;
1469 term
->utf_char
= (c
& 0x01);
1480 if ((c
& 0xC0) != 0x80) {
1483 term
->utf_state
= 0;
1486 term
->utf_char
= (term
->utf_char
<< 6) | (c
& 0x3f);
1487 if (--term
->utf_state
)
1492 /* Is somebody trying to be evil! */
1494 (c
< 0x800 && term
->utf_size
>= 2) ||
1495 (c
< 0x10000 && term
->utf_size
>= 3) ||
1496 (c
< 0x200000 && term
->utf_size
>= 4) ||
1497 (c
< 0x4000000 && term
->utf_size
>= 5))
1500 /* Unicode line separator and paragraph separator are CR-LF */
1501 if (c
== 0x2028 || c
== 0x2029)
1504 /* High controls are probably a Baaad idea too. */
1508 /* The UTF-16 surrogates are not nice either. */
1509 /* The standard give the option of decoding these:
1510 * I don't want to! */
1511 if (c
>= 0xD800 && c
< 0xE000)
1514 /* ISO 10646 characters now limited to UTF-16 range. */
1518 /* This is currently a TagPhobic application.. */
1519 if (c
>= 0xE0000 && c
<= 0xE007F)
1522 /* U+FEFF is best seen as a null. */
1525 /* But U+FFFE is an error. */
1526 if (c
== 0xFFFE || c
== 0xFFFF)
1529 /* Oops this is a 16bit implementation */
1534 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1535 else if(term
->sco_acs
&&
1536 (c
!='\033' && c
!='\012' && c
!='\015' && c
!='\b'))
1538 if (term
->sco_acs
== 2) c
|= 0x80;
1541 switch (term
->cset_attr
[term
->cset
]) {
1543 * Linedraw characters are different from 'ESC ( B'
1544 * only for a small range. For ones outside that
1545 * range, make sure we use the same font as well as
1546 * the same encoding.
1549 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
1550 c
= term
->ucsdata
->unitab_ctrl
[c
];
1552 c
= ((unsigned char) c
) | ATTR_LINEDRW
;
1556 /* If UK-ASCII, make the '#' a LineDraw Pound */
1558 c
= '}' | ATTR_LINEDRW
;
1561 /*FALLTHROUGH*/ case ATTR_ASCII
:
1562 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
1563 c
= term
->ucsdata
->unitab_ctrl
[c
];
1565 c
= ((unsigned char) c
) | ATTR_ASCII
;
1568 if (c
>=' ') c
= ((unsigned char)c
) | ATTR_SCOACS
;
1574 /* How about C1 controls ? */
1575 if ((c
& -32) == 0x80 && term
->termstate
< DO_CTRLS
&&
1576 !term
->vt52_mode
&& has_compat(VT220
)) {
1577 term
->termstate
= SEEN_ESC
;
1578 term
->esc_query
= FALSE
;
1579 c
= '@' + (c
& 0x1F);
1582 /* Or the GL control. */
1583 if (c
== '\177' && term
->termstate
< DO_CTRLS
&& has_compat(OTHER
)) {
1584 if (term
->curs
.x
&& !term
->wrapnext
)
1586 term
->wrapnext
= FALSE
;
1588 if (!term
->cfg
.no_dbackspace
) /* destructive bksp might be disabled */
1589 *term
->cpos
= (' ' | term
->curr_attr
| ATTR_ASCII
);
1591 /* Or normal C0 controls. */
1592 if ((c
& -32) == 0 && term
->termstate
< DO_CTRLS
) {
1594 case '\005': /* terminal type query */
1595 /* Strictly speaking this is VT100 but a VT100 defaults to
1596 * no response. Other terminals respond at their option.
1598 * Don't put a CR in the default string as this tends to
1599 * upset some weird software.
1601 * An xterm returns "xterm" (5 characters)
1603 compatibility(ANSIMIN
);
1605 char abuf
[256], *s
, *d
;
1607 for (s
= term
->cfg
.answerback
, d
= abuf
; *s
; s
++) {
1609 if (*s
>= 'a' && *s
<= 'z')
1610 *d
++ = (*s
- ('a' - 1));
1611 else if ((*s
>= '@' && *s
<= '_') ||
1612 *s
== '?' || (*s
& 0x80))
1617 } else if (*s
== '^') {
1622 lpage_send(term
->ldisc
, DEFAULT_CODEPAGE
,
1628 struct beeptime
*newbeep
;
1629 unsigned long ticks
;
1631 ticks
= GETTICKCOUNT();
1633 if (!term
->beep_overloaded
) {
1634 newbeep
= snew(struct beeptime
);
1635 newbeep
->ticks
= ticks
;
1636 newbeep
->next
= NULL
;
1637 if (!term
->beephead
)
1638 term
->beephead
= newbeep
;
1640 term
->beeptail
->next
= newbeep
;
1641 term
->beeptail
= newbeep
;
1646 * Throw out any beeps that happened more than
1649 while (term
->beephead
&&
1650 term
->beephead
->ticks
< ticks
- term
->cfg
.bellovl_t
) {
1651 struct beeptime
*tmp
= term
->beephead
;
1652 term
->beephead
= tmp
->next
;
1654 if (!term
->beephead
)
1655 term
->beeptail
= NULL
;
1659 if (term
->cfg
.bellovl
&& term
->beep_overloaded
&&
1660 ticks
- term
->lastbeep
>= (unsigned)term
->cfg
.bellovl_s
) {
1662 * If we're currently overloaded and the
1663 * last beep was more than s seconds ago,
1664 * leave overload mode.
1666 term
->beep_overloaded
= FALSE
;
1667 } else if (term
->cfg
.bellovl
&& !term
->beep_overloaded
&&
1668 term
->nbeeps
>= term
->cfg
.bellovl_n
) {
1670 * Now, if we have n or more beeps
1671 * remaining in the queue, go into overload
1674 term
->beep_overloaded
= TRUE
;
1676 term
->lastbeep
= ticks
;
1679 * Perform an actual beep if we're not overloaded.
1681 if (!term
->cfg
.bellovl
|| !term
->beep_overloaded
) {
1682 beep(term
->frontend
, term
->cfg
.beep
);
1683 if (term
->cfg
.beep
== BELL_VISUAL
) {
1684 term
->in_vbell
= TRUE
;
1685 term
->vbell_startpoint
= ticks
;
1693 if (term
->curs
.x
== 0 &&
1694 (term
->curs
.y
== 0 || term
->wrap
== 0))
1696 else if (term
->curs
.x
== 0 && term
->curs
.y
> 0)
1697 term
->curs
.x
= term
->cols
- 1, term
->curs
.y
--;
1698 else if (term
->wrapnext
)
1699 term
->wrapnext
= FALSE
;
1703 term
->seen_disp_event
= TRUE
;
1706 compatibility(VT100
);
1710 compatibility(VT100
);
1714 if (term
->vt52_mode
)
1715 term
->termstate
= VT52_ESC
;
1717 compatibility(ANSIMIN
);
1718 term
->termstate
= SEEN_ESC
;
1719 term
->esc_query
= FALSE
;
1724 term
->wrapnext
= FALSE
;
1726 term
->seen_disp_event
= TRUE
;
1727 term
->paste_hold
= 0;
1729 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
1732 if (has_compat(SCOANSI
)) {
1733 move(term
, 0, 0, 0);
1734 erase_lots(term
, FALSE
, FALSE
, TRUE
);
1736 term
->wrapnext
= FALSE
;
1737 term
->seen_disp_event
= 1;
1741 compatibility(VT100
);
1743 if (term
->curs
.y
== term
->marg_b
)
1744 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1745 else if (term
->curs
.y
< term
->rows
- 1)
1747 if (term
->cfg
.lfhascr
)
1750 term
->wrapnext
= FALSE
;
1751 term
->seen_disp_event
= 1;
1752 term
->paste_hold
= 0;
1754 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
1758 pos old_curs
= term
->curs
;
1759 unsigned long *ldata
= lineptr(term
->curs
.y
);
1763 } while (term
->curs
.x
< term
->cols
- 1 &&
1764 !term
->tabs
[term
->curs
.x
]);
1766 if ((ldata
[term
->cols
] & LATTR_MODE
) != LATTR_NORM
) {
1767 if (term
->curs
.x
>= term
->cols
/ 2)
1768 term
->curs
.x
= term
->cols
/ 2 - 1;
1770 if (term
->curs
.x
>= term
->cols
)
1771 term
->curs
.x
= term
->cols
- 1;
1775 check_selection(term
, old_curs
, term
->curs
);
1777 term
->seen_disp_event
= TRUE
;
1781 switch (term
->termstate
) {
1783 /* Only graphic characters get this far;
1784 * ctrls are stripped above */
1785 if (term
->wrapnext
&& term
->wrap
) {
1786 term
->cpos
[1] |= LATTR_WRAPPED
;
1787 if (term
->curs
.y
== term
->marg_b
)
1788 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1789 else if (term
->curs
.y
< term
->rows
- 1)
1793 term
->wrapnext
= FALSE
;
1797 if (term
->selstate
!= NO_SELECTION
) {
1798 pos cursplus
= term
->curs
;
1800 check_selection(term
, term
->curs
, cursplus
);
1802 if (((c
& CSET_MASK
) == ATTR_ASCII
|| (c
& CSET_MASK
) == 0) &&
1804 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
1810 width
= wcwidth((wchar_t) c
);
1814 * If we're about to display a double-width
1815 * character starting in the rightmost
1816 * column, then we do something special
1817 * instead. We must print a space in the
1818 * last column of the screen, then wrap;
1819 * and we also set LATTR_WRAPPED2 which
1820 * instructs subsequent cut-and-pasting not
1821 * only to splice this line to the one
1822 * after it, but to ignore the space in the
1823 * last character position as well.
1824 * (Because what was actually output to the
1825 * terminal was presumably just a sequence
1826 * of CJK characters, and we don't want a
1827 * space to be pasted in the middle of
1828 * those just because they had the
1829 * misfortune to start in the wrong parity
1830 * column. xterm concurs.)
1832 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1833 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
1834 if (term
->curs
.x
== term
->cols
-1) {
1835 *term
->cpos
++ = ATTR_ASCII
| ' ' | term
->curr_attr
;
1836 *term
->cpos
|= LATTR_WRAPPED
| LATTR_WRAPPED2
;
1837 if (term
->curs
.y
== term
->marg_b
)
1838 scroll(term
, term
->marg_t
, term
->marg_b
,
1840 else if (term
->curs
.y
< term
->rows
- 1)
1844 /* Now we must check_boundary again, of course. */
1845 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1846 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
1848 *term
->cpos
++ = c
| term
->curr_attr
;
1849 *term
->cpos
++ = UCSWIDE
| term
->curr_attr
;
1853 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1854 check_boundary(term
, term
->curs
.x
+1, term
->curs
.y
);
1855 *term
->cpos
++ = c
| term
->curr_attr
;
1862 if (term
->curs
.x
== term
->cols
) {
1865 term
->wrapnext
= TRUE
;
1866 if (term
->wrap
&& term
->vt52_mode
) {
1867 term
->cpos
[1] |= LATTR_WRAPPED
;
1868 if (term
->curs
.y
== term
->marg_b
)
1869 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1870 else if (term
->curs
.y
< term
->rows
- 1)
1874 term
->wrapnext
= FALSE
;
1877 term
->seen_disp_event
= 1;
1882 * This state is virtually identical to SEEN_ESC, with the
1883 * exception that we have an OSC sequence in the pipeline,
1884 * and _if_ we see a backslash, we process it.
1888 term
->termstate
= TOPLEVEL
;
1891 /* else fall through */
1893 if (c
>= ' ' && c
<= '/') {
1894 if (term
->esc_query
)
1895 term
->esc_query
= -1;
1897 term
->esc_query
= c
;
1900 term
->termstate
= TOPLEVEL
;
1901 switch (ANSI(c
, term
->esc_query
)) {
1902 case '[': /* enter CSI mode */
1903 term
->termstate
= SEEN_CSI
;
1904 term
->esc_nargs
= 1;
1905 term
->esc_args
[0] = ARG_DEFAULT
;
1906 term
->esc_query
= FALSE
;
1908 case ']': /* xterm escape sequences */
1909 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1910 compatibility(OTHER
);
1911 term
->termstate
= SEEN_OSC
;
1912 term
->esc_args
[0] = 0;
1914 case '7': /* save cursor */
1915 compatibility(VT100
);
1916 save_cursor(term
, TRUE
);
1918 case '8': /* restore cursor */
1919 compatibility(VT100
);
1920 save_cursor(term
, FALSE
);
1921 term
->seen_disp_event
= TRUE
;
1924 compatibility(VT100
);
1925 term
->app_keypad_keys
= TRUE
;
1928 compatibility(VT100
);
1929 term
->app_keypad_keys
= FALSE
;
1931 case 'D': /* exactly equivalent to LF */
1932 compatibility(VT100
);
1933 if (term
->curs
.y
== term
->marg_b
)
1934 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1935 else if (term
->curs
.y
< term
->rows
- 1)
1938 term
->wrapnext
= FALSE
;
1939 term
->seen_disp_event
= TRUE
;
1941 case 'E': /* exactly equivalent to CR-LF */
1942 compatibility(VT100
);
1944 if (term
->curs
.y
== term
->marg_b
)
1945 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1946 else if (term
->curs
.y
< term
->rows
- 1)
1949 term
->wrapnext
= FALSE
;
1950 term
->seen_disp_event
= TRUE
;
1952 case 'M': /* reverse index - backwards LF */
1953 compatibility(VT100
);
1954 if (term
->curs
.y
== term
->marg_t
)
1955 scroll(term
, term
->marg_t
, term
->marg_b
, -1, TRUE
);
1956 else if (term
->curs
.y
> 0)
1959 term
->wrapnext
= FALSE
;
1960 term
->seen_disp_event
= TRUE
;
1962 case 'Z': /* terminal type query */
1963 compatibility(VT100
);
1965 ldisc_send(term
->ldisc
, term
->id_string
,
1966 strlen(term
->id_string
), 0);
1968 case 'c': /* restore power-on settings */
1969 compatibility(VT100
);
1971 if (term
->ldisc
) /* cause ldisc to notice changes */
1972 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1973 if (term
->reset_132
) {
1974 if (!term
->cfg
.no_remote_resize
)
1975 request_resize(term
->frontend
, 80, term
->rows
);
1976 term
->reset_132
= 0;
1980 term
->seen_disp_event
= TRUE
;
1982 case 'H': /* set a tab */
1983 compatibility(VT100
);
1984 term
->tabs
[term
->curs
.x
] = TRUE
;
1987 case ANSI('8', '#'): /* ESC # 8 fills screen with Es :-) */
1988 compatibility(VT100
);
1990 unsigned long *ldata
;
1994 for (i
= 0; i
< term
->rows
; i
++) {
1996 for (j
= 0; j
< term
->cols
; j
++)
1997 ldata
[j
] = ATTR_DEFAULT
| 'E';
1998 ldata
[term
->cols
] = 0;
2001 term
->seen_disp_event
= TRUE
;
2002 scrtop
.x
= scrtop
.y
= 0;
2004 scrbot
.y
= term
->rows
;
2005 check_selection(term
, scrtop
, scrbot
);
2009 case ANSI('3', '#'):
2010 case ANSI('4', '#'):
2011 case ANSI('5', '#'):
2012 case ANSI('6', '#'):
2013 compatibility(VT100
);
2015 unsigned long nlattr
;
2016 unsigned long *ldata
;
2017 switch (ANSI(c
, term
->esc_query
)) {
2018 case ANSI('3', '#'):
2021 case ANSI('4', '#'):
2024 case ANSI('5', '#'):
2025 nlattr
= LATTR_NORM
;
2027 default: /* spiritually case ANSI('6', '#'): */
2028 nlattr
= LATTR_WIDE
;
2031 ldata
= lineptr(term
->curs
.y
);
2032 ldata
[term
->cols
] &= ~LATTR_MODE
;
2033 ldata
[term
->cols
] |= nlattr
;
2037 case ANSI('A', '('):
2038 compatibility(VT100
);
2039 if (!term
->cfg
.no_remote_charset
)
2040 term
->cset_attr
[0] = ATTR_GBCHR
;
2042 case ANSI('B', '('):
2043 compatibility(VT100
);
2044 if (!term
->cfg
.no_remote_charset
)
2045 term
->cset_attr
[0] = ATTR_ASCII
;
2047 case ANSI('0', '('):
2048 compatibility(VT100
);
2049 if (!term
->cfg
.no_remote_charset
)
2050 term
->cset_attr
[0] = ATTR_LINEDRW
;
2052 case ANSI('U', '('):
2053 compatibility(OTHER
);
2054 if (!term
->cfg
.no_remote_charset
)
2055 term
->cset_attr
[0] = ATTR_SCOACS
;
2058 case ANSI('A', ')'):
2059 compatibility(VT100
);
2060 if (!term
->cfg
.no_remote_charset
)
2061 term
->cset_attr
[1] = ATTR_GBCHR
;
2063 case ANSI('B', ')'):
2064 compatibility(VT100
);
2065 if (!term
->cfg
.no_remote_charset
)
2066 term
->cset_attr
[1] = ATTR_ASCII
;
2068 case ANSI('0', ')'):
2069 compatibility(VT100
);
2070 if (!term
->cfg
.no_remote_charset
)
2071 term
->cset_attr
[1] = ATTR_LINEDRW
;
2073 case ANSI('U', ')'):
2074 compatibility(OTHER
);
2075 if (!term
->cfg
.no_remote_charset
)
2076 term
->cset_attr
[1] = ATTR_SCOACS
;
2079 case ANSI('8', '%'): /* Old Linux code */
2080 case ANSI('G', '%'):
2081 compatibility(OTHER
);
2082 if (!term
->cfg
.no_remote_charset
)
2085 case ANSI('@', '%'):
2086 compatibility(OTHER
);
2087 if (!term
->cfg
.no_remote_charset
)
2093 term
->termstate
= TOPLEVEL
; /* default */
2095 if (term
->esc_nargs
<= ARGS_MAX
) {
2096 if (term
->esc_args
[term
->esc_nargs
- 1] == ARG_DEFAULT
)
2097 term
->esc_args
[term
->esc_nargs
- 1] = 0;
2098 term
->esc_args
[term
->esc_nargs
- 1] =
2099 10 * term
->esc_args
[term
->esc_nargs
- 1] + c
- '0';
2101 term
->termstate
= SEEN_CSI
;
2102 } else if (c
== ';') {
2103 if (++term
->esc_nargs
<= ARGS_MAX
)
2104 term
->esc_args
[term
->esc_nargs
- 1] = ARG_DEFAULT
;
2105 term
->termstate
= SEEN_CSI
;
2106 } else if (c
< '@') {
2107 if (term
->esc_query
)
2108 term
->esc_query
= -1;
2110 term
->esc_query
= TRUE
;
2112 term
->esc_query
= c
;
2113 term
->termstate
= SEEN_CSI
;
2115 switch (ANSI(c
, term
->esc_query
)) {
2116 case 'A': /* move up N lines */
2117 move(term
, term
->curs
.x
,
2118 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
2119 term
->seen_disp_event
= TRUE
;
2121 case 'e': /* move down N lines */
2122 compatibility(ANSI
);
2125 move(term
, term
->curs
.x
,
2126 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
2127 term
->seen_disp_event
= TRUE
;
2129 case ANSI('c', '>'): /* report xterm version */
2130 compatibility(OTHER
);
2131 /* this reports xterm version 136 so that VIM can
2132 use the drag messages from the mouse reporting */
2134 ldisc_send(term
->ldisc
, "\033[>0;136;0c", 11, 0);
2136 case 'a': /* move right N cols */
2137 compatibility(ANSI
);
2140 move(term
, term
->curs
.x
+ def(term
->esc_args
[0], 1),
2142 term
->seen_disp_event
= TRUE
;
2144 case 'D': /* move left N cols */
2145 move(term
, term
->curs
.x
- def(term
->esc_args
[0], 1),
2147 term
->seen_disp_event
= TRUE
;
2149 case 'E': /* move down N lines and CR */
2150 compatibility(ANSI
);
2152 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
2153 term
->seen_disp_event
= TRUE
;
2155 case 'F': /* move up N lines and CR */
2156 compatibility(ANSI
);
2158 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
2159 term
->seen_disp_event
= TRUE
;
2162 case '`': /* set horizontal posn */
2163 compatibility(ANSI
);
2164 move(term
, def(term
->esc_args
[0], 1) - 1,
2166 term
->seen_disp_event
= TRUE
;
2168 case 'd': /* set vertical posn */
2169 compatibility(ANSI
);
2170 move(term
, term
->curs
.x
,
2171 ((term
->dec_om ? term
->marg_t
: 0) +
2172 def(term
->esc_args
[0], 1) - 1),
2173 (term
->dec_om ?
2 : 0));
2174 term
->seen_disp_event
= TRUE
;
2177 case 'f': /* set horz and vert posns at once */
2178 if (term
->esc_nargs
< 2)
2179 term
->esc_args
[1] = ARG_DEFAULT
;
2180 move(term
, def(term
->esc_args
[1], 1) - 1,
2181 ((term
->dec_om ? term
->marg_t
: 0) +
2182 def(term
->esc_args
[0], 1) - 1),
2183 (term
->dec_om ?
2 : 0));
2184 term
->seen_disp_event
= TRUE
;
2186 case 'J': /* erase screen or parts of it */
2188 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
2191 erase_lots(term
, FALSE
, !!(i
& 2), !!(i
& 1));
2194 term
->seen_disp_event
= TRUE
;
2196 case 'K': /* erase line or parts of it */
2198 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
2201 erase_lots(term
, TRUE
, !!(i
& 2), !!(i
& 1));
2203 term
->seen_disp_event
= TRUE
;
2205 case 'L': /* insert lines */
2206 compatibility(VT102
);
2207 if (term
->curs
.y
<= term
->marg_b
)
2208 scroll(term
, term
->curs
.y
, term
->marg_b
,
2209 -def(term
->esc_args
[0], 1), FALSE
);
2211 term
->seen_disp_event
= TRUE
;
2213 case 'M': /* delete lines */
2214 compatibility(VT102
);
2215 if (term
->curs
.y
<= term
->marg_b
)
2216 scroll(term
, term
->curs
.y
, term
->marg_b
,
2217 def(term
->esc_args
[0], 1),
2220 term
->seen_disp_event
= TRUE
;
2222 case '@': /* insert chars */
2223 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
2224 compatibility(VT102
);
2225 insch(term
, def(term
->esc_args
[0], 1));
2226 term
->seen_disp_event
= TRUE
;
2228 case 'P': /* delete chars */
2229 compatibility(VT102
);
2230 insch(term
, -def(term
->esc_args
[0], 1));
2231 term
->seen_disp_event
= TRUE
;
2233 case 'c': /* terminal type query */
2234 compatibility(VT100
);
2235 /* This is the response for a VT102 */
2237 ldisc_send(term
->ldisc
, term
->id_string
,
2238 strlen(term
->id_string
), 0);
2240 case 'n': /* cursor position query */
2242 if (term
->esc_args
[0] == 6) {
2244 sprintf(buf
, "\033[%d;%dR", term
->curs
.y
+ 1,
2246 ldisc_send(term
->ldisc
, buf
, strlen(buf
), 0);
2247 } else if (term
->esc_args
[0] == 5) {
2248 ldisc_send(term
->ldisc
, "\033[0n", 4, 0);
2252 case 'h': /* toggle modes to high */
2254 compatibility(VT100
);
2257 for (i
= 0; i
< term
->esc_nargs
; i
++)
2258 toggle_mode(term
, term
->esc_args
[i
],
2259 term
->esc_query
, TRUE
);
2264 compatibility(VT100
);
2266 if (term
->esc_nargs
!= 1) break;
2267 if (term
->esc_args
[0] == 5 && *term
->cfg
.printer
) {
2268 term
->printing
= TRUE
;
2269 term
->only_printing
= !term
->esc_query
;
2270 term
->print_state
= 0;
2271 term_print_setup(term
);
2272 } else if (term
->esc_args
[0] == 4 &&
2274 term_print_finish(term
);
2278 case 'l': /* toggle modes to low */
2280 compatibility(VT100
);
2283 for (i
= 0; i
< term
->esc_nargs
; i
++)
2284 toggle_mode(term
, term
->esc_args
[i
],
2285 term
->esc_query
, FALSE
);
2288 case 'g': /* clear tabs */
2289 compatibility(VT100
);
2290 if (term
->esc_nargs
== 1) {
2291 if (term
->esc_args
[0] == 0) {
2292 term
->tabs
[term
->curs
.x
] = FALSE
;
2293 } else if (term
->esc_args
[0] == 3) {
2295 for (i
= 0; i
< term
->cols
; i
++)
2296 term
->tabs
[i
] = FALSE
;
2300 case 'r': /* set scroll margins */
2301 compatibility(VT100
);
2302 if (term
->esc_nargs
<= 2) {
2304 top
= def(term
->esc_args
[0], 1) - 1;
2305 bot
= (term
->esc_nargs
<= 1
2306 || term
->esc_args
[1] == 0 ?
2308 def(term
->esc_args
[1], term
->rows
)) - 1;
2309 if (bot
>= term
->rows
)
2310 bot
= term
->rows
- 1;
2311 /* VTTEST Bug 9 - if region is less than 2 lines
2312 * don't change region.
2314 if (bot
- top
> 0) {
2319 * I used to think the cursor should be
2320 * placed at the top of the newly marginned
2321 * area. Apparently not: VMS TPU falls over
2324 * Well actually it should for
2327 term
->curs
.y
= (term
->dec_om ?
2330 term
->seen_disp_event
= TRUE
;
2334 case 'm': /* set graphics rendition */
2337 * A VT100 without the AVO only had one
2338 * attribute, either underline or
2339 * reverse video depending on the
2340 * cursor type, this was selected by
2344 * This is sometimes DIM, eg on the
2347 * This is sometimes INVIS various ANSI.
2349 * This like 22 disables BOLD, DIM and INVIS
2351 * The ANSI colours appear on any
2352 * terminal that has colour (obviously)
2353 * but the interaction between sgr0 and
2354 * the colours varies but is usually
2355 * related to the background colour
2356 * erase item. The interaction between
2357 * colour attributes and the mono ones
2358 * is also very implementation
2361 * The 39 and 49 attributes are likely
2362 * to be unimplemented.
2365 for (i
= 0; i
< term
->esc_nargs
; i
++) {
2366 switch (def(term
->esc_args
[i
], 0)) {
2367 case 0: /* restore defaults */
2368 term
->curr_attr
= ATTR_DEFAULT
;
2370 case 1: /* enable bold */
2371 compatibility(VT100AVO
);
2372 term
->curr_attr
|= ATTR_BOLD
;
2374 case 21: /* (enable double underline) */
2375 compatibility(OTHER
);
2376 case 4: /* enable underline */
2377 compatibility(VT100AVO
);
2378 term
->curr_attr
|= ATTR_UNDER
;
2380 case 5: /* enable blink */
2381 compatibility(VT100AVO
);
2382 term
->curr_attr
|= ATTR_BLINK
;
2384 case 7: /* enable reverse video */
2385 term
->curr_attr
|= ATTR_REVERSE
;
2387 case 10: /* SCO acs off */
2388 compatibility(SCOANSI
);
2389 if (term
->cfg
.no_remote_charset
) break;
2390 term
->sco_acs
= 0; break;
2391 case 11: /* SCO acs on */
2392 compatibility(SCOANSI
);
2393 if (term
->cfg
.no_remote_charset
) break;
2394 term
->sco_acs
= 1; break;
2395 case 12: /* SCO acs on, |0x80 */
2396 compatibility(SCOANSI
);
2397 if (term
->cfg
.no_remote_charset
) break;
2398 term
->sco_acs
= 2; break;
2399 case 22: /* disable bold */
2400 compatibility2(OTHER
, VT220
);
2401 term
->curr_attr
&= ~ATTR_BOLD
;
2403 case 24: /* disable underline */
2404 compatibility2(OTHER
, VT220
);
2405 term
->curr_attr
&= ~ATTR_UNDER
;
2407 case 25: /* disable blink */
2408 compatibility2(OTHER
, VT220
);
2409 term
->curr_attr
&= ~ATTR_BLINK
;
2411 case 27: /* disable reverse video */
2412 compatibility2(OTHER
, VT220
);
2413 term
->curr_attr
&= ~ATTR_REVERSE
;
2424 term
->curr_attr
&= ~ATTR_FGMASK
;
2426 (term
->esc_args
[i
] - 30)<<ATTR_FGSHIFT
;
2436 /* xterm-style bright foreground */
2437 term
->curr_attr
&= ~ATTR_FGMASK
;
2439 ((term
->esc_args
[i
] - 90 + 16)
2442 case 39: /* default-foreground */
2443 term
->curr_attr
&= ~ATTR_FGMASK
;
2444 term
->curr_attr
|= ATTR_DEFFG
;
2455 term
->curr_attr
&= ~ATTR_BGMASK
;
2457 (term
->esc_args
[i
] - 40)<<ATTR_BGSHIFT
;
2467 /* xterm-style bright background */
2468 term
->curr_attr
&= ~ATTR_BGMASK
;
2470 ((term
->esc_args
[i
] - 100 + 16)
2473 case 49: /* default-background */
2474 term
->curr_attr
&= ~ATTR_BGMASK
;
2475 term
->curr_attr
|= ATTR_DEFBG
;
2480 term
->erase_char
= (' ' | ATTR_ASCII
|
2486 case 's': /* save cursor */
2487 save_cursor(term
, TRUE
);
2489 case 'u': /* restore cursor */
2490 save_cursor(term
, FALSE
);
2491 term
->seen_disp_event
= TRUE
;
2493 case 't': /* set page size - ie window height */
2495 * VT340/VT420 sequence DECSLPP, DEC only allows values
2496 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2497 * illegal values (eg first arg 1..9) for window changing
2500 if (term
->esc_nargs
<= 1
2501 && (term
->esc_args
[0] < 1 ||
2502 term
->esc_args
[0] >= 24)) {
2503 compatibility(VT340TEXT
);
2504 if (!term
->cfg
.no_remote_resize
)
2505 request_resize(term
->frontend
, term
->cols
,
2506 def(term
->esc_args
[0], 24));
2508 } else if (term
->esc_nargs
>= 1 &&
2509 term
->esc_args
[0] >= 1 &&
2510 term
->esc_args
[0] < 24) {
2511 compatibility(OTHER
);
2513 switch (term
->esc_args
[0]) {
2517 set_iconic(term
->frontend
, FALSE
);
2520 set_iconic(term
->frontend
, TRUE
);
2523 if (term
->esc_nargs
>= 3) {
2524 if (!term
->cfg
.no_remote_resize
)
2525 move_window(term
->frontend
,
2526 def(term
->esc_args
[1], 0),
2527 def(term
->esc_args
[2], 0));
2531 /* We should resize the window to a given
2532 * size in pixels here, but currently our
2533 * resizing code isn't healthy enough to
2538 set_zorder(term
->frontend
, TRUE
);
2541 /* move to bottom */
2542 set_zorder(term
->frontend
, FALSE
);
2545 refresh_window(term
->frontend
);
2548 if (term
->esc_nargs
>= 3) {
2549 if (!term
->cfg
.no_remote_resize
)
2550 request_resize(term
->frontend
,
2551 def(term
->esc_args
[2], term
->cfg
.width
),
2552 def(term
->esc_args
[1], term
->cfg
.height
));
2556 if (term
->esc_nargs
>= 2)
2557 set_zoomed(term
->frontend
,
2563 ldisc_send(term
->ldisc
,
2564 is_iconic(term
->frontend
) ?
2565 "\033[1t" : "\033[2t", 4, 0);
2569 get_window_pos(term
->frontend
, &x
, &y
);
2570 len
= sprintf(buf
, "\033[3;%d;%dt", x
, y
);
2571 ldisc_send(term
->ldisc
, buf
, len
, 0);
2576 get_window_pixels(term
->frontend
, &x
, &y
);
2577 len
= sprintf(buf
, "\033[4;%d;%dt", x
, y
);
2578 ldisc_send(term
->ldisc
, buf
, len
, 0);
2583 len
= sprintf(buf
, "\033[8;%d;%dt",
2584 term
->rows
, term
->cols
);
2585 ldisc_send(term
->ldisc
, buf
, len
, 0);
2590 * Hmmm. Strictly speaking we
2591 * should return `the size of the
2592 * screen in characters', but
2593 * that's not easy: (a) window
2594 * furniture being what it is it's
2595 * hard to compute, and (b) in
2596 * resize-font mode maximising the
2597 * window wouldn't change the
2598 * number of characters. *shrug*. I
2599 * think we'll ignore it for the
2600 * moment and see if anyone
2601 * complains, and then ask them
2602 * what they would like it to do.
2607 p
= get_window_title(term
->frontend
, TRUE
);
2609 ldisc_send(term
->ldisc
, "\033]L", 3, 0);
2610 ldisc_send(term
->ldisc
, p
, len
, 0);
2611 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
2616 p
= get_window_title(term
->frontend
,FALSE
);
2618 ldisc_send(term
->ldisc
, "\033]l", 3, 0);
2619 ldisc_send(term
->ldisc
, p
, len
, 0);
2620 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
2627 compatibility(SCOANSI
);
2628 scroll(term
, term
->marg_t
, term
->marg_b
,
2629 def(term
->esc_args
[0], 1), TRUE
);
2631 term
->wrapnext
= FALSE
;
2632 term
->seen_disp_event
= TRUE
;
2635 compatibility(SCOANSI
);
2636 scroll(term
, term
->marg_t
, term
->marg_b
,
2637 -def(term
->esc_args
[0], 1), TRUE
);
2639 term
->wrapnext
= FALSE
;
2640 term
->seen_disp_event
= TRUE
;
2642 case ANSI('|', '*'):
2643 /* VT420 sequence DECSNLS
2644 * Set number of lines on screen
2645 * VT420 uses VGA like hardware and can support any size in
2646 * reasonable range (24..49 AIUI) with no default specified.
2648 compatibility(VT420
);
2649 if (term
->esc_nargs
== 1 && term
->esc_args
[0] > 0) {
2650 if (!term
->cfg
.no_remote_resize
)
2651 request_resize(term
->frontend
, term
->cols
,
2652 def(term
->esc_args
[0],
2657 case ANSI('|', '$'):
2658 /* VT340/VT420 sequence DECSCPP
2659 * Set number of columns per page
2660 * Docs imply range is only 80 or 132, but I'll allow any.
2662 compatibility(VT340TEXT
);
2663 if (term
->esc_nargs
<= 1) {
2664 if (!term
->cfg
.no_remote_resize
)
2665 request_resize(term
->frontend
,
2666 def(term
->esc_args
[0],
2667 term
->cfg
.width
), term
->rows
);
2671 case 'X': /* write N spaces w/o moving cursor */
2672 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
2673 compatibility(ANSIMIN
);
2675 int n
= def(term
->esc_args
[0], 1);
2677 unsigned long *p
= term
->cpos
;
2678 if (n
> term
->cols
- term
->curs
.x
)
2679 n
= term
->cols
- term
->curs
.x
;
2680 cursplus
= term
->curs
;
2682 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2683 check_boundary(term
, term
->curs
.x
+n
, term
->curs
.y
);
2684 check_selection(term
, term
->curs
, cursplus
);
2686 *p
++ = term
->erase_char
;
2687 term
->seen_disp_event
= TRUE
;
2690 case 'x': /* report terminal characteristics */
2691 compatibility(VT100
);
2694 int i
= def(term
->esc_args
[0], 0);
2695 if (i
== 0 || i
== 1) {
2696 strcpy(buf
, "\033[2;1;1;112;112;1;0x");
2698 ldisc_send(term
->ldisc
, buf
, 20, 0);
2702 case 'Z': /* BackTab for xterm */
2703 compatibility(OTHER
);
2705 int i
= def(term
->esc_args
[0], 1);
2706 pos old_curs
= term
->curs
;
2708 for(;i
>0 && term
->curs
.x
>0; i
--) {
2711 } while (term
->curs
.x
>0 &&
2712 !term
->tabs
[term
->curs
.x
]);
2715 check_selection(term
, old_curs
, term
->curs
);
2718 case ANSI('L', '='):
2719 compatibility(OTHER
);
2720 term
->use_bce
= (term
->esc_args
[0] <= 0);
2721 term
->erase_char
= ERASE_CHAR
;
2723 term
->erase_char
= (' ' | ATTR_ASCII
|
2725 (ATTR_FGMASK
| ATTR_BGMASK
)));
2727 case ANSI('E', '='):
2728 compatibility(OTHER
);
2729 term
->blink_is_real
= (term
->esc_args
[0] >= 1);
2731 case ANSI('p', '"'):
2733 * Allow the host to make this emulator a
2734 * 'perfect' VT102. This first appeared in
2735 * the VT220, but we do need to get back to
2736 * PuTTY mode so I won't check it.
2738 * The arg in 40..42,50 are a PuTTY extension.
2739 * The 2nd arg, 8bit vs 7bit is not checked.
2741 * Setting VT102 mode should also change
2742 * the Fkeys to generate PF* codes as a
2743 * real VT102 has no Fkeys. The VT220 does
2744 * this, F11..F13 become ESC,BS,LF other
2745 * Fkeys send nothing.
2747 * Note ESC c will NOT change this!
2750 switch (term
->esc_args
[0]) {
2752 term
->compatibility_level
&= ~TM_VTXXX
;
2753 term
->compatibility_level
|= TM_VT102
;
2756 term
->compatibility_level
&= ~TM_VTXXX
;
2757 term
->compatibility_level
|= TM_VT220
;
2761 if (term
->esc_args
[0] > 60 &&
2762 term
->esc_args
[0] < 70)
2763 term
->compatibility_level
|= TM_VTXXX
;
2767 term
->compatibility_level
&= TM_VTXXX
;
2770 term
->compatibility_level
= TM_PUTTY
;
2773 term
->compatibility_level
= TM_SCOANSI
;
2777 term
->compatibility_level
= TM_PUTTY
;
2783 /* Change the response to CSI c */
2784 if (term
->esc_args
[0] == 50) {
2787 strcpy(term
->id_string
, "\033[?");
2788 for (i
= 1; i
< term
->esc_nargs
; i
++) {
2790 strcat(term
->id_string
, ";");
2791 sprintf(lbuf
, "%d", term
->esc_args
[i
]);
2792 strcat(term
->id_string
, lbuf
);
2794 strcat(term
->id_string
, "c");
2797 /* Is this a good idea ?
2798 * Well we should do a soft reset at this point ...
2800 if (!has_compat(VT420
) && has_compat(VT100
)) {
2801 if (!term
->cfg
.no_remote_resize
) {
2802 if (term
->reset_132
)
2803 request_resize(132, 24);
2805 request_resize(80, 24);
2813 term
->osc_w
= FALSE
;
2815 case 'P': /* Linux palette sequence */
2816 term
->termstate
= SEEN_OSC_P
;
2817 term
->osc_strlen
= 0;
2819 case 'R': /* Linux palette reset */
2820 palette_reset(term
->frontend
);
2821 term_invalidate(term
);
2822 term
->termstate
= TOPLEVEL
;
2824 case 'W': /* word-set */
2825 term
->termstate
= SEEN_OSC_W
;
2838 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
2842 * Grotty hack to support xterm and DECterm title
2843 * sequences concurrently.
2845 if (term
->esc_args
[0] == 2) {
2846 term
->esc_args
[0] = 1;
2849 /* else fall through */
2851 term
->termstate
= OSC_STRING
;
2852 term
->osc_strlen
= 0;
2857 * This OSC stuff is EVIL. It takes just one character to get into
2858 * sysline mode and it's not initially obvious how to get out.
2859 * So I've added CR and LF as string aborts.
2860 * This shouldn't effect compatibility as I believe embedded
2861 * control characters are supposed to be interpreted (maybe?)
2862 * and they don't display anything useful anyway.
2866 if (c
== '\012' || c
== '\015') {
2867 term
->termstate
= TOPLEVEL
;
2868 } else if (c
== 0234 || c
== '\007') {
2870 * These characters terminate the string; ST and BEL
2871 * terminate the sequence and trigger instant
2872 * processing of it, whereas ESC goes back to SEEN_ESC
2873 * mode unless it is followed by \, in which case it is
2874 * synonymous with ST in the first place.
2877 term
->termstate
= TOPLEVEL
;
2878 } else if (c
== '\033')
2879 term
->termstate
= OSC_MAYBE_ST
;
2880 else if (term
->osc_strlen
< OSC_STR_MAX
)
2881 term
->osc_string
[term
->osc_strlen
++] = c
;
2885 int max
= (term
->osc_strlen
== 0 ?
21 : 16);
2887 if (c
>= '0' && c
<= '9')
2889 else if (c
>= 'A' && c
<= 'A' + max
- 10)
2891 else if (c
>= 'a' && c
<= 'a' + max
- 10)
2894 term
->termstate
= TOPLEVEL
;
2897 term
->osc_string
[term
->osc_strlen
++] = val
;
2898 if (term
->osc_strlen
>= 7) {
2899 palette_set(term
->frontend
, term
->osc_string
[0],
2900 term
->osc_string
[1] * 16 + term
->osc_string
[2],
2901 term
->osc_string
[3] * 16 + term
->osc_string
[4],
2902 term
->osc_string
[5] * 16 + term
->osc_string
[6]);
2903 term_invalidate(term
);
2904 term
->termstate
= TOPLEVEL
;
2920 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
2923 term
->termstate
= OSC_STRING
;
2924 term
->osc_strlen
= 0;
2928 term
->termstate
= TOPLEVEL
;
2929 term
->seen_disp_event
= TRUE
;
2932 move(term
, term
->curs
.x
, term
->curs
.y
- 1, 1);
2935 move(term
, term
->curs
.x
, term
->curs
.y
+ 1, 1);
2938 move(term
, term
->curs
.x
+ 1, term
->curs
.y
, 1);
2941 move(term
, term
->curs
.x
- 1, term
->curs
.y
, 1);
2944 * From the VT100 Manual
2945 * NOTE: The special graphics characters in the VT100
2946 * are different from those in the VT52
2948 * From VT102 manual:
2949 * 137 _ Blank - Same
2950 * 140 ` Reserved - Humm.
2951 * 141 a Solid rectangle - Similar
2952 * 142 b 1/ - Top half of fraction for the
2953 * 143 c 3/ - subscript numbers below.
2956 * 146 f Degrees - Same
2957 * 147 g Plus or minus - Same
2959 * 151 i Ellipsis (dots)
2962 * 154 l Bar at scan 0
2963 * 155 m Bar at scan 1
2964 * 156 n Bar at scan 2
2965 * 157 o Bar at scan 3 - Similar
2966 * 160 p Bar at scan 4 - Similar
2967 * 161 q Bar at scan 5 - Similar
2968 * 162 r Bar at scan 6 - Same
2969 * 163 s Bar at scan 7 - Similar
2984 term
->cset_attr
[term
->cset
= 0] = ATTR_LINEDRW
;
2987 term
->cset_attr
[term
->cset
= 0] = ATTR_ASCII
;
2990 move(term
, 0, 0, 0);
2993 if (term
->curs
.y
== 0)
2994 scroll(term
, 0, term
->rows
- 1, -1, TRUE
);
2995 else if (term
->curs
.y
> 0)
2998 term
->wrapnext
= FALSE
;
3001 erase_lots(term
, FALSE
, FALSE
, TRUE
);
3005 erase_lots(term
, TRUE
, FALSE
, TRUE
);
3009 /* XXX Print cursor line */
3012 /* XXX Start controller mode */
3015 /* XXX Stop controller mode */
3019 term
->termstate
= VT52_Y1
;
3023 ldisc_send(term
->ldisc
, "\033/Z", 3, 0);
3026 term
->app_keypad_keys
= TRUE
;
3029 term
->app_keypad_keys
= FALSE
;
3032 /* XXX This should switch to VT100 mode not current or default
3033 * VT mode. But this will only have effect in a VT220+
3036 term
->vt52_mode
= FALSE
;
3037 term
->blink_is_real
= term
->cfg
.blinktext
;
3041 /* XXX Enter auto print mode */
3044 /* XXX Exit auto print mode */
3047 /* XXX Print screen */
3053 /* compatibility(ATARI) */
3054 move(term
, 0, 0, 0);
3055 erase_lots(term
, FALSE
, FALSE
, TRUE
);
3059 /* compatibility(ATARI) */
3060 if (term
->curs
.y
<= term
->marg_b
)
3061 scroll(term
, term
->curs
.y
, term
->marg_b
, -1, FALSE
);
3064 /* compatibility(ATARI) */
3065 if (term
->curs
.y
<= term
->marg_b
)
3066 scroll(term
, term
->curs
.y
, term
->marg_b
, 1, TRUE
);
3069 /* compatibility(ATARI) */
3070 term
->termstate
= VT52_FG
;
3073 /* compatibility(ATARI) */
3074 term
->termstate
= VT52_BG
;
3077 /* compatibility(ATARI) */
3078 erase_lots(term
, FALSE
, TRUE
, FALSE
);
3082 /* compatibility(ATARI) */
3083 term
->cursor_on
= TRUE
;
3086 /* compatibility(ATARI) */
3087 term
->cursor_on
= FALSE
;
3089 /* case 'j': Save cursor position - broken on ST */
3090 /* case 'k': Restore cursor position */
3092 /* compatibility(ATARI) */
3093 erase_lots(term
, TRUE
, TRUE
, TRUE
);
3095 term
->wrapnext
= FALSE
;
3099 /* compatibility(ATARI) */
3100 erase_lots(term
, TRUE
, TRUE
, FALSE
);
3103 /* compatibility(ATARI) */
3104 term
->curr_attr
|= ATTR_REVERSE
;
3107 /* compatibility(ATARI) */
3108 term
->curr_attr
&= ~ATTR_REVERSE
;
3110 case 'v': /* wrap Autowrap on - Wyse style */
3111 /* compatibility(ATARI) */
3114 case 'w': /* Autowrap off */
3115 /* compatibility(ATARI) */
3120 /* compatibility(OTHER) */
3121 term
->vt52_bold
= FALSE
;
3122 term
->curr_attr
= ATTR_DEFAULT
;
3124 term
->erase_char
= (' ' | ATTR_ASCII
|
3126 (ATTR_FGMASK
| ATTR_BGMASK
)));
3129 /* compatibility(VI50) */
3130 term
->curr_attr
|= ATTR_UNDER
;
3133 /* compatibility(VI50) */
3134 term
->curr_attr
&= ~ATTR_UNDER
;
3137 /* compatibility(VI50) */
3138 term
->vt52_bold
= TRUE
;
3139 term
->curr_attr
|= ATTR_BOLD
;
3142 /* compatibility(VI50) */
3143 term
->vt52_bold
= FALSE
;
3144 term
->curr_attr
&= ~ATTR_BOLD
;
3150 term
->termstate
= VT52_Y2
;
3151 move(term
, term
->curs
.x
, c
- ' ', 0);
3154 term
->termstate
= TOPLEVEL
;
3155 move(term
, c
- ' ', term
->curs
.y
, 0);
3160 term
->termstate
= TOPLEVEL
;
3161 term
->curr_attr
&= ~ATTR_FGMASK
;
3162 term
->curr_attr
&= ~ATTR_BOLD
;
3163 term
->curr_attr
|= (c
& 0x7) << ATTR_FGSHIFT
;
3164 if ((c
& 0x8) || term
->vt52_bold
)
3165 term
->curr_attr
|= ATTR_BOLD
;
3168 term
->erase_char
= (' ' | ATTR_ASCII
|
3170 (ATTR_FGMASK
| ATTR_BGMASK
)));
3173 term
->termstate
= TOPLEVEL
;
3174 term
->curr_attr
&= ~ATTR_BGMASK
;
3175 term
->curr_attr
&= ~ATTR_BLINK
;
3176 term
->curr_attr
|= (c
& 0x7) << ATTR_BGSHIFT
;
3178 /* Note: bold background */
3180 term
->curr_attr
|= ATTR_BLINK
;
3183 term
->erase_char
= (' ' | ATTR_ASCII
|
3185 (ATTR_FGMASK
| ATTR_BGMASK
)));
3188 default: break; /* placate gcc warning about enum use */
3190 if (term
->selstate
!= NO_SELECTION
) {
3191 pos cursplus
= term
->curs
;
3193 check_selection(term
, term
->curs
, cursplus
);
3197 term_print_flush(term
);
3202 * Compare two lines to determine whether they are sufficiently
3203 * alike to scroll-optimise one to the other. Return the degree of
3206 static int linecmp(Terminal
*term
, unsigned long *a
, unsigned long *b
)
3210 for (i
= n
= 0; i
< term
->cols
; i
++)
3211 n
+= (*a
++ == *b
++);
3217 * Given a context, update the window. Out of paranoia, we don't
3218 * allow WM_PAINT responses to do scrolling optimisations.
3220 static void do_paint(Terminal
*term
, Context ctx
, int may_optimise
)
3222 int i
, j
, our_curs_y
, our_curs_x
;
3223 unsigned long rv
, cursor
;
3226 long cursor_background
= ERASE_CHAR
;
3227 unsigned long ticks
;
3228 #ifdef OPTIMISE_SCROLL
3229 struct scrollregion
*sr
;
3230 #endif /* OPTIMISE_SCROLL */
3233 * Check the visual bell state.
3235 if (term
->in_vbell
) {
3236 ticks
= GETTICKCOUNT();
3237 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
3238 term
->in_vbell
= FALSE
;
3241 rv
= (!term
->rvideo
^ !term
->in_vbell ? ATTR_REVERSE
: 0);
3244 * screen array, disptop, scrtop,
3246 * cfg.blinkpc, blink_is_real, tblinker,
3247 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
3250 /* Has the cursor position or type changed ? */
3251 if (term
->cursor_on
) {
3252 if (term
->has_focus
) {
3253 if (term
->blinker
|| !term
->cfg
.blink_cur
)
3254 cursor
= TATTR_ACTCURS
;
3258 cursor
= TATTR_PASCURS
;
3260 cursor
|= TATTR_RIGHTCURS
;
3263 our_curs_y
= term
->curs
.y
- term
->disptop
;
3266 * Adjust the cursor position in the case where it's
3267 * resting on the right-hand half of a CJK wide character.
3268 * xterm's behaviour here, which seems adequate to me, is
3269 * to display the cursor covering the _whole_ character,
3270 * exactly as if it were one space to the left.
3272 unsigned long *ldata
= lineptr(term
->curs
.y
);
3273 our_curs_x
= term
->curs
.x
;
3274 if (our_curs_x
> 0 &&
3275 (ldata
[our_curs_x
] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
3279 if (term
->dispcurs
&& (term
->curstype
!= cursor
||
3281 term
->disptext
+ our_curs_y
* (term
->cols
+ 1) +
3283 if (term
->dispcurs
> term
->disptext
&&
3284 (*term
->dispcurs
& (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
3285 term
->dispcurs
[-1] |= ATTR_INVALID
;
3286 if ( (term
->dispcurs
[1] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
3287 term
->dispcurs
[1] |= ATTR_INVALID
;
3288 *term
->dispcurs
|= ATTR_INVALID
;
3291 term
->dispcurs
= NULL
;
3293 #ifdef OPTIMISE_SCROLL
3295 sr
= term
->scrollhead
;
3297 struct scrollregion
*next
= sr
->next
;
3298 do_scroll(ctx
, sr
->topline
, sr
->botline
, sr
->lines
);
3302 term
->scrollhead
= term
->scrolltail
= NULL
;
3303 #endif /* OPTIMISE_SCROLL */
3305 /* The normal screen data */
3306 for (i
= 0; i
< term
->rows
; i
++) {
3307 unsigned long *ldata
;
3309 int idx
, dirty_line
, dirty_run
, selected
;
3310 unsigned long attr
= 0;
3311 int updated_line
= 0;
3314 int last_run_dirty
= 0;
3316 scrpos
.y
= i
+ term
->disptop
;
3317 ldata
= lineptr(scrpos
.y
);
3318 lattr
= (ldata
[term
->cols
] & LATTR_MODE
);
3320 idx
= i
* (term
->cols
+ 1);
3321 dirty_run
= dirty_line
= (ldata
[term
->cols
] !=
3322 term
->disptext
[idx
+ term
->cols
]);
3323 term
->disptext
[idx
+ term
->cols
] = ldata
[term
->cols
];
3325 for (j
= 0; j
< term
->cols
; j
++, idx
++) {
3326 unsigned long tattr
, tchar
;
3327 unsigned long *d
= ldata
+ j
;
3331 tchar
= (*d
& (CHAR_MASK
| CSET_MASK
));
3332 tattr
= (*d
& (ATTR_MASK
^ CSET_MASK
));
3333 switch (tchar
& CSET_MASK
) {
3335 tchar
= term
->ucsdata
->unitab_line
[tchar
& 0xFF];
3338 tchar
= term
->ucsdata
->unitab_xterm
[tchar
& 0xFF];
3341 tchar
= term
->ucsdata
->unitab_scoacs
[tchar
&0xFF];
3344 tattr
|= (tchar
& CSET_MASK
);
3346 if ((d
[1] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
3349 /* Video reversing things */
3350 if (term
->selstate
== DRAGGING
|| term
->selstate
== SELECTED
) {
3351 if (term
->seltype
== LEXICOGRAPHIC
)
3352 selected
= (posle(term
->selstart
, scrpos
) &&
3353 poslt(scrpos
, term
->selend
));
3355 selected
= (posPle(term
->selstart
, scrpos
) &&
3356 posPlt(scrpos
, term
->selend
));
3360 ^ (selected ? ATTR_REVERSE
: 0));
3362 /* 'Real' blinking ? */
3363 if (term
->blink_is_real
&& (tattr
& ATTR_BLINK
)) {
3364 if (term
->has_focus
&& term
->tblinker
) {
3365 tchar
= term
->ucsdata
->unitab_line
[(unsigned char)' '];
3367 tattr
&= ~ATTR_BLINK
;
3371 * Check the font we'll _probably_ be using to see if
3372 * the character is wide when we don't want it to be.
3374 if ((tchar
| tattr
) != (term
->disptext
[idx
]& ~ATTR_NARROW
)) {
3375 if ((tattr
& ATTR_WIDE
) == 0 &&
3376 char_width(ctx
, (tchar
| tattr
) & 0xFFFF) == 2)
3377 tattr
|= ATTR_NARROW
;
3378 } else if (term
->disptext
[idx
]&ATTR_NARROW
)
3379 tattr
|= ATTR_NARROW
;
3381 /* Cursor here ? Save the 'background' */
3382 if (i
== our_curs_y
&& j
== our_curs_x
) {
3383 cursor_background
= tattr
| tchar
;
3384 term
->dispcurs
= term
->disptext
+ idx
;
3387 if ((term
->disptext
[idx
] ^ tattr
) & ATTR_WIDE
)
3390 break_run
= (((tattr
^ attr
) & term
->attr_mask
) ||
3391 j
- start
>= sizeof(ch
));
3393 /* Special hack for VT100 Linedraw glyphs */
3394 if ((attr
& CSET_MASK
) == 0x2300 && tchar
>= 0xBA
3395 && tchar
<= 0xBD) break_run
= TRUE
;
3397 if (!term
->ucsdata
->dbcs_screenfont
&& !dirty_line
) {
3398 if ((tchar
| tattr
) == term
->disptext
[idx
])
3400 else if (!dirty_run
&& ccount
== 1)
3405 if ((dirty_run
|| last_run_dirty
) && ccount
> 0) {
3406 do_text(ctx
, start
, i
, ch
, ccount
, attr
, lattr
);
3412 if (term
->ucsdata
->dbcs_screenfont
)
3413 last_run_dirty
= dirty_run
;
3414 dirty_run
= dirty_line
;
3417 if ((tchar
| tattr
) != term
->disptext
[idx
])
3419 ch
[ccount
++] = (char) tchar
;
3420 term
->disptext
[idx
] = tchar
| tattr
;
3422 /* If it's a wide char step along to the next one. */
3423 if (tattr
& ATTR_WIDE
) {
3424 if (++j
< term
->cols
) {
3428 * By construction above, the cursor should not
3429 * be on the right-hand half of this character.
3432 assert(!(i
== our_curs_y
&& j
== our_curs_x
));
3433 if (term
->disptext
[idx
] != *d
)
3435 term
->disptext
[idx
] = *d
;
3439 if (dirty_run
&& ccount
> 0) {
3440 do_text(ctx
, start
, i
, ch
, ccount
, attr
, lattr
);
3444 /* Cursor on this line ? (and changed) */
3445 if (i
== our_curs_y
&& (term
->curstype
!= cursor
|| updated_line
)) {
3446 ch
[0] = (char) (cursor_background
& CHAR_MASK
);
3447 attr
= (cursor_background
& ATTR_MASK
) | cursor
;
3448 do_cursor(ctx
, our_curs_x
, i
, ch
, 1, attr
, lattr
);
3449 term
->curstype
= cursor
;
3455 * Flick the switch that says if blinking things should be shown or hidden.
3458 void term_blink(Terminal
*term
, int flg
)
3460 long now
, blink_diff
;
3462 now
= GETTICKCOUNT();
3463 blink_diff
= now
- term
->last_tblink
;
3465 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3466 if (blink_diff
< 0 || blink_diff
> (TICKSPERSEC
* 9 / 20)) {
3467 term
->last_tblink
= now
;
3468 term
->tblinker
= !term
->tblinker
;
3473 term
->last_blink
= now
;
3477 blink_diff
= now
- term
->last_blink
;
3479 /* Make sure the cursor blinks no faster than system blink rate */
3480 if (blink_diff
>= 0 && blink_diff
< (long) CURSORBLINK
)
3483 term
->last_blink
= now
;
3484 term
->blinker
= !term
->blinker
;
3488 * Invalidate the whole screen so it will be repainted in full.
3490 void term_invalidate(Terminal
*term
)
3494 for (i
= 0; i
< term
->rows
* (term
->cols
+ 1); i
++)
3495 term
->disptext
[i
] = ATTR_INVALID
;
3499 * Paint the window in response to a WM_PAINT message.
3501 void term_paint(Terminal
*term
, Context ctx
,
3502 int left
, int top
, int right
, int bottom
, int immediately
)
3505 if (left
< 0) left
= 0;
3506 if (top
< 0) top
= 0;
3507 if (right
>= term
->cols
) right
= term
->cols
-1;
3508 if (bottom
>= term
->rows
) bottom
= term
->rows
-1;
3510 for (i
= top
; i
<= bottom
&& i
< term
->rows
; i
++) {
3511 if ((term
->disptext
[i
* (term
->cols
+ 1) + term
->cols
] &
3512 LATTR_MODE
) == LATTR_NORM
)
3513 for (j
= left
; j
<= right
&& j
< term
->cols
; j
++)
3514 term
->disptext
[i
* (term
->cols
+ 1) + j
] = ATTR_INVALID
;
3516 for (j
= left
/ 2; j
<= right
/ 2 + 1 && j
< term
->cols
; j
++)
3517 term
->disptext
[i
* (term
->cols
+ 1) + j
] = ATTR_INVALID
;
3520 /* This should happen soon enough, also for some reason it sometimes
3521 * fails to actually do anything when re-sizing ... painting the wrong
3525 do_paint (term
, ctx
, FALSE
);
3529 * Attempt to scroll the scrollback. The second parameter gives the
3530 * position we want to scroll to; the first is +1 to denote that
3531 * this position is relative to the beginning of the scrollback, -1
3532 * to denote it is relative to the end, and 0 to denote that it is
3533 * relative to the current position.
3535 void term_scroll(Terminal
*term
, int rel
, int where
)
3537 int sbtop
= -sblines(term
);
3538 #ifdef OPTIMISE_SCROLL
3539 int olddisptop
= term
->disptop
;
3541 #endif /* OPTIMISE_SCROLL */
3543 term
->disptop
= (rel
< 0 ?
0 : rel
> 0 ? sbtop
: term
->disptop
) + where
;
3544 if (term
->disptop
< sbtop
)
3545 term
->disptop
= sbtop
;
3546 if (term
->disptop
> 0)
3549 #ifdef OPTIMISE_SCROLL
3550 shift
= (term
->disptop
- olddisptop
);
3551 if (shift
< term
->rows
&& shift
> -term
->rows
)
3552 scroll_display(term
, 0, term
->rows
- 1, shift
);
3553 #endif /* OPTIMISE_SCROLL */
3557 static void clipme(Terminal
*term
, pos top
, pos bottom
, int rect
)
3560 wchar_t *wbptr
; /* where next char goes within workbuf */
3562 int wblen
= 0; /* workbuf len */
3563 int buflen
; /* amount of memory allocated to workbuf */
3565 buflen
= 5120; /* Default size */
3566 workbuf
= snewn(buflen
, wchar_t);
3567 wbptr
= workbuf
; /* start filling here */
3568 old_top_x
= top
.x
; /* needed for rect==1 */
3570 while (poslt(top
, bottom
)) {
3572 unsigned long *ldata
= lineptr(top
.y
);
3576 * nlpos will point at the maximum position on this line we
3577 * should copy up to. So we start it at the end of the
3581 nlpos
.x
= term
->cols
;
3584 * ... move it backwards if there's unused space at the end
3585 * of the line (and also set `nl' if this is the case,
3586 * because in normal selection mode this means we need a
3587 * newline at the end)...
3589 if (!(ldata
[term
->cols
] & LATTR_WRAPPED
)) {
3590 while (((ldata
[nlpos
.x
- 1] & 0xFF) == 0x20 ||
3591 (DIRECT_CHAR(ldata
[nlpos
.x
- 1]) &&
3592 (ldata
[nlpos
.x
- 1] & CHAR_MASK
) == 0x20))
3593 && poslt(top
, nlpos
))
3595 if (poslt(nlpos
, bottom
))
3597 } else if (ldata
[term
->cols
] & LATTR_WRAPPED2
) {
3598 /* Ignore the last char on the line in a WRAPPED2 line. */
3603 * ... and then clip it to the terminal x coordinate if
3604 * we're doing rectangular selection. (In this case we
3605 * still did the above, so that copying e.g. the right-hand
3606 * column from a table doesn't fill with spaces on the
3610 if (nlpos
.x
> bottom
.x
)
3612 nl
= (top
.y
< bottom
.y
);
3615 while (poslt(top
, bottom
) && poslt(top
, nlpos
)) {
3618 sprintf(cbuf
, "<U+%04x>", (ldata
[top
.x
] & 0xFFFF));
3620 wchar_t cbuf
[16], *p
;
3621 int uc
= (ldata
[top
.x
] & 0xFFFF);
3624 if (uc
== UCSWIDE
) {
3629 switch (uc
& CSET_MASK
) {
3631 if (!term
->cfg
.rawcnp
) {
3632 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
3636 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
3639 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
3642 switch (uc
& CSET_MASK
) {
3644 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
3647 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
3651 set
= (uc
& CSET_MASK
);
3652 c
= (uc
& CHAR_MASK
);
3656 if (DIRECT_FONT(uc
)) {
3657 if (c
>= ' ' && c
!= 0x7F) {
3661 if (is_dbcs_leadbyte(term
->ucsdata
->font_codepage
, (BYTE
) c
)) {
3663 buf
[1] = (char) (0xFF & ldata
[top
.x
+ 1]);
3664 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 2, wbuf
, 4);
3668 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 1, wbuf
, 4);
3672 memcpy(cbuf
, wbuf
, rv
* sizeof(wchar_t));
3679 for (p
= cbuf
; *p
; p
++) {
3680 /* Enough overhead for trailing NL and nul */
3681 if (wblen
>= buflen
- 16) {
3683 workbuf
= sresize(workbuf
, buflen
, wchar_t);
3684 wbptr
= workbuf
+ wblen
;
3693 for (i
= 0; i
< sel_nl_sz
; i
++) {
3695 *wbptr
++ = sel_nl
[i
];
3699 top
.x
= rect ? old_top_x
: 0;
3701 #if SELECTION_NUL_TERMINATED
3705 write_clip(term
->frontend
, workbuf
, wblen
, FALSE
); /* transfer to clipbd */
3706 if (buflen
> 0) /* indicates we allocated this buffer */
3710 void term_copyall(Terminal
*term
)
3713 top
.y
= -sblines(term
);
3715 clipme(term
, top
, term
->curs
, 0);
3719 * The wordness array is mainly for deciding the disposition of the
3720 * US-ASCII characters.
3722 static int wordtype(Terminal
*term
, int uc
)
3725 int start
, end
, ctype
;
3727 static const struct ucsword ucs_words
[] = {
3733 0x037e, 0x037e, 1}, /* Greek question mark */
3735 0x0387, 0x0387, 1}, /* Greek ano teleia */
3737 0x055a, 0x055f, 1}, /* Armenian punctuation */
3739 0x0589, 0x0589, 1}, /* Armenian full stop */
3741 0x0700, 0x070d, 1}, /* Syriac punctuation */
3743 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3745 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3747 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3749 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3751 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3753 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3755 0x2000, 0x200a, 0}, /* Various spaces */
3757 0x2070, 0x207f, 2}, /* superscript */
3759 0x2080, 0x208f, 2}, /* subscript */
3761 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3763 0x3000, 0x3000, 0}, /* ideographic space */
3765 0x3001, 0x3020, 1}, /* ideographic punctuation */
3767 0x303f, 0x309f, 3}, /* Hiragana */
3769 0x30a0, 0x30ff, 3}, /* Katakana */
3771 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3773 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3775 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3777 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3779 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3781 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3783 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3785 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3787 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3791 const struct ucsword
*wptr
;
3793 uc
&= (CSET_MASK
| CHAR_MASK
);
3795 switch (uc
& CSET_MASK
) {
3797 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
3800 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
3803 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
3806 switch (uc
& CSET_MASK
) {
3808 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
3811 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
3815 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3816 * fail as there's such a thing as a double width space. :-(
3818 if (term
->ucsdata
->dbcs_screenfont
&&
3819 term
->ucsdata
->font_codepage
== term
->ucsdata
->line_codepage
)
3823 return term
->wordness
[uc
];
3825 for (wptr
= ucs_words
; wptr
->start
; wptr
++) {
3826 if (uc
>= wptr
->start
&& uc
<= wptr
->end
)
3834 * Spread the selection outwards according to the selection mode.
3836 static pos
sel_spread_half(Terminal
*term
, pos p
, int dir
)
3838 unsigned long *ldata
;
3840 int topy
= -sblines(term
);
3842 ldata
= lineptr(p
.y
);
3844 switch (term
->selmode
) {
3847 * In this mode, every character is a separate unit, except
3848 * for runs of spaces at the end of a non-wrapping line.
3850 if (!(ldata
[term
->cols
] & LATTR_WRAPPED
)) {
3851 unsigned long *q
= ldata
+ term
->cols
;
3852 while (q
> ldata
&& (q
[-1] & CHAR_MASK
) == 0x20)
3854 if (q
== ldata
+ term
->cols
)
3856 if (p
.x
>= q
- ldata
)
3857 p
.x
= (dir
== -1 ? q
- ldata
: term
->cols
- 1);
3862 * In this mode, the units are maximal runs of characters
3863 * whose `wordness' has the same value.
3865 wvalue
= wordtype(term
, UCSGET(ldata
, p
.x
));
3868 int maxcols
= (ldata
[term
->cols
] & LATTR_WRAPPED2 ?
3869 term
->cols
-1 : term
->cols
);
3870 if (p
.x
< maxcols
-1) {
3871 if (wordtype(term
, UCSGET(ldata
, p
.x
+ 1)) == wvalue
)
3876 if (ldata
[term
->cols
] & LATTR_WRAPPED
) {
3877 unsigned long *ldata2
;
3878 ldata2
= lineptr(p
.y
+1);
3879 if (wordtype(term
, UCSGET(ldata2
, 0)) == wvalue
) {
3892 if (wordtype(term
, UCSGET(ldata
, p
.x
- 1)) == wvalue
)
3897 unsigned long *ldata2
;
3901 ldata2
= lineptr(p
.y
-1);
3902 maxcols
= (ldata2
[term
->cols
] & LATTR_WRAPPED2 ?
3903 term
->cols
-1 : term
->cols
);
3904 if (ldata2
[term
->cols
] & LATTR_WRAPPED
) {
3905 if (wordtype(term
, UCSGET(ldata2
, maxcols
-1))
3920 * In this mode, every line is a unit.
3922 p
.x
= (dir
== -1 ?
0 : term
->cols
- 1);
3928 static void sel_spread(Terminal
*term
)
3930 if (term
->seltype
== LEXICOGRAPHIC
) {
3931 term
->selstart
= sel_spread_half(term
, term
->selstart
, -1);
3932 decpos(term
->selend
);
3933 term
->selend
= sel_spread_half(term
, term
->selend
, +1);
3934 incpos(term
->selend
);
3938 void term_do_paste(Terminal
*term
)
3943 get_clip(term
->frontend
, &data
, &len
);
3944 if (data
&& len
> 0) {
3947 term_seen_key_event(term
); /* pasted data counts */
3949 if (term
->paste_buffer
)
3950 sfree(term
->paste_buffer
);
3951 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
3952 term
->paste_buffer
= snewn(len
, wchar_t);
3955 while (p
< data
+ len
) {
3956 while (p
< data
+ len
&&
3957 !(p
<= data
+ len
- sel_nl_sz
&&
3958 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
3963 for (i
= 0; i
< p
- q
; i
++) {
3964 term
->paste_buffer
[term
->paste_len
++] = q
[i
];
3968 if (p
<= data
+ len
- sel_nl_sz
&&
3969 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
3970 term
->paste_buffer
[term
->paste_len
++] = '\015';
3976 /* Assume a small paste will be OK in one go. */
3977 if (term
->paste_len
< 256) {
3979 luni_send(term
->ldisc
, term
->paste_buffer
, term
->paste_len
, 0);
3980 if (term
->paste_buffer
)
3981 sfree(term
->paste_buffer
);
3982 term
->paste_buffer
= 0;
3983 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
3986 get_clip(term
->frontend
, NULL
, NULL
);
3989 void term_mouse(Terminal
*term
, Mouse_Button braw
, Mouse_Button bcooked
,
3990 Mouse_Action a
, int x
, int y
, int shift
, int ctrl
, int alt
)
3993 unsigned long *ldata
;
3994 int raw_mouse
= (term
->xterm_mouse
&&
3995 !term
->cfg
.no_mouse_rep
&&
3996 !(term
->cfg
.mouse_override
&& shift
));
3997 int default_seltype
;
4001 if (a
== MA_DRAG
&& !raw_mouse
)
4002 term_scroll(term
, 0, -1);
4004 if (y
>= term
->rows
) {
4006 if (a
== MA_DRAG
&& !raw_mouse
)
4007 term_scroll(term
, 0, +1);
4016 if (x
>= term
->cols
)
4019 selpoint
.y
= y
+ term
->disptop
;
4021 ldata
= lineptr(selpoint
.y
);
4022 if ((ldata
[term
->cols
] & LATTR_MODE
) != LATTR_NORM
)
4026 int encstate
= 0, r
, c
;
4033 encstate
= 0x20; /* left button down */
4044 case MBT_WHEEL_DOWN
:
4047 default: break; /* placate gcc warning about enum use */
4051 if (term
->xterm_mouse
== 1)
4057 term
->mouse_is_down
= 0;
4060 if (term
->mouse_is_down
== braw
)
4062 term
->mouse_is_down
= braw
;
4064 default: break; /* placate gcc warning about enum use */
4073 sprintf(abuf
, "\033[M%c%c%c", encstate
, c
, r
);
4074 ldisc_send(term
->ldisc
, abuf
, 6, 0);
4080 * Set the selection type (rectangular or normal) at the start
4081 * of a selection attempt, from the state of Alt.
4083 if (!alt
^ !term
->cfg
.rect_select
)
4084 default_seltype
= RECTANGULAR
;
4086 default_seltype
= LEXICOGRAPHIC
;
4088 if (term
->selstate
== NO_SELECTION
) {
4089 term
->seltype
= default_seltype
;
4092 if (bcooked
== MBT_SELECT
&& a
== MA_CLICK
) {
4094 term
->selstate
= ABOUT_TO
;
4095 term
->seltype
= default_seltype
;
4096 term
->selanchor
= selpoint
;
4097 term
->selmode
= SM_CHAR
;
4098 } else if (bcooked
== MBT_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
4100 term
->selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
4101 term
->selstate
= DRAGGING
;
4102 term
->selstart
= term
->selanchor
= selpoint
;
4103 term
->selend
= term
->selstart
;
4104 incpos(term
->selend
);
4106 } else if ((bcooked
== MBT_SELECT
&& a
== MA_DRAG
) ||
4107 (bcooked
== MBT_EXTEND
&& a
!= MA_RELEASE
)) {
4108 if (term
->selstate
== ABOUT_TO
&& poseq(term
->selanchor
, selpoint
))
4110 if (bcooked
== MBT_EXTEND
&& a
!= MA_DRAG
&&
4111 term
->selstate
== SELECTED
) {
4112 if (term
->seltype
== LEXICOGRAPHIC
) {
4114 * For normal selection, we extend by moving
4115 * whichever end of the current selection is closer
4118 if (posdiff(selpoint
, term
->selstart
) <
4119 posdiff(term
->selend
, term
->selstart
) / 2) {
4120 term
->selanchor
= term
->selend
;
4121 decpos(term
->selanchor
);
4123 term
->selanchor
= term
->selstart
;
4127 * For rectangular selection, we have a choice of
4128 * _four_ places to put selanchor and selpoint: the
4129 * four corners of the selection.
4131 if (2*selpoint
.x
< term
->selstart
.x
+ term
->selend
.x
)
4132 term
->selanchor
.x
= term
->selend
.x
-1;
4134 term
->selanchor
.x
= term
->selstart
.x
;
4136 if (2*selpoint
.y
< term
->selstart
.y
+ term
->selend
.y
)
4137 term
->selanchor
.y
= term
->selend
.y
;
4139 term
->selanchor
.y
= term
->selstart
.y
;
4141 term
->selstate
= DRAGGING
;
4143 if (term
->selstate
!= ABOUT_TO
&& term
->selstate
!= DRAGGING
)
4144 term
->selanchor
= selpoint
;
4145 term
->selstate
= DRAGGING
;
4146 if (term
->seltype
== LEXICOGRAPHIC
) {
4148 * For normal selection, we set (selstart,selend) to
4149 * (selpoint,selanchor) in some order.
4151 if (poslt(selpoint
, term
->selanchor
)) {
4152 term
->selstart
= selpoint
;
4153 term
->selend
= term
->selanchor
;
4154 incpos(term
->selend
);
4156 term
->selstart
= term
->selanchor
;
4157 term
->selend
= selpoint
;
4158 incpos(term
->selend
);
4162 * For rectangular selection, we may need to
4163 * interchange x and y coordinates (if the user has
4164 * dragged in the -x and +y directions, or vice versa).
4166 term
->selstart
.x
= min(term
->selanchor
.x
, selpoint
.x
);
4167 term
->selend
.x
= 1+max(term
->selanchor
.x
, selpoint
.x
);
4168 term
->selstart
.y
= min(term
->selanchor
.y
, selpoint
.y
);
4169 term
->selend
.y
= max(term
->selanchor
.y
, selpoint
.y
);
4172 } else if ((bcooked
== MBT_SELECT
|| bcooked
== MBT_EXTEND
) &&
4174 if (term
->selstate
== DRAGGING
) {
4176 * We've completed a selection. We now transfer the
4177 * data to the clipboard.
4179 clipme(term
, term
->selstart
, term
->selend
,
4180 (term
->seltype
== RECTANGULAR
));
4181 term
->selstate
= SELECTED
;
4183 term
->selstate
= NO_SELECTION
;
4184 } else if (bcooked
== MBT_PASTE
4186 #if MULTICLICK_ONLY_EVENT
4187 || a
== MA_2CLK
|| a
== MA_3CLK
4190 request_paste(term
->frontend
);
4196 void term_key(Terminal
*term
, Key_Sym keysym
, wchar_t *text
, size_t tlen
,
4197 unsigned int modifiers
, unsigned int flags
)
4201 int prependesc
= FALSE
;
4205 fprintf(stderr
, "keysym = %d, %d chars:", keysym
, tlen
);
4206 for (i
= 0; i
< tlen
; i
++)
4207 fprintf(stderr
, " %04x", (unsigned)text
[i
]);
4208 fprintf(stderr
, "\n");
4212 if ((flags
& PKF_REPEAT
) && term
->repeat_off
)
4215 /* Currently, Meta always just prefixes everything with ESC. */
4216 if (modifiers
& PKM_META
)
4218 modifiers
&= ~PKM_META
;
4221 * Alt is only used for Alt+keypad, which isn't supported yet, so
4224 modifiers
&= ~PKM_ALT
;
4226 /* Standard local function keys */
4227 switch (modifiers
& (PKM_SHIFT
| PKM_CONTROL
)) {
4229 if (keysym
== PK_PAGEUP
)
4230 /* scroll up one page */;
4231 if (keysym
== PK_PAGEDOWN
)
4232 /* scroll down on page */;
4233 if (keysym
== PK_INSERT
)
4234 term_do_paste(term
);
4237 if (keysym
== PK_PAGEUP
)
4238 /* scroll up one line */;
4239 if (keysym
== PK_PAGEDOWN
)
4240 /* scroll down one line */;
4241 /* Control-Numlock for app-keypad mode switch */
4242 if (keysym
== PK_PF1
)
4243 term
->app_keypad_keys
^= 1;
4247 if (modifiers
& PKM_ALT
) {
4248 /* Alt+F4 (close) */
4249 /* Alt+Return (full screen) */
4250 /* Alt+Space (system menu) */
4253 if (keysym
== PK_NULL
&& (modifiers
& PKM_CONTROL
) && tlen
== 1 &&
4254 text
[0] >= 0x20 && text
[0] <= 0x7e) {
4255 /* ASCII chars + Control */
4256 if ((text
[0] >= 0x40 && text
[0] <= 0x5f) ||
4257 (text
[0] >= 0x61 && text
[0] <= 0x7a))
4261 * Control-2 should return ^@ (0x00), Control-6 should return
4262 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
4263 * the DOS keyboard handling did it, and we have nothing better
4264 * to do with the key combo in question, we'll also map
4265 * Control-Backquote to ^\ (0x1C).
4268 case ' ': text
[0] = 0x00; break;
4269 case '-': text
[0] = 0x1f; break;
4270 case '/': text
[0] = 0x1f; break;
4271 case '2': text
[0] = 0x00; break;
4272 case '3': text
[0] = 0x1b; break;
4273 case '4': text
[0] = 0x1c; break;
4274 case '5': text
[0] = 0x1d; break;
4275 case '6': text
[0] = 0x1e; break;
4276 case '7': text
[0] = 0x1f; break;
4277 case '8': text
[0] = 0x7f; break;
4278 case '`': text
[0] = 0x1c; break;
4283 /* Nethack keypad */
4284 if (term
->cfg
.nethack_keypad
) {
4287 case PK_KP1
: c
= 'b'; break;
4288 case PK_KP2
: c
= 'j'; break;
4289 case PK_KP3
: c
= 'n'; break;
4290 case PK_KP4
: c
= 'h'; break;
4291 case PK_KP5
: c
= '.'; break;
4292 case PK_KP6
: c
= 'l'; break;
4293 case PK_KP7
: c
= 'y'; break;
4294 case PK_KP8
: c
= 'k'; break;
4295 case PK_KP9
: c
= 'u'; break;
4296 default: break; /* else gcc warns `enum value not used' */
4300 if (modifiers
& PKM_CONTROL
)
4302 else if (modifiers
& PKM_SHIFT
)
4310 /* Numeric Keypad */
4311 if (PK_ISKEYPAD(keysym
)) {
4315 * In VT400 mode, PFn always emits an escape sequence. In
4316 * Linux and tilde modes, this only happens in app keypad mode.
4318 if (term
->cfg
.funky_type
== FUNKY_VT400
||
4319 ((term
->cfg
.funky_type
== FUNKY_LINUX
||
4320 term
->cfg
.funky_type
== FUNKY_TILDE
) &&
4321 term
->app_keypad_keys
&& !term
->cfg
.no_applic_k
)) {
4323 case PK_PF1
: xkey
= 'P'; break;
4324 case PK_PF2
: xkey
= 'Q'; break;
4325 case PK_PF3
: xkey
= 'R'; break;
4326 case PK_PF4
: xkey
= 'S'; break;
4327 default: break; /* else gcc warns `enum value not used' */
4330 if (term
->app_keypad_keys
&& !term
->cfg
.no_applic_k
) {
4332 case PK_KP0
: xkey
= 'p'; break;
4333 case PK_KP1
: xkey
= 'q'; break;
4334 case PK_KP2
: xkey
= 'r'; break;
4335 case PK_KP3
: xkey
= 's'; break;
4336 case PK_KP4
: xkey
= 't'; break;
4337 case PK_KP5
: xkey
= 'u'; break;
4338 case PK_KP6
: xkey
= 'v'; break;
4339 case PK_KP7
: xkey
= 'w'; break;
4340 case PK_KP8
: xkey
= 'x'; break;
4341 case PK_KP9
: xkey
= 'y'; break;
4342 case PK_KPDECIMAL
: xkey
= 'n'; break;
4343 case PK_KPENTER
: xkey
= 'M'; break;
4344 default: break; /* else gcc warns `enum value not used' */
4346 if (term
->cfg
.funky_type
== FUNKY_XTERM
&& tlen
> 0) {
4348 * xterm can't see the layout of the keypad, so it has
4349 * to rely on the X keysyms returned by the keys.
4350 * Hence, we look at the strings here, not the PuTTY
4351 * keysyms (which describe the layout).
4355 if (modifiers
& PKM_SHIFT
)
4360 case '/': xkey
= 'o'; break;
4361 case '*': xkey
= 'j'; break;
4362 case '-': xkey
= 'm'; break;
4366 * In all other modes, we try to retain the layout of
4367 * the DEC keypad in application mode.
4371 /* This key covers the '-' and ',' keys on a VT220 */
4372 if (modifiers
& PKM_SHIFT
)
4373 xkey
= 'm'; /* VT220 '-' */
4375 xkey
= 'l'; /* VT220 ',' */
4377 case PK_KPMINUS
: xkey
= 'm'; break;
4378 case PK_KPCOMMA
: xkey
= 'l'; break;
4379 default: break; /* else gcc warns `enum value not used' */
4384 if (term
->vt52_mode
) {
4385 if (xkey
>= 'P' && xkey
<= 'S')
4386 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
4388 p
+= sprintf((char *) p
, "\x1B?%c", xkey
);
4390 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
4393 /* Not in application mode -- treat the number pad as arrow keys? */
4394 if ((flags
& PKF_NUMLOCK
) == 0) {
4396 case PK_KP0
: keysym
= PK_INSERT
; break;
4397 case PK_KP1
: keysym
= PK_END
; break;
4398 case PK_KP2
: keysym
= PK_DOWN
; break;
4399 case PK_KP3
: keysym
= PK_PAGEDOWN
; break;
4400 case PK_KP4
: keysym
= PK_LEFT
; break;
4401 case PK_KP5
: keysym
= PK_REST
; break;
4402 case PK_KP6
: keysym
= PK_RIGHT
; break;
4403 case PK_KP7
: keysym
= PK_HOME
; break;
4404 case PK_KP8
: keysym
= PK_UP
; break;
4405 case PK_KP9
: keysym
= PK_PAGEUP
; break;
4406 default: break; /* else gcc warns `enum value not used' */
4411 /* Miscellaneous keys */
4418 *p
++ = (term
->cfg
.bksp_is_delete ?
0x7F : 0x08);
4419 else if (modifiers
== PKM_SHIFT
)
4420 /* We do the opposite of what is configured */
4421 *p
++ = (term
->cfg
.bksp_is_delete ?
0x08 : 0x7F);
4427 else if (modifiers
== PKM_SHIFT
)
4428 *p
++ = 0x1B, *p
++ = '[', *p
++ = 'Z';
4431 /* XXX window.c has ctrl+shift+space sending 0xa0 */
4433 if (modifiers
== PKM_CONTROL
)
4438 case PK_KPENTER
: /* Odd keypad modes handled above */
4439 if (modifiers
== 0) {
4441 if (term
->cr_lf_return
)
4445 default: break; /* else gcc warns `enum value not used' */
4448 /* SCO function keys and editing keys */
4449 if (term
->cfg
.funky_type
== FUNKY_SCO
) {
4450 if (PK_ISFKEY(keysym
) && keysym
<= PK_F12
) {
4451 static char const codes
[] =
4452 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
4453 int index
= keysym
- PK_F1
;
4455 if (modifiers
& PKM_SHIFT
) index
+= 12;
4456 if (modifiers
& PKM_CONTROL
) index
+= 24;
4457 p
+= sprintf((char *) p
, "\x1B[%c", codes
[index
]);
4460 if (PK_ISEDITING(keysym
)) {
4464 case PK_DELETE
: *p
++ = 0x7f; goto done
;
4465 case PK_HOME
: xkey
= 'H'; break;
4466 case PK_INSERT
: xkey
= 'L'; break;
4467 case PK_END
: xkey
= 'F'; break;
4468 case PK_PAGEUP
: xkey
= 'I'; break;
4469 case PK_PAGEDOWN
: xkey
= 'G'; break;
4470 default: break; /* else gcc warns `enum value not used' */
4472 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
4476 if (PK_ISEDITING(keysym
) && (modifiers
& PKM_SHIFT
) == 0) {
4479 if (term
->cfg
.funky_type
== FUNKY_XTERM
) {
4480 /* Xterm shuffles these keys, apparently. */
4482 case PK_HOME
: keysym
= PK_INSERT
; break;
4483 case PK_INSERT
: keysym
= PK_HOME
; break;
4484 case PK_DELETE
: keysym
= PK_END
; break;
4485 case PK_END
: keysym
= PK_PAGEUP
; break;
4486 case PK_PAGEUP
: keysym
= PK_DELETE
; break;
4487 case PK_PAGEDOWN
: keysym
= PK_PAGEDOWN
; break;
4488 default: break; /* else gcc warns `enum value not used' */
4493 if (term
->cfg
.rxvt_homeend
&&
4494 (keysym
== PK_HOME
|| keysym
== PK_END
)) {
4495 p
+= sprintf((char *) p
, keysym
== PK_HOME ?
"\x1B[H" : "\x1BOw");
4499 if (term
->vt52_mode
) {
4503 * A real VT52 doesn't have these, and a VT220 doesn't
4504 * send anything for them in VT52 mode.
4507 case PK_HOME
: xkey
= 'H'; break;
4508 case PK_INSERT
: xkey
= 'L'; break;
4509 case PK_DELETE
: xkey
= 'M'; break;
4510 case PK_END
: xkey
= 'E'; break;
4511 case PK_PAGEUP
: xkey
= 'I'; break;
4512 case PK_PAGEDOWN
: xkey
= 'G'; break;
4513 default: break; /* else gcc warns `enum value not used' */
4515 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
4520 case PK_HOME
: code
= 1; break;
4521 case PK_INSERT
: code
= 2; break;
4522 case PK_DELETE
: code
= 3; break;
4523 case PK_END
: code
= 4; break;
4524 case PK_PAGEUP
: code
= 5; break;
4525 case PK_PAGEDOWN
: code
= 6; break;
4526 default: break; /* else gcc warns `enum value not used' */
4528 p
+= sprintf((char *) p
, "\x1B[%d~", code
);
4532 if (PK_ISFKEY(keysym
)) {
4533 /* Map Shift+F1-F10 to F11-F20 */
4534 if (keysym
>= PK_F1
&& keysym
<= PK_F10
&& (modifiers
& PKM_SHIFT
))
4536 if ((term
->vt52_mode
|| term
->cfg
.funky_type
== FUNKY_VT100P
) &&
4538 /* XXX This overrides the XTERM/VT52 mode below */
4540 if (keysym
>= PK_F6
) offt
++;
4541 if (keysym
>= PK_F12
) offt
++;
4542 p
+= sprintf((char *) p
, term
->vt52_mode ?
"\x1B%c" : "\x1BO%c",
4543 'P' + keysym
- PK_F1
- offt
);
4546 if (term
->cfg
.funky_type
== FUNKY_LINUX
&& keysym
<= PK_F5
) {
4547 p
+= sprintf((char *) p
, "\x1B[[%c", 'A' + keysym
- PK_F1
);
4550 if (term
->cfg
.funky_type
== FUNKY_XTERM
&& keysym
<= PK_F4
) {
4551 if (term
->vt52_mode
)
4552 p
+= sprintf((char *) p
, "\x1B%c", 'P' + keysym
- PK_F1
);
4554 p
+= sprintf((char *) p
, "\x1BO%c", 'P' + keysym
- PK_F1
);
4557 p
+= sprintf((char *) p
, "\x1B[%d~", 11 + keysym
- PK_F1
);
4561 if (PK_ISCURSOR(keysym
)) {
4565 case PK_UP
: xkey
= 'A'; break;
4566 case PK_DOWN
: xkey
= 'B'; break;
4567 case PK_RIGHT
: xkey
= 'C'; break;
4568 case PK_LEFT
: xkey
= 'D'; break;
4569 case PK_REST
: xkey
= 'G'; break; /* centre key on number pad */
4570 default: break; /* else gcc warns `enum value not used' */
4572 if (term
->vt52_mode
)
4573 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
4575 int app_flg
= (term
->app_cursor_keys
&& !term
->cfg
.no_applic_c
);
4577 /* Useful mapping of Ctrl-arrows */
4578 if (modifiers
== PKM_CONTROL
)
4582 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
4584 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
4590 if (p
> output
|| tlen
> 0) {
4592 * Interrupt an ongoing paste. I'm not sure
4593 * this is sensible, but for the moment it's
4594 * preferable to having to faff about buffering
4600 * We need not bother about stdin backlogs
4601 * here, because in GUI PuTTY we can't do
4602 * anything about it anyway; there's no means
4603 * of asking Windows to hold off on KEYDOWN
4604 * messages. We _have_ to buffer everything
4607 term_seen_key_event(term
);
4611 fprintf(stderr
, "sending ESC\n");
4613 ldisc_send(term
->ldisc
, "\x1b", 1, 1);
4618 fprintf(stderr
, "sending %d bytes:", p
- output
);
4619 for (i
= 0; i
< p
- output
; i
++)
4620 fprintf(stderr
, " %02x", output
[i
]);
4621 fprintf(stderr
, "\n");
4623 ldisc_send(term
->ldisc
, output
, p
- output
, 1);
4624 } else if (tlen
> 0) {
4626 fprintf(stderr
, "sending %d unichars:", tlen
);
4627 for (i
= 0; i
< tlen
; i
++)
4628 fprintf(stderr
, " %04x", (unsigned) text
[i
]);
4629 fprintf(stderr
, "\n");
4631 luni_send(term
->ldisc
, text
, tlen
, 1);
4636 void term_nopaste(Terminal
*term
)
4638 if (term
->paste_len
== 0)
4640 sfree(term
->paste_buffer
);
4641 term
->paste_buffer
= NULL
;
4642 term
->paste_len
= 0;
4645 int term_paste_pending(Terminal
*term
)
4647 return term
->paste_len
!= 0;
4650 void term_paste(Terminal
*term
)
4652 long now
, paste_diff
;
4654 if (term
->paste_len
== 0)
4657 /* Don't wait forever to paste */
4658 if (term
->paste_hold
) {
4659 now
= GETTICKCOUNT();
4660 paste_diff
= now
- term
->last_paste
;
4661 if (paste_diff
>= 0 && paste_diff
< 450)
4664 term
->paste_hold
= 0;
4666 while (term
->paste_pos
< term
->paste_len
) {
4668 while (n
+ term
->paste_pos
< term
->paste_len
) {
4669 if (term
->paste_buffer
[term
->paste_pos
+ n
++] == '\015')
4673 luni_send(term
->ldisc
, term
->paste_buffer
+ term
->paste_pos
, n
, 0);
4674 term
->paste_pos
+= n
;
4676 if (term
->paste_pos
< term
->paste_len
) {
4677 term
->paste_hold
= 1;
4681 sfree(term
->paste_buffer
);
4682 term
->paste_buffer
= NULL
;
4683 term
->paste_len
= 0;
4686 static void deselect(Terminal
*term
)
4688 term
->selstate
= NO_SELECTION
;
4689 term
->selstart
.x
= term
->selstart
.y
= term
->selend
.x
= term
->selend
.y
= 0;
4692 void term_deselect(Terminal
*term
)
4698 int term_ldisc(Terminal
*term
, int option
)
4700 if (option
== LD_ECHO
)
4701 return term
->term_echoing
;
4702 if (option
== LD_EDIT
)
4703 return term
->term_editing
;
4708 * from_backend(), to get data from the backend for the terminal.
4710 int from_backend(void *vterm
, int is_stderr
, const char *data
, int len
)
4712 Terminal
*term
= (Terminal
*)vterm
;
4716 bufchain_add(&term
->inbuf
, data
, len
);
4719 * term_out() always completely empties inbuf. Therefore,
4720 * there's no reason at all to return anything other than zero
4721 * from this function, because there _can't_ be a question of
4722 * the remote side needing to wait until term_out() has cleared
4725 * This is a slightly suboptimal way to deal with SSH2 - in
4726 * principle, the window mechanism would allow us to continue
4727 * to accept data on forwarded ports and X connections even
4728 * while the terminal processing was going slowly - but we
4729 * can't do the 100% right thing without moving the terminal
4730 * processing into a separate thread, and that might hurt
4731 * portability. So we manage stdout buffering the old SSH1 way:
4732 * if the terminal processing goes slowly, the whole SSH
4733 * connection stops accepting data until it's ready.
4735 * In practice, I can't imagine this causing serious trouble.
4740 void term_provide_logctx(Terminal
*term
, void *logctx
)
4742 term
->logctx
= logctx
;