obs-app.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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 <time.h>
  15. #include <stdio.h>
  16. #include <wchar.h>
  17. #include <chrono>
  18. #include <ratio>
  19. #include <sstream>
  20. #include <mutex>
  21. #include <util/bmem.h>
  22. #include <util/dstr.h>
  23. #include <util/platform.h>
  24. #include <util/profiler.hpp>
  25. #include <obs-config.h>
  26. #include <obs.hpp>
  27. #include <QProxyStyle>
  28. #include "qt-wrappers.hpp"
  29. #include "obs-app.hpp"
  30. #include "window-basic-main.hpp"
  31. #include "window-basic-settings.hpp"
  32. #include "window-license-agreement.hpp"
  33. #include "crash-report.hpp"
  34. #include "platform.hpp"
  35. #include <fstream>
  36. #include <curl/curl.h>
  37. #ifdef _WIN32
  38. #include <windows.h>
  39. #else
  40. #include <signal.h>
  41. #endif
  42. using namespace std;
  43. static log_handler_t def_log_handler;
  44. static string currentLogFile;
  45. static string lastLogFile;
  46. static bool portable_mode = false;
  47. QObject *CreateShortcutFilter()
  48. {
  49. return new OBSEventFilter([](QObject *obj, QEvent *event)
  50. {
  51. auto mouse_event = [](QMouseEvent &event)
  52. {
  53. obs_key_combination_t hotkey = {0, OBS_KEY_NONE};
  54. bool pressed = event.type() == QEvent::MouseButtonPress;
  55. switch (event.button()) {
  56. case Qt::NoButton:
  57. case Qt::LeftButton:
  58. case Qt::RightButton:
  59. case Qt::AllButtons:
  60. case Qt::MouseButtonMask:
  61. return false;
  62. case Qt::MidButton:
  63. hotkey.key = OBS_KEY_MOUSE3;
  64. break;
  65. #define MAP_BUTTON(i, j) case Qt::ExtraButton ## i: \
  66. hotkey.key = OBS_KEY_MOUSE ## j; break;
  67. MAP_BUTTON( 1, 4);
  68. MAP_BUTTON( 2, 5);
  69. MAP_BUTTON( 3, 6);
  70. MAP_BUTTON( 4, 7);
  71. MAP_BUTTON( 5, 8);
  72. MAP_BUTTON( 6, 9);
  73. MAP_BUTTON( 7, 10);
  74. MAP_BUTTON( 8, 11);
  75. MAP_BUTTON( 9, 12);
  76. MAP_BUTTON(10, 13);
  77. MAP_BUTTON(11, 14);
  78. MAP_BUTTON(12, 15);
  79. MAP_BUTTON(13, 16);
  80. MAP_BUTTON(14, 17);
  81. MAP_BUTTON(15, 18);
  82. MAP_BUTTON(16, 19);
  83. MAP_BUTTON(17, 20);
  84. MAP_BUTTON(18, 21);
  85. MAP_BUTTON(19, 22);
  86. MAP_BUTTON(20, 23);
  87. MAP_BUTTON(21, 24);
  88. MAP_BUTTON(22, 25);
  89. MAP_BUTTON(23, 26);
  90. MAP_BUTTON(24, 27);
  91. #undef MAP_BUTTON
  92. }
  93. hotkey.modifiers = TranslateQtKeyboardEventModifiers(
  94. event.modifiers());
  95. obs_hotkey_inject_event(hotkey, pressed);
  96. return true;
  97. };
  98. auto key_event = [&](QKeyEvent *event)
  99. {
  100. QDialog *dialog = qobject_cast<QDialog*>(obj);
  101. obs_key_combination_t hotkey = {0, OBS_KEY_NONE};
  102. bool pressed = event->type() == QEvent::KeyPress;
  103. switch (event->key()) {
  104. case Qt::Key_Shift:
  105. case Qt::Key_Control:
  106. case Qt::Key_Alt:
  107. case Qt::Key_Meta:
  108. break;
  109. #ifdef __APPLE__
  110. case Qt::Key_CapsLock:
  111. // kVK_CapsLock == 57
  112. hotkey.key = obs_key_from_virtual_key(57);
  113. pressed = true;
  114. break;
  115. #endif
  116. case Qt::Key_Enter:
  117. case Qt::Key_Escape:
  118. case Qt::Key_Return:
  119. if (dialog && pressed)
  120. return false;
  121. default:
  122. hotkey.key = obs_key_from_virtual_key(
  123. event->nativeVirtualKey());
  124. }
  125. hotkey.modifiers = TranslateQtKeyboardEventModifiers(
  126. event->modifiers());
  127. obs_hotkey_inject_event(hotkey, pressed);
  128. return true;
  129. };
  130. switch (event->type()) {
  131. case QEvent::MouseButtonPress:
  132. case QEvent::MouseButtonRelease:
  133. return mouse_event(*static_cast<QMouseEvent*>(event));
  134. /*case QEvent::MouseButtonDblClick:
  135. case QEvent::Wheel:*/
  136. case QEvent::KeyPress:
  137. case QEvent::KeyRelease:
  138. return key_event(static_cast<QKeyEvent*>(event));
  139. default:
  140. return false;
  141. }
  142. });
  143. }
  144. string CurrentTimeString()
  145. {
  146. using namespace std::chrono;
  147. struct tm tstruct;
  148. char buf[80];
  149. auto tp = system_clock::now();
  150. auto now = system_clock::to_time_t(tp);
  151. tstruct = *localtime(&now);
  152. size_t written = strftime(buf, sizeof(buf), "%X", &tstruct);
  153. if (ratio_less<system_clock::period, seconds::period>::value &&
  154. written && (sizeof(buf) - written) > 5) {
  155. auto tp_secs =
  156. time_point_cast<seconds>(tp);
  157. auto millis =
  158. duration_cast<milliseconds>(tp - tp_secs).count();
  159. snprintf(buf + written, sizeof(buf) - written, ".%03u",
  160. static_cast<unsigned>(millis));
  161. }
  162. return buf;
  163. }
  164. string CurrentDateTimeString()
  165. {
  166. time_t now = time(0);
  167. struct tm tstruct;
  168. char buf[80];
  169. tstruct = *localtime(&now);
  170. strftime(buf, sizeof(buf), "%Y-%m-%d, %X", &tstruct);
  171. return buf;
  172. }
  173. static inline void LogString(fstream &logFile, const char *timeString,
  174. char *str)
  175. {
  176. logFile << timeString << str << endl;
  177. }
  178. static inline void LogStringChunk(fstream &logFile, char *str)
  179. {
  180. char *nextLine = str;
  181. string timeString = CurrentTimeString();
  182. timeString += ": ";
  183. while (*nextLine) {
  184. char *nextLine = strchr(str, '\n');
  185. if (!nextLine)
  186. break;
  187. if (nextLine != str && nextLine[-1] == '\r') {
  188. nextLine[-1] = 0;
  189. } else {
  190. nextLine[0] = 0;
  191. }
  192. LogString(logFile, timeString.c_str(), str);
  193. nextLine++;
  194. str = nextLine;
  195. }
  196. LogString(logFile, timeString.c_str(), str);
  197. }
  198. #define MAX_REPEATED_LINES 30
  199. #define MAX_CHAR_VARIATION (255 * 3)
  200. static inline int sum_chars(const char *str)
  201. {
  202. int val = 0;
  203. for (; *str != 0; str++)
  204. val += *str;
  205. return val;
  206. }
  207. static inline bool too_many_repeated_entries(fstream &logFile, const char *msg,
  208. const char *output_str)
  209. {
  210. static mutex log_mutex;
  211. static const char *last_msg_ptr = nullptr;
  212. static int last_char_sum = 0;
  213. static char cmp_str[4096];
  214. static int rep_count = 0;
  215. int new_sum = sum_chars(output_str);
  216. lock_guard<mutex> guard(log_mutex);
  217. if (last_msg_ptr == msg) {
  218. int diff = std::abs(new_sum - last_char_sum);
  219. if (diff < MAX_CHAR_VARIATION) {
  220. return (rep_count++ >= MAX_REPEATED_LINES);
  221. }
  222. }
  223. if (rep_count > MAX_REPEATED_LINES) {
  224. logFile << CurrentTimeString() <<
  225. ": Last log entry repeated for " <<
  226. to_string(rep_count - MAX_REPEATED_LINES) <<
  227. " more lines" << endl;
  228. }
  229. last_msg_ptr = msg;
  230. strcpy(cmp_str, output_str);
  231. last_char_sum = new_sum;
  232. rep_count = 0;
  233. return false;
  234. }
  235. static void do_log(int log_level, const char *msg, va_list args, void *param)
  236. {
  237. fstream &logFile = *static_cast<fstream*>(param);
  238. char str[4096];
  239. #ifndef _WIN32
  240. va_list args2;
  241. va_copy(args2, args);
  242. #endif
  243. vsnprintf(str, 4095, msg, args);
  244. #ifdef _WIN32
  245. OutputDebugStringA(str);
  246. OutputDebugStringA("\n");
  247. #else
  248. def_log_handler(log_level, msg, args2, nullptr);
  249. #endif
  250. if (too_many_repeated_entries(logFile, msg, str))
  251. return;
  252. if (log_level <= LOG_INFO)
  253. LogStringChunk(logFile, str);
  254. #ifdef _WIN32
  255. if (log_level <= LOG_ERROR && IsDebuggerPresent())
  256. __debugbreak();
  257. #endif
  258. }
  259. #define DEFAULT_LANG "en-US"
  260. bool OBSApp::InitGlobalConfigDefaults()
  261. {
  262. config_set_default_string(globalConfig, "General", "Language",
  263. DEFAULT_LANG);
  264. config_set_default_uint(globalConfig, "General", "MaxLogs", 10);
  265. #if _WIN32
  266. config_set_default_string(globalConfig, "Video", "Renderer",
  267. "Direct3D 11");
  268. #else
  269. config_set_default_string(globalConfig, "Video", "Renderer", "OpenGL");
  270. #endif
  271. config_set_default_bool(globalConfig, "BasicWindow", "PreviewEnabled",
  272. true);
  273. #ifdef __APPLE__
  274. config_set_default_bool(globalConfig, "Video", "DisableOSXVSync", true);
  275. config_set_default_bool(globalConfig, "Video", "ResetOSXVSyncOnExit",
  276. true);
  277. #endif
  278. return true;
  279. }
  280. static bool do_mkdir(const char *path)
  281. {
  282. if (os_mkdirs(path) == MKDIR_ERROR) {
  283. OBSErrorBox(NULL, "Failed to create directory %s", path);
  284. return false;
  285. }
  286. return true;
  287. }
  288. static bool MakeUserDirs()
  289. {
  290. char path[512];
  291. if (GetConfigPath(path, sizeof(path), "obs-studio/basic") <= 0)
  292. return false;
  293. if (!do_mkdir(path))
  294. return false;
  295. if (GetConfigPath(path, sizeof(path), "obs-studio/logs") <= 0)
  296. return false;
  297. if (!do_mkdir(path))
  298. return false;
  299. if (GetConfigPath(path, sizeof(path), "obs-studio/profiler_data") <= 0)
  300. return false;
  301. if (!do_mkdir(path))
  302. return false;
  303. #ifdef _WIN32
  304. if (GetConfigPath(path, sizeof(path), "obs-studio/crashes") <= 0)
  305. return false;
  306. if (!do_mkdir(path))
  307. return false;
  308. #endif
  309. if (GetConfigPath(path, sizeof(path), "obs-studio/plugin_config") <= 0)
  310. return false;
  311. if (!do_mkdir(path))
  312. return false;
  313. return true;
  314. }
  315. static bool MakeUserProfileDirs()
  316. {
  317. char path[512];
  318. if (GetConfigPath(path, sizeof(path), "obs-studio/basic/profiles") <= 0)
  319. return false;
  320. if (!do_mkdir(path))
  321. return false;
  322. if (GetConfigPath(path, sizeof(path), "obs-studio/basic/scenes") <= 0)
  323. return false;
  324. if (!do_mkdir(path))
  325. return false;
  326. return true;
  327. }
  328. bool OBSApp::InitGlobalConfig()
  329. {
  330. char path[512];
  331. int len = GetConfigPath(path, sizeof(path),
  332. "obs-studio/global.ini");
  333. if (len <= 0) {
  334. return false;
  335. }
  336. int errorcode = globalConfig.Open(path, CONFIG_OPEN_ALWAYS);
  337. if (errorcode != CONFIG_SUCCESS) {
  338. OBSErrorBox(NULL, "Failed to open global.ini: %d", errorcode);
  339. return false;
  340. }
  341. return InitGlobalConfigDefaults();
  342. }
  343. bool OBSApp::InitLocale()
  344. {
  345. ProfileScope("OBSApp::InitLocale");
  346. const char *lang = config_get_string(globalConfig, "General",
  347. "Language");
  348. locale = lang;
  349. string englishPath;
  350. if (!GetDataFilePath("locale/" DEFAULT_LANG ".ini", englishPath)) {
  351. OBSErrorBox(NULL, "Failed to find locale/" DEFAULT_LANG ".ini");
  352. return false;
  353. }
  354. textLookup = text_lookup_create(englishPath.c_str());
  355. if (!textLookup) {
  356. OBSErrorBox(NULL, "Failed to create locale from file '%s'",
  357. englishPath.c_str());
  358. return false;
  359. }
  360. bool userLocale = config_has_user_value(globalConfig, "General",
  361. "Language");
  362. bool defaultLang = astrcmpi(lang, DEFAULT_LANG) == 0;
  363. if (userLocale && defaultLang)
  364. return true;
  365. if (!userLocale && defaultLang) {
  366. for (auto &locale_ : GetPreferredLocales()) {
  367. if (locale_ == lang)
  368. return true;
  369. stringstream file;
  370. file << "locale/" << locale_ << ".ini";
  371. string path;
  372. if (!GetDataFilePath(file.str().c_str(), path))
  373. continue;
  374. if (!text_lookup_add(textLookup, path.c_str()))
  375. continue;
  376. blog(LOG_INFO, "Using preferred locale '%s'",
  377. locale_.c_str());
  378. locale = locale_;
  379. return true;
  380. }
  381. return true;
  382. }
  383. stringstream file;
  384. file << "locale/" << lang << ".ini";
  385. string path;
  386. if (GetDataFilePath(file.str().c_str(), path)) {
  387. if (!text_lookup_add(textLookup, path.c_str()))
  388. blog(LOG_ERROR, "Failed to add locale file '%s'",
  389. path.c_str());
  390. } else {
  391. blog(LOG_ERROR, "Could not find locale file '%s'",
  392. file.str().c_str());
  393. }
  394. return true;
  395. }
  396. bool OBSApp::SetTheme(std::string name, std::string path)
  397. {
  398. theme = name;
  399. /* Check user dir first, then preinstalled themes. */
  400. if (path == "") {
  401. char userDir[512];
  402. name = "themes/" + name + ".qss";
  403. string temp = "obs-studio/" + name;
  404. int ret = GetConfigPath(userDir, sizeof(userDir),
  405. temp.c_str());
  406. if (ret > 0 && QFile::exists(userDir)) {
  407. path = string(userDir);
  408. } else if (!GetDataFilePath(name.c_str(), path)) {
  409. OBSErrorBox(NULL, "Failed to find %s.", name.c_str());
  410. return false;
  411. }
  412. }
  413. QString mpath = QString("file:///") + path.c_str();
  414. setStyleSheet(mpath);
  415. return true;
  416. }
  417. bool OBSApp::InitTheme()
  418. {
  419. const char *themeName = config_get_string(globalConfig, "General",
  420. "Theme");
  421. if (!themeName)
  422. themeName = "Default";
  423. stringstream t;
  424. t << themeName;
  425. return SetTheme(t.str());
  426. }
  427. OBSApp::OBSApp(int &argc, char **argv, profiler_name_store_t *store)
  428. : QApplication(argc, argv),
  429. profilerNameStore(store)
  430. {
  431. sleepInhibitor = os_inhibit_sleep_create("OBS Video/audio");
  432. }
  433. OBSApp::~OBSApp()
  434. {
  435. #ifdef __APPLE__
  436. bool vsyncDiabled = config_get_bool(globalConfig, "Video",
  437. "DisableOSXVSync");
  438. bool resetVSync = config_get_bool(globalConfig, "Video",
  439. "ResetOSXVSyncOnExit");
  440. if (vsyncDiabled && resetVSync)
  441. EnableOSXVSync(true);
  442. #endif
  443. os_inhibit_sleep_set_active(sleepInhibitor, false);
  444. os_inhibit_sleep_destroy(sleepInhibitor);
  445. }
  446. static void move_basic_to_profiles(void)
  447. {
  448. char path[512];
  449. char new_path[512];
  450. os_glob_t *glob;
  451. /* if not first time use */
  452. if (GetConfigPath(path, 512, "obs-studio/basic") <= 0)
  453. return;
  454. if (!os_file_exists(path))
  455. return;
  456. /* if the profiles directory doesn't already exist */
  457. if (GetConfigPath(new_path, 512, "obs-studio/basic/profiles") <= 0)
  458. return;
  459. if (os_file_exists(new_path))
  460. return;
  461. if (os_mkdir(new_path) == MKDIR_ERROR)
  462. return;
  463. strcat(new_path, "/");
  464. strcat(new_path, Str("Untitled"));
  465. if (os_mkdir(new_path) == MKDIR_ERROR)
  466. return;
  467. strcat(path, "/*.*");
  468. if (os_glob(path, 0, &glob) != 0)
  469. return;
  470. strcpy(path, new_path);
  471. for (size_t i = 0; i < glob->gl_pathc; i++) {
  472. struct os_globent ent = glob->gl_pathv[i];
  473. char *file;
  474. if (ent.directory)
  475. continue;
  476. file = strrchr(ent.path, '/');
  477. if (!file++)
  478. continue;
  479. if (astrcmpi(file, "scenes.json") == 0)
  480. continue;
  481. strcpy(new_path, path);
  482. strcat(new_path, "/");
  483. strcat(new_path, file);
  484. os_rename(ent.path, new_path);
  485. }
  486. os_globfree(glob);
  487. }
  488. static void move_basic_to_scene_collections(void)
  489. {
  490. char path[512];
  491. char new_path[512];
  492. if (GetConfigPath(path, 512, "obs-studio/basic") <= 0)
  493. return;
  494. if (!os_file_exists(path))
  495. return;
  496. if (GetConfigPath(new_path, 512, "obs-studio/basic/scenes") <= 0)
  497. return;
  498. if (os_file_exists(new_path))
  499. return;
  500. if (os_mkdir(new_path) == MKDIR_ERROR)
  501. return;
  502. strcat(path, "/scenes.json");
  503. strcat(new_path, "/");
  504. strcat(new_path, Str("Untitled"));
  505. strcat(new_path, ".json");
  506. os_rename(path, new_path);
  507. }
  508. void OBSApp::AppInit()
  509. {
  510. ProfileScope("OBSApp::AppInit");
  511. if (!InitApplicationBundle())
  512. throw "Failed to initialize application bundle";
  513. if (!MakeUserDirs())
  514. throw "Failed to create required user directories";
  515. if (!InitGlobalConfig())
  516. throw "Failed to initialize global config";
  517. if (!InitLocale())
  518. throw "Failed to load locale";
  519. if (!InitTheme())
  520. throw "Failed to load theme";
  521. config_set_default_string(globalConfig, "Basic", "Profile",
  522. Str("Untitled"));
  523. config_set_default_string(globalConfig, "Basic", "ProfileDir",
  524. Str("Untitled"));
  525. config_set_default_string(globalConfig, "Basic", "SceneCollection",
  526. Str("Untitled"));
  527. config_set_default_string(globalConfig, "Basic", "SceneCollectionFile",
  528. Str("Untitled"));
  529. #ifdef __APPLE__
  530. if (config_get_bool(globalConfig, "Video", "DisableOSXVSync"))
  531. EnableOSXVSync(false);
  532. #endif
  533. move_basic_to_profiles();
  534. move_basic_to_scene_collections();
  535. if (!MakeUserProfileDirs())
  536. throw "Failed to create profile directories";
  537. }
  538. const char *OBSApp::GetRenderModule() const
  539. {
  540. const char *renderer = config_get_string(globalConfig, "Video",
  541. "Renderer");
  542. return (astrcmpi(renderer, "Direct3D 11") == 0) ?
  543. DL_D3D11 : DL_OPENGL;
  544. }
  545. static bool StartupOBS(const char *locale, profiler_name_store_t *store)
  546. {
  547. char path[512];
  548. if (GetConfigPath(path, sizeof(path), "obs-studio/plugin_config") <= 0)
  549. return false;
  550. return obs_startup(locale, path, store);
  551. }
  552. bool OBSApp::OBSInit()
  553. {
  554. ProfileScope("OBSApp::OBSInit");
  555. bool licenseAccepted = config_get_bool(globalConfig, "General",
  556. "LicenseAccepted");
  557. OBSLicenseAgreement agreement(nullptr);
  558. if (licenseAccepted || agreement.exec() == QDialog::Accepted) {
  559. if (!licenseAccepted) {
  560. config_set_bool(globalConfig, "General",
  561. "LicenseAccepted", true);
  562. config_save(globalConfig);
  563. }
  564. if (!StartupOBS(locale.c_str(), GetProfilerNameStore()))
  565. return false;
  566. mainWindow = new OBSBasic();
  567. mainWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  568. connect(mainWindow, SIGNAL(destroyed()), this, SLOT(quit()));
  569. mainWindow->OBSInit();
  570. connect(this, &QGuiApplication::applicationStateChanged,
  571. [](Qt::ApplicationState state)
  572. {
  573. obs_hotkey_enable_background_press(
  574. state != Qt::ApplicationActive);
  575. });
  576. obs_hotkey_enable_background_press(
  577. applicationState() != Qt::ApplicationActive);
  578. return true;
  579. } else {
  580. return false;
  581. }
  582. }
  583. string OBSApp::GetVersionString() const
  584. {
  585. stringstream ver;
  586. #ifdef HAVE_OBSCONFIG_H
  587. ver << OBS_VERSION;
  588. #else
  589. ver << LIBOBS_API_MAJOR_VER << "." <<
  590. LIBOBS_API_MINOR_VER << "." <<
  591. LIBOBS_API_PATCH_VER;
  592. #endif
  593. ver << " (";
  594. #ifdef _WIN32
  595. if (sizeof(void*) == 8)
  596. ver << "64bit, ";
  597. ver << "windows)";
  598. #elif __APPLE__
  599. ver << "mac)";
  600. #elif __FreeBSD__
  601. ver << "freebsd)";
  602. #else /* assume linux for the time being */
  603. ver << "linux)";
  604. #endif
  605. return ver.str();
  606. }
  607. #ifdef __APPLE__
  608. #define INPUT_AUDIO_SOURCE "coreaudio_input_capture"
  609. #define OUTPUT_AUDIO_SOURCE "coreaudio_output_capture"
  610. #elif _WIN32
  611. #define INPUT_AUDIO_SOURCE "wasapi_input_capture"
  612. #define OUTPUT_AUDIO_SOURCE "wasapi_output_capture"
  613. #else
  614. #define INPUT_AUDIO_SOURCE "pulse_input_capture"
  615. #define OUTPUT_AUDIO_SOURCE "pulse_output_capture"
  616. #endif
  617. const char *OBSApp::InputAudioSource() const
  618. {
  619. return INPUT_AUDIO_SOURCE;
  620. }
  621. const char *OBSApp::OutputAudioSource() const
  622. {
  623. return OUTPUT_AUDIO_SOURCE;
  624. }
  625. const char *OBSApp::GetLastLog() const
  626. {
  627. return lastLogFile.c_str();
  628. }
  629. const char *OBSApp::GetCurrentLog() const
  630. {
  631. return currentLogFile.c_str();
  632. }
  633. QString OBSTranslator::translate(const char *context, const char *sourceText,
  634. const char *disambiguation, int n) const
  635. {
  636. const char *out = nullptr;
  637. if (!text_lookup_getstr(App()->GetTextLookup(), sourceText, &out))
  638. return QString();
  639. UNUSED_PARAMETER(context);
  640. UNUSED_PARAMETER(disambiguation);
  641. UNUSED_PARAMETER(n);
  642. return QT_UTF8(out);
  643. }
  644. static bool get_token(lexer *lex, string &str, base_token_type type)
  645. {
  646. base_token token;
  647. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  648. return false;
  649. if (token.type != type)
  650. return false;
  651. str.assign(token.text.array, token.text.len);
  652. return true;
  653. }
  654. static bool expect_token(lexer *lex, const char *str, base_token_type type)
  655. {
  656. base_token token;
  657. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  658. return false;
  659. if (token.type != type)
  660. return false;
  661. return strref_cmp(&token.text, str) == 0;
  662. }
  663. static uint64_t convert_log_name(const char *name)
  664. {
  665. BaseLexer lex;
  666. string year, month, day, hour, minute, second;
  667. lexer_start(lex, name);
  668. if (!get_token(lex, year, BASETOKEN_DIGIT)) return 0;
  669. if (!expect_token(lex, "-", BASETOKEN_OTHER)) return 0;
  670. if (!get_token(lex, month, BASETOKEN_DIGIT)) return 0;
  671. if (!expect_token(lex, "-", BASETOKEN_OTHER)) return 0;
  672. if (!get_token(lex, day, BASETOKEN_DIGIT)) return 0;
  673. if (!get_token(lex, hour, BASETOKEN_DIGIT)) return 0;
  674. if (!expect_token(lex, "-", BASETOKEN_OTHER)) return 0;
  675. if (!get_token(lex, minute, BASETOKEN_DIGIT)) return 0;
  676. if (!expect_token(lex, "-", BASETOKEN_OTHER)) return 0;
  677. if (!get_token(lex, second, BASETOKEN_DIGIT)) return 0;
  678. stringstream timestring;
  679. timestring << year << month << day << hour << minute << second;
  680. return std::stoull(timestring.str());
  681. }
  682. static void delete_oldest_file(const char *location)
  683. {
  684. BPtr<char> logDir(GetConfigPathPtr(location));
  685. string oldestLog;
  686. uint64_t oldest_ts = (uint64_t)-1;
  687. struct os_dirent *entry;
  688. unsigned int maxLogs = (unsigned int)config_get_uint(
  689. App()->GlobalConfig(), "General", "MaxLogs");
  690. os_dir_t *dir = os_opendir(logDir);
  691. if (dir) {
  692. unsigned int count = 0;
  693. while ((entry = os_readdir(dir)) != NULL) {
  694. if (entry->directory || *entry->d_name == '.')
  695. continue;
  696. uint64_t ts = convert_log_name(entry->d_name);
  697. if (ts) {
  698. if (ts < oldest_ts) {
  699. oldestLog = entry->d_name;
  700. oldest_ts = ts;
  701. }
  702. count++;
  703. }
  704. }
  705. os_closedir(dir);
  706. if (count > maxLogs) {
  707. stringstream delPath;
  708. delPath << logDir << "/" << oldestLog;
  709. os_unlink(delPath.str().c_str());
  710. }
  711. }
  712. }
  713. static void get_last_log(void)
  714. {
  715. BPtr<char> logDir(GetConfigPathPtr("obs-studio/logs"));
  716. struct os_dirent *entry;
  717. os_dir_t *dir = os_opendir(logDir);
  718. uint64_t highest_ts = 0;
  719. if (dir) {
  720. while ((entry = os_readdir(dir)) != NULL) {
  721. if (entry->directory || *entry->d_name == '.')
  722. continue;
  723. uint64_t ts = convert_log_name(entry->d_name);
  724. if (ts > highest_ts) {
  725. lastLogFile = entry->d_name;
  726. highest_ts = ts;
  727. }
  728. }
  729. os_closedir(dir);
  730. }
  731. }
  732. string GenerateTimeDateFilename(const char *extension, bool noSpace)
  733. {
  734. time_t now = time(0);
  735. char file[256] = {};
  736. struct tm *cur_time;
  737. cur_time = localtime(&now);
  738. snprintf(file, sizeof(file), "%d-%02d-%02d%c%02d-%02d-%02d.%s",
  739. cur_time->tm_year+1900,
  740. cur_time->tm_mon+1,
  741. cur_time->tm_mday,
  742. noSpace ? '_' : ' ',
  743. cur_time->tm_hour,
  744. cur_time->tm_min,
  745. cur_time->tm_sec,
  746. extension);
  747. return string(file);
  748. }
  749. vector<pair<string, string>> GetLocaleNames()
  750. {
  751. string path;
  752. if (!GetDataFilePath("locale.ini", path))
  753. throw "Could not find locale.ini path";
  754. ConfigFile ini;
  755. if (ini.Open(path.c_str(), CONFIG_OPEN_EXISTING) != 0)
  756. throw "Could not open locale.ini";
  757. size_t sections = config_num_sections(ini);
  758. vector<pair<string, string>> names;
  759. names.reserve(sections);
  760. for (size_t i = 0; i < sections; i++) {
  761. const char *tag = config_get_section(ini, i);
  762. const char *name = config_get_string(ini, tag, "Name");
  763. names.emplace_back(tag, name);
  764. }
  765. return names;
  766. }
  767. static void create_log_file(fstream &logFile)
  768. {
  769. stringstream dst;
  770. get_last_log();
  771. currentLogFile = GenerateTimeDateFilename("txt");
  772. dst << "obs-studio/logs/" << currentLogFile.c_str();
  773. BPtr<char> path(GetConfigPathPtr(dst.str().c_str()));
  774. logFile.open(path,
  775. ios_base::in | ios_base::out | ios_base::trunc);
  776. if (logFile.is_open()) {
  777. delete_oldest_file("obs-studio/logs");
  778. base_set_log_handler(do_log, &logFile);
  779. } else {
  780. blog(LOG_ERROR, "Failed to open log file");
  781. }
  782. }
  783. static auto ProfilerNameStoreRelease = [](profiler_name_store_t *store)
  784. {
  785. profiler_name_store_free(store);
  786. };
  787. using ProfilerNameStore =
  788. std::unique_ptr<profiler_name_store_t,
  789. decltype(ProfilerNameStoreRelease)>;
  790. ProfilerNameStore CreateNameStore()
  791. {
  792. return ProfilerNameStore{profiler_name_store_create(),
  793. ProfilerNameStoreRelease};
  794. }
  795. static auto SnapshotRelease = [](profiler_snapshot_t *snap)
  796. {
  797. profile_snapshot_free(snap);
  798. };
  799. using ProfilerSnapshot =
  800. std::unique_ptr<profiler_snapshot_t, decltype(SnapshotRelease)>;
  801. ProfilerSnapshot GetSnapshot()
  802. {
  803. return ProfilerSnapshot{profile_snapshot_create(), SnapshotRelease};
  804. }
  805. static void SaveProfilerData(const ProfilerSnapshot &snap)
  806. {
  807. if (currentLogFile.empty())
  808. return;
  809. auto pos = currentLogFile.rfind('.');
  810. if (pos == currentLogFile.npos)
  811. return;
  812. #define LITERAL_SIZE(x) x, (sizeof(x) - 1)
  813. ostringstream dst;
  814. dst.write(LITERAL_SIZE("obs-studio/profiler_data/"));
  815. dst.write(currentLogFile.c_str(), pos);
  816. dst.write(LITERAL_SIZE(".csv.gz"));
  817. #undef LITERAL_SIZE
  818. BPtr<char> path = GetConfigPathPtr(dst.str().c_str());
  819. if (!profiler_snapshot_dump_csv_gz(snap.get(), path))
  820. blog(LOG_WARNING, "Could not save profiler data to '%s'",
  821. static_cast<const char*>(path));
  822. }
  823. static auto ProfilerFree = [](void *)
  824. {
  825. profiler_stop();
  826. auto snap = GetSnapshot();
  827. profiler_print(snap.get());
  828. profiler_print_time_between_calls(snap.get());
  829. SaveProfilerData(snap);
  830. profiler_free();
  831. };
  832. static const char *run_program_init = "run_program_init";
  833. static int run_program(fstream &logFile, int argc, char *argv[])
  834. {
  835. int ret = -1;
  836. auto profilerNameStore = CreateNameStore();
  837. std::unique_ptr<void, decltype(ProfilerFree)>
  838. prof_release(static_cast<void*>(&ProfilerFree),
  839. ProfilerFree);
  840. profiler_start();
  841. profile_register_root(run_program_init, 0);
  842. auto PrintInitProfile = [&]()
  843. {
  844. auto snap = GetSnapshot();
  845. profiler_snapshot_filter_roots(snap.get(), [](void *data,
  846. const char *name, bool *remove)
  847. {
  848. *remove = (*static_cast<const char**>(data)) != name;
  849. return true;
  850. }, static_cast<void*>(&run_program_init));
  851. profiler_print(snap.get());
  852. };
  853. ScopeProfiler prof{run_program_init};
  854. QCoreApplication::addLibraryPath(".");
  855. OBSApp program(argc, argv, profilerNameStore.get());
  856. try {
  857. program.AppInit();
  858. OBSTranslator translator;
  859. create_log_file(logFile);
  860. delete_oldest_file("obs-studio/profiler_data");
  861. program.installTranslator(&translator);
  862. if (!program.OBSInit())
  863. return 0;
  864. prof.Stop();
  865. PrintInitProfile();
  866. return program.exec();
  867. } catch (const char *error) {
  868. blog(LOG_ERROR, "%s", error);
  869. OBSErrorBox(nullptr, "%s", error);
  870. }
  871. return ret;
  872. }
  873. #define MAX_CRASH_REPORT_SIZE (50 * 1024)
  874. #ifdef _WIN32
  875. #define CRASH_MESSAGE \
  876. "Woops, OBS has crashed!\n\nWould you like to copy the crash log " \
  877. "to the clipboard? (Crash logs will still be saved to the " \
  878. "%appdata%\\obs-studio\\crashes directory)"
  879. static void main_crash_handler(const char *format, va_list args, void *param)
  880. {
  881. char *text = new char[MAX_CRASH_REPORT_SIZE];
  882. vsnprintf(text, MAX_CRASH_REPORT_SIZE, format, args);
  883. delete_oldest_file("obs-studio/crashes");
  884. string name = "obs-studio/crashes/Crash ";
  885. name += GenerateTimeDateFilename("txt");
  886. BPtr<char> path(GetConfigPathPtr(name.c_str()));
  887. fstream file;
  888. file.open(path, ios_base::in | ios_base::out | ios_base::trunc);
  889. file << text;
  890. file.close();
  891. int ret = MessageBoxA(NULL, CRASH_MESSAGE, "OBS has crashed!",
  892. MB_YESNO | MB_ICONERROR | MB_TASKMODAL);
  893. if (ret == IDYES) {
  894. size_t len = strlen(text);
  895. HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, len);
  896. memcpy(GlobalLock(mem), text, len);
  897. GlobalUnlock(mem);
  898. OpenClipboard(0);
  899. EmptyClipboard();
  900. SetClipboardData(CF_TEXT, mem);
  901. CloseClipboard();
  902. }
  903. exit(-1);
  904. UNUSED_PARAMETER(param);
  905. }
  906. static void load_debug_privilege(void)
  907. {
  908. const DWORD flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY;
  909. TOKEN_PRIVILEGES tp;
  910. HANDLE token;
  911. LUID val;
  912. if (!OpenProcessToken(GetCurrentProcess(), flags, &token)) {
  913. return;
  914. }
  915. if (!!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &val)) {
  916. tp.PrivilegeCount = 1;
  917. tp.Privileges[0].Luid = val;
  918. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  919. AdjustTokenPrivileges(token, false, &tp,
  920. sizeof(tp), NULL, NULL);
  921. }
  922. CloseHandle(token);
  923. }
  924. #endif
  925. #ifdef __APPLE__
  926. #define BASE_PATH ".."
  927. #else
  928. #define BASE_PATH "../.."
  929. #endif
  930. #define CONFIG_PATH BASE_PATH "/config"
  931. #ifndef OBS_UNIX_STRUCTURE
  932. #define OBS_UNIX_STRUCTURE 0
  933. #endif
  934. int GetConfigPath(char *path, size_t size, const char *name)
  935. {
  936. if (!OBS_UNIX_STRUCTURE && portable_mode) {
  937. if (name && *name) {
  938. return snprintf(path, size, CONFIG_PATH "/%s", name);
  939. } else {
  940. return snprintf(path, size, CONFIG_PATH);
  941. }
  942. } else {
  943. return os_get_config_path(path, size, name);
  944. }
  945. }
  946. char *GetConfigPathPtr(const char *name)
  947. {
  948. if (!OBS_UNIX_STRUCTURE && portable_mode) {
  949. char path[512];
  950. if (snprintf(path, sizeof(path), CONFIG_PATH "/%s", name) > 0) {
  951. return bstrdup(path);
  952. } else {
  953. return NULL;
  954. }
  955. } else {
  956. return os_get_config_path_ptr(name);
  957. }
  958. }
  959. bool GetFileSafeName(const char *name, std::string &file)
  960. {
  961. size_t base_len = strlen(name);
  962. size_t len = os_utf8_to_wcs(name, base_len, nullptr, 0);
  963. std::wstring wfile;
  964. if (!len)
  965. return false;
  966. wfile.resize(len);
  967. os_utf8_to_wcs(name, base_len, &wfile[0], len);
  968. for (size_t i = wfile.size(); i > 0; i--) {
  969. size_t im1 = i - 1;
  970. if (iswspace(wfile[im1])) {
  971. wfile[im1] = '_';
  972. } else if (wfile[im1] != '_' && !iswalnum(wfile[im1])) {
  973. wfile.erase(im1, 1);
  974. }
  975. }
  976. if (wfile.size() == 0)
  977. wfile = L"characters_only";
  978. len = os_wcs_to_utf8(wfile.c_str(), wfile.size(), nullptr, 0);
  979. if (!len)
  980. return false;
  981. file.resize(len);
  982. os_wcs_to_utf8(wfile.c_str(), wfile.size(), &file[0], len);
  983. return true;
  984. }
  985. bool GetClosestUnusedFileName(std::string &path, const char *extension)
  986. {
  987. size_t len = path.size();
  988. if (extension) {
  989. path += ".";
  990. path += extension;
  991. }
  992. if (!os_file_exists(path.c_str()))
  993. return true;
  994. int index = 1;
  995. do {
  996. path.resize(len);
  997. path += std::to_string(++index);
  998. if (extension) {
  999. path += ".";
  1000. path += extension;
  1001. }
  1002. } while (os_file_exists(path.c_str()));
  1003. return true;
  1004. }
  1005. static inline bool arg_is(const char *arg,
  1006. const char *long_form, const char *short_form)
  1007. {
  1008. return (long_form && strcmp(arg, long_form) == 0) ||
  1009. (short_form && strcmp(arg, short_form) == 0);
  1010. }
  1011. #if !defined(_WIN32) && !defined(__APPLE__)
  1012. #define IS_UNIX 1
  1013. #endif
  1014. /* if using XDG and was previously using an older build of OBS, move config
  1015. * files to XDG directory */
  1016. #if defined(USE_XDG) && defined(IS_UNIX)
  1017. static void move_to_xdg(void)
  1018. {
  1019. char old_path[512];
  1020. char new_path[512];
  1021. char *home = getenv("HOME");
  1022. if (!home)
  1023. return;
  1024. if (snprintf(old_path, 512, "%s/.obs-studio", home) <= 0)
  1025. return;
  1026. /* make base xdg path if it doesn't already exist */
  1027. if (GetConfigPath(new_path, 512, "") <= 0)
  1028. return;
  1029. if (os_mkdirs(new_path) == MKDIR_ERROR)
  1030. return;
  1031. if (GetConfigPath(new_path, 512, "obs-studio") <= 0)
  1032. return;
  1033. if (os_file_exists(old_path) && !os_file_exists(new_path)) {
  1034. rename(old_path, new_path);
  1035. }
  1036. }
  1037. #endif
  1038. static bool update_ffmpeg_output(ConfigFile &config)
  1039. {
  1040. if (config_has_user_value(config, "AdvOut", "FFOutputToFile"))
  1041. return false;
  1042. const char *url = config_get_string(config, "AdvOut", "FFURL");
  1043. if (!url)
  1044. return false;
  1045. bool isActualURL = strstr(url, "://") != nullptr;
  1046. if (isActualURL)
  1047. return false;
  1048. string urlStr = url;
  1049. string extension;
  1050. for (size_t i = urlStr.length(); i > 0; i--) {
  1051. size_t idx = i - 1;
  1052. if (urlStr[idx] == '.') {
  1053. extension = &urlStr[i];
  1054. }
  1055. if (urlStr[idx] == '\\' || urlStr[idx] == '/') {
  1056. urlStr[idx] = 0;
  1057. break;
  1058. }
  1059. }
  1060. if (urlStr.empty() || extension.empty())
  1061. return false;
  1062. config_remove_value(config, "AdvOut", "FFURL");
  1063. config_set_string(config, "AdvOut", "FFFilePath", urlStr.c_str());
  1064. config_set_string(config, "AdvOut", "FFExtension", extension.c_str());
  1065. config_set_bool(config, "AdvOut", "FFOutputToFile", true);
  1066. return true;
  1067. }
  1068. static bool move_reconnect_settings(ConfigFile &config, const char *sec)
  1069. {
  1070. bool changed = false;
  1071. if (config_has_user_value(config, sec, "Reconnect")) {
  1072. bool reconnect = config_get_bool(config, sec, "Reconnect");
  1073. config_set_bool(config, "Output", "Reconnect", reconnect);
  1074. changed = true;
  1075. }
  1076. if (config_has_user_value(config, sec, "RetryDelay")) {
  1077. int delay = (int)config_get_uint(config, sec, "RetryDelay");
  1078. config_set_uint(config, "Output", "RetryDelay", delay);
  1079. changed = true;
  1080. }
  1081. if (config_has_user_value(config, sec, "MaxRetries")) {
  1082. int retries = (int)config_get_uint(config, sec, "MaxRetries");
  1083. config_set_uint(config, "Output", "MaxRetries", retries);
  1084. changed = true;
  1085. }
  1086. return changed;
  1087. }
  1088. static bool update_reconnect(ConfigFile &config)
  1089. {
  1090. if (!config_has_user_value(config, "Output", "Mode"))
  1091. return false;
  1092. const char *mode = config_get_string(config, "Output", "Mode");
  1093. if (!mode)
  1094. return false;
  1095. const char *section = (strcmp(mode, "Advanced") == 0) ?
  1096. "AdvOut" : "SimpleOutput";
  1097. if (move_reconnect_settings(config, section)) {
  1098. config_remove_value(config, "SimpleOutput", "Reconnect");
  1099. config_remove_value(config, "SimpleOutput", "RetryDelay");
  1100. config_remove_value(config, "SimpleOutput", "MaxRetries");
  1101. config_remove_value(config, "AdvOut", "Reconnect");
  1102. config_remove_value(config, "AdvOut", "RetryDelay");
  1103. config_remove_value(config, "AdvOut", "MaxRetries");
  1104. return true;
  1105. }
  1106. return false;
  1107. }
  1108. static void upgrade_settings(void)
  1109. {
  1110. char path[512];
  1111. int pathlen = GetConfigPath(path, 512, "obs-studio/basic/profiles");
  1112. if (pathlen <= 0)
  1113. return;
  1114. if (!os_file_exists(path))
  1115. return;
  1116. os_dir_t *dir = os_opendir(path);
  1117. if (!dir)
  1118. return;
  1119. struct os_dirent *ent = os_readdir(dir);
  1120. while (ent) {
  1121. if (ent->directory) {
  1122. strcat(path, "/");
  1123. strcat(path, ent->d_name);
  1124. strcat(path, "/basic.ini");
  1125. ConfigFile config;
  1126. int ret;
  1127. ret = config.Open(path, CONFIG_OPEN_EXISTING);
  1128. if (ret == CONFIG_SUCCESS) {
  1129. if (update_ffmpeg_output(config) ||
  1130. update_reconnect(config)) {
  1131. config_save_safe(config, "tmp",
  1132. nullptr);
  1133. }
  1134. }
  1135. path[pathlen] = 0;
  1136. }
  1137. ent = os_readdir(dir);
  1138. }
  1139. os_closedir(dir);
  1140. }
  1141. int main(int argc, char *argv[])
  1142. {
  1143. #ifndef _WIN32
  1144. signal(SIGPIPE, SIG_IGN);
  1145. #endif
  1146. #ifdef _WIN32
  1147. load_debug_privilege();
  1148. base_set_crash_handler(main_crash_handler, nullptr);
  1149. #endif
  1150. base_get_log_handler(&def_log_handler, nullptr);
  1151. #if defined(USE_XDG) && defined(IS_UNIX)
  1152. move_to_xdg();
  1153. #endif
  1154. for (int i = 1; i < argc; i++) {
  1155. if (arg_is(argv[i], "--portable", "-p")) {
  1156. portable_mode = true;
  1157. }
  1158. }
  1159. #if !OBS_UNIX_STRUCTURE
  1160. if (!portable_mode) {
  1161. portable_mode =
  1162. os_file_exists(BASE_PATH "/portable_mode") ||
  1163. os_file_exists(BASE_PATH "/obs_portable_mode") ||
  1164. os_file_exists(BASE_PATH "/portable_mode.txt") ||
  1165. os_file_exists(BASE_PATH "/obs_portable_mode.txt");
  1166. }
  1167. #endif
  1168. upgrade_settings();
  1169. fstream logFile;
  1170. curl_global_init(CURL_GLOBAL_ALL);
  1171. int ret = run_program(logFile, argc, argv);
  1172. blog(LOG_INFO, "Number of memory leaks: %ld", bnum_allocs());
  1173. base_set_log_handler(nullptr, nullptr);
  1174. return ret;
  1175. }