Skip to content

Commit 5118789

Browse files
committed
refactor(uinput): migrate spec defaults from C to Lua
1 parent 9f65f7d commit 5118789

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

src/evdev/uinput.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,29 @@ int evdev_create_uinput(lua_State *L) {
236236

237237
luaL_checktype(L, 1, LUA_TTABLE);
238238
spec_index = 1;
239-
path = evdev_get_table_string_field(L, spec_index, "path", "/dev/uinput");
240-
name = evdev_get_table_string_field(L, spec_index, "name",
241-
"Lua evdev virtual keyboard");
239+
240+
if (!evdev_table_has_field(L, spec_index, "path")) {
241+
return evdev_push_error(L, "missing field 'path'");
242+
}
243+
path = evdev_get_table_string_field(L, spec_index, "path", NULL);
244+
245+
if (!evdev_table_has_field(L, spec_index, "name")) {
246+
return evdev_push_error(L, "missing field 'name'");
247+
}
248+
name = evdev_get_table_string_field(L, spec_index, "name", NULL);
249+
250+
if (!evdev_table_has_field(L, spec_index, "bustype")) {
251+
return evdev_push_error(L, "missing field 'bustype'");
252+
}
253+
if (!evdev_table_has_field(L, spec_index, "vendor")) {
254+
return evdev_push_error(L, "missing field 'vendor'");
255+
}
256+
if (!evdev_table_has_field(L, spec_index, "product")) {
257+
return evdev_push_error(L, "missing field 'product'");
258+
}
259+
if (!evdev_table_has_field(L, spec_index, "version")) {
260+
return evdev_push_error(L, "missing field 'version'");
261+
}
242262

243263
fd = evdev_open_cloexec(path, O_WRONLY | O_NONBLOCK);
244264
if (fd < 0) {
@@ -266,13 +286,13 @@ int evdev_create_uinput(lua_State *L) {
266286
memset(&setup, 0, sizeof(setup));
267287
snprintf(setup.name, UINPUT_MAX_NAME_SIZE, "%s", name);
268288
setup.id.bustype = (unsigned short)evdev_get_table_integer_field(
269-
L, spec_index, "bustype", BUS_USB);
289+
L, spec_index, "bustype", 0);
270290
setup.id.vendor = (unsigned short)evdev_get_table_integer_field(
271-
L, spec_index, "vendor", 0x1209);
291+
L, spec_index, "vendor", 0);
272292
setup.id.product = (unsigned short)evdev_get_table_integer_field(
273-
L, spec_index, "product", 0xE7DE);
293+
L, spec_index, "product", 0);
274294
setup.id.version = (unsigned short)evdev_get_table_integer_field(
275-
L, spec_index, "version", 1);
295+
L, spec_index, "version", 0);
276296

277297
if (evdev_write_all(fd, &setup, sizeof(setup)) < 0) {
278298
result = evdev_push_errno(L, "configure uinput", path);

src/evdev/uinput.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ local fmt = string.format
1515
---@diagnostic disable-next-line: missing-fields
1616
local UInput = {}
1717

18+
-- stylua: ignore
19+
---@type evdev.deviceInfo
20+
local defaults = {
21+
path = "/dev/uinput",
22+
name = "Lua Virtual Device",
23+
bustype = 3, -- BUS_USB
24+
vendor = 0x1209,
25+
product = 0xE7DE,
26+
version = 1,
27+
}
28+
1829
---@type fun(dev:evdev.UInput, fname:string, ...):...
1930
local function call_uinput(ui, fname, ...)
2031
local core = rawget(ui, "_core")
@@ -135,6 +146,12 @@ local function normalize(spec)
135146
validate("spec.name", spec.name, "string", true)
136147
validate("spec.path", spec.path, "string", true)
137148

149+
for k, v in pairs(defaults) do
150+
if spec[k] == nil then
151+
spec[k] = v
152+
end
153+
end
154+
138155
normalize_code_list(spec, "keys", "KEY_* or BTN_*")
139156
normalize_code_list(spec, "rels", "REL_*")
140157
normalize_code_list(spec, "event_types", "EV_*")

0 commit comments

Comments
 (0)