From d670a9a584279b982e5f1adc74a93795612f8b29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 6 Nov 2022 01:29:00 +0800 Subject: [PATCH 01/27] Kernel: support Windows 7 --- src/util/windows/utsname.c | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/util/windows/utsname.c b/src/util/windows/utsname.c index 07f6e52e81..a1f68021df 100644 --- a/src/util/windows/utsname.c +++ b/src/util/windows/utsname.c @@ -27,20 +27,22 @@ static int detectVersion(struct utsname *name) DWORD bufSize; - DWORD currentMajorVersionNumber; - bufSize = sizeof(currentMajorVersionNumber); - if(RegGetValueA(hKey, NULL, "CurrentMajorVersionNumber", RRF_RT_REG_DWORD, NULL, ¤tMajorVersionNumber, &bufSize) != ERROR_SUCCESS) - { - RegCloseKey(hKey); - return 1; - } + char currentVersion[32]; - DWORD currentMinorVersionNumber; - bufSize = sizeof(currentMinorVersionNumber); - if(RegGetValueA(hKey, NULL, "CurrentMinorVersionNumber", RRF_RT_REG_DWORD, NULL, ¤tMinorVersionNumber, &bufSize) != ERROR_SUCCESS) { - RegCloseKey(hKey); - return 1; + DWORD currentMajorVersionNumber; + DWORD currentMinorVersionNumber; + bufSize = sizeof(currentMajorVersionNumber); + if(RegGetValueW(hKey, NULL, L"CurrentMajorVersionNumber", RRF_RT_REG_DWORD, NULL, ¤tMajorVersionNumber, &bufSize) == ERROR_SUCCESS && + RegGetValueW(hKey, NULL, L"CurrentMinorVersionNumber", RRF_RT_REG_DWORD, NULL, ¤tMinorVersionNumber, &bufSize) == ERROR_SUCCESS + ) + snprintf(currentVersion, sizeof(currentVersion), "%u.%u", (unsigned)currentMajorVersionNumber, (unsigned)currentMinorVersionNumber); + else + { + bufSize = sizeof(currentVersion); + if(RegGetValueA(hKey, NULL, "CurrentVersion", RRF_RT_REG_SZ, NULL, currentVersion, &bufSize) != ERROR_SUCCESS) + strcpy(currentVersion, "0.0"); + } } char currentBuildNumber[32]; @@ -53,7 +55,7 @@ static int detectVersion(struct utsname *name) if(RegGetValueA(hKey, NULL, "UBR", RRF_RT_REG_DWORD, NULL, &ubr, &bufSize) != ERROR_SUCCESS || bufSize != sizeof(ubr)) ubr = 0; - snprintf(name->release, sizeof(name->release), "%u.%u.%s.%u", (unsigned)currentMajorVersionNumber, (unsigned)currentMinorVersionNumber, currentBuildNumber, (unsigned)ubr); + snprintf(name->release, sizeof(name->release), "%s.%s.%u", currentVersion, currentBuildNumber, (unsigned)ubr); bufSize = sizeof(name->version); RegGetValueA(hKey, NULL, "DisplayVersion", RRF_RT_REG_SZ, NULL, name->version, &bufSize); From f889d919f818dbd41f90962135ebaae7ef3c3497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 6 Nov 2022 14:41:44 +0800 Subject: [PATCH 02/27] TerminalShell: add basic support for ConEmu --- src/detection/terminalshell/terminalshell_windows.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/detection/terminalshell/terminalshell_windows.cpp b/src/detection/terminalshell/terminalshell_windows.cpp index 770e043eb5..6f0139a2ee 100644 --- a/src/detection/terminalshell/terminalshell_windows.cpp +++ b/src/detection/terminalshell/terminalshell_windows.cpp @@ -189,6 +189,8 @@ static uint32_t getTerminalInfo(FFTerminalShellResult* result, uint32_t pid) ffStrbufSetS(&result->terminalPrettyName, "Visual Studio Code"); else if(ffStrbufIgnCaseEqualS(&result->terminalPrettyName, "explorer")) ffStrbufSetS(&result->terminalPrettyName, "Windows Explorer"); + else if(ffStrbufStartsWithIgnCaseS(&result->terminalPrettyName, "ConEmuC")) + ffStrbufSetS(&result->terminalPrettyName, "ConEmu"); return ppid; } From 0605cb754ba2ecdae7a8560e86e75363c38a8a38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 6 Nov 2022 14:55:41 +0800 Subject: [PATCH 03/27] OS: fix Windows 7 detection --- src/detection/os/os_windows.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/detection/os/os_windows.cpp b/src/detection/os/os_windows.cpp index e28607c9c1..5bd9d11c53 100644 --- a/src/detection/os/os_windows.cpp +++ b/src/detection/os/os_windows.cpp @@ -26,6 +26,7 @@ void ffDetectOSImpl(FFOSResult* os, const FFinstance* instance) if(FFWmiRecord record = query.next()) { record.getString(L"Caption", &os->variant); + ffStrbufTrimRight(&os->variant, ' '); if(ffStrbufStartsWithS(&os->variant, "Microsoft Windows ")) { ffStrbufAppendS(&os->name, "Microsoft Windows"); From 1ea44417d054beae28f566caa4c93cfc3e5983ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 6 Nov 2022 15:48:12 +0800 Subject: [PATCH 04/27] TerminalFont: add support for ConEmu --- .../terminalfont/terminalfont_windows.c | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/detection/terminalfont/terminalfont_windows.c b/src/detection/terminalfont/terminalfont_windows.c index 6a68bb6a5f..7e1d5c8cba 100644 --- a/src/detection/terminalfont/terminalfont_windows.c +++ b/src/detection/terminalfont/terminalfont_windows.c @@ -1,4 +1,5 @@ #include "common/properties.h" +#include "common/io.h" #include "detection/terminalshell/terminalshell.h" #include "terminalfont.h" @@ -73,10 +74,64 @@ static void detectConhost(const FFinstance* instance, FFTerminalFontResult* term RegCloseKey(hKey); } +static void detectConEmu(const FFinstance* instance, FFTerminalFontResult* terminalFont) +{ + FF_UNUSED(instance) + + //https://conemu.github.io/en/ConEmuXml.html#search-sequence + FFstrbuf path; + ffStrbufInit(&path); + + FFstrbuf fontName; + ffStrbufInit(&fontName); + + FFstrbuf fontSize; + ffStrbufInit(&fontSize); + + const char* paths[] = { "ConEmuDir", "ConEmuBaseDir", "APPDATA" }; + for (uint32_t i = 0; i < sizeof(paths) / sizeof(paths[0]); ++i) + { + ffStrbufSetS(&path, getenv(paths[i])); + if(path.length > 0) + { + ffStrbufAppendS(&path, "/ConEmu.xml"); + if(ffParsePropFileValues(path.chars, 2, (FFpropquery[]){ + {"error, "Failed to parse ConEmu.xml"); + return; + } + + if(fontName.length > 0) + ffStrbufSubstrBeforeLastC(&fontName, '"'); + else + ffStrbufAppendS(&fontName, "Consola"); + + if(fontSize.length > 0) + ffStrbufSubstrBeforeLastC(&fontSize, '"'); + else + ffStrbufAppendS(&fontSize, "14"); + + ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars); + + ffStrbufDestroy(&fontName); + ffStrbufDestroy(&fontSize); +} + void ffDetectTerminalFontPlatform(const FFinstance* instance, const FFTerminalShellResult* terminalShell, FFTerminalFontResult* terminalFont) { if(ffStrbufIgnCaseCompS(&terminalShell->terminalProcessName, "mintty") == 0) detectMintty(instance, terminalFont); else if(ffStrbufIgnCaseCompS(&terminalShell->terminalProcessName, "conhost.exe") == 0) detectConhost(instance, terminalFont); + else if(ffStrbufStartsWithIgnCaseS(&terminalShell->terminalProcessName, "ConEmuC")) + detectConEmu(instance, terminalFont); } From 85a554b60de4e9bd620e80304788ebf2e4a99747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 6 Nov 2022 17:04:34 +0800 Subject: [PATCH 05/27] CPU: support Windows 7 --- src/detection/cpu/cpu_windows.cpp | 17 +++++++++++++---- src/util/windows/wmi.hpp | 20 +++++++++++++------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/detection/cpu/cpu_windows.cpp b/src/detection/cpu/cpu_windows.cpp index 8740fa4b96..e5939cd604 100644 --- a/src/detection/cpu/cpu_windows.cpp +++ b/src/detection/cpu/cpu_windows.cpp @@ -17,11 +17,21 @@ void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu, bool cached) ffStrbufInit(&cpu->name); ffStrbufInit(&cpu->vendor); - FFWmiQuery query(L"SELECT Name, Manufacturer, NumberOfCores, NumberOfLogicalProcessors, ThreadCount, CurrentClockSpeed, MaxClockSpeed FROM Win32_Processor WHERE ProcessorType = 3"); + FFWmiQuery query(L"SELECT Name, Manufacturer, NumberOfCores, NumberOfLogicalProcessors, NumberOfEnabledCore, CurrentClockSpeed, MaxClockSpeed FROM Win32_Processor WHERE ProcessorType = 3"); if(!query) return; - if(FFWmiRecord record = query.next()) + FFWmiRecord record = query.next(); + if(!record) + { + //NumberOfEnabledCore is not supported on Windows 10- + query = FFWmiQuery(L"SELECT Name, Manufacturer, NumberOfCores, NumberOfLogicalProcessors, CurrentClockSpeed, MaxClockSpeed FROM Win32_Processor WHERE ProcessorType = 3"); + if(!query) + return; + record = query.next(); + } + + if(record) { record.getString(L"Name", &cpu->name); record.getString(L"Manufacturer", &cpu->vendor); @@ -32,8 +42,7 @@ void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu, bool cached) cpu->coresPhysical = (uint16_t)value; record.getUnsigned(L"NumberOfLogicalProcessors", &value); cpu->coresLogical = (uint16_t)value; - record.getUnsigned(L"ThreadCount", &value); - cpu->coresOnline = (uint16_t)value; + cpu->coresOnline = record.getUnsigned(L"NumberOfEnabledCore", &value) ? (uint16_t)value : cpu->coresPhysical; record.getUnsigned(L"CurrentClockSpeed", &value); //There's no MinClockSpeed in Win32_Processor cpu->frequencyMin = (double)value / 1000.0; record.getUnsigned(L"MaxClockSpeed", &value); diff --git a/src/util/windows/wmi.hpp b/src/util/windows/wmi.hpp index 7b27c75904..6f7d214cb9 100644 --- a/src/util/windows/wmi.hpp +++ b/src/util/windows/wmi.hpp @@ -23,12 +23,15 @@ struct FFWmiRecord if(!ok) obj = nullptr; } FFWmiRecord(const FFWmiRecord&) = delete; - FFWmiRecord(FFWmiRecord&& other) { + FFWmiRecord(FFWmiRecord&& other) { *this = (FFWmiRecord&&)other; } + ~FFWmiRecord() { if(obj) obj->Release(); } + explicit operator bool() { return !!obj; } + FFWmiRecord& operator =(FFWmiRecord&& other) { + if(obj) obj->Release(); obj = other.obj; other.obj = nullptr; + return *this; } - ~FFWmiRecord() { if(obj) obj->Release(); } - explicit operator bool() { return !!obj; } bool getString(const wchar_t* key, FFstrbuf* strbuf); bool getSigned(const wchar_t* key, int64_t* integer); @@ -43,13 +46,16 @@ struct FFWmiQuery FFWmiQuery(const wchar_t* queryStr, FFstrbuf* error = nullptr); explicit FFWmiQuery(IEnumWbemClassObject* pEnumerator): pEnumerator(pEnumerator) {} FFWmiQuery(const FFWmiQuery& other) = delete; - FFWmiQuery(FFWmiQuery&& other) { - pEnumerator = other.pEnumerator; - other.pEnumerator = nullptr; - } + FFWmiQuery(FFWmiQuery&& other) { *this = (FFWmiQuery&&)other; } ~FFWmiQuery() { if(pEnumerator) pEnumerator->Release(); } explicit operator bool() { return !!pEnumerator; } + FFWmiQuery& operator =(FFWmiQuery&& other) { + if(pEnumerator) pEnumerator->Release(); + pEnumerator = other.pEnumerator; + other.pEnumerator = nullptr; + return *this; + } FFWmiRecord next() { FFWmiRecord result(pEnumerator); From f4666fcb0a083486c32fa82f362992e8bb927bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 6 Nov 2022 16:01:10 +0800 Subject: [PATCH 06/27] README: document Windows 10- support --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 646e4277ea..c9a0a17764 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Fastfetch -Fastfetch is a [neofetch](https://github.com/dylanaraps/neofetch)-like tool for fetching system information and displaying them in a pretty way. It is written in pure c, with performance and customizability in mind. Currently Linux, Android, FreeBSD, MacOS and Windows are supported. +Fastfetch is a [neofetch](https://github.com/dylanaraps/neofetch)-like tool for fetching system information and displaying them in a pretty way. It is written in pure c, with performance and customizability in mind. Currently Linux, Android, FreeBSD, MacOS and Windows 7+ are supported. @@ -61,6 +61,8 @@ The following libraries are used if present at runtime: * [`libvulkan`](https://www.vulkan.org/): Vulkan module. Usually has been provided by GPU drivers. * [`libOpenCL`](https://www.khronos.org/opencl/): OpenCL module +Note: On Windows 10-, [ConEmu](https://conemu.github.io/en/AnsiEscapeCodes.html) is required to run fastfetch due to [the lack of ASCII escape code native support](https://en.wikipedia.org/wiki/ANSI_escape_code#DOS,_OS/2,_and_Windows). Also make sure to [use `chcp 65001` to enable UTF-8 support](https://conemu.github.io/en/UnicodeSupport.html#utf-8) if you run Windows locale other than English. + ### Android * [`freetype`](https://www.freetype.org/): Used for Termux font detection. @@ -101,7 +103,7 @@ KDE Plasma, Gnome, Cinnamon, Mate, XFCE4, LXQt ##### Terminal fonts ``` -Konsole, Gnome Terminal, Tilix, XFCE4 Terminal, Alacritty, LXTerminal, iTerm2, Apple Terminal, TTY, Windows Terminal, Termux, mintty +Konsole, Gnome Terminal, Tilix, XFCE4 Terminal, Alacritty, LXTerminal, iTerm2, Apple Terminal, TTY, Windows Terminal, Termux, mintty, ConEmu ``` ## Building From e3de39d3ab56d54679dd8480074a10d12f02c474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 6 Nov 2022 01:33:06 +0800 Subject: [PATCH 07/27] Windows: add version info in binaries --- CMakeLists.txt | 22 ++++++++++++++++ src/util/windows/version.rc.in | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/util/windows/version.rc.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 91649c043e..1edbb9bc1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,8 @@ project(fastfetch HOMEPAGE_URL "https://github.com/LinusDierheimer/fastfetch" ) +set(PROJECT_LICENSE "MIT license") + ################### # Target Platform # ################### @@ -578,6 +580,26 @@ target_link_libraries(flashfetch PRIVATE libfastfetch ) +if(WIN32) + if(PROJECT_VERSION_TWEAK) + string(REGEX MATCH "[0-9]+" PROJECT_VERSION_TWEAK_NUM "${PROJECT_VERSION_TWEAK}") + else() + set(PROJECT_VERSION_TWEAK_NUM "0") + endif() + + set(TARGET_NAME fastfetch) + configure_file(src/util/windows/version.rc.in version.fastfetch.rc) + target_sources(fastfetch + PRIVATE version.fastfetch.rc + ) + + set(TARGET_NAME flashfetch) + configure_file(src/util/windows/version.rc.in version.flashfetch.rc) + target_sources(flashfetch + PRIVATE version.flashfetch.rc + ) +endif() + ################### # Testing targets # ################### diff --git a/src/util/windows/version.rc.in b/src/util/windows/version.rc.in new file mode 100644 index 0000000000..3e1833eec4 --- /dev/null +++ b/src/util/windows/version.rc.in @@ -0,0 +1,47 @@ +// +// Include the necessary resources +// +#include +#include + +#ifdef RC_INVOKED + +// +// Set up debug information +// +#if DEBUG +#define VER_DEBUG VS_FF_DEBUG +#else +#define VER_DEBUG 0 +#endif + +// ------- version info ------------------------------------------------------- + +VS_VERSION_INFO VERSIONINFO +FILEVERSION @PROJECT_VERSION_MAJOR@,@PROJECT_VERSION_MINOR@,@PROJECT_VERSION_PATCH@,@PROJECT_VERSION_TWEAK_NUM@ +PRODUCTVERSION @PROJECT_VERSION_MAJOR@,@PROJECT_VERSION_MINOR@,@PROJECT_VERSION_PATCH@,@PROJECT_VERSION_TWEAK_NUM@ +FILEFLAGSMASK VS_FFI_FILEFLAGSMASK +FILEFLAGS (VER_DEBUG|VS_FF_PRERELEASE) +FILEOS VOS_NT +FILETYPE VFT_APP +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904b0" + BEGIN + VALUE "Comments", "@PROJECT_HOMEPAGE_URL@" + VALUE "FileDescription", "@PROJECT_DESCRIPTION@" + VALUE "FileVersion", "@PROJECT_VERSION@@PROJECT_VERSION_TWEAK@" + VALUE "InternalName", "@TARGET_NAME@.exe" + VALUE "LegalCopyright", "@PROJECT_LICENSE@" + VALUE "OriginalFilename", "@TARGET_NAME@.exe" + VALUE "ProductName", "@PROJECT_NAME@" + VALUE "ProductVersion", "@PROJECT_VERSION@@PROJECT_VERSION_TWEAK@" + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x0409,1252 + END +END +#endif From a5306d60eec383f0c697bd16ac308254dc1dce45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sat, 12 Nov 2022 23:30:34 +0800 Subject: [PATCH 08/27] Windows: enable console processing, disable output buffer UCRT doesn't support line buffering, which makes modules being not printed ASAP --- src/common/init.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/common/init.c b/src/common/init.c index 962bde9fee..8a424a4832 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -125,7 +125,6 @@ static void initState(FFstate* state) #ifdef WIN32 //https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale?source=recommendations&view=msvc-170#utf-8-support setlocale(LC_ALL, ".UTF8"); - setvbuf(stdout, NULL, _IOFBF, 4096); #endif state->logoWidth = 0; @@ -334,12 +333,9 @@ static void resetConsole() #ifdef _WIN32 BOOL WINAPI consoleHandler(DWORD signal) { - if(signal == CTRL_C_EVENT) - { - resetConsole(); - return TRUE; - } - return false; + FF_UNUSED(signal); + resetConsole(); + exit(0); } #else static void exitSignalHandler(int signal) @@ -360,6 +356,11 @@ void ffStart(FFinstance* instance) #ifdef _WIN32 SetConsoleCtrlHandler(consoleHandler, TRUE); + HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); + DWORD mode = 0; + GetConsoleMode(hStdout, &mode); + SetConsoleMode(hStdout, mode | ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING); + SetConsoleOutputCP(CP_UTF8); #else struct sigaction action = { .sa_handler = exitSignalHandler }; sigaction(SIGINT, &action, NULL); From 227eb707d2cada6b79f4b976f3b0901922c07ea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 15 Nov 2022 17:30:16 +0800 Subject: [PATCH 09/27] CpuUsage: fix calucation on Windows --- src/detection/cpuUsage/cpuUsage_windows.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/detection/cpuUsage/cpuUsage_windows.c b/src/detection/cpuUsage/cpuUsage_windows.c index 1aa9ba3f64..bf4cc7644e 100644 --- a/src/detection/cpuUsage/cpuUsage_windows.c +++ b/src/detection/cpuUsage/cpuUsage_windows.c @@ -1,8 +1,7 @@ #include "fastfetch.h" #include "cpuUsage.h" -#define WIN32_LEAN_AND_MEAN 1 -#include +#include static inline uint64_t fileTimeToUint64(const FILETIME* ft) { return (((uint64_t)ft->dwHighDateTime) << 32) | ((uint64_t)ft->dwLowDateTime); @@ -11,10 +10,12 @@ static inline uint64_t fileTimeToUint64(const FILETIME* ft) { const char* ffGetCpuUsageInfo(uint64_t* inUseAll, uint64_t* totalAll) { FILETIME idleTime, kernelTime, userTime; - if(GetSystemTimes(&idleTime, &kernelTime, &userTime) == 0) + if(!GetSystemTimes(&idleTime, &kernelTime, &userTime)) return "GetSystemTimes() failed"; - *inUseAll = fileTimeToUint64(&userTime) + fileTimeToUint64(&kernelTime); - *totalAll = *inUseAll + fileTimeToUint64(&idleTime); + // https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getsystemtimes + // `kernelTime` also includes the amount of time the system has been idle. + *totalAll = fileTimeToUint64(&userTime) + fileTimeToUint64(&kernelTime); + *inUseAll = *totalAll - fileTimeToUint64(&idleTime); return NULL; } From 3f008c4b6d10ea8ec54e6591cbef41c92786f959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 15 Nov 2022 17:48:57 +0800 Subject: [PATCH 10/27] CpuUsage: improve performance --- src/detection/cpuUsage/cpuUsage.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/detection/cpuUsage/cpuUsage.c b/src/detection/cpuUsage/cpuUsage.c index 7470236dc3..82bf1f7d43 100644 --- a/src/detection/cpuUsage/cpuUsage.c +++ b/src/detection/cpuUsage/cpuUsage.c @@ -36,23 +36,30 @@ const char* ffGetCpuUsageResult(double* result) error = ffGetCpuUsageInfo(&inUseAll1, &totalAll1); if(error) return error; - ffTimeSleep(1000); + ffTimeSleep(250); } else { uint64_t duration = ffTimeGetTick() - startTime; - if(duration < 1000) - ffTimeSleep(1000 - (uint32_t) duration); + if(duration < 250) + ffTimeSleep(250 - (uint32_t) duration); } - uint64_t inUseAll2, totalAll2; - error = ffGetCpuUsageInfo(&inUseAll2, &totalAll2); - if(error) - return error; - - *result = (double)(inUseAll2 - inUseAll1) / (double)(totalAll2 - totalAll1) * 100; + while(true) + { + uint64_t inUseAll2, totalAll2; + error = ffGetCpuUsageInfo(&inUseAll2, &totalAll2); + if(error) + return error; - return NULL; + if(inUseAll2 != inUseAll1) + { + *result = (double)(inUseAll2 - inUseAll1) / (double)(totalAll2 - totalAll1) * 100; + return NULL; + } + else + ffTimeSleep(250); + } } #endif //FF_DETECTION_CPUUSAGE_NOWAIT From 0bbecb3b3348420925443487d5fefcf9561111d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 17 Nov 2022 00:13:25 +0800 Subject: [PATCH 11/27] Thread: silence compiler warnings --- src/common/thread.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/thread.h b/src/common/thread.h index 74c536cf2b..c4d2230842 100644 --- a/src/common/thread.h +++ b/src/common/thread.h @@ -29,7 +29,7 @@ static inline void ffThreadMutexLock(FFThreadMutex* mutex) { pthread_mutex_lock(mutex); } static inline void ffThreadMutexUnlock(FFThreadMutex* mutex) { pthread_mutex_unlock(mutex); } static inline FFThreadType ffThreadCreate(void* (* func)(void*), void* data) { - FFThreadType newThread = NULL; + FFThreadType newThread = 0; pthread_create(&newThread, NULL, func, data); return newThread; } From 8aca8a22298f1396327895a521f0885131dd08ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 15 Nov 2022 14:57:45 +0800 Subject: [PATCH 12/27] Temps: silence compiler warnings --- src/detection/temps/temps_apple.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/detection/temps/temps_apple.c b/src/detection/temps/temps_apple.c index 015546ecf0..9daee1c917 100644 --- a/src/detection/temps/temps_apple.c +++ b/src/detection/temps/temps_apple.c @@ -104,12 +104,11 @@ static uint32_t smcStrtoul(const char *str, int size, int base) static void smcUltostr(char *str, uint32_t val) { - str[0] = '\0'; - sprintf(str, "%c%c%c%c", - (unsigned int)val >> 24, - (unsigned int)val >> 16, - (unsigned int)val >> 8, - (unsigned int)val); + str[0] = (char)(val >> 24); + str[1] = (char)(val >> 16); + str[2] = (char)(val >> 8); + str[3] = (char)val; + str[4] = '\0'; } static const char *smcCall(io_connect_t conn, uint32_t selector, SmcKeyData_t *inputStructure, SmcKeyData_t *outputStructure) From d0c18c46796f05189aafd75da85bacd232c162b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 16 Nov 2022 18:19:23 +0800 Subject: [PATCH 13/27] DisplayServer: don't use private API (macOS) --- .../displayserver/displayserver_apple.c | 56 ++++++------------- 1 file changed, 17 insertions(+), 39 deletions(-) diff --git a/src/detection/displayserver/displayserver_apple.c b/src/detection/displayserver/displayserver_apple.c index 80211fb38b..96bdf3d2ea 100644 --- a/src/detection/displayserver/displayserver_apple.c +++ b/src/detection/displayserver/displayserver_apple.c @@ -4,53 +4,31 @@ #include #include #include -#include - -//Resolution code heavily inspired by displayplacer <3 - -typedef union -{ - uint8_t rawData[0xDC]; - struct - { - uint32_t mode; - uint32_t flags; // 0x4 - uint32_t width; // 0x8 - uint32_t height; // 0xC - uint32_t depth; // 0x10 - uint32_t dc2[42]; - uint16_t dc3; - uint16_t freq; // 0xBC - uint32_t dc4[4]; - float density; // 0xD0 - } derived; -} modes_D4; - -void CGSGetCurrentDisplayMode(CGDirectDisplayID display, int* modeNum); -void CGSGetDisplayModeDescriptionOfLength(CGDirectDisplayID display, int idx, modes_D4* mode, int length); +#include +#include static void detectResolution(FFDisplayServerResult* ds) { - CGDisplayCount screenCount; - CGGetOnlineDisplayList(INT_MAX, NULL, &screenCount); - if(screenCount == 0) + CGDirectDisplayID screens[128]; + uint32_t screenCount; + if(CGGetOnlineDisplayList(sizeof(screens) / sizeof(screens[0]), screens, &screenCount) != kCGErrorSuccess) return; - CGDirectDisplayID* screens = malloc(screenCount * sizeof(CGDirectDisplayID)); - CGGetOnlineDisplayList(INT_MAX, screens, &screenCount); - for(uint32_t i = 0; i < screenCount; i++) { - int modeID; - CGSGetCurrentDisplayMode(screens[i], &modeID); - modes_D4 mode; - CGSGetDisplayModeDescriptionOfLength(screens[i], modeID, &mode, 0xD4); - - uint32_t refreshRate = ffdsParseRefreshRate(mode.derived.freq); - ffdsAppendResolution(ds, mode.derived.width, mode.derived.height, refreshRate); + CGDirectDisplayID screen = screens[i]; + CGDisplayModeRef mode = CGDisplayCopyDisplayMode(screen); + if(mode) + { + ffdsAppendResolution(ds, + (uint32_t)CGDisplayModeGetWidth(mode), + (uint32_t)CGDisplayModeGetHeight(mode), + (uint32_t)CGDisplayModeGetRefreshRate(mode) + ); + CGDisplayModeRelease(mode); + } + CGDisplayRelease(screen); } - - free(screens); } static void detectWM(FFDisplayServerResult* ds) From 0361a30c401389f06ac76d7658e83df6cef5f810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 16 Nov 2022 18:20:28 +0800 Subject: [PATCH 14/27] Font: simplify code; fix memleaks (macOS) --- src/detection/font/font.h | 8 ++++---- src/detection/font/font_apple.m | 19 +++++-------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/detection/font/font.h b/src/detection/font/font.h index 63d19e2ee6..80bed7a395 100644 --- a/src/detection/font/font.h +++ b/src/detection/font/font.h @@ -12,10 +12,10 @@ typedef struct FFFontResult FFstrbuf error; /** - * Linux / BSD: QT, GTK2, GTK3, GTK4 - * MacOS: System, User, Monospace, Application - * Windows: Desktop, Unset, Unset, Unset - * Other: Unset, Unset, Unset, Unset + * Linux / BSD: QT, GTK2, GTK3, GTK4 + * MacOS: System, User, System Mono, User Mono + * Windows: Desktop, Unset, Unset, Unset + * Other: Unset, Unset, Unset, Unset */ FFstrbuf fonts[FF_DETECT_FONT_NUM_FONTS]; } FFFontResult; diff --git a/src/detection/font/font_apple.m b/src/detection/font/font_apple.m index 91689f6e93..773c7705b5 100644 --- a/src/detection/font/font_apple.m +++ b/src/detection/font/font_apple.m @@ -1,23 +1,14 @@ #include "common/font.h" #include "common/io.h" -#include "util/apple/cf_helpers.h" #include "font.h" -#import - -static void detectFontForType(CTFontUIFontType uiType, FFstrbuf* font) -{ - CTFontRef ctFont = CTFontCreateUIFontForLanguage(uiType, 12, NULL); - ffCfStrGetString(CTFontCopyFamilyName(ctFont), font); -} +#import void ffDetectFontImpl(const FFinstance* instance, FFFontResult* result) { FF_UNUSED(instance); - ffSuppressIO(true); - detectFontForType(kCTFontUIFontSystem, &result->fonts[0]); - detectFontForType(kCTFontUIFontUser, &result->fonts[1]); - detectFontForType(kCTFontUIFontUserFixedPitch, &result->fonts[2]); - detectFontForType(kCTFontUIFontApplication, &result->fonts[3]); - ffSuppressIO(false); + ffStrbufAppendS(&result->fonts[0], [NSFont systemFontOfSize:12].familyName.UTF8String); + ffStrbufAppendS(&result->fonts[1], [NSFont userFontOfSize:12].familyName.UTF8String); + ffStrbufAppendS(&result->fonts[2], [NSFont monospacedSystemFontOfSize:12 weight:400].familyName.UTF8String); + ffStrbufAppendS(&result->fonts[3], [NSFont userFixedPitchFontOfSize:12].familyName.UTF8String); } From d0d6f48751f2a56e05e0308f1fa5cdb3f577e710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Wed, 16 Nov 2022 18:21:20 +0800 Subject: [PATCH 15/27] osascript: simplify code (macOS) --- src/util/apple/osascript.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/util/apple/osascript.m b/src/util/apple/osascript.m index 363a50d12d..8b01ba0697 100644 --- a/src/util/apple/osascript.m +++ b/src/util/apple/osascript.m @@ -4,14 +4,14 @@ #import #import -bool ffOsascript(const char* input, FFstrbuf* result) { - NSString* appleScript = [NSString stringWithUTF8String: input]; - NSAppleScript* script = [[NSAppleScript alloc] initWithSource:appleScript]; +bool ffOsascript(const char* input, FFstrbuf* result) +{ + NSAppleScript* script = [NSAppleScript.alloc initWithSource:@(input)]; NSDictionary* errInfo = nil; NSAppleEventDescriptor* descriptor = [script executeAndReturnError:&errInfo]; if (errInfo) return false; - ffStrbufSetS(result, [[descriptor stringValue] cStringUsingEncoding:NSUTF8StringEncoding]); + ffStrbufSetS(result, descriptor.stringValue.UTF8String); return true; } From a498e9e14230c09c00d1f89409f609fa551c1e4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 17 Nov 2022 10:33:50 +0800 Subject: [PATCH 16/27] CPU: don't remove `(R)` in CPU name to be consistant with GPU name --- src/detection/cpu/cpu.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/detection/cpu/cpu.c b/src/detection/cpu/cpu.c index 3a304606e5..3bfe92e872 100644 --- a/src/detection/cpu/cpu.c +++ b/src/detection/cpu/cpu.c @@ -31,7 +31,6 @@ static void detectCPU(const FFinstance* instance, FFCPUResult* cpu) return; const char* removeStrings[] = { - "(R)", "(r)", "(TM)", "(tm)", " CPU", " FPU", " APU", " Processor", " Dual-Core", " Quad-Core", " Six-Core", " Eight-Core", " Ten-Core", " 2-Core", " 4-Core", " 6-Core", " 8-Core", " 10-Core", " 12-Core", " 14-Core", " 16-Core", From fb78d9bbb9140ad62a1abf6a349ecdd093e013ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 17 Nov 2022 18:14:14 +0800 Subject: [PATCH 17/27] TerminalFont: support Warp; improve performance of Apple Terminal (macOS) --- README.md | 2 +- .../terminalfont/terminalfont_apple.m | 45 ++++++++++++++----- .../terminalshell/terminalshell_linux.c | 19 ++++++-- 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index c9a0a17764..6a5f0b7703 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ KDE Plasma, Gnome, Cinnamon, Mate, XFCE4, LXQt ##### Terminal fonts ``` -Konsole, Gnome Terminal, Tilix, XFCE4 Terminal, Alacritty, LXTerminal, iTerm2, Apple Terminal, TTY, Windows Terminal, Termux, mintty, ConEmu +Konsole, Gnome Terminal, Tilix, XFCE4 Terminal, Alacritty, LXTerminal, iTerm2, Apple Terminal, Warp, TTY, Windows Terminal, Termux, mintty, ConEmu ``` ## Building diff --git a/src/detection/terminalfont/terminalfont_apple.m b/src/detection/terminalfont/terminalfont_apple.m index fb8c2769b3..72e4c09cce 100644 --- a/src/detection/terminalfont/terminalfont_apple.m +++ b/src/detection/terminalfont/terminalfont_apple.m @@ -22,7 +22,7 @@ static void detectIterm2(const FFinstance* instance, FFTerminalFontResult* termi error:&error]; if(error) { - ffStrbufAppendS(&terminalFont->error, [error localizedDescription].UTF8String); + ffStrbufAppendS(&terminalFont->error, error.localizedDescription.UTF8String); return; } @@ -46,25 +46,44 @@ static void detectIterm2(const FFinstance* instance, FFTerminalFontResult* termi static void detectAppleTerminal(FFTerminalFontResult* terminalFont) { - FFstrbuf fontName; - ffStrbufInit(&fontName); - ffOsascript("tell application \"Terminal\" to font name of window frontmost", &fontName); + FFstrbuf font; + ffStrbufInit(&font); + ffOsascript("tell application \"Terminal\" to font name of window frontmost & \" \" & font size of window frontmost", &font); - if(fontName.length == 0) + if(font.length == 0) { ffStrbufAppendS(&terminalFont->error, "executing osascript failed"); - ffStrbufDestroy(&fontName); + ffStrbufDestroy(&font); return; } - FFstrbuf fontSize; - ffStrbufInit(&fontSize); - ffOsascript("tell application \"Terminal\" to font size of window frontmost", &fontSize); + ffFontInitWithSpace(&terminalFont->font, font.chars); + ffStrbufDestroy(&font); +} + +static void detectWarpTerminal(const FFinstance* instance, FFTerminalFontResult* terminalFont) +{ + NSError* error; + NSString* fileName = [NSString stringWithFormat:@"file://%s/Library/Preferences/dev.warp.Warp-Stable.plist", instance->state.passwd->pw_dir]; + NSDictionary* dict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:fileName] + error:&error]; + if(error) + { + ffStrbufAppendS(&terminalFont->error, error.localizedDescription.UTF8String); + return; + } + + NSString* fontName = [dict valueForKey:@"FontName"]; + if(!fontName) + fontName = @"Hack"; + else + fontName = [fontName stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\""]]; - ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars); + NSString* fontSize = [dict valueForKey:@"FontSize"]; + if(!fontSize) + fontSize = @"13"; - ffStrbufDestroy(&fontName); - ffStrbufDestroy(&fontSize); + ffFontInitValues(&terminalFont->font, fontName.UTF8String, fontSize.UTF8String); } void ffDetectTerminalFontPlatform(const FFinstance* instance, const FFTerminalShellResult* terminalShell, FFTerminalFontResult* terminalFont) @@ -73,4 +92,6 @@ void ffDetectTerminalFontPlatform(const FFinstance* instance, const FFTerminalSh detectIterm2(instance, terminalFont); else if(ffStrbufIgnCaseCompS(&terminalShell->terminalProcessName, "Apple_Terminal") == 0) detectAppleTerminal(terminalFont); + else if(ffStrbufIgnCaseCompS(&terminalShell->terminalProcessName, "WarpTerminal") == 0) + detectWarpTerminal(instance, terminalFont); } diff --git a/src/detection/terminalshell/terminalshell_linux.c b/src/detection/terminalshell/terminalshell_linux.c index 57867fdb60..0dfc40a10a 100644 --- a/src/detection/terminalshell/terminalshell_linux.c +++ b/src/detection/terminalshell/terminalshell_linux.c @@ -180,10 +180,17 @@ static void getTerminalFromEnv(FFTerminalShellResult* result) if( result->terminalProcessName.length > 0 && !ffStrbufStartsWithIgnCaseS(&result->terminalProcessName, "login") && - ffStrbufIgnCaseCompS(&result->terminalProcessName, "(login)") != 0 && - ffStrbufIgnCaseCompS(&result->terminalProcessName, "systemd") != 0 && - ffStrbufIgnCaseCompS(&result->terminalProcessName, "init") != 0 && - ffStrbufIgnCaseCompS(&result->terminalProcessName, "(init)") != 0 && + !ffStrbufIgnCaseEqualS(&result->terminalProcessName, "(login)") && + + #ifdef __APPLE__ + !ffStrbufIgnCaseEqualS(&result->terminalProcessName, "launchd") && + !ffStrbufIgnCaseEqualS(&result->terminalProcessName, "stable") && //for WarpTerminal + #else + !ffStrbufIgnCaseEqualS(&result->terminalProcessName, "systemd") && + !ffStrbufIgnCaseEqualS(&result->terminalProcessName, "init") && + !ffStrbufIgnCaseEqualS(&result->terminalProcessName, "(init)") && + #endif + ffStrbufIgnCaseCompS(&result->terminalProcessName, "0") != 0 ) return; @@ -193,11 +200,13 @@ static void getTerminalFromEnv(FFTerminalShellResult* result) if(getenv("SSH_CONNECTION") != NULL) term = getenv("SSH_TTY"); + #ifdef __linux__ //Windows Terminal if(!ffStrSet(term) && ( getenv("WT_SESSION") != NULL || getenv("WT_PROFILE_ID") != NULL )) term = "Windows Terminal"; + #endif //Alacritty if(!ffStrSet(term) && ( @@ -365,6 +374,8 @@ const FFTerminalShellResult* ffDetectTerminalShell(const FFinstance* instance) ffStrbufInitS(&result.terminalPrettyName, "iTerm"); else if(ffStrbufEqualS(&result.terminalProcessName, "Apple_Terminal")) ffStrbufInitS(&result.terminalPrettyName, "Apple Terminal"); + else if(ffStrbufEqualS(&result.terminalProcessName, "WarpTerminal")) + ffStrbufInitS(&result.terminalPrettyName, "Warp"); else if(strncmp(result.terminalExeName, result.terminalProcessName.chars, result.terminalProcessName.length) == 0) // if exeName starts with processName, print it. Otherwise print processName ffStrbufInitS(&result.terminalPrettyName, result.terminalExeName); else From e8e586ce7ce8f589eb8d095e82bb1001f5c19caa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 13 Nov 2022 00:04:34 +0800 Subject: [PATCH 18/27] Windows / macOS: simplify code 1. simplify resource management by using C++ RAII like GCC attribute extension `cleanup` 2. simplify HEKY reading Actually `__attribute__((__cleanup__(cleanupFn)))` is supported on all major C compiler ( gcc, clang and icc ) except MSVC I only used it on Windows and macOS for now. Let's see if the project author like it. --- src/common/networking_windows.c | 4 +- src/common/processing_windows.c | 41 ++++++++++--------- src/detection/battery/battery_apple.c | 3 +- src/detection/cpu/cpu_apple.c | 3 +- src/detection/font/font_windows.cpp | 4 +- src/detection/gpu/gpu_apple.c | 3 +- src/detection/packages/packages_apple.c | 10 ++--- .../terminalfont/terminalfont_apple.m | 4 +- .../terminalfont/terminalfont_windows.c | 41 ++++++++----------- src/detection/wmtheme/wmtheme_windows.c | 25 ++++++----- src/util/FFlist.h | 4 ++ src/util/FFstrbuf.h | 5 +++ 12 files changed, 72 insertions(+), 75 deletions(-) diff --git a/src/common/networking_windows.c b/src/common/networking_windows.c index dd32fac164..5814acc165 100644 --- a/src/common/networking_windows.c +++ b/src/common/networking_windows.c @@ -75,7 +75,7 @@ bool ffNetworkingSendHttpRequest(FFNetworkingState* state, const char* host, con } } - FFstrbuf command; + FF_STRBUF_AUTO_DESTROY command; ffStrbufInitA(&command, 64); ffStrbufAppendS(&command, "GET "); ffStrbufAppendS(&command, path); @@ -87,7 +87,6 @@ bool ffNetworkingSendHttpRequest(FFNetworkingState* state, const char* host, con BOOL result = ConnectEx(state->sockfd, addr->ai_addr, (int)addr->ai_addrlen, command.chars, command.length, NULL, &state->overlapped); freeaddrinfo(addr); - ffStrbufDestroy(&command); if(!result && WSAGetLastError() != WSA_IO_PENDING) { @@ -95,7 +94,6 @@ bool ffNetworkingSendHttpRequest(FFNetworkingState* state, const char* host, con return false; } - ffStrbufDestroy(&command); return true; } diff --git a/src/common/processing_windows.c b/src/common/processing_windows.c index cd091ac6c3..30e5157343 100644 --- a/src/common/processing_windows.c +++ b/src/common/processing_windows.c @@ -27,27 +27,30 @@ const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[]) .hStdOutput = hChildStdoutWrite, }; - FFstrbuf cmdline; - ffStrbufInitF(&cmdline, "\"%s\"", argv[0]); - for(char* const* parg = &argv[1]; *parg; ++parg) - { - ffStrbufAppendC(&cmdline, ' '); - ffStrbufAppendS(&cmdline, *parg); - } + BOOL success; - BOOL success = CreateProcessA( - NULL, // application name - cmdline.chars, // command line - NULL, // process security attributes - NULL, // primary thread security attributes - TRUE, // handles are inherited - 0, // creation flags - NULL, // use parent's environment - NULL, // use parent's current directory - &siStartInfo, // STARTUPINFO pointer - &piProcInfo); // receives PROCESS_INFORMATION + { + FF_STRBUF_AUTO_DESTROY cmdline; + ffStrbufInitF(&cmdline, "\"%s\"", argv[0]); + for(char* const* parg = &argv[1]; *parg; ++parg) + { + ffStrbufAppendC(&cmdline, ' '); + ffStrbufAppendS(&cmdline, *parg); + } - ffStrbufDestroy(&cmdline); + success = CreateProcessA( + NULL, // application name + cmdline.chars, // command line + NULL, // process security attributes + NULL, // primary thread security attributes + TRUE, // handles are inherited + 0, // creation flags + NULL, // use parent's environment + NULL, // use parent's current directory + &siStartInfo, // STARTUPINFO pointer + &piProcInfo // receives PROCESS_INFORMATION + ); + } CloseHandle(hChildStdoutWrite); if(!success) diff --git a/src/detection/battery/battery_apple.c b/src/detection/battery/battery_apple.c index 88499aaa70..7244a89a15 100644 --- a/src/detection/battery/battery_apple.c +++ b/src/detection/battery/battery_apple.c @@ -7,7 +7,7 @@ static double detectBatteryTemp() { - FFlist temps; + FF_LIST_AUTO_DESTROY temps; ffListInit(&temps, sizeof(FFTempValue)); ffDetectCoreTemps(FF_TEMP_BATTERY, &temps); @@ -25,7 +25,6 @@ static double detectBatteryTemp() ffStrbufDestroy(&tempValue->deviceClass); } result /= temps.length; - ffListDestroy(&temps); return result; } diff --git a/src/detection/cpu/cpu_apple.c b/src/detection/cpu/cpu_apple.c index dc0c1a3ebe..5f1b354948 100644 --- a/src/detection/cpu/cpu_apple.c +++ b/src/detection/cpu/cpu_apple.c @@ -15,7 +15,7 @@ static double getFrequency(const char* propName) static double detectCpuTemp(const FFstrbuf* cpuName) { - FFlist temps; + FF_LIST_AUTO_DESTROY temps; ffListInit(&temps, sizeof(FFTempValue)); if(ffStrbufStartsWithS(cpuName, "Apple M1")) @@ -38,7 +38,6 @@ static double detectCpuTemp(const FFstrbuf* cpuName) ffStrbufDestroy(&tempValue->deviceClass); } result /= temps.length; - ffListDestroy(&temps); return result; } diff --git a/src/detection/font/font_windows.cpp b/src/detection/font/font_windows.cpp index 3d883bd586..fd400fb715 100644 --- a/src/detection/font/font_windows.cpp +++ b/src/detection/font/font_windows.cpp @@ -18,7 +18,7 @@ void ffDetectFontImpl(const FFinstance* instance, FFFontResult* result) if(FFWmiRecord record = query.next()) { - FFstrbuf fontName; + FF_STRBUF_AUTO_DESTROY fontName; ffStrbufInit(&fontName); record.getString(L"IconTitleFaceName", &fontName); @@ -26,8 +26,6 @@ void ffDetectFontImpl(const FFinstance* instance, FFFontResult* result) record.getUnsigned(L"IconTitleSize", &fontSize); ffStrbufAppendF(&result->fonts[0], "%*s (%upt)", fontName.length, fontName.chars, (unsigned)fontSize); - - ffStrbufDestroy(&fontName); } else ffStrbufInitS(&result->error, "No WMI result returned"); diff --git a/src/detection/gpu/gpu_apple.c b/src/detection/gpu/gpu_apple.c index bad6747d3a..52292d83e8 100644 --- a/src/detection/gpu/gpu_apple.c +++ b/src/detection/gpu/gpu_apple.c @@ -8,7 +8,7 @@ static double detectGpuTemp(const FFstrbuf* gpuName) { - FFlist temps; + FF_LIST_AUTO_DESTROY temps; ffListInit(&temps, sizeof(FFTempValue)); if(ffStrbufStartsWithS(gpuName, "Apple M1")) @@ -35,7 +35,6 @@ static double detectGpuTemp(const FFstrbuf* gpuName) ffStrbufDestroy(&tempValue->deviceClass); } result /= temps.length; - ffListDestroy(&temps); return result; } diff --git a/src/detection/packages/packages_apple.c b/src/detection/packages/packages_apple.c index 3966240fdc..e1323e1e55 100644 --- a/src/detection/packages/packages_apple.c +++ b/src/detection/packages/packages_apple.c @@ -27,7 +27,7 @@ static uint32_t getNumElements(const char* dirname, unsigned char type) static uint32_t countBrewPackages(const char* dirname) { - FFstrbuf baseDir; + FF_STRBUF_AUTO_DESTROY baseDir; ffStrbufInitS(&baseDir, dirname); uint32_t result = 0; @@ -41,7 +41,6 @@ static uint32_t countBrewPackages(const char* dirname) result += getNumElements(baseDir.chars, DT_DIR); ffStrbufSubstrBefore(&baseDir, baseDirLength); - ffStrbufDestroy(&baseDir); return result; } @@ -59,14 +58,11 @@ static uint32_t getBrewPackages() static uint32_t countMacPortsPackages(const char* dirname) { - FFstrbuf baseDir; + FF_STRBUF_AUTO_DESTROY baseDir; ffStrbufInitS(&baseDir, dirname); ffStrbufAppendS(&baseDir, "/var/macports/software"); - uint32_t result = getNumElements(baseDir.chars, DT_DIR); - - ffStrbufDestroy(&baseDir); - return result; + return getNumElements(baseDir.chars, DT_DIR); } static uint32_t getMacPortsPackages() diff --git a/src/detection/terminalfont/terminalfont_apple.m b/src/detection/terminalfont/terminalfont_apple.m index 72e4c09cce..1ee387aa27 100644 --- a/src/detection/terminalfont/terminalfont_apple.m +++ b/src/detection/terminalfont/terminalfont_apple.m @@ -46,19 +46,17 @@ static void detectIterm2(const FFinstance* instance, FFTerminalFontResult* termi static void detectAppleTerminal(FFTerminalFontResult* terminalFont) { - FFstrbuf font; + FF_STRBUF_AUTO_DESTROY font; ffStrbufInit(&font); ffOsascript("tell application \"Terminal\" to font name of window frontmost & \" \" & font size of window frontmost", &font); if(font.length == 0) { ffStrbufAppendS(&terminalFont->error, "executing osascript failed"); - ffStrbufDestroy(&font); return; } ffFontInitWithSpace(&terminalFont->font, font.chars); - ffStrbufDestroy(&font); } static void detectWarpTerminal(const FFinstance* instance, FFTerminalFontResult* terminalFont) diff --git a/src/detection/terminalfont/terminalfont_windows.c b/src/detection/terminalfont/terminalfont_windows.c index 7e1d5c8cba..e6ea456be2 100644 --- a/src/detection/terminalfont/terminalfont_windows.c +++ b/src/detection/terminalfont/terminalfont_windows.c @@ -8,10 +8,10 @@ static void detectMintty(const FFinstance* instance, FFTerminalFontResult* terminalFont) { - FFstrbuf fontName; + FF_STRBUF_AUTO_DESTROY fontName; ffStrbufInit(&fontName); - FFstrbuf fontSize; + FF_STRBUF_AUTO_DESTROY fontSize; ffStrbufInit(&fontSize); ffParsePropFileHomeValues(instance, ".minttyrc", 2, (FFpropquery[]) { @@ -24,9 +24,12 @@ static void detectMintty(const FFinstance* instance, FFTerminalFontResult* termi ffStrbufAppendC(&fontSize, '9'); ffFontInitValues(&terminalFont->font, fontName.chars, fontSize.chars); +} - ffStrbufDestroy(&fontName); - ffStrbufDestroy(&fontSize); +static inline void wrapRegCloseKey(HKEY* phKey) +{ + if(*phKey) + RegCloseKey(*phKey); } static void detectConhost(const FFinstance* instance, FFTerminalFontResult* terminalFont) @@ -35,7 +38,7 @@ static void detectConhost(const FFinstance* instance, FFTerminalFontResult* term //Current font of conhost doesn't seem to be detectable, we detect default font instead - HKEY hKey; + HKEY __attribute__((__cleanup__(wrapRegCloseKey))) hKey = NULL; if(RegOpenKeyExW(HKEY_CURRENT_USER, L"Console", 0, KEY_READ, &hKey) != ERROR_SUCCESS) { ffStrbufAppendS(&terminalFont->error, "RegOpenKeyExW() failed"); @@ -44,34 +47,26 @@ static void detectConhost(const FFinstance* instance, FFTerminalFontResult* term DWORD bufSize; - wchar_t fontNameW[64]; - bufSize = sizeof(fontNameW); - if(RegQueryValueExW(hKey, L"FaceName", NULL, NULL, (LPBYTE)fontNameW, &bufSize) != ERROR_SUCCESS) + char fontName[128]; + bufSize = sizeof(fontName); + if(RegGetValueA(hKey, NULL, "FaceName", RRF_RT_REG_SZ, NULL, fontName, &bufSize) != ERROR_SUCCESS) { - ffStrbufAppendS(&terminalFont->error, "RegOpenKeyExW(FaceName) failed"); - goto exit; + ffStrbufAppendS(&terminalFont->error, "RegGetValueA(FaceName) failed"); + return; } - fontNameW[bufSize] = '\0'; - - char fontNameA[128]; - int fontNameALen = WideCharToMultiByte(CP_UTF8, 0, fontNameW, (int)(bufSize / 2), fontNameA, sizeof(fontNameA), NULL, NULL); - fontNameA[fontNameALen] = '\0'; uint32_t fontSizeNum = 0; bufSize = sizeof(fontSizeNum); - if(RegQueryValueExW(hKey, L"fontSize", NULL, NULL, (LPBYTE)&fontSizeNum, &bufSize) != ERROR_SUCCESS) + if(RegGetValueW(hKey, NULL, L"FontSize", RRF_RT_DWORD, NULL, &fontSizeNum, &bufSize) != ERROR_SUCCESS) { - ffStrbufAppendS(&terminalFont->error, "RegOpenKeyExW(fontSize) failed"); - goto exit; + ffStrbufAppendS(&terminalFont->error, "RegGetValueW(FontSize) failed"); + return; } char fontSize[16]; - snprintf(fontSize, sizeof(fontSize), "%u", (fontSizeNum >> 16)); - - ffFontInitValues(&terminalFont->font, fontNameA, fontSize); + _ultoa((unsigned long)(fontSizeNum >> 16), fontSize, 10); -exit: - RegCloseKey(hKey); + ffFontInitValues(&terminalFont->font, fontName, 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 7611f05dae..308893e1fc 100644 --- a/src/detection/wmtheme/wmtheme_windows.c +++ b/src/detection/wmtheme/wmtheme_windows.c @@ -4,38 +4,41 @@ #define WIN32_LEAN_AND_MEAN 1 #include +static inline void wrapRegCloseKey(HKEY* phKey) +{ + if(*phKey) + RegCloseKey(*phKey); +} + bool ffDetectWmTheme(FFinstance* instance, FFstrbuf* themeOrError) { FF_UNUSED(instance); - HKEY hKey; + HKEY __attribute__((__cleanup__(wrapRegCloseKey))) hKey = NULL; if(RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", 0, KEY_READ, &hKey) != ERROR_SUCCESS) { ffStrbufAppendS(themeOrError, "RegOpenKeyExW() failed"); return false; } - bool result = true; int SystemUsesLightTheme = 1; DWORD bufSize = sizeof(SystemUsesLightTheme); - if(RegQueryValueExW(hKey, L"SystemUsesLightTheme", NULL, NULL, (LPBYTE)&SystemUsesLightTheme, &bufSize) != ERROR_SUCCESS) + if(RegGetValueW(hKey, NULL, L"SystemUsesLightTheme", RRF_RT_DWORD, NULL, &SystemUsesLightTheme, &bufSize) != ERROR_SUCCESS) { - ffStrbufAppendS(themeOrError, "RegOpenKeyExW(SystemUsesLightTheme) failed"); - goto exit; + ffStrbufAppendS(themeOrError, "RegGetValueW(SystemUsesLightTheme) failed"); + return false; } int AppsUsesLightTheme = 1; bufSize = sizeof(AppsUsesLightTheme); - if(RegQueryValueExW(hKey, L"AppsUseLightTheme", NULL, NULL, (LPBYTE)&AppsUsesLightTheme, &bufSize) != ERROR_SUCCESS) + if(RegGetValueW(hKey, NULL, L"AppsUseLightTheme", RRF_RT_DWORD, NULL, &AppsUsesLightTheme, &bufSize) != ERROR_SUCCESS) { - ffStrbufAppendS(themeOrError, "RegOpenKeyExW(AppsUseLightTheme) failed"); - goto exit; + ffStrbufAppendS(themeOrError, "RegGetValueW(AppsUseLightTheme) failed"); + return false; } ffStrbufAppendF(themeOrError, "System - %s, Apps - %s", SystemUsesLightTheme ? "Light" : "Dark", AppsUsesLightTheme ? "Light" : "Dark"); -exit: - RegCloseKey(hKey); - return result; + return true; } diff --git a/src/util/FFlist.h b/src/util/FFlist.h index d9d5167922..fd7a0f5ca6 100644 --- a/src/util/FFlist.h +++ b/src/util/FFlist.h @@ -45,4 +45,8 @@ static inline void ffListSort(FFlist* list, int(*compar)(const void*, const void qsort(list->data, list->length, list->elementSize, compar); } +#if defined(_WIN32) || defined(__APPLE__) + #define FF_LIST_AUTO_DESTROY FFlist __attribute__((__cleanup__(ffListDestroy))) +#endif + #endif diff --git a/src/util/FFstrbuf.h b/src/util/FFstrbuf.h index 736e1d26da..80503e55d3 100644 --- a/src/util/FFstrbuf.h +++ b/src/util/FFstrbuf.h @@ -301,4 +301,9 @@ static inline FF_C_NODISCARD bool ffStrbufEndsWithIgnCase(const FFstrbuf* strbuf { return ffStrbufEndsWithIgnCaseNS(strbuf, end->length, end->chars); } + +#if defined(_WIN32) || defined(__APPLE__) + #define FF_STRBUF_AUTO_DESTROY FFstrbuf __attribute__((__cleanup__(ffStrbufDestroy))) +#endif + #endif From fde9379bb285a6d6894deabb37c47a12f428835c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 17 Nov 2022 20:19:27 +0800 Subject: [PATCH 19/27] Android: simplify code; fix mistakes in terminal font detection code --- src/detection/os/os_android.c | 21 +++++++------------ .../terminalfont/terminalfont_android.c | 4 ++-- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/detection/os/os_android.c b/src/detection/os/os_android.c index bb3b6c9daf..eb4bf97d55 100644 --- a/src/detection/os/os_android.c +++ b/src/detection/os/os_android.c @@ -5,14 +5,11 @@ void ffDetectOSImpl(FFOSResult* os, const FFinstance* instance) { FF_UNUSED(instance); - ffStrbufInit(&os->name); - ffStrbufSetS(&os->name, "Android"); + ffStrbufInitS(&os->name, "Android"); - ffStrbufInit(&os->prettyName); - ffStrbufSetS(&os->prettyName, "Android"); + ffStrbufInitS(&os->prettyName, "Android"); - ffStrbufInit(&os->id); - ffStrbufSetS(&os->id, "android"); + ffStrbufInitS(&os->id, "android"); ffStrbufInit(&os->version); ffSettingsGetAndroidProperty("ro.build.version.release", &os->version); @@ -26,13 +23,11 @@ void ffDetectOSImpl(FFOSResult* os, const FFinstance* instance) ffStrbufInit(&os->buildID); ffSettingsGetAndroidProperty("ro.build.id", &os->buildID); - ffStrbufInit(&os->systemName); - ffStrbufSetS(&os->systemName, instance->state.utsname.sysname); + ffStrbufInitS(&os->systemName, instance->state.utsname.sysname); - ffStrbufInit(&os->architecture); - ffStrbufSetS(&os->architecture, instance->state.utsname.machine); + ffStrbufInitS(&os->architecture, instance->state.utsname.machine); - ffStrbufInitA(&os->idLike, 0); - ffStrbufInitA(&os->variant, 0); - ffStrbufInitA(&os->variantID, 0); + ffStrbufInit(&os->idLike); + ffStrbufInit(&os->variant); + ffStrbufInit(&os->variantID); } diff --git a/src/detection/terminalfont/terminalfont_android.c b/src/detection/terminalfont/terminalfont_android.c index 8dd1ff2fe3..eaa4fa52d8 100644 --- a/src/detection/terminalfont/terminalfont_android.c +++ b/src/detection/terminalfont/terminalfont_android.c @@ -31,7 +31,7 @@ const char* detectTermux(const FFinstance* instance, FFTerminalFontResult* termi goto exit; } - if(ffFT_New_Face(library, FF_TERMUX_FONT_PATH, 0, &face )) + if(ffFT_New_Face(library, FF_TERMUX_FONT_PATH, 0, &face)) { error = "FT_NEW_Face(" FF_TERMUX_FONT_PATH ") failed"; goto exit; @@ -49,7 +49,7 @@ const char* detectTermux(const FFinstance* instance, FFTerminalFontResult* termi #else FF_UNUSED(terminalFont); - ffStrbufSetS(&terminalFont->error, "fastfetch is built without freetype2 support"); + return "fastfetch is built without freetype2 support"; #endif } From 2a349784878e03f1a4e31af5414af990f0ef3032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 17 Nov 2022 00:13:25 +0800 Subject: [PATCH 20/27] Thread: silence compiler warnings --- src/common/networking_linux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/networking_linux.c b/src/common/networking_linux.c index 5d656fddd7..7fb6fade55 100644 --- a/src/common/networking_linux.c +++ b/src/common/networking_linux.c @@ -67,7 +67,7 @@ bool ffNetworkingSendHttpRequest(FFNetworkingState* state, const char* host, con #ifdef FF_HAVE_THREADS state->thread = ffThreadCreate(connectAndSendThreadMain, state); - return state->thread != NULL; + return !!state->thread; #else connectAndSend(state); return state->sockfd != -1; From 70847857f75a355411df344c00a548a621f238f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 17 Nov 2022 22:34:58 +0800 Subject: [PATCH 21/27] GPU: remove special handling for WSL WSL2 is VM, which can only provide virtual GPUs, like other VMs do. We should honor it. As of WSL 1.0, it prints `Microsoft Corporation Basic Render Driver` --- src/modules/gpu.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/modules/gpu.c b/src/modules/gpu.c index 11507191a4..905fd9774a 100644 --- a/src/modules/gpu.c +++ b/src/modules/gpu.c @@ -44,12 +44,6 @@ void ffPrintGPU(FFinstance* instance) if(ffPrintFromCache(instance, FF_GPU_MODULE_NAME, &instance->config.gpu, FF_GPU_NUM_FORMAT_ARGS)) return; - if(ffStrbufCompS(&ffDetectHost()->productName, FF_HOST_PRODUCT_NAME_WSL) == 0) - { - ffPrintError(instance, FF_GPU_MODULE_NAME, 0, &instance->config.gpu, "WSL doesn't expose senseful GPU names"); - return; - } - const FFlist* gpus = ffDetectGPU(instance); if(gpus->length == 0) From f1cc94207efd389277c60947bc1cef9e04aeb310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 17 Nov 2022 22:35:40 +0800 Subject: [PATCH 22/27] Host: detect WSL version --- src/detection/host/host.h | 3 --- src/detection/host/host_linux.c | 24 ++++++++++++++++++- .../terminalshell/terminalshell_linux.c | 7 ++---- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/detection/host/host.h b/src/detection/host/host.h index e66af41fb6..e49e8faadc 100644 --- a/src/detection/host/host.h +++ b/src/detection/host/host.h @@ -5,9 +5,6 @@ #include "fastfetch.h" -#define FF_HOST_PRODUCT_NAME_WSL "Windows Subsystem for Linux" -#define FF_HOST_PRODUCT_NAME_MSYS "Windows on MSYS" - typedef struct FFHostResult { FFstrbuf productFamily; diff --git a/src/detection/host/host_linux.c b/src/detection/host/host_linux.c index ef9cfbf5e3..4833131036 100644 --- a/src/detection/host/host_linux.c +++ b/src/detection/host/host_linux.c @@ -1,5 +1,6 @@ #include "host.h" #include "common/io.h" +#include "common/processing.h" #include @@ -94,6 +95,27 @@ void ffDetectHostImpl(FFHostResult* host) { //On WSL, the real host can't be detected. Instead use WSL as host. if(getenv("WSL_DISTRO") != NULL || getenv("WSL_INTEROP") != NULL) - ffStrbufAppendS(&host->productName, FF_HOST_PRODUCT_NAME_WSL); + { + ffStrbufAppendS(&host->productName, "Windows Subsystem for Linux"); + + FFstrbuf wslVer; //Wide charactors + ffStrbufInit(&wslVer); + if(!ffProcessAppendStdOut(&wslVer, (char* const[]){ + "wsl.exe", + "--version", + NULL + }) && wslVer.length > 0) + { + ffStrbufSubstrBeforeFirstC(&wslVer, '\r'); //CRLF + ffStrbufSubstrAfterLastC(&wslVer, ' '); + ffStrbufAppendS(&host->productName, " ("); + for(uint32_t i = 0; i < wslVer.length; ++i) { + if(wslVer.chars[i]) //don't append \0 + ffStrbufAppendC(&host->productName, wslVer.chars[i]); + } + ffStrbufAppendC(&host->productName, ')'); + } + ffStrbufDestroy(&wslVer); + } } } diff --git a/src/detection/terminalshell/terminalshell_linux.c b/src/detection/terminalshell/terminalshell_linux.c index 0dfc40a10a..2c3e7319de 100644 --- a/src/detection/terminalshell/terminalshell_linux.c +++ b/src/detection/terminalshell/terminalshell_linux.c @@ -1,5 +1,4 @@ #include "fastfetch.h" -#include "detection/host/host.h" #include "common/io.h" #include "common/parsing.h" #include "common/processing.h" @@ -239,10 +238,8 @@ static void getTerminalFromEnv(FFTerminalShellResult* result) if(!ffStrSet(term)) { //We are in WSL but not in Windows Terminal - const FFHostResult* host = ffDetectHost(); - if(ffStrbufCompS(&host->productName, FF_HOST_PRODUCT_NAME_WSL) == 0 || - ffStrbufCompS(&host->productName, FF_HOST_PRODUCT_NAME_MSYS) == 0) //TODO better WSL or MSYS detection - term = "conhost"; + if(getenv("WSL_DISTRO") != NULL || getenv("WSL_INTEROP") != NULL) + term = "conhost"; } #endif From 859f645fe59ac3915046a0c47c45d95333167434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 18 Nov 2022 00:10:16 +0800 Subject: [PATCH 23/27] Disk: fix size detection on 32bit Linux Ref: #337 --- src/detection/disk/disk_linux.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index 93dee0f7d3..941c779573 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -73,9 +73,9 @@ void ffDetectDisksImpl(FFDiskResult* disks) #endif //Detects stats - struct statvfs fs; - if(statvfs(disk->mountpoint.chars, &fs) != 0) - memset(&fs, 0, sizeof(struct statvfs)); //Set all values to 0, so our values get initialized to 0 too + struct statvfs64 fs; + if(statvfs64(disk->mountpoint.chars, &fs) != 0) + memset(&fs, 0, sizeof(struct statvfs64)); //Set all values to 0, so our values get initialized to 0 too disk->bytesTotal = fs.f_blocks * fs.f_frsize; disk->bytesUsed = disk->bytesTotal - (fs.f_bavail * fs.f_frsize); From 0a658679377427fb722c73b458ac5b75cf803f28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 20 Nov 2022 00:52:52 +0800 Subject: [PATCH 24/27] Windows: fix memleaks --- src/detection/host/host_windows.cpp | 2 +- src/util/windows/wmi.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/detection/host/host_windows.cpp b/src/detection/host/host_windows.cpp index 5d0333db85..cbdd440ad1 100644 --- a/src/detection/host/host_windows.cpp +++ b/src/detection/host/host_windows.cpp @@ -28,5 +28,5 @@ extern "C" void ffDetectHostImpl(FFHostResult* host) record.getString(L"Vendor", &host->sysVendor); } else - ffStrbufInitS(&host->error, "No Wmi result returned"); + ffStrbufAppendS(&host->error, "No Wmi result returned"); } diff --git a/src/util/windows/wmi.cpp b/src/util/windows/wmi.cpp index 6a273f86a4..cba0c07c55 100644 --- a/src/util/windows/wmi.cpp +++ b/src/util/windows/wmi.cpp @@ -128,7 +128,7 @@ FFWmiQuery::FFWmiQuery(const wchar_t* queryStr, FFstrbuf* error) if (InitOnceExecuteOnce(&s_InitOnce, &InitHandleFunction, nullptr, (void**)&context) == FALSE) { if(error) - ffStrbufInitS(error, context); + ffStrbufAppendS(error, context); return; } From 55c6374949329b168e456e5a541801098da6f6da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 20 Nov 2022 00:53:14 +0800 Subject: [PATCH 25/27] WmTheme: support Windows 7 --- src/detection/wmtheme/wmtheme_windows.c | 62 ++++++++++++++++++------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/src/detection/wmtheme/wmtheme_windows.c b/src/detection/wmtheme/wmtheme_windows.c index 308893e1fc..00303300d4 100644 --- a/src/detection/wmtheme/wmtheme_windows.c +++ b/src/detection/wmtheme/wmtheme_windows.c @@ -15,30 +15,56 @@ 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) + if(RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", 0, KEY_READ, &hKey) == ERROR_SUCCESS) { - ffStrbufAppendS(themeOrError, "RegOpenKeyExW() failed"); - return false; - } + int SystemUsesLightTheme = 1; + DWORD bufSize = sizeof(SystemUsesLightTheme); - 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"); + return false; + } - if(RegGetValueW(hKey, NULL, L"SystemUsesLightTheme", RRF_RT_DWORD, NULL, &SystemUsesLightTheme, &bufSize) != ERROR_SUCCESS) - { - ffStrbufAppendS(themeOrError, "RegGetValueW(SystemUsesLightTheme) failed"); - 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"); + 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) + { + DWORD length = 0; + if(RegGetValueA(hKey, NULL, "CurrentTheme", RRF_RT_REG_SZ, NULL, NULL, &length) != ERROR_SUCCESS) + { + ffStrbufAppendS(themeOrError, "RegGetValueA(CurrentTheme, NULL) failed"); + 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])) + themeOrError->chars[0] = (char)toupper(themeOrError->chars[0]); - int AppsUsesLightTheme = 1; - bufSize = sizeof(AppsUsesLightTheme); - if(RegGetValueW(hKey, NULL, L"AppsUseLightTheme", RRF_RT_DWORD, NULL, &AppsUsesLightTheme, &bufSize) != ERROR_SUCCESS) + return true; + } + else { - ffStrbufAppendS(themeOrError, "RegGetValueW(AppsUseLightTheme) failed"); + ffStrbufAppendS(themeOrError, "Failed to find current theme"); return false; } - - ffStrbufAppendF(themeOrError, "System - %s, Apps - %s", SystemUsesLightTheme ? "Light" : "Dark", AppsUsesLightTheme ? "Light" : "Dark"); - - return true; } From b6b95581cd1db5fd27b339468a3deb100e5a512e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 20 Nov 2022 02:18:06 +0800 Subject: [PATCH 26/27] Windows: partially revert a5306d60eec383f0c697bd16ac308254dc1dce45 to fix crashes on Windows 7 Very strange behavior. Needs further investigation --- src/common/init.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/common/init.c b/src/common/init.c index 8a424a4832..e8dc449401 100644 --- a/src/common/init.c +++ b/src/common/init.c @@ -125,6 +125,7 @@ static void initState(FFstate* state) #ifdef WIN32 //https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale?source=recommendations&view=msvc-170#utf-8-support setlocale(LC_ALL, ".UTF8"); + setvbuf(stdout, NULL, _IOFBF, 4096); #endif state->logoWidth = 0; @@ -334,7 +335,7 @@ static void resetConsole() BOOL WINAPI consoleHandler(DWORD signal) { FF_UNUSED(signal); - resetConsole(); + resetConsole(); exit(0); } #else @@ -360,7 +361,7 @@ void ffStart(FFinstance* instance) DWORD mode = 0; GetConsoleMode(hStdout, &mode); SetConsoleMode(hStdout, mode | ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING); - SetConsoleOutputCP(CP_UTF8); + // SetConsoleOutputCP(CP_UTF8); #else struct sigaction action = { .sa_handler = exitSignalHandler }; sigaction(SIGINT, &action, NULL); From 1c69d6c78836dbd961c703e2f9df6104be4295f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 20 Nov 2022 19:43:02 +0800 Subject: [PATCH 27/27] Disk: use statvfs64 only when __USE_LARGEFILE64 is defined --- src/detection/disk/disk_linux.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/detection/disk/disk_linux.c b/src/detection/disk/disk_linux.c index 941c779573..8abfbdf20e 100644 --- a/src/detection/disk/disk_linux.c +++ b/src/detection/disk/disk_linux.c @@ -73,9 +73,15 @@ void ffDetectDisksImpl(FFDiskResult* disks) #endif //Detects stats - struct statvfs64 fs; - if(statvfs64(disk->mountpoint.chars, &fs) != 0) - memset(&fs, 0, sizeof(struct statvfs64)); //Set all values to 0, so our values get initialized to 0 too + #ifdef __USE_LARGEFILE64 + struct statvfs64 fs; + if(statvfs64(disk->mountpoint.chars, &fs) != 0) + memset(&fs, 0, sizeof(struct statvfs64)); //Set all values to 0, so our values get initialized to 0 too + #else + struct statvfs fs; + if(statvfs(disk->mountpoint.chars, &fs) != 0) + memset(&fs, 0, sizeof(struct statvfs)); //Set all values to 0, so our values get initialized to 0 too + #endif disk->bytesTotal = fs.f_blocks * fs.f_frsize; disk->bytesUsed = disk->bytesTotal - (fs.f_bavail * fs.f_frsize);