master ohmu/fastfetch-openbsd / src / modules / poweradapter / poweradapter.c
ohmu  ·  2026-10-15
  1#include "common/printing.h"
  2#include "common/jsonconfig.h"
  3#include "common/strutil.h"
  4#include "detection/poweradapter/poweradapter.h"
  5#include "modules/poweradapter/poweradapter.h"
  6
  7bool ffPrintPowerAdapter(FFPowerAdapterOptions* options) {
  8    FF_LIST_AUTO_DESTROY results = ffListCreate();
  9
 10    const char* error = ffDetectPowerAdapter(&results);
 11
 12    if (error) {
 13        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(PowerAdapter), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
 14        return false;
 15    }
 16
 17    if (results.length == 0) {
 18        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(PowerAdapter), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No power adapters found");
 19        return false;
 20    }
 21
 22    for (uint8_t i = 0; i < (uint8_t) results.length; i++) {
 23        FFPowerAdapterResult* result = FF_LIST_GET(FFPowerAdapterResult, results, i);
 24
 25        if (options->moduleArgs.outputFormat.length == 0) {
 26            ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(PowerAdapter), i, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 27
 28            if (result->name.length > 0) {
 29                puts(result->name.chars);
 30            } else {
 31                printf("%dW\n", result->watts);
 32            }
 33        } else {
 34            FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(PowerAdapter), i, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 35                                                                                                                      FF_ARG(result->watts, "watts"),
 36                                                                                                                      FF_ARG(result->name, "name"),
 37                                                                                                                      FF_ARG(result->manufacturer, "manufacturer"),
 38                                                                                                                      FF_ARG(result->modelName, "model-name"),
 39                                                                                                                      FF_ARG(result->description, "description"),
 40                                                                                                                      FF_ARG(result->serial, "serial"),
 41                                                                                                                  }));
 42        }
 43
 44        ffStrbufDestroy(&result->manufacturer);
 45        ffStrbufDestroy(&result->description);
 46        ffStrbufDestroy(&result->modelName);
 47        ffStrbufDestroy(&result->name);
 48        ffStrbufDestroy(&result->serial);
 49    }
 50
 51    return true;
 52}
 53
 54[[gnu::cold]]
 55void ffGeneratePowerAdapterJsonConfig(FFPowerAdapterOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 56    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 57}
 58
 59void ffParsePowerAdapterJsonObject(FFPowerAdapterOptions* options, yyjson_val* module) {
 60    yyjson_val *key, *val;
 61    size_t idx, max;
 62    yyjson_obj_foreach (module, idx, max, key, val) {
 63        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 64            continue;
 65        }
 66
 67        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(PowerAdapter), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 68    }
 69}
 70
 71bool ffGeneratePowerAdapterJsonResult([[maybe_unused]] FFPowerAdapterOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 72    FF_LIST_AUTO_DESTROY results = ffListCreate();
 73
 74    const char* error = ffDetectPowerAdapter(&results);
 75
 76    if (error) {
 77        yyjson_mut_obj_add_str(doc, module, "error", error);
 78        return false;
 79    }
 80
 81    yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
 82    FF_LIST_FOR_EACH (FFPowerAdapterResult, item, results) {
 83        yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
 84        yyjson_mut_obj_add_strbuf(doc, obj, "description", &item->description);
 85        yyjson_mut_obj_add_strbuf(doc, obj, "manufacturer", &item->manufacturer);
 86        yyjson_mut_obj_add_strbuf(doc, obj, "modelName", &item->modelName);
 87        yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name);
 88        yyjson_mut_obj_add_strbuf(doc, obj, "serial", &item->serial);
 89        yyjson_mut_obj_add_int(doc, obj, "watts", item->watts);
 90    }
 91
 92    FF_LIST_FOR_EACH (FFPowerAdapterResult, item, results) {
 93        ffStrbufDestroy(&item->manufacturer);
 94        ffStrbufDestroy(&item->description);
 95        ffStrbufDestroy(&item->modelName);
 96        ffStrbufDestroy(&item->name);
 97        ffStrbufDestroy(&item->serial);
 98    }
 99
100    return true;
101}
102
103void ffInitPowerAdapterOptions(FFPowerAdapterOptions* options) {
104    ffOptionInitModuleArg(&options->moduleArgs, "󰚥");
105}
106
107void ffDestroyPowerAdapterOptions(FFPowerAdapterOptions* options) {
108    ffOptionDestroyModuleArg(&options->moduleArgs);
109}
110
111FFModuleBaseInfo ffPowerAdapterModuleInfo = {
112    .name = "PowerAdapter",
113    .description = "Print power adapter name and charging watts",
114    .displayName = {
115        .en = "Power Adapter",
116        .ar = "محول الطاقة",
117        .cs = "Napájecí adaptér",
118        .de = "Netzteil",
119        .es = "Adaptador de corriente",
120        .fi = "Virta-adapteri",
121        .fr = "Adaptateur secteur",
122        .gl = "Adaptador de corrente",
123        .he = "מתאם מתח",
124        .id = "Adaptor Daya",
125        .it = "Alimentatore",
126        .ja = "電源アダプター",
127        .ko = "전원 어댑터",
128        .pl = "Zasilacz",
129        .pt = "Fonte de alimentação",
130        .ru = "Адаптер питания",
131        .tr = "Güç Adaptörü",
132        .uk = "Блок живлення",
133        .vi = "Bộ chuyển đổi nguồn",
134        .zh_CN = "电源适配器",
135        .zh_TW = "電源轉接器",
136    },
137    .initOptions = (void*) ffInitPowerAdapterOptions,
138    .destroyOptions = (void*) ffDestroyPowerAdapterOptions,
139    .parseJsonObject = (void*) ffParsePowerAdapterJsonObject,
140    .printModule = (void*) ffPrintPowerAdapter,
141    .generateJsonResult = (void*) ffGeneratePowerAdapterJsonResult,
142    .generateJsonConfig = (void*) ffGeneratePowerAdapterJsonConfig,
143    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
144        { "Power adapter watts", "watts" },
145        { "Power adapter name", "name" },
146        { "Power adapter manufacturer", "manufacturer" },
147        { "Power adapter model name", "model-name" },
148        { "Power adapter description", "description" },
149        { "Power adapter serial number", "serial" },
150    })),
151    .defaultOrder = 46,
152};