initialstart: Use DBus api to set brightness

Port away from deprecated P5Support.DataSource, doesn't seem to work anyway in this case
This commit is contained in:
Devin Lin 2023-11-05 20:10:20 -08:00
parent 715bc8bbd2
commit e82af2b681
5 changed files with 159 additions and 47 deletions

View file

@ -1,9 +1,12 @@
# SPDX-FileCopyrightText: 2023 Devin Lin <devin@kde.org> # SPDX-FileCopyrightText: 2023 Devin Lin <devin@kde.org>
# SPDX-License-Identifier: GPL-2.0-or-later # SPDX-License-Identifier: GPL-2.0-or-later
qt_add_dbus_interfaces(DBUS_SRCS dbus/org.kde.Solid.PowerManagement.Actions.BrightnessControl.xml)
set(prepareplugin_SRCS set(prepareplugin_SRCS
prepareplugin.cpp prepareplugin.cpp
prepareutil.cpp prepareutil.cpp
${DBUS_SRCS}
) )
add_library(prepareplugin ${prepareplugin_SRCS}) add_library(prepareplugin ${prepareplugin_SRCS})

View file

@ -0,0 +1,33 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<!--
- SPDX-FileCopyrightText: 2015 Kai Uwe Broulik <kbroulik@kde.org>
- SPDX-License-Identifier: GPL-2.0-or-later
-->
<node>
<interface name="org.kde.Solid.PowerManagement.Actions.BrightnessControl">
<method name="setBrightness">
<arg type="i" direction="in" />
</method>
<method name="setBrightnessSilent">
<arg type="i" direction="in" />
</method>
<method name="brightness">
<arg type="i" direction="out" />
</method>
<method name="brightnessMax">
<arg type="i" direction="out" />
</method>
<signal name="brightnessChanged">
<arg type="i" direction="out" />
</signal>
<signal name="brightnessMaxChanged">
<arg type="i" direction="out" />
</signal>
<method name="brightnessSteps">
<arg type="i" direction="out" />
</method>
</interface>
</node>

View file

