2017-11-23 15:31:43 +10:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# vim: set expandtab shiftwidth=4:
|
|
|
|
|
# -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
|
|
|
|
|
#
|
|
|
|
|
# Copyright © 2018 Red Hat, Inc.
|
|
|
|
|
#
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
# copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
# to deal in the Software without restriction, including without limitation
|
|
|
|
|
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
# and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
# Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
#
|
|
|
|
|
# The above copyright notice and this permission notice (including the next
|
|
|
|
|
# paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
# Software.
|
|
|
|
|
#
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
# DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
import time
|
|
|
|
|
import multiprocessing
|
|
|
|
|
import argparse
|
2019-09-04 13:51:07 +10:00
|
|
|
from pathlib import Path
|
2017-11-23 15:31:43 +10:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
import libevdev
|
|
|
|
|
import yaml
|
|
|
|
|
except ModuleNotFoundError as e:
|
|
|
|
|
print('Error: {}'.format(e), file=sys.stderr)
|
|
|
|
|
print('One or more python modules are missing. Please install those '
|
|
|
|
|
'modules and re-run this tool.')
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SUPPORTED_FILE_VERSION = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def error(msg, **kwargs):
|
|
|
|
|
print(msg, **kwargs, file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class YamlException(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def fetch(yaml, key):
|
|
|
|
|
'''Helper function to avoid confusing a YAML error with a
|
|
|
|
|
normal KeyError bug'''
|
|
|
|
|
try:
|
|
|
|
|
return yaml[key]
|
|
|
|
|
except KeyError:
|
|
|
|
|
raise YamlException('Failed to get \'{}\' from recording.'.format(key))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create(device):
|
|
|
|
|
evdev = fetch(device, 'evdev')
|
|
|
|
|
|
|
|
|
|
d = libevdev.Device()
|
|
|
|
|
d.name = fetch(evdev, 'name')
|
|
|
|
|
|
|
|
|
|
ids = fetch(evdev, 'id')
|
|
|
|
|
if len(ids) != 4:
|
|
|
|
|
raise YamlException('Invalid ID format: {}'.format(ids))
|
|
|
|
|
d.id = dict(zip(['bustype', 'vendor', 'product', 'version'], ids))
|
|
|
|
|
|
|
|
|
|
codes = fetch(evdev, 'codes')
|
|
|
|
|
for evtype, evcodes in codes.items():
|
|
|
|
|
for code in evcodes:
|
|
|
|
|
data = None
|
|
|
|
|
if evtype == libevdev.EV_ABS.value:
|
|
|
|
|
values = fetch(evdev, 'absinfo')[code]
|
|
|
|
|
absinfo = libevdev.InputAbsInfo(minimum=values[0],
|
|
|
|
|
maximum=values[1],
|
|
|
|
|
fuzz=values[2],
|
|
|
|
|
flat=values[3],
|
|
|
|
|
resolution=values[4])
|
|
|
|
|
data = absinfo
|
|
|
|
|
elif evtype == libevdev.EV_REP.value:
|
|
|
|
|
if code == libevdev.EV_REP.REP_DELAY.value:
|
|
|
|
|
data = 500
|
|
|
|
|
elif code == libevdev.EV_REP.REP_PERIOD.value:
|
|
|
|
|
data = 20
|
|
|
|
|
d.enable(libevdev.evbit(evtype, code), data=data)
|
|
|
|
|
|
2018-06-21 14:07:02 +10:00
|
|
|
properties = fetch(evdev, 'properties')
|
|
|
|
|
for prop in properties:
|
|
|
|
|
d.enable(libevdev.propbit(prop))
|
|
|
|
|
|
2017-11-23 15:31:43 +10:00
|
|
|
uinput = d.create_uinput_device()
|
|
|
|
|
return uinput
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def print_events(devnode, indent, evs):
|
|
|
|
|
devnode = os.path.basename(devnode)
|
|
|
|
|
for e in evs:
|
|
|
|
|
print("{}: {}{:06d}.{:06d} {} / {:<20s} {:4d}".format(
|
|
|
|
|
devnode, ' ' * (indent * 8), e.sec, e.usec, e.type.name, e.code.name, e.value))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def replay(device, verbose):
|
|
|
|
|
events = fetch(device, 'events')
|
|
|
|
|
if events is None:
|
|
|
|
|
return
|
|
|
|
|
uinput = device['__uinput']
|
|
|
|
|
|
2019-11-25 14:47:42 +10:00
|
|
|
# The first event may have a nonzero offset but we want to replay
|
|
|
|
|
# immediately regardless. When replaying multiple devices, the first
|
|
|
|
|
# offset is the offset from the first event on any device.
|
|
|
|
|
offset = time.time() - device['__first_event_offset']
|
2017-11-23 15:31:43 +10:00
|
|
|
|
2020-01-30 09:47:51 +10:00
|
|
|
if offset < 0:
|
|
|
|
|
error('WARNING: event time offset is in the future, refusing to replay')
|
|
|
|
|
return
|
|
|
|
|
|
2017-11-23 15:31:43 +10:00
|
|
|
# each 'evdev' set contains one SYN_REPORT so we only need to check for
|
|
|
|
|
# the time offset once per event
|
|
|
|
|
for event in events:
|
tools: libinput-record: add support for printing libinput events
Collect libinput events together with the evdev events and print them to the
log. This makes it possible to debug the full behavior of a user's machine
rather than having to replay it with potential different race conditions/side
effects.
Example event output:
- evdev:
- [ 2, 314443, 4, 4, 57] # EV_MSC / MSC_SCAN 57
- [ 2, 314443, 1, 57, 1] # EV_KEY / KEY_SPACE 1
- [ 2, 314443, 0, 0, 0] # ------------ SYN_REPORT (0) ---------- +87ms
libinput:
- {time: 2.314443, type: KEYBOARD_KEY, key: 57, state: pressed}
- evdev:
- [ 2, 377203, 4, 4, 57] # EV_MSC / MSC_SCAN 57
- [ 2, 377203, 1, 57, 0] # EV_KEY / KEY_SPACE 0
- [ 2, 377203, 0, 0, 0] # ------------ SYN_REPORT (0) ---------- +63ms
libinput:
- {time: 2.377203, type: KEYBOARD_KEY, key: 57, state: released}
Note that the only way to know that events are within the same frame is to
check the timestamp. libinput keeps those intact which means we can tell that
if we just had an evdev frame with timestamp T and get a pointer motion with
timestamp T, that frame caused the motion event.
So far, only key, pointer and touch events are printed. We also
hardcode-enable tapping where available until we have options to enable this
on the commandline just because that's useful to have.
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
2018-03-01 10:04:11 +10:00
|
|
|
try:
|
|
|
|
|
evdev = fetch(event, 'evdev')
|
|
|
|
|
except YamlException:
|
|
|
|
|
continue
|
|
|
|
|
|
2017-11-23 15:31:43 +10:00
|
|
|
(sec, usec, evtype, evcode, value) = evdev[0]
|
2019-09-11 10:00:33 +10:00
|
|
|
evtime = sec + usec / 1e6 + offset
|
2017-11-23 15:31:43 +10:00
|
|
|
now = time.time()
|
|
|
|
|
|
2019-09-11 10:00:33 +10:00
|
|
|
if evtime - now > 150 / 1e6: # 150 µs error margin
|
|
|
|
|
time.sleep(evtime - now - 150 / 1e6)
|
2017-11-23 15:31:43 +10:00
|
|
|
|
|
|
|
|
evs = [libevdev.InputEvent(libevdev.evbit(e[2], e[3]), value=e[4], sec=e[0], usec=e[1]) for e in evdev]
|
|
|
|
|
uinput.send_events(evs)
|
2018-03-12 13:41:22 +10:00
|
|
|
if verbose:
|
|
|
|
|
print_events(uinput.devnode, device['__index'], evs)
|
2017-11-23 15:31:43 +10:00
|
|
|
|
|
|
|
|
|
2019-11-25 14:47:42 +10:00
|
|
|
def first_timestamp(device):
|
|
|
|
|
try:
|
|
|
|
|
events = fetch(device, 'events')
|
|
|
|
|
if events is None:
|
|
|
|
|
raise YamlException('No events from this device')
|
|
|
|
|
|
|
|
|
|
evdev = fetch(events[0], 'evdev')
|
|
|
|
|
(sec, usec, *_) = evdev[0]
|
|
|
|
|
|
|
|
|
|
return sec + usec / 1.e6
|
|
|
|
|
|
|
|
|
|
except YamlException:
|
|
|
|
|
import math
|
|
|
|
|
return math.inf
|
|
|
|
|
|
|
|
|
|
|
2017-11-23 15:31:43 +10:00
|
|
|
def wrap(func, *args):
|
|
|
|
|
try:
|
|
|
|
|
func(*args)
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def loop(args, recording):
|
|
|
|
|
devices = fetch(recording, 'devices')
|
|
|
|
|
|
2019-11-25 14:47:42 +10:00
|
|
|
# All devices need to start replaying at the same time, so let's find
|
|
|
|
|
# the very first event and offset everything by that timestamp.
|
|
|
|
|
toffset = min([first_timestamp(d) for d in devices])
|
|
|
|
|
|
2017-11-23 15:31:43 +10:00
|
|
|
for idx, d in enumerate(devices):
|
|
|
|
|
uinput = create(d)
|
|
|
|
|
print('{}: {}'.format(uinput.devnode, uinput.name))
|
|
|
|
|
d['__uinput'] = uinput # cheaper to hide it in the dict then work around it
|
|
|
|
|
d['__index'] = idx
|
2019-11-25 14:47:42 +10:00
|
|
|
d['__first_event_offset'] = toffset
|
2017-11-23 15:31:43 +10:00
|
|
|
|
2019-09-04 13:50:37 +10:00
|
|
|
while True:
|
2017-11-23 15:31:43 +10:00
|
|
|
input('Hit enter to start replaying')
|
|
|
|
|
|
|
|
|
|
processes = []
|
|
|
|
|
for d in devices:
|
|
|
|
|
p = multiprocessing.Process(target=wrap, args=(replay, d, args.verbose))
|
|
|
|
|
processes.append(p)
|
|
|
|
|
|
|
|
|
|
for p in processes:
|
|
|
|
|
p.start()
|
|
|
|
|
|
|
|
|
|
for p in processes:
|
|
|
|
|
p.join()
|
|
|
|
|
|
|
|
|
|
del processes
|
|
|
|
|
|
|
|
|
|
|
2019-09-04 13:51:07 +10:00
|
|
|
def create_device_quirk(device):
|
2020-05-22 13:15:24 +10:00
|
|
|
try:
|
|
|
|
|
quirks = fetch(device, 'quirks')
|
|
|
|
|
if not quirks:
|
|
|
|
|
return None
|
|
|
|
|
except YamlException:
|
2019-09-04 13:51:07 +10:00
|
|
|
return None
|
|
|
|
|
# Where the device has a quirk, we match on name, vendor and product.
|
|
|
|
|
# That's the best match we can assemble here from the info we have.
|
|
|
|
|
evdev = fetch(device, 'evdev')
|
|
|
|
|
name = fetch(evdev, 'name')
|
|
|
|
|
id = fetch(evdev, 'id')
|
|
|
|
|
quirk = ('[libinput-replay {name}]\n'
|
|
|
|
|
'MatchName={name}\n'
|
|
|
|
|
'MatchVendor=0x{id[1]:04X}\n'
|
|
|
|
|
'MatchProduct=0x{id[2]:04X}\n').format(name=name, id=id)
|
|
|
|
|
quirk += '\n'.join(quirks)
|
|
|
|
|
return quirk
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setup_quirks(recording):
|
|
|
|
|
devices = fetch(recording, 'devices')
|
|
|
|
|
overrides = None
|
2020-08-24 08:31:21 +10:00
|
|
|
quirks = []
|
|
|
|
|
for d in devices:
|
|
|
|
|
if 'quirks' in d:
|
|
|
|
|
quirk = create_device_quirk(d)
|
|
|
|
|
if quirk:
|
|
|
|
|
quirks.append(quirk)
|
2019-09-04 13:51:07 +10:00
|
|
|
if not quirks:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
overrides = Path('/etc/libinput/local-overrides.quirks')
|
|
|
|
|
if overrides.exists():
|
|
|
|
|
print('{} exists, please move it out of the way first'.format(overrides), file=sys.stderr)
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
2020-02-24 09:58:24 +10:00
|
|
|
overrides.parent.mkdir(exist_ok=True)
|
2019-09-04 13:51:07 +10:00
|
|
|
with overrides.open('w+') as fd:
|
|
|
|
|
fd.write('# This file was generated by libinput replay\n')
|
|
|
|
|
fd.write('# Unless libinput replay is running right now, remove this file.\n')
|
|
|
|
|
fd.write('\n\n'.join(quirks))
|
|
|
|
|
|
|
|
|
|
return overrides
|
|
|
|
|
|
|
|
|
|
|
2019-09-04 13:33:25 +10:00
|
|
|
def check_file(recording):
|
|
|
|
|
version = fetch(recording, 'version')
|
|
|
|
|
if version != SUPPORTED_FILE_VERSION:
|
|
|
|
|
raise YamlException('Invalid file format: {}, expected {}'.format(version, SUPPORTED_FILE_VERSION))
|
|
|
|
|
|
|
|
|
|
ndevices = fetch(recording, 'ndevices')
|
|
|
|
|
devices = fetch(recording, 'devices')
|
|
|
|
|
if ndevices != len(devices):
|
|
|
|
|
error('WARNING: truncated file, expected {} devices, got {}'.format(ndevices, len(devices)))
|
|
|
|
|
|
|
|
|
|
|
2017-11-23 15:31:43 +10:00
|
|
|
def main():
|
2019-09-11 10:00:33 +10:00
|
|
|
parser = argparse.ArgumentParser(description='Replay a device recording')
|
2017-11-23 15:31:43 +10:00
|
|
|
parser.add_argument('recording', metavar='recorded-file.yaml',
|
|
|
|
|
type=str, help='Path to device recording')
|
|
|
|
|
parser.add_argument('--verbose', action='store_true')
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
2019-09-04 13:51:07 +10:00
|
|
|
quirks_file = None
|
|
|
|
|
|
2017-11-23 15:31:43 +10:00
|
|
|
try:
|
|
|
|
|
with open(args.recording) as f:
|
|
|
|
|
y = yaml.safe_load(f)
|
2019-09-04 13:51:07 +10:00
|
|
|
check_file(y)
|
|
|
|
|
quirks_file = setup_quirks(y)
|
2017-11-23 15:31:43 +10:00
|
|
|
loop(args, y)
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
pass
|
|
|
|
|
except (PermissionError, OSError) as e:
|
|
|
|
|
error('Error: failed to open device: {}'.format(e))
|
|
|
|
|
except YamlException as e:
|
|
|
|
|
error('Error: failed to parse recording: {}'.format(e))
|
2019-09-04 13:51:07 +10:00
|
|
|
finally:
|
|
|
|
|
if quirks_file:
|
|
|
|
|
quirks_file.unlink()
|
2017-11-23 15:31:43 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|