File: macro-websocket-trigger.cpp

package info (click to toggle)
obs-advanced-scene-switcher 1.32.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,492 kB
  • sloc: xml: 297,593; cpp: 147,875; python: 387; sh: 280; ansic: 170; makefile: 33
file content (111 lines) | stat: -rw-r--r-- 2,917 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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