Properly destroy physical monitor handle

This commit is contained in:
BlackMark 2020-07-08 21:50:49 +02:00
parent 927c318f54
commit 19d73640c7
3 changed files with 25 additions and 4 deletions

View File

@ -19,6 +19,8 @@ extern BOOL enumMonitorsCallback(HMONITOR, HDC, LPRECT, LPARAM);
class Monitor {
public:
Monitor() = delete;
Monitor(const Monitor&) = delete;
Monitor(Monitor&& other) noexcept;
~Monitor();
bool setBrightness(float percentage);
@ -36,8 +38,8 @@ class Monitor {
friend BOOL detail::enumMonitorsCallback(HMONITOR, HDC, LPRECT, LPARAM);
const HMONITOR m_hMonitor;
const PhysicalMonitor m_physicalMonitor;
HMONITOR m_hMonitor;
PhysicalMonitor m_physicalMonitor;
std::optional<std::pair<DWORD, DWORD>> m_brightnessRange;
bool m_errorOccurred = false;

View File

@ -28,14 +28,14 @@ static BOOL enumMonitorsCallback(HMONITOR hMonitor, [[maybe_unused]] HDC hDC, [[
}
for(const auto& physicalMonitor: physicalMonitors) {
const auto monitor = Monitor(hMonitor, physicalMonitor);
auto monitor = Monitor(hMonitor, physicalMonitor);
if(monitor.hasBrightnessCapability()) {
qInfo(ltr("Found brightness capable monitor '%1' with handle '0x%2' and physical handle '0x%3'")
.arg(monitor.m_physicalMonitor.name)
.arg(reinterpret_cast<qulonglong>(monitor.m_hMonitor), 0, 16)
.arg(reinterpret_cast<qulonglong>(monitor.m_physicalMonitor.handle), 0, 16));
monitors.push_back(monitor);
monitors.push_back(std::move(monitor));
}
}

View File

@ -25,12 +25,31 @@ Monitor::Monitor(HMONITOR hMonitor, PHYSICAL_MONITOR physicalMonitor)
.arg(reinterpret_cast<qulonglong>(m_physicalMonitor.handle), 0, 16));
}
Monitor::Monitor(Monitor&& other) noexcept
: m_hMonitor(other.m_hMonitor)
, m_physicalMonitor(other.m_physicalMonitor)
, m_brightnessRange(other.m_brightnessRange)
, m_errorOccurred(other.m_errorOccurred)
{
other.m_hMonitor = 0;
other.m_physicalMonitor.name.clear();
other.m_physicalMonitor.handle = 0;
}
Monitor::~Monitor()
{
qDebug(ltr("Destroying 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(m_physicalMonitor.handle && !DestroyPhysicalMonitor(m_physicalMonitor.handle)) {
qCritical(ltr("Error occurred while destroying 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()));
}
}
bool Monitor::setBrightness(float percentage)