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 branched BarnOwl project, Copyright (c) |
---|
22 | * 2006-2009 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 | static struct _owl_keypress_specialmap { |
---|
32 | int kj; |
---|
33 | char *ks; |
---|
34 | } specialmap[] = { |
---|
35 | #ifdef KEY_CODE_YES |
---|
36 | { KEY_CODE_YES, "CODE_YES" }, |
---|
37 | #endif |
---|
38 | { KEY_MIN, "MIN" }, |
---|
39 | { KEY_BREAK, "BREAK" }, |
---|
40 | { KEY_DOWN, "DOWN" }, |
---|
41 | { KEY_UP, "UP" }, |
---|
42 | { KEY_LEFT, "LEFT" }, |
---|
43 | { KEY_RIGHT, "RIGHT" }, |
---|
44 | { KEY_HOME, "HOME" }, |
---|
45 | { KEY_BACKSPACE, "BACKSPACE" }, |
---|
46 | { KEY_F0, "F0" }, |
---|
47 | { KEY_F(1), "F1" }, |
---|
48 | { KEY_F(2), "F2" }, |
---|
49 | { KEY_F(3), "F3" }, |
---|
50 | { KEY_F(4), "F4" }, |
---|
51 | { KEY_F(5), "F5" }, |
---|
52 | { KEY_F(6), "F6" }, |
---|
53 | { KEY_F(7), "F7" }, |
---|
54 | { KEY_F(8), "F8" }, |
---|
55 | { KEY_F(9), "F9" }, |
---|
56 | { KEY_F(10), "F10" }, |
---|
57 | { KEY_F(11), "F11" }, |
---|
58 | { KEY_F(12), "F12" }, |
---|
59 | { KEY_DL, "DL" }, |
---|
60 | { KEY_IL, "IL" }, |
---|
61 | { KEY_DC, "DC" }, |
---|
62 | { KEY_IC, "IC" }, |
---|
63 | { KEY_EIC, "EIC" }, |
---|
64 | { KEY_CLEAR, "CLEAR" }, |
---|
65 | { KEY_EOS, "EOS" }, |
---|
66 | { KEY_EOL, "EOL" }, |
---|
67 | { KEY_SF, "SF" }, |
---|
68 | { KEY_SR, "SR" }, |
---|
69 | { KEY_NPAGE, "NPAGE" }, |
---|
70 | { KEY_PPAGE, "PPAGE" }, |
---|
71 | { KEY_STAB, "STAB" }, |
---|
72 | { KEY_CTAB, "CTAB" }, |
---|
73 | { KEY_CATAB, "CATAB" }, |
---|
74 | { KEY_ENTER, "ENTER" }, |
---|
75 | { KEY_SRESET, "SRESET" }, |
---|
76 | { KEY_RESET, "RESET" }, |
---|
77 | { KEY_PRINT, "PRINT" }, |
---|
78 | { KEY_LL, "LL" }, |
---|
79 | { KEY_A1, "A1" }, |
---|
80 | { KEY_A3, "A3" }, |
---|
81 | { KEY_B2, "B2" }, |
---|
82 | { KEY_C1, "C1" }, |
---|
83 | { KEY_C3, "C3" }, |
---|
84 | { KEY_BTAB, "BTAB" }, |
---|
85 | { KEY_BEG, "BEG" }, |
---|
86 | { KEY_CANCEL, "CANCEL" }, |
---|
87 | { KEY_CLOSE, "CLOSE" }, |
---|
88 | { KEY_COMMAND, "COMMAND" }, |
---|
89 | { KEY_COPY, "COPY" }, |
---|
90 | { KEY_CREATE, "CREATE" }, |
---|
91 | { KEY_END, "END" }, |
---|
92 | { KEY_EXIT, "EXIT" }, |
---|
93 | { KEY_FIND, "FIND" }, |
---|
94 | { KEY_HELP, "HELP" }, |
---|
95 | { KEY_MARK, "MARK" }, |
---|
96 | { KEY_MESSAGE, "MESSAGE" }, |
---|
97 | { KEY_MOVE, "MOVE" }, |
---|
98 | { KEY_NEXT, "NEXT" }, |
---|
99 | { KEY_OPEN, "OPEN" }, |
---|
100 | { KEY_OPTIONS, "OPTIONS" }, |
---|
101 | { KEY_PREVIOUS, "PREVIOUS" }, |
---|
102 | { KEY_REDO, "REDO" }, |
---|
103 | { KEY_REFERENCE, "REFERENCE" }, |
---|
104 | { KEY_REFRESH, "REFRESH" }, |
---|
105 | { KEY_REPLACE, "REPLACE" }, |
---|
106 | { KEY_RESTART, "RESTART" }, |
---|
107 | { KEY_RESUME, "RESUME" }, |
---|
108 | { KEY_SAVE, "SAVE" }, |
---|
109 | { KEY_SBEG, "SBEG" }, |
---|
110 | { KEY_SCANCEL, "SCANCEL" }, |
---|
111 | { KEY_SCOMMAND, "SCOMMAND" }, |
---|
112 | { KEY_SCOPY, "SCOPY" }, |
---|
113 | { KEY_SCREATE, "SCREATE" }, |
---|
114 | { KEY_SDC, "SDC" }, |
---|
115 | { KEY_SDL, "SDL" }, |
---|
116 | { KEY_SELECT, "SELECT" }, |
---|
117 | { KEY_SEND, "SEND" }, |
---|
118 | { KEY_SEOL, "SEOL" }, |
---|
119 | { KEY_SEXIT, "SEXIT" }, |
---|
120 | { KEY_SFIND, "SFIND" }, |
---|
121 | { KEY_SHELP, "SHELP" }, |
---|
122 | { KEY_SHOME, "SHOME" }, |
---|
123 | { KEY_SIC, "SIC" }, |
---|
124 | { KEY_SLEFT, "SLEFT" }, |
---|
125 | { KEY_SMESSAGE, "SMESSAGE" }, |
---|
126 | { KEY_SMOVE, "SMOVE" }, |
---|
127 | { KEY_SNEXT, "SNEXT" }, |
---|
128 | { KEY_SOPTIONS, "SOPTIONS" }, |
---|
129 | { KEY_SPREVIOUS, "SPREVIOUS" }, |
---|
130 | { KEY_SPRINT, "SPRINT" }, |
---|
131 | { KEY_SREDO, "SREDO" }, |
---|
132 | { KEY_SREPLACE, "SREPLACE" }, |
---|
133 | { KEY_SRIGHT, "SRIGHT" }, |
---|
134 | { KEY_SRSUME, "SRSUME" }, |
---|
135 | { KEY_SSAVE, "SSAVE" }, |
---|
136 | { KEY_SSUSPEND, "SSUSPEND" }, |
---|
137 | { KEY_SUNDO, "SUNDO" }, |
---|
138 | { KEY_SUSPEND, "SUSPEND" }, |
---|
139 | { KEY_UNDO, "UNDO" }, |
---|
140 | { KEY_MOUSE, "MOUSE" }, |
---|
141 | #ifdef KEY_RESIZE |
---|
142 | { KEY_RESIZE, "RESIZE" }, |
---|
143 | #endif |
---|
144 | { KEY_MAX, "MAX" }, |
---|
145 | { ' ', "SPACE" }, |
---|
146 | { 27, "ESCAPE" }, |
---|
147 | { 127, "DELETE" }, |
---|
148 | { '\r', "CR" }, |
---|
149 | { '\n', "LF" }, |
---|
150 | { 0, NULL } |
---|
151 | }; |
---|
152 | |
---|
153 | #define OWL_CTRL(key) ((key)&037) |
---|
154 | /* OWL_META is definied in owl.h */ |
---|
155 | |
---|
156 | /* returns 0 on success */ |
---|
157 | int owl_keypress_tostring(int j, int esc, char *buff, int bufflen) |
---|
158 | { |
---|
159 | char kb[64], kb2[2]; |
---|
160 | struct _owl_keypress_specialmap *sm; |
---|
161 | |
---|
162 | *kb = '\0'; |
---|
163 | for (sm = specialmap; sm->kj!=0; sm++) { |
---|
164 | if (j == OWL_META(sm->kj) || (esc && j == sm->kj)) { |
---|
165 | strcat(kb, "M-"); |
---|
166 | strcat(kb, sm->ks); |
---|
167 | break; |
---|
168 | } else if (j == sm->kj) { |
---|
169 | strcat(kb, sm->ks); |
---|
170 | break; |
---|
171 | } |
---|
172 | } |
---|
173 | if (!*kb) { |
---|
174 | if (j&OWL_META(0)) { |
---|
175 | strcat(kb, "M-"); |
---|
176 | j &= ~OWL_META(0); |
---|
177 | } |
---|
178 | if ((OWL_CTRL(j) == j)) { |
---|
179 | strcat(kb, "C-"); |
---|
180 | j |= 0x40; |
---|
181 | if (isupper(j)) j = tolower(j); |
---|
182 | |
---|
183 | } |
---|
184 | if (isascii(j)) { |
---|
185 | kb2[0] = j; |
---|
186 | kb2[1] = 0; |
---|
187 | strcat(kb, kb2); |
---|
188 | } |
---|
189 | } |
---|
190 | if (!*kb) { |
---|
191 | /* not a valid key */ |
---|
192 | strncpy(buff, "INVALID", bufflen); |
---|
193 | return(-1); |
---|
194 | } |
---|
195 | strncpy(buff, kb, bufflen); |
---|
196 | return(0); |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | /* returns ERR on failure, else a keycode */ |
---|
201 | int owl_keypress_fromstring(char *kb) |
---|
202 | { |
---|
203 | struct _owl_keypress_specialmap *sm; |
---|
204 | int ismeta=0, isctrl=0; |
---|
205 | int j = ERR; |
---|
206 | |
---|
207 | while (*kb && kb[1] == '-' && (kb[0] == 'C' || kb[0] == 'M')) { |
---|
208 | if ((kb[0] == 'C') && (kb[1] == '-')) { |
---|
209 | isctrl = 1; |
---|
210 | kb+=2; |
---|
211 | } |
---|
212 | if ((kb[0] == 'M') && (kb[1] == '-')) { |
---|
213 | ismeta = 1; |
---|
214 | kb+=2; |
---|
215 | } |
---|
216 | } |
---|
217 | if (isascii(kb[0]) && !kb[1]) { |
---|
218 | j = kb[0]; |
---|
219 | } else { |
---|
220 | for (sm = specialmap; sm->kj!=0; sm++) { |
---|
221 | if (!strcmp(sm->ks, kb)) { |
---|
222 | j = sm->kj; |
---|
223 | } |
---|
224 | } |
---|
225 | } |
---|
226 | if (j==ERR) return(ERR); |
---|
227 | if (isctrl) { |
---|
228 | j = OWL_CTRL(j); |
---|
229 | } |
---|
230 | if (ismeta) { |
---|
231 | j = OWL_META(j); |
---|
232 | } |
---|
233 | return(j); |
---|
234 | } |
---|
235 | |
---|