1
0

window-basic-main.cpp 11 KB

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