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