Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: daemon/BackendConfig.cmake
- ===================================================================
- --- daemon/BackendConfig.cmake (revision 1206705)
- +++ daemon/BackendConfig.cmake (working copy)
- @@ -33,6 +33,13 @@
- set(powerdevilupowerbackend_LIBS ${X11_LIBRARIES} ${QT_QTGUI_LIBRARY} ${X11_Xrandr_LIB})
- +## backlight helper executable
- +kde4_add_executable(backlighthelper backends/upower/backlighthelper.cpp ${backlighthelper_mocs})
- +target_link_libraries(backlighthelper ${KDE4_KDECORE_LIBS})
- +install(TARGETS backlighthelper DESTINATION ${LIBEXEC_INSTALL_DIR})
- +kde4_install_auth_helper_files(backlighthelper org.kde.powerdevil.backlighthelper root)
- +kde4_install_auth_actions(org.kde.powerdevil.backlighthelper ${CMAKE_CURRENT_SOURCE_DIR}/backends/upower/backlight_helper_actions.actions)
- +
- ########################## HAL Backend #####################################
- include_directories(${CMAKE_CURRENT_SOURCE_DIR}/backends/hal
- Index: daemon/backends/upower/backlighthelper.h
- ===================================================================
- --- daemon/backends/upower/backlighthelper.h (revision 0)
- +++ daemon/backends/upower/backlighthelper.h (revision 0)
- @@ -0,0 +1,44 @@
- +/* This file is part of the KDE project
- + * Copyright (C) 2010 Lukas Tinkl <[email protected]>
- + *
- + * This library is free software; you can redistribute it and/or
- + * modify it under the terms of the GNU Library General Public
- + * License version 2 as published by the Free Software Foundation.
- + *
- + * This library is distributed in the hope that it will be useful,
- + * but WITHOUT ANY WARRANTY; without even the implied warranty of
- + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- + * Library General Public License for more details.
- + *
- + * You should have received a copy of the GNU Library General Public License
- + * along with this library; see the file COPYING.LIB. If not, write to
- + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- + * Boston, MA 02110-1301, USA.
- + *
- + */
- +
- +#ifndef BACKLIGHTHELPER_H
- +#define BACKLIGHTHELPER_H
- +
- +#include <QObject>
- +#include <kauth.h>
- +
- +using namespace KAuth;
- +
- +class BacklightHelper: public QObject
- +{
- + Q_OBJECT
- +public:
- + BacklightHelper(QObject * parent = 0);
- +
- +public slots:
- + ActionReply brightness(const QVariantMap & args);
- + ActionReply setbrightness(const QVariantMap & args);
- +private:
- + void init();
- + int maxBrightness() const;
- + bool m_isSupported;
- + QString m_dirname;
- +};
- +
- +#endif // BACKLIGHTHELPER_H
- Index: daemon/backends/upower/backlighthelper.cpp
- ===================================================================
- --- daemon/backends/upower/backlighthelper.cpp (revision 0)
- +++ daemon/backends/upower/backlighthelper.cpp (revision 0)
- @@ -0,0 +1,144 @@
- +/* This file is part of the KDE project
- + * Copyright (C) 2010 Lukas Tinkl <[email protected]>
- + *
- + * This library is free software; you can redistribute it and/or
- + * modify it under the terms of the GNU Library General Public
- + * License version 2 as published by the Free Software Foundation.
- + *
- + * This library is distributed in the hope that it will be useful,
- + * but WITHOUT ANY WARRANTY; without even the implied warranty of
- + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- + * Library General Public License for more details.
- + *
- + * You should have received a copy of the GNU Library General Public License
- + * along with this library; see the file COPYING.LIB. If not, write to
- + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- + * Boston, MA 02110-1301, USA.
- + *
- + */
- +
- +#include "backlighthelper.h"
- +
- +#include <QtCore/QDir>
- +#include <QtCore/QDebug>
- +
- +#define PREFIX "/sys/class/backlight/"
- +
- +BacklightHelper::BacklightHelper(QObject * parent)
- + : QObject(parent), m_isSupported(false)
- +{
- + init();
- +}
- +
- +void BacklightHelper::init()
- +{
- + // find the first existing device with backlight support
- + QStringList interfaces;
- + interfaces << "nv_backlight" << "asus_laptop" << "toshiba"
- + << "eeepc" << "thinkpad_screen" << "acpi_video1"
- + << "mbp_backlight" << "acpi_video0"
- + << "fujitsu-laptop" << "sony" << "samsung";
- +
- + QDir dir;
- + foreach (const QString & interface, interfaces) {
- + dir.setPath(PREFIX + interface);
- + //qDebug() << "searching dir:" << dir;
- + if (dir.exists()) {
- + m_dirname = dir.path();
- + qDebug() << "kernel backlight support found in" << m_dirname;
- + break;
- + }
- + }
- +
- + if (m_dirname.isEmpty()) {
- + qWarning() << "no kernel backlight interface found";
- + return;
- + }
- +
- + m_isSupported = true;
- +
- + //brightness(QVariantMap());
- +}
- +
- +ActionReply BacklightHelper::brightness(const QVariantMap & args)
- +{
- + Q_UNUSED(args);
- +
- + ActionReply reply;
- +
- + if (!m_isSupported) {
- + reply = ActionReply::HelperErrorReply;
- + return reply;
- + }
- +
- + // current brightness
- + QFile file(m_dirname + "/brightness");
- + if (!file.open(QIODevice::ReadOnly)) {
- + reply = ActionReply::HelperErrorReply;
- + reply.setErrorCode(file.error());
- + qWarning() << "reading brightness failed with error code " << file.error() << file.errorString();
- + return reply;
- + }
- +
- + QTextStream stream(&file);
- + int brightness;
- + stream >> brightness;
- + qDebug() << "brightness:" << brightness;
- + file.close();
- +
- + reply.addData("brightness", brightness * 100 / maxBrightness());
- + qDebug() << "data contains:" << reply.data()["brightness"];
- +
- + return reply;
- +}
- +
- +ActionReply BacklightHelper::setbrightness(const QVariantMap & args)
- +{
- + ActionReply reply;
- +
- + if (!m_isSupported) {
- + reply = ActionReply::HelperErrorReply;
- + return reply;
- + }
- +
- + QFile file(m_dirname + "/brightness");
- + if (!file.open(QIODevice::WriteOnly)) {
- + reply = ActionReply::HelperErrorReply;
- + reply.setErrorCode(file.error());
- + qWarning() << "writing brightness failed with error code " << file.error() << file.errorString();
- + return reply;
- + }
- +
- + int actual_brightness = args["brightness"].toFloat() * maxBrightness() / 100;
- + qDebug() << "setting brightness:" << actual_brightness;
- + int result = file.write(QByteArray::number(actual_brightness));
- + file.close();
- +
- + if (result == -1) {
- + reply = ActionReply::HelperErrorReply;
- + reply.setErrorCode(file.error());
- + qWarning() << "writing brightness failed with error code " << file.error() << file.errorString();
- + }
- +
- + return reply;
- +}
- +
- +int BacklightHelper::maxBrightness() const
- +{
- + // maximum brightness
- + QFile file(m_dirname + "/max_brightness");
- + if (!file.open(QIODevice::ReadOnly)) {
- + qWarning() << "reading max brightness failed with error code " << file.error() << file.errorString();
- + return -1; // some non-zero value
- + }
- +
- + QTextStream stream(&file);
- + int max_brightness;
- + stream >> max_brightness;
- + qDebug() << "max brightness:" << max_brightness;
- + file.close();
- +
- + return max_brightness;
- +}
- +
- +KDE4_AUTH_HELPER_MAIN("org.kde.powerdevil.backlighthelper", BacklightHelper)
- Index: daemon/backends/upower/powerdevilupowerbackend.cpp
- ===================================================================
- --- daemon/backends/upower/powerdevilupowerbackend.cpp (revision 1206705)
- +++ daemon/backends/upower/powerdevilupowerbackend.cpp (working copy)
- @@ -27,10 +27,13 @@
- #include <KDebug>
- #include <KPluginFactory>
- +#include <KAuth/Action>
- #include "xrandrbrightness.h"
- #include "upowersuspendjob.h"
- +#define HELPER_ID "org.kde.powerdevil.backlighthelper"
- +
- PowerDevilUPowerBackend::PowerDevilUPowerBackend(QObject* parent)
- : BackendInterface(parent),
- m_brightNessControl(0),
- @@ -98,9 +101,7 @@
- // Brightness Controls available
- BrightnessControlsList controls;
- - if (m_brightNessControl->isSupported()) {
- - controls.insert(QLatin1String("LVDS1"), Screen);
- - }
- + controls.insert(QLatin1String("LVDS1"), Screen);
- if (m_kbdBacklight->isValid())
- controls.insert(QLatin1String("KBD"), Keyboard);
- @@ -177,22 +178,52 @@
- float PowerDevilUPowerBackend::brightness(PowerDevil::BackendInterface::BrightnessControlType type) const
- {
- + float result = 0.0;
- +
- if (type == Screen) {
- - kDebug() << "Screen brightness: " << m_brightNessControl->brightness();
- - return m_brightNessControl->brightness();
- + if (m_brightNessControl->isSupported()) {
- + //kDebug() << "Calling xrandr brightness";
- + result = m_brightNessControl->brightness();
- + } else {
- + //kDebug() << "Falling back to helper to get brightness";
- + KAuth::Action action("org.kde.powerdevil.backlighthelper.brightness");
- + action.setHelperID(HELPER_ID);
- + KAuth::ActionReply reply = action.execute();
- + if (reply.succeeded()) {
- + result = reply.data()["brightness"].toFloat();
- + //kDebug() << "org.kde.powerdevil.backlighthelper.brightness succeeded: " << reply.data()["brightness"];
- + }
- + else
- + kWarning() << "org.kde.powerdevil.backlighthelper.brightness failed";
- +
- + }
- + kDebug() << "Screen brightness: " << result;
- } else if (type == Keyboard) {
- kDebug() << "Kbd backlight brightness: " << m_kbdBacklight->GetBrightness();
- - return m_kbdBacklight->GetBrightness() / m_kbdBacklight->GetMaxBrightness() * 100;
- + result = m_kbdBacklight->GetBrightness() / m_kbdBacklight->GetMaxBrightness() * 100;
- }
- - return 0.0;
- + return result;
- }
- bool PowerDevilUPowerBackend::setBrightness(float brightnessValue, PowerDevil::BackendInterface::BrightnessControlType type)
- {
- if (type == Screen) {
- kDebug() << "set screen brightness: " << brightnessValue;
- - m_brightNessControl->setBrightness(brightnessValue);
- + if (m_brightNessControl->isSupported()) {
- + m_brightNessControl->setBrightness(brightnessValue);
- + } else {
- + //kDebug() << "Falling back to helper to set brightness";
- + KAuth::Action action("org.kde.powerdevil.backlighthelper.setbrightness");
- + action.setHelperID(HELPER_ID);
- + action.addArgument("brightness", brightnessValue);
- + KAuth::ActionReply reply = action.execute();
- + if (reply.failed()) {
- + kWarning() << "org.kde.powerdevil.backlighthelper.setbrightness failed";
- + return false;
- + }
- + }
- +
- float newBrightness = brightness(Screen);
- if (!qFuzzyCompare(newBrightness, m_cachedBrightness)) {
- m_cachedBrightness = newBrightness;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement