19 std::string _hotkeys_file;
29 const std::string_view
name;
41 {
"BACKSPACE", WKC_BACKSPACE},
44 {
"PAGEUP", WKC_PAGEUP},
45 {
"PAGEDOWN", WKC_PAGEDOWN},
48 {
"RETURN", WKC_RETURN},
62 {
"BACKQUOTE", WKC_BACKQUOTE},
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},
97 static uint16_t
ParseCode(
const char *start,
const char *end)
100 while (start < end && *start ==
' ') start++;
101 while (end > start && *end ==
' ') end--;
102 std::string_view str{start, end};
108 if (end - start == 1) {
109 if (*start >=
'a' && *start <=
'z')
return *start - (
'a'-
'A');
111 if (*(
const uint8_t *)start < 128)
return *start;
124 assert(start <= end);
125 uint16_t keycode = 0;
127 const char *cur = start;
128 while (*cur !=
'+' && cur != end) cur++;
130 if (code == 0)
return 0;
131 if (code & WKC_SPECIAL_KEYS) {
133 if (code & ~WKC_SPECIAL_KEYS)
return 0;
137 if (keycode & ~WKC_SPECIAL_KEYS)
return 0;
140 if (cur == end)
break;
154 const char *start = value;
155 while (*start !=
'\0') {
156 const char *end = start;
157 while (*end !=
'\0' && *end !=
',') end++;
160 start = (*end ==
',') ? end + 1: end;
177 if (keycode & WKC_SHIFT) {
178 if (!str.empty()) str +=
"+";
181 if (keycode & WKC_CTRL) {
182 if (!str.empty()) str +=
"+";
185 if (keycode & WKC_ALT) {
186 if (!str.empty()) str +=
"+";
189 if (keycode & WKC_META) {
190 if (!str.empty()) str +=
"+";
193 if (!str.empty()) str +=
"+";
194 keycode = keycode & ~WKC_SPECIAL_KEYS;
197 if (kn.keycode == keycode) {
202 assert(keycode < 128);
203 str.push_back(keycode);
216 for (
auto keycode : hotkey.keycodes) {
217 if (!str.empty()) str +=
",";
233 if (default_keycode != 0) this->
AddKeycode(default_keycode);
242 Hotkey::Hotkey(
const std::vector<uint16_t> &default_keycodes,
const std::string &name,
int num) :
246 for (uint16_t keycode : default_keycodes) {
258 this->keycodes.insert(keycode);
261 HotkeyList::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)
268 HotkeyList::~HotkeyList()
280 if (group ==
nullptr)
return;
281 for (
Hotkey &hotkey : this->items) {
283 if (item !=
nullptr) {
284 hotkey.keycodes.clear();
297 for (
const Hotkey &hotkey : this->items) {
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)) {
322 static void SaveLoadHotkeys(
bool save)
335 if (save) ini.SaveToDisk(_hotkeys_file);
342 SaveLoadHotkeys(
false);
348 SaveLoadHotkeys(
true);
351 void HandleGlobalHotkeys([[maybe_unused]] char32_t key, uint16_t keycode)
354 if (list->global_hotkey_handler ==
nullptr)
continue;
356 int hotkey = list->CheckMatch(keycode,
true);
357 if (hotkey >= 0 && (list->global_hotkey_handler(hotkey) ==
ES_HANDLED))
return;
@ NO_DIRECTORY
A path without any base directory.
@ WKC_BACKSLASH
\ Backslash
@ WKC_SLASH
/ Forward slash
@ WKC_SINGLEQUOTE
' Single quote
@ WKC_R_BRACKET
] Right square bracket
@ WKC_L_BRACKET
[ Left square bracket
@ WKC_GLOBAL_HOTKEY
Fake keycode bit to indicate global hotkeys.
@ WKC_SEMICOLON
; Semicolon
static std::vector< HotkeyList * > * _hotkey_lists
List of all HotkeyLists.
static uint16_t ParseKeycode(const char *start, const char *end)
Parse a string representation of a keycode.
void LoadHotkeysFromConfig()
Load the hotkeys from the config file.
static void ParseHotkeys(Hotkey &hotkey, const char *value)
Parse a string to the keycodes it represents.
std::string SaveKeycodes(const Hotkey &hotkey)
Convert all keycodes attached to a hotkey to a single string.
static uint16_t ParseCode(const char *start, const char *end)
Try to parse a single part of a keycode.
void SaveHotkeysToConfig()
Save the hotkeys to the config file.
static std::string KeycodeToString(uint16_t keycode)
Convert a hotkey to it's string representation so it can be written to the config file.
static const std::initializer_list< KeycodeNames > _keycode_to_name
Array of non-standard keycodes that can be used in the hotkeys config file.
Hotkey related functions.
Types related to reading/writing '*.ini' files.
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.
Functions related to low-level strings.
List of hotkeys for a window.
void Save(IniFile &ini) const
Save HotkeyList to IniFile.
int CheckMatch(uint16_t keycode, bool global_only=false) const
Check if a keycode is bound to something.
void Load(const IniFile &ini)
Load HotkeyList from IniFile.
All data for a single hotkey.
Hotkey(uint16_t default_keycode, const std::string &name, int num)
Create a new Hotkey object with a single default keycode.
void AddKeycode(uint16_t keycode)
Add a keycode to this hotkey, from now that keycode will be matched in addition to any previously add...
Ini file that supports both loading and saving.
A group within an ini file.
const IniItem * GetItem(std::string_view name) const
Get the item with the given name.
IniItem & GetOrCreateItem(std::string_view name)
Get the item with the given name, and if it doesn't exist create a new item.
A single "line" in an ini file.
std::optional< std::string > value
The value of this item.
void SetValue(std::string_view value)
Replace the current value with another value.
void LoadFromDisk(const std::string &filename, Subdirectory subdir)
Load the Ini file's data from the disk.
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
IniGroup & GetOrCreateGroup(std::string_view name)
Get the group with the given name, and if it doesn't exist create a new group.
String representation of a keycode.
WindowKeyCodes keycode
The keycode.
const std::string_view name
Name of the keycode.
Functions, definitions and such used only by the GUI.
@ ES_HANDLED
The passed event is handled.