]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_mouse.c
ncurses 6.3 - patch 20221203
[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.26 2022/12/04 00:40:11 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(int ok)
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         ,USAGE_COMMON
133         ,"Options:"
134         ," -r       show raw input stream, injecting a new line before every ESC"
135         ," -i n     set mouse interval to n; default is 0 (no double-clicks)"
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     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
143 }
144 /* *INDENT-OFF* */
145 VERSION_COMMON()
146 /* *INDENT-ON* */
147
148 int
149 main(int argc, char *argv[])
150 {
151     bool rawmode = FALSE;
152     int interval = 0;
153     int ch;
154     MEVENT event;
155     char *my_environ = NULL;
156     const char *term_format = "TERM=%s";
157
158     while ((ch = getopt(argc, argv, OPTS_COMMON "i:rT:")) != -1) {
159         switch (ch) {
160         case 'i':
161             interval = atoi(optarg);
162             break;
163         case 'r':
164             rawmode = TRUE;
165             break;
166         case 'T':
167             my_environ = malloc(strlen(term_format) + strlen(optarg));
168             sprintf(my_environ, term_format, optarg);
169             putenv(my_environ);
170             break;
171         case OPTS_VERSION:
172             show_version(argv);
173             ExitProgram(EXIT_SUCCESS);
174         default:
175             usage(ch == OPTS_USAGE);
176             /* NOTREACHED */
177         }
178     }
179     if (optind < argc) {
180         usage(FALSE);
181         ExitProgram(EXIT_FAILURE);
182     }
183
184     if (rawmode) {
185         printf("Entering raw mode. Ctrl-c to quit.\n");
186         return raw_loop();
187     }
188
189     initscr();
190     noecho();
191     cbreak();                   /* Line buffering disabled; pass everything */
192     nonl();
193     keypad(stdscr, TRUE);
194
195     /* Get all the mouse events */
196     mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
197     mouseinterval(interval);
198
199     logw("Ctrl-c to quit");
200     logw("--------------");
201     if (my_environ)
202         logw("%s", my_environ);
203     logoffset = getcury(stdscr);
204
205     while (1) {
206         int c = getch();
207
208         switch (c) {
209         case KEY_MOUSE:
210             if (getmouse(&event) == OK) {
211                 unsigned btn;
212                 mmask_t events;
213 #if NCURSES_MOUSE_VERSION > 1
214                 const unsigned max_btn = 5;
215 #else
216                 const unsigned max_btn = 4;
217 #endif
218                 const mmask_t btn_mask = (NCURSES_BUTTON_RELEASED |
219                                           NCURSES_BUTTON_PRESSED |
220                                           NCURSES_BUTTON_CLICKED |
221                                           NCURSES_DOUBLE_CLICKED |
222                                           NCURSES_TRIPLE_CLICKED);
223                 bool found = FALSE;
224                 for (btn = 1; btn <= max_btn; btn++) {
225                     events = (mmask_t) (event.bstate
226                                         & NCURSES_MOUSE_MASK(btn, btn_mask));
227                     if (events == 0)
228                         continue;
229 #define ShowQ(btn,name) \
230         (((event.bstate & NCURSES_MOUSE_MASK(btn, NCURSES_ ## name)) != 0) \
231          ? (" " #name) \
232          : "")
233 #define ShowM(name) \
234         (((event.bstate & NCURSES_MOUSE_MASK(btn, BUTTON_ ## name)) != 0) \
235          ? (" " #name) \
236          : "")
237 #define ShowP() \
238          ((event.bstate & REPORT_MOUSE_POSITION) != 0 \
239           ? " position" \
240           : "")
241                     logw("[%08lX] button %d%s%s%s%s%s%s%s%s%s @ %d, %d",
242                          (unsigned long) events,
243                          btn,
244                          ShowQ(btn, BUTTON_RELEASED),
245                          ShowQ(btn, BUTTON_PRESSED),
246                          ShowQ(btn, BUTTON_CLICKED),
247                          ShowQ(btn, DOUBLE_CLICKED),
248                          ShowQ(btn, TRIPLE_CLICKED),
249                          ShowM(SHIFT),
250                          ShowM(CTRL),
251                          ShowM(ALT),
252                          ShowP(),
253                          event.y, event.x);
254                     found = TRUE;
255                 }
256                 /*
257                  * A position report need not have a button associated with it.
258                  * The modifiers probably are unused.
259                  */
260                 if (!found && (event.bstate & REPORT_MOUSE_POSITION)) {
261                     logw("[%08lX]%s%s%s%s @ %d, %d",
262                          (unsigned long) events,
263                          ShowM(SHIFT),
264                          ShowM(CTRL),
265                          ShowM(ALT),
266                          ShowP(),
267                          event.y, event.x);
268                 }
269             }
270             break;
271         case '\003':
272             goto end;
273         default:
274             logw("got another char: 0x%x", UChar(c));
275         }
276         refresh();
277     }
278   end:
279     endwin();
280     ExitProgram(EXIT_SUCCESS);
281 }
282 #else
283 int
284 main(void)
285 {
286     printf("This program requires the ncurses library\n");
287     ExitProgram(EXIT_FAILURE);
288 }
289 #endif