]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_defkey.c
ncurses 6.1 - patch 20181013
[ncurses.git] / test / demo_defkey.c
1 /****************************************************************************
2  * Copyright (c) 2002-2017,2018 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.28 2018/02/12 09:57:31 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     int y, x, n;
49     char temp[256];
50
51     if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
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     size_t need = 1;
102     int pass;
103     int n;
104
105     if (string != 0 && *string != '\0') {
106         for (pass = 0; pass < 2; ++pass) {
107             for (n = 0; string[n] != '\0'; ++n) {
108                 char temp[80];
109                 _nc_STRNCPY(temp, visichar(string[n]), sizeof(temp) - 2);
110                 if (pass) {
111                     _nc_STRCAT(result, temp, need);
112                 } else {
113                     need += strlen(temp);
114                 }
115             }
116             if (!pass)
117                 result = typeCalloc(char, need);
118         }
119     } else {
120         result = typeCalloc(char, (size_t) 1);
121     }
122     return result;
123 }
124
125 static void
126 really_define_key(WINDOW *win, const char *new_string, int code)
127 {
128     int rc;
129     const char *code_name = keyname(code);
130     char *old_string;
131     char *vis_string = 0;
132     char temp[80];
133
134     if (code_name == 0) {
135         _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "Keycode %d", code);
136         code_name = temp;
137     }
138
139     if ((old_string = keybound(code, 0)) != 0) {
140         wprintw(win, "%s is %s\n",
141                 code_name,
142                 vis_string = visible(old_string));
143     } else {
144         wprintw(win, "%s is not bound\n",
145                 code_name);
146     }
147     log_last_line(win);
148
149     if (vis_string != 0) {
150         free(vis_string);
151         vis_string = 0;
152     }
153
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));
157         log_last_line(win);
158     } else if (new_string != 0 && rc < 0) {
159         wprintw(win, "%s conflicts with longer strings\n", vis_string);
160         log_last_line(win);
161     }
162     rc = define_key(new_string, code);
163     if (rc == ERR) {
164         wprintw(win, "%s unchanged\n", code_name);
165         log_last_line(win);
166     } else if (new_string != 0) {
167         wprintw(win, "%s is now bound to %s\n",
168                 vis_string,
169                 code_name);
170         log_last_line(win);
171     } else if (old_string != 0) {
172         wprintw(win, "%s deleted\n", code_name);
173         log_last_line(win);
174     }
175     if (vis_string != 0)
176         free(vis_string);
177     if (old_string != 0)
178         free(old_string);
179 }
180
181 static void
182 duplicate(WINDOW *win, NCURSES_CONST char *name, int code)
183 {
184     char *value = tigetstr(name);
185
186     if (value != 0) {
187         const char *prefix = 0;
188         char temp[BUFSIZ];
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             _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
197                         "%s%s", prefix, value + 2);
198             really_define_key(win, temp, code);
199         }
200     }
201 }
202
203 static void
204 redefine(WINDOW *win, char *string, int code)
205 {
206     really_define_key(win, string, code);
207 }
208
209 static void
210 remove_definition(WINDOW *win, int code)
211 {
212     really_define_key(win, 0, code);
213 }
214
215 int
216 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
217 {
218     char *fkeys[12];
219     int n;
220     int ch;
221     WINDOW *win;
222
223     unlink(MY_LOGFILE);
224
225     initscr();
226     (void) cbreak();            /* take input chars one at a time, no wait for \n */
227     (void) noecho();            /* don't echo input */
228
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");
232     refresh();
233
234     win = newwin(LINES - 3, COLS, 3, 0);
235     scrollok(win, TRUE);
236     keypad(win, TRUE);
237     wmove(win, 0, 0);
238
239     /* we do the define_key() calls after keypad(), since the first call to
240      * keypad() initializes the corresponding data.
241      */
242     for (n = 0; n < 12; ++n) {
243         char name[10];
244         _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n + 1);
245         fkeys[n] = tigetstr(name);
246     }
247     for (n = 0; n < 12; ++n) {
248         redefine(win, fkeys[11 - n], KEY_F(n + 1));
249     }
250
251     duplicate(win, "kcub1", KEY_LEFT);
252     duplicate(win, "kcuu1", KEY_UP);
253     duplicate(win, "kcud1", KEY_DOWN);
254     duplicate(win, "kcuf1", KEY_RIGHT);
255
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);
261
262     really_define_key(win, "\033O", 1023);
263
264     while ((ch = wgetch(win)) != ERR) {
265         const char *name = keyname(ch);
266         wprintw(win, "Keycode %d, name %s\n",
267                 ch,
268                 name != 0 ? name : "<null>");
269         log_last_line(win);
270         wclrtoeol(win);
271         if (ch == 'q')
272             break;
273     }
274     endwin();
275     ExitProgram(EXIT_SUCCESS);
276 }
277 #else
278 int
279 main(void)
280 {
281     printf("This program requires the ncurses library\n");
282     ExitProgram(EXIT_FAILURE);
283 }
284 #endif