inputor

Evdev remapping utility in lua
git clone https://git.neptards.moe/u3shit/inputor.git
Log | Files | Refs

event.lua (2269B)


      1 local base = string.match(..., "(.-)[^%.]+%.[^%.]+$")
      2 local eh = require(base.."helper.enum")
      3 local enums = require(base.."ev_api.decls").enums
      4 local ffi = require("ffi")
      5 local fh = require(base.."helper.ffi_struct")
      6 
      7 local error, tonumber, type = error, tonumber, type
      8 local format = string.format
      9 local floor = math.floor
     10 local os_date = os.date
     11 local enum_to_int = eh.to_int
     12 
     13 local mt, index = {}, {}
     14 
     15 -- TIME
     16 function index:get_posix_time() return self.raw.__sec end
     17 function index:set_posix_time(sec)
     18   local raw = self.raw
     19   raw.__sec = sec
     20   raw.__usec = 0
     21 end
     22 
     23 function index:get_posix_time_usec()
     24   local raw = self.raw
     25   return raw.__sec * 1000000 + raw.__usec
     26 end
     27 function index:set_posix_time_usec(usec)
     28   local raw = self.raw
     29   raw.__sec = floor(usec / 1000000)
     30   raw.__usec = usec % 1000000
     31 end
     32 
     33 function index:get_time()
     34   local raw = self.raw
     35   return tonumber(raw.__sec) + tonumber(raw.__usec) / 1000000
     36 end
     37 function index:set_time(t)
     38   local raw = self.raw
     39   raw.__sec = t
     40   raw.__usec = (t % 1) * 1000000
     41 end
     42 
     43 function index:get_time_str()
     44   local raw = self.raw
     45   local a = os_date('%Y-%m-%d %H:%M:%S', tonumber(raw.__sec))
     46   local b = format('%06d', tonumber(raw.__usec))
     47   return a.."."..b
     48 end
     49 
     50 -- TYPE
     51 local EV = enums.EV
     52 fh.gen_enum_accessor(index, EV, "type")
     53 
     54 -- CODE
     55 function index:get_code()
     56   local raw = self.raw
     57   local x = enums[EV[raw.type]]
     58   return x and x[raw.code] or raw.code
     59 end
     60 
     61 function index:set_code(val)
     62   local raw = self.raw
     63   local t = type(val)
     64   if t == "number" or t == "cdata" then
     65     raw.code = val
     66   elseif t == "string" then
     67     local x = enums[self.type]
     68     if not x then error("Type "..self.type.." has no codes") end
     69     raw.code = enum_to_int(x, val)
     70   else
     71     error("Invalid enum type "..t)
     72   end
     73 end
     74 
     75 -- VALUE
     76 fh.gen_raw_accessor(index, "value")
     77 
     78 -- MISC
     79 -- hack: we need to set type before setting code
     80 local orig_set = fh.base_funcs.set_from_table
     81 function index:set_from_table(tbl)
     82   if tbl.type then self.type = tbl.type end
     83   orig_set(self, tbl)
     84 end
     85 
     86 function index:tostring()
     87   return format("event{time=%s, type=%s, code=%s, value=%#x}",
     88                 self.time_str, self.type, self.code, self.value)
     89 end
     90 mt.__tostring = index.tostring
     91 
     92 return fh.base(mt, index, ffi.typeof("struct input_event"))