From c8af502bd2a50398bf1f4da06b90457150340316 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Thu, 9 Sep 2021 11:05:53 +0200 Subject: [PATCH] Inline the Windows monitor name code It's just used in this one place, and isn't very large. Also move things around a bit to avoid having to define new complex types. --- vncviewer/MonitorArrangement.cxx | 36 +++++++++++++++++++++- vncviewer/win32.c | 51 -------------------------------- vncviewer/win32.h | 2 -- 3 files changed, 35 insertions(+), 54 deletions(-) diff --git a/vncviewer/MonitorArrangement.cxx b/vncviewer/MonitorArrangement.cxx index 735177ba..60637b95 100644 --- a/vncviewer/MonitorArrangement.cxx +++ b/vncviewer/MonitorArrangement.cxx @@ -347,13 +347,47 @@ std::string MonitorArrangement::description(int m) return ss.str(); } +#if defined(WIN32) +static BOOL CALLBACK EnumDisplayMonitorsCallback( + HMONITOR monitor, HDC deviceContext, LPRECT rect, LPARAM userData) +{ + std::set* sys_monitors = (std::set*)userData; + sys_monitors->insert(monitor); + return TRUE; +} +#endif + int MonitorArrangement::get_monitor_name(int m, char name[], size_t name_len) { #if defined(WIN32) + std::set sys_monitors; + std::set::const_iterator iter; int x, y, w, h; + Fl::screen_xywh(x, y, w, h, m); - return win32_get_monitor_name(x, y, w, h, name, name_len); + EnumDisplayMonitors(NULL, NULL, EnumDisplayMonitorsCallback, + (LPARAM)&sys_monitors); + + for (iter = sys_monitors.begin(); iter != sys_monitors.end(); ++iter) { + MONITORINFOEX info; + + info.cbSize = sizeof(info); + GetMonitorInfo(*iter, (LPMONITORINFO)&info); + + if (info.rcMonitor.left != x) + continue; + if (info.rcMonitor.top != y) + continue; + if ((info.rcMonitor.right - info.rcMonitor.left) != w) + continue; + if ((info.rcMonitor.bottom - info.rcMonitor.top) != h) + continue; + + return snprintf(name, name_len, "%.*s", (int)(name_len - 1), info.szDevice); + } + + return -1; #elif defined(__APPLE__) CGDisplayCount count; int bytes_written = 0; diff --git a/vncviewer/win32.c b/vncviewer/win32.c index 9032f36a..80305bc4 100644 --- a/vncviewer/win32.c +++ b/vncviewer/win32.c @@ -419,54 +419,3 @@ int win32_has_altgr(void) return has_altgr; } - -typedef struct { - int x, y, w, h; - char* name; - size_t name_len; - int bytes_written; -} EnumCallbackData; - -static BOOL CALLBACK EnumDisplayMonitorsCallback( - HMONITOR monitor, HDC deviceContext, LPRECT rect, LPARAM userData) -{ - EnumCallbackData *data = (EnumCallbackData *)userData; - MONITORINFOEX info; - info.cbSize = sizeof(info); - GetMonitorInfo(monitor, (LPMONITORINFO)&info); - - int x = info.rcMonitor.left; - int y = info.rcMonitor.top; - int w = info.rcMonitor.right - info.rcMonitor.left; - int h = info.rcMonitor.bottom - info.rcMonitor.top; - - if ((data->x == x) && (data->y == y) && (data->w == w) && (data->h == h)) { - data->bytes_written = snprintf(data->name, data->name_len, - "%.*s", (int)(data->name_len - 1), info.szDevice); - - if (data->bytes_written < 0) - return FALSE; - - // Stop the iteration. - return FALSE; - } - - // Keep iterating. - return TRUE; -} - -int win32_get_monitor_name(int x, int y, int w, int h, char name[], size_t name_len) -{ - EnumCallbackData data = { - .x = x, - .y = y, - .w = w, - .h = h, - .name = name, - .name_len = name_len, - .bytes_written = -1 - }; - - EnumDisplayMonitors(NULL, NULL, EnumDisplayMonitorsCallback, (LPARAM) &data); - return data.bytes_written; -} diff --git a/vncviewer/win32.h b/vncviewer/win32.h index 2dae04b9..ebcfccb0 100644 --- a/vncviewer/win32.h +++ b/vncviewer/win32.h @@ -33,8 +33,6 @@ void win32_disable_lowlevel_keyboard(HWND hwnd); int win32_vkey_to_keysym(UINT vkey, int extended); int win32_has_altgr(void); - -int win32_get_monitor_name(int x, int y, int w, int h, char name[], size_t name_len); }; #endif -- 2.39.5