<s> '''<STR_LIT:U+0020>''' <EOL> __docformat__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> from fcntl import ioctl <EOL> import array <EOL> import ctypes <EOL> import errno <EOL> import os <EOL> import struct <EOL> import sys <EOL> import pyglet <EOL> from linux_const import * <EOL> c = ctypes . cdll . LoadLibrary ( '<STR_LIT>' ) <EOL> _IOC_NRBITS = <NUM_LIT:8> <EOL> _IOC_TYPEBITS = <NUM_LIT:8> <EOL> _IOC_SIZEBITS = <NUM_LIT> <EOL> _IOC_DIRBITS = <NUM_LIT:2> <EOL> _IOC_NRMASK = ( ( <NUM_LIT:1> << _IOC_NRBITS ) - <NUM_LIT:1> ) <EOL> _IOC_TYPEMASK = ( ( <NUM_LIT:1> << _IOC_TYPEBITS ) - <NUM_LIT:1> ) <EOL> _IOC_SIZEMASK = ( ( <NUM_LIT:1> << _IOC_SIZEBITS ) - <NUM_LIT:1> ) <EOL> _IOC_DIRMASK = ( ( <NUM_LIT:1> << _IOC_DIRBITS ) - <NUM_LIT:1> ) <EOL> _IOC_NRSHIFT = <NUM_LIT:0> <EOL> _IOC_TYPESHIFT = ( _IOC_NRSHIFT + _IOC_NRBITS ) <EOL> _IOC_SIZESHIFT = ( _IOC_TYPESHIFT + _IOC_TYPEBITS ) <EOL> _IOC_DIRSHIFT = ( _IOC_SIZESHIFT + _IOC_SIZEBITS ) <EOL> _IOC_NONE = <NUM_LIT:0> <EOL> _IOC_WRITE = <NUM_LIT:1> <EOL> _IOC_READ = <NUM_LIT:2> <EOL> def _IOC ( dir , type , nr , size ) : <EOL> return ( ( dir << _IOC_DIRSHIFT ) | <EOL> ( type << _IOC_TYPESHIFT ) | <EOL> ( nr << _IOC_NRSHIFT ) | <EOL> ( size << _IOC_SIZESHIFT ) ) <EOL> def _IOR ( type , nr , struct ) : <EOL> request = _IOC ( _IOC_READ , ord ( type ) , nr , ctypes . sizeof ( struct ) ) <EOL> def f ( fileno ) : <EOL> buffer = struct ( ) <EOL> if c . ioctl ( fileno , request , ctypes . byref ( buffer ) ) < <NUM_LIT:0> : <EOL> err = ctypes . c_int . in_dll ( c , '<STR_LIT>' ) . value <EOL> raise OSError ( err , errno . errorcode [ err ] ) <EOL> return buffer <EOL> return f <EOL> def _IOR_len ( type , nr ) : <EOL> def f ( fileno , buffer ) : <EOL> request = _IOC ( _IOC_READ , ord ( type ) , nr , ctypes . sizeof ( buffer ) ) <EOL> if c . ioctl ( fileno , request , ctypes . byref ( buffer ) ) < <NUM_LIT:0> : <EOL> err = ctypes . c_int . in_dll ( c , '<STR_LIT>' ) . value <EOL> raise OSError ( err , errno . errorcode [ err ] ) <EOL> return buffer <EOL> return f <EOL> def _IOR_str ( type , nr ) : <EOL> g = _IOR_len ( type , nr ) <EOL> def f ( fileno , len = <NUM_LIT> ) : <EOL> return g ( fileno , ctypes . create_string_buffer ( len ) ) . value <EOL> return f <EOL> time_t = ctypes . c_long <EOL> suseconds_t = ctypes . c_long <EOL> class timeval ( ctypes . Structure ) : <EOL> _fields_ = ( <EOL> ( '<STR_LIT>' , time_t ) , <EOL> ( '<STR_LIT>' , suseconds_t ) <EOL> ) <EOL> class input_event ( ctypes . Structure ) : <EOL> _fields_ = ( <EOL> ( '<STR_LIT:time>' , timeval ) , <EOL> ( '<STR_LIT:type>' , ctypes . c_uint16 ) , <EOL> ( '<STR_LIT:code>' , ctypes . c_uint16 ) , <EOL> ( '<STR_LIT:value>' , ctypes . c_int32 ) <EOL> ) <EOL> class input_id ( ctypes . Structure ) : <EOL> _fields_ = ( <EOL> ( '<STR_LIT>' , ctypes . c_uint16 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint16 ) , <EOL> ( '<STR_LIT>' , ctypes . c_uint16 ) , <EOL> ( '<STR_LIT:version>' , ctypes . c_uint16 ) , <EOL> ) <EOL> class input_absinfo ( ctypes . Structure ) : <EOL> _fields_ = ( <EOL> ( '<STR_LIT:value>' , ctypes . c_int32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_int32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_int32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_int32 ) , <EOL> ( '<STR_LIT>' , ctypes . c_int32 ) , <EOL> ) <EOL> EVIOCGVERSION = _IOR ( '<STR_LIT:E>' , <NUM_LIT> , ctypes . c_int ) <EOL> EVIOCGID = _IOR ( '<STR_LIT:E>' , <NUM_LIT> , input_id ) <EOL> EVIOCGNAME = _IOR_str ( '<STR_LIT:E>' , <NUM_LIT> ) <EOL> EVIOCGPHYS = _IOR_str ( '<STR_LIT:E>' , <NUM_LIT> ) <EOL> EVIOCGUNIQ = _IOR_str ( '<STR_LIT:E>' , <NUM_LIT> ) <EOL> def EVIOCGBIT ( fileno , ev , buffer ) : <EOL> return _IOR_len ( '<STR_LIT:E>' , <NUM_LIT> + ev ) ( fileno , buffer ) <EOL> def EVIOCGABS ( fileno , abs ) : <EOL> buffer = input_absinfo ( ) <EOL> return _IOR_len ( '<STR_LIT:E>' , <NUM_LIT> + abs ) ( fileno , buffer ) <EOL> def get_set_bits ( bytes ) : <EOL> bits = set ( ) <EOL> j = <NUM_LIT:0> <EOL> for byte in bytes : <EOL> for i in range ( <NUM_LIT:8> ) : <EOL> if byte & <NUM_LIT:1> : <EOL> bits . add ( j + i ) <EOL> byte >>= <NUM_LIT:1> <EOL> j += <NUM_LIT:8> <EOL> return bits <EOL> class Element ( object ) : <EOL> value = None <EOL> def __init__ ( self , fileno , event_type , event_code ) : <EOL> self . event_type = event_type <EOL> self . event_code = event_code <EOL> self . name = '<STR_LIT>' % ( event_type , event_code ) <EOL> def get_value ( self ) : <EOL> return self . value <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . name ) <EOL> class AbsoluteElement ( Element ) : <EOL> def __init__ ( self , fileno , event_type , event_code ) : <EOL> super ( AbsoluteElement , self ) . __init__ ( fileno , event_type , event_code ) <EOL> absinfo = EVIOCGABS ( fileno , event_code ) <EOL> self . value = absinfo . value <EOL> self . minimum = absinfo . minimum <EOL> self . maximum = absinfo . maximum <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , self . value , self . minimum , self . maximum ) <EOL> event_types = { <EOL> EV_KEY : ( KEY_MAX , Element ) , <EOL> EV_REL : ( REL_MAX , Element ) , <EOL> EV_ABS : ( ABS_MAX , AbsoluteElement ) , <EOL> EV_MSC : ( MSC_MAX , Element ) , <EOL> EV_LED : ( LED_MAX , Element ) , <EOL> EV_SND : ( SND_MAX , Element ) , <EOL> } <EOL> class Device ( object ) : <EOL> _fileno = None <EOL> def __init__ ( self , filename ) : <EOL> self . filename = filename <EOL> self . _init_elements ( ) <EOL> def _init_elements ( self ) : <EOL> fileno = os . open ( self . filename , os . O_RDONLY ) <EOL> event_version = EVIOCGVERSION ( fileno ) . value <EOL> id = EVIOCGID ( fileno ) <EOL> self . id_bustype = id . bustype <EOL> self . id_vendor = hex ( id . vendor ) <EOL> self . id_product = hex ( id . product ) <EOL> self . id_version = id . version <EOL> self . name = EVIOCGNAME ( fileno ) <EOL> try : <EOL> self . phys = EVIOCGPHYS ( fileno ) <EOL> except OSError : <EOL> self . phys = '<STR_LIT>' <EOL> try : <EOL> self . uniq = EVIOCGUNIQ ( fileno ) <EOL> except OSError : <EOL> self . uniq = '<STR_LIT>' <EOL> self . elements = [ ] <EOL> self . element_map = { } <EOL> event_types_bits = ( ctypes . c_byte * <NUM_LIT:4> ) ( ) <EOL> EVIOCGBIT ( fileno , <NUM_LIT:0> , event_types_bits ) <EOL> for event_type in get_set_bits ( event_types_bits ) : <EOL> if event_type not in event_types : <EOL> continue <EOL> max_code , element_class = event_types [ event_type ] <EOL> nbytes = max_code // <NUM_LIT:8> + <NUM_LIT:1> <EOL> event_codes_bits = ( ctypes . c_byte * nbytes ) ( ) <EOL> EVIOCGBIT ( fileno , event_type , event_codes_bits ) <EOL> for event_code in get_set_bits ( event_codes_bits ) : <EOL> element = element_class ( fileno , event_type , event_code ) <EOL> self . element_map [ ( event_type , event_code ) ] = element <EOL> self . elements . append ( element ) <EOL> os . close ( fileno ) <EOL> def open ( self ) : <EOL> if self . _fileno : <EOL> return <EOL> self . _fileno = os . open ( self . filename , os . O_RDONLY | os . O_NONBLOCK ) <EOL> pyglet . clock . schedule ( self . dispatch_events ) <EOL> def close ( self ) : <EOL> if not self . _fileno : <EOL> return <EOL> os . close ( self . _fileno ) <EOL> self . _fileno = None <EOL> pyglet . clock . unschedule ( self . dispatch_events ) <EOL> def dispatch_events ( self , dt = None ) : <EOL> if not self . _fileno : <EOL> return <EOL> events = ( input_event * <NUM_LIT:64> ) ( ) <EOL> bytes = c . read ( self . _fileno , events , ctypes . sizeof ( events ) ) <EOL> n_events = bytes // ctypes . sizeof ( input_event ) <EOL> for event in events [ : n_events ] : <EOL> try : <EOL> element = self . element_map [ ( event . type , event . code ) ] <EOL> element . value = event . value <EOL> except KeyError : <EOL> pass <EOL> _devices = { } <EOL> def get_devices ( ) : <EOL> base = '<STR_LIT>' <EOL> for filename in os . listdir ( base ) : <EOL> if filename . startswith ( '<STR_LIT>' ) : <EOL> path = os . path . join ( base , filename ) <EOL> if path in _devices : <EOL> continue <EOL> try : <EOL> _devices [ path ] = Device ( path ) <EOL> except OSError : <EOL> pass <EOL> return _devices . values ( ) </s>
