From 954b20baff7534450450318b78d93344524f3a43 Mon Sep 17 00:00:00 2001 From: aavit Date: Tue, 13 Jan 2015 15:52:46 +0100 Subject: B2Qt utils: implement backlight setting for eLinux This at least works for Sabre and BD-SL, and should be easy enough to extend if other boards have the controlling device in a different path. In case of multiple screens, this version just sets all of them. This is sufficient for demos and the common single-screen case. Next version will add API to address each screen separately. Change-Id: I1bbd5201474bd92d20efba6d55b11339200c141a Reviewed-by: Andy Nichols --- src/utils/b2qtdevice.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 5 deletions(-) diff --git a/src/utils/b2qtdevice.cpp b/src/utils/b2qtdevice.cpp index d7a7cd2..689b01f 100644 --- a/src/utils/b2qtdevice.cpp +++ b/src/utils/b2qtdevice.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #ifdef Q_OS_ANDROID_NO_SDK #include @@ -70,6 +71,50 @@ void B2QtDevice::powerOff() } +class LightDevice +{ +public: + QString name; + QString deviceFile; + quint8 value; + uint maxValue; +}; + +static QList lightDevices; +static bool lightDevicesInitialized = false; + +static void initLightDevices() +{ + if (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(); + lightDevices.append(ld); + } + if (!lightDevices.isEmpty()) + knownBrightness = lightDevices.at(0).value; + lightDevicesInitialized = true; +} + /*! * Sets the display brightness (i.e. the intensity of the backlight) * to \a value. A value of 255 requests maximum brightness, while 0 requests @@ -99,13 +144,21 @@ bool B2QtDevice::setDisplayBrightness(quint8 value) return false; device->common.close(&device->common); - knownBrightness = value; - emit displayBrightnessChanged(value); - return true; #else - Q_UNUSED(value); - return false; + initLightDevices(); + for (int i = 0; i < lightDevices.size(); i++) { + LightDevice &ld = 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; + } #endif + knownBrightness = value; + return true; } @@ -115,6 +168,7 @@ bool B2QtDevice::setDisplayBrightness(quint8 value) */ quint8 B2QtDevice::displayBrightness() const { +#ifdef Q_OS_ANDROID_NO_SDK QFile sysFile(QStringLiteral("/sys/class/leds/lcd-backlight/brightness")); if (sysFile.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) { bool ok = false; @@ -122,6 +176,10 @@ quint8 B2QtDevice::displayBrightness() const if (ok) knownBrightness = qBound(0, sysVal, 255); } +#else + initLightDevices(); +#endif + return knownBrightness; } -- cgit v1.2.3