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