tests/interactive-ascii: update test7.c
authorMatthew Mondor <mmondor@pulsar-zone.net>
Sat, 8 Apr 2023 02:37:38 +0000 (02:37 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Sat, 8 Apr 2023 02:37:38 +0000 (02:37 +0000)
tests/interactive-ascii/test7.c

index b55f37f..4d33f92 100644 (file)
@@ -7,15 +7,17 @@
  * to avoid possible artifacts like syscalls returning EINTR because of our
  * interrupting clock signal.  An alternative would be using an auto-restart
  * signal with SA_RESTART that takes care of all updates while taking care not
- * to execute syscalls that are not signal handler safe.
+ * to execute syscalls that are not signal handler safe.  This now also uses
+ * erase() and doupdate() instead of clear() and refresh(), removing the
+ * requirement for clearok() to prevent double clears.
+ * This version can also preserve default terminal colors like the foreground,
+ * using an extension to X/Open that both ncurses and BSD curses support.
  *
  * TODO
  * - Consider another test keeping the time to determine if to delay again,
  *   instead of relying on signal interruption by an interval timer.  This
- *   possibly could allow more reliable user input.
- * - See if calling termios directly can be avoided with OSX still restoring
- *   the tty settings properly.  Also look at
- *   reset_shell_mode()/reset_prog_mode(), def_shell_mode()/def_prog_mode().
+ *   possibly could allow more reliable user input.  However, non-blocking
+ *   getch() may potentially interfere with key arrow sequence interpretation.
  * 
  * $ cc -Wall -O0 -g -o test7 test7.c -lcurses
  */
@@ -124,28 +126,38 @@ main(void)
        /* Exit cleanup hook to restore normal terminal */
        (void)atexit(cleanup);
 
+       /*
+        * XXX Useful but relies on an ncurses extension, also supported by
+        * NetBSD curses.  init_pair() cannot accept -1 according to X/Open.
+        * But this special value means to keep the default foreground or
+        * background color of the terminal configuration.  This also permits
+        * enforced color pairs to really only define the foreground or
+        * background color.
+        */
+       (void)use_default_colors();
        if (start_color() == ERR)
                err(EXIT_FAILURE, "start_color()");
-       (void)init_pair(0, COLOR_WHITE, COLOR_BLACK);
-       (void)init_pair(1, COLOR_WHITE, COLOR_RED);
-       (void)init_pair(2, COLOR_WHITE, COLOR_GREEN);
-       (void)init_pair(3, COLOR_WHITE, COLOR_YELLOW);
-       (void)init_pair(4, COLOR_WHITE, COLOR_BLUE);
-       (void)init_pair(5, COLOR_WHITE, COLOR_MAGENTA);
-       (void)init_pair(6, COLOR_WHITE, COLOR_CYAN);
-       (void)init_pair(7, COLOR_WHITE, COLOR_WHITE);
+       (void)init_pair(0, -1, COLOR_BLACK);
+       (void)init_pair(1, -1, COLOR_RED);
+       (void)init_pair(2, -1, COLOR_GREEN);
+       (void)init_pair(3, -1, COLOR_YELLOW);
+       (void)init_pair(4, -1, COLOR_BLUE);
+       (void)init_pair(5, -1, COLOR_MAGENTA);
+       (void)init_pair(6, -1, COLOR_CYAN);
+       (void)init_pair(7, -1, COLOR_WHITE);
 
        (void)cbreak(); /* Use raw() if not wanting SIGINT */
        (void)noecho();
        (void)keypad(w, TRUE);
+       (void)nonl();
+       (void)intrflush(stdscr, FALSE);
        (void)timeout(-1);
 
        /* Setup animation world */
        anim_init();
 
        /* Initialize screen */
-       (void)clear();
-       (void)clearok(w, FALSE);
+       (void)erase();
        safe_refresh();
        (void)curs_set(0);
 
@@ -267,7 +279,7 @@ safe_refresh(void)
        set = block_mask;
        (void)sigprocmask(SIG_BLOCK, &set, NULL);
 
-       (void)wrefresh(w);
+       (void)doupdate();
 
        set = block_mask;
        (void)sigprocmask(SIG_UNBLOCK, &set, NULL);
@@ -303,8 +315,7 @@ anim_redraw(void)
        int i;
 
        (void)attrset(COLOR_PAIR(0));
-       (void)clear();
-       (void)clearok(w, FALSE);
+       (void)erase();
        for (i = 0; i < NOBJECTS; i++) {
                aobject_t *o = &objects[i];