
    4                         d Z ddlm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mZ ddlmZmZmZ ddlZddlZdZdZ ede	z        Z G d de      ZddZd Zy)zg
Win32 event loop.

Windows notes:
    - Somehow it doesn't seem to work with the 'ProactorEventLoop'.
    )unicode_literals   )ConsoleInputReader)SECURITY_ATTRIBUTES   )	EventLoopINPUT_TIMEOUT)InputHookContext)TimeIt)windllpointer)DWORDBOOLHANDLEN)Win32EventLoopi    c                   R    e Zd ZdZddZd ZddZd Zd Zd Z	dd	Z
d
 Zd Zd Zy)r   z
    Event loop for Windows systems.

    :param recognize_paste: When True, try to discover paste actions and turn
        the event into a BracketedPaste.
    Nc                     |t        |      sJ t               | _        t        |      | _        g | _        d| _        d| _        i | _        |rt        |      | _
        y d | _
        y )N)recognize_pasteF)callable_create_event_eventr   _console_input_reader_calls_from_executorclosed_running	_read_fdsr
   _inputhook_context)self	inputhookr   s      1lib/third_party/prompt_toolkit/eventloop/win32.py__init__zWin32EventLoop.__init__$   sf     HY$777#o%7%X"$&!  BK"29"=PT    c                 B   
  j                   rt        d      t        
d _         j                  r_t	               5 } j
                  r!
 fd} j
                  j                  |       d d d        
dk(  rd}n%t        d
t        dj                  z        z
        } j                  |      }| j                  j                  k(  r9 j                  j                         }|D ]  }|j                  |        t        
n| j                  k(  r:t         j"                  j%                   j                          j'                          n7| j(                  v r j(                  |   }	 |	        n|j+                          d
 j                  r^y y # 1 sw Y   *xY w)NzEvent loop already closed.Tc                 P    t        j                  | r
            S d            S )zF True when there is input ready. The inputhook should return control. r   )bool_ready_for_reading)waitcurrent_timeoutr   s    r!   readyz!Win32EventLoop.run.<locals>.ready?   s'    #D$;$;tO$[\\YZ$[\\r#   r   r   )r   	ExceptionINPUT_TIMEOUT_MSr   r   r   call_inputhookmaxintdurationr'   r   handlereadfeed_keyr   r   kernel32
ResetEvent#_process_queued_calls_from_executorr   input_timeout)r   stdin	callbacksinputhook_timerr*   remaining_timeoutr2   keyskcallbackr)   s   `         @r!   runzWin32EventLoop.run4   sT   ;;899*mm_**] ++::5A  "$$&!$'?SH`H`A`=a+a$b! ,,->?F33:::11668A&&q) "24;;&**4;;788:4>>)>>&1
 '')"$I mms   .FFc                     | j                   | j                  j                  g}|j                  | j                  j                                t        ||      S )zS
        Return the handle that is ready for reading or `None` on timeout.
        )r   r   r2   extendr   r=   _wait_for_handles)r   timeouthandless      r!   r'   z!Win32EventLoop._ready_for_readinga   sD     ;; : : A ABt~~**,- '22r#   c                     d| _         y )NF)r   r   s    r!   stopzWin32EventLoop.stopi   s	    r#   c                     d| _         t        j                  j                  | j                         | j
                  r| j
                  j                          | j                  j                          y NT)r   r   r5   CloseHandler   r   closer   rG   s    r!   rL   zWin32EventLoop.closel   sN     	##DKK0""##))+""((*r#   c                 2    fd}| j                  |       y)z
        Run a long running function in a background thread.
        (This is recommended for code that could block the event loop.)
        Similar to Twisted's ``deferToThread``.
        c                  N    t        j                         j                          y )N)target)	threadingThreadstart)r?   s   r!   start_executorz6Win32EventLoop.run_in_executor.<locals>.start_executor   s    H-335r#   N)call_from_executor)r   r?   rS   s    ` r!   run_in_executorzWin32EventLoop.run_in_executorw   s    	6/r#   c                     | j                   j                  |       t        j                  j	                  | j
                         y)zm
        Call this function in the main event loop.
        Similar to Twisted's ``callFromThread``.
        N)r   appendr   r5   SetEventr   )r   r?   _max_postpone_untils      r!   rT   z!Win32EventLoop.call_from_executor   s0     	!!((2 	  -r#   c                 H    | j                   g c}| _         |D ]	  } |         y N)r   )r   calls_from_executorcs      r!   r7   z2Win32EventLoop._process_queued_calls_from_executor   s(    9=9R9RTV6T6$AC %r#   c                 L    t        j                  |      }|| j                  |<   y)z; Start watching the file descriptor for read availability. Nmsvcrtget_osfhandler   )r   fdr?   hs       r!   
add_readerzWin32EventLoop.add_reader   s       $$qr#   c                 f    t        j                  |      }|| j                  v r| j                  |= yy)z: Stop watching the file descriptor for read availability. Nr_   )r   rb   rc   s      r!   remove_readerzWin32EventLoop.remove_reader   s/      $q! r#   rJ   r[   )__name__
__module____qualname____doc__r"   r@   r'   rH   rL   rU   rT   r7   rd   rf    r#   r!   r   r      s;    U +%Z3	+0	.%
"r#   r   c                     t         t        |       z  } ||  }t        j                  j	                  t        |      |t        d      t        |            }|t        k(  ry||   }|S )z
    Waits for multiple handles. (Similar to 'select') Returns the handle which is ready.
    Returns `None` on timeout.

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms687025(v=vs.85).aspx
    FN)r   lenr   r5   WaitForMultipleObjectsr   r   WAIT_TIMEOUT)rE   rD   arrtypehandle_arrayretrc   s         r!   rC   rC      sc     s7|#GG$L
//
0
0L<eeGnFC lr#   c                      t         j                  j                  t        t	                     t        d      t        d      d      S )z}
    Creates a Win32 unnamed Event .

    http://msdn.microsoft.com/en-us/library/windows/desktop/ms682396(v=vs.85).aspx
    TFN)r   r5   CreateEventAr   r   r   rk   r#   r!   r   r      s4     ??''0C0E(FT
TXY^T_aeffr#   )r+   )rj   
__future__r   terminal.win32_inputr   win32_typesr   baser   r	   r    r
   utilsr   ctypesr   r   ctypes.wintypesr   r   r   r`   rP   __all__ro   r0   r-   r   rC   r   rk   r#   r!   <module>r}      se    ( 5 - * '  " / /   tm+, A"Y A"H(gr#   