Skip to content

Commit

Permalink
Add option to wait for spawned process to exit
Browse files Browse the repository at this point in the history
  • Loading branch information
WarmUpTill committed Dec 25, 2023
1 parent abfd679 commit 4288503
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
1 change: 1 addition & 0 deletions data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ AdvSceneSwitcher.action.streaming.type.username="Set username"
AdvSceneSwitcher.action.streaming.type.password="Set password"
AdvSceneSwitcher.action.streaming.entry="{{actions}}{{keyFrameInterval}}{{stringValue}}{{showPassword}}"
AdvSceneSwitcher.action.run="Run"
AdvSceneSwitcher.action.run.entry.wait="{{wait}}Wait for process exit or at most {{timeout}}"
AdvSceneSwitcher.action.sceneVisibility="Scene item visibility"
AdvSceneSwitcher.action.sceneVisibility.type.show="Show"
AdvSceneSwitcher.action.sceneVisibility.type.hide="Hide"
Expand Down
67 changes: 61 additions & 6 deletions src/macro-core/macro-action-run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ bool MacroActionRun::_registered = MacroActionFactory::Register(

bool MacroActionRun::PerformAction()
{
bool procStarted = QProcess::startDetached(
QString::fromStdString(_procConfig.Path()), _procConfig.Args(),
QString::fromStdString(_procConfig.WorkingDir()));
if (_wait) {
_procConfig.StartProcessAndWait(_timeout.Milliseconds());
return true;
}

bool procStarted = _procConfig.StartProcessDetached();

// Fall back to using default application to open given file
if (!procStarted && _procConfig.Args().empty()) {
vblog(LOG_INFO, "run \"%s\" using QDesktopServices",
_procConfig.Path().c_str());
Expand All @@ -35,13 +40,22 @@ bool MacroActionRun::Save(obs_data_t *obj) const
{
MacroAction::Save(obj);
_procConfig.Save(obj);
_timeout.Save(obj);
obs_data_set_bool(obj, "wait", _wait);
obs_data_set_int(obj, "version", 1);
return true;
}

bool MacroActionRun::Load(obs_data_t *obj)
{
MacroAction::Load(obj);
_procConfig.Load(obj);
// TODO: Remove this fallback in a future version
if (!obs_data_has_user_value(obj, "version")) {
return true;
}
_timeout.Load(obj);
_wait = obs_data_get_bool(obj, "wait");
return true;
}

Expand All @@ -52,14 +66,30 @@ std::string MacroActionRun::GetShortDesc() const

MacroActionRunEdit::MacroActionRunEdit(
QWidget *parent, std::shared_ptr<MacroActionRun> entryData)
: QWidget(parent), _procConfig(new ProcessConfigEdit(this))
: QWidget(parent),
_procConfig(new ProcessConfigEdit(this)),
_waitLayout(new QHBoxLayout()),
_wait(new QCheckBox()),
_timeout(new DurationSelection(this, true, 0.1))
{
QWidget::connect(_procConfig,
SIGNAL(ConfigChanged(const ProcessConfig &)), this,
SLOT(ProcessConfigChanged(const ProcessConfig &)));

auto *layout = new QVBoxLayout;
QWidget::connect(_procConfig, SIGNAL(AdvancedSettingsEnabled()), this,
SLOT(ProcessConfigAdvancedSettingsShown()));
QWidget::connect(_wait, SIGNAL(stateChanged(int)), this,
SLOT(WaitChanged(int)));
QWidget::connect(_timeout, SIGNAL(DurationChanged(const Duration &)),
this, SLOT(TimeoutChanged(const Duration &)));

PlaceWidgets(obs_module_text("AdvSceneSwitcher.action.run.entry.wait"),
_waitLayout,
{{"{{wait}}", _wait}, {"{{timeout}}", _timeout}});
SetLayoutVisible(_waitLayout, false);

auto layout = new QVBoxLayout;
layout->addWidget(_procConfig);
layout->addLayout(_waitLayout);
setLayout(layout);

_entryData = entryData;
Expand All @@ -73,6 +103,31 @@ void MacroActionRunEdit::UpdateEntryData()
return;
}
_procConfig->SetProcessConfig(_entryData->_procConfig);
_wait->setChecked(_entryData->_wait);
_timeout->SetDuration(_entryData->_timeout);
}

void MacroActionRunEdit::ProcessConfigAdvancedSettingsShown()
{
SetLayoutVisible(_waitLayout, true);
}

void MacroActionRunEdit::WaitChanged(int value)
{
if (_loading || !_entryData) {
return;
}
auto lock = LockContext();
_entryData->_wait = value;
}

void MacroActionRunEdit::TimeoutChanged(const Duration &timeout)
{
if (_loading || !_entryData) {
return;
}
auto lock = LockContext();
_entryData->_timeout = timeout;
}

void MacroActionRunEdit::ProcessConfigChanged(const ProcessConfig &conf)
Expand Down
16 changes: 13 additions & 3 deletions src/macro-core/macro-action-run.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once
#include "macro-action-edit.hpp"
#include "process-config.hpp"
#include "duration-control.hpp"

#include <QCheckBox>

namespace advss {

Expand All @@ -19,6 +22,8 @@ class MacroActionRun : public MacroAction {
}

ProcessConfig _procConfig;
bool _wait = false;
Duration _timeout = 1;

private:
static bool _registered;
Expand All @@ -42,14 +47,19 @@ class MacroActionRunEdit : public QWidget {

private slots:
void ProcessConfigChanged(const ProcessConfig &);
void ProcessConfigAdvancedSettingsShown();
void WaitChanged(int);
void TimeoutChanged(const Duration &);
signals:
void HeaderInfoChanged(const QString &);

protected:
std::shared_ptr<MacroActionRun> _entryData;

private:
ProcessConfigEdit *_procConfig;
QHBoxLayout *_waitLayout;
QCheckBox *_wait;
DurationSelection *_timeout;

std::shared_ptr<MacroActionRun> _entryData;
bool _loading = true;
};

Expand Down

0 comments on commit 4288503

Please sign in to comment.