
    i34                         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 ddlmZmZmZmZ ddlmZmZ ddlmZ 	 d dlmZ  G d	 d
e      Z G d de      Zy# e$ r	 d dlmZ Y $w xY w)    N)defaultdict)UnionTupleDictSequenceOptional   )_uinputecodesffutil)InputDeviceAbsInfo)
InputEvent)EventIOc                       e Zd Zy)UInputErrorN)__name__
__module____qualname__     V/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/evdev/uinput.pyr   r      s    r   r   c                      e Zd ZdZdZeej                  ej                  fdde	e
e	eeej                  f   f   dee   fd       Zddd	d	d	d
dddej                  j%                  dd      f
deeeee   f      dededededededefdZd Zd Zd Zd Zd Zd Zd+dedefdZd  Zd! Z d" Z!d# Z"d$ Z#d%ed&e
fd'Z$d(ed&e
fd)Z%d&e	e
df   fd*Z&y),UInputzf
    A userland input device and that can inject input events into the
    linux input subsystem.
    )	namevendorproductversionbustypeeventsdevnodefddevice)filtered_typesdevicesr%   c                   g }|D ]7  }t        |t              st        t        |            }|j                  |       9 t	        t
              }d|vr&t        |D cg c]  }|j                   c}      |d<   |D ]<  }|j                         j                         D ]  \  }}||   j                  |        > |D ]
  }	|	|v s||	=   | dd|i|S c c}w )a  
        Create an UInput device with the capabilities of one or more input
        devices.

        Arguments
        ---------
        devices : InputDevice|str
          Varargs of InputDevice instances or paths to input devices.

        filtered_types : Tuple[event type codes]
          Event types to exclude from the capabilities of the uinput device.

        **kwargs
          Keyword arguments to UInput constructor (i.e. name, vendor etc.).
        max_effectsr!   r   )
isinstancer   strappendr   setminff_effects_countcapabilitiesitemsupdate)
clsr%   r&   kwargsdevice_instancesdevall_capabilitiesev_typeev_codesevtypes
             r   from_devicezUInput.from_device*   s    .  	)Cc;/!#c(+##C(	)
 's+&$'IY(Z#)=)=(Z$[F=! $ 	;C%(%5%5%7%=%=%? ;! )00:;	; % 	-F))$V,	- 5*5f55 )[s   CNzpy-evdev-uinputr	      z/dev/uinputFF_MAX_EFFECTS`   r!   r   r   r   r   r    r"   physc           
         || _         || _        || _        || _        || _        || _        || _        |s.t        j                  t        j                  j                         i}| j                          t        j                  |      | _        | j                  |      \  }}t        j                  | j                  |       |	xs g }	|	D ]"  }t        j                   | j                  |       $ |D ]&  \  }}t        j"                  | j                  ||       ( t        j$                  | j                  |||||||
       t        j&                  | j                         t)        j*                  t        j,                        | _        t(        j0                  | j.                  j2                  _        t(        j0                  | j.                  j6                  _        | j9                  | j                        | _        y)a@  
        Arguments
        ---------
        events : dict
          Dictionary of event types mapping to lists of event codes. The
          event types and codes that the uinput device will be able to
          inject - defaults to all key codes.

        name
          The name of the input device.

        vendor
          Vendor identifier.

        product
          Product identifier.

        version
          Version identifier.

        bustype
          Bustype identifier.

        phys
          Physical path.

        input_props
          Input properties and quirks.

        max_effects
          Maximum simultaneous force-feedback effects.

        Note
        ----
        If you do not specify any events, the uinput device will be able
        to inject only ``KEY_*`` and ``BTN_*`` event codes.
        N)r   r   r   r   r    r>   r"   r   EV_KEYkeys_verifyr
   openr#   _prepare_eventsset_physset_propenablesetupcreatectypesCDLL__file__dllc_int_uinput_begin_uploadrestype_uinput_end_upload_find_devicer$   )selfr!   r   r   r   r   r    r"   r>   input_propsr(   absinfoprepared_eventspropetypecodes                   r   __init__zUInput.__init__W   s   l 	!###	#mmV[[%5%5%78F ,,w' $(#7#7#?  	$' "'R 	,DTWWd+	, + 	1KE4NN477E40	1 	dggtVWgwQ\] 	tww;;w//006%%-.4ll##+
 $(#4#4TWW#=r   c           
      T   g g }}|j                         D ]  \  }}|D ]  }t        |t        t        t        f      rT|d   g}|j                  |d          |j                  dgdt        |d         z
  z         |j                  |       |d   }|j                  ||f         ||fS )z>Prepare events for passing to _uinput.enable and _uinput.setupr   r	      )r0   r)   tuplelistr   extendlenr+   )rS   r!   rU   rV   rX   codesrY   fs           r   rD   zUInput._prepare_events   s    #%r"LLN 	6LE5 6dUD'$:;a	AHHT!W% HHaSADG$456NN1%7D&&t}56	6 ''r   c                     | S Nr   rS   s    r   	__enter__zUInput.__enter__   s    r   c                 >    t        | d      r| j                          y y )Nr#   )hasattrclose)rS   typevaluetbs       r   __exit__zUInput.__exit__   s    4JJL r   c                       fddD        }dj                   j                  j                  dj                  |            S )Nc              3   H   K   | ]  }t        t        |              y wrd   )reprgetattr).0irS   s     r   	<genexpr>z"UInput.__repr__.<locals>.<genexpr>   s     iT'$"#is   ")r   r    r   r   r   r>   z{}({})z, )format	__class__r   join)rS   vs   ` r   __repr__zUInput.__repr__   s1    i-hit~~66		!EEr   c                 X   d}| j                  d      j                         D cg c]  }|d   	 }}|j                  | j                  t        j
                  | j                     | j                  | j                  | j                  | j                  dj                  |            }|S c c}w )Nzcname "{}", bus "{}", vendor "{:04x}", product "{:04x}", version "{:04x}", phys "{}"
