window-basic-source-select.cpp 11 KB

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