{-# 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 handler io
= bracket (do hd <- mkHandler handler
void $ c_SetConsoleCtrlHandler hd True
return hd)
(\hd -> void $ c_SetConsoleCtrlHandler hd False)
$ const io
setConsoleCtrlHandler :: PHANDLER_ROUTINE -> BOOL -> IO ()
setConsoleCtrlHandler handler flag
= failIfFalse_ "SetConsoleCtrlHandler"
$ c_SetConsoleCtrlHandler handler 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