File indexing completed on 2024-05-12 05:38:44

0001 /*  This file is part of the KDE project
0002     SPDX-FileCopyrightText: 2006 Kevin Ottens <ervin@kde.org>
0003     SPDX-FileCopyrightText: 2008-2010 Dario Freddi <drf@kde.org>
0004     SPDX-FileCopyrightText: 2010 Alejandro Fiestas <alex@eyeos.org>
0005     SPDX-FileCopyrightText: 2010-2013 Lukáš Tinkl <ltinkl@redhat.com>
0006     SPDX-FileCopyrightText: 2015 Kai Uwe Broulik <kde@privat.broulik.de>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-only
0009 
0010 */
0011 
0012 #include "keyboardbrightnesscontroller.h"
0013 
0014 #include <powerdevil_debug.h>
0015 
0016 #include <QDBusConnection>
0017 #include <QDBusMessage>
0018 #include <QDebug>
0019 
0020 #include "brightnessosdwidget.h"
0021 
0022 #define UPOWER_SERVICE "org.freedesktop.UPower"
0023 
0024 KeyboardBrightnessController::KeyboardBrightnessController()
0025     : m_kbdMaxBrightness(0)
0026     , m_cachedKeyboardBrightness(0)
0027     , m_kbdBacklight(nullptr)
0028 {
0029     m_kbdBacklight = new OrgFreedesktopUPowerKbdBacklightInterface(UPOWER_SERVICE, "/org/freedesktop/UPower/KbdBacklight", QDBusConnection::systemBus(), this);
0030     if (m_kbdBacklight->isValid()) {
0031         // Cache max value
0032         QDBusPendingReply<int> rep = m_kbdBacklight->GetMaxBrightness();
0033         rep.waitForFinished();
0034         if (rep.isValid()) {
0035             m_kbdMaxBrightness = rep.value();
0036             m_keyboardBrightnessAvailable = true;
0037         }
0038         // TODO Do a proper check if the kbd backlight dbus object exists. But that should work for now ..
0039         if (m_kbdMaxBrightness) {
0040             m_cachedKeyboardBrightness = keyboardBrightness();
0041             qCDebug(POWERDEVIL) << "current keyboard backlight brightness value: " << m_cachedKeyboardBrightness;
0042             connect(m_kbdBacklight,
0043                     &OrgFreedesktopUPowerKbdBacklightInterface::BrightnessChangedWithSource,
0044                     this,
0045                     &KeyboardBrightnessController::onKeyboardBrightnessChanged);
0046         }
0047     }
0048 }
0049 
0050 int KeyboardBrightnessController::keyboardBrightnessSteps()
0051 {
0052     m_keyboardBrightnessLogic.setValueMax(keyboardBrightnessMax());
0053     return m_keyboardBrightnessLogic.steps();
0054 }
0055 
0056 int KeyboardBrightnessController::calculateNextKeyboardBrightnessStep(int value, int valueMax, PowerDevil::BrightnessLogic::BrightnessKeyType keyType)
0057 {
0058     m_keyboardBrightnessLogic.setValueMax(valueMax);
0059     m_keyboardBrightnessLogic.setValue(value);
0060     m_keyboardBrightnessLogic.setValueBeforeTogglingOff(m_keyboardBrightnessBeforeTogglingOff);
0061 
0062     return m_keyboardBrightnessLogic.action(keyType);
0063 }
0064 
0065 int KeyboardBrightnessController::keyboardBrightness() const
0066 {
0067     int result = m_kbdBacklight->GetBrightness();
0068     qCDebug(POWERDEVIL) << "Kbd backlight brightness value: " << result;
0069 
0070     return result;
0071 }
0072 
0073 int KeyboardBrightnessController::keyboardBrightnessMax() const
0074 {
0075     int result = m_kbdMaxBrightness;
0076     qCDebug(POWERDEVIL) << "Kbd backlight brightness value max: " << result;
0077 
0078     return result;
0079 }
0080 
0081 void KeyboardBrightnessController::setKeyboardBrightness(int value)
0082 {
0083     qCDebug(POWERDEVIL) << "set kbd backlight value: " << value;
0084     m_kbdBacklight->SetBrightness(value);
0085     m_keyboardBrightnessBeforeTogglingOff = keyboardBrightness();
0086 }
0087 
0088 void KeyboardBrightnessController::setKeyboardBrightnessOff()
0089 {
0090     // save value before toggling so that we can restore it later
0091     m_keyboardBrightnessBeforeTogglingOff = keyboardBrightness();
0092     qCDebug(POWERDEVIL) << "set kbd backlight value: " << 0;
0093     m_kbdBacklight->SetBrightness(0);
0094 }
0095 
0096 bool KeyboardBrightnessController::keyboardBrightnessAvailable() const
0097 {
0098     return m_keyboardBrightnessAvailable;
0099 }
0100 
0101 int KeyboardBrightnessController::keyboardBrightnessKeyPressed(PowerDevil::BrightnessLogic::BrightnessKeyType type)
0102 {
0103     if (!m_keyboardBrightnessAvailable) {
0104         return -1; // ignore as we are not able to determine the brightness level
0105     }
0106 
0107     int currentBrightness = keyboardBrightness();
0108     // m_cachedBrightnessMap is not being updated during animation, thus checking the m_cachedBrightnessMap
0109     // value here doesn't make much sense, use the endValue from brightness() anyway.
0110     // This prevents brightness key being ignored during the animation.
0111     if (currentBrightness != m_cachedKeyboardBrightness) {
0112         m_cachedKeyboardBrightness = currentBrightness;
0113         return currentBrightness;
0114     }
0115 
0116     int maxBrightness = keyboardBrightnessMax();
0117     int newBrightness = calculateNextKeyboardBrightnessStep(currentBrightness, maxBrightness, type);
0118 
0119     if (newBrightness < 0) {
0120         return -1;
0121     }
0122 
0123     if (type == PowerDevil::BrightnessLogic::BrightnessKeyType::Toggle && newBrightness == 0) {
0124         setKeyboardBrightnessOff();
0125     } else {
0126         setKeyboardBrightness(newBrightness);
0127     }
0128     return newBrightness;
0129 }
0130 
0131 void KeyboardBrightnessController::onKeyboardBrightnessChanged(int value, const QString &source)
0132 {
0133     qCDebug(POWERDEVIL) << "Keyboard brightness changed!!";
0134     if (value != m_cachedKeyboardBrightness) {
0135         m_cachedKeyboardBrightness = value;
0136         // source: internal = keyboard brightness changed through hardware, eg a firmware-handled hotkey being pressed -> show the OSD
0137         //         external = keyboard brightness changed through upower -> don't trigger the OSD as we would already have done that where necessary
0138         m_keyboardBrightnessLogic.setValueMax(keyboardBrightnessMax());
0139         m_keyboardBrightnessLogic.setValue(value);
0140 
0141         if (source == QLatin1String("internal")) {
0142             BrightnessOSDWidget::show(m_keyboardBrightnessLogic.percentage(value), PowerDevil::BrightnessControlType::Keyboard);
0143         }
0144 
0145         Q_EMIT keyboardBrightnessChanged(m_keyboardBrightnessLogic.info());
0146     }
0147 }
0148 
0149 #include "moc_keyboardbrightnesscontroller.cpp"