window-basic-main.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. wxSize size = previewPanel->GetClientSize();
  94. struct obs_video_info ovi;
  95. wxGetApp().GetConfigFPS(ovi.fps_num, ovi.fps_den);
  96. ovi.graphics_module = wxGetApp().GetRenderModule();
  97. ovi.window_width = size.x;
  98. ovi.window_height = size.y;
  99. ovi.base_width = (uint32_t)config_get_uint(GetGlobalConfig(),
  100. "Video", "BaseCX");
  101. ovi.base_height = (uint32_t)config_get_uint(GetGlobalConfig(),
  102. "Video", "BaseCY");
  103. ovi.output_width = (uint32_t)config_get_uint(GetGlobalConfig(),
  104. "Video", "OutputCX");
  105. ovi.output_height = (uint32_t)config_get_uint(GetGlobalConfig(),
  106. "Video", "OutputCY");
  107. ovi.output_format = VIDEO_FORMAT_RGBA;
  108. ovi.adapter = 0;
  109. ovi.window = WxToGSWindow(previewPanel);
  110. if (!obs_reset_video(&ovi))
  111. return false;
  112. //required to make opengl display stuff on osx(?)
  113. SendSizeEvent();
  114. return true;
  115. }
  116. void OBSBasic::OnClose(wxCloseEvent &event)
  117. {
  118. wxGetApp().ExitMainLoop();
  119. event.Skip();
  120. }
  121. void OBSBasic::OnMinimize(wxIconizeEvent &event)
  122. {
  123. event.Skip();
  124. }
  125. void OBSBasic::OnSize(wxSizeEvent &event)
  126. {
  127. struct obs_video_info ovi;
  128. event.Skip();
  129. if (!obs_get_video_info(&ovi))
  130. return;
  131. /* resize preview panel to fix to the top section of the window */
  132. wxSize targetSize = GetPreviewContainer()->GetSize();
  133. double targetAspect = double(targetSize.x) / double(targetSize.y);
  134. double baseAspect = double(ovi.base_width) / double(ovi.base_height);
  135. wxSize newSize;
  136. if (targetAspect > baseAspect)
  137. newSize = wxSize(targetSize.y * baseAspect, targetSize.y);
  138. else
  139. newSize = wxSize(targetSize.x, targetSize.x / baseAspect);
  140. GetPreviewPanel()->SetMinSize(newSize);
  141. gs_entercontext(obs_graphics());
  142. gs_resize(newSize.x, newSize.y);
  143. gs_leavecontext();
  144. }
  145. void OBSBasic::fileNewClicked(wxCommandEvent &event)
  146. {
  147. }
  148. void OBSBasic::fileOpenClicked(wxCommandEvent &event)
  149. {
  150. }
  151. void OBSBasic::fileSaveClicked(wxCommandEvent &event)
  152. {
  153. }
  154. void OBSBasic::fileExitClicked(wxCommandEvent &event)
  155. {
  156. wxGetApp().ExitMainLoop();
  157. }
  158. void OBSBasic::scenesClicked(wxCommandEvent &event)
  159. {
  160. int sel = scenes->GetSelection();
  161. obs_source_t source = NULL;
  162. if (sel != wxNOT_FOUND) {
  163. obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sel);
  164. source = obs_scene_getsource(scene);
  165. }
  166. obs_set_output_source(0, source);
  167. }
  168. void OBSBasic::scenesRDown(wxMouseEvent &event)
  169. {
  170. }
  171. void OBSBasic::sceneAddClicked(wxCommandEvent &event)
  172. {
  173. string name;
  174. int ret = NameDialog::AskForName(this,
  175. Str("MainWindow.AddSceneDlg.Title"),
  176. Str("MainWindow.AddSceneDlg.Text"),
  177. name);
  178. if (ret == wxID_OK) {
  179. obs_source_t source = obs_get_source_by_name(name.c_str());
  180. if (source) {
  181. wxMessageBox(WXStr("MainWindow.NameExists.Text"),
  182. WXStr("MainWindow.NameExists.Title"),
  183. wxOK|wxCENTRE, this);
  184. obs_source_release(source);
  185. sceneAddClicked(event);
  186. return;
  187. }
  188. obs_scene_t scene = obs_scene_create(name.c_str());
  189. obs_add_source(obs_scene_getsource(scene));
  190. obs_scene_release(scene);
  191. }
  192. }
  193. void OBSBasic::sceneRemoveClicked(wxCommandEvent &event)
  194. {
  195. int sel = scenes->GetSelection();
  196. if (sel == wxNOT_FOUND)
  197. return;
  198. obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sel);
  199. obs_source_t source = obs_scene_getsource(scene);
  200. obs_source_remove(source);
  201. }
  202. void OBSBasic::scenePropertiesClicked(wxCommandEvent &event)
  203. {
  204. }
  205. void OBSBasic::sceneUpClicked(wxCommandEvent &event)
  206. {
  207. }
  208. void OBSBasic::sceneDownClicked(wxCommandEvent &event)
  209. {
  210. }
  211. void OBSBasic::sourcesClicked(wxCommandEvent &event)
  212. {
  213. }
  214. void OBSBasic::sourcesToggled(wxCommandEvent &event)
  215. {
  216. }
  217. void OBSBasic::sourcesRDown(wxMouseEvent &event)
  218. {
  219. }
  220. void OBSBasic::AddSource(obs_scene_t scene, const char *id)
  221. {
  222. string name;
  223. bool success = false;
  224. while (!success) {
  225. int ret = NameDialog::AskForName(this,
  226. Str("MainWindow.AddSourceDlg.Title"),
  227. Str("MainWindow.AddSourceDlg.Text"),
  228. name);
  229. if (ret == wxID_CANCEL)
  230. break;
  231. obs_source_t source = obs_get_source_by_name(
  232. name.c_str());
  233. if (!source) {
  234. success = true;
  235. } else {
  236. wxMessageBox(WXStr("MainWindow.NameExists.Text"),
  237. WXStr("MainWindow.NameExists.Title"),
  238. wxOK|wxCENTRE, this);
  239. obs_source_release(source);
  240. }
  241. }
  242. if (success) {
  243. obs_source_t source = obs_source_create(SOURCE_INPUT, id,
  244. name.c_str(), NULL);
  245. obs_add_source(source);
  246. obs_sceneitem_t item = obs_scene_add(scene, source);
  247. obs_source_release(source);
  248. }
  249. }
  250. void OBSBasic::AddSourcePopup()
  251. {
  252. int sceneSel = scenes->GetSelection();
  253. size_t idx = 0;
  254. const char *type;
  255. vector<const char *> types;
  256. if (sceneSel == wxNOT_FOUND)
  257. return;
  258. obs_scene_t scene = (obs_scene_t)scenes->GetClientData(sceneSel);
  259. obs_scene_addref(scene);
  260. unique_ptr<wxMenu> popup(new wxMenu());
  261. while (obs_enum_input_types(idx, &type)) {
  262. const char *name = obs_source_getdisplayname(SOURCE_INPUT,
  263. type, wxGetApp().GetLocale());
  264. types.push_back(type);
  265. popup->Append((int)idx+1, wxString(name, wxConvUTF8));
  266. idx++;
  267. }
  268. if (idx) {
  269. int id = WXDoPopupMenu(this, popup.get());
  270. if (id != -1)
  271. AddSource(scene, types[id-1]);
  272. }
  273. obs_scene_release(scene);
  274. }
  275. void OBSBasic::sourceAddClicked(wxCommandEvent &event)
  276. {
  277. AddSourcePopup();
  278. }
  279. void OBSBasic::sourceRemoveClicked(wxCommandEvent &event)
  280. {
  281. }
  282. void OBSBasic::sourcePropertiesClicked(wxCommandEvent &event)
  283. {
  284. }
  285. void OBSBasic::sourceUpClicked(wxCommandEvent &event)
  286. {
  287. }
  288. void OBSBasic::sourceDownClicked(wxCommandEvent &event)
  289. {
  290. }
  291. void OBSBasic::settingsClicked(wxCommandEvent &event)
  292. {
  293. OBSBasicSettings test(this);
  294. test.ShowModal();
  295. }
  296. void OBSBasic::exitClicked(wxCommandEvent &event)
  297. {
  298. wxGetApp().ExitMainLoop();
  299. }