obs-app.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.hpp>
  19. #include "qt-wrappers.hpp"
  20. #include "obs-app.hpp"
  21. #include "window-basic-main.hpp"
  22. #include "platform.hpp"
  23. #include <windows.h>
  24. using namespace std;
  25. static void do_log(enum log_type type, const char *msg, va_list args)
  26. {
  27. #ifdef _WIN32
  28. char bla[4096];
  29. vsnprintf(bla, 4095, msg, args);
  30. OutputDebugStringA(bla);
  31. OutputDebugStringA("\n");
  32. if (type >= LOG_WARNING && IsDebuggerPresent())
  33. __debugbreak();
  34. #else
  35. vprintf(msg, args);
  36. printf("\n");
  37. #endif
  38. }
  39. bool OBSApp::InitGlobalConfigDefaults()
  40. {
  41. config_set_default_string(globalConfig, "General", "Language", "en");
  42. config_set_default_int(globalConfig, "Window", "PosX", -1);
  43. config_set_default_int(globalConfig, "Window", "PosY", -1);
  44. config_set_default_int(globalConfig, "Window", "SizeX", -1);
  45. config_set_default_int(globalConfig, "Window", "SizeY", -1);
  46. vector<MonitorInfo> monitors;
  47. GetMonitors(monitors);
  48. if (!monitors.size()) {
  49. OBSErrorBox(NULL, "There appears to be no monitors. Er, this "
  50. "technically shouldn't be possible.");
  51. return false;
  52. }
  53. uint32_t cx = monitors[0].cx;
  54. uint32_t cy = monitors[0].cy;
  55. #if _WIN32
  56. config_set_default_string(globalConfig, "Video", "Renderer",
  57. "Direct3D 11");
  58. #else
  59. config_set_default_string(globalConfig, "Video", "Renderer",
  60. "OpenGL");
  61. #endif
  62. config_set_default_uint(globalConfig, "Video", "BaseCX", cx);
  63. config_set_default_uint(globalConfig, "Video", "BaseCY", cy);
  64. cx = cx * 10 / 15;
  65. cy = cy * 10 / 15;
  66. config_set_default_uint(globalConfig, "Video", "OutputCX", cx);
  67. config_set_default_uint(globalConfig, "Video", "OutputCY", cy);
  68. config_set_default_string(globalConfig, "Video", "FPSType", "Common");
  69. config_set_default_string(globalConfig, "Video", "FPSCommon", "30");
  70. config_set_default_uint(globalConfig, "Video", "FPSInt", 30);
  71. config_set_default_uint(globalConfig, "Video", "FPSNum", 30);
  72. config_set_default_uint(globalConfig, "Video", "FPSDen", 1);
  73. config_set_default_uint(globalConfig, "Video", "FPSNS", 33333333);
  74. return true;
  75. }
  76. static bool do_mkdir(const char *path)
  77. {
  78. if (os_mkdir(path) == MKDIR_ERROR) {
  79. OBSErrorBox(NULL, "Failed to create directory %s", path);
  80. return false;
  81. }
  82. return true;
  83. }
  84. static bool MakeUserDirs()
  85. {
  86. BPtr<char> configPath(os_get_config_path("obs-studio"));
  87. return do_mkdir(configPath);
  88. }
  89. bool OBSApp::InitGlobalConfig()
  90. {
  91. BPtr<char> path(os_get_config_path("obs-studio/global.ini"));
  92. int errorcode = globalConfig.Open(path, CONFIG_OPEN_ALWAYS);
  93. if (errorcode != CONFIG_SUCCESS) {
  94. OBSErrorBox(NULL, "Failed to open global.ini: %d", errorcode);
  95. return false;
  96. }
  97. return InitGlobalConfigDefaults();
  98. }
  99. #define DEFAULT_LANG "en"
  100. bool OBSApp::InitLocale()
  101. {
  102. const char *lang = config_get_string(globalConfig, "General",
  103. "Language");
  104. locale = lang;
  105. stringstream file;
  106. file << "locale/" << lang << ".txt";
  107. string englishPath;
  108. if (!GetDataFilePath("locale/" DEFAULT_LANG ".txt", englishPath)) {
  109. OBSErrorBox(NULL, "Failed to find locale/" DEFAULT_LANG ".txt");
  110. return false;
  111. }
  112. textLookup = text_lookup_create(englishPath.c_str());
  113. if (!textLookup) {
  114. OBSErrorBox(NULL, "Failed to create locale from file '%s'",
  115. englishPath.c_str());
  116. return false;
  117. }
  118. if (astrcmpi(lang, DEFAULT_LANG) == 0)
  119. return true;
  120. string path;
  121. if (GetDataFilePath(file.str().c_str(), path)) {
  122. if (!text_lookup_add(textLookup, path.c_str()))
  123. blog(LOG_WARNING, "Failed to add locale file '%s'",
  124. path.c_str());
  125. } else {
  126. blog(LOG_WARNING, "Could not find locale file '%s'",
  127. file.str().c_str());
  128. }
  129. return true;
  130. }
  131. bool OBSApp::InitOBSBasic()
  132. {
  133. try {
  134. mainWindow = move(unique_ptr<OBSBasic>(new OBSBasic()));
  135. mainWindow->show();
  136. return true;
  137. } catch (const char *error) {
  138. blog(LOG_ERROR, "%s", error);
  139. }
  140. return false;
  141. }
  142. OBSApp::OBSApp(int &argc, char **argv)
  143. : QApplication(argc, argv)
  144. {
  145. if (!InitApplicationBundle())
  146. throw "Failed to initialize application bundle";
  147. if (!MakeUserDirs())
  148. throw "Failed to created required user directories";
  149. if (!InitGlobalConfig())
  150. throw "Failed to initialize global config";
  151. if (!InitLocale())
  152. throw "Failed to load locale";
  153. if (!InitOBSBasic())
  154. throw "Failed to create main window";
  155. }
  156. void OBSApp::GetFPSCommon(uint32_t &num, uint32_t &den) const
  157. {
  158. const char *val = config_get_string(globalConfig, "Video", "FPSCommon");
  159. if (strcmp(val, "10") == 0) {
  160. num = 30;
  161. den = 1;
  162. } else if (strcmp(val, "20") == 0) {
  163. num = 20;
  164. den = 1;
  165. } else if (strcmp(val, "25") == 0) {
  166. num = 25;
  167. den = 1;
  168. } else if (strcmp(val, "29.97") == 0) {
  169. num = 30000;
  170. den = 1001;
  171. } else if (strcmp(val, "48") == 0) {
  172. num = 48;
  173. den = 1;
  174. } else if (strcmp(val, "59.94") == 0) {
  175. num = 60000;
  176. den = 1001;
  177. } else if (strcmp(val, "60") == 0) {
  178. num = 60;
  179. den = 1;
  180. } else {
  181. num = 30;
  182. den = 1;
  183. }
  184. }
  185. void OBSApp::GetFPSInteger(uint32_t &num, uint32_t &den) const
  186. {
  187. num = (uint32_t)config_get_uint(globalConfig, "Video", "FPSInt");
  188. den = 1;
  189. }
  190. void OBSApp::GetFPSFraction(uint32_t &num, uint32_t &den) const
  191. {
  192. num = (uint32_t)config_get_uint(globalConfig, "Video", "FPSNum");
  193. den = (uint32_t)config_get_uint(globalConfig, "Video", "FPSDen");
  194. }
  195. void OBSApp::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
  196. {
  197. num = 1000000000;
  198. den = (uint32_t)config_get_uint(globalConfig, "Video", "FPSNS");
  199. }
  200. void OBSApp::GetConfigFPS(uint32_t &num, uint32_t &den) const
  201. {
  202. const char *type = config_get_string(globalConfig, "Video", "FPSType");
  203. if (astrcmpi(type, "Integer") == 0)
  204. GetFPSInteger(num, den);
  205. else if (astrcmpi(type, "Fraction") == 0)
  206. GetFPSFraction(num, den);
  207. else if (astrcmpi(type, "Nanoseconds") == 0)
  208. GetFPSNanoseconds(num, den);
  209. else
  210. GetFPSCommon(num, den);
  211. }
  212. const char *OBSApp::GetRenderModule() const
  213. {
  214. const char *renderer = config_get_string(globalConfig, "Video",
  215. "Renderer");
  216. if (astrcmpi(renderer, "Direct3D 11") == 0)
  217. return "libobs-d3d11";
  218. else
  219. return "libobs-opengl";
  220. }
  221. int main(int argc, char *argv[])
  222. {
  223. int ret = -1;
  224. QCoreApplication::addLibraryPath(".");
  225. base_set_log_handler(do_log);
  226. try {
  227. OBSApp program(argc, argv);
  228. ret = program.exec();
  229. } catch (const char *error) {
  230. blog(LOG_ERROR, "%s", error);
  231. }
  232. blog(LOG_INFO, "Number of memory leaks: %llu", bnum_allocs());
  233. return ret;
  234. }