1 | /* Copyright (c) 2002,2003,2004,2009 James M. Kretchmar |
---|
2 | * |
---|
3 | * This file is part of Owl. |
---|
4 | * |
---|
5 | * Owl is free software: you can redistribute it and/or modify |
---|
6 | * it under the terms of the GNU General Public License as published by |
---|
7 | * the Free Software Foundation, either version 3 of the License, or |
---|
8 | * (at your option) any later version. |
---|
9 | * |
---|
10 | * Owl is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with Owl. If not, see <http://www.gnu.org/licenses/>. |
---|
17 | * |
---|
18 | * --------------------------------------------------------------- |
---|
19 | * |
---|
20 | * As of Owl version 2.1.12 there are patches contributed by |
---|
21 | * developers of the the branched BarnOwl project, Copyright (c) |
---|
22 | * 2006-2008 The BarnOwl Developers. All rights reserved. |
---|
23 | */ |
---|
24 | |
---|
25 | #include <ctype.h> |
---|
26 | #include <string.h> |
---|
27 | #include "owl.h" |
---|
28 | |
---|
29 | static const char fileIdent[] = "$Id$"; |
---|
30 | |
---|
31 | /* |
---|
32 | * TODO: Idea for allowing functions to be user-specified --- |
---|
33 | * Have function have a context bitmask that says where it |
---|
34 | * can be used, and have keymaps also have one, and compare |
---|
35 | * the two when setting. |
---|
36 | * |
---|
37 | */ |
---|
38 | |
---|
39 | /* sets up a new keybinding for a command */ |
---|
40 | int owl_keybinding_init(owl_keybinding *kb, char *keyseq, char *command, void (*function_fn)(void), char *desc) |
---|
41 | { |
---|
42 | char **ktokens; |
---|
43 | int nktokens, i; |
---|
44 | |
---|
45 | owl_function_debugmsg("owl_keybinding_init: creating binding for <%s> with desc: <%s>", keyseq, desc); |
---|
46 | if (command && !function_fn) { |
---|
47 | kb->type = OWL_KEYBINDING_COMMAND; |
---|
48 | } else if (!command && function_fn) { |
---|
49 | kb->type = OWL_KEYBINDING_FUNCTION; |
---|
50 | } else { |
---|
51 | return(-1); |
---|
52 | } |
---|
53 | |
---|
54 | ktokens = atokenize(keyseq, " ", &nktokens); |
---|
55 | if (!ktokens) return(-1); |
---|
56 | if (nktokens > OWL_KEYMAP_MAXSTACK) { |
---|
57 | atokenize_free(ktokens, nktokens); |
---|
58 | return(-1); |
---|
59 | } |
---|
60 | kb->j = owl_malloc((nktokens+1)*sizeof(int)); |
---|
61 | for (i=0; i<nktokens; i++) { |
---|
62 | kb->j[i] = owl_keypress_fromstring(ktokens[i]); |
---|
63 | if (kb->j[i] == ERR) { |
---|
64 | atokenize_free(ktokens, nktokens); |
---|
65 | owl_free(kb->j); |
---|
66 | return(-1); |
---|
67 | } |
---|
68 | } |
---|
69 | kb->j[i] = 0; |
---|
70 | |
---|
71 | atokenize_free(ktokens, nktokens); |
---|
72 | |
---|
73 | if (command) kb->command = owl_strdup(command); |
---|
74 | kb->function_fn = function_fn; |
---|
75 | if (desc) kb->desc = owl_strdup(desc); |
---|
76 | else kb->desc = NULL; |
---|
77 | return(0); |
---|
78 | } |
---|
79 | |
---|
80 | /* Releases data associated with a keybinding */ |
---|
81 | void owl_keybinding_free(owl_keybinding *kb) |
---|
82 | { |
---|
83 | if (kb->j) owl_free(kb->j); |
---|
84 | if (kb->desc) owl_free(kb->desc); |
---|
85 | if (kb->command) owl_free(kb->command); |
---|
86 | } |
---|
87 | |
---|
88 | /* Releases data associated with a keybinding, and the kb itself */ |
---|
89 | void owl_keybinding_free_all(owl_keybinding *kb) |
---|
90 | { |
---|
91 | owl_keybinding_free(kb); |
---|
92 | owl_free(kb); |
---|
93 | } |
---|
94 | |
---|
95 | /* executes a keybinding */ |
---|
96 | void owl_keybinding_execute(owl_keybinding *kb, int j) |
---|
97 | { |
---|
98 | if (kb->type == OWL_KEYBINDING_COMMAND && kb->command) { |
---|
99 | owl_function_command_norv(kb->command); |
---|
100 | } else if (kb->type == OWL_KEYBINDING_FUNCTION && kb->function_fn) { |
---|
101 | kb->function_fn(); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | /* returns 0 on success */ |
---|
106 | int owl_keybinding_stack_tostring(int *j, char *buff, int bufflen) |
---|
107 | { |
---|
108 | char *pos = buff; |
---|
109 | int rem = bufflen; |
---|
110 | int i, n; |
---|
111 | |
---|
112 | for (i=0; j[i]; i++) { |
---|
113 | owl_keypress_tostring(j[i], 0, pos, rem-1); |
---|
114 | if (j[i+1]) strcat(pos, " "); |
---|
115 | n = strlen(pos); |
---|
116 | pos += n; |
---|
117 | rem -= n; |
---|
118 | } |
---|
119 | return 0; |
---|
120 | } |
---|
121 | |
---|
122 | /* returns 0 on success */ |
---|
123 | int owl_keybinding_tostring(owl_keybinding *kb, char *buff, int bufflen) |
---|
124 | { |
---|
125 | return owl_keybinding_stack_tostring(kb->j, buff, bufflen); |
---|
126 | } |
---|
127 | |
---|
128 | char *owl_keybinding_get_desc(owl_keybinding *kb) |
---|
129 | { |
---|
130 | return kb->desc; |
---|
131 | } |
---|
132 | |
---|
133 | /* returns 0 on no match, 1 on subset match, and 2 on complete match */ |
---|
134 | int owl_keybinding_match(owl_keybinding *kb, int *kpstack) |
---|
135 | { |
---|
136 | int *kbstack = kb->j; |
---|
137 | |
---|
138 | while (*kbstack && *kpstack) { |
---|
139 | if (*kbstack != *kpstack) { |
---|
140 | return 0; |
---|
141 | } |
---|
142 | kbstack++; kpstack++; |
---|
143 | } |
---|
144 | if (!*kpstack && !*kbstack) { |
---|
145 | return 2; |
---|
146 | } else if (!*kpstack) { |
---|
147 | return 1; |
---|
148 | } else { |
---|
149 | return 0; |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | /* returns 1 if keypress sequence is the same */ |
---|
154 | int owl_keybinding_equal(owl_keybinding *kb1, owl_keybinding *kb2) |
---|
155 | { |
---|
156 | int *j1 = kb1->j; |
---|
157 | int *j2 = kb2->j; |
---|
158 | while (*j1 && *j2) { |
---|
159 | if (*(j1++) != *(j2++)) return(0); |
---|
160 | } |
---|
161 | if (*j1 != *j2) return(0); |
---|
162 | return(1); |
---|
163 | } |
---|