blob: 441667263f69bf585445c7a6d242790b35ba99ff [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2023,2024 Thomas E. Dickey *
3 * Copyright 2002-2016,2017 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
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/*
micky3879b9f5e72025-07-08 18:04:53 -040030 * $Id: demo_defkey.c,v 1.35 2024/01/20 20:46:12 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010031 *
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 */
45static void
46log_last_line(WINDOW *win)
47{
48 FILE *fp;
Steve Kondikae271bc2015-11-15 02:50:53 +010049
50 if ((fp = fopen(MY_LOGFILE, "a")) != 0) {
micky3879b9f5e72025-07-08 18:04:53 -040051 char temp[256];
52 int y, x, n;
Steve Kondikae271bc2015-11-15 02:50:53 +010053 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 */
74static char *
75visichar(int ch)
76{
micky3879b9f5e72025-07-08 18:04:53 -040077 static char temp[20];
Steve Kondikae271bc2015-11-15 02:50:53 +010078
79 ch = UChar(ch);
80 assert(ch >= 0 && ch < 256);
81 if (ch == '\\') {
micky3879b9f5e72025-07-08 18:04:53 -040082 _nc_STRCPY(temp, "\\\\", sizeof(temp));
Steve Kondikae271bc2015-11-15 02:50:53 +010083 } else if (ch == '\033') {
micky3879b9f5e72025-07-08 18:04:53 -040084 _nc_STRCPY(temp, "\\E", sizeof(temp));
Steve Kondikae271bc2015-11-15 02:50:53 +010085 } else if (ch < ' ') {
micky3879b9f5e72025-07-08 18:04:53 -040086 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\\%03o", ch);
Steve Kondikae271bc2015-11-15 02:50:53 +010087 } else if (ch >= 127) {
micky3879b9f5e72025-07-08 18:04:53 -040088 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\\%03o", ch);
Steve Kondikae271bc2015-11-15 02:50:53 +010089 } else {
micky3879b9f5e72025-07-08 18:04:53 -040090 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "%c", ch);
Steve Kondikae271bc2015-11-15 02:50:53 +010091 }
92 return temp;
93}
94
95/*
96 * Convert a string to visible form.
97 */
98static char *
99visible(const char *string)
100{
101 char *result = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100102
micky3879b9f5e72025-07-08 18:04:53 -0400103 if (VALID_STRING(string) && *string != '\0') {
104 int pass;
105 int n;
106 size_t need = 1;
107
Steve Kondikae271bc2015-11-15 02:50:53 +0100108 for (pass = 0; pass < 2; ++pass) {
109 for (n = 0; string[n] != '\0'; ++n) {
110 char temp[80];
micky3879b9f5e72025-07-08 18:04:53 -0400111 _nc_STRNCPY(temp, visichar(string[n]), sizeof(temp) - 2);
112 if (pass) {
113 _nc_STRCAT(result, temp, need);
114 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100115 need += strlen(temp);
micky3879b9f5e72025-07-08 18:04:53 -0400116 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100117 }
118 if (!pass)
119 result = typeCalloc(char, need);
120 }
121 } else {
122 result = typeCalloc(char, (size_t) 1);
123 }
124 return result;
125}
126
127static void
128really_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) {
micky3879b9f5e72025-07-08 18:04:53 -0400137 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "Keycode %d", code);
Steve Kondikae271bc2015-11-15 02:50:53 +0100138 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
micky3879b9f5e72025-07-08 18:04:53 -0400151 free(vis_string);
Steve Kondikae271bc2015-11-15 02:50:53 +0100152 vis_string = visible(new_string);
153 if ((rc = key_defined(new_string)) > 0) {
154 wprintw(win, "%s was bound to %s\n", vis_string, keyname(rc));
155 log_last_line(win);
156 } else if (new_string != 0 && rc < 0) {
157 wprintw(win, "%s conflicts with longer strings\n", vis_string);
158 log_last_line(win);
159 }
160 rc = define_key(new_string, code);
161 if (rc == ERR) {
162 wprintw(win, "%s unchanged\n", code_name);
163 log_last_line(win);
164 } else if (new_string != 0) {
165 wprintw(win, "%s is now bound to %s\n",
166 vis_string,
167 code_name);
168 log_last_line(win);
169 } else if (old_string != 0) {
170 wprintw(win, "%s deleted\n", code_name);
171 log_last_line(win);
172 }
173 if (vis_string != 0)
174 free(vis_string);
175 if (old_string != 0)
176 free(old_string);
177}
178
179static void
180duplicate(WINDOW *win, NCURSES_CONST char *name, int code)
181{
182 char *value = tigetstr(name);
183
micky3879b9f5e72025-07-08 18:04:53 -0400184 if (VALID_STRING(value)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100185 const char *prefix = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100186
micky3879b9f5e72025-07-08 18:04:53 -0400187 if (!(strncmp) (value, "\033[", (size_t) 2)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100188 prefix = "\033O";
micky3879b9f5e72025-07-08 18:04:53 -0400189 } else if (!(strncmp) (value, "\033O", (size_t) 2)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100190 prefix = "\033[";
191 }
192 if (prefix != 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400193 char temp[BUFSIZ];
194 _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
195 "%s%s", prefix, value + 2);
Steve Kondikae271bc2015-11-15 02:50:53 +0100196 really_define_key(win, temp, code);
197 }
198 }
199}
200
201static void
202redefine(WINDOW *win, char *string, int code)
203{
204 really_define_key(win, string, code);
205}
206
207static void
208remove_definition(WINDOW *win, int code)
209{
210 really_define_key(win, 0, code);
211}
212
micky3879b9f5e72025-07-08 18:04:53 -0400213static void
214usage(int ok)
215{
216 static const char *msg[] =
217 {
218 "Usage: demo_defkey [options]"
219 ,""
220 ,USAGE_COMMON
221 };
222 size_t n;
223
224 for (n = 0; n < SIZEOF(msg); n++)
225 fprintf(stderr, "%s\n", msg[n]);
226
227 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
228}
229/* *INDENT-OFF* */
230VERSION_COMMON()
231/* *INDENT-ON* */
232
Steve Kondikae271bc2015-11-15 02:50:53 +0100233int
micky3879b9f5e72025-07-08 18:04:53 -0400234main(int argc, char *argv[])
Steve Kondikae271bc2015-11-15 02:50:53 +0100235{
236 char *fkeys[12];
237 int n;
238 int ch;
239 WINDOW *win;
240
micky3879b9f5e72025-07-08 18:04:53 -0400241 while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
242 switch (ch) {
243 case OPTS_VERSION:
244 show_version(argv);
245 ExitProgram(EXIT_SUCCESS);
246 default:
247 usage(ch == OPTS_USAGE);
248 /* NOTREACHED */
249 }
250 }
251 if (optind < argc)
252 usage(FALSE);
253
Steve Kondikae271bc2015-11-15 02:50:53 +0100254 unlink(MY_LOGFILE);
255
micky3879b9f5e72025-07-08 18:04:53 -0400256 setlocale(LC_ALL, "");
Steve Kondikae271bc2015-11-15 02:50:53 +0100257 initscr();
258 (void) cbreak(); /* take input chars one at a time, no wait for \n */
259 (void) noecho(); /* don't echo input */
260
261 printw("This demo is best on xterm: it reverses the definitions for f1-f12,\n");
262 printw("adds duplicate definitions for cursor application and normal modes,\n");
263 printw("and removes any definitions for the mini keypad. Type any of those:\n");
264 refresh();
265
266 win = newwin(LINES - 3, COLS, 3, 0);
267 scrollok(win, TRUE);
268 keypad(win, TRUE);
269 wmove(win, 0, 0);
270
271 /* we do the define_key() calls after keypad(), since the first call to
272 * keypad() initializes the corresponding data.
273 */
274 for (n = 0; n < 12; ++n) {
micky3879b9f5e72025-07-08 18:04:53 -0400275 char name[20];
276 _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n + 1);
Steve Kondikae271bc2015-11-15 02:50:53 +0100277 fkeys[n] = tigetstr(name);
278 }
279 for (n = 0; n < 12; ++n) {
280 redefine(win, fkeys[11 - n], KEY_F(n + 1));
281 }
282
283 duplicate(win, "kcub1", KEY_LEFT);
284 duplicate(win, "kcuu1", KEY_UP);
285 duplicate(win, "kcud1", KEY_DOWN);
286 duplicate(win, "kcuf1", KEY_RIGHT);
287
288 remove_definition(win, KEY_A1);
289 remove_definition(win, KEY_A3);
290 remove_definition(win, KEY_B2);
291 remove_definition(win, KEY_C1);
292 remove_definition(win, KEY_C3);
293
294 really_define_key(win, "\033O", 1023);
295
296 while ((ch = wgetch(win)) != ERR) {
297 const char *name = keyname(ch);
298 wprintw(win, "Keycode %d, name %s\n",
299 ch,
300 name != 0 ? name : "<null>");
301 log_last_line(win);
302 wclrtoeol(win);
303 if (ch == 'q')
304 break;
305 }
306 endwin();
micky3879b9f5e72025-07-08 18:04:53 -0400307 ExitProgram(EXIT_SUCCESS);
Steve Kondikae271bc2015-11-15 02:50:53 +0100308}
309#else
310int
311main(void)
312{
313 printf("This program requires the ncurses library\n");
314 ExitProgram(EXIT_FAILURE);
315}
316#endif