main.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 <QGuiApplication>
  23. #include <QQuickView>
  24. #include <QQmlContext>
  25. #include <QQmlEngine>
  26. #include <QSettings>
  27. #include <QSurfaceFormat>
  28. #include <QTimer>
  29. #include <QQuickWindow>
  30. #include <QDir>
  31. #include <QFileInfo>
  32. #include <QTextStream>
  33. #include <QDebug>
  34. #include <QScreen>
  35. #include <QDBusInterface>
  36. #include <QDBusConnection>
  37. #include <QFileSystemWatcher>
  38. #include <QtPlugin>
  39. #include <QMetaObject>
  40. class CloseEventFilter : public QObject
  41. {
  42. Q_OBJECT
  43. public:
  44. CloseEventFilter(QObject *p) : QObject(p) { /**/ }
  45. protected:
  46. bool eventFilter(QObject *obj, QEvent *event);
  47. };
  48. bool CloseEventFilter::eventFilter(QObject *obj, QEvent *event)
  49. {
  50. if (event->type() == QEvent::Close) {
  51. qApp->quit();
  52. }
  53. return QObject::eventFilter(obj, event);
  54. }
  55. class NativeUtils : public QObject {
  56. Q_OBJECT
  57. Q_PROPERTY(bool rebootRequired MEMBER rebootRequired NOTIFY rebootRequiredChanged)
  58. public:
  59. NativeUtils(QObject *p);
  60. signals:
  61. void rebootRequiredChanged();
  62. public slots:
  63. void monitorRunPath(const QString &path);
  64. private:
  65. QString watchFile;
  66. QFileSystemWatcher runDirWatcher;
  67. bool rebootRequired;
  68. };
  69. NativeUtils::NativeUtils(QObject *p)
  70. : QObject(p),
  71. watchFile("/run/reboot-required"),
  72. rebootRequired(false)
  73. {
  74. runDirWatcher.addPath(QFileInfo(watchFile).absolutePath());
  75. connect(&runDirWatcher, &QFileSystemWatcher::directoryChanged, this, &NativeUtils::monitorRunPath);
  76. monitorRunPath("");
  77. }
  78. void NativeUtils::monitorRunPath(const QString &path)
  79. {
  80. Q_UNUSED(path);
  81. rebootRequired = QFileInfo::exists(watchFile);
  82. emit rebootRequiredChanged();
  83. }
  84. class ArtView {
  85. public:
  86. static void populateScreen(QScreen *screen = nullptr);
  87. private:
  88. static QQmlEngine* sharedQmlEngine;
  89. };
  90. QQmlEngine* ArtView::sharedQmlEngine = nullptr;
  91. void ArtView::populateScreen(QScreen *screen)
  92. {
  93. static QString qmlPath;
  94. #ifdef COMPILED_RESOURCES
  95. qmlPath = "qrc:/qml";
  96. #else
  97. qmlPath = QCoreApplication::applicationDirPath() % "/qml";
  98. if (!QDir(qmlPath).exists()) {
  99. qmlPath = "/usr/share/" % qApp->applicationName() % "/qml";
  100. }
  101. #endif
  102. QQuickView *view;
  103. if (sharedQmlEngine) {
  104. view = new QQuickView(sharedQmlEngine, nullptr);
  105. } else {
  106. view = new QQuickView();
  107. sharedQmlEngine = view->engine();
  108. sharedQmlEngine->addImportPath(qmlPath);
  109. sharedQmlEngine->rootContext()->setContextProperty("nativeUtils", new NativeUtils(sharedQmlEngine));
  110. sharedQmlEngine->rootContext()->setContextProperty("imageModel", new PictureModel(sharedQmlEngine));
  111. QObject::connect(sharedQmlEngine, &QQmlEngine::quit, qApp, &QCoreApplication::quit);
  112. }
  113. QObject::connect(view, &QQuickView::statusChanged, [](QQuickView::Status status) {
  114. if (status == QQuickView::Error) {
  115. QMetaObject::invokeMethod(qApp, "quit", Qt::QueuedConnection);
  116. }
  117. });
  118. view->setColor(Qt::transparent);
  119. view->setResizeMode(QQuickView::SizeRootObjectToView);
  120. view->setSource(QUrl(qmlPath + "/main.qml"));
  121. if (screen) {
  122. QRect geometry = screen->availableGeometry();
  123. view->setScreen(screen);
  124. view->setGeometry(geometry);
  125. qDebug() << "Displaying artwork on" << screen << "with geometry" << geometry;
  126. }
  127. view->showFullScreen();
  128. }
  129. int main(int argc, char *argv[])
  130. {
  131. #ifdef STATIC_BUILD
  132. Q_IMPORT_PLUGIN(QmlSettingsPlugin)
  133. Q_IMPORT_PLUGIN(QtQuick2WindowPlugin)
  134. Q_IMPORT_PLUGIN(QtQuick2Plugin)
  135. #endif
  136. qsrand(time(NULL));
  137. QGuiApplication app(argc, argv);
  138. app.setOrganizationName("Chaos Reins");
  139. app.setApplicationName("artriculate");
  140. app.installEventFilter(new CloseEventFilter(&app));
  141. QSettings settings;
  142. if (settings.value("raster", false).toBool()) {
  143. #if QT_VERSION < QT_VERSION_CHECK(5, 8, 0)
  144. qDebug() << "Trying to use the SG software backend prior to Qt 5.8";
  145. #else
  146. QQuickWindow::setSceneGraphBackend(QSGRendererInterface::Software);
  147. #endif
  148. } else {
  149. if (settings.value("force32bpp", true).toBool()) {
  150. QSurfaceFormat format = QSurfaceFormat::defaultFormat();
  151. format.setAlphaBufferSize(0);
  152. format.setRedBufferSize(8);
  153. format.setGreenBufferSize(8);
  154. format.setBlueBufferSize(8);
  155. QSurfaceFormat::setDefaultFormat(format);
  156. }
  157. if (settings.value("forceSingleBuffer", false).toBool()) {
  158. QSurfaceFormat format = QSurfaceFormat::defaultFormat();
  159. format.setSwapBehavior(QSurfaceFormat::SingleBuffer);
  160. QSurfaceFormat::setDefaultFormat(format);
  161. } else if (settings.value("forceDoubleBuffer", false).toBool()) {
  162. QSurfaceFormat format = QSurfaceFormat::defaultFormat();
  163. format.setSwapBehavior(QSurfaceFormat::TripleBuffer);
  164. QSurfaceFormat::setDefaultFormat(format);
  165. }
  166. }
  167. // qdbus org.freedesktop.ScreenSaver /org/freedesktop/ScreenSaver Inhibit "artriculate" "media playback"
  168. if (settings.value("suppressScreensaver", false).toBool()) {
  169. QDBusInterface screenSaver("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver");
  170. uint id = screenSaver.call("Inhibit", app.applicationName(), "Media playback").arguments().at(0).toInt();
  171. QObject::connect(&app, &QCoreApplication::aboutToQuit, [id]() {
  172. QDBusInterface screenSaver("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver");
  173. screenSaver.call("UnInhibit", id);
  174. });
  175. }
  176. qmlRegisterType<PictureModel>("PictureModel", 1, 0, "PictureModel");
  177. int screenIndex = settings.value("screenIndex", -2).toInt();
  178. QList<QScreen*> screens = QGuiApplication::screens();
  179. if (screenIndex == -2) {
  180. ArtView::populateScreen();
  181. } else if (screenIndex == -1) {
  182. foreach(QScreen *screen, screens) {
  183. ArtView::populateScreen(screen);
  184. }
  185. } else {
  186. if ((screenIndex >= 0) && (screenIndex < screens.length())) {
  187. ArtView::populateScreen(screens.at(screenIndex));
  188. } else {
  189. ArtView::populateScreen();
  190. }
  191. }
  192. settings.setValue("screenIndex", screenIndex);
  193. QGuiApplication::processEvents();
  194. #ifdef USING_SYSTEMD
  195. sd_notify(0, "READY=1");
  196. #endif
  197. return app.exec();
  198. }
  199. #include "main.moc"