obs-app.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678
  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 <string>
  20. #include <sstream>
  21. #include <mutex>
  22. #include <util/bmem.h>
  23. #include <util/dstr.hpp>
  24. #include <util/platform.h>
  25. #include <util/profiler.hpp>
  26. #include <util/cf-parser.h>
  27. #include <obs-config.h>
  28. #include <obs.hpp>
  29. #include <QGuiApplication>
  30. #include <QProxyStyle>
  31. #include <QScreen>
  32. #include <QProcess>
  33. #include "qt-wrappers.hpp"
  34. #include "obs-app.hpp"
  35. #include "log-viewer.hpp"
  36. #include "window-basic-main.hpp"
  37. #include "window-basic-settings.hpp"
  38. #include "crash-report.hpp"
  39. #include "platform.hpp"
  40. #include <fstream>
  41. #include <curl/curl.h>
  42. #ifdef _WIN32
  43. #include <windows.h>
  44. #include <filesystem>
  45. #else
  46. #include <signal.h>
  47. #include <pthread.h>
  48. #endif
  49. #include <iostream>
  50. #include "ui-config.h"
  51. using namespace std;
  52. static log_handler_t def_log_handler;
  53. static string currentLogFile;
  54. static string lastLogFile;
  55. static string lastCrashLogFile;
  56. bool portable_mode = false;
  57. static bool multi = false;
  58. static bool log_verbose = false;
  59. static bool unfiltered_log = false;
  60. bool opt_start_streaming = false;
  61. bool opt_start_recording = false;
  62. bool opt_studio_mode = false;
  63. bool opt_start_replaybuffer = false;
  64. bool opt_start_virtualcam = false;
  65. bool opt_minimize_tray = false;
  66. bool opt_allow_opengl = false;
  67. bool opt_always_on_top = false;
  68. bool opt_disable_updater = false;
  69. string opt_starting_collection;
  70. string opt_starting_profile;
  71. string opt_starting_scene;
  72. bool remuxAfterRecord = false;
  73. string remuxFilename;
  74. bool restart = false;
  75. QPointer<OBSLogViewer> obsLogViewer;
  76. // GPU hint exports for AMD/NVIDIA laptops
  77. #ifdef _MSC_VER
  78. extern "C" __declspec(dllexport) DWORD NvOptimusEnablement = 1;
  79. extern "C" __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
  80. #endif
  81. QObject *CreateShortcutFilter()
  82. {
  83. return new OBSEventFilter([](QObject *obj, QEvent *event) {
  84. auto mouse_event = [](QMouseEvent &event) {
  85. if (!App()->HotkeysEnabledInFocus() &&
  86. event.button() != Qt::LeftButton)
  87. return true;
  88. obs_key_combination_t hotkey = {0, OBS_KEY_NONE};
  89. bool pressed = event.type() == QEvent::MouseButtonPress;
  90. switch (event.button()) {
  91. case Qt::NoButton:
  92. case Qt::LeftButton:
  93. case Qt::RightButton:
  94. case Qt::AllButtons:
  95. case Qt::MouseButtonMask:
  96. return false;
  97. case Qt::MidButton:
  98. hotkey.key = OBS_KEY_MOUSE3;
  99. break;
  100. #define MAP_BUTTON(i, j) \
  101. case Qt::ExtraButton##i: \
  102. hotkey.key = OBS_KEY_MOUSE##j; \
  103. break;
  104. MAP_BUTTON(1, 4);
  105. MAP_BUTTON(2, 5);
  106. MAP_BUTTON(3, 6);
  107. MAP_BUTTON(4, 7);
  108. MAP_BUTTON(5, 8);
  109. MAP_BUTTON(6, 9);
  110. MAP_BUTTON(7, 10);
  111. MAP_BUTTON(8, 11);
  112. MAP_BUTTON(9, 12);
  113. MAP_BUTTON(10, 13);
  114. MAP_BUTTON(11, 14);
  115. MAP_BUTTON(12, 15);
  116. MAP_BUTTON(13, 16);
  117. MAP_BUTTON(14, 17);
  118. MAP_BUTTON(15, 18);
  119. MAP_BUTTON(16, 19);
  120. MAP_BUTTON(17, 20);
  121. MAP_BUTTON(18, 21);
  122. MAP_BUTTON(19, 22);
  123. MAP_BUTTON(20, 23);
  124. MAP_BUTTON(21, 24);
  125. MAP_BUTTON(22, 25);
  126. MAP_BUTTON(23, 26);
  127. MAP_BUTTON(24, 27);
  128. #undef MAP_BUTTON
  129. }
  130. hotkey.modifiers = TranslateQtKeyboardEventModifiers(
  131. event.modifiers());
  132. obs_hotkey_inject_event(hotkey, pressed);
  133. return true;
  134. };
  135. auto key_event = [&](QKeyEvent *event) {
  136. if (!App()->HotkeysEnabledInFocus())
  137. return true;
  138. QDialog *dialog = qobject_cast<QDialog *>(obj);
  139. obs_key_combination_t hotkey = {0, OBS_KEY_NONE};
  140. bool pressed = event->type() == QEvent::KeyPress;
  141. switch (event->key()) {
  142. case Qt::Key_Shift:
  143. case Qt::Key_Control:
  144. case Qt::Key_Alt:
  145. case Qt::Key_Meta:
  146. break;
  147. #ifdef __APPLE__
  148. case Qt::Key_CapsLock:
  149. // kVK_CapsLock == 57
  150. hotkey.key = obs_key_from_virtual_key(57);
  151. pressed = true;
  152. break;
  153. #endif
  154. case Qt::Key_Enter:
  155. case Qt::Key_Escape:
  156. case Qt::Key_Return:
  157. if (dialog && pressed)
  158. return false;
  159. /* Falls through. */
  160. default:
  161. hotkey.key = obs_key_from_virtual_key(
  162. event->nativeVirtualKey());
  163. }
  164. if (event->isAutoRepeat())
  165. return true;
  166. hotkey.modifiers = TranslateQtKeyboardEventModifiers(
  167. event->modifiers());
  168. obs_hotkey_inject_event(hotkey, pressed);
  169. return true;
  170. };
  171. switch (event->type()) {
  172. case QEvent::MouseButtonPress:
  173. case QEvent::MouseButtonRelease:
  174. return mouse_event(*static_cast<QMouseEvent *>(event));
  175. /*case QEvent::MouseButtonDblClick:
  176. case QEvent::Wheel:*/
  177. case QEvent::KeyPress:
  178. case QEvent::KeyRelease:
  179. return key_event(static_cast<QKeyEvent *>(event));
  180. default:
  181. return false;
  182. }
  183. });
  184. }
  185. string CurrentTimeString()
  186. {
  187. using namespace std::chrono;
  188. struct tm tstruct;
  189. char buf[80];
  190. auto tp = system_clock::now();
  191. auto now = system_clock::to_time_t(tp);
  192. tstruct = *localtime(&now);
  193. size_t written = strftime(buf, sizeof(buf), "%X", &tstruct);
  194. if (ratio_less<system_clock::period, seconds::period>::value &&
  195. written && (sizeof(buf) - written) > 5) {
  196. auto tp_secs = time_point_cast<seconds>(tp);
  197. auto millis = duration_cast<milliseconds>(tp - tp_secs).count();
  198. snprintf(buf + written, sizeof(buf) - written, ".%03u",
  199. static_cast<unsigned>(millis));
  200. }
  201. return buf;
  202. }
  203. string CurrentDateTimeString()
  204. {
  205. time_t now = time(0);
  206. struct tm tstruct;
  207. char buf[80];
  208. tstruct = *localtime(&now);
  209. strftime(buf, sizeof(buf), "%Y-%m-%d, %X", &tstruct);
  210. return buf;
  211. }
  212. static inline void LogString(fstream &logFile, const char *timeString,
  213. char *str, int log_level)
  214. {
  215. string msg;
  216. msg += timeString;
  217. msg += str;
  218. logFile << msg << endl;
  219. if (!!obsLogViewer)
  220. QMetaObject::invokeMethod(obsLogViewer.data(), "AddLine",
  221. Qt::QueuedConnection,
  222. Q_ARG(int, log_level),
  223. Q_ARG(QString, QString(msg.c_str())));
  224. }
  225. static inline void LogStringChunk(fstream &logFile, char *str, int log_level)
  226. {
  227. char *nextLine = str;
  228. string timeString = CurrentTimeString();
  229. timeString += ": ";
  230. while (*nextLine) {
  231. char *nextLine = strchr(str, '\n');
  232. if (!nextLine)
  233. break;
  234. if (nextLine != str && nextLine[-1] == '\r') {
  235. nextLine[-1] = 0;
  236. } else {
  237. nextLine[0] = 0;
  238. }
  239. LogString(logFile, timeString.c_str(), str, log_level);
  240. nextLine++;
  241. str = nextLine;
  242. }
  243. LogString(logFile, timeString.c_str(), str, log_level);
  244. }
  245. #define MAX_REPEATED_LINES 30
  246. #define MAX_CHAR_VARIATION (255 * 3)
  247. static inline int sum_chars(const char *str)
  248. {
  249. int val = 0;
  250. for (; *str != 0; str++)
  251. val += *str;
  252. return val;
  253. }
  254. static inline bool too_many_repeated_entries(fstream &logFile, const char *msg,
  255. const char *output_str)
  256. {
  257. static mutex log_mutex;
  258. static const char *last_msg_ptr = nullptr;
  259. static int last_char_sum = 0;
  260. static char cmp_str[4096];
  261. static int rep_count = 0;
  262. int new_sum = sum_chars(output_str);
  263. lock_guard<mutex> guard(log_mutex);
  264. if (unfiltered_log) {
  265. return false;
  266. }
  267. if (last_msg_ptr == msg) {
  268. int diff = std::abs(new_sum - last_char_sum);
  269. if (diff < MAX_CHAR_VARIATION) {
  270. return (rep_count++ >= MAX_REPEATED_LINES);
  271. }
  272. }
  273. if (rep_count > MAX_REPEATED_LINES) {
  274. logFile << CurrentTimeString()
  275. << ": Last log entry repeated for "
  276. << to_string(rep_count - MAX_REPEATED_LINES)
  277. << " more lines" << endl;
  278. }
  279. last_msg_ptr = msg;
  280. strcpy(cmp_str, output_str);
  281. last_char_sum = new_sum;
  282. rep_count = 0;
  283. return false;
  284. }
  285. static void do_log(int log_level, const char *msg, va_list args, void *param)
  286. {
  287. fstream &logFile = *static_cast<fstream *>(param);
  288. char str[4096];
  289. #ifndef _WIN32
  290. va_list args2;
  291. va_copy(args2, args);
  292. #endif
  293. vsnprintf(str, 4095, msg, args);
  294. #ifdef _WIN32
  295. if (IsDebuggerPresent()) {
  296. int wNum = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
  297. if (wNum > 1) {
  298. static wstring wide_buf;
  299. static mutex wide_mutex;
  300. lock_guard<mutex> lock(wide_mutex);
  301. wide_buf.reserve(wNum + 1);
  302. wide_buf.resize(wNum - 1);
  303. MultiByteToWideChar(CP_UTF8, 0, str, -1, &wide_buf[0],
  304. wNum);
  305. wide_buf.push_back('\n');
  306. OutputDebugStringW(wide_buf.c_str());
  307. }
  308. }
  309. #else
  310. def_log_handler(log_level, msg, args2, nullptr);
  311. va_end(args2);
  312. #endif
  313. if (log_level <= LOG_INFO || log_verbose) {
  314. if (too_many_repeated_entries(logFile, msg, str))
  315. return;
  316. LogStringChunk(logFile, str, log_level);
  317. }
  318. #if defined(_WIN32) && defined(OBS_DEBUGBREAK_ON_ERROR)
  319. if (log_level <= LOG_ERROR && IsDebuggerPresent())
  320. __debugbreak();
  321. #endif
  322. }
  323. #define DEFAULT_LANG "en-US"
  324. bool OBSApp::InitGlobalConfigDefaults()
  325. {
  326. config_set_default_string(globalConfig, "General", "Language",
  327. DEFAULT_LANG);
  328. config_set_default_uint(globalConfig, "General", "MaxLogs", 10);
  329. config_set_default_int(globalConfig, "General", "InfoIncrement", -1);
  330. config_set_default_string(globalConfig, "General", "ProcessPriority",
  331. "Normal");
  332. config_set_default_bool(globalConfig, "General", "EnableAutoUpdates",
  333. true);
  334. #if _WIN32
  335. config_set_default_string(globalConfig, "Video", "Renderer",
  336. "Direct3D 11");
  337. #else
  338. config_set_default_string(globalConfig, "Video", "Renderer", "OpenGL");
  339. #endif
  340. config_set_default_bool(globalConfig, "BasicWindow", "PreviewEnabled",
  341. true);
  342. config_set_default_bool(globalConfig, "BasicWindow",
  343. "PreviewProgramMode", false);
  344. config_set_default_bool(globalConfig, "BasicWindow",
  345. "SceneDuplicationMode", true);
  346. config_set_default_bool(globalConfig, "BasicWindow", "SwapScenesMode",
  347. true);
  348. config_set_default_bool(globalConfig, "BasicWindow", "SnappingEnabled",
  349. true);
  350. config_set_default_bool(globalConfig, "BasicWindow", "ScreenSnapping",
  351. true);
  352. config_set_default_bool(globalConfig, "BasicWindow", "SourceSnapping",
  353. true);
  354. config_set_default_bool(globalConfig, "BasicWindow", "CenterSnapping",
  355. false);
  356. config_set_default_double(globalConfig, "BasicWindow", "SnapDistance",
  357. 10.0);
  358. config_set_default_bool(globalConfig, "BasicWindow",
  359. "RecordWhenStreaming", false);
  360. config_set_default_bool(globalConfig, "BasicWindow",
  361. "KeepRecordingWhenStreamStops", false);
  362. config_set_default_bool(globalConfig, "BasicWindow", "SysTrayEnabled",
  363. true);
  364. config_set_default_bool(globalConfig, "BasicWindow",
  365. "SysTrayWhenStarted", false);
  366. config_set_default_bool(globalConfig, "BasicWindow", "SaveProjectors",
  367. false);
  368. config_set_default_bool(globalConfig, "BasicWindow", "ShowTransitions",
  369. true);
  370. config_set_default_bool(globalConfig, "BasicWindow",
  371. "ShowListboxToolbars", true);
  372. config_set_default_bool(globalConfig, "BasicWindow", "ShowStatusBar",
  373. true);
  374. config_set_default_bool(globalConfig, "BasicWindow", "ShowSourceIcons",
  375. true);
  376. config_set_default_bool(globalConfig, "BasicWindow", "StudioModeLabels",
  377. true);
  378. if (!config_get_bool(globalConfig, "General", "Pre21Defaults")) {
  379. config_set_default_string(globalConfig, "General",
  380. "CurrentTheme", DEFAULT_THEME);
  381. }
  382. config_set_default_string(globalConfig, "General", "HotkeyFocusType",
  383. "NeverDisableHotkeys");
  384. config_set_default_bool(globalConfig, "BasicWindow",
  385. "VerticalVolControl", false);
  386. config_set_default_bool(globalConfig, "BasicWindow",
  387. "MultiviewMouseSwitch", true);
  388. config_set_default_bool(globalConfig, "BasicWindow",
  389. "MultiviewDrawNames", true);
  390. config_set_default_bool(globalConfig, "BasicWindow",
  391. "MultiviewDrawAreas", true);
  392. #ifdef _WIN32
  393. uint32_t winver = GetWindowsVersion();
  394. config_set_default_bool(globalConfig, "Audio", "DisableAudioDucking",
  395. true);
  396. config_set_default_bool(globalConfig, "General", "BrowserHWAccel",
  397. winver > 0x601);
  398. #endif
  399. #ifdef __APPLE__
  400. config_set_default_bool(globalConfig, "Video", "DisableOSXVSync", true);
  401. config_set_default_bool(globalConfig, "Video", "ResetOSXVSyncOnExit",
  402. true);
  403. #endif
  404. config_set_default_bool(globalConfig, "BasicWindow",
  405. "MediaControlsCountdownTimer", true);
  406. return true;
  407. }
  408. static bool do_mkdir(const char *path)
  409. {
  410. if (os_mkdirs(path) == MKDIR_ERROR) {
  411. OBSErrorBox(NULL, "Failed to create directory %s", path);
  412. return false;
  413. }
  414. return true;
  415. }
  416. static bool MakeUserDirs()
  417. {
  418. char path[512];
  419. if (GetConfigPath(path, sizeof(path), "obs-studio/basic") <= 0)
  420. return false;
  421. if (!do_mkdir(path))
  422. return false;
  423. if (GetConfigPath(path, sizeof(path), "obs-studio/logs") <= 0)
  424. return false;
  425. if (!do_mkdir(path))
  426. return false;
  427. if (GetConfigPath(path, sizeof(path), "obs-studio/profiler_data") <= 0)
  428. return false;
  429. if (!do_mkdir(path))
  430. return false;
  431. #ifdef _WIN32
  432. if (GetConfigPath(path, sizeof(path), "obs-studio/crashes") <= 0)
  433. return false;
  434. if (!do_mkdir(path))
  435. return false;
  436. if (GetConfigPath(path, sizeof(path), "obs-studio/updates") <= 0)
  437. return false;
  438. if (!do_mkdir(path))
  439. return false;
  440. #endif
  441. if (GetConfigPath(path, sizeof(path), "obs-studio/plugin_config") <= 0)
  442. return false;
  443. if (!do_mkdir(path))
  444. return false;
  445. return true;
  446. }
  447. static bool MakeUserProfileDirs()
  448. {
  449. char path[512];
  450. if (GetConfigPath(path, sizeof(path), "obs-studio/basic/profiles") <= 0)
  451. return false;
  452. if (!do_mkdir(path))
  453. return false;
  454. if (GetConfigPath(path, sizeof(path), "obs-studio/basic/scenes") <= 0)
  455. return false;
  456. if (!do_mkdir(path))
  457. return false;
  458. return true;
  459. }
  460. static string GetProfileDirFromName(const char *name)
  461. {
  462. string outputPath;
  463. os_glob_t *glob;
  464. char path[512];
  465. if (GetConfigPath(path, sizeof(path), "obs-studio/basic/profiles") <= 0)
  466. return outputPath;
  467. strcat(path, "/*");
  468. if (os_glob(path, 0, &glob) != 0)
  469. return outputPath;
  470. for (size_t i = 0; i < glob->gl_pathc; i++) {
  471. struct os_globent ent = glob->gl_pathv[i];
  472. if (!ent.directory)
  473. continue;
  474. strcpy(path, ent.path);
  475. strcat(path, "/basic.ini");
  476. ConfigFile config;
  477. if (config.Open(path, CONFIG_OPEN_EXISTING) != 0)
  478. continue;
  479. const char *curName =
  480. config_get_string(config, "General", "Name");
  481. if (astrcmpi(curName, name) == 0) {
  482. outputPath = ent.path;
  483. break;
  484. }
  485. }
  486. os_globfree(glob);
  487. if (!outputPath.empty()) {
  488. replace(outputPath.begin(), outputPath.end(), '\\', '/');
  489. const char *start = strrchr(outputPath.c_str(), '/');
  490. if (start)
  491. outputPath.erase(0, start - outputPath.c_str() + 1);
  492. }
  493. return outputPath;
  494. }
  495. static string GetSceneCollectionFileFromName(const char *name)
  496. {
  497. string outputPath;
  498. os_glob_t *glob;
  499. char path[512];
  500. if (GetConfigPath(path, sizeof(path), "obs-studio/basic/scenes") <= 0)
  501. return outputPath;
  502. strcat(path, "/*.json");
  503. if (os_glob(path, 0, &glob) != 0)
  504. return outputPath;
  505. for (size_t i = 0; i < glob->gl_pathc; i++) {
  506. struct os_globent ent = glob->gl_pathv[i];
  507. if (ent.directory)
  508. continue;
  509. obs_data_t *data =
  510. obs_data_create_from_json_file_safe(ent.path, "bak");
  511. const char *curName = obs_data_get_string(data, "name");
  512. if (astrcmpi(name, curName) == 0) {
  513. outputPath = ent.path;
  514. obs_data_release(data);
  515. break;
  516. }
  517. obs_data_release(data);
  518. }
  519. os_globfree(glob);
  520. if (!outputPath.empty()) {
  521. outputPath.resize(outputPath.size() - 5);
  522. replace(outputPath.begin(), outputPath.end(), '\\', '/');
  523. const char *start = strrchr(outputPath.c_str(), '/');
  524. if (start)
  525. outputPath.erase(0, start - outputPath.c_str() + 1);
  526. }
  527. return outputPath;
  528. }
  529. bool OBSApp::UpdatePre22MultiviewLayout(const char *layout)
  530. {
  531. if (!layout)
  532. return false;
  533. if (astrcmpi(layout, "horizontaltop") == 0) {
  534. config_set_int(
  535. globalConfig, "BasicWindow", "MultiviewLayout",
  536. static_cast<int>(
  537. MultiviewLayout::HORIZONTAL_TOP_8_SCENES));
  538. return true;
  539. }
  540. if (astrcmpi(layout, "horizontalbottom") == 0) {
  541. config_set_int(
  542. globalConfig, "BasicWindow", "MultiviewLayout",
  543. static_cast<int>(
  544. MultiviewLayout::HORIZONTAL_BOTTOM_8_SCENES));
  545. return true;
  546. }
  547. if (astrcmpi(layout, "verticalleft") == 0) {
  548. config_set_int(
  549. globalConfig, "BasicWindow", "MultiviewLayout",
  550. static_cast<int>(
  551. MultiviewLayout::VERTICAL_LEFT_8_SCENES));
  552. return true;
  553. }
  554. if (astrcmpi(layout, "verticalright") == 0) {
  555. config_set_int(
  556. globalConfig, "BasicWindow", "MultiviewLayout",
  557. static_cast<int>(
  558. MultiviewLayout::VERTICAL_RIGHT_8_SCENES));
  559. return true;
  560. }
  561. return false;
  562. }
  563. bool OBSApp::InitGlobalConfig()
  564. {
  565. char path[512];
  566. bool changed = false;
  567. int len = GetConfigPath(path, sizeof(path), "obs-studio/global.ini");
  568. if (len <= 0) {
  569. return false;
  570. }
  571. int errorcode = globalConfig.Open(path, CONFIG_OPEN_ALWAYS);
  572. if (errorcode != CONFIG_SUCCESS) {
  573. OBSErrorBox(NULL, "Failed to open global.ini: %d", errorcode);
  574. return false;
  575. }
  576. if (!opt_starting_collection.empty()) {
  577. string path = GetSceneCollectionFileFromName(
  578. opt_starting_collection.c_str());
  579. if (!path.empty()) {
  580. config_set_string(globalConfig, "Basic",
  581. "SceneCollection",
  582. opt_starting_collection.c_str());
  583. config_set_string(globalConfig, "Basic",
  584. "SceneCollectionFile", path.c_str());
  585. changed = true;
  586. }
  587. }
  588. if (!opt_starting_profile.empty()) {
  589. string path =
  590. GetProfileDirFromName(opt_starting_profile.c_str());
  591. if (!path.empty()) {
  592. config_set_string(globalConfig, "Basic", "Profile",
  593. opt_starting_profile.c_str());
  594. config_set_string(globalConfig, "Basic", "ProfileDir",
  595. path.c_str());
  596. changed = true;
  597. }
  598. }
  599. uint32_t lastVersion =
  600. config_get_int(globalConfig, "General", "LastVersion");
  601. if (!config_has_user_value(globalConfig, "General", "Pre19Defaults")) {
  602. bool useOldDefaults = lastVersion &&
  603. lastVersion <
  604. MAKE_SEMANTIC_VERSION(19, 0, 0);
  605. config_set_bool(globalConfig, "General", "Pre19Defaults",
  606. useOldDefaults);
  607. changed = true;
  608. }
  609. if (!config_has_user_value(globalConfig, "General", "Pre21Defaults")) {
  610. bool useOldDefaults = lastVersion &&
  611. lastVersion <
  612. MAKE_SEMANTIC_VERSION(21, 0, 0);
  613. config_set_bool(globalConfig, "General", "Pre21Defaults",
  614. useOldDefaults);
  615. changed = true;
  616. }
  617. if (!config_has_user_value(globalConfig, "General", "Pre23Defaults")) {
  618. bool useOldDefaults = lastVersion &&
  619. lastVersion <
  620. MAKE_SEMANTIC_VERSION(23, 0, 0);
  621. config_set_bool(globalConfig, "General", "Pre23Defaults",
  622. useOldDefaults);
  623. changed = true;
  624. }
  625. #define PRE_24_1_DEFS "Pre24.1Defaults"
  626. if (!config_has_user_value(globalConfig, "General", PRE_24_1_DEFS)) {
  627. bool useOldDefaults = lastVersion &&
  628. lastVersion <
  629. MAKE_SEMANTIC_VERSION(24, 1, 0);
  630. config_set_bool(globalConfig, "General", PRE_24_1_DEFS,
  631. useOldDefaults);
  632. changed = true;
  633. }
  634. #undef PRE_24_1_DEFS
  635. if (config_has_user_value(globalConfig, "BasicWindow",
  636. "MultiviewLayout")) {
  637. const char *layout = config_get_string(
  638. globalConfig, "BasicWindow", "MultiviewLayout");
  639. changed |= UpdatePre22MultiviewLayout(layout);
  640. }
  641. if (lastVersion && lastVersion < MAKE_SEMANTIC_VERSION(24, 0, 0)) {
  642. bool disableHotkeysInFocus = config_get_bool(
  643. globalConfig, "General", "DisableHotkeysInFocus");
  644. if (disableHotkeysInFocus)
  645. config_set_string(globalConfig, "General",
  646. "HotkeyFocusType",
  647. "DisableHotkeysInFocus");
  648. changed = true;
  649. }
  650. if (changed)
  651. config_save_safe(globalConfig, "tmp", nullptr);
  652. return InitGlobalConfigDefaults();
  653. }
  654. bool OBSApp::InitLocale()
  655. {
  656. ProfileScope("OBSApp::InitLocale");
  657. const char *lang =
  658. config_get_string(globalConfig, "General", "Language");
  659. locale = lang;
  660. string englishPath;
  661. if (!GetDataFilePath("locale/" DEFAULT_LANG ".ini", englishPath)) {
  662. OBSErrorBox(NULL, "Failed to find locale/" DEFAULT_LANG ".ini");
  663. return false;
  664. }
  665. textLookup = text_lookup_create(englishPath.c_str());
  666. if (!textLookup) {
  667. OBSErrorBox(NULL, "Failed to create locale from file '%s'",
  668. englishPath.c_str());
  669. return false;
  670. }
  671. bool userLocale =
  672. config_has_user_value(globalConfig, "General", "Language");
  673. bool defaultLang = astrcmpi(lang, DEFAULT_LANG) == 0;
  674. if (userLocale && defaultLang)
  675. return true;
  676. if (!userLocale && defaultLang) {
  677. for (auto &locale_ : GetPreferredLocales()) {
  678. if (locale_ == lang)
  679. return true;
  680. stringstream file;
  681. file << "locale/" << locale_ << ".ini";
  682. string path;
  683. if (!GetDataFilePath(file.str().c_str(), path))
  684. continue;
  685. if (!text_lookup_add(textLookup, path.c_str()))
  686. continue;
  687. blog(LOG_INFO, "Using preferred locale '%s'",
  688. locale_.c_str());
  689. locale = locale_;
  690. return true;
  691. }
  692. return true;
  693. }
  694. stringstream file;
  695. file << "locale/" << lang << ".ini";
  696. string path;
  697. if (GetDataFilePath(file.str().c_str(), path)) {
  698. if (!text_lookup_add(textLookup, path.c_str()))
  699. blog(LOG_ERROR, "Failed to add locale file '%s'",
  700. path.c_str());
  701. } else {
  702. blog(LOG_ERROR, "Could not find locale file '%s'",
  703. file.str().c_str());
  704. }
  705. return true;
  706. }
  707. void OBSApp::AddExtraThemeColor(QPalette &pal, int group, const char *name,
  708. uint32_t color)
  709. {
  710. std::function<void(QPalette::ColorGroup)> func;
  711. #define DEF_PALETTE_ASSIGN(name) \
  712. do { \
  713. func = [&](QPalette::ColorGroup group) { \
  714. pal.setColor(group, QPalette::name, \
  715. QColor::fromRgb(color)); \
  716. }; \
  717. } while (false)
  718. if (astrcmpi(name, "alternateBase") == 0) {
  719. DEF_PALETTE_ASSIGN(AlternateBase);
  720. } else if (astrcmpi(name, "base") == 0) {
  721. DEF_PALETTE_ASSIGN(Base);
  722. } else if (astrcmpi(name, "brightText") == 0) {
  723. DEF_PALETTE_ASSIGN(BrightText);
  724. } else if (astrcmpi(name, "button") == 0) {
  725. DEF_PALETTE_ASSIGN(Button);
  726. } else if (astrcmpi(name, "buttonText") == 0) {
  727. DEF_PALETTE_ASSIGN(ButtonText);
  728. } else if (astrcmpi(name, "brightText") == 0) {
  729. DEF_PALETTE_ASSIGN(BrightText);
  730. } else if (astrcmpi(name, "dark") == 0) {
  731. DEF_PALETTE_ASSIGN(Dark);
  732. } else if (astrcmpi(name, "highlight") == 0) {
  733. DEF_PALETTE_ASSIGN(Highlight);
  734. } else if (astrcmpi(name, "highlightedText") == 0) {
  735. DEF_PALETTE_ASSIGN(HighlightedText);
  736. } else if (astrcmpi(name, "light") == 0) {
  737. DEF_PALETTE_ASSIGN(Light);
  738. } else if (astrcmpi(name, "link") == 0) {
  739. DEF_PALETTE_ASSIGN(Link);
  740. } else if (astrcmpi(name, "linkVisited") == 0) {
  741. DEF_PALETTE_ASSIGN(LinkVisited);
  742. } else if (astrcmpi(name, "mid") == 0) {
  743. DEF_PALETTE_ASSIGN(Mid);
  744. } else if (astrcmpi(name, "midlight") == 0) {
  745. DEF_PALETTE_ASSIGN(Midlight);
  746. } else if (astrcmpi(name, "shadow") == 0) {
  747. DEF_PALETTE_ASSIGN(Shadow);
  748. } else if (astrcmpi(name, "text") == 0 ||
  749. astrcmpi(name, "foreground") == 0) {
  750. DEF_PALETTE_ASSIGN(Text);
  751. } else if (astrcmpi(name, "toolTipBase") == 0) {
  752. DEF_PALETTE_ASSIGN(ToolTipBase);
  753. } else if (astrcmpi(name, "toolTipText") == 0) {
  754. DEF_PALETTE_ASSIGN(ToolTipText);
  755. } else if (astrcmpi(name, "windowText") == 0) {
  756. DEF_PALETTE_ASSIGN(WindowText);
  757. } else if (astrcmpi(name, "window") == 0 ||
  758. astrcmpi(name, "background") == 0) {
  759. DEF_PALETTE_ASSIGN(Window);
  760. } else {
  761. return;
  762. }
  763. #undef DEF_PALETTE_ASSIGN
  764. switch (group) {
  765. case QPalette::Disabled:
  766. case QPalette::Active:
  767. case QPalette::Inactive:
  768. func((QPalette::ColorGroup)group);
  769. break;
  770. default:
  771. func((QPalette::ColorGroup)QPalette::Disabled);
  772. func((QPalette::ColorGroup)QPalette::Active);
  773. func((QPalette::ColorGroup)QPalette::Inactive);
  774. }
  775. }
  776. struct CFParser {
  777. cf_parser cfp = {};
  778. inline ~CFParser() { cf_parser_free(&cfp); }
  779. inline operator cf_parser *() { return &cfp; }
  780. inline cf_parser *operator->() { return &cfp; }
  781. };
  782. void OBSApp::ParseExtraThemeData(const char *path)
  783. {
  784. BPtr<char> data = os_quick_read_utf8_file(path);
  785. QPalette pal = palette();
  786. CFParser cfp;
  787. int ret;
  788. cf_parser_parse(cfp, data, path);
  789. while (cf_go_to_token(cfp, "OBSTheme", nullptr)) {
  790. if (!cf_next_token(cfp))
  791. return;
  792. int group = -1;
  793. if (cf_token_is(cfp, ":")) {
  794. ret = cf_next_token_should_be(cfp, ":", nullptr,
  795. nullptr);
  796. if (ret != PARSE_SUCCESS)
  797. continue;
  798. if (!cf_next_token(cfp))
  799. return;
  800. if (cf_token_is(cfp, "disabled")) {
  801. group = QPalette::Disabled;
  802. } else if (cf_token_is(cfp, "active")) {
  803. group = QPalette::Active;
  804. } else if (cf_token_is(cfp, "inactive")) {
  805. group = QPalette::Inactive;
  806. } else {
  807. continue;
  808. }
  809. if (!cf_next_token(cfp))
  810. return;
  811. }
  812. if (!cf_token_is(cfp, "{"))
  813. continue;
  814. for (;;) {
  815. if (!cf_next_token(cfp))
  816. return;
  817. ret = cf_token_is_type(cfp, CFTOKEN_NAME, "name",
  818. nullptr);
  819. if (ret != PARSE_SUCCESS)
  820. break;
  821. DStr name;
  822. dstr_copy_strref(name, &cfp->cur_token->str);
  823. ret = cf_next_token_should_be(cfp, ":", ";", nullptr);
  824. if (ret != PARSE_SUCCESS)
  825. continue;
  826. if (!cf_next_token(cfp))
  827. return;
  828. const char *array;
  829. uint32_t color = 0;
  830. if (cf_token_is(cfp, "#")) {
  831. array = cfp->cur_token->str.array;
  832. color = strtol(array + 1, nullptr, 16);
  833. } else if (cf_token_is(cfp, "rgb")) {
  834. ret = cf_next_token_should_be(cfp, "(", ";",
  835. nullptr);
  836. if (ret != PARSE_SUCCESS)
  837. continue;
  838. if (!cf_next_token(cfp))
  839. return;
  840. array = cfp->cur_token->str.array;
  841. color |= strtol(array, nullptr, 10) << 16;
  842. ret = cf_next_token_should_be(cfp, ",", ";",
  843. nullptr);
  844. if (ret != PARSE_SUCCESS)
  845. continue;
  846. if (!cf_next_token(cfp))
  847. return;
  848. array = cfp->cur_token->str.array;
  849. color |= strtol(array, nullptr, 10) << 8;
  850. ret = cf_next_token_should_be(cfp, ",", ";",
  851. nullptr);
  852. if (ret != PARSE_SUCCESS)
  853. continue;
  854. if (!cf_next_token(cfp))
  855. return;
  856. array = cfp->cur_token->str.array;
  857. color |= strtol(array, nullptr, 10);
  858. } else if (cf_token_is(cfp, "white")) {
  859. color = 0xFFFFFF;
  860. } else if (cf_token_is(cfp, "black")) {
  861. color = 0;
  862. }
  863. if (!cf_go_to_token(cfp, ";", nullptr))
  864. return;
  865. AddExtraThemeColor(pal, group, name->array, color);
  866. }
  867. ret = cf_token_should_be(cfp, "}", "}", nullptr);
  868. if (ret != PARSE_SUCCESS)
  869. continue;
  870. }
  871. setPalette(pal);
  872. }
  873. bool OBSApp::SetTheme(std::string name, std::string path)
  874. {
  875. theme = name;
  876. /* Check user dir first, then preinstalled themes. */
  877. if (path == "") {
  878. char userDir[512];
  879. name = "themes/" + name + ".qss";
  880. string temp = "obs-studio/" + name;
  881. int ret = GetConfigPath(userDir, sizeof(userDir), temp.c_str());
  882. if (ret > 0 && QFile::exists(userDir)) {
  883. path = string(userDir);
  884. } else if (!GetDataFilePath(name.c_str(), path)) {
  885. OBSErrorBox(NULL, "Failed to find %s.", name.c_str());
  886. return false;
  887. }
  888. }
  889. QString mpath = QString("file:///") + path.c_str();
  890. setPalette(defaultPalette);
  891. setStyleSheet(mpath);
  892. ParseExtraThemeData(path.c_str());
  893. emit StyleChanged();
  894. return true;
  895. }
  896. bool OBSApp::InitTheme()
  897. {
  898. defaultPalette = palette();
  899. const char *themeName =
  900. config_get_string(globalConfig, "General", "CurrentTheme2");
  901. if (!themeName)
  902. /* Use deprecated "CurrentTheme" value if available */
  903. themeName = config_get_string(globalConfig, "General",
  904. "CurrentTheme");
  905. if (!themeName)
  906. /* Use deprecated "Theme" value if available */
  907. themeName = config_get_string(globalConfig, "General", "Theme");
  908. if (!themeName)
  909. themeName = DEFAULT_THEME;
  910. if (!themeName)
  911. themeName = "Dark";
  912. if (strcmp(themeName, "Default") == 0)
  913. themeName = "System";
  914. if (strcmp(themeName, "System") != 0 && SetTheme(themeName))
  915. return true;
  916. return SetTheme("System");
  917. }
  918. OBSApp::OBSApp(int &argc, char **argv, profiler_name_store_t *store)
  919. : QApplication(argc, argv), profilerNameStore(store)
  920. {
  921. sleepInhibitor = os_inhibit_sleep_create("OBS Video/audio");
  922. setWindowIcon(QIcon::fromTheme("obs", QIcon(":/res/images/obs.png")));
  923. }
  924. OBSApp::~OBSApp()
  925. {
  926. #ifdef _WIN32
  927. bool disableAudioDucking =
  928. config_get_bool(globalConfig, "Audio", "DisableAudioDucking");
  929. if (disableAudioDucking)
  930. DisableAudioDucking(false);
  931. #endif
  932. #ifdef __APPLE__
  933. bool vsyncDiabled =
  934. config_get_bool(globalConfig, "Video", "DisableOSXVSync");
  935. bool resetVSync =
  936. config_get_bool(globalConfig, "Video", "ResetOSXVSyncOnExit");
  937. if (vsyncDiabled && resetVSync)
  938. EnableOSXVSync(true);
  939. #endif
  940. os_inhibit_sleep_set_active(sleepInhibitor, false);
  941. os_inhibit_sleep_destroy(sleepInhibitor);
  942. if (libobs_initialized)
  943. obs_shutdown();
  944. }
  945. static void move_basic_to_profiles(void)
  946. {
  947. char path[512];
  948. char new_path[512];
  949. os_glob_t *glob;
  950. /* if not first time use */
  951. if (GetConfigPath(path, 512, "obs-studio/basic") <= 0)
  952. return;
  953. if (!os_file_exists(path))
  954. return;
  955. /* if the profiles directory doesn't already exist */
  956. if (GetConfigPath(new_path, 512, "obs-studio/basic/profiles") <= 0)
  957. return;
  958. if (os_file_exists(new_path))
  959. return;
  960. if (os_mkdir(new_path) == MKDIR_ERROR)
  961. return;
  962. strcat(new_path, "/");
  963. strcat(new_path, Str("Untitled"));
  964. if (os_mkdir(new_path) == MKDIR_ERROR)
  965. return;
  966. strcat(path, "/*.*");
  967. if (os_glob(path, 0, &glob) != 0)
  968. return;
  969. strcpy(path, new_path);
  970. for (size_t i = 0; i < glob->gl_pathc; i++) {
  971. struct os_globent ent = glob->gl_pathv[i];
  972. char *file;
  973. if (ent.directory)
  974. continue;
  975. file = strrchr(ent.path, '/');
  976. if (!file++)
  977. continue;
  978. if (astrcmpi(file, "scenes.json") == 0)
  979. continue;
  980. strcpy(new_path, path);
  981. strcat(new_path, "/");
  982. strcat(new_path, file);
  983. os_rename(ent.path, new_path);
  984. }
  985. os_globfree(glob);
  986. }
  987. static void move_basic_to_scene_collections(void)
  988. {
  989. char path[512];
  990. char new_path[512];
  991. if (GetConfigPath(path, 512, "obs-studio/basic") <= 0)
  992. return;
  993. if (!os_file_exists(path))
  994. return;
  995. if (GetConfigPath(new_path, 512, "obs-studio/basic/scenes") <= 0)
  996. return;
  997. if (os_file_exists(new_path))
  998. return;
  999. if (os_mkdir(new_path) == MKDIR_ERROR)
  1000. return;
  1001. strcat(path, "/scenes.json");
  1002. strcat(new_path, "/");
  1003. strcat(new_path, Str("Untitled"));
  1004. strcat(new_path, ".json");
  1005. os_rename(path, new_path);
  1006. }
  1007. void OBSApp::AppInit()
  1008. {
  1009. ProfileScope("OBSApp::AppInit");
  1010. if (!InitApplicationBundle())
  1011. throw "Failed to initialize application bundle";
  1012. if (!MakeUserDirs())
  1013. throw "Failed to create required user directories";
  1014. if (!InitGlobalConfig())
  1015. throw "Failed to initialize global config";
  1016. if (!InitLocale())
  1017. throw "Failed to load locale";
  1018. if (!InitTheme())
  1019. throw "Failed to load theme";
  1020. config_set_default_string(globalConfig, "Basic", "Profile",
  1021. Str("Untitled"));
  1022. config_set_default_string(globalConfig, "Basic", "ProfileDir",
  1023. Str("Untitled"));
  1024. config_set_default_string(globalConfig, "Basic", "SceneCollection",
  1025. Str("Untitled"));
  1026. config_set_default_string(globalConfig, "Basic", "SceneCollectionFile",
  1027. Str("Untitled"));
  1028. if (!config_has_user_value(globalConfig, "Basic", "Profile")) {
  1029. config_set_string(globalConfig, "Basic", "Profile",
  1030. Str("Untitled"));
  1031. config_set_string(globalConfig, "Basic", "ProfileDir",
  1032. Str("Untitled"));
  1033. }
  1034. if (!config_has_user_value(globalConfig, "Basic", "SceneCollection")) {
  1035. config_set_string(globalConfig, "Basic", "SceneCollection",
  1036. Str("Untitled"));
  1037. config_set_string(globalConfig, "Basic", "SceneCollectionFile",
  1038. Str("Untitled"));
  1039. }
  1040. #ifdef _WIN32
  1041. bool disableAudioDucking =
  1042. config_get_bool(globalConfig, "Audio", "DisableAudioDucking");
  1043. if (disableAudioDucking)
  1044. DisableAudioDucking(true);
  1045. #endif
  1046. #ifdef __APPLE__
  1047. if (config_get_bool(globalConfig, "Video", "DisableOSXVSync"))
  1048. EnableOSXVSync(false);
  1049. #endif
  1050. UpdateHotkeyFocusSetting(false);
  1051. move_basic_to_profiles();
  1052. move_basic_to_scene_collections();
  1053. if (!MakeUserProfileDirs())
  1054. throw "Failed to create profile directories";
  1055. }
  1056. const char *OBSApp::GetRenderModule() const
  1057. {
  1058. const char *renderer =
  1059. config_get_string(globalConfig, "Video", "Renderer");
  1060. return (astrcmpi(renderer, "Direct3D 11") == 0) ? DL_D3D11 : DL_OPENGL;
  1061. }
  1062. static bool StartupOBS(const char *locale, profiler_name_store_t *store)
  1063. {
  1064. char path[512];
  1065. if (GetConfigPath(path, sizeof(path), "obs-studio/plugin_config") <= 0)
  1066. return false;
  1067. return obs_startup(locale, path, store);
  1068. }
  1069. inline void OBSApp::ResetHotkeyState(bool inFocus)
  1070. {
  1071. obs_hotkey_enable_background_press(
  1072. (inFocus && enableHotkeysInFocus) ||
  1073. (!inFocus && enableHotkeysOutOfFocus));
  1074. }
  1075. void OBSApp::UpdateHotkeyFocusSetting(bool resetState)
  1076. {
  1077. enableHotkeysInFocus = true;
  1078. enableHotkeysOutOfFocus = true;
  1079. const char *hotkeyFocusType =
  1080. config_get_string(globalConfig, "General", "HotkeyFocusType");
  1081. if (astrcmpi(hotkeyFocusType, "DisableHotkeysInFocus") == 0) {
  1082. enableHotkeysInFocus = false;
  1083. } else if (astrcmpi(hotkeyFocusType, "DisableHotkeysOutOfFocus") == 0) {
  1084. enableHotkeysOutOfFocus = false;
  1085. }
  1086. if (resetState)
  1087. ResetHotkeyState(applicationState() == Qt::ApplicationActive);
  1088. }
  1089. void OBSApp::DisableHotkeys()
  1090. {
  1091. enableHotkeysInFocus = false;
  1092. enableHotkeysOutOfFocus = false;
  1093. ResetHotkeyState(applicationState() == Qt::ApplicationActive);
  1094. }
  1095. Q_DECLARE_METATYPE(VoidFunc)
  1096. void OBSApp::Exec(VoidFunc func)
  1097. {
  1098. func();
  1099. }
  1100. static void ui_task_handler(obs_task_t task, void *param, bool wait)
  1101. {
  1102. auto doTask = [=]() {
  1103. /* to get clang-format to behave */
  1104. task(param);
  1105. };
  1106. QMetaObject::invokeMethod(App(), "Exec",
  1107. wait ? WaitConnection() : Qt::AutoConnection,
  1108. Q_ARG(VoidFunc, doTask));
  1109. }
  1110. bool OBSApp::OBSInit()
  1111. {
  1112. ProfileScope("OBSApp::OBSInit");
  1113. setAttribute(Qt::AA_UseHighDpiPixmaps);
  1114. qRegisterMetaType<VoidFunc>();
  1115. if (!StartupOBS(locale.c_str(), GetProfilerNameStore()))
  1116. return false;
  1117. libobs_initialized = true;
  1118. obs_set_ui_task_handler(ui_task_handler);
  1119. #ifdef _WIN32
  1120. bool browserHWAccel =
  1121. config_get_bool(globalConfig, "General", "BrowserHWAccel");
  1122. obs_data_t *settings = obs_data_create();
  1123. obs_data_set_bool(settings, "BrowserHWAccel", browserHWAccel);
  1124. obs_apply_private_data(settings);
  1125. obs_data_release(settings);
  1126. blog(LOG_INFO, "Current Date/Time: %s",
  1127. CurrentDateTimeString().c_str());
  1128. blog(LOG_INFO, "Browser Hardware Acceleration: %s",
  1129. browserHWAccel ? "true" : "false");
  1130. #endif
  1131. blog(LOG_INFO, "Portable mode: %s", portable_mode ? "true" : "false");
  1132. setQuitOnLastWindowClosed(false);
  1133. mainWindow = new OBSBasic();
  1134. mainWindow->setAttribute(Qt::WA_DeleteOnClose, true);
  1135. connect(mainWindow, SIGNAL(destroyed()), this, SLOT(quit()));
  1136. mainWindow->OBSInit();
  1137. connect(this, &QGuiApplication::applicationStateChanged,
  1138. [this](Qt::ApplicationState state) {
  1139. ResetHotkeyState(state == Qt::ApplicationActive);
  1140. });
  1141. ResetHotkeyState(applicationState() == Qt::ApplicationActive);
  1142. return true;
  1143. }
  1144. string OBSApp::GetVersionString() const
  1145. {
  1146. stringstream ver;
  1147. #ifdef HAVE_OBSCONFIG_H
  1148. ver << OBS_VERSION;
  1149. #else
  1150. ver << LIBOBS_API_MAJOR_VER << "." << LIBOBS_API_MINOR_VER << "."
  1151. << LIBOBS_API_PATCH_VER;
  1152. #endif
  1153. ver << " (";
  1154. #ifdef _WIN32
  1155. if (sizeof(void *) == 8)
  1156. ver << "64-bit, ";
  1157. else
  1158. ver << "32-bit, ";
  1159. ver << "windows)";
  1160. #elif __APPLE__
  1161. ver << "mac)";
  1162. #elif __FreeBSD__
  1163. ver << "freebsd)";
  1164. #else /* assume linux for the time being */
  1165. ver << "linux)";
  1166. #endif
  1167. return ver.str();
  1168. }
  1169. bool OBSApp::IsPortableMode()
  1170. {
  1171. return portable_mode;
  1172. }
  1173. bool OBSApp::IsUpdaterDisabled()
  1174. {
  1175. return opt_disable_updater;
  1176. }
  1177. #ifdef __APPLE__
  1178. #define INPUT_AUDIO_SOURCE "coreaudio_input_capture"
  1179. #define OUTPUT_AUDIO_SOURCE "coreaudio_output_capture"
  1180. #elif _WIN32
  1181. #define INPUT_AUDIO_SOURCE "wasapi_input_capture"
  1182. #define OUTPUT_AUDIO_SOURCE "wasapi_output_capture"
  1183. #else
  1184. #define INPUT_AUDIO_SOURCE "pulse_input_capture"
  1185. #define OUTPUT_AUDIO_SOURCE "pulse_output_capture"
  1186. #endif
  1187. const char *OBSApp::InputAudioSource() const
  1188. {
  1189. return INPUT_AUDIO_SOURCE;
  1190. }
  1191. const char *OBSApp::OutputAudioSource() const
  1192. {
  1193. return OUTPUT_AUDIO_SOURCE;
  1194. }
  1195. const char *OBSApp::GetLastLog() const
  1196. {
  1197. return lastLogFile.c_str();
  1198. }
  1199. const char *OBSApp::GetCurrentLog() const
  1200. {
  1201. return currentLogFile.c_str();
  1202. }
  1203. const char *OBSApp::GetLastCrashLog() const
  1204. {
  1205. return lastCrashLogFile.c_str();
  1206. }
  1207. bool OBSApp::TranslateString(const char *lookupVal, const char **out) const
  1208. {
  1209. for (obs_frontend_translate_ui_cb cb : translatorHooks) {
  1210. if (cb(lookupVal, out))
  1211. return true;
  1212. }
  1213. return text_lookup_getstr(App()->GetTextLookup(), lookupVal, out);
  1214. }
  1215. QString OBSTranslator::translate(const char *context, const char *sourceText,
  1216. const char *disambiguation, int n) const
  1217. {
  1218. const char *out = nullptr;
  1219. if (!App()->TranslateString(sourceText, &out))
  1220. return QString(sourceText);
  1221. UNUSED_PARAMETER(context);
  1222. UNUSED_PARAMETER(disambiguation);
  1223. UNUSED_PARAMETER(n);
  1224. return QT_UTF8(out);
  1225. }
  1226. static bool get_token(lexer *lex, string &str, base_token_type type)
  1227. {
  1228. base_token token;
  1229. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  1230. return false;
  1231. if (token.type != type)
  1232. return false;
  1233. str.assign(token.text.array, token.text.len);
  1234. return true;
  1235. }
  1236. static bool expect_token(lexer *lex, const char *str, base_token_type type)
  1237. {
  1238. base_token token;
  1239. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  1240. return false;
  1241. if (token.type != type)
  1242. return false;
  1243. return strref_cmp(&token.text, str) == 0;
  1244. }
  1245. static uint64_t convert_log_name(bool has_prefix, const char *name)
  1246. {
  1247. BaseLexer lex;
  1248. string year, month, day, hour, minute, second;
  1249. lexer_start(lex, name);
  1250. if (has_prefix) {
  1251. string temp;
  1252. if (!get_token(lex, temp, BASETOKEN_ALPHA))
  1253. return 0;
  1254. }
  1255. if (!get_token(lex, year, BASETOKEN_DIGIT))
  1256. return 0;
  1257. if (!expect_token(lex, "-", BASETOKEN_OTHER))
  1258. return 0;
  1259. if (!get_token(lex, month, BASETOKEN_DIGIT))
  1260. return 0;
  1261. if (!expect_token(lex, "-", BASETOKEN_OTHER))
  1262. return 0;
  1263. if (!get_token(lex, day, BASETOKEN_DIGIT))
  1264. return 0;
  1265. if (!get_token(lex, hour, BASETOKEN_DIGIT))
  1266. return 0;
  1267. if (!expect_token(lex, "-", BASETOKEN_OTHER))
  1268. return 0;
  1269. if (!get_token(lex, minute, BASETOKEN_DIGIT))
  1270. return 0;
  1271. if (!expect_token(lex, "-", BASETOKEN_OTHER))
  1272. return 0;
  1273. if (!get_token(lex, second, BASETOKEN_DIGIT))
  1274. return 0;
  1275. stringstream timestring;
  1276. timestring << year << month << day << hour << minute << second;
  1277. return std::stoull(timestring.str());
  1278. }
  1279. static void delete_oldest_file(bool has_prefix, const char *location)
  1280. {
  1281. BPtr<char> logDir(GetConfigPathPtr(location));
  1282. string oldestLog;
  1283. uint64_t oldest_ts = (uint64_t)-1;
  1284. struct os_dirent *entry;
  1285. unsigned int maxLogs = (unsigned int)config_get_uint(
  1286. App()->GlobalConfig(), "General", "MaxLogs");
  1287. os_dir_t *dir = os_opendir(logDir);
  1288. if (dir) {
  1289. unsigned int count = 0;
  1290. while ((entry = os_readdir(dir)) != NULL) {
  1291. if (entry->directory || *entry->d_name == '.')
  1292. continue;
  1293. uint64_t ts =
  1294. convert_log_name(has_prefix, entry->d_name);
  1295. if (ts) {
  1296. if (ts < oldest_ts) {
  1297. oldestLog = entry->d_name;
  1298. oldest_ts = ts;
  1299. }
  1300. count++;
  1301. }
  1302. }
  1303. os_closedir(dir);
  1304. if (count > maxLogs) {
  1305. stringstream delPath;
  1306. delPath << logDir << "/" << oldestLog;
  1307. os_unlink(delPath.str().c_str());
  1308. }
  1309. }
  1310. }
  1311. static void get_last_log(bool has_prefix, const char *subdir_to_use,
  1312. std::string &last)
  1313. {
  1314. BPtr<char> logDir(GetConfigPathPtr(subdir_to_use));
  1315. struct os_dirent *entry;
  1316. os_dir_t *dir = os_opendir(logDir);
  1317. uint64_t highest_ts = 0;
  1318. if (dir) {
  1319. while ((entry = os_readdir(dir)) != NULL) {
  1320. if (entry->directory || *entry->d_name == '.')
  1321. continue;
  1322. uint64_t ts =
  1323. convert_log_name(has_prefix, entry->d_name);
  1324. if (ts > highest_ts) {
  1325. last = entry->d_name;
  1326. highest_ts = ts;
  1327. }
  1328. }
  1329. os_closedir(dir);
  1330. }
  1331. }
  1332. string GenerateTimeDateFilename(const char *extension, bool noSpace)
  1333. {
  1334. time_t now = time(0);
  1335. char file[256] = {};
  1336. struct tm *cur_time;
  1337. cur_time = localtime(&now);
  1338. snprintf(file, sizeof(file), "%d-%02d-%02d%c%02d-%02d-%02d.%s",
  1339. cur_time->tm_year + 1900, cur_time->tm_mon + 1,
  1340. cur_time->tm_mday, noSpace ? '_' : ' ', cur_time->tm_hour,
  1341. cur_time->tm_min, cur_time->tm_sec, extension);
  1342. return string(file);
  1343. }
  1344. string GenerateSpecifiedFilename(const char *extension, bool noSpace,
  1345. const char *format)
  1346. {
  1347. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  1348. bool autoRemux = config_get_bool(main->Config(), "Video", "AutoRemux");
  1349. if ((strcmp(extension, "mp4") == 0) && autoRemux)
  1350. extension = "mkv";
  1351. BPtr<char> filename =
  1352. os_generate_formatted_filename(extension, !noSpace, format);
  1353. remuxFilename = string(filename);
  1354. remuxAfterRecord = autoRemux;
  1355. return string(filename);
  1356. }
  1357. static void FindBestFilename(string &strPath, bool noSpace)
  1358. {
  1359. int num = 2;
  1360. if (!os_file_exists(strPath.c_str()))
  1361. return;
  1362. const char *ext = strrchr(strPath.c_str(), '.');
  1363. if (!ext)
  1364. return;
  1365. int extStart = int(ext - strPath.c_str());
  1366. for (;;) {
  1367. string testPath = strPath;
  1368. string numStr;
  1369. numStr = noSpace ? "_" : " (";
  1370. numStr += to_string(num++);
  1371. if (!noSpace)
  1372. numStr += ")";
  1373. testPath.insert(extStart, numStr);
  1374. if (!os_file_exists(testPath.c_str())) {
  1375. strPath = testPath;
  1376. break;
  1377. }
  1378. }
  1379. }
  1380. static void ensure_directory_exists(string &path)
  1381. {
  1382. replace(path.begin(), path.end(), '\\', '/');
  1383. size_t last = path.rfind('/');
  1384. if (last == string::npos)
  1385. return;
  1386. string directory = path.substr(0, last);
  1387. os_mkdirs(directory.c_str());
  1388. }
  1389. static void remove_reserved_file_characters(string &s)
  1390. {
  1391. replace(s.begin(), s.end(), '\\', '/');
  1392. replace(s.begin(), s.end(), '*', '_');
  1393. replace(s.begin(), s.end(), '?', '_');
  1394. replace(s.begin(), s.end(), '"', '_');
  1395. replace(s.begin(), s.end(), '|', '_');
  1396. replace(s.begin(), s.end(), ':', '_');
  1397. replace(s.begin(), s.end(), '>', '_');
  1398. replace(s.begin(), s.end(), '<', '_');
  1399. }
  1400. string GetFormatString(const char *format, const char *prefix,
  1401. const char *suffix)
  1402. {
  1403. string f;
  1404. if (prefix && *prefix) {
  1405. f += prefix;
  1406. if (f.back() != ' ')
  1407. f += " ";
  1408. }
  1409. f += format;
  1410. if (suffix && *suffix) {
  1411. if (*suffix != ' ')
  1412. f += " ";
  1413. f += suffix;
  1414. }
  1415. remove_reserved_file_characters(f);
  1416. return f;
  1417. }
  1418. string GetOutputFilename(const char *path, const char *ext, bool noSpace,
  1419. bool overwrite, const char *format)
  1420. {
  1421. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  1422. os_dir_t *dir = path && path[0] ? os_opendir(path) : nullptr;
  1423. if (!dir) {
  1424. if (main->isVisible())
  1425. OBSMessageBox::warning(main,
  1426. QTStr("Output.BadPath.Title"),
  1427. QTStr("Output.BadPath.Text"));
  1428. else
  1429. main->SysTrayNotify(QTStr("Output.BadPath.Text"),
  1430. QSystemTrayIcon::Warning);
  1431. return "";
  1432. }
  1433. os_closedir(dir);
  1434. string strPath;
  1435. strPath += path;
  1436. char lastChar = strPath.back();
  1437. if (lastChar != '/' && lastChar != '\\')
  1438. strPath += "/";
  1439. strPath += GenerateSpecifiedFilename(ext, noSpace, format);
  1440. ensure_directory_exists(strPath);
  1441. if (!overwrite)
  1442. FindBestFilename(strPath, noSpace);
  1443. return strPath;
  1444. }
  1445. vector<pair<string, string>> GetLocaleNames()
  1446. {
  1447. string path;
  1448. if (!GetDataFilePath("locale.ini", path))
  1449. throw "Could not find locale.ini path";
  1450. ConfigFile ini;
  1451. if (ini.Open(path.c_str(), CONFIG_OPEN_EXISTING) != 0)
  1452. throw "Could not open locale.ini";
  1453. size_t sections = config_num_sections(ini);
  1454. vector<pair<string, string>> names;
  1455. names.reserve(sections);
  1456. for (size_t i = 0; i < sections; i++) {
  1457. const char *tag = config_get_section(ini, i);
  1458. const char *name = config_get_string(ini, tag, "Name");
  1459. names.emplace_back(tag, name);
  1460. }
  1461. return names;
  1462. }
  1463. static void create_log_file(fstream &logFile)
  1464. {
  1465. stringstream dst;
  1466. get_last_log(false, "obs-studio/logs", lastLogFile);
  1467. #ifdef _WIN32
  1468. get_last_log(true, "obs-studio/crashes", lastCrashLogFile);
  1469. #endif
  1470. currentLogFile = GenerateTimeDateFilename("txt");
  1471. dst << "obs-studio/logs/" << currentLogFile.c_str();
  1472. BPtr<char> path(GetConfigPathPtr(dst.str().c_str()));
  1473. #ifdef _WIN32
  1474. BPtr<wchar_t> wpath;
  1475. os_utf8_to_wcs_ptr(path, 0, &wpath);
  1476. logFile.open(wpath, ios_base::in | ios_base::out | ios_base::trunc);
  1477. #else
  1478. logFile.open(path, ios_base::in | ios_base::out | ios_base::trunc);
  1479. #endif
  1480. if (logFile.is_open()) {
  1481. delete_oldest_file(false, "obs-studio/logs");
  1482. base_set_log_handler(do_log, &logFile);
  1483. } else {
  1484. blog(LOG_ERROR, "Failed to open log file");
  1485. }
  1486. }
  1487. static auto ProfilerNameStoreRelease = [](profiler_name_store_t *store) {
  1488. profiler_name_store_free(store);
  1489. };
  1490. using ProfilerNameStore = std::unique_ptr<profiler_name_store_t,
  1491. decltype(ProfilerNameStoreRelease)>;
  1492. ProfilerNameStore CreateNameStore()
  1493. {
  1494. return ProfilerNameStore{profiler_name_store_create(),
  1495. ProfilerNameStoreRelease};
  1496. }
  1497. static auto SnapshotRelease = [](profiler_snapshot_t *snap) {
  1498. profile_snapshot_free(snap);
  1499. };
  1500. using ProfilerSnapshot =
  1501. std::unique_ptr<profiler_snapshot_t, decltype(SnapshotRelease)>;
  1502. ProfilerSnapshot GetSnapshot()
  1503. {
  1504. return ProfilerSnapshot{profile_snapshot_create(), SnapshotRelease};
  1505. }
  1506. static void SaveProfilerData(const ProfilerSnapshot &snap)
  1507. {
  1508. if (currentLogFile.empty())
  1509. return;
  1510. auto pos = currentLogFile.rfind('.');
  1511. if (pos == currentLogFile.npos)
  1512. return;
  1513. #define LITERAL_SIZE(x) x, (sizeof(x) - 1)
  1514. ostringstream dst;
  1515. dst.write(LITERAL_SIZE("obs-studio/profiler_data/"));
  1516. dst.write(currentLogFile.c_str(), pos);
  1517. dst.write(LITERAL_SIZE(".csv.gz"));
  1518. #undef LITERAL_SIZE
  1519. BPtr<char> path = GetConfigPathPtr(dst.str().c_str());
  1520. if (!profiler_snapshot_dump_csv_gz(snap.get(), path))
  1521. blog(LOG_WARNING, "Could not save profiler data to '%s'",
  1522. static_cast<const char *>(path));
  1523. }
  1524. static auto ProfilerFree = [](void *) {
  1525. profiler_stop();
  1526. auto snap = GetSnapshot();
  1527. profiler_print(snap.get());
  1528. profiler_print_time_between_calls(snap.get());
  1529. SaveProfilerData(snap);
  1530. profiler_free();
  1531. };
  1532. static const char *run_program_init = "run_program_init";
  1533. static int run_program(fstream &logFile, int argc, char *argv[])
  1534. {
  1535. int ret = -1;
  1536. auto profilerNameStore = CreateNameStore();
  1537. std::unique_ptr<void, decltype(ProfilerFree)> prof_release(
  1538. static_cast<void *>(&ProfilerFree), ProfilerFree);
  1539. profiler_start();
  1540. profile_register_root(run_program_init, 0);
  1541. ScopeProfiler prof{run_program_init};
  1542. #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
  1543. QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  1544. #endif
  1545. #if !defined(_WIN32) && !defined(__APPLE__) && BROWSER_AVAILABLE
  1546. setenv("QT_NO_GLIB", "1", true);
  1547. #endif
  1548. QCoreApplication::addLibraryPath(".");
  1549. #if __APPLE__
  1550. InstallNSApplicationSubclass();
  1551. #endif
  1552. OBSApp program(argc, argv, profilerNameStore.get());
  1553. try {
  1554. bool created_log = false;
  1555. program.AppInit();
  1556. delete_oldest_file(false, "obs-studio/profiler_data");
  1557. OBSTranslator translator;
  1558. program.installTranslator(&translator);
  1559. #ifdef _WIN32
  1560. /* --------------------------------------- */
  1561. /* check and warn if already running */
  1562. bool cancel_launch = false;
  1563. bool already_running = false;
  1564. RunOnceMutex rom = GetRunOnceMutex(already_running);
  1565. if (!already_running) {
  1566. goto run;
  1567. }
  1568. if (!multi) {
  1569. QMessageBox::StandardButtons buttons(
  1570. QMessageBox::Yes | QMessageBox::Cancel);
  1571. QMessageBox mb(QMessageBox::Question,
  1572. QTStr("AlreadyRunning.Title"),
  1573. QTStr("AlreadyRunning.Text"), buttons,
  1574. nullptr);
  1575. mb.setButtonText(QMessageBox::Yes,
  1576. QTStr("AlreadyRunning.LaunchAnyway"));
  1577. mb.setButtonText(QMessageBox::Cancel, QTStr("Cancel"));
  1578. mb.setDefaultButton(QMessageBox::Cancel);
  1579. QMessageBox::StandardButton button;
  1580. button = (QMessageBox::StandardButton)mb.exec();
  1581. cancel_launch = button == QMessageBox::Cancel;
  1582. }
  1583. if (cancel_launch)
  1584. return 0;
  1585. if (!created_log) {
  1586. create_log_file(logFile);
  1587. created_log = true;
  1588. }
  1589. if (multi) {
  1590. blog(LOG_INFO, "User enabled --multi flag and is now "
  1591. "running multiple instances of OBS.");
  1592. } else {
  1593. blog(LOG_WARNING, "================================");
  1594. blog(LOG_WARNING, "Warning: OBS is already running!");
  1595. blog(LOG_WARNING, "================================");
  1596. blog(LOG_WARNING, "User is now running multiple "
  1597. "instances of OBS!");
  1598. }
  1599. /* --------------------------------------- */
  1600. run:
  1601. #endif
  1602. #if !defined(_WIN32) && !defined(__APPLE__)
  1603. // Mounted by termina during chromeOS linux container startup
  1604. // https://chromium.googlesource.com/chromiumos/overlays/board-overlays/+/master/project-termina/chromeos-base/termina-lxd-scripts/files/lxd_setup.sh
  1605. os_dir_t *crosDir = os_opendir("/opt/google/cros-containers");
  1606. if (crosDir) {
  1607. QMessageBox::StandardButtons buttons(QMessageBox::Ok);
  1608. QMessageBox mb(QMessageBox::Critical,
  1609. QTStr("ChromeOS.Title"),
  1610. QTStr("ChromeOS.Text"), buttons,
  1611. nullptr);
  1612. mb.exec();
  1613. return 0;
  1614. }
  1615. #endif
  1616. if (!created_log) {
  1617. create_log_file(logFile);
  1618. created_log = true;
  1619. }
  1620. if (argc > 1) {
  1621. stringstream stor;
  1622. stor << argv[1];
  1623. for (int i = 2; i < argc; ++i) {
  1624. stor << " " << argv[i];
  1625. }
  1626. blog(LOG_INFO, "Command Line Arguments: %s",
  1627. stor.str().c_str());
  1628. }
  1629. if (!program.OBSInit())
  1630. return 0;
  1631. prof.Stop();
  1632. ret = program.exec();
  1633. } catch (const char *error) {
  1634. blog(LOG_ERROR, "%s", error);
  1635. OBSErrorBox(nullptr, "%s", error);
  1636. }
  1637. if (restart)
  1638. QProcess::startDetached(qApp->arguments()[0],
  1639. qApp->arguments());
  1640. return ret;
  1641. }
  1642. #define MAX_CRASH_REPORT_SIZE (150 * 1024)
  1643. #ifdef _WIN32
  1644. #define CRASH_MESSAGE \
  1645. "Woops, OBS has crashed!\n\nWould you like to copy the crash log " \
  1646. "to the clipboard? The crash log will still be saved to:\n\n%s"
  1647. static void main_crash_handler(const char *format, va_list args, void *param)
  1648. {
  1649. char *text = new char[MAX_CRASH_REPORT_SIZE];
  1650. vsnprintf(text, MAX_CRASH_REPORT_SIZE, format, args);
  1651. text[MAX_CRASH_REPORT_SIZE - 1] = 0;
  1652. string crashFilePath = "obs-studio/crashes";
  1653. delete_oldest_file(true, crashFilePath.c_str());
  1654. string name = crashFilePath + "/";
  1655. name += "Crash " + GenerateTimeDateFilename("txt");
  1656. BPtr<char> path(GetConfigPathPtr(name.c_str()));
  1657. fstream file;
  1658. #ifdef _WIN32
  1659. BPtr<wchar_t> wpath;
  1660. os_utf8_to_wcs_ptr(path, 0, &wpath);
  1661. file.open(wpath, ios_base::in | ios_base::out | ios_base::trunc |
  1662. ios_base::binary);
  1663. #else
  1664. file.open(path, ios_base::in | ios_base::out | ios_base::trunc |
  1665. ios_base::binary);
  1666. #endif
  1667. file << text;
  1668. file.close();
  1669. string pathString(path.Get());
  1670. #ifdef _WIN32
  1671. std::replace(pathString.begin(), pathString.end(), '/', '\\');
  1672. #endif
  1673. string absolutePath =
  1674. canonical(filesystem::path(pathString)).u8string();
  1675. size_t size = snprintf(nullptr, 0, CRASH_MESSAGE, absolutePath.c_str());
  1676. unique_ptr<char[]> message_buffer(new char[size + 1]);
  1677. snprintf(message_buffer.get(), size + 1, CRASH_MESSAGE,
  1678. absolutePath.c_str());
  1679. string finalMessage =
  1680. string(message_buffer.get(), message_buffer.get() + size);
  1681. int ret = MessageBoxA(NULL, finalMessage.c_str(), "OBS has crashed!",
  1682. MB_YESNO | MB_ICONERROR | MB_TASKMODAL);
  1683. if (ret == IDYES) {
  1684. size_t len = strlen(text);
  1685. HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, len);
  1686. memcpy(GlobalLock(mem), text, len);
  1687. GlobalUnlock(mem);
  1688. OpenClipboard(0);
  1689. EmptyClipboard();
  1690. SetClipboardData(CF_TEXT, mem);
  1691. CloseClipboard();
  1692. }
  1693. exit(-1);
  1694. UNUSED_PARAMETER(param);
  1695. }
  1696. static void load_debug_privilege(void)
  1697. {
  1698. const DWORD flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY;
  1699. TOKEN_PRIVILEGES tp;
  1700. HANDLE token;
  1701. LUID val;
  1702. if (!OpenProcessToken(GetCurrentProcess(), flags, &token)) {
  1703. return;
  1704. }
  1705. if (!!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &val)) {
  1706. tp.PrivilegeCount = 1;
  1707. tp.Privileges[0].Luid = val;
  1708. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1709. AdjustTokenPrivileges(token, false, &tp, sizeof(tp), NULL,
  1710. NULL);
  1711. }
  1712. if (!!LookupPrivilegeValue(NULL, SE_INC_BASE_PRIORITY_NAME, &val)) {
  1713. tp.PrivilegeCount = 1;
  1714. tp.Privileges[0].Luid = val;
  1715. tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  1716. if (!AdjustTokenPrivileges(token, false, &tp, sizeof(tp), NULL,
  1717. NULL)) {
  1718. blog(LOG_INFO, "Could not set privilege to "
  1719. "increase GPU priority");
  1720. }
  1721. }
  1722. CloseHandle(token);
  1723. }
  1724. #endif
  1725. #ifdef __APPLE__
  1726. #define BASE_PATH ".."
  1727. #else
  1728. #define BASE_PATH "../.."
  1729. #endif
  1730. #define CONFIG_PATH BASE_PATH "/config"
  1731. #ifndef OBS_UNIX_STRUCTURE
  1732. #define OBS_UNIX_STRUCTURE 0
  1733. #endif
  1734. int GetConfigPath(char *path, size_t size, const char *name)
  1735. {
  1736. if (!OBS_UNIX_STRUCTURE && portable_mode) {
  1737. if (name && *name) {
  1738. return snprintf(path, size, CONFIG_PATH "/%s", name);
  1739. } else {
  1740. return snprintf(path, size, CONFIG_PATH);
  1741. }
  1742. } else {
  1743. return os_get_config_path(path, size, name);
  1744. }
  1745. }
  1746. char *GetConfigPathPtr(const char *name)
  1747. {
  1748. if (!OBS_UNIX_STRUCTURE && portable_mode) {
  1749. char path[512];
  1750. if (snprintf(path, sizeof(path), CONFIG_PATH "/%s", name) > 0) {
  1751. return bstrdup(path);
  1752. } else {
  1753. return NULL;
  1754. }
  1755. } else {
  1756. return os_get_config_path_ptr(name);
  1757. }
  1758. }
  1759. int GetProgramDataPath(char *path, size_t size, const char *name)
  1760. {
  1761. return os_get_program_data_path(path, size, name);
  1762. }
  1763. char *GetProgramDataPathPtr(const char *name)
  1764. {
  1765. return os_get_program_data_path_ptr(name);
  1766. }
  1767. bool GetFileSafeName(const char *name, std::string &file)
  1768. {
  1769. size_t base_len = strlen(name);
  1770. size_t len = os_utf8_to_wcs(name, base_len, nullptr, 0);
  1771. std::wstring wfile;
  1772. if (!len)
  1773. return false;
  1774. wfile.resize(len);
  1775. os_utf8_to_wcs(name, base_len, &wfile[0], len + 1);
  1776. for (size_t i = wfile.size(); i > 0; i--) {
  1777. size_t im1 = i - 1;
  1778. if (iswspace(wfile[im1])) {
  1779. wfile[im1] = '_';
  1780. } else if (wfile[im1] != '_' && !iswalnum(wfile[im1])) {
  1781. wfile.erase(im1, 1);
  1782. }
  1783. }
  1784. if (wfile.size() == 0)
  1785. wfile = L"characters_only";
  1786. len = os_wcs_to_utf8(wfile.c_str(), wfile.size(), nullptr, 0);
  1787. if (!len)
  1788. return false;
  1789. file.resize(len);
  1790. os_wcs_to_utf8(wfile.c_str(), wfile.size(), &file[0], len + 1);
  1791. return true;
  1792. }
  1793. bool GetClosestUnusedFileName(std::string &path, const char *extension)
  1794. {
  1795. size_t len = path.size();
  1796. if (extension) {
  1797. path += ".";
  1798. path += extension;
  1799. }
  1800. if (!os_file_exists(path.c_str()))
  1801. return true;
  1802. int index = 1;
  1803. do {
  1804. path.resize(len);
  1805. path += std::to_string(++index);
  1806. if (extension) {
  1807. path += ".";
  1808. path += extension;
  1809. }
  1810. } while (os_file_exists(path.c_str()));
  1811. return true;
  1812. }
  1813. bool WindowPositionValid(QRect rect)
  1814. {
  1815. for (QScreen *screen : QGuiApplication::screens()) {
  1816. if (screen->availableGeometry().intersects(rect))
  1817. return true;
  1818. }
  1819. return false;
  1820. }
  1821. static inline bool arg_is(const char *arg, const char *long_form,
  1822. const char *short_form)
  1823. {
  1824. return (long_form && strcmp(arg, long_form) == 0) ||
  1825. (short_form && strcmp(arg, short_form) == 0);
  1826. }
  1827. #if !defined(_WIN32) && !defined(__APPLE__)
  1828. #define IS_UNIX 1
  1829. #endif
  1830. /* if using XDG and was previously using an older build of OBS, move config
  1831. * files to XDG directory */
  1832. #if defined(USE_XDG) && defined(IS_UNIX)
  1833. static void move_to_xdg(void)
  1834. {
  1835. char old_path[512];
  1836. char new_path[512];
  1837. char *home = getenv("HOME");
  1838. if (!home)
  1839. return;
  1840. if (snprintf(old_path, 512, "%s/.obs-studio", home) <= 0)
  1841. return;
  1842. /* make base xdg path if it doesn't already exist */
  1843. if (GetConfigPath(new_path, 512, "") <= 0)
  1844. return;
  1845. if (os_mkdirs(new_path) == MKDIR_ERROR)
  1846. return;
  1847. if (GetConfigPath(new_path, 512, "obs-studio") <= 0)
  1848. return;
  1849. if (os_file_exists(old_path) && !os_file_exists(new_path)) {
  1850. rename(old_path, new_path);
  1851. }
  1852. }
  1853. #endif
  1854. static bool update_ffmpeg_output(ConfigFile &config)
  1855. {
  1856. if (config_has_user_value(config, "AdvOut", "FFOutputToFile"))
  1857. return false;
  1858. const char *url = config_get_string(config, "AdvOut", "FFURL");
  1859. if (!url)
  1860. return false;
  1861. bool isActualURL = strstr(url, "://") != nullptr;
  1862. if (isActualURL)
  1863. return false;
  1864. string urlStr = url;
  1865. string extension;
  1866. for (size_t i = urlStr.length(); i > 0; i--) {
  1867. size_t idx = i - 1;
  1868. if (urlStr[idx] == '.') {
  1869. extension = &urlStr[i];
  1870. }
  1871. if (urlStr[idx] == '\\' || urlStr[idx] == '/') {
  1872. urlStr[idx] = 0;
  1873. break;
  1874. }
  1875. }
  1876. if (urlStr.empty() || extension.empty())
  1877. return false;
  1878. config_remove_value(config, "AdvOut", "FFURL");
  1879. config_set_string(config, "AdvOut", "FFFilePath", urlStr.c_str());
  1880. config_set_string(config, "AdvOut", "FFExtension", extension.c_str());
  1881. config_set_bool(config, "AdvOut", "FFOutputToFile", true);
  1882. return true;
  1883. }
  1884. static bool move_reconnect_settings(ConfigFile &config, const char *sec)
  1885. {
  1886. bool changed = false;
  1887. if (config_has_user_value(config, sec, "Reconnect")) {
  1888. bool reconnect = config_get_bool(config, sec, "Reconnect");
  1889. config_set_bool(config, "Output", "Reconnect", reconnect);
  1890. changed = true;
  1891. }
  1892. if (config_has_user_value(config, sec, "RetryDelay")) {
  1893. int delay = (int)config_get_uint(config, sec, "RetryDelay");
  1894. config_set_uint(config, "Output", "RetryDelay", delay);
  1895. changed = true;
  1896. }
  1897. if (config_has_user_value(config, sec, "MaxRetries")) {
  1898. int retries = (int)config_get_uint(config, sec, "MaxRetries");
  1899. config_set_uint(config, "Output", "MaxRetries", retries);
  1900. changed = true;
  1901. }
  1902. return changed;
  1903. }
  1904. static bool update_reconnect(ConfigFile &config)
  1905. {
  1906. if (!config_has_user_value(config, "Output", "Mode"))
  1907. return false;
  1908. const char *mode = config_get_string(config, "Output", "Mode");
  1909. if (!mode)
  1910. return false;
  1911. const char *section = (strcmp(mode, "Advanced") == 0) ? "AdvOut"
  1912. : "SimpleOutput";
  1913. if (move_reconnect_settings(config, section)) {
  1914. config_remove_value(config, "SimpleOutput", "Reconnect");
  1915. config_remove_value(config, "SimpleOutput", "RetryDelay");
  1916. config_remove_value(config, "SimpleOutput", "MaxRetries");
  1917. config_remove_value(config, "AdvOut", "Reconnect");
  1918. config_remove_value(config, "AdvOut", "RetryDelay");
  1919. config_remove_value(config, "AdvOut", "MaxRetries");
  1920. return true;
  1921. }
  1922. return false;
  1923. }
  1924. static void convert_x264_settings(obs_data_t *data)
  1925. {
  1926. bool use_bufsize = obs_data_get_bool(data, "use_bufsize");
  1927. if (use_bufsize) {
  1928. int buffer_size = (int)obs_data_get_int(data, "buffer_size");
  1929. if (buffer_size == 0)
  1930. obs_data_set_string(data, "rate_control", "CRF");
  1931. }
  1932. }
  1933. static void convert_14_2_encoder_setting(const char *encoder, const char *file)
  1934. {
  1935. obs_data_t *data = obs_data_create_from_json_file_safe(file, "bak");
  1936. obs_data_item_t *cbr_item = obs_data_item_byname(data, "cbr");
  1937. obs_data_item_t *rc_item = obs_data_item_byname(data, "rate_control");
  1938. bool modified = false;
  1939. bool cbr = true;
  1940. if (cbr_item) {
  1941. cbr = obs_data_item_get_bool(cbr_item);
  1942. obs_data_item_unset_user_value(cbr_item);
  1943. obs_data_set_string(data, "rate_control", cbr ? "CBR" : "VBR");
  1944. modified = true;
  1945. }
  1946. if (!rc_item && astrcmpi(encoder, "obs_x264") == 0) {
  1947. if (!cbr_item)
  1948. obs_data_set_string(data, "rate_control", "CBR");
  1949. else if (!cbr)
  1950. convert_x264_settings(data);
  1951. modified = true;
  1952. }
  1953. if (modified)
  1954. obs_data_save_json_safe(data, file, "tmp", "bak");
  1955. obs_data_item_release(&rc_item);
  1956. obs_data_item_release(&cbr_item);
  1957. obs_data_release(data);
  1958. }
  1959. static void upgrade_settings(void)
  1960. {
  1961. char path[512];
  1962. int pathlen = GetConfigPath(path, 512, "obs-studio/basic/profiles");
  1963. if (pathlen <= 0)
  1964. return;
  1965. if (!os_file_exists(path))
  1966. return;
  1967. os_dir_t *dir = os_opendir(path);
  1968. if (!dir)
  1969. return;
  1970. struct os_dirent *ent = os_readdir(dir);
  1971. while (ent) {
  1972. if (ent->directory && strcmp(ent->d_name, ".") != 0 &&
  1973. strcmp(ent->d_name, "..") != 0) {
  1974. strcat(path, "/");
  1975. strcat(path, ent->d_name);
  1976. strcat(path, "/basic.ini");
  1977. ConfigFile config;
  1978. int ret;
  1979. ret = config.Open(path, CONFIG_OPEN_EXISTING);
  1980. if (ret == CONFIG_SUCCESS) {
  1981. if (update_ffmpeg_output(config) ||
  1982. update_reconnect(config)) {
  1983. config_save_safe(config, "tmp",
  1984. nullptr);
  1985. }
  1986. }
  1987. if (config) {
  1988. const char *sEnc = config_get_string(
  1989. config, "AdvOut", "Encoder");
  1990. const char *rEnc = config_get_string(
  1991. config, "AdvOut", "RecEncoder");
  1992. /* replace "cbr" option with "rate_control" for
  1993. * each profile's encoder data */
  1994. path[pathlen] = 0;
  1995. strcat(path, "/");
  1996. strcat(path, ent->d_name);
  1997. strcat(path, "/recordEncoder.json");
  1998. convert_14_2_encoder_setting(rEnc, path);
  1999. path[pathlen] = 0;
  2000. strcat(path, "/");
  2001. strcat(path, ent->d_name);
  2002. strcat(path, "/streamEncoder.json");
  2003. convert_14_2_encoder_setting(sEnc, path);
  2004. }
  2005. path[pathlen] = 0;
  2006. }
  2007. ent = os_readdir(dir);
  2008. }
  2009. os_closedir(dir);
  2010. }
  2011. void ctrlc_handler(int s)
  2012. {
  2013. UNUSED_PARAMETER(s);
  2014. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  2015. main->close();
  2016. }
  2017. int main(int argc, char *argv[])
  2018. {
  2019. #ifndef _WIN32
  2020. signal(SIGPIPE, SIG_IGN);
  2021. struct sigaction sig_handler;
  2022. sig_handler.sa_handler = ctrlc_handler;
  2023. sigemptyset(&sig_handler.sa_mask);
  2024. sig_handler.sa_flags = 0;
  2025. sigaction(SIGINT, &sig_handler, NULL);
  2026. /* Block SIGPIPE in all threads, this can happen if a thread calls write on
  2027. a closed pipe. */
  2028. sigset_t sigpipe_mask;
  2029. sigemptyset(&sigpipe_mask);
  2030. sigaddset(&sigpipe_mask, SIGPIPE);
  2031. sigset_t saved_mask;
  2032. if (pthread_sigmask(SIG_BLOCK, &sigpipe_mask, &saved_mask) == -1) {
  2033. perror("pthread_sigmask");
  2034. exit(1);
  2035. }
  2036. #endif
  2037. #ifdef _WIN32
  2038. obs_init_win32_crash_handler();
  2039. SetErrorMode(SEM_FAILCRITICALERRORS);
  2040. load_debug_privilege();
  2041. base_set_crash_handler(main_crash_handler, nullptr);
  2042. #endif
  2043. base_get_log_handler(&def_log_handler, nullptr);
  2044. #if defined(USE_XDG) && defined(IS_UNIX)
  2045. move_to_xdg();
  2046. #endif
  2047. obs_set_cmdline_args(argc, argv);
  2048. for (int i = 1; i < argc; i++) {
  2049. if (arg_is(argv[i], "--portable", "-p")) {
  2050. portable_mode = true;
  2051. } else if (arg_is(argv[i], "--multi", "-m")) {
  2052. multi = true;
  2053. } else if (arg_is(argv[i], "--verbose", nullptr)) {
  2054. log_verbose = true;
  2055. } else if (arg_is(argv[i], "--always-on-top", nullptr)) {
  2056. opt_always_on_top = true;
  2057. } else if (arg_is(argv[i], "--unfiltered_log", nullptr)) {
  2058. unfiltered_log = true;
  2059. } else if (arg_is(argv[i], "--startstreaming", nullptr)) {
  2060. opt_start_streaming = true;
  2061. } else if (arg_is(argv[i], "--startrecording", nullptr)) {
  2062. opt_start_recording = true;
  2063. } else if (arg_is(argv[i], "--startreplaybuffer", nullptr)) {
  2064. opt_start_replaybuffer = true;
  2065. } else if (arg_is(argv[i], "--startvirtualcam", nullptr)) {
  2066. opt_start_virtualcam = true;
  2067. } else if (arg_is(argv[i], "--collection", nullptr)) {
  2068. if (++i < argc)
  2069. opt_starting_collection = argv[i];
  2070. } else if (arg_is(argv[i], "--profile", nullptr)) {
  2071. if (++i < argc)
  2072. opt_starting_profile = argv[i];
  2073. } else if (arg_is(argv[i], "--scene", nullptr)) {
  2074. if (++i < argc)
  2075. opt_starting_scene = argv[i];
  2076. } else if (arg_is(argv[i], "--minimize-to-tray", nullptr)) {
  2077. opt_minimize_tray = true;
  2078. } else if (arg_is(argv[i], "--studio-mode", nullptr)) {
  2079. opt_studio_mode = true;
  2080. } else if (arg_is(argv[i], "--allow-opengl", nullptr)) {
  2081. opt_allow_opengl = true;
  2082. } else if (arg_is(argv[i], "--disable-updater", nullptr)) {
  2083. opt_disable_updater = true;
  2084. } else if (arg_is(argv[i], "--help", "-h")) {
  2085. std::string help =
  2086. "--help, -h: Get list of available commands.\n\n"
  2087. "--startstreaming: Automatically start streaming.\n"
  2088. "--startrecording: Automatically start recording.\n"
  2089. "--startreplaybuffer: Start replay buffer.\n"
  2090. "--startvirtualcam: Start virtual camera (if available).\n\n"
  2091. "--collection <string>: Use specific scene collection."
  2092. "\n"
  2093. "--profile <string>: Use specific profile.\n"
  2094. "--scene <string>: Start with specific scene.\n\n"
  2095. "--studio-mode: Enable studio mode.\n"
  2096. "--minimize-to-tray: Minimize to system tray.\n"
  2097. "--portable, -p: Use portable mode.\n"
  2098. "--multi, -m: Don't warn when launching multiple instances.\n\n"
  2099. "--verbose: Make log more verbose.\n"
  2100. "--always-on-top: Start in 'always on top' mode.\n\n"
  2101. "--unfiltered_log: Make log unfiltered.\n\n"
  2102. "--disable-updater: Disable built-in updater (Windows/Mac only)\n\n";
  2103. #ifdef _WIN32
  2104. MessageBoxA(NULL, help.c_str(), "Help",
  2105. MB_OK | MB_ICONASTERISK);
  2106. #else
  2107. std::cout << help
  2108. << "--version, -V: Get current version.\n";
  2109. #endif
  2110. exit(0);
  2111. } else if (arg_is(argv[i], "--version", "-V")) {
  2112. std::cout << "OBS Studio - "
  2113. << App()->GetVersionString() << "\n";
  2114. exit(0);
  2115. }
  2116. }
  2117. #if !OBS_UNIX_STRUCTURE
  2118. if (!portable_mode) {
  2119. portable_mode =
  2120. os_file_exists(BASE_PATH "/portable_mode") ||
  2121. os_file_exists(BASE_PATH "/obs_portable_mode") ||
  2122. os_file_exists(BASE_PATH "/portable_mode.txt") ||
  2123. os_file_exists(BASE_PATH "/obs_portable_mode.txt");
  2124. }
  2125. if (!opt_disable_updater) {
  2126. opt_disable_updater =
  2127. os_file_exists(BASE_PATH "/disable_updater") ||
  2128. os_file_exists(BASE_PATH "/disable_updater.txt");
  2129. }
  2130. #endif
  2131. upgrade_settings();
  2132. fstream logFile;
  2133. curl_global_init(CURL_GLOBAL_ALL);
  2134. int ret = run_program(logFile, argc, argv);
  2135. blog(LOG_INFO, "Number of memory leaks: %ld", bnum_allocs());
  2136. base_set_log_handler(nullptr, nullptr);
  2137. return ret;
  2138. }