From d7c40afa4334c25c967e8a83401ac1a968a0c084 Mon Sep 17 00:00:00 2001 From: derekhe Date: Sat, 27 Jun 2015 18:15:41 +0800 Subject: Add touch support. --- touch.py | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 touch.py (limited to 'touch.py') diff --git a/touch.py b/touch.py new file mode 100644 index 0000000..f32a28c --- /dev/null +++ b/touch.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 + +import struct +import time +import math +import glob +import uinput +import pyudev +import os + +# Wait and find devices +def read_and_emulate_mouse(deviceFound): + with open(deviceFound, 'rb') as f: + print("Read buffer") + + device = uinput.Device([ + uinput.BTN_LEFT, + uinput.BTN_RIGHT, + uinput.ABS_X, + uinput.ABS_Y, + ]) + + clicked = False + rightClicked = False + (lastX, lastY) = (0, 0) + startTime = time.time() + + while True: + b = f.read(25) + (tag, btnLeft, x, y) = struct.unpack_from('>c?HH', b) + print(btnLeft, x, y) + + if btnLeft: + device.emit(uinput.ABS_X, x, True) + device.emit(uinput.ABS_Y, y, True) + + if not clicked: + print("Left click") + device.emit(uinput.BTN_LEFT, 1) + clicked = True + startTime = time.time() + (lastX, lastY) = (x, y) + + duration = time.time() - startTime + movement = math.sqrt(pow(x - lastX, 2) + pow(y - lastY, 2)) + + if clicked and (not rightClicked) and (duration > 1) and (movement < 20): + print("Right click") + device.emit(uinput.BTN_RIGHT, 1) + device.emit(uinput.BTN_RIGHT, 0) + rightClicked = True + else: + print("Release") + clicked = False + rightClicked = False + device.emit(uinput.BTN_LEFT, 0) + + +if __name__ == "__main__": + os.system("modprobe uinput") + os.system("chmod 666 /dev/hidraw*") + os.system("chmod 666 /dev/uinput*") + + while True: + # try: + print("Waiting device") + hidrawDevices = glob.glob("/dev/hidraw*") + + context = pyudev.Context() + + deviceFound = None + for hid in hidrawDevices: + device = pyudev.Device.from_device_file(context, hid) + if "0EEF:0005" in device.device_path: + deviceFound = hid + + if deviceFound: + print("Device found", deviceFound) + read_and_emulate_mouse(deviceFound) + # except: + # print("Error:", sys.exc_info()) + # pass + # finally: + # time.sleep(1) -- cgit v1.2.1