1 /****************************************************************************
2 * Copyright (c) 2006 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.4 2010/11/14 01:00:53 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) {
158 if (info[j].state == 0
159 && sscanf(info[j].value,
165 && (value = strdup(info[j].value)) != 0) {
166 sprintf(value, "\033[%d%c", first, *final);
167 for (k = 0; k < info_len; ++k) {
168 if (info[k].state == 0
169 && !strcmp(info[k].value, value)) {
174 if (info[j].state == 0) {
175 sprintf(value, "\033O%c", *final);
176 for (k = 0; k < info_len; ++k) {
177 if (info[k].state == 0
178 && !strcmp(info[k].value, value)) {
184 if (info[j].state == 1) {
185 if ((define_key(info[j].value, info[k].code)) != ERR) {
186 printw("map %s to %s\n", info[j].value, info[k].value);
187 keyok(info[j].code, FALSE);
190 printw("? cannot define_key %d:%s\n", j, info[j].value);
193 printw("? cannot merge %d:%s\n", j, info[j].value);
198 printw("Merged to %d key definitions\n", info_len - merged);
202 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
205 #if HAVE_GETTIMEOFDAY
207 struct timeval current, previous;
212 newterm(0, stdout, stdin);
213 (void) cbreak(); /* take input chars one at a time, no wait for \n */
214 (void) noecho(); /* don't echo input */
216 scrollok(stdscr, TRUE);
217 keypad(stdscr, TRUE);
222 #if HAVE_GETTIMEOFDAY
223 gettimeofday(&previous, 0);
226 while ((ch = getch()) != ERR) {
227 bool escaped = (ch >= MY_KEYS);
228 const char *name = keyname(escaped ? (ch - MY_KEYS) : ch);
230 #if HAVE_GETTIMEOFDAY
231 gettimeofday(¤t, 0);
232 secs = (int) (current.tv_sec - previous.tv_sec);
233 msecs = (int) ((current.tv_usec - previous.tv_usec) / 1000);
239 secs += msecs / 1000;
242 printw("%6d.%03d ", secs, msecs);
245 printw("Keycode %d, name %s%s\n",
247 escaped ? "ESC-" : "",
248 name != 0 ? name : "<null>");
249 log_last_line(stdscr);
255 ExitProgram(EXIT_SUCCESS);
261 printf("This program requires the ncurses library\n");
262 ExitProgram(EXIT_FAILURE);