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