1 /****************************************************************************
2 * Copyright 2018-2020,2022 Thomas E. Dickey *
3 * Copyright 2005-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * $Id: demo_altkeys.c,v 1.15 2022/04/16 18:26:57 tom Exp $
32 * Demonstrate the define_key() function.
33 * Thomas Dickey - 2005/10/22
37 #include <test.priv.h>
39 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
41 #define MY_LOGFILE "demo_altkeys.log"
42 #define MY_KEYS (KEY_MAX + 1)
45 * Log the most recently-written line to our logfile
48 log_last_line(WINDOW *win)
52 if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
55 int need = sizeof(temp) - 1;
61 n = winnstr(win, temp, need);
63 if (isspace(UChar(temp[n])))
69 fprintf(fp, "%s\n", temp);
75 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
80 struct timeval previous;
85 setlocale(LC_ALL, "");
86 if (newterm(0, stdout, stdin) == 0) {
87 fprintf(stderr, "Cannot initialize terminal\n");
88 ExitProgram(EXIT_FAILURE);
90 (void) cbreak(); /* take input chars one at a time, no wait for \n */
91 (void) noecho(); /* don't echo input */
93 scrollok(stdscr, TRUE);
97 /* we do the define_key() calls after keypad(), since the first call to
98 * keypad() initializes the corresponding data.
100 for (n = 0; n < 255; ++n) {
102 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\033%c", n);
103 define_key(temp, n + MY_KEYS);
105 for (n = KEY_MIN; n < KEY_MAX; ++n) {
107 if ((value = keybound(n, 0)) != 0) {
108 size_t need = strlen(value) + 2;
109 char *temp = typeMalloc(char, need);
110 _nc_SPRINTF(temp, _nc_SLIMIT(need) "\033%s", value);
111 define_key(temp, n + MY_KEYS);
117 #if HAVE_GETTIMEOFDAY
118 gettimeofday(&previous, 0);
121 while ((ch = getch()) != ERR) {
122 bool escaped = (ch >= MY_KEYS);
123 const char *name = keyname(escaped ? (ch - MY_KEYS) : ch);
124 #if HAVE_GETTIMEOFDAY
126 struct timeval current;
128 gettimeofday(¤t, 0);
129 secs = (int) (current.tv_sec - previous.tv_sec);
130 msecs = (int) ((current.tv_usec - previous.tv_usec) / 1000);
136 secs += msecs / 1000;
139 printw("%6d.%03d ", secs, msecs);
142 printw("Keycode %d, name %s%s\n",
144 escaped ? "ESC-" : "",
145 name != 0 ? name : "<null>");
146 log_last_line(stdscr);
152 ExitProgram(EXIT_SUCCESS);
158 printf("This program requires the ncurses library\n");
159 ExitProgram(EXIT_FAILURE);