]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_mvcur.c
ncurses 6.1 - patch 20190309
[ncurses.git] / test / dots_mvcur.c
1 /****************************************************************************
2  * Copyright (c) 2007-2017,2019 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.20 2019/01/21 14:20:18 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 static int
101 get_number(NCURSES_CONST char *cap, int map)
102 {
103     int result = map;
104     if (cap != 0) {
105         int check = tigetnum(cap);
106         if (check > 0)
107             result = check;
108     }
109     return result;
110 }
111
112 static void
113 usage(void)
114 {
115     static const char *msg[] =
116     {
117         "Usage: dots_termcap [options]"
118         ,""
119         ,"Options:"
120         ," -T TERM  override $TERM"
121 #if HAVE_USE_ENV
122         ," -e       allow environment $LINES / $COLUMNS"
123 #endif
124         ," -f       use tigetnum rather than <term.h> mapping"
125         ," -m SIZE  set margin (default: 2)"
126         ," -s MSECS delay 1% of the time (default: 1 msecs)"
127     };
128     size_t n;
129
130     for (n = 0; n < SIZEOF(msg); n++)
131         fprintf(stderr, "%s\n", msg[n]);
132
133     ExitProgram(EXIT_FAILURE);
134 }
135
136 int
137 main(int argc GCC_UNUSED,
138      char *argv[]GCC_UNUSED)
139 {
140     int x0 = 1, y0 = 1;
141     int x, y, z, p;
142     double r;
143     double c;
144     SCREEN *sp;
145     int my_colors;
146     int f_option = 0;
147     int m_option = 2;
148     int s_option = 1;
149     size_t need;
150     char *my_env;
151
152     while ((x = getopt(argc, argv, "T:efm:s:")) != -1) {
153         switch (x) {
154         case 'T':
155             need = 6 + strlen(optarg);
156             my_env = malloc(need);
157             _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
158             putenv(my_env);
159             break;
160 #if HAVE_USE_ENV
161         case 'e':
162             use_env(TRUE);
163             break;
164 #endif
165         case 'f':
166             f_option = 1;
167             break;
168         case 'm':
169             m_option = atoi(optarg);
170             break;
171         case 's':
172             s_option = atoi(optarg);
173             break;
174         default:
175             usage();
176             break;
177         }
178     }
179
180     InitAndCatch((sp = newterm((char *) 0, stdout, stdin)), onsig);
181     refresh();                  /* needed with Solaris curses to cancel endwin */
182
183     if (sp == 0) {
184         fprintf(stderr, "Cannot initialize terminal\n");
185         ExitProgram(EXIT_FAILURE);
186     }
187
188     srand((unsigned) time(0));
189
190     outs(clear_screen);
191     outs(cursor_home);
192     outs(cursor_invisible);
193
194 #define GetNumber(ln,sn) get_number(f_option ? #sn : 0, ln)
195     my_colors = GetNumber(max_colors, colors);
196     if (my_colors > 1) {
197         if (!VALID_STRING(set_a_foreground)
198             || !VALID_STRING(set_a_background)
199             || (!VALID_STRING(orig_colors) && !VALID_STRING(orig_pair)))
200             my_colors = -1;
201     }
202
203     r = (double) (GetNumber(lines, lines) - (m_option * 2));
204     c = (double) (GetNumber(columns, cols) - (m_option * 2));
205     started = time((time_t *) 0);
206
207     while (!interrupted) {
208         x = (int) (c * ranf()) + m_option;
209         y = (int) (r * ranf()) + m_option;
210         p = (ranf() > 0.9) ? '*' : ' ';
211
212         if (mvcur(y0, x0, y, x) != ERR) {
213             x0 = x;
214             y0 = y;
215         }
216
217         if (my_colors > 0) {
218             z = (int) (ranf() * my_colors);
219             if (ranf() > 0.01) {
220                 tputs(tparm2(set_a_foreground, z), 1, outc);
221             } else {
222                 tputs(tparm2(set_a_background, z), 1, outc);
223                 napms(s_option);
224             }
225         } else if (VALID_STRING(exit_attribute_mode)
226                    && VALID_STRING(enter_reverse_mode)) {
227             if (ranf() <= 0.01) {
228                 outs((ranf() > 0.6)
229                      ? enter_reverse_mode
230                      : exit_attribute_mode);
231                 napms(s_option);
232             }
233         }
234         outc(p);
235         ++x0;
236         fflush(stdout);
237         ++total_chars;
238     }
239     cleanup();
240     endwin();
241     delscreen(sp);
242     ExitProgram(EXIT_SUCCESS);
243 }
244 #else
245 int
246 main(int argc GCC_UNUSED,
247      char *argv[]GCC_UNUSED)
248 {
249     fprintf(stderr, "This program requires terminfo\n");
250     exit(EXIT_FAILURE);
251 }
252 #endif