mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-31 10:02:49 +01:00
Introduce FFPlatform
This commit is contained in:
+7
-181
@@ -18,186 +18,13 @@
|
||||
#include <signal.h>
|
||||
#endif
|
||||
|
||||
#define FF_ENSURE_ONLY_ONCE_IN_LIST(list, element) \
|
||||
if(ffListFirstIndexComp(list, element, (bool(*)(const void*, const void*))ffStrbufEqual) < list->length - 1) \
|
||||
{ \
|
||||
ffStrbufDestroy(ffListGet(list, list->length - 1)); \
|
||||
--list->length; \
|
||||
}
|
||||
|
||||
static void pathsAddHome(FFlist* dirs, FFstate* state, const char* suffix)
|
||||
{
|
||||
FFstrbuf* buffer = (FFstrbuf*) ffListAdd(dirs);
|
||||
ffStrbufInitA(buffer, 64);
|
||||
ffStrbufAppendS(buffer, state->passwd->pw_dir);
|
||||
ffStrbufAppendS(buffer, suffix);
|
||||
ffStrbufEnsureEndsWithC(buffer, '/');
|
||||
FF_ENSURE_ONLY_ONCE_IN_LIST(dirs, buffer);
|
||||
}
|
||||
|
||||
static void pathsAddAbsolute(FFlist* dirs, const char* path)
|
||||
{
|
||||
FFstrbuf* buffer = (FFstrbuf*) ffListAdd(dirs);
|
||||
ffStrbufInitA(buffer, 64);
|
||||
ffStrbufAppendS(buffer, path);
|
||||
ffStrbufEnsureEndsWithC(buffer, '/');
|
||||
FF_ENSURE_ONLY_ONCE_IN_LIST(dirs, buffer);
|
||||
}
|
||||
|
||||
static void pathsAddEnv(FFlist* dirs, const char* env)
|
||||
{
|
||||
const char* envValue = getenv(env);
|
||||
if(!ffStrSet(envValue))
|
||||
return;
|
||||
|
||||
FFstrbuf value;
|
||||
ffStrbufInitA(&value, 64);
|
||||
ffStrbufAppendS(&value, envValue);
|
||||
|
||||
uint32_t startIndex = 0;
|
||||
while (startIndex < value.length)
|
||||
{
|
||||
uint32_t colonIndex = ffStrbufNextIndexC(&value, startIndex, ':');
|
||||
value.chars[colonIndex] = '\0';
|
||||
|
||||
if(!ffStrSet(value.chars + startIndex))
|
||||
{
|
||||
startIndex = colonIndex + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
pathsAddAbsolute(dirs, value.chars + startIndex);
|
||||
|
||||
startIndex = colonIndex + 1;
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&value);
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
static void pathsAddKnownFolder(FFlist* dirs, REFKNOWNFOLDERID folderId)
|
||||
{
|
||||
PWSTR pPath;
|
||||
if(SUCCEEDED(SHGetKnownFolderPath(folderId, 0, NULL, &pPath)))
|
||||
{
|
||||
FFstrbuf* buffer = (FFstrbuf*) ffListAdd(dirs);
|
||||
ffStrbufInit(buffer);
|
||||
ffStrbufSetWS(buffer, pPath);
|
||||
ffStrbufReplaceAllC(buffer, '\\', '/');
|
||||
ffStrbufEnsureEndsWithC(buffer, '/');
|
||||
FF_ENSURE_ONLY_ONCE_IN_LIST(dirs, buffer);
|
||||
}
|
||||
CoTaskMemFree(pPath);
|
||||
}
|
||||
|
||||
static void pathsAddEnvSuffix(FFlist* dirs, const char* env, const char* suffix)
|
||||
{
|
||||
const char* value = getenv(env);
|
||||
if(!ffStrSet(value))
|
||||
return;
|
||||
|
||||
FFstrbuf* buffer = ffListAdd(dirs);
|
||||
ffStrbufInitA(buffer, 64);
|
||||
ffStrbufAppendS(buffer, value);
|
||||
ffStrbufReplaceAllC(buffer, '\\', '/');
|
||||
ffStrbufEnsureEndsWithC(buffer, '/');
|
||||
ffStrbufAppendS(buffer, suffix);
|
||||
ffStrbufEnsureEndsWithC(buffer, '/');
|
||||
FF_ENSURE_ONLY_ONCE_IN_LIST(dirs, buffer);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void initConfigDirs(FFstate* state)
|
||||
{
|
||||
ffListInit(&state->configDirs, sizeof(FFstrbuf));
|
||||
|
||||
pathsAddEnv(&state->configDirs, "XDG_CONFIG_HOME"); // On systems where this is not set (Windows & Apple presumably), this wil do nothing
|
||||
pathsAddHome(&state->configDirs, state, "/.config/");
|
||||
|
||||
#ifdef _WIN32
|
||||
pathsAddKnownFolder(&state->configDirs, &FOLDERID_RoamingAppData);
|
||||
pathsAddKnownFolder(&state->configDirs, &FOLDERID_LocalAppData);
|
||||
#elif defined(__APPLE__)
|
||||
pathsAddHome(&state->configDirs, state, "/Library/Preferences/");
|
||||
#endif
|
||||
|
||||
pathsAddHome(&state->configDirs, state, "");
|
||||
|
||||
#ifdef _WIN32
|
||||
if(getenv("MSYSTEM") && getenv("HOME"))
|
||||
{
|
||||
// We are in MSYS2 / Git Bash
|
||||
pathsAddEnvSuffix(&state->configDirs, "HOME", ".config/");
|
||||
pathsAddEnvSuffix(&state->configDirs, "HOME", "");
|
||||
}
|
||||
#endif
|
||||
|
||||
pathsAddEnv(&state->configDirs, "XDG_CONFIG_DIRS");
|
||||
|
||||
#if !defined(_WIN32) && !defined(__APPLE__)
|
||||
pathsAddAbsolute(&state->configDirs, FASTFETCH_TARGET_DIR_ETC"/xdg/");
|
||||
#endif
|
||||
|
||||
#if !defined(_WIN32)
|
||||
pathsAddAbsolute(&state->configDirs, FASTFETCH_TARGET_DIR_ETC"/");
|
||||
pathsAddAbsolute(&state->configDirs, FASTFETCH_TARGET_DIR_INSTALL_SYSCONF"/");
|
||||
#endif
|
||||
}
|
||||
|
||||
static void initDataDirs(FFstate* state)
|
||||
{
|
||||
ffListInit(&state->dataDirs, sizeof(FFstrbuf));
|
||||
|
||||
pathsAddEnv(&state->dataDirs, "XDG_DATA_HOME"); // On systems where this is not set (Windows & Apple presumably), this wil do nothing
|
||||
pathsAddHome(&state->dataDirs, state, "/.local/share/");
|
||||
|
||||
#ifdef _WIN32
|
||||
pathsAddKnownFolder(&state->dataDirs, &FOLDERID_RoamingAppData);
|
||||
pathsAddKnownFolder(&state->dataDirs, &FOLDERID_LocalAppData);
|
||||
#elif defined(__APPLE__)
|
||||
pathsAddHome(&state->dataDirs, state, "/Library/Application Support/");
|
||||
#endif
|
||||
|
||||
pathsAddHome(&state->dataDirs, state, "");
|
||||
|
||||
#ifdef _WIN32
|
||||
if(getenv("MSYSTEM") && getenv("HOME"))
|
||||
{
|
||||
// We are in MSYS2 / Git Bash
|
||||
pathsAddEnvSuffix(&state->dataDirs, "HOME", ".local/share/");
|
||||
pathsAddEnvSuffix(&state->dataDirs, "HOME", "");
|
||||
}
|
||||
#endif
|
||||
|
||||
pathsAddEnv(&state->dataDirs, "XDG_DATA_DIRS");
|
||||
|
||||
#if !defined(_WIN32)
|
||||
pathsAddAbsolute(&state->dataDirs, FASTFETCH_TARGET_DIR_USR"/local/share/");
|
||||
pathsAddAbsolute(&state->dataDirs, FASTFETCH_TARGET_DIR_USR"/share/");
|
||||
#endif
|
||||
}
|
||||
|
||||
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");
|
||||
#endif
|
||||
|
||||
state->logoWidth = 0;
|
||||
state->logoHeight = 0;
|
||||
state->keysHeight = 0;
|
||||
#ifndef WIN32
|
||||
state->passwd = getpwuid(getuid());
|
||||
#else
|
||||
state->passwd = ffGetPasswd();
|
||||
#endif
|
||||
|
||||
if(uname(&state->utsname) != 0)
|
||||
memset(&state->utsname, 0, sizeof(struct utsname));
|
||||
|
||||
initConfigDirs(state);
|
||||
initDataDirs(state);
|
||||
ffPlatformInit(&state->platform);
|
||||
}
|
||||
|
||||
static void initModuleArg(FFModuleArgs* args)
|
||||
@@ -366,6 +193,11 @@ static void defaultConfig(FFinstance* instance)
|
||||
|
||||
void ffInitInstance(FFinstance* instance)
|
||||
{
|
||||
#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");
|
||||
#endif
|
||||
|
||||
initState(&instance->state);
|
||||
defaultConfig(instance);
|
||||
}
|
||||
@@ -588,13 +420,7 @@ static void destroyConfig(FFinstance* instance)
|
||||
|
||||
static void destroyState(FFinstance* instance)
|
||||
{
|
||||
FF_LIST_FOR_EACH(FFstrbuf, configDir, instance->state.configDirs)
|
||||
ffStrbufDestroy(configDir);
|
||||
ffListDestroy(&instance->state.configDirs);
|
||||
|
||||
FF_LIST_FOR_EACH(FFstrbuf, dataDir, instance->state.dataDirs)
|
||||
ffStrbufDestroy(dataDir);
|
||||
ffListDestroy(&instance->state.dataDirs);
|
||||
ffPlatformDestroy(&instance->state.platform);
|
||||
}
|
||||
|
||||
void ffDestroyInstance(FFinstance* instance)
|
||||
|
||||
@@ -3,17 +3,6 @@
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
bool ffStrSet(const char* str)
|
||||
{
|
||||
if(str == NULL)
|
||||
return false;
|
||||
|
||||
while(isspace(*str))
|
||||
str++;
|
||||
|
||||
return *str != '\0';
|
||||
}
|
||||
|
||||
void ffParseSemver(FFstrbuf* buffer, const FFstrbuf* major, const FFstrbuf* minor, const FFstrbuf* patch)
|
||||
{
|
||||
if(major->length > 0)
|
||||
|
||||
@@ -14,7 +14,6 @@ typedef struct FFVersion
|
||||
|
||||
#define FF_VERSION_INIT ((FFVersion) {0})
|
||||
|
||||
bool ffStrSet(const char* str);
|
||||
void ffParseSemver(FFstrbuf* buffer, const FFstrbuf* major, const FFstrbuf* minor, const FFstrbuf* patch);
|
||||
void ffParseGTK(FFstrbuf* buffer, const FFstrbuf* gtk2, const FFstrbuf* gtk3, const FFstrbuf* gtk4);
|
||||
|
||||
|
||||
@@ -148,7 +148,7 @@ bool ffParsePropFileHomeValues(const FFinstance* instance, const char* relativeF
|
||||
{
|
||||
FFstrbuf absolutePath;
|
||||
ffStrbufInitA(&absolutePath, 64);
|
||||
ffStrbufAppendS(&absolutePath, instance->state.passwd->pw_dir);
|
||||
ffStrbufAppend(&absolutePath, &instance->state.platform.homeDir);
|
||||
ffStrbufAppendC(&absolutePath, '/');
|
||||
ffStrbufAppendS(&absolutePath, relativeFile);
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ static inline bool ffParsePropFileList(const FFlist* list, const char* relativeF
|
||||
|
||||
static inline bool ffParsePropFileConfigValues(const FFinstance* instance, const char* relativeFile, uint32_t numQueries, FFpropquery* queries)
|
||||
{
|
||||
return ffParsePropFileListValues(&instance->state.configDirs, relativeFile, numQueries, queries);
|
||||
return ffParsePropFileListValues(&instance->state.platform.configDirs, relativeFile, numQueries, queries);
|
||||
}
|
||||
|
||||
static inline bool ffParsePropFileConfig(const FFinstance* instance, const char* relativeFile, const char* start, FFstrbuf* buffer)
|
||||
@@ -44,7 +44,7 @@ static inline bool ffParsePropFileConfig(const FFinstance* instance, const char*
|
||||
|
||||
static inline bool ffParsePropFileDataValues(const FFinstance* instance, const char* relativeFile, uint32_t numQueries, FFpropquery* queries)
|
||||
{
|
||||
return ffParsePropFileListValues(&instance->state.dataDirs, relativeFile, numQueries, queries);
|
||||
return ffParsePropFileListValues(&instance->state.platform.dataDirs, relativeFile, numQueries, queries);
|
||||
}
|
||||
|
||||
static inline bool ffParsePropFileData(const FFinstance* instance, const char* relativeFile, const char* start, FFstrbuf* buffer)
|
||||
|
||||
Reference in New Issue
Block a user