From 04c9d16c1640545460b02958f52c49391a18f54b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Mon, 21 Nov 2022 17:26:43 +0800 Subject: [PATCH] util: introduce register helpers and use them (Windows) --- CMakeLists.txt | 1 + .../terminalfont/terminalfont_windows.c | 36 +++-------- src/detection/wmtheme/wmtheme_windows.c | 47 +++------------ src/util/windows/register.c | 59 +++++++++++++++++++ src/util/windows/register.h | 23 ++++++++ 5 files changed, 100 insertions(+), 66 deletions(-) create mode 100644 src/util/windows/register.c create mode 100644 src/util/windows/register.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 283c4729..e60ff1e4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -448,6 +448,7 @@ elseif(WIN32) src/detection/wmtheme/wmtheme_windows.c src/util/windows/getline.c src/util/windows/pwd.c + src/util/windows/register.c src/util/windows/utsname.c src/util/windows/wmi.cpp ) diff --git a/src/detection/terminalfont/terminalfont_windows.c b/src/detection/terminalfont/terminalfont_windows.c index e6ea456b..42041964 100644 --- a/src/detection/terminalfont/terminalfont_windows.c +++ b/src/detection/terminalfont/terminalfont_windows.c @@ -2,9 +2,7 @@ #include "common/io.h" #include "detection/terminalshell/terminalshell.h" #include "terminalfont.h" - -#define WIN32_LEAN_AND_MEAN 1 -#include +#include "util/windows/register.h" static void detectMintty(const FFinstance* instance, FFTerminalFontResult* terminalFont) { @@ -26,47 +24,29 @@ static void detectMintty(const FFinstance* instance, FFTerminalFontResult* termi ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars); } -static inline void wrapRegCloseKey(HKEY* phKey) -{ - if(*phKey) - RegCloseKey(*phKey); -} - static void detectConhost(const FFinstance* instance, FFTerminalFontResult* terminalFont) { FF_UNUSED(instance); //Current font of conhost doesn't seem to be detectable, we detect default font instead - HKEY __attribute__((__cleanup__(wrapRegCloseKey))) hKey = NULL; - if(RegOpenKeyExW(HKEY_CURRENT_USER, L"Console", 0, KEY_READ, &hKey) != ERROR_SUCCESS) - { - ffStrbufAppendS(&terminalFont->error, "RegOpenKeyExW() failed"); + FF_HKEY_AUTO_DESTROY hKey = NULL; + if(!ffRegOpenKeyForRead(HKEY_CURRENT_USER, "Console", &hKey, &terminalFont->error)) return; - } - DWORD bufSize; - - char fontName[128]; - bufSize = sizeof(fontName); - if(RegGetValueA(hKey, NULL, "FaceName", RRF_RT_REG_SZ, NULL, fontName, &bufSize) != ERROR_SUCCESS) - { - ffStrbufAppendS(&terminalFont->error, "RegGetValueA(FaceName) failed"); + FF_STRBUF_AUTO_DESTROY fontName; + ffStrbufInit(&fontName); + if(!ffRegReadStrbuf(hKey, "FaceName", &fontName, &terminalFont->error)) return; - } uint32_t fontSizeNum = 0; - bufSize = sizeof(fontSizeNum); - if(RegGetValueW(hKey, NULL, L"FontSize", RRF_RT_DWORD, NULL, &fontSizeNum, &bufSize) != ERROR_SUCCESS) - { - ffStrbufAppendS(&terminalFont->error, "RegGetValueW(FontSize) failed"); + if(!ffRegReadUint(hKey, "FontSize", &fontSizeNum, &terminalFont->error)) return; - } char fontSize[16]; _ultoa((unsigned long)(fontSizeNum >> 16), fontSize, 10); - ffFontInitValues(&terminalFont->font, fontName, fontSize); + ffFontInitValues(&terminalFont->font, fontName.chars, fontSize); } static void detectConEmu(const FFinstance* instance, FFTerminalFontResult* terminalFont) diff --git a/src/detection/wmtheme/wmtheme_windows.c b/src/detection/wmtheme/wmtheme_windows.c index 00303300..c84a2c92 100644 --- a/src/detection/wmtheme/wmtheme_windows.c +++ b/src/detection/wmtheme/wmtheme_windows.c @@ -1,60 +1,31 @@ #include "fastfetch.h" #include "wmtheme.h" - -#define WIN32_LEAN_AND_MEAN 1 -#include - -static inline void wrapRegCloseKey(HKEY* phKey) -{ - if(*phKey) - RegCloseKey(*phKey); -} +#include "util/windows/register.h" bool ffDetectWmTheme(FFinstance* instance, FFstrbuf* themeOrError) { FF_UNUSED(instance); - HKEY __attribute__((__cleanup__(wrapRegCloseKey))) hKey = NULL; - if(RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", 0, KEY_READ, &hKey) == ERROR_SUCCESS) + FF_HKEY_AUTO_DESTROY hKey = NULL; + if(ffRegOpenKeyForRead(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", &hKey, NULL)) { - int SystemUsesLightTheme = 1; - DWORD bufSize = sizeof(SystemUsesLightTheme); - - if(RegGetValueW(hKey, NULL, L"SystemUsesLightTheme", RRF_RT_DWORD, NULL, &SystemUsesLightTheme, &bufSize) != ERROR_SUCCESS) - { - ffStrbufAppendS(themeOrError, "RegGetValueW(SystemUsesLightTheme) failed"); + uint32_t SystemUsesLightTheme = 1; + if(!ffRegReadUint(hKey, "SystemUsesLightTheme", &SystemUsesLightTheme, themeOrError)) return false; - } - int AppsUsesLightTheme = 1; - bufSize = sizeof(AppsUsesLightTheme); - if(RegGetValueW(hKey, NULL, L"AppsUseLightTheme", RRF_RT_DWORD, NULL, &AppsUsesLightTheme, &bufSize) != ERROR_SUCCESS) - { - ffStrbufAppendS(themeOrError, "RegGetValueW(AppsUseLightTheme) failed"); + uint32_t AppsUsesLightTheme = 1; + if(!ffRegReadUint(hKey, "AppsUseLightTheme", &AppsUsesLightTheme, themeOrError)) return false; - } ffStrbufAppendF(themeOrError, "System - %s, Apps - %s", SystemUsesLightTheme ? "Light" : "Dark", AppsUsesLightTheme ? "Light" : "Dark"); return true; } - else if(RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes", 0, KEY_READ, &hKey) == ERROR_SUCCESS) + else if(ffRegOpenKeyForRead(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes", &hKey, NULL)) { - DWORD length = 0; - if(RegGetValueA(hKey, NULL, "CurrentTheme", RRF_RT_REG_SZ, NULL, NULL, &length) != ERROR_SUCCESS) - { - ffStrbufAppendS(themeOrError, "RegGetValueA(CurrentTheme, NULL) failed"); + if(!ffRegReadStrbuf(hKey, "CurrentTheme", themeOrError, themeOrError)) return false; - } - ffStrbufEnsureFree(themeOrError, length); - if(RegGetValueA(hKey, NULL, "CurrentTheme", RRF_RT_REG_SZ, NULL, themeOrError->chars, &length) != ERROR_SUCCESS) - { - ffStrbufAppendS(themeOrError, "RegGetValueA(CurrentTheme) failed"); - return false; - } - - themeOrError->length = length; ffStrbufSubstrBeforeLastC(themeOrError, '.'); ffStrbufSubstrAfterLastC(themeOrError, '\\'); if (isalpha(themeOrError->chars[0])) diff --git a/src/util/windows/register.c b/src/util/windows/register.c new file mode 100644 index 00000000..1e477f2e --- /dev/null +++ b/src/util/windows/register.c @@ -0,0 +1,59 @@ +#include "register.h" + +static const char* hKey2Str(HKEY hKey) +{ + #define HKEY_CASE(compareKey) if(hKey == compareKey) return #compareKey; + HKEY_CASE(HKEY_CLASSES_ROOT) + HKEY_CASE(HKEY_CURRENT_USER) + HKEY_CASE(HKEY_LOCAL_MACHINE) + HKEY_CASE(HKEY_USERS) + HKEY_CASE(HKEY_PERFORMANCE_DATA) + HKEY_CASE(HKEY_PERFORMANCE_TEXT) + HKEY_CASE(HKEY_PERFORMANCE_NLSTEXT) + HKEY_CASE(HKEY_CURRENT_CONFIG) + HKEY_CASE(HKEY_DYN_DATA) + HKEY_CASE(HKEY_CURRENT_USER_LOCAL_SETTINGS) + #undef HKEY_CASE + + return "UNKNOWN"; +} + +bool ffRegOpenKeyForRead(HKEY hKey, const char* lpSubKey, HKEY* result, FFstrbuf* error) +{ + if(RegOpenKeyExA(hKey, lpSubKey, 0, KEY_READ, result) != ERROR_SUCCESS) + { + if(error) + ffStrbufAppendF(error, "RegOpenKeyExW(%s\\%s) failed", hKey2Str(hKey), lpSubKey); + return false; + } + return true; +} + +bool ffRegReadStrbuf(HKEY hKey, const char* valueName, FFstrbuf* result, FFstrbuf* error) +{ + DWORD bufSize; //with tailing '\0' + if(RegGetValueA(hKey, NULL, valueName, RRF_RT_REG_SZ, NULL, NULL, &bufSize) != ERROR_SUCCESS) + { + if(error) ffStrbufAppendF(error, "RegGetValueA(%s, NULL, RRF_RT_REG_SZ) failed", valueName); + return false; + } + ffStrbufEnsureFree(result, bufSize - 1); + if(RegGetValueA(hKey, NULL, valueName, RRF_RT_REG_SZ, NULL, result->chars, &bufSize) != ERROR_SUCCESS) + { + if(error) ffStrbufAppendF(error, "RegGetValueA(%s, result, RRF_RT_REG_SZ) failed", valueName); + return false; + } + result->length = bufSize - 1; + return true; +} + +bool ffRegReadUint(HKEY hKey, const char* valueName, uint32_t* result, FFstrbuf* error) +{ + DWORD bufSize = sizeof(*result); + if(RegGetValueA(hKey, NULL, valueName, RRF_RT_DWORD, NULL, result, &bufSize) != ERROR_SUCCESS) + { + if(error) ffStrbufAppendF(error, "RegGetValueA(%s, result, RRF_RT_DWORD) failed", valueName); + return false; + } + return true; +} diff --git a/src/util/windows/register.h b/src/util/windows/register.h new file mode 100644 index 00000000..25746c1f --- /dev/null +++ b/src/util/windows/register.h @@ -0,0 +1,23 @@ +#pragma once + +#ifndef FASTFETCH_INCLUDED_REGISTER_H +#define FASTFETCH_INCLUDED_REGISTER_H + +#include "fastfetch.h" + +#define WIN32_LEAN_AND_MEAN +#include + +static inline void wrapRegCloseKey(HKEY* phKey) +{ + if(*phKey) + RegCloseKey(*phKey); +} + +#define FF_HKEY_AUTO_DESTROY HKEY __attribute__((__cleanup__(wrapRegCloseKey))) + +bool ffRegOpenKeyForRead(HKEY hKey, const char* lpSubKey, HKEY* result, FFstrbuf* error); +bool ffRegReadStrbuf(HKEY hKey, const char* valueName, FFstrbuf* result, FFstrbuf* error); +bool ffRegReadUint(HKEY hKey, const char* valueName, uint32_t* result, FFstrbuf* error); + +#endif