OpenTTD Source: src/hotkeys.cpp Source File

OpenTTD Source 1.11.0-RC1

hotkeys.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #include "stdafx.h"
11 #include "openttd.h"
12 #include "hotkeys.h"
13 #include "ini_type.h"
14 #include "string_func.h"
15 #include "window_gui.h"
16
17 #include "safeguards.h"
18
19 std::string _hotkeys_file;
20
25 static std::vector<HotkeyList*> *_hotkey_lists = nullptr;
26
28 struct KeycodeNames {
29 const char *name;
30 WindowKeyCodes keycode;
31 };
32
34 static const KeycodeNames _keycode_to_name[] = {
35 {"SHIFT", WKC_SHIFT},
36 {"CTRL", WKC_CTRL},
37 {"ALT", WKC_ALT},
38 {"META", WKC_META},
39 {"GLOBAL", WKC_GLOBAL_HOTKEY},
40 {"ESC", WKC_ESC},
41 {"BACKSPACE", WKC_BACKSPACE},
42 {"INS", WKC_INSERT},
43 {"DEL", WKC_DELETE},
44 {"PAGEUP", WKC_PAGEUP},
45 {"PAGEDOWN", WKC_PAGEDOWN},
46 {"END", WKC_END},
47 {"HOME", WKC_HOME},
48 {"RETURN", WKC_RETURN},
49 {"SPACE", WKC_SPACE},
50 {"F1", WKC_F1},
51 {"F2", WKC_F2},
52 {"F3", WKC_F3},
53 {"F4", WKC_F4},
54 {"F5", WKC_F5},
55 {"F6", WKC_F6},
56 {"F7", WKC_F7},
57 {"F8", WKC_F8},
58 {"F9", WKC_F9},
59 {"F10", WKC_F10},
60 {"F11", WKC_F11},
61 {"F12", WKC_F12},
62 {"BACKQUOTE", WKC_BACKQUOTE},
63 {"PAUSE", WKC_PAUSE},
64 {"NUM_DIV", WKC_NUM_DIV},
65 {"NUM_MUL", WKC_NUM_MUL},
66 {"NUM_MINUS", WKC_NUM_MINUS},
67 {"NUM_PLUS", WKC_NUM_PLUS},
68 {"NUM_ENTER", WKC_NUM_ENTER},
69 {"NUM_DOT", WKC_NUM_DECIMAL},
70 {"SLASH", WKC_SLASH},
71 {"/", WKC_SLASH}, /* deprecated, use SLASH */
72 {"SEMICOLON", WKC_SEMICOLON},
73 {";", WKC_SEMICOLON}, /* deprecated, use SEMICOLON */
74 {"EQUALS", WKC_EQUALS},
75 {"=", WKC_EQUALS}, /* deprecated, use EQUALS */
76 {"L_BRACKET", WKC_L_BRACKET},
77 {"[", WKC_L_BRACKET}, /* deprecated, use L_BRACKET */
78 {"BACKSLASH", WKC_BACKSLASH},
79 {"\\", WKC_BACKSLASH}, /* deprecated, use BACKSLASH */
80 {"R_BRACKET", WKC_R_BRACKET},
81 {"]", WKC_R_BRACKET}, /* deprecated, use R_BRACKET */
82 {"SINGLEQUOTE", WKC_SINGLEQUOTE},
83 {"'", WKC_SINGLEQUOTE}, /* deprecated, use SINGLEQUOTE */
84 {"COMMA", WKC_COMMA},
85 {"PERIOD", WKC_PERIOD},
86 {".", WKC_PERIOD}, /* deprecated, use PERIOD */
87 {"MINUS", WKC_MINUS},
88 {"-", WKC_MINUS}, /* deprecated, use MINUS */
89 };
90
97 static uint16 ParseCode(const char *start, const char *end)
98 {
99 assert(start <= end);
100 while (start < end && *start == ' ') start++;
101 while (end > start && *end == ' ') end--;
102 for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
103 if (strlen(_keycode_to_name[i].name) == (size_t)(end - start) && strncasecmp(start, _keycode_to_name[i].name, end - start) == 0) {
104 return _keycode_to_name[i].keycode;
105 }
106 }
107 if (end - start == 1) {
108 if (*start >= 'a' && *start <= 'z') return *start - ('a'-'A');
109 /* Ignore invalid keycodes */
110 if (*(const uint8 *)start < 128) return *start;
111 }
112 return 0;
113 }
114
121 static uint16 ParseKeycode(const char *start, const char *end)
122 {
123 assert(start <= end);
124 uint16 keycode = 0;
125 for (;;) {
126 const char *cur = start;
127 while (*cur != '+' && cur != end) cur++;
128 uint16 code = ParseCode(start, cur);
129 if (code == 0) return 0;
130 if (code & WKC_SPECIAL_KEYS) {
131 /* Some completely wrong keycode we don't support. */
132 if (code & ~WKC_SPECIAL_KEYS) return 0;
133 keycode |= code;
134 } else {
135 /* Ignore the code if it has more then 1 letter. */
136 if (keycode & ~WKC_SPECIAL_KEYS) return 0;
137 keycode |= code;
138 }
139 if (cur == end) break;
140 assert(cur < end);
141 start = cur + 1;
142 }
143 return keycode;
144 }
145
151 static void ParseHotkeys(Hotkey *hotkey, const char *value)
152 {
153 const char *start = value;
154 while (*start != '\0') {
155 const char *end = start;
156 while (*end != '\0' && *end != ',') end++;
157 uint16 keycode = ParseKeycode(start, end);
158 if (keycode != 0) hotkey->AddKeycode(keycode);
159 start = (*end == ',') ? end + 1: end;
160 }
161 }
162
172 static const char *KeycodeToString(uint16 keycode)
173 {
174 static char buf[32];
175 buf[0] = '\0';
176 bool first = true;
177 if (keycode & WKC_GLOBAL_HOTKEY) {
178 strecat(buf, "GLOBAL", lastof(buf));
179 first = false;
180 }
181 if (keycode & WKC_SHIFT) {
182 if (!first) strecat(buf, "+", lastof(buf));
183 strecat(buf, "SHIFT", lastof(buf));
184 first = false;
185 }
186 if (keycode & WKC_CTRL) {
187 if (!first) strecat(buf, "+", lastof(buf));
188 strecat(buf, "CTRL", lastof(buf));
189 first = false;
190 }
191 if (keycode & WKC_ALT) {
192 if (!first) strecat(buf, "+", lastof(buf));
193 strecat(buf, "ALT", lastof(buf));
194 first = false;
195 }
196 if (keycode & WKC_META) {
197 if (!first) strecat(buf, "+", lastof(buf));
198 strecat(buf, "META", lastof(buf));
199 first = false;
200 }
201 if (!first) strecat(buf, "+", lastof(buf));
202 keycode = keycode & ~WKC_SPECIAL_KEYS;
203
204 for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
205 if (_keycode_to_name[i].keycode == keycode) {
206 strecat(buf, _keycode_to_name[i].name, lastof(buf));
207 return buf;
208 }
209 }
210 assert(keycode < 128);
211 char key[2];
212 key[0] = keycode;
213 key[1] = '\0';
214 strecat(buf, key, lastof(buf));
215 return buf;
216 }
217
226 const char *SaveKeycodes(const Hotkey *hotkey)
227 {
228 static char buf[128];
229 buf[0] = '\0';
230 for (uint i = 0; i < hotkey->keycodes.size(); i++) {
231 const char *str = KeycodeToString(hotkey->keycodes[i]);
232 if (i > 0) strecat(buf, ",", lastof(buf));
233 strecat(buf, str, lastof(buf));
234 }
235 return buf;
236 }
237
244 Hotkey::Hotkey(uint16 default_keycode, const char *name, int num) :
245 name(name),
246 num(num)
247 {
248 if (default_keycode != 0) this->AddKeycode(default_keycode);
249 }
250
257 Hotkey::Hotkey(const uint16 *default_keycodes, const char *name, int num) :
258 name(name),
259 num(num)
260 {
261 const uint16 *keycode = default_keycodes;
262 while (*keycode != 0) {
263 this->AddKeycode(*keycode);
264 keycode++;
265 }
266 }
267
273 void Hotkey::AddKeycode(uint16 keycode)
274 {
275 include(this->keycodes, keycode);
276 }
277
278 HotkeyList::HotkeyList(const char *ini_group, Hotkey *items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
279 global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(items)
280 {
281 if (_hotkey_lists == nullptr) _hotkey_lists = new std::vector<HotkeyList*>();
282 _hotkey_lists->push_back(this);
283 }
284
285 HotkeyList::~HotkeyList()
286 {
287 _hotkey_lists->erase(std::find(_hotkey_lists->begin(), _hotkey_lists->end(), this));
288 }
289
294 void HotkeyList::Load(IniFile *ini)
295 {
296 IniGroup *group = ini->GetGroup(this->ini_group);
297 for (Hotkey *hotkey = this->items; hotkey->name != nullptr; ++hotkey) {
298 IniItem *item = group->GetItem(hotkey->name, false);
299 if (item != nullptr) {
300 hotkey->keycodes.clear();
301 if (item->value.has_value()) ParseHotkeys(hotkey, item->value->c_str());
302 }
303 }
304 }
305
310 void HotkeyList::Save(IniFile *ini) const
311 {
312 IniGroup *group = ini->GetGroup(this->ini_group);
313 for (const Hotkey *hotkey = this->items; hotkey->name != nullptr; ++hotkey) {
314 IniItem *item = group->GetItem(hotkey->name, true);
315 item->SetValue(SaveKeycodes(hotkey));
316 }
317 }
318
325 int HotkeyList::CheckMatch(uint16 keycode, bool global_only) const
326 {
327 for (const Hotkey *list = this->items; list->name != nullptr; ++list) {
328 auto begin = list->keycodes.begin();
329 auto end = list->keycodes.end();
330 if (std::find(begin, end, keycode | WKC_GLOBAL_HOTKEY) != end || (!global_only && std::find(begin, end, keycode) != end)) {
331 return list->num;
332 }
333 }
334 return -1;
335 }
336
337
338 static void SaveLoadHotkeys(bool save)
339 {
340 IniFile *ini = new IniFile();
341 ini->LoadFromDisk(_hotkeys_file, NO_DIRECTORY);
342
343 for (HotkeyList *list : *_hotkey_lists) {
344 if (save) {
345 list->Save(ini);
346 } else {
347 list->Load(ini);
348 }
349 }
350
351 if (save) ini->SaveToDisk(_hotkeys_file);
352 delete ini;
353 }
354
355
357 void LoadHotkeysFromConfig()
358 {
359 SaveLoadHotkeys(false);
360 }
361
363 void SaveHotkeysToConfig()
364 {
365 SaveLoadHotkeys(true);
366 }
367
368 void HandleGlobalHotkeys(WChar key, uint16 keycode)
369 {
370 for (HotkeyList *list : *_hotkey_lists) {
371 if (list->global_hotkey_handler == nullptr) continue;
372
373 int hotkey = list->CheckMatch(keycode, true);
374 if (hotkey >= 0 && (list->global_hotkey_handler(hotkey) == ES_HANDLED)) return;
375 }
376 }
377
ES_HANDLED
@ ES_HANDLED
The passed event is handled.
Definition: window_type.h:718
WKC_SINGLEQUOTE
@ WKC_SINGLEQUOTE
' Single quote
Definition: gfx_type.h:101
_keycode_to_name
static const KeycodeNames _keycode_to_name[]
Array of non-standard keycodes that can be used in the hotkeys config file.
Definition: hotkeys.cpp:34
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
HotkeyList
List of hotkeys for a window.
Definition: hotkeys.h:40
WindowKeyCodes
WindowKeyCodes
Definition: gfx_type.h:27
IniItem
A single "line" in an ini file.
Definition: ini_type.h:25
WKC_SLASH
@ WKC_SLASH
/ Forward slash
Definition: gfx_type.h:95
IniGroup
A group within an ini file.
Definition: ini_type.h:38
WKC_BACKSLASH
@ WKC_BACKSLASH
\ Backslash
Definition: gfx_type.h:99
include
bool include(std::vector< T > &vec, const T &item)
Helper function to append an item to a vector if it is not already contained Consider using std::set,...
Definition: smallvec_type.hpp:27
WKC_L_BRACKET
@ WKC_L_BRACKET
[ Left square bracket
Definition: gfx_type.h:98
ParseCode
static uint16 ParseCode(const char *start, const char *end)
Try to parse a single part of a keycode.
Definition: hotkeys.cpp:97
ParseHotkeys
static void ParseHotkeys(Hotkey *hotkey, const char *value)
Parse a string to the keycodes it represents.
Definition: hotkeys.cpp:151
Hotkey::AddKeycode
void AddKeycode(uint16 keycode)
Add a keycode to this hotkey, from now that keycode will be matched in addition to any previously add...
Definition: hotkeys.cpp:273
KeycodeNames
String representation of a keycode.
Definition: hotkeys.cpp:28
window_gui.h
WKC_GLOBAL_HOTKEY
@ WKC_GLOBAL_HOTKEY
Fake keycode bit to indicate global hotkeys.
Definition: gfx_type.h:33
WKC_EQUALS
@ WKC_EQUALS
= Equals
Definition: gfx_type.h:97
HotkeyList::Save
void Save(IniFile *ini) const
Save HotkeyList to IniFile.
Definition: hotkeys.cpp:310
HotkeyList::Load
void Load(IniFile *ini)
Load HotkeyList from IniFile.
Definition: hotkeys.cpp:294
IniItem::value
std::optional< std::string > value
The value of this item.
Definition: ini_type.h:28
KeycodeNames::name
const char * name
Name of the keycode.
Definition: hotkeys.cpp:29
IniFile::SaveToDisk
bool SaveToDisk(const std::string &filename)
Save the Ini file's data to the disk.
Definition: ini.cpp:46
safeguards.h
SaveHotkeysToConfig
void SaveHotkeysToConfig()
Save the hotkeys to the config file.
Definition: hotkeys.cpp:363
stdafx.h
KeycodeNames::keycode
WindowKeyCodes keycode
The keycode.
Definition: hotkeys.cpp:30
WKC_R_BRACKET
@ WKC_R_BRACKET
] Right square bracket
Definition: gfx_type.h:100
string_func.h
WKC_PERIOD
@ WKC_PERIOD
. Period
Definition: gfx_type.h:103
SaveKeycodes
const char * SaveKeycodes(const Hotkey *hotkey)
Convert all keycodes attached to a hotkey to a single string.
Definition: hotkeys.cpp:226
KeycodeToString
static const char * KeycodeToString(uint16 keycode)
Convert a hotkey to it's string representation so it can be written to the config file.
Definition: hotkeys.cpp:172
IniFile
Ini file that supports both loading and saving.
Definition: ini_type.h:88
NO_DIRECTORY
@ NO_DIRECTORY
A path without any base directory.
Definition: fileio_type.h:125
ParseKeycode
static uint16 ParseKeycode(const char *start, const char *end)
Parse a string representation of a keycode.
Definition: hotkeys.cpp:121
Hotkey::Hotkey
Hotkey(uint16 default_keycode, const char *name, int num)
Create a new Hotkey object with a single default keycode.
Definition: hotkeys.cpp:244
WKC_COMMA
@ WKC_COMMA
, Comma
Definition: gfx_type.h:102
openttd.h
IniItem::SetValue
void SetValue(const char *value)
Replace the current value with another value.
Definition: ini_load.cpp:41
HotkeyList::CheckMatch
int CheckMatch(uint16 keycode, bool global_only=false) const
Check if a keycode is bound to something.
Definition: hotkeys.cpp:325
WKC_SEMICOLON
@ WKC_SEMICOLON
; Semicolon
Definition: gfx_type.h:96
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
LoadHotkeysFromConfig
void LoadHotkeysFromConfig()
Load the hotkeys from the config file.
Definition: hotkeys.cpp:357
IniGroup::GetItem
IniItem * GetItem(const std::string &name, bool create)
Get the item with the given name, and if it doesn't exist and create is true it creates a new item.
Definition: ini_load.cpp:95
strecat
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: string.cpp:84
WKC_MINUS
@ WKC_MINUS
Definition: gfx_type.h:104
IniLoadFile::LoadFromDisk
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition: ini_load.cpp:195
_hotkey_lists
static std::vector< HotkeyList * > * _hotkey_lists
List of all HotkeyLists.
Definition: hotkeys.cpp:25
ini_type.h
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
Hotkey
All data for a single hotkey.
Definition: hotkeys.h:22
IniLoadFile::GetGroup
IniGroup * GetGroup(const std::string &name, bool create_new=true)
Get the group with the given name.
Definition: ini_load.cpp:143
hotkeys.h