1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * $Id: demo_new_pair.c,v 1.21 2020/02/02 23:34:34 tom Exp $
32 * Demonstrate the alloc_pair() function.
35 #include <test.priv.h>
37 #include <popup_msg.h>
39 #if HAVE_ALLOC_PAIR && USE_WIDEC_SUPPORT
41 #include <sys/types.h>
45 #define MAX_BITS 8 /* all but A_ALTCHARSET */
46 #define MAX_ATTR ((1<<MAX_BITS)-1)
49 valid_cap(NCURSES_CONST char *name)
51 char *value = tigetstr(name);
52 return (value != 0 && value != (char *) -1) ? TRUE : FALSE;
58 static bool init = FALSE;
59 static attr_t table[MAX_BITS * MAX_BITS];
64 attr_t bits[MAX_BITS];
67 bits[limit++] = WA_NORMAL;
68 if (valid_cap("smso"))
69 bits[limit++] = WA_STANDOUT;
70 if (valid_cap("smul"))
71 bits[limit++] = WA_UNDERLINE;
73 bits[limit++] = WA_REVERSE;
74 if (valid_cap("blink"))
75 bits[limit++] = WA_BLINK;
77 bits[limit++] = WA_DIM;
78 if (valid_cap("bold"))
79 bits[limit++] = WA_BOLD;
80 for (j = 0; j < limit; ++j) {
81 for (k = 0; k < limit; ++k) {
82 table[j * limit + k] = bits[j] | bits[k];
86 return table[now % limit];
90 our_content(int pair, int *fg, int *bg)
93 *fg = (pair / COLORS) % COLORS;
94 *bg = (pair % COLORS);
101 our_content(now, &fg, &bg);
102 return alloc_pair(fg, bg);
109 if ((short) now > 0) {
110 if (now < COLOR_PAIRS) {
112 our_content(now, &fg, &bg);
113 if (init_pair((short) now, (short) fg, (short) bg) != OK)
126 return time((time_t *) 0);
132 static const char *msg[] =
134 "Usage: demo_new_pair [options]",
136 "Repeatedly print using all possible color combinations.",
139 " -i use init_pair rather than alloc_pair",
140 " -p start in paged-mode",
141 " -s start in single-step mode",
142 " -w print a wide-character cell",
145 for (n = 0; n < SIZEOF(msg); ++n) {
146 fprintf(stderr, "%s\n", msg[n]);
148 ExitProgram(EXIT_FAILURE);
151 #define use_pages() \
152 paged_mode = TRUE, single_mode = TRUE
154 #define use_single() \
155 paged_mode = FALSE, single_mode = TRUE
157 #define update_modes() \
158 scrollok(stdscr, !paged_mode); \
159 nodelay(stdscr, !single_mode || paged_mode)
162 main(int argc, char *argv[])
164 static const char *help[] =
166 "This program iterates over the possible color combinations,",
167 "allocating or initializing color pairs. For best results,",
168 "choose screen-width dividing evenly into the number of colors,",
171 " 32x64,32x128 256 colors",
172 " 24x44,24x88 88 colors",
173 " 32x64,24x128 16 colors",
176 " c toggle between coloring and de-coloring cells",
177 " p show one page at a time",
178 " s show one character at a time",
179 " <space> display char/page without pausing",
180 " v/V cycle through video attributes",
181 " w toggle between \"#\" and a double-width equivalent",
182 " ? print this screen (exit on any character).",
184 "To exit this program, press ^Q, ^[ or \"q\".",
189 bool clobber = FALSE;
190 bool hascolor = FALSE;
191 bool use_init = FALSE;
192 bool use_wide = FALSE;
193 bool paged_mode = FALSE;
194 bool single_mode = FALSE;
199 time_t start = now();
200 long total_cells = 0;
203 setlocale(LC_ALL, "");
205 while ((ch = getopt(argc, argv, "ipsw")) != -1) {
225 if (isatty(fileno(stderr))) {
227 } else if ((ch = open("/dev/tty", O_WRONLY)) >= 0) {
228 output = fdopen(ch, "w");
230 fprintf(stderr, "cannot open terminal for output\n");
231 ExitProgram(EXIT_FAILURE);
233 if (newterm(NULL, output, stdin) == 0) {
234 fprintf(stderr, "Cannot initialize terminal\n");
236 ExitProgram(EXIT_FAILURE);
238 (void) cbreak(); /* read chars without wait for \n */
239 (void) noecho(); /* don't echo input */
243 keypad(stdscr, TRUE);
245 if ((hascolor = has_colors())) {
253 * Repeatedly cycle through all colors, initializing pairs as needed.
254 * Provide for single-stepping, or page-at-a-time, as well as quitting.
263 popup_msg(stdscr, help);
288 if (--video_mode < 0)
289 video_mode = MAX_ATTR;
292 if (video_mode > MAX_ATTR)
296 use_wide = !use_wide;
305 my_attrs = next_attr(video_mode);
308 our_content(current, &fg, &bg);
309 my_pair = find_pair(fg, bg);
316 ? next_color(current)
317 : make_color(current));
320 my_attrs = next_attr(current);
325 wch[0] = use_wide ? 0xff03 : '#';
327 setcchar(&temp, wch, my_attrs,
329 (use_init ? NULL : (void *) &my_pair));
331 * At the end of a page, move the cursor to the home position.
333 if ((add_wch(&temp) == ERR) && paged_mode) {
334 nodelay(stdscr, !single_mode);
337 total_cells += 1 + (use_wide ? 1 : 0);
343 printf("%.1f cells/second\n",
344 (double) (total_cells) / (double) (now() - start));
346 ExitProgram(EXIT_SUCCESS);
353 printf("This program requires the ncurses alloc_pair function\n");
354 ExitProgram(EXIT_FAILURE);