2 * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <bear/debug.h>
36 typedef std::map<string, SDLKey> kMap;
39 /// ***** Private Method Headers *****
41 void processLine(const string& str);
42 bool extractLine(const string& str, string* name, string* value);
45 /// ***** Private Variables *****
47 const char* configDir = "configs/";
48 const char* testFile = "keys.cfg";
50 /// ***** Public Methods *****
58 strcpy(fileName, configDir);
59 strcat(fileName, testFile);
61 ifstream file(fileName);
65 cerr << "Unable to open file " << fileName << "." << endl;
83 /// ***** Private Methods *****
85 void processLine(const string& str)
92 extracted = extractLine(str, &name, &value);
96 SDLKey key = keyMap[value];
100 *(key::sdlMap[name]) = key;
106 * Return true iff the string str, had a name and value extracted and saved in
107 * name and value. Otherwise return false and leave name and value alone.
109 bool extractLine(const string& str, string* name, string* value)
112 * state := 0 if eating leading name whitespace
113 * := 1 if saving name
114 * := 2 if eating trailing name whitespace
115 * := 3 if eating leading value whitespace
116 * := 4 if saving value
117 * := 5 if all is done and well
129 const char* c_str = str.c_str();
133 if(c_str[char_pos] == 0 && state != 4)
139 if(c_str[char_pos] != ' '
140 && c_str[char_pos] != '\t')
143 name_start = char_pos;
147 if(c_str[char_pos] == ' '
148 || c_str[char_pos] == '\t'
149 || c_str[char_pos] == '=')
155 if(c_str[char_pos] == '=')
156 char_pos--; // decrement to stay on this char
159 if(c_str[char_pos] != ' '
160 && c_str[char_pos] != '\t'
161 && c_str[char_pos] != '=')
166 if(c_str[char_pos] == '=')
172 if(c_str[char_pos] != ' '
173 && c_str[char_pos] != '\t')
176 value_start = char_pos;
180 if(c_str[char_pos] == ' '
181 || c_str[char_pos] == '\t'
182 || c_str[char_pos] == 0)
185 value_end = char_pos;
199 name->replace (0, 0, c_str, name_start, name_end - name_start);
200 value->replace (0, 0, c_str, value_start, value_end - value_start);
209 // add all the letters
210 for (int i = 'A'; i <= 'Z'; i++) // uppercase
213 keyMap[buf] = (SDLKey)(i + 'a' - 'A');
215 for (int i = 'a'; i <= 'z'; i++) // lowercase
218 keyMap[buf] = (SDLKey)i;
221 // add all the numbers
222 for (int i = '0'; i <= '9'; i++)
225 keyMap[buf] = (SDLKey)i;
229 // add the function keys
230 int F1 = (int)Key.F1;
231 for (int i = F1; i <= (int)Key.F15; i++)
233 keyMap.Add("F" + (i - F1 + 1), (Key)i);
234 keyMap.Add("f" + (i - F1 + 1), (Key)i);
238 keyMap["LCtrl"] = SDLK_LCTRL;
239 keyMap["LeftControl"] = SDLK_LCTRL;
240 keyMap["LAlt"] = SDLK_LALT;
241 keyMap["LeftAlt"] = SDLK_LALT;
242 keyMap["LShift"] = SDLK_LSHIFT;
243 keyMap["LeftShift"] = SDLK_LSHIFT;
244 keyMap["LWin"] = SDLK_LSUPER;
245 keyMap["LeftWindows"] = SDLK_LSUPER;
246 keyMap["LeftMeta"] = SDLK_LMETA;
247 keyMap["LMeta"] = SDLK_LMETA;
249 keyMap["RCtrl"] = SDLK_RCTRL;
250 keyMap["RightControl"] = SDLK_RCTRL;
251 keyMap["RAlt"] = SDLK_RALT;
252 keyMap["RightAlt"] = SDLK_RALT;
253 keyMap["RShift"] = SDLK_RSHIFT;
254 keyMap["RightShift"] = SDLK_RSHIFT;
255 keyMap["RWin"] = SDLK_RSUPER;
256 keyMap["RightWindows"] = SDLK_RSUPER;
257 keyMap["RightMeta"] = SDLK_RMETA;
258 keyMap["RMeta"] = SDLK_RMETA;
260 keyMap["Esc"] = SDLK_ESCAPE;
261 keyMap["Escape"] = SDLK_ESCAPE;
263 keyMap["Return"] = SDLK_RETURN;
264 keyMap["Enter"] = SDLK_RETURN;
266 keyMap["Insert"] = SDLK_INSERT;
267 keyMap["Home"] = SDLK_HOME;
268 keyMap["Delete"] = SDLK_DELETE;
269 keyMap["End"] = SDLK_END;
270 keyMap["PageUp"] = SDLK_PAGEUP;
271 keyMap["PageDown"] = SDLK_PAGEDOWN;
273 keyMap["Minus"] = SDLK_MINUS;
274 keyMap["Equal"] = SDLK_EQUALS;
275 keyMap["Equals"] = SDLK_EQUALS;
276 keyMap["LeftBracket"] = SDLK_LEFTBRACKET;
277 keyMap["LBracket"] = SDLK_LEFTBRACKET;
278 keyMap["RightBracket"] = SDLK_RIGHTBRACKET;
279 keyMap["RBracket"] = SDLK_RIGHTBRACKET;
280 keyMap["Backslash"] = SDLK_BACKSLASH;
281 keyMap["Slash"] = SDLK_SLASH;
282 keyMap["Semicolon"] = SDLK_SEMICOLON;
283 keyMap["Semi"] = SDLK_SEMICOLON;
284 keyMap["Quote"] = SDLK_QUOTE;
285 keyMap["Comma"] = SDLK_COMMA;
286 keyMap["Period"] = SDLK_PERIOD;
287 keyMap["Space"] = SDLK_SPACE;
288 keyMap["BSpace"] = SDLK_BACKSPACE;
289 keyMap["Backspace"] = SDLK_BACKSPACE;
290 keyMap["BackSpace"] = SDLK_BACKSPACE;
292 keyMap["Tab"] = SDLK_TAB;
293 keyMap["BackQuote"] = SDLK_BACKQUOTE;
294 keyMap["BQuote"] = SDLK_BACKQUOTE;
295 keyMap["CapsLock"] = SDLK_CAPSLOCK;
296 keyMap["Caps"] = SDLK_CAPSLOCK;
298 keyMap["Up"] = SDLK_UP;
299 keyMap["Down"] = SDLK_DOWN;
300 keyMap["Left"] = SDLK_LEFT;
301 keyMap["Right"] = SDLK_RIGHT;