Robert de Bath's multi-purpose patch, slightly modified.
[u/mdw/putty] / terminal.c
1 ++ terminal.c.new Mon Nov 22 15:02:03 1999
2 #include <windows.h>
3
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include "putty.h"
8
9 static unsigned long *text; /* buffer of text on terminal screen */
10 static unsigned long *scrtop; /* top of working screen */
11 static unsigned long *disptop; /* top of displayed screen */
12 static unsigned long *sbtop; /* top of scrollback */
13 static unsigned long *cpos; /* cursor position (convenience) */
14 static unsigned long *disptext; /* buffer of text on real screen */
15 static unsigned long *wanttext; /* buffer of text we want on screen */
16 static unsigned long *alttext; /* buffer of text on alt. screen */
17
18 static unsigned char *selspace; /* buffer for building selections in */
19
20 #define TSIZE (sizeof(*text))
21 #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
22
23 static unsigned long curr_attr, save_attr;
24
25 static int curs_x, curs_y; /* cursor */
26 static int save_x, save_y; /* saved cursor position */
27 static int marg_t, marg_b; /* scroll margins */
28 static int dec_om; /* DEC origin mode flag */
29 static int wrap, wrapnext; /* wrap flags */
30 static int insert; /* insert-mode flag */
31 static int cset; /* 0 or 1: which char set */
32 static int save_cset, save_csattr; /* saved with cursor position */
33 static int rvideo; /* global reverse video flag */
34
35 static unsigned long cset_attr[2];
36
37 /*
38 * Saved settings on the alternate screen.
39 */
40 static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
41 static int alt_t, alt_b;
42 static int alt_which;
43
44 #define ARGS_MAX 32 /* max # of esc sequence arguments */
45 #define ARG_DEFAULT -1 /* if an arg isn't specified */
46 #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
47 static int esc_args[ARGS_MAX];
48 static int esc_nargs;
49 static int esc_query;
50
51 #define OSC_STR_MAX 2048
52 static int osc_strlen;
53 static char osc_string[OSC_STR_MAX+1];
54 static int osc_w;
55
56 static unsigned char *tabs;
57
58 #define MAXNL 5
59 static int nl_count;
60
61 static int scroll_heuristic;
62
63 static enum {
64 TOPLEVEL,
65 SEEN_ESC,
66 SEEN_CSI,
67 SEEN_OSC,
68 SEEN_OSC_W,
69
70 DO_CTRLS,
71
72 IGNORE_NEXT,
73 SET_GL, SET_GR,
74 SEEN_OSC_P,
75 OSC_STRING, OSC_MAYBE_ST,
76 SEEN_ESCHASH
77 } termstate;
78
79 static enum {
80 NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
81 } selstate;
82 static enum {
83 SM_CHAR, SM_WORD, SM_LINE
84 } selmode;
85 static unsigned long *selstart, *selend, *selanchor;
86
87 static short wordness[256] = {
88 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 01 */
89 0,1,2,1,1,1,1,1,1,1,1,1,1,2,2,2, 2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1, /* 23 */
90 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2, /* 45 */
91 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1, /* 67 */
92 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 89 */
93 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* AB */
94 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* CD */
95 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* EF */
96 };
97
98 static unsigned char sel_nl[] = SEL_NL;
99
100 /*
101 * Internal prototypes.
102 */
103 static void do_paint (Context, int);
104 static void erase_lots (int, int, int);
105 static void swap_screen (int);
106 static void update_sbar (void);
107 static void deselect (void);
108
109 /*
110 * Set up power-on settings for the terminal.
111 */
112 static void power_on(void) {
113 curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
114 alt_t = marg_t = 0;
115 if (rows != -1)
116 alt_b = marg_b = rows - 1;
117 else
118 alt_b = marg_b = 0;
119 if (cols != -1) {
120 int i;
121 for (i = 0; i < cols; i++)
122 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
123 }
124 alt_om = dec_om = cfg.dec_om;
125 alt_wnext = wrapnext = alt_ins = insert = FALSE;
126 alt_wrap = wrap = cfg.wrap_mode;
127 alt_cset = cset = 0;
128 cset_attr[0] = cset_attr[1] = ATTR_ASCII;
129 rvideo = 0;
130 save_attr = curr_attr = ATTR_DEFAULT;
131 app_cursor_keys = cfg.app_cursor;
132 app_keypad_keys = cfg.app_keypad;
133 alt_which = 0;
134 {
135 int i;
136 for (i = 0; i < 256; i++)
137 wordness[i] = cfg.wordness[i];
138 }
139 if (text) {
140 swap_screen (1);
141 erase_lots (FALSE, TRUE, TRUE);
142 swap_screen (0);
143 erase_lots (FALSE, TRUE, TRUE);
144 }
145 }
146
147 /*
148 * Force a screen update.
149 */
150 void term_update(void) {
151 Context ctx;
152 ctx = get_ctx();
153 if (ctx) {
154 if ( (seen_key_event && (unscroll_event & US_KEY)) &&
155 (seen_disp_event && (unscroll_event & US_DISP)) ) {
156 disptop = scrtop;
157 seen_disp_event = seen_key_event = 0;
158 }
159 do_paint (ctx, TRUE);
160 free_ctx (ctx);
161 nl_count = 0;
162 scroll_heuristic = 0;
163 }
164 }
165
166 /*
167 * Same as power_on(), but an external function.
168 */
169 void term_pwron(void) {
170 power_on();
171 fix_cpos;
172 disptop = scrtop;
173 deselect();
174 term_update();
175 }
176
177 /*
178 * Clear the scrollback.
179 */
180 void term_clrsb(void) {
181 disptop = sbtop = scrtop;
182 update_sbar();
183 }
184
185 /*
186 * Initialise the terminal.
187 */
188 void term_init(void) {
189 text = sbtop = scrtop = disptop = cpos = NULL;
190 disptext = wanttext = NULL;
191 tabs = NULL;
192 selspace = NULL;
193 deselect();
194 rows = cols = -1;
195 nl_count = 0;
196 power_on();
197 }
198
199 /*
200 * Set up the terminal for a given size.
201 */
202 void term_size(int newrows, int newcols, int newsavelines) {
203 unsigned long *newtext, *newdisp, *newwant, *newalt;
204 int i, j, crows, ccols;
205
206 if (newrows == rows && newcols == cols && newsavelines == savelines)
207 return; /* nothing to do */
208
209 alt_t = marg_t = 0;
210 alt_b = marg_b = newrows - 1;
211
212 newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
213 disptop = newtext + newsavelines*(newcols+1);
214 for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
215 newtext[i] = ERASE_CHAR;
216 if (rows != -1) {
217 crows = rows + (scrtop - sbtop) / (cols+1);
218 if (crows > newrows+newsavelines)
219 crows = newrows+newsavelines;
220 ccols = (cols < newcols ? cols : newcols);
221 for (i=0; i<crows; i++) {
222 int oldidx = (rows + savelines - crows + i) * (cols+1);
223 int newidx = (newrows + newsavelines - crows + i) * (newcols+1);
224 for (j=0; j<ccols; j++)
225 newtext[newidx+j] = text[oldidx+j];
226 newtext[newidx+newcols] =
227 (cols == newcols ? text[oldidx+cols] : 0);
228 }
229 sbtop = disptop - (crows - newrows) * (newcols+1);
230 if (sbtop > disptop)
231 sbtop = disptop;
232 } else
233 sbtop = disptop;
234 scrtop = disptop;
235 sfree (text);
236 text = newtext;
237
238 newdisp = smalloc (newrows*(newcols+1)*TSIZE);
239 for (i=0; i<newrows*(newcols+1); i++)
240 newdisp[i] = ATTR_INVALID;
241 sfree (disptext);
242 disptext = newdisp;
243
244 newwant = smalloc (newrows*(newcols+1)*TSIZE);
245 for (i=0; i<newrows*(newcols+1); i++)
246 newwant[i] = ATTR_INVALID;
247 sfree (wanttext);
248 wanttext = newwant;
249
250 newalt = smalloc (newrows*(newcols+1)*TSIZE);
251 for (i=0; i<newrows*(newcols+1); i++)
252 newalt[i] = ERASE_CHAR;
253 sfree (alttext);
254 alttext = newalt;
255
256 sfree (selspace);
257 selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
258
259 tabs = srealloc (tabs, newcols*sizeof(*tabs));
260 {
261 int i;
262 for (i = (cols > 0 ? cols : 0); i < newcols; i++)
263 tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
264 }
265
266 if (rows > 0)
267 curs_y += newrows - rows;
268 if (curs_y < 0)
269 curs_y = 0;
270 if (curs_y >= newrows)
271 curs_y = newrows-1;
272 if (curs_x >= newcols)
273 curs_x = newcols-1;
274 alt_x = alt_y = 0;
275 wrapnext = alt_wnext = FALSE;
276
277 rows = newrows;
278 cols = newcols;
279 savelines = newsavelines;
280 fix_cpos;
281
282 deselect();
283 update_sbar();
284 term_update();
285 }
286
287 /*
288 * Swap screens.
289 */
290 static void swap_screen (int which) {
291 int t;
292 unsigned long tt;
293
294 if (which == alt_which)
295 return;
296
297 alt_which = which;
298
299 for (t=0; t<rows*(cols+1); t++) {
300 tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
301 }
302
303 t = curs_x; curs_x = alt_x; alt_x = t;
304 t = curs_y; curs_y = alt_y; alt_y = t;
305 t = marg_t; marg_t = alt_t; alt_t = t;
306 t = marg_b; marg_b = alt_b; alt_b = t;
307 t = dec_om; dec_om = alt_om; alt_om = t;
308 t = wrap; wrap = alt_wrap; alt_wrap = t;
309 t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
310 t = insert; insert = alt_ins; alt_ins = t;
311 t = cset; cset = alt_cset; alt_cset = t;
312
313 fix_cpos;
314 }
315
316 /*
317 * Retrieve a character from `inbuf'.
318 */
319 static int inbuf_getc(void) {
320 if (inbuf_head == inbuf_reap)
321 return -1; /* EOF */
322 else {
323 int n = inbuf_reap;
324 inbuf_reap = (inbuf_reap+1) & INBUF_MASK;
325 return inbuf[n];
326 }
327 }
328
329 /*
330 * Update the scroll bar.
331 */
332 static void update_sbar(void) {
333 int min;
334
335 min = (sbtop - text) / (cols+1);
336 set_sbar ((scrtop - text) / (cols+1) + rows - min,
337 (disptop - text) / (cols+1) - min,
338 rows);
339 }
340
341 /*
342 * Check whether the region bounded by the two pointers intersects
343 * the scroll region, and de-select the on-screen selection if so.
344 */
345 static void check_selection (unsigned long *from, unsigned long *to) {
346 if (from < selend && selstart < to)
347 deselect();
348 }
349
350 /*
351 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
352 * for backward.) `sb' is TRUE if the scrolling is permitted to
353 * affect the scrollback buffer.
354 */
355 static void scroll (int topline, int botline, int lines, int sb) {
356 unsigned long *scroll_top;
357 int scroll_size, size, i;
358
359 scroll_top = scrtop + topline*(cols+1);
360 size = (lines < 0 ? -lines : lines) * (cols+1);
361 scroll_size = (botline - topline + 1) * (cols+1) - size;
362
363 if (lines > 0 && topline == 0 && alt_which == 0 && sb) {
364 /*
365 * Since we're going to scroll the top line and we're on the
366 * scrolling screen let's also affect the scrollback buffer.
367 */
368 sbtop -= lines * (cols+1);
369 if (sbtop < text)
370 sbtop = text;
371 scroll_size += scroll_top - sbtop;
372 scroll_top = sbtop;
373 update_sbar();
374 }
375
376 if (scroll_size < 0) {
377 size += scroll_size;
378 scroll_size = 0;
379 }
380
381 if (lines > 0) {
382 if (scroll_size)
383 memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
384 for (i = 0; i < size; i++)
385 scroll_top[i+scroll_size] = ERASE_CHAR;
386 if (selstart > scroll_top &&
387 selstart < scroll_top + size + scroll_size) {
388 selstart -= size;
389 if (selstart < scroll_top)
390 selstart = scroll_top;
391 }
392 if (selend > scroll_top &&
393 selend < scroll_top + size + scroll_size) {
394 selend -= size;
395 if (selend < scroll_top)
396 selend = scroll_top;
397 }
398 } else {
399 if (scroll_size)
400 memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
401 for (i = 0; i < size; i++)
402 scroll_top[i] = ERASE_CHAR;
403 if (selstart > scroll_top &&
404 selstart < scroll_top + size + scroll_size) {
405 selstart += size;
406 if (selstart > scroll_top + size + scroll_size)
407 selstart = scroll_top + size + scroll_size;
408 }
409 if (selend > scroll_top &&
410 selend < scroll_top + size + scroll_size) {
411 selend += size;
412 if (selend > scroll_top + size + scroll_size)
413 selend = scroll_top + size + scroll_size;
414 }
415 }
416
417 scroll_heuristic += lines;
418 }
419
420 /*
421 * Move the cursor to a given position, clipping at boundaries. We
422 * may or may not want to clip at the scroll margin: marg_clip is 0
423 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
424 * even _being_ outside the margins.
425 */
426 static void move (int x, int y, int marg_clip) {
427 if (x < 0)
428 x = 0;
429 if (x >= cols)
430 x = cols-1;
431 if (marg_clip) {
432 if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
433 y = marg_t;
434 if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
435 y = marg_b;
436 }
437 if (y < 0)
438 y = 0;
439 if (y >= rows)
440 y = rows-1;
441 curs_x = x;
442 curs_y = y;
443 fix_cpos;
444 wrapnext = FALSE;
445 }
446
447 /*
448 * Save or restore the cursor and SGR mode.
449 */
450 static void save_cursor(int save) {
451 if (save) {
452 save_x = curs_x;
453 save_y = curs_y;
454 save_attr = curr_attr;
455 save_cset = cset;
456 save_csattr = cset_attr[cset];
457 } else {
458 curs_x = save_x;
459 curs_y = save_y;
460 curr_attr = save_attr;
461 cset = save_cset;
462 cset_attr[cset] = save_csattr;
463 fix_cpos;
464 }
465 }
466
467 /*
468 * Erase a large portion of the screen: the whole screen, or the
469 * whole line, or parts thereof.
470 */
471 static void erase_lots (int line_only, int from_begin, int to_end) {
472 unsigned long *startpos, *endpos;
473
474 if (line_only) {
475 startpos = cpos - curs_x;
476 endpos = startpos + cols+1;
477 } else {
478 startpos = scrtop;
479 endpos = startpos + rows * (cols+1);
480 }
481 if (!from_begin)
482 startpos = cpos;
483 if (!to_end)
484 endpos = cpos;
485 check_selection (startpos, endpos);
486 while (startpos < endpos)
487 *startpos++ = ERASE_CHAR;
488 }
489
490 /*
491 * Insert or delete characters within the current line. n is +ve if
492 * insertion is desired, and -ve for deletion.
493 */
494 static void insch (int n) {
495 int dir = (n < 0 ? -1 : +1);
496 int m;
497
498 n = (n < 0 ? -n : n);
499 if (n > cols - curs_x)
500 n = cols - curs_x;
501 m = cols - curs_x - n;
502 check_selection (cpos, cpos+n);
503 if (dir < 0) {
504 memmove (cpos, cpos+n, m*TSIZE);
505 while (n--)
506 cpos[m++] = ERASE_CHAR;
507 } else {
508 memmove (cpos+n, cpos, m*TSIZE);
509 while (n--)
510 cpos[n] = ERASE_CHAR;
511 }
512 }
513
514 /*
515 * Toggle terminal mode `mode' to state `state'. (`query' indicates
516 * whether the mode is a DEC private one or a normal one.)
517 */
518 static void toggle_mode (int mode, int query, int state) {
519 if (query) switch (mode) {
520 case 1: /* application cursor keys */
521 app_cursor_keys = state;
522 break;
523 case 3: /* 80/132 columns */
524 deselect();
525 request_resize (state ? 132 : 80, rows);
526 break;
527 case 5: /* reverse video */
528 rvideo = state;
529 seen_disp_event = TRUE;
530 break;
531 case 6: /* DEC origin mode */
532 dec_om = state;
533 break;
534 case 7: /* auto wrap */
535 wrap = state;
536 break;
537 case 47: /* alternate screen */
538 deselect();
539 swap_screen (state);
540 disptop = scrtop;
541 break;
542 } else switch (mode) {
543 case 4: /* set insert mode */
544 insert = state;
545 break;
546 }
547 }
548
549 /*
550 * Process an OSC sequence: set window title or icon name.
551 */
552 static void do_osc(void) {
553 if (osc_w) {
554 while (osc_strlen--)
555 wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
556 } else {
557 osc_string[osc_strlen] = '\0';
558 switch (esc_args[0]) {
559 case 0:
560 case 1:
561 set_icon (osc_string);
562 if (esc_args[0] == 1)
563 break;
564 /* fall through: parameter 0 means set both */
565 case 2:
566 case 21:
567 set_title (osc_string);
568 break;
569 }
570 }
571 }
572
573 /*
574 * Remove everything currently in `inbuf' and stick it up on the
575 * in-memory display. There's a big state machine in here to
576 * process escape sequences...
577 */
578 void term_out(void) {
579 int c;
580
581 while ( (c = inbuf_getc()) != -1) {
582 #ifdef LOG
583 {
584 static FILE *fp = NULL;
585 if (!fp) fp = fopen("putty.log", "wb");
586 if (fp) fputc (c, fp);
587 }
588 #endif
589 if( termstate < DO_CTRLS && (c&0x60) == 0 ) {
590 switch (c) {
591 case '\005': /* terminal type query */
592 ldisc->send ("PuTTY\r", 6);
593 break;
594 case '\007':
595 beep();
596 disptop = scrtop;
597 break;
598 case '\b':
599 if (curs_x == 0 && curs_y > 0)
600 curs_x = cols-1, curs_y--;
601 else if (wrapnext)
602 wrapnext = FALSE;
603 else
604 curs_x--;
605 fix_cpos;
606 seen_disp_event = TRUE;
607 break;
608 case '\016':
609 cset = 1;
610 break;
611 case '\017':
612 cset = 0;
613 break;
614 case '\033':
615 termstate = SEEN_ESC;
616 break;
617 case 0233:
618 termstate = SEEN_CSI;
619 esc_nargs = 1;
620 esc_args[0] = ARG_DEFAULT;
621 esc_query = FALSE;
622 break;
623 case 0235:
624 termstate = SEEN_OSC;
625 esc_args[0] = 0;
626 break;
627 case '\r':
628 curs_x = 0;
629 wrapnext = FALSE;
630 fix_cpos;
631 seen_disp_event = TRUE;
632 break;
633 case '\013':
634 case '\014':
635 case '\n':
636 if (curs_y == marg_b)
637 scroll (marg_t, marg_b, 1, TRUE);
638 else if (curs_y < rows-1)
639 curs_y++;
640 if (cfg.lfhascr)
641 curs_x = 0;
642 fix_cpos;
643 wrapnext = FALSE;
644 seen_disp_event = 1;
645 nl_count++;
646 break;
647 case '\t':
648 do {
649 curs_x++;
650 } while (curs_x < cols-1 && !tabs[curs_x]);
651 if (curs_x >= cols)
652 curs_x = cols-1;
653 {
654 unsigned long *old_cpos = cpos;
655 fix_cpos;
656 check_selection (old_cpos, cpos);
657 }
658 seen_disp_event = TRUE;
659 break;
660 }
661 }
662 else switch (termstate) {
663 case TOPLEVEL:
664 if (c >= ' ' && c != 0234) {
665 if (wrapnext) {
666 cpos[1] = ATTR_WRAPPED;
667 if (curs_y == marg_b)
668 scroll (marg_t, marg_b, 1, TRUE);
669 else if (curs_y < rows-1)
670 curs_y++;
671 curs_x = 0;
672 fix_cpos;
673 wrapnext = FALSE;
674 nl_count++;
675 }
676 if (insert)
677 insch (1);
678 check_selection (cpos, cpos+1);
679 *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
680 (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
681 curs_x++;
682 if (curs_x == cols) {
683 cpos--;
684 curs_x--;
685 wrapnext = wrap;
686 }
687 seen_disp_event = 1;
688 }
689 break;
690
691 case IGNORE_NEXT:
692 termstate = TOPLEVEL;
693 break;
694 case OSC_MAYBE_ST:
695 /*
696 * This state is virtually identical to SEEN_ESC, with the
697 * exception that we have an OSC sequence in the pipeline,
698 * and _if_ we see a backslash, we process it.
699 */
700 if (c == '\\') {
701 do_osc();
702 termstate = TOPLEVEL;
703 break;
704 }
705 /* else fall through */
706 case SEEN_ESC:
707 termstate = TOPLEVEL;
708 switch (c) {
709 case ' ': /* some weird sequence? */
710 termstate = IGNORE_NEXT;
711 break;
712 case '[': /* enter CSI mode */
713 termstate = SEEN_CSI;
714 esc_nargs = 1;
715 esc_args[0] = ARG_DEFAULT;
716 esc_query = FALSE;
717 break;
718 case ']': /* xterm escape sequences */
719 termstate = SEEN_OSC;
720 esc_args[0] = 0;
721 break;
722 case '(': /* should set GL */
723 termstate = SET_GL;
724 break;
725 case ')': /* should set GR */
726 termstate = SET_GR;
727 break;
728 case '7': /* save cursor */
729 save_cursor (TRUE);
730 break;
731 case '8': /* restore cursor */
732 save_cursor (FALSE);
733 seen_disp_event = TRUE;
734 break;
735 case '=':
736 app_keypad_keys = TRUE;
737 break;
738 case '>':
739 app_keypad_keys = FALSE;
740 break;
741 case 'D': /* exactly equivalent to LF */
742 if (curs_y == marg_b)
743 scroll (marg_t, marg_b, 1, TRUE);
744 else if (curs_y < rows-1)
745 curs_y++;
746 fix_cpos;
747 wrapnext = FALSE;
748 seen_disp_event = TRUE;
749 nl_count++;
750 break;
751 case 'E': /* exactly equivalent to CR-LF */
752 curs_x = 0;
753 wrapnext = FALSE;
754 if (curs_y == marg_b)
755 scroll (marg_t, marg_b, 1, TRUE);
756 else if (curs_y < rows-1)
757 curs_y++;
758 fix_cpos;
759 wrapnext = FALSE;
760 nl_count++;
761 seen_disp_event = TRUE;
762 break;
763 case 'M': /* reverse index - backwards LF */
764 if (curs_y == marg_t)
765 scroll (marg_t, marg_b, -1, TRUE);
766 else if (curs_y > 0)
767 curs_y--;
768 fix_cpos;
769 wrapnext = FALSE;
770 seen_disp_event = TRUE;
771 break;
772 case 'Z': /* terminal type query */
773 ldisc->send ("\033[?6c", 5);
774 break;
775 case 'c': /* restore power-on settings */
776 power_on();
777 fix_cpos;
778 disptop = scrtop;
779 seen_disp_event = TRUE;
780 break;
781 case '#': /* ESC # 8 fills screen with Es :-) */
782 termstate = SEEN_ESCHASH;
783 break;
784 case 'H': /* set a tab */
785 tabs[curs_x] = TRUE;
786 break;
787 }
788 break;
789 case SEEN_CSI:
790 termstate = TOPLEVEL; /* default */
791 switch (c) {
792 case '0': case '1': case '2': case '3': case '4':
793 case '5': case '6': case '7': case '8': case '9':
794 if (esc_nargs <= ARGS_MAX) {
795 if (esc_args[esc_nargs-1] == ARG_DEFAULT)
796 esc_args[esc_nargs-1] = 0;
797 esc_args[esc_nargs-1] =
798 10 * esc_args[esc_nargs-1] + c - '0';
799 }
800 termstate = SEEN_CSI;
801 break;
802 case ';':
803 if (++esc_nargs <= ARGS_MAX)
804 esc_args[esc_nargs-1] = ARG_DEFAULT;
805 termstate = SEEN_CSI;
806 break;
807 case '?':
808 esc_query = TRUE;
809 termstate = SEEN_CSI;
810 break;
811 case 'A': /* move up N lines */
812 move (curs_x, curs_y - def(esc_args[0], 1), 1);
813 seen_disp_event = TRUE;
814 break;
815 case 'B': case 'e': /* move down N lines */
816 move (curs_x, curs_y + def(esc_args[0], 1), 1);
817 seen_disp_event = TRUE;
818 break;
819 case 'C': case 'a': /* move right N cols */
820 move (curs_x + def(esc_args[0], 1), curs_y, 1);
821 seen_disp_event = TRUE;
822 break;
823 case 'D': /* move left N cols */
824 move (curs_x - def(esc_args[0], 1), curs_y, 1);
825 seen_disp_event = TRUE;
826 break;
827 case 'E': /* move down N lines and CR */
828 move (0, curs_y + def(esc_args[0], 1), 1);
829 seen_disp_event = TRUE;
830 break;
831 case 'F': /* move up N lines and CR */
832 move (0, curs_y - def(esc_args[0], 1), 1);
833 seen_disp_event = TRUE;
834 break;
835 case 'G': case '`': /* set horizontal posn */
836 move (def(esc_args[0], 1) - 1, curs_y, 0);
837 seen_disp_event = TRUE;
838 break;
839 case 'd': /* set vertical posn */
840 move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
841 (dec_om ? 2 : 0));
842 seen_disp_event = TRUE;
843 break;
844 case 'H': case 'f': /* set horz and vert posns at once */
845 if (esc_nargs < 2)
846 esc_args[1] = ARG_DEFAULT;
847 move (def(esc_args[1], 1) - 1,
848 (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
849 (dec_om ? 2 : 0));
850 seen_disp_event = TRUE;
851 break;
852 case 'J': /* erase screen or parts of it */
853 {
854 unsigned int i = def(esc_args[0], 0) + 1;
855 if (i > 3)
856 i = 0;
857 erase_lots(FALSE, !!(i & 2), !!(i & 1));
858 }
859 disptop = scrtop;
860 seen_disp_event = TRUE;
861 break;
862 case 'K': /* erase line or parts of it */
863 {
864 unsigned int i = def(esc_args[0], 0) + 1;
865 if (i > 3)
866 i = 0;
867 erase_lots(TRUE, !!(i & 2), !!(i & 1));
868 }
869 seen_disp_event = TRUE;
870 break;
871 case 'L': /* insert lines */
872 if (curs_y <= marg_b)
873 scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
874 seen_disp_event = TRUE;
875 break;
876 case 'M': /* delete lines */
877 if (curs_y <= marg_b)
878 scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
879 seen_disp_event = TRUE;
880 break;
881 case '@': /* insert chars */
882 insch (def(esc_args[0], 1));
883 seen_disp_event = TRUE;
884 break;
885 case 'P': /* delete chars */
886 insch (-def(esc_args[0], 1));
887 seen_disp_event = TRUE;
888 break;
889 case 'c': /* terminal type query */
890 ldisc->send ("\033[?6c", 5);
891 break;
892 case 'n': /* cursor position query */
893 if (esc_args[0] == 6) {
894 char buf[32];
895 sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
896 ldisc->send (buf, strlen(buf));
897 }
898 break;
899 case 'h': /* toggle a mode to high */
900 toggle_mode (esc_args[0], esc_query, TRUE);
901 break;
902 case 'l': /* toggle a mode to low */
903 toggle_mode (esc_args[0], esc_query, FALSE);
904 break;
905 case 'g': /* clear tabs */
906 if (esc_nargs == 1) {
907 if (esc_args[0] == 0) {
908 tabs[curs_x] = FALSE;
909 } else if (esc_args[0] == 3) {
910 int i;
911 for (i = 0; i < cols; i++)
912 tabs[i] = FALSE;
913 }
914 }
915 break;
916 case 'r': /* set scroll margins */
917 if (!esc_query && esc_nargs <= 2) {
918 int top, bot;
919 top = def(esc_args[0], 1) - 1;
920 if (top < 0)
921 top = 0;
922 bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
923 def(esc_args[1], rows)) - 1;
924 if (bot >= rows)
925 bot = rows-1;
926 if (top <= bot) {
927 marg_t = top;
928 marg_b = bot;
929 curs_x = 0;
930 /*
931 * I used to think the cursor should be
932 * placed at the top of the newly marginned
933 * area. Apparently not: VMS TPU falls over
934 * if so.
935 */
936 curs_y = 0;
937 fix_cpos;
938 seen_disp_event = TRUE;
939 }
940 }
941 break;
942 case 'm': /* set graphics rendition */
943 {
944 int i;
945 for (i=0; i<esc_nargs; i++) {
946 switch (def(esc_args[i], 0)) {
947 case 0: /* restore defaults */
948 curr_attr = ATTR_DEFAULT; break;
949 case 1: /* enable bold */
950 curr_attr |= ATTR_BOLD; break;
951 case 4: /* enable underline */
952 case 21: /* (enable double underline) */
953 curr_attr |= ATTR_UNDER; break;
954 case 7: /* enable reverse video */
955 curr_attr |= ATTR_REVERSE; break;
956 case 22: /* disable bold */
957 curr_attr &= ~ATTR_BOLD; break;
958 case 24: /* disable underline */
959 curr_attr &= ~ATTR_UNDER; break;
960 case 27: /* disable reverse video */
961 curr_attr &= ~ATTR_REVERSE; break;
962 case 30: case 31: case 32: case 33:
963 case 34: case 35: case 36: case 37:
964 /* foreground */
965 curr_attr &= ~ATTR_FGMASK;
966 curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
967 break;
968 case 39: /* default-foreground */
969 curr_attr &= ~ATTR_FGMASK;
970 curr_attr |= ATTR_DEFFG;
971 break;
972 case 40: case 41: case 42: case 43:
973 case 44: case 45: case 46: case 47:
974 /* background */
975 curr_attr &= ~ATTR_BGMASK;
976 curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
977 break;
978 case 49: /* default-background */
979 curr_attr &= ~ATTR_BGMASK;
980 curr_attr |= ATTR_DEFBG;
981 break;
982 }
983 }
984 }
985 break;
986 case 's': /* save cursor */
987 save_cursor (TRUE);
988 break;
989 case 'u': /* restore cursor */
990 save_cursor (FALSE);
991 seen_disp_event = TRUE;
992 break;
993 case 't': /* set page size - ie window height */
994 request_resize (cols, def(esc_args[0], 24));
995 deselect();
996 break;
997 case 'X': /* write N spaces w/o moving cursor */
998 {
999 int n = def(esc_args[0], 1);
1000 unsigned long *p = cpos;
1001 if (n > cols - curs_x)
1002 n = cols - curs_x;
1003 check_selection (cpos, cpos+n);
1004 while (n--)
1005 *p++ = ERASE_CHAR;
1006 seen_disp_event = TRUE;
1007 }
1008 break;
1009 case 'x': /* report terminal characteristics */
1010 {
1011 char buf[32];
1012 int i = def(esc_args[0], 0);
1013 if (i == 0 || i == 1) {
1014 strcpy (buf, "\033[2;1;1;112;112;1;0x");
1015 buf[2] += i;
1016 ldisc->send (buf, 20);
1017 }
1018 }
1019 break;
1020 }
1021 break;
1022 case SET_GL:
1023 case SET_GR:
1024 switch (c) {
1025 case 'A':
1026 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
1027 break;
1028 case '0':
1029 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
1030 break;
1031 default: /* specifically, 'B' */
1032 cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
1033 break;
1034 }
1035 termstate = TOPLEVEL;
1036 break;
1037 case SEEN_OSC:
1038 osc_w = FALSE;
1039 switch (c) {
1040 case 'P': /* Linux palette sequence */
1041 termstate = SEEN_OSC_P;
1042 osc_strlen = 0;
1043 break;
1044 case 'R': /* Linux palette reset */
1045 palette_reset();
1046 term_invalidate();
1047 termstate = TOPLEVEL;
1048 break;
1049 case 'W': /* word-set */
1050 termstate = SEEN_OSC_W;
1051 osc_w = TRUE;
1052 break;
1053 case '0': case '1': case '2': case '3': case '4':
1054 case '5': case '6': case '7': case '8': case '9':
1055 esc_args[0] = 10 * esc_args[0] + c - '0';
1056 break;
1057 case 'L':
1058 /*
1059 * Grotty hack to support xterm and DECterm title
1060 * sequences concurrently.
1061 */
1062 if (esc_args[0] == 2) {
1063 esc_args[0] = 1;
1064 break;
1065 }
1066 /* else fall through */
1067 default:
1068 termstate = OSC_STRING;
1069 osc_strlen = 0;
1070 }
1071 break;
1072 case OSC_STRING:
1073 if (c == 0234 || c == '\007') {
1074 /*
1075 * These characters terminate the string; ST and BEL
1076 * terminate the sequence and trigger instant
1077 * processing of it, whereas ESC goes back to SEEN_ESC
1078 * mode unless it is followed by \, in which case it is
1079 * synonymous with ST in the first place.
1080 */
1081 do_osc();
1082 termstate = TOPLEVEL;
1083 } else if (c == '\033')
1084 termstate = OSC_MAYBE_ST;
1085 else if (osc_strlen < OSC_STR_MAX)
1086 osc_string[osc_strlen++] = c;
1087 break;
1088 case SEEN_OSC_P:
1089 {
1090 int max = (osc_strlen == 0 ? 21 : 16);
1091 int val;
1092 if (c >= '0' && c <= '9')
1093 val = c - '0';
1094 else if (c >= 'A' && c <= 'A'+max-10)
1095 val = c - 'A' + 10;
1096 else if (c >= 'a' && c <= 'a'+max-10)
1097 val = c - 'a' + 10;
1098 else
1099 termstate = TOPLEVEL;
1100 osc_string[osc_strlen++] = val;
1101 if (osc_strlen >= 7) {
1102 palette_set (osc_string[0],
1103 osc_string[1] * 16 + osc_string[2],
1104 osc_string[3] * 16 + osc_string[4],
1105 osc_string[5] * 16 + osc_string[6]);
1106 term_invalidate();
1107 termstate = TOPLEVEL;
1108 }
1109 }
1110 break;
1111 case SEEN_OSC_W:
1112 switch (c) {
1113 case '0': case '1': case '2': case '3': case '4':
1114 case '5': case '6': case '7': case '8': case '9':
1115 esc_args[0] = 10 * esc_args[0] + c - '0';
1116 break;
1117 default:
1118 termstate = OSC_STRING;
1119 osc_strlen = 0;
1120 }
1121 break;
1122 case SEEN_ESCHASH:
1123 if (c == '8') {
1124 unsigned long *p = scrtop;
1125 int n = rows * (cols+1);
1126 while (n--)
1127 *p++ = ATTR_DEFAULT | 'E';
1128 disptop = scrtop;
1129 seen_disp_event = TRUE;
1130 check_selection (scrtop, scrtop + rows * (cols+1));
1131 }
1132 termstate = TOPLEVEL;
1133 break;
1134 }
1135 check_selection (cpos, cpos+1);
1136 }
1137
1138 if (nl_count > MAXNL)
1139 term_update();
1140 }
1141
1142 /*
1143 * Compare two lines to determine whether they are sufficiently
1144 * alike to scroll-optimise one to the other. Return the degree of
1145 * similarity.
1146 */
1147 static int linecmp (unsigned long *a, unsigned long *b) {
1148 int i, n;
1149
1150 for (i=n=0; i < cols; i++)
1151 n += (*a++ == *b++);
1152 return n;
1153 }
1154
1155 /*
1156 * Given a context, update the window. Out of paranoia, we don't
1157 * allow WM_PAINT responses to do scrolling optimisations.
1158 */
1159 static void do_paint (Context ctx, int may_optimise){
1160 int i, j, start, our_curs_y;
1161 unsigned long attr, rv, cursor;
1162 char ch[1024];
1163
1164 cursor = (has_focus ? ATTR_ACTCURS : ATTR_PASCURS);
1165 rv = (rvideo ? ATTR_REVERSE : 0);
1166 our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
1167
1168 for (i=0; i<rows; i++) {
1169 int idx = i*(cols+1);
1170 for (j=0; j<=cols; j++,idx++) {
1171 unsigned long *d = disptop+idx;
1172 wanttext[idx] = ((*d ^ rv
1173 ^ (selstart <= d && d < selend ?
1174 ATTR_REVERSE : 0)) |
1175 (i==our_curs_y && j==curs_x ? cursor : 0));
1176 }
1177 }
1178
1179 /*
1180 * We would perform scrolling optimisations in here, if they
1181 * didn't have a nasty tendency to cause the whole sodding
1182 * program to hang for a second at speed-critical moments.
1183 * We'll leave it well alone...
1184 */
1185
1186 for (i=0; i<rows; i++) {
1187 int idx = i*(cols+1);
1188 start = -1;
1189 for (j=0; j<=cols; j++,idx++) {
1190 unsigned long t = wanttext[idx];
1191 int needs_update = (j < cols && t != disptext[idx]);
1192 int keep_going = (start != -1 && needs_update &&
1193 (t & ATTR_MASK) == attr &&
1194 j-start < sizeof(ch));
1195 if (start != -1 && !keep_going) {
1196 do_text (ctx, start, i, ch, j-start, attr);
1197 start = -1;
1198 }
1199 if (needs_update) {
1200 if (start == -1) {
1201 start = j;
1202 attr = t & ATTR_MASK;
1203 }
1204 ch[j-start] = (char) (t & CHAR_MASK);
1205 }
1206 disptext[idx] = t;
1207 }
1208 }
1209 }
1210
1211 /*
1212 * Invalidate the whole screen so it will be repainted in full.
1213 */
1214 void term_invalidate(void) {
1215 int i;
1216
1217 for (i=0; i<rows*(cols+1); i++)
1218 disptext[i] = ATTR_INVALID;
1219 }
1220
1221 /*
1222 * Paint the window in response to a WM_PAINT message.
1223 */
1224 void term_paint (Context ctx, int l, int t, int r, int b) {
1225 int i, j, left, top, right, bottom;
1226
1227 left = l / font_width;
1228 right = (r - 1) / font_width;
1229 top = t / font_height;
1230 bottom = (b - 1) / font_height;
1231 for (i = top; i <= bottom && i < rows ; i++)
1232 for (j = left; j <= right && j < cols ; j++)
1233 disptext[i*(cols+1)+j] = ATTR_INVALID;
1234
1235 do_paint (ctx, FALSE);
1236 }
1237
1238 /*
1239 * Attempt to scroll the scrollback. The second parameter gives the
1240 * position we want to scroll to; the first is +1 to denote that
1241 * this position is relative to the beginning of the scrollback, -1
1242 * to denote it is relative to the end, and 0 to denote that it is
1243 * relative to the current position.
1244 */
1245 void term_scroll (int rel, int where) {
1246 int n = where * (cols+1);
1247
1248 disptop = (rel < 0 ? scrtop :
1249 rel > 0 ? sbtop : disptop) + n;
1250 if (disptop < sbtop)
1251 disptop = sbtop;
1252 if (disptop > scrtop)
1253 disptop = scrtop;
1254 update_sbar();
1255 term_update();
1256 }
1257
1258 /*
1259 * Spread the selection outwards according to the selection mode.
1260 */
1261 static unsigned long *sel_spread_half (unsigned long *p, int dir) {
1262 unsigned long *linestart, *lineend;
1263 int x;
1264 short wvalue;
1265
1266 x = (p - text) % (cols+1);
1267 linestart = p - x;
1268 lineend = linestart + cols;
1269
1270 switch (selmode) {
1271 case SM_CHAR:
1272 /*
1273 * In this mode, every character is a separate unit, except
1274 * for runs of spaces at the end of a non-wrapping line.
1275 */
1276 if (!(linestart[cols] & ATTR_WRAPPED)) {
1277 unsigned long *q = lineend;
1278 while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
1279 q--;
1280 if (q == lineend)
1281 q--;
1282 if (p >= q)
1283 p = (dir == -1 ? q : lineend - 1);
1284 }
1285 break;
1286 case SM_WORD:
1287 /*
1288 * In this mode, the units are maximal runs of characters
1289 * whose `wordness' has the same value.
1290 */
1291 wvalue = wordness[*p & CHAR_MASK];
1292 if (dir == +1) {
1293 while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
1294 p++;
1295 } else {
1296 while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
1297 p--;
1298 }
1299 break;
1300 case SM_LINE:
1301 /*
1302 * In this mode, every line is a unit.
1303 */
1304 p = (dir == -1 ? linestart : lineend - 1);
1305 break;
1306 }
1307 return p;
1308 }
1309
1310 static void sel_spread (void) {
1311 selstart = sel_spread_half (selstart, -1);
1312 selend = sel_spread_half (selend - 1, +1) + 1;
1313 }
1314
1315 void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
1316 unsigned long *selpoint;
1317
1318 if (y<0) y = 0;
1319 if (y>=rows) y = rows-1;
1320 if (x<0) {
1321 if (y > 0) {
1322 x = cols-1;
1323 y--;
1324 } else
1325 x = 0;
1326 }
1327 if (x>=cols) x = cols-1;
1328
1329 selpoint = disptop + y * (cols+1) + x;
1330
1331 if (b == MB_SELECT && a == MA_CLICK) {
1332 deselect();
1333 selstate = ABOUT_TO;
1334 selanchor = selpoint;
1335 selmode = SM_CHAR;
1336 } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
1337 deselect();
1338 selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
1339 selstate = DRAGGING;
1340 selstart = selanchor = selpoint;
1341 selend = selstart + 1;
1342 sel_spread();
1343 } else if ((b == MB_SELECT && a == MA_DRAG) ||
1344 (b == MB_EXTEND && a != MA_RELEASE)) {
1345 if (selstate == ABOUT_TO && selanchor == selpoint)
1346 return;
1347 if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
1348 if (selpoint-selstart < (selend-selstart)/2)
1349 selanchor = selend - 1;
1350 else
1351 selanchor = selstart;
1352 selstate = DRAGGING;
1353 }
1354 if (selstate != ABOUT_TO && selstate != DRAGGING)
1355 selanchor = selpoint;
1356 selstate = DRAGGING;
1357 if (selpoint < selanchor) {
1358 selstart = selpoint;
1359 selend = selanchor + 1;
1360 } else {
1361 selstart = selanchor;
1362 selend = selpoint + 1;
1363 }
1364 sel_spread();
1365 } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
1366 if (selstate == DRAGGING) {
1367 /*
1368 * We've completed a selection. We now transfer the
1369 * data to the clipboard.
1370 */
1371 unsigned char *p = selspace;
1372 unsigned long *q = selstart;
1373
1374 while (q < selend) {
1375 int nl = FALSE;
1376 unsigned long *lineend = q - (q-text) % (cols+1) + cols;
1377 unsigned long *nlpos = lineend;
1378
1379 if (!(*nlpos & ATTR_WRAPPED)) {
1380 while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
1381 nlpos--;
1382 if (nlpos < selend)
1383 nl = TRUE;
1384 }
1385 while (q < nlpos && q < selend)
1386 *p++ = (unsigned char) (*q++ & CHAR_MASK);
1387 if (nl) {
1388 int i;
1389 for (i=0; i<sizeof(sel_nl); i++)
1390 *p++ = sel_nl[i];
1391 }
1392 q = lineend + 1; /* start of next line */
1393 }
1394 write_clip (selspace, p - selspace);
1395 selstate = SELECTED;
1396 } else
1397 selstate = NO_SELECTION;
1398 } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
1399 char *data;
1400 int len;
1401
1402 get_clip((void **) &data, &len);
1403 if (data) {
1404 char *p, *q;
1405 p = q = data;
1406 while (p < data+len) {
1407 while (p < data+len &&
1408 !(p <= data+len-sizeof(sel_nl) &&
1409 !memcmp(p, sel_nl, sizeof(sel_nl))))
1410 p++;
1411
1412 {
1413 int i;
1414 unsigned char c;
1415 for(i=0;i<p-q;i++)
1416 {
1417 c=xlat_kbd2tty(q[i]);
1418 ldisc->send(&c,1);
1419 }
1420 }
1421
1422 if (p <= data+len-sizeof(sel_nl) &&
1423 !memcmp(p, sel_nl, sizeof(sel_nl))) {
1424 ldisc->send ("\r", 1);
1425 p += sizeof(sel_nl);
1426 }
1427 q = p;
1428 }
1429 }
1430 get_clip(NULL, NULL);
1431 }
1432
1433 term_update();
1434 }
1435
1436 static void deselect (void) {
1437 selstate = NO_SELECTION;
1438 selstart = selend = scrtop;
1439 }
1440
1441 void term_deselect (void) {
1442 deselect();
1443 term_update();
1444 }