inputor

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

ioctl.lua (1176B)


      1 local bit = require("bit")
      2 local ffi = require("ffi")
      3 
      4 local lshift, bor = bit.lshift, bit.bor
      5 
      6 local module = {}
      7 
      8 ffi.cdef[[
      9 int __ioctl_time64(int fd, unsigned long request, ...);
     10 int ioctl(int fd, unsigned long request, ...);
     11 ]]
     12 
     13 -- __ioctl_time64 is not always available
     14 module.ioctl = ffi.C.ioctl
     15 pcall(function() module.ioctl = ffi.C.__ioctl_time64 end)
     16 
     17 local typebits, nrbits, sizebits = 8, 8, 14
     18 local arch = ffi.arch
     19 if arch == "ppc" or arch:sub(1,4) == "mips" then
     20   -- TODO: alpha, sparc (when supported by luajit)
     21   sizebits = 13
     22 end
     23 
     24 local nrshift = 0
     25 local typeshift = nrshift + nrbits
     26 local sizeshift = typeshift + typebits
     27 local dirshift = sizeshift + sizebits
     28 
     29 module.nrshift = nrshift
     30 module.typeshift = typeshift
     31 module.sizeshift = sizeshift
     32 module.dirshift = dirshift
     33 
     34 local write_bit = 1
     35 local read_bit = 2
     36 
     37 function module.ioctl_req(rd, wr, type, nr, size)
     38   local dir = 0ULL -- do not convert numbers to int32
     39   if rd then dir = dir + read_bit end
     40   if wr then dir = dir + write_bit end
     41   return bor(lshift(dir, dirshift),
     42              lshift(type, typeshift),
     43              lshift(nr, nrshift),
     44              lshift(size, sizeshift))
     45 end
     46 
     47 return module