changed copyright years in source files
[fegdk.git] / core / code / system / f_keybinder.cpp
blob6f40a25c787d60293bbfc14e93e8c006caa11347
1 /*
2 fegdk: FE Game Development Kit
3 Copyright (C) 2001-2008 Alexey "waker" Yakovenko
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library 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 GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 Alexey Yakovenko
20 waker@users.sourceforge.net
23 #include "pch.h"
24 #include "f_keybinder.h"
25 #include "f_keycodes.h"
26 #include "f_engine.h"
27 #include "f_console.h"
28 #include "f_input.h"
30 namespace fe
33 keyBinder::keyBinder (void)
37 keyBinder::~keyBinder (void)
41 void keyBinder::bind (int keycombo, const char *cmd)
43 mHandlers[keycombo] = cmd;
46 void keyBinder::unbind (int keycombo)
48 handlersMap::iterator it = mHandlers.find (keycombo);
49 if (it != mHandlers.end ())
50 mHandlers.erase (it);
53 void keyBinder::unbindAll (void)
55 mHandlers.clear ();
58 int keyBinder::keycodeForString (const char *str) const
60 const char *ptr = str;
61 int i;
62 int keycombo = 0;
63 struct { const char* name; int value; } modifiers[] = {
64 {"lalt",lalt},
65 {"lctrl",lctrl},
66 {"lshift",lshift},
67 {"ralt",ralt},
68 {"rctrl",rctrl},
69 {"rshift",rshift},
70 {"alt",alt},
71 {"shift",shift},
72 {"ctrl",ctrl},
73 {NULL,0}
75 char symbols[] = "[]\\-=`;'/,.";
76 // tab capslock backspace esc ins del home end pgup pgdown
77 // arrow keys
78 // all numpad keys
79 // function keys (f1-f15)
80 // mouse1-mouse5,
81 // wheelup, wheeldown
82 struct { const char* name; int value; } syskeys[] = {
83 {"tab", Key_Tab},
84 {"capslock", Key_CapsLock},
85 {"backspace", Key_BackSpace},
86 {"esc", Key_Escape},
87 {"escape", Key_Escape},
88 {"enter", Key_Enter},
89 {"return", Key_Enter},
90 {"ins", Key_Ins},
91 {"del", Key_Del},
92 {"home", Key_Home},
93 {"end", Key_End},
94 {"pgup", Key_PgUp},
95 {"pgdn", Key_PgDn},
96 {"left", Key_LeftArrow},
97 {"right", Key_RightArrow},
98 {"up", Key_UpArrow},
99 {"down", Key_DownArrow},
100 {"kphome", Key_Kp_Home},
101 {"kpup", Key_Kp_UpArrow},
102 {"kppgup", Key_Kp_PgUp},
103 {"kpleft", Key_Kp_LeftArrow},
104 {"kp5", Key_Kp_5},
105 {"kpright", Key_Kp_RightArrow},
106 {"kpend", Key_Kp_End},
107 {"kpdown", Key_Kp_DownArrow},
108 {"kpenter", Key_Kp_Enter},
109 {"kpins", Key_Kp_Ins},
110 {"kpdel", Key_Kp_Del},
111 {"kpslash", Key_Kp_Slash},
112 {"kpminus", Key_Kp_Minus},
113 {"kpplus", Key_Kp_Plus},
114 {"kpnumlock", Key_Kp_NumLock},
115 {"kpstar", Key_Kp_Star},
116 {"kpequals", Key_Kp_Equals},
117 {"f1", Key_F1},
118 {"f2", Key_F2},
119 {"f3", Key_F3},
120 {"f4", Key_F4},
121 {"f5", Key_F5},
122 {"f6", Key_F6},
123 {"f7", Key_F7},
124 {"f8", Key_F8},
125 {"f9", Key_F9},
126 {"f10", Key_F10},
127 {"f11", Key_F11},
128 {"f12", Key_F12},
129 {"f13", Key_F13},
130 {"f14", Key_F14},
131 {"f15", Key_F15},
132 {"mouse1", Key_Mouse1},
133 {"mouse2", Key_Mouse2},
134 {"mouse3", Key_Mouse3},
135 {"mouse4", Key_Mouse4},
136 {"mouse5", Key_Mouse5},
137 {"wheelup", Key_MWheelUp},
138 {"wheeldown", Key_MWheelDown},
139 {NULL, 0}
142 for (;;)
144 if (*ptr == 0)
145 break;
146 else if (*ptr == '+' || *ptr == ' ')
148 ptr++;
149 continue;
152 // check if we have a modifier key
153 for (i = 0; modifiers[i].name; i++)
155 size_t l = strlen (modifiers[i].name);
156 if (!strncmp (modifiers[i].name, ptr, l)
157 && (*(ptr+l) == 0 || *(ptr+l) == '+' || *(ptr+l) == ' '))
159 keycombo |= modifiers[i].value;
160 ptr += l;
161 break;
164 if (modifiers[i].name)
165 continue;
167 // lowercase letters
168 if ((*ptr >= 0x61 && *ptr <= 0x7a)
169 && (*(ptr+1)=='+' || *(ptr+1)==' ' || *(ptr+1)==0))
171 keycombo |= *ptr;
172 ptr++;
173 continue;
176 // digits
177 if ((*ptr >= 0x31 && *ptr <= 0x38)
178 && (*(ptr+1)=='+' || *(ptr+1)==' ' || *(ptr+1)==0))
180 keycombo |= *ptr;
181 ptr++;
182 continue;
185 // []\-=`;'/,. symbols
186 if (strchr (symbols, *ptr))
188 keycombo |= *ptr;
189 ptr++;
190 continue;
193 // tab capslock backspace esc ins del home end pgup pgdown
194 // arrow keys
195 // all numpad keys
196 // function keys (f1-f15)
197 // mouse1-mouse5,
198 // wheelup, wheeldown
199 // joy1-joy32,
200 for (i = 0; syskeys[i].name; i++)
202 size_t l = strlen (syskeys[i].name);
203 if (!strncmp (syskeys[i].name, ptr, l)
204 && (*(ptr+l) == 0 || *(ptr+l) == '+' || *(ptr+l) == ' '))
206 keycombo |= syskeys[i].value;
207 ptr += l;
208 break;
211 if (syskeys[i].name)
212 continue;
214 // if we got there -- error
215 return -1;
217 return keycombo;
220 bool keyBinder::keyDown (int keycombo)
222 handlersMap::iterator it;
223 inputState *st = g_engine->getInputDevice ()->getState ();
225 // we gonna check all possible mod-combos
226 // we allow only one modifier + key at a time
227 /* if (st->isPressed (Key_Shift)
228 || st->isPressed (Key_LShift)
229 || st->isPressed (Key_RShift))
231 it = mHandlers.find (keycombo | shift);
233 else*/ if (st->isPressed (Key_LShift))
235 it = mHandlers.find (keycombo | lshift);
237 else if (st->isPressed (Key_RShift))
239 it = mHandlers.find (keycombo | rshift);
241 /*else if (st->isPressed (Key_Alt)
242 || st->isPressed (Key_LAlt)
243 || st->isPressed (Key_RAlt))
245 it = mHandlers.find (keycombo | alt);
247 else if (st->isPressed (Key_LAlt))
249 it = mHandlers.find (keycombo | lalt);
251 else if (st->isPressed (Key_RAlt))
253 it = mHandlers.find (keycombo | ralt);
255 /*else if (st->isPressed (Key_Ctrl)
256 || st->isPressed (Key_LCtrl)
257 || st->isPressed (Key_RCtrl))
259 it = mHandlers.find (keycombo | ctrl);
261 else if (st->isPressed (Key_LCtrl))
263 it = mHandlers.find (keycombo | lctrl);
265 else if (st->isPressed (Key_RCtrl))
267 it = mHandlers.find (keycombo | rctrl);
269 else
271 it = mHandlers.find (keycombo);
274 if (it != mHandlers.end ())
276 g_engine->getConsole ()->command ((*it).second.c_str ());
277 return true;
279 return false;
282 bool keyBinder::keyUp (int keycombo)
284 handlersMap::iterator it;
285 it = mHandlers.find (keycombo);
286 if (it != mHandlers.end ())
288 if ((*it).second[0] == '+')
290 const char *p = (*it).second.c_str ();
291 g_engine->getConsole ()->command (cStr("-") + p + 1);
292 return true;
295 return false;