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 2019-2022,2023 Thomas E. Dickey * |
| 3 | * Copyright 2006-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: echochar.c,v 1.26 2023/05/27 20:13:10 tom Exp $ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 31 | * |
| 32 | * Demonstrate the echochar function (compare to dots.c). |
| 33 | * Thomas Dickey - 2006/11/4 |
| 34 | */ |
| 35 | |
| 36 | #include <test.priv.h> |
| 37 | |
| 38 | #include <time.h> |
| 39 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 40 | static bool interrupted = FALSE; |
| 41 | static long total_chars = 0; |
| 42 | static time_t started; |
| 43 | |
| 44 | static void |
| 45 | cleanup(void) |
| 46 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 47 | stop_curses(); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 48 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 49 | printf("\n\n%ld total cells, rate %.2f/sec\n", |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 50 | total_chars, |
| 51 | ((double) (total_chars) / (double) (time((time_t *) 0) - started))); |
| 52 | } |
| 53 | |
| 54 | static void |
| 55 | onsig(int n GCC_UNUSED) |
| 56 | { |
| 57 | interrupted = TRUE; |
| 58 | } |
| 59 | |
| 60 | static double |
| 61 | ranf(void) |
| 62 | { |
| 63 | long r = (rand() & 077777); |
| 64 | return ((double) r / 32768.); |
| 65 | } |
| 66 | |
| 67 | static void |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 68 | set_color(const char *const my_pairs, int fg, int bg) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 69 | { |
| 70 | int pair = (fg * COLORS) + bg; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 71 | if (pair < COLOR_PAIRS) { |
| 72 | if (!my_pairs[pair]) { |
| 73 | init_pair((short) pair, |
| 74 | (short) fg, |
| 75 | (short) bg); |
| 76 | } |
| 77 | attron(COLOR_PAIR(pair)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 78 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 79 | } |
| 80 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 81 | static void |
| 82 | usage(int ok) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 83 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 84 | static const char *msg[] = |
| 85 | { |
| 86 | "Usage: echochar" |
| 87 | ,"" |
| 88 | ,USAGE_COMMON |
| 89 | ,"Options:" |
| 90 | ," -r use addch/refresh rather than echochar()" |
| 91 | }; |
| 92 | size_t n; |
| 93 | |
| 94 | for (n = 0; n < SIZEOF(msg); n++) |
| 95 | fprintf(stderr, "%s\n", msg[n]); |
| 96 | |
| 97 | ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); |
| 98 | } |
| 99 | /* *INDENT-OFF* */ |
| 100 | VERSION_COMMON() |
| 101 | /* *INDENT-ON* */ |
| 102 | |
| 103 | int |
| 104 | main(int argc, char *argv[]) |
| 105 | { |
| 106 | int ch; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 107 | double r; |
| 108 | double c; |
| 109 | bool use_colors; |
| 110 | bool opt_r = FALSE; |
| 111 | char *my_pairs = 0; |
| 112 | int last_fg = 0; |
| 113 | int last_bg = 0; |
| 114 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 115 | while ((ch = getopt(argc, argv, OPTS_COMMON "r")) != -1) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 116 | switch (ch) { |
| 117 | case 'r': |
| 118 | opt_r = TRUE; |
| 119 | break; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 120 | case OPTS_VERSION: |
| 121 | show_version(argv); |
| 122 | ExitProgram(EXIT_SUCCESS); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 123 | default: |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 124 | usage(ch == OPTS_USAGE); |
| 125 | /* NOTREACHED */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 129 | InitAndCatch(initscr(), onsig); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 130 | |
| 131 | use_colors = has_colors(); |
| 132 | if (use_colors) { |
| 133 | start_color(); |
| 134 | if (COLOR_PAIRS > 0) { |
| 135 | my_pairs = typeCalloc(char, (size_t) COLOR_PAIRS); |
| 136 | } |
| 137 | use_colors = (my_pairs != 0); |
| 138 | } |
| 139 | |
| 140 | srand((unsigned) time(0)); |
| 141 | |
| 142 | curs_set(0); |
| 143 | |
| 144 | r = (double) (LINES - 4); |
| 145 | c = (double) (COLS - 4); |
| 146 | started = time((time_t *) 0); |
| 147 | |
| 148 | while (!interrupted) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 149 | int x = (int) (c * ranf()) + 2; |
| 150 | int y = (int) (r * ranf()) + 2; |
| 151 | int p = (ranf() > 0.9) ? '*' : ' '; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 152 | |
| 153 | move(y, x); |
| 154 | if (use_colors > 0) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 155 | int z = (int) (ranf() * COLORS); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 156 | if (ranf() > 0.01) { |
| 157 | set_color(my_pairs, z, last_bg); |
| 158 | last_fg = z; |
| 159 | } else { |
| 160 | set_color(my_pairs, last_fg, z); |
| 161 | last_bg = z; |
| 162 | napms(1); |
| 163 | } |
| 164 | } else { |
| 165 | if (ranf() <= 0.01) { |
| 166 | if (ranf() > 0.6) |
| 167 | attron(A_REVERSE); |
| 168 | else |
| 169 | attroff(A_REVERSE); |
| 170 | napms(1); |
| 171 | } |
| 172 | } |
| 173 | if (opt_r) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 174 | AddCh(UChar(p)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 175 | refresh(); |
| 176 | } else { |
| 177 | echochar(UChar(p)); |
| 178 | } |
| 179 | ++total_chars; |
| 180 | } |
| 181 | cleanup(); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 182 | free(my_pairs); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 183 | ExitProgram(EXIT_SUCCESS); |
| 184 | } |