1 /****************************************************************************
2 * Copyright (c) 2002-2007,2008 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.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
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.
79 assert(ch
>= 0 && ch
< 256);
82 } else if (ch
== '\033') {
84 } else if (ch
< ' ') {
85 sprintf(temp
, "\\%03o", ch
);
86 } else if (ch
>= 127) {
87 sprintf(temp
, "\\%03o", ch
);
89 sprintf(temp
, "%c", ch
);
95 * Convert a string to visible form.
98 visible(const char *string
)
105 if (string
!= 0 && *string
!= '\0') {
106 for (pass
= 0; pass
< 2; ++pass
) {
107 for (n
= 0; string
[n
] != '\0'; ++n
) {
109 strcpy(temp
, visichar(string
[n
]));
111 strcat(result
, temp
);
113 need
+= strlen(temp
);
116 result
= typeCalloc(char, need
);
119 result
= typeCalloc(char, 1);
125 really_define_key(WINDOW
*win
, const char *new_string
, int code
)
128 const char *code_name
= keyname(code
);
130 char *vis_string
= 0;
133 if (code_name
== 0) {
134 sprintf(temp
, "Keycode %d", code
);
138 if ((old_string
= keybound(code
, 0)) != 0) {
139 wprintw(win
, "%s is %s\n",
141 vis_string
= visible(old_string
));
143 wprintw(win
, "%s is not bound\n",
148 if (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
));
157 } else if (new_string
!= 0 && rc
< 0) {
158 wprintw(win
, "%s conflicts with longer strings\n", vis_string
);
161 rc
= define_key(new_string
, code
);
163 wprintw(win
, "%s unchanged\n", code_name
);
165 } else if (new_string
!= 0) {
166 wprintw(win
, "%s is now bound to %s\n",
170 } else if (old_string
!= 0) {
171 wprintw(win
, "%s deleted\n", code_name
);
181 duplicate(WINDOW
*win
, NCURSES_CONST
char *name
, int code
)
183 char *value
= tigetstr(name
);
186 const char *prefix
= 0;
189 if (!strncmp(value
, "\033[", 2)) {
191 } else if (!strncmp(value
, "\033O", 2)) {
195 sprintf(temp
, "%s%s", prefix
, value
+ 2);
196 really_define_key(win
, temp
, code
);
202 redefine(WINDOW
*win
, char *string
, int code
)
204 really_define_key(win
, string
, code
);
208 remove_definition(WINDOW
*win
, int code
)
210 really_define_key(win
, 0, code
);
214 main(int argc GCC_UNUSED
, char *argv
[]GCC_UNUSED
)
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");
232 win
= newwin(LINES
- 3, COLS
, 3, 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
) {
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>");
273 ExitProgram(EXIT_FAILURE
);
279 printf("This program requires the ncurses library\n");
280 ExitProgram(EXIT_FAILURE
);