
    i#                        d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZ d Zej8                  d        Z G d	 d
e      Zej8                  d        Z ddZ!d Z" G d de      Z#y)z9
Utility functions and classes for the *Darwin* backend.
    N)	CFRelease)CFMachPortCreateRunLoopSourceCFRunLoopAddSourceCFRunLoopGetCurrentCFRunLoopRunInModeCFRunLoopStopCGEventGetIntegerValueFieldCGEventTapCreateCGEventTapEnablekCFRunLoopDefaultModekCFRunLoopRunTimedOutkCGEventSourceUnixProcessIDkCGEventTapOptionDefaultkCGEventTapOptionListenOnlykCGHeadInsertEventTapkCGSessionEventTap   )AbstractListenerc                 6    | t        j                  |       S dS )zzConverts a pointer to a *Python objc* value.

    :param value: The pointer to convert.

    :return: a wrapped value
    N)c_void_p)objcobjc_object)values    ]/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/pynput/_util/darwin.py_wrap_valuer   :   s     05/@4U+JdJ    c              #   b   K   t        |       }	 |  t        |       y# t        |       w xY ww)zA context manager that converts a raw pointer to a *Python objc* value.

    When the block is exited, the value is released.

    :param value: The raw value to wrap.
    N)r   r   )r   wrapped_values     r   _wrappedr   D   s,       &M!- 	- s   / /,/c                   6   e Zd ZdZej
                  j                  ej                  j                  d            Z	g e	j                  _        ej                  e	j                  _        g e	j                  _        ej                  e	j                  _        ej                  ej                  ge	j                  _        ej                  e	j                  _        g e	j                   _        ej"                  e	j                   _        ej                  ej$                  ej$                  ej"                  ej"                  ej"                   ej&                  ej"                        ej(                   ej&                  ej(                        ej$                  dz  g
e	j*                  _        ej"                  e	j*                  _        e	j                  Z
e	j                  Zej                  j-                  e	d      Ze	j                  Ze	j                   ZdZdZe	j*                  Zy)CarbonExtrazWA class exposing some missing functionality from *Carbon* as class
    attributes.
    Carbon    kTISPropertyUnicodeKeyLayoutData   r   N)__name__
__module____qualname____doc__ctypescdllLoadLibraryutilfind_library_Carbon!TISCopyCurrentKeyboardInputSourceargtypesr   restype3TISCopyCurrentASCIICapableKeyboardLayoutInputSourceTISGetInputSourcePropertyLMGetKbdTypec_uint32c_uint16POINTERc_uint8UCKeyTranslatein_dllr$   kUCKeyActionDisplaykUCKeyTranslateNoDeadKeysBit r   r   r!   r!   T   s    kk%%fkk&>&>x&HIG9;G--68>G--5KMG??H ??G 	2*G%%.06G%%-$&G!#)??G  	v'v~~&!
'G# &,__G" 	11 & 	CC 8 (.'='=3(5$ 	))  	  #$  	 r   r!   c               #     K   d\  } }t         j                  t         j                  fD ]  }t         |             5 }t         j	                         } t        t         j                  |t         j                              }|r|j                         j                         nd}||	 ddd        n
ddd        | |f y# 1 sw Y   xY ww)z\Returns an opaque value representing a context for translating keycodes
    to strings.
    )NNN)
r!   r0   r3   r   r5   r   r4   r$   bytestobytes)keyboard_typelayout_datasourcekeyboardlayouts        r   keycode_contextrG      s     
 ",M;99KKM 
 fh 	8'446M !F!F<<"> ?F 7=&,,.002$K#(?	 	 	
 +
&&	 	s   9CA)B?$C?C	Cc                    d}| \  }}t        j                         }t        j                         }t        j                  |z         t        j                  ||t        j                  ||t        j                  t        j                  |      |t        j                  |      
       dj                  fdt        |j                        D              S )z$Converts a keycode to a string.
    r#    c              3   N   K   | ]  }t        j                  |           y wN)sixunichr).0iunicode_strings     r   	<genexpr>z$keycode_to_string.<locals>.<genexpr>   s'      & 	

