From 6f2368f8094b8e2f62006a909317e1bb6cc4320f Mon Sep 17 00:00:00 2001 From: vaxerski <43317083+vaxerski@users.noreply.github.com> Date: Tue, 26 Jul 2022 17:30:30 +0200 Subject: [PATCH] Add a pass dispatcher --- src/Compositor.cpp | 55 +++++++++++++++++++ src/Compositor.hpp | 1 + src/managers/KeybindManager.cpp | 96 ++++++++++++++------------------- src/managers/KeybindManager.hpp | 18 ++++--- 4 files changed, 106 insertions(+), 64 deletions(-) diff --git a/src/Compositor.cpp b/src/Compositor.cpp index 9e4957dc..2b7a78c7 100644 --- a/src/Compositor.cpp +++ b/src/Compositor.cpp @@ -1421,4 +1421,59 @@ void CCompositor::scheduleFrameForMonitor(SMonitor* pMonitor) { return; wlr_output_schedule_frame(pMonitor->output); +} + +CWindow* CCompositor::getWindowByRegex(const std::string& regexp) { + eFocusWindowMode mode = MODE_CLASS_REGEX; + + std::regex regexCheck(regexp); + std::string matchCheck; + if (regexp.find("title:") == 0) { + mode = MODE_TITLE_REGEX; + regexCheck = std::regex(regexp.substr(6)); + } else if (regexp.find("address:") == 0) { + mode = MODE_ADDRESS; + matchCheck = regexp.substr(8); + } else if (regexp.find("pid:") == 0) { + mode = MODE_PID; + matchCheck = regexp.substr(4); + } + + for (auto& w : g_pCompositor->m_vWindows) { + if (!w->m_bIsMapped || w->m_bHidden) + continue; + + switch (mode) { + case MODE_CLASS_REGEX: { + const auto windowClass = g_pXWaylandManager->getAppIDClass(w.get()); + if (!std::regex_search(g_pXWaylandManager->getAppIDClass(w.get()), regexCheck)) + continue; + break; + } + case MODE_TITLE_REGEX: { + const auto windowTitle = g_pXWaylandManager->getTitle(w.get()); + if (!std::regex_search(windowTitle, regexCheck)) + continue; + break; + } + case MODE_ADDRESS: { + std::string addr = getFormat("0x%x", w.get()); + if (matchCheck != addr) + continue; + break; + } + case MODE_PID: { + std::string pid = getFormat("%d", w->getPID()); + if (matchCheck != pid) + continue; + break; + } + default: + break; + } + + return w.get(); + } + + return nullptr; } \ No newline at end of file diff --git a/src/Compositor.hpp b/src/Compositor.hpp index 17d076e5..05bf4b7e 100644 --- a/src/Compositor.hpp +++ b/src/Compositor.hpp @@ -148,6 +148,7 @@ public: void scheduleFrameForMonitor(SMonitor*); void addToFadingOutSafe(SLayerSurface*); void addToFadingOutSafe(CWindow*); + CWindow* getWindowByRegex(const std::string&); std::string explicitConfigPath; diff --git a/src/managers/KeybindManager.cpp b/src/managers/KeybindManager.cpp index 61769c38..0548ee0e 100644 --- a/src/managers/KeybindManager.cpp +++ b/src/managers/KeybindManager.cpp @@ -33,6 +33,7 @@ CKeybindManager::CKeybindManager() { m_mDispatchers["focuswindowbyclass"] = focusWindow; m_mDispatchers["focuswindow"] = focusWindow; m_mDispatchers["submap"] = setSubmap; + m_mDispatchers["pass"] = pass; } void CKeybindManager::addKeybind(SKeybind kb) { @@ -93,6 +94,8 @@ bool CKeybindManager::onKeyEvent(wlr_keyboard_key_event* e, SKeyboard* pKeyboard const auto MODS = g_pInputManager->accumulateModsFromAllKBs(); + m_uTimeLastMs = e->time_msec; + m_uLastCode = KEYCODE; bool found = false; if (e->state == WL_KEYBOARD_KEY_STATE_PRESSED) { @@ -1229,69 +1232,20 @@ void CKeybindManager::circleNext(std::string arg) { } void CKeybindManager::focusWindow(std::string regexp) { - eFocusWindowMode mode = MODE_CLASS_REGEX; + const auto PWINDOW = g_pCompositor->getWindowByRegex(regexp); - std::regex regexCheck(regexp); - std::string matchCheck; - if (regexp.find("title:") == 0) { - mode = MODE_TITLE_REGEX; - regexCheck = std::regex(regexp.substr(6)); - } - else if (regexp.find("address:") == 0) { - mode = MODE_ADDRESS; - matchCheck = regexp.substr(8); - } - else if (regexp.find("pid:") == 0) { - mode = MODE_PID; - matchCheck = regexp.substr(4); - } + if (!PWINDOW) + return; - for (auto& w : g_pCompositor->m_vWindows) { - if (!w->m_bIsMapped || w->m_bHidden) - continue; + Debug::log(LOG, "Focusing to window name: %s", PWINDOW->m_szTitle.c_str()); - switch (mode) { - case MODE_CLASS_REGEX: { - const auto windowClass = g_pXWaylandManager->getAppIDClass(w.get()); - if (!std::regex_search(g_pXWaylandManager->getAppIDClass(w.get()), regexCheck)) - continue; - break; - } - case MODE_TITLE_REGEX: { - const auto windowTitle = g_pXWaylandManager->getTitle(w.get()); - if (!std::regex_search(windowTitle, regexCheck)) - continue; - break; - } - case MODE_ADDRESS: { - std::string addr = getFormat("0x%x", w.get()); - if (matchCheck != addr) - continue; - break; - } - case MODE_PID: { - std::string pid = getFormat("%d", w->getPID()); - if (matchCheck != pid) - continue; - break; - } - default: - break; - } + changeworkspace("[internal]" + std::to_string(PWINDOW->m_iWorkspaceID)); + g_pCompositor->focusWindow(PWINDOW); - Debug::log(LOG, "Focusing to window name: %s", w->m_szTitle.c_str()); + const auto MIDPOINT = PWINDOW->m_vRealPosition.goalv() + PWINDOW->m_vRealSize.goalv() / 2.f; - changeworkspace("[internal]" + std::to_string(w->m_iWorkspaceID)); - - g_pCompositor->focusWindow(w.get()); - - const auto MIDPOINT = w->m_vRealPosition.goalv() + w->m_vRealSize.goalv() / 2.f; - - wlr_cursor_warp(g_pCompositor->m_sWLRCursor, nullptr, MIDPOINT.x, MIDPOINT.y); - - break; - } + wlr_cursor_warp(g_pCompositor->m_sWLRCursor, nullptr, MIDPOINT.x, MIDPOINT.y); } void CKeybindManager::setSubmap(std::string submap) { @@ -1311,3 +1265,31 @@ void CKeybindManager::setSubmap(std::string submap) { Debug::log(ERR, "Cannot set submap %s, submap doesn't exist (wasn't registered!)", submap.c_str()); } + +void CKeybindManager::pass(std::string regexp) { + + // find the first window passing the regex + const auto PWINDOW = g_pCompositor->getWindowByRegex(regexp); + + if (!PWINDOW) { + Debug::log(ERR, "pass: window not found"); + return; + } + + const auto PLASTSRF = g_pCompositor->m_pLastFocus; + + const auto KEYBOARD = wlr_seat_get_keyboard(g_pCompositor->m_sSeat.seat); + + if (!KEYBOARD){ + Debug::log(ERR, "No kb in pass?"); + return; + } + + // pass all mf shit + wlr_seat_keyboard_notify_enter(g_pCompositor->m_sSeat.seat, g_pXWaylandManager->getWindowSurface(PWINDOW), KEYBOARD->keycodes, KEYBOARD->num_keycodes, &KEYBOARD->modifiers); + + wlr_seat_keyboard_notify_key(g_pCompositor->m_sSeat.seat, g_pKeybindManager->m_uTimeLastMs, g_pKeybindManager->m_uLastCode - 8, WLR_BUTTON_PRESSED); + wlr_seat_keyboard_notify_key(g_pCompositor->m_sSeat.seat, g_pKeybindManager->m_uTimeLastMs, g_pKeybindManager->m_uLastCode - 8, WLR_BUTTON_RELEASED); + + wlr_seat_keyboard_notify_enter(g_pCompositor->m_sSeat.seat, PLASTSRF, KEYBOARD->keycodes, KEYBOARD->num_keycodes, &KEYBOARD->modifiers); +} \ No newline at end of file diff --git a/src/managers/KeybindManager.hpp b/src/managers/KeybindManager.hpp index 30d3e859..49e1ed96 100644 --- a/src/managers/KeybindManager.hpp +++ b/src/managers/KeybindManager.hpp @@ -21,6 +21,13 @@ struct SKeybind { bool shadowed = false; }; +enum eFocusWindowMode { + MODE_CLASS_REGEX = 0, + MODE_TITLE_REGEX, + MODE_ADDRESS, + MODE_PID +}; + class CKeybindManager { public: CKeybindManager(); @@ -50,6 +57,9 @@ private: SKeybind* m_pActiveKeybind = nullptr; + uint32_t m_uTimeLastMs = 0; + uint32_t m_uLastCode = 0; + bool handleKeybinds(const uint32_t&, const std::string&, const xkb_keysym_t&, const int&, bool, uint32_t); bool handleInternalKeybinds(xkb_keysym_t); @@ -83,15 +93,9 @@ private: static void circleNext(std::string); static void focusWindow(std::string); static void setSubmap(std::string); + static void pass(std::string); friend class CCompositor; - - enum eFocusWindowMode { - MODE_CLASS_REGEX = 0, - MODE_TITLE_REGEX, - MODE_ADDRESS, - MODE_PID - }; }; inline std::unique_ptr g_pKeybindManager;