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

125 lines
4.6 KiB
C
Raw Normal View History

2026-01-06 09:48:38 +08:00
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/stringUtils.h"
2023-03-13 18:54:57 +08:00
#include "detection/board/board.h"
#include "modules/board/board.h"
2026-03-29 09:28:49 +08:00
bool ffPrintBoard(FFBoardOptions* options) {
bool success = false;
2023-03-13 18:54:57 +08:00
FFBoardResult result;
2023-06-22 22:32:14 +08:00
ffStrbufInit(&result.name);
ffStrbufInit(&result.vendor);
ffStrbufInit(&result.version);
2024-01-06 18:12:51 +08:00
ffStrbufInit(&result.serial);
2023-03-13 18:54:57 +08:00
2024-01-06 18:12:51 +08:00
const char* error = ffDetectBoard(&result);
2026-03-29 09:28:49 +08:00
if (error) {
ffPrintError(FF_BOARD_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
2023-03-13 18:54:57 +08:00
goto exit;
}
2026-03-29 09:28:49 +08:00
if (result.name.length == 0) {
ffPrintError(FF_BOARD_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "board_name is not set.");
2023-03-13 18:54:57 +08:00
goto exit;
}
2026-03-29 09:28:49 +08:00
if (options->moduleArgs.outputFormat.length == 0) {
ffPrintLogoAndKey(FF_BOARD_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-06-22 22:32:14 +08:00
ffStrbufWriteTo(&result.name, stdout);
2026-03-29 09:28:49 +08:00
if (result.version.length) {
2023-06-22 22:32:14 +08:00
printf(" (%s)", result.version.chars);
2026-03-29 09:28:49 +08:00
}
2023-06-19 21:17:07 +08:00
putchar('\n');
2026-03-29 09:28:49 +08:00
} else {
2024-12-11 10:47:02 +08:00
FF_PRINT_FORMAT_CHECKED(FF_BOARD_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
2026-03-29 09:28:49 +08:00
FF_ARG(result.name, "name"),
FF_ARG(result.vendor, "vendor"),
FF_ARG(result.version, "version"),
FF_ARG(result.serial, "serial"),
}));
2023-03-13 18:54:57 +08:00
}
success = true;
2023-03-13 18:54:57 +08:00
exit:
2023-06-22 22:32:14 +08:00
ffStrbufDestroy(&result.name);
ffStrbufDestroy(&result.vendor);
ffStrbufDestroy(&result.version);
2024-01-06 18:12:51 +08:00
ffStrbufDestroy(&result.serial);
return success;
2023-03-13 18:54:57 +08:00
}
2026-03-29 09:28:49 +08:00
void ffParseBoardJsonObject(FFBoardOptions* options, yyjson_val* module) {
yyjson_val *key, *val;
2023-08-17 16:09:41 +08:00
size_t idx, max;
2026-03-29 09:28:49 +08:00
yyjson_obj_foreach (module, idx, max, key, val) {
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
2023-08-17 16:09:41 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2023-03-15 11:24:49 +08:00
ffPrintError(FF_BOARD_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2023-03-13 18:54:57 +08:00
}
}
2023-08-30 13:20:00 +08:00
2026-03-29 09:28:49 +08:00
void ffGenerateBoardJsonConfig(FFBoardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2023-10-23 16:34:33 +08:00
}
2026-03-29 09:28:49 +08:00
bool ffGenerateBoardJsonResult(FF_MAYBE_UNUSED FFBoardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
bool success = false;
2023-08-30 13:20:00 +08:00
FFBoardResult board;
ffStrbufInit(&board.name);
ffStrbufInit(&board.vendor);
ffStrbufInit(&board.version);
2024-01-06 18:12:51 +08:00
ffStrbufInit(&board.serial);
2023-08-30 13:20:00 +08:00
const char* error = ffDetectBoard(&board);
2026-03-29 09:28:49 +08:00
if (error) {
2023-08-30 13:20:00 +08:00
yyjson_mut_obj_add_str(doc, module, "error", error);
goto exit;
}
2026-03-29 09:28:49 +08:00
if (board.name.length == 0) {
2023-08-30 13:20:00 +08:00
yyjson_mut_obj_add_str(doc, module, "error", "board_name is not set.");
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 13:20:00 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "name", &board.name);
yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &board.vendor);
yyjson_mut_obj_add_strbuf(doc, obj, "version", &board.version);
2024-01-06 18:12:51 +08:00
yyjson_mut_obj_add_strbuf(doc, obj, "serial", &board.serial);
success = true;
2023-08-30 13:20:00 +08:00
exit:
ffStrbufDestroy(&board.name);
ffStrbufDestroy(&board.vendor);
ffStrbufDestroy(&board.version);
2024-01-06 18:12:51 +08:00
ffStrbufDestroy(&board.serial);
return success;
2023-08-30 13:20:00 +08:00
}
2023-10-07 13:40:40 +08:00
2026-03-29 09:28:49 +08:00
void ffInitBoardOptions(FFBoardOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "");
}
2026-03-29 09:28:49 +08:00
void ffDestroyBoardOptions(FFBoardOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffBoardModuleInfo = {
2024-12-11 10:47:02 +08:00
.name = FF_BOARD_MODULE_NAME,
.description = "Print motherboard name and other info",
.initOptions = (void*) ffInitBoardOptions,
.destroyOptions = (void*) ffDestroyBoardOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseBoardJsonObject,
.printModule = (void*) ffPrintBoard,
.generateJsonResult = (void*) ffGenerateBoardJsonResult,
.generateJsonConfig = (void*) ffGenerateBoardJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
{"Board name", "name"},
{"Board vendor", "vendor"},
{"Board version", "version"},
{"Board serial number", "serial"},
2026-03-29 09:28:49 +08:00
}))};