mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-29 08:13:44 +01:00
better result handling of calculation functions
This commit is contained in:
@@ -4,34 +4,34 @@
|
||||
#include <pthread.h>
|
||||
#include <dconf/client/dconf-client.h>
|
||||
|
||||
static inline bool allPropertiesSet(FFgtkval* gtkval)
|
||||
static inline bool allPropertiesSet(FFGTKResult* result)
|
||||
{
|
||||
return
|
||||
gtkval->theme.length > 0 &&
|
||||
gtkval->icons.length > 0 &&
|
||||
gtkval->font.length > 0;
|
||||
result->theme.length > 0 &&
|
||||
result->icons.length > 0 &&
|
||||
result->font.length > 0;
|
||||
}
|
||||
|
||||
static inline void initGtkval(FFgtkval* gtkval)
|
||||
static inline void initresult(FFGTKResult* result)
|
||||
{
|
||||
ffStrbufInit(>kval->theme);
|
||||
ffStrbufInit(>kval->icons);
|
||||
ffStrbufInit(>kval->font);
|
||||
ffStrbufInit(&result->theme);
|
||||
ffStrbufInit(&result->icons);
|
||||
ffStrbufInit(&result->font);
|
||||
}
|
||||
|
||||
static inline void applyGTKDConfSettings(FFgtkval* gtkval, const gchar* themeName, const gchar* iconsName, const gchar* fontName)
|
||||
static inline void applyGTKDConfSettings(FFGTKResult* result, const gchar* themeName, const gchar* iconsName, const gchar* fontName)
|
||||
{
|
||||
if(gtkval->theme.length == 0)
|
||||
ffStrbufAppendS(>kval->theme, themeName);
|
||||
if(result->theme.length == 0)
|
||||
ffStrbufAppendS(&result->theme, themeName);
|
||||
|
||||
if(gtkval->icons.length == 0)
|
||||
ffStrbufAppendS(>kval->icons, iconsName);
|
||||
if(result->icons.length == 0)
|
||||
ffStrbufAppendS(&result->icons, iconsName);
|
||||
|
||||
if(gtkval->font.length == 0)
|
||||
ffStrbufAppendS(>kval->font, fontName);
|
||||
if(result->font.length == 0)
|
||||
ffStrbufAppendS(&result->font, fontName);
|
||||
}
|
||||
|
||||
static void parseGTKDConfSettings(FFinstance* instance, FFgtkval* gtkval)
|
||||
static void parseGTKDConfSettings(FFinstance* instance, FFGTKResult* result)
|
||||
{
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
@@ -45,7 +45,7 @@ static void parseGTKDConfSettings(FFinstance* instance, FFgtkval* gtkval)
|
||||
|
||||
if(init)
|
||||
{
|
||||
applyGTKDConfSettings(gtkval, themeName, iconsName, fontName);
|
||||
applyGTKDConfSettings(result, themeName, iconsName, fontName);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return;
|
||||
}
|
||||
@@ -102,11 +102,11 @@ static void parseGTKDConfSettings(FFinstance* instance, FFgtkval* gtkval)
|
||||
|
||||
dlclose(dconf);
|
||||
|
||||
applyGTKDConfSettings(gtkval, themeName, iconsName, fontName);
|
||||
applyGTKDConfSettings(result, themeName, iconsName, fontName);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
}
|
||||
|
||||
static void parseGTKConfigFile(FFstrbuf* fileName, FFgtkval* gtkval)
|
||||
static void parseGTKConfigFile(FFstrbuf* fileName, FFGTKResult* result)
|
||||
{
|
||||
FILE* file = fopen(fileName->chars, "r");
|
||||
if(file == NULL)
|
||||
@@ -117,22 +117,22 @@ static void parseGTKConfigFile(FFstrbuf* fileName, FFgtkval* gtkval)
|
||||
|
||||
while(getline(&line, &len, file) != -1)
|
||||
{
|
||||
if(gtkval->theme.length == 0)
|
||||
if(result->theme.length == 0)
|
||||
{
|
||||
sscanf(line, "gtk-theme-name=%[^\n]", gtkval->theme.chars);
|
||||
sscanf(line, "gtk-theme-name=\"%[^\"]+", gtkval->theme.chars);
|
||||
sscanf(line, "gtk-theme-name=%[^\n]", result->theme.chars);
|
||||
sscanf(line, "gtk-theme-name=\"%[^\"]+", result->theme.chars);
|
||||
}
|
||||
|
||||
if(gtkval->icons.length == 0)
|
||||
if(result->icons.length == 0)
|
||||
{
|
||||
sscanf(line, "gtk-icon-theme-name=%[^\n]", gtkval->icons.chars);
|
||||
sscanf(line, "gtk-icon-theme-name=\"%[^\"]+", gtkval->icons.chars);
|
||||
sscanf(line, "gtk-icon-theme-name=%[^\n]", result->icons.chars);
|
||||
sscanf(line, "gtk-icon-theme-name=\"%[^\"]+", result->icons.chars);
|
||||
}
|
||||
|
||||
if(gtkval->font.length == 0)
|
||||
if(result->font.length == 0)
|
||||
{
|
||||
sscanf(line, "gtk-font-name=%[^\n]", gtkval->font.chars);
|
||||
sscanf(line, "gtk-font-name=\"%[^\"]+", gtkval->font.chars);
|
||||
sscanf(line, "gtk-font-name=%[^\n]", result->font.chars);
|
||||
sscanf(line, "gtk-font-name=\"%[^\"]+", result->font.chars);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,12 +141,12 @@ static void parseGTKConfigFile(FFstrbuf* fileName, FFgtkval* gtkval)
|
||||
|
||||
fclose(file);
|
||||
|
||||
ffStrbufRecalculateLength(>kval->theme);
|
||||
ffStrbufRecalculateLength(>kval->icons);
|
||||
ffStrbufRecalculateLength(>kval->font);
|
||||
ffStrbufRecalculateLength(&result->theme);
|
||||
ffStrbufRecalculateLength(&result->icons);
|
||||
ffStrbufRecalculateLength(&result->font);
|
||||
}
|
||||
|
||||
static void calculateGTKFromConfigDir(const char* configDir, const char* version, FFgtkval* gtkval)
|
||||
static void calculateGTKFromConfigDir(const char* configDir, const char* version, FFGTKResult* result)
|
||||
{
|
||||
// <configdir>/gtk-<version>.0/settings.ini
|
||||
FFstrbuf file1;
|
||||
@@ -155,9 +155,9 @@ static void calculateGTKFromConfigDir(const char* configDir, const char* version
|
||||
ffStrbufAppendS(&file1, "/gtk-");
|
||||
ffStrbufAppendS(&file1, version);
|
||||
ffStrbufAppendS(&file1, ".0/settings.ini");
|
||||
parseGTKConfigFile(&file1, gtkval);
|
||||
parseGTKConfigFile(&file1, result);
|
||||
ffStrbufDestroy(&file1);
|
||||
if(allPropertiesSet(gtkval))
|
||||
if(allPropertiesSet(result))
|
||||
return;
|
||||
|
||||
// <configdir>/gtk-<version>.0/gtkrc
|
||||
@@ -167,9 +167,9 @@ static void calculateGTKFromConfigDir(const char* configDir, const char* version
|
||||
ffStrbufAppendS(&file2, "/gtk-");
|
||||
ffStrbufAppendS(&file2, version);
|
||||
ffStrbufAppendS(&file2, ".0/gtkrc");
|
||||
parseGTKConfigFile(&file2, gtkval);
|
||||
parseGTKConfigFile(&file2, result);
|
||||
ffStrbufDestroy(&file2);
|
||||
if(allPropertiesSet(gtkval))
|
||||
if(allPropertiesSet(result))
|
||||
return;
|
||||
|
||||
// <configdir>/gtkrc-<version>.0
|
||||
@@ -179,9 +179,9 @@ static void calculateGTKFromConfigDir(const char* configDir, const char* version
|
||||
ffStrbufAppendS(&file3, "/gtkrc-");
|
||||
ffStrbufAppendS(&file3, version);
|
||||
ffStrbufAppendS(&file3, ".0");
|
||||
parseGTKConfigFile(&file3, gtkval);
|
||||
parseGTKConfigFile(&file3, result);
|
||||
ffStrbufDestroy(&file3);
|
||||
if(allPropertiesSet(gtkval))
|
||||
if(allPropertiesSet(result))
|
||||
return;
|
||||
|
||||
// <configdir>/.gtkrc-<version>.0
|
||||
@@ -191,11 +191,11 @@ static void calculateGTKFromConfigDir(const char* configDir, const char* version
|
||||
ffStrbufAppendS(&file4, "/.gtkrc-");
|
||||
ffStrbufAppendS(&file4, version);
|
||||
ffStrbufAppendS(&file4, ".0");
|
||||
parseGTKConfigFile(&file4, gtkval);
|
||||
parseGTKConfigFile(&file4, result);
|
||||
ffStrbufDestroy(&file4);
|
||||
}
|
||||
|
||||
static void calculateGTK(FFinstance* instance, const char* version, FFgtkval* gtkval)
|
||||
static void calculateGTK(FFinstance* instance, const char* version, FFGTKResult* result)
|
||||
{
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
@@ -204,7 +204,7 @@ static void calculateGTK(FFinstance* instance, const char* version, FFgtkval* gt
|
||||
static bool xdgIsDifferent;
|
||||
static bool init = false;
|
||||
|
||||
initGtkval(gtkval);
|
||||
initresult(result);
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
|
||||
@@ -228,52 +228,57 @@ static void calculateGTK(FFinstance* instance, const char* version, FFgtkval* gt
|
||||
if(xdgIsDifferent)
|
||||
{
|
||||
// $XDG_CONFIG_HOME
|
||||
calculateGTKFromConfigDir(xdgConfigDir.chars, version, gtkval);
|
||||
if(allPropertiesSet(gtkval))
|
||||
calculateGTKFromConfigDir(xdgConfigDir.chars, version, result);
|
||||
if(allPropertiesSet(result))
|
||||
return;
|
||||
}
|
||||
|
||||
// /home/<user>/.config
|
||||
calculateGTKFromConfigDir(configDir.chars, version, gtkval);
|
||||
if(allPropertiesSet(gtkval))
|
||||
calculateGTKFromConfigDir(configDir.chars, version, result);
|
||||
if(allPropertiesSet(result))
|
||||
return;
|
||||
|
||||
// /home/<user>
|
||||
calculateGTKFromConfigDir(instance->state.passwd->pw_dir, version, gtkval);
|
||||
if(allPropertiesSet(gtkval))
|
||||
calculateGTKFromConfigDir(instance->state.passwd->pw_dir, version, result);
|
||||
if(allPropertiesSet(result))
|
||||
return;
|
||||
|
||||
parseGTKDConfSettings(instance, gtkval);
|
||||
if(allPropertiesSet(gtkval))
|
||||
parseGTKDConfSettings(instance, result);
|
||||
if(allPropertiesSet(result))
|
||||
return;
|
||||
|
||||
// /etc
|
||||
calculateGTKFromConfigDir("/etc/", version, gtkval);
|
||||
calculateGTKFromConfigDir("/etc/", version, result);
|
||||
}
|
||||
|
||||
#define FF_CALCULATE_GTK_IMPL(version) \
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; \
|
||||
static FFGTKResult result; \
|
||||
static bool init = false; \
|
||||
pthread_mutex_lock(&mutex); \
|
||||
if(init){ \
|
||||
pthread_mutex_unlock(&mutex);\
|
||||
return; \
|
||||
return &result; \
|
||||
} \
|
||||
init = true; \
|
||||
calculateGTK(instance, #version, &instance->state.gtk##version); \
|
||||
pthread_mutex_unlock(&mutex);
|
||||
ffStrbufInit(&result.theme); \
|
||||
ffStrbufInit(&result.icons); \
|
||||
ffStrbufInit(&result.font); \
|
||||
calculateGTK(instance, #version, &result); \
|
||||
pthread_mutex_unlock(&mutex); \
|
||||
return &result;
|
||||
|
||||
void ffCalculateGTK2(FFinstance* instance)
|
||||
const FFGTKResult* ffCalculateGTK2(FFinstance* instance)
|
||||
{
|
||||
FF_CALCULATE_GTK_IMPL(2)
|
||||
}
|
||||
|
||||
void ffCalculateGTK3(FFinstance* instance)
|
||||
const FFGTKResult* ffCalculateGTK3(FFinstance* instance)
|
||||
{
|
||||
FF_CALCULATE_GTK_IMPL(3)
|
||||
}
|
||||
|
||||
void ffCalculateGTK4(FFinstance* instance)
|
||||
const FFGTKResult* ffCalculateGTK4(FFinstance* instance)
|
||||
{
|
||||
FF_CALCULATE_GTK_IMPL(4)
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
#include "fastfetch.h"
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
const FFOSResult* ffCalculateOS(FFinstance* instance)
|
||||
{
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static FFOSResult result;
|
||||
static bool init = false;
|
||||
pthread_mutex_lock(&mutex);
|
||||
if(init)
|
||||
{
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return &result;
|
||||
}
|
||||
init = true;
|
||||
|
||||
FILE* osRelease = fopen("/etc/os-release", "r");
|
||||
|
||||
if(osRelease == NULL)
|
||||
osRelease = fopen("/usr/lib/os-release", "r");
|
||||
|
||||
ffStrbufInitA(&result.error, 64);
|
||||
if(osRelease == NULL)
|
||||
{
|
||||
ffStrbufAppendS(&result.error, "couldn't read /etc/os-release nor /usr/lib/os-release");
|
||||
return &result;
|
||||
}
|
||||
|
||||
ffStrbufInitA(&result.systemName, 256);
|
||||
ffStrbufInitA(&result.name, 256);
|
||||
ffStrbufInitA(&result.prettyName, 256);
|
||||
ffStrbufInitA(&result.id, 256);
|
||||
ffStrbufInitA(&result.idLike, 256);
|
||||
ffStrbufInitA(&result.variant, 256);
|
||||
ffStrbufInitA(&result.variantID, 256);
|
||||
ffStrbufInitA(&result.version, 256);
|
||||
ffStrbufInitA(&result.versionID, 256);
|
||||
ffStrbufInitA(&result.codename, 256);
|
||||
ffStrbufInitA(&result.buildID, 256);
|
||||
ffStrbufInitA(&result.architecture, 256);
|
||||
ffStrbufInitA(&result.error, 256);
|
||||
|
||||
ffStrbufSetS(&result.systemName, instance->state.utsname.sysname);
|
||||
ffStrbufSetS(&result.architecture, instance->state.utsname.machine);
|
||||
|
||||
char* line = NULL;
|
||||
size_t len = 0;
|
||||
|
||||
// Documentation of the fields:
|
||||
// https://www.freedesktop.org/software/systemd/man/os-release.html
|
||||
while (getline(&line, &len, osRelease) != -1)
|
||||
{
|
||||
sscanf(line, "NAME=%[^\n]", result.name.chars);
|
||||
sscanf(line, "NAME=\"%[^\"]+", result.name.chars);
|
||||
sscanf(line, "PRETTY_NAME=%[^\n]", result.prettyName.chars);
|
||||
sscanf(line, "PRETTY_NAME=\"%[^\"]+", result.prettyName.chars);
|
||||
sscanf(line, "ID=%[^\n]", result.id.chars);
|
||||
sscanf(line, "ID=\"%[^\"]+", result.id.chars);
|
||||
sscanf(line, "ID_LIKE=%[^\n]", result.idLike.chars);
|
||||
sscanf(line, "ID_LIKE=\"%[^\"]+", result.idLike.chars);
|
||||
sscanf(line, "VARIANT=%[^\n]", result.variant.chars);
|
||||
sscanf(line, "VARIANT=\"%[^\"]+", result.variant.chars);
|
||||
sscanf(line, "VARIANT_ID=%[^\n]", result.variantID.chars);
|
||||
sscanf(line, "VARIANT_ID=\"%[^\"]+", result.variantID.chars);
|
||||
sscanf(line, "VERSION=%[^\n]", result.version.chars);
|
||||
sscanf(line, "VERSION=\"%[^\"]+", result.version.chars);
|
||||
sscanf(line, "VERSION_ID=%[^\n]", result.versionID.chars);
|
||||
sscanf(line, "VERSION_ID=\"%[^\"]+", result.versionID.chars);
|
||||
sscanf(line, "VERSION_CODENAME=%[^\n]", result.codename.chars);
|
||||
sscanf(line, "VERSION_CODENAME=\"%[^\"]+", result.codename.chars);
|
||||
sscanf(line, "BUILD_ID=%[^\n]", result.buildID.chars);
|
||||
sscanf(line, "BUILD_ID=\"%[^\"]+", result.buildID.chars);
|
||||
}
|
||||
|
||||
ffStrbufRecalculateLength(&result.systemName);
|
||||
ffStrbufRecalculateLength(&result.name);
|
||||
ffStrbufRecalculateLength(&result.prettyName);
|
||||
ffStrbufRecalculateLength(&result.id);
|
||||
ffStrbufRecalculateLength(&result.idLike);
|
||||
ffStrbufRecalculateLength(&result.variant);
|
||||
ffStrbufRecalculateLength(&result.variantID);
|
||||
ffStrbufRecalculateLength(&result.version);
|
||||
ffStrbufRecalculateLength(&result.versionID);
|
||||
ffStrbufRecalculateLength(&result.codename);
|
||||
ffStrbufRecalculateLength(&result.buildID);
|
||||
ffStrbufRecalculateLength(&result.architecture);
|
||||
ffStrbufRecalculateLength(&result.error);
|
||||
|
||||
if(line != NULL)
|
||||
free(line);
|
||||
|
||||
fclose(osRelease);
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
return &result;
|
||||
}
|
||||
@@ -11,25 +11,23 @@ typedef enum PlasmaCategory
|
||||
PLASMA_CATEGORY_OTHER
|
||||
} PlasmaCategory;
|
||||
|
||||
void ffCalculatePlasma(FFinstance* instance)
|
||||
const FFPlasmaResult* ffCalculatePlasma(FFinstance* instance)
|
||||
{
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static FFPlasmaResult result;
|
||||
static bool init = false;
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
|
||||
if(init)
|
||||
{
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return;
|
||||
return &result;
|
||||
}
|
||||
|
||||
init = true;
|
||||
|
||||
ffStrbufInit(&instance->state.plasma.widgetStyle);
|
||||
ffStrbufInit(&instance->state.plasma.colorScheme);
|
||||
ffStrbufInit(&instance->state.plasma.icons);
|
||||
ffStrbufInit(&instance->state.plasma.font);
|
||||
ffStrbufInit(&result.widgetStyle);
|
||||
ffStrbufInit(&result.colorScheme);
|
||||
ffStrbufInit(&result.icons);
|
||||
ffStrbufInit(&result.font);
|
||||
|
||||
FF_STRBUF_CREATE(kdeglobalsFile);
|
||||
ffStrbufAppendS(&kdeglobalsFile, instance->state.passwd->pw_dir);
|
||||
@@ -40,7 +38,7 @@ void ffCalculatePlasma(FFinstance* instance)
|
||||
{
|
||||
ffStrbufDestroy(&kdeglobalsFile);
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return;
|
||||
return &result;
|
||||
}
|
||||
|
||||
char* line = NULL;
|
||||
@@ -69,28 +67,28 @@ void ffCalculatePlasma(FFinstance* instance)
|
||||
|
||||
if(category == PLASMA_CATEGORY_KDE)
|
||||
{
|
||||
sscanf(line, "widgetStyle=%[^\n]", instance->state.plasma.widgetStyle.chars);
|
||||
sscanf(line, "widgetStyle=\"%[^\"]+", instance->state.plasma.widgetStyle.chars);
|
||||
sscanf(line, "widgetStyle=%[^\n]", result.widgetStyle.chars);
|
||||
sscanf(line, "widgetStyle=\"%[^\"]+", result.widgetStyle.chars);
|
||||
}
|
||||
else if(category == PLASMA_CATEGORY_GENERAL)
|
||||
{
|
||||
sscanf(line, "ColorScheme=%[^\n]", instance->state.plasma.colorScheme.chars);
|
||||
sscanf(line, "ColorScheme=\"%[^\"]+", instance->state.plasma.colorScheme.chars);
|
||||
sscanf(line, "ColorScheme=%[^\n]", result.colorScheme.chars);
|
||||
sscanf(line, "ColorScheme=\"%[^\"]+", result.colorScheme.chars);
|
||||
|
||||
sscanf(line, "font=%[^\n]", instance->state.plasma.font.chars);
|
||||
sscanf(line, "font=\"%[^\"]+", instance->state.plasma.font.chars);
|
||||
sscanf(line, "font=%[^\n]", result.font.chars);
|
||||
sscanf(line, "font=\"%[^\"]+", result.font.chars);
|
||||
}
|
||||
else if(category == PLASMA_CATEGORY_ICONS)
|
||||
{
|
||||
sscanf(line, "Theme=%[^\n]", instance->state.plasma.icons.chars);
|
||||
sscanf(line, "Theme=\"%[^\"]+", instance->state.plasma.icons.chars);
|
||||
sscanf(line, "Theme=%[^\n]", result.icons.chars);
|
||||
sscanf(line, "Theme=\"%[^\"]+", result.icons.chars);
|
||||
}
|
||||
}
|
||||
|
||||
ffStrbufRecalculateLength(&instance->state.plasma.widgetStyle);
|
||||
ffStrbufRecalculateLength(&instance->state.plasma.colorScheme);
|
||||
ffStrbufRecalculateLength(&instance->state.plasma.icons);
|
||||
ffStrbufRecalculateLength(&instance->state.plasma.font);
|
||||
ffStrbufRecalculateLength(&result.widgetStyle);
|
||||
ffStrbufRecalculateLength(&result.colorScheme);
|
||||
ffStrbufRecalculateLength(&result.icons);
|
||||
ffStrbufRecalculateLength(&result.font);
|
||||
|
||||
if(line != NULL)
|
||||
free(line);
|
||||
@@ -100,17 +98,19 @@ void ffCalculatePlasma(FFinstance* instance)
|
||||
|
||||
//In Plasma the default value is never set in the config file, but the whole key-value is discarded.
|
||||
///We must set these values by our self if the file exists (it always does here)
|
||||
if(instance->state.plasma.widgetStyle.length == 0)
|
||||
ffStrbufAppendS(&instance->state.plasma.widgetStyle, "Breeze");
|
||||
if(result.widgetStyle.length == 0)
|
||||
ffStrbufAppendS(&result.widgetStyle, "Breeze");
|
||||
|
||||
if(instance->state.plasma.colorScheme.length == 0)
|
||||
ffStrbufAppendS(&instance->state.plasma.colorScheme, "BreezeLight");
|
||||
if(result.colorScheme.length == 0)
|
||||
ffStrbufAppendS(&result.colorScheme, "BreezeLight");
|
||||
|
||||
if(instance->state.plasma.icons.length == 0)
|
||||
ffStrbufAppendS(&instance->state.plasma.icons, "Breeze");
|
||||
if(result.icons.length == 0)
|
||||
ffStrbufAppendS(&result.icons, "Breeze");
|
||||
|
||||
if(instance->state.plasma.font.length == 0)
|
||||
ffStrbufAppendS(&instance->state.plasma.font, "Noto Sans, 10");
|
||||
if(result.font.length == 0)
|
||||
ffStrbufAppendS(&result.font, "Noto Sans, 10");
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
return &result;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <unistd.h>
|
||||
#include <pthread.h>
|
||||
|
||||
static void getTerminalName(FFinstance* instance, const char* pid)
|
||||
static void getTerminalName(FFinstance* instance, const char* pid, FFTerminalResult* result)
|
||||
{
|
||||
char statFile[234];
|
||||
sprintf(statFile, "/proc/%s/stat", pid);
|
||||
@@ -12,7 +12,7 @@ static void getTerminalName(FFinstance* instance, const char* pid)
|
||||
FILE* stat = fopen(statFile, "r");
|
||||
if(stat == NULL)
|
||||
{
|
||||
ffStrbufSetF(&instance->state.terminal.error, "fopen(\"%s\", \"r\") == NULL", statFile);
|
||||
ffStrbufSetF(&result->error, "fopen(\"%s\", \"r\") == NULL", statFile);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ static void getTerminalName(FFinstance* instance, const char* pid)
|
||||
char ppid[256];
|
||||
if(fscanf(stat, "%*s (%[^)])%*s%s", name, ppid) != 2)
|
||||
{
|
||||
ffStrbufSetS(&instance->state.terminal.error, "fscanf(stat, \"%*s (%[^)])%*s%s\", name, ppid) != 2");
|
||||
ffStrbufSetS(&result->error, "fscanf(stat, \"%*s (%[^)])%*s%s\", name, ppid) != 2");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -37,42 +37,43 @@ static void getTerminalName(FFinstance* instance, const char* pid)
|
||||
strcasecmp(name, "doas") == 0 ||
|
||||
strcasecmp(name, "strace") == 0 )
|
||||
{
|
||||
getTerminalName(instance, ppid);
|
||||
getTerminalName(instance, ppid, result);
|
||||
return;
|
||||
}
|
||||
|
||||
char cmdlineFile[234];
|
||||
sprintf(cmdlineFile, "/proc/%s/cmdline", pid);
|
||||
|
||||
ffGetFileContent(cmdlineFile, &instance->state.terminal.exeName);
|
||||
ffStrbufSubstrBeforeFirstC(&instance->state.terminal.exeName, '\0');
|
||||
ffStrbufSubstrAfterLastC(&instance->state.terminal.exeName, '/');
|
||||
ffGetFileContent(cmdlineFile, &result->exeName);
|
||||
ffStrbufSubstrBeforeFirstC(&result->exeName, '\0');
|
||||
ffStrbufSubstrAfterLastC(&result->exeName, '/');
|
||||
|
||||
ffStrbufSetS(&instance->state.terminal.processName, name);
|
||||
ffStrbufSetS(&result->processName, name);
|
||||
}
|
||||
|
||||
void ffCalculateTerminal(FFinstance* instance)
|
||||
const FFTerminalResult* ffCalculateTerminal(FFinstance* instance)
|
||||
{
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static FFTerminalResult result;
|
||||
static bool init = false;
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
|
||||
if(init)
|
||||
{
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return;
|
||||
return &result;
|
||||
}
|
||||
init = true;
|
||||
|
||||
ffStrbufInit(&instance->state.terminal.exeName);
|
||||
ffStrbufInit(&instance->state.terminal.processName);
|
||||
ffStrbufInit(&instance->state.terminal.error);
|
||||
ffStrbufInit(&result.exeName);
|
||||
ffStrbufInit(&result.processName);
|
||||
ffStrbufInit(&result.error);
|
||||
|
||||
char ppid[256];
|
||||
sprintf(ppid, "%i", getppid());
|
||||
|
||||
getTerminalName(instance, ppid);
|
||||
getTerminalName(instance, ppid, &result);
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
return &result;
|
||||
}
|
||||
|
||||
+19
-17
@@ -3,31 +3,31 @@
|
||||
#include <dirent.h>
|
||||
#include <pthread.h>
|
||||
|
||||
void ffCalculateWM(FFinstance* instance)
|
||||
const FFWMResult* ffCalculateWM(FFinstance* instance)
|
||||
{
|
||||
UNUSED(instance);
|
||||
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
static FFWMResult result;
|
||||
static bool init = false;
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
|
||||
if(init)
|
||||
{
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return;
|
||||
return &result;
|
||||
}
|
||||
|
||||
init = true;
|
||||
|
||||
ffStrbufInit(&instance->state.wm.processName);
|
||||
ffStrbufInit(&instance->state.wm.prettyName);
|
||||
ffStrbufInit(&instance->state.wm.error);
|
||||
ffStrbufInit(&result.processName);
|
||||
ffStrbufInit(&result.prettyName);
|
||||
ffStrbufInit(&result.error);
|
||||
|
||||
DIR* proc = opendir("/proc/");
|
||||
if(proc == NULL)
|
||||
{
|
||||
ffStrbufSetS(&instance->state.wm.error, "opendir(\"/proc/\") == NULL");
|
||||
ffStrbufSetS(&result.error, "opendir(\"/proc/\") == NULL");
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return;
|
||||
return &result;
|
||||
}
|
||||
|
||||
struct dirent* dirent;
|
||||
@@ -40,22 +40,24 @@ void ffCalculateWM(FFinstance* instance)
|
||||
char path[20];
|
||||
sprintf(path, "/proc/%.8s/comm", dirent->d_name);
|
||||
|
||||
ffGetFileContent(path, &instance->state.wm.processName);
|
||||
ffGetFileContent(path, &result.processName);
|
||||
|
||||
if(ffStrbufIgnCaseCompS(&instance->state.wm.processName, "kwin_wayland") == 0 || ffStrbufIgnCaseCompS(&instance->state.wm.processName, "kwin_x11") == 0)
|
||||
ffStrbufSetS(&instance->state.wm.prettyName, "KWin");
|
||||
if(ffStrbufIgnCaseCompS(&result.processName, "kwin_wayland") == 0 || ffStrbufIgnCaseCompS(&result.processName, "kwin_x11") == 0)
|
||||
ffStrbufSetS(&result.prettyName, "KWin");
|
||||
|
||||
if(instance->state.wm.prettyName.length > 0)
|
||||
if(result.prettyName.length > 0)
|
||||
break;
|
||||
}
|
||||
|
||||
closedir(proc);
|
||||
|
||||
if(instance->state.wm.prettyName.length == 0)
|
||||
if(result.prettyName.length == 0)
|
||||
{
|
||||
ffStrbufSetS(&instance->state.wm.error, "No process name matches the name of known display managers");
|
||||
ffStrbufClear(&instance->state.wm.processName);
|
||||
ffStrbufSetS(&result.error, "No process name matches the name of known display managers");
|
||||
ffStrbufClear(&result.processName);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&mutex);
|
||||
|
||||
return &result;
|
||||
}
|
||||
|
||||
@@ -83,14 +83,6 @@ void ffInitInstance(FFinstance* instance)
|
||||
ffCacheValidate(instance);
|
||||
}
|
||||
|
||||
void ffCalculatePlasmaAndGtk(FFinstance* instance)
|
||||
{
|
||||
ffCalculatePlasma(instance);
|
||||
ffCalculateGTK2(instance);
|
||||
ffCalculateGTK3(instance);
|
||||
ffCalculateGTK3(instance);
|
||||
}
|
||||
|
||||
static void ffCleanup(FFinstance* instance)
|
||||
{
|
||||
UNUSED(instance);
|
||||
|
||||
+60
-40
@@ -6,6 +6,9 @@
|
||||
#include <sys/stat.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#define FF_IO_CACHE_VALUE_EXTENSION "ffcv"
|
||||
#define FF_IO_CACHE_SPLIT_EXTENSION "ffcs"
|
||||
|
||||
void ffPrintLogoAndKey(FFinstance* instance, const char* moduleName, uint8_t moduleIndex, const FFstrbuf* customKeyFormat)
|
||||
{
|
||||
ffPrintLogoLine(instance);
|
||||
@@ -120,29 +123,41 @@ static void appendCacheDir(FFinstance* instance, FFstrbuf* buffer)
|
||||
ffStrbufAppend(buffer, &cacheDir);
|
||||
}
|
||||
|
||||
static inline void getCacheFileValue(FFinstance* instance, const char* moduleName, FFstrbuf* cacheFilePath)
|
||||
void ffGetCacheFilePath(FFinstance* instance, const char* moduleName, const char* extension, FFstrbuf* buffer)
|
||||
{
|
||||
appendCacheDir(instance, cacheFilePath);
|
||||
ffStrbufAppendS(cacheFilePath, moduleName);
|
||||
ffStrbufAppendS(cacheFilePath, ".ffcv");
|
||||
appendCacheDir(instance, buffer);
|
||||
ffStrbufAppendS(buffer, moduleName);
|
||||
|
||||
if(extension != NULL)
|
||||
{
|
||||
ffStrbufAppendC(buffer, '.');
|
||||
ffStrbufAppendS(buffer, extension);
|
||||
}
|
||||
}
|
||||
|
||||
static inline void getCacheFileSplit(FFinstance* instance, const char* moduleName, FFstrbuf* cacheFilePath)
|
||||
void ffReadCacheFile(FFinstance* instance, const char* moduleName, const char* extension, FFstrbuf* buffer)
|
||||
{
|
||||
appendCacheDir(instance, cacheFilePath);
|
||||
ffStrbufAppendS(cacheFilePath, moduleName);
|
||||
ffStrbufAppendS(cacheFilePath, ".ffcs");
|
||||
FFstrbuf path;
|
||||
ffStrbufInitA(&path, 64);
|
||||
ffGetCacheFilePath(instance, moduleName, extension, &path);
|
||||
ffAppendFileContent(path.chars, buffer);
|
||||
ffStrbufDestroy(&path);
|
||||
}
|
||||
|
||||
void ffWriteCacheFile(FFinstance* instance, const char* moduleName, const char* extension, FFstrbuf* content)
|
||||
{
|
||||
FFstrbuf path;
|
||||
ffStrbufInitA(&path, 64);
|
||||
ffGetCacheFilePath(instance, moduleName, extension, &path);
|
||||
ffWriteFileContent(path.chars, content);
|
||||
ffStrbufDestroy(&path);
|
||||
}
|
||||
|
||||
static bool printCachedValue(FFinstance* instance, const char* moduleName, const FFstrbuf* customKeyFormat)
|
||||
{
|
||||
FFstrbuf cacheFilePath;
|
||||
ffStrbufInitA(&cacheFilePath, 64);
|
||||
getCacheFileValue(instance, moduleName, &cacheFilePath);
|
||||
|
||||
FFstrbuf content;
|
||||
ffStrbufInitA(&content, 512);
|
||||
ffAppendFileContent(cacheFilePath.chars, &content);
|
||||
ffReadCacheFile(instance, moduleName, FF_IO_CACHE_VALUE_EXTENSION, &content);
|
||||
|
||||
ffStrbufTrimRight(&content, '\0'); //Strbuf always appends a '\0' at the end. We want the last null byte to be at the position of the length
|
||||
|
||||
@@ -163,28 +178,21 @@ static bool printCachedValue(FFinstance* instance, const char* moduleName, const
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&content);
|
||||
ffStrbufDestroy(&cacheFilePath);
|
||||
|
||||
return moduleCounter > 1;
|
||||
}
|
||||
|
||||
static bool printCachedFormat(FFinstance* instance, const char* moduleName, const FFstrbuf* customKeyFormat, const FFstrbuf* formatString, uint32_t numArgs)
|
||||
{
|
||||
FFstrbuf cacheFilePath;
|
||||
ffStrbufInitA(&cacheFilePath, 64);
|
||||
getCacheFileSplit(instance, moduleName, &cacheFilePath);
|
||||
|
||||
FFstrbuf content;
|
||||
ffStrbufInitA(&content, 512);
|
||||
ffAppendFileContent(cacheFilePath.chars, &content);
|
||||
ffReadCacheFile(instance, moduleName, FF_IO_CACHE_SPLIT_EXTENSION, &content);
|
||||
|
||||
ffStrbufTrimRight(&content, '\0'); //Strbuf always appends a '\0' at the end. We want the last null byte to be at the position of the length
|
||||
|
||||
if(content.length == 0)
|
||||
return false;
|
||||
|
||||
//Strbuf adds an extra nullbyte at chars[length]. We want this one to be the end of the last value to test for index == length
|
||||
if(content.chars[content.length - 1] == '\0')
|
||||
ffStrbufSubstrBefore(&content, content.length - 1);
|
||||
|
||||
uint8_t moduleCounter = 1;
|
||||
|
||||
FFformatarg arguments[numArgs];
|
||||
@@ -211,7 +219,6 @@ static bool printCachedFormat(FFinstance* instance, const char* moduleName, cons
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&content);
|
||||
ffStrbufDestroy(&cacheFilePath);
|
||||
|
||||
return moduleCounter > 1;
|
||||
}
|
||||
@@ -269,47 +276,44 @@ void ffPrintAndSaveToCache(FFinstance* instance, const char* moduleName, const F
|
||||
|
||||
void ffCacheValidate(FFinstance* instance)
|
||||
{
|
||||
FFstrbuf cacheFilePath;
|
||||
ffStrbufInitA(&cacheFilePath, 64);
|
||||
appendCacheDir(instance, &cacheFilePath);
|
||||
ffStrbufAppendS(&cacheFilePath, "cacheversion.ffv");
|
||||
FFstrbuf path;
|
||||
ffStrbufInitA(&path, 64);
|
||||
ffGetCacheFilePath(instance, "cacheversion", "ffv", &path);
|
||||
|
||||
FFstrbuf content;
|
||||
ffStrbufInit(&content);
|
||||
ffAppendFileContent(cacheFilePath.chars, &content);
|
||||
ffAppendFileContent(path.chars, &content);
|
||||
|
||||
if(ffStrbufCompS(&content, FASTFETCH_PROJECT_VERSION) == 0)
|
||||
{
|
||||
ffStrbufDestroy(&content);
|
||||
ffStrbufDestroy(&cacheFilePath);
|
||||
ffStrbufDestroy(&path);
|
||||
return;
|
||||
}
|
||||
|
||||
instance->config.recache = true;
|
||||
|
||||
FILE* versionFile = fopen(cacheFilePath.chars, "w");
|
||||
if(versionFile == NULL)
|
||||
return;
|
||||
|
||||
fputs(FASTFETCH_PROJECT_VERSION, versionFile);
|
||||
|
||||
fclose(versionFile);
|
||||
FFstrbuf version;
|
||||
ffStrbufInit(&version);
|
||||
ffStrbufAppendS(&version, FASTFETCH_PROJECT_VERSION);
|
||||
ffWriteFileContent(path.chars, &version);
|
||||
ffStrbufDestroy(&version);
|
||||
|
||||
ffStrbufDestroy(&content);
|
||||
ffStrbufDestroy(&cacheFilePath);
|
||||
ffStrbufDestroy(&path);
|
||||
}
|
||||
|
||||
void ffCacheOpenWrite(FFinstance* instance, const char* moduleName, FFcache* cache)
|
||||
{
|
||||
FFstrbuf cacheFileValue;
|
||||
ffStrbufInitA(&cacheFileValue, 64);
|
||||
getCacheFileValue(instance, moduleName, &cacheFileValue);
|
||||
ffGetCacheFilePath(instance, moduleName, FF_IO_CACHE_VALUE_EXTENSION, &cacheFileValue);
|
||||
cache->value = fopen(cacheFileValue.chars, "w");
|
||||
ffStrbufDestroy(&cacheFileValue);
|
||||
|
||||
FFstrbuf cacheFileSplit;
|
||||
ffStrbufInitA(&cacheFileSplit, 64);
|
||||
getCacheFileSplit(instance, moduleName, &cacheFileSplit);
|
||||
ffGetCacheFilePath(instance, moduleName, FF_IO_CACHE_SPLIT_EXTENSION, &cacheFileSplit);
|
||||
cache->split = fopen(cacheFileSplit.chars, "w");
|
||||
ffStrbufDestroy(&cacheFileSplit);
|
||||
}
|
||||
@@ -358,6 +362,22 @@ void ffParsePropFileHome(FFinstance* instance, const char* relativeFile, const c
|
||||
ffStrbufDestroy(&absolutePath);
|
||||
}
|
||||
|
||||
void ffWriteFDContent(int fd, const FFstrbuf* content)
|
||||
{
|
||||
write(fd, content->chars, content->length);
|
||||
}
|
||||
|
||||
void ffWriteFileContent(const char* fileName, const FFstrbuf* content)
|
||||
{
|
||||
int fd = open(fileName, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
||||
if(fd == -1)
|
||||
return;
|
||||
|
||||
ffWriteFDContent(fd, content);
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
void ffAppendFDContent(int fd, FFstrbuf* buffer)
|
||||
{
|
||||
ssize_t readed;
|
||||
|
||||
+38
-23
@@ -170,7 +170,7 @@ static void loadManjaroLogo(FFlogo *logo, bool doColor)
|
||||
/* color = light green */
|
||||
const char* color = doColor ? "\033[92m" : "";
|
||||
strcpy(logo->color, "\033[92m");
|
||||
|
||||
|
||||
sprintf(logo->chars[0], FASTFETCH_TEXT_MODIFIER_BOLT"%s██████████████████ ████████"FASTFETCH_TEXT_MODIFIER_RESET, color);
|
||||
sprintf(logo->chars[1], FASTFETCH_TEXT_MODIFIER_BOLT"%s██████████████████ ████████"FASTFETCH_TEXT_MODIFIER_RESET, color);
|
||||
sprintf(logo->chars[2], FASTFETCH_TEXT_MODIFIER_BOLT"%s██████████████████ ████████"FASTFETCH_TEXT_MODIFIER_RESET, color);
|
||||
@@ -226,29 +226,19 @@ void ffLoadLogoSet(FFconfig* config, const char* logo)
|
||||
config->logo_spacing = 0; //This is wanted in most cases, so just set it
|
||||
}
|
||||
else if(strcasecmp(logo, "arch") == 0)
|
||||
{
|
||||
loadArchLogo(&config->logo, config->colorLogo);
|
||||
}
|
||||
else if(strcasecmp(logo, "artix") == 0)
|
||||
{
|
||||
loadArtixLogo(&config->logo, config->colorLogo);
|
||||
}
|
||||
else if(strcasecmp(logo, "ubuntu") == 0)
|
||||
{
|
||||
loadUbuntuLogo(&config->logo, config->colorLogo);
|
||||
}
|
||||
else if(strcasecmp(logo, "debian") == 0)
|
||||
{
|
||||
loadDebianLogo(&config->logo, config->colorLogo);
|
||||
}
|
||||
else if(strcasecmp(logo, "manjaro") == 0)
|
||||
{
|
||||
loadManjaroLogo(&config->logo, config->colorLogo);
|
||||
}
|
||||
else if(strcasecmp(logo, "void") == 0)
|
||||
{
|
||||
loadVoidLogo(&config->logo, config->colorLogo);
|
||||
}
|
||||
else if(strcasecmp(logo, "unknown") == 0)
|
||||
loadUnknownLogo(&config->logo);
|
||||
else
|
||||
{
|
||||
if(config->showErrors)
|
||||
@@ -257,20 +247,45 @@ void ffLoadLogoSet(FFconfig* config, const char* logo)
|
||||
}
|
||||
}
|
||||
|
||||
void ffLoadLogo(FFconfig* config)
|
||||
void ffLoadLogo(FFinstance* instance)
|
||||
{
|
||||
char id[256];
|
||||
ffParsePropFile("/etc/os-release", "ID=%[^\n]", id);
|
||||
if(id[0] == '\0')
|
||||
FFstrbuf cacheFilePath;
|
||||
ffStrbufInitA(&cacheFilePath, 64);
|
||||
ffGetCacheFilePath(instance, "logo.ffcl", NULL, &cacheFilePath);
|
||||
|
||||
if(!instance->config.recache)
|
||||
{
|
||||
if(config->showErrors)
|
||||
puts(FASTFETCH_TEXT_MODIFIER_ERROR"Error: \"ID=%[^\\n]\" not found in \"/etc/os-release\""FASTFETCH_TEXT_MODIFIER_RESET);
|
||||
loadUnknownLogo(&config->logo);
|
||||
FFstrbuf content;
|
||||
ffStrbufInit(&content);
|
||||
ffAppendFileContent(cacheFilePath.chars, &content);
|
||||
|
||||
if(content.length > 0)
|
||||
{
|
||||
ffLoadLogoSet(&instance->config, content.chars);
|
||||
ffStrbufDestroy(&content);
|
||||
ffStrbufDestroy(&cacheFilePath);
|
||||
return;
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&content);
|
||||
}
|
||||
|
||||
const FFOSResult* result = ffCalculateOS(instance);
|
||||
|
||||
if(result->id.length > 0)
|
||||
{
|
||||
ffLoadLogoSet(&instance->config, result->id.chars);
|
||||
ffWriteFileContent(cacheFilePath.chars, &result->id);
|
||||
}
|
||||
else if(result->name.length > 0)
|
||||
{
|
||||
ffLoadLogoSet(&instance->config, result->name.chars);
|
||||
ffWriteFileContent(cacheFilePath.chars, &result->name);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffLoadLogoSet(config, id);
|
||||
}
|
||||
loadUnknownLogo(&instance->config.logo);
|
||||
|
||||
ffStrbufDestroy(&cacheFilePath);
|
||||
}
|
||||
|
||||
void ffPrintLogoLine(FFinstance* instance)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "fastfetch.h"
|
||||
|
||||
void ffGetGtkPretty(FFstrbuf* buffer, FFstrbuf* gtk2, FFstrbuf* gtk3, FFstrbuf* gtk4)
|
||||
void ffGetGtkPretty(FFstrbuf* buffer, const FFstrbuf* gtk2, const FFstrbuf* gtk3, const FFstrbuf* gtk4)
|
||||
{
|
||||
if(gtk2->length > 0 && gtk3->length > 0 && gtk4->length > 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user