tools: switch measure-touchpad-pressure to python-libevdev

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2019-03-15 11:56:48 +10:00
parent deb759a069
commit 33afe9f875

View file

@ -28,8 +28,7 @@ import sys
import subprocess import subprocess
import argparse import argparse
try: try:
import evdev import libevdev
import evdev.ecodes
import pyudev import pyudev
except ModuleNotFoundError as e: except ModuleNotFoundError as e:
print('Error: {}'.format(str(e)), file=sys.stderr) print('Error: {}'.format(str(e)), file=sys.stderr)
@ -150,41 +149,33 @@ class InvalidDeviceError(Exception):
pass pass
class Device(object): class Device(libevdev.Device):
def __init__(self, path): def __init__(self, path):
if path is None: if path is None:
self.path = self.find_touchpad_device() self.path = self.find_touchpad_device()
else: else:
self.path = path self.path = path
self.device = evdev.InputDevice(self.path) fd = open(self.path, 'rb')
super().__init__(fd)
print("Using {}: {}\n".format(self.device.name, self.path)) print("Using {}: {}\n".format(self.name, self.path))
# capabilities rturns a dict with the EV_* codes as key, self.has_mt_pressure = True
# each of which is a list of tuples of (code, AbsInfo) absinfo = self.absinfo[libevdev.EV_ABS.ABS_MT_PRESSURE]
# if absinfo is None:
# Get the abs list first (or empty list if missing), absinfo = self.absinfo[libevdev.EV_ABS.ABS_PRESSURE]
# then extract the pressure absinfo from that
all_caps = self.device.capabilities(absinfo=True)
caps = all_caps.get(evdev.ecodes.EV_ABS, [])
p = [cap[1] for cap in caps if cap[0] == evdev.ecodes.ABS_MT_PRESSURE]
if not p:
p = [cap[1] for cap in caps if cap[0] == evdev.ecodes.ABS_PRESSURE]
if not p:
raise InvalidDeviceError("device does not have ABS_PRESSURE/ABS_MT_PRESSURE")
self.has_mt_pressure = False self.has_mt_pressure = False
else: if absinfo is None:
self.has_mt_pressure = True raise InvalidDeviceError("device does not have ABS_PRESSURE/ABS_MT_PRESSURE")
p = p[0] prange = absinfo.maximum - absinfo.minimum
prange = p.max - p.min
# libinput defaults # libinput defaults
self.down = int(p.min + 0.12 * prange) self.down = int(absinfo.minimum + 0.12 * prange)
self.up = int(p.min + 0.10 * prange) self.up = int(absinfo.minimum + 0.10 * prange)
self.palm = 130 # the libinput default self.palm = 130 # the libinput default
self.thumb = p.max self.thumb = absinfo.maximum
self._init_thresholds_from_quirks() self._init_thresholds_from_quirks()
self.sequences = [] self.sequences = []
@ -230,10 +221,10 @@ class Device(object):
def handle_key(device, event): def handle_key(device, event):
tapcodes = [ tapcodes = [
evdev.ecodes.BTN_TOOL_DOUBLETAP, libevdev.EV_KEY.BTN_TOOL_DOUBLETAP,
evdev.ecodes.BTN_TOOL_TRIPLETAP, libevdev.EV_KEY.BTN_TOOL_TRIPLETAP,
evdev.ecodes.BTN_TOOL_QUADTAP, libevdev.EV_KEY.BTN_TOOL_QUADTAP,
evdev.ecodes.BTN_TOOL_QUINTTAP libevdev.EV_KEY.BTN_TOOL_QUINTTAP
] ]
if event.code in tapcodes and event.value > 0: if event.code in tapcodes and event.value > 0:
print("\rThis tool cannot handle multiple fingers, " print("\rThis tool cannot handle multiple fingers, "
@ -241,7 +232,7 @@ def handle_key(device, event):
def handle_abs(device, event): def handle_abs(device, event):
if event.code == evdev.ecodes.ABS_MT_TRACKING_ID: if event.matches(libevdev.EV_ABS.ABS_MT_TRACKING_ID):
if event.value > -1: if event.value > -1:
device.start_new_sequence(event.value) device.start_new_sequence(event.value)
else: else:
@ -252,8 +243,8 @@ def handle_abs(device, event):
except IndexError: except IndexError:
# If the finger was down at startup # If the finger was down at startup
pass pass
elif ((event.code == evdev.ecodes.ABS_MT_PRESSURE) or elif (event.matches(libevdev.EV_ABS.ABS_MT_PRESSURE) or
(event.code == evdev.ecodes.ABS_PRESSURE and not device.has_mt_pressure)): (event.matches(libevdev.EV_ABS.ABS_PRESSURE) and not device.has_mt_pressure)):
try: try:
s = device.current_sequence() s = device.current_sequence()
s.append(Touch(pressure=event.value)) s.append(Touch(pressure=event.value))
@ -264,9 +255,9 @@ def handle_abs(device, event):
def handle_event(device, event): def handle_event(device, event):
if event.type == evdev.ecodes.EV_ABS: if event.matches(libevdev.EV_ABS):
handle_abs(device, event) handle_abs(device, event)
elif event.type == evdev.ecodes.EV_KEY: elif event.matches(libevdev.EV_KEY):
handle_key(device, event) handle_key(device, event)
@ -278,8 +269,9 @@ def loop(device):
print("Place a single finger on the touchpad to measure pressure values.\n" print("Place a single finger on the touchpad to measure pressure values.\n"
"Ctrl+C to exit\n") "Ctrl+C to exit\n")
for event in device.device.read_loop(): while True:
handle_event(device, event) for event in device.events():
handle_event(device, event)
def colon_tuple(string): def colon_tuple(string):