U
    Pe                  
   @   s  U d Z dZddlmZ ddlmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lZz ddlmZmZmZmZmZ W n  ek
r   ed  Y nX ddlmZ dd Zzdd
lZedkrej j!Z"dd Zd<ddZ#n^edkre$dZ%n,ddl&m'Z' ej$e'dddZ%dd Z(e( Zej)ge%j*_+e%j*Z,d=ddZ#W n& e-ee.fk
rt   d>ddZ#Y nX G dd de/Z0d e0j_ G d!d" d"e0Z1G d#d$ d$e1Z2G d%d& d&e0eZ3G d'd( d(e1eZ4G d)d* d*e2eZ5G d+d, d,e2eZ6d-d. Z7d?d0d1Z8d2ekr d
Z9e3e:d3< nfe3e4e5e6d4Z;e<d5e<d6d7Z=e=e;krde>d8?e=e@e;A e
d3e;e= e<d9d:d;Z9e3e:d3< d
S )@ayI  
Clock object
============

The :class:`Clock` object allows you to schedule a function call in the
future; once or repeatedly at specified intervals. You can get the time
elapsed between the scheduling and the calling of the callback via the
`dt` argument::

    # dt means delta-time
    def my_callback(dt):
        pass

    # call my_callback every 0.5 seconds
    Clock.schedule_interval(my_callback, 0.5)

    # call my_callback in 5 seconds
    Clock.schedule_once(my_callback, 5)

    # call my_callback as soon as possible (usually next frame.)
    Clock.schedule_once(my_callback)

.. note::

    If the callback returns False, the schedule will be canceled and won't
    repeat.

If you want to schedule a function to call with default arguments, you can use
the `functools.partial
<http://docs.python.org/library/functools.html#functools.partial>`_ python
module::

    from functools import partial

    def my_callback(value, key, *largs):
        pass

    Clock.schedule_interval(partial(my_callback, 'my value', 'my key'), 0.5)

Conversely, if you want to schedule a function that doesn't accept the dt
argument, you can use a `lambda
<http://docs.python.org/2/reference/expressions.html#lambda>`_ expression
to write a short function that does accept dt. For Example::

    def no_args_func():
        print("I accept no arguments, so don't schedule me in the clock")

    Clock.schedule_once(lambda dt: no_args_func(), 0.5)

.. note::

    You cannot unschedule an anonymous function unless you keep a
    reference to it. It's better to add \*args to your function
    definition so that it can be called with an arbitrary number of
    parameters.

.. important::

    The class method callback is weak-referenced: you are responsible for
    keeping a reference to your original object/callback. If you don't keep a
    reference, the ClockBase will never execute your callback. For
    example::

        class Foo(object):
            def start(self):
                Clock.schedule_interval(self.callback, 0.5)

            def callback(self, dt):
                print('In callback')

        # A Foo object is created and the method start is called.
        # Because no reference is kept to the instance returned from Foo(),
        # the object will be collected by the Python Garbage Collector and
        # your callback will be never called.
        Foo().start()

        # So you should do the following and keep a reference to the instance
        # of foo until you don't need it anymore!
        foo = Foo()
        foo.start()


.. _schedule-before-frame:

Schedule before frame
---------------------

.. versionadded:: 1.0.5

Sometimes you need to schedule a callback BEFORE the next frame. Starting
from 1.0.5, you can use a timeout of -1::

    Clock.schedule_once(my_callback, 0) # call after the next frame
    Clock.schedule_once(my_callback, -1) # call before the next frame

The Clock will execute all the callbacks with a timeout of -1 before the
next frame even if you add a new callback with -1 from a running
callback. However, :class:`Clock` has an iteration limit for these
callbacks: it defaults to 10.

If you schedule a callback that schedules a callback that schedules a ... etc
more than 10 times, it will leave the loop and send a warning to the console,
then continue after the next frame. This is implemented to prevent bugs from
hanging or crashing the application.

If you need to increase the limit, set the :attr:`max_iteration` property::

    from kivy.clock import Clock
    Clock.max_iteration = 20

.. _triggered-events:

Triggered Events
----------------

.. versionadded:: 1.0.5

:meth:`CyClockBase.create_trigger` is an advanced method way to defer a
callback. It functions exactly like :meth:`CyClockBase.schedule_once` and
:meth:`CyClockBase.schedule_interval` except that it doesn't immediately
schedule the callback. Instead, one schedules the callback using the
:class:`ClockEvent` returned by it. This ensures that you can call the event
multiple times but it won't be scheduled more than once. This is not the case
with :meth:`CyClockBase.schedule_once`::

    # will run the callback twice before the next frame
    Clock.schedule_once(my_callback)
    Clock.schedule_once(my_callback)

    # will run the callback once before the next frame
    event = Clock.create_trigger(my_callback)
    event()
    event()

    # will also run the callback only once before the next frame
    event = Clock.schedule_once(my_callback)  # now it's already scheduled
    event()  # won't be scheduled again
    event()