event types: {}Tr    )r/   rA   ru   r   r   BUSr    r   r   r   r>   rw   )rS   msgrs   evtypess       r   __str__zUInput.__str__   s    t!%!2!24!8!=!=!?@A1Q4@@jjIIvzz$,,/dllDLLZ^ZcZcehememnuev
 
 As   B'c                     | j                   | j                   j                          | j                  dkD  r't        j                  | j                         d| _        y y )N)r$   ri   r#   r
   re   s    r   ri   zUInput.close   sE    ;;"KK 77R<MM$''"DG r   verboserU   c                 h    | j                   t        d      | j                   j                  ||      S )zASee :func:`capabilities <evdev.device.InputDevice.capabilities>`.z2input device not opened - cannot read capabilities)r$   r   r/   )rS   r   rU   s      r   r/   zUInput.capabilities   s/    ;;RSS{{''99r   c                     t        j                         }||_        | j                  j	                  | j
                  t        j                  |            }|r!t        dt        j                  |      z         |S )NzFailed to begin uinput upload: )r   UInputUpload	effect_idrM   rO   r#   rJ   byrefr   osstrerror)rS   r   uploadrets       r   begin_uploadzUInput.begin_upload   s[    "$hh++DGGV\\&5IJ?"++cBRRSSr   c                     | j                   j                  | j                  t        j                  |            }|r!t        dt        j                  |      z         y )NzFailed to end uinput upload: )rM   rQ   r#   rJ   r   r   r   r   )rS   r   r   s      r   
end_uploadzUInput.end_upload   sG    hh))$''6<<3GH=C@PPQQ r   c                     t        j                         }||_        | j                  j	                  | j
                  t        j                  |            }|r!t        dt        j                  |      z         |S )NzFailed to begin uinput erase: )r   UInputEraser   rM   _uinput_begin_eraser#   rJ   r   r   r   r   )rS   r   eraser   s       r   begin_erasezUInput.begin_erase  sZ     #hh**477FLL4GH>SAQQRRr   c                     | j                   j                  | j                  t        j                  |            }|r!t        dt        j                  |      z         y )NzFailed to end uinput erase: )rM   _uinput_end_eraser#   rJ   r   r   r   r   )rS   r   r   s      r   	end_erasezUInput.end_erase
  sG    hh((&,,u2EF<r{{3?OOPP r   c                 Z   	 t        j                  | j                        t        j                     }t        j                  |      sJ 	 t        j                  | j                  t         j                        s&d}t        |j                  | j                              t        | j                        t        j                  kD  r*d}t        |j                  t        j                              y# t
        t        t        f$ r' d}t        |j                  | j                              w xY w)zr
        Verify that an uinput device exists and is readable and writable
        by the current process.
        z_"{}" does not exist or is not a character device file - verify that the uinput module is loadedz!"{}" cannot be opened for writingz8uinput device name must not be longer than {} charactersN)r   statr"   ST_MODES_ISCHR
