diff --git a/DeviceManager/CMakeLists.txt b/DeviceManager/CMakeLists.txt index 60992c6..dcd5430 100644 --- a/DeviceManager/CMakeLists.txt +++ b/DeviceManager/CMakeLists.txt @@ -19,6 +19,14 @@ else() set(ARCH_DIR "x86") endif() +option(IS_DEBUG "Enable debug mode" OFF) +if (CMAKE_BUILD_TYPE STREQUAL "Debug") + set(IS_DEBUG ON) +else() + set(IS_DEBUG OFF) +endif() + + set(WinDevice_OUTPUT "../WinDevice/output") message("WinDevice_OUTPUT: ${WinDevice_OUTPUT}") # 获取架构信息 @@ -27,7 +35,7 @@ set(WinDevice_INCLUDE_DIR "${WinDevice_OUTPUT}/include/") message("WinDevice_INCLUDE_DIR: ${WinDevice_INCLUDE_DIR}") set(WinDevice_THIRD_INCLUDE_DIR "${WinDevice_OUTPUT}/third_lib/") message("WinDevice_THIRD_INCLUDE_DIR: ${WinDevice_THIRD_INCLUDE_DIR}") -if (CMAKE_BUILD_TYPE STREQUAL "Debug") +if (IS_DEBUG) set(WinDevice_LIB_DIR "${WinDevice_OUTPUT}/${ARCH_DIR}/debug/") else() set(WinDevice_LIB_DIR "${WinDevice_OUTPUT}/${ARCH_DIR}/release/") @@ -79,22 +87,11 @@ else() endif() endif() -# 将 DeviceManager 目标的构建依赖于 BuildWinDevice 自定义目标 -# add_dependencies(DeviceManager BuildWinDevice) -message("include_directories ${WinDevice_INCLUDE_DIR} ${WinDevice_THIRD_INCLUDE_DIR}") - -# 添加头文件目录 -include_directories(${WinDevice_INCLUDE_DIR} ${WinDevice_THIRD_INCLUDE_DIR}) -# 使用 find_library 查找动态库文件 -find_library(WinDevice_LIB NAMES WinDevice HINTS ${WinDevice_LIB_DIR}) -# 如果找到 WinDevice 库,才链接到 DeviceManager -if(WinDevice_LIB) - message(STATUS "Found WinDevice library: ${WinDevice_LIB}") - target_link_libraries(DeviceManager PRIVATE ${WinDevice_LIB}) -else() - message(WARNING "WinDevice library not found in ${WinDevice_LIB_DIR}") -endif() +# 添加 WinDevice 子目录 +add_subdirectory(../WinDevice ${CMAKE_BINARY_DIR}/WinDevice) +# 链接 WinDevice 库 +target_link_libraries(DeviceManager PRIVATE WinDevice) # 假设 WinDevice 是在 WinDevice CMakeLists.txt 中创建的目标 target_link_libraries(DeviceManager PRIVATE Qt${QT_VERSION_MAJOR}::Widgets) diff --git a/DeviceManager/Utils/LogManager.cpp b/DeviceManager/Utils/LogManager.cpp index 5b110ac..ffaaa8b 100644 --- a/DeviceManager/Utils/LogManager.cpp +++ b/DeviceManager/Utils/LogManager.cpp @@ -1,12 +1,38 @@ // // Created by zyz on 2023/10/17. // +#ifdef WIN32 +#include "windows.h" +#endif #include "LogManager.h" void LogManager::CustomMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { QByteArray localMsg = msg.toUtf8(); +#ifdef WIN32 + + QString outputMsg; + switch (type) { + case QtDebugMsg: + outputMsg = QString("Debug: %1 (%2:%3, %4)").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function); + break; + case QtInfoMsg: + outputMsg = QString("Info: %1 (%2:%3, %4)").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function); + break; + case QtWarningMsg: + outputMsg = QString("Warning: %1 (%2:%3, %4)").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function); + break; + case QtCriticalMsg: + outputMsg = QString("Critical: %1 (%2:%3, %4)").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function); + break; + case QtFatalMsg: + outputMsg = QString("Fatal: %1 (%2:%3, %4)").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function); + abort(); + } + // 使用 OutputDebugStringA 输出消息 + ::OutputDebugStringA(qUtf8Printable(outputMsg + "\n")); +#else switch (type) { case QtDebugMsg: fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); @@ -24,4 +50,5 @@ void LogManager::CustomMessageHandler(QtMsgType type, const QMessageLogContext & fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); abort(); } +#endif } diff --git a/DeviceManager/main.cpp b/DeviceManager/main.cpp index 53bd123..d0b489b 100644 --- a/DeviceManager/main.cpp +++ b/DeviceManager/main.cpp @@ -2,26 +2,27 @@ #include #include "Utils/LogManager.h" -#include "Video/ScreenManager.h" +#include "../WinDevice/src/Video/ScreenManager.h" +#include "../WinDevice/src/WinDeviceManager.h" + int main(int argc, char *argv[]) { QApplication a(argc, argv); qInstallMessageHandler(LogManager::CustomMessageHandler); + WinDevice::WinDeviceManager::Init(); ScreenManager screenManager; screenManager.UpdateDisplayInfo(); - HRESULT hr = S_OK; - for (size_t i = 0; i < screenManager._displayAdapterList.size(); i++) { - DXGI_ADAPTER_DESC adapterDesc; - hr = screenManager._displayAdapterList[i]->GetDesc(&adapterDesc); - if (SUCCEEDED(hr)) { - qDebug("Adapter: %s", adapterDesc.Description); - } + qDebug("Adapter Count: %d", screenManager._displayDeviceList.size()); + for (size_t i = 0; i < screenManager._displayDeviceList.size(); i++) { + qDebug("Adapter: %s", screenManager._displayDeviceList[i].DeviceName); } MainWindow w; w.show(); return a.exec(); } + + diff --git a/WinDevice/CMakeLists.txt b/WinDevice/CMakeLists.txt index f089d7e..da603a9 100644 --- a/WinDevice/CMakeLists.txt +++ b/WinDevice/CMakeLists.txt @@ -129,7 +129,14 @@ file(GLOB_RECURSE EXPORTED_HEADERS ${CMAKE_SOURCE_DIR}/src/*.h) foreach (HEADER ${EXPORTED_HEADERS}) get_filename_component(HEADER_DIR ${HEADER} DIRECTORY) get_filename_component(HEADER_NAME ${HEADER} NAME) - string(REPLACE "${CMAKE_SOURCE_DIR}/src/" "" HEADER_RELATIVE_PATH ${HEADER_DIR}) + # 检查 HEADER_DIR 是否包含 "/src/" + string(FIND ${HEADER_DIR} "/src/" SRC_DIR_INDEX) + # 判断是否找到 + if (SRC_DIR_INDEX GREATER -1) + string(REPLACE "${CMAKE_SOURCE_DIR}/src/" "" HEADER_RELATIVE_PATH ${HEADER_DIR}) + else() + string(REPLACE "${CMAKE_SOURCE_DIR}/src" "" HEADER_RELATIVE_PATH ${HEADER_DIR}) + endif() file(COPY ${HEADER} DESTINATION ${OUTPUT_DIR}/include/${HEADER_RELATIVE_PATH}) endforeach () diff --git a/WinDevice/src/Utils/Log.cpp b/WinDevice/src/Utils/Log.cpp index 5c8ffb2..cf30f5c 100644 --- a/WinDevice/src/Utils/Log.cpp +++ b/WinDevice/src/Utils/Log.cpp @@ -1,9 +1,12 @@ #include "Log.h" #include "spdlog/spdlog.h" +#include "spdlog/sinks/stdout_sinks.h" void Log::Init(LogLevel level, std::string fileName) { + spdlog::stdout_logger_mt("console"); + spdlog::set_level(static_cast(level)); // 设置日志级别为 debug spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v"); } diff --git a/WinDevice/src/Utils/StringUtil.h b/WinDevice/src/Utils/StringUtil.h index 5516d60..481f3c5 100644 --- a/WinDevice/src/Utils/StringUtil.h +++ b/WinDevice/src/Utils/StringUtil.h @@ -2,17 +2,24 @@ #include #include -template -To Convert(const From& input) { - std::wstring_convert, typename From::value_type> converter; - return converter.to_bytes(input); -} +namespace WinDevcie { -std::string Wstring2String(const std::wstring& input) { - return Convert(input); -} +namespace Utils { -std::string Wchar2String(const WCHAR* input) { - std::wstring_convert> converter; - return converter.to_bytes(input); -} \ No newline at end of file + template + To Convert(const From& input) { + std::wstring_convert, typename From::value_type> converter; + return converter.to_bytes(input); + } + + std::string Wstring2String(const std::wstring& input) { + return Convert(input); + } + + std::string Wchar2String(const WCHAR* input) { + std::wstring_convert> converter; + return converter.to_bytes(input); + } + } + +} diff --git a/WinDevice/src/Video/ScreenManager.cpp b/WinDevice/src/Video/ScreenManager.cpp index dd7ffda..9ff2df5 100644 --- a/WinDevice/src/Video/ScreenManager.cpp +++ b/WinDevice/src/Video/ScreenManager.cpp @@ -1,7 +1,7 @@ #include "ScreenManager.h" #include "spdlog/spdlog.h" -#include "Utils/StringUtil.h" -#include "Utils/Log.h" +#include "../Utils/StringUtil.h" +#include "../Utils/Log.h" #include ScreenManager::ScreenManager() @@ -56,7 +56,7 @@ void ScreenManager::_UpdateDisplayAdapterList() DXGI_ADAPTER_DESC adapterDesc; pAdapter->GetDesc(&adapterDesc); spdlog::info("Adapter Index:{0}, Description:{1}, DeviceId:{2}, VendorId:{3}, SubSysId:{4}, Revision:{5}, AdapterLuid(H-L):{6}-{7} ", - adapterIndex, Wchar2String(adapterDesc.Description), adapterDesc.DeviceId, adapterDesc.VendorId, adapterDesc.SubSysId, + adapterIndex, WinDevcie::Utils::Wchar2String(adapterDesc.Description), adapterDesc.DeviceId, adapterDesc.VendorId, adapterDesc.SubSysId, adapterDesc.Revision, adapterDesc.AdapterLuid.HighPart, adapterDesc.AdapterLuid.LowPart); // print adapter output info @@ -72,7 +72,7 @@ void ScreenManager::_UpdateDisplayAdapterList() { // 输出友好名称 spdlog::info("Adapter Output Index:{0}, DeviceName:{1}, szDevice:{2}, right:{3}, bottom:{4}", - j, Wchar2String(outputDesc.DeviceName), monitorInfo.szDevice, monitorInfo.rcMonitor.right, monitorInfo.rcMonitor.bottom); + j, WinDevcie::Utils::Wchar2String(outputDesc.DeviceName), monitorInfo.szDevice, monitorInfo.rcMonitor.right, monitorInfo.rcMonitor.bottom); } } } @@ -110,7 +110,7 @@ BOOL ScreenManager::_EnumMonitorProc(HMONITOR hMonitor) if (DisplayConfigGetDeviceInfo(&targetDeviceName.header) == ERROR_SUCCESS) { spdlog::info("_UpdateMonitorInfoMap, monitorDevicePath:{0}, monitorFriendlyDeviceName:{1}", - Wchar2String(targetDeviceName.monitorDevicePath), Wchar2String(targetDeviceName.monitorFriendlyDeviceName)); + WinDevcie::Utils::Wchar2String(targetDeviceName.monitorDevicePath), WinDevcie::Utils::Wchar2String(targetDeviceName.monitorFriendlyDeviceName)); // 获取指定 HMONITOR 的分辨率信息 DEVMODE dm; diff --git a/WinDevice/src/WinDeviceManager.cpp b/WinDevice/src/WinDeviceManager.cpp new file mode 100644 index 0000000..71d8cb1 --- /dev/null +++ b/WinDevice/src/WinDeviceManager.cpp @@ -0,0 +1,15 @@ + +#include "WinDeviceManager.h" +#include "Utils/Log.h" + +WinDevice::WinDeviceManager::WinDeviceManager() { + +} + +WinDevice::WinDeviceManager::~WinDeviceManager() { + +} + +void WinDevice::WinDeviceManager::Init() { + Log::Init(LogLevel::Debug, "WinDevice.log"); +} diff --git a/WinDevice/src/WinDeviceManager.h b/WinDevice/src/WinDeviceManager.h new file mode 100644 index 0000000..7b64c3a --- /dev/null +++ b/WinDevice/src/WinDeviceManager.h @@ -0,0 +1,16 @@ +#ifndef WINDEVICEMANAGER_H +#define WINDEVICEMANAGER_H + +#endif // WINDEVICEMANAGER_H + +namespace WinDevice { + +class WinDeviceManager +{ +public: + WinDeviceManager(); + ~WinDeviceManager(); + + static void Init(); +}; +}