
    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Zddl	Zddl
mZ ddlmZ d Z e        [ G d de      Zej"                  d	        Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z G d de       Z!y)z7
Utility functions and classes for the *Xorg* backend.
    N   )AbstractListener)SYMBOLSc                      t         j                  j                         } | j                          t         j                  j
                  D ]!  }t         j                  j                  |       # y N)XlibdisplayDisplayclose	keysymdef__all__XKload_keysym_group)r	   groups     [/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/pynput/_util/xorg.py_check_and_initializer   &   sH    ll""$GMMO'' )!!%()    c                       e Zd ZdZy)X11Errorz|An error that is thrown at the end of a code block managed by a
    :func:`display_manager` if an *X11* error occurred.
    N)__name__
__module____qualname____doc__ r   r   r   r   0   s     	r   r   c              #      K   g fd}| j                  |      }	 |  | j                          | j                  |       rt              y# | j                  |       w xY ww)a6  Traps *X* errors and raises an :class:``X11Error`` at the end if any
    error occurred.

    This handler also ensures that the :class:`Xlib.display.Display` being
    managed is sync'd.

    :param Xlib.display.Display display: The *X* display.

    :return: the display
    :rtype: Xlib.display.Display
    c                  (    j                  |        y)z"The *Xlib* error handler.
        N)append)argserrorss    r   handlerz display_manager.<locals>.handlerF   s     	dr   N)set_error_handlersyncr   )r	   r    old_handlerr   s      @r   display_managerr$   7   sj      F
 ++G4K/!!+.v  	!!+.s   A&A A&A##A&c                     | j                  t        j                  j                  |            }t	        | j                               D ]  \  }}|D ]  }||k(  s	d|z  c c S   y)zReturns the mode flags to use for a modifier symbol.

    :param Xlib.display.Display display: The *X* display.

    :param str symbol: The name of the symbol.

    :return: the modifier mask
    r   r   )keysym_to_keycoder   r   string_to_keysym	enumerateget_modifier_mapping)r	   symbolmodifier_keycodeindexkeycodeskeycodes         r   
_find_maskr/   U   sq     00  (* %W%A%A%CD "x 	"G**Ez!	""
 r   c                 T    t        | d      st        | d      | _        | j                  S )   Returns the *alt* mask flags.

    The first time this function is called for a display, the value is cached.
    Subsequent calls will return the cached value.

    :param Xlib.display.Display display: The *X* display.

    :return: the modifier mask
    
