missing ncurses sources
[tomato.git] / release / src / router / libncurses / test / demo_altkeys.c
blobf2b07b1c8c90855c042fc6b8a7b9be2424e1d1cc
1 /****************************************************************************
2 * Copyright (c) 2005-2006,2008 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 ****************************************************************************/
29 * $Id: demo_altkeys.c,v 1.9 2010/11/14 00:59:26 tom Exp $
31 * Demonstrate the define_key() function.
32 * Thomas Dickey - 2005/10/22
35 #include <test.priv.h>
37 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
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
50 #define MY_LOGFILE "demo_altkeys.log"
51 #define MY_KEYS (KEY_MAX + 1)
54 * Log the most recently-written line to our logfile
56 static void
57 log_last_line(WINDOW *win)
59 FILE *fp;
60 int y, x, n;
61 char temp[256];
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;
76 wmove(win, y, x);
77 fprintf(fp, "%s\n", temp);
78 fclose(fp);
82 int
83 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
85 int n;
86 int ch;
87 #if HAVE_GETTIMEOFDAY
88 int secs, msecs;
89 struct timeval current, previous;
90 #endif
92 unlink(MY_LOGFILE);
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 */
98 scrollok(stdscr, TRUE);
99 keypad(stdscr, TRUE);
100 move(0, 0);
102 /* we do the define_key() calls after keypad(), since the first call to
103 * keypad() initializes the corresponding data.
105 for (n = 0; n < 255; ++n) {
106 char temp[10];
107 sprintf(temp, "\033%c", n);
108 define_key(temp, n + MY_KEYS);
110 for (n = KEY_MIN; n < KEY_MAX; ++n) {
111 char *value;
112 if ((value = keybound(n, 0)) != 0) {
113 char *temp = typeMalloc(char, strlen(value) + 2);
114 sprintf(temp, "\033%s", value);
115 define_key(temp, n + MY_KEYS);
116 free(temp);
117 free(value);
121 #if HAVE_GETTIMEOFDAY
122 gettimeofday(&previous, 0);
123 #endif
125 while ((ch = getch()) != ERR) {
126 bool escaped = (ch >= MY_KEYS);
127 const char *name = keyname(escaped ? (ch - MY_KEYS) : ch);
129 #if HAVE_GETTIMEOFDAY
130 gettimeofday(&current, 0);
131 secs = (int) (current.tv_sec - previous.tv_sec);
132 msecs = (int) ((current.tv_usec - previous.tv_usec) / 1000);
133 if (msecs < 0) {
134 msecs += 1000;
135 --secs;
137 if (msecs >= 1000) {
138 secs += msecs / 1000;
139 msecs %= 1000;
141 printw("%6d.%03d ", secs, msecs);
142 previous = current;
143 #endif
144 printw("Keycode %d, name %s%s\n",
146 escaped ? "ESC-" : "",
147 name != 0 ? name : "<null>");
148 log_last_line(stdscr);
149 clrtoeol();
150 if (ch == 'q')
151 break;
153 endwin();
154 ExitProgram(EXIT_SUCCESS);
156 #else
158 main(void)
160 printf("This program requires the ncurses library\n");
161 ExitProgram(EXIT_FAILURE);
163 #endif