]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/ditto.c
ncurses 5.6 - patch 20080329
[ncurses.git] / test / ditto.c
index 2a338d8b50f82e25d785099ed6c527c742df95c5..1a06feaee7c3d682623aee622c8e4e80703307f2 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2001,2005 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2007,2008 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            *
@@ -29,7 +29,7 @@
 /*
  * Author: Thomas E. Dickey <dickey@clark.net> 1998
  *
- * $Id: ditto.c,v 1.5 2005/04/16 16:35:49 tom Exp $
+ * $Id: ditto.c,v 1.14 2008/03/29 21:35:39 tom Exp $
  *
  * The program illustrates how to set up multiple screens from a single
  * program.  Invoke the program by specifying another terminal on the same
@@ -72,19 +72,51 @@ open_tty(char *path)
        errno = ENOTTY;
        failed(path);
     }
-    fp = fopen(path, "a+");
+    fp = fopen(path, "r+");
     if (fp == 0)
        failed(path);
     printf("opened %s\n", path);
     return fp;
 }
 
+static int
+close_screen(SCREEN *sp GCC_UNUSED, void *arg GCC_UNUSED)
+{
+    (void) sp;
+    (void) arg;
+    return endwin();
+}
+
+static int
+read_screen(SCREEN *sp GCC_UNUSED, void *arg GCC_UNUSED)
+{
+    return getch();
+}
+
+static int
+write_screen(SCREEN *sp GCC_UNUSED, void *arg GCC_UNUSED)
+{
+    addstr((char *) arg);
+    refresh();
+    return OK;
+}
+
+static void
+show_ditto(DITTO * data, int count, char *msg)
+{
+    int n;
+
+    for (n = 0; n < count; n++) {
+       USING_SCREEN(data[n].screen, write_screen, (void *) msg);
+    }
+}
+
 int
 main(int argc GCC_UNUSED,
      char *argv[]GCC_UNUSED)
 {
     int j;
-    int active_tty = 0;
+    int count;
     DITTO *data;
 
     if (argc <= 1)
@@ -105,7 +137,6 @@ main(int argc GCC_UNUSED,
      * Set up the screens.
      */
     for (j = 0; j < argc; j++) {
-       active_tty++;
        data[j].screen = newterm((char *) 0,    /* assume $TERM is the same */
                                 data[j].output,
                                 data[j].input);
@@ -114,33 +145,36 @@ main(int argc GCC_UNUSED,
        cbreak();
        noecho();
        scrollok(stdscr, TRUE);
+       nodelay(stdscr, TRUE);
     }
 
     /*
      * Loop, reading characters from any of the inputs and writing to all
      * of the screens.
      */
-    for (;;) {
+    for (count = 0;; ++count) {
+       char message[80];
        int ch;
-       set_term(data[0].screen);
-       ch = getch();
-       if (ch == ERR)
+       int which = (count % argc);
+
+       napms(20);
+       ch = USING_SCREEN(data[which].screen, read_screen, 0);
+       if (ch == ERR) {
+           /* echochar('.'); */
            continue;
-       if (ch == 4)
-           break;
-       for (j = 0; j < argc; j++) {
-           set_term(data[j].screen);
-           addch(UChar(ch));
-           refresh();
        }
+       if (ch == CTRL('D'))
+           break;
+       sprintf(message, "from[%d:%d] '%c' (%#x)\n", count, which, ch, ch);
+       show_ditto(data, argc, message);
     }
 
     /*
      * Cleanup and exit
      */
     for (j = argc - 1; j >= 0; j--) {
-       set_term(data[j].screen);
-       endwin();
+       USING_SCREEN(data[j].screen, close_screen, 0);
+       delscreen(data[j].screen);
     }
     ExitProgram(EXIT_SUCCESS);
 }