]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_defkey.c
ncurses 6.4 - patch 20240414
[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.32 2022/04/16 18:26:57 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 int
218 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
219 {
220     char *fkeys[12];
221     int n;
222     int ch;
223     WINDOW *win;
224
225     unlink(MY_LOGFILE);
226
227     setlocale(LC_ALL, "");
228     initscr();
229     (void) cbreak();            /* take input chars one at a time, no wait for \n */
230     (void) noecho();            /* don't echo input */
231
232     printw("This demo is best on xterm: it reverses the definitions for f1-f12,\n");
233     printw("adds duplicate definitions for cursor application and normal modes,\n");
234     printw("and removes any definitions for the mini keypad.  Type any of those:\n");
235     refresh();
236
237     win = newwin(LINES - 3, COLS, 3, 0);
238     scrollok(win, TRUE);
239     keypad(win, TRUE);
240     wmove(win, 0, 0);
241
242     /* we do the define_key() calls after keypad(), since the first call to
243      * keypad() initializes the corresponding data.
244      */
245     for (n = 0; n < 12; ++n) {
246         char name[20];
247         _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n + 1);
248         fkeys[n] = tigetstr(name);
249     }
250     for (n = 0; n < 12; ++n) {
251         redefine(win, fkeys[11 - n], KEY_F(n + 1));
252     }
253
254     duplicate(win, "kcub1", KEY_LEFT);
255     duplicate(win, "kcuu1", KEY_UP);
256     duplicate(win, "kcud1", KEY_DOWN);
257     duplicate(win, "kcuf1", KEY_RIGHT);
258
259     remove_definition(win, KEY_A1);
260     remove_definition(win, KEY_A3);
261     remove_definition(win, KEY_B2);
262     remove_definition(win, KEY_C1);
263     remove_definition(win, KEY_C3);
264
265     really_define_key(win, "\033O", 1023);
266
267     while ((ch = wgetch(win)) != ERR) {
268         const char *name = keyname(ch);
269         wprintw(win, "Keycode %d, name %s\n",
270                 ch,
271                 name != 0 ? name : "<null>");
272         log_last_line(win);
273         wclrtoeol(win);
274         if (ch == 'q')
275             break;
276     }
277     endwin();
278     ExitProgram(EXIT_SUCCESS);
279 }
280 #else
281 int
282 main(void)
283 {
284     printf("This program requires the ncurses library\n");
285     ExitProgram(EXIT_FAILURE);
286 }
287 #endif