File: macro-helpers.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 (305 lines) | stat: -rw-r--r-- 5,690 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "macro-helpers.hpp"
#include "macro.hpp"
#include "macro-action-macro.hpp"
#include "plugin-state-helpers.hpp"

namespace advss {

static std::atomic_bool abortMacroWait = {false};
static std::atomic_bool macroSceneSwitched = {false};
static std::atomic_int shutdownConditionCount = {0};

static void appendNestedMacros(std::deque<std::shared_ptr<Macro>> &macros,
			       Macro *macro)
{
	if (!macro) {
		return;
	}

	const auto iterate = [&macros](const std::deque<
				       std::shared_ptr<MacroAction>> &actions) {
		for (const auto &action : actions) {
			const auto nestedMacroAction =
				dynamic_cast<MacroActionMacro *>(action.get());
			if (!nestedMacroAction) {
				continue;
			}

			macros.push_back(nestedMacroAction->_nestedMacro);
			appendNestedMacros(
				macros, nestedMacroAction->_nestedMacro.get());
		}
	};

	iterate(macro->Actions());
	iterate(macro->ElseActions());
}

std::deque<std::shared_ptr<Macro>> &GetTopLevelMacros()
{
	static std::deque<std::shared_ptr<Macro>> macros;
	return macros;
}

std::deque<std::shared_ptr<Macro>> &GetTemporaryMacros()
{
	static std::deque<std::shared_ptr<Macro>> tempMacros;
	return tempMacros;
}

std::deque<std::shared_ptr<Macro>> GetAllMacros()
{
	auto macros = GetTopLevelMacros();
	for (const auto &topLevelMacro : macros) {
		appendNestedMacros(macros, topLevelMacro.get());
	}

	const auto &tempMacros = GetTemporaryMacros();
	macros.insert(macros.end(), tempMacros.begin(), tempMacros.end());
	for (const auto &tempMacro : tempMacros) {
		appendNestedMacros(macros, tempMacro.get());
	}

	return macros;
}

Macro *GetMacroByName(const char *name)
{
	for (const auto &m : GetTopLevelMacros()) {
		if (m->Name() == name) {
			return m.get();
		}
	}

	return nullptr;
}

Macro *GetMacroByQString(const QString &name)
{
	return GetMacroByName(name.toUtf8().constData());
}

std::weak_ptr<Macro> GetWeakMacroByName(const char *name)
{
	for (const auto &m : GetTopLevelMacros()) {
		if (m->Name() == name) {
			return m;
		}
	}

	return {};
}

std::optional<std::deque<std::shared_ptr<MacroAction>>>
GetMacroActions(Macro *macro)
{
	if (!macro) {
		return {};
	}
	return macro->Actions();
}

std::optional<std::deque<std::shared_ptr<MacroAction>>>
GetMacroElseActions(Macro *macro)
{
	if (!macro) {
		return {};
	}
	return macro->ElseActions();
}

std::optional<std::deque<std::shared_ptr<MacroCondition>>>
GetMacroConditions(Macro *macro)
{
	if (!macro) {
		return {};
	}
	return macro->Conditions();
}

bool IsGroupMacro(Macro *macro)
{
	return macro && macro->IsGroup();
}

std::vector<std::shared_ptr<Macro>> GetGroupMacroEntries(Macro *macro)
{
	if (!macro || !macro->IsGroup()) {
		return {};
	}

	std::vector<std::shared_ptr<Macro>> entries;
	entries.reserve(macro->GroupSize());

	const auto &macros = GetTopLevelMacros();
	for (auto it = macros.begin(); it < macros.end(); it++) {
		if ((*it)->Name() != macro->Name()) {
			continue;
		}
		for (uint32_t i = 1; i <= macro->GroupSize(); i++) {
			entries.emplace_back(*std::next(it, i));
		}
		break;
	}

	return entries;
}

std::condition_variable &GetMacroWaitCV()
{
	static std::condition_variable cv;
	return cv;
}

std::condition_variable &GetMacroTransitionCV()
{
	static std::condition_variable cv;
	return cv;
}

std::atomic_bool &MacroWaitShouldAbort()
{
	return abortMacroWait;
}

void SetMacroAbortWait(bool value)
{
	abortMacroWait = value;
}

bool ShutdownCheckIsNecessary()
{
	return shutdownConditionCount > 0;
}

std::atomic_int &GetShutdownConditionCount()
{
	return shutdownConditionCount;
}

void SetMacroSwitchedScene(bool value)
{
	static bool setupDone = false;
	if (!setupDone) {
		AddIntervalResetStep([]() { macroSceneSwitched = false; });
		setupDone = true;
	}
	macroSceneSwitched = value;
}

bool MacroSwitchedScene()
{
	return macroSceneSwitched;
}

std::string GetMacroName(const Macro *macro)
{
	return macro ? macro->Name() : "";
}

std::chrono::high_resolution_clock::time_point
LastMacroConditionCheckTime(const Macro *macro)
{
	return macro ? macro->LastConditionCheckTime()
		     : std::chrono::high_resolution_clock::time_point{};
}

bool MacroIsStopped(const Macro *macro)
{
	return macro ? macro->GetStop() : true;
}

bool MacroIsPaused(const Macro *macro)
{
	return macro ? macro->Paused() : true;
}

bool MacroWasPausedSince(
	const Macro *macro,
	const std::chrono::high_resolution_clock::time_point &time)
{
	return macro ? macro->WasPausedSince(time) : false;
}

bool MacroWasCheckedSinceLastStart(const Macro *macro)
{
	if (!macro) {
		return false;
	}
	return macro->LastConditionCheckTime().time_since_epoch().count() != 0;
}

void AddMacroHelperThread(Macro *macro, std::thread &&newThread)
{
	if (!macro) {
		return;
	}
	macro->AddHelperThread(std::move(newThread));
}

bool RunMacroActions(Macro *macro)
{
	return macro && macro->PerformActions(true);
}

bool RunMacroElseActions(Macro *macro)
{
	return macro && macro->PerformActions(false);
}

void ResetMacroConditionTimers(Macro *macro)
{
	if (!macro) {
		return;
	}
	macro->ResetTimers();
}

void ResetMacroRunCount(Macro *macro)
{
	if (!macro) {
		return;
	}
	macro->ResetRunCount();
}

bool IsValidActionIndex(const Macro *m, const int idx)
{
	if (!m || idx < 0) {
		return false;
	}

	if (idx >= (int)m->Actions().size()) {
		return false;
	}

	return true;
}

bool IsValidElseActionIndex(const Macro *m, const int idx)
{
	if (!m || idx < 0) {
		return false;
	}

	if (idx >= (int)m->ElseActions().size()) {
		return false;
	}

	return true;
}

bool IsValidConditionIndex(const Macro *m, const int idx)
{
	if (!m || idx < 0) {
		return false;
	}

	if (idx >= (int)m->Conditions().size()) {
		return false;
	}

	return true;
}

} // namespace advss