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