Files
fastfetch/src/detection/qt.c
T

163 lines
4.6 KiB
C
Raw Normal View History

2021-04-07 12:55:11 +02:00
#include "fastfetch.h"
2022-07-26 17:42:28 +02:00
#include "detection/qt.h"
#include "common/properties.h"
2022-09-06 15:42:37 +02:00
#include "detection/displayserver/displayserver.h"
2021-04-07 12:55:11 +02:00
2022-06-18 14:25:31 +02:00
#include <stdlib.h>
2021-04-09 19:21:56 +02:00
#include <string.h>
2021-04-07 17:36:03 +02:00
#include <pthread.h>
2022-07-22 17:18:36 +02:00
static inline bool allValuesSet(const FFQtResult* result)
{
return
result->widgetStyle.length > 0 &&
result->colorScheme.length > 0 &&
result->icons.length > 0 &&
result->font.length > 0;
}
2021-04-09 19:21:56 +02:00
typedef enum PlasmaCategory
{
PLASMA_CATEGORY_GENERAL,
PLASMA_CATEGORY_KDE,
PLASMA_CATEGORY_ICONS,
PLASMA_CATEGORY_OTHER
} PlasmaCategory;
2022-07-22 17:18:36 +02:00
static bool detectPlasmaFromFile(const char* filename, FFQtResult* result)
2021-04-07 12:55:11 +02:00
{
FILE* kdeglobals = fopen(filename, "r");
2021-04-07 12:55:11 +02:00
if(kdeglobals == NULL)
2021-05-07 14:55:43 +02:00
return false;
2021-04-07 12:55:11 +02:00
char* line = NULL;
size_t len = 0;
2021-04-09 19:21:56 +02:00
PlasmaCategory category = PLASMA_CATEGORY_OTHER;
2021-04-07 12:55:11 +02:00
while(getline(&line, &len, kdeglobals) != -1)
{
2021-04-09 19:21:56 +02:00
if(line[0] == '[')
{
char categoryName[32];
2022-02-12 22:30:57 +01:00
sscanf(line, "[%31[^]]", categoryName);
2021-04-09 19:21:56 +02:00
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;
continue;
}
2021-10-22 18:42:14 +02:00
if(category == PLASMA_CATEGORY_KDE && result->widgetStyle.length == 0)
2022-04-08 15:50:49 +02:00
ffParsePropLine(line, "widgetStyle =", &result->widgetStyle);
2021-10-22 18:42:14 +02:00
else if(category == PLASMA_CATEGORY_ICONS && result->icons.length == 0)
2022-04-08 15:50:49 +02:00
ffParsePropLine(line, "Theme =", &result->icons);
2021-05-13 12:27:44 +02:00
else if(category == PLASMA_CATEGORY_GENERAL)
2021-04-09 19:21:56 +02:00
{
2021-10-22 18:42:14 +02:00
if(result->colorScheme.length == 0)
2022-04-08 15:50:49 +02:00
ffParsePropLine(line, "ColorScheme =", &result->colorScheme);
2021-10-22 18:42:14 +02:00
2021-11-13 16:46:32 +01:00
if(result->font.length == 0)
2022-04-08 15:50:49 +02:00
ffParsePropLine(line, "font =", &result->font);
2021-11-13 16:46:32 +01:00
//Before plasma 5.23, "Font" was the key instead of "font". Since a lot of distros ship older versions, we test for both.
2021-10-22 18:42:14 +02:00
if(result->font.length == 0)
2022-04-08 15:50:49 +02:00
ffParsePropLine(line, "Font =", &result->font);
2021-04-09 19:21:56 +02:00
}
2021-04-07 12:55:11 +02:00
}
if(line != NULL)
free(line);
fclose(kdeglobals);
2021-05-07 14:55:43 +02:00
return true;
}
2022-07-22 17:18:36 +02:00
static void detectPlasma(const FFinstance* instance, FFQtResult* result)
2021-05-07 14:55:43 +02:00
{
bool foundAFile = false;
//We need to do this because we use multiple threads on configDirs
FFstrbuf baseDir;
ffStrbufInitA(&baseDir, 64);
2021-05-07 14:55:43 +02:00
for(uint32_t i = 0; i < instance->state.configDirs.length; i++)
{
ffStrbufSet(&baseDir, ffListGet(&instance->state.configDirs, i));
ffStrbufAppendS(&baseDir, "kdeglobals");
2021-05-07 14:55:43 +02:00
2022-07-22 17:18:36 +02:00
if(detectPlasmaFromFile(baseDir.chars, result))
2021-05-07 14:55:43 +02:00
foundAFile = true;
2022-07-22 17:18:36 +02:00
if(allValuesSet(result))
{
ffStrbufDestroy(&baseDir);
return;
}
2021-05-07 14:55:43 +02:00
}
ffStrbufDestroy(&baseDir);
2021-05-07 14:55:43 +02:00
if(!foundAFile)
2022-07-22 17:18:36 +02:00
return;
//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(result->widgetStyle.length == 0)
ffStrbufAppendS(&result->widgetStyle, "Breeze");
if(result->colorScheme.length == 0)
ffStrbufAppendS(&result->colorScheme, "BreezeLight");
if(result->icons.length == 0)
ffStrbufAppendS(&result->icons, "Breeze");
if(result->font.length == 0)
ffStrbufAppendS(&result->font, "Noto Sans, 10");
}
static void detectLXQt(const FFinstance* instance, FFQtResult* result)
{
ffParsePropFileConfigValues(instance, "lxqt/lxqt.conf", 3, (FFpropquery[]) {
{"style = ", &result->widgetStyle},
{"icon_theme = ", &result->icons},
{"font = ", &result->font}
});
}
const FFQtResult* ffDetectQt(FFinstance* instance)
{
static FFQtResult result;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static bool init = false;
pthread_mutex_lock(&mutex);
if(init)
2021-05-07 14:55:43 +02:00
{
pthread_mutex_unlock(&mutex);
return &result;
}
2022-07-22 17:18:36 +02:00
init = true;
2021-04-07 12:55:11 +02:00
2022-07-22 17:18:36 +02:00
ffStrbufInit(&result.widgetStyle);
ffStrbufInit(&result.colorScheme);
ffStrbufInit(&result.icons);
ffStrbufInit(&result.font);
2021-04-09 19:21:56 +02:00
2022-07-22 17:18:36 +02:00
const FFDisplayServerResult* wmde = ffConnectDisplayServer(instance);
2021-04-09 19:21:56 +02:00
2022-07-22 17:18:36 +02:00
if(ffStrbufIgnCaseCompS(&wmde->dePrettyName, "KDE Plasma") == 0)
detectPlasma(instance, &result);
else if(ffStrbufIgnCaseCompS(&wmde->dePrettyName, "LXQt") == 0)
detectLXQt(instance, &result);
2021-04-07 17:36:03 +02:00
pthread_mutex_unlock(&mutex);
return &result;
2021-04-07 12:55:11 +02:00
}