In addition, it is more convenient to create and bind to
the triggered event than using :meth:`CyClockBase.schedule_once` in a
function::

    from kivy.clock import Clock
    from kivy.uix.widget import Widget

    class Sample(Widget):
        def __init__(self, **kwargs):
            self._trigger = Clock.create_trigger(self.cb)
            super(Sample, self).__init__(**kwargs)
            self.bind(x=self._trigger, y=self._trigger)

        def cb(self, *largs):
            pass

Even if x and y changes within one frame, the callback is only run once.

Unscheduling
-------------

An event scheduled with :meth:`CyClockBase.schedule_once`,
:meth:`CyClockBase.schedule_interval`, or with
:meth:`CyClockBase.create_trigger` and then triggered can be unscheduled in
multiple ways. E.g::

    def my_callback(dt):
        pass

    # call my_callback every 0.5 seconds
    event = Clock.schedule_interval(my_callback, 0.5)

    # call my_callback in 5 seconds
    event2 = Clock.schedule_once(my_callback, 5)

    event_trig = Clock.create_trigger(my_callback, 5)
    event_trig()

    # unschedule using cancel
    event.cancel()

    # unschedule using Clock.unschedule
    Clock.unschedule(event2)

    # unschedule using Clock.unschedule with the callback
    # NOT RECOMMENDED
    Clock.unschedule(my_callback)

The best way to unschedule a callback is with :meth:`ClockEvent.cancel`.
:meth:`CyClockBase.unschedule` is mainly an alias for that for that function.
However, if the original callback itself is passed to
:meth:`CyClockBase.unschedule`, it'll unschedule all instances of that
callback (provided ``all`` is True, the default, otherwise only the first match
is removed).

Calling :meth:`CyClockBase.unschedule` on the original callback is highly
discouraged because it's significantly slower than when using the event.

Clock Lifecycle
---------------

Kivy's clock has a lifecycle. By default, scheduling a callback after the Clock
has ended will not raise an error, even though the callback may never be
called. That's because most callbacks are like services, e.g. responding to a
user button press - if the app is running the callbacks need to service the app
and respond to the input, but once the app has stopped or is stopping, we can
safely not process these events.

Other events always need to be processed. E.g. another thread may request a
callback in kivy's thread and then process some result. If the event is not
processed in Kivy's thread because the app stopped, the second thread may
block forever hanging the application as it exits.

Consequently, we provide a API
(:meth:`CyClockBase.create_lifecycle_aware_trigger`) for scheduling callbacks
that raise a :class:`ClockNotRunningError` if the clock has stopped. If the
scheduling succeeded it guarantees that one of its callbacks will be called.
I.e. the new :meth:`CyClockBase.create_lifecycle_aware_trigger` accepts an
additional ``clock_ended_callback`` parameter. Normally, ``callback`` will be
called when the event is processed. But, if the clock is stopped before it can
be processed, if the application exited normally (and the app was started) and
the event wasn't canceled, and the callbacks are not garbage collected, then
``clock_ended_callback`` will be called instead when the clock is stopped.

That is, given these conditions, if :class:`ClockNotRunningError` was not
raised when the event was scheduled, then one of these callbacks will be
called - either ``callback`` if the event executed normally, or
``clock_ended_callback`` if the clock is stopped while the event is scheduled.

By default, events can be scheduled before the clock is started because it is
assumed the clock will eventually be started when the app starts. I.e.
calling :meth:`CyClockBase.create_lifecycle_aware_trigger` before the clock
and application starts will succeed. But if the app never actually starts, then
neither of the callbacks may be executed.

.. versionadded:: 2.0.0
    The lifecycle was added in 2.0.0

Exception Handling
------------------

Kivy provides a exception handling manager,
:attr:`~kivy.base.ExceptionManager`, to handle its internal exceptions
including exceptions raised by clock callbacks, without crashing the
application. By default when an exception is raised, the app will crash.
But, if a handler is registered with the exception manager and the handler
handles the exception, the app will not crash and will continue as normal.::

    from kivy.base import ExceptionHandler, ExceptionManager
    class MyHandler(ExceptionHandler):
        def handle_exception(self, inst):
            if isinstance(inst, ValueError):
                Logger.exception('ValueError caught by MyHandler')
                return ExceptionManager.PASS
            return ExceptionManager.RAISE

    ExceptionManager.add_handler(MyHandler())

