Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2018-2021,2022 Thomas E. Dickey * |
| 3 | * Copyright 2003-2014,2017 Free Software Foundation, Inc. * |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4 | * * |
| 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: * |
| 12 | * * |
| 13 | * The above copyright notice and this permission notice shall be included * |
| 14 | * in all copies or substantial portions of the Software. * |
| 15 | * * |
| 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. * |
| 23 | * * |
| 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 * |
| 27 | * authorization. * |
| 28 | ****************************************************************************/ |
| 29 | /* |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 30 | * $Id: background.c,v 1.24 2022/12/10 22:28:50 tom Exp $ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 31 | */ |
| 32 | |
| 33 | #define NEED_COLOR_CODE 1 |
| 34 | #define NEED_COLOR_NAME 1 |
| 35 | #include <color_name.h> |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 36 | #include <dump_window.h> |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 37 | |
| 38 | static int default_bg = COLOR_BLACK; |
| 39 | static int default_fg = COLOR_WHITE; |
| 40 | |
| 41 | static void |
| 42 | test_background(void) |
| 43 | { |
| 44 | NCURSES_COLOR_T f, b; |
| 45 | int row; |
| 46 | int chr; |
| 47 | |
| 48 | if (pair_content(0, &f, &b) == ERR) { |
| 49 | printw("pair 0 contains no data\n"); |
| 50 | } else { |
| 51 | printw("pair 0 contains (%d,%d)\n", (int) f, (int) b); |
| 52 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 53 | dump_window(stdscr); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 54 | |
| 55 | printw("Initializing pair 1 to red/%s\n", color_name(default_bg)); |
| 56 | init_pair(1, COLOR_RED, (NCURSES_COLOR_T) default_bg); |
| 57 | bkgdset((chtype) (' ' | COLOR_PAIR(1))); |
| 58 | printw("RED/BLACK\n"); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 59 | dump_window(stdscr); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 60 | |
| 61 | printw("Initializing pair 2 to %s/blue\n", color_name(default_fg)); |
| 62 | init_pair(2, (NCURSES_COLOR_T) default_fg, COLOR_BLUE); |
| 63 | bkgdset((chtype) (' ' | COLOR_PAIR(2))); |
| 64 | printw("This line should be %s/blue\n", color_name(default_fg)); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 65 | dump_window(stdscr); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 66 | |
| 67 | printw("Initializing pair 3 to %s/cyan (ACS_HLINE)\n", color_name(default_fg)); |
| 68 | init_pair(3, (NCURSES_COLOR_T) default_fg, COLOR_CYAN); |
| 69 | printw("...and drawing a box which should be followed by lines\n"); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 70 | bkgdset(ACS_HLINE | (chtype) COLOR_PAIR(3)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 71 | /* |
| 72 | * Characters from vt100 line-drawing should be mapped to line-drawing, |
| 73 | * since A_ALTCHARSET is set in the background, and the character part |
| 74 | * of the background is replaced by the nonblank characters written. |
| 75 | * |
| 76 | * Characters not in the line-drawing range are usually sent as-is. |
| 77 | * |
| 78 | * With SVr4 curses it is possible to rely on this to mix uppercase text |
| 79 | * with the (lowercase) line-drawing characters. ncurses uses some of |
| 80 | * the uppercase characters for encoding thick- and double-lines. |
| 81 | */ |
| 82 | row = 7; |
| 83 | mvprintw(row++, 10, "l"); |
| 84 | for (chr = 0; chr < 32; ++chr) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 85 | AddCh(' '); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 86 | printw("x\n"); |
| 87 | chr = 32; |
| 88 | while (chr < 128) { |
| 89 | if ((chr % 32) == 0) |
| 90 | mvprintw(row++, 10, "x"); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 91 | AddCh((chr == 127) ? ' ' : chr); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 92 | if ((++chr % 32) == 0) |
| 93 | printw("x\n"); |
| 94 | } |
| 95 | mvprintw(row++, 10, "m"); |
| 96 | for (chr = 0; chr < 32; ++chr) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 97 | AddCh(' '); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 98 | printw("j\n"); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 99 | dump_window(stdscr); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 100 | |
| 101 | bkgdset((chtype) (' ' | COLOR_PAIR(0))); |
| 102 | printw("Default Colors\n"); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 103 | dump_window(stdscr); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 104 | |
| 105 | printw("Resetting colors to pair 1\n"); |
| 106 | bkgdset((chtype) (' ' | COLOR_PAIR(1))); |
| 107 | printw("This line should be red/%s\n", color_name(default_bg)); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 108 | dump_window(stdscr); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 109 | |
| 110 | printw("Setting screen to pair 0\n"); |
| 111 | bkgd((chtype) (' ' | COLOR_PAIR(0))); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 112 | dump_window(stdscr); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 113 | |
| 114 | printw("Setting screen to pair 1\n"); |
| 115 | bkgd((chtype) (' ' | COLOR_PAIR(1))); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 116 | dump_window(stdscr); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 117 | |
| 118 | printw("Setting screen to pair 2\n"); |
| 119 | bkgd((chtype) (' ' | COLOR_PAIR(2))); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 120 | dump_window(stdscr); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 121 | |
| 122 | printw("Setting screen to pair 3\n"); |
| 123 | bkgd((chtype) (' ' | COLOR_PAIR(3))); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 124 | dump_window(stdscr); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 125 | |
| 126 | printw("Setting screen to pair 0\n"); |
| 127 | bkgd((chtype) (' ' | COLOR_PAIR(0))); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 128 | dump_window(stdscr); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 132 | usage(int ok) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 133 | { |
| 134 | static const char *msg[] = |
| 135 | { |
| 136 | "Usage: background [options]" |
| 137 | ,"" |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 138 | ,USAGE_COMMON |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 139 | ,"Options:" |
| 140 | #if HAVE_ASSUME_DEFAULT_COLORS |
| 141 | ," -a invoke assume_default_colors, repeat to use in init_pair" |
| 142 | #endif |
| 143 | ," -b XXX specify background color" |
| 144 | #if HAVE_USE_DEFAULT_COLORS |
| 145 | ," -d invoke use_default_colors, repeat to use in init_pair" |
| 146 | #endif |
| 147 | ," -f XXX specify foreground color" |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 148 | ," -l FILE log window-dumps to this file" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 149 | }; |
| 150 | size_t n; |
| 151 | |
| 152 | for (n = 0; n < SIZEOF(msg); n++) |
| 153 | fprintf(stderr, "%s\n", msg[n]); |
| 154 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 155 | ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 156 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 157 | /* *INDENT-OFF* */ |
| 158 | VERSION_COMMON() |
| 159 | /* *INDENT-ON* */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 160 | |
| 161 | int |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 162 | main(int argc, char *argv[]) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 163 | { |
| 164 | #if HAVE_ASSUME_DEFAULT_COLORS |
| 165 | int a_option = 0; |
| 166 | #endif |
| 167 | #if HAVE_USE_DEFAULT_COLORS |
| 168 | int d_option = 0; |
| 169 | #endif |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 170 | int ch; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 171 | |
| 172 | setlocale(LC_ALL, ""); |
| 173 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 174 | while ((ch = getopt(argc, argv, OPTS_COMMON "ab:df:l:")) != -1) { |
| 175 | switch (ch) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 176 | #if HAVE_ASSUME_DEFAULT_COLORS |
| 177 | case 'a': |
| 178 | ++a_option; |
| 179 | break; |
| 180 | #endif |
| 181 | case 'b': |
| 182 | default_bg = color_code(optarg); |
| 183 | break; |
| 184 | #if HAVE_USE_DEFAULT_COLORS |
| 185 | case 'd': |
| 186 | ++d_option; |
| 187 | break; |
| 188 | #endif |
| 189 | case 'f': |
| 190 | default_fg = color_code(optarg); |
| 191 | break; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 192 | case 'l': |
| 193 | if (!open_dump(optarg)) |
| 194 | usage(FALSE); |
| 195 | break; |
| 196 | case OPTS_VERSION: |
| 197 | show_version(argv); |
| 198 | ExitProgram(EXIT_SUCCESS); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 199 | default: |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 200 | usage(ch == OPTS_USAGE); |
| 201 | /* NOTREACHED */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | #if HAVE_USE_DEFAULT_COLORS && HAVE_ASSUME_DEFAULT_COLORS |
| 205 | if (a_option && d_option) { |
| 206 | fprintf(stderr, "Use either -a or -d option, but not both\n"); |
| 207 | ExitProgram(EXIT_FAILURE); |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | initscr(); |
| 212 | cbreak(); |
| 213 | noecho(); |
| 214 | |
| 215 | if (has_colors()) { |
| 216 | start_color(); |
| 217 | |
| 218 | #if HAVE_USE_DEFAULT_COLORS |
| 219 | if (d_option) { |
| 220 | printw("Using default colors...\n"); |
| 221 | use_default_colors(); |
| 222 | if (d_option > 1) { |
| 223 | default_fg = -1; |
| 224 | default_bg = -1; |
| 225 | } |
| 226 | } |
| 227 | #endif |
| 228 | #if HAVE_ASSUME_DEFAULT_COLORS |
| 229 | if (a_option) { |
| 230 | printw("Using assumed colors %s/%s...\n", |
| 231 | color_name(default_fg), |
| 232 | color_name(default_bg)); |
| 233 | assume_default_colors(default_fg, default_bg); |
| 234 | if (a_option > 1) { |
| 235 | default_fg = -1; |
| 236 | default_bg = -1; |
| 237 | } |
| 238 | } |
| 239 | #endif |
| 240 | |
| 241 | test_background(); |
| 242 | |
| 243 | } else { |
| 244 | printw("This demo requires a color terminal"); |
| 245 | getch(); |
| 246 | } |
| 247 | endwin(); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 248 | close_dump(); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 249 | ExitProgram(EXIT_SUCCESS); |
| 250 | } |