<s> """<STR_LIT>""" <EOL> import sys , os , struct , time , fcntl <EOL> from pycopia . aid import Queue <EOL> INT = "<STR_LIT:i>" <EOL> INT2 = "<STR_LIT>" <EOL> INT5 = "<STR_LIT>" <EOL> SHORT = "<STR_LIT:h>" <EOL> USHORT = "<STR_LIT:H>" <EOL> SHORT4 = "<STR_LIT>" <EOL> SIZEOF_INT2 = struct . calcsize ( INT2 ) <EOL> from pycopia . OS . Linux . IOCTL import _IOC , _IO , _IOW , _IOR , _IOC_READ <EOL> EVIOCGVERSION = _IOR ( <NUM_LIT> , <NUM_LIT> , INT ) <EOL> EVIOCGID = _IOR ( <NUM_LIT> , <NUM_LIT> , SHORT4 ) <EOL> EVIOCGREP = _IOR ( <NUM_LIT> , <NUM_LIT> , INT2 ) <EOL> EVIOCSREP = _IOW ( <NUM_LIT> , <NUM_LIT> , INT2 ) <EOL> EVIOCGKEYCODE = _IOR ( <NUM_LIT> , <NUM_LIT> , INT2 ) <EOL> EVIOCSKEYCODE = _IOW ( <NUM_LIT> , <NUM_LIT> , INT2 ) <EOL> EVIOCGKEY = _IOR ( <NUM_LIT> , <NUM_LIT> , INT2 ) <EOL> EVIOCGNAME = _IOC ( _IOC_READ , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) <EOL> EVIOCGPHYS = _IOC ( _IOC_READ , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) <EOL> EVIOCGUNIQ = _IOC ( _IOC_READ , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:255> ) <EOL> EVIOCRMFF = _IOW ( <NUM_LIT> , <NUM_LIT> , INT ) <EOL> EVIOCSGAIN = _IOW ( <NUM_LIT> , <NUM_LIT> , USHORT ) <EOL> EVIOCSAUTOCENTER = _IOW ( <NUM_LIT> , <NUM_LIT> , USHORT ) <EOL> EVIOCGEFFECTS = _IOR ( <NUM_LIT> , <NUM_LIT> , INT ) <EOL> EVIOCGRAB = _IOW ( <NUM_LIT> , <NUM_LIT> , INT ) <EOL> def EVIOCGBIT ( evtype , len = <NUM_LIT:255> ) : <EOL> return _IOC ( _IOC_READ , <NUM_LIT> , <NUM_LIT> + evtype , len ) <EOL> def EVIOCGABS ( abs ) : <EOL> return _IOR ( <NUM_LIT> , <NUM_LIT> + abs , INT5 ) <EOL> def EVIOCGSW ( len ) : <EOL> return _IOC ( _IOC_READ , <NUM_LIT> , <NUM_LIT> , len ) <EOL> def EVIOCGLED ( len ) : <EOL> return _IOC ( _IOC_READ , <NUM_LIT> , <NUM_LIT> , len ) <EOL> EVFMT = "<STR_LIT>" <EOL> EVsize = struct . calcsize ( EVFMT ) <EOL> EV_SYN = <NUM_LIT> <EOL> EV_KEY = <NUM_LIT> <EOL> EV_REL = <NUM_LIT> <EOL> EV_ABS = <NUM_LIT> <EOL> EV_MSC = <NUM_LIT> <EOL> EV_SW = <NUM_LIT> <EOL> EV_LED = <NUM_LIT> <EOL> EV_SND = <NUM_LIT> <EOL> EV_REP = <NUM_LIT> <EOL> EV_FF = <NUM_LIT> <EOL> EV_PWR = <NUM_LIT> <EOL> EV_FF_STATUS = <NUM_LIT> <EOL> EV_MAX = <NUM_LIT> <EOL> class Features ( object ) : <EOL> """<STR_LIT>""" <EOL> NAMES = { <EOL> EV_SYN : "<STR_LIT>" , <EOL> EV_KEY : "<STR_LIT>" , <EOL> EV_REL : "<STR_LIT>" , <EOL> EV_ABS : "<STR_LIT>" , <EOL> EV_MSC : "<STR_LIT>" , <EOL> EV_SW : "<STR_LIT>" , <EOL> EV_LED : "<STR_LIT>" , <EOL> EV_SND : "<STR_LIT>" , <EOL> EV_REP : "<STR_LIT>" , <EOL> EV_FF : "<STR_LIT>" , <EOL> EV_PWR : "<STR_LIT>" , <EOL> EV_FF_STATUS : "<STR_LIT>" , <EOL> } <EOL> def __init__ ( self , bits = <NUM_LIT:0> ) : <EOL> self . _bits = bits <EOL> def has_keys ( self ) : <EOL> return ( self . _bits >> EV_KEY ) & <NUM_LIT:1> <EOL> def has_leds ( self ) : <EOL> return ( self . _bits >> EV_LED ) & <NUM_LIT:1> <EOL> def has_sound ( self ) : <EOL> return ( self . _bits >> EV_SND ) & <NUM_LIT:1> <EOL> def has_relative_axes ( self ) : <EOL> return ( self . _bits >> EV_REL ) & <NUM_LIT:1> <EOL> def has_absolute_axes ( self ) : <EOL> return ( self . _bits >> EV_ABS ) & <NUM_LIT:1> <EOL> def has_misc ( self ) : <EOL> return ( self . _bits >> EV_MSC ) & <NUM_LIT:1> <EOL> def has_switches ( self ) : <EOL> return ( self . _bits >> EV_SW ) & <NUM_LIT:1> <EOL> def has_repeat ( self ) : <EOL> return ( self . _bits >> EV_REP ) & <NUM_LIT:1> <EOL> def has_forcefeedback ( self ) : <EOL> return ( self . _bits >> EV_FF ) & <NUM_LIT:1> <EOL> def has_forcefeedback_status ( self ) : <EOL> return ( self . _bits >> EV_FF_STATUS ) & <NUM_LIT:1> <EOL> def has_power ( self ) : <EOL> return ( self . _bits >> EV_PWR ) & <NUM_LIT:1> <EOL> def _make_set ( self ) : <EOL> featureset = set ( ) <EOL> bits = self . _bits <EOL> for bit in ( EV_KEY , EV_REL , EV_ABS , EV_MSC , EV_SW , EV_LED , EV_SND , EV_REP , EV_FF , EV_PWR , EV_FF_STATUS ) : <EOL> if ( bits >> bit ) & <NUM_LIT:1> : <EOL> featureset . add ( bit ) <EOL> return featureset <EOL> def match ( self , other ) : <EOL> pass <EOL> def __str__ ( self ) : <EOL> s = [ ] <EOL> bits = self . _bits <EOL> for bit , name in self . NAMES . items ( ) : <EOL> if ( bits >> bit ) & <NUM_LIT:1> : <EOL> s . append ( name ) <EOL> return "<STR_LIT:U+002CU+0020>" . join ( s ) <EOL> class Event ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , time = <NUM_LIT:0.0> , evtype = <NUM_LIT:0> , code = <NUM_LIT:0> , value = <NUM_LIT:0> ) : <EOL> self . time = time <EOL> self . evtype = evtype <EOL> self . code = code <EOL> self . value = value <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . time , self . evtype , self . code , self . value ) <EOL> def encode ( self ) : <EOL> tv_sec , tv_usec = divmod ( self . time , <NUM_LIT:1.0> ) <EOL> return struct . pack ( EVFMT , long ( tv_sec ) , long ( tv_usec * <NUM_LIT> ) , self . evtype , self . code , self . value ) <EOL> def decode ( self , ev ) : <EOL> tv_sec , tv_usec , self . evtype , self . code , self . value = struct . unpack ( EVFMT , ev ) <EOL> self . time = float ( tv_sec ) + float ( tv_usec ) / <NUM_LIT> <EOL> def set ( self , evtype , code , value ) : <EOL> self . time = time . time ( ) <EOL> self . evtype = int ( evtype ) <EOL> self . code = int ( code ) <EOL> self . value = int ( value ) <EOL> class EventFile ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , fname , mode = "<STR_LIT:r>" ) : <EOL> self . _fo = open ( fname , mode ) <EOL> self . _eventq = Queue ( ) <EOL> def read ( self , amt = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . _eventq : <EOL> if not self . _fill ( ) : <EOL> return None <EOL> return self . _eventq . pop ( ) <EOL> def readall ( self ) : <EOL> ev = self . read ( ) <EOL> while ev : <EOL> yield ev <EOL> ev = self . read ( ) <EOL> def _fill ( self ) : <EOL> raw = self . _fo . read ( EVsize * <NUM_LIT:32> ) <EOL> if raw : <EOL> for i in xrange ( len ( raw ) / EVsize ) : <EOL> ev = Event ( ) <EOL> ev . decode ( raw [ i * EVsize : ( i + <NUM_LIT:1> ) * EVsize ] ) <EOL> self . _eventq . push ( ev ) <EOL> return True <EOL> else : <EOL> return False <EOL> class EventDevice ( object ) : <EOL> DEVNAME = None <EOL> def __init__ ( self , fname = None ) : <EOL> self . _fd = None <EOL> self . name = "<STR_LIT>" <EOL> self . _eventq = Queue ( ) <EOL> self . idbus = self . idvendor = self . idproduct = self . idversion = None <EOL> if fname : <EOL> self . open ( fname ) <EOL> self . initialize ( ) <EOL> def __str__ ( self ) : <EOL> if self . idbus is None : <EOL> self . get_deviceid ( ) <EOL> return "<STR_LIT>" % ( self . name , self . idbus , self . idvendor , self . idproduct , self . idversion , self . get_features ( ) ) <EOL> def _fill ( self ) : <EOL> global EVsize <EOL> try : <EOL> raw = os . read ( self . _fd , EVsize * <NUM_LIT:32> ) <EOL> except EOFError : <EOL> self . close ( ) <EOL> else : <EOL> if raw : <EOL> for i in range ( len ( raw ) / EVsize ) : <EOL> ev = Event ( ) <EOL> ev . decode ( raw [ i * EVsize : ( i + <NUM_LIT:1> ) * EVsize ] ) <EOL> self . _eventq . push ( ev ) <EOL> def find ( self , start = <NUM_LIT:0> , name = None ) : <EOL> name = name or self . DEVNAME <EOL> assert name is not None , "<STR_LIT>" <EOL> for d in range ( start , <NUM_LIT:16> ) : <EOL> filename = "<STR_LIT>" % ( d , ) <EOL> if os . path . exists ( filename ) : <EOL> try : <EOL> self . open ( filename ) <EOL> except ( OSError , IOError ) : <EOL> pass <EOL> else : <EOL> if name in self . name : <EOL> return <EOL> self . close ( ) <EOL> raise IOError ( "<STR_LIT>" % ( name , ) ) <EOL> def open ( self , filename ) : <EOL> self . _fd = os . open ( filename , os . O_RDWR ) <EOL> name = fcntl . ioctl ( self . _fd , EVIOCGNAME , chr ( <NUM_LIT:0> ) * <NUM_LIT> ) <EOL> self . name = name . replace ( chr ( <NUM_LIT:0> ) , '<STR_LIT>' ) <EOL> def fileno ( self ) : <EOL> return self . _fd <EOL> def close ( self ) : <EOL> if self . _fd is not None : <EOL> os . close ( self . _fd ) <EOL> self . _fd = None <EOL> self . name = "<STR_LIT>" <EOL> def read ( self ) : <EOL> if not self . _eventq : <EOL> self . _fill ( ) <EOL> return self . _eventq . pop ( ) <EOL> def readall ( self ) : <EOL> ev = self . read ( ) <EOL> while ev : <EOL> yield ev <EOL> ev = self . read ( ) <EOL> def write ( self , evtype , code , value ) : <EOL> ev = Event ( <NUM_LIT:0.0> , evtype , code , value ) <EOL> return os . write ( self . _fd , ev . encode ( ) ) <EOL> def get_driverversion ( self ) : <EOL> ver = fcntl . ioctl ( self . _fd , EVIOCGVERSION , '<STR_LIT>' ) <EOL> return struct . unpack ( INT , ver ) [ <NUM_LIT:0> ] <EOL> def get_deviceid ( self ) : <EOL> ver = fcntl . ioctl ( self . _fd , EVIOCGID , '<STR_LIT>' ) <EOL> self . idbus , self . idvendor , self . idproduct , self . idversion = struct . unpack ( SHORT4 , ver ) <EOL> return self . idbus , self . idvendor , self . idproduct , self . idversion <EOL> def get_features ( self ) : <EOL> caps = fcntl . ioctl ( self . _fd , EVIOCGBIT ( <NUM_LIT:0> ) , '<STR_LIT>' ) <EOL> caps = struct . unpack ( INT , caps ) [ <NUM_LIT:0> ] <EOL> return Features ( caps ) <EOL> def readable ( self ) : <EOL> return bool ( self . _fd ) <EOL> def writable ( self ) : <EOL> return False <EOL> def priority ( self ) : <EOL> return False <EOL> def read_handler ( self ) : <EOL> self . _fill ( ) <EOL> def write_handler ( self ) : <EOL> pass <EOL> def pri_handler ( self ) : <EOL> pass <EOL> def hangup_handler ( self ) : <EOL> pass <EOL> def initialize ( self ) : <EOL> pass <EOL> def get_device_names ( start = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> names = [ ] <EOL> for d in range ( start , <NUM_LIT:16> ) : <EOL> filename = "<STR_LIT>" % ( d , ) <EOL> if os . path . exists ( filename ) : <EOL> try : <EOL> fd = os . open ( filename , os . O_RDWR ) <EOL> try : <EOL> name = fcntl . ioctl ( fd , EVIOCGNAME , chr ( <NUM_LIT:0> ) * <NUM_LIT> ) <EOL> finally : <EOL> os . close ( fd ) <EOL> name = name . replace ( chr ( <NUM_LIT:0> ) , '<STR_LIT>' ) <EOL> except ( OSError , IOError ) : <EOL> continue <EOL> else : <EOL> names . append ( ( d , name ) ) <EOL> return names <EOL> def get_devices ( start = <NUM_LIT:0> ) : <EOL> devs = [ ] <EOL> for d in range ( start , <NUM_LIT:16> ) : <EOL> filename = "<STR_LIT>" % ( d , ) <EOL> if os . path . exists ( filename ) : <EOL> devs . append ( EventDevice ( filename ) ) <EOL> return devs <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> for dev in get_devices ( ) : <EOL> print dev </s>
