m
zAc           @   s   d  Z  d k Z d k Z d k Z d k Z d k l Z d d g Z d   Z d f  d     YZ	 e
 d d	 @ Z e
 d
 d	 @ Z e
 d d	 @ Z e
 d d	 @ Z d e	 f d     YZ d f  d     YZ d f  d     YZ e d j o e   n d S(   s   evdev.py

This is a Python interface to the Linux input system's event device.
Events can be read from an open event file and decoded into spiffy
python objects. The Event objects can optionally be fed into a Device
object that represents the complete state of the device being monitored.

Copyright (C) 2003-2004 Micah Dowty <micah@navi.cx>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
N(   s   ioctlt   Eventt   Devicec          C   s;   t  t i d  }  x! |  i   t i d  |  GHq Wd S(   s   Open the event device named on the command line, use incoming
       events to update a device, and show the state of this device.
       i   f0.10000000000000001N(   R   t   syst   argvt   devt   pollt   timet   sleep(   R   (    (    t   evdev.pyt   demo   s       
t
   BaseDevicec           B   sM   t  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 RS(   s   Base class representing the state of an input device, with axes and buttons.
       Event instances can be fed into the Device to update its state.
       c         C   s   h  |  _ h  |  _ d  |  _ d  S(   N(   t   selft   axest   buttonst   Nonet   name(   R   (    (    R   t   __init__.   s    		c         C   s   d |  i |  i |  i f S(   Ns#   <Device name=%r axes=%r buttons=%r>(   R   R   R   R   (   R   (    (    R   t   __repr__3   s    c         C   s2   t  |  d | i d   } | o | |  n d  S(   Ns	   update_%s(   t   getattrR   t   eventt   typeR   t   f(   R   R   R   (    (    R   t   update7   s    c         C   s   | i |  i | i <d  S(   N(   R   t   valueR   R   t   code(   R   R   (    (    R   t   update_EV_KEY<   s    c         C   s   | i |  i | i <d  S(   N(   R   R   R   R   R   (   R   R   (    (    R   t   update_EV_ABS?   s    c         C   s-   |  i i | i d  | i |  i | i <d  S(   Ni    (   R   R   t   getR   R   R   (   R   R   (    (    R   t   update_EV_RELB   s    c         C   s6   | |  i j o |  i | Sn |  i i | d  Sd S(   sx   Retrieve the current value of an axis or button,
           or zero if no data has been received for it yet.
           i    N(   R   R   R   R   R   (   R   R   (    (    R   t   __getitem__E   s     (
   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R   (    (    (    R   R
   *   s    						lE  l    lE  l!E lAE(  c           B   s2   t  Z d  Z d   Z d   Z d   Z d   Z RS(   s>   An abstract input device attached to a Linux evdev device nodec         C   sO   t  i |   t i | t i t i B |  _ t	 i
 t i  |  _ |  i   d  S(   N(   R
   R   R   t   ost   opent   filenamet   O_RDWRt
   O_NONBLOCKt   fdt   structt   calcsizeR    t   formatt
   packetSizet   readMetadata(   R   R#   (    (    R   R   Y   s    c         C   sX   xQ y t  i |  i |  i  } Wn t j
 o d Sn X|  i t d |   q Wd S(   s.   Receive and process all available input eventsi   Nt   unpack(	   R!   t   readR   R&   R*   t   buffert   OSErrorR   R    (   R   R.   (    (    R   R   _   s       	c         C   s   d d } t |  i t |  |  _ |  i |  i i d   |  _ t i d } d t
 i d  } h  |  _ xd | i i   D]S \ } } t
 i d t |  i t | |   } t t d |   } | |  i | <qw Wd
 S(   s1   Read device identity and capabilities via ioctl()t    i   t   EV_ABSt   iiiiiR   t   mint   maxt   fuzzt   flatN(   s   values   mins   maxR5   R6   (   R.   t   ioctlR   R&   t   EVIOCGNAME_512R   t   findR    t   codeMapst   absmapR'   R(   t   absAxisInfot   nameMapt	   iteritemst   numberR,   t   EVIOCGABS_512t   valuest   dictt   zip(   R   R.   R?   R;   RA   R   (    (    R   R+   h   s     
	 %c         C   sq   y |  i | i } Wn t j
 o d Sn Xt | d | d  } | i | d | d d |  i	 | i <d S(   s@   Scale the absolute axis into the range [-1, 1] using absAxisInfoNR4   R3   f2.0f1.0(
   R   R<   R   R   t   infot   KeyErrort   floatt   rangeR   R   (   R   R   RD   RG   (    (    R   R   y   s     	(   R   R   R    R   R   R+   R   (    (    (    R   R   W   s
    				t   EnumDictc           B   s)   t  Z d  Z d   Z d   Z d   Z RS(   s   A 1:1 mapping from numbers to strings or other objects, for enumerated
       types and other assigned numbers. The mapping can be queried in either
       direction. All values, by default, map to themselves.
       c         C   s@   | |  _  h  |  _ x' | i   D] \ } } | |  i | <q Wd  S(   N(   t	   numberMapR   R=   R>   t   keyR   (   R   RI   R   RJ   (    (    R   R      s
    		 c         C   s   |  i i | |  S(   N(   R   R=   R   R   (   R   R   (    (    R   t   toNumber   s    c         C   s   |  i i | |  S(   N(   R   RI   R   t   num(   R   RL   (    (    R   t
   fromNumber   s    (   R   R   R    R   RK   RM   (    (    (    R   RH      s    		c           B   s  t  Z d  Z d Z e h  d d <d d <d d <d d	 <d
 d <d d <d d <d d <d d < Z h  d e h  d d <d d <d d <d d <d
 d <d d <d d <d d <d d  <d! d" <d# d$ <d% d& <d' d( <d) d* <d+ d, <d- d. <d/ d0 <d d1 <d d2 <d3 d4 <d d5 <d d6 <d7 d8 <d9 d: <d; d< <d= d> <d? d@ <dA dB <dC dD <dE dF <dG dH <dI dJ <dK dL <dM dN <dO dP <dQ dR <dS dT <dU dV <dW dX <dY dZ <d[ d\ <d] d^ <d_ d` <da db <dc dd <de df <dg dh <di dj <dk dl <dm dn <do dp <dq dr <ds dt <du dv <dw dx <dy dz <d{ d| <d} d~ <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <d d <dd<dd<dd<dd<d	d
<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd <d!d"<d#d$<d%d&<d'd(<d)d*<d+d,<d-d.<d/d0<d1d2<d3d4<d5d6<d7d8<d9d:<d;d<<d=d><d?d@<dAdB<dCdD<dEdF<dGdH<dIdJ<dKdL<dMdN<dOdP<dQdR<dSdT<dUdV<dWdX<dYdZ<d[d\<d]d^<d_d`<dadb<dcdd<dedf<dgdh<didj<dkdl<dmdn<dodp<dqdr<dsdt<dudv<dwdx<dydz<d{d|<d}d~<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd<dd <dd<dd<dd<dd<d	d
<dd<dd<dd<dd<dd<dd<dd<dd< <d e h  d d<d d<d d<d d<d d<d d <d! d!< <d	 e h  d d"<d d#<d d$<d d%<d
 d&<d d'<d d(<d d)<d d*<d! d+<d# d,<d/ d-<d d.<d d/<d3 d0<d d1<d d2<d7 d3<d9 d4<d; d5<d= d6<d? d7<dA d8<dC d9< <d e h  d d:<d d;< <d e h  d d<<d d=<d d><d d?<d
 d@<d dA<d dB<d dC<d dD< <d e h  d dE<d dF< <d e h  d dG<d dH< <Z d d d d e e dI Z dJ  Z	 dK  Z
 dL  Z dM  Z RS(N  s   Represents one linux input system event. It can
       be encoded and decoded in the 'struct input_event'
       format used by the kernel. Types and codes are automatically
       encoded and decoded with the #define names used in input.h
       t   LLHHli    t   EV_RSTi   t   EV_KEYi   t   EV_RELi   R1   i   t   EV_MSCi   t   EV_LEDi   t   EV_SNDi   t   EV_REPi   t   EV_FFt   KEY_RESERVEDt   KEY_ESCt   KEY_1t   KEY_2t   KEY_3i   t   KEY_4i   t   KEY_5i   t   KEY_6i   t   KEY_7i	   t   KEY_8i
   t   KEY_9i   t   KEY_0i   t	   KEY_MINUSi   t	   KEY_EQUALi   t   KEY_BACKSPACEi   t   KEY_TABi   t   KEY_Qt   KEY_Wt   KEY_Ei   t   KEY_Rt   KEY_Tt   KEY_Yi   t   KEY_Ui   t   KEY_Ii   t   KEY_Oi   t   KEY_Pi   t   KEY_LEFTBRACEi   t   KEY_RIGHTBRACEi   t	   KEY_ENTERi   t   KEY_LEFTCTRLi   t   KEY_Ai   t   KEY_Si    t   KEY_Di!   t   KEY_Fi"   t   KEY_Gi#   t   KEY_Hi$   t   KEY_Ji%   t   KEY_Ki&   t   KEY_Li'   t   KEY_SEMICOLONi(   t   KEY_APOSTROPHEi)   t	   KEY_GRAVEi*   t   KEY_LEFTSHIFTi+   t   KEY_BACKSLASHi,   t   KEY_Zi-   t   KEY_Xi.   t   KEY_Ci/   t   KEY_Vi0   t   KEY_Bi1   t   KEY_Ni2   t   KEY_Mi3   t	   KEY_COMMAi4   t   KEY_DOTi5   t	   KEY_SLASHi6   t   KEY_RIGHTSHIFTi7   t   KEY_KPASTERISKi8   t   KEY_LEFTALTi9   t	   KEY_SPACEi:   t   KEY_CAPSLOCKi;   t   KEY_F1i<   t   KEY_F2i=   t   KEY_F3i>   t   KEY_F4i?   t   KEY_F5i@   t   KEY_F6iA   t   KEY_F7iB   t   KEY_F8iC   t   KEY_F9iD   t   KEY_F10iE   t   KEY_NUMLOCKiF   t   KEY_SCROLLLOCKiG   t   KEY_KP7iH   t   KEY_KP8iI   t   KEY_KP9iJ   t   KEY_KPMINUSiK   t   KEY_KP4iL   t   KEY_KP5iM   t   KEY_KP6iN   t
   KEY_KPPLUSiO   t   KEY_KP1iP   t   KEY_KP2iQ   t   KEY_KP3iR   t   KEY_KP0iS   t	   KEY_KPDOTiT   t	   KEY_103RDiU   t   KEY_F13iV   t	   KEY_102NDiW   t   KEY_F11iX   t   KEY_F12iY   t   KEY_F14iZ   t   KEY_F15i[   t   KEY_F16i\   t   KEY_F17i]   t   KEY_F18i^   t   KEY_F19i_   t   KEY_F20i`   t   KEY_KPENTERia   t   KEY_RIGHTCTRLib   t   KEY_KPSLASHic   t	   KEY_SYSRQid   t   KEY_RIGHTALTie   t   KEY_LINEFEEDif   t   KEY_HOMEig   t   KEY_UPih   t
   KEY_PAGEUPii   t   KEY_LEFTij   t	   KEY_RIGHTik   t   KEY_ENDil   t   KEY_DOWNim   t   KEY_PAGEDOWNin   t
   KEY_INSERTio   t
   KEY_DELETEip   t	   KEY_MACROiq   t   KEY_MUTEir   t   KEY_VOLUMEDOWNis   t   KEY_VOLUMEUPit   t	   KEY_POWERiu   t   KEY_KPEQUALiv   t   KEY_KPPLUSMINUSiw   t	   KEY_PAUSEix   t   KEY_F21iy   t   KEY_F22iz   t   KEY_F23i{   t   KEY_F24i|   t   KEY_KPCOMMAi}   t   KEY_LEFTMETAi~   t   KEY_RIGHTMETAi   t   KEY_COMPOSEi   t   KEY_STOPi   t	   KEY_AGAINi   t	   KEY_PROPSi   t   KEY_UNDOi   t	   KEY_FRONTi   t   KEY_COPYi   t   KEY_OPENi   t	   KEY_PASTEi   t   KEY_FINDi   t   KEY_CUTi   t   KEY_HELPi   t   KEY_MENUi   t   KEY_CALCi   t	   KEY_SETUPi   t	   KEY_SLEEPi   t
   KEY_WAKEUPi   t   KEY_FILEi   t   KEY_SENDFILEi   t   KEY_DELETEFILEi   t   KEY_XFERi   t	   KEY_PROG1i   t	   KEY_PROG2i   t   KEY_WWWi   t	   KEY_MSDOSi   t
   KEY_COFFEEi   t   KEY_DIRECTIONi   t   KEY_CYCLEWINDOWSi   t   KEY_MAILi   t   KEY_BOOKMARKSi   t   KEY_COMPUTERi   t   KEY_BACKi   t   KEY_FORWARDi   t   KEY_CLOSECDi   t   KEY_EJECTCDi   t   KEY_EJECTCLOSECDi   t   KEY_NEXTSONGi   t   KEY_PLAYPAUSEi   t   KEY_PREVIOUSSONGi   t
   KEY_STOPCDi   t
   KEY_RECORDi   t
   KEY_REWINDi   t	   KEY_PHONEi   t   KEY_ISOi   t
   KEY_CONFIGi   t   KEY_HOMEPAGEi   t   KEY_REFRESHi   t   KEY_EXITi   t   KEY_MOVEi   t   KEY_EDITi   t   KEY_SCROLLUPi   t   KEY_SCROLLDOWNi   t   KEY_KPLEFTPARENi   t   KEY_KPRIGHTPARENi   t	   KEY_INTL1i   t	   KEY_INTL2i   t	   KEY_INTL3i   t	   KEY_INTL4i   t	   KEY_INTL5i   t	   KEY_INTL6i   t	   KEY_INTL7i   t	   KEY_INTL8i   t	   KEY_INTL9i   t	   KEY_LANG1i   t	   KEY_LANG2i   t	   KEY_LANG3i   t	   KEY_LANG4i   t	   KEY_LANG5i   t	   KEY_LANG6i   t	   KEY_LANG7i   t	   KEY_LANG8i   t	   KEY_LANG9i   t
   KEY_PLAYCDi   t   KEY_PAUSECDi   t	   KEY_PROG3i   t	   KEY_PROG4i   t   KEY_SUSPENDi   t	   KEY_CLOSEi   t   KEY_UNKNOWNi   t   KEY_BRIGHTNESSDOWNi   t   KEY_BRIGHTNESSUPi   t   BTN_0i  t   BTN_1i  t   BTN_2i  t   BTN_3i  t   BTN_4i  t   BTN_5i  t   BTN_6i  t   BTN_7i  t   BTN_8i	  t   BTN_9i  t   BTN_LEFTi  t	   BTN_RIGHTi  t
   BTN_MIDDLEi  t   BTN_SIDEi  t	   BTN_EXTRAi  t   BTN_FORWARDi  t   BTN_BACKi   t   BTN_TRIGGERi!  t	   BTN_THUMBi"  t
   BTN_THUMB2i#  t   BTN_TOPi$  t   BTN_TOP2i%  t
   BTN_PINKIEi&  t   BTN_BASEi'  t	   BTN_BASE2i(  t	   BTN_BASE3i)  t	   BTN_BASE4i*  t	   BTN_BASE5i+  t	   BTN_BASE6i/  t   BTN_DEADi0  t   BTN_Ai1  t   BTN_Bi2  t   BTN_Ci3  t   BTN_Xi4  t   BTN_Yi5  t   BTN_Zi6  t   BTN_TLi7  t   BTN_TRi8  t   BTN_TL2i9  t   BTN_TR2i:  t
   BTN_SELECTi;  t	   BTN_STARTi<  t   BTN_MODEi=  t
   BTN_THUMBLi>  t
   BTN_THUMBRi@  t   BTN_TOOL_PENiA  t   BTN_TOOL_RUBBERiB  t   BTN_TOOL_BRUSHiC  t   BTN_TOOL_PENCILiD  t   BTN_TOOL_AIRBRUSHiE  t   BTN_TOOL_FINGERiF  t   BTN_TOOL_MOUSEiG  t   BTN_TOOL_LENSiJ  t	   BTN_TOUCHiK  t
   BTN_STYLUSiL  t   BTN_STYLUS2t   REL_Xt   REL_Yt   REL_Zt
   REL_HWHEELt   REL_DIALt	   REL_WHEELt   REL_MISCt   ABS_Xt   ABS_Yt   ABS_Zt   ABS_RXt   ABS_RYt   ABS_RZt   ABS_THROTTLEt
   ABS_RUDDERt	   ABS_WHEELt   ABS_GASt	   ABS_BRAKEt	   ABS_HAT0Xt	   ABS_HAT0Yt	   ABS_HAT1Xt	   ABS_HAT1Yt	   ABS_HAT2Xt	   ABS_HAT2Yt	   ABS_HAT3Xt	   ABS_HAT3Yt   ABS_PRESSUREt   ABS_DISTANCEt
   ABS_TILT_Xt
   ABS_TILT_Yt   ABS_MISCt
   MSC_SERIALt   MSC_PULSELEDt   LED_NUMLt	   LED_CAPSLt   LED_SCROLLLt   LED_COMPOSEt   LED_KANAt	   LED_SLEEPt   LED_SUSPENDt   LED_MUTEt   LED_MISCt	   REP_DELAYt
   REP_PERIODt	   SND_CLICKt   SND_BELLc         C   sd   | |  _  | |  _ | |  _ | |  _ | d  j	 o |  i |  n | d  j	 o |  i |  n d  S(   N(   t	   timestampR   R   R   R   R,   R   t   readFrom(   R   R  R   R   R   R,   R  (    (    R   R     s    				c         C   s    d |  i |  i |  i |  i f S(   Ns-   <Event timestamp=%r type=%r code=%r value=%r>(   R   R  R   R   R   (   R   (    (    R   R     s    c         C   s   t  |  i  } t  |  i | d  } |  i i |  i  } |  i |  i	 j o  |  i	 |  i i |  i
  } n
 |  i
 } t i |  i | | | | |  i  S(   sd   Pack this event into an input_event struct in
           the local machine's byte order.
           i@B N(   t   intR   R  t   secst   usecst   typeMapRK   R   t
   packedTypeR:   R   t
   packedCodeR'   t   packR)   R   (   R   R  R  R  R  (    (    R   R    s      	c         C   s   t  i |  i |  \ } } } } |  _	 | | d |  _
 |  i i |  |  _ |  i |  i j o  |  i |  i i |  |  _ n
 | |  _ d S(   sn   Unpack ourselves from the given string,, an
           input_event struct in the local byte order.
           f	1000000.0N(   R'   R,   R   R)   t   sR  R  R  R  R   R  R  RM   R   R:   R   (   R   R  R  R  R  R  (    (    R   R,     s     ' c         C   s&   |  i | i t i |  i    d S(   s3   Read the next event from the given file-like objectN(   R   R,   t   streamR-   R'   R(   R)   (   R   R  (    (    R   R    s     (   R   R   R    R)   RH   R  R:   R   R   R   R  R,   R  (    (    (    R   R       s*    ]           ` O
			t   __main__(   R    R'   R   R!   R   t   fcntlR7   t   __all__R	   R
   R  R8   t   EVIOCGIDt   EVIOCGBIT_512R@   R   RH   R    R   (   R  RH   R'   R  R	   R
   R@   R   R7   R8   R   R   R!   R  R    (    (    R   t   ?   s   $	', 