[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 | |
---|
| 13 | /* sets up a new keybinding for a command */ |
---|
[e19eb97] | 14 | int owl_keybinding_init(owl_keybinding *kb, const char *keyseq, const char *command, void (*function_fn)(void), const char *desc) |
---|
[cf83b7a] | 15 | { |
---|
[176d3443] | 16 | owl_function_debugmsg("owl_keybinding_init: creating binding for <%s> with desc: <%s>", keyseq, desc); |
---|
[7d4fbcd] | 17 | if (command && !function_fn) { |
---|
| 18 | kb->type = OWL_KEYBINDING_COMMAND; |
---|
| 19 | } else if (!command && function_fn) { |
---|
| 20 | kb->type = OWL_KEYBINDING_FUNCTION; |
---|
| 21 | } else { |
---|
| 22 | return(-1); |
---|
| 23 | } |
---|
| 24 | |
---|
[5643f99] | 25 | if (owl_keybinding_make_keys(kb, keyseq) != 0) { |
---|
| 26 | return(-1); |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | if (command) kb->command = owl_strdup(command); |
---|
| 30 | kb->function_fn = function_fn; |
---|
| 31 | if (desc) kb->desc = owl_strdup(desc); |
---|
| 32 | else kb->desc = NULL; |
---|
| 33 | return(0); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq) |
---|
| 37 | { |
---|
| 38 | char **ktokens; |
---|
| 39 | int nktokens, i; |
---|
| 40 | |
---|
[d275eb2] | 41 | ktokens = g_strsplit_set(keyseq, " ", 0); |
---|
| 42 | nktokens = g_strv_length(ktokens); |
---|
| 43 | if (nktokens < 1 || nktokens > OWL_KEYMAP_MAXSTACK) { |
---|
| 44 | g_strfreev(ktokens); |
---|
[7d4fbcd] | 45 | return(-1); |
---|
| 46 | } |
---|
[e1b136bf] | 47 | kb->keys = owl_malloc(nktokens*sizeof(int)); |
---|
[8a921b5] | 48 | for (i=0; i<nktokens; i++) { |
---|
[e1b136bf] | 49 | kb->keys[i] = owl_keypress_fromstring(ktokens[i]); |
---|
[d275eb2] | 50 | if (kb->keys[i] == ERR) { |
---|
| 51 | g_strfreev(ktokens); |
---|
[e1b136bf] | 52 | owl_free(kb->keys); |
---|
[7d4fbcd] | 53 | return(-1); |
---|
| 54 | } |
---|
| 55 | } |
---|
[e1b136bf] | 56 | kb->len = nktokens; |
---|
[d275eb2] | 57 | g_strfreev(ktokens); |
---|
[7d4fbcd] | 58 | return(0); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /* Releases data associated with a keybinding */ |
---|
[b646d00] | 62 | void owl_keybinding_cleanup(owl_keybinding *kb) |
---|
[cf83b7a] | 63 | { |
---|
[e1b136bf] | 64 | if (kb->keys) owl_free(kb->keys); |
---|
[7d4fbcd] | 65 | if (kb->desc) owl_free(kb->desc); |
---|
| 66 | if (kb->command) owl_free(kb->command); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | /* Releases data associated with a keybinding, and the kb itself */ |
---|
[920201c] | 70 | void owl_keybinding_delete(owl_keybinding *kb) |
---|
[cf83b7a] | 71 | { |
---|
[b646d00] | 72 | owl_keybinding_cleanup(kb); |
---|
[7d4fbcd] | 73 | owl_free(kb); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /* executes a keybinding */ |
---|
[f1d7d0f] | 77 | void owl_keybinding_execute(const owl_keybinding *kb, int j) |
---|
[cf83b7a] | 78 | { |
---|
[7d4fbcd] | 79 | if (kb->type == OWL_KEYBINDING_COMMAND && kb->command) { |
---|
| 80 | owl_function_command_norv(kb->command); |
---|
| 81 | } else if (kb->type == OWL_KEYBINDING_FUNCTION && kb->function_fn) { |
---|
| 82 | kb->function_fn(); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /* returns 0 on success */ |
---|
[e1b136bf] | 87 | int owl_keybinding_stack_tostring(int *j, int len, char *buff, int bufflen) |
---|
[cf83b7a] | 88 | { |
---|
[7d4fbcd] | 89 | char *pos = buff; |
---|
| 90 | int rem = bufflen; |
---|
| 91 | int i, n; |
---|
| 92 | |
---|
[e1b136bf] | 93 | for (i=0; i < len; i++) { |
---|
[7d4fbcd] | 94 | owl_keypress_tostring(j[i], 0, pos, rem-1); |
---|
[e1b136bf] | 95 | if (i < len - 1) strcat(pos, " "); |
---|
[7d4fbcd] | 96 | n = strlen(pos); |
---|
| 97 | pos += n; |
---|
| 98 | rem -= n; |
---|
| 99 | } |
---|
| 100 | return 0; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /* returns 0 on success */ |
---|
[f1d7d0f] | 104 | int owl_keybinding_tostring(const owl_keybinding *kb, char *buff, int bufflen) |
---|
[cf83b7a] | 105 | { |
---|
[e1b136bf] | 106 | return owl_keybinding_stack_tostring(kb->keys, kb->len, buff, bufflen); |
---|
[7d4fbcd] | 107 | } |
---|
| 108 | |
---|
[f1d7d0f] | 109 | const char *owl_keybinding_get_desc(const owl_keybinding *kb) |
---|
[cf83b7a] | 110 | { |
---|
[7d4fbcd] | 111 | return kb->desc; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /* returns 0 on no match, 1 on subset match, and 2 on complete match */ |
---|
[12bc46a] | 115 | int owl_keybinding_match(const owl_keybinding *kb, const owl_keyhandler *kh) |
---|
[cf83b7a] | 116 | { |
---|
[e1b136bf] | 117 | int i; |
---|
| 118 | for(i = 0; i <= kh->kpstackpos && i < kb->len; i++) { |
---|
| 119 | if(kb->keys[i] != kh->kpstack[i]) |
---|
[7d4fbcd] | 120 | return 0; |
---|
| 121 | } |
---|
[e1b136bf] | 122 | |
---|
| 123 | /* If we've made it to this point, then they match as far as they are. */ |
---|
| 124 | if(kb->len == kh->kpstackpos + 1) { |
---|
| 125 | /* Equal length */ |
---|
[7d4fbcd] | 126 | return 2; |
---|
[e1b136bf] | 127 | } else if(kb->len > kh->kpstackpos + 1) { |
---|
[7d4fbcd] | 128 | return 1; |
---|
| 129 | } |
---|
[e1b136bf] | 130 | |
---|
| 131 | return 0; |
---|
[7d4fbcd] | 132 | } |
---|
| 133 | |
---|
| 134 | /* returns 1 if keypress sequence is the same */ |
---|
[f1d7d0f] | 135 | int owl_keybinding_equal(const owl_keybinding *kb1, const owl_keybinding *kb2) |
---|
[cf83b7a] | 136 | { |
---|
[e1b136bf] | 137 | int i; |
---|
| 138 | |
---|
| 139 | if(kb1->len != kb2->len) return 0; |
---|
| 140 | |
---|
| 141 | for(i = 0; i < kb1->len; i++) { |
---|
| 142 | if(kb1->keys[i] != kb2->keys[i]) |
---|
| 143 | return 0; |
---|
[7d4fbcd] | 144 | } |
---|
[e1b136bf] | 145 | |
---|
| 146 | return 1; |
---|
[7d4fbcd] | 147 | } |
---|