1 /****************************************************************************
2 * Copyright (c) 2017 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.11 2017/04/08 21:48:53 tom Exp $
31 * Demonstrate the alloc_pair() function.
34 #include <test.priv.h>
37 #if HAVE_ALLOC_PAIR && USE_WIDEC_SUPPORT
39 #include <sys/types.h>
43 #define MAX_BITS 8 /* all but A_ALTCHARSET */
44 #define MAX_ATTR ((1<<MAX_BITS)-1)
47 valid_cap(NCURSES_CONST char *name)
49 char *value = tigetstr(name);
50 return (value != 0 && value != (char *) -1) ? TRUE : FALSE;
56 static bool init = FALSE;
57 static attr_t table[MAX_BITS * MAX_BITS];
62 attr_t bits[MAX_BITS];
65 bits[limit++] = A_NORMAL;
66 if (valid_cap("smso"))
67 bits[limit++] = A_STANDOUT;
68 if (valid_cap("smul"))
69 bits[limit++] = A_UNDERLINE;
71 bits[limit++] = A_REVERSE;
72 if (valid_cap("blink"))
73 bits[limit++] = A_BLINK;
75 bits[limit++] = A_DIM;
76 if (valid_cap("bold"))
77 bits[limit++] = A_BOLD;
78 for (j = 0; j < limit; ++j) {
79 for (k = 0; k < limit; ++k) {
80 table[j * limit + k] = bits[j] | bits[k];
84 return table[now % limit];
88 our_content(int pair, int *fg, int *bg)
91 *fg = (pair / COLORS) % COLORS;
92 *bg = (pair % COLORS);
99 our_content(now, &fg, &bg);
100 return alloc_pair(fg, bg);
107 if ((short) now > 0) {
108 if (now < COLOR_PAIRS) {
110 our_content(now, &fg, &bg);
111 if (init_pair((short) now, (short) fg, (short) bg) != OK)
122 show_help(const char **help)
124 WINDOW *mywin = newwin(LINES, COLS, 0, 0);
128 for (n = 0; help[n] != 0; ++n) {
129 wmove(mywin, 1 + n, 2);
130 wprintw(mywin, "%.*s\n", COLS - 4, help[n]);
142 return time((time_t *) 0);
148 static const char *msg[] =
150 "Usage: demo_new_pair [options]",
152 "Repeatedly print using all possible color combinations.",
155 " -i use init_pair rather than alloc_pair",
156 " -p start in paged-mode",
157 " -s start in single-step mode",
158 " -w print a wide-character cell",
161 for (n = 0; n < SIZEOF(msg); ++n) {
162 fprintf(stderr, "%s\n", msg[n]);
164 ExitProgram(EXIT_FAILURE);
167 #define use_pages() \
168 paged_mode = TRUE, single_mode = TRUE
170 #define use_single() \
171 paged_mode = FALSE, single_mode = TRUE
173 #define update_modes() \
174 scrollok(stdscr, !paged_mode); \
175 nodelay(stdscr, !single_mode || paged_mode)
178 main(int argc, char *argv[])
180 static const char *help[] =
182 "This program iterates over the possible color combinations,",
183 "allocating or initializing color pairs. For best results,",
184 "choose screen-width dividing evenly into the number of colors,",
187 " 32x64,32x128 256 colors",
188 " 24x44,24x88 88 colors",
189 " 32x64,24x128 16 colors",
192 " c toggle between coloring and de-coloring cells",
193 " p show one page at a time",
194 " s show one character at a time",
195 " <space> display char/page without pausing",
196 " v/V cycle through video attributes",
197 " w toggle between \"#\" and a double-width equivalent",
198 " ? print this screen (exit on any character).",
200 "To exit this program, press ^Q, ^[ or \"q\".",
205 bool clobber = FALSE;
206 bool hascolor = FALSE;
207 bool use_init = FALSE;
208 bool use_wide = FALSE;
209 bool paged_mode = FALSE;
210 bool single_mode = FALSE;
215 time_t start = now();
216 long total_cells = 0;
219 setlocale(LC_ALL, "");
221 while ((ch = getopt(argc, argv, "ipsw")) != -1) {
241 if (isatty(fileno(stderr))) {
243 } else if ((ch = open("/dev/tty", O_WRONLY)) != 0) {
244 output = fdopen(ch, "w");
246 fprintf(stderr, "cannot open terminal for output\n");
247 ExitProgram(EXIT_FAILURE);
249 if (newterm(NULL, output, stdin) == 0)
251 (void) cbreak(); /* read chars without wait for \n */
252 (void) noecho(); /* don't echo input */
256 keypad(stdscr, TRUE);
258 if ((hascolor = has_colors())) {
266 * Repeatedly cycle through all colors, initializing pairs as needed.
267 * Provide for single-stepping, or page-at-a-time, as well as quitting.
301 if (--video_mode < 0)
302 video_mode = MAX_ATTR;
305 if (video_mode > MAX_ATTR)
309 use_wide = !use_wide;
318 my_attrs = next_attr(video_mode);
321 our_content(current, &fg, &bg);
322 my_pair = find_pair(fg, bg);
329 ? next_color(current)
330 : make_color(current));
333 my_attrs = next_attr(current);
338 wch[0] = use_wide ? 0xff03 : '#';
340 setcchar(&temp, wch, my_attrs, (short) my_pair, NULL);
342 * At the end of a page, move the cursor to the home position.
344 if ((add_wch(&temp) == ERR) && paged_mode) {
345 nodelay(stdscr, !single_mode);
348 total_cells += 1 + (use_wide ? 1 : 0);
353 printf("%.1f cells/second\n",
354 (double) (total_cells) / (double) (now() - start));
356 ExitProgram(EXIT_SUCCESS);
363 printf("This program requires the ncurses alloc_pair function\n");
364 ExitProgram(EXIT_FAILURE);