summaryrefslogtreecommitdiffstats
path: root/XMonad/Stockholm/Shutdown.hs
diff options
context:
space:
mode:
Diffstat (limited to 'XMonad/Stockholm/Shutdown.hs')
-rw-r--r--XMonad/Stockholm/Shutdown.hs54
1 files changed, 54 insertions, 0 deletions
diff --git a/XMonad/Stockholm/Shutdown.hs b/XMonad/Stockholm/Shutdown.hs
new file mode 100644
index 0000000..164ddd8
--- /dev/null
+++ b/XMonad/Stockholm/Shutdown.hs
@@ -0,0 +1,54 @@
+{-# LANGUAGE LambdaCase #-}
+
+module XMonad.Stockholm.Shutdown
+ ( sendShutdownEvent
+ , handleShutdownEvent
+ , shutdown
+ )
+ where
+
+import qualified Data.Map as Map
+import qualified XMonad.StackSet as W
+import Control.Monad
+import Data.Maybe (catMaybes)
+import Data.Monoid
+import System.Environment (getEnv)
+import System.Exit (exitSuccess)
+import XMonad
+
+sendShutdownEvent :: IO ()
+sendShutdownEvent = do
+ dpy <- openDisplay ""
+ rw <- rootWindow dpy $ defaultScreen dpy
+ a <- internAtom dpy "XMONAD_SHUTDOWN" False
+ allocaXEvent $ \e -> do
+ setEventType e clientMessage
+ setClientMessageEvent e rw a 32 0 currentTime
+ sendEvent dpy rw False structureNotifyMask e
+ sync dpy False
+
+handleShutdownEvent :: Event -> X All
+handleShutdownEvent = \case
+ ClientMessageEvent { ev_message_type = mt } -> do
+ c <- (mt ==) <$> getAtom "XMONAD_SHUTDOWN"
+ when c shutdown
+ return (All c)
+ _ ->
+ return (All True)
+
+shutdown :: X ()
+shutdown = do
+ broadcastMessage ReleaseResources
+ io . flush =<< asks display
+ let wsData = show . W.mapLayout show . windowset
+ maybeShow (t, Right (PersistentExtension ext)) = Just (t, show ext)
+ maybeShow (t, Left str) = Just (t, str)
+ maybeShow _ = Nothing
+ extState =
+ return . show . catMaybes . map maybeShow . Map.toList . extensibleState
+ s <- gets (\s -> (wsData s : extState s))
+ _ <- io $ do
+ path <- getEnv "XMONAD_STATE"
+ writeFile path (unlines s)
+ exitSuccess
+ return ()