better handling of WM / DE

This commit is contained in:
Linus Dierheimer
2021-04-25 21:32:35 +02:00
parent e9cf1c0252
commit 2a29d3ed17
5 changed files with 156 additions and 108 deletions
+94 -41
View File
@@ -1,8 +1,89 @@
#include "fastfetch.h"
#include <string.h>
#include <dirent.h>
#include <pthread.h>
const char* ffGetSessionDesktop()
{
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static char* sessionDesktop = NULL;
pthread_mutex_lock(&mutex);
if(sessionDesktop == NULL)
sessionDesktop = getenv("XDG_CURRENT_DESKTOP");
if(sessionDesktop == NULL)
sessionDesktop = getenv("XDG_SESSION_DESKTOP");
pthread_mutex_unlock(&mutex);
return sessionDesktop;
}
static void getFromProcDir(FFWMResult* result)
{
DIR* proc = opendir("/proc/");
if(proc == NULL)
return;
struct dirent* dirent;
while((dirent = readdir(proc)) != NULL)
{
if(dirent->d_type != DT_DIR)
continue;
char path[20];
sprintf(path, "/proc/%.8s/comm", dirent->d_name);
ffGetFileContent(path, &result->processName);
if(ffStrbufIgnCaseCompS(&result->processName, "kwin_wayland") == 0)
{
ffStrbufSetS(&result->prettyName, "KWin");
ffStrbufSetS(&result->protocolName, "Wayland");
}
else if(ffStrbufIgnCaseCompS(&result->processName, "kwin_x11") == 0)
{
ffStrbufSetS(&result->prettyName, "KWin");
ffStrbufSetS(&result->protocolName, "X11");
}
else if(ffStrbufIgnCaseCompS(&result->processName, "openbox") == 0)
ffStrbufSetS(&result->prettyName, "Openbox");
else if(ffStrbufIgnCaseCompS(&result->processName, "cinnamon") == 0)
ffStrbufSetS(&result->prettyName, "Muffin");
else if(ffStrbufIgnCaseCompS(&result->processName, "xfwm4") == 0)
ffStrbufSetS(&result->prettyName, "XFWM");
else if(ffStrbufIgnCaseCompS(&result->processName, "wayfire") == 0)
ffStrbufSetS(&result->prettyName, "Wayfire");
if(result->prettyName.length > 0)
break;
}
closedir(proc);
}
static void getSessionType(FFstrbuf* sessionType)
{
const char* xdgSessionType = getenv("XDG_SESSION_TYPE");
if (xdgSessionType == NULL)
return;
else if(strcasecmp(xdgSessionType, "wayland") == 0)
ffStrbufSetS(sessionType, "Wayland");
else if(strcasecmp(xdgSessionType, "x11") == 0)
ffStrbufSetS(sessionType, "X11");
else if(strcasecmp(xdgSessionType, "tty") == 0)
ffStrbufSetS(sessionType, "TTY");
else if(strcasecmp(xdgSessionType, "mir") == 0)
ffStrbufSetS(sessionType, "Mir");
else
ffStrbufSetS(sessionType, xdgSessionType);
}
const FFWMResult* ffCalculateWM(FFinstance* instance)
{
UNUSED(instance);
@@ -20,53 +101,25 @@ const FFWMResult* ffCalculateWM(FFinstance* instance)
ffStrbufInit(&result.processName);
ffStrbufInit(&result.prettyName);
ffStrbufInit(&result.protocolName);
ffStrbufInit(&result.error);
DIR* proc = opendir("/proc/");
if(proc == NULL)
{
ffStrbufSetS(&result.error, "opendir(\"/proc/\") == NULL");
pthread_mutex_unlock(&mutex);
return &result;
}
struct dirent* dirent;
while((dirent = readdir(proc)) != NULL)
{
if(dirent->d_type != DT_DIR)
continue;
char path[20];
sprintf(path, "/proc/%.8s/comm", dirent->d_name);
ffGetFileContent(path, &result.processName);
if(ffStrbufIgnCaseCompS(&result.processName, "kwin_wayland") == 0 || ffStrbufIgnCaseCompS(&result.processName, "kwin_x11") == 0)
ffStrbufSetS(&result.prettyName, "KWin");
if(ffStrbufIgnCaseCompS(&result.processName, "openbox") == 0)
ffStrbufSetS(&result.prettyName, "Openbox");
if(ffStrbufIgnCaseCompS(&result.processName, "cinnamon") == 0)
ffStrbufSetS(&result.prettyName, "Muffin");
if(ffStrbufIgnCaseCompS(&result.processName, "xfwm4") == 0)
ffStrbufSetS(&result.prettyName, "XFCE Window Manager");
if(ffStrbufIgnCaseCompS(&result.processName, "wayfire") == 0)
ffStrbufSetS(&result.prettyName, "Wayfire");
if(result.prettyName.length > 0)
break;
}
closedir(proc);
getFromProcDir(&result);
if(result.prettyName.length == 0)
{
ffStrbufSetS(&result.error, "No process name matches the name of known display managers");
const char* sessionDesktop = ffGetSessionDesktop();
ffStrbufSetS(&result.processName, sessionDesktop);
ffStrbufSetS(&result.prettyName, sessionDesktop);
}
if(result.protocolName.length == 0)
getSessionType(&result.protocolName);
if(result.prettyName.length == 0 && result.protocolName.length == 0)
{
ffStrbufClear(&result.processName);
ffStrbufSetS(&result.error, "No WM or WM protocol type found");
}
pthread_mutex_unlock(&mutex);
+6 -5
View File
@@ -301,16 +301,17 @@ static inline void printCommandHelp(const char* command)
else if(strcasecmp(command, "de-format") == 0)
{
constructAndPrintCommandHelpFormat("de", "{} {} ({})", 3,
"DE name",
"DE version",
"Name of display server"
"Session desktop",
"Session name",
"Session version"
);
}
else if(strcasecmp(command, "wm-format") == 0)
{
constructAndPrintCommandHelpFormat("wm", "{2}", 2,
constructAndPrintCommandHelpFormat("wm", "{2} {3}", 3,
"WM process name",
"WM pretty name"
"WM pretty name",
"WM protocol name"
);
}
else if(strcasecmp(command, "wm-theme-format") == 0)
+2
View File
@@ -145,6 +145,7 @@ typedef struct FFWMResult
{
FFstrbuf processName;
FFstrbuf prettyName;
FFstrbuf protocolName;
FFstrbuf error;
} FFWMResult;
@@ -258,6 +259,7 @@ const FFGTKResult* ffCalculateGTK3(FFinstance* instance);
const FFWMResult* ffCalculateWM(FFinstance* instance);
//common/calculateTerminal.c
const char* ffGetSessionDesktop();
const FFTerminalResult* ffCalculateTerminal(FFinstance* instance);
/********************/
+31 -51
View File
@@ -5,10 +5,8 @@
#define FF_DE_MODULE_NAME "DE"
#define FF_DE_NUM_FORMAT_ARGS 3
static void getKDE(FFstrbuf* name, FFstrbuf* version, FFstrbuf* type)
static void getKDE(FFstrbuf* name, FFstrbuf* version)
{
UNUSED(type);
ffStrbufSetS(name, "KDE Plasma");
char versionBuf[256];
@@ -19,76 +17,58 @@ static void getKDE(FFstrbuf* name, FFstrbuf* version, FFstrbuf* type)
void ffPrintDesktopEnvironment(FFinstance* instance)
{
FF_STRBUF_CREATE(sessionDesktop);
ffStrbufAppendS(&sessionDesktop, getenv("XDG_CURRENT_DESKTOP"));
const char* sessionDesktop = ffGetSessionDesktop();
if(sessionDesktop.length == 0)
ffStrbufSetS(&sessionDesktop, getenv("XDG_SESSION_DESKTOP"));
FF_STRBUF_CREATE(sessionVersion);
FF_STRBUF_CREATE(sessionType);
if(ffStrbufIgnCaseCompS(&sessionDesktop, "KDE") == 0)
getKDE(&sessionDesktop, &sessionVersion, &sessionType);
if(sessionType.length == 0)
{
const char* xdgSessionType = getenv("XDG_SESSION_TYPE");
if(strcasecmp(xdgSessionType, "wayland") == 0)
ffStrbufSetS(&sessionType, "Wayland");
else if(strcasecmp(xdgSessionType, "x11") == 0)
ffStrbufSetS(&sessionType, "X11");
else if(strcasecmp(xdgSessionType, "tty") == 0)
ffStrbufSetS(&sessionType, "TTY");
else if(strcasecmp(xdgSessionType, "mir") == 0)
ffStrbufSetS(&sessionType, "Mir");
else if(xdgSessionType != NULL)
ffStrbufSetS(&sessionType, xdgSessionType);
}
if(sessionDesktop.length == 0 && sessionType.length == 0)
if(sessionDesktop == NULL)
{
ffPrintError(instance, FF_DE_MODULE_NAME, 0, &instance->config.deKey, &instance->config.deFormat, FF_DE_NUM_FORMAT_ARGS, "No relevant XDG_SESSION_* environment variable set");
ffStrbufDestroy(&sessionDesktop);
ffStrbufDestroy(&sessionVersion);
ffStrbufDestroy(&sessionType);
return;
}
const FFWMResult* wm = ffCalculateWM(instance);
// test if we are running only a WM
if(
ffStrbufIgnCaseCompS(&wm->processName, sessionDesktop) == 0 ||
ffStrbufIgnCaseCompS(&wm->prettyName, sessionDesktop) == 0
) return;
FFstrbuf sessionName;
ffStrbufInit(&sessionName);
FFstrbuf sessionVersion;
ffStrbufInit(&sessionVersion);
if(strcasecmp(sessionDesktop, "KDE") == 0)
getKDE(&sessionName, &sessionVersion);
if(instance->config.deFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_DE_MODULE_NAME, 0, &instance->config.deKey);
if(sessionDesktop.length > 0)
if(sessionName.length > 0)
ffStrbufWriteTo(&sessionName, stdout);
else
fputs(sessionDesktop, stdout);
if(sessionVersion.length > 0)
{
ffStrbufWriteTo(&sessionDesktop, stdout);
if(sessionVersion.length > 0)
{
putchar(' ');
ffStrbufWriteTo(&sessionVersion, stdout);
}
if(sessionType.length > 0)
putchar(' ');
putchar(' ');
ffStrbufWriteTo(&sessionVersion, stdout);
}
if(sessionType.length > 0)
printf("(%s)", sessionType.chars);
putchar('\n');
}
else
{
ffPrintFormatString(instance, FF_DE_MODULE_NAME, 0, &instance->config.deKey, &instance->config.deFormat, NULL, FF_DE_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &sessionDesktop},
{FF_FORMAT_ARG_TYPE_STRING, sessionDesktop},
{FF_FORMAT_ARG_TYPE_STRBUF, &sessionName},
{FF_FORMAT_ARG_TYPE_STRBUF, &sessionVersion},
{FF_FORMAT_ARG_TYPE_STRBUF, &sessionType}
});
}
ffStrbufDestroy(&sessionDesktop);
ffStrbufDestroy(&sessionName);
ffStrbufDestroy(&sessionVersion);
ffStrbufDestroy(&sessionType);
}
+23 -11
View File
@@ -1,7 +1,7 @@
#include "fastfetch.h"
#define FF_WM_MODULE_NAME "WM"
#define FF_WM_NUM_FORMAT_ARGS 2
#define FF_WM_NUM_FORMAT_ARGS 3
void ffPrintWM(FFinstance* instance)
{
@@ -13,25 +13,37 @@ void ffPrintWM(FFinstance* instance)
return;
}
if(result->prettyName.length == 0 && result->processName.length == 0)
{
ffPrintError(instance, FF_WM_MODULE_NAME, 0, &instance->config.wmKey, &instance->config.wmFormat, FF_WM_NUM_FORMAT_ARGS, "No wm name provided");
return;
}
if(instance->config.wmFormat.length == 0)
{
ffPrintLogoAndKey(instance, "WM", 0, &instance->config.wmKey);
if(result->prettyName.length > 0)
ffStrbufPutTo(&result->prettyName, stdout);
if(result->prettyName.length == 0 && result->processName.length == 0)
{
ffStrbufPutTo(&result->protocolName, stdout);
}
else
ffStrbufPutTo(&result->processName, stdout);
{
if(result->prettyName.length > 0)
ffStrbufWriteTo(&result->prettyName, stdout);
else
ffStrbufWriteTo(&result->processName, stdout);
if(result->protocolName.length > 0)
{
fputs(" (", stdout);
ffStrbufWriteTo(&result->protocolName, stdout);
putchar(')');
}
putchar('\n');
}
}
else
{
ffPrintFormatString(instance, FF_WM_MODULE_NAME, 0, &instance->config.wmKey, &instance->config.wmFormat, NULL, FF_WM_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &result->processName},
{FF_FORMAT_ARG_TYPE_STRBUF, &result->prettyName}
{FF_FORMAT_ARG_TYPE_STRBUF, &result->prettyName},
{FF_FORMAT_ARG_TYPE_STRBUF, &result->protocolName}
});
}
}