window-basic-main.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. void OBSBasic::SceneAdded(obs_source_t source)
  23. {
  24. const char *name = obs_source_getname(source);
  25. obs_scene_t scene = obs_scene_fromsource(source);
  26. scenes->Append(wxString(name, wxConvUTF8), scene);
  27. }
  28. void OBSBasic::SceneRemoved(obs_source_t source)
  29. {
  30. const char *name = obs_source_getname(source);
  31. int item = scenes->FindString(name);
  32. if (item != wxNOT_FOUND) {
  33. scenes->Delete(item);
  34. return;
  35. }
  36. item = sources->FindString(name);
  37. if (item != wxNOT_FOUND)
  38. sources->Delete(item);
  39. }
  40. void OBSBasic::SourceAdded(void *data, calldata_t params)
  41. {
  42. OBSBasic *window = (OBSBasic*)data;
  43. obs_source_t source;
  44. calldata_getptr(params, "source", (void**)&source);
  45. obs_source_type type;
  46. obs_source_gettype(source, &type, NULL);
  47. if (type == SOURCE_SCENE)
  48. window->SceneAdded(source);
  49. }
  50. void OBSBasic::SourceDestroyed(void *data, calldata_t params)
  51. {
  52. OBSBasic *window = (OBSBasic*)data;
  53. obs_source_t source;
  54. calldata_getptr(params, "source", (void**)&source);
  55. obs_source_type type;
  56. obs_source_gettype(source, &type, NULL);
  57. if (type == SOURCE_SCENE)
  58. window->SceneRemoved(source);
  59. }
  60. bool OBSBasic::Init()
  61. {
  62. if (!obs_startup())
  63. return false;
  64. if (!InitGraphics())
  65. return false;
  66. signal_handler_connect(obs_signalhandler(), "source-add",
  67. OBSBasic::SourceAdded, this);
  68. signal_handler_connect(obs_signalhandler(), "source-destroy",
  69. OBSBasic::SourceDestroyed, this);
  70. //obs_scene_t scene = obs_scene_create("test scene");
  71. //obs_add_source(obs_scene_getsource(scene));
  72. /* TODO: this is a test */
  73. obs_load_module("test-input");
  74. obs_source_t test = obs_source_create(SOURCE_INPUT, "random", "test",
  75. NULL);
  76. obs_add_source(test);
  77. obs_set_output_source(0, test);
  78. /*obs_scene_t scene = obs_scene_create("test2");
  79. obs_set_output_source(0, obs_scene_getsource(scene));
  80. obs_sceneitem_t bla = obs_scene_add(scene, test);
  81. struct vec2 ddd = {100.0f, 100.0f};
  82. obs_sceneitem_setscale(bla, &ddd);
  83. obs_scene_release(scene);*/
  84. obs_source_release(test);
  85. return true;
  86. }
  87. OBSBasic::~OBSBasic()
  88. {
  89. obs_shutdown();
  90. }
  91. bool OBSBasic::InitGraphics()
  92. {
  93. struct obs_video_info ovi;
  94. wxGetApp().GetConfigFPS(ovi.fps_num, ovi.fps_den);
  95. ovi.graphics_module = wxGetApp().GetRenderModule();
  96. ovi.base_width = (uint32_t)config_get_uint(GetGlobalConfig(),
  97. "Video", "BaseCX");
  98. ovi.base_height = (uint32_t)config_get_uint(GetGlobalConfig(),
  99. "Video", "BaseCY");
  100. ovi.output_width = (uint32_t)config_get_uint(GetGlobalConfig(),
  101. "Video", "OutputCX");
  102. ovi.output_height = (uint32_t)config_get_uint(GetGlobalConfig(),
  103. "Video", "OutputCY");
  104. ovi.output_format = VIDEO_FORMAT_RGBA;
  105. ovi.adapter = 0;
  106. ovi.window = WxToGSWindow(previewPanel);
  107. //required to make opengl display stuff on osx(?)
  108. ResizePreview(ovi.base_width, ovi.base_height);
  109. SendSizeEvent();
  110. wxSize size = previewPanel->GetMinSize();
  111. ovi.window_width = size.x;
  112. ovi.window_height = size.y;
  113. if (!obs_reset_video(&ovi))
  114. return false;
  115. return true;
  116. }
  117. void OBSBasic::OnClose(wxCloseEvent &event)
  118. {
  119. wxGetApp().ExitMainLoop();
  120. event.Skip();
  121. }
  122. void OBSBasic::OnMinimize(wxIconizeEvent &event)
  123. {
  124. event.Skip();
  125. }
  126. void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
  127. {
  128. /* resize preview panel to fix to the top section of the window */
  129. wxSize targetSize = GetPreviewContainer()->GetSize();
  130. double targetAspect = double(targetSize.x) / double(targetSize.y);
  131. double baseAspect = double(cx) / double(cy);
  132. wxSize newSize;
  133. if (targetAspect > baseAspect)
  134. newSize = wxSize(targetSize.y * baseAspect, targetSize.y);
  135. else
  136. newSize = wxSize(targetSize.x, targetSize.x / baseAspect);
  137. GetPreviewPanel()->SetMinSize(newSize);
  138. }
  139. void OBSBasic::OnSize(wxSizeEvent &event)
  140. {
  141. struct obs_video_info ovi;
  142. event.Skip();
  143. if (!obs_get_video_info(&ovi))
  144. return;
  145. ResizePreview(ovi.base_width, ovi.base_height);
  146. }
  147. void OBSBasic::OnResizePreview(wxSizeEvent &event)
  148. {
  149. event.Skip();
  150. wxSize newSize = previewPanel->GetMinSize();
  151. graphics_t graphics = obs_graphics();
  152. if (graphics) {
  153. gs_entercontext(graphics);
  154. gs_resize(newSize.x, newSize.y);
  155. gs_leavecontext();
  156. }
  157. }
  158. void OBSBasic::fileNewClicked(wxCommandEvent &event)
  159. {
  160. }
  161. void OBSBasic::fileOpenClicked(wxCommandEvent &event)
  162. {
  163. }
  164. void OBSBasic::fileSaveClicked(wxCommandEvent &event)
  165. {
  166. }
  167. void OBSBasic::fileExitClicked(wxCommandEvent &event)
  168. {
  169. wxGetApp().ExitMainLoop();
  170. }
  171. void OBSBasic::scenesClicked(wxCommandEvent &event)
  172. {
  173. int sel = scenes->GetSelection();
  174. obs_source_t source = NULL;
  175. if (sel != wxNOT_FOUND) {
  176. obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sel);
  177. source = obs_scene_getsource(scene);
  178. }
  179. obs_set_output_source(0, source);
  180. }
  181. void OBSBasic::scenesRDown(wxMouseEvent &event)
  182. {
  183. }
  184. void OBSBasic::sceneAddClicked(wxCommandEvent &event)
  185. {
  186. string name;
  187. int ret = NameDialog::AskForName(this,
  188. Str("MainWindow.AddSceneDlg.Title"),
  189. Str("MainWindow.AddSceneDlg.Text"),
  190. name);
  191. if (ret == wxID_OK) {
  192. obs_source_t source = obs_get_source_by_name(name.c_str());
  193. if (source) {
  194. wxMessageBox(WXStr("MainWindow.NameExists.Text"),
  195. WXStr("MainWindow.NameExists.Title"),
  196. wxOK|wxCENTRE, this);
  197. obs_source_release(source);
  198. sceneAddClicked(event);
  199. return;
  200. }
  201. obs_scene_t scene = obs_scene_create(name.c_str());
  202. obs_add_source(obs_scene_getsource(scene));
  203. obs_scene_release(scene);
  204. }
  205. }
  206. void OBSBasic::sceneRemoveClicked(wxCommandEvent &event)
  207. {
  208. int sel = scenes->GetSelection();
  209. if (sel == wxNOT_FOUND)
  210. return;
  211. obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sel);
  212. obs_source_t source = obs_scene_getsource(scene);
  213. obs_source_remove(source);
  214. }
  215. void OBSBasic::scenePropertiesClicked(wxCommandEvent &event)
  216. {
  217. }
  218. void OBSBasic::sceneUpClicked(wxCommandEvent &event)
  219. {
  220. }
  221. void OBSBasic::sceneDownClicked(wxCommandEvent &event)
  222. {
  223. }
  224. void OBSBasic::sourcesClicked(wxCommandEvent &event)
  225. {
  226. }
  227. void OBSBasic::sourcesToggled(wxCommandEvent &event)
  228. {
  229. }
  230. void OBSBasic::sourcesRDown(wxMouseEvent &event)
  231. {
  232. }
  233. void OBSBasic::AddSource(obs_scene_t scene, const char *id)
  234. {
  235. string name;
  236. bool success = false;
  237. while (!success) {
  238. int ret = NameDialog::AskForName(this,
  239. Str("MainWindow.AddSourceDlg.Title"),
  240. Str("MainWindow.AddSourceDlg.Text"),
  241. name);
  242. if (ret == wxID_CANCEL)
  243. break;
  244. obs_source_t source = obs_get_source_by_name(
  245. name.c_str());
  246. if (!source) {
  247. success = true;
  248. } else {
  249. wxMessageBox(WXStr("MainWindow.NameExists.Text"),
  250. WXStr("MainWindow.NameExists.Title"),
  251. wxOK|wxCENTRE, this);
  252. obs_source_release(source);
  253. }
  254. }
  255. if (success) {
  256. obs_source_t source = obs_source_create(SOURCE_INPUT, id,
  257. name.c_str(), NULL);
  258. obs_add_source(source);
  259. obs_sceneitem_t item = obs_scene_add(scene, source);
  260. obs_source_release(source);
  261. }
  262. }
  263. void OBSBasic::AddSourcePopup()
  264. {
  265. int sceneSel = scenes->GetSelection();
  266. size_t idx = 0;
  267. const char *type;
  268. vector<const char *> types;
  269. if (sceneSel == wxNOT_FOUND)
  270. return;
  271. obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sceneSel);
  272. obs_scene_addref(scene);
  273. unique_ptr<wxMenu> popup(new wxMenu());
  274. while (obs_enum_input_types(idx, &type)) {
  275. const char *name = obs_source_getdisplayname(SOURCE_INPUT,
  276. type, wxGetApp().GetLocale());
  277. types.push_back(type);
  278. popup->Append((int)idx+1, wxString(name, wxConvUTF8));
  279. idx++;
  280. }
  281. if (idx) {
  282. int id = WXDoPopupMenu(this, popup.get());
  283. if (id != -1)
  284. AddSource(scene, types[id-1]);
  285. }
  286. obs_scene_release(scene);
  287. }
  288. void OBSBasic::sourceAddClicked(wxCommandEvent &event)
  289. {
  290. AddSourcePopup();
  291. }
  292. void OBSBasic::sourceRemoveClicked(wxCommandEvent &event)
  293. {
  294. }
  295. void OBSBasic::sourcePropertiesClicked(wxCommandEvent &event)
  296. {
  297. }
  298. void OBSBasic::sourceUpClicked(wxCommandEvent &event)
  299. {
  300. }
  301. void OBSBasic::sourceDownClicked(wxCommandEvent &event)
  302. {
  303. }
  304. void OBSBasic::settingsClicked(wxCommandEvent &event)
  305. {
  306. OBSBasicSettings test(this);
  307. test.ShowModal();
  308. }
  309. void OBSBasic::exitClicked(wxCommandEvent &event)
  310. {
  311. wxGetApp().ExitMainLoop();
  312. }