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.18 2022/05/15 00:20:27 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;
48 setupterm(NULL, 0, 0);
49 xtermcap = tigetstr("XM");
50 if (!VALID_STRING(xtermcap)) {
51 fprintf(stderr, "couldn't get XM terminfo");
55 putp(tparm(xtermcap, 1));
58 tcsetattr(0, TCSANOW, &tty);
64 if (c == -1 || c == '\003') {
66 } else if (c == '\033') {
68 } else if ((pretty = unctrl((chtype) c)) != NULL) {
70 } else if (isprint(c)) {
73 printf("{%x}", UChar(c));
77 putp(tparm(xtermcap, 0));
79 tcsetattr(0, TCSANOW, &old);
83 static void logw(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
86 logw(const char *fmt, ...)
88 int row = getcury(stdscr);
92 wmove(stdscr, row++, 0);
93 vw_printw(stdscr, fmt, args);
96 row %= (getmaxy(stdscr) - logoffset);
97 if (row < logoffset) {
101 wmove(stdscr, row, 0);
102 wprintw(stdscr, ">");
109 static const char *msg[] =
111 "Usage: test_mouse [options]",
113 "Test mouse events. These examples for $TERM demonstrate xterm"
120 " -r show raw input stream, injecting a new line before every ESC",
121 " -i n set mouse interval to n; default is 0 (no double-clicks)",
122 " -h show this message",
123 " -T term use terminal description other than $TERM"
126 for (n = 0; n < sizeof(msg) / sizeof(char *); ++n) {
127 fprintf(stderr, "%s\n", msg[n]);
132 main(int argc, char *argv[])
134 bool rawmode = FALSE;
138 char *my_environ = NULL;
139 const char *term_format = "TERM=%s";
141 while ((c = getopt(argc, argv, "hi:rT:")) != -1) {
145 ExitProgram(EXIT_SUCCESS);
147 interval = atoi(optarg);
153 my_environ = malloc(strlen(term_format) + strlen(optarg));
154 sprintf(my_environ, term_format, optarg);
159 ExitProgram(EXIT_FAILURE);
164 ExitProgram(EXIT_FAILURE);
168 printf("Entering raw mode. Ctrl-c to quit.\n");
174 cbreak(); /* Line buffering disabled; pass everything */
176 keypad(stdscr, TRUE);
178 /* Get all the mouse events */
179 mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
180 mouseinterval(interval);
182 logw("Ctrl-c to quit");
183 logw("--------------");
185 logw("%s", my_environ);
186 logoffset = getcury(stdscr);
193 if (getmouse(&event) == OK) {
196 #if NCURSES_MOUSE_VERSION > 1
197 const int max_btn = 5;
199 const int max_btn = 4;
201 const mmask_t btn_mask = (NCURSES_BUTTON_RELEASED |
202 NCURSES_BUTTON_PRESSED |
203 NCURSES_BUTTON_CLICKED |
204 NCURSES_DOUBLE_CLICKED |
205 NCURSES_TRIPLE_CLICKED);
207 for (btn = 1; btn <= max_btn; btn++) {
208 events = (mmask_t) (event.bstate
209 & NCURSES_MOUSE_MASK(btn, btn_mask));
212 #define ShowQ(btn,name) \
213 (((event.bstate & NCURSES_MOUSE_MASK(btn, NCURSES_ ## name)) != 0) \
216 #define ShowM(name) \
217 (((event.bstate & NCURSES_MOUSE_MASK(btn, BUTTON_ ## name)) != 0) \
221 ((event.bstate & REPORT_MOUSE_POSITION) != 0 \
224 logw("[%08lX] button %d%s%s%s%s%s%s%s%s%s @ %d, %d",
225 (unsigned long) events,
227 ShowQ(btn, BUTTON_RELEASED),
228 ShowQ(btn, BUTTON_PRESSED),
229 ShowQ(btn, BUTTON_CLICKED),
230 ShowQ(btn, DOUBLE_CLICKED),
231 ShowQ(btn, TRIPLE_CLICKED),
240 * A position report need not have a button associated with it.
241 * The modifiers probably are unused.
243 if (!found && (event.bstate & REPORT_MOUSE_POSITION)) {
244 logw("[%08lX]%s%s%s%s @ %d, %d",
245 (unsigned long) events,
257 logw("got another char: 0x%x", UChar(c));
263 ExitProgram(EXIT_SUCCESS);
269 printf("This program requires the ncurses library\n");
270 ExitProgram(EXIT_FAILURE);