]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_mvcur.c
ncurses 6.0 - patch 20170819
[ncurses.git] / test / dots_mvcur.c
1 /****************************************************************************
2  * Copyright (c) 2007-2013,2017 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 - 2007
31  *
32  * $Id: dots_mvcur.c,v 1.11 2017/06/17 18:25:30 tom Exp $
33  *
34  * A simple demo of the terminfo interface, and mvcur.
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, (size_t) 1) == -1)
57             rc = EOF;
58     } else {
59         if (putc(c, stdout) == EOF)
60             rc = EOF;
61     }
62     TPUTS_RETURN(rc);
63 }
64
65 static bool
66 outs(const char *s)
67 {
68     if (valid(s)) {
69         tputs(s, 1, outc);
70         return TRUE;
71     }
72     return FALSE;
73 }
74
75 static void
76 cleanup(void)
77 {
78     outs(exit_attribute_mode);
79     if (!outs(orig_colors))
80         outs(orig_pair);
81     outs(clear_screen);
82     outs(cursor_normal);
83
84     printf("\n\n%ld total chars, rate %.2f/sec\n",
85            total_chars,
86            ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
87 }
88
89 static void
90 onsig(int n GCC_UNUSED)
91 {
92     interrupted = TRUE;
93 }
94
95 static double
96 ranf(void)
97 {
98     long r = (rand() & 077777);
99     return ((double) r / 32768.);
100 }
101
102 int
103 main(int argc GCC_UNUSED,
104      char *argv[]GCC_UNUSED)
105 {
106     int x0 = 1, y0 = 1;
107     int x, y, z, p;
108     double r;
109     double c;
110     SCREEN *sp;
111     int my_colors;
112
113     CATCHALL(onsig);
114
115     srand((unsigned) time(0));
116     if ((sp = newterm((char *) 0, stdout, stdin)) == 0) {
117         fprintf(stderr, "Cannot initialize terminal\n");
118         ExitProgram(EXIT_FAILURE);
119     }
120     outs(clear_screen);
121     outs(cursor_home);
122     outs(cursor_invisible);
123     my_colors = max_colors;
124     if (my_colors > 1) {
125         if (!valid(set_a_foreground)
126             || !valid(set_a_background)
127             || (!valid(orig_colors) && !valid(orig_pair)))
128             my_colors = -1;
129     }
130
131     r = (double) (lines - 4);
132     c = (double) (columns - 4);
133     started = time((time_t *) 0);
134
135     while (!interrupted) {
136         x = (int) (c * ranf()) + 2;
137         y = (int) (r * ranf()) + 2;
138         p = (ranf() > 0.9) ? '*' : ' ';
139
140         if (mvcur(y0, x0, y, x) != ERR) {
141             x0 = x;
142             y0 = y;
143         }
144
145         if (my_colors > 0) {
146             z = (int) (ranf() * my_colors);
147             if (ranf() > 0.01) {
148                 tputs(tparm2(set_a_foreground, z), 1, outc);
149             } else {
150                 tputs(tparm2(set_a_background, z), 1, outc);
151                 napms(1);
152             }
153         } else if (valid(exit_attribute_mode)
154                    && valid(enter_reverse_mode)) {
155             if (ranf() <= 0.01) {
156                 outs((ranf() > 0.6)
157                      ? enter_reverse_mode
158                      : exit_attribute_mode);
159                 napms(1);
160             }
161         }
162         outc(p);
163         ++x0;
164         fflush(stdout);
165         ++total_chars;
166     }
167     cleanup();
168     endwin();
169     delscreen(sp);
170     ExitProgram(EXIT_SUCCESS);
171 }
172 #else
173 int
174 main(int argc GCC_UNUSED,
175      char *argv[]GCC_UNUSED)
176 {
177     fprintf(stderr, "This program requires terminfo\n");
178     exit(EXIT_FAILURE);
179 }
180 #endif