1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 2002-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * $Id: demo_defkey.c,v 1.31 2020/12/26 17:55:13 tom Exp $
32 * Demonstrate the define_key() function.
33 * Thomas Dickey - 2002/11/23
36 #include <test.priv.h>
38 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
40 #define MY_LOGFILE "demo_defkey.log"
43 * Log the most recently-written line to our logfile
46 log_last_line(WINDOW *win)
50 if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
53 int need = sizeof(temp) - 1;
58 n = winnstr(win, temp, need);
60 if (isspace(UChar(temp[n])))
66 fprintf(fp, "%s\n", temp);
72 * Convert a character to visible form.
80 assert(ch >= 0 && ch < 256);
82 _nc_STRCPY(temp, "\\\\", sizeof(temp));
83 } else if (ch == '\033') {
84 _nc_STRCPY(temp, "\\E", sizeof(temp));
85 } else if (ch < ' ') {
86 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\\%03o", ch);
87 } else if (ch >= 127) {
88 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\\%03o", ch);
90 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "%c", ch);
96 * Convert a string to visible form.
99 visible(const char *string)
103 if (string != 0 && *string != '\0') {
108 for (pass = 0; pass < 2; ++pass) {
109 for (n = 0; string[n] != '\0'; ++n) {
111 _nc_STRNCPY(temp, visichar(string[n]), sizeof(temp) - 2);
113 _nc_STRCAT(result, temp, need);
115 need += strlen(temp);
119 result = typeCalloc(char, need);
122 result = typeCalloc(char, (size_t) 1);
128 really_define_key(WINDOW *win, const char *new_string, int code)
131 const char *code_name = keyname(code);
133 char *vis_string = 0;
136 if (code_name == 0) {
137 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "Keycode %d", code);
141 if ((old_string = keybound(code, 0)) != 0) {
142 wprintw(win, "%s is %s\n",
144 vis_string = visible(old_string));
146 wprintw(win, "%s is not bound\n",
151 if (vis_string != 0) {
156 vis_string = visible(new_string);
157 if ((rc = key_defined(new_string)) > 0) {
158 wprintw(win, "%s was bound to %s\n", vis_string, keyname(rc));
160 } else if (new_string != 0 && rc < 0) {
161 wprintw(win, "%s conflicts with longer strings\n", vis_string);
164 rc = define_key(new_string, code);
166 wprintw(win, "%s unchanged\n", code_name);
168 } else if (new_string != 0) {
169 wprintw(win, "%s is now bound to %s\n",
173 } else if (old_string != 0) {
174 wprintw(win, "%s deleted\n", code_name);
184 duplicate(WINDOW *win, NCURSES_CONST char *name, int code)
186 char *value = tigetstr(name);
189 const char *prefix = 0;
191 if (!(strncmp) (value, "\033[", (size_t) 2)) {
193 } else if (!(strncmp) (value, "\033O", (size_t) 2)) {
198 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
199 "%s%s", prefix, value + 2);
200 really_define_key(win, temp, code);
206 redefine(WINDOW *win, char *string, int code)
208 really_define_key(win, string, code);
212 remove_definition(WINDOW *win, int code)
214 really_define_key(win, 0, code);
218 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
228 (void) cbreak(); /* take input chars one at a time, no wait for \n */
229 (void) noecho(); /* don't echo input */
231 printw("This demo is best on xterm: it reverses the definitions for f1-f12,\n");
232 printw("adds duplicate definitions for cursor application and normal modes,\n");
233 printw("and removes any definitions for the mini keypad. Type any of those:\n");
236 win = newwin(LINES - 3, COLS, 3, 0);
241 /* we do the define_key() calls after keypad(), since the first call to
242 * keypad() initializes the corresponding data.
244 for (n = 0; n < 12; ++n) {
246 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n + 1);
247 fkeys[n] = tigetstr(name);
249 for (n = 0; n < 12; ++n) {
250 redefine(win, fkeys[11 - n], KEY_F(n + 1));
253 duplicate(win, "kcub1", KEY_LEFT);
254 duplicate(win, "kcuu1", KEY_UP);
255 duplicate(win, "kcud1", KEY_DOWN);
256 duplicate(win, "kcuf1", KEY_RIGHT);
258 remove_definition(win, KEY_A1);
259 remove_definition(win, KEY_A3);
260 remove_definition(win, KEY_B2);
261 remove_definition(win, KEY_C1);
262 remove_definition(win, KEY_C3);
264 really_define_key(win, "\033O", 1023);
266 while ((ch = wgetch(win)) != ERR) {
267 const char *name = keyname(ch);
268 wprintw(win, "Keycode %d, name %s\n",
270 name != 0 ? name : "<null>");
277 ExitProgram(EXIT_SUCCESS);
283 printf("This program requires the ncurses library\n");
284 ExitProgram(EXIT_FAILURE);