2010-07-03 13:27:32 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file hotkeys.cpp Implementation of hotkey related functions */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
2010-07-03 21:43:44 +00:00
|
|
|
#include "openttd.h"
|
2010-07-03 13:27:32 +00:00
|
|
|
#include "hotkeys.h"
|
2010-07-03 13:28:15 +00:00
|
|
|
#include "ini_type.h"
|
|
|
|
#include "string_func.h"
|
2010-07-03 21:43:44 +00:00
|
|
|
#include "window_gui.h"
|
2010-07-03 13:27:32 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2020-12-06 20:11:42 +00:00
|
|
|
std::string _hotkeys_file;
|
2010-07-03 13:28:15 +00:00
|
|
|
|
2013-06-15 15:28:09 +00:00
|
|
|
/**
|
|
|
|
* List of all HotkeyLists.
|
|
|
|
* This is a pointer to ensure initialisation order with the various static HotkeyList instances.
|
|
|
|
*/
|
2019-04-10 21:07:06 +00:00
|
|
|
static std::vector<HotkeyList*> *_hotkey_lists = nullptr;
|
2013-06-15 15:28:09 +00:00
|
|
|
|
2010-07-03 13:28:15 +00:00
|
|
|
/** String representation of a keycode */
|
|
|
|
struct KeycodeNames {
|
|
|
|
const char *name; ///< Name of the keycode
|
|
|
|
WindowKeyCodes keycode; ///< The keycode
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Array of non-standard keycodes that can be used in the hotkeys config file. */
|
|
|
|
static const KeycodeNames _keycode_to_name[] = {
|
|
|
|
{"SHIFT", WKC_SHIFT},
|
|
|
|
{"CTRL", WKC_CTRL},
|
|
|
|
{"ALT", WKC_ALT},
|
|
|
|
{"META", WKC_META},
|
2010-07-03 20:14:56 +00:00
|
|
|
{"GLOBAL", WKC_GLOBAL_HOTKEY},
|
2010-07-03 13:28:15 +00:00
|
|
|
{"ESC", WKC_ESC},
|
2018-09-16 08:10:26 +00:00
|
|
|
{"BACKSPACE", WKC_BACKSPACE},
|
2019-12-27 08:32:34 +00:00
|
|
|
{"INS", WKC_INSERT},
|
|
|
|
{"DEL", WKC_DELETE},
|
|
|
|
{"PAGEUP", WKC_PAGEUP},
|
|
|
|
{"PAGEDOWN", WKC_PAGEDOWN},
|
|
|
|
{"END", WKC_END},
|
|
|
|
{"HOME", WKC_HOME},
|
2010-07-03 13:28:15 +00:00
|
|
|
{"RETURN", WKC_RETURN},
|
2019-12-27 08:32:34 +00:00
|
|
|
{"SPACE", WKC_SPACE},
|
2010-07-03 13:28:15 +00:00
|
|
|
{"F1", WKC_F1},
|
|
|
|
{"F2", WKC_F2},
|
|
|
|
{"F3", WKC_F3},
|
|
|
|
{"F4", WKC_F4},
|
|
|
|
{"F5", WKC_F5},
|
|
|
|
{"F6", WKC_F6},
|
|
|
|
{"F7", WKC_F7},
|
|
|
|
{"F8", WKC_F8},
|
|
|
|
{"F9", WKC_F9},
|
|
|
|
{"F10", WKC_F10},
|
|
|
|
{"F11", WKC_F11},
|
|
|
|
{"F12", WKC_F12},
|
2019-12-27 08:32:34 +00:00
|
|
|
{"BACKQUOTE", WKC_BACKQUOTE},
|
2010-07-03 13:28:15 +00:00
|
|
|
{"PAUSE", WKC_PAUSE},
|
2019-12-27 08:32:34 +00:00
|
|
|
{"NUM_DIV", WKC_NUM_DIV},
|
|
|
|
{"NUM_MUL", WKC_NUM_MUL},
|
2010-07-04 09:35:06 +00:00
|
|
|
{"NUM_MINUS", WKC_NUM_MINUS},
|
2019-12-27 08:32:34 +00:00
|
|
|
{"NUM_PLUS", WKC_NUM_PLUS},
|
|
|
|
{"NUM_ENTER", WKC_NUM_ENTER},
|
|
|
|
{"NUM_DOT", WKC_NUM_DECIMAL},
|
2020-08-10 06:56:01 +00:00
|
|
|
{"SLASH", WKC_SLASH},
|
|
|
|
{"/", WKC_SLASH}, /* deprecated, use SLASH */
|
|
|
|
{"SEMICOLON", WKC_SEMICOLON},
|
|
|
|
{";", WKC_SEMICOLON}, /* deprecated, use SEMICOLON */
|
|
|
|
{"EQUALS", WKC_EQUALS},
|
|
|
|
{"=", WKC_EQUALS}, /* deprecated, use EQUALS */
|
|
|
|
{"L_BRACKET", WKC_L_BRACKET},
|
|
|
|
{"[", WKC_L_BRACKET}, /* deprecated, use L_BRACKET */
|
|
|
|
{"BACKSLASH", WKC_BACKSLASH},
|
|
|
|
{"\\", WKC_BACKSLASH}, /* deprecated, use BACKSLASH */
|
|
|
|
{"R_BRACKET", WKC_R_BRACKET},
|
|
|
|
{"]", WKC_R_BRACKET}, /* deprecated, use R_BRACKET */
|
|
|
|
{"SINGLEQUOTE", WKC_SINGLEQUOTE},
|
|
|
|
{"'", WKC_SINGLEQUOTE}, /* deprecated, use SINGLEQUOTE */
|
|
|
|
{"COMMA", WKC_COMMA},
|
|
|
|
{"PERIOD", WKC_PERIOD},
|
|
|
|
{".", WKC_PERIOD}, /* deprecated, use PERIOD */
|
|
|
|
{"MINUS", WKC_MINUS},
|
|
|
|
{"-", WKC_MINUS}, /* deprecated, use MINUS */
|
2020-09-21 02:39:02 +00:00
|
|
|
{"HASH", WKC_HASH},
|
2015-08-02 18:37:42 +00:00
|
|
|
{"PAGE_UP", WKC_PAGEUP},
|
|
|
|
{"PAGE_DOWN", WKC_PAGEDOWN},
|
2010-07-03 13:28:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to parse a single part of a keycode.
|
|
|
|
* @param start Start of the string to parse.
|
|
|
|
* @param end End of the string to parse.
|
|
|
|
* @return A keycode if a match is found or 0.
|
|
|
|
*/
|
|
|
|
static uint16 ParseCode(const char *start, const char *end)
|
|
|
|
{
|
|
|
|
assert(start <= end);
|
|
|
|
while (start < end && *start == ' ') start++;
|
|
|
|
while (end > start && *end == ' ') end--;
|
|
|
|
for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
|
2010-08-01 10:53:39 +00:00
|
|
|
if (strlen(_keycode_to_name[i].name) == (size_t)(end - start) && strncasecmp(start, _keycode_to_name[i].name, end - start) == 0) {
|
2010-07-03 13:28:15 +00:00
|
|
|
return _keycode_to_name[i].keycode;
|
|
|
|
}
|
|
|
|
}
|
2010-07-31 23:33:09 +00:00
|
|
|
if (end - start == 1) {
|
2011-01-26 08:14:36 +00:00
|
|
|
if (*start >= 'a' && *start <= 'z') return *start - ('a'-'A');
|
2011-02-25 13:59:02 +00:00
|
|
|
/* Ignore invalid keycodes */
|
2011-11-12 08:10:22 +00:00
|
|
|
if (*(const uint8 *)start < 128) return *start;
|
2010-07-31 23:33:09 +00:00
|
|
|
}
|
2010-07-03 13:28:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a string representation of a keycode.
|
|
|
|
* @param start Start of the input.
|
|
|
|
* @param end End of the input.
|
|
|
|
* @return A valid keycode or 0.
|
|
|
|
*/
|
|
|
|
static uint16 ParseKeycode(const char *start, const char *end)
|
|
|
|
{
|
|
|
|
assert(start <= end);
|
|
|
|
uint16 keycode = 0;
|
2011-02-25 21:53:43 +00:00
|
|
|
for (;;) {
|
2010-07-03 13:28:15 +00:00
|
|
|
const char *cur = start;
|
|
|
|
while (*cur != '+' && cur != end) cur++;
|
|
|
|
uint16 code = ParseCode(start, cur);
|
|
|
|
if (code == 0) return 0;
|
|
|
|
if (code & WKC_SPECIAL_KEYS) {
|
2011-02-17 23:14:43 +00:00
|
|
|
/* Some completely wrong keycode we don't support. */
|
|
|
|
if (code & ~WKC_SPECIAL_KEYS) return 0;
|
2010-07-03 13:28:15 +00:00
|
|
|
keycode |= code;
|
|
|
|
} else {
|
|
|
|
/* Ignore the code if it has more then 1 letter. */
|
|
|
|
if (keycode & ~WKC_SPECIAL_KEYS) return 0;
|
|
|
|
keycode |= code;
|
|
|
|
}
|
|
|
|
if (cur == end) break;
|
|
|
|
assert(cur < end);
|
|
|
|
start = cur + 1;
|
|
|
|
}
|
|
|
|
return keycode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a string to the keycodes it represents
|
|
|
|
* @param hotkey The hotkey object to add the keycodes to
|
|
|
|
* @param value The string to parse
|
|
|
|
*/
|
2013-06-15 15:27:33 +00:00
|
|
|
static void ParseHotkeys(Hotkey *hotkey, const char *value)
|
2010-07-03 13:28:15 +00:00
|
|
|
{
|
|
|
|
const char *start = value;
|
|
|
|
while (*start != '\0') {
|
|
|
|
const char *end = start;
|
|
|
|
while (*end != '\0' && *end != ',') end++;
|
|
|
|
uint16 keycode = ParseKeycode(start, end);
|
|
|
|
if (keycode != 0) hotkey->AddKeycode(keycode);
|
|
|
|
start = (*end == ',') ? end + 1: end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a hotkey to it's string representation so it can be written to the
|
2013-01-08 22:46:42 +00:00
|
|
|
* config file. Separate parts of the keycode (like "CTRL" and "F1" are split
|
2010-07-03 13:28:15 +00:00
|
|
|
* by a '+'.
|
|
|
|
* @param keycode The keycode to convert to a string.
|
|
|
|
* @return A string representation of this keycode.
|
2014-04-25 15:40:32 +00:00
|
|
|
* @note The return value is a static buffer, stredup the result before calling
|
2010-07-03 13:28:15 +00:00
|
|
|
* this function again.
|
|
|
|
*/
|
|
|
|
static const char *KeycodeToString(uint16 keycode)
|
|
|
|
{
|
|
|
|
static char buf[32];
|
|
|
|
buf[0] = '\0';
|
|
|
|
bool first = true;
|
2010-07-03 20:40:18 +00:00
|
|
|
if (keycode & WKC_GLOBAL_HOTKEY) {
|
|
|
|
strecat(buf, "GLOBAL", lastof(buf));
|
|
|
|
first = false;
|
|
|
|
}
|
2010-07-03 13:28:15 +00:00
|
|
|
if (keycode & WKC_SHIFT) {
|
2010-07-03 20:40:18 +00:00
|
|
|
if (!first) strecat(buf, "+", lastof(buf));
|
2010-07-03 13:28:15 +00:00
|
|
|
strecat(buf, "SHIFT", lastof(buf));
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
if (keycode & WKC_CTRL) {
|
|
|
|
if (!first) strecat(buf, "+", lastof(buf));
|
|
|
|
strecat(buf, "CTRL", lastof(buf));
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
if (keycode & WKC_ALT) {
|
|
|
|
if (!first) strecat(buf, "+", lastof(buf));
|
|
|
|
strecat(buf, "ALT", lastof(buf));
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
if (keycode & WKC_META) {
|
|
|
|
if (!first) strecat(buf, "+", lastof(buf));
|
|
|
|
strecat(buf, "META", lastof(buf));
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
if (!first) strecat(buf, "+", lastof(buf));
|
|
|
|
keycode = keycode & ~WKC_SPECIAL_KEYS;
|
|
|
|
|
|
|
|
for (uint i = 0; i < lengthof(_keycode_to_name); i++) {
|
|
|
|
if (_keycode_to_name[i].keycode == keycode) {
|
|
|
|
strecat(buf, _keycode_to_name[i].name, lastof(buf));
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(keycode < 128);
|
|
|
|
char key[2];
|
|
|
|
key[0] = keycode;
|
|
|
|
key[1] = '\0';
|
|
|
|
strecat(buf, key, lastof(buf));
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert all keycodes attached to a hotkey to a single string. If multiple
|
|
|
|
* keycodes are attached to the hotkey they are split by a comma.
|
|
|
|
* @param hotkey The keycodes of this hotkey need to be converted to a string.
|
|
|
|
* @return A string representation of all keycodes.
|
2014-04-25 15:40:32 +00:00
|
|
|
* @note The return value is a static buffer, stredup the result before calling
|
2010-07-03 13:28:15 +00:00
|
|
|
* this function again.
|
|
|
|
*/
|
2013-06-15 15:27:33 +00:00
|
|
|
const char *SaveKeycodes(const Hotkey *hotkey)
|
2010-07-03 13:28:15 +00:00
|
|
|
{
|
|
|
|
static char buf[128];
|
|
|
|
buf[0] = '\0';
|
2018-09-23 11:23:54 +00:00
|
|
|
for (uint i = 0; i < hotkey->keycodes.size(); i++) {
|
2010-07-03 13:28:15 +00:00
|
|
|
const char *str = KeycodeToString(hotkey->keycodes[i]);
|
|
|
|
if (i > 0) strecat(buf, ",", lastof(buf));
|
|
|
|
strecat(buf, str, lastof(buf));
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2013-06-15 15:27:33 +00:00
|
|
|
/**
|
|
|
|
* Create a new Hotkey object with a single default keycode.
|
|
|
|
* @param default_keycode The default keycode for this hotkey.
|
|
|
|
* @param name The name of this hotkey.
|
|
|
|
* @param num Number of this hotkey, should be unique within the hotkey list.
|
|
|
|
*/
|
|
|
|
Hotkey::Hotkey(uint16 default_keycode, const char *name, int num) :
|
|
|
|
name(name),
|
|
|
|
num(num)
|
|
|
|
{
|
|
|
|
if (default_keycode != 0) this->AddKeycode(default_keycode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new Hotkey object with multiple default keycodes.
|
|
|
|
* @param default_keycodes An array of default keycodes terminated with 0.
|
|
|
|
* @param name The name of this hotkey.
|
|
|
|
* @param num Number of this hotkey, should be unique within the hotkey list.
|
|
|
|
*/
|
|
|
|
Hotkey::Hotkey(const uint16 *default_keycodes, const char *name, int num) :
|
|
|
|
name(name),
|
|
|
|
num(num)
|
|
|
|
{
|
|
|
|
const uint16 *keycode = default_keycodes;
|
|
|
|
while (*keycode != 0) {
|
|
|
|
this->AddKeycode(*keycode);
|
|
|
|
keycode++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a keycode to this hotkey, from now that keycode will be matched
|
|
|
|
* in addition to any previously added keycodes.
|
|
|
|
* @param keycode The keycode to add.
|
|
|
|
*/
|
|
|
|
void Hotkey::AddKeycode(uint16 keycode)
|
|
|
|
{
|
2019-02-20 21:35:41 +00:00
|
|
|
include(this->keycodes, keycode);
|
2013-06-15 15:27:33 +00:00
|
|
|
}
|
|
|
|
|
2013-06-15 15:31:22 +00:00
|
|
|
HotkeyList::HotkeyList(const char *ini_group, Hotkey *items, GlobalHotkeyHandlerFunc global_hotkey_handler) :
|
|
|
|
global_hotkey_handler(global_hotkey_handler), ini_group(ini_group), items(items)
|
2013-06-15 15:28:09 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (_hotkey_lists == nullptr) _hotkey_lists = new std::vector<HotkeyList*>();
|
2019-02-18 22:39:06 +00:00
|
|
|
_hotkey_lists->push_back(this);
|
2013-06-15 15:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HotkeyList::~HotkeyList()
|
2010-07-03 13:28:15 +00:00
|
|
|
{
|
2018-09-23 16:36:45 +00:00
|
|
|
_hotkey_lists->erase(std::find(_hotkey_lists->begin(), _hotkey_lists->end(), this));
|
2013-06-15 15:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load HotkeyList from IniFile.
|
|
|
|
* @param ini IniFile to load from.
|
|
|
|
*/
|
|
|
|
void HotkeyList::Load(IniFile *ini)
|
|
|
|
{
|
|
|
|
IniGroup *group = ini->GetGroup(this->ini_group);
|
2019-04-10 21:07:06 +00:00
|
|
|
for (Hotkey *hotkey = this->items; hotkey->name != nullptr; ++hotkey) {
|
2010-07-03 13:28:15 +00:00
|
|
|
IniItem *item = group->GetItem(hotkey->name, false);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (item != nullptr) {
|
2018-09-20 22:44:14 +00:00
|
|
|
hotkey->keycodes.clear();
|
2020-05-17 21:32:03 +00:00
|
|
|
if (item->value.has_value()) ParseHotkeys(hotkey, item->value->c_str());
|
2010-07-03 13:28:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 15:28:09 +00:00
|
|
|
/**
|
|
|
|
* Save HotkeyList to IniFile.
|
|
|
|
* @param ini IniFile to save to.
|
|
|
|
*/
|
|
|
|
void HotkeyList::Save(IniFile *ini) const
|
2010-07-03 13:28:15 +00:00
|
|
|
{
|
2013-06-15 15:28:09 +00:00
|
|
|
IniGroup *group = ini->GetGroup(this->ini_group);
|
2019-04-10 21:07:06 +00:00
|
|
|
for (const Hotkey *hotkey = this->items; hotkey->name != nullptr; ++hotkey) {
|
2010-07-03 13:28:15 +00:00
|
|
|
IniItem *item = group->GetItem(hotkey->name, true);
|
2010-07-03 21:18:02 +00:00
|
|
|
item->SetValue(SaveKeycodes(hotkey));
|
2010-07-03 13:28:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 15:27:33 +00:00
|
|
|
/**
|
|
|
|
* Check if a keycode is bound to something.
|
|
|
|
* @param keycode The keycode that was pressed
|
|
|
|
* @param global_only Limit the search to hotkeys defined as 'global'.
|
|
|
|
* @return The number of the matching hotkey or -1.
|
|
|
|
*/
|
2013-06-15 15:28:09 +00:00
|
|
|
int HotkeyList::CheckMatch(uint16 keycode, bool global_only) const
|
2013-06-15 15:27:33 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
for (const Hotkey *list = this->items; list->name != nullptr; ++list) {
|
2019-02-12 22:59:12 +00:00
|
|
|
auto begin = list->keycodes.begin();
|
|
|
|
auto end = list->keycodes.end();
|
|
|
|
if (std::find(begin, end, keycode | WKC_GLOBAL_HOTKEY) != end || (!global_only && std::find(begin, end, keycode) != end)) {
|
2013-06-15 15:27:33 +00:00
|
|
|
return list->num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-07-03 13:28:15 +00:00
|
|
|
|
|
|
|
static void SaveLoadHotkeys(bool save)
|
|
|
|
{
|
|
|
|
IniFile *ini = new IniFile();
|
2017-03-20 19:30:49 +00:00
|
|
|
ini->LoadFromDisk(_hotkeys_file, NO_DIRECTORY);
|
2010-07-03 13:28:15 +00:00
|
|
|
|
2019-02-17 11:20:52 +00:00
|
|
|
for (HotkeyList *list : *_hotkey_lists) {
|
2013-06-15 15:28:09 +00:00
|
|
|
if (save) {
|
2019-02-17 11:20:52 +00:00
|
|
|
list->Save(ini);
|
2013-06-15 15:28:09 +00:00
|
|
|
} else {
|
2019-02-17 11:20:52 +00:00
|
|
|
list->Load(ini);
|
2013-06-15 15:28:09 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-03 13:28:15 +00:00
|
|
|
|
|
|
|
if (save) ini->SaveToDisk(_hotkeys_file);
|
|
|
|
delete ini;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Load the hotkeys from the config file */
|
|
|
|
void LoadHotkeysFromConfig()
|
|
|
|
{
|
|
|
|
SaveLoadHotkeys(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Save the hotkeys to the config file */
|
|
|
|
void SaveHotkeysToConfig()
|
|
|
|
{
|
|
|
|
SaveLoadHotkeys(true);
|
|
|
|
}
|
2010-07-03 13:27:32 +00:00
|
|
|
|
2013-08-05 20:36:28 +00:00
|
|
|
void HandleGlobalHotkeys(WChar key, uint16 keycode)
|
2010-07-03 21:43:44 +00:00
|
|
|
{
|
2019-02-17 11:20:52 +00:00
|
|
|
for (HotkeyList *list : *_hotkey_lists) {
|
2019-04-10 21:07:06 +00:00
|
|
|
if (list->global_hotkey_handler == nullptr) continue;
|
2013-06-15 15:31:22 +00:00
|
|
|
|
2019-02-17 11:20:52 +00:00
|
|
|
int hotkey = list->CheckMatch(keycode, true);
|
|
|
|
if (hotkey >= 0 && (list->global_hotkey_handler(hotkey) == ES_HANDLED)) return;
|
2010-07-03 21:43:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|