]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/test_mouse.c
ncurses 6.5 - patch 20240504
[ncurses.git] / test / test_mouse.c
index 8cb12d9fc6e17b25612ae2c912d5a7f24886e827..5508ea42f50343fffa0ffe82c6db4a43b87c92ec 100644 (file)
@@ -1,6 +1,6 @@
 /****************************************************************************
+ * Copyright 2022-2023,2024 Thomas E. Dickey                                *
  * Copyright 2022 Leonid S. Usov <leonid.s.usov at gmail.com>               *
- * Copyright 2022 Thomas E. Dickey                                          *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -22,7 +22,7 @@
  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
  ****************************************************************************/
 /*
- * $Id: test_mouse.c,v 1.18 2022/05/15 00:20:27 tom Exp $
+ * $Id: test_mouse.c,v 1.31 2024/03/30 20:45:31 tom Exp $
  *
  * Author: Leonid S Usov
  *
 
 static int logoffset = 0;
 
-static int
+static void
 raw_loop(void)
 {
-    struct termios tty;
-    struct termios old;
     char *xtermcap;
 
-    tcgetattr(0, &old);
-    cfmakeraw(&tty);
+    printf("Entering raw mode. Ctrl-c to quit.\n");
 
-    setupterm(NULL, 0, 0);
+    newterm(NULL, stdout, stdin);
+    raw();
     xtermcap = tigetstr("XM");
     if (!VALID_STRING(xtermcap)) {
        fprintf(stderr, "couldn't get XM terminfo");
-       return 1;
+       return;
     }
 
-    putp(tparm(xtermcap, 1));
+    putp(tgoto(xtermcap, 1, 1));
     fflush(stdout);
 
-    tcsetattr(0, TCSANOW, &tty);
-
-    while (true) {
+    while (1) {
        int c = getc(stdin);
        const char *pretty;
 
@@ -74,10 +70,9 @@ raw_loop(void)
        }
     }
 
-    putp(tparm(xtermcap, 0));
+    putp(tgoto(xtermcap, 0, 0));
     fflush(stdout);
-    tcsetattr(0, TCSANOW, &old);
-    return 0;
+    noraw();
 }
 
 static void logw(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
@@ -86,11 +81,13 @@ static void
 logw(const char *fmt, ...)
 {
     int row = getcury(stdscr);
-
     va_list args;
+
     va_start(args, fmt);
     wmove(stdscr, row++, 0);
     vw_printw(stdscr, fmt, args);
+    va_end(args);
+
     clrtoeol();
 
     row %= (getmaxy(stdscr) - logoffset);
@@ -104,70 +101,9 @@ logw(const char *fmt, ...)
 }
 
 static void
-usage(void)
+cooked_loop(char *my_environ, int interval)
 {
-    static const char *msg[] =
-    {
-       "Usage: test_mouse [options]",
-       "",
-       "Test mouse events.  These examples for $TERM demonstrate xterm"
-       "features:",
-       "    xterm",
-       "    xterm-1002",
-       "    xterm-1003",
-       "",
-       "Options:",
-       " -r       show raw input stream, injecting a new line before every ESC",
-       " -i n     set mouse interval to n; default is 0 (no double-clicks)",
-       " -h       show this message",
-       " -T term  use terminal description other than $TERM"
-    };
-    unsigned n;
-    for (n = 0; n < sizeof(msg) / sizeof(char *); ++n) {
-       fprintf(stderr, "%s\n", msg[n]);
-    }
-}
-
-int
-main(int argc, char *argv[])
-{
-    bool rawmode = FALSE;
-    int interval = 0;
-    int c;
     MEVENT event;
-    char *my_environ = NULL;
-    const char *term_format = "TERM=%s";
-
-    while ((c = getopt(argc, argv, "hi:rT:")) != -1) {
-       switch (c) {
-       case 'h':
-           usage();
-           ExitProgram(EXIT_SUCCESS);
-       case 'i':
-           interval = atoi(optarg);
-           break;
-       case 'r':
-           rawmode = TRUE;
-           break;
-       case 'T':
-           my_environ = malloc(strlen(term_format) + strlen(optarg));
-           sprintf(my_environ, term_format, optarg);
-           putenv(my_environ);
-           break;
-       default:
-           usage();
-           ExitProgram(EXIT_FAILURE);
-       }
-    }
-    if (optind < argc) {
-       usage();
-       ExitProgram(EXIT_FAILURE);
-    }
-
-    if (rawmode) {
-       printf("Entering raw mode. Ctrl-c to quit.\n");
-       return raw_loop();
-    }
 
     initscr();
     noecho();
@@ -186,7 +122,7 @@ main(int argc, char *argv[])
     logoffset = getcury(stdscr);
 
     while (1) {
-       c = getch();
+       int c = getch();
 
        switch (c) {
        case KEY_MOUSE:
@@ -194,9 +130,9 @@ main(int argc, char *argv[])
                unsigned btn;
                mmask_t events;
 #if NCURSES_MOUSE_VERSION > 1
-               const int max_btn = 5;
+               const unsigned max_btn = 5;
 #else
-               const int max_btn = 4;
+               const unsigned max_btn = 4;
 #endif
                const mmask_t btn_mask = (NCURSES_BUTTON_RELEASED |
                                          NCURSES_BUTTON_PRESSED |
@@ -260,6 +196,82 @@ main(int argc, char *argv[])
     }
   end:
     endwin();
+}
+
+static void
+usage(int ok)
+{
+    static const char *msg[] =
+    {
+       "Usage: test_mouse [options]"
+       ,""
+       ,"Test mouse events.  These examples for $TERM demonstrate xterm"
+       ,"features:"
+       ,"    xterm"
+       ,"    xterm-1002"
+       ,"    xterm-1003"
+       ,""
+       ,USAGE_COMMON
+       ,"Options:"
+       ," -r       show raw input stream, injecting a new line before every ESC"
+       ," -i n     set mouse interval to n; default is 0 (no double-clicks)"
+       ," -T term  use terminal description other than $TERM"
+    };
+    unsigned n;
+    for (n = 0; n < sizeof(msg) / sizeof(char *); ++n) {
+       fprintf(stderr, "%s\n", msg[n]);
+    }
+    ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
+}
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
+
+int
+main(int argc, char *argv[])
+{
+    bool rawmode = FALSE;
+    int interval = 0;
+    int ch;
+    size_t my_len;
+    char *my_environ = NULL;
+    const char *term_format = "TERM=%s";
+
+    while ((ch = getopt(argc, argv, OPTS_COMMON "i:rT:")) != -1) {
+       switch (ch) {
+       case 'i':
+           interval = atoi(optarg);
+           break;
+       case 'r':
+           rawmode = TRUE;
+           break;
+       case 'T':
+           my_len = strlen(term_format) + strlen(optarg) + 1;
+           my_environ = malloc(my_len);
+           if (my_environ != NULL) {
+               _nc_SPRINTF(my_environ, _nc_SLIMIT(my_len) term_format, optarg);
+               putenv(my_environ);
+           }
+           break;
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
+       default:
+           usage(ch == OPTS_USAGE);
+           /* NOTREACHED */
+       }
+    }
+    if (optind < argc) {
+       usage(FALSE);
+       ExitProgram(EXIT_FAILURE);
+    }
+
+    if (rawmode) {
+       raw_loop();
+    } else {
+       cooked_loop(my_environ, interval);
+    }
+
     ExitProgram(EXIT_SUCCESS);
 }
 #else