2 * $Id: dlg_keys.c,v 1.58 2020/11/26 17:11:56 Glenn.Herteg Exp $
4 * dlg_keys.c -- runtime binding support for dialog
6 * Copyright 2006-2019,2020 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
26 #include <dlg_internals.h>
28 #define LIST_BINDINGS struct _list_bindings
30 #define CHR_BACKSLASH '\\'
31 #define IsOctal(ch) ((ch) >= '0' && (ch) <= '7')
35 WINDOW *win; /* window on which widget gets input */
36 const char *name; /* widget name */
37 bool buttons; /* true only for dlg_register_buttons() */
38 DLG_KEYS_BINDING *binding; /* list of bindings */
42 static LIST_BINDINGS *all_bindings;
43 static const DLG_KEYS_BINDING end_keys_binding = END_KEYS_BINDING;
46 * For a given named widget's window, associate a binding table.
49 dlg_register_window(WINDOW *win, const char *name, DLG_KEYS_BINDING * binding)
53 for (p = all_bindings, q = 0; p != 0; q = p, p = p->link) {
54 if (p->win == win && !strcmp(p->name, name)) {
59 /* add built-in bindings at the end of the list (see compare_bindings). */
60 if ((p = dlg_calloc(LIST_BINDINGS, 1)) != 0) {
70 #if defined(HAVE_DLG_TRACE) && defined(HAVE_RC_FILE)
72 * Trace the binding information assigned to this window. For most widgets
73 * there is only one binding table. forms have two, so the trace will be
74 * longer. Since compiled-in bindings are only visible when the widget is
75 * registered, there is no other way to see what bindings are available,
76 * than by running dialog and tracing it.
78 DLG_TRACE(("# dlg_register_window %s\n", name));
79 dlg_dump_keys(dialog_state.trace_output);
80 dlg_dump_window_keys(dialog_state.trace_output, win);
81 DLG_TRACE(("# ...done dlg_register_window %s\n", name));
86 * Unlike dlg_lookup_key(), this looks for either widget-builtin or rc-file
87 * definitions, depending on whether 'win' is null.
90 key_is_bound(WINDOW *win, const char *name, int curses_key, int function_key)
94 for (p = all_bindings; p != 0; p = p->link) {
95 if (p->win == win && !dlg_strcmp(p->name, name)) {
97 for (n = 0; p->binding[n].is_function_key >= 0; ++n) {
98 if (p->binding[n].curses_key == curses_key
99 && p->binding[n].is_function_key == function_key) {
109 * Call this function after dlg_register_window(), for the list of button
110 * labels associated with the widget.
112 * Ensure that dlg_lookup_key() will not accidentally translate a key that
113 * we would like to use for a button abbreviation to some other key, e.g.,
114 * h/j/k/l for navigation into a cursor key. Do this by binding the key
117 * See dlg_char_to_button().
120 dlg_register_buttons(WINDOW *win, const char *name, const char **buttons)
129 for (n = 0; buttons[n] != 0; ++n) {
130 int curses_key = dlg_button_to_char(buttons[n]);
132 /* ignore binding if there is no key to bind */
136 /* ignore multibyte characters */
137 if (curses_key >= KEY_MIN)
140 /* if it is not bound in the widget, skip it (no conflicts) */
141 if (!key_is_bound(win, name, curses_key, FALSE))
145 /* if it is bound in the rc-file, skip it */
146 if (key_is_bound(0, name, curses_key, FALSE))
150 if ((p = dlg_calloc(LIST_BINDINGS, 1)) != 0) {
151 if ((q = dlg_calloc(DLG_KEYS_BINDING, 2)) != 0) {
152 q[0].is_function_key = 0;
153 q[0].curses_key = curses_key;
154 q[0].dialog_key = curses_key;
155 q[1] = end_keys_binding;
162 /* put these at the beginning, to override the widget's table */
163 p->link = all_bindings;
173 * Remove the bindings for a given window.
176 dlg_unregister_window(WINDOW *win)
178 LIST_BINDINGS *p, *q;
180 for (p = all_bindings, q = 0; p != 0; p = p->link) {
185 all_bindings = p->link;
187 /* the user-defined and buttons-bindings all are length=1 */
188 if (p->binding[1].is_function_key < 0)
191 dlg_unregister_window(win);
199 * Call this after wgetch(), using the same window pointer and passing
202 * If there is no binding associated with the widget, it simply returns
203 * the given curses-key.
206 * win is the window on which the wgetch() was done.
207 * curses_key is the value returned by wgetch().
208 * fkey in/out (on input, it is nonzero if curses_key is a function key,
209 * and on output, it is nonzero if the result is a function key).
212 dlg_lookup_key(WINDOW *win, int curses_key, int *fkey)
218 * Ignore mouse clicks, since they are already encoded properly.
221 if (*fkey != 0 && curses_key == KEY_MOUSE) {
226 * Ignore resize events, since they are already encoded properly.
229 if (*fkey != 0 && curses_key == KEY_RESIZE) {
233 if (*fkey == 0 || curses_key < KEY_MAX) {
234 const char *name = WILDNAME;
236 for (p = all_bindings; p != 0; p = p->link) {
243 for (p = all_bindings; p != 0; p = p->link) {
246 (!strcmp(p->name, name) || !strcmp(p->name, WILDNAME)))) {
247 int function_key = (*fkey != 0);
248 for (q = p->binding; q->is_function_key >= 0; ++q) {
251 && q->curses_key == (int) dlg_toupper(curses_key)) {
253 return q->dialog_key;
255 if (q->curses_key == curses_key
256 && q->is_function_key == function_key) {
257 *fkey = q->dialog_key;
268 * Test a dialog internal keycode to see if it corresponds to one of the push
269 * buttons on the widget such as "OK".
271 * This is only useful if there are user-defined key bindings, since there are
272 * no built-in bindings that map directly to DLGK_OK, etc.
274 * See also dlg_ok_buttoncode().
277 dlg_result_key(int dialog_key, int fkey GCC_UNUSED, int *resultp)
281 DLG_TRACE(("# dlg_result_key(dialog_key=%d, fkey=%d)\n", dialog_key, fkey));
283 if (dialog_state.had_resize) {
284 if (dialog_key == ERR) {
287 dialog_state.had_resize = FALSE;
289 } else if (fkey && dialog_key == KEY_RESIZE) {
290 dialog_state.had_resize = TRUE;
295 switch ((DLG_KEYS_ENUM) dialog_key) {
297 if (!dialog_vars.nook) {
298 *resultp = DLG_EXIT_OK;
303 if (!dialog_vars.nocancel) {
304 *resultp = DLG_EXIT_CANCEL;
309 if (dialog_vars.extra_button) {
310 *resultp = DLG_EXIT_EXTRA;
315 if (dialog_vars.help_button) {
316 *resultp = DLG_EXIT_HELP;
321 *resultp = DLG_EXIT_ESC;
329 if (dialog_key == ESC) {
330 *resultp = DLG_EXIT_ESC;
332 } else if (dialog_key == ERR) {
333 *resultp = DLG_EXIT_ERROR;
341 * If a key was bound to one of the button-codes in dlg_result_key(), fake
342 * a button-value and an "Enter" key to cause the calling widget to return
343 * the corresponding status.
345 * See dlg_ok_buttoncode(), which maps settings for ok/extra/help and button
346 * number into exit-code.
349 dlg_button_key(int exit_code, int *button, int *dialog_key, int *fkey)
354 if (!dialog_vars.nook) {
360 if (dialog_vars.extra_button) {
361 *button = dialog_vars.nook ? 0 : 1;
365 case DLG_EXIT_CANCEL:
366 if (!dialog_vars.nocancel) {
367 *button = dialog_vars.nook ? 1 : 2;
372 if (dialog_vars.help_button) {
373 int cancel = dialog_vars.nocancel ? 0 : 1;
374 int extra = dialog_vars.extra_button ? 1 : 0;
375 int okay = dialog_vars.nook ? 0 : 1;
376 *button = okay + extra + cancel;
382 DLG_TRACE(("# dlg_button_key(%d:%s) button %d\n",
383 exit_code, dlg_exitcode2s(exit_code), *button));
384 *dialog_key = *fkey = DLGK_ENTER;
390 dlg_ok_button_key(int exit_code, int *button, int *dialog_key, int *fkey)
395 dlg_save_vars(&save);
396 dialog_vars.nocancel = TRUE;
398 result = dlg_button_key(exit_code, button, dialog_key, fkey);
400 dlg_restore_vars(&save);
410 #define ASCII_NAME(name,code) { #name, code }
411 #define CURSES_NAME(upper) { #upper, KEY_ ## upper }
412 #define COUNT_CURSES TableSize(curses_names)
413 static const CODENAME curses_names[] =
415 ASCII_NAME(ESC, '\033'),
416 ASCII_NAME(CR, '\r'),
417 ASCII_NAME(LF, '\n'),
418 ASCII_NAME(FF, '\f'),
419 ASCII_NAME(TAB, '\t'),
420 ASCII_NAME(DEL, '\177'),
427 CURSES_NAME(BACKSPACE),
456 CURSES_NAME(COMMAND),
464 CURSES_NAME(MESSAGE),
468 CURSES_NAME(OPTIONS),
469 CURSES_NAME(PREVIOUS),
471 CURSES_NAME(REFERENCE),
472 CURSES_NAME(REFRESH),
473 CURSES_NAME(REPLACE),
474 CURSES_NAME(RESTART),
478 CURSES_NAME(SCANCEL),
479 CURSES_NAME(SCOMMAND),
481 CURSES_NAME(SCREATE),
493 CURSES_NAME(SMESSAGE),
496 CURSES_NAME(SOPTIONS),
497 CURSES_NAME(SPREVIOUS),
500 CURSES_NAME(SREPLACE),
504 CURSES_NAME(SSUSPEND),
506 CURSES_NAME(SUSPEND),
510 #define DIALOG_NAME(upper) { #upper, DLGK_ ## upper }
511 #define COUNT_DIALOG TableSize(dialog_names)
512 static const CODENAME dialog_names[] =
519 DIALOG_NAME(PAGE_FIRST),
520 DIALOG_NAME(PAGE_LAST),
521 DIALOG_NAME(PAGE_NEXT),
522 DIALOG_NAME(PAGE_PREV),
523 DIALOG_NAME(ITEM_FIRST),
524 DIALOG_NAME(ITEM_LAST),
525 DIALOG_NAME(ITEM_NEXT),
526 DIALOG_NAME(ITEM_PREV),
527 DIALOG_NAME(FIELD_FIRST),
528 DIALOG_NAME(FIELD_LAST),
529 DIALOG_NAME(FIELD_NEXT),
530 DIALOG_NAME(FIELD_PREV),
531 DIALOG_NAME(FORM_FIRST),
532 DIALOG_NAME(FORM_LAST),
533 DIALOG_NAME(FORM_NEXT),
534 DIALOG_NAME(FORM_PREV),
535 DIALOG_NAME(GRID_UP),
536 DIALOG_NAME(GRID_DOWN),
537 DIALOG_NAME(GRID_LEFT),
538 DIALOG_NAME(GRID_RIGHT),
539 DIALOG_NAME(DELETE_LEFT),
540 DIALOG_NAME(DELETE_RIGHT),
541 DIALOG_NAME(DELETE_ALL),
546 DIALOG_NAME(HELPFILE),
552 #define MAP2(letter,actual) { letter, actual }
554 static const struct {
557 } escaped_letters[] = {
559 MAP2('a', DLG_CTRL('G')),
560 MAP2('b', DLG_CTRL('H')),
561 MAP2('f', DLG_CTRL('L')),
562 MAP2('n', DLG_CTRL('J')),
563 MAP2('r', DLG_CTRL('M')),
564 MAP2('s', CHR_SPACE),
565 MAP2('t', DLG_CTRL('I')),
574 while (*s != '\0' && isspace(UCH(*s)))
582 while (*s != '\0' && !isspace(UCH(*s)))
588 * Find a user-defined binding, given the curses key code.
590 static DLG_KEYS_BINDING *
591 find_binding(char *widget, int curses_key)
594 DLG_KEYS_BINDING *result = 0;
596 for (p = all_bindings; p != 0; p = p->link) {
598 && !dlg_strcmp(p->name, widget)
599 && p->binding->curses_key == curses_key) {
608 * Built-in bindings have a nonzero "win" member, and the associated binding
609 * table can have more than one entry. We keep those last, since lookups will
610 * find the user-defined bindings first and use those.
612 * Sort "*" (all-widgets) entries past named widgets, since those are less
616 compare_bindings(LIST_BINDINGS * a, LIST_BINDINGS * b)
619 if (a->win == b->win) {
620 if (!strcmp(a->name, b->name)) {
621 result = a->binding[0].curses_key - b->binding[0].curses_key;
622 } else if (!strcmp(b->name, WILDNAME)) {
624 } else if (!strcmp(a->name, WILDNAME)) {
627 result = dlg_strcmp(a->name, b->name);
638 * Find a user-defined binding, given the curses key code. If it does not
639 * exist, create a new one, inserting it into the linked list, keeping it
640 * sorted to simplify lookups for user-defined bindings that can override
641 * the built-in bindings.
643 static DLG_KEYS_BINDING *
644 make_binding(char *widget, int curses_key, int is_function, int dialog_key)
646 LIST_BINDINGS *entry = 0;
647 DLG_KEYS_BINDING *data = 0;
649 DLG_KEYS_BINDING *result = find_binding(widget, curses_key);
652 && (entry = dlg_calloc(LIST_BINDINGS, 1)) != 0
653 && (data = dlg_calloc(DLG_KEYS_BINDING, 2)) != 0
654 && (name = dlg_strclone(widget)) != 0) {
655 LIST_BINDINGS *p, *q;
658 entry->binding = data;
660 data[0].is_function_key = is_function;
661 data[0].curses_key = curses_key;
662 data[0].dialog_key = dialog_key;
664 data[1] = end_keys_binding;
666 for (p = all_bindings, q = 0; p != 0; q = p, p = p->link) {
667 if (compare_bindings(entry, p) < 0) {
674 all_bindings = entry;
680 } else if (entry != 0) {
690 decode_escaped(char **string)
694 if (IsOctal(**string)) {
696 while (limit-- > 0 && IsOctal(**string)) {
699 result = (result << 3) | (ch - '0');
704 for (n = 0; n < TableSize(escaped_letters); ++n) {
705 if (**string == escaped_letters[n].letter) {
707 result = escaped_letters[n].actual;
716 encode_escaped(int value)
718 static char result[80];
721 for (n = 0; n < TableSize(escaped_letters); ++n) {
722 if (value == escaped_letters[n].actual) {
724 sprintf(result, "%c", escaped_letters[n].letter);
729 sprintf(result, "%03o", value & 0xff);
735 * Parse the parameters of the "bindkey" configuration-file entry. This
736 * expects widget name which may be "*", followed by curses key definition and
737 * then dialog key definition.
739 * The curses key "should" be one of the names (ignoring case) from
740 * curses_names[], but may also be a single control character (prefix "^" or
741 * "~" depending on whether it is C0 or C1), or an escaped single character.
742 * Binding a printable character with dialog is possible but not useful.
744 * The dialog key must be one of the names from dialog_names[].
747 dlg_parse_bindkey(char *params)
749 char *p = skip_white(params);
760 if (p != widget && *p != '\0') {
763 bool escaped = FALSE;
765 int is_function = FALSE;
770 while (*p != '\0' && curses_key < 0) {
773 curses_key = decode_escaped(&p);
774 } else if (*p == CHR_BACKSLASH) {
776 } else if (modified) {
778 curses_key = ((modified == '^')
782 curses_key = ((modified == '^')
784 : ((*p & 0x1f) | 0x80));
786 } else if (*p == '^') {
788 } else if (*p == '~') {
790 } else if (isspace(UCH(*p))) {
795 if (!isspace(UCH(*p))) {
799 if (curses_key < 0) {
803 if (sscanf(q, "%1[Ff]%d%c", fprefix, &keynumber, check) == 2) {
804 curses_key = KEY_F(keynumber);
807 for (xx = 0; xx < COUNT_CURSES; ++xx) {
808 if (!dlg_strcmp(curses_names[xx].name, q)) {
809 curses_key = curses_names[xx].code;
810 is_function = (curses_key >= KEY_MIN);
820 for (xx = 0; xx < COUNT_DIALOG; ++xx) {
821 if (!dlg_strcmp(dialog_names[xx].name, q)) {
822 dialog_key = dialog_names[xx].code;
830 && make_binding(widget, curses_key, is_function, dialog_key) != 0) {
838 dump_curses_key(FILE *fp, int curses_key)
840 if (curses_key > KEY_MIN) {
843 for (n = 0; n < COUNT_CURSES; ++n) {
844 if (curses_names[n].code == curses_key) {
845 fprintf(fp, "%s", curses_names[n].name);
852 if (is_DLGK_MOUSE(curses_key)) {
853 fprintf(fp, "MOUSE-");
854 dump_curses_key(fp, curses_key - M_EVENT);
857 if (curses_key >= KEY_F(0)) {
858 fprintf(fp, "F%d", curses_key - KEY_F(0));
860 fprintf(fp, "curses%d", curses_key);
863 } else if (curses_key >= 0 && curses_key < 32) {
864 fprintf(fp, "^%c", curses_key + 64);
865 } else if (curses_key == 127) {
867 } else if (curses_key >= 128 && curses_key < 160) {
868 fprintf(fp, "~%c", curses_key - 64);
869 } else if (curses_key == 255) {
871 } else if (curses_key > 32 &&
873 curses_key != CHR_BACKSLASH) {
874 fprintf(fp, "%c", curses_key);
876 fprintf(fp, "%c%s", CHR_BACKSLASH, encode_escaped(curses_key));
881 dump_dialog_key(FILE *fp, int dialog_key)
885 for (n = 0; n < COUNT_DIALOG; ++n) {
886 if (dialog_names[n].code == dialog_key) {
887 fputs(dialog_names[n].name, fp);
893 fprintf(fp, "dialog%d", dialog_key);
898 dump_one_binding(FILE *fp,
901 DLG_KEYS_BINDING * binding)
904 int fkey = (binding->curses_key > 255);
906 fprintf(fp, "bindkey %s ", widget);
907 dump_curses_key(fp, binding->curses_key);
909 dump_dialog_key(fp, binding->dialog_key);
910 actual = dlg_lookup_key(win, binding->curses_key, &fkey);
912 if (is_DLGK_MOUSE(binding->curses_key) && is_DLGK_MOUSE(actual)) {
916 if (actual != binding->dialog_key) {
917 fprintf(fp, "\t# overridden by ");
918 dump_dialog_key(fp, actual);
924 * Dump bindings for the given window. If it is a null, then this dumps the
925 * initial bindings which were loaded from the rc-file that are used as
929 dlg_dump_window_keys(FILE *fp, WINDOW *win)
934 const char *last = "";
936 for (p = all_bindings; p != 0; p = p->link) {
938 if (dlg_strcmp(last, p->name)) {
939 fprintf(fp, "# key bindings for %s widgets%s\n",
940 !strcmp(p->name, WILDNAME) ? "all" : p->name,
941 win == 0 ? " (user-defined)" : "");
944 for (q = p->binding; q->is_function_key >= 0; ++q) {
945 dump_one_binding(fp, win, p->name, q);
953 * Dump all of the bindings which are not specific to a given widget, i.e.,
954 * the "win" member is null.
957 dlg_dump_keys(FILE *fp)
963 for (p = all_bindings; p != 0; p = p->link) {
969 dlg_dump_window_keys(fp, 0);
973 #endif /* HAVE_RC_FILE */