1 /****************************************************************************
2 * Copyright 2018-2019,2020 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.14 2020/02/02 23:34:34 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 if (newterm(0, stdout, stdin) == 0) {
86 fprintf(stderr, "Cannot initialize terminal\n");
87 ExitProgram(EXIT_FAILURE);
89 (void) cbreak(); /* take input chars one at a time, no wait for \n */
90 (void) noecho(); /* don't echo input */
92 scrollok(stdscr, TRUE);
96 /* we do the define_key() calls after keypad(), since the first call to
97 * keypad() initializes the corresponding data.
99 for (n = 0; n < 255; ++n) {
101 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\033%c", n);
102 define_key(temp, n + MY_KEYS);
104 for (n = KEY_MIN; n < KEY_MAX; ++n) {
106 if ((value = keybound(n, 0)) != 0) {
107 size_t need = strlen(value) + 2;
108 char *temp = typeMalloc(char, need);
109 _nc_SPRINTF(temp, _nc_SLIMIT(need) "\033%s", value);
110 define_key(temp, n + MY_KEYS);
116 #if HAVE_GETTIMEOFDAY
117 gettimeofday(&previous, 0);
120 while ((ch = getch()) != ERR) {
121 bool escaped = (ch >= MY_KEYS);
122 const char *name = keyname(escaped ? (ch - MY_KEYS) : ch);
123 #if HAVE_GETTIMEOFDAY
125 struct timeval current;
127 gettimeofday(¤t, 0);
128 secs = (int) (current.tv_sec - previous.tv_sec);
129 msecs = (int) ((current.tv_usec - previous.tv_usec) / 1000);
135 secs += msecs / 1000;
138 printw("%6d.%03d ", secs, msecs);
141 printw("Keycode %d, name %s%s\n",
143 escaped ? "ESC-" : "",
144 name != 0 ? name : "<null>");
145 log_last_line(stdscr);
151 ExitProgram(EXIT_SUCCESS);
157 printf("This program requires the ncurses library\n");
158 ExitProgram(EXIT_FAILURE);