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