summaryrefslogtreecommitdiffstats
path: root/tv/2configs/xserver/xmonad/Util/Shutdown.hs
blob: 89e22b4e7140dc6d87431a4d04d494fec5914c87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{-# LANGUAGE LambdaCase #-}
module Util.Shutdown
    ( sendShutdownEvent
    , handleShutdownEvent
    , shutdown
    )
  where

import Control.Monad
import Data.Monoid
import Data.Maybe (catMaybes)
import qualified Data.Map as Map
import System.Environment (getEnv)
import System.Exit (exitSuccess)
import XMonad
import qualified XMonad.StackSet as W

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 ()