| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <ctype.h> |
| 4 | |
| 5 | #include <time.h> |
| 6 | #include <assert.h> |
| 7 | #include "putty.h" |
| 8 | #include "terminal.h" |
| 9 | |
| 10 | #define poslt(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x < (p2).x ) ) |
| 11 | #define posle(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x <= (p2).x ) ) |
| 12 | #define poseq(p1,p2) ( (p1).y == (p2).y && (p1).x == (p2).x ) |
| 13 | #define posdiff(p1,p2) ( ((p1).y - (p2).y) * (term->cols+1) + (p1).x - (p2).x ) |
| 14 | |
| 15 | /* Product-order comparisons for rectangular block selection. */ |
| 16 | #define posPlt(p1,p2) ( (p1).y <= (p2).y && (p1).x < (p2).x ) |
| 17 | #define posPle(p1,p2) ( (p1).y <= (p2).y && (p1).x <= (p2).x ) |
| 18 | |
| 19 | #define incpos(p) ( (p).x == term->cols ? ((p).x = 0, (p).y++, 1) : ((p).x++, 0) ) |
| 20 | #define decpos(p) ( (p).x == 0 ? ((p).x = term->cols, (p).y--, 1) : ((p).x--, 0) ) |
| 21 | |
| 22 | #define VT52_PLUS |
| 23 | |
| 24 | #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */ |
| 25 | #define CL_VT100 0x0002 /* VT100 */ |
| 26 | #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */ |
| 27 | #define CL_VT102 0x0008 /* VT102 */ |
| 28 | #define CL_VT220 0x0010 /* VT220 */ |
| 29 | #define CL_VT320 0x0020 /* VT320 */ |
| 30 | #define CL_VT420 0x0040 /* VT420 */ |
| 31 | #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */ |
| 32 | #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */ |
| 33 | #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */ |
| 34 | #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */ |
| 35 | #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */ |
| 36 | |
| 37 | #define TM_VT100 (CL_ANSIMIN|CL_VT100) |
| 38 | #define TM_VT100AVO (TM_VT100|CL_VT100AVO) |
| 39 | #define TM_VT102 (TM_VT100AVO|CL_VT102) |
| 40 | #define TM_VT220 (TM_VT102|CL_VT220) |
| 41 | #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320) |
| 42 | #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI) |
| 43 | |
| 44 | #define TM_PUTTY (0xFFFF) |
| 45 | |
| 46 | #define compatibility(x) \ |
| 47 | if ( ((CL_##x)&term->compatibility_level) == 0 ) { \ |
| 48 | term->termstate=TOPLEVEL; \ |
| 49 | break; \ |
| 50 | } |
| 51 | #define compatibility2(x,y) \ |
| 52 | if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \ |
| 53 | term->termstate=TOPLEVEL; \ |
| 54 | break; \ |
| 55 | } |
| 56 | |
| 57 | #define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 ) |
| 58 | |
| 59 | #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t)) |
| 60 | const wchar_t sel_nl[] = SEL_NL; |
| 61 | |
| 62 | /* |
| 63 | * Fetch the character at a particular position in a line array, |
| 64 | * for purposes of `wordtype'. The reason this isn't just a simple |
| 65 | * array reference is that if the character we find is UCSWIDE, |
| 66 | * then we must look one space further to the left. |
| 67 | */ |
| 68 | #define UCSGET(a, x) \ |
| 69 | ( (x)>0 && ((a)[(x)] & (CHAR_MASK | CSET_MASK)) == UCSWIDE ? \ |
| 70 | (a)[(x)-1] : (a)[(x)] ) |
| 71 | |
| 72 | /* |
| 73 | * Internal prototypes. |
| 74 | */ |
| 75 | static unsigned long *resizeline(unsigned long *, int); |
| 76 | static unsigned long *lineptr(Terminal *, int, int); |
| 77 | static void do_paint(Terminal *, Context, int); |
| 78 | static void erase_lots(Terminal *, int, int, int); |
| 79 | static void swap_screen(Terminal *, int, int, int); |
| 80 | static void update_sbar(Terminal *); |
| 81 | static void deselect(Terminal *); |
| 82 | static void term_print_finish(Terminal *); |
| 83 | #ifdef OPTIMISE_SCROLL |
| 84 | static void scroll_display(Terminal *, int, int, int); |
| 85 | #endif /* OPTIMISE_SCROLL */ |
| 86 | |
| 87 | /* |
| 88 | * Resize a line to make it `cols' columns wide. |
| 89 | */ |
| 90 | static unsigned long *resizeline(unsigned long *line, int cols) |
| 91 | { |
| 92 | int i, oldlen; |
| 93 | unsigned long lineattrs; |
| 94 | |
| 95 | if (line[0] != (unsigned long)cols) { |
| 96 | /* |
| 97 | * This line is the wrong length, which probably means it |
| 98 | * hasn't been accessed since a resize. Resize it now. |
| 99 | */ |
| 100 | oldlen = line[0]; |
| 101 | lineattrs = line[oldlen + 1]; |
| 102 | line = srealloc(line, TSIZE * (2 + cols)); |
| 103 | line[0] = cols; |
| 104 | for (i = oldlen; i < cols; i++) |
| 105 | line[i + 1] = ERASE_CHAR; |
| 106 | line[cols + 1] = lineattrs & LATTR_MODE; |
| 107 | } |
| 108 | |
| 109 | return line; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Get the number of lines in the scrollback. |
| 114 | */ |
| 115 | static int sblines(Terminal *term) |
| 116 | { |
| 117 | int sblines = count234(term->scrollback); |
| 118 | if (term->cfg.erase_to_scrollback && |
| 119 | term->alt_which && term->alt_screen) { |
| 120 | sblines += term->alt_sblines; |
| 121 | } |
| 122 | return sblines; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * Retrieve a line of the screen or of the scrollback, according to |
| 127 | * whether the y coordinate is non-negative or negative |
| 128 | * (respectively). |
| 129 | */ |
| 130 | static unsigned long *lineptr(Terminal *term, int y, int lineno) |
| 131 | { |
| 132 | unsigned long *line, *newline; |
| 133 | tree234 *whichtree; |
| 134 | int treeindex; |
| 135 | |
| 136 | if (y >= 0) { |
| 137 | whichtree = term->screen; |
| 138 | treeindex = y; |
| 139 | } else { |
| 140 | int altlines = 0; |
| 141 | if (term->cfg.erase_to_scrollback && |
| 142 | term->alt_which && term->alt_screen) { |
| 143 | altlines = term->alt_sblines; |
| 144 | } |
| 145 | if (y < -altlines) { |
| 146 | whichtree = term->scrollback; |
| 147 | treeindex = y + altlines + count234(term->scrollback); |
| 148 | } else { |
| 149 | whichtree = term->alt_screen; |
| 150 | treeindex = y + term->alt_sblines; |
| 151 | /* treeindex = y + count234(term->alt_screen); */ |
| 152 | } |
| 153 | } |
| 154 | line = index234(whichtree, treeindex); |
| 155 | |
| 156 | /* We assume that we don't screw up and retrieve something out of range. */ |
| 157 | assert(line != NULL); |
| 158 | |
| 159 | newline = resizeline(line, term->cols); |
| 160 | if (newline != line) { |
| 161 | delpos234(whichtree, treeindex); |
| 162 | addpos234(whichtree, newline, treeindex); |
| 163 | line = newline; |
| 164 | } |
| 165 | |
| 166 | return line + 1; |
| 167 | } |
| 168 | |
| 169 | #define lineptr(x) lineptr(term,x,__LINE__) |
| 170 | |
| 171 | /* |
| 172 | * Set up power-on settings for the terminal. |
| 173 | */ |
| 174 | static void power_on(Terminal *term) |
| 175 | { |
| 176 | term->curs.x = term->curs.y = 0; |
| 177 | term->alt_x = term->alt_y = 0; |
| 178 | term->savecurs.x = term->savecurs.y = 0; |
| 179 | term->alt_t = term->marg_t = 0; |
| 180 | if (term->rows != -1) |
| 181 | term->alt_b = term->marg_b = term->rows - 1; |
| 182 | else |
| 183 | term->alt_b = term->marg_b = 0; |
| 184 | if (term->cols != -1) { |
| 185 | int i; |
| 186 | for (i = 0; i < term->cols; i++) |
| 187 | term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE); |
| 188 | } |
| 189 | term->alt_om = term->dec_om = term->cfg.dec_om; |
| 190 | term->alt_ins = term->insert = FALSE; |
| 191 | term->alt_wnext = term->wrapnext = term->save_wnext = FALSE; |
| 192 | term->alt_wrap = term->wrap = term->cfg.wrap_mode; |
| 193 | term->alt_cset = term->cset = term->save_cset = 0; |
| 194 | term->alt_utf = term->utf = term->save_utf = 0; |
| 195 | term->utf_state = 0; |
| 196 | term->alt_sco_acs = term->sco_acs = term->save_sco_acs = 0; |
| 197 | term->cset_attr[0] = term->cset_attr[1] = term->save_csattr = ATTR_ASCII; |
| 198 | term->rvideo = 0; |
| 199 | term->in_vbell = FALSE; |
| 200 | term->cursor_on = 1; |
| 201 | term->big_cursor = 0; |
| 202 | term->save_attr = term->curr_attr = ATTR_DEFAULT; |
| 203 | term->term_editing = term->term_echoing = FALSE; |
| 204 | term->app_cursor_keys = term->cfg.app_cursor; |
| 205 | term->app_keypad_keys = term->cfg.app_keypad; |
| 206 | term->use_bce = term->cfg.bce; |
| 207 | term->blink_is_real = term->cfg.blinktext; |
| 208 | term->erase_char = ERASE_CHAR; |
| 209 | term->alt_which = 0; |
| 210 | term_print_finish(term); |
| 211 | { |
| 212 | int i; |
| 213 | for (i = 0; i < 256; i++) |
| 214 | term->wordness[i] = term->cfg.wordness[i]; |
| 215 | } |
| 216 | if (term->screen) { |
| 217 | swap_screen(term, 1, FALSE, FALSE); |
| 218 | erase_lots(term, FALSE, TRUE, TRUE); |
| 219 | swap_screen(term, 0, FALSE, FALSE); |
| 220 | erase_lots(term, FALSE, TRUE, TRUE); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Force a screen update. |
| 226 | */ |
| 227 | void term_update(Terminal *term) |
| 228 | { |
| 229 | Context ctx; |
| 230 | ctx = get_ctx(term->frontend); |
| 231 | if (ctx) { |
| 232 | int need_sbar_update = term->seen_disp_event; |
| 233 | if (term->seen_disp_event && term->cfg.scroll_on_disp) { |
| 234 | term->disptop = 0; /* return to main screen */ |
| 235 | term->seen_disp_event = 0; |
| 236 | need_sbar_update = TRUE; |
| 237 | } |
| 238 | if (need_sbar_update) |
| 239 | update_sbar(term); |
| 240 | do_paint(term, ctx, TRUE); |
| 241 | sys_cursor(term->frontend, term->curs.x, term->curs.y - term->disptop); |
| 242 | free_ctx(ctx); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Called from front end when a keypress occurs, to trigger |
| 248 | * anything magical that needs to happen in that situation. |
| 249 | */ |
| 250 | void term_seen_key_event(Terminal *term) |
| 251 | { |
| 252 | /* |
| 253 | * On any keypress, clear the bell overload mechanism |
| 254 | * completely, on the grounds that large numbers of |
| 255 | * beeps coming from deliberate key action are likely |
| 256 | * to be intended (e.g. beeps from filename completion |
| 257 | * blocking repeatedly). |
| 258 | */ |
| 259 | term->beep_overloaded = FALSE; |
| 260 | while (term->beephead) { |
| 261 | struct beeptime *tmp = term->beephead; |
| 262 | term->beephead = tmp->next; |
| 263 | sfree(tmp); |
| 264 | } |
| 265 | term->beeptail = NULL; |
| 266 | term->nbeeps = 0; |
| 267 | |
| 268 | /* |
| 269 | * Reset the scrollback on keypress, if we're doing that. |
| 270 | */ |
| 271 | if (term->cfg.scroll_on_key) { |
| 272 | term->disptop = 0; /* return to main screen */ |
| 273 | term->seen_disp_event = 1; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Same as power_on(), but an external function. |
| 279 | */ |
| 280 | void term_pwron(Terminal *term) |
| 281 | { |
| 282 | power_on(term); |
| 283 | if (term->ldisc) /* cause ldisc to notice changes */ |
| 284 | ldisc_send(term->ldisc, NULL, 0, 0); |
| 285 | fix_cpos; |
| 286 | term->disptop = 0; |
| 287 | deselect(term); |
| 288 | term_update(term); |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * When the user reconfigures us, we need to check the forbidden- |
| 293 | * alternate-screen config option, disable raw mouse mode if the |
| 294 | * user has disabled mouse reporting, and abandon a print job if |
| 295 | * the user has disabled printing. |
| 296 | */ |
| 297 | void term_reconfig(Terminal *term, Config *cfg) |
| 298 | { |
| 299 | /* |
| 300 | * Before adopting the new config, check all those terminal |
| 301 | * settings which control power-on defaults; and if they've |
| 302 | * changed, we will modify the current state as well as the |
| 303 | * default one. The full list is: Auto wrap mode, DEC Origin |
| 304 | * Mode, BCE, blinking text, character classes. |
| 305 | */ |
| 306 | int reset_wrap, reset_decom, reset_bce, reset_blink, reset_charclass; |
| 307 | int i; |
| 308 | |
| 309 | reset_wrap = (term->cfg.wrap_mode != cfg->wrap_mode); |
| 310 | reset_decom = (term->cfg.dec_om != cfg->dec_om); |
| 311 | reset_bce = (term->cfg.bce != cfg->bce); |
| 312 | reset_blink = (term->cfg.blinktext != cfg->blinktext); |
| 313 | reset_charclass = 0; |
| 314 | for (i = 0; i < lenof(term->cfg.wordness); i++) |
| 315 | if (term->cfg.wordness[i] != cfg->wordness[i]) |
| 316 | reset_charclass = 1; |
| 317 | |
| 318 | term->cfg = *cfg; /* STRUCTURE COPY */ |
| 319 | |
| 320 | if (reset_wrap) |
| 321 | term->alt_wrap = term->wrap = term->cfg.wrap_mode; |
| 322 | if (reset_decom) |
| 323 | term->alt_om = term->dec_om = term->cfg.dec_om; |
| 324 | if (reset_bce) |
| 325 | term->use_bce = term->cfg.bce; |
| 326 | if (reset_blink) |
| 327 | term->blink_is_real = term->cfg.blinktext; |
| 328 | if (reset_charclass) |
| 329 | for (i = 0; i < 256; i++) |
| 330 | term->wordness[i] = term->cfg.wordness[i]; |
| 331 | |
| 332 | if (term->cfg.no_alt_screen) |
| 333 | swap_screen(term, 0, FALSE, FALSE); |
| 334 | if (term->cfg.no_mouse_rep) { |
| 335 | term->xterm_mouse = 0; |
| 336 | set_raw_mouse_mode(term->frontend, 0); |
| 337 | } |
| 338 | if (term->cfg.no_remote_charset) { |
| 339 | term->cset_attr[0] = term->cset_attr[1] = ATTR_ASCII; |
| 340 | term->sco_acs = term->alt_sco_acs = 0; |
| 341 | term->utf = 0; |
| 342 | } |
| 343 | if (!*term->cfg.printer) { |
| 344 | term_print_finish(term); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Clear the scrollback. |
| 350 | */ |
| 351 | void term_clrsb(Terminal *term) |
| 352 | { |
| 353 | unsigned long *line; |
| 354 | term->disptop = 0; |
| 355 | while ((line = delpos234(term->scrollback, 0)) != NULL) { |
| 356 | sfree(line); |
| 357 | } |
| 358 | term->tempsblines = 0; |
| 359 | term->alt_sblines = 0; |
| 360 | update_sbar(term); |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Initialise the terminal. |
| 365 | */ |
| 366 | Terminal *term_init(Config *mycfg, struct unicode_data *ucsdata, |
| 367 | void *frontend) |
| 368 | { |
| 369 | Terminal *term; |
| 370 | |
| 371 | /* |
| 372 | * Allocate a new Terminal structure and initialise the fields |
| 373 | * that need it. |
| 374 | */ |
| 375 | term = smalloc(sizeof(Terminal)); |
| 376 | term->frontend = frontend; |
| 377 | term->ucsdata = ucsdata; |
| 378 | term->cfg = *mycfg; /* STRUCTURE COPY */ |
| 379 | term->logctx = NULL; |
| 380 | term->compatibility_level = TM_PUTTY; |
| 381 | strcpy(term->id_string, "\033[?6c"); |
| 382 | term->last_blink = term->last_tblink = 0; |
| 383 | term->paste_buffer = NULL; |
| 384 | term->paste_len = 0; |
| 385 | term->last_paste = 0; |
| 386 | bufchain_init(&term->inbuf); |
| 387 | bufchain_init(&term->printer_buf); |
| 388 | term->printing = term->only_printing = FALSE; |
| 389 | term->print_job = NULL; |
| 390 | term->vt52_mode = FALSE; |
| 391 | term->cr_lf_return = FALSE; |
| 392 | term->seen_disp_event = FALSE; |
| 393 | term->xterm_mouse = term->mouse_is_down = FALSE; |
| 394 | term->reset_132 = FALSE; |
| 395 | term->blinker = term->tblinker = 0; |
| 396 | term->has_focus = 1; |
| 397 | term->repeat_off = FALSE; |
| 398 | term->termstate = TOPLEVEL; |
| 399 | term->selstate = NO_SELECTION; |
| 400 | term->curstype = 0; |
| 401 | |
| 402 | term->screen = term->alt_screen = term->scrollback = NULL; |
| 403 | term->tempsblines = 0; |
| 404 | term->alt_sblines = 0; |
| 405 | term->disptop = 0; |
| 406 | term->disptext = term->dispcurs = NULL; |
| 407 | term->tabs = NULL; |
| 408 | deselect(term); |
| 409 | term->rows = term->cols = -1; |
| 410 | power_on(term); |
| 411 | term->beephead = term->beeptail = NULL; |
| 412 | #ifdef OPTIMISE_SCROLL |
| 413 | term->scrollhead = term->scrolltail = NULL; |
| 414 | #endif /* OPTIMISE_SCROLL */ |
| 415 | term->nbeeps = 0; |
| 416 | term->lastbeep = FALSE; |
| 417 | term->beep_overloaded = FALSE; |
| 418 | term->attr_mask = 0xffffffff; |
| 419 | term->resize_fn = NULL; |
| 420 | term->resize_ctx = NULL; |
| 421 | |
| 422 | return term; |
| 423 | } |
| 424 | |
| 425 | void term_free(Terminal *term) |
| 426 | { |
| 427 | unsigned long *line; |
| 428 | struct beeptime *beep; |
| 429 | |
| 430 | while ((line = delpos234(term->scrollback, 0)) != NULL) |
| 431 | sfree(line); |
| 432 | freetree234(term->scrollback); |
| 433 | while ((line = delpos234(term->screen, 0)) != NULL) |
| 434 | sfree(line); |
| 435 | freetree234(term->screen); |
| 436 | while ((line = delpos234(term->alt_screen, 0)) != NULL) |
| 437 | sfree(line); |
| 438 | freetree234(term->alt_screen); |
| 439 | sfree(term->disptext); |
| 440 | while (term->beephead) { |
| 441 | beep = term->beephead; |
| 442 | term->beephead = beep->next; |
| 443 | sfree(beep); |
| 444 | } |
| 445 | bufchain_clear(&term->inbuf); |
| 446 | if(term->print_job) |
| 447 | printer_finish_job(term->print_job); |
| 448 | bufchain_clear(&term->printer_buf); |
| 449 | sfree(term->paste_buffer); |
| 450 | sfree(term); |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * Set up the terminal for a given size. |
| 455 | */ |
| 456 | void term_size(Terminal *term, int newrows, int newcols, int newsavelines) |
| 457 | { |
| 458 | tree234 *newalt; |
| 459 | unsigned long *newdisp, *line; |
| 460 | int i, j; |
| 461 | int sblen; |
| 462 | int save_alt_which = term->alt_which; |
| 463 | |
| 464 | if (newrows == term->rows && newcols == term->cols && |
| 465 | newsavelines == term->savelines) |
| 466 | return; /* nothing to do */ |
| 467 | |
| 468 | deselect(term); |
| 469 | swap_screen(term, 0, FALSE, FALSE); |
| 470 | |
| 471 | term->alt_t = term->marg_t = 0; |
| 472 | term->alt_b = term->marg_b = newrows - 1; |
| 473 | |
| 474 | if (term->rows == -1) { |
| 475 | term->scrollback = newtree234(NULL); |
| 476 | term->screen = newtree234(NULL); |
| 477 | term->tempsblines = 0; |
| 478 | term->rows = 0; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Resize the screen and scrollback. We only need to shift |
| 483 | * lines around within our data structures, because lineptr() |
| 484 | * will take care of resizing each individual line if |
| 485 | * necessary. So: |
| 486 | * |
| 487 | * - If the new screen is longer, we shunt lines in from temporary |
| 488 | * scrollback if possible, otherwise we add new blank lines at |
| 489 | * the bottom. |
| 490 | * |
| 491 | * - If the new screen is shorter, we remove any blank lines at |
| 492 | * the bottom if possible, otherwise shunt lines above the cursor |
| 493 | * to scrollback if possible, otherwise delete lines below the |
| 494 | * cursor. |
| 495 | * |
| 496 | * - Then, if the new scrollback length is less than the |
| 497 | * amount of scrollback we actually have, we must throw some |
| 498 | * away. |
| 499 | */ |
| 500 | sblen = count234(term->scrollback); |
| 501 | /* Do this loop to expand the screen if newrows > rows */ |
| 502 | assert(term->rows == count234(term->screen)); |
| 503 | while (term->rows < newrows) { |
| 504 | if (term->tempsblines > 0) { |
| 505 | /* Insert a line from the scrollback at the top of the screen. */ |
| 506 | assert(sblen >= term->tempsblines); |
| 507 | line = delpos234(term->scrollback, --sblen); |
| 508 | term->tempsblines -= 1; |
| 509 | addpos234(term->screen, line, 0); |
| 510 | term->curs.y += 1; |
| 511 | term->savecurs.y += 1; |
| 512 | } else { |
| 513 | /* Add a new blank line at the bottom of the screen. */ |
| 514 | line = smalloc(TSIZE * (newcols + 2)); |
| 515 | line[0] = newcols; |
| 516 | for (j = 0; j < newcols; j++) |
| 517 | line[j + 1] = ERASE_CHAR; |
| 518 | line[newcols + 1] = LATTR_NORM; |
| 519 | addpos234(term->screen, line, count234(term->screen)); |
| 520 | } |
| 521 | term->rows += 1; |
| 522 | } |
| 523 | /* Do this loop to shrink the screen if newrows < rows */ |
| 524 | while (term->rows > newrows) { |
| 525 | if (term->curs.y < term->rows - 1) { |
| 526 | /* delete bottom row, unless it contains the cursor */ |
| 527 | sfree(delpos234(term->screen, term->rows - 1)); |
| 528 | } else { |
| 529 | /* push top row to scrollback */ |
| 530 | line = delpos234(term->screen, 0); |
| 531 | addpos234(term->scrollback, line, sblen++); |
| 532 | term->tempsblines += 1; |
| 533 | term->curs.y -= 1; |
| 534 | term->savecurs.y -= 1; |
| 535 | } |
| 536 | term->rows -= 1; |
| 537 | } |
| 538 | assert(term->rows == newrows); |
| 539 | assert(count234(term->screen) == newrows); |
| 540 | |
| 541 | /* Delete any excess lines from the scrollback. */ |
| 542 | while (sblen > newsavelines) { |
| 543 | line = delpos234(term->scrollback, 0); |
| 544 | sfree(line); |
| 545 | sblen--; |
| 546 | } |
| 547 | if (sblen < term->tempsblines) |
| 548 | term->tempsblines = sblen; |
| 549 | assert(count234(term->scrollback) <= newsavelines); |
| 550 | assert(count234(term->scrollback) >= term->tempsblines); |
| 551 | term->disptop = 0; |
| 552 | |
| 553 | /* Make a new displayed text buffer. */ |
| 554 | newdisp = smalloc(newrows * (newcols + 1) * TSIZE); |
| 555 | for (i = 0; i < newrows * (newcols + 1); i++) |
| 556 | newdisp[i] = ATTR_INVALID; |
| 557 | sfree(term->disptext); |
| 558 | term->disptext = newdisp; |
| 559 | term->dispcurs = NULL; |
| 560 | |
| 561 | /* Make a new alternate screen. */ |
| 562 | newalt = newtree234(NULL); |
| 563 | for (i = 0; i < newrows; i++) { |
| 564 | line = smalloc(TSIZE * (newcols + 2)); |
| 565 | line[0] = newcols; |
| 566 | for (j = 0; j < newcols; j++) |
| 567 | line[j + 1] = term->erase_char; |
| 568 | line[newcols + 1] = LATTR_NORM; |
| 569 | addpos234(newalt, line, i); |
| 570 | } |
| 571 | if (term->alt_screen) { |
| 572 | while (NULL != (line = delpos234(term->alt_screen, 0))) |
| 573 | sfree(line); |
| 574 | freetree234(term->alt_screen); |
| 575 | } |
| 576 | term->alt_screen = newalt; |
| 577 | term->alt_sblines = 0; |
| 578 | |
| 579 | term->tabs = srealloc(term->tabs, newcols * sizeof(*term->tabs)); |
| 580 | { |
| 581 | int i; |
| 582 | for (i = (term->cols > 0 ? term->cols : 0); i < newcols; i++) |
| 583 | term->tabs[i] = (i % 8 == 0 ? TRUE : FALSE); |
| 584 | } |
| 585 | |
| 586 | /* Check that the cursor positions are still valid. */ |
| 587 | if (term->savecurs.y < 0) |
| 588 | term->savecurs.y = 0; |
| 589 | if (term->savecurs.y >= newrows) |
| 590 | term->savecurs.y = newrows - 1; |
| 591 | if (term->curs.y < 0) |
| 592 | term->curs.y = 0; |
| 593 | if (term->curs.y >= newrows) |
| 594 | term->curs.y = newrows - 1; |
| 595 | if (term->curs.x >= newcols) |
| 596 | term->curs.x = newcols - 1; |
| 597 | term->alt_x = term->alt_y = 0; |
| 598 | term->wrapnext = term->alt_wnext = FALSE; |
| 599 | |
| 600 | term->rows = newrows; |
| 601 | term->cols = newcols; |
| 602 | term->savelines = newsavelines; |
| 603 | fix_cpos; |
| 604 | |
| 605 | swap_screen(term, save_alt_which, FALSE, FALSE); |
| 606 | |
| 607 | update_sbar(term); |
| 608 | term_update(term); |
| 609 | if (term->resize_fn) |
| 610 | term->resize_fn(term->resize_ctx, term->cols, term->rows); |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * Hand a function and context pointer to the terminal which it can |
| 615 | * use to notify a back end of resizes. |
| 616 | */ |
| 617 | void term_provide_resize_fn(Terminal *term, |
| 618 | void (*resize_fn)(void *, int, int), |
| 619 | void *resize_ctx) |
| 620 | { |
| 621 | term->resize_fn = resize_fn; |
| 622 | term->resize_ctx = resize_ctx; |
| 623 | if (term->cols > 0 && term->rows > 0) |
| 624 | resize_fn(resize_ctx, term->cols, term->rows); |
| 625 | } |
| 626 | |
| 627 | /* Find the bottom line on the screen that has any content. |
| 628 | * If only the top line has content, returns 0. |
| 629 | * If no lines have content, return -1. |
| 630 | */ |
| 631 | static int find_last_nonempty_line(Terminal * term, tree234 * screen) |
| 632 | { |
| 633 | int i; |
| 634 | for (i = count234(screen) - 1; i >= 0; i--) { |
| 635 | unsigned long *line = index234(screen, i); |
| 636 | int j; |
| 637 | int cols = line[0]; |
| 638 | for (j = 0; j < cols; j++) { |
| 639 | if (line[j + 1] != term->erase_char) break; |
| 640 | } |
| 641 | if (j != cols) break; |
| 642 | } |
| 643 | return i; |
| 644 | } |
| 645 | |
| 646 | /* |
| 647 | * Swap screens. If `reset' is TRUE and we have been asked to |
| 648 | * switch to the alternate screen, we must bring most of its |
| 649 | * configuration from the main screen and erase the contents of the |
| 650 | * alternate screen completely. (This is even true if we're already |
| 651 | * on it! Blame xterm.) |
| 652 | */ |
| 653 | static void swap_screen(Terminal *term, int which, int reset, int keep_cur_pos) |
| 654 | { |
| 655 | int t; |
| 656 | tree234 *ttr; |
| 657 | |
| 658 | if (!which) |
| 659 | reset = FALSE; /* do no weird resetting if which==0 */ |
| 660 | |
| 661 | if (which != term->alt_which) { |
| 662 | term->alt_which = which; |
| 663 | |
| 664 | ttr = term->alt_screen; |
| 665 | term->alt_screen = term->screen; |
| 666 | term->screen = ttr; |
| 667 | term->alt_sblines = find_last_nonempty_line(term, term->alt_screen) + 1; |
| 668 | t = term->curs.x; |
| 669 | if (!reset && !keep_cur_pos) |
| 670 | term->curs.x = term->alt_x; |
| 671 | term->alt_x = t; |
| 672 | t = term->curs.y; |
| 673 | if (!reset && !keep_cur_pos) |
| 674 | term->curs.y = term->alt_y; |
| 675 | term->alt_y = t; |
| 676 | t = term->marg_t; |
| 677 | if (!reset) term->marg_t = term->alt_t; |
| 678 | term->alt_t = t; |
| 679 | t = term->marg_b; |
| 680 | if (!reset) term->marg_b = term->alt_b; |
| 681 | term->alt_b = t; |
| 682 | t = term->dec_om; |
| 683 | if (!reset) term->dec_om = term->alt_om; |
| 684 | term->alt_om = t; |
| 685 | t = term->wrap; |
| 686 | if (!reset) term->wrap = term->alt_wrap; |
| 687 | term->alt_wrap = t; |
| 688 | t = term->wrapnext; |
| 689 | if (!reset) term->wrapnext = term->alt_wnext; |
| 690 | term->alt_wnext = t; |
| 691 | t = term->insert; |
| 692 | if (!reset) term->insert = term->alt_ins; |
| 693 | term->alt_ins = t; |
| 694 | t = term->cset; |
| 695 | if (!reset) term->cset = term->alt_cset; |
| 696 | term->alt_cset = t; |
| 697 | t = term->utf; |
| 698 | if (!reset) term->utf = term->alt_utf; |
| 699 | term->alt_utf = t; |
| 700 | t = term->sco_acs; |
| 701 | if (!reset) term->sco_acs = term->alt_sco_acs; |
| 702 | term->alt_sco_acs = t; |
| 703 | } |
| 704 | |
| 705 | if (reset && term->screen) { |
| 706 | /* |
| 707 | * Yes, this _is_ supposed to honour background-colour-erase. |
| 708 | */ |
| 709 | erase_lots(term, FALSE, TRUE, TRUE); |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * This might not be possible if we're called during |
| 714 | * initialisation. |
| 715 | */ |
| 716 | if (term->screen) |
| 717 | fix_cpos; |
| 718 | } |
| 719 | |
| 720 | /* |
| 721 | * Update the scroll bar. |
| 722 | */ |
| 723 | static void update_sbar(Terminal *term) |
| 724 | { |
| 725 | int nscroll = sblines(term); |
| 726 | set_sbar(term->frontend, nscroll + term->rows, |
| 727 | nscroll + term->disptop, term->rows); |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * Check whether the region bounded by the two pointers intersects |
| 732 | * the scroll region, and de-select the on-screen selection if so. |
| 733 | */ |
| 734 | static void check_selection(Terminal *term, pos from, pos to) |
| 735 | { |
| 736 | if (poslt(from, term->selend) && poslt(term->selstart, to)) |
| 737 | deselect(term); |
| 738 | } |
| 739 | |
| 740 | /* |
| 741 | * Scroll the screen. (`lines' is +ve for scrolling forward, -ve |
| 742 | * for backward.) `sb' is TRUE if the scrolling is permitted to |
| 743 | * affect the scrollback buffer. |
| 744 | * |
| 745 | * NB this function invalidates all pointers into lines of the |
| 746 | * screen data structures. In particular, you MUST call fix_cpos |
| 747 | * after calling scroll() and before doing anything else that |
| 748 | * uses the cpos shortcut pointer. |
| 749 | */ |
| 750 | static void scroll(Terminal *term, int topline, int botline, int lines, int sb) |
| 751 | { |
| 752 | unsigned long *line, *line2; |
| 753 | int i, seltop, olddisptop, shift; |
| 754 | |
| 755 | if (topline != 0 || term->alt_which != 0) |
| 756 | sb = FALSE; |
| 757 | |
| 758 | olddisptop = term->disptop; |
| 759 | shift = lines; |
| 760 | if (lines < 0) { |
| 761 | while (lines < 0) { |
| 762 | line = delpos234(term->screen, botline); |
| 763 | line = resizeline(line, term->cols); |
| 764 | for (i = 0; i < term->cols; i++) |
| 765 | line[i + 1] = term->erase_char; |
| 766 | line[term->cols + 1] = 0; |
| 767 | addpos234(term->screen, line, topline); |
| 768 | |
| 769 | if (term->selstart.y >= topline && term->selstart.y <= botline) { |
| 770 | term->selstart.y++; |
| 771 | if (term->selstart.y > botline) { |
| 772 | term->selstart.y = botline; |
| 773 | term->selstart.x = 0; |
| 774 | } |
| 775 | } |
| 776 | if (term->selend.y >= topline && term->selend.y <= botline) { |
| 777 | term->selend.y++; |
| 778 | if (term->selend.y > botline) { |
| 779 | term->selend.y = botline; |
| 780 | term->selend.x = 0; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | lines++; |
| 785 | } |
| 786 | } else { |
| 787 | while (lines > 0) { |
| 788 | line = delpos234(term->screen, topline); |
| 789 | if (sb && term->savelines > 0) { |
| 790 | int sblen = count234(term->scrollback); |
| 791 | /* |
| 792 | * We must add this line to the scrollback. We'll |
| 793 | * remove a line from the top of the scrollback to |
| 794 | * replace it, or allocate a new one if the |
| 795 | * scrollback isn't full. |
| 796 | */ |
| 797 | if (sblen == term->savelines) { |
| 798 | sblen--, line2 = delpos234(term->scrollback, 0); |
| 799 | } else { |
| 800 | line2 = smalloc(TSIZE * (term->cols + 2)); |
| 801 | line2[0] = term->cols; |
| 802 | term->tempsblines += 1; |
| 803 | } |
| 804 | addpos234(term->scrollback, line, sblen); |
| 805 | line = line2; |
| 806 | |
| 807 | /* |
| 808 | * If the user is currently looking at part of the |
| 809 | * scrollback, and they haven't enabled any options |
| 810 | * that are going to reset the scrollback as a |
| 811 | * result of this movement, then the chances are |
| 812 | * they'd like to keep looking at the same line. So |
| 813 | * we move their viewpoint at the same rate as the |
| 814 | * scroll, at least until their viewpoint hits the |
| 815 | * top end of the scrollback buffer, at which point |
| 816 | * we don't have the choice any more. |
| 817 | * |
| 818 | * Thanks to Jan Holmen Holsten for the idea and |
| 819 | * initial implementation. |
| 820 | */ |
| 821 | if (term->disptop > -term->savelines && term->disptop < 0) |
| 822 | term->disptop--; |
| 823 | } |
| 824 | line = resizeline(line, term->cols); |
| 825 | for (i = 0; i < term->cols; i++) |
| 826 | line[i + 1] = term->erase_char; |
| 827 | line[term->cols + 1] = LATTR_NORM; |
| 828 | addpos234(term->screen, line, botline); |
| 829 | |
| 830 | /* |
| 831 | * If the selection endpoints move into the scrollback, |
| 832 | * we keep them moving until they hit the top. However, |
| 833 | * of course, if the line _hasn't_ moved into the |
| 834 | * scrollback then we don't do this, and cut them off |
| 835 | * at the top of the scroll region. |
| 836 | * |
| 837 | * This applies to selstart and selend (for an existing |
| 838 | * selection), and also selanchor (for one being |
| 839 | * selected as we speak). |
| 840 | */ |
| 841 | seltop = sb ? -term->savelines : topline; |
| 842 | |
| 843 | if (term->selstart.y >= seltop && |
| 844 | term->selstart.y <= botline) { |
| 845 | term->selstart.y--; |
| 846 | if (term->selstart.y < seltop) { |
| 847 | term->selstart.y = seltop; |
| 848 | term->selstart.x = 0; |
| 849 | } |
| 850 | } |
| 851 | if (term->selend.y >= seltop && term->selend.y <= botline) { |
| 852 | term->selend.y--; |
| 853 | if (term->selend.y < seltop) { |
| 854 | term->selend.y = seltop; |
| 855 | term->selend.x = 0; |
| 856 | } |
| 857 | } |
| 858 | if (term->selanchor.y >= seltop && term->selanchor.y <= botline) { |
| 859 | term->selanchor.y--; |
| 860 | if (term->selanchor.y < seltop) { |
| 861 | term->selanchor.y = seltop; |
| 862 | term->selanchor.x = 0; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | lines--; |
| 867 | } |
| 868 | } |
| 869 | #ifdef OPTIMISE_SCROLL |
| 870 | shift += term->disptop - olddisptop; |
| 871 | if (shift < term->rows && shift > -term->rows && shift != 0) |
| 872 | scroll_display(term, topline, botline, shift); |
| 873 | #endif /* OPTIMISE_SCROLL */ |
| 874 | } |
| 875 | |
| 876 | #ifdef OPTIMISE_SCROLL |
| 877 | /* |
| 878 | * Add a scroll of a region on the screen into the pending scroll list. |
| 879 | * `lines' is +ve for scrolling forward, -ve for backward. |
| 880 | * |
| 881 | * If the scroll is on the same area as the last scroll in the list, |
| 882 | * merge them. |
| 883 | */ |
| 884 | static void save_scroll(Terminal *term, int topline, int botline, int lines) |
| 885 | { |
| 886 | struct scrollregion *newscroll; |
| 887 | if (term->scrolltail && |
| 888 | term->scrolltail->topline == topline && |
| 889 | term->scrolltail->botline == botline) { |
| 890 | term->scrolltail->lines += lines; |
| 891 | } else { |
| 892 | newscroll = smalloc(sizeof(struct scrollregion)); |
| 893 | newscroll->topline = topline; |
| 894 | newscroll->botline = botline; |
| 895 | newscroll->lines = lines; |
| 896 | newscroll->next = NULL; |
| 897 | |
| 898 | if (!term->scrollhead) |
| 899 | term->scrollhead = newscroll; |
| 900 | else |
| 901 | term->scrolltail->next = newscroll; |
| 902 | term->scrolltail = newscroll; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * Scroll the physical display, and our conception of it in disptext. |
| 908 | */ |
| 909 | static void scroll_display(Terminal *term, int topline, int botline, int lines) |
| 910 | { |
| 911 | unsigned long *start, *end; |
| 912 | int distance, size, i; |
| 913 | |
| 914 | start = term->disptext + topline * (term->cols + 1); |
| 915 | end = term->disptext + (botline + 1) * (term->cols + 1); |
| 916 | distance = (lines > 0 ? lines : -lines) * (term->cols + 1); |
| 917 | size = end - start - distance; |
| 918 | if (lines > 0) { |
| 919 | memmove(start, start + distance, size * TSIZE); |
| 920 | if (term->dispcurs >= start + distance && |
| 921 | term->dispcurs <= start + distance + size) |
| 922 | term->dispcurs -= distance; |
| 923 | for (i = 0; i < distance; i++) |
| 924 | (start + size)[i] |= ATTR_INVALID; |
| 925 | } else { |
| 926 | memmove(start + distance, start, size * TSIZE); |
| 927 | if (term->dispcurs >= start && term->dispcurs <= start + size) |
| 928 | term->dispcurs += distance; |
| 929 | for (i = 0; i < distance; i++) |
| 930 | start[i] |= ATTR_INVALID; |
| 931 | } |
| 932 | save_scroll(term, topline, botline, lines); |
| 933 | } |
| 934 | #endif /* OPTIMISE_SCROLL */ |
| 935 | |
| 936 | /* |
| 937 | * Move the cursor to a given position, clipping at boundaries. We |
| 938 | * may or may not want to clip at the scroll margin: marg_clip is 0 |
| 939 | * not to, 1 to disallow _passing_ the margins, and 2 to disallow |
| 940 | * even _being_ outside the margins. |
| 941 | */ |
| 942 | static void move(Terminal *term, int x, int y, int marg_clip) |
| 943 | { |
| 944 | if (x < 0) |
| 945 | x = 0; |
| 946 | if (x >= term->cols) |
| 947 | x = term->cols - 1; |
| 948 | if (marg_clip) { |
| 949 | if ((term->curs.y >= term->marg_t || marg_clip == 2) && |
| 950 | y < term->marg_t) |
| 951 | y = term->marg_t; |
| 952 | if ((term->curs.y <= term->marg_b || marg_clip == 2) && |
| 953 | y > term->marg_b) |
| 954 | y = term->marg_b; |
| 955 | } |
| 956 | if (y < 0) |
| 957 | y = 0; |
| 958 | if (y >= term->rows) |
| 959 | y = term->rows - 1; |
| 960 | term->curs.x = x; |
| 961 | term->curs.y = y; |
| 962 | fix_cpos; |
| 963 | term->wrapnext = FALSE; |
| 964 | } |
| 965 | |
| 966 | /* |
| 967 | * Save or restore the cursor and SGR mode. |
| 968 | */ |
| 969 | static void save_cursor(Terminal *term, int save) |
| 970 | { |
| 971 | if (save) { |
| 972 | term->savecurs = term->curs; |
| 973 | term->save_attr = term->curr_attr; |
| 974 | term->save_cset = term->cset; |
| 975 | term->save_utf = term->utf; |
| 976 | term->save_wnext = term->wrapnext; |
| 977 | term->save_csattr = term->cset_attr[term->cset]; |
| 978 | term->save_sco_acs = term->sco_acs; |
| 979 | } else { |
| 980 | term->curs = term->savecurs; |
| 981 | /* Make sure the window hasn't shrunk since the save */ |
| 982 | if (term->curs.x >= term->cols) |
| 983 | term->curs.x = term->cols - 1; |
| 984 | if (term->curs.y >= term->rows) |
| 985 | term->curs.y = term->rows - 1; |
| 986 | |
| 987 | term->curr_attr = term->save_attr; |
| 988 | term->cset = term->save_cset; |
| 989 | term->utf = term->save_utf; |
| 990 | term->wrapnext = term->save_wnext; |
| 991 | /* |
| 992 | * wrapnext might reset to False if the x position is no |
| 993 | * longer at the rightmost edge. |
| 994 | */ |
| 995 | if (term->wrapnext && term->curs.x < term->cols-1) |
| 996 | term->wrapnext = FALSE; |
| 997 | term->cset_attr[term->cset] = term->save_csattr; |
| 998 | term->sco_acs = term->save_sco_acs; |
| 999 | fix_cpos; |
| 1000 | if (term->use_bce) |
| 1001 | term->erase_char = (' ' | ATTR_ASCII | |
| 1002 | (term->curr_attr & |
| 1003 | (ATTR_FGMASK | ATTR_BGMASK))); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | /* |
| 1008 | * This function is called before doing _anything_ which affects |
| 1009 | * only part of a line of text. It is used to mark the boundary |
| 1010 | * between two character positions, and it indicates that some sort |
| 1011 | * of effect is going to happen on only one side of that boundary. |
| 1012 | * |
| 1013 | * The effect of this function is to check whether a CJK |
| 1014 | * double-width character is straddling the boundary, and to remove |
| 1015 | * it and replace it with two spaces if so. (Of course, one or |
| 1016 | * other of those spaces is then likely to be replaced with |
| 1017 | * something else again, as a result of whatever happens next.) |
| 1018 | * |
| 1019 | * Also, if the boundary is at the right-hand _edge_ of the screen, |
| 1020 | * it implies something deliberate is being done to the rightmost |
| 1021 | * column position; hence we must clear LATTR_WRAPPED2. |
| 1022 | * |
| 1023 | * The input to the function is the coordinates of the _second_ |
| 1024 | * character of the pair. |
| 1025 | */ |
| 1026 | static void check_boundary(Terminal *term, int x, int y) |
| 1027 | { |
| 1028 | unsigned long *ldata; |
| 1029 | |
| 1030 | /* Validate input coordinates, just in case. */ |
| 1031 | if (x == 0 || x > term->cols) |
| 1032 | return; |
| 1033 | |
| 1034 | ldata = lineptr(y); |
| 1035 | if (x == term->cols) { |
| 1036 | ldata[x] &= ~LATTR_WRAPPED2; |
| 1037 | } else { |
| 1038 | if ((ldata[x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE) { |
| 1039 | ldata[x-1] = ldata[x] = |
| 1040 | (ldata[x-1] &~ (CHAR_MASK | CSET_MASK)) | ATTR_ASCII | ' '; |
| 1041 | } |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | /* |
| 1046 | * Erase a large portion of the screen: the whole screen, or the |
| 1047 | * whole line, or parts thereof. |
| 1048 | */ |
| 1049 | static void erase_lots(Terminal *term, |
| 1050 | int line_only, int from_begin, int to_end) |
| 1051 | { |
| 1052 | pos start, end; |
| 1053 | int erase_lattr; |
| 1054 | int erasing_lines_from_top = 0; |
| 1055 | |
| 1056 | if (line_only) { |
| 1057 | start.y = term->curs.y; |
| 1058 | start.x = 0; |
| 1059 | end.y = term->curs.y + 1; |
| 1060 | end.x = 0; |
| 1061 | erase_lattr = FALSE; |
| 1062 | } else { |
| 1063 | start.y = 0; |
| 1064 | start.x = 0; |
| 1065 | end.y = term->rows; |
| 1066 | end.x = 0; |
| 1067 | erase_lattr = TRUE; |
| 1068 | } |
| 1069 | if (!from_begin) { |
| 1070 | start = term->curs; |
| 1071 | } |
| 1072 | if (!to_end) { |
| 1073 | end = term->curs; |
| 1074 | incpos(end); |
| 1075 | } |
| 1076 | if (!from_begin || !to_end) |
| 1077 | check_boundary(term, term->curs.x, term->curs.y); |
| 1078 | check_selection(term, start, end); |
| 1079 | |
| 1080 | /* Clear screen also forces a full window redraw, just in case. */ |
| 1081 | if (start.y == 0 && start.x == 0 && end.y == term->rows) |
| 1082 | term_invalidate(term); |
| 1083 | |
| 1084 | /* Lines scrolled away shouldn't be brought back on if the terminal |
| 1085 | * resizes. */ |
| 1086 | if (start.y == 0 && start.x == 0 && end.x == 0 && erase_lattr) |
| 1087 | erasing_lines_from_top = 1; |
| 1088 | |
| 1089 | if (term->cfg.erase_to_scrollback && erasing_lines_from_top) { |
| 1090 | /* If it's a whole number of lines, starting at the top, and |
| 1091 | * we're fully erasing them, erase by scrolling and keep the |
| 1092 | * lines in the scrollback. */ |
| 1093 | int scrolllines = end.y; |
| 1094 | if (end.y == term->rows) { |
| 1095 | /* Shrink until we find a non-empty row.*/ |
| 1096 | scrolllines = find_last_nonempty_line(term, term->screen) + 1; |
| 1097 | } |
| 1098 | if (scrolllines > 0) |
| 1099 | scroll(term, 0, scrolllines - 1, scrolllines, TRUE); |
| 1100 | fix_cpos; |
| 1101 | } else { |
| 1102 | unsigned long *ldata = lineptr(start.y); |
| 1103 | while (poslt(start, end)) { |
| 1104 | if (start.x == term->cols) { |
| 1105 | if (!erase_lattr) |
| 1106 | ldata[start.x] &= ~(LATTR_WRAPPED | LATTR_WRAPPED2); |
| 1107 | else |
| 1108 | ldata[start.x] = LATTR_NORM; |
| 1109 | } else { |
| 1110 | ldata[start.x] = term->erase_char; |
| 1111 | } |
| 1112 | if (incpos(start) && start.y < term->rows) |
| 1113 | ldata = lineptr(start.y); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | /* After an erase of lines from the top of the screen, we shouldn't |
| 1118 | * bring the lines back again if the terminal enlarges (since the user or |
| 1119 | * application has explictly thrown them away). */ |
| 1120 | if (erasing_lines_from_top && !(term->alt_which)) |
| 1121 | term->tempsblines = 0; |
| 1122 | } |
| 1123 | |
| 1124 | /* |
| 1125 | * Insert or delete characters within the current line. n is +ve if |
| 1126 | * insertion is desired, and -ve for deletion. |
| 1127 | */ |
| 1128 | static void insch(Terminal *term, int n) |
| 1129 | { |
| 1130 | int dir = (n < 0 ? -1 : +1); |
| 1131 | int m; |
| 1132 | pos cursplus; |
| 1133 | unsigned long *ldata; |
| 1134 | |
| 1135 | n = (n < 0 ? -n : n); |
| 1136 | if (n > term->cols - term->curs.x) |
| 1137 | n = term->cols - term->curs.x; |
| 1138 | m = term->cols - term->curs.x - n; |
| 1139 | cursplus.y = term->curs.y; |
| 1140 | cursplus.x = term->curs.x + n; |
| 1141 | check_selection(term, term->curs, cursplus); |
| 1142 | check_boundary(term, term->curs.x, term->curs.y); |
| 1143 | if (dir < 0) |
| 1144 | check_boundary(term, term->curs.x + n, term->curs.y); |
| 1145 | ldata = lineptr(term->curs.y); |
| 1146 | if (dir < 0) { |
| 1147 | memmove(ldata + term->curs.x, ldata + term->curs.x + n, m * TSIZE); |
| 1148 | while (n--) |
| 1149 | ldata[term->curs.x + m++] = term->erase_char; |
| 1150 | } else { |
| 1151 | memmove(ldata + term->curs.x + n, ldata + term->curs.x, m * TSIZE); |
| 1152 | while (n--) |
| 1153 | ldata[term->curs.x + n] = term->erase_char; |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | /* |
| 1158 | * Toggle terminal mode `mode' to state `state'. (`query' indicates |
| 1159 | * whether the mode is a DEC private one or a normal one.) |
| 1160 | */ |
| 1161 | static void toggle_mode(Terminal *term, int mode, int query, int state) |
| 1162 | { |
| 1163 | unsigned long ticks; |
| 1164 | |
| 1165 | if (query) |
| 1166 | switch (mode) { |
| 1167 | case 1: /* application cursor keys */ |
| 1168 | term->app_cursor_keys = state; |
| 1169 | break; |
| 1170 | case 2: /* VT52 mode */ |
| 1171 | term->vt52_mode = !state; |
| 1172 | if (term->vt52_mode) { |
| 1173 | term->blink_is_real = FALSE; |
| 1174 | term->vt52_bold = FALSE; |
| 1175 | } else { |
| 1176 | term->blink_is_real = term->cfg.blinktext; |
| 1177 | } |
| 1178 | break; |
| 1179 | case 3: /* 80/132 columns */ |
| 1180 | deselect(term); |
| 1181 | if (!term->cfg.no_remote_resize) |
| 1182 | request_resize(term->frontend, state ? 132 : 80, term->rows); |
| 1183 | term->reset_132 = state; |
| 1184 | break; |
| 1185 | case 5: /* reverse video */ |
| 1186 | /* |
| 1187 | * Toggle reverse video. If we receive an OFF within the |
| 1188 | * visual bell timeout period after an ON, we trigger an |
| 1189 | * effective visual bell, so that ESC[?5hESC[?5l will |
| 1190 | * always be an actually _visible_ visual bell. |
| 1191 | */ |
| 1192 | ticks = GETTICKCOUNT(); |
| 1193 | /* turn off a previous vbell to avoid inconsistencies */ |
| 1194 | if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT) |
| 1195 | term->in_vbell = FALSE; |
| 1196 | if (term->rvideo && !state && /* we're turning it off... */ |
| 1197 | (ticks - term->rvbell_startpoint) < VBELL_TIMEOUT) {/*...soon*/ |
| 1198 | /* If there's no vbell timeout already, or this one lasts |
| 1199 | * longer, replace vbell_timeout with ours. */ |
| 1200 | if (!term->in_vbell || |
| 1201 | (term->rvbell_startpoint - term->vbell_startpoint < |
| 1202 | VBELL_TIMEOUT)) |
| 1203 | term->vbell_startpoint = term->rvbell_startpoint; |
| 1204 | term->in_vbell = TRUE; /* may clear rvideo but set in_vbell */ |
| 1205 | } else if (!term->rvideo && state) { |
| 1206 | /* This is an ON, so we notice the time and save it. */ |
| 1207 | term->rvbell_startpoint = ticks; |
| 1208 | } |
| 1209 | term->rvideo = state; |
| 1210 | term->seen_disp_event = TRUE; |
| 1211 | if (state) |
| 1212 | term_update(term); |
| 1213 | break; |
| 1214 | case 6: /* DEC origin mode */ |
| 1215 | term->dec_om = state; |
| 1216 | break; |
| 1217 | case 7: /* auto wrap */ |
| 1218 | term->wrap = state; |
| 1219 | break; |
| 1220 | case 8: /* auto key repeat */ |
| 1221 | term->repeat_off = !state; |
| 1222 | break; |
| 1223 | case 10: /* set local edit mode */ |
| 1224 | term->term_editing = state; |
| 1225 | if (term->ldisc) /* cause ldisc to notice changes */ |
| 1226 | ldisc_send(term->ldisc, NULL, 0, 0); |
| 1227 | break; |
| 1228 | case 25: /* enable/disable cursor */ |
| 1229 | compatibility2(OTHER, VT220); |
| 1230 | term->cursor_on = state; |
| 1231 | term->seen_disp_event = TRUE; |
| 1232 | break; |
| 1233 | case 47: /* alternate screen */ |
| 1234 | compatibility(OTHER); |
| 1235 | deselect(term); |
| 1236 | swap_screen(term, term->cfg.no_alt_screen ? 0 : state, FALSE, FALSE); |
| 1237 | term->disptop = 0; |
| 1238 | break; |
| 1239 | case 1000: /* xterm mouse 1 */ |
| 1240 | term->xterm_mouse = state ? 1 : 0; |
| 1241 | set_raw_mouse_mode(term->frontend, state); |
| 1242 | break; |
| 1243 | case 1002: /* xterm mouse 2 */ |
| 1244 | term->xterm_mouse = state ? 2 : 0; |
| 1245 | set_raw_mouse_mode(term->frontend, state); |
| 1246 | break; |
| 1247 | case 1047: /* alternate screen */ |
| 1248 | compatibility(OTHER); |
| 1249 | deselect(term); |
| 1250 | swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, TRUE); |
| 1251 | term->disptop = 0; |
| 1252 | break; |
| 1253 | case 1048: /* save/restore cursor */ |
| 1254 | save_cursor(term, state); |
| 1255 | if (!state) term->seen_disp_event = TRUE; |
| 1256 | break; |
| 1257 | case 1049: /* cursor & alternate screen */ |
| 1258 | if (state) |
| 1259 | save_cursor(term, state); |
| 1260 | if (!state) term->seen_disp_event = TRUE; |
| 1261 | compatibility(OTHER); |
| 1262 | deselect(term); |
| 1263 | swap_screen(term, term->cfg.no_alt_screen ? 0 : state, TRUE, FALSE); |
| 1264 | if (!state) |
| 1265 | save_cursor(term, state); |
| 1266 | term->disptop = 0; |
| 1267 | break; |
| 1268 | } else |
| 1269 | switch (mode) { |
| 1270 | case 4: /* set insert mode */ |
| 1271 | compatibility(VT102); |
| 1272 | term->insert = state; |
| 1273 | break; |
| 1274 | case 12: /* set echo mode */ |
| 1275 | term->term_echoing = !state; |
| 1276 | if (term->ldisc) /* cause ldisc to notice changes */ |
| 1277 | ldisc_send(term->ldisc, NULL, 0, 0); |
| 1278 | break; |
| 1279 | case 20: /* Return sends ... */ |
| 1280 | term->cr_lf_return = state; |
| 1281 | break; |
| 1282 | case 34: /* Make cursor BIG */ |
| 1283 | compatibility2(OTHER, VT220); |
| 1284 | term->big_cursor = !state; |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | /* |
| 1289 | * Process an OSC sequence: set window title or icon name. |
| 1290 | */ |
| 1291 | static void do_osc(Terminal *term) |
| 1292 | { |
| 1293 | if (term->osc_w) { |
| 1294 | while (term->osc_strlen--) |
| 1295 | term->wordness[(unsigned char) |
| 1296 | term->osc_string[term->osc_strlen]] = term->esc_args[0]; |
| 1297 | } else { |
| 1298 | term->osc_string[term->osc_strlen] = '\0'; |
| 1299 | switch (term->esc_args[0]) { |
| 1300 | case 0: |
| 1301 | case 1: |
| 1302 | if (!term->cfg.no_remote_wintitle) |
| 1303 | set_icon(term->frontend, term->osc_string); |
| 1304 | if (term->esc_args[0] == 1) |
| 1305 | break; |
| 1306 | /* fall through: parameter 0 means set both */ |
| 1307 | case 2: |
| 1308 | case 21: |
| 1309 | if (!term->cfg.no_remote_wintitle) |
| 1310 | set_title(term->frontend, term->osc_string); |
| 1311 | break; |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /* |
| 1317 | * ANSI printing routines. |
| 1318 | */ |
| 1319 | static void term_print_setup(Terminal *term) |
| 1320 | { |
| 1321 | bufchain_clear(&term->printer_buf); |
| 1322 | term->print_job = printer_start_job(term->cfg.printer); |
| 1323 | } |
| 1324 | static void term_print_flush(Terminal *term) |
| 1325 | { |
| 1326 | void *data; |
| 1327 | int len; |
| 1328 | int size; |
| 1329 | while ((size = bufchain_size(&term->printer_buf)) > 5) { |
| 1330 | bufchain_prefix(&term->printer_buf, &data, &len); |
| 1331 | if (len > size-5) |
| 1332 | len = size-5; |
| 1333 | printer_job_data(term->print_job, data, len); |
| 1334 | bufchain_consume(&term->printer_buf, len); |
| 1335 | } |
| 1336 | } |
| 1337 | static void term_print_finish(Terminal *term) |
| 1338 | { |
| 1339 | void *data; |
| 1340 | int len, size; |
| 1341 | char c; |
| 1342 | |
| 1343 | if (!term->printing && !term->only_printing) |
| 1344 | return; /* we need do nothing */ |
| 1345 | |
| 1346 | term_print_flush(term); |
| 1347 | while ((size = bufchain_size(&term->printer_buf)) > 0) { |
| 1348 | bufchain_prefix(&term->printer_buf, &data, &len); |
| 1349 | c = *(char *)data; |
| 1350 | if (c == '\033' || c == '\233') { |
| 1351 | bufchain_consume(&term->printer_buf, size); |
| 1352 | break; |
| 1353 | } else { |
| 1354 | printer_job_data(term->print_job, &c, 1); |
| 1355 | bufchain_consume(&term->printer_buf, 1); |
| 1356 | } |
| 1357 | } |
| 1358 | printer_finish_job(term->print_job); |
| 1359 | term->print_job = NULL; |
| 1360 | term->printing = term->only_printing = FALSE; |
| 1361 | } |
| 1362 | |
| 1363 | /* |
| 1364 | * Remove everything currently in `inbuf' and stick it up on the |
| 1365 | * in-memory display. There's a big state machine in here to |
| 1366 | * process escape sequences... |
| 1367 | */ |
| 1368 | void term_out(Terminal *term) |
| 1369 | { |
| 1370 | int c, unget; |
| 1371 | unsigned char localbuf[256], *chars; |
| 1372 | int nchars = 0; |
| 1373 | |
| 1374 | unget = -1; |
| 1375 | |
| 1376 | chars = NULL; /* placate compiler warnings */ |
| 1377 | while (nchars > 0 || bufchain_size(&term->inbuf) > 0) { |
| 1378 | if (unget == -1) { |
| 1379 | if (nchars == 0) { |
| 1380 | void *ret; |
| 1381 | bufchain_prefix(&term->inbuf, &ret, &nchars); |
| 1382 | if (nchars > sizeof(localbuf)) |
| 1383 | nchars = sizeof(localbuf); |
| 1384 | memcpy(localbuf, ret, nchars); |
| 1385 | bufchain_consume(&term->inbuf, nchars); |
| 1386 | chars = localbuf; |
| 1387 | assert(chars != NULL); |
| 1388 | } |
| 1389 | c = *chars++; |
| 1390 | nchars--; |
| 1391 | |
| 1392 | /* |
| 1393 | * Optionally log the session traffic to a file. Useful for |
| 1394 | * debugging and possibly also useful for actual logging. |
| 1395 | */ |
| 1396 | if (term->cfg.logtype == LGTYP_DEBUG && term->logctx) |
| 1397 | logtraffic(term->logctx, (unsigned char) c, LGTYP_DEBUG); |
| 1398 | } else { |
| 1399 | c = unget; |
| 1400 | unget = -1; |
| 1401 | } |
| 1402 | |
| 1403 | /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even |
| 1404 | * be able to display 8-bit characters, but I'll let that go 'cause |
| 1405 | * of i18n. |
| 1406 | */ |
| 1407 | |
| 1408 | /* |
| 1409 | * If we're printing, add the character to the printer |
| 1410 | * buffer. |
| 1411 | */ |
| 1412 | if (term->printing) { |
| 1413 | bufchain_add(&term->printer_buf, &c, 1); |
| 1414 | |
| 1415 | /* |
| 1416 | * If we're in print-only mode, we use a much simpler |
| 1417 | * state machine designed only to recognise the ESC[4i |
| 1418 | * termination sequence. |
| 1419 | */ |
| 1420 | if (term->only_printing) { |
| 1421 | if (c == '\033') |
| 1422 | term->print_state = 1; |
| 1423 | else if (c == (unsigned char)'\233') |
| 1424 | term->print_state = 2; |
| 1425 | else if (c == '[' && term->print_state == 1) |
| 1426 | term->print_state = 2; |
| 1427 | else if (c == '4' && term->print_state == 2) |
| 1428 | term->print_state = 3; |
| 1429 | else if (c == 'i' && term->print_state == 3) |
| 1430 | term->print_state = 4; |
| 1431 | else |
| 1432 | term->print_state = 0; |
| 1433 | if (term->print_state == 4) { |
| 1434 | term_print_finish(term); |
| 1435 | } |
| 1436 | continue; |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | /* First see about all those translations. */ |
| 1441 | if (term->termstate == TOPLEVEL) { |
| 1442 | if (in_utf(term)) |
| 1443 | switch (term->utf_state) { |
| 1444 | case 0: |
| 1445 | if (c < 0x80) { |
| 1446 | /* UTF-8 must be stateless so we ignore iso2022. */ |
| 1447 | if (term->ucsdata->unitab_ctrl[c] != 0xFF) |
| 1448 | c = term->ucsdata->unitab_ctrl[c]; |
| 1449 | else c = ((unsigned char)c) | ATTR_ASCII; |
| 1450 | break; |
| 1451 | } else if ((c & 0xe0) == 0xc0) { |
| 1452 | term->utf_size = term->utf_state = 1; |
| 1453 | term->utf_char = (c & 0x1f); |
| 1454 | } else if ((c & 0xf0) == 0xe0) { |
| 1455 | term->utf_size = term->utf_state = 2; |
| 1456 | term->utf_char = (c & 0x0f); |
| 1457 | } else if ((c & 0xf8) == 0xf0) { |
| 1458 | term->utf_size = term->utf_state = 3; |
| 1459 | term->utf_char = (c & 0x07); |
| 1460 | } else if ((c & 0xfc) == 0xf8) { |
| 1461 | term->utf_size = term->utf_state = 4; |
| 1462 | term->utf_char = (c & 0x03); |
| 1463 | } else if ((c & 0xfe) == 0xfc) { |
| 1464 | term->utf_size = term->utf_state = 5; |
| 1465 | term->utf_char = (c & 0x01); |
| 1466 | } else { |
| 1467 | c = UCSERR; |
| 1468 | break; |
| 1469 | } |
| 1470 | continue; |
| 1471 | case 1: |
| 1472 | case 2: |
| 1473 | case 3: |
| 1474 | case 4: |
| 1475 | case 5: |
| 1476 | if ((c & 0xC0) != 0x80) { |
| 1477 | unget = c; |
| 1478 | c = UCSERR; |
| 1479 | term->utf_state = 0; |
| 1480 | break; |
| 1481 | } |
| 1482 | term->utf_char = (term->utf_char << 6) | (c & 0x3f); |
| 1483 | if (--term->utf_state) |
| 1484 | continue; |
| 1485 | |
| 1486 | c = term->utf_char; |
| 1487 | |
| 1488 | /* Is somebody trying to be evil! */ |
| 1489 | if (c < 0x80 || |
| 1490 | (c < 0x800 && term->utf_size >= 2) || |
| 1491 | (c < 0x10000 && term->utf_size >= 3) || |
| 1492 | (c < 0x200000 && term->utf_size >= 4) || |
| 1493 | (c < 0x4000000 && term->utf_size >= 5)) |
| 1494 | c = UCSERR; |
| 1495 | |
| 1496 | /* Unicode line separator and paragraph separator are CR-LF */ |
| 1497 | if (c == 0x2028 || c == 0x2029) |
| 1498 | c = 0x85; |
| 1499 | |
| 1500 | /* High controls are probably a Baaad idea too. */ |
| 1501 | if (c < 0xA0) |
| 1502 | c = 0xFFFD; |
| 1503 | |
| 1504 | /* The UTF-16 surrogates are not nice either. */ |
| 1505 | /* The standard give the option of decoding these: |
| 1506 | * I don't want to! */ |
| 1507 | if (c >= 0xD800 && c < 0xE000) |
| 1508 | c = UCSERR; |
| 1509 | |
| 1510 | /* ISO 10646 characters now limited to UTF-16 range. */ |
| 1511 | if (c > 0x10FFFF) |
| 1512 | c = UCSERR; |
| 1513 | |
| 1514 | /* This is currently a TagPhobic application.. */ |
| 1515 | if (c >= 0xE0000 && c <= 0xE007F) |
| 1516 | continue; |
| 1517 | |
| 1518 | /* U+FEFF is best seen as a null. */ |
| 1519 | if (c == 0xFEFF) |
| 1520 | continue; |
| 1521 | /* But U+FFFE is an error. */ |
| 1522 | if (c == 0xFFFE || c == 0xFFFF) |
| 1523 | c = UCSERR; |
| 1524 | |
| 1525 | /* Oops this is a 16bit implementation */ |
| 1526 | if (c >= 0x10000) |
| 1527 | c = 0xFFFD; |
| 1528 | break; |
| 1529 | } |
| 1530 | /* Are we in the nasty ACS mode? Note: no sco in utf mode. */ |
| 1531 | else if(term->sco_acs && |
| 1532 | (c!='\033' && c!='\012' && c!='\015' && c!='\b')) |
| 1533 | { |
| 1534 | if (term->sco_acs == 2) c |= 0x80; |
| 1535 | c |= ATTR_SCOACS; |
| 1536 | } else { |
| 1537 | switch (term->cset_attr[term->cset]) { |
| 1538 | /* |
| 1539 | * Linedraw characters are different from 'ESC ( B' |
| 1540 | * only for a small range. For ones outside that |
| 1541 | * range, make sure we use the same font as well as |
| 1542 | * the same encoding. |
| 1543 | */ |
| 1544 | case ATTR_LINEDRW: |
| 1545 | if (term->ucsdata->unitab_ctrl[c] != 0xFF) |
| 1546 | c = term->ucsdata->unitab_ctrl[c]; |
| 1547 | else |
| 1548 | c = ((unsigned char) c) | ATTR_LINEDRW; |
| 1549 | break; |
| 1550 | |
| 1551 | case ATTR_GBCHR: |
| 1552 | /* If UK-ASCII, make the '#' a LineDraw Pound */ |
| 1553 | if (c == '#') { |
| 1554 | c = '}' | ATTR_LINEDRW; |
| 1555 | break; |
| 1556 | } |
| 1557 | /*FALLTHROUGH*/ case ATTR_ASCII: |
| 1558 | if (term->ucsdata->unitab_ctrl[c] != 0xFF) |
| 1559 | c = term->ucsdata->unitab_ctrl[c]; |
| 1560 | else |
| 1561 | c = ((unsigned char) c) | ATTR_ASCII; |
| 1562 | break; |
| 1563 | case ATTR_SCOACS: |
| 1564 | if (c>=' ') c = ((unsigned char)c) | ATTR_SCOACS; |
| 1565 | break; |
| 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | /* How about C1 controls ? */ |
| 1571 | if ((c & -32) == 0x80 && term->termstate < DO_CTRLS && |
| 1572 | !term->vt52_mode && has_compat(VT220)) { |
| 1573 | term->termstate = SEEN_ESC; |
| 1574 | term->esc_query = FALSE; |
| 1575 | c = '@' + (c & 0x1F); |
| 1576 | } |
| 1577 | |
| 1578 | /* Or the GL control. */ |
| 1579 | if (c == '\177' && term->termstate < DO_CTRLS && has_compat(OTHER)) { |
| 1580 | if (term->curs.x && !term->wrapnext) |
| 1581 | term->curs.x--; |
| 1582 | term->wrapnext = FALSE; |
| 1583 | fix_cpos; |
| 1584 | if (!term->cfg.no_dbackspace) /* destructive bksp might be disabled */ |
| 1585 | *term->cpos = (' ' | term->curr_attr | ATTR_ASCII); |
| 1586 | } else |
| 1587 | /* Or normal C0 controls. */ |
| 1588 | if ((c & -32) == 0 && term->termstate < DO_CTRLS) { |
| 1589 | switch (c) { |
| 1590 | case '\005': /* terminal type query */ |
| 1591 | /* Strictly speaking this is VT100 but a VT100 defaults to |
| 1592 | * no response. Other terminals respond at their option. |
| 1593 | * |
| 1594 | * Don't put a CR in the default string as this tends to |
| 1595 | * upset some weird software. |
| 1596 | * |
| 1597 | * An xterm returns "xterm" (5 characters) |
| 1598 | */ |
| 1599 | compatibility(ANSIMIN); |
| 1600 | if (term->ldisc) { |
| 1601 | char abuf[256], *s, *d; |
| 1602 | int state = 0; |
| 1603 | for (s = term->cfg.answerback, d = abuf; *s; s++) { |
| 1604 | if (state) { |
| 1605 | if (*s >= 'a' && *s <= 'z') |
| 1606 | *d++ = (*s - ('a' - 1)); |
| 1607 | else if ((*s >= '@' && *s <= '_') || |
| 1608 | *s == '?' || (*s & 0x80)) |
| 1609 | *d++ = ('@' ^ *s); |
| 1610 | else if (*s == '~') |
| 1611 | *d++ = '^'; |
| 1612 | state = 0; |
| 1613 | } else if (*s == '^') { |
| 1614 | state = 1; |
| 1615 | } else |
| 1616 | *d++ = *s; |
| 1617 | } |
| 1618 | lpage_send(term->ldisc, DEFAULT_CODEPAGE, |
| 1619 | abuf, d - abuf, 0); |
| 1620 | } |
| 1621 | break; |
| 1622 | case '\007': |
| 1623 | { |
| 1624 | struct beeptime *newbeep; |
| 1625 | unsigned long ticks; |
| 1626 | |
| 1627 | ticks = GETTICKCOUNT(); |
| 1628 | |
| 1629 | if (!term->beep_overloaded) { |
| 1630 | newbeep = smalloc(sizeof(struct beeptime)); |
| 1631 | newbeep->ticks = ticks; |
| 1632 | newbeep->next = NULL; |
| 1633 | if (!term->beephead) |
| 1634 | term->beephead = newbeep; |
| 1635 | else |
| 1636 | term->beeptail->next = newbeep; |
| 1637 | term->beeptail = newbeep; |
| 1638 | term->nbeeps++; |
| 1639 | } |
| 1640 | |
| 1641 | /* |
| 1642 | * Throw out any beeps that happened more than |
| 1643 | * t seconds ago. |
| 1644 | */ |
| 1645 | while (term->beephead && |
| 1646 | term->beephead->ticks < ticks - term->cfg.bellovl_t) { |
| 1647 | struct beeptime *tmp = term->beephead; |
| 1648 | term->beephead = tmp->next; |
| 1649 | sfree(tmp); |
| 1650 | if (!term->beephead) |
| 1651 | term->beeptail = NULL; |
| 1652 | term->nbeeps--; |
| 1653 | } |
| 1654 | |
| 1655 | if (term->cfg.bellovl && term->beep_overloaded && |
| 1656 | ticks - term->lastbeep >= (unsigned)term->cfg.bellovl_s) { |
| 1657 | /* |
| 1658 | * If we're currently overloaded and the |
| 1659 | * last beep was more than s seconds ago, |
| 1660 | * leave overload mode. |
| 1661 | */ |
| 1662 | term->beep_overloaded = FALSE; |
| 1663 | } else if (term->cfg.bellovl && !term->beep_overloaded && |
| 1664 | term->nbeeps >= term->cfg.bellovl_n) { |
| 1665 | /* |
| 1666 | * Now, if we have n or more beeps |
| 1667 | * remaining in the queue, go into overload |
| 1668 | * mode. |
| 1669 | */ |
| 1670 | term->beep_overloaded = TRUE; |
| 1671 | } |
| 1672 | term->lastbeep = ticks; |
| 1673 | |
| 1674 | /* |
| 1675 | * Perform an actual beep if we're not overloaded. |
| 1676 | */ |
| 1677 | if (!term->cfg.bellovl || !term->beep_overloaded) { |
| 1678 | beep(term->frontend, term->cfg.beep); |
| 1679 | if (term->cfg.beep == BELL_VISUAL) { |
| 1680 | term->in_vbell = TRUE; |
| 1681 | term->vbell_startpoint = ticks; |
| 1682 | term_update(term); |
| 1683 | } |
| 1684 | } |
| 1685 | term->disptop = 0; |
| 1686 | } |
| 1687 | break; |
| 1688 | case '\b': |
| 1689 | if (term->curs.x == 0 && |
| 1690 | (term->curs.y == 0 || term->wrap == 0)) |
| 1691 | /* do nothing */ ; |
| 1692 | else if (term->curs.x == 0 && term->curs.y > 0) |
| 1693 | term->curs.x = term->cols - 1, term->curs.y--; |
| 1694 | else if (term->wrapnext) |
| 1695 | term->wrapnext = FALSE; |
| 1696 | else |
| 1697 | term->curs.x--; |
| 1698 | fix_cpos; |
| 1699 | term->seen_disp_event = TRUE; |
| 1700 | break; |
| 1701 | case '\016': |
| 1702 | compatibility(VT100); |
| 1703 | term->cset = 1; |
| 1704 | break; |
| 1705 | case '\017': |
| 1706 | compatibility(VT100); |
| 1707 | term->cset = 0; |
| 1708 | break; |
| 1709 | case '\033': |
| 1710 | if (term->vt52_mode) |
| 1711 | term->termstate = VT52_ESC; |
| 1712 | else { |
| 1713 | compatibility(ANSIMIN); |
| 1714 | term->termstate = SEEN_ESC; |
| 1715 | term->esc_query = FALSE; |
| 1716 | } |
| 1717 | break; |
| 1718 | case '\015': |
| 1719 | term->curs.x = 0; |
| 1720 | term->wrapnext = FALSE; |
| 1721 | fix_cpos; |
| 1722 | term->seen_disp_event = TRUE; |
| 1723 | term->paste_hold = 0; |
| 1724 | if (term->logctx) |
| 1725 | logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII); |
| 1726 | break; |
| 1727 | case '\014': |
| 1728 | if (has_compat(SCOANSI)) { |
| 1729 | move(term, 0, 0, 0); |
| 1730 | erase_lots(term, FALSE, FALSE, TRUE); |
| 1731 | term->disptop = 0; |
| 1732 | term->wrapnext = FALSE; |
| 1733 | term->seen_disp_event = 1; |
| 1734 | break; |
| 1735 | } |
| 1736 | case '\013': |
| 1737 | compatibility(VT100); |
| 1738 | case '\012': |
| 1739 | if (term->curs.y == term->marg_b) |
| 1740 | scroll(term, term->marg_t, term->marg_b, 1, TRUE); |
| 1741 | else if (term->curs.y < term->rows - 1) |
| 1742 | term->curs.y++; |
| 1743 | if (term->cfg.lfhascr) |
| 1744 | term->curs.x = 0; |
| 1745 | fix_cpos; |
| 1746 | term->wrapnext = FALSE; |
| 1747 | term->seen_disp_event = 1; |
| 1748 | term->paste_hold = 0; |
| 1749 | if (term->logctx) |
| 1750 | logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII); |
| 1751 | break; |
| 1752 | case '\t': |
| 1753 | { |
| 1754 | pos old_curs = term->curs; |
| 1755 | unsigned long *ldata = lineptr(term->curs.y); |
| 1756 | |
| 1757 | do { |
| 1758 | term->curs.x++; |
| 1759 | } while (term->curs.x < term->cols - 1 && |
| 1760 | !term->tabs[term->curs.x]); |
| 1761 | |
| 1762 | if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM) { |
| 1763 | if (term->curs.x >= term->cols / 2) |
| 1764 | term->curs.x = term->cols / 2 - 1; |
| 1765 | } else { |
| 1766 | if (term->curs.x >= term->cols) |
| 1767 | term->curs.x = term->cols - 1; |
| 1768 | } |
| 1769 | |
| 1770 | fix_cpos; |
| 1771 | check_selection(term, old_curs, term->curs); |
| 1772 | } |
| 1773 | term->seen_disp_event = TRUE; |
| 1774 | break; |
| 1775 | } |
| 1776 | } else |
| 1777 | switch (term->termstate) { |
| 1778 | case TOPLEVEL: |
| 1779 | /* Only graphic characters get this far; |
| 1780 | * ctrls are stripped above */ |
| 1781 | if (term->wrapnext && term->wrap) { |
| 1782 | term->cpos[1] |= LATTR_WRAPPED; |
| 1783 | if (term->curs.y == term->marg_b) |
| 1784 | scroll(term, term->marg_t, term->marg_b, 1, TRUE); |
| 1785 | else if (term->curs.y < term->rows - 1) |
| 1786 | term->curs.y++; |
| 1787 | term->curs.x = 0; |
| 1788 | fix_cpos; |
| 1789 | term->wrapnext = FALSE; |
| 1790 | } |
| 1791 | if (term->insert) |
| 1792 | insch(term, 1); |
| 1793 | if (term->selstate != NO_SELECTION) { |
| 1794 | pos cursplus = term->curs; |
| 1795 | incpos(cursplus); |
| 1796 | check_selection(term, term->curs, cursplus); |
| 1797 | } |
| 1798 | if (((c & CSET_MASK) == ATTR_ASCII || (c & CSET_MASK) == 0) && |
| 1799 | term->logctx) |
| 1800 | logtraffic(term->logctx, (unsigned char) c, LGTYP_ASCII); |
| 1801 | { |
| 1802 | int width = 0; |
| 1803 | if (DIRECT_CHAR(c)) |
| 1804 | width = 1; |
| 1805 | if (!width) |
| 1806 | width = wcwidth((wchar_t) c); |
| 1807 | switch (width) { |
| 1808 | case 2: |
| 1809 | /* |
| 1810 | * If we're about to display a double-width |
| 1811 | * character starting in the rightmost |
| 1812 | * column, then we do something special |
| 1813 | * instead. We must print a space in the |
| 1814 | * last column of the screen, then wrap; |
| 1815 | * and we also set LATTR_WRAPPED2 which |
| 1816 | * instructs subsequent cut-and-pasting not |
| 1817 | * only to splice this line to the one |
| 1818 | * after it, but to ignore the space in the |
| 1819 | * last character position as well. |
| 1820 | * (Because what was actually output to the |
| 1821 | * terminal was presumably just a sequence |
| 1822 | * of CJK characters, and we don't want a |
| 1823 | * space to be pasted in the middle of |
| 1824 | * those just because they had the |
| 1825 | * misfortune to start in the wrong parity |
| 1826 | * column. xterm concurs.) |
| 1827 | */ |
| 1828 | check_boundary(term, term->curs.x, term->curs.y); |
| 1829 | check_boundary(term, term->curs.x+2, term->curs.y); |
| 1830 | if (term->curs.x == term->cols-1) { |
| 1831 | *term->cpos++ = ATTR_ASCII | ' ' | term->curr_attr; |
| 1832 | *term->cpos |= LATTR_WRAPPED | LATTR_WRAPPED2; |
| 1833 | if (term->curs.y == term->marg_b) |
| 1834 | scroll(term, term->marg_t, term->marg_b, |
| 1835 | 1, TRUE); |
| 1836 | else if (term->curs.y < term->rows - 1) |
| 1837 | term->curs.y++; |
| 1838 | term->curs.x = 0; |
| 1839 | fix_cpos; |
| 1840 | /* Now we must check_boundary again, of course. */ |
| 1841 | check_boundary(term, term->curs.x, term->curs.y); |
| 1842 | check_boundary(term, term->curs.x+2, term->curs.y); |
| 1843 | } |
| 1844 | *term->cpos++ = c | term->curr_attr; |
| 1845 | *term->cpos++ = UCSWIDE | term->curr_attr; |
| 1846 | term->curs.x++; |
| 1847 | break; |
| 1848 | case 1: |
| 1849 | check_boundary(term, term->curs.x, term->curs.y); |
| 1850 | check_boundary(term, term->curs.x+1, term->curs.y); |
| 1851 | *term->cpos++ = c | term->curr_attr; |
| 1852 | break; |
| 1853 | default: |
| 1854 | continue; |
| 1855 | } |
| 1856 | } |
| 1857 | term->curs.x++; |
| 1858 | if (term->curs.x == term->cols) { |
| 1859 | term->cpos--; |
| 1860 | term->curs.x--; |
| 1861 | term->wrapnext = TRUE; |
| 1862 | if (term->wrap && term->vt52_mode) { |
| 1863 | term->cpos[1] |= LATTR_WRAPPED; |
| 1864 | if (term->curs.y == term->marg_b) |
| 1865 | scroll(term, term->marg_t, term->marg_b, 1, TRUE); |
| 1866 | else if (term->curs.y < term->rows - 1) |
| 1867 | term->curs.y++; |
| 1868 | term->curs.x = 0; |
| 1869 | fix_cpos; |
| 1870 | term->wrapnext = FALSE; |
| 1871 | } |
| 1872 | } |
| 1873 | term->seen_disp_event = 1; |
| 1874 | break; |
| 1875 | |
| 1876 | case OSC_MAYBE_ST: |
| 1877 | /* |
| 1878 | * This state is virtually identical to SEEN_ESC, with the |
| 1879 | * exception that we have an OSC sequence in the pipeline, |
| 1880 | * and _if_ we see a backslash, we process it. |
| 1881 | */ |
| 1882 | if (c == '\\') { |
| 1883 | do_osc(term); |
| 1884 | term->termstate = TOPLEVEL; |
| 1885 | break; |
| 1886 | } |
| 1887 | /* else fall through */ |
| 1888 | case SEEN_ESC: |
| 1889 | if (c >= ' ' && c <= '/') { |
| 1890 | if (term->esc_query) |
| 1891 | term->esc_query = -1; |
| 1892 | else |
| 1893 | term->esc_query = c; |
| 1894 | break; |
| 1895 | } |
| 1896 | term->termstate = TOPLEVEL; |
| 1897 | switch (ANSI(c, term->esc_query)) { |
| 1898 | case '[': /* enter CSI mode */ |
| 1899 | term->termstate = SEEN_CSI; |
| 1900 | term->esc_nargs = 1; |
| 1901 | term->esc_args[0] = ARG_DEFAULT; |
| 1902 | term->esc_query = FALSE; |
| 1903 | break; |
| 1904 | case ']': /* xterm escape sequences */ |
| 1905 | /* Compatibility is nasty here, xterm, linux, decterm yuk! */ |
| 1906 | compatibility(OTHER); |
| 1907 | term->termstate = SEEN_OSC; |
| 1908 | term->esc_args[0] = 0; |
| 1909 | break; |
| 1910 | case '7': /* save cursor */ |
| 1911 | compatibility(VT100); |
| 1912 | save_cursor(term, TRUE); |
| 1913 | break; |
| 1914 | case '8': /* restore cursor */ |
| 1915 | compatibility(VT100); |
| 1916 | save_cursor(term, FALSE); |
| 1917 | term->seen_disp_event = TRUE; |
| 1918 | break; |
| 1919 | case '=': |
| 1920 | compatibility(VT100); |
| 1921 | term->app_keypad_keys = TRUE; |
| 1922 | break; |
| 1923 | case '>': |
| 1924 | compatibility(VT100); |
| 1925 | term->app_keypad_keys = FALSE; |
| 1926 | break; |
| 1927 | case 'D': /* exactly equivalent to LF */ |
| 1928 | compatibility(VT100); |
| 1929 | if (term->curs.y == term->marg_b) |
| 1930 | scroll(term, term->marg_t, term->marg_b, 1, TRUE); |
| 1931 | else if (term->curs.y < term->rows - 1) |
| 1932 | term->curs.y++; |
| 1933 | fix_cpos; |
| 1934 | term->wrapnext = FALSE; |
| 1935 | term->seen_disp_event = TRUE; |
| 1936 | break; |
| 1937 | case 'E': /* exactly equivalent to CR-LF */ |
| 1938 | compatibility(VT100); |
| 1939 | term->curs.x = 0; |
| 1940 | if (term->curs.y == term->marg_b) |
| 1941 | scroll(term, term->marg_t, term->marg_b, 1, TRUE); |
| 1942 | else if (term->curs.y < term->rows - 1) |
| 1943 | term->curs.y++; |
| 1944 | fix_cpos; |
| 1945 | term->wrapnext = FALSE; |
| 1946 | term->seen_disp_event = TRUE; |
| 1947 | break; |
| 1948 | case 'M': /* reverse index - backwards LF */ |
| 1949 | compatibility(VT100); |
| 1950 | if (term->curs.y == term->marg_t) |
| 1951 | scroll(term, term->marg_t, term->marg_b, -1, TRUE); |
| 1952 | else if (term->curs.y > 0) |
| 1953 | term->curs.y--; |
| 1954 | fix_cpos; |
| 1955 | term->wrapnext = FALSE; |
| 1956 | term->seen_disp_event = TRUE; |
| 1957 | break; |
| 1958 | case 'Z': /* terminal type query */ |
| 1959 | compatibility(VT100); |
| 1960 | if (term->ldisc) |
| 1961 | ldisc_send(term->ldisc, term->id_string, |
| 1962 | strlen(term->id_string), 0); |
| 1963 | break; |
| 1964 | case 'c': /* restore power-on settings */ |
| 1965 | compatibility(VT100); |
| 1966 | power_on(term); |
| 1967 | if (term->ldisc) /* cause ldisc to notice changes */ |
| 1968 | ldisc_send(term->ldisc, NULL, 0, 0); |
| 1969 | if (term->reset_132) { |
| 1970 | if (!term->cfg.no_remote_resize) |
| 1971 | request_resize(term->frontend, 80, term->rows); |
| 1972 | term->reset_132 = 0; |
| 1973 | } |
| 1974 | fix_cpos; |
| 1975 | term->disptop = 0; |
| 1976 | term->seen_disp_event = TRUE; |
| 1977 | break; |
| 1978 | case 'H': /* set a tab */ |
| 1979 | compatibility(VT100); |
| 1980 | term->tabs[term->curs.x] = TRUE; |
| 1981 | break; |
| 1982 | |
| 1983 | case ANSI('8', '#'): /* ESC # 8 fills screen with Es :-) */ |
| 1984 | compatibility(VT100); |
| 1985 | { |
| 1986 | unsigned long *ldata; |
| 1987 | int i, j; |
| 1988 | pos scrtop, scrbot; |
| 1989 | |
| 1990 | for (i = 0; i < term->rows; i++) { |
| 1991 | ldata = lineptr(i); |
| 1992 | for (j = 0; j < term->cols; j++) |
| 1993 | ldata[j] = ATTR_DEFAULT | 'E'; |
| 1994 | ldata[term->cols] = 0; |
| 1995 | } |
| 1996 | term->disptop = 0; |
| 1997 | term->seen_disp_event = TRUE; |
| 1998 | scrtop.x = scrtop.y = 0; |
| 1999 | scrbot.x = 0; |
| 2000 | scrbot.y = term->rows; |
| 2001 | check_selection(term, scrtop, scrbot); |
| 2002 | } |
| 2003 | break; |
| 2004 | |
| 2005 | case ANSI('3', '#'): |
| 2006 | case ANSI('4', '#'): |
| 2007 | case ANSI('5', '#'): |
| 2008 | case ANSI('6', '#'): |
| 2009 | compatibility(VT100); |
| 2010 | { |
| 2011 | unsigned long nlattr; |
| 2012 | unsigned long *ldata; |
| 2013 | switch (ANSI(c, term->esc_query)) { |
| 2014 | case ANSI('3', '#'): |
| 2015 | nlattr = LATTR_TOP; |
| 2016 | break; |
| 2017 | case ANSI('4', '#'): |
| 2018 | nlattr = LATTR_BOT; |
| 2019 | break; |
| 2020 | case ANSI('5', '#'): |
| 2021 | nlattr = LATTR_NORM; |
| 2022 | break; |
| 2023 | default: /* spiritually case ANSI('6', '#'): */ |
| 2024 | nlattr = LATTR_WIDE; |
| 2025 | break; |
| 2026 | } |
| 2027 | ldata = lineptr(term->curs.y); |
| 2028 | ldata[term->cols] &= ~LATTR_MODE; |
| 2029 | ldata[term->cols] |= nlattr; |
| 2030 | } |
| 2031 | break; |
| 2032 | |
| 2033 | case ANSI('A', '('): |
| 2034 | compatibility(VT100); |
| 2035 | if (!term->cfg.no_remote_charset) |
| 2036 | term->cset_attr[0] = ATTR_GBCHR; |
| 2037 | break; |
| 2038 | case ANSI('B', '('): |
| 2039 | compatibility(VT100); |
| 2040 | if (!term->cfg.no_remote_charset) |
| 2041 | term->cset_attr[0] = ATTR_ASCII; |
| 2042 | break; |
| 2043 | case ANSI('0', '('): |
| 2044 | compatibility(VT100); |
| 2045 | if (!term->cfg.no_remote_charset) |
| 2046 | term->cset_attr[0] = ATTR_LINEDRW; |
| 2047 | break; |
| 2048 | case ANSI('U', '('): |
| 2049 | compatibility(OTHER); |
| 2050 | if (!term->cfg.no_remote_charset) |
| 2051 | term->cset_attr[0] = ATTR_SCOACS; |
| 2052 | break; |
| 2053 | |
| 2054 | case ANSI('A', ')'): |
| 2055 | compatibility(VT100); |
| 2056 | if (!term->cfg.no_remote_charset) |
| 2057 | term->cset_attr[1] = ATTR_GBCHR; |
| 2058 | break; |
| 2059 | case ANSI('B', ')'): |
| 2060 | compatibility(VT100); |
| 2061 | if (!term->cfg.no_remote_charset) |
| 2062 | term->cset_attr[1] = ATTR_ASCII; |
| 2063 | break; |
| 2064 | case ANSI('0', ')'): |
| 2065 | compatibility(VT100); |
| 2066 | if (!term->cfg.no_remote_charset) |
| 2067 | term->cset_attr[1] = ATTR_LINEDRW; |
| 2068 | break; |
| 2069 | case ANSI('U', ')'): |
| 2070 | compatibility(OTHER); |
| 2071 | if (!term->cfg.no_remote_charset) |
| 2072 | term->cset_attr[1] = ATTR_SCOACS; |
| 2073 | break; |
| 2074 | |
| 2075 | case ANSI('8', '%'): /* Old Linux code */ |
| 2076 | case ANSI('G', '%'): |
| 2077 | compatibility(OTHER); |
| 2078 | if (!term->cfg.no_remote_charset) |
| 2079 | term->utf = 1; |
| 2080 | break; |
| 2081 | case ANSI('@', '%'): |
| 2082 | compatibility(OTHER); |
| 2083 | if (!term->cfg.no_remote_charset) |
| 2084 | term->utf = 0; |
| 2085 | break; |
| 2086 | } |
| 2087 | break; |
| 2088 | case SEEN_CSI: |
| 2089 | term->termstate = TOPLEVEL; /* default */ |
| 2090 | if (isdigit(c)) { |
| 2091 | if (term->esc_nargs <= ARGS_MAX) { |
| 2092 | if (term->esc_args[term->esc_nargs - 1] == ARG_DEFAULT) |
| 2093 | term->esc_args[term->esc_nargs - 1] = 0; |
| 2094 | term->esc_args[term->esc_nargs - 1] = |
| 2095 | 10 * term->esc_args[term->esc_nargs - 1] + c - '0'; |
| 2096 | } |
| 2097 | term->termstate = SEEN_CSI; |
| 2098 | } else if (c == ';') { |
| 2099 | if (++term->esc_nargs <= ARGS_MAX) |
| 2100 | term->esc_args[term->esc_nargs - 1] = ARG_DEFAULT; |
| 2101 | term->termstate = SEEN_CSI; |
| 2102 | } else if (c < '@') { |
| 2103 | if (term->esc_query) |
| 2104 | term->esc_query = -1; |
| 2105 | else if (c == '?') |
| 2106 | term->esc_query = TRUE; |
| 2107 | else |
| 2108 | term->esc_query = c; |
| 2109 | term->termstate = SEEN_CSI; |
| 2110 | } else |
| 2111 | switch (ANSI(c, term->esc_query)) { |
| 2112 | case 'A': /* move up N lines */ |
| 2113 | move(term, term->curs.x, |
| 2114 | term->curs.y - def(term->esc_args[0], 1), 1); |
| 2115 | term->seen_disp_event = TRUE; |
| 2116 | break; |
| 2117 | case 'e': /* move down N lines */ |
| 2118 | compatibility(ANSI); |
| 2119 | /* FALLTHROUGH */ |
| 2120 | case 'B': |
| 2121 | move(term, term->curs.x, |
| 2122 | term->curs.y + def(term->esc_args[0], 1), 1); |
| 2123 | term->seen_disp_event = TRUE; |
| 2124 | break; |
| 2125 | case ANSI('c', '>'): /* report xterm version */ |
| 2126 | compatibility(OTHER); |
| 2127 | /* this reports xterm version 136 so that VIM can |
| 2128 | use the drag messages from the mouse reporting */ |
| 2129 | if (term->ldisc) |
| 2130 | ldisc_send(term->ldisc, "\033[>0;136;0c", 11, 0); |
| 2131 | break; |
| 2132 | case 'a': /* move right N cols */ |
| 2133 | compatibility(ANSI); |
| 2134 | /* FALLTHROUGH */ |
| 2135 | case 'C': |
| 2136 | move(term, term->curs.x + def(term->esc_args[0], 1), |
| 2137 | term->curs.y, 1); |
| 2138 | term->seen_disp_event = TRUE; |
| 2139 | break; |
| 2140 | case 'D': /* move left N cols */ |
| 2141 | move(term, term->curs.x - def(term->esc_args[0], 1), |
| 2142 | term->curs.y, 1); |
| 2143 | term->seen_disp_event = TRUE; |
| 2144 | break; |
| 2145 | case 'E': /* move down N lines and CR */ |
| 2146 | compatibility(ANSI); |
| 2147 | move(term, 0, |
| 2148 | term->curs.y + def(term->esc_args[0], 1), 1); |
| 2149 | term->seen_disp_event = TRUE; |
| 2150 | break; |
| 2151 | case 'F': /* move up N lines and CR */ |
| 2152 | compatibility(ANSI); |
| 2153 | move(term, 0, |
| 2154 | term->curs.y - def(term->esc_args[0], 1), 1); |
| 2155 | term->seen_disp_event = TRUE; |
| 2156 | break; |
| 2157 | case 'G': |
| 2158 | case '`': /* set horizontal posn */ |
| 2159 | compatibility(ANSI); |
| 2160 | move(term, def(term->esc_args[0], 1) - 1, |
| 2161 | term->curs.y, 0); |
| 2162 | term->seen_disp_event = TRUE; |
| 2163 | break; |
| 2164 | case 'd': /* set vertical posn */ |
| 2165 | compatibility(ANSI); |
| 2166 | move(term, term->curs.x, |
| 2167 | ((term->dec_om ? term->marg_t : 0) + |
| 2168 | def(term->esc_args[0], 1) - 1), |
| 2169 | (term->dec_om ? 2 : 0)); |
| 2170 | term->seen_disp_event = TRUE; |
| 2171 | break; |
| 2172 | case 'H': |
| 2173 | case 'f': /* set horz and vert posns at once */ |
| 2174 | if (term->esc_nargs < 2) |
| 2175 | term->esc_args[1] = ARG_DEFAULT; |
| 2176 | move(term, def(term->esc_args[1], 1) - 1, |
| 2177 | ((term->dec_om ? term->marg_t : 0) + |
| 2178 | def(term->esc_args[0], 1) - 1), |
| 2179 | (term->dec_om ? 2 : 0)); |
| 2180 | term->seen_disp_event = TRUE; |
| 2181 | break; |
| 2182 | case 'J': /* erase screen or parts of it */ |
| 2183 | { |
| 2184 | unsigned int i = def(term->esc_args[0], 0) + 1; |
| 2185 | if (i > 3) |
| 2186 | i = 0; |
| 2187 | erase_lots(term, FALSE, !!(i & 2), !!(i & 1)); |
| 2188 | } |
| 2189 | term->disptop = 0; |
| 2190 | term->seen_disp_event = TRUE; |
| 2191 | break; |
| 2192 | case 'K': /* erase line or parts of it */ |
| 2193 | { |
| 2194 | unsigned int i = def(term->esc_args[0], 0) + 1; |
| 2195 | if (i > 3) |
| 2196 | i = 0; |
| 2197 | erase_lots(term, TRUE, !!(i & 2), !!(i & 1)); |
| 2198 | } |
| 2199 | term->seen_disp_event = TRUE; |
| 2200 | break; |
| 2201 | case 'L': /* insert lines */ |
| 2202 | compatibility(VT102); |
| 2203 | if (term->curs.y <= term->marg_b) |
| 2204 | scroll(term, term->curs.y, term->marg_b, |
| 2205 | -def(term->esc_args[0], 1), FALSE); |
| 2206 | fix_cpos; |
| 2207 | term->seen_disp_event = TRUE; |
| 2208 | break; |
| 2209 | case 'M': /* delete lines */ |
| 2210 | compatibility(VT102); |
| 2211 | if (term->curs.y <= term->marg_b) |
| 2212 | scroll(term, term->curs.y, term->marg_b, |
| 2213 | def(term->esc_args[0], 1), |
| 2214 | TRUE); |
| 2215 | fix_cpos; |
| 2216 | term->seen_disp_event = TRUE; |
| 2217 | break; |
| 2218 | case '@': /* insert chars */ |
| 2219 | /* XXX VTTEST says this is vt220, vt510 manual says vt102 */ |
| 2220 | compatibility(VT102); |
| 2221 | insch(term, def(term->esc_args[0], 1)); |
| 2222 | term->seen_disp_event = TRUE; |
| 2223 | break; |
| 2224 | case 'P': /* delete chars */ |
| 2225 | compatibility(VT102); |
| 2226 | insch(term, -def(term->esc_args[0], 1)); |
| 2227 | term->seen_disp_event = TRUE; |
| 2228 | break; |
| 2229 | case 'c': /* terminal type query */ |
| 2230 | compatibility(VT100); |
| 2231 | /* This is the response for a VT102 */ |
| 2232 | if (term->ldisc) |
| 2233 | ldisc_send(term->ldisc, term->id_string, |
| 2234 | strlen(term->id_string), 0); |
| 2235 | break; |
| 2236 | case 'n': /* cursor position query */ |
| 2237 | if (term->ldisc) { |
| 2238 | if (term->esc_args[0] == 6) { |
| 2239 | char buf[32]; |
| 2240 | sprintf(buf, "\033[%d;%dR", term->curs.y + 1, |
| 2241 | term->curs.x + 1); |
| 2242 | ldisc_send(term->ldisc, buf, strlen(buf), 0); |
| 2243 | } else if (term->esc_args[0] == 5) { |
| 2244 | ldisc_send(term->ldisc, "\033[0n", 4, 0); |
| 2245 | } |
| 2246 | } |
| 2247 | break; |
| 2248 | case 'h': /* toggle modes to high */ |
| 2249 | case ANSI_QUE('h'): |
| 2250 | compatibility(VT100); |
| 2251 | { |
| 2252 | int i; |
| 2253 | for (i = 0; i < term->esc_nargs; i++) |
| 2254 | toggle_mode(term, term->esc_args[i], |
| 2255 | term->esc_query, TRUE); |
| 2256 | } |
| 2257 | break; |
| 2258 | case 'i': |
| 2259 | case ANSI_QUE('i'): |
| 2260 | compatibility(VT100); |
| 2261 | { |
| 2262 | if (term->esc_nargs != 1) break; |
| 2263 | if (term->esc_args[0] == 5 && *term->cfg.printer) { |
| 2264 | term->printing = TRUE; |
| 2265 | term->only_printing = !term->esc_query; |
| 2266 | term->print_state = 0; |
| 2267 | term_print_setup(term); |
| 2268 | } else if (term->esc_args[0] == 4 && |
| 2269 | term->printing) { |
| 2270 | term_print_finish(term); |
| 2271 | } |
| 2272 | } |
| 2273 | break; |
| 2274 | case 'l': /* toggle modes to low */ |
| 2275 | case ANSI_QUE('l'): |
| 2276 | compatibility(VT100); |
| 2277 | { |
| 2278 | int i; |
| 2279 | for (i = 0; i < term->esc_nargs; i++) |
| 2280 | toggle_mode(term, term->esc_args[i], |
| 2281 | term->esc_query, FALSE); |
| 2282 | } |
| 2283 | break; |
| 2284 | case 'g': /* clear tabs */ |
| 2285 | compatibility(VT100); |
| 2286 | if (term->esc_nargs == 1) { |
| 2287 | if (term->esc_args[0] == 0) { |
| 2288 | term->tabs[term->curs.x] = FALSE; |
| 2289 | } else if (term->esc_args[0] == 3) { |
| 2290 | int i; |
| 2291 | for (i = 0; i < term->cols; i++) |
| 2292 | term->tabs[i] = FALSE; |
| 2293 | } |
| 2294 | } |
| 2295 | break; |
| 2296 | case 'r': /* set scroll margins */ |
| 2297 | compatibility(VT100); |
| 2298 | if (term->esc_nargs <= 2) { |
| 2299 | int top, bot; |
| 2300 | top = def(term->esc_args[0], 1) - 1; |
| 2301 | bot = (term->esc_nargs <= 1 |
| 2302 | || term->esc_args[1] == 0 ? |
| 2303 | term->rows : |
| 2304 | def(term->esc_args[1], term->rows)) - 1; |
| 2305 | if (bot >= term->rows) |
| 2306 | bot = term->rows - 1; |
| 2307 | /* VTTEST Bug 9 - if region is less than 2 lines |
| 2308 | * don't change region. |
| 2309 | */ |
| 2310 | if (bot - top > 0) { |
| 2311 | term->marg_t = top; |
| 2312 | term->marg_b = bot; |
| 2313 | term->curs.x = 0; |
| 2314 | /* |
| 2315 | * I used to think the cursor should be |
| 2316 | * placed at the top of the newly marginned |
| 2317 | * area. Apparently not: VMS TPU falls over |
| 2318 | * if so. |
| 2319 | * |
| 2320 | * Well actually it should for |
| 2321 | * Origin mode - RDB |
| 2322 | */ |
| 2323 | term->curs.y = (term->dec_om ? |
| 2324 | term->marg_t : 0); |
| 2325 | fix_cpos; |
| 2326 | term->seen_disp_event = TRUE; |
| 2327 | } |
| 2328 | } |
| 2329 | break; |
| 2330 | case 'm': /* set graphics rendition */ |
| 2331 | { |
| 2332 | /* |
| 2333 | * A VT100 without the AVO only had one |
| 2334 | * attribute, either underline or |
| 2335 | * reverse video depending on the |
| 2336 | * cursor type, this was selected by |
| 2337 | * CSI 7m. |
| 2338 | * |
| 2339 | * case 2: |
| 2340 | * This is sometimes DIM, eg on the |
| 2341 | * GIGI and Linux |
| 2342 | * case 8: |
| 2343 | * This is sometimes INVIS various ANSI. |
| 2344 | * case 21: |
| 2345 | * This like 22 disables BOLD, DIM and INVIS |
| 2346 | * |
| 2347 | * The ANSI colours appear on any |
| 2348 | * terminal that has colour (obviously) |
| 2349 | * but the interaction between sgr0 and |
| 2350 | * the colours varies but is usually |
| 2351 | * related to the background colour |
| 2352 | * erase item. The interaction between |
| 2353 | * colour attributes and the mono ones |
| 2354 | * is also very implementation |
| 2355 | * dependent. |
| 2356 | * |
| 2357 | * The 39 and 49 attributes are likely |
| 2358 | * to be unimplemented. |
| 2359 | */ |
| 2360 | int i; |
| 2361 | for (i = 0; i < term->esc_nargs; i++) { |
| 2362 | switch (def(term->esc_args[i], 0)) { |
| 2363 | case 0: /* restore defaults */ |
| 2364 | term->curr_attr = ATTR_DEFAULT; |
| 2365 | break; |
| 2366 | case 1: /* enable bold */ |
| 2367 | compatibility(VT100AVO); |
| 2368 | term->curr_attr |= ATTR_BOLD; |
| 2369 | break; |
| 2370 | case 21: /* (enable double underline) */ |
| 2371 | compatibility(OTHER); |
| 2372 | case 4: /* enable underline */ |
| 2373 | compatibility(VT100AVO); |
| 2374 | term->curr_attr |= ATTR_UNDER; |
| 2375 | break; |
| 2376 | case 5: /* enable blink */ |
| 2377 | compatibility(VT100AVO); |
| 2378 | term->curr_attr |= ATTR_BLINK; |
| 2379 | break; |
| 2380 | case 7: /* enable reverse video */ |
| 2381 | term->curr_attr |= ATTR_REVERSE; |
| 2382 | break; |
| 2383 | case 10: /* SCO acs off */ |
| 2384 | compatibility(SCOANSI); |
| 2385 | if (term->cfg.no_remote_charset) break; |
| 2386 | term->sco_acs = 0; break; |
| 2387 | case 11: /* SCO acs on */ |
| 2388 | compatibility(SCOANSI); |
| 2389 | if (term->cfg.no_remote_charset) break; |
| 2390 | term->sco_acs = 1; break; |
| 2391 | case 12: /* SCO acs on, |0x80 */ |
| 2392 | compatibility(SCOANSI); |
| 2393 | if (term->cfg.no_remote_charset) break; |
| 2394 | term->sco_acs = 2; break; |
| 2395 | case 22: /* disable bold */ |
| 2396 | compatibility2(OTHER, VT220); |
| 2397 | term->curr_attr &= ~ATTR_BOLD; |
| 2398 | break; |
| 2399 | case 24: /* disable underline */ |
| 2400 | compatibility2(OTHER, VT220); |
| 2401 | term->curr_attr &= ~ATTR_UNDER; |
| 2402 | break; |
| 2403 | case 25: /* disable blink */ |
| 2404 | compatibility2(OTHER, VT220); |
| 2405 | term->curr_attr &= ~ATTR_BLINK; |
| 2406 | break; |
| 2407 | case 27: /* disable reverse video */ |
| 2408 | compatibility2(OTHER, VT220); |
| 2409 | term->curr_attr &= ~ATTR_REVERSE; |
| 2410 | break; |
| 2411 | case 30: |
| 2412 | case 31: |
| 2413 | case 32: |
| 2414 | case 33: |
| 2415 | case 34: |
| 2416 | case 35: |
| 2417 | case 36: |
| 2418 | case 37: |
| 2419 | /* foreground */ |
| 2420 | term->curr_attr &= ~ATTR_FGMASK; |
| 2421 | term->curr_attr |= |
| 2422 | (term->esc_args[i] - 30)<<ATTR_FGSHIFT; |
| 2423 | break; |
| 2424 | case 90: |
| 2425 | case 91: |
| 2426 | case 92: |
| 2427 | case 93: |
| 2428 | case 94: |
| 2429 | case 95: |
| 2430 | case 96: |
| 2431 | case 97: |
| 2432 | /* xterm-style bright foreground */ |
| 2433 | term->curr_attr &= ~ATTR_FGMASK; |
| 2434 | term->curr_attr |= |
| 2435 | ((term->esc_args[i] - 90 + 16) |
| 2436 | << ATTR_FGSHIFT); |
| 2437 | break; |
| 2438 | case 39: /* default-foreground */ |
| 2439 | term->curr_attr &= ~ATTR_FGMASK; |
| 2440 | term->curr_attr |= ATTR_DEFFG; |
| 2441 | break; |
| 2442 | case 40: |
| 2443 | case 41: |
| 2444 | case 42: |
| 2445 | case 43: |
| 2446 | case 44: |
| 2447 | case 45: |
| 2448 | case 46: |
| 2449 | case 47: |
| 2450 | /* background */ |
| 2451 | term->curr_attr &= ~ATTR_BGMASK; |
| 2452 | term->curr_attr |= |
| 2453 | (term->esc_args[i] - 40)<<ATTR_BGSHIFT; |
| 2454 | break; |
| 2455 | case 100: |
| 2456 | case 101: |
| 2457 | case 102: |
| 2458 | case 103: |
| 2459 | case 104: |
| 2460 | case 105: |
| 2461 | case 106: |
| 2462 | case 107: |
| 2463 | /* xterm-style bright background */ |
| 2464 | term->curr_attr &= ~ATTR_BGMASK; |
| 2465 | term->curr_attr |= |
| 2466 | ((term->esc_args[i] - 100 + 16) |
| 2467 | << ATTR_BGSHIFT); |
| 2468 | break; |
| 2469 | case 49: /* default-background */ |
| 2470 | term->curr_attr &= ~ATTR_BGMASK; |
| 2471 | term->curr_attr |= ATTR_DEFBG; |
| 2472 | break; |
| 2473 | } |
| 2474 | } |
| 2475 | if (term->use_bce) |
| 2476 | term->erase_char = (' ' | ATTR_ASCII | |
| 2477 | (term->curr_attr & |
| 2478 | (ATTR_FGMASK | |
| 2479 | ATTR_BGMASK))); |
| 2480 | } |
| 2481 | break; |
| 2482 | case 's': /* save cursor */ |
| 2483 | save_cursor(term, TRUE); |
| 2484 | break; |
| 2485 | case 'u': /* restore cursor */ |
| 2486 | save_cursor(term, FALSE); |
| 2487 | term->seen_disp_event = TRUE; |
| 2488 | break; |
| 2489 | case 't': /* set page size - ie window height */ |
| 2490 | /* |
| 2491 | * VT340/VT420 sequence DECSLPP, DEC only allows values |
| 2492 | * 24/25/36/48/72/144 other emulators (eg dtterm) use |
| 2493 | * illegal values (eg first arg 1..9) for window changing |
| 2494 | * and reports. |
| 2495 | */ |
| 2496 | if (term->esc_nargs <= 1 |
| 2497 | && (term->esc_args[0] < 1 || |
| 2498 | term->esc_args[0] >= 24)) { |
| 2499 | compatibility(VT340TEXT); |
| 2500 | if (!term->cfg.no_remote_resize) |
| 2501 | request_resize(term->frontend, term->cols, |
| 2502 | def(term->esc_args[0], 24)); |
| 2503 | deselect(term); |
| 2504 | } else if (term->esc_nargs >= 1 && |
| 2505 | term->esc_args[0] >= 1 && |
| 2506 | term->esc_args[0] < 24) { |
| 2507 | compatibility(OTHER); |
| 2508 | |
| 2509 | switch (term->esc_args[0]) { |
| 2510 | int x, y, len; |
| 2511 | char buf[80], *p; |
| 2512 | case 1: |
| 2513 | set_iconic(term->frontend, FALSE); |
| 2514 | break; |
| 2515 | case 2: |
| 2516 | set_iconic(term->frontend, TRUE); |
| 2517 | break; |
| 2518 | case 3: |
| 2519 | if (term->esc_nargs >= 3) { |
| 2520 | if (!term->cfg.no_remote_resize) |
| 2521 | move_window(term->frontend, |
| 2522 | def(term->esc_args[1], 0), |
| 2523 | def(term->esc_args[2], 0)); |
| 2524 | } |
| 2525 | break; |
| 2526 | case 4: |
| 2527 | /* We should resize the window to a given |
| 2528 | * size in pixels here, but currently our |
| 2529 | * resizing code isn't healthy enough to |
| 2530 | * manage it. */ |
| 2531 | break; |
| 2532 | case 5: |
| 2533 | /* move to top */ |
| 2534 | set_zorder(term->frontend, TRUE); |
| 2535 | break; |
| 2536 | case 6: |
| 2537 | /* move to bottom */ |
| 2538 | set_zorder(term->frontend, FALSE); |
| 2539 | break; |
| 2540 | case 7: |
| 2541 | refresh_window(term->frontend); |
| 2542 | break; |
| 2543 | case 8: |
| 2544 | if (term->esc_nargs >= 3) { |
| 2545 | if (!term->cfg.no_remote_resize) |
| 2546 | request_resize(term->frontend, |
| 2547 | def(term->esc_args[2], term->cfg.width), |
| 2548 | def(term->esc_args[1], term->cfg.height)); |
| 2549 | } |
| 2550 | break; |
| 2551 | case 9: |
| 2552 | if (term->esc_nargs >= 2) |
| 2553 | set_zoomed(term->frontend, |
| 2554 | term->esc_args[1] ? |
| 2555 | TRUE : FALSE); |
| 2556 | break; |
| 2557 | case 11: |
| 2558 | if (term->ldisc) |
| 2559 | ldisc_send(term->ldisc, |
| 2560 | is_iconic(term->frontend) ? |
| 2561 | "\033[1t" : "\033[2t", 4, 0); |
| 2562 | break; |
| 2563 | case 13: |
| 2564 | if (term->ldisc) { |
| 2565 | get_window_pos(term->frontend, &x, &y); |
| 2566 | len = sprintf(buf, "\033[3;%d;%dt", x, y); |
| 2567 | ldisc_send(term->ldisc, buf, len, 0); |
| 2568 | } |
| 2569 | break; |
| 2570 | case 14: |
| 2571 | if (term->ldisc) { |
| 2572 | get_window_pixels(term->frontend, &x, &y); |
| 2573 | len = sprintf(buf, "\033[4;%d;%dt", x, y); |
| 2574 | ldisc_send(term->ldisc, buf, len, 0); |
| 2575 | } |
| 2576 | break; |
| 2577 | case 18: |
| 2578 | if (term->ldisc) { |
| 2579 | len = sprintf(buf, "\033[8;%d;%dt", |
| 2580 | term->rows, term->cols); |
| 2581 | ldisc_send(term->ldisc, buf, len, 0); |
| 2582 | } |
| 2583 | break; |
| 2584 | case 19: |
| 2585 | /* |
| 2586 | * Hmmm. Strictly speaking we |
| 2587 | * should return `the size of the |
| 2588 | * screen in characters', but |
| 2589 | * that's not easy: (a) window |
| 2590 | * furniture being what it is it's |
| 2591 | * hard to compute, and (b) in |
| 2592 | * resize-font mode maximising the |
| 2593 | * window wouldn't change the |
| 2594 | * number of characters. *shrug*. I |
| 2595 | * think we'll ignore it for the |
| 2596 | * moment and see if anyone |
| 2597 | * complains, and then ask them |
| 2598 | * what they would like it to do. |
| 2599 | */ |
| 2600 | break; |
| 2601 | case 20: |
| 2602 | if (term->ldisc) { |
| 2603 | p = get_window_title(term->frontend, TRUE); |
| 2604 | len = strlen(p); |
| 2605 | ldisc_send(term->ldisc, "\033]L", 3, 0); |
| 2606 | ldisc_send(term->ldisc, p, len, 0); |
| 2607 | ldisc_send(term->ldisc, "\033\\", 2, 0); |
| 2608 | } |
| 2609 | break; |
| 2610 | case 21: |
| 2611 | if (term->ldisc) { |
| 2612 | p = get_window_title(term->frontend,FALSE); |
| 2613 | len = strlen(p); |
| 2614 | ldisc_send(term->ldisc, "\033]l", 3, 0); |
| 2615 | ldisc_send(term->ldisc, p, len, 0); |
| 2616 | ldisc_send(term->ldisc, "\033\\", 2, 0); |
| 2617 | } |
| 2618 | break; |
| 2619 | } |
| 2620 | } |
| 2621 | break; |
| 2622 | case 'S': |
| 2623 | compatibility(SCOANSI); |
| 2624 | scroll(term, term->marg_t, term->marg_b, |
| 2625 | def(term->esc_args[0], 1), TRUE); |
| 2626 | fix_cpos; |
| 2627 | term->wrapnext = FALSE; |
| 2628 | term->seen_disp_event = TRUE; |
| 2629 | break; |
| 2630 | case 'T': |
| 2631 | compatibility(SCOANSI); |
| 2632 | scroll(term, term->marg_t, term->marg_b, |
| 2633 | -def(term->esc_args[0], 1), TRUE); |
| 2634 | fix_cpos; |
| 2635 | term->wrapnext = FALSE; |
| 2636 | term->seen_disp_event = TRUE; |
| 2637 | break; |
| 2638 | case ANSI('|', '*'): |
| 2639 | /* VT420 sequence DECSNLS |
| 2640 | * Set number of lines on screen |
| 2641 | * VT420 uses VGA like hardware and can support any size in |
| 2642 | * reasonable range (24..49 AIUI) with no default specified. |
| 2643 | */ |
| 2644 | compatibility(VT420); |
| 2645 | if (term->esc_nargs == 1 && term->esc_args[0] > 0) { |
| 2646 | if (!term->cfg.no_remote_resize) |
| 2647 | request_resize(term->frontend, term->cols, |
| 2648 | def(term->esc_args[0], |
| 2649 | term->cfg.height)); |
| 2650 | deselect(term); |
| 2651 | } |
| 2652 | break; |
| 2653 | case ANSI('|', '$'): |
| 2654 | /* VT340/VT420 sequence DECSCPP |
| 2655 | * Set number of columns per page |
| 2656 | * Docs imply range is only 80 or 132, but I'll allow any. |
| 2657 | */ |
| 2658 | compatibility(VT340TEXT); |
| 2659 | if (term->esc_nargs <= 1) { |
| 2660 | if (!term->cfg.no_remote_resize) |
| 2661 | request_resize(term->frontend, |
| 2662 | def(term->esc_args[0], |
| 2663 | term->cfg.width), term->rows); |
| 2664 | deselect(term); |
| 2665 | } |
| 2666 | break; |
| 2667 | case 'X': /* write N spaces w/o moving cursor */ |
| 2668 | /* XXX VTTEST says this is vt220, vt510 manual says vt100 */ |
| 2669 | compatibility(ANSIMIN); |
| 2670 | { |
| 2671 | int n = def(term->esc_args[0], 1); |
| 2672 | pos cursplus; |
| 2673 | unsigned long *p = term->cpos; |
| 2674 | if (n > term->cols - term->curs.x) |
| 2675 | n = term->cols - term->curs.x; |
| 2676 | cursplus = term->curs; |
| 2677 | cursplus.x += n; |
| 2678 | check_boundary(term, term->curs.x, term->curs.y); |
| 2679 | check_boundary(term, term->curs.x+n, term->curs.y); |
| 2680 | check_selection(term, term->curs, cursplus); |
| 2681 | while (n--) |
| 2682 | *p++ = term->erase_char; |
| 2683 | term->seen_disp_event = TRUE; |
| 2684 | } |
| 2685 | break; |
| 2686 | case 'x': /* report terminal characteristics */ |
| 2687 | compatibility(VT100); |
| 2688 | if (term->ldisc) { |
| 2689 | char buf[32]; |
| 2690 | int i = def(term->esc_args[0], 0); |
| 2691 | if (i == 0 || i == 1) { |
| 2692 | strcpy(buf, "\033[2;1;1;112;112;1;0x"); |
| 2693 | buf[2] += i; |
| 2694 | ldisc_send(term->ldisc, buf, 20, 0); |
| 2695 | } |
| 2696 | } |
| 2697 | break; |
| 2698 | case 'Z': /* BackTab for xterm */ |
| 2699 | compatibility(OTHER); |
| 2700 | { |
| 2701 | int i = def(term->esc_args[0], 1); |
| 2702 | pos old_curs = term->curs; |
| 2703 | |
| 2704 | for(;i>0 && term->curs.x>0; i--) { |
| 2705 | do { |
| 2706 | term->curs.x--; |
| 2707 | } while (term->curs.x >0 && |
| 2708 | !term->tabs[term->curs.x]); |
| 2709 | } |
| 2710 | fix_cpos; |
| 2711 | check_selection(term, old_curs, term->curs); |
| 2712 | } |
| 2713 | break; |
| 2714 | case ANSI('L', '='): |
| 2715 | compatibility(OTHER); |
| 2716 | term->use_bce = (term->esc_args[0] <= 0); |
| 2717 | term->erase_char = ERASE_CHAR; |
| 2718 | if (term->use_bce) |
| 2719 | term->erase_char = (' ' | ATTR_ASCII | |
| 2720 | (term->curr_attr & |
| 2721 | (ATTR_FGMASK | ATTR_BGMASK))); |
| 2722 | break; |
| 2723 | case ANSI('E', '='): |
| 2724 | compatibility(OTHER); |
| 2725 | term->blink_is_real = (term->esc_args[0] >= 1); |
| 2726 | break; |
| 2727 | case ANSI('p', '"'): |
| 2728 | /* |
| 2729 | * Allow the host to make this emulator a |
| 2730 | * 'perfect' VT102. This first appeared in |
| 2731 | * the VT220, but we do need to get back to |
| 2732 | * PuTTY mode so I won't check it. |
| 2733 | * |
| 2734 | * The arg in 40..42,50 are a PuTTY extension. |
| 2735 | * The 2nd arg, 8bit vs 7bit is not checked. |
| 2736 | * |
| 2737 | * Setting VT102 mode should also change |
| 2738 | * the Fkeys to generate PF* codes as a |
| 2739 | * real VT102 has no Fkeys. The VT220 does |
| 2740 | * this, F11..F13 become ESC,BS,LF other |
| 2741 | * Fkeys send nothing. |
| 2742 | * |
| 2743 | * Note ESC c will NOT change this! |
| 2744 | */ |
| 2745 | |
| 2746 | switch (term->esc_args[0]) { |
| 2747 | case 61: |
| 2748 | term->compatibility_level &= ~TM_VTXXX; |
| 2749 | term->compatibility_level |= TM_VT102; |
| 2750 | break; |
| 2751 | case 62: |
| 2752 | term->compatibility_level &= ~TM_VTXXX; |
| 2753 | term->compatibility_level |= TM_VT220; |
| 2754 | break; |
| 2755 | |
| 2756 | default: |
| 2757 | if (term->esc_args[0] > 60 && |
| 2758 | term->esc_args[0] < 70) |
| 2759 | term->compatibility_level |= TM_VTXXX; |
| 2760 | break; |
| 2761 | |
| 2762 | case 40: |
| 2763 | term->compatibility_level &= TM_VTXXX; |
| 2764 | break; |
| 2765 | case 41: |
| 2766 | term->compatibility_level = TM_PUTTY; |
| 2767 | break; |
| 2768 | case 42: |
| 2769 | term->compatibility_level = TM_SCOANSI; |
| 2770 | break; |
| 2771 | |
| 2772 | case ARG_DEFAULT: |
| 2773 | term->compatibility_level = TM_PUTTY; |
| 2774 | break; |
| 2775 | case 50: |
| 2776 | break; |
| 2777 | } |
| 2778 | |
| 2779 | /* Change the response to CSI c */ |
| 2780 | if (term->esc_args[0] == 50) { |
| 2781 | int i; |
| 2782 | char lbuf[64]; |
| 2783 | strcpy(term->id_string, "\033[?"); |
| 2784 | for (i = 1; i < term->esc_nargs; i++) { |
| 2785 | if (i != 1) |
| 2786 | strcat(term->id_string, ";"); |
| 2787 | sprintf(lbuf, "%d", term->esc_args[i]); |
| 2788 | strcat(term->id_string, lbuf); |
| 2789 | } |
| 2790 | strcat(term->id_string, "c"); |
| 2791 | } |
| 2792 | #if 0 |
| 2793 | /* Is this a good idea ? |
| 2794 | * Well we should do a soft reset at this point ... |
| 2795 | */ |
| 2796 | if (!has_compat(VT420) && has_compat(VT100)) { |
| 2797 | if (!term->cfg.no_remote_resize) { |
| 2798 | if (term->reset_132) |
| 2799 | request_resize(132, 24); |
| 2800 | else |
| 2801 | request_resize(80, 24); |
| 2802 | } |
| 2803 | } |
| 2804 | #endif |
| 2805 | break; |
| 2806 | } |
| 2807 | break; |
| 2808 | case SEEN_OSC: |
| 2809 | term->osc_w = FALSE; |
| 2810 | switch (c) { |
| 2811 | case 'P': /* Linux palette sequence */ |
| 2812 | term->termstate = SEEN_OSC_P; |
| 2813 | term->osc_strlen = 0; |
| 2814 | break; |
| 2815 | case 'R': /* Linux palette reset */ |
| 2816 | palette_reset(term->frontend); |
| 2817 | term_invalidate(term); |
| 2818 | term->termstate = TOPLEVEL; |
| 2819 | break; |
| 2820 | case 'W': /* word-set */ |
| 2821 | term->termstate = SEEN_OSC_W; |
| 2822 | term->osc_w = TRUE; |
| 2823 | break; |
| 2824 | case '0': |
| 2825 | case '1': |
| 2826 | case '2': |
| 2827 | case '3': |
| 2828 | case '4': |
| 2829 | case '5': |
| 2830 | case '6': |
| 2831 | case '7': |
| 2832 | case '8': |
| 2833 | case '9': |
| 2834 | term->esc_args[0] = 10 * term->esc_args[0] + c - '0'; |
| 2835 | break; |
| 2836 | case 'L': |
| 2837 | /* |
| 2838 | * Grotty hack to support xterm and DECterm title |
| 2839 | * sequences concurrently. |
| 2840 | */ |
| 2841 | if (term->esc_args[0] == 2) { |
| 2842 | term->esc_args[0] = 1; |
| 2843 | break; |
| 2844 | } |
| 2845 | /* else fall through */ |
| 2846 | default: |
| 2847 | term->termstate = OSC_STRING; |
| 2848 | term->osc_strlen = 0; |
| 2849 | } |
| 2850 | break; |
| 2851 | case OSC_STRING: |
| 2852 | /* |
| 2853 | * This OSC stuff is EVIL. It takes just one character to get into |
| 2854 | * sysline mode and it's not initially obvious how to get out. |
| 2855 | * So I've added CR and LF as string aborts. |
| 2856 | * This shouldn't effect compatibility as I believe embedded |
| 2857 | * control characters are supposed to be interpreted (maybe?) |
| 2858 | * and they don't display anything useful anyway. |
| 2859 | * |
| 2860 | * -- RDB |
| 2861 | */ |
| 2862 | if (c == '\012' || c == '\015') { |
| 2863 | term->termstate = TOPLEVEL; |
| 2864 | } else if (c == 0234 || c == '\007') { |
| 2865 | /* |
| 2866 | * These characters terminate the string; ST and BEL |
| 2867 | * terminate the sequence and trigger instant |
| 2868 | * processing of it, whereas ESC goes back to SEEN_ESC |
| 2869 | * mode unless it is followed by \, in which case it is |
| 2870 | * synonymous with ST in the first place. |
| 2871 | */ |
| 2872 | do_osc(term); |
| 2873 | term->termstate = TOPLEVEL; |
| 2874 | } else if (c == '\033') |
| 2875 | term->termstate = OSC_MAYBE_ST; |
| 2876 | else if (term->osc_strlen < OSC_STR_MAX) |
| 2877 | term->osc_string[term->osc_strlen++] = c; |
| 2878 | break; |
| 2879 | case SEEN_OSC_P: |
| 2880 | { |
| 2881 | int max = (term->osc_strlen == 0 ? 21 : 16); |
| 2882 | int val; |
| 2883 | if (c >= '0' && c <= '9') |
| 2884 | val = c - '0'; |
| 2885 | else if (c >= 'A' && c <= 'A' + max - 10) |
| 2886 | val = c - 'A' + 10; |
| 2887 | else if (c >= 'a' && c <= 'a' + max - 10) |
| 2888 | val = c - 'a' + 10; |
| 2889 | else { |
| 2890 | term->termstate = TOPLEVEL; |
| 2891 | break; |
| 2892 | } |
| 2893 | term->osc_string[term->osc_strlen++] = val; |
| 2894 | if (term->osc_strlen >= 7) { |
| 2895 | palette_set(term->frontend, term->osc_string[0], |
| 2896 | term->osc_string[1] * 16 + term->osc_string[2], |
| 2897 | term->osc_string[3] * 16 + term->osc_string[4], |
| 2898 | term->osc_string[5] * 16 + term->osc_string[6]); |
| 2899 | term_invalidate(term); |
| 2900 | term->termstate = TOPLEVEL; |
| 2901 | } |
| 2902 | } |
| 2903 | break; |
| 2904 | case SEEN_OSC_W: |
| 2905 | switch (c) { |
| 2906 | case '0': |
| 2907 | case '1': |
| 2908 | case '2': |
| 2909 | case '3': |
| 2910 | case '4': |
| 2911 | case '5': |
| 2912 | case '6': |
| 2913 | case '7': |
| 2914 | case '8': |
| 2915 | case '9': |
| 2916 | term->esc_args[0] = 10 * term->esc_args[0] + c - '0'; |
| 2917 | break; |
| 2918 | default: |
| 2919 | term->termstate = OSC_STRING; |
| 2920 | term->osc_strlen = 0; |
| 2921 | } |
| 2922 | break; |
| 2923 | case VT52_ESC: |
| 2924 | term->termstate = TOPLEVEL; |
| 2925 | term->seen_disp_event = TRUE; |
| 2926 | switch (c) { |
| 2927 | case 'A': |
| 2928 | move(term, term->curs.x, term->curs.y - 1, 1); |
| 2929 | break; |
| 2930 | case 'B': |
| 2931 | move(term, term->curs.x, term->curs.y + 1, 1); |
| 2932 | break; |
| 2933 | case 'C': |
| 2934 | move(term, term->curs.x + 1, term->curs.y, 1); |
| 2935 | break; |
| 2936 | case 'D': |
| 2937 | move(term, term->curs.x - 1, term->curs.y, 1); |
| 2938 | break; |
| 2939 | /* |
| 2940 | * From the VT100 Manual |
| 2941 | * NOTE: The special graphics characters in the VT100 |
| 2942 | * are different from those in the VT52 |
| 2943 | * |
| 2944 | * From VT102 manual: |
| 2945 | * 137 _ Blank - Same |
| 2946 | * 140 ` Reserved - Humm. |
| 2947 | * 141 a Solid rectangle - Similar |
| 2948 | * 142 b 1/ - Top half of fraction for the |
| 2949 | * 143 c 3/ - subscript numbers below. |
| 2950 | * 144 d 5/ |
| 2951 | * 145 e 7/ |
| 2952 | * 146 f Degrees - Same |
| 2953 | * 147 g Plus or minus - Same |
| 2954 | * 150 h Right arrow |
| 2955 | * 151 i Ellipsis (dots) |
| 2956 | * 152 j Divide by |
| 2957 | * 153 k Down arrow |
| 2958 | * 154 l Bar at scan 0 |
| 2959 | * 155 m Bar at scan 1 |
| 2960 | * 156 n Bar at scan 2 |
| 2961 | * 157 o Bar at scan 3 - Similar |
| 2962 | * 160 p Bar at scan 4 - Similar |
| 2963 | * 161 q Bar at scan 5 - Similar |
| 2964 | * 162 r Bar at scan 6 - Same |
| 2965 | * 163 s Bar at scan 7 - Similar |
| 2966 | * 164 t Subscript 0 |
| 2967 | * 165 u Subscript 1 |
| 2968 | * 166 v Subscript 2 |
| 2969 | * 167 w Subscript 3 |
| 2970 | * 170 x Subscript 4 |
| 2971 | * 171 y Subscript 5 |
| 2972 | * 172 z Subscript 6 |
| 2973 | * 173 { Subscript 7 |
| 2974 | * 174 | Subscript 8 |
| 2975 | * 175 } Subscript 9 |
| 2976 | * 176 ~ Paragraph |
| 2977 | * |
| 2978 | */ |
| 2979 | case 'F': |
| 2980 | term->cset_attr[term->cset = 0] = ATTR_LINEDRW; |
| 2981 | break; |
| 2982 | case 'G': |
| 2983 | term->cset_attr[term->cset = 0] = ATTR_ASCII; |
| 2984 | break; |
| 2985 | case 'H': |
| 2986 | move(term, 0, 0, 0); |
| 2987 | break; |
| 2988 | case 'I': |
| 2989 | if (term->curs.y == 0) |
| 2990 | scroll(term, 0, term->rows - 1, -1, TRUE); |
| 2991 | else if (term->curs.y > 0) |
| 2992 | term->curs.y--; |
| 2993 | fix_cpos; |
| 2994 | term->wrapnext = FALSE; |
| 2995 | break; |
| 2996 | case 'J': |
| 2997 | erase_lots(term, FALSE, FALSE, TRUE); |
| 2998 | term->disptop = 0; |
| 2999 | break; |
| 3000 | case 'K': |
| 3001 | erase_lots(term, TRUE, FALSE, TRUE); |
| 3002 | break; |
| 3003 | #if 0 |
| 3004 | case 'V': |
| 3005 | /* XXX Print cursor line */ |
| 3006 | break; |
| 3007 | case 'W': |
| 3008 | /* XXX Start controller mode */ |
| 3009 | break; |
| 3010 | case 'X': |
| 3011 | /* XXX Stop controller mode */ |
| 3012 | break; |
| 3013 | #endif |
| 3014 | case 'Y': |
| 3015 | term->termstate = VT52_Y1; |
| 3016 | break; |
| 3017 | case 'Z': |
| 3018 | if (term->ldisc) |
| 3019 | ldisc_send(term->ldisc, "\033/Z", 3, 0); |
| 3020 | break; |
| 3021 | case '=': |
| 3022 | term->app_keypad_keys = TRUE; |
| 3023 | break; |
| 3024 | case '>': |
| 3025 | term->app_keypad_keys = FALSE; |
| 3026 | break; |
| 3027 | case '<': |
| 3028 | /* XXX This should switch to VT100 mode not current or default |
| 3029 | * VT mode. But this will only have effect in a VT220+ |
| 3030 | * emulation. |
| 3031 | */ |
| 3032 | term->vt52_mode = FALSE; |
| 3033 | term->blink_is_real = term->cfg.blinktext; |
| 3034 | break; |
| 3035 | #if 0 |
| 3036 | case '^': |
| 3037 | /* XXX Enter auto print mode */ |
| 3038 | break; |
| 3039 | case '_': |
| 3040 | /* XXX Exit auto print mode */ |
| 3041 | break; |
| 3042 | case ']': |
| 3043 | /* XXX Print screen */ |
| 3044 | break; |
| 3045 | #endif |
| 3046 | |
| 3047 | #ifdef VT52_PLUS |
| 3048 | case 'E': |
| 3049 | /* compatibility(ATARI) */ |
| 3050 | move(term, 0, 0, 0); |
| 3051 | erase_lots(term, FALSE, FALSE, TRUE); |
| 3052 | term->disptop = 0; |
| 3053 | break; |
| 3054 | case 'L': |
| 3055 | /* compatibility(ATARI) */ |
| 3056 | if (term->curs.y <= term->marg_b) |
| 3057 | scroll(term, term->curs.y, term->marg_b, -1, FALSE); |
| 3058 | break; |
| 3059 | case 'M': |
| 3060 | /* compatibility(ATARI) */ |
| 3061 | if (term->curs.y <= term->marg_b) |
| 3062 | scroll(term, term->curs.y, term->marg_b, 1, TRUE); |
| 3063 | break; |
| 3064 | case 'b': |
| 3065 | /* compatibility(ATARI) */ |
| 3066 | term->termstate = VT52_FG; |
| 3067 | break; |
| 3068 | case 'c': |
| 3069 | /* compatibility(ATARI) */ |
| 3070 | term->termstate = VT52_BG; |
| 3071 | break; |
| 3072 | case 'd': |
| 3073 | /* compatibility(ATARI) */ |
| 3074 | erase_lots(term, FALSE, TRUE, FALSE); |
| 3075 | term->disptop = 0; |
| 3076 | break; |
| 3077 | case 'e': |
| 3078 | /* compatibility(ATARI) */ |
| 3079 | term->cursor_on = TRUE; |
| 3080 | break; |
| 3081 | case 'f': |
| 3082 | /* compatibility(ATARI) */ |
| 3083 | term->cursor_on = FALSE; |
| 3084 | break; |
| 3085 | /* case 'j': Save cursor position - broken on ST */ |
| 3086 | /* case 'k': Restore cursor position */ |
| 3087 | case 'l': |
| 3088 | /* compatibility(ATARI) */ |
| 3089 | erase_lots(term, TRUE, TRUE, TRUE); |
| 3090 | term->curs.x = 0; |
| 3091 | term->wrapnext = FALSE; |
| 3092 | fix_cpos; |
| 3093 | break; |
| 3094 | case 'o': |
| 3095 | /* compatibility(ATARI) */ |
| 3096 | erase_lots(term, TRUE, TRUE, FALSE); |
| 3097 | break; |
| 3098 | case 'p': |
| 3099 | /* compatibility(ATARI) */ |
| 3100 | term->curr_attr |= ATTR_REVERSE; |
| 3101 | break; |
| 3102 | case 'q': |
| 3103 | /* compatibility(ATARI) */ |
| 3104 | term->curr_attr &= ~ATTR_REVERSE; |
| 3105 | break; |
| 3106 | case 'v': /* wrap Autowrap on - Wyse style */ |
| 3107 | /* compatibility(ATARI) */ |
| 3108 | term->wrap = 1; |
| 3109 | break; |
| 3110 | case 'w': /* Autowrap off */ |
| 3111 | /* compatibility(ATARI) */ |
| 3112 | term->wrap = 0; |
| 3113 | break; |
| 3114 | |
| 3115 | case 'R': |
| 3116 | /* compatibility(OTHER) */ |
| 3117 | term->vt52_bold = FALSE; |
| 3118 | term->curr_attr = ATTR_DEFAULT; |
| 3119 | if (term->use_bce) |
| 3120 | term->erase_char = (' ' | ATTR_ASCII | |
| 3121 | (term->curr_attr & |
| 3122 | (ATTR_FGMASK | ATTR_BGMASK))); |
| 3123 | break; |
| 3124 | case 'S': |
| 3125 | /* compatibility(VI50) */ |
| 3126 | term->curr_attr |= ATTR_UNDER; |
| 3127 | break; |
| 3128 | case 'W': |
| 3129 | /* compatibility(VI50) */ |
| 3130 | term->curr_attr &= ~ATTR_UNDER; |
| 3131 | break; |
| 3132 | case 'U': |
| 3133 | /* compatibility(VI50) */ |
| 3134 | term->vt52_bold = TRUE; |
| 3135 | term->curr_attr |= ATTR_BOLD; |
| 3136 | break; |
| 3137 | case 'T': |
| 3138 | /* compatibility(VI50) */ |
| 3139 | term->vt52_bold = FALSE; |
| 3140 | term->curr_attr &= ~ATTR_BOLD; |
| 3141 | break; |
| 3142 | #endif |
| 3143 | } |
| 3144 | break; |
| 3145 | case VT52_Y1: |
| 3146 | term->termstate = VT52_Y2; |
| 3147 | move(term, term->curs.x, c - ' ', 0); |
| 3148 | break; |
| 3149 | case VT52_Y2: |
| 3150 | term->termstate = TOPLEVEL; |
| 3151 | move(term, c - ' ', term->curs.y, 0); |
| 3152 | break; |
| 3153 | |
| 3154 | #ifdef VT52_PLUS |
| 3155 | case VT52_FG: |
| 3156 | term->termstate = TOPLEVEL; |
| 3157 | term->curr_attr &= ~ATTR_FGMASK; |
| 3158 | term->curr_attr &= ~ATTR_BOLD; |
| 3159 | term->curr_attr |= (c & 0x7) << ATTR_FGSHIFT; |
| 3160 | if ((c & 0x8) || term->vt52_bold) |
| 3161 | term->curr_attr |= ATTR_BOLD; |
| 3162 | |
| 3163 | if (term->use_bce) |
| 3164 | term->erase_char = (' ' | ATTR_ASCII | |
| 3165 | (term->curr_attr & |
| 3166 | (ATTR_FGMASK | ATTR_BGMASK))); |
| 3167 | break; |
| 3168 | case VT52_BG: |
| 3169 | term->termstate = TOPLEVEL; |
| 3170 | term->curr_attr &= ~ATTR_BGMASK; |
| 3171 | term->curr_attr &= ~ATTR_BLINK; |
| 3172 | term->curr_attr |= (c & 0x7) << ATTR_BGSHIFT; |
| 3173 | |
| 3174 | /* Note: bold background */ |
| 3175 | if (c & 0x8) |
| 3176 | term->curr_attr |= ATTR_BLINK; |
| 3177 | |
| 3178 | if (term->use_bce) |
| 3179 | term->erase_char = (' ' | ATTR_ASCII | |
| 3180 | (term->curr_attr & |
| 3181 | (ATTR_FGMASK | ATTR_BGMASK))); |
| 3182 | break; |
| 3183 | #endif |
| 3184 | default: break; /* placate gcc warning about enum use */ |
| 3185 | } |
| 3186 | if (term->selstate != NO_SELECTION) { |
| 3187 | pos cursplus = term->curs; |
| 3188 | incpos(cursplus); |
| 3189 | check_selection(term, term->curs, cursplus); |
| 3190 | } |
| 3191 | } |
| 3192 | |
| 3193 | term_print_flush(term); |
| 3194 | } |
| 3195 | |
| 3196 | #if 0 |
| 3197 | /* |
| 3198 | * Compare two lines to determine whether they are sufficiently |
| 3199 | * alike to scroll-optimise one to the other. Return the degree of |
| 3200 | * similarity. |
| 3201 | */ |
| 3202 | static int linecmp(Terminal *term, unsigned long *a, unsigned long *b) |
| 3203 | { |
| 3204 | int i, n; |
| 3205 | |
| 3206 | for (i = n = 0; i < term->cols; i++) |
| 3207 | n += (*a++ == *b++); |
| 3208 | return n; |
| 3209 | } |
| 3210 | #endif |
| 3211 | |
| 3212 | /* |
| 3213 | * Given a context, update the window. Out of paranoia, we don't |
| 3214 | * allow WM_PAINT responses to do scrolling optimisations. |
| 3215 | */ |
| 3216 | static void do_paint(Terminal *term, Context ctx, int may_optimise) |
| 3217 | { |
| 3218 | int i, j, our_curs_y, our_curs_x; |
| 3219 | unsigned long rv, cursor; |
| 3220 | pos scrpos; |
| 3221 | char ch[1024]; |
| 3222 | long cursor_background = ERASE_CHAR; |
| 3223 | unsigned long ticks; |
| 3224 | #ifdef OPTIMISE_SCROLL |
| 3225 | struct scrollregion *sr; |
| 3226 | #endif /* OPTIMISE_SCROLL */ |
| 3227 | |
| 3228 | /* |
| 3229 | * Check the visual bell state. |
| 3230 | */ |
| 3231 | if (term->in_vbell) { |
| 3232 | ticks = GETTICKCOUNT(); |
| 3233 | if (ticks - term->vbell_startpoint >= VBELL_TIMEOUT) |
| 3234 | term->in_vbell = FALSE; |
| 3235 | } |
| 3236 | |
| 3237 | rv = (!term->rvideo ^ !term->in_vbell ? ATTR_REVERSE : 0); |
| 3238 | |
| 3239 | /* Depends on: |
| 3240 | * screen array, disptop, scrtop, |
| 3241 | * selection, rv, |
| 3242 | * cfg.blinkpc, blink_is_real, tblinker, |
| 3243 | * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext |
| 3244 | */ |
| 3245 | |
| 3246 | /* Has the cursor position or type changed ? */ |
| 3247 | if (term->cursor_on) { |
| 3248 | if (term->has_focus) { |
| 3249 | if (term->blinker || !term->cfg.blink_cur) |
| 3250 | cursor = TATTR_ACTCURS; |
| 3251 | else |
| 3252 | cursor = 0; |
| 3253 | } else |
| 3254 | cursor = TATTR_PASCURS; |
| 3255 | if (term->wrapnext) |
| 3256 | cursor |= TATTR_RIGHTCURS; |
| 3257 | } else |
| 3258 | cursor = 0; |
| 3259 | our_curs_y = term->curs.y - term->disptop; |
| 3260 | { |
| 3261 | /* |
| 3262 | * Adjust the cursor position in the case where it's |
| 3263 | * resting on the right-hand half of a CJK wide character. |
| 3264 | * xterm's behaviour here, which seems adequate to me, is |
| 3265 | * to display the cursor covering the _whole_ character, |
| 3266 | * exactly as if it were one space to the left. |
| 3267 | */ |
| 3268 | unsigned long *ldata = lineptr(term->curs.y); |
| 3269 | our_curs_x = term->curs.x; |
| 3270 | if (our_curs_x > 0 && |
| 3271 | (ldata[our_curs_x] & (CHAR_MASK | CSET_MASK)) == UCSWIDE) |
| 3272 | our_curs_x--; |
| 3273 | } |
| 3274 | |
| 3275 | if (term->dispcurs && (term->curstype != cursor || |
| 3276 | term->dispcurs != |
| 3277 | term->disptext + our_curs_y * (term->cols + 1) + |
| 3278 | our_curs_x)) { |
| 3279 | if (term->dispcurs > term->disptext && |
| 3280 | (*term->dispcurs & (CHAR_MASK | CSET_MASK)) == UCSWIDE) |
| 3281 | term->dispcurs[-1] |= ATTR_INVALID; |
| 3282 | if ( (term->dispcurs[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE) |
| 3283 | term->dispcurs[1] |= ATTR_INVALID; |
| 3284 | *term->dispcurs |= ATTR_INVALID; |
| 3285 | term->curstype = 0; |
| 3286 | } |
| 3287 | term->dispcurs = NULL; |
| 3288 | |
| 3289 | #ifdef OPTIMISE_SCROLL |
| 3290 | /* Do scrolls */ |
| 3291 | sr = term->scrollhead; |
| 3292 | while (sr) { |
| 3293 | struct scrollregion *next = sr->next; |
| 3294 | do_scroll(ctx, sr->topline, sr->botline, sr->lines); |
| 3295 | sfree(sr); |
| 3296 | sr = next; |
| 3297 | } |
| 3298 | term->scrollhead = term->scrolltail = NULL; |
| 3299 | #endif /* OPTIMISE_SCROLL */ |
| 3300 | |
| 3301 | /* The normal screen data */ |
| 3302 | for (i = 0; i < term->rows; i++) { |
| 3303 | unsigned long *ldata; |
| 3304 | int lattr; |
| 3305 | int idx, dirty_line, dirty_run, selected; |
| 3306 | unsigned long attr = 0; |
| 3307 | int updated_line = 0; |
| 3308 | int start = 0; |
| 3309 | int ccount = 0; |
| 3310 | int last_run_dirty = 0; |
| 3311 | |
| 3312 | scrpos.y = i + term->disptop; |
| 3313 | ldata = lineptr(scrpos.y); |
| 3314 | lattr = (ldata[term->cols] & LATTR_MODE); |
| 3315 | |
| 3316 | idx = i * (term->cols + 1); |
| 3317 | dirty_run = dirty_line = (ldata[term->cols] != |
| 3318 | term->disptext[idx + term->cols]); |
| 3319 | term->disptext[idx + term->cols] = ldata[term->cols]; |
| 3320 | |
| 3321 | for (j = 0; j < term->cols; j++, idx++) { |
| 3322 | unsigned long tattr, tchar; |
| 3323 | unsigned long *d = ldata + j; |
| 3324 | int break_run; |
| 3325 | scrpos.x = j; |
| 3326 | |
| 3327 | tchar = (*d & (CHAR_MASK | CSET_MASK)); |
| 3328 | tattr = (*d & (ATTR_MASK ^ CSET_MASK)); |
| 3329 | switch (tchar & CSET_MASK) { |
| 3330 | case ATTR_ASCII: |
| 3331 | tchar = term->ucsdata->unitab_line[tchar & 0xFF]; |
| 3332 | break; |
| 3333 | case ATTR_LINEDRW: |
| 3334 | tchar = term->ucsdata->unitab_xterm[tchar & 0xFF]; |
| 3335 | break; |
| 3336 | case ATTR_SCOACS: |
| 3337 | tchar = term->ucsdata->unitab_scoacs[tchar&0xFF]; |
| 3338 | break; |
| 3339 | } |
| 3340 | tattr |= (tchar & CSET_MASK); |
| 3341 | tchar &= CHAR_MASK; |
| 3342 | if ((d[1] & (CHAR_MASK | CSET_MASK)) == UCSWIDE) |
| 3343 | tattr |= ATTR_WIDE; |
| 3344 | |
| 3345 | /* Video reversing things */ |
| 3346 | if (term->selstate == DRAGGING || term->selstate == SELECTED) { |
| 3347 | if (term->seltype == LEXICOGRAPHIC) |
| 3348 | selected = (posle(term->selstart, scrpos) && |
| 3349 | poslt(scrpos, term->selend)); |
| 3350 | else |
| 3351 | selected = (posPle(term->selstart, scrpos) && |
| 3352 | posPlt(scrpos, term->selend)); |
| 3353 | } else |
| 3354 | selected = FALSE; |
| 3355 | tattr = (tattr ^ rv |
| 3356 | ^ (selected ? ATTR_REVERSE : 0)); |
| 3357 | |
| 3358 | /* 'Real' blinking ? */ |
| 3359 | if (term->blink_is_real && (tattr & ATTR_BLINK)) { |
| 3360 | if (term->has_focus && term->tblinker) { |
| 3361 | tchar = term->ucsdata->unitab_line[(unsigned char)' ']; |
| 3362 | } |
| 3363 | tattr &= ~ATTR_BLINK; |
| 3364 | } |
| 3365 | |
| 3366 | /* |
| 3367 | * Check the font we'll _probably_ be using to see if |
| 3368 | * the character is wide when we don't want it to be. |
| 3369 | */ |
| 3370 | if ((tchar | tattr) != (term->disptext[idx]& ~ATTR_NARROW)) { |
| 3371 | if ((tattr & ATTR_WIDE) == 0 && |
| 3372 | char_width(ctx, (tchar | tattr) & 0xFFFF) == 2) |
| 3373 | tattr |= ATTR_NARROW; |
| 3374 | } else if (term->disptext[idx]&ATTR_NARROW) |
| 3375 | tattr |= ATTR_NARROW; |
| 3376 | |
| 3377 | /* Cursor here ? Save the 'background' */ |
| 3378 | if (i == our_curs_y && j == our_curs_x) { |
| 3379 | cursor_background = tattr | tchar; |
| 3380 | term->dispcurs = term->disptext + idx; |
| 3381 | } |
| 3382 | |
| 3383 | if ((term->disptext[idx] ^ tattr) & ATTR_WIDE) |
| 3384 | dirty_line = TRUE; |
| 3385 | |
| 3386 | break_run = (((tattr ^ attr) & term->attr_mask) || |
| 3387 | j - start >= sizeof(ch)); |
| 3388 | |
| 3389 | /* Special hack for VT100 Linedraw glyphs */ |
| 3390 | if ((attr & CSET_MASK) == 0x2300 && tchar >= 0xBA |
| 3391 | && tchar <= 0xBD) break_run = TRUE; |
| 3392 | |
| 3393 | if (!term->ucsdata->dbcs_screenfont && !dirty_line) { |
| 3394 | if ((tchar | tattr) == term->disptext[idx]) |
| 3395 | break_run = TRUE; |
| 3396 | else if (!dirty_run && ccount == 1) |
| 3397 | break_run = TRUE; |
| 3398 | } |
| 3399 | |
| 3400 | if (break_run) { |
| 3401 | if ((dirty_run || last_run_dirty) && ccount > 0) { |
| 3402 | do_text(ctx, start, i, ch, ccount, attr, lattr); |
| 3403 | updated_line = 1; |
| 3404 | } |
| 3405 | start = j; |
| 3406 | ccount = 0; |
| 3407 | attr = tattr; |
| 3408 | if (term->ucsdata->dbcs_screenfont) |
| 3409 | last_run_dirty = dirty_run; |
| 3410 | dirty_run = dirty_line; |
| 3411 | } |
| 3412 | |
| 3413 | if ((tchar | tattr) != term->disptext[idx]) |
| 3414 | dirty_run = TRUE; |
| 3415 | ch[ccount++] = (char) tchar; |
| 3416 | term->disptext[idx] = tchar | tattr; |
| 3417 | |
| 3418 | /* If it's a wide char step along to the next one. */ |
| 3419 | if (tattr & ATTR_WIDE) { |
| 3420 | if (++j < term->cols) { |
| 3421 | idx++; |
| 3422 | d++; |
| 3423 | /* |
| 3424 | * By construction above, the cursor should not |
| 3425 | * be on the right-hand half of this character. |
| 3426 | * Ever. |
| 3427 | */ |
| 3428 | assert(!(i == our_curs_y && j == our_curs_x)); |
| 3429 | if (term->disptext[idx] != *d) |
| 3430 | dirty_run = TRUE; |
| 3431 | term->disptext[idx] = *d; |
| 3432 | } |
| 3433 | } |
| 3434 | } |
| 3435 | if (dirty_run && ccount > 0) { |
| 3436 | do_text(ctx, start, i, ch, ccount, attr, lattr); |
| 3437 | updated_line = 1; |
| 3438 | } |
| 3439 | |
| 3440 | /* Cursor on this line ? (and changed) */ |
| 3441 | if (i == our_curs_y && (term->curstype != cursor || updated_line)) { |
| 3442 | ch[0] = (char) (cursor_background & CHAR_MASK); |
| 3443 | attr = (cursor_background & ATTR_MASK) | cursor; |
| 3444 | do_cursor(ctx, our_curs_x, i, ch, 1, attr, lattr); |
| 3445 | term->curstype = cursor; |
| 3446 | } |
| 3447 | } |
| 3448 | } |
| 3449 | |
| 3450 | /* |
| 3451 | * Flick the switch that says if blinking things should be shown or hidden. |
| 3452 | */ |
| 3453 | |
| 3454 | void term_blink(Terminal *term, int flg) |
| 3455 | { |
| 3456 | long now, blink_diff; |
| 3457 | |
| 3458 | now = GETTICKCOUNT(); |
| 3459 | blink_diff = now - term->last_tblink; |
| 3460 | |
| 3461 | /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */ |
| 3462 | if (blink_diff < 0 || blink_diff > (TICKSPERSEC * 9 / 20)) { |
| 3463 | term->last_tblink = now; |
| 3464 | term->tblinker = !term->tblinker; |
| 3465 | } |
| 3466 | |
| 3467 | if (flg) { |
| 3468 | term->blinker = 1; |
| 3469 | term->last_blink = now; |
| 3470 | return; |
| 3471 | } |
| 3472 | |
| 3473 | blink_diff = now - term->last_blink; |
| 3474 | |
| 3475 | /* Make sure the cursor blinks no faster than system blink rate */ |
| 3476 | if (blink_diff >= 0 && blink_diff < (long) CURSORBLINK) |
| 3477 | return; |
| 3478 | |
| 3479 | term->last_blink = now; |
| 3480 | term->blinker = !term->blinker; |
| 3481 | } |
| 3482 | |
| 3483 | /* |
| 3484 | * Invalidate the whole screen so it will be repainted in full. |
| 3485 | */ |
| 3486 | void term_invalidate(Terminal *term) |
| 3487 | { |
| 3488 | int i; |
| 3489 | |
| 3490 | for (i = 0; i < term->rows * (term->cols + 1); i++) |
| 3491 | term->disptext[i] = ATTR_INVALID; |
| 3492 | } |
| 3493 | |
| 3494 | /* |
| 3495 | * Paint the window in response to a WM_PAINT message. |
| 3496 | */ |
| 3497 | void term_paint(Terminal *term, Context ctx, |
| 3498 | int left, int top, int right, int bottom, int immediately) |
| 3499 | { |
| 3500 | int i, j; |
| 3501 | if (left < 0) left = 0; |
| 3502 | if (top < 0) top = 0; |
| 3503 | if (right >= term->cols) right = term->cols-1; |
| 3504 | if (bottom >= term->rows) bottom = term->rows-1; |
| 3505 | |
| 3506 | for (i = top; i <= bottom && i < term->rows; i++) { |
| 3507 | if ((term->disptext[i * (term->cols + 1) + term->cols] & |
| 3508 | LATTR_MODE) == LATTR_NORM) |
| 3509 | for (j = left; j <= right && j < term->cols; j++) |
| 3510 | term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID; |
| 3511 | else |
| 3512 | for (j = left / 2; j <= right / 2 + 1 && j < term->cols; j++) |
| 3513 | term->disptext[i * (term->cols + 1) + j] = ATTR_INVALID; |
| 3514 | } |
| 3515 | |
| 3516 | /* This should happen soon enough, also for some reason it sometimes |
| 3517 | * fails to actually do anything when re-sizing ... painting the wrong |
| 3518 | * window perhaps ? |
| 3519 | */ |
| 3520 | if (immediately) |
| 3521 | do_paint (term, ctx, FALSE); |
| 3522 | } |
| 3523 | |
| 3524 | /* |
| 3525 | * Attempt to scroll the scrollback. The second parameter gives the |
| 3526 | * position we want to scroll to; the first is +1 to denote that |
| 3527 | * this position is relative to the beginning of the scrollback, -1 |
| 3528 | * to denote it is relative to the end, and 0 to denote that it is |
| 3529 | * relative to the current position. |
| 3530 | */ |
| 3531 | void term_scroll(Terminal *term, int rel, int where) |
| 3532 | { |
| 3533 | int sbtop = -sblines(term); |
| 3534 | #ifdef OPTIMISE_SCROLL |
| 3535 | int olddisptop = term->disptop; |
| 3536 | int shift; |
| 3537 | #endif /* OPTIMISE_SCROLL */ |
| 3538 | |
| 3539 | term->disptop = (rel < 0 ? 0 : rel > 0 ? sbtop : term->disptop) + where; |
| 3540 | if (term->disptop < sbtop) |
| 3541 | term->disptop = sbtop; |
| 3542 | if (term->disptop > 0) |
| 3543 | term->disptop = 0; |
| 3544 | update_sbar(term); |
| 3545 | #ifdef OPTIMISE_SCROLL |
| 3546 | shift = (term->disptop - olddisptop); |
| 3547 | if (shift < term->rows && shift > -term->rows) |
| 3548 | scroll_display(term, 0, term->rows - 1, shift); |
| 3549 | #endif /* OPTIMISE_SCROLL */ |
| 3550 | term_update(term); |
| 3551 | } |
| 3552 | |
| 3553 | static void clipme(Terminal *term, pos top, pos bottom, int rect) |
| 3554 | { |
| 3555 | wchar_t *workbuf; |
| 3556 | wchar_t *wbptr; /* where next char goes within workbuf */ |
| 3557 | int old_top_x; |
| 3558 | int wblen = 0; /* workbuf len */ |
| 3559 | int buflen; /* amount of memory allocated to workbuf */ |
| 3560 | |
| 3561 | buflen = 5120; /* Default size */ |
| 3562 | workbuf = smalloc(buflen * sizeof(wchar_t)); |
| 3563 | wbptr = workbuf; /* start filling here */ |
| 3564 | old_top_x = top.x; /* needed for rect==1 */ |
| 3565 | |
| 3566 | while (poslt(top, bottom)) { |
| 3567 | int nl = FALSE; |
| 3568 | unsigned long *ldata = lineptr(top.y); |
| 3569 | pos nlpos; |
| 3570 | |
| 3571 | /* |
| 3572 | * nlpos will point at the maximum position on this line we |
| 3573 | * should copy up to. So we start it at the end of the |
| 3574 | * line... |
| 3575 | */ |
| 3576 | nlpos.y = top.y; |
| 3577 | nlpos.x = term->cols; |
| 3578 | |
| 3579 | /* |
| 3580 | * ... move it backwards if there's unused space at the end |
| 3581 | * of the line (and also set `nl' if this is the case, |
| 3582 | * because in normal selection mode this means we need a |
| 3583 | * newline at the end)... |
| 3584 | */ |
| 3585 | if (!(ldata[term->cols] & LATTR_WRAPPED)) { |
| 3586 | while (((ldata[nlpos.x - 1] & 0xFF) == 0x20 || |
| 3587 | (DIRECT_CHAR(ldata[nlpos.x - 1]) && |
| 3588 | (ldata[nlpos.x - 1] & CHAR_MASK) == 0x20)) |
| 3589 | && poslt(top, nlpos)) |
| 3590 | decpos(nlpos); |
| 3591 | if (poslt(nlpos, bottom)) |
| 3592 | nl = TRUE; |
| 3593 | } else if (ldata[term->cols] & LATTR_WRAPPED2) { |
| 3594 | /* Ignore the last char on the line in a WRAPPED2 line. */ |
| 3595 | decpos(nlpos); |
| 3596 | } |
| 3597 | |
| 3598 | /* |
| 3599 | * ... and then clip it to the terminal x coordinate if |
| 3600 | * we're doing rectangular selection. (In this case we |
| 3601 | * still did the above, so that copying e.g. the right-hand |
| 3602 | * column from a table doesn't fill with spaces on the |
| 3603 | * right.) |
| 3604 | */ |
| 3605 | if (rect) { |
| 3606 | if (nlpos.x > bottom.x) |
| 3607 | nlpos.x = bottom.x; |
| 3608 | nl = (top.y < bottom.y); |
| 3609 | } |
| 3610 | |
| 3611 | while (poslt(top, bottom) && poslt(top, nlpos)) { |
| 3612 | #if 0 |
| 3613 | char cbuf[16], *p; |
| 3614 | sprintf(cbuf, "<U+%04x>", (ldata[top.x] & 0xFFFF)); |
| 3615 | #else |
| 3616 | wchar_t cbuf[16], *p; |
| 3617 | int uc = (ldata[top.x] & 0xFFFF); |
| 3618 | int set, c; |
| 3619 | |
| 3620 | if (uc == UCSWIDE) { |
| 3621 | top.x++; |
| 3622 | continue; |
| 3623 | } |
| 3624 | |
| 3625 | switch (uc & CSET_MASK) { |
| 3626 | case ATTR_LINEDRW: |
| 3627 | if (!term->cfg.rawcnp) { |
| 3628 | uc = term->ucsdata->unitab_xterm[uc & 0xFF]; |
| 3629 | break; |
| 3630 | } |
| 3631 | case ATTR_ASCII: |
| 3632 | uc = term->ucsdata->unitab_line[uc & 0xFF]; |
| 3633 | break; |
| 3634 | case ATTR_SCOACS: |
| 3635 | uc = term->ucsdata->unitab_scoacs[uc&0xFF]; |
| 3636 | break; |
| 3637 | } |
| 3638 | switch (uc & CSET_MASK) { |
| 3639 | case ATTR_ACP: |
| 3640 | uc = term->ucsdata->unitab_font[uc & 0xFF]; |
| 3641 | break; |
| 3642 | case ATTR_OEMCP: |
| 3643 | uc = term->ucsdata->unitab_oemcp[uc & 0xFF]; |
| 3644 | break; |
| 3645 | } |
| 3646 | |
| 3647 | set = (uc & CSET_MASK); |
| 3648 | c = (uc & CHAR_MASK); |
| 3649 | cbuf[0] = uc; |
| 3650 | cbuf[1] = 0; |
| 3651 | |
| 3652 | if (DIRECT_FONT(uc)) { |
| 3653 | if (c >= ' ' && c != 0x7F) { |
| 3654 | char buf[4]; |
| 3655 | WCHAR wbuf[4]; |
| 3656 | int rv; |
| 3657 | if (is_dbcs_leadbyte(term->ucsdata->font_codepage, (BYTE) c)) { |
| 3658 | buf[0] = c; |
| 3659 | buf[1] = (char) (0xFF & ldata[top.x + 1]); |
| 3660 | rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 2, wbuf, 4); |
| 3661 | top.x++; |
| 3662 | } else { |
| 3663 | buf[0] = c; |
| 3664 | rv = mb_to_wc(term->ucsdata->font_codepage, 0, buf, 1, wbuf, 4); |
| 3665 | } |
| 3666 | |
| 3667 | if (rv > 0) { |
| 3668 | memcpy(cbuf, wbuf, rv * sizeof(wchar_t)); |
| 3669 | cbuf[rv] = 0; |
| 3670 | } |
| 3671 | } |
| 3672 | } |
| 3673 | #endif |
| 3674 | |
| 3675 | for (p = cbuf; *p; p++) { |
| 3676 | /* Enough overhead for trailing NL and nul */ |
| 3677 | if (wblen >= buflen - 16) { |
| 3678 | workbuf = |
| 3679 | srealloc(workbuf, |
| 3680 | sizeof(wchar_t) * (buflen += 100)); |
| 3681 | wbptr = workbuf + wblen; |
| 3682 | } |
| 3683 | wblen++; |
| 3684 | *wbptr++ = *p; |
| 3685 | } |
| 3686 | top.x++; |
| 3687 | } |
| 3688 | if (nl) { |
| 3689 | int i; |
| 3690 | for (i = 0; i < sel_nl_sz; i++) { |
| 3691 | wblen++; |
| 3692 | *wbptr++ = sel_nl[i]; |
| 3693 | } |
| 3694 | } |
| 3695 | top.y++; |
| 3696 | top.x = rect ? old_top_x : 0; |
| 3697 | } |
| 3698 | #if SELECTION_NUL_TERMINATED |
| 3699 | wblen++; |
| 3700 | *wbptr++ = 0; |
| 3701 | #endif |
| 3702 | write_clip(term->frontend, workbuf, wblen, FALSE); /* transfer to clipbd */ |
| 3703 | if (buflen > 0) /* indicates we allocated this buffer */ |
| 3704 | sfree(workbuf); |
| 3705 | } |
| 3706 | |
| 3707 | void term_copyall(Terminal *term) |
| 3708 | { |
| 3709 | pos top; |
| 3710 | top.y = -sblines(term); |
| 3711 | top.x = 0; |
| 3712 | clipme(term, top, term->curs, 0); |
| 3713 | } |
| 3714 | |
| 3715 | /* |
| 3716 | * The wordness array is mainly for deciding the disposition of the |
| 3717 | * US-ASCII characters. |
| 3718 | */ |
| 3719 | static int wordtype(Terminal *term, int uc) |
| 3720 | { |
| 3721 | struct ucsword { |
| 3722 | int start, end, ctype; |
| 3723 | }; |
| 3724 | static const struct ucsword ucs_words[] = { |
| 3725 | { |
| 3726 | 128, 160, 0}, { |
| 3727 | 161, 191, 1}, { |
| 3728 | 215, 215, 1}, { |
| 3729 | 247, 247, 1}, { |
| 3730 | 0x037e, 0x037e, 1}, /* Greek question mark */ |
| 3731 | { |
| 3732 | 0x0387, 0x0387, 1}, /* Greek ano teleia */ |
| 3733 | { |
| 3734 | 0x055a, 0x055f, 1}, /* Armenian punctuation */ |
| 3735 | { |
| 3736 | 0x0589, 0x0589, 1}, /* Armenian full stop */ |
| 3737 | { |
| 3738 | 0x0700, 0x070d, 1}, /* Syriac punctuation */ |
| 3739 | { |
| 3740 | 0x104a, 0x104f, 1}, /* Myanmar punctuation */ |
| 3741 | { |
| 3742 | 0x10fb, 0x10fb, 1}, /* Georgian punctuation */ |
| 3743 | { |
| 3744 | 0x1361, 0x1368, 1}, /* Ethiopic punctuation */ |
| 3745 | { |
| 3746 | 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */ |
| 3747 | { |
| 3748 | 0x17d4, 0x17dc, 1}, /* Khmer punctuation */ |
| 3749 | { |
| 3750 | 0x1800, 0x180a, 1}, /* Mongolian punctuation */ |
| 3751 | { |
| 3752 | 0x2000, 0x200a, 0}, /* Various spaces */ |
| 3753 | { |
| 3754 | 0x2070, 0x207f, 2}, /* superscript */ |
| 3755 | { |
| 3756 | 0x2080, 0x208f, 2}, /* subscript */ |
| 3757 | { |
| 3758 | 0x200b, 0x27ff, 1}, /* punctuation and symbols */ |
| 3759 | { |
| 3760 | 0x3000, 0x3000, 0}, /* ideographic space */ |
| 3761 | { |
| 3762 | 0x3001, 0x3020, 1}, /* ideographic punctuation */ |
| 3763 | { |
| 3764 | 0x303f, 0x309f, 3}, /* Hiragana */ |
| 3765 | { |
| 3766 | 0x30a0, 0x30ff, 3}, /* Katakana */ |
| 3767 | { |
| 3768 | 0x3300, 0x9fff, 3}, /* CJK Ideographs */ |
| 3769 | { |
| 3770 | 0xac00, 0xd7a3, 3}, /* Hangul Syllables */ |
| 3771 | { |
| 3772 | 0xf900, 0xfaff, 3}, /* CJK Ideographs */ |
| 3773 | { |
| 3774 | 0xfe30, 0xfe6b, 1}, /* punctuation forms */ |
| 3775 | { |
| 3776 | 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */ |
| 3777 | { |
| 3778 | 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */ |
| 3779 | { |
| 3780 | 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */ |
| 3781 | { |
| 3782 | 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */ |
| 3783 | { |
| 3784 | 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */ |
| 3785 | { |
| 3786 | 0, 0, 0} |
| 3787 | }; |
| 3788 | const struct ucsword *wptr; |
| 3789 | |
| 3790 | uc &= (CSET_MASK | CHAR_MASK); |
| 3791 | |
| 3792 | switch (uc & CSET_MASK) { |
| 3793 | case ATTR_LINEDRW: |
| 3794 | uc = term->ucsdata->unitab_xterm[uc & 0xFF]; |
| 3795 | break; |
| 3796 | case ATTR_ASCII: |
| 3797 | uc = term->ucsdata->unitab_line[uc & 0xFF]; |
| 3798 | break; |
| 3799 | case ATTR_SCOACS: |
| 3800 | uc = term->ucsdata->unitab_scoacs[uc&0xFF]; |
| 3801 | break; |
| 3802 | } |
| 3803 | switch (uc & CSET_MASK) { |
| 3804 | case ATTR_ACP: |
| 3805 | uc = term->ucsdata->unitab_font[uc & 0xFF]; |
| 3806 | break; |
| 3807 | case ATTR_OEMCP: |
| 3808 | uc = term->ucsdata->unitab_oemcp[uc & 0xFF]; |
| 3809 | break; |
| 3810 | } |
| 3811 | |
| 3812 | /* For DBCS font's I can't do anything usefull. Even this will sometimes |
| 3813 | * fail as there's such a thing as a double width space. :-( |
| 3814 | */ |
| 3815 | if (term->ucsdata->dbcs_screenfont && |
| 3816 | term->ucsdata->font_codepage == term->ucsdata->line_codepage) |
| 3817 | return (uc != ' '); |
| 3818 | |
| 3819 | if (uc < 0x80) |
| 3820 | return term->wordness[uc]; |
| 3821 | |
| 3822 | for (wptr = ucs_words; wptr->start; wptr++) { |
| 3823 | if (uc >= wptr->start && uc <= wptr->end) |
| 3824 | return wptr->ctype; |
| 3825 | } |
| 3826 | |
| 3827 | return 2; |
| 3828 | } |
| 3829 | |
| 3830 | /* |
| 3831 | * Spread the selection outwards according to the selection mode. |
| 3832 | */ |
| 3833 | static pos sel_spread_half(Terminal *term, pos p, int dir) |
| 3834 | { |
| 3835 | unsigned long *ldata; |
| 3836 | short wvalue; |
| 3837 | int topy = -sblines(term); |
| 3838 | |
| 3839 | ldata = lineptr(p.y); |
| 3840 | |
| 3841 | switch (term->selmode) { |
| 3842 | case SM_CHAR: |
| 3843 | /* |
| 3844 | * In this mode, every character is a separate unit, except |
| 3845 | * for runs of spaces at the end of a non-wrapping line. |
| 3846 | */ |
| 3847 | if (!(ldata[term->cols] & LATTR_WRAPPED)) { |
| 3848 | unsigned long *q = ldata + term->cols; |
| 3849 | while (q > ldata && (q[-1] & CHAR_MASK) == 0x20) |
| 3850 | q--; |
| 3851 | if (q == ldata + term->cols) |
| 3852 | q--; |
| 3853 | if (p.x >= q - ldata) |
| 3854 | p.x = (dir == -1 ? q - ldata : term->cols - 1); |
| 3855 | } |
| 3856 | break; |
| 3857 | case SM_WORD: |
| 3858 | /* |
| 3859 | * In this mode, the units are maximal runs of characters |
| 3860 | * whose `wordness' has the same value. |
| 3861 | */ |
| 3862 | wvalue = wordtype(term, UCSGET(ldata, p.x)); |
| 3863 | if (dir == +1) { |
| 3864 | while (1) { |
| 3865 | int maxcols = (ldata[term->cols] & LATTR_WRAPPED2 ? |
| 3866 | term->cols-1 : term->cols); |
| 3867 | if (p.x < maxcols-1) { |
| 3868 | if (wordtype(term, UCSGET(ldata, p.x + 1)) == wvalue) |
| 3869 | p.x++; |
| 3870 | else |
| 3871 | break; |
| 3872 | } else { |
| 3873 | if (ldata[term->cols] & LATTR_WRAPPED) { |
| 3874 | unsigned long *ldata2; |
| 3875 | ldata2 = lineptr(p.y+1); |
| 3876 | if (wordtype(term, UCSGET(ldata2, 0)) == wvalue) { |
| 3877 | p.x = 0; |
| 3878 | p.y++; |
| 3879 | ldata = ldata2; |
| 3880 | } else |
| 3881 | break; |
| 3882 | } else |
| 3883 | break; |
| 3884 | } |
| 3885 | } |
| 3886 | } else { |
| 3887 | while (1) { |
| 3888 | if (p.x > 0) { |
| 3889 | if (wordtype(term, UCSGET(ldata, p.x - 1)) == wvalue) |
| 3890 | p.x--; |
| 3891 | else |
| 3892 | break; |
| 3893 | } else { |
| 3894 | unsigned long *ldata2; |
| 3895 | int maxcols; |
| 3896 | if (p.y <= topy) |
| 3897 | break; |
| 3898 | ldata2 = lineptr(p.y-1); |
| 3899 | maxcols = (ldata2[term->cols] & LATTR_WRAPPED2 ? |
| 3900 | term->cols-1 : term->cols); |
| 3901 | if (ldata2[term->cols] & LATTR_WRAPPED) { |
| 3902 | if (wordtype(term, UCSGET(ldata2, maxcols-1)) |
| 3903 | == wvalue) { |
| 3904 | p.x = maxcols-1; |
| 3905 | p.y--; |
| 3906 | ldata = ldata2; |
| 3907 | } else |
| 3908 | break; |
| 3909 | } else |
| 3910 | break; |
| 3911 | } |
| 3912 | } |
| 3913 | } |
| 3914 | break; |
| 3915 | case SM_LINE: |
| 3916 | /* |
| 3917 | * In this mode, every line is a unit. |
| 3918 | */ |
| 3919 | p.x = (dir == -1 ? 0 : term->cols - 1); |
| 3920 | break; |
| 3921 | } |
| 3922 | return p; |
| 3923 | } |
| 3924 | |
| 3925 | static void sel_spread(Terminal *term) |
| 3926 | { |
| 3927 | if (term->seltype == LEXICOGRAPHIC) { |
| 3928 | term->selstart = sel_spread_half(term, term->selstart, -1); |
| 3929 | decpos(term->selend); |
| 3930 | term->selend = sel_spread_half(term, term->selend, +1); |
| 3931 | incpos(term->selend); |
| 3932 | } |
| 3933 | } |
| 3934 | |
| 3935 | void term_do_paste(Terminal *term) |
| 3936 | { |
| 3937 | wchar_t *data; |
| 3938 | int len; |
| 3939 | |
| 3940 | get_clip(term->frontend, &data, &len); |
| 3941 | if (data && len > 0) { |
| 3942 | wchar_t *p, *q; |
| 3943 | |
| 3944 | term_seen_key_event(term); /* pasted data counts */ |
| 3945 | |
| 3946 | if (term->paste_buffer) |
| 3947 | sfree(term->paste_buffer); |
| 3948 | term->paste_pos = term->paste_hold = term->paste_len = 0; |
| 3949 | term->paste_buffer = smalloc(len * sizeof(wchar_t)); |
| 3950 | |
| 3951 | p = q = data; |
| 3952 | while (p < data + len) { |
| 3953 | while (p < data + len && |
| 3954 | !(p <= data + len - sel_nl_sz && |
| 3955 | !memcmp(p, sel_nl, sizeof(sel_nl)))) |
| 3956 | p++; |
| 3957 | |
| 3958 | { |
| 3959 | int i; |
| 3960 | for (i = 0; i < p - q; i++) { |
| 3961 | term->paste_buffer[term->paste_len++] = q[i]; |
| 3962 | } |
| 3963 | } |
| 3964 | |
| 3965 | if (p <= data + len - sel_nl_sz && |
| 3966 | !memcmp(p, sel_nl, sizeof(sel_nl))) { |
| 3967 | term->paste_buffer[term->paste_len++] = '\015'; |
| 3968 | p += sel_nl_sz; |
| 3969 | } |
| 3970 | q = p; |
| 3971 | } |
| 3972 | |
| 3973 | /* Assume a small paste will be OK in one go. */ |
| 3974 | if (term->paste_len < 256) { |
| 3975 | if (term->ldisc) |
| 3976 | luni_send(term->ldisc, term->paste_buffer, term->paste_len, 0); |
| 3977 | if (term->paste_buffer) |
| 3978 | sfree(term->paste_buffer); |
| 3979 | term->paste_buffer = 0; |
| 3980 | term->paste_pos = term->paste_hold = term->paste_len = 0; |
| 3981 | } |
| 3982 | } |
| 3983 | get_clip(term->frontend, NULL, NULL); |
| 3984 | } |
| 3985 | |
| 3986 | void term_mouse(Terminal *term, Mouse_Button braw, Mouse_Button bcooked, |
| 3987 | Mouse_Action a, int x, int y, int shift, int ctrl, int alt) |
| 3988 | { |
| 3989 | pos selpoint; |
| 3990 | unsigned long *ldata; |
| 3991 | int raw_mouse = (term->xterm_mouse && |
| 3992 | !term->cfg.no_mouse_rep && |
| 3993 | !(term->cfg.mouse_override && shift)); |
| 3994 | int default_seltype; |
| 3995 | |
| 3996 | if (y < 0) { |
| 3997 | y = 0; |
| 3998 | if (a == MA_DRAG && !raw_mouse) |
| 3999 | term_scroll(term, 0, -1); |
| 4000 | } |
| 4001 | if (y >= term->rows) { |
| 4002 | y = term->rows - 1; |
| 4003 | if (a == MA_DRAG && !raw_mouse) |
| 4004 | term_scroll(term, 0, +1); |
| 4005 | } |
| 4006 | if (x < 0) { |
| 4007 | if (y > 0) { |
| 4008 | x = term->cols - 1; |
| 4009 | y--; |
| 4010 | } else |
| 4011 | x = 0; |
| 4012 | } |
| 4013 | if (x >= term->cols) |
| 4014 | x = term->cols - 1; |
| 4015 | |
| 4016 | selpoint.y = y + term->disptop; |
| 4017 | selpoint.x = x; |
| 4018 | ldata = lineptr(selpoint.y); |
| 4019 | if ((ldata[term->cols] & LATTR_MODE) != LATTR_NORM) |
| 4020 | selpoint.x /= 2; |
| 4021 | |
| 4022 | if (raw_mouse) { |
| 4023 | int encstate = 0, r, c; |
| 4024 | char abuf[16]; |
| 4025 | |
| 4026 | if (term->ldisc) { |
| 4027 | |
| 4028 | switch (braw) { |
| 4029 | case MBT_LEFT: |
| 4030 | encstate = 0x20; /* left button down */ |
| 4031 | break; |
| 4032 | case MBT_MIDDLE: |
| 4033 | encstate = 0x21; |
| 4034 | break; |
| 4035 | case MBT_RIGHT: |
| 4036 | encstate = 0x22; |
| 4037 | break; |
| 4038 | case MBT_WHEEL_UP: |
| 4039 | encstate = 0x60; |
| 4040 | break; |
| 4041 | case MBT_WHEEL_DOWN: |
| 4042 | encstate = 0x61; |
| 4043 | break; |
| 4044 | default: break; /* placate gcc warning about enum use */ |
| 4045 | } |
| 4046 | switch (a) { |
| 4047 | case MA_DRAG: |
| 4048 | if (term->xterm_mouse == 1) |
| 4049 | return; |
| 4050 | encstate += 0x20; |
| 4051 | break; |
| 4052 | case MA_RELEASE: |
| 4053 | encstate = 0x23; |
| 4054 | term->mouse_is_down = 0; |
| 4055 | break; |
| 4056 | case MA_CLICK: |
| 4057 | if (term->mouse_is_down == braw) |
| 4058 | return; |
| 4059 | term->mouse_is_down = braw; |
| 4060 | break; |
| 4061 | default: break; /* placate gcc warning about enum use */ |
| 4062 | } |
| 4063 | if (shift) |
| 4064 | encstate += 0x04; |
| 4065 | if (ctrl) |
| 4066 | encstate += 0x10; |
| 4067 | r = y + 33; |
| 4068 | c = x + 33; |
| 4069 | |
| 4070 | sprintf(abuf, "\033[M%c%c%c", encstate, c, r); |
| 4071 | ldisc_send(term->ldisc, abuf, 6, 0); |
| 4072 | } |
| 4073 | return; |
| 4074 | } |
| 4075 | |
| 4076 | /* |
| 4077 | * Set the selection type (rectangular or normal) at the start |
| 4078 | * of a selection attempt, from the state of Alt. |
| 4079 | */ |
| 4080 | if (!alt ^ !term->cfg.rect_select) |
| 4081 | default_seltype = RECTANGULAR; |
| 4082 | else |
| 4083 | default_seltype = LEXICOGRAPHIC; |
| 4084 | |
| 4085 | if (term->selstate == NO_SELECTION) { |
| 4086 | term->seltype = default_seltype; |
| 4087 | } |
| 4088 | |
| 4089 | if (bcooked == MBT_SELECT && a == MA_CLICK) { |
| 4090 | deselect(term); |
| 4091 | term->selstate = ABOUT_TO; |
| 4092 | term->seltype = default_seltype; |
| 4093 | term->selanchor = selpoint; |
| 4094 | term->selmode = SM_CHAR; |
| 4095 | } else if (bcooked == MBT_SELECT && (a == MA_2CLK || a == MA_3CLK)) { |
| 4096 | deselect(term); |
| 4097 | term->selmode = (a == MA_2CLK ? SM_WORD : SM_LINE); |
| 4098 | term->selstate = DRAGGING; |
| 4099 | term->selstart = term->selanchor = selpoint; |
| 4100 | term->selend = term->selstart; |
| 4101 | incpos(term->selend); |
| 4102 | sel_spread(term); |
| 4103 | } else if ((bcooked == MBT_SELECT && a == MA_DRAG) || |
| 4104 | (bcooked == MBT_EXTEND && a != MA_RELEASE)) { |
| 4105 | if (term->selstate == ABOUT_TO && poseq(term->selanchor, selpoint)) |
| 4106 | return; |
| 4107 | if (bcooked == MBT_EXTEND && a != MA_DRAG && |
| 4108 | term->selstate == SELECTED) { |
| 4109 | if (term->seltype == LEXICOGRAPHIC) { |
| 4110 | /* |
| 4111 | * For normal selection, we extend by moving |
| 4112 | * whichever end of the current selection is closer |
| 4113 | * to the mouse. |
| 4114 | */ |
| 4115 | if (posdiff(selpoint, term->selstart) < |
| 4116 | posdiff(term->selend, term->selstart) / 2) { |
| 4117 | term->selanchor = term->selend; |
| 4118 | decpos(term->selanchor); |
| 4119 | } else { |
| 4120 | term->selanchor = term->selstart; |
| 4121 | } |
| 4122 | } else { |
| 4123 | /* |
| 4124 | * For rectangular selection, we have a choice of |
| 4125 | * _four_ places to put selanchor and selpoint: the |
| 4126 | * four corners of the selection. |
| 4127 | */ |
| 4128 | if (2*selpoint.x < term->selstart.x + term->selend.x) |
| 4129 | term->selanchor.x = term->selend.x-1; |
| 4130 | else |
| 4131 | term->selanchor.x = term->selstart.x; |
| 4132 | |
| 4133 | if (2*selpoint.y < term->selstart.y + term->selend.y) |
| 4134 | term->selanchor.y = term->selend.y; |
| 4135 | else |
| 4136 | term->selanchor.y = term->selstart.y; |
| 4137 | } |
| 4138 | term->selstate = DRAGGING; |
| 4139 | } |
| 4140 | if (term->selstate != ABOUT_TO && term->selstate != DRAGGING) |
| 4141 | term->selanchor = selpoint; |
| 4142 | term->selstate = DRAGGING; |
| 4143 | if (term->seltype == LEXICOGRAPHIC) { |
| 4144 | /* |
| 4145 | * For normal selection, we set (selstart,selend) to |
| 4146 | * (selpoint,selanchor) in some order. |
| 4147 | */ |
| 4148 | if (poslt(selpoint, term->selanchor)) { |
| 4149 | term->selstart = selpoint; |
| 4150 | term->selend = term->selanchor; |
| 4151 | incpos(term->selend); |
| 4152 | } else { |
| 4153 | term->selstart = term->selanchor; |
| 4154 | term->selend = selpoint; |
| 4155 | incpos(term->selend); |
| 4156 | } |
| 4157 | } else { |
| 4158 | /* |
| 4159 | * For rectangular selection, we may need to |
| 4160 | * interchange x and y coordinates (if the user has |
| 4161 | * dragged in the -x and +y directions, or vice versa). |
| 4162 | */ |
| 4163 | term->selstart.x = min(term->selanchor.x, selpoint.x); |
| 4164 | term->selend.x = 1+max(term->selanchor.x, selpoint.x); |
| 4165 | term->selstart.y = min(term->selanchor.y, selpoint.y); |
| 4166 | term->selend.y = max(term->selanchor.y, selpoint.y); |
| 4167 | } |
| 4168 | sel_spread(term); |
| 4169 | } else if ((bcooked == MBT_SELECT || bcooked == MBT_EXTEND) && |
| 4170 | a == MA_RELEASE) { |
| 4171 | if (term->selstate == DRAGGING) { |
| 4172 | /* |
| 4173 | * We've completed a selection. We now transfer the |
| 4174 | * data to the clipboard. |
| 4175 | */ |
| 4176 | clipme(term, term->selstart, term->selend, |
| 4177 | (term->seltype == RECTANGULAR)); |
| 4178 | term->selstate = SELECTED; |
| 4179 | } else |
| 4180 | term->selstate = NO_SELECTION; |
| 4181 | } else if (bcooked == MBT_PASTE |
| 4182 | && (a == MA_CLICK |
| 4183 | #if MULTICLICK_ONLY_EVENT |
| 4184 | || a == MA_2CLK || a == MA_3CLK |
| 4185 | #endif |
| 4186 | )) { |
| 4187 | request_paste(term->frontend); |
| 4188 | } |
| 4189 | |
| 4190 | term_update(term); |
| 4191 | } |
| 4192 | |
| 4193 | void term_key(Terminal *term, Key_Sym keysym, wchar_t *text, size_t tlen, |
| 4194 | unsigned int modifiers, unsigned int flags) |
| 4195 | { |
| 4196 | char output[10]; |
| 4197 | char *p = output; |
| 4198 | int prependesc = FALSE; |
| 4199 | #if 0 |
| 4200 | int i; |
| 4201 | |
| 4202 | fprintf(stderr, "keysym = %d, %d chars:", keysym, tlen); |
| 4203 | for (i = 0; i < tlen; i++) |
| 4204 | fprintf(stderr, " %04x", (unsigned)text[i]); |
| 4205 | fprintf(stderr, "\n"); |
| 4206 | #endif |
| 4207 | |
| 4208 | /* XXX Num Lock */ |
| 4209 | if ((flags & PKF_REPEAT) && term->repeat_off) |
| 4210 | return; |
| 4211 | |
| 4212 | /* Currently, Meta always just prefixes everything with ESC. */ |
| 4213 | if (modifiers & PKM_META) |
| 4214 | prependesc = TRUE; |
| 4215 | modifiers &= ~PKM_META; |
| 4216 | |
| 4217 | /* |
| 4218 | * Alt is only used for Alt+keypad, which isn't supported yet, so |
| 4219 | * ignore it. |
| 4220 | */ |
| 4221 | modifiers &= ~PKM_ALT; |
| 4222 | |
| 4223 | /* Standard local function keys */ |
| 4224 | switch (modifiers & (PKM_SHIFT | PKM_CONTROL)) { |
| 4225 | case PKM_SHIFT: |
| 4226 | if (keysym == PK_PAGEUP) |
| 4227 | /* scroll up one page */; |
| 4228 | if (keysym == PK_PAGEDOWN) |
| 4229 | /* scroll down on page */; |
| 4230 | if (keysym == PK_INSERT) |
| 4231 | term_do_paste(term); |
| 4232 | break; |
| 4233 | case PKM_CONTROL: |
| 4234 | if (keysym == PK_PAGEUP) |
| 4235 | /* scroll up one line */; |
| 4236 | if (keysym == PK_PAGEDOWN) |
| 4237 | /* scroll down one line */; |
| 4238 | /* Control-Numlock for app-keypad mode switch */ |
| 4239 | if (keysym == PK_PF1) |
| 4240 | term->app_keypad_keys ^= 1; |
| 4241 | break; |
| 4242 | } |
| 4243 | |
| 4244 | if (modifiers & PKM_ALT) { |
| 4245 | /* Alt+F4 (close) */ |
| 4246 | /* Alt+Return (full screen) */ |
| 4247 | /* Alt+Space (system menu) */ |
| 4248 | } |
| 4249 | |
| 4250 | if (keysym == PK_NULL && (modifiers & PKM_CONTROL) && tlen == 1 && |
| 4251 | text[0] >= 0x20 && text[0] <= 0x7e) { |
| 4252 | /* ASCII chars + Control */ |
| 4253 | if ((text[0] >= 0x40 && text[0] <= 0x5f) || |
| 4254 | (text[0] >= 0x61 && text[0] <= 0x7a)) |
| 4255 | text[0] &= 0x1f; |
| 4256 | else { |
| 4257 | /* |
| 4258 | * Control-2 should return ^@ (0x00), Control-6 should return |
| 4259 | * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since |
| 4260 | * the DOS keyboard handling did it, and we have nothing better |
| 4261 | * to do with the key combo in question, we'll also map |
| 4262 | * Control-Backquote to ^\ (0x1C). |
| 4263 | */ |
| 4264 | switch (text[0]) { |
| 4265 | case ' ': text[0] = 0x00; break; |
| 4266 | case '-': text[0] = 0x1f; break; |
| 4267 | case '/': text[0] = 0x1f; break; |
| 4268 | case '2': text[0] = 0x00; break; |
| 4269 | case '3': text[0] = 0x1b; break; |
| 4270 | case '4': text[0] = 0x1c; break; |
| 4271 | case '5': text[0] = 0x1d; break; |
| 4272 | case '6': text[0] = 0x1e; break; |
| 4273 | case '7': text[0] = 0x1f; break; |
| 4274 | case '8': text[0] = 0x7f; break; |
| 4275 | case '`': text[0] = 0x1c; break; |
| 4276 | } |
| 4277 | } |
| 4278 | } |
| 4279 | |
| 4280 | /* Nethack keypad */ |
| 4281 | if (term->cfg.nethack_keypad) { |
| 4282 | char c = 0; |
| 4283 | switch (keysym) { |
| 4284 | case PK_KP1: c = 'b'; break; |
| 4285 | case PK_KP2: c = 'j'; break; |
| 4286 | case PK_KP3: c = 'n'; break; |
| 4287 | case PK_KP4: c = 'h'; break; |
| 4288 | case PK_KP5: c = '.'; break; |
| 4289 | case PK_KP6: c = 'l'; break; |
| 4290 | case PK_KP7: c = 'y'; break; |
| 4291 | case PK_KP8: c = 'k'; break; |
| 4292 | case PK_KP9: c = 'u'; break; |
| 4293 | default: break; /* else gcc warns `enum value not used' */ |
| 4294 | } |
| 4295 | if (c != 0) { |
| 4296 | if (c != '.') { |
| 4297 | if (modifiers & PKM_CONTROL) |
| 4298 | c &= 0x1f; |
| 4299 | else if (modifiers & PKM_SHIFT) |
| 4300 | c = toupper(c); |
| 4301 | } |
| 4302 | *p++ = c; |
| 4303 | goto done; |
| 4304 | } |
| 4305 | } |
| 4306 | |
| 4307 | /* Numeric Keypad */ |
| 4308 | if (PK_ISKEYPAD(keysym)) { |
| 4309 | int xkey = 0; |
| 4310 | |
| 4311 | /* |
| 4312 | * In VT400 mode, PFn always emits an escape sequence. In |
| 4313 | * Linux and tilde modes, this only happens in app keypad mode. |
| 4314 | */ |
| 4315 | if (term->cfg.funky_type == FUNKY_VT400 || |
| 4316 | ((term->cfg.funky_type == FUNKY_LINUX || |
| 4317 | term->cfg.funky_type == FUNKY_TILDE) && |
| 4318 | term->app_keypad_keys && !term->cfg.no_applic_k)) { |
| 4319 | switch (keysym) { |
| 4320 | case PK_PF1: xkey = 'P'; break; |
| 4321 | case PK_PF2: xkey = 'Q'; break; |
| 4322 | case PK_PF3: xkey = 'R'; break; |
| 4323 | case PK_PF4: xkey = 'S'; break; |
| 4324 | default: break; /* else gcc warns `enum value not used' */ |
| 4325 | } |
| 4326 | } |
| 4327 | if (term->app_keypad_keys && !term->cfg.no_applic_k) { |
| 4328 | switch (keysym) { |
| 4329 | case PK_KP0: xkey = 'p'; break; |
| 4330 | case PK_KP1: xkey = 'q'; break; |
| 4331 | case PK_KP2: xkey = 'r'; break; |
| 4332 | case PK_KP3: xkey = 's'; break; |
| 4333 | case PK_KP4: xkey = 't'; break; |
| 4334 | case PK_KP5: xkey = 'u'; break; |
| 4335 | case PK_KP6: xkey = 'v'; break; |
| 4336 | case PK_KP7: xkey = 'w'; break; |
| 4337 | case PK_KP8: xkey = 'x'; break; |
| 4338 | case PK_KP9: xkey = 'y'; break; |
| 4339 | case PK_KPDECIMAL: xkey = 'n'; break; |
| 4340 | case PK_KPENTER: xkey = 'M'; break; |
| 4341 | default: break; /* else gcc warns `enum value not used' */ |
| 4342 | } |
| 4343 | if (term->cfg.funky_type == FUNKY_XTERM && tlen > 0) { |
| 4344 | /* |
| 4345 | * xterm can't see the layout of the keypad, so it has |
| 4346 | * to rely on the X keysyms returned by the keys. |
| 4347 | * Hence, we look at the strings here, not the PuTTY |
| 4348 | * keysyms (which describe the layout). |
| 4349 | */ |
| 4350 | switch (text[0]) { |
| 4351 | case '+': |
| 4352 | if (modifiers & PKM_SHIFT) |
| 4353 | xkey = 'l'; |
| 4354 | else |
| 4355 | xkey = 'k'; |
| 4356 | break; |
| 4357 | case '/': xkey = 'o'; break; |
| 4358 | case '*': xkey = 'j'; break; |
| 4359 | case '-': xkey = 'm'; break; |
| 4360 | } |
| 4361 | } else { |
| 4362 | /* |
| 4363 | * In all other modes, we try to retain the layout of |
| 4364 | * the DEC keypad in application mode. |
| 4365 | */ |
| 4366 | switch (keysym) { |
| 4367 | case PK_KPBIGPLUS: |
| 4368 | /* This key covers the '-' and ',' keys on a VT220 */ |
| 4369 | if (modifiers & PKM_SHIFT) |
| 4370 | xkey = 'm'; /* VT220 '-' */ |
| 4371 | else |
| 4372 | xkey = 'l'; /* VT220 ',' */ |
| 4373 | break; |
| 4374 | case PK_KPMINUS: xkey = 'm'; break; |
| 4375 | case PK_KPCOMMA: xkey = 'l'; break; |
| 4376 | default: break; /* else gcc warns `enum value not used' */ |
| 4377 | } |
| 4378 | } |
| 4379 | } |
| 4380 | if (xkey) { |
| 4381 | if (term->vt52_mode) { |
| 4382 | if (xkey >= 'P' && xkey <= 'S') |
| 4383 | p += sprintf((char *) p, "\x1B%c", xkey); |
| 4384 | else |
| 4385 | p += sprintf((char *) p, "\x1B?%c", xkey); |
| 4386 | } else |
| 4387 | p += sprintf((char *) p, "\x1BO%c", xkey); |
| 4388 | goto done; |
| 4389 | } |
| 4390 | /* Not in application mode -- treat the number pad as arrow keys? */ |
| 4391 | if ((flags & PKF_NUMLOCK) == 0) { |
| 4392 | switch (keysym) { |
| 4393 | case PK_KP0: keysym = PK_INSERT; break; |
| 4394 | case PK_KP1: keysym = PK_END; break; |
| 4395 | case PK_KP2: keysym = PK_DOWN; break; |
| 4396 | case PK_KP3: keysym = PK_PAGEDOWN; break; |
| 4397 | case PK_KP4: keysym = PK_LEFT; break; |
| 4398 | case PK_KP5: keysym = PK_REST; break; |
| 4399 | case PK_KP6: keysym = PK_RIGHT; break; |
| 4400 | case PK_KP7: keysym = PK_HOME; break; |
| 4401 | case PK_KP8: keysym = PK_UP; break; |
| 4402 | case PK_KP9: keysym = PK_PAGEUP; break; |
| 4403 | default: break; /* else gcc warns `enum value not used' */ |
| 4404 | } |
| 4405 | } |
| 4406 | } |
| 4407 | |
| 4408 | /* Miscellaneous keys */ |
| 4409 | switch (keysym) { |
| 4410 | case PK_ESCAPE: |
| 4411 | *p++ = 0x1b; |
| 4412 | goto done; |
| 4413 | case PK_BACKSPACE: |
| 4414 | if (modifiers == 0) |
| 4415 | *p++ = (term->cfg.bksp_is_delete ? 0x7F : 0x08); |
| 4416 | else if (modifiers == PKM_SHIFT) |
| 4417 | /* We do the opposite of what is configured */ |
| 4418 | *p++ = (term->cfg.bksp_is_delete ? 0x08 : 0x7F); |
| 4419 | else break; |
| 4420 | goto done; |
| 4421 | case PK_TAB: |
| 4422 | if (modifiers == 0) |
| 4423 | *p++ = 0x09; |
| 4424 | else if (modifiers == PKM_SHIFT) |
| 4425 | *p++ = 0x1B, *p++ = '[', *p++ = 'Z'; |
| 4426 | else break; |
| 4427 | goto done; |
| 4428 | /* XXX window.c has ctrl+shift+space sending 0xa0 */ |
| 4429 | case PK_PAUSE: |
| 4430 | if (modifiers == PKM_CONTROL) |
| 4431 | *p++ = 26; |
| 4432 | else break; |
| 4433 | goto done; |
| 4434 | case PK_RETURN: |
| 4435 | case PK_KPENTER: /* Odd keypad modes handled above */ |
| 4436 | if (modifiers == 0) { |
| 4437 | *p++ = 0x0d; |
| 4438 | if (term->cr_lf_return) |
| 4439 | *p++ = 0x0a; |
| 4440 | goto done; |
| 4441 | } |
| 4442 | default: break; /* else gcc warns `enum value not used' */ |
| 4443 | } |
| 4444 | |
| 4445 | /* SCO function keys and editing keys */ |
| 4446 | if (term->cfg.funky_type == FUNKY_SCO) { |
| 4447 | if (PK_ISFKEY(keysym) && keysym <= PK_F12) { |
| 4448 | static char const codes[] = |
| 4449 | "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{"; |
| 4450 | int index = keysym - PK_F1; |
| 4451 | |
| 4452 | if (modifiers & PKM_SHIFT) index += 12; |
| 4453 | if (modifiers & PKM_CONTROL) index += 24; |
| 4454 | p += sprintf((char *) p, "\x1B[%c", codes[index]); |
| 4455 | goto done; |
| 4456 | } |
| 4457 | if (PK_ISEDITING(keysym)) { |
| 4458 | int xkey = 0; |
| 4459 | |
| 4460 | switch (keysym) { |
| 4461 | case PK_DELETE: *p++ = 0x7f; goto done; |
| 4462 | case PK_HOME: xkey = 'H'; break; |
| 4463 | case PK_INSERT: xkey = 'L'; break; |
| 4464 | case PK_END: xkey = 'F'; break; |
| 4465 | case PK_PAGEUP: xkey = 'I'; break; |
| 4466 | case PK_PAGEDOWN: xkey = 'G'; break; |
| 4467 | default: break; /* else gcc warns `enum value not used' */ |
| 4468 | } |
| 4469 | p += sprintf((char *) p, "\x1B[%c", xkey); |
| 4470 | } |
| 4471 | } |
| 4472 | |
| 4473 | if (PK_ISEDITING(keysym) && (modifiers & PKM_SHIFT) == 0) { |
| 4474 | int code; |
| 4475 | |
| 4476 | if (term->cfg.funky_type == FUNKY_XTERM) { |
| 4477 | /* Xterm shuffles these keys, apparently. */ |
| 4478 | switch (keysym) { |
| 4479 | case PK_HOME: keysym = PK_INSERT; break; |
| 4480 | case PK_INSERT: keysym = PK_HOME; break; |
| 4481 | case PK_DELETE: keysym = PK_END; break; |
| 4482 | case PK_END: keysym = PK_PAGEUP; break; |
| 4483 | case PK_PAGEUP: keysym = PK_DELETE; break; |
| 4484 | case PK_PAGEDOWN: keysym = PK_PAGEDOWN; break; |
| 4485 | default: break; /* else gcc warns `enum value not used' */ |
| 4486 | } |
| 4487 | } |
| 4488 | |
| 4489 | /* RXVT Home/End */ |
| 4490 | if (term->cfg.rxvt_homeend && |
| 4491 | (keysym == PK_HOME || keysym == PK_END)) { |
| 4492 | p += sprintf((char *) p, keysym == PK_HOME ? "\x1B[H" : "\x1BOw"); |
| 4493 | goto done; |
| 4494 | } |
| 4495 | |
| 4496 | if (term->vt52_mode) { |
| 4497 | int xkey; |
| 4498 | |
| 4499 | /* |
| 4500 | * A real VT52 doesn't have these, and a VT220 doesn't |
| 4501 | * send anything for them in VT52 mode. |
| 4502 | */ |
| 4503 | switch (keysym) { |
| 4504 | case PK_HOME: xkey = 'H'; break; |
| 4505 | case PK_INSERT: xkey = 'L'; break; |
| 4506 | case PK_DELETE: xkey = 'M'; break; |
| 4507 | case PK_END: xkey = 'E'; break; |
| 4508 | case PK_PAGEUP: xkey = 'I'; break; |
| 4509 | case PK_PAGEDOWN: xkey = 'G'; break; |
| 4510 | default: break; /* else gcc warns `enum value not used' */ |
| 4511 | } |
| 4512 | p += sprintf((char *) p, "\x1B%c", xkey); |
| 4513 | goto done; |
| 4514 | } |
| 4515 | |
| 4516 | switch (keysym) { |
| 4517 | case PK_HOME: code = 1; break; |
| 4518 | case PK_INSERT: code = 2; break; |
| 4519 | case PK_DELETE: code = 3; break; |
| 4520 | case PK_END: code = 4; break; |
| 4521 | case PK_PAGEUP: code = 5; break; |
| 4522 | case PK_PAGEDOWN: code = 6; break; |
| 4523 | default: break; /* else gcc warns `enum value not used' */ |
| 4524 | } |
| 4525 | p += sprintf((char *) p, "\x1B[%d~", code); |
| 4526 | goto done; |
| 4527 | } |
| 4528 | |
| 4529 | if (PK_ISFKEY(keysym)) { |
| 4530 | /* Map Shift+F1-F10 to F11-F20 */ |
| 4531 | if (keysym >= PK_F1 && keysym <= PK_F10 && (modifiers & PKM_SHIFT)) |
| 4532 | keysym += 10; |
| 4533 | if ((term->vt52_mode || term->cfg.funky_type == FUNKY_VT100P) && |
| 4534 | keysym <= PK_F14) { |
| 4535 | /* XXX This overrides the XTERM/VT52 mode below */ |
| 4536 | int offt = 0; |
| 4537 | if (keysym >= PK_F6) offt++; |
| 4538 | if (keysym >= PK_F12) offt++; |
| 4539 | p += sprintf((char *) p, term->vt52_mode ? "\x1B%c" : "\x1BO%c", |
| 4540 | 'P' + keysym - PK_F1 - offt); |
| 4541 | goto done; |
| 4542 | } |
| 4543 | if (term->cfg.funky_type == FUNKY_LINUX && keysym <= PK_F5) { |
| 4544 | p += sprintf((char *) p, "\x1B[[%c", 'A' + keysym - PK_F1); |
| 4545 | goto done; |
| 4546 | } |
| 4547 | if (term->cfg.funky_type == FUNKY_XTERM && keysym <= PK_F4) { |
| 4548 | if (term->vt52_mode) |
| 4549 | p += sprintf((char *) p, "\x1B%c", 'P' + keysym - PK_F1); |
| 4550 | else |
| 4551 | p += sprintf((char *) p, "\x1BO%c", 'P' + keysym - PK_F1); |
| 4552 | goto done; |
| 4553 | } |
| 4554 | p += sprintf((char *) p, "\x1B[%d~", 11 + keysym - PK_F1); |
| 4555 | goto done; |
| 4556 | } |
| 4557 | |
| 4558 | if (PK_ISCURSOR(keysym)) { |
| 4559 | int xkey; |
| 4560 | |
| 4561 | switch (keysym) { |
| 4562 | case PK_UP: xkey = 'A'; break; |
| 4563 | case PK_DOWN: xkey = 'B'; break; |
| 4564 | case PK_RIGHT: xkey = 'C'; break; |
| 4565 | case PK_LEFT: xkey = 'D'; break; |
| 4566 | case PK_REST: xkey = 'G'; break; /* centre key on number pad */ |
| 4567 | default: break; /* else gcc warns `enum value not used' */ |
| 4568 | } |
| 4569 | if (term->vt52_mode) |
| 4570 | p += sprintf((char *) p, "\x1B%c", xkey); |
| 4571 | else { |
| 4572 | int app_flg = (term->app_cursor_keys && !term->cfg.no_applic_c); |
| 4573 | |
| 4574 | /* Useful mapping of Ctrl-arrows */ |
| 4575 | if (modifiers == PKM_CONTROL) |
| 4576 | app_flg = !app_flg; |
| 4577 | |
| 4578 | if (app_flg) |
| 4579 | p += sprintf((char *) p, "\x1BO%c", xkey); |
| 4580 | else |
| 4581 | p += sprintf((char *) p, "\x1B[%c", xkey); |
| 4582 | } |
| 4583 | goto done; |
| 4584 | } |
| 4585 | |
| 4586 | done: |
| 4587 | if (p > output || tlen > 0) { |
| 4588 | /* |
| 4589 | * Interrupt an ongoing paste. I'm not sure |
| 4590 | * this is sensible, but for the moment it's |
| 4591 | * preferable to having to faff about buffering |
| 4592 | * things. |
| 4593 | */ |
| 4594 | term_nopaste(term); |
| 4595 | |
| 4596 | /* |
| 4597 | * We need not bother about stdin backlogs |
| 4598 | * here, because in GUI PuTTY we can't do |
| 4599 | * anything about it anyway; there's no means |
| 4600 | * of asking Windows to hold off on KEYDOWN |
| 4601 | * messages. We _have_ to buffer everything |
| 4602 | * we're sent. |
| 4603 | */ |
| 4604 | term_seen_key_event(term); |
| 4605 | |
| 4606 | if (prependesc) { |
| 4607 | #if 0 |
| 4608 | fprintf(stderr, "sending ESC\n"); |
| 4609 | #endif |
| 4610 | ldisc_send(term->ldisc, "\x1b", 1, 1); |
| 4611 | } |
| 4612 | |
| 4613 | if (p > output) { |
| 4614 | #if 0 |
| 4615 | fprintf(stderr, "sending %d bytes:", p - output); |
| 4616 | for (i = 0; i < p - output; i++) |
| 4617 | fprintf(stderr, " %02x", output[i]); |
| 4618 | fprintf(stderr, "\n"); |
| 4619 | #endif |
| 4620 | ldisc_send(term->ldisc, output, p - output, 1); |
| 4621 | } else if (tlen > 0) { |
| 4622 | #if 0 |
| 4623 | fprintf(stderr, "sending %d unichars:", tlen); |
| 4624 | for (i = 0; i < tlen; i++) |
| 4625 | fprintf(stderr, " %04x", (unsigned) text[i]); |
| 4626 | fprintf(stderr, "\n"); |
| 4627 | #endif |
| 4628 | luni_send(term->ldisc, text, tlen, 1); |
| 4629 | } |
| 4630 | } |
| 4631 | } |
| 4632 | |
| 4633 | void term_nopaste(Terminal *term) |
| 4634 | { |
| 4635 | if (term->paste_len == 0) |
| 4636 | return; |
| 4637 | sfree(term->paste_buffer); |
| 4638 | term->paste_buffer = NULL; |
| 4639 | term->paste_len = 0; |
| 4640 | } |
| 4641 | |
| 4642 | int term_paste_pending(Terminal *term) |
| 4643 | { |
| 4644 | return term->paste_len != 0; |
| 4645 | } |
| 4646 | |
| 4647 | void term_paste(Terminal *term) |
| 4648 | { |
| 4649 | long now, paste_diff; |
| 4650 | |
| 4651 | if (term->paste_len == 0) |
| 4652 | return; |
| 4653 | |
| 4654 | /* Don't wait forever to paste */ |
| 4655 | if (term->paste_hold) { |
| 4656 | now = GETTICKCOUNT(); |
| 4657 | paste_diff = now - term->last_paste; |
| 4658 | if (paste_diff >= 0 && paste_diff < 450) |
| 4659 | return; |
| 4660 | } |
| 4661 | term->paste_hold = 0; |
| 4662 | |
| 4663 | while (term->paste_pos < term->paste_len) { |
| 4664 | int n = 0; |
| 4665 | while (n + term->paste_pos < term->paste_len) { |
| 4666 | if (term->paste_buffer[term->paste_pos + n++] == '\015') |
| 4667 | break; |
| 4668 | } |
| 4669 | if (term->ldisc) |
| 4670 | luni_send(term->ldisc, term->paste_buffer + term->paste_pos, n, 0); |
| 4671 | term->paste_pos += n; |
| 4672 | |
| 4673 | if (term->paste_pos < term->paste_len) { |
| 4674 | term->paste_hold = 1; |
| 4675 | return; |
| 4676 | } |
| 4677 | } |
| 4678 | sfree(term->paste_buffer); |
| 4679 | term->paste_buffer = NULL; |
| 4680 | term->paste_len = 0; |
| 4681 | } |
| 4682 | |
| 4683 | static void deselect(Terminal *term) |
| 4684 | { |
| 4685 | term->selstate = NO_SELECTION; |
| 4686 | term->selstart.x = term->selstart.y = term->selend.x = term->selend.y = 0; |
| 4687 | } |
| 4688 | |
| 4689 | void term_deselect(Terminal *term) |
| 4690 | { |
| 4691 | deselect(term); |
| 4692 | term_update(term); |
| 4693 | } |
| 4694 | |
| 4695 | int term_ldisc(Terminal *term, int option) |
| 4696 | { |
| 4697 | if (option == LD_ECHO) |
| 4698 | return term->term_echoing; |
| 4699 | if (option == LD_EDIT) |
| 4700 | return term->term_editing; |
| 4701 | return FALSE; |
| 4702 | } |
| 4703 | |
| 4704 | /* |
| 4705 | * from_backend(), to get data from the backend for the terminal. |
| 4706 | */ |
| 4707 | int from_backend(void *vterm, int is_stderr, const char *data, int len) |
| 4708 | { |
| 4709 | Terminal *term = (Terminal *)vterm; |
| 4710 | |
| 4711 | assert(len > 0); |
| 4712 | |
| 4713 | bufchain_add(&term->inbuf, data, len); |
| 4714 | |
| 4715 | /* |
| 4716 | * term_out() always completely empties inbuf. Therefore, |
| 4717 | * there's no reason at all to return anything other than zero |
| 4718 | * from this function, because there _can't_ be a question of |
| 4719 | * the remote side needing to wait until term_out() has cleared |
| 4720 | * a backlog. |
| 4721 | * |
| 4722 | * This is a slightly suboptimal way to deal with SSH2 - in |
| 4723 | * principle, the window mechanism would allow us to continue |
| 4724 | * to accept data on forwarded ports and X connections even |
| 4725 | * while the terminal processing was going slowly - but we |
| 4726 | * can't do the 100% right thing without moving the terminal |
| 4727 | * processing into a separate thread, and that might hurt |
| 4728 | * portability. So we manage stdout buffering the old SSH1 way: |
| 4729 | * if the terminal processing goes slowly, the whole SSH |
| 4730 | * connection stops accepting data until it's ready. |
| 4731 | * |
| 4732 | * In practice, I can't imagine this causing serious trouble. |
| 4733 | */ |
| 4734 | return 0; |
| 4735 | } |
| 4736 | |
| 4737 | void term_provide_logctx(Terminal *term, void *logctx) |
| 4738 | { |
| 4739 | term->logctx = logctx; |
| 4740 | } |