📄 lib_mvcur.c
字号:
diff = after.tv_usec - before.tv_usec + (after.tv_sec - before.tv_sec) * 1000000; if (!profiling) (void) fprintf(stderr, "onscreen: %d microsec, %f 28.8Kbps char-equivalents\n", (int) diff, diff / 288);#endif /* MAIN */ if (usecost != INFINITY) { TPUTS_TRACE("mvcur"); tputs(buffer, 1, _nc_outch); SP->_cursrow = ynew; SP->_curscol = xnew; return (OK); } else return (ERR);}NCURSES_EXPORT(int)mvcur(int yold, int xold, int ynew, int xnew)/* optimized cursor move from (yold, xold) to (ynew, xnew) */{ NCURSES_CH_T oldattr; int code; TR(TRACE_CALLS | TRACE_MOVE, (T_CALLED("mvcur(%d,%d,%d,%d)"), yold, xold, ynew, xnew)); if (SP == 0) { code = ERR; } else if (yold == ynew && xold == xnew) { code = OK; } else { /* * Most work here is rounding for terminal boundaries getting the * column position implied by wraparound or the lack thereof and * rolling up the screen to get ynew on the screen. */ if (xnew >= screen_columns) { ynew += xnew / screen_columns; xnew %= screen_columns; } /* * Force restore even if msgr is on when we're in an alternate * character set -- these have a strong tendency to screw up the CR & * LF used for local character motions! */ oldattr = SCREEN_ATTRS(SP); if ((AttrOf(oldattr) & A_ALTCHARSET) || (AttrOf(oldattr) && !move_standout_mode)) { TR(TRACE_CHARPUT, ("turning off (%#lx) %s before move", (unsigned long) AttrOf(oldattr), _traceattr(AttrOf(oldattr)))); (void) VIDATTR(A_NORMAL, 0); } if (xold >= screen_columns) { int l; if (SP->_nl) { l = (xold + 1) / screen_columns; yold += l; if (yold >= screen_lines) l -= (yold - screen_lines - 1); if (l > 0) { if (carriage_return) { TPUTS_TRACE("carriage_return"); putp(carriage_return); } else _nc_outch('\r'); xold = 0; while (l > 0) { if (newline) { TPUTS_TRACE("newline"); putp(newline); } else _nc_outch('\n'); l--; } } } else { /* * If caller set nonl(), we cannot really use newlines to * position to the next row. */ xold = -1; yold = -1; } } if (yold > screen_lines - 1) yold = screen_lines - 1; if (ynew > screen_lines - 1) ynew = screen_lines - 1; /* destination location is on screen now */ code = onscreen_mvcur(yold, xold, ynew, xnew, TRUE); /* * Restore attributes if we disabled them before moving. */ if (!SameAttrOf(oldattr, SCREEN_ATTRS(SP))) { TR(TRACE_CHARPUT, ("turning on (%#lx) %s after move", (unsigned long) AttrOf(oldattr), _traceattr(AttrOf(oldattr)))); (void) VIDATTR(AttrOf(oldattr), GetPair(oldattr)); } } returnCode(code);}#if defined(TRACE) || defined(NCURSES_TEST)NCURSES_EXPORT_VAR(int) _nc_optimize_enable = OPTIMIZE_ALL;#endif#if defined(MAIN) || defined(NCURSES_TEST)/**************************************************************************** * * Movement optimizer test code * ****************************************************************************/#include <tic.h>#include <dump_entry.h>NCURSES_EXPORT_VAR(const char *) _nc_progname = "mvcur";static unsigned long xmits;/* these override lib_tputs.c */NCURSES_EXPORT(int)tputs(const char *string, int affcnt GCC_UNUSED, int (*outc) (int) GCC_UNUSED)/* stub tputs() that dumps sequences in a visible form */{ if (profiling) xmits += strlen(string); else (void) fputs(_nc_visbuf(string), stdout); return (OK);}NCURSES_EXPORT(int)putp(const char *string){ return (tputs(string, 1, _nc_outch));}NCURSES_EXPORT(int)_nc_outch(int ch){ putc(ch, stdout); return OK;}NCURSES_EXPORT(int)delay_output(int ms GCC_UNUSED){ return OK;}static char tname[PATH_MAX];static voidload_term(void){ (void) setupterm(tname, STDOUT_FILENO, NULL);}static introll(int n){ int i, j; i = (RAND_MAX / n) * n; while ((j = rand()) >= i) continue; return (j % n);}intmain(int argc GCC_UNUSED, char *argv[]GCC_UNUSED){ (void) strcpy(tname, termname()); load_term(); _nc_setupscreen(lines, columns, stdout); baudrate(); _nc_mvcur_init(); NC_BUFFERED(FALSE); (void) puts("The mvcur tester. Type ? for help"); fputs("smcup:", stdout); putchar('\n'); for (;;) { int fy, fx, ty, tx, n, i; char buf[BUFSIZ], capname[BUFSIZ]; (void) fputs("> ", stdout); (void) fgets(buf, sizeof(buf), stdin); if (buf[0] == '?') { (void) puts("? -- display this help message"); (void) puts("fy fx ty tx -- (4 numbers) display (fy,fx)->(ty,tx) move"); (void) puts("s[croll] n t b m -- display scrolling sequence"); (void) printf("r[eload] -- reload terminal info for %s\n", termname()); (void) puts("l[oad] <term> -- load terminal info for type <term>"); (void) puts("d[elete] <cap> -- delete named capability"); (void) puts("i[nspect] -- display terminal capabilities"); (void) puts("c[ost] -- dump cursor-optimization cost table"); (void) puts("o[optimize] -- toggle movement optimization"); (void) puts("t[orture] <num> -- torture-test with <num> random moves"); (void) puts("q[uit] -- quit the program"); } else if (sscanf(buf, "%d %d %d %d", &fy, &fx, &ty, &tx) == 4) { struct timeval before, after; putchar('"'); gettimeofday(&before, NULL); mvcur(fy, fx, ty, tx); gettimeofday(&after, NULL); printf("\" (%ld msec)\n", (long) (after.tv_usec - before.tv_usec + (after.tv_sec - before.tv_sec) * 1000000)); } else if (sscanf(buf, "s %d %d %d %d", &fy, &fx, &ty, &tx) == 4) { struct timeval before, after; putchar('"'); gettimeofday(&before, NULL); _nc_scrolln(fy, fx, ty, tx); gettimeofday(&after, NULL); printf("\" (%ld msec)\n", (long) (after.tv_usec - before.tv_usec + (after.tv_sec - before.tv_sec) * 1000000)); } else if (buf[0] == 'r') { (void) strcpy(tname, termname()); load_term(); } else if (sscanf(buf, "l %s", tname) == 1) { load_term(); } else if (sscanf(buf, "d %s", capname) == 1) { struct name_table_entry const *np = _nc_find_entry(capname, _nc_info_hash_table); if (np == NULL) (void) printf("No such capability as \"%s\"\n", capname); else { switch (np->nte_type) { case BOOLEAN: cur_term->type.Booleans[np->nte_index] = FALSE; (void) printf("Boolean capability `%s' (%d) turned off.\n", np->nte_name, np->nte_index); break; case NUMBER: cur_term->type.Numbers[np->nte_index] = ABSENT_NUMERIC; (void) printf("Number capability `%s' (%d) set to -1.\n", np->nte_name, np->nte_index); break; case STRING: cur_term->type.Strings[np->nte_index] = ABSENT_STRING; (void) printf("String capability `%s' (%d) deleted.\n", np->nte_name, np->nte_index); break; } } } else if (buf[0] == 'i') { dump_init((char *) NULL, F_TERMINFO, S_TERMINFO, 70, 0, FALSE); dump_entry(&cur_term->type, FALSE, TRUE, 0, 0, 0); putchar('\n'); } else if (buf[0] == 'o') { if (_nc_optimize_enable & OPTIMIZE_MVCUR) { _nc_optimize_enable &= ~OPTIMIZE_MVCUR; (void) puts("Optimization is now off."); } else { _nc_optimize_enable |= OPTIMIZE_MVCUR; (void) puts("Optimization is now on."); } } /* * You can use the `t' test to profile and tune the movement * optimizer. Use iteration values in three digits or more. * At above 5000 iterations the profile timing averages are stable * to within a millisecond or three. * * The `overhead' field of the report will help you pick a * COMPUTE_OVERHEAD figure appropriate for your processor and * expected line speed. The `total estimated time' is * computation time plus a character-transmission time * estimate computed from the number of transmits and the baud * rate. * * Use this together with the `o' command to get a read on the * optimizer's effectiveness. Compare the total estimated times * for `t' runs of the same length in both optimized and un-optimized * modes. As long as the optimized times are less, the optimizer * is winning. */ else if (sscanf(buf, "t %d", &n) == 1) { float cumtime = 0.0, perchar; int speeds[] = {2400, 9600, 14400, 19200, 28800, 38400, 0}; srand((unsigned) (getpid() + time((time_t *) 0))); profiling = TRUE; xmits = 0; for (i = 0; i < n; i++) { /* * This does a move test between two random locations, * Random moves probably short-change the optimizer, * which will work better on the short moves probably * typical of doupdate()'s usage pattern. Still, * until we have better data... */#ifdef FIND_COREDUMP int from_y = roll(lines); int to_y = roll(lines); int from_x = roll(columns); int to_x = roll(columns); printf("(%d,%d) -> (%d,%d)\n", from_y, from_x, to_y, to_x); mvcur(from_y, from_x, to_y, to_x);#else mvcur(roll(lines), roll(columns), roll(lines), roll(columns));#endif /* FIND_COREDUMP */ if (diff) cumtime += diff; } profiling = FALSE; /* * Average milliseconds per character optimization time. * This is the key figure to watch when tuning the optimizer. */ perchar = cumtime / n; (void) printf("%d moves (%ld chars) in %d msec, %f msec each:\n", n, xmits, (int) cumtime, perchar); for (i = 0; speeds[i]; i++) { /* * Total estimated time for the moves, computation and * transmission both. Transmission time is an estimate * assuming 9 bits/char, 8 bits + 1 stop bit. */ float totalest = cumtime + xmits * 9 * 1e6 / speeds[i]; /* * Per-character optimization overhead in character transmits * at the current speed. Round this to the nearest integer * to figure COMPUTE_OVERHEAD for the speed. */ float overhead = speeds[i] * perchar / 1e6; (void) printf("%6d bps: %3.2f char-xmits overhead; total estimated time %15.2f\n", speeds[i], overhead, totalest); } } else if (buf[0] == 'c') { (void) printf("char padding: %d\n", SP->_char_padding); (void) printf("cr cost: %d\n", SP->_cr_cost); (void) printf("cup cost: %d\n", SP->_cup_cost); (void) printf("home cost: %d\n", SP->_home_cost); (void) printf("ll cost: %d\n", SP->_ll_cost);#if USE_HARD_TABS (void) printf("ht cost: %d\n", SP->_ht_cost); (void) printf("cbt cost: %d\n", SP->_cbt_cost);#endif /* USE_HARD_TABS */ (void) printf("cub1 cost: %d\n", SP->_cub1_cost); (void) printf("cuf1 cost: %d\n", SP->_cuf1_cost); (void) printf("cud1 cost: %d\n", SP->_cud1_cost); (void) printf("cuu1 cost: %d\n", SP->_cuu1_cost); (void) printf("cub cost: %d\n", SP->_cub_cost); (void) printf("cuf cost: %d\n", SP->_cuf_cost); (void) printf("cud cost: %d\n", SP->_cud_cost); (void) printf("cuu cost: %d\n", SP->_cuu_cost); (void) printf("hpa cost: %d\n", SP->_hpa_cost); (void) printf("vpa cost: %d\n", SP->_vpa_cost); } else if (buf[0] == 'x' || buf[0] == 'q') break; else (void) puts("Invalid command."); } (void) fputs("rmcup:", stdout); _nc_mvcur_wrap(); putchar('\n'); return (0);}#endif /* MAIN *//* lib_mvcur.c ends here */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -