[feat] optimize Windows display device names

This commit is contained in:
dijunkun
2025-11-05 20:36:04 +08:00
parent b3132db785
commit b78c9cf7d1
4 changed files with 40 additions and 18 deletions

View File

@@ -76,22 +76,34 @@ std::filesystem::path PathManager::GetKnownFolder(REFKNOWNFOLDERID id) {
#endif #endif
std::string PathManager::GetHome() { std::string PathManager::GetHome() {
if (const char* home = getenv("HOME")) {
return std::string(home);
}
#ifdef _WIN32 #ifdef _WIN32
char path[MAX_PATH]; char path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, 0, path))) if (SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_PROFILE, NULL, 0, path)))
return std::string(path); return std::string(path);
#else
if (const char* home = getenv("HOME")) {
return std::string(home);
}
#endif #endif
return {}; return {};
} }
std::filesystem::path PathManager::GetEnvOrDefault(const char* env_var, std::filesystem::path PathManager::GetEnvOrDefault(const char* env_var,
const std::string& def) { const std::string& def) {
#ifdef _WIN32
char* buffer = nullptr;
size_t size = 0;
if (_dupenv_s(&buffer, &size, env_var) == 0 && buffer != nullptr) {
std::filesystem::path result(buffer);
free(buffer);
return result;
}
#else
if (const char* val = getenv(env_var)) { if (const char* val = getenv(env_var)) {
return std::filesystem::path(val); return std::filesystem::path(val);
} }
#endif
return std::filesystem::path(def); return std::filesystem::path(def);
} }

View File

