2 * $Id: dlg_keys.c,v 1.26 2009/02/22 16:19:51 tom Exp $
4 * dlg_keys.c -- runtime binding support for dialog
6 * Copyright 2006-2007,2009 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
31 WINDOW *win; /* window on which widget gets input */
32 const char *name; /* widget name */
33 bool buttons; /* true only for dlg_register_buttons() */
34 DLG_KEYS_BINDING *binding; /* list of bindings */
37 static LIST_BINDINGS *all_bindings;
38 static const DLG_KEYS_BINDING end_keys_binding = END_KEYS_BINDING;
41 * For a given named widget's window, associate a binding table.
44 dlg_register_window(WINDOW *win, const char *name, DLG_KEYS_BINDING * binding)
48 for (p = all_bindings, q = 0; p != 0; q = p, p = p->link) {
49 if (p->win == win && !strcmp(p->name, name)) {
54 /* add built-in bindings at the end of the list (see compare_bindings). */
55 if ((p = dlg_calloc(LIST_BINDINGS, 1)) != 0) {
67 * Unlike dlg_lookup_key(), this looks for either widget-builtin or rc-file
68 * definitions, depending on whether 'win' is null.
71 key_is_bound(WINDOW *win, const char *name, int curses_key, int function_key)
75 for (p = all_bindings; p != 0; p = p->link) {
76 if (p->win == win && !dlg_strcmp(p->name, name)) {
78 for (n = 0; p->binding[n].is_function_key >= 0; ++n) {
79 if (p->binding[n].curses_key == curses_key
80 && p->binding[n].is_function_key == function_key) {
90 * Call this function after dlg_register_window(), for the list of button
91 * labels associated with the widget.
93 * Ensure that dlg_lookup_key() will not accidentally translate a key that
94 * we would like to use for a button abbreviation to some other key, e.g.,
95 * h/j/k/l for navigation into a cursor key. Do this by binding the key
98 * See dlg_char_to_button().
101 dlg_register_buttons(WINDOW *win, const char *name, const char **buttons)
110 for (n = 0; buttons[n] != 0; ++n) {
111 int curses_key = dlg_button_to_char(buttons[n]);
113 /* ignore multibyte characters */
114 if (curses_key >= KEY_MIN)
117 /* if it is not bound in the widget, skip it (no conflicts) */
118 if (!key_is_bound(win, name, curses_key, FALSE))
122 /* if it is bound in the rc-file, skip it */
123 if (key_is_bound(0, name, curses_key, FALSE))
127 if ((p = dlg_calloc(LIST_BINDINGS, 1)) != 0) {
128 if ((q = dlg_calloc(DLG_KEYS_BINDING, 2)) != 0) {
129 q[0].is_function_key = 0;
130 q[0].curses_key = curses_key;
131 q[0].dialog_key = curses_key;
132 q[1] = end_keys_binding;
139 /* put these at the beginning, to override the widget's table */
140 p->link = all_bindings;
150 * Remove the bindings for a given window.
153 dlg_unregister_window(WINDOW *win)
155 LIST_BINDINGS *p, *q;
157 for (p = all_bindings, q = 0; p != 0; p = p->link) {
162 all_bindings = p->link;
164 /* the user-defined and buttons-bindings all are length=1 */
165 if (p->binding[1].is_function_key < 0)
168 dlg_unregister_window(win);
176 * Call this after wgetch(), using the same window pointer and passing
179 * If there is no binding associated with the widget, it simply returns
180 * the given curses-key.
183 * win is the window on which the wgetch() was done.
184 * curses_key is the value returned by wgetch().
185 * fkey in/out (on input, it is true if curses_key is a function key,
186 * and on output, it is true if the result is a function key).
189 dlg_lookup_key(WINDOW *win, int curses_key, int *fkey)
195 * Ignore mouse clicks, since they are already encoded properly.
198 if (*fkey != 0 && curses_key == KEY_MOUSE) {
203 * Ignore resize events, since they are already encoded properly.
206 if (*fkey != 0 && curses_key == KEY_RESIZE) {
210 if (*fkey == 0 || curses_key < KEY_MAX) {
211 for (p = all_bindings; p != 0; p = p->link) {
212 if (p->win == win || p->win == 0) {
213 int function_key = (*fkey != 0);
214 for (n = 0; p->binding[n].is_function_key >= 0; ++n) {
217 && p->binding[n].curses_key == (int) dlg_toupper(curses_key)) {
219 return p->binding[n].dialog_key;
221 if (p->binding[n].curses_key == curses_key
222 && p->binding[n].is_function_key == function_key) {
223 *fkey = p->binding[n].dialog_key;
234 * Test a dialog internal keycode to see if it corresponds to one of the push
235 * buttons on the widget such as "OK".
237 * This is only useful if there are user-defined key bindings, since there are
238 * no built-in bindings that map directly to DLGK_OK, etc.
240 * See also dlg_ok_buttoncode().
243 dlg_result_key(int dialog_key, int fkey GCC_UNUSED, int *resultp)
249 switch ((DLG_KEYS_ENUM) dialog_key) {
251 *resultp = DLG_EXIT_OK;
255 if (!dialog_vars.nocancel) {
256 *resultp = DLG_EXIT_CANCEL;
261 if (dialog_vars.extra_button) {
262 *resultp = DLG_EXIT_EXTRA;
267 if (dialog_vars.help_button) {
268 *resultp = DLG_EXIT_HELP;
273 *resultp = DLG_EXIT_ESC;
281 if (dialog_key == ESC) {
282 *resultp = DLG_EXIT_ESC;
284 } else if (dialog_key == ERR) {
285 *resultp = DLG_EXIT_ERROR;
298 #define CURSES_NAME(upper) { #upper, KEY_ ## upper }
299 #define COUNT_CURSES sizeof(curses_names)/sizeof(curses_names[0])
300 static const CODENAME curses_names[] =
307 CURSES_NAME(BACKSPACE),
336 CURSES_NAME(COMMAND),
344 CURSES_NAME(MESSAGE),
348 CURSES_NAME(OPTIONS),
349 CURSES_NAME(PREVIOUS),
351 CURSES_NAME(REFERENCE),
352 CURSES_NAME(REFRESH),
353 CURSES_NAME(REPLACE),
354 CURSES_NAME(RESTART),
358 CURSES_NAME(SCANCEL),
359 CURSES_NAME(SCOMMAND),
361 CURSES_NAME(SCREATE),
373 CURSES_NAME(SMESSAGE),
376 CURSES_NAME(SOPTIONS),
377 CURSES_NAME(SPREVIOUS),
380 CURSES_NAME(SREPLACE),
384 CURSES_NAME(SSUSPEND),
386 CURSES_NAME(SUSPEND),
390 #define DIALOG_NAME(upper) { #upper, DLGK_ ## upper }
391 #define COUNT_DIALOG sizeof(dialog_names)/sizeof(dialog_names[0])
392 static const CODENAME dialog_names[] =
399 DIALOG_NAME(PAGE_FIRST),
400 DIALOG_NAME(PAGE_LAST),
401 DIALOG_NAME(PAGE_NEXT),
402 DIALOG_NAME(PAGE_PREV),
403 DIALOG_NAME(ITEM_FIRST),
404 DIALOG_NAME(ITEM_LAST),
405 DIALOG_NAME(ITEM_NEXT),
406 DIALOG_NAME(ITEM_PREV),
407 DIALOG_NAME(FIELD_FIRST),
408 DIALOG_NAME(FIELD_LAST),
409 DIALOG_NAME(FIELD_NEXT),
410 DIALOG_NAME(FIELD_PREV),
411 DIALOG_NAME(GRID_UP),
412 DIALOG_NAME(GRID_DOWN),
413 DIALOG_NAME(GRID_LEFT),
414 DIALOG_NAME(GRID_RIGHT),
415 DIALOG_NAME(DELETE_LEFT),
416 DIALOG_NAME(DELETE_RIGHT),
417 DIALOG_NAME(DELETE_ALL),
427 while (*s != '\0' && isspace(UCH(*s)))
435 while (*s != '\0' && !isspace(UCH(*s)))
441 * Find a user-defined binding, given the curses key code.
443 static DLG_KEYS_BINDING *
444 find_binding(char *widget, int curses_key)
447 DLG_KEYS_BINDING *result = 0;
449 for (p = all_bindings; p != 0; p = p->link) {
451 && !dlg_strcmp(p->name, widget)
452 && p->binding->curses_key == curses_key) {
461 * Built-in bindings have a nonzero "win" member, and the associated binding
462 * table can have more than one entry. We keep those last, since lookups will
463 * find the user-defined bindings first and use those.
465 * Sort "*" (all-widgets) entries past named widgets, since those are less
469 compare_bindings(LIST_BINDINGS * a, LIST_BINDINGS * b)
472 if (a->win == b->win) {
473 if (!strcmp(a->name, b->name)) {
474 result = a->binding[0].curses_key - b->binding[0].curses_key;
475 } else if (!strcmp(b->name, "*")) {
477 } else if (!strcmp(a->name, "*")) {
480 result = dlg_strcmp(a->name, b->name);
491 * Find a user-defined binding, given the curses key code. If it does not
492 * exist, create a new one, inserting it into the linked list, keeping it
493 * sorted to simplify lookups for user-defined bindings that can override
494 * the built-in bindings.
496 static DLG_KEYS_BINDING *
497 make_binding(char *widget, int curses_key, int is_function, int dialog_key)
499 LIST_BINDINGS *entry = 0;
500 DLG_KEYS_BINDING *data = 0;
502 LIST_BINDINGS *p, *q;
503 DLG_KEYS_BINDING *result = find_binding(widget, curses_key);
506 && (entry = dlg_calloc(LIST_BINDINGS, 1)) != 0
507 && (data = dlg_calloc(DLG_KEYS_BINDING, 2)) != 0
508 && (name = dlg_strclone(widget)) != 0) {
511 entry->binding = data;
513 data[0].is_function_key = is_function;
514 data[0].curses_key = curses_key;
515 data[0].dialog_key = dialog_key;
517 data[1] = end_keys_binding;
519 for (p = all_bindings, q = 0; p != 0; q = p, p = p->link) {
520 if (compare_bindings(entry, p) < 0) {
527 all_bindings = entry;
533 } else if (entry != 0) {
543 * Parse the parameters of the "bindkeys" configuration-file entry. This
544 * expects widget name which may be "*", followed by curses key definition and
545 * then dialog key definition.
547 * The curses key "should" be one of the names (ignoring case) from
548 * curses_names[], but may also be a single control character (prefix "^" or
549 * "~" depending on whether it is C0 or C1), or an escaped single character.
550 * Binding a printable character with dialog is possible but not useful.
552 * The dialog key must be one of the names from dialog_names[].
555 dlg_parse_bindkey(char *params)
557 char *p = skip_white(params);
559 bool escaped = FALSE;
564 int is_function = FALSE;
573 if (p != widget && *p != '\0') {
576 while (*p != '\0' && curses_key < 0) {
580 } else if (*p == '\\') {
582 } else if (modified) {
584 curses_key = ((modified == '^')
588 curses_key = ((modified == '^')
590 : ((*p & 0x1f) | 0x80));
592 } else if (*p == '^') {
594 } else if (*p == '~') {
596 } else if (isspace(UCH(*p))) {
601 if (!isspace(UCH(*p))) {
605 if (curses_key < 0) {
609 if (sscanf(q, "%[Ff]%d%c", fprefix, &keynumber, check) == 2) {
610 curses_key = KEY_F(keynumber);
613 for (xx = 0; xx < COUNT_CURSES; ++xx) {
614 if (!dlg_strcmp(curses_names[xx].name, q)) {
615 curses_key = curses_names[xx].code;
626 for (xx = 0; xx < COUNT_DIALOG; ++xx) {
627 if (!dlg_strcmp(dialog_names[xx].name, q)) {
628 dialog_key = dialog_names[xx].code;
636 && make_binding(widget, curses_key, is_function, dialog_key) != 0) {
644 dump_curses_key(FILE *fp, int curses_key)
646 if (curses_key > KEY_MIN) {
649 for (n = 0; n < COUNT_CURSES; ++n) {
650 if (curses_names[n].code == curses_key) {
651 fprintf(fp, "%s", curses_names[n].name);
657 if (curses_key >= KEY_F(0)) {
658 fprintf(fp, "F%d", curses_key - KEY_F(0));
660 fprintf(fp, "curses%d", curses_key);
663 } else if (curses_key >= 0 && curses_key < 32) {
664 fprintf(fp, "^%c", curses_key + 64);
665 } else if (curses_key == 127) {
667 } else if (curses_key >= 128 && curses_key < 160) {
668 fprintf(fp, "~%c", curses_key - 64);
669 } else if (curses_key == 255) {
672 fprintf(fp, "\\%c", curses_key);
677 dump_dialog_key(FILE *fp, int dialog_key)
681 for (n = 0; n < COUNT_DIALOG; ++n) {
682 if (dialog_names[n].code == dialog_key) {
683 fputs(dialog_names[n].name, fp);
689 fprintf(fp, "dialog%d", dialog_key);
694 dump_one_binding(FILE *fp, const char *widget, DLG_KEYS_BINDING * binding)
696 fprintf(fp, "bindkey %s ", widget);
697 dump_curses_key(fp, binding->curses_key);
699 dump_dialog_key(fp, binding->dialog_key);
704 dlg_dump_keys(FILE *fp)
707 const char *last = "";
711 for (p = all_bindings; p != 0; p = p->link) {
717 for (p = all_bindings, n = 0; p != 0; p = p->link) {
719 if (dlg_strcmp(last, p->name)) {
720 fprintf(fp, "\n# key bindings for %s widgets\n",
721 !strcmp(p->name, "*") ? "all" : p->name);
724 dump_one_binding(fp, p->name, p->binding);
729 #endif /* HAVE_RC_FILE */