missing ncurses sources
[tomato.git] / release / src / router / libncurses / test / demo_defkey.c
blobac6dc2df79e8fc3c467b5232f88d87454b64f1da
1 /****************************************************************************
2 * Copyright (c) 2002-2007,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_defkey.c,v 1.20 2010/11/14 00:59:35 tom Exp $
31 * Demonstrate the define_key() function.
32 * Thomas Dickey - 2002/11/23
35 #include <test.priv.h>
37 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
39 #define MY_LOGFILE "demo_defkey.log"
42 * Log the most recently-written line to our logfile
44 static void
45 log_last_line(WINDOW *win)
47 FILE *fp;
48 int y, x, n;
49 char temp[256];
51 if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
52 int need = sizeof(temp) - 1;
53 if (need > COLS)
54 need = COLS;
55 getyx(win, y, x);
56 wmove(win, y - 1, 0);
57 n = winnstr(win, temp, need);
58 while (n-- > 0) {
59 if (isspace(UChar(temp[n])))
60 temp[n] = '\0';
61 else
62 break;
64 wmove(win, y, x);
65 fprintf(fp, "%s\n", temp);
66 fclose(fp);
71 * Convert a character to visible form.
73 static char *
74 visichar(int ch)
76 static char temp[10];
78 ch = UChar(ch);
79 assert(ch >= 0 && ch < 256);
80 if (ch == '\\') {
81 strcpy(temp, "\\\\");
82 } else if (ch == '\033') {
83 strcpy(temp, "\\E");
84 } else if (ch < ' ') {
85 sprintf(temp, "\\%03o", ch);
86 } else if (ch >= 127) {
87 sprintf(temp, "\\%03o", ch);
88 } else {
89 sprintf(temp, "%c", ch);
91 return temp;
95 * Convert a string to visible form.
97 static char *
98 visible(const char *string)
100 char *result = 0;
101 size_t need = 1;
102 int pass;
103 int n;
105 if (string != 0 && *string != '\0') {
106 for (pass = 0; pass < 2; ++pass) {
107 for (n = 0; string[n] != '\0'; ++n) {
108 char temp[80];
109 strcpy(temp, visichar(string[n]));
110 if (pass)
111 strcat(result, temp);
112 else
113 need += strlen(temp);
115 if (!pass)
116 result = typeCalloc(char, need);
118 } else {
119 result = typeCalloc(char, 1);
121 return result;
124 static void
125 really_define_key(WINDOW *win, const char *new_string, int code)
127 int rc;
128 const char *code_name = keyname(code);
129 char *old_string;
130 char *vis_string = 0;
131 char temp[80];
133 if (code_name == 0) {
134 sprintf(temp, "Keycode %d", code);
135 code_name = temp;
138 if ((old_string = keybound(code, 0)) != 0) {
139 wprintw(win, "%s is %s\n",
140 code_name,
141 vis_string = visible(old_string));
142 } else {
143 wprintw(win, "%s is not bound\n",
144 code_name);
146 log_last_line(win);
148 if (vis_string != 0) {
149 free(vis_string);
150 vis_string = 0;
153 vis_string = visible(new_string);
154 if ((rc = key_defined(new_string)) > 0) {
155 wprintw(win, "%s was bound to %s\n", vis_string, keyname(rc));
156 log_last_line(win);
157 } else if (new_string != 0 && rc < 0) {
158 wprintw(win, "%s conflicts with longer strings\n", vis_string);
159 log_last_line(win);
161 rc = define_key(new_string, code);
162 if (rc == ERR) {
163 wprintw(win, "%s unchanged\n", code_name);
164 log_last_line(win);
165 } else if (new_string != 0) {
166 wprintw(win, "%s is now bound to %s\n",
167 vis_string,
168 code_name);
169 log_last_line(win);
170 } else if (old_string != 0) {
171 wprintw(win, "%s deleted\n", code_name);
172 log_last_line(win);
174 if (vis_string != 0)
175 free(vis_string);
176 if (old_string != 0)
177 free(old_string);
180 static void
181 duplicate(WINDOW *win, NCURSES_CONST char *name, int code)
183 char *value = tigetstr(name);
185 if (value != 0) {
186 const char *prefix = 0;
187 char temp[BUFSIZ];
189 if (!strncmp(value, "\033[", 2)) {
190 prefix = "\033O";
191 } else if (!strncmp(value, "\033O", 2)) {
192 prefix = "\033[";
194 if (prefix != 0) {
195 sprintf(temp, "%s%s", prefix, value + 2);
196 really_define_key(win, temp, code);
201 static void
202 redefine(WINDOW *win, char *string, int code)
204 really_define_key(win, string, code);
207 static void
208 remove_definition(WINDOW *win, int code)
210 really_define_key(win, 0, code);
214 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
216 char *fkeys[12];
217 int n;
218 int ch;
219 WINDOW *win;
221 unlink(MY_LOGFILE);
223 initscr();
224 (void) cbreak(); /* take input chars one at a time, no wait for \n */
225 (void) noecho(); /* don't echo input */
227 printw("This demo is best on xterm: it reverses the definitions for f1-f12,\n");
228 printw("adds duplicate definitions for cursor application and normal modes,\n");
229 printw("and removes any definitions for the mini keypad. Type any of those:\n");
230 refresh();
232 win = newwin(LINES - 3, COLS, 3, 0);
233 scrollok(win, TRUE);
234 keypad(win, TRUE);
235 wmove(win, 0, 0);
237 /* we do the define_key() calls after keypad(), since the first call to
238 * keypad() initializes the corresponding data.
240 for (n = 0; n < 12; ++n) {
241 char name[10];
242 sprintf(name, "kf%d", n + 1);
243 fkeys[n] = tigetstr(name);
245 for (n = 0; n < 12; ++n) {
246 redefine(win, fkeys[11 - n], KEY_F(n + 1));
249 duplicate(win, "kcub1", KEY_LEFT);
250 duplicate(win, "kcuu1", KEY_UP);
251 duplicate(win, "kcud1", KEY_DOWN);
252 duplicate(win, "kcuf1", KEY_RIGHT);
254 remove_definition(win, KEY_A1);
255 remove_definition(win, KEY_A3);
256 remove_definition(win, KEY_B2);
257 remove_definition(win, KEY_C1);
258 remove_definition(win, KEY_C3);
260 really_define_key(win, "\033O", 1023);
262 while ((ch = wgetch(win)) != ERR) {
263 const char *name = keyname(ch);
264 wprintw(win, "Keycode %d, name %s\n",
266 name != 0 ? name : "<null>");
267 log_last_line(win);
268 wclrtoeol(win);
269 if (ch == 'q')
270 break;
272 endwin();
273 ExitProgram(EXIT_FAILURE);
275 #else
277 main(void)
279 printf("This program requires the ncurses library\n");
280 ExitProgram(EXIT_FAILURE);
282 #endif