U
    ‰ª/e  ã                   @   sb  d Z ddlZddlZddlmZ ej ej d¡¡Z	ej
Zee	j_ee	j_ee	j_eege	j_e	jZdd„ Zdd„ Zd	d
„ Zej ej d¡¡ZejZeje_ejZee_ejZee_ejZde_ege_ejZee_eejejejejeege_ejZde_eeege_e ed¡Zdd„ Z dd„ Z!eƒ Z"ddd„Z#e $dee¡Z%e%e#ƒZ&dd„ Z'ddd„Z(dS )zGEventloop hook for OS X

Calls NSApp / CoreFoundation APIs via ctypes.
é    N)ÚEventÚobjcc                 C   s   t | tƒs|  d¡} | S )zensure utf8 bytesÚutf8)Ú
isinstanceÚbytesÚencode)Ús© r	   ú>/tmp/pip-unpacked-wheel-g43y689f/ipykernel/_eventloop_macos.pyÚ_utf8   s    

r   c                 C   s   t  t| ƒ¡S )z)create a selector name (for ObjC methods))r   Úsel_registerNamer   )Únamer	   r	   r
   Ún    s    r   c                 C   s   t  t| ƒ¡S )zget an ObjC Class by name)r   Úobjc_getClassr   )Ú	classnamer	   r	   r
   ÚC%   s    r   ÚCoreFoundationÚkCFRunLoopCommonModesc                   C   s   t tdƒtdƒƒS )z0Return the global NSApplication instance (NSApp)ZNSApplicationZsharedApplication)Úmsgr   r   r	   r	   r	   r
   Ú_NSAppQ   s    r   c                 C   s>   t tdƒtdƒdddddddddƒ}t | tdƒt|ƒdƒ dS )zWake the ApplicationZNSEventz]otherEventWithType:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:é   r   NzpostEvent:atStart:T)r   r   r   Úvoid_p)ÚNSAppÚeventr	   r	   r
   Ú_wakeV   s     ÿòr   c                 C   sD   t  ¡  tƒ }t|tdƒƒr6t|tdƒ|ƒ t|ƒ n
ttƒ ƒ dS )z.Callback to fire when there's input to be readZ	isRunningzstop:N)Ú
_triggeredÚsetr   r   r   r   ÚCFRunLoopStopÚCFRunLoopGetCurrent)ÚtimerZloopr   r	   r	   r
   Ústopn   s    
r    c                 C   s,   t dtƒ |  dddtdƒ}ttƒ |tƒ dS )z1Register callback to stop eventloop after a delayNr   )ÚCFRunLoopTimerCreateÚCFAbsoluteTimeGetCurrentÚ_c_stop_callbackÚCFRunLoopAddTimerÚCFRunLoopGetMainr   )Údelayr   r	   r	   r
   Ú_stop_after   s    ù	ýr'   é   c                 C   s8   t  ¡  tƒ }t| ƒ t|tdƒƒ t  ¡ s4t ¡  dS )z<run the Cocoa eventloop for the specified duration (seconds)ÚrunN)	r   Úclearr   r'   r   r   Úis_setr   ZCFRunLoopRun)Údurationr   r	   r	   r
   Úmainloop‘   s    r-   )NN)r(   ))Ú__doc__ÚctypesZctypes.utilÚ	threadingr   ÚcdllÚLoadLibraryÚutilZfind_libraryr   Úc_void_pr   r   Úrestyper   Zobjc_msgSendÚargtypesr   r   r   r   r   r"   Úc_doubler   r%   r   r!   Úc_intr$   Úin_dllr   r   r   r   r    Ú	CFUNCTYPEZ_c_callback_func_typer#   r'   r-   r	   r	   r	   r
   Ú<module>   s^   
ÿù

