window-basic-main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. Copyright (C) 2014 by Zachary Lund <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #include <obs.hpp>
  16. #include <QMessageBox>
  17. #include <QShowEvent>
  18. #include "obs-app.hpp"
  19. #include "window-basic-settings.hpp"
  20. #include "window-namedialog.hpp"
  21. #include "window-basic-main.hpp"
  22. #include "qt-wrappers.hpp"
  23. #include "qt-ptr-variant.hpp"
  24. #include "ui_OBSBasic.h"
  25. using namespace std;
  26. obs_scene_t OBSBasic::GetCurrentScene()
  27. {
  28. QListWidgetItem *item = ui->scenes->currentItem();
  29. return item ? VariantPtr<obs_scene_t>(item->data(Qt::UserRole)) :
  30. nullptr;
  31. }
  32. void OBSBasic::AddScene(obs_source_t source)
  33. {
  34. const char *name = obs_source_getname(source);
  35. obs_scene_t scene = obs_scene_fromsource(source);
  36. QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
  37. item->setData(Qt::UserRole, PtrVariant(scene));
  38. ui->scenes->addItem(item);
  39. signal_handler_t handler = obs_source_signalhandler(source);
  40. signal_handler_connect(handler, "add", OBSBasic::SceneItemAdded, this);
  41. signal_handler_connect(handler, "remove", OBSBasic::SceneItemRemoved,
  42. this);
  43. }
  44. void OBSBasic::RemoveScene(obs_source_t source)
  45. {
  46. const char *name = obs_source_getname(source);
  47. QListWidgetItem *sel = ui->scenes->currentItem();
  48. QList<QListWidgetItem*> items = ui->scenes->findItems(QT_UTF8(name),
  49. Qt::MatchExactly);
  50. if (sel != nullptr) {
  51. if (items.contains(sel))
  52. ui->sources->clear();
  53. delete sel;
  54. }
  55. }
  56. void OBSBasic::AddSceneItem(obs_sceneitem_t item)
  57. {
  58. obs_scene_t scene = obs_sceneitem_getscene(item);
  59. obs_source_t source = obs_sceneitem_getsource(item);
  60. const char *name = obs_source_getname(source);
  61. if (GetCurrentScene() == scene) {
  62. QListWidgetItem *listItem = new QListWidgetItem(QT_UTF8(name));
  63. listItem->setData(Qt::UserRole, PtrVariant(item));
  64. ui->sources->insertItem(0, listItem);
  65. }
  66. sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
  67. }
  68. void OBSBasic::RemoveSceneItem(obs_sceneitem_t item)
  69. {
  70. obs_scene_t scene = obs_sceneitem_getscene(item);
  71. if (GetCurrentScene() == scene) {
  72. for (int i = 0; i < ui->sources->count(); i++) {
  73. QListWidgetItem *listItem = ui->sources->item(i);
  74. QVariant userData = listItem->data(Qt::UserRole);
  75. if (item == VariantPtr<obs_sceneitem_t>(userData)) {
  76. delete listItem;
  77. break;
  78. }
  79. }
  80. }
  81. obs_source_t source = obs_sceneitem_getsource(item);
  82. obs_source_addref(source);
  83. obs_source_release(source);
  84. int scenes = sourceSceneRefs[source] - 1;
  85. if (scenes == 0) {
  86. obs_source_remove(source);
  87. sourceSceneRefs.erase(source);
  88. }
  89. }
  90. void OBSBasic::UpdateSources(obs_scene_t scene)
  91. {
  92. ui->sources->clear();
  93. obs_scene_enum_items(scene,
  94. [] (obs_scene_t scene, obs_sceneitem_t item, void *p)
  95. {
  96. OBSBasic *window = static_cast<OBSBasic*>(p);
  97. window->AddSceneItem(item);
  98. return true;
  99. }, this);
  100. }
  101. void OBSBasic::UpdateSceneSelection(obs_source_t source)
  102. {
  103. if (source) {
  104. obs_source_type type;
  105. obs_source_gettype(source, &type, NULL);
  106. if (type != SOURCE_SCENE)
  107. return;
  108. obs_scene_t scene = obs_scene_fromsource(source);
  109. const char *name = obs_source_getname(source);
  110. QListWidgetItem *sel = ui->scenes->currentItem();
  111. QList<QListWidgetItem*> items =
  112. ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);
  113. if (items.contains(sel)) {
  114. ui->scenes->setCurrentItem(sel);
  115. UpdateSources(scene);
  116. }
  117. }
  118. }
  119. /* OBS Callbacks */
  120. void OBSBasic::SceneItemAdded(void *data, calldata_t params)
  121. {
  122. OBSBasic *window = static_cast<OBSBasic*>(data);
  123. obs_scene_t scene = (obs_scene_t)calldata_ptr(params, "scene");
  124. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  125. window->AddSceneItem(item);
  126. }
  127. void OBSBasic::SceneItemRemoved(void *data, calldata_t params)
  128. {
  129. OBSBasic *window = static_cast<OBSBasic*>(data);
  130. obs_scene_t scene = (obs_scene_t)calldata_ptr(params, "scene");
  131. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  132. window->RemoveSceneItem(item);
  133. }
  134. void OBSBasic::SourceAdded(void *data, calldata_t params)
  135. {
  136. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  137. obs_source_type type;
  138. obs_source_gettype(source, &type, NULL);
  139. if (type == SOURCE_SCENE)
  140. static_cast<OBSBasic*>(data)->AddScene(source);
  141. }
  142. void OBSBasic::SourceDestroyed(void *data, calldata_t params)
  143. {
  144. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  145. obs_source_type type;
  146. obs_source_gettype(source, &type, NULL);
  147. if (type == SOURCE_SCENE)
  148. static_cast<OBSBasic*>(data)->RemoveScene(source);
  149. }
  150. void OBSBasic::ChannelChanged(void *data, calldata_t params)
  151. {
  152. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  153. uint32_t channel = calldata_uint32(params, "channel");
  154. if (channel == 0)
  155. static_cast<OBSBasic*>(data)->UpdateSceneSelection(source);
  156. }
  157. /* Main class functions */
  158. OBSBasic::OBSBasic(QWidget *parent)
  159. : OBSMainWindow (parent),
  160. ui (new Ui::OBSBasic)
  161. {
  162. ui->setupUi(this);
  163. }
  164. void OBSBasic::OBSInit()
  165. {
  166. /* make sure it's fully displayed before doing any initialization */
  167. show();
  168. App()->processEvents();
  169. if (!obs_startup())
  170. throw "Failed to initialize libobs";
  171. if (!InitGraphics())
  172. throw "Failed to initialize graphics";
  173. if (!InitAudio())
  174. throw "Failed to initialize audio";
  175. signal_handler_connect(obs_signalhandler(), "source-add",
  176. OBSBasic::SourceAdded, this);
  177. signal_handler_connect(obs_signalhandler(), "source-destroy",
  178. OBSBasic::SourceDestroyed, this);
  179. signal_handler_connect(obs_signalhandler(), "channel-change",
  180. OBSBasic::ChannelChanged, this);
  181. /* TODO: this is a test */
  182. obs_load_module("test-input");
  183. #ifdef _WIN32
  184. /* HACK: fixes a windows qt bug with native widgets with native
  185. * repaint */
  186. ui->previewContainer->repaint();
  187. #endif
  188. }
  189. OBSBasic::~OBSBasic()
  190. {
  191. obs_shutdown();
  192. }
  193. bool OBSBasic::InitGraphics()
  194. {
  195. struct obs_video_info ovi;
  196. App()->GetConfigFPS(ovi.fps_num, ovi.fps_den);
  197. ovi.graphics_module = App()->GetRenderModule();
  198. ovi.base_width = (uint32_t)config_get_uint(GetGlobalConfig(),
  199. "Video", "BaseCX");
  200. ovi.base_height = (uint32_t)config_get_uint(GetGlobalConfig(),
  201. "Video", "BaseCY");
  202. ovi.output_width = (uint32_t)config_get_uint(GetGlobalConfig(),
  203. "Video", "OutputCX");
  204. ovi.output_height = (uint32_t)config_get_uint(GetGlobalConfig(),
  205. "Video", "OutputCY");
  206. ovi.output_format = VIDEO_FORMAT_RGBA;
  207. ovi.adapter = 0;
  208. //#ifdef __WXGTK__
  209. /* Ugly hack for GTK, I'm hoping this can be avoided eventually... */
  210. // gtk_widget_realize(previewPanel->GetHandle());
  211. //#endif
  212. QTToGSWindow(ui->preview, ovi.window);
  213. //required to make opengl display stuff on osx(?)
  214. ResizePreview(ovi.base_width, ovi.base_height);
  215. QSize size = ui->preview->size();
  216. ovi.window_width = size.width();
  217. ovi.window_height = size.height();
  218. return obs_reset_video(&ovi);
  219. }
  220. bool OBSBasic::InitAudio()
  221. {
  222. /* TODO: load audio settings from config */
  223. struct audio_output_info ai;
  224. ai.name = "test";
  225. ai.samples_per_sec = 44100;
  226. ai.format = AUDIO_FORMAT_16BIT;
  227. ai.speakers = SPEAKERS_STEREO;
  228. ai.buffer_ms = 700;
  229. return obs_reset_audio(&ai);
  230. }
  231. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  232. {
  233. double targetAspect, baseAspect;
  234. QSize targetSize;
  235. int x, y;
  236. /* resize preview panel to fix to the top section of the window */
  237. targetSize = ui->previewContainer->size();
  238. targetAspect = double(targetSize.width()) / double(targetSize.height());
  239. baseAspect = double(cx) / double(cy);
  240. if (targetAspect > baseAspect) {
  241. cx = targetSize.height() * baseAspect;
  242. cy = targetSize.height();
  243. } else {
  244. cx = targetSize.width();
  245. cy = targetSize.width() / baseAspect;
  246. }
  247. x = targetSize.width() /2 - cx/2;
  248. y = targetSize.height()/2 - cy/2;
  249. ui->preview->setGeometry(x, y, cx, cy);
  250. graphics_t graphics = obs_graphics();
  251. if (graphics && isVisible()) {
  252. gs_entercontext(graphics);
  253. gs_resize(cx, cy);
  254. gs_leavecontext();
  255. }
  256. }
  257. void OBSBasic::closeEvent(QCloseEvent *event)
  258. {
  259. }
  260. void OBSBasic::changeEvent(QEvent *event)
  261. {
  262. }
  263. void OBSBasic::resizeEvent(QResizeEvent *event)
  264. {
  265. struct obs_video_info ovi;
  266. if (obs_get_video_info(&ovi))
  267. ResizePreview(ovi.base_width, ovi.base_height);
  268. }
  269. void OBSBasic::on_action_New_triggered()
  270. {
  271. }
  272. void OBSBasic::on_action_Open_triggered()
  273. {
  274. }
  275. void OBSBasic::on_action_Save_triggered()
  276. {
  277. }
  278. void OBSBasic::on_scenes_itemChanged(QListWidgetItem *item)
  279. {
  280. obs_source_t source = NULL;
  281. if (item) {
  282. obs_scene_t scene;
  283. scene = VariantPtr<obs_scene_t>(item->data(Qt::UserRole));
  284. source = obs_scene_getsource(scene);
  285. UpdateSources(scene);
  286. }
  287. /* TODO: allow transitions */
  288. obs_set_output_source(0, source);
  289. }
  290. void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
  291. {
  292. }
  293. void OBSBasic::on_actionAddScene_triggered()
  294. {
  295. string name;
  296. bool accepted = NameDialog::AskForName(this,
  297. QTStr("MainWindow.AddSceneDlg.Title"),
  298. QTStr("MainWindow.AddSceneDlg.Text"),
  299. name);
  300. if (accepted) {
  301. obs_source_t source = obs_get_source_by_name(name.c_str());
  302. if (source) {
  303. QMessageBox::information(this,
  304. QTStr("MainWindow.NameExists.Title"),
  305. QTStr("MainWindow.NameExists.Text"));
  306. obs_source_release(source);
  307. on_actionAddScene_triggered();
  308. return;
  309. }
  310. obs_scene_t scene = obs_scene_create(name.c_str());
  311. source = obs_scene_getsource(scene);
  312. obs_add_source(source);
  313. obs_scene_release(scene);
  314. obs_set_output_source(0, source);
  315. }
  316. }
  317. void OBSBasic::on_actionRemoveScene_triggered()
  318. {
  319. QListWidgetItem *item = ui->scenes->currentItem();
  320. if (!item)
  321. return;
  322. QVariant userData = item->data(Qt::UserRole);
  323. obs_scene_t scene = VariantPtr<obs_scene_t>(userData);
  324. obs_source_t source = obs_scene_getsource(scene);
  325. obs_source_remove(source);
  326. }
  327. void OBSBasic::on_actionSceneProperties_triggered()
  328. {
  329. }
  330. void OBSBasic::on_actionSceneUp_triggered()
  331. {
  332. }
  333. void OBSBasic::on_actionSceneDown_triggered()
  334. {
  335. }
  336. void OBSBasic::on_sources_itemChanged(QListWidgetItem *item)
  337. {
  338. }
  339. void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
  340. {
  341. }
  342. void OBSBasic::AddSource(obs_scene_t scene, const char *id)
  343. {
  344. string name;
  345. bool success = false;
  346. while (!success) {
  347. bool accepted = NameDialog::AskForName(this,
  348. Str("MainWindow.AddSourceDlg.Title"),
  349. Str("MainWindow.AddSourceDlg.Text"),
  350. name);
  351. if (!accepted)
  352. break;
  353. obs_source_t source = obs_get_source_by_name(name.c_str());
  354. if (!source) {
  355. success = true;
  356. } else {
  357. QMessageBox::information(this,
  358. QTStr("MainWindow.NameExists.Title"),
  359. QTStr("MainWindow.NameExists.Text"));
  360. obs_source_release(source);
  361. }
  362. }
  363. if (success) {
  364. obs_source_t source = obs_source_create(SOURCE_INPUT, id,
  365. name.c_str(), NULL);
  366. sourceSceneRefs[source] = 0;
  367. obs_add_source(source);
  368. obs_sceneitem_t item = obs_scene_add(scene, source);
  369. obs_source_release(source);
  370. }
  371. }
  372. void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
  373. {
  374. QListWidgetItem *sel = ui->scenes->currentItem();
  375. const char *type;
  376. bool foundValues = false;
  377. size_t idx = 0;
  378. if (!sel)
  379. return;
  380. obs_scene_t scene = VariantPtr<obs_scene_t>(sel->data(Qt::UserRole));
  381. obs_scene_addref(scene);
  382. QMenu popup;
  383. while (obs_enum_input_types(idx++, &type)) {
  384. const char *name = obs_source_getdisplayname(SOURCE_INPUT,
  385. type, App()->GetLocale());
  386. QAction *popupItem = new QAction(QT_UTF8(name), this);
  387. popupItem->setData(QT_UTF8(type));
  388. popup.addAction(popupItem);
  389. foundValues = true;
  390. }
  391. if (foundValues) {
  392. QAction *ret = popup.exec(pos);
  393. if (ret)
  394. AddSource(scene, ret->data().toString().toUtf8());
  395. }
  396. obs_scene_release(scene);
  397. }
  398. void OBSBasic::on_actionAddSource_triggered()
  399. {
  400. AddSourcePopupMenu(QCursor::pos());
  401. }
  402. void OBSBasic::on_actionRemoveSource_triggered()
  403. {
  404. QListWidgetItem *sel = ui->sources->currentItem();
  405. if (!sel)
  406. return;
  407. obs_scene_t scene = GetCurrentScene();
  408. if (!scene)
  409. return;
  410. gs_entercontext(obs_graphics());
  411. obs_scene_addref(scene);
  412. QVariant userData = sel->data(Qt::UserRole);
  413. obs_sceneitem_t item = VariantPtr<obs_sceneitem_t>(userData);
  414. obs_sceneitem_destroy(scene, item);
  415. obs_scene_release(scene);
  416. gs_leavecontext();
  417. }
  418. void OBSBasic::on_actionSourceProperties_triggered()
  419. {
  420. }
  421. void OBSBasic::on_actionSourceUp_triggered()
  422. {
  423. }
  424. void OBSBasic::on_actionSourceDown_triggered()
  425. {
  426. }
  427. void OBSBasic::on_settingsButton_clicked()
  428. {
  429. OBSBasicSettings settings(this);
  430. settings.exec();
  431. }