From a0b9c2c569c2efea9d40388bedde20e6008c7757 Mon Sep 17 00:00:00 2001 From: Devin Lin Date: Sun, 5 Nov 2023 15:09:33 -0800 Subject: [PATCH] quicksettings/screenrotation: Fix it by porting to changed API It appears that the screen rotation DBus API was dropped with https://invent.kde.org/plasma/kscreen/-/merge_requests/237 Port to the new API. Fixes https://invent.kde.org/plasma/plasma-mobile/-/issues/257 --- .kde-ci.yml | 1 + CMakeLists.txt | 3 + quicksettings/screenrotation/CMakeLists.txt | 5 +- .../screenrotation/dbus/org.kde.KScreen.xml | 31 ------- .../package/contents/ui/main.qml | 5 +- .../screenrotation/screenrotationplugin.cpp | 7 +- .../screenrotation/screenrotationutil.cpp | 82 ++++++++++--------- .../screenrotation/screenrotationutil.h | 23 +++--- 8 files changed, 65 insertions(+), 92 deletions(-) delete mode 100644 quicksettings/screenrotation/dbus/org.kde.KScreen.xml diff --git a/.kde-ci.yml b/.kde-ci.yml index 934ef119..cd2b3ddb 100644 --- a/.kde-ci.yml +++ b/.kde-ci.yml @@ -18,6 +18,7 @@ Dependencies: 'frameworks/modemmanager-qt': '@latest-kf6' 'frameworks/networkmanager-qt': '@latest-kf6' 'frameworks/kjobwidgets': '@latest-kf6' + 'plasma/libkscreen': '@same' 'plasma/plasma-framework': '@same' 'plasma/kwin': '@same' 'plasma/plasma-workspace': '@same' diff --git a/CMakeLists.txt b/CMakeLists.txt index 19bf9b23..587676d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,7 @@ find_package(Qt6 ${QT_MIN_VERSION} CONFIG REQUIRED Qml Quick Gui + Sensors ) find_package(KF6 ${KF6_MIN_VERSION} REQUIRED COMPONENTS @@ -72,6 +73,8 @@ find_package(KF6 ${KF6_MIN_VERSION} REQUIRED COMPONENTS JobWidgets ) +find_package(KF6Screen CONFIG REQUIRED) + find_package(PkgConfig REQUIRED) pkg_check_modules(GOBJECT gobject-2.0 REQUIRED IMPORTED_TARGET) diff --git a/quicksettings/screenrotation/CMakeLists.txt b/quicksettings/screenrotation/CMakeLists.txt index 64b3e297..aa70d977 100644 --- a/quicksettings/screenrotation/CMakeLists.txt +++ b/quicksettings/screenrotation/CMakeLists.txt @@ -1,12 +1,9 @@ # SPDX-FileCopyrightText: 2022 Devin Lin # SPDX-License-Identifier: GPL-2.0-or-later -qt_add_dbus_interfaces(DBUS_SRCS dbus/org.kde.KScreen.xml) - set(screenrotationplugin_SRCS screenrotationplugin.cpp screenrotationutil.cpp - ${DBUS_SRCS} ) add_library(screenrotationplugin ${screenrotationplugin_SRCS}) @@ -16,11 +13,13 @@ target_link_libraries(screenrotationplugin Qt::Qml Qt::Quick Qt::DBus + Qt::Sensors KF6::CoreAddons KF6::ConfigCore KF6::ConfigGui KF6::I18n KF6::Notifications + KF6::Screen ) set_property(TARGET screenrotationplugin PROPERTY LIBRARY_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/org/kde/plasma/quicksetting/screenrotation) diff --git a/quicksettings/screenrotation/dbus/org.kde.KScreen.xml b/quicksettings/screenrotation/dbus/org.kde.KScreen.xml deleted file mode 100644 index e9c5979c..00000000 --- a/quicksettings/screenrotation/dbus/org.kde.KScreen.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/quicksettings/screenrotation/package/contents/ui/main.qml b/quicksettings/screenrotation/package/contents/ui/main.qml index 242dae7c..f697850f 100644 --- a/quicksettings/screenrotation/package/contents/ui/main.qml +++ b/quicksettings/screenrotation/package/contents/ui/main.qml @@ -10,9 +10,10 @@ QS.QuickSetting { text: i18n("Auto-rotate") icon: "rotation-allowed" settingsCommand: "plasma-open-settings kcm_kscreen" - enabled: ScreenRotationUtil.screenRotationEnabled + enabled: ScreenRotationUtil.autoScreenRotationEnabled available: ScreenRotationUtil.available + function toggle() { - ScreenRotationUtil.screenRotationEnabled = !enabled + ScreenRotationUtil.autoScreenRotationEnabled = !enabled } } diff --git a/quicksettings/screenrotation/screenrotationplugin.cpp b/quicksettings/screenrotation/screenrotationplugin.cpp index 7580c04a..e5ab52c5 100644 --- a/quicksettings/screenrotation/screenrotationplugin.cpp +++ b/quicksettings/screenrotation/screenrotationplugin.cpp @@ -1,8 +1,5 @@ -/* - * SPDX-FileCopyrightText: 2022 by Devin Lin - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ +// SPDX-FileCopyrightText: 2022-2023 Devin Lin +// SPDX-License-Identifier: GPL-2.0-or-later #include "screenrotationplugin.h" diff --git a/quicksettings/screenrotation/screenrotationutil.cpp b/quicksettings/screenrotation/screenrotationutil.cpp index 82f52f9e..2f6c9268 100644 --- a/quicksettings/screenrotation/screenrotationutil.cpp +++ b/quicksettings/screenrotation/screenrotationutil.cpp @@ -1,64 +1,68 @@ -/* - * SPDX-FileCopyrightText: 2022 by Devin Lin - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ +// SPDX-FileCopyrightText: 2022-2023 Devin Lin +// SPDX-License-Identifier: GPL-2.0-or-later #include "screenrotationutil.h" #include #include +#include +#include +#include +#include + #include +#include ScreenRotationUtil::ScreenRotationUtil(QObject *parent) : QObject{parent} + , m_config{nullptr} + , m_sensor{new QOrientationSensor(this)} { - m_kscreenInterface = new org::kde::KScreen(QStringLiteral("org.kde.kded6"), QStringLiteral("/modules/kscreen"), QDBusConnection::sessionBus(), this); + connect(m_sensor, &QOrientationSensor::activeChanged, this, &ScreenRotationUtil::availableChanged); + + connect(new KScreen::GetConfigOperation(), &KScreen::GetConfigOperation::finished, this, [this](auto *op) { + m_config = qobject_cast(op)->config(); + + Q_EMIT autoScreenRotationEnabledChanged(); + }); } -bool ScreenRotationUtil::screenRotation() +bool ScreenRotationUtil::autoScreenRotationEnabled() { - QDBusPendingReply reply = m_kscreenInterface->getAutoRotate(); - reply.waitForFinished(); - if (reply.isError()) { - qWarning() << "Getting auto rotate failed:" << reply.error().name() << reply.error().message(); + if (!m_config) { return false; - } else { - return reply.value(); } + const auto outputs = m_config->outputs(); + + for (KScreen::OutputPtr output : outputs) { + if (output->autoRotatePolicy() != KScreen::Output::AutoRotatePolicy::Always) { + return false; + } + } + + return true; } -void ScreenRotationUtil::setScreenRotation(bool value) +void ScreenRotationUtil::setAutoScreenRotationEnabled(bool value) { - QDBusPendingReply<> reply = m_kscreenInterface->setAutoRotate(value); - reply.waitForFinished(); - if (reply.isError()) { - qWarning() << "Setting auto rotate failed:" << reply.error().name() << reply.error().message(); - } else { - Q_EMIT screenRotationChanged(value); + if (!m_config) { + return; } + + KScreen::Output::AutoRotatePolicy policy = value ? KScreen::Output::AutoRotatePolicy::Always : KScreen::Output::AutoRotatePolicy::Never; + + const auto outputs = m_config->outputs(); + for (KScreen::OutputPtr output : outputs) { + if (output->autoRotatePolicy() != policy) { + output->setAutoRotatePolicy(policy); + } + } + + Q_EMIT autoScreenRotationEnabledChanged(); } bool ScreenRotationUtil::isAvailable() { - QDBusPendingReply reply = m_kscreenInterface->isAutoRotateAvailable(); - QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this); - - connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) { - QDBusPendingReply reply = *watcher; - if (reply.isError()) { - qWarning() << "Getting available failed:" << reply.error().name() << reply.error().message(); - } else { - // make sure we don't go into an infinite loop - if (m_available != reply.value()) { - Q_EMIT availableChanged(m_available); - } - - m_available = reply.value(); - } - watcher->deleteLater(); - }); - - return m_available; + return m_sensor->connectToBackend(); } diff --git a/quicksettings/screenrotation/screenrotationutil.h b/quicksettings/screenrotation/screenrotationutil.h index 263cbb78..32c49346 100644 --- a/quicksettings/screenrotation/screenrotationutil.h +++ b/quicksettings/screenrotation/screenrotationutil.h @@ -1,35 +1,34 @@ -/* - * SPDX-FileCopyrightText: 2022 by Devin Lin - * - * SPDX-License-Identifier: GPL-2.0-or-later - */ +// SPDX-FileCopyrightText: 2022-2023 Devin Lin +// SPDX-License-Identifier: GPL-2.0-or-later #pragma once #include +#include -#include "kscreeninterface.h" +#include class ScreenRotationUtil : public QObject { Q_OBJECT - Q_PROPERTY(bool screenRotationEnabled READ screenRotation WRITE setScreenRotation NOTIFY screenRotationChanged); + Q_PROPERTY(bool autoScreenRotationEnabled READ autoScreenRotationEnabled WRITE setAutoScreenRotationEnabled NOTIFY autoScreenRotationEnabledChanged); Q_PROPERTY(bool available READ isAvailable NOTIFY availableChanged); public: ScreenRotationUtil(QObject *parent = nullptr); - bool screenRotation(); - void setScreenRotation(bool value); + bool autoScreenRotationEnabled(); + void setAutoScreenRotationEnabled(bool value); bool isAvailable(); Q_SIGNALS: - void screenRotationChanged(bool value); - void availableChanged(bool value); + void autoScreenRotationEnabledChanged(); + void availableChanged(); private: - org::kde::KScreen *m_kscreenInterface; + KScreen::ConfigPtr m_config; + QOrientationSensor *m_sensor; bool m_available; };