Implement getting monitor brightness

This commit is contained in:
BlackMark 2020-07-05 20:47:55 +02:00
parent b9170388b6
commit 058ba0846b
2 changed files with 27 additions and 1 deletions

View File

@ -38,6 +38,12 @@ class Monitor {
bool hasBrightnessCapability() const;
template<typename InType, typename OutType>
auto map(InType value, InType inMin, InType inMax, OutType outMin, OutType outMax)
{
return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
const HMONITOR m_hMonitor;
const PhysicalMonitor m_physicalMonitor;
};

View File

@ -2,6 +2,8 @@
#ifdef _MSC_VER
#include <algorithm>
#include <cmath>
#include <QtDebug>
@ -37,9 +39,27 @@ bool Monitor::setBrightness(float percentage)
float Monitor::getBrightness()
{
qDebug(ltr("Getting brightness of monitor '%1' with handle '0x%2' and physical handle '0x%3'")
.arg(m_physicalMonitor.name)
.arg(reinterpret_cast<qulonglong>(m_hMonitor), 0, 16)
.arg(reinterpret_cast<qulonglong>(m_physicalMonitor.handle), 0, 16));
DWORD minBrightness = 0;
DWORD brightness = 0;
DWORD maxBrightness = 0;
if(!GetMonitorBrightness(m_physicalMonitor.handle, &minBrightness, &brightness, &maxBrightness)) {
qCritical(ltr("Failed to get brightness of monitor '%1' with handle '0x%2' and physical handle '0x%3', error: %4")
.arg(m_physicalMonitor.name)
.arg(reinterpret_cast<qulonglong>(m_hMonitor), 0, 16)
.arg(reinterpret_cast<qulonglong>(m_physicalMonitor.handle), 0, 16)
.arg(getLastErrorString()));
return NAN;
}
return std::clamp(map(brightness, minBrightness, maxBrightness, 0.f, 1.f), 0.f, 1.f);
}
Monitor::operator bool() const
{
return false;