OpenTTD Source 20250205-master-gfd85ab1e2c
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
19std::string _hotkeys_file;
20
25static std::vector<HotkeyList*> *_hotkey_lists = nullptr;
26
29 const std::string_view name;
31};
32
34static const std::initializer_list<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
97static uint16_t 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 std::string_view str{start, end};
103 for (const auto &kn : _keycode_to_name) {
104 if (StrEqualsIgnoreCase(str, kn.name)) {
105 return kn.keycode;
106 }
107 }
108 if (end - start == 1) {
109 if (*start >= 'a' && *start <= 'z') return *start - ('a'-'A');
110 /* Ignore invalid keycodes */
111 if (*(const uint8_t *)start < 128) return *start;
112 }
113 return 0;
114}
115
122static uint16_t ParseKeycode(const char *start, const char *end)
123{
124 assert(start <= end);
125 uint16_t keycode = 0;
126 for (;;) {
127 const char *cur = start;
128 while (*cur != '+' && cur != end) cur++;
129 uint16_t code = ParseCode(start, cur);
130 if (code == 0) return 0;
131 if (code & WKC_SPECIAL_KEYS) {
132 /* Some completely wrong keycode we don't support. */
133 if (code & ~WKC_SPECIAL_KEYS) return 0;
134 keycode |= code;
135 } else {
136 /* Ignore the code if it has more then 1 letter. */
137 if (keycode & ~WKC_SPECIAL_KEYS) return 0;
138 keycode |= code;
139 }
140 if (cur == end) break;
141 assert(cur < end);
142 start = cur + 1;
143 }
144 return keycode;
145}
146
152static void ParseHotkeys(Hotkey &hotkey, const char *value)
153{
154 const char *start = value;
155 while (*start != '\0') {
156 const char *end = start;
157 while (*end != '\0' && *end != ',') end++;
158 uint16_t keycode = ParseKeycode(start, end);
159 if (keycode != 0) hotkey.AddKeycode(keycode);
160 start = (*end == ',') ? end + 1: end;
161 }
162}
163
171static std::string KeycodeToString(uint16_t keycode)
172{
173 std::string str;
174 if (keycode & WKC_GLOBAL_HOTKEY) {
175 str += "GLOBAL";
176 }
177 if (keycode & WKC_SHIFT) {
178 if (!str.empty()) str += "+";
179 str += "SHIFT";
180 }
181 if (keycode & WKC_CTRL) {
182 if (!str.empty()) str += "+";
183 str += "CTRL";
184 }
185 if (keycode & WKC_ALT) {
186 if (!str.empty()) str += "+";
187 str += "ALT";
188 }
189 if (keycode & WKC_META) {
190 if (!str.empty()) str += "+";
191 str += "META";
192 }
193 if (!str.empty()) str += "+";
194 keycode = keycode & ~WKC_SPECIAL_KEYS;
195
196 for (const auto &kn : _keycode_to_name) {
197 if (kn.keycode == keycode) {
198 str += kn.name;
199 return str;
200 }
201 }
202 assert(keycode < 128);
203 str.push_back(keycode);
204 return str;
205}
206
213std::string SaveKeycodes(const Hotkey &hotkey)
214{
215 std::string str;
216 for (auto keycode : hotkey.keycodes) {
217 if (!str.empty()) str += ",";
218 str += KeycodeToString(keycode);
219 }
220 return str;
221}
222
229Hotkey::Hotkey(uint16_t default_keycode, const std::string &name, int num) :
230 name(name),
231 num(num)
232{
233 if (default_keycode != 0) this->AddKeycode(default_keycode);
234}
235
242Hotkey::Hotkey(const std::vector<uint16_t> &default_keycodes, const std::string &name, int num) :
243 name(name),
244 num(num)
245{
246 for (uint16_t keycode : default_keycodes) {
247 this->AddKeycode(keycode);
248 }
249}
250
256void Hotkey::AddKeycode(uint16_t keycode)
257{
258 this->keycodes.insert(keycode);
259}
260
261HotkeyList::HotkeyList(const std::string &ini_group, const std::vector<Hotkey> &items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
262 global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(items)
263{
264 if (_hotkey_lists == nullptr) _hotkey_lists = new std::vector<HotkeyList*>();
265 _hotkey_lists->push_back(this);
266}
267
268HotkeyList::~HotkeyList()
269{
270 _hotkey_lists->erase(std::ranges::find(*_hotkey_lists, this));
271}
272
277void HotkeyList::Load(const IniFile &ini)
278{
279 const IniGroup *group = ini.GetGroup(this->ini_group);
280 if (group == nullptr) return;
281 for (Hotkey &hotkey : this->items) {
282 const IniItem *item = group->GetItem(hotkey.name);
283 if (item != nullptr) {
284 hotkey.keycodes.clear();
285 if (item->value.has_value()) ParseHotkeys(hotkey, item->value->c_str());
286 }
287 }
288}
289
294void HotkeyList::Save(IniFile &ini) const
295{
296 IniGroup &group = ini.GetOrCreateGroup(this->ini_group);
297 for (const Hotkey &hotkey : this->items) {
298 IniItem &item = group.GetOrCreateItem(hotkey.name);
299 item.SetValue(SaveKeycodes(hotkey));
300 }
301}
302
309int HotkeyList::CheckMatch(uint16_t keycode, bool global_only) const
310{
311 for (const Hotkey &hotkey : this->items) {
312 auto begin = hotkey.keycodes.begin();
313 auto end = hotkey.keycodes.end();
314 if (std::find(begin, end, keycode | WKC_GLOBAL_HOTKEY) != end || (!global_only && std::find(begin, end, keycode) != end)) {
315 return hotkey.num;
316 }
317 }
318 return -1;
319}
320
321
322static void SaveLoadHotkeys(bool save)
323{
324 IniFile ini{};
325 ini.LoadFromDisk(_hotkeys_file, NO_DIRECTORY);
326
327 for (HotkeyList *list : *_hotkey_lists) {
328 if (save) {
329 list->Save(ini);
330 } else {
331 list->Load(ini);
332 }
333 }
334
335 if (save) ini.SaveToDisk(_hotkeys_file);
336}
337
338
341{
342 SaveLoadHotkeys(false);
343}
344
347{
348 SaveLoadHotkeys(true);
349}
350
351void HandleGlobalHotkeys([[maybe_unused]] char32_t key, uint16_t keycode)
352{
353 for (HotkeyList *list : *_hotkey_lists) {
354 if (list->global_hotkey_handler == nullptr) continue;
355
356 int hotkey = list->CheckMatch(keycode, true);
357 if (hotkey >= 0 && (list->global_hotkey_handler(hotkey) == ES_HANDLED)) return;
358 }
359}
360
@ NO_DIRECTORY
A path without any base directory.
WindowKeyCodes
Definition gfx_type.h:27
@ WKC_BACKSLASH
\ Backslash
Definition gfx_type.h:99
@ WKC_MINUS
Definition gfx_type.h:104
@ WKC_COMMA
, Comma
Definition gfx_type.h:102
@ WKC_PERIOD
. Period
Definition gfx_type.h:103
@ WKC_EQUALS
= Equals
Definition gfx_type.h:97
@ WKC_SLASH
/ Forward slash
Definition gfx_type.h:95
@ WKC_SINGLEQUOTE
' Single quote
Definition gfx_type.h:101
@ WKC_R_BRACKET
] Right square bracket
Definition gfx_type.h:100
@ WKC_L_BRACKET
[ Left square bracket
Definition gfx_type.h:98
@ WKC_GLOBAL_HOTKEY
Fake keycode bit to indicate global hotkeys.
Definition gfx_type.h:33
@ WKC_SEMICOLON
; Semicolon
Definition gfx_type.h:96
static std::vector< HotkeyList * > * _hotkey_lists
List of all HotkeyLists.
Definition hotkeys.cpp:25
static uint16_t ParseKeycode(const char *start, const char *end)
Parse a string representation of a keycode.
Definition hotkeys.cpp:122
void LoadHotkeysFromConfig()
Load the hotkeys from the config file.
Definition hotkeys.cpp:340
static void ParseHotkeys(Hotkey &hotkey, const char *value)
Parse a string to the keycodes it represents.
Definition hotkeys.cpp:152
std::string SaveKeycodes(const Hotkey &hotkey)
Convert all keycodes attached to a hotkey to a single string.
Definition hotkeys.cpp:213
static uint16_t ParseCode(const char *start, const char *end)
Try to parse a single part of a keycode.
Definition hotkeys.cpp:97
void SaveHotkeysToConfig()
Save the hotkeys to the config file.
Definition hotkeys.cpp:346
static std::string KeycodeToString(uint16_t keycode)
Convert a hotkey to it's string representation so it can be written to the config file.
Definition hotkeys.cpp:171
static const std::initializer_list< KeycodeNames > _keycode_to_name
Array of non-standard keycodes that can be used in the hotkeys config file.
Definition hotkeys.cpp:34
Hotkey related functions.
Types related to reading/writing '*.ini' files.
Some generic types.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
bool StrEqualsIgnoreCase(const std::string_view str1, const std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Definition string.cpp:347
Functions related to low-level strings.
List of hotkeys for a window.
Definition hotkeys.h:37
void Save(IniFile &ini) const
Save HotkeyList to IniFile.
Definition hotkeys.cpp:294
int CheckMatch(uint16_t keycode, bool global_only=false) const
Check if a keycode is bound to something.
Definition hotkeys.cpp:309
void Load(const IniFile &ini)
Load HotkeyList from IniFile.
Definition hotkeys.cpp:277
All data for a single hotkey.
Definition hotkeys.h:21
Hotkey(uint16_t default_keycode, const std::string &name, int num)
Create a new Hotkey object with a single default keycode.
Definition hotkeys.cpp:229
void AddKeycode(uint16_t keycode)
Add a keycode to this hotkey, from now that keycode will be matched in addition to any previously add...
Definition hotkeys.cpp:256
Ini file that supports both loading and saving.
Definition ini_type.h:88
A group within an ini file.
Definition ini_type.h:34
const IniItem * GetItem(std::string_view name) const
Get the item with the given name.
Definition ini_load.cpp:52
IniItem & GetOrCreateItem(std::string_view name)
Get the item with the given name, and if it doesn't exist create a new item.
Definition ini_load.cpp:66
A single "line" in an ini file.
Definition ini_type.h:23
std::optional< std::string > value
The value of this item.
Definition ini_type.h:25
void SetValue(std::string_view value)
Replace the current value with another value.
Definition ini_load.cpp:32
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
Definition ini_load.cpp:187
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
Definition ini_load.cpp:119
IniGroup & GetOrCreateGroup(std::string_view name)
Get the group with the given name, and if it doesn't exist create a new group.
Definition ini_load.cpp:147
String representation of a keycode.
Definition hotkeys.cpp:28
WindowKeyCodes keycode
The keycode.
Definition hotkeys.cpp:30
const std::string_view name
Name of the keycode.
Definition hotkeys.cpp:29
Functions, definitions and such used only by the GUI.
@ ES_HANDLED
The passed event is handled.