]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_mouse.c
f96d658ceb95aa9743256ee7669cbddf1523b324
[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.8 2022/05/08 00:36:07 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     cfmakeraw(&tty);
47
48     setupterm(NULL, 0, 0);
49     xtermcap = tigetstr("XM");
50     if (xtermcap == 0 || xtermcap == (char *) -1) {
51         fprintf(stderr, "couldn't get XM terminfo");
52         return 1;
53     }
54
55     putp(tparm(xtermcap, 1));
56     fflush(stdout);
57
58     tcsetattr(0, TCSANOW, &tty);
59
60     while (true) {
61         int c = getc(stdin);
62         const char *pretty;
63
64         if (c == ERR || c == '\003') {
65             break;
66         } else if (c == '\033') {
67             printf("\r\n");
68         } else if ((pretty = unctrl((chtype) c)) != NULL) {
69             printf("%s", pretty);
70         } else if (isprint(c)) {
71             printf("%c", c);
72         } else {
73             printf("{%x}", c);
74         }
75     }
76
77     putp(tparm(xtermcap, 0));
78     fflush(stdout);
79     tcsetattr(0, TCSANOW, &old);
80     return 0;
81 }
82
83 static int logw(int line, const char *fmt, ...) GCC_PRINTFLIKE(2, 3);
84
85 static int
86 logw(int line, const char *fmt, ...)
87 {
88     va_list args;
89     va_start(args, fmt);
90     wmove(stdscr, line++, 0);
91     vw_printw(stdscr, fmt, args);
92     clrtoeol();
93
94     line %= (getmaxy(stdscr) - logoffset);
95     if (line < logoffset) {
96         line = logoffset;
97     }
98
99     wmove(stdscr, line, 0);
100     wprintw(stdscr, ">");
101     clrtoeol();
102     return line;
103 }
104
105 static void
106 usage(void)
107 {
108     static const char *msg[] =
109     {
110         "Usage: test_mouse [options]",
111         "",
112         "Test mouse events.  These examples for $TERM demonstrate xterm"
113         "features:",
114         "    xterm",
115         "    xterm-1002",
116         "    xterm-1003",
117         "",
118         "Options:",
119         " -r       show raw input stream, injecting a new line before every ESC",
120         " -i n     set mouse interval to n; default is 0",
121         " -h       show this message",
122         " -T term  use terminal description other than $TERM"
123     };
124     unsigned n;
125     for (n = 0; n < sizeof(msg) / sizeof(char *); ++n) {
126         fprintf(stderr, "%s\n", msg[n]);
127     }
128 }
129
130 int
131 main(int argc, char *argv[])
132 {
133     bool rawmode = FALSE;
134     int interval = 0;
135     int curline;
136     int c;
137     MEVENT event;
138     char *my_environ;
139     const char *term_format = "TERM=%s";
140
141     while ((c = getopt(argc, argv, "hi:rT:")) != -1) {
142         switch (c) {
143         case 'h':
144             usage();
145             ExitProgram(EXIT_SUCCESS);
146         case 'i':
147             interval = atoi(optarg);
148             break;
149         case 'r':
150             rawmode = TRUE;
151             break;
152         case 'T':
153             my_environ = malloc(strlen(term_format) + strlen(optarg));
154             sprintf(my_environ, term_format, optarg);
155             putenv(my_environ);
156             break;
157         default:
158             usage();
159             ExitProgram(EXIT_FAILURE);
160         }
161     }
162     if (optind < argc) {
163         usage();
164         ExitProgram(EXIT_FAILURE);
165     }
166
167     if (rawmode) {
168         printf("Entering raw mode. Ctrl-c to quit.\n");
169         return raw_loop();
170     }
171
172     initscr();
173     clear();
174     noecho();
175     cbreak();                   /* Line buffering disabled; pass everything */
176     nonl();
177     keypad(stdscr, TRUE);
178
179     /* Get all the mouse events */
180     mousemask(ALL_MOUSE_EVENTS | REPORT_MOUSE_POSITION, NULL);
181     mouseinterval(interval);
182
183     logoffset = logw(logoffset, "Ctrl-c to quit");
184     logoffset = logw(logoffset, "--------------");
185
186     curline = logoffset;
187
188     while (1) {
189         c = getch();
190
191         switch (c) {
192         case KEY_MOUSE:
193             if (getmouse(&event) == OK) {
194                 unsigned btn;
195                 mmask_t events;
196 #if NCURSES_MOUSE_VERSION > 1
197                 const int max_btn = 5;
198 #else
199                 const int max_btn = 4;
200 #endif
201                 for (btn = 1; btn <= max_btn; btn++) {
202                     events = (mmask_t) (event.bstate
203                                         & NCURSES_MOUSE_MASK(btn,
204                                                              NCURSES_BUTTON_RELEASED |
205                                                              NCURSES_BUTTON_PRESSED |
206                                                              NCURSES_BUTTON_CLICKED |
207                                                              NCURSES_DOUBLE_CLICKED |
208                                                              NCURSES_TRIPLE_CLICKED));
209                     if (events == 0)
210                         continue;
211 #define Show(btn,name) ((event.bstate & NCURSES_MOUSE_MASK(btn, name)) != 0) ? #name : ""
212                     curline = logw(curline,
213                                    "button %d %s %s %s %s %s %d[%x] @ %d, %d",
214                                    btn,
215                                    Show(btn, NCURSES_BUTTON_RELEASED),
216                                    Show(btn, NCURSES_BUTTON_PRESSED),
217                                    Show(btn, NCURSES_BUTTON_CLICKED),
218                                    Show(btn, NCURSES_DOUBLE_CLICKED),
219                                    Show(btn, NCURSES_TRIPLE_CLICKED),
220                                    (event.bstate & REPORT_MOUSE_POSITION) != 0,
221                                    events,
222                                    event.y, event.x);
223                 }
224             }
225             break;
226         case '\003':
227             goto end;
228         default:
229             curline = logw(curline, "got another char: 0x%x", c);
230         }
231         refresh();
232     }
233   end:
234     endwin();
235     ExitProgram(EXIT_SUCCESS);
236 }
237 #else
238 int
239 main(void)
240 {
241     printf("This program requires the ncurses library\n");
242     ExitProgram(EXIT_FAILURE);
243 }
244 #endif