main.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. QRect geometry = screen->availableGeometry();
  120. view->setColor(Qt::transparent);
  121. view->setResizeMode(QQuickView::SizeRootObjectToView);
  122. view->setSource(QUrl(qmlPath + "/main.qml"));
  123. view->setGeometry(geometry);
  124. view->showFullScreen();
  125. QObject::connect(view, &QQuickView::statusChanged, [](QQuickView::Status status) {
  126. if (status == QQuickView::Error) {
  127. QMetaObject::invokeMethod(qApp, "quit", Qt::QueuedConnection);
  128. }
  129. });
  130. qDebug() << "Displaying artwork on" << screen << "with geometry" << geometry;
  131. }
  132. int main(int argc, char *argv[])
  133. {
  134. #ifdef STATIC_BUILD
  135. Q_IMPORT_PLUGIN(QmlSettingsPlugin)
  136. Q_IMPORT_PLUGIN(QtQuick2WindowPlugin)
  137. Q_IMPORT_PLUGIN(QtQuick2Plugin)
  138. #endif
  139. qsrand(time(NULL));
  140. QGuiApplication app(argc, argv);
  141. if (QFontDatabase::addApplicationFont(":/Lato-Regular.ttf") == -1) {
  142. qDebug() << "Failed to successfully add the application font";
  143. }
  144. app.setFont(QFont("Lato Regular"));
  145. app.setOrganizationName("Chaos Reins");
  146. app.setApplicationName("artriculate");
  147. app.installEventFilter(new CloseEventFilter(&app));
  148. QSettings settings;
  149. if (settings.value("raster", false).toBool()) {
  150. #if QT_VERSION < QT_VERSION_CHECK(5, 8, 0)
  151. qDebug() << "Trying to use the SG software backend prior to Qt 5.8";
  152. #else
  153. QQuickWindow::setSceneGraphBackend(QSGRendererInterface::Software);
  154. #endif
  155. } else {
  156. if (settings.value("force32bpp", true).toBool()) {
  157. QSurfaceFormat format = QSurfaceFormat::defaultFormat();
  158. format.setAlphaBufferSize(0);
  159. format.setRedBufferSize(8);
  160. format.setGreenBufferSize(8);
  161. format.setBlueBufferSize(8);
  162. QSurfaceFormat::setDefaultFormat(format);
  163. } else if (settings.value("force16bpp", true).toBool()) {
  164. QSurfaceFormat format = QSurfaceFormat::defaultFormat();
  165. format.setAlphaBufferSize(0);
  166. format.setRedBufferSize(5);
  167. format.setGreenBufferSize(6);
  168. format.setBlueBufferSize(5);
  169. QSurfaceFormat::setDefaultFormat(format);
  170. }
  171. if (settings.value("forceSingleBuffer", false).toBool()) {
  172. QSurfaceFormat format = QSurfaceFormat::defaultFormat();
  173. format.setSwapBehavior(QSurfaceFormat::SingleBuffer);
  174. QSurfaceFormat::setDefaultFormat(format);
  175. } else if (settings.value("forceDoubleBuffer", false).toBool()) {
  176. QSurfaceFormat format = QSurfaceFormat::defaultFormat();
  177. format.setSwapBehavior(QSurfaceFormat::TripleBuffer);
  178. QSurfaceFormat::setDefaultFormat(format);
  179. }
  180. }
  181. // qdbus org.freedesktop.ScreenSaver /org/freedesktop/ScreenSaver Inhibit "artriculate" "media playback"
  182. if (settings.value("suppressScreensaver", false).toBool()) {
  183. QDBusInterface screenSaver("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver");
  184. uint id = screenSaver.call("Inhibit", app.applicationName(), "Media playback").arguments().at(0).toInt();
  185. QObject::connect(&app, &QCoreApplication::aboutToQuit, [id]() {
  186. QDBusInterface screenSaver("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver");
  187. screenSaver.call("UnInhibit", id);
  188. });
  189. }
  190. qmlRegisterType<PictureModel>("PictureModel", 1, 0, "PictureModel");
  191. int screenIndex = settings.value("screenIndex", -2).toInt();
  192. QList<QScreen*> screens = QGuiApplication::screens();
  193. if (screenIndex == -2) {
  194. ArtView::populateScreen();
  195. } else if (screenIndex == -1) {
  196. foreach(QScreen *screen, screens) {
  197. ArtView::populateScreen(screen);
  198. }
  199. } else {
  200. if ((screenIndex >= 0) && (screenIndex < screens.length())) {
  201. ArtView::populateScreen(screens.at(screenIndex));
  202. } else {
  203. ArtView::populateScreen();
  204. }
  205. }
  206. settings.setValue("screenIndex", screenIndex);
  207. QGuiApplication::processEvents();
  208. #ifdef USING_SYSTEMD
  209. sd_notify(0, "READY=1");
  210. #endif
  211. return app.exec();
  212. }
  213. #include "moc/main.moc"