dfhack/plugins/hotkeys.cpp

205 lines
6.5 KiB
C++

#include <map>
#include <string>
#include <vector>
2014-06-01 21:48:34 -06:00
#include "modules/Gui.h"
#include "modules/Screen.h"
2014-06-01 21:48:34 -06:00
2022-11-07 11:01:49 -07:00
#include "Debug.h"
2022-07-15 10:44:24 -06:00
#include "LuaTools.h"
2014-06-01 21:48:34 -06:00
#include "PluginManager.h"
DFHACK_PLUGIN("hotkeys");
2022-11-07 11:01:49 -07:00
namespace DFHack {
DBG_DECLARE(hotkeys, log, DebugCategory::LINFO);
}
using std::map;
using std::string;
using std::vector;
using namespace DFHack;
static const string INVOKE_MENU_COMMAND = "overlay trigger hotkeys.menu";
static const string INVOKE_HOTKEYS_COMMAND = "hotkeys";
static const std::string MENU_SCREEN_FOCUS_STRING = "dfhack/lua/hotkeys/menu";
2014-06-01 21:48:34 -06:00
2022-11-09 13:16:44 -07:00
static bool valid = false; // whether the following two vars contain valid data
static string current_focus;
2014-06-01 21:48:34 -06:00
static map<string, string> current_bindings;
static vector<string> sorted_keys;
2022-11-09 13:16:44 -07:00
static bool can_invoke(const string &cmdline, df::viewscreen *screen) {
2014-06-01 21:48:34 -06:00
vector<string> cmd_parts;
split_string(&cmd_parts, cmdline, " ");
return Core::getInstance().getPluginManager()->CanInvokeHotkey(cmd_parts[0], screen);
}
2022-11-09 13:16:44 -07:00
static int cleanupHotkeys(lua_State *) {
DEBUG(log).print("cleaning up old stub keybindings for %s\n", current_focus.c_str());
std::for_each(sorted_keys.begin(), sorted_keys.end(), [](const string &sym) {
string keyspec = sym + "@" + MENU_SCREEN_FOCUS_STRING;
DEBUG(log).print("clearing keybinding: %s\n", keyspec.c_str());
Core::getInstance().ClearKeyBindings(keyspec);
});
valid = false;
current_focus = "";
sorted_keys.clear();
current_bindings.clear();
return 0;
}
static void add_binding_if_valid(const string &sym, const string &cmdline, df::viewscreen *screen, bool filtermenu) {
2014-06-01 21:48:34 -06:00
if (!can_invoke(cmdline, screen))
return;
if (filtermenu && (cmdline == INVOKE_MENU_COMMAND ||
cmdline == INVOKE_HOTKEYS_COMMAND)) {
DEBUG(log).print("filtering out hotkey menu keybinding\n");
return;
}
2014-06-01 21:48:34 -06:00
current_bindings[sym] = cmdline;
sorted_keys.push_back(sym);
string keyspec = sym + "@" + MENU_SCREEN_FOCUS_STRING;
2022-11-07 11:01:49 -07:00
string binding = "hotkeys invoke " + int_to_string(sorted_keys.size() - 1);
DEBUG(log).print("adding keybinding: %s -> %s\n", keyspec.c_str(), binding.c_str());
Core::getInstance().AddKeyBinding(keyspec, binding);
2014-06-01 21:48:34 -06:00
}
static void find_active_keybindings(df::viewscreen *screen, bool filtermenu) {
2022-11-09 13:16:44 -07:00
DEBUG(log).print("scanning for active keybindings\n");
if (valid)
cleanupHotkeys(NULL);
2014-06-01 21:48:34 -06:00
vector<string> valid_keys;
2022-07-26 12:35:31 -06:00
2022-11-09 13:16:44 -07:00
for (char c = 'A'; c <= 'Z'; c++) {
2014-06-01 21:48:34 -06:00
valid_keys.push_back(string(&c, 1));
}
2022-11-09 13:16:44 -07:00
for (int i = 1; i < 10; i++) {
2014-06-01 21:48:34 -06:00
valid_keys.push_back("F" + int_to_string(i));
}
2022-07-15 10:44:24 -06:00
valid_keys.push_back("`");
2022-11-09 13:16:44 -07:00
current_focus = Gui::getFocusString(screen);
for (int shifted = 0; shifted < 2; shifted++) {
for (int alt = 0; alt < 2; alt++) {
for (int ctrl = 0; ctrl < 2; ctrl++) {
for (auto it = valid_keys.begin(); it != valid_keys.end(); it++) {
2014-06-01 21:48:34 -06:00
string sym;
if (ctrl) sym += "Ctrl-";
if (alt) sym += "Alt-";
if (shifted) sym += "Shift-";
2014-06-01 21:48:34 -06:00
sym += *it;
auto list = Core::getInstance().ListKeyBindings(sym);
2022-11-09 13:16:44 -07:00
for (auto invoke_cmd = list.begin(); invoke_cmd != list.end(); invoke_cmd++) {
string::size_type colon_pos = invoke_cmd->find(":");
// colons at location 0 are for commands like ":lua"
if (colon_pos == string::npos || colon_pos == 0) {
add_binding_if_valid(sym, *invoke_cmd, screen, filtermenu);
2014-06-01 21:48:34 -06:00
}
2022-11-09 13:16:44 -07:00
else {
2014-06-01 21:48:34 -06:00
vector<string> tokens;
split_string(&tokens, *invoke_cmd, ":");
string focus = tokens[0].substr(1);
2022-11-09 13:16:44 -07:00
if (prefix_matches(focus, current_focus)) {
2014-06-01 21:48:34 -06:00
auto cmdline = trim(tokens[1]);
add_binding_if_valid(sym, cmdline, screen, filtermenu);
2014-06-01 21:48:34 -06:00
}
}
}
}
}
}
}
2022-11-09 13:16:44 -07:00
valid = true;
}
static int getHotkeys(lua_State *L) {
find_active_keybindings(Gui::getCurViewscreen(true), true);
2022-11-09 13:16:44 -07:00
Lua::PushVector(L, sorted_keys);
Lua::Push(L, current_bindings);
return 2;
}
DFHACK_PLUGIN_LUA_COMMANDS {
DFHACK_LUA_COMMAND(getHotkeys),
DFHACK_LUA_COMMAND(cleanupHotkeys),
DFHACK_LUA_END
};
static void list(color_ostream &out) {
DEBUG(log).print("listing active hotkeys\n");
bool was_valid = valid;
if (!valid)
find_active_keybindings(Gui::getCurViewscreen(true), false);
2022-11-09 13:16:44 -07:00
out.print("Valid keybindings for the current screen (%s)\n",
current_focus.c_str());
std::for_each(sorted_keys.begin(), sorted_keys.end(), [&](const string &sym) {
out.print("%s: %s\n", sym.c_str(), current_bindings[sym].c_str());
});
if (!was_valid)
cleanupHotkeys(NULL);
2014-06-01 21:48:34 -06:00
}
2022-11-09 13:16:44 -07:00
static bool invoke_command(color_ostream &out, const size_t index) {
2014-06-01 21:48:34 -06:00
auto screen = Core::getTopViewscreen();
2022-11-07 11:01:49 -07:00
if (sorted_keys.size() <= index ||
Gui::getFocusString(screen) != MENU_SCREEN_FOCUS_STRING)
2014-06-01 21:48:34 -06:00
return false;
2022-11-07 11:01:49 -07:00
auto cmd = current_bindings[sorted_keys[index]];
DEBUG(log).print("invoking command: '%s'\n", cmd.c_str());
{
Screen::Hide hideGuard(screen, Screen::Hide::RESTORE_AT_TOP);
Core::getInstance().runCommand(out, cmd);
}
Screen::dismiss(screen);
return true;
2014-06-01 21:48:34 -06:00
}
2022-11-09 13:16:44 -07:00
static command_result hotkeys_cmd(color_ostream &out, vector <string> & parameters) {
if (!parameters.size()) {
DEBUG(log).print("invoking command: '%s'\n", INVOKE_MENU_COMMAND.c_str());
return Core::getInstance().runCommand(out, INVOKE_MENU_COMMAND );
2022-11-09 13:16:44 -07:00
}
if (parameters[0] == "list") {
list(out);
return CR_OK;
}
// internal command -- intentionally undocumented
if (parameters.size() != 2 || parameters[0] != "invoke")
return CR_WRONG_USAGE;
2014-06-01 21:48:34 -06:00
2022-11-07 11:01:49 -07:00
CoreSuspender guard;
2022-11-09 13:16:44 -07:00
int index = string_to_int(parameters[1], -1);
if (index < 0)
return CR_WRONG_USAGE;
2022-11-07 11:01:49 -07:00
return invoke_command(out, index) ? CR_OK : CR_WRONG_USAGE;
2014-06-01 21:48:34 -06:00
}
2022-11-09 13:16:44 -07:00
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands) {
2014-06-01 21:48:34 -06:00
commands.push_back(
PluginCommand(
2022-07-25 22:54:34 -06:00
"hotkeys",
"Invoke hotkeys from the interactive menu.",
hotkeys_cmd,
Gui::anywhere_hotkey));
2014-06-01 21:48:34 -06:00
return CR_OK;
}