File: 10-delay-motion.lua

package info (click to toggle)
libinput 1.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,404 kB
  • sloc: ansic: 104,881; python: 3,570; sh: 183; makefile: 37; cpp: 7
file content (64 lines) | stat: -rw-r--r-- 1,995 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
-- SPDX-License-Identifier: MIT
--
-- This is an example libinput plugin
--
-- This plugin delays any event with relative motion by the given DELAY
-- by storing it in a table and replaying it via a timer callback later.

-- UNCOMMENT THIS LINE TO ACTIVATE THE PLUGIN
-- libinput:register({1})

DELAY = 1500 * 1000 -- 1.5s
next_timer_expiry = 0
devices = {}

function timer_expired(time_in_microseconds)
    next_timer_expiry = 0
    for device, frames in pairs(devices) do
        while #frames > 0 and frames[1].time <= time_in_microseconds do
            --- we don't have a current frame so it doesn't matter
            --- whether we prepend or append
            device:prepend_frame(frames[1].frame)
            table.remove(frames, 1)
        end
        local next_frame = frames[1]
        if next_frame and (next_timer_expiry == 0 or next_frame.time < next_timer_expiry) then
            next_timer_expiry = next_frame.time
        end
    end
    if next_timer_expiry ~= 0 then
        libinput:timer_set_absolute(next_timer_expiry)
    end
end

function frame(device, frame, timestamp)
    for _, v in ipairs(frame) do
        if v.usage == evdev.REL_X or v.usage == evdev.REL_Y then
            local next_time = timestamp + DELAY
            table.insert(devices[device], {
                time = next_time,
                frame = frame
            })
            if next_timer_expiry == 0 then
                next_timer_expiry = next_time
                libinput:timer_set_absolute(next_timer_expiry)
            end
            return {} -- discard frame
        end
    end
    return nil
end

function device_new(device)
    local usages = device:usages()
    if usages[evdev.REL_X] then
        devices[device] = {}
        device:connect("evdev-frame", frame)
        device:connect("device-removed", function(dev)
            devices[dev] = nil
        end)
    end
end

libinput:connect("new-evdev-device", device_new)
libinput:connect("timer-expired", timer_expired)