22std::string _hotkeys_file;
32 const std::string_view
name;
44 {
"BACKSPACE", WKC_BACKSPACE},
47 {
"PAGEUP", WKC_PAGEUP},
48 {
"PAGEDOWN", WKC_PAGEDOWN},
51 {
"RETURN", WKC_RETURN},
65 {
"BACKQUOTE", WKC_BACKQUOTE},
67 {
"NUM_DIV", WKC_NUM_DIV},
68 {
"NUM_MUL", WKC_NUM_MUL},
69 {
"NUM_MINUS", WKC_NUM_MINUS},
70 {
"NUM_PLUS", WKC_NUM_PLUS},
71 {
"NUM_ENTER", WKC_NUM_ENTER},
72 {
"NUM_DOT", WKC_NUM_DECIMAL},
99static std::optional<uint16_t>
ParseCode(std::string_view keystr)
107 if (keystr.size() == 1) {
109 if (c >=
'a' && c <=
'z')
return c - (
'a'-
'A');
111 if (
static_cast<uint8_t
>(c) < 128)
return c;
123 if (keystr.empty())
return std::nullopt;
125 uint16_t keycode = 0;
129 if (!code.has_value())
return std::nullopt;
130 if (*code & WKC_SPECIAL_KEYS) {
132 if (*code & ~WKC_SPECIAL_KEYS)
return std::nullopt;
136 if (keycode & ~WKC_SPECIAL_KEYS)
return std::nullopt;
154 if (keycode.has_value()) hotkey.
AddKeycode(*keycode);
171 if (keycode & WKC_SHIFT) {
172 if (!str.empty()) str +=
"+";
175 if (keycode & WKC_CTRL) {
176 if (!str.empty()) str +=
"+";
179 if (keycode & WKC_ALT) {
180 if (!str.empty()) str +=
"+";
183 if (keycode & WKC_META) {
184 if (!str.empty()) str +=
"+";
187 if (!str.empty()) str +=
"+";
188 keycode = keycode & ~WKC_SPECIAL_KEYS;
191 if (kn.keycode == keycode) {
196 assert(keycode < 128);
197 str.push_back(keycode);
210 for (
auto keycode : hotkey.keycodes) {
211 if (!str.empty()) str +=
",";
227 if (default_keycode != 0) this->
AddKeycode(default_keycode);
236Hotkey::Hotkey(
const std::vector<uint16_t> &default_keycodes,
const std::string &name,
int num) :
240 for (uint16_t keycode : default_keycodes) {
252 this->keycodes.insert(keycode);
255HotkeyList::HotkeyList(
const std::string &ini_group,
const std::vector<Hotkey> &items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
256 global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(items)
275 if (group ==
nullptr)
return;
276 for (
Hotkey &hotkey : this->items) {
278 if (item !=
nullptr) {
279 hotkey.keycodes.clear();
292 for (
const Hotkey &hotkey : this->items) {
306 for (
const Hotkey &hotkey : this->items) {
307 auto begin = hotkey.keycodes.begin();
308 auto end = hotkey.keycodes.end();
309 if (std::find(begin, end, keycode |
WKC_GLOBAL_HOTKEY) != end || (!global_only && std::find(begin, end, keycode) != end)) {
317static void SaveLoadHotkeys(
bool save)
337 SaveLoadHotkeys(
false);
343 SaveLoadHotkeys(
true);
362void HandleGlobalHotkeys([[maybe_unused]]
char32_t key, uint16_t keycode)
365 if (list->global_hotkey_handler ==
nullptr)
continue;
367 int hotkey = list->CheckMatch(keycode,
true);
Class for backupping variables and making sure they are restored later.
Parse data from a string / buffer.
std::string_view ReadUntilChar(char c, SeparatorUsage sep)
Read data until the first occurrence of 8-bit char 'c', and advance reader.
@ SKIP_ONE_SEPARATOR
Read and discard one separator, do not include it in the result.
bool AnyBytesLeft() const noexcept
Check whether any bytes left to read.
static const std::string_view WHITESPACE_NO_NEWLINE
ASCII whitespace characters, excluding new-line.
@ None
A path without any base directory.
bool _shift_pressed
Is Shift pressed?
bool _ctrl_pressed
Is Ctrl pressed?
Functions related to the gfx engine.
@ 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 EventState HandleGlobalHotkey(HotkeyList::GlobalHotkeyHandlerFunc func, int hotkey)
Call the global handler for a hotkey.
static std::vector< HotkeyList * > * _hotkey_lists
List of all HotkeyLists.
void LoadHotkeysFromConfig()
Load the hotkeys from the config file.
static std::optional< uint16_t > ParseCode(std::string_view keystr)
Try to parse a single part of a keycode.
static void ParseHotkeys(Hotkey &hotkey, std::string_view 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.
void SaveHotkeysToConfig()
Save the hotkeys to the config file.
static std::optional< uint16_t > ParseKeycode(std::string_view keystr)
Parse a string representation of a keycode.
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(std::string_view str1, std::string_view str2)
Compares two string( view)s for equality, while ignoring the case of the characters.
Functions related to low-level strings.
Class to backup a specific variable and restore it upon destruction of this object to prevent stack v...
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.
~HotkeyList()
Remove ourselves from the global hotkey list.
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.
bool SaveToDisk(const std::string &filename)
Save the Ini file's data to the disk.
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.
const IniGroup * GetGroup(std::string_view name) const
Get the group with the given name.
void LoadFromDisk(std::string_view filename, Subdirectory subdir)
Load the Ini file's data from the disk.
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.
EventState
State of handling an event.
@ Handled
The passed event is handled.