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

150 lines
4.8 KiB
C
Raw Normal View History

2023-06-09 10:08:04 +08:00
#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2023-06-09 10:08:04 +08:00
#include "detection/chassis/chassis.h"
#include "modules/chassis/chassis.h"
#include "util/stringUtils.h"
2023-06-09 10:08:04 +08:00
void ffPrintChassis(FFChassisOptions* options)
2023-06-09 10:08:04 +08:00
{
FFChassisResult result;
2023-06-22 19:05:13 +08:00
ffStrbufInit(&result.type);
ffStrbufInit(&result.vendor);
ffStrbufInit(&result.version);
2024-01-06 18:35:10 +08:00
ffStrbufInit(&result.serial);
2023-06-09 10:08:04 +08:00
const char* error = ffDetectChassis(&result);
2023-06-16 16:33:43 +08:00
if(error)
2023-06-09 10:08:04 +08:00
{
ffPrintError(FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
2023-06-09 10:08:04 +08:00
goto exit;
}
2023-06-22 19:05:13 +08:00
if(result.type.length == 0)
2023-06-09 10:08:04 +08:00
{
ffPrintError(FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "chassis_type is not set by O.E.M.");
2023-06-16 16:33:43 +08:00
goto exit;
2023-06-09 10:08:04 +08:00
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-06-22 19:05:13 +08:00
ffStrbufWriteTo(&result.type, stdout);
if (result.version.length)
printf(" (%s)", result.version.chars);
2023-06-19 21:19:09 +08:00
putchar('\n');
2023-06-09 10:08:04 +08:00
}
else
{
2024-12-11 10:47:02 +08:00
FF_PRINT_FORMAT_CHECKED(FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
FF_FORMAT_ARG(result.type, "type"),
FF_FORMAT_ARG(result.vendor, "vendor"),
FF_FORMAT_ARG(result.version, "version"),
FF_FORMAT_ARG(result.serial, "serial"),
}));
2023-06-09 10:08:04 +08:00
}
exit:
2023-06-22 19:05:13 +08:00
ffStrbufDestroy(&result.type);
ffStrbufDestroy(&result.vendor);
ffStrbufDestroy(&result.version);
2024-01-06 18:35:10 +08:00
ffStrbufDestroy(&result.serial);
2023-06-09 10:08:04 +08:00
}
bool ffParseChassisCommandOptions(FFChassisOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_CHASSIS_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
2023-08-17 10:56:54 +08:00
void ffParseChassisJsonObject(FFChassisOptions* options, yyjson_val* module)
2023-06-09 10:08:04 +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-09 10:08:04 +08:00
{
2023-08-17 16:09:41 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type") || ffStrEqualsIgnCase(key, "condition"))
2023-08-17 16:09:41 +08:00
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-09 10:08:04 +08:00
ffPrintError(FF_CHASSIS_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
2023-06-09 10:08:04 +08:00
}
}
2023-08-30 14:13:26 +08:00
2023-10-24 14:41:29 +08:00
void ffGenerateChassisJsonConfig(FFChassisOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
__attribute__((__cleanup__(ffDestroyChassisOptions))) FFChassisOptions defaultOptions;
ffInitChassisOptions(&defaultOptions);
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
}
2023-10-23 10:14:22 +08:00
void ffGenerateChassisJsonResult(FF_MAYBE_UNUSED FFChassisOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2023-08-30 14:13:26 +08:00
{
FFChassisResult result;
ffStrbufInit(&result.type);
ffStrbufInit(&result.vendor);
ffStrbufInit(&result.version);
2024-01-06 18:35:10 +08:00
ffStrbufInit(&result.serial);
2023-08-30 14:13:26 +08:00
const char* error = ffDetectChassis(&result);
2023-08-30 14:13:26 +08:00
if (error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
goto exit;
}
if(result.type.length == 0)
{
yyjson_mut_obj_add_str(doc, module, "error", "chassis_type is not set by O.E.M.");
goto exit;
}
2023-08-31 13:54:46 +08:00
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
2023-08-30 14:13:26 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "type", &result.type);
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &result.vendor);
yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);
2024-01-06 18:35:10 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "serial", &result.serial);
2023-08-30 14:13:26 +08:00
exit:
ffStrbufDestroy(&result.type);
ffStrbufDestroy(&result.vendor);
ffStrbufDestroy(&result.version);
2024-01-06 18:35:10 +08:00
ffStrbufDestroy(&result.serial);
2023-08-30 14:13:26 +08:00
}
2023-10-07 13:40:40 +08:00
2024-12-11 10:47:02 +08:00
static FFModuleBaseInfo ffModuleInfo = {
.name = FF_CHASSIS_MODULE_NAME,
.description = "Print chassis type (desktop, laptop, etc)",
.parseCommandOptions = (void*) ffParseChassisCommandOptions,
.parseJsonObject = (void*) ffParseChassisJsonObject,
.printModule = (void*) ffPrintChassis,
.generateJsonResult = (void*) ffGenerateChassisJsonResult,
.generateJsonConfig = (void*) ffGenerateChassisJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
{"Chassis type", "type"},
{"Chassis vendor", "vendor"},
{"Chassis version", "version"},
{"Chassis serial number", "serial"},
})),
};
2023-10-23 13:32:18 +08:00
void ffInitChassisOptions(FFChassisOptions* options)
{
2024-12-11 10:47:02 +08:00
options->moduleInfo = ffModuleInfo;
2024-07-23 23:02:35 +08:00
ffOptionInitModuleArg(&options->moduleArgs, "");
2023-10-23 13:32:18 +08:00
}
void ffDestroyChassisOptions(FFChassisOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}