window-basic-source-select.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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_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_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_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_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. source = obs_source_create(id, name, NULL, nullptr);
  147. if (source) {
  148. AddSourceData data;
  149. data.source = source;
  150. data.visible = visible;
  151. obs_enter_graphics();
  152. obs_scene_atomic_update(scene, AddSource, &data);
  153. obs_leave_graphics();
  154. newSource = source;
  155. /* set monitoring if source monitors by default */
  156. uint32_t flags = obs_source_get_output_flags(source);
  157. if ((flags & OBS_SOURCE_MONITOR_BY_DEFAULT) != 0) {
  158. obs_source_set_monitoring_type(
  159. source,
  160. OBS_MONITORING_TYPE_MONITOR_ONLY);
  161. }
  162. success = true;
  163. }
  164. }
  165. obs_source_release(source);
  166. return success;
  167. }
  168. void OBSBasicSourceSelect::on_buttonBox_accepted()
  169. {
  170. bool useExisting = ui->selectExisting->isChecked();
  171. bool visible = ui->sourceVisible->isChecked();
  172. if (useExisting) {
  173. QListWidgetItem *item = ui->sourceList->currentItem();
  174. if (!item)
  175. return;
  176. AddExisting(QT_TO_UTF8(item->text()), visible, false);
  177. } else {
  178. if (ui->sourceName->text().isEmpty()) {
  179. OBSMessageBox::warning(this,
  180. QTStr("NoNameEntered.Title"),
  181. QTStr("NoNameEntered.Text"));
  182. return;
  183. }
  184. if (!AddNew(this, id, QT_TO_UTF8(ui->sourceName->text()),
  185. visible, newSource))
  186. return;
  187. }
  188. done(DialogCode::Accepted);
  189. }
  190. void OBSBasicSourceSelect::on_buttonBox_rejected()
  191. {
  192. done(DialogCode::Rejected);
  193. }
  194. static inline const char *GetSourceDisplayName(const char *id)
  195. {
  196. if (strcmp(id, "scene") == 0)
  197. return Str("Basic.Scene");
  198. return obs_source_get_display_name(id);
  199. }
  200. Q_DECLARE_METATYPE(OBSScene);
  201. template<typename T> static inline T GetOBSRef(QListWidgetItem *item)
  202. {
  203. return item->data(static_cast<int>(QtDataRole::OBSRef)).value<T>();
  204. }
  205. OBSBasicSourceSelect::OBSBasicSourceSelect(OBSBasic *parent, const char *id_)
  206. : QDialog(parent), ui(new Ui::OBSBasicSourceSelect), id(id_)
  207. {
  208. setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
  209. ui->setupUi(this);
  210. ui->sourceList->setAttribute(Qt::WA_MacShowFocusRect, false);
  211. QString placeHolderText{QT_UTF8(GetSourceDisplayName(id))};
  212. QString text{placeHolderText};
  213. int i = 2;
  214. obs_source_t *source = nullptr;
  215. while ((source = obs_get_source_by_name(QT_TO_UTF8(text)))) {
  216. obs_source_release(source);
  217. text = QString("%1 %2").arg(placeHolderText).arg(i++);
  218. }
  219. ui->sourceName->setText(text);
  220. ui->sourceName->setFocus(); //Fixes deselect of text.
  221. ui->sourceName->selectAll();
  222. installEventFilter(CreateShortcutFilter());
  223. if (strcmp(id_, "scene") == 0) {
  224. OBSBasic *main =
  225. reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  226. OBSSource curSceneSource = main->GetCurrentSceneSource();
  227. ui->selectExisting->setChecked(true);
  228. ui->createNew->setChecked(false);
  229. ui->createNew->setEnabled(false);
  230. ui->sourceName->setEnabled(false);
  231. int count = main->ui->scenes->count();
  232. for (int i = 0; i < count; i++) {
  233. QListWidgetItem *item = main->ui->scenes->item(i);
  234. OBSScene scene = GetOBSRef<OBSScene>(item);
  235. OBSSource sceneSource = obs_scene_get_source(scene);
  236. if (curSceneSource == sceneSource)
  237. continue;
  238. const char *name = obs_source_get_name(sceneSource);
  239. ui->sourceList->addItem(QT_UTF8(name));
  240. }
  241. } else if (strcmp(id_, "group") == 0) {
  242. obs_enum_sources(EnumGroups, this);
  243. } else {
  244. obs_enum_sources(EnumSources, this);
  245. }
  246. }
  247. void OBSBasicSourceSelect::SourcePaste(const char *name, bool visible, bool dup)
  248. {
  249. AddExisting(name, visible, dup);
  250. }