Then, all ValueError exceptions will be logged to the console and ignored.
Similarly, if a scheduled clock callback raises a ValueError, other clock
events will still be processed normally.

If an event's callback raises an exception, before the exception handler is
executed, the callback is immediately canceled.

It still is possible for the app to be corrupted if kivy itself is the source
of the exception. I.e. even with a handler that ignores exceptions and doesn't
crash, the app may be in a corrupted state if the error originates from within
Kivy itself. However, the exception handler can help protect the app from
crashing and can help protect against user callbacks crashing the app.

.. versionchanged:: 2.0.0
    Prior to Kivy 2.0.0, an exception raised in a event's callback would
    cause the clock to crash and subsequent events may or may not be executed.
    Even if the exception was handled by an
    :class:`~kivy.base.ExceptionHandler`, there was no guarantee that some
    scheduled events would not be skipped.

    From 2.0.0 onward, if a event's exception is handled by an
    :class:`~kivy.base.ExceptionHandler`, other events will be shielded from
    the exception and will execute normally.

Scheduling from ``__del__``
---------------------------

It is not safe to schedule Clock events from a object's ``__del__`` or
``__dealloc__`` method. If you must schedule a Clock call from this method, use
:meth:`CyClockBase.schedule_del_safe` or
:meth:`CyClockBase.schedule_lifecycle_aware_del_safe` instead.

Threading and Callback Order
-----------------------------

Beginning with 1.10.0, all the events scheduled for the same frame, e.g.
all the events scheduled in the same frame with a ``timeout`` of ``0``,
well be executed in the order they were scheduled.

Also, all the scheduling and canceling methods are fully thread safe and
can be safely used from external threads.

As a a consequence, calling :meth:`CyClockBase.unschedule` with the original
callback is now significantly slower and highly discouraged. Instead, the
returned events should be used to cancel. As a tradeoff, all the other methods
are now significantly faster than before.

Advanced Clock Details
-----------------------

The following section goes into the internal kivy clock details as well
as the various clock options. It is meant only for advanced users.

Fundamentally, the Kivy clock attempts to execute any scheduled callback
rhythmically as determined by the specified fps (frame per second, see
``maxfps`` in :mod:`~kivy.config`). That is, ideally, given e.g. a desired fps
of 30, the clock will execute the callbacks at intervals of 1 / 30 seconds, or
every 33.33 ms. All the callbacks in a frame are given the same timestamp,
i.e. the ``dt`` passed to the callback are all the same and it's the difference
in time between the start of this and the previous frame.

Because of inherent indeterminism, the frames do not actually occur exactly
at intervals of the fps and ``dt`` may be under or over the desired fps.
Also, once the timeout is "close enough" to the desired timeout, as determined
internally, Kivy will execute the callback in the current frame even when the
"actual time" has not elapsed the ``timeout`` amount.

Kivy offers now, since ``1.10.0``, multiple clocks with different behaviors.

Default Clock
^^^^^^^^^^^^^^

The default clock (``default``) behaves as described above. When a callback
with a timeout of zero or non-zero is scheduled, they are executed at the frame
that is near the timeout, which is a function of the fps. So a timeout of zero
would still result in a delay of one frame or about 1 / fps, typically a bit
less but sometimes more depending on the CPU usage of the other events
scheduled for that frame.

In a test using a fps of 30, a callback with a timeout of 0, 0.001, and 0.05,
resulted in a mean callback delay of 0.02487, 0.02488, and 0.05011 seconds,
respectively. When tested with a fps of 600 the delay for 0.05 was similar,
except the standard deviation was reduced resulting in overall better accuracy.

Interruptible Clock
^^^^^^^^^^^^^^^^^^^^

The default clock suffers from the quantization problem, as frames occur only
on intervals and any scheduled timeouts will not be able to occur during an
interval. For example, with the timeout of 0.05, while the mean was 0.05011,
its values ranged between 0.02548 - 0.07348 and a standard deviation of 0.002.
Also, there's the minimum timeout of about 0.02487.

The interruptible clock (``interrupt``) will execute timeouts even during a
frame. So a timeout of zero will execute as quickly as possible and similarly
a non-zero timeout will be executed even during the interval.

This clock, and all the clocks described after this have an option,
:attr:`ClockBaseInterruptBehavior.interupt_next_only`. When True, any of the
behavior new behavior will only apply to the callbacks with a timeout of
zero. Non-zero timeouts will behave like in the default clock. E.g. for this
clock when True, only zero timeouts will execute during the the interval.

