]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/ncurses.c
ncurses 6.0 - patch 20160206
[ncurses.git] / test / ncurses.c
index 59a88c457402cc0a4ab43325e750246a0634ee56..9a90d4018dc9fbdb740e2d9988d48cd15508fce3 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2014,2015 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2015,2016 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -40,7 +40,7 @@ AUTHOR
    Author: Eric S. Raymond <esr@snark.thyrsus.com> 1993
            Thomas E. Dickey (beginning revision 1.27 in 1996).
 
-$Id: ncurses.c,v 1.420 2015/05/23 23:41:25 tom Exp $
+$Id: ncurses.c,v 1.429 2016/01/03 01:50:10 tom Exp $
 
 ***************************************************************************/
 
@@ -636,15 +636,22 @@ setup_getch(WINDOW *win, GetchFlags flags)
 }
 
 static void
-init_getch(WINDOW *win, GetchFlags flags)
+init_getch(WINDOW *win, GetchFlags flags, int delay)
 {
     memset(flags, FALSE, NUM_GETCH_FLAGS);
     flags[UChar('k')] = (win == stdscr);
     flags[UChar('m')] = TRUE;
+    flags[UChar('t')] = (delay != 0);
 
     setup_getch(win, flags);
 }
 
+static bool
+blocking_getch(GetchFlags flags, int delay)
+{
+    return ((delay < 0) && flags['t']);
+}
+
 static void
 wgetch_help(WINDOW *win, GetchFlags flags)
 {
@@ -655,7 +662,8 @@ wgetch_help(WINDOW *win, GetchFlags flags)
        ,"k  -- toggle keypad/literal mode"
        ,"m  -- toggle meta (7-bit/8-bit) mode"
        ,"^q -- quit"
-       ,"s  -- shell out\n"
+       ,"s  -- shell out"
+       ,"t  -- toggle timeout"
        ,"w  -- create a new window"
 #ifdef SIGTSTP
        ,"z  -- suspend this process"
@@ -810,9 +818,9 @@ wgetch_test(unsigned level, WINDOW *win, int delay)
     int c;
     int incount = 0;
     GetchFlags flags;
-    bool blocking = (delay < 0);
 
-    init_getch(win, flags);
+    init_getch(win, flags, delay);
+    notimeout(win, FALSE);
     wtimeout(win, delay);
     getyx(win, first_y, first_x);
 
@@ -823,7 +831,7 @@ wgetch_test(unsigned level, WINDOW *win, int delay)
     for (;;) {
        while ((c = wGetchar(win)) == ERR) {
            incount++;
-           if (blocking) {
+           if (blocking_getch(flags, delay)) {
                (void) wprintw(win, "%05d: input error", incount);
                break;
            } else {
@@ -831,7 +839,7 @@ wgetch_test(unsigned level, WINDOW *win, int delay)
            }
            wgetch_wrap(win, first_y);
        }
-       if (c == ERR && blocking) {
+       if (c == ERR && blocking_getch(flags, delay)) {
            wprintw(win, "ERR");
            wgetch_wrap(win, first_y);
        } else if (isQuit(c)) {
@@ -860,6 +868,10 @@ wgetch_test(unsigned level, WINDOW *win, int delay)
            wgetch_help(win, flags);
        } else if (c == 's') {
            ShellOut(TRUE);
+       } else if (c == 't') {
+           notimeout(win, flags[UChar('t')]);
+           flags[UChar('t')] = !flags[UChar('t')];
+           wgetch_help(win, flags);
        } else if (c == 'w') {
            int high = getmaxy(win) - 1 - first_y + 1;
            int wide = getmaxx(win) - first_x;
@@ -931,7 +943,7 @@ wgetch_test(unsigned level, WINDOW *win, int delay)
     wtimeout(win, -1);
 
     if (!level)
-       init_getch(win, flags);
+       init_getch(win, flags, delay);
 }
 
 static int
@@ -958,7 +970,7 @@ begin_getch_test(void)
        delay = -1;
     }
     raw();
-    move(5, 0);
+    move(6, 0);
     return delay;
 }
 
@@ -1060,11 +1072,11 @@ wget_wch_test(unsigned level, WINDOW *win, int delay)
     wint_t c;
     int incount = 0;
     GetchFlags flags;
-    bool blocking = (delay < 0);
     int code;
     char *temp;
 
-    init_getch(win, flags);
+    init_getch(win, flags, delay);
+    notimeout(win, FALSE);
     wtimeout(win, delay);
     getyx(win, first_y, first_x);
 
@@ -1075,7 +1087,7 @@ wget_wch_test(unsigned level, WINDOW *win, int delay)
     for (;;) {
        while ((code = wGet_wchar(win, &c)) == ERR) {
            incount++;
-           if (blocking) {
+           if (blocking_getch(flags, delay)) {
                (void) wprintw(win, "%05d: input error", incount);
                break;
            } else {
@@ -1083,7 +1095,7 @@ wget_wch_test(unsigned level, WINDOW *win, int delay)
            }
            wgetch_wrap(win, first_y);
        }
-       if (code == ERR && blocking) {
+       if (code == ERR && blocking_getch(flags, delay)) {
            wprintw(win, "ERR");
            wgetch_wrap(win, first_y);
        } else if (isQuit((int) c)) {
@@ -1125,6 +1137,10 @@ wget_wch_test(unsigned level, WINDOW *win, int delay)
            wgetch_help(win, flags);
        } else if (c == 's') {
            ShellOut(TRUE);
+       } else if (c == 't') {
+           notimeout(win, flags[UChar('t')]);
+           flags[UChar('t')] = !flags[UChar('t')];
+           wgetch_help(win, flags);
        } else if (c == 'w') {
            int high = getmaxy(win) - 1 - first_y + 1;
            int wide = getmaxx(win) - first_x;
@@ -1184,7 +1200,7 @@ wget_wch_test(unsigned level, WINDOW *win, int delay)
     wtimeout(win, -1);
 
     if (!level)
-       init_getch(win, flags);
+       init_getch(win, flags, delay);
 }
 
 static void
@@ -1255,9 +1271,10 @@ my_termattrs(void)
 #define ATTRSTRING_1ST 32      /* ' ' */
 #define ATTRSTRING_END 126     /* '~' */
 
-#define COL_ATTRSTRING 25
-#define MARGIN_4_ATTRS (COL_ATTRSTRING + 8)
-#define LEN_ATTRSTRING (COLS - MARGIN_4_ATTRS)
+#define COLS_PRE_ATTRS 5
+#define COLS_AFT_ATTRS 15
+#define COL_ATTRSTRING (COLS_PRE_ATTRS + 17)
+#define LEN_ATTRSTRING (COLS - (COL_ATTRSTRING + COLS_AFT_ATTRS))
 #define MAX_ATTRSTRING (ATTRSTRING_END + 1 - ATTRSTRING_1ST)
 
 static char attr_test_string[MAX_ATTRSTRING + 1];
@@ -1412,11 +1429,11 @@ static int
 show_attr(WINDOW *win, int row, int skip, bool arrow, chtype attr, const char *name)
 {
     int ncv = get_ncv();
-    chtype test = attr & (chtype) (~A_ALTCHARSET);
+    chtype test = attr & (chtype) (~(A_ALTCHARSET | A_CHARTEXT));
 
     if (arrow)
-       MvPrintw(row, 5, "-->");
-    MvPrintw(row, 8, "%s mode:", name);
+       MvPrintw(row, COLS_PRE_ATTRS - 3, "-->");
+    MvPrintw(row, COLS_PRE_ATTRS, "%s mode:", name);
     MvPrintw(row, COL_ATTRSTRING - 1, "|");
     if (skip)
        printw("%*s", skip, " ");
@@ -1477,8 +1494,9 @@ show_attr(WINDOW *win, int row, int skip, bool arrow, chtype attr, const char *n
                if (found)
                    printw(" (NCV)");
            }
-           if ((termattrs() & test) != test)
+           if ((termattrs() & test) != test) {
                printw(" (Part)");
+           }
        }
     }
     return row + 2;
@@ -1661,10 +1679,10 @@ attr_test(void)
                                my_list[j].name);
            }
 
-           MvPrintw(row, 8,
+           MvPrintw(row, COLS_PRE_ATTRS,
                     "This terminal does %shave the magic-cookie glitch",
                     get_xmc() > -1 ? "" : "not ");
-           MvPrintw(row + 1, 8, "Enter '?' for help.");
+           MvPrintw(row + 1, COLS_PRE_ATTRS, "Enter '?' for help.");
            show_color_attr(fg, bg, tx);
            printw("  ACS (%d)", ac != 0);
 
@@ -1802,8 +1820,8 @@ wide_show_attr(WINDOW *win,
     chtype test = attr & ~WA_ALTCHARSET;
 
     if (arrow)
-       MvPrintw(row, 5, "-->");
-    MvPrintw(row, 8, "%s mode:", name);
+       MvPrintw(row, COLS_PRE_ATTRS - 3, "-->");
+    MvPrintw(row, COLS_PRE_ATTRS, "%s mode:", name);
     MvPrintw(row, COL_ATTRSTRING - 1, "|");
     if (skip)
        printw("%*s", skip, " ");
@@ -1867,8 +1885,9 @@ wide_show_attr(WINDOW *win,
                if (found)
                    printw(" (NCV)");
            }
-           if ((term_attrs() & test) != test)
+           if ((term_attrs() & test) != test) {
                printw(" (Part)");
+           }
        }
     }
     return row + 2;
@@ -2012,10 +2031,10 @@ wide_attr_test(void)
                                     my_list[j].name);
            }
 
