added keybind repeat flag

This commit is contained in:
vaxerski 2022-07-25 14:42:49 +02:00
parent 067e2d39f6
commit bb539f0a8d
3 changed files with 52 additions and 2 deletions

View file

@ -513,6 +513,7 @@ void CConfigManager::handleBind(const std::string& command, const std::string& v
// flags // flags
bool locked = false; bool locked = false;
bool release = false; bool release = false;
bool repeat = false;
const auto ARGS = command.substr(4); const auto ARGS = command.substr(4);
for (auto& arg : ARGS) { for (auto& arg : ARGS) {
@ -520,6 +521,8 @@ void CConfigManager::handleBind(const std::string& command, const std::string& v
locked = true; locked = true;
} else if (arg == 'r') { } else if (arg == 'r') {
release = true; release = true;
} else if (arg == 'e') {
repeat = true;
} else { } else {
parseError = "bind: invalid flag"; parseError = "bind: invalid flag";
return; return;
@ -556,9 +559,9 @@ void CConfigManager::handleBind(const std::string& command, const std::string& v
if (KEY != "") { if (KEY != "") {
if (isNumber(KEY) && std::stoi(KEY) > 9) if (isNumber(KEY) && std::stoi(KEY) > 9)
g_pKeybindManager->addKeybind(SKeybind{"", std::stoi(KEY), MOD, HANDLER, COMMAND, locked, m_szCurrentSubmap, release}); g_pKeybindManager->addKeybind(SKeybind{"", std::stoi(KEY), MOD, HANDLER, COMMAND, locked, m_szCurrentSubmap, release, repeat});
else else
g_pKeybindManager->addKeybind(SKeybind{KEY, -1, MOD, HANDLER, COMMAND, locked, m_szCurrentSubmap, release}); g_pKeybindManager->addKeybind(SKeybind{KEY, -1, MOD, HANDLER, COMMAND, locked, m_szCurrentSubmap, release, repeat});
} }
} }

View file

@ -37,6 +37,8 @@ CKeybindManager::CKeybindManager() {
void CKeybindManager::addKeybind(SKeybind kb) { void CKeybindManager::addKeybind(SKeybind kb) {
m_lKeybinds.push_back(kb); m_lKeybinds.push_back(kb);
m_pActiveKeybind = nullptr;
} }
void CKeybindManager::removeKeybind(uint32_t mod, const std::string& key) { void CKeybindManager::removeKeybind(uint32_t mod, const std::string& key) {
@ -58,6 +60,8 @@ void CKeybindManager::removeKeybind(uint32_t mod, const std::string& key) {
break; break;
} }
} }
m_pActiveKeybind = nullptr;
} }
uint32_t CKeybindManager::stringToModMask(std::string mods) { uint32_t CKeybindManager::stringToModMask(std::string mods) {
@ -92,6 +96,13 @@ bool CKeybindManager::onKeyEvent(wlr_keyboard_key_event* e, SKeyboard* pKeyboard
bool found = false; bool found = false;
if (e->state == WL_KEYBOARD_KEY_STATE_PRESSED) { if (e->state == WL_KEYBOARD_KEY_STATE_PRESSED) {
// clean repeat
if (m_pActiveKeybindEventSource) {
wl_event_source_remove(m_pActiveKeybindEventSource);
m_pActiveKeybindEventSource = nullptr;
m_pActiveKeybind = nullptr;
}
m_dPressedKeycodes.push_back(KEYCODE); m_dPressedKeycodes.push_back(KEYCODE);
m_dPressedKeysyms.push_back(keysym); m_dPressedKeysyms.push_back(keysym);
@ -102,6 +113,12 @@ bool CKeybindManager::onKeyEvent(wlr_keyboard_key_event* e, SKeyboard* pKeyboard
if (found) if (found)
shadowKeybinds(keysym, KEYCODE); shadowKeybinds(keysym, KEYCODE);
} else if (e->state == WL_KEYBOARD_KEY_STATE_RELEASED) { } else if (e->state == WL_KEYBOARD_KEY_STATE_RELEASED) {
// clean repeat
if (m_pActiveKeybindEventSource) {
wl_event_source_remove(m_pActiveKeybindEventSource);
m_pActiveKeybindEventSource = nullptr;
m_pActiveKeybind = nullptr;
}
m_dPressedKeycodes.erase(std::remove(m_dPressedKeycodes.begin(), m_dPressedKeycodes.end(), KEYCODE)); m_dPressedKeycodes.erase(std::remove(m_dPressedKeycodes.begin(), m_dPressedKeycodes.end(), KEYCODE));
m_dPressedKeysyms.erase(std::remove(m_dPressedKeysyms.begin(), m_dPressedKeysyms.end(), keysym)); m_dPressedKeysyms.erase(std::remove(m_dPressedKeysyms.begin(), m_dPressedKeysyms.end(), keysym));
@ -131,6 +148,22 @@ bool CKeybindManager::onAxisEvent(wlr_pointer_axis_event* e) {
return !found; return !found;
} }
int repeatKeyHandler(void* data) {
SKeybind** ppActiveKeybind = (SKeybind**)data;
if (!*ppActiveKeybind)
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_pInputManager->m_pActiveKeyboard->repeatRate);
return 0;
}
bool CKeybindManager::handleKeybinds(const uint32_t& modmask, const std::string& key, const xkb_keysym_t& keysym, const int& keycode, bool pressed, uint32_t time) { bool CKeybindManager::handleKeybinds(const uint32_t& modmask, const std::string& key, const xkb_keysym_t& keysym, const int& keycode, bool pressed, uint32_t time) {
bool found = false; bool found = false;
@ -187,6 +220,15 @@ bool CKeybindManager::handleKeybinds(const uint32_t& modmask, const std::string&
DISPATCHER->second(k.arg); DISPATCHER->second(k.arg);
} }
if (k.repeat) {
m_pActiveKeybind = &k;
m_pActiveKeybindEventSource = wl_event_loop_add_timer(g_pCompositor->m_sWLEventLoop, repeatKeyHandler, &m_pActiveKeybind);
const auto PACTIVEKEEB = g_pInputManager->m_pActiveKeyboard;
wl_event_source_timer_update(m_pActiveKeybindEventSource, PACTIVEKEEB->repeatDelay);
}
found = true; found = true;
} }

View file

@ -15,6 +15,7 @@ struct SKeybind {
bool locked = false; bool locked = false;
std::string submap = ""; std::string submap = "";
bool release = false; bool release = false;
bool repeat = false;
// DO NOT INITIALIZE // DO NOT INITIALIZE
bool shadowed = false; bool shadowed = false;
@ -35,6 +36,8 @@ public:
std::unordered_map<std::string, std::function<void(std::string)>> m_mDispatchers; std::unordered_map<std::string, std::function<void(std::string)>> m_mDispatchers;
wl_event_source* m_pActiveKeybindEventSource = nullptr;
private: private:
std::list<SKeybind> m_lKeybinds; std::list<SKeybind> m_lKeybinds;
std::deque<xkb_keysym_t> m_dPressedKeysyms; std::deque<xkb_keysym_t> m_dPressedKeysyms;
@ -44,6 +47,8 @@ private:
xkb_keysym_t m_kHeldBack = 0; xkb_keysym_t m_kHeldBack = 0;
SKeybind* m_pActiveKeybind = nullptr;
bool handleKeybinds(const uint32_t&, const std::string&, const xkb_keysym_t&, const int&, bool, uint32_t); bool handleKeybinds(const uint32_t&, const std::string&, const xkb_keysym_t&, const int&, bool, uint32_t);
bool handleInternalKeybinds(xkb_keysym_t); bool handleInternalKeybinds(xkb_keysym_t);