source: keymap.c @ bf5e6a2

release-1.10release-1.8release-1.9
Last change on this file since bf5e6a2 was ce68f23, checked in by David Benjamin <davidben@mit.edu>, 13 years ago
Make owl_dict_get_keys return a GPtrArray Almost all the remaining uses of owl_list are tightly coupled into this one giant blob.
  • Property mode set to 100644
File size: 8.9 KB
RevLine 
[7d4fbcd]1#include <string.h>
2#include "owl.h"
3
[13ebf92]4static void _owl_keymap_format_bindings(const owl_keymap *km, owl_fmtext *fm);
[44cc9ab]5static void _owl_keymap_format_with_parents(const owl_keymap *km, owl_fmtext *fm);
[13ebf92]6
[7d4fbcd]7/* returns 0 on success */
[e19eb97]8int 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))
[cf83b7a]9{
[7d4fbcd]10  if (!name || !desc) return(-1);
[fda61d3]11  km->name = g_strdup(name);
12  km->desc = g_strdup(desc);
[e4d7cb6]13  km->bindings = g_ptr_array_new();
[44cc9ab]14  km->parent = NULL;
[7d4fbcd]15  km->default_fn = default_fn;
16  km->prealways_fn = prealways_fn;
17  km->postalways_fn = postalways_fn;
18  return(0);
19}
20
21/* note that this will free the memory for the bindings! */
[0bdb3a6]22void owl_keymap_cleanup(owl_keymap *km)
[cf83b7a]23{
[ddbbcffa]24  g_free(km->name);
25  g_free(km->desc);
[3cdd6d2]26  owl_ptr_array_free(km->bindings, (GDestroyNotify)owl_keybinding_delete);
[7d4fbcd]27}
28
[44cc9ab]29void owl_keymap_set_parent(owl_keymap *km, const owl_keymap *parent)
[cf83b7a]30{
[44cc9ab]31  km->parent = parent;
[7d4fbcd]32}
33
34/* creates and adds a key binding */
[e19eb97]35int owl_keymap_create_binding(owl_keymap *km, const char *keyseq, const char *command, void (*function_fn)(void), const char *desc)
[cf83b7a]36{
[e4d7cb6]37  owl_keybinding *kb;
[7d4fbcd]38  int i;
39
[b13daa0]40  kb = owl_keybinding_new(keyseq, command, function_fn, desc);
41  if (kb == NULL)
42    return -1;
[7d4fbcd]43  /* see if another matching binding, and if so remove it.
44   * otherwise just add this one.
45   */
[e4d7cb6]46  for (i = km->bindings->len-1; i >= 0; i--) {
47    if (owl_keybinding_equal(km->bindings->pdata[i], kb)) {
48      owl_keybinding_delete(g_ptr_array_remove_index(km->bindings, i));
[7d4fbcd]49    }
50  }
[e4d7cb6]51  g_ptr_array_add(km->bindings, kb);
[d427f08]52  return 0;
[7d4fbcd]53}
54
[8a921b5]55/* removes the binding associated with the keymap */
56int owl_keymap_remove_binding(owl_keymap *km, const char *keyseq)
57{
[e4d7cb6]58  owl_keybinding *kb;
[8a921b5]59  int i;
60
[b13daa0]61  kb = owl_keybinding_new(keyseq, NULL, NULL, NULL);
62  if (kb == NULL)
63    return -1;
[8a921b5]64
[e4d7cb6]65  for (i = km->bindings->len-1; i >= 0; i--) {
66    if (owl_keybinding_equal(km->bindings->pdata[i], kb)) {
67      owl_keybinding_delete(g_ptr_array_remove_index(km->bindings, i));
[b13daa0]68      owl_keybinding_delete(kb);
[8a921b5]69      return(0);
70    }
71  }
[b13daa0]72  owl_keybinding_delete(kb);
[8a921b5]73  return(-2);
74}
75
76
[7d4fbcd]77/* returns a summary line describing this keymap.  the caller must free. */
[6829afc]78CALLER_OWN char *owl_keymap_summary(const owl_keymap *km)
[cf83b7a]79{
[7d4fbcd]80  if (!km || !km->name || !km->desc) return NULL;
[3472845]81  return g_strdup_printf("%-15s - %s", km->name, km->desc);
[7d4fbcd]82}
83
84/* Appends details about the keymap to fm */
[13ebf92]85void owl_keymap_get_details(const owl_keymap *km, owl_fmtext *fm, int recurse)
[cf83b7a]86{
[7d4fbcd]87  owl_fmtext_append_bold(fm, "KEYMAP - ");
88  owl_fmtext_append_bold(fm, km->name);
89  owl_fmtext_append_normal(fm, "\n");
90  if (km->desc) {
91    owl_fmtext_append_normal(fm, OWL_TABSTR "Purpose:    ");
92    owl_fmtext_append_normal(fm, km->desc);
93    owl_fmtext_append_normal(fm, "\n");
94  }
[44cc9ab]95  if (km->parent) {
96    owl_fmtext_append_normal(fm, OWL_TABSTR "Has parent: ");
97    owl_fmtext_append_normal(fm, km->parent->name);
[7d4fbcd]98    owl_fmtext_append_normal(fm, "\n");
99  }
100    owl_fmtext_append_normal(fm, "\n");
101  if (km->default_fn) {
102    owl_fmtext_append_normal(fm, OWL_TABSTR
103     "Has a default keypress handler (default_fn).\n");
104  }
105  if (km->prealways_fn) {
106    owl_fmtext_append_normal(fm, OWL_TABSTR
107     "Executes a function (prealways_fn) on every keypress.\n");
108  }
109  if (km->postalways_fn) {
110    owl_fmtext_append_normal(fm, OWL_TABSTR
111     "Executes a function (postalways_fn) after handling every keypress.\n");
112  }
113
114  owl_fmtext_append_bold(fm, "\nKey bindings:\n\n"); 
[13ebf92]115  if (recurse) {
[44cc9ab]116    _owl_keymap_format_with_parents(km, fm);
[13ebf92]117  } else {
118    _owl_keymap_format_bindings(km, fm);
119  }
120}
121
[44cc9ab]122static void _owl_keymap_format_with_parents(const owl_keymap *km, owl_fmtext *fm)
[13ebf92]123{
124  while (km) {
125    _owl_keymap_format_bindings(km, fm);
[44cc9ab]126    km = km->parent;
[13ebf92]127    if (km) {
128      owl_fmtext_append_bold(fm, "\nInherited from ");
129      owl_fmtext_append_bold(fm, km->name);
130      owl_fmtext_append_bold(fm, ":\n\n");
131    }
132  }
133}
134
135static void _owl_keymap_format_bindings(const owl_keymap *km, owl_fmtext *fm)
136{
[e4d7cb6]137  int i;
[13ebf92]138  const owl_keybinding *kb;
139 
[e4d7cb6]140  for (i = 0; i < km->bindings->len; i++) {
[45e2c95]141    char *kbstr;
[0a0fb74]142    const owl_cmd *cmd;
[e19eb97]143    const char *tmpdesc, *desc = "";
[7d4fbcd]144
[e4d7cb6]145    kb = km->bindings->pdata[i];
[45e2c95]146    kbstr = owl_keybinding_tostring(kb);
[7d4fbcd]147    owl_fmtext_append_normal(fm, OWL_TABSTR);
[45e2c95]148    owl_fmtext_append_normal(fm, kbstr);
149    owl_fmtext_append_spaces(fm, 11-strlen(kbstr));
150    g_free(kbstr);
[7d4fbcd]151    owl_fmtext_append_normal(fm, " - ");
152    if (kb->desc && *kb->desc) {
153      desc = kb->desc;
154    } else if ((cmd=owl_function_get_cmd(kb->command))
155               && (tmpdesc = owl_cmd_get_summary(cmd))) {
156      desc = tmpdesc;
157    }
158    owl_fmtext_append_normal(fm, desc);
159    if (kb->command && *(kb->command)) {
160      owl_fmtext_append_normal(fm, "   [");
161      owl_fmtext_append_normal(fm, kb->command);
162      owl_fmtext_append_normal(fm, "]");
163    } 
164    owl_fmtext_append_normal(fm, "\n");
165  }
166}
167
168
169
170/***********************************************************************/
171/***************************** KEYHANDLER ******************************/
172/***********************************************************************/
173
174/* NOTE: keyhandler has private access to the internals of keymap */
175
[4c7c21f]176void owl_keyhandler_init(owl_keyhandler *kh)
[cf83b7a]177{
[4c7c21f]178  owl_dict_create(&kh->keymaps);
[7d4fbcd]179  kh->active = NULL;
180  owl_keyhandler_reset(kh);
181}
182
183/* adds a new keymap */
[cf83b7a]184void owl_keyhandler_add_keymap(owl_keyhandler *kh, owl_keymap *km)
185{
[7d4fbcd]186  owl_dict_insert_element(&kh->keymaps, km->name, km, NULL);
187}
188
[e19eb97]189owl_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))
[cf83b7a]190{
[7d4fbcd]191  owl_keymap *km;
[96828e4]192  km = g_new(owl_keymap, 1);
[7d4fbcd]193  owl_keymap_init(km, name, desc, default_fn, prealways_fn, postalways_fn);
194  owl_keyhandler_add_keymap(kh, km);
195  return km;
196}
197
198/* resets state and clears out key stack */
[cf83b7a]199void owl_keyhandler_reset(owl_keyhandler *kh)
200{
[7d4fbcd]201  kh->in_esc = 0;
202  memset(kh->kpstack, 0, (OWL_KEYMAP_MAXSTACK+1)*sizeof(int));
203  kh->kpstackpos = -1;
204}
205
[12bc46a]206owl_keymap *owl_keyhandler_get_keymap(const owl_keyhandler *kh, const char *mapname)
[cf83b7a]207{
[4d86e06]208  return owl_dict_find_element(&kh->keymaps, mapname);
[7d4fbcd]209}
210
[ce68f23]211CALLER_OWN GPtrArray *owl_keyhandler_get_keymap_names(const owl_keyhandler *kh)
[cf83b7a]212{
[ce68f23]213  return owl_dict_get_keys(&kh->keymaps);
[7d4fbcd]214}
215
216
217/* sets the active keymap, which will also reset any key state.
218 * returns the new keymap, or NULL on failure. */
[afa200a]219const owl_keymap *owl_keyhandler_activate(owl_keyhandler *kh, const char *mapname)
[cf83b7a]220{
[afa200a]221  const owl_keymap *km;
[7d4fbcd]222  if (kh->active && !strcmp(mapname, kh->active->name)) return(kh->active);
[4d86e06]223  km = owl_dict_find_element(&kh->keymaps, mapname);
[7d4fbcd]224  if (!km) return(NULL);
225  owl_keyhandler_reset(kh);
226  kh->active = km;
227  return(km);
228}
229
230/* processes a keypress.  returns 0 if the keypress was handled,
231 * 1 if not handled, -1 on error, and -2 if j==ERR. */
[428834d]232int owl_keyhandler_process(owl_keyhandler *kh, owl_input j)
[cf83b7a]233{
[afa200a]234  const owl_keymap     *km;
[f1d7d0f]235  const owl_keybinding *kb;
[7d4fbcd]236  int i, match;
237
238  if (!kh->active) {
239    owl_function_makemsg("No active keymap!!!");
240    return(-1);
241  }
[7131cf2]242
[7d4fbcd]243  /* deal with ESC prefixing */
[428834d]244  if (!kh->in_esc && j.ch == 27) {
[7d4fbcd]245    kh->in_esc = 1;
246    return(0);
247  }
248  if (kh->in_esc) {
[428834d]249    j.ch = OWL_META(j.ch);
[7d4fbcd]250    kh->in_esc = 0;
251  }
252 
[428834d]253  kh->kpstack[++(kh->kpstackpos)] = j.ch;
[7d4fbcd]254  if (kh->kpstackpos >= OWL_KEYMAP_MAXSTACK) {
255    owl_keyhandler_reset(kh);
256    owl_function_makemsg("Too many prefix keys pressed...");
257    return(-1);
258  }
259
[44cc9ab]260  /* deal with the always_fn for the map and parents */
261  for (km=kh->active; km; km=km->parent) {
[7d4fbcd]262    if (km->prealways_fn) {
263      km->prealways_fn(j);
264    }
265  }
266
267  /* search for a match.  goes through active keymap and then
[44cc9ab]268   * through parents... TODO:  clean this up so we can pull
[7d4fbcd]269   * keyhandler and keymap apart.  */
[44cc9ab]270  for (km=kh->active; km; km=km->parent) {
[e4d7cb6]271    for (i = km->bindings->len-1; i >= 0; i--) {
272      kb = km->bindings->pdata[i];
[e1b136bf]273      match = owl_keybinding_match(kb, kh);
[7d4fbcd]274      if (match == 1) {         /* subset match */
[10b866d]275
276        /* owl_function_debugmsg("processkey: found subset match in %s", km->name); */
[7d4fbcd]277        return(0);
278      } else if (match == 2) {  /* exact match */
[10b866d]279        /* owl_function_debugmsg("processkey: found exact match in %s", km->name); */
[428834d]280        owl_keybinding_execute(kb, j.ch);
[7d4fbcd]281        owl_keyhandler_reset(kh);
282        if (km->postalways_fn) {
283          km->postalways_fn(j);
284        }
285        return(0);
286      }
287    }
288  }
289
290  /* see if a default action exists for the active keymap */
291  if (kh->active->default_fn && kh->kpstackpos<1) {
[10b866d]292    /*owl_function_debugmsg("processkey: executing default_fn");*/
293
[7d4fbcd]294    kh->active->default_fn(j);
295    owl_keyhandler_reset(kh);
296    if (kh->active->postalways_fn) {
297      kh->active->postalways_fn(j);
298    }
299    return(0);
300  }
301
302  owl_keyhandler_invalidkey(kh);
303  /* unable to handle */
304  return(1); 
305}
306
[cf83b7a]307void owl_keyhandler_invalidkey(owl_keyhandler *kh)
308{
[45e2c95]309    char *kbbuff = owl_keybinding_stack_tostring(kh->kpstack, kh->kpstackpos+1);
[7d4fbcd]310    owl_function_makemsg("'%s' is not a valid key in this context.", kbbuff);
[45e2c95]311    g_free(kbbuff);
[7d4fbcd]312    owl_keyhandler_reset(kh);
313}
Note: See TracBrowser for help on using the repository browser.