Reduce dependencies of macros to switcher data

This commit is contained in:
WarmUpTill 2023-12-24 01:46:13 +01:00 committed by WarmUpTill
parent 71e20c4983
commit 0cf9e1dd81
22 changed files with 206 additions and 193 deletions

View File

@ -1,5 +1,6 @@
#include "advanced-scene-switcher.hpp"
#include "switcher-data.hpp"
#include "macro-helpers.hpp"
#include "status-control.hpp"
#include "scene-switch-helpers.hpp"
#include "curl-helper.hpp"
@ -236,7 +237,6 @@ void SwitcherData::Thread()
// if a longer transition is used than the configured check interval
bool setPrevSceneAfterLinger = false;
bool macroMatch = false;
macroSceneSwitched = false;
endTime = std::chrono::high_resolution_clock::now();
auto runTime =
std::chrono::duration_cast<std::chrono::milliseconds>(
@ -434,7 +434,7 @@ bool SwitcherData::CheckForMatch(OBSWeakSource &scene,
static void ResetMacros()
{
for (auto &m : switcher->macros) {
for (auto &m : GetMacros()) {
m->ResetRunCount();
m->ResetTimers();
}
@ -477,9 +477,9 @@ void SwitcherData::Stop()
if (th && th->isRunning()) {
stop = true;
cv.notify_all();
abortMacroWait = true;
macroWaitCv.notify_all();
macroTransitionCv.notify_all();
SetMacroAbortWait(true);
GetMacroWaitCV().notify_all();
GetMacroTransitionCV().notify_all();
// Not waiting if a dialog was closed is a workaround to avoid
// deadlocks when a variable input dialog is opened while Stop()
@ -646,10 +646,10 @@ static void handleShutdown()
return;
}
switcher->obsIsShuttingDown = true;
if (switcher->shutdownConditionCount) {
if (ShutdownCheckIsNecessary()) {
switcher->Stop();
switcher->CheckMacros();
switcher->RunMacros();
CheckMacros();
RunMacros();
// Unfortunately this will not work as OBS will now allow saving
// the scene collection data at this point, So any OBS specific

View File

@ -1,6 +1,8 @@
#include "macro-action-edit.hpp"
#include "macro-helpers.hpp"
#include "macro.hpp"
#include "macro-properties.hpp"
#include "advanced-scene-switcher.hpp"
#include "switcher-data.hpp"
#include "macro-action-scene-switch.hpp"
#include "section.hpp"
#include "switch-button.hpp"
@ -28,7 +30,8 @@ static inline void populateActionSelection(QComboBox *list)
MacroActionEdit::MacroActionEdit(QWidget *parent,
std::shared_ptr<MacroAction> *entryData,
const std::string &id)
: MacroSegmentEdit(switcher->macroProperties._highlightActions, parent),
: MacroSegmentEdit(GetGlobalMacroProperties()._highlightActions,
parent),
_actionSelection(new FilterComboBox()),
_enable(new SwitchButton()),
_entryData(entryData)
@ -82,7 +85,7 @@ void MacroActionEdit::ActionSelectionChanged(const QString &text)
auto idx = _entryData->get()->GetIndex();
auto macro = _entryData->get()->GetMacro();
{
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
_entryData->reset();
*_entryData = MacroActionFactory::Create(id, macro);
(*_entryData)->SetIndex(idx);
@ -133,7 +136,7 @@ void MacroActionEdit::ActionEnableChanged(bool value)
return;
}
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
(*_entryData)->SetEnabled(value);
SetDisableEffect(!value);
}
@ -177,7 +180,7 @@ void AdvSceneSwitcher::AddMacroAction(int idx)
id = temp.GetId();
}
{
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
macro->Actions().emplace(
macro->Actions().begin() + idx,
MacroActionFactory::Create(id, macro.get()));
@ -228,11 +231,11 @@ void AdvSceneSwitcher::RemoveMacroAction(int idx)
}
{
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
ui->actionsList->Remove(idx);
macro->Actions().erase(macro->Actions().begin() + idx);
switcher->abortMacroWait = true;
switcher->macroWaitCv.notify_all();
SetMacroAbortWait(true);
GetMacroWaitCV().notify_all();
macro->UpdateActionIndices();
SetActionData(*macro);
}
@ -373,7 +376,7 @@ void AdvSceneSwitcher::SwapActions(Macro *m, int pos1, int pos2)
std::swap(pos1, pos2);
}
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
iter_swap(m->Actions().begin() + pos1, m->Actions().begin() + pos2);
m->UpdateActionIndices();
auto widget1 = static_cast<MacroActionEdit *>(
@ -433,7 +436,7 @@ void AdvSceneSwitcher::MacroElseActionReorder(int to, int from)
return;
}
{
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
auto action = macro->ElseActions().at(from);
macro->ElseActions().erase(macro->ElseActions().begin() + from);
macro->ElseActions().insert(macro->ElseActions().begin() + to,
@ -466,7 +469,7 @@ void AdvSceneSwitcher::AddMacroElseAction(int idx)
id = temp.GetId();
}
{
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
macro->ElseActions().emplace(
macro->ElseActions().begin() + idx,
MacroActionFactory::Create(id, macro.get()));
@ -498,11 +501,11 @@ void AdvSceneSwitcher::RemoveMacroElseAction(int idx)
}
{
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
ui->elseActionsList->Remove(idx);
macro->ElseActions().erase(macro->ElseActions().begin() + idx);
switcher->abortMacroWait = true;
switcher->macroWaitCv.notify_all();
SetMacroAbortWait(true);
GetMacroWaitCV().notify_all();
macro->UpdateElseActionIndices();
SetActionData(*macro);
}
@ -520,7 +523,7 @@ void AdvSceneSwitcher::SwapElseActions(Macro *m, int pos1, int pos2)
std::swap(pos1, pos2);
}
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
iter_swap(m->ElseActions().begin() + pos1,
m->ElseActions().begin() + pos2);
m->UpdateElseActionIndices();
@ -583,7 +586,7 @@ void AdvSceneSwitcher::MacroActionReorder(int to, int from)
return;
}
{
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
auto action = macro->Actions().at(from);
macro->Actions().erase(macro->Actions().begin() + from);
macro->Actions().insert(macro->Actions().begin() + to, action);

View File

@ -1,5 +1,7 @@
#include "macro-action-scene-switch.hpp"
#include "switcher-data.hpp"
#include "macro-helpers.hpp"
#include "plugin-state-helpers.hpp"
#include "macro.hpp"
#include "scene-switch-helpers.hpp"
#include "utility.hpp"
@ -7,7 +9,7 @@ namespace advss {
using namespace std::chrono_literals;
const std::string MacroActionSwitchScene::id = "scene_switch";
const std::string MacroActionSwitchScene::id = GetSceneSwitchActionId().data();
bool MacroActionSwitchScene::_registered = MacroActionFactory::Register(
MacroActionSwitchScene::id,
@ -33,9 +35,9 @@ static void waitForTransitionChange(OBSWeakSource &transition,
}
bool stillTransitioning = true;
while (stillTransitioning && !switcher->abortMacroWait &&
while (stillTransitioning && !MacroWaitShouldAbort() &&
!macro->GetStop()) {
switcher->macroTransitionCv.wait_for(*lock, time);
GetMacroTransitionCV().wait_for(*lock, time);
float t = obs_transition_get_time(source);
stillTransitioning = t < 1.0f && t > 0.0f;
}
@ -49,8 +51,8 @@ static void waitForTransitionChangeFixedDuration(
auto time = std::chrono::high_resolution_clock::now() +
std::chrono::milliseconds(duration);
while (!switcher->abortMacroWait && !macro->GetStop()) {
if (switcher->macroTransitionCv.wait_until(*lock, time) ==
while (!MacroWaitShouldAbort() && !macro->GetStop()) {
if (GetMacroTransitionCV().wait_until(*lock, time) ==
std::cv_status::timeout) {
break;
}
@ -91,11 +93,16 @@ static OBSWeakSource getOverrideTransition(OBSWeakSource &scene)
return transition;
}
static int getExpectedTransitionDuration(OBSWeakSource &scene, OBSWeakSource &t,
static int getExpectedTransitionDuration(OBSWeakSource &scene,
OBSWeakSource &transiton_,
double duration)
{
OBSWeakSource transition = t;
if (!switcher->transitionOverrideOverride) {
OBSWeakSource transition = transiton_;
// If we are not modifying the transition override, we will have to
// check, if a transition override is set by the user and use its
// duration instead
if (!ShouldModifyTransitionOverrides()) {
auto overrideTransition = getOverrideTransition(scene);
if (overrideTransition) {
transition = overrideTransition;
@ -104,6 +111,7 @@ static int getExpectedTransitionDuration(OBSWeakSource &scene, OBSWeakSource &t,
}
}
}
if (isUsingFixedLengthTransition(transition)) {
return -1; // no API is available to access the fixed duration
}
@ -118,9 +126,9 @@ bool MacroActionSwitchScene::WaitForTransition(OBSWeakSource &scene,
{
const int expectedTransitionDuration = getExpectedTransitionDuration(
scene, transition, _duration.Seconds());
switcher->abortMacroWait = false;
SetMacroAbortWait(false);
std::unique_lock<std::mutex> lock(switcher->m);
std::unique_lock<std::mutex> lock(*GetMutex());
if (expectedTransitionDuration < 0) {
waitForTransitionChange(transition, &lock, GetMacro());
} else {
@ -128,7 +136,7 @@ bool MacroActionSwitchScene::WaitForTransition(OBSWeakSource &scene,
&lock, GetMacro());
}
return !switcher->abortMacroWait;
return !MacroWaitShouldAbort();
}
bool MacroActionSwitchScene::PerformAction()
@ -145,8 +153,7 @@ bool MacroActionSwitchScene::PerformAction()
auto transition = _transition.GetTransition();
SwitchScene({scene, transition, (int)(_duration.Milliseconds())},
obs_frontend_preview_program_mode_active());
if (_blockUntilTransitionDone && scene &&
scene != switcher->currentScene) {
if (_blockUntilTransitionDone && scene && scene != GetCurrentScene()) {
return WaitForTransition(scene, transition);
}
return true;

View File

@ -1,5 +1,7 @@
#include "macro-action-wait.hpp"
#include "switcher-data.hpp"
#include "macro-helpers.hpp"
#include "macro.hpp"
#include "sync-helpers.hpp"
#include "utility.hpp"
#include <random>
@ -24,8 +26,8 @@ static std::default_random_engine re(rd());
static void waitHelper(std::unique_lock<std::mutex> *lock, Macro *macro,
std::chrono::high_resolution_clock::time_point &time)
{
while (!switcher->abortMacroWait && !macro->GetStop()) {
if (switcher->macroWaitCv.wait_until(*lock, time) ==
while (!MacroWaitShouldAbort() && !macro->GetStop()) {
if (GetMacroWaitCV().wait_until(*lock, time) ==
std::cv_status::timeout) {
break;
}
@ -53,11 +55,11 @@ bool MacroActionWait::PerformAction()
auto time = std::chrono::high_resolution_clock::now() +
std::chrono::milliseconds((int)(sleepDuration * 1000));
switcher->abortMacroWait = false;
std::unique_lock<std::mutex> lock(switcher->m);
SetMacroAbortWait(false);
std::unique_lock<std::mutex> lock(*GetMutex());
waitHelper(&lock, GetMacro(), time);
return !switcher->abortMacroWait;
return !MacroWaitShouldAbort();
}
bool MacroActionWait::Save(obs_data_t *obj) const

View File

@ -1,6 +1,7 @@
#include "macro-condition-edit.hpp"
#include "macro.hpp"
#include "macro-properties.hpp"
#include "advanced-scene-switcher.hpp"
#include "switcher-data.hpp"
#include "macro-condition-scene.hpp"
#include "section.hpp"
#include "utility.hpp"
@ -118,7 +119,7 @@ void DurationModifierEdit::Collapse(bool collapse)
MacroConditionEdit::MacroConditionEdit(
QWidget *parent, std::shared_ptr<MacroCondition> *entryData,
const std::string &id, bool root)
: MacroSegmentEdit(switcher->macroProperties._highlightConditions,
: MacroSegmentEdit(GetGlobalMacroProperties()._highlightConditions,
parent),
_logicSelection(new QComboBox()),
_conditionSelection(new FilterComboBox()),

View File

@ -1,5 +1,7 @@
#include "macro-condition-media.hpp"
#include "switcher-data.hpp"
#include "macro.hpp"
#include "plugin-state-helpers.hpp"
#include "scene-switch-helpers.hpp"
#include "utility.hpp"
namespace advss {
@ -167,7 +169,7 @@ bool MacroConditionMedia::CheckMediaMatch()
void MacroConditionMedia::HandleSceneChange()
{
UpdateMediaSourcesOfSceneList();
_lastConfigureScene = switcher->currentScene;
_lastConfigureScene = GetCurrentScene();
}
bool MacroConditionMedia::CheckCondition()
@ -194,7 +196,7 @@ bool MacroConditionMedia::CheckCondition()
break;
}
if (_lastConfigureScene != switcher->currentScene) {
if (_lastConfigureScene != GetCurrentScene()) {
HandleSceneChange();
}

View File

@ -1,5 +1,6 @@
#include "macro-condition-plugin-state.hpp"
#include "switcher-data.hpp"
#include "macro-helpers.hpp"
#include "plugin-state-helpers.hpp"
#include "utility.hpp"
namespace advss {
@ -33,7 +34,7 @@ const static std::map<MacroConditionPluginState::Condition, std::string>
MacroConditionPluginState::~MacroConditionPluginState()
{
if (_condition == Condition::OBS_SHUTDOWN) {
switcher->shutdownConditionCount--;
GetShutdownConditionCount()--;
}
}
@ -41,15 +42,15 @@ bool MacroConditionPluginState::CheckCondition()
{
switch (_condition) {
case Condition::PLUGIN_SCENE_CHANGE:
return switcher->macroSceneSwitched;
return MacroSwitchedScene();
case Condition::PLUGIN_RUNNING:
return true;
case Condition::OBS_SHUTDOWN:
return switcher->obsIsShuttingDown;
return OBSIsShuttingDown();
case Condition::PLUGIN_START:
return switcher->firstInterval;
return IsFirstInterval();
case Condition::PLUGIN_RESTART:
return switcher->firstIntervalAfterStop;
return IsFirstIntervalAfterStop();
case Condition::SCENE_COLLECTION_CHANGE:
if (_firstCheckAfterSceneCollectionChange) {
_firstCheckAfterSceneCollectionChange = false;
@ -83,25 +84,33 @@ bool MacroConditionPluginState::Load(obs_data_t *obj)
obs_data_get_int(obj, "condition"));
switch (oldValue) {
case PluginStateCondition::SCENE_SWITCHED:
_condition = Condition::PLUGIN_SCENE_CHANGE;
SetCondition(Condition::PLUGIN_SCENE_CHANGE);
break;
case PluginStateCondition::RUNNING:
_condition = Condition::PLUGIN_RUNNING;
SetCondition(Condition::PLUGIN_RUNNING);
break;
case PluginStateCondition::SHUTDOWN:
_condition = Condition::OBS_SHUTDOWN;
SetCondition(Condition::OBS_SHUTDOWN);
break;
}
} else {
_condition = static_cast<Condition>(
obs_data_get_int(obj, "condition"));
}
if (_condition == Condition::OBS_SHUTDOWN) {
switcher->shutdownConditionCount++;
SetCondition(static_cast<Condition>(
obs_data_get_int(obj, "condition")));
}
return true;
}
void MacroConditionPluginState::SetCondition(Condition newValue)
{
if (_condition == Condition::OBS_SHUTDOWN) {
GetShutdownConditionCount()--;
}
if (newValue == Condition::OBS_SHUTDOWN) {
GetShutdownConditionCount()++;
}
_condition = newValue;
}
static inline void populateConditionSelection(QComboBox *list)
{
for (const auto &[action, name] : pluginStateConditionTypes) {
@ -148,17 +157,9 @@ void MacroConditionPluginStateEdit::ConditionChanged(int idx)
}
auto lock = LockContext();
if (_entryData->_condition ==
MacroConditionPluginState::Condition::OBS_SHUTDOWN) {
switcher->shutdownConditionCount--;
}
_entryData->_condition =
_entryData->SetCondition(
static_cast<MacroConditionPluginState::Condition>(
_condition->itemData(idx).toInt());
if (_entryData->_condition ==
MacroConditionPluginState::Condition::OBS_SHUTDOWN) {
switcher->shutdownConditionCount++;
}
_condition->itemData(idx).toInt()));
SetWidgetVisibility();
}
@ -168,15 +169,15 @@ void MacroConditionPluginStateEdit::UpdateEntryData()
return;
}
_condition->setCurrentIndex(
_condition->findData(static_cast<int>(_entryData->_condition)));
_condition->setCurrentIndex(_condition->findData(
static_cast<int>(_entryData->GetCondition())));
SetWidgetVisibility();
}
void MacroConditionPluginStateEdit::SetWidgetVisibility()
{
_shutdownLimitation->setVisible(
_entryData->_condition ==
_entryData->GetCondition() ==
MacroConditionPluginState::Condition::OBS_SHUTDOWN);
adjustSize();
updateGeometry();

View File

@ -26,10 +26,11 @@ public:
SCENE_COLLECTION_CHANGE,
PLUGIN_SCENE_CHANGE,
};
Condition _condition = Condition::PLUGIN_SCENE_CHANGE;
void SetCondition(Condition);
Condition GetCondition() const { return _condition; }
private:
Condition _condition = Condition::PLUGIN_SCENE_CHANGE;
bool _firstCheckAfterSceneCollectionChange = true;
const static uint32_t _version;

View File

@ -1,5 +1,4 @@
#include "macro-condition-process.hpp"
#include "switcher-data.hpp"
#include "platform-funcs.hpp"
#include "utility.hpp"
@ -172,8 +171,9 @@ void MacroConditionProcessEdit::FocusChanged(int state)
void MacroConditionProcessEdit::UpdateFocusProcess()
{
_focusProcess->setText(
QString::fromStdString(switcher->currentForegroundProcess));
std::string name;
GetForegroundProcessName(name);
_focusProcess->setText(QString::fromStdString(name));
}
void MacroConditionProcessEdit::SetWidgetVisibility()

View File

@ -1,5 +1,5 @@
#include "macro-condition-scene.hpp"
#include "switcher-data.hpp"
#include "scene-switch-helpers.hpp"
#include "utility.hpp"
namespace advss {
@ -53,23 +53,22 @@ static OBSWeakSource getCurrentSceneHelper(bool useTransitionTargetScene)
obs_source_release(current);
return weak;
}
return switcher->currentScene;
return GetCurrentScene();
}
static OBSWeakSource getPreviousSceneHelper(bool useTransitionTargetScene)
{
if (switcher->AnySceneTransitionStarted() && useTransitionTargetScene) {
return switcher->currentScene;
if (AnySceneTransitionStarted() && useTransitionTargetScene) {
return GetCurrentScene();
}
return switcher->previousScene;
return GetPreviousScene();
}
bool MacroConditionScene::CheckCondition()
{
bool sceneChanged = _lastSceneChangeTime !=
switcher->lastSceneChangeTime;
bool sceneChanged = _lastSceneChangeTime != GetLastSceneChangeTime();
if (sceneChanged) {
_lastSceneChangeTime = switcher->lastSceneChangeTime;
_lastSceneChangeTime = GetLastSceneChangeTime();
}
switch (_type) {
@ -95,14 +94,14 @@ bool MacroConditionScene::CheckCondition()
return scene == _scene.GetScene(false);
}
case Type::CHANGED:
SetVariableValue(GetWeakSourceName(switcher->currentScene));
SetVariableValue(GetWeakSourceName(GetCurrentScene()));
SetTempVarValue("current",
GetWeakSourceName(switcher->currentScene));
GetWeakSourceName(GetCurrentScene()));
return sceneChanged;
case Type::NOT_CHANGED:
SetVariableValue(GetWeakSourceName(switcher->currentScene));
SetVariableValue(GetWeakSourceName(GetCurrentScene()));
SetTempVarValue("current",
GetWeakSourceName(switcher->currentScene));
GetWeakSourceName(GetCurrentScene()));
return !sceneChanged;
case Type::CURRENT_PATTERN: {
auto scene = getCurrentSceneHelper(_useTransitionTargetScene);
@ -189,8 +188,8 @@ bool MacroConditionScene::Load(obs_data_t *obj)
break;
default:
blog(LOG_WARNING,
"failed to convert scene condition type (%d) for macro %s",
oldType, GetMacro()->Name().c_str());
"failed to convert scene condition type (%d)",
oldType);
_type = Type::CURRENT;
break;
}

View File

@ -1,5 +1,5 @@
#include "macro-condition-transition.hpp"
#include "switcher-data.hpp"
#include "scene-switch-helpers.hpp"
#include "utility.hpp"
namespace advss {
@ -50,8 +50,8 @@ static bool isTargetScene(OBSWeakSource &target)
bool MacroConditionTransition::CheckCondition()
{
bool anyTransitionEnded = _lastTransitionEndTime !=
switcher->lastTransitionEndTime;
bool anyTransitionStarted = switcher->AnySceneTransitionStarted();
GetLastTransitionEndTime();
bool anyTransitionStarted = AnySceneTransitionStarted();
bool transitionStarted = false;
bool transitionEnded = false;
@ -82,7 +82,7 @@ bool MacroConditionTransition::CheckCondition()
break;
case TransitionCondition::TRANSITION_SOURCE:
ret = anyTransitionStarted &&
_scene.GetScene() == switcher->currentScene;
_scene.GetScene() == GetCurrentScene();
break;
case TransitionCondition::TRANSITION_TARGET: {
auto scene = _scene.GetScene();
@ -101,7 +101,7 @@ bool MacroConditionTransition::CheckCondition()
_ended = false;
}
if (anyTransitionEnded) {
_lastTransitionEndTime = switcher->lastTransitionEndTime;
_lastTransitionEndTime = GetLastTransitionEndTime();
}
return ret;

View File

@ -1,5 +1,4 @@
#include "macro-condition-websocket.hpp"
#include "switcher-data.hpp"
#include "utility.hpp"
#include <regex>

View File

@ -1,5 +1,5 @@
#include "macro-condition-window.hpp"
#include "switcher-data.hpp"
#include "plugin-state-helpers.hpp"
#include "platform-funcs.hpp"
#include "utility.hpp"
@ -43,7 +43,8 @@ static bool windowContainsText(const std::string &window,
bool MacroConditionWindow::WindowMatchesRequirements(
const std::string &window) const
{
const bool focusCheckOK = (!_focus || window == switcher->currentTitle);
const bool focusCheckOK =
(!_focus || window == ForegroundWindowTitle());
if (!focusCheckOK) {
return false;
}
@ -104,13 +105,13 @@ void MacroConditionWindow::SetVariableValueBasedOnMatch(
auto text = GetTextInWindow(matchWindow);
SetVariableValue(text.value_or(""));
} else {
SetVariableValue(switcher->currentTitle);
SetVariableValue(ForegroundWindowTitle());
}
}
static bool foregroundWindowChanged()
{
return switcher->currentTitle != switcher->lastTitle;
return ForegroundWindowTitle() != PreviousForegroundWindowTitle();
}
bool MacroConditionWindow::CheckCondition()
@ -432,7 +433,7 @@ void MacroConditionWindowEdit::WindowFocusChanged(int state)
void MacroConditionWindowEdit::UpdateFocusWindow()
{
_focusWindow->setText(QString::fromStdString(switcher->currentTitle));
_focusWindow->setText(QString::fromStdString(ForegroundWindowTitle()));
}
void MacroConditionWindowEdit::SetWidgetVisibility()

View File

@ -4,7 +4,7 @@
// so it makes sense to include them here:
#include "log-helper.hpp"
#include "obs-module-helper.hpp"
#include "sync-helper.hpp"
#include "sync-helpers.hpp"
#include "temp-variable.hpp"
#include <QWidget>

View File

@ -1,6 +1,6 @@
#include "macro-selection.hpp"
#include "macro.hpp"
#include "advanced-scene-switcher.hpp"
#include "switcher-data.hpp"
#include "utility.hpp"
#include <QStandardItemModel>
@ -12,7 +12,7 @@ MacroSelection::MacroSelection(QWidget *parent)
: FilterComboBox(parent,
obs_module_text("AdvSceneSwitcher.selectMacro"))
{
for (const auto &m : switcher->macros) {
for (const auto &m : GetMacros()) {
if (m->IsGroup()) {
continue;
}

View File

@ -63,7 +63,7 @@ bool AdvSceneSwitcher::AddNewMacro(std::shared_ptr<Macro> &res,
}
res = std::make_shared<Macro>(
name, switcher->macroProperties._newMacroRegisterHotkeys);
name, GetGlobalMacroProperties()._newMacroRegisterHotkeys);
return true;
}
@ -197,8 +197,8 @@ static void addGroupSubitems(std::vector<std::shared_ptr<Macro>> &macros,
subitems.reserve(group->GroupSize());
// Find all subitems
for (auto it = switcher->macros.begin(); it < switcher->macros.end();
it++) {
auto allMacros = GetMacros();
for (auto it = allMacros.begin(); it < allMacros.end(); it++) {
if ((*it)->Name() == group->Name()) {
for (uint32_t i = 1; i <= group->GroupSize(); i++) {
subitems.emplace_back(*std::next(it, i));
@ -429,7 +429,7 @@ void AdvSceneSwitcher::ImportMacros()
}
importedMacros.emplace_back(macro);
switcher->macros.emplace_back(macro);
GetMacros().emplace_back(macro);
if (groupSize > 0 && !macro->IsGroup()) {
Macro::PrepareMoveToGroup(group, macro);
groupSize--;
@ -449,8 +449,8 @@ void AdvSceneSwitcher::ImportMacros()
macro->PostLoad();
}
ui->macros->Reset(switcher->macros,
switcher->macroProperties._highlightExecuted);
ui->macros->Reset(GetMacros(),
GetGlobalMacroProperties()._highlightExecuted);
}
void AdvSceneSwitcher::on_macroName_editingFinished()
@ -729,8 +729,8 @@ void AdvSceneSwitcher::MacroSelectionChanged()
void AdvSceneSwitcher::HighlightOnChange()
{
if (!switcher->macroProperties._highlightActions &&
!switcher->macroProperties._highlightExecuted) {
if (!GetGlobalMacroProperties()._highlightActions &&
!GetGlobalMacroProperties()._highlightExecuted) {
return;
}
@ -747,13 +747,13 @@ void AdvSceneSwitcher::HighlightOnChange()
void AdvSceneSwitcher::on_macroProperties_clicked()
{
MacroProperties prop = switcher->macroProperties;
MacroProperties prop = GetGlobalMacroProperties();
bool accepted = MacroPropertiesDialog::AskForSettings(
this, prop, GetSelectedMacro().get());
if (!accepted) {
return;
}
switcher->macroProperties = prop;
GetGlobalMacroProperties() = prop;
emit HighlightMacrosChanged(prop._highlightExecuted);
emit HighlightActionsChanged(prop._highlightActions);
emit HighlightConditionsChanged(prop._highlightConditions);
@ -789,11 +789,11 @@ void AdvSceneSwitcher::SetupMacroTab()
{
ui->macroElseActions->installEventFilter(this);
if (switcher->macros.size() == 0 && !switcher->disableHints) {
if (GetMacros().size() == 0 && !switcher->disableHints) {
addPulse = PulseWidget(ui->macroAdd, QColor(Qt::green));
}
ui->macros->Reset(switcher->macros,
switcher->macroProperties._highlightExecuted);
ui->macros->Reset(GetMacros(),
GetGlobalMacroProperties()._highlightExecuted);
connect(ui->macros, SIGNAL(MacroSelectionAboutToChange()), this,
SLOT(MacroSelectionAboutToChange()));
connect(ui->macros, SIGNAL(MacroSelectionChanged()), this,

View File

@ -1,7 +1,7 @@
#include "macro-tree.hpp"
#include "macro.hpp"
#include "sync-helpers.hpp"
#include "utility.hpp"
#include "switcher-data.hpp"
#include <obs.h>
#include <string>
@ -395,7 +395,7 @@ CountItemsVisibleInModel(const std::deque<std::shared_ptr<Macro>> &macros)
void MacroTreeModel::Add(std::shared_ptr<Macro> item)
{
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
auto idx = CountItemsVisibleInModel(_macros);
beginInsertRows(QModelIndex(), idx, idx);
_macros.emplace_back(item);
@ -408,7 +408,7 @@ void MacroTreeModel::Add(std::shared_ptr<Macro> item)
void MacroTreeModel::Remove(std::shared_ptr<Macro> item)
{
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
auto uiStartIdx = GetItemModelIndex(item);
if (uiStartIdx == -1) {
return;
@ -643,7 +643,7 @@ void MacroTreeModel::GroupSelectedItems(QModelIndexList &indices)
return;
}
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
QString name = GetNewGroupName();
std::vector<std::shared_ptr<Macro>> itemsToGroup;
itemsToGroup.reserve(indices.size());
@ -691,7 +691,7 @@ void MacroTreeModel::UngroupSelectedGroups(QModelIndexList &indices)
return;
}
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
for (int i = indices.count() - 1; i >= 0; i--) {
std::shared_ptr<Macro> item = _macros[ModelIndexToMacroIndex(
indices[i].row(), _macros)];
@ -1055,7 +1055,7 @@ void MacroTree::dropEvent(QDropEvent *event)
}
// Move items in backend
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
int to = row;
try {
to = ModelIndexToMacroIndex(row, items);
@ -1181,7 +1181,7 @@ void MacroTree::Remove(std::shared_ptr<Macro> item) const
void MacroTree::Up(std::shared_ptr<Macro> item) const
{
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
auto above = GetModel()->Neighbor(item, true);
if (!above) {
return;
@ -1207,7 +1207,7 @@ void MacroTree::Up(std::shared_ptr<Macro> item) const
void MacroTree::Down(std::shared_ptr<Macro> item) const
{
std::lock_guard<std::mutex> lock(switcher->m);
auto lock = LockContext();
auto below = GetModel()->Neighbor(item, false);
if (!below) {
return;

View File

@ -1,20 +1,24 @@
#include "macro.hpp"
#include "macro-action-edit.hpp"
#include "macro-condition-edit.hpp"
#include "macro-action-factory.hpp"
#include "macro-condition-factory.hpp"
#include "macro-dock.hpp"
#include "macro-action-scene-switch.hpp"
#include "switcher-data.hpp"
#include "macro-helpers.hpp"
#include "plugin-state-helpers.hpp"
#include "hotkey.hpp"
#include "sync-helpers.hpp"
#include "utility.hpp"
#include <limits>
#undef max
#include <chrono>
#include <unordered_map>
#include <QAction>
#include <QMainWindow>
namespace advss {
constexpr int perfLogThreshold = 300;
static constexpr int perfLogThreshold = 300;
static std::deque<std::shared_ptr<Macro>> macros;
Macro::Macro(const std::string &name, const bool addHotkey)
{
@ -33,7 +37,7 @@ Macro::~Macro()
// Keep the dock widgets in case of shutdown so they can be restored by
// OBS on startup
if (!switcher->obsIsShuttingDown) {
if (!OBSIsShuttingDown()) {
RemoveDock();
}
}
@ -53,9 +57,8 @@ Macro::CreateGroup(const std::string &name,
void Macro::RemoveGroup(std::shared_ptr<Macro> group)
{
auto it = std::find(switcher->macros.begin(), switcher->macros.end(),
group);
if (it == switcher->macros.end()) {
auto it = std::find(macros.begin(), macros.end(), group);
if (it == macros.end()) {
return;
}
@ -65,12 +68,12 @@ void Macro::RemoveGroup(std::shared_ptr<Macro> group)
(*m)->SetParent(nullptr);
}
switcher->macros.erase(it);
macros.erase(it);
}
void Macro::PrepareMoveToGroup(Macro *group, std::shared_ptr<Macro> item)
{
for (auto &m : switcher->macros) {
for (const auto &m : macros) {
if (m.get() == group) {
PrepareMoveToGroup(m, item);
return;
@ -351,7 +354,7 @@ void Macro::AddHelperThread(std::thread &&newThread)
void Macro::Stop()
{
_stop = true;
switcher->macroWaitCv.notify_all();
GetMacroWaitCV().notify_all();
for (auto &t : _helperThreads) {
if (t.joinable()) {
t.join();
@ -743,15 +746,13 @@ bool Macro::PostLoad()
bool Macro::SwitchesScene() const
{
MacroActionSwitchScene temp(nullptr);
auto sceneSwitchId = temp.GetId();
for (const auto &a : _actions) {
if (a->GetId() == sceneSwitchId) {
if (a->GetId() == GetSceneSwitchActionId()) {
return true;
}
}
for (const auto &a : _elseActions) {
if (a->GetId() == sceneSwitchId) {
if (a->GetId() == GetSceneSwitchActionId()) {
return true;
}
}
@ -947,7 +948,7 @@ void Macro::EnableDock(bool value)
// geometry set here.
// The function calls here are only intended to attempt to restore the
// dock status when switching scene collections.
if (switcher->startupLoadDone) {
if (InitialLoadIsComplete()) {
_dock->setVisible(_dockIsVisible);
if (window->dockWidgetArea(_dock) != _dockArea) {
window->addDockWidget(_dockArea, _dock);
@ -1163,12 +1164,10 @@ void Macro::SetHotkeysDesc() const
_name, _togglePauseHotkey);
}
void SwitcherData::SaveMacros(obs_data_t *obj)
void SaveMacros(obs_data_t *obj)
{
switcher->macroProperties.Save(obj);
obs_data_array_t *macroArray = obs_data_array_create();
for (auto &m : macros) {
for (const auto &m : macros) {
obs_data_t *array_obj = obs_data_create();
m->Save(array_obj);
@ -1180,10 +1179,9 @@ void SwitcherData::SaveMacros(obs_data_t *obj)
obs_data_array_release(macroArray);
}
void SwitcherData::LoadMacros(obs_data_t *obj)
void LoadMacros(obs_data_t *obj)
{
Hotkey::ClearAllHotkeys();
switcher->macroProperties.Load(obj);
macros.clear();
obs_data_array_t *macroArray = obs_data_get_array(obj, "macros");
@ -1200,7 +1198,7 @@ void SwitcherData::LoadMacros(obs_data_t *obj)
int groupCount = 0;
std::shared_ptr<Macro> group;
std::vector<std::shared_ptr<Macro>> invalidGroups;
for (auto &m : macros) {
for (const auto &m : macros) {
if (groupCount && m->IsGroup()) {
blog(LOG_ERROR,
"nested group detected - will delete \"%s\"",
@ -1235,23 +1233,28 @@ void SwitcherData::LoadMacros(obs_data_t *obj)
}
}
bool SwitcherData::CheckMacros()
std::deque<std::shared_ptr<Macro>> &GetMacros()
{
bool ret = false;
for (auto &m : macros) {
return macros;
}
bool CheckMacros()
{
bool matchFound = false;
for (const auto &m : macros) {
if (m->CeckMatch() || m->ElseActions().size() > 0) {
ret = true;
matchFound = true;
// This has to be performed here for now as actions are
// not performed immediately after checking conditions.
if (m->SwitchesScene()) {
switcher->macroSceneSwitched = true;
SetMacroSwitchedScene(true);
}
}
}
return ret;
return matchFound;
}
bool SwitcherData::RunMacros()
bool RunMacros()
{
// Create copy of macro list as elements might be removed, inserted, or
// reordered while macros are currently being executed.
@ -1269,15 +1272,16 @@ bool SwitcherData::RunMacros()
// the constructor of AdvSceneSwitcher() to complete.
// The constructor of AdvSceneSwitcher() cannot continue however as it
// cannot lock the main switcher mutex.
if (GetLock()) {
GetLock()->unlock();
auto lock = GetLoopLock();
if (lock) {
lock->unlock();
}
for (auto &m : runPhaseMacros) {
if (!m || !m->ShouldRunActions()) {
continue;
}
if (firstInterval && m->SkipExecOnStart()) {
if (IsFirstInterval() && m->SkipExecOnStart()) {
blog(LOG_INFO,
"skip execution of macro \"%s\" at startup",
m->Name().c_str());
@ -1288,15 +1292,15 @@ bool SwitcherData::RunMacros()
blog(LOG_WARNING, "abort macro: %s", m->Name().c_str());
}
}
if (GetLock()) {
GetLock()->lock();
if (lock) {
lock->lock();
}
return true;
}
Macro *GetMacroByName(const char *name)
{
for (auto &m : switcher->macros) {
for (const auto &m : macros) {
if (m->Name() == name) {
return m.get();
}
@ -1312,7 +1316,7 @@ Macro *GetMacroByQString(const QString &name)
std::weak_ptr<Macro> GetWeakMacroByName(const char *name)
{
for (auto &m : switcher->macros) {
for (const auto &m : macros) {
if (m->Name() == name) {
return m;
}
@ -1323,7 +1327,7 @@ std::weak_ptr<Macro> GetWeakMacroByName(const char *name)
void InvalidateMacroTempVarValues()
{
for (auto &m : switcher->macros) {
for (const auto &m : macros) {
m->InvalidateTempVarValues();
}
}

View File

@ -202,6 +202,11 @@ private:
QAction *_dockAction = nullptr;
};
void LoadMacros(obs_data_t *obj);
void SaveMacros(obs_data_t *obj);
std::deque<std::shared_ptr<Macro>> &GetMacros();
bool CheckMacros();
bool RunMacros();
Macro *GetMacroByName(const char *name);
Macro *GetMacroByQString(const QString &name);
std::weak_ptr<Macro> GetWeakMacroByName(const char *name);

View File

@ -1,7 +1,7 @@
#include "macro-condition-video.hpp"
#include <macro-condition-edit.hpp>
#include <switcher-data.hpp>
#include <plugin-state-helpers.hpp>
#include <utility.hpp>
#include <QFileDialog>
@ -222,8 +222,8 @@ void MacroConditionVideo::GetScreenshot(bool blocking)
_areaParameters.area.width,
_areaParameters.area.height);
}
new (&_screenshotData) ScreenshotHelper(
source, screenshotArea, blocking, GetSwitcher()->interval);
new (&_screenshotData) ScreenshotHelper(source, screenshotArea,
blocking, GetIntervalValue());
obs_source_release(source);
_getNextScreenshot = false;
}
@ -1110,9 +1110,9 @@ MacroConditionVideoEdit::MacroConditionVideoEdit(
"AdvSceneSwitcher.condition.video.patternMatchMode.tip"));
populatePatternMatchModeSelection(_patternMatchMode);
_throttleCount->setMinimum(1 * GetSwitcher()->interval);
_throttleCount->setMaximum(10 * GetSwitcher()->interval);
_throttleCount->setSingleStep(GetSwitcher()->interval);
_throttleCount->setMinimum(1 * GetIntervalValue());
_throttleCount->setMaximum(10 * GetIntervalValue());
_throttleCount->setSingleStep(GetIntervalValue());
_brightness->setSizePolicy(QSizePolicy::MinimumExpanding,
QSizePolicy::Preferred);
@ -1507,7 +1507,7 @@ void MacroConditionVideoEdit::ThrottleCountChanged(int value)
}
auto lock = LockContext();
_entryData->_throttleCount = value / GetSwitcher()->interval;
_entryData->_throttleCount = value / GetIntervalValue();
}
void MacroConditionVideoEdit::ShowMatchClicked()
@ -1629,7 +1629,7 @@ void MacroConditionVideoEdit::UpdateEntryData()
_entryData->_patternMatchParameters.matchMode));
_throttleEnable->setChecked(_entryData->_throttleEnabled);
_throttleCount->setValue(_entryData->_throttleCount *
GetSwitcher()->interval);
GetIntervalValue());
UpdatePreviewTooltip();
SetupPreviewDialogParams();
SetWidgetVisibility();

View File

@ -21,7 +21,7 @@
#include "curl-helper.hpp"
#include "priority-helper.hpp"
#include "log-helper.hpp"
#include "plugin-state-helper.hpp"
#include "plugin-state-helpers.hpp"
#include <condition_variable>
#include <vector>
@ -47,6 +47,7 @@ class SwitcherData;
extern SwitcherData *switcher;
SwitcherData *GetSwitcher();
std::mutex *GetSwitcherMutex();
std::unique_lock<std::mutex> *GetSwitcherLoopLock();
bool VerboseLoggingEnabled();
class SwitcherData {
@ -54,7 +55,6 @@ public:
void Thread();
void Start();
void Stop();
std::unique_lock<std::mutex> *GetLock() { return mainLoopLock; }
const char *Translate(const char *);
obs_module_t *GetModule();
@ -72,15 +72,12 @@ public:
bool CheckForMatch(OBSWeakSource &scene, OBSWeakSource &transition,
int &linger, bool &setPreviousSceneAsMatch,
bool &macroMatch);
bool CheckMacros();
bool RunMacros();
void CheckNoMatchSwitch(bool &match, OBSWeakSource &scene,
OBSWeakSource &transition, int &sleep);
/* --- Start of saving / loading section --- */
void SaveSettings(obs_data_t *obj);
void SaveMacros(obs_data_t *obj);
void SaveVariables(obs_data_t *obj);
void SaveGeneralSettings(obs_data_t *obj);
void SaveHotkeys(obs_data_t *obj);
@ -88,7 +85,6 @@ public:
void SaveVersion(obs_data_t *obj, const std::string &currentVersion);
void LoadSettings(obs_data_t *obj);
void LoadMacros(obs_data_t *obj);
void LoadVariables(obs_data_t *obj);
void LoadGeneralSettings(obs_data_t *obj);
void LoadHotkeys(obs_data_t *obj);
@ -110,9 +106,6 @@ public:
std::unique_lock<std::mutex> *mainLoopLock = nullptr;
bool stop = false;
std::condition_variable cv;
std::condition_variable macroWaitCv;
std::atomic_bool abortMacroWait = {false};
std::condition_variable macroTransitionCv;
std::vector<std::function<void(obs_data_t *)>> saveSteps;
std::vector<std::function<void(obs_data_t *)>> loadSteps;
@ -126,7 +119,6 @@ public:
bool obsIsShuttingDown = false;
bool firstInterval = true;
bool firstIntervalAfterStop = true;
int shutdownConditionCount = 0;
bool startupLoadDone = false;
obs_source_t *waitScene = nullptr;
@ -165,10 +157,6 @@ public:
AudioFadeInfo masterAudioFade;
std::unordered_map<std::string, AudioFadeInfo> activeAudioFades;
MacroProperties macroProperties;
std::deque<std::shared_ptr<Macro>> macros;
bool macroSceneSwitched = false;
Curlhelper curl;
std::deque<std::shared_ptr<Item>> variables;

View File

@ -1,7 +1,7 @@
#include "macro-segment-selection.hpp"
#include "advanced-scene-switcher.hpp"
#include "obs-module-helper.hpp"
#include "switcher-data.hpp"
#include "plugin-state-helpers.hpp"
#include "utility.hpp"
namespace advss {
@ -108,7 +108,7 @@ void MacroSegmentSelection::IndexChanged(const IntVariable &value)
void MacroSegmentSelection::MarkSelectedSegment()
{
if (!GetSwitcher() || GetSwitcher()->disableHints) {
if (!HighlightUIElementsEnabled()) {
return;
}