]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_curses.c
ncurses 6.0 - patch 20160618
[ncurses.git] / test / dots_curses.c
1 /****************************************************************************
2  * Copyright (c) 2014 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
31  *
32  * $Id: dots_curses.c,v 1.3 2014/08/09 22:28:42 tom Exp $
33  *
34  * A simple demo of the curses interface used for comparison with termcap.
35  */
36 #include <test.priv.h>
37
38 #if !defined(__MINGW32__)
39 #include <sys/time.h>
40 #endif
41
42 #include <time.h>
43
44 #define valid(s) ((s != 0) && s != (char *)-1)
45
46 static bool interrupted = FALSE;
47 static long total_chars = 0;
48 static time_t started;
49
50 static void
51 cleanup(void)
52 {
53     endwin();
54
55     printf("\n\n%ld total chars, rate %.2f/sec\n",
56            total_chars,
57            ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
58 }
59
60 static void
61 onsig(int n GCC_UNUSED)
62 {
63     interrupted = TRUE;
64 }
65
66 static double
67 ranf(void)
68 {
69     long r = (rand() & 077777);
70     return ((double) r / 32768.);
71 }
72
73 static int
74 mypair(int fg, int bg)
75 {
76     int pair = (fg * COLORS) + bg;
77     return (pair >= COLOR_PAIRS) ? -1 : pair;
78 }
79
80 static void
81 set_colors(int fg, int bg)
82 {
83     int pair = mypair(fg, bg);
84     if (pair > 0) {
85         attron(COLOR_PAIR(mypair(fg, bg)));
86     }
87 }
88
89 int
90 main(int argc GCC_UNUSED,
91      char *argv[]GCC_UNUSED)
92 {
93     int x, y, z, p;
94     int fg, bg;
95     double r;
96     double c;
97
98     CATCHALL(onsig);
99
100     srand((unsigned) time(0));
101
102     initscr();
103     if (has_colors()) {
104         start_color();
105         for (fg = 0; fg < COLORS; fg++) {
106             for (bg = 0; bg < COLORS; bg++) {
107                 int pair = mypair(fg, bg);
108                 if (pair > 0)
109                     init_pair((short) pair, (short) fg, (short) bg);
110             }
111         }
112     }
113
114     r = (double) (LINES - 4);
115     c = (double) (COLS - 4);
116     started = time((time_t *) 0);
117
118     fg = COLOR_WHITE;
119     bg = COLOR_BLACK;
120     while (!interrupted) {
121         x = (int) (c * ranf()) + 2;
122         y = (int) (r * ranf()) + 2;
123         p = (ranf() > 0.9) ? '*' : ' ';
124
125         move(y, x);
126         if (has_colors()) {
127             z = (int) (ranf() * COLORS);
128             if (ranf() > 0.01) {
129                 set_colors(fg = z, bg);
130                 attron(COLOR_PAIR(mypair(fg, bg)));
131             } else {
132                 set_colors(fg, bg = z);
133                 napms(1);
134             }
135         } else {
136             if (ranf() <= 0.01) {
137                 if (ranf() > 0.6) {
138                     attron(A_REVERSE);
139                 } else {
140                     attroff(A_REVERSE);
141                 }
142                 napms(1);
143             }
144         }
145         addch((chtype) p);
146         refresh();
147         ++total_chars;
148     }
149     cleanup();
150     ExitProgram(EXIT_SUCCESS);
151 }