You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.2 KiB
Lua

local base = string.match(..., "(.-)[^%.]+%.[^%.]+$")
local eh = require(base.."helper.enum")
local enums = require(base.."ev_api.decls").enums
local ffi = require("ffi")
local fh = require(base.."helper.ffi_struct")
local error, tonumber, type = error, tonumber, type
local format = string.format
local floor = math.floor
local os_date = os.date
local enum_to_int = eh.to_int
local mt, index = {}, {}
-- TIME
function index:get_posix_time() return self.raw.__sec end
function index:set_posix_time(sec)
local raw = self.raw
raw.__sec = sec
raw.__usec = 0
end
function index:get_posix_time_usec()
local raw = self.raw
return raw.__sec * 1000000 + raw.__usec
end
function index:set_posix_time_usec(usec)
local raw = self.raw
raw.__sec = floor(usec / 1000000)
raw.__usec = usec % 1000000
end
function index:get_time()
local raw = self.raw
return tonumber(raw.__sec) + tonumber(raw.__usec) / 1000000
end
function index:set_time(t)
local raw = self.raw
raw.__sec = t
raw.__usec = (t % 1) * 1000000
end
function index:get_time_str()
local raw = self.raw
local a = os_date('%Y-%m-%d %H:%M:%S', tonumber(raw.__sec))
local b = format('%06d', tonumber(raw.__usec))
return a.."."..b
end
-- TYPE
local EV = enums.EV
fh.gen_enum_accessor(index, EV, "type")
-- CODE
function index:get_code()
local raw = self.raw
local x = enums[EV[raw.type]]
return x and x[raw.code] or raw.code
end
function index:set_code(val)
local raw = self.raw
local t = type(val)
if t == "number" or t == "cdata" then
raw.code = val
elseif t == "string" then
local x = enums[self.type]
if not x then error("Type "..self.type.." has no codes") end
raw.code = enum_to_int(x, val)
else
error("Invalid enum type "..t)
end
end
-- VALUE
fh.gen_raw_accessor(index, "value")
-- MISC
-- hack: we need to set type before setting code
local orig_set = fh.base_funcs.set_from_table
function index:set_from_table(tbl)
if tbl.type then self.type = tbl.type end
orig_set(self, tbl)
end
function index:tostring()
return format("event{time=%s, type=%s, code=%s, value=%#x}",
self.time_str, self.type, self.code, self.value)
end
mt.__tostring = index.tostring
return fh.base(mt, index, ffi.typeof("struct input_event"))