OSDN Git Service

xwallpaper: new fancy wallpaper
[kde/kde-playground.git] / xwallpaper / xwallpaper.cpp
1 /*  This file is part of the KDE project
2     Copyright (C) 2023 Ivailo Monev <xakepa10@gmail.com>
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Library General Public
6     License version 2, as published by the Free Software Foundation.
7
8     This library is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11     Library General Public License for more details.
12
13     You should have received a copy of the GNU Library General Public License
14     along with this library; see the file COPYING.LIB.  If not, write to
15     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16     Boston, MA 02110-1301, USA.
17 */
18
19 #include "xwallpaper.h"
20
21 #include <QX11Info>
22 #include <QApplication>
23 #include <QDesktopWidget>
24 #include <KDebug>
25
26 static const QString s_xscreensaver = QString::fromLatin1("/usr/libexec/xscreensaver/binaryring");
27 static const int s_updateinterval = 100; // increase to decrease CPU usage
28
29 XWallpaper::XWallpaper(QObject *parent, const QVariantList &args)
30     : Plasma::Wallpaper(parent, args),
31     m_widget(nullptr),
32     m_proc(nullptr),
33     m_timer(nullptr)
34 {
35     m_widget = new QWidget(nullptr, Qt::X11BypassWindowManagerHint);
36     // move it outside the screen space, should the resolution change the widget may be visible but
37     // neither rendering nor grabbing the window works without the winodow being visible
38     const QRect trect = QApplication::desktop()->geometry();
39     m_widget->move(trect.bottom(), trect.right());
40     m_widget->show();
41
42     m_proc = new QProcess(this);
43     m_proc->start(
44         s_xscreensaver,
45         QStringList() << "--window-id" << QString::number(qlonglong(m_widget->winId()))
46     );
47     if (!m_proc->waitForStarted()) {
48         kWarning() << "Could not start" << s_xscreensaver;
49         return;
50     } else {
51         kDebug() << "Started" << s_xscreensaver << QString::number(qlonglong(m_widget->winId()));
52     }
53
54     m_timer = new QTimer(this);
55     m_timer->setInterval(s_updateinterval);
56     connect(m_timer, SIGNAL(timeout()), this, SLOT(slotTimeout()));
57     m_timer->start();
58 }
59
60 XWallpaper::~XWallpaper()
61 {
62     m_timer->stop();
63     m_proc->terminate();
64     m_proc->waitForFinished();
65     delete m_widget;
66 }
67
68 void XWallpaper::paint(QPainter *painter, const QRectF &exposedRect)
69 {
70     kDebug() << "Rendering" << s_xscreensaver << targetSizeHint().toSize();
71     const QSize tsize = targetSizeHint().toSize();
72     m_widget->resize(tsize.width(), tsize.height());
73     painter->drawPixmap(QPoint(), QPixmap::grabWindow(m_widget->winId()));
74 }
75
76 void XWallpaper::slotTimeout()
77 {
78     emit update(QRectF());
79 }
80
81 #include "moc_xwallpaper.cpp"