main.cpp 8.2 KB

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