<s> __author__ = '<STR_LIT>' <EOL> from Quartz import CoreGraphics as CG <EOL> from time import sleep <EOL> from . . interfaces . i_mouse import IMouse <EOL> from . . utils . mac_utils import MacUtils <EOL> class MacMouse ( IMouse ) : <EOL> LEFT_BUTTON = u'<STR_LIT>' <EOL> RIGHT_BUTTON = u'<STR_LIT>' <EOL> _SUPPORTED_BUTTON_NAMES = [ LEFT_BUTTON , RIGHT_BUTTON ] <EOL> _LEFT_BUTTON_CODES = [ <EOL> CG . kCGEventLeftMouseDown , <EOL> CG . kCGEventLeftMouseDragged , <EOL> CG . kCGEventLeftMouseUp ] <EOL> _RIGHT_BUTTON_CODES = [ <EOL> CG . kCGEventRightMouseDown , <EOL> CG . kCGEventRightMouseDragged , <EOL> CG . kCGEventRightMouseUp ] <EOL> def _compose_mouse_event_chain ( self , name , press = True , release = False ) : <EOL> """<STR_LIT>""" <EOL> mouse_event_chain = [ ] <EOL> if name == self . LEFT_BUTTON : <EOL> if press : <EOL> mouse_event_chain . append ( CG . kCGEventLeftMouseDown ) <EOL> if release : <EOL> mouse_event_chain . append ( CG . kCGEventLeftMouseUp ) <EOL> if name == self . RIGHT_BUTTON : <EOL> if press : <EOL> mouse_event_chain . append ( CG . kCGEventRightMouseDown ) <EOL> if release : <EOL> mouse_event_chain . append ( CG . kCGEventRightMouseUp ) <EOL> return mouse_event_chain <EOL> def _do_event ( self , code , x , y ) : <EOL> """<STR_LIT>""" <EOL> if code in self . _LEFT_BUTTON_CODES : <EOL> button = CG . kCGMouseButtonLeft <EOL> elif code in self . _RIGHT_BUTTON_CODES : <EOL> button = CG . kCGMouseButtonRight <EOL> else : <EOL> button = CG . kCGMouseButtonCenter <EOL> CG . CGEventPost ( <EOL> CG . kCGHIDEventTap , <EOL> CG . CGEventCreateMouseEvent ( None , code , ( x , y ) , button ) <EOL> ) <EOL> def _do_events ( self , codes , x , y ) : <EOL> """<STR_LIT>""" <EOL> for code in codes : <EOL> self . _do_event ( code , x , y ) <EOL> def move ( self , x , y , smooth = True ) : <EOL> MacUtils . verify_xy_coordinates ( x , y ) <EOL> old_x , old_y = self . get_position ( ) <EOL> for i in xrange ( <NUM_LIT:100> ) : <EOL> intermediate_x = old_x + ( x - old_x ) * ( i + <NUM_LIT:1> ) / <NUM_LIT> <EOL> intermediate_y = old_y + ( y - old_y ) * ( i + <NUM_LIT:1> ) / <NUM_LIT> <EOL> smooth and sleep ( <NUM_LIT> ) <EOL> self . _do_event ( CG . kCGEventMouseMoved , int ( intermediate_x ) , <EOL> int ( intermediate_y ) ) <EOL> def drag ( self , x1 , y1 , x2 , y2 , smooth = True ) : <EOL> MacUtils . verify_xy_coordinates ( x1 , y1 ) <EOL> MacUtils . verify_xy_coordinates ( x2 , y2 ) <EOL> self . press_button ( x1 , y1 , self . LEFT_BUTTON ) <EOL> for i in xrange ( <NUM_LIT:100> ) : <EOL> x = x1 + ( x2 - x1 ) * ( i + <NUM_LIT:1> ) / <NUM_LIT> <EOL> y = y1 + ( y2 - y1 ) * ( i + <NUM_LIT:1> ) / <NUM_LIT> <EOL> smooth and sleep ( <NUM_LIT> ) <EOL> self . _do_event ( CG . kCGEventLeftMouseDragged , x , y ) <EOL> self . release_button ( self . LEFT_BUTTON ) <EOL> def press_button ( self , x , y , button_name = LEFT_BUTTON ) : <EOL> MacUtils . verify_xy_coordinates ( x , y ) <EOL> MacUtils . verify_mouse_button_name ( button_name , <EOL> self . _SUPPORTED_BUTTON_NAMES ) <EOL> event_codes = self . _compose_mouse_event_chain ( <EOL> button_name , press = True , release = False ) <EOL> self . _do_events ( event_codes , x , y ) <EOL> def release_button ( self , button_name = LEFT_BUTTON ) : <EOL> MacUtils . verify_mouse_button_name ( button_name , <EOL> self . _SUPPORTED_BUTTON_NAMES ) <EOL> event_codes = self . _compose_mouse_event_chain ( <EOL> button_name , press = False , release = True ) <EOL> self . _do_events ( event_codes , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> def click ( self , x , y , button_name = LEFT_BUTTON ) : <EOL> MacUtils . verify_xy_coordinates ( x , y ) <EOL> MacUtils . verify_mouse_button_name ( button_name , <EOL> self . _SUPPORTED_BUTTON_NAMES ) <EOL> event_codes = self . _compose_mouse_event_chain ( <EOL> button_name , press = True , release = True ) <EOL> self . _do_events ( event_codes , x , y ) <EOL> def double_click ( self , x , y , button_name = LEFT_BUTTON ) : <EOL> MacUtils . verify_xy_coordinates ( x , y ) <EOL> MacUtils . verify_mouse_button_name ( button_name , <EOL> self . _SUPPORTED_BUTTON_NAMES ) <EOL> if button_name == self . LEFT_BUTTON : <EOL> button = CG . kCGMouseButtonLeft <EOL> down = CG . kCGEventLeftMouseDown <EOL> up = CG . kCGEventLeftMouseUp <EOL> if button_name == self . RIGHT_BUTTON : <EOL> button = CG . kCGMouseButtonRight <EOL> down = CG . kCGEventRightMouseDown <EOL> up = CG . kCGEventRightMouseUp <EOL> event = CG . CGEventCreateMouseEvent ( None , down , ( x , y ) , button ) <EOL> CG . CGEventPost ( CG . kCGHIDEventTap , event ) <EOL> CG . CGEventSetType ( event , up ) <EOL> CG . CGEventPost ( CG . kCGHIDEventTap , event ) <EOL> CG . CGEventSetIntegerValueField ( event , CG . kCGMouseEventClickState , <NUM_LIT:2> ) <EOL> CG . CGEventSetType ( event , down ) <EOL> CG . CGEventPost ( CG . kCGHIDEventTap , event ) <EOL> CG . CGEventSetType ( event , up ) <EOL> CG . CGEventPost ( CG . kCGHIDEventTap , event ) <EOL> def get_position ( self ) : <EOL> position = CG . CGEventGetLocation ( CG . CGEventCreate ( None ) ) <EOL> return int ( position . x ) , int ( position . y ) </s>
