Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2018-2023,2024 Thomas E. Dickey * |
| 3 | * Copyright 2002-2016,2017 Free Software Foundation, Inc. * |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | /* |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 30 | * $Id: demo_defkey.c,v 1.35 2024/01/20 20:46:12 tom Exp $ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 49 | |
| 50 | if ((fp = fopen(MY_LOGFILE, "a")) != 0) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 51 | char temp[256]; |
| 52 | int y, x, n; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 77 | static char temp[20]; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 78 | |
| 79 | ch = UChar(ch); |
| 80 | assert(ch >= 0 && ch < 256); |
| 81 | if (ch == '\\') { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 82 | _nc_STRCPY(temp, "\\\\", sizeof(temp)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 83 | } else if (ch == '\033') { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 84 | _nc_STRCPY(temp, "\\E", sizeof(temp)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 85 | } else if (ch < ' ') { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 86 | _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\\%03o", ch); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 87 | } else if (ch >= 127) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 88 | _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "\\%03o", ch); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 89 | } else { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 90 | _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "%c", ch); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 102 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 103 | if (VALID_STRING(string) && *string != '\0') { |
| 104 | int pass; |
| 105 | int n; |
| 106 | size_t need = 1; |
| 107 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 108 | for (pass = 0; pass < 2; ++pass) { |
| 109 | for (n = 0; string[n] != '\0'; ++n) { |
| 110 | char temp[80]; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 111 | _nc_STRNCPY(temp, visichar(string[n]), sizeof(temp) - 2); |
| 112 | if (pass) { |
| 113 | _nc_STRCAT(result, temp, need); |
| 114 | } else { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 115 | need += strlen(temp); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 116 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 137 | _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) "Keycode %d", code); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 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 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 151 | free(vis_string); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 152 | 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 | |
| 179 | static void |
| 180 | duplicate(WINDOW *win, NCURSES_CONST char *name, int code) |
| 181 | { |
| 182 | char *value = tigetstr(name); |
| 183 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 184 | if (VALID_STRING(value)) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 185 | const char *prefix = 0; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 186 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 187 | if (!(strncmp) (value, "\033[", (size_t) 2)) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 188 | prefix = "\033O"; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 189 | } else if (!(strncmp) (value, "\033O", (size_t) 2)) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 190 | prefix = "\033["; |
| 191 | } |
| 192 | if (prefix != 0) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 193 | char temp[BUFSIZ]; |
| 194 | _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp)) |
| 195 | "%s%s", prefix, value + 2); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 196 | really_define_key(win, temp, code); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | static void |
| 202 | redefine(WINDOW *win, char *string, int code) |
| 203 | { |
| 204 | really_define_key(win, string, code); |
| 205 | } |
| 206 | |
| 207 | static void |
| 208 | remove_definition(WINDOW *win, int code) |
| 209 | { |
| 210 | really_define_key(win, 0, code); |
| 211 | } |
| 212 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 213 | static void |
| 214 | usage(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* */ |
| 230 | VERSION_COMMON() |
| 231 | /* *INDENT-ON* */ |
| 232 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 233 | int |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 234 | main(int argc, char *argv[]) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 235 | { |
| 236 | char *fkeys[12]; |
| 237 | int n; |
| 238 | int ch; |
| 239 | WINDOW *win; |
| 240 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 241 | 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 Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 254 | unlink(MY_LOGFILE); |
| 255 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 256 | setlocale(LC_ALL, ""); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 257 | 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) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 275 | char name[20]; |
| 276 | _nc_SPRINTF(name, _nc_SLIMIT(sizeof(name)) "kf%d", n + 1); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 277 | 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(); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 307 | ExitProgram(EXIT_SUCCESS); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 308 | } |
| 309 | #else |
| 310 | int |
| 311 | main(void) |
| 312 | { |
| 313 | printf("This program requires the ncurses library\n"); |
| 314 | ExitProgram(EXIT_FAILURE); |
| 315 | } |
| 316 | #endif |