In a test using a fps of 30, a callback with a timeout of 0, 0.001, and 0.05,
resulted in a mean callback delay of 0.00013, 0.00013, and 0.04120 seconds,
respectively when :attr:`ClockBaseInterruptBehavior.interupt_next_only` was
False. Also, compared to the default clock the standard deviation was reduced.
When :attr:`ClockBaseInterruptBehavior.interupt_next_only` was True, the values
were 0.00010, 0.02414, and 0.05034, respectively.

Free Clock
^^^^^^^^^^^

The interruptible clock may not be ideal for all cases because all the events
are executed during the intervals and events are not executed anymore
rhythmically as multiples of the fps. For example, there may not be any benefit
for the graphics to update in a sub-interval, so the additional accuracy
wastes CPU.

The Free clock (``free_all``) solves this by having ``Clock.xxx_free`` versions
of all the Clock scheduling methods. By free, we mean the event is free from
the fps because it's not fps limited. E.g.
:meth:`CyClockBaseFree.create_trigger_free` corresponds to
:meth:`CyClockBase.create_trigger`. Only when an event scheduled using the
``Clock.xxx_free`` methods is present will the clock interrupt and execute
the events during the interval. So, if no ``free`` event is present the clock
behaves like the ``default`` clock, otherwise it behaves like the ``interrupt``
clock.

In a test using a fps of 30, a callback with a timeout of 0s, 0.001s, and
0.05s, resulted in a mean callback delay of 0.00012s, 0.00017s, and 0.04121s
seconds, respectively when it was a free event and 0.02403s, 0.02405s, and
0.04829s, respectively when it wasn't.

Free Only Clock
^^^^^^^^^^^^^^^^^

The Free clock executes all events when a free event was scheduled. This
results in normal events also being execute in the middle of the interval
when a free event is scheduled. For example, above, when a free event was
absent, a normal event with a 0.001s timeout was delayed for 0.02405s. However,
if a free event happened to be also scheduled, the normal event was only
delayed 0.00014s, which may be undesirable.

The Free only clock (``free_only``) solves it by only executing free events
during the interval and normal events are always executed like with the
default clock. For example, in the presence of a free event, a normal event
with a timeout of 0.001s still had a delay of 0.02406. So this clock,
treats free and normal events independently, with normal events always being
fps limited, but never the free events.

Summary
^^^^^^^^

The kivy clock type to use can be set with the ``kivy_clock`` option the
:mod:`~kivy.config`. If ``KIVY_CLOCK`` is present in the environment it
overwrites the config selection. Its possible values are as follows:

* When ``kivy_clock`` is ``default``, the normal clock, :class:`ClockBase`,
  which limits callbacks to the maxfps quantization - is used.
* When ``kivy_clock`` is ``interrupt``, a interruptible clock,
  :class:`ClockBaseInterrupt`, which doesn't limit any callbacks to the
  maxfps - is used. Callbacks will be executed at any time.
* When ``kivy_clock`` is ``free_all``, a interruptible clock,
  :class:`ClockBaseFreeInterruptAll`, which doesn't limit any callbacks to the
  maxfps in the presence of free events, but in their absence it limits events
  to the fps quantization interval - is used.
* When ``kivy_clock`` is ``free_only``, a interruptible clock,
  :class:`ClockBaseFreeInterruptAll`, which treats free and normal events
  independently; normal events are fps limited while free events are not - is
  used.

Async clock support
-------------------

.. versionadded:: 2.0.0

Experimental async support has been added in 2.0.0. The Clock now has a
:meth:`ClockBaseBehavior.async_tick` and :meth:`ClockBaseBehavior.async_idle`
coroutine method which is used by the kivy EventLoop when the kivy EventLoop is
executed in a asynchronous manner. When used, the kivy clock does not
block while idling.

The async library to use is selected with the `KIVY_EVENTLOOP` environmental
variable or by  calling :meth:`~kivy.clock.ClockBaseBehavior.init_async_lib`
directly. The library can be one of `"asyncio"` when the standard library
`asyncio` should be used, or `"trio"` if the trio library
should be used. If not set it defaults to `"asyncio"`.

