+static void
+wgetch_wrap(WINDOW *win, int first_y)
+{
+ int last_y = getmaxy(win) - 1;
+ int y = getcury(win) + 1;
+
+ if (y >= last_y)
+ y = first_y;
+ wmove(win, y, 0);
+ wclrtoeol(win);
+}
+
+#if defined(NCURSES_VERSION) && defined(KEY_RESIZE) && HAVE_WRESIZE
+typedef struct {
+ WINDOW *text;
+ WINDOW *frame;
+} WINSTACK;
+
+static WINSTACK *winstack = 0;
+static unsigned len_winstack = 0;
+
+static void
+forget_boxes(void)
+{
+ if (winstack != 0) {
+ free(winstack);
+ }
+ winstack = 0;
+ len_winstack = 0;
+}
+
+static void
+remember_boxes(unsigned level, WINDOW *txt_win, WINDOW *box_win)
+{
+ unsigned need = (level + 1) * 2;
+
+ if (winstack == 0) {
+ len_winstack = 20;
+ winstack = typeMalloc(WINSTACK, len_winstack);
+ } else if (need >= len_winstack) {
+ len_winstack = need;
+ winstack = typeRealloc(WINSTACK, len_winstack, winstack);
+ }
+ winstack[level].text = txt_win;
+ winstack[level].frame = box_win;
+}
+
+#if USE_SOFTKEYS && (NCURSES_VERSION_PATCH < 20071229) && NCURSES_EXT_FUNCS
+static void
+slk_repaint(void)
+{
+ /* this chunk is now done in resize_term() */
+ slk_touch();
+ slk_clear();
+ slk_noutrefresh();
+}
+
+#else
+#define slk_repaint() /* nothing */
+#endif
+
+/*
+ * For wgetch_test(), we create pairs of windows - one for a box, one for text.
+ * Resize both and paint the box in the parent.
+ */
+static void
+resize_boxes(unsigned level, WINDOW *win)
+{
+ unsigned n;
+ int base = 5;
+ int high = LINES - base;
+ int wide = COLS;
+
+ touchwin(stdscr);
+ wnoutrefresh(stdscr);
+
+ slk_repaint();
+
+ 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(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();
+}
+#else
+#define forget_boxes() /* nothing */
+#define remember_boxes(level,text,frame) /* nothing */
+#endif
+
+static void
+wgetch_test(unsigned 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);
+
+ 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 (isQuit(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();
+ 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) {
+ int y, x;
+ 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) {
+ 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();
+
+ slk_restore();
+ wgetch_test(0, stdscr, delay);
+ forget_boxes();
+ finish_getch_test();
+}
+
+#if USE_WIDEC_SUPPORT
+/*
+ * For wget_wch_test(), we create pairs of windows - one for a box, one for text.
+ * Resize both and paint the box in the parent.
+ */
+#if defined(KEY_RESIZE) && HAVE_WRESIZE
+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);
+
+ slk_repaint();
+
+ 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 = typeCalloc(char, have + 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'.",
+ (int) wcslen(wchar_buf), temp);
+ free(temp);
+ } else {
+ wprintw(win, "I saw %d characters (cannot convert).",
+ (int) 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) {
+#if defined(KEY_RESIZE) && HAVE_WRESIZE
+ 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();
+
+ slk_restore();
+ 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("),");
+ }
+}
+
+static bool
+cycle_color_attr(int ch, short *fg, short *bg, short *tx)
+{
+ bool error = FALSE;
+
+ if (use_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;
+ case 't':
+ *tx = (*tx + 1);
+ break;
+ case 'T':
+ *tx = (*tx - 1);
+ break;
+ default:
+ beep();
+ error = TRUE;
+ break;
+ }
+ if (*fg >= COLORS)
+ *fg = min_colors;
+ if (*fg < min_colors)
+ *fg = COLORS - 1;
+ if (*bg >= COLORS)
+ *bg = min_colors;
+ if (*bg < min_colors)
+ *bg = COLORS - 1;
+ if (*tx >= COLORS)
+ *tx = -1;
+ if (*tx < -1)
+ *tx = COLORS - 1;
+ } else {
+ beep();
+ error = TRUE;
+ }
+ return error;
+}
+
+static void
+adjust_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; j < MAX_ATTRSTRING && 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 void
+init_attr_string(void)
+{
+ attr_test_string[0] = 'a';
+ adjust_attr_string(0);
+}
+
+static int
+show_attr(int row, int skip, bool arrow, chtype attr, const char *name)
+{
+ int ncv = get_ncv();
+ chtype test = attr & (chtype) (~A_ALTCHARSET);
+
+ if (arrow)
+ mvprintw(row, 5, "-->");
+ mvprintw(row, 8, "%s mode:", name);
+ mvprintw(row, 24, "|");
+ if (skip)
+ printw("%*s", skip, " ");
+ /*
+ * Just for testing, write text using the alternate character set one
+ * character at a time (to pass its rendition directly), and use the
+ * string operation for the other attributes.
+ */
+ if (attr & A_ALTCHARSET) {
+ const char *s;
+ chtype ch;
+
+ for (s = attr_test_string; *s != '\0'; ++s) {
+ ch = UChar(*s);
+ addch(ch | attr);
+ }
+ } else {
+ attrset(attr);
+ 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,
+#ifdef A_INVIS
+ A_INVIS,
+#endif
+ 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)");
+ }
+ if ((termattrs() & test) != test)
+ printw(" (Part)");
+ }
+ }
+ return row + 2;
+}
+/* *INDENT-OFF* */
+static const struct {
+ chtype attr;
+ NCURSES_CONST char * name;
+} attrs_to_test[] = {
+ { A_STANDOUT, "STANDOUT" },
+ { A_REVERSE, "REVERSE" },
+ { A_BOLD, "BOLD" },
+ { A_UNDERLINE, "UNDERLINE" },
+ { A_DIM, "DIM" },
+ { A_BLINK, "BLINK" },
+ { A_PROTECT, "PROTECT" },
+#ifdef A_INVIS
+ { A_INVIS, "INVISIBLE" },
+#endif
+ { A_NORMAL, "NORMAL" },
+};
+/* *INDENT-ON* */
+
+static bool
+attr_getc(int *skip, short *fg, short *bg, short *tx, int *ac, unsigned *kc)
+{
+ bool result = TRUE;
+ bool error = FALSE;
+ WINDOW *helpwin;
+
+ do {
+ int ch = Getchar();
+
+ error = FALSE;
+ if (ch < 256 && isdigit(ch)) {
+ *skip = (ch - '0');
+ } else {
+ switch (ch) {
+ case CTRL('L'):
+ Repaint();
+ break;
+ case '?':
+ if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
+ box(helpwin, 0, 0);
+ attr_legend(helpwin);
+ wGetchar(helpwin);
+ delwin(helpwin);
+ }
+ break;
+ case 'a':
+ *ac = 0;
+ break;
+ case 'A':
+ *ac = A_ALTCHARSET;
+ break;
+ case 'v':
+ if (*kc == 0)
+ *kc = SIZEOF(attrs_to_test) - 1;
+ else
+ *kc -= 1;
+ break;
+ case 'V':
+ *kc += 1;
+ if (*kc >= SIZEOF(attrs_to_test))
+ *kc = 0;
+ break;
+ case '<':
+ adjust_attr_string(-1);
+ break;
+ case '>':
+ adjust_attr_string(1);
+ break;
+ case case_QUIT:
+ result = FALSE;
+ break;
+ default:
+ error = cycle_color_attr(ch, fg, bg, tx);
+ break;
+ }
+ }
+ } while (error);
+ return result;
+}
+
+static void
+attr_test(void)
+/* test text attributes */
+{
+ int n;
+ int skip = get_xmc();
+ short fg = COLOR_BLACK; /* color pair 0 is special */
+ short bg = COLOR_BLACK;
+ short tx = -1;
+ int ac = 0;
+ unsigned j, k;
+
+ if (skip < 0)
+ skip = 0;
+
+ n = skip; /* make it easy */
+ k = SIZEOF(attrs_to_test) - 1;
+ init_attr_string();
+
+ do {
+ int row = 2;
+ chtype normal = A_NORMAL | BLANK;
+ chtype extras = ac;
+
+ if (use_colors) {
+ short pair = (fg != COLOR_BLACK || bg != COLOR_BLACK);
+ if (pair != 0) {
+ pair = 1;
+ if (init_pair(pair, fg, bg) == ERR) {
+ beep();
+ } else {
+ normal |= COLOR_PAIR(pair);
+ }
+ }
+ if (tx >= 0) {
+ pair = 2;
+ if (init_pair(pair, tx, bg) == ERR) {
+ beep();
+ } else {
+ extras |= COLOR_PAIR(pair);
+ }
+ }
+ }
+ bkgd(normal);
+ bkgdset(normal);
+ erase();
+
+ box(stdscr, 0, 0);
+ mvaddstr(0, 20, "Character attribute test display");
+
+ for (j = 0; j < SIZEOF(attrs_to_test); ++j) {
+ bool arrow = (j == k);
+ row = show_attr(row, n, arrow,
+ extras |
+ attrs_to_test[j].attr |
+ attrs_to_test[k].attr,
+ attrs_to_test[j].name);
+ }
+
+ mvprintw(row, 8,
+ "This terminal does %shave the magic-cookie glitch",
+ get_xmc() > -1 ? "" : "not ");
+ mvprintw(row + 1, 8, "Enter '?' for help.");
+ show_color_attr(fg, bg, tx);
+ printw(" ACS (%d)", ac != 0);
+
+ refresh();
+ } while (attr_getc(&n, &fg, &bg, &tx, &ac, &k));
+
+ bkgdset(A_NORMAL | BLANK);
+ erase();
+ endwin();
+}
+
+#if USE_WIDEC_SUPPORT
+static wchar_t wide_attr_test_string[MAX_ATTRSTRING + 1];
+
+static void
+wide_adjust_attr_string(int adjust)
+{
+ int first = ((int) UChar(wide_attr_test_string[0])) + adjust;
+ int last = first + LEN_ATTRSTRING;
+
+ if (first >= ' ' && last <= '~') { /* 32..126 */
+ int j, k;
+ for (j = 0, k = first; j < MAX_ATTRSTRING && k <= last; ++j, ++k) {
+ wide_attr_test_string[j] = k;
+ if (((k + 1 - first) % 5) == 0) {
+ ++j;
+ if (j < MAX_ATTRSTRING)
+ wide_attr_test_string[j] = ' ';
+ }
+ }
+ while (j < MAX_ATTRSTRING)
+ wide_attr_test_string[j++] = ' ';
+ wide_attr_test_string[j] = '\0';
+ } else {
+ beep();
+ }
+}
+
+static void
+wide_init_attr_string(void)
+{
+ wide_attr_test_string[0] = 'a';
+ wide_adjust_attr_string(0);
+}
+
+static void
+set_wide_background(short pair)
+{
+ cchar_t normal;
+ wchar_t blank[2];
+
+ blank[0] = ' ';
+ blank[1] = 0;
+ setcchar(&normal, blank, A_NORMAL, pair, 0);
+ bkgrnd(&normal);
+ bkgrndset(&normal);
+}
+
+static attr_t
+get_wide_background(void)
+{
+ attr_t result = A_NORMAL;
+ attr_t attr;
+ cchar_t ch;
+ short pair;
+ wchar_t wch[10];
+
+ if (getbkgrnd(&ch) != ERR) {
+ if (getcchar(&ch, wch, &attr, &pair, 0) != ERR) {
+ result = attr;
+ }
+ }
+ return result;
+}
+
+static int
+wide_show_attr(int row, int skip, bool arrow, chtype attr, short pair, const char *name)
+{
+ int ncv = get_ncv();
+ chtype test = attr & ~WA_ALTCHARSET;
+
+ if (arrow)
+ mvprintw(row, 5, "-->");
+ mvprintw(row, 8, "%s mode:", name);
+ mvprintw(row, 24, "|");
+ if (skip)
+ printw("%*s", skip, " ");
+
+ /*
+ * Just for testing, write text using the alternate character set one
+ * character at a time (to pass its rendition directly), and use the
+ * string operation for the other attributes.
+ */
+ if (attr & WA_ALTCHARSET) {
+ const wchar_t *s;
+ cchar_t ch;
+
+ for (s = wide_attr_test_string; *s != L'\0'; ++s) {
+ wchar_t fill[2];
+ fill[0] = *s;
+ fill[1] = L'\0';
+ setcchar(&ch, fill, attr, pair, 0);
+ add_wch(&ch);
+ }
+ } else {
+ attr_t old_attr;
+ short old_pair;
+
+ attr_get(&old_attr, &old_pair, 0);
+ attr_set(attr, pair, 0);
+ addwstr(wide_attr_test_string);
+ attr_set(old_attr, old_pair, 0);
+ }
+ if (skip)
+ printw("%*s", skip, " ");
+ printw("|");
+ if (test != A_NORMAL) {
+ if (!(term_attrs() & test)) {
+ printw(" (N/A)");
+ } else {
+ if (ncv > 0 && (get_wide_background() & A_COLOR)) {
+ static const attr_t table[] =
+ {
+ WA_STANDOUT,
+ WA_UNDERLINE,
+ WA_REVERSE,
+ WA_BLINK,
+ WA_DIM,
+ WA_BOLD,
+ WA_INVIS,
+ WA_PROTECT,
+ WA_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)");
+ }
+ if ((term_attrs() & test) != test)
+ printw(" (Part)");
+ }
+ }
+ return row + 2;
+}
+
+static bool
+wide_attr_getc(int *skip, short *fg, short *bg, short *tx, int *ac, unsigned *kc)
+{
+ bool result = TRUE;
+ bool error = FALSE;
+ WINDOW *helpwin;
+
+ do {
+ int ch = Getchar();
+
+ error = FALSE;
+ if (ch < 256 && isdigit(ch)) {
+ *skip = (ch - '0');
+ } else {
+ switch (ch) {
+ case CTRL('L'):
+ Repaint();
+ break;
+ case '?':
+ if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
+ box_set(helpwin, 0, 0);
+ attr_legend(helpwin);
+ wGetchar(helpwin);
+ delwin(helpwin);
+ }
+ break;
+ case 'a':
+ *ac = 0;
+ break;
+ case 'A':
+ *ac = A_ALTCHARSET;
+ break;
+ case 'v':
+ if (*kc == 0)
+ *kc = SIZEOF(attrs_to_test) - 1;
+ else
+ *kc -= 1;
+ break;
+ case 'V':
+ *kc += 1;
+ if (*kc >= SIZEOF(attrs_to_test))
+ *kc = 0;
+ break;
+ case '<':
+ wide_adjust_attr_string(-1);
+ break;
+ case '>':
+ wide_adjust_attr_string(1);
+ break;
+ case case_QUIT:
+ result = FALSE;
+ break;
+ default:
+ error = cycle_color_attr(ch, fg, bg, tx);
+ break;
+ }
+ }
+ } while (error);
+ return result;
+}
+
+static void
+wide_attr_test(void)
+/* test text attributes using wide-character calls */
+{
+ int n;
+ int skip = get_xmc();
+ short fg = COLOR_BLACK; /* color pair 0 is special */
+ short bg = COLOR_BLACK;
+ short tx = -1;
+ int ac = 0;
+ unsigned j, k;
+
+ if (skip < 0)
+ skip = 0;
+
+ n = skip; /* make it easy */
+ k = SIZEOF(attrs_to_test) - 1;
+ wide_init_attr_string();
+
+ do {
+ int row = 2;
+ short pair = 0;
+ short extras = 0;
+
+ if (use_colors) {
+ pair = (fg != COLOR_BLACK || bg != COLOR_BLACK);
+ if (pair != 0) {
+ pair = 1;
+ if (init_pair(pair, fg, bg) == ERR) {
+ beep();
+ }
+ }
+ extras = pair;
+ if (tx >= 0) {
+ extras = 2;
+ if (init_pair(extras, tx, bg) == ERR) {
+ beep();
+ }
+ }
+ }
+ set_wide_background(pair);
+ erase();
+
+ box_set(stdscr, 0, 0);
+ mvaddstr(0, 20, "Character attribute test display");
+
+ for (j = 0; j < SIZEOF(attrs_to_test); ++j) {
+ row = wide_show_attr(row, n, j == k,
+ ac |
+ attrs_to_test[j].attr |
+ attrs_to_test[k].attr,
+ extras,
+ attrs_to_test[j].name);
+ }
+
+ mvprintw(row, 8,
+ "This terminal does %shave the magic-cookie glitch",
+ get_xmc() > -1 ? "" : "not ");
+ mvprintw(row + 1, 8, "Enter '?' for help.");
+ show_color_attr(fg, bg, tx);
+ printw(" ACS (%d)", ac != 0);
+
+ refresh();
+ } while (wide_attr_getc(&n, &fg, &bg, &tx, &ac, &k));
+
+ set_wide_background(0);
+ erase();
+ endwin();
+}
+#endif
+
+/****************************************************************************
+ *
+ * Color support tests
+ *
+ ****************************************************************************/
+
+static NCURSES_CONST char *the_color_names[] =
+{
+ "black",
+ "red",
+ "green",
+ "yellow",
+ "blue",
+ "magenta",
+ "cyan",
+ "white",
+ "BLACK",
+ "RED",
+ "GREEN",
+ "YELLOW",
+ "BLUE",
+ "MAGENTA",
+ "CYAN",
+ "WHITE"
+};
+
+static void
+show_color_name(int y, int x, int color, bool wide)
+{
+ if (move(y, x) != ERR) {
+ char temp[80];
+ int width = 8;
+
+ if (wide) {
+ sprintf(temp, "%02d", color);
+ width = 4;
+ } else if (color >= 8) {
+ sprintf(temp, "[%02d]", color);
+ } else {
+ strcpy(temp, the_color_names[color]);
+ }
+ printw("%-*.*s", width, width, temp);
+ }
+}
+
+static void
+color_legend(WINDOW *helpwin, bool wide)
+{
+ int row = 1;
+ int col = 1;
+
+ mvwprintw(helpwin, row++, col,
+ "ESC to exit.");
+ ++row;
+ mvwprintw(helpwin, row++, col,
+ "Use up/down arrow to scroll through the display if it is");
+ mvwprintw(helpwin, row++, col,
+ "longer than one screen. Control/N and Control/P can be used");
+ mvwprintw(helpwin, row++, col,
+ "in place of up/down arrow. Use pageup/pagedown to scroll a");
+ mvwprintw(helpwin, row++, col,
+ "full screen; control/B and control/F can be used here.");
+ ++row;
+ mvwprintw(helpwin, row++, col,
+ "Toggles:");
+ mvwprintw(helpwin, row++, col,
+ " a/A toggle altcharset off/on");
+ mvwprintw(helpwin, row++, col,
+ " b/B toggle bold off/on");
+ mvwprintw(helpwin, row++, col,
+ " n/N toggle text/number on/off");
+ mvwprintw(helpwin, row++, col,
+ " w/W toggle width between 8/16 colors");
+#if USE_WIDEC_SUPPORT
+ if (wide) {
+ mvwprintw(helpwin, row++, col,
+ "Wide characters:");
+ mvwprintw(helpwin, row++, col,
+ " x/X toggle text between ASCII and wide-character");
+ }
+#else
+ (void) wide;
+#endif
+}
+
+#define set_color_test(name, value) if (name != value) { name = value; base_row = 0; }
+
+/* generate a color test pattern */
+static void
+color_test(void)
+{
+ short i;
+ int top = 0, width;
+ int base_row = 0;
+ int grid_top = top + 3;
+ int page_size = (LINES - grid_top);
+ int pairs_max = PAIR_NUMBER(A_COLOR) + 1;
+ int row_limit;
+ int per_row;
+ char numbered[80];
+ const char *hello;
+ bool done = FALSE;
+ bool opt_acsc = FALSE;
+ bool opt_bold = FALSE;
+ bool opt_wide = FALSE;
+ bool opt_nums = FALSE;
+ WINDOW *helpwin;
+
+ if (pairs_max > COLOR_PAIRS)
+ pairs_max = COLOR_PAIRS;
+
+ while (!done) {
+ int shown = 0;
+
+ /* this assumes an 80-column line */
+ if (opt_wide) {
+ width = 4;
+ hello = "Test";
+ per_row = (COLORS > 8) ? 16 : 8;
+ } else {
+ width = 8;
+ hello = "Hello";
+ per_row = 8;
+ }
+
+ row_limit = (pairs_max + per_row - 1) / per_row;
+
+ move(0, 0);
+ (void) printw("There are %d color pairs and %d colors\n",
+ pairs_max, COLORS);