main.cpp 8.4 KB

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