U
    Pe!                     @   sD   d Z dZdd Zdadadd Zdd	 Zdadad
d Z	dd Z
dS )zR
Support
=======

Activate other frameworks/toolkits inside the kivy event loop.

)install_gobject_iterationinstall_twisted_reactoruninstall_twisted_reactorinstall_androidc                     s   ddl m}  zddlm} W n tk
r8   ddl}Y nX t|drHdS d|_| }|	  |
   fdd}| |d dS )	zImport and install gobject context iteration inside our event loop.
    This is used as soon as gobject is used (like gstreamer).
        Clock)GObjectN_gobject_already_installedTc                     s,   d}   r(|dk r( d |d7 }qd S )Nr   
   F   )pending	iteration)largsloopcontext 0/tmp/pip-unpacked-wheel-xzebddm3/kivy/support.py_gobject_iteration%   s    
z5install_gobject_iteration.<locals>._gobject_iteration)
kivy.clockr   Zgi.repositoryr   ImportErrorgobjecthasattrr	   ZMainLoopZthreads_initZget_contextschedule_interval)r   r   r   r   r   r   r   r      s    
r   r   Nc                  G   s.   ddl m} |j  td8 atdk r*dS d S )Nr   Windowr   F)kivy.core.windowr   canvas
ask_updateg_android_redraw_count)r   r   r   r   r   _android_ask_redraw6   s
    
r    c                     s   zddl W n tk
r*   td Y dS X ddlm  ddlm}  ddl}| d 	  
j|j 
j|j  fdd} |d dS )	zInstall hooks for the android platform.

    * Automatically sleep when the device is paused.
    * Automatically kill the application when the return key is pressed.
    r   Nz4Android lib is missing, cannot install android hooksr   LoggerzSupport: Android install hooksc                     s    sd S ddlm} ddlm} ddlm} ddlm} |	d |
 }|d krj|	d |  d S |dr|	d	    r|	d
 |  nP|	d |d |j  datd kr؈ tdant  t  |	d n|	d |  d S )Nr   )App)stopTouchAppr!   r   z/Android: Must go into sleep mode, check the appz)Android: No app running, stop everything.Zon_pausez)Android: App paused, now wait for resume.z(Android: Android wants to close our app.z-Android: Android has resumed, resume the app.Z	on_resume   g?zAndroid: App resume completed.z.Android: App doesn't support pause mode, stop.)Zcheck_pauseZkivy.appr#   	kivy.baser$   kivy.loggerr"   r   r   infoZget_running_appdispatchZwait_for_resumeZ
check_stopr   r   r   _redraw_eventr   r    cancel)r   r#   r$   r"   r   Zappr   androidr   r   android_check_pauseZ   s@    







 
z,install_android.<locals>.android_check_pause)r-   r   printr   r   r'   r"   pygamer(   initZmap_keyZKEYCODE_MENUZK_MENUZKEYCODE_BACKZK_ESCAPEr   )r"   r0   r.   r   r,   r   r   A   s    
4r   c                     s   ddl }t|drdS d|_dd ddlm} |  ddlm ddlm	 dd	l
m} dd
lm} ddlm ddlm  | fddfdda fdd}fdd}|a |d |j|d dS )a  Installs a threaded twisted reactor, which will schedule one
    reactor iteration before the next frame only when twisted needs
    to do some work.

    Any arguments or keyword arguments passed to this function will be
    passed on the the threadedselect reactors interleave function. These
    are the arguments one would usually pass to twisted's reactor.startRunning.

    Unlike the default twisted reactor, the installed reactor will not handle
    any signals unless you set the 'installSignalHandlers' keyword argument
    to 1 explicitly. This is done to allow kivy to handle the signals as
    usual unless you specifically want the twisted reactor to handle the
    signals (e.g. SIGINT).

    .. note::
        Twisted is not included in iOS build by default. To use it on iOS,
        put the twisted distribution (and zope.interface dependency) in your
        application directory.
    r   N_kivy_twisted_reactor_installedTZinstallSignalHandlers)_threadedselect)reactor)ReactorNotRunning)deque	EventLoopr!   r   c                    s     d |  dS )zFWakeup the twisted reactor to start processing the task queue
        z-Support: twisted wakeup call to schedule taskN)traceappend)Ztwisted_loop_nextr"   qr   r   reactor_wake   s    
z-install_twisted_reactor.<locals>.reactor_wakec                     s"     d tr   q
dS )z/Process the twisted reactor task queue
        z&Support: processing twisted task queueN)r9   lenpopleftargsr;   r   r   reactor_work   s    
z-install_twisted_reactor.<locals>.reactor_workc                     s(    d jf  d dS )z,Start the twisted reactor main loop
        z!Support: Starting twisted reactorr   N)r(   Z
interleaver   r@   )r   r"   kwargsr4   r=   rB   r   r   reactor_start   s    
z.install_twisted_reactor.<locals>.reactor_startc                     sh   j r d j    d   z  W n k
rL   Y nX ddl}|jdd dS )z/Shutdown the twisted reactor main loop
        z!Support: Stopping twisted threadsz&Support: Shutting down twisted reactorr   Nztwisted.internet.reactor)Z
threadpoolr(   stopZ_mainLoopShutdownsysmodulespop)rA   rF   )r"   r5   r4   r   r   reactor_stop   s    


z-install_twisted_reactor.<locals>.reactor_stopZon_stop)twistedr   r2   
setdefaultZtwisted.internetr3   installr4   Ztwisted.internet.errorr5   collectionsr6   r&   r8   r'   r"   r   r   _twisted_reactor_work_twisted_reactor_stopperZschedule_oncebind)rC   rK   r3   r6   r8   rD   rI   r   )r   r"   r5   rC   r<   r4   r=   rB   r   r      s,    
r   c                  C   s<   ddl } t| dsdS ddlm} t  |jtd | `dS )zUninstalls the Kivy's threaded Twisted Reactor. No more Twisted
    tasks will run after this got called. Use this to clean the
    `twisted.internet.reactor` .

    .. versionadded:: 1.9.0
    r   Nr2   r7   rJ   )rK   r   r&   r8   rP   Zunbindr2   )rK   r8   r   r   r   r      s    
r   )__doc____all__r   r   r*   r    r   rP   rO   r   r   r   r   r   r   <module>   s   %Pb