+/*
+ * 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;
+
+ if (first >= ATTRSTRING_1ST) {
+ int j, k;
+
+ 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;
+
+ memset(&ch, 0, sizeof(ch));
+ if (getbkgrnd(&ch) != ERR) {
+ wchar_t wch[CCHARW_MAX];
+
+ 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;
+ 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) {
+ unsigned j, k;
+
+ 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, int zoom)
+{
+ if (move(y, x) != ERR) {
+ char temp[80];
+ int width = 8;
+
+ if (wide || zoom) {
+ _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
+ "%02d", color);
+ if (wide)
+ width = 4;
+ if ((int) strlen(temp) >= width) {
+ int pwr2 = 0;
+ while ((1 << pwr2) < color)
+ ++pwr2;
+ _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
+ width > 4 ? "2^%d" : "^%d", pwr2);
+ }
+ } 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 switch width between 4/8 columns");
+ MvWPrintw(helpwin, row++, col,
+ " z/Z zoom out (or in)");
+#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;
+ int opt_zoom = 0;
+ WINDOW *helpwin;
+
+ if (!UseColors) {
+ Cannot("does not support color.");
+ return ERR;
+ }
+
+ numbered = typeCalloc(char, COLS + 1);
+ 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) ;
+
+ reloop:
+ while (!done) {
+ int shown = 0;
+ int zoom_size = (1 << opt_zoom);
+ int colors_max1 = colors_max / zoom_size;
+ double colors_max2 = (double) colors_max1 * (double) colors_max1;
+
+ pairs_max = PAIR_NUMBER(A_COLOR) + 1;
+ if (colors_max2 <= COLOR_PAIRS) {
+ int limit = (colors_max1 - MinColors) * (colors_max1 - MinColors);
+ if (pairs_max > limit)
+ pairs_max = limit;
+ }
+ if (pairs_max > COLOR_PAIRS)
+ pairs_max = COLOR_PAIRS;
+ if (pairs_max < colors_max1)
+ pairs_max = colors_max1;
+
+ /* this assumes an 80-column line */
+ if (opt_wide) {
+ width = 4;
+ hello = "Test";
+ per_row = (col_limit / ((colors_max1 > 8) ? width : 8));
+ } else {
+ width = 8;
+ hello = "Hello";
+ per_row = (col_limit / width);
+ }
+ 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_max1 != COLORS)
+ (void) printw(" (using %d colors)", colors_max1);
+ if (MinColors)
+ (void) addstr(" besides 'default'");
+ if (opt_zoom)
+ (void) printw(" zoom:%d", opt_zoom);
+
+ 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,
+ (int) i * zoom_size + MinColors,
+ opt_wide,
+ opt_zoom);
+ }
+
+ /* 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) (int)((((unsigned long)(i) * (unsigned long)zoom_size) % (unsigned long)(colors_max1 - MinColors)) + (unsigned long)MinColors)
+#define InxToBG(i) (int)((((unsigned long)(i) * (unsigned long)zoom_size) / (unsigned long)(colors_max1 - MinColors)) + (unsigned long)MinColors)
+ if (row >= 0 && move(row, col) != ERR) {
+ NCURSES_COLOR_T fg = (NCURSES_COLOR_T) InxToFG(i);
+ NCURSES_COLOR_T bg = (NCURSES_COLOR_T) 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,
+ opt_zoom);
+ }
+ ++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 'z':
+ if (opt_zoom <= 0) {
+ beep();
+ } else {
+ --opt_zoom;
+ goto reloop;
+ }
+ break;
+ case 'Z':
+ if ((1 << opt_zoom) >= colors_max) {
+ beep();
+ } else {
+ ++opt_zoom;
+ goto reloop;
+ }
+ 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
+
+#if USE_EXTENDED_COLOR
+#define InitExtendedPair(p,f,g) init_extended_pair((p),(f),(g))
+#define ExtendedColorSet(p) color_set((NCURSES_PAIRS_T) (p), &(p))
+#define EXTENDED_PAIRS_T int
+#else
+#define InitExtendedPair(p,f,g) init_pair((NCURSES_PAIRS_T) (p),(NCURSES_COLOR_T)(f),(NCURSES_COLOR_T)(g))
+#define ExtendedColorSet(p) color_set((NCURSES_PAIRS_T) (p), NULL)
+#define EXTENDED_PAIRS_T NCURSES_PAIRS_T
+#endif
+
+/* generate a color test pattern */
+static int
+x_color_test(bool recur GCC_UNUSED)
+{
+ long 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_wide = FALSE;
+ bool opt_nums = FALSE;
+ bool opt_xchr = FALSE;
+ int opt_zoom = 0;
+ wchar_t *buffer = 0;
+ WINDOW *helpwin;
+
+ if (!UseColors) {
+ Cannot("does not support color.");
+ return ERR;
+ }
+ numbered = typeCalloc(char, COLS + 1);
+ buffer = typeCalloc(wchar_t, COLS + 1);
+ 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) ;
+
+ reloop:
+ while (!done) {
+ int shown = 0;
+ int zoom_size = (1 << opt_zoom);
+ int colors_max1 = colors_max / zoom_size;
+ double colors_max2 = (double) colors_max1 * (double) colors_max1;
+
+ pairs_max = ((unsigned) (-1)) / 2;
+ if (colors_max2 <= COLOR_PAIRS) {
+ int limit = (colors_max1 - MinColors) * (colors_max1 - MinColors);
+ if (pairs_max > limit)
+ pairs_max = limit;
+ }
+ if (pairs_max > COLOR_PAIRS)
+ pairs_max = COLOR_PAIRS;
+ if (pairs_max < colors_max1)
+ pairs_max = colors_max1;
+
+ if (opt_wide) {
+ width = 4;
+ hello = "Test";
+ per_row = (col_limit / ((colors_max1 > 8) ? width : 8));
+ } else {
+ width = 8;
+ hello = "Hello";
+ per_row = (col_limit / width);
+ }
+ 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_max1 != COLORS)
+ (void) printw(" (using %d colors)", colors_max1);
+ if (MinColors)
+ (void) addstr(" besides 'default'");
+ if (opt_zoom)
+ (void) printw(" zoom:%d", opt_zoom);
+
+ 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,
+ ((int) i + 1) * width,
+ (int) i * zoom_size + MinColors,
+ opt_wide,
+ opt_zoom);
+ }
+
+ /* 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 + ((int) i / per_row) - base_row;
+ int col = ((int) i % per_row + 1) * width;
+ int pair = (int) i;
+
+ if ((i / per_row) > row_limit)
+ break;
+
+ if (row >= 0 && move(row, col) != ERR) {
+ InitExtendedPair(pair, InxToFG(i), InxToBG(i));
+ (void) ExtendedColorSet(pair);
+ 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}", (unsigned) 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,
+ opt_zoom);
+ }
+ ++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 'z':
+ if (opt_zoom <= 0) {
+ beep();
+ } else {
+ --opt_zoom;
+ goto reloop;
+ }
+ break;
+ case 'Z':
+ if ((1 << opt_zoom) >= colors_max) {
+ beep();
+ } else {
+ ++opt_zoom;
+ goto reloop;
+ }
+ 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;
+ char result[BUFSIZ];
+ int check_n;
+ unsigned check_r, check_g, check_b;
+
+ raw();
+ noecho();
+
+ for (n = 0; n < MaxColors; ++n) {
+ int got;
+
+ 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;
+ if (scale < 100)
+ scale = 100;
+ if (scale > 1000)
+ scale = 1000;
+ } 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'):