local fn = ... local input = require("lib.input") local uinput = require("lib.uinput") local poll = require("lib.helper.posix_io").poll_read local inp = input(fn) local ui = uinput() local led = io.popen("echo /sys/class/leds/*:red"):read():gsub(":.*", ":") local function set_color(color, val) local f = io.open(led..color.."/brightness", "w") if not f then return end f:write(tostring(val)) f:close() end ui:copy_caps(inp) 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.ff_count } ui:create() inp:grab() -- ! DANGER local names = { circle = "BTN_EAST", x = "BTN_GAMEPAD", square = "BTN_WEST", triangle = "BTN_NORTH", share = "BTN_SELECT", options = "BTN_START", l1 = "BTN_TL", r1 = "BTN_TR", l2 = "BTN_TL2", r2 = "BTN_TR2", l3 = "BTN_THUMBL", r3 = "BTN_THUMBR", } local mode = "passthrough" local mode_pressed local l1, r1 = false, false local brake, accel = 0, 0 local function write_accel() local val = math.floor((brake - accel) / 2 + 127.5) ui:write{type="ABS", code="RY", value=val} end local menu_rebind = { [names.circle] = names.x, [names.x] = names.triangle, [names.triangle] = names.circle, } -- A config -- Left L-R = steer; Right U-D = accel/brake -- R1 = handbrake, circle = horn, triangle = camera -- R3 = speed boost, L1 = look back -- start = pause, L2 = zone freeze, R2 = zone zoom, POV down = cycle hud -- R2 = shift up, L2 = shift down -- I want: triangle = camera, [] = horn, x = handbrake, circle = boost -- cycle hud = share -- r2 = accel, l2 = break local race_rebind = { -- [names.triangle] = names.triangle, -- camera [names.square] = names.circle, -- horn [names.x] = names.r1, -- handbrake [names.circle] = names.r3, -- boost -- [names.share] = pov down, -- cycle hud -- [names.l1] = names.l2, -- l2-l1 swap -- [names.r1] = names.r2, -- l2-l1 swap } local to_poll = { inp, ui } local ui_ff = ui:gen_ff_forwarder(inp) while true do local rdy = poll(to_poll) if rdy[inp] then local rd = inp:read() if rd.type == "SYN" then ui:write(rd) set_color("red", mode == "menu" and 0 or 255) set_color("green", mode == "race" and 0 or 255) set_color("blue", mode == "passthrough" and 255 or 0) elseif rd.type == "KEY" and rd.code == "BTN_MODE" then if inp.absinfo.RX.value < 250 then if rd.value == 1 then mode_pressed = rd.time elseif rd.time - mode_pressed > 1 then mode = "passthrough" else mode = mode == "menu" and "race" or "menu" end -- reset for _,v in pairs{"HAT0X", "HAT0Y", "Z", "RZ"} do ui:write{type="ABS", code=v, value=0} end for _,v in pairs{"X", "Y", "RX", "RY"} do ui:write{type="ABS", code=v, value=127} end l1 = false r1 = false brake = 0 accel = 0 end ui:write(rd) elseif mode == "passthrough" then ui:write(rd) elseif mode == "menu" then local nc = menu_rebind[rd.code] if nc then rd.code = nc end ui:write(rd) elseif mode == "race" then if rd.type == "KEY" then local code = rd.code if race_rebind[code] then rd.code = race_rebind[code] ui:write(rd) elseif code == names.l1 then ui:write{type="ABS", code="Z", value=rd.value*255} elseif code == names.r1 then ui:write{type="ABS", code="RZ", value=rd.value*255} elseif code == names.share then ui:write{type="ABS", code="HAT0Y", value=-rd.value} else ui:write(rd) end elseif rd.type == "ABS" then local code = rd.code if code == "X" then ui:write(rd) elseif code == "Z" then -- L2 brake = rd.value write_accel() elseif code == "RZ" then -- R2 accel = rd.value write_accel() elseif code == "RY" then -- look back ui:write{type="KEY", code=names.l1, value=rd.value > 160 and 1 or 0} end end end end if rdy[ui] then ui_ff() end end