#!/usr/bin/env python

'''
'''

__docformat__ = 'restructuredtext'
__version__ = '$Id: $'

import ctypes

from pyglet.app import windows, BaseEventLoop
from pyglet.window.carbon import carbon, types, constants

EventLoopTimerProc = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.c_void_p)
kEventDurationForever = ctypes.c_double(constants.kEventDurationForever)

class CarbonEventLoop(BaseEventLoop):
    def run(self):
        self._setup()

        e = ctypes.c_void_p()
        event_dispatcher = carbon.GetEventDispatcherTarget()
        event_loop = carbon.GetMainEventLoop()
        event_queue = carbon.GetMainEventQueue()
        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))

        self._sleep_time = None

        self.dispatch_event('on_enter')

        while not self.has_exit:
            duration = kEventDurationForever
            if self._sleep_time == 0.:
                duration = 0
            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(event_queue) == 0 or duration == 0:
                self._timer_proc(timer, None)

        self.dispatch_event('on_exit')

    def _timer_proc(self, timer, data):
        for window in windows:
            # Check for live resizing
            if window._resizing is not None:
                old_width, old_height = window._resizing
                rect = types.Rect()
                carbon.GetWindowBounds(window._window, 
                                       constants.kWindowContentRgn,
                                       ctypes.byref(rect))
                width = rect.right - rect.left
                height = rect.bottom - rect.top
                if width != old_width or height != old_height:
                    window._resizing = width, height
                    window.switch_to()
                    window.dispatch_event('on_resize', width, height) 

        self._sleep_time = sleep_time = self.idle()

        if sleep_time is None:
            sleep_time = constants.kEventDurationForever
        carbon.SetEventLoopTimerNextFireTime(timer, ctypes.c_double(sleep_time))

@@ -202,17 +202,27 @@
 onstants
+, _oscheck
 %0A
-
 %0AEventLo
@@ -540,24 +540,43 @@
 et()%0A       
+ self._event_loop =
  event_loop 
@@ -1187,53 +1187,8 @@
 it:%0A
-            duration = kEventDurationForever%0A
     
@@ -1251,16 +1251,158 @@
 ion = 0%0A
+                self._blocked = False%0A            else:%0A                duration = kEventDurationForever%0A                self._blocked = True%0A
         
@@ -2625,24 +2625,24 @@
 elf.idle()%0A%0A
-
         if s
@@ -2709,32 +2709,205 @@
 DurationForever%0A
+        elif sleep_time == 0.:%0A            if self._blocked:%0A                carbon.QuitEventLoop(self._event_loop)%0A            sleep_time = constants.kEventDurationForever%0A
         carbon.S

Fix carbon polling in event loop.