{-# LANGUAGE CPP #-}
module System.Win32.Console.CtrlHandler
( CtrlEvent, Handler, PHANDLER_ROUTINE
, withConsoleCtrlHandler
, setConsoleCtrlHandler, c_SetConsoleCtrlHandler
, mkHandler
, cTRL_C_EVENT, cTRL_BREAK_EVENT
) where
import Control.Exception ( bracket )
import Control.Monad ( void )
import Foreign.Ptr ( FunPtr )
import System.Win32.Console ( CtrlEvent, cTRL_C_EVENT, cTRL_BREAK_EVENT )
import System.Win32.Types ( BOOL, failIfFalse_ )
#include "windows_cconv.h"
type Handler = CtrlEvent -> IO BOOL
type PHANDLER_ROUTINE = FunPtr Handler
withConsoleCtrlHandler :: Handler -> IO a -> IO a
withConsoleCtrlHandler :: forall a. Handler -> IO a -> IO a
withConsoleCtrlHandler Handler
handler IO a
io
= IO PHANDLER_ROUTINE
-> (PHANDLER_ROUTINE -> IO ())
-> (PHANDLER_ROUTINE -> IO a)
-> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (do PHANDLER_ROUTINE
hd <- Handler -> IO PHANDLER_ROUTINE
mkHandler Handler
handler
IO BOOL -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO BOOL -> IO ()) -> IO BOOL -> IO ()
forall a b. (a -> b) -> a -> b
$ PHANDLER_ROUTINE -> BOOL -> IO BOOL
c_SetConsoleCtrlHandler PHANDLER_ROUTINE
hd BOOL
True
PHANDLER_ROUTINE -> IO PHANDLER_ROUTINE
forall (m :: * -> *) a. Monad m => a -> m a
return PHANDLER_ROUTINE
hd)
(\PHANDLER_ROUTINE
hd -> IO BOOL -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO BOOL -> IO ()) -> IO BOOL -> IO ()
forall a b. (a -> b) -> a -> b
$ PHANDLER_ROUTINE -> BOOL -> IO BOOL
c_SetConsoleCtrlHandler PHANDLER_ROUTINE
hd BOOL
False)
((PHANDLER_ROUTINE -> IO a) -> IO a)
-> (PHANDLER_ROUTINE -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ IO a -> PHANDLER_ROUTINE -> IO a
forall a b. a -> b -> a
const IO a
io
setConsoleCtrlHandler :: PHANDLER_ROUTINE -> BOOL -> IO ()
setConsoleCtrlHandler :: PHANDLER_ROUTINE -> BOOL -> IO ()
setConsoleCtrlHandler PHANDLER_ROUTINE
handler BOOL
flag
= String -> IO BOOL -> IO ()
failIfFalse_ String
"SetConsoleCtrlHandler"
(IO BOOL -> IO ()) -> IO BOOL -> IO ()
forall a b. (a -> b) -> a -> b
$ PHANDLER_ROUTINE -> BOOL -> IO BOOL
c_SetConsoleCtrlHandler PHANDLER_ROUTINE
handler BOOL
flag
foreign import WINDOWS_CCONV "wrapper" mkHandler :: Handler -> IO PHANDLER_ROUTINE
foreign import WINDOWS_CCONV "windows.h SetConsoleCtrlHandler"
c_SetConsoleCtrlHandler :: PHANDLER_ROUTINE -> BOOL -> IO BOOL