• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
30
31try:
32    import libevdev
33    import pyudev
34except ModuleNotFoundError as e:
35    print("Error: {}".format(str(e)), file=sys.stderr)
36    print(
37        "One or more python modules are missing. Please install those "
38        "modules and re-run this tool."
39    )
40    sys.exit(1)
41
42
43class Range(object):
44    """Class to keep a min/max of a value around"""
45
46    def __init__(self):
47        self.min = float("inf")
48        self.max = float("-inf")
49
50    def update(self, value):
51        self.min = min(self.min, value)
52        self.max = max(self.max, value)
53
54
55class Touch(object):
56    """A single data point of a sequence (i.e. one event frame)"""
57
58    def __init__(self, major=None, minor=None, orientation=None):
59        self._major = major
60        self._minor = minor
61        self._orientation = orientation
62        self.dirty = False
63
64    @property
65    def major(self):
66        return self._major
67
68    @major.setter
69    def major(self, major):
70        self._major = major
71        self.dirty = True
72
73    @property
74    def minor(self):
75        return self._minor
76
77    @minor.setter
78    def minor(self, minor):
79        self._minor = minor
80        self.dirty = True
81
82    @property
83    def orientation(self):
84        return self._orientation
85
86    @orientation.setter
87    def orientation(self, orientation):
88        self._orientation = orientation
89        self.dirty = True
90
91    def __str__(self):
92        s = "Touch: major {:3d}".format(self.major)
93        if self.minor is not None:
94            s += ", minor {:3d}".format(self.minor)
95        if self.orientation is not None:
96            s += ", orientation {:+3d}".format(self.orientation)
97        return s
98
99
100class TouchSequence(object):
101    """A touch sequence from beginning to end"""
102
103    def __init__(self, device, tracking_id):
104        self.device = device
105        self.tracking_id = tracking_id
106        self.points = []
107
108        self.is_active = True
109
110        self.is_down = False
111        self.was_down = False
112        self.is_palm = False
113        self.was_palm = False
114        self.is_thumb = False
115        self.was_thumb = False
116
117        self.major_range = Range()
118        self.minor_range = Range()
119
120    def append(self, touch):
121        """Add a Touch to the sequence"""
122        self.points.append(touch)
123        self.major_range.update(touch.major)
124        self.minor_range.update(touch.minor)
125
126        if touch.major < self.device.up or touch.minor < self.device.up:
127            self.is_down = False
128        elif touch.major > self.device.down or touch.minor > self.device.down:
129            self.is_down = True
130            self.was_down = True
131
132        self.is_palm = touch.major > self.device.palm
133        if self.is_palm:
134            self.was_palm = True
135
136        self.is_thumb = self.device.thumb != 0 and touch.major > self.device.thumb
137        if self.is_thumb:
138            self.was_thumb = True
139
140    def finalize(self):
141        """Mark the TouchSequence as complete (finger is up)"""
142        self.is_active = False
143
144    def __str__(self):
145        return self._str_state() if self.is_active else self._str_summary()
146
147    def _str_summary(self):
148        if not self.points:
149            return "{:78s}".format("Sequence: no major/minor values recorded")
150
151        s = "Sequence: major: [{:3d}..{:3d}] ".format(
152            self.major_range.min, self.major_range.max
153        )
154        if self.device.has_minor:
155            s += "minor: [{:3d}..{:3d}] ".format(
156                self.minor_range.min, self.minor_range.max
157            )
158        if self.was_down:
159            s += " down"
160        if self.was_palm:
161            s += " palm"
162        if self.was_thumb:
163            s += " thumb"
164
165        return s
166
167    def _str_state(self):
168        touch = self.points[-1]
169        s = "{}, tags: {} {} {}".format(
170            touch,
171            "down" if self.is_down else "    ",
172            "palm" if self.is_palm else "    ",
173            "thumb" if self.is_thumb else "     ",
174        )
175        return s
176
177
178class InvalidDeviceError(Exception):
179    pass
180
181
182class Device(libevdev.Device):
183    def __init__(self, path):
184        if path is None:
185            self.path = self.find_touch_device()
186        else:
187            self.path = path
188
189        fd = open(self.path, "rb")
190        super().__init__(fd)
191
192        print("Using {}: {}\n".format(self.name, self.path))
193
194        if not self.has(libevdev.EV_ABS.ABS_MT_TOUCH_MAJOR):
195            raise InvalidDeviceError("Device does not have ABS_MT_TOUCH_MAJOR")
196
197        self.has_minor = self.has(libevdev.EV_ABS.ABS_MT_TOUCH_MINOR)
198        self.has_orientation = self.has(libevdev.EV_ABS.ABS_MT_ORIENTATION)
199
200        self.up = 0
201        self.down = 0
202        self.palm = 0
203        self.thumb = 0
204
205        self._init_thresholds_from_quirks()
206        self.sequences = []
207        self.touch = Touch(0, 0)
208
209    def find_touch_device(self):
210        context = pyudev.Context()
211        for device in context.list_devices(subsystem="input"):
212            if not device.get("ID_INPUT_TOUCHPAD", 0) and not device.get(
213                "ID_INPUT_TOUCHSCREEN", 0
214            ):
215                continue
216
217            if not device.device_node or not device.device_node.startswith(
218                "/dev/input/event"
219            ):
220                continue
221
222            return device.device_node
223
224        print("Unable to find a touch device.", file=sys.stderr)
225        sys.exit(1)
226
227    def _init_thresholds_from_quirks(self):
228        command = ["libinput", "quirks", "list", self.path]
229        cmd = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
230        if cmd.returncode != 0:
231            print(
232                "Error querying quirks: {}".format(cmd.stderr.decode("utf-8")),
233                file=sys.stderr,
234            )
235            return
236
237        stdout = cmd.stdout.decode("utf-8")
238        quirks = [q.split("=") for q in stdout.split("\n")]
239
240        for q in quirks:
241            if q[0] == "AttrPalmSizeThreshold":
242                self.palm = int(q[1])
243            elif q[0] == "AttrTouchSizeRange":
244                self.down, self.up = colon_tuple(q[1])
245            elif q[0] == "AttrThumbSizeThreshold":
246                self.thumb = int(q[1])
247
248    def start_new_sequence(self, tracking_id):
249        self.sequences.append(TouchSequence(self, tracking_id))
250
251    def current_sequence(self):
252        return self.sequences[-1]
253
254    def handle_key(self, event):
255        tapcodes = [
256            libevdev.EV_KEY.BTN_TOOL_DOUBLETAP,
257            libevdev.EV_KEY.BTN_TOOL_TRIPLETAP,
258            libevdev.EV_KEY.BTN_TOOL_QUADTAP,
259            libevdev.EV_KEY.BTN_TOOL_QUINTTAP,
260        ]
261        if event.code in tapcodes and event.value > 0:
262            print(
263                "\rThis tool cannot handle multiple fingers, " "output will be invalid",
264                file=sys.stderr,
265            )
266
267    def handle_abs(self, event):
268        if event.matches(libevdev.EV_ABS.ABS_MT_TRACKING_ID):
269            if event.value > -1:
270                self.start_new_sequence(event.value)
271            else:
272                try:
273                    s = self.current_sequence()
274                    s.finalize()
275                    print("\r{}".format(s))
276                except IndexError:
277                    # If the finger was down during start
278                    pass
279        elif event.matches(libevdev.EV_ABS.ABS_MT_TOUCH_MAJOR):
280            self.touch.major = event.value
281        elif event.matches(libevdev.EV_ABS.ABS_MT_TOUCH_MINOR):
282            self.touch.minor = event.value
283        elif event.matches(libevdev.EV_ABS.ABS_MT_ORIENTATION):
284            self.touch.orientation = event.value
285
286    def handle_syn(self, event):
287        if self.touch.dirty:
288            try:
289                self.current_sequence().append(self.touch)
290                print("\r{}".format(self.current_sequence()), end="")
291                self.touch = Touch(
292                    major=self.touch.major,
293                    minor=self.touch.minor,
294                    orientation=self.touch.orientation,
295                )
296            except IndexError:
297                pass
298
299    def handle_event(self, event):
300        if event.matches(libevdev.EV_ABS):
301            self.handle_abs(event)
302        elif event.matches(libevdev.EV_KEY):
303            self.handle_key(event)
304        elif event.matches(libevdev.EV_SYN):
305            self.handle_syn(event)
306
307    def read_events(self):
308        print("Ready for recording data.")
309        print("Touch sizes used: {}:{}".format(self.down, self.up))
310        print("Palm size used: {}".format(self.palm))
311        print("Thumb size used: {}".format(self.thumb))
312        print(
313            "Place a single finger on the device to measure touch size.\n"
314            "Ctrl+C to exit\n"
315        )
316
317        while True:
318            for event in self.events():
319                self.handle_event(event)
320
321
322def colon_tuple(string):
323    try:
324        ts = string.split(":")
325        t = tuple([int(x) for x in ts])
326        if len(t) == 2 and t[0] >= t[1]:
327            return t
328    except:  # noqa
329        pass
330
331    msg = "{} is not in format N:M (N >= M)".format(string)
332    raise argparse.ArgumentTypeError(msg)
333
334
335def main(args):
336    parser = argparse.ArgumentParser(description="Measure touch size and orientation")
337    parser.add_argument(
338        "path",
339        metavar="/dev/input/event0",
340        nargs="?",
341        type=str,
342        help="Path to device (optional)",
343    )
344    parser.add_argument(
345        "--touch-thresholds",
346        metavar="down:up",
347        type=colon_tuple,
348        help="Thresholds when a touch is logically down or up",
349    )
350    parser.add_argument(
351        "--palm-threshold",
352        metavar="t",
353        type=int,
354        help="Threshold when a touch is a palm",
355    )
356    args = parser.parse_args()
357
358    try:
359        device = Device(args.path)
360
361        if args.touch_thresholds is not None:
362            device.down, device.up = args.touch_thresholds
363
364        if args.palm_threshold is not None:
365            device.palm = args.palm_threshold
366
367        device.read_events()
368    except KeyboardInterrupt:
369        pass
370    except (PermissionError, OSError):
371        print("Error: failed to open device")
372    except InvalidDeviceError as e:
373        print(
374            "This device does not have the capabilities for size-based touch detection."
375        )
376        print("Details: {}".format(e))
377
378
379if __name__ == "__main__":
380    main(sys.argv)
381