1 /****************************************************************************
2 * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
28 /****************************************************************************
31 ncurses.c --- ncurses library exerciser
37 An interactive test module for the ncurses library.
40 Author: Eric S. Raymond <esr@snark.thyrsus.com> 1993
42 $Id: ncurses.c,v 1.138 2000/09/17 01:24:00 tom Exp $
44 ***************************************************************************/
46 #include <test.priv.h>
59 #if HAVE_SYS_TIME_H && HAVE_SYS_TIME_SELECT
63 #include <sys/select.h>
68 #define USE_LIBPANEL 1
71 #define USE_LIBPANEL 0
74 #if HAVE_MENU_H && HAVE_LIBMENU
81 #if HAVE_FORM_H && HAVE_LIBFORM
88 #ifdef NCURSES_VERSION
91 static int save_trace = TRACE_ORDINARY | TRACE_CALLS;
92 extern int _nc_tracing;
95 #if !defined(HAVE_NAPMS)
101 #define mmask_t chtype /* not specified in XSI */
102 #define attr_t chtype /* not specified in XSI */
103 #define ACS_S3 (acs_map['p']) /* scan line 3 */
104 #define ACS_S7 (acs_map['r']) /* scan line 7 */
105 #define ACS_LEQUAL (acs_map['y']) /* less/equal */
106 #define ACS_GEQUAL (acs_map['z']) /* greater/equal */
107 #define ACS_PI (acs_map['{']) /* Pi */
108 #define ACS_NEQUAL (acs_map['|']) /* not equal */
109 #define ACS_STERLING (acs_map['}']) /* UK pound sign */
113 #define P(string) printw("%s\n", string)
115 #define CTRL(x) ((x) & 0x1f)
118 #define QUIT CTRL('Q')
119 #define ESCAPE CTRL('[')
120 #define BLANK ' ' /* this is the background character */
122 /* The behavior of mvhline, mvvline for negative/zero length is unspecified,
123 * though we can rely on negative x/y values to stop the macro.
126 do_h_line(int y, int x, chtype c, int to)
129 mvhline(y, x, c, (to) - (x));
133 do_v_line(int y, int x, chtype c, int to)
136 mvvline(y, x, c, (to) - (y));
139 /* Common function to allow ^T to toggle trace-mode in the middle of a test
140 * so that trace-files can be made smaller.
143 wGetchar(WINDOW *win)
147 while ((c = wgetch(win)) == CTRL('T')) {
149 save_trace = _nc_tracing;
150 _tracef("TOGGLE-TRACING OFF");
153 _nc_tracing = save_trace;
157 _tracef("TOGGLE-TRACING ON");
164 #define Getchar() wGetchar(stdscr)
170 addstr("Press any key to continue... ");
175 Cannot(const char *what)
177 printw("\nThis %s terminal %s\n\n", getenv("TERM"), what);
182 ShellOut(bool message)
185 addstr("Shelling out...");
190 addstr("returned from shellout.\n");
194 #ifdef NCURSES_MOUSE_VERSION
196 mouse_decode(MEVENT const *ep)
200 (void) sprintf(buf, "id %2d at (%2d, %2d, %2d) state %4lx = {",
201 ep->id, ep->x, ep->y, ep->z, ep->bstate);
203 #define SHOW(m, s) if ((ep->bstate & m)==m) {strcat(buf,s); strcat(buf, ", ");}
204 SHOW(BUTTON1_RELEASED, "release-1");
205 SHOW(BUTTON1_PRESSED, "press-1");
206 SHOW(BUTTON1_CLICKED, "click-1");
207 SHOW(BUTTON1_DOUBLE_CLICKED, "doubleclick-1");
208 SHOW(BUTTON1_TRIPLE_CLICKED, "tripleclick-1");
209 SHOW(BUTTON1_RESERVED_EVENT, "reserved-1");
210 SHOW(BUTTON2_RELEASED, "release-2");
211 SHOW(BUTTON2_PRESSED, "press-2");
212 SHOW(BUTTON2_CLICKED, "click-2");
213 SHOW(BUTTON2_DOUBLE_CLICKED, "doubleclick-2");
214 SHOW(BUTTON2_TRIPLE_CLICKED, "tripleclick-2");
215 SHOW(BUTTON2_RESERVED_EVENT, "reserved-2");
216 SHOW(BUTTON3_RELEASED, "release-3");
217 SHOW(BUTTON3_PRESSED, "press-3");
218 SHOW(BUTTON3_CLICKED, "click-3");
219 SHOW(BUTTON3_DOUBLE_CLICKED, "doubleclick-3");
220 SHOW(BUTTON3_TRIPLE_CLICKED, "tripleclick-3");
221 SHOW(BUTTON3_RESERVED_EVENT, "reserved-3");
222 SHOW(BUTTON4_RELEASED, "release-4");
223 SHOW(BUTTON4_PRESSED, "press-4");
224 SHOW(BUTTON4_CLICKED, "click-4");
225 SHOW(BUTTON4_DOUBLE_CLICKED, "doubleclick-4");
226 SHOW(BUTTON4_TRIPLE_CLICKED, "tripleclick-4");
227 SHOW(BUTTON4_RESERVED_EVENT, "reserved-4");
228 SHOW(BUTTON_CTRL, "ctrl");
229 SHOW(BUTTON_SHIFT, "shift");
230 SHOW(BUTTON_ALT, "alt");
231 SHOW(ALL_MOUSE_EVENTS, "all-events");
232 SHOW(REPORT_MOUSE_POSITION, "position");
235 if (buf[strlen(buf) - 1] == ' ')
236 buf[strlen(buf) - 2] = '\0';
237 (void) strcat(buf, "}");
240 #endif /* NCURSES_MOUSE_VERSION */
242 /****************************************************************************
244 * Character input test
246 ****************************************************************************/
250 /* test the keypad feature */
254 int incount = 0, firsttime = 0;
255 bool blocking = TRUE;
260 #ifdef NCURSES_MOUSE_VERSION
261 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
264 (void) printw("Delay in 10ths of a second (<CR> for blocking input)? ");
270 if (isdigit(buf[0])) {
271 timeout(atoi(buf) * 100);
279 printw("Key pressed: %04o ", c);
280 #ifdef NCURSES_MOUSE_VERSION
281 if (c == KEY_MOUSE) {
285 printw("KEY_MOUSE, %s\n", mouse_decode(&event));
287 #endif /* NCURSES_MOUSE_VERSION */
289 (void) addstr(keyname(c));
291 } else if (c > 0x80) {
294 (void) printw("M-%c", c2);
296 (void) printw("M-%s", unctrl(c2));
297 addstr(" (high-half character)\n");
300 (void) printw("%c (ASCII printable character)\n", c);
302 (void) printw("%s (ASCII control character)\n", unctrl(c));
311 addstr("getstr test: ");
315 printw("I saw `%s'.\n", buf);
320 if (c == 'x' || c == 'q' || (c == ERR && blocking))
323 addstr("Type any key to see its keypad value. Also:\n");
324 addstr("g -- triggers a getstr test\n");
325 addstr("s -- shell out\n");
326 addstr("q -- quit\n");
327 addstr("? -- repeats this help message\n");
330 while ((c = Getchar()) == ERR)
332 (void) printw("%05d: input timed out\n", incount++);
334 (void) printw("%05d: input error\n", incount++);
339 #ifdef NCURSES_MOUSE_VERSION
340 mousemask(0, (mmask_t *) 0);
350 show_attr(int row, int skip, chtype attr, const char *name)
352 static const char *string = "abcde fghij klmno pqrst uvwxy z";
353 int ncv = tigetnum("ncv");
355 mvprintw(row, 8, "%s mode:", name);
356 mvprintw(row, 24, "|");
358 printw("%*s", skip, " ");
361 * If we're to write a string in the alternate character set, it is not
362 * sufficient to just set A_ALTCHARSET. We have to perform the mapping
363 * that corresponds. This is not needed for vt100-compatible devices
364 * because the acs_map[] is 1:1, but for PC-style devices such as Linux
365 * console, the acs_map[] is scattered about the range.
367 * The addch/addstr functions do not themselves do this mapping, since it
368 * is possible to turn off the A_ALTCHARSET flag for the characters which
369 * are added, and it would be an unexpected result to have the mapped
370 * characters visible on the screen.
372 * This example works because the indices into acs_map[] are mostly from
373 * the lowercase characters.
375 if (attr & A_ALTCHARSET) {
376 const char *s = string;
379 if ((ch = acs_map[ch]) == 0)
388 printw("%*s", skip, " ");
390 if (attr != A_NORMAL) {
391 if (!(termattrs() & attr)) {
393 } else if (ncv > 0 && (getbkgd(stdscr) & A_COLOR)) {
394 static const attr_t table[] =
408 for (n = 0; n < sizeof(table) / sizeof(table[0]); n++) {
409 if ((table[n] & attr) != 0
410 && ((1 << n) & ncv) != 0) {
423 attr_getc(int *skip, int *fg, int *bg, int *ac)
430 } else if (ch == CTRL('L')) {
434 } else if (has_colors()) {
484 /* test text attributes */
487 int skip = tigetnum("xmc");
488 int fg = COLOR_BLACK; /* color pair 0 is special */
489 int bg = COLOR_BLACK;
491 bool *pairs = (bool *) calloc(COLOR_PAIRS, sizeof(bool));
497 n = skip; /* make it easy */
501 int normal = A_NORMAL | BLANK;
504 int pair = (fg * COLORS) + bg;
506 init_pair(pair, fg, bg);
509 normal |= COLOR_PAIR(pair);
514 mvaddstr(0, 20, "Character attribute test display");
516 row = show_attr(row, n, ac | A_STANDOUT, "STANDOUT");
517 row = show_attr(row, n, ac | A_REVERSE, "REVERSE");
518 row = show_attr(row, n, ac | A_BOLD, "BOLD");
519 row = show_attr(row, n, ac | A_UNDERLINE, "UNDERLINE");
520 row = show_attr(row, n, ac | A_DIM, "DIM");
521 row = show_attr(row, n, ac | A_BLINK, "BLINK");
522 row = show_attr(row, n, ac | A_PROTECT, "PROTECT");
523 row = show_attr(row, n, ac | A_INVIS, "INVISIBLE");
524 row = show_attr(row, n, ac | A_NORMAL, "NORMAL");
527 "This terminal does %shave the magic-cookie glitch",
528 tigetnum("xmc") > -1 ? "" : "not ");
530 "Enter a digit to set gaps on each side of displayed attributes");
534 printw(". f/F/b/F toggle colors (now %d/%d), a/A altcharset (%d)",
537 printw(". a/A altcharset (%d)", ac != 0);
540 } while (attr_getc(&n, &fg, &bg, &ac));
542 free((char *) pairs);
543 bkgdset(A_NORMAL | BLANK);
548 /****************************************************************************
550 * Color support tests
552 ****************************************************************************/
554 static NCURSES_CONST char *color_names[] =
575 show_color_name(int y, int x, int color)
578 mvprintw(y, x, "%02d ", color);
580 mvaddstr(y, x, color_names[color]);
585 /* generate a color test pattern */
588 int base, top, width;
589 NCURSES_CONST char *hello;
592 (void) printw("There are %d color pairs\n", COLOR_PAIRS);
594 width = (COLORS > 8) ? 4 : 8;
595 hello = (COLORS > 8) ? "Test" : "Hello";
597 for (base = 0; base < 2; base++) {
598 top = (COLORS > 8) ? 0 : base * (COLORS + 3);
600 (void) mvprintw(top + 1, 0,
601 "%dx%d matrix of foreground/background colors, bright *%s*\n",
603 base ? "on" : "off");
604 for (i = 0; i < COLORS; i++)
605 show_color_name(top + 2, (i + 1) * width, i);
606 for (i = 0; i < COLORS; i++)
607 show_color_name(top + 3 + i, 0, i);
608 for (i = 1; i < COLOR_PAIRS; i++) {
609 init_pair(i, i % COLORS, i / COLORS);
610 attron((attr_t) COLOR_PAIR(i));
612 attron((attr_t) A_BOLD);
613 mvaddstr(top + 3 + (i / COLORS), (i % COLORS + 1) * width, hello);
616 if ((COLORS > 8) || base)
625 change_color(int current, int field, int value, int usebase)
627 short red, green, blue;
630 color_content(current, &red, &green, &blue);
632 red = green = blue = 0;
646 if (init_color(current, red, green, blue) == ERR)
652 /* display the color test pattern, without trying to edit colors */
654 int i, this_c = 0, value = 0, current = 0, field = 0;
656 int max_colors = COLORS > 16 ? 16 : COLORS;
660 for (i = 0; i < max_colors; i++)
661 init_pair(i, COLOR_WHITE, i);
663 mvprintw(LINES - 2, 0, "Number: %d", value);
666 short red, green, blue;
669 mvaddstr(0, 20, "Color RGB Value Editing");
672 for (i = 0; i < max_colors; i++) {
673 mvprintw(2 + i, 0, "%c %-8s:",
674 (i == current ? '>' : ' '),
675 (i < (int) SIZEOF(color_names)
676 ? color_names[i] : ""));
677 attrset(COLOR_PAIR(i));
682 * Note: this refresh should *not* be necessary! It works around
683 * a bug in attribute handling that apparently causes the A_NORMAL
684 * attribute sets to interfere with the actual emission of the
685 * color setting somehow. This needs to be fixed.
689 color_content(i, &red, &green, &blue);
691 if (current == i && field == 0)
694 if (current == i && field == 0)
697 if (current == i && field == 1)
699 printw("%04d", green);
700 if (current == i && field == 1)
703 if (current == i && field == 2)
705 printw("%04d", blue);
706 if (current == i && field == 2)
712 mvaddstr(max_colors + 3, 0,
713 "Use up/down to select a color, left/right to change fields.");
714 mvaddstr(max_colors + 4, 0,
715 "Modify field by typing nnn=, nnn-, or nnn+. ? for help.");
717 move(2 + current, 0);
721 if (isdigit(this_c) && !isdigit(last_c))
726 current = (current == 0 ? (max_colors - 1) : current - 1);
730 current = (current == (max_colors - 1) ? 0 : current + 1);
734 field = (field == 2 ? 0 : field + 1);
738 field = (field == 0 ? 2 : field - 1);
751 value = value * 10 + (this_c - '0');
755 change_color(current, field, value, 1);
759 change_color(current, field, -value, 1);
763 change_color(current, field, value, 0);
768 P(" RGB Value Editing Help");
770 P("You are in the RGB value editor. Use the arrow keys to select one of");
771 P("the fields in one of the RGB triples of the current colors; the one");
772 P("currently selected will be reverse-video highlighted.");
774 P("To change a field, enter the digits of the new value; they are echoed");
775 P("as entered. Finish by typing `='. The change will take effect instantly.");
776 P("To increment or decrement a value, use the same procedure, but finish");
777 P("with a `+' or `-'.");
779 P("To quit, do `x' or 'q'");
793 mvprintw(LINES - 2, 0, "Number: %d", value);
796 (this_c != 'x' && this_c != 'q');
802 /****************************************************************************
804 * Soft-key label test
806 ****************************************************************************/
810 /* exercise the soft keys */
822 mvaddstr(0, 20, "Soft Key Exerciser");
826 P("Available commands are:");
828 P("^L -- refresh screen");
829 P("a -- activate or restore soft keys");
830 P("d -- disable soft keys");
831 P("c -- set centered format for labels");
832 P("l -- set left-justified format for labels");
833 P("r -- set right-justified format for labels");
834 P("[12345678] -- set label; labels are numbered 1 through 8");
835 P("e -- erase stdscr (should not erase labels)");
836 P("s -- test scrolling of shortened screen");
837 P("x, q -- return to main menu");
839 P("Note: if activating the soft keys causes your terminal to");
840 P("scroll up one line, your terminal auto-scrolls when anything");
841 P("is written to the last screen position. The ncurses code");
842 P("does not yet handle this gracefully.");
855 mvprintw(20, 0, "Press Q to stop the scrolling-test: ");
856 while ((c = Getchar()) != 'Q' && (c != ERR))
884 (void) mvaddstr(20, 0, "Please enter the label value: ");
886 wgetnstr(stdscr, buf, 8);
888 slk_set((c - '0'), buf, fmt);
902 ((c = Getchar()) != EOF);
909 /****************************************************************************
911 * Alternate character-set stuff
913 ****************************************************************************/
915 /* ISO 6429: codes 0x80 to 0x9f may be control characters that cause the
916 * terminal to perform functions. The remaining codes can be graphic.
919 show_upper_chars(int first)
921 bool C1 = (first == 128);
923 int last = first + 31;
928 mvprintw(0, 20, "Display of %s Character Codes %d to %d",
929 C1 ? "C1" : "GR", first, last);
933 for (code = first; code <= last; code++) {
934 int row = 4 + ((code - first) % 16);
935 int col = ((code - first) / 16) * COLS / 2;
937 sprintf(tmp, "%3d (0x%x)", code, code);
938 mvprintw(row, col, "%*s: ", COLS / 4, tmp);
940 nodelay(stdscr, TRUE);
943 /* (yes, this _is_ crude) */
944 while ((reply = Getchar()) != ERR) {
948 nodelay(stdscr, FALSE);
954 show_1_acs(int n, const char *name, chtype code)
956 const int height = 16;
957 int row = 4 + (n % height);
958 int col = (n / height) * COLS / 2;
959 mvprintw(row, col, "%*s : ", COLS / 4, name);
966 /* display the ACS character set */
970 #define BOTH(name) #name, name
974 mvaddstr(0, 20, "Display of the ACS Character Set");
978 n = show_1_acs(0, BOTH(ACS_ULCORNER));
979 n = show_1_acs(n, BOTH(ACS_LLCORNER));
980 n = show_1_acs(n, BOTH(ACS_URCORNER));
981 n = show_1_acs(n, BOTH(ACS_LRCORNER));
982 n = show_1_acs(n, BOTH(ACS_RTEE));
983 n = show_1_acs(n, BOTH(ACS_LTEE));
984 n = show_1_acs(n, BOTH(ACS_BTEE));
985 n = show_1_acs(n, BOTH(ACS_TTEE));
986 n = show_1_acs(n, BOTH(ACS_HLINE));
987 n = show_1_acs(n, BOTH(ACS_VLINE));
988 n = show_1_acs(n, BOTH(ACS_PLUS));
989 n = show_1_acs(n, BOTH(ACS_S1));
990 n = show_1_acs(n, BOTH(ACS_S9));
991 n = show_1_acs(n, BOTH(ACS_DIAMOND));
992 n = show_1_acs(n, BOTH(ACS_CKBOARD));
993 n = show_1_acs(n, BOTH(ACS_DEGREE));
994 n = show_1_acs(n, BOTH(ACS_PLMINUS));
995 n = show_1_acs(n, BOTH(ACS_BULLET));
996 n = show_1_acs(n, BOTH(ACS_LARROW));
997 n = show_1_acs(n, BOTH(ACS_RARROW));
998 n = show_1_acs(n, BOTH(ACS_DARROW));
999 n = show_1_acs(n, BOTH(ACS_UARROW));
1000 n = show_1_acs(n, BOTH(ACS_BOARD));
1001 n = show_1_acs(n, BOTH(ACS_LANTERN));
1002 n = show_1_acs(n, BOTH(ACS_BLOCK));
1003 n = show_1_acs(n, BOTH(ACS_S3));
1004 n = show_1_acs(n, BOTH(ACS_S7));
1005 n = show_1_acs(n, BOTH(ACS_LEQUAL));
1006 n = show_1_acs(n, BOTH(ACS_GEQUAL));
1007 n = show_1_acs(n, BOTH(ACS_PI));
1008 n = show_1_acs(n, BOTH(ACS_NEQUAL));
1009 n = show_1_acs(n, BOTH(ACS_STERLING));
1026 show_upper_chars((c - '0') * 32 + 128);
1029 mvprintw(LINES - 3, 0,
1030 "Note: ANSI terminals may not display C1 characters.");
1031 mvprintw(LINES - 2, 0,
1032 "Select: a=ACS, 0=C1, 1,2,3=GR characters, q=quit");
1034 } while ((c = Getchar()) != 'x' && c != 'q');
1042 * Graphic-rendition test (adapted from vttest)
1045 test_sgr_attributes(void)
1049 for (pass = 0; pass < 2; pass++) {
1050 int normal = ((pass == 0 ? A_NORMAL : A_REVERSE)) | BLANK;
1052 /* Use non-default colors if possible to exercise bce a little */
1054 init_pair(1, COLOR_WHITE, COLOR_BLUE);
1055 normal |= COLOR_PAIR(1);
1059 mvprintw(1, 20, "Graphic rendition test pattern:");
1061 mvprintw(4, 1, "vanilla");
1063 #define set_sgr(mask) bkgdset((normal^(mask)));
1065 mvprintw(4, 40, "bold");
1067 set_sgr(A_UNDERLINE);
1068 mvprintw(6, 6, "underline");
1070 set_sgr(A_BOLD | A_UNDERLINE);
1071 mvprintw(6, 45, "bold underline");
1074 mvprintw(8, 1, "blink");
1076 set_sgr(A_BLINK | A_BOLD);
1077 mvprintw(8, 40, "bold blink");
1079 set_sgr(A_UNDERLINE | A_BLINK);
1080 mvprintw(10, 6, "underline blink");
1082 set_sgr(A_BOLD | A_UNDERLINE | A_BLINK);
1083 mvprintw(10, 45, "bold underline blink");
1086 mvprintw(12, 1, "negative");
1088 set_sgr(A_BOLD | A_REVERSE);
1089 mvprintw(12, 40, "bold negative");
1091 set_sgr(A_UNDERLINE | A_REVERSE);
1092 mvprintw(14, 6, "underline negative");
1094 set_sgr(A_BOLD | A_UNDERLINE | A_REVERSE);
1095 mvprintw(14, 45, "bold underline negative");
1097 set_sgr(A_BLINK | A_REVERSE);
1098 mvprintw(16, 1, "blink negative");
1100 set_sgr(A_BOLD | A_BLINK | A_REVERSE);
1101 mvprintw(16, 40, "bold blink negative");
1103 set_sgr(A_UNDERLINE | A_BLINK | A_REVERSE);
1104 mvprintw(18, 6, "underline blink negative");
1106 set_sgr(A_BOLD | A_UNDERLINE | A_BLINK | A_REVERSE);
1107 mvprintw(18, 45, "bold underline blink negative");
1110 mvprintw(LINES - 2, 1, "%s background. ", pass == 0 ? "Dark" :
1116 bkgdset(A_NORMAL | BLANK);
1121 /****************************************************************************
1123 * Windows and scrolling tester.
1125 ****************************************************************************/
1127 #define BOTLINES 4 /* number of line stolen from screen bottom */
1133 #define FRAME struct frame
1142 /* We need to know if these flags are actually set, so don't look in FRAME.
1143 * These names are known to work with SVr4 curses as well as ncurses.
1146 HaveKeypad(FRAME * curp)
1148 WINDOW *win = (curp ? curp->wind : stdscr);
1149 return win->_use_keypad;
1153 HaveScroll(FRAME * curp)
1155 WINDOW *win = (curp ? curp->wind : stdscr);
1156 return win->_scroll;
1160 newwin_legend(FRAME * curp)
1162 static const struct {
1167 "^C = create window", 0
1170 "^N = next window", 0
1173 "^P = previous window", 0
1176 "^F = scroll forward", 0
1179 "^B = scroll backward", 0
1182 "^K = keypad(%s)", 1
1185 "^S = scrollok(%s)", 2
1188 "^W = save window to file", 0
1191 "^R = restore window", 0
1204 bool do_keypad = HaveKeypad(curp);
1205 bool do_scroll = HaveScroll(curp);
1209 for (n = 0; n < SIZEOF(legend); n++) {
1210 switch (legend[n].code) {
1212 strcpy(buf, legend[n].msg);
1215 sprintf(buf, legend[n].msg, do_keypad ? "yes" : "no");
1218 sprintf(buf, legend[n].msg, do_scroll ? "yes" : "no");
1221 sprintf(buf, legend[n].msg, do_keypad ? "/ESC" : "");
1224 getyx(stdscr, y, x);
1225 addstr((COLS < (x + 3 + (int) strlen(buf))) ? "\n" : (n ? ", " : ""));
1232 transient(FRAME * curp, NCURSES_CONST char *msg)
1234 newwin_legend(curp);
1236 mvaddstr(LINES - 1, 0, msg);
1242 printw("%s characters are echoed, window should %sscroll.",
1243 HaveKeypad(curp) ? "Non-arrow" : "All other",
1244 HaveScroll(curp) ? "" : "not ");
1249 newwin_report(FRAME * curp)
1250 /* report on the cursor's current position, then restore it */
1252 WINDOW *win = (curp != 0) ? curp->wind : stdscr;
1256 transient(curp, (char *) 0);
1258 move(LINES - 1, COLS - 17);
1259 printw("Y = %2d X = %2d", y, x);
1267 selectcell(int uli, int ulj, int lri, int lrj)
1268 /* arrows keys move cursor, return location at current on non-arrow key */
1270 static pair res; /* result cell */
1271 int si = lri - uli + 1; /* depth of the select area */
1272 int sj = lrj - ulj + 1; /* width of the select area */
1273 int i = 0, j = 0; /* offsets into the select area */
1278 move(uli + i, ulj + j);
1279 newwin_report((FRAME *) 0);
1281 switch (Getchar()) {
1296 return ((pair *) 0);
1297 #ifdef NCURSES_MOUSE_VERSION
1303 if (event.y > uli && event.x > ulj) {
1324 outerbox(pair ul, pair lr, bool onoff)
1325 /* draw or erase a box *outside* the given pair of corners */
1327 mvaddch(ul.y - 1, lr.x - 1, onoff ? ACS_ULCORNER : ' ');
1328 mvaddch(ul.y - 1, lr.x + 1, onoff ? ACS_URCORNER : ' ');
1329 mvaddch(lr.y + 1, lr.x + 1, onoff ? ACS_LRCORNER : ' ');
1330 mvaddch(lr.y + 1, ul.x - 1, onoff ? ACS_LLCORNER : ' ');
1331 move(ul.y - 1, ul.x);
1332 hline(onoff ? ACS_HLINE : ' ', lr.x - ul.x + 1);
1333 move(ul.y, ul.x - 1);
1334 vline(onoff ? ACS_VLINE : ' ', lr.y - ul.y + 1);
1335 move(lr.y + 1, ul.x);
1336 hline(onoff ? ACS_HLINE : ' ', lr.x - ul.x + 1);
1337 move(ul.y, lr.x + 1);
1338 vline(onoff ? ACS_VLINE : ' ', lr.y - ul.y + 1);
1343 /* Ask user for a window definition */
1350 addstr("Use arrows to move cursor, anything else to mark corner 1");
1352 if ((tmp = selectcell(2, 1, LINES - BOTLINES - 2, COLS - 2)) == (pair *) 0)
1353 return ((WINDOW *) 0);
1354 memcpy(&ul, tmp, sizeof(pair));
1355 mvaddch(ul.y - 1, ul.x - 1, ACS_ULCORNER);
1358 addstr("Use arrows to move cursor, anything else to mark corner 2");
1360 if ((tmp = selectcell(ul.y, ul.x, LINES - BOTLINES - 2, COLS - 2)) ==
1362 return ((WINDOW *) 0);
1363 memcpy(&lr, tmp, sizeof(pair));
1365 rwindow = subwin(stdscr, lr.y - ul.y + 1, lr.x - ul.x + 1, ul.y, ul.x);
1367 outerbox(ul, lr, TRUE);
1378 newwin_move(FRAME * curp, int dy, int dx)
1380 WINDOW *win = (curp != 0) ? curp->wind : stdscr;
1384 getyx(win, cur_y, cur_x);
1385 getmaxyx(win, max_y, max_x);
1386 if ((cur_x += dx) < 0)
1388 else if (cur_x >= max_x)
1390 if ((cur_y += dy) < 0)
1392 else if (cur_y >= max_y)
1394 wmove(win, cur_y, cur_x);
1398 delete_framed(FRAME * fp, bool showit)
1402 fp->last->next = fp->next;
1403 fp->next->last = fp->last;
1411 np = (fp == fp->next) ? 0 : fp->next;
1417 acs_and_scroll(void)
1418 /* Demonstrate windows */
1422 FRAME *current = (FRAME *) 0, *neww;
1423 WINDOW *usescr = stdscr;
1425 #define DUMPFILE "screendump"
1427 #ifdef NCURSES_MOUSE_VERSION
1428 mousemask(BUTTON1_CLICKED, (mmask_t *) 0);
1433 transient((FRAME *) 0, (char *) 0);
1436 neww = (FRAME *) calloc(1, sizeof(FRAME));
1437 if ((neww->wind = getwindow()) == (WINDOW *) 0)
1440 if (current == 0) { /* First element, */
1441 neww->next = neww; /* so point it at itself */
1444 neww->next = current->next;
1445 neww->last = current;
1446 neww->last->next = neww;
1447 neww->next->last = neww;
1450 /* SVr4 curses sets the keypad on all newly-created windows to
1451 * false. Someone reported that PDCurses makes new windows inherit
1452 * this flag. Remove the following 'keypad()' call to test this
1454 keypad(current->wind, TRUE);
1455 current->do_keypad = HaveKeypad(current);
1456 current->do_scroll = HaveScroll(current);
1459 case CTRL('N'): /* go to next window */
1461 current = current->next;
1464 case CTRL('P'): /* go to previous window */
1466 current = current->last;
1469 case CTRL('F'): /* scroll current window forward */
1471 wscrl(current->wind, 1);
1474 case CTRL('B'): /* scroll current window backwards */
1476 wscrl(current->wind, -1);
1479 case CTRL('K'): /* toggle keypad mode for current */
1481 current->do_keypad = !current->do_keypad;
1482 keypad(current->wind, current->do_keypad);
1488 current->do_scroll = !current->do_scroll;
1489 scrollok(current->wind, current->do_scroll);
1493 case CTRL('W'): /* save and delete window */
1494 if (current == current->next)
1496 if ((fp = fopen(DUMPFILE, "w")) == (FILE *) 0)
1497 transient(current, "Can't open screen dump file");
1499 (void) putwin(current->wind, fp);
1502 current = delete_framed(current, TRUE);
1506 case CTRL('R'): /* restore window */
1507 if ((fp = fopen(DUMPFILE, "r")) == (FILE *) 0)
1508 transient(current, "Can't open screen dump file");
1510 neww = (FRAME *) calloc(1, sizeof(FRAME));
1512 neww->next = current->next;
1513 neww->last = current;
1514 neww->last->next = neww;
1515 neww->next->last = neww;
1517 neww->wind = getwin(fp);
1520 wrefresh(neww->wind);
1525 case CTRL('X'): /* resize window */
1532 addstr("Use arrows to move cursor, anything else to mark new corner");
1535 getbegyx(current->wind, ul.y, ul.x);
1537 tmp = selectcell(ul.y, ul.x, LINES - BOTLINES - 2, COLS - 2);
1538 if (tmp == (pair *) 0) {
1543 getmaxyx(current->wind, lr.y, lr.x);
1546 outerbox(ul, lr, FALSE);
1547 wnoutrefresh(stdscr);
1549 /* strictly cosmetic hack for the test */
1550 getmaxyx(current->wind, my, mx);
1551 if (my > tmp->y - ul.y) {
1552 getyx(current->wind, lr.y, lr.x);
1553 wmove(current->wind, tmp->y - ul.y + 1, 0);
1554 wclrtobot(current->wind);
1555 wmove(current->wind, lr.y, lr.x);
1557 if (mx > tmp->x - ul.x)
1558 for (i = 0; i < my; i++) {
1559 wmove(current->wind, i, tmp->x - ul.x + 1);
1560 wclrtoeol(current->wind);
1562 wnoutrefresh(current->wind);
1564 memcpy(&lr, tmp, sizeof(pair));
1565 (void) wresize(current->wind, lr.y - ul.y + 0, lr.x - ul.x + 0);
1567 getbegyx(current->wind, ul.y, ul.x);
1568 getmaxyx(current->wind, lr.y, lr.x);
1571 outerbox(ul, lr, TRUE);
1572 wnoutrefresh(stdscr);
1574 wnoutrefresh(current->wind);
1580 #endif /* HAVE_WRESIZE */
1582 case KEY_F(10): /* undocumented --- use this to test area clears */
1583 selectcell(0, 0, LINES - 1, COLS - 1);
1589 newwin_move(current, -1, 0);
1592 newwin_move(current, 1, 0);
1595 newwin_move(current, 0, -1);
1598 newwin_move(current, 0, 1);
1606 getyx(current->wind, y, x);
1610 x = getmaxx(current->wind) - 1;
1612 mvwdelch(current->wind, y, x);
1622 waddch(current->wind, (chtype) c);
1627 newwin_report(current);
1628 usescr = (current ? current->wind : stdscr);
1631 ((c = wGetchar(usescr)) != QUIT
1632 && !((c == ESCAPE) && (usescr->_use_keypad))
1636 while (current != 0)
1637 current = delete_framed(current, FALSE);
1639 scrollok(stdscr, TRUE); /* reset to driver's default */
1640 #ifdef NCURSES_MOUSE_VERSION
1641 mousemask(0, (mmask_t *) 0);
1648 /****************************************************************************
1652 ****************************************************************************/
1655 static unsigned long nap_msec = 1;
1657 static NCURSES_CONST char *mod[] =
1667 /*+-------------------------------------------------------------------------
1669 --------------------------------------------------------------------------*/
1671 wait_a_while(unsigned long msec GCC_UNUSED)
1681 else if (msec > 1000L)
1682 sleep((int) msec / 1000L);
1686 } /* end of wait_a_while */
1688 /*+-------------------------------------------------------------------------
1690 --------------------------------------------------------------------------*/
1692 saywhat(NCURSES_CONST char *text)
1694 wmove(stdscr, LINES - 1, 0);
1696 waddstr(stdscr, text);
1697 } /* end of saywhat */
1699 /*+-------------------------------------------------------------------------
1700 mkpanel(rows,cols,tly,tlx) - alloc a win and panel and associate them
1701 --------------------------------------------------------------------------*/
1703 mkpanel(int color, int rows, int cols, int tly, int tlx)
1708 if ((win = newwin(rows, cols, tly, tlx)) != 0) {
1709 if ((pan = new_panel(win)) == 0) {
1711 } else if (has_colors()) {
1712 int fg = (color == COLOR_BLUE) ? COLOR_WHITE : COLOR_BLACK;
1714 init_pair(color, fg, bg);
1715 wbkgdset(win, COLOR_PAIR(color) | ' ');
1717 wbkgdset(win, A_BOLD | ' ');
1721 } /* end of mkpanel */
1723 /*+-------------------------------------------------------------------------
1725 --------------------------------------------------------------------------*/
1727 rmpanel(PANEL * pan)
1729 WINDOW *win = panel_window(pan);
1732 } /* end of rmpanel */
1734 /*+-------------------------------------------------------------------------
1736 --------------------------------------------------------------------------*/
1742 } /* end of pflush */
1744 /*+-------------------------------------------------------------------------
1746 --------------------------------------------------------------------------*/
1748 fill_panel(PANEL * pan)
1750 WINDOW *win = panel_window(pan);
1751 int num = ((const char *) panel_userptr(pan))[1];
1755 wprintw(win, "-pan%c-", num);
1758 for (y = 2; y < getmaxy(win) - 1; y++) {
1759 for (x = 1; x < getmaxx(win) - 1; x++) {
1764 } /* end of fill_panel */
1774 for (y = 0; y < LINES - 1; y++) {
1775 for (x = 0; x < COLS; x++)
1776 wprintw(stdscr, "%d", (y + x) % 10);
1778 for (y = 0; y < 5; y++) {
1785 p1 = mkpanel(COLOR_RED,
1790 set_panel_userptr(p1, "p1");
1792 p2 = mkpanel(COLOR_GREEN,
1797 set_panel_userptr(p2, "p2");
1799 p3 = mkpanel(COLOR_YELLOW,
1804 set_panel_userptr(p3, "p3");
1806 p4 = mkpanel(COLOR_BLUE,
1811 set_panel_userptr(p4, "p4");
1813 p5 = mkpanel(COLOR_MAGENTA,
1818 set_panel_userptr(p5, "p5");
1828 saywhat("press any key to continue");
1829 wait_a_while(nap_msec);
1831 saywhat("h3 s1 s2 s4 s5; press any key to continue");
1832 move_panel(p1, 0, 0);
1839 wait_a_while(nap_msec);
1841 saywhat("s1; press any key to continue");
1844 wait_a_while(nap_msec);
1846 saywhat("s2; press any key to continue");
1849 wait_a_while(nap_msec);
1851 saywhat("m2; press any key to continue");
1852 move_panel(p2, LINES / 3 + 1, COLS / 8);
1854 wait_a_while(nap_msec);
1859 wait_a_while(nap_msec);
1861 saywhat("m3; press any key to continue");
1862 move_panel(p3, LINES / 4 + 1, COLS / 15);
1864 wait_a_while(nap_msec);
1866 saywhat("b3; press any key to continue");
1869 wait_a_while(nap_msec);
1871 saywhat("s4; press any key to continue");
1874 wait_a_while(nap_msec);
1876 saywhat("s5; press any key to continue");
1879 wait_a_while(nap_msec);
1881 saywhat("t3; press any key to continue");
1884 wait_a_while(nap_msec);
1886 saywhat("t1; press any key to continue");
1889 wait_a_while(nap_msec);
1891 saywhat("t2; press any key to continue");
1894 wait_a_while(nap_msec);
1896 saywhat("t3; press any key to continue");
1899 wait_a_while(nap_msec);
1901 saywhat("t4; press any key to continue");
1904 wait_a_while(nap_msec);
1906 for (itmp = 0; itmp < 6; itmp++) {
1907 WINDOW *w4 = panel_window(p4);
1908 WINDOW *w5 = panel_window(p5);
1910 saywhat("m4; press any key to continue");
1911 wmove(w4, LINES / 8, 1);
1912 waddstr(w4, mod[itmp]);
1913 move_panel(p4, LINES / 6, itmp * (COLS / 8));
1914 wmove(w5, LINES / 6, 1);
1915 waddstr(w5, mod[itmp]);
1917 wait_a_while(nap_msec);
1919 saywhat("m5; press any key to continue");
1920 wmove(w4, LINES / 6, 1);
1921 waddstr(w4, mod[itmp]);
1922 move_panel(p5, LINES / 3 - 1, (itmp * 10) + 6);
1923 wmove(w5, LINES / 8, 1);
1924 waddstr(w5, mod[itmp]);
1926 wait_a_while(nap_msec);
1929 saywhat("m4; press any key to continue");
1930 move_panel(p4, LINES / 6, itmp * (COLS / 8));
1932 wait_a_while(nap_msec);
1934 saywhat("t5; press any key to continue");
1937 wait_a_while(nap_msec);
1939 saywhat("t2; press any key to continue");
1942 wait_a_while(nap_msec);
1944 saywhat("t1; press any key to continue");
1947 wait_a_while(nap_msec);
1949 saywhat("d2; press any key to continue");
1952 wait_a_while(nap_msec);
1954 saywhat("h3; press any key to continue");
1957 wait_a_while(nap_msec);
1959 saywhat("d1; press any key to continue");
1962 wait_a_while(nap_msec);
1964 saywhat("d4; press any key to continue");
1967 wait_a_while(nap_msec);
1969 saywhat("d5; press any key to continue");
1972 wait_a_while(nap_msec);
1982 /****************************************************************************
1986 ****************************************************************************/
1990 static bool pending_pan = FALSE;
1991 static bool show_panner_legend = TRUE;
1994 panner_legend(int line)
1996 static const char *const legend[] =
1998 "Use arrow keys (or U,D,L,R) to pan, q to quit, ! to shell-out.",
1999 "Use +,- (or j,k) to grow/shrink the panner vertically.",
2000 "Use <,> (or h,l) to grow/shrink the panner horizontally.",
2001 "Number repeats. Toggle legend:?, timer:t, scroll mark:s."
2003 int n = (SIZEOF(legend) - (LINES - line));
2004 if (line < LINES && (n >= 0)) {
2006 if (show_panner_legend)
2007 printw("%s", legend[n]);
2009 return show_panner_legend;
2015 panner_h_cleanup(int from_y, int from_x, int to_x)
2017 if (!panner_legend(from_y))
2018 do_h_line(from_y, from_x, ' ', to_x);
2022 panner_v_cleanup(int from_y, int from_x, int to_y)
2024 if (!panner_legend(from_y))
2025 do_v_line(from_y, from_x, ' ', to_y);
2030 int top_x, int top_y, int porty, int portx,
2031 int (*pgetc) (WINDOW *))
2033 #if HAVE_GETTIMEOFDAY
2034 struct timeval before, after;
2037 bool scrollers = TRUE;
2040 int pxmax, pymax, lowend, highend, c;
2042 getmaxyx(pad, pymax, pxmax);
2043 scrollok(stdscr, FALSE); /* we don't want stdscr to scroll! */
2047 #ifdef NCURSES_VERSION
2049 * During shell-out, the user may have resized the window. Adjust
2050 * the port size of the pad to accommodate this. Ncurses automatically
2051 * resizes all of the normal windows to fit on the new screen.
2069 show_panner_legend = !show_panner_legend;
2070 panner_legend(LINES - 4);
2071 panner_legend(LINES - 3);
2072 panner_legend(LINES - 2);
2073 panner_legend(LINES - 1);
2075 #if HAVE_GETTIMEOFDAY
2079 panner_legend(LINES - 1);
2083 scrollers = !scrollers;
2086 /* Move the top-left corner of the pad, keeping the bottom-right
2089 case 'h': /* increase-columns: move left edge to left */
2093 panner_v_cleanup(top_y, top_x, porty);
2098 case 'j': /* decrease-lines: move top-edge down */
2102 panner_h_cleanup(top_y - 1, top_x - (top_x > 0), portx);
2107 case 'k': /* increase-lines: move top-edge up */
2112 panner_h_cleanup(top_y, top_x, portx);
2116 case 'l': /* decrease-columns: move left-edge to right */
2120 panner_v_cleanup(top_y - (top_y > 0), top_x - 1, porty);
2125 /* Move the bottom-right corner of the pad, keeping the top-left
2128 case KEY_IC: /* increase-columns: move right-edge to right */
2129 if (portx >= pxmax || portx >= COLS)
2132 panner_v_cleanup(top_y - (top_y > 0), portx - 1, porty);
2137 case KEY_IL: /* increase-lines: move bottom-edge down */
2138 if (porty >= pymax || porty >= LINES)
2141 panner_h_cleanup(porty - 1, top_x - (top_x > 0), portx);
2146 case KEY_DC: /* decrease-columns: move bottom edge up */
2151 panner_v_cleanup(top_y - (top_y > 0), portx, porty);
2155 case KEY_DL: /* decrease-lines */
2160 panner_h_cleanup(porty, top_x - (top_x > 0), portx);
2164 case KEY_LEFT: /* pan leftwards */
2171 case KEY_RIGHT: /* pan rightwards */
2172 if (basex + portx - (pymax > porty) < pxmax)
2178 case KEY_UP: /* pan upwards */
2185 case KEY_DOWN: /* pan downwards */
2186 if (basey + porty - (pxmax > portx) < pymax)
2201 basey = pymax - porty;
2211 mvaddch(top_y - 1, top_x - 1, ACS_ULCORNER);
2212 do_v_line(top_y, top_x - 1, ACS_VLINE, porty);
2213 do_h_line(top_y - 1, top_x, ACS_HLINE, portx);
2215 if (scrollers && (pxmax > portx - 1)) {
2216 int length = (portx - top_x - 1);
2217 float ratio = ((float) length) / ((float) pxmax);
2219 lowend = (int) (top_x + (basex * ratio));
2220 highend = (int) (top_x + ((basex + length) * ratio));
2222 do_h_line(porty - 1, top_x, ACS_HLINE, lowend);
2223 if (highend < portx) {
2225 do_h_line(porty - 1, lowend, ' ', highend + 1);
2227 do_h_line(porty - 1, highend + 1, ACS_HLINE, portx);
2230 do_h_line(porty - 1, top_x, ACS_HLINE, portx);
2232 if (scrollers && (pymax > porty - 1)) {
2233 int length = (porty - top_y - 1);
2234 float ratio = ((float) length) / ((float) pymax);
2236 lowend = (int) (top_y + (basey * ratio));
2237 highend = (int) (top_y + ((basey + length) * ratio));
2239 do_v_line(top_y, portx - 1, ACS_VLINE, lowend);
2240 if (highend < porty) {
2242 do_v_line(lowend, portx - 1, ' ', highend + 1);
2244 do_v_line(highend + 1, portx - 1, ACS_VLINE, porty);
2247 do_v_line(top_y, portx - 1, ACS_VLINE, porty);
2249 mvaddch(top_y - 1, portx - 1, ACS_URCORNER);
2250 mvaddch(porty - 1, top_x - 1, ACS_LLCORNER);
2251 mvaddch(porty - 1, portx - 1, ACS_LRCORNER);
2254 #if HAVE_GETTIMEOFDAY
2255 gettimeofday(&before, 0);
2257 wnoutrefresh(stdscr);
2262 porty - (pxmax > portx) - 1,
2263 portx - (pymax > porty) - 1);
2266 #if HAVE_GETTIMEOFDAY
2269 gettimeofday(&after, 0);
2270 elapsed = (after.tv_sec + after.tv_usec / 1.0e6)
2271 - (before.tv_sec + before.tv_usec / 1.0e6);
2272 move(LINES - 1, COLS - 20);
2273 printw("Secs: %2.03f", elapsed);
2280 ((c = pgetc(pad)) != KEY_EXIT);
2282 scrollok(stdscr, TRUE); /* reset to driver's default */
2286 padgetch(WINDOW *win)
2292 if ((pending_pan = (count > 0)) != FALSE) {
2294 pending_pan = (count != 0);
2297 switch (c = wGetchar(win)) {
2333 case ERR: /* FALLTHRU */
2339 if (c >= '0' && c <= '9') {
2340 count = count * 10 + (c - '0');
2354 #define PAD_HIGH 200
2355 #define PAD_WIDE 200
2359 /* Demonstrate pads. */
2362 unsigned gridcount = 0;
2363 WINDOW *panpad = newpad(PAD_HIGH, PAD_WIDE);
2366 Cannot("cannot create requested pad");
2370 for (i = 0; i < PAD_HIGH; i++) {
2371 for (j = 0; j < PAD_WIDE; j++)
2372 if (i % GRIDSIZE == 0 && j % GRIDSIZE == 0) {
2373 if (i == 0 || j == 0)
2374 waddch(panpad, '+');
2376 waddch(panpad, (chtype) ('A' + (gridcount++ % 26)));
2377 } else if (i % GRIDSIZE == 0)
2378 waddch(panpad, '-');
2379 else if (j % GRIDSIZE == 0)
2380 waddch(panpad, '|');
2382 waddch(panpad, ' ');
2384 panner_legend(LINES - 4);
2385 panner_legend(LINES - 3);
2386 panner_legend(LINES - 2);
2387 panner_legend(LINES - 1);
2389 keypad(panpad, TRUE);
2391 /* Make the pad (initially) narrow enough that a trace file won't wrap.
2392 * We'll still be able to widen it during a test, since that's required
2393 * for testing boundaries.
2395 panner(panpad, 2, 2, LINES - 5, COLS - 15, padgetch);
2401 #endif /* USE_LIBPANEL */
2403 /****************************************************************************
2405 * Tests from John Burnell's PDCurses tester
2407 ****************************************************************************/
2410 Continue(WINDOW *win)
2414 mvwaddstr(win, 10, 1, " Press any key to continue");
2420 flushinp_test(WINDOW *win)
2421 /* Input test, adapted from John Burnell's PDCurses tester */
2423 int w, h, bx, by, sw, sh, i;
2428 getmaxyx(win, h, w);
2429 getbegyx(win, by, bx);
2432 if ((subWin = subwin(win, sh, sw, by + h - sh - 2, bx + w - sw - 2)) == 0)
2437 init_pair(2, COLOR_CYAN, COLOR_BLUE);
2438 wbkgd(subWin, COLOR_PAIR(2) | ' ');
2441 wattrset(subWin, A_BOLD);
2442 box(subWin, ACS_VLINE, ACS_HLINE);
2443 mvwaddstr(subWin, 2, 1, "This is a subwindow");
2447 * This used to set 'nocbreak()'. However, Alexander Lukyanov says that
2448 * it only happened to "work" on SVr4 because that implementation does not
2449 * emulate nocbreak+noecho mode, whereas ncurses does. To get the desired
2450 * test behavior, we're using 'cbreak()', which will allow a single
2451 * character to return without needing a newline. - T.Dickey 1997/10/11.
2454 mvwaddstr(win, 0, 1, "This is a test of the flushinp() call.");
2456 mvwaddstr(win, 2, 1, "Type random keys for 5 seconds.");
2457 mvwaddstr(win, 3, 1,
2458 "These should be discarded (not echoed) after the subwindow goes away.");
2461 for (i = 0; i < 5; i++) {
2462 mvwprintw(subWin, 1, 1, "Time = %d", i);
2474 mvwaddstr(win, 2, 1,
2475 "If you were still typing when the window timer expired,");
2476 mvwaddstr(win, 3, 1,
2477 "or else you typed nothing at all while it was running,");
2478 mvwaddstr(win, 4, 1,
2479 "test was invalid. You'll see garbage or nothing at all. ");
2480 mvwaddstr(win, 6, 1, "Press a key");
2486 mvwaddstr(win, 12, 0,
2487 "If you see any key other than what you typed, flushinp() is broken.");
2496 "What you typed should now have been deleted; if not, wdelch() failed.");
2502 /****************************************************************************
2506 ****************************************************************************/
2514 menu_virtualize(int c)
2516 if (c == '\n' || c == KEY_EXIT)
2517 return (MAX_COMMAND + 1);
2519 return (REQ_SCR_ULINE);
2521 return (REQ_SCR_DLINE);
2522 else if (c == 'b' || c == KEY_NPAGE)
2523 return (REQ_SCR_UPAGE);
2524 else if (c == 'f' || c == KEY_PPAGE)
2525 return (REQ_SCR_DPAGE);
2526 else if (c == 'n' || c == KEY_DOWN)
2527 return (REQ_NEXT_ITEM);
2528 else if (c == 'p' || c == KEY_UP)
2529 return (REQ_PREV_ITEM);
2531 return (REQ_TOGGLE_ITEM);
2539 static const char *animals[] =
2541 "Lions", "Tigers", "Bears", "(Oh my!)", "Newts", "Platypi", "Lemurs",
2549 ITEM *items[SIZEOF(animals)];
2552 int mrows, mcols, c;
2555 #ifdef NCURSES_MOUSE_VERSION
2556 mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0);
2558 mvaddstr(0, 0, "This is the menu test:");
2559 mvaddstr(2, 0, " Use up and down arrow to move the select bar.");
2560 mvaddstr(3, 0, " 'n' and 'p' act like arrows.");
2562 " 'b' and 'f' scroll up/down (page), 'u' and 'd' (line).");
2563 mvaddstr(5, 0, " Press return to exit.");
2566 for (ap = animals; *ap; ap++)
2567 *ip++ = new_item(*ap, "");
2570 m = new_menu(items);
2572 set_menu_format(m, (SIZEOF(animals) + 1) / 2, 1);
2573 scale_menu(m, &mrows, &mcols);
2575 menuwin = newwin(mrows + 2, mcols + 2, MENU_Y, MENU_X);
2576 set_menu_win(m, menuwin);
2577 keypad(menuwin, TRUE);
2580 set_menu_sub(m, derwin(menuwin, mrows, mcols, 1, 1));
2584 while ((c = menu_driver(m, menu_virtualize(wGetchar(menuwin)))) != E_UNKNOWN_COMMAND) {
2585 if (c == E_REQUEST_DENIED)
2590 (void) mvprintw(LINES - 2, 0,
2591 "You chose: %s\n", item_name(current_item(m)));
2592 (void) addstr("Press any key to continue...");
2599 for (ip = items; *ip; ip++)
2601 #ifdef NCURSES_MOUSE_VERSION
2602 mousemask(0, (mmask_t *) 0);
2607 #define T_TBL(name) { #name, name }
2613 T_TBL(TRACE_DISABLE),
2616 T_TBL(TRACE_UPDATE),
2618 T_TBL(TRACE_CHARPUT),
2619 T_TBL(TRACE_ORDINARY),
2621 T_TBL(TRACE_VIRTPUT),
2622 T_TBL(TRACE_IEVENT),
2624 T_TBL(TRACE_ICALLS),
2625 T_TBL(TRACE_CCALLS),
2626 T_TBL(TRACE_DATABASE),
2628 T_TBL(TRACE_MAXIMUM),
2635 tracetrace(int tlevel)
2642 for (n = 0; t_tbl[n].name != 0; n++)
2643 need += strlen(t_tbl[n].name) + 2;
2644 buf = (char *) malloc(need);
2646 sprintf(buf, "0x%02x = {", tlevel);
2648 sprintf(buf + strlen(buf), "%s, ", t_tbl[0].name);
2650 for (n = 1; t_tbl[n].name != 0; n++)
2651 if ((tlevel & t_tbl[n].mask) == t_tbl[n].mask) {
2652 strcat(buf, t_tbl[n].name);
2656 if (buf[strlen(buf) - 2] == ',')
2657 buf[strlen(buf) - 2] = '\0';
2658 return (strcat(buf, "}"));
2661 /* fake a dynamically reconfigurable menu using the 0th entry to deselect
2665 run_trace_menu(MENU * m)
2671 bool changed = FALSE;
2672 switch (menu_driver(m, menu_virtualize(wGetchar(menu_win(m))))) {
2673 case E_UNKNOWN_COMMAND:
2676 items = menu_items(m);
2677 i = current_item(m);
2678 if (i == items[0]) {
2679 if (item_value(i)) {
2680 for (p = items + 1; *p != 0; p++)
2681 if (item_value(*p)) {
2682 set_item_value(*p, FALSE);
2687 for (p = items + 1; *p != 0; p++)
2688 if (item_value(*p)) {
2689 set_item_value(items[0], FALSE);
2702 /* interactively set the trace level */
2705 ITEM *items[SIZEOF(t_tbl)];
2707 int mrows, mcols, newtrace;
2711 mvaddstr(0, 0, "Interactively set trace level:");
2712 mvaddstr(2, 0, " Press space bar to toggle a selection.");
2713 mvaddstr(3, 0, " Use up and down arrow to move the select bar.");
2714 mvaddstr(4, 0, " Press return to set the trace level.");
2715 mvprintw(6, 0, "(Current trace level is %s)", tracetrace(_nc_tracing));
2719 for (n = 0; t_tbl[n].name != 0; n++)
2720 *ip++ = new_item(t_tbl[n].name, "");
2723 m = new_menu(items);
2725 set_menu_format(m, 0, 2);
2726 scale_menu(m, &mrows, &mcols);
2728 menu_opts_off(m, O_ONEVALUE);
2729 menuwin = newwin(mrows + 2, mcols + 2, MENU_Y, MENU_X);
2730 set_menu_win(m, menuwin);
2731 keypad(menuwin, TRUE);
2734 set_menu_sub(m, derwin(menuwin, mrows, mcols, 1, 1));
2738 for (ip = menu_items(m); *ip; ip++) {
2739 int mask = t_tbl[item_index(*ip)].mask;
2741 set_item_value(*ip, _nc_tracing == 0);
2742 else if ((mask & _nc_tracing) == mask)
2743 set_item_value(*ip, TRUE);
2746 while (run_trace_menu(m))
2750 for (ip = menu_items(m); *ip; ip++)
2751 if (item_value(*ip))
2752 newtrace |= t_tbl[item_index(*ip)].mask;
2754 _tracef("trace level interactively set to %s", tracetrace(_nc_tracing));
2756 (void) mvprintw(LINES - 2, 0,
2757 "Trace level is %s\n", tracetrace(_nc_tracing));
2758 (void) addstr("Press any key to continue...");
2765 for (ip = items; *ip; ip++)
2769 #endif /* USE_LIBMENU */
2771 /****************************************************************************
2775 ****************************************************************************/
2778 make_label(int frow, int fcol, NCURSES_CONST char *label)
2780 FIELD *f = new_field(1, strlen(label), frow, fcol, 0, 0);
2783 set_field_buffer(f, 0, label);
2784 set_field_opts(f, field_opts(f) & ~O_ACTIVE);
2790 make_field(int frow, int fcol, int rows, int cols, bool secure)
2792 FIELD *f = new_field(rows, cols, frow, fcol, 0, secure ? 1 : 0);
2795 set_field_back(f, A_UNDERLINE);
2796 set_field_userptr(f, (void *) 0);
2802 display_form(FORM * f)
2807 scale_form(f, &rows, &cols);
2809 if ((w = newwin(rows + 2, cols + 4, 0, 0)) != (WINDOW *) 0) {
2811 set_form_sub(f, derwin(w, rows, cols, 1, 2));
2816 if (post_form(f) != E_OK)
2821 erase_form(FORM * f)
2823 WINDOW *w = form_win(f);
2824 WINDOW *s = form_sub(f);
2834 edit_secure(FIELD * me, int c)
2836 int rows, cols, frow, fcol, nrow, nbuf;
2838 if (field_info(me, &rows, &cols, &frow, &fcol, &nrow, &nbuf) == E_OK
2843 strcpy(temp, field_buffer(me, 1));
2844 len = (long) (char *) field_userptr(me);
2849 set_field_buffer(me, 1, temp);
2869 case REQ_RIGHT_CHAR:
2871 c = 0; /* we don't want to do inline editing */
2876 set_field_buffer(me, 1, temp);
2883 set_field_buffer(me, 1, temp);
2888 set_field_userptr(me, (void *) len);
2894 form_virtualize(FORM * f, WINDOW *w)
2896 static const struct {
2901 CTRL('A'), REQ_NEXT_CHOICE
2904 CTRL('B'), REQ_PREV_WORD
2907 CTRL('C'), REQ_CLR_EOL
2910 CTRL('D'), REQ_DOWN_FIELD
2913 CTRL('E'), REQ_END_FIELD
2916 CTRL('F'), REQ_NEXT_PAGE
2919 CTRL('G'), REQ_DEL_WORD
2922 CTRL('H'), REQ_DEL_PREV
2925 CTRL('I'), REQ_INS_CHAR
2928 CTRL('K'), REQ_CLR_EOF
2931 CTRL('L'), REQ_LEFT_FIELD
2934 CTRL('M'), REQ_NEW_LINE
2937 CTRL('N'), REQ_NEXT_FIELD
2940 CTRL('O'), REQ_INS_LINE
2943 CTRL('P'), REQ_PREV_FIELD
2946 CTRL('R'), REQ_RIGHT_FIELD
2949 CTRL('S'), REQ_BEG_FIELD
2952 CTRL('U'), REQ_UP_FIELD
2955 CTRL('V'), REQ_DEL_CHAR
2958 CTRL('W'), REQ_NEXT_WORD
2961 CTRL('X'), REQ_CLR_FIELD
2964 CTRL('Y'), REQ_DEL_LINE
2967 CTRL('Z'), REQ_PREV_CHOICE
2970 ESCAPE, MAX_FORM_COMMAND + 1
2973 KEY_BACKSPACE, REQ_DEL_PREV
2976 KEY_DOWN, REQ_DOWN_CHAR
2979 KEY_END, REQ_LAST_FIELD
2982 KEY_HOME, REQ_FIRST_FIELD
2985 KEY_LEFT, REQ_LEFT_CHAR
2988 KEY_LL, REQ_LAST_FIELD
2991 KEY_NEXT, REQ_NEXT_FIELD
2994 KEY_NPAGE, REQ_NEXT_PAGE
2997 KEY_PPAGE, REQ_PREV_PAGE
3000 KEY_PREVIOUS, REQ_PREV_FIELD
3003 KEY_RIGHT, REQ_RIGHT_CHAR
3009 QUIT, MAX_FORM_COMMAND + 1
3013 static int mode = REQ_INS_MODE;
3014 int c = wGetchar(w);
3016 FIELD *me = current_field(f);
3018 if (c == CTRL(']')) {
3019 if (mode == REQ_INS_MODE)
3020 mode = REQ_OVL_MODE;
3022 mode = REQ_INS_MODE;
3025 for (n = 0; n < sizeof(lookup) / sizeof(lookup[0]); n++) {
3026 if (lookup[n].code == c) {
3027 c = lookup[n].result;
3034 * Force the field that the user is typing into to be in reverse video,
3035 * while the other fields are shown underlined.
3038 c = edit_secure(me, c);
3039 set_field_back(me, A_REVERSE);
3040 } else if (c <= MAX_FORM_COMMAND) {
3041 c = edit_secure(me, c);
3042 set_field_back(me, A_UNDERLINE);
3048 my_form_driver(FORM * form, int c)
3050 if (c == (MAX_FORM_COMMAND + 1)
3051 && form_driver(form, REQ_VALIDATION) == E_OK)
3064 FIELD *f[12], *secure;
3065 int finished = 0, c;
3069 addstr("Defined form-traversal keys: ^Q/ESC- exit form\n");
3070 addstr("^N -- go to next field ^P -- go to previous field\n");
3071 addstr("Home -- go to first field End -- go to last field\n");
3072 addstr("^L -- go to field to left ^R -- go to field to right\n");
3073 addstr("^U -- move upward to field ^D -- move downward to field\n");
3074 addstr("^W -- go to next word ^B -- go to previous word\n");
3075 addstr("^S -- go to start of field ^E -- go to end of field\n");
3076 addstr("^H -- delete previous char ^Y -- delete line\n");
3077 addstr("^G -- delete current word ^C -- clear to end of line\n");
3078 addstr("^K -- clear to end of field ^X -- clear field\n");
3079 addstr("Arrow keys move within a field as you would expect.");
3081 mvaddstr(4, 57, "Forms Entry Test");
3085 /* describe the form */
3086 f[n++] = make_label(0, 15, "Sample Form");
3087 f[n++] = make_label(2, 0, "Last Name");
3088 f[n++] = make_field(3, 0, 1, 18, FALSE);
3089 f[n++] = make_label(2, 20, "First Name");
3090 f[n++] = make_field(3, 20, 1, 12, FALSE);
3091 f[n++] = make_label(2, 34, "Middle Name");
3092 f[n++] = make_field(3, 34, 1, 12, FALSE);
3093 f[n++] = make_label(5, 0, "Comments");
3094 f[n++] = make_field(6, 0, 4, 46, FALSE);
3095 f[n++] = make_label(5, 20, "Password:");
3097 f[n++] = make_field(5, 30, 1, 9, TRUE);
3098 f[n++] = (FIELD *) 0;
3106 nonl(); /* lets us read ^M's */
3108 switch (form_driver(form, c = form_virtualize(form, w))) {
3110 mvaddstr(5, 57, field_buffer(secure, 1));
3114 case E_UNKNOWN_COMMAND:
3115 finished = my_form_driver(form, c);
3126 for (c = 0; f[c] != 0; c++)
3131 #endif /* USE_LIBFORM */
3133 /****************************************************************************
3137 ****************************************************************************/
3140 fillwin(WINDOW *win, char ch)
3145 getmaxyx(win, y1, x1);
3146 for (y = 0; y < y1; y++) {
3148 for (x = 0; x < x1; x++)
3154 crosswin(WINDOW *win, char ch)
3159 getmaxyx(win, y1, x1);
3160 for (y = 0; y < y1; y++) {
3161 for (x = 0; x < x1; x++)
3162 if (((x > (x1 - 1) / 3) && (x <= (2 * (x1 - 1)) / 3))
3163 || (((y > (y1 - 1) / 3) && (y <= (2 * (y1 - 1)) / 3)))) {
3172 /* test effects of overlapping windows */
3176 WINDOW *win1 = newwin(9, 20, 3, 3);
3177 WINDOW *win2 = newwin(9, 20, 9, 16);
3182 printw("This test shows the behavior of wnoutrefresh() with respect to\n");
3183 printw("the shared region of two overlapping windows A and B. The cross\n");
3184 printw("pattern in each window does not overlap the other.\n");
3187 printw("a = refresh A, then B, then doupdate. b = refresh B, then A, then doupdaute\n");
3188 printw("c = fill window A with letter A. d = fill window B with letter B.\n");
3189 printw("e = cross pattern in window A. f = cross pattern in window B.\n");
3190 printw("g = clear window A. h = clear window B.\n");
3191 printw("i = overwrite A onto B. j = overwrite B onto A.\n");
3192 printw("^Q/ESC = terminate test.");
3194 while ((ch = Getchar()) != QUIT && ch != ESCAPE)
3196 case 'a': /* refresh window A first, then B */
3202 case 'b': /* refresh window B first, then A */
3208 case 'c': /* fill window A so it's visible */
3212 case 'd': /* fill window B so it's visible */
3216 case 'e': /* cross test pattern in window A */
3217 crosswin(win1, 'A');
3220 case 'f': /* cross test pattern in window A */
3221 crosswin(win2, 'B');
3224 case 'g': /* clear window A */
3229 case 'h': /* clear window B */
3234 case 'i': /* overwrite A onto B */
3235 overwrite(win1, win2);
3238 case 'j': /* overwrite B onto A */
3239 overwrite(win2, win1);
3249 /****************************************************************************
3253 ****************************************************************************/
3256 do_single_test(const char c)
3257 /* perform a single specified test */
3270 Cannot("does not support color.");
3277 Cannot("does not support color.");
3278 else if (!can_change_color())
3279 Cannot("has hardwired color values.");
3303 flushinp_test(stdscr);
3307 test_sgr_attributes();
3332 #if USE_LIBMENU && defined(TRACE)
3351 static const char *const tbl[] =
3353 "Usage: ncurses [options]"
3356 #ifdef NCURSES_VERSION
3357 ," -a f,b set default-colors (assumed white-on-black)"
3358 ," -d use default-colors if terminal supports them"
3360 ," -e fmt specify format for soft-keys test (e)"
3361 ," -f rip-off footer line (can repeat)"
3362 ," -h rip-off header line (can repeat)"
3363 ," -s msec specify nominal time for panel-demo (default: 1, to hold)"
3365 ," -t mask specify default trace-level (may toggle with ^T)"
3369 for (n = 0; n < sizeof(tbl) / sizeof(tbl[0]); n++)
3370 fprintf(stderr, "%s\n", tbl[n]);
3375 set_terminal_modes(void)
3380 scrollok(stdscr, TRUE);
3381 idlok(stdscr, TRUE);
3382 keypad(stdscr, TRUE);
3387 announce_sig(int sig)
3389 (void) fprintf(stderr, "Handled signal %d\r\n", sig);
3394 rip_footer(WINDOW *win, int columns)
3396 wbkgd(win, A_REVERSE);
3399 wprintw(win, "footer: %d columns", columns);
3405 rip_header(WINDOW *win, int columns)
3407 wbkgd(win, A_REVERSE);
3410 wprintw(win, "header: %d columns", columns);
3415 /*+-------------------------------------------------------------------------
3417 --------------------------------------------------------------------------*/
3420 main(int argc, char *argv[])
3424 #ifdef NCURSES_VERSION
3425 int default_fg = COLOR_WHITE;
3426 int default_bg = COLOR_BLACK;
3427 bool assumed_colors = FALSE;
3428 bool default_colors = FALSE;
3432 setlocale(LC_CTYPE, "");
3435 while ((c = getopt(argc, argv, "a:de:fhs:t:")) != EOF) {
3437 #ifdef NCURSES_VERSION
3439 assumed_colors = TRUE;
3440 sscanf(optarg, "%d,%d", &default_fg, &default_bg);
3443 default_colors = TRUE;
3447 my_e_param = atoi(optarg);
3448 #ifdef NCURSES_VERSION
3449 if (my_e_param > 3) /* allow extended layouts */
3457 ripoffline(-1, rip_footer);
3460 ripoffline(1, rip_header);
3464 nap_msec = atol(optarg);
3469 save_trace = atoi(optarg);
3478 * If there's no menus (unlikely for ncurses!), then we'll have to set
3479 * tracing on initially, just in case the user wants to test something that
3480 * doesn't involve wGetchar.
3483 /* enable debugging */
3487 if (!isatty(fileno(stdin)))
3489 #endif /* USE_LIBMENU */
3492 /* tell it we're going to play with soft keys */
3493 slk_init(my_e_param);
3496 /* set up null signal catcher so we can see what interrupts to getch do */
3497 signal(SIGUSR1, announce_sig);
3500 /* we must initialize the curses data structure only once */
3504 /* tests, in general, will want these modes */
3507 #ifdef NCURSES_VERSION
3509 use_default_colors();
3510 else if (assumed_colors)
3511 assume_default_colors(default_fg, default_bg);
3514 set_terminal_modes();
3518 * Return to terminal mode, so we're guaranteed of being able to
3519 * select terminal commands even if the capabilities are wrong.
3523 #if HAVE_CURSES_VERSION
3524 (void) printf("Welcome to %s. Press ? for help.\n", curses_version());
3525 #elif defined(NCURSES_VERSION_MAJOR) && defined(NCURSES_VERSION_MINOR) && defined(NCURSES_VERSION_PATCH)
3526 (void) printf("Welcome to ncurses %d.%d.%d. Press ? for help.\n",
3527 NCURSES_VERSION_MAJOR,
3528 NCURSES_VERSION_MINOR,
3529 NCURSES_VERSION_PATCH);
3531 (void) puts("Welcome to ncurses. Press ? for help.");
3535 (void) puts("This is the ncurses main menu");
3536 (void) puts("a = keyboard and mouse input test");
3537 (void) puts("b = character attribute test");
3538 (void) puts("c = color test pattern");
3539 (void) puts("d = edit RGB color values");
3540 (void) puts("e = exercise soft keys");
3541 (void) puts("f = display ACS characters");
3542 (void) puts("g = display windows and scrolling");
3543 (void) puts("i = test of flushinp()");
3544 (void) puts("k = display character attributes");
3546 (void) puts("m = menu code test");
3549 (void) puts("o = exercise panels library");
3550 (void) puts("p = exercise pad features");
3551 (void) puts("q = quit");
3554 (void) puts("r = exercise forms code");
3556 (void) puts("s = overlapping-refresh test");
3557 #if USE_LIBMENU && defined(TRACE)
3558 (void) puts("t = set trace level");
3560 (void) puts("? = repeat this command summary");
3562 (void) fputs("> ", stdout);
3563 (void) fflush(stdout); /* necessary under SVr4 curses */
3566 * This used to be an 'fgets()' call. However (on Linux, at least)
3567 * mixing stream I/O and 'read()' (used in the library) causes the
3568 * input stream to be flushed when switching between the two.
3573 if (read(fileno(stdin), &ch, 1) <= 0) {
3577 } else if (command == 0 && !isspace(ch)) {
3579 } else if (ch == '\n' || ch == '\r') {
3582 (void) fputs("> ", stdout);
3583 (void) fflush(stdout);
3587 if (do_single_test(command)) {
3589 * This may be overkill; it's intended to reset everything back
3590 * to the initial terminal modes so that tests don't get in
3594 set_terminal_modes();
3599 if (command == '?') {
3600 (void) puts("This is the ncurses capability tester.");
3602 puts("You may select a test from the main menu by typing the");
3604 puts("key letter of the choice (the letter to left of the =)");
3606 puts("at the > prompt. The commands `x' or `q' will exit.");
3613 ExitProgram(EXIT_SUCCESS);
3616 /* ncurses.c ends here */