window-basic-main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /******************************************************************************
  2. Copyright (C) 2013 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 <obs.hpp>
  15. #include <wx/msgdlg.h>
  16. #include "obs-app.hpp"
  17. #include "wx-wrappers.hpp"
  18. #include "window-basic-settings.hpp"
  19. #include "window-basic-main.hpp"
  20. #include "window-namedialog.hpp"
  21. using namespace std;
  22. obs_scene_t OBSBasic::GetCurrentScene()
  23. {
  24. int sel = scenes->GetSelection();
  25. if (sel == wxNOT_FOUND)
  26. return NULL;
  27. return (obs_scene_t)scenes->GetClientData(sel);
  28. }
  29. void OBSBasic::AddScene(obs_source_t source)
  30. {
  31. const char *name = obs_source_getname(source);
  32. obs_scene_t scene = obs_scene_fromsource(source);
  33. scenes->Append(WX_UTF8(name), scene);
  34. signal_handler_t handler = obs_source_signalhandler(source);
  35. signal_handler_connect(handler, "add", OBSBasic::SceneItemAdded,
  36. this);
  37. signal_handler_connect(handler, "remove", OBSBasic::SceneItemRemoved,
  38. this);
  39. }
  40. void OBSBasic::RemoveScene(obs_source_t source)
  41. {
  42. const char *name = obs_source_getname(source);
  43. int idx = scenes->FindString(name);
  44. int sel = scenes->GetSelection();
  45. if (idx != wxNOT_FOUND) {
  46. if (sel == idx)
  47. sources->Clear();
  48. scenes->Delete(idx);
  49. }
  50. }
  51. void OBSBasic::AddSceneItem(obs_sceneitem_t item)
  52. {
  53. obs_scene_t scene = obs_sceneitem_getscene(item);
  54. obs_source_t source = obs_sceneitem_getsource(item);
  55. const char *name = obs_source_getname(source);
  56. if (GetCurrentScene() == scene)
  57. sources->Insert(WX_UTF8(name), 0, item);
  58. sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
  59. }
  60. void OBSBasic::RemoveSceneItem(obs_sceneitem_t item)
  61. {
  62. obs_scene_t scene = obs_sceneitem_getscene(item);
  63. if (GetCurrentScene() == scene) {
  64. for (unsigned int idx = 0; idx < sources->GetCount(); idx++) {
  65. obs_sceneitem_t curItem;
  66. curItem = (obs_sceneitem_t)sources->GetClientData(idx);
  67. if (item == curItem) {
  68. sources->Delete(idx);
  69. break;
  70. }
  71. }
  72. }
  73. obs_source_t source = obs_sceneitem_getsource(item);
  74. obs_source_addref(source);
  75. obs_source_release(source);
  76. int scenes = sourceSceneRefs[source] - 1;
  77. if (scenes == 0) {
  78. obs_source_remove(source);
  79. sourceSceneRefs.erase(source);
  80. }
  81. }
  82. void OBSBasic::UpdateSources(obs_scene_t scene)
  83. {
  84. sources->Clear();
  85. obs_scene_enum_items(scene,
  86. [] (obs_scene_t scene, obs_sceneitem_t item, void *p)
  87. {
  88. OBSBasic *window = static_cast<OBSBasic*>(p);
  89. window->AddSceneItem(item);
  90. return true;
  91. }, this);
  92. }
  93. void OBSBasic::UpdateSceneSelection(obs_source_t source)
  94. {
  95. if (source) {
  96. obs_source_type type;
  97. obs_source_gettype(source, &type, NULL);
  98. if (type == SOURCE_SCENE) {
  99. obs_scene_t scene = obs_scene_fromsource(source);
  100. const char *name = obs_source_getname(source);
  101. int idx = scenes->FindString(WX_UTF8(name));
  102. int sel = scenes->GetSelection();
  103. if (idx != sel) {
  104. scenes->SetSelection(idx);
  105. UpdateSources(scene);
  106. }
  107. }
  108. } else {
  109. scenes->SetSelection(wxNOT_FOUND);
  110. }
  111. }
  112. /* OBS Callbacks */
  113. void OBSBasic::SceneItemAdded(void *data, calldata_t params)
  114. {
  115. OBSBasic *window = static_cast<OBSBasic*>(data);
  116. obs_scene_t scene = (obs_scene_t)calldata_ptr(params, "scene");
  117. obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
  118. window->AddSceneItem(item);
  119. }
  120. void OBSBasic::SceneItemRemoved(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->RemoveSceneItem(item);
  126. }
  127. void OBSBasic::SourceAdded(void *data, calldata_t params)
  128. {
  129. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  130. obs_source_type type;
  131. obs_source_gettype(source, &type, NULL);
  132. if (type == SOURCE_SCENE)
  133. static_cast<OBSBasic*>(data)->AddScene(source);
  134. }
  135. void OBSBasic::SourceDestroyed(void *data, calldata_t params)
  136. {
  137. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  138. obs_source_type type;
  139. obs_source_gettype(source, &type, NULL);
  140. if (type == SOURCE_SCENE)
  141. static_cast<OBSBasic*>(data)->RemoveScene(source);
  142. }
  143. void OBSBasic::ChannelChanged(void *data, calldata_t params)
  144. {
  145. obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
  146. uint32_t channel = calldata_uint32(params, "channel");
  147. if (channel == 0)
  148. static_cast<OBSBasic*>(data)->UpdateSceneSelection(source);
  149. }
  150. /* Main class functions */
  151. bool OBSBasic::Init()
  152. {
  153. if (!obs_startup())
  154. return false;
  155. if (!InitGraphics())
  156. return false;
  157. signal_handler_connect(obs_signalhandler(), "source-add",
  158. OBSBasic::SourceAdded, this);
  159. signal_handler_connect(obs_signalhandler(), "source-destroy",
  160. OBSBasic::SourceDestroyed, this);
  161. signal_handler_connect(obs_signalhandler(), "channel-change",
  162. OBSBasic::ChannelChanged, this);
  163. /* TODO: this is a test */
  164. obs_load_module("test-input");
  165. return true;
  166. }
  167. OBSBasic::~OBSBasic()
  168. {
  169. obs_shutdown();
  170. }
  171. bool OBSBasic::InitGraphics()
  172. {
  173. struct obs_video_info ovi;
  174. wxGetApp().GetConfigFPS(ovi.fps_num, ovi.fps_den);
  175. ovi.graphics_module = wxGetApp().GetRenderModule();
  176. ovi.base_width = (uint32_t)config_get_uint(GetGlobalConfig(),
  177. "Video", "BaseCX");
  178. ovi.base_height = (uint32_t)config_get_uint(GetGlobalConfig(),
  179. "Video", "BaseCY");
  180. ovi.output_width = (uint32_t)config_get_uint(GetGlobalConfig(),
  181. "Video", "OutputCX");
  182. ovi.output_height = (uint32_t)config_get_uint(GetGlobalConfig(),
  183. "Video", "OutputCY");
  184. ovi.output_format = VIDEO_FORMAT_RGBA;
  185. ovi.adapter = 0;
  186. ovi.window = WxToGSWindow(previewPanel);
  187. //required to make opengl display stuff on osx(?)
  188. ResizePreview(ovi.base_width, ovi.base_height);
  189. SendSizeEvent();
  190. wxSize size = previewPanel->GetClientSize();
  191. ovi.window_width = size.x;
  192. ovi.window_height = size.y;
  193. if (!obs_reset_video(&ovi))
  194. return false;
  195. return true;
  196. }
  197. void OBSBasic::OnClose(wxCloseEvent &event)
  198. {
  199. wxGetApp().ExitMainLoop();
  200. event.Skip();
  201. }
  202. void OBSBasic::OnMinimize(wxIconizeEvent &event)
  203. {
  204. event.Skip();
  205. }
  206. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  207. {
  208. /* resize preview panel to fix to the top section of the window */
  209. wxSize targetSize = GetPreviewContainer()->GetSize();
  210. double targetAspect = double(targetSize.x) / double(targetSize.y);
  211. double baseAspect = double(cx) / double(cy);
  212. wxSize newSize;
  213. if (targetAspect > baseAspect)
  214. newSize = wxSize(targetSize.y * baseAspect, targetSize.y);
  215. else
  216. newSize = wxSize(targetSize.x, targetSize.x / baseAspect);
  217. GetPreviewPanel()->SetMinSize(newSize);
  218. }
  219. void OBSBasic::OnSize(wxSizeEvent &event)
  220. {
  221. struct obs_video_info ovi;
  222. event.Skip();
  223. if (!obs_get_video_info(&ovi))
  224. return;
  225. ResizePreview(ovi.base_width, ovi.base_height);
  226. }
  227. void OBSBasic::OnResizePreview(wxSizeEvent &event)
  228. {
  229. event.Skip();
  230. wxSize newSize = previewPanel->GetClientSize();
  231. graphics_t graphics = obs_graphics();
  232. if (graphics) {
  233. gs_entercontext(graphics);
  234. gs_resize(newSize.x, newSize.y);
  235. gs_leavecontext();
  236. }
  237. }
  238. void OBSBasic::fileNewClicked(wxCommandEvent &event)
  239. {
  240. }
  241. void OBSBasic::fileOpenClicked(wxCommandEvent &event)
  242. {
  243. }
  244. void OBSBasic::fileSaveClicked(wxCommandEvent &event)
  245. {
  246. }
  247. void OBSBasic::fileExitClicked(wxCommandEvent &event)
  248. {
  249. wxGetApp().ExitMainLoop();
  250. }
  251. void OBSBasic::scenesClicked(wxCommandEvent &event)
  252. {
  253. int sel = scenes->GetSelection();
  254. obs_source_t source = NULL;
  255. if (sel != wxNOT_FOUND) {
  256. obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sel);
  257. source = obs_scene_getsource(scene);
  258. UpdateSources(scene);
  259. }
  260. /* TODO: allow transitions */
  261. obs_set_output_source(0, source);
  262. }
  263. void OBSBasic::scenesRDown(wxMouseEvent &event)
  264. {
  265. }
  266. void OBSBasic::sceneAddClicked(wxCommandEvent &event)
  267. {
  268. string name;
  269. int ret = NameDialog::AskForName(this,
  270. Str("MainWindow.AddSceneDlg.Title"),
  271. Str("MainWindow.AddSceneDlg.Text"),
  272. name);
  273. if (ret == wxID_OK) {
  274. obs_source_t source = obs_get_source_by_name(name.c_str());
  275. if (source) {
  276. wxMessageBox(WXStr("MainWindow.NameExists.Text"),
  277. WXStr("MainWindow.NameExists.Title"),
  278. wxOK|wxCENTRE, this);
  279. obs_source_release(source);
  280. sceneAddClicked(event);
  281. return;
  282. }
  283. obs_scene_t scene = obs_scene_create(name.c_str());
  284. source = obs_scene_getsource(scene);
  285. obs_add_source(source);
  286. obs_scene_release(scene);
  287. obs_set_output_source(0, source);
  288. }
  289. }
  290. void OBSBasic::sceneRemoveClicked(wxCommandEvent &event)
  291. {
  292. int sel = scenes->GetSelection();
  293. if (sel == wxNOT_FOUND)
  294. return;
  295. obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sel);
  296. obs_source_t source = obs_scene_getsource(scene);
  297. obs_source_remove(source);
  298. }
  299. void OBSBasic::scenePropertiesClicked(wxCommandEvent &event)
  300. {
  301. }
  302. void OBSBasic::sceneUpClicked(wxCommandEvent &event)
  303. {
  304. }
  305. void OBSBasic::sceneDownClicked(wxCommandEvent &event)
  306. {
  307. }
  308. void OBSBasic::sourcesClicked(wxCommandEvent &event)
  309. {
  310. }
  311. void OBSBasic::sourcesToggled(wxCommandEvent &event)
  312. {
  313. }
  314. void OBSBasic::sourcesRDown(wxMouseEvent &event)
  315. {
  316. }
  317. void OBSBasic::AddSource(obs_scene_t scene, const char *id)
  318. {
  319. string name;
  320. bool success = false;
  321. while (!success) {
  322. int ret = NameDialog::AskForName(this,
  323. Str("MainWindow.AddSourceDlg.Title"),
  324. Str("MainWindow.AddSourceDlg.Text"),
  325. name);
  326. if (ret == wxID_CANCEL)
  327. break;
  328. obs_source_t source = obs_get_source_by_name(
  329. name.c_str());
  330. if (!source) {
  331. success = true;
  332. } else {
  333. wxMessageBox(WXStr("MainWindow.NameExists.Text"),
  334. WXStr("MainWindow.NameExists.Title"),
  335. wxOK|wxCENTRE, this);
  336. obs_source_release(source);
  337. }
  338. }
  339. if (success) {
  340. obs_source_t source = obs_source_create(SOURCE_INPUT, id,
  341. name.c_str(), NULL);
  342. sourceSceneRefs[source] = 0;
  343. obs_add_source(source);
  344. obs_sceneitem_t item = obs_scene_add(scene, source);
  345. obs_source_release(source);
  346. }
  347. }
  348. void OBSBasic::AddSourcePopupMenu()
  349. {
  350. int sceneSel = scenes->GetSelection();
  351. size_t idx = 0;
  352. const char *type;
  353. vector<const char *> types;
  354. if (sceneSel == wxNOT_FOUND)
  355. return;
  356. obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sceneSel);
  357. obs_scene_addref(scene);
  358. unique_ptr<wxMenu> popup(new wxMenu());
  359. while (obs_enum_input_types(idx, &type)) {
  360. const char *name = obs_source_getdisplayname(SOURCE_INPUT,
  361. type, wxGetApp().GetLocale());
  362. types.push_back(type);
  363. popup->Append((int)idx+1, wxString(name, wxConvUTF8));
  364. idx++;
  365. }
  366. if (idx) {
  367. int id = WXDoPopupMenu(this, popup.get());
  368. if (id != -1)
  369. AddSource(scene, types[id-1]);
  370. }
  371. obs_scene_release(scene);
  372. }
  373. void OBSBasic::sourceAddClicked(wxCommandEvent &event)
  374. {
  375. AddSourcePopupMenu();
  376. }
  377. void OBSBasic::sourceRemoveClicked(wxCommandEvent &event)
  378. {
  379. int sel = sources->GetSelection();
  380. if (sel == wxNOT_FOUND)
  381. return;
  382. obs_sceneitem_t item = (obs_sceneitem_t)sources->GetClientData(sel);
  383. obs_source_t source = obs_sceneitem_getsource(item);
  384. obs_sceneitem_destroy(item);
  385. }
  386. void OBSBasic::sourcePropertiesClicked(wxCommandEvent &event)
  387. {
  388. }
  389. void OBSBasic::sourceUpClicked(wxCommandEvent &event)
  390. {
  391. }
  392. void OBSBasic::sourceDownClicked(wxCommandEvent &event)
  393. {
  394. }
  395. void OBSBasic::settingsClicked(wxCommandEvent &event)
  396. {
  397. OBSBasicSettings test(this);
  398. test.ShowModal();
  399. }
  400. void OBSBasic::exitClicked(wxCommandEvent &event)
  401. {
  402. wxGetApp().ExitMainLoop();
  403. }