
    iz<                         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	Z	ddl
mZ ddddZd Zd	 Z G d
 dej                        Z G d de      Z G d de      Zy)z(
General utility functions and classes.
    N)queuezRPlease make sure that you have Python bindings for the system frameworks installedzYPlease make sure that you are running as root, and that the utility dumpkeys is installedznPlease make sure that you have an X server running, and that the DISPLAY environment variable is set correctly)darwinuinputxorgc                    t         j                  j                  dj                  | j	                  d      d   j                               t         j                  j                  dd            }|r|g}n1t        j                  dk(  rdg}nt        j                  dk(  rdg}ndg}g }g }|D ]  }	 t        j                  d	|z   |       c S  t        |rGd
j                  dj                  d |D                    ddj                  d |D              z   z         d      # t        $ r;}|j                  |       |t        v r|j                  t        |          Y d}~d}~ww xY w)zpReturns the backend module for a package.

    :param str package: The package for which to load a backend.
    zPYNPUT_BACKEND_{}.PYNPUT_BACKENDNr   win32r   z._z"this platform is not supported: {}z; c              3   2   K   | ]  }t        |        y wN)str).0es     _/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/pynput/_util/__init__.py	<genexpr>zbackend.<locals>.<genexpr>O   s     )Q#a&)s   z)

Try one of the following resolutions:

z

c              3   >   K   | ]  }d j                  |        yw)z * {}Nformat)r   ss     r   r   zbackend.<locals>.<genexpr>Q   s!      & q!&s    )osenvirongetr   rsplituppersysplatform	importlibimport_moduleImportErrorappendRESOLUTIONSjoin)packagebackend_namemoduleserrorsresolutionsmoduler   s          r   backendr+   3   sc   
 ::>>""7>>##6r#:#@#@#BC


'.0L .		!*		 )(FK 8	8**4&='BB8   ;AA		)&))+/8kk &$& &/&' $ $ !#$ $  	8MM!$"";v#67	8s   /D	E#(1EE#c                      t         fd|j                  dd       D ]?  }| u r'|j                  j                  dd      d   dd dz   c S t	         |      }|=|c S  y)zCalculates the prefix to use for platform specific options for a
    specific class.

    The prefix if the name of the module containing the class that is an
    immediate subclass of ``base`` among the super classes of ``cls``.
    c                     t        |       S r   )
issubclass)clsbases    r   <lambda>zprefix.<locals>.<lambda>_   s    
3-        Nr   r	   _)filter__mro__
__module__r   prefix)r0   r/   	super_clsresults   `   r   r8   r8   W   sr     -KKO 	 >>((a04QR83>>D),F!r2   c                        e Zd ZdZ G d de      Z e       Zd fd	Ze	d        Z
e	d        Zd Zd Zd	 Zd
 Zd Zed        Zd Zd Zd Zd Zd fd	Z xZS )AbstractListenera  A class implementing the basic behaviour for event listeners.

    Instances of this class can be used as context managers. This is equivalent
    to the following code::

        listener.start()
        listener.wait()
        try:
            with_statements()
        finally:
            listener.stop()

    Actual implementations of this class must set the attribute ``_log``, which
    must be an instance of :class:`logging.Logger`.

    :param bool suppress: Whether to suppress events. Setting this to ``True``
        will prevent the input events from being passed to the rest of the
        system.

    :param kwargs: A mapping from callback attribute to callback handler. All
        handlers will be wrapped in a function reading the return value of the
        callback, and if it ``is False``, raising :class:`StopException`.

        Any callback that is falsy will be ignored.
    c                       e Zd ZdZy)AbstractListener.StopExceptionzfIf an event listener callback raises this exception, the current
        listener is stopped.
        N)__name__r7   __qualname____doc__ r2   r   StopExceptionr>      s    	 	r2   rC   c                 \    t         t                    fd}| _        d _        t        j                          _        t        j                          _	        d _
        t        j                  d       _        d _        |j                         D ]  \  }}t!         | ||              y )Nc                       fd}|S )Nc                  4     |  du rj                         y )NF)rC   )argsfselfs    r   innerz9AbstractListener.__init__.<locals>.wrapper.<locals>.inner   s#    d8u$,,.. %r2   rB   )rH   rJ   rI   s   ` r   wrapperz*AbstractListener.__init__.<locals>.wrapper   s    / Lr2   F
   T)superr<   __init__	_suppress_running	threadingcurrent_thread_thread	Condition
