+ wrefresh(stdscr);
+ wmove(win, y, x);
+}
+
+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(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;
+
+ assert(level < (unsigned) COLS);
+
+ 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);
+ }
+ if (!winstack)
+ failed("remember_boxes");
+ winstack[level].text = txt_win;
+ winstack[level].frame = box_win;
+}
+
+#if USE_SOFTKEYS && (defined(NCURSES_VERSION_PATCH) && 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
+
+#if defined(NCURSES_VERSION) && defined(KEY_RESIZE) && HAVE_WRESIZE
+/*
+ * 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();
+}
+#endif /* resize_boxes */
+#else
+#define forget_boxes() /* nothing */
+#define remember_boxes(level,text,frame) /* nothing */
+#endif
+
+/*
+ * Return-code is OK/ERR or a keyname.
+ */
+static const char *
+ok_keyname(int code)
+{
+ return ((code == OK) ? "OK" : ((code == ERR) ? "ERR" : keyname(code)));
+}
+
+static void
+wgetch_test(unsigned level, WINDOW *win, int delay)
+{
+ char buf[BUFSIZ];
+ int first_y, first_x;
+ int c;
+ int incount = 0;
+ GetchFlags flags;
+
+ init_getch(win, flags, delay);
+ notimeout(win, FALSE);
+ 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_getch(flags, delay)) {
+ (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_getch(flags, delay)) {
+ wprintw(win, "ERR");
+ wgetch_wrap(win, first_y);
+ } else if (isQuit(c, ExitOnEscape())) {
+ 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();
+ c = wgetnstr(win, buf, sizeof(buf) - 1);
+ noecho();
+ wprintw(win, "I saw %d characters:\n\t`%s' (%s).",
+ (int) strlen(buf), buf,
+ ok_keyname(c));
+ 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 == 't') {
+ notimeout(win, flags[UChar('t')]);
+ flags[UChar('t')] = !flags[UChar('t')];
+ wgetch_help(win, flags);
+ } 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) {
+ show_mouse(win);
+ } 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 = (unsigned) c;
+#if !(defined(NCURSES_VERSION) || defined(_XOPEN_CURSES))
+ /* at least Solaris SVR4 curses breaks unctrl(128), etc. */
+ c2 &= 0x7f;
+#endif
+ if (isprint(c))
+ (void) wprintw(win, "%c", UChar(c));
+ else if (c2 != UChar(c))
+ (void) wprintw(win, "M-%s", unctrl(c2));
+ else
+ (void) wprintw(win, "%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);
+
+ if (!level)
+ init_getch(win, flags, delay);
+}
+
+static int
+begin_getch_test(void)
+{
+ char buf[BUFSIZ];
+ int delay;
+
+ refresh();
+
+#ifdef NCURSES_MOUSE_VERSION
+ mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, (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(6, 0);
+ return delay;
+}
+
+static void
+finish_getch_test(void)
+{
+#ifdef NCURSES_MOUSE_VERSION
+ mousemask(0, (mmask_t *) 0);
+#endif
+ erase();
+ noraw();
+ nl();
+ endwin();
+}
+
+static int
+getch_test(bool recur GCC_UNUSED)
+{
+ int delay = begin_getch_test();
+
+ slk_restore();
+ wgetch_test(0, stdscr, delay);
+ forget_boxes();
+ finish_getch_test();
+ slk_clear();
+ return OK;
+}
+
+#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;
+ char *result = 0;
+ const wchar_t *tmp = src;
+#ifndef state_unused
+ mbstate_t state;
+#endif
+
+ reset_wchars(state);
+ if ((need = (int) count_wchars(tmp, 0, &state)) > 0) {
+ unsigned have = (unsigned) need;
+ if ((result = typeCalloc(char, have + 1)) != 0) {
+ tmp = src;
+ if (trans_wchars(result, tmp, have, &state) != have) {
+ free(result);
+ result = 0;
+ }
+ } else {
+ failed("wcstos");
+ }
+ }
+ 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;
+ GetchFlags flags;
+ int code;
+ char *temp;
+
+ init_getch(win, flags, delay);
+ notimeout(win, FALSE);
+ 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_getch(flags, delay)) {
+ (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_getch(flags, delay)) {
+ wprintw(win, "ERR");
+ wgetch_wrap(win, first_y);
+ } else if (isQuit((int) c, ExitOnEscape())) {
+ 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, BUFSIZ - 1);
+ noecho();
+ if (code == ERR) {
+ wprintw(win, "wgetn_wstr returns an error.");
+ } else {
+ int n;
+ for (n = 0; (wchar_buf[n] = (wchar_t) 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 == 't') {
+ notimeout(win, flags[UChar('t')]);
+ flags[UChar('t')] = !flags[UChar('t')];
+ wgetch_help(win, flags);
+ } 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 ", (int) c);
+#ifdef NCURSES_MOUSE_VERSION
+ if (c == KEY_MOUSE) {
+ show_mouse(win);
+ } 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, keyname((wchar_t) c));
+ } else {
+ (void) waddstr(win, key_name((wchar_t) c));
+ if (c < 256 && iscntrl(c)) {
+ (void) wprintw(win, " (control character)");
+ } else {
+ (void) wprintw(win, " = %#x (printable character)",
+ (unsigned) c);
+ }
+ }
+ wgetch_wrap(win, first_y);
+ }
+ }
+
+ wtimeout(win, -1);
+
+ if (!level)
+ init_getch(win, flags, delay);
+}
+
+static int
+x_getch_test(bool recur GCC_UNUSED)
+{
+ int delay = begin_getch_test();
+
+ slk_restore();
+ wget_wch_test(0, stdscr, delay);
+ forget_boxes();
+ finish_getch_test();
+ slk_clear();
+ return OK;
+}
+#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 ATTRSTRING_1ST 32 /* ' ' */
+#define ATTRSTRING_END 126 /* '~' */
+
+#define COLS_PRE_ATTRS 5
+#define COLS_AFT_ATTRS 15
+#define COL_ATTRSTRING (COLS_PRE_ATTRS + 17)
+#define LEN_ATTRSTRING (COLS - (COL_ATTRSTRING + COLS_AFT_ATTRS))
+#define MAX_ATTRSTRING (ATTRSTRING_END + 1 - ATTRSTRING_1ST)
+
+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 (UseColors) {
+ MvWPrintw(helpwin, row++, col,
+ " f/F/b/B 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");
+#if USE_WIDEC_SUPPORT
+ MvWPrintw(helpwin, row, col,
+ " w/W toggle normal/wide (double-width) test-characters");
+#endif
+}
+
+static void
+show_color_attr(int fg, int bg, int tx)
+{
+ if (UseColors) {
+ printw(" Colors (fg %d, bg %d", fg, bg);
+ if (tx >= 0)
+ printw(", text %d", tx);
+ printw("),");
+ }
+}
+
+static bool
+cycle_color_attr(int ch, NCURSES_COLOR_T *fg, NCURSES_COLOR_T *bg, NCURSES_COLOR_T *tx)
+{
+ bool error = FALSE;
+
+ if (UseColors) {
+ switch (ch) {
+ case 'f':
+ *fg = (NCURSES_COLOR_T) (*fg + 1);
+ break;
+ case 'F':
+ *fg = (NCURSES_COLOR_T) (*fg - 1);
+ break;
+ case 'b':
+ *bg = (NCURSES_COLOR_T) (*bg + 1);
+ break;
+ case 'B':
+ *bg = (NCURSES_COLOR_T) (*bg - 1);
+ break;
+ case 't':
+ *tx = (NCURSES_COLOR_T) (*tx + 1);
+ break;
+ case 'T':
+ *tx = (NCURSES_COLOR_T) (*tx - 1);
+ break;
+ default:
+ beep();
+ error = TRUE;
+ break;
+ }
+ if (*fg >= COLORS)
+ *fg = (NCURSES_COLOR_T) MinColors;
+ if (*fg < MinColors)
+ *fg = (NCURSES_COLOR_T) (COLORS - 1);
+ if (*bg >= COLORS)
+ *bg = (NCURSES_COLOR_T) MinColors;
+ if (*bg < MinColors)
+ *bg = (NCURSES_COLOR_T) (COLORS - 1);
+ if (*tx >= COLORS)
+ *tx = -1;
+ if (*tx < -1)
+ *tx = (NCURSES_COLOR_T) (COLORS - 1);
+ } else {
+ beep();
+ error = TRUE;
+ }
+ return error;
+}
+
+static void
+adjust_attr_string(int adjust)
+{
+ char save = attr_test_string[0];
+ int first = ((int) UChar(save)) + adjust;
+ int j, k;
+
+ if (first >= ATTRSTRING_1ST) {
+ for (j = 0, k = first; j < MAX_ATTRSTRING; ++j, ++k) {
+ if (k > ATTRSTRING_END)
+ break;
+ attr_test_string[j] = (char) k;
+ if (((k + 1 - first) % 5) == 0) {
+ if (++j >= MAX_ATTRSTRING)
+ break;
+ attr_test_string[j] = ' ';
+ }
+ }
+ if ((LEN_ATTRSTRING - j) > 5) {
+ attr_test_string[0] = save;
+ adjust_attr_string(adjust - 1);
+ } else {
+ while (j < MAX_ATTRSTRING)
+ attr_test_string[j++] = ' ';
+ attr_test_string[j] = '\0';
+ }
+ }
+}
+
+/*
+ * Prefer the right-end of the string for starting, since that maps to the
+ * VT100 line-drawing.
+ */
+static int
+default_attr_string(void)
+{
+ int result = (ATTRSTRING_END - LEN_ATTRSTRING);
+ result += (LEN_ATTRSTRING / 5);
+ if (result < ATTRSTRING_1ST)
+ result = ATTRSTRING_1ST;
+ return result;
+}
+
+static void
+init_attr_string(void)
+{
+ attr_test_string[0] = (char) default_attr_string();
+ adjust_attr_string(0);
+}
+
+static int
+show_attr(WINDOW *win, int row, int skip, bool arrow, chtype attr, const char *name)
+{
+ int ncv = get_ncv();
+ chtype test = attr & (chtype) (~(A_ALTCHARSET | A_CHARTEXT));
+
+ if (arrow)
+ MvPrintw(row, COLS_PRE_ATTRS - 3, "-->");
+ MvPrintw(row, COLS_PRE_ATTRS, "%s mode:", name);
+ MvPrintw(row, COL_ATTRSTRING - 1, "|");
+ 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.
+ */
+ wmove(win, 0, 0);
+ werase(win);
+ if (attr & A_ALTCHARSET) {
+ const char *s;
+ chtype ch;
+
+ for (s = attr_test_string; *s != '\0'; ++s) {
+ ch = UChar(*s);
+ (void) waddch(win, ch | attr);
+ }
+ } else {
+ (void) wattrset(win, AttrArg(attr, 0));
+ (void) waddstr(win, attr_test_string);
+ (void) wattroff(win, (int) attr);
+ }
+ if (skip)
+ printw("%*s", skip, " ");
+ MvPrintw(row, COL_ATTRSTRING + LEN_ATTRSTRING, "|");
+ if (test != A_NORMAL) {
+ if (!(termattrs() & test)) {
+ printw(" (N/A)");
+ } else {
+ if (ncv > 0 && stdscr && (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
+#ifdef A_ITALIC
+ A_ITALIC,
+#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;
+}
+
+typedef struct {
+ chtype attr;
+ NCURSES_CONST char *name;
+} ATTR_TBL;
+/* *INDENT-OFF* */
+static const ATTR_TBL 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
+#ifdef A_ITALIC
+ { A_ITALIC, "ITALIC" },
+#endif
+ { A_NORMAL, "NORMAL" },
+};
+/* *INDENT-ON* */
+
+static unsigned
+init_attr_list(ATTR_TBL * target, attr_t attrs)
+{
+ unsigned result = 0;
+ size_t n;
+
+ for (n = 0; n < SIZEOF(attrs_to_test); ++n) {
+ attr_t test = attrs_to_test[n].attr;
+ if (test == A_NORMAL || (test & attrs) != 0) {
+ target[result++] = attrs_to_test[n];
+ }
+ }
+ return result;
+}
+
+#if USE_WIDEC_SUPPORT
+typedef struct {
+ attr_t attr;
+ NCURSES_CONST char *name;
+} W_ATTR_TBL;
+/* *INDENT-OFF* */
+static const W_ATTR_TBL w_attrs_to_test[] = {
+ { WA_STANDOUT, "STANDOUT" },
+ { WA_REVERSE, "REVERSE" },
+ { WA_BOLD, "BOLD" },
+ { WA_UNDERLINE, "UNDERLINE" },
+ { WA_DIM, "DIM" },
+ { WA_BLINK, "BLINK" },
+ { WA_PROTECT, "PROTECT" },
+#ifdef WA_INVIS
+ { WA_INVIS, "INVISIBLE" },
+#endif
+#ifdef WA_ITALIC
+ { WA_ITALIC, "ITALIC" },
+#endif
+ { WA_NORMAL, "NORMAL" },
+};
+/* *INDENT-ON* */
+
+static unsigned
+init_w_attr_list(W_ATTR_TBL * target, attr_t attrs)
+{
+ unsigned result = 0;
+ size_t n;
+
+ for (n = 0; n < SIZEOF(w_attrs_to_test); ++n) {
+ attr_t test = w_attrs_to_test[n].attr;
+ if (test == WA_NORMAL || (test & attrs) != 0) {
+ target[result++] = w_attrs_to_test[n];
+ }
+ }
+ return result;
+}
+#endif
+
+static bool
+attr_getc(int *skip,
+ NCURSES_COLOR_T *fg,
+ NCURSES_COLOR_T *bg,
+ NCURSES_COLOR_T *tx,
+ int *ac,
+ unsigned *kc,
+ unsigned limit)
+{
+ 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 HELP_KEY_1:
+ 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 = limit - 1;
+ else
+ *kc -= 1;
+ break;
+ case 'V':
+ *kc += 1;
+ if (*kc >= limit)
+ *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 int
+attr_test(bool recur GCC_UNUSED)
+/* test text attributes */
+{
+ int n;
+ int skip = get_xmc();
+ NCURSES_COLOR_T fg = COLOR_BLACK; /* color pair 0 is special */
+ NCURSES_COLOR_T bg = COLOR_BLACK;
+ NCURSES_COLOR_T tx = -1;
+ int ac = 0;
+ unsigned j, k;
+ WINDOW *my_wins[SIZEOF(attrs_to_test)];
+ ATTR_TBL my_list[SIZEOF(attrs_to_test)];
+ unsigned my_size = init_attr_list(my_list, termattrs());
+
+ if (my_size > 1) {
+ for (j = 0; j < my_size; ++j) {
+ my_wins[j] = subwin(stdscr,
+ 1, LEN_ATTRSTRING,
+ 2 + (int) (2 * j), COL_ATTRSTRING);
+ scrollok(my_wins[j], FALSE);
+ }
+
+ if (skip < 0)
+ skip = 0;
+
+ n = skip; /* make it easy */
+ k = my_size - 1;
+ init_attr_string();
+
+ do {
+ int row = 2;
+ chtype normal = A_NORMAL | BLANK;
+ chtype extras = (chtype) ac;
+
+ if (UseColors) {
+ NCURSES_PAIRS_T pair = 0;
+ if ((fg != COLOR_BLACK) || (bg != COLOR_BLACK)) {
+ pair = 1;
+ if (init_pair(pair, fg, bg) == ERR) {
+ beep();
+ } else {
+ normal |= (chtype) COLOR_PAIR(pair);
+ }
+ }
+ if (tx >= 0) {
+ pair = 2;
+ if (init_pair(pair, tx, bg) == ERR) {
+ beep();
+ } else {
+ extras |= (chtype) COLOR_PAIR(pair);
+ normal &= ~A_COLOR;
+ }
+ }
+ }
+ bkgd(normal);
+ bkgdset(normal);
+ erase();
+
+ box(stdscr, 0, 0);
+ MvAddStr(0, 20, "Character attribute test display");
+
+ for (j = 0; j < my_size; ++j) {
+ bool arrow = (j == k);
+ row = show_attr(my_wins[j], row, n, arrow,
+ normal |
+ extras |
+ my_list[j].attr |
+ my_list[k].attr,
+ my_list[j].name);
+ }
+
+ MvPrintw(row, COLS_PRE_ATTRS,
+ "This terminal does %shave the magic-cookie glitch",
+ get_xmc() > -1 ? "" : "not ");
+ MvPrintw(row + 1, COLS_PRE_ATTRS, "Enter '?' for help.");
+ show_color_attr(fg, bg, tx);
+ printw(" ACS (%d)", ac != 0);
+
+ refresh();
+ } while (attr_getc(&n, &fg, &bg, &tx, &ac, &k, my_size));
+
+ bkgdset(A_NORMAL | BLANK);
+ erase();
+ endwin();
+ return OK;
+ } else {
+ Cannot("does not support video attributes.");
+ return ERR;
+ }
+}
+
+#if USE_WIDEC_SUPPORT
+static bool use_fullwidth;
+static wchar_t wide_attr_test_string[MAX_ATTRSTRING + 1];
+
+#define FULL_LO 0xff00
+#define FULL_HI 0xff5e
+#define HALF_LO 0x20
+
+#define isFullWidth(ch) ((int)(ch) >= FULL_LO && (int)(ch) <= FULL_HI)
+#define ToNormalWidth(ch) (wchar_t) (((int)(ch) - FULL_LO) + HALF_LO)
+#define ToFullWidth(ch) (wchar_t) (((int)(ch) - HALF_LO) + FULL_LO)
+
+/*
+ * Returns an ASCII code in [32..126]
+ */
+static wchar_t
+normal_wchar(int ch)
+{
+ wchar_t result = (wchar_t) ch;
+ if (isFullWidth(ch))
+ result = ToNormalWidth(ch);
+ return result;
+}
+
+/*
+ * Returns either an ASCII code in in [32..126] or full-width in
+ * [0xff00..0xff5e], according to use_fullwidth setting.
+ */
+static wchar_t
+target_wchar(int ch)
+{
+ wchar_t result = (wchar_t) ch;
+ if (use_fullwidth) {
+ if (!isFullWidth(ch))
+ result = ToFullWidth(ch);
+ } else {
+ if (isFullWidth(ch))
+ result = ToNormalWidth(ch);
+ }
+ return result;
+}
+
+static void
+wide_adjust_attr_string(int adjust)
+{
+ wchar_t save = wide_attr_test_string[0];
+ int first = ((int) normal_wchar(save)) + adjust;
+ int j, k;
+
+ if (first >= ATTRSTRING_1ST) {
+ for (j = 0, k = first; j < MAX_ATTRSTRING; ++j, ++k) {
+ if (k > ATTRSTRING_END)
+ break;
+ wide_attr_test_string[j] = target_wchar(k);
+ if (((k + 1 - first) % 5) == 0) {
+ if (++j >= MAX_ATTRSTRING)
+ break;
+ wide_attr_test_string[j] = ' ';
+ }
+ }
+ if ((LEN_ATTRSTRING - j) > 5) {
+ wide_attr_test_string[0] = save;
+ wide_adjust_attr_string(adjust - 1);
+ } else {
+ while (j < MAX_ATTRSTRING)
+ wide_attr_test_string[j++] = ' ';
+ wide_attr_test_string[j] = '\0';
+ }
+ }
+}
+
+static void
+wide_init_attr_string(void)
+{
+ use_fullwidth = FALSE;
+ wide_attr_test_string[0] = (wchar_t) default_attr_string();
+ wide_adjust_attr_string(0);
+}
+
+static void
+set_wide_background(NCURSES_PAIRS_T 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 = WA_NORMAL;
+ attr_t attr;
+ cchar_t ch;
+ NCURSES_PAIRS_T pair;
+ wchar_t wch[10];
+
+ memset(&ch, 0, sizeof(ch));
+ if (getbkgrnd(&ch) != ERR) {
+ if (getcchar(&ch, wch, &attr, &pair, 0) != ERR) {
+ result = attr;
+ }
+ }
+ return result;
+}
+
+static int
+wide_show_attr(WINDOW *win,
+ int row,
+ int skip,
+ bool arrow,
+ attr_t attr,
+ NCURSES_PAIRS_T pair,
+ const char *name)
+{
+ int ncv = get_ncv();
+ attr_t test = attr & ~WA_ALTCHARSET;
+
+ if (arrow)
+ MvPrintw(row, COLS_PRE_ATTRS - 3, "-->");
+ MvPrintw(row, COLS_PRE_ATTRS, "%s mode:", name);
+ MvPrintw(row, COL_ATTRSTRING - 1, "|");
+ 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.
+ */
+ wmove(win, 0, 0);
+ werase(win);
+ 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);
+ (void) wadd_wch(win, &ch);
+ }
+ } else {
+ attr_t old_attr = 0;
+ NCURSES_PAIRS_T old_pair = 0;
+
+ (void) (wattr_get) (win, &old_attr, &old_pair, 0);
+ (void) wattr_set(win, attr, pair, 0);
+ (void) waddwstr(win, wide_attr_test_string);
+ (void) wattr_set(win, old_attr, old_pair, 0);
+ }
+ if (skip)
+ printw("%*s", skip, " ");
+ MvPrintw(row, COL_ATTRSTRING + LEN_ATTRSTRING, "|");
+ 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,
+ NCURSES_COLOR_T *fg, NCURSES_COLOR_T *bg,
+ NCURSES_COLOR_T *tx, int *ac,
+ unsigned *kc, unsigned limit)
+{
+ 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 HELP_KEY_1:
+ 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 = limit - 1;
+ else
+ *kc -= 1;
+ break;
+ case 'V':
+ *kc += 1;
+ if (*kc >= limit)
+ *kc = 0;
+ break;
+ case 'w':
+ use_fullwidth = FALSE;
+ wide_adjust_attr_string(0);
+ break;
+ case 'W':
+ use_fullwidth = TRUE;
+ wide_adjust_attr_string(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 int
+x_attr_test(bool recur GCC_UNUSED)
+/* test text attributes using wide-character calls */
+{
+ int n;
+ int skip = get_xmc();
+ NCURSES_COLOR_T fg = COLOR_BLACK; /* color pair 0 is special */
+ NCURSES_COLOR_T bg = COLOR_BLACK;
+ NCURSES_COLOR_T tx = -1;
+ int ac = 0;
+ unsigned j, k;
+ W_ATTR_TBL my_list[SIZEOF(w_attrs_to_test)];
+ WINDOW *my_wins[SIZEOF(w_attrs_to_test)];
+ unsigned my_size = init_w_attr_list(my_list, term_attrs());
+
+ if (my_size > 1) {
+ for (j = 0; j < my_size; ++j) {
+ my_wins[j] = subwin(stdscr,
+ 1, LEN_ATTRSTRING,
+ 2 + (int) (2 * j), COL_ATTRSTRING);
+ scrollok(my_wins[j], FALSE);
+ }
+
+ if (skip < 0)
+ skip = 0;
+
+ n = skip; /* make it easy */
+ k = my_size - 1;
+ wide_init_attr_string();
+
+ do {
+ int row = 2;
+ NCURSES_PAIRS_T pair = 0;
+ NCURSES_PAIRS_T extras = 0;
+
+ if (UseColors) {
+ pair = (NCURSES_PAIRS_T) (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 < my_size; ++j) {
+ row = wide_show_attr(my_wins[j], row, n, (j == k),
+ ((attr_t) ac |
+ my_list[j].attr |
+ my_list[k].attr),
+ extras,
+ my_list[j].name);
+ }
+
+ MvPrintw(row, COLS_PRE_ATTRS,
+ "This terminal does %shave the magic-cookie glitch",
+ get_xmc() > -1 ? "" : "not ");
+ MvPrintw(row + 1, COLS_PRE_ATTRS, "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, my_size));
+
+ set_wide_background(0);
+ erase();
+ endwin();
+ return OK;
+ } else {
+ Cannot("does not support extended video attributes.");
+ return ERR;
+ }
+}
+#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) {
+ _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
+ "%02d", color);
+ width = 4;
+ } else if (color >= 8) {
+ _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
+ "[%02d]", color);
+ } else if (color < 0) {
+ _nc_STRCPY(temp, "default", sizeof(temp));
+ } else {
+ _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
+ "%.*s", 16, 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");
+ if (has_colors()) {
+ MvWPrintw(helpwin, row++, col,
+ " c/C cycle used-colors through 8,16,...,COLORS");
+ }
+ MvWPrintw(helpwin, row++, col,
+ " n/N toggle text/number on/off");
+ MvWPrintw(helpwin, row++, col,
+ " r/R toggle reverse 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; }
+
+static int
+color_cycle(int current, int step)
+{
+ int result = current;
+ if (step < 0) {
+ if (current <= 8) {
+ result = COLORS;
+ } else {
+ result = 8;
+ if ((result * 2) > COLORS) {
+ result = COLORS;
+ } else {
+ while ((result * 2) < current) {
+ result *= 2;
+ }
+ }
+ }
+ } else {
+ if (current >= COLORS) {
+ result = 8;
+ } else {
+ result *= 2;
+ }
+ if (result > COLORS)
+ result = COLORS;
+ }
+ return result;
+}
+
+/* generate a color test pattern */
+static int
+color_test(bool recur GCC_UNUSED)
+{
+ NCURSES_PAIRS_T i;
+ int top = 0, width;
+ int base_row = 0;
+ int grid_top = top + 3;
+ int page_size = (LINES - grid_top);
+ int pairs_max;
+ int colors_max = COLORS;
+ int col_limit;
+ int row_limit;
+ int per_row;
+ char *numbered = 0;
+ const char *hello;
+ bool done = FALSE;
+ bool opt_acsc = FALSE;
+ bool opt_bold = FALSE;
+ bool opt_revs = FALSE;
+ bool opt_nums = FALSE;
+ bool opt_wide = FALSE;
+ WINDOW *helpwin;
+
+ if (!UseColors) {
+ Cannot("does not support color.");
+ return ERR;
+ }
+
+ numbered = (char *) calloc((size_t) (COLS + 1), sizeof(char));
+ done = ((COLS < 16) || (numbered == 0));
+
+ /*
+ * Because the number of colors is usually a power of two, we also use
+ * a power of two for the number of colors shown per line (to be tidy).
+ */
+ for (col_limit = 1; col_limit * 2 < COLS; col_limit *= 2) ;
+
+ while (!done) {
+ int shown = 0;
+
+ pairs_max = PAIR_NUMBER(A_COLOR) + 1;
+ if (colors_max * colors_max <= COLOR_PAIRS) {
+ int limit = (colors_max - MinColors) * (colors_max - MinColors);
+ if (pairs_max > limit)
+ pairs_max = limit;
+ } else {
+ if (pairs_max > COLOR_PAIRS)
+ pairs_max = COLOR_PAIRS;
+ }
+
+ /* this assumes an 80-column line */
+ if (opt_wide) {
+ width = 4;
+ hello = "Test";
+ per_row = (col_limit / ((colors_max > 8) ? 4 : 8));
+ } else {
+ width = 8;
+ hello = "Hello";
+ per_row = (col_limit / 8);
+ }
+ per_row -= MinColors;
+
+ row_limit = (pairs_max + per_row - 1) / per_row;
+
+ move(0, 0);
+ (void) printw("There are %d color pairs and %d colors",
+ pairs_max, COLORS);
+ if (colors_max != COLORS)
+ (void) printw(" (using %d colors)", colors_max);
+ if (MinColors)
+ (void) addstr(" besides 'default'");
+
+ clrtobot();
+ MvPrintw(top + 1, 0,
+ "%dx%d matrix of foreground/background colors, bold *%s*\n",
+ row_limit,
+ per_row,
+ opt_bold ? "on" : "off");
+
+ /* show color names/numbers across the top */
+ for (i = 0; i < per_row; i++)
+ show_color_name(top + 2, (i + 1) * width, i + MinColors, opt_wide);
+
+ /* show a grid of colors, with color names/ numbers on the left */
+ for (i = (NCURSES_PAIRS_T) (base_row * per_row); i < pairs_max; i++) {
+ int row = grid_top + (i / per_row) - base_row;
+ int col = (i % per_row + 1) * width;
+ NCURSES_PAIRS_T pair = i;
+
+ if ((i / per_row) > row_limit)
+ break;
+
+#define InxToFG(i) (NCURSES_COLOR_T) ((i % (colors_max - MinColors)) + MinColors)
+#define InxToBG(i) (NCURSES_COLOR_T) ((i / (colors_max - MinColors)) + MinColors)
+ if (row >= 0 && move(row, col) != ERR) {
+ NCURSES_COLOR_T fg = InxToFG(i);
+ NCURSES_COLOR_T bg = InxToBG(i);
+
+ init_pair(pair, fg, bg);
+ attron(COLOR_PAIR(pair));
+ if (opt_acsc)
+ attron(A_ALTCHARSET);
+ if (opt_bold)
+ attron(A_BOLD);
+ if (opt_revs)
+ attron(A_REVERSE);
+
+ if (opt_nums) {
+ _nc_SPRINTF(numbered, _nc_SLIMIT((size_t) (COLS + 1))
+ "{%02X}", (int) i);
+ hello = numbered;
+ }
+ printw("%-*.*s", width, width, hello);
+ (void) attrset(A_NORMAL);
+
+ if ((i % per_row) == 0 && InxToFG(i) == MinColors) {
+ show_color_name(row, 0, InxToBG(i), opt_wide);
+ }
+ ++shown;
+ } else if (shown) {
+ break;
+ }
+ }
+
+ switch (wGetchar(stdscr)) {
+ case 'a':
+ opt_acsc = FALSE;
+ break;
+ case 'A':
+ opt_acsc = TRUE;
+ break;
+ case 'b':
+ opt_bold = FALSE;
+ break;
+ case 'B':
+ opt_bold = TRUE;
+ break;
+ case 'c':
+ colors_max = color_cycle(colors_max, -1);
+ break;
+ case 'C':
+ colors_max = color_cycle(colors_max, 1);
+ break;
+ case 'n':
+ opt_nums = FALSE;
+ break;
+ case 'N':
+ opt_nums = TRUE;
+ break;
+ case 'r':
+ opt_revs = FALSE;
+ break;
+ case 'R':
+ opt_revs = TRUE;
+ break;
+ case case_QUIT:
+ done = TRUE;
+ continue;
+ case 'w':
+ set_color_test(opt_wide, FALSE);
+ break;
+ case 'W':
+ set_color_test(opt_wide, TRUE);
+ break;
+ case CTRL('p'):
+ case KEY_UP:
+ if (base_row <= 0) {
+ beep();
+ } else {
+ base_row -= 1;
+ }
+ break;
+ case CTRL('n'):
+ case KEY_DOWN:
+ if (base_row + page_size >= row_limit) {
+ beep();
+ } else {
+ base_row += 1;
+ }
+ break;
+ case CTRL('b'):
+ case KEY_PREVIOUS:
+ case KEY_PPAGE:
+ if (base_row <= 0) {
+ beep();
+ } else {
+ base_row -= (page_size - 1);
+ if (base_row < 0)
+ base_row = 0;
+ }
+ break;
+ case CTRL('f'):
+ case KEY_NEXT:
+ case KEY_NPAGE:
+ if (base_row + page_size >= row_limit) {
+ beep();
+ } else {
+ base_row += page_size - 1;
+ if (base_row + page_size >= row_limit) {
+ base_row = row_limit - page_size - 1;
+ }
+ }
+ break;
+ case HELP_KEY_1:
+ if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
+ box(helpwin, 0, 0);
+ color_legend(helpwin, FALSE);
+ wGetchar(helpwin);
+ delwin(helpwin);
+ }
+ break;
+ default:
+ beep();
+ continue;
+ }
+ }
+
+ erase();
+ endwin();
+
+ free(numbered);
+ return OK;
+}
+
+#if USE_WIDEC_SUPPORT
+/* generate a color test pattern */
+static int
+x_color_test(bool recur GCC_UNUSED)
+{
+ int i;
+ int top = 0, width;
+ int base_row = 0;
+ int grid_top = top + 3;
+ int page_size = (LINES - grid_top);
+ int pairs_max = (unsigned short) (-1);
+ int colors_max = COLORS;
+ int col_limit;
+ int row_limit;
+ int per_row;
+ char *numbered = 0;
+ const char *hello;
+ bool done = FALSE;
+ bool opt_acsc = FALSE;
+ bool opt_bold = FALSE;
+ bool opt_revs = FALSE;
+ bool opt_wide = FALSE;
+ bool opt_nums = FALSE;
+ bool opt_xchr = FALSE;
+ wchar_t *buffer = 0;
+ WINDOW *helpwin;
+
+ if (!UseColors) {
+ Cannot("does not support color.");
+ return ERR;
+ }
+ numbered = (char *) calloc((size_t) (COLS + 1), sizeof(char));
+ buffer = (wchar_t *) calloc((size_t) (COLS + 1), sizeof(wchar_t));
+ done = ((COLS < 16) || (numbered == 0) || (buffer == 0));
+
+ /*
+ * Because the number of colors is usually a power of two, we also use
+ * a power of two for the number of colors shown per line (to be tidy).
+ */
+ for (col_limit = 1; col_limit * 2 < COLS; col_limit *= 2) ;
+
+ while (!done) {
+ int shown = 0;
+
+ pairs_max = (unsigned short) (-1);
+ if (colors_max * colors_max <= COLOR_PAIRS) {
+ int limit = (colors_max - MinColors) * (colors_max - MinColors);
+ if (pairs_max > limit)
+ pairs_max = limit;
+ } else {
+ if (pairs_max > COLOR_PAIRS)
+ pairs_max = COLOR_PAIRS;
+ }
+
+ if (opt_wide) {
+ width = 4;
+ hello = "Test";
+ per_row = (col_limit / ((colors_max > 8) ? 4 : 8));
+ } else {
+ width = 8;
+ hello = "Hello";
+ per_row = (col_limit / 8);
+ }
+ per_row -= MinColors;
+
+ if (opt_xchr) {
+ make_fullwidth_text(buffer, hello);
+ width *= 2;
+ per_row /= 2;
+ } else {
+ make_narrow_text(buffer, hello);
+ }
+
+ row_limit = (pairs_max + per_row - 1) / per_row;
+
+ move(0, 0);
+ (void) printw("There are %d color pairs and %d colors",
+ pairs_max, COLORS);
+ if (colors_max != COLORS)
+ (void) printw(" (using %d colors)", colors_max);
+ if (MinColors)
+ (void) addstr(" besides 'default'");
+
+ clrtobot();
+ MvPrintw(top + 1, 0,
+ "%dx%d matrix of foreground/background colors, bold *%s*\n",
+ row_limit,
+ per_row,
+ opt_bold ? "on" : "off");
+
+ /* show color names/numbers across the top */
+ for (i = 0; i < per_row; i++)
+ show_color_name(top + 2, (i + 1) * width, i + MinColors, opt_wide);
+
+ /* show a grid of colors, with color names/ numbers on the left */
+ for (i = (base_row * per_row); i < pairs_max; i++) {
+ int row = grid_top + (i / per_row) - base_row;
+ int col = (i % per_row + 1) * width;
+ NCURSES_PAIRS_T pair = (NCURSES_PAIRS_T) i;
+
+ if ((i / per_row) > row_limit)
+ break;
+
+ if (row >= 0 && move(row, col) != ERR) {
+ init_pair(pair, InxToFG(i), InxToBG(i));
+ (void) color_set(pair, NULL);
+ if (opt_acsc)
+ attr_on(WA_ALTCHARSET, NULL);
+ if (opt_bold)
+ attr_on(WA_BOLD, NULL);
+ if (opt_revs)
+ attr_on(WA_REVERSE, NULL);
+
+ if (opt_nums) {
+ _nc_SPRINTF(numbered,
+ _nc_SLIMIT((size_t) (COLS + 1) * sizeof(wchar_t))
+ "{%02X}", i);
+ if (opt_xchr) {
+ make_fullwidth_text(buffer, numbered);
+ } else {
+ make_narrow_text(buffer, numbered);
+ }
+ }
+ addnwstr(buffer, width);
+ (void) attr_set(A_NORMAL, 0, NULL);
+
+ if ((i % per_row) == 0 && InxToFG(i) == MinColors) {
+ show_color_name(row, 0, InxToBG(i), opt_wide);
+ }
+ ++shown;
+ } else if (shown) {
+ break;
+ }
+ }
+
+ switch (wGetchar(stdscr)) {
+ case 'a':
+ opt_acsc = FALSE;
+ break;
+ case 'A':
+ opt_acsc = TRUE;
+ break;
+ case 'b':
+ opt_bold = FALSE;
+ break;
+ case 'B':
+ opt_bold = TRUE;
+ break;
+ case 'c':
+ colors_max = color_cycle(colors_max, -1);
+ break;
+ case 'C':
+ colors_max = color_cycle(colors_max, 1);
+ break;
+ case 'n':
+ opt_nums = FALSE;
+ break;
+ case 'N':
+ opt_nums = TRUE;
+ break;
+ case 'r':
+ opt_revs = FALSE;
+ break;
+ case 'R':
+ opt_revs = TRUE;
+ break;
+ case case_QUIT:
+ done = TRUE;
+ continue;
+ case 'w':
+ set_color_test(opt_wide, FALSE);
+ break;
+ case 'W':
+ set_color_test(opt_wide, TRUE);
+ break;
+ case 'x':
+ opt_xchr = FALSE;
+ break;
+ case 'X':
+ opt_xchr = TRUE;
+ break;
+ case CTRL('p'):
+ case KEY_UP:
+ if (base_row <= 0) {
+ beep();
+ } else {
+ base_row -= 1;
+ }
+ break;
+ case CTRL('n'):
+ case KEY_DOWN:
+ if (base_row + page_size >= row_limit) {
+ beep();
+ } else {
+ base_row += 1;
+ }
+ break;
+ case CTRL('b'):
+ case KEY_PREVIOUS:
+ case KEY_PPAGE:
+ if (base_row <= 0) {
+ beep();
+ } else {
+ base_row -= (page_size - 1);
+ if (base_row < 0)
+ base_row = 0;
+ }
+ break;
+ case CTRL('f'):
+ case KEY_NEXT:
+ case KEY_NPAGE:
+ if (base_row + page_size >= row_limit) {
+ beep();
+ } else {
+ base_row += page_size - 1;
+ if (base_row + page_size >= row_limit) {
+ base_row = row_limit - page_size - 1;
+ }
+ }
+ break;
+ case HELP_KEY_1:
+ if ((helpwin = newwin(LINES - 1, COLS - 2, 0, 0)) != 0) {
+ box(helpwin, 0, 0);
+ color_legend(helpwin, TRUE);
+ wGetchar(helpwin);
+ delwin(helpwin);
+ }
+ break;
+ default:
+ beep();
+ continue;
+ }
+ }
+
+ erase();
+ endwin();
+
+ free(numbered);
+ free(buffer);
+ return OK;
+}
+#endif /* USE_WIDEC_SUPPORT */
+
+#if HAVE_COLOR_CONTENT
+static void
+change_color(NCURSES_PAIRS_T current, int field, int value, int usebase)
+{
+ NCURSES_COLOR_T red, green, blue;
+
+ color_content(current, &red, &green, &blue);
+
+ switch (field) {
+ case 0:
+ red = (NCURSES_COLOR_T) (usebase ? (red + value) : value);
+ break;
+ case 1:
+ green = (NCURSES_COLOR_T) (usebase ? (green + value) : value);
+ break;
+ case 2:
+ blue = (NCURSES_COLOR_T) (usebase ? (blue + value) : value);
+ break;
+ }
+
+ if (init_color(current, red, green, blue) == ERR)
+ beep();
+}
+
+static void
+reset_all_colors(void)
+{
+ NCURSES_PAIRS_T c;
+
+ for (c = 0; c < COLORS; ++c)
+ init_color(c,
+ all_colors[c].red,
+ all_colors[c].green,
+ all_colors[c].blue);
+}
+
+#define okCOLOR(n) ((n) >= 0 && (n) < MaxColors)
+#define okRGB(n) ((n) >= 0 && (n) <= 1000)
+#define DecodeRGB(n) (NCURSES_COLOR_T) ((n * 1000) / 0xffff)
+
+static void
+init_all_colors(bool xterm_colors, char *palette_file)
+{
+ NCURSES_PAIRS_T cp;
+ all_colors = typeMalloc(RGB_DATA, (unsigned) MaxColors);
+ if (!all_colors)
+ failed("all_colors");
+ for (cp = 0; cp < MaxColors; ++cp) {
+ color_content(cp,
+ &all_colors[cp].red,
+ &all_colors[cp].green,
+ &all_colors[cp].blue);
+ }
+ /* xterm and compatible terminals can read results of an OSC string
+ * asking for the current color palette.
+ */
+ if (xterm_colors) {
+ int n;
+ int got;
+ char result[BUFSIZ];
+ int check_n;
+ unsigned check_r, check_g, check_b;
+
+ raw();
+ noecho();
+ for (n = 0; n < MaxColors; ++n) {
+ fprintf(stderr, "\033]4;%d;?\007", n);
+ got = (int) read(0, result, sizeof(result) - 1);
+ if (got < 0)
+ break;
+ result[got] = '\0';
+ if (sscanf(result, "\033]4;%d;rgb:%x/%x/%x\007",
+ &check_n,
+ &check_r,
+ &check_g,
+ &check_b) == 4 &&
+ check_n == n) {
+ all_colors[n].red = DecodeRGB(check_r);
+ all_colors[n].green = DecodeRGB(check_g);
+ all_colors[n].blue = DecodeRGB(check_b);
+ } else {
+ break;
+ }
+ }
+ reset_prog_mode();
+ }
+ if (palette_file != 0) {
+ FILE *fp = fopen(palette_file, "r");
+ if (fp != 0) {
+ char buffer[BUFSIZ];
+ int red, green, blue;
+ int scale = 1000;
+ int c;
+ while (fgets(buffer, sizeof(buffer), fp) != 0) {
+ if (sscanf(buffer, "scale:%d", &c) == 1) {
+ scale = c;
+ } else if (sscanf(buffer, "%d:%d %d %d",
+ &c,
+ &red,
+ &green,
+ &blue) == 4
+ && okCOLOR(c)
+ && okRGB(red)
+ && okRGB(green)
+ && okRGB(blue)) {
+#define Scaled(n) (NCURSES_COLOR_T) (((n) * 1000) / scale)
+ all_colors[c].red = Scaled(red);
+ all_colors[c].green = Scaled(green);
+ all_colors[c].blue = Scaled(blue);
+ }
+ }
+ fclose(fp);
+ }
+ }
+}
+
+#define scaled_rgb(n) ((255 * (n)) / 1000)
+
+static int
+color_edit(bool recur GCC_UNUSED)
+/* display the color test pattern, without trying to edit colors */
+{
+ int i;
+ int current;
+ int this_c, value, field;
+ int last_c;
+ int top_color;
+ int page_size;
+
+ if (!UseColors) {
+ Cannot("does not support color.");
+ return ERR;
+ } else if (!can_change_color()) {
+ Cannot("has hardwired color values.");
+ return ERR;
+ }
+
+ reset_all_colors();
+#ifdef KEY_RESIZE
+ retry:
+#endif
+ current = 0;
+ this_c = 0;
+ value = 0;
+ field = 0;
+ top_color = 0;
+ page_size = (LINES - 6);
+ erase();
+
+ for (i = 0; i < MaxColors; i++)
+ init_pair((NCURSES_PAIRS_T) i,
+ (NCURSES_COLOR_T) COLOR_WHITE,
+ (NCURSES_COLOR_T) i);
+
+ MvPrintw(LINES - 2, 0, "Number: %d", value);
+
+ do {
+ NCURSES_COLOR_T red, green, blue;
+
+ attron(A_BOLD);
+ MvAddStr(0, 20, "Color RGB Value Editing");
+ attroff(A_BOLD);
+
+ for (i = (NCURSES_COLOR_T) top_color;
+ (i - top_color < page_size)
+ && (i < MaxColors); i++) {
+ char numeric[80];
+
+ _nc_SPRINTF(numeric, _nc_SLIMIT(sizeof(numeric)) "[%d]", i);
+ MvPrintw(2 + i - top_color, 0, "%c %-8s:",
+ (i == current ? '>' : ' '),
+ (i < (int) SIZEOF(the_color_names)
+ ? the_color_names[i] : numeric));
+ (void) attrset(AttrArg(COLOR_PAIR(i), 0));
+ addstr(" ");
+ (void) attrset(A_NORMAL);
+
+ color_content((NCURSES_PAIRS_T) i, &red, &green, &blue);
+ addstr(" R = ");
+ if (current == i && field == 0)
+ attron(A_STANDOUT);
+ printw("%04d", (int) red);
+ if (current == i && field == 0)
+ (void) attrset(A_NORMAL);
+ addstr(", G = ");
+ if (current == i && field == 1)
+ attron(A_STANDOUT);
+ printw("%04d", (int) green);
+ if (current == i && field == 1)
+ (void) attrset(A_NORMAL);
+ addstr(", B = ");
+ if (current == i && field == 2)
+ attron(A_STANDOUT);
+ printw("%04d", (int) blue);
+ if (current == i && field == 2)
+ (void) attrset(A_NORMAL);
+ (void) attrset(A_NORMAL);
+ printw(" ( %3d %3d %3d )",
+ (int) scaled_rgb(red),
+ (int) scaled_rgb(green),
+ (int) scaled_rgb(blue));
+ }
+
+ MvAddStr(LINES - 3, 0,
+ "Use up/down to select a color, left/right to change fields.");
+ MvAddStr(LINES - 2, 0,
+ "Modify field by typing nnn=, nnn-, or nnn+. ? for help.");
+
+ move(2 + current - top_color, 0);
+
+ last_c = this_c;
+ this_c = Getchar();
+ if (this_c < 256 && isdigit(this_c) && !isdigit(last_c))
+ value = 0;
+
+ switch (this_c) {
+#ifdef KEY_RESIZE
+ case KEY_RESIZE:
+ move(0, 0);
+ goto retry;
+#endif
+ case '!':
+ ShellOut(FALSE);
+ /* FALLTHRU */
+ case CTRL('r'):
+ endwin();
+ refresh();
+ break;
+ case CTRL('l'):
+ refresh();
+ break;
+ case CTRL('b'):
+ case KEY_PPAGE:
+ if (current > 0)
+ current -= (page_size - 1);
+ else
+ beep();
+ break;
+
+ case CTRL('f'):
+ case KEY_NPAGE:
+ if (current < (MaxColors - 1))
+ current += (page_size - 1);
+ else
+ beep();
+ break;
+
+ case CTRL('p'):
+ case KEY_UP:
+ current = (current == 0 ? (MaxColors - 1) : current - 1);
+ break;
+
+ case CTRL('n'):
+ case KEY_DOWN:
+ current = (current == (MaxColors - 1) ? 0 : current + 1);
+ break;
+
+ case '\t':
+ case KEY_RIGHT:
+ field = (field == 2 ? 0 : field + 1);
+ break;
+
+ case KEY_BTAB:
+ case KEY_LEFT:
+ field = (field == 0 ? 2 : field - 1);
+ break;
+
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ value = value * 10 + (this_c - '0');
+ break;
+
+ case '+':
+ change_color((NCURSES_PAIRS_T) current, field, value, 1);
+ break;
+
+ case '-':
+ change_color((NCURSES_PAIRS_T) current, field, -value, 1);
+ break;
+
+ case '=':
+ change_color((NCURSES_PAIRS_T) current, field, value, 0);
+ break;
+
+ case HELP_KEY_1:
+ erase();
+ P(" RGB Value Editing Help");
+ P("");
+ P("You are in the RGB value editor. Use the arrow keys to select one of");
+ P("the fields in one of the RGB triples of the current colors; the one");
+ P("currently selected will be reverse-video highlighted.");
+ P("");
+ P("To change a field, enter the digits of the new value; they are echoed");
+ P("as entered. Finish by typing `='. The change will take effect instantly.");
+ P("To increment or decrement a value, use the same procedure, but finish");
+ P("with a `+' or `-'.");
+ P("");
+ P("Use `!' to shell-out, ^R or ^L to repaint the screen.");
+ P("");
+ P("Press 'm' to invoke the top-level menu with the current color settings.");
+ P("To quit, do ESC");
+
+ Pause();
+ erase();
+ break;
+
+ case 'm':
+ endwin();
+ main_menu(FALSE);
+ for (i = 0; i < MaxColors; i++)
+ init_pair((NCURSES_PAIRS_T) i,
+ (NCURSES_COLOR_T) COLOR_WHITE,
+ (NCURSES_COLOR_T) i);
+ refresh();
+ break;
+
+ case case_QUIT:
+ break;
+
+ default:
+ beep();
+ break;
+ }
+
+ if (current < 0)
+ current = 0;
+ if (current >= MaxColors)
+ current = MaxColors - 1;
+ if (current < top_color)
+ top_color = current;
+ if (current - top_color >= page_size)
+ top_color = current - (page_size - 1);
+
+ MvPrintw(LINES - 1, 0, "Number: %d", value);
+ clrtoeol();
+ } while
+ (!isQuit(this_c, TRUE));
+
+ erase();
+
+ /*
+ * ncurses does not reset each color individually when calling endwin().
+ */
+ reset_all_colors();
+
+ endwin();
+ return OK;
+}
+#endif /* HAVE_COLOR_CONTENT */
+
+/****************************************************************************
+ *
+ * Alternate character-set stuff
+ *
+ ****************************************************************************/
+static bool
+cycle_attr(int ch, unsigned *at_code, chtype *attr, ATTR_TBL * list, unsigned limit)
+{
+ bool result = TRUE;
+
+ switch (ch) {
+ case 'v':
+ if ((*at_code += 1) >= limit)
+ *at_code = 0;
+ break;
+ case 'V':
+ if (*at_code == 0)
+ *at_code = limit - 1;
+ else
+ *at_code -= 1;
+ break;
+ default:
+ result = FALSE;
+ break;
+ }
+ if (result)
+ *attr = list[*at_code].attr;
+ return result;
+}
+
+#if USE_WIDEC_SUPPORT
+static bool
+cycle_w_attr(int ch, unsigned *at_code, attr_t *attr, W_ATTR_TBL * list, unsigned limit)
+{
+ bool result = TRUE;
+
+ switch (ch) {
+ case 'v':
+ if ((*at_code += 1) >= limit)
+ *at_code = 0;
+ break;
+ case 'V':
+ if (*at_code == 0)
+ *at_code = limit - 1;
+ else
+ *at_code -= 1;
+ break;
+ default:
+ result = FALSE;
+ break;
+ }
+ if (result)
+ *attr = list[*at_code].attr;
+ return result;
+}
+#endif
+
+static bool
+cycle_colors(int ch, int *fg, int *bg, NCURSES_PAIRS_T *pair)
+{
+ bool result = FALSE;
+
+ if (UseColors) {
+ result = TRUE;
+ switch (ch) {
+ case 'F':
+ if ((*fg -= 1) < 0)
+ *fg = COLORS - 1;
+ break;
+ case 'f':
+ if ((*fg += 1) >= COLORS)
+ *fg = 0;
+ break;
+ case 'B':
+ if ((*bg -= 1) < 0)
+ *bg = COLORS - 1;
+ break;
+ case 'b':
+ if ((*bg += 1) >= COLORS)
+ *bg = 0;
+ break;
+ default:
+ result = FALSE;
+ break;
+ }
+ if (result) {
+ *pair = (NCURSES_PAIRS_T) (*fg != COLOR_BLACK || *bg != COLOR_BLACK);
+ if (*pair != 0) {
+ *pair = 1;
+ if (init_pair(*pair,
+ (NCURSES_COLOR_T) *fg,
+ (NCURSES_COLOR_T) *bg) == ERR) {
+ result = FALSE;
+ }
+ }
+ }
+ }
+ return result;
+}
+
+/****************************************************************************
+ *
+ * Soft-key label test
+ *
+ ****************************************************************************/
+
+#if USE_SOFTKEYS
+
+#define SLK_HELP 17
+#define SLK_WORK (SLK_HELP + 3)
+
+static void
+slk_help(void)
+{
+ static const char *table[] =
+ {
+ "Available commands are:"
+ ,""
+ ,"^L -- repaint this message and activate soft keys"
+ ,"a/d -- activate/disable soft keys"
+ ,"c -- set centered format for labels"
+ ,"l -- set left-justified format for labels"
+ ,"r -- set right-justified format for labels"
+ ,"[12345678] -- set label; labels are numbered 1 through 8"
+ ,"e -- erase stdscr (should not erase labels)"
+ ,"s -- test scrolling of shortened screen"
+ ,"v/V -- cycle through video attributes"
+#if HAVE_SLK_COLOR
+ ,"F/f/B/b -- cycle through foreground/background colors"
+#endif
+ ,"ESC -- return to main menu"
+ ,""
+ ,"Note: if activating the soft keys causes your terminal to scroll up"
+ ,"one line, your terminal auto-scrolls when anything is written to the"
+ ,"last screen position. The ncurses code does not yet handle this"
+ ,"gracefully."
+ };
+ unsigned j;
+
+ move(2, 0);
+ for (j = 0; j < SIZEOF(table); ++j) {
+ P(table[j]);
+ }
+ refresh();
+}
+
+#if HAVE_SLK_COLOR
+static void
+call_slk_color(int fg, int bg)
+{
+ init_pair(1, (NCURSES_COLOR_T) bg, (NCURSES_COLOR_T) fg);
+ slk_color(1);
+ MvPrintw(SLK_WORK, 0, "Colors %d/%d\n", fg, bg);
+ clrtoeol();
+ slk_touch();
+ slk_noutrefresh();
+ refresh();
+}
+#endif
+
+static int
+slk_test(bool recur GCC_UNUSED)
+/* exercise the soft keys */
+{
+ int c, fmt = 1;
+ char buf[9];
+ char *s;
+ chtype attr = A_NORMAL;
+ unsigned at_code = 0;
+#if HAVE_SLK_COLOR
+ int fg = COLOR_BLACK;
+ int bg = COLOR_WHITE;
+ NCURSES_PAIRS_T pair = 0;
+#endif
+ ATTR_TBL my_list[SIZEOF(attrs_to_test)];
+ unsigned my_size = init_attr_list(my_list, termattrs());
+
+ c = CTRL('l');
+#if HAVE_SLK_COLOR
+ if (UseColors) {
+ call_slk_color(fg, bg);
+ }
+#endif
+
+ do {
+ move(0, 0);
+ switch (c) {
+ case CTRL('l'):
+ erase();
+ attron(A_BOLD);
+ MvAddStr(0, 20, "Soft Key Exerciser");
+ attroff(A_BOLD);
+
+ slk_help();
+ /* fall through */
+
+ case 'a':
+ slk_restore();
+ break;
+
+ case 'e':
+ wclear(stdscr);
+ break;
+
+ case 's':
+ MvPrintw(SLK_WORK, 0, "Press Q to stop the scrolling-test: ");
+ while ((c = Getchar()) != 'Q' && (c != ERR))
+ AddCh(c);
+ break;
+
+ case 'd':
+ slk_clear();
+ break;
+
+ case 'l':
+ fmt = 0;
+ break;
+
+ case 'c':
+ fmt = 1;
+ break;
+
+ case 'r':
+ fmt = 2;
+ break;
+
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ MvAddStr(SLK_WORK, 0, "Please enter the label value: ");
+ _nc_STRCPY(buf, "", sizeof(buf));
+ if ((s = slk_label(c - '0')) != 0) {
+ _nc_STRNCPY(buf, s, (size_t) 8);
+ }
+ wGetstring(stdscr, buf, 8);
+ slk_set((c - '0'), buf, fmt);
+ slk_refresh();
+ move(SLK_WORK, 0);
+ clrtobot();
+ break;
+
+ case case_QUIT:
+ goto done;
+
+#if defined(NCURSES_VERSION) && defined(KEY_RESIZE) && HAVE_WRESIZE
+ case KEY_RESIZE:
+ wnoutrefresh(stdscr);
+ break;
+#endif
+
+ default:
+ if (cycle_attr(c, &at_code, &attr, my_list, my_size)) {
+ slk_attrset(attr);
+ slk_touch();
+ slk_noutrefresh();
+ break;
+ }
+#if HAVE_SLK_COLOR
+ if (cycle_colors(c, &fg, &bg, &pair)) {
+ if (UseColors) {
+ call_slk_color(fg, bg);
+ } else {
+ beep();
+ }
+ break;
+ }
+#endif
+ beep();
+ break;
+ }
+ } while (!isQuit(c = Getchar(), TRUE));
+
+ done:
+ slk_clear();
+ erase();
+ endwin();
+ return OK;
+}
+
+#if USE_WIDEC_SUPPORT
+#define SLKLEN 8
+static int
+x_slk_test(bool recur GCC_UNUSED)
+/* exercise the soft keys */
+{
+ int c, fmt = 1;
+ wchar_t buf[SLKLEN + 1];
+ char *s;
+ attr_t attr = WA_NORMAL;
+ unsigned at_code = 0;
+ int fg = COLOR_BLACK;
+ int bg = COLOR_WHITE;
+ NCURSES_PAIRS_T pair = 0;
+ W_ATTR_TBL my_list[SIZEOF(w_attrs_to_test)];
+ unsigned my_size = init_w_attr_list(my_list, term_attrs());
+
+ c = CTRL('l');
+ if (UseColors) {
+ call_slk_color(fg, bg);
+ }
+ do {
+ move(0, 0);
+ switch (c) {
+ case CTRL('l'):
+ erase();
+ attr_on(WA_BOLD, NULL);
+ MvAddStr(0, 20, "Soft Key Exerciser");
+ attr_off(WA_BOLD, NULL);
+
+ slk_help();
+ /* fall through */
+
+ case 'a':
+ slk_restore();
+ break;
+
+ case 'e':
+ wclear(stdscr);
+ break;
+
+ case 's':
+ MvPrintw(SLK_WORK, 0, "Press Q to stop the scrolling-test: ");
+ while ((c = Getchar()) != 'Q' && (c != ERR))
+ AddCh(c);
+ break;
+
+ case 'd':
+ slk_clear();
+ break;
+
+ case 'l':
+ fmt = 0;
+ break;
+
+ case 'c':
+ fmt = 1;
+ break;
+
+ case 'r':
+ fmt = 2;
+ break;
+
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ MvAddStr(SLK_WORK, 0, "Please enter the label value: ");
+ *buf = 0;
+ if ((s = slk_label(c - '0')) != 0) {
+ char *temp = strdup(s);
+ size_t used = strlen(temp);
+ size_t want = SLKLEN;
+ size_t test;
+#ifndef state_unused
+ mbstate_t state;
+#endif
+
+ buf[0] = L'\0';
+ while (want > 0 && used != 0) {
+ const char *base = s;
+ reset_mbytes(state);
+ test = count_mbytes(base, 0, &state);
+ if (test == (size_t) -1) {
+ temp[--used] = 0;
+ } else if (test > want) {
+ temp[--used] = 0;
+ } else {
+ reset_mbytes(state);
+ trans_mbytes(buf, base, want, &state);
+ break;
+ }
+ }
+ free(temp);
+ }
+ wGet_wstring(stdscr, buf, SLKLEN);
+ slk_wset((c - '0'), buf, fmt);
+ slk_refresh();
+ move(SLK_WORK, 0);
+ clrtobot();
+ break;
+
+ case case_QUIT:
+ goto done;
+
+ case 'F':
+ if (UseColors) {
+ fg = (NCURSES_COLOR_T) ((fg + 1) % COLORS);
+ call_slk_color(fg, bg);
+ }
+ break;
+ case 'B':
+ if (UseColors) {
+ bg = (NCURSES_COLOR_T) ((bg + 1) % COLORS);
+ call_slk_color(fg, bg);
+ }
+ break;
+#if defined(NCURSES_VERSION) && defined(KEY_RESIZE) && HAVE_WRESIZE
+ case KEY_RESIZE:
+ wnoutrefresh(stdscr);
+ break;
+#endif
+ default:
+ if (cycle_w_attr(c, &at_code, &attr, my_list, my_size)) {
+ slk_attr_set(attr, (NCURSES_COLOR_T) (fg || bg), NULL);
+ slk_touch();
+ slk_noutrefresh();
+ break;
+ }
+#if HAVE_SLK_COLOR
+ if (cycle_colors(c, &fg, &bg, &pair)) {
+ if (UseColors) {
+ call_slk_color(fg, bg);
+ } else {
+ beep();
+ }
+ break;
+ }
+#endif
+ beep();
+ break;
+ }
+ } while (!isQuit(c = Getchar(), TRUE));
+
+ done:
+ slk_clear();
+ erase();
+ endwin();
+ return OK;
+}
+#endif
+#endif /* SLK_INIT */
+
+static void
+show_256_chars(int repeat, attr_t attr, NCURSES_PAIRS_T pair)
+{
+ unsigned first = 0;
+ unsigned last = 255;
+ unsigned code;
+ int count;
+
+ erase();
+ attron(A_BOLD);
+ MvPrintw(0, 20, "Display of Character Codes %#0x to %#0x",
+ first, last);
+ attroff(A_BOLD);
+ refresh();
+
+ for (code = first; code <= last; ++code) {
+ int row = (int) (2 + (code / 16));
+ int col = (int) (5 * (code % 16));
+ IGNORE_RC(mvaddch(row, col, colored_chtype(code, attr, pair)));
+ for (count = 1; count < repeat; ++count) {
+ AddCh(colored_chtype(code, attr, pair));
+ }
+ }
+
+}
+
+/*
+ * Show a slice of 32 characters, allowing those to be repeated up to the
+ * screen's width.
+ *
+ * ISO 6429: codes 0x80 to 0x9f may be control characters that cause the
+ * terminal to perform functions. The remaining codes can be graphic.
+ */
+static void
+show_upper_chars(int base, int pagesize, int repeat, attr_t attr, NCURSES_PAIRS_T pair)
+{
+ unsigned code;
+ unsigned first = (unsigned) base;
+ unsigned last = first + (unsigned) pagesize - 2;
+ bool C1 = (first == 128);
+ int reply;
+
+ erase();
+ attron(A_BOLD);
+ MvPrintw(0, 20, "Display of %s Character Codes %d to %d",
+ C1 ? "C1" : "GR", first, last);
+ attroff(A_BOLD);
+ refresh();
+
+ for (code = first; code <= last; code++) {
+ int count = repeat;
+ int row = 2 + ((int) (code - first) % (pagesize / 2));
+ int col = ((int) (code - first) / (pagesize / 2)) * COLS / 2;
+ char tmp[80];
+ _nc_SPRINTF(tmp, _nc_SLIMIT(sizeof(tmp)) "%3u (0x%x)", code, code);
+ MvPrintw(row, col, "%*s: ", COLS / 4, tmp);
+
+ do {
+ if (C1)
+ nodelay(stdscr, TRUE);
+ echochar(colored_chtype(code, attr, pair));
+ if (C1) {
+ /* (yes, this _is_ crude) */
+ while ((reply = Getchar()) != ERR) {
+ AddCh(UChar(reply));
+ napms(10);
+ }
+ nodelay(stdscr, FALSE);
+ }
+ } while (--count > 0);
+ }
+}
+
+#define PC_COLS 4
+
+static void
+show_pc_chars(int repeat, attr_t attr, NCURSES_PAIRS_T pair)
+{
+ unsigned code;
+
+ erase();
+ attron(A_BOLD);
+ MvPrintw(0, 20, "Display of PC Character Codes");
+ attroff(A_BOLD);
+ refresh();
+
+ for (code = 0; code < 16; ++code) {
+ MvPrintw(2, (int) code * PC_COLS + 8, "%X", code);
+ }
+ for (code = 0; code < 256; code++) {
+ int count = repeat;
+ int row = 3 + (int) (code / 16) + (code >= 128);
+ int col = 8 + (int) (code % 16) * PC_COLS;
+ if ((code % 16) == 0)
+ MvPrintw(row, 0, "0x%02x:", code);
+ move(row, col);
+ do {
+ switch (code) {
+ case '\n':
+ case '\r':
+ case '\b':
+ case '\f':
+ case '\033':
+ case 0x9b:
+ /*
+ * Skip the ones that do not work.
+ */
+ break;
+ default:
+ AddCh(colored_chtype(code, A_ALTCHARSET | attr, pair));
+ break;
+ }
+ } while (--count > 0);
+ }
+}
+
+static void
+show_box_chars(int repeat, attr_t attr, NCURSES_PAIRS_T pair)
+{
+ (void) repeat;
+
+ attr |= (attr_t) COLOR_PAIR(pair);
+
+ erase();
+ attron(A_BOLD);
+ MvAddStr(0, 20, "Display of the ACS Line-Drawing Set");
+ attroff(A_BOLD);
+ refresh();
+ /* *INDENT-OFF* */
+ wborder(stdscr,
+ colored_chtype(ACS_VLINE, attr, pair),
+ colored_chtype(ACS_VLINE, attr, pair),
+ colored_chtype(ACS_HLINE, attr, pair),
+ colored_chtype(ACS_HLINE, attr, pair),
+ colored_chtype(ACS_ULCORNER, attr, pair),
+ colored_chtype(ACS_URCORNER, attr, pair),
+ colored_chtype(ACS_LLCORNER, attr, pair),
+ colored_chtype(ACS_LRCORNER, attr, pair));
+ MvHLine(LINES / 2, 0, colored_chtype(ACS_HLINE, attr, pair), COLS);
+ MvVLine(0, COLS / 2, colored_chtype(ACS_VLINE, attr, pair), LINES);
+ MvAddCh(0, COLS / 2, colored_chtype(ACS_TTEE, attr, pair));
+ MvAddCh(LINES / 2, COLS / 2, colored_chtype(ACS_PLUS, attr, pair));
+ MvAddCh(LINES - 1, COLS / 2, colored_chtype(ACS_BTEE, attr, pair));
+ MvAddCh(LINES / 2, 0, colored_chtype(ACS_LTEE, attr, pair));
+ MvAddCh(LINES / 2, COLS - 1, colored_chtype(ACS_RTEE, attr, pair));
+ /* *INDENT-ON* */
+
+}
+
+static int
+show_1_acs(int n, int repeat, const char *name, chtype code)
+{
+ const int height = 16;
+ int row = 2 + (n % height);
+ int col = (n / height) * COLS / 2;
+
+ MvPrintw(row, col, "%*s : ", COLS / 4, name);
+ do {
+ AddCh(code);
+ } while (--repeat > 0);
+ return n + 1;
+}
+
+static void
+show_acs_chars(int repeat, attr_t attr, NCURSES_PAIRS_T pair)
+/* display the ACS character set */
+{
+ int n;
+
+#define BOTH(name) #name, colored_chtype(name, attr, (chtype) pair)
+
+ erase();
+ attron(A_BOLD);
+ MvAddStr(0, 20, "Display of the ACS Character Set");
+ attroff(A_BOLD);
+ refresh();
+
+ n = show_1_acs(0, repeat, BOTH(ACS_ULCORNER));
+ n = show_1_acs(n, repeat, BOTH(ACS_URCORNER));
+ n = show_1_acs(n, repeat, BOTH(ACS_LLCORNER));
+ n = show_1_acs(n, repeat, BOTH(ACS_LRCORNER));
+
+ n = show_1_acs(n, repeat, BOTH(ACS_LTEE));
+ n = show_1_acs(n, repeat, BOTH(ACS_RTEE));
+ n = show_1_acs(n, repeat, BOTH(ACS_TTEE));
+ n = show_1_acs(n, repeat, BOTH(ACS_BTEE));
+
+ n = show_1_acs(n, repeat, BOTH(ACS_HLINE));
+ n = show_1_acs(n, repeat, BOTH(ACS_VLINE));
+
+ /*
+ * HPUX's ACS definitions are broken here. Just give up.
+ */
+#if !(defined(__hpux) && !defined(NCURSES_VERSION))
+ n = show_1_acs(n, repeat, BOTH(ACS_LARROW));
+ n = show_1_acs(n, repeat, BOTH(ACS_RARROW));
+ n = show_1_acs(n, repeat, BOTH(ACS_UARROW));
+ n = show_1_acs(n, repeat, BOTH(ACS_DARROW));
+
+ n = show_1_acs(n, repeat, BOTH(ACS_BLOCK));
+ n = show_1_acs(n, repeat, BOTH(ACS_BOARD));
+ n = show_1_acs(n, repeat, BOTH(ACS_LANTERN));
+ n = show_1_acs(n, repeat, BOTH(ACS_BULLET));
+ n = show_1_acs(n, repeat, BOTH(ACS_CKBOARD));
+ n = show_1_acs(n, repeat, BOTH(ACS_DEGREE));
+ n = show_1_acs(n, repeat, BOTH(ACS_DIAMOND));
+ n = show_1_acs(n, repeat, BOTH(ACS_PLMINUS));
+ n = show_1_acs(n, repeat, BOTH(ACS_PLUS));
+
+ n = show_1_acs(n, repeat, BOTH(ACS_GEQUAL));
+ n = show_1_acs(n, repeat, BOTH(ACS_NEQUAL));
+ n = show_1_acs(n, repeat, BOTH(ACS_LEQUAL));
+
+ n = show_1_acs(n, repeat, BOTH(ACS_STERLING));
+ n = show_1_acs(n, repeat, BOTH(ACS_PI));
+ n = show_1_acs(n, repeat, BOTH(ACS_S1));
+ n = show_1_acs(n, repeat, BOTH(ACS_S3));
+ n = show_1_acs(n, repeat, BOTH(ACS_S7));
+ (void) show_1_acs(n, repeat, BOTH(ACS_S9));
+#endif
+#undef BOTH
+}
+
+static int
+acs_test(bool recur GCC_UNUSED)
+{
+ int c = 'a';
+ int pagesize = 32;
+ char *term = getenv("TERM");
+ const char *pch_kludge = ((term != 0 && strstr(term, "linux"))
+ ? "p=PC, "
+ : "");
+ chtype attr = A_NORMAL;
+ int digit = 0;
+ int repeat = 1;
+ int fg = COLOR_BLACK;
+ int bg = COLOR_BLACK;
+ unsigned at_code = 0;
+ NCURSES_PAIRS_T pair = 0;
+ void (*last_show_acs) (int, attr_t, NCURSES_PAIRS_T) = 0;
+ ATTR_TBL my_list[SIZEOF(attrs_to_test)];
+ unsigned my_size = init_attr_list(my_list, termattrs());
+
+ do {
+ switch (c) {
+ case CTRL('L'):
+ Repaint();
+ break;
+ case 'a':
+ ToggleAcs(last_show_acs, show_acs_chars);
+ break;
+ case 'p':
+ if (*pch_kludge)
+ ToggleAcs(last_show_acs, show_pc_chars);
+ else
+ beep();
+ break;
+ case 'w':
+ if (pagesize == 32) {
+ pagesize = 256;
+ } else {
+ pagesize = 32;
+ }
+ break;
+ case 'x':
+ ToggleAcs(last_show_acs, show_box_chars);
+ break;
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ digit = (c - '0');
+ last_show_acs = 0;
+ break;
+ case '-':
+ if (digit > 0) {
+ --digit;
+ last_show_acs = 0;
+ } else {
+ beep();
+ }
+ break;
+ case '+':
+ if (digit < 3) {
+ ++digit;
+ last_show_acs = 0;
+ } else {
+ beep();
+ }
+ break;
+ case '>':
+ if (repeat < (COLS / 4))
+ ++repeat;
+ break;
+ case '<':
+ if (repeat > 1)
+ --repeat;
+ break;
+ default:
+ if (cycle_attr(c, &at_code, &attr, my_list, my_size)
+ || cycle_colors(c, &fg, &bg, &pair)) {
+ break;
+ } else {
+ beep();
+ }
+ break;
+ }
+ if (pagesize != 32) {
+ show_256_chars(repeat, attr, pair);
+ } else if (last_show_acs != 0) {
+ last_show_acs(repeat, attr, pair);
+ } else {
+ show_upper_chars(digit * pagesize + 128, pagesize, repeat, attr, pair);
+ }