Files
fastfetch/src/modules/de/de.c
T

147 lines
4.3 KiB
C
Raw Normal View History

2023-06-07 10:39:53 +08:00
#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2023-06-07 10:39:53 +08:00
#include "detection/displayserver/displayserver.h"
#include "detection/de/de.h"
2023-06-07 10:39:53 +08:00
#include "modules/de/de.h"
#include "util/stringUtils.h"
2023-06-07 10:39:53 +08:00
#define FF_DE_NUM_FORMAT_ARGS 3
void ffPrintDE(FFDEOptions* options)
2023-06-07 10:39:53 +08:00
{
const FFDisplayServerResult* result = ffConnectDisplayServer();
2023-06-07 10:39:53 +08:00
if(result->dePrettyName.length == 0)
{
ffPrintError(FF_DE_MODULE_NAME, 0, &options->moduleArgs, "No DE found");
2023-06-07 10:39:53 +08:00
return;
}
FF_STRBUF_AUTO_DESTROY version = ffStrbufCreate();
ffDetectDEVersion(&result->dePrettyName, &version, options);
2023-06-07 10:39:53 +08:00
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_DE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-06-07 10:39:53 +08:00
ffStrbufWriteTo(&result->dePrettyName, stdout);
if(version.length > 0)
2023-06-07 10:39:53 +08:00
{
putchar(' ');
ffStrbufWriteTo(&version, stdout);
2023-06-07 10:39:53 +08:00
}
putchar('\n');
}
else
{
ffPrintFormat(FF_DE_MODULE_NAME, 0, &options->moduleArgs, FF_DE_NUM_FORMAT_ARGS, (FFformatarg[]){
2023-06-07 10:39:53 +08:00
{FF_FORMAT_ARG_TYPE_STRBUF, &result->deProcessName},
{FF_FORMAT_ARG_TYPE_STRBUF, &result->dePrettyName},
{FF_FORMAT_ARG_TYPE_STRBUF, &version}
2023-06-07 10:39:53 +08:00
});
}
}
bool ffParseDECommandOptions(FFDEOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_DE_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
2023-10-31 09:54:12 +08:00
if (ffStrEqualsIgnCase(subKey, "slow-version-detection"))
{
options->slowVersionDetection = ffOptionParseBoolean(value);
return true;
}
2023-06-07 10:39:53 +08:00
return false;
}
2023-08-17 10:56:54 +08:00
void ffParseDEJsonObject(FFDEOptions* options, yyjson_val* module)
2023-06-07 10:39:53 +08:00
{
2023-08-17 16:09:41 +08:00
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key_, val)
2023-06-07 10:39:53 +08:00
{
2023-08-17 16:09:41 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
continue;
2023-06-10 15:01:54 +08:00
2023-08-17 16:09:41 +08:00
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
2023-06-07 10:39:53 +08:00
if (ffStrEqualsIgnCase(key, "slowVersionDetection"))
{
options->slowVersionDetection = yyjson_get_bool(val);
continue;
}
2023-08-17 16:09:41 +08:00
ffPrintError(FF_DE_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
2023-06-07 10:39:53 +08:00
}
}
2023-08-31 13:45:19 +08:00
2023-10-24 14:41:29 +08:00
void ffGenerateDEJsonConfig(FFDEOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
__attribute__((__cleanup__(ffDestroyDEOptions))) FFDEOptions defaultOptions;
ffInitDEOptions(&defaultOptions);
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
if (defaultOptions.slowVersionDetection != options->slowVersionDetection)
yyjson_mut_obj_add_bool(doc, module, "slowVersionDetection", options->slowVersionDetection);
2023-10-24 14:41:29 +08:00
}
2023-10-23 10:14:22 +08:00
void ffGenerateDEJsonResult(FF_MAYBE_UNUSED FFDEOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2023-08-31 13:45:19 +08:00
{
const FFDisplayServerResult* result = ffConnectDisplayServer();
if(result->dePrettyName.length == 0)
{
yyjson_mut_obj_add_str(doc, module, "error", "No DE found");
return;
}
FF_STRBUF_AUTO_DESTROY version = ffStrbufCreate();
ffDetectDEVersion(&result->dePrettyName, &version, options);
2023-08-31 13:45:19 +08:00
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
yyjson_mut_obj_add_strbuf(doc, obj, "processName", &result->deProcessName);
yyjson_mut_obj_add_strbuf(doc, obj, "prettyName", &result->dePrettyName);
yyjson_mut_obj_add_strbuf(doc, obj, "version", &version);
2023-08-31 13:45:19 +08:00
}
2023-10-07 13:40:40 +08:00
void ffPrintDEHelpFormat(void)
{
ffPrintModuleFormatHelp(FF_DE_MODULE_NAME, "{2} {3}", FF_DE_NUM_FORMAT_ARGS, (const char* []) {
"DE process name",
"DE pretty name",
"DE version"
});
}
2023-10-23 13:32:18 +08:00
void ffInitDEOptions(FFDEOptions* options)
{
ffOptionInitModuleBaseInfo(
&options->moduleInfo,
FF_DE_MODULE_NAME,
"Print desktop environment name",
ffParseDECommandOptions,
ffParseDEJsonObject,
ffPrintDE,
ffGenerateDEJsonResult,
ffPrintDEHelpFormat,
2023-10-24 14:41:29 +08:00
ffGenerateDEJsonConfig
);
2023-10-23 13:32:18 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
options->slowVersionDetection = false;
2023-10-23 13:32:18 +08:00
}
void ffDestroyDEOptions(FFDEOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}