__alt_maskAlt_L)hasattrr/   r2   r	   s    r   alt_maskr6   j   s)     7L)'9r   c                 T    t        | d      st        | d      | _        | j                  S )r1   __altgr_maskMode_switch)r4   r/   r8   r5   s    r   alt_gr_maskr:   y   s)     7N+)'=Ar   c                 T    t        | d      st        | d      | _        | j                  S )a  Returns the *numlock* mask flags.

    The first time this function is called for a display, the value is cached.
    Subsequent calls will return the cached value.

    :param Xlib.display.Display display: The *X* display.

    :return: the modifier mask
    __numlock_maskNum_Lock)r4   r/   r<   r5   s    r   numlock_maskr>      s*     7,-!+GZ!@!!!r   c                     t         j                  j                  | cxk  xr t         j                  j                  k  S c S )zDetermines whether a *keysym* is an upper case *latin* character.

    This is true only if ``XK_A`` <= ``keysym`` <= ` XK_Z``.

    :param in keysym: The *keysym* to check.
    )r   r   XK_AXK_Zkeysyms    r   keysym_is_latin_upperrD      *     77<<61TWW\\1111r   c                     t         j                  j                  | cxk  xr t         j                  j                  k  S c S )zDetermines whether a *keysym* is a lower case *latin* character.

    This is true only if ``XK_a`` <= ``keysym`` <= ` XK_z``.

    :param in keysym: The *keysym* to check.
    )r   r   XK_aXK_zrB   s    r   keysym_is_latin_lowerrI      rE   r   c                 `   |t         j                  j                  k(  rt        |       r:t         j                  j                  | z   t         j                  j
                  z
  | fS t        |       r:| t         j                  j
                  | z   t         j                  j                  z
  fS | | fS | |fS )a  Generates a group from two *keysyms*.

    The implementation of this function comes from:

        Within each group, if the second element of the group is ``NoSymbol``,
        then the group should be treated as if the second element were the same
        as the first element, except when the first element is an alphabetic
        *KeySym* ``K`` for which both lowercase and uppercase forms are
        defined.

        In that case, the group should be treated as if the first element were
        the lowercase form of ``K`` and the second element were the uppercase
        form of ``K``.

    This function assumes that *alphabetic* means *latin*; this assumption
    appears to be consistent with observations of the return values from
    ``XGetKeyboardMapping``.

    :param ks1: The first *keysym*.

    :param ks2: The second *keysym*.

    :return: a tuple conforming to the description above
    )r   r   NoSymbolrD   rG   r@   rI   )ks1ks2s     r   keysym_grouprN      s    2 dgg %GGLL3&5s;;"3'+dggll:;;:Szr   c                    t        t        t        t        j                  d t        |                               }|syt	        |      dk(  rNt        |d   t        j                  j                        t        |d   t        j                  j                        fS t	        |      dk(  r$t        |d   |d         t        |d   |d         fS t	        |      dk(  r9t        |d   |d         t        |d   t        j                  j                        fS t	        |      dk\  r$t        |d   |d         t        |d   |d	         fS t        |d   |d         t        |d   |d         fS )
a+  Normalises a list of *keysyms*.

    The implementation of this function comes from:

        If the list (ignoring trailing ``NoSymbol`` entries) is a single
        *KeySym* ``K``, then the list is treated as if it were the list
        ``K NoSymbol K NoSymbol``.

        If the list (ignoring trailing ``NoSymbol`` entries) is a pair of
        *KeySyms* ``K1 K2``, then the list is treated as if it were the list
        ``K1 K2 K1 K2``.

        If the list (ignoring trailing ``NoSymbol`` entries) is a triple of
        *KeySyms* ``K1 K2 K3``, then the list is treated as if it were the list
        ``K1 K2 K3 NoSymbol``.

    This function will also group the *keysyms* using :func:`keysym_group`.

    :param keysyms: A list of keysyms.

    :return: the tuple ``(group_1, group_2)`` or ``None``
    c                 <    | t         j                  j                  k(  S r   )r   r   rK   )ns    r   <lambda>z"keysym_normalize.<locals>.<lambda>   s    a477+++ r   Nr   r                  )	listreversed	itertools	dropwhilelenrN   r   r   rK   )rC   strippeds     r   keysym_normalizer^      so   0 HT+V	   !H
 	X!	!dgg&6&67!dgg&6&679 	9 
X!	!hqk2!hqk24 	4 
X!	!hqk2!dgg&6&679 	9 
X!	 !hqk2!hqk24 	4 !hqk2!hqk24 	4r   c                 >    |dz  rdnd|dz  rt        |       z  S dz  S a
  Converts an index in a *key code* list to the corresponding shift state.

    :param Xlib.display.Display display: The display for which to retrieve the
        shift mask.

    :param int index: The keyboard mapping *key code* index.

    :return: a shift mask
    r   r   rS   r:   )r	   r,   s     r   index_to_shiftrb     s4     19!!&W		3401	34r   c                 >    |dz  rdnd|t        |       z  rdz   S dz   S r`   ra   )r	   shifts     r   shift_to_indexre     s5     aiQk'**	3401	34r   c                    i }d}t        |       }| j                  j                  j                  }| j                  j                  j                  |z
  dz   }t        | j                  ||            D ]j  \  }}||z   }t        |      }	|	st        |	d      D ]C  \  }
}t        |
d      D ]/  \  }}|s	d|r|ndz  |r|ndz  }||v r||   d   |k  r)||f||<   1 E l |S )zGenerates a mapping from *keysyms* to *key codes* and required
    modifier shift states.

    :param Xlib.display.Display display: The display for which to retrieve the
        keyboard mapping.

    :return: the keyboard mapping
    r   )FTr   )	r:   r	   infomin_keycodemax_keycoder(   get_keyboard_mappingr^   zip)r	   mapping
shift_mask
group_maskrh   keycode_countr,   keysymskey_code
normalizedgroupsr   rC   rd   shift_states                  r   keyboard_mappingru   )  s    GJW%J //&&22KOO((44{BQFM#G$@$@%( ) :w;& &g.
 !]; 	:MFE!$V]!; 
:%*z3%*z3
 W$);k)I#+["9
:	::. Nr   c                 0    t        |       }|dk  r|S |dz  S )zConverts a unicode character to a *keysym*.

    :param str char: The unicode character.

    :return: the corresponding *keysym*, or ``0`` if it cannot be found
       i   )ord)charordinals     r   char_to_keysymr{   T  s$     $iG##r   c                     	 t         j                  j                  |       xsC t        t         j                  j
                  d| z   d      xs t        j                  | d      d   S )zConverts a symbol name to a *keysym*.

    :param str symbol: The name of the symbol.

    :return: the corresponding *keysym*, or ``0`` if it cannot be found
    r   XK_)r   )r   r   r'   getattrr   xkbr   get)r*   s    r   symbol_to_keysymr   b  sW    (77##F+(4>>%%uv~q9( ;;vt$Q')r   c                       e Zd ZdZ e       Zej                  j                  j                  d      Z