-           MvPrintw(row, 8,
+           MvPrintw(row, COLS_PRE_ATTRS,
                     "This terminal does %shave the magic-cookie glitch",
                     get_xmc() > -1 ? "" : "not ");
-           MvPrintw(row + 1, 8, "Enter '?' for help.");
+           MvPrintw(row + 1, COLS_PRE_ATTRS, "Enter '?' for help.");
            show_color_attr(fg, bg, tx);
            printw("  ACS (%d)", ac != 0);
 
@@ -2166,9 +2185,10 @@ color_test(void)
     int page_size = (LINES - grid_top);
     int pairs_max;
     int colors_max = COLORS;
+    int col_limit;
     int row_limit;
     int per_row;
-    char numbered[80];
+    char *numbered = 0;
     const char *hello;
     bool done = FALSE;
     bool opt_acsc = FALSE;
@@ -2178,6 +2198,15 @@ color_test(void)
     bool opt_wide = FALSE;
     WINDOW *helpwin;
 
+    numbered = (char *) calloc((size_t) (COLS + 1), sizeof(char));
+    done = ((COLS < 16) || (numbered == 0));
+
+    /*
+     * Because the number of colors is usually a power of two, we also use
+     * a power of two for the number of colors shown per line (to be tidy).
+     */
+    for (col_limit = 1; col_limit * 2 < COLS; col_limit *= 2) ;
+
     while (!done) {
        int shown = 0;
 
@@ -2195,11 +2224,11 @@ color_test(void)
        if (opt_wide) {
            width = 4;
            hello = "Test";
-           per_row = (colors_max > 8) ? 16 : 8;
+           per_row = (col_limit / ((colors_max > 8) ? 4 : 8));
        } else {
            width = 8;
            hello = "Hello";
-           per_row = 8;
+           per_row = (col_limit / 8);
        }
        per_row -= min_colors;
 
@@ -2230,6 +2259,9 @@ color_test(void)
            int col = (i % per_row + 1) * width;
            NCURSES_PAIRS_T pair = i;
 
+           if ((i / per_row) > row_limit)
+               break;
+
 #define InxToFG(i) (NCURSES_COLOR_T) ((i % (colors_max - min_colors)) + min_colors)
 #define InxToBG(i) (NCURSES_COLOR_T) ((i / (colors_max - min_colors)) + min_colors)
            if (row >= 0 && move(row, col) != ERR) {
@@ -2356,6 +2388,8 @@ color_test(void)
 
     erase();
     endwin();
+
+    free(numbered);
 }
 
 #if USE_WIDEC_SUPPORT
@@ -2370,9 +2404,10 @@ wide_color_test(void)
     int page_size = (LINES - grid_top);
     int pairs_max = (unsigned short) (-1);
     int colors_max = COLORS;
+    int col_limit;
     int row_limit;
     int per_row;
-    char numbered[80];
+    char *numbered = 0;
     const char *hello;
     bool done = FALSE;
     bool opt_acsc = FALSE;
@@ -2381,9 +2416,19 @@ wide_color_test(void)
     bool opt_wide = FALSE;
     bool opt_nums = FALSE;
     bool opt_xchr = FALSE;
-    wchar_t buffer[80];
+    wchar_t *buffer = 0;
     WINDOW *helpwin;
 
+    numbered = (char *) calloc((size_t) (COLS + 1), sizeof(char));
+    buffer = (wchar_t *) calloc((size_t) (COLS + 1), sizeof(wchar_t));
+    done = ((COLS < 16) || (numbered == 0) || (buffer == 0));
+
+    /*
+     * Because the number of colors is usually a power of two, we also use
+     * a power of two for the number of colors shown per line (to be tidy).
+     */
+    for (col_limit = 1; col_limit * 2 < COLS; col_limit *= 2) ;
+
     while (!done) {
        int shown = 0;
 
@@ -2397,15 +2442,14 @@ wide_color_test(void)
                pairs_max = COLOR_PAIRS;
        }
 
-       /* this assumes an 80-column line */
        if (opt_wide) {
            width = 4;
            hello = "Test";
-           per_row = (colors_max > 8) ? 16 : 8;
+           per_row = (col_limit / ((colors_max > 8) ? 4 : 8));
        } else {
            width = 8;
            hello = "Hello";
-           per_row = 8;
+           per_row = (col_limit / 8);
        }
        per_row -= min_colors;
 
@@ -2444,6 +2488,9 @@ wide_color_test(void)
            int col = (i % per_row + 1) * width;
            NCURSES_PAIRS_T pair = (NCURSES_PAIRS_T) i;
 
+           if ((i / per_row) > row_limit)
+               break;
+
            if (row >= 0 && move(row, col) != ERR) {
                init_pair(pair, InxToFG(i), InxToBG(i));
                (void) color_set(pair, NULL);
@@ -2575,6 +2622,9 @@ wide_color_test(void)
 
     erase();
     endwin();
+
+    free(numbered);
+    free(buffer);
 }
 #endif /* USE_WIDEC_SUPPORT */
 
@@ -5523,7 +5573,6 @@ demo_pad(bool colored)
        Cannot("cannot create requested pad");
        return;
     }
-
 #ifdef A_COLOR
     if (colored && use_colors) {
        init_pair(1, COLOR_BLACK, COLOR_GREEN);