1 /****************************************************************************
2 * Copyright 2022 Leonid S. Usov <leonid.s.usov at gmail.com> *
3 * Copyright 2022 Thomas E. Dickey *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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 ****************************************************************************/
25 * $Id: test_mouse.c,v 1.22 2022/07/24 15:18:53 tom Exp $
27 * Author: Leonid S Usov
29 * Observe mouse events in the raw terminal or parsed ncurses modes
32 #include <test.priv.h>
34 #if defined(NCURSES_MOUSE_VERSION) && !defined(_NC_WINDOWS)
36 static int logoffset = 0;
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));
56 tcsetattr(0, TCSANOW, &tty);
59 setupterm(NULL, 0, 0);
60 xtermcap = tigetstr("XM");
61 if (!VALID_STRING(xtermcap)) {
62 fprintf(stderr, "couldn't get XM terminfo");
66 putp(tparm(xtermcap, 1));
69 tcsetattr(0, TCSANOW, &tty);
75 if (c == -1 || c == '\003') {
77 } else if (c == '\033') {
79 } else if ((pretty = unctrl((chtype) c)) != NULL) {
81 } else if (isprint(c)) {
84 printf("{%x}", UChar(c));
88 putp(tparm(xtermcap, 0));
90 tcsetattr(0, TCSANOW, &old);
94 static void logw(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
97 logw(const char *fmt, ...)
99 int row = getcury(stdscr);
103 wmove(stdscr, row++, 0);
104 vw_printw(stdscr, fmt, args);
107 row %= (getmaxy(stdscr) - logoffset);
108 if (row < logoffset) {
112 wmove(stdscr, row, 0);
113 wprintw(stdscr, ">");
120 static const char *msg[] =
122 "Usage: test_mouse [options]",
124 "Test mouse events. These examples for $TERM demonstrate xterm",
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"
137 for (n = 0; n < sizeof(msg) / sizeof(char *); ++n) {
138 fprintf(stderr, "%s\n", msg[n]);
143 main(int argc, char *argv[])
145 bool rawmode = FALSE;
149 char *my_environ = NULL;
150 const char *term_format = "TERM=%s";
152 while ((c = getopt(argc, argv, "hi:rT:")) != -1) {
156 ExitProgram(EXIT_SUCCESS);
158 interval = atoi(optarg);
164 my_environ = malloc(strlen(term_format) + strlen(optarg));
165 sprintf(my_environ, term_format, optarg);
170 ExitProgram(EXIT_FAILURE);
175 ExitProgram(EXIT_FAILURE);
179 printf("Entering raw mode. Ctrl-c to quit.\n");
185 cbreak(); /* Line buffering disabled; pass everything */
187 keypad(stdscr, TRUE);
189 /* Get all the mouse events */
190 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
191 mouseinterval(interval);
193 logw("Ctrl-c to quit");
194 logw("--------------");
196 logw("%s", my_environ);
197 logoffset = getcury(stdscr);
204 if (getmouse(&event) == OK) {
207 #if NCURSES_MOUSE_VERSION > 1
208 const unsigned max_btn = 5;
210 const unsigned max_btn = 4;
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);
218 for (btn = 1; btn <= max_btn; btn++) {
219 events = (mmask_t) (event.bstate
220 & NCURSES_MOUSE_MASK(btn, btn_mask));
223 #define ShowQ(btn,name) \
224 (((event.bstate & NCURSES_MOUSE_MASK(btn, NCURSES_ ## name)) != 0) \
227 #define ShowM(name) \
228 (((event.bstate & NCURSES_MOUSE_MASK(btn, BUTTON_ ## name)) != 0) \
232 ((event.bstate & REPORT_MOUSE_POSITION) != 0 \
235 logw("[%08lX] button %d%s%s%s%s%s%s%s%s%s @ %d, %d",
236 (unsigned long) events,
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),
251 * A position report need not have a button associated with it.
252 * The modifiers probably are unused.
254 if (!found && (event.bstate & REPORT_MOUSE_POSITION)) {
255 logw("[%08lX]%s%s%s%s @ %d, %d",
256 (unsigned long) events,
268 logw("got another char: 0x%x", UChar(c));
274 ExitProgram(EXIT_SUCCESS);
280 printf("This program requires the ncurses library\n");
281 ExitProgram(EXIT_FAILURE);