| 1 | #include <windows.h> |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | |
| 6 | #include "putty.h" |
| 7 | |
| 8 | #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */ |
| 9 | #define CL_VT100 0x0002 /* VT100 */ |
| 10 | #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */ |
| 11 | #define CL_VT102 0x0008 /* VT102 */ |
| 12 | #define CL_VT220 0x0010 /* VT220 */ |
| 13 | #define CL_VT320 0x0020 /* VT320 */ |
| 14 | #define CL_VT420 0x0040 /* VT420 */ |
| 15 | #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */ |
| 16 | #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */ |
| 17 | #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */ |
| 18 | #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */ |
| 19 | #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */ |
| 20 | |
| 21 | #define TM_ANSIMIN (CL_ANSIMIN) |
| 22 | #define TM_VT100 (CL_ANSIMIN|CL_VT100) |
| 23 | #define TM_VT100AVO (TM_VT100|CL_VT100AVO) |
| 24 | #define TM_VT102 (TM_VT100AVO|CL_VT102) |
| 25 | #define TM_VT220 (TM_VT102|CL_VT220) |
| 26 | #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320) |
| 27 | #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI) |
| 28 | |
| 29 | #define TM_PUTTY (0xFFFF) |
| 30 | |
| 31 | #define compatibility(x) \ |
| 32 | if ( ((CL_##x)&compatibility_level) == 0 ) { \ |
| 33 | termstate=TOPLEVEL; \ |
| 34 | break; \ |
| 35 | } |
| 36 | #define compatibility2(x,y) \ |
| 37 | if ( ((CL_##x|CL_##y)&compatibility_level) == 0 ) { \ |
| 38 | termstate=TOPLEVEL; \ |
| 39 | break; \ |
| 40 | } |
| 41 | |
| 42 | #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 ) |
| 43 | |
| 44 | static int compatibility_level = TM_PUTTY; |
| 45 | |
| 46 | |
| 47 | static unsigned long *text; /* buffer of text on terminal screen */ |
| 48 | static unsigned long *scrtop; /* top of working screen */ |
| 49 | static unsigned long *disptop; /* top of displayed screen */ |
| 50 | static unsigned long *sbtop; /* top of scrollback */ |
| 51 | static unsigned long *sbbot; /* furthest extent of scrollback */ |
| 52 | static unsigned long *cpos; /* cursor position (convenience) */ |
| 53 | static unsigned long *disptext; /* buffer of text on real screen */ |
| 54 | static unsigned long *wanttext; /* buffer of text we want on screen */ |
| 55 | static unsigned long *alttext; /* buffer of text on alt. screen */ |
| 56 | |
| 57 | static unsigned char *selspace; /* buffer for building selections in */ |
| 58 | |
| 59 | #define TSIZE (sizeof(*text)) |
| 60 | #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0) |
| 61 | |
| 62 | static unsigned long curr_attr, save_attr; |
| 63 | static unsigned long erase_char = ERASE_CHAR; |
| 64 | |
| 65 | static int curs_x, curs_y; /* cursor */ |
| 66 | static int save_x, save_y; /* saved cursor position */ |
| 67 | static int marg_t, marg_b; /* scroll margins */ |
| 68 | static int dec_om; /* DEC origin mode flag */ |
| 69 | static int wrap, wrapnext; /* wrap flags */ |
| 70 | static int insert; /* insert-mode flag */ |
| 71 | static int cset; /* 0 or 1: which char set */ |
| 72 | static int save_cset, save_csattr; /* saved with cursor position */ |
| 73 | static int rvideo; /* global reverse video flag */ |
| 74 | static int cursor_on; /* cursor enabled flag */ |
| 75 | static int reset_132; /* Flag ESC c resets to 80 cols */ |
| 76 | static int use_bce; /* Use Background coloured erase */ |
| 77 | static int blinker; /* When blinking is the cursor on ? */ |
| 78 | static int tblinker; /* When the blinking text is on */ |
| 79 | static int blink_is_real; /* Actually blink blinking text */ |
| 80 | |
| 81 | static unsigned long cset_attr[2]; |
| 82 | |
| 83 | /* |
| 84 | * Saved settings on the alternate screen. |
| 85 | */ |
| 86 | static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset; |
| 87 | static int alt_t, alt_b; |
| 88 | static int alt_which; |
| 89 | |
| 90 | #define ARGS_MAX 32 /* max # of esc sequence arguments */ |
| 91 | #define ARG_DEFAULT 0 /* if an arg isn't specified */ |
| 92 | #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) ) |
| 93 | static int esc_args[ARGS_MAX]; |
| 94 | static int esc_nargs; |
| 95 | static int esc_query; |
| 96 | #define ANSI(x,y) ((x)+((y)<<8)) |
| 97 | #define ANSI_QUE(x) ANSI(x,TRUE) |
| 98 | |
| 99 | #define OSC_STR_MAX 2048 |
| 100 | static int osc_strlen; |
| 101 | static char osc_string[OSC_STR_MAX+1]; |
| 102 | static int osc_w; |
| 103 | |
| 104 | static char id_string[1024] = "\033[?6c"; |
| 105 | |
| 106 | static unsigned char *tabs; |
| 107 | |
| 108 | static enum { |
| 109 | TOPLEVEL, |
| 110 | SEEN_ESC, |
| 111 | SEEN_CSI, |
| 112 | SEEN_OSC, |
| 113 | SEEN_OSC_W, |
| 114 | |
| 115 | DO_CTRLS, |
| 116 | |
| 117 | IGNORE_NEXT, |
| 118 | SET_GL, SET_GR, |
| 119 | SEEN_OSC_P, |
| 120 | OSC_STRING, OSC_MAYBE_ST, |
| 121 | SEEN_ESCHASH, |
| 122 | VT52_ESC, |
| 123 | VT52_Y1, |
| 124 | VT52_Y2 |
| 125 | } termstate; |
| 126 | |
| 127 | static enum { |
| 128 | NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED |
| 129 | } selstate; |
| 130 | static enum { |
| 131 | SM_CHAR, SM_WORD, SM_LINE |
| 132 | } selmode; |
| 133 | static unsigned long *selstart, *selend, *selanchor; |
| 134 | |
| 135 | static short wordness[256] = { |
| 136 | 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 */ |
| 137 | 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 */ |
| 138 | 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 */ |
| 139 | 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 */ |
| 140 | 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 */ |
| 141 | 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 */ |
| 142 | 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 */ |
| 143 | 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 */ |
| 144 | }; |
| 145 | |
| 146 | static unsigned char sel_nl[] = SEL_NL; |
| 147 | static char * paste_buffer = 0; |
| 148 | static int paste_len, paste_pos, paste_hold; |
| 149 | |
| 150 | /* |
| 151 | * Internal prototypes. |
| 152 | */ |
| 153 | static void do_paint (Context, int); |
| 154 | static void erase_lots (int, int, int); |
| 155 | static void swap_screen (int); |
| 156 | static void update_sbar (void); |
| 157 | static void deselect (void); |
| 158 | |
| 159 | /* |
| 160 | * Set up power-on settings for the terminal. |
| 161 | */ |
| 162 | static void power_on(void) { |
| 163 | curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0; |
| 164 | alt_t = marg_t = 0; |
| 165 | if (rows != -1) |
| 166 | alt_b = marg_b = rows - 1; |
| 167 | else |
| 168 | alt_b = marg_b = 0; |
| 169 | if (cols != -1) { |
| 170 | int i; |
| 171 | for (i = 0; i < cols; i++) |
| 172 | tabs[i] = (i % 8 == 0 ? TRUE : FALSE); |
| 173 | } |
| 174 | alt_om = dec_om = cfg.dec_om; |
| 175 | alt_wnext = wrapnext = alt_ins = insert = FALSE; |
| 176 | alt_wrap = wrap = cfg.wrap_mode; |
| 177 | alt_cset = cset = 0; |
| 178 | cset_attr[0] = cset_attr[1] = ATTR_ASCII; |
| 179 | rvideo = 0; |
| 180 | cursor_on = 1; |
| 181 | save_attr = curr_attr = ATTR_DEFAULT; |
| 182 | app_cursor_keys = cfg.app_cursor; |
| 183 | app_keypad_keys = cfg.app_keypad; |
| 184 | use_bce = cfg.bce; |
| 185 | blink_is_real = cfg.blinktext; |
| 186 | erase_char = ERASE_CHAR; |
| 187 | alt_which = 0; |
| 188 | { |
| 189 | int i; |
| 190 | for (i = 0; i < 256; i++) |
| 191 | wordness[i] = cfg.wordness[i]; |
| 192 | } |
| 193 | if (text) { |
| 194 | swap_screen (1); |
| 195 | erase_lots (FALSE, TRUE, TRUE); |
| 196 | swap_screen (0); |
| 197 | erase_lots (FALSE, TRUE, TRUE); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Force a screen update. |
| 203 | */ |
| 204 | void term_update(void) { |
| 205 | Context ctx; |
| 206 | ctx = get_ctx(); |
| 207 | if (ctx) { |
| 208 | if ( (seen_key_event && (cfg.scroll_on_key)) || |
| 209 | (seen_disp_event && (!cfg.scroll_on_key)) ) { |
| 210 | disptop = scrtop; |
| 211 | seen_disp_event = seen_key_event = 0; |
| 212 | } |
| 213 | do_paint (ctx, TRUE); |
| 214 | free_ctx (ctx); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Same as power_on(), but an external function. |
| 220 | */ |
| 221 | void term_pwron(void) { |
| 222 | power_on(); |
| 223 | fix_cpos; |
| 224 | disptop = scrtop; |
| 225 | deselect(); |
| 226 | term_update(); |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Clear the scrollback. |
| 231 | */ |
| 232 | void term_clrsb(void) { |
| 233 | disptop = sbtop = scrtop; |
| 234 | update_sbar(); |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Initialise the terminal. |
| 239 | */ |
| 240 | void term_init(void) { |
| 241 | text = sbtop = sbbot = scrtop = disptop = cpos = NULL; |
| 242 | disptext = wanttext = NULL; |
| 243 | tabs = NULL; |
| 244 | selspace = NULL; |
| 245 | deselect(); |
| 246 | rows = cols = -1; |
| 247 | power_on(); |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Set up the terminal for a given size. |
| 252 | */ |
| 253 | void term_size(int newrows, int newcols, int newsavelines) { |
| 254 | unsigned long *newtext, *newdisp, *newwant, *newalt; |
| 255 | int i, j, crows, ccols; |
| 256 | |
| 257 | int save_alt_which = alt_which; |
| 258 | |
| 259 | if (newrows == rows && newcols == cols && newsavelines == savelines) |
| 260 | return; /* nothing to do */ |
| 261 | |
| 262 | deselect(); |
| 263 | swap_screen(0); |
| 264 | |
| 265 | alt_t = marg_t = 0; |
| 266 | alt_b = marg_b = newrows - 1; |
| 267 | |
| 268 | newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE); |
| 269 | sbbot = newtext + newsavelines*(newcols+1); |
| 270 | for (i=0; i<(newrows+newsavelines)*(newcols+1); i++) |
| 271 | newtext[i] = erase_char; |
| 272 | if (rows != -1) { |
| 273 | crows = rows + (scrtop - sbtop) / (cols+1); |
| 274 | if (crows > newrows+newsavelines) |
| 275 | crows = newrows+newsavelines; |
| 276 | if (newrows>crows) |
| 277 | disptop = newtext; |
| 278 | else |
| 279 | disptop = newtext + (crows-newrows)*(newcols+1); |
| 280 | ccols = (cols < newcols ? cols : newcols); |
| 281 | for (i=0; i<crows; i++) { |
| 282 | int oldidx = (rows - crows + i) * (cols+1); |
| 283 | int newidx = (newrows - crows + i) * (newcols+1); |
| 284 | |
| 285 | for (j=0; j<ccols; j++) |
| 286 | disptop[newidx+j] = scrtop[oldidx+j]; |
| 287 | disptop[newidx+newcols] = |
| 288 | (cols == newcols ? scrtop[oldidx+cols] |
| 289 | : (scrtop[oldidx+cols]&LATTR_MODE)); |
| 290 | } |
| 291 | sbtop = newtext; |
| 292 | } else { |
| 293 | sbtop = disptop = newtext; |
| 294 | } |
| 295 | scrtop = disptop; |
| 296 | sfree (text); |
| 297 | text = newtext; |
| 298 | |
| 299 | newdisp = smalloc (newrows*(newcols+1)*TSIZE); |
| 300 | for (i=0; i<newrows*(newcols+1); i++) |
| 301 | newdisp[i] = ATTR_INVALID; |
| 302 | sfree (disptext); |
| 303 | disptext = newdisp; |
| 304 | |
| 305 | newwant = smalloc (newrows*(newcols+1)*TSIZE); |
| 306 | for (i=0; i<newrows*(newcols+1); i++) |
| 307 | newwant[i] = ATTR_INVALID; |
| 308 | sfree (wanttext); |
| 309 | wanttext = newwant; |
| 310 | |
| 311 | newalt = smalloc (newrows*(newcols+1)*TSIZE); |
| 312 | for (i=0; i<newrows*(newcols+1); i++) |
| 313 | newalt[i] = erase_char; |
| 314 | sfree (alttext); |
| 315 | alttext = newalt; |
| 316 | |
| 317 | sfree (selspace); |
| 318 | selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) ); |
| 319 | |
| 320 | tabs = srealloc (tabs, newcols*sizeof(*tabs)); |
| 321 | { |
| 322 | int i; |
| 323 | for (i = (cols > 0 ? cols : 0); i < newcols; i++) |
| 324 | tabs[i] = (i % 8 == 0 ? TRUE : FALSE); |
| 325 | } |
| 326 | |
| 327 | if (rows > 0) |
| 328 | curs_y += newrows - rows; |
| 329 | if (curs_y < 0) |
| 330 | curs_y = 0; |
| 331 | if (curs_y >= newrows) |
| 332 | curs_y = newrows-1; |
| 333 | if (curs_x >= newcols) |
| 334 | curs_x = newcols-1; |
| 335 | alt_x = alt_y = 0; |
| 336 | wrapnext = alt_wnext = FALSE; |
| 337 | |
| 338 | rows = newrows; |
| 339 | cols = newcols; |
| 340 | savelines = newsavelines; |
| 341 | fix_cpos; |
| 342 | |
| 343 | swap_screen(save_alt_which); |
| 344 | |
| 345 | update_sbar(); |
| 346 | term_update(); |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Swap screens. |
| 351 | */ |
| 352 | static void swap_screen (int which) { |
| 353 | int t; |
| 354 | unsigned long tt; |
| 355 | |
| 356 | if (which == alt_which) |
| 357 | return; |
| 358 | |
| 359 | alt_which = which; |
| 360 | |
| 361 | for (t=0; t<rows*(cols+1); t++) { |
| 362 | tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt; |
| 363 | } |
| 364 | |
| 365 | t = curs_x; curs_x = alt_x; alt_x = t; |
| 366 | t = curs_y; curs_y = alt_y; alt_y = t; |
| 367 | t = marg_t; marg_t = alt_t; alt_t = t; |
| 368 | t = marg_b; marg_b = alt_b; alt_b = t; |
| 369 | t = dec_om; dec_om = alt_om; alt_om = t; |
| 370 | t = wrap; wrap = alt_wrap; alt_wrap = t; |
| 371 | t = wrapnext; wrapnext = alt_wnext; alt_wnext = t; |
| 372 | t = insert; insert = alt_ins; alt_ins = t; |
| 373 | t = cset; cset = alt_cset; alt_cset = t; |
| 374 | |
| 375 | fix_cpos; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * Update the scroll bar. |
| 380 | */ |
| 381 | static void update_sbar(void) { |
| 382 | int min; |
| 383 | |
| 384 | min = (sbtop - text) / (cols+1); |
| 385 | set_sbar ((scrtop - text) / (cols+1) + rows - min, |
| 386 | (disptop - text) / (cols+1) - min, |
| 387 | rows); |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Check whether the region bounded by the two pointers intersects |
| 392 | * the scroll region, and de-select the on-screen selection if so. |
| 393 | */ |
| 394 | static void check_selection (unsigned long *from, unsigned long *to) { |
| 395 | if (from < selend && selstart < to) |
| 396 | deselect(); |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Scroll the screen. (`lines' is +ve for scrolling forward, -ve |
| 401 | * for backward.) `sb' is TRUE if the scrolling is permitted to |
| 402 | * affect the scrollback buffer. |
| 403 | */ |
| 404 | static void scroll (int topline, int botline, int lines, int sb) { |
| 405 | unsigned long *scroll_top; |
| 406 | unsigned long *newscr; |
| 407 | int scroll_size, size, i; |
| 408 | int scrtop_is_disptop = (scrtop==disptop); |
| 409 | static int recursive = 0; |
| 410 | |
| 411 | /* Only scroll more than the window if we're doing a 10% scroll */ |
| 412 | if (!recursive && lines > botline - topline + 1) |
| 413 | lines = botline - topline + 1; |
| 414 | |
| 415 | scroll_top = scrtop + topline*(cols+1); |
| 416 | size = (lines < 0 ? -lines : lines) * (cols+1); |
| 417 | scroll_size = (botline - topline + 1) * (cols+1) - size; |
| 418 | |
| 419 | if (lines > 0 && topline == 0 && alt_which == 0 && sb) { |
| 420 | /* |
| 421 | * Since we're going to scroll the top line and we're on the |
| 422 | * scrolling screen let's also effect the scrollback buffer. |
| 423 | * |
| 424 | * This is normally done by moving the position the screen |
| 425 | * painter reads from to reduce the amount of memory copying |
| 426 | * required. |
| 427 | */ |
| 428 | if (scroll_size >= 0 && !recursive) { |
| 429 | newscr = scrtop + lines * (cols+1); |
| 430 | |
| 431 | if (newscr > sbbot && botline == rows-1) { |
| 432 | /* We've hit the bottom of memory, so we have to do a |
| 433 | * physical scroll. But instead of just 1 line do it |
| 434 | * by 10% of the available memory. |
| 435 | * |
| 436 | * If the scroll region isn't the whole screen then we can't |
| 437 | * do this as it stands. We would need to recover the bottom |
| 438 | * of the screen from the scroll buffer after being sure that |
| 439 | * it doesn't get deleted. |
| 440 | */ |
| 441 | |
| 442 | i = (rows+savelines)/10; |
| 443 | |
| 444 | /* Make it simple and ensure safe recursion */ |
| 445 | if ( i<savelines-1) { |
| 446 | recursive ++; |
| 447 | scroll(topline, botline, i, sb); |
| 448 | recursive --; |
| 449 | |
| 450 | newscr = scrtop - i * (cols+1); |
| 451 | if (scrtop_is_disptop) disptop = newscr; |
| 452 | scrtop = newscr; |
| 453 | } |
| 454 | |
| 455 | newscr = scrtop + lines * (cols+1); |
| 456 | } |
| 457 | |
| 458 | if (newscr <= sbbot) { |
| 459 | if (scrtop_is_disptop) disptop = newscr; |
| 460 | scrtop = newscr; |
| 461 | |
| 462 | if (botline == rows-1 ) |
| 463 | for (i = 0; i < size; i++) |
| 464 | scrtop[i+scroll_size] = erase_char; |
| 465 | |
| 466 | update_sbar(); |
| 467 | fix_cpos; |
| 468 | |
| 469 | if (botline != rows-1) { |
| 470 | /* This fastscroll only works for full window scrolls. |
| 471 | * If the caller wanted a partial one we have to reverse |
| 472 | * scroll the bottom of the screen. |
| 473 | */ |
| 474 | scroll(botline-lines+1, rows-1, -lines, 0); |
| 475 | } |
| 476 | return ; |
| 477 | } |
| 478 | |
| 479 | /* If we can't scroll by memory remapping do it physically. |
| 480 | * But rather than expensivly doing the scroll buffer just |
| 481 | * scroll the screen. All it means is that sometimes we choose |
| 482 | * to not add lines from a scroll region to the scroll buffer. |
| 483 | */ |
| 484 | |
| 485 | if (savelines <= 400) { |
| 486 | sbtop -= lines * (cols+1); |
| 487 | if (sbtop < text) |
| 488 | sbtop = text; |
| 489 | scroll_size += scroll_top - sbtop; |
| 490 | scroll_top = sbtop; |
| 491 | |
| 492 | update_sbar(); |
| 493 | } |
| 494 | } else { |
| 495 | /* Ho hum, expensive scroll required. */ |
| 496 | |
| 497 | sbtop -= lines * (cols+1); |
| 498 | if (sbtop < text) |
| 499 | sbtop = text; |
| 500 | scroll_size += scroll_top - sbtop; |
| 501 | scroll_top = sbtop; |
| 502 | |
| 503 | update_sbar(); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | if (scroll_size < 0) { |
| 508 | size += scroll_size; |
| 509 | scroll_size = 0; |
| 510 | } |
| 511 | |
| 512 | if (lines > 0) { |
| 513 | if (scroll_size) |
| 514 | memmove (scroll_top, scroll_top + size, scroll_size*TSIZE); |
| 515 | for (i = 0; i < size; i++) |
| 516 | scroll_top[i+scroll_size] = erase_char; |
| 517 | if (selstart > scroll_top && |
| 518 | selstart < scroll_top + size + scroll_size) { |
| 519 | selstart -= size; |
| 520 | if (selstart < scroll_top) |
| 521 | selstart = scroll_top; |
| 522 | } |
| 523 | if (selend > scroll_top && |
| 524 | selend < scroll_top + size + scroll_size) { |
| 525 | selend -= size; |
| 526 | if (selend < scroll_top) |
| 527 | selend = scroll_top; |
| 528 | } |
| 529 | if (scrtop_is_disptop) |
| 530 | disptop = scrtop; |
| 531 | else |
| 532 | if (disptop > scroll_top && |
| 533 | disptop < scroll_top + size + scroll_size) { |
| 534 | disptop -= size; |
| 535 | if (disptop < scroll_top) |
| 536 | disptop = scroll_top; |
| 537 | } |
| 538 | } else { |
| 539 | if (scroll_size) |
| 540 | memmove (scroll_top + size, scroll_top, scroll_size*TSIZE); |
| 541 | for (i = 0; i < size; i++) |
| 542 | scroll_top[i] = erase_char; |
| 543 | if (selstart > scroll_top && |
| 544 | selstart < scroll_top + size + scroll_size) { |
| 545 | selstart += size; |
| 546 | if (selstart > scroll_top + size + scroll_size) |
| 547 | selstart = scroll_top + size + scroll_size; |
| 548 | } |
| 549 | if (selend > scroll_top && |
| 550 | selend < scroll_top + size + scroll_size) { |
| 551 | selend += size; |
| 552 | if (selend > scroll_top + size + scroll_size) |
| 553 | selend = scroll_top + size + scroll_size; |
| 554 | } |
| 555 | if (scrtop_is_disptop) |
| 556 | disptop = scrtop; |
| 557 | else if (disptop > scroll_top && |
| 558 | disptop < scroll_top + size + scroll_size) { |
| 559 | disptop += size; |
| 560 | if (disptop > scroll_top + size + scroll_size) |
| 561 | disptop = scroll_top + size + scroll_size; |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Move the cursor to a given position, clipping at boundaries. We |
| 568 | * may or may not want to clip at the scroll margin: marg_clip is 0 |
| 569 | * not to, 1 to disallow _passing_ the margins, and 2 to disallow |
| 570 | * even _being_ outside the margins. |
| 571 | */ |
| 572 | static void move (int x, int y, int marg_clip) { |
| 573 | if (x < 0) |
| 574 | x = 0; |
| 575 | if (x >= cols) |
| 576 | x = cols-1; |
| 577 | if (marg_clip) { |
| 578 | if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t) |
| 579 | y = marg_t; |
| 580 | if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b) |
| 581 | y = marg_b; |
| 582 | } |
| 583 | if (y < 0) |
| 584 | y = 0; |
| 585 | if (y >= rows) |
| 586 | y = rows-1; |
| 587 | curs_x = x; |
| 588 | curs_y = y; |
| 589 | fix_cpos; |
| 590 | wrapnext = FALSE; |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * Save or restore the cursor and SGR mode. |
| 595 | */ |
| 596 | static void save_cursor(int save) { |
| 597 | if (save) { |
| 598 | save_x = curs_x; |
| 599 | save_y = curs_y; |
| 600 | save_attr = curr_attr; |
| 601 | save_cset = cset; |
| 602 | save_csattr = cset_attr[cset]; |
| 603 | } else { |
| 604 | curs_x = save_x; |
| 605 | curs_y = save_y; |
| 606 | /* Make sure the window hasn't shrunk since the save */ |
| 607 | if (curs_x >= cols) curs_x = cols-1; |
| 608 | if (curs_y >= rows) curs_y = rows-1; |
| 609 | |
| 610 | curr_attr = save_attr; |
| 611 | cset = save_cset; |
| 612 | cset_attr[cset] = save_csattr; |
| 613 | fix_cpos; |
| 614 | if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK))); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Erase a large portion of the screen: the whole screen, or the |
| 620 | * whole line, or parts thereof. |
| 621 | */ |
| 622 | static void erase_lots (int line_only, int from_begin, int to_end) { |
| 623 | unsigned long *startpos, *endpos; |
| 624 | |
| 625 | if (line_only) { |
| 626 | startpos = cpos - curs_x; |
| 627 | endpos = startpos + cols; |
| 628 | /* I've removed the +1 so that the Wide screen stuff is not |
| 629 | * removed when it shouldn't be. |
| 630 | */ |
| 631 | } else { |
| 632 | startpos = scrtop; |
| 633 | endpos = startpos + rows * (cols+1); |
| 634 | } |
| 635 | if (!from_begin) |
| 636 | startpos = cpos; |
| 637 | if (!to_end) |
| 638 | endpos = cpos+1; |
| 639 | check_selection (startpos, endpos); |
| 640 | |
| 641 | /* Clear screen also forces a full window redraw, just in case. */ |
| 642 | if (startpos == scrtop && endpos == scrtop + rows * (cols+1)) |
| 643 | term_invalidate(); |
| 644 | |
| 645 | while (startpos < endpos) |
| 646 | *startpos++ = erase_char; |
| 647 | } |
| 648 | |
| 649 | /* |
| 650 | * Insert or delete characters within the current line. n is +ve if |
| 651 | * insertion is desired, and -ve for deletion. |
| 652 | */ |
| 653 | static void insch (int n) { |
| 654 | int dir = (n < 0 ? -1 : +1); |
| 655 | int m; |
| 656 | |
| 657 | n = (n < 0 ? -n : n); |
| 658 | if (n > cols - curs_x) |
| 659 | n = cols - curs_x; |
| 660 | m = cols - curs_x - n; |
| 661 | check_selection (cpos, cpos+n); |
| 662 | if (dir < 0) { |
| 663 | memmove (cpos, cpos+n, m*TSIZE); |
| 664 | while (n--) |
| 665 | cpos[m++] = erase_char; |
| 666 | } else { |
| 667 | memmove (cpos+n, cpos, m*TSIZE); |
| 668 | while (n--) |
| 669 | cpos[n] = erase_char; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | /* |
| 674 | * Toggle terminal mode `mode' to state `state'. (`query' indicates |
| 675 | * whether the mode is a DEC private one or a normal one.) |
| 676 | */ |
| 677 | static void toggle_mode (int mode, int query, int state) { |
| 678 | if (query) switch (mode) { |
| 679 | case 1: /* application cursor keys */ |
| 680 | app_cursor_keys = state; |
| 681 | break; |
| 682 | case 2: /* VT52 mode */ |
| 683 | vt52_mode = !state; |
| 684 | break; |
| 685 | case 3: /* 80/132 columns */ |
| 686 | deselect(); |
| 687 | request_resize (state ? 132 : 80, rows, 1); |
| 688 | reset_132 = state; |
| 689 | break; |
| 690 | case 5: /* reverse video */ |
| 691 | rvideo = state; |
| 692 | seen_disp_event = TRUE; |
| 693 | if (state) term_update(); |
| 694 | break; |
| 695 | case 6: /* DEC origin mode */ |
| 696 | dec_om = state; |
| 697 | break; |
| 698 | case 7: /* auto wrap */ |
| 699 | wrap = state; |
| 700 | break; |
| 701 | case 8: /* auto key repeat */ |
| 702 | repeat_off = !state; |
| 703 | break; |
| 704 | case 25: /* enable/disable cursor */ |
| 705 | compatibility(VT220); |
| 706 | cursor_on = state; |
| 707 | seen_disp_event = TRUE; |
| 708 | break; |
| 709 | case 47: /* alternate screen */ |
| 710 | compatibility(OTHER); |
| 711 | deselect(); |
| 712 | swap_screen (state); |
| 713 | disptop = scrtop; |
| 714 | break; |
| 715 | } else switch (mode) { |
| 716 | case 4: /* set insert mode */ |
| 717 | compatibility(VT102); |
| 718 | insert = state; |
| 719 | break; |
| 720 | case 12: /* set echo mode */ |
| 721 | /* |
| 722 | * This may be very good in smcup and rmcup (or smkx & rmkx) if you |
| 723 | * have a long RTT and the telnet client/daemon doesn't understand |
| 724 | * linemode. |
| 725 | * |
| 726 | * DONT send TS_RECHO/TS_LECHO; the telnet daemon tries to fix the |
| 727 | * tty and _really_ confuses some programs. |
| 728 | */ |
| 729 | compatibility(VT220); |
| 730 | ldisc = (state? &ldisc_simple : &ldisc_term); |
| 731 | break; |
| 732 | case 20: /* Return sends ... */ |
| 733 | cr_lf_return = state; |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Process an OSC sequence: set window title or icon name. |
| 740 | */ |
| 741 | static void do_osc(void) { |
| 742 | if (osc_w) { |
| 743 | while (osc_strlen--) |
| 744 | wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0]; |
| 745 | } else { |
| 746 | osc_string[osc_strlen] = '\0'; |
| 747 | switch (esc_args[0]) { |
| 748 | case 0: |
| 749 | case 1: |
| 750 | set_icon (osc_string); |
| 751 | if (esc_args[0] == 1) |
| 752 | break; |
| 753 | /* fall through: parameter 0 means set both */ |
| 754 | case 2: |
| 755 | case 21: |
| 756 | set_title (osc_string); |
| 757 | break; |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | /* |
| 763 | * Remove everything currently in `inbuf' and stick it up on the |
| 764 | * in-memory display. There's a big state machine in here to |
| 765 | * process escape sequences... |
| 766 | */ |
| 767 | void term_out(void) { |
| 768 | int c, inbuf_reap; |
| 769 | |
| 770 | static int beep_overload = 0; |
| 771 | int beep_count = 0; |
| 772 | |
| 773 | for(inbuf_reap = 0; inbuf_reap < inbuf_head; inbuf_reap++) |
| 774 | { |
| 775 | c = inbuf[inbuf_reap]; |
| 776 | |
| 777 | /* |
| 778 | * Optionally log the session traffic to a file. Useful for |
| 779 | * debugging and possibly also useful for actual logging. |
| 780 | */ |
| 781 | if (logfile) { |
| 782 | static FILE *fp = NULL; |
| 783 | if (!fp) fp = fopen(logfile, "wb"); |
| 784 | if (fp) fputc (c, fp); |
| 785 | } |
| 786 | /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even |
| 787 | * be able to display 8-bit characters, but I'll let that go 'cause |
| 788 | * of i18n. |
| 789 | */ |
| 790 | if( ( (c&0x60) == 0 || c == '\177') && |
| 791 | termstate < DO_CTRLS && |
| 792 | ( (c&0x80) == 0 || has_compat(VT220))) { |
| 793 | switch (c) { |
| 794 | case '\005': /* terminal type query */ |
| 795 | /* Strictly speaking this is VT100 but a VT100 defaults to |
| 796 | * no response. Other terminals respond at their option. |
| 797 | * |
| 798 | * Don't put a CR in the default string as this tends to |
| 799 | * upset some weird software. |
| 800 | * |
| 801 | * An xterm returns "xterm" (5 characters) |
| 802 | */ |
| 803 | compatibility(OTHER); |
| 804 | ldisc->send ("PuTTY", 5); |
| 805 | break; |
| 806 | case '\007': |
| 807 | beep_count++; |
| 808 | if(beep_count>6) beep_overload=1; |
| 809 | disptop = scrtop; |
| 810 | break; |
| 811 | case '\b': |
| 812 | if (curs_x == 0 && curs_y == 0) |
| 813 | ; |
| 814 | else if (curs_x == 0 && curs_y > 0) |
| 815 | curs_x = cols-1, curs_y--; |
| 816 | else if (wrapnext) |
| 817 | wrapnext = FALSE; |
| 818 | else |
| 819 | curs_x--; |
| 820 | fix_cpos; |
| 821 | seen_disp_event = TRUE; |
| 822 | break; |
| 823 | case '\016': |
| 824 | compatibility(VT100); |
| 825 | cset = 1; |
| 826 | break; |
| 827 | case '\017': |
| 828 | compatibility(VT100); |
| 829 | cset = 0; |
| 830 | break; |
| 831 | case '\033': |
| 832 | if (vt52_mode) |
| 833 | termstate = VT52_ESC; |
| 834 | else { |
| 835 | compatibility(ANSIMIN); |
| 836 | termstate = SEEN_ESC; |
| 837 | } |
| 838 | break; |
| 839 | case 0233: |
| 840 | compatibility(VT220); |
| 841 | termstate = SEEN_CSI; |
| 842 | esc_nargs = 1; |
| 843 | esc_args[0] = ARG_DEFAULT; |
| 844 | esc_query = FALSE; |
| 845 | break; |
| 846 | case 0235: |
| 847 | compatibility(VT220); |
| 848 | termstate = SEEN_OSC; |
| 849 | esc_args[0] = 0; |
| 850 | break; |
| 851 | case '\r': |
| 852 | curs_x = 0; |
| 853 | wrapnext = FALSE; |
| 854 | fix_cpos; |
| 855 | seen_disp_event = TRUE; |
| 856 | paste_hold = 0; |
| 857 | break; |
| 858 | case '\013': |
| 859 | case '\014': |
| 860 | compatibility(VT100); |
| 861 | case '\n': |
| 862 | if (curs_y == marg_b) |
| 863 | scroll (marg_t, marg_b, 1, TRUE); |
| 864 | else if (curs_y < rows-1) |
| 865 | curs_y++; |
| 866 | if (cfg.lfhascr) |
| 867 | curs_x = 0; |
| 868 | fix_cpos; |
| 869 | wrapnext = FALSE; |
| 870 | seen_disp_event = 1; |
| 871 | paste_hold = 0; |
| 872 | break; |
| 873 | case '\t': |
| 874 | { |
| 875 | unsigned long *old_cpos = cpos; |
| 876 | unsigned long *p = scrtop + curs_y * (cols+1) + cols; |
| 877 | |
| 878 | do { |
| 879 | curs_x++; |
| 880 | } while (curs_x < cols-1 && !tabs[curs_x]); |
| 881 | |
| 882 | if ((*p & LATTR_MODE) != LATTR_NORM) |
| 883 | { |
| 884 | if (curs_x >= cols/2) |
| 885 | curs_x = cols/2-1; |
| 886 | } |
| 887 | else |
| 888 | { |
| 889 | if (curs_x >= cols) |
| 890 | curs_x = cols-1; |
| 891 | } |
| 892 | |
| 893 | fix_cpos; |
| 894 | check_selection (old_cpos, cpos); |
| 895 | } |
| 896 | seen_disp_event = TRUE; |
| 897 | break; |
| 898 | } |
| 899 | } |
| 900 | else switch (termstate) { |
| 901 | case TOPLEVEL: |
| 902 | /* Only graphic characters get this far, ctrls are stripped above */ |
| 903 | if (wrapnext) { |
| 904 | cpos[1] |= ATTR_WRAPPED; |
| 905 | if (curs_y == marg_b) |
| 906 | scroll (marg_t, marg_b, 1, TRUE); |
| 907 | else if (curs_y < rows-1) |
| 908 | curs_y++; |
| 909 | curs_x = 0; |
| 910 | fix_cpos; |
| 911 | wrapnext = FALSE; |
| 912 | } |
| 913 | if (insert) |
| 914 | insch (1); |
| 915 | if (selstate != NO_SELECTION) |
| 916 | check_selection (cpos, cpos+1); |
| 917 | switch (cset_attr[cset]) { |
| 918 | /* Linedraw characters are different from 'ESC ( B' only |
| 919 | * for a small range, for ones outside that range make sure |
| 920 | * we use the same font as well as the same encoding. |
| 921 | */ |
| 922 | case ATTR_LINEDRW: |
| 923 | if (c<0x60 || c>0x7F) |
| 924 | *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr | |
| 925 | ATTR_ASCII; |
| 926 | else |
| 927 | *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW; |
| 928 | break; |
| 929 | default: |
| 930 | *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr | |
| 931 | (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII); |
| 932 | break; |
| 933 | } |
| 934 | curs_x++; |
| 935 | if (curs_x == cols) { |
| 936 | cpos--; |
| 937 | curs_x--; |
| 938 | wrapnext = wrap; |
| 939 | } |
| 940 | seen_disp_event = 1; |
| 941 | break; |
| 942 | |
| 943 | case IGNORE_NEXT: |
| 944 | termstate = TOPLEVEL; |
| 945 | break; |
| 946 | case OSC_MAYBE_ST: |
| 947 | /* |
| 948 | * This state is virtually identical to SEEN_ESC, with the |
| 949 | * exception that we have an OSC sequence in the pipeline, |
| 950 | * and _if_ we see a backslash, we process it. |
| 951 | */ |
| 952 | if (c == '\\') { |
| 953 | do_osc(); |
| 954 | termstate = TOPLEVEL; |
| 955 | break; |
| 956 | } |
| 957 | /* else fall through */ |
| 958 | case SEEN_ESC: |
| 959 | termstate = TOPLEVEL; |
| 960 | switch (c) { |
| 961 | case ' ': /* some weird sequence? */ |
| 962 | compatibility(VT220); |
| 963 | termstate = IGNORE_NEXT; |
| 964 | break; |
| 965 | case '[': /* enter CSI mode */ |
| 966 | termstate = SEEN_CSI; |
| 967 | esc_nargs = 1; |
| 968 | esc_args[0] = ARG_DEFAULT; |
| 969 | esc_query = FALSE; |
| 970 | break; |
| 971 | case ']': /* xterm escape sequences */ |
| 972 | /* Compatibility is nasty here, xterm, linux, decterm yuk! */ |
| 973 | compatibility(OTHER); |
| 974 | termstate = SEEN_OSC; |
| 975 | esc_args[0] = 0; |
| 976 | break; |
| 977 | case '(': /* should set GL */ |
| 978 | compatibility(VT100); |
| 979 | termstate = SET_GL; |
| 980 | break; |
| 981 | case ')': /* should set GR */ |
| 982 | compatibility(VT100); |
| 983 | termstate = SET_GR; |
| 984 | break; |
| 985 | case '7': /* save cursor */ |
| 986 | compatibility(VT100); |
| 987 | save_cursor (TRUE); |
| 988 | break; |
| 989 | case '8': /* restore cursor */ |
| 990 | compatibility(VT100); |
| 991 | save_cursor (FALSE); |
| 992 | seen_disp_event = TRUE; |
| 993 | break; |
| 994 | case '=': |
| 995 | compatibility(VT100); |
| 996 | app_keypad_keys = TRUE; |
| 997 | break; |
| 998 | case '>': |
| 999 | compatibility(VT100); |
| 1000 | app_keypad_keys = FALSE; |
| 1001 | break; |
| 1002 | case 'D': /* exactly equivalent to LF */ |
| 1003 | compatibility(VT100); |
| 1004 | if (curs_y == marg_b) |
| 1005 | scroll (marg_t, marg_b, 1, TRUE); |
| 1006 | else if (curs_y < rows-1) |
| 1007 | curs_y++; |
| 1008 | fix_cpos; |
| 1009 | wrapnext = FALSE; |
| 1010 | seen_disp_event = TRUE; |
| 1011 | break; |
| 1012 | case 'E': /* exactly equivalent to CR-LF */ |
| 1013 | compatibility(VT100); |
| 1014 | curs_x = 0; |
| 1015 | if (curs_y == marg_b) |
| 1016 | scroll (marg_t, marg_b, 1, TRUE); |
| 1017 | else if (curs_y < rows-1) |
| 1018 | curs_y++; |
| 1019 | fix_cpos; |
| 1020 | wrapnext = FALSE; |
| 1021 | seen_disp_event = TRUE; |
| 1022 | break; |
| 1023 | case 'M': /* reverse index - backwards LF */ |
| 1024 | compatibility(VT100); |
| 1025 | if (curs_y == marg_t) |
| 1026 | scroll (marg_t, marg_b, -1, TRUE); |
| 1027 | else if (curs_y > 0) |
| 1028 | curs_y--; |
| 1029 | fix_cpos; |
| 1030 | wrapnext = FALSE; |
| 1031 | seen_disp_event = TRUE; |
| 1032 | break; |
| 1033 | case 'Z': /* terminal type query */ |
| 1034 | compatibility(VT100); |
| 1035 | ldisc->send (id_string, strlen(id_string)); |
| 1036 | break; |
| 1037 | case 'c': /* restore power-on settings */ |
| 1038 | compatibility(VT100); |
| 1039 | power_on(); |
| 1040 | if (reset_132) { |
| 1041 | request_resize (80, rows, 1); |
| 1042 | reset_132 = 0; |
| 1043 | } |
| 1044 | fix_cpos; |
| 1045 | disptop = scrtop; |
| 1046 | seen_disp_event = TRUE; |
| 1047 | break; |
| 1048 | case '#': /* ESC # 8 fills screen with Es :-) */ |
| 1049 | compatibility(VT100); |
| 1050 | termstate = SEEN_ESCHASH; |
| 1051 | break; |
| 1052 | case 'H': /* set a tab */ |
| 1053 | compatibility(VT100); |
| 1054 | tabs[curs_x] = TRUE; |
| 1055 | break; |
| 1056 | } |
| 1057 | break; |
| 1058 | case SEEN_CSI: |
| 1059 | termstate = TOPLEVEL; /* default */ |
| 1060 | if( isdigit(c) ) |
| 1061 | { |
| 1062 | if (esc_nargs <= ARGS_MAX) { |
| 1063 | if (esc_args[esc_nargs-1] == ARG_DEFAULT) |
| 1064 | esc_args[esc_nargs-1] = 0; |
| 1065 | esc_args[esc_nargs-1] = |
| 1066 | 10 * esc_args[esc_nargs-1] + c - '0'; |
| 1067 | } |
| 1068 | termstate = SEEN_CSI; |
| 1069 | } |
| 1070 | else if( c == ';' ) |
| 1071 | { |
| 1072 | if (++esc_nargs <= ARGS_MAX) |
| 1073 | esc_args[esc_nargs-1] = ARG_DEFAULT; |
| 1074 | termstate = SEEN_CSI; |
| 1075 | } |
| 1076 | else if( c < '@' ) |
| 1077 | { |
| 1078 | if( esc_query ) esc_query = -1; |
| 1079 | else if( c == '?' ) esc_query = TRUE; |
| 1080 | else esc_query = c; |
| 1081 | termstate = SEEN_CSI; |
| 1082 | } |
| 1083 | else switch (ANSI(c,esc_query)) { |
| 1084 | case 'A': /* move up N lines */ |
| 1085 | move (curs_x, curs_y - def(esc_args[0], 1), 1); |
| 1086 | seen_disp_event = TRUE; |
| 1087 | break; |
| 1088 | case 'e': /* move down N lines */ |
| 1089 | compatibility(ANSI); |
| 1090 | case 'B': |
| 1091 | move (curs_x, curs_y + def(esc_args[0], 1), 1); |
| 1092 | seen_disp_event = TRUE; |
| 1093 | break; |
| 1094 | case 'a': /* move right N cols */ |
| 1095 | compatibility(ANSI); |
| 1096 | case 'C': |
| 1097 | move (curs_x + def(esc_args[0], 1), curs_y, 1); |
| 1098 | seen_disp_event = TRUE; |
| 1099 | break; |
| 1100 | case 'D': /* move left N cols */ |
| 1101 | move (curs_x - def(esc_args[0], 1), curs_y, 1); |
| 1102 | seen_disp_event = TRUE; |
| 1103 | break; |
| 1104 | case 'E': /* move down N lines and CR */ |
| 1105 | compatibility(ANSI); |
| 1106 | move (0, curs_y + def(esc_args[0], 1), 1); |
| 1107 | seen_disp_event = TRUE; |
| 1108 | break; |
| 1109 | case 'F': /* move up N lines and CR */ |
| 1110 | compatibility(ANSI); |
| 1111 | move (0, curs_y - def(esc_args[0], 1), 1); |
| 1112 | seen_disp_event = TRUE; |
| 1113 | break; |
| 1114 | case 'G': case '`': /* set horizontal posn */ |
| 1115 | compatibility(ANSI); |
| 1116 | move (def(esc_args[0], 1) - 1, curs_y, 0); |
| 1117 | seen_disp_event = TRUE; |
| 1118 | break; |
| 1119 | case 'd': /* set vertical posn */ |
| 1120 | compatibility(ANSI); |
| 1121 | move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1, |
| 1122 | (dec_om ? 2 : 0)); |
| 1123 | seen_disp_event = TRUE; |
| 1124 | break; |
| 1125 | case 'H': case 'f': /* set horz and vert posns at once */ |
| 1126 | if (esc_nargs < 2) |
| 1127 | esc_args[1] = ARG_DEFAULT; |
| 1128 | move (def(esc_args[1], 1) - 1, |
| 1129 | (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1, |
| 1130 | (dec_om ? 2 : 0)); |
| 1131 | seen_disp_event = TRUE; |
| 1132 | break; |
| 1133 | case 'J': /* erase screen or parts of it */ |
| 1134 | { |
| 1135 | unsigned int i = def(esc_args[0], 0) + 1; |
| 1136 | if (i > 3) |
| 1137 | i = 0; |
| 1138 | erase_lots(FALSE, !!(i & 2), !!(i & 1)); |
| 1139 | } |
| 1140 | disptop = scrtop; |
| 1141 | seen_disp_event = TRUE; |
| 1142 | break; |
| 1143 | case 'K': /* erase line or parts of it */ |
| 1144 | { |
| 1145 | unsigned int i = def(esc_args[0], 0) + 1; |
| 1146 | if (i > 3) |
| 1147 | i = 0; |
| 1148 | erase_lots(TRUE, !!(i & 2), !!(i & 1)); |
| 1149 | } |
| 1150 | seen_disp_event = TRUE; |
| 1151 | break; |
| 1152 | case 'L': /* insert lines */ |
| 1153 | compatibility(VT102); |
| 1154 | if (curs_y <= marg_b) |
| 1155 | scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE); |
| 1156 | seen_disp_event = TRUE; |
| 1157 | break; |
| 1158 | case 'M': /* delete lines */ |
| 1159 | compatibility(VT102); |
| 1160 | if (curs_y <= marg_b) |
| 1161 | scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE); |
| 1162 | seen_disp_event = TRUE; |
| 1163 | break; |
| 1164 | case '@': /* insert chars */ |
| 1165 | /* XXX VTTEST says this is vt220, vt510 manual says vt102 */ |
| 1166 | compatibility(VT102); |
| 1167 | insch (def(esc_args[0], 1)); |
| 1168 | seen_disp_event = TRUE; |
| 1169 | break; |
| 1170 | case 'P': /* delete chars */ |
| 1171 | compatibility(VT102); |
| 1172 | insch (-def(esc_args[0], 1)); |
| 1173 | seen_disp_event = TRUE; |
| 1174 | break; |
| 1175 | case 'c': /* terminal type query */ |
| 1176 | compatibility(VT100); |
| 1177 | /* This is the response for a VT102 */ |
| 1178 | ldisc->send (id_string, strlen(id_string)); |
| 1179 | break; |
| 1180 | case 'n': /* cursor position query */ |
| 1181 | if (esc_args[0] == 6) { |
| 1182 | char buf[32]; |
| 1183 | sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1); |
| 1184 | ldisc->send (buf, strlen(buf)); |
| 1185 | } |
| 1186 | else if (esc_args[0] == 5) { |
| 1187 | ldisc->send ("\033[0n", 4); |
| 1188 | } |
| 1189 | break; |
| 1190 | case 'h': /* toggle modes to high */ |
| 1191 | case ANSI_QUE('h'): |
| 1192 | compatibility(VT100); |
| 1193 | { |
| 1194 | int i; |
| 1195 | for (i=0; i<esc_nargs; i++) |
| 1196 | toggle_mode (esc_args[i], esc_query, TRUE); |
| 1197 | } |
| 1198 | break; |
| 1199 | case 'l': /* toggle modes to low */ |
| 1200 | case ANSI_QUE('l'): |
| 1201 | compatibility(VT100); |
| 1202 | { |
| 1203 | int i; |
| 1204 | for (i=0; i<esc_nargs; i++) |
| 1205 | toggle_mode (esc_args[i], esc_query, FALSE); |
| 1206 | } |
| 1207 | break; |
| 1208 | case 'g': /* clear tabs */ |
| 1209 | compatibility(VT100); |
| 1210 | if (esc_nargs == 1) { |
| 1211 | if (esc_args[0] == 0) { |
| 1212 | tabs[curs_x] = FALSE; |
| 1213 | } else if (esc_args[0] == 3) { |
| 1214 | int i; |
| 1215 | for (i = 0; i < cols; i++) |
| 1216 | tabs[i] = FALSE; |
| 1217 | } |
| 1218 | } |
| 1219 | break; |
| 1220 | case 'r': /* set scroll margins */ |
| 1221 | compatibility(VT100); |
| 1222 | if (esc_nargs <= 2) { |
| 1223 | int top, bot; |
| 1224 | /* VTTEST Bug 9 if the first arg is default _or_ zero |
| 1225 | * this is a full screen region irrespective of 2nd arg. |
| 1226 | */ |
| 1227 | if (esc_args[0] <= 0) { |
| 1228 | top=0; |
| 1229 | bot=rows-1; |
| 1230 | } else { |
| 1231 | top = def(esc_args[0], 1) - 1; |
| 1232 | bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows : |
| 1233 | def(esc_args[1], rows)) - 1; |
| 1234 | } |
| 1235 | if (bot >= rows) |
| 1236 | bot = rows-1; |
| 1237 | if (top <= bot) { |
| 1238 | marg_t = top; |
| 1239 | marg_b = bot; |
| 1240 | curs_x = 0; |
| 1241 | /* |
| 1242 | * I used to think the cursor should be |
| 1243 | * placed at the top of the newly marginned |
| 1244 | * area. Apparently not: VMS TPU falls over |
| 1245 | * if so. |
| 1246 | * |
| 1247 | * Well actually it should for Origin mode - RDB |
| 1248 | */ |
| 1249 | curs_y = (dec_om ? marg_t : 0); |
| 1250 | fix_cpos; |
| 1251 | seen_disp_event = TRUE; |
| 1252 | } |
| 1253 | } |
| 1254 | break; |
| 1255 | case 'm': /* set graphics rendition */ |
| 1256 | { |
| 1257 | /* |
| 1258 | * A VT100 without the AVO only had one attribute, either |
| 1259 | * underline or reverse video depending on the cursor type, |
| 1260 | * this was selected by CSI 7m. |
| 1261 | * |
| 1262 | * case 2: |
| 1263 | * This is DIM on the VT100-AVO and VT102 |
| 1264 | * case 5: |
| 1265 | * This is BLINK on the VT100-AVO and VT102+ |
| 1266 | * case 8: |
| 1267 | * This is INVIS on the VT100-AVO and VT102 |
| 1268 | * case 21: |
| 1269 | * This like 22 disables BOLD, DIM and INVIS |
| 1270 | * |
| 1271 | * The ANSI colours appear on any terminal that has colour |
| 1272 | * (obviously) but the interaction between sgr0 and the |
| 1273 | * colours varies but is usually related to the background |
| 1274 | * colour erase item. |
| 1275 | * The interaction between colour attributes and the mono |
| 1276 | * ones is also very implementation dependent. |
| 1277 | * |
| 1278 | * The 39 and 49 attributes are likely to be unimplemented. |
| 1279 | */ |
| 1280 | int i; |
| 1281 | for (i=0; i<esc_nargs; i++) { |
| 1282 | switch (def(esc_args[i], 0)) { |
| 1283 | case 0: /* restore defaults */ |
| 1284 | curr_attr = ATTR_DEFAULT; break; |
| 1285 | case 1: /* enable bold */ |
| 1286 | compatibility(VT100AVO); |
| 1287 | curr_attr |= ATTR_BOLD; break; |
| 1288 | case 21: /* (enable double underline) */ |
| 1289 | compatibility(OTHER); |
| 1290 | case 4: /* enable underline */ |
| 1291 | compatibility(VT100AVO); |
| 1292 | curr_attr |= ATTR_UNDER; break; |
| 1293 | case 5: /* enable blink */ |
| 1294 | compatibility(VT100AVO); |
| 1295 | curr_attr |= ATTR_BLINK; break; |
| 1296 | case 7: /* enable reverse video */ |
| 1297 | curr_attr |= ATTR_REVERSE; break; |
| 1298 | case 22: /* disable bold */ |
| 1299 | compatibility(VT220); |
| 1300 | curr_attr &= ~ATTR_BOLD; break; |
| 1301 | case 24: /* disable underline */ |
| 1302 | compatibility(VT220); |
| 1303 | curr_attr &= ~ATTR_UNDER; break; |
| 1304 | case 25: /* disable blink */ |
| 1305 | compatibility(VT220); |
| 1306 | curr_attr &= ~ATTR_BLINK; break; |
| 1307 | case 27: /* disable reverse video */ |
| 1308 | compatibility(VT220); |
| 1309 | curr_attr &= ~ATTR_REVERSE; break; |
| 1310 | case 30: case 31: case 32: case 33: |
| 1311 | case 34: case 35: case 36: case 37: |
| 1312 | /* foreground */ |
| 1313 | curr_attr &= ~ATTR_FGMASK; |
| 1314 | curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT; |
| 1315 | break; |
| 1316 | case 39: /* default-foreground */ |
| 1317 | curr_attr &= ~ATTR_FGMASK; |
| 1318 | curr_attr |= ATTR_DEFFG; |
| 1319 | break; |
| 1320 | case 40: case 41: case 42: case 43: |
| 1321 | case 44: case 45: case 46: case 47: |
| 1322 | /* background */ |
| 1323 | curr_attr &= ~ATTR_BGMASK; |
| 1324 | curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT; |
| 1325 | break; |
| 1326 | case 49: /* default-background */ |
| 1327 | curr_attr &= ~ATTR_BGMASK; |
| 1328 | curr_attr |= ATTR_DEFBG; |
| 1329 | break; |
| 1330 | } |
| 1331 | } |
| 1332 | if (use_bce) |
| 1333 | erase_char = |
| 1334 | (' '| |
| 1335 | (curr_attr&(ATTR_FGMASK|ATTR_BGMASK|ATTR_BLINK)) |
| 1336 | ); |
| 1337 | } |
| 1338 | break; |
| 1339 | case 's': /* save cursor */ |
| 1340 | save_cursor (TRUE); |
| 1341 | break; |
| 1342 | case 'u': /* restore cursor */ |
| 1343 | save_cursor (FALSE); |
| 1344 | seen_disp_event = TRUE; |
| 1345 | break; |
| 1346 | case 't': /* set page size - ie window height */ |
| 1347 | /* |
| 1348 | * VT340/VT420 sequence DECSLPP, DEC only allows values |
| 1349 | * 24/25/36/48/72/144 other emulators (eg dtterm) use |
| 1350 | * illegal values (eg first arg 1..9) for window changing |
| 1351 | * and reports. |
| 1352 | */ |
| 1353 | compatibility(VT340TEXT); |
| 1354 | if (esc_nargs<=1 && (esc_args[0]<1 || esc_args[0]>=24)) { |
| 1355 | request_resize (cols, def(esc_args[0], 24), 0); |
| 1356 | deselect(); |
| 1357 | } |
| 1358 | break; |
| 1359 | case ANSI('|', '*'): |
| 1360 | /* VT420 sequence DECSNLS |
| 1361 | * Set number of lines on screen |
| 1362 | * VT420 uses VGA like hardware and can support any size in |
| 1363 | * reasonable range (24..49 AIUI) with no default specified. |
| 1364 | */ |
| 1365 | compatibility(VT420); |
| 1366 | if (esc_nargs==1 && esc_args[0]>0) { |
| 1367 | request_resize (cols, def(esc_args[0], cfg.height), 0); |
| 1368 | deselect(); |
| 1369 | } |
| 1370 | break; |
| 1371 | case ANSI('|', '$'): |
| 1372 | /* VT340/VT420 sequence DECSCPP |
| 1373 | * Set number of columns per page |
| 1374 | * Docs imply range is only 80 or 132, but I'll allow any. |
| 1375 | */ |
| 1376 | compatibility(VT340TEXT); |
| 1377 | if (esc_nargs<=1) { |
| 1378 | request_resize (def(esc_args[0], cfg.width), rows, 0); |
| 1379 | deselect(); |
| 1380 | } |
| 1381 | break; |
| 1382 | case 'X': /* write N spaces w/o moving cursor */ |
| 1383 | /* XXX VTTEST says this is vt220, vt510 manual says vt100 */ |
| 1384 | compatibility(ANSIMIN); |
| 1385 | { |
| 1386 | int n = def(esc_args[0], 1); |
| 1387 | unsigned long *p = cpos; |
| 1388 | if (n > cols - curs_x) |
| 1389 | n = cols - curs_x; |
| 1390 | check_selection (cpos, cpos+n); |
| 1391 | while (n--) |
| 1392 | *p++ = erase_char; |
| 1393 | seen_disp_event = TRUE; |
| 1394 | } |
| 1395 | break; |
| 1396 | case 'x': /* report terminal characteristics */ |
| 1397 | compatibility(VT100); |
| 1398 | { |
| 1399 | char buf[32]; |
| 1400 | int i = def(esc_args[0], 0); |
| 1401 | if (i == 0 || i == 1) { |
| 1402 | strcpy (buf, "\033[2;1;1;112;112;1;0x"); |
| 1403 | buf[2] += i; |
| 1404 | ldisc->send (buf, 20); |
| 1405 | } |
| 1406 | } |
| 1407 | break; |
| 1408 | case ANSI('L','='): |
| 1409 | compatibility(OTHER); |
| 1410 | use_bce = (esc_args[0]<=0); |
| 1411 | erase_char = ERASE_CHAR; |
| 1412 | if (use_bce) |
| 1413 | erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK))); |
| 1414 | break; |
| 1415 | case ANSI('E','='): |
| 1416 | compatibility(OTHER); |
| 1417 | blink_is_real = (esc_args[0]>=1); |
| 1418 | break; |
| 1419 | case ANSI('p','"'): |
| 1420 | /* Allow the host to make this emulator a 'perfect' VT102. |
| 1421 | * This first appeared in the VT220, but we do need to get |
| 1422 | * back to PuTTY mode so I won't check it. |
| 1423 | * |
| 1424 | * The arg == 60 is a PuTTY extension. |
| 1425 | * The 2nd arg, 8bit vs 7bit is not obeyed. |
| 1426 | * |
| 1427 | * Setting VT102 mode should also change the Fkeys to |
| 1428 | * generate PF* codes as a real VT102 has no Fkeys. |
| 1429 | * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys |
| 1430 | * send nothing. |
| 1431 | * |
| 1432 | * Note ESC c will NOT change this! |
| 1433 | */ |
| 1434 | |
| 1435 | if (esc_args[0] == 61) compatibility_level = TM_VT102; |
| 1436 | else if (esc_args[0] == 60) compatibility_level = TM_ANSIMIN; |
| 1437 | else compatibility_level = TM_PUTTY; |
| 1438 | break; |
| 1439 | } |
| 1440 | break; |
| 1441 | case SET_GL: |
| 1442 | case SET_GR: |
| 1443 | /* VT100 only here, checked above */ |
| 1444 | switch (c) { |
| 1445 | case 'A': |
| 1446 | cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR; |
| 1447 | break; |
| 1448 | case '0': |
| 1449 | cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW; |
| 1450 | break; |
| 1451 | case 'B': |
| 1452 | default: /* specifically, 'B' */ |
| 1453 | cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII; |
| 1454 | break; |
| 1455 | } |
| 1456 | if( !has_compat(VT220) || c != '%' ) |
| 1457 | termstate = TOPLEVEL; |
| 1458 | break; |
| 1459 | case SEEN_OSC: |
| 1460 | osc_w = FALSE; |
| 1461 | switch (c) { |
| 1462 | case 'P': /* Linux palette sequence */ |
| 1463 | termstate = SEEN_OSC_P; |
| 1464 | osc_strlen = 0; |
| 1465 | break; |
| 1466 | case 'R': /* Linux palette reset */ |
| 1467 | palette_reset(); |
| 1468 | term_invalidate(); |
| 1469 | termstate = TOPLEVEL; |
| 1470 | break; |
| 1471 | case 'W': /* word-set */ |
| 1472 | termstate = SEEN_OSC_W; |
| 1473 | osc_w = TRUE; |
| 1474 | break; |
| 1475 | case '0': case '1': case '2': case '3': case '4': |
| 1476 | case '5': case '6': case '7': case '8': case '9': |
| 1477 | esc_args[0] = 10 * esc_args[0] + c - '0'; |
| 1478 | break; |
| 1479 | case 'L': |
| 1480 | /* |
| 1481 | * Grotty hack to support xterm and DECterm title |
| 1482 | * sequences concurrently. |
| 1483 | */ |
| 1484 | if (esc_args[0] == 2) { |
| 1485 | esc_args[0] = 1; |
| 1486 | break; |
| 1487 | } |
| 1488 | /* else fall through */ |
| 1489 | default: |
| 1490 | termstate = OSC_STRING; |
| 1491 | osc_strlen = 0; |
| 1492 | } |
| 1493 | break; |
| 1494 | case OSC_STRING: |
| 1495 | /* |
| 1496 | * This OSC stuff is EVIL. It takes just one character to get into |
| 1497 | * sysline mode and it's not initially obvious how to get out. |
| 1498 | * So I've added CR and LF as string aborts. |
| 1499 | * This shouldn't effect compatibility as I believe embedded |
| 1500 | * control characters are supposed to be interpreted (maybe?) |
| 1501 | * and they don't display anything useful anyway. |
| 1502 | * |
| 1503 | * -- RDB |
| 1504 | */ |
| 1505 | if (c == '\n' || c == '\r') { |
| 1506 | termstate = TOPLEVEL; |
| 1507 | } else if (c == 0234 || c == '\007' ) { |
| 1508 | /* |
| 1509 | * These characters terminate the string; ST and BEL |
| 1510 | * terminate the sequence and trigger instant |
| 1511 | * processing of it, whereas ESC goes back to SEEN_ESC |
| 1512 | * mode unless it is followed by \, in which case it is |
| 1513 | * synonymous with ST in the first place. |
| 1514 | */ |
| 1515 | do_osc(); |
| 1516 | termstate = TOPLEVEL; |
| 1517 | } else if (c == '\033') |
| 1518 | termstate = OSC_MAYBE_ST; |
| 1519 | else if (osc_strlen < OSC_STR_MAX) |
| 1520 | osc_string[osc_strlen++] = c; |
| 1521 | break; |
| 1522 | case SEEN_OSC_P: |
| 1523 | { |
| 1524 | int max = (osc_strlen == 0 ? 21 : 16); |
| 1525 | int val; |
| 1526 | if (c >= '0' && c <= '9') |
| 1527 | val = c - '0'; |
| 1528 | else if (c >= 'A' && c <= 'A'+max-10) |
| 1529 | val = c - 'A' + 10; |
| 1530 | else if (c >= 'a' && c <= 'a'+max-10) |
| 1531 | val = c - 'a' + 10; |
| 1532 | else |
| 1533 | termstate = TOPLEVEL; |
| 1534 | osc_string[osc_strlen++] = val; |
| 1535 | if (osc_strlen >= 7) { |
| 1536 | palette_set (osc_string[0], |
| 1537 | osc_string[1] * 16 + osc_string[2], |
| 1538 | osc_string[3] * 16 + osc_string[4], |
| 1539 | osc_string[5] * 16 + osc_string[6]); |
| 1540 | term_invalidate(); |
| 1541 | termstate = TOPLEVEL; |
| 1542 | } |
| 1543 | } |
| 1544 | break; |
| 1545 | case SEEN_OSC_W: |
| 1546 | switch (c) { |
| 1547 | case '0': case '1': case '2': case '3': case '4': |
| 1548 | case '5': case '6': case '7': case '8': case '9': |
| 1549 | esc_args[0] = 10 * esc_args[0] + c - '0'; |
| 1550 | break; |
| 1551 | default: |
| 1552 | termstate = OSC_STRING; |
| 1553 | osc_strlen = 0; |
| 1554 | } |
| 1555 | break; |
| 1556 | case SEEN_ESCHASH: |
| 1557 | { |
| 1558 | unsigned long *p; |
| 1559 | unsigned long nlattr; |
| 1560 | int n; |
| 1561 | |
| 1562 | switch (c) { |
| 1563 | case '8': |
| 1564 | p = scrtop; |
| 1565 | n = rows * (cols+1); |
| 1566 | while (n--) |
| 1567 | *p++ = ATTR_DEFAULT | 'E'; |
| 1568 | disptop = scrtop; |
| 1569 | seen_disp_event = TRUE; |
| 1570 | check_selection (scrtop, scrtop + rows * (cols+1)); |
| 1571 | break; |
| 1572 | |
| 1573 | case '3': nlattr = LATTR_TOP; if(0) { |
| 1574 | case '4': nlattr = LATTR_BOT; } if(0) { |
| 1575 | case '5': nlattr = LATTR_NORM; } if(0) { |
| 1576 | case '6': nlattr = LATTR_WIDE; } |
| 1577 | |
| 1578 | p = scrtop + curs_y * (cols+1) + cols; |
| 1579 | *p &= ~LATTR_MODE; |
| 1580 | *p |= nlattr; |
| 1581 | } |
| 1582 | } |
| 1583 | termstate = TOPLEVEL; |
| 1584 | break; |
| 1585 | case VT52_ESC: |
| 1586 | termstate = TOPLEVEL; |
| 1587 | seen_disp_event = TRUE; |
| 1588 | switch (c) { |
| 1589 | case 'A': |
| 1590 | move (curs_x, curs_y - 1, 1); |
| 1591 | break; |
| 1592 | case 'B': |
| 1593 | move (curs_x, curs_y + 1, 1); |
| 1594 | break; |
| 1595 | case 'C': |
| 1596 | move (curs_x + 1, curs_y, 1); |
| 1597 | break; |
| 1598 | case 'D': |
| 1599 | move (curs_x - 1, curs_y, 1); |
| 1600 | break; |
| 1601 | case 'F': |
| 1602 | cset_attr[cset=0] = ATTR_LINEDRW; |
| 1603 | break; |
| 1604 | case 'G': |
| 1605 | cset_attr[cset=0] = ATTR_ASCII; |
| 1606 | break; |
| 1607 | case 'H': |
| 1608 | move (0, 0, 0); |
| 1609 | break; |
| 1610 | case 'I': |
| 1611 | if (curs_y == 0) |
| 1612 | scroll (0, rows-1, -1, TRUE); |
| 1613 | else if (curs_y > 0) |
| 1614 | curs_y--; |
| 1615 | fix_cpos; |
| 1616 | wrapnext = FALSE; |
| 1617 | break; |
| 1618 | case 'J': |
| 1619 | erase_lots(FALSE, FALSE, TRUE); |
| 1620 | disptop = scrtop; |
| 1621 | break; |
| 1622 | case 'K': |
| 1623 | erase_lots(TRUE, FALSE, TRUE); |
| 1624 | break; |
| 1625 | case 'V': |
| 1626 | /* XXX Print cursor line */ |
| 1627 | break; |
| 1628 | case 'W': |
| 1629 | /* XXX Start controller mode */ |
| 1630 | break; |
| 1631 | case 'X': |
| 1632 | /* XXX Stop controller mode */ |
| 1633 | break; |
| 1634 | case 'Y': |
| 1635 | termstate = VT52_Y1; |
| 1636 | break; |
| 1637 | case 'Z': |
| 1638 | ldisc->send ("\033/Z", 3); |
| 1639 | break; |
| 1640 | case '=': |
| 1641 | app_cursor_keys = TRUE; |
| 1642 | break; |
| 1643 | case '>': |
| 1644 | app_cursor_keys = FALSE; |
| 1645 | break; |
| 1646 | case '<': |
| 1647 | /* XXX This should switch to VT100 mode not current or default |
| 1648 | * VT mode. But this will only have effect in a VT220+ |
| 1649 | * emulation. |
| 1650 | */ |
| 1651 | vt52_mode = FALSE; |
| 1652 | break; |
| 1653 | case '^': |
| 1654 | /* XXX Enter auto print mode */ |
| 1655 | break; |
| 1656 | case '_': |
| 1657 | /* XXX Exit auto print mode */ |
| 1658 | break; |
| 1659 | case ']': |
| 1660 | /* XXX Print screen */ |
| 1661 | break; |
| 1662 | } |
| 1663 | break; |
| 1664 | case VT52_Y1: |
| 1665 | termstate = VT52_Y2; |
| 1666 | move(curs_x, c-' ', 0); |
| 1667 | break; |
| 1668 | case VT52_Y2: |
| 1669 | termstate = TOPLEVEL; |
| 1670 | move(c-' ', curs_y, 0); |
| 1671 | break; |
| 1672 | } |
| 1673 | if (selstate != NO_SELECTION) |
| 1674 | check_selection (cpos, cpos+1); |
| 1675 | } |
| 1676 | inbuf_head = 0; |
| 1677 | |
| 1678 | if (beep_overload) |
| 1679 | { |
| 1680 | if(!beep_count) beep_overload=0; |
| 1681 | } |
| 1682 | else if(beep_count && beep_count<5 && cfg.beep) |
| 1683 | beep(beep_count/3); |
| 1684 | } |
| 1685 | |
| 1686 | /* |
| 1687 | * Compare two lines to determine whether they are sufficiently |
| 1688 | * alike to scroll-optimise one to the other. Return the degree of |
| 1689 | * similarity. |
| 1690 | */ |
| 1691 | static int linecmp (unsigned long *a, unsigned long *b) { |
| 1692 | int i, n; |
| 1693 | |
| 1694 | for (i=n=0; i < cols; i++) |
| 1695 | n += (*a++ == *b++); |
| 1696 | return n; |
| 1697 | } |
| 1698 | |
| 1699 | /* |
| 1700 | * Given a context, update the window. Out of paranoia, we don't |
| 1701 | * allow WM_PAINT responses to do scrolling optimisations. |
| 1702 | */ |
| 1703 | static void do_paint (Context ctx, int may_optimise){ |
| 1704 | int i, j, start, our_curs_y; |
| 1705 | unsigned long attr, rv, cursor; |
| 1706 | char ch[1024]; |
| 1707 | |
| 1708 | if (cursor_on) { |
| 1709 | if (has_focus) { |
| 1710 | if (blinker || !cfg.blink_cur) |
| 1711 | cursor = ATTR_ACTCURS; |
| 1712 | else |
| 1713 | cursor = 0; |
| 1714 | } |
| 1715 | else |
| 1716 | cursor = ATTR_PASCURS; |
| 1717 | } |
| 1718 | else cursor = 0; |
| 1719 | rv = (rvideo ? ATTR_REVERSE : 0); |
| 1720 | our_curs_y = curs_y + (scrtop - disptop) / (cols+1); |
| 1721 | |
| 1722 | for (i=0; i<rows; i++) { |
| 1723 | int idx = i*(cols+1); |
| 1724 | int lattr = (disptop[idx+cols] & LATTR_MODE); |
| 1725 | for (j=0; j<=cols; j++,idx++) { |
| 1726 | unsigned long *d = disptop+idx; |
| 1727 | wanttext[idx] = lattr | ((*d ^ rv |
| 1728 | ^ (selstart <= d && d < selend ? |
| 1729 | ATTR_REVERSE : 0)) | |
| 1730 | (i==our_curs_y && j==curs_x ? cursor : 0)); |
| 1731 | |
| 1732 | if (blink_is_real) { |
| 1733 | if (has_focus && tblinker && (wanttext[idx]&ATTR_BLINK) ) |
| 1734 | { |
| 1735 | wanttext[idx] &= ATTR_MASK; |
| 1736 | wanttext[idx] += ' '; |
| 1737 | } |
| 1738 | wanttext[idx] &= ~ATTR_BLINK; |
| 1739 | } |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | /* |
| 1744 | * We would perform scrolling optimisations in here, if they |
| 1745 | * didn't have a nasty tendency to cause the whole sodding |
| 1746 | * program to hang for a second at speed-critical moments. |
| 1747 | * We'll leave it well alone... |
| 1748 | */ |
| 1749 | |
| 1750 | for (i=0; i<rows; i++) { |
| 1751 | int idx = i*(cols+1); |
| 1752 | int lattr = (wanttext[idx+cols] & LATTR_MODE); |
| 1753 | start = -1; |
| 1754 | for (j=0; j<=cols; j++,idx++) { |
| 1755 | unsigned long t = wanttext[idx]; |
| 1756 | int needs_update = (j < cols && t != disptext[idx]); |
| 1757 | int keep_going = (start != -1 && needs_update && |
| 1758 | (t & ATTR_MASK) == attr && |
| 1759 | j-start < sizeof(ch)); |
| 1760 | if (start != -1 && !keep_going) { |
| 1761 | do_text (ctx, start, i, ch, j-start, attr, lattr); |
| 1762 | start = -1; |
| 1763 | } |
| 1764 | if (needs_update) { |
| 1765 | if (start == -1) { |
| 1766 | start = j; |
| 1767 | attr = t & ATTR_MASK; |
| 1768 | } |
| 1769 | ch[j-start] = (char) (t & CHAR_MASK); |
| 1770 | } |
| 1771 | disptext[idx] = t; |
| 1772 | } |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | /* |
| 1777 | * Flick the switch that says if blinking things should be shown or hidden. |
| 1778 | */ |
| 1779 | |
| 1780 | void term_blink(int flg) { |
| 1781 | static long last_blink = 0; |
| 1782 | static long last_tblink = 0; |
| 1783 | long now, blink_diff; |
| 1784 | |
| 1785 | now = GetTickCount(); |
| 1786 | blink_diff = now-last_tblink; |
| 1787 | |
| 1788 | /* Make sure the text blinks no more than 2Hz */ |
| 1789 | if (blink_diff<0 || blink_diff>450) |
| 1790 | { |
| 1791 | last_tblink = now; |
| 1792 | tblinker = !tblinker; |
| 1793 | } |
| 1794 | |
| 1795 | if (flg) { |
| 1796 | blinker = 1; |
| 1797 | last_blink = now; |
| 1798 | return; |
| 1799 | } |
| 1800 | |
| 1801 | blink_diff = now-last_blink; |
| 1802 | |
| 1803 | /* Make sure the cursor blinks no more than 2Hz */ |
| 1804 | if (blink_diff>=0 && blink_diff<450) |
| 1805 | return; |
| 1806 | |
| 1807 | last_blink = now; |
| 1808 | blinker = !blinker; |
| 1809 | } |
| 1810 | |
| 1811 | /* |
| 1812 | * Invalidate the whole screen so it will be repainted in full. |
| 1813 | */ |
| 1814 | void term_invalidate(void) { |
| 1815 | int i; |
| 1816 | |
| 1817 | for (i=0; i<rows*(cols+1); i++) |
| 1818 | disptext[i] = ATTR_INVALID; |
| 1819 | } |
| 1820 | |
| 1821 | /* |
| 1822 | * Paint the window in response to a WM_PAINT message. |
| 1823 | */ |
| 1824 | void term_paint (Context ctx, int l, int t, int r, int b) { |
| 1825 | int i, j, left, top, right, bottom; |
| 1826 | |
| 1827 | left = l / font_width; |
| 1828 | right = (r - 1) / font_width; |
| 1829 | top = t / font_height; |
| 1830 | bottom = (b - 1) / font_height; |
| 1831 | for (i = top; i <= bottom && i < rows ; i++) |
| 1832 | { |
| 1833 | if ( (disptext[i*(cols+1)+cols]&LATTR_MODE) == LATTR_NORM) |
| 1834 | for (j = left; j <= right && j < cols ; j++) |
| 1835 | disptext[i*(cols+1)+j] = ATTR_INVALID; |
| 1836 | else |
| 1837 | for (j = left/2; j <= right/2+1 && j < cols ; j++) |
| 1838 | disptext[i*(cols+1)+j] = ATTR_INVALID; |
| 1839 | } |
| 1840 | |
| 1841 | /* This should happen soon enough, also for some reason it sometimes |
| 1842 | * fails to actually do anything when re-sizing ... painting the wrong |
| 1843 | * window perhaps ? |
| 1844 | do_paint (ctx, FALSE); |
| 1845 | */ |
| 1846 | } |
| 1847 | |
| 1848 | /* |
| 1849 | * Attempt to scroll the scrollback. The second parameter gives the |
| 1850 | * position we want to scroll to; the first is +1 to denote that |
| 1851 | * this position is relative to the beginning of the scrollback, -1 |
| 1852 | * to denote it is relative to the end, and 0 to denote that it is |
| 1853 | * relative to the current position. |
| 1854 | */ |
| 1855 | void term_scroll (int rel, int where) { |
| 1856 | int n = where * (cols+1); |
| 1857 | |
| 1858 | disptop = (rel < 0 ? scrtop : |
| 1859 | rel > 0 ? sbtop : disptop) + n; |
| 1860 | if (disptop < sbtop) |
| 1861 | disptop = sbtop; |
| 1862 | if (disptop > scrtop) |
| 1863 | disptop = scrtop; |
| 1864 | update_sbar(); |
| 1865 | term_update(); |
| 1866 | } |
| 1867 | |
| 1868 | /* |
| 1869 | * Spread the selection outwards according to the selection mode. |
| 1870 | */ |
| 1871 | static unsigned long *sel_spread_half (unsigned long *p, int dir) { |
| 1872 | unsigned long *linestart, *lineend; |
| 1873 | int x; |
| 1874 | short wvalue; |
| 1875 | |
| 1876 | x = (p - text) % (cols+1); |
| 1877 | linestart = p - x; |
| 1878 | lineend = linestart + cols; |
| 1879 | |
| 1880 | switch (selmode) { |
| 1881 | case SM_CHAR: |
| 1882 | /* |
| 1883 | * In this mode, every character is a separate unit, except |
| 1884 | * for runs of spaces at the end of a non-wrapping line. |
| 1885 | */ |
| 1886 | if (!(linestart[cols] & ATTR_WRAPPED)) { |
| 1887 | unsigned long *q = lineend; |
| 1888 | while (q > linestart && (q[-1] & CHAR_MASK) == 0x20) |
| 1889 | q--; |
| 1890 | if (q == lineend) |
| 1891 | q--; |
| 1892 | if (p >= q) |
| 1893 | p = (dir == -1 ? q : lineend - 1); |
| 1894 | } |
| 1895 | break; |
| 1896 | case SM_WORD: |
| 1897 | /* |
| 1898 | * In this mode, the units are maximal runs of characters |
| 1899 | * whose `wordness' has the same value. |
| 1900 | */ |
| 1901 | wvalue = wordness[*p & CHAR_MASK]; |
| 1902 | if (dir == +1) { |
| 1903 | while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue) |
| 1904 | p++; |
| 1905 | } else { |
| 1906 | while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue) |
| 1907 | p--; |
| 1908 | } |
| 1909 | break; |
| 1910 | case SM_LINE: |
| 1911 | /* |
| 1912 | * In this mode, every line is a unit. |
| 1913 | */ |
| 1914 | p = (dir == -1 ? linestart : lineend - 1); |
| 1915 | break; |
| 1916 | } |
| 1917 | return p; |
| 1918 | } |
| 1919 | |
| 1920 | static void sel_spread (void) { |
| 1921 | selstart = sel_spread_half (selstart, -1); |
| 1922 | selend = sel_spread_half (selend - 1, +1) + 1; |
| 1923 | } |
| 1924 | |
| 1925 | void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) { |
| 1926 | unsigned long *selpoint; |
| 1927 | |
| 1928 | if (y<0) y = 0; |
| 1929 | if (y>=rows) y = rows-1; |
| 1930 | if (x<0) { |
| 1931 | if (y > 0) { |
| 1932 | x = cols-1; |
| 1933 | y--; |
| 1934 | } else |
| 1935 | x = 0; |
| 1936 | } |
| 1937 | if (x>=cols) x = cols-1; |
| 1938 | |
| 1939 | selpoint = disptop + y * (cols+1); |
| 1940 | if ((selpoint[cols]&LATTR_MODE) != LATTR_NORM) |
| 1941 | selpoint += x/2; |
| 1942 | else |
| 1943 | selpoint += x; |
| 1944 | |
| 1945 | if (b == MB_SELECT && a == MA_CLICK) { |
| 1946 | deselect(); |
| 1947 | selstate = ABOUT_TO; |
| 1948 | selanchor = selpoint; |
| 1949 | selmode = SM_CHAR; |
| 1950 | } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) { |
| 1951 | deselect(); |
| 1952 | selmode = (a == MA_2CLK ? SM_WORD : SM_LINE); |
| 1953 | selstate = DRAGGING; |
| 1954 | selstart = selanchor = selpoint; |
| 1955 | selend = selstart + 1; |
| 1956 | sel_spread(); |
| 1957 | } else if ((b == MB_SELECT && a == MA_DRAG) || |
| 1958 | (b == MB_EXTEND && a != MA_RELEASE)) { |
| 1959 | if (selstate == ABOUT_TO && selanchor == selpoint) |
| 1960 | return; |
| 1961 | if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) { |
| 1962 | if (selpoint-selstart < (selend-selstart)/2) |
| 1963 | selanchor = selend - 1; |
| 1964 | else |
| 1965 | selanchor = selstart; |
| 1966 | selstate = DRAGGING; |
| 1967 | } |
| 1968 | if (selstate != ABOUT_TO && selstate != DRAGGING) |
| 1969 | selanchor = selpoint; |
| 1970 | selstate = DRAGGING; |
| 1971 | if (selpoint < selanchor) { |
| 1972 | selstart = selpoint; |
| 1973 | selend = selanchor + 1; |
| 1974 | } else { |
| 1975 | selstart = selanchor; |
| 1976 | selend = selpoint + 1; |
| 1977 | } |
| 1978 | sel_spread(); |
| 1979 | } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) { |
| 1980 | if (selstate == DRAGGING) { |
| 1981 | /* |
| 1982 | * We've completed a selection. We now transfer the |
| 1983 | * data to the clipboard. |
| 1984 | */ |
| 1985 | unsigned char *p = selspace; |
| 1986 | unsigned long *q = selstart; |
| 1987 | |
| 1988 | while (q < selend) { |
| 1989 | int nl = FALSE; |
| 1990 | unsigned long *lineend = q - (q-text) % (cols+1) + cols; |
| 1991 | unsigned long *nlpos = lineend; |
| 1992 | |
| 1993 | if (!(*nlpos & ATTR_WRAPPED)) { |
| 1994 | while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q) |
| 1995 | nlpos--; |
| 1996 | if (nlpos < selend) |
| 1997 | nl = TRUE; |
| 1998 | } |
| 1999 | while (q < nlpos && q < selend) |
| 2000 | *p++ = (unsigned char) (*q++ & CHAR_MASK); |
| 2001 | if (nl) { |
| 2002 | int i; |
| 2003 | for (i=0; i<sizeof(sel_nl); i++) |
| 2004 | *p++ = sel_nl[i]; |
| 2005 | } |
| 2006 | q = lineend + 1; /* start of next line */ |
| 2007 | } |
| 2008 | write_clip (selspace, p - selspace); |
| 2009 | selstate = SELECTED; |
| 2010 | } else |
| 2011 | selstate = NO_SELECTION; |
| 2012 | } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) { |
| 2013 | char *data; |
| 2014 | int len; |
| 2015 | |
| 2016 | get_clip((void **) &data, &len); |
| 2017 | if (data) { |
| 2018 | char *p, *q; |
| 2019 | |
| 2020 | if (paste_buffer) sfree(paste_buffer); |
| 2021 | paste_pos = paste_hold = paste_len = 0; |
| 2022 | paste_buffer = smalloc(len); |
| 2023 | |
| 2024 | p = q = data; |
| 2025 | while (p < data+len) { |
| 2026 | while (p < data+len && |
| 2027 | !(p <= data+len-sizeof(sel_nl) && |
| 2028 | !memcmp(p, sel_nl, sizeof(sel_nl)))) |
| 2029 | p++; |
| 2030 | |
| 2031 | { |
| 2032 | int i; |
| 2033 | unsigned char c; |
| 2034 | for(i=0;i<p-q;i++) |
| 2035 | { |
| 2036 | c=xlat_kbd2tty(q[i]); |
| 2037 | paste_buffer[paste_len++] = c; |
| 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | if (p <= data+len-sizeof(sel_nl) && |
| 2042 | !memcmp(p, sel_nl, sizeof(sel_nl))) { |
| 2043 | paste_buffer[paste_len++] = '\r'; |
| 2044 | p += sizeof(sel_nl); |
| 2045 | } |
| 2046 | q = p; |
| 2047 | } |
| 2048 | |
| 2049 | /* Assume a small paste will be OK in one go. */ |
| 2050 | if (paste_len<256) { |
| 2051 | ldisc->send (paste_buffer, paste_len); |
| 2052 | if (paste_buffer) sfree(paste_buffer); |
| 2053 | paste_buffer = 0; |
| 2054 | paste_pos = paste_hold = paste_len = 0; |
| 2055 | } |
| 2056 | } |
| 2057 | get_clip(NULL, NULL); |
| 2058 | } |
| 2059 | |
| 2060 | term_update(); |
| 2061 | } |
| 2062 | |
| 2063 | void term_nopaste() { |
| 2064 | if(paste_len == 0) return; |
| 2065 | sfree(paste_buffer); |
| 2066 | paste_buffer = 0; |
| 2067 | paste_len = 0; |
| 2068 | } |
| 2069 | |
| 2070 | void term_paste() { |
| 2071 | static long last_paste = 0; |
| 2072 | long now, paste_diff; |
| 2073 | |
| 2074 | if(paste_len == 0) return; |
| 2075 | |
| 2076 | /* Don't wait forever to paste */ |
| 2077 | if(paste_hold) { |
| 2078 | now = GetTickCount(); |
| 2079 | paste_diff = now-last_paste; |
| 2080 | if (paste_diff>=0 && paste_diff<450) |
| 2081 | return; |
| 2082 | } |
| 2083 | paste_hold = 0; |
| 2084 | |
| 2085 | while(paste_pos<paste_len) |
| 2086 | { |
| 2087 | char c = paste_buffer[paste_pos++]; |
| 2088 | ldisc->send (&c, 1); |
| 2089 | |
| 2090 | if (c =='\r') { |
| 2091 | paste_hold = 1; |
| 2092 | return; |
| 2093 | } |
| 2094 | } |
| 2095 | sfree(paste_buffer); |
| 2096 | paste_buffer = 0; |
| 2097 | paste_len = 0; |
| 2098 | } |
| 2099 | |
| 2100 | static void deselect (void) { |
| 2101 | selstate = NO_SELECTION; |
| 2102 | selstart = selend = scrtop; |
| 2103 | } |
| 2104 | |
| 2105 | void term_deselect (void) { |
| 2106 | deselect(); |
| 2107 | term_update(); |
| 2108 | } |