]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_defkey.c
ncurses 6.3 - patch 20221210
[ncurses.git] / test / demo_defkey.c
1 /****************************************************************************
2  * Copyright 2018-2020,2022 Thomas E. Dickey                                *
3  * Copyright 2002-2016,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
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:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
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.                               *
23  *                                                                          *
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       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 /*
30  * $Id: demo_defkey.c,v 1.33 2022/12/10 23:31:31 tom Exp $
31  *
32  * Demonstrate the define_key() function.
33  * Thomas Dickey - 2002/11/23
34  */
35
36 #include <test.priv.h>
37
38 #if defined(NCURSES_VERSION) && NCURSES_EXT_FUNCS
39
40 #define MY_LOGFILE "demo_defkey.log"
41
42 /*
43  * Log the most recently-written line to our logfile
44  */
45 static void
46 log_last_line(WINDOW *win)
47 {
48     FILE *fp;
49
50     if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
51         char temp[256];
52         int y, x, n;
53         int need = sizeof(temp) - 1;
54         if (need > COLS)
55             need = COLS;
56         getyx(win, y, x);
57         wmove(win, y - 1, 0);
58         n = winnstr(win, temp, need);
59         while (n-- > 0) {
60             if (isspace(UChar(temp[n])))
61                 temp[n] = '\0';
62             else
63                 break;
64         }
65         wmove(win, y, x);
66         fprintf(fp, "%s\n", temp);
67         fclose(fp);
68     }
69 }
70
71 /*
72  * Convert a character to visible form.
73  */
74 static char *
75 visichar(int ch)
76 {
77     static char temp[20];
78
79     ch = UChar(ch);
80     assert(ch >= 0 && ch < 256);
81     if (ch == '\\') {
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);
89     } else {
90         _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "%c", ch);
91     }
92     return temp;
93 }
94
95 /*
96  * Convert a string to visible form.
97  */
98 static char *
99 visible(const char *string)
100 {
101     char *result = 0;
102
103     if (string != 0 && *string != '\0') {
104         int pass;
105         int n;
106         size_t need = 1;
107
108         for (pass = 0; pass < 2; ++pass) {
109             for (n = 0; string[n] != '\0'; ++n) {
110                 char temp[80];
111                 _nc_STRNCPY(temp, visichar(string[n]), sizeof(temp) - 2);
112                 if (pass) {
113                     _nc_STRCAT(result, temp, need);
114                 } else {
115                     need += strlen(temp);
116                 }
117             }
118             if (!pass)
119                 result = typeCalloc(char, need);
120         }
121     } else {
122         result = typeCalloc(char, (size_t) 1);
123     }
124     return result;
125 }
126
127 static void
128 really_define_key(WINDOW *win, const char *new_string, int code)
129 {
130     int rc;
131     const char *code_name = keyname(code);
132     char *old_string;
133     char *vis_string = 0;
134     char temp[80];
135
136     if (code_name == 0) {
137         _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "Keycode %d", code);
138         code_name = temp;
139     }
140
141     if ((old_string = keybound(code, 0)) != 0) {
142         wprintw(win, "%s is %s\n",
143                 code_name,
144                 vis_string = visible(old_string));
145     } else {
146         wprintw(win, "%s is not bound\n",
147                 code_name);
148     }
149     log_last_line(win);
150
151     if (vis_string != 0) {
152         free(vis_string);
153         vis_string = 0;
154     }
155
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));
159         log_last_line(win);
160     } else if (new_string != 0 && rc < 0) {
161         wprintw(win, "%s conflicts with longer strings\n", vis_string);
162         log_last_line(win);
163     }
164     rc = define_key(new_string, code);
165     if (rc == ERR) {
166         wprintw(win, "%s unchanged\n", code_name);
167         log_last_line(win);
168     } else if (new_string != 0) {
169         wprintw(win, "%s is now bound to %s\n",
170                 vis_string,
171                 code_name);
172         log_last_line(win);
173     } else if (old_string != 0) {
174         wprintw(win, "%s deleted\n", code_name);
175         log_last_line(win);
176     }
177     if (vis_string != 0)
178         free(vis_string);
179     if (old_string != 0)
180         free(old_string);
181 }
182
183 static void
184 duplicate(WINDOW *win, NCURSES_CONST char *name, int code)
185 {
186     char *value = tigetstr(name);
187
188     if (value != 0) {
189         const char *prefix = 0;
190
191         if (!(strncmp) (value, "\033[", (size_t) 2)) {
192             prefix = "\033O";
193         } else if (!(strncmp) (value, "\033O", (size_t) 2)) {
194             prefix = "\033[";
195         }
196         if (prefix != 0) {
197             char temp[BUFSIZ];
198             _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
199                         "%s%s", prefix, value + 2);
200             really_define_key(win, temp, code);
201         }
202     }
203 }
204
205 static void
206 redefine(WINDOW *win, char *string, int code)
207 {
208     really_define_key(win, string, code);
209 }
210
211 static void
212 remove_definition(WINDOW *win, int code)
213 {
214     really_define_key(win, 0, code);
215 }
216
217 static void
218 usage(int ok)
219 {
220     static const char *msg[] =
221     {
222         "Usage: demo_defkey [options]"
223         ,""
224         ,USAGE_COMMON
225     };
226     size_t n;
227
228     for (n = 0; n < SIZEOF(msg); n++)
229         fprintf(stderr, "%s\n", msg[n]);
230
231     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
232 }
233 /* *INDENT-OFF* */
234 VERSION_COMMON()
235 /* *INDENT-ON* */
236
237 int
238 main(int argc, char *argv[])
239 {
240     char *fkeys[12];
241     int n;
242     int ch;
243     WINDOW *win;
244
245     while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
246         switch (ch) {
247         case OPTS_VERSION:
248             show_version(argv);
249             ExitProgram(EXIT_SUCCESS);
250         default:
251             usage(ch == OPTS_USAGE);
252             /* NOTREACHED */
253         }
254     }
255     if (optind < argc)
256         usage(FALSE);
257
258     unlink(MY_LOGFILE);
259
260     setlocale(LC_ALL, "");
261     initscr();
262     (void) cbreak();            /* take input chars one at a time, no wait for \n */
263     (void) noecho();            /* don't echo input */
264
265     printw("This demo is best on xterm: it reverses the definitions for f1-f12,\n");
266     printw("adds duplicate definitions for cursor application and normal modes,\n");
267     printw("and removes any definitions for the mini keypad.  Type any of those:\n");
268     refresh();
269
270     win = newwin(LINES - 3, COLS, 3, 0);
271     scrollok(win, TRUE);
272     keypad(win, TRUE);
273     wmove(win, 0, 0);
274
275     /* we do the define_key() calls after keypad(), since the first call to
276      * keypad() initializes the corresponding data.
277      */
278     for (n = 0; n < 12; ++n) {
279         char name[20];
280         _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n + 1);
281         fkeys[n] = tigetstr(name);
282     }
283     for (n = 0; n < 12; ++n) {
284         redefine(win, fkeys[11 - n], KEY_F(n + 1));
285     }
286
287     duplicate(win, "kcub1", KEY_LEFT);
288     duplicate(win, "kcuu1", KEY_UP);
289     duplicate(win, "kcud1", KEY_DOWN);
290     duplicate(win, "kcuf1", KEY_RIGHT);
291
292     remove_definition(win, KEY_A1);
293     remove_definition(win, KEY_A3);
294     remove_definition(win, KEY_B2);
295     remove_definition(win, KEY_C1);
296     remove_definition(win, KEY_C3);
297
298     really_define_key(win, "\033O", 1023);
299
300     while ((ch = wgetch(win)) != ERR) {
301         const char *name = keyname(ch);
302         wprintw(win, "Keycode %d, name %s\n",
303                 ch,
304                 name != 0 ? name : "<null>");
305         log_last_line(win);
306         wclrtoeol(win);
307         if (ch == 'q')
308             break;
309     }
310     endwin();
311     ExitProgram(EXIT_SUCCESS);
312 }
313 #else
314 int
315 main(void)
316 {
317     printf("This program requires the ncurses library\n");
318     ExitProgram(EXIT_FAILURE);
319 }
320 #endif