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) {
|
2024-10-09 09:45:09 +08:00
|
|
|
ffPrintLogoAndKey(FF_MOUSE_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
|
|
|
|
ffStrbufPutTo(&device->name, stdout);
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2024-12-11 10:47:02 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_MOUSE_MODULE_NAME, 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) {
|
2024-10-09 09:45:09 +08:00
|
|
|
ffPrintError(FF_MOUSE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
2025-08-18 13:38:30 +08:00
|
|
|
return false;
|
2024-10-09 09:45:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (!result.length) {
|
2024-10-09 09:45:09 +08:00
|
|
|
ffPrintError(FF_MOUSE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No devices detected");
|
2025-08-18 13:38:30 +08:00
|
|
|
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) {
|
2025-09-19 11:00:18 +08:00
|
|
|
bool ignored = false;
|
2026-03-29 09:28:49 +08:00
|
|
|
FF_LIST_FOR_EACH (FFstrbuf, ignore, options->ignores) {
|
|
|
|
|
if (ffStrbufStartsWithIgnCase(&device->name, ignore)) {
|
2025-09-19 11:00:18 +08:00
|
|
|
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);
|
2025-09-19 11:00:18 +08:00
|
|
|
*ptr = device;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 13:36:02 +08:00
|
|
|
bool ret = true;
|
2026-03-29 09:28:49 +08:00
|
|
|
if (!filtered.length) {
|
2025-09-19 11:00:18 +08:00
|
|
|
ffPrintError(FF_MOUSE_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "All devices are ignored");
|
2025-09-19 13:36:02 +08:00
|
|
|
ret = false;
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2025-09-19 13:36:02 +08:00
|
|
|
uint8_t index = 0;
|
2026-03-29 09:28:49 +08:00
|
|
|
FF_LIST_FOR_EACH (FFMouseDevice*, pdevice, filtered) {
|
2025-09-19 13:36:02 +08:00
|
|
|
FFMouseDevice* device = *pdevice;
|
|
|
|
|
printDevice(options, device, filtered.length > 1 ? ++index : 0);
|
|
|
|
|
}
|
2025-09-19 11:00:18 +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);
|
|
|
|
|
}
|
2025-08-18 13:38:30 +08:00
|
|
|
|
2025-09-19 13:36:02 +08:00
|
|
|
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) {
|
2025-08-02 18:51:03 +08:00
|
|
|
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;
|
2025-09-19 11:00:18 +08:00
|
|
|
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);
|
2025-09-19 11:00:18 +08:00
|
|
|
ffStrbufInitJsonVal(strbuf, elem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
ffPrintError(FF_MOUSE_MODULE_NAME, 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) {
|
2025-08-04 16:46:28 +08:00
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
|
2025-09-19 11:00:18 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (options->ignores.length > 0) {
|
2025-09-19 11:00:18 +08:00
|
|
|
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) {
|
2025-09-19 11:00:18 +08:00
|
|
|
yyjson_mut_arr_append(ignores, yyjson_mut_strncpy(doc, strbuf->chars, strbuf->length));
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2025-09-19 11:00:18 +08:00
|
|
|
}
|
2024-10-09 09:45:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-10 21:13:03 +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);
|
2025-08-18 13:38:30 +08:00
|
|
|
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);
|
2025-09-19 11:00:18 +08:00
|
|
|
|
|
|
|
|
bool ignored = false;
|
2026-03-29 09:28:49 +08:00
|
|
|
FF_LIST_FOR_EACH (FFstrbuf, ignore, options->ignores) {
|
|
|
|
|
if (ffStrbufStartsWithIgnCase(&device->name, ignore)) {
|
2025-09-19 11:00:18 +08:00
|
|
|
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);
|
|
|
|
|
}
|
2025-08-18 13:38:30 +08:00
|
|
|
|
|
|
|
|
return true;
|
2024-10-09 09:45:09 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffInitMouseOptions(FFMouseOptions* options) {
|
2025-08-04 14:25:14 +08:00
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
2025-09-19 11:00:18 +08:00
|
|
|
|
2026-04-14 10:41:18 +08:00
|
|
|
ffListInit(&options->ignores);
|
2025-08-04 14:25:14 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffDestroyMouseOptions(FFMouseOptions* options) {
|
2025-08-04 14:25:14 +08:00
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
2025-09-19 11:00:18 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
FF_LIST_FOR_EACH (FFstrbuf, str, options->ignores) {
|
2025-09-19 11:00:18 +08:00
|
|
|
ffStrbufDestroy(str);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2025-09-19 11:00:18 +08:00
|
|
|
ffListDestroy(&options->ignores);
|
2025-08-04 14:25:14 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-03 22:50:56 +08:00
|
|
|
FFModuleBaseInfo ffMouseModuleInfo = {
|
2024-12-11 10:47:02 +08:00
|
|
|
.name = FF_MOUSE_MODULE_NAME,
|
2026-04-16 18:27:11 +08:00
|
|
|
.description = "List connected mice",
|
2025-08-03 18:39:41 +08:00
|
|
|
.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" },
|
2026-03-30 10:17:45 +08:00
|
|
|
}))
|
|
|
|
|
};
|