Exemple #1
0
    def __init__(self, display):
        from event_handlers.mouse_event_handler import MouseEventHandler
        from event_handlers.window_event_handler import WindowEventHandler
        from event_handlers.keyboard_event_handler import KeyboardEventHandler
        from event_handlers.screen_event_handler import ScreenEventHandler

        self.display = display
        self.handlers = {}
        self.screens = []

        self.mouse_event_handler    = MouseEventHandler(self)
        self.window_event_handler   = WindowEventHandler(self)
        self.keyboard_event_handler = KeyboardEventHandler(self)
        self.screen_event_handler   = ScreenEventHandler(self)

        self.handle_events()
Exemple #2
0
class EventDispatcher:

    def __init__(self, display):
        from event_handlers.mouse_event_handler import MouseEventHandler
        from event_handlers.window_event_handler import WindowEventHandler
        from event_handlers.keyboard_event_handler import KeyboardEventHandler
        from event_handlers.screen_event_handler import ScreenEventHandler

        self.display = display
        self.handlers = {}
        self.screens = []

        self.mouse_event_handler    = MouseEventHandler(self)
        self.window_event_handler   = WindowEventHandler(self)
        self.keyboard_event_handler = KeyboardEventHandler(self)
        self.screen_event_handler   = ScreenEventHandler(self)

        self.handle_events()

    def handle_events(self):
        errors = 0

        while True:
            try:
                # Get the next event, or block until the next event is received.
                event = self.display.next_event()
                self.handle_event(event)
            except Xlib.error.ConnectionClosedError:
                log_error('Display connection closed by server.')
                raise KeyboardInterrupt
            except (KeyboardInterrupt, SystemExit):
                raise
            except:
                errors += 1
                if errors > MAX_EXCEPTIONS:
                    raise
                traceback.print_exc()


    def add_handler(self, event_type, handler_function):
        self.handlers[event_type] = handler_function


    def handle_event(self, event):
        # Get the event handler, and handle the event.
        handler = self.handlers.get(event.type)
        if handler:
            handler(event)
        else:
            print 'Unhandled event: {0}'.format(event.type)


    def redirect_screen_events(self, screen_id):

        error_catcher = Xlib.error.CatchError(Xlib.error.BadAccess)
        # Enable placement control of subwindows.
        mask = Xlib.X.SubstructureRedirectMask

        root_window = self.display.screen(screen_id).root
        root_window.change_attributes(event_mask=mask, onerror=error_catcher)

        # Ensure that all errors are caught, and that everything is in sync.
        self.display.sync()

        if error_catcher.get_error():
            return False

        for keycode in get_enter_keycodes(self.display):
            root_window.grab_key(keycode,
                                 Xlib.X.Mod1Mask & ~RELEASE_MODIFIER,
                                 1,
                                 Xlib.X.GrabModeAsync,
                                 Xlib.X.GrabModeAsync)

        # Get all existing windows
        for window in root_window.query_tree().children:
            print "Grabbing motion events for window {0}...".format(window)
            self.window_event_handler.grab_window_events(window)

        return True