missing ncurses sources
[tomato.git] / release / src / router / libncurses / test / testcurs.c
blob5cc62f2df3014115609a1d34fb7ed0cf3ebce0f2
1 /*
2 * This is a test program for the PDCurses screen package for IBM PC type
3 * machines.
5 * This program was written by John Burnell (johnb@kea.am.dsir.govt.nz)
6 * wrs(5/28/93) -- modified to be consistent (perform identically) with either
7 * PDCurses or under Unix System V, R4
9 * $Id: testcurs.c,v 1.43 2010/11/13 21:02:28 tom Exp $
12 #include <test.priv.h>
14 #if defined(XCURSES)
15 char *XCursesProgramName = "testcurs";
16 #endif
18 static int initTest(WINDOW **);
19 static void display_menu(int, int);
20 static void inputTest(WINDOW *);
21 static void introTest(WINDOW *);
22 static void outputTest(WINDOW *);
23 static void padTest(WINDOW *);
24 static void scrollTest(WINDOW *);
25 #if defined(PDCURSES) && !defined(XCURSES)
26 static void resizeTest(WINDOW *);
27 #endif
29 struct commands {
30 NCURSES_CONST char *text;
31 void (*function) (WINDOW *);
33 typedef struct commands COMMAND;
35 static const COMMAND command[] =
37 {"General Test", introTest},
38 {"Pad Test", padTest},
39 #if defined(PDCURSES) && !defined(XCURSES)
40 {"Resize Test", resizeTest},
41 #endif
42 {"Scroll Test", scrollTest},
43 {"Input Test", inputTest},
44 {"Output Test", outputTest}
46 #define MAX_OPTIONS (int) SIZEOF(command)
48 #if !HAVE_STRDUP
49 #define strdup my_strdup
50 static char *
51 strdup(char *s)
53 char *p = typeMalloc(char, strlen(s) + 1);
54 if (p)
55 strcpy(p, s);
56 return (p);
58 #endif /* not HAVE_STRDUP */
60 static int width, height;
62 int
63 main(
64 int argc GCC_UNUSED,
65 char *argv[]GCC_UNUSED)
67 WINDOW *win;
68 int key;
69 int old_option = (-1);
70 int new_option = 0;
71 bool quit = FALSE;
72 int n;
74 setlocale(LC_ALL, "");
76 #ifdef PDCDEBUG
77 PDC_debug("testcurs started\n");
78 #endif
79 if (!initTest(&win))
80 ExitProgram(EXIT_FAILURE);
82 erase();
83 display_menu(old_option, new_option);
84 for (;;) {
85 #ifdef A_COLOR
86 if (has_colors()) {
87 init_pair(1, COLOR_WHITE, COLOR_BLUE);
88 wbkgd(win, COLOR_PAIR(1));
89 } else
90 wbkgd(win, A_REVERSE);
91 #else
92 wbkgd(win, A_REVERSE);
93 #endif
94 werase(win);
96 noecho();
97 keypad(stdscr, TRUE);
98 raw();
99 key = getch();
100 if (key < KEY_MIN && key > 0 && isalpha(key)) {
101 if (islower(key))
102 key = toupper(key);
103 for (n = 0; n < MAX_OPTIONS; ++n) {
104 if (key == command[n].text[0]) {
105 display_menu(old_option, new_option = n);
106 key = ' ';
107 break;
111 switch (key) {
112 case 10:
113 case 13:
114 case KEY_ENTER:
115 erase();
116 refresh();
117 (*command[new_option].function) (win);
118 erase();
119 display_menu(old_option, new_option);
120 break;
121 case KEY_UP:
122 new_option = ((new_option == 0)
123 ? new_option
124 : new_option - 1);
125 display_menu(old_option, new_option);
126 break;
127 case KEY_DOWN:
128 new_option = ((new_option == (MAX_OPTIONS - 1))
129 ? new_option
130 : new_option + 1);
131 display_menu(old_option, new_option);
132 break;
133 case 'Q':
134 case 'q':
135 quit = TRUE;
136 break;
137 default:
138 beep();
139 break;
140 case ' ':
141 break;
143 if (quit == TRUE)
144 break;
147 delwin(win);
149 endwin();
150 #ifdef XCURSES
151 XCursesExit();
152 #endif
153 ExitProgram(EXIT_SUCCESS);
156 static void
157 Continue(WINDOW *win)
159 int y1 = getmaxy(win);
160 int x1 = getmaxx(win);
161 int y0 = y1 < 10 ? y1 : 10;
162 int x0 = 1;
163 chtype save;
165 save = mvwinch(win, y0, x1 - 1);
167 MvWAddStr(win, y0, x0, " Press any key to continue");
168 wclrtoeol(win);
169 getyx(win, y0, x0);
171 MvWAddCh(win, y0, x1 - 1, save);
173 wmove(win, y0, x0);
174 raw();
175 wgetch(win);
178 static int
179 initTest(WINDOW **win)
181 #ifdef PDCDEBUG
182 PDC_debug("initTest called\n");
183 #endif
184 #ifdef TRACE
185 trace(TRACE_MAXIMUM);
186 #endif
187 initscr();
188 #ifdef PDCDEBUG
189 PDC_debug("after initscr()\n");
190 #endif
191 #ifdef A_COLOR
192 if (has_colors())
193 start_color();
194 #endif
195 width = 60;
196 height = 13; /* Create a drawing window */
197 *win = newwin(height, width, (LINES - height) / 2, (COLS - width) / 2);
198 if (*win == NULL) {
199 endwin();
200 return 0;
202 return 1;
205 static void
206 introTest(WINDOW *win)
208 wmove(win, height / 2 - 5, width / 2);
209 wvline(win, ACS_VLINE, 10);
210 wmove(win, height / 2, width / 2 - 10);
211 whline(win, ACS_HLINE, 20);
212 Continue(win);
214 beep();
215 werase(win);
217 box(win, ACS_VLINE, ACS_HLINE);
218 wrefresh(win);
219 cbreak();
220 MvWAddStr(win, 1, 1,
221 "You should have rectangle in the middle of the screen");
222 MvWAddStr(win, 2, 1, "You should have heard a beep");
223 Continue(win);
224 return;
227 static void
228 scrollTest(WINDOW *win)
230 int i;
231 int half;
232 int OldY;
233 NCURSES_CONST char *Message = "The window will now scroll slowly";
235 wclear(win);
236 OldY = getmaxy(win);
237 half = OldY / 2;
238 MvWAddStr(win, OldY - 2, 1, Message);
239 wrefresh(win);
240 scrollok(win, TRUE);
241 for (i = 1; i <= OldY; i++) {
242 napms(600);
243 scroll(win);
244 wrefresh(win);
247 werase(win);
248 for (i = 1; i < OldY; i++) {
249 MvWPrintw(win, i, 1, "Line %d", i);
251 MvWPrintw(win, OldY - 2, 1, "The top of the window will scroll");
252 wmove(win, 1, 1);
253 wsetscrreg(win, 0, half - 1);
254 box(win, ACS_VLINE, ACS_HLINE);
255 wrefresh(win);
256 for (i = 1; i <= half; i++) {
257 napms(600);
258 scroll(win);
259 box(win, ACS_VLINE, ACS_HLINE);
260 wrefresh(win);
263 werase(win);
264 for (i = 1; i < OldY; i++) {
265 MvWPrintw(win, i, 1, "Line %d", i);
267 MvWPrintw(win, 1, 1, "The bottom of the window will scroll");
268 wmove(win, OldY - 2, 1);
269 wsetscrreg(win, half, --OldY);
270 box(win, ACS_VLINE, ACS_HLINE);
271 wrefresh(win);
272 for (i = half; i <= OldY; i++) {
273 napms(600);
274 wscrl(win, -1);
275 box(win, ACS_VLINE, ACS_HLINE);
276 wrefresh(win);
278 wsetscrreg(win, 0, OldY);
281 static void
282 inputTest(WINDOW *win)
284 int answered;
285 int repeat;
286 int w, h, bx, by, sw, sh, i, c, num;
287 char buffer[80];
288 WINDOW *subWin;
289 wclear(win);
291 getmaxyx(win, h, w);
292 getbegyx(win, by, bx);
293 sw = w / 3;
294 sh = h / 3;
295 if ((subWin = subwin(win, sh, sw, by + h - sh - 2, bx + w - sw - 2)) == NULL)
296 return;
298 #ifdef A_COLOR
299 if (has_colors()) {
300 init_pair(2, COLOR_WHITE, COLOR_RED);
301 wbkgd(subWin, COLOR_PAIR(2) | A_BOLD);
302 } else
303 wbkgd(subWin, A_BOLD);
304 #else
305 wbkgd(subWin, A_BOLD);
306 #endif
307 box(subWin, ACS_VLINE, ACS_HLINE);
308 wrefresh(win);
310 nocbreak();
311 MvWAddStr(win, 2, 1, "Press some keys for 5 seconds");
312 MvWAddStr(win, 1, 1, "Pressing ^C should do nothing");
313 wrefresh(win);
315 werase(subWin);
316 box(subWin, ACS_VLINE, ACS_HLINE);
317 for (i = 0; i < 5; i++) {
318 MvWPrintw(subWin, 1, 1, "Time = %d", i);
319 wrefresh(subWin);
320 napms(1000);
321 flushinp();
324 delwin(subWin);
325 werase(win);
326 flash();
327 wrefresh(win);
328 napms(500);
330 MvWAddStr(win, 2, 1, "Press a key, followed by ENTER");
331 wmove(win, 9, 10);
332 wrefresh(win);
333 echo();
334 noraw();
335 wgetch(win);
336 flushinp();
338 wmove(win, 9, 10);
339 wdelch(win);
340 MvWAddStr(win, 4, 1, "The character should now have been deleted");
341 Continue(win);
343 wclear(win);
344 MvWAddStr(win, 1, 1, "Press keys (or mouse buttons) to show their names");
345 MvWAddStr(win, 2, 1, "Press spacebar to finish");
346 wrefresh(win);
348 keypad(win, TRUE);
349 raw();
350 noecho();
352 #if HAVE_TYPEAHEAD
353 typeahead(-1);
354 #endif
356 #if defined(PDCURSES)
357 mouse_set(ALL_MOUSE_EVENTS);
358 #endif
360 for (;;) {
361 wmove(win, 3, 5);
362 c = wgetch(win);
363 wclrtobot(win);
364 if (c >= KEY_MIN)
365 wprintw(win, "Key Pressed: %s", keyname(c));
366 else if (isprint(c))
367 wprintw(win, "Key Pressed: %c", c);
368 else
369 wprintw(win, "Key Pressed: %s", unctrl(UChar(c)));
370 #if defined(PDCURSES)
371 if (c == KEY_MOUSE) {
372 int button = 0;
373 request_mouse_pos();
374 if (BUTTON_CHANGED(1))
375 button = 1;
376 else if (BUTTON_CHANGED(2))
377 button = 2;
378 else if (BUTTON_CHANGED(3))
379 button = 3;
380 else
381 button = 0;
382 wmove(win, 4, 18);
383 wprintw(win, "Button %d: ", button);
384 if (MOUSE_MOVED)
385 wprintw(win, "moved: ");
386 else if ((BUTTON_STATUS(button) & BUTTON_ACTION_MASK) == BUTTON_PRESSED)
387 wprintw(win, "pressed: ");
388 else if ((BUTTON_STATUS(button) & BUTTON_ACTION_MASK) == BUTTON_DOUBLE_CLICKED)
389 wprintw(win, "double: ");
390 else
391 wprintw(win, "released: ");
392 wprintw(win, " Position: Y: %d X: %d", MOUSE_Y_POS, MOUSE_X_POS);
394 #endif
395 wrefresh(win);
396 if (c == ' ')
397 break;
399 #if 0
400 nodelay(win, TRUE);
401 wgetch(win);
402 nodelay(win, FALSE);
403 #endif
404 #if defined(PDCURSES)
405 mouse_set(0L);
406 #endif
407 refresh();
409 repeat = 0;
410 do {
411 static const char *fmt[] =
413 "%d %10s",
414 "%d %[a-zA-Z]s",
415 "%d %[][a-zA-Z]s",
416 "%d %[^0-9]"
418 const char *format = fmt[(unsigned) repeat % SIZEOF(fmt)];
420 wclear(win);
421 MvWAddStr(win, 3, 2, "The window should have moved");
422 MvWAddStr(win, 4, 2,
423 "This text should have appeared without you pressing a key");
424 MvWPrintw(win, 6, 2,
425 "Scanning with format \"%s\"", format);
426 mvwin(win, 2 + 2 * (repeat % 4), 1 + 2 * (repeat % 4));
427 erase();
428 refresh();
429 wrefresh(win);
430 echo();
431 noraw();
432 num = 0;
433 *buffer = 0;
434 answered = mvwscanw(win, 7, 6, strdup(format), &num, buffer);
435 MvWPrintw(win, 8, 6,
436 "String: %s Number: %d (%d values read)",
437 buffer, num, answered);
438 Continue(win);
439 ++repeat;
440 } while (answered > 0);
443 static void
444 outputTest(WINDOW *win)
446 WINDOW *win1;
447 char Buffer[80];
448 chtype ch;
449 int by, bx;
451 #if !HAVE_TIGETSTR
452 #if HAVE_TGETENT
453 char tc_buffer[4096];
454 char tc_parsed[4096];
455 char *area_pointer = tc_parsed;
456 tgetent(tc_buffer, getenv("TERM"));
457 #else
458 #define tgetstr(a,b) 0
459 #endif
460 #endif /* !HAVE_TIGETSTR */
462 nl();
463 wclear(win);
464 MvWAddStr(win, 1, 1,
465 "You should now have a screen in the upper left corner, and this text should have wrapped");
466 mvwin(win, 2, 1);
467 waddstr(win, "\nThis text should be down\n");
468 waddstr(win, "and broken into two here ^");
469 Continue(win);
471 wclear(win);
472 wattron(win, A_BOLD);
473 MvWAddStr(win, 1, 1, "A new window will appear with this text in it");
474 MvWAddStr(win, 8, 1, "Press any key to continue");
475 wrefresh(win);
476 wgetch(win);
478 getbegyx(win, by, bx);
480 if (LINES < 24 || COLS < 75) {
481 MvWAddStr(win, 5, 1,
482 "Some tests have been skipped as they require a");
483 MvWAddStr(win, 6, 1, "display of at least 24 LINES by 75 COLUMNS");
484 Continue(win);
485 } else {
486 win1 = newwin(10, 50, 14, 25);
487 if (win1 == NULL) {
488 endwin();
489 return;
491 #ifdef A_COLOR
492 if (has_colors()) {
493 init_pair(3, COLOR_BLUE, COLOR_WHITE);
494 wbkgd(win1, COLOR_PAIR(3));
495 } else
496 wbkgd(win1, A_NORMAL);
497 #else
498 wbkgd(win1, A_NORMAL);
499 #endif
500 wclear(win1);
501 MvWAddStr(win1, 5, 1,
502 "This text should appear; using overlay option");
503 copywin(win, win1, 0, 0, 0, 0, 9, 49, TRUE);
505 #if defined(PDCURSES) && !defined(XCURSES)
506 box(win1, 0xb3, 0xc4);
507 #else
508 box(win1, ACS_VLINE, ACS_HLINE);
509 #endif
510 wmove(win1, 8, 26);
511 wrefresh(win1);
512 wgetch(win1);
514 wclear(win1);
515 wattron(win1, A_BLINK);
516 MvWAddStr(win1, 4, 1,
517 "This blinking text should appear in only the second window");
518 wattroff(win1, A_BLINK);
519 mvwin(win1, by, bx);
520 overlay(win, win1);
521 mvwin(win1, 14, 25);
522 wmove(win1, 8, 26);
523 wrefresh(win1);
524 wgetch(win1);
525 delwin(win1);
528 clear();
529 wclear(win);
530 wrefresh(win);
531 MvWAddStr(win, 6, 2, "This line shouldn't appear");
532 MvWAddStr(win, 4, 2, "Only half of the next line is visible");
533 MvWAddStr(win, 5, 2, "Only half of the next line is visible");
534 wmove(win, 6, 1);
535 wclrtobot(win);
536 wmove(win, 5, 20);
537 wclrtoeol(win);
538 MvWAddStr(win, 8, 2, "This line also shouldn't appear");
539 wmove(win, 8, 1);
540 wdeleteln(win);
541 Continue(win);
543 wmove(win, 5, 9);
544 ch = winch(win);
546 wclear(win);
547 wmove(win, 6, 2);
548 waddstr(win, "The next char should be l: ");
549 winsch(win, ch);
550 Continue(win);
552 #if HAVE_WINSSTR
553 (void) mvwinsstr(win, 6, 2, "A1B2C3D4E5");
554 Continue(win);
555 #endif
557 wmove(win, 5, 1);
558 winsertln(win);
559 MvWAddStr(win, 5, 2, "The lines below should have moved down");
560 Continue(win);
562 wclear(win);
563 wmove(win, 2, 2);
564 wprintw(win, "This is a formatted string in a window: %d %s\n", 42,
565 "is it");
566 MvWAddStr(win, 10, 1, "Enter a string: ");
567 wrefresh(win);
568 noraw();
569 echo();
570 *Buffer = 0;
571 wscanw(win, "%s", Buffer);
573 printw("This is a formatted string in stdscr: %d %s\n", 42, "is it");
574 MvAddStr(10, 1, "Enter a string: ");
575 *Buffer = 0;
576 scanw("%s", Buffer);
578 if (TIGETSTR("cvvis", "vs") != 0) {
579 wclear(win);
580 curs_set(2);
581 MvWAddStr(win, 1, 1, "The cursor should appear as a block (visible)");
582 Continue(win);
585 if (TIGETSTR("civis", "vi") != 0) {
586 wclear(win);
587 curs_set(0);
588 MvWAddStr(win, 1, 1,
589 "The cursor should have disappeared (invisible)");
590 Continue(win);
593 if (TIGETSTR("cnorm", "ve") != 0) {
594 wclear(win);
595 curs_set(1);
596 MvWAddStr(win, 1, 1, "The cursor should be an underline (normal)");
597 Continue(win);
599 #ifdef A_COLOR
600 if (has_colors()) {
601 wclear(win);
602 MvWAddStr(win, 1, 1, "Colors should change after you press a key");
603 Continue(win);
604 init_pair(1, COLOR_RED, COLOR_WHITE);
605 wrefresh(win);
607 #endif
609 werase(win);
611 #if HAVE_TERMNAME
612 MvWAddStr(win, 1, 1, "Information About Your Terminal");
613 MvWAddStr(win, 3, 1, termname());
614 MvWAddStr(win, 4, 1, longname());
615 if (termattrs() & A_BLINK)
616 MvWAddStr(win, 5, 1, "This terminal supports blinking.");
617 else
618 MvWAddStr(win, 5, 1, "This terminal does NOT support blinking.");
619 #endif
621 (void) mvwaddnstr(win, 7, 5, "Have a nice day!ok", 16);
622 wrefresh(win);
624 (void) mvwinnstr(win, 7, 5, Buffer, 18);
625 MvAddStr(LINES - 2, 10, Buffer);
626 refresh();
627 Continue(win);
630 #if defined(PDCURSES) && !defined(XCURSES)
631 static void
632 resizeTest(WINDOW *dummy GCC_UNUSED)
634 WINDOW *win1;
636 savetty();
638 clear();
639 refresh();
640 # if defined(OS2)
641 resize_term(50, 120);
642 # else
643 resize_term(50, 80);
644 # endif
646 win1 = newwin(10, 50, 14, 25);
647 if (win1 == NULL) {
648 endwin();
649 return;
651 #ifdef A_COLOR
652 if (has_colors()) {
653 init_pair(3, COLOR_BLUE, COLOR_WHITE);
654 wattrset(win1, COLOR_PAIR(3));
656 #endif
657 wclear(win1);
659 MvWAddStr(win1, 1, 1, "The screen may now have 50 lines");
660 Continue(win1);
662 wclear(win1);
663 resetty();
665 MvWAddStr(win1, 1, 1, "The screen should now be reset");
666 Continue(win1);
668 delwin(win1);
670 clear();
671 refresh();
674 #endif
676 static void
677 padTest(WINDOW *dummy GCC_UNUSED)
679 WINDOW *pad, *spad;
681 if ((pad = newpad(50, 100)) != 0) {
682 wattron(pad, A_REVERSE);
683 MvWAddStr(pad, 5, 2, "This is a new pad");
684 (void) wattrset(pad, A_NORMAL);
685 MvWAddStr(pad, 8, 0,
686 "The end of this line should be truncated here:except now");
687 MvWAddStr(pad, 11, 1, "This line should not appear.It will now");
688 wmove(pad, 10, 1);
689 wclrtoeol(pad);
690 MvWAddStr(pad, 10, 1, " Press any key to continue");
691 prefresh(pad, 0, 0, 0, 0, 10, 45);
692 keypad(pad, TRUE);
693 raw();
694 wgetch(pad);
696 spad = subpad(pad, 12, 25, 6, 52);
697 MvWAddStr(spad, 2, 2, "This is a new subpad");
698 box(spad, 0, 0);
699 prefresh(pad, 0, 0, 0, 0, 15, 75);
700 keypad(pad, TRUE);
701 raw();
702 wgetch(pad);
704 MvWAddStr(pad, 35, 2, "This is displayed at line 35 in the pad");
705 MvWAddStr(pad, 40, 1, " Press any key to continue");
706 prefresh(pad, 30, 0, 0, 0, 10, 45);
707 keypad(pad, TRUE);
708 raw();
709 wgetch(pad);
711 delwin(pad);
715 static void
716 display_menu(int old_option, int new_option)
718 int i;
720 assert((new_option >= 0) && (new_option < MAX_OPTIONS));
722 (void) attrset(A_NORMAL);
723 MvAddStr(3, 20, "PDCurses Test Program");
725 for (i = 0; i < (int) MAX_OPTIONS; i++)
726 MvAddStr(5 + i, 25, command[i].text);
728 if ((old_option >= 0) && (old_option < MAX_OPTIONS))
729 MvAddStr(5 + old_option, 25, command[old_option].text);
731 (void) attrset(A_REVERSE);
732 MvAddStr(5 + new_option, 25, command[new_option].text);
733 (void) attrset(A_NORMAL);
734 MvAddStr(13, 3,
735 "Use Up and Down Arrows to select - Enter to run - Q to quit");
736 refresh();