1 /****************************************************************************
2 * Copyright (c) 2002-2013,2016 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.25 2016/09/10 21:22:01 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);
81 _nc_STRCPY(temp, "\\\\", sizeof(temp));
82 } else if (ch == '\033') {
83 _nc_STRCPY(temp, "\\E", sizeof(temp));
84 } else if (ch < ' ') {
85 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\\%03o", ch);
86 } else if (ch >= 127) {
87 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\\%03o", ch);
89 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(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 _nc_STRNCPY(temp, visichar(string[n]), sizeof(temp) - 2);
111 _nc_STRCAT(result, temp, sizeof(temp));
113 need += strlen(temp);
117 result = typeCalloc(char, need);
120 result = typeCalloc(char, (size_t) 1);
126 really_define_key(WINDOW *win, const char *new_string, int code)
129 const char *code_name = keyname(code);
131 char *vis_string = 0;
134 if (code_name == 0) {
135 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "Keycode %d", code);
139 if ((old_string = keybound(code, 0)) != 0) {
140 wprintw(win, "%s is %s\n",
142 vis_string = visible(old_string));
144 wprintw(win, "%s is not bound\n",
149 if (vis_string != 0) {
154 vis_string = visible(new_string);
155 if ((rc = key_defined(new_string)) > 0) {
156 wprintw(win, "%s was bound to %s\n", vis_string, keyname(rc));
158 } else if (new_string != 0 && rc < 0) {
159 wprintw(win, "%s conflicts with longer strings\n", vis_string);
162 rc = define_key(new_string, code);
164 wprintw(win, "%s unchanged\n", code_name);
166 } else if (new_string != 0) {
167 wprintw(win, "%s is now bound to %s\n",
171 } else if (old_string != 0) {
172 wprintw(win, "%s deleted\n", code_name);
182 duplicate(WINDOW *win, NCURSES_CONST char *name, int code)
184 char *value = tigetstr(name);
187 const char *prefix = 0;
190 if (!strncmp(value, "\033[", (size_t) 2)) {
192 } else if (!strncmp(value, "\033O", (size_t) 2)) {
196 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
197 "%s%s", prefix, value + 2);
198 really_define_key(win, temp, code);
204 redefine(WINDOW *win, char *string, int code)
206 really_define_key(win, string, code);
210 remove_definition(WINDOW *win, int code)
212 really_define_key(win, 0, code);
216 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
226 (void) cbreak(); /* take input chars one at a time, no wait for \n */
227 (void) noecho(); /* don't echo input */
229 printw("This demo is best on xterm: it reverses the definitions for f1-f12,\n");
230 printw("adds duplicate definitions for cursor application and normal modes,\n");
231 printw("and removes any definitions for the mini keypad. Type any of those:\n");
234 win = newwin(LINES - 3, COLS, 3, 0);
239 /* we do the define_key() calls after keypad(), since the first call to
240 * keypad() initializes the corresponding data.
242 for (n = 0; n < 12; ++n) {
244 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n + 1);
245 fkeys[n] = tigetstr(name);
247 for (n = 0; n < 12; ++n) {
248 redefine(win, fkeys[11 - n], KEY_F(n + 1));
251 duplicate(win, "kcub1", KEY_LEFT);
252 duplicate(win, "kcuu1", KEY_UP);
253 duplicate(win, "kcud1", KEY_DOWN);
254 duplicate(win, "kcuf1", KEY_RIGHT);
256 remove_definition(win, KEY_A1);
257 remove_definition(win, KEY_A3);
258 remove_definition(win, KEY_B2);
259 remove_definition(win, KEY_C1);
260 remove_definition(win, KEY_C3);
262 really_define_key(win, "\033O", 1023);
264 while ((ch = wgetch(win)) != ERR) {
265 const char *name = keyname(ch);
266 wprintw(win, "Keycode %d, name %s\n",
268 name != 0 ? name : "<null>");
275 ExitProgram(EXIT_FAILURE);
281 printf("This program requires the ncurses library\n");
282 ExitProgram(EXIT_FAILURE);