main.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. {"desktop", "Start in desktop mode"},
  84. {"tv", "Start in TV mode"},
  85. {"windowed", "Start in windowed mode"},
  86. {"fullscreen", "Start in fullscreen"},
  87. {"terminal", "Log to terminal"},
  88. {"disable-gpu", "Disable QtWebEngine gpu accel"}});
  89. auto scaleOption = QCommandLineOption("scale-factor", "Set to a integer or default auto which controls" \
  90. "the scale (DPI) of the desktop interface.");
  91. scaleOption.setValueName("scale");
  92. scaleOption.setDefaultValue("auto");
  93. auto devOption = QCommandLineOption("remote-debugging-port", "Port number for devtools.");
  94. devOption.setValueName("port");
  95. parser.addOption(scaleOption);
  96. parser.addOption(devOption);
  97. char **newArgv = appendCommandLineArguments(argc, argv, g_qtFlags);
  98. int newArgc = argc + g_qtFlags.size();
  99. // Qt calls setlocale(LC_ALL, "") in a bunch of places, which breaks
  100. // float/string processing in mpv and ffmpeg.
  101. #ifdef Q_OS_UNIX
  102. qputenv("LC_ALL", "C");
  103. qputenv("LC_NUMERIC", "C");
  104. #endif
  105. preinitQt();
  106. detectOpenGLEarly();
  107. QStringList arguments;
  108. for (int i = 0; i < argc; i++)
  109. arguments << QString::fromLatin1(argv[i]);
  110. {
  111. // This is kinda dumb. But in order for the QCommandLineParser
  112. // to work properly we need to init if before we call process
  113. // but we don't want to do that for the main application since
  114. // we need to set the scale factor before we do that. So it becomes
  115. // a small chicken-or-egg problem, which we "solve" by making
  116. // this temporary console app.
  117. //
  118. QCoreApplication core(newArgc, newArgv);
  119. // Now parse the command line.
  120. parser.process(arguments);
  121. }
  122. if (parser.isSet("licenses"))
  123. {
  124. ShowLicenseInfo();
  125. return EXIT_SUCCESS;
  126. }
  127. auto scale = parser.value("scale-factor");
  128. if (scale.isEmpty() || scale == "auto")
  129. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  130. else if (scale != "none")
  131. qputenv("QT_SCALE_FACTOR", scale.toUtf8());
  132. QApplication app(newArgc, newArgv);
  133. #if defined(Q_OS_WIN) || defined(Q_OS_LINUX)
  134. // Setting window icon on OSX will break user ability to change it
  135. app.setWindowIcon(QIcon(":/images/icon.png"));
  136. #endif
  137. #if defined(Q_OS_MAC) && defined(NDEBUG)
  138. PFMoveToApplicationsFolderIfNecessary();
  139. #endif
  140. UniqueApplication* uniqueApp = new UniqueApplication();
  141. if (!uniqueApp->ensureUnique())
  142. return EXIT_SUCCESS;
  143. #ifdef Q_OS_UNIX
  144. // install signals handlers for proper app closing.
  145. SignalManager signalManager(&app);
  146. Q_UNUSED(signalManager);
  147. #endif
  148. Log::Init();
  149. if (parser.isSet("terminal"))
  150. Log::EnableTerminalOutput();
  151. detectOpenGLLate();
  152. Codecs::preinitCodecs();
  153. // Initialize all the components. This needs to be done
  154. // early since most everything else relies on it
  155. //
  156. ComponentManager::Get().initialize();
  157. SettingsComponent::Get().setCommandLineValues(parser.optionNames());
  158. QtWebEngine::initialize();
  159. // load QtWebChannel so that we can register our components with it.
  160. QQmlApplicationEngine *engine = Globals::Engine();
  161. KonvergoWindow::RegisterClass();
  162. Globals::SetContextProperty("components", &ComponentManager::Get().getQmlPropertyMap());
  163. // the only way to detect if QML parsing fails is to hook to this signal and then see
  164. // if we get a valid object passed to it. Any error messages will be reported on stderr
  165. // but since no normal user should ever see this it should be fine
  166. //
  167. QObject::connect(engine, &QQmlApplicationEngine::objectCreated, [=](QObject* object, const QUrl& url)
  168. {
  169. Q_UNUSED(url);
  170. if (object == nullptr)
  171. throw FatalException(QObject::tr("Failed to parse application engine script."));
  172. KonvergoWindow* window = Globals::MainWindow();
  173. QObject* webChannel = qvariant_cast<QObject*>(window->property("webChannel"));
  174. Q_ASSERT(webChannel);
  175. ComponentManager::Get().setWebChannel(qobject_cast<QWebChannel*>(webChannel));
  176. QObject::connect(uniqueApp, &UniqueApplication::otherApplicationStarted, window, &KonvergoWindow::otherAppFocus);
  177. });
  178. engine->load(QUrl(QStringLiteral("qrc:/ui/webview.qml")));
  179. Log::UpdateLogLevel();
  180. // run our application
  181. int ret = app.exec();
  182. delete uniqueApp;
  183. Globals::EngineDestroy();
  184. Codecs::Uninit();
  185. Log::Uninit();
  186. return ret;
  187. }
  188. catch (FatalException& e)
  189. {
  190. QLOG_FATAL() << "Unhandled FatalException:" << qPrintable(e.message());
  191. QApplication errApp(argc, argv);
  192. auto msg = new ErrorMessage(e.message(), true);
  193. msg->show();
  194. errApp.exec();
  195. Codecs::Uninit();
  196. Log::Uninit();
  197. return 1;
  198. }
  199. }