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-2022,2023 Thomas E. Dickey * |
| 3 | * Copyright 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 | |
| 30 | /* |
| 31 | * Author: Thomas E. Dickey |
| 32 | * |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 33 | * $Id: dots_curses.c,v 1.25 2023/01/07 17:21:48 tom Exp $ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 34 | * |
| 35 | * A simple demo of the curses interface used for comparison with termcap. |
| 36 | */ |
| 37 | #include <test.priv.h> |
| 38 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 39 | #if !defined(_NC_WINDOWS) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 40 | #include <sys/time.h> |
| 41 | #endif |
| 42 | |
| 43 | #include <time.h> |
| 44 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 45 | static bool interrupted = FALSE; |
| 46 | static long total_chars = 0; |
| 47 | static time_t started; |
| 48 | |
| 49 | static void |
| 50 | cleanup(void) |
| 51 | { |
| 52 | endwin(); |
| 53 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 54 | fflush(stdout); |
| 55 | fprintf(stderr, "\n\n%ld total cells, rate %.2f/sec\n", |
| 56 | total_chars, |
| 57 | ((double) (total_chars) / (double) (time((time_t *) 0) - started))); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static void |
| 61 | onsig(int n GCC_UNUSED) |
| 62 | { |
| 63 | interrupted = TRUE; |
| 64 | } |
| 65 | |
| 66 | static double |
| 67 | ranf(void) |
| 68 | { |
| 69 | long r = (rand() & 077777); |
| 70 | return ((double) r / 32768.); |
| 71 | } |
| 72 | |
| 73 | static int |
| 74 | mypair(int fg, int bg) |
| 75 | { |
| 76 | int pair = (fg * COLORS) + bg; |
| 77 | return (pair >= COLOR_PAIRS) ? -1 : pair; |
| 78 | } |
| 79 | |
| 80 | static void |
| 81 | set_colors(int fg, int bg) |
| 82 | { |
| 83 | int pair = mypair(fg, bg); |
| 84 | if (pair > 0) { |
| 85 | attron(COLOR_PAIR(mypair(fg, bg))); |
| 86 | } |
| 87 | } |
| 88 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 89 | static void |
| 90 | usage(int ok) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 91 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 92 | static const char *msg[] = |
| 93 | { |
| 94 | "Usage: dots_curses [options]" |
| 95 | ,"" |
| 96 | ,USAGE_COMMON |
| 97 | ,"Options:" |
| 98 | ," -T TERM override $TERM" |
| 99 | #if HAVE_USE_DEFAULT_COLORS |
| 100 | ," -d invoke use_default_colors()" |
| 101 | #endif |
| 102 | #if HAVE_USE_ENV |
| 103 | ," -e allow environment $LINES / $COLUMNS" |
| 104 | #endif |
| 105 | ," -m SIZE set margin (default: 2)" |
| 106 | ," -r SECS self-interrupt/exit after specified number of seconds" |
| 107 | ," -s MSECS delay 1% of the time (default: 1 msecs)" |
| 108 | }; |
| 109 | size_t n; |
| 110 | |
| 111 | for (n = 0; n < SIZEOF(msg); n++) |
| 112 | fprintf(stderr, "%s\n", msg[n]); |
| 113 | |
| 114 | ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 115 | } |
| 116 | /* *INDENT-OFF* */ |
| 117 | VERSION_COMMON() |
| 118 | /* *INDENT-ON* */ |
| 119 | |
| 120 | int |
| 121 | main(int argc, char *argv[]) |
| 122 | { |
| 123 | int ch; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 124 | int fg, bg; |
| 125 | double r; |
| 126 | double c; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 127 | #if HAVE_USE_DEFAULT_COLORS |
| 128 | bool d_option = FALSE; |
| 129 | #endif |
| 130 | int m_option = 2; |
| 131 | int r_option = 0; |
| 132 | int s_option = 1; |
| 133 | size_t need; |
| 134 | char *my_env; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 135 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 136 | while ((ch = getopt(argc, argv, OPTS_COMMON "T:dem:r:s:")) != -1) { |
| 137 | switch (ch) { |
| 138 | case 'T': |
| 139 | need = 6 + strlen(optarg); |
| 140 | if ((my_env = malloc(need)) != NULL) { |
| 141 | _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg); |
| 142 | putenv(my_env); |
| 143 | } |
| 144 | break; |
| 145 | #if HAVE_USE_DEFAULT_COLORS |
| 146 | case 'd': |
| 147 | d_option = TRUE; |
| 148 | break; |
| 149 | #endif |
| 150 | #if HAVE_USE_ENV |
| 151 | case 'e': |
| 152 | use_env(TRUE); |
| 153 | break; |
| 154 | #endif |
| 155 | case 'm': |
| 156 | m_option = atoi(optarg); |
| 157 | break; |
| 158 | case 'r': |
| 159 | r_option = atoi(optarg); |
| 160 | break; |
| 161 | case 's': |
| 162 | s_option = atoi(optarg); |
| 163 | break; |
| 164 | case OPTS_VERSION: |
| 165 | show_version(argv); |
| 166 | ExitProgram(EXIT_SUCCESS); |
| 167 | default: |
| 168 | usage(ch == OPTS_USAGE); |
| 169 | /* NOTREACHED */ |
| 170 | } |
| 171 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 172 | |
| 173 | srand((unsigned) time(0)); |
| 174 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 175 | SetupAlarm(r_option); |
| 176 | InitAndCatch(initscr(), onsig); |
| 177 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 178 | if (has_colors()) { |
| 179 | start_color(); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 180 | #if HAVE_USE_DEFAULT_COLORS |
| 181 | if (d_option) |
| 182 | use_default_colors(); |
| 183 | #endif |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 184 | for (fg = 0; fg < COLORS; fg++) { |
| 185 | for (bg = 0; bg < COLORS; bg++) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 186 | int pair; |
| 187 | if (interrupted) { |
| 188 | cleanup(); |
| 189 | ExitProgram(EXIT_FAILURE); |
| 190 | } |
| 191 | pair = mypair(fg, bg); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 192 | if (pair > 0) |
| 193 | init_pair((short) pair, (short) fg, (short) bg); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 198 | r = (double) (LINES - (m_option * 2)); |
| 199 | c = (double) (COLS - (m_option * 2)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 200 | started = time((time_t *) 0); |
| 201 | |
| 202 | fg = COLOR_WHITE; |
| 203 | bg = COLOR_BLACK; |
| 204 | while (!interrupted) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 205 | int x = (int) (c * ranf()) + m_option; |
| 206 | int y = (int) (r * ranf()) + m_option; |
| 207 | int p = (ranf() > 0.9) ? '*' : ' '; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 208 | |
| 209 | move(y, x); |
| 210 | if (has_colors()) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 211 | int z = (int) (ranf() * COLORS); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 212 | if (ranf() > 0.01) { |
| 213 | set_colors(fg = z, bg); |
| 214 | attron(COLOR_PAIR(mypair(fg, bg))); |
| 215 | } else { |
| 216 | set_colors(fg, bg = z); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 217 | if (s_option) |
| 218 | napms(s_option); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 219 | } |
| 220 | } else { |
| 221 | if (ranf() <= 0.01) { |
| 222 | if (ranf() > 0.6) { |
| 223 | attron(A_REVERSE); |
| 224 | } else { |
| 225 | attroff(A_REVERSE); |
| 226 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 227 | if (s_option) |
| 228 | napms(s_option); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 229 | } |
| 230 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 231 | AddCh(p); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 232 | refresh(); |
| 233 | ++total_chars; |
| 234 | } |
| 235 | cleanup(); |
| 236 | ExitProgram(EXIT_SUCCESS); |
| 237 | } |