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