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