]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/echochar.c
ncurses 6.1 - patch 20180127
[ncurses.git] / test / echochar.c
1 /****************************************************************************
2  * Copyright (c) 2006-2014,2017 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  * $Id: echochar.c,v 1.18 2017/10/18 23:04:52 tom Exp $
30  *
31  * Demonstrate the echochar function (compare to dots.c).
32  * Thomas Dickey - 2006/11/4
33  */
34
35 #include <test.priv.h>
36
37 #include <time.h>
38
39 static bool interrupted = FALSE;
40 static long total_chars = 0;
41 static time_t started;
42
43 static void
44 cleanup(void)
45 {
46     exit_curses();
47
48     printf("\n\n%ld total cells, rate %.2f/sec\n",
49            total_chars,
50            ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
51 }
52
53 static void
54 onsig(int n GCC_UNUSED)
55 {
56     interrupted = TRUE;
57 }
58
59 static double
60 ranf(void)
61 {
62     long r = (rand() & 077777);
63     return ((double) r / 32768.);
64 }
65
66 static void
67 set_color(char *my_pairs, int fg, int bg)
68 {
69     int pair = (fg * COLORS) + bg;
70     if (pair < COLOR_PAIRS) {
71         if (!my_pairs[pair]) {
72             init_pair((short) pair,
73                       (short) fg,
74                       (short) bg);
75         }
76         attron(COLOR_PAIR(pair));
77     }
78 }
79
80 int
81 main(int argc GCC_UNUSED,
82      char *argv[]GCC_UNUSED)
83 {
84     int ch, x, y, z, p;
85     double r;
86     double c;
87     bool use_colors;
88     bool opt_r = FALSE;
89     char *my_pairs = 0;
90     int last_fg = 0;
91     int last_bg = 0;
92
93     while ((ch = getopt(argc, argv, "r")) != -1) {
94         switch (ch) {
95         case 'r':
96             opt_r = TRUE;
97             break;
98         default:
99             fprintf(stderr, "Usage: echochar [-r]\n");
100             ExitProgram(EXIT_FAILURE);
101         }
102     }
103
104     InitAndCatch(initscr(), onsig);
105
106     use_colors = has_colors();
107     if (use_colors) {
108         start_color();
109         if (COLOR_PAIRS > 0) {
110             my_pairs = typeCalloc(char, (size_t) COLOR_PAIRS);
111         }
112         use_colors = (my_pairs != 0);
113     }
114
115     srand((unsigned) time(0));
116
117     curs_set(0);
118
119     r = (double) (LINES - 4);
120     c = (double) (COLS - 4);
121     started = time((time_t *) 0);
122
123     while (!interrupted) {
124         x = (int) (c * ranf()) + 2;
125         y = (int) (r * ranf()) + 2;
126         p = (ranf() > 0.9) ? '*' : ' ';
127
128         move(y, x);
129         if (use_colors > 0) {
130             z = (int) (ranf() * COLORS);
131             if (ranf() > 0.01) {
132                 set_color(my_pairs, z, last_bg);
133                 last_fg = z;
134             } else {
135                 set_color(my_pairs, last_fg, z);
136                 last_bg = z;
137                 napms(1);
138             }
139         } else {
140             if (ranf() <= 0.01) {
141                 if (ranf() > 0.6)
142                     attron(A_REVERSE);
143                 else
144                     attroff(A_REVERSE);
145                 napms(1);
146             }
147         }
148         if (opt_r) {
149             AddCh(UChar(p));
150             refresh();
151         } else {
152             echochar(UChar(p));
153         }
154         ++total_chars;
155     }
156     cleanup();
157     free(my_pairs);
158     ExitProgram(EXIT_SUCCESS);
159 }