2 * $Id: dlg_keys.c,v 1.45 2018/05/28 17:27:10 tom Exp $
4 * dlg_keys.c -- runtime binding support for dialog
6 * Copyright 2006-2017,2018 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.
27 #define LIST_BINDINGS struct _list_bindings
29 #define CHR_BACKSLASH '\\'
30 #define IsOctal(ch) ((ch) >= '0' && (ch) <= '7')
31 #define TableSize(name) (sizeof(name)/sizeof(name[0]))
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 multibyte characters */
133 if (curses_key >= KEY_MIN)
136 /* if it is not bound in the widget, skip it (no conflicts) */
137 if (!key_is_bound(win, name, curses_key, FALSE))
141 /* if it is bound in the rc-file, skip it */
142 if (key_is_bound(0, name, curses_key, FALSE))
146 if ((p = dlg_calloc(LIST_BINDINGS, 1)) != 0) {
147 if ((q = dlg_calloc(DLG_KEYS_BINDING, 2)) != 0) {
148 q[0].is_function_key = 0;
149 q[0].curses_key = curses_key;
150 q[0].dialog_key = curses_key;
151 q[1] = end_keys_binding;
158 /* put these at the beginning, to override the widget's table */
159 p->link = all_bindings;
169 * Remove the bindings for a given window.
172 dlg_unregister_window(WINDOW *win)
174 LIST_BINDINGS *p, *q;
176 for (p = all_bindings, q = 0; p != 0; p = p->link) {
181 all_bindings = p->link;
183 /* the user-defined and buttons-bindings all are length=1 */
184 if (p->binding[1].is_function_key < 0)
187 dlg_unregister_window(win);
195 * Call this after wgetch(), using the same window pointer and passing
198 * If there is no binding associated with the widget, it simply returns
199 * the given curses-key.
202 * win is the window on which the wgetch() was done.
203 * curses_key is the value returned by wgetch().
204 * fkey in/out (on input, it is nonzero if curses_key is a function key,
205 * and on output, it is nonzero if the result is a function key).
208 dlg_lookup_key(WINDOW *win, int curses_key, int *fkey)
214 * Ignore mouse clicks, since they are already encoded properly.
217 if (*fkey != 0 && curses_key == KEY_MOUSE) {
222 * Ignore resize events, since they are already encoded properly.
225 if (*fkey != 0 && curses_key == KEY_RESIZE) {
229 if (*fkey == 0 || curses_key < KEY_MAX) {
230 const char *name = WILDNAME;
232 for (p = all_bindings; p != 0; p = p->link) {
239 for (p = all_bindings; p != 0; p = p->link) {
242 (!strcmp(p->name, name) || !strcmp(p->name, WILDNAME)))) {
243 int function_key = (*fkey != 0);
244 for (q = p->binding; q->is_function_key >= 0; ++q) {
247 && q->curses_key == (int) dlg_toupper(curses_key)) {
249 return q->dialog_key;
251 if (q->curses_key == curses_key
252 && q->is_function_key == function_key) {
253 *fkey = q->dialog_key;
264 * Test a dialog internal keycode to see if it corresponds to one of the push
265 * buttons on the widget such as "OK".
267 * This is only useful if there are user-defined key bindings, since there are
268 * no built-in bindings that map directly to DLGK_OK, etc.
270 * See also dlg_ok_buttoncode().
273 dlg_result_key(int dialog_key, int fkey GCC_UNUSED, int *resultp)
279 switch ((DLG_KEYS_ENUM) dialog_key) {
281 *resultp = DLG_EXIT_OK;
285 if (!dialog_vars.nocancel) {
286 *resultp = DLG_EXIT_CANCEL;
291 if (dialog_vars.extra_button) {
292 *resultp = DLG_EXIT_EXTRA;
297 if (dialog_vars.help_button) {
298 *resultp = DLG_EXIT_HELP;
303 *resultp = DLG_EXIT_ESC;
311 if (dialog_key == ESC) {
312 *resultp = DLG_EXIT_ESC;
314 } else if (dialog_key == ERR) {
315 *resultp = DLG_EXIT_ERROR;
328 #define ASCII_NAME(name,code) { #name, code }
329 #define CURSES_NAME(upper) { #upper, KEY_ ## upper }
330 #define COUNT_CURSES TableSize(curses_names)
331 static const CODENAME curses_names[] =
333 ASCII_NAME(ESC, '\033'),
334 ASCII_NAME(CR, '\r'),
335 ASCII_NAME(LF, '\n'),
336 ASCII_NAME(FF, '\f'),
337 ASCII_NAME(TAB, '\t'),
338 ASCII_NAME(DEL, '\177'),
345 CURSES_NAME(BACKSPACE),
374 CURSES_NAME(COMMAND),
382 CURSES_NAME(MESSAGE),
386 CURSES_NAME(OPTIONS),
387 CURSES_NAME(PREVIOUS),
389 CURSES_NAME(REFERENCE),
390 CURSES_NAME(REFRESH),
391 CURSES_NAME(REPLACE),
392 CURSES_NAME(RESTART),
396 CURSES_NAME(SCANCEL),
397 CURSES_NAME(SCOMMAND),
399 CURSES_NAME(SCREATE),
411 CURSES_NAME(SMESSAGE),
414 CURSES_NAME(SOPTIONS),
415 CURSES_NAME(SPREVIOUS),
418 CURSES_NAME(SREPLACE),
422 CURSES_NAME(SSUSPEND),
424 CURSES_NAME(SUSPEND),
428 #define DIALOG_NAME(upper) { #upper, DLGK_ ## upper }
429 #define COUNT_DIALOG TableSize(dialog_names)
430 static const CODENAME dialog_names[] =
437 DIALOG_NAME(PAGE_FIRST),
438 DIALOG_NAME(PAGE_LAST),
439 DIALOG_NAME(PAGE_NEXT),
440 DIALOG_NAME(PAGE_PREV),
441 DIALOG_NAME(ITEM_FIRST),
442 DIALOG_NAME(ITEM_LAST),
443 DIALOG_NAME(ITEM_NEXT),
444 DIALOG_NAME(ITEM_PREV),
445 DIALOG_NAME(FIELD_FIRST),
446 DIALOG_NAME(FIELD_LAST),
447 DIALOG_NAME(FIELD_NEXT),
448 DIALOG_NAME(FIELD_PREV),
449 DIALOG_NAME(FORM_FIRST),
450 DIALOG_NAME(FORM_LAST),
451 DIALOG_NAME(FORM_NEXT),
452 DIALOG_NAME(FORM_PREV),
453 DIALOG_NAME(GRID_UP),
454 DIALOG_NAME(GRID_DOWN),
455 DIALOG_NAME(GRID_LEFT),
456 DIALOG_NAME(GRID_RIGHT),
457 DIALOG_NAME(DELETE_LEFT),
458 DIALOG_NAME(DELETE_RIGHT),
459 DIALOG_NAME(DELETE_ALL),
464 DIALOG_NAME(HELPFILE),
469 #define MAP2(letter,actual) { letter, actual }
471 static const struct {
474 } escaped_letters[] = {
476 MAP2('a', DLG_CTRL('G')),
477 MAP2('b', DLG_CTRL('H')),
478 MAP2('f', DLG_CTRL('L')),
479 MAP2('n', DLG_CTRL('J')),
480 MAP2('r', DLG_CTRL('M')),
481 MAP2('s', CHR_SPACE),
482 MAP2('t', DLG_CTRL('I')),
491 while (*s != '\0' && isspace(UCH(*s)))
499 while (*s != '\0' && !isspace(UCH(*s)))
505 * Find a user-defined binding, given the curses key code.
507 static DLG_KEYS_BINDING *
508 find_binding(char *widget, int curses_key)
511 DLG_KEYS_BINDING *result = 0;
513 for (p = all_bindings; p != 0; p = p->link) {
515 && !dlg_strcmp(p->name, widget)
516 && p->binding->curses_key == curses_key) {
525 * Built-in bindings have a nonzero "win" member, and the associated binding
526 * table can have more than one entry. We keep those last, since lookups will
527 * find the user-defined bindings first and use those.
529 * Sort "*" (all-widgets) entries past named widgets, since those are less
533 compare_bindings(LIST_BINDINGS * a, LIST_BINDINGS * b)
536 if (a->win == b->win) {
537 if (!strcmp(a->name, b->name)) {
538 result = a->binding[0].curses_key - b->binding[0].curses_key;
539 } else if (!strcmp(b->name, WILDNAME)) {
541 } else if (!strcmp(a->name, WILDNAME)) {
544 result = dlg_strcmp(a->name, b->name);
555 * Find a user-defined binding, given the curses key code. If it does not
556 * exist, create a new one, inserting it into the linked list, keeping it
557 * sorted to simplify lookups for user-defined bindings that can override
558 * the built-in bindings.
560 static DLG_KEYS_BINDING *
561 make_binding(char *widget, int curses_key, int is_function, int dialog_key)
563 LIST_BINDINGS *entry = 0;
564 DLG_KEYS_BINDING *data = 0;
566 LIST_BINDINGS *p, *q;
567 DLG_KEYS_BINDING *result = find_binding(widget, curses_key);
570 && (entry = dlg_calloc(LIST_BINDINGS, 1)) != 0
571 && (data = dlg_calloc(DLG_KEYS_BINDING, 2)) != 0
572 && (name = dlg_strclone(widget)) != 0) {
575 entry->binding = data;
577 data[0].is_function_key = is_function;
578 data[0].curses_key = curses_key;
579 data[0].dialog_key = dialog_key;
581 data[1] = end_keys_binding;
583 for (p = all_bindings, q = 0; p != 0; q = p, p = p->link) {
584 if (compare_bindings(entry, p) < 0) {
591 all_bindings = entry;
597 } else if (entry != 0) {
607 decode_escaped(char **string)
612 if (IsOctal(**string)) {
614 while (limit-- > 0 && IsOctal(**string)) {
617 result = (result << 3) | (ch - '0');
620 for (n = 0; n < TableSize(escaped_letters); ++n) {
621 if (**string == escaped_letters[n].letter) {
623 result = escaped_letters[n].actual;
632 encode_escaped(int value)
634 static char result[80];
637 for (n = 0; n < TableSize(escaped_letters); ++n) {
638 if (value == escaped_letters[n].actual) {
640 sprintf(result, "%c", escaped_letters[n].letter);
645 sprintf(result, "%03o", value & 0xff);
651 * Parse the parameters of the "bindkey" configuration-file entry. This
652 * expects widget name which may be "*", followed by curses key definition and
653 * then dialog key definition.
655 * The curses key "should" be one of the names (ignoring case) from
656 * curses_names[], but may also be a single control character (prefix "^" or
657 * "~" depending on whether it is C0 or C1), or an escaped single character.
658 * Binding a printable character with dialog is possible but not useful.
660 * The dialog key must be one of the names from dialog_names[].
663 dlg_parse_bindkey(char *params)
665 char *p = skip_white(params);
667 bool escaped = FALSE;
672 int is_function = FALSE;
681 if (p != widget && *p != '\0') {
685 while (*p != '\0' && curses_key < 0) {
688 curses_key = decode_escaped(&p);
689 } else if (*p == CHR_BACKSLASH) {
691 } else if (modified) {
693 curses_key = ((modified == '^')
697 curses_key = ((modified == '^')
699 : ((*p & 0x1f) | 0x80));
701 } else if (*p == '^') {
703 } else if (*p == '~') {
705 } else if (isspace(UCH(*p))) {
710 if (!isspace(UCH(*p))) {
714 if (curses_key < 0) {
718 if (sscanf(q, "%[Ff]%d%c", fprefix, &keynumber, check) == 2) {
719 curses_key = KEY_F(keynumber);
722 for (xx = 0; xx < COUNT_CURSES; ++xx) {
723 if (!dlg_strcmp(curses_names[xx].name, q)) {
724 curses_key = curses_names[xx].code;
725 is_function = (curses_key >= KEY_MIN);
735 for (xx = 0; xx < COUNT_DIALOG; ++xx) {
736 if (!dlg_strcmp(dialog_names[xx].name, q)) {
737 dialog_key = dialog_names[xx].code;
745 && make_binding(widget, curses_key, is_function, dialog_key) != 0) {
753 dump_curses_key(FILE *fp, int curses_key)
755 if (curses_key > KEY_MIN) {
758 for (n = 0; n < COUNT_CURSES; ++n) {
759 if (curses_names[n].code == curses_key) {
760 fprintf(fp, "%s", curses_names[n].name);
767 if (is_DLGK_MOUSE(curses_key)) {
768 fprintf(fp, "MOUSE-");
769 dump_curses_key(fp, curses_key - M_EVENT);
772 if (curses_key >= KEY_F(0)) {
773 fprintf(fp, "F%d", curses_key - KEY_F(0));
775 fprintf(fp, "curses%d", curses_key);
778 } else if (curses_key >= 0 && curses_key < 32) {
779 fprintf(fp, "^%c", curses_key + 64);
780 } else if (curses_key == 127) {
782 } else if (curses_key >= 128 && curses_key < 160) {
783 fprintf(fp, "~%c", curses_key - 64);
784 } else if (curses_key == 255) {
786 } else if (curses_key > 32 &&
788 curses_key != CHR_BACKSLASH) {
789 fprintf(fp, "%c", curses_key);
791 fprintf(fp, "%c%s", CHR_BACKSLASH, encode_escaped(curses_key));
796 dump_dialog_key(FILE *fp, int dialog_key)
800 for (n = 0; n < COUNT_DIALOG; ++n) {
801 if (dialog_names[n].code == dialog_key) {
802 fputs(dialog_names[n].name, fp);
808 fprintf(fp, "dialog%d", dialog_key);
813 dump_one_binding(FILE *fp,
816 DLG_KEYS_BINDING * binding)
819 int fkey = (binding->curses_key > 255);
821 fprintf(fp, "bindkey %s ", widget);
822 dump_curses_key(fp, binding->curses_key);
824 dump_dialog_key(fp, binding->dialog_key);
825 actual = dlg_lookup_key(win, binding->curses_key, &fkey);
827 if (is_DLGK_MOUSE(binding->curses_key) && is_DLGK_MOUSE(actual)) {
831 if (actual != binding->dialog_key) {
832 fprintf(fp, "\t# overridden by ");
833 dump_dialog_key(fp, actual);
839 * Dump bindings for the given window. If it is a null, then this dumps the
840 * initial bindings which were loaded from the rc-file that are used as
844 dlg_dump_window_keys(FILE *fp, WINDOW *win)
849 const char *last = "";
851 for (p = all_bindings; p != 0; p = p->link) {
853 if (dlg_strcmp(last, p->name)) {
854 fprintf(fp, "# key bindings for %s widgets%s\n",
855 !strcmp(p->name, WILDNAME) ? "all" : p->name,
856 win == 0 ? " (user-defined)" : "");
859 for (q = p->binding; q->is_function_key >= 0; ++q) {
860 dump_one_binding(fp, win, p->name, q);
868 * Dump all of the bindings which are not specific to a given widget, i.e.,
869 * the "win" member is null.
872 dlg_dump_keys(FILE *fp)
878 for (p = all_bindings; p != 0; p = p->link) {
884 dlg_dump_window_keys(fp, 0);
888 #endif /* HAVE_RC_FILE */