Ejemplo n.º 1
0
class EventCapture(threading.Thread):


    def __init__(self):
        super( EventCapture, self ).__init__()
        self.finished = threading.Event()
        
        # Hook to our display.
        self.local_display = display.Display()
        self.record_display = display.Display()
        
        # Set up event handler     
        self.event_handler = EventHandler()
        
        # Check for record extension
        if not self.record_display.has_extension("RECORD"):
            raise Exception("Your X-Server does not have the RECORD extension available/enabled.")
        
                
    def run(self):
        # Create a recording context
        self.context = self.record_display.record_create_context(
                0,
                [record.AllClients],
                [{
                        'core_requests': (0, 0),
                        'core_replies': (0, 0),
                        'ext_requests': (0, 0, 0, 0),
                        'ext_replies': (0, 0, 0, 0),
                        'delivered_events': (0, 0),
                        'device_events': (X.KeyPress, X.ButtonPress),
                        'errors': (0, 0),
                        'client_started': False,
                        'client_died': False,
                }]
            )

        # Enable the context. Only returns after a call to record_disable_context,
        # while calling the callback function in the meantime
        self.record_display.record_enable_context(self.context, self.__processEvents )
        
        # Free the context
        self.record_display.record_free_context(self.context)
        self.record_display.close()


    def stop(self):
        """ Close everything down """

        self.finished.set()
        self.local_display.record_disable_context(self.context)
        self.local_display.flush()
        self.event_handler.event_queue.put((None, None))
        self.join()
    
    
    def shouldReturn( self, reply ):
        """ Check whether or not we should continue processing """
    
        if reply.category != record.FromServer:
            return True
        elif reply.client_swapped:
            return True
        elif not len(reply.data) or ord(reply.data[0]) < 2:
            return True
        else:
            return False
    
    
    def extractEventAndData( self, data ):
        """ Return the event and data """
    
        event_field = rq.EventField(None)
        return event_field.parse_binary_value(
               data, 
               self.record_display.display, 
               None, 
               None
           )
    
    
    def __processEvents( self, reply ):
        if self.shouldReturn( reply ) is False:
            data = reply.data
            
            while len(data):
                event, data = self.extractEventAndData( data )
                
                if event.type == X.KeyPress:
                    self.event_handler.handleKeyPress('key_code')
                
                elif event.type == X.KeyRelease:
                    self.event_handler.handleKeyRelease('key_code')
                
                elif event.type == X.ButtonPress:
                    logger.info( "button pressed" )