]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/test_mouse.c
ncurses 6.4 - patch 20230424
[ncurses.git] / test / test_mouse.c
index f96d658ceb95aa9743256ee7669cbddf1523b324..9179105002e1f69e8f93c75effc29901494c571d 100644 (file)
@@ -1,6 +1,6 @@
 /****************************************************************************
+ * Copyright 2022,2023 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.8 2022/05/08 00:36:07 tom Exp $
+ * $Id: test_mouse.c,v 1.28 2023/04/16 17:36:47 tom Exp $
  *
  * Author: Leonid S Usov
  *
@@ -43,106 +43,120 @@ raw_loop(void)
     char *xtermcap;
 
     tcgetattr(0, &old);
+#if HAVE_CFMAKERAW
     cfmakeraw(&tty);
+#else
+    tty = old;
+    tty.c_iflag &= (unsigned) (~(IGNBRK | BRKINT | PARMRK | ISTRIP
+                                | INLCR | IGNCR | ICRNL | IXON));
+    tty.c_oflag &= (unsigned) (~OPOST);
+    tty.c_lflag &= (unsigned) (~(ECHO | ECHONL | ICANON | ISIG | IEXTEN));
+    tty.c_cflag &= (unsigned) (~(CSIZE | PARENB));
+    tty.c_cflag |= CS8;
+    tcsetattr(0, TCSANOW, &tty);
+#endif
 
     setupterm(NULL, 0, 0);
     xtermcap = tigetstr("XM");
-    if (xtermcap == 0 || xtermcap == (char *) -1) {
+    if (!VALID_STRING(xtermcap)) {
        fprintf(stderr, "couldn't get XM terminfo");
        return 1;
     }
 
-    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;
 
-       if (c == ERR || c == '\003') {
+       if (c == -1 || c == '\003') {
            break;
        } else if (c == '\033') {
-           printf("\r\n");
+           printf("\r\n\\E");
        } else if ((pretty = unctrl((chtype) c)) != NULL) {
            printf("%s", pretty);
        } else if (isprint(c)) {
            printf("%c", c);
        } else {
-           printf("{%x}", c);
+           printf("{%x}", UChar(c));
        }
     }
 
-    putp(tparm(xtermcap, 0));
+    putp(tgoto(xtermcap, 0, 0));
     fflush(stdout);
     tcsetattr(0, TCSANOW, &old);
     return 0;
 }
 
-static int logw(int line, const char *fmt, ...) GCC_PRINTFLIKE(2, 3);
+static void logw(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
 
-static int
-logw(int line, const char *fmt, ...)
+static void
+logw(const char *fmt, ...)
 {
+    int row = getcury(stdscr);
     va_list args;
+
     va_start(args, fmt);
-    wmove(stdscr, line++, 0);
+    wmove(stdscr, row++, 0);
     vw_printw(stdscr, fmt, args);
+    va_end(args);
+
     clrtoeol();
 
-    line %= (getmaxy(stdscr) - logoffset);
-    if (line < logoffset) {
-       line = logoffset;
+    row %= (getmaxy(stdscr) - logoffset);
+    if (row < logoffset) {
+       row = logoffset;
     }
 
-    wmove(stdscr, line, 0);
+    wmove(stdscr, row, 0);
     wprintw(stdscr, ">");
     clrtoeol();
-    return line;
 }
 
 static void
-usage(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",
-       "",
-       "Options:",
-       " -r       show raw input stream, injecting a new line before every ESC",
-       " -i n     set mouse interval to n; default is 0",
-       " -h       show this message",
-       " -T term  use terminal description other than $TERM"
+       "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 curline;
-    int c;
+    int ch;
     MEVENT event;
-    char *my_environ;
+    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);
+    while ((ch = getopt(argc, argv, OPTS_COMMON "i:rT:")) != -1) {
+       switch (ch) {
        case 'i':
            interval = atoi(optarg);
            break;
@@ -151,16 +165,21 @@ main(int argc, char *argv[])
            break;
        case 'T':
            my_environ = malloc(strlen(term_format) + strlen(optarg));
-           sprintf(my_environ, term_format, optarg);
-           putenv(my_environ);
+           if (my_environ != NULL) {
+               sprintf(my_environ, term_format, optarg);
+               putenv(my_environ);
+           }
            break;
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
        default:
-           usage();
-           ExitProgram(EXIT_FAILURE);
+           usage(ch == OPTS_USAGE);
+           /* NOTREACHED */
        }
     }
     if (optind < argc) {
-       usage();
+       usage(FALSE);
        ExitProgram(EXIT_FAILURE);
     }
 
