main.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #include <locale.h>
  2. #include <QGuiApplication>
  3. #include <QApplication>
  4. #include <QFileInfo>
  5. #include <QIcon>
  6. #include <QtQml>
  7. #include <QtWebEngine/qtwebengineglobal.h>
  8. #include <QErrorMessage>
  9. #include <QCommandLineOption>
  10. #include "shared/Names.h"
  11. #include "system/SystemComponent.h"
  12. #include "system/UpdateManager.h"
  13. #include "system/UpdaterComponent.h"
  14. #include "QsLog.h"
  15. #include "Paths.h"
  16. #include "player/CodecsComponent.h"
  17. #include "player/PlayerComponent.h"
  18. #include "player/OpenGLDetect.h"
  19. #include "breakpad/CrashDumps.h"
  20. #include "Version.h"
  21. #include "settings/SettingsComponent.h"
  22. #include "settings/SettingsSection.h"
  23. #include "ui/KonvergoWindow.h"
  24. #include "ui/KonvergoWindow.h"
  25. #include "Globals.h"
  26. #include "ui/ErrorMessage.h"
  27. #include "UniqueApplication.h"
  28. #include "utils/HelperLauncher.h"
  29. #include "utils/Log.h"
  30. #ifdef Q_OS_MAC
  31. #include "PFMoveApplication.h"
  32. #endif
  33. #if defined(Q_OS_MAC) || defined(Q_OS_LINUX)
  34. #include "SignalManager.h"
  35. #endif
  36. /////////////////////////////////////////////////////////////////////////////////////////
  37. static void preinitQt()
  38. {
  39. QCoreApplication::setApplicationName(Names::MainName());
  40. QCoreApplication::setApplicationVersion(Version::GetVersionString());
  41. QCoreApplication::setOrganizationDomain("plex.tv");
  42. #ifdef Q_OS_WIN32
  43. QVariant useOpengl = SettingsComponent::readPreinitValue(SETTINGS_SECTION_MAIN, "useOpenGL");
  44. // Warning: this must be the same as the default value as declared in
  45. // the settings_description.json file, or confusion will result.
  46. if (useOpengl.type() != QMetaType::Bool)
  47. useOpengl = false;
  48. if (useOpengl.toBool())
  49. QCoreApplication::setAttribute(Qt::AA_UseDesktopOpenGL);
  50. else
  51. QCoreApplication::setAttribute(Qt::AA_UseOpenGLES);
  52. #endif
  53. }
  54. /////////////////////////////////////////////////////////////////////////////////////////
  55. char** appendCommandLineArguments(int argc, char **argv, const QStringList& args)
  56. {
  57. size_t newSize = (argc + args.length() + 1) * sizeof(char*);
  58. char** newArgv = (char**)calloc(1, newSize);
  59. memcpy(newArgv, argv, (size_t)(argc * sizeof(char*)));
  60. int pos = argc;
  61. for(const QString& str : args)
  62. newArgv[pos++] = qstrdup(str.toUtf8().data());
  63. return newArgv;
  64. }
  65. /////////////////////////////////////////////////////////////////////////////////////////
  66. void ShowLicenseInfo()
  67. {
  68. QFile licenses(":/misc/licenses.txt");
  69. licenses.open(QIODevice::ReadOnly | QIODevice::Text);
  70. QByteArray contents = licenses.readAll();
  71. printf("%.*s\n", contents.size(), contents.data());
  72. }
  73. /////////////////////////////////////////////////////////////////////////////////////////
  74. QStringList g_qtFlags = {
  75. "--disable-gpu",
  76. "--disable-web-security"
  77. };
  78. /////////////////////////////////////////////////////////////////////////////////////////
  79. int main(int argc, char *argv[])
  80. {
  81. try
  82. {
  83. QCommandLineParser parser;
  84. parser.setApplicationDescription("Plex Media Player");
  85. parser.addHelpOption();
  86. parser.addVersionOption();
  87. parser.addOptions({{{"l", "licenses"}, "Show license information"},
  88. {{"a", "from-auto-update"}, "When invoked from auto-update"},
  89. {"desktop", "Start in desktop mode"},
  90. {"tv", "Start in TV mode"},
  91. {"auto-layout", "Use auto-layout mode"},
  92. {"windowed", "Start in windowed mode"},
  93. {"fullscreen", "Start in fullscreen"},
  94. {"no-updates", "Disable auto-updating"},
  95. {"terminal", "Log to terminal"}});
  96. auto scaleOption = QCommandLineOption("scale-factor", "Set to a integer or default auto which controls" \
  97. "the scale (DPI) of the desktop interface.");
  98. scaleOption.setValueName("scale");
  99. scaleOption.setDefaultValue("auto");
  100. parser.addOption(scaleOption);
  101. char **newArgv = appendCommandLineArguments(argc, argv, g_qtFlags);
  102. int newArgc = argc + g_qtFlags.size();
  103. // Suppress SSL related warnings on OSX
  104. // See https://bugreports.qt.io/browse/QTBUG-43173 for more info
  105. //
  106. #ifdef Q_OS_MAC
  107. qputenv("QT_LOGGING_RULES", "qt.network.ssl.warning=false");
  108. #endif
  109. // Qt calls setlocale(LC_ALL, "") in a bunch of places, which breaks
  110. // float/string processing in mpv and ffmpeg.
  111. #ifdef Q_OS_UNIX
  112. qputenv("LC_ALL", "C");
  113. qputenv("LC_NUMERIC", "C");
  114. #endif
  115. preinitQt();
  116. detectOpenGLEarly();
  117. QStringList arguments;
  118. for (int i = 0; i < argc; i++)
  119. arguments << QString::fromLatin1(argv[i]);
  120. {
  121. // This is kinda dumb. But in order for the QCommandLineParser
  122. // to work properly we need to init if before we call process
  123. // but we don't want to do that for the main application since
  124. // we need to set the scale factor before we do that. So it becomes
  125. // a small chicken-or-egg problem, which we "solve" by making
  126. // this temporary console app.
  127. //
  128. QCoreApplication core(newArgc, newArgv);
  129. // Now parse the command line.
  130. parser.process(arguments);
  131. }
  132. if (parser.isSet("licenses"))
  133. {
  134. ShowLicenseInfo();
  135. return EXIT_SUCCESS;
  136. }
  137. auto scale = parser.value("scale-factor");
  138. if (scale.isEmpty() || scale == "auto")
  139. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  140. else if (scale != "none")
  141. qputenv("QT_SCALE_FACTOR", scale.toUtf8());
  142. QApplication app(newArgc, newArgv);
  143. app.setWindowIcon(QIcon(":/images/icon.png"));
  144. #if defined(Q_OS_MAC) && defined(NDEBUG)
  145. PFMoveToApplicationsFolderIfNecessary();
  146. #endif
  147. // init breakpad.
  148. setupCrashDumper();
  149. UniqueApplication* uniqueApp = new UniqueApplication();
  150. if (!uniqueApp->ensureUnique())
  151. return EXIT_SUCCESS;
  152. #ifdef Q_OS_UNIX
  153. // install signals handlers for proper app closing.
  154. SignalManager signalManager(&app);
  155. Q_UNUSED(signalManager);
  156. #endif
  157. Log::Init();
  158. if (parser.isSet("terminal"))
  159. Log::EnableTerminalOutput();
  160. // Quit app and apply update if we find one.
  161. if (!parser.isSet("no-updates") && UpdateManager::CheckForUpdates())
  162. {
  163. app.quit();
  164. return 0;
  165. }
  166. detectOpenGLLate();
  167. Codecs::preinitCodecs();
  168. // Initialize all the components. This needs to be done
  169. // early since most everything else relies on it
  170. //
  171. ComponentManager::Get().initialize();
  172. if (parser.isSet("no-updates"))
  173. UpdaterComponent::Get().disable();
  174. SettingsComponent::Get().setCommandLineValues(parser.optionNames());
  175. // enable remote inspection if we have the correct setting for it.
  176. if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "remoteInspector").toBool())
  177. qputenv("QTWEBENGINE_REMOTE_DEBUGGING", "0.0.0.0:9992");
  178. QtWebEngine::initialize();
  179. // start our helper
  180. #if ENABLE_HELPER
  181. HelperLauncher::Get().connectToHelper();
  182. #endif
  183. // load QtWebChannel so that we can register our components with it.
  184. QQmlApplicationEngine *engine = Globals::Engine();
  185. KonvergoWindow::RegisterClass();
  186. Globals::SetContextProperty("components", &ComponentManager::Get().getQmlPropertyMap());
  187. // the only way to detect if QML parsing fails is to hook to this signal and then see
  188. // if we get a valid object passed to it. Any error messages will be reported on stderr
  189. // but since no normal user should ever see this it should be fine
  190. //
  191. QObject::connect(engine, &QQmlApplicationEngine::objectCreated, [=](QObject* object, const QUrl& url)
  192. {
  193. Q_UNUSED(url);
  194. if (object == nullptr)
  195. throw FatalException(QObject::tr("Failed to parse application engine script."));
  196. KonvergoWindow* window = Globals::MainWindow();
  197. QObject* webChannel = qvariant_cast<QObject*>(window->property("webChannel"));
  198. Q_ASSERT(webChannel);
  199. ComponentManager::Get().setWebChannel(qobject_cast<QWebChannel*>(webChannel));
  200. QObject::connect(uniqueApp, &UniqueApplication::otherApplicationStarted, window, &KonvergoWindow::otherAppFocus);
  201. });
  202. engine->load(QUrl(QStringLiteral("qrc:/ui/webview.qml")));
  203. Log::UpdateLogLevel();
  204. // run our application
  205. int ret = app.exec();
  206. delete uniqueApp;
  207. Globals::EngineDestroy();
  208. Codecs::Uninit();
  209. Log::Uninit();
  210. return ret;
  211. }
  212. catch (FatalException& e)
  213. {
  214. QLOG_FATAL() << "Unhandled FatalException:" << qPrintable(e.message());
  215. QApplication errApp(argc, argv);
  216. auto msg = new ErrorMessage(e.message(), true);
  217. msg->show();
  218. errApp.exec();
  219. Codecs::Uninit();
  220. Log::Uninit();
  221. return 1;
  222. }
  223. }