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