1 {-# OPTIONS_GHC -cpp #-}
2 -----------------------------------------------------------------------------
4 -- Module : GHC.ConsoleHandler
5 -- Copyright : whatevah
6 -- License : see libraries/base/LICENSE
8 -- Maintainer : cvs-ghc@haskell.org
9 -- Stability : internal
10 -- Portability : non-portable (GHC extensions)
12 -- Installing Win32 console handlers.
14 -----------------------------------------------------------------------------
16 module GHC.ConsoleHandler
17 #ifndef mingw32_HOST_OS
19 import Prelude -- necessary to get dependencies right
20 #else /* whole file */
31 import Prelude -- necessary to get dependencies right
41 | Catch (ConsoleEvent -> IO ())
47 -- these are sent to Services only.
51 installHandler :: Handler -> IO Handler
52 installHandler handler =
56 Default -> rts_installHandler STG_SIG_DFL p_sp
57 Ignore -> rts_installHandler STG_SIG_IGN p_sp
59 v <- newStablePtr (toHandler h)
61 rts_installHandler STG_SIG_HAN p_sp
63 STG_SIG_DFL -> return Default
64 STG_SIG_IGN -> return Ignore
67 oldh <- deRefStablePtr osptr
68 -- stable pointer is no longer in use, free it.
70 return (Catch (\ ev -> oldh (fromConsoleEvent ev)))
74 0 {- CTRL_C_EVENT-} -> Just ControlC
75 1 {- CTRL_BREAK_EVENT-} -> Just Break
76 2 {- CTRL_CLOSE_EVENT-} -> Just Close
77 5 {- CTRL_LOGOFF_EVENT-} -> Just Logoff
78 6 {- CTRL_SHUTDOWN_EVENT-} -> Just Shutdown
82 ControlC -> 0 {- CTRL_C_EVENT-}
83 Break -> 1 {- CTRL_BREAK_EVENT-}
84 Close -> 2 {- CTRL_CLOSE_EVENT-}
85 Logoff -> 5 {- CTRL_LOGOFF_EVENT-}
86 Shutdown -> 6 {- CTRL_SHUTDOWN_EVENT-}
88 toHandler hdlr ev = do
89 case toConsoleEvent ev of
90 -- see rts/win32/ConsoleHandler.c for comments as to why
91 -- rts_ConsoleHandlerDone is called here.
92 Just x -> hdlr x >> rts_ConsoleHandlerDone ev
93 Nothing -> return () -- silently ignore..
95 foreign import ccall unsafe "RtsExternal.h rts_InstallConsoleEvent"
96 rts_installHandler :: CInt -> Ptr (StablePtr (CInt -> IO ())) -> IO CInt
97 foreign import ccall unsafe "RtsExternal.h rts_ConsoleHandlerDone"
98 rts_ConsoleHandlerDone :: CInt -> IO ()
101 flushConsole :: Handle -> IO ()
103 wantReadableHandle "flushConsole" h $ \ h_ ->
104 throwErrnoIfMinus1Retry_ "flushConsole"
105 (flush_console_fd (fromIntegral (haFD h_)))
107 foreign import ccall unsafe "consUtils.h flush_input_console__"
108 flush_console_fd :: CInt -> IO CInt
109 #endif /* mingw32_HOST_OS */