source: keymap.c @ 9f5e847

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 9f5e847 was 36486be, checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
Replace several owl_malloc, sprintf sequences with owl_strdup or owl_sprintf. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 8.2 KB
Line 
1#include <string.h>
2#include "owl.h"
3
4/* returns 0 on success */
5int owl_keymap_init(owl_keymap *km, const char *name, const char *desc, void (*default_fn)(owl_input), void (*prealways_fn)(owl_input), void (*postalways_fn)(owl_input))
6{
7  if (!name || !desc) return(-1);
8  if ((km->name = owl_strdup(name)) == NULL) return(-1);
9  if ((km->desc = owl_strdup(desc)) == NULL) return(-1);
10  if (0 != owl_list_create(&km->bindings)) return(-1);
11  km->submap = NULL;
12  km->default_fn = default_fn;
13  km->prealways_fn = prealways_fn;
14  km->postalways_fn = postalways_fn;
15  return(0);
16}
17
18/* note that this will free the memory for the bindings! */
19void owl_keymap_free(owl_keymap *km)
20{
21  owl_free(km->name);
22  owl_free(km->desc);
23  owl_list_free_all(&km->bindings, (void(*)(void*))owl_keybinding_free_all);
24}
25
26void owl_keymap_set_submap(owl_keymap *km, const owl_keymap *submap)
27{
28  km->submap = submap;
29}
30
31/* creates and adds a key binding */
32int owl_keymap_create_binding(owl_keymap *km, const char *keyseq, const char *command, void (*function_fn)(void), const char *desc)
33{
34  owl_keybinding *kb, *curkb;
35  int i;
36
37  if ((kb = owl_malloc(sizeof(owl_keybinding))) == NULL) return(-1);
38  if (0 != owl_keybinding_init(kb, keyseq, command, function_fn, desc)) {
39    owl_free(kb);
40    return(-1);
41  }
42  /* see if another matching binding, and if so remove it.
43   * otherwise just add this one.
44   */
45  for (i = owl_list_get_size(&km->bindings)-1; i>=0; i--) {
46    curkb = owl_list_get_element(&km->bindings, i);
47    if (owl_keybinding_equal(curkb, kb)) {
48      owl_list_remove_element(&km->bindings, i);
49      owl_keybinding_free_all(curkb);
50    }
51  }
52  return owl_list_append_element(&km->bindings, kb); 
53}
54
55/* returns a summary line describing this keymap.  the caller must free. */
56char *owl_keymap_summary(const owl_keymap *km)
57{
58  if (!km || !km->name || !km->desc) return NULL;
59  return owl_sprintf("%-15s - %s", km->name, km->desc);
60}
61
62/* Appends details about the keymap to fm */
63void owl_keymap_get_details(const owl_keymap *km, owl_fmtext *fm)
64{
65  int i, nbindings; 
66  const owl_keybinding *kb;
67 
68  owl_fmtext_append_bold(fm, "KEYMAP - ");
69  owl_fmtext_append_bold(fm, km->name);
70  owl_fmtext_append_normal(fm, "\n");
71  if (km->desc) {
72    owl_fmtext_append_normal(fm, OWL_TABSTR "Purpose:    ");
73    owl_fmtext_append_normal(fm, km->desc);
74    owl_fmtext_append_normal(fm, "\n");
75  }
76  if (km->submap) {
77    owl_fmtext_append_normal(fm, OWL_TABSTR "Has submap: ");
78    owl_fmtext_append_normal(fm, km->submap->name);
79    owl_fmtext_append_normal(fm, "\n");
80  }
81    owl_fmtext_append_normal(fm, "\n");
82  if (km->default_fn) {
83    owl_fmtext_append_normal(fm, OWL_TABSTR
84     "Has a default keypress handler (default_fn).\n");
85  }
86  if (km->prealways_fn) {
87    owl_fmtext_append_normal(fm, OWL_TABSTR
88     "Executes a function (prealways_fn) on every keypress.\n");
89  }
90  if (km->postalways_fn) {
91    owl_fmtext_append_normal(fm, OWL_TABSTR
92     "Executes a function (postalways_fn) after handling every keypress.\n");
93  }
94
95  owl_fmtext_append_bold(fm, "\nKey bindings:\n\n"); 
96  nbindings = owl_list_get_size(&km->bindings);
97  for (i=0; i<nbindings; i++) {
98    char buff[100];
99    const owl_cmd *cmd;
100    const char *tmpdesc, *desc = "";
101
102    kb = owl_list_get_element(&km->bindings, i);
103    owl_keybinding_tostring(kb, buff, 100);
104    owl_fmtext_append_normal(fm, OWL_TABSTR);
105    owl_fmtext_append_normal(fm, buff);
106    owl_fmtext_append_spaces(fm, 11-strlen(buff));
107    owl_fmtext_append_normal(fm, " - ");
108    if (kb->desc && *kb->desc) {
109      desc = kb->desc;
110    } else if ((cmd=owl_function_get_cmd(kb->command))
111               && (tmpdesc = owl_cmd_get_summary(cmd))) {
112      desc = tmpdesc;
113    }
114    owl_fmtext_append_normal(fm, desc);
115    if (kb->command && *(kb->command)) {
116      owl_fmtext_append_normal(fm, "   [");
117      owl_fmtext_append_normal(fm, kb->command);
118      owl_fmtext_append_normal(fm, "]");
119    } 
120    owl_fmtext_append_normal(fm, "\n");
121  }
122}
123
124
125
126/***********************************************************************/
127/***************************** KEYHANDLER ******************************/
128/***********************************************************************/
129
130/* NOTE: keyhandler has private access to the internals of keymap */
131
132int owl_keyhandler_init(owl_keyhandler *kh)
133{
134  if (0 != owl_dict_create(&kh->keymaps)) return(-1); 
135  kh->active = NULL;
136  owl_keyhandler_reset(kh);
137  return(0);
138}
139
140/* adds a new keymap */
141void owl_keyhandler_add_keymap(owl_keyhandler *kh, owl_keymap *km)
142{
143  owl_dict_insert_element(&kh->keymaps, km->name, km, NULL);
144}
145
146owl_keymap *owl_keyhandler_create_and_add_keymap(owl_keyhandler *kh, const char *name, const char *desc, void (*default_fn)(owl_input), void (*prealways_fn)(owl_input), void (*postalways_fn)(owl_input))
147{
148  owl_keymap *km;
149  km = owl_malloc(sizeof(owl_keymap));
150  if (!km) return NULL;
151  owl_keymap_init(km, name, desc, default_fn, prealways_fn, postalways_fn);
152  owl_keyhandler_add_keymap(kh, km);
153  return km;
154}
155
156/* resets state and clears out key stack */
157void owl_keyhandler_reset(owl_keyhandler *kh)
158{
159  kh->in_esc = 0;
160  memset(kh->kpstack, 0, (OWL_KEYMAP_MAXSTACK+1)*sizeof(int));
161  kh->kpstackpos = -1;
162}
163
164owl_keymap *owl_keyhandler_get_keymap(const owl_keyhandler *kh, const char *mapname)
165{
166  return owl_dict_find_element(&kh->keymaps, mapname);
167}
168
169/* free the list with owl_cmddict_namelist_free */
170void owl_keyhandler_get_keymap_names(const owl_keyhandler *kh, owl_list *l)
171{
172  owl_dict_get_keys(&kh->keymaps, l);
173}
174
175void owl_keyhandler_keymap_namelist_free(owl_list *l)
176{
177  owl_list_free_all(l, owl_free);
178}
179
180
181
182/* sets the active keymap, which will also reset any key state.
183 * returns the new keymap, or NULL on failure. */
184const owl_keymap *owl_keyhandler_activate(owl_keyhandler *kh, const char *mapname)
185{
186  const owl_keymap *km;
187  if (kh->active && !strcmp(mapname, kh->active->name)) return(kh->active);
188  km = owl_dict_find_element(&kh->keymaps, mapname);
189  if (!km) return(NULL);
190  owl_keyhandler_reset(kh);
191  kh->active = km;
192  return(km);
193}
194
195/* processes a keypress.  returns 0 if the keypress was handled,
196 * 1 if not handled, -1 on error, and -2 if j==ERR. */
197int owl_keyhandler_process(owl_keyhandler *kh, owl_input j)
198{
199  const owl_keymap     *km;
200  const owl_keybinding *kb;
201  int i, match;
202
203  if (!kh->active) {
204    owl_function_makemsg("No active keymap!!!");
205    return(-1);
206  }
207
208  /* deal with ESC prefixing */
209  if (!kh->in_esc && j.ch == 27) {
210    kh->in_esc = 1;
211    return(0);
212  }
213  if (kh->in_esc) {
214    j.ch = OWL_META(j.ch);
215    kh->in_esc = 0;
216  }
217 
218  kh->kpstack[++(kh->kpstackpos)] = j.ch;
219  if (kh->kpstackpos >= OWL_KEYMAP_MAXSTACK) {
220    owl_keyhandler_reset(kh);
221    owl_function_makemsg("Too many prefix keys pressed...");
222    return(-1);
223  }
224
225  /* deal with the always_fn for the map and submaps */
226  for (km=kh->active; km; km=km->submap) {
227    if (km->prealways_fn) {
228      km->prealways_fn(j);
229    }
230  }
231
232  /* search for a match.  goes through active keymap and then
233   * through submaps... TODO:  clean this up so we can pull
234   * keyhandler and keymap apart.  */
235  for (km=kh->active; km; km=km->submap) {
236    for (i=owl_list_get_size(&km->bindings)-1; i>=0; i--) {
237      kb = owl_list_get_element(&km->bindings, i);
238      match = owl_keybinding_match(kb, kh);
239      if (match == 1) {         /* subset match */
240
241        /* owl_function_debugmsg("processkey: found subset match in %s", km->name); */
242        return(0);
243      } else if (match == 2) {  /* exact match */
244        /* owl_function_debugmsg("processkey: found exact match in %s", km->name); */
245        owl_keybinding_execute(kb, j.ch);
246        owl_keyhandler_reset(kh);
247        if (km->postalways_fn) {
248          km->postalways_fn(j);
249        }
250        return(0);
251      }
252    }
253  }
254
255  /* see if a default action exists for the active keymap */
256  if (kh->active->default_fn && kh->kpstackpos<1) {
257    /*owl_function_debugmsg("processkey: executing default_fn");*/
258
259    kh->active->default_fn(j);
260    owl_keyhandler_reset(kh);
261    if (kh->active->postalways_fn) {
262      kh->active->postalways_fn(j);
263    }
264    return(0);
265  }
266
267  owl_keyhandler_invalidkey(kh);
268  /* unable to handle */
269  return(1); 
270}
271
272void owl_keyhandler_invalidkey(owl_keyhandler *kh)
273{
274    char kbbuff[500];
275    owl_keybinding_stack_tostring(kh->kpstack, kh->kpstackpos+1, kbbuff, 500);
276    owl_function_makemsg("'%s' is not a valid key in this context.", kbbuff);
277    owl_keyhandler_reset(kh);
278}
Note: See TracBrowser for help on using the repository browser.