master ohmu/fastfetch-openbsd / src / modules / board / board.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/strutil.h"
  4#include "detection/board/board.h"
  5#include "modules/board/board.h"
  6
  7bool ffPrintBoard(FFBoardOptions* options) {
  8    bool success = false;
  9    FFBoardResult result;
 10    ffStrbufInit(&result.name);
 11    ffStrbufInit(&result.vendor);
 12    ffStrbufInit(&result.version);
 13    ffStrbufInit(&result.serial);
 14
 15    const char* error = ffDetectBoard(&result);
 16    if (error) {
 17        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Board), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
 18        goto exit;
 19    }
 20
 21    if (result.name.length == 0) {
 22        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Board), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "board_name is not set.");
 23        goto exit;
 24    }
 25
 26    if (options->moduleArgs.outputFormat.length == 0) {
 27        ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Board), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 28        ffStrbufWriteTo(&result.name, stdout);
 29        if (result.version.length) {
 30            printf(" (%s)", result.version.chars);
 31        }
 32        putchar('\n');
 33    } else {
 34        FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Board), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 35                                                                                                          FF_ARG(result.name, "name"),
 36                                                                                                          FF_ARG(result.vendor, "vendor"),
 37                                                                                                          FF_ARG(result.version, "version"),
 38                                                                                                          FF_ARG(result.serial, "serial"),
 39                                                                                                      }));
 40    }
 41    success = true;
 42
 43exit:
 44    ffStrbufDestroy(&result.name);
 45    ffStrbufDestroy(&result.vendor);
 46    ffStrbufDestroy(&result.version);
 47    ffStrbufDestroy(&result.serial);
 48    return success;
 49}
 50
 51void ffParseBoardJsonObject(FFBoardOptions* options, yyjson_val* module) {
 52    yyjson_val *key, *val;
 53    size_t idx, max;
 54    yyjson_obj_foreach (module, idx, max, key, val) {
 55        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 56            continue;
 57        }
 58
 59        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Board), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 60    }
 61}
 62
 63[[gnu::cold]]
 64void ffGenerateBoardJsonConfig(FFBoardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 65    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 66}
 67
 68bool ffGenerateBoardJsonResult([[maybe_unused]] FFBoardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 69    bool success = false;
 70    FFBoardResult board;
 71    ffStrbufInit(&board.name);
 72    ffStrbufInit(&board.vendor);
 73    ffStrbufInit(&board.version);
 74    ffStrbufInit(&board.serial);
 75
 76    const char* error = ffDetectBoard(&board);
 77
 78    if (error) {
 79        yyjson_mut_obj_add_str(doc, module, "error", error);
 80        goto exit;
 81    }
 82
 83    if (board.name.length == 0) {
 84        yyjson_mut_obj_add_str(doc, module, "error", "board_name is not set.");
 85        goto exit;
 86    }
 87
 88    yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
 89    yyjson_mut_obj_add_strbuf(doc, obj, "name", &board.name);
 90    yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &board.vendor);
 91    yyjson_mut_obj_add_strbuf(doc, obj, "version", &board.version);
 92    yyjson_mut_obj_add_strbuf(doc, obj, "serial", &board.serial);
 93    success = true;
 94
 95exit:
 96    ffStrbufDestroy(&board.name);
 97    ffStrbufDestroy(&board.vendor);
 98    ffStrbufDestroy(&board.version);
 99    ffStrbufDestroy(&board.serial);
100    return success;
101}
102
103void ffInitBoardOptions(FFBoardOptions* options) {
104    ffOptionInitModuleArg(&options->moduleArgs, "");
105}
106
107void ffDestroyBoardOptions(FFBoardOptions* options) {
108    ffOptionDestroyModuleArg(&options->moduleArgs);
109}
110
111FFModuleBaseInfo ffBoardModuleInfo = {
112    .name = "Board",
113    .description = "Print motherboard name and other information",
114    .displayName = {
115        .en = "Board",
116        .ar = "اللوحة الأم",
117        .cs = "Základní deska",
118        .de = "Mainboard",
119        .es = "Placa base",
120        .fi = "Emolevy",
121        .fr = "Carte mère",
122        .gl = "Placa base",
123        .he = "לוח אם",
124        .id = "Papan Induk",
125        .it = "Scheda madre",
126        .ja = "マザーボード",
127        .ko = "메인보드",
128        .pl = "Płyta główna",
129        .pt = "Placa-mãe",
130        .ru = "Материнская плата",
131        .tr = "Anakart",
132        .uk = "Материнська плата",
133        .vi = "Bo mạch chủ",
134        .zh_CN = "主板",
135        .zh_TW = "主機板",
136    },
137    .initOptions = (void*) ffInitBoardOptions,
138    .destroyOptions = (void*) ffDestroyBoardOptions,
139    .parseJsonObject = (void*) ffParseBoardJsonObject,
140    .printModule = (void*) ffPrintBoard,
141    .generateJsonResult = (void*) ffGenerateBoardJsonResult,
142    .generateJsonConfig = (void*) ffGenerateBoardJsonConfig,
143    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
144        { "Board name", "name" },
145        { "Board vendor", "vendor" },
146        { "Board version", "version" },
147        { "Board serial number", "serial" },
148    })),
149    .defaultOrder = 7,
150};