diff --git a/tools/libinput-measure-fuzz.py b/tools/libinput-measure-fuzz.py index 6bc5796..f7ab5b8 100755 --- a/tools/libinput-measure-fuzz.py +++ b/tools/libinput-measure-fuzz.py @@ -29,8 +29,7 @@ import sys import argparse import subprocess try: - import evdev - import evdev.ecodes + import libevdev import pyudev except ModuleNotFoundError as e: print('Error: {}'.format(str(e)), file=sys.stderr) @@ -70,15 +69,15 @@ class InvalidDeviceError(Exception): pass -class Device(object): +class Device(libevdev.Device): def __init__(self, path): if path is None: self.path = self.find_touch_device() else: self.path = path - self.device = evdev.InputDevice(self.path) - self.name = self.device.name + fd = open(self.path, 'rb') + super().__init__(fd) context = pyudev.Context() self.udev_device = pyudev.Devices.from_device_file(context, self.path) @@ -133,37 +132,18 @@ class Device(object): Returns a tuple of (xfuzz, yfuzz) with the fuzz as set on the device axis. Returns None if no fuzz is set. ''' - # capabilities returns a dict with the EV_* codes as key, - # each of which is a list of tuples of (code, AbsInfo) - # - # Get the abs list first (or empty list if missing), - # then extract the touch major absinfo from that - caps = self.device.capabilities(absinfo=True).get( - evdev.ecodes.EV_ABS, [] - ) - codes = [cap[0] for cap in caps] - - if evdev.ecodes.ABS_X not in codes or evdev.ecodes.ABS_Y not in codes: + if not self.has(libevdev.EV_ABS.ABS_X) or not self.has(libevdev.EV_ABS.ABS_Y): raise InvalidDeviceError('device does not have x/y axes') - if (evdev.ecodes.ABS_MT_POSITION_X in codes) != (evdev.ecodes.ABS_MT_POSITION_Y in codes): + if self.has(libevdev.EV_ABS.ABS_MT_POSITION_X) != self.has(libevdev.EV_ABS.ABS_MT_POSITION_Y): raise InvalidDeviceError('device does not have both multitouch axes') - axes = { - 0x00: None, - 0x01: None, - 0x35: None, - 0x36: None, - } + xfuzz = self.absinfo[libevdev.EV_ABS.ABS_X].fuzz or \ + self.absinfo[libevdev.EV_ABS.ABS_MT_POSITION_X].fuzz + yfuzz = self.absinfo[libevdev.EV_ABS.ABS_Y].fuzz or \ + self.absinfo[libevdev.EV_ABS.ABS_MT_POSITION_Y].fuzz - for c in caps: - if c[0] in axes.keys(): - axes[c[0]] = c[1].fuzz - - xfuzz = axes[0x35] or axes[0x00] - yfuzz = axes[0x36] or axes[0x01] - - if xfuzz is None and yfuzz is None: + if xfuzz is 0 and yfuzz is 0: return None return (xfuzz, yfuzz) diff --git a/tools/libinput-measure-touch-size.py b/tools/libinput-measure-touch-size.py index f72ca01..3683f41 100755 --- a/tools/libinput-measure-touch-size.py +++ b/tools/libinput-measure-touch-size.py @@ -28,8 +28,7 @@ import sys import subprocess import argparse try: - import evdev - import evdev.ecodes + import libevdev import pyudev except ModuleNotFoundError as e: print('Error: {}'.format(str(e)), file=sys.stderr) @@ -178,32 +177,23 @@ class InvalidDeviceError(Exception): pass -class Device(object): +class Device(libevdev.Device): def __init__(self, path): if path is None: self.path = self.find_touch_device() else: 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 returns a dict with the EV_* codes as key, - # each of which is a list of tuples of (code, AbsInfo) - # - # Get the abs list first (or empty list if missing), - # then extract the touch major absinfo from that - caps = self.device.capabilities(absinfo=True).get( - evdev.ecodes.EV_ABS, [] - ) - codes = [cap[0] for cap in caps] - - if evdev.ecodes.ABS_MT_TOUCH_MAJOR not in codes: + if not self.has(libevdev.EV_ABS.ABS_MT_TOUCH_MAJOR): raise InvalidDeviceError("Device does not have ABS_MT_TOUCH_MAJOR") - self.has_minor = evdev.ecodes.ABS_MT_TOUCH_MINOR in codes - self.has_orientation = evdev.ecodes.ABS_MT_ORIENTATION in codes + self.has_minor = self.has(libevdev.EV_ABS.ABS_MT_TOUCH_MINOR) + self.has_orientation = self.has(libevdev.EV_ABS.ABS_MT_ORIENTATION) self.up = 0 self.down = 0 @@ -255,16 +245,16 @@ class Device(object): return self.sequences[-1] def handle_key(self, event): - tapcodes = [evdev.ecodes.BTN_TOOL_DOUBLETAP, - evdev.ecodes.BTN_TOOL_TRIPLETAP, - evdev.ecodes.BTN_TOOL_QUADTAP, - evdev.ecodes.BTN_TOOL_QUINTTAP] + tapcodes = [libevdev.EV_KEY.BTN_TOOL_DOUBLETAP, + libevdev.EV_KEY.BTN_TOOL_TRIPLETAP, + libevdev.EV_KEY.BTN_TOOL_QUADTAP, + libevdev.EV_KEY.BTN_TOOL_QUINTTAP] if event.code in tapcodes and event.value > 0: print("\rThis tool cannot handle multiple fingers, " "output will be invalid", file=sys.stderr) def handle_abs(self, event): - if event.code == evdev.ecodes.ABS_MT_TRACKING_ID: + if event.matches(libevdev.EV_ABS.ABS_MT_TRACKING_ID): if event.value > -1: self.start_new_sequence(event.value) else: @@ -275,11 +265,11 @@ class Device(object): except IndexError: # If the finger was down during start pass - elif event.code == evdev.ecodes.ABS_MT_TOUCH_MAJOR: + elif event.matches(libevdev.EV_ABS.ABS_MT_TOUCH_MAJOR): self.touch.major = event.value - elif event.code == evdev.ecodes.ABS_MT_TOUCH_MINOR: + elif event.matches(libevdev.EV_ABS.ABS_MT_TOUCH_MINOR): self.touch.minor = event.value - elif event.code == evdev.ecodes.ABS_MT_ORIENTATION: + elif event.matches(libevdev.EV_ABS.ABS_MT_ORIENTATION): self.touch.orientation = event.value def handle_syn(self, event): @@ -294,11 +284,11 @@ class Device(object): pass def handle_event(self, event): - if event.type == evdev.ecodes.EV_ABS: + if event.matches(libevdev.EV_ABS): self.handle_abs(event) - elif event.type == evdev.ecodes.EV_KEY: + elif event.matches(libevdev.EV_KEY): self.handle_key(event) - elif event.type == evdev.ecodes.EV_SYN: + elif event.matches(libevdev.EV_SYN): self.handle_syn(event) def read_events(self): @@ -309,8 +299,9 @@ class Device(object): print("Place a single finger on the device to measure touch size.\n" "Ctrl+C to exit\n") - for event in self.device.read_loop(): - self.handle_event(event) + while True: + for event in self.events(): + self.handle_event(event) def colon_tuple(string): diff --git a/tools/libinput-measure-touchpad-pressure.py b/tools/libinput-measure-touchpad-pressure.py index 46faf47..dfd9b53 100755 --- a/tools/libinput-measure-touchpad-pressure.py +++ b/tools/libinput-measure-touchpad-pressure.py @@ -28,8 +28,7 @@ import sys import subprocess import argparse try: - import evdev - import evdev.ecodes + import libevdev import pyudev except ModuleNotFoundError as e: print('Error: {}'.format(str(e)), file=sys.stderr) @@ -150,41 +149,33 @@ class InvalidDeviceError(Exception): pass -class Device(object): +class Device(libevdev.Device): def __init__(self, path): if path is None: self.path = self.find_touchpad_device() else: self.path = path - self.device = evdev.InputDevice(self.path) - - print("Using {}: {}\n".format(self.device.name, self.path)) - - # capabilities rturns a dict with the EV_* codes as key, - # each of which is a list of tuples of (code, AbsInfo) - # - # Get the abs list first (or empty list if missing), - # 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 or ABS_MT_PRESSURE") + fd = open(self.path, 'rb') + super().__init__(fd) + + print("Using {}: {}\n".format(self.name, self.path)) + + self.has_mt_pressure = True + absinfo = self.absinfo[libevdev.EV_ABS.ABS_MT_PRESSURE] + if absinfo is None: + absinfo = self.absinfo[libevdev.EV_ABS.ABS_PRESSURE] self.has_mt_pressure = False - else: - self.has_mt_pressure = True + if absinfo is None: + raise InvalidDeviceError("Device does not have ABS_PRESSURE or ABS_MT_PRESSURE") - p = p[0] - prange = p.max - p.min + prange = absinfo.maximum - absinfo.minimum # libinput defaults - self.down = int(p.min + 0.12 * prange) - self.up = int(p.min + 0.10 * prange) + self.down = int(absinfo.minimum + 0.12 * prange) + self.up = int(absinfo.minimum + 0.10 * prange) self.palm = 130 # the libinput default - self.thumb = p.max + self.thumb = absinfo.maximum self._init_thresholds_from_quirks() self.sequences = [] @@ -230,10 +221,10 @@ class Device(object): def handle_key(device, event): tapcodes = [ - evdev.ecodes.BTN_TOOL_DOUBLETAP, - evdev.ecodes.BTN_TOOL_TRIPLETAP, - evdev.ecodes.BTN_TOOL_QUADTAP, - evdev.ecodes.BTN_TOOL_QUINTTAP + libevdev.EV_KEY.BTN_TOOL_DOUBLETAP, + libevdev.EV_KEY.BTN_TOOL_TRIPLETAP, + libevdev.EV_KEY.BTN_TOOL_QUADTAP, + libevdev.EV_KEY.BTN_TOOL_QUINTTAP ] if event.code in tapcodes and event.value > 0: print("\rThis tool cannot handle multiple fingers, " @@ -241,7 +232,7 @@ def handle_key(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: device.start_new_sequence(event.value) else: @@ -252,8 +243,8 @@ def handle_abs(device, event): except IndexError: # If the finger was down at startup pass - elif ((event.code == evdev.ecodes.ABS_MT_PRESSURE) or - (event.code == evdev.ecodes.ABS_PRESSURE and not device.has_mt_pressure)): + elif (event.matches(libevdev.EV_ABS.ABS_MT_PRESSURE) or + (event.matches(libevdev.EV_ABS.ABS_PRESSURE) and not device.has_mt_pressure)): try: s = device.current_sequence() s.append(Touch(pressure=event.value)) @@ -264,9 +255,9 @@ def handle_abs(device, event): def handle_event(device, event): - if event.type == evdev.ecodes.EV_ABS: + if event.matches(libevdev.EV_ABS): handle_abs(device, event) - elif event.type == evdev.ecodes.EV_KEY: + elif event.matches(libevdev.EV_KEY): handle_key(device, event) @@ -278,8 +269,9 @@ def loop(device): print("Place a single finger on the touchpad to measure pressure values.\n" "Ctrl+C to exit\n") - for event in device.device.read_loop(): - handle_event(device, event) + while True: + for event in device.events(): + handle_event(device, event) def colon_tuple(string): diff --git a/tools/libinput-measure-touchpad-tap.py b/tools/libinput-measure-touchpad-tap.py index 81c4991..92ac139 100755 --- a/tools/libinput-measure-touchpad-tap.py +++ b/tools/libinput-measure-touchpad-tap.py @@ -27,8 +27,7 @@ import sys import argparse try: - import evdev - import evdev.ecodes + import libevdev import textwrap import pyudev except ModuleNotFoundError as e: @@ -83,27 +82,18 @@ class InvalidDeviceError(Exception): pass -class Device(object): +class Device(libevdev.Device): def __init__(self, path): if path is None: self.path = self._find_touch_device() else: self.path = path + fd = open(self.path, 'rb') + super().__init__(fd) - self.device = evdev.InputDevice(self.path) + print("Using {}: {}\n".format(self.name, self.path)) - print("Using {}: {}\n".format(self.device.name, self.path)) - - # capabilities returns a dict with the EV_* codes as key, - # each of which is a list of tuples of (code, AbsInfo) - # - # Get the abs list first (or empty list if missing), - # then extract the pressure absinfo from that - codes = self.device.capabilities(absinfo=True).get( - evdev.ecodes.EV_KEY, [] - ) - - if evdev.ecodes.BTN_TOUCH not in codes: + if not self.has(libevdev.EV_KEY.BTN_TOUCH): raise InvalidDeviceError("device does not have BTN_TOUCH") self.touches = [] @@ -141,16 +131,16 @@ class Device(object): end='') def handle_key(self, event): - tapcodes = [evdev.ecodes.BTN_TOOL_DOUBLETAP, - evdev.ecodes.BTN_TOOL_TRIPLETAP, - evdev.ecodes.BTN_TOOL_QUADTAP, - evdev.ecodes.BTN_TOOL_QUINTTAP] + tapcodes = [libevdev.EV_KEY.BTN_TOOL_DOUBLETAP, + libevdev.EV_KEY.BTN_TOOL_TRIPLETAP, + libevdev.EV_KEY.BTN_TOOL_QUADTAP, + libevdev.EV_KEY.BTN_TOOL_QUINTTAP] if event.code in tapcodes and event.value > 0: error("\rThis tool cannot handle multiple fingers, " "output will be invalid") return - if event.code == evdev.ecodes.BTN_TOUCH: + if event.matches(libevdev.EV_KEY.BTN_TOUCH): self.handle_btn_touch(event) def handle_syn(self, event): @@ -161,12 +151,13 @@ class Device(object): orientation=self.touch.orientation) def handle_event(self, event): - if event.type == evdev.ecodes.EV_KEY: + if event.matches(libevdev.EV_KEY): self.handle_key(event) def read_events(self): - for event in self.device.read_loop(): - self.handle_event(event) + while True: + for event in self.events(): + self.handle_event(event) def print_summary(self): deltas = sorted(t.tdelta for t in self.touches)