See :mod:`~kivy.app` for example usage.
)ClockClockNotRunningError
ClockEventFreeClockEventCyClockBaseCyClockBaseFree	triggeredClockBaseBehaviorClockBaseInterruptBehaviorClockBaseInterruptFreeBehavior	ClockBaseClockBaseInterruptClockBaseFreeInterruptAllClockBaseFreeInterruptOnly
mainthread    )platform)environ)wrapspartial)register_context)Config)Logger)clockN)r   r   r   r   r   a  Clock: Unable to import kivy._clock. Have you perhaps forgotten to compile kivy? Kivy contains Cython code which needs to be compiled. A missing kivy._clock often indicates the Cython code has not been compiled. Please follow the installation instructions and make sure to compile Kivy)Eventc                   C   s   d S N r   r   r   ./tmp/pip-unpacked-wheel-xzebddm3/kivy/clock.py_get_sleep_obj  s    r   )win32cygwinc                   C   s   t d dd S )NT)	_kernel32ZCreateWaitableTimerAr   r   r   r   r     s    c                 C   sF   t t|  d }t|t |dt  t  d t|d d S )N
   r   Fl    )ctypes
c_longlongintr    ZSetWaitableTimerbyrefc_void_pZWaitForSingleObject)microsecondsobjdelayr   r   r   _usleep  s        r*   darwinz
libc.dylib)find_librarycT)	use_errnoc                     s   ddl m G dd dtj} tj tjt| g _dt	krDdn2dt	krddt
d	t	 nd
t	krrdnd|   fdd}|S )Nr   )strerrorc                   @   s    e Zd ZdejfdejfgZdS )z._libc_clock_gettime_wrapper.<locals>.struct_tvtv_sectv_usecN)__name__
__module____qualname__r"   c_long_fields_r   r   r   r   	struct_tv  s   r7   linux   Zfreebsd   z clock.py: {{{:s}}} clock ID {:d}Zopenbsd      c                     s@    t t dkr0t  } t| | jjd  S )Nr   g&.>)r"   r5   pointer	get_errnoOSErrorr0   r1   )Z_ernnoZ_clock_gettimeZ_clockidr/   tvr   r   _time  s    
z*_libc_clock_gettime_wrapper.<locals>._time)osr/   r"   	Structure_libcclock_gettimer5   POINTERargtypesr   r   debugformat)r7   rB   r   r@   r   _libc_clock_gettime_wrapper   s(     rK   c                 C   s   t t|  d S r   )_libc_usleepr$   r'   r(   r   r   r   r*   +  s    c                 C   s   t | d  d S Ng    .A)timesleeprM   r   r   r   r*   5  s    c                       s  e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZdZdZdZed ZdZdZd. fdd		Zd
d Zedd Zedd Zedd Zdd Zdd Zdd Zdd Zdd Zdd Z dd Z!d d! Z"d"d# Z#d$d% Z$d&d' Z%d(d) Z&d*d+ Z'e(e)e*Z+d,d- Z,  Z-S )/r   a  The base of the kivy clock.

    :parameters:

        `async_lib`: string
            The async library to use when the clock is run asynchronously.
            Can be one of, `"asyncio"` when the standard library asyncio
            should be used, or `"trio"` if the trio library should be used.

            It defaults to `'asyncio'` or the value in the environmental
            variable `KIVY_EVENTLOOP` if set. :meth:`init_async_lib` can also
            be called directly to set the library.
    g-C6?Nr   g{Gzt?gMbP?asyncioc                    sH   |  | tt| jf | |   | _ | _| _tt	
dd| _d S )NZgraphicsZmaxfps)init_async_libsuperr   __init__rO   _duration_ts0_start_tick
_last_tickfloatr   getint_max_fps)self	async_libkwargs	__class__r   r   rT   e  s    
zClockBaseBehavior.__init__c                    s\   |dkr*ddl   | _ fdd}|| _n.|dkrJddl}|| _|j| _ntd|dS )a  Manually sets the async library to use internally, when running in
        a asynchronous manner.

        This can be called anytime before the kivy event loop has started,
        but not once the kivy App is running.

        :parameters:

            `lib`: string
                The async library to use when the clock is run asynchronously.
                Can be one of, `"asyncio"` when the standard library asyncio
                should be used, or `"trio"` if the trio library should be used.
        trior   Nc              	      s$     | | I d H  W 5 Q R X d S r   )Zmove_on_after)corotr`   r   r   wait_for}  s    z2ClockBaseBehavior.init_async_lib.<locals>.wait_forrQ   zasync library {} not recognized)r`   