@@ -170,7 +189,6 @@ main(int argc, char *argv[])
     }
 
     initscr();
-    clear();
     noecho();
     cbreak();                  /* Line buffering disabled; pass everything */
     nonl();
@@ -180,13 +198,14 @@ main(int argc, char *argv[])
     mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
     mouseinterval(interval);
 
-    logoffset = logw(logoffset, "Ctrl-c to quit");
-    logoffset = logw(logoffset, "--------------");
-
-    curline = logoffset;
+    logw("Ctrl-c to quit");
+    logw("--------------");
+    if (my_environ)
+       logw("%s", my_environ);
+    logoffset = getcury(stdscr);
 
     while (1) {
-       c = getch();
+       int c = getch();
 
        switch (c) {
        case KEY_MOUSE:
@@ -194,39 +213,67 @@ 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 |
+                                         NCURSES_BUTTON_CLICKED |
+                                         NCURSES_DOUBLE_CLICKED |
+                                         NCURSES_TRIPLE_CLICKED);
+               bool found = FALSE;
                for (btn = 1; btn <= max_btn; btn++) {
                    events = (mmask_t) (event.bstate
-                                       & NCURSES_MOUSE_MASK(btn,
-                                                            NCURSES_BUTTON_RELEASED |
-                                                            NCURSES_BUTTON_PRESSED |
-                                                            NCURSES_BUTTON_CLICKED |
-                                                            NCURSES_DOUBLE_CLICKED |
-                                                            NCURSES_TRIPLE_CLICKED));
+                                       & NCURSES_MOUSE_MASK(btn, btn_mask));
                    if (events == 0)
                        continue;
-#define Show(btn,name) ((event.bstate & NCURSES_MOUSE_MASK(btn, name)) != 0) ? #name : ""
-                   curline = logw(curline,
-                                  "button %d %s %s %s %s %s %d[%x] @ %d, %d",
-                                  btn,
-                                  Show(btn, NCURSES_BUTTON_RELEASED),
-                                  Show(btn, NCURSES_BUTTON_PRESSED),
-                                  Show(btn, NCURSES_BUTTON_CLICKED),
-                                  Show(btn, NCURSES_DOUBLE_CLICKED),
-                                  Show(btn, NCURSES_TRIPLE_CLICKED),
-                                  (event.bstate & REPORT_MOUSE_POSITION) != 0,
-                                  events,
-                                  event.y, event.x);
+#define ShowQ(btn,name) \
+       (((event.bstate & NCURSES_MOUSE_MASK(btn, NCURSES_ ## name)) != 0) \
+        ? (" " #name) \
+        : "")
+#define ShowM(name) \
+       (((event.bstate & NCURSES_MOUSE_MASK(btn, BUTTON_ ## name)) != 0) \
+        ? (" " #name) \
+        : "")
+#define ShowP() \
+        ((event.bstate & REPORT_MOUSE_POSITION) != 0 \
+         ? " position" \
+         : "")
+                   logw("[%08lX] button %d%s%s%s%s%s%s%s%s%s @ %d, %d",
+                        (unsigned long) events,
+                        btn,
+                        ShowQ(btn, BUTTON_RELEASED),
+                        ShowQ(btn, BUTTON_PRESSED),
+                        ShowQ(btn, BUTTON_CLICKED),
+                        ShowQ(btn, DOUBLE_CLICKED),
+                        ShowQ(btn, TRIPLE_CLICKED),
+                        ShowM(SHIFT),
+                        ShowM(CTRL),
+                        ShowM(ALT),
+                        ShowP(),
+                        event.y, event.x);
+                   found = TRUE;
+               }
+               /*
+                * A position report need not have a button associated with it.
+                * The modifiers probably are unused.
+                */
+               if (!found && (event.bstate & REPORT_MOUSE_POSITION)) {
+                   logw("[%08lX]%s%s%s%s @ %d, %d",
+                        (unsigned long) events,
+                        ShowM(SHIFT),
+                        ShowM(CTRL),
+                        ShowM(ALT),
+                        ShowP(),
+                        event.y, event.x);
                }
            }
            break;
        case '\003':
            goto end;
        default:
-           curline = logw(curline, "got another char: 0x%x", c);
+           logw("got another char: 0x%x", UChar(c));
        }
        refresh();
     }