]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_mouse.c
ncurses 6.3 - patch 20220716
[ncurses.git] / test / test_mouse.c
1 /****************************************************************************
2  * Copyright 2022 Leonid S. Usov <leonid.s.usov at gmail.com>               *
3  * Copyright 2022 Thomas E. Dickey                                          *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  ****************************************************************************/
24 /*
25  * $Id: test_mouse.c,v 1.20 2022/07/16 18:52:09 tom Exp $
26  *
27  * Author: Leonid S Usov
28  *
29  * Observe mouse events in the raw terminal or parsed ncurses modes
30  */
31
32 #include <test.priv.h>
33
34 #if defined(NCURSES_MOUSE_VERSION) && !defined(_NC_WINDOWS)
35
36 static int logoffset = 0;
37
38 static int
39 raw_loop(void)
40 {
41     struct termios tty;
42     struct termios old;
43     char *xtermcap;
44
45     tcgetattr(0, &old);
46 #if HAVE_CFMAKERAW
47     cfmakeraw(&tty);
48 #else
49     tty = old;
50     tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
51                      | INLCR | IGNCR | ICRNL | IXON);
52     tty.c_oflag &= ~OPOST;
53     tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
54     tty.c_cflag &= ~(CSIZE | PARENB);
55     tty.c_cflag |= CS8;
56     tcsetattr(0, TCSANOW, &tty);
57 #endif
58
59     setupterm(NULL, 0, 0);
60     xtermcap = tigetstr("XM");
61     if (!VALID_STRING(xtermcap)) {
62         fprintf(stderr, "couldn't get XM terminfo");
63         return 1;
64     }
65
66     putp(tparm(xtermcap, 1));
67     fflush(stdout);
68
69     tcsetattr(0, TCSANOW, &tty);
70
71     while (1) {
72         int c = getc(stdin);
73         const char *pretty;
74
75         if (c == -1 || c == '\003') {
76             break;
77         } else if (c == '\033') {
78             printf("\r\n\\E");
79         } else if ((pretty = unctrl((chtype) c)) != NULL) {
80             printf("%s", pretty);
81         } else if (isprint(c)) {
82             printf("%c", c);
83         } else {
84             printf("{%x}", UChar(c));
85         }
86     }
87
88     putp(tparm(xtermcap, 0));
89     fflush(stdout);
90     tcsetattr(0, TCSANOW, &old);
91     return 0;
92 }
93
94 static void logw(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
95
96 static void
97 logw(const char *fmt, ...)
98 {
99     int row = getcury(stdscr);
100
101     va_list args;
102     va_start(args, fmt);
103     wmove(stdscr, row++, 0);
104     vw_printw(stdscr, fmt, args);
105     clrtoeol();
106
107     row %= (getmaxy(stdscr) - logoffset);
108     if (row < logoffset) {
109         row = logoffset;
110     }
111
112     wmove(stdscr, row, 0);
113     wprintw(stdscr, ">");
114     clrtoeol();
115 }
116
117 static void
118 usage(void)
119 {
120     static const char *msg[] =
121     {
122         "Usage: test_mouse [options]",
123         "",
124         "Test mouse events.  These examples for $TERM demonstrate xterm"
125         "features:",
126         "    xterm",
127         "    xterm-1002",
128         "    xterm-1003",
129         "",
130         "Options:",
131         " -r       show raw input stream, injecting a new line before every ESC",
132         " -i n     set mouse interval to n; default is 0 (no double-clicks)",
133         " -h       show this message",
134         " -T term  use terminal description other than $TERM"
135     };
136     unsigned n;
137     for (n = 0; n < sizeof(msg) / sizeof(char *); ++n) {
138         fprintf(stderr, "%s\n", msg[n]);
139     }
140 }
141
142 int
143 main(int argc, char *argv[])
144 {
145     bool rawmode = FALSE;
146     int interval = 0;
147     int c;
148     MEVENT event;
149     char *my_environ = NULL;
150     const char *term_format = "TERM=%s";
151
152     while ((c = getopt(argc, argv, "hi:rT:")) != -1) {
153         switch (c) {
154         case 'h':
155             usage();
156             ExitProgram(EXIT_SUCCESS);
157         case 'i':
158             interval = atoi(optarg);
159             break;
160         case 'r':
161             rawmode = TRUE;
162             break;
163         case 'T':
164             my_environ = malloc(strlen(term_format) + strlen(optarg));
165             sprintf(my_environ, term_format, optarg);
166             putenv(my_environ);
167             break;
168         default:
169             usage();
170             ExitProgram(EXIT_FAILURE);
171         }
172     }
173     if (optind < argc) {
174         usage();
175         ExitProgram(EXIT_FAILURE);
176     }
177
178     if (rawmode) {
179         printf("Entering raw mode. Ctrl-c to quit.\n");
180         return raw_loop();
181     }
182
183     initscr();
184     noecho();
185     cbreak();                   /* Line buffering disabled; pass everything */
186     nonl();
187     keypad(stdscr, TRUE);
188
189     /* Get all the mouse events */
190     mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
191     mouseinterval(interval);
192
193     logw("Ctrl-c to quit");
194     logw("--------------");
195     if (my_environ)
196         logw("%s", my_environ);
197     logoffset = getcury(stdscr);
198
199     while (1) {
200         c = getch();
201
202         switch (c) {
203         case KEY_MOUSE:
204             if (getmouse(&event) == OK) {
205                 unsigned btn;
206                 mmask_t events;
207 #if NCURSES_MOUSE_VERSION > 1
208                 const unsigned max_btn = 5;
209 #else
210                 const unsigned max_btn = 4;
211 #endif
212                 const mmask_t btn_mask = (NCURSES_BUTTON_RELEASED |
213                                           NCURSES_BUTTON_PRESSED |
214                                           NCURSES_BUTTON_CLICKED |
215                                           NCURSES_DOUBLE_CLICKED |
216                                           NCURSES_TRIPLE_CLICKED);
217                 bool found = FALSE;
218                 for (btn = 1; btn <= max_btn; btn++) {
219                     events = (mmask_t) (event.bstate
220                                         & NCURSES_MOUSE_MASK(btn, btn_mask));
221                     if (events == 0)
222                         continue;
223 #define ShowQ(btn,name) \
224         (((event.bstate & NCURSES_MOUSE_MASK(btn, NCURSES_ ## name)) != 0) \
225          ? (" " #name) \
226          : "")
227 #define ShowM(name) \
228         (((event.bstate & NCURSES_MOUSE_MASK(btn, BUTTON_ ## name)) != 0) \
229          ? (" " #name) \
230          : "")
231 #define ShowP() \
232          ((event.bstate & REPORT_MOUSE_POSITION) != 0 \
233           ? " position" \
234           : "")
235                     logw("[%08lX] button %d%s%s%s%s%s%s%s%s%s @ %d, %d",
236                          (unsigned long) events,
237                          btn,
238                          ShowQ(btn, BUTTON_RELEASED),
239                          ShowQ(btn, BUTTON_PRESSED),
240                          ShowQ(btn, BUTTON_CLICKED),
241                          ShowQ(btn, DOUBLE_CLICKED),
242                          ShowQ(btn, TRIPLE_CLICKED),
243                          ShowM(SHIFT),
244                          ShowM(CTRL),
245                          ShowM(ALT),
246                          ShowP(),
247                          event.y, event.x);
248                     found = TRUE;
249                 }
250                 /*
251                  * A position report need not have a button associated with it.
252                  * The modifiers probably are unused.
253                  */
254                 if (!found && (event.bstate & REPORT_MOUSE_POSITION)) {
255                     logw("[%08lX]%s%s%s%s @ %d, %d",
256                          (unsigned long) events,
257                          ShowM(SHIFT),
258                          ShowM(CTRL),
259                          ShowM(ALT),
260                          ShowP(),
261                          event.y, event.x);
262                 }
263             }
264             break;
265         case '\003':
266             goto end;
267         default:
268             logw("got another char: 0x%x", UChar(c));
269         }
270         refresh();
271     }
272   end:
273     endwin();
274     ExitProgram(EXIT_SUCCESS);
275 }
276 #else
277 int
278 main(void)
279 {
280     printf("This program requires the ncurses library\n");
281     ExitProgram(EXIT_FAILURE);
282 }
283 #endif