_async_lib_async_wait_forrQ   rd   
ValueErrorrJ   )r[   librd   rQ   r   rc   r   rR   k  s    
z ClockBaseBehavior.init_async_libc                 C   s   | j S )zwTime spent between the last frame and the current frame
        (in seconds).

        .. versionadded:: 1.8.0
        )_dtr[   r   r   r   	frametime  s    zClockBaseBehavior.frametimec                 C   s   | j S )zNumber of internal frames (not necessarily drawn) from the start of
        the clock.

        .. versionadded:: 1.8.0
        )_framesrj   r   r   r   frames  s    zClockBaseBehavior.framesc                 C   s   | j S )z@Number of displayed frames from the start of the clock.
        )_frames_displayedrj   r   r   r   frames_displayed  s    z"ClockBaseBehavior.frames_displayedc                 C   s   dS )z/Sleeps for the number of microseconds.
        Nr   r[   r'   r   r   r   usleep  s    zClockBaseBehavior.usleepc           	      C   sz   | j }|dkr\|  }d| }| j}| j}||||\}}|s\|d|  ||||\}}q:|  }|| j | _|| _|S )z4(internal) waits here until the next frame.
        r   皙?i@B )rZ   get_resolutionrq   _check_readyrO   rW   ri   )	r[   fps	min_sleep
undershootrq   readydone	sleeptimecurrentr   r   r   idle  s    zClockBaseBehavior.idlec           	         s   | j }|dkr||  }d| }| j}d}||||\}}|sdd}| j|I dH  ||||\}}q8|s| jdI dH  n| jdI dH  |  }|| j | _|| _|S )z2(internal) async version of :meth:`idle`.
        r   rr   FTN)rZ   rs   rt   re   rP   rO   rW   ri   )	r[   ru   rv   rw   rx   sleptry   rz   r{   r   r   r   
async_idle  s$    zClockBaseBehavior.async_idlec                 C   s*   d| |   | j  }|| |k|| fS )Nr<   )rO   rW   )r[   ru   rv   rw   rz   r   r   r   rt     s    zClockBaseBehavior._check_readyc                 C   s$   |    |  }| ||   dS )zAdvance the clock to the next step. Must be called every frame.
        The default clock has a tick() function called by the core Kivy
        framework.N)pre_idlerO   	post_idler|   )r[   tsr   r   r   tick  s    zClockBaseBehavior.tickc                    s.   |    |  }|  I dH }| || dS )zasync version of :meth:`tick`. N)r   rO   r~   r   )r[   r   r{   r   r   r   
async_tick  s    zClockBaseBehavior.async_tickc                 C   s   |    dS )z4Called before :meth:`idle` by :meth:`tick`.
        N)Z_release_referencesrj   r   r   r   r     s    zClockBaseBehavior.pre_idlec                 C   s   |  j d7  _ |  jd7  _|  jd7  _|  j|| 7  _|| j }|dkrv|| j t| j | _|| _d | _| _| jdkr|| _nB|| j dkrt|| j }| j| | _| j	| _
|| _d| _d| _	|   | jS )z3Called after :meth:`idle` by :meth:`tick`.
        r<   g      ?r   N)rl   _fps_counter_duration_count_sleep_timerU   rX   _events_duration_last_fps_tick_fps_rfps_counter_rfpsZ_process_eventsri   )r[   r   r{   Zt_totdr   r   r   r     s*    

zClockBaseBehavior.post_idlec                 C   s(   |    |  jd7  _|  jd7  _dS )z"Tick the drawing counter.
        r<   N)Z_process_events_before_framer   rn   rj   r   r   r   	tick_draw  s    zClockBaseBehavior.tick_drawc                 C   s   | j S )z=Get the current average FPS calculated by the clock.
        )r   rj   r   r   r   get_fps  s    zClockBaseBehavior.get_fpsc                 C   s   | j S )a  Get the current "real" FPS calculated by the clock.
        This counter reflects the real framerate displayed on the screen.

        In contrast to get_fps(), this function returns a counter of the
        number of frames, not the average of frames per second.
        )r   rj   r   r   r   get_rfps  s    zClockBaseBehavior.get_rfpsc                 C   s   | j S )z$Get the last tick made by the clock.)rW   rj   r   r   r   get_time!  s    zClockBaseBehavior.get_timec                 C   s   | j | j S )z3Get the time in seconds from the application start.)rW   rV   rj   r   r   r   get_boottime%  s    zClockBaseBehavior.get_boottimec                 C   s"   ddl m} |||jkr d S )Nr   )ExceptionManager)Z	kivy.baser   handle_exceptionZRAISE)r[   er   r   r   r   r   +  s    z"ClockBaseBehavior.handle_exception)rQ   ).r2   r3   r4   __doc__ri   r   rV   r   r   r   r   rl   rn   r   r   r   rU   Z	MIN_SLEEPZSLEEP_UNDERSHOOTre   rf   rT   rR   propertyrk   rm   ro   rq   r|   r~   rt   r   r   r   r   r   r   r   r   r   staticmethodr   _default_timerO   r   __classcell__r   r   r^   r   r   9  sR   


