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.

80 lines
2.1 KiB
Lua

local fn_fw, fn_acc = ...
local input = require("lib.input")
local uinput = require("lib.uinput")
local poll = require("lib.helper.posix_io").poll_read
local inp_fwd, inp_acc = input(fn_fw), input(fn_acc)
local ui = uinput()
local function to_float(axis)
local info = inp_acc.absinfo[axis]
local min, max = info.minimum, info.maximum
if min < 0 and min == -max then
local mul = 1/max
return function(v) return v.value * mul end
else
local mul = 2 / (max - min)
return function(v) return (v.value + min) * mul - 1 end
end
end
local MAP = { X = to_float("X"), Z = to_float("Z") }
local AXIS = "X"
local MUL = 0xffff
ui:copy_caps(inp_fwd)
ui:abs_setup("Z", {minimum=0,maximum=256*2-1})
ui:abs_setup("RZ", {minimum=0,maximum=256*2-1})
if AXIS then ui:abs_setup(AXIS, {minimum = -MUL, maximum = MUL}) end
ui:setup{
-- id=inp.device_id,
-- some apps (like wine) doesn't like if you have multiple devices with the
-- same id, in that case fake something here:
id={bustype="BLUETOOTH", vendor=1, product=2},
name="Virtual gamepad",
ff_effects_max=inp_fwd.ff_count
}
ui:create()
print("created", ui.sysname)
inp_fwd:grab() -- ! DANGER
inp_acc:grab()
local to_poll = { inp_fwd, inp_acc, ui }
local ui_ff = ui:gen_ff_forwarder(inp_fwd)
local vals = { X = 0, Z = 0 }
local function recalc()
local v = math.atan2(-vals.X, -vals.Z)
if v < -1 then v = -1 elseif v > 1 then v = 1 end
--print(v*MUL)
ui:write{type="ABS", code=AXIS, value=v*MUL}
end
while true do
local rdy = poll(to_poll)
if rdy[inp_fwd] then
local rd = inp_fwd:read()
if rd.type ~= "ABS" or rd.code ~= AXIS then
if rd.type == "ABS" and (rd.code == "Z" or rd.code == "RZ") then
rd.value = rd.value + 256
end
ui:write(rd)
end
end
if rdy[ui] then ui_ff() end
if rdy[inp_acc] then
local rd = inp_acc:read()
if rd.type == "ABS" and MAP[rd.code] then
local c = rd.code
vals[c] = MAP[c](rd)
elseif rd.type == "SYN" and rd.code == "REPORT" and AXIS then
recalc()
ui:write{type="SYN", code="REPORT"}
end
end
end