summaryrefslogtreecommitdiffstats
path: root/src/modules/poweradapter/poweradapter.c
blob: 2e70c000d9bacc872c95f3b81aa3e0aa38a8c991 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "common/printing.h"
#include "common/jsonconfig.h"
#include "common/strutil.h"
#include "detection/poweradapter/poweradapter.h"
#include "modules/poweradapter/poweradapter.h"

#define FF_POWERADAPTER_DISPLAY_NAME "Power Adapter"

bool ffPrintPowerAdapter(FFPowerAdapterOptions* options) {
    FF_LIST_AUTO_DESTROY results = ffListCreate();

    const char* error = ffDetectPowerAdapter(&results);

    if (error) {
        ffPrintError(FF_POWERADAPTER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
        return false;
    }

    if (results.length == 0) {
        ffPrintError(FF_POWERADAPTER_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No power adapters found");
        return false;
    }

    for (uint8_t i = 0; i < (uint8_t) results.length; i++) {
        FFPowerAdapterResult* result = FF_LIST_GET(FFPowerAdapterResult, results, i);

        if (options->moduleArgs.outputFormat.length == 0) {
            ffPrintLogoAndKey(FF_POWERADAPTER_DISPLAY_NAME, i, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);

            if (result->name.length > 0) {
                puts(result->name.chars);
            } else {
                printf("%dW\n", result->watts);
            }
        } else {
            FF_PRINT_FORMAT_CHECKED(FF_POWERADAPTER_DISPLAY_NAME, i, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
                                                                                                                      FF_ARG(result->watts, "watts"),
                                                                                                                      FF_ARG(result->name, "name"),
                                                                                                                      FF_ARG(result->manufacturer, "manufacturer"),
                                                                                                                      FF_ARG(result->modelName, "model-name"),
                                                                                                                      FF_ARG(result->description, "description"),
                                                                                                                      FF_ARG(result->serial, "serial"),
                                                                                                                  }));
        }

        ffStrbufDestroy(&result->manufacturer);
        ffStrbufDestroy(&result->description);
        ffStrbufDestroy(&result->modelName);
        ffStrbufDestroy(&result->name);
        ffStrbufDestroy(&result->serial);
    }

    return true;
}

void ffGeneratePowerAdapterJsonConfig(FFPowerAdapterOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
}

void ffParsePowerAdapterJsonObject(FFPowerAdapterOptions* options, yyjson_val* module) {
    yyjson_val *key, *val;
    size_t idx, max;
    yyjson_obj_foreach (module, idx, max, key, val) {
        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
            continue;
        }

        ffPrintError(FF_POWERADAPTER_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
    }
}

bool ffGeneratePowerAdapterJsonResult(FF_A_UNUSED FFPowerAdapterOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
    FF_LIST_AUTO_DESTROY results = ffListCreate();

    const char* error = ffDetectPowerAdapter(&results);

    if (error) {
        yyjson_mut_obj_add_str(doc, module, "error", error);
        return false;
    }

    yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
    FF_LIST_FOR_EACH (FFPowerAdapterResult, item, results) {
        yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
        yyjson_mut_obj_add_strbuf(doc, obj, "description", &item->description);
        yyjson_mut_obj_add_strbuf(doc, obj, "manufacturer", &item->manufacturer);
        yyjson_mut_obj_add_strbuf(doc, obj, "modelName", &item->modelName);
        yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name);
        yyjson_mut_obj_add_strbuf(doc, obj, "serial", &item->serial);
        yyjson_mut_obj_add_int(doc, obj, "watts", item->watts);
    }

    FF_LIST_FOR_EACH (FFPowerAdapterResult, item, results) {
        ffStrbufDestroy(&item->manufacturer);
        ffStrbufDestroy(&item->description);
        ffStrbufDestroy(&item->modelName);
        ffStrbufDestroy(&item->name);
        ffStrbufDestroy(&item->serial);
    }

    return true;
}

void ffInitPowerAdapterOptions(FFPowerAdapterOptions* options) {
    ffOptionInitModuleArg(&options->moduleArgs, "󰚥");
}

void ffDestroyPowerAdapterOptions(FFPowerAdapterOptions* options) {
    ffOptionDestroyModuleArg(&options->moduleArgs);
}

FFModuleBaseInfo ffPowerAdapterModuleInfo = {
    .name = FF_POWERADAPTER_MODULE_NAME,
    .description = "Print power adapter name and charging watts",
    .initOptions = (void*) ffInitPowerAdapterOptions,
    .destroyOptions = (void*) ffDestroyPowerAdapterOptions,
    .parseJsonObject = (void*) ffParsePowerAdapterJsonObject,
    .printModule = (void*) ffPrintPowerAdapter,
    .generateJsonResult = (void*) ffGeneratePowerAdapterJsonResult,
    .generateJsonConfig = (void*) ffGeneratePowerAdapterJsonConfig,
    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
        { "Power adapter watts", "watts" },
        { "Power adapter name", "name" },
        { "Power adapter manufacturer", "manufacturer" },
        { "Power adapter model", "model" },
        { "Power adapter description", "description" },
        { "Power adapter serial number", "serial" },
    }))
};