Skip to content

Commit

Permalink
With exception of window focus, make sure hotkeys are processed in th…
Browse files Browse the repository at this point in the history
…e game thread
  • Loading branch information
3vcloud committed Jan 5, 2024
1 parent 176f80d commit f193318
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions GWToolboxdll/Windows/HotkeysWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ namespace {

TBHotkey* current_hotkey = nullptr;

std::queue<TBHotkey*> pending_hotkeys;
std::recursive_mutex pending_mutex;
void PushPendingHotkey(TBHotkey* hk) {
pending_mutex.lock();
pending_hotkeys.push(hk);
pending_mutex.unlock();
}
TBHotkey* PopPendingHotkey() {
TBHotkey* hk = nullptr;
pending_mutex.lock();
if (pending_hotkeys.size()) {
hk = pending_hotkeys.front();
pending_hotkeys.pop();
}
pending_mutex.unlock();
return hk;
}

bool loaded_action_labels = false;
// NB: GetActionLabel_Func() must be called when we're in-game, because it relies on other gw modules being loaded internally.
// Because we only draw this module when we're in-game, we just need to call this from the Draw() loop instead of on Initialise()
Expand Down Expand Up @@ -187,6 +205,7 @@ namespace {
if (!block_hotkeys && !hk->pressed
&& ((activated && hk->trigger_on_gain_focus)
|| (!activated && hk->trigger_on_lose_focus))) {
// Would be nice to use PushPendingHotkey here, but losing/gaining focus is a special case
hk->pressed = true;
current_hotkey = hk;
hk->Execute();
Expand Down Expand Up @@ -599,10 +618,7 @@ bool HotkeysWindow::WndProc(const UINT Message, const WPARAM wParam, LPARAM)
&& !hk->pressed
&& keyData == hk->hotkey
&& modifier == hk->modifier) {
hk->pressed = true;
current_hotkey = hk;
hk->Toggle();
current_hotkey = nullptr;
PushPendingHotkey(hk);
if (hk->block_gw) {
triggered = true;
}
Expand Down Expand Up @@ -655,4 +671,11 @@ void HotkeysWindow::Update(const float)
hotkeys[i]->Execute();
}
}
while (const auto hk = PopPendingHotkey()) {
hk->pressed = true;
current_hotkey = hk;
hk->Execute();
current_hotkey = nullptr;
hk->pressed = false;
}
}

0 comments on commit f193318

Please sign in to comment.