master ohmu/fastfetch-openbsd / src / modules / tpm / tpm.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/strutil.h"
  4#include "detection/tpm/tpm.h"
  5#include "modules/tpm/tpm.h"
  6
  7bool ffPrintTPM(FFTPMOptions* options) {
  8    FFTPMResult result = {
  9        .version = ffStrbufCreate(),
 10        .description = ffStrbufCreate()
 11    };
 12    const char* error = ffDetectTPM(&result);
 13
 14    if (error) {
 15        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(TPM), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
 16        return false;
 17    }
 18
 19    if (options->moduleArgs.outputFormat.length == 0) {
 20        ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(TPM), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 21        if (result.description.length > 0) {
 22            ffStrbufPutTo(&result.description, stdout);
 23        } else {
 24            ffStrbufPutTo(&result.version, stdout);
 25        }
 26    } else {
 27        FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(TPM), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 28                                                                                                        FF_ARG(result.version, "version"),
 29                                                                                                        FF_ARG(result.description, "description"),
 30                                                                                                    }));
 31    }
 32
 33    ffStrbufDestroy(&result.version);
 34    ffStrbufDestroy(&result.description);
 35
 36    return true;
 37}
 38
 39void ffParseTPMJsonObject(FFTPMOptions* options, yyjson_val* module) {
 40    yyjson_val *key, *val;
 41    size_t idx, max;
 42    yyjson_obj_foreach (module, idx, max, key, val) {
 43        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 44            continue;
 45        }
 46
 47        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(TPM), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 48    }
 49}
 50
 51[[gnu::cold]]
 52void ffGenerateTPMJsonConfig(FFTPMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 53    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 54}
 55
 56bool ffGenerateTPMJsonResult([[maybe_unused]] FFTPMOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 57    FFTPMResult result = {
 58        .version = ffStrbufCreate(),
 59        .description = ffStrbufCreate()
 60    };
 61    const char* error = ffDetectTPM(&result);
 62
 63    if (error) {
 64        yyjson_mut_obj_add_str(doc, module, "error", error);
 65        return false;
 66    }
 67
 68    yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
 69    yyjson_mut_obj_add_strbuf(doc, obj, "version", &result.version);
 70    yyjson_mut_obj_add_strbuf(doc, obj, "description", &result.description);
 71
 72    ffStrbufDestroy(&result.version);
 73    ffStrbufDestroy(&result.description);
 74
 75    return true;
 76}
 77
 78void ffInitTPMOptions(FFTPMOptions* options) {
 79    ffOptionInitModuleArg(&options->moduleArgs, "");
 80}
 81
 82void ffDestroyTPMOptions(FFTPMOptions* options) {
 83    ffOptionDestroyModuleArg(&options->moduleArgs);
 84}
 85
 86FFModuleBaseInfo ffTPMModuleInfo = {
 87    .name = "TPM",
 88    .description = "Print information about the Trusted Platform Module (TPM) security device",
 89    .displayName = {
 90        .en = "TPM",
 91        .ar = "TPM",
 92        .cs = "TPM",
 93        .de = "TPM",
 94        .es = "TPM",
 95        .fi = "TPM",
 96        .fr = "TPM",
 97        .gl = "TPM",
 98        .he = "TPM",
 99        .id = "TPM",
100        .it = "TPM",
101        .ja = "TPM",
102        .ko = "TPM",
103        .pl = "TPM",
104        .pt = "TPM",
105        .ru = "TPM",
106        .tr = "TPM",
107        .uk = "TPM",
108        .vi = "TPM",
109        .zh_CN = "TPM",
110        .zh_TW = "TPM",
111    },
112    .initOptions = (void*) ffInitTPMOptions,
113    .destroyOptions = (void*) ffDestroyTPMOptions,
114    .parseJsonObject = (void*) ffParseTPMJsonObject,
115    .printModule = (void*) ffPrintTPM,
116    .generateJsonResult = (void*) ffGenerateTPMJsonResult,
117    .generateJsonConfig = (void*) ffGenerateTPMJsonConfig,
118    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
119        { "TPM device version", "version" },
120        { "TPM general description", "description" },
121    })),
122    .defaultOrder = 70,
123};