chris_c
Posts: 147
Joined: Sun May 06, 2012 10:23 am

Work around for broken right click emulation

Tue Jan 26, 2016 5:36 pm

it aint nice, it aint perfect....

Code: Select all

#!/usr/bin/env python3.4
"""
copyright (C) 2016 Mr C Camacho (codifies) all rights reserved
bedroomcoders.co.uk programming and electronics blog

rightTouch - fake right click on touchscreen

Xorg button 3 emulation seems to have stopped working on
multiple distributions (upstream issue?)

in the mean time this script is a work around

it requires access to /dev/uinput and you should be in the
input group for /dev/input/*
care should be taken with write permissions on /dev/uinput !!

if you choose to use this script you must agree to take
FULL responsibility for all damages

USE AT YOUR OWN RISK

If you publish this script, you must include this unaltered comment
at the top of the script.
"""

INTERVAL=20 # poll interval in ms
TIMEOUT=500 # screen pressed for longer than this long then its a right click
AXISTHRESHOLD=20 # if you move more than this distance along one axis its NOT a right click

from evdev import InputDevice, list_devices, categorize, ecodes, UInput
from datetime import datetime,timezone,timedelta
from time import sleep
import sys

caps = { # capabilities for uinput output
#    ecodes.EV_REL : (ecodes.REL_X, ecodes.REL_Y), 
    ecodes.EV_ABS : (ecodes.ABS_X, ecodes.ABS_Y, ecodes.ABS_MT_TRACKING_ID, ecodes.ABS_MT_POSITION_X, ecodes.ABS_MT_POSITION_Y),
    ecodes.EV_KEY : (ecodes.BTN_LEFT, ecodes.BTN_RIGHT),
}

found=False # device found
curX=-1     # last ABS pos
curY=-1
downX=-1    # position when initial touch happened
downY=-1

out = UInput(caps)  # for sending fake events
leftDown = 0;       # timeout counter
rightDown = False;  # state of fake right mouse 
epoch = datetime(1970, 1, 1, tzinfo=timezone.utc) # use POSIX epoch

devices = [InputDevice(fn) for fn in list_devices()]
for d in devices:
   #print(d.fn, d.name, d.phys)
   if d.name=="FT5406 memory based driver":
	   dev = d
	   #print ("Device found")
	   found=True
	   break


if not found:
	print("Can't find the touch screen")
	sys.exit()

# we have to poll because we want a timeout as well as spying
# on events
while True:
    sleep(INTERVAL/1000.0)
    
    event = dev.read_one()
    
    while event != None:
        if event.type == ecodes.EV_KEY:
            if event.code == ecodes.BTN_TOUCH:
                if event.value == 1:
                    if leftDown == 0:
                        leftDown = int ( (event.sec * 1000) + (event.usec/1000) )
                        downX=curX
                        downY=curY
                if event.value == 0:
                    leftDown=0
                    if rightDown:
                        out.write(ecodes.EV_KEY, ecodes.BTN_RIGHT, 0)
                        out.syn()
                        rightDown = False
        else:
            if event.type==ecodes.EV_ABS:
                if event.code==ecodes.ABS_MT_POSITION_X:
                    curX=event.value
                    if leftDown>0:
                        if abs(curX-downX)>AXISTHRESHOLD:
                            leftDown = 0
                if event.code==ecodes.ABS_MT_POSITION_Y:
                    curY=event.value
                    if leftDown>0:
                        if abs(curY-downY)>AXISTHRESHOLD:
                            leftDown = 0
            
        event = dev.read_one()

    if leftDown!=0:
        now = int ((datetime.now(timezone.utc) - epoch) / timedelta(milliseconds=1))
        if (now-leftDown)>TIMEOUT:
            out.write(ecodes.EV_KEY, ecodes.BTN_RIGHT, 1)
            out.syn()
            rightDown = True
            leftDown=0


User avatar
FloatingCloud
Posts: 30
Joined: Wed Nov 18, 2015 9:53 am

Re: Work around for broken right click emulation

Tue Jun 12, 2018 10:27 am

It aint nice, it aint perfect... but it works!
Just change "FT5406 memory based driver" for whatever your touchscreen driver is.
Thanks chris_c!! :D

Return to “Official Foundation Display”

Who is online

Users browsing this forum: No registered users and 3 guests