eb5f36a813a30b7011d8b50a49eb29081f97a3c6
[ghc-base.git] / GHC / ConsoleHandler.hs
1 {-# OPTIONS_GHC -cpp #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  GHC.ConsoleHandler
5 -- Copyright   :  (c) The University of Glasgow
6 -- License     :  see libraries/base/LICENSE
7 -- 
8 -- Maintainer  :  cvs-ghc@haskell.org
9 -- Stability   :  internal
10 -- Portability :  non-portable (GHC extensions)
11 --
12 -- NB. the contents of this module are only available on Windows.
13 --
14 -- Installing Win32 console handlers.
15 -- 
16 -----------------------------------------------------------------------------
17
18 module GHC.ConsoleHandler
19 #if !defined(mingw32_HOST_OS) && !defined(__HADDOCK__)
20         where
21 #else /* whole file */
22         ( Handler(..)
23         , installHandler
24         , ConsoleEvent(..)
25         , flushConsole
26         ) where
27
28 {-
29 #include "rts/Signals.h"
30 -}
31
32 import Foreign
33 import Foreign.C
34 import GHC.IO.FD
35 import GHC.IO.Exception
36 import GHC.IO.Handle.Types
37 import GHC.IO.Handle.Internals
38 import GHC.Conc
39 import Control.Concurrent.MVar
40 import Data.Typeable
41
42 #ifdef mingw32_HOST_OS
43 import Data.Maybe
44 import GHC.Base
45 #endif
46
47 data Handler
48  = Default
49  | Ignore
50  | Catch (ConsoleEvent -> IO ())
51
52 -- | Allows Windows console events to be caught and handled.  To
53 -- handle a console event, call 'installHandler' passing the
54 -- appropriate 'Handler' value.  When the event is received, if the
55 -- 'Handler' value is @Catch f@, then a new thread will be spawned by
56 -- the system to execute @f e@, where @e@ is the 'ConsoleEvent' that
57 -- was received.
58 --
59 -- Note that console events can only be received by an application
60 -- running in a Windows console.  Certain environments that look like consoles
61 -- do not support console events, these include:
62 --
63 --  * Cygwin shells with @CYGWIN=tty@ set (if you don't set @CYGWIN=tty@,
64 --    then a Cygwin shell behaves like a Windows console).
65 --  * Cygwin xterm and rxvt windows
66 --  * MSYS rxvt windows
67 --
68 -- In order for your application to receive console events, avoid running
69 -- it in one of these environments.
70 --
71 installHandler :: Handler -> IO Handler
72 installHandler handler
73   | threaded =
74     modifyMVar win32ConsoleHandler $ \old_h -> do
75       (new_h,rc) <-
76         case handler of
77           Default -> do
78             r <- rts_installHandler STG_SIG_DFL nullPtr
79             return (no_handler, r)
80           Ignore  -> do
81             r <- rts_installHandler STG_SIG_IGN nullPtr
82             return (no_handler, r)
83           Catch h -> do
84             r <- rts_installHandler STG_SIG_HAN nullPtr
85             return (h, r)
86       prev_handler <-
87         case rc of
88           STG_SIG_DFL -> return Default
89           STG_SIG_IGN -> return Ignore
90           STG_SIG_HAN -> return (Catch old_h)
91           _           -> error "installHandler: Bad threaded rc value"
92       return (new_h, prev_handler)
93
94   | otherwise =
95   alloca $ \ p_sp -> do
96    rc <-
97     case handler of
98      Default -> rts_installHandler STG_SIG_DFL p_sp
99      Ignore  -> rts_installHandler STG_SIG_IGN p_sp
100      Catch h -> do
101         v <- newStablePtr (toHandler h)
102         poke p_sp v
103         rts_installHandler STG_SIG_HAN p_sp
104    case rc of
105      STG_SIG_DFL -> return Default
106      STG_SIG_IGN -> return Ignore
107      STG_SIG_HAN -> do
108         osptr <- peek p_sp
109         oldh  <- deRefStablePtr osptr
110          -- stable pointer is no longer in use, free it.
111         freeStablePtr osptr
112         return (Catch (\ ev -> oldh (fromConsoleEvent ev)))
113      _           -> error "installHandler: Bad non-threaded rc value"
114   where
115    fromConsoleEvent ev =
116      case ev of
117        ControlC -> 0 {- CTRL_C_EVENT-}
118        Break    -> 1 {- CTRL_BREAK_EVENT-}
119        Close    -> 2 {- CTRL_CLOSE_EVENT-}
120        Logoff   -> 5 {- CTRL_LOGOFF_EVENT-}
121        Shutdown -> 6 {- CTRL_SHUTDOWN_EVENT-}
122
123    toHandler hdlr ev = do
124       case toWin32ConsoleEvent ev of
125          -- see rts/win32/ConsoleHandler.c for comments as to why
126          -- rts_ConsoleHandlerDone is called here.
127         Just x  -> hdlr x >> rts_ConsoleHandlerDone ev
128         Nothing -> return () -- silently ignore..
129
130    no_handler = error "win32ConsoleHandler"
131
132 foreign import ccall "rtsSupportsBoundThreads" threaded :: Bool
133
134 foreign import ccall unsafe "RtsExternal.h rts_InstallConsoleEvent" 
135   rts_installHandler :: CInt -> Ptr (StablePtr (CInt -> IO ())) -> IO CInt
136 foreign import ccall unsafe "RtsExternal.h rts_ConsoleHandlerDone"
137   rts_ConsoleHandlerDone :: CInt -> IO ()
138
139
140 flushConsole :: Handle -> IO ()
141 flushConsole h =
142   wantReadableHandle_ "flushConsole" h $ \ Handle__{haDevice=dev} ->
143     case cast dev of
144       Nothing -> ioException $
145                     IOError (Just h) IllegalOperation "flushConsole"
146                         "handle is not a file descriptor" Nothing Nothing
147       Just fd -> do
148         throwErrnoIfMinus1Retry_ "flushConsole" $
149            flush_console_fd (fdFD fd)
150
151 foreign import ccall unsafe "consUtils.h flush_input_console__"
152         flush_console_fd :: CInt -> IO CInt
153
154 #endif /* mingw32_HOST_OS */