window-basic-main.cpp 11 KB

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