mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2025-02-20 11:43:27 +08:00
util: introduce register helpers and use them (Windows)
This commit is contained in:
parent
308408a962
commit
04c9d16c16
@ -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
|
||||
)
|
||||
|
@ -2,9 +2,7 @@
|
||||
#include "common/io.h"
|
||||
#include "detection/terminalshell/terminalshell.h"
|
||||
#include "terminalfont.h"
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <Windows.h>
|
||||
#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)
|
||||
|
@ -1,60 +1,31 @@
|
||||
#include "fastfetch.h"
|
||||
#include "wmtheme.h"
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <Windows.h>
|
||||
|
||||
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]))
|
||||
|
59
src/util/windows/register.c
Normal file
59
src/util/windows/register.c
Normal file
@ -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;
|
||||
}
|
23
src/util/windows/register.h
Normal file
23
src/util/windows/register.h
Normal file
@ -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 <Windows.h>
|
||||
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user