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
44 | Catch (ConsoleEvent -> IO ())
50 -- these are sent to Services only.
53 deriving (Eq, Ord, Enum, Show, Read, Typeable)
55 -- | Allows Windows console events to be caught and handled. To
56 -- handle a console event, call 'installHandler' passing the
57 -- appropriate 'Handler' value. When the event is received, if the
58 -- 'Handler' value is @Catch f@, then a new thread will be spawned by
59 -- the system to execute @f e@, where @e@ is the 'ConsoleEvent' that
62 -- Note that console events can only be received by an application
63 -- running in a Windows console. Certain environments that look like consoles
64 -- do not support console events, these include:
66 -- * Cygwin shells with @CYGWIN=tty@ set (if you don't set @CYGWIN=tty@,
67 -- then a Cygwin shell behaves like a Windows console).
68 -- * Cygwin xterm and rxvt windows
69 -- * MSYS rxvt windows
71 -- In order for your application to receive console events, avoid running
72 -- it in one of these environments.
74 installHandler :: Handler -> IO Handler
75 installHandler handler =
79 Default -> rts_installHandler STG_SIG_DFL p_sp
80 Ignore -> rts_installHandler STG_SIG_IGN p_sp
82 v <- newStablePtr (toHandler h)
84 rts_installHandler STG_SIG_HAN p_sp
86 STG_SIG_DFL -> return Default
87 STG_SIG_IGN -> return Ignore
90 oldh <- deRefStablePtr osptr
91 -- stable pointer is no longer in use, free it.
93 return (Catch (\ ev -> oldh (fromConsoleEvent ev)))
97 0 {- CTRL_C_EVENT-} -> Just ControlC
98 1 {- CTRL_BREAK_EVENT-} -> Just Break
99 2 {- CTRL_CLOSE_EVENT-} -> Just Close
100 5 {- CTRL_LOGOFF_EVENT-} -> Just Logoff
101 6 {- CTRL_SHUTDOWN_EVENT-} -> Just Shutdown
103 fromConsoleEvent ev =
105 ControlC -> 0 {- CTRL_C_EVENT-}
106 Break -> 1 {- CTRL_BREAK_EVENT-}
107 Close -> 2 {- CTRL_CLOSE_EVENT-}
108 Logoff -> 5 {- CTRL_LOGOFF_EVENT-}
109 Shutdown -> 6 {- CTRL_SHUTDOWN_EVENT-}
111 toHandler hdlr ev = do
112 case toConsoleEvent ev of
113 -- see rts/win32/ConsoleHandler.c for comments as to why
114 -- rts_ConsoleHandlerDone is called here.
115 Just x -> hdlr x >> rts_ConsoleHandlerDone ev
116 Nothing -> return () -- silently ignore..
118 foreign import ccall unsafe "RtsExternal.h rts_InstallConsoleEvent"
119 rts_installHandler :: CInt -> Ptr (StablePtr (CInt -> IO ())) -> IO CInt
120 foreign import ccall unsafe "RtsExternal.h rts_ConsoleHandlerDone"
121 rts_ConsoleHandlerDone :: CInt -> IO ()
124 flushConsole :: Handle -> IO ()
126 wantReadableHandle "flushConsole" h $ \ h_ ->
127 throwErrnoIfMinus1Retry_ "flushConsole"
128 (flush_console_fd (fromIntegral (haFD h_)))
130 foreign import ccall unsafe "consUtils.h flush_input_console__"
131 flush_console_fd :: CInt -> IO CInt
132 #endif /* mingw32_HOST_OS */