quicksettings: Move Flashlight write to KAuth

This commit is contained in:
Florian RICHER 2025-07-20 11:02:20 +02:00
parent b25405dc31
commit c42758b335
5 changed files with 115 additions and 3 deletions

View file

@ -13,6 +13,7 @@ target_link_libraries(flashlightplugin PRIVATE
Qt::Qml
Qt::Quick
Qt::DBus
KF6::AuthCore
KF6::CoreAddons
KF6::ConfigCore
KF6::ConfigGui
@ -24,3 +25,5 @@ target_link_libraries(flashlightplugin PRIVATE
ecm_finalize_qml_module(flashlightplugin)
plasma_install_package(package org.kde.plasma.quicksetting.flashlight quicksettings)
add_subdirectory(kauth)

View file

@ -1,6 +1,7 @@
/*
* SPDX-FileCopyrightText: 2020 Han Young <hanyoung@protonmail.com>
* SPDX-FileCopyrightText: 2022 by Devin Lin <devin@kde.org>
* SPDX-FileCopyrightText: 2024-2025 Florian RICHER <florian.richer@protonmail.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
@ -15,6 +16,11 @@
#include <QDebug>
#include <QFileInfo>
#include <KAuth/Action>
#include <KAuth/ExecuteJob>
using namespace Qt::StringLiterals;
#define TORCH_SUBSYSTEM "leds"
FlashlightUtil::FlashlightUtil(QObject *parent)
@ -39,9 +45,20 @@ void FlashlightUtil::toggleTorch()
return;
}
int ret = udev_device_set_sysattr_value(m_device, "brightness", const_cast<char *>(m_torchEnabled ? "0" : m_maxBrightness));
if (ret < 0) {
qWarning() << "Flashlight can't be toggled";
const QString sysPath = udev_device_get_syspath(m_device);
const QString brightness = m_torchEnabled ? "0" : m_maxBrightness;
const QVariantMap args = {
{u"sysPath"_s, sysPath},
{u"brightness"_s, brightness},
};
KAuth::Action writeAction(u"org.kde.plasma.mobileshell.flashlighthelper.setbrightness"_s);
writeAction.setHelperId(u"org.kde.plasma.mobileshell.flashlighthelper"_s);
writeAction.setArguments(args);
KAuth::ExecuteJob *job = writeAction.execute();
if (!job->exec()) {
qDebug() << "Flashlight can't be toggled: kauth returned an error code:" << job->error() << " message: " << job->errorString();
return;
}

View file

@ -0,0 +1,27 @@
# SPDX-FileCopyrightText: 2025 Florian RICHER <florian.richer@protonmail.com>
# SPDX-License-Identifier: BSD-2-Clause
add_executable(flashlighthelper)
target_sources(flashlighthelper PRIVATE flashlighthelper.cpp)
target_link_libraries(flashlighthelper
Qt6::Core
KF6::AuthCore
KF6::CoreAddons
udev
)
install(TARGETS flashlighthelper DESTINATION ${KAUTH_HELPER_INSTALL_DIR})
find_package(KF6Auth NO_MODULE)
kauth_install_helper_files(flashlighthelper org.kde.plasma.mobileshell.flashlighthelper root)
kauth_install_actions(org.kde.plasma.mobileshell.flashlighthelper flashlighthelper.actions)
ecm_qt_declare_logging_category(flashlighthelper
HEADER flashlighthelper_debug.h
IDENTIFIER FLASHLIGHTHELPER
DEFAULT_SEVERITY Warning
CATEGORY_NAME org.kde.plasma.mobileshell.flashlighthelper
DESCRIPTION "Helper for Flashlight for some actions need root access"
)

View file

@ -0,0 +1,13 @@
# SPDX-FileCopyrightText: 2025 Florian RICHER <florian.richer@protonmail.com>
# SPDX-License-Identifier: GPL-2.0-or-later
[Domain]
Name=Waydroid Management
Icon=color
[org.kde.plasma.mobileshell.flashlighthelper.setbrightness]
Name=Set flashlight brightness
Description=Allow set brightness on led device
Policy=yes
PolicyInactive=yes
Persistence=session

View file

@ -0,0 +1,52 @@
/*
* SPDX-FileCopyrightText: 2025 Florian RICHER <florian.richer@protonmail.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "flashlighthelper_debug.h"
#include <KAuth/ActionReply>
#include <KAuth/HelperSupport>
#include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QLoggingCategory>
#include <QObject>
#include <libudev.h>
using namespace Qt::StringLiterals;
class Flashlighthelper : public QObject
{
Q_OBJECT
public Q_SLOTS:
KAuth::ActionReply setbrightness(const QVariantMap &args);
};
KAuth::ActionReply Flashlighthelper::setbrightness(const QVariantMap &args)
{
const char *sysPath = args.value("sysPath"_L1).toString().toUtf8().constData();
const char *brightness = args.value("brightness"_L1).toString().toUtf8().constData();
struct udev *udev = udev_new();
struct udev_device *device = udev_device_new_from_syspath(udev, sysPath);
int ret = udev_device_set_sysattr_value(device, "brightness", const_cast<char *>(brightness));
udev_device_unref(device);
udev_unref(udev);
if (ret >= 0) {
return KAuth::ActionReply::SuccessReply();
} else {
qCWarning(FLASHLIGHTHELPER) << "Failed to set udev system attribute";
return KAuth::ActionReply::HelperErrorReply();
}
}
KAUTH_HELPER_MAIN("org.kde.plasma.mobileshell.flashlighthelper", Flashlighthelper)
#include "flashlighthelper.moc"