]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/dots_xcurses.c
ncurses 6.2 - patch 20200718
[ncurses.git] / test / dots_xcurses.c
index abf0a00ae030b4dfc03b3522290f0f1162f34c7a..d8fe80319307d3c1525a8e6ca45035aa8b7d9299 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 2017 Free Software Foundation, Inc.                        *
+ * Copyright 2018-2019,2020 Thomas E. Dickey                                *
+ * Copyright 2017 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            *
 /*
  * Author: Thomas E. Dickey
  *
- * $Id: dots_xcurses.c,v 1.8 2017/10/12 00:20:41 tom Exp $
+ * $Id: dots_xcurses.c,v 1.23 2020/05/29 23:04:02 tom Exp $
  *
  * A simple demo of the wide-curses interface used for comparison with termcap.
  */
 #include <test.priv.h>
 
-#if !defined(__MINGW32__)
+#if !defined(_WIN32)
 #include <sys/time.h>
 #endif
 
@@ -55,8 +56,7 @@ static bool interrupted = FALSE;
 static long total_chars = 0;
 static time_t started;
 
-#ifdef NCURSES_VERSION
-static bool d_option = FALSE;
+#if HAVE_ALLOC_PAIR
 static bool x_option = FALSE;
 #endif
 
@@ -65,9 +65,10 @@ cleanup(void)
 {
     endwin();
 
-    printf("\n\n%ld total cells, rate %.2f/sec\n",
-          total_chars,
-          ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
+    fflush(stdout);
+    fprintf(stderr, "\n\n%ld total cells, rate %.2f/sec\n",
+           total_chars,
+           ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
 }
 
 static void
@@ -104,22 +105,28 @@ set_colors(int fg, int bg)
 {
     int pair = mypair(fg, bg);
     if (pair > 0) {
-       color_set((short) pair, NewPair(pair));
+       (void) color_set((short) pair, NewPair(pair));
     }
 }
 
-#if defined(NCURSES_VERSION)
 static void
 usage(void)
 {
     static const char *msg[] =
     {
-       "Usage: firework [options]"
+       "Usage: dots_xcurses [options]"
        ,""
        ,"Options:"
+       ," -T TERM  override $TERM"
 #if HAVE_USE_DEFAULT_COLORS
        ," -d       invoke use_default_colors()"
 #endif
+#if HAVE_USE_ENV
+       ," -e       allow environment $LINES / $COLUMNS"
+#endif
+       ," -m SIZE  set margin (default: 2)"
+       ," -r SECS  self-interrupt/exit after specified number of seconds"
+       ," -s MSECS delay 1% of the time (default: 1 msecs)"
 #if HAVE_ALLOC_PAIR
        ," -x       use alloc_pair() rather than init_pair()"
 #endif
@@ -131,26 +138,50 @@ usage(void)
 
     ExitProgram(EXIT_FAILURE);
 }
-#endif
 
 int
-main(int argc GCC_UNUSED,
-     char *argv[]GCC_UNUSED)
+main(int argc, char *argv[])
 {
-    int margin = 2;
-    int x, y, z, p;
     int fg, bg, ch;
     wchar_t wch[2];
-    int pair;
     double r;
     double c;
+#if HAVE_USE_DEFAULT_COLORS
+    bool d_option = FALSE;
+#endif
+    int m_option = 2;
+    int r_option = 0;
+    int s_option = 1;
+    size_t need;
+    char *my_env;
 
-#if defined(NCURSES_VERSION)
-    while ((ch = getopt(argc, argv, "dx")) != -1) {
+    while ((ch = getopt(argc, argv, "T:dem:r:s:x")) != -1) {
        switch (ch) {
+       case 'T':
+           need = 6 + strlen(optarg);
+           my_env = malloc(need);
+           _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
+           putenv(my_env);
+           break;
+#if HAVE_USE_DEFAULT_COLORS
        case 'd':
            d_option = TRUE;
            break;
+#endif
+#if HAVE_USE_ENV
+       case 'e':
+           use_env(TRUE);
+           break;
+#endif
+       case 'm':
+           m_option = atoi(optarg);
+           break;
+       case 'r':
+           r_option = atoi(optarg);
+           break;
+       case 's':
+           s_option = atoi(optarg);
+           break;
 #if HAVE_ALLOC_PAIR
        case 'x':
            x_option = TRUE;
@@ -161,10 +192,10 @@ main(int argc GCC_UNUSED,
            break;
        }
     }
-#endif
 
     srand((unsigned) time(0));
 
+    SetupAlarm(r_option);
     InitAndCatch(initscr(), onsig);
     if (has_colors()) {
        start_color();
@@ -172,11 +203,19 @@ main(int argc GCC_UNUSED,
        if (d_option)
            use_default_colors();
 #endif
+#if HAVE_ALLOC_PAIR
        if (x_option) {
            ;                   /* nothing */
-       } else {
+       } else
+#endif
+       {
            for (fg = 0; fg < COLORS; fg++) {
                for (bg = 0; bg < COLORS; bg++) {
+                   int pair;
+                   if (interrupted) {
+                       cleanup();
+                       ExitProgram(EXIT_FAILURE);
+                   }
                    pair = mypair(fg, bg);
                    if (pair > 0) {
                        InitPair(pair, fg, bg);
@@ -186,27 +225,27 @@ main(int argc GCC_UNUSED,
        }
     }
 
-    r = (double) (LINES - (2 * margin));
-    c = (double) (COLS - (2 * margin));
+    r = (double) (LINES - (2 * m_option));
+    c = (double) (COLS - (2 * m_option));
     started = time((time_t *) 0);
 
     fg = COLOR_WHITE;
     bg = COLOR_BLACK;
-    pair = 0;
     wch[1] = 0;
     while (!interrupted) {
-       x = (int) (c * ranf()) + margin;
-       y = (int) (r * ranf()) + margin;
-       p = (ranf() > 0.9) ? '*' : ' ';
+       int x = (int) (c * ranf()) + m_option;
+       int y = (int) (r * ranf()) + m_option;
+       int p = (ranf() > 0.9) ? '*' : ' ';
 
        move(y, x);
        if (has_colors()) {
-           z = (int) (ranf() * COLORS);
+           int z = (int) (ranf() * COLORS);
            if (ranf() > 0.01) {
                set_colors(fg = z, bg);
            } else {
                set_colors(fg, bg = z);
-               napms(1);
+               if (s_option)
+                   napms(s_option);
            }
        } else {
            if (ranf() <= 0.01) {
@@ -215,10 +254,11 @@ main(int argc GCC_UNUSED,
                } else {
                    attr_off(WA_REVERSE, NULL);
                }
-               napms(1);
+               if (s_option)
+                   napms(s_option);
            }
        }
-       wch[0] = p;
+       wch[0] = (wchar_t) p;
        addnwstr(wch, 1);
        refresh();
        ++total_chars;