obs-app.cpp 7.3 KB

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