]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/lrtest.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / lrtest.c
1 /****************************************************************************
2  * Copyright 2019-2020,2022 Thomas E. Dickey                                *
3  * Copyright 1998-2010,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  * Test lower-right-hand corner access
31  *
32  * originally by Eric S. Raymond <esr@thyrsus.com>, written for animation
33  * and resizing -T.Dickey
34  *
35  * This can't be part of the ncurses test-program, because ncurses rips off the
36  * bottom line to do labels.
37  *
38  * $Id: lrtest.c,v 1.29 2022/12/10 23:44:18 tom Exp $
39  */
40
41 #include <test.priv.h>
42
43 typedef struct {
44     int y, x, mode, dir, inc;
45     chtype value;
46 } MARK;
47
48 /*
49  * Make a couple of markers go 'round the border to demonstrate that we can
50  * really write to all positions properly.
51  */
52 static void
53 show(MARK *m)
54 {
55     MvAddCh(m->y, m->x, m->value);
56     if (m->mode == 0) {         /* along the x-direction */
57         m->x += m->inc;
58         if (m->x >= COLS) {
59             m->x = COLS - 1;
60             m->inc = -m->dir * m->inc;
61             m->y += m->inc;
62             m->mode = 1;
63         } else if (m->x < 0) {
64             m->x = 0;
65             m->inc = -m->dir * m->inc;
66             m->y += m->inc;
67             m->mode = 1;
68         }
69     } else {                    /* along the y-direction */
70         m->y += m->inc;
71         if (m->y >= LINES) {
72             m->y = LINES - 1;
73             m->inc = m->dir * m->inc;
74             m->x += m->inc;
75             m->mode = 0;
76         } else if (m->y < 0) {
77             m->y = 0;
78             m->inc = m->dir * m->inc;
79             m->x += m->inc;
80             m->mode = 0;
81         }
82     }
83 }
84
85 static void
86 usage(int ok)
87 {
88     static const char *msg[] =
89     {
90         "Usage: lrtest [options]"
91         ,""
92         ,USAGE_COMMON
93     };
94     size_t n;
95
96     for (n = 0; n < SIZEOF(msg); n++)
97         fprintf(stderr, "%s\n", msg[n]);
98
99     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
100 }
101 /* *INDENT-OFF* */
102 VERSION_COMMON()
103 /* *INDENT-ON* */
104
105 int
106 main(int argc, char *argv[])
107 {
108     static MARK marks[] =
109     {
110         {0, 0, 0, -1, 1, '+' | A_BOLD},
111         {0, 0, 1, 1, 2, 'X'},
112         {0, 0, 1, -1, 3, 'Y'},
113         {0, 8, 0, -1, 1, '+' | A_BOLD},
114         {0, 9, 0, -1, 1, '+' | A_BOLD},
115         {1, 0, 1, 1, 1, '*' | A_REVERSE},
116         {2, 0, 1, 1, 1, '*' | A_REVERSE}
117     };
118     int ch;
119
120     while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
121         switch (ch) {
122         case OPTS_VERSION:
123             show_version(argv);
124             ExitProgram(EXIT_SUCCESS);
125         default:
126             usage(ch == OPTS_USAGE);
127             /* NOTREACHED */
128         }
129     }
130     if (optind < argc)
131         usage(FALSE);
132
133     setlocale(LC_ALL, "");
134
135     initscr();
136     noecho();
137     cbreak();
138     nodelay(stdscr, TRUE);
139     curs_set(0);
140
141 #ifdef KEY_RESIZE
142     keypad(stdscr, TRUE);
143   restart:
144 #endif
145     move(LINES / 2 - 1, 4);
146     if (!(has_ic()
147 #if HAVE_SETUPTERM
148     /* see PutCharLR() */
149           || auto_right_margin
150           || (enter_am_mode && exit_am_mode)
151 #endif
152         )) {
153         addstr("Your terminal lacks the capabilities needed to address the\n");
154         move(LINES / 2, 4);
155         addstr("lower-right-hand corner of the screen.\n");
156     } else {
157         addstr("This is a test of access to the lower right corner.\n");
158         move(LINES / 2, 4);
159         addstr("If the top of the box is missing, the test failed.\n");
160         move(LINES / 2 + 1, 4);
161         addstr("Please report this (with a copy of your terminfo entry).\n");
162         move(LINES / 2 + 2, 4);
163         addstr("to the ncurses maintainers, at bug-ncurses@gnu.org.\n");
164     }
165
166     for (;;) {
167         int c2;
168         unsigned n;
169
170         box(stdscr, 0, 0);
171         for (n = 0; n < SIZEOF(marks); n++) {
172             show(&marks[n]);
173         }
174
175         if ((c2 = getch()) > 0) {
176             if (c2 == 'q')
177                 break;
178             else if (c2 == 's')
179                 nodelay(stdscr, FALSE);
180             else if (c2 == ' ')
181                 nodelay(stdscr, TRUE);
182 #ifdef TRACE
183             else if (c2 == 'T')
184                 curses_trace(0);
185             else if (c2 == 't')
186                 curses_trace(TRACE_CALLS | TRACE_ICALLS | TRACE_UPDATE);
187 #endif
188 #ifdef KEY_RESIZE
189             else if (c2 == KEY_RESIZE) {
190                 for (n = 0; n < SIZEOF(marks); n++) {
191                     if (marks[n].mode == 0) {   /* moving along x-direction */
192                         if (marks[n].y)
193                             marks[n].y = LINES - 1;
194                     } else {
195                         if (marks[n].x)
196                             marks[n].x = COLS - 1;
197                     }
198                 }
199                 flash();
200                 erase();
201                 wrefresh(curscr);
202                 goto restart;
203             }
204 #endif
205         }
206         napms(50);
207         refresh();
208     }
209
210     stop_curses();
211     ExitProgram(EXIT_SUCCESS);
212 }
213
214 /* lrtest.c ends here */