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