auto-scene-switcher.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. #include <obs-frontend-api.h>
  2. #include <obs-module.h>
  3. #include <obs.hpp>
  4. #include <util/util.hpp>
  5. #include <QMainWindow>
  6. #include <QMessageBox>
  7. #include <QAction>
  8. #include "auto-scene-switcher.hpp"
  9. #include "tool-helpers.hpp"
  10. #include <condition_variable>
  11. #include <chrono>
  12. #include <string>
  13. #include <vector>
  14. #include <thread>
  15. #include <regex>
  16. #include <mutex>
  17. using namespace std;
  18. #define DEFAULT_INTERVAL 300
  19. struct SceneSwitch {
  20. OBSWeakSource scene;
  21. string window;
  22. regex re;
  23. inline SceneSwitch(OBSWeakSource scene_, const char *window_)
  24. : scene(scene_), window(window_), re(window_)
  25. {
  26. }
  27. };
  28. static inline bool WeakSourceValid(obs_weak_source_t *ws)
  29. {
  30. obs_source_t *source = obs_weak_source_get_source(ws);
  31. if (source)
  32. obs_source_release(source);
  33. return !!source;
  34. }
  35. struct SwitcherData {
  36. thread th;
  37. condition_variable cv;
  38. mutex m;
  39. bool stop = false;
  40. vector<SceneSwitch> switches;
  41. OBSWeakSource nonMatchingScene;
  42. int interval = DEFAULT_INTERVAL;
  43. bool switchIfNotMatching = false;
  44. bool startAtLaunch = false;
  45. void Thread();
  46. void Start();
  47. void Stop();
  48. void Prune()
  49. {
  50. for (size_t i = 0; i < switches.size(); i++) {
  51. SceneSwitch &s = switches[i];
  52. if (!WeakSourceValid(s.scene))
  53. switches.erase(switches.begin() + i--);
  54. }
  55. if (nonMatchingScene && !WeakSourceValid(nonMatchingScene)) {
  56. switchIfNotMatching = false;
  57. nonMatchingScene = nullptr;
  58. }
  59. }
  60. inline ~SwitcherData()
  61. {
  62. Stop();
  63. }
  64. };
  65. static SwitcherData *switcher = nullptr;
  66. static inline QString MakeSwitchName(const QString &scene,
  67. const QString &window)
  68. {
  69. return QStringLiteral("[") + scene + QStringLiteral("]: ") + window;
  70. }
  71. SceneSwitcher::SceneSwitcher(QWidget *parent)
  72. : QDialog(parent),
  73. ui(new Ui_SceneSwitcher)
  74. {
  75. ui->setupUi(this);
  76. lock_guard<mutex> lock(switcher->m);
  77. switcher->Prune();
  78. BPtr<char*> scenes = obs_frontend_get_scene_names();
  79. char **temp = scenes;
  80. while (*temp) {
  81. const char *name = *temp;
  82. ui->scenes->addItem(name);
  83. ui->noMatchSwitchScene->addItem(name);
  84. temp++;
  85. }
  86. if (switcher->switchIfNotMatching)
  87. ui->noMatchSwitch->setChecked(true);
  88. else
  89. ui->noMatchDontSwitch->setChecked(true);
  90. ui->noMatchSwitchScene->setCurrentText(
  91. GetWeakSourceName(switcher->nonMatchingScene).c_str());
  92. ui->checkInterval->setValue(switcher->interval);
  93. vector<string> windows;
  94. GetWindowList(windows);
  95. for (string &window : windows)
  96. ui->windows->addItem(window.c_str());
  97. for (auto &s : switcher->switches) {
  98. string sceneName = GetWeakSourceName(s.scene);
  99. QString text = MakeSwitchName(sceneName.c_str(),
  100. s.window.c_str());
  101. QListWidgetItem *item = new QListWidgetItem(text,
  102. ui->switches);
  103. item->setData(Qt::UserRole, s.window.c_str());
  104. }
  105. if (switcher->th.joinable())
  106. SetStarted();
  107. else
  108. SetStopped();
  109. loading = false;
  110. }
  111. void SceneSwitcher::closeEvent(QCloseEvent*)
  112. {
  113. obs_frontend_save();
  114. }
  115. int SceneSwitcher::FindByData(const QString &window)
  116. {
  117. int count = ui->switches->count();
  118. int idx = -1;
  119. for (int i = 0; i < count; i++) {
  120. QListWidgetItem *item = ui->switches->item(i);
  121. QString itemWindow =
  122. item->data(Qt::UserRole).toString();
  123. if (itemWindow == window) {
  124. idx = i;
  125. break;
  126. }
  127. }
  128. return idx;
  129. }
  130. void SceneSwitcher::on_switches_currentRowChanged(int idx)
  131. {
  132. if (loading)
  133. return;
  134. if (idx == -1)
  135. return;
  136. QListWidgetItem *item = ui->switches->item(idx);
  137. QString window = item->data(Qt::UserRole).toString();
  138. lock_guard<mutex> lock(switcher->m);
  139. for (auto &s : switcher->switches) {
  140. if (window.compare(s.window.c_str()) == 0) {
  141. string name = GetWeakSourceName(s.scene);
  142. ui->scenes->setCurrentText(name.c_str());
  143. ui->windows->setCurrentText(window);
  144. break;
  145. }
  146. }
  147. }
  148. void SceneSwitcher::on_close_clicked()
  149. {
  150. done(0);
  151. }
  152. void SceneSwitcher::on_add_clicked()
  153. {
  154. QString sceneName = ui->scenes->currentText();
  155. QString windowName = ui->windows->currentText();
  156. if (windowName.isEmpty())
  157. return;
  158. OBSWeakSource source = GetWeakSourceByQString(sceneName);
  159. QVariant v = QVariant::fromValue(windowName);
  160. QString text = MakeSwitchName(sceneName, windowName);
  161. int idx = FindByData(windowName);
  162. if (idx == -1) {
  163. try {
  164. lock_guard<mutex> lock(switcher->m);
  165. switcher->switches.emplace_back(source,
  166. windowName.toUtf8().constData());
  167. QListWidgetItem *item = new QListWidgetItem(text,
  168. ui->switches);
  169. item->setData(Qt::UserRole, v);
  170. } catch (const regex_error &) {
  171. QMessageBox::warning(this,
  172. obs_module_text("InvalidRegex.Title"),
  173. obs_module_text("InvalidRegex.Text"));
  174. }
  175. } else {
  176. QListWidgetItem *item = ui->switches->item(idx);
  177. item->setText(text);
  178. string window = windowName.toUtf8().constData();
  179. {
  180. lock_guard<mutex> lock(switcher->m);
  181. for (auto &s : switcher->switches) {
  182. if (s.window == window) {
  183. s.scene = source;
  184. break;
  185. }
  186. }
  187. }
  188. ui->switches->sortItems();
  189. }
  190. }
  191. void SceneSwitcher::on_remove_clicked()
  192. {
  193. QListWidgetItem *item = ui->switches->currentItem();
  194. if (!item)
  195. return;
  196. string window =
  197. item->data(Qt::UserRole).toString().toUtf8().constData();
  198. {
  199. lock_guard<mutex> lock(switcher->m);
  200. auto &switches = switcher->switches;
  201. for (auto it = switches.begin(); it != switches.end(); ++it) {
  202. auto &s = *it;
  203. if (s.window == window) {
  204. switches.erase(it);
  205. break;
  206. }
  207. }
  208. }
  209. delete item;
  210. }
  211. void SceneSwitcher::on_startAtLaunch_toggled(bool value)
  212. {
  213. if (loading)
  214. return;
  215. lock_guard<mutex> lock(switcher->m);
  216. switcher->startAtLaunch = value;
  217. }
  218. void SceneSwitcher::UpdateNonMatchingScene(const QString &name)
  219. {
  220. obs_source_t *scene = obs_get_source_by_name(
  221. name.toUtf8().constData());
  222. obs_weak_source_t *ws = obs_source_get_weak_source(scene);
  223. switcher->nonMatchingScene = ws;
  224. obs_weak_source_release(ws);
  225. obs_source_release(scene);
  226. }
  227. void SceneSwitcher::on_noMatchDontSwitch_clicked()
  228. {
  229. if (loading)
  230. return;
  231. lock_guard<mutex> lock(switcher->m);
  232. switcher->switchIfNotMatching = false;
  233. }
  234. void SceneSwitcher::on_noMatchSwitch_clicked()
  235. {
  236. if (loading)
  237. return;
  238. lock_guard<mutex> lock(switcher->m);
  239. switcher->switchIfNotMatching = true;
  240. UpdateNonMatchingScene(ui->noMatchSwitchScene->currentText());
  241. }
  242. void SceneSwitcher::on_noMatchSwitchScene_currentTextChanged(
  243. const QString &text)
  244. {
  245. if (loading)
  246. return;
  247. lock_guard<mutex> lock(switcher->m);
  248. UpdateNonMatchingScene(text);
  249. }
  250. void SceneSwitcher::on_checkInterval_valueChanged(int value)
  251. {
  252. if (loading)
  253. return;
  254. lock_guard<mutex> lock(switcher->m);
  255. switcher->interval = value;
  256. }
  257. void SceneSwitcher::SetStarted()
  258. {
  259. ui->toggleStartButton->setText(obs_module_text("Stop"));
  260. ui->pluginRunningText->setText(obs_module_text("Active"));
  261. }
  262. void SceneSwitcher::SetStopped()
  263. {
  264. ui->toggleStartButton->setText(obs_module_text("Start"));
  265. ui->pluginRunningText->setText(obs_module_text("Inactive"));
  266. }
  267. void SceneSwitcher::on_toggleStartButton_clicked()
  268. {
  269. if (switcher->th.joinable()) {
  270. switcher->Stop();
  271. SetStopped();
  272. } else {
  273. switcher->Start();
  274. SetStarted();
  275. }
  276. }
  277. static void SaveSceneSwitcher(obs_data_t *save_data, bool saving, void *)
  278. {
  279. if (saving) {
  280. lock_guard<mutex> lock(switcher->m);
  281. obs_data_t *obj = obs_data_create();
  282. obs_data_array_t *array = obs_data_array_create();
  283. switcher->Prune();
  284. for (SceneSwitch &s : switcher->switches) {
  285. obs_data_t *array_obj = obs_data_create();
  286. obs_source_t *source = obs_weak_source_get_source(
  287. s.scene);
  288. if (source) {
  289. const char *n = obs_source_get_name(source);
  290. obs_data_set_string(array_obj, "scene", n);
  291. obs_data_set_string(array_obj, "window_title",
  292. s.window.c_str());
  293. obs_data_array_push_back(array, array_obj);
  294. obs_source_release(source);
  295. }
  296. obs_data_release(array_obj);
  297. }
  298. string nonMatchingSceneName =
  299. GetWeakSourceName(switcher->nonMatchingScene);
  300. obs_data_set_int(obj, "interval", switcher->interval);
  301. obs_data_set_string(obj, "non_matching_scene",
  302. nonMatchingSceneName.c_str());
  303. obs_data_set_bool(obj, "switch_if_not_matching",
  304. switcher->switchIfNotMatching);
  305. obs_data_set_bool(obj, "active", switcher->th.joinable());
  306. obs_data_set_array(obj, "switches", array);
  307. obs_data_set_obj(save_data, "auto-scene-switcher", obj);
  308. obs_data_array_release(array);
  309. obs_data_release(obj);
  310. } else {
  311. switcher->m.lock();
  312. obs_data_t *obj = obs_data_get_obj(save_data,
  313. "auto-scene-switcher");
  314. obs_data_array_t *array = obs_data_get_array(obj, "switches");
  315. size_t count = obs_data_array_count(array);
  316. if (!obj)
  317. obj = obs_data_create();
  318. obs_data_set_default_int(obj, "interval", DEFAULT_INTERVAL);
  319. switcher->interval = obs_data_get_int(obj, "interval");
  320. switcher->switchIfNotMatching =
  321. obs_data_get_bool(obj, "switch_if_not_matching");
  322. string nonMatchingScene =
  323. obs_data_get_string(obj, "non_matching_scene");
  324. bool active = obs_data_get_bool(obj, "active");
  325. switcher->nonMatchingScene =
  326. GetWeakSourceByName(nonMatchingScene.c_str());
  327. switcher->switches.clear();
  328. for (size_t i = 0; i < count; i++) {
  329. obs_data_t *array_obj = obs_data_array_item(array, i);
  330. const char *scene =
  331. obs_data_get_string(array_obj, "scene");
  332. const char *window =
  333. obs_data_get_string(array_obj, "window_title");
  334. switcher->switches.emplace_back(
  335. GetWeakSourceByName(scene),
  336. window);
  337. obs_data_release(array_obj);
  338. }
  339. obs_data_array_release(array);
  340. obs_data_release(obj);
  341. switcher->m.unlock();
  342. if (active)
  343. switcher->Start();
  344. else
  345. switcher->Stop();
  346. }
  347. }
  348. void SwitcherData::Thread()
  349. {
  350. chrono::duration<long long, milli> duration =
  351. chrono::milliseconds(interval);
  352. string lastTitle;
  353. string title;
  354. for (;;) {
  355. unique_lock<mutex> lock(m);
  356. OBSWeakSource scene;
  357. bool match = false;
  358. cv.wait_for(lock, duration);
  359. if (switcher->stop) {
  360. switcher->stop = false;
  361. break;
  362. }
  363. duration = chrono::milliseconds(interval);
  364. GetCurrentWindowTitle(title);
  365. if (lastTitle != title) {
  366. switcher->Prune();
  367. for (SceneSwitch &s : switches) {
  368. if (s.window == title) {
  369. match = true;
  370. scene = s.scene;
  371. break;
  372. }
  373. }
  374. /* try regex */
  375. if (!match) {
  376. for (SceneSwitch &s : switches) {
  377. try {
  378. bool matches = regex_match(
  379. title, s.re);
  380. if (matches) {
  381. match = true;
  382. scene = s.scene;
  383. break;
  384. }
  385. } catch (const regex_error &) {}
  386. }
  387. }
  388. if (!match && switchIfNotMatching &&
  389. nonMatchingScene) {
  390. match = true;
  391. scene = nonMatchingScene;
  392. }
  393. if (match) {
  394. obs_source_t *source =
  395. obs_weak_source_get_source(scene);
  396. obs_source_t *currentSource =
  397. obs_frontend_get_current_scene();
  398. if (source && source != currentSource)
  399. obs_frontend_set_current_scene(source);
  400. obs_source_release(currentSource);
  401. obs_source_release(source);
  402. }
  403. }
  404. lastTitle = title;
  405. }
  406. }
  407. void SwitcherData::Start()
  408. {
  409. if (!switcher->th.joinable())
  410. switcher->th = thread([] () {switcher->Thread();});
  411. }
  412. void SwitcherData::Stop()
  413. {
  414. if (th.joinable()) {
  415. {
  416. lock_guard<mutex> lock(m);
  417. stop = true;
  418. }
  419. cv.notify_one();
  420. th.join();
  421. }
  422. }
  423. extern "C" void FreeSceneSwitcher()
  424. {
  425. delete switcher;
  426. switcher = nullptr;
  427. }
  428. static void OBSEvent(enum obs_frontend_event event, void *)
  429. {
  430. if (event == OBS_FRONTEND_EVENT_EXIT)
  431. FreeSceneSwitcher();
  432. }
  433. extern "C" void InitSceneSwitcher()
  434. {
  435. QAction *action = (QAction*)obs_frontend_add_tools_menu_qaction(
  436. obs_module_text("SceneSwitcher"));
  437. switcher = new SwitcherData;
  438. auto cb = [] ()
  439. {
  440. obs_frontend_push_ui_translation(obs_module_get_string);
  441. QMainWindow *window =
  442. (QMainWindow*)obs_frontend_get_main_window();
  443. SceneSwitcher ss(window);
  444. ss.exec();
  445. obs_frontend_pop_ui_translation();
  446. };
  447. obs_frontend_add_save_callback(SaveSceneSwitcher, nullptr);
  448. obs_frontend_add_event_callback(OBSEvent, nullptr);
  449. action->connect(action, &QAction::triggered, cb);
  450. }