>!$%&s   "%)r*   r6   r9   r7   r!   r:   r<   r=   byrefjoinranger   )	contextkeycodemodifier_stateLENGTHrB   rC   dead_key_statelengthrP   s	           @r   keycode_to_stringr[      s     F!(M;__&N^^Foo.1N''00^$V
 88 &v||$& & &r   c                      t               5 } t        d      D ci c]  }t        | |      | c}cddd       S c c}w # 1 sw Y   yxY w)zsReturns a mapping from unicode strings to virtual key codes.

    :return: a dict mapping key codes to strings
       N)rG   rT   r[   )rU   rV   s     r   get_unicode_to_keycode_mapr^      sS    
 
	 'g !:' gw/8'' ''' 's   >9>>Ac                   ^    e Zd ZdZ e       ZdZd Zd Zd Z	e
j                  d        Zd Zy)	ListenerMixinzA mixin for *Quartz* event listeners.

    Subclasses should set a value for :attr:`_EVENTS` and implement
    :meth:`_handle_message`.
    Fc                 P   t        j                         | _        | j                  s| j                  j	                  d       d | _        	 | j                         }|| j                          	 d | _        y t        d |d      }t               | _        t        | j
                  |t               t        |d       | j                          	 | j                  r*t        t        dd      }	 |t        k7  rn	 | j                  r*d | _        y # t         $ r Y w xY w#  Y xY w# d | _        w xY w)NztThis process is not trusted! Input event monitoring will not be possible until it is added to accessibility clients.r   Tr   F)
HIServicesAXIsProcessTrusted
IS_TRUSTED_logwarning_loop_create_event_tap_mark_readyr   r   r   r   r   runningr   r   AttributeError)selftaploop_sourceresults       r   _runzListenerMixin._run   s   $779IIJK 
"	((*C{  "< DJ9 8c1K,.DJ

K)>@S$'ll/-q%9F!%::! ;	 ll  DJ *  DJsI   	"D 4AD D &	D /D 	DD DD DD 	D%c                 h    	 | j                   t        | j                          y y # t        $ r Y y w xY wrK   )rg   r   rk   rl   s    r   _stop_platformzListenerMixin._stop_platform   s4    	zz%djj) & 		s   !% 	11c                     t        t        t        	 | j                  s| j                  t
        nt        | j                  | j                  d      S )zSCreates the event tap used by the listener.

        :return: an event tap
        N)	r
   r   r   suppress
_interceptr   r   _EVENTS_handlerrr   s    r   rh   zListenerMixin._create_event_tap  sF    
  !OO+ ( *LLMM
 
	r   c                     t        |t              dk7  }| j                  |||||       | j                  | j                  ||      S | j                  ryy)zThe callback registered with *macOS* for mouse events.

        This method will call the callbacks registered on initialisation.
        r   N)r	   r   _handle_messagerv   ru   )rl   proxy
event_typeeventrefconis_injecteds         r   rx   zListenerMixin._handler  s^     3')-./ 	UJv{K??&??:u55]] r   c                     t               )zThe device specific callback handler.

        This method calls the appropriate callback registered when this
        listener was created based on the event.
        )NotImplementedError)rl   r{   r|   r}   r~   s        r   rz   zListenerMixin._handle_message(  s     "##r   N)r&   r'   r(   r)   tuplerw   rd   rp   rs   rh   r   _emitterrx   rz   r>   r   r   r`   r`      sF     gG J*X"   $r   r`   )r   )$r)   
contextlibr*   ctypes.utilrL   r   rb   CoreFoundationr   Quartzr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   rI   r   r   contextmanagerr   objectr!   rG   r[   r^   r`   r>   r   r   <module>r      s   "    
      $ K ! !4& 4n ' '&&4'j$F j$r   