U
    Pe#                     @   s   d Z dZddlZddlmZ dejkr,dZnddlZddlm	Z	m
Z
 ddlmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ dadZdadaG dd deZdd Zdd Z dd Z!dd Z"G dd deZe#de dS )aN  
Auto Create Input Provider Config Entry for Available MT Hardware (linux only).
===============================================================================

Thanks to Marc Tardif for the probing code, taken from scan-for-mt-device.

The device discovery is done by this provider. However, the reading of
input can be performed by other providers like: hidinput, mtdev and
linuxwacom. mtdev is used prior to other providers. For more
information about mtdev, check :py:class:`~kivy.input.providers.mtdev`.

Here is an example of auto creation::

    [input]
    # using mtdev
    device_%(name)s = probesysfs,provider=mtdev
    # using hidinput
    device_%(name)s = probesysfs,provider=hidinput
    # using mtdev with a match on name
    device_%(name)s = probesysfs,provider=mtdev,match=acer

    # using hidinput with custom parameters to hidinput (all on one line)
    %(name)s = probesysfs,
        provider=hidinput,param=min_pressure=1,param=max_pressure=99

    # you can also match your wacom touchscreen
    touch = probesysfs,match=E3 Finger,provider=linuxwacom,
        select_all=1,param=mode=touch
    # and your wacom pen
    pen = probesysfs,match=E3 Pen,provider=linuxwacom,
        select_all=1,param=mode=pen

By default, ProbeSysfs module will enumerate hardware from the /sys/class/input
device, and configure hardware with ABS_MT_POSITION_X capability. But for
example, the wacom screen doesn't support this capability. You can prevent this
behavior by putting select_all=1 in your config line. Add use_mouse=1 to also
include touchscreen hardware that offers core pointer functionality.
)ProbeSysfsHardwareProbe    N)sepZKIVY_DOC)match
IGNORECASE)glob)PopenPIPE)Logger)MotionEventProvider)MouseMotionEventProvider)MotionEventFactory)_is_rpi5   c                   @   sH   e Zd Zdd Zedd Zedd Zdd Zd	d
 Zedd Z	dS )Inputc                 C   s   t   || _d S N)query_xinputpathselfr    r   C/tmp/pip-unpacked-wheel-xzebddm3/kivy/input/providers/probesysfs.py__init__F   s    zInput.__init__c                 C   s   t j| j}t jdd|S )Nz/devinput)osr   basenamejoin)r   baser   r   r   deviceJ   s    zInput.devicec                 C   s   t j| jdd}t|S )Nr   name)r   r   r   	read_liner   r   r   r   r   O   s    z
