]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots.c
ncurses 5.5
[ncurses.git] / test / dots.c
1 /****************************************************************************
2  * Copyright (c) 1999-2002,2004 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.11 2005/05/28 21:38:45 tom Exp $
33  *
34  * A simple demo of the terminfo interface.
35  */
36 #include <test.priv.h>
37
38 #include <time.h>
39
40 #define valid(s) ((s != 0) && s != (char *)-1)
41
42 static bool interrupted = FALSE;
43
44 static int
45 outc(int c)
46 {
47     if (interrupted) {
48         char tmp = c;
49         write(STDOUT_FILENO, &tmp, 1);
50     } else {
51         putc(c, stdout);
52     }
53     return 0;
54 }
55
56 static bool
57 outs(char *s)
58 {
59     if (valid(s)) {
60         tputs(s, 1, outc);
61         return TRUE;
62     }
63     return FALSE;
64 }
65
66 static void
67 cleanup(void)
68 {
69     outs(exit_attribute_mode);
70     if (!outs(orig_colors))
71         outs(orig_pair);
72     outs(clear_screen);
73     outs(cursor_normal);
74 }
75
76 static void
77 onsig(int n GCC_UNUSED)
78 {
79     interrupted = TRUE;
80     cleanup();
81     ExitProgram(EXIT_FAILURE);
82 }
83
84 static float
85 ranf(void)
86 {
87     long r = (rand() & 077777);
88     return ((float) r / 32768.);
89 }
90
91 int
92 main(
93         int argc GCC_UNUSED,
94         char *argv[]GCC_UNUSED)
95 {
96     int x, y, z, j, p;
97     float r;
98     float c;
99
100     for (j = SIGHUP; j <= SIGTERM; j++)
101         if (signal(j, SIG_IGN) != SIG_IGN)
102             signal(j, onsig);
103
104     srand((unsigned) time(0));
105     setupterm((char *) 0, 1, (int *) 0);
106     outs(clear_screen);
107     outs(cursor_invisible);
108     if (max_colors > 1) {
109         if (!valid(set_a_foreground)
110             || !valid(set_a_background)
111             || (!valid(orig_colors) && !valid(orig_pair)))
112             max_colors = -1;
113     }
114
115     r = (float) (lines - 4);
116     c = (float) (columns - 4);
117
118     for (;;) {
119         x = (int) (c * ranf()) + 2;
120         y = (int) (r * ranf()) + 2;
121         p = (ranf() > 0.9) ? '*' : ' ';
122
123         tputs(tparm3(cursor_address, y, x), 1, outc);
124         if (max_colors > 0) {
125             z = (int) (ranf() * max_colors);
126             if (ranf() > 0.01) {
127                 tputs(tparm2(set_a_foreground, z), 1, outc);
128             } else {
129                 tputs(tparm2(set_a_background, z), 1, outc);
130                 napms(1);
131             }
132         } else if (valid(exit_attribute_mode)
133                    && valid(enter_reverse_mode)) {
134             if (ranf() <= 0.01) {
135                 outs((ranf() > 0.6)
136                      ? enter_reverse_mode
137                      : exit_attribute_mode);
138                 napms(1);
139             }
140         }
141         outc(p);
142         fflush(stdout);
143     }
144 }