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/keyboard/keyboard.h"
6#include "modules/keyboard/keyboard.h"
7
8static void printDevice(FFKeyboardOptions* options, const FFKeyboardDevice* device, uint8_t index) {
9 if (options->moduleArgs.outputFormat.length == 0) {
10 ffPrintLogoAndKey(FF_MODULE_GET_DISPLAY_NAME(Keyboard), index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
11 ffStrbufPutTo(&device->name, stdout);
12 } else {
13 FF_PRINT_FORMAT_CHECKED(FF_MODULE_GET_DISPLAY_NAME(Keyboard), 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 ffPrintKeyboard(FFKeyboardOptions* options) {
21 FF_LIST_AUTO_DESTROY result = ffListCreate();
22
23 const char* error = ffDetectKeyboard(&result);
24
25 if (error) {
26 ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Keyboard), 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(Keyboard), 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 (FFKeyboardDevice, 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 FFKeyboardDevice** ptr = FF_LIST_ADD(FFKeyboardDevice*, filtered);
46 *ptr = device;
47 }
48 }
49
50 bool ret = true;
51 if (!filtered.length) {
52 ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Keyboard), 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 (FFKeyboardDevice*, pdevice, filtered) {
57 printDevice(options, *pdevice, filtered.length > 1 ? ++index : 0);
58 }
59 }
60
61 // `filtered` only holds borrowed pointers, so the strings of every device — including the
62 // ignored ones and the ones skipped when every device is ignored — have to be released here
63 FF_LIST_FOR_EACH (FFKeyboardDevice, device, result) {
64 ffStrbufDestroy(&device->serial);
65 ffStrbufDestroy(&device->name);
66 }
67
68 return ret;
69}
70
71void ffParseKeyboardJsonObject(FFKeyboardOptions* options, yyjson_val* module) {
72 yyjson_val *key, *val;
73 size_t idx, max;
74 yyjson_obj_foreach (module, idx, max, key, val) {
75 if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs)) {
76 continue;
77 }
78
79 if (unsafe_yyjson_equals_str(key, "ignores")) {
80 yyjson_val* elem;
81 size_t eidx, emax;
82 yyjson_arr_foreach (val, eidx, emax, elem) {
83 if (yyjson_is_str(elem)) {
84 FFstrbuf* strbuf = FF_LIST_ADD(FFstrbuf, options->ignores);
85 ffStrbufInitJsonVal(strbuf, elem);
86 }
87 }
88 continue;
89 }
90
91 ffPrintError(FF_MODULE_GET_DISPLAY_NAME(Keyboard), 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
92 }
93}
94
95[[gnu::cold]]
96void ffGenerateKeyboardJsonConfig(FFKeyboardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
97 ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
98
99 if (options->ignores.length > 0) {
100 yyjson_mut_val* ignores = yyjson_mut_obj_add_arr(doc, module, "ignores");
101 FF_LIST_FOR_EACH (FFstrbuf, strbuf, options->ignores) {
102 yyjson_mut_arr_append(ignores, yyjson_mut_strncpy(doc, strbuf->chars, strbuf->length));
103 }
104 }
105}
106
107bool ffGenerateKeyboardJsonResult([[maybe_unused]] FFKeyboardOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) {
108 FF_LIST_AUTO_DESTROY result = ffListCreate();
109
110 const char* error = ffDetectKeyboard(&result);
111
112 if (error) {
113 yyjson_mut_obj_add_str(doc, module, "error", error);
114 return false;
115 }
116
117 yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
118 FF_LIST_FOR_EACH (FFKeyboardDevice, device, result) {
119 yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
120 yyjson_mut_obj_add_strbuf(doc, obj, "serial", &device->serial);
121 yyjson_mut_obj_add_strbuf(doc, obj, "name", &device->name);
122
123 bool ignored = false;
124 FF_LIST_FOR_EACH (FFstrbuf, ignore, options->ignores) {
125 if (ffStrbufStartsWithIgnCase(&device->name, ignore)) {
126 ignored = true;
127 break;
128 }
129 }
130 yyjson_mut_obj_add_bool(doc, obj, "ignored", ignored);
131 }
132
133 FF_LIST_FOR_EACH (FFKeyboardDevice, device, result) {
134 ffStrbufDestroy(&device->serial);
135 ffStrbufDestroy(&device->name);
136 }
137
138 return true;
139}
140
141void ffInitKeyboardOptions(FFKeyboardOptions* options) {
142 ffOptionInitModuleArg(&options->moduleArgs, "");
143
144 ffListInit(&options->ignores);
145}
146
147void ffDestroyKeyboardOptions(FFKeyboardOptions* options) {
148 ffOptionDestroyModuleArg(&options->moduleArgs);
149
150 FF_LIST_FOR_EACH (FFstrbuf, str, options->ignores) {
151 ffStrbufDestroy(str);
152 }
153 ffListDestroy(&options->ignores);
154}
155
156FFModuleBaseInfo ffKeyboardModuleInfo = {
157 .name = "Keyboard",
158 .description = "List connected keyboards",
159 .displayName = {
160 .en = "Keyboard",
161 .ar = "لوحة المفاتيح",
162 .cs = "Klávesnice",
163 .de = "Tastatur",
164 .es = "Teclado",
165 .fi = "Näppäimistö",
166 .fr = "Clavier",
167 .gl = "Teclado",
168 .he = "מקלדת",
169 .id = "Papan Ketik",
170 .it = "Tastiera",
171 .ja = "キーボード",
172 .ko = "키보드",
173 .pl = "Klawiatura",
174 .pt = "Teclado",
175 .ru = "Клавиатура",
176 .tr = "Klavye",
177 .uk = "Клавіатура",
178 .vi = "Bàn phím",
179 .zh_CN = "键盘",
180 .zh_TW = "鍵盤",
181 },
182 .initOptions = (void*) ffInitKeyboardOptions,
183 .destroyOptions = (void*) ffDestroyKeyboardOptions,
184 .parseJsonObject = (void*) ffParseKeyboardJsonObject,
185 .printModule = (void*) ffPrintKeyboard,
186 .generateJsonResult = (void*) ffGenerateKeyboardJsonResult,
187 .generateJsonConfig = (void*) ffGenerateKeyboardJsonConfig,
188 .formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
189 { "Name", "name" },
190 { "Serial number", "serial" },
191 })),
192 .defaultOrder = 65,
193};