ó
&9_[c           @   sK  d  Z  d d l Z d d l Z d d l m Z e j j e j j d ƒ ƒ Z	 e j
 Z e e	 j _ e e	 j _ e e	 j _ e e g e	 j _ e	 j Z d „  Z d „  Z d „  Z e j j e j j d ƒ ƒ Z e j Z e e _ e e j e j e g e _ e j Z e j e _ e g e _ e j Z d e _ e e j g e _ e j Z e e _ e e e g e _ e j Z e e _ e j Z d e _ e e e g e _ e j  Z  d e  _ e g e  _ e j! Z! d e! _ e g e! _ d	 Z" e j# e d
 ƒ Z$ d „  Z% d „  Z& e ƒ  Z' d „  Z( e j) d e e e ƒ Z* e* e( ƒ Z+ d „  Z, d „  Z- d S(   sB   Inputhook for OS X

Calls NSApp / CoreFoundation APIs via ctypes.
iÿÿÿÿN(   t   Eventt   objcc         C   s%   t  |  t ƒ s! |  j d ƒ }  n  |  S(   s   ensure utf8 bytest   utf8(   t
   isinstancet   bytest   encode(   t   s(    (    sA   lib/python2.7/site-packages/IPython/terminal/pt_inputhooks/osx.pyt   _utf8   s    c         C   s   t  j t |  ƒ ƒ S(   s)   create a selector name (for ObjC methods)(   R   t   sel_registerNameR   (   t   name(    (    sA   lib/python2.7/site-packages/IPython/terminal/pt_inputhooks/osx.pyt   n   s    c         C   s   t  j t |  ƒ ƒ S(   s   get an ObjC Class by name(   R   t   objc_getClassR   (   t	   classname(    (    sA   lib/python2.7/site-packages/IPython/terminal/pt_inputhooks/osx.pyt   C!   s    t   CoreFoundationi   t   kCFRunLoopCommonModesc           C   s   t  t d ƒ t d ƒ ƒ S(   s0   Return the global NSApplication instance (NSApp)t   NSApplicationt   sharedApplication(   t   msgR   R
   (    (    (    sA   lib/python2.7/site-packages/IPython/terminal/pt_inputhooks/osx.pyt   _NSAppN   s    c         C   sY   t  t d ƒ t d ƒ d d d d d d d d d ƒ } t  |  t d ƒ t | ƒ t ƒ d S(   s   Wake the Applicationt   NSEvents]   otherEventWithType:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:i   i    s   postEvent:atStart:N(   R   R   R
   t   Nonet   void_pt   True(   t   NSAppt   event(    (    sA   lib/python2.7/site-packages/IPython/terminal/pt_inputhooks/osx.pyt   _wakeS   s    		c         C   sK   t  j ƒ  t |  ƒ t |  ƒ t ƒ  } t | t d ƒ | ƒ t | ƒ d S(   s.   Callback to fire when there's input to be reads   stop:N(   t
   _triggeredt   sett   CFFileDescriptorInvalidatet	   CFReleaseR   R   R
   R   (   t   fdreft   flagst   infoR   (    (    sA   lib/python2.7/site-packages/IPython/terminal/pt_inputhooks/osx.pyt   _input_callbackg   s    


	c         C   sh   t  j ƒ  t d |  t t d ƒ } t | t ƒ t d | d ƒ } t	 ƒ  } t
 | | t ƒ t | ƒ d S(   s;   Register callback to stop eventloop when there's data on fdi    N(   R   t   cleart   CFFileDescriptorCreateR   t   Falset   _c_input_callbackt   CFFileDescriptorEnableCallBackst   kCFFileDescriptorReadCallBackt#   CFFileDescriptorCreateRunLoopSourcet   CFRunLoopGetCurrentt   CFRunLoopAddSourceR   R   (   t   fdR   t   sourcet   loop(    (    sA   lib/python2.7/site-packages/IPython/terminal/pt_inputhooks/osx.pyt   _stop_on_readt   s    
	c         C   sI   t  ƒ  } t |  j ƒ  ƒ t | t d ƒ ƒ t j ƒ  sE t j ƒ  n  d S(   s   Inputhook for Cocoa (NSApp)t   runN(	   R   R/   t   filenoR   R
   R   t   is_setR   t   CFRunLoopRun(   t   contextR   (    (    sA   lib/python2.7/site-packages/IPython/terminal/pt_inputhooks/osx.pyt	   inputhook   s
    	(.   t   __doc__t   ctypest   ctypes.utilt	   threadingR    t   cdllt   LoadLibraryt   utilt   find_libraryR   t   c_void_pR   R   t   restypeR   t   objc_msgSendt   argtypesR   R   R
   R   R   R$   t   c_intt   c_boolt#   CFFileDescriptorGetNativeDescriptorR'   R   t   c_ulongR)   R*   R+   R   R   R(   t   in_dllR   R   R   R   R"   t	   CFUNCTYPEt   _c_callback_func_typeR&   R/   R5   (    (    (    sA   lib/python2.7/site-packages/IPython/terminal/pt_inputhooks/osx.pyt   <module>   s^   																										