Hotkeys actions are executed from an external thread to avoid deadlocks that may occur if running commands from the render or simulation threads.
| 147 | // Hotkeys actions are executed from an external thread to avoid deadlocks |
| 148 | // that may occur if running commands from the render or simulation threads. |
| 149 | void HotkeyManager::hotkey_thread_fn() { |
| 150 | auto& core = DFHack::Core::getInstance(); |
| 151 | |
| 152 | std::unique_lock<std::mutex> l(lock); |
| 153 | while (true) { |
| 154 | cond.wait(l, [this]() { return this->hotkey_sig != HotkeySignal::None; }); |
| 155 | if (hotkey_sig == HotkeySignal::Shutdown) |
| 156 | return; |
| 157 | if (hotkey_sig != HotkeySignal::CmdReady) |
| 158 | continue; |
| 159 | |
| 160 | // Copy and reset important data, then release the lock |
| 161 | this->hotkey_sig = HotkeySignal::None; |
| 162 | std::string cmd = this->queued_command; |
| 163 | this->queued_command.clear(); |
| 164 | l.unlock(); |
| 165 | |
| 166 | // Attempt execution of command |
| 167 | DFHack::color_ostream_proxy out(core.getConsole()); |
| 168 | auto res = core.runCommand(out, cmd); |
| 169 | if (res == DFHack::CR_NOT_IMPLEMENTED) |
| 170 | out.printerr("Invalid hotkey command: '%s'\n", cmd.c_str()); |
| 171 | l.lock(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | bool HotkeyManager::addKeybind(KeySpec spec, std::string_view cmd) { |