<s> '''<STR_LIT>''' <EOL> __all__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> import os <EOL> from kivy . input . motionevent import MotionEvent <EOL> from kivy . input . shape import ShapeRect <EOL> class MTDMotionEvent ( MotionEvent ) : <EOL> def depack ( self , args ) : <EOL> self . is_touch = True <EOL> self . sx = args [ '<STR_LIT:x>' ] <EOL> self . sy = args [ '<STR_LIT:y>' ] <EOL> self . profile = [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in args and '<STR_LIT>' in args : <EOL> self . shape = ShapeRect ( ) <EOL> self . shape . width = args [ '<STR_LIT>' ] <EOL> self . shape . height = args [ '<STR_LIT>' ] <EOL> self . profile . append ( '<STR_LIT>' ) <EOL> if '<STR_LIT>' in args : <EOL> self . pressure = args [ '<STR_LIT>' ] <EOL> self . profile . append ( '<STR_LIT>' ) <EOL> super ( MTDMotionEvent , self ) . depack ( args ) <EOL> def __str__ ( self ) : <EOL> i , sx , sy , d = ( self . id , self . sx , self . sy , self . device ) <EOL> return '<STR_LIT>' % ( i , sx , sy , d ) <EOL> if '<STR_LIT>' in os . environ : <EOL> MTDMotionEventProvider = None <EOL> else : <EOL> import threading <EOL> import collections <EOL> from kivy . lib . mtdev import Device , MTDEV_TYPE_EV_ABS , MTDEV_CODE_SLOT , MTDEV_CODE_POSITION_X , MTDEV_CODE_POSITION_Y , MTDEV_CODE_PRESSURE , MTDEV_CODE_TOUCH_MAJOR , MTDEV_CODE_TOUCH_MINOR , MTDEV_CODE_TRACKING_ID , MTDEV_ABS_POSITION_X , MTDEV_ABS_POSITION_Y , MTDEV_ABS_TOUCH_MINOR , MTDEV_ABS_TOUCH_MAJOR <EOL> from kivy . input . provider import MotionEventProvider <EOL> from kivy . input . factory import MotionEventFactory <EOL> from kivy . logger import Logger <EOL> class MTDMotionEventProvider ( MotionEventProvider ) : <EOL> options = ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def __init__ ( self , device , args ) : <EOL> super ( MTDMotionEventProvider , self ) . __init__ ( device , args ) <EOL> self . _device = None <EOL> self . input_fn = None <EOL> self . default_ranges = dict ( ) <EOL> args = args . split ( '<STR_LIT:U+002C>' ) <EOL> if not args : <EOL> Logger . error ( '<STR_LIT>' ) <EOL> Logger . error ( '<STR_LIT>' ) <EOL> return None <EOL> self . input_fn = args [ <NUM_LIT:0> ] <EOL> Logger . info ( '<STR_LIT>' % self . input_fn ) <EOL> for arg in args [ <NUM_LIT:1> : ] : <EOL> if arg == '<STR_LIT>' : <EOL> continue <EOL> arg = arg . split ( '<STR_LIT:=>' ) <EOL> if len ( arg ) != <NUM_LIT:2> : <EOL> err = '<STR_LIT>' % arg <EOL> Logger . error ( err ) <EOL> continue <EOL> key , value = arg <EOL> if key not in MTDMotionEventProvider . options : <EOL> Logger . error ( '<STR_LIT>' % key ) <EOL> continue <EOL> try : <EOL> self . default_ranges [ key ] = int ( value ) <EOL> except ValueError : <EOL> err = '<STR_LIT>' % ( key , value ) <EOL> Logger . error ( err ) <EOL> continue <EOL> Logger . info ( '<STR_LIT>' % ( key , int ( value ) ) ) <EOL> if '<STR_LIT>' not in self . default_ranges : <EOL> self . default_ranges [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> elif self . default_ranges [ '<STR_LIT>' ] not in ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) : <EOL> Logger . error ( '<STR_LIT>' . format ( <EOL> self . default_ranges [ '<STR_LIT>' ] ) ) <EOL> self . default_ranges [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> def start ( self ) : <EOL> if self . input_fn is None : <EOL> return <EOL> self . uid = <NUM_LIT:0> <EOL> self . queue = collections . deque ( ) <EOL> self . thread = threading . Thread ( <EOL> target = self . _thread_run , <EOL> kwargs = dict ( <EOL> queue = self . queue , <EOL> input_fn = self . input_fn , <EOL> device = self . device , <EOL> default_ranges = self . default_ranges ) ) <EOL> self . thread . daemon = True <EOL> self . thread . start ( ) <EOL> def _thread_run ( self , ** kwargs ) : <EOL> input_fn = kwargs . get ( '<STR_LIT>' ) <EOL> queue = kwargs . get ( '<STR_LIT>' ) <EOL> device = kwargs . get ( '<STR_LIT>' ) <EOL> drs = kwargs . get ( '<STR_LIT>' ) . get <EOL> touches = { } <EOL> touches_sent = [ ] <EOL> point = { } <EOL> l_points = { } <EOL> def assign_coord ( point , value , invert , coords ) : <EOL> cx , cy = coords <EOL> if invert : <EOL> value = <NUM_LIT:1.> - value <EOL> if rotation == <NUM_LIT:0> : <EOL> point [ cx ] = value <EOL> elif rotation == <NUM_LIT> : <EOL> point [ cy ] = value <EOL> elif rotation == <NUM_LIT> : <EOL> point [ cx ] = <NUM_LIT:1.> - value <EOL> elif rotation == <NUM_LIT> : <EOL> point [ cy ] = <NUM_LIT:1.> - value <EOL> def process ( points ) : <EOL> for args in points : <EOL> if '<STR_LIT:id>' not in args : <EOL> continue <EOL> tid = args [ '<STR_LIT:id>' ] <EOL> try : <EOL> touch = touches [ tid ] <EOL> except KeyError : <EOL> touch = MTDMotionEvent ( device , tid , args ) <EOL> touches [ touch . id ] = touch <EOL> touch . move ( args ) <EOL> action = '<STR_LIT>' <EOL> if tid not in touches_sent : <EOL> action = '<STR_LIT>' <EOL> touches_sent . append ( tid ) <EOL> if '<STR_LIT>' in args : <EOL> action = '<STR_LIT:end>' <EOL> del args [ '<STR_LIT>' ] <EOL> del touches [ touch . id ] <EOL> touches_sent . remove ( tid ) <EOL> touch . update_time_end ( ) <EOL> queue . append ( ( action , touch ) ) <EOL> def normalize ( value , vmin , vmax ) : <EOL> return ( value - vmin ) / float ( vmax - vmin ) <EOL> _fn = input_fn <EOL> _slot = <NUM_LIT:0> <EOL> _device = Device ( _fn ) <EOL> _changes = set ( ) <EOL> ab = _device . get_abs ( MTDEV_ABS_POSITION_X ) <EOL> range_min_position_x = drs ( '<STR_LIT>' , ab . minimum ) <EOL> range_max_position_x = drs ( '<STR_LIT>' , ab . maximum ) <EOL> Logger . info ( '<STR_LIT>' % <EOL> ( _fn , range_min_position_x , range_max_position_x ) ) <EOL> ab = _device . get_abs ( MTDEV_ABS_POSITION_Y ) <EOL> range_min_position_y = drs ( '<STR_LIT>' , ab . minimum ) <EOL> range_max_position_y = drs ( '<STR_LIT>' , ab . maximum ) <EOL> Logger . info ( '<STR_LIT>' % <EOL> ( _fn , range_min_position_y , range_max_position_y ) ) <EOL> ab = _device . get_abs ( MTDEV_ABS_TOUCH_MAJOR ) <EOL> range_min_major = drs ( '<STR_LIT>' , ab . minimum ) <EOL> range_max_major = drs ( '<STR_LIT>' , ab . maximum ) <EOL> Logger . info ( '<STR_LIT>' % <EOL> ( _fn , range_min_major , range_max_major ) ) <EOL> ab = _device . get_abs ( MTDEV_ABS_TOUCH_MINOR ) <EOL> range_min_minor = drs ( '<STR_LIT>' , ab . minimum ) <EOL> range_max_minor = drs ( '<STR_LIT>' , ab . maximum ) <EOL> Logger . info ( '<STR_LIT>' % <EOL> ( _fn , range_min_minor , range_max_minor ) ) <EOL> range_min_pressure = drs ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> range_max_pressure = drs ( '<STR_LIT>' , <NUM_LIT:255> ) <EOL> Logger . info ( '<STR_LIT>' % <EOL> ( _fn , range_min_pressure , range_max_pressure ) ) <EOL> invert_x = int ( bool ( drs ( '<STR_LIT>' , <NUM_LIT:0> ) ) ) <EOL> invert_y = int ( bool ( drs ( '<STR_LIT>' , <NUM_LIT:0> ) ) ) <EOL> Logger . info ( '<STR_LIT>' % <EOL> ( _fn , invert_x , invert_y ) ) <EOL> rotation = drs ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> Logger . info ( '<STR_LIT>' % <EOL> ( _fn , rotation ) ) <EOL> while _device : <EOL> while _device . idle ( <NUM_LIT:1000> ) : <EOL> continue <EOL> while True : <EOL> data = _device . get ( ) <EOL> if data is None : <EOL> break <EOL> if data . type == MTDEV_TYPE_EV_ABS and data . code == MTDEV_CODE_SLOT : <EOL> _slot = data . value <EOL> continue <EOL> if not _slot in l_points : <EOL> l_points [ _slot ] = dict ( ) <EOL> point = l_points [ _slot ] <EOL> ev_value = data . value <EOL> ev_code = data . code <EOL> if ev_code == MTDEV_CODE_POSITION_X : <EOL> val = normalize ( ev_value , <EOL> range_min_position_x , <EOL> range_max_position_x ) <EOL> assign_coord ( point , val , invert_x , '<STR_LIT>' ) <EOL> elif ev_code == MTDEV_CODE_POSITION_Y : <EOL> val = <NUM_LIT:1.> - normalize ( ev_value , <EOL> range_min_position_y , <EOL> range_max_position_y ) <EOL> assign_coord ( point , val , invert_y , '<STR_LIT>' ) <EOL> elif ev_code == MTDEV_CODE_PRESSURE : <EOL> point [ '<STR_LIT>' ] = normalize ( ev_value , <EOL> range_min_pressure , <EOL> range_max_pressure ) <EOL> elif ev_code == MTDEV_CODE_TOUCH_MAJOR : <EOL> point [ '<STR_LIT>' ] = normalize ( ev_value , <EOL> range_min_major , <EOL> range_max_major ) <EOL> elif ev_code == MTDEV_CODE_TOUCH_MINOR : <EOL> point [ '<STR_LIT>' ] = normalize ( ev_value , <EOL> range_min_minor , <EOL> range_max_minor ) <EOL> elif ev_code == MTDEV_CODE_TRACKING_ID : <EOL> if ev_value == - <NUM_LIT:1> : <EOL> point [ '<STR_LIT>' ] = True <EOL> _changes . add ( _slot ) <EOL> process ( [ l_points [ x ] for x in _changes ] ) <EOL> _changes . clear ( ) <EOL> continue <EOL> else : <EOL> point [ '<STR_LIT:id>' ] = ev_value <EOL> else : <EOL> continue <EOL> _changes . add ( _slot ) <EOL> if _changes : <EOL> process ( [ l_points [ x ] for x in _changes ] ) <EOL> _changes . clear ( ) <EOL> def update ( self , dispatch_fn ) : <EOL> try : <EOL> while True : <EOL> event_type , touch = self . queue . popleft ( ) <EOL> dispatch_fn ( event_type , touch ) <EOL> except : <EOL> pass <EOL> MotionEventFactory . register ( '<STR_LIT>' , MTDMotionEventProvider ) </s>
