Files
fastfetch/src/modules/wm.c
T

77 lines
1.6 KiB
C
Raw Normal View History

2021-03-05 21:38:35 +01:00
#include "fastfetch.h"
#include <dirent.h>
2021-03-19 20:57:07 +01:00
static bool parseProcessName(FFstrbuf* name)
2021-03-05 21:38:35 +01:00
{
2021-03-19 20:57:07 +01:00
if(ffStrbufIgnCaseCompS(name, "kwin_wayland") == 0 || ffStrbufIgnCaseCompS(name, "kwin_x11") == 0)
ffStrbufSetS(name, "KWin");
else
return false;
return true;
2021-03-05 21:38:35 +01:00
}
void ffPrintWM(FFinstance* instance)
{
DIR* proc = opendir("/proc/");
if(proc == NULL)
{
ffPrintError(instance, "WM", "opendir(\"/proc/\") == NULL");
return;
}
struct dirent* dirent;
2021-03-19 20:57:07 +01:00
FFstrbuf name;
ffStrbufInit(&name);
bool found = false;
2021-03-05 21:38:35 +01:00
while((dirent = readdir(proc)) != NULL)
{
if(dirent->d_type != DT_DIR)
continue;
char path[20];
sprintf(path, "/proc/%.8s/comm", dirent->d_name);
char comm[17]; //MAX_COMM_LENGTH is 17 + null terminator
ffGetFileContent(path, comm, sizeof(comm));
2021-03-19 20:57:07 +01:00
ffStrbufSetS(&name, comm);
2021-03-05 21:38:35 +01:00
2021-03-19 20:57:07 +01:00
if((found = parseProcessName(&name)))
2021-03-05 21:38:35 +01:00
break;
}
closedir(proc);
2021-03-19 20:57:07 +01:00
if(!found)
2021-03-05 21:38:35 +01:00
{
ffPrintError(instance, "WM", "No process name matches the name of known display managers");
2021-03-19 20:57:07 +01:00
return;
}
if(ffStrbufIsEmpty(&instance->config.wmFormat))
{
ffPrintLogoAndKey(instance, "WM");
ffStrbufWriteTo(&name, stdout);
2021-03-05 21:38:35 +01:00
}
else
{
2021-03-19 20:57:07 +01:00
FFstrbuf wm;
ffStrbufInit(&wm);
ffParseFormatString(&wm, &instance->config.wmFormat, 1,
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, &name}
);
2021-03-05 21:38:35 +01:00
ffPrintLogoAndKey(instance, "WM");
2021-03-19 20:57:07 +01:00
ffStrbufWriteTo(&wm, stdout);
ffStrbufDestroy(&wm);
2021-03-05 21:38:35 +01:00
}
2021-03-19 20:57:07 +01:00
putchar('\n');
2021-03-05 21:38:35 +01:00
}