#!/usr/bin/env python

'''
'''

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

import ctypes

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

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()
        self._event_loop = 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:
            if self._sleep_time == 0.:
                duration = 0
                self._blocked = False
            else:
                duration = kEventDurationForever
                self._blocked = True
            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
        elif sleep_time == 0.:
            if self._blocked:
                carbon.QuitEventLoop(self._event_loop)
            sleep_time = constants.kEventDurationForever
        carbon.SetEventLoopTimerNextFireTime(timer, ctypes.c_double(sleep_time))

@@ -1094,24 +1094,25 @@
 lf._
-sleep_time = Non
+force_idle = Fals
 e%0A%0A 
@@ -1205,32 +1205,26 @@
 f self._
-sleep_time == 0.
+force_idle
 :%0A      
@@ -1721,22 +1721,66 @@
  or 
-duration == 0:
+self._force_idle:%0A                self._force_idle = False
 %0A   
@@ -1820,17 +1820,25 @@
 er, None
-)
+, False)%0A
 %0A%0A      
@@ -1908,16 +1908,32 @@
 er, data
+, in_events=True
 ):%0A     
@@ -2651,26 +2651,445 @@
     
-self._sleep_time =
+    # Check for deferred recreate%0A            if window._recreate_deferred:%0A                if in_events:%0A                    # Break out of ReceiveNextEvent so it can be processed%0A                    # in next iteration.%0A                    carbon.QuitEventLoop(self._event_loop)%0A                    self._force_idle = True%0A                else:%0A                    # Do it now.%0A                    window._recreate_immediate()%0A%0A       
  sle
@@ -3307,32 +3307,68 @@
 lf._event_loop)%0A
+            self._force_idle = True%0A
             slee
@@ -3489,8 +3489,9 @@
 _time))%0A
+%0A

Fix carbon fullscreen in event loop.