@@ -14,33 +14,42 @@ namespace crossdesk {
static std::vector<DisplayInfo> gs_display_list; static std::vector<DisplayInfo> gs_display_list;
std::string WideToUtf8(const wchar_t* wideStr) { std::string WideToUtf8(const std::wstring& wstr) {
int size_needed = WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, nullptr, 0, if (wstr.empty()) return {};
nullptr, nullptr); int size_needed = WideCharToMultiByte(
CP_UTF8, 0, wstr.data(), (int)wstr.size(), nullptr, 0, nullptr, nullptr);
std::string result(size_needed, 0); std::string result(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, &result[0], size_needed, nullptr, WideCharToMultiByte(CP_UTF8, 0, wstr.data(), (int)wstr.size(), result.data(),
nullptr); size_needed, nullptr, nullptr);
result.pop_back();
return result; return result;
} }
std::string CleanDisplayName(const std::wstring& wide_name) {
std::string name = WideToUtf8(wide_name);
name.erase(std::remove_if(name.begin(), name.end(),
[](unsigned char c) { return !std::isalnum(c); }),
name.end());
return name;
}
BOOL WINAPI EnumMonitorProc(HMONITOR hmonitor, [[maybe_unused]] HDC hdc, BOOL WINAPI EnumMonitorProc(HMONITOR hmonitor, [[maybe_unused]] HDC hdc,
[[maybe_unused]] LPRECT lprc, LPARAM data) { [[maybe_unused]] LPRECT lprc, LPARAM data) {
MONITORINFOEX monitor_info_; MONITORINFOEX monitor_info_;
monitor_info_.cbSize = sizeof(MONITORINFOEX); monitor_info_.cbSize = sizeof(MONITORINFOEX);
if (GetMonitorInfo(hmonitor, &monitor_info_)) { if (GetMonitorInfo(hmonitor, &monitor_info_)) {
std::string display_name = CleanDisplayName(monitor_info_.szDevice);
if (monitor_info_.dwFlags & MONITORINFOF_PRIMARY) { if (monitor_info_.dwFlags & MONITORINFOF_PRIMARY) {
gs_display_list.insert( gs_display_list.insert(
gs_display_list.begin(), gs_display_list.begin(),
{(void*)hmonitor, WideToUtf8(monitor_info_.szDevice), {(void*)hmonitor, display_name,
(monitor_info_.dwFlags & MONITORINFOF_PRIMARY) ? true : false, (monitor_info_.dwFlags & MONITORINFOF_PRIMARY) ? true : false,
monitor_info_.rcMonitor.left, monitor_info_.rcMonitor.top, monitor_info_.rcMonitor.left, monitor_info_.rcMonitor.top,
monitor_info_.rcMonitor.right, monitor_info_.rcMonitor.bottom}); monitor_info_.rcMonitor.right, monitor_info_.rcMonitor.bottom});
*(HMONITOR*)data = hmonitor; *(HMONITOR*)data = hmonitor;
} else { } else {
gs_display_list.push_back(DisplayInfo( gs_display_list.push_back(DisplayInfo(
(void*)hmonitor, WideToUtf8(monitor_info_.szDevice), (void*)hmonitor, display_name,
(monitor_info_.dwFlags & MONITORINFOF_PRIMARY) ? true : false, (monitor_info_.dwFlags & MONITORINFOF_PRIMARY) ? true : false,
monitor_info_.rcMonitor.left, monitor_info_.rcMonitor.top, monitor_info_.rcMonitor.left, monitor_info_.rcMonitor.top,
monitor_info_.rcMonitor.right, monitor_info_.rcMonitor.bottom)); monitor_info_.rcMonitor.right, monitor_info_.rcMonitor.bottom));

View File

@@ -118,13 +118,13 @@ void ScaleNv12ToABGR(char* src, int src_w, int src_h, int dst_w, int dst_h,
memset(dst_rgba, 0, dst_w * dst_h * 4); memset(dst_rgba, 0, dst_w * dst_h * 4);
for (int i = 0; i < dst_w * dst_h; ++i) { for (int i = 0; i < dst_w * dst_h; ++i) {
dst_rgba[i * 4 + 3] = 0xFF; dst_rgba[i * 4 + 3] = static_cast<char>(0xFF);
} }
for (int y = 0; y < fit_h; ++y) { for (int row = 0; row < fit_h; ++row) {
int dst_offset = int dst_offset =
((y + (dst_h - fit_h) / 2) * dst_w + (dst_w - fit_w) / 2) * 4; ((row + (dst_h - fit_h) / 2) * dst_w + (dst_w - fit_w) / 2) * 4;
memcpy(dst_rgba + dst_offset, abgr.data() + y * fit_w * 4, fit_w * 4); memcpy(dst_rgba + dst_offset, abgr.data() + row * fit_w * 4, fit_w * 4);
} }
} }
@@ -172,7 +172,7 @@ int Thumbnail::SaveToThumbnail(const char* yuv420p, int width, int height,
memset(rgba_buffer_, 0x00, thumbnail_width_ * thumbnail_height_ * 4); memset(rgba_buffer_, 0x00, thumbnail_width_ * thumbnail_height_ * 4);
for (int i = 0; i < thumbnail_width_ * thumbnail_height_; ++i) { for (int i = 0; i < thumbnail_width_ * thumbnail_height_; ++i) {
// Set alpha channel to opaque // Set alpha channel to opaque
rgba_buffer_[i * 4 + 3] = 0xFF; rgba_buffer_[i * 4 + 3] = static_cast<char>(0xFF);
} }
} }
@@ -214,7 +214,7 @@ int Thumbnail::LoadThumbnail(
return -1; return -1;
} else { } else {
for (int i = 0; i < image_paths.size(); i++) { for (int i = 0; i < image_paths.size(); i++) {
size_t pos1 = image_paths[i].string().find('/') + 1; // size_t pos1 = image_paths[i].string().find('/') + 1;
std::string cipher_image_name = image_paths[i].filename().string(); std::string cipher_image_name = image_paths[i].filename().string();
std::string remote_id; std::string remote_id;
std::string cipher_password; std::string cipher_password;
@@ -241,7 +241,7 @@ int Thumbnail::LoadThumbnail(
AES_decrypt(cipher_password, aes128_key_, aes128_iv_); AES_decrypt(cipher_password, aes128_key_, aes128_iv_);
} else { } else {
size_t pos_n = cipher_image_name.find('N'); size_t pos_n = cipher_image_name.find('N');
size_t pos_at = cipher_image_name.find('@'); // size_t pos_at = cipher_image_name.find('@');
if (pos_n == std::string::npos) { if (pos_n == std::string::npos) {
LOG_ERROR("Invalid filename"); LOG_ERROR("Invalid filename");

View File

@@ -13,6 +13,7 @@ set_encodings("utf-8")
-- set_policy("build.warning", true) -- set_policy("build.warning", true)
-- set_warnings("all", "extra") -- set_warnings("all", "extra")
-- add_cxxflags("/W4", "/WX")
add_defines("UNICODE") add_defines("UNICODE")
if is_mode("debug") then if is_mode("debug") then