master ohmu/fastfetch-openbsd / src / modules / chassis / chassis.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/strutil.h"
  4#include "detection/chassis/chassis.h"
  5#include "modules/chassis/chassis.h"
  6
  7bool ffPrintChassis(FFChassisOptions* options) {
  8    bool success = false;
  9
 10    FFChassisResult result;
 11    ffStrbufInit(&result.type);
 12    ffStrbufInit(&result.vendor);
 13    ffStrbufInit(&result.version);
 14    ffStrbufInit(&result.serial);
 15
 16    const char* error = ffDetectChassis(&result);
 17
 18    if (error) {
 19        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Chassis), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
 20        goto exit;
 21    }
 22
 23    if (result.type.length == 0) {
 24        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Chassis), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "chassis_type is not set by O.E.M.");
 25        goto exit;
 26    }
 27
 28    if (options->moduleArgs.outputFormat.length == 0) {
 29        ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Chassis), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 30        ffStrbufWriteTo(&result.type, stdout);
 31        if (result.version.length) {
 32            printf(" (%s)", result.version.chars);
 33        }
 34        putchar('\n');
 35    } else {
 36        FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Chassis), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 37                                                                                                            FF_ARG(result.type, "type"),
 38                                                                                                            FF_ARG(result.vendor, "vendor"),
 39                                                                                                            FF_ARG(result.version, "version"),
 40                                                                                                            FF_ARG(result.serial, "serial"),
 41                                                                                                        }));
 42    }
 43    success = true;
 44
 45exit:
 46    ffStrbufDestroy(&result.type);
 47    ffStrbufDestroy(&result.vendor);
 48    ffStrbufDestroy(&result.version);
 49    ffStrbufDestroy(&result.serial);
 50    return success;
 51}
 52
 53void ffParseChassisJsonObject(FFChassisOptions* options, yyjson_val* module) {
 54    yyjson_val *key, *val;
 55    size_t idx, max;
 56    yyjson_obj_foreach (module, idx, max, key, val) {
 57        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 58            continue;
 59        }
 60
 61        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Chassis), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 62    }
 63}
 64
 65[[gnu::cold]]
 66void ffGenerateChassisJsonConfig(FFChassisOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 67    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 68}
 69
 70bool ffGenerateChassisJsonResult([[maybe_unused]] FFChassisOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 71    bool success = false;
 72    FFChassisResult result;
 73    ffStrbufInit(&result.type);
 74    ffStrbufInit(&result.vendor);
 75    ffStrbufInit(&result.version);
 76    ffStrbufInit(&result.serial);
 77
 78    const char* error = ffDetectChassis(&result);
 79
 80    if (error) {
 81        yyjson_mut_obj_add_str(doc, module, "error", error);
 82        goto exit;
 83    }
 84
 85    if (result.type.length == 0) {
 86        yyjson_mut_obj_add_str(doc, module, "error", "chassis_type is not set by O.E.M.");
 87        goto exit;
 88    }
 89
 90    yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
 91    yyjson_mut_obj_add_strbuf(doc, obj, "type", &result.type);
 92    yyjson_mut_obj_add_strbuf(doc, obj, "vendor", &result.vendor);
 93    yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);
 94    yyjson_mut_obj_add_strbuf(doc, obj, "serial", &result.serial);
 95    success = true;
 96
 97exit:
 98    ffStrbufDestroy(&result.type);
 99    ffStrbufDestroy(&result.vendor);
100    ffStrbufDestroy(&result.version);
101    ffStrbufDestroy(&result.serial);
102    return success;
103}
104
105void ffInitChassisOptions(FFChassisOptions* options) {
106    ffOptionInitModuleArg(&options->moduleArgs, "");
107}
108
109void ffDestroyChassisOptions(FFChassisOptions* options) {
110    ffOptionDestroyModuleArg(&options->moduleArgs);
111}
112
113FFModuleBaseInfo ffChassisModuleInfo = {
114    .name = "Chassis",
115    .description = "Print chassis type information (desktop, laptop, etc.)",
116    .displayName = {
117        .en = "Chassis",
118        .ar = "الهيكل",
119        .cs = "Skříň",
120        .de = "Gehäuse",
121        .es = "Chasis",
122        .fi = "Alusta",
123        .fr = "Châssis",
124        .gl = "Chasis",
125        .he = "מארז",
126        .id = "Casing",
127        .it = "Telaio",
128        .ja = "シャーシ",
129        .ko = "섀시",
130        .pl = "Obudowa",
131        .pt = "Chassi",
132        .ru = "Корпус",
133        .tr = "Kasa",
134        .uk = "Корпус",
135        .vi = "Vỏ máy",
136        .zh_CN = "机箱",
137        .zh_TW = "機箱",
138    },
139    .initOptions = (void*) ffInitChassisOptions,
140    .destroyOptions = (void*) ffDestroyChassisOptions,
141    .parseJsonObject = (void*) ffParseChassisJsonObject,
142    .printModule = (void*) ffPrintChassis,
143    .generateJsonResult = (void*) ffGenerateChassisJsonResult,
144    .generateJsonConfig = (void*) ffGenerateChassisJsonConfig,
145    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
146        { "Chassis type", "type" },
147        { "Chassis vendor", "vendor" },
148        { "Chassis version", "version" },
149        { "Chassis serial number", "serial" },
150    })),
151    .defaultOrder = 8,
152};