10-example.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. -- SPDX-License-Identifier: MIT
  2. --
  3. -- This is an example libinput plugin
  4. --
  5. -- This plugin swaps left and right buttons on any device that has both buttons.
  6. -- Let's create a plugin. A single Lua script may create more than one
  7. -- plugin instance but that's a bit of a nice use-case. Most scripts
  8. -- should be a single plugin.
  9. -- A plugin needs to be registered to activate. If it isn't, it is
  10. -- cleaned up immediately. In the register call we supply
  11. -- the list of plugin versions we support. Currently we only
  12. -- have version 1.
  13. --
  14. -- UNCOMMENT THIS LINE TO ACTIVATE THE PLUGIN
  15. -- libinput:register({1})
  16. -- Note to the reader: it will be easier to understand this example
  17. -- if you read it bottom-up from here on.
  18. -- The callback for our "evdev-frame" signal.
  19. -- These frames are sent *before* libinput gets to handle them so
  20. -- any modifications will affect libinput.
  21. function frame(device, frame, time_in_microseconds)
  22. -- Frame is a table in the form
  23. -- { { usage: 123, value: 3 }, ... }
  24. -- Let's use the evdev module to make it more readable, evdev.KEY_A
  25. -- is simply the value (0x1 << 16) | 0x1 (see linux/input-event-codes.h)
  26. for _, v in ipairs(frame) do
  27. -- If we get a right button event, change it to left, and
  28. -- vice versa. Because this happens before libinput (or the next
  29. -- plugin in the precedence order) sees the
  30. -- frame it doesn't know that the swap happened.
  31. if v.usage == evdev.BTN_RIGHT then
  32. v.usage = evdev.BTN_LEFT
  33. elseif v.usage == evdev.BTN_LEFT then
  34. v.usage = evdev.BTN_RIGHT
  35. end
  36. end
  37. -- We changed the frame, let's return it. If we return nil
  38. -- the original frame is being used as-is.
  39. return frame
  40. end
  41. -- This is a timer callback. It is invoked after one second
  42. -- (see below) but only once - re-set the timer so
  43. -- it goes off every second.
  44. function timer_expired(time_in_microseconds)
  45. libinput:timer_set_absolute(time_in_microseconds + 1000000)
  46. end
  47. -- Callback for the "new-evdev-device" signal, see below
  48. -- The argument is the EvdevDevice object, see the documentation.
  49. function device_new(device)
  50. -- A table of evdev usages available on our device.
  51. -- Using the evdev module makes it more readable but you can
  52. -- use numbers (which is what evdev.EV_KEY and
  53. -- friends resolve to anyway).
  54. local usages = device:usages()
  55. if usages[evdev.BTN_LEFT] and usages[evdev.BTN_RIGHT] then
  56. -- The "evdev-frame" callback is invoked whenever the device
  57. -- provided us with one evdev frame, i.e. a bunch of events up
  58. -- to excluding EV_SYN SYN_REPORT.
  59. device:connect("evdev-frame", frame)
  60. end
  61. -- The device has udev information, let's print it out. Right
  62. -- now all we get are the ID_INPUT_ bits.
  63. -- If this is empty we know libinput will ignore this device anyway
  64. local udev_info = device:udev_properties()
  65. for k, v in pairs(udev_info) do
  66. libinput:log_debug(k .. "=" .. v)
  67. end
  68. end
  69. -- Let's connect to the "new-evdev-device" signal. This function
  70. -- is invoked when libinput detects a new evdev device (but before
  71. -- that device is actually available to libinput as libinput device).
  72. -- This allows us to e.g. change properties on the device.
  73. libinput:connect("new-evdev-device", device_new)
  74. -- Set our timer to expire 1s from now (in microseconds).
  75. -- Timers are absolute, so they need to be added to the
  76. -- current time
  77. libinput:connect("timer-expired", timer_expired)
  78. libinput:timer_set_relative(1000000)