main.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /****************************************************************************
  2. ** Artriculate: Art comes tumbling down
  3. ** Copyright (C) 2016 Chaos Reins
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ****************************************************************************/
  18. #include "picturemodel.h"
  19. #ifdef USING_SYSTEMD
  20. #include <systemd/sd-daemon.h>
  21. #endif
  22. #include <QFontDatabase>
  23. #include <QGuiApplication>
  24. #include <QQuickView>
  25. #include <QQmlContext>
  26. #include <QQmlEngine>
  27. #include <QSettings>
  28. #include <QSurfaceFormat>
  29. #include <QTimer>
  30. #include <QQuickWindow>
  31. #include <QDir>
  32. #include <QFileInfo>
  33. #include <QTextStream>
  34. #include <QDebug>
  35. #include <QScreen>
  36. #include <QDBusInterface>
  37. #include <QDBusConnection>
  38. #include <QFileSystemWatcher>
  39. #include <QtPlugin>
  40. #include <QMetaObject>
  41. class CloseEventFilter : public QObject
  42. {
  43. Q_OBJECT
  44. public:
  45. CloseEventFilter(QObject *p) : QObject(p) { /**/ }
  46. protected:
  47. bool eventFilter(QObject *obj, QEvent *event);
  48. };
  49. bool CloseEventFilter::eventFilter(QObject *obj, QEvent *event)
  50. {
  51. if (event->type() == QEvent::Close) {
  52. qApp->quit();
  53. }
  54. return QObject::eventFilter(obj, event);
  55. }
  56. class NativeUtils : public QObject {
  57. Q_OBJECT
  58. Q_PROPERTY(bool rebootRequired MEMBER rebootRequired NOTIFY rebootRequiredChanged)
  59. public:
  60. NativeUtils(QObject *p);
  61. signals:
  62. void rebootRequiredChanged();
  63. public slots:
  64. void monitorRunPath(const QString &path);
  65. private:
  66. QString watchFile;
  67. QFileSystemWatcher runDirWatcher;
  68. bool rebootRequired;
  69. };
  70. NativeUtils::NativeUtils(QObject *p)
  71. : QObject(p),
  72. watchFile("/run/reboot-required"),
  73. rebootRequired(false)
  74. {
  75. runDirWatcher.addPath(QFileInfo(watchFile).absolutePath());
  76. connect(&runDirWatcher, &QFileSystemWatcher::directoryChanged, this, &NativeUtils::monitorRunPath);
  77. monitorRunPath("");
  78. }
  79. void NativeUtils::monitorRunPath(const QString &path)
  80. {
  81. Q_UNUSED(path);
  82. rebootRequired = QFileInfo::exists(watchFile);
  83. emit rebootRequiredChanged();
  84. }
  85. class ArtView {
  86. public:
  87. static void populateScreen(QScreen *screen = nullptr);
  88. private:
  89. static QQmlEngine* sharedQmlEngine;
  90. };
  91. QQmlEngine* ArtView::sharedQmlEngine = nullptr;
  92. void ArtView::populateScreen(QScreen *screen)
  93. {
  94. static QString qmlPath;
  95. #ifdef COMPILED_RESOURCES
  96. qmlPath = "qrc:/qml";
  97. #else
  98. qmlPath = QCoreApplication::applicationDirPath() % "/qml";
  99. if (!QDir(qmlPath).exists()) {
  100. qmlPath = "/usr/share/" % qApp->applicationName() % "/qml";
  101. }
  102. #endif
  103. QQuickView *view;
  104. if (sharedQmlEngine) {
  105. view = new QQuickView(sharedQmlEngine, nullptr);
  106. } else {
  107. view = new QQuickView();
  108. sharedQmlEngine = view->engine();
  109. sharedQmlEngine->addImportPath(qmlPath);
  110. sharedQmlEngine->rootContext()->setContextProperty("nativeUtils", new NativeUtils(sharedQmlEngine));
  111. sharedQmlEngine->rootContext()->setContextProperty("imageModel", new PictureModel(sharedQmlEngine));
  112. QObject::connect(sharedQmlEngine, &QQmlEngine::quit, qApp, &QCoreApplication::quit);
  113. }
  114. if (screen) {
  115. view->setScreen(screen);
  116. } else {
  117. screen = view->screen();
  118. }
  119. QSettings settings;
  120. QRect geometry = screen->availableGeometry();
  121. bool transparentToplevel = settings.value("transparentToplevel", false).toBool();
  122. settings.setValue("transparentToplevel", transparentToplevel);
  123. if (transparentToplevel) {
  124. view->setColor(Qt::transparent);
  125. } else {
  126. view->setColor(Qt::black);
  127. }
  128. view->setResizeMode(QQuickView::SizeRootObjectToView);
  129. view->setSource(QUrl(qmlPath + "/main.qml"));
  130. view->setGeometry(geometry);
  131. view->showFullScreen();
  132. QObject::connect(view, &QQuickView::statusChanged, [](QQuickView::Status status) {
  133. if (status == QQuickView::Error) {
  134. QMetaObject::invokeMethod(qApp, "quit", Qt::QueuedConnection);
  135. }
  136. });
  137. qDebug() << "Displaying artwork on" << screen << "with geometry" << geometry;
  138. }
  139. int main(int argc, char *argv[])
  140. {
  141. const char *kms_screen_config_env_var = "QT_QPA_EGLFS_KMS_CONFIG";
  142. // Specify an explicit kms configuration rather than respecting fbset
  143. //if(qEnvironmentVariableIsEmpty(kms_screen_config_env_var))
  144. // qputenv(kms_screen_config_env_var, ":/kms-screen.json");
  145. #ifdef STATIC_BUILD
  146. Q_IMPORT_PLUGIN(QmlSettingsPlugin)
  147. Q_IMPORT_PLUGIN(QtQuick2WindowPlugin)
  148. Q_IMPORT_PLUGIN(QtQuick2Plugin)
  149. #endif
  150. qsrand(time(NULL));
  151. QGuiApplication app(argc, argv);
  152. if (QFontDatabase::addApplicationFont(":/Lato-Regular.ttf") == -1) {
  153. qDebug() << "Failed to successfully add the application font";
  154. }
  155. app.setFont(QFont("Lato Regular"));
  156. app.setOrganizationName("Chaos Reins");
  157. app.setApplicationName("artriculate");
  158. app.installEventFilter(new CloseEventFilter(&app));
  159. QSettings settings;
  160. if (settings.value("raster", false).toBool()) {
  161. #if QT_VERSION < QT_VERSION_CHECK(5, 8, 0)
  162. qDebug() << "Trying to use the SG software backend prior to Qt 5.8";
  163. #else
  164. QQuickWindow::setSceneGraphBackend(QSGRendererInterface::Software);
  165. #endif
  166. } else {
  167. if (settings.value("force24bpp", false).toBool()) {
  168. QSurfaceFormat format = QSurfaceFormat::defaultFormat();
  169. format.setAlphaBufferSize(0);
  170. format.setRedBufferSize(8);
  171. format.setGreenBufferSize(8);
  172. format.setBlueBufferSize(8);
  173. QSurfaceFormat::setDefaultFormat(format);
  174. } else if (settings.value("force16bpp", false).toBool()) {
  175. QSurfaceFormat format = QSurfaceFormat::defaultFormat();
  176. format.setAlphaBufferSize(0);
  177. format.setRedBufferSize(5);
  178. format.setGreenBufferSize(6);
  179. format.setBlueBufferSize(5);
  180. QSurfaceFormat::setDefaultFormat(format);
  181. }
  182. if (settings.value("forceSingleBuffer", false).toBool()) {
  183. QSurfaceFormat format = QSurfaceFormat::defaultFormat();
  184. format.setSwapBehavior(QSurfaceFormat::SingleBuffer);
  185. QSurfaceFormat::setDefaultFormat(format);
  186. } else if (settings.value("forceDoubleBuffer", false).toBool()) {
  187. QSurfaceFormat format = QSurfaceFormat::defaultFormat();
  188. format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
  189. QSurfaceFormat::setDefaultFormat(format);
  190. } else if (settings.value("forceTripleBuffer", false).toBool()) {
  191. QSurfaceFormat format = QSurfaceFormat::defaultFormat();
  192. format.setSwapBehavior(QSurfaceFormat::TripleBuffer);
  193. QSurfaceFormat::setDefaultFormat(format);
  194. }
  195. }
  196. // qdbus org.freedesktop.ScreenSaver /org/freedesktop/ScreenSaver Inhibit "artriculate" "media playback"
  197. if (settings.value("suppressScreensaver", false).toBool()) {
  198. QDBusInterface screenSaver("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver");
  199. uint id = screenSaver.call("Inhibit", app.applicationName(), "Media playback").arguments().at(0).toInt();
  200. QObject::connect(&app, &QCoreApplication::aboutToQuit, [id]() {
  201. QDBusInterface screenSaver("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver");
  202. screenSaver.call("UnInhibit", id);
  203. });
  204. }
  205. qmlRegisterType<PictureModel>("PictureModel", 1, 0, "PictureModel");
  206. int screenIndex = settings.value("screenIndex", -2).toInt();
  207. QList<QScreen*> screens = QGuiApplication::screens();
  208. if (screenIndex == -2) {
  209. ArtView::populateScreen();
  210. } else if (screenIndex == -1) {
  211. foreach(QScreen *screen, screens) {
  212. ArtView::populateScreen(screen);
  213. }
  214. } else {
  215. if ((screenIndex >= 0) && (screenIndex < screens.length())) {
  216. ArtView::populateScreen(screens.at(screenIndex));
  217. } else {
  218. ArtView::populateScreen();
  219. }
  220. }
  221. settings.setValue("screenIndex", screenIndex);
  222. QGuiApplication::processEvents();
  223. #ifdef USING_SYSTEMD
  224. sd_notify(0, "READY=1");
  225. #endif
  226. return app.exec();
  227. }
  228. #include "moc/main.moc"