Files
fastfetch/src/modules/mouse/mouse.c
T

183 lines
6.3 KiB
C
Raw Normal View History

2026-01-06 09:48:38 +08:00
#include "common/percent.h"
#include "common/printing.h"
#include "common/jsonconfig.h"
2026-05-28 15:58:08 +08:00
#include "common/strutil.h"
2024-10-09 09:45:09 +08:00
#include "detection/mouse/mouse.h"
#include "modules/mouse/mouse.h"
2026-03-29 09:28:49 +08:00
static void printDevice(FFMouseOptions* options, const FFMouseDevice* device, uint8_t index) {
if (options->moduleArgs.outputFormat.length == 0) {
2026-08-14 23:33:54 +08:00
ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Mouse), index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2024-10-09 09:45:09 +08:00
ffStrbufPutTo(&device->name, stdout);
2026-03-29 09:28:49 +08:00
} else {
2026-08-14 23:33:54 +08:00
FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Mouse), index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
2026-03-29 09:28:49 +08:00
FF_ARG(device->name, "name"),
FF_ARG(device->serial, "serial"),
}));
2024-10-09 09:45:09 +08:00
}
}
2026-03-29 09:28:49 +08:00
bool ffPrintMouse(FFMouseOptions* options) {
2026-04-14 10:41:18 +08:00
FF_LIST_AUTO_DESTROY result = ffListCreate();
2024-10-09 09:45:09 +08:00
const char* error = ffDetectMouse(&result);
2026-03-29 09:28:49 +08:00
if (error) {
2026-08-14 23:33:54 +08:00
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Mouse), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
return false;
2024-10-09 09:45:09 +08:00
}
2026-03-29 09:28:49 +08:00
if (!result.length) {
2026-08-14 23:33:54 +08:00
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Mouse), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No devices detected");
return false;
2024-10-09 09:45:09 +08:00
}
2026-04-14 10:41:18 +08:00
FF_LIST_AUTO_DESTROY filtered = ffListCreate();
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFMouseDevice, device, result) {
bool ignored = false;
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFstrbuf, ignore, options->ignores) {
if (ffStrbufStartsWithIgnCase(&device->name, ignore)) {
ignored = true;
break;
}
}
2026-03-29 09:28:49 +08:00
if (!ignored) {
2026-04-14 10:41:18 +08:00
FFMouseDevice** ptr = FF_LIST_ADD(FFMouseDevice*, filtered);
*ptr = device;
}
}
bool ret = true;
2026-03-29 09:28:49 +08:00
if (!filtered.length) {
2026-08-14 23:33:54 +08:00
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Mouse), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "All devices are ignored");
ret = false;
2026-03-29 09:28:49 +08:00
} else {
uint8_t index = 0;
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFMouseDevice*, pdevice, filtered) {
FFMouseDevice* device = *pdevice;
printDevice(options, device, filtered.length > 1 ? ++index : 0);
}
}
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFMouseDevice, device, result) {
2024-10-09 09:45:09 +08:00
ffStrbufDestroy(&device->serial);
ffStrbufDestroy(&device->name);
}
return ret;
2024-10-09 09:45:09 +08:00
}
2026-03-29 09:28:49 +08:00
void ffParseMouseJsonObject(FFMouseOptions* options, yyjson_val* module) {
yyjson_val *key, *val;
2024-10-09 09:45:09 +08:00
size_t idx, max;
2026-03-29 09:28:49 +08:00
yyjson_obj_foreach (module, idx, max, key, val) {
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
2024-10-09 09:45:09 +08:00
continue;
2026-03-29 09:28:49 +08:00
}
2024-10-09 09:45:09 +08:00
2026-03-29 09:28:49 +08:00
if (unsafe_yyjson_equals_str(key, "ignores")) {
yyjson_val* elem;
size_t eidx, emax;
2026-03-29 09:28:49 +08:00
yyjson_arr_foreach (val, eidx, emax, elem) {
if (yyjson_is_str(elem)) {
2026-04-14 10:41:18 +08:00
FFstrbuf* strbuf = FF_LIST_ADD(FFstrbuf, options->ignores);
ffStrbufInitJsonVal(strbuf, elem);
}
}
continue;
}
2026-08-14 23:33:54 +08:00
ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Mouse), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
2024-10-09 09:45:09 +08:00
}
}
2026-03-29 09:28:49 +08:00
void ffGenerateMouseJsonConfig(FFMouseOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
2026-03-29 09:28:49 +08:00
if (options->ignores.length > 0) {
yyjson_mut_val* ignores = yyjson_mut_obj_add_arr(doc, module, "ignores");
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFstrbuf, strbuf, options->ignores) {
yyjson_mut_arr_append(ignores, yyjson_mut_strncpy(doc, strbuf->chars, strbuf->length));
2026-03-29 09:28:49 +08:00
}
}
2024-10-09 09:45:09 +08:00
}
bool ffGenerateMouseJsonResult([[maybe_unused]] FFMouseOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
2026-04-14 10:41:18 +08:00
FF_LIST_AUTO_DESTROY result = ffListCreate();
2024-10-09 09:45:09 +08:00
const char* error = ffDetectMouse(&result);
2026-03-29 09:28:49 +08:00
if (error) {
2024-10-09 09:45:09 +08:00
yyjson_mut_obj_add_str(doc, module, "error", error);
return false;
2024-10-09 09:45:09 +08:00
}
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFMouseDevice, device, result) {
2024-10-09 09:45:09 +08:00
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
yyjson_mut_obj_add_strbuf(doc, obj, "serial", &device->serial);
yyjson_mut_obj_add_strbuf(doc, obj, "name", &device->name);
bool ignored = false;
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFstrbuf, ignore, options->ignores) {
if (ffStrbufStartsWithIgnCase(&device->name, ignore)) {
ignored = true;
break;
}
}
yyjson_mut_obj_add_bool(doc, obj, "ignored", ignored);
2024-10-09 09:45:09 +08:00
}
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFMouseDevice, device, result) {
2024-10-09 09:45:09 +08:00
ffStrbufDestroy(&device->serial);
ffStrbufDestroy(&device->name);
}
return true;
2024-10-09 09:45:09 +08:00
}
2026-03-29 09:28:49 +08:00
void ffInitMouseOptions(FFMouseOptions* options) {
ffOptionInitModuleArg(&options->moduleArgs, "󰍽");
2026-04-14 10:41:18 +08:00
ffListInit(&options->ignores);
}
2026-03-29 09:28:49 +08:00
void ffDestroyMouseOptions(FFMouseOptions* options) {
ffOptionDestroyModuleArg(&options->moduleArgs);
2026-03-29 09:28:49 +08:00
FF_LIST_FOR_EACH (FFstrbuf, str, options->ignores) {
ffStrbufDestroy(str);
2026-03-29 09:28:49 +08:00
}
ffListDestroy(&options->ignores);
}
2025-08-03 22:50:56 +08:00
FFModuleBaseInfo ffMouseModuleInfo = {
2026-08-14 23:33:54 +08:00
.name = "Mouse",
2026-04-16 18:27:11 +08:00
.description = "List connected mice",
2026-08-14 20:38:58 +08:00
.displayName = {
.en = "Mouse",
.de = "Maus",
.es = "Ratón",
.fr = "Souris",
.it = "Mouse",
.ja = "マウス",
.ko = "마우스",
.pl = "Mysz",
.pt_BR = "Mouse",
.ru = "Мышь",
.zh_CN = "鼠标",
.zh_TW = "滑鼠",
},
.initOptions = (void*) ffInitMouseOptions,
.destroyOptions = (void*) ffDestroyMouseOptions,
2024-12-11 10:47:02 +08:00
.parseJsonObject = (void*) ffParseMouseJsonObject,
.printModule = (void*) ffPrintMouse,
.generateJsonResult = (void*) ffGenerateMouseJsonResult,
.generateJsonConfig = (void*) ffGenerateMouseJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
2026-03-30 10:27:23 +08:00
{ "Mouse name", "name" },
{ "Mouse serial number", "serial" },
})),
.defaultOrder = 63,
};