
    4                         S r SSKJr  SSKJr  SSKJr  SSKJrJ	r	  SSK
Jr  SS	KJr  SS
KJrJr  SSKJrJrJr  SSKrSSKrSrSr\" S\	-  5      r " S S\5      rSS jrS rg)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S rSrSrSS jrS rSS jrS rS r	S	 r
SS
 jrS rS rS rSrg)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                     Ub  [        U5      (       d   e[        5       U l        [        US9U l        / U l        SU l        SU l        0 U l        U(       a  [        U5      U l
        g S U l
        g )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__Win32EventLoop.__init__$   si     HY$7$777#o%7%X"$&!  BK"29"=PT    c                   ^ ^
 T R                   (       a  [        S5      e[        m
ST l        T R                  (       Gar  [	        5        nT R
                  (       a"  U
U 4S jnT R
                  R                  U5        S S S 5        T
S:X  a  SnO%[        ST
[        SWR                  -  5      -
  5      nT R                  U5      nUT R                  R                  :X  a;  T R                  R                  5       nU H  nUR                  U5        M     [        m
OUT R                  :X  a:  [         R"                  R%                  T R                  5        T R'                  5         O9UT R(                  ;   a  T R(                  U   n	U	" 5         OUR+                  5         Sm
T R                  (       a  GMq  g g ! , (       d  f       GN<= f)NzEvent loop already closed.Tc                 Z   > [        TR                  U (       a
  T5      5      $ S5      5      $ )zETrue when there is input ready. The inputhook should return control. r   )bool_ready_for_reading)waitcurrent_timeoutr    s    r"   ready!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_timeoutr5   keyskcallbackr+   s   `         @r"   runWin32EventLoop.run4   sX   ;;899*mmm_**] ++::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 mmms   4F11
G c                     U R                   U R                  R                  /nUR                  U R                  R                  5       5        [        X!5      $ )zC
Return the handle that is ready for reading or `None` on timeout.
)r   r   r5   extendr   r@   _wait_for_handles)r    timeouthandless      r"   r)   !Win32EventLoop._ready_for_readinga   sB     ;; : : A ABt~~**,- 22r%   c                     SU l         g )NF)r   r    s    r"   stopWin32EventLoop.stopi   s	    r%   c                     SU l         [        R                  R                  U R                  5        U R
                  (       a  U R
                  R                  5         U R                  R                  5         g NT)r   r   r8   CloseHandler   r   closer   rL   s    r"   rR   Win32EventLoop.closel   sN     	##DKK0""##))+""((*r%   c                 4   ^ U4S jnU R                  U5        g)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                  J   > [         R                  " T S9R                  5         g )N)target)	threadingThreadstart)rB   s   r"   start_executor6Win32EventLoop.run_in_executor.<locals>.start_executor   s    H-335r%   N)call_from_executor)r    rB   rZ   s    ` r"   run_in_executorWin32EventLoop.run_in_executorw   s    	6/r%   c                     U R                   R                  U5        [        R                  R	                  U R
                  5        g)zU
Call this function in the main event loop.
Similar to Twisted's ``callFromThread``.
N)r   appendr   r8   SetEventr   )r    rB   _max_postpone_untils      r"   r\   !Win32EventLoop.call_from_executor   s0     	!!((2 	  -r%   c                 J    U R                   / sol         U H
  nU" 5         M     g N)r   )r    calls_from_executorcs      r"   r:   2Win32EventLoop._process_queued_calls_from_executor   s&    9=9R9RTV66$AC %r%   c                 L    [         R                  " U5      nX R                  U'   g)z:Start watching the file descriptor for read availability. Nmsvcrtget_osfhandler   )r    fdrB   hs       r"   
add_readerWin32EventLoop.add_reader   s      $$qr%   c                 j    [         R                  " U5      nX R                  ;   a  U R                  U	 gg)z9Stop watching the file descriptor for read availability. Nrj   )r    rm   rn   s      r"   remove_readerWin32EventLoop.remove_reader   s-      $q! r%   )r   r   r   r   r   r   r   rP   re   )__name__
__module____qualname____firstlineno____doc__r#   rC   r)   rM   rR   r]   r\   r:   ro   rr   __static_attributes__ r%   r"   r   r      s;    U +%Z3	+0	.%
"r%   r   c                     [         [        U 5      -  nU" U 6 n[        R                  R	                  [        U5      U[        S5      [        U5      5      nU[        :X  a  gX4   nU$ )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   r8   WaitForMultipleObjectsr   r   WAIT_TIMEOUT)rI   rH   arrtypehandle_arrayretrn   s         r"   rG   rG      sa     s7|#GG$L
//
0
0L<eeGnFC lr%   c                      [         R                  R                  [        [	        5       5      [        S5      [        S5      S5      $ )zq
Creates a Win32 unnamed Event .

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