Files
fastfetch/src/modules/wmtheme.c
T

180 lines
6.6 KiB
C
Raw Normal View History

2021-04-03 23:10:14 +02:00
#include "fastfetch.h"
2021-04-26 17:27:12 +02:00
#include <string.h>
2021-04-03 23:10:14 +02:00
2021-04-15 18:59:26 +02:00
#define FF_WMTHEME_MODULE_NAME "WM Theme"
#define FF_WMTHEME_NUM_FORMAT_ARGS 1
2021-04-03 23:10:14 +02:00
static void printWMTheme(FFinstance* instance, const char* theme)
{
if(instance->config.wmThemeFormat.length == 0)
{
2021-04-15 18:59:26 +02:00
ffPrintLogoAndKey(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmThemeKey);
2021-04-03 23:10:14 +02:00
puts(theme);
}
else
{
2021-04-15 18:59:26 +02:00
ffPrintFormatString(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmThemeKey, &instance->config.wmThemeFormat, NULL, FF_WMTHEME_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRING, theme}
});
2021-04-03 23:10:14 +02:00
}
}
2021-05-09 21:41:17 +02:00
static void printWMThemeFromConfig(FFinstance* instance, const char* configFile, const char* themeRegex, const char* defaultValue)
2021-04-03 23:10:14 +02:00
{
char theme[256];
2021-05-09 21:41:17 +02:00
if(!ffParsePropFileConfig(instance, configFile, themeRegex, theme))
{
ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmThemeKey, &instance->config.wmThemeFormat, FF_WMTHEME_NUM_FORMAT_ARGS, "Config file %s doesn't exist", configFile);
return;
}
2021-04-03 23:10:14 +02:00
2021-05-07 11:02:09 +02:00
if(*theme == '\0')
2021-04-03 23:10:14 +02:00
{
2021-05-09 21:41:17 +02:00
if(defaultValue == NULL)
2021-05-07 11:02:09 +02:00
{
2021-05-09 22:04:18 +02:00
ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmThemeKey, &instance->config.wmThemeFormat, FF_WMTHEME_NUM_FORMAT_ARGS, "Couldn't find theme in %s", configFile);
2021-05-07 11:02:09 +02:00
return;
}
2021-05-09 21:41:17 +02:00
printWMTheme(instance, defaultValue);
return;
2021-04-03 23:10:14 +02:00
}
printWMTheme(instance, theme);
}
2021-05-01 21:22:07 +02:00
static void printMutter(FFinstance* instance)
{
2021-05-08 17:56:56 +02:00
const char* theme = ffSettingsGetStr(instance, "/org/gnome/shell/extensions/user-theme/name", "org.gnome.shell.extensions.user-theme", NULL, "name");
2021-05-01 21:22:07 +02:00
2021-05-02 18:52:42 +02:00
if(theme == NULL)
2021-05-08 17:56:56 +02:00
theme = ffSettingsGetStr(instance, "/org/gnome/desktop/wm/preferences/theme", "org.gnome.desktop.wm.preferences", NULL, "theme");
2021-05-01 21:22:07 +02:00
2021-05-02 18:52:42 +02:00
if(theme == NULL)
2021-05-01 21:22:07 +02:00
{
2021-05-02 18:52:42 +02:00
ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmThemeKey, &instance->config.wmThemeFormat, FF_WMTHEME_NUM_FORMAT_ARGS, "Couldn't found mutter theme in GSettings / DConf");
return;
2021-05-01 21:22:07 +02:00
}
2021-05-02 18:52:42 +02:00
printWMTheme(instance, theme);
2021-05-01 21:22:07 +02:00
}
2021-05-08 11:20:17 +02:00
static void printMuffin(FFinstance* instance)
{
2021-05-08 17:56:56 +02:00
const char* name = ffSettingsGetStr(instance, "/org/cinnamon/theme/name", "org.cinnamon.theme", NULL, "name");
const char* theme = ffSettingsGetStr(instance, "/org/cinnamon/desktop/wm/preferences/theme", "org.cinnamon.desktop.wm.preferences", NULL, "theme");
2021-05-08 11:20:17 +02:00
if(name == NULL && theme == NULL)
{
ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmThemeKey, &instance->config.wmThemeFormat, FF_WMTHEME_NUM_FORMAT_ARGS, "Couldn't find muffin theme in GSettings / DConf");
return;
}
if(name == NULL)
printWMTheme(instance, theme);
else if(theme == NULL)
printWMTheme(instance, name);
else
{
FFstrbuf buffer;
ffStrbufInit(&buffer);
ffStrbufAppendS(&buffer, name);
ffStrbufAppendS(&buffer, " (");
ffStrbufAppendS(&buffer, theme);
ffStrbufAppendC(&buffer, ')');
printWMTheme(instance, buffer.chars);
ffStrbufDestroy(&buffer);
}
}
2021-05-01 13:03:30 +02:00
static void printOpenbox(FFinstance* instance, const FFstrbuf* dePrettyName)
2021-04-25 21:18:56 +02:00
{
2021-04-26 21:56:21 +02:00
FFstrbuf absolutePath;
2021-04-25 21:18:56 +02:00
ffStrbufInitA(&absolutePath, 64);
ffStrbufAppendS(&absolutePath, instance->state.passwd->pw_dir);
2021-04-26 17:27:12 +02:00
ffStrbufAppendC(&absolutePath, '/');
2021-05-01 13:03:30 +02:00
if(ffStrbufIgnCaseCompS(dePrettyName, "LXQT") == 0)
2021-04-26 17:39:05 +02:00
ffStrbufAppendS(&absolutePath, ".config/openbox/lxqt-rc.xml");
2021-05-01 13:03:30 +02:00
else if(ffStrbufIgnCaseCompS(dePrettyName, "LXDE") == 0)
2021-04-26 17:39:05 +02:00
ffStrbufAppendS(&absolutePath, ".config/openbox/lxde-rc.xml");
2021-04-26 21:56:21 +02:00
else
2021-04-26 17:39:05 +02:00
ffStrbufAppendS(&absolutePath, ".config/openbox/rc.xml");
2021-04-26 21:56:21 +02:00
2021-04-25 21:18:56 +02:00
char* line = NULL;
size_t len = 0;
FILE* file = fopen(absolutePath.chars, "r");
2021-04-26 21:46:57 +02:00
if(file == NULL)
2021-04-25 21:18:56 +02:00
{
2021-04-26 21:46:57 +02:00
ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmThemeKey, &instance->config.wmThemeFormat, FF_WMTHEME_NUM_FORMAT_ARGS, "Couldn't open \"%s\"", absolutePath.chars);
ffStrbufDestroy(&absolutePath);
return;
}
2021-04-26 21:56:21 +02:00
FFstrbuf theme;
ffStrbufInitA(&theme, 256);
2021-04-26 21:46:57 +02:00
while(getline(&line, &len, file) != -1)
{
if(strstr(line, "<theme>") != 0)
break;
}
2021-04-26 21:56:21 +02:00
2021-04-26 21:46:57 +02:00
while(getline(&line, &len, file) != -1)
{
if(strstr(line, "<name>") != 0)
2021-04-26 20:47:32 +02:00
{
2021-04-26 21:46:57 +02:00
ffStrbufAppendS(&theme, line);
ffStrbufRemoveStrings(&theme, 2, "<name>", "</name>");
ffStrbufTrimRight(&theme, '\n');
ffStrbufTrim(&theme, ' ');
break;
2021-04-26 20:47:32 +02:00
}
2021-04-26 21:56:21 +02:00
if(strstr(line, "</theme>") != 0) // sanity check
2021-04-26 21:46:57 +02:00
break;
2021-04-26 17:27:12 +02:00
}
2021-04-26 21:56:21 +02:00
2021-04-26 21:46:57 +02:00
if(line != NULL)
free(line);
fclose(file);
2021-04-26 20:47:32 +02:00
2021-04-26 17:39:05 +02:00
if(theme.length == 0)
ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmThemeKey, &instance->config.wmThemeFormat, FF_WMTHEME_NUM_FORMAT_ARGS, "Couldn't find theme name in \"%s\"", absolutePath.chars);
2021-04-26 21:46:57 +02:00
else
printWMTheme(instance, theme.chars);
2021-04-25 21:18:56 +02:00
2021-04-26 17:27:12 +02:00
ffStrbufDestroy(&theme);
2021-04-26 20:47:32 +02:00
ffStrbufDestroy(&absolutePath);
2021-04-26 17:27:12 +02:00
}
2021-04-25 21:18:56 +02:00
2021-04-03 23:10:14 +02:00
void ffPrintWMTheme(FFinstance* instance)
{
2021-05-01 13:03:30 +02:00
const FFWMDEResult* result = ffDetectWMDE(instance);
2021-04-03 23:10:14 +02:00
2021-05-01 13:03:30 +02:00
if(result->wmPrettyName.length == 0)
2021-04-03 23:10:14 +02:00
{
2021-04-15 18:59:26 +02:00
ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmThemeKey, &instance->config.wmThemeFormat, FF_WMTHEME_NUM_FORMAT_ARGS, "WM Theme needs sucessfull WM detection");
2021-04-03 23:10:14 +02:00
return;
}
2021-05-01 21:22:07 +02:00
if(ffStrbufIgnCaseCompS(&result->wmPrettyName, "KWin") == 0 || ffStrbufIgnCaseCompS(&result->wmPrettyName, "KDE") == 0 || ffStrbufIgnCaseCompS(&result->wmPrettyName, "Plasma") == 0)
2021-05-09 21:41:17 +02:00
printWMThemeFromConfig(instance, "kwinrc", " theme=%s", "Breeze");
else if(ffStrbufIgnCaseCompS(&result->wmPrettyName, "Xfwm4") == 0 || ffStrbufIgnCaseCompS(&result->wmPrettyName, "Xfwm") == 0)
printWMThemeFromConfig(instance, "xfce4/xfconf/xfce-perchannel-xml/xfwm4.xml", " <property name=\"theme\" type=\"string\" value=\"%[^\"]", "Default");
2021-05-01 21:22:07 +02:00
else if(ffStrbufIgnCaseCompS(&result->wmPrettyName, "Mutter") == 0 || ffStrbufIgnCaseCompS(&result->wmPrettyName, "Gnome") == 0 || ffStrbufIgnCaseCompS(&result->wmPrettyName, "Ubuntu") == 0)
printMutter(instance);
2021-05-08 11:20:17 +02:00
else if(ffStrbufIgnCaseCompS(&result->wmPrettyName, "Muffin") == 0)
printMuffin(instance);
2021-05-01 13:03:30 +02:00
else if(ffStrbufIgnCaseCompS(&result->wmPrettyName, "Openbox") == 0)
printOpenbox(instance, &result->dePrettyName);
2021-04-03 23:10:14 +02:00
else
2021-05-01 13:03:30 +02:00
ffPrintError(instance, FF_WMTHEME_MODULE_NAME, 0, &instance->config.wmThemeKey, &instance->config.wmThemeFormat, FF_WMTHEME_NUM_FORMAT_ARGS, "Unknown WM: %s", result->dePrettyName.chars);
2021-04-03 23:10:14 +02:00
}