@ -8,7 +8,6 @@ import QtQuick.Layouts 1.15
import org.kde.kirigami 2.20 as Kirigami import org.kde.kirigami 2.20 as Kirigami
import org.kde.kirigamiaddons.formcard 1.0 as FormCard import org.kde.kirigamiaddons.formcard 1.0 as FormCard
import org.kde.plasma.mobileinitialstart.prepare 1.0 as Prepare import org.kde.plasma.mobileinitialstart.prepare 1.0 as Prepare
import org.kde.plasma.plasma5support 2.0 as P5Support
Item { Item {
id: root id: root
@ -16,48 +15,6 @@ Item {
readonly property real cardWidth: Math.min(Kirigami.Units.gridUnit * 30, root.width - Kirigami.Units.gridUnit * 2) readonly property real cardWidth: Math.min(Kirigami.Units.gridUnit * 30, root.width - Kirigami.Units.gridUnit * 2)
// brightness controls
property int screenBrightness: 0
property bool disableBrightnessUpdate: true
readonly property int maximumScreenBrightness: pmSource.data["PowerDevil"] ? pmSource.data["PowerDevil"]["Maximum Screen Brightness"] || 0 : 0
property QtObject updateScreenBrightnessJob
function updateBrightnessUI() {
if (updateScreenBrightnessJob)
return;
root.disableBrightnessUpdate = true;
root.screenBrightness = pmSource.data["PowerDevil"]["Screen Brightness"];
root.disableBrightnessUpdate = false;
}
onScreenBrightnessChanged: {
brightnessSlider.value = root.screenBrightness
if (!disableBrightnessUpdate) {
const service = pmSource.serviceForSource("PowerDevil");
const operation = service.operationDescription("setBrightness");
operation.brightness = screenBrightness;
operation.silent = true; // don't show OSD
updateScreenBrightnessJob = service.startOperationCall(operation);
updateScreenBrightnessJob.finished.connect(function (job) {
root.updateBrightnessUI();
});
}
}
P5Support.DataSource {
id: pmSource
engine: "powermanagement"
connectedSources: ["PowerDevil"]
onSourceAdded: if (source === "PowerDevil") {
disconnectSource(source);
connectSource(source);
}
onDataChanged: root.updateBrightnessUI()
}
ScrollView { ScrollView {
anchors { anchors {
fill: parent fill: parent
@ -77,12 +34,15 @@ Item {
Layout.alignment: Qt.AlignTop Layout.alignment: Qt.AlignTop
Layout.fillWidth: true Layout.fillWidth: true
visible: Prepare.PrepareUtil.brightnessAvailable
wrapMode: Text.Wrap wrapMode: Text.Wrap
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
text: i18n("Adjust the screen brightness to be comfortable for the installation process.") text: i18n("Adjust the screen brightness to be comfortable for the installation process.")
} }
FormCard.FormCard { FormCard.FormCard {
id: brightnessCard
visible: Prepare.PrepareUtil.brightnessAvailable
maximumWidth: root.cardWidth maximumWidth: root.cardWidth
Layout.alignment: Qt.AlignTop | Qt.AlignHCenter Layout.alignment: Qt.AlignTop | Qt.AlignHCenter
@ -103,9 +63,17 @@ Item {
id: brightnessSlider id: brightnessSlider
Layout.fillWidth: true Layout.fillWidth: true
from: 1 from: 1
to: root.maximumScreenBrightness to: Prepare.PrepareUtil.maxBrightness
value: root.screenBrightness value: Prepare.PrepareUtil.brightness
onMoved: root.screenBrightness = value; onMoved: Prepare.PrepareUtil.brightness = value;
// HACK: for some reason, the slider initial value doesn't set without being done after the component completes loading
Timer {
interval: 0
running: true
repeat: false
onTriggered: brightnessSlider.value = Qt.binding(() => Prepare.PrepareUtil.brightness)
}
} }
Kirigami.Icon { Kirigami.Icon {
@ -129,6 +97,7 @@ Item {
} }
FormCard.FormCard { FormCard.FormCard {
id: scalingCard
maximumWidth: root.cardWidth maximumWidth: root.cardWidth
Layout.alignment: Qt.AlignTop | Qt.AlignHCenter Layout.alignment: Qt.AlignTop | Qt.AlignHCenter

View file

@ -8,9 +8,24 @@
#include <kscreen/output.h> #include <kscreen/output.h>
#include <kscreen/setconfigoperation.h> #include <kscreen/setconfigoperation.h>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
PrepareUtil::PrepareUtil(QObject *parent) PrepareUtil::PrepareUtil(QObject *parent)
: QObject{parent} : QObject{parent}
{ {
m_brightnessInterface =
new org::kde::Solid::PowerManagement::Actions::BrightnessControl(QStringLiteral("org.kde.Solid.PowerManagement"),
QStringLiteral("/org/kde/Solid/PowerManagement/Actions/BrightnessControl"),
QDBusConnection::sessionBus(),
this);
fetchBrightness();
fetchMaxBrightness();
connect(m_brightnessInterface, &org::kde::Solid::PowerManagement::Actions::BrightnessControl::brightnessChanged, this, &PrepareUtil::fetchBrightness);
connect(m_brightnessInterface, &org::kde::Solid::PowerManagement::Actions::BrightnessControl::brightnessMaxChanged, this, &PrepareUtil::fetchMaxBrightness);
connect(new KScreen::GetConfigOperation(), &KScreen::GetConfigOperation::finished, this, [this](auto *op) { connect(new KScreen::GetConfigOperation(), &KScreen::GetConfigOperation::finished, this, [this](auto *op) {
m_config = qobject_cast<KScreen::GetConfigOperation *>(op)->config(); m_config = qobject_cast<KScreen::GetConfigOperation *>(op)->config();
@ -28,6 +43,20 @@ PrepareUtil::PrepareUtil(QObject *parent)
m_scaling = scaling; m_scaling = scaling;
Q_EMIT scalingChanged(); Q_EMIT scalingChanged();
}); });
// watch for brightness interface
m_brightnessInterfaceWatcher = new QDBusServiceWatcher(QStringLiteral("org.kde.Solid.PowerManagement.Actions.BrightnessControl"),
QDBusConnection::sessionBus(),
QDBusServiceWatcher::WatchForOwnerChange,
this);
connect(m_brightnessInterfaceWatcher, &QDBusServiceWatcher::serviceRegistered, this, [this]() -> void {
Q_EMIT brightnessAvailableChanged();
});
connect(m_brightnessInterfaceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, [this]() -> void {
Q_EMIT brightnessAvailableChanged();
});
} }
int PrepareUtil::scaling() const int PrepareUtil::scaling() const
@ -59,3 +88,57 @@ QStringList PrepareUtil::scalingOptions()
{ {
return {"50%", "100%", "150%", "200%", "250%", "300%"}; return {"50%", "100%", "150%", "200%", "250%", "300%"};
} }
int PrepareUtil::brightness() const
{
return m_brightness;
}
void PrepareUtil::setBrightness(int brightness)
{
m_brightnessInterface->setBrightness(brightness);
}
int PrepareUtil::maxBrightness() const
{
return m_maxBrightness;
}
bool PrepareUtil::brightnessAvailable() const
{
return m_brightnessInterface->isValid();
}
void PrepareUtil::fetchBrightness()
{
QDBusPendingReply<int> reply = m_brightnessInterface->brightness();
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<int> reply = *watcher;
if (reply.isError()) {
qWarning() << "Getting brightness failed:" << reply.error().name() << reply.error().message();
} else if (m_brightness != reply.value()) {
m_brightness = reply.value();
Q_EMIT brightnessChanged();
}
watcher->deleteLater();
});
}
void PrepareUtil::fetchMaxBrightness()
{
QDBusPendingReply<int> reply = m_brightnessInterface->brightnessMax();
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<int> reply = *watcher;
if (reply.isError()) {
qWarning() << "Getting max brightness failed:" << reply.error().name() << reply.error().message();
} else if (m_maxBrightness != reply.value()) {
m_maxBrightness = reply.value();
Q_EMIT maxBrightnessChanged();
}
watcher->deleteLater();
});
}

View file

@ -3,16 +3,21 @@
#pragma once #pragma once
#include <QDBusServiceWatcher>
#include <QObject> #include <QObject>
#include <QProcess>
#include <kscreen/config.h> #include <kscreen/config.h>
#include "brightnesscontrolinterface.h"
class PrepareUtil : public QObject class PrepareUtil : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(int scaling READ scaling WRITE setScaling NOTIFY scalingChanged); Q_PROPERTY(int scaling READ scaling WRITE setScaling NOTIFY scalingChanged);
Q_PROPERTY(QStringList scalingOptions READ scalingOptions CONSTANT); Q_PROPERTY(QStringList scalingOptions READ scalingOptions CONSTANT);
Q_PROPERTY(int brightness READ brightness WRITE setBrightness NOTIFY brightnessChanged);
Q_PROPERTY(int maxBrightness READ maxBrightness NOTIFY maxBrightnessChanged)
Q_PROPERTY(bool brightnessAvailable READ brightnessAvailable NOTIFY brightnessAvailableChanged)
public: public:
PrepareUtil(QObject *parent = nullptr); PrepareUtil(QObject *parent = nullptr);
@ -22,10 +27,29 @@ public:
QStringList scalingOptions(); QStringList scalingOptions();
int brightness() const;
void setBrightness(int brightness);
int maxBrightness() const;
bool brightnessAvailable() const;
Q_SIGNALS: Q_SIGNALS:
void scalingChanged(); void scalingChanged();
void brightnessChanged();
void maxBrightnessChanged();
void brightnessAvailableChanged();
private Q_SLOTS:
void fetchBrightness();
void fetchMaxBrightness();
private: private:
int m_scaling; int m_scaling;
int m_brightness;
int m_maxBrightness;
KScreen::ConfigPtr m_config; KScreen::ConfigPtr m_config;
org::kde::Solid::PowerManagement::Actions::BrightnessControl *m_brightnessInterface;
QDBusServiceWatcher *m_brightnessInterfaceWatcher;
}; };