!	r   z-Proxy method for :func:`~kivy.compat.clock`. c                       sn   e Zd ZdZdZdZdZdZd fdd	Z fddZ	dd	 Z
d
d Zdd Zdd Zdd Zdd Z  ZS )r	   zLA kivy clock which can be interrupted during a frame to execute events.
    FNc                    s,   t t| jf | t | _|| _| j| _d S r   )rS   r	   rT   ThreadingEvent_eventinterupt_next_onlyZget_min_timeout_get_min_timeout_func)r[   r   r]   r^   r   r   rT   ?  s    z#ClockBaseInterruptBehavior.__init__c                    s^   t t| | |dkr6dd l}| | _| j  n$|dkrZdd l}| | _| j  d S )Nr`   r   rQ   )rS   r	   rR   r`   r   _async_eventsetrQ   )r[   rh   r`   rQ   r^   r   r   rR   E  s    

z)ClockBaseInterruptBehavior.init_async_libc                 C   s   | j   | j |d  d S rN   )r   clearwaitrp   r   r   r   rq   S  s    
z!ClockBaseInterruptBehavior.usleepc                    s*   | j   | | j  |d I d H  d S rN   )r   r   rf   r   rp   r   r   r   async_usleepW  s
    
 z'ClockBaseInterruptBehavior.async_usleepc                 C   s`   | j }|sd S |jrB| js\|jd| |  | j  d|    kr\| j  | jr\| j  d S )Nr<   rr   )	rZ   timeoutr   rO   rW   rs   r   r   r   )r[   eventru   r   r   r   on_schedule\  s"    

z&ClockBaseInterruptBehavior.on_schedulec                 C   sh   | j }| j}|  }|dkrB| ||d| |\}}|sB|| |  }|| j | _|| _|  |S Nr   rr   )	rZ   r   rs   rt   r   rO   rW   ri   r   r[   ru   r   
resolutionry   rz   r{   r   r   r   r|   j  s"       
zClockBaseInterruptBehavior.idlec                    s   | j }| j}|  }|dkrd| ||d| |\}}|sP| | |I d H  qv| jdI d H  n| jdI d H  |  }|| j	 | _
|| _	|  |S r   )rZ   r   rs   rt   rf   r   re   rP   rO   rW   ri   r   r   r   r   r   r~     s&       z%ClockBaseInterruptBehavior.async_idlec                 C   sr   |  rdS |  }|sdS | jsH|  }td| || j  || }nd| |  | j  }|| |k|| fS )N)Tr   r<   )is_setr   r   rO   minrW   )r[   ru   rv   rw   r   rb   Zcurr_trz   r   r   r   rt     s    z'ClockBaseInterruptBehavior._check_ready)F)r2   r3   r4   r   r   r   r   r   rT   rR   rq   r   r   r|   r~   rt   r   r   r   r^   r   r	   6  s   r	   c                       s,   e Zd ZdZ fddZ fddZ  ZS )r
   zWA base class for the clock that interrupts the sleep interval for
    free events.
    c                    s   t t| jf | | j| _d S r   )rS   r
   rT   get_min_free_timeoutr   r[   r]   r^   r   r   rT     s    z'ClockBaseInterruptFreeBehavior.__init__c                    s$   |j s
d S |  |_tt| |S r   )freerO   Z_last_dtrS   r
   r   )r[   r   r^   r   r   r     s    
z*ClockBaseInterruptFreeBehavior.on_schedule)r2   r3   r4   r   rT   r   r   r   r   r^   r   r
     s   r
   c                       s,   e Zd ZdZdZ fddZdd Z  ZS )r   z8The ``default`` kivy clock. See module for details.
    Nc                    s   t t| jf | t | _d S r   )rS   r   rT   r   
_sleep_objr   r^   r   r   rT     s    zClockBase.__init__c                 C   s   t || j d S r   )r*   r   rp   r   r   r   rq     s    zClockBase.usleep)r2   r3   r4   r   r   rT   rq   r   r   r   r^   r   r     s   r   c                   @   s   e Zd ZdZdS )r   z:The ``interrupt`` kivy clock. See module for details.
    Nr2   r3   r4   r   r   r   r   r   r     s   r   c                   @   s   e Zd ZdZdS )r   z9The ``free_all`` kivy clock. See module for details.
    Nr   r   r   r   r   r     s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   z:The ``free_only`` kivy clock. See module for details.
    c                 C   s   | j }|  }| j}|dkr|  }| j}d| }| j}| j}d| || j  }	|	| |kr| rjd}
