Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Copyright (c) 2013 Free Software Foundation, Inc. * |
| 3 | * * |
| 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: * |
| 11 | * * |
| 12 | * The above copyright notice and this permission notice shall be included * |
| 13 | * in all copies or substantial portions of the Software. * |
| 14 | * * |
| 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. * |
| 22 | * * |
| 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 * |
| 26 | * authorization. * |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | /* |
| 30 | * Author: Thomas E. Dickey |
| 31 | * |
| 32 | * $Id: dots_termcap.c,v 1.8 2014/09/25 09:00:56 tom Exp $ |
| 33 | * |
| 34 | * A simple demo of the termcap interface. |
| 35 | */ |
| 36 | #define USE_TINFO |
| 37 | #include <test.priv.h> |
| 38 | |
| 39 | #if !defined(__MINGW32__) |
| 40 | #include <sys/time.h> |
| 41 | #endif |
| 42 | |
| 43 | #if HAVE_TGETENT |
| 44 | |
| 45 | #include <time.h> |
| 46 | |
| 47 | #define valid(s) ((s != 0) && s != (char *)-1) |
| 48 | |
| 49 | static bool interrupted = FALSE; |
| 50 | static long total_chars = 0; |
| 51 | static time_t started; |
| 52 | |
| 53 | static char *t_AB; |
| 54 | static char *t_AF; |
| 55 | static char *t_cl; |
| 56 | static char *t_cm; |
| 57 | static char *t_me; |
| 58 | static char *t_mr; |
| 59 | static char *t_oc; |
| 60 | static char *t_op; |
| 61 | static char *t_ve; |
| 62 | static char *t_vi; |
| 63 | |
| 64 | static struct { |
| 65 | const char *name; |
| 66 | char **value; |
| 67 | } my_caps[] = { |
| 68 | |
| 69 | { |
| 70 | "AB", &t_AB |
| 71 | }, |
| 72 | { |
| 73 | "AF", &t_AF |
| 74 | }, |
| 75 | { |
| 76 | "cl", &t_cl |
| 77 | }, |
| 78 | { |
| 79 | "cm", &t_cm |
| 80 | }, |
| 81 | { |
| 82 | "me", &t_me |
| 83 | }, |
| 84 | { |
| 85 | "mr", &t_mr |
| 86 | }, |
| 87 | { |
| 88 | "oc", &t_oc |
| 89 | }, |
| 90 | { |
| 91 | "op", &t_op |
| 92 | }, |
| 93 | { |
| 94 | "ve", &t_ve |
| 95 | }, |
| 96 | { |
| 97 | "vi", &t_vi |
| 98 | }, |
| 99 | }; |
| 100 | |
| 101 | static |
| 102 | TPUTS_PROTO(outc, c) |
| 103 | { |
| 104 | int rc = c; |
| 105 | |
| 106 | if (interrupted) { |
| 107 | char tmp = (char) c; |
| 108 | if (write(STDOUT_FILENO, &tmp, (size_t) 1) == -1) |
| 109 | rc = EOF; |
| 110 | } else { |
| 111 | rc = putc(c, stdout); |
| 112 | } |
| 113 | TPUTS_RETURN(rc); |
| 114 | } |
| 115 | |
| 116 | static bool |
| 117 | outs(char *s) |
| 118 | { |
| 119 | if (valid(s)) { |
| 120 | tputs(s, 1, outc); |
| 121 | return TRUE; |
| 122 | } |
| 123 | return FALSE; |
| 124 | } |
| 125 | |
| 126 | static void |
| 127 | cleanup(void) |
| 128 | { |
| 129 | outs(t_me); |
| 130 | if (!outs(t_oc)) |
| 131 | outs(t_op); |
| 132 | outs(t_cl); |
| 133 | outs(t_ve); |
| 134 | |
| 135 | printf("\n\n%ld total chars, rate %.2f/sec\n", |
| 136 | total_chars, |
| 137 | ((double) (total_chars) / (double) (time((time_t *) 0) - started))); |
| 138 | } |
| 139 | |
| 140 | static void |
| 141 | onsig(int n GCC_UNUSED) |
| 142 | { |
| 143 | interrupted = TRUE; |
| 144 | } |
| 145 | |
| 146 | static double |
| 147 | ranf(void) |
| 148 | { |
| 149 | long r = (rand() & 077777); |
| 150 | return ((double) r / 32768.); |
| 151 | } |
| 152 | |
| 153 | static void |
| 154 | my_napms(int ms) |
| 155 | { |
| 156 | #if defined(__MINGW32__) || !HAVE_GETTIMEOFDAY |
| 157 | Sleep((DWORD) ms); |
| 158 | #else |
| 159 | struct timeval data; |
| 160 | data.tv_sec = 0; |
| 161 | data.tv_usec = ms * 1000; |
| 162 | select(0, NULL, NULL, NULL, &data); |
| 163 | #endif |
| 164 | } |
| 165 | |
| 166 | int |
| 167 | main(int argc GCC_UNUSED, |
| 168 | char *argv[]GCC_UNUSED) |
| 169 | { |
| 170 | int x, y, z, p; |
| 171 | int num_colors; |
| 172 | int num_lines; |
| 173 | int num_columns; |
| 174 | double r; |
| 175 | double c; |
| 176 | char buffer[1024]; |
| 177 | char area[1024]; |
| 178 | char *name; |
| 179 | |
| 180 | CATCHALL(onsig); |
| 181 | |
| 182 | srand((unsigned) time(0)); |
| 183 | |
| 184 | if ((name = getenv("TERM")) == 0) { |
| 185 | fprintf(stderr, "TERM is not set\n"); |
| 186 | ExitProgram(EXIT_FAILURE); |
| 187 | } else if (tgetent(buffer, name) < 0) { |
| 188 | fprintf(stderr, "terminal description not found\n"); |
| 189 | ExitProgram(EXIT_FAILURE); |
| 190 | } else { |
| 191 | size_t t; |
| 192 | char *ap = area; |
| 193 | for (t = 0; t < SIZEOF(my_caps); ++t) { |
| 194 | *(my_caps[t].value) = tgetstr((NCURSES_CONST char *) |
| 195 | my_caps[t].name, &ap); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | num_colors = tgetnum("Co"); |
| 200 | num_lines = tgetnum("li"); |
| 201 | num_columns = tgetnum("co"); |
| 202 | |
| 203 | outs(t_cl); |
| 204 | outs(t_vi); |
| 205 | if (num_colors > 1) { |
| 206 | if (!valid(t_AF) |
| 207 | || !valid(t_AB) |
| 208 | || (!valid(t_oc) && !valid(t_op))) |
| 209 | num_colors = -1; |
| 210 | } |
| 211 | |
| 212 | r = (double) (num_lines - 4); |
| 213 | c = (double) (num_columns - 4); |
| 214 | started = time((time_t *) 0); |
| 215 | |
| 216 | while (!interrupted) { |
| 217 | x = (int) (c * ranf()) + 2; |
| 218 | y = (int) (r * ranf()) + 2; |
| 219 | p = (ranf() > 0.9) ? '*' : ' '; |
| 220 | |
| 221 | tputs(tgoto(t_cm, x, y), 1, outc); |
| 222 | if (num_colors > 0) { |
| 223 | z = (int) (ranf() * num_colors); |
| 224 | if (ranf() > 0.01) { |
| 225 | tputs(tgoto(t_AF, 0, z), 1, outc); |
| 226 | } else { |
| 227 | tputs(tgoto(t_AB, 0, z), 1, outc); |
| 228 | my_napms(1); |
| 229 | } |
| 230 | } else if (valid(t_me) |
| 231 | && valid(t_mr)) { |
| 232 | if (ranf() <= 0.01) { |
| 233 | outs((ranf() > 0.6) |
| 234 | ? t_mr |
| 235 | : t_me); |
| 236 | my_napms(1); |
| 237 | } |
| 238 | } |
| 239 | outc(p); |
| 240 | fflush(stdout); |
| 241 | ++total_chars; |
| 242 | } |
| 243 | cleanup(); |
| 244 | ExitProgram(EXIT_SUCCESS); |
| 245 | } |
| 246 | #else |
| 247 | int |
| 248 | main(int argc GCC_UNUSED, |
| 249 | char *argv[]GCC_UNUSED) |
| 250 | { |
| 251 | fprintf(stderr, "This program requires termcap\n"); |
| 252 | exit(EXIT_FAILURE); |
| 253 | } |
| 254 | #endif |