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

96 lines
2.9 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 "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;
}
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(result->deVersion.length > 0)
{
putchar(' ');
ffStrbufWriteTo(&result->deVersion, stdout);
}
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, &result->deVersion}
});
}
}
void ffInitDEOptions(FFDEOptions* options)
{
2023-08-31 13:45:19 +08:00
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_DE_MODULE_NAME, ffParseDECommandOptions, ffParseDEJsonObject, ffPrintDE, ffGenerateDEJson);
2023-06-07 10:39:53 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
}
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;
return false;
}
void ffDestroyDEOptions(FFDEOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
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
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
void ffGenerateDEJson(FF_MAYBE_UNUSED FFDEOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
const FFDisplayServerResult* result = ffConnectDisplayServer();
if(result->dePrettyName.length == 0)
{
yyjson_mut_obj_add_str(doc, module, "error", "No DE found");
return;
}
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", &result->deVersion);
}