d Zd Zd Zd Zed        Zej$                  d        Zd	 Zd
 Zy)ListenerMixinzA mixin for *X* event listeners.

    Subclasses should set a value for :attr:`_EVENTS` and implement
    :meth:`_handle_message`.
    Nc                 r   t         j                  j                         | _        t         j                  j                         | _        d| _        t        | j                        5 }|j                  dt         j                  j                  j                  gddddd| j                  dddd	g      | _        d d d        	 | j                  | j                         | j                          | j                  r/t        | j                        5 }| j!                  |       d d d        | j                  j#                  | j                  | j$                         | j                  r/t        | j                        5 }| j'                  |       d d d        | j                  j)                  | j                         | j                  j+                          | j                  j-                  | j                         | j                  j/                          | j                  j/                          y # 1 sw Y   uxY w# 1 sw Y   xY w#  Y xY w# 1 sw Y   xY w# | j                  r<t        | j                        5 }| j'                  |       d d d        n# 1 sw Y   nxY w| j                  j)                  | j                         | j                  j+                          | j                  j-                  | j                         | j                  j/                          | j                  j/                          w xY w)NFr   )r   r   )r   r   r   r   )	core_requestscore_repliesext_requestsext_repliesdelivered_eventsdevice_eventsr   client_startedclient_died)r   r	   r
   _display_stop_display_record_stoppedr$   record_create_contextextrecord
AllClients_EVENTS_context_initialize_mark_readysuppress_suppress_startrecord_enable_context_handler_suppress_stoprecord_disable_contextflushrecord_free_contextr   )selfdms     r   _runzListenerMixin._run}  sy   !\\113#||335T112 	,b44++,%+$*$0#/(.%)\\$&+#(	* 	+,DM	, 	)T//0}}$T%7%78 -B((,-  66t}}. }}$T%7%78 ,B''+,55dmmD$$&  44T]]C$$&  &&(G	, 	,(- -	, , }}$T%7%78 ,B''+, , ,55dmmD$$&  44T]]C$$&  &&(sc   #AH'=AI 	H48I 4I'H14H>9I II I"L66J	L6JB L6c                     t        | d      s| j                          | j                  j                  | j                         y )Nr   )r4   waitr   r   r   r   s    r   _stop_platformzListenerMixin._stop_platform  s/    tZ(IIK 	33DMMBr   c                     t               zStarts suppressing events.

        :param Xlib.display.Display display: The display for which to suppress
            events.
        NotImplementedErrorr   r	   s     r   r   zListenerMixin._suppress_start       "##r   c                     t               r   r   r   s     r   r   zListenerMixin._suppress_stop  r   r   c                 `    t        j                  t        j                  | j                  d      S )zThe event mask.
        r   )	functoolsreduceoperator__or__r   r   s    r   _event_maskzListenerMixin._event_mask  s!     qAAr   c                 L   | j                   s| j                         |j                  }|rzt        |      rn| j                  j                  || j                  j                  dd      \  }}|j                  }| j                  | j                  ||       |rt        |      rlyyyy)a  The callback registered with *X* for mouse events.

        This method will parse the response and call the callbacks registered
        on initialisation.

        :param events: The events passed by *X*. This is a binary block
            parsable by :attr:`_EVENT_PARSER`.
        N)runningStopExceptiondatar\   _EVENT_PARSERparse_binary_valuer   r	   
send_event_handle_messager   )r   eventsr   eventinjecteds        r   r   zListenerMixin._handler  s     ||$$&&{{s4y,,??d**22D$@KE4 ''H  !3!3UHE s4ydydr   c                      y)zInitialises this listener.

        This method is called immediately before the event loop, from the
        handler thread.

        :param display: The display being used.
        Nr   r   s     r   r   zListenerMixin._initialize  s     	r   c                      y)a:  The device specific callback handler.

        This method calls the appropriate callback registered when this
        listener was created based on the event.

        :param display: The display being used.

        :param event: The event.

        :param bool injected: Whether the event was injected.
        Nr   )r   r	   r   r   s       r   r   zListenerMixin._handle_message  s     	r   )r   r   r   r   tupler   r   protocolrq
EventFieldr   r   r   r   r   propertyr   r   _emitterr   r   r   r   r   r   r   r   q  s~     gG MM$$//5M')TC$$ B B
 F F*r   r   )"r   
contextlibr   rZ   r   Xlib.displayr   Xlib.keysymdefXlib.threadedXlib.XK r   xorg_keysymsr   r   	Exceptionr   contextmanagerr$   r/   r6   r:   r>   rD   rI   rN   r^   rb   re   ru   r{   r   objectr   r   r   r   <module>r      s   "          !)  	y 	  :* "22!H94x44(V$)F r   