]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/foldkeys.c
ncurses 6.4 - patch 20240414
[ncurses.git] / test / foldkeys.c
1 /****************************************************************************
2  * Copyright 2018-2020,2021 Thomas E. Dickey                                *
3  * Copyright 2006-2016,2017 Free Software Foundation, Inc.                  *
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  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /*
31  * Author: Thomas E. Dickey, 2006
32  *
33  * $Id: foldkeys.c,v 1.10 2021/08/08 00:41:57 tom Exp $
34  *
35  * Demonstrate a method for altering key definitions at runtime.
36  *
37  * This program reads the key definitions, merging those which have xterm-style
38  * modifiers into their equivalents which have no modifiers.  It does this
39  * merging only for the keys which are defined in the terminal description.
40  */
41
42 #define NEED_TIME_H
43 #include <test.priv.h>
44
45 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
46
47 #define MY_LOGFILE "demo_foldkeys.log"
48 #define MY_KEYS (KEY_MAX + 1)
49
50 /*
51  * Log the most recently-written line to our logfile
52  */
53 static void
54 log_last_line(WINDOW *win)
55 {
56     FILE *fp;
57
58     if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
59         char temp[256];
60         int y, x, n;
61         int need = sizeof(temp) - 1;
62         if (need > COLS)
63             need = COLS;
64         getyx(win, y, x);
65         wmove(win, y - 1, 0);
66         n = winnstr(win, temp, need);
67         while (n-- > 0) {
68             if (isspace(UChar(temp[n])))
69                 temp[n] = '\0';
70             else
71                 break;
72         }
73         wmove(win, y, x);
74         fprintf(fp, "%s\n", temp);
75         fclose(fp);
76     }
77 }
78
79 /*
80  * ncurses has no API for telling what the actual last key-code is.  That is
81  * a secret because the codes past KEY_MAX are computed at run-time and may
82  * differ depending on the previous calls to newterm(), etc.  It is unlikely
83  * that one could have more than a thousand key definitions...
84  */
85 #define MAX_KEYS 2000
86
87 typedef struct {
88     const char *name;
89     const char *value;
90     int code;
91     int state;
92 } KeyInfo;
93
94 static void
95 demo_foldkeys(void)
96 {
97     KeyInfo info[MAX_KEYS];
98     int info_len = 0;
99     int merged = 0;
100     int code;
101     int j, k;
102
103     /*
104      * Tell ncurses that we want to use function keys.  That will make it add
105      * any user-defined keys that appear in the terminfo.
106      */
107     keypad(stdscr, TRUE);
108
109     /*
110      * List the predefined keys using the strnames[] array.
111      */
112     for (code = 0; code < STRCOUNT; ++code) {
113         NCURSES_CONST char *name = strnames[code];
114         NCURSES_CONST char *value = tigetstr(name);
115         if (value != 0 && value != (NCURSES_CONST char *) -1) {
116             info[info_len].name = strnames[code];
117             info[info_len].code = key_defined(value);
118             info[info_len].value = value;
119             info[info_len].state = 0;
120             if (info[info_len].code > 0)
121                 ++info_len;
122         }
123     }
124
125     /*
126      * We can get the names for user-defined keys from keyname().  It returns
127      * a name like KEY_foo for the predefined keys, which tigetstr() does not
128      * understand.
129      */
130     for (code = KEY_MAX; code < MAX_KEYS; ++code) {
131         NCURSES_CONST char *name = keyname(code);
132         if (name != 0) {
133             info[info_len].name = name;
134             info[info_len].code = code;
135             info[info_len].value = tigetstr(name);
136             info[info_len].state = 0;
137             ++info_len;
138         }
139     }
140     printw("Initially %d key definitions\n", info_len);
141
142     /*
143      * Look for keys that have xterm-style modifiers.
144      */
145     for (j = 0; j < info_len; ++j) {
146         int first, second;
147         char final[2];
148         char *value;
149         size_t need;
150
151         if (info[j].state == 0
152             && sscanf(info[j].value,
153                       "\033[%d;%d%c",
154                       &first,
155                       &second,
156                       final) == 3
157             && *final != ';'
158             && (need = strlen(info[j].value)) != 0
159             && (value = strdup(info[j].value)) != 0) {
160             (void) need;        /* _nc_SLIMIT is normally nothing  */
161             _nc_SPRINTF(value, _nc_SLIMIT(need) "\033[%d%c", first, *final);
162             for (k = 0; k < info_len; ++k) {
163                 if (info[k].state == 0
164                     && !strcmp(info[k].value, value)) {
165                     info[j].state = 1;
166                     break;
167                 }
168             }
169             if (info[j].state == 0) {
170                 _nc_SPRINTF(value, _nc_SLIMIT(need) "\033O%c", *final);
171                 for (k = 0; k < info_len; ++k) {
172                     if (info[k].state == 0
173                         && !strcmp(info[k].value, value)) {
174                         info[j].state = 1;
175                         break;
176                     }
177                 }
178             }
179             if (info[j].state == 1) {
180                 if ((define_key(info[j].value, info[k].code)) != ERR) {
181                     printw("map %s to %s\n", info[j].value, info[k].value);
182                     keyok(info[j].code, FALSE);
183                     ++merged;
184                 } else {
185                     printw("? cannot define_key %d:%s\n", j, info[j].value);
186                 }
187             } else {
188                 printw("? cannot merge %d:%s\n", j, info[j].value);
189             }
190             free(value);
191         }
192     }
193     printw("Merged to %d key definitions\n", info_len - merged);
194 }
195
196 int
197 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
198 {
199     int ch;
200 #if HAVE_GETTIMEOFDAY
201     struct timeval previous;
202 #endif
203
204     if (newterm(0, stdout, stdin) == 0) {
205         fprintf(stderr, "Cannot initialize terminal\n");
206         ExitProgram(EXIT_FAILURE);
207     }
208
209     unlink(MY_LOGFILE);
210
211     (void) cbreak();            /* take input chars one at a time, no wait for \n */
212     (void) noecho();            /* don't echo input */
213
214     scrollok(stdscr, TRUE);
215     keypad(stdscr, TRUE);
216     move(0, 0);
217
218     demo_foldkeys();
219
220 #if HAVE_GETTIMEOFDAY
221     gettimeofday(&previous, 0);
222 #endif
223
224     while ((ch = getch()) != ERR) {
225         bool escaped = (ch >= MY_KEYS);
226         const char *name = keyname(escaped ? (ch - MY_KEYS) : ch);
227
228 #if HAVE_GETTIMEOFDAY
229         int secs, msecs;
230         struct timeval current;
231
232         gettimeofday(&current, 0);
233         secs = (int) (current.tv_sec - previous.tv_sec);
234         msecs = (int) ((current.tv_usec - previous.tv_usec) / 1000);
235         if (msecs < 0) {
236             msecs += 1000;
237             --secs;
238         }
239         if (msecs >= 1000) {
240             secs += msecs / 1000;
241             msecs %= 1000;
242         }
243         printw("%6d.%03d ", secs, msecs);
244         previous = current;
245 #endif
246         printw("Keycode %d, name %s%s\n",
247                ch,
248                escaped ? "ESC-" : "",
249                name != 0 ? name : "<null>");
250         log_last_line(stdscr);
251         clrtoeol();
252         if (ch == 'q')
253             break;
254     }
255     endwin();
256     ExitProgram(EXIT_SUCCESS);
257 }
258 #else
259 int
260 main(void)
261 {
262     printf("This program requires the ncurses library\n");
263     ExitProgram(EXIT_FAILURE);
264 }
265 #endif