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