obs-app.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 <sstream>
  15. #include <util/bmem.h>
  16. #include <util/dstr.h>
  17. #include <util/platform.h>
  18. #include "obs-app.hpp"
  19. #include "window-main-basic.hpp"
  20. #include "obs-wrappers.hpp"
  21. #include "wx-wrappers.hpp"
  22. #include "platform.hpp"
  23. using namespace std;
  24. IMPLEMENT_APP(OBSApp);
  25. OBSAppBase::~OBSAppBase()
  26. {
  27. blog(LOG_INFO, "Number of memory leaks: %llu", bnum_allocs());
  28. }
  29. static void do_log(enum log_type type, const char *msg, va_list args)
  30. {
  31. char bla[4096];
  32. vsnprintf(bla, 4095, msg, args);
  33. #ifdef _WIN32
  34. OutputDebugStringA(bla);
  35. OutputDebugStringA("\n");
  36. if (type >= LOG_WARNING)
  37. __debugbreak();
  38. #endif
  39. }
  40. bool OBSApp::InitGlobalConfigDefaults()
  41. {
  42. config_set_default_string(globalConfig, "General", "Language", "en");
  43. config_set_default_int(globalConfig, "Window", "PosX", -1);
  44. config_set_default_int(globalConfig, "Window", "PosY", -1);
  45. config_set_default_int(globalConfig, "Window", "SizeX", -1);
  46. config_set_default_int(globalConfig, "Window", "SizeY", -1);
  47. vector<MonitorInfo> monitors;
  48. GetMonitors(monitors);
  49. if (!monitors.size()) {
  50. OBSErrorBox(NULL, "There appears to be no monitors. Er, this "
  51. "technically shouldn't be possible.");
  52. return false;
  53. }
  54. uint32_t cx = monitors[0].cx;
  55. uint32_t cy = monitors[0].cy;
  56. #if _WIN32
  57. config_set_default_string(globalConfig, "Video", "Renderer",
  58. "Direct3D 11");
  59. #else
  60. config_set_default_string(globalConfig, "Video", "Renderer",
  61. "OpenGL");
  62. #endif
  63. config_set_default_uint(globalConfig, "Video", "BaseCX", cx);
  64. config_set_default_uint(globalConfig, "Video", "BaseCY", cy);
  65. cx = cx * 10 / 15;
  66. cy = cy * 10 / 15;
  67. config_set_default_uint(globalConfig, "Video", "OutputCX", cx);
  68. config_set_default_uint(globalConfig, "Video", "OutputCY", cy);
  69. config_set_default_string(globalConfig, "Video", "FPSType", "Common");
  70. config_set_default_string(globalConfig, "Video", "FPSCommon", "30");
  71. config_set_default_uint(globalConfig, "Video", "FPSInt", 30);
  72. config_set_default_uint(globalConfig, "Video", "FPSNum", 30);
  73. config_set_default_uint(globalConfig, "Video", "FPSDen", 1);
  74. config_set_default_uint(globalConfig, "Video", "FPSNS", 33333333);
  75. return true;
  76. }
  77. static bool do_mkdir(const char *path)
  78. {
  79. if (os_mkdir(path) == MKDIR_ERROR) {
  80. OBSErrorBox(NULL, "Failed to create directory %s", path);
  81. return false;
  82. }
  83. return true;
  84. }
  85. static bool MakeUserDirs()
  86. {
  87. BPtr<char> homePath(os_get_home_path());
  88. stringstream str;
  89. str << homePath << "/obs-studio";
  90. if (!do_mkdir(str.str().c_str()))
  91. return false;
  92. return true;
  93. }
  94. bool OBSApp::InitGlobalConfig()
  95. {
  96. BPtr<char> homePath(os_get_home_path());
  97. stringstream str;
  98. if (!homePath) {
  99. OBSErrorBox(NULL, "Failed to get home path");
  100. return false;
  101. }
  102. str << homePath << "/obs-studio/global.ini";
  103. string path = move(str.str());
  104. int errorcode = globalConfig.Open(path.c_str(), CONFIG_OPEN_ALWAYS);
  105. if (errorcode != CONFIG_SUCCESS) {
  106. OBSErrorBox(NULL, "Failed to open global.ini: %d", errorcode);
  107. return false;
  108. }
  109. if (!InitGlobalConfigDefaults())
  110. return false;
  111. return true;
  112. }
  113. #define DEFAULT_LANG "en"
  114. bool OBSApp::InitLocale()
  115. {
  116. const char *lang = config_get_string(globalConfig, "General",
  117. "Language");
  118. stringstream file;
  119. file << "locale/" << lang << ".txt";
  120. string englishPath;
  121. if (!GetDataFilePath("locale/" DEFAULT_LANG ".txt", englishPath)) {
  122. OBSErrorBox(NULL, "Failed to find locale/" DEFAULT_LANG ".txt");
  123. return false;
  124. }
  125. textLookup = text_lookup_create(englishPath.c_str());
  126. if (!textLookup) {
  127. OBSErrorBox(NULL, "Failed to create locale from file '%s'",
  128. englishPath.c_str());
  129. return false;
  130. }
  131. if (astrcmpi(lang, DEFAULT_LANG) == 0)
  132. return true;
  133. string path;
  134. if (GetDataFilePath(file.str().c_str(), path)) {
  135. if (!text_lookup_add(textLookup, path.c_str()))
  136. blog(LOG_WARNING, "Failed to add locale file '%s'",
  137. path.c_str());
  138. } else {
  139. blog(LOG_WARNING, "Could not find locale file '%s'",
  140. file.str().c_str());
  141. }
  142. return true;
  143. }
  144. bool OBSApp::OnInit()
  145. {
  146. base_set_log_handler(do_log);
  147. if (!wxApp::OnInit())
  148. return false;
  149. if (!MakeUserDirs())
  150. return false;
  151. if (!InitGlobalConfig())
  152. return false;
  153. if (!InitLocale())
  154. return false;
  155. if (!obs_startup())
  156. return false;
  157. wxInitAllImageHandlers();
  158. OBSBasic *mainWindow = new OBSBasic();
  159. wxSize size = mainWindow->GetPreviewPanel()->GetClientSize();
  160. mainWindow->Show();
  161. /* this is a test */
  162. struct obs_video_info ovi;
  163. ovi.graphics_module = "libobs-opengl";
  164. ovi.fps_num = 30000;
  165. ovi.fps_den = 1001;
  166. ovi.window_width = size.x;
  167. ovi.window_height = size.y;
  168. ovi.base_width = 1920;
  169. ovi.base_height = 1080;
  170. ovi.output_width = 1280;
  171. ovi.output_height = 720;
  172. ovi.output_format = VIDEO_FORMAT_RGBA;
  173. ovi.adapter = 0;
  174. ovi.window = WxToGSWindow(mainWindow->GetPreviewPanel());
  175. if (!obs_reset_video(&ovi))
  176. return false;
  177. //required to make opengl display stuff on osx(?)
  178. mainWindow->SendSizeEvent();
  179. return true;
  180. }
  181. int OBSApp::OnExit()
  182. {
  183. obs_shutdown();
  184. return wxApp::OnExit();
  185. }
  186. void OBSApp::CleanUp()
  187. {
  188. wxApp::CleanUp();
  189. }