From 7afbc443c932678c0a61f70ce1c767c9f2fc6e17 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Sun, 5 Jul 2020 16:56:27 +0200 Subject: [PATCH] Implement monitor enumeration --- AdaptiveBrightness/monitor.hpp | 34 ++++++++++++++- AdaptiveBrightness/monitor_control_win.cpp | 49 +++++++++++++++++++++- AdaptiveBrightness/monitor_win.cpp | 2 +- CMakeLists.txt | 6 +++ 4 files changed, 88 insertions(+), 3 deletions(-) diff --git a/AdaptiveBrightness/monitor.hpp b/AdaptiveBrightness/monitor.hpp index 5965b45..828f8b0 100644 --- a/AdaptiveBrightness/monitor.hpp +++ b/AdaptiveBrightness/monitor.hpp @@ -1,8 +1,40 @@ #pragma once +#ifdef _MSC_VER + + #include + + #include + #include + +namespace detail { + +extern BOOL enumMonitorsCallback(HMONITOR, HDC, LPRECT, LPARAM); + +} // namespace detail + class Monitor { + public: + Monitor() = delete; + private: + friend BOOL detail::enumMonitorsCallback(HMONITOR, HDC, LPRECT, LPARAM); + + Monitor(HMONITOR hMonitor, PHYSICAL_MONITOR hPhysicalMonitor); + + const HMONITOR m_hMonitor; + const PHYSICAL_MONITOR m_physicalMonitor; +}; + +#endif + +#if defined(__clang__) || defined(__GNUC__) + +class Monitor { + public: Monitor(); - public: + private: }; + +#endif diff --git a/AdaptiveBrightness/monitor_control_win.cpp b/AdaptiveBrightness/monitor_control_win.cpp index 6334010..9db8da7 100644 --- a/AdaptiveBrightness/monitor_control_win.cpp +++ b/AdaptiveBrightness/monitor_control_win.cpp @@ -2,9 +2,56 @@ #ifdef _MSC_VER -std::vector enumerateMonitors() + #include + #include + #include + + #include "log_tr.hpp" + +namespace detail { + +static QString getLastErrorString() { + DWORD lastErrorId = GetLastError(); + return {}; } +static BOOL enumMonitorsCallback(HMONITOR hMonitor, [[maybe_unused]] HDC hDC, [[maybe_unused]] LPRECT lpRect, LPARAM lParam) +{ + std::vector& monitors = *reinterpret_cast*>(lParam); + DWORD numMonitors; + + if(!GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &numMonitors)) { + qCritical(ltr("Unable to get number of physical monitors from handle, error: %1").arg(detail::getLastErrorString())); + return true; // Continue enumerating + } + + std::vector physicalMonitors(numMonitors); + + if(!GetPhysicalMonitorsFromHMONITOR(hMonitor, numMonitors, physicalMonitors.data())) { + qCritical(ltr("Unable to get physical monitors from handle, error: %1").arg(detail::getLastErrorString())); + return true; // Continue enumerating + } + + for(const auto& physicalMonitor: physicalMonitors) { + monitors.push_back(Monitor(hMonitor, physicalMonitor)); + } + + return true; +} + +} // namespace detail + +std::vector enumerateMonitors() +{ + std::vector monitors; + + if(!EnumDisplayMonitors(nullptr, nullptr, detail::enumMonitorsCallback, reinterpret_cast(&monitors))) { + qCritical(ltr("Unable to enumerate display monitors, error: %1").arg(detail::getLastErrorString())); + } + + return monitors; +} + #endif diff --git a/AdaptiveBrightness/monitor_win.cpp b/AdaptiveBrightness/monitor_win.cpp index 89aded6..3383f9e 100644 --- a/AdaptiveBrightness/monitor_win.cpp +++ b/AdaptiveBrightness/monitor_win.cpp @@ -2,6 +2,6 @@ #ifdef _MSC_VER -Monitor::Monitor() {} +Monitor::Monitor(HMONITOR hMonitor, PHYSICAL_MONITOR hPhysicalMonitor) : m_hMonitor(hMonitor), m_physicalMonitor(m_physicalMonitor) {} #endif diff --git a/CMakeLists.txt b/CMakeLists.txt index 596f9b7..1adae54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -71,6 +71,12 @@ target_link_libraries(AdaptiveBrightness Qt5::SerialPort ) +if(MSVC) +target_link_libraries(AdaptiveBrightness + Dxva2.lib +) +endif() + if(MSVC) get_target_property(QT5_QMAKE_EXECUTABLE Qt5::qmake IMPORTED_LOCATION) get_filename_component(QT5_WINDEPLOYQT_EXECUTABLE ${QT5_QMAKE_EXECUTABLE} PATH)