2024-10-09 09:45:09 +08:00
|
|
|
#include "common/percent.h"
|
|
|
|
|
#include "common/printing.h"
|
|
|
|
|
#include "common/jsonconfig.h"
|
|
|
|
|
#include "detection/keyboard/keyboard.h"
|
|
|
|
|
#include "modules/keyboard/keyboard.h"
|
|
|
|
|
#include "util/stringUtils.h"
|
|
|
|
|
|
|
|
|
|
static void printDevice(FFKeyboardOptions* options, const FFKeyboardDevice* device, uint8_t index)
|
|
|
|
|
{
|
|
|
|
|
if(options->moduleArgs.outputFormat.length == 0)
|
|
|
|
|
{
|
|
|
|
|
ffPrintLogoAndKey(FF_KEYBOARD_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
|
|
|
|
ffStrbufPutTo(&device->name, stdout);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-12-11 10:47:02 +08:00
|
|
|
FF_PRINT_FORMAT_CHECKED(FF_KEYBOARD_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
|
2024-10-09 09:45:09 +08:00
|
|
|
FF_FORMAT_ARG(device->name, "name"),
|
|
|
|
|
FF_FORMAT_ARG(device->serial, "serial"),
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffPrintKeyboard(FFKeyboardOptions* options)
|
|
|
|
|
{
|
|
|
|
|
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFKeyboardDevice));
|
|
|
|
|
|
|
|
|
|
const char* error = ffDetectKeyboard(&result);
|
|
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
|
|
|
|
ffPrintError(FF_KEYBOARD_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!result.length)
|
|
|
|
|
{
|
|
|
|
|
ffPrintError(FF_KEYBOARD_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No devices detected");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t index = 0;
|
|
|
|
|
FF_LIST_FOR_EACH(FFKeyboardDevice, device, result)
|
|
|
|
|
{
|
|
|
|
|
printDevice(options, device, result.length > 1 ? ++index : 0);
|
|
|
|
|
ffStrbufDestroy(&device->serial);
|
|
|
|
|
ffStrbufDestroy(&device->name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffParseKeyboardJsonObject(FFKeyboardOptions* 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;
|
2025-08-02 18:51:03 +08:00
|
|
|
yyjson_obj_foreach(module, idx, max, key, val)
|
2024-10-09 09:45:09 +08:00
|
|
|
{
|
|
|
|
|
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
|
|
|
|
continue;
|
|
|
|
|
|
2025-08-02 18:51:03 +08:00
|
|
|
ffPrintError(FF_KEYBOARD_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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffGenerateKeyboardJsonConfig(FFKeyboardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
2025-08-04 16:46:28 +08:00
|
|
|
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
|
2024-10-09 09:45:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffGenerateKeyboardJsonResult(FF_MAYBE_UNUSED FFKeyboardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
|
|
|
|
{
|
|
|
|
|
FF_LIST_AUTO_DESTROY result = ffListCreate(sizeof(FFKeyboardDevice));
|
|
|
|
|
|
|
|
|
|
const char* error = ffDetectKeyboard(&result);
|
|
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
|
|
|
|
|
FF_LIST_FOR_EACH(FFKeyboardDevice, device, result)
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FF_LIST_FOR_EACH(FFKeyboardDevice, device, result)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufDestroy(&device->serial);
|
|
|
|
|
ffStrbufDestroy(&device->name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-04 14:25:14 +08:00
|
|
|
void ffInitKeyboardOptions(FFKeyboardOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionInitModuleArg(&options->moduleArgs, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ffDestroyKeyboardOptions(FFKeyboardOptions* options)
|
|
|
|
|
{
|
|
|
|
|
ffOptionDestroyModuleArg(&options->moduleArgs);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-03 22:50:56 +08:00
|
|
|
FFModuleBaseInfo ffKeyboardModuleInfo = {
|
2024-12-11 10:47:02 +08:00
|
|
|
.name = FF_KEYBOARD_MODULE_NAME,
|
|
|
|
|
.description = "List (connected) keyboards",
|
2025-08-03 18:39:41 +08:00
|
|
|
.initOptions = (void*) ffInitKeyboardOptions,
|
|
|
|
|
.destroyOptions = (void*) ffDestroyKeyboardOptions,
|
2024-12-11 10:47:02 +08:00
|
|
|
.parseJsonObject = (void*) ffParseKeyboardJsonObject,
|
|
|
|
|
.printModule = (void*) ffPrintKeyboard,
|
|
|
|
|
.generateJsonResult = (void*) ffGenerateKeyboardJsonResult,
|
|
|
|
|
.generateJsonConfig = (void*) ffGenerateKeyboardJsonConfig,
|
|
|
|
|
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
|
|
|
|
|
{"Name", "name"},
|
|
|
|
|
{"Serial number", "serial"},
|
|
|
|
|
}))
|
|
|
|
|
};
|