1 /****************************************************************************
2 * Copyright (c) 2017-2018,2019 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 * $Id: demo_new_pair.c,v 1.20 2019/12/14 23:26:09 tom Exp $
31 * Demonstrate the alloc_pair() function.
34 #include <test.priv.h>
36 #include <popup_msg.h>
38 #if HAVE_ALLOC_PAIR && USE_WIDEC_SUPPORT
40 #include <sys/types.h>
44 #define MAX_BITS 8 /* all but A_ALTCHARSET */
45 #define MAX_ATTR ((1<<MAX_BITS)-1)
48 valid_cap(NCURSES_CONST char *name)
50 char *value = tigetstr(name);
51 return (value != 0 && value != (char *) -1) ? TRUE : FALSE;
57 static bool init = FALSE;
58 static attr_t table[MAX_BITS * MAX_BITS];
63 attr_t bits[MAX_BITS];
66 bits[limit++] = WA_NORMAL;
67 if (valid_cap("smso"))
68 bits[limit++] = WA_STANDOUT;
69 if (valid_cap("smul"))
70 bits[limit++] = WA_UNDERLINE;
72 bits[limit++] = WA_REVERSE;
73 if (valid_cap("blink"))
74 bits[limit++] = WA_BLINK;
76 bits[limit++] = WA_DIM;
77 if (valid_cap("bold"))
78 bits[limit++] = WA_BOLD;
79 for (j = 0; j < limit; ++j) {
80 for (k = 0; k < limit; ++k) {
81 table[j * limit + k] = bits[j] | bits[k];
85 return table[now % limit];
89 our_content(int pair, int *fg, int *bg)
92 *fg = (pair / COLORS) % COLORS;
93 *bg = (pair % COLORS);
100 our_content(now, &fg, &bg);
101 return alloc_pair(fg, bg);
108 if ((short) now > 0) {
109 if (now < COLOR_PAIRS) {
111 our_content(now, &fg, &bg);
112 if (init_pair((short) now, (short) fg, (short) bg) != OK)
125 return time((time_t *) 0);
131 static const char *msg[] =
133 "Usage: demo_new_pair [options]",
135 "Repeatedly print using all possible color combinations.",
138 " -i use init_pair rather than alloc_pair",
139 " -p start in paged-mode",
140 " -s start in single-step mode",
141 " -w print a wide-character cell",
144 for (n = 0; n < SIZEOF(msg); ++n) {
145 fprintf(stderr, "%s\n", msg[n]);
147 ExitProgram(EXIT_FAILURE);
150 #define use_pages() \
151 paged_mode = TRUE, single_mode = TRUE
153 #define use_single() \
154 paged_mode = FALSE, single_mode = TRUE
156 #define update_modes() \
157 scrollok(stdscr, !paged_mode); \
158 nodelay(stdscr, !single_mode || paged_mode)
161 main(int argc, char *argv[])
163 static const char *help[] =
165 "This program iterates over the possible color combinations,",
166 "allocating or initializing color pairs. For best results,",
167 "choose screen-width dividing evenly into the number of colors,",
170 " 32x64,32x128 256 colors",
171 " 24x44,24x88 88 colors",
172 " 32x64,24x128 16 colors",
175 " c toggle between coloring and de-coloring cells",
176 " p show one page at a time",
177 " s show one character at a time",
178 " <space> display char/page without pausing",
179 " v/V cycle through video attributes",
180 " w toggle between \"#\" and a double-width equivalent",
181 " ? print this screen (exit on any character).",
183 "To exit this program, press ^Q, ^[ or \"q\".",
188 bool clobber = FALSE;
189 bool hascolor = FALSE;
190 bool use_init = FALSE;
191 bool use_wide = FALSE;
192 bool paged_mode = FALSE;
193 bool single_mode = FALSE;
198 time_t start = now();
199 long total_cells = 0;
202 setlocale(LC_ALL, "");
204 while ((ch = getopt(argc, argv, "ipsw")) != -1) {
224 if (isatty(fileno(stderr))) {
226 } else if ((ch = open("/dev/tty", O_WRONLY)) >= 0) {
227 output = fdopen(ch, "w");
229 fprintf(stderr, "cannot open terminal for output\n");
230 ExitProgram(EXIT_FAILURE);
232 if (newterm(NULL, output, stdin) == 0) {
233 fprintf(stderr, "Cannot initialize terminal\n");
235 ExitProgram(EXIT_FAILURE);
237 (void) cbreak(); /* read chars without wait for \n */
238 (void) noecho(); /* don't echo input */
242 keypad(stdscr, TRUE);
244 if ((hascolor = has_colors())) {
252 * Repeatedly cycle through all colors, initializing pairs as needed.
253 * Provide for single-stepping, or page-at-a-time, as well as quitting.
262 popup_msg(stdscr, help);
287 if (--video_mode < 0)
288 video_mode = MAX_ATTR;
291 if (video_mode > MAX_ATTR)
295 use_wide = !use_wide;
304 my_attrs = next_attr(video_mode);
307 our_content(current, &fg, &bg);
308 my_pair = find_pair(fg, bg);
315 ? next_color(current)
316 : make_color(current));
319 my_attrs = next_attr(current);
324 wch[0] = use_wide ? 0xff03 : '#';
326 setcchar(&temp, wch, my_attrs,
328 (use_init ? NULL : (void *) &my_pair));
330 * At the end of a page, move the cursor to the home position.
332 if ((add_wch(&temp) == ERR) && paged_mode) {
333 nodelay(stdscr, !single_mode);
336 total_cells += 1 + (use_wide ? 1 : 0);
342 printf("%.1f cells/second\n",
343 (double) (total_cells) / (double) (now() - start));
345 ExitProgram(EXIT_SUCCESS);
352 printf("This program requires the ncurses alloc_pair function\n");
353 ExitProgram(EXIT_FAILURE);