📄 ncurses.c
字号:
wgetch_test(int level, WINDOW *win, int delay){ char buf[BUFSIZ]; int first_y, first_x; int c; int incount = 0; bool flags[256]; bool blocking = (delay < 0); int y, x; memset(flags, FALSE, sizeof(flags)); flags[UChar('k')] = (win == stdscr); setup_getch(win, flags); wtimeout(win, delay); getyx(win, first_y, first_x); wgetch_help(win, flags); wsetscrreg(win, first_y, getmaxy(win) - 1); scrollok(win, TRUE); for (;;) { while ((c = wGetchar(win)) == ERR) { incount++; if (blocking) { (void) wprintw(win, "%05d: input error", incount); break; } else { (void) wprintw(win, "%05d: input timed out", incount); } wgetch_wrap(win, first_y); } if (c == ERR && blocking) { wprintw(win, "ERR"); wgetch_wrap(win, first_y); } else if (c == 'x' || c == 'q') { break; } else if (c == 'e') { flags[UChar('e')] = !flags[UChar('e')]; setup_getch(win, flags); wgetch_help(win, flags); } else if (c == 'g') { waddstr(win, "getstr test: "); echo(); wgetnstr(win, buf, sizeof(buf) - 1); noecho(); wprintw(win, "I saw %d characters:\n\t`%s'.", (int) strlen(buf), buf); wclrtoeol(win); wgetch_wrap(win, first_y); } else if (c == 'k') { flags[UChar('k')] = !flags[UChar('k')]; setup_getch(win, flags); wgetch_help(win, flags); } else if (c == 'm') { flags[UChar('m')] = !flags[UChar('m')]; setup_getch(win, flags); wgetch_help(win, flags); } else if (c == 's') { ShellOut(TRUE); } else if (c == 'w') { int high = getmaxy(win) - 1 - first_y + 1; int wide = getmaxx(win) - first_x; int old_y, old_x; int new_y = first_y + getbegy(win); int new_x = first_x + getbegx(win); getyx(win, old_y, old_x); if (high > 2 && wide > 2) { WINDOW *wb = newwin(high, wide, new_y, new_x); WINDOW *wi = newwin(high - 2, wide - 2, new_y + 1, new_x + 1); box(wb, 0, 0); wrefresh(wb); wmove(wi, 0, 0); remember_boxes(level, wi, wb); wgetch_test(level + 1, wi, delay); delwin(wi); delwin(wb); wgetch_help(win, flags); wmove(win, old_y, old_x); touchwin(win); wrefresh(win); doupdate(); }#ifdef SIGTSTP } else if (c == 'z') { kill(getpid(), SIGTSTP);#endif } else { wprintw(win, "Key pressed: %04o ", c);#ifdef NCURSES_MOUSE_VERSION if (c == KEY_MOUSE) { MEVENT event; getmouse(&event); wprintw(win, "KEY_MOUSE, %s", mouse_decode(&event)); getyx(win, y, x); move(event.y, event.x); addch('*'); wmove(win, y, x); } else#endif /* NCURSES_MOUSE_VERSION */ if (c >= KEY_MIN) {#if defined(NCURSES_VERSION) && defined(KEY_RESIZE) && HAVE_WRESIZE if (c == KEY_RESIZE) { resize_boxes(level, win); }#endif (void) waddstr(win, keyname(c)); } else if (c > 0x80) { int c2 = (c & 0x7f); if (isprint(c2)) (void) wprintw(win, "M-%c", c2); else (void) wprintw(win, "M-%s", unctrl(c2)); waddstr(win, " (high-half character)"); } else { if (isprint(c)) (void) wprintw(win, "%c (ASCII printable character)", c); else (void) wprintw(win, "%s (ASCII control character)", unctrl(c)); } wgetch_wrap(win, first_y); } } wtimeout(win, -1);}static intbegin_getch_test(void){ char buf[BUFSIZ]; int delay; refresh();#ifdef NCURSES_MOUSE_VERSION mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);#endif (void) printw("Delay in 10ths of a second (<CR> for blocking input)? "); echo(); getnstr(buf, sizeof(buf) - 1); noecho(); nonl(); if (isdigit(UChar(buf[0]))) { delay = atoi(buf) * 100; } else { delay = -1; } raw(); move(5, 0); return delay;}static voidfinish_getch_test(void){#ifdef NCURSES_MOUSE_VERSION mousemask(0, (mmask_t *) 0);#endif erase(); noraw(); nl(); endwin();}static voidgetch_test(void){ int delay = begin_getch_test(); wgetch_test(0, stdscr, delay); finish_getch_test();}#if USE_WIDEC_SUPPORT/* * For wgetch_test(), we create pairs of windows - one for a box, one for text. * Resize both and paint the box in the parent. */#ifdef KEY_RESIZEstatic voidresize_wide_boxes(int level, WINDOW *win){ unsigned n; int base = 5; int high = LINES - base; int wide = COLS; touchwin(stdscr); wnoutrefresh(stdscr); /* FIXME: this chunk should be done in resizeterm() */ slk_touch(); slk_clear(); slk_noutrefresh(); for (n = 0; (int) n < level; ++n) { wresize(winstack[n].frame, high, wide); wresize(winstack[n].text, high - 2, wide - 2); high -= 2; wide -= 2; werase(winstack[n].text); box_set(winstack[n].frame, 0, 0); wnoutrefresh(winstack[n].frame); wprintw(winstack[n].text, "size %dx%d\n", getmaxy(winstack[n].text), getmaxx(winstack[n].text)); wnoutrefresh(winstack[n].text); if (winstack[n].text == win) break; } doupdate();}#endif /* KEY_RESIZE */static char *wcstos(const wchar_t * src){ int need; mbstate_t state; char *result = 0; const wchar_t *tmp = src; memset(&state, 0, sizeof(state)); if ((need = wcsrtombs(0, &tmp, 0, &state)) > 0) { result = (char *) calloc(need + 1, 1); tmp = src; if (wcsrtombs(result, &tmp, need, &state) != (size_t) need) { free(result); result = 0; } } return result;}static voidwget_wch_test(int level, WINDOW *win, int delay){ wchar_t buf[BUFSIZ]; int first_y, first_x; wint_t c; int incount = 0; bool flags[256]; bool blocking = (delay < 0); int y, x, code; char *temp; memset(flags, FALSE, sizeof(flags)); flags[UChar('k')] = (win == stdscr); setup_getch(win, flags); wtimeout(win, delay); getyx(win, first_y, first_x); wgetch_help(win, flags); wsetscrreg(win, first_y, getmaxy(win) - 1); scrollok(win, TRUE); for (;;) { while ((code = wGet_wchar(win, &c)) == ERR) { incount++; if (blocking) { (void) wprintw(win, "%05d: input error", incount); break; } else { (void) wprintw(win, "%05d: input timed out", incount); } wgetch_wrap(win, first_y); } if (code == ERR && blocking) { wprintw(win, "ERR"); wgetch_wrap(win, first_y); } else if (c == 'x' || c == 'q') { break; } else if (c == 'e') { flags[UChar('e')] = !flags[UChar('e')]; setup_getch(win, flags); wgetch_help(win, flags); } else if (c == 'g') { waddstr(win, "getstr test: "); echo(); wgetn_wstr(win, (wint_t *) buf, sizeof(buf) - 1); noecho(); if ((temp = wcstos(buf)) != 0) { wprintw(win, "I saw %d characters:\n\t`%s'.", wcslen(buf), temp); free(temp); } else { wprintw(win, "I saw %d characters (cannot convert).", wcslen(buf)); } wclrtoeol(win); wgetch_wrap(win, first_y); } else if (c == 'k') { flags[UChar('k')] = !flags[UChar('k')]; setup_getch(win, flags); wgetch_help(win, flags); } else if (c == 'm') { flags[UChar('m')] = !flags[UChar('m')]; setup_getch(win, flags); wgetch_help(win, flags); } else if (c == 's') { ShellOut(TRUE); } else if (c == 'w') { int high = getmaxy(win) - 1 - first_y + 1; int wide = getmaxx(win) - first_x; int old_y, old_x; int new_y = first_y + getbegy(win); int new_x = first_x + getbegx(win); getyx(win, old_y, old_x); if (high > 2 && wide > 2) { WINDOW *wb = newwin(high, wide, new_y, new_x); WINDOW *wi = newwin(high - 2, wide - 2, new_y + 1, new_x + 1); box_set(wb, 0, 0); wrefresh(wb); wmove(wi, 0, 0); remember_boxes(level, wi, wb); wget_wch_test(level + 1, wi, delay); delwin(wi); delwin(wb); wgetch_help(win, flags); wmove(win, old_y, old_x); touchwin(win); wrefresh(win); }#ifdef SIGTSTP } else if (c == 'z') { kill(getpid(), SIGTSTP);#endif } else { wprintw(win, "Key pressed: %04o ", c);#ifdef NCURSES_MOUSE_VERSION if (c == KEY_MOUSE) { MEVENT event; getmouse(&event); wprintw(win, "KEY_MOUSE, %s", mouse_decode(&event)); getyx(win, y, x); move(event.y, event.x); addch('*'); wmove(win, y, x); } else#endif /* NCURSES_MOUSE_VERSION */ if (code == KEY_CODE_YES) {#ifdef KEY_RESIZE if (c == KEY_RESIZE) { resize_wide_boxes(level, win); }#endif (void) waddstr(win, key_name(c)); } else { if (c < 256 && iscntrl(c)) { (void) wprintw(win, "%s (control character)", unctrl(c)); } else { wchar_t c2 = c; waddnwstr(win, &c2, 1); (void) wprintw(win, " = %#x (printable character)", c); } } wgetch_wrap(win, first_y); } } wtimeout(win, -1);}static voidget_wch_test(void){ int delay = begin_getch_test(); wget_wch_test(0, stdscr, delay); finish_getch_test();}#endif/**************************************************************************** * * Character attributes test * ****************************************************************************/#define MAX_ATTRSTRING 31#define LEN_ATTRSTRING 26static char attr_test_string[] = "abcde fghij klmno pqrst uvwxy z";static voidadjust_attr_string(int adjust){ int first = ((int) UChar(attr_test_string[0])) + adjust; int last = first + LEN_ATTRSTRING; if (first >= ' ' && last <= '~') { /* 32..126 */ int j, k; for (j = 0, k = first; k <= last; ++j, ++k) { attr_test_string[j] = k; if (((k + 1 - first) % 5) == 0) { ++j; if (j < MAX_ATTRSTRING) attr_test_string[j] = ' '; } } while (j < MAX_ATTRSTRING) attr_test_string[j++] = ' '; attr_test_string[j] = '\0'; } else { beep(); }}static intshow_attr(int row, int skip, chtype attr, const char *name){ int ncv = tigetnum("ncv"); chtype test = attr & ~A_ALTCHARSET; mvprintw(row, 8, "%s mode:", name); mvprintw(row, 24, "|"); if (skip) printw("%*s", skip, " "); attrset(attr); /* * If we're to write a string in the alternate character set, it is not * sufficient to just set A_ALTCHARSET. We have to perform the mapping * that corresponds. This is not needed for vt100-compatible devices * because the acs_map[] is 1:1, but for PC-style devices such as Linux * console, the acs_map[] is scattered about the range. * * The addch/addstr functions do not themselves do this mapping, since it * is possible to turn off the A_ALTCHARSET flag for the characters which * are added, and it would be an unexpected result to have the mapped * characters visible on the screen. */ if (attr & A_ALTCHARSET) { const char *s; int ch; for (s = attr_test_string; *s != '\0'; ++s) { ch = UChar(*s); addch(ch); } } else { addstr(attr_test_string); } attroff(attr); if (skip) printw("%*s", skip, " "); printw("|"); if (test != A_NORMAL) { if (!(termattrs() & test)) { printw(" (N/A)"); } else if (ncv > 0 && (getbkgd(stdscr) & A_COLOR)) { static const chtype table[] = { A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK, A_DIM, A_BOLD, A_INVIS, A_PROTECT, A_ALTCHARSET }; unsigned n; bool found = FALSE; for (n = 0; n < SIZEOF(table); n++) { if ((table[n] & attr) != 0 && ((1 << n) & ncv) != 0) { found = TRUE; break; } } if (found) printw(" (NCV)"); } } return row + 2;}static boolattr_getc(int *skip, int *fg, int *bg, int *ac){ int ch = Getchar(); if (isdigit(ch)) { *skip = (ch - '0'); } else if (ch == CTRL('L')) { touchwin(stdscr); touchwin(curscr); wrefresh(curscr); } else { switch (ch) { case 'a': *ac = 0; break; case 'A': *ac = A_ALTCHARSET; break; case '<': adjust_attr_string(-1); break; case '>': adjust_attr_string(1); break; default: if (has_colors()) { switch (ch) { case 'f': *fg = (*fg + 1); break; case 'F': *fg = (*fg - 1); break; case 'b': *bg = (*bg + 1); break; case 'B': *bg = (*bg - 1); break; default: return FALSE; } if (*fg >= max_colors) *fg = 0; if (*fg < 0) *fg = max_colors - 1; if (*bg >= max_colors) *bg = 0; if (*bg < 0) *bg = max_colors - 1; } break; } } return TRUE;}static voidattr_test(void)/* test text attributes */{ int n; int skip = tigetnum("xmc"); int fg = COLOR_BLACK; /* color pair 0 is special */ int bg = COLOR_BLACK; int ac = 0; bool *pairs = (bool *) calloc(max_pairs, sizeof(bool)); pairs[0] = TRUE; if (skip < 0) skip = 0; n = skip; /* make it easy */ do { int row = 2; int normal = A_NORMAL | BLANK;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -