]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/testcurs.c
ncurses 5.4
[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.32 2002/10/19 22:11:24 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 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 = (char *) malloc(strlen(s) + 1);
55     if (p)
56         strcpy(p, s);
57     return (p);
58 }
59 #endif /* not HAVE_STRDUP */
60
61 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     long 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     keypad(win, TRUE);
346     raw();
347     noecho();
348     typeahead(-1);
349 #if defined(PDCURSES)
350     mouse_set(ALL_MOUSE_EVENTS);
351 #endif
352     for (;;) {
353         wmove(win, 3, 5);
354         c = wgetch(win);
355         wclrtobot(win);
356         if (c >= KEY_MIN)
357             wprintw(win, "Key Pressed: %s", keyname(c));
358         else if (isprint(c))
359             wprintw(win, "Key Pressed: %c", c);
360         else
361             wprintw(win, "Key Pressed: %s", unctrl(c));
362 #if defined(PDCURSES)
363         if (c == KEY_MOUSE) {
364             int button = 0;
365             request_mouse_pos();
366             if (BUTTON_CHANGED(1))
367                 button = 1;
368             else if (BUTTON_CHANGED(2))
369                 button = 2;
370             else if (BUTTON_CHANGED(3))
371                 button = 3;
372             else
373                 button = 0;
374             wmove(win, 4, 18);
375             wprintw(win, "Button %d: ", button);
376             if (MOUSE_MOVED)
377                 wprintw(win, "moved: ");
378             else if ((BUTTON_STATUS(button) & BUTTON_ACTION_MASK) == BUTTON_PRESSED)
379                 wprintw(win, "pressed: ");
380             else if ((BUTTON_STATUS(button) & BUTTON_ACTION_MASK) == BUTTON_DOUBLE_CLICKED)
381                 wprintw(win, "double: ");
382             else
383                 wprintw(win, "released: ");
384             wprintw(win, " Position: Y: %d X: %d", MOUSE_Y_POS, MOUSE_X_POS);
385         }
386 #endif
387         wrefresh(win);
388         if (c == ' ')
389             break;
390     }
391 #if 0
392     nodelay(win, TRUE);
393     wgetch(win);
394     nodelay(win, FALSE);
395 #endif
396 #if defined(PDCURSES)
397     mouse_set(0L);
398 #endif
399     refresh();
400
401     repeat = 0;
402     do {
403         static const char *fmt[] = {
404             "%d %10s",
405             "%d %[a-zA-Z]s",
406             "%d %[][a-zA-Z]s",
407             "%d %[^0-9]"
408         };
409         const char *format = fmt[repeat % SIZEOF(fmt)];
410
411         wclear(win);
412         mvwaddstr(win, 3, 2, "The window should have moved");
413         mvwaddstr(win, 4, 2,
414                   "This text should have appeared without you pressing a key");
415         mvwprintw(win, 6, 2,
416                   "Scanning with format \"%s\"", format);
417         mvwin(win, 2 + 2 * (repeat % 4), 1 + 2 * (repeat % 4));
418         erase();
419         refresh();
420         wrefresh(win);
421         echo();
422         noraw();
423         num = 0;
424         *buffer = 0;
425         answered = mvwscanw(win, 7, 6, strdup(format), &num, buffer);
426         mvwprintw(win, 8, 6,
427                   "String: %s Number: %d (%d values read)",
428                   buffer, num, answered);
429         Continue(win);
430         ++repeat;
431     } while (answered > 0);
432 }
433
434 static void
435 outputTest(WINDOW *win)
436 {
437     WINDOW *win1;
438     char Buffer[80];
439     chtype ch;
440     int by, bx;
441
442     nl();
443     wclear(win);
444     mvwaddstr(win, 1, 1,
445               "You should now have a screen in the upper left corner, and this text should have wrapped");
446     mvwin(win, 2, 1);
447     waddstr(win, "\nThis text should be down\n");
448     waddstr(win, "and broken into two here ^");
449     Continue(win);
450
451     wclear(win);
452     wattron(win, A_BOLD);
453     mvwaddstr(win, 1, 1, "A new window will appear with this text in it");
454     mvwaddstr(win, 8, 1, "Press any key to continue");
455     wrefresh(win);
456     wgetch(win);
457
458     getbegyx(win, by, bx);
459
460     if (LINES < 24 || COLS < 75) {
461         mvwaddstr(win, 5, 1,
462                   "Some tests have been skipped as they require a");
463         mvwaddstr(win, 6, 1, "display of at least 24 LINES by 75 COLUMNS");
464         Continue(win);
465     } else {
466         win1 = newwin(10, 50, 14, 25);
467         if (win1 == NULL) {
468             endwin();
469             return;
470         }
471 #ifdef A_COLOR
472         if (has_colors()) {
473             init_pair(3, COLOR_BLUE, COLOR_WHITE);
474             wbkgd(win1, COLOR_PAIR(3));
475         } else
476             wbkgd(win1, A_NORMAL);
477 #else
478         wbkgd(win1, A_NORMAL);
479 #endif
480         wclear(win1);
481         mvwaddstr(win1, 5, 1,
482                   "This text should appear; using overlay option");
483         copywin(win, win1, 0, 0, 0, 0, 9, 49, TRUE);
484
485 #if defined(PDCURSES) && !defined(XCURSES)
486         box(win1, 0xb3, 0xc4);
487 #else
488         box(win1, ACS_VLINE, ACS_HLINE);
489 #endif
490         wmove(win1, 8, 26);
491         wrefresh(win1);
492         wgetch(win1);
493
494         wclear(win1);
495         wattron(win1, A_BLINK);
496         mvwaddstr(win1, 4, 1,
497                   "This blinking text should appear in only the second window");
498         wattroff(win1, A_BLINK);
499         mvwin(win1, by, bx);
500         overlay(win, win1);
501         mvwin(win1, 14, 25);
502         wmove(win1, 8, 26);
503         wrefresh(win1);
504         wgetch(win1);
505         delwin(win1);
506     }
507
508     clear();
509     wclear(win);
510     wrefresh(win);
511     mvwaddstr(win, 6, 2, "This line shouldn't appear");
512     mvwaddstr(win, 4, 2, "Only half of the next line is visible");
513     mvwaddstr(win, 5, 2, "Only half of the next line is visible");
514     wmove(win, 6, 1);
515     wclrtobot(win);
516     wmove(win, 5, 20);
517     wclrtoeol(win);
518     mvwaddstr(win, 8, 2, "This line also shouldn't appear");
519     wmove(win, 8, 1);
520     wdeleteln(win);
521     Continue(win);
522
523     wmove(win, 5, 9);
524     ch = winch(win);
525
526     wclear(win);
527     wmove(win, 6, 2);
528     waddstr(win, "The next char should be l:  ");
529     winsch(win, ch);
530     Continue(win);
531
532     mvwinsstr(win, 6, 2, "A1B2C3D4E5");
533     Continue(win);
534
535     wmove(win, 5, 1);
536     winsertln(win);
537     mvwaddstr(win, 5, 2, "The lines below should have moved down");
538     Continue(win);
539
540     wclear(win);
541     wmove(win, 2, 2);
542     wprintw(win, "This is a formatted string in a window: %d %s\n", 42,
543             "is it");
544     mvwaddstr(win, 10, 1, "Enter a string: ");
545     wrefresh(win);
546     noraw();
547     echo();
548     *Buffer = 0;
549     wscanw(win, "%s", Buffer);
550
551     printw("This is a formatted string in stdscr: %d %s\n", 42, "is it");
552     mvaddstr(10, 1, "Enter a string: ");
553     *Buffer = 0;
554     scanw("%s", Buffer);
555
556     if (tigetstr("cvvis") != 0) {
557         wclear(win);
558         curs_set(2);
559         mvwaddstr(win, 1, 1, "The cursor should appear as a block (visible)");
560         Continue(win);
561     }
562
563     if (tigetstr("civis") != 0) {
564         wclear(win);
565         curs_set(0);
566         mvwaddstr(win, 1, 1,
567                   "The cursor should have disappeared (invisible)");
568         Continue(win);
569     }
570
571     if (tigetstr("cnorm") != 0) {
572         wclear(win);
573         curs_set(1);
574         mvwaddstr(win, 1, 1, "The cursor should be an underline (normal)");
575         Continue(win);
576     }
577 #ifdef A_COLOR
578     if (has_colors()) {
579         wclear(win);
580         mvwaddstr(win, 1, 1, "Colors should change after you press a key");
581         Continue(win);
582         init_pair(1, COLOR_RED, COLOR_WHITE);
583         wrefresh(win);
584     }
585 #endif
586
587     werase(win);
588     mvwaddstr(win, 1, 1, "Information About Your Terminal");
589     mvwaddstr(win, 3, 1, termname());
590     mvwaddstr(win, 4, 1, longname());
591     if (termattrs() & A_BLINK)
592         mvwaddstr(win, 5, 1, "This terminal supports blinking.");
593     else
594         mvwaddstr(win, 5, 1, "This terminal does NOT support blinking.");
595
596     mvwaddnstr(win, 7, 5, "Have a nice day!ok", 16);
597     wrefresh(win);
598
599     mvwinnstr(win, 7, 5, Buffer, 18);
600     mvaddstr(LINES - 2, 10, Buffer);
601     refresh();
602     Continue(win);
603 }
604
605 #if defined(PDCURSES) && !defined(XCURSES)
606 static void
607 resizeTest(WINDOW *dummy GCC_UNUSED)
608 {
609     WINDOW *win1;
610
611     savetty();
612
613     clear();
614     refresh();
615 #  if defined(OS2)
616     resize_term(50, 120);
617 #  else
618     resize_term(50, 80);
619 #  endif
620
621     win1 = newwin(10, 50, 14, 25);
622     if (win1 == NULL) {
623         endwin();
624         return;
625     }
626 #ifdef A_COLOR
627     if (has_colors()) {
628         init_pair(3, COLOR_BLUE, COLOR_WHITE);
629         wattrset(win1, COLOR_PAIR(3));
630     }
631 #endif
632     wclear(win1);
633
634     mvwaddstr(win1, 1, 1, "The screen may now have 50 lines");
635     Continue(win1);
636
637     wclear(win1);
638     resetty();
639
640     mvwaddstr(win1, 1, 1, "The screen should now be reset");
641     Continue(win1);
642
643     delwin(win1);
644
645     clear();
646     refresh();
647
648 }
649 #endif
650
651 static void
652 padTest(WINDOW *dummy GCC_UNUSED)
653 {
654     WINDOW *pad, *spad;
655
656     pad = newpad(50, 100);
657     wattron(pad, A_REVERSE);
658     mvwaddstr(pad, 5, 2, "This is a new pad");
659     wattrset(pad, A_NORMAL);
660     mvwaddstr(pad, 8, 0,
661               "The end of this line should be truncated here:except  now");
662     mvwaddstr(pad, 11, 1, "This line should not appear.It will now");
663     wmove(pad, 10, 1);
664     wclrtoeol(pad);
665     mvwaddstr(pad, 10, 1, " Press any key to continue");
666     prefresh(pad, 0, 0, 0, 0, 10, 45);
667     keypad(pad, TRUE);
668     raw();
669     wgetch(pad);
670
671     spad = subpad(pad, 12, 25, 6, 52);
672     mvwaddstr(spad, 2, 2, "This is a new subpad");
673     box(spad, 0, 0);
674     prefresh(pad, 0, 0, 0, 0, 15, 75);
675     keypad(pad, TRUE);
676     raw();
677     wgetch(pad);
678
679     mvwaddstr(pad, 35, 2, "This is displayed at line 35 in the pad");
680     mvwaddstr(pad, 40, 1, " Press any key to continue");
681     prefresh(pad, 30, 0, 0, 0, 10, 45);
682     keypad(pad, TRUE);
683     raw();
684     wgetch(pad);
685
686     delwin(pad);
687 }
688
689 static void
690 display_menu(int old_option, int new_option)
691 {
692     register size_t i;
693
694     attrset(A_NORMAL);
695     mvaddstr(3, 20, "PDCurses Test Program");
696
697     for (i = 0; i < MAX_OPTIONS; i++)
698         mvaddstr(5 + i, 25, command[i].text);
699     if (old_option != (-1))
700         mvaddstr(5 + old_option, 25, command[old_option].text);
701     attrset(A_REVERSE);
702     mvaddstr(5 + new_option, 25, command[new_option].text);
703     attrset(A_NORMAL);
704     mvaddstr(13, 3,
705              "Use Up and Down Arrows to select - Enter to run - Q to quit");
706     refresh();
707 }