]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/echochar.c
ncurses 5.9 - patch 20131207
[ncurses.git] / test / echochar.c
1 /****************************************************************************
2  * Copyright (c) 2006-2010,2012 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.9 2012/06/09 20:30:32 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     endwin();
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 (!my_pairs[pair]) {
73         init_pair((short) pair,
74                   (short) fg,
75                   (short) bg);
76     }
77     attron((attr_t) COLOR_PAIR(pair));
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     CATCHALL(onsig);
105     initscr();
106
107     use_colors = has_colors();
108     if (use_colors) {
109         start_color();
110         if (COLOR_PAIRS > 0) {
111             my_pairs = typeCalloc(char, (size_t) COLOR_PAIRS);
112         }
113         use_colors = (my_pairs != 0);
114     }
115
116     srand((unsigned) time(0));
117
118     curs_set(0);
119
120     r = (double) (LINES - 4);
121     c = (double) (COLS - 4);
122     started = time((time_t *) 0);
123
124     while (!interrupted) {
125         x = (int) (c * ranf()) + 2;
126         y = (int) (r * ranf()) + 2;
127         p = (ranf() > 0.9) ? '*' : ' ';
128
129         move(y, x);
130         if (use_colors > 0) {
131             z = (int) (ranf() * COLORS);
132             if (ranf() > 0.01) {
133                 set_color(my_pairs, z, last_bg);
134                 last_fg = z;
135             } else {
136                 set_color(my_pairs, last_fg, z);
137                 last_bg = z;
138                 napms(1);
139             }
140         } else {
141             if (ranf() <= 0.01) {
142                 if (ranf() > 0.6)
143                     attron(A_REVERSE);
144                 else
145                     attroff(A_REVERSE);
146                 napms(1);
147             }
148         }
149         if (opt_r) {
150             addch(UChar(p));
151             refresh();
152         } else {
153             echochar(UChar(p));
154         }
155         ++total_chars;
156     }
157     cleanup();
158     ExitProgram(EXIT_SUCCESS);
159 }