Ejemplo n.º 1
0
    def post_event(self, dispatcher, event, *args):
        self._post_event_queue.put((dispatcher, event, args))

        if not self._running:
            return

        event_class = POST_EVENT_CLASS
        event_kind = POST_EVENT_KIND
        event_ref = ctypes.c_void_p()
        _oscheck(
            carbon.CreateEvent(None, 
                               event_class, event_kind, 0,
                               kEventAttributeUserEvent,
                               ctypes.byref(event_ref))
        )
        _oscheck(
            carbon.SetEventParameter(event_ref, 
                                     kEventParamPostTarget,
                                     typeEventTargetRef,
                                     ctypes.sizeof(ctypes.c_void_p),
                                     ctypes.byref(self._post_event_target))
        )
        _oscheck(
            carbon.PostEventToQueue(self._event_queue, event_ref, 
                                    kEventPriorityStandard)
        )
        carbon.ReleaseEvent(event_ref)
Ejemplo n.º 2
0
    def run(self):
        self._setup()

        e = ctypes.c_void_p()
        event_dispatcher = carbon.GetEventDispatcherTarget()
        self._event_loop = event_loop = carbon.GetMainEventLoop()
        self._event_queue = carbon.GetMainEventQueue()

        # Create timer
        self._timer = timer = ctypes.c_void_p()
        idle_event_proc = EventLoopTimerProc(self._timer_proc)
        carbon.InstallEventLoopTimer(
            event_loop,
            ctypes.c_double(0.1),  # ?
            kEventDurationForever,
            idle_event_proc,
            None,
            ctypes.byref(timer))

        # TODO only once
        self._setup_post_event_handler()

        self._force_idle = False
        self._allow_polling = True

        self.dispatch_event('on_enter')

        # Dispatch events posted before entered run looop
        self._running = True  # TODO: consolidate
        self._post_event_handler(None, None, None)

        while not self.has_exit:
            if self._force_idle:
                duration = 0
            else:
                duration = kEventDurationForever
            if carbon.ReceiveNextEvent(0, None, duration, True,
                                       ctypes.byref(e)) == 0:
                carbon.SendEventToEventTarget(e, event_dispatcher)
                carbon.ReleaseEvent(e)

            # Manual idle event
            if (carbon.GetNumEventsInQueue(self._event_queue) == 0
                    or self._force_idle):
                self._force_idle = False
                self._timer_proc(timer, None, False)

        carbon.RemoveEventLoopTimer(self._timer)
        self.dispatch_event('on_exit')