]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_mouse.c
ncurses 6.3 - patch 20221126
[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.23 2022/08/20 18:12:16 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 &= (unsigned) (~(IGNBRK | BRKINT | PARMRK | ISTRIP
51                                  | INLCR | IGNCR | ICRNL | IXON));
52     tty.c_oflag &= (unsigned) (~OPOST);
53     tty.c_lflag &= (unsigned) (~(ECHO | ECHONL | ICANON | ISIG | IEXTEN));
54     tty.c_cflag &= (unsigned) (~(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     va_list args;
101
102     va_start(args, fmt);
103     wmove(stdscr, row++, 0);
104     vw_printw(stdscr, fmt, args);
105     va_end(args);
106
107     clrtoeol();
108
109     row %= (getmaxy(stdscr) - logoffset);
110     if (row < logoffset) {
111         row = logoffset;
112     }
113
114     wmove(stdscr, row, 0);
115     wprintw(stdscr, ">");
116     clrtoeol();
117 }
118
119 static void
120 usage(void)
121 {
122     static const char *msg[] =
123     {
124         "Usage: test_mouse [options]",
125         "",
126         "Test mouse events.  These examples for $TERM demonstrate xterm",
127         "features:",
128         "    xterm",
129         "    xterm-1002",
130         "    xterm-1003",
131         "",
132         "Options:",
133         " -r       show raw input stream, injecting a new line before every ESC",
134         " -i n     set mouse interval to n; default is 0 (no double-clicks)",
135         " -h       show this message",
136         " -T term  use terminal description other than $TERM"
137     };
138     unsigned n;
139     for (n = 0; n < sizeof(msg) / sizeof(char *); ++n) {
140         fprintf(stderr, "%s\n", msg[n]);
141     }
142 }
143
144 int
145 main(int argc, char *argv[])
146 {
147     bool rawmode = FALSE;
148     int interval = 0;
149     int c;
150     MEVENT event;
151     char *my_environ = NULL;
152     const char *term_format = "TERM=%s";
153
154     while ((c = getopt(argc, argv, "hi:rT:")) != -1) {
155         switch (c) {
156         case 'h':
157             usage();
158             ExitProgram(EXIT_SUCCESS);
159         case 'i':
160             interval = atoi(optarg);
161             break;
162         case 'r':
163             rawmode = TRUE;
164             break;
165         case 'T':
166             my_environ = malloc(strlen(term_format) + strlen(optarg));
167             sprintf(my_environ, term_format, optarg);
168             putenv(my_environ);
169             break;
170         default:
171             usage();
172             ExitProgram(EXIT_FAILURE);
173         }
174     }
175     if (optind < argc) {
176         usage();
177         ExitProgram(EXIT_FAILURE);
178     }
179
180     if (rawmode) {
181         printf("Entering raw mode. Ctrl-c to quit.\n");
182         return raw_loop();
183     }
184
185     initscr();
186     noecho();
187     cbreak();                   /* Line buffering disabled; pass everything */
188     nonl();
189     keypad(stdscr, TRUE);
190
191     /* Get all the mouse events */
192     mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
193     mouseinterval(interval);
194
195     logw("Ctrl-c to quit");
196     logw("--------------");
197     if (my_environ)
198         logw("%s", my_environ);
199     logoffset = getcury(stdscr);
200
201     while (1) {
202         c = getch();
203
204         switch (c) {
205         case KEY_MOUSE:
206             if (getmouse(&event) == OK) {
207                 unsigned btn;
208                 mmask_t events;
209 #if NCURSES_MOUSE_VERSION > 1
210                 const unsigned max_btn = 5;
211 #else
212                 const unsigned max_btn = 4;
213 #endif
214                 const mmask_t btn_mask = (NCURSES_BUTTON_RELEASED |
215                                           NCURSES_BUTTON_PRESSED |
216                                           NCURSES_BUTTON_CLICKED |
217                                           NCURSES_DOUBLE_CLICKED |
218                                           NCURSES_TRIPLE_CLICKED);
219                 bool found = FALSE;
220                 for (btn = 1; btn <= max_btn; btn++) {
221                     events = (mmask_t) (event.bstate
222                                         & NCURSES_MOUSE_MASK(btn, btn_mask));
223                     if (events == 0)
224                         continue;
225 #define ShowQ(btn,name) \
226         (((event.bstate & NCURSES_MOUSE_MASK(btn, NCURSES_ ## name)) != 0) \
227          ? (" " #name) \
228          : "")
229 #define ShowM(name) \
230         (((event.bstate & NCURSES_MOUSE_MASK(btn, BUTTON_ ## name)) != 0) \
231          ? (" " #name) \
232          : "")
233 #define ShowP() \
234          ((event.bstate & REPORT_MOUSE_POSITION) != 0 \
235           ? " position" \
236           : "")
237                     logw("[%08lX] button %d%s%s%s%s%s%s%s%s%s @ %d, %d",
238                          (unsigned long) events,
239                          btn,
240                          ShowQ(btn, BUTTON_RELEASED),
241                          ShowQ(btn, BUTTON_PRESSED),
242                          ShowQ(btn, BUTTON_CLICKED),
243                          ShowQ(btn, DOUBLE_CLICKED),
244                          ShowQ(btn, TRIPLE_CLICKED),
245                          ShowM(SHIFT),
246                          ShowM(CTRL),
247                          ShowM(ALT),
248                          ShowP(),
249                          event.y, event.x);
250                     found = TRUE;
251                 }
252                 /*
253                  * A position report need not have a button associated with it.
254                  * The modifiers probably are unused.
255                  */
256                 if (!found && (event.bstate & REPORT_MOUSE_POSITION)) {
257                     logw("[%08lX]%s%s%s%s @ %d, %d",
258                          (unsigned long) events,
259                          ShowM(SHIFT),
260                          ShowM(CTRL),
261                          ShowM(ALT),
262                          ShowP(),
263                          event.y, event.x);
264                 }
265             }
266             break;
267         case '\003':
268             goto end;
269         default:
270             logw("got another char: 0x%x", UChar(c));
271         }
272         refresh();
273     }
274   end:
275     endwin();
276     ExitProgram(EXIT_SUCCESS);
277 }
278 #else
279 int
280 main(void)
281 {
282     printf("This program requires the ncurses library\n");
283     ExitProgram(EXIT_FAILURE);
284 }
285 #endif