<s> import ctypes <EOL> import pyglet <EOL> from pyglet . window . xlib import xlib <EOL> import lib_xinput as xi <EOL> class XInputDevice ( object ) : <EOL> def __init__ ( self , display , device_info ) : <EOL> self . _x_display = display . _display <EOL> self . _device_id = device_info . id <EOL> self . name = device_info . name <EOL> self . _open_device = None <EOL> def open ( self ) : <EOL> if self . _open_device : <EOL> return <EOL> self . _open_device = xi . XOpenDevice ( self . _x_display , self . _device_id ) <EOL> if not self . _open_device : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def close ( self ) : <EOL> if not self . _open_device : <EOL> return <EOL> xi . XCloseDevice ( self . _x_display , self . _open_device ) <EOL> def attach ( self , window ) : <EOL> assert window . _x_display == self . _x_display <EOL> return XInputDeviceInstance ( self , window ) <EOL> class XInputDeviceInstance ( pyglet . event . EventDispatcher ) : <EOL> def __init__ ( self , device , window ) : <EOL> '''<STR_LIT>''' <EOL> assert device . _x_display == window . _x_display <EOL> assert device . _open_device <EOL> self . device = device <EOL> self . window = window <EOL> self . _events = [ ] <EOL> try : <EOL> dispatcher = window . __xinput_window_event_dispatcher <EOL> except AttributeError : <EOL> dispatcher = window . __xinput_window_event_dispatcher = XInputWindowEventDispatcher ( ) <EOL> dispatcher . add_instance ( self ) <EOL> device = device . _open_device . contents <EOL> if not device . num_classes : <EOL> return <EOL> for i in range ( device . num_classes ) : <EOL> class_info = device . classes [ i ] <EOL> if class_info . input_class == xi . KeyClass : <EOL> self . _add ( class_info , xi . _deviceKeyPress , <EOL> dispatcher . _event_xinput_key_press ) <EOL> self . _add ( class_info , xi . _deviceKeyRelease , <EOL> dispatcher . _event_xinput_key_release ) <EOL> elif class_info . input_class == xi . ButtonClass : <EOL> self . _add ( class_info , xi . _deviceButtonPress , <EOL> dispatcher . _event_xinput_button_press ) <EOL> self . _add ( class_info , xi . _deviceButtonRelease , <EOL> dispatcher . _event_xinput_button_release ) <EOL> elif class_info . input_class == xi . ValuatorClass : <EOL> self . _add ( class_info , xi . _deviceMotionNotify , <EOL> dispatcher . _event_xinput_motion ) <EOL> elif class_info . input_class == xi . ProximityClass : <EOL> self . _add ( class_info , xi . _proximityIn , <EOL> dispatcher . _event_xinput_proximity_in ) <EOL> self . _add ( class_info , xi . _proximityOut , <EOL> dispatcher . _event_xinput_proximity_out ) <EOL> elif class_info . input_class == xi . FeedbackClass : <EOL> pass <EOL> elif class_info . input_class == xi . FocusClass : <EOL> pass <EOL> elif class_info . input_class == xi . OtherClass : <EOL> pass <EOL> array = ( xi . XEventClass * len ( self . _events ) ) ( * self . _events ) <EOL> xi . XSelectExtensionEvent ( window . _x_display , <EOL> window . _window , <EOL> array , <EOL> len ( array ) ) <EOL> def _add ( self , class_info , event , handler ) : <EOL> _type = class_info . event_type_base + event <EOL> _class = self . device . _device_id << <NUM_LIT:8> | _type <EOL> self . _events . append ( _class ) <EOL> self . window . _event_handlers [ _type ] = handler <EOL> XInputDeviceInstance . register_event_type ( '<STR_LIT>' ) <EOL> XInputDeviceInstance . register_event_type ( '<STR_LIT>' ) <EOL> XInputDeviceInstance . register_event_type ( '<STR_LIT>' ) <EOL> XInputDeviceInstance . register_event_type ( '<STR_LIT>' ) <EOL> XInputDeviceInstance . register_event_type ( '<STR_LIT>' ) <EOL> class XInputWindowEventDispatcher ( object ) : <EOL> def __init__ ( self ) : <EOL> self . _instances = { } <EOL> def add_instance ( self , instance ) : <EOL> self . _instances [ instance . device . _device_id ] = instance <EOL> def remove_instance ( self , instance ) : <EOL> del self . _instances [ instance . device . _device_id ] <EOL> def dispatch_instance_event ( self , e , * args ) : <EOL> try : <EOL> instance = self . _instances [ e . deviceid ] <EOL> except KeyError : <EOL> return <EOL> instance . dispatch_event ( * args ) <EOL> @ pyglet . window . xlib . XlibEventHandler ( <NUM_LIT:0> ) <EOL> def _event_xinput_key_press ( self , ev ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> @ pyglet . window . xlib . XlibEventHandler ( <NUM_LIT:0> ) <EOL> def _event_xinput_key_release ( self , ev ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> @ pyglet . window . xlib . XlibEventHandler ( <NUM_LIT:0> ) <EOL> def _event_xinput_button_press ( self , ev ) : <EOL> e = ctypes . cast ( ctypes . byref ( ev ) , <EOL> ctypes . POINTER ( xi . XDeviceButtonEvent ) ) . contents <EOL> self . dispatch_instance_event ( e , '<STR_LIT>' , e . button ) <EOL> @ pyglet . window . xlib . XlibEventHandler ( <NUM_LIT:0> ) <EOL> def _event_xinput_button_release ( self , ev ) : <EOL> e = ctypes . cast ( ctypes . byref ( ev ) , <EOL> ctypes . POINTER ( xi . XDeviceButtonEvent ) ) . contents <EOL> self . dispatch_instance_event ( e , '<STR_LIT>' , e . button ) <EOL> @ pyglet . window . xlib . XlibEventHandler ( <NUM_LIT:0> ) <EOL> def _event_xinput_motion ( self , ev ) : <EOL> e = ctypes . cast ( ctypes . byref ( ev ) , <EOL> ctypes . POINTER ( xi . XDeviceMotionEvent ) ) . contents <EOL> axis_data = [ ] <EOL> for i in range ( e . axes_count ) : <EOL> axis_data . append ( e . axis_data [ i ] ) <EOL> self . dispatch_instance_event ( e , '<STR_LIT>' , axis_data , e . x , e . y ) <EOL> @ pyglet . window . xlib . XlibEventHandler ( <NUM_LIT:0> ) <EOL> def _event_xinput_proximity_in ( self , ev ) : <EOL> e = ctypes . cast ( ctypes . byref ( ev ) , <EOL> ctypes . POINTER ( xi . XProximityNotifyEvent ) ) . contents <EOL> self . dispatch_instance_event ( e , '<STR_LIT>' ) <EOL> @ pyglet . window . xlib . XlibEventHandler ( - <NUM_LIT:1> ) <EOL> def _event_xinput_proximity_out ( self , ev ) : <EOL> e = ctypes . cast ( ctypes . byref ( ev ) , <EOL> ctypes . POINTER ( xi . XProximityNotifyEvent ) ) . contents <EOL> self . dispatch_instance_event ( e , '<STR_LIT>' ) <EOL> def _check_extension ( display ) : <EOL> major_opcode = ctypes . c_int ( ) <EOL> first_event = ctypes . c_int ( ) <EOL> first_error = ctypes . c_int ( ) <EOL> xlib . XQueryExtension ( display . _display , '<STR_LIT>' , <EOL> ctypes . byref ( major_opcode ) , <EOL> ctypes . byref ( first_event ) , <EOL> ctypes . byref ( first_error ) ) <EOL> if not major_opcode . value : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def get_devices ( display ) : <EOL> _check_extension ( display ) <EOL> devices = [ ] <EOL> count = ctypes . c_int ( <NUM_LIT:0> ) <EOL> device_list = xi . XListInputDevices ( display . _display , count ) <EOL> for i in range ( count . value ) : <EOL> device_info = device_list [ i ] <EOL> devices . append ( XInputDevice ( display , device_info ) ) <EOL> return devices </s>
