1 /****************************************************************************
2 * Copyright (c) 2006-2018,2019 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
30 * Author: Thomas E. Dickey, 2006
32 * $Id: foldkeys.c,v 1.8 2019/08/24 23:11:01 tom Exp $
34 * Demonstrate a method for altering key definitions at runtime.
36 * This program reads the key definitions, merging those which have xterm-style
37 * modifiers into their equivalents which have no modifiers. It does this
38 * merging only for the keys which are defined in the terminal description.
42 #include <test.priv.h>
44 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
46 #define MY_LOGFILE "demo_foldkeys.log"
47 #define MY_KEYS (KEY_MAX + 1)
50 * Log the most recently-written line to our logfile
53 log_last_line(WINDOW *win)
57 if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
60 int need = sizeof(temp) - 1;
65 n = winnstr(win, temp, need);
67 if (isspace(UChar(temp[n])))
73 fprintf(fp, "%s\n", temp);
79 * ncurses has no API for telling what the actual last key-code is. That is
80 * a secret because the codes past KEY_MAX are computed at run-time and may
81 * differ depending on the previous calls to newterm(), etc. It is unlikely
82 * that one could have more than a thousand key definitions...
96 KeyInfo info[MAX_KEYS];
103 * Tell ncurses that we want to use function keys. That will make it add
104 * any user-defined keys that appear in the terminfo.
106 keypad(stdscr, TRUE);
109 * List the predefined keys using the strnames[] array.
111 for (code = 0; code < STRCOUNT; ++code) {
112 NCURSES_CONST char *name = strnames[code];
113 NCURSES_CONST char *value = tigetstr(name);
114 if (value != 0 && value != (NCURSES_CONST char *) -1) {
115 info[info_len].name = strnames[code];
116 info[info_len].code = key_defined(value);
117 info[info_len].value = value;
118 info[info_len].state = 0;
119 if (info[info_len].code > 0)
125 * We can get the names for user-defined keys from keyname(). It returns
126 * a name like KEY_foo for the predefined keys, which tigetstr() does not
129 for (code = KEY_MAX; code < MAX_KEYS; ++code) {
130 NCURSES_CONST char *name = keyname(code);
132 info[info_len].name = name;
133 info[info_len].code = code;
134 info[info_len].value = tigetstr(name);
135 info[info_len].state = 0;
139 printw("Initially %d key definitions\n", info_len);
142 * Look for keys that have xterm-style modifiers.
144 for (j = 0; j < info_len; ++j) {
149 if (info[j].state == 0
150 && sscanf(info[j].value,
156 && (need = strlen(info[j].value)) != 0
157 && (value = strdup(info[j].value)) != 0) {
158 _nc_SPRINTF(value, _nc_SLIMIT(need) "\033[%d%c", first, *final);
159 for (k = 0; k < info_len; ++k) {
160 if (info[k].state == 0
161 && !strcmp(info[k].value, value)) {
166 if (info[j].state == 0) {
167 _nc_SPRINTF(value, _nc_SLIMIT(need) "\033O%c", *final);
168 for (k = 0; k < info_len; ++k) {
169 if (info[k].state == 0
170 && !strcmp(info[k].value, value)) {
176 if (info[j].state == 1) {
177 if ((define_key(info[j].value, info[k].code)) != ERR) {
178 printw("map %s to %s\n", info[j].value, info[k].value);
179 keyok(info[j].code, FALSE);
182 printw("? cannot define_key %d:%s\n", j, info[j].value);
185 printw("? cannot merge %d:%s\n", j, info[j].value);
190 printw("Merged to %d key definitions\n", info_len - merged);
194 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
197 #if HAVE_GETTIMEOFDAY
198 struct timeval previous;
201 if (newterm(0, stdout, stdin) == 0) {
202 fprintf(stderr, "Cannot initialize terminal\n");
203 ExitProgram(EXIT_FAILURE);
208 (void) cbreak(); /* take input chars one at a time, no wait for \n */
209 (void) noecho(); /* don't echo input */
211 scrollok(stdscr, TRUE);
212 keypad(stdscr, TRUE);
217 #if HAVE_GETTIMEOFDAY
218 gettimeofday(&previous, 0);
221 while ((ch = getch()) != ERR) {
222 bool escaped = (ch >= MY_KEYS);
223 const char *name = keyname(escaped ? (ch - MY_KEYS) : ch);
225 #if HAVE_GETTIMEOFDAY
227 struct timeval current;
229 gettimeofday(¤t, 0);
230 secs = (int) (current.tv_sec - previous.tv_sec);
231 msecs = (int) ((current.tv_usec - previous.tv_usec) / 1000);
237 secs += msecs / 1000;
240 printw("%6d.%03d ", secs, msecs);
243 printw("Keycode %d, name %s%s\n",
245 escaped ? "ESC-" : "",
246 name != 0 ? name : "<null>");
247 log_last_line(stdscr);
253 ExitProgram(EXIT_SUCCESS);
259 printf("This program requires the ncurses library\n");
260 ExitProgram(EXIT_FAILURE);