1
0

window-basic-source-select.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. };
  23. bool OBSBasicSourceSelect::EnumSources(void *data, obs_source_t *source)
  24. {
  25. if (obs_source_is_hidden(source))
  26. return false;
  27. OBSBasicSourceSelect *window =
  28. static_cast<OBSBasicSourceSelect *>(data);
  29. const char *name = obs_source_get_name(source);
  30. const char *id = obs_source_get_unversioned_id(source);
  31. if (strcmp(id, window->id) == 0)
  32. window->ui->sourceList->addItem(QT_UTF8(name));
  33. return true;
  34. }
  35. bool OBSBasicSourceSelect::EnumGroups(void *data, obs_source_t *source)
  36. {
  37. OBSBasicSourceSelect *window =
  38. static_cast<OBSBasicSourceSelect *>(data);
  39. const char *name = obs_source_get_name(source);
  40. const char *id = obs_source_get_unversioned_id(source);
  41. if (strcmp(id, window->id) == 0) {
  42. OBSBasic *main =
  43. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  44. OBSScene scene = main->GetCurrentScene();
  45. obs_sceneitem_t *existing = obs_scene_get_group(scene, name);
  46. if (!existing)
  47. window->ui->sourceList->addItem(QT_UTF8(name));
  48. }
  49. return true;
  50. }
  51. void OBSBasicSourceSelect::OBSSourceAdded(void *data, calldata_t *calldata)
  52. {
  53. OBSBasicSourceSelect *window =
  54. static_cast<OBSBasicSourceSelect *>(data);
  55. obs_source_t *source = (obs_source_t *)calldata_ptr(calldata, "source");
  56. QMetaObject::invokeMethod(window, "SourceAdded",
  57. Q_ARG(OBSSource, source));
  58. }
  59. void OBSBasicSourceSelect::OBSSourceRemoved(void *data, calldata_t *calldata)
  60. {
  61. OBSBasicSourceSelect *window =
  62. static_cast<OBSBasicSourceSelect *>(data);
  63. obs_source_t *source = (obs_source_t *)calldata_ptr(calldata, "source");
  64. QMetaObject::invokeMethod(window, "SourceRemoved",
  65. Q_ARG(OBSSource, source));
  66. }
  67. void OBSBasicSourceSelect::SourceAdded(OBSSource source)
  68. {
  69. const char *name = obs_source_get_name(source);
  70. const char *sourceId = obs_source_get_unversioned_id(source);
  71. if (strcmp(sourceId, id) != 0)
  72. return;
  73. ui->sourceList->addItem(name);
  74. }
  75. void OBSBasicSourceSelect::SourceRemoved(OBSSource source)
  76. {
  77. const char *name = obs_source_get_name(source);
  78. const char *sourceId = obs_source_get_unversioned_id(source);
  79. if (strcmp(sourceId, id) != 0)
  80. return;
  81. QList<QListWidgetItem *> items =
  82. ui->sourceList->findItems(name, Qt::MatchFixedString);
  83. if (!items.count())
  84. return;
  85. delete items[0];
  86. }
  87. static void AddSource(void *_data, obs_scene_t *scene)
  88. {
  89. AddSourceData *data = (AddSourceData *)_data;
  90. obs_sceneitem_t *sceneitem;
  91. sceneitem = obs_scene_add(scene, data->source);
  92. obs_sceneitem_set_visible(sceneitem, data->visible);
  93. }
  94. static char *get_new_source_name(const char *name)
  95. {
  96. struct dstr new_name = {0};
  97. int inc = 0;
  98. dstr_copy(&new_name, name);
  99. for (;;) {
  100. obs_source_t *existing_source =
  101. obs_get_source_by_name(new_name.array);
  102. if (!existing_source)
  103. break;
  104. obs_source_release(existing_source);
  105. dstr_printf(&new_name, "%s %d", name, ++inc + 1);
  106. }
  107. return new_name.array;
  108. }
  109. static void AddExisting(const char *name, bool visible, bool duplicate)
  110. {
  111. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  112. OBSScene scene = main->GetCurrentScene();
  113. if (!scene)
  114. return;
  115. obs_source_t *source = obs_get_source_by_name(name);
  116. if (source) {
  117. if (duplicate) {
  118. obs_source_t *from = source;
  119. char *new_name = get_new_source_name(name);
  120. source = obs_source_duplicate(from, new_name, false);
  121. bfree(new_name);
  122. obs_source_release(from);
  123. if (!source)
  124. return;
  125. }
  126. AddSourceData data;
  127. data.source = source;
  128. data.visible = visible;
  129. obs_enter_graphics();
  130. obs_scene_atomic_update(scene, AddSource, &data);
  131. obs_leave_graphics();
  132. obs_source_release(source);
  133. }
  134. }
  135. bool AddNew(QWidget *parent, const char *id, const char *name,
  136. const bool visible, OBSSource &newSource)
  137. {
  138. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  139. OBSScene scene = main->GetCurrentScene();
  140. bool success = false;
  141. if (!scene)
  142. return false;
  143. obs_source_t *source = obs_get_source_by_name(name);
  144. if (source && parent) {
  145. OBSMessageBox::information(parent, QTStr("NameExists.Title"),
  146. QTStr("NameExists.Text"));
  147. } else {
  148. const char *v_id = obs_get_latest_input_type_id(id);
  149. source = obs_source_create(v_id, name, NULL, nullptr);
  150. if (source) {
  151. AddSourceData data;
  152. data.source = source;
  153. data.visible = visible;
  154. obs_enter_graphics();
  155. obs_scene_atomic_update(scene, AddSource, &data);
  156. obs_leave_graphics();
  157. newSource = source;
  158. /* set monitoring if source monitors by default */
  159. uint32_t flags = obs_source_get_output_flags(source);
  160. if ((flags & OBS_SOURCE_MONITOR_BY_DEFAULT) != 0) {
  161. obs_source_set_monitoring_type(
  162. source,
  163. OBS_MONITORING_TYPE_MONITOR_ONLY);
  164. }
  165. success = true;
  166. }
  167. }
  168. obs_source_release(source);
  169. return success;
  170. }
  171. void OBSBasicSourceSelect::on_buttonBox_accepted()
  172. {
  173. bool useExisting = ui->selectExisting->isChecked();
  174. bool visible = ui->sourceVisible->isChecked();
  175. if (useExisting) {
  176. QListWidgetItem *item = ui->sourceList->currentItem();
  177. if (!item)
  178. return;
  179. AddExisting(QT_TO_UTF8(item->text()), visible, false);
  180. } else {
  181. if (ui->sourceName->text().isEmpty()) {
  182. OBSMessageBox::warning(this,
  183. QTStr("NoNameEntered.Title"),
  184. QTStr("NoNameEntered.Text"));
  185. return;
  186. }
  187. if (!AddNew(this, id, QT_TO_UTF8(ui->sourceName->text()),
  188. visible, newSource))
  189. return;
  190. OBSBasic *main =
  191. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  192. std::string scene_name =
  193. obs_source_get_name(main->GetCurrentSceneSource());
  194. auto undo = [scene_name, main](const std::string &data) {
  195. obs_source_t *source =
  196. obs_get_source_by_name(data.c_str());
  197. obs_source_release(source);
  198. obs_source_remove(source);
  199. obs_source_t *scene_source =
  200. obs_get_source_by_name(scene_name.c_str());
  201. main->SetCurrentScene(scene_source, true);
  202. obs_source_release(scene_source);
  203. main->RefreshSources(main->GetCurrentScene());
  204. };
  205. obs_data_t *wrapper = obs_data_create();
  206. obs_data_set_string(wrapper, "id", id);
  207. obs_sceneitem_t *item = obs_scene_sceneitem_from_source(
  208. main->GetCurrentScene(), newSource);
  209. obs_data_set_int(wrapper, "item_id",
  210. obs_sceneitem_get_id(item));
  211. obs_data_set_string(
  212. wrapper, "name",
  213. ui->sourceName->text().toUtf8().constData());
  214. obs_data_set_bool(wrapper, "visible", visible);
  215. auto redo = [scene_name, main](const std::string &data) {
  216. obs_data_t *dat =
  217. obs_data_create_from_json(data.c_str());
  218. OBSSource source;
  219. AddNew(NULL, obs_data_get_string(dat, "id"),
  220. obs_data_get_string(dat, "name"),
  221. obs_data_get_bool(dat, "visible"), source);
  222. obs_sceneitem_t *item = obs_scene_sceneitem_from_source(
  223. main->GetCurrentScene(), source);
  224. obs_sceneitem_set_id(item, (int64_t)obs_data_get_int(
  225. dat, "item_id"));
  226. obs_source_t *scene_source =
  227. obs_get_source_by_name(scene_name.c_str());
  228. main->SetCurrentScene(scene_source, true);
  229. obs_source_release(scene_source);
  230. main->RefreshSources(main->GetCurrentScene());
  231. obs_data_release(dat);
  232. obs_sceneitem_release(item);
  233. };
  234. undo_s.add_action(QTStr("Undo.Add").arg(ui->sourceName->text()),
  235. undo, redo,
  236. std::string(obs_source_get_name(newSource)),
  237. std::string(obs_data_get_json(wrapper)),
  238. NULL);
  239. obs_data_release(wrapper);
  240. obs_sceneitem_release(item);
  241. }
  242. done(DialogCode::Accepted);
  243. }
  244. void OBSBasicSourceSelect::on_buttonBox_rejected()
  245. {
  246. done(DialogCode::Rejected);
  247. }
  248. static inline const char *GetSourceDisplayName(const char *id)
  249. {
  250. if (strcmp(id, "scene") == 0)
  251. return Str("Basic.Scene");
  252. const char *v_id = obs_get_latest_input_type_id(id);
  253. return obs_source_get_display_name(v_id);
  254. }
  255. Q_DECLARE_METATYPE(OBSScene);
  256. template<typename T> static inline T GetOBSRef(QListWidgetItem *item)
  257. {
  258. return item->data(static_cast<int>(QtDataRole::OBSRef)).value<T>();
  259. }
  260. OBSBasicSourceSelect::OBSBasicSourceSelect(OBSBasic *parent, const char *id_,
  261. undo_stack &undo_s)
  262. : QDialog(parent),
  263. ui(new Ui::OBSBasicSourceSelect),
  264. id(id_),
  265. undo_s(undo_s)
  266. {
  267. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  268. ui->setupUi(this);
  269. ui->sourceList->setAttribute(Qt::WA_MacShowFocusRect, false);
  270. QString placeHolderText{QT_UTF8(GetSourceDisplayName(id))};
  271. QString text{placeHolderText};
  272. int i = 2;
  273. obs_source_t *source = nullptr;
  274. while ((source = obs_get_source_by_name(QT_TO_UTF8(text)))) {
  275. obs_source_release(source);
  276. text = QString("%1 %2").arg(placeHolderText).arg(i++);
  277. }
  278. ui->sourceName->setText(text);
  279. ui->sourceName->setFocus(); //Fixes deselect of text.
  280. ui->sourceName->selectAll();
  281. installEventFilter(CreateShortcutFilter());
  282. if (strcmp(id_, "scene") == 0) {
  283. OBSBasic *main =
  284. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  285. OBSSource curSceneSource = main->GetCurrentSceneSource();
  286. ui->selectExisting->setChecked(true);
  287. ui->createNew->setChecked(false);
  288. ui->createNew->setEnabled(false);
  289. ui->sourceName->setEnabled(false);
  290. int count = main->ui->scenes->count();
  291. for (int i = 0; i < count; i++) {
  292. QListWidgetItem *item = main->ui->scenes->item(i);
  293. OBSScene scene = GetOBSRef<OBSScene>(item);
  294. OBSSource sceneSource = obs_scene_get_source(scene);
  295. if (curSceneSource == sceneSource)
  296. continue;
  297. const char *name = obs_source_get_name(sceneSource);
  298. ui->sourceList->addItem(QT_UTF8(name));
  299. }
  300. } else if (strcmp(id_, "group") == 0) {
  301. obs_enum_sources(EnumGroups, this);
  302. } else {
  303. obs_enum_sources(EnumSources, this);
  304. }
  305. }
  306. void OBSBasicSourceSelect::SourcePaste(const char *name, bool visible, bool dup)
  307. {
  308. AddExisting(name, visible, dup);
  309. }