window-basic-source-select.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /******************************************************************************
  2. Copyright (C) 2014 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <QMessageBox>
  15. #include "window-basic-main.hpp"
  16. #include "window-basic-source-select.hpp"
  17. #include "qt-wrappers.hpp"
  18. #include "obs-app.hpp"
  19. struct AddSourceData {
  20. obs_source_t *source;
  21. bool visible;
  22. obs_transform_info *transform = nullptr;
  23. obs_sceneitem_crop *crop = nullptr;
  24. obs_blending_type *blend = nullptr;
  25. };
  26. bool OBSBasicSourceSelect::EnumSources(void *data, obs_source_t *source)
  27. {
  28. if (obs_source_is_hidden(source))
  29. return false;
  30. OBSBasicSourceSelect *window =
  31. static_cast<OBSBasicSourceSelect *>(data);
  32. const char *name = obs_source_get_name(source);
  33. const char *id = obs_source_get_unversioned_id(source);
  34. if (strcmp(id, window->id) == 0)
  35. window->ui->sourceList->addItem(QT_UTF8(name));
  36. return true;
  37. }
  38. bool OBSBasicSourceSelect::EnumGroups(void *data, obs_source_t *source)
  39. {
  40. OBSBasicSourceSelect *window =
  41. static_cast<OBSBasicSourceSelect *>(data);
  42. const char *name = obs_source_get_name(source);
  43. const char *id = obs_source_get_unversioned_id(source);
  44. if (strcmp(id, window->id) == 0) {
  45. OBSBasic *main =
  46. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  47. OBSScene scene = main->GetCurrentScene();
  48. obs_sceneitem_t *existing = obs_scene_get_group(scene, name);
  49. if (!existing)
  50. window->ui->sourceList->addItem(QT_UTF8(name));
  51. }
  52. return true;
  53. }
  54. void OBSBasicSourceSelect::OBSSourceAdded(void *data, calldata_t *calldata)
  55. {
  56. OBSBasicSourceSelect *window =
  57. static_cast<OBSBasicSourceSelect *>(data);
  58. obs_source_t *source = (obs_source_t *)calldata_ptr(calldata, "source");
  59. QMetaObject::invokeMethod(window, "SourceAdded",
  60. Q_ARG(OBSSource, source));
  61. }
  62. void OBSBasicSourceSelect::OBSSourceRemoved(void *data, calldata_t *calldata)
  63. {
  64. OBSBasicSourceSelect *window =
  65. static_cast<OBSBasicSourceSelect *>(data);
  66. obs_source_t *source = (obs_source_t *)calldata_ptr(calldata, "source");
  67. QMetaObject::invokeMethod(window, "SourceRemoved",
  68. Q_ARG(OBSSource, source));
  69. }
  70. void OBSBasicSourceSelect::SourceAdded(OBSSource source)
  71. {
  72. const char *name = obs_source_get_name(source);
  73. const char *sourceId = obs_source_get_unversioned_id(source);
  74. if (strcmp(sourceId, id) != 0)
  75. return;
  76. ui->sourceList->addItem(name);
  77. }
  78. void OBSBasicSourceSelect::SourceRemoved(OBSSource source)
  79. {
  80. const char *name = obs_source_get_name(source);
  81. const char *sourceId = obs_source_get_unversioned_id(source);
  82. if (strcmp(sourceId, id) != 0)
  83. return;
  84. QList<QListWidgetItem *> items =
  85. ui->sourceList->findItems(name, Qt::MatchFixedString);
  86. if (!items.count())
  87. return;
  88. delete items[0];
  89. }
  90. static void AddSource(void *_data, obs_scene_t *scene)
  91. {
  92. AddSourceData *data = (AddSourceData *)_data;
  93. obs_sceneitem_t *sceneitem;
  94. sceneitem = obs_scene_add(scene, data->source);
  95. if (data->transform != nullptr)
  96. obs_sceneitem_set_info(sceneitem, data->transform);
  97. if (data->crop != nullptr)
  98. obs_sceneitem_set_crop(sceneitem, data->crop);
  99. if (data->blend != nullptr)
  100. obs_sceneitem_set_blending_mode(sceneitem, *data->blend);
  101. obs_sceneitem_set_visible(sceneitem, data->visible);
  102. }
  103. static char *get_new_source_name(const char *name)
  104. {
  105. struct dstr new_name = {0};
  106. int inc = 0;
  107. dstr_copy(&new_name, name);
  108. for (;;) {
  109. OBSSourceAutoRelease existing_source =
  110. obs_get_source_by_name(new_name.array);
  111. if (!existing_source)
  112. break;
  113. dstr_printf(&new_name, "%s %d", name, ++inc + 1);
  114. }
  115. return new_name.array;
  116. }
  117. static void AddExisting(OBSSource source, bool visible, bool duplicate,
  118. obs_transform_info *transform, obs_sceneitem_crop *crop,
  119. obs_blending_type *blend)
  120. {
  121. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  122. OBSScene scene = main->GetCurrentScene();
  123. if (!scene)
  124. return;
  125. if (duplicate) {
  126. OBSSource from = source;
  127. char *new_name =
  128. get_new_source_name(obs_source_get_name(source));
  129. source = obs_source_duplicate(from, new_name, false);
  130. obs_source_release(source);
  131. bfree(new_name);
  132. if (!source)
  133. return;
  134. }
  135. AddSourceData data;
  136. data.source = source;
  137. data.visible = visible;
  138. data.transform = transform;
  139. data.crop = crop;
  140. data.blend = blend;
  141. obs_enter_graphics();
  142. obs_scene_atomic_update(scene, AddSource, &data);
  143. obs_leave_graphics();
  144. }
  145. static void AddExisting(const char *name, bool visible, bool duplicate,
  146. obs_transform_info *transform, obs_sceneitem_crop *crop,
  147. obs_blending_type *blend)
  148. {
  149. OBSSourceAutoRelease source = obs_get_source_by_name(name);
  150. if (source) {
  151. AddExisting(source.Get(), visible, duplicate, transform, crop,
  152. blend);
  153. }
  154. }
  155. bool AddNew(QWidget *parent, const char *id, const char *name,
  156. const bool visible, OBSSource &newSource)
  157. {
  158. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  159. OBSScene scene = main->GetCurrentScene();
  160. bool success = false;
  161. if (!scene)
  162. return false;
  163. OBSSourceAutoRelease source = obs_get_source_by_name(name);
  164. if (source && parent) {
  165. OBSMessageBox::information(parent, QTStr("NameExists.Title"),
  166. QTStr("NameExists.Text"));
  167. } else {
  168. const char *v_id = obs_get_latest_input_type_id(id);
  169. source = obs_source_create(v_id, name, NULL, nullptr);
  170. if (source) {
  171. AddSourceData data;
  172. data.source = source;
  173. data.visible = visible;
  174. obs_enter_graphics();
  175. obs_scene_atomic_update(scene, AddSource, &data);
  176. obs_leave_graphics();
  177. newSource = source;
  178. /* set monitoring if source monitors by default */
  179. uint32_t flags = obs_source_get_output_flags(source);
  180. if ((flags & OBS_SOURCE_MONITOR_BY_DEFAULT) != 0) {
  181. obs_source_set_monitoring_type(
  182. source,
  183. OBS_MONITORING_TYPE_MONITOR_ONLY);
  184. }
  185. success = true;
  186. }
  187. }
  188. return success;
  189. }
  190. void OBSBasicSourceSelect::on_buttonBox_accepted()
  191. {
  192. bool useExisting = ui->selectExisting->isChecked();
  193. bool visible = ui->sourceVisible->isChecked();
  194. if (useExisting) {
  195. QListWidgetItem *item = ui->sourceList->currentItem();
  196. if (!item)
  197. return;
  198. QString source_name = item->text();
  199. AddExisting(QT_TO_UTF8(source_name), visible, false, nullptr,
  200. nullptr, nullptr);
  201. OBSBasic *main =
  202. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  203. const char *scene_name =
  204. obs_source_get_name(main->GetCurrentSceneSource());
  205. auto undo = [scene_name, main](const std::string &data) {
  206. UNUSED_PARAMETER(data);
  207. obs_source_t *scene_source =
  208. obs_get_source_by_name(scene_name);
  209. main->SetCurrentScene(scene_source, true);
  210. obs_source_release(scene_source);
  211. obs_scene_t *scene = obs_get_scene_by_name(scene_name);
  212. OBSSceneItem item;
  213. auto cb = [](obs_scene_t *scene,
  214. obs_sceneitem_t *sceneitem, void *data) {
  215. UNUSED_PARAMETER(scene);
  216. OBSSceneItem &last =
  217. *reinterpret_cast<OBSSceneItem *>(data);
  218. last = sceneitem;
  219. return true;
  220. };
  221. obs_scene_enum_items(scene, cb, &item);
  222. obs_sceneitem_remove(item);
  223. obs_scene_release(scene);
  224. };
  225. auto redo = [scene_name, main, source_name,
  226. visible](const std::string &data) {
  227. UNUSED_PARAMETER(data);
  228. obs_source_t *scene_source =
  229. obs_get_source_by_name(scene_name);
  230. main->SetCurrentScene(scene_source, true);
  231. obs_source_release(scene_source);
  232. AddExisting(QT_TO_UTF8(source_name), visible, false,
  233. nullptr, nullptr, nullptr);
  234. };
  235. undo_s.add_action(QTStr("Undo.Add").arg(source_name), undo,
  236. redo, "", "");
  237. } else {
  238. if (ui->sourceName->text().isEmpty()) {
  239. OBSMessageBox::warning(this,
  240. QTStr("NoNameEntered.Title"),
  241. QTStr("NoNameEntered.Text"));
  242. return;
  243. }
  244. if (!AddNew(this, id, QT_TO_UTF8(ui->sourceName->text()),
  245. visible, newSource))
  246. return;
  247. OBSBasic *main =
  248. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  249. std::string scene_name =
  250. obs_source_get_name(main->GetCurrentSceneSource());
  251. auto undo = [scene_name, main](const std::string &data) {
  252. OBSSourceAutoRelease source =
  253. obs_get_source_by_name(data.c_str());
  254. obs_source_remove(source);
  255. OBSSourceAutoRelease scene_source =
  256. obs_get_source_by_name(scene_name.c_str());
  257. main->SetCurrentScene(scene_source.Get(), true);
  258. };
  259. OBSDataAutoRelease wrapper = obs_data_create();
  260. obs_data_set_string(wrapper, "id", id);
  261. OBSSceneItemAutoRelease item = obs_scene_sceneitem_from_source(
  262. main->GetCurrentScene(), newSource);
  263. obs_data_set_int(wrapper, "item_id",
  264. obs_sceneitem_get_id(item));
  265. obs_data_set_string(
  266. wrapper, "name",
  267. ui->sourceName->text().toUtf8().constData());
  268. obs_data_set_bool(wrapper, "visible", visible);
  269. auto redo = [scene_name, main](const std::string &data) {
  270. OBSSourceAutoRelease scene_source =
  271. obs_get_source_by_name(scene_name.c_str());
  272. main->SetCurrentScene(scene_source.Get(), true);
  273. OBSDataAutoRelease dat =
  274. obs_data_create_from_json(data.c_str());
  275. OBSSource source;
  276. AddNew(NULL, obs_data_get_string(dat, "id"),
  277. obs_data_get_string(dat, "name"),
  278. obs_data_get_bool(dat, "visible"), source);
  279. OBSSceneItemAutoRelease item =
  280. obs_scene_sceneitem_from_source(
  281. main->GetCurrentScene(), source);
  282. obs_sceneitem_set_id(item, (int64_t)obs_data_get_int(
  283. dat, "item_id"));
  284. };
  285. undo_s.add_action(QTStr("Undo.Add").arg(ui->sourceName->text()),
  286. undo, redo,
  287. std::string(obs_source_get_name(newSource)),
  288. std::string(obs_data_get_json(wrapper)));
  289. }
  290. done(DialogCode::Accepted);
  291. }
  292. void OBSBasicSourceSelect::on_buttonBox_rejected()
  293. {
  294. done(DialogCode::Rejected);
  295. }
  296. static inline const char *GetSourceDisplayName(const char *id)
  297. {
  298. if (strcmp(id, "scene") == 0)
  299. return Str("Basic.Scene");
  300. const char *v_id = obs_get_latest_input_type_id(id);
  301. return obs_source_get_display_name(v_id);
  302. }
  303. Q_DECLARE_METATYPE(OBSScene);
  304. template<typename T> static inline T GetOBSRef(QListWidgetItem *item)
  305. {
  306. return item->data(static_cast<int>(QtDataRole::OBSRef)).value<T>();
  307. }
  308. OBSBasicSourceSelect::OBSBasicSourceSelect(OBSBasic *parent, const char *id_,
  309. undo_stack &undo_s)
  310. : QDialog(parent),
  311. ui(new Ui::OBSBasicSourceSelect),
  312. id(id_),
  313. undo_s(undo_s)
  314. {
  315. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  316. ui->setupUi(this);
  317. ui->sourceList->setAttribute(Qt::WA_MacShowFocusRect, false);
  318. QString placeHolderText{QT_UTF8(GetSourceDisplayName(id))};
  319. QString text{placeHolderText};
  320. int i = 2;
  321. OBSSourceAutoRelease source = nullptr;
  322. while ((source = obs_get_source_by_name(QT_TO_UTF8(text)))) {
  323. text = QString("%1 %2").arg(placeHolderText).arg(i++);
  324. }
  325. ui->sourceName->setText(text);
  326. ui->sourceName->setFocus(); //Fixes deselect of text.
  327. ui->sourceName->selectAll();
  328. installEventFilter(CreateShortcutFilter());
  329. if (strcmp(id_, "scene") == 0) {
  330. OBSBasic *main =
  331. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  332. OBSSource curSceneSource = main->GetCurrentSceneSource();
  333. ui->selectExisting->setChecked(true);
  334. ui->createNew->setChecked(false);
  335. ui->createNew->setEnabled(false);
  336. ui->sourceName->setEnabled(false);
  337. int count = main->ui->scenes->count();
  338. for (int i = 0; i < count; i++) {
  339. QListWidgetItem *item = main->ui->scenes->item(i);
  340. OBSScene scene = GetOBSRef<OBSScene>(item);
  341. OBSSource sceneSource = obs_scene_get_source(scene);
  342. if (curSceneSource == sceneSource)
  343. continue;
  344. const char *name = obs_source_get_name(sceneSource);
  345. ui->sourceList->addItem(QT_UTF8(name));
  346. }
  347. } else if (strcmp(id_, "group") == 0) {
  348. obs_enum_sources(EnumGroups, this);
  349. } else {
  350. obs_enum_sources(EnumSources, this);
  351. }
  352. }
  353. void OBSBasicSourceSelect::SourcePaste(SourceCopyInfo &info, bool dup)
  354. {
  355. OBSSource source = OBSGetStrongRef(info.weak_source);
  356. if (!source)
  357. return;
  358. AddExisting(source, info.visible, dup, &info.transform, &info.crop,
  359. &info.blend);
  360. }