]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/foldkeys.c
98e72b0450a51ad8a03c4539b9a86160304f038a
[ncurses.git] / test / foldkeys.c
1 /****************************************************************************
2  * Copyright (c) 2006-2018,2019 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.8 2019/08/24 23:11:01 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 #define NEED_TIME_H
42 #include <test.priv.h>
43
44 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
45
46 #define MY_LOGFILE "demo_foldkeys.log"
47 #define MY_KEYS (KEY_MAX + 1)
48
49 /*
50  * Log the most recently-written line to our logfile
51  */
52 static void
53 log_last_line(WINDOW *win)
54 {
55     FILE *fp;
56
57     if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
58         char temp[256];
59         int y, x, n;
60         int need = sizeof(temp) - 1;
61         if (need > COLS)
62             need = COLS;
63         getyx(win, y, x);
64         wmove(win, y - 1, 0);
65         n = winnstr(win, temp, need);
66         while (n-- > 0) {
67             if (isspace(UChar(temp[n])))
68                 temp[n] = '\0';
69             else
70                 break;
71         }
72         wmove(win, y, x);
73         fprintf(fp, "%s\n", temp);
74         fclose(fp);
75     }
76 }
77
78 /*
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...
83  */
84 #define MAX_KEYS 2000
85
86 typedef struct {
87     const char *name;
88     const char *value;
89     int code;
90     int state;
91 } KeyInfo;
92
93 static void
94 demo_foldkeys(void)
95 {
96     KeyInfo info[MAX_KEYS];
97     int info_len = 0;
98     int merged = 0;
99     int code;
100     int j, k;
101
102     /*
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.
105      */
106     keypad(stdscr, TRUE);
107
108     /*
109      * List the predefined keys using the strnames[] array.
110      */
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)
120                 ++info_len;
121         }
122     }
123
124     /*
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
127      * understand.
128      */
129     for (code = KEY_MAX; code < MAX_KEYS; ++code) {
130         NCURSES_CONST char *name = keyname(code);
131         if (name != 0) {
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;
136             ++info_len;
137         }
138     }
139     printw("Initially %d key definitions\n", info_len);
140
141     /*
142      * Look for keys that have xterm-style modifiers.
143      */
144     for (j = 0; j < info_len; ++j) {
145         int first, second;
146         char final[2];
147         char *value;
148         size_t need = 0;
149         if (info[j].state == 0
150             && sscanf(info[j].value,
151                       "\033[%d;%d%c",
152                       &first,
153                       &second,
154                       final) == 3
155             && *final != ';'
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)) {
162                     info[j].state = 1;
163                     break;
164                 }
165             }
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)) {
171                         info[j].state = 1;
172                         break;
173                     }
174                 }
175             }
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);
180                     ++merged;
181                 } else {
182                     printw("? cannot define_key %d:%s\n", j, info[j].value);
183                 }
184             } else {
185                 printw("? cannot merge %d:%s\n", j, info[j].value);
186             }
187             free(value);
188         }
189     }
190     printw("Merged to %d key definitions\n", info_len - merged);
191 }
192
193 int
194 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
195 {
196     int ch;
197 #if HAVE_GETTIMEOFDAY
198     struct timeval previous;
199 #endif
200
201     if (newterm(0, stdout, stdin) == 0) {
202         fprintf(stderr, "Cannot initialize terminal\n");
203         ExitProgram(EXIT_FAILURE);
204     }
205
206     unlink(MY_LOGFILE);
207
208     (void) cbreak();            /* take input chars one at a time, no wait for \n */
209     (void) noecho();            /* don't echo input */
210
211     scrollok(stdscr, TRUE);
212     keypad(stdscr, TRUE);
213     move(0, 0);
214
215     demo_foldkeys();
216
217 #if HAVE_GETTIMEOFDAY
218     gettimeofday(&previous, 0);
219 #endif
220
221     while ((ch = getch()) != ERR) {
222         bool escaped = (ch >= MY_KEYS);
223         const char *name = keyname(escaped ? (ch - MY_KEYS) : ch);
224
225 #if HAVE_GETTIMEOFDAY
226         int secs, msecs;
227         struct timeval current;
228
229         gettimeofday(&current, 0);
230         secs = (int) (current.tv_sec - previous.tv_sec);
231         msecs = (int) ((current.tv_usec - previous.tv_usec) / 1000);
232         if (msecs < 0) {
233             msecs += 1000;
234             --secs;
235         }
236         if (msecs >= 1000) {
237             secs += msecs / 1000;
238             msecs %= 1000;
239         }
240         printw("%6d.%03d ", secs, msecs);
241         previous = current;
242 #endif
243         printw("Keycode %d, name %s%s\n",
244                ch,
245                escaped ? "ESC-" : "",
246                name != 0 ? name : "<null>");
247         log_last_line(stdscr);
248         clrtoeol();
249         if (ch == 'q')
250             break;
251     }
252     endwin();
253     ExitProgram(EXIT_SUCCESS);
254 }
255 #else
256 int
257 main(void)
258 {
259     printf("This program requires the ncurses library\n");
260     ExitProgram(EXIT_FAILURE);
261 }
262 #endif