window-basic-source-select.cpp 8.4 KB

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