Skip to content

Commit 82bafb9

Browse files
committed
refactor: extract ctype constructors
1 parent 2040166 commit 82bafb9

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

evdev/device.lua

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local ffi = require("ffi")
22

3+
local input = require("evdev.linux.input")
34
local Event = require("evdev.event")
45
local evdev = require("evdev.libevdev")
56
local util = require("evdev.util")
@@ -8,6 +9,8 @@ local libevdev_grab_mode = evdev.enum.libevdev_grab_mode
89
local libevdev_read_flag = evdev.enum.libevdev_read_flag
910
local open_flag = util.enum.open_flag
1011

12+
local new_libevdev_ptr = ffi.typeof("struct libevdev *[1]")
13+
1114
---@class Device
1215
---@field fd number
1316
---@field pathname string
@@ -30,7 +33,7 @@ local function init(class, pathname, flags)
3033

3134
self.fd = fd
3235

33-
local dev_ptr = ffi.new("struct libevdev *[1]")
36+
local dev_ptr = new_libevdev_ptr()
3437

3538
local rc = evdev.libevdev_new_from_fd(fd, dev_ptr)
3639
if rc < 0 then
@@ -194,10 +197,10 @@ end
194197

195198
---@param ev_type number
196199
---@param code number
197-
---@param value_ptr? ffi.cdata*
200+
---@param value_ptr? ffi.cdata*|{ [0]: number }
198201
---@return number|nil
199202
function Device:fetch_event_value(ev_type, code, value_ptr)
200-
value_ptr = value_ptr or ffi.new("int[1]")
203+
value_ptr = value_ptr or input.new_int_ptr()
201204
local rc = evdev.libevdev_fetch_event_value(self.dev, ev_type, code, value_ptr)
202205
if rc ~= 0 then
203206
return value_ptr[0]
@@ -213,10 +216,10 @@ end
213216

214217
---@param slot number
215218
---@param code number
216-
---@param value_ptr? ffi.cdata*
219+
---@param value_ptr? ffi.cdata*|{ [0]: number }
217220
---@return number|nil
218221
function Device:fetch_slot_value(slot, code, value_ptr)
219-
value_ptr = value_ptr or ffi.new("int[1]")
222+
value_ptr = value_ptr or input.new_int_ptr()
220223
local rc = evdev.libevdev_fetch_slot_value(self.dev, slot, code, value_ptr)
221224
if rc ~= 0 then
222225
return value_ptr[0]
@@ -252,9 +255,9 @@ end
252255
function Device:enable_event_code(ev_type, code, data)
253256
local data_type = type(data)
254257
if data_type == "number" then
255-
data = ffi.new("int", data)
258+
data = input.new_int(data)
256259
elseif data_type == "table" then
257-
data = ffi.new("struct input_absinfo", data)
260+
data = input.new_input_absinfo(data)
258261
end
259262

260263
return evdev.libevdev_enable_event_code(self.dev, ev_type, code, data)

evdev/event.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
local ffi = require("ffi")
2-
31
local input = require("evdev.linux.input")
42

53
---@class Event
@@ -12,7 +10,7 @@ local function init(class, ev)
1210
---@type Event
1311
local self = setmetatable({}, { __index = class })
1412

15-
self.ev = ev or ffi.new("struct input_event")
13+
self.ev = ev or input.new_input_event()
1614

1715
return self
1816
end

evdev/linux/input.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ struct ff_effect {
124124
---@alias evdev_input_absinfo { value: number, minimum: number, maximum: number, fuzz: number, flat: number, resolution: number }
125125

126126
local int = ffi.typeof("int")
127+
local int_ptr = ffi.typeof("int[1]")
127128
local uint2 = ffi.typeof("unsigned int[2]")
129+
local input_event = ffi.typeof("struct input_event")
128130
local input_id = ffi.typeof("struct input_id")
129131
local input_absinfo = ffi.typeof("struct input_absinfo")
130132
local input_keymap_entry = ffi.typeof("struct input_keymap_entry")
@@ -205,6 +207,18 @@ local mod = {
205207
EVIOCSCLOCKID = _IOW("E", 0xa0, int),
206208
}
207209

210+
---@type fun(init: number|nil): ffi.ctype*
211+
mod.new_int = int
212+
213+
---@type fun(): ffi.ctype*|{ [0]: number }
214+
mod.new_int_ptr = int_ptr
215+
216+
---@type fun(): ffi.ctype*|evdev_input_event
217+
mod.new_input_event = input_event
218+
219+
---@type fun(init: evdev_input_absinfo|nil): ffi.ctype*|evdev_input_absinfo
220+
mod.new_input_absinfo = input_absinfo
221+
208222
local const = require("evdev.linux.input-constant")
209223

210224
mod.grouped_constant = const

0 commit comments

Comments
 (0)