+ 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) {
+ unsigned c2 = (c & 0x7f);
+ if (isprint(c2))
+ (void) wprintw(win, "M-%c", UChar(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(UChar(c)));
+ }
+ wgetch_wrap(win, first_y);
+ }
+ }
+
+ wtimeout(win, -1);
+}
+
+static int
+begin_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 void
+finish_getch_test(void)
+{
+#ifdef NCURSES_MOUSE_VERSION
+ mousemask(0, (mmask_t *) 0);
+#endif
+ erase();
+ noraw();
+ nl();
+ endwin();
+}
+
+static void
+getch_test(void)
+{
+ int delay = begin_getch_test();
+ wgetch_test(0, stdscr, delay);
+ forget_boxes();
+ 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_RESIZE
+static void
+resize_wide_boxes(unsigned level, WINDOW *win)
+{
+ unsigned n;
+ int base = 5;
+ int high = LINES - base;
+ int wide = COLS;
+
+ touchwin(stdscr);
+ wnoutrefresh(stdscr);
+
+#if USE_SOFTKEYS
+ /* FIXME: this chunk should be done in resizeterm() */
+ slk_touch();
+ slk_clear();
+ slk_noutrefresh();
+#endif
+
+ for (n = 0; 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) {
+ unsigned have = need;
+ result = (char *) calloc(have + 1, 1);
+ tmp = src;
+ if (wcsrtombs(result, &tmp, have, &state) != have) {
+ free(result);
+ result = 0;
+ }
+ }
+ return result;
+}
+
+static void
+wget_wch_test(unsigned level, WINDOW *win, int delay)
+{
+ wchar_t wchar_buf[BUFSIZ];
+ wint_t wint_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 (isQuit((int) c)) {
+ 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();
+ code = wgetn_wstr(win, wint_buf, sizeof(wint_buf) - 1);
+ noecho();
+ if (code == ERR) {
+ wprintw(win, "wgetn_wstr returns an error.");
+ } else {
+ int n;
+ for (n = 0; (wchar_buf[n] = wint_buf[n]) != 0; ++n) ;
+ if ((temp = wcstos(wchar_buf)) != 0) {
+ wprintw(win, "I saw %d characters:\n\t`%s'.",
+ wcslen(wchar_buf), temp);
+ free(temp);
+ } else {
+ wprintw(win, "I saw %d characters (cannot convert).",
+ wcslen(wchar_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((wchar_t) 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 void
+get_wch_test(void)
+{
+ int delay = begin_getch_test();
+ wget_wch_test(0, stdscr, delay);
+ forget_boxes();
+ finish_getch_test();
+}
+#endif
+
+/****************************************************************************
+ *
+ * Character attributes test
+ *
+ ****************************************************************************/
+
+#if HAVE_SETUPTERM || HAVE_TGETENT
+#define get_ncv() TIGETNUM("ncv","NC")
+#define get_xmc() TIGETNUM("xmc","sg")
+#else
+#define get_ncv() -1
+#define get_xmc() -1
+#endif
+
+#if !HAVE_TERMATTRS
+static chtype
+my_termattrs(void)
+{
+ static int first = TRUE;
+ static chtype result = 0;
+
+ if (first) {
+#if !HAVE_TIGETSTR
+ char buffer[4096];
+ char parsed[4096];
+ char *area_pointer = parsed;
+
+ tgetent(buffer, getenv("TERM"));
+#endif
+
+ if (TIGETSTR("smso", "so"))
+ result |= A_STANDOUT;
+ if (TIGETSTR("smul", "us"))
+ result |= A_UNDERLINE;
+ if (TIGETSTR("rev", "mr"))
+ result |= A_REVERSE;
+ if (TIGETSTR("blink", "mb"))
+ result |= A_BLINK;
+ if (TIGETSTR("dim", "mh"))
+ result |= A_DIM;
+ if (TIGETSTR("bold", "md"))
+ result |= A_BOLD;
+ if (TIGETSTR("smacs", "ac"))
+ result |= A_ALTCHARSET;
+
+ first = FALSE;
+ }
+ return result;
+}
+#define termattrs() my_termattrs()
+#endif
+
+#define MAX_ATTRSTRING 31
+#define LEN_ATTRSTRING 26
+
+static char attr_test_string[MAX_ATTRSTRING + 1];
+
+static void
+attr_legend(WINDOW *helpwin)
+{
+ int row = 1;
+ int col = 1;
+
+ mvwprintw(helpwin, row++, col,
+ "ESC to exit.");
+ mvwprintw(helpwin, row++, col,
+ "^L repaints.");
+ ++row;
+ mvwprintw(helpwin, row++, col,
+ "Modify the test strings:");
+ mvwprintw(helpwin, row++, col,
+ " A digit sets gaps on each side of displayed attributes");
+ mvwprintw(helpwin, row++, col,
+ " </> shifts the text left/right. ");
+ ++row;
+ mvwprintw(helpwin, row++, col,
+ "Toggles:");
+ if (use_colors) {
+ mvwprintw(helpwin, row++, col,
+ " f/F/b/F toggle foreground/background background color");
+ mvwprintw(helpwin, row++, col,
+ " t/T toggle text/background color attribute");
+ }
+ mvwprintw(helpwin, row++, col,
+ " a/A toggle ACS (alternate character set) mapping");
+ mvwprintw(helpwin, row++, col,
+ " v/V toggle video attribute to combine with each line");
+}
+
+static void
+show_color_attr(int fg, int bg, int tx)
+{
+ if (use_colors) {
+ printw(" Colors (fg %d, bg %d", fg, bg);
+ if (tx >= 0)
+ printw(", text %d", tx);
+ printw("),");
+ }
+}