1 /****************************************************************************
2 * Copyright (c) 2002-2006,2007 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 * $Id: demo_defkey.c,v 1.17 2007/04/07 17:13:03 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
45 log_last_line(WINDOW *win)
51 if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
52 int need = sizeof(temp) - 1;
57 n = winnstr(win, temp, need);
59 if (isspace(UChar(temp[n])))
65 fprintf(fp, "%s\n", temp);
71 * Convert a character to visible form.
81 } else if (ch == '\033') {
83 } else if (ch < ' ') {
84 sprintf(temp, "\\%03o", ch);
85 } else if (ch >= 127) {
86 sprintf(temp, "\\%03o", ch);
88 sprintf(temp, "%c", ch);
94 * Convert a string to visible form.
97 visible(const char *string)
104 if (string != 0 && *string != '\0') {
105 for (pass = 0; pass < 2; ++pass) {
106 for (n = 0; string[n] != '\0'; ++n) {
108 strcpy(temp, visichar(string[n]));
110 strcat(result, temp);
112 need += strlen(temp);
115 result = (char *) calloc(need, 1);
118 result = (char *) calloc(1, 1);
124 really_define_key(WINDOW *win, const char *new_string, int code)
127 const char *code_name = keyname(code);
129 char *vis_string = 0;
132 if (code_name == 0) {
133 sprintf(temp, "Keycode %d", code);
137 if ((old_string = keybound(code, 0)) != 0) {
138 wprintw(win, "%s is %s\n",
140 vis_string = visible(old_string));
142 wprintw(win, "%s is not bound\n",
147 if (vis_string != 0) {
152 vis_string = visible(new_string);
153 if ((rc = key_defined(new_string)) > 0) {
154 wprintw(win, "%s was bound to %s\n", vis_string, keyname(rc));
156 } else if (new_string != 0 && rc < 0) {
157 wprintw(win, "%s conflicts with longer strings\n", vis_string);
160 rc = define_key(new_string, code);
162 wprintw(win, "%s unchanged\n", code_name);
164 } else if (new_string != 0) {
165 wprintw(win, "%s is now bound to %s\n",
169 } else if (old_string != 0) {
170 wprintw(win, "%s deleted\n", code_name);
180 duplicate(WINDOW *win, NCURSES_CONST char *name, int code)
182 char *value = tigetstr(name);
185 const char *prefix = 0;
188 if (!strncmp(value, "\033[", 2)) {
190 } else if (!strncmp(value, "\033O", 2)) {
194 sprintf(temp, "%s%s", prefix, value + 2);
195 really_define_key(win, temp, code);
201 redefine(WINDOW *win, char *string, int code)
203 really_define_key(win, string, code);
207 remove_definition(WINDOW *win, int code)
209 really_define_key(win, 0, code);
213 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
223 (void) cbreak(); /* take input chars one at a time, no wait for \n */
224 (void) noecho(); /* don't echo input */
226 printw("This demo is best on xterm: it reverses the definitions for f1-f12,\n");
227 printw("adds duplicate definitions for cursor application and normal modes,\n");
228 printw("and removes any definitions for the mini keypad. Type any of those:\n");
231 win = newwin(LINES - 3, COLS, 3, 0);
236 /* we do the define_key() calls after keypad(), since the first call to
237 * keypad() initializes the corresponding data.
239 for (n = 0; n < 12; ++n) {
241 sprintf(name, "kf%d", n + 1);
242 fkeys[n] = tigetstr(name);
244 for (n = 0; n < 12; ++n) {
245 redefine(win, fkeys[11 - n], KEY_F(n + 1));
248 duplicate(win, "kcub1", KEY_LEFT);
249 duplicate(win, "kcuu1", KEY_UP);
250 duplicate(win, "kcud1", KEY_DOWN);
251 duplicate(win, "kcuf1", KEY_RIGHT);
253 remove_definition(win, KEY_A1);
254 remove_definition(win, KEY_A3);
255 remove_definition(win, KEY_B2);
256 remove_definition(win, KEY_C1);
257 remove_definition(win, KEY_C3);
259 really_define_key(win, "\033O", 1023);
261 while ((ch = wgetch(win)) != ERR) {
262 const char *name = keyname(ch);
263 wprintw(win, "Keycode %d, name %s\n",
265 name != 0 ? name : "<null>");
272 ExitProgram(EXIT_FAILURE);
278 printf("This program requires the ncurses library\n");
279 ExitProgram(EXIT_FAILURE);