Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move SUPPRESS_DUPLICATE_KEYBOARD_EVENTS pref into cpp #3584

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions docs/Core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions docs/dev/Lua API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 15 additions & 11 deletions library/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1423,14 +1423,14 @@ 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;
last_pause_state = false;
top_viewscreen = NULL;

color_ostream::log_errors_to_stderr = true;

};

void Core::fatal (std::string output)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
}

Expand Down
14 changes: 13 additions & 1 deletion library/LuaApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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 }
};

Expand Down
4 changes: 4 additions & 0 deletions library/include/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ namespace DFHack
std::string findScript(std::string name);
void getScriptPaths(std::vector<std::string> *dest);

bool getSuppressDuplicateKeyboardEvents();
void setSuppressDuplicateKeyboardEvents(bool suppress);

bool ClearKeyBindings(std::string keyspec);
bool AddKeyBinding(std::string keyspec, std::string cmdline);
std::vector<std::string> ListKeyBindings(std::string keyspec);
Expand Down Expand Up @@ -249,6 +252,7 @@ namespace DFHack
};
int8_t modstate;

bool suppress_duplicate_keyboard_events;
std::map<int, std::vector<KeyBinding> > key_bindings;
std::string hotkey_cmd;
enum hotkey_set_t {
Expand Down
5 changes: 0 additions & 5 deletions library/lua/dfhack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down