IndexErrorOSErrorAssertionErrorr   ru   accessW_OKr`   r   r
   
maxnamelen)rS   mr}   s      r   rB   zUInput._verify  s    
	8%dll3A<<?"?
 yyrww/5Ccjj677tyy>G...LCcjj););<== / G^4 	8sCcjj677	8s   AC/ /;D*r#   returnc                     t        j                         dk(  r'	 t        j                  |      }| j	                  |      S | j                         S # t
        $ r Y | j                         S w xY w)z
        Tries to find the device node. Will delegate this task to one of
        several platform-specific functions.
        Linux)platformsystemr
   get_sysname_find_device_linuxr   _find_device_fallback)rS   r#   sysnames      r   rR   zUInput._find_device#  sq    
 ??'!--b1..w77 ))++    ))++s   %A 	A*)A*r   c                 R   d| }t        j                  d      }t        j                  |      D ]  }|j	                  |      sd| } n t               t        d      D ]  }	 t        |      c S  t        |      S # t
        t        f$ r t        j                  d       Y Cw xY w)zF
        Tries to find the device node when running on Linux.
        z/sys/devices/virtual/input/zevent[0-9]+/dev/input/   皙?)recompiler   listdir	fullmatchFileNotFoundErrorranger   PermissionErrortimesleep)rS   r   syspathregexentrydevice_pathattempts          r   r   zUInput._find_device_linux5  s    
 0y9
 

=)ZZ( 	&Eu% +E73	&
 $%% Ry 	 G ";//	  ;'' &7  

3 s   &
A??$B&%B&c                    t        j                  d       g }t        j                  d      }t	        j
                  d      D ]7  }|j                  |      }|st        |d         }|j                  ||f       9 |j                  d d       |D ]-  \  }}t        |      }|j                  | j                  k(  s+|c S  y)	z
        Tries to find the device node when UI_GET_SYSNAME is not available or
        we're running on a system sufficiently exotic that we do not know how
        to interpret its return value.
        r   z/dev/input/event([0-9]+)r   r	   c                     | d   S )Nr	   r   )pairs    r   <lambda>z.UInput._find_device_fallback.<locals>.<lambda>r  s
    Q r   T)keyreverseN)r   r   r   r   r   list_devicesr   intr+   sortr   r   )rS   path_number_pairsr   pathregex_matchdevice_number_ds           r   r   zUInput._find_device_fallbackY  s     	

3 

56%%m4 	<D//$/KA/M$$dM%:;	< 	#7F( 	GD!D!Avv"	r   )FT)'r   r   r   __doc__	__slots__classmethodr   EV_SYNEV_FFr   r   r*   bytesr   PathLiker   r   r:   getr   r   r   rZ   rD   rf   rm   ry   r   ri   boolr/   r   r   r   r   rB   rR   r   r   r   r   r   r   r      s   

I  '-mmV\\%B*6U3r{{+B%CCD*6 c
*6 *6\ 6:%$% MM%%&6;`>c8C=012`> `> 	`>
 `> `> `> `> `>D($F
:D :4 :R
Q
>(,s ,{ ,$"(# "(+ "(Hu[$->'? r   r   )rJ   r   r   r   r   r   collectionsr   typingr   r   r   r   r    r
   r   r   r   r$   r   r   r!   r   evdev.eventio_asyncr   ImportErrorevdev.eventio	Exceptionr   r   r   r   r   <module>r      sb     	  	   # 9 9 ' ' ( &+
	) 	_W _  &%&s   A% %A32A3