1 /****************************************************************************
2 * Copyright 2021 Thomas E. Dickey *
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: back_ground.c,v 1.3 2021/02/13 21:07:58 tom Exp $
32 #define NEED_COLOR_CODE 1
33 #define NEED_COLOR_NAME 1
34 #include <color_name.h>
35 #include <dump_window.h>
39 static int default_bg = COLOR_BLACK;
40 static int default_fg = COLOR_WHITE;
41 static wchar_t wide_fill = L' ';
44 decode_wchar(const char *value)
50 if (!strncmp(value, "U+", 2)) {
54 result = strtol(value, &next, radix);
55 if (next == value || (next == NULL || *next != '\0')) {
56 fprintf(stderr, "decoding wchar_t: %s\n", value);
72 if (pair_content(0, &f, &b) == ERR) {
73 printw("pair 0 contains no data\n");
75 printw("pair 0 contains (%d,%d)\n", (int) f, (int) b);
82 printw("Initializing pair 1 to red/%s\n", color_name(default_bg));
83 init_pair(1, COLOR_RED, (NCURSES_COLOR_T) default_bg);
84 setcchar(&data, blank, A_NORMAL, 1, NULL);
86 printw("RED/BLACK\n");
89 printw("Initializing pair 2 to %s/blue\n", color_name(default_fg));
90 init_pair(2, (NCURSES_COLOR_T) default_fg, COLOR_BLUE);
91 setcchar(&data, blank, A_NORMAL, 2, NULL);
93 printw("This line should be %s/blue\n", color_name(default_fg));
96 printw("Initializing pair 3 to %s/cyan (ACS_HLINE)\n", color_name(default_fg));
97 init_pair(3, (NCURSES_COLOR_T) default_fg, COLOR_CYAN);
98 printw("...and drawing a box which should be followed by lines\n");
99 graphics[0] = ACS_HLINE & A_CHARTEXT;
101 setcchar(&data, graphics, A_ALTCHARSET, 3, NULL);
104 * Characters from vt100 line-drawing should be mapped to line-drawing,
105 * since A_ALTCHARSET is set in the background, and the character part
106 * of the background is replaced by the nonblank characters written.
108 * Characters not in the line-drawing range are usually sent as-is.
110 * With SVr4 curses it is possible to rely on this to mix uppercase text
111 * with the (lowercase) line-drawing characters. ncurses uses some of
112 * the uppercase characters for encoding thick- and double-lines.
115 mvprintw(row++, 10, "l");
116 for (chr = 0; chr < 32; ++chr)
122 mvprintw(row++, 10, "x");
123 AddCh((chr == 127) ? ' ' : chr);
124 if ((++chr % 32) == 0)
127 mvprintw(row++, 10, "m");
128 for (chr = 0; chr < 32; ++chr)
133 setcchar(&data, blank, A_NORMAL, 0, NULL);
135 printw("Default Colors\n");
138 printw("Resetting colors to pair 1\n");
139 setcchar(&data, blank, A_NORMAL, 1, NULL);
141 printw("This line should be red/%s\n", color_name(default_bg));
144 printw("Setting screen to pair 0\n");
145 setcchar(&data, blank, A_NORMAL, 0, NULL);
149 printw("Setting screen to pair 1\n");
150 setcchar(&data, blank, A_NORMAL, 1, NULL);
154 printw("Setting screen to pair 2\n");
155 setcchar(&data, blank, A_NORMAL, 2, NULL);
159 printw("Setting screen to pair 3\n");
160 setcchar(&data, blank, A_NORMAL, 3, NULL);
164 printw("Setting screen to pair 0\n");
165 setcchar(&data, blank, A_NORMAL, 0, NULL);
173 static const char *msg[] =
175 "Usage: background [options]"
178 #if HAVE_ASSUME_DEFAULT_COLORS
179 ," -a invoke assume_default_colors, repeat to use in init_pair"
181 ," -b XXX specify background color"
182 #if HAVE_USE_DEFAULT_COLORS
183 ," -d invoke use_default_colors, repeat to use in init_pair"
185 ," -f XXX specify foreground color"
186 ," -l FILE log window-dumps to this file"
187 ," -w fill background with stipple pattern"
188 ," -W CODE fill background with this Unicode value"
192 for (n = 0; n < SIZEOF(msg); n++)
193 fprintf(stderr, "%s\n", msg[n]);
195 ExitProgram(EXIT_FAILURE);
199 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
201 #if HAVE_ASSUME_DEFAULT_COLORS
204 #if HAVE_USE_DEFAULT_COLORS
209 setlocale(LC_ALL, "");
211 while ((n = getopt(argc, argv, "ab:df:l:wW:")) != -1) {
213 #if HAVE_ASSUME_DEFAULT_COLORS
219 default_bg = color_code(optarg);
221 #if HAVE_USE_DEFAULT_COLORS
227 default_fg = color_code(optarg);
230 if (!open_dump(optarg))
234 wide_fill = L'\u2591';
237 wide_fill = decode_wchar(optarg);
243 #if HAVE_USE_DEFAULT_COLORS && HAVE_ASSUME_DEFAULT_COLORS
244 if (a_option && d_option) {
245 fprintf(stderr, "Use either -a or -d option, but not both\n");
246 ExitProgram(EXIT_FAILURE);
257 #if HAVE_USE_DEFAULT_COLORS
259 printw("Using default colors...\n");
260 use_default_colors();
267 #if HAVE_ASSUME_DEFAULT_COLORS
269 printw("Using assumed colors %s/%s...\n",
270 color_name(default_fg),
271 color_name(default_bg));
272 assume_default_colors(default_fg, default_bg);
283 printw("This demo requires a color terminal");
288 ExitProgram(EXIT_SUCCESS);
295 printf("This program requires the wide-curses library\n");
296 ExitProgram(EXIT_FAILURE);
298 #endif /* USE_WIDEC_SUPPORT */