]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_altkeys.c
ncurses 6.2 - patch 20210807
[ncurses.git] / test / demo_altkeys.c
1 /****************************************************************************
2  * Copyright 2018-2019,2020 Thomas E. Dickey                                *
3  * Copyright 2005-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
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:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
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.                               *
23  *                                                                          *
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       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 /*
30  * $Id: demo_altkeys.c,v 1.14 2020/02/02 23:34:34 tom Exp $
31  *
32  * Demonstrate the define_key() function.
33  * Thomas Dickey - 2005/10/22
34  */
35
36 #define NEED_TIME_H
37 #include <test.priv.h>
38
39 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
40
41 #define MY_LOGFILE "demo_altkeys.log"
42 #define MY_KEYS (KEY_MAX + 1)
43
44 /*
45  * Log the most recently-written line to our logfile
46  */
47 static void
48 log_last_line(WINDOW *win)
49 {
50     FILE *fp;
51
52     if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
53         char temp[256];
54         int y, x, n;
55         int need = sizeof(temp) - 1;
56
57         if (need > COLS)
58             need = COLS;
59         getyx(win, y, x);
60         wmove(win, y - 1, 0);
61         n = winnstr(win, temp, need);
62         while (n-- > 0) {
63             if (isspace(UChar(temp[n])))
64                 temp[n] = '\0';
65             else
66                 break;
67         }
68         wmove(win, y, x);
69         fprintf(fp, "%s\n", temp);
70         fclose(fp);
71     }
72 }
73
74 int
75 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
76 {
77     int n;
78     int ch;
79 #if HAVE_GETTIMEOFDAY
80     struct timeval previous;
81 #endif
82
83     unlink(MY_LOGFILE);
84
85     if (newterm(0, stdout, stdin) == 0) {
86         fprintf(stderr, "Cannot initialize terminal\n");
87         ExitProgram(EXIT_FAILURE);
88     }
89     (void) cbreak();            /* take input chars one at a time, no wait for \n */
90     (void) noecho();            /* don't echo input */
91
92     scrollok(stdscr, TRUE);
93     keypad(stdscr, TRUE);
94     move(0, 0);
95
96     /* we do the define_key() calls after keypad(), since the first call to
97      * keypad() initializes the corresponding data.
98      */
99     for (n = 0; n < 255; ++n) {
100         char temp[10];
101         _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\033%c", n);
102         define_key(temp, n + MY_KEYS);
103     }
104     for (n = KEY_MIN; n < KEY_MAX; ++n) {
105         char *value;
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);
111             free(temp);
112             free(value);
113         }
114     }
115
116 #if HAVE_GETTIMEOFDAY
117     gettimeofday(&previous, 0);
118 #endif
119
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
124         int secs, msecs;
125         struct timeval current;
126
127         gettimeofday(&current, 0);
128         secs = (int) (current.tv_sec - previous.tv_sec);
129         msecs = (int) ((current.tv_usec - previous.tv_usec) / 1000);
130         if (msecs < 0) {
131             msecs += 1000;
132             --secs;
133         }
134         if (msecs >= 1000) {
135             secs += msecs / 1000;
136             msecs %= 1000;
137         }
138         printw("%6d.%03d ", secs, msecs);
139         previous = current;
140 #endif
141         printw("Keycode %d, name %s%s\n",
142                ch,
143                escaped ? "ESC-" : "",
144                name != 0 ? name : "<null>");
145         log_last_line(stdscr);
146         clrtoeol();
147         if (ch == 'q')
148             break;
149     }
150     endwin();
151     ExitProgram(EXIT_SUCCESS);
152 }
153 #else
154 int
155 main(void)
156 {
157     printf("This program requires the ncurses library\n");
158     ExitProgram(EXIT_FAILURE);
159 }
160 #endif