obs-app.cpp 64 KB

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