]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_mvcur.c
ncurses 6.2 - patch 20200222
[ncurses.git] / test / dots_mvcur.c
1 /****************************************************************************
2  * Copyright 2019,2020 Thomas E. Dickey                                     *
3  * Copyright 2007-2013,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /*
31  * Author: Thomas E. Dickey - 2007
32  *
33  * $Id: dots_mvcur.c,v 1.22 2020/02/02 23:34:34 tom Exp $
34  *
35  * A simple demo of the terminfo interface, and mvcur.
36  */
37 #define USE_TINFO
38 #include <test.priv.h>
39
40 #if HAVE_SETUPTERM
41
42 #include <time.h>
43
44 static bool interrupted = FALSE;
45 static long total_chars = 0;
46 static time_t started;
47
48 static
49 TPUTS_PROTO(outc, c)
50 {
51     int rc = c;
52
53     if (interrupted) {
54         char tmp = (char) c;
55         if (write(STDOUT_FILENO, &tmp, (size_t) 1) == -1)
56             rc = EOF;
57     } else {
58         if (putc(c, stdout) == EOF)
59             rc = EOF;
60     }
61     TPUTS_RETURN(rc);
62 }
63
64 static bool
65 outs(const char *s)
66 {
67     if (VALID_STRING(s)) {
68         tputs(s, 1, outc);
69         return TRUE;
70     }
71     return FALSE;
72 }
73
74 static void
75 cleanup(void)
76 {
77     outs(exit_attribute_mode);
78     if (!outs(orig_colors))
79         outs(orig_pair);
80     outs(clear_screen);
81     outs(cursor_normal);
82
83     printf("\n\n%ld total cells, rate %.2f/sec\n",
84            total_chars,
85            ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
86 }
87
88 static void
89 onsig(int n GCC_UNUSED)
90 {
91     interrupted = TRUE;
92 }
93
94 static double
95 ranf(void)
96 {
97     long r = (rand() & 077777);
98     return ((double) r / 32768.);
99 }
100
101 static int
102 get_number(NCURSES_CONST char *cap, int map)
103 {
104     int result = map;
105     if (cap != 0) {
106         int check = tigetnum(cap);
107         if (check > 0)
108             result = check;
109     }
110     return result;
111 }
112
113 static void
114 usage(void)
115 {
116     static const char *msg[] =
117     {
118         "Usage: dots_termcap [options]"
119         ,""
120         ,"Options:"
121         ," -T TERM  override $TERM"
122 #if HAVE_USE_ENV
123         ," -e       allow environment $LINES / $COLUMNS"
124 #endif
125         ," -f       use tigetnum rather than <term.h> mapping"
126         ," -m SIZE  set margin (default: 2)"
127         ," -s MSECS delay 1% of the time (default: 1 msecs)"
128     };
129     size_t n;
130
131     for (n = 0; n < SIZEOF(msg); n++)
132         fprintf(stderr, "%s\n", msg[n]);
133
134     ExitProgram(EXIT_FAILURE);
135 }
136
137 int
138 main(int argc GCC_UNUSED,
139      char *argv[]GCC_UNUSED)
140 {
141     int x0 = 1, y0 = 1;
142     int ch;
143     double r;
144     double c;
145     SCREEN *sp;
146     int my_colors;
147     int f_option = 0;
148     int m_option = 2;
149     int s_option = 1;
150     size_t need;
151     char *my_env;
152
153     while ((ch = getopt(argc, argv, "T:efm:s:")) != -1) {
154         switch (ch) {
155         case 'T':
156             need = 6 + strlen(optarg);
157             my_env = malloc(need);
158             _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
159             putenv(my_env);
160             break;
161 #if HAVE_USE_ENV
162         case 'e':
163             use_env(TRUE);
164             break;
165 #endif
166         case 'f':
167             f_option = 1;
168             break;
169         case 'm':
170             m_option = atoi(optarg);
171             break;
172         case 's':
173             s_option = atoi(optarg);
174             break;
175         default:
176             usage();
177             break;
178         }
179     }
180
181     InitAndCatch((sp = newterm((char *) 0, stdout, stdin)), onsig);
182     refresh();                  /* needed with Solaris curses to cancel endwin */
183
184     if (sp == 0) {
185         fprintf(stderr, "Cannot initialize terminal\n");
186         ExitProgram(EXIT_FAILURE);
187     }
188
189     srand((unsigned) time(0));
190
191     outs(clear_screen);
192     outs(cursor_home);
193     outs(cursor_invisible);
194
195 #define GetNumber(ln,sn) get_number(f_option ? #sn : 0, ln)
196     my_colors = GetNumber(max_colors, colors);
197     if (my_colors > 1) {
198         if (!VALID_STRING(set_a_foreground)
199             || !VALID_STRING(set_a_background)
200             || (!VALID_STRING(orig_colors) && !VALID_STRING(orig_pair)))
201             my_colors = -1;
202     }
203
204     r = (double) (GetNumber(lines, lines) - (m_option * 2));
205     c = (double) (GetNumber(columns, cols) - (m_option * 2));
206     started = time((time_t *) 0);
207
208     while (!interrupted) {
209         int x = (int) (c * ranf()) + m_option;
210         int y = (int) (r * ranf()) + m_option;
211         int p = (ranf() > 0.9) ? '*' : ' ';
212
213         if (mvcur(y0, x0, y, x) != ERR) {
214             x0 = x;
215             y0 = y;
216         }
217
218         if (my_colors > 0) {
219             int z = (int) (ranf() * my_colors);
220             if (ranf() > 0.01) {
221                 tputs(tparm2(set_a_foreground, z), 1, outc);
222             } else {
223                 tputs(tparm2(set_a_background, z), 1, outc);
224                 napms(s_option);
225             }
226         } else if (VALID_STRING(exit_attribute_mode)
227                    && VALID_STRING(enter_reverse_mode)) {
228             if (ranf() <= 0.01) {
229                 outs((ranf() > 0.6)
230                      ? enter_reverse_mode
231                      : exit_attribute_mode);
232                 napms(s_option);
233             }
234         }
235         outc(p);
236         ++x0;
237         fflush(stdout);
238         ++total_chars;
239     }
240     cleanup();
241     endwin();
242     delscreen(sp);
243     ExitProgram(EXIT_SUCCESS);
244 }
245 #else
246 int
247 main(int argc GCC_UNUSED,
248      char *argv[]GCC_UNUSED)
249 {
250     fprintf(stderr, "This program requires terminfo\n");
251     exit(EXIT_FAILURE);
252 }
253 #endif