From ce0f445f28854ae01445e44420927d2cde57c816 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Wed, 19 Jul 2023 00:35:36 -0700 Subject: [PATCH] move SUPPRESS_DUPLICATE_KEYBOARD_EVENTS pref into cpp --- docs/Core.rst | 3 --- docs/dev/Lua API.rst | 6 ++++++ library/Core.cpp | 26 +++++++++++++++----------- library/LuaApi.cpp | 14 +++++++++++++- library/include/Core.h | 4 ++++ library/lua/dfhack.lua | 5 ----- 6 files changed, 38 insertions(+), 20 deletions(-) diff --git a/docs/Core.rst b/docs/Core.rst index ac17401dc31..a8991147fe3 100644 --- a/docs/Core.rst +++ b/docs/Core.rst @@ -394,9 +394,6 @@ restarting DF. - ``dfhack.HIDE_ARMOK_TOOLS``: Whether to hide "armok" tools in command lists. -- ``dfhack.SUPPRESS_DUPLICATE_KEYBOARD_EVENTS``: Whether to prevent DFHack - keybindings from producing DF key events. - Miscellaneous notes =================== This section is for odd but important notes that don't fit anywhere else. diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index b52bf3c7639..02e17fb52ac 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -2857,6 +2857,12 @@ and are only documented here for completeness: Sets the system clipboard text from a CP437 string. +* ``dfhack.internal.getSuppressDuplicateKeyboardEvents()`` +* ``dfhack.internal.setSuppressDuplicateKeyboardEvents(suppress)`` + + Gets and sets the flag for whether to suppress DF key events when a DFHack + keybinding is matched and a command is launched. + .. _lua-core-context: Core interpreter context diff --git a/library/Core.cpp b/library/Core.cpp index 0a1c1935166..c4b03a7ac88 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -1423,6 +1423,7 @@ Core::Core() : memset(&(s_mods), 0, sizeof(s_mods)); // set up hotkey capture + suppress_duplicate_keyboard_events = true; hotkey_set = NO; last_world_data_ptr = NULL; last_local_map_ptr = NULL; @@ -1430,7 +1431,6 @@ Core::Core() : top_viewscreen = NULL; color_ostream::log_errors_to_stderr = true; - }; void Core::fatal (std::string output) @@ -2356,12 +2356,14 @@ bool Core::DFH_ncurses_key(int key) return ncurses_wgetch(key, dummy); } -static bool getSuppressDuplicateKeyboardEvents() { - auto L = Lua::Core::State; - color_ostream_proxy out(Core::getInstance().getConsole()); - Lua::StackUnwinder top(L); - return DFHack::Lua::PushModulePublic(out, L, "dfhack", "SUPPRESS_DUPLICATE_KEYBOARD_EVENTS") && - lua_toboolean(L, -1); +bool Core::getSuppressDuplicateKeyboardEvents() { + return suppress_duplicate_keyboard_events; +} + +void Core::setSuppressDuplicateKeyboardEvents(bool suppress) { + DEBUG(keybinding).print("setting suppress_duplicate_keyboard_events to %s\n", + suppress ? "true" : "false"); + suppress_duplicate_keyboard_events = suppress; } // returns true if the event is handled @@ -2396,9 +2398,10 @@ bool Core::DFH_SDL_Event(SDL_Event* ev) DEBUG(keybinding).print("key down: sym=%d (%c)\n", sym, sym); bool handled = SelectHotkey(sym, modstate); if (handled) { - DEBUG(keybinding).print("inhibiting SDL key down event\n"); + DEBUG(keybinding).print("%sinhibiting SDL key down event\n", + suppress_duplicate_keyboard_events ? "" : "not "); hotkey_states[sym] = true; - return getSuppressDuplicateKeyboardEvents(); + return suppress_duplicate_keyboard_events; } } else if (ke.state == SDL_RELEASED) @@ -2411,8 +2414,9 @@ bool Core::DFH_SDL_Event(SDL_Event* ev) auto &te = ev->text; DEBUG(keybinding).print("text input: '%s'\n", te.text); if (strlen(te.text) == 1 && hotkey_states[te.text[0]]) { - DEBUG(keybinding).print("inhibiting SDL text event\n"); - return true; + DEBUG(keybinding).print("%sinhibiting SDL text event\n", + suppress_duplicate_keyboard_events ? "" : "not "); + return suppress_duplicate_keyboard_events; } } diff --git a/library/LuaApi.cpp b/library/LuaApi.cpp index 2cd900934ab..050c99b86a6 100644 --- a/library/LuaApi.cpp +++ b/library/LuaApi.cpp @@ -3611,13 +3611,23 @@ static int internal_md5file(lua_State *L) } } +static int internal_getSuppressDuplicateKeyboardEvents(lua_State *L) { + Lua::Push(L, Core::getInstance().getSuppressDuplicateKeyboardEvents()); + return 1; +} + +static int internal_setSuppressDuplicateKeyboardEvents(lua_State *L) { + bool suppress = lua_toboolean(L, 1); + Core::getInstance().setSuppressDuplicateKeyboardEvents(suppress); + return 0; +} + static const luaL_Reg dfhack_internal_funcs[] = { { "getPE", internal_getPE }, { "getMD5", internal_getmd5 }, { "getAddress", internal_getAddress }, { "setAddress", internal_setAddress }, { "getVTable", internal_getVTable }, - { "adjustOffset", internal_adjustOffset }, { "getMemRanges", internal_getMemRanges }, { "patchMemory", internal_patchMemory }, @@ -3639,6 +3649,8 @@ static const luaL_Reg dfhack_internal_funcs[] = { { "getCommandDescription", internal_getCommandDescription }, { "threadid", internal_threadid }, { "md5File", internal_md5file }, + { "getSuppressDuplicateKeyboardEvents", internal_getSuppressDuplicateKeyboardEvents }, + { "setSuppressDuplicateKeyboardEvents", internal_setSuppressDuplicateKeyboardEvents }, { NULL, NULL } }; diff --git a/library/include/Core.h b/library/include/Core.h index 180b5cc7b50..d9cd4dca6d7 100644 --- a/library/include/Core.h +++ b/library/include/Core.h @@ -159,6 +159,9 @@ namespace DFHack std::string findScript(std::string name); void getScriptPaths(std::vector *dest); + bool getSuppressDuplicateKeyboardEvents(); + void setSuppressDuplicateKeyboardEvents(bool suppress); + bool ClearKeyBindings(std::string keyspec); bool AddKeyBinding(std::string keyspec, std::string cmdline); std::vector ListKeyBindings(std::string keyspec); @@ -249,6 +252,7 @@ namespace DFHack }; int8_t modstate; + bool suppress_duplicate_keyboard_events; std::map > key_bindings; std::string hotkey_cmd; enum hotkey_set_t { diff --git a/library/lua/dfhack.lua b/library/lua/dfhack.lua index 860b56cc1ed..8ea5e9dacc4 100644 --- a/library/lua/dfhack.lua +++ b/library/lua/dfhack.lua @@ -63,11 +63,6 @@ function dfhack.getHideArmokTools() return dfhack.HIDE_ARMOK_TOOLS end -dfhack.SUPPRESS_DUPLICATE_KEYBOARD_EVENTS = true -function dfhack.getSuppressDuplicateKeyboardEvents() - return dfhack.SUPPRESS_DUPLICATE_KEYBOARD_EVENTS -end - -- Error handling safecall = dfhack.safecall