1 /****************************************************************************
2 * Copyright 2018-2021,2022 Thomas E. Dickey *
3 * Copyright 2006-2016,2017 Free Software Foundation, Inc. *
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. *
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 *
28 ****************************************************************************/
31 * Author: Thomas E. Dickey, 2006
33 * $Id: foldkeys.c,v 1.11 2022/12/10 23:31:31 tom Exp $
35 * Demonstrate a method for altering key definitions at runtime.
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.
43 #include <test.priv.h>
45 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
47 #define MY_LOGFILE "demo_foldkeys.log"
48 #define MY_KEYS (KEY_MAX + 1)
51 * Log the most recently-written line to our logfile
54 log_last_line(WINDOW *win)
58 if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
61 int need = sizeof(temp) - 1;
66 n = winnstr(win, temp, need);
68 if (isspace(UChar(temp[n])))
74 fprintf(fp, "%s\n", temp);
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...
97 KeyInfo info[MAX_KEYS];
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.
107 keypad(stdscr, TRUE);
110 * List the predefined keys using the strnames[] array.
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)
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
130 for (code = KEY_MAX; code < MAX_KEYS; ++code) {
131 NCURSES_CONST char *name = keyname(code);
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;
140 printw("Initially %d key definitions\n", info_len);
143 * Look for keys that have xterm-style modifiers.
145 for (j = 0; j < info_len; ++j) {
151 if (info[j].state == 0
152 && sscanf(info[j].value,
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)) {
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)) {
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);
185 printw("? cannot define_key %d:%s\n", j, info[j].value);
188 printw("? cannot merge %d:%s\n", j, info[j].value);
193 printw("Merged to %d key definitions\n", info_len - merged);
199 static const char *msg[] =
201 "Usage: foldkeys [options]"
207 for (n = 0; n < SIZEOF(msg); n++)
208 fprintf(stderr, "%s\n", msg[n]);
210 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
217 main(int argc, char *argv[])
220 #if HAVE_GETTIMEOFDAY
221 struct timeval previous;
224 while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
228 ExitProgram(EXIT_SUCCESS);
230 usage(ch == OPTS_USAGE);
237 if (newterm(0, stdout, stdin) == 0) {
238 fprintf(stderr, "Cannot initialize terminal\n");
239 ExitProgram(EXIT_FAILURE);
244 (void) cbreak(); /* take input chars one at a time, no wait for \n */
245 (void) noecho(); /* don't echo input */
247 scrollok(stdscr, TRUE);
248 keypad(stdscr, TRUE);
253 #if HAVE_GETTIMEOFDAY
254 gettimeofday(&previous, 0);
257 while ((ch = getch()) != ERR) {
258 bool escaped = (ch >= MY_KEYS);
259 const char *name = keyname(escaped ? (ch - MY_KEYS) : ch);
261 #if HAVE_GETTIMEOFDAY
263 struct timeval current;
265 gettimeofday(¤t, 0);
266 secs = (int) (current.tv_sec - previous.tv_sec);
267 msecs = (int) ((current.tv_usec - previous.tv_usec) / 1000);
273 secs += msecs / 1000;
276 printw("%6d.%03d ", secs, msecs);
279 printw("Keycode %d, name %s%s\n",
281 escaped ? "ESC-" : "",
282 name != 0 ? name : "<null>");
283 log_last_line(stdscr);
289 ExitProgram(EXIT_SUCCESS);
295 printf("This program requires the ncurses library\n");
296 ExitProgram(EXIT_FAILURE);