add namespace

This commit is contained in:
DevWiki 2024-09-29 10:18:35 +08:00
parent ccf642ef92
commit 5c8f902c11
15 changed files with 151 additions and 142 deletions

View File

@ -13,44 +13,42 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>") add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>")
if(CMAKE_SIZEOF_VOID_P EQUAL 8) # option(IS_DEBUG "Enable debug mode" OFF)
set(ARCH_DIR "x64") # if (CMAKE_BUILD_TYPE STREQUAL "Debug")
else() # set(IS_DEBUG ON)
set(ARCH_DIR "x86") # else()
endif() # set(IS_DEBUG OFF)
# endif()
option(IS_DEBUG "Enable debug mode" OFF) # set(WinDevice_OUTPUT "../WinDevice/output")
if (CMAKE_BUILD_TYPE STREQUAL "Debug") # message("WinDevice_OUTPUT: ${WinDevice_OUTPUT}")
set(IS_DEBUG ON) # #
else() # if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(IS_DEBUG OFF) # set(ARCH_DIR "x64")
endif() # 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") # add_custom_target(BuildWinDevice ALL
message("WinDevice_OUTPUT: ${WinDevice_OUTPUT}") # 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}
set(WinDevice_INCLUDE_DIR "${WinDevice_OUTPUT}/include/") # COMMENT "Building WinDevice project in ${CMAKE_BUILD_TYPE} mode"
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"
)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets) find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)

View File

@ -12,7 +12,7 @@ int main(int argc, char *argv[])
qInstallMessageHandler(LogManager::CustomMessageHandler); qInstallMessageHandler(LogManager::CustomMessageHandler);
WinDevice::WinDeviceManager::Init(); WinDevice::WinDeviceManager::Init();
ScreenManager screenManager; WinDevice::ScreenManager screenManager;
screenManager.UpdateDisplayInfo(); screenManager.UpdateDisplayInfo();
qDebug("Adapter Count: %d", screenManager._displayDeviceList.size()); qDebug("Adapter Count: %d", screenManager._displayDeviceList.size());

View File

@ -1,7 +1,9 @@
#pragma once #pragma once
enum AudioDeviceType namespace WinDevice {
{ enum AudioDeviceType
{
Render, Render,
Capture Capture
}; };
}

View File

