1 /* $OpenBSD: lib_mvcur.c,v 1.8 2000/07/10 03:06:16 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 34 ****************************************************************************/ 35 36 /* 37 ** lib_mvcur.c 38 ** 39 ** The routines for moving the physical cursor and scrolling: 40 ** 41 ** void _nc_mvcur_init(void) 42 ** 43 ** void _nc_mvcur_resume(void) 44 ** 45 ** int mvcur(int old_y, int old_x, int new_y, int new_x) 46 ** 47 ** void _nc_mvcur_wrap(void) 48 ** 49 ** Comparisons with older movement optimizers: 50 ** SVr3 curses mvcur() can't use cursor_to_ll or auto_left_margin. 51 ** 4.4BSD curses can't use cuu/cud/cuf/cub/hpa/vpa/tab/cbt for local 52 ** motions. It doesn't use tactics based on auto_left_margin. Weirdly 53 ** enough, it doesn't use its own hardware-scrolling routine to scroll up 54 ** destination lines for out-of-bounds addresses! 55 ** old ncurses optimizer: less accurate cost computations (in fact, 56 ** it was broken and had to be commented out!). 57 ** 58 ** Compile with -DMAIN to build an interactive tester/timer for the movement 59 ** optimizer. You can use it to investigate the optimizer's behavior. 60 ** You can also use it for tuning the formulas used to determine whether 61 ** or not full optimization is attempted. 62 ** 63 ** This code has a nasty tendency to find bugs in terminfo entries, because it 64 ** exercises the non-cup movement capabilities heavily. If you think you've 65 ** found a bug, try deleting subsets of the following capabilities (arranged 66 ** in decreasing order of suspiciousness): it, tab, cbt, hpa, vpa, cuu, cud, 67 ** cuf, cub, cuu1, cud1, cuf1, cub1. It may be that one or more are wrong. 68 ** 69 ** Note: you should expect this code to look like a resource hog in a profile. 70 ** That's because it does a lot of I/O, through the tputs() calls. The I/O 71 ** cost swamps the computation overhead (and as machines get faster, this 72 ** will become even more true). Comments in the test exerciser at the end 73 ** go into detail about tuning and how you can gauge the optimizer's 74 ** effectiveness. 75 **/ 76 77 /**************************************************************************** 78 * 79 * Constants and macros for optimizer tuning. 80 * 81 ****************************************************************************/ 82 83 /* 84 * The average overhead of a full optimization computation in character 85 * transmission times. If it's too high, the algorithm will be a bit 86 * over-biased toward using cup rather than local motions; if it's too 87 * low, the algorithm may spend more time than is strictly optimal 88 * looking for non-cup motions. Profile the optimizer using the `t' 89 * command of the exerciser (see below), and round to the nearest integer. 90 * 91 * Yes, I (esr) thought about computing expected overhead dynamically, say 92 * by derivation from a running average of optimizer times. But the 93 * whole point of this optimization is to *decrease* the frequency of 94 * system calls. :-) 95 */ 96 #define COMPUTE_OVERHEAD 1 /* I use a 90MHz Pentium @ 9.6Kbps */ 97 98 /* 99 * LONG_DIST is the distance we consider to be just as costly to move over as a 100 * cup sequence is to emit. In other words, it's the length of a cup sequence 101 * adjusted for average computation overhead. The magic number is the length 102 * of "\033[yy;xxH", the typical cup sequence these days. 103 */ 104 #define LONG_DIST (8 - COMPUTE_OVERHEAD) 105 106 /* 107 * Tell whether a motion is optimizable by local motions. Needs to be cheap to 108 * compute. In general, all the fast moves go to either the right or left edge 109 * of the screen. So any motion to a location that is (a) further away than 110 * LONG_DIST and (b) further inward from the right or left edge than LONG_DIST, 111 * we'll consider nonlocal. 112 */ 113 #define NOT_LOCAL(fy, fx, ty, tx) ((tx > LONG_DIST) && (tx < screen_lines - 1 - LONG_DIST) && (abs(ty-fy) + abs(tx-fx) > LONG_DIST)) 114 115 /**************************************************************************** 116 * 117 * External interfaces 118 * 119 ****************************************************************************/ 120 121 /* 122 * For this code to work OK, the following components must live in the 123 * screen structure: 124 * 125 * int _char_padding; // cost of character put 126 * int _cr_cost; // cost of (carriage_return) 127 * int _cup_cost; // cost of (cursor_address) 128 * int _home_cost; // cost of (cursor_home) 129 * int _ll_cost; // cost of (cursor_to_ll) 130 *#if USE_HARD_TABS 131 * int _ht_cost; // cost of (tab) 132 * int _cbt_cost; // cost of (back_tab) 133 *#endif USE_HARD_TABS 134 * int _cub1_cost; // cost of (cursor_left) 135 * int _cuf1_cost; // cost of (cursor_right) 136 * int _cud1_cost; // cost of (cursor_down) 137 * int _cuu1_cost; // cost of (cursor_up) 138 * int _cub_cost; // cost of (parm_cursor_left) 139 * int _cuf_cost; // cost of (parm_cursor_right) 140 * int _cud_cost; // cost of (parm_cursor_down) 141 * int _cuu_cost; // cost of (parm_cursor_up) 142 * int _hpa_cost; // cost of (column_address) 143 * int _vpa_cost; // cost of (row_address) 144 * int _ech_cost; // cost of (erase_chars) 145 * int _rep_cost; // cost of (repeat_char) 146 * 147 * The USE_HARD_TABS switch controls whether it is reliable to use tab/backtabs 148 * for local motions. On many systems, it's not, due to uncertainties about 149 * tab delays and whether or not tabs will be expanded in raw mode. If you 150 * have parm_right_cursor, tab motions don't win you a lot anyhow. 151 */ 152 153 #include <curses.priv.h> 154 #include <term.h> 155 #include <ctype.h> 156 157 MODULE_ID("$From: lib_mvcur.c,v 1.67 2000/06/24 21:13:51 tom Exp $") 158 159 #define STRLEN(s) (s != 0) ? strlen(s) : 0 160 161 #define CURRENT_ROW SP->_cursrow /* phys cursor row */ 162 #define CURRENT_COLUMN SP->_curscol /* phys cursor column */ 163 #define CURRENT_ATTR SP->_current_attr /* current phys attribute */ 164 #define REAL_ATTR SP->_current_attr /* phys current attribute */ 165 #define WANT_CHAR(y, x) SP->_newscr->_line[y].text[x] /* desired state */ 166 #define BAUDRATE cur_term->_baudrate /* bits per second */ 167 168 #if defined(MAIN) || defined(NCURSES_TEST) 169 #include <sys/time.h> 170 171 static bool profiling = FALSE; 172 static float diff; 173 #endif /* MAIN */ 174 175 #define OPT_SIZE 512 176 177 static int normalized_cost(const char *const cap, int affcnt); 178 179 #if !HAVE_STRSTR 180 char * 181 _nc_strstr(const char *haystack, const char *needle) 182 { 183 size_t len1 = strlen(haystack); 184 size_t len2 = strlen(needle); 185 char *result = 0; 186 187 while ((len1 != 0) && (len1-- >= len2)) { 188 if (!strncmp(haystack, needle, len2)) { 189 result = haystack; 190 break; 191 } 192 haystack++; 193 } 194 return result; 195 } 196 #endif 197 198 /**************************************************************************** 199 * 200 * Initialization/wrapup (including cost pre-computation) 201 * 202 ****************************************************************************/ 203 204 #ifdef TRACE 205 static int 206 trace_cost_of(const char *capname, const char *cap, int affcnt) 207 { 208 int result = _nc_msec_cost(cap, affcnt); 209 TR(TRACE_CHARPUT | TRACE_MOVE, 210 ("CostOf %s %d %s", capname, result, _nc_visbuf(cap))); 211 return result; 212 } 213 #define CostOf(cap,affcnt) trace_cost_of(#cap,cap,affcnt); 214 215 static int 216 trace_normalized_cost(const char *capname, const char *cap, int affcnt) 217 { 218 int result = normalized_cost(cap, affcnt); 219 TR(TRACE_CHARPUT | TRACE_MOVE, 220 ("NormalizedCost %s %d %s", capname, result, _nc_visbuf(cap))); 221 return result; 222 } 223 #define NormalizedCost(cap,affcnt) trace_normalized_cost(#cap,cap,affcnt); 224 225 #else 226 227 #define CostOf(cap,affcnt) _nc_msec_cost(cap,affcnt); 228 #define NormalizedCost(cap,affcnt) normalized_cost(cap,affcnt); 229 230 #endif 231 232 int 233 _nc_msec_cost(const char *const cap, int affcnt) 234 /* compute the cost of a given operation */ 235 { 236 if (cap == 0) 237 return (INFINITY); 238 else { 239 const char *cp; 240 float cum_cost = 0; 241 242 for (cp = cap; *cp; cp++) { 243 /* extract padding, either mandatory or required */ 244 if (cp[0] == '$' && cp[1] == '<' && strchr(cp, '>')) { 245 float number = 0; 246 247 for (cp += 2; *cp != '>'; cp++) { 248 if (isdigit(*cp)) 249 number = number * 10 + (*cp - '0'); 250 else if (*cp == '*') 251 number *= affcnt; 252 else if (*cp == '.' && (*++cp != '>') && isdigit(*cp)) 253 number += (*cp - '0') / 10.0; 254 } 255 256 #ifdef NCURSES_NO_PADDING 257 if (!(SP->_no_padding)) 258 #endif 259 cum_cost += number * 10; 260 } else 261 cum_cost += SP->_char_padding; 262 } 263 264 return ((int) cum_cost); 265 } 266 } 267 268 static int 269 normalized_cost(const char *const cap, int affcnt) 270 /* compute the effective character-count for an operation (round up) */ 271 { 272 int cost = _nc_msec_cost(cap, affcnt); 273 if (cost != INFINITY) 274 cost = (cost + SP->_char_padding - 1) / SP->_char_padding; 275 return cost; 276 } 277 278 static void 279 reset_scroll_region(void) 280 /* Set the scroll-region to a known state (the default) */ 281 { 282 if (change_scroll_region) { 283 TPUTS_TRACE("change_scroll_region"); 284 putp(tparm(change_scroll_region, 0, screen_lines - 1)); 285 } 286 } 287 288 void 289 _nc_mvcur_resume(void) 290 /* what to do at initialization time and after each shellout */ 291 { 292 /* initialize screen for cursor access */ 293 if (enter_ca_mode) { 294 TPUTS_TRACE("enter_ca_mode"); 295 putp(enter_ca_mode); 296 } 297 298 /* 299 * Doing this here rather than in _nc_mvcur_wrap() ensures that 300 * ncurses programs will see a reset scroll region even if a 301 * program that messed with it died ungracefully. 302 * 303 * This also undoes the effects of terminal init strings that assume 304 * they know the screen size. This is useful when you're running 305 * a vt100 emulation through xterm. 306 */ 307 reset_scroll_region(); 308 SP->_cursrow = SP->_curscol = -1; 309 310 /* restore cursor shape */ 311 if (SP->_cursor != -1) { 312 int cursor = SP->_cursor; 313 SP->_cursor = -1; 314 curs_set(cursor); 315 } 316 } 317 318 void 319 _nc_mvcur_init(void) 320 /* initialize the cost structure */ 321 { 322 /* 323 * 9 = 7 bits + 1 parity + 1 stop. 324 */ 325 SP->_char_padding = (9 * 1000 * 10) / (BAUDRATE > 0 ? BAUDRATE : 9600); 326 if (SP->_char_padding <= 0) 327 SP->_char_padding = 1; /* must be nonzero */ 328 TR(TRACE_CHARPUT | TRACE_MOVE, ("char_padding %d msecs", SP->_char_padding)); 329 330 /* non-parameterized local-motion strings */ 331 SP->_cr_cost = CostOf(carriage_return, 0); 332 SP->_home_cost = CostOf(cursor_home, 0); 333 SP->_ll_cost = CostOf(cursor_to_ll, 0); 334 #if USE_HARD_TABS 335 SP->_ht_cost = CostOf(tab, 0); 336 SP->_cbt_cost = CostOf(back_tab, 0); 337 #endif /* USE_HARD_TABS */ 338 SP->_cub1_cost = CostOf(cursor_left, 0); 339 SP->_cuf1_cost = CostOf(cursor_right, 0); 340 SP->_cud1_cost = CostOf(cursor_down, 0); 341 SP->_cuu1_cost = CostOf(cursor_up, 0); 342 343 SP->_smir_cost = CostOf(enter_insert_mode, 0); 344 SP->_rmir_cost = CostOf(exit_insert_mode, 0); 345 SP->_ip_cost = 0; 346 if (insert_padding) { 347 SP->_ip_cost = CostOf(insert_padding, 0); 348 } 349 350 /* 351 * Assumption: if the terminal has memory_relative addressing, the 352 * initialization strings or smcup will set single-page mode so we 353 * can treat it like absolute screen addressing. This seems to be true 354 * for all cursor_mem_address terminal types in the terminfo database. 355 */ 356 SP->_address_cursor = cursor_address ? cursor_address : cursor_mem_address; 357 358 /* 359 * Parametrized local-motion strings. This static cost computation 360 * depends on the following assumptions: 361 * 362 * (1) They never have * padding. In the entire master terminfo database 363 * as of March 1995, only the obsolete Zenith Z-100 pc violates this. 364 * (Proportional padding is found mainly in insert, delete and scroll 365 * capabilities). 366 * 367 * (2) The average case of cup has two two-digit parameters. Strictly, 368 * the average case for a 24 * 80 screen has ((10*10*(1 + 1)) + 369 * (14*10*(1 + 2)) + (10*70*(2 + 1)) + (14*70*4)) / (24*80) = 3.458 370 * digits of parameters. On a 25x80 screen the average is 3.6197. 371 * On larger screens the value gets much closer to 4. 372 * 373 * (3) The average case of cub/cuf/hpa/ech/rep has 2 digits of parameters 374 * (strictly, (((10 * 1) + (70 * 2)) / 80) = 1.8750). 375 * 376 * (4) The average case of cud/cuu/vpa has 2 digits of parameters 377 * (strictly, (((10 * 1) + (14 * 2)) / 24) = 1.5833). 378 * 379 * All these averages depend on the assumption that all parameter values 380 * are equally probable. 381 */ 382 SP->_cup_cost = CostOf(tparm(SP->_address_cursor, 23, 23), 1); 383 SP->_cub_cost = CostOf(tparm(parm_left_cursor, 23), 1); 384 SP->_cuf_cost = CostOf(tparm(parm_right_cursor, 23), 1); 385 SP->_cud_cost = CostOf(tparm(parm_down_cursor, 23), 1); 386 SP->_cuu_cost = CostOf(tparm(parm_up_cursor, 23), 1); 387 SP->_hpa_cost = CostOf(tparm(column_address, 23), 1); 388 SP->_vpa_cost = CostOf(tparm(row_address, 23), 1); 389 390 /* non-parameterized screen-update strings */ 391 SP->_ed_cost = NormalizedCost(clr_eos, 1); 392 SP->_el_cost = NormalizedCost(clr_eol, 1); 393 SP->_el1_cost = NormalizedCost(clr_bol, 1); 394 SP->_dch1_cost = NormalizedCost(delete_character, 1); 395 SP->_ich1_cost = NormalizedCost(insert_character, 1); 396 397 /* parameterized screen-update strings */ 398 SP->_dch_cost = NormalizedCost(tparm(parm_dch, 23), 1); 399 SP->_ich_cost = NormalizedCost(tparm(parm_ich, 23), 1); 400 SP->_ech_cost = NormalizedCost(tparm(erase_chars, 23), 1); 401 SP->_rep_cost = NormalizedCost(tparm(repeat_char, ' ', 23), 1); 402 403 SP->_cup_ch_cost = NormalizedCost(tparm(SP->_address_cursor, 23, 23), 1); 404 SP->_hpa_ch_cost = NormalizedCost(tparm(column_address, 23), 1); 405 SP->_cuf_ch_cost = NormalizedCost(tparm(parm_right_cursor, 23), 1); 406 SP->_inline_cost = min(SP->_cup_ch_cost, 407 min(SP->_hpa_ch_cost, 408 SP->_cuf_ch_cost)); 409 410 /* pre-compute some capability lengths */ 411 SP->_carriage_return_length = STRLEN(carriage_return); 412 SP->_cursor_home_length = STRLEN(cursor_home); 413 SP->_cursor_to_ll_length = STRLEN(cursor_to_ll); 414 415 /* 416 * If save_cursor is used within enter_ca_mode, we should not use it for 417 * scrolling optimization, since the corresponding restore_cursor is not 418 * nested on the various terminals (vt100, xterm, etc.) which use this 419 * feature. 420 */ 421 if (save_cursor != 0 422 && enter_ca_mode != 0 423 && strstr(enter_ca_mode, save_cursor) != 0) { 424 T(("...suppressed sc/rc capability due to conflict with smcup/rmcup")); 425 save_cursor = 0; 426 restore_cursor = 0; 427 } 428 429 /* 430 * A different, possibly better way to arrange this would be to set 431 * SP->_endwin = TRUE at window initialization time and let this be 432 * called by doupdate's return-from-shellout code. 433 */ 434 _nc_mvcur_resume(); 435 } 436 437 void 438 _nc_mvcur_wrap(void) 439 /* wrap up cursor-addressing mode */ 440 { 441 /* leave cursor at screen bottom */ 442 mvcur(-1, -1, screen_lines - 1, 0); 443 444 /* set cursor to normal mode */ 445 if (SP->_cursor != -1) 446 curs_set(1); 447 448 if (exit_ca_mode) { 449 TPUTS_TRACE("exit_ca_mode"); 450 putp(exit_ca_mode); 451 } 452 /* 453 * Reset terminal's tab counter. There's a long-time bug that 454 * if you exit a "curses" program such as vi or more, tab 455 * forward, and then backspace, the cursor doesn't go to the 456 * right place. The problem is that the kernel counts the 457 * escape sequences that reset things as column positions. 458 * Utter a \r to reset this invisibly. 459 */ 460 _nc_outch('\r'); 461 } 462 463 /**************************************************************************** 464 * 465 * Optimized cursor movement 466 * 467 ****************************************************************************/ 468 469 /* 470 * Perform repeated-append, returning cost 471 */ 472 static inline int 473 repeated_append(int total, int num, int repeat, char *dst, const char *src) 474 { 475 register size_t src_len = strlen(src); 476 register size_t dst_len = STRLEN(dst); 477 478 if ((dst_len + repeat * src_len) < OPT_SIZE - 1) { 479 total += (num * repeat); 480 if (dst) { 481 dst += dst_len; 482 while (repeat-- > 0) { 483 (void) strcpy(dst, src); 484 dst += src_len; 485 } 486 } 487 } else { 488 total = INFINITY; 489 } 490 return total; 491 } 492 493 #ifndef NO_OPTIMIZE 494 #define NEXTTAB(fr) (fr + init_tabs - (fr % init_tabs)) 495 496 /* 497 * Assume back_tab (CBT) does not wrap backwards at the left margin, return 498 * a negative value at that point to simplify the loop. 499 */ 500 #define LASTTAB(fr) ((fr > 0) ? ((fr - 1) / init_tabs) * init_tabs : -1) 501 502 /* Note: we'd like to inline this for speed, but GNU C barfs on the attempt. */ 503 504 static int 505 relative_move(char *result, int from_y, int from_x, int to_y, int to_x, bool ovw) 506 /* move via local motions (cuu/cuu1/cud/cud1/cub1/cub/cuf1/cuf/vpa/hpa) */ 507 { 508 int n, vcost = 0, hcost = 0; 509 510 if (result) 511 result[0] = '\0'; 512 513 if (to_y != from_y) { 514 vcost = INFINITY; 515 516 if (row_address) { 517 if (result) 518 (void) strcpy(result, tparm(row_address, to_y)); 519 vcost = SP->_vpa_cost; 520 } 521 522 if (to_y > from_y) { 523 n = (to_y - from_y); 524 525 if (parm_down_cursor && SP->_cud_cost < vcost) { 526 if (result) 527 (void) strcpy(result, tparm(parm_down_cursor, n)); 528 vcost = SP->_cud_cost; 529 } 530 531 if (cursor_down && (n * SP->_cud1_cost < vcost)) { 532 if (result) 533 result[0] = '\0'; 534 vcost = repeated_append(0, SP->_cud1_cost, n, result, cursor_down); 535 } 536 } else { /* (to_y < from_y) */ 537 n = (from_y - to_y); 538 539 if (parm_up_cursor && SP->_cup_cost < vcost) { 540 if (result) 541 (void) strcpy(result, tparm(parm_up_cursor, n)); 542 vcost = SP->_cup_cost; 543 } 544 545 if (cursor_up && (n * SP->_cuu1_cost < vcost)) { 546 if (result) 547 result[0] = '\0'; 548 vcost = repeated_append(0, SP->_cuu1_cost, n, result, cursor_up); 549 } 550 } 551 552 if (vcost == INFINITY) 553 return (INFINITY); 554 } 555 556 if (result) 557 result += strlen(result); 558 559 if (to_x != from_x) { 560 char str[OPT_SIZE]; 561 562 hcost = INFINITY; 563 564 if (column_address) { 565 if (result) 566 (void) strcpy(result, tparm(column_address, to_x)); 567 hcost = SP->_hpa_cost; 568 } 569 570 if (to_x > from_x) { 571 n = to_x - from_x; 572 573 if (parm_right_cursor && SP->_cuf_cost < hcost) { 574 if (result) 575 (void) strcpy(result, tparm(parm_right_cursor, n)); 576 hcost = SP->_cuf_cost; 577 } 578 579 if (cursor_right) { 580 int lhcost = 0; 581 582 str[0] = '\0'; 583 584 #if USE_HARD_TABS 585 /* use hard tabs, if we have them, to do as much as possible */ 586 if (init_tabs > 0 && tab) { 587 int nxt, fr; 588 589 for (fr = from_x; (nxt = NEXTTAB(fr)) <= to_x; fr = nxt) { 590 lhcost = repeated_append(lhcost, SP->_ht_cost, 1, 591 str, tab); 592 if (lhcost == INFINITY) 593 break; 594 } 595 596 n = to_x - fr; 597 from_x = fr; 598 } 599 #endif /* USE_HARD_TABS */ 600 601 #if defined(REAL_ATTR) && defined(WANT_CHAR) 602 #ifdef BSD_TPUTS 603 /* 604 * If we're allowing BSD-style padding in tputs, don't generate 605 * a string with a leading digit. Otherwise, that will be 606 * interpreted as a padding value rather than sent to the 607 * screen. 608 */ 609 if (ovw 610 && n > 0 611 && vcost == 0 612 && str[0] == '\0' 613 && isdigit(TextOf(WANT_CHAR(to_y, from_x)))) 614 ovw = FALSE; 615 #endif 616 /* 617 * If we have no attribute changes, overwrite is cheaper. 618 * Note: must suppress this by passing in ovw = FALSE whenever 619 * WANT_CHAR would return invalid data. In particular, this 620 * is true between the time a hardware scroll has been done 621 * and the time the structure WANT_CHAR would access has been 622 * updated. 623 */ 624 if (ovw) { 625 int i; 626 627 for (i = 0; i < n; i++) 628 if ((WANT_CHAR(to_y, from_x + i) & A_ATTRIBUTES) != CURRENT_ATTR) { 629 ovw = FALSE; 630 break; 631 } 632 } 633 if (ovw) { 634 char *sp; 635 int i; 636 637 sp = str + strlen(str); 638 639 for (i = 0; i < n; i++) 640 *sp++ = WANT_CHAR(to_y, from_x + i); 641 *sp = '\0'; 642 lhcost += n * SP->_char_padding; 643 } else 644 #endif /* defined(REAL_ATTR) && defined(WANT_CHAR) */ 645 { 646 lhcost = repeated_append(lhcost, SP->_cuf1_cost, n, str, cursor_right); 647 } 648 649 if (lhcost < hcost) { 650 if (result) 651 (void) strcpy(result, str); 652 hcost = lhcost; 653 } 654 } 655 } else { /* (to_x < from_x) */ 656 n = from_x - to_x; 657 658 if (parm_left_cursor && SP->_cub_cost < hcost) { 659 if (result) 660 (void) strcpy(result, tparm(parm_left_cursor, n)); 661 hcost = SP->_cub_cost; 662 } 663 664 if (cursor_left) { 665 int lhcost = 0; 666 667 str[0] = '\0'; 668 669 #if USE_HARD_TABS 670 if (init_tabs > 0 && back_tab) { 671 int nxt, fr; 672 673 for (fr = from_x; (nxt = LASTTAB(fr)) >= to_x; fr = nxt) { 674 lhcost = repeated_append(lhcost, SP->_cbt_cost, 1, 675 str, back_tab); 676 if (lhcost == INFINITY) 677 break; 678 } 679 680 n = fr - to_x; 681 } 682 #endif /* USE_HARD_TABS */ 683 684 lhcost = repeated_append(lhcost, SP->_cub1_cost, n, str, cursor_left); 685 686 if (lhcost < hcost) { 687 if (result) 688 (void) strcpy(result, str); 689 hcost = lhcost; 690 } 691 } 692 } 693 694 if (hcost == INFINITY) 695 return (INFINITY); 696 } 697 698 return (vcost + hcost); 699 } 700 #endif /* !NO_OPTIMIZE */ 701 702 /* 703 * With the machinery set up above, it's conceivable that 704 * onscreen_mvcur could be modified into a recursive function that does 705 * an alpha-beta search of motion space, as though it were a chess 706 * move tree, with the weight function being boolean and the search 707 * depth equated to length of string. However, this would jack up the 708 * computation cost a lot, especially on terminals without a cup 709 * capability constraining the search tree depth. So we settle for 710 * the simpler method below. 711 */ 712 713 static inline int 714 onscreen_mvcur(int yold, int xold, int ynew, int xnew, bool ovw) 715 /* onscreen move from (yold, xold) to (ynew, xnew) */ 716 { 717 char use[OPT_SIZE], *sp; 718 int tactic = 0, newcost, usecost = INFINITY; 719 int t5_cr_cost; 720 721 #if defined(MAIN) || defined(NCURSES_TEST) 722 struct timeval before, after; 723 724 gettimeofday(&before, NULL); 725 #endif /* MAIN */ 726 727 /* tactic #0: use direct cursor addressing */ 728 sp = tparm(SP->_address_cursor, ynew, xnew); 729 if (sp) { 730 tactic = 0; 731 (void) strcpy(use, sp); 732 usecost = SP->_cup_cost; 733 734 #if defined(TRACE) || defined(NCURSES_TEST) 735 if (!(_nc_optimize_enable & OPTIMIZE_MVCUR)) 736 goto nonlocal; 737 #endif /* TRACE */ 738 739 /* 740 * We may be able to tell in advance that the full optimization 741 * will probably not be worth its overhead. Also, don't try to 742 * use local movement if the current attribute is anything but 743 * A_NORMAL...there are just too many ways this can screw up 744 * (like, say, local-movement \n getting mapped to some obscure 745 * character because A_ALTCHARSET is on). 746 */ 747 if (yold == -1 || xold == -1 || NOT_LOCAL(yold, xold, ynew, xnew)) { 748 #if defined(MAIN) || defined(NCURSES_TEST) 749 if (!profiling) { 750 (void) fputs("nonlocal\n", stderr); 751 goto nonlocal; /* always run the optimizer if profiling */ 752 } 753 #else 754 goto nonlocal; 755 #endif /* MAIN */ 756 } 757 } 758 #ifndef NO_OPTIMIZE 759 /* tactic #1: use local movement */ 760 if (yold != -1 && xold != -1 761 && ((newcost = relative_move(NULL, yold, xold, ynew, xnew, ovw)) != INFINITY) 762 && newcost < usecost) { 763 tactic = 1; 764 usecost = newcost; 765 } 766 767 /* tactic #2: use carriage-return + local movement */ 768 if (yold != -1 && carriage_return 769 && ((newcost = relative_move(NULL, yold, 0, ynew, xnew, ovw)) != INFINITY) 770 && SP->_cr_cost + newcost < usecost) { 771 tactic = 2; 772 usecost = SP->_cr_cost + newcost; 773 } 774 775 /* tactic #3: use home-cursor + local movement */ 776 if (cursor_home 777 && ((newcost = relative_move(NULL, 0, 0, ynew, xnew, ovw)) != INFINITY) 778 && SP->_home_cost + newcost < usecost) { 779 tactic = 3; 780 usecost = SP->_home_cost + newcost; 781 } 782 783 /* tactic #4: use home-down + local movement */ 784 if (cursor_to_ll 785 && ((newcost = relative_move(NULL, screen_lines - 1, 0, ynew, xnew, 786 ovw)) != INFINITY) 787 && SP->_ll_cost + newcost < usecost) { 788 tactic = 4; 789 usecost = SP->_ll_cost + newcost; 790 } 791 792 /* 793 * tactic #5: use left margin for wrap to right-hand side, 794 * unless strange wrap behavior indicated by xenl might hose us. 795 */ 796 t5_cr_cost = (xold > 0 ? SP->_cr_cost : 0); 797 if (auto_left_margin && !eat_newline_glitch 798 && yold > 0 && cursor_left 799 && ((newcost = relative_move(NULL, yold - 1, screen_columns - 1, 800 ynew, xnew, ovw)) != INFINITY) 801 && t5_cr_cost + SP->_cub1_cost + newcost < usecost) { 802 tactic = 5; 803 usecost = t5_cr_cost + SP->_cub1_cost + newcost; 804 } 805 806 /* 807 * These cases are ordered by estimated relative frequency. 808 */ 809 switch (tactic) { 810 case 1: 811 (void) relative_move(use, yold, xold, ynew, xnew, ovw); 812 break; 813 case 2: 814 (void) strcpy(use, carriage_return); 815 (void) relative_move(use + SP->_carriage_return_length, 816 yold, 0, ynew, xnew, ovw); 817 break; 818 case 3: 819 (void) strcpy(use, cursor_home); 820 (void) relative_move(use + SP->_cursor_home_length, 821 0, 0, ynew, xnew, ovw); 822 break; 823 case 4: 824 (void) strcpy(use, cursor_to_ll); 825 (void) relative_move(use + SP->_cursor_to_ll_length, 826 screen_lines - 1, 0, ynew, xnew, ovw); 827 break; 828 case 5: 829 use[0] = '\0'; 830 if (xold > 0) 831 (void) strcat(use, carriage_return); 832 (void) strcat(use, cursor_left); 833 (void) relative_move(use + strlen(use), 834 yold - 1, screen_columns - 1, ynew, xnew, ovw); 835 break; 836 } 837 #endif /* !NO_OPTIMIZE */ 838 839 #if defined(MAIN) || defined(NCURSES_TEST) 840 gettimeofday(&after, NULL); 841 diff = after.tv_usec - before.tv_usec 842 + (after.tv_sec - before.tv_sec) * 1000000; 843 if (!profiling) 844 (void) fprintf(stderr, 845 "onscreen: %d msec, %f 28.8Kbps char-equivalents\n", 846 (int) diff, diff / 288); 847 #endif /* MAIN */ 848 849 nonlocal: 850 if (usecost != INFINITY) { 851 TPUTS_TRACE("mvcur"); 852 tputs(use, 1, _nc_outch); 853 return (OK); 854 } else 855 return (ERR); 856 } 857 858 int 859 mvcur(int yold, int xold, int ynew, int xnew) 860 /* optimized cursor move from (yold, xold) to (ynew, xnew) */ 861 { 862 TR(TRACE_MOVE, ("mvcur(%d,%d,%d,%d) called", yold, xold, ynew, xnew)); 863 864 if (yold == ynew && xold == xnew) 865 return (OK); 866 867 /* 868 * Most work here is rounding for terminal boundaries getting the 869 * column position implied by wraparound or the lack thereof and 870 * rolling up the screen to get ynew on the screen. 871 */ 872 873 if (xnew >= screen_columns) { 874 ynew += xnew / screen_columns; 875 xnew %= screen_columns; 876 } 877 if (xold >= screen_columns) { 878 int l; 879 880 l = (xold + 1) / screen_columns; 881 yold += l; 882 if (yold >= screen_lines) 883 l -= (yold - screen_lines - 1); 884 885 while (l > 0) { 886 if (newline) { 887 TPUTS_TRACE("newline"); 888 tputs(newline, 0, _nc_outch); 889 } else 890 putchar('\n'); 891 l--; 892 if (xold > 0) { 893 if (carriage_return) { 894 TPUTS_TRACE("carriage_return"); 895 tputs(carriage_return, 0, _nc_outch); 896 } else 897 putchar('\r'); 898 xold = 0; 899 } 900 } 901 } 902 903 if (yold > screen_lines - 1) 904 yold = screen_lines - 1; 905 if (ynew > screen_lines - 1) 906 ynew = screen_lines - 1; 907 908 /* destination location is on screen now */ 909 return (onscreen_mvcur(yold, xold, ynew, xnew, TRUE)); 910 } 911 912 #if defined(TRACE) || defined(NCURSES_TEST) 913 int _nc_optimize_enable = OPTIMIZE_ALL; 914 #endif 915 916 #if defined(MAIN) || defined(NCURSES_TEST) 917 /**************************************************************************** 918 * 919 * Movement optimizer test code 920 * 921 ****************************************************************************/ 922 923 #include <tic.h> 924 #include <dump_entry.h> 925 926 const char *_nc_progname = "mvcur"; 927 928 static unsigned long xmits; 929 930 /* these override lib_tputs.c */ 931 int 932 tputs(const char *string, int affcnt GCC_UNUSED, int (*outc) (int) GCC_UNUSED) 933 /* stub tputs() that dumps sequences in a visible form */ 934 { 935 if (profiling) 936 xmits += strlen(string); 937 else 938 (void) fputs(_nc_visbuf(string), stdout); 939 return (OK); 940 } 941 942 int 943 putp(const char *string) 944 { 945 return (tputs(string, 1, _nc_outch)); 946 } 947 948 int 949 _nc_outch(int ch) 950 { 951 putc(ch, stdout); 952 return OK; 953 } 954 955 char PC = 0; /* used by termcap library */ 956 speed_t ospeed = 0; /* used by termcap library */ 957 int _nc_nulls_sent = 0; /* used by 'tack' program */ 958 959 int 960 delay_output(int ms GCC_UNUSED) 961 { 962 return OK; 963 } 964 965 static char tname[MAX_ALIAS]; 966 967 static void 968 load_term(void) 969 { 970 (void) setupterm(tname, STDOUT_FILENO, NULL); 971 } 972 973 static int 974 roll(int n) 975 { 976 int i, j; 977 978 i = (RAND_MAX / n) * n; 979 while ((j = rand()) >= i) 980 continue; 981 return (j % n); 982 } 983 984 int 985 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) 986 { 987 (void) strcpy(tname, termname()); 988 load_term(); 989 _nc_setupscreen(lines, columns, stdout); 990 baudrate(); 991 992 _nc_mvcur_init(); 993 NC_BUFFERED(FALSE); 994 995 (void) puts("The mvcur tester. Type ? for help"); 996 997 fputs("smcup:", stdout); 998 putchar('\n'); 999 1000 for (;;) { 1001 int fy, fx, ty, tx, n, i; 1002 char buf[BUFSIZ], capname[BUFSIZ]; 1003 1004 (void) fputs("> ", stdout); 1005 (void) fgets(buf, sizeof(buf), stdin); 1006 1007 if (buf[0] == '?') { 1008 (void) puts("? -- display this help message"); 1009 (void) 1010 puts("fy fx ty tx -- (4 numbers) display (fy,fx)->(ty,tx) move"); 1011 (void) puts("s[croll] n t b m -- display scrolling sequence"); 1012 (void) 1013 printf("r[eload] -- reload terminal info for %s\n", 1014 termname()); 1015 (void) 1016 puts("l[oad] <term> -- load terminal info for type <term>"); 1017 (void) puts("d[elete] <cap> -- delete named capability"); 1018 (void) puts("i[nspect] -- display terminal capabilities"); 1019 (void) 1020 puts("c[ost] -- dump cursor-optimization cost table"); 1021 (void) puts("o[optimize] -- toggle movement optimization"); 1022 (void) 1023 puts("t[orture] <num> -- torture-test with <num> random moves"); 1024 (void) puts("q[uit] -- quit the program"); 1025 } else if (sscanf(buf, "%d %d %d %d", &fy, &fx, &ty, &tx) == 4) { 1026 struct timeval before, after; 1027 1028 putchar('"'); 1029 1030 gettimeofday(&before, NULL); 1031 mvcur(fy, fx, ty, tx); 1032 gettimeofday(&after, NULL); 1033 1034 printf("\" (%ld msec)\n", 1035 (long) (after.tv_usec - before.tv_usec + (after.tv_sec - 1036 before.tv_sec) * 1000000)); 1037 } else if (sscanf(buf, "s %d %d %d %d", &fy, &fx, &ty, &tx) == 4) { 1038 struct timeval before, after; 1039 1040 putchar('"'); 1041 1042 gettimeofday(&before, NULL); 1043 _nc_scrolln(fy, fx, ty, tx); 1044 gettimeofday(&after, NULL); 1045 1046 printf("\" (%ld msec)\n", 1047 (long) (after.tv_usec - before.tv_usec + (after.tv_sec - 1048 before.tv_sec) * 1000000)); 1049 } else if (buf[0] == 'r') { 1050 (void) strcpy(tname, termname()); 1051 load_term(); 1052 } else if (sscanf(buf, "l %s", tname) == 1) { 1053 load_term(); 1054 } else if (sscanf(buf, "d %s", capname) == 1) { 1055 struct name_table_entry const *np = _nc_find_entry(capname, 1056 _nc_info_hash_table); 1057 1058 if (np == NULL) 1059 (void) printf("No such capability as \"%s\"\n", capname); 1060 else { 1061 switch (np->nte_type) { 1062 case BOOLEAN: 1063 cur_term->type.Booleans[np->nte_index] = FALSE; 1064 (void) 1065 printf("Boolean capability `%s' (%d) turned off.\n", 1066 np->nte_name, np->nte_index); 1067 break; 1068 1069 case NUMBER: 1070 cur_term->type.Numbers[np->nte_index] = ABSENT_NUMERIC; 1071 (void) printf("Number capability `%s' (%d) set to -1.\n", 1072 np->nte_name, np->nte_index); 1073 break; 1074 1075 case STRING: 1076 cur_term->type.Strings[np->nte_index] = ABSENT_STRING; 1077 (void) printf("String capability `%s' (%d) deleted.\n", 1078 np->nte_name, np->nte_index); 1079 break; 1080 } 1081 } 1082 } else if (buf[0] == 'i') { 1083 dump_init((char *) NULL, F_TERMINFO, S_TERMINFO, 70, 0, FALSE); 1084 dump_entry(&cur_term->type, FALSE, TRUE, 0); 1085 putchar('\n'); 1086 } else if (buf[0] == 'o') { 1087 if (_nc_optimize_enable & OPTIMIZE_MVCUR) { 1088 _nc_optimize_enable &= ~OPTIMIZE_MVCUR; 1089 (void) puts("Optimization is now off."); 1090 } else { 1091 _nc_optimize_enable |= OPTIMIZE_MVCUR; 1092 (void) puts("Optimization is now on."); 1093 } 1094 } 1095 /* 1096 * You can use the `t' test to profile and tune the movement 1097 * optimizer. Use iteration values in three digits or more. 1098 * At above 5000 iterations the profile timing averages are stable 1099 * to within a millisecond or three. 1100 * 1101 * The `overhead' field of the report will help you pick a 1102 * COMPUTE_OVERHEAD figure appropriate for your processor and 1103 * expected line speed. The `total estimated time' is 1104 * computation time plus a character-transmission time 1105 * estimate computed from the number of transmits and the baud 1106 * rate. 1107 * 1108 * Use this together with the `o' command to get a read on the 1109 * optimizer's effectiveness. Compare the total estimated times 1110 * for `t' runs of the same length in both optimized and un-optimized 1111 * modes. As long as the optimized times are less, the optimizer 1112 * is winning. 1113 */ 1114 else if (sscanf(buf, "t %d", &n) == 1) { 1115 float cumtime = 0, perchar; 1116 int speeds[] = 1117 {2400, 9600, 14400, 19200, 28800, 38400, 0}; 1118 1119 srand((unsigned) (getpid() + time((time_t *) 0))); 1120 profiling = TRUE; 1121 xmits = 0; 1122 for (i = 0; i < n; i++) { 1123 /* 1124 * This does a move test between two random locations, 1125 * Random moves probably short-change the optimizer, 1126 * which will work better on the short moves probably 1127 * typical of doupdate()'s usage pattern. Still, 1128 * until we have better data... 1129 */ 1130 #ifdef FIND_COREDUMP 1131 int from_y = roll(lines); 1132 int to_y = roll(lines); 1133 int from_x = roll(columns); 1134 int to_x = roll(columns); 1135 1136 printf("(%d,%d) -> (%d,%d)\n", from_y, from_x, to_y, to_x); 1137 mvcur(from_y, from_x, to_y, to_x); 1138 #else 1139 mvcur(roll(lines), roll(columns), roll(lines), roll(columns)); 1140 #endif /* FIND_COREDUMP */ 1141 if (diff) 1142 cumtime += diff; 1143 } 1144 profiling = FALSE; 1145 1146 /* 1147 * Average milliseconds per character optimization time. 1148 * This is the key figure to watch when tuning the optimizer. 1149 */ 1150 perchar = cumtime / n; 1151 1152 (void) printf("%d moves (%ld chars) in %d msec, %f msec each:\n", 1153 n, xmits, (int) cumtime, perchar); 1154 1155 for (i = 0; speeds[i]; i++) { 1156 /* 1157 * Total estimated time for the moves, computation and 1158 * transmission both. Transmission time is an estimate 1159 * assuming 9 bits/char, 8 bits + 1 stop bit. 1160 */ 1161 float totalest = cumtime + xmits * 9 * 1e6 / speeds[i]; 1162 1163 /* 1164 * Per-character optimization overhead in character transmits 1165 * at the current speed. Round this to the nearest integer 1166 * to figure COMPUTE_OVERHEAD for the speed. 1167 */ 1168 float overhead = speeds[i] * perchar / 1e6; 1169 1170 (void) 1171 printf("%6d bps: %3.2f char-xmits overhead; total estimated time %15.2f\n", 1172 speeds[i], overhead, totalest); 1173 } 1174 } else if (buf[0] == 'c') { 1175 (void) printf("char padding: %d\n", SP->_char_padding); 1176 (void) printf("cr cost: %d\n", SP->_cr_cost); 1177 (void) printf("cup cost: %d\n", SP->_cup_cost); 1178 (void) printf("home cost: %d\n", SP->_home_cost); 1179 (void) printf("ll cost: %d\n", SP->_ll_cost); 1180 #if USE_HARD_TABS 1181 (void) printf("ht cost: %d\n", SP->_ht_cost); 1182 (void) printf("cbt cost: %d\n", SP->_cbt_cost); 1183 #endif /* USE_HARD_TABS */ 1184 (void) printf("cub1 cost: %d\n", SP->_cub1_cost); 1185 (void) printf("cuf1 cost: %d\n", SP->_cuf1_cost); 1186 (void) printf("cud1 cost: %d\n", SP->_cud1_cost); 1187 (void) printf("cuu1 cost: %d\n", SP->_cuu1_cost); 1188 (void) printf("cub cost: %d\n", SP->_cub_cost); 1189 (void) printf("cuf cost: %d\n", SP->_cuf_cost); 1190 (void) printf("cud cost: %d\n", SP->_cud_cost); 1191 (void) printf("cuu cost: %d\n", SP->_cuu_cost); 1192 (void) printf("hpa cost: %d\n", SP->_hpa_cost); 1193 (void) printf("vpa cost: %d\n", SP->_vpa_cost); 1194 } else if (buf[0] == 'x' || buf[0] == 'q') 1195 break; 1196 else 1197 (void) puts("Invalid command."); 1198 } 1199 1200 (void) fputs("rmcup:", stdout); 1201 _nc_mvcur_wrap(); 1202 putchar('\n'); 1203 1204 return (0); 1205 } 1206 1207 #endif /* MAIN */ 1208 1209 /* lib_mvcur.c ends here */ 1210