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