master ohmu/fastfetch-openbsd / src / modules / mouse / mouse.c
ohmu  ·  2026-10-15
  1#include "common/percent.h"
  2#include "common/printing.h"
  3#include "common/jsonconfig.h"
  4#include "common/strutil.h"
  5#include "detection/mouse/mouse.h"
  6#include "modules/mouse/mouse.h"
  7
  8static void printDevice(FFMouseOptions* options, const FFMouseDevice* device, uint8_t index) {
  9    if (options->moduleArgs.outputFormat.length == 0) {
 10        ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Mouse), index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
 11        ffStrbufPutTo(&device->name, stdout);
 12    } else {
 13        FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Mouse), index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
 14                                                                                                              FF_ARG(device->name, "name"),
 15                                                                                                              FF_ARG(device->serial, "serial"),
 16                                                                                                          }));
 17    }
 18}
 19
 20bool ffPrintMouse(FFMouseOptions* options) {
 21    FF_LIST_AUTO_DESTROY result = ffListCreate();
 22
 23    const char* error = ffDetectMouse(&result);
 24
 25    if (error) {
 26        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Mouse), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
 27        return false;
 28    }
 29
 30    if (!result.length) {
 31        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Mouse), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No devices detected");
 32        return false;
 33    }
 34
 35    FF_LIST_AUTO_DESTROY filtered = ffListCreate();
 36    FF_LIST_FOR_EACH (FFMouseDevice, device, result) {
 37        bool ignored = false;
 38        FF_LIST_FOR_EACH (FFstrbuf, ignore, options->ignores) {
 39            if (ffStrbufStartsWithIgnCase(&device->name, ignore)) {
 40                ignored = true;
 41                break;
 42            }
 43        }
 44        if (!ignored) {
 45            FFMouseDevice** ptr = FF_LIST_ADD(FFMouseDevice*, filtered);
 46            *ptr = device;
 47        }
 48    }
 49
 50    bool ret = true;
 51    if (!filtered.length) {
 52        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Mouse), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "All devices are ignored");
 53        ret = false;
 54    } else {
 55        uint8_t index = 0;
 56        FF_LIST_FOR_EACH (FFMouseDevice*, pdevice, filtered) {
 57            FFMouseDevice* device = *pdevice;
 58            printDevice(options, device, filtered.length > 1 ? ++index : 0);
 59        }
 60    }
 61
 62    FF_LIST_FOR_EACH (FFMouseDevice, device, result) {
 63        ffStrbufDestroy(&device->serial);
 64        ffStrbufDestroy(&device->name);
 65    }
 66
 67    return ret;
 68}
 69
 70void ffParseMouseJsonObject(FFMouseOptions* options, yyjson_val* module) {
 71    yyjson_val *key, *val;
 72    size_t idx, max;
 73    yyjson_obj_foreach (module, idx, max, key, val) {
 74        if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
 75            continue;
 76        }
 77
 78        if (unsafe_yyjson_equals_str(key, "ignores")) {
 79            yyjson_val* elem;
 80            size_t eidx, emax;
 81            yyjson_arr_foreach (val, eidx, emax, elem) {
 82                if (yyjson_is_str(elem)) {
 83                    FFstrbuf* strbuf = FF_LIST_ADD(FFstrbuf, options->ignores);
 84                    ffStrbufInitJsonVal(strbuf, elem);
 85                }
 86            }
 87            continue;
 88        }
 89
 90        ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Mouse), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
 91    }
 92}
 93
 94[[gnu::cold]]
 95void ffGenerateMouseJsonConfig(FFMouseOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
 96    ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
 97
 98    if (options->ignores.length > 0) {
 99        yyjson_mut_val* ignores = yyjson_mut_obj_add_arr(doc, module, "ignores");
100        FF_LIST_FOR_EACH (FFstrbuf, strbuf, options->ignores) {
101            yyjson_mut_arr_append(ignores, yyjson_mut_strncpy(doc, strbuf->chars, strbuf->length));
102        }
103    }
104}
105
106bool ffGenerateMouseJsonResult([[maybe_unused]] FFMouseOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
107    FF_LIST_AUTO_DESTROY result = ffListCreate();
108
109    const char* error = ffDetectMouse(&result);
110
111    if (error) {
112        yyjson_mut_obj_add_str(doc, module, "error", error);
113        return false;
114    }
115
116    yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
117    FF_LIST_FOR_EACH (FFMouseDevice, device, result) {
118        yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
119        yyjson_mut_obj_add_strbuf(doc, obj, "serial", &device->serial);
120        yyjson_mut_obj_add_strbuf(doc, obj, "name", &device->name);
121
122        bool ignored = false;
123        FF_LIST_FOR_EACH (FFstrbuf, ignore, options->ignores) {
124            if (ffStrbufStartsWithIgnCase(&device->name, ignore)) {
125                ignored = true;
126                break;
127            }
128        }
129        yyjson_mut_obj_add_bool(doc, obj, "ignored", ignored);
130    }
131
132    FF_LIST_FOR_EACH (FFMouseDevice, device, result) {
133        ffStrbufDestroy(&device->serial);
134        ffStrbufDestroy(&device->name);
135    }
136
137    return true;
138}
139
140void ffInitMouseOptions(FFMouseOptions* options) {
141    ffOptionInitModuleArg(&options->moduleArgs, "󰍽");
142
143    ffListInit(&options->ignores);
144}
145
146void ffDestroyMouseOptions(FFMouseOptions* options) {
147    ffOptionDestroyModuleArg(&options->moduleArgs);
148
149    FF_LIST_FOR_EACH (FFstrbuf, str, options->ignores) {
150        ffStrbufDestroy(str);
151    }
152    ffListDestroy(&options->ignores);
153}
154
155FFModuleBaseInfo ffMouseModuleInfo = {
156    .name = "Mouse",
157    .description = "List connected mice",
158    .displayName = {
159        .en = "Mouse",
160        .ar = "الفأرة",
161        .cs = "Myš",
162        .de = "Maus",
163        .es = "Ratón",
164        .fi = "Hiiri",
165        .fr = "Souris",
166        .gl = "Rato",
167        .he = "עכבר",
168        .id = "Tetikus",
169        .it = "Mouse",
170        .ja = "マウス",
171        .ko = "마우스",
172        .pl = "Mysz",
173        .pt = "Mouse",
174        .ru = "Мышь",
175        .tr = "Fare",
176        .uk = "Миша",
177        .vi = "Chuột",
178        .zh_CN = "鼠标",
179        .zh_TW = "滑鼠",
180    },
181    .initOptions = (void*) ffInitMouseOptions,
182    .destroyOptions = (void*) ffDestroyMouseOptions,
183    .parseJsonObject = (void*) ffParseMouseJsonObject,
184    .printModule = (void*) ffPrintMouse,
185    .generateJsonResult = (void*) ffGenerateMouseJsonResult,
186    .generateJsonConfig = (void*) ffGenerateMouseJsonConfig,
187    .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
188        { "Mouse name", "name" },
189        { "Mouse serial number", "serial" },
190    })),
191    .defaultOrder = 64,
192};