From 0e1add6fae63eb51c5f73caf4dd42838ec955a86 Mon Sep 17 00:00:00 2001 From: Hannes Schulze Date: Sun, 4 Aug 2024 17:31:56 +0200 Subject: implement signal handling --- highway/Main.hs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'highway/Main.hs') diff --git a/highway/Main.hs b/highway/Main.hs index 17b768d..d387466 100644 --- a/highway/Main.hs +++ b/highway/Main.hs @@ -2,7 +2,7 @@ import Wayland.Server import Lens.Micro.TH (makeLenses) -import Lens.Micro.Mtl ((%=)) +import Lens.Micro.Mtl ((%=), (<%=)) import Data.Data (Typeable) import Control.Monad (void) import Control.Monad.Trans (liftIO) @@ -24,9 +24,21 @@ data BindRequest = BindOutput WLOutput.Handle deriving (Typeable, Show) +data SignalRequest + = ReqSIGINT + deriving (Typeable, Show) + +instance RequestClass SignalRequest +instance RequestClass BindRequest + data Output = Output + deriving (Show) + +data State = State { _outputs :: M.Map WLOutput.Handle Output + , _interruptedCount :: Int + } + deriving (Show) -data State = State { _outputs :: M.Map WLOutput.Handle Output } makeLenses ''State handleRequest :: Request -> DisplayM State () @@ -37,12 +49,16 @@ handleRequest r sendEvent o $ WLOutput.EvGeometry geometry sendEvent o $ WLOutput.EvDone flushEvents + | Just (ReqSIGINT) <- fromRequest r = do c <- interruptedCount <%= (+1) + liftIO $ putStrLn $ "Received SIGINT " ++ show c ++ "/3" + if c >= 3 then requestTermination else pure () | otherwise = pure () main :: IO () -main = void $ withDisplay (State M.empty) $ do +main = void $ withDisplay (State M.empty 0) $ do socket <- bindSocket liftIO $ putStrLn $ "Listening on socket: " ++ socket + registerSignalEvent ReqSIGINT 2 output <- registerGlobal BindOutput processRequests handleRequest unregisterGlobal output -- cgit v1.3.1