diff options
Diffstat (limited to 'src/displaysettings')
-rw-r--r-- | src/displaysettings/displaysettings.cpp | 97 | ||||
-rw-r--r-- | src/displaysettings/displaysettings.h | 76 | ||||
-rw-r--r-- | src/displaysettings/displaysettings.pro | 17 | ||||
-rw-r--r-- | src/displaysettings/displaysettings_p.cpp | 247 | ||||
-rw-r--r-- | src/displaysettings/displaysettings_p.h | 100 |
5 files changed, 537 insertions, 0 deletions
diff --git a/src/displaysettings/displaysettings.cpp b/src/displaysettings/displaysettings.cpp new file mode 100644 index 0000000..8066993 --- /dev/null +++ b/src/displaysettings/displaysettings.cpp @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "displaysettings.h" +#include "displaysettings_p.h" + +DisplaySettings::DisplaySettings(QObject *parent) + : QObject(parent) + ,d_ptr(new DisplaySettingsPrivate(this)) +{ +} + +DisplaySettings::~DisplaySettings() +{ +} + + +/*! + * Sets the display brightness (i.e. the intensity of the backlight) + * to \a value. A value of 255 requests maximum brightness, while 0 requests + * minimum (typically, the backlight turned off). + * + * Returns true on success. + */ +bool DisplaySettings::setDisplayBrightness(int v) +{ + Q_D(DisplaySettings); + return d->setDisplayBrightness(v); +} + + +/*! + * Returns the current backlight intensity. + * \sa setDisplayBrightness + */ +int DisplaySettings::displayBrightness() +{ + Q_D(DisplaySettings); + return d->displayBrightness(); +} + + +int DisplaySettings::physicalScreenSizeInch() const +{ + Q_D(const DisplaySettings); + return d->physicalScreenSizeInch(); +} + +void DisplaySettings::setPhysicalScreenSizeInch(int inches) +{ + Q_D(DisplaySettings); + d->setPhysicalScreenSizeInch(inches); +} + +bool DisplaySettings::physicalScreenSizeOverride() const +{ + Q_D(const DisplaySettings); + return d->physicalScreenSizeOverride(); +} + +void DisplaySettings::setPhysicalScreenSizeOverride(bool enable) +{ + Q_D(DisplaySettings); + d->setPhysicalScreenSizeOverride(enable); +} diff --git a/src/displaysettings/displaysettings.h b/src/displaysettings/displaysettings.h new file mode 100644 index 0000000..fc8fe74 --- /dev/null +++ b/src/displaysettings/displaysettings.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef DISPLAYSETTINGS_H +#define DISPLAYSETTINGS_H + +#include <qobject.h> + +class DisplaySettingsPrivate; + +class Q_DECL_EXPORT DisplaySettings : public QObject +{ + Q_OBJECT + + Q_PROPERTY(int displayBrightness READ displayBrightness WRITE setDisplayBrightness NOTIFY displayBrightnessChanged) + Q_PROPERTY(int physicalScreenSizeInch READ physicalScreenSizeInch WRITE setPhysicalScreenSizeInch NOTIFY physicalScreenSizeInchChanged) + Q_PROPERTY(bool physicalScreenSizeOverride READ physicalScreenSizeOverride WRITE setPhysicalScreenSizeOverride NOTIFY physicalScreenSizeOverrideChanged) + +public: + DisplaySettings(QObject *parent = 0); + ~DisplaySettings(); + + int displayBrightness(); + int physicalScreenSizeInch() const; + bool physicalScreenSizeOverride() const; + +public Q_SLOTS: + bool setDisplayBrightness(int value); + void setPhysicalScreenSizeInch(int inches); + void setPhysicalScreenSizeOverride(bool enable); + +signals: + void displayBrightnessChanged(int newValue); + void physicalScreenSizeInchChanged(int newInches); + void physicalScreenSizeOverrideChanged(bool newValue); + +protected: + DisplaySettingsPrivate *d_ptr; + + Q_DISABLE_COPY(DisplaySettings) + Q_DECLARE_PRIVATE(DisplaySettings) +}; + +#endif // DISPLAYSETTINGS_H diff --git a/src/displaysettings/displaysettings.pro b/src/displaysettings/displaysettings.pro new file mode 100644 index 0000000..c03168d --- /dev/null +++ b/src/displaysettings/displaysettings.pro @@ -0,0 +1,17 @@ +load(qt_build_config) + +TARGET = QtDisplaySettings +VERSION = 1.0 +CONFIG += dll warn_on + +QT = core + +MODULE = displaysettings +load(qt_module) + +SOURCES += displaysettings.cpp \ + displaysettings_p.cpp + +HEADERS += displaysettings.h \ + displaysettings_p.h + diff --git a/src/displaysettings/displaysettings_p.cpp b/src/displaysettings/displaysettings_p.cpp new file mode 100644 index 0000000..867531a --- /dev/null +++ b/src/displaysettings/displaysettings_p.cpp @@ -0,0 +1,247 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include <QtMath> +#include <QDirIterator> +#include "displaysettings_p.h" + +DisplaySettingsPrivate::DisplaySettingsPrivate(DisplaySettings *qq) + :q_ptr(qq) + ,m_brightness(255) + ,m_lightDevicesInitialized(false) + ,m_physScreenSize(new PhysicalScreenSize(qq)) +{ + +} + +void DisplaySettingsPrivate::initLightDevices() +{ + if (m_lightDevicesInitialized) + return; + QDirIterator it(QStringLiteral("/sys/class/backlight")); + while (it.hasNext()) { + LightDevice ld; + ld.deviceFile = it.next() + QStringLiteral("/brightness"); + QFile maxFile(it.filePath() + QStringLiteral("/max_brightness")); + if (!maxFile.open(QIODevice::ReadOnly)) + continue; + bool ok = false; + ld.maxValue = maxFile.read(10).simplified().toUInt(&ok); + if (!ok || !ld.maxValue) + continue; + QFile valFile(ld.deviceFile); + if (!valFile.open(QIODevice::ReadOnly)) + continue; + ok = false; + uint val = valFile.read(10).simplified().toUInt(&ok); + if (!ok) + continue; + // map max->max as that is a common case, otherwise choose a reasonable value + ld.value = (val == ld.maxValue) ? 255 : (val * 256)/(ld.maxValue+1); + ld.name = it.fileName(); + m_lightDevices.append(ld); + } + if (!m_lightDevices.isEmpty()) + m_brightness = m_lightDevices.at(0).value; + m_lightDevicesInitialized = true; +} + + +bool DisplaySettingsPrivate::setDisplayBrightness(int v) +{ + Q_Q(DisplaySettings); + quint8 value = qBound(0, v, 255); + initLightDevices(); + for (int i = 0; i < m_lightDevices.size(); i++) { + LightDevice &ld = m_lightDevices[i]; + QFile devFile(ld.deviceFile); + if (!devFile.open(QIODevice::WriteOnly)) + continue; + // Maps only 0 to 0, since 0 often means "off"; other values are degrees of "on". + uint newVal = value ? 1 + ((value * ld.maxValue) / 256) : 0; + devFile.write(QByteArray::number(newVal)); + ld.value = value; + } + m_brightness = value; + return true; +} + +int DisplaySettingsPrivate::displayBrightness() +{ + initLightDevices(); + return m_brightness; +} + +int DisplaySettingsPrivate::physicalScreenSizeInch() const +{ + return m_physScreenSize->size(); +} + +void DisplaySettingsPrivate::setPhysicalScreenSizeInch(int inches) +{ + Q_Q(DisplaySettings); + if (m_physScreenSize->size() != inches) { + m_physScreenSize->setSize(inches); + } +} + +bool DisplaySettingsPrivate::physicalScreenSizeOverride() const +{ + return m_physScreenSize->enabled(); +} + +void DisplaySettingsPrivate::setPhysicalScreenSizeOverride(bool enable) +{ + Q_Q(DisplaySettings); + if (m_physScreenSize->enabled() != enable) { + m_physScreenSize->setEnabled(enable); + emit q->physicalScreenSizeOverrideChanged(enable); + } +} + +PhysicalScreenSize::PhysicalScreenSize(QObject *parent) + : QObject(parent) + ,physScreenSizeEnabled(false), physScreenSizeInch(7) +{ + physWriteTimer.setSingleShot(true); + physWriteTimer.setInterval(1000); + QObject::connect(&physWriteTimer, SIGNAL(timeout()), this, SLOT(onTimeout())); + + read(QStringLiteral("/etc/appcontroller.conf")); + read(QStringLiteral("/var/lib/b2qt/appcontroller.conf.d/physical_screen_size.conf")); +} + +PhysicalScreenSize::~PhysicalScreenSize() +{ + +} + +void PhysicalScreenSize::read(const QString &filename) +{ + QFile f(filename); + if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) + return; + + int physScreenWidth = 154, physScreenHeight = 90; + int found = 0; + while (!f.atEnd()) { + QByteArray line = f.readLine().trimmed(); + if (line.startsWith(QByteArrayLiteral("env="))) { + QByteArrayList values = line.split('='); + if (values.count() == 3) { + bool ok; + if (values[1] == QByteArrayLiteral("QT_QPA_EGLFS_PHYSICAL_WIDTH")) { + int val = values[2].toInt(&ok); + if (ok) { + ++found; + physScreenWidth = val; + } + } else if (values[1] == QByteArrayLiteral("QT_QPA_EGLFS_PHYSICAL_HEIGHT")) { + int val = values[2].toInt(&ok); + if (ok) { + ++found; + physScreenHeight = val; + } + } + } + } + } + if (found == 2) + physScreenSizeEnabled = true; + + const qreal diagMM = qSqrt(physScreenWidth * physScreenWidth + physScreenHeight * physScreenHeight); + physScreenSizeInch = qRound(diagMM / 25.4); +} + +void PhysicalScreenSize::onTimeout() +{ + write(); +} + +void PhysicalScreenSize::write(bool includePhysSize) +{ + QDir(QStringLiteral("/var/lib")).mkpath(QStringLiteral("b2qt/appcontroller.conf.d")); + write(QStringLiteral("/var/lib/b2qt/appcontroller.conf.d/physical_screen_size.conf"), includePhysSize); +} + +void PhysicalScreenSize::write(const QString &filename, bool includePhysSize) +{ + QFile f(filename); + + QByteArrayList lines; + if (f.open(QIODevice::ReadOnly | QIODevice::Text)) { + while (!f.atEnd()) { + QByteArray line = f.readLine().trimmed(); + if (!line.startsWith(QByteArrayLiteral("env=QT_QPA_EGLFS_PHYSICAL_WIDTH=")) + && !line.startsWith(QByteArrayLiteral("env=QT_QPA_EGLFS_PHYSICAL_HEIGHT="))) + lines.append(line); + } + f.close(); + } + + if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) + return; + + const qreal diagMM = physScreenSizeInch * 25.4; + // Assume 16:9 aspect ratio + const int physScreenHeight = qRound(diagMM / 1.975); + const int physScreenWidth = qRound(physScreenHeight * 1.777); + + foreach (const QByteArray &line, lines) + f.write(line + QByteArrayLiteral("\n")); + + if (includePhysSize) + f.write(QByteArrayLiteral("env=QT_QPA_EGLFS_PHYSICAL_WIDTH=") + QByteArray::number(physScreenWidth) + + QByteArrayLiteral("\nenv=QT_QPA_EGLFS_PHYSICAL_HEIGHT=") + QByteArray::number(physScreenHeight) + + QByteArrayLiteral("\n")); +} + +void PhysicalScreenSize::setSize(int inches) +{ + physScreenSizeInch = inches; + physWriteTimer.start(); +} + +bool PhysicalScreenSize::enabled() const +{ + return physScreenSizeEnabled; +} + +void PhysicalScreenSize::setEnabled(bool enable) +{ + physScreenSizeEnabled = enable; + // Rewrite appcontroller.conf with or without the physical width/height lines. + write(enable); +} diff --git a/src/displaysettings/displaysettings_p.h b/src/displaysettings/displaysettings_p.h new file mode 100644 index 0000000..149b9c7 --- /dev/null +++ b/src/displaysettings/displaysettings_p.h @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the Qt Device Utilities module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL3$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPLv3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or later as published by the Free +** Software Foundation and appearing in the file LICENSE.GPL included in +** the packaging of this file. Please review the following information to +** ensure the GNU General Public License version 2.0 requirements will be +** met: http://www.gnu.org/licenses/gpl-2.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef DISPLAYSETTINGSPRIVATE_H +#define DISPLAYSETTINGSPRIVATE_H + +#include <QTimer> +#include "displaysettings.h" + +class PhysicalScreenSize : public QObject +{ + Q_OBJECT + +public: + explicit PhysicalScreenSize(QObject *parent=0); + virtual ~PhysicalScreenSize(); + + void setSize(int inches); + int size() const { return physScreenSizeInch; } + bool enabled() const; + void setEnabled(bool enable); + +private slots: + void onTimeout(); + +private: + void read(const QString &filename); + void write(bool includePhysSize = true); + void write(const QString &filename, bool includePhysSize = true); + + bool physScreenSizeEnabled; + int physScreenSizeInch; + QTimer physWriteTimer; +}; + +class LightDevice +{ +public: + QString name; + QString deviceFile; + quint8 value; + uint maxValue; +}; + +class DisplaySettingsPrivate +{ + Q_DECLARE_PUBLIC(DisplaySettings) +public: + DisplaySettingsPrivate(DisplaySettings* qq); + int displayBrightness(); + int physicalScreenSizeInch() const; + bool physicalScreenSizeOverride() const; + + bool setDisplayBrightness(int value); + void setPhysicalScreenSizeInch(int inches); + void setPhysicalScreenSizeOverride(bool enable); +private: + void initLightDevices(); + DisplaySettings *q_ptr; + int m_brightness; + int m_screenSizeInch; + bool m_sizeOverride; + QList<LightDevice> m_lightDevices; + bool m_lightDevicesInitialized; + PhysicalScreenSize *m_physScreenSize; +}; + +#endif // DISPLAYSETTINGSPRIVATE_H |