window-basic-main.cpp 8.2 KB

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