Implement setting monitor brightness
This commit is contained in:
parent
058ba0846b
commit
5197295368
@ -2,7 +2,9 @@
|
|||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <physicalmonitorenumerationapi.h>
|
#include <physicalmonitorenumerationapi.h>
|
||||||
@ -46,6 +48,8 @@ class Monitor {
|
|||||||
|
|
||||||
const HMONITOR m_hMonitor;
|
const HMONITOR m_hMonitor;
|
||||||
const PhysicalMonitor m_physicalMonitor;
|
const PhysicalMonitor m_physicalMonitor;
|
||||||
|
|
||||||
|
std::optional<std::pair<DWORD, DWORD>> m_brightnessRange;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -34,9 +34,37 @@ Monitor::~Monitor()
|
|||||||
|
|
||||||
bool Monitor::setBrightness(float percentage)
|
bool Monitor::setBrightness(float percentage)
|
||||||
{
|
{
|
||||||
|
if(!m_brightnessRange) {
|
||||||
|
qDebug(ltr("No brightness range known for 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));
|
||||||
|
|
||||||
|
if(std::isnan(getBrightness()))
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto mappedBrightness = map(percentage, 0.f, 1.f, m_brightnessRange->first, m_brightnessRange->second);
|
||||||
|
const auto newBrightness = std::clamp<DWORD>(std::round(mappedBrightness), m_brightnessRange->first, m_brightnessRange->second);
|
||||||
|
|
||||||
|
if(!SetMonitorBrightness(m_physicalMonitor.handle, newBrightness)) {
|
||||||
|
qCritical(ltr("Failed to set 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 false;
|
||||||
|
}
|
||||||
|
|
||||||
|
qDebug(ltr("Setting brightness of monitor '%1' with handle '0x%2' and physical handle '0x%3' to %4")
|
||||||
|
.arg(m_physicalMonitor.name)
|
||||||
|
.arg(reinterpret_cast<qulonglong>(m_hMonitor), 0, 16)
|
||||||
|
.arg(reinterpret_cast<qulonglong>(m_physicalMonitor.handle), 0, 16)
|
||||||
|
.arg(newBrightness));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
float Monitor::getBrightness()
|
float Monitor::getBrightness()
|
||||||
{
|
{
|
||||||
qDebug(ltr("Getting brightness of monitor '%1' with handle '0x%2' and physical handle '0x%3'")
|
qDebug(ltr("Getting brightness of monitor '%1' with handle '0x%2' and physical handle '0x%3'")
|
||||||
@ -57,6 +85,8 @@ float Monitor::getBrightness()
|
|||||||
return NAN;
|
return NAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_brightnessRange = std::pair{minBrightness, maxBrightness};
|
||||||
|
|
||||||
return std::clamp(map(brightness, minBrightness, maxBrightness, 0.f, 1.f), 0.f, 1.f);
|
return std::clamp(map(brightness, minBrightness, maxBrightness, 0.f, 1.f), 0.f, 1.f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user