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