Moved detection files to their own folder

This commit is contained in:
Linus Dierheimer
2021-12-20 16:34:46 +01:00
parent 65bac0efc9
commit d4f753a9f8
7 changed files with 9 additions and 9 deletions
-235
View File
@@ -1,235 +0,0 @@
#include "fastfetch.h"
#include <pthread.h>
static inline bool allPropertiesSet(FFGTKResult* result)
{
return
result->theme.length > 0 &&
result->icons.length > 0 &&
result->font.length > 0;
}
static inline void applyGTKDConfSettings(FFGTKResult* result, const char* themeName, const char* iconsName, const char* fontName, const char* cursorTheme, int cursorSize)
{
if(result->theme.length == 0)
ffStrbufAppendS(&result->theme, themeName);
if(result->icons.length == 0)
ffStrbufAppendS(&result->icons, iconsName);
if(result->font.length == 0)
ffStrbufAppendS(&result->font, fontName);
if(result->cursor.length == 0)
ffStrbufAppendS(&result->cursor, cursorTheme);
if(result->cursorSize.length == 0 && cursorSize > 0)
ffStrbufAppendF(&result->cursorSize, "%i", cursorSize);
}
static void detectGTKFromDConf(FFinstance* instance, FFGTKResult* result)
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static const char* themeName = NULL;
static const char* iconsName = NULL;
static const char* fontName = NULL;
static const char* cursorTheme = NULL;
static int cursorSize = 0;
static bool init = false;
pthread_mutex_lock(&mutex);
if(init)
{
pthread_mutex_unlock(&mutex);
applyGTKDConfSettings(result, themeName, iconsName, fontName, cursorTheme, cursorSize);
return;
}
init = true;
const FFWMDEResult* wmde = ffDetectWMDE(instance);
if(ffStrbufIgnCaseCompS(&wmde->dePrettyName, "Cinnamon") == 0)
{
themeName = ffSettingsGet(instance, "/org/cinnamon/desktop/interface/gtk-theme", "org.cinnamon.desktop.interface", NULL, "gtk-theme", FF_VARIANT_TYPE_STRING).strValue;
iconsName = ffSettingsGet(instance, "/org/cinnamon/desktop/interface/icon-theme", "org.cinnamon.desktop.interface", NULL, "icon-theme", FF_VARIANT_TYPE_STRING).strValue;
fontName = ffSettingsGet(instance, "/org/cinnamon/desktop/interface/font-name", "org.cinnamon.desktop.interface", NULL, "font-name", FF_VARIANT_TYPE_STRING).strValue;
cursorTheme = ffSettingsGet(instance, "/org/cinnamon/desktop/interface/cursor-theme", "org.cinnamon.desktop.interface", NULL, "cursor-theme", FF_VARIANT_TYPE_STRING).strValue;
cursorSize = ffSettingsGet(instance, "/org/cinnamon/desktop/interface/cursor-size", "org.cinnamon.desktop.interface", NULL, "cursor-size", FF_VARIANT_TYPE_INT).intValue;
}
else if(ffStrbufIgnCaseCompS(&wmde->dePrettyName, "Mate") == 0)
{
themeName = ffSettingsGet(instance, "/org/mate/interface/gtk-theme", "org.mate.interface", NULL, "gtk-theme", FF_VARIANT_TYPE_STRING).strValue;
iconsName = ffSettingsGet(instance, "/org/mate/interface/icon-theme", "org.mate.interface", NULL, "icon-theme", FF_VARIANT_TYPE_STRING).strValue;
fontName = ffSettingsGet(instance, "/org/mate/interface/font-name", "org.mate.interface", NULL, "font-name", FF_VARIANT_TYPE_STRING).strValue;
cursorTheme = ffSettingsGet(instance, "/org/mate/peripherals-mouse/cursor-theme", "org.mate.peripherals-mouse", NULL, "cursor-theme", FF_VARIANT_TYPE_STRING).strValue;
cursorSize = ffSettingsGet(instance, "/org/mate/peripherals-mouse/cursor-size", "org.mate.peripherals-mouse", NULL, "cursor-size", FF_VARIANT_TYPE_INT).intValue;
}
//Fallback + Gnome impl
if(themeName == NULL)
themeName = ffSettingsGet(instance, "/org/gnome/desktop/interface/gtk-theme", "org.gnome.desktop.interface", NULL, "gtk-theme", FF_VARIANT_TYPE_STRING).strValue;
if(iconsName == NULL)
iconsName = ffSettingsGet(instance, "/org/gnome/desktop/interface/icon-theme", "org.gnome.desktop.interface", NULL, "icon-theme", FF_VARIANT_TYPE_STRING).strValue;
if(fontName == NULL)
fontName = ffSettingsGet(instance, "/org/gnome/desktop/interface/font-name", "org.gnome.desktop.interface", NULL, "font-name", FF_VARIANT_TYPE_STRING).strValue;
if(cursorTheme == NULL)
cursorTheme = ffSettingsGet(instance, "/org/gnome/desktop/interface/cursor-theme", "org.gnome.desktop.interface", NULL, "cursor-theme", FF_VARIANT_TYPE_STRING).strValue;
if(cursorSize <= 0)
cursorSize = ffSettingsGet(instance, "/org/gnome/desktop/interface/cursor-size", "org.gnome.desktop.interface", NULL, "cursor-size", FF_VARIANT_TYPE_INT).intValue;
pthread_mutex_unlock(&mutex);
applyGTKDConfSettings(result, themeName, iconsName, fontName, cursorTheme, cursorSize);
}
static void detectGTKFromConfigFile(const char* filename, FFGTKResult* result)
{
ffParsePropFileValues(filename, 5, (FFpropquery[]) {
{"gtk-theme-name =", &result->theme},
{"gtk-icon-theme-name =", &result->icons},
{"gtk-font-name =", &result->font},
{"gtk-cursor-theme-name =", &result->cursor},
{"gtk-cursor-theme-size =", &result->cursorSize}
});
}
static void detectGTKFromConfigDir(FFstrbuf* configDir, const char* version, FFGTKResult* result)
{
//In case of an empty env variable
ffStrbufTrim(configDir, ' ');
if(configDir->length == 0)
return;
uint32_t configDirLength = configDir->length;
// <configdir>/gtk-<version>.0/settings.ini
ffStrbufAppendS(configDir, "/gtk-");
ffStrbufAppendS(configDir, version);
ffStrbufAppendS(configDir, ".0/settings.ini");
detectGTKFromConfigFile(configDir->chars, result);
ffStrbufSubstrBefore(configDir, configDirLength);
if(allPropertiesSet(result))
return;
// <configdir>/gtk-<version>.0/gtkrc
ffStrbufAppendS(configDir, "/gtk-");
ffStrbufAppendS(configDir, version);
ffStrbufAppendS(configDir, ".0/gtkrc");
detectGTKFromConfigFile(configDir->chars, result);
ffStrbufSubstrBefore(configDir, configDirLength);
if(allPropertiesSet(result))
return;
// <configdir>/gtkrc-<version>.0
ffStrbufAppendS(configDir, "/gtkrc-");
ffStrbufAppendS(configDir, version);
ffStrbufAppendS(configDir, ".0");
detectGTKFromConfigFile(configDir->chars, result);
ffStrbufSubstrBefore(configDir, configDirLength);
if(allPropertiesSet(result))
return;
// <configdir>/.gtkrc-<version>.0
ffStrbufAppendS(configDir, "/.gtkrc-");
ffStrbufAppendS(configDir, version);
ffStrbufAppendS(configDir, ".0");
detectGTKFromConfigFile(configDir->chars, result);
ffStrbufSubstrBefore(configDir, configDirLength);
}
static void detectGTK(FFinstance* instance, const char* version, const char* envVariable, FFGTKResult* result)
{
FFstrbuf buffer;
ffStrbufInitA(&buffer, 128);
// From ENV: GTK*_RC_FILES
ffStrbufSetS(&buffer, getenv(envVariable));
uint32_t startIndex = 0;
while (startIndex < buffer.length)
{
uint32_t colonIndex = ffStrbufNextIndexC(&buffer, startIndex, ':');
buffer.chars[colonIndex] = '\0';
detectGTKFromConfigFile(buffer.chars + startIndex, result);
if(allPropertiesSet(result))
{
ffStrbufDestroy(&buffer);
return;
}
startIndex = colonIndex + 1;
}
//From DConf / GSettings
detectGTKFromDConf(instance, result);
if(allPropertiesSet(result))
{
ffStrbufDestroy(&buffer);
return;
}
//From config dirs
//We need to do this because we use multiple threads on configDirs
FFstrbuf baseDirCopy;
ffStrbufInitA(&baseDirCopy, 64);
for(uint32_t i = 0; i < instance->state.configDirs.length; i++)
{
FFstrbuf* baseDir = (FFstrbuf*) ffListGet(&instance->state.configDirs, i);
ffStrbufSet(&baseDirCopy, baseDir);
detectGTKFromConfigDir(&baseDirCopy, version, result);
if(allPropertiesSet(result))
break;
}
ffStrbufDestroy(&baseDirCopy);
ffStrbufDestroy(&buffer);
}
#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 &result; \
} \
init = true; \
ffStrbufInit(&result.theme); \
ffStrbufInit(&result.icons); \
ffStrbufInit(&result.font); \
ffStrbufInit(&result.cursor); \
ffStrbufInit(&result.cursorSize); \
detectGTK(instance, #version, "GTK"#version"_RC_FILES", &result); \
pthread_mutex_unlock(&mutex); \
return &result;
const FFGTKResult* ffDetectGTK2(FFinstance* instance)
{
FF_CALCULATE_GTK_IMPL(2)
}
const FFGTKResult* ffDetectGTK3(FFinstance* instance)
{
FF_CALCULATE_GTK_IMPL(3)
}
const FFGTKResult* ffDetectGTK4(FFinstance* instance)
{
FF_CALCULATE_GTK_IMPL(4)
}
#undef FF_CALCULATE_GTK_IMPL
-143
View File
@@ -1,143 +0,0 @@
#include "fastfetch.h"
#include <string.h>
#include <pthread.h>
typedef enum PlasmaCategory
{
PLASMA_CATEGORY_GENERAL,
PLASMA_CATEGORY_KDE,
PLASMA_CATEGORY_ICONS,
PLASMA_CATEGORY_OTHER
} PlasmaCategory;
static bool detectFromConfigFile(const FFstrbuf* filename, FFPlasmaResult* result)
{
FILE* kdeglobals = fopen(filename->chars, "r");
if(kdeglobals == NULL)
return false;
char* line = NULL;
size_t len = 0;
PlasmaCategory category = PLASMA_CATEGORY_OTHER;
while(getline(&line, &len, kdeglobals) != -1)
{
if(line[0] == '[')
{
char categoryName[32];
sscanf(line, "[%32[^]]", categoryName);
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;
}
if(category == PLASMA_CATEGORY_KDE && result->widgetStyle.length == 0)
ffGetPropValue(line, "widgetStyle =", &result->widgetStyle);
else if(category == PLASMA_CATEGORY_ICONS && result->icons.length == 0)
ffGetPropValue(line, "Theme =", &result->icons);
else if(category == PLASMA_CATEGORY_GENERAL)
{
if(result->colorScheme.length == 0)
ffGetPropValue(line, "ColorScheme =", &result->colorScheme);
if(result->font.length == 0)
ffGetPropValue(line, "font =", &result->font);
//Before plasma 5.23, "Font" was the key instead of "font". Since a lot of distros ship older versions, we test for both.
if(result->font.length == 0)
ffGetPropValue(line, "Font =", &result->font);
}
}
if(line != NULL)
free(line);
fclose(kdeglobals);
return true;
}
const FFPlasmaResult* ffDetectPlasma(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 &result;
}
init = true;
ffStrbufInit(&result.widgetStyle);
ffStrbufInit(&result.colorScheme);
ffStrbufInit(&result.icons);
ffStrbufInit(&result.font);
const FFWMDEResult* wmde = ffDetectWMDE(instance);
if(ffStrbufIgnCaseCompS(&wmde->deProcessName, "plasmashell") != 0)
{
pthread_mutex_unlock(&mutex);
return &result;
}
bool foundAFile = false;
//We need to do this because we use multiple threads on configDirs
FFstrbuf baseDirCopy;
ffStrbufInitA(&baseDirCopy, 64);
for(uint32_t i = 0; i < instance->state.configDirs.length; i++)
{
FFstrbuf* baseDir = (FFstrbuf*) ffListGet(&instance->state.configDirs, i);
ffStrbufSet(&baseDirCopy, baseDir);
ffStrbufAppendS(&baseDirCopy, "/kdeglobals");
if(detectFromConfigFile(&baseDirCopy, &result))
foundAFile = true;
if(
result.widgetStyle.length > 0 &&
result.colorScheme.length > 0 &&
result.icons.length > 0 &&
result.font.length > 0
) break;
}
ffStrbufDestroy(&baseDirCopy);
if(!foundAFile)
{
pthread_mutex_unlock(&mutex);
return &result;
}
//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");
pthread_mutex_unlock(&mutex);
return &result;
}
-267
View File
@@ -1,267 +0,0 @@
#include "fastfetch.h"
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
static void setExeName(FFstrbuf* exe, const char** exeName)
{
uint32_t lastSlashIndex = ffStrbufLastIndexC(exe, '/');
if(lastSlashIndex < exe->length)
*exeName = exe->chars + lastSlashIndex + 1;
}
static void getProcessInformation(const char* pid, FFstrbuf* processName, FFstrbuf* exe, const char** exeName)
{
FFstrbuf cmdlineFilePath;
ffStrbufInit(&cmdlineFilePath);
ffStrbufAppendS(&cmdlineFilePath, "/proc/");
ffStrbufAppendS(&cmdlineFilePath, pid);
ffStrbufAppendS(&cmdlineFilePath, "/cmdline");
ffGetFileContent(cmdlineFilePath.chars, exe);
ffStrbufSubstrBeforeFirstC(exe, '\0'); //Trim the arguments
ffStrbufTrimLeft(exe, '-'); //Happens in TTY
if(exe->length == 0)
ffStrbufSet(exe, processName);
setExeName(exe, exeName);
ffStrbufDestroy(&cmdlineFilePath);
}
static void getTerminalShell(FFTerminalShellResult* result, const char* pid)
{
FFstrbuf statFilePath;
ffStrbufInit(&statFilePath);
ffStrbufAppendS(&statFilePath, "/proc/");
ffStrbufAppendS(&statFilePath, pid);
ffStrbufAppendS(&statFilePath, "/stat");
FILE* stat = fopen(statFilePath.chars, "r");
ffStrbufDestroy(&statFilePath);
if(stat == NULL)
return;
char name[256];
name[0] = '\0';
char ppid[256];
ppid[0] = '\0';
if(
fscanf(stat, "%*s (%255[^)]) %*c %255s", name, ppid) != 2 || //stat (comm) state ppid
*name == '\0' ||
*ppid == '\0' ||
*ppid == '-' ||
strcasecmp(ppid, "0") == 0
) {
fclose(stat);
return;
}
fclose(stat);
//Common programs that are between terminal and own process, but are not the shell
if(
strcasecmp(name, "sudo") == 0 ||
strcasecmp(name, "su") == 0 ||
strcasecmp(name, "doas") == 0 ||
strcasecmp(name, "strace") == 0 ||
strcasecmp(name, "sshd") == 0 ||
strcasecmp(name, "gdb") == 0
) {
getTerminalShell(result, ppid);
return;
}
//Known shells
if(
strcasecmp(name, "bash") == 0 ||
strcasecmp(name, "sh") == 0 ||
strcasecmp(name, "zsh") == 0 ||
strcasecmp(name, "ksh") == 0 ||
strcasecmp(name, "fish") == 0 ||
strcasecmp(name, "dash") == 0 ||
strcasecmp(name, "git-shell") == 0
) {
ffStrbufAppendS(&result->shellProcessName, name);
getProcessInformation(pid, &result->shellProcessName, &result->shellExe, &result->shellExeName);
getTerminalShell(result, ppid);
return;
}
ffStrbufAppendS(&result->terminalProcessName, name);
getProcessInformation(pid, &result->terminalProcessName, &result->terminalExe, &result->terminalExeName);
}
static void getTerminalFromEnv(FFTerminalShellResult* result)
{
if(
result->terminalProcessName.length > 0 &&
!ffStrbufStartsWithIgnCaseS(&result->terminalProcessName, "login") &&
ffStrbufIgnCaseCompS(&result->terminalProcessName, "(login)") != 0 &&
ffStrbufIgnCaseCompS(&result->terminalProcessName, "systemd") != 0 &&
ffStrbufIgnCaseCompS(&result->terminalProcessName, "init") != 0 &&
ffStrbufIgnCaseCompS(&result->terminalProcessName, "(init)") != 0 &&
ffStrbufIgnCaseCompS(&result->terminalProcessName, "0") != 0
) return;
char* term = NULL;
//SSH
if(getenv("SSH_CONNECTION") != NULL)
term = getenv("SSH_TTY");
//Windows Terminal
if((term == NULL || *term == '\0') && getenv("WT_SESSION") != NULL)
term = "Windows Terminal";
//Normal Terminal
if(term == NULL || *term == '\0')
term = getenv("TERM");
//TTY
if(term == NULL || *term == '\0' || strcasecmp(term, "linux") == 0)
term = ttyname(STDIN_FILENO);
ffStrbufSetS(&result->terminalProcessName, term);
ffStrbufSetS(&result->terminalExe, term);
setExeName(&result->terminalExe, &result->terminalExeName);
}
static void getUserShellFromEnv(FFTerminalShellResult* result)
{
ffStrbufAppendS(&result->userShellExe, getenv("SHELL"));
setExeName(&result->userShellExe, &result->userShellExeName);
//If shell detection via processes failed
if(result->shellProcessName.length == 0 && result->userShellExe.length > 0)
{
ffStrbufAppendS(&result->shellProcessName, result->userShellExeName);
ffStrbufSet(&result->shellExe, &result->userShellExe);
setExeName(&result->shellExe, &result->shellExeName);
}
}
static void getShellVersionBash(FFstrbuf* exe, FFstrbuf* version)
{
ffProcessAppendStdOut(version, (char* const[]) {
"env",
"-i",
exe->chars,
"--norc",
"--noprofile",
"-c",
"printf \"%s\" \"$BASH_VERSION\"",
NULL
});
ffStrbufSubstrBeforeFirstC(version, '(');
}
static void getShellVersionZsh(FFstrbuf* exe, FFstrbuf* version)
{
ffProcessAppendStdOut(version, (char* const[]) {
exe->chars,
"--version",
NULL
});
ffStrbufTrimRight(version, '\n');
ffStrbufSubstrBeforeLastC(version, ' ');
ffStrbufSubstrAfterFirstC(version, ' ');
}
static void getShellVersionFish(FFstrbuf* exe, FFstrbuf* version)
{
ffProcessAppendStdOut(version, (char* const[]) {
exe->chars,
"--version",
NULL
});
ffStrbufTrimRight(version, '\n');
ffStrbufSubstrAfterLastC(version, ' ');
}
static void getShellVersionGeneric(FFstrbuf* exe, const char* exeName, FFstrbuf* version)
{
FFstrbuf command;
ffStrbufInit(&command);
ffStrbufAppendS(&command, "printf \"%s\" \"$");
ffStrbufAppendTransformS(&command, exeName, toupper);
ffStrbufAppendS(&command, "_VERSION\"");
ffProcessAppendStdOut(version, (char* const[]) {
"env",
"-i",
exe->chars,
"-c",
command.chars,
NULL
});
ffStrbufSubstrBeforeFirstC(version, '(');
ffStrbufRemoveStrings(version, 2, "-release", "release");
ffStrbufDestroy(&command);
}
static void getShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version)
{
if(strcasecmp(exeName, "bash") == 0)
getShellVersionBash(exe, version);
else if(strcasecmp(exeName, "zsh") == 0)
getShellVersionZsh(exe, version);
else if(strcasecmp(exeName, "fish") == 0)
getShellVersionFish(exe, version);
else
getShellVersionGeneric(exe, exeName, version);
}
const FFTerminalShellResult* ffDetectTerminalShell(FFinstance* instance)
{
FF_UNUSED(instance);
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static FFTerminalShellResult result;
static bool init = false;
pthread_mutex_lock(&mutex);
if(init)
{
pthread_mutex_unlock(&mutex);
return &result;
}
init = true;
ffStrbufInit(&result.shellProcessName);
ffStrbufInitA(&result.shellExe, 128);
result.shellExeName = result.shellExe.chars;
ffStrbufInit(&result.shellVersion);
ffStrbufInit(&result.terminalProcessName);
ffStrbufInitA(&result.terminalExe, 128);
result.terminalExeName = result.terminalExe.chars;
ffStrbufInit(&result.userShellExe);
result.userShellExeName = result.userShellExe.chars;
ffStrbufInit(&result.userShellVersion);
char ppid[256];
snprintf(ppid, 255, "%i", getppid());
getTerminalShell(&result, ppid);
getTerminalFromEnv(&result);
getUserShellFromEnv(&result);
getShellVersion(&result.shellExe, result.shellExeName, &result.shellVersion);
if(strcasecmp(result.shellExeName, result.userShellExeName) != 0)
getShellVersion(&result.userShellExe, result.userShellExeName, &result.userShellVersion);
else
ffStrbufSet(&result.userShellVersion, &result.shellVersion);
pthread_mutex_unlock(&mutex);
return &result;
}
-414
View File
@@ -1,414 +0,0 @@
#include "fastfetch.h"
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <pthread.h>
typedef enum ProtocolHint
{
FF_PROTOCOL_HINT_UNKNOWN = 0,
FF_PROTOCOL_HINT_X11,
FF_PROTOCOL_HINT_WAYLAND
} ProtocolHint;
static void getSessionDesktop(FFWMDEResult* result)
{
result->sessionDesktop = getenv("XDG_CURRENT_DESKTOP");
if(result->sessionDesktop != NULL && *result->sessionDesktop != '\0')
return;
result->sessionDesktop = getenv("XDG_SESSION_DESKTOP");
if(result->sessionDesktop != NULL && *result->sessionDesktop != '\0')
return;
result->sessionDesktop = getenv("CURRENT_DESKTOP");
if(result->sessionDesktop != NULL && *result->sessionDesktop != '\0')
return;
result->sessionDesktop = getenv("SESSION_DESKTOP");
if(result->sessionDesktop != NULL && *result->sessionDesktop != '\0')
return;
char* env;
env = getenv("DESKTOP_SESSION");
if(env != NULL && *env != '\0')
{
if(strcasecmp(env, "plasma") == 0)
result->sessionDesktop = "KDE";
else
result->sessionDesktop = env;
return;
}
env = getenv("GNOME_DESKTOP_SESSION_ID");
if(env != NULL)
{
result->sessionDesktop = "Gnome";
return;
}
env = getenv("MATE_DESKTOP_SESSION_ID");
if(env != NULL)
{
result->sessionDesktop = "Mate";
return;
}
env = getenv("TDE_FULL_SESSION");
if(env != NULL)
{
result->sessionDesktop = "Trinity";
return;
}
}
static void applyPrettyNameIfWM(FFWMDEResult* result, const char* processName, ProtocolHint* protocolHint)
{
if(processName == NULL || *processName == '\0')
return;
if(strcasecmp(processName, "kwin_wayland") == 0)
{
ffStrbufSetS(&result->wmPrettyName, "KWin");
*protocolHint = FF_PROTOCOL_HINT_WAYLAND;
}
else if(strcasecmp(processName, "kwin_x11") == 0)
{
ffStrbufSetS(&result->wmPrettyName, "KWin");
*protocolHint = FF_PROTOCOL_HINT_X11;
}
else if(strcasecmp(processName, "sway") == 0)
{
ffStrbufSetS(&result->wmPrettyName, "Sway");
*protocolHint = FF_PROTOCOL_HINT_WAYLAND;
}
else if(strcasecmp(processName, "weston") == 0)
{
ffStrbufSetS(&result->wmPrettyName, "Weston");
*protocolHint = FF_PROTOCOL_HINT_WAYLAND;
}
else if(strcasecmp(processName, "wayfire") == 0)
{
ffStrbufSetS(&result->wmPrettyName, "Wayfire");
*protocolHint = FF_PROTOCOL_HINT_WAYLAND;
}
else if(strcasecmp(processName, "openbox") == 0)
{
ffStrbufSetS(&result->wmPrettyName, "Openbox");
*protocolHint = FF_PROTOCOL_HINT_X11;
}
else if(strcasecmp(processName, "xfwm4") == 0)
{
ffStrbufSetS(&result->wmPrettyName, "Xfwm4");
*protocolHint = FF_PROTOCOL_HINT_X11;
}
else if(strcasecmp(processName, "Marco") == 0)
{
ffStrbufSetS(&result->wmPrettyName, "Marco");
*protocolHint = FF_PROTOCOL_HINT_X11;
}
else if(strcasecmp(processName, "dwm") == 0)
{
ffStrbufSetS(&result->wmPrettyName, "dwm");
*protocolHint = FF_PROTOCOL_HINT_X11;
}
else if(strcasecmp(processName, "gnome-session-binary") == 0 || strcasecmp(processName, "Mutter") == 0)
ffStrbufSetS(&result->wmPrettyName, "Mutter");
else if(strcasecmp(processName, "cinnamon-session") == 0 || strcasecmp(processName, "Muffin") == 0)
ffStrbufSetS(&result->wmPrettyName, "Muffin");
if(result->wmPrettyName.length > 0 && result->wmProcessName.length == 0)
ffStrbufSetS(&result->wmProcessName, processName);
}
static void applyBetterWM(FFWMDEResult* result, const char* processName, ProtocolHint* protocolHint)
{
if(processName == NULL || *processName == '\0')
return;
ffStrbufSetS(&result->wmProcessName, processName);
applyPrettyNameIfWM(result, processName, protocolHint);
if(result->wmPrettyName.length == 0)
ffStrbufAppend(&result->wmPrettyName, &result->wmProcessName);
}
static void getKDE(FFWMDEResult* result, ProtocolHint* protocolHint)
{
ffStrbufSetS(&result->deProcessName, "plasmashell");
ffStrbufSetS(&result->dePrettyName, "KDE Plasma");
ffParsePropFile("/usr/share/xsessions/plasma.desktop", "X-KDE-PluginInfo-Version =", &result->deVersion);
applyBetterWM(result, getenv("KDEWM"), protocolHint);
}
static void getGnome(FFWMDEResult* result)
{
ffStrbufSetS(&result->deProcessName, "gnome-shell");
ffStrbufSetS(&result->dePrettyName, "GNOME");
ffParsePropFile("/usr/share/gnome-shell/org.gnome.Extensions", "version :", &result->deVersion);
}
static void getCinnamon(FFWMDEResult* result)
{
ffStrbufSetS(&result->deProcessName, "cinnamon");
ffStrbufSetS(&result->dePrettyName, "Cinnamon");
ffParsePropFile("/usr/share/applications/cinnamon.desktop", "X-GNOME-Bugzilla-Version =", &result->deVersion);
}
static void getMate(FFinstance* instance, FFWMDEResult* result)
{
ffStrbufSetS(&result->deProcessName, "mate-session");
ffStrbufSetS(&result->dePrettyName, "MATE");
FFstrbuf major; ffStrbufInit(&major);
FFstrbuf minor; ffStrbufInit(&minor);
FFstrbuf micro; ffStrbufInit(&micro);
ffParsePropFileValues("/usr/share/mate-about/mate-version.xml", 3, (FFpropquery[]) {
{"<platform>", &major},
{"<minor>", &minor},
{"<micro>", &micro}
});
ffParseSemver(&result->deVersion, &major, &minor, &micro);
ffStrbufDestroy(&major);
ffStrbufDestroy(&minor);
ffStrbufDestroy(&micro);
if(result->deVersion.length == 0 && instance->config.allowSlowOperations)
{
ffProcessAppendStdOut(&result->deVersion, (char* const[]){
"mate-session",
"--version",
NULL
});
ffStrbufSubstrAfterFirstC(&result->deVersion, ' ');
ffStrbufTrim(&result->deVersion, ' ');
}
}
static void getXFCE4(FFinstance* instance, FFWMDEResult* result)
{
ffStrbufSetS(&result->deProcessName, "xfce4-session");
ffStrbufSetS(&result->dePrettyName, "Xfce4");
ffParsePropFile("/usr/share/gtk-doc/html/libxfce4ui/index.html", "<div><p class=\"releaseinfo\">Version", &result->deVersion);
if(result->deVersion.length == 0 && instance->config.allowSlowOperations)
{
//This is somewhat slow
ffProcessAppendStdOut(&result->deVersion, (char* const[]){
"xfce4-session",
"--version",
NULL
});
ffStrbufSubstrBeforeFirstC(&result->deVersion, '(');
ffStrbufSubstrAfterFirstC(&result->deVersion, ' ');
ffStrbufTrim(&result->deVersion, ' ');
}
}
static void getLXQt(FFinstance* instance, FFWMDEResult* result, ProtocolHint* protocolHint)
{
ffStrbufSetS(&result->deProcessName, "lxqt-session");
ffStrbufSetS(&result->dePrettyName, "LXQt");
ffParsePropFile("/usr/lib/pkgconfig/lxqt.pc", "Version:", &result->deVersion);
if(result->deVersion.length == 0)
ffParsePropFile("/usr/share/cmake/lxqt/lxqt-config.cmake", "set ( LXQT_VERSION", &result->deVersion);
if(result->deVersion.length == 0)
ffParsePropFile("/usr/share/cmake/lxqt/lxqt-config-version.cmake", "set ( PACKAGE_VERSION", &result->deVersion);
if(result->deVersion.length == 0 && instance->config.allowSlowOperations)
{
//This is really, really, really slow. Thank you, LXQt developers
ffProcessAppendStdOut(&result->deVersion, (char* const[]){
"lxqt-session",
"-v",
NULL
});
result->deVersion.length = 0; //don't set '\0' byte
ffGetPropValueFromLines(result->deVersion.chars , "liblxqt", &result->deVersion);
}
FFstrbuf wmProcessNameBuffer;
ffStrbufInit(&wmProcessNameBuffer);
ffParsePropFileConfig(instance, "lxqt/session.conf", "window_manager =", &wmProcessNameBuffer);
applyBetterWM(result, wmProcessNameBuffer.chars, protocolHint);
ffStrbufDestroy(&wmProcessNameBuffer);
}
static void applyPrettyNameIfDE(FFinstance* instance, FFWMDEResult* result, const char* name, ProtocolHint* protocolHint)
{
if(name == NULL || *name == '\0')
return;
if(strcasecmp(name, "KDE") == 0 || strcasecmp(name, "plasma") == 0 || strcasecmp(name, "plasmashell") == 0)
getKDE(result, protocolHint);
else if(strcasecmp(name, "Gnome") == 0 || strcasecmp(name, "ubuntu:GNOME") == 0 || strcasecmp(name, "ubuntu") == 0 || strcasecmp(name, "gnome-shell") == 0)
getGnome(result);
else if(strcasecmp(name, "X-Cinnamon") == 0 || strcasecmp(name, "Cinnamon") == 0)
getCinnamon(result);
else if(strcasecmp(name, "XFCE") == 0 || strcasecmp(name, "X-XFCE") == 0 || strcasecmp(name, "XFCE4") == 0 || strcasecmp(name, "X-XFCE4") == 0 || strcasecmp(name, "xfce4-session") == 0)
getXFCE4(instance, result);
else if(strcasecmp(name, "MATE") == 0 || strcasecmp(name, "X-MATE") == 0 || strcasecmp(name, "mate-session") == 0)
getMate(instance, result);
else if(strcasecmp(name, "LXQt") == 0 || strcasecmp(name, "X-LXQT") == 0 || strcasecmp(name, "lxqt-session") == 0)
getLXQt(instance, result, protocolHint);
}
static void getSessionTypeFromHint(FFWMDEResult* result, ProtocolHint protocolHint)
{
if(
result->wmProtocolName.length > 0 ||
protocolHint == FF_PROTOCOL_HINT_UNKNOWN
) return;
if(protocolHint == FF_PROTOCOL_HINT_WAYLAND)
ffStrbufSetS(&result->wmProtocolName, "Wayland");
else if(protocolHint == FF_PROTOCOL_HINT_X11)
ffStrbufSetS(&result->wmProtocolName, "X11");
}
static void getSessionTypeFromEnv(FFWMDEResult* result)
{
const char* env = getenv("XDG_SESSION_TYPE");
if(env != NULL && *env != '\0')
{
if(strcasecmp(env, "wayland") == 0)
ffStrbufSetS(&result->wmProtocolName, "Wayland");
else if(strcasecmp(env, "x11") == 0)
ffStrbufSetS(&result->wmProtocolName, "X11");
else if(strcasecmp(env, "tty") == 0)
ffStrbufSetS(&result->wmProtocolName, "TTY");
else if(strcasecmp(env, "mir") == 0)
ffStrbufSetS(&result->wmProtocolName, "Mir");
else
ffStrbufSetS(&result->wmProtocolName, env);
return;
}
env = getenv("WAYLAND_DISPLAY");
if(env != NULL && *env != '\0')
{
ffStrbufSetS(&result->wmProtocolName, "Wayland");
return;
}
env = getenv("TERM");
if(env != NULL && *env != '\0')
{
ffStrbufSetS(&result->wmProtocolName, "TTY");
return;
}
}
static void getFromProcDir(FFinstance* instance, FFWMDEResult* result, ProtocolHint* protocolHint)
{
DIR* proc = opendir("/proc");
if(proc == NULL)
return;
FFstrbuf procPath;
ffStrbufInitA(&procPath, 64);
ffStrbufAppendS(&procPath, "/proc/");
uint32_t procPathLength = procPath.length;
FFstrbuf processName;
ffStrbufInitA(&processName, 256); //Some processes have large command lines (looking at you chrome)
struct dirent* dirent;
while((dirent = readdir(proc)) != NULL)
{
if(dirent->d_type != DT_DIR || dirent->d_name[0] == '.')
continue;
ffStrbufAppendS(&procPath, dirent->d_name);
ffStrbufAppendS(&procPath, "/cmdline");
ffGetFileContent(procPath.chars, &processName);
ffStrbufSubstrBeforeFirstC(&processName, '\0'); //Trim the arguments
ffStrbufSubstrAfterLastC(&processName, '/');
ffStrbufSubstrBefore(&procPath, procPathLength);
if(result->dePrettyName.length == 0)
applyPrettyNameIfDE(instance, result, processName.chars, protocolHint);
if(result->wmPrettyName.length == 0)
applyPrettyNameIfWM(result, processName.chars, protocolHint);
if(result->deProcessName.length > 0 && result->wmPrettyName.length > 0)
break;
}
closedir(proc);
ffStrbufDestroy(&processName);
ffStrbufDestroy(&procPath);
}
void getWMDE(FFinstance* instance, FFWMDEResult* result)
{
ProtocolHint protocolHint;
//If sessionDesktop is a known DE, set it. Some DEs even have config files which tell us the WM, so we can return very fast in this case
applyPrettyNameIfDE(instance, result, result->sessionDesktop, &protocolHint);
if(result->dePrettyName.length > 0 && result->wmPrettyName.length > 0)
{
getSessionTypeFromHint(result, protocolHint);
return;
}
//If sessionDesktop is a known WM, set it.
applyPrettyNameIfWM(result, result->sessionDesktop, &protocolHint);
//Search for missing DE / WM in processes
getFromProcDir(instance, result, &protocolHint);
//Fallback for unknown WMs and DEs. This will falsely detect DEs as WMs if they (and their used WM) are unknown
if(result->wmPrettyName.length == 0 && result->dePrettyName.length == 0)
{
ffStrbufSetS(&result->wmProcessName, result->sessionDesktop);
ffStrbufSetS(&result->wmPrettyName, result->sessionDesktop);
}
getSessionTypeFromHint(result, protocolHint);
}
const FFWMDEResult* ffDetectWMDE(FFinstance* instance)
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static FFWMDEResult result;
static bool init = false;
pthread_mutex_lock(&mutex);
if(init)
{
pthread_mutex_unlock(&mutex);
return &result;
}
init = true;
ffStrbufInit(&result.wmProcessName);
ffStrbufInit(&result.wmPrettyName);
ffStrbufInit(&result.wmProtocolName);
ffStrbufInit(&result.deProcessName);
ffStrbufInit(&result.dePrettyName);
ffStrbufInit(&result.deVersion);
getSessionDesktop(&result);
getSessionTypeFromEnv(&result);
//Don't run anyting when on TTY. This prevents us to catch process from other users in at least that case.
if(ffStrbufIgnCaseCompS(&result.wmProtocolName, "TTY") != 0)
getWMDE(instance, &result);
pthread_mutex_unlock(&mutex);
return &result;
}