1#!/usr/bin/env python3

2# This file is part of Checkbox.

3#

4# Copyright 2019-2022 Canonical Ltd.

5#

6# Checkbox is free software: you can redistribute it and/or modify

7# it under the terms of the GNU General Public License version 3,

8# as published by the Free Software Foundation.

9#

10# Checkbox is distributed in the hope that it will be useful,

11# but WITHOUT ANY WARRANTY; without even the implied warranty of

12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

13# GNU General Public License for more details.

14#

15# You should have received a copy of the GNU General Public License

16# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.

17

18 import argparse

19

20 import evdev

21 from evdev.ecodes import ABS_MT_SLOT # press event

22 from evdev.ecodes import BTN_TOUCH # finger touches the screen

23 from evdev.ecodes import EV_ABS # touch event on touchscreen

24 from evdev.ecodes import EV_KEY

25

26

27 def get_device(name):

28 for d in [evdev.InputDevice(path) for path in evdev.list_devices()]:

29 if d.name == name:

30 return d

31 else:

32 raise SystemExit("Touch device not found!")

33

34

35 def main():

36 parser = argparse.ArgumentParser()

37 parser.add_argument("name", help="Touch device product name")

38 parser.add_argument(

39 "--xfingers", "-x", type=int, default=1, help="X-fingers tap event"

40 )

41 args = parser.parse_args()

42

43 device = get_device(args.name)

44 tap = args.xfingers

45 while True:

46 for e in device.read_loop():

47 if tap == 1:

48 if e.type == EV_ABS and e.code == ABS_MT_SLOT and e.value > 0:

49 raise SystemExit(

50 "Multitouch Event detected but Single was expected"

51 )

52 # value is a boolean, 1 means a PRESS, 0 a RELEASED event

53 if e.type == EV_KEY and e.code == BTN_TOUCH and e.value == 1:

54 print("SUCCESS:", e)

55 raise SystemExit

56 else:

57 # value is the 0-indexed amount of simultaneous detected

58 # fingers

59 if (

60 e.type == EV_ABS

61 and e.code == ABS_MT_SLOT

62 and e.value == tap - 1

63 ):

64 print("SUCCESS:", e)

65 raise SystemExit

66

67

68 if __name__ == "__main__":

69 main()