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

allow repeating multiple binds #8290

Merged
merged 4 commits into from
Nov 18, 2024
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
69 changes: 31 additions & 38 deletions src/managers/KeybindManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,30 @@ CKeybindManager::CKeybindManager() {
},
nullptr);

m_pRepeatKeyTimer = makeShared<CEventLoopTimer>(
std::nullopt,
[this](SP<CEventLoopTimer> self, void* data) {
if (m_vActiveKeybinds.size() == 0 || g_pSeatManager->keyboard.expired())
return;

for (SKeybind* k : m_vActiveKeybinds) {
vaxerski marked this conversation as resolved.
Show resolved Hide resolved
const auto DISPATCHER = g_pKeybindManager->m_mDispatchers.find(k->handler);

Debug::log(LOG, "Keybind repeat triggered, calling dispatcher.");
DISPATCHER->second(k->arg);
}

const auto PACTIVEKEEB = g_pSeatManager->keyboard.lock();
self->updateTimeout(std::chrono::milliseconds(1000 / PACTIVEKEEB->repeatRate));
},
nullptr);

g_pEventLoopManager->addTimer(m_pLongPressTimer);
g_pEventLoopManager->addTimer(m_pRepeatKeyTimer);

static auto P = g_pHookSystem->hookDynamic("configReloaded", [this](void* hk, SCallbackInfo& info, std::any param) {
// clear cuz realloc'd
m_pActiveKeybind = nullptr;
m_vActiveKeybinds.clear();
m_pLastLongPressKeybind = nullptr;
m_vPressedSpecialBinds.clear();
});
Expand All @@ -161,12 +180,16 @@ CKeybindManager::~CKeybindManager() {
g_pEventLoopManager->removeTimer(m_pLongPressTimer);
m_pLongPressTimer.reset();
}
if (m_pRepeatKeyTimer && g_pEventLoopManager) {
g_pEventLoopManager->removeTimer(m_pRepeatKeyTimer);
m_pRepeatKeyTimer.reset();
}
}

void CKeybindManager::addKeybind(SKeybind kb) {
m_lKeybinds.push_back(kb);

m_pActiveKeybind = nullptr;
m_vActiveKeybinds.clear();
m_pLastLongPressKeybind = nullptr;
}

Expand All @@ -180,7 +203,7 @@ void CKeybindManager::removeKeybind(uint32_t mod, const SParsedKey& key) {
}
}

m_pActiveKeybind = nullptr;
m_vActiveKeybinds.clear();
m_pLastLongPressKeybind = nullptr;
}

Expand Down Expand Up @@ -426,11 +449,7 @@ bool CKeybindManager::onKeyEvent(std::any event, SP<IKeyboard> pKeyboard) {
.submapAtPress = m_szCurrentSelectedSubmap,
};

if (m_pActiveKeybindEventSource) {
wl_event_source_remove(m_pActiveKeybindEventSource);
m_pActiveKeybindEventSource = nullptr;
m_pActiveKeybind = nullptr;
}
m_vActiveKeybinds.clear();

m_pLastLongPressKeybind = nullptr;

Expand Down Expand Up @@ -482,11 +501,7 @@ bool CKeybindManager::onAxisEvent(const IPointer::SAxisEvent& e) {

m_tScrollTimer.reset();

if (m_pActiveKeybindEventSource) {
wl_event_source_remove(m_pActiveKeybindEventSource);
m_pActiveKeybindEventSource = nullptr;
m_pActiveKeybind = nullptr;
}
m_vActiveKeybinds.clear();

bool found = false;
if (e.source == WL_POINTER_AXIS_SOURCE_WHEEL && e.axis == WL_POINTER_AXIS_VERTICAL_SCROLL) {
Expand Down Expand Up @@ -525,11 +540,7 @@ bool CKeybindManager::onMouseEvent(const IPointer::SButtonEvent& e) {
.modmaskAtPressTime = MODS,
};

if (m_pActiveKeybindEventSource) {
wl_event_source_remove(m_pActiveKeybindEventSource);
m_pActiveKeybindEventSource = nullptr;
m_pActiveKeybind = nullptr;
}
m_vActiveKeybinds.clear();

if (e.state == WL_POINTER_BUTTON_STATE_PRESSED) {
m_dPressedKeys.push_back(KEY);
Expand Down Expand Up @@ -580,22 +591,6 @@ void CKeybindManager::onSwitchOffEvent(const std::string& switchName) {
handleKeybinds(0, SPressedKeyWithMods{.keyName = "switch:off:" + switchName}, true);
}

int repeatKeyHandler(void* data) {
SKeybind** ppActiveKeybind = (SKeybind**)data;

if (!*ppActiveKeybind || g_pSeatManager->keyboard.expired())
return 0;

const auto DISPATCHER = g_pKeybindManager->m_mDispatchers.find((*ppActiveKeybind)->handler);

Debug::log(LOG, "Keybind repeat triggered, calling dispatcher.");
DISPATCHER->second((*ppActiveKeybind)->arg);

wl_event_source_timer_update(g_pKeybindManager->m_pActiveKeybindEventSource, 1000 / g_pSeatManager->keyboard->repeatRate);

return 0;
}

eMultiKeyCase CKeybindManager::mkKeysymSetMatches(const std::set<xkb_keysym_t> keybindKeysyms, const std::set<xkb_keysym_t> pressedKeysyms) {
// Returns whether two sets of keysyms are equal, partially equal, or not
// matching. (Partially matching means that pressed is a subset of bound)
Expand Down Expand Up @@ -770,12 +765,10 @@ SDispatchResult CKeybindManager::handleKeybinds(const uint32_t modmask, const SP
}

if (k.repeat) {
m_pActiveKeybind = &k;
m_pActiveKeybindEventSource = wl_event_loop_add_timer(g_pCompositor->m_sWLEventLoop, repeatKeyHandler, &m_pActiveKeybind);

const auto PACTIVEKEEB = g_pSeatManager->keyboard.lock();

wl_event_source_timer_update(m_pActiveKeybindEventSource, PACTIVEKEEB->repeatDelay);
m_vActiveKeybinds.push_back(&k);
m_pRepeatKeyTimer->updateTimeout(std::chrono::milliseconds(PACTIVEKEEB->repeatDelay));
}

if (!k.nonConsuming)
Expand Down
7 changes: 3 additions & 4 deletions src/managers/KeybindManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ class CKeybindManager {

std::unordered_map<std::string, std::function<SDispatchResult(std::string)>> m_mDispatchers;

wl_event_source* m_pActiveKeybindEventSource = nullptr;

bool m_bGroupsLocked = false;

std::list<SKeybind> m_lKeybinds;
Expand All @@ -121,8 +119,9 @@ class CKeybindManager {

inline static std::string m_szCurrentSelectedSubmap = "";

SKeybind * m_pActiveKeybind = nullptr, *m_pLastLongPressKeybind = nullptr;
SP<CEventLoopTimer> m_pLongPressTimer;
std::vector<SKeybind*> m_vActiveKeybinds;
SKeybind* m_pLastLongPressKeybind = nullptr;
SP<CEventLoopTimer> m_pLongPressTimer, m_pRepeatKeyTimer;

uint32_t m_uTimeLastMs = 0;
uint32_t m_uLastCode = 0;
Expand Down
Loading