From b828e763add922693b863480408df4c50c265562 Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Wed, 12 Oct 2022 16:58:59 -0400 Subject: [PATCH] Some refactoring to help code reuse - Move MsgToEventLog from main.c to misc.c - Move dpi_initialize from main.c to misc.c Signed-off-by: Selva Nair --- config_parser.c | 1 + main.c | 58 +------------------------------------------------ main.h | 3 --- misc.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ misc.h | 17 +++++++++++++++ 5 files changed, 76 insertions(+), 60 deletions(-) diff --git a/config_parser.c b/config_parser.c index 8d4b274..377dbf9 100644 --- a/config_parser.c +++ b/config_parser.c @@ -25,6 +25,7 @@ #include #include #include "main.h" +#include "misc.h" #include "config_parser.h" static int diff --git a/main.c b/main.c index d7b3049..273819d 100644 --- a/main.c +++ b/main.c @@ -410,34 +410,6 @@ ResumeConnections() } } -/* - * Get dpi of the system and set the scale factor. - * The system dpi may be different from the per monitor dpi on - * Win 8.1 later. We set dpi awareness to system-dpi level in the - * manifest, and let Windows automatically re-scale windows - * if/when dpi changes dynamically. - */ -static void -dpi_initialize(void) -{ - UINT dpix = 0; - HDC hdc = GetDC(NULL); - - if (hdc) - { - dpix = GetDeviceCaps(hdc, LOGPIXELSX); - ReleaseDC(NULL, hdc); - PrintDebug(L"System DPI: dpix = %u", dpix); - } - else - { - PrintDebug(L"GetDC failed, using default dpi = 96 (error = %lu)", GetLastError()); - dpix = 96; - } - - DpiSetScale(&o, dpix); -} - static int HandleCopyDataMessage(const COPYDATASTRUCT *copy_data) { @@ -584,7 +556,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM /* Save Window Handle */ o.hWnd = hwnd; - dpi_initialize(); + dpi_initialize(&o); s_uTaskbarRestart = RegisterWindowMessage(TEXT("TaskbarCreated")); @@ -947,34 +919,6 @@ DWORD GetDllVersion(LPCTSTR lpszDllName) return dwVersion; } -void -MsgToEventLog(WORD type, wchar_t *format, ...) -{ - const wchar_t *msg[2]; - wchar_t buf[256]; - int size = _countof(buf); - - if (!o.event_log) - { - o.event_log = RegisterEventSource(NULL, TEXT(PACKAGE_NAME)); - if (!o.event_log) - return; - } - - va_list args; - va_start(args, format); - int nchar = vswprintf(buf, size-1, format, args); - va_end(args); - - if (nchar == -1) return; - - buf[size - 1] = '\0'; - - msg[0] = TEXT(PACKAGE_NAME); - msg[1] = buf; - ReportEventW(o.event_log, type, 0, 0, NULL, 2, 0, msg, NULL); -} - void ErrorExit(int exit_code, const wchar_t *msg) { diff --git a/main.h b/main.h index ea9970c..eda9bea 100644 --- a/main.h +++ b/main.h @@ -140,9 +140,6 @@ void PrintDebugMsg(TCHAR *msg); DWORD GetDllVersion(LPCTSTR lpszDllName); -#define DPI_SCALE(x) MulDiv(x, o.dpi_scale, 100) -void MsgToEventLog(WORD type, wchar_t *format, ...); - void ErrorExit(int exit_code, const wchar_t *msg); #endif diff --git a/misc.c b/misc.c index 0d4d48f..29f6556 100644 --- a/misc.c +++ b/misc.c @@ -912,3 +912,60 @@ ParseManagementAddress(connection_t *c) return ret; } + +/* Write a message to the event log */ +void +MsgToEventLog(WORD type, wchar_t *format, ...) +{ + const wchar_t *msg[2]; + wchar_t buf[256]; + int size = _countof(buf); + + if (!o.event_log) + { + o.event_log = RegisterEventSource(NULL, TEXT(PACKAGE_NAME)); + if (!o.event_log) + return; + } + + va_list args; + va_start(args, format); + int nchar = vswprintf(buf, size-1, format, args); + va_end(args); + + if (nchar == -1) return; + + buf[size - 1] = '\0'; + + msg[0] = TEXT(PACKAGE_NAME); + msg[1] = buf; + ReportEventW(o.event_log, type, 0, 0, NULL, 2, 0, msg, NULL); +} + +/* + * Get dpi of the system and set the scale factor. + * The system dpi may be different from the per monitor dpi on + * Win 8.1 later. We set dpi awareness to system-dpi level in the + * manifest, and let Windows automatically re-scale windows + * if/when dpi changes dynamically. + */ +void +dpi_initialize(options_t *options) +{ + UINT dpix = 0; + HDC hdc = GetDC(NULL); + + if (hdc) + { + dpix = GetDeviceCaps(hdc, LOGPIXELSX); + ReleaseDC(NULL, hdc); + PrintDebug(L"System DPI: dpix = %u", dpix); + } + else + { + PrintDebug(L"GetDC failed, using default dpi = 96 (error = %lu)", GetLastError()); + dpix = 96; + } + + DpiSetScale(options, dpix); +} diff --git a/misc.h b/misc.h index 0af3114..9d55e2c 100644 --- a/misc.h +++ b/misc.h @@ -109,4 +109,21 @@ BOOL find_free_tcp_port(SOCKADDR_IN *addr); */ BOOL ParseManagementAddress(connection_t *c); +/** + * Get dpi of the system and set the scale factor. + * @param o : pointer to the options struct + * On return initializes o.dpi_scale using the logical pixels + * per inch value of the system. + */ +#define DPI_SCALE(x) MulDiv(x, o.dpi_scale, 100) +void dpi_initialize(options_t *o); + +/** + * Write a message to the event log + * @param type : event log type + * @param format : message format in printf style + * @param ... : extra args + */ +void MsgToEventLog(WORD type, wchar_t *format, ...); + #endif