1
0

10-pointer-go-slower.lua 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. -- SPDX-License-Identifier: MIT
  2. --
  3. -- An example plugin to make the pointer go three times as slow
  4. --
  5. -- Install this file in /etc/libinput/plugins and
  6. --
  7. -- UNCOMMENT THIS LINE TO ACTIVATE THE PLUGIN
  8. -- libinput:register({1})
  9. remainders = {}
  10. function split(v)
  11. if math.abs(v) >= 1.0 then
  12. local i = math.floor(math.abs(v))
  13. local r = math.abs(v) % 1.0
  14. if v < 0.0 then
  15. i = -i
  16. r = -r
  17. end
  18. return i, r
  19. else
  20. return 0, v
  21. end
  22. end
  23. function decelerate(device, x, y)
  24. local remainder = remainders[device]
  25. local rx, ry = 0, 0
  26. if x ~= 0.0 then
  27. rx, remainder.x = split(remainder.x + x/3.0)
  28. end
  29. if y ~= 0.0 then
  30. ry, remainder.y = split(remainder.y + y/3.0)
  31. end
  32. return rx, ry
  33. end
  34. libinput:connect("new-evdev-device", function(device)
  35. local usages = device:usages()
  36. if usages[evdev.REL_X] then
  37. remainders[device] = { x = 0.0, y = 0.0 }
  38. device:connect("evdev-frame", function(device, frame, timestamp)
  39. for _, v in ipairs(frame) do
  40. if v.usage == evdev.REL_X then
  41. v.value, _ = decelerate(device, v.value, 0.0)
  42. elseif v.usage == evdev.REL_Y then
  43. _, v.value = decelerate(device, 0.0, v.value)
  44. end
  45. end
  46. return frame
  47. end)
  48. device:connect("device-removed", function(dev)
  49. remainders[dev] = nil
  50. end)
  51. end
  52. end)