1 {-# OPTIONS_GHC -cpp #-}
2 -----------------------------------------------------------------------------
4 -- Module : GHC.ConsoleHandler
5 -- Copyright : (c) The University of Glasgow
6 -- License : see libraries/base/LICENSE
8 -- Maintainer : cvs-ghc@haskell.org
9 -- Stability : internal
10 -- Portability : non-portable (GHC extensions)
12 -- NB. the contents of this module are only available on Windows.
14 -- Installing Win32 console handlers.
16 -----------------------------------------------------------------------------
18 module GHC.ConsoleHandler
19 #if !defined(mingw32_HOST_OS) && !defined(__HADDOCK__)
21 import Prelude -- necessary to get dependencies right
22 #else /* whole file */
33 import Prelude -- necessary to get dependencies right
38 import GHC.IO.Exception
39 import GHC.IO.Handle.Types
40 import GHC.IO.Handle.Internals
42 import Control.Concurrent.MVar
48 | Catch (ConsoleEvent -> IO ())
50 -- | Allows Windows console events to be caught and handled. To
51 -- handle a console event, call 'installHandler' passing the
52 -- appropriate 'Handler' value. When the event is received, if the
53 -- 'Handler' value is @Catch f@, then a new thread will be spawned by
54 -- the system to execute @f e@, where @e@ is the 'ConsoleEvent' that
57 -- Note that console events can only be received by an application
58 -- running in a Windows console. Certain environments that look like consoles
59 -- do not support console events, these include:
61 -- * Cygwin shells with @CYGWIN=tty@ set (if you don't set @CYGWIN=tty@,
62 -- then a Cygwin shell behaves like a Windows console).
63 -- * Cygwin xterm and rxvt windows
64 -- * MSYS rxvt windows
66 -- In order for your application to receive console events, avoid running
67 -- it in one of these environments.
69 installHandler :: Handler -> IO Handler
70 installHandler handler
72 modifyMVar win32ConsoleHandler $ \old_h -> do
76 r <- rts_installHandler STG_SIG_DFL nullPtr
77 return (no_handler, r)
79 r <- rts_installHandler STG_SIG_IGN nullPtr
80 return (no_handler, r)
82 r <- rts_installHandler STG_SIG_HAN nullPtr
86 STG_SIG_DFL -> return Default
87 STG_SIG_IGN -> return Ignore
88 STG_SIG_HAN -> return (Catch old_h)
89 _ -> error "installHandler: Bad threaded rc value"
90 return (new_h, prev_handler)
96 Default -> rts_installHandler STG_SIG_DFL p_sp
97 Ignore -> rts_installHandler STG_SIG_IGN p_sp
99 v <- newStablePtr (toHandler h)
101 rts_installHandler STG_SIG_HAN p_sp
103 STG_SIG_DFL -> return Default
104 STG_SIG_IGN -> return Ignore
107 oldh <- deRefStablePtr osptr
108 -- stable pointer is no longer in use, free it.
110 return (Catch (\ ev -> oldh (fromConsoleEvent ev)))
111 _ -> error "installHandler: Bad non-threaded rc value"
113 fromConsoleEvent ev =
115 ControlC -> 0 {- CTRL_C_EVENT-}
116 Break -> 1 {- CTRL_BREAK_EVENT-}
117 Close -> 2 {- CTRL_CLOSE_EVENT-}
118 Logoff -> 5 {- CTRL_LOGOFF_EVENT-}
119 Shutdown -> 6 {- CTRL_SHUTDOWN_EVENT-}
121 toHandler hdlr ev = do
122 case toWin32ConsoleEvent ev of
123 -- see rts/win32/ConsoleHandler.c for comments as to why
124 -- rts_ConsoleHandlerDone is called here.
125 Just x -> hdlr x >> rts_ConsoleHandlerDone ev
126 Nothing -> return () -- silently ignore..
128 no_handler = error "win32ConsoleHandler"
130 foreign import ccall "rtsSupportsBoundThreads" threaded :: Bool
132 foreign import ccall unsafe "RtsExternal.h rts_InstallConsoleEvent"
133 rts_installHandler :: CInt -> Ptr (StablePtr (CInt -> IO ())) -> IO CInt
134 foreign import ccall unsafe "RtsExternal.h rts_ConsoleHandlerDone"
135 rts_ConsoleHandlerDone :: CInt -> IO ()
138 flushConsole :: Handle -> IO ()
140 wantReadableHandle_ "flushConsole" h $ \ Handle__{haDevice=dev} ->
142 Nothing -> ioException $
143 IOError (Just h) IllegalOperation "flushConsole"
144 "handle is not a file descriptor" Nothing Nothing
146 throwErrnoIfMinus1Retry_ "flushConsole" $
147 flush_console_fd (fromIntegral (fdFD fd))
149 foreign import ccall unsafe "consUtils.h flush_input_console__"
150 flush_console_fd :: CInt -> IO CInt
152 #endif /* mingw32_HOST_OS */