[7d4fbcd] | 1 | #include <ctype.h> |
---|
| 2 | #include <string.h> |
---|
| 3 | #include "owl.h" |
---|
| 4 | |
---|
| 5 | /* |
---|
| 6 | * TODO: Idea for allowing functions to be user-specified --- |
---|
| 7 | * Have function have a context bitmask that says where it |
---|
| 8 | * can be used, and have keymaps also have one, and compare |
---|
| 9 | * the two when setting. |
---|
| 10 | * |
---|
| 11 | */ |
---|
| 12 | |
---|
[b13daa0] | 13 | static int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq); |
---|
| 14 | |
---|
[7d4fbcd] | 15 | /* sets up a new keybinding for a command */ |
---|
[b13daa0] | 16 | owl_keybinding *owl_keybinding_new(const char *keyseq, const char *command, void (*function_fn)(void), const char *desc) |
---|
[cf83b7a] | 17 | { |
---|
[b13daa0] | 18 | owl_keybinding *kb = g_new(owl_keybinding, 1); |
---|
| 19 | |
---|
[176d3443] | 20 | owl_function_debugmsg("owl_keybinding_init: creating binding for <%s> with desc: <%s>", keyseq, desc); |
---|
[b13daa0] | 21 | if (command && function_fn) { |
---|
| 22 | g_free(kb); |
---|
| 23 | return NULL; |
---|
| 24 | } else if (command && !function_fn) { |
---|
[7d4fbcd] | 25 | kb->type = OWL_KEYBINDING_COMMAND; |
---|
| 26 | } else if (!command && function_fn) { |
---|
| 27 | kb->type = OWL_KEYBINDING_FUNCTION; |
---|
| 28 | } else { |
---|
[b13daa0] | 29 | kb->type = OWL_KEYBINDING_NOOP; |
---|
[7d4fbcd] | 30 | } |
---|
| 31 | |
---|
[5643f99] | 32 | if (owl_keybinding_make_keys(kb, keyseq) != 0) { |
---|
[b13daa0] | 33 | g_free(kb); |
---|
| 34 | return NULL; |
---|
[5643f99] | 35 | } |
---|
| 36 | |
---|
[b13daa0] | 37 | kb->command = g_strdup(command); |
---|
[5643f99] | 38 | kb->function_fn = function_fn; |
---|
[b13daa0] | 39 | kb->desc = g_strdup(desc); |
---|
| 40 | return kb; |
---|
[5643f99] | 41 | } |
---|
| 42 | |
---|
[b13daa0] | 43 | static int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq) |
---|
[5643f99] | 44 | { |
---|
| 45 | char **ktokens; |
---|
| 46 | int nktokens, i; |
---|
| 47 | |
---|
[d275eb2] | 48 | ktokens = g_strsplit_set(keyseq, " ", 0); |
---|
| 49 | nktokens = g_strv_length(ktokens); |
---|
| 50 | if (nktokens < 1 || nktokens > OWL_KEYMAP_MAXSTACK) { |
---|
| 51 | g_strfreev(ktokens); |
---|
[7d4fbcd] | 52 | return(-1); |
---|
| 53 | } |
---|
[96828e4] | 54 | kb->keys = g_new(int, nktokens); |
---|
[8a921b5] | 55 | for (i=0; i<nktokens; i++) { |
---|
[e1b136bf] | 56 | kb->keys[i] = owl_keypress_fromstring(ktokens[i]); |
---|
[d275eb2] | 57 | if (kb->keys[i] == ERR) { |
---|
| 58 | g_strfreev(ktokens); |
---|
[ddbbcffa] | 59 | g_free(kb->keys); |
---|
[7d4fbcd] | 60 | return(-1); |
---|
| 61 | } |
---|
| 62 | } |
---|
[e1b136bf] | 63 | kb->len = nktokens; |
---|
[d275eb2] | 64 | g_strfreev(ktokens); |
---|
[7d4fbcd] | 65 | return(0); |
---|
| 66 | } |
---|
| 67 | |
---|
[b13daa0] | 68 | /* Releases data associated with a keybinding, and the kb itself */ |
---|
| 69 | void owl_keybinding_delete(owl_keybinding *kb) |
---|
[cf83b7a] | 70 | { |
---|
[ddbbcffa] | 71 | if (kb->keys) g_free(kb->keys); |
---|
| 72 | if (kb->desc) g_free(kb->desc); |
---|
| 73 | if (kb->command) g_free(kb->command); |
---|
| 74 | g_free(kb); |
---|
[7d4fbcd] | 75 | } |
---|
| 76 | |
---|
| 77 | /* executes a keybinding */ |
---|
[f1d7d0f] | 78 | void owl_keybinding_execute(const owl_keybinding *kb, int j) |
---|
[cf83b7a] | 79 | { |
---|
[7d4fbcd] | 80 | if (kb->type == OWL_KEYBINDING_COMMAND && kb->command) { |
---|
| 81 | owl_function_command_norv(kb->command); |
---|
| 82 | } else if (kb->type == OWL_KEYBINDING_FUNCTION && kb->function_fn) { |
---|
| 83 | kb->function_fn(); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
[45e2c95] | 87 | char *owl_keybinding_stack_tostring(int *j, int len) |
---|
[cf83b7a] | 88 | { |
---|
[45e2c95] | 89 | GString *string; |
---|
| 90 | int i; |
---|
| 91 | |
---|
| 92 | string = g_string_new(""); |
---|
| 93 | for (i = 0; i < len; i++) { |
---|
[d07af84] | 94 | char *keypress = owl_keypress_tostring(j[i], 0); |
---|
| 95 | g_string_append(string, keypress ? keypress : "INVALID"); |
---|
| 96 | g_free(keypress); |
---|
[45e2c95] | 97 | if (i < len - 1) g_string_append_c(string, ' '); |
---|
[7d4fbcd] | 98 | } |
---|
[45e2c95] | 99 | return g_string_free(string, false); |
---|
[7d4fbcd] | 100 | } |
---|
| 101 | |
---|
[45e2c95] | 102 | char *owl_keybinding_tostring(const owl_keybinding *kb) |
---|
[cf83b7a] | 103 | { |
---|
[45e2c95] | 104 | return owl_keybinding_stack_tostring(kb->keys, kb->len); |
---|
[7d4fbcd] | 105 | } |
---|
| 106 | |
---|
[f1d7d0f] | 107 | const char *owl_keybinding_get_desc(const owl_keybinding *kb) |
---|
[cf83b7a] | 108 | { |
---|
[7d4fbcd] | 109 | return kb->desc; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /* returns 0 on no match, 1 on subset match, and 2 on complete match */ |
---|
[12bc46a] | 113 | int owl_keybinding_match(const owl_keybinding *kb, const owl_keyhandler *kh) |
---|
[cf83b7a] | 114 | { |
---|
[e1b136bf] | 115 | int i; |
---|
| 116 | for(i = 0; i <= kh->kpstackpos && i < kb->len; i++) { |
---|
| 117 | if(kb->keys[i] != kh->kpstack[i]) |
---|
[7d4fbcd] | 118 | return 0; |
---|
| 119 | } |
---|
[e1b136bf] | 120 | |
---|
| 121 | /* If we've made it to this point, then they match as far as they are. */ |
---|
| 122 | if(kb->len == kh->kpstackpos + 1) { |
---|
| 123 | /* Equal length */ |
---|
[7d4fbcd] | 124 | return 2; |
---|
[e1b136bf] | 125 | } else if(kb->len > kh->kpstackpos + 1) { |
---|
[7d4fbcd] | 126 | return 1; |
---|
| 127 | } |
---|
[e1b136bf] | 128 | |
---|
| 129 | return 0; |
---|
[7d4fbcd] | 130 | } |
---|
| 131 | |
---|
| 132 | /* returns 1 if keypress sequence is the same */ |
---|
[f1d7d0f] | 133 | int owl_keybinding_equal(const owl_keybinding *kb1, const owl_keybinding *kb2) |
---|
[cf83b7a] | 134 | { |
---|
[e1b136bf] | 135 | int i; |
---|
| 136 | |
---|
| 137 | if(kb1->len != kb2->len) return 0; |
---|
| 138 | |
---|
| 139 | for(i = 0; i < kb1->len; i++) { |
---|
| 140 | if(kb1->keys[i] != kb2->keys[i]) |
---|
| 141 | return 0; |
---|
[7d4fbcd] | 142 | } |
---|
[e1b136bf] | 143 | |
---|
| 144 | return 1; |
---|
[7d4fbcd] | 145 | } |
---|