Files
fastfetch/src/modules/wm.c
T

91 lines
2.1 KiB
C
Raw Normal View History

2021-03-05 21:38:35 +01:00
#include "fastfetch.h"
#include <dirent.h>
2021-04-03 23:10:14 +02:00
void ffCalculateWM(FFinstance* instance, FFstrbuf** prettyNamePtr, FFstrbuf** processNamePtr, FFstrbuf** errorPtr)
2021-03-05 21:38:35 +01:00
{
2021-04-03 23:10:14 +02:00
static FFstrbuf prettyName;
static FFstrbuf processName;
static FFstrbuf error;
static bool init = false;
if(prettyNamePtr != NULL)
*prettyNamePtr = &prettyName;
if(processNamePtr != NULL)
*processNamePtr = &processName;
if(errorPtr != NULL)
*errorPtr = &error;
if(init)
return;
init = true;
ffStrbufInit(&prettyName);
ffStrbufInit(&processName);
ffStrbufInit(&error);
2021-03-05 21:38:35 +01:00
DIR* proc = opendir("/proc/");
if(proc == NULL)
{
2021-04-03 23:10:14 +02:00
ffStrbufSetS(&error, "opendir(\"/proc/\") == NULL");
2021-03-05 21:38:35 +01:00
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);
2021-03-28 20:36:49 +02:00
ffGetFileContent(path, &processName);
if(ffStrbufIgnCaseCompS(&processName, "kwin_wayland") == 0 || ffStrbufIgnCaseCompS(&processName, "kwin_x11") == 0)
ffStrbufSetS(&prettyName, "KWin");
2021-03-05 21:38:35 +01:00
2021-03-28 20:36:49 +02:00
if(prettyName.length > 0)
2021-03-05 21:38:35 +01:00
break;
}
closedir(proc);
2021-04-03 21:51:50 +02:00
if(prettyName.length == 0)
2021-03-19 20:57:07 +01:00
{
2021-04-03 23:10:14 +02:00
ffStrbufSetS(&error, "No process name matches the name of known display managers");
return;
}
}
void ffPrintWM(FFinstance* instance)
{
FFstrbuf* prettyName;
FFstrbuf* processName;
FFstrbuf* error;
ffCalculateWM(instance, &prettyName, &processName, &error);
if(error->length > 0)
{
ffPrintError(instance, &instance->config.wmKey, "WM", error->chars);
2021-04-03 21:51:50 +02:00
return;
}
2021-04-03 21:51:50 +02:00
if(instance->config.wmFormat.length == 0)
{
2021-04-03 11:39:59 +02:00
ffPrintLogoAndKey(instance, &instance->config.wmKey, "WM");
2021-04-03 23:10:14 +02:00
ffStrbufPutTo(prettyName, stdout);
2021-03-05 21:38:35 +01:00
}
else
{
2021-04-03 11:39:59 +02:00
ffPrintFormatString(instance, &instance->config.wmKey, "WM", &instance->config.wmFormat, 2,
2021-04-03 23:10:14 +02:00
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, processName},
(FFformatarg){FF_FORMAT_ARG_TYPE_STRBUF, prettyName}
2021-03-19 20:57:07 +01:00
);
2021-03-05 21:38:35 +01:00
}
2021-03-27 18:14:19 +01:00
}