Input.namec              	      s   t j| jddd}d}zt|}W n ttfk
r@   g  Y S X g }ttjd }t	|
dD ]4\} t d  fdd	t|D }||d d
< qd|S )Nr   capabilitiesabs0       c                    s   g | ]}t  d |> @ qS )   )bool).0iwordr   r   
<listcomp>`   s   z*Input.get_capabilities.<locals>.<listcomp>r   )r   r   r   r   IOErrorOSErrorctypessizeofc_long	enumeratesplitintrange)r   r   liner    Zlong_bitr)   Zsubcapabilitiesr   r*   r   get_capabilitiesT   s    


zInput.get_capabilitiesc                 C   s   |   }t||ko|| S r   )r7   len)r   Z
capabilityr    r   r   r   has_capabilityf   s    zInput.has_capabilityc                 C   s
   | j tkS r   )r   _cache_xinputr   r   r   r   is_mousej   s    zInput.is_mouseN)
__name__
__module____qualname__r   propertyr   r   r7   r9   r<   r   r   r   r   r   D   s   

r   c                  G   s2   zt | td d W S  tk
r,   Y dS X d S )N)stdoutr    )r   r   communicater.   )argsr   r   r   getoutn   s    rE   c               
   C   s   t d krg a tddd} |  D ]}tdd|}d }| D ]^}| }|drd|drdd } q|dr<z|dd	 }W q< tk
r   d }Y q<X q<|r t | q d S )
NZxinputz--listz	--id-onlyz--list-propss   Device Enabled   0s   Device Node"r&   )	r:   rE   
splitlinesstrip
startswithendswithr3   	Exceptionappend)ZdevidsZdidZdevpropsZevpathpropr   r   r   r   t   s(    

r   c                 C   s,   t d kr(tj| d}dd t|D a t S )Nzevent*c                 S   s   g | ]}t |qS r   )r   r(   xr   r   r   r,      s     zget_inputs.<locals>.<listcomp>)_cache_inputr   r   r   r   )r   Z
event_globr   r   r   
get_inputs   s    rR   c                 C   s(   t | }z|  W S |  X d S r   )openclosereadlinerI   )r   fr   r   r   r      s    r   c                       s8   e Zd Z fddZ fddZdd Zdd Z  ZS )	r   c                    s    t t| | }||| d S r   )superr   __new__r   )r   r   rD   instance	__class__r   r   rX      s    zProbeSysfsHardwareProbe.__new__c                    s.  t t| || d| _d | _d| _tr,dnd| _d| _d| _	g | _
|d}|D ]}|dkr`qR|dd}t|d	krtd
|  qR|\}}|dkr|| _qR|dkr|| _qR|dkrtt|| _	qR|dkrtt|| _qR|dkrtt|| _qR|dkr| j
| qRtd|  qRqR|   d S )NZmtdevz/sys/class/inputTF,rB   =r&      z7ProbeSysfs: invalid parameters %s, not key=value formatr   provider	use_regex
select_all	use_mouseparamzProbeSysfs: unknown %s option)rW   r   r   r_   r   
input_pathr   ra   rb   r`   rD   r3   r8   r	   errorr'   r4   rM   probe)r   r   rD   argkeyvaluerZ   r   r   r      sD    

z ProbeSysfsHardwareProbe.__init__c                 C   s   | j ptdd tjD  S )Nc                 s   s   | ]}t |tr|V  qd S r   )
isinstancer   )r(   pr   r   r   	<genexpr>   s    
z;ProbeSysfsHardwareProbe.should_use_mouse.<locals>.<genexpr>)rb   any	EventLoopZinput_providersr;   r   r   r   should_use_mouse   s    z(ProbeSysfsHardwareProbe.should_use_mousec                    sD  ddl ma t| j}td |   | js@ fdd|D }|D ]}td|j|j	f  | j
r| jrt
| j
|jtstd qDn| j
|jkrqDtd|j	  |j	}| j	t|td	 d
 }t| j}|d krtd| j  td td}|d krtd qD||d|j	d| jf }|rDt| qDd S )Nr   )rn   zProbeSysfs: using probesysfs!c                    s$   g | ]}| tr s|js|qS r   )r9   ABS_MT_POSITION_Xr<   rO   rb   r   r   r,      s
    
 z1ProbeSysfsHardwareProbe.probe.<locals>.<listcomp>z"ProbeSysfs: found device: %s at %sz:ProbeSysfs: device not match the rule in config, ignoring.zProbeSysfs: device match: %s)r   z&ProbeSysfs: Unable to find provider %sz ProbeSysfs: fallback on hidinputZhidinputz;ProbeSysfs: no input provider found to handle this device !z%s,%sr\   )Z	kivy.basern   rR   rd   r	   debugro   ra   r   r   r   r`   r   infodictr3   r   r   getr_   criticalr   rD   Zadd_input_provider)r   inputsr   dZ
devicenamer_   rY   r   rq   r   rf      sJ    

 




 
zProbeSysfsHardwareProbe.probe)r=   r>   r?   rX   r   ro   rf   __classcell__r   r   rZ   r   r      s   (r   Z
probesysfs)$__doc____all__r   os.pathr   environr   r/   rer   r   r   
subprocessr   r   Zkivy.loggerr	   Zkivy.input.providerr
   Zkivy.input.providers.mouser   Zkivy.input.factoryr   Zkivy.configr   rn   rp   rQ   r:   objectr   rE   r   rR   r   registerr   r   r   r   <module>   s2   '
*f