blob: a6119237bda72f3036c2e7fff9fe500e64ae8912 [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2019-2022,2023 Thomas E. Dickey *
3 * Copyright 2007-2013,2017 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
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 *
micky3879b9f5e72025-07-08 18:04:53 -040033 * $Id: dots_mvcur.c,v 1.31 2023/01/07 17:21:48 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010034 *
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
Steve Kondikae271bc2015-11-15 02:50:53 +010044static bool interrupted = FALSE;
45static long total_chars = 0;
46static time_t started;
47
48static
49TPUTS_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
64static bool
65outs(const char *s)
66{
micky3879b9f5e72025-07-08 18:04:53 -040067 if (VALID_STRING(s)) {
Steve Kondikae271bc2015-11-15 02:50:53 +010068 tputs(s, 1, outc);
69 return TRUE;
70 }
71 return FALSE;
72}
73
74static void
75cleanup(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
micky3879b9f5e72025-07-08 18:04:53 -040083 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)));
Steve Kondikae271bc2015-11-15 02:50:53 +010087}
88
89static void
90onsig(int n GCC_UNUSED)
91{
92 interrupted = TRUE;
93}
94
95static double
96ranf(void)
97{
98 long r = (rand() & 077777);
99 return ((double) r / 32768.);
100}
101
micky3879b9f5e72025-07-08 18:04:53 -0400102static int
103get_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
114static void
115usage(int ok)
116{
117 static const char *msg[] =
118 {
119 "Usage: dots_termcap [options]"
120 ,""
121 ,USAGE_COMMON
122 ,"Options:"
123 ," -T TERM override $TERM"
124#if HAVE_USE_ENV
125 ," -e allow environment $LINES / $COLUMNS"
126#endif
127 ," -f use tigetnum rather than <term.h> mapping"
128 ," -m SIZE set margin (default: 2)"
129 ," -r SECS self-interrupt/exit after specified number of seconds"
130 ," -s MSECS delay 1% of the time (default: 1 msecs)"
131 };
132 size_t n;
133
134 for (n = 0; n < SIZEOF(msg); n++)
135 fprintf(stderr, "%s\n", msg[n]);
136
137 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
138}
139/* *INDENT-OFF* */
140VERSION_COMMON()
141/* *INDENT-ON* */
142
Steve Kondikae271bc2015-11-15 02:50:53 +0100143int
micky3879b9f5e72025-07-08 18:04:53 -0400144main(int argc, char *argv[])
Steve Kondikae271bc2015-11-15 02:50:53 +0100145{
146 int x0 = 1, y0 = 1;
micky3879b9f5e72025-07-08 18:04:53 -0400147 int ch;
Steve Kondikae271bc2015-11-15 02:50:53 +0100148 double r;
149 double c;
150 SCREEN *sp;
151 int my_colors;
micky3879b9f5e72025-07-08 18:04:53 -0400152 int f_option = 0;
153 int m_option = 2;
154 int r_option = 0;
155 int s_option = 1;
156 size_t need;
157 char *my_env;
Steve Kondikae271bc2015-11-15 02:50:53 +0100158
micky3879b9f5e72025-07-08 18:04:53 -0400159 while ((ch = getopt(argc, argv, OPTS_COMMON "T:efm:r:s:")) != -1) {
160 switch (ch) {
161 case 'T':
162 need = 6 + strlen(optarg);
163 if ((my_env = malloc(need)) != NULL) {
164 _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
165 putenv(my_env);
166 }
167 break;
168#if HAVE_USE_ENV
169 case 'e':
170 use_env(TRUE);
171 break;
172#endif
173 case 'f':
174 f_option = 1;
175 break;
176 case 'm':
177 m_option = atoi(optarg);
178 break;
179 case 'r':
180 r_option = atoi(optarg);
181 break;
182 case 's':
183 s_option = atoi(optarg);
184 break;
185 case OPTS_VERSION:
186 show_version(argv);
187 ExitProgram(EXIT_SUCCESS);
188 default:
189 usage(ch == OPTS_USAGE);
190 /* NOTREACHED */
191 }
192 }
193
194 SetupAlarm(r_option);
195 InitAndCatch((sp = newterm((char *) 0, stdout, stdin)), onsig);
196 refresh(); /* needed with Solaris curses to cancel endwin */
197
198 if (sp == 0) {
199 fprintf(stderr, "Cannot initialize terminal\n");
200 ExitProgram(EXIT_FAILURE);
201 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100202
203 srand((unsigned) time(0));
micky3879b9f5e72025-07-08 18:04:53 -0400204
Steve Kondikae271bc2015-11-15 02:50:53 +0100205 outs(clear_screen);
206 outs(cursor_home);
207 outs(cursor_invisible);
micky3879b9f5e72025-07-08 18:04:53 -0400208
209#define GetNumber(ln,sn) get_number(f_option ? #sn : 0, ln)
210 my_colors = GetNumber(max_colors, colors);
Steve Kondikae271bc2015-11-15 02:50:53 +0100211 if (my_colors > 1) {
micky3879b9f5e72025-07-08 18:04:53 -0400212 if (!VALID_STRING(set_a_foreground)
213 || !VALID_STRING(set_a_background)
214 || (!VALID_STRING(orig_colors) && !VALID_STRING(orig_pair)))
Steve Kondikae271bc2015-11-15 02:50:53 +0100215 my_colors = -1;
216 }
217
micky3879b9f5e72025-07-08 18:04:53 -0400218 r = (double) (GetNumber(lines, lines) - (m_option * 2));
219 c = (double) (GetNumber(columns, cols) - (m_option * 2));
Steve Kondikae271bc2015-11-15 02:50:53 +0100220 started = time((time_t *) 0);
221
222 while (!interrupted) {
micky3879b9f5e72025-07-08 18:04:53 -0400223 int x = (int) (c * ranf()) + m_option;
224 int y = (int) (r * ranf()) + m_option;
225 int p = (ranf() > 0.9) ? '*' : ' ';
Steve Kondikae271bc2015-11-15 02:50:53 +0100226
227 if (mvcur(y0, x0, y, x) != ERR) {
228 x0 = x;
229 y0 = y;
230 }
231
232 if (my_colors > 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400233 int z = (int) (ranf() * my_colors);
Steve Kondikae271bc2015-11-15 02:50:53 +0100234 if (ranf() > 0.01) {
235 tputs(tparm2(set_a_foreground, z), 1, outc);
236 } else {
237 tputs(tparm2(set_a_background, z), 1, outc);
micky3879b9f5e72025-07-08 18:04:53 -0400238 if (s_option)
239 napms(s_option);
Steve Kondikae271bc2015-11-15 02:50:53 +0100240 }
micky3879b9f5e72025-07-08 18:04:53 -0400241 } else if (VALID_STRING(exit_attribute_mode)
242 && VALID_STRING(enter_reverse_mode)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100243 if (ranf() <= 0.01) {
244 outs((ranf() > 0.6)
245 ? enter_reverse_mode
246 : exit_attribute_mode);
micky3879b9f5e72025-07-08 18:04:53 -0400247 if (s_option)
248 napms(s_option);
Steve Kondikae271bc2015-11-15 02:50:53 +0100249 }
250 }
251 outc(p);
252 ++x0;
253 fflush(stdout);
254 ++total_chars;
255 }
256 cleanup();
257 endwin();
258 delscreen(sp);
259 ExitProgram(EXIT_SUCCESS);
260}
261#else
262int
micky3879b9f5e72025-07-08 18:04:53 -0400263main(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100264{
265 fprintf(stderr, "This program requires terminfo\n");
266 exit(EXIT_FAILURE);
267}
268#endif