[7d4fbcd] | 1 | #include <string.h> |
---|
| 2 | #include "owl.h" |
---|
| 3 | |
---|
| 4 | /* returns 0 on success */ |
---|
[e19eb97] | 5 | int 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] | 6 | { |
---|
[7d4fbcd] | 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! */ |
---|
[0bdb3a6] | 19 | void owl_keymap_cleanup(owl_keymap *km) |
---|
[cf83b7a] | 20 | { |
---|
[7d4fbcd] | 21 | owl_free(km->name); |
---|
| 22 | owl_free(km->desc); |
---|
[920201c] | 23 | owl_list_cleanup(&km->bindings, (void (*)(void *))owl_keybinding_delete); |
---|
[7d4fbcd] | 24 | } |
---|
| 25 | |
---|
[afa200a] | 26 | void owl_keymap_set_submap(owl_keymap *km, const owl_keymap *submap) |
---|
[cf83b7a] | 27 | { |
---|
[7d4fbcd] | 28 | km->submap = submap; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | /* creates and adds a key binding */ |
---|
[e19eb97] | 32 | int owl_keymap_create_binding(owl_keymap *km, const char *keyseq, const char *command, void (*function_fn)(void), const char *desc) |
---|
[cf83b7a] | 33 | { |
---|
[7d4fbcd] | 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--) { |
---|
[4d86e06] | 46 | curkb = owl_list_get_element(&km->bindings, i); |
---|
[7d4fbcd] | 47 | if (owl_keybinding_equal(curkb, kb)) { |
---|
| 48 | owl_list_remove_element(&km->bindings, i); |
---|
[920201c] | 49 | owl_keybinding_delete(curkb); |
---|
[7d4fbcd] | 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. */ |
---|
[afa200a] | 56 | char *owl_keymap_summary(const owl_keymap *km) |
---|
[cf83b7a] | 57 | { |
---|
[7d4fbcd] | 58 | if (!km || !km->name || !km->desc) return NULL; |
---|
[36486be] | 59 | return owl_sprintf("%-15s - %s", km->name, km->desc); |
---|
[7d4fbcd] | 60 | } |
---|
| 61 | |
---|
| 62 | /* Appends details about the keymap to fm */ |
---|
[afa200a] | 63 | void owl_keymap_get_details(const owl_keymap *km, owl_fmtext *fm) |
---|
[cf83b7a] | 64 | { |
---|
[7d4fbcd] | 65 | int i, nbindings; |
---|
[f1d7d0f] | 66 | const owl_keybinding *kb; |
---|
[7d4fbcd] | 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]; |
---|
[0a0fb74] | 99 | const owl_cmd *cmd; |
---|
[e19eb97] | 100 | const char *tmpdesc, *desc = ""; |
---|
[7d4fbcd] | 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 | |
---|
[cf83b7a] | 132 | int owl_keyhandler_init(owl_keyhandler *kh) |
---|
| 133 | { |
---|
[7d4fbcd] | 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 */ |
---|
[cf83b7a] | 141 | void owl_keyhandler_add_keymap(owl_keyhandler *kh, owl_keymap *km) |
---|
| 142 | { |
---|
[7d4fbcd] | 143 | owl_dict_insert_element(&kh->keymaps, km->name, km, NULL); |
---|
| 144 | } |
---|
| 145 | |
---|
[e19eb97] | 146 | owl_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] | 147 | { |
---|
[7d4fbcd] | 148 | owl_keymap *km; |
---|
[4d86e06] | 149 | km = owl_malloc(sizeof(owl_keymap)); |
---|
[7d4fbcd] | 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 */ |
---|
[cf83b7a] | 157 | void owl_keyhandler_reset(owl_keyhandler *kh) |
---|
| 158 | { |
---|
[7d4fbcd] | 159 | kh->in_esc = 0; |
---|
| 160 | memset(kh->kpstack, 0, (OWL_KEYMAP_MAXSTACK+1)*sizeof(int)); |
---|
| 161 | kh->kpstackpos = -1; |
---|
| 162 | } |
---|
| 163 | |
---|
[12bc46a] | 164 | owl_keymap *owl_keyhandler_get_keymap(const owl_keyhandler *kh, const char *mapname) |
---|
[cf83b7a] | 165 | { |
---|
[4d86e06] | 166 | return owl_dict_find_element(&kh->keymaps, mapname); |
---|
[7d4fbcd] | 167 | } |
---|
| 168 | |
---|
[8d4b521] | 169 | /* free the list with owl_cmddict_namelist_cleanup */ |
---|
[12bc46a] | 170 | void owl_keyhandler_get_keymap_names(const owl_keyhandler *kh, owl_list *l) |
---|
[cf83b7a] | 171 | { |
---|
[7d4fbcd] | 172 | owl_dict_get_keys(&kh->keymaps, l); |
---|
| 173 | } |
---|
| 174 | |
---|
[bb0d439] | 175 | void owl_keyhandler_keymap_namelist_cleanup(owl_list *l) |
---|
[cf83b7a] | 176 | { |
---|
[8c59178] | 177 | owl_list_cleanup(l, owl_free); |
---|
[7d4fbcd] | 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. */ |
---|
[afa200a] | 184 | const owl_keymap *owl_keyhandler_activate(owl_keyhandler *kh, const char *mapname) |
---|
[cf83b7a] | 185 | { |
---|
[afa200a] | 186 | const owl_keymap *km; |
---|
[7d4fbcd] | 187 | if (kh->active && !strcmp(mapname, kh->active->name)) return(kh->active); |
---|
[4d86e06] | 188 | km = owl_dict_find_element(&kh->keymaps, mapname); |
---|
[7d4fbcd] | 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. */ |
---|
[428834d] | 197 | int owl_keyhandler_process(owl_keyhandler *kh, owl_input j) |
---|
[cf83b7a] | 198 | { |
---|
[afa200a] | 199 | const owl_keymap *km; |
---|
[f1d7d0f] | 200 | const owl_keybinding *kb; |
---|
[7d4fbcd] | 201 | int i, match; |
---|
| 202 | |
---|
| 203 | if (!kh->active) { |
---|
| 204 | owl_function_makemsg("No active keymap!!!"); |
---|
| 205 | return(-1); |
---|
| 206 | } |
---|
[7131cf2] | 207 | |
---|
[7d4fbcd] | 208 | /* deal with ESC prefixing */ |
---|
[428834d] | 209 | if (!kh->in_esc && j.ch == 27) { |
---|
[7d4fbcd] | 210 | kh->in_esc = 1; |
---|
| 211 | return(0); |
---|
| 212 | } |
---|
| 213 | if (kh->in_esc) { |
---|
[428834d] | 214 | j.ch = OWL_META(j.ch); |
---|
[7d4fbcd] | 215 | kh->in_esc = 0; |
---|
| 216 | } |
---|
| 217 | |
---|
[428834d] | 218 | kh->kpstack[++(kh->kpstackpos)] = j.ch; |
---|
[7d4fbcd] | 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--) { |
---|
[4d86e06] | 237 | kb = owl_list_get_element(&km->bindings, i); |
---|
[e1b136bf] | 238 | match = owl_keybinding_match(kb, kh); |
---|
[7d4fbcd] | 239 | if (match == 1) { /* subset match */ |
---|
[10b866d] | 240 | |
---|
| 241 | /* owl_function_debugmsg("processkey: found subset match in %s", km->name); */ |
---|
[7d4fbcd] | 242 | return(0); |
---|
| 243 | } else if (match == 2) { /* exact match */ |
---|
[10b866d] | 244 | /* owl_function_debugmsg("processkey: found exact match in %s", km->name); */ |
---|
[428834d] | 245 | owl_keybinding_execute(kb, j.ch); |
---|
[7d4fbcd] | 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) { |
---|
[10b866d] | 257 | /*owl_function_debugmsg("processkey: executing default_fn");*/ |
---|
| 258 | |
---|
[7d4fbcd] | 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 | |
---|
[cf83b7a] | 272 | void owl_keyhandler_invalidkey(owl_keyhandler *kh) |
---|
| 273 | { |
---|
[7d4fbcd] | 274 | char kbbuff[500]; |
---|
[e1b136bf] | 275 | owl_keybinding_stack_tostring(kh->kpstack, kh->kpstackpos+1, kbbuff, 500); |
---|
[7d4fbcd] | 276 | owl_function_makemsg("'%s' is not a valid key in this context.", kbbuff); |
---|
| 277 | owl_keyhandler_reset(kh); |
---|
| 278 | } |
---|