diff --git a/lib/macro/macro-action-macro.cpp b/lib/macro/macro-action-macro.cpp index 30f3e360..4b1b7841 100644 --- a/lib/macro/macro-action-macro.cpp +++ b/lib/macro/macro-action-macro.cpp @@ -196,13 +196,13 @@ void MacroActionMacro::RunActions(Macro *actionMacro) const } if (_runOptions.reevaluateConditionState) { - conditionMacro->CeckMatch(true); + conditionMacro->CheckConditions(true); } if ((_runOptions.logic == RunOptions::Logic::CONDITIONS && - conditionMacro->Matched()) || + conditionMacro->ConditionsMatched()) || (_runOptions.logic == RunOptions::Logic::INVERT_CONDITIONS && - !conditionMacro->Matched())) { + !conditionMacro->ConditionsMatched())) { runActionsHelper(actionMacro, _runOptions.runElseActions, _runOptions.setInputs, _runOptions.inputs); } diff --git a/lib/macro/macro-condition-macro.cpp b/lib/macro/macro-condition-macro.cpp index 16a81254..2a1a3373 100644 --- a/lib/macro/macro-condition-macro.cpp +++ b/lib/macro/macro-condition-macro.cpp @@ -58,7 +58,7 @@ bool MacroConditionMacro::CheckStateCondition() return false; } - return macro->Matched(); + return macro->ConditionsMatched(); } bool MacroConditionMacro::CheckMultiStateCondition() @@ -72,7 +72,7 @@ bool MacroConditionMacro::CheckMultiStateCondition() if (!macro) { continue; } - if (macro->Matched()) { + if (macro->ConditionsMatched()) { matchedCount++; } } diff --git a/lib/macro/macro-dock.cpp b/lib/macro/macro-dock.cpp index ad8cc338..7731a8f7 100644 --- a/lib/macro/macro-dock.cpp +++ b/lib/macro/macro-dock.cpp @@ -138,8 +138,9 @@ void MacroDock::UpdateText() _pauseToggle->setText(macro->Paused() ? _unpauseButtonText.c_str() : _pauseButtonText.c_str()); - _statusText->setText(macro->Matched() ? _conditionsTrueText.c_str() - : _conditionsFalseText.c_str()); + _statusText->setText(macro->ConditionsMatched() + ? _conditionsTrueText.c_str() + : _conditionsFalseText.c_str()); } void MacroDock::Highlight() diff --git a/lib/macro/macro-helpers.cpp b/lib/macro/macro-helpers.cpp index 50f4319e..67520bcd 100644 --- a/lib/macro/macro-helpers.cpp +++ b/lib/macro/macro-helpers.cpp @@ -85,9 +85,11 @@ std::string GetMacroName(Macro *macro) return macro ? macro->Name() : ""; } -int64_t MillisecondsSinceMacroConditionCheck(Macro *macro) +std::chrono::high_resolution_clock::time_point +LastMacroConditionCheckTime(Macro *macro) { - return macro ? macro->MsSinceLastCheck() : 0; + return macro ? macro->LastConditionCheckTime() + : std::chrono::high_resolution_clock::time_point{}; } bool MacroIsStopped(Macro *macro) diff --git a/lib/macro/macro-helpers.hpp b/lib/macro/macro-helpers.hpp index 666bdf45..6cb308de 100644 --- a/lib/macro/macro-helpers.hpp +++ b/lib/macro/macro-helpers.hpp @@ -43,7 +43,8 @@ EXPORT bool MacroSwitchedScene(); EXPORT std::string GetMacroName(Macro *); -EXPORT int64_t MillisecondsSinceMacroConditionCheck(Macro *); +EXPORT std::chrono::high_resolution_clock::time_point +LastMacroConditionCheckTime(Macro *); EXPORT bool MacroIsStopped(Macro *); EXPORT bool MacroIsPaused(Macro *); diff --git a/lib/macro/macro.cpp b/lib/macro/macro.cpp index ccbb7fd5..8885eae0 100644 --- a/lib/macro/macro.cpp +++ b/lib/macro/macro.cpp @@ -238,7 +238,7 @@ bool Macro::CheckConditionHelper( return result; } -bool Macro::CeckMatch(bool ignorePause) +bool Macro::CheckConditions(bool ignorePause) { if (_isGroup) { return false; @@ -309,8 +309,7 @@ bool Macro::PerformActions(bool match, bool forceParallel, bool ignorePause) return ret; } -bool Macro::WasExecutedSince( - const std::chrono::high_resolution_clock::time_point &time) const +bool Macro::WasExecutedSince(const TimePoint &time) const { return _lastExecutionTime > time; } @@ -337,18 +336,6 @@ bool Macro::ShouldRunActions() const return hasActionsToExecute; } -int64_t Macro::MsSinceLastCheck() const -{ - if (_lastCheckTime.time_since_epoch().count() == 0) { - return 0; - } - const auto timePassed = - std::chrono::high_resolution_clock::now() - _lastCheckTime; - return std::chrono::duration_cast(timePassed) - .count() + - 1; -} - void Macro::SetName(const std::string &name) { const bool nameChanged = _name == name; @@ -414,8 +401,7 @@ bool Macro::RunElseActions(bool ignorePause) return RunActionsHelper(_elseActions, ignorePause); } -bool Macro::WasPausedSince( - const std::chrono::high_resolution_clock::time_point &time) const +bool Macro::WasPausedSince(const TimePoint &time) const { return _lastUnpauseTime > time; } @@ -1303,7 +1289,7 @@ bool CheckMacros() { bool matchFound = false; for (const auto &m : macros) { - if (m->CeckMatch() || m->ElseActions().size() > 0) { + if (m->CheckConditions() || m->ElseActions().size() > 0) { matchFound = true; // This has to be performed here for now as actions are // not performed immediately after checking conditions. @@ -1349,7 +1335,7 @@ bool RunMacros() continue; } vblog(LOG_INFO, "running macro: %s", m->Name().c_str()); - if (!m->PerformActions(m->Matched())) { + if (!m->PerformActions(m->ConditionsMatched())) { blog(LOG_WARNING, "abort macro: %s", m->Name().c_str()); } } diff --git a/lib/macro/macro.hpp b/lib/macro/macro.hpp index 476b27a1..f19f75ff 100644 --- a/lib/macro/macro.hpp +++ b/lib/macro/macro.hpp @@ -22,6 +22,8 @@ namespace advss { class MacroDock; class Macro { + using TimePoint = std::chrono::high_resolution_clock::time_point; + public: Macro(const std::string &name = "", const bool addHotkey = false, const bool shortCircuitEvaluation = false); @@ -30,17 +32,17 @@ public: std::string Name() const { return _name; } void SetName(const std::string &name); - bool CeckMatch(bool ignorePause = false); - bool Matched() const { return _matched; } - int64_t MsSinceLastCheck() const; + bool CheckConditions(bool ignorePause = false); + bool ConditionsMatched() const { return _matched; } + TimePoint LastConditionCheckTime() const { return _lastCheckTime; } + bool ShouldRunActions() const; bool PerformActions(bool match, bool forceParallel = false, bool ignorePause = false); void SetPaused(bool pause = true); bool Paused() const { return _paused; } - bool WasPausedSince( - const std::chrono::high_resolution_clock::time_point &) const; + bool WasPausedSince(const TimePoint &) const; void Stop(); bool GetStop() const { return _stop; } @@ -117,8 +119,7 @@ public: void SetElseActionSplitterPosition(const QList); const QList &GetElseActionSplitterPosition() const; bool HasValidSplitterPositions() const; - bool WasExecutedSince( - const std::chrono::high_resolution_clock::time_point &) const; + bool WasExecutedSince(const TimePoint &) const; bool OnChangePreventedActionsRecently(); void ResetUIHelpers(); @@ -171,9 +172,9 @@ private: bool _die = false; bool _stop = false; bool _done = true; - std::chrono::high_resolution_clock::time_point _lastCheckTime{}; - std::chrono::high_resolution_clock::time_point _lastUnpauseTime{}; - std::chrono::high_resolution_clock::time_point _lastExecutionTime{}; + TimePoint _lastCheckTime{}; + TimePoint _lastUnpauseTime{}; + TimePoint _lastExecutionTime{}; std::thread _backgroundThread; std::vector _helperThreads; diff --git a/plugins/base/macro-condition-date.cpp b/plugins/base/macro-condition-date.cpp index e65e3ce2..e1e45698 100644 --- a/plugins/base/macro-condition-date.cpp +++ b/plugins/base/macro-condition-date.cpp @@ -180,11 +180,16 @@ bool MacroConditionDate::CheckCondition() if (!m) { return false; } - auto msSinceLastCheck = MillisecondsSinceMacroConditionCheck(m); + const auto timePassed = std::chrono::high_resolution_clock::now() - + LastMacroConditionCheckTime(m); + const auto msSinceLastCheck = + std::chrono::duration_cast( + timePassed); + if (_dayOfWeekCheck) { - return CheckDayOfWeek(msSinceLastCheck); + return CheckDayOfWeek(msSinceLastCheck.count()); } - return CheckRegularDate(msSinceLastCheck); + return CheckRegularDate(msSinceLastCheck.count()); } bool MacroConditionDate::Save(obs_data_t *obj) const