<s> '''<STR_LIT>''' <EOL> __all__ = ( '<STR_LIT>' , ) <EOL> import ctypes <EOL> import threading <EOL> import collections <EOL> import os <EOL> from kivy . input . provider import MotionEventProvider <EOL> from kivy . input . factory import MotionEventFactory <EOL> from kivy . input . motionevent import MotionEvent <EOL> from kivy . input . shape import ShapeRect <EOL> if '<STR_LIT>' not in os . environ : <EOL> CFArrayRef = ctypes . c_void_p <EOL> CFMutableArrayRef = ctypes . c_void_p <EOL> CFIndex = ctypes . c_long <EOL> dll = '<STR_LIT>' + '<STR_LIT>' <EOL> MultitouchSupport = ctypes . CDLL ( dll ) <EOL> CFArrayGetCount = MultitouchSupport . CFArrayGetCount <EOL> CFArrayGetCount . argtypes = [ CFArrayRef ] <EOL> CFArrayGetCount . restype = CFIndex <EOL> CFArrayGetValueAtIndex = MultitouchSupport . CFArrayGetValueAtIndex <EOL> CFArrayGetValueAtIndex . argtypes = [ CFArrayRef , CFIndex ] <EOL> CFArrayGetValueAtIndex . restype = ctypes . c_void_p <EOL> MTDeviceCreateList = MultitouchSupport . MTDeviceCreateList <EOL> MTDeviceCreateList . argtypes = [ ] <EOL> MTDeviceCreateList . restype = CFMutableArrayRef <EOL> class MTPoint ( ctypes . Structure ) : <EOL> _fields_ = [ ( '<STR_LIT:x>' , ctypes . c_float ) , <EOL> ( '<STR_LIT:y>' , ctypes . c_float ) ] <EOL> class MTVector ( ctypes . Structure ) : <EOL> _fields_ = [ ( '<STR_LIT>' , MTPoint ) , <EOL> ( '<STR_LIT>' , MTPoint ) ] <EOL> class MTData ( ctypes . Structure ) : <EOL> _fields_ = [ <EOL> ( '<STR_LIT>' , ctypes . c_int ) , <EOL> ( '<STR_LIT>' , ctypes . c_double ) , <EOL> ( '<STR_LIT>' , ctypes . c_int ) , <EOL> ( '<STR_LIT:state>' , ctypes . c_int ) , <EOL> ( '<STR_LIT>' , ctypes . c_int ) , <EOL> ( '<STR_LIT>' , ctypes . c_int ) , <EOL> ( '<STR_LIT>' , MTVector ) , <EOL> ( '<STR_LIT:size>' , ctypes . c_float ) , <EOL> ( '<STR_LIT>' , ctypes . c_int ) , <EOL> ( '<STR_LIT>' , ctypes . c_float ) , <EOL> ( '<STR_LIT>' , ctypes . c_float ) , <EOL> ( '<STR_LIT>' , ctypes . c_float ) , <EOL> ( '<STR_LIT>' , MTVector ) , <EOL> ( '<STR_LIT>' , ctypes . c_int ) , <EOL> ( '<STR_LIT>' , ctypes . c_int ) , <EOL> ( '<STR_LIT>' , ctypes . c_float ) , ] <EOL> MTDataRef = ctypes . POINTER ( MTData ) <EOL> MTContactCallbackFunction = ctypes . CFUNCTYPE ( ctypes . c_int , ctypes . c_int , <EOL> MTDataRef , ctypes . c_int , <EOL> ctypes . c_double , ctypes . c_int ) <EOL> MTDeviceRef = ctypes . c_void_p <EOL> MTRegisterContactFrameCallback = MultitouchSupport . MTRegisterContactFrameCallback <EOL> MTRegisterContactFrameCallback . argtypes = [ MTDeviceRef , MTContactCallbackFunction ] <EOL> MTRegisterContactFrameCallback . restype = None <EOL> MTDeviceStart = MultitouchSupport . MTDeviceStart <EOL> MTDeviceStart . argtypes = [ MTDeviceRef , ctypes . c_int ] <EOL> MTDeviceStart . restype = None <EOL> else : <EOL> MTContactCallbackFunction = lambda x : None <EOL> class MacMotionEvent ( MotionEvent ) : <EOL> '''<STR_LIT>''' <EOL> def depack ( self , args ) : <EOL> self . is_touch = True <EOL> self . shape = ShapeRect ( ) <EOL> self . sx , self . sy = args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] <EOL> self . shape . width = args [ <NUM_LIT:2> ] <EOL> self . shape . height = args [ <NUM_LIT:2> ] <EOL> self . profile = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> super ( MacMotionEvent , self ) . depack ( args ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . id , self . sx , self . sy , self . device ) <EOL> _instance = None <EOL> class MacMotionEventProvider ( MotionEventProvider ) : <EOL> def __init__ ( self , * largs , ** kwargs ) : <EOL> global _instance <EOL> if _instance is not None : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> _instance = self <EOL> super ( MacMotionEventProvider , self ) . __init__ ( * largs , ** kwargs ) <EOL> def start ( self ) : <EOL> self . uid = <NUM_LIT:0> <EOL> self . touches = { } <EOL> self . lock = threading . Lock ( ) <EOL> self . queue = collections . deque ( ) <EOL> devices = MultitouchSupport . MTDeviceCreateList ( ) <EOL> num_devices = CFArrayGetCount ( devices ) <EOL> for i in range ( num_devices ) : <EOL> device = CFArrayGetValueAtIndex ( devices , i ) <EOL> data_id = str ( device ) <EOL> self . touches [ data_id ] = { } <EOL> MTRegisterContactFrameCallback ( device , self . _mts_callback ) <EOL> MTDeviceStart ( device , <NUM_LIT:0> ) <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> def stop ( self ) : <EOL> pass <EOL> @ MTContactCallbackFunction <EOL> def _mts_callback ( device , data_ptr , n_fingers , timestamp , frame ) : <EOL> global _instance <EOL> devid = str ( device ) <EOL> if not devid in _instance . touches : <EOL> _instance . touches [ devid ] = { } <EOL> touches = _instance . touches [ devid ] <EOL> actives = [ ] <EOL> for i in range ( n_fingers ) : <EOL> data = data_ptr [ i ] <EOL> actives . append ( data . identifier ) <EOL> data_id = data . identifier <EOL> norm_pos = data . normalized . position <EOL> args = ( norm_pos . x , norm_pos . y , data . size ) <EOL> if not data_id in touches : <EOL> _instance . lock . acquire ( ) <EOL> _instance . uid += <NUM_LIT:1> <EOL> touch = MacMotionEvent ( _instance . device , _instance . uid , args ) <EOL> _instance . lock . release ( ) <EOL> _instance . queue . append ( ( '<STR_LIT>' , touch ) ) <EOL> touches [ data_id ] = touch <EOL> else : <EOL> touch = touches [ data_id ] <EOL> if data . normalized . position . x == touch . sx and data . normalized . position . y == touch . sy : <EOL> continue <EOL> touch . move ( args ) <EOL> _instance . queue . append ( ( '<STR_LIT>' , touch ) ) <EOL> for tid in list ( touches . keys ( ) ) [ : ] : <EOL> if tid not in actives : <EOL> touch = touches [ tid ] <EOL> touch . update_time_end ( ) <EOL> _instance . queue . append ( ( '<STR_LIT:end>' , touch ) ) <EOL> del touches [ tid ] <EOL> return <NUM_LIT:0> <EOL> MotionEventFactory . register ( '<STR_LIT>' , MacMotionEventProvider ) </s>