n4| }|szd}
n$|rd}
nt	|	|| }	|	| |k}
|
r|
  | | n||	|  |  }d| || j  }	qP|| j | _|| _|
  |S )Nr   rr   r<   TF)rZ   rO   r   rs   rq   r   r   rW   r   r   r   _process_free_eventsr   ri   )r[   ru   r{   r   rv   rq   rw   min_tr   rz   do_freerb   r   r   r   r|     s<    zClockBaseFreeInterruptOnly.idlec                    s@  | j }|  }| j}|dkr|  }| j}d| }| j}| j}d| || j  }	d}
|	| |kr| rpd}n4| }|sd}n$|rd}nt	|	|| }	|	| |k}|r|
  | | nd}
| | |	| I d H  |  }d| || j  }	qV|
s"| jdI d H  n| jdI d H  || j | _|| _|
  |S )Nr   rr   r<   FT)rZ   rO   r   rs   rq   r   r   rW   r   r   r   r   rf   r   re   rP   ri   )r[   ru   r{   r   rv   rq   rw   r   r   rz   r}   r   rb   r   r   r   r~     sL    
 
z%ClockBaseFreeInterruptOnly.async_idleN)r2   r3   r4   r   r|   r~   r   r   r   r   r     s   &r   c                    s   t   fdd}|S )a{  Decorator that will schedule the call of the function for the next
    available frame in the mainthread. It can be useful when you use
    :class:`~kivy.network.urlrequest.UrlRequest` or when you do Thread
    programming: you cannot do any OpenGL-related work in a thread.

    Please note that this method will return directly and no result can be
    returned::

        @mainthread
        def callback(self, *args):
            print('The request succeeded!',
                  'This callback is called in the main thread.')


        self.req = UrlRequest(url='http://...', on_success=callback)

    .. versionadded:: 1.8.0
    c                     s     fdd}t |d d S )Nc                    s     d S r   r   dt)argsfuncr]   r   r   callback_funcF  s    z7mainthread.<locals>.delayed_func.<locals>.callback_funcr   )r   Zschedule_once)r   r]   r   r   r   r]   r   delayed_funcD  s    z mainthread.<locals>.delayed_func)r   )r   r   r   r   r   r   1  s    r   Fc                    s    fdd}|S )a  Decorator that will trigger the call of the function at the specified
    timeout, through the method :meth:`CyClockBase.create_trigger`. Subsequent
    calls to the decorated function (while the timeout is active) are ignored.

    It can be helpful when an expensive function (i.e. call to a server) can be
    triggered by different methods. Setting a proper timeout will delay the
    calling and only one of them will be triggered.

        @triggered(timeout, interval=False)
        def callback(id):
            print('The callback has been called with id=%d' % id)

        >> callback(id=1)
        >> callback(id=2)
        The callback has been called with id=2

    The decorated callback can also be unscheduled using:

        >> callback.cancel()

    .. versionadded:: 1.10.1
    c                    s\   g  i  fdd}t j|dt fdd}fdd}t|d| |S )	Nc                    s   t   d S r   )tupler   )_args_kwargsr   r   r   cb_functioni  s    z9triggered.<locals>.wrapper_triggered.<locals>.cb_function)r   intervalc                     s6   g  d d <   t|    |   d S r   )extendlistr   updater   )r   r   
cb_triggerr   r   trigger_functionq  s
    
z>triggered.<locals>.wrapper_triggered.<locals>.trigger_functionc                      s       d S r   )cancelr   )r   r   r   trigger_cancely  s    z<triggered.<locals>.wrapper_triggered.<locals>.trigger_cancelr   )r   Zcreate_triggerr   setattr)r   r   r   r   r   r   )r   r   r   r   r   wrapper_triggeredd  s    z$triggered.<locals>.wrapper_triggeredr   )r   r   r   r   r   r   r   L  s    r   ZKIVY_DOC_INCLUDEr   )defaultZ	interruptZfree_allZ	free_onlyZ
KIVY_CLOCKZkivyZ
kivy_clockz1{} is not a valid kivy clock. Valid clocks are {}ZKIVY_EVENTLOOPrQ   )r\   )N)N)N)r   F)Br   __all__sysr   rC   r   	functoolsr   r   Zkivy.contextr   Zkivy.configr   Zkivy.loggerr   Zkivy.compatr   r   rO   Zkivy._clockr   r   r   r   r   ImportErrorerror	threadingr   r   r   r"   windllkernel32r    r*   CDLLrE   Zctypes.utilr,   rK   c_ulongrq   rH   rL   r?   AttributeErrorobjectr   r	   r
   r   r   r   r   r   r   r   __annotations__Z_classesgetZ_clk	ExceptionrJ   sortedkeysr   r   r   r   <module>   s      D 
& {r
 
 Z
7

 
 
