1 /****************************************************************************
2 * Copyright (c) 2006,2016 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.5 2016/09/04 20:49:06 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.
41 #include <test.priv.h>
43 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
45 #if TIME_WITH_SYS_TIME
46 # include <sys/time.h>
50 # include <sys/time.h>
56 #define MY_LOGFILE "demo_foldkeys.log"
57 #define MY_KEYS (KEY_MAX + 1)
60 * Log the most recently-written line to our logfile
63 log_last_line(WINDOW *win)
69 if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
70 int need = sizeof(temp) - 1;
75 n = winnstr(win, temp, need);
77 if (isspace(UChar(temp[n])))
83 fprintf(fp, "%s\n", temp);
89 * ncurses has no API for telling what the actual last key-code is. That is
90 * a secret because the codes past KEY_MAX are computed at run-time and may
91 * differ depending on the previous calls to newterm(), etc. It is unlikely
92 * that one could have more than a thousand key definitions...
106 KeyInfo info[MAX_KEYS];
113 * Tell ncurses that we want to use function keys. That will make it add
114 * any user-defined keys that appear in the terminfo.
116 keypad(stdscr, TRUE);
119 * List the predefined keys using the strnames[] array.
121 for (code = 0; code < STRCOUNT; ++code) {
122 NCURSES_CONST char *name = strnames[code];
123 NCURSES_CONST char *value = tigetstr(name);
124 if (value != 0 && value != (NCURSES_CONST char *) -1) {
125 info[info_len].name = strnames[code];
126 info[info_len].code = key_defined(value);
127 info[info_len].value = value;
128 info[info_len].state = 0;
129 if (info[info_len].code > 0)
135 * We can get the names for user-defined keys from keyname(). It returns
136 * a name like KEY_foo for the predefined keys, which tigetstr() does not
139 for (code = KEY_MAX; code < MAX_KEYS; ++code) {
140 NCURSES_CONST char *name = keyname(code);
142 info[info_len].name = name;
143 info[info_len].code = code;
144 info[info_len].value = tigetstr(name);
145 info[info_len].state = 0;
149 printw("Initially %d key definitions\n", info_len);
152 * Look for keys that have xterm-style modifiers.
154 for (j = 0; j < info_len; ++j) {
159 if (info[j].state == 0
160 && sscanf(info[j].value,
166 && (need = strlen(info[j].value)) != 0
167 && (value = strdup(info[j].value)) != 0) {
168 _nc_SPRINTF(value, _nc_SLIMIT(need) "\033[%d%c", first, *final);
169 for (k = 0; k < info_len; ++k) {
170 if (info[k].state == 0
171 && !strcmp(info[k].value, value)) {
176 if (info[j].state == 0) {
177 _nc_SPRINTF(value, _nc_SLIMIT(need) "\033O%c", *final);
178 for (k = 0; k < info_len; ++k) {
179 if (info[k].state == 0
180 && !strcmp(info[k].value, value)) {
186 if (info[j].state == 1) {
187 if ((define_key(info[j].value, info[k].code)) != ERR) {
188 printw("map %s to %s\n", info[j].value, info[k].value);
189 keyok(info[j].code, FALSE);
192 printw("? cannot define_key %d:%s\n", j, info[j].value);
195 printw("? cannot merge %d:%s\n", j, info[j].value);
200 printw("Merged to %d key definitions\n", info_len - merged);
204 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
207 #if HAVE_GETTIMEOFDAY
209 struct timeval current, previous;
214 newterm(0, stdout, stdin);
215 (void) cbreak(); /* take input chars one at a time, no wait for \n */
216 (void) noecho(); /* don't echo input */
218 scrollok(stdscr, TRUE);
219 keypad(stdscr, TRUE);
224 #if HAVE_GETTIMEOFDAY
225 gettimeofday(&previous, 0);
228 while ((ch = getch()) != ERR) {
229 bool escaped = (ch >= MY_KEYS);
230 const char *name = keyname(escaped ? (ch - MY_KEYS) : ch);
232 #if HAVE_GETTIMEOFDAY
233 gettimeofday(¤t, 0);
234 secs = (int) (current.tv_sec - previous.tv_sec);
235 msecs = (int) ((current.tv_usec - previous.tv_usec) / 1000);
241 secs += msecs / 1000;
244 printw("%6d.%03d ", secs, msecs);
247 printw("Keycode %d, name %s%s\n",
249 escaped ? "ESC-" : "",
250 name != 0 ? name : "<null>");
251 log_last_line(stdscr);
257 ExitProgram(EXIT_SUCCESS);
263 printf("This program requires the ncurses library\n");
264 ExitProgram(EXIT_FAILURE);