From bdd9d12257b3fc0370d2dd8fce63ff7ecde3c770 Mon Sep 17 00:00:00 2001 From: WarmUpTill Date: Fri, 18 Jun 2021 20:09:40 +0200 Subject: [PATCH] Add setPreconditions() The idea is to avoid repeated calls to the same functions which lead to the same outcome, but just calling them once in the precondition checks. For now move repeated calls of GetCurrentWindowTitle() and ignoreWindow checks to this function. --- src/advanced-scene-switcher.cpp | 27 +++++++++++++++++++++++++++ src/headers/switcher-data-structs.hpp | 2 ++ src/macro-condition-process.cpp | 4 ---- src/switch-executable.cpp | 20 +------------------- src/switch-idle.cpp | 3 +-- src/switch-pause.cpp | 4 +--- src/switch-window.cpp | 23 +---------------------- 7 files changed, 33 insertions(+), 50 deletions(-) diff --git a/src/advanced-scene-switcher.cpp b/src/advanced-scene-switcher.cpp index dcf43a51..b58dcc6d 100644 --- a/src/advanced-scene-switcher.cpp +++ b/src/advanced-scene-switcher.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -199,6 +200,7 @@ void SwitcherData::Thread() if (checkPause()) { continue; } + setPreconditions(); match = checkForMatch(scene, transition, linger, setPrevSceneAfterLinger, macroMatch); if (stop) { @@ -252,6 +254,31 @@ void SwitcherData::Thread() blog(LOG_INFO, "stopped"); } +void SwitcherData::setPreconditions() +{ + // Window title + lastTitle = currentTitle; + std::string title; + GetCurrentWindowTitle(title); + for (auto &window : ignoreWindowsSwitches) { + bool equals = (title == window); + bool matches = false; + if (!equals) { + try { + std::regex expr(window); + matches = std::regex_match(title, expr); + + } catch (const std::regex_error &) { + } + } + if (equals || matches) { + title = lastTitle; + break; + } + } + currentTitle = title; +} + bool SwitcherData::checkForMatch(OBSWeakSource &scene, OBSWeakSource &transition, int &linger, bool &setPrevSceneAfterLinger, diff --git a/src/headers/switcher-data-structs.hpp b/src/headers/switcher-data-structs.hpp index f5b2b0fb..08e8382c 100644 --- a/src/headers/switcher-data-structs.hpp +++ b/src/headers/switcher-data-structs.hpp @@ -98,6 +98,7 @@ struct SwitcherData { std::deque windowSwitches; std::vector ignoreIdleWindows; std::string lastTitle; + std::string currentTitle; std::deque screenRegionSwitches; @@ -202,6 +203,7 @@ struct SwitcherData { void writeSceneInfoToFile(); void writeToStatusFile(const QString &msg); + void setPreconditions(); bool checkForMatch(OBSWeakSource &scene, OBSWeakSource &transition, int &linger, bool &setPreviousSceneAsMatch, bool ¯oMatch); diff --git a/src/macro-condition-process.cpp b/src/macro-condition-process.cpp index a71341de..af0cb5fe 100644 --- a/src/macro-condition-process.cpp +++ b/src/macro-condition-process.cpp @@ -14,12 +14,8 @@ bool MacroConditionProcess::_registered = MacroConditionFactory::Register( bool MacroConditionProcess::CheckCondition() { - std::string title; QStringList runningProcesses; - QString proc = QString::fromStdString(_process); - - GetCurrentWindowTitle(title); GetProcessList(runningProcesses); bool equals = runningProcesses.contains(proc); diff --git a/src/switch-executable.cpp b/src/switch-executable.cpp index 42138f09..134fa7cc 100644 --- a/src/switch-executable.cpp +++ b/src/switch-executable.cpp @@ -84,29 +84,11 @@ bool SwitcherData::checkExeSwitch(OBSWeakSource &scene, return false; } - std::string title; + std::string title = switcher->currentTitle; QStringList runningProcesses; bool ignored = false; bool match = false; - // Check if current window is ignored - GetCurrentWindowTitle(title); - for (auto &window : ignoreWindowsSwitches) { - // True if ignored switch equals title - bool equals = (title == window); - // True if ignored switch matches title - bool matches = QString::fromStdString(title).contains( - QRegularExpression(QString::fromStdString(window))); - - if (equals || matches) { - ignored = true; - title = lastTitle; - - break; - } - } - lastTitle = title; - // Check for match GetProcessList(runningProcesses); for (ExecutableSwitch &s : executableSwitches) { diff --git a/src/switch-idle.cpp b/src/switch-idle.cpp index 07cdcd00..85622e3c 100644 --- a/src/switch-idle.cpp +++ b/src/switch-idle.cpp @@ -13,9 +13,8 @@ bool SwitcherData::checkIdleSwitch(OBSWeakSource &scene, return false; } - std::string title; + std::string title = switcher->currentTitle; bool ignoreIdle = false; - GetCurrentWindowTitle(title); bool match = false; for (std::string &window : ignoreIdleWindows) { diff --git a/src/switch-pause.cpp b/src/switch-pause.cpp index 3a4511eb..1318d665 100644 --- a/src/switch-pause.cpp +++ b/src/switch-pause.cpp @@ -171,9 +171,7 @@ bool checkPauseWindow(std::string ¤tTitle, std::string &title, bool SwitcherData::checkPause() { bool pauseAll = false; - - std::string title; - GetCurrentWindowTitle(title); + std::string title = switcher->currentTitle; resetPause(); diff --git a/src/switch-window.cpp b/src/switch-window.cpp index ebd83322..6727e197 100644 --- a/src/switch-window.cpp +++ b/src/switch-window.cpp @@ -221,29 +221,8 @@ bool SwitcherData::checkWindowTitleSwitch(OBSWeakSource &scene, return false; } - std::string currentWindowTitle; - GetCurrentWindowTitle(currentWindowTitle); + std::string currentWindowTitle = switcher->currentTitle; bool match = false; - - // Check if current window is ignored - for (auto &window : ignoreWindowsSwitches) { - bool equals = (currentWindowTitle == window); - - try { - std::regex expr(window); - bool matches = - std::regex_match(currentWindowTitle, expr); - - if (equals || matches) { - currentWindowTitle = lastTitle; - break; - } - } catch (const std::regex_error &) { - } - } - - lastTitle = currentWindowTitle; - std::vector windowList; GetWindowList(windowList);