_condition_readyr   Queue_queuedaemonitemssetattr)rI   suppresskwargsrK   namecallback	__class__s   `     r   rN   zAbstractListener.__init__   s    .0	 " //1#--/ kk"o$lln 	3ND(D$ 12	3r2   c                     | j                   S )z$Whether to suppress events.
        )rO   rI   s    r   r\   zAbstractListener.suppress   s     ~~r2   c                     | j                   S )z3Whether the listener is currently running.
        )rP   rb   s    r   runningzAbstractListener.running   s     }}r2   c                     | j                   r3d| _         | j                  j                  d       | j                          yy)at  Stops listening for events.

        When this method returns, no more events will be delivered. Once this
        method has been called, the listener instance cannot be used any more,
        since a listener is a :class:`threading.Thread`, and once stopped it
        cannot be restarted.

        To resume listening for event, a new listener must be created.
        FN)rP   rX   put_stop_platformrb   s    r   stopzAbstractListener.stop   s3     ==!DMKKOOD!! r2   c                 F    | j                          | j                          | S r   )startwaitrb   s    r   	__enter__zAbstractListener.__enter__   s    

		r2   c                 $    | j                          y r   )rh   )rI   exc_typevalue	tracebacks       r   __exit__zAbstractListener.__exit__   s    		r2   c                     | j                   j                          | j                  s'| j                   j                          | j                  s'| j                   j	                          y)z1Waits for this listener to become ready.
        N)rU   acquirerV   rk   releaserb   s    r   rk   zAbstractListener.wait   sD     	!++OO  " ++!r2   c                     d| _         t        j                         | _        | j	                          | j
                  j                  d       y)z"The thread runner method.
        TN)rP   rQ   rR   rS   _runrX   rf   rb   s    r   runzAbstractListener.run   s6      //1		 	r2   c                 F     t        j                         fd       }|S )aa  A decorator to mark a method as the one emitting the callbacks.

        This decorator will wrap the method and catch exception. If a
        :class:`StopException` is caught, the listener will be stopped
        gracefully. If any other exception is caught, it will be propagated to
        the thread calling :meth:`join` and reraised there.
        c                    	  | g|i |S # t         $ r}t        || j                        st        |t        j                        s| j
                  j                  d       | j                  j                  t        |j                        rd nt        j                                | j                           d }~ww xY w)Nz(Unhandled exception in listener callback)	Exception
isinstance_HANDLED_EXCEPTIONSr<   rC   _log	exceptionrX   rf   r   exc_inforh   )rI   rG   r]   r   r/   rH   s       r   rJ   z(AbstractListener._emitter.<locals>.inner   s    //// 	!!T%=%=>%a)9)G)GH		++FHKKOO *1c.?.? @ \\^- IIK	s    	B>B!B99B>	functoolswraps)r/   rH   rJ   s   `` r   _emitterzAbstractListener._emitter   s'     
		 
	  r2   c                     | j                   j                          d| _        | j                   j                          | j                   j	                          y)zMarks this listener as ready to receive events.

        This method must be called from :meth:`_run`. :meth:`wait` will block
        until this method is called.
        TN)rU   rs   rV   notifyrt   rb   s    r   _mark_readyzAbstractListener._mark_ready   s;     	! !r2   c                     t               )zlThe implementation of the :meth:`run` method.

        This is a platform dependent implementation.
        NotImplementedErrorrb   s    r   rv   zAbstractListener._run       
 "##r2   c                     t               )zmThe implementation of the :meth:`stop` method.

        This is a platform dependent implementation.
        r   rb   s    r   rg   zAbstractListener._stop_platform  r   r2   c                 (   d S t        j                        }t        t        j                        j                        |j
                  rt        |j
                        nd}|z
  |kD  rt              |k\  s|j                  S fdS )a4  Wraps a callable to make it accept ``args`` number of arguments.

        :param f: The callable to wrap. If this is ``None`` a no-op wrapper is
            returned.

        :param int args: The number of arguments to accept.

        :raises ValueError: if f requires more than ``args`` arguments
        c                       y r   rB   )as    r   r1   z(AbstractListener._wrap.<locals>.<lambda>  s    r2   r   c                       | d   S r   rB   )r   actualrH   s    r   r1   z(AbstractListener._wrap.<locals>.<lambda>#  s    !QwZ. r2   )inspectgetfullargspeclen	signature
parametersdefaults
ValueErrorvarargs)rI   rH   rG   argspecr   r   s    `   @r   _wrapzAbstractListener._wrap  s     9"",,Q/G**1-889F070@0@s7++,aH 4' m#47??#>00r2   c                 \   t        j                          }t        t        |   |g|  |$t	        d|t        j                          |z
  z
        nd }	 | j
                  j                  |      \  }}}t        j                  |||       y # t        j                  $ r Y y t        $ r Y y w xY w)Ng        timeout)timerM   r<   r$   maxrX   r   sixreraiser   Empty	TypeError)rI   r   rG   rj   rn   	exc_valueexc_tracebackr`   s          r   r$   zAbstractListener.join%  s    		*7:T:" c7diikE&9:; 		15 2A 2!.HiKK)];{{ 	 		s   7B B+ B+*B+)Fr   )r?   r7   r@   rA   rz   rC   tupler|   rN   propertyr\   rd   rh   rl   rq   rk   rw   classmethodr   r   rv   rg   r   r$   __classcell__r`   s   @r   r<   r<   i   s    2	   '3.  
  
