From 5c8f902c11bc7a7bfed3ada57e4177d0535c2ca5 Mon Sep 17 00:00:00 2001 From: DevWiki Date: Sun, 29 Sep 2024 10:18:35 +0800 Subject: [PATCH] add namespace --- DeviceManager/CMakeLists.txt | 68 +++++++++++++-------------- DeviceManager/main.cpp | 2 +- WinDevice/src/Audio/AudioEnum.h | 12 +++-- WinDevice/src/Audio/AudioManager.cpp | 16 +++---- WinDevice/src/Audio/AudioManager.h | 38 ++++++++------- WinDevice/src/Utils/CmdUtil.cpp | 2 +- WinDevice/src/Utils/CmdUtil.h | 13 +++-- WinDevice/src/Utils/Log.cpp | 6 +-- WinDevice/src/Utils/Log.h | 27 ++++++----- WinDevice/src/Utils/StringUtil.h | 27 +++++------ WinDevice/src/Utils/TimeUtil.cpp | 5 +- WinDevice/src/Utils/TimeUtil.h | 15 +++--- WinDevice/src/Video/ScreenManager.cpp | 24 +++++----- WinDevice/src/Video/ScreenManager.h | 34 +++++++------- WinDevice/src/main.cpp | 4 +- 15 files changed, 151 insertions(+), 142 deletions(-) diff --git a/DeviceManager/CMakeLists.txt b/DeviceManager/CMakeLists.txt index dcd5430..c8cdf34 100644 --- a/DeviceManager/CMakeLists.txt +++ b/DeviceManager/CMakeLists.txt @@ -13,44 +13,42 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) add_compile_options("$<$:/source-charset:utf-8>") -if(CMAKE_SIZEOF_VOID_P EQUAL 8) - set(ARCH_DIR "x64") -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() -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}") +# # 获取架构信息 +# if(CMAKE_SIZEOF_VOID_P EQUAL 8) +# set(ARCH_DIR "x64") +# else() +# set(ARCH_DIR "x86") +# endif() + +# 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 (IS_DEBUG) +# set(WinDevice_LIB_DIR "${WinDevice_OUTPUT}/${ARCH_DIR}/debug/") +# else() +# set(WinDevice_LIB_DIR "${WinDevice_OUTPUT}/${ARCH_DIR}/release/") +# endif() +# message("WinDevice_LIB_DIR: ${WinDevice_LIB_DIR}") + +# message("build WinDecie in: ${CMAKE_BUILD_TYPE} mode") -set(WinDevice_OUTPUT "../WinDevice/output") -message("WinDevice_OUTPUT: ${WinDevice_OUTPUT}") -# 获取架构信息 - -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 (IS_DEBUG) - set(WinDevice_LIB_DIR "${WinDevice_OUTPUT}/${ARCH_DIR}/debug/") -else() - set(WinDevice_LIB_DIR "${WinDevice_OUTPUT}/${ARCH_DIR}/release/") -endif() -message("WinDevice_LIB_DIR: ${WinDevice_LIB_DIR}") - -message("build WinDecie in: ${CMAKE_BUILD_TYPE} mode") - - -add_custom_target(BuildWinDevice ALL - COMMAND ${CMAKE_COMMAND} -E echo "Starting to build WinDevice project..." - COMMAND ${CMAKE_COMMAND} -S ../WinDevice -B ../WinDevice/build -G "Visual Studio 17 2022" -A ${ARCH_DIR} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} - COMMAND ${CMAKE_COMMAND} --build ../WinDevice/build --config ${CMAKE_BUILD_TYPE} - COMMENT "Building WinDevice project in ${CMAKE_BUILD_TYPE} mode" -) +# add_custom_target(BuildWinDevice ALL +# COMMAND ${CMAKE_COMMAND} -E echo "Starting to build WinDevice project..." +# COMMAND ${CMAKE_COMMAND} -S ../WinDevice -B ../WinDevice/build -G "Visual Studio 17 2022" -A ${ARCH_DIR} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} +# COMMAND ${CMAKE_COMMAND} --build ../WinDevice/build --config ${CMAKE_BUILD_TYPE} +# COMMENT "Building WinDevice project in ${CMAKE_BUILD_TYPE} mode" +# ) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) diff --git a/DeviceManager/main.cpp b/DeviceManager/main.cpp index d0b489b..9d489ea 100644 --- a/DeviceManager/main.cpp +++ b/DeviceManager/main.cpp @@ -12,7 +12,7 @@ int main(int argc, char *argv[]) qInstallMessageHandler(LogManager::CustomMessageHandler); WinDevice::WinDeviceManager::Init(); - ScreenManager screenManager; + WinDevice::ScreenManager screenManager; screenManager.UpdateDisplayInfo(); qDebug("Adapter Count: %d", screenManager._displayDeviceList.size()); diff --git a/WinDevice/src/Audio/AudioEnum.h b/WinDevice/src/Audio/AudioEnum.h index 7f85ffe..5014550 100644 --- a/WinDevice/src/Audio/AudioEnum.h +++ b/WinDevice/src/Audio/AudioEnum.h @@ -1,7 +1,9 @@ #pragma once -enum AudioDeviceType -{ - Render, - Capture -}; +namespace WinDevice { + enum AudioDeviceType + { + Render, + Capture + }; +} diff --git a/WinDevice/src/Audio/AudioManager.cpp b/WinDevice/src/Audio/AudioManager.cpp index c5c801f..75f162a 100644 --- a/WinDevice/src/Audio/AudioManager.cpp +++ b/WinDevice/src/Audio/AudioManager.cpp @@ -2,16 +2,16 @@ #include -AudioManager::AudioManager() +WinDevice::AudioManager::AudioManager() { // 创建设备枚举器 } -AudioManager::~AudioManager() +WinDevice::AudioManager::~AudioManager() { } -HRESULT AudioManager::Init() +HRESULT WinDevice::AudioManager::Init() { HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), reinterpret_cast(&pEnumerator)); if (FAILED(hr)) @@ -31,7 +31,7 @@ HRESULT AudioManager::Init() return hr; } -HRESULT AudioManager::Uninit() +HRESULT WinDevice::AudioManager::Uninit() { pEnumerator = nullptr; pCaptureCollection = nullptr; @@ -41,7 +41,7 @@ HRESULT AudioManager::Uninit() return S_OK; } -IMMDeviceCollection* AudioManager::GetDeviceList(EDataFlow flow) +IMMDeviceCollection* WinDevice::AudioManager::GetDeviceList(EDataFlow flow) { if (flow == eCapture) { @@ -66,7 +66,7 @@ IMMDeviceCollection* AudioManager::GetDeviceList(EDataFlow flow) return nullptr; } -IMMDevice* AudioManager::GetDefaultDevice(EDataFlow flow) +IMMDevice* WinDevice::AudioManager::GetDefaultDevice(EDataFlow flow) { if (flow == eCapture) { @@ -79,7 +79,7 @@ IMMDevice* AudioManager::GetDefaultDevice(EDataFlow flow) return nullptr; } -HRESULT AudioManager::_UpdateDeviceList(EDataFlow flow) +HRESULT WinDevice::AudioManager::_UpdateDeviceList(EDataFlow flow) { HRESULT hr = S_OK; if (flow == eCapture) @@ -93,7 +93,7 @@ HRESULT AudioManager::_UpdateDeviceList(EDataFlow flow) return hr; } -HRESULT AudioManager::_UpdateDefaultDevice(EDataFlow flow) +HRESULT WinDevice::AudioManager::_UpdateDefaultDevice(EDataFlow flow) { HRESULT hr = S_OK; if (flow == eCapture) diff --git a/WinDevice/src/Audio/AudioManager.h b/WinDevice/src/Audio/AudioManager.h index 5588ad5..f27516c 100644 --- a/WinDevice/src/Audio/AudioManager.h +++ b/WinDevice/src/Audio/AudioManager.h @@ -2,25 +2,27 @@ #include -class AudioManager -{ -public: - AudioManager(); - ~AudioManager(); +namespace WinDevice { + class AudioManager + { + public: + AudioManager(); + ~AudioManager(); - HRESULT Init(); - HRESULT Uninit(); + HRESULT Init(); + HRESULT Uninit(); - IMMDeviceCollection* GetDeviceList(EDataFlow flow); - IMMDevice* GetDefaultDevice(EDataFlow flow); + IMMDeviceCollection* GetDeviceList(EDataFlow flow); + IMMDevice* GetDefaultDevice(EDataFlow flow); -private: - IMMDeviceEnumerator* pEnumerator = NULL; - IMMDeviceCollection* pRenderCollection = NULL; - IMMDeviceCollection* pCaptureCollection = NULL; - IMMDevice* pDefaultRenderEndpoint = NULL; - IMMDevice* pDefaultCaptureEndpoint = NULL; + private: + IMMDeviceEnumerator* pEnumerator = NULL; + IMMDeviceCollection* pRenderCollection = NULL; + IMMDeviceCollection* pCaptureCollection = NULL; + IMMDevice* pDefaultRenderEndpoint = NULL; + IMMDevice* pDefaultCaptureEndpoint = NULL; - HRESULT _UpdateDeviceList(EDataFlow flow); - HRESULT _UpdateDefaultDevice(EDataFlow flow); -}; + HRESULT _UpdateDeviceList(EDataFlow flow); + HRESULT _UpdateDefaultDevice(EDataFlow flow); + }; +} diff --git a/WinDevice/src/Utils/CmdUtil.cpp b/WinDevice/src/Utils/CmdUtil.cpp index 88f59dd..f197702 100644 --- a/WinDevice/src/Utils/CmdUtil.cpp +++ b/WinDevice/src/Utils/CmdUtil.cpp @@ -7,7 +7,7 @@ using namespace std; -std::string CmdUtil::ExecuteCommand(const std::string& command) +std::string WinDevice::CmdUtil::ExecuteCommand(const std::string& command) { spdlog::info("ExecuteCommand command:{0}", command); auto cmd_result = std::string(); diff --git a/WinDevice/src/Utils/CmdUtil.h b/WinDevice/src/Utils/CmdUtil.h index 1c6f4e8..e7a9f2c 100644 --- a/WinDevice/src/Utils/CmdUtil.h +++ b/WinDevice/src/Utils/CmdUtil.h @@ -1,8 +1,11 @@ #pragma once #include -class CmdUtil -{ -public: - static std::string ExecuteCommand(const std::string& command); -}; \ No newline at end of file + +namespace WinDevice { + class CmdUtil + { + public: + static std::string ExecuteCommand(const std::string& command); + }; +} \ No newline at end of file diff --git a/WinDevice/src/Utils/Log.cpp b/WinDevice/src/Utils/Log.cpp index cf30f5c..83be0a2 100644 --- a/WinDevice/src/Utils/Log.cpp +++ b/WinDevice/src/Utils/Log.cpp @@ -3,18 +3,18 @@ #include "spdlog/sinks/stdout_sinks.h" -void Log::Init(LogLevel level, std::string fileName) +void WinDevice::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"); } -void Log::Info(std::string format, std::string args...) +void WinDevice::Log::Info(std::string format, std::string args...) { } -void Log::WInfo(std::wstring format, std::wstring args...) +void WinDevice::Log::WInfo(std::wstring format, std::wstring args...) { } diff --git a/WinDevice/src/Utils/Log.h b/WinDevice/src/Utils/Log.h index 9b63fc8..9d708a0 100644 --- a/WinDevice/src/Utils/Log.h +++ b/WinDevice/src/Utils/Log.h @@ -4,17 +4,20 @@ #define LOG_FUNC_START() spdlog::info("=====> {0} start <=====", __FUNCTION__) #define LOG_FUNC_END() spdlog::info("=====> {0} end <=====", __FUNCTION__) -enum LogLevel : int -{ - Debug = spdlog::level::debug, - Info = spdlog::level::info, -}; +namespace WinDevice { + enum LogLevel : int + { + Debug = spdlog::level::debug, + Info = spdlog::level::info, + }; -class Log -{ -public: - static void Init(LogLevel level, std::string fileName); + class Log + { + public: + static void Init(LogLevel level, std::string fileName); + + static void Info(std::string format, std::string args...); + static void WInfo(std::wstring format, std::wstring args...); + }; +} - static void Info(std::string format, std::string args...); - static void WInfo(std::wstring format, std::wstring args...); -}; diff --git a/WinDevice/src/Utils/StringUtil.h b/WinDevice/src/Utils/StringUtil.h index 481f3c5..5d59224 100644 --- a/WinDevice/src/Utils/StringUtil.h +++ b/WinDevice/src/Utils/StringUtil.h @@ -2,24 +2,21 @@ #include #include -namespace WinDevcie { +namespace WinDevice { -namespace Utils { + template + To Convert(const From& input) { + std::wstring_convert, typename From::value_type> converter; + return converter.to_bytes(input); + } - 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 Wstring2String(const std::wstring& input) { - return Convert(input); - } - - std::string Wchar2String(const WCHAR* input) { - std::wstring_convert> converter; - return converter.to_bytes(input); - } + std::string Wchar2String(const WCHAR* input) { + std::wstring_convert> converter; + return converter.to_bytes(input); } } diff --git a/WinDevice/src/Utils/TimeUtil.cpp b/WinDevice/src/Utils/TimeUtil.cpp index e4bfd5f..0c077b6 100644 --- a/WinDevice/src/Utils/TimeUtil.cpp +++ b/WinDevice/src/Utils/TimeUtil.cpp @@ -1,9 +1,8 @@ #include "TimeUtil.h" #include "spdlog/spdlog.h" - template -void TimeUtil::CalExecuteTime(Func func) +void WinDevice::TimeUtil::CalExecuteTime(Func func) { // 测量函数调用的耗时 const auto start = std::chrono::high_resolution_clock::now(); @@ -11,4 +10,4 @@ void TimeUtil::CalExecuteTime(Func func) const auto end = std::chrono::high_resolution_clock::now(); const auto duration = std::chrono::duration_cast(end - start); spdlog::info("Function call duration: {0} ms", duration.count()); -} +} \ No newline at end of file diff --git a/WinDevice/src/Utils/TimeUtil.h b/WinDevice/src/Utils/TimeUtil.h index 92f94d5..121e833 100644 --- a/WinDevice/src/Utils/TimeUtil.h +++ b/WinDevice/src/Utils/TimeUtil.h @@ -1,8 +1,11 @@ #pragma once -template -class TimeUtil -{ -public: - static void CalExecuteTime(Func func); -}; +namespace WinDevice { + template + class TimeUtil + { + public: + static void CalExecuteTime(Func func); + }; + +} diff --git a/WinDevice/src/Video/ScreenManager.cpp b/WinDevice/src/Video/ScreenManager.cpp index 9ff2df5..b43ea4e 100644 --- a/WinDevice/src/Video/ScreenManager.cpp +++ b/WinDevice/src/Video/ScreenManager.cpp @@ -4,24 +4,24 @@ #include "../Utils/Log.h" #include -ScreenManager::ScreenManager() +WinDevice::ScreenManager::ScreenManager() { } -ScreenManager::~ScreenManager() +WinDevice::ScreenManager::~ScreenManager() { } -void ScreenManager::UpdateDisplayInfo() +void WinDevice::ScreenManager::UpdateDisplayInfo() { _UpdateDisplayDeviceList(); _UpdateMonitorInfoMap(); _UpdateDisplayAdapterList(); } -void ScreenManager::_UpdateDisplayDeviceList() +void WinDevice::ScreenManager::_UpdateDisplayDeviceList() { spdlog::info("=====GetInfoByEnumDisplayDevices start====="); DISPLAY_DEVICE displayDevice; @@ -39,7 +39,7 @@ void ScreenManager::_UpdateDisplayDeviceList() spdlog::info("=====GetInfoByEnumDisplayDevices end====="); } -void ScreenManager::_UpdateDisplayAdapterList() +void WinDevice::ScreenManager::_UpdateDisplayAdapterList() { LOG_FUNC_START(); HRESULT hr = S_OK; @@ -56,8 +56,8 @@ 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, WinDevcie::Utils::Wchar2String(adapterDesc.Description), adapterDesc.DeviceId, adapterDesc.VendorId, adapterDesc.SubSysId, - adapterDesc.Revision, adapterDesc.AdapterLuid.HighPart, adapterDesc.AdapterLuid.LowPart); + adapterIndex, WinDevice::Wchar2String(adapterDesc.Description), adapterDesc.DeviceId, adapterDesc.VendorId, adapterDesc.SubSysId, + adapterDesc.Revision, adapterDesc.AdapterLuid.HighPart, adapterDesc.AdapterLuid.LowPart); // print adapter output info IDXGIOutput* pOutput; @@ -72,7 +72,7 @@ void ScreenManager::_UpdateDisplayAdapterList() { // 输出友好名称 spdlog::info("Adapter Output Index:{0}, DeviceName:{1}, szDevice:{2}, right:{3}, bottom:{4}", - j, WinDevcie::Utils::Wchar2String(outputDesc.DeviceName), monitorInfo.szDevice, monitorInfo.rcMonitor.right, monitorInfo.rcMonitor.bottom); + j, WinDevice::Wchar2String(outputDesc.DeviceName), monitorInfo.szDevice, monitorInfo.rcMonitor.right, monitorInfo.rcMonitor.bottom); } } } @@ -80,7 +80,7 @@ void ScreenManager::_UpdateDisplayAdapterList() LOG_FUNC_END(); } -BOOL ScreenManager::_EnumMonitorProc(HMONITOR hMonitor) +BOOL WinDevice::ScreenManager::_EnumMonitorProc(HMONITOR hMonitor) { MONITORINFOEX monitorInfo; monitorInfo.cbSize = sizeof(MONITORINFOEX); @@ -110,7 +110,7 @@ BOOL ScreenManager::_EnumMonitorProc(HMONITOR hMonitor) if (DisplayConfigGetDeviceInfo(&targetDeviceName.header) == ERROR_SUCCESS) { spdlog::info("_UpdateMonitorInfoMap, monitorDevicePath:{0}, monitorFriendlyDeviceName:{1}", - WinDevcie::Utils::Wchar2String(targetDeviceName.monitorDevicePath), WinDevcie::Utils::Wchar2String(targetDeviceName.monitorFriendlyDeviceName)); + WinDevice::Wchar2String(targetDeviceName.monitorDevicePath), WinDevice::Wchar2String(targetDeviceName.monitorFriendlyDeviceName)); // 获取指定 HMONITOR 的分辨率信息 DEVMODE dm; @@ -133,12 +133,12 @@ BOOL ScreenManager::_EnumMonitorProc(HMONITOR hMonitor) return TRUE; } -BOOL CALLBACK ScreenManager::EnumMonitorsProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) +BOOL CALLBACK WinDevice::ScreenManager::EnumMonitorsProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { return ((ScreenManager*)dwData)->_EnumMonitorProc(hMonitor); } -void ScreenManager::_UpdateMonitorInfoMap() +void WinDevice::ScreenManager::_UpdateMonitorInfoMap() { spdlog::info("=====GetInfoByEnumDisplayMonitors start====="); // 枚举显示器 diff --git a/WinDevice/src/Video/ScreenManager.h b/WinDevice/src/Video/ScreenManager.h index b65333b..12acdd0 100644 --- a/WinDevice/src/Video/ScreenManager.h +++ b/WinDevice/src/Video/ScreenManager.h @@ -3,21 +3,23 @@ #include #include -class ScreenManager -{ -public: - ScreenManager(); - ~ScreenManager(); - void UpdateDisplayInfo(); +namespace WinDevice { + class ScreenManager + { + public: + ScreenManager(); + ~ScreenManager(); + void UpdateDisplayInfo(); - std::vector _displayDeviceList; - std::map _hMonitorInfoMap; - std::vector _displayAdapterList; + std::vector _displayDeviceList; + std::map _hMonitorInfoMap; + std::vector _displayAdapterList; -private: - void _UpdateDisplayDeviceList(); - void _UpdateMonitorInfoMap(); - void _UpdateDisplayAdapterList(); - static BOOL CALLBACK EnumMonitorsProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData); - BOOL _EnumMonitorProc(HMONITOR hMonitor); -}; + private: + void _UpdateDisplayDeviceList(); + void _UpdateMonitorInfoMap(); + void _UpdateDisplayAdapterList(); + static BOOL CALLBACK EnumMonitorsProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData); + BOOL _EnumMonitorProc(HMONITOR hMonitor); + }; +} diff --git a/WinDevice/src/main.cpp b/WinDevice/src/main.cpp index d5076bc..4ad13e0 100644 --- a/WinDevice/src/main.cpp +++ b/WinDevice/src/main.cpp @@ -4,9 +4,9 @@ #include #include "Video/ScreenManager.h" int main() { - ScreenManager screenManager; + WinDevice::ScreenManager screenManager; screenManager.UpdateDisplayInfo(); std::cout << "Press Enter to exit..." << std::endl; // 输出提示信息 std::cin.get(); // 等待用户输入 return 0; -} \ No newline at end of file +}