1/****************************************************************************
2 * Copyright (c) 2006 Free Software Foundation, Inc.                        *
3 *                                                                          *
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:                 *
11 *                                                                          *
12 * The above copyright notice and this permission notice shall be included  *
13 * in all copies or substantial portions of the Software.                   *
14 *                                                                          *
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.                               *
22 *                                                                          *
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       *
26 * authorization.                                                           *
27 ****************************************************************************/
28
29/*
30 * Author: Thomas E. Dickey, 2006
31 *
32 * $Id: foldkeys.c,v 1.3 2006/12/09 16:54:07 tom Exp $
33 *
34 * Demonstrate a method for altering key definitions at runtime.
35 *
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.
39 */
40
41#include <test.priv.h>
42
43#if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
44
45#if TIME_WITH_SYS_TIME
46# include <sys/time.h>
47# include <time.h>
48#else
49# if HAVE_SYS_TIME_H
50#  include <sys/time.h>
51# else
52#  include <time.h>
53# endif
54#endif
55
56#define MY_LOGFILE "demo_foldkeys.log"
57#define MY_KEYS (KEY_MAX + 1)
58
59/*
60 * Log the most recently-written line to our logfile
61 */
62static void
63log_last_line(WINDOW *win)
64{
65    FILE *fp;
66    int y, x, n;
67    char temp[256];
68
69    if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
70	int need = sizeof(temp) - 1;
71	if (need > COLS)
72	    need = COLS;
73	getyx(win, y, x);
74	wmove(win, y - 1, 0);
75	n = winnstr(win, temp, need);
76	while (n-- > 0) {
77	    if (isspace(UChar(temp[n])))
78		temp[n] = '\0';
79	    else
80		break;
81	}
82	wmove(win, y, x);
83	fprintf(fp, "%s\n", temp);
84	fclose(fp);
85    }
86}
87
88/*
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...
93 */
94#define MAX_KEYS 2000
95
96typedef struct {
97    const char *name;
98    const char *value;
99    int code;
100    int state;
101} KeyInfo;
102
103static void
104demo_foldkeys(void)
105{
106    KeyInfo info[MAX_KEYS];
107    int info_len = 0;
108    int merged = 0;
109    int code;
110    int j, k;
111
112    /*
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.
115     */
116    keypad(stdscr, TRUE);
117
118    /*
119     * List the predefined keys using the strnames[] array.
120     */
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)
130		++info_len;
131	}
132    }
133
134    /*
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
137     * understand.
138     */
139    for (code = KEY_MAX; code < MAX_KEYS; ++code) {
140	NCURSES_CONST char *name = keyname(code);
141	if (name != 0) {
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;
146	    ++info_len;
147	}
148    }
149    printw("Initially %d key definitions\n", info_len);
150
151    /*
152     * Look for keys that have xterm-style modifiers.
153     */
154    for (j = 0; j < info_len; ++j) {
155	int first, second;
156	char final[2];
157	char *value;
158	if (info[j].state == 0
159	    && sscanf(info[j].value,
160		      "\033[%d;%d%c",
161		      &first,
162		      &second,
163		      final) == 3
164	    && *final != ';'
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)) {
170		    info[j].state = 1;
171		    break;
172		}
173	    }
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)) {
179			info[j].state = 1;
180			break;
181		    }
182		}
183	    }
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);
188		    ++merged;
189		} else {
190		    printw("? cannot define_key %d:%s\n", j, info[j].value);
191		}
192	    } else {
193		printw("? cannot merge %d:%s\n", j, info[j].value);
194	    }
195	    free(value);
196	}
197    }
198    printw("Merged to %d key definitions\n", info_len - merged);
199}
200
201int
202main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
203{
204    int ch;
205#if HAVE_GETTIMEOFDAY
206    int secs, msecs;
207    struct timeval current, previous;
208#endif
209
210    unlink(MY_LOGFILE);
211
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 */
215
216    scrollok(stdscr, TRUE);
217    keypad(stdscr, TRUE);
218    move(0, 0);
219
220    demo_foldkeys();
221
222#if HAVE_GETTIMEOFDAY
223    gettimeofday(&previous, 0);
224#endif
225
226    while ((ch = getch()) != ERR) {
227	bool escaped = (ch >= MY_KEYS);
228	const char *name = keyname(escaped ? (ch - MY_KEYS) : ch);
229
230#if HAVE_GETTIMEOFDAY
231	gettimeofday(&current, 0);
232	secs = current.tv_sec - previous.tv_sec;
233	msecs = (current.tv_usec - previous.tv_usec) / 1000;
234	if (msecs < 0) {
235	    msecs += 1000;
236	    --secs;
237	}
238	if (msecs >= 1000) {
239	    secs += msecs / 1000;
240	    msecs %= 1000;
241	}
242	printw("%6d.%03d ", secs, msecs);
243	previous = current;
244#endif
245	printw("Keycode %d, name %s%s\n",
246	       ch,
247	       escaped ? "ESC-" : "",
248	       name != 0 ? name : "<null>");
249	log_last_line(stdscr);
250	clrtoeol();
251	if (ch == 'q')
252	    break;
253    }
254    endwin();
255    ExitProgram(EXIT_SUCCESS);
256}
257#else
258int
259main(void)
260{
261    printf("This program requires the ncurses library\n");
262    ExitProgram(EXIT_FAILURE);
263}
264#endif
265