From 2a29d3ed178679601479e305cc83be037f6ba4bd Mon Sep 17 00:00:00 2001 From: Linus Dierheimer Date: Sun, 25 Apr 2021 21:32:35 +0200 Subject: [PATCH] better handling of WM / DE --- src/common/calculateWM.c | 135 +++++++++++++++++++++++++++------------ src/fastfetch.c | 11 ++-- src/fastfetch.h | 2 + src/modules/de.c | 82 +++++++++--------------- src/modules/wm.c | 34 ++++++---- 5 files changed, 156 insertions(+), 108 deletions(-) diff --git a/src/common/calculateWM.c b/src/common/calculateWM.c index a7795eead..b9c46262c 100644 --- a/src/common/calculateWM.c +++ b/src/common/calculateWM.c @@ -1,8 +1,89 @@ #include "fastfetch.h" +#include #include #include +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); diff --git a/src/fastfetch.c b/src/fastfetch.c index 9e91c77d3..98ace55e3 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -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) diff --git a/src/fastfetch.h b/src/fastfetch.h index abd7dab69..90c6613b0 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -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); /********************/ diff --git a/src/modules/de.c b/src/modules/de.c index b06b8739e..9ebf07f53 100644 --- a/src/modules/de.c +++ b/src/modules/de.c @@ -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); } diff --git a/src/modules/wm.c b/src/modules/wm.c index c735586dd..eaab0e7e5 100644 --- a/src/modules/wm.c +++ b/src/modules/wm.c @@ -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} }); } }