plasma color theme detection

This commit is contained in:
Linus Dierheimer
2021-04-09 19:21:56 +02:00
parent 2b55a14c16
commit 0ed54eddc4
9 changed files with 159 additions and 36 deletions
+62 -9
View File
@@ -1,12 +1,22 @@
#include "fastfetch.h"
#include <string.h>
#include <pthread.h>
void ffCalculatePlasma(FFinstance* instance, FFstrbuf** themeNamePtr, FFstrbuf** iconsNamePtr, FFstrbuf** fontNamePtr)
typedef enum PlasmaCategory
{
PLASMA_CATEGORY_GENERAL,
PLASMA_CATEGORY_KDE,
PLASMA_CATEGORY_ICONS,
PLASMA_CATEGORY_OTHER
} PlasmaCategory;
void ffCalculatePlasma(FFinstance* instance, FFstrbuf** themeNamePtr, FFstrbuf** colorNamePtr, FFstrbuf** iconsNamePtr, FFstrbuf** fontNamePtr)
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static FFstrbuf themeName;
static FFstrbuf colorName;
static FFstrbuf iconsName;
static FFstrbuf fontName;
static bool init = false;
@@ -14,6 +24,9 @@ void ffCalculatePlasma(FFinstance* instance, FFstrbuf** themeNamePtr, FFstrbuf**
if(themeNamePtr != NULL)
*themeNamePtr = &themeName;
if(colorNamePtr != NULL)
*colorNamePtr = &colorName;
if(iconsNamePtr != NULL)
*iconsNamePtr = &iconsName;
@@ -31,6 +44,7 @@ void ffCalculatePlasma(FFinstance* instance, FFstrbuf** themeNamePtr, FFstrbuf**
init = true;
ffStrbufInit(&themeName);
ffStrbufInit(&colorName);
ffStrbufInit(&iconsName);
ffStrbufInit(&fontName);
@@ -49,19 +63,49 @@ void ffCalculatePlasma(FFinstance* instance, FFstrbuf** themeNamePtr, FFstrbuf**
char* line = NULL;
size_t len = 0;
PlasmaCategory category = PLASMA_CATEGORY_OTHER;
while(getline(&line, &len, kdeglobals) != -1)
{
sscanf(line, "widgetStyle=%[^\n]", themeName.chars);
sscanf(line, "widgetStyle=\"%[^\"]+", themeName.chars);
if(line[0] == '[')
{
char categoryName[32];
sscanf(line, "[%[^]]", categoryName);
sscanf(line, "Theme=%[^\n]", iconsName.chars);
sscanf(line, "Theme=\"%[^\"]+", iconsName.chars);
if(strcasecmp(categoryName, "General") == 0)
category = PLASMA_CATEGORY_GENERAL;
else if(strcasecmp(categoryName, "KDE") == 0)
category = PLASMA_CATEGORY_KDE;
else if(strcasecmp(categoryName, "Icons") == 0)
category = PLASMA_CATEGORY_ICONS;
else
category = PLASMA_CATEGORY_OTHER;
sscanf(line, "font=%[^\n]", fontName.chars);
sscanf(line, "font=\"%[^\"]+", fontName.chars);
continue;
}
if(category == PLASMA_CATEGORY_KDE)
{
sscanf(line, "widgetStyle=%[^\n]", themeName.chars);
sscanf(line, "widgetStyle=\"%[^\"]+", themeName.chars);
}
else if(category == PLASMA_CATEGORY_GENERAL)
{
sscanf(line, "ColorScheme=%[^\n]", colorName.chars);
sscanf(line, "ColorScheme=\"%[^\"]+", colorName.chars);
sscanf(line, "font=%[^\n]", fontName.chars);
sscanf(line, "font=\"%[^\"]+", fontName.chars);
}
else if(category == PLASMA_CATEGORY_ICONS)
{
sscanf(line, "Theme=%[^\n]", iconsName.chars);
sscanf(line, "Theme=\"%[^\"]+", iconsName.chars);
}
}
ffStrbufRecalculateLength(&themeName);
ffStrbufRecalculateLength(&colorName);
ffStrbufRecalculateLength(&iconsName);
ffStrbufRecalculateLength(&fontName);
@@ -71,8 +115,17 @@ void ffCalculatePlasma(FFinstance* instance, FFstrbuf** themeNamePtr, FFstrbuf**
fclose(kdeglobals);
ffStrbufDestroy(&kdeglobalsFile);
//When using Noto Sans 10, the default font in KDE Plasma, the font entry is deleted from the config file instead of set.
//So the pure existence of the file sets this font value if not set other in the file itself.
//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(themeName.length == 0)
ffStrbufAppendS(&themeName, "Breeze");
if(colorName.length == 0)
ffStrbufAppendS(&colorName, "BreezeLight");
if(iconsName.length == 0)
ffStrbufAppendS(&iconsName, "Breeze");
if(fontName.length == 0)
ffStrbufAppendS(&fontName, "Noto Sans, 10");
+1 -1
View File
@@ -4,7 +4,7 @@
static inline void* calculatePlasmaThreadMain(void* instance)
{
ffCalculatePlasma((FFinstance*)instance, NULL, NULL, NULL);
ffCalculatePlasma((FFinstance*)instance, NULL, NULL, NULL, NULL);
return NULL;
}
+3 -1
View File
@@ -308,8 +308,10 @@ static inline void printCommandHelp(const char* command)
}
else if(strcasecmp(command, "theme-format") == 0)
{
constructAndPrintCommandHelpFormat("theme", "{} [Plasma], {5}", 5,
constructAndPrintCommandHelpFormat("theme", "{} ({3}) [Plasma], {7}", 7,
"Plasma theme",
"Plasma color scheme",
"Plasma color scheme pretty",
"GTK2 theme",
"GTK3 theme",
"GTK4 theme",
+1 -1
View File
@@ -170,7 +170,7 @@ void ffGetFont(const char* font, FFstrbuf* name, double* size);
void ffGetFontPretty(FFstrbuf* buffer, const FFstrbuf* name, double size);
//common/calculatePlasma.c
void ffCalculatePlasma(FFinstance* instance, FFstrbuf** themeNamePtr, FFstrbuf** iconsNamePtr, FFstrbuf** fontNamePtr);
void ffCalculatePlasma(FFinstance* instance, FFstrbuf** themeNamePtr, FFstrbuf** colorNamePtr, FFstrbuf** iconsNamePtr, FFstrbuf** fontNamePtr);
//common/calculateGTK.c
void ffCalculateGTK2(FFinstance* instance, FFstrbuf** themeNamePtr, FFstrbuf** iconsNamePtr, FFstrbuf** fontNamePtr);
+1 -1
View File
@@ -3,7 +3,7 @@
void ffPrintFont(FFinstance* instance)
{
FFstrbuf* plasma;
ffCalculatePlasma(instance, NULL, NULL, &plasma);
ffCalculatePlasma(instance, NULL, NULL, NULL, &plasma);
FFstrbuf* gtk2;
ffCalculateGTK2(instance, NULL, NULL, &gtk2);
+1 -1
View File
@@ -3,7 +3,7 @@
void ffPrintIcons(FFinstance* instance)
{
FFstrbuf* plasma;
ffCalculatePlasma(instance, NULL, &plasma, NULL);
ffCalculatePlasma(instance, NULL, NULL, &plasma, NULL);
FFstrbuf* gtk2;
ffCalculateGTK2(instance, NULL, &gtk2, NULL);
+44 -7
View File
@@ -2,8 +2,9 @@
void ffPrintTheme(FFinstance* instance)
{
FFstrbuf* plasma;
ffCalculatePlasma(instance, &plasma, NULL, NULL);
FFstrbuf* plasmaTheme;
FFstrbuf* plasmaColor;
ffCalculatePlasma(instance, &plasmaTheme, &plasmaColor, NULL, NULL);
FFstrbuf* gtk2;
ffCalculateGTK2(instance, &gtk2, NULL, NULL);
@@ -14,12 +15,20 @@ void ffPrintTheme(FFinstance* instance)
FFstrbuf* gtk4;
ffCalculateGTK4(instance, &gtk4, NULL, NULL);
if(plasma->length == 0 && gtk2->length == 0 && gtk3->length == 0 && gtk4->length == 0)
if(plasmaTheme->length == 0 && plasmaColor->length == 0 && gtk2->length == 0 && gtk3->length == 0 && gtk4->length == 0)
{
ffPrintError(instance, &instance->config.themeKey, "Theme", "No themes found");
return;
}
FF_STRBUF_CREATE(plasmaColorPretty);
if(ffStrbufStartsWithIgnCase(plasmaColor, plasmaTheme))
ffStrbufAppendNS(&plasmaColorPretty, plasmaColor->length - plasmaTheme->length, &plasmaColor->chars[plasmaTheme->length]);
else
ffStrbufAppend(&plasmaColorPretty, plasmaColor);
ffStrbufTrim(&plasmaColorPretty, ' ');
FF_STRBUF_CREATE(gtkPretty);
ffGetGtkPretty(&gtkPretty, gtk2, gtk3, gtk4);
@@ -27,9 +36,32 @@ void ffPrintTheme(FFinstance* instance)
{
ffPrintLogoAndKey(instance, &instance->config.themeKey, "Theme");
if(plasma->length > 0)
if(plasmaTheme->length > 0)
{
ffStrbufWriteTo(plasmaTheme, stdout);
if(plasmaColor->length > 0)
{
fputs(" (", stdout);
if(plasmaColorPretty.length > 0)
ffStrbufWriteTo(&plasmaColorPretty, stdout);
else
ffStrbufWriteTo(plasmaColor, stdout);
putchar(')');
}
}
else if(plasmaColor->length > 0)
{
if(plasmaColorPretty.length > 0)
ffStrbufWriteTo(&plasmaColorPretty, stdout);
else
ffStrbufWriteTo(plasmaColor, stdout);
}
if(plasmaTheme->length > 0 || plasmaColor->length > 0)
{
ffStrbufWriteTo(plasma, stdout);
fputs(" [Plasma]", stdout);
if(gtkPretty.length > 0)
@@ -40,12 +72,17 @@ void ffPrintTheme(FFinstance* instance)
}
else
{
ffPrintFormatString(instance, &instance->config.themeKey, "Theme", &instance->config.themeFormat, 5,
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, plasma},
ffPrintFormatString(instance, &instance->config.themeKey, "Theme", &instance->config.themeFormat, 7,
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, plasmaTheme},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, plasmaColor},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, &plasmaColorPretty},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, gtk2},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, gtk3},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, gtk4},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, &gtkPretty}
);
}
ffStrbufDestroy(&plasmaColorPretty);
ffStrbufDestroy(&gtkPretty);
}
+42 -15
View File
@@ -1,6 +1,7 @@
#include "FFstrbuf.h"
#include <malloc.h>
#include <ctype.h>
#include <string.h>
void ffStrbufInit(FFstrbuf* strbuf)
@@ -371,11 +372,6 @@ void ffStrbufSubstrBeforeFirstC(FFstrbuf* strbuf, const char c)
ffStrbufSubstrBefore(strbuf, ffStrbufFirstIndexC(strbuf, c));
}
bool ffStrbufStartsWith(FFstrbuf* strbuf, FFstrbuf* start)
{
return ffStrbufStartsWithNS(strbuf, start->length, start->chars);
}
void ffStrbufSubstrAfter(FFstrbuf* strbuf, uint32_t index)
{
if(index >= strbuf->length)
@@ -396,22 +392,19 @@ void ffStrbufSubstrAfterLastC(FFstrbuf* strbuf, const char c)
ffStrbufSubstrAfter(strbuf, index);
}
static inline bool testSame(FFstrbuf* strbuf, uint32_t i, const char* str)
bool ffStrbufStartsWith(FFstrbuf* strbuf, FFstrbuf* start)
{
if(i > strbuf->length)
return false;
if(strbuf->chars[i] != str[i])
return false;
return true;
return ffStrbufStartsWithNS(strbuf, start->length, start->chars);
}
bool ffStrbufStartsWithS(FFstrbuf* strbuf, const char* start)
{
for(uint32_t i = 0; start[i] != '\0'; i++)
{
if(!testSame(strbuf, i, start))
if(i >= strbuf->length)
return false;
if(strbuf->chars[i] != start[i])
return false;
}
return true;
@@ -421,7 +414,41 @@ bool ffStrbufStartsWithNS(FFstrbuf* strbuf, uint32_t length, const char* start)
{
for(uint32_t i = 0; i < length; i++)
{
if(!testSame(strbuf, i, start))
if(i >= strbuf->length)
return false;
if(strbuf->chars[i] != start[i])
return false;
}
return true;
}
bool ffStrbufStartsWithIgnCase(FFstrbuf* strbuf, FFstrbuf* start)
{
return ffStrbufStartsWithNS(strbuf, start->length, start->chars);
}
bool ffStrbufStartsWithIgnCaseS(FFstrbuf* strbuf, const char* start)
{
for(uint32_t i = 0; start[i] != '\0'; i++)
{
if(i >= strbuf->length)
return false;
if(tolower(strbuf->chars[i]) != tolower(start[i]))
return false;
}
return true;
}
bool ffStrbufStartsWithIgnCaseNS(FFstrbuf* strbuf, uint32_t length, const char* start)
{
for(uint32_t i = 0; i < length; i++)
{
if(i >= strbuf->length)
return false;
if(tolower(strbuf->chars[i]) != tolower(start[i]))
return false;
}
return true;
+4
View File
@@ -81,6 +81,10 @@ bool ffStrbufStartsWith(FFstrbuf* strbuf, FFstrbuf* start);
bool ffStrbufStartsWithS(FFstrbuf* strbuf, const char* start);
bool ffStrbufStartsWithNS(FFstrbuf* strbuf, uint32_t length, const char* start);
bool ffStrbufStartsWithIgnCase(FFstrbuf* strbuf, FFstrbuf* start);
bool ffStrbufStartsWithIgnCaseS(FFstrbuf* strbuf, const char* start);
bool ffStrbufStartsWithIgnCaseNS(FFstrbuf* strbuf, uint32_t length, const char* start);
void ffStrbufRecalculateLength(FFstrbuf* strbuf);
void ffStrbufDestroy(FFstrbuf* strbuf);