1 /****************************************************************************
2 * Copyright (c) 2005-2018,2019 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 ****************************************************************************/
29 * $Id: demo_altkeys.c,v 1.13 2019/08/17 21:49:40 tom Exp $
31 * Demonstrate the define_key() function.
32 * Thomas Dickey - 2005/10/22
36 #include <test.priv.h>
38 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
40 #define MY_LOGFILE "demo_altkeys.log"
41 #define MY_KEYS (KEY_MAX + 1)
44 * Log the most recently-written line to our logfile
47 log_last_line(WINDOW *win)
51 if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
54 int need = sizeof(temp) - 1;
60 n = winnstr(win, temp, need);
62 if (isspace(UChar(temp[n])))
68 fprintf(fp, "%s\n", temp);
74 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
79 struct timeval previous;
84 if (newterm(0, stdout, stdin) == 0) {
85 fprintf(stderr, "Cannot initialize terminal\n");
86 ExitProgram(EXIT_FAILURE);
88 (void) cbreak(); /* take input chars one at a time, no wait for \n */
89 (void) noecho(); /* don't echo input */
91 scrollok(stdscr, TRUE);
95 /* we do the define_key() calls after keypad(), since the first call to
96 * keypad() initializes the corresponding data.
98 for (n = 0; n < 255; ++n) {
100 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\033%c", n);
101 define_key(temp, n + MY_KEYS);
103 for (n = KEY_MIN; n < KEY_MAX; ++n) {
105 if ((value = keybound(n, 0)) != 0) {
106 size_t need = strlen(value) + 2;
107 char *temp = typeMalloc(char, need);
108 _nc_SPRINTF(temp, _nc_SLIMIT(need) "\033%s", value);
109 define_key(temp, n + MY_KEYS);
115 #if HAVE_GETTIMEOFDAY
116 gettimeofday(&previous, 0);
119 while ((ch = getch()) != ERR) {
120 bool escaped = (ch >= MY_KEYS);
121 const char *name = keyname(escaped ? (ch - MY_KEYS) : ch);
122 #if HAVE_GETTIMEOFDAY
124 struct timeval current;
126 gettimeofday(¤t, 0);
127 secs = (int) (current.tv_sec - previous.tv_sec);
128 msecs = (int) ((current.tv_usec - previous.tv_usec) / 1000);
134 secs += msecs / 1000;
137 printw("%6d.%03d ", secs, msecs);
140 printw("Keycode %d, name %s%s\n",
142 escaped ? "ESC-" : "",
143 name != 0 ? name : "<null>");
144 log_last_line(stdscr);
150 ExitProgram(EXIT_SUCCESS);
156 printf("This program requires the ncurses library\n");
157 ExitProgram(EXIT_FAILURE);