SystemComponent.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include <QSysInfo>
  2. #include <QProcess>
  3. #include <QMap>
  4. #include <QtNetwork/qnetworkinterface.h>
  5. #include <QGuiApplication>
  6. #include <QDesktopServices>
  7. #include <QDir>
  8. #include <QJsonObject>
  9. #include "input/InputComponent.h"
  10. #include "SystemComponent.h"
  11. #include "Version.h"
  12. #include "QsLog.h"
  13. #include "settings/SettingsComponent.h"
  14. #include "ui/KonvergoWindow.h"
  15. #include "settings/SettingsSection.h"
  16. #include "Paths.h"
  17. #include "Names.h"
  18. #include "utils/Utils.h"
  19. #define MOUSE_TIMEOUT 5 * 1000
  20. #define KONVERGO_PRODUCTID_DEFAULT 3
  21. #define KONVERGO_PRODUCTID_OPENELEC 4
  22. // Platform types map
  23. QMap<SystemComponent::PlatformType, QString> g_platformTypeNames = { \
  24. { SystemComponent::platformTypeOsx, "macosx" }, \
  25. { SystemComponent::platformTypeWindows, "windows" },
  26. { SystemComponent::platformTypeLinux, "linux" },
  27. { SystemComponent::platformTypeOpenELEC, "openelec" },
  28. { SystemComponent::platformTypeUnknown, "unknown" },
  29. };
  30. // platform Archictecture map
  31. QMap<SystemComponent::PlatformArch, QString> g_platformArchNames = {
  32. { SystemComponent::platformArchX86_32, "i386" },
  33. { SystemComponent::platformArchX86_64, "x86_64" },
  34. { SystemComponent::platformArchRpi2, "rpi2" },
  35. { SystemComponent::platformArchUnknown, "unknown" }
  36. };
  37. ///////////////////////////////////////////////////////////////////////////////////////////////////
  38. SystemComponent::SystemComponent(QObject* parent) : ComponentBase(parent), m_platformType(platformTypeUnknown), m_platformArch(platformArchUnknown), m_doLogMessages(false), m_cursorVisible(true), m_scale(1)
  39. {
  40. m_mouseOutTimer = new QTimer(this);
  41. m_mouseOutTimer->setSingleShot(true);
  42. connect(m_mouseOutTimer, &QTimer::timeout, [&] () { setCursorVisibility(false); });
  43. // define OS Type
  44. #if defined(Q_OS_MAC)
  45. m_platformType = platformTypeOsx;
  46. #elif defined(Q_OS_WIN)
  47. m_platformType = platformTypeWindows;
  48. #elif defined(KONVERGO_OPENELEC)
  49. m_platformType = platformTypeOpenELEC;
  50. #elif defined(Q_OS_LINUX)
  51. m_platformType = platformTypeLinux;
  52. #endif
  53. // define target type
  54. #if TARGET_RPI
  55. m_platformArch = platformArchRpi2;
  56. #elif defined(Q_PROCESSOR_X86_32)
  57. m_platformArch = platformArchX86_32;
  58. #elif defined(Q_PROCESSOR_X86_64)
  59. m_platformArch = platformArchX86_64;
  60. #endif
  61. connect(SettingsComponent::Get().getSection(SETTINGS_SECTION_AUDIO), &SettingsSection::valuesUpdated, [=]()
  62. {
  63. emit capabilitiesChanged(getCapabilitiesString());
  64. });
  65. }
  66. /////////////////////////////////////////////////////////////////////////////////////////
  67. bool SystemComponent::componentInitialize()
  68. {
  69. QDir().mkpath(Paths::dataDir("scripts"));
  70. QDir().mkpath(Paths::dataDir("sounds"));
  71. // Hide mouse pointer on any keyboard input
  72. connect(&InputComponent::Get(), &InputComponent::receivedInput, [=]() { setCursorVisibility(false); });
  73. return true;
  74. }
  75. /////////////////////////////////////////////////////////////////////////////////////////
  76. void SystemComponent::crashApp()
  77. {
  78. *(volatile int*)nullptr=0;
  79. }
  80. /////////////////////////////////////////////////////////////////////////////////////////
  81. void SystemComponent::componentPostInitialize()
  82. {
  83. InputComponent::Get().registerHostCommand("crash!", this, "crashApp");
  84. InputComponent::Get().registerHostCommand("script", this, "runUserScript");
  85. InputComponent::Get().registerHostCommand("message", this, "hostMessage");
  86. }
  87. ///////////////////////////////////////////////////////////////////////////////////////////////////
  88. QString SystemComponent::getPlatformTypeString() const
  89. {
  90. return g_platformTypeNames[m_platformType];
  91. }
  92. ///////////////////////////////////////////////////////////////////////////////////////////////////
  93. QString SystemComponent::getPlatformArchString() const
  94. {
  95. return g_platformArchNames[m_platformArch];
  96. }
  97. ///////////////////////////////////////////////////////////////////////////////////////////////////
  98. QVariantMap SystemComponent::systemInformation() const
  99. {
  100. QVariantMap info;
  101. QString build;
  102. QString dist;
  103. QString arch;
  104. int productid = KONVERGO_PRODUCTID_DEFAULT;
  105. #ifdef Q_OS_WIN
  106. arch = (sizeof(void *) == 8) ? "x86_64" : "i386";
  107. #else
  108. arch = QSysInfo::currentCpuArchitecture();
  109. #endif
  110. build = getPlatformTypeString();
  111. dist = getPlatformTypeString();
  112. #if defined(KONVERGO_OPENELEC)
  113. productid = KONVERGO_PRODUCTID_OPENELEC;
  114. dist = "openelec";
  115. if (m_platformArch == platformArchRpi2)
  116. {
  117. build = "rpi2";
  118. }
  119. else
  120. {
  121. build = "generic";
  122. }
  123. #endif
  124. info["build"] = build + "-" + arch;
  125. info["dist"] = dist;
  126. info["version"] = Version::GetVersionString();
  127. info["productid"] = productid;
  128. QLOG_DEBUG() << QString(
  129. "System Information : build(%1)-arch(%2).dist(%3).version(%4).productid(%5)")
  130. .arg(build)
  131. .arg(arch)
  132. .arg(dist)
  133. .arg(Version::GetVersionString())
  134. .arg(productid);
  135. return info;
  136. }
  137. ///////////////////////////////////////////////////////////////////////////////////////////////////
  138. void SystemComponent::exit()
  139. {
  140. qApp->quit();
  141. }
  142. ///////////////////////////////////////////////////////////////////////////////////////////////////
  143. void SystemComponent::restart()
  144. {
  145. qApp->quit();
  146. QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
  147. }
  148. ///////////////////////////////////////////////////////////////////////////////////////////////////
  149. void SystemComponent::info(QString text)
  150. {
  151. if (QsLogging::Logger::instance().loggingLevel() <= QsLogging::InfoLevel)
  152. QsLogging::Logger::Helper(QsLogging::InfoLevel).stream() << "JS:" << qPrintable(text);
  153. }
  154. ///////////////////////////////////////////////////////////////////////////////////////////////////
  155. void SystemComponent::setCursorVisibility(bool visible)
  156. {
  157. if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "webMode") == "desktop")
  158. visible = true;
  159. if (visible == m_cursorVisible)
  160. return;
  161. m_cursorVisible = visible;
  162. if (visible)
  163. {
  164. qApp->restoreOverrideCursor();
  165. m_mouseOutTimer->start(MOUSE_TIMEOUT);
  166. }
  167. else
  168. {
  169. qApp->setOverrideCursor(QCursor(Qt::BlankCursor));
  170. m_mouseOutTimer->stop();
  171. }
  172. #ifdef Q_OS_MAC
  173. // OSX notifications will reset the cursor image (without Qt's knowledge). The
  174. // only thing we can do override this is using Cocoa's native cursor hiding.
  175. OSXUtils::SetCursorVisible(visible);
  176. #endif
  177. }
  178. ///////////////////////////////////////////////////////////////////////////////////////////////////
  179. QString SystemComponent::getUserAgent()
  180. {
  181. QString osVersion = QSysInfo::productVersion();
  182. QString userAgent = QString("JellyfinMediaPlayer %1 (%2-%3 %4)").arg(Version::GetVersionString()).arg(getPlatformTypeString()).arg(getPlatformArchString()).arg(osVersion);
  183. return userAgent;
  184. }
  185. /////////////////////////////////////////////////////////////////////////////////////////
  186. QString SystemComponent::debugInformation()
  187. {
  188. QString debugInfo;
  189. QTextStream stream(&debugInfo);
  190. stream << "Jellyfin Media Player\n";
  191. stream << " Version: " << Version::GetVersionString() << " built: " << Version::GetBuildDate() << "\n";
  192. stream << " Web Client Version: " << Version::GetWebVersion() << "\n";
  193. stream << " Web Client URL: " << SettingsComponent::Get().value(SETTINGS_SECTION_PATH, "startupurl").toString() << "\n";
  194. stream << " Platform: " << getPlatformTypeString() << "-" << getPlatformArchString() << "\n";
  195. stream << " User-Agent: " << getUserAgent() << "\n";
  196. stream << " Qt version: " << qVersion() << QString("(%1)").arg(Version::GetQtDepsVersion()) << "\n";
  197. stream << " Depends version: " << Version::GetDependenciesVersion() << "\n";
  198. stream << "\n";
  199. stream << "Files\n";
  200. stream << " Log file: " << Paths::logDir(Names::MainName() + ".log") << "\n";
  201. stream << " Config file: " << Paths::dataDir(Names::MainName() + ".conf") << "\n";
  202. stream << "\n";
  203. stream << "Network Addresses\n";
  204. for(const QString& addr : networkAddresses())
  205. {
  206. stream << " " << addr << "\n";
  207. }
  208. stream << "\n";
  209. stream.flush();
  210. return debugInfo;
  211. }
  212. /////////////////////////////////////////////////////////////////////////////////////////
  213. QStringList SystemComponent::networkAddresses() const
  214. {
  215. QStringList list;
  216. for(const QHostAddress& address : QNetworkInterface::allAddresses())
  217. {
  218. if (! address.isLoopback() && (address.protocol() == QAbstractSocket::IPv4Protocol ||
  219. address.protocol() == QAbstractSocket::IPv6Protocol))
  220. {
  221. auto s = address.toString();
  222. if (!s.startsWith("fe80::"))
  223. list << s;
  224. }
  225. }
  226. return list;
  227. }
  228. /////////////////////////////////////////////////////////////////////////////////////////
  229. void SystemComponent::openExternalUrl(const QString& url)
  230. {
  231. QDesktopServices::openUrl(QUrl(url));
  232. }
  233. /////////////////////////////////////////////////////////////////////////////////////////
  234. void SystemComponent::runUserScript(QString script)
  235. {
  236. // We take the path the user supplied and run it through fileInfo and
  237. // look for the fileName() part, this is to avoid people sharing keymaps
  238. // that tries to execute things like ../../ etc. Note that this function
  239. // is still not safe, people can do nasty things with it, so users needs
  240. // to be careful with their keymaps.
  241. //
  242. QFileInfo fi(script);
  243. QString scriptPath = Paths::dataDir("scripts/" + fi.fileName());
  244. QFile scriptFile(scriptPath);
  245. if (scriptFile.exists())
  246. {
  247. if (!QFileInfo(scriptFile).isExecutable())
  248. {
  249. QLOG_WARN() << "Script:" << script << "is not executable";
  250. return;
  251. }
  252. QLOG_INFO() << "Running script:" << scriptPath;
  253. if (QProcess::startDetached(scriptPath, QStringList()))
  254. QLOG_DEBUG() << "Script started successfully";
  255. else
  256. QLOG_WARN() << "Error running script:" << scriptPath;
  257. }
  258. else
  259. {
  260. QLOG_WARN() << "Could not find script:" << scriptPath;
  261. }
  262. }
  263. /////////////////////////////////////////////////////////////////////////////////////////
  264. void SystemComponent::hello(const QString& version)
  265. {
  266. QLOG_DEBUG() << QString("Web-client (%1) fully inited.").arg(version);
  267. m_webClientVersion = version;
  268. }
  269. /////////////////////////////////////////////////////////////////////////////////////////
  270. QString SystemComponent::getNativeShellScript()
  271. {
  272. auto path = SettingsComponent::Get().getExtensionPath();
  273. QLOG_DEBUG() << QString("Using path for extension: %1").arg(path);
  274. QFile file {path + "nativeshell.js"};
  275. file.open(QIODevice::ReadOnly);
  276. auto nativeshellString = QTextStream(&file).readAll();
  277. QJsonObject clientData;
  278. clientData.insert("deviceName", QJsonValue::fromVariant(SettingsComponent::Get().getClientName()));
  279. clientData.insert("scriptPath", QJsonValue::fromVariant("file:///" + path));
  280. clientData.insert("mode", QJsonValue::fromVariant(SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "layout").toString()));
  281. nativeshellString.replace("@@data@@", QJsonDocument(clientData).toJson(QJsonDocument::Compact).toBase64());
  282. return nativeshellString;
  283. }
  284. /////////////////////////////////////////////////////////////////////////////////////////
  285. #define BASESTR "protocols=shoutcast,http-video;videoDecoders=h264{profile:high&resolution:2160&level:52};audioDecoders=mp3,aac,dts{bitrate:800000&channels:%1},ac3{bitrate:800000&channels:%2}"
  286. /////////////////////////////////////////////////////////////////////////////////////////
  287. QString SystemComponent::getCapabilitiesString()
  288. {
  289. auto capstring = QString(BASESTR);
  290. auto channels = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "channels").toString();
  291. auto dtsenabled = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "passthrough.dts").toBool();
  292. auto ac3enabled = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "passthrough.ac3").toBool();
  293. // Assume that auto means that we want to select multi-channel tracks by default.
  294. // So really only disable it when 2.0 is selected.
  295. //
  296. int ac3channels = 2;
  297. int dtschannels = 2;
  298. if (channels != "2.0")
  299. dtschannels = ac3channels = 8;
  300. else if (dtsenabled)
  301. dtschannels = 8;
  302. else if (ac3enabled)
  303. ac3channels = 8;
  304. return capstring.arg(dtschannels).arg(ac3channels);
  305. }