OpenTTD: src/hotkeys.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

hotkeys.cpp
Go to the documentation of this file.
1 /* $Id: hotkeys.cpp 26509 2014-04-25 15:40:32Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #include "stdafx.h"
13 #include "openttd.h"
14 #include "hotkeys.h"
15 #include "ini_type.h"
16 #include "string_func.h"
17 #include "window_gui.h"
18
19 #include "safeguards.h"
20
21 char *_hotkeys_file;
22
27 static SmallVector<HotkeyList*, 16> *_hotkey_lists = NULL;
28
30 struct KeycodeNames {
31 const char *name;
32 WindowKeyCodes keycode;
33 };
34
36 static const KeycodeNames _keycode_to_name[] = {
37 {"SHIFT", WKC_SHIFT},
38 {"CTRL", WKC_CTRL},
39 {"ALT", WKC_ALT},
40 {"META", WKC_META},
41 {"GLOBAL", WKC_GLOBAL_HOTKEY},
42 {"ESC", WKC_ESC},
43 {"DEL", WKC_DELETE},
44 {"RETURN", WKC_RETURN},
45 {"BACKQUOTE", WKC_BACKQUOTE},
46 {"F1", WKC_F1},
47 {"F2", WKC_F2},
48 {"F3", WKC_F3},
49 {"F4", WKC_F4},
50 {"F5", WKC_F5},
51 {"F6", WKC_F6},
52 {"F7", WKC_F7},
53 {"F8", WKC_F8},
54 {"F9", WKC_F9},
55 {"F10", WKC_F10},
56 {"F11", WKC_F11},
57 {"F12", WKC_F12},
58 {"PAUSE", WKC_PAUSE},
59 {"COMMA", WKC_COMMA},
60 {"NUM_PLUS", WKC_NUM_PLUS},
61 {"NUM_MINUS", WKC_NUM_MINUS},
62 {"=", WKC_EQUALS},
63 {"-", WKC_MINUS},
64 };
65
72 static uint16 ParseCode(const char *start, const char *end)
73 {
74 assert(start <= end);
75 while (start < end && *start == ' ') start++;
76 while (end > start && *end == ' ') end--;
77 for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
78 if (strlen(_keycode_to_name[i].name) == (size_t)(end - start) && strncasecmp(start, _keycode_to_name[i].name, end - start) == 0) {
79 return _keycode_to_name[i].keycode;
80 }
81 }
82 if (end - start == 1) {
83 if (*start >= 'a' && *start <= 'z') return *start - ('a'-'A');
84 /* Ignore invalid keycodes */
85 if (*(const uint8 *)start < 128) return *start;
86 }
87 return 0;
88 }
89
96 static uint16 ParseKeycode(const char *start, const char *end)
97 {
98 assert(start <= end);
99 uint16 keycode = 0;
100 for (;;) {
101 const char *cur = start;
102 while (*cur != '+' && cur != end) cur++;
103 uint16 code = ParseCode(start, cur);
104 if (code == 0) return 0;
105 if (code & WKC_SPECIAL_KEYS) {
106 /* Some completely wrong keycode we don't support. */
107 if (code & ~WKC_SPECIAL_KEYS) return 0;
108 keycode |= code;
109 } else {
110 /* Ignore the code if it has more then 1 letter. */
111 if (keycode & ~WKC_SPECIAL_KEYS) return 0;
112 keycode |= code;
113 }
114 if (cur == end) break;
115 assert(cur < end);
116 start = cur + 1;
117 }
118 return keycode;
119 }
120
126 static void ParseHotkeys(Hotkey *hotkey, const char *value)
127 {
128 const char *start = value;
129 while (*start != '\0') {
130 const char *end = start;
131 while (*end != '\0' && *end != ',') end++;
132 uint16 keycode = ParseKeycode(start, end);
133 if (keycode != 0) hotkey->AddKeycode(keycode);
134 start = (*end == ',') ? end + 1: end;
135 }
136 }
137
147 static const char *KeycodeToString(uint16 keycode)
148 {
149 static char buf[32];
150 buf[0] = '\0';
151 bool first = true;
152 if (keycode & WKC_GLOBAL_HOTKEY) {
153 strecat(buf, "GLOBAL", lastof(buf));
154 first = false;
155 }
156 if (keycode & WKC_SHIFT) {
157 if (!first) strecat(buf, "+", lastof(buf));
158 strecat(buf, "SHIFT", lastof(buf));
159 first = false;
160 }
161 if (keycode & WKC_CTRL) {
162 if (!first) strecat(buf, "+", lastof(buf));
163 strecat(buf, "CTRL", lastof(buf));
164 first = false;
165 }
166 if (keycode & WKC_ALT) {
167 if (!first) strecat(buf, "+", lastof(buf));
168 strecat(buf, "ALT", lastof(buf));
169 first = false;
170 }
171 if (keycode & WKC_META) {
172 if (!first) strecat(buf, "+", lastof(buf));
173 strecat(buf, "META", lastof(buf));
174 first = false;
175 }
176 if (!first) strecat(buf, "+", lastof(buf));
177 keycode = keycode & ~WKC_SPECIAL_KEYS;
178
179 for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
180 if (_keycode_to_name[i].keycode == keycode) {
181 strecat(buf, _keycode_to_name[i].name, lastof(buf));
182 return buf;
183 }
184 }
185 assert(keycode < 128);
186 char key[2];
187 key[0] = keycode;
188 key[1] = '\0';
189 strecat(buf, key, lastof(buf));
190 return buf;
191 }
192
201 const char *SaveKeycodes(const Hotkey *hotkey)
202 {
203 static char buf[128];
204 buf[0] = '\0';
205 for (uint i = 0; i < hotkey->keycodes.Length(); i++) {
206 const char *str = KeycodeToString(hotkey->keycodes[i]);
207 if (i > 0) strecat(buf, ",", lastof(buf));
208 strecat(buf, str, lastof(buf));
209 }
210 return buf;
211 }
212
219 Hotkey::Hotkey(uint16 default_keycode, const char *name, int num) :
220 name(name),
221 num(num)
222 {
223 if (default_keycode != 0) this->AddKeycode(default_keycode);
224 }
225
232 Hotkey::Hotkey(const uint16 *default_keycodes, const char *name, int num) :
233 name(name),
234 num(num)
235 {
236 const uint16 *keycode = default_keycodes;
237 while (*keycode != 0) {
238 this->AddKeycode(*keycode);
239 keycode++;
240 }
241 }
242
248 void Hotkey::AddKeycode(uint16 keycode)
249 {
250 this->keycodes.Include(keycode);
251 }
252
253 HotkeyList::HotkeyList(const char *ini_group, Hotkey *items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
254 global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(items)
255 {
256 if (_hotkey_lists == NULL) _hotkey_lists = new SmallVector<HotkeyList*, 16>();
257 *_hotkey_lists->Append() = this;
258 }
259
260 HotkeyList::~HotkeyList()
261 {
262 _hotkey_lists->Erase(_hotkey_lists->Find(this));
263 }
264
269 void HotkeyList::Load(IniFile *ini)
270 {
271 IniGroup *group = ini->GetGroup(this->ini_group);
272 for (Hotkey *hotkey = this->items; hotkey->name != NULL; ++hotkey) {
273 IniItem *item = group->GetItem(hotkey->name, false);
274 if (item != NULL) {
275 hotkey->keycodes.Clear();
276 if (item->value != NULL) ParseHotkeys(hotkey, item->value);
277 }
278 }
279 }
280
285 void HotkeyList::Save(IniFile *ini) const
286 {
287 IniGroup *group = ini->GetGroup(this->ini_group);
288 for (const Hotkey *hotkey = this->items; hotkey->name != NULL; ++hotkey) {
289 IniItem *item = group->GetItem(hotkey->name, true);
290 item->SetValue(SaveKeycodes(hotkey));
291 }
292 }
293
300 int HotkeyList::CheckMatch(uint16 keycode, bool global_only) const
301 {
302 for (const Hotkey *list = this->items; list->name != NULL; ++list) {
303 if (list->keycodes.Contains(keycode | WKC_GLOBAL_HOTKEY) || (!global_only && list->keycodes.Contains(keycode))) {
304 return list->num;
305 }
306 }
307 return -1;
308 }
309
310
311 static void SaveLoadHotkeys(bool save)
312 {
313 IniFile *ini = new IniFile();
314 ini->LoadFromDisk(_hotkeys_file, BASE_DIR);
315
316 for (HotkeyList **list = _hotkey_lists->Begin(); list != _hotkey_lists->End(); ++list) {
317 if (save) {
318 (*list)->Save(ini);
319 } else {
320 (*list)->Load(ini);
321 }
322 }
323
324 if (save) ini->SaveToDisk(_hotkeys_file);
325 delete ini;
326 }
327
328
330 void LoadHotkeysFromConfig()
331 {
332 SaveLoadHotkeys(false);
333 }
334
336 void SaveHotkeysToConfig()
337 {
338 SaveLoadHotkeys(true);
339 }
340
341 void HandleGlobalHotkeys(WChar key, uint16 keycode)
342 {
343 for (HotkeyList **list = _hotkey_lists->Begin(); list != _hotkey_lists->End(); ++list) {
344 if ((*list)->global_hotkey_handler == NULL) continue;
345
346 int hotkey = (*list)->CheckMatch(keycode, true);
347 if (hotkey >= 0 && ((*list)->global_hotkey_handler(hotkey) == ES_HANDLED)) return;
348 }
349 }
350