mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-03-21 17:34:57 -05:00
Add websocket callbacks to run macros and set variables
This commit is contained in:
parent
84e5faa21a
commit
a8dbdea6de
|
|
@ -166,6 +166,7 @@ target_sources(
|
|||
lib/macro/macro-tab.cpp
|
||||
lib/macro/macro-tree.cpp
|
||||
lib/macro/macro-tree.hpp
|
||||
lib/macro/macro-websocket-trigger.cpp
|
||||
lib/macro/macro.cpp
|
||||
lib/macro/macro.hpp)
|
||||
|
||||
|
|
|
|||
|
|
@ -242,6 +242,11 @@ bool RunMacroActions(Macro *macro)
|
|||
return macro && macro->PerformActions(true);
|
||||
}
|
||||
|
||||
bool RunMacroElseActions(Macro *macro)
|
||||
{
|
||||
return macro && macro->PerformActions(false);
|
||||
}
|
||||
|
||||
void ResetMacroConditionTimers(Macro *macro)
|
||||
{
|
||||
if (!macro) {
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@
|
|||
#include <condition_variable>
|
||||
#include <deque>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
|
||||
struct obs_data;
|
||||
|
|
@ -69,6 +68,7 @@ EXPORT void AddMacroHelperThread(Macro *, std::thread &&);
|
|||
EXPORT bool CheckMacros();
|
||||
|
||||
EXPORT bool RunMacroActions(Macro *);
|
||||
bool RunMacroElseActions(Macro *);
|
||||
EXPORT bool RunMacros();
|
||||
void StopAllMacros();
|
||||
|
||||
|
|
|
|||
111
lib/macro/macro-websocket-trigger.cpp
Normal file
111
lib/macro/macro-websocket-trigger.cpp
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
#include "websocket-api.hpp"
|
||||
#include "log-helper.hpp"
|
||||
#include "plugin-state-helpers.hpp"
|
||||
#include "variable.hpp"
|
||||
#include "macro-helpers.hpp"
|
||||
|
||||
#include <obs.hpp>
|
||||
|
||||
namespace advss {
|
||||
|
||||
static bool setup();
|
||||
static bool setupDone = setup();
|
||||
static void receiveRunMacroMessage(obs_data_t *data, obs_data_t *);
|
||||
static void receiveSetVariablesMessage(obs_data_t *data, obs_data_t *);
|
||||
|
||||
static const char *runMacroMessage = "AdvancedSceneSwitcherRunMacro";
|
||||
static const char *setVariablesMessage = "AdvancedSceneSwitcherSetVariables";
|
||||
|
||||
static bool setup()
|
||||
{
|
||||
AddPluginInitStep([]() {
|
||||
RegisterWebsocketRequest(runMacroMessage,
|
||||
receiveRunMacroMessage);
|
||||
RegisterWebsocketRequest(setVariablesMessage,
|
||||
receiveSetVariablesMessage);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
static void setVariables(obs_data_t *data)
|
||||
{
|
||||
OBSDataArrayAutoRelease variables =
|
||||
obs_data_get_array(data, "variables");
|
||||
size_t count = obs_data_array_count(variables);
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
OBSDataAutoRelease item = obs_data_array_item(variables, i);
|
||||
if (!obs_data_has_user_value(item, "name")) {
|
||||
blog(LOG_INFO,
|
||||
"ignoring invalid variable entry in \"%s\" websocket message (missing \"name\")",
|
||||
runMacroMessage);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!obs_data_has_user_value(item, "value")) {
|
||||
blog(LOG_INFO,
|
||||
"ignoring invalid variable entry in \"%s\" websocket message (missing \"value\")",
|
||||
runMacroMessage);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto varName = obs_data_get_string(item, "name");
|
||||
auto value = obs_data_get_string(item, "value");
|
||||
|
||||
auto weakVar = GetWeakVariableByName(varName);
|
||||
auto var = weakVar.lock();
|
||||
if (!var) {
|
||||
blog(LOG_INFO,
|
||||
"ignoring invalid variable name \"%s\" in \"%s\" websocket message",
|
||||
varName, runMacroMessage);
|
||||
continue;
|
||||
}
|
||||
|
||||
var->SetValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
static void receiveRunMacroMessage(obs_data_t *data, obs_data_t *)
|
||||
{
|
||||
if (!obs_data_has_user_value(data, "name")) {
|
||||
blog(LOG_INFO,
|
||||
"ignoring invalid \"%s\" websocket message (missing \"name\")",
|
||||
runMacroMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
auto name = obs_data_get_string(data, "name");
|
||||
const auto weakMacro =
|
||||
GetWeakMacroByName(obs_data_get_string(data, "name"));
|
||||
const auto macro = weakMacro.lock();
|
||||
if (!macro) {
|
||||
blog(LOG_INFO,
|
||||
"ignoring invalid \"%s\" websocket message (macro \"%s\") not found",
|
||||
runMacroMessage, name);
|
||||
return;
|
||||
}
|
||||
|
||||
if (obs_data_has_user_value(data, "variables")) {
|
||||
setVariables(data);
|
||||
}
|
||||
|
||||
const bool runElse = obs_data_get_bool(data, "runElseActions");
|
||||
if (runElse) {
|
||||
RunMacroElseActions(macro.get());
|
||||
} else {
|
||||
RunMacroActions(macro.get());
|
||||
}
|
||||
}
|
||||
|
||||
static void receiveSetVariablesMessage(obs_data_t *data, obs_data_t *)
|
||||
{
|
||||
if (!obs_data_has_user_value(data, "variables")) {
|
||||
blog(LOG_INFO,
|
||||
"ignoring invalid \"%s\" websocket message (missing \"variables\")",
|
||||
setVariablesMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
setVariables(data);
|
||||
}
|
||||
|
||||
} // namespace advss
|
||||
Loading…
Reference in New Issue
Block a user