File indexing completed on 2024-04-14 05:39:36

0001 // SPDX-License-Identifier: GPL-3.0-or-later
0002 /*
0003   Copyright 2020 - 2023 Martin Koller, kollix@aon.at
0004 
0005   This file is part of liquidshell.
0006 
0007   liquidshell is free software: you can redistribute it and/or modify
0008   it under the terms of the GNU General Public License as published by
0009   the Free Software Foundation, either version 3 of the License, or
0010   (at your option) any later version.
0011 
0012   liquidshell is distributed in the hope that it will be useful,
0013   but WITHOUT ANY WARRANTY; without even the implied warranty of
0014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0015   GNU General Public License for more details.
0016 
0017   You should have received a copy of the GNU General Public License
0018   along with liquidshell.  If not, see <http://www.gnu.org/licenses/>.
0019 */
0020 
0021 #include <OnScreenBrightness.hxx>
0022 #include <KWinCompat.hxx>
0023 
0024 #include <QDBusConnection>
0025 #include <QDBusMessage>
0026 #include <QDBusReply>
0027 #include <QApplication>
0028 #include <QScreen>
0029 #include <QDebug>
0030 
0031 //--------------------------------------------------------------------------------
0032 
0033 OnScreenBrightness::OnScreenBrightness(QWidget *parent)
0034   : QProgressBar(parent)
0035 {
0036   setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus);
0037 
0038   setFixedSize(400, 40);
0039   hide();
0040 
0041   KWindowSystem::setState(winId(), NET::KeepAbove);
0042   KWindowSystem::setType(winId(), NET::Dock);
0043   KWinCompat::setOnAllDesktops(winId(), true);
0044 
0045   hideTimer.setInterval(1000);
0046   hideTimer.setSingleShot(true);
0047   connect(&hideTimer, &QTimer::timeout, this, &QWidget::hide);
0048 
0049   QDBusConnection::sessionBus()
0050       .connect("org.kde.Solid.PowerManagement", "/org/kde/Solid/PowerManagement/Actions/BrightnessControl",
0051                "org.kde.Solid.PowerManagement.Actions.BrightnessControl", "brightnessChanged",
0052                this, SLOT(brightnessChanged(int)));
0053 
0054   QDBusConnection::sessionBus()
0055       .connect("org.kde.Solid.PowerManagement", "/org/kde/Solid/PowerManagement/Actions/BrightnessControl",
0056                "org.kde.Solid.PowerManagement.Actions.BrightnessControl", "brightnessMaxChanged",
0057                this, SLOT(brightnessMaxChanged(int)));
0058 
0059   QDBusMessage msg =
0060       QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
0061                                      "/org/kde/Solid/PowerManagement/Actions/BrightnessControl",
0062                                      "org.kde.Solid.PowerManagement.Actions.BrightnessControl",
0063                                      "brightnessMax");
0064 
0065   QDBusConnection::sessionBus().callWithCallback(msg, this, SLOT(gotBrightnessMax(QDBusMessage)), nullptr);
0066 }
0067 
0068 //--------------------------------------------------------------------------------
0069 
0070 void OnScreenBrightness::gotBrightnessMax(QDBusMessage msg)
0071 {
0072   QDBusReply<int> reply = msg;
0073 
0074   if ( !reply.isValid() )
0075     return;
0076 
0077   int max = reply.value();
0078 
0079   setMaximum(max);
0080 }
0081 
0082 //--------------------------------------------------------------------------------
0083 
0084 void OnScreenBrightness::brightnessMaxChanged(int value)
0085 {
0086   setMaximum(value);
0087 }
0088 
0089 //--------------------------------------------------------------------------------
0090 
0091 void OnScreenBrightness::brightnessChanged(int value)
0092 {
0093   setValue(value);
0094 
0095   move((QApplication::primaryScreen()->size().width() - width()) / 2,
0096         QApplication::primaryScreen()->size().height() * 0.8);
0097 
0098   show();
0099   hideTimer.start();
0100 }
0101 
0102 //--------------------------------------------------------------------------------