"
"  6	"$$1. r2   r<   c                   b     e Zd ZdZdZ G d de      Z fdZd Zd Z	d Z
d	 Zdd
Zd Z xZS )Eventsz2A base class to enable iterating over events.
    Nc                       e Zd Zd Zd Zy)Events.Eventc           	          dj                  | j                  j                  dj                  d t	        |       j                         D                    S )Nz{}({})z, c              3   F   K   | ]  \  }}d j                  ||        yw)z{}={}Nr   )r   kvs      r   r   z'Events.Event.__str__.<locals>.<genexpr>B  s'      6A NN1a(6s   !)r   r`   r?   r$   varsrZ   rb   s    r   __str__zEvents.Event.__str__?  sF    ??''		 6"&t*"2"2"46 67 7r2   c                       j                   j                   k(  xr7 t               t              k(  xr t         fdt               D              S )Nc              3   P   K   | ]  }t        |      t        |      k(    y wr   )getattr)r   r   otherrI   s     r   r   z&Events.Event.__eq__.<locals>.<genexpr>I  s,      ( D!$q(99(s   #&)r`   dirall)rI   r   s   ``r   __eq__zEvents.Event.__eq__F  sK    >>U__4 (IU+( ( Y( ((r2   N)r?   r7   r@   r   r   rB   r2   r   Eventr   >  s    	7	(r2   r   c                 L   t         t        |           t        j                         | _        t               | _         | j                  |i |j                         D ci c]  \  }}|| j                  |       c}}| _        | j                  j                  | _        y c c}}w r   )rM   r   rN   r   rW   _event_queueobject	_sentinel	_ListenerrZ   _event_mapper	_listenerrj   )rI   rG   r]   keyro   r`   s        r   rN   zEvents.__init__M  s    fd$&!KKM' 1 &20e ##E**20 1 ^^))
20s   B c                 :    | j                   j                          | S r   )r   rl   rb   s    r   rl   zEvents.__enter__V  s      "r2   c                      | j                   j                  |  	 	 | j                  j                          # t        j
                  $ r Y nw xY w| j                  j                  | j                         y r   )r   rq   r   
get_nowaitr   r   rf   r   )rI   rG   s     r   rq   zEvents.__exit__Z  si    & !!,,.  ;;  	dnn-s   7 AAc                     | S r   rB   rb   s    r   __iter__zEvents.__iter__f  s    r2   c                 >    | j                         }||S t               r   )r   StopIteration)rI   events     r   __next__zEvents.__next__i  s     
L/!r2   c                     	 | j                   j                  |      }|| j                  ur|S dS # t        j                  $ r Y yw xY w)a  Attempts to read the next event.

        :param int timeout: An optional timeout. If this is not provided, this
            method may block infinitely.

        :return: the next event, or ``None`` if the source has been stopped or
            no events were received
        r   N)r   r   r   r   r   )rI   r   r   s      r   r   z
Events.getp  sL    	%%))'):E!75ATA{{ 		s   +0 0 AAc                 F     t        j                         fd       }|S )zGenerates an event callback to transforms the callback arguments to
        an event and then publishes it.

        :param callback event: A function generating an event object.

        :return: a callback
        c                  z    	 j                   j                   |  d       y # t        j                  $ r Y y w xY w)NF)block)r   rf   r   Full)rG   r   rI   s    r   rJ   z#Events._event_mapper.<locals>.inner  s;    !!%%eTl%%@:: s    $ ::r   )rI   r   rJ   s   `` r   r   zEvents._event_mapper  s'     
		 
 	 r2   r   )r?   r7   r@   rA   r   r   r   rN   rl   rq   r   r   r   r   r   r   s   @r   r   r   8  s>     I( (*
."r2   r   c                   V    e Zd ZdZd Zed        Zed        Zed        Zed        Z	y)NotifierMixinzA mixin for notifiers of fake events.

    This mixin can be used for controllers on platforms where sending fake
    events does not cause a listener to receive a notification.
    c                     g }| j                         D ]  }	  t        ||      |   |D ]  }|j	                           y# |j                  $ r |j                  |       Y Ow xY w)a  Sends a notification to all registered listeners.

        This method will ensure that listeners that raise
        :class:`StopException` are stopped.

        :param str action: The name of the notification.

        :param args: The arguments to pass.
        N)
_listenersr   rC   r"   rh   )rI   actionrG   stoppedlisteners        r   _emitzNotifierMixin._emit  sr     ) 	)H))&)40	)
   	HMMO	 )) )x()s   A   A#"A#c                     t         j                  d        }||_        | |_        t	        | d      s(t               | _        t        j                         | _	        |S )ae  A decorator to make a class able to receive fake events from a
        controller.

        This decorator will add the method ``_receive`` to the decorated class.

        This method is a context manager which ensures that all calls to
        :meth:`_emit` will invoke the named method in the listener instance
        while the block is active.
        c              3      K   | j                   j                  |        	 d | j                   j                  |        y# | j                   j                  |        w xY ww)zyExecutes a code block with this listener instance registered as
            a receiver of fake input events.
            N)_controller_class_add_listener_remove_listenerrb   s    r   receivez(NotifierMixin._receiver.<locals>.receive  sL     
 ""006>&&77=&&77=s   A? AAA_listener_cache)

contextlibcontextmanager_receiver   hasattrsetr   rQ   Lock_listener_lock)r/   listener_classr   s      r   	_receiverzNotifierMixin._receiver  sZ     
	"	"	> 
#	> #*+.( s-."%%C!*!1Cr2   c              #      K   | j                   sy| j                  5  | j                   D ]  }|  	 ddd       y# 1 sw Y   yxY ww)a0  Iterates over the set of running listeners.

        This method will quit without acquiring the lock if the set is empty,
        so there is potential for race conditions. This is an optimisation,
        since :class:`Controller` will need to call this method for every
        control event.
        N)r   r   r/   r   s     r   r   zNotifierMixin._listeners  sK      "" 	// 	 	 	s   A<	AAAc                 |    | j                   5  | j                  j                  |       ddd       y# 1 sw Y   yxY w)zqAdds a listener to the set of running listeners.

        :param listener: The listener for fake events.
        N)r   r   addr   s     r   r   zNotifierMixin._add_listener  s6      	.##H-	. 	. 	.   2;c                 |    | j                   5  | j                  j                  |       ddd       y# 1 sw Y   yxY w)zyRemoves this listener from the set of running listeners.

        :param listener: The listener for fake events.
        N)r   r   remover   s     r   r   zNotifierMixin._remove_listener  s6      	1&&x0	1 	1 	1r   N)
r?   r7   r@   rA   r   r   r   r   r   r   rB   r2   r   r   r     s\    
&  >   . . 1 1r2   r   )rA   r   r   r   r   r   r   rQ   r   r   	six.movesr   r#   r+   r8   Threadr<   r   r   r   rB   r2   r   <module>r      s}   "     	 
   
 
&,<=!$H$Ly'' L^VV VrX1F X1r2   