@ -2,16 +2,16 @@
#include <iostream> #include <iostream>
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<void**>(&pEnumerator)); HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), reinterpret_cast<void**>(&pEnumerator));
if (FAILED(hr)) if (FAILED(hr))
@ -31,7 +31,7 @@ HRESULT AudioManager::Init()
return hr; return hr;
} }
HRESULT AudioManager::Uninit() HRESULT WinDevice::AudioManager::Uninit()
{ {
pEnumerator = nullptr; pEnumerator = nullptr;
pCaptureCollection = nullptr; pCaptureCollection = nullptr;
@ -41,7 +41,7 @@ HRESULT AudioManager::Uninit()
return S_OK; return S_OK;
} }
IMMDeviceCollection* AudioManager::GetDeviceList(EDataFlow flow) IMMDeviceCollection* WinDevice::AudioManager::GetDeviceList(EDataFlow flow)
{ {
if (flow == eCapture) if (flow == eCapture)
{ {
@ -66,7 +66,7 @@ IMMDeviceCollection* AudioManager::GetDeviceList(EDataFlow flow)
return nullptr; return nullptr;
} }
IMMDevice* AudioManager::GetDefaultDevice(EDataFlow flow) IMMDevice* WinDevice::AudioManager::GetDefaultDevice(EDataFlow flow)
{ {
if (flow == eCapture) if (flow == eCapture)
{ {
@ -79,7 +79,7 @@ IMMDevice* AudioManager::GetDefaultDevice(EDataFlow flow)
return nullptr; return nullptr;
} }
HRESULT AudioManager::_UpdateDeviceList(EDataFlow flow) HRESULT WinDevice::AudioManager::_UpdateDeviceList(EDataFlow flow)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
if (flow == eCapture) if (flow == eCapture)
@ -93,7 +93,7 @@ HRESULT AudioManager::_UpdateDeviceList(EDataFlow flow)
return hr; return hr;
} }
HRESULT AudioManager::_UpdateDefaultDevice(EDataFlow flow) HRESULT WinDevice::AudioManager::_UpdateDefaultDevice(EDataFlow flow)
{ {
HRESULT hr = S_OK; HRESULT hr = S_OK;
if (flow == eCapture) if (flow == eCapture)

View File

@ -2,9 +2,10 @@
#include <Mmdeviceapi.h> #include <Mmdeviceapi.h>
class AudioManager namespace WinDevice {
{ class AudioManager
public: {
public:
AudioManager(); AudioManager();
~AudioManager(); ~AudioManager();
@ -14,7 +15,7 @@ public:
IMMDeviceCollection* GetDeviceList(EDataFlow flow); IMMDeviceCollection* GetDeviceList(EDataFlow flow);
IMMDevice* GetDefaultDevice(EDataFlow flow); IMMDevice* GetDefaultDevice(EDataFlow flow);
private: private:
IMMDeviceEnumerator* pEnumerator = NULL; IMMDeviceEnumerator* pEnumerator = NULL;
IMMDeviceCollection* pRenderCollection = NULL; IMMDeviceCollection* pRenderCollection = NULL;
IMMDeviceCollection* pCaptureCollection = NULL; IMMDeviceCollection* pCaptureCollection = NULL;
@ -23,4 +24,5 @@ private:
HRESULT _UpdateDeviceList(EDataFlow flow); HRESULT _UpdateDeviceList(EDataFlow flow);
HRESULT _UpdateDefaultDevice(EDataFlow flow); HRESULT _UpdateDefaultDevice(EDataFlow flow);
}; };
}

View File

@ -7,7 +7,7 @@
using namespace std; 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); spdlog::info("ExecuteCommand command:{0}", command);
auto cmd_result = std::string(); auto cmd_result = std::string();

View File

@ -1,8 +1,11 @@
#pragma once #pragma once
#include <string> #include <string>
class CmdUtil
{ namespace WinDevice {
public: class CmdUtil
{
public:
static std::string ExecuteCommand(const std::string& command); static std::string ExecuteCommand(const std::string& command);
}; };
}

View File

@ -3,18 +3,18 @@
#include "spdlog/sinks/stdout_sinks.h" #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::stdout_logger_mt("console");
spdlog::set_level(static_cast<spdlog::level::level_enum>(level)); // 设置日志级别为 debug spdlog::set_level(static_cast<spdlog::level::level_enum>(level)); // 设置日志级别为 debug
spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v"); 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...)
{ {
} }

View File

@ -4,17 +4,20 @@
#define LOG_FUNC_START() spdlog::info("=====> {0} start <=====", __FUNCTION__) #define LOG_FUNC_START() spdlog::info("=====> {0} start <=====", __FUNCTION__)
#define LOG_FUNC_END() spdlog::info("=====> {0} end <=====", __FUNCTION__) #define LOG_FUNC_END() spdlog::info("=====> {0} end <=====", __FUNCTION__)
enum LogLevel : int namespace WinDevice {
{ enum LogLevel : int
{
Debug = spdlog::level::debug, Debug = spdlog::level::debug,
Info = spdlog::level::info, Info = spdlog::level::info,
}; };
class Log class Log
{ {
public: public:
static void Init(LogLevel level, std::string fileName); static void Init(LogLevel level, std::string fileName);
static void Info(std::string format, std::string args...); static void Info(std::string format, std::string args...);
static void WInfo(std::wstring format, std::wstring args...); static void WInfo(std::wstring format, std::wstring args...);
}; };
}

View File

@ -2,9 +2,7 @@
#include <locale> #include <locale>
#include <codecvt> #include <codecvt>
namespace WinDevcie { namespace WinDevice {
namespace Utils {
template<typename To, typename From> template<typename To, typename From>
To Convert(const From& input) { To Convert(const From& input) {
@ -20,6 +18,5 @@ namespace Utils {
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
return converter.to_bytes(input); return converter.to_bytes(input);
} }
}
} }

View File

@ -1,9 +1,8 @@
#include "TimeUtil.h" #include "TimeUtil.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
template <typename Func> template <typename Func>
void TimeUtil<Func>::CalExecuteTime(Func func) void WinDevice::TimeUtil<Func>::CalExecuteTime(Func func)
{ {
// 测量函数调用的耗时 // 测量函数调用的耗时
const auto start = std::chrono::high_resolution_clock::now(); const auto start = std::chrono::high_resolution_clock::now();

View File

@ -1,8 +1,11 @@
#pragma once #pragma once
template <typename Func>
class TimeUtil namespace WinDevice {
{ template <typename Func>
public: class TimeUtil
{
public:
static void CalExecuteTime(Func func); static void CalExecuteTime(Func func);
}; };
}

View File

@ -4,24 +4,24 @@
#include "../Utils/Log.h" #include "../Utils/Log.h"
#include <dxgi.h> #include <dxgi.h>
ScreenManager::ScreenManager() WinDevice::ScreenManager::ScreenManager()
{ {
} }
ScreenManager::~ScreenManager() WinDevice::ScreenManager::~ScreenManager()
{ {
} }
void ScreenManager::UpdateDisplayInfo() void WinDevice::ScreenManager::UpdateDisplayInfo()
{ {
_UpdateDisplayDeviceList(); _UpdateDisplayDeviceList();
_UpdateMonitorInfoMap(); _UpdateMonitorInfoMap();
_UpdateDisplayAdapterList(); _UpdateDisplayAdapterList();
} }
void ScreenManager::_UpdateDisplayDeviceList() void WinDevice::ScreenManager::_UpdateDisplayDeviceList()
{ {
spdlog::info("=====GetInfoByEnumDisplayDevices start====="); spdlog::info("=====GetInfoByEnumDisplayDevices start=====");
DISPLAY_DEVICE displayDevice; DISPLAY_DEVICE displayDevice;
@ -39,7 +39,7 @@ void ScreenManager::_UpdateDisplayDeviceList()
spdlog::info("=====GetInfoByEnumDisplayDevices end====="); spdlog::info("=====GetInfoByEnumDisplayDevices end=====");
} }
void ScreenManager::_UpdateDisplayAdapterList() void WinDevice::ScreenManager::_UpdateDisplayAdapterList()
{ {
LOG_FUNC_START(); LOG_FUNC_START();
HRESULT hr = S_OK; HRESULT hr = S_OK;
@ -56,7 +56,7 @@ void ScreenManager::_UpdateDisplayAdapterList()
DXGI_ADAPTER_DESC adapterDesc; DXGI_ADAPTER_DESC adapterDesc;
pAdapter->GetDesc(&adapterDesc); pAdapter->GetDesc(&adapterDesc);
spdlog::info("Adapter Index:{0}, Description:{1}, DeviceId:{2}, VendorId:{3}, SubSysId:{4}, Revision:{5}, AdapterLuid(H-L):{6}-{7} ", 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, adapterIndex, WinDevice::Wchar2String(adapterDesc.Description), adapterDesc.DeviceId, adapterDesc.VendorId, adapterDesc.SubSysId,
adapterDesc.Revision, adapterDesc.AdapterLuid.HighPart, adapterDesc.AdapterLuid.LowPart); adapterDesc.Revision, adapterDesc.AdapterLuid.HighPart, adapterDesc.AdapterLuid.LowPart);
// print adapter output info // 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}", 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(); LOG_FUNC_END();
} }
BOOL ScreenManager::_EnumMonitorProc(HMONITOR hMonitor) BOOL WinDevice::ScreenManager::_EnumMonitorProc(HMONITOR hMonitor)
{ {
MONITORINFOEX monitorInfo; MONITORINFOEX monitorInfo;
monitorInfo.cbSize = sizeof(MONITORINFOEX); monitorInfo.cbSize = sizeof(MONITORINFOEX);
@ -110,7 +110,7 @@ BOOL ScreenManager::_EnumMonitorProc(HMONITOR hMonitor)
if (DisplayConfigGetDeviceInfo(&targetDeviceName.header) == ERROR_SUCCESS) if (DisplayConfigGetDeviceInfo(&targetDeviceName.header) == ERROR_SUCCESS)
{ {
spdlog::info("_UpdateMonitorInfoMap, monitorDevicePath:{0}, monitorFriendlyDeviceName:{1}", 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 的分辨率信息 // 获取指定 HMONITOR 的分辨率信息
DEVMODE dm; DEVMODE dm;
@ -133,12 +133,12 @@ BOOL ScreenManager::_EnumMonitorProc(HMONITOR hMonitor)
return TRUE; 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); return ((ScreenManager*)dwData)->_EnumMonitorProc(hMonitor);
} }
void ScreenManager::_UpdateMonitorInfoMap() void WinDevice::ScreenManager::_UpdateMonitorInfoMap()
{ {
spdlog::info("=====GetInfoByEnumDisplayMonitors start====="); spdlog::info("=====GetInfoByEnumDisplayMonitors start=====");
// 枚举显示器 // 枚举显示器

View File

@ -3,9 +3,10 @@
#include <map> #include <map>
#include <dxgi.h> #include <dxgi.h>
class ScreenManager namespace WinDevice {
{ class ScreenManager
public: {
public:
ScreenManager(); ScreenManager();
~ScreenManager(); ~ScreenManager();
void UpdateDisplayInfo(); void UpdateDisplayInfo();
@ -14,10 +15,11 @@ public:
std::map<HMONITOR, MONITORINFOEX> _hMonitorInfoMap; std::map<HMONITOR, MONITORINFOEX> _hMonitorInfoMap;
std::vector<IDXGIAdapter*> _displayAdapterList; std::vector<IDXGIAdapter*> _displayAdapterList;
private: private:
void _UpdateDisplayDeviceList(); void _UpdateDisplayDeviceList();
void _UpdateMonitorInfoMap(); void _UpdateMonitorInfoMap();
void _UpdateDisplayAdapterList(); void _UpdateDisplayAdapterList();
static BOOL CALLBACK EnumMonitorsProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData); static BOOL CALLBACK EnumMonitorsProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData);
BOOL _EnumMonitorProc(HMONITOR hMonitor); BOOL _EnumMonitorProc(HMONITOR hMonitor);
}; };
}

View File

@ -4,7 +4,7 @@
#include <iostream> #include <iostream>
#include "Video/ScreenManager.h" #include "Video/ScreenManager.h"
int main() { int main() {
ScreenManager screenManager; WinDevice::ScreenManager screenManager;
screenManager.UpdateDisplayInfo(); screenManager.UpdateDisplayInfo();
std::cout << "Press Enter to exit..." << std::endl; // 输出提示信息 std::cout << "Press Enter to exit..." << std::endl; // 输出提示信息
std::cin.get(); // 等待用户输入 std::cin.get(); // 等待用户输入