window-basic-main.cpp 13 KB

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