]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots.c
ncurses 5.6 - patch 20061223
[ncurses.git] / test / dots.c
1 /****************************************************************************
2  * Copyright (c) 1999-2005,2006 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 <dickey@clark.net> 1999
31  *
32  * $Id: dots.c,v 1.15 2006/11/04 19:54:42 tom Exp $
33  *
34  * A simple demo of the terminfo interface.
35  */
36 #include <test.priv.h>
37
38 #if HAVE_SETUPTERM
39
40 #include <time.h>
41
42 #define valid(s) ((s != 0) && s != (char *)-1)
43
44 static bool interrupted = FALSE;
45 static long total_chars = 0;
46 static time_t started;
47
48 static int
49 outc(int c)
50 {
51     if (interrupted) {
52         char tmp = c;
53         write(STDOUT_FILENO, &tmp, 1);
54     } else {
55         putc(c, stdout);
56     }
57     return 0;
58 }
59
60 static bool
61 outs(char *s)
62 {
63     if (valid(s)) {
64         tputs(s, 1, outc);
65         return TRUE;
66     }
67     return FALSE;
68 }
69
70 static void
71 cleanup(void)
72 {
73     outs(exit_attribute_mode);
74     if (!outs(orig_colors))
75         outs(orig_pair);
76     outs(clear_screen);
77     outs(cursor_normal);
78
79     printf("\n\n%ld total chars, rate %.2f/sec\n",
80            total_chars,
81            ((double) (total_chars) / (time((time_t *) 0) - started)));
82 }
83
84 static void
85 onsig(int n GCC_UNUSED)
86 {
87     interrupted = TRUE;
88 }
89
90 static float
91 ranf(void)
92 {
93     long r = (rand() & 077777);
94     return ((float) r / 32768.);
95 }
96
97 int
98 main(
99         int argc GCC_UNUSED,
100         char *argv[]GCC_UNUSED)
101 {
102     int x, y, z, p;
103     float r;
104     float c;
105
106     CATCHALL(onsig);
107
108     srand((unsigned) time(0));
109     setupterm((char *) 0, 1, (int *) 0);
110     outs(clear_screen);
111     outs(cursor_invisible);
112     if (max_colors > 1) {
113         if (!valid(set_a_foreground)
114             || !valid(set_a_background)
115             || (!valid(orig_colors) && !valid(orig_pair)))
116             max_colors = -1;
117     }
118
119     r = (float) (lines - 4);
120     c = (float) (columns - 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         tputs(tparm3(cursor_address, y, x), 1, outc);
129         if (max_colors > 0) {
130             z = (int) (ranf() * max_colors);
131             if (ranf() > 0.01) {
132                 tputs(tparm2(set_a_foreground, z), 1, outc);
133             } else {
134                 tputs(tparm2(set_a_background, z), 1, outc);
135                 napms(1);
136             }
137         } else if (valid(exit_attribute_mode)
138                    && valid(enter_reverse_mode)) {
139             if (ranf() <= 0.01) {
140                 outs((ranf() > 0.6)
141                      ? enter_reverse_mode
142                      : exit_attribute_mode);
143                 napms(1);
144             }
145         }
146         outc(p);
147         fflush(stdout);
148         ++total_chars;
149     }
150     cleanup();
151     ExitProgram(EXIT_SUCCESS);
152 }
153 #else
154 int
155 main(int argc GCC_UNUSED,
156      char *argv[]GCC_UNUSED)
157 {
158     fprintf(stderr, "This program requires terminfo\n");
159     exit(EXIT_FAILURE);
160 }
161 #endif