1#!/usr/bin/env python3 2# vim: set expandtab shiftwidth=4: 3# -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */ 4# 5# Copyright © 2017 Red Hat, Inc. 6# 7# Permission is hereby granted, free of charge, to any person obtaining a 8# copy of this software and associated documentation files (the "Software"), 9# to deal in the Software without restriction, including without limitation 10# the rights to use, copy, modify, merge, publish, distribute, sublicense, 11# and/or sell copies of the Software, and to permit persons to whom the 12# Software is furnished to do so, subject to the following conditions: 13# 14# The above copyright notice and this permission notice (including the next 15# paragraph) shall be included in all copies or substantial portions of the 16# Software. 17# 18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24# DEALINGS IN THE SOFTWARE. 25# 26 27import sys 28import subprocess 29import argparse 30try: 31 import libevdev 32 import pyudev 33except ModuleNotFoundError as e: 34 print('Error: {}'.format(str(e)), file=sys.stderr) 35 print('One or more python modules are missing. Please install those ' 36 'modules and re-run this tool.') 37 sys.exit(1) 38 39 40class Range(object): 41 """Class to keep a min/max of a value around""" 42 def __init__(self): 43 self.min = float('inf') 44 self.max = float('-inf') 45 46 def update(self, value): 47 self.min = min(self.min, value) 48 self.max = max(self.max, value) 49 50 51class Touch(object): 52 """A single data point of a sequence (i.e. one event frame)""" 53 54 def __init__(self, major=None, minor=None, orientation=None): 55 self._major = major 56 self._minor = minor 57 self._orientation = orientation 58 self.dirty = False 59 60 @property 61 def major(self): 62 return self._major 63 64 @major.setter 65 def major(self, major): 66 self._major = major 67 self.dirty = True 68 69 @property 70 def minor(self): 71 return self._minor 72 73 @minor.setter 74 def minor(self, minor): 75 self._minor = minor 76 self.dirty = True 77 78 @property 79 def orientation(self): 80 return self._orientation 81 82 @orientation.setter 83 def orientation(self, orientation): 84 self._orientation = orientation 85 self.dirty = True 86 87 def __str__(self): 88 s = "Touch: major {:3d}".format(self.major) 89 if self.minor is not None: 90 s += ", minor {:3d}".format(self.minor) 91 if self.orientation is not None: 92 s += ", orientation {:+3d}".format(self.orientation) 93 return s 94 95 96class TouchSequence(object): 97 """A touch sequence from beginning to end""" 98 99 def __init__(self, device, tracking_id): 100 self.device = device 101 self.tracking_id = tracking_id 102 self.points = [] 103 104 self.is_active = True 105 106 self.is_down = False 107 self.was_down = False 108 self.is_palm = False 109 self.was_palm = False 110 self.is_thumb = False 111 self.was_thumb = False 112 113 self.major_range = Range() 114 self.minor_range = Range() 115 116 def append(self, touch): 117 """Add a Touch to the sequence""" 118 self.points.append(touch) 119 self.major_range.update(touch.major) 120 self.minor_range.update(touch.minor) 121 122 if touch.major < self.device.up or touch.minor < self.device.up: 123 self.is_down = False 124 elif touch.major > self.device.down or touch.minor > self.device.down: 125 self.is_down = True 126 self.was_down = True 127 128 self.is_palm = touch.major > self.device.palm 129 if self.is_palm: 130 self.was_palm = True 131 132 self.is_thumb = self.device.thumb != 0 and touch.major > self.device.thumb 133 if self.is_thumb: 134 self.was_thumb = True 135 136 def finalize(self): 137 """Mark the TouchSequence as complete (finger is up)""" 138 self.is_active = False 139 140 def __str__(self): 141 return self._str_state() if self.is_active else self._str_summary() 142 143 def _str_summary(self): 144 if not self.points: 145 return "{:78s}".format("Sequence: no major/minor values recorded") 146 147 s = "Sequence: major: [{:3d}..{:3d}] ".format( 148 self.major_range.min, self.major_range.max 149 ) 150 if self.device.has_minor: 151 s += "minor: [{:3d}..{:3d}] ".format(self.minor_range.min, self.minor_range.max) 152 if self.was_down: 153 s += " down" 154 if self.was_palm: 155 s += " palm" 156 if self.was_thumb: 157 s += " thumb" 158 159 return s 160 161 def _str_state(self): 162 touch = self.points[-1] 163 s = "{}, tags: {} {} {}".format(touch, 164 "down" if self.is_down else " ", 165 "palm" if self.is_palm else " ", 166 "thumb" if self.is_thumb else " ") 167 return s 168 169 170class InvalidDeviceError(Exception): 171 pass 172 173 174class Device(libevdev.Device): 175 def __init__(self, path): 176 if path is None: 177 self.path = self.find_touch_device() 178 else: 179 self.path = path 180 181 fd = open(self.path, 'rb') 182 super().__init__(fd) 183 184 print("Using {}: {}\n".format(self.name, self.path)) 185 186 if not self.has(libevdev.EV_ABS.ABS_MT_TOUCH_MAJOR): 187 raise InvalidDeviceError("Device does not have ABS_MT_TOUCH_MAJOR") 188 189 self.has_minor = self.has(libevdev.EV_ABS.ABS_MT_TOUCH_MINOR) 190 self.has_orientation = self.has(libevdev.EV_ABS.ABS_MT_ORIENTATION) 191 192 self.up = 0 193 self.down = 0 194 self.palm = 0 195 self.thumb = 0 196 197 self._init_thresholds_from_quirks() 198 self.sequences = [] 199 self.touch = Touch(0, 0) 200 201 def find_touch_device(self): 202 context = pyudev.Context() 203 for device in context.list_devices(subsystem='input'): 204 if not device.get('ID_INPUT_TOUCHPAD', 0) and \ 205 not device.get('ID_INPUT_TOUCHSCREEN', 0): 206 continue 207 208 if not device.device_node or \ 209 not device.device_node.startswith('/dev/input/event'): 210 continue 211 212 return device.device_node 213 214 print("Unable to find a touch device.", file=sys.stderr) 215 sys.exit(1) 216 217 def _init_thresholds_from_quirks(self): 218 command = ['libinput', 'quirks', 'list', self.path] 219 cmd = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 220 if cmd.returncode != 0: 221 print("Error querying quirks: {}".format(cmd.stderr.decode('utf-8')), file=sys.stderr) 222 return 223 224 stdout = cmd.stdout.decode('utf-8') 225 quirks = [q.split('=') for q in stdout.split('\n')] 226 227 for q in quirks: 228 if q[0] == 'AttrPalmSizeThreshold': 229 self.palm = int(q[1]) 230 elif q[0] == 'AttrTouchSizeRange': 231 self.down, self.up = colon_tuple(q[1]) 232 elif q[0] == 'AttrThumbSizeThreshold': 233 self.thumb = int(q[1]) 234 235 def start_new_sequence(self, tracking_id): 236 self.sequences.append(TouchSequence(self, tracking_id)) 237 238 def current_sequence(self): 239 return self.sequences[-1] 240 241 def handle_key(self, event): 242 tapcodes = [libevdev.EV_KEY.BTN_TOOL_DOUBLETAP, 243 libevdev.EV_KEY.BTN_TOOL_TRIPLETAP, 244 libevdev.EV_KEY.BTN_TOOL_QUADTAP, 245 libevdev.EV_KEY.BTN_TOOL_QUINTTAP] 246 if event.code in tapcodes and event.value > 0: 247 print("\rThis tool cannot handle multiple fingers, " 248 "output will be invalid", file=sys.stderr) 249 250 def handle_abs(self, event): 251 if event.matches(libevdev.EV_ABS.ABS_MT_TRACKING_ID): 252 if event.value > -1: 253 self.start_new_sequence(event.value) 254 else: 255 try: 256 s = self.current_sequence() 257 s.finalize() 258 print("\r{}".format(s)) 259 except IndexError: 260 # If the finger was down during start 261 pass 262 elif event.matches(libevdev.EV_ABS.ABS_MT_TOUCH_MAJOR): 263 self.touch.major = event.value 264 elif event.matches(libevdev.EV_ABS.ABS_MT_TOUCH_MINOR): 265 self.touch.minor = event.value 266 elif event.matches(libevdev.EV_ABS.ABS_MT_ORIENTATION): 267 self.touch.orientation = event.value 268 269 def handle_syn(self, event): 270 if self.touch.dirty: 271 try: 272 self.current_sequence().append(self.touch) 273 print("\r{}".format(self.current_sequence()), end="") 274 self.touch = Touch(major=self.touch.major, 275 minor=self.touch.minor, 276 orientation=self.touch.orientation) 277 except IndexError: 278 pass 279 280 def handle_event(self, event): 281 if event.matches(libevdev.EV_ABS): 282 self.handle_abs(event) 283 elif event.matches(libevdev.EV_KEY): 284 self.handle_key(event) 285 elif event.matches(libevdev.EV_SYN): 286 self.handle_syn(event) 287 288 def read_events(self): 289 print("Ready for recording data.") 290 print("Touch sizes used: {}:{}".format(self.down, self.up)) 291 print("Palm size used: {}".format(self.palm)) 292 print("Thumb size used: {}".format(self.thumb)) 293 print("Place a single finger on the device to measure touch size.\n" 294 "Ctrl+C to exit\n") 295 296 while True: 297 for event in self.events(): 298 self.handle_event(event) 299 300 301def colon_tuple(string): 302 try: 303 ts = string.split(':') 304 t = tuple([int(x) for x in ts]) 305 if len(t) == 2 and t[0] >= t[1]: 306 return t 307 except: # noqa 308 pass 309 310 msg = "{} is not in format N:M (N >= M)".format(string) 311 raise argparse.ArgumentTypeError(msg) 312 313 314def main(args): 315 parser = argparse.ArgumentParser(description="Measure touch size and orientation") 316 parser.add_argument('path', metavar='/dev/input/event0', 317 nargs='?', type=str, help='Path to device (optional)') 318 parser.add_argument('--touch-thresholds', metavar='down:up', 319 type=colon_tuple, 320 help='Thresholds when a touch is logically down or up') 321 parser.add_argument('--palm-threshold', metavar='t', 322 type=int, help='Threshold when a touch is a palm') 323 args = parser.parse_args() 324 325 try: 326 device = Device(args.path) 327 328 if args.touch_thresholds is not None: 329 device.down, device.up = args.touch_thresholds 330 331 if args.palm_threshold is not None: 332 device.palm = args.palm_threshold 333 334 device.read_events() 335 except KeyboardInterrupt: 336 pass 337 except (PermissionError, OSError): 338 print("Error: failed to open device") 339 except InvalidDeviceError as e: 340 print("This device does not have the capabilities for size-based touch detection.") 341 print("Details: {}".format(e)) 342 343 344if __name__ == "__main__": 345 main(sys.argv) 346