From 24a95621e5118e39e1a6e05b17ef89f4ec2c8c01 Mon Sep 17 00:00:00 2001 From: ArchSav <96357545+ArchSav@users.noreply.github.com> Date: Sat, 14 Mar 2026 00:48:01 +1100 Subject: input: add device specific binds (#13073) Adds support for device-specific binds --- src/config/ConfigManager.cpp | 29 ++++++++++++++----- src/managers/KeybindManager.cpp | 37 +++++++++++++----------- src/managers/KeybindManager.hpp | 57 +++++++++++++++++++------------------ src/managers/PointerManager.cpp | 4 +-- src/managers/input/InputManager.cpp | 10 +++---- src/managers/input/InputManager.hpp | 4 +-- 6 files changed, 82 insertions(+), 59 deletions(-) (limited to 'src') diff --git a/src/config/ConfigManager.cpp b/src/config/ConfigManager.cpp index 3e0a9957..e4632bb9 100644 --- a/src/config/ConfigManager.cpp +++ b/src/config/ConfigManager.cpp @@ -2542,6 +2542,7 @@ std::optional CConfigManager::handleBind(const std::string& command bool click = false; bool drag = false; bool submapUniversal = false; + bool isPerDevice = false; const auto BINDARGS = command.substr(4); for (auto const& arg : BINDARGS) { @@ -2566,6 +2567,7 @@ std::optional CConfigManager::handleBind(const std::string& command release = true; break; case 'u': submapUniversal = true; break; + case 'k': isPerDevice = true; break; default: return "bind: invalid flag"; } } @@ -2579,13 +2581,14 @@ std::optional CConfigManager::handleBind(const std::string& command if (click && drag) return "flags c and g are mutually exclusive"; - const int numbArgs = hasDescription ? 5 : 4; + const int numbArgs = (hasDescription ? 5 : 4) + sc(isPerDevice); const auto ARGS = CVarList(value, numbArgs); - const int DESCR_OFFSET = hasDescription ? 1 : 0; + const int DESCR_OFFSET = hasDescription ? 1 : 0; + const int DEVICE_OFFSET = sc(isPerDevice); if ((ARGS.size() < 3 && !mouse) || (ARGS.size() < 3 && mouse)) return "bind: too few args"; - else if ((ARGS.size() > sc(4) + DESCR_OFFSET && !mouse) || (ARGS.size() > sc(3) + DESCR_OFFSET && mouse)) + else if ((ARGS.size() > sc(4) + DESCR_OFFSET + DEVICE_OFFSET && !mouse) || (ARGS.size() > sc(3) + DESCR_OFFSET + DEVICE_OFFSET && mouse)) return "bind: too many args"; std::set KEYSYMS; @@ -2604,11 +2607,13 @@ std::optional CConfigManager::handleBind(const std::string& command const auto KEY = multiKey ? "" : ARGS[1]; - const auto DESCRIPTION = hasDescription ? ARGS[2] : ""; + const auto DEVICEARGS = isPerDevice ? ARGS[2] : ""; - auto HANDLER = ARGS[2 + DESCR_OFFSET]; + const auto DESCRIPTION = hasDescription ? ARGS[2 + DEVICE_OFFSET] : ""; - const auto COMMAND = mouse ? HANDLER : ARGS[3 + DESCR_OFFSET]; + auto HANDLER = ARGS[2 + DESCR_OFFSET + DEVICE_OFFSET]; + + const auto COMMAND = mouse ? HANDLER : ARGS[3 + DESCR_OFFSET + DEVICE_OFFSET]; if (mouse) HANDLER = "mouse"; @@ -2628,6 +2633,16 @@ std::optional CConfigManager::handleBind(const std::string& command return "Invalid mod, requested mod \"" + MODSTR + "\" is not a valid mod."; } + //[!]keyboard1 keyboard2 ... + bool deviceInclusive = false; + std::unordered_set devices = {}; + if (!DEVICEARGS.empty()) { + deviceInclusive = DEVICEARGS[0] != '!'; + for (const auto deviceString : std::ranges::views::split(DEVICEARGS.substr(deviceInclusive ? 0 : 1), ' ')) { + devices.emplace(std::string_view(deviceString)); + } + } + if ((!KEY.empty()) || multiKey) { SParsedKey parsedKey = parseKey(KEY); @@ -2639,7 +2654,7 @@ std::optional CConfigManager::handleBind(const std::string& command g_pKeybindManager->addKeybind(SKeybind{parsedKey.key, KEYSYMS, parsedKey.keycode, parsedKey.catchAll, MOD, MODS, HANDLER, COMMAND, locked, m_currentSubmap, DESCRIPTION, release, repeat, longPress, mouse, nonConsuming, transparent, ignoreMods, multiKey, hasDescription, dontInhibit, - click, drag, submapUniversal}); + click, drag, submapUniversal, deviceInclusive, devices}); } return {}; diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index fd7c4e72..e6064472 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -480,7 +480,7 @@ bool CKeybindManager::onKeyEvent(std::any event, SP pKeyboard) { m_pressedKeys.push_back(KEY); - suppressEvent = !handleKeybinds(MODS, KEY, true, pKeyboard).passEvent; + suppressEvent = !handleKeybinds(MODS, KEY, true, pKeyboard, pKeyboard).passEvent; if (suppressEvent) shadowKeybinds(keysym, KEYCODE); @@ -491,7 +491,7 @@ bool CKeybindManager::onKeyEvent(std::any event, SP pKeyboard) { bool foundInPressedKeys = false; for (auto it = m_pressedKeys.begin(); it != m_pressedKeys.end();) { if (it->keycode == KEYCODE) { - handleKeybinds(MODS, *it, false, pKeyboard); + handleKeybinds(MODS, *it, false, pKeyboard, pKeyboard); foundInPressedKeys = true; suppressEvent = !it->sent; it = m_pressedKeys.erase(it); @@ -502,7 +502,7 @@ bool CKeybindManager::onKeyEvent(std::any event, SP pKeyboard) { if (!foundInPressedKeys) { Log::logger->log(Log::ERR, "BUG THIS: key not found in m_dPressedKeys"); // fallback with wrong `KEY.modmaskAtPressTime`, this can be buggy - suppressEvent = !handleKeybinds(MODS, KEY, false, pKeyboard).passEvent; + suppressEvent = !handleKeybinds(MODS, KEY, false, pKeyboard, pKeyboard).passEvent; } shadowKeybinds(); @@ -511,7 +511,7 @@ bool CKeybindManager::onKeyEvent(std::any event, SP pKeyboard) { return !suppressEvent && !mouseBindWasActive; } -bool CKeybindManager::onAxisEvent(const IPointer::SAxisEvent& e) { +bool CKeybindManager::onAxisEvent(const IPointer::SAxisEvent& e, SP pointer) { const auto MODS = g_pInputManager->getModsFromAllKBs(); static auto PDELAY = CConfigValue("binds:scroll_event_delay"); @@ -526,14 +526,14 @@ bool CKeybindManager::onAxisEvent(const IPointer::SAxisEvent& e) { bool found = false; if (e.source == WL_POINTER_AXIS_SOURCE_WHEEL && e.axis == WL_POINTER_AXIS_VERTICAL_SCROLL) { if (e.delta < 0) - found = !handleKeybinds(MODS, SPressedKeyWithMods{.keyName = "mouse_down"}, true, nullptr).passEvent; + found = !handleKeybinds(MODS, SPressedKeyWithMods{.keyName = "mouse_down"}, true, nullptr, pointer).passEvent; else - found = !handleKeybinds(MODS, SPressedKeyWithMods{.keyName = "mouse_up"}, true, nullptr).passEvent; + found = !handleKeybinds(MODS, SPressedKeyWithMods{.keyName = "mouse_up"}, true, nullptr, pointer).passEvent; } else if (e.source == WL_POINTER_AXIS_SOURCE_WHEEL && e.axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) { if (e.delta < 0) - found = !handleKeybinds(MODS, SPressedKeyWithMods{.keyName = "mouse_left"}, true, nullptr).passEvent; + found = !handleKeybinds(MODS, SPressedKeyWithMods{.keyName = "mouse_left"}, true, nullptr, pointer).passEvent; else - found = !handleKeybinds(MODS, SPressedKeyWithMods{.keyName = "mouse_right"}, true, nullptr).passEvent; + found = !handleKeybinds(MODS, SPressedKeyWithMods{.keyName = "mouse_right"}, true, nullptr, pointer).passEvent; } if (found) @@ -542,7 +542,7 @@ bool CKeybindManager::onAxisEvent(const IPointer::SAxisEvent& e) { return !found; } -bool CKeybindManager::onMouseEvent(const IPointer::SButtonEvent& e) { +bool CKeybindManager::onMouseEvent(const IPointer::SButtonEvent& e, SP mouse) { const auto MODS = g_pInputManager->getModsFromAllKBs(); bool suppressEvent = false; @@ -566,7 +566,7 @@ bool CKeybindManager::onMouseEvent(const IPointer::SButtonEvent& e) { if (e.state == WL_POINTER_BUTTON_STATE_PRESSED) { m_pressedKeys.push_back(KEY); - suppressEvent = !handleKeybinds(MODS, KEY, true, nullptr).passEvent; + suppressEvent = !handleKeybinds(MODS, KEY, true, nullptr, mouse).passEvent; if (suppressEvent) shadowKeybinds(); @@ -576,7 +576,7 @@ bool CKeybindManager::onMouseEvent(const IPointer::SButtonEvent& e) { bool foundInPressedKeys = false; for (auto it = m_pressedKeys.begin(); it != m_pressedKeys.end();) { if (it->keyName == KEY_NAME) { - suppressEvent = !handleKeybinds(MODS, *it, false, nullptr).passEvent; + suppressEvent = !handleKeybinds(MODS, *it, false, nullptr, mouse).passEvent; foundInPressedKeys = true; suppressEvent = !it->sent; it = m_pressedKeys.erase(it); @@ -587,7 +587,7 @@ bool CKeybindManager::onMouseEvent(const IPointer::SButtonEvent& e) { if (!foundInPressedKeys) { Log::logger->log(Log::ERR, "BUG THIS: key not found in m_dPressedKeys (2)"); // fallback with wrong `KEY.modmaskAtPressTime`, this can be buggy - suppressEvent = !handleKeybinds(MODS, KEY, false, nullptr).passEvent; + suppressEvent = !handleKeybinds(MODS, KEY, false, nullptr, mouse).passEvent; } shadowKeybinds(); @@ -601,15 +601,15 @@ void CKeybindManager::resizeWithBorder(const IPointer::SButtonEvent& e) { } void CKeybindManager::onSwitchEvent(const std::string& switchName) { - handleKeybinds(0, SPressedKeyWithMods{.keyName = "switch:" + switchName}, true, nullptr); + handleKeybinds(0, SPressedKeyWithMods{.keyName = "switch:" + switchName}, true, nullptr, nullptr); } void CKeybindManager::onSwitchOnEvent(const std::string& switchName) { - handleKeybinds(0, SPressedKeyWithMods{.keyName = "switch:on:" + switchName}, true, nullptr); + handleKeybinds(0, SPressedKeyWithMods{.keyName = "switch:on:" + switchName}, true, nullptr, nullptr); } void CKeybindManager::onSwitchOffEvent(const std::string& switchName) { - handleKeybinds(0, SPressedKeyWithMods{.keyName = "switch:off:" + switchName}, true, nullptr); + handleKeybinds(0, SPressedKeyWithMods{.keyName = "switch:off:" + switchName}, true, nullptr, nullptr); } eMultiKeyCase CKeybindManager::mkKeysymSetMatches(const std::set keybindKeysyms, const std::set pressedKeysyms) { @@ -642,7 +642,7 @@ SSubmap CKeybindManager::getCurrentSubmap() { return m_currentSelectedSubmap; } -SDispatchResult CKeybindManager::handleKeybinds(const uint32_t modmask, const SPressedKeyWithMods& key, bool pressed, SP keyboard) { +SDispatchResult CKeybindManager::handleKeybinds(const uint32_t modmask, const SPressedKeyWithMods& key, bool pressed, SP keyboard, SP device) { static auto PDISABLEINHIBIT = CConfigValue("binds:disable_keybind_grabbing"); static auto PDRAGTHRESHOLD = CConfigValue("binds:drag_threshold"); @@ -682,6 +682,11 @@ SDispatchResult CKeybindManager::handleKeybinds(const uint32_t modmask, const SP if (!IGNORECONDITIONS && ((modmask != k->modmask && !k->ignoreMods) || (k->submap != m_currentSelectedSubmap && !k->submapUniversal) || k->shadowed)) continue; + if (device) { + if (k->deviceInclusive ^ k->devices.contains(device->m_hlName)) + continue; + } + if (k->multiKey) { switch (mkBindMatches(k)) { case MK_NO_MATCH: continue; diff --git a/src/managers/KeybindManager.hpp b/src/managers/KeybindManager.hpp index 1f013606..16f0b67d 100644 --- a/src/managers/KeybindManager.hpp +++ b/src/managers/KeybindManager.hpp @@ -3,6 +3,7 @@ #include "../defines.hpp" #include #include +#include #include #include #include @@ -26,30 +27,32 @@ struct SSubmap { }; struct SKeybind { - std::string key = ""; - std::set sMkKeys = {}; - uint32_t keycode = 0; - bool catchAll = false; - uint32_t modmask = 0; - std::set sMkMods = {}; - std::string handler = ""; - std::string arg = ""; - bool locked = false; - SSubmap submap = {}; - std::string description = ""; - bool release = false; - bool repeat = false; - bool longPress = false; - bool mouse = false; - bool nonConsuming = false; - bool transparent = false; - bool ignoreMods = false; - bool multiKey = false; - bool hasDescription = false; - bool dontInhibit = false; - bool click = false; - bool drag = false; - bool submapUniversal = false; + std::string key = ""; + std::set sMkKeys = {}; + uint32_t keycode = 0; + bool catchAll = false; + uint32_t modmask = 0; + std::set sMkMods = {}; + std::string handler = ""; + std::string arg = ""; + bool locked = false; + SSubmap submap = {}; + std::string description = ""; + bool release = false; + bool repeat = false; + bool longPress = false; + bool mouse = false; + bool nonConsuming = false; + bool transparent = false; + bool ignoreMods = false; + bool multiKey = false; + bool hasDescription = false; + bool dontInhibit = false; + bool click = false; + bool drag = false; + bool submapUniversal = false; + bool deviceInclusive = false; + std::unordered_set devices = {}; // DO NOT INITIALIZE bool shadowed = false; @@ -94,8 +97,8 @@ class CKeybindManager { ~CKeybindManager(); bool onKeyEvent(std::any, SP); - bool onAxisEvent(const IPointer::SAxisEvent&); - bool onMouseEvent(const IPointer::SButtonEvent&); + bool onAxisEvent(const IPointer::SAxisEvent&, SP); + bool onMouseEvent(const IPointer::SButtonEvent&, SP); void resizeWithBorder(const IPointer::SButtonEvent&); void onSwitchEvent(const std::string&); void onSwitchOnEvent(const std::string&); @@ -145,7 +148,7 @@ class CKeybindManager { CTimer m_scrollTimer; - SDispatchResult handleKeybinds(const uint32_t, const SPressedKeyWithMods&, bool, SP); + SDispatchResult handleKeybinds(const uint32_t, const SPressedKeyWithMods&, bool, SP, SP); std::set m_mkKeys = {}; std::set m_mkMods = {}; diff --git a/src/managers/PointerManager.cpp b/src/managers/PointerManager.cpp index d004f5b8..f78984f9 100644 --- a/src/managers/PointerManager.cpp +++ b/src/managers/PointerManager.cpp @@ -950,8 +950,8 @@ void CPointerManager::attachPointer(SP pointer) { CKeybindManager::dpms("on"); }); - listener->button = pointer->m_pointerEvents.button.listen([](const IPointer::SButtonEvent& event) { - g_pInputManager->onMouseButton(event); + listener->button = pointer->m_pointerEvents.button.listen([weak = WP(pointer)](const IPointer::SButtonEvent& event) { + g_pInputManager->onMouseButton(event, weak.lock()); PROTO::idle->onActivity(); }); diff --git a/src/managers/input/InputManager.cpp b/src/managers/input/InputManager.cpp index 9195536f..d88b29cd 100644 --- a/src/managers/input/InputManager.cpp +++ b/src/managers/input/InputManager.cpp @@ -647,7 +647,7 @@ void CInputManager::mouseMoveUnified(uint32_t time, bool refocus, bool mouse, st g_pSeatManager->sendPointerMotion(time, surfaceLocal); } -void CInputManager::onMouseButton(IPointer::SButtonEvent e) { +void CInputManager::onMouseButton(IPointer::SButtonEvent e, SP mouse) { Event::SCallbackInfo info; Event::bus()->m_events.input.mouse.button.emit(e, info); if (info.cancelled) @@ -667,7 +667,7 @@ void CInputManager::onMouseButton(IPointer::SButtonEvent e) { } switch (m_clickBehavior) { - case CLICKMODE_DEFAULT: processMouseDownNormal(e); break; + case CLICKMODE_DEFAULT: processMouseDownNormal(e, mouse); break; case CLICKMODE_KILL: processMouseDownKill(e); break; default: break; } @@ -755,11 +755,11 @@ void CInputManager::setClickMode(eClickBehaviorMode mode) { } } -void CInputManager::processMouseDownNormal(const IPointer::SButtonEvent& e) { +void CInputManager::processMouseDownNormal(const IPointer::SButtonEvent& e, SP mouse) { // notify the keybind manager static auto PPASSMOUSE = CConfigValue("binds:pass_mouse_when_bound"); - const auto PASS = g_pKeybindManager->onMouseEvent(e); + const auto PASS = g_pKeybindManager->onMouseEvent(e, mouse); static auto PFOLLOWMOUSE = CConfigValue("input:follow_mouse"); static auto PRESIZEONBORDER = CConfigValue("general:resize_on_border"); static auto PBORDERSIZE = CConfigValue("general:border_size"); @@ -884,7 +884,7 @@ void CInputManager::onMouseWheel(IPointer::SAxisEvent e, SP pointer) { if (e.mouse) recheckMouseWarpOnMouseInput(); - bool passEvent = g_pKeybindManager->onAxisEvent(e); + bool passEvent = g_pKeybindManager->onAxisEvent(e, pointer); if (!passEvent) return; diff --git a/src/managers/input/InputManager.hpp b/src/managers/input/InputManager.hpp index ca235a23..0727b7b1 100644 --- a/src/managers/input/InputManager.hpp +++ b/src/managers/input/InputManager.hpp @@ -89,7 +89,7 @@ class CInputManager { void onMouseMoved(IPointer::SMotionEvent); void onMouseWarp(IPointer::SMotionAbsoluteEvent); - void onMouseButton(IPointer::SButtonEvent); + void onMouseButton(IPointer::SButtonEvent, SP); void onMouseWheel(IPointer::SAxisEvent, SP pointer = nullptr); void onPointerFrame(); void onKeyboardKey(const IKeyboard::SKeyEvent&, SP); @@ -233,7 +233,7 @@ class CInputManager { void setupKeyboard(SP keeb); void setupMouse(SP mauz); - void processMouseDownNormal(const IPointer::SButtonEvent& e); + void processMouseDownNormal(const IPointer::SButtonEvent& e, SP); void processMouseDownKill(const IPointer::SButtonEvent& e); bool cursorImageUnlocked(); -- cgit v1.2.3