o
    éË·eµ  ã                   @   sœ  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e_eejeje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 "ed¡Z#dd„ Z$dd„ Z%eƒ Z&dd„ Z'e (deee¡Z)e)e'ƒZ*dd„ Z+dd„ Z,dS )zBInputhook 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	   úY/var/www/ideatree/venv/lib/python3.10/site-packages/IPython/terminal/pt_inputhooks/osx.pyÚ_utf8   s   

r   c                 C   ó   t  t| ƒ¡S )z)create a selector name (for ObjC methods))r   Úsel_registerNamer   )Únamer	   r	   r
   Ún   ó   r   c                 C   r   )zget an ObjC Class by name)r   Úobjc_getClassr   )Ú	classnamer	   r	   r
   ÚC!   r   r   ÚCoreFoundationé   ÚkCFRunLoopCommonModesc                   C   s   t t gtj_ttdƒtdƒƒS )z0Return the global NSApplication instance (NSApp)ÚNSApplicationÚsharedApplication©Úvoid_pr   Úobjc_msgSendÚargtypesÚmsgr   r   r	   r	   r	   r
   Ú_NSAppN   s   r   c                 C   sl   t t t t t t t t t t t gtj_ttdƒtdƒdddddddddƒ}t t t t gtj_t| tdƒt |ƒdƒ dS )zWake the ApplicationÚNSEventz]otherEventWithType:location:modifierFlags:timestamp:windowNumber:context:subtype:data1:data2:é   r   NzpostEvent:atStart:Tr   )ÚNSAppÚeventr	   r	   r
   Ú_wakeT   s:   õÿòr#   c                 C   sH   t  ¡  t| ƒ t| ƒ tƒ }tttgtj_t	|t
dƒ|ƒ t|ƒ dS )z.Callback to fire when there's input to be readzstop:N)Ú
_triggeredÚsetÚCFFileDescriptorInvalidateÚ	CFReleaser   r   r   r   r   r   r   r#   )ÚfdrefÚflagsÚinfor!   r	   r	   r
   Ú_input_callbacky   s   r+   c                 C   sL   t  ¡  td| dtdƒ}t|tƒ td|dƒ}tƒ }t||t	ƒ t
|ƒ dS )z;Register callback to stop eventloop when there's data on fdNFr   )r$   ÚclearÚCFFileDescriptorCreateÚ_c_input_callbackÚCFFileDescriptorEnableCallBacksÚkCFFileDescriptorReadCallBackÚ#CFFileDescriptorCreateRunLoopSourceÚCFRunLoopGetCurrentÚCFRunLoopAddSourcer   r'   )Úfdr(   ÚsourceÚloopr	   r	   r
   Ú_stop_on_read‡   s   
r7   c                 C   sD   t ƒ }t|  ¡ ƒ ttgtj_t|tdƒƒ t	 
¡ s t ¡  dS dS )zInputhook for Cocoa (NSApp)ÚrunN)r   r7   Úfilenor   r   r   r   r   r   r$   Úis_setr   ÚCFRunLoopRun)Úcontextr!   r	   r	   r
   Ú	inputhook’   s   ûr=   )-Ú__doc__ÚctypesÚctypes.utilÚ	threadingr   ÚcdllÚLoadLibraryÚutilÚfind_libraryr   Úc_void_pr   r   Úrestyper   r   r   r   r   r   r   r   r-   Úc_intÚc_boolÚ#CFFileDescriptorGetNativeDescriptorr/   Úc_ulongr1   r2   r3   r'   r&   r0   Úin_dllr   r   r#   r$   r+   Ú	CFUNCTYPEÚ_c_callback_func_typer.   r7   r=   r	   r	   r	   r
   Ú<module>   s`    #
