Skip to content

Commit 975b7a5

Browse files
committed
refactor: move to_fd to util
1 parent e77c00e commit 975b7a5

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

evdev/device.lua

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,38 +10,19 @@ local evdev = libevdev.lib
1010

1111
local libevdev_grab_mode = evdev_enum.libevdev_grab_mode
1212
local libevdev_read_flag = evdev_enum.libevdev_read_flag
13-
local open_flag = util.enum.open_flag
1413

1514
---@class Device
1615
---@field dev ffi.cdata*
1716
local Device = {}
1817

19-
---@param fd_or_pathname? number|string
20-
---@param flags? nil|number[]
21-
---@return number fd, string|nil err
22-
local function get_fd(fd_or_pathname, flags)
23-
local fd
24-
25-
if type(fd_or_pathname) == "number" then
26-
fd = fd_or_pathname
27-
elseif type(fd_or_pathname) == "string" then
28-
fd = util.open_file(fd_or_pathname, flags or { open_flag.RDONLY, open_flag.NONBLOCK })
29-
if fd < 0 then
30-
return nil, string.format("Error: can't open %s - %s", fd_or_pathname, util.err_string(ffi.errno()))
31-
end
32-
end
33-
34-
return fd
35-
end
36-
3718
---@param fd_or_pathname? number|string
3819
---@param flags? nil|number[]
3920
---@return Device
4021
local function init(class, fd_or_pathname, flags)
4122
---@type Device
4223
local self = setmetatable({}, { __index = class })
4324

44-
local fd, fd_err = get_fd(fd_or_pathname, flags)
25+
local fd, fd_err = util.to_fd(fd_or_pathname, flags)
4526
if fd_err then
4627
return nil, fd_err
4728
end
@@ -89,7 +70,7 @@ function Device:fd(fd_or_pathname, flags)
8970
return self._fd
9071
end
9172

92-
local fd, fd_err = get_fd(fd_or_pathname, flags)
73+
local fd, fd_err = util.to_fd(fd_or_pathname, flags)
9374
if fd_err then
9475
return nil, fd_err
9576
end

evdev/util.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,22 @@ function mod.err_string(errnum)
6868
return mod.to_string(clib.strerror(errnum))
6969
end
7070

71+
---@param fd_or_pathname? number|string
72+
---@param flags? nil|number[]
73+
---@return nil|number fd, string|nil err
74+
function mod.to_fd(fd_or_pathname, flags)
75+
local fd
76+
77+
if type(fd_or_pathname) == "number" then
78+
fd = fd_or_pathname
79+
elseif type(fd_or_pathname) == "string" then
80+
fd = mod.open_file(fd_or_pathname, flags or { enum.open_flag.RDONLY, enum.open_flag.NONBLOCK })
81+
if fd < 0 then
82+
return nil, string.format("Error: can't open %s - %s", fd_or_pathname, mod.err_string(ffi.errno()))
83+
end
84+
end
85+
86+
return fd
87+
end
88+
7189
return mod

0 commit comments

Comments
 (0)