Skip to content

Commit 2210bc4

Browse files
committed
feat: support new evdev.device without fd
1 parent b09c0e4 commit 2210bc4

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ local Device = require("evdev.device")
1313
local function print_device(dev)
1414
local name = dev:name():gsub("\n", " | ")
1515
print("============", "===")
16-
print("= Pathname: ", dev.pathname)
1716
print("= Name: ", name)
1817
print("= Phys: ", dev:phys())
1918
print("= Uniq: ", dev:uniq())

evdev/device.lua

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,52 @@ local libevdev_read_flag = evdev_enum.libevdev_read_flag
1313
local open_flag = util.enum.open_flag
1414

1515
---@class Device
16-
---@field fd number
17-
---@field pathname string
1816
---@field dev ffi.cdata*
1917
local Device = {}
2018

21-
---@param pathname string
22-
---@param flags number[] `open_flag[]`
19+
---@param fd_or_pathname? number|string
20+
---@param flags? nil|number[]
2321
---@return Device
24-
local function init(class, pathname, flags)
22+
local function init(class, fd_or_pathname, flags)
2523
---@type Device
2624
local self = setmetatable({}, { __index = class })
2725

28-
self.pathname = pathname
29-
30-
local fd = util.open_file(pathname, flags or { open_flag.RDONLY, open_flag.NONBLOCK })
31-
if fd < 0 then
32-
return nil, string.format("Error: %s", util.err_string(ffi.errno()))
26+
---@type nil|number
27+
local fd
28+
if type(fd_or_pathname) == "number" then
29+
fd = fd_or_pathname
30+
elseif type(fd_or_pathname) == "string" then
31+
fd = util.open_file(fd_or_pathname, flags or { open_flag.RDONLY, open_flag.NONBLOCK })
32+
if fd < 0 then
33+
return nil, string.format("Error: can't open %s - %s", fd_or_pathname, util.err_string(ffi.errno()))
34+
end
3335
end
3436

35-
self.fd = fd
37+
if fd then
38+
local dev_ptr = libevdev.ctype.libevdev_ptr()
3639

37-
local dev_ptr = libevdev.ctype.libevdev_ptr()
40+
local rc = evdev.libevdev_new_from_fd(fd, dev_ptr)
41+
if rc < 0 then
42+
return nil, string.format("Error: %s", util.err_string(-rc))
43+
end
3844

39-
local rc = evdev.libevdev_new_from_fd(fd, dev_ptr)
40-
if rc < 0 then
41-
return nil, string.format("Error: %s", util.err_string(-rc))
45+
self.dev = dev_ptr[0]
46+
else
47+
self.dev = evdev.libevdev_new()
4248
end
4349

44-
self.dev = dev_ptr[0]
45-
4650
ffi.gc(self.dev, evdev.libevdev_free)
4751

4852
return self
4953
end
5054

51-
---@param pathname string
52-
---@param flags number[] `open_flag[]`
53-
---@return Device
54-
function Device:new(pathname, flags)
55-
return init(self, pathname, flags)
55+
---@override fun(): Device
56+
---@override fun(fd: number): Device
57+
---@override fun(pathname: string, flags: number[]): Device
58+
---@param fd_or_pathname? number|string
59+
---@param flags? nil|number[]
60+
function Device:new(fd_or_pathname, flags)
61+
return init(self, fd_or_pathname, flags)
5662
end
5763

5864
---@param mode boolean|number `libevdev_grab_mode`